Get rid of global variables mc_main_config and mc_panels_config.
[midnight-commander.git] / src / filemanager / layout.c
blobc9dabd642e3e7adbde4899060e8ebc5091420667
1 /*
2 Panel layout module for the Midnight Commander
4 Copyright (C) 1995-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Janne Kukonlehto, 1995
9 Miguel de Icaza, 1995
10 Andrew Borodin <aborodin@vmail.ru>, 2011, 2012, 2013
11 Slava Zanko <slavazanko@gmail.com>, 2013
12 Avi Kelman <patcherton.fixesthings@gmail.com>, 2013
14 This file is part of the Midnight Commander.
16 The Midnight Commander is free software: you can redistribute it
17 and/or modify it under the terms of the GNU General Public License as
18 published by the Free Software Foundation, either version 3 of the License,
19 or (at your option) any later version.
21 The Midnight Commander is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 /** \file layout.c
31 * \brief Source: panel layout module
34 #include <config.h>
36 #include <pwd.h> /* for username in xterm title */
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <unistd.h>
43 #include "lib/global.h"
44 #include "lib/tty/tty.h"
45 #include "lib/skin.h"
46 #include "lib/tty/key.h"
47 #include "lib/tty/mouse.h"
48 #include "lib/mcconfig.h"
49 #include "lib/vfs/vfs.h" /* For _vfs_get_cwd () */
50 #include "lib/strutil.h"
51 #include "lib/widget.h"
52 #include "lib/event.h"
54 #include "src/consaver/cons.saver.h"
55 #include "src/viewer/mcviewer.h" /* The view widget */
56 #include "src/setup.h"
57 #ifdef ENABLE_SUBSHELL
58 #include "src/subshell/subshell.h"
59 #endif
61 #include "command.h"
62 #include "midnight.h"
63 #include "tree.h"
64 /* Needed for the extern declarations of integer parameters */
65 #include "dir.h"
66 #include "layout.h"
67 #include "info.h" /* The Info widget */
69 /*** global variables ****************************************************************************/
71 panels_layout_t panels_layout = {
72 /* Set if the panels are split horizontally */
73 .horizontal_split = 0,
75 /* vertical split */
76 .vertical_equal = 1,
77 .left_panel_size = 0,
79 /* horizontal split */
80 .horizontal_equal = 1,
81 .top_panel_size = 0
84 /* Controls the display of the rotating dash on the verbose mode */
85 int nice_rotating_dash = 1;
87 /* The number of output lines shown (if available) */
88 int output_lines = 0;
90 /* Set if the command prompt is to be displayed */
91 gboolean command_prompt = TRUE;
93 /* Set if the main menu is visible */
94 int menubar_visible = 1;
96 /* Set to show current working dir in xterm window title */
97 gboolean xterm_title = TRUE;
99 /* Set to show free space on device assigned to current directory */
100 int free_space = 1;
102 /* The starting line for the output of the subprogram */
103 int output_start_y = 0;
105 int ok_to_refresh = 1;
107 /*** file scope macro definitions ****************************************************************/
109 /* The maximum number of views managed by the set_display_type routine */
110 /* Must be at least two (for current and other). Please note that until */
111 /* Janne gets around this, we will only manage two of them :-) */
112 #define MAX_VIEWS 2
114 /* Width 12 for a wee Quick (Hex) View */
115 #define MINWIDTH 12
116 #define MINHEIGHT 5
118 #define B_2LEFT B_USER
119 #define B_2RIGHT (B_USER + 1)
120 #define B_PLUS (B_USER + 2)
121 #define B_MINUS (B_USER + 3)
123 #define LAYOUT_OPTIONS_COUNT G_N_ELEMENTS (check_options)
125 /*** file scope type declarations ****************************************************************/
127 /*** file scope variables ************************************************************************/
129 static struct
131 panel_view_mode_t type;
132 Widget *widget;
133 char *last_saved_dir; /* last view_list working directory */
134 } panels[MAX_VIEWS] =
136 /* *INDENT-OFF* */
137 /* init MAX_VIEWS items */
138 { view_listing, NULL, NULL},
139 { view_listing, NULL, NULL}
140 /* *INDENT-ON* */
143 /* These variables are used to avoid updating the information unless */
144 /* we need it */
145 static panels_layout_t old_layout;
146 static int old_output_lines;
148 /* Internal variables */
149 static int equal_split;
150 static int _output_lines;
152 static int height;
154 static WRadio *radio_widget;
156 static struct
158 const char *text;
159 int *variable;
160 WCheck *widget;
161 } check_options[] =
163 /* *INDENT-OFF* */
164 { N_("&Equal split"), &equal_split, NULL },
165 { N_("&Menubar visible"), &menubar_visible, NULL },
166 { N_("Command &prompt"), &command_prompt, NULL },
167 { N_("&Keybar visible"), &mc_global.keybar_visible, NULL },
168 { N_("H&intbar visible"), &mc_global.message_visible, NULL },
169 { N_("&XTerm window title"), &xterm_title, NULL },
170 { N_("&Show free space"), &free_space, NULL }
171 /* *INDENT-ON* */
174 static const char *output_lines_label = NULL;
175 static int output_lines_label_len;
177 static WButton *bleft_widget, *bright_widget;
179 /*** file scope functions ************************************************************************/
180 /* --------------------------------------------------------------------------------------------- */
182 /* don't use max() macro to avoid double call of str_term_width1() in widget width calculation */
183 #undef max
185 static int
186 max (int a, int b)
188 return a > b ? a : b;
191 /* --------------------------------------------------------------------------------------------- */
193 static void
194 check_split (panels_layout_t * layout)
196 if (layout->horizontal_split)
198 if (layout->horizontal_equal)
199 layout->top_panel_size = height / 2;
200 else if (layout->top_panel_size < MINHEIGHT)
201 layout->top_panel_size = MINHEIGHT;
202 else if (layout->top_panel_size > height - MINHEIGHT)
203 layout->top_panel_size = height - MINHEIGHT;
205 else
207 if (layout->vertical_equal)
208 layout->left_panel_size = COLS / 2;
209 else if (layout->left_panel_size < MINWIDTH)
210 layout->left_panel_size = MINWIDTH;
211 else if (layout->left_panel_size > COLS - MINWIDTH)
212 layout->left_panel_size = COLS - MINWIDTH;
216 /* --------------------------------------------------------------------------------------------- */
218 static void
219 update_split (const WDialog * h)
221 /* Check split has to be done before testing if it changed, since
222 it can change due to calling check_split() as well */
223 check_split (&panels_layout);
225 if (panels_layout.horizontal_split)
226 check_options[0].widget->state = panels_layout.horizontal_equal ? 1 : 0;
227 else
228 check_options[0].widget->state = panels_layout.vertical_equal ? 1 : 0;
229 widget_redraw (WIDGET (check_options[0].widget));
231 tty_setcolor ((check_options[0].widget->state & C_BOOL) ? DISABLED_COLOR : COLOR_NORMAL);
233 widget_move (h, 6, 5);
234 if (panels_layout.horizontal_split)
235 tty_printf ("%03d", panels_layout.top_panel_size);
236 else
237 tty_printf ("%03d", panels_layout.left_panel_size);
239 widget_move (h, 6, 17);
240 if (panels_layout.horizontal_split)
241 tty_printf ("%03d", height - panels_layout.top_panel_size);
242 else
243 tty_printf ("%03d", COLS - panels_layout.left_panel_size);
245 widget_move (h, 6, 12);
246 tty_print_char ('=');
249 /* --------------------------------------------------------------------------------------------- */
251 static int
252 b_left_right_cback (WButton * button, int action)
254 (void) action;
256 if (button == bright_widget)
258 if (panels_layout.horizontal_split)
259 panels_layout.top_panel_size++;
260 else
261 panels_layout.left_panel_size++;
263 else
265 if (panels_layout.horizontal_split)
266 panels_layout.top_panel_size--;
267 else
268 panels_layout.left_panel_size--;
271 update_split (WIDGET (button)->owner);
272 layout_change ();
273 do_refresh ();
274 return 0;
277 /* --------------------------------------------------------------------------------------------- */
279 static int
280 bplus_cback (WButton * button, int action)
282 (void) button;
283 (void) action;
285 if (_output_lines < 99)
286 _output_lines++;
287 return 0;
290 /* --------------------------------------------------------------------------------------------- */
292 static int
293 bminus_cback (WButton * button, int action)
295 (void) button;
296 (void) action;
298 if (_output_lines > 0)
299 _output_lines--;
300 return 0;
303 /* --------------------------------------------------------------------------------------------- */
305 static cb_ret_t
306 layout_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
308 WDialog *h = DIALOG (w);
310 switch (msg)
312 case MSG_DRAW:
313 /* When repainting the whole dialog (e.g. with C-l) we have to
314 update everything */
315 dlg_default_repaint (h);
317 old_output_lines = -1;
319 update_split (h);
321 if (old_output_lines != _output_lines)
323 old_output_lines = _output_lines;
324 tty_setcolor (mc_global.tty.console_flag != '\0' ? COLOR_NORMAL : DISABLED_COLOR);
325 widget_move (h, 9, 5);
326 tty_print_string (output_lines_label);
327 widget_move (h, 9, 5 + 3 + output_lines_label_len);
328 tty_printf ("%02d", _output_lines);
330 return MSG_HANDLED;
332 case MSG_POST_KEY:
334 int _menubar_visible, _command_prompt, _keybar_visible, _message_visible;
336 _menubar_visible = check_options[1].widget->state & C_BOOL;
337 _command_prompt = check_options[2].widget->state & C_BOOL;
338 _keybar_visible = check_options[3].widget->state & C_BOOL;
339 _message_visible = check_options[4].widget->state & C_BOOL;
341 if (mc_global.tty.console_flag != '\0')
343 int minimum;
345 if (_output_lines < 0)
346 _output_lines = 0;
347 height =
348 LINES - _keybar_visible - _command_prompt - _menubar_visible - _output_lines -
349 _message_visible;
350 minimum = MINHEIGHT * (1 + panels_layout.horizontal_split);
351 if (height < minimum)
353 _output_lines -= minimum - height;
354 height = minimum;
357 else
358 height =
359 LINES - _keybar_visible - _command_prompt - _menubar_visible - _output_lines -
360 _message_visible;
362 if (old_output_lines != _output_lines)
364 old_output_lines = _output_lines;
365 tty_setcolor (mc_global.tty.console_flag != '\0' ? COLOR_NORMAL : DISABLED_COLOR);
366 widget_move (h, 9, 5 + 3 + output_lines_label_len);
367 tty_printf ("%02d", _output_lines);
370 return MSG_HANDLED;
372 case MSG_NOTIFY:
373 if (sender == WIDGET (radio_widget))
375 if (panels_layout.horizontal_split != radio_widget->sel)
377 int eq;
379 panels_layout.horizontal_split = radio_widget->sel;
381 if (panels_layout.horizontal_split)
383 eq = panels_layout.horizontal_equal;
384 if (eq)
385 panels_layout.top_panel_size = height / 2;
387 else
389 eq = panels_layout.vertical_equal;
390 if (eq)
391 panels_layout.left_panel_size = COLS / 2;
394 widget_disable (WIDGET (bleft_widget), eq);
395 widget_disable (WIDGET (bright_widget), eq);
397 update_split (h);
398 layout_change ();
399 do_refresh ();
401 else
402 update_split (h);
404 return MSG_HANDLED;
407 if (sender == WIDGET (check_options[0].widget))
409 int eq;
411 if (panels_layout.horizontal_split)
413 panels_layout.horizontal_equal = check_options[0].widget->state & C_BOOL;
414 eq = panels_layout.horizontal_equal;
416 else
418 panels_layout.vertical_equal = check_options[0].widget->state & C_BOOL;
419 eq = panels_layout.vertical_equal;
422 widget_disable (WIDGET (bleft_widget), eq);
423 widget_disable (WIDGET (bright_widget), eq);
425 update_split (h);
426 layout_change ();
427 do_refresh ();
429 return MSG_HANDLED;
432 return MSG_NOT_HANDLED;
434 default:
435 return dlg_default_callback (w, sender, msg, parm, data);
439 /* --------------------------------------------------------------------------------------------- */
441 static WDialog *
442 init_layout (void)
444 WDialog *layout_dlg;
445 int l1 = 0, width;
446 int b1, b2, b;
447 size_t i;
449 const char *title1 = N_("Panel split");
450 const char *title2 = N_("Console output");
451 const char *title3 = N_("Other options");
453 const char *s_split_direction[2] = {
454 N_("&Vertical"),
455 N_("&Horizontal")
458 const char *ok_button = N_("&OK");
459 const char *cancel_button = N_("&Cancel");
461 output_lines_label = _("Output lines:");
463 /* save old params */
464 old_output_lines = -1;
465 _output_lines = output_lines;
467 #ifdef ENABLE_NLS
469 static gboolean i18n = FALSE;
471 title1 = _(title1);
472 title2 = _(title2);
473 title3 = _(title3);
475 i = G_N_ELEMENTS (s_split_direction);
476 while (i-- != 0)
477 s_split_direction[i] = _(s_split_direction[i]);
479 if (!i18n)
481 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
482 check_options[i].text = _(check_options[i].text);
483 i18n = TRUE;
486 ok_button = _(ok_button);
487 cancel_button = _(cancel_button);
489 #endif
491 /* radiobuttons */
492 i = G_N_ELEMENTS (s_split_direction);
493 while (i-- != 0)
494 l1 = max (l1, str_term_width1 (s_split_direction[i]) + 7);
495 /* checkboxes */
496 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
497 l1 = max (l1, str_term_width1 (check_options[i].text) + 7);
498 /* groupboxes */
499 l1 = max (l1, str_term_width1 (title1) + 4);
500 l1 = max (l1, str_term_width1 (title2) + 4);
501 l1 = max (l1, str_term_width1 (title3) + 4);
502 /* label + "+"/"-" buttons */
503 output_lines_label_len = str_term_width1 (output_lines_label);
504 l1 = max (l1, output_lines_label_len + 12);
505 /* buttons */
506 b1 = str_term_width1 (ok_button) + 5; /* default button */
507 b2 = str_term_width1 (cancel_button) + 3;
508 b = b1 + b2 + 1;
509 /* dialog width */
510 width = max (l1 * 2 + 7, b);
512 layout_dlg =
513 dlg_create (TRUE, 0, 0, 15, width, dialog_colors, layout_callback, NULL, "[Layout]",
514 _("Layout"), DLG_CENTER);
516 #define XTRACT(i) *check_options[i].variable, check_options[i].text
518 /* "Panel split" groupbox */
519 add_widget (layout_dlg, groupbox_new (2, 3, 6, l1, title1));
521 radio_widget = radio_new (3, 5, 2, s_split_direction);
522 radio_widget->sel = panels_layout.horizontal_split;
523 add_widget (layout_dlg, radio_widget);
525 check_options[0].widget = check_new (5, 5, XTRACT (0));
526 add_widget (layout_dlg, check_options[0].widget);
528 equal_split = panels_layout.horizontal_split ?
529 panels_layout.horizontal_equal : panels_layout.vertical_equal;
531 bleft_widget = button_new (6, 8, B_2LEFT, NARROW_BUTTON, "&<", b_left_right_cback);
532 widget_disable (WIDGET (bleft_widget), equal_split);
533 add_widget (layout_dlg, bleft_widget);
535 bright_widget = button_new (6, 14, B_2RIGHT, NARROW_BUTTON, "&>", b_left_right_cback);
536 widget_disable (WIDGET (bright_widget), equal_split);
537 add_widget (layout_dlg, bright_widget);
539 /* "Console output" groupbox */
541 const int disabled = mc_global.tty.console_flag != '\0' ? 0 : W_DISABLED;
542 Widget *w;
544 w = WIDGET (groupbox_new (8, 3, 3, l1, title2));
545 w->options |= disabled;
546 add_widget (layout_dlg, w);
548 w = WIDGET (button_new (9, output_lines_label_len + 5, B_PLUS,
549 NARROW_BUTTON, "&+", bplus_cback));
550 w->options |= disabled;
551 add_widget (layout_dlg, w);
553 w = WIDGET (button_new (9, output_lines_label_len + 5 + 5, B_MINUS,
554 NARROW_BUTTON, "&-", bminus_cback));
555 w->options |= disabled;
556 add_widget (layout_dlg, w);
559 /* "Other options" groupbox */
560 add_widget (layout_dlg, groupbox_new (2, 4 + l1, 9, l1, title3));
562 for (i = 1; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
564 check_options[i].widget = check_new (i + 2, 6 + l1, XTRACT (i));
565 add_widget (layout_dlg, check_options[i].widget);
568 #undef XTRACT
570 add_widget (layout_dlg, hline_new (11, -1, -1));
571 /* buttons */
572 add_widget (layout_dlg,
573 button_new (12, (width - b) / 2, B_ENTER, DEFPUSH_BUTTON, ok_button, 0));
574 add_widget (layout_dlg,
575 button_new (12, (width - b) / 2 + b1 + 1, B_CANCEL, NORMAL_BUTTON,
576 cancel_button, 0));
578 dlg_select_widget (radio_widget);
580 return layout_dlg;
583 /* --------------------------------------------------------------------------------------------- */
585 static void
586 panel_do_cols (int idx)
588 if (get_display_type (idx) == view_listing)
589 set_panel_formats (PANEL (panels[idx].widget));
590 else
591 panel_update_cols (panels[idx].widget, frame_half);
594 /* --------------------------------------------------------------------------------------------- */
595 /** Save current list_view widget directory into panel */
597 static Widget *
598 restore_into_right_dir_panel (int idx, Widget * from_widget)
600 WPanel *new_widget;
601 const char *saved_dir = panels[idx].last_saved_dir;
602 gboolean last_was_panel = (from_widget && get_display_type (idx) != view_listing);
603 const char *p_name = get_nth_panel_name (idx);
605 if (last_was_panel)
607 vfs_path_t *saved_dir_vpath;
609 saved_dir_vpath = vfs_path_from_str (saved_dir);
610 new_widget = panel_new_with_dir (p_name, saved_dir_vpath);
611 vfs_path_free (saved_dir_vpath);
613 else
614 new_widget = panel_new (p_name);
616 return WIDGET (new_widget);
619 /* --------------------------------------------------------------------------------------------- */
620 /*** public functions ****************************************************************************/
621 /* --------------------------------------------------------------------------------------------- */
623 void
624 layout_change (void)
626 setup_panels ();
627 /* update the main menu, because perhaps there was a change in the way
628 how the panel are split (horizontal/vertical),
629 and a change of menu visibility. */
630 update_menu ();
631 load_hint (TRUE);
634 /* --------------------------------------------------------------------------------------------- */
636 void
637 layout_box (void)
639 WDialog *layout_dlg;
641 old_layout = panels_layout;
642 old_output_lines = output_lines;
643 layout_dlg = init_layout ();
645 if (dlg_run (layout_dlg) == B_ENTER)
647 size_t i;
649 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
650 if (check_options[i].widget != NULL)
651 *check_options[i].variable = check_options[i].widget->state & C_BOOL;
653 else
655 /* restore layout */
656 panels_layout = old_layout;
657 output_lines = old_output_lines;
658 update_split (layout_dlg);
661 dlg_destroy (layout_dlg);
662 layout_change ();
663 do_refresh ();
666 /* --------------------------------------------------------------------------------------------- */
668 void
669 setup_panels (void)
671 int start_y;
673 if (mc_global.tty.console_flag != '\0')
675 int minimum;
677 if (output_lines < 0)
678 output_lines = 0;
679 height =
680 LINES - mc_global.keybar_visible - (command_prompt ? 1 : 0) - menubar_visible -
681 output_lines - mc_global.message_visible;
682 minimum = MINHEIGHT * (1 + panels_layout.horizontal_split);
683 if (height < minimum)
685 output_lines -= minimum - height;
686 height = minimum;
689 else
691 height =
692 LINES - menubar_visible - (command_prompt ? 1 : 0) - mc_global.keybar_visible -
693 mc_global.message_visible;
696 check_split (&panels_layout);
697 start_y = menubar_visible;
699 /* The column computing is deferred until panel_do_cols */
700 if (panels_layout.horizontal_split)
702 widget_set_size (panels[0].widget, start_y, 0, panels_layout.top_panel_size, 0);
703 widget_set_size (panels[1].widget, start_y + panels_layout.top_panel_size, 0,
704 height - panels_layout.top_panel_size, 0);
706 else
708 widget_set_size (panels[0].widget, start_y, 0, height, 0);
709 widget_set_size (panels[1].widget, start_y, panels_layout.left_panel_size, height, 0);
712 panel_do_cols (0);
713 panel_do_cols (1);
715 widget_set_size (WIDGET (the_menubar), 0, 0, 1, COLS);
717 if (command_prompt)
719 #ifdef ENABLE_SUBSHELL
720 if (!mc_global.tty.use_subshell || !do_load_prompt ())
721 #endif
722 setup_cmdline ();
724 else
726 widget_set_size (WIDGET (cmdline), 0, 0, 0, 0);
727 widget_set_size (WIDGET (the_prompt), LINES, COLS, 0, 0);
730 widget_set_size (WIDGET (the_bar), LINES - 1, 0, mc_global.keybar_visible, COLS);
731 buttonbar_set_visible (the_bar, mc_global.keybar_visible);
733 /* Output window */
734 if (mc_global.tty.console_flag != '\0' && output_lines)
736 output_start_y = LINES - (command_prompt ? 1 : 0) - mc_global.keybar_visible - output_lines;
737 show_console_contents (output_start_y,
738 LINES - output_lines - mc_global.keybar_visible - 1,
739 LINES - mc_global.keybar_visible - 1);
742 if (mc_global.message_visible)
743 widget_set_size (WIDGET (the_hint), height + start_y, 0, 1, COLS);
744 else
745 widget_set_size (WIDGET (the_hint), 0, 0, 0, 0);
747 update_xterm_title_path ();
750 /* --------------------------------------------------------------------------------------------- */
752 void
753 panels_split_equal (void)
755 if (panels_layout.horizontal_split)
756 panels_layout.horizontal_equal = TRUE;
757 else
758 panels_layout.vertical_equal = TRUE;
760 layout_change ();
761 do_refresh ();
764 /* --------------------------------------------------------------------------------------------- */
766 void
767 panels_split_more (void)
769 if (panels_layout.horizontal_split)
771 panels_layout.horizontal_equal = FALSE;
772 panels_layout.top_panel_size++;
774 else
776 panels_layout.vertical_equal = FALSE;
777 panels_layout.left_panel_size++;
780 layout_change ();
783 /* --------------------------------------------------------------------------------------------- */
785 void
786 panels_split_less (void)
788 if (panels_layout.horizontal_split)
790 panels_layout.horizontal_equal = FALSE;
791 panels_layout.top_panel_size--;
793 else
795 panels_layout.vertical_equal = FALSE;
796 panels_layout.left_panel_size--;
799 layout_change ();
802 /* --------------------------------------------------------------------------------------------- */
805 void
806 setup_cmdline (void)
808 int prompt_width;
809 int y;
810 char *tmp_prompt = (char *) mc_prompt;
812 #ifdef ENABLE_SUBSHELL
813 if (mc_global.tty.use_subshell)
815 tmp_prompt = g_string_free (subshell_prompt, FALSE);
816 (void) strip_ctrl_codes (tmp_prompt);
818 #endif
820 prompt_width = str_term_width1 (tmp_prompt);
822 /* Check for prompts too big */
823 if (COLS > 8 && prompt_width > COLS - 8)
825 int prompt_len;
827 prompt_width = COLS - 8;
828 prompt_len = str_offset_to_pos (tmp_prompt, prompt_width);
829 tmp_prompt[prompt_len] = '\0';
832 #ifdef ENABLE_SUBSHELL
833 if (mc_global.tty.use_subshell)
835 subshell_prompt = g_string_new (tmp_prompt);
836 g_free (tmp_prompt);
837 mc_prompt = subshell_prompt->str;
839 #endif
841 y = LINES - 1 - mc_global.keybar_visible;
843 widget_set_size (WIDGET (the_prompt), y, 0, 1, prompt_width);
844 label_set_text (the_prompt, mc_prompt);
845 widget_set_size (WIDGET (cmdline), y, prompt_width, 1, COLS - prompt_width);
848 /* --------------------------------------------------------------------------------------------- */
850 void
851 use_dash (gboolean flag)
853 if (flag)
854 ok_to_refresh++;
855 else
856 ok_to_refresh--;
859 /* --------------------------------------------------------------------------------------------- */
861 void
862 set_hintbar (const char *str)
864 label_set_text (the_hint, str);
865 if (ok_to_refresh > 0)
866 mc_refresh ();
869 /* --------------------------------------------------------------------------------------------- */
871 void
872 rotate_dash (gboolean show)
874 Widget *w = WIDGET (midnight_dlg);
876 if (!nice_rotating_dash || (ok_to_refresh <= 0))
877 return;
879 widget_move (w, (menubar_visible != 0) ? 1 : 0, w->cols - 1);
880 tty_setcolor (NORMAL_COLOR);
882 if (!show)
883 tty_print_alt_char (ACS_URCORNER, FALSE);
884 else
886 static const char rotating_dash[4] = "|/-\\";
887 static size_t pos = 0;
889 tty_print_char (rotating_dash[pos]);
890 pos = (pos + 1) % sizeof (rotating_dash);
893 mc_refresh ();
896 /* --------------------------------------------------------------------------------------------- */
898 const char *
899 get_nth_panel_name (int num)
901 if (!num)
902 return "New Left Panel";
903 else if (num == 1)
904 return "New Right Panel";
905 else
907 static char buffer[BUF_SMALL];
909 g_snprintf (buffer, sizeof (buffer), "%ith Panel", num);
910 return buffer;
914 /* --------------------------------------------------------------------------------------------- */
915 /* I wonder if I should start to use the folding mode than Dugan uses */
916 /* */
917 /* This is the centralized managing of the panel display types */
918 /* This routine takes care of destroying and creating new widgets */
919 /* Please note that it could manage MAX_VIEWS, not just left and right */
920 /* Currently nothing in the code takes advantage of this and has hard- */
921 /* coded values for two panels only */
923 /* Set the num-th panel to the view type: type */
924 /* This routine also keeps at least one WPanel object in the screen */
925 /* since a lot of routines depend on the current_panel variable */
927 void
928 set_display_type (int num, panel_view_mode_t type)
930 int x = 0, y = 0, cols = 0, lines = 0;
931 unsigned int the_other = 0; /* Index to the other panel */
932 const char *file_name = NULL; /* For Quick view */
933 Widget *new_widget = NULL, *old_widget = NULL;
934 panel_view_mode_t old_type = view_listing;
935 WPanel *the_other_panel = NULL;
937 if (num >= MAX_VIEWS)
939 fprintf (stderr, "Cannot allocate more that %d views\n", MAX_VIEWS);
940 abort ();
942 /* Check that we will have a WPanel * at least */
943 if (type != view_listing)
945 the_other = num == 0 ? 1 : 0;
947 if (panels[the_other].type != view_listing)
948 return;
951 /* Get rid of it */
952 if (panels[num].widget != NULL)
954 Widget *w = panels[num].widget;
955 WPanel *panel = PANEL (w);
957 x = w->x;
958 y = w->y;
959 cols = w->cols;
960 lines = w->lines;
961 old_widget = w;
962 old_type = panels[num].type;
964 if (old_type == view_listing && panel->frame_size == frame_full && type != view_listing)
966 if (panels_layout.horizontal_split)
968 cols = COLS;
969 x = 0;
971 else
973 cols = COLS - panels_layout.left_panel_size;
974 if (num == 1)
975 x = panels_layout.left_panel_size;
980 /* Restoring saved path from panels.ini for nonlist panel */
981 /* when it's first creation (for example view_info) */
982 if (old_widget == NULL && type != view_listing)
983 panels[num].last_saved_dir = _vfs_get_cwd ();
985 switch (type)
987 case view_nothing:
988 case view_listing:
989 new_widget = restore_into_right_dir_panel (num, old_widget);
990 widget_set_size (new_widget, y, x, lines, cols);
991 break;
993 case view_info:
994 new_widget = WIDGET (info_new (y, x, lines, cols));
995 break;
997 case view_tree:
998 new_widget = WIDGET (tree_new (y, x, lines, cols, TRUE));
999 break;
1001 case view_quick:
1002 new_widget = WIDGET (mcview_new (y, x, lines, cols, TRUE));
1003 the_other_panel = PANEL (panels[the_other].widget);
1004 if (the_other_panel != NULL)
1005 file_name = the_other_panel->dir.list[the_other_panel->selected].fname;
1006 else
1007 file_name = "";
1009 mcview_load ((WView *) new_widget, 0, file_name, 0, 0, 0);
1010 break;
1012 default:
1013 break;
1016 if (type != view_listing)
1017 /* Must save dir, for restoring after change type to */
1018 /* view_listing */
1019 save_panel_dir (num);
1021 panels[num].type = type;
1022 panels[num].widget = new_widget;
1024 /* We use replace to keep the circular list of the dialog in the */
1025 /* same state. Maybe we could just kill it and then replace it */
1026 if ((midnight_dlg != NULL) && (old_widget != NULL))
1028 if (old_type == view_listing)
1030 /* save and write directory history of panel
1031 * ... and other histories of midnight_dlg */
1032 dlg_save_history (midnight_dlg);
1035 widget_replace (old_widget, new_widget);
1038 if (type == view_listing)
1040 WPanel *panel = PANEL (new_widget);
1042 /* if existing panel changed type to view_listing, then load history */
1043 if (old_widget != NULL)
1045 ev_history_load_save_t event_data = { NULL, new_widget };
1047 mc_event_raise (midnight_dlg->event_group, MCEVENT_HISTORY_LOAD, &event_data);
1050 if (num == 0)
1051 left_panel = panel;
1052 else
1053 right_panel = panel;
1055 /* forced update format after set new sizes */
1056 set_panel_formats (panel);
1059 if (type == view_tree)
1060 the_tree = (WTree *) new_widget;
1062 /* Prevent current_panel's value from becoming invalid.
1063 * It's just a quick hack to prevent segfaults. Comment out and
1064 * try following:
1065 * - select left panel
1066 * - invoke menue left/tree
1067 * - as long as you stay in the left panel almost everything that uses
1068 * current_panel causes segfault, e.g. C-Enter, C-x c, ...
1070 if ((type != view_listing) && (current_panel == PANEL (old_widget)))
1071 current_panel = num == 0 ? right_panel : left_panel;
1073 g_free (old_widget);
1076 /* --------------------------------------------------------------------------------------------- */
1077 /** This routine is deeply sticked to the two panels idea.
1078 What should it do in more panels. ANSWER - don't use it
1079 in any multiple panels environment. */
1081 void
1082 swap_panels (void)
1084 WPanel *panel1, *panel2;
1085 Widget *tmp_widget;
1087 panel1 = PANEL (panels[0].widget);
1088 panel2 = PANEL (panels[1].widget);
1090 if (panels[0].type == view_listing && panels[1].type == view_listing &&
1091 !mc_config_get_bool (mc_global.main_config, CONFIG_PANELS_SECTION, "simple_swap", FALSE))
1093 WPanel panel;
1095 #define panelswap(x) panel.x = panel1->x; panel1->x = panel2->x; panel2->x = panel.x;
1097 #define panelswapstr(e) strcpy (panel.e, panel1->e); \
1098 strcpy (panel1->e, panel2->e); \
1099 strcpy (panel2->e, panel.e);
1100 /* Change content and related stuff */
1101 panelswap (dir);
1102 panelswap (active);
1103 panelswap (cwd_vpath);
1104 panelswap (lwd_vpath);
1105 panelswap (marked);
1106 panelswap (dirs_marked);
1107 panelswap (total);
1108 panelswap (top_file);
1109 panelswap (selected);
1110 panelswap (is_panelized);
1111 panelswap (dir_stat);
1112 #undef panelswapstr
1113 #undef panelswap
1115 panel1->searching = FALSE;
1116 panel2->searching = FALSE;
1118 if (current_panel == panel1)
1119 current_panel = panel2;
1120 else
1121 current_panel = panel1;
1123 /* if sort options are different -> resort panels */
1124 if (memcmp (&panel1->sort_info, &panel2->sort_info, sizeof (dir_sort_options_t)) != 0)
1126 panel_re_sort (other_panel);
1127 panel_re_sort (current_panel);
1130 if (widget_is_active (panels[0].widget))
1131 dlg_select_widget (panels[1].widget);
1132 else if (widget_is_active (panels[1].widget))
1133 dlg_select_widget (panels[0].widget);
1135 else
1137 WPanel *tmp_panel;
1138 int x, y, cols, lines;
1139 int tmp_type;
1141 tmp_panel = right_panel;
1142 right_panel = left_panel;
1143 left_panel = tmp_panel;
1145 if (panels[0].type == view_listing)
1147 if (strcmp (panel1->panel_name, get_nth_panel_name (0)) == 0)
1149 g_free (panel1->panel_name);
1150 panel1->panel_name = g_strdup (get_nth_panel_name (1));
1153 if (panels[1].type == view_listing)
1155 if (strcmp (panel2->panel_name, get_nth_panel_name (1)) == 0)
1157 g_free (panel2->panel_name);
1158 panel2->panel_name = g_strdup (get_nth_panel_name (0));
1162 x = panels[0].widget->x;
1163 y = panels[0].widget->y;
1164 cols = panels[0].widget->cols;
1165 lines = panels[0].widget->lines;
1167 panels[0].widget->x = panels[1].widget->x;
1168 panels[0].widget->y = panels[1].widget->y;
1169 panels[0].widget->cols = panels[1].widget->cols;
1170 panels[0].widget->lines = panels[1].widget->lines;
1172 panels[1].widget->x = x;
1173 panels[1].widget->y = y;
1174 panels[1].widget->cols = cols;
1175 panels[1].widget->lines = lines;
1177 tmp_widget = panels[0].widget;
1178 panels[0].widget = panels[1].widget;
1179 panels[1].widget = tmp_widget;
1180 tmp_type = panels[0].type;
1181 panels[0].type = panels[1].type;
1182 panels[1].type = tmp_type;
1184 /* force update formats because of possible changed sizes */
1185 if (panels[0].type == view_listing)
1186 set_panel_formats (PANEL (panels[0].widget));
1187 if (panels[1].type == view_listing)
1188 set_panel_formats (PANEL (panels[1].widget));
1192 /* --------------------------------------------------------------------------------------------- */
1194 panel_view_mode_t
1195 get_display_type (int idx)
1197 return panels[idx].type;
1200 /* --------------------------------------------------------------------------------------------- */
1202 Widget *
1203 get_panel_widget (int idx)
1205 return panels[idx].widget;
1208 /* --------------------------------------------------------------------------------------------- */
1211 get_current_index (void)
1213 if (panels[0].widget == WIDGET (current_panel))
1214 return 0;
1215 else
1216 return 1;
1219 /* --------------------------------------------------------------------------------------------- */
1222 get_other_index (void)
1224 return !get_current_index ();
1227 /* --------------------------------------------------------------------------------------------- */
1229 WPanel *
1230 get_other_panel (void)
1232 return PANEL (get_panel_widget (get_other_index ()));
1235 /* --------------------------------------------------------------------------------------------- */
1236 /** Returns the view type for the current panel/view */
1238 panel_view_mode_t
1239 get_current_type (void)
1241 if (panels[0].widget == WIDGET (current_panel))
1242 return panels[0].type;
1243 else
1244 return panels[1].type;
1247 /* --------------------------------------------------------------------------------------------- */
1248 /** Returns the view type of the unselected panel */
1250 panel_view_mode_t
1251 get_other_type (void)
1253 if (panels[0].widget == WIDGET (current_panel))
1254 return panels[1].type;
1255 else
1256 return panels[0].type;
1259 /* --------------------------------------------------------------------------------------------- */
1260 /** Save current list_view widget directory into panel */
1262 void
1263 save_panel_dir (int idx)
1265 panel_view_mode_t type = get_display_type (idx);
1266 Widget *widget = get_panel_widget (idx);
1268 if ((type == view_listing) && (widget != NULL))
1270 WPanel *w = PANEL (widget);
1272 g_free (panels[idx].last_saved_dir); /* last path no needed */
1273 /* Because path can be nonlocal */
1274 panels[idx].last_saved_dir = g_strdup (vfs_path_as_str (w->cwd_vpath));
1278 /* --------------------------------------------------------------------------------------------- */
1279 /** Return working dir, if it's view_listing - cwd,
1280 but for other types - last_saved_dir */
1282 char *
1283 get_panel_dir_for (const WPanel * widget)
1285 int i;
1287 for (i = 0; i < MAX_VIEWS; i++)
1288 if (PANEL (get_panel_widget (i)) == widget)
1289 break;
1291 if (i >= MAX_VIEWS)
1292 return g_strdup (".");
1294 if (get_display_type (i) == view_listing)
1296 vfs_path_t *cwd_vpath;
1298 cwd_vpath = PANEL (get_panel_widget (i))->cwd_vpath;
1299 return g_strdup (vfs_path_as_str (cwd_vpath));
1302 return g_strdup (panels[i].last_saved_dir);
1305 /* --------------------------------------------------------------------------------------------- */
1307 #ifdef ENABLE_SUBSHELL
1308 gboolean
1309 do_load_prompt (void)
1311 gboolean ret = FALSE;
1313 if (!read_subshell_prompt ())
1314 return ret;
1316 /* Don't actually change the prompt if it's invisible */
1317 if (top_dlg != NULL && DIALOG (top_dlg->data) == midnight_dlg && command_prompt)
1319 setup_cmdline ();
1321 /* since the prompt has changed, and we are called from one of the
1322 * tty_get_event channels, the prompt updating does not take place
1323 * automatically: force a cursor update and a screen refresh
1325 update_cursor (midnight_dlg);
1326 mc_refresh ();
1327 ret = TRUE;
1329 update_subshell_prompt = TRUE;
1330 return ret;
1333 /* --------------------------------------------------------------------------------------------- */
1336 load_prompt (int fd, void *unused)
1338 (void) fd;
1339 (void) unused;
1341 do_load_prompt ();
1342 return 0;
1344 #endif /* ENABLE_SUBSHELL */
1346 /* --------------------------------------------------------------------------------------------- */
1348 void
1349 title_path_prepare (char **path, char **login)
1351 char host[BUF_TINY];
1352 struct passwd *pw = NULL;
1353 int res = 0;
1355 *path =
1356 vfs_path_to_str_flags (current_panel->cwd_vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
1358 res = gethostname (host, sizeof (host));
1359 if (res != 0)
1360 host[0] = '\0';
1361 else
1362 host[sizeof (host) - 1] = '\0';
1364 pw = getpwuid (getuid ());
1365 if (pw != NULL)
1366 *login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1367 else
1368 *login = g_strdup (host);
1371 /* --------------------------------------------------------------------------------------------- */
1373 /** Show current directory in the xterm title */
1374 void
1375 update_xterm_title_path (void)
1377 if (mc_global.tty.xterm_flag && xterm_title)
1379 char *p;
1380 char *path;
1381 char *login;
1383 title_path_prepare (&path, &login);
1385 p = g_strdup_printf ("mc [%s]:%s", login, path);
1386 g_free (login);
1387 g_free (path);
1389 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1390 g_free (p);
1392 if (!mc_global.tty.alternate_plus_minus)
1393 numeric_keypad_mode ();
1394 (void) fflush (stdout);
1398 /* --------------------------------------------------------------------------------------------- */