Ticket #4536: skins: add root variant of julia256 skin.
[midnight-commander.git] / src / filemanager / layout.c
bloba8dbcb793b2cd9460557d565c3d76792f216964e
1 /*
2 Panel layout module for the Midnight Commander
4 Copyright (C) 1995-2024
5 Free Software Foundation, Inc.
7 Written by:
8 Janne Kukonlehto, 1995
9 Miguel de Icaza, 1995
10 Andrew Borodin <aborodin@vmail.ru>, 2011-2022
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" /* vfs_get_cwd () */
50 #include "lib/strutil.h"
51 #include "lib/widget.h"
52 #include "lib/event.h"
53 #include "lib/util.h" /* mc_time_elapsed() */
55 #include "src/consaver/cons.saver.h"
56 #include "src/viewer/mcviewer.h" /* The view widget */
57 #include "src/setup.h"
58 #ifdef ENABLE_SUBSHELL
59 #include "src/subshell/subshell.h"
60 #endif
62 #include "command.h"
63 #include "filemanager.h"
64 #include "tree.h"
65 /* Needed for the extern declarations of integer parameters */
66 #include "dir.h"
67 #include "layout.h"
68 #include "info.h" /* The Info widget */
70 /*** global variables ****************************************************************************/
72 panels_layout_t panels_layout = {
73 /* Set if the panels are split horizontally */
74 .horizontal_split = FALSE,
76 /* vertical split */
77 .vertical_equal = TRUE,
78 .left_panel_size = 0,
80 /* horizontal split */
81 .horizontal_equal = TRUE,
82 .top_panel_size = 0
85 /* Controls the display of the rotating dash on the verbose mode */
86 gboolean nice_rotating_dash = TRUE;
88 /* The number of output lines shown (if available) */
89 int output_lines = 0;
91 /* Set if the command prompt is to be displayed */
92 gboolean command_prompt = TRUE;
94 /* Set if the main menu is visible */
95 gboolean menubar_visible = TRUE;
97 /* Set to show current working dir in xterm window title */
98 gboolean xterm_title = TRUE;
100 /* Set to show free space on device assigned to current directory */
101 gboolean free_space = TRUE;
103 /* The starting line for the output of the subprogram */
104 int output_start_y = 0;
106 int ok_to_refresh = 1;
108 /*** file scope macro definitions ****************************************************************/
110 /* The maximum number of views managed by the create_panel routine */
111 /* Must be at least two (for current and other). Please note that until */
112 /* Janne gets around this, we will only manage two of them :-) */
113 #define MAX_VIEWS 2
115 /* Width 12 for a wee Quick (Hex) View */
116 #define MINWIDTH 12
117 #define MINHEIGHT 5
119 #define B_2LEFT B_USER
120 #define B_2RIGHT (B_USER + 1)
121 #define B_PLUS (B_USER + 2)
122 #define B_MINUS (B_USER + 3)
124 #define LAYOUT_OPTIONS_COUNT G_N_ELEMENTS (check_options)
126 /*** file scope type declarations ****************************************************************/
128 typedef struct
130 gboolean menubar_visible;
131 gboolean command_prompt;
132 gboolean keybar_visible;
133 gboolean message_visible;
134 gboolean xterm_title;
135 gboolean free_space;
136 int output_lines;
137 } layout_t;
139 /*** forward declarations (file scope functions) *************************************************/
141 /*** file scope variables ************************************************************************/
143 static struct
145 panel_view_mode_t type;
146 Widget *widget;
147 char *last_saved_dir; /* last view_list working directory */
148 } panels[MAX_VIEWS] =
150 /* *INDENT-OFF* */
151 /* init MAX_VIEWS items */
152 { view_listing, NULL, NULL},
153 { view_listing, NULL, NULL}
154 /* *INDENT-ON* */
157 static layout_t old_layout;
158 static panels_layout_t old_panels_layout;
160 static gboolean equal_split;
161 static int _output_lines;
163 static int height;
165 static WRadio *radio_widget;
167 static struct
169 const char *text;
170 gboolean *variable;
171 WCheck *widget;
172 } check_options[] =
174 /* *INDENT-OFF* */
175 { N_("&Equal split"), &equal_split, NULL },
176 { N_("&Menubar visible"), &menubar_visible, NULL },
177 { N_("Command &prompt"), &command_prompt, NULL },
178 { N_("&Keybar visible"), &mc_global.keybar_visible, NULL },
179 { N_("H&intbar visible"), &mc_global.message_visible, NULL },
180 { N_("&XTerm window title"), &xterm_title, NULL },
181 { N_("&Show free space"), &free_space, NULL }
182 /* *INDENT-ON* */
185 static const char *output_lines_label = NULL;
186 static int output_lines_label_len;
188 static WButton *bleft_widget, *bright_widget;
190 /* --------------------------------------------------------------------------------------------- */
191 /*** file scope functions ************************************************************************/
192 /* --------------------------------------------------------------------------------------------- */
194 /* don't use max() macro to avoid double call of str_term_width1() in widget width calculation */
195 #undef max
197 static int
198 max (int a, int b)
200 return a > b ? a : b;
203 /* --------------------------------------------------------------------------------------------- */
205 static void
206 check_split (panels_layout_t * layout)
208 if (layout->horizontal_split)
210 if (layout->horizontal_equal)
211 layout->top_panel_size = height / 2;
212 else if (layout->top_panel_size < MINHEIGHT)
213 layout->top_panel_size = MINHEIGHT;
214 else if (layout->top_panel_size > height - MINHEIGHT)
215 layout->top_panel_size = height - MINHEIGHT;
217 else
219 int md_cols = CONST_WIDGET (filemanager)->rect.cols;
221 if (layout->vertical_equal)
222 layout->left_panel_size = md_cols / 2;
223 else if (layout->left_panel_size < MINWIDTH)
224 layout->left_panel_size = MINWIDTH;
225 else if (layout->left_panel_size > md_cols - MINWIDTH)
226 layout->left_panel_size = md_cols - MINWIDTH;
230 /* --------------------------------------------------------------------------------------------- */
232 static void
233 update_split (const WDialog * h)
235 /* Check split has to be done before testing if it changed, since
236 it can change due to calling check_split() as well */
237 check_split (&panels_layout);
239 if (panels_layout.horizontal_split)
240 check_options[0].widget->state = panels_layout.horizontal_equal;
241 else
242 check_options[0].widget->state = panels_layout.vertical_equal;
243 widget_draw (WIDGET (check_options[0].widget));
245 tty_setcolor (check_options[0].widget->state ? DISABLED_COLOR : COLOR_NORMAL);
247 widget_gotoyx (h, 6, 5);
248 if (panels_layout.horizontal_split)
249 tty_printf ("%03d", panels_layout.top_panel_size);
250 else
251 tty_printf ("%03d", panels_layout.left_panel_size);
253 widget_gotoyx (h, 6, 17);
254 if (panels_layout.horizontal_split)
255 tty_printf ("%03d", height - panels_layout.top_panel_size);
256 else
257 tty_printf ("%03d", CONST_WIDGET (filemanager)->rect.cols - panels_layout.left_panel_size);
259 widget_gotoyx (h, 6, 12);
260 tty_print_char ('=');
263 /* --------------------------------------------------------------------------------------------- */
265 static int
266 b_left_right_cback (WButton * button, int action)
268 (void) action;
270 if (button == bright_widget)
272 if (panels_layout.horizontal_split)
273 panels_layout.top_panel_size++;
274 else
275 panels_layout.left_panel_size++;
277 else
279 if (panels_layout.horizontal_split)
280 panels_layout.top_panel_size--;
281 else
282 panels_layout.left_panel_size--;
285 update_split (DIALOG (WIDGET (button)->owner));
286 layout_change ();
287 do_refresh ();
288 return 0;
291 /* --------------------------------------------------------------------------------------------- */
293 static int
294 bplus_cback (WButton * button, int action)
296 (void) button;
297 (void) action;
299 if (_output_lines < 99)
300 _output_lines++;
301 return 0;
304 /* --------------------------------------------------------------------------------------------- */
306 static int
307 bminus_cback (WButton * button, int action)
309 (void) button;
310 (void) action;
312 if (_output_lines > 0)
313 _output_lines--;
314 return 0;
317 /* --------------------------------------------------------------------------------------------- */
319 static cb_ret_t
320 layout_bg_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
322 switch (msg)
324 case MSG_DRAW:
325 frame_callback (w, NULL, MSG_DRAW, 0, NULL);
327 old_layout.output_lines = -1;
329 update_split (DIALOG (w->owner));
331 if (old_layout.output_lines != _output_lines)
333 old_layout.output_lines = _output_lines;
334 tty_setcolor (mc_global.tty.console_flag != '\0' ? COLOR_NORMAL : DISABLED_COLOR);
335 widget_gotoyx (w, 9, 5);
336 tty_print_string (output_lines_label);
337 widget_gotoyx (w, 9, 5 + 3 + output_lines_label_len);
338 tty_printf ("%02d", _output_lines);
340 return MSG_HANDLED;
342 default:
343 return frame_callback (w, sender, msg, parm, data);
347 /* --------------------------------------------------------------------------------------------- */
349 static cb_ret_t
350 layout_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
352 WDialog *h = DIALOG (w);
354 switch (msg)
356 case MSG_POST_KEY:
358 const Widget *mw = CONST_WIDGET (filemanager);
359 gboolean _menubar_visible, _command_prompt, _keybar_visible, _message_visible;
361 _menubar_visible = check_options[1].widget->state;
362 _command_prompt = check_options[2].widget->state;
363 _keybar_visible = check_options[3].widget->state;
364 _message_visible = check_options[4].widget->state;
366 if (mc_global.tty.console_flag == '\0')
367 height =
368 mw->rect.lines - (_keybar_visible ? 1 : 0) - (_command_prompt ? 1 : 0) -
369 (_menubar_visible ? 1 : 0) - _output_lines - (_message_visible ? 1 : 0);
370 else
372 int minimum;
374 if (_output_lines < 0)
375 _output_lines = 0;
376 height =
377 mw->rect.lines - (_keybar_visible ? 1 : 0) - (_command_prompt ? 1 : 0) -
378 (_menubar_visible ? 1 : 0) - _output_lines - (_message_visible ? 1 : 0);
379 minimum = MINHEIGHT * (1 + (panels_layout.horizontal_split ? 1 : 0));
380 if (height < minimum)
382 _output_lines -= minimum - height;
383 height = minimum;
387 if (old_layout.output_lines != _output_lines)
389 old_layout.output_lines = _output_lines;
390 tty_setcolor (mc_global.tty.console_flag != '\0' ? COLOR_NORMAL : DISABLED_COLOR);
391 widget_gotoyx (h, 9, 5 + 3 + output_lines_label_len);
392 tty_printf ("%02d", _output_lines);
395 return MSG_HANDLED;
397 case MSG_NOTIFY:
398 if (sender == WIDGET (radio_widget))
400 if ((panels_layout.horizontal_split ? 1 : 0) == radio_widget->sel)
401 update_split (h);
402 else
404 int eq;
406 panels_layout.horizontal_split = radio_widget->sel != 0;
408 if (panels_layout.horizontal_split)
410 eq = panels_layout.horizontal_equal;
411 if (eq)
412 panels_layout.top_panel_size = height / 2;
414 else
416 eq = panels_layout.vertical_equal;
417 if (eq)
418 panels_layout.left_panel_size = CONST_WIDGET (filemanager)->rect.cols / 2;
421 widget_disable (WIDGET (bleft_widget), eq);
422 widget_disable (WIDGET (bright_widget), eq);
424 update_split (h);
425 layout_change ();
426 do_refresh ();
429 return MSG_HANDLED;
432 if (sender == WIDGET (check_options[0].widget))
434 gboolean eq;
436 if (panels_layout.horizontal_split)
438 panels_layout.horizontal_equal = check_options[0].widget->state;
439 eq = panels_layout.horizontal_equal;
441 else
443 panels_layout.vertical_equal = check_options[0].widget->state;
444 eq = panels_layout.vertical_equal;
447 widget_disable (WIDGET (bleft_widget), eq);
448 widget_disable (WIDGET (bright_widget), eq);
450 update_split (h);
451 layout_change ();
452 do_refresh ();
454 return MSG_HANDLED;
458 gboolean ok = TRUE;
460 if (sender == WIDGET (check_options[1].widget))
461 menubar_visible = check_options[1].widget->state;
462 else if (sender == WIDGET (check_options[2].widget))
463 command_prompt = check_options[2].widget->state;
464 else if (sender == WIDGET (check_options[3].widget))
465 mc_global.keybar_visible = check_options[3].widget->state;
466 else if (sender == WIDGET (check_options[4].widget))
467 mc_global.message_visible = check_options[4].widget->state;
468 else if (sender == WIDGET (check_options[5].widget))
469 xterm_title = check_options[5].widget->state;
470 else if (sender == WIDGET (check_options[6].widget))
471 free_space = check_options[6].widget->state;
472 else
473 ok = FALSE;
475 if (ok)
477 update_split (h);
478 layout_change ();
479 do_refresh ();
480 return MSG_HANDLED;
484 return MSG_NOT_HANDLED;
486 default:
487 return dlg_default_callback (w, sender, msg, parm, data);
491 /* --------------------------------------------------------------------------------------------- */
493 static WDialog *
494 layout_dlg_create (void)
496 WDialog *layout_dlg;
497 WGroup *g;
498 int l1 = 0, width;
499 int b1, b2, b;
500 size_t i;
502 const char *title1 = N_("Panel split");
503 const char *title2 = N_("Console output");
504 const char *title3 = N_("Other options");
506 const char *s_split_direction[2] = {
507 N_("&Vertical"),
508 N_("&Horizontal")
511 const char *ok_button = N_("&OK");
512 const char *cancel_button = N_("&Cancel");
514 output_lines_label = _("Output lines:");
516 #ifdef ENABLE_NLS
518 static gboolean i18n = FALSE;
520 title1 = _(title1);
521 title2 = _(title2);
522 title3 = _(title3);
524 i = G_N_ELEMENTS (s_split_direction);
525 while (i-- != 0)
526 s_split_direction[i] = _(s_split_direction[i]);
528 if (!i18n)
530 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
531 check_options[i].text = _(check_options[i].text);
532 i18n = TRUE;
535 ok_button = _(ok_button);
536 cancel_button = _(cancel_button);
538 #endif
540 /* radiobuttons */
541 i = G_N_ELEMENTS (s_split_direction);
542 while (i-- != 0)
543 l1 = max (l1, str_term_width1 (s_split_direction[i]) + 7);
544 /* checkboxes */
545 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
546 l1 = max (l1, str_term_width1 (check_options[i].text) + 7);
547 /* groupboxes */
548 l1 = max (l1, str_term_width1 (title1) + 4);
549 l1 = max (l1, str_term_width1 (title2) + 4);
550 l1 = max (l1, str_term_width1 (title3) + 4);
551 /* label + "+"/"-" buttons */
552 output_lines_label_len = str_term_width1 (output_lines_label);
553 l1 = max (l1, output_lines_label_len + 12);
554 /* buttons */
555 b1 = str_term_width1 (ok_button) + 5; /* default button */
556 b2 = str_term_width1 (cancel_button) + 3;
557 b = b1 + b2 + 1;
558 /* dialog width */
559 width = max (l1 * 2 + 7, b);
561 layout_dlg =
562 dlg_create (TRUE, 0, 0, 15, width, WPOS_CENTER, FALSE, dialog_colors, layout_callback, NULL,
563 "[Layout]", _("Layout"));
564 g = GROUP (layout_dlg);
566 /* draw background */
567 layout_dlg->bg->callback = layout_bg_callback;
569 #define XTRACT(i) (*check_options[i].variable != 0), check_options[i].text
571 /* "Panel split" groupbox */
572 group_add_widget (g, groupbox_new (2, 3, 6, l1, title1));
574 radio_widget = radio_new (3, 5, 2, s_split_direction);
575 radio_widget->sel = panels_layout.horizontal_split ? 1 : 0;
576 group_add_widget (g, radio_widget);
578 check_options[0].widget = check_new (5, 5, XTRACT (0));
579 group_add_widget (g, check_options[0].widget);
581 equal_split = panels_layout.horizontal_split ?
582 panels_layout.horizontal_equal : panels_layout.vertical_equal;
584 bleft_widget = button_new (6, 8, B_2LEFT, NARROW_BUTTON, "&<", b_left_right_cback);
585 widget_disable (WIDGET (bleft_widget), equal_split);
586 group_add_widget (g, bleft_widget);
588 bright_widget = button_new (6, 14, B_2RIGHT, NARROW_BUTTON, "&>", b_left_right_cback);
589 widget_disable (WIDGET (bright_widget), equal_split);
590 group_add_widget (g, bright_widget);
592 /* "Console output" groupbox */
594 widget_state_t disabled;
595 Widget *w;
597 disabled = mc_global.tty.console_flag != '\0' ? 0 : WST_DISABLED;
599 w = WIDGET (groupbox_new (8, 3, 3, l1, title2));
600 w->state |= disabled;
601 group_add_widget (g, w);
603 w = WIDGET (button_new (9, output_lines_label_len + 5, B_PLUS,
604 NARROW_BUTTON, "&+", bplus_cback));
605 w->state |= disabled;
606 group_add_widget (g, w);
608 w = WIDGET (button_new (9, output_lines_label_len + 5 + 5, B_MINUS,
609 NARROW_BUTTON, "&-", bminus_cback));
610 w->state |= disabled;
611 group_add_widget (g, w);
614 /* "Other options" groupbox */
615 group_add_widget (g, groupbox_new (2, 4 + l1, 9, l1, title3));
617 for (i = 1; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
619 check_options[i].widget = check_new (i + 2, 6 + l1, XTRACT (i));
620 group_add_widget (g, check_options[i].widget);
623 #undef XTRACT
625 group_add_widget (g, hline_new (11, -1, -1));
626 /* buttons */
627 group_add_widget (g, button_new (12, (width - b) / 2, B_ENTER, DEFPUSH_BUTTON, ok_button, 0));
628 group_add_widget (g,
629 button_new (12, (width - b) / 2 + b1 + 1, B_CANCEL, NORMAL_BUTTON,
630 cancel_button, 0));
632 widget_select (WIDGET (radio_widget));
634 return layout_dlg;
637 /* --------------------------------------------------------------------------------------------- */
639 static void
640 panel_do_cols (int idx)
642 if (get_panel_type (idx) == view_listing)
643 set_panel_formats (PANEL (panels[idx].widget));
644 else
645 panel_update_cols (panels[idx].widget, frame_half);
648 /* --------------------------------------------------------------------------------------------- */
649 /** Save current list_view widget directory into panel */
651 static Widget *
652 restore_into_right_dir_panel (int idx, gboolean last_was_panel, const WRect * r)
654 WPanel *new_widget;
655 const char *p_name;
657 p_name = get_nth_panel_name (idx);
659 if (last_was_panel)
661 vfs_path_t *saved_dir_vpath;
663 saved_dir_vpath = vfs_path_from_str (panels[idx].last_saved_dir);
664 new_widget = panel_sized_with_dir_new (p_name, r, saved_dir_vpath);
665 vfs_path_free (saved_dir_vpath, TRUE);
667 else
668 new_widget = panel_sized_new (p_name, r);
670 return WIDGET (new_widget);
673 /* --------------------------------------------------------------------------------------------- */
675 static void
676 layout_save (void)
678 old_layout.menubar_visible = menubar_visible;
679 old_layout.command_prompt = command_prompt;
680 old_layout.keybar_visible = mc_global.keybar_visible;
681 old_layout.message_visible = mc_global.message_visible;
682 old_layout.xterm_title = xterm_title;
683 old_layout.free_space = free_space;
684 old_layout.output_lines = -1;
686 _output_lines = output_lines;
688 old_panels_layout = panels_layout;
691 /* --------------------------------------------------------------------------------------------- */
693 static void
694 layout_restore (void)
696 menubar_visible = old_layout.menubar_visible;
697 command_prompt = old_layout.command_prompt;
698 mc_global.keybar_visible = old_layout.keybar_visible;
699 mc_global.message_visible = old_layout.message_visible;
700 xterm_title = old_layout.xterm_title;
701 free_space = old_layout.free_space;
702 output_lines = old_layout.output_lines;
704 panels_layout = old_panels_layout;
707 /* --------------------------------------------------------------------------------------------- */
708 /*** public functions ****************************************************************************/
709 /* --------------------------------------------------------------------------------------------- */
711 void
712 layout_change (void)
714 setup_panels ();
715 /* update the main menu, because perhaps there was a change in the way
716 how the panel are split (horizontal/vertical),
717 and a change of menu visibility. */
718 update_menu ();
719 load_hint (TRUE);
722 /* --------------------------------------------------------------------------------------------- */
724 void
725 layout_box (void)
727 WDialog *layout_dlg;
729 layout_save ();
731 layout_dlg = layout_dlg_create ();
733 if (dlg_run (layout_dlg) == B_ENTER)
735 size_t i;
737 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
738 if (check_options[i].widget != NULL)
739 *check_options[i].variable = check_options[i].widget->state;
741 output_lines = _output_lines;
743 else
744 layout_restore ();
746 widget_destroy (WIDGET (layout_dlg));
747 layout_change ();
748 do_refresh ();
751 /* --------------------------------------------------------------------------------------------- */
753 void
754 panel_update_cols (Widget * widget, panel_display_t frame_size)
756 const Widget *mw = CONST_WIDGET (filemanager);
757 int cols, x;
759 /* don't touch panel if it is not in dialog yet */
760 /* if panel is not in dialog it is not in widgets list
761 and cannot be compared with get_panel_widget() result */
762 if (widget->owner == NULL)
763 return;
765 if (panels_layout.horizontal_split)
767 widget->rect.cols = mw->rect.cols;
768 return;
771 if (frame_size == frame_full)
773 cols = mw->rect.cols;
774 x = mw->rect.x;
776 else if (widget == get_panel_widget (0))
778 cols = panels_layout.left_panel_size;
779 x = mw->rect.x;
781 else
783 cols = mw->rect.cols - panels_layout.left_panel_size;
784 x = mw->rect.x + panels_layout.left_panel_size;
787 widget->rect.cols = cols;
788 widget->rect.x = x;
791 /* --------------------------------------------------------------------------------------------- */
793 void
794 setup_panels (void)
796 /* File manager screen layout:
798 * +---------------------------------------------------------------+
799 * | Menu bar |
800 * +-------------------------------+-------------------------------+
801 * | | |
802 * | | |
803 * | | |
804 * | | |
805 * | Left panel | Right panel |
806 * | | |
807 * | | |
808 * | | |
809 * | | |
810 * +-------------------------------+-------------------------------+
811 * | Hint (message) bar |
812 * +---------------------------------------------------------------+
813 * | |
814 * | Console content |
815 * | |
816 * +--------+------------------------------------------------------+
817 * | Prompt | Command line |
818 * | Key (button) bar |
819 * +--------+------------------------------------------------------+
822 Widget *mw = WIDGET (filemanager);
823 const WRect *r = &CONST_WIDGET (mw)->rect;
824 int start_y;
825 gboolean active;
826 WRect rb;
828 active = widget_get_state (mw, WST_ACTIVE);
830 /* lock the group to avoid many redraws */
831 if (active)
832 widget_set_state (mw, WST_SUSPENDED, TRUE);
834 /* initial height of panels */
835 height =
836 r->lines - (menubar_visible ? 1 : 0) - (mc_global.message_visible ? 1 : 0) -
837 (command_prompt ? 1 : 0) - (mc_global.keybar_visible ? 1 : 0);
839 if (mc_global.tty.console_flag != '\0')
841 int minimum;
843 if (output_lines < 0)
844 output_lines = 0;
845 else
846 height -= output_lines;
847 minimum = MINHEIGHT * (1 + (panels_layout.horizontal_split ? 1 : 0));
848 if (height < minimum)
850 output_lines -= minimum - height;
851 height = minimum;
855 rb = *r;
856 rb.lines = 1;
857 widget_set_size_rect (WIDGET (the_menubar), &rb);
858 widget_set_visibility (WIDGET (the_menubar), menubar_visible);
860 check_split (&panels_layout);
861 start_y = r->y + (menubar_visible ? 1 : 0);
863 /* update columns first... */
864 panel_do_cols (0);
865 panel_do_cols (1);
867 /* ...then rows and origin */
868 if (panels_layout.horizontal_split)
870 widget_set_size (panels[0].widget, start_y, r->x, panels_layout.top_panel_size,
871 panels[0].widget->rect.cols);
872 widget_set_size (panels[1].widget, start_y + panels_layout.top_panel_size, r->x,
873 height - panels_layout.top_panel_size, panels[1].widget->rect.cols);
875 else
877 widget_set_size (panels[0].widget, start_y, r->x, height, panels[0].widget->rect.cols);
878 widget_set_size (panels[1].widget, start_y, panels[1].widget->rect.x, height,
879 panels[1].widget->rect.cols);
882 widget_set_size (WIDGET (the_hint), height + start_y, r->x, 1, r->cols);
883 widget_set_visibility (WIDGET (the_hint), mc_global.message_visible);
885 /* Output window */
886 if (mc_global.tty.console_flag != '\0' && output_lines != 0)
888 unsigned char end_line;
890 end_line = r->lines - (mc_global.keybar_visible ? 1 : 0) - 1;
891 output_start_y = end_line - (command_prompt ? 1 : 0) - output_lines + 1;
892 show_console_contents (output_start_y, end_line - output_lines, end_line);
895 if (command_prompt)
897 #ifdef ENABLE_SUBSHELL
898 if (!mc_global.tty.use_subshell || !do_load_prompt ())
899 #endif
900 setup_cmdline ();
902 else
904 /* make invisible */
905 widget_hide (WIDGET (cmdline));
906 widget_hide (WIDGET (the_prompt));
909 rb = *r;
910 rb.y = r->lines - 1;
911 rb.lines = 1;
912 widget_set_size_rect (WIDGET (the_bar), &rb);
913 widget_set_visibility (WIDGET (the_bar), mc_global.keybar_visible);
915 update_xterm_title_path ();
916 update_terminal_cwd ();
918 /* unlock */
919 if (active)
921 widget_set_state (mw, WST_ACTIVE, TRUE);
922 widget_draw (mw);
926 /* --------------------------------------------------------------------------------------------- */
928 void
929 panels_split_equal (void)
931 if (panels_layout.horizontal_split)
932 panels_layout.horizontal_equal = TRUE;
933 else
934 panels_layout.vertical_equal = TRUE;
936 layout_change ();
937 do_refresh ();
940 /* --------------------------------------------------------------------------------------------- */
942 void
943 panels_split_more (void)
945 if (panels_layout.horizontal_split)
947 panels_layout.horizontal_equal = FALSE;
948 panels_layout.top_panel_size++;
950 else
952 panels_layout.vertical_equal = FALSE;
953 panels_layout.left_panel_size++;
956 layout_change ();
959 /* --------------------------------------------------------------------------------------------- */
961 void
962 panels_split_less (void)
964 if (panels_layout.horizontal_split)
966 panels_layout.horizontal_equal = FALSE;
967 panels_layout.top_panel_size--;
969 else
971 panels_layout.vertical_equal = FALSE;
972 panels_layout.left_panel_size--;
975 layout_change ();
978 /* --------------------------------------------------------------------------------------------- */
980 void
981 setup_cmdline (void)
983 const Widget *mw = CONST_WIDGET (filemanager);
984 const WRect *r = &mw->rect;
985 int prompt_width;
986 int y;
987 char *tmp_prompt = (char *) mc_prompt;
989 if (!command_prompt)
990 return;
992 #ifdef ENABLE_SUBSHELL
993 if (mc_global.tty.use_subshell)
995 /* Workaround: avoid crash on FreeBSD (see ticket #4213 for details) */
996 if (subshell_prompt != NULL)
997 tmp_prompt = g_string_free (subshell_prompt, FALSE);
998 else
999 tmp_prompt = g_strdup (mc_prompt);
1000 (void) strip_ctrl_codes (tmp_prompt);
1002 #endif
1004 prompt_width = str_term_width1 (tmp_prompt);
1006 /* Check for prompts too big */
1007 if (r->cols > 8 && prompt_width > r->cols - 8)
1009 int prompt_len;
1011 prompt_width = r->cols - 8;
1012 prompt_len = str_offset_to_pos (tmp_prompt, prompt_width);
1013 tmp_prompt[prompt_len] = '\0';
1016 #ifdef ENABLE_SUBSHELL
1017 if (mc_global.tty.use_subshell)
1019 subshell_prompt = g_string_new_take (tmp_prompt);
1020 mc_prompt = subshell_prompt->str;
1022 #endif
1024 y = r->lines - 1 - (mc_global.keybar_visible ? 1 : 0);
1026 widget_set_size (WIDGET (the_prompt), y, r->x, 1, prompt_width);
1027 label_set_text (the_prompt, mc_prompt);
1028 widget_set_size (WIDGET (cmdline), y, r->x + prompt_width, 1, r->cols - prompt_width);
1030 widget_show (WIDGET (the_prompt));
1031 widget_show (WIDGET (cmdline));
1034 /* --------------------------------------------------------------------------------------------- */
1036 void
1037 use_dash (gboolean flag)
1039 if (flag)
1040 ok_to_refresh++;
1041 else
1042 ok_to_refresh--;
1045 /* --------------------------------------------------------------------------------------------- */
1047 void
1048 set_hintbar (const char *str)
1050 label_set_text (the_hint, str);
1051 if (ok_to_refresh > 0)
1052 mc_refresh ();
1055 /* --------------------------------------------------------------------------------------------- */
1057 void
1058 rotate_dash (gboolean show)
1060 static gint64 timestamp = 0;
1061 /* update with 10 FPS rate */
1062 static const gint64 delay = G_USEC_PER_SEC / 10;
1064 const Widget *w = CONST_WIDGET (filemanager);
1066 if (!nice_rotating_dash || (ok_to_refresh <= 0))
1067 return;
1069 if (show && !mc_time_elapsed (&timestamp, delay))
1070 return;
1072 widget_gotoyx (w, menubar_visible ? 1 : 0, w->rect.cols - 1);
1073 tty_setcolor (NORMAL_COLOR);
1075 if (!show)
1076 tty_print_alt_char (ACS_URCORNER, FALSE);
1077 else
1079 static const char rotating_dash[4] = "|/-\\";
1080 static size_t pos = 0;
1082 tty_print_char (rotating_dash[pos]);
1083 pos = (pos + 1) % sizeof (rotating_dash);
1086 mc_refresh ();
1089 /* --------------------------------------------------------------------------------------------- */
1091 const char *
1092 get_nth_panel_name (int num)
1094 if (num == 0)
1095 return "New Left Panel";
1097 if (num == 1)
1098 return "New Right Panel";
1101 static char buffer[BUF_SMALL];
1103 g_snprintf (buffer, sizeof (buffer), "%ith Panel", num);
1104 return buffer;
1108 /* --------------------------------------------------------------------------------------------- */
1109 /* I wonder if I should start to use the folding mode than Dugan uses */
1110 /* */
1111 /* This is the centralized managing of the panel display types */
1112 /* This routine takes care of destroying and creating new widgets */
1113 /* Please note that it could manage MAX_VIEWS, not just left and right */
1114 /* Currently nothing in the code takes advantage of this and has hard- */
1115 /* coded values for two panels only */
1117 /* Set the num-th panel to the view type: type */
1118 /* This routine also keeps at least one WPanel object in the screen */
1119 /* since a lot of routines depend on the current_panel variable */
1121 void
1122 create_panel (int num, panel_view_mode_t type)
1124 WRect r = { 0, 0, 0, 0 };
1125 unsigned int the_other = 0; /* Index to the other panel */
1126 Widget *new_widget = NULL, *old_widget = NULL;
1127 panel_view_mode_t old_type = view_listing;
1129 if (num >= MAX_VIEWS)
1131 fprintf (stderr, "Cannot allocate more that %d views\n", MAX_VIEWS);
1132 abort ();
1134 /* Check that we will have a WPanel * at least */
1135 if (type != view_listing)
1137 the_other = num == 0 ? 1 : 0;
1139 if (panels[the_other].type != view_listing)
1140 return;
1143 /* Get rid of it */
1144 if (panels[num].widget != NULL)
1146 Widget *w = panels[num].widget;
1147 WPanel *panel = PANEL (w);
1149 r = w->rect;
1150 old_widget = w;
1151 old_type = panels[num].type;
1153 if (old_type == view_listing && panel->frame_size == frame_full && type != view_listing)
1155 int md_cols = CONST_WIDGET (filemanager)->rect.cols;
1157 if (panels_layout.horizontal_split)
1159 r.cols = md_cols;
1160 r.x = 0;
1162 else
1164 r.cols = md_cols - panels_layout.left_panel_size;
1165 if (num == 1)
1166 r.x = panels_layout.left_panel_size;
1171 /* Restoring saved path from panels.ini for nonlist panel */
1172 /* when it's first creation (for example view_info) */
1173 if (old_widget == NULL && type != view_listing)
1174 panels[num].last_saved_dir = vfs_get_cwd ();
1176 switch (type)
1178 case view_nothing:
1179 case view_listing:
1181 gboolean last_was_panel;
1183 last_was_panel = old_widget != NULL && get_panel_type (num) != view_listing;
1184 new_widget = restore_into_right_dir_panel (num, last_was_panel, &r);
1185 break;
1188 case view_info:
1189 new_widget = WIDGET (info_new (&r));
1190 break;
1192 case view_tree:
1193 new_widget = WIDGET (tree_new (&r, TRUE));
1194 break;
1196 case view_quick:
1198 WPanel *the_other_panel;
1199 const char *file_name = "";
1201 new_widget = WIDGET (mcview_new (&r, TRUE));
1202 the_other_panel = PANEL (panels[the_other].widget);
1203 if (the_other_panel != NULL)
1204 file_name = panel_current_entry (the_other_panel)->fname->str;
1206 mcview_load ((WView *) new_widget, 0, file_name, 0, 0, 0);
1207 break;
1210 default:
1211 break;
1214 if (type != view_listing)
1215 /* Must save dir, for restoring after change type to */
1216 /* view_listing */
1217 save_panel_dir (num);
1219 panels[num].type = type;
1220 panels[num].widget = new_widget;
1222 /* We use replace to keep the circular list of the dialog in the */
1223 /* same state. Maybe we could just kill it and then replace it */
1224 if (old_widget != NULL)
1226 if (old_type == view_listing)
1228 /* save and write directory history of panel
1229 * ... and other histories of filemanager */
1230 dlg_save_history (filemanager);
1233 widget_replace (old_widget, new_widget);
1236 if (type == view_listing)
1238 WPanel *panel = PANEL (new_widget);
1240 /* if existing panel changed type to view_listing, then load history */
1241 if (old_widget != NULL)
1243 ev_history_load_save_t event_data = { NULL, new_widget };
1245 mc_event_raise (filemanager->event_group, MCEVENT_HISTORY_LOAD, &event_data);
1248 if (num == 0)
1249 left_panel = panel;
1250 else
1251 right_panel = panel;
1253 /* forced update format after set new sizes */
1254 set_panel_formats (panel);
1257 if (type == view_tree)
1258 the_tree = (WTree *) new_widget;
1260 /* Prevent current_panel's value from becoming invalid.
1261 * It's just a quick hack to prevent segfaults. Comment out and
1262 * try following:
1263 * - select left panel
1264 * - invoke menu left/tree
1265 * - as long as you stay in the left panel almost everything that uses
1266 * current_panel causes segfault, e.g. C-Enter, C-x c, ...
1268 if ((type != view_listing) && (current_panel == PANEL (old_widget)))
1269 current_panel = num == 0 ? right_panel : left_panel;
1271 g_free (old_widget);
1274 /* --------------------------------------------------------------------------------------------- */
1275 /** This routine is deeply sticked to the two panels idea.
1276 What should it do in more panels. ANSWER - don't use it
1277 in any multiple panels environment. */
1279 void
1280 swap_panels (void)
1282 WPanel *panel1, *panel2;
1283 Widget *tmp_widget;
1285 panel1 = PANEL (panels[0].widget);
1286 panel2 = PANEL (panels[1].widget);
1288 if (panels[0].type == view_listing && panels[1].type == view_listing &&
1289 !mc_config_get_bool (mc_global.main_config, CONFIG_PANELS_SECTION, "simple_swap", FALSE))
1291 WPanel panel;
1293 #define panelswap(x) panel.x = panel1->x; panel1->x = panel2->x; panel2->x = panel.x;
1294 /* Change content and related stuff */
1295 panelswap (dir);
1296 panelswap (active);
1297 panelswap (cwd_vpath);
1298 panelswap (lwd_vpath);
1299 panelswap (marked);
1300 panelswap (dirs_marked);
1301 panelswap (total);
1302 panelswap (top);
1303 panelswap (current);
1304 panelswap (is_panelized);
1305 panelswap (panelized_descr);
1306 panelswap (dir_stat);
1307 #undef panelswap
1309 panel1->quick_search.active = FALSE;
1310 panel2->quick_search.active = FALSE;
1312 if (current_panel == panel1)
1313 current_panel = panel2;
1314 else
1315 current_panel = panel1;
1317 /* if sort options are different -> resort panels */
1318 if (memcmp (&panel1->sort_info, &panel2->sort_info, sizeof (dir_sort_options_t)) != 0)
1320 panel_re_sort (other_panel);
1321 panel_re_sort (current_panel);
1324 if (widget_is_active (panels[0].widget))
1325 widget_select (panels[1].widget);
1326 else if (widget_is_active (panels[1].widget))
1327 widget_select (panels[0].widget);
1329 else
1331 WPanel *tmp_panel;
1332 WRect r;
1333 int tmp_type;
1335 tmp_panel = right_panel;
1336 right_panel = left_panel;
1337 left_panel = tmp_panel;
1339 if (panels[0].type == view_listing)
1341 if (strcmp (panel1->name, get_nth_panel_name (0)) == 0)
1343 g_free (panel1->name);
1344 panel1->name = g_strdup (get_nth_panel_name (1));
1347 if (panels[1].type == view_listing)
1349 if (strcmp (panel2->name, get_nth_panel_name (1)) == 0)
1351 g_free (panel2->name);
1352 panel2->name = g_strdup (get_nth_panel_name (0));
1356 r = panels[0].widget->rect;
1357 panels[0].widget->rect = panels[1].widget->rect;
1358 panels[1].widget->rect = r;
1360 tmp_widget = panels[0].widget;
1361 panels[0].widget = panels[1].widget;
1362 panels[1].widget = tmp_widget;
1363 tmp_type = panels[0].type;
1364 panels[0].type = panels[1].type;
1365 panels[1].type = tmp_type;
1367 /* force update formats because of possible changed sizes */
1368 if (panels[0].type == view_listing)
1369 set_panel_formats (PANEL (panels[0].widget));
1370 if (panels[1].type == view_listing)
1371 set_panel_formats (PANEL (panels[1].widget));
1375 /* --------------------------------------------------------------------------------------------- */
1377 panel_view_mode_t
1378 get_panel_type (int idx)
1380 return panels[idx].type;
1383 /* --------------------------------------------------------------------------------------------- */
1385 Widget *
1386 get_panel_widget (int idx)
1388 return panels[idx].widget;
1391 /* --------------------------------------------------------------------------------------------- */
1394 get_current_index (void)
1396 return (panels[0].widget == WIDGET (current_panel) ? 0 : 1);
1399 /* --------------------------------------------------------------------------------------------- */
1402 get_other_index (void)
1404 return (get_current_index () == 0 ? 1 : 0);
1407 /* --------------------------------------------------------------------------------------------- */
1409 WPanel *
1410 get_other_panel (void)
1412 return PANEL (get_panel_widget (get_other_index ()));
1415 /* --------------------------------------------------------------------------------------------- */
1416 /** Returns the view type for the current panel/view */
1418 panel_view_mode_t
1419 get_current_type (void)
1421 return (panels[0].widget == WIDGET (current_panel) ? panels[0].type : panels[1].type);
1424 /* --------------------------------------------------------------------------------------------- */
1425 /** Returns the view type of the unselected panel */
1427 panel_view_mode_t
1428 get_other_type (void)
1430 return (panels[0].widget == WIDGET (current_panel) ? panels[1].type : panels[0].type);
1433 /* --------------------------------------------------------------------------------------------- */
1434 /** Save current list_view widget directory into panel */
1436 void
1437 save_panel_dir (int idx)
1439 panel_view_mode_t type;
1441 type = get_panel_type (idx);
1442 if (type == view_listing)
1444 WPanel *p;
1446 p = PANEL (get_panel_widget (idx));
1447 if (p != NULL)
1449 g_free (panels[idx].last_saved_dir); /* last path no needed */
1450 /* Because path can be nonlocal */
1451 panels[idx].last_saved_dir = g_strdup (vfs_path_as_str (p->cwd_vpath));
1456 /* --------------------------------------------------------------------------------------------- */
1457 /** Return working dir, if it's view_listing - cwd,
1458 but for other types - last_saved_dir */
1460 char *
1461 get_panel_dir_for (const WPanel * widget)
1463 int i;
1465 for (i = 0; i < MAX_VIEWS; i++)
1466 if (PANEL (get_panel_widget (i)) == widget)
1467 break;
1469 if (i >= MAX_VIEWS)
1470 return g_strdup (".");
1472 if (get_panel_type (i) == view_listing)
1474 vfs_path_t *cwd_vpath;
1476 cwd_vpath = PANEL (get_panel_widget (i))->cwd_vpath;
1477 return g_strdup (vfs_path_as_str (cwd_vpath));
1480 return g_strdup (panels[i].last_saved_dir);
1483 /* --------------------------------------------------------------------------------------------- */
1485 #ifdef ENABLE_SUBSHELL
1486 gboolean
1487 do_load_prompt (void)
1489 gboolean ret = FALSE;
1491 if (!read_subshell_prompt ())
1492 return ret;
1494 /* Don't actually change the prompt if it's invisible */
1495 if (top_dlg != NULL && DIALOG (top_dlg->data) == filemanager && command_prompt)
1497 setup_cmdline ();
1499 /* since the prompt has changed, and we are called from one of the
1500 * tty_get_event channels, the prompt updating does not take place
1501 * automatically: force a cursor update and a screen refresh
1503 widget_update_cursor (WIDGET (filemanager));
1504 mc_refresh ();
1505 ret = TRUE;
1507 update_subshell_prompt = TRUE;
1508 return ret;
1511 /* --------------------------------------------------------------------------------------------- */
1514 load_prompt (int fd, void *unused)
1516 (void) fd;
1517 (void) unused;
1519 if (should_read_new_subshell_prompt)
1520 do_load_prompt ();
1521 else
1522 flush_subshell (0, QUIETLY);
1524 return 0;
1526 #endif /* ENABLE_SUBSHELL */
1528 /* --------------------------------------------------------------------------------------------- */
1530 void
1531 title_path_prepare (char **path, char **login)
1533 char host[BUF_TINY];
1534 struct passwd *pw = NULL;
1535 int res = 0;
1537 *path =
1538 vfs_path_to_str_flags (current_panel->cwd_vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
1540 res = gethostname (host, sizeof (host));
1541 if (res != 0)
1542 host[0] = '\0';
1543 else
1544 host[sizeof (host) - 1] = '\0';
1546 pw = getpwuid (getuid ());
1547 if (pw != NULL)
1548 *login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1549 else
1550 *login = g_strdup (host);
1553 /* --------------------------------------------------------------------------------------------- */
1555 /** Show current directory in the xterm title */
1556 void
1557 update_xterm_title_path (void)
1559 if (mc_global.tty.xterm_flag && xterm_title)
1561 char *p;
1562 char *path;
1563 char *login;
1565 title_path_prepare (&path, &login);
1567 p = g_strdup_printf ("mc [%s]:%s", login, path);
1568 g_free (login);
1569 g_free (path);
1571 fprintf (stdout, ESC_STR "]0;%s" ESC_STR "\\", str_term_form (p));
1572 g_free (p);
1574 if (!mc_global.tty.alternate_plus_minus)
1575 numeric_keypad_mode ();
1576 (void) fflush (stdout);
1580 /* --------------------------------------------------------------------------------------------- */
1582 /** Tell the current directory to the terminal so it can open new tabs there */
1583 void
1584 update_terminal_cwd (void)
1586 if (mc_global.tty.xterm_flag && vfs_current_is_local ())
1588 const gchar *host;
1589 char *path, *path_uri;
1591 host = g_get_host_name ();
1592 path = vfs_path_to_str_flags (current_panel->cwd_vpath, 0, VPF_NONE);
1593 path_uri = g_uri_escape_string (path, "/", FALSE);
1595 fprintf (stdout, ESC_STR "]7;file://%s%s" ESC_STR "\\", host, path_uri);
1596 (void) fflush (stdout);
1598 g_free (path_uri);
1599 g_free (path);
1603 /* --------------------------------------------------------------------------------------------- */