move do_load_prompt() and load_prompt() from src/main.c to src/filemanager/layout.c
[midnight-commander.git] / src / filemanager / layout.c
blob7cb1b463d96ead38be125ffa55bb2cca84d1f41a
1 /*
2 Panel layout module for the Midnight Commander
4 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2006, 2007, 2009, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Janne Kukonlehto, 1995
10 Miguel de Icaza, 1995
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file layout.c
29 * \brief Source: panel layout module
32 #include <config.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <unistd.h>
40 #include "lib/global.h"
41 #include "lib/tty/tty.h"
42 #include "lib/skin.h"
43 #include "lib/tty/key.h"
44 #include "lib/tty/mouse.h"
45 #include "lib/mcconfig.h"
46 #include "lib/vfs/vfs.h" /* For _vfs_get_cwd () */
47 #include "lib/strutil.h"
48 #include "lib/widget.h"
49 #include "lib/event.h"
51 #include "src/consaver/cons.saver.h"
52 #include "src/viewer/mcviewer.h" /* The view widget */
53 #include "src/setup.h"
54 #ifdef HAVE_SUBSHELL_SUPPORT
55 #include "src/main.h" /* do_load_prompt() */
56 #include "src/subshell.h"
57 #endif
59 #include "command.h"
60 #include "midnight.h"
61 #include "tree.h"
62 /* Needed for the extern declarations of integer parameters */
63 #include "dir.h"
64 #include "layout.h"
65 #include "info.h" /* The Info widget */
67 /*** global variables ****************************************************************************/
69 panels_layout_t panels_layout = {
70 /* Set if the panels are split horizontally */
71 .horizontal_split = 0,
73 /* vertical split */
74 .vertical_equal = 1,
75 .left_panel_size = 0,
77 /* horizontal split */
78 .horizontal_equal = 1,
79 .top_panel_size = 0
82 /* Controls the display of the rotating dash on the verbose mode */
83 int nice_rotating_dash = 1;
85 /* The number of output lines shown (if available) */
86 int output_lines = 0;
88 /* Set if the command prompt is to be displayed */
89 gboolean command_prompt = TRUE;
91 /* Set if the main menu is visible */
92 int menubar_visible = 1;
94 /* Set to show current working dir in xterm window title */
95 gboolean xterm_title = TRUE;
97 /* Set to show free space on device assigned to current directory */
98 int free_space = 1;
100 /* The starting line for the output of the subprogram */
101 int output_start_y = 0;
103 int ok_to_refresh = 1;
105 /*** file scope macro definitions ****************************************************************/
107 /* The maximum number of views managed by the set_display_type routine */
108 /* Must be at least two (for current and other). Please note that until */
109 /* Janne gets around this, we will only manage two of them :-) */
110 #define MAX_VIEWS 2
112 /* Width 12 for a wee Quick (Hex) View */
113 #define MINWIDTH 12
114 #define MINHEIGHT 5
116 #define B_2LEFT B_USER
117 #define B_2RIGHT (B_USER + 1)
118 #define B_PLUS (B_USER + 2)
119 #define B_MINUS (B_USER + 3)
121 #define LAYOUT_OPTIONS_COUNT G_N_ELEMENTS (check_options)
122 #define OTHER_OPTIONS_COUNT (LAYOUT_OPTIONS_COUNT - 1)
124 /*** file scope type declarations ****************************************************************/
126 /*** file scope variables ************************************************************************/
128 static struct
130 panel_view_mode_t type;
131 Widget *widget;
132 char *last_saved_dir; /* last view_list working directory */
133 } panels[MAX_VIEWS] =
135 /* *INDENT-OFF* */
136 /* init MAX_VIEWS items */
137 { view_listing, NULL, NULL},
138 { view_listing, NULL, NULL}
139 /* *INDENT-ON* */
142 /* These variables are used to avoid updating the information unless */
143 /* we need it */
144 static panels_layout_t old_layout;
145 static int old_output_lines;
147 /* Internal variables */
148 panels_layout_t _panels_layout;
149 static int equal_split;
150 static int _menubar_visible;
151 static int _output_lines;
152 static gboolean _command_prompt;
153 static int _keybar_visible;
154 static int _message_visible;
155 static gboolean _xterm_title;
156 static int _free_space;
158 static int height;
160 static WRadio *radio_widget;
162 static struct
164 const char *text;
165 int *variable;
166 WCheck *widget;
167 } check_options[] =
169 /* *INDENT-OFF* */
170 { N_("Show free sp&ace"), &free_space, NULL},
171 { N_("&XTerm window title"), &xterm_title, NULL},
172 { N_("H&intbar visible"), &mc_global.message_visible, NULL},
173 { N_("&Keybar visible"), &mc_global.keybar_visible, NULL},
174 { N_("Command &prompt"), &command_prompt, NULL},
175 { N_("Menu&bar visible"), &menubar_visible, NULL},
176 { N_("&Equal split"), &equal_split, NULL}
177 /* *INDENT-ON* */
180 static const char *output_lines_label = NULL;
181 static int output_lines_label_len;
183 static WButton *bleft_widget, *bright_widget;
185 /*** file scope functions ************************************************************************/
186 /* --------------------------------------------------------------------------------------------- */
188 /* don't use max() macro to avoid double call of str_term_width1() in widget width calculation */
189 #undef max
191 static int
192 max (int a, int b)
194 return a > b ? a : b;
197 /* --------------------------------------------------------------------------------------------- */
199 static void
200 check_split (panels_layout_t * layout)
202 if (layout->horizontal_split)
204 if (layout->horizontal_equal)
205 layout->top_panel_size = height / 2;
206 else if (layout->top_panel_size < MINHEIGHT)
207 layout->top_panel_size = MINHEIGHT;
208 else if (layout->top_panel_size > height - MINHEIGHT)
209 layout->top_panel_size = height - MINHEIGHT;
211 else
213 if (layout->vertical_equal)
214 layout->left_panel_size = COLS / 2;
215 else if (layout->left_panel_size < MINWIDTH)
216 layout->left_panel_size = MINWIDTH;
217 else if (layout->left_panel_size > COLS - MINWIDTH)
218 layout->left_panel_size = COLS - MINWIDTH;
222 /* --------------------------------------------------------------------------------------------- */
224 static void
225 update_split (const Dlg_head * h)
227 /* Check split has to be done before testing if it changed, since
228 it can change due to calling check_split() as well */
229 check_split (&_panels_layout);
230 old_layout = _panels_layout;
232 if (_panels_layout.horizontal_split)
233 check_options[6].widget->state = _panels_layout.horizontal_equal ? 1 : 0;
234 else
235 check_options[6].widget->state = _panels_layout.vertical_equal ? 1 : 0;
236 send_message ((Widget *) check_options[6].widget, WIDGET_DRAW, 0);
238 tty_setcolor (check_options[6].widget->state & C_BOOL ? DISABLED_COLOR : COLOR_NORMAL);
240 dlg_move (h, 6, 5);
241 if (_panels_layout.horizontal_split)
242 tty_printf ("%03d", _panels_layout.top_panel_size);
243 else
244 tty_printf ("%03d", _panels_layout.left_panel_size);
246 dlg_move (h, 6, 17);
247 if (_panels_layout.horizontal_split)
248 tty_printf ("%03d", height - _panels_layout.top_panel_size);
249 else
250 tty_printf ("%03d", COLS - _panels_layout.left_panel_size);
252 dlg_move (h, 6, 12);
253 tty_print_char ('=');
256 /* --------------------------------------------------------------------------------------------- */
258 static int
259 b_left_right_cback (WButton * button, int action)
261 (void) action;
263 if (button == bleft_widget)
265 if (_panels_layout.horizontal_split)
266 _panels_layout.top_panel_size++;
267 else
268 _panels_layout.left_panel_size++;
270 else
272 if (_panels_layout.horizontal_split)
273 _panels_layout.top_panel_size--;
274 else
275 _panels_layout.left_panel_size--;
278 update_split (button->widget.owner);
279 return 0;
282 /* --------------------------------------------------------------------------------------------- */
284 static int
285 bplus_cback (WButton * button, int action)
287 (void) button;
288 (void) action;
290 if (_output_lines < 99)
291 _output_lines++;
292 return 0;
295 /* --------------------------------------------------------------------------------------------- */
297 static int
298 bminus_cback (WButton * button, int action)
300 (void) button;
301 (void) action;
303 if (_output_lines > 0)
304 _output_lines--;
305 return 0;
308 /* --------------------------------------------------------------------------------------------- */
310 static cb_ret_t
311 layout_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
313 switch (msg)
315 case DLG_DRAW:
316 /* When repainting the whole dialog (e.g. with C-l) we have to
317 update everything */
318 common_dialog_repaint (h);
320 old_layout.horizontal_split = -1;
321 old_layout.left_panel_size = -1;
322 old_layout.top_panel_size = -1;
324 old_output_lines = -1;
326 update_split (h);
328 if (old_output_lines != _output_lines)
330 old_output_lines = _output_lines;
331 tty_setcolor (mc_global.tty.console_flag != '\0' ? COLOR_NORMAL : DISABLED_COLOR);
332 dlg_move (h, 9, 5);
333 tty_print_string (output_lines_label);
334 dlg_move (h, 9, 5 + 3 + output_lines_label_len);
335 tty_printf ("%02d", _output_lines);
337 return MSG_HANDLED;
339 case DLG_POST_KEY:
340 _menubar_visible = check_options[5].widget->state & C_BOOL;
341 _command_prompt = (check_options[4].widget->state & C_BOOL) != 0;
342 _keybar_visible = check_options[3].widget->state & C_BOOL;
343 _message_visible = check_options[2].widget->state & C_BOOL;
344 _xterm_title = (check_options[1].widget->state & C_BOOL) != 0;
345 _free_space = check_options[0].widget->state & C_BOOL;
347 if (mc_global.tty.console_flag != '\0')
349 int minimum;
351 if (_output_lines < 0)
352 _output_lines = 0;
353 height = LINES - _keybar_visible - (_command_prompt ? 1 : 0) -
354 _menubar_visible - _output_lines - _message_visible;
355 minimum = MINHEIGHT * (1 + _panels_layout.horizontal_split);
356 if (height < minimum)
358 _output_lines -= minimum - height;
359 height = minimum;
362 else
363 height = LINES - _keybar_visible - (_command_prompt ? 1 : 0) -
364 _menubar_visible - _output_lines - _message_visible;
366 if (old_output_lines != _output_lines)
368 old_output_lines = _output_lines;
369 tty_setcolor (mc_global.tty.console_flag != '\0' ? COLOR_NORMAL : DISABLED_COLOR);
370 dlg_move (h, 9, 5 + 3 + output_lines_label_len);
371 tty_printf ("%02d", _output_lines);
373 return MSG_HANDLED;
375 case DLG_ACTION:
376 if (sender == (Widget *) radio_widget)
378 if (_panels_layout.horizontal_split != radio_widget->sel)
380 _panels_layout.horizontal_split = radio_widget->sel;
382 if (_panels_layout.horizontal_split)
384 if (_panels_layout.horizontal_equal)
385 _panels_layout.top_panel_size = height / 2;
387 else
389 if (_panels_layout.vertical_equal)
390 _panels_layout.left_panel_size = COLS / 2;
394 update_split (h);
396 return MSG_HANDLED;
399 if (sender == (Widget *) check_options[6].widget)
401 int eq;
403 if (_panels_layout.horizontal_split)
405 _panels_layout.horizontal_equal = check_options[6].widget->state & C_BOOL;
406 eq = _panels_layout.horizontal_equal;
408 else
410 _panels_layout.vertical_equal = check_options[6].widget->state & C_BOOL;
411 eq = _panels_layout.vertical_equal;
414 widget_disable (bleft_widget->widget, eq);
415 send_message ((Widget *) bleft_widget, WIDGET_DRAW, 0);
416 widget_disable (bright_widget->widget, eq);
417 send_message ((Widget *) bright_widget, WIDGET_DRAW, 0);
419 update_split (h);
421 return MSG_HANDLED;
424 return MSG_NOT_HANDLED;
426 default:
427 return default_dlg_callback (h, sender, msg, parm, data);
431 /* --------------------------------------------------------------------------------------------- */
433 static Dlg_head *
434 init_layout (void)
436 Dlg_head *layout_dlg;
437 int l1 = 0, width;
438 int b1, b2, b;
439 size_t i;
441 const char *title1 = N_("Panel split");
442 const char *title2 = N_("Console output");
443 const char *title3 = N_("Other options");
445 const char *s_split_direction[2] = {
446 N_("&Vertical"),
447 N_("&Horizontal")
450 const char *ok_button = N_("&OK");
451 const char *cancel_button = N_("&Cancel");
453 output_lines_label = _("Output lines:");
455 /* save old params */
456 _panels_layout = panels_layout;
457 _menubar_visible = menubar_visible;
458 _command_prompt = command_prompt;
459 _keybar_visible = mc_global.keybar_visible;
460 _message_visible = mc_global.message_visible;
461 _xterm_title = xterm_title;
462 _free_space = free_space;
464 old_layout.horizontal_split = -1;
465 old_layout.left_panel_size = -1;
466 old_layout.top_panel_size = -1;
468 old_output_lines = -1;
469 _output_lines = output_lines;
471 #ifdef ENABLE_NLS
473 static gboolean i18n = FALSE;
475 title1 = _(title1);
476 title2 = _(title2);
477 title3 = _(title3);
479 i = G_N_ELEMENTS (s_split_direction);
480 while (i-- != 0)
481 s_split_direction[i] = _(s_split_direction[i]);
483 if (!i18n)
485 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
486 check_options[i].text = _(check_options[i].text);
487 i18n = TRUE;
490 ok_button = _(ok_button);
491 cancel_button = _(cancel_button);
493 #endif
495 /* radiobuttons */
496 i = G_N_ELEMENTS (s_split_direction);
497 while (i-- != 0)
498 l1 = max (l1, str_term_width1 (s_split_direction[i]) + 7);
499 /* checkboxes */
500 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
501 l1 = max (l1, str_term_width1 (check_options[i].text) + 7);
502 /* groupboxes */
503 l1 = max (l1, str_term_width1 (title1) + 4);
504 l1 = max (l1, str_term_width1 (title2) + 4);
505 l1 = max (l1, str_term_width1 (title3) + 4);
506 /* label + "+"/"-" buttons */
507 output_lines_label_len = str_term_width1 (output_lines_label);
508 l1 = max (l1, output_lines_label_len + 12);
509 /* buttons */
510 b1 = str_term_width1 (ok_button) + 5; /* default button */
511 b2 = str_term_width1 (cancel_button) + 3;
512 b = b1 + b2 + 1;
513 /* dialog width */
514 width = max (l1 * 2 + 7, b);
516 layout_dlg =
517 create_dlg (TRUE, 0, 0, 14, width,
518 dialog_colors, layout_callback, NULL, "[Layout]",
519 _("Layout"), DLG_CENTER | DLG_REVERSE);
521 /* buttons */
522 add_widget (layout_dlg,
523 button_new (11, (width - b) / 3 * 2 + b1 + 1, B_CANCEL, NORMAL_BUTTON,
524 cancel_button, 0));
525 add_widget (layout_dlg,
526 button_new (11, (width - b) / 3, B_ENTER, DEFPUSH_BUTTON, ok_button, 0));
528 #define XTRACT(i) *check_options[i].variable, check_options[i].text
530 /* "Other options" groupbox */
531 for (i = 0; i < (size_t) OTHER_OPTIONS_COUNT; i++)
533 check_options[i].widget = check_new (OTHER_OPTIONS_COUNT - i + 2, 6 + l1, XTRACT (i));
534 add_widget (layout_dlg, check_options[i].widget);
537 add_widget (layout_dlg, groupbox_new (2, 4 + l1, 9, l1, title3));
539 /* "Console output" groupbox */
541 const int disabled = mc_global.tty.console_flag != '\0' ? 0 : W_DISABLED;
542 Widget *w;
544 w = (Widget *) button_new (9, output_lines_label_len + 5 + 5, B_MINUS,
545 NARROW_BUTTON, "&-", bminus_cback);
546 w->options |= disabled;
547 add_widget (layout_dlg, w);
549 w = (Widget *) button_new (9, output_lines_label_len + 5, B_PLUS,
550 NARROW_BUTTON, "&+", bplus_cback);
551 w->options |= disabled;
552 add_widget (layout_dlg, w);
554 w = (Widget *) groupbox_new (8, 3, 3, l1, title2);
555 w->options |= disabled;
556 add_widget (layout_dlg, w);
559 equal_split = panels_layout.horizontal_split ?
560 panels_layout.horizontal_equal : panels_layout.vertical_equal;
562 /* "Panel split" groupbox */
563 bright_widget = button_new (6, 14, B_2RIGHT, NARROW_BUTTON, "&>", b_left_right_cback);
564 widget_disable (bright_widget->widget, equal_split);
565 add_widget (layout_dlg, bright_widget);
567 bleft_widget = button_new (6, 8, B_2LEFT, NARROW_BUTTON, "&<", b_left_right_cback);
568 widget_disable (bleft_widget->widget, equal_split);
569 add_widget (layout_dlg, bleft_widget);
571 check_options[6].widget = check_new (5, 5, XTRACT (6));
572 add_widget (layout_dlg, check_options[6].widget);
574 radio_widget = radio_new (3, 5, 2, s_split_direction);
575 radio_widget->sel = panels_layout.horizontal_split;
576 add_widget (layout_dlg, radio_widget);
578 add_widget (layout_dlg, groupbox_new (2, 3, 6, l1, title1));
580 #undef XTRACT
582 return layout_dlg;
585 /* --------------------------------------------------------------------------------------------- */
587 static void
588 panel_do_cols (int idx)
590 if (get_display_type (idx) == view_listing)
591 set_panel_formats ((WPanel *) panels[idx].widget);
592 else
593 panel_update_cols (panels[idx].widget, frame_half);
596 /* --------------------------------------------------------------------------------------------- */
597 /** Save current list_view widget directory into panel */
599 static Widget *
600 restore_into_right_dir_panel (int idx, Widget * from_widget)
602 Widget *new_widget = NULL;
603 const char *saved_dir = panels[idx].last_saved_dir;
604 gboolean last_was_panel = (from_widget && get_display_type (idx) != view_listing);
605 const char *p_name = get_nth_panel_name (idx);
607 if (last_was_panel)
608 new_widget = (Widget *) panel_new_with_dir (p_name, saved_dir);
609 else
610 new_widget = (Widget *) panel_new (p_name);
612 return new_widget;
615 /* --------------------------------------------------------------------------------------------- */
616 /*** public functions ****************************************************************************/
617 /* --------------------------------------------------------------------------------------------- */
619 void
620 layout_change (void)
622 setup_panels ();
623 /* update the main menu, because perhaps there was a change in the way
624 how the panel are split (horizontal/vertical),
625 and a change of menu visibility. */
626 update_menu ();
627 load_hint (1);
630 /* --------------------------------------------------------------------------------------------- */
632 void
633 layout_box (void)
635 Dlg_head *layout_dlg;
636 gboolean layout_do_change = FALSE;
638 layout_dlg = init_layout ();
640 if (run_dlg (layout_dlg) == B_ENTER)
642 size_t i;
644 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
645 if (check_options[i].widget != NULL)
646 *check_options[i].variable = check_options[i].widget->state & C_BOOL;
648 panels_layout.horizontal_split = radio_widget->sel;
649 if (panels_layout.horizontal_split)
651 panels_layout.horizontal_equal = *check_options[6].variable;
652 panels_layout.top_panel_size = _panels_layout.top_panel_size;
654 else
656 panels_layout.vertical_equal = *check_options[6].variable;
657 panels_layout.left_panel_size = _panels_layout.left_panel_size;
659 output_lines = _output_lines;
660 layout_do_change = TRUE;
663 destroy_dlg (layout_dlg);
664 if (layout_do_change)
665 layout_change ();
668 /* --------------------------------------------------------------------------------------------- */
670 void
671 setup_panels (void)
673 int start_y;
675 if (mc_global.tty.console_flag != '\0')
677 int minimum;
679 if (output_lines < 0)
680 output_lines = 0;
681 height =
682 LINES - mc_global.keybar_visible - (command_prompt ? 1 : 0) - menubar_visible -
683 output_lines - mc_global.message_visible;
684 minimum = MINHEIGHT * (1 + panels_layout.horizontal_split);
685 if (height < minimum)
687 output_lines -= minimum - height;
688 height = minimum;
691 else
693 height =
694 LINES - menubar_visible - (command_prompt ? 1 : 0) - mc_global.keybar_visible -
695 mc_global.message_visible;
698 check_split (&panels_layout);
699 start_y = menubar_visible;
701 /* The column computing is defered until panel_do_cols */
702 if (panels_layout.horizontal_split)
704 widget_set_size (panels[0].widget, start_y, 0, panels_layout.top_panel_size, 0);
705 widget_set_size (panels[1].widget, start_y + panels_layout.top_panel_size, 0,
706 height - panels_layout.top_panel_size, 0);
708 else
710 widget_set_size (panels[0].widget, start_y, 0, height, 0);
711 widget_set_size (panels[1].widget, start_y, panels_layout.left_panel_size, height, 0);
714 panel_do_cols (0);
715 panel_do_cols (1);
717 widget_set_size (&the_menubar->widget, 0, 0, 1, COLS);
719 if (command_prompt)
721 #ifdef HAVE_SUBSHELL_SUPPORT
722 if (!mc_global.tty.use_subshell || !do_load_prompt ())
723 #endif
724 setup_cmdline ();
726 else
728 widget_set_size (&cmdline->widget, 0, 0, 0, 0);
729 input_set_origin (cmdline, 0, 0);
730 widget_set_size (&the_prompt->widget, LINES, COLS, 0, 0);
733 widget_set_size (&the_bar->widget, LINES - 1, 0, mc_global.keybar_visible, COLS);
734 buttonbar_set_visible (the_bar, mc_global.keybar_visible);
736 /* Output window */
737 if (mc_global.tty.console_flag != '\0' && output_lines)
739 output_start_y = LINES - (command_prompt ? 1 : 0) - mc_global.keybar_visible - output_lines;
740 show_console_contents (output_start_y,
741 LINES - output_lines - mc_global.keybar_visible - 1,
742 LINES - mc_global.keybar_visible - 1);
745 if (mc_global.message_visible)
746 widget_set_size (&the_hint->widget, height + start_y, 0, 1, COLS);
747 else
748 widget_set_size (&the_hint->widget, 0, 0, 0, 0);
750 update_xterm_title_path ();
753 /* --------------------------------------------------------------------------------------------- */
755 void
756 panels_split_equal (void)
758 if (panels_layout.horizontal_split)
759 panels_layout.horizontal_equal = TRUE;
760 else
761 panels_layout.vertical_equal = TRUE;
763 layout_change ();
764 do_refresh ();
767 /* --------------------------------------------------------------------------------------------- */
769 void
770 panels_split_more (void)
772 if (panels_layout.horizontal_split)
774 panels_layout.horizontal_equal = FALSE;
775 panels_layout.top_panel_size++;
777 else
779 panels_layout.vertical_equal = FALSE;
780 panels_layout.left_panel_size++;
783 layout_change ();
786 /* --------------------------------------------------------------------------------------------- */
788 void
789 panels_split_less (void)
791 if (panels_layout.horizontal_split)
793 panels_layout.horizontal_equal = FALSE;
794 panels_layout.top_panel_size--;
796 else
798 panels_layout.vertical_equal = FALSE;
799 panels_layout.left_panel_size--;
802 layout_change ();
805 /* --------------------------------------------------------------------------------------------- */
808 void
809 setup_cmdline (void)
811 int prompt_len;
812 int y;
813 char *tmp_prompt = NULL;
815 #ifdef HAVE_SUBSHELL_SUPPORT
816 if (mc_global.tty.use_subshell)
817 tmp_prompt = strip_ctrl_codes (subshell_prompt);
818 if (tmp_prompt == NULL)
819 #endif
820 tmp_prompt = (char *) mc_prompt;
821 prompt_len = str_term_width1 (tmp_prompt);
823 /* Check for prompts too big */
824 if (COLS > 8 && prompt_len > COLS - 8)
826 prompt_len = COLS - 8;
827 tmp_prompt[prompt_len] = '\0';
830 mc_prompt = tmp_prompt;
832 y = LINES - 1 - mc_global.keybar_visible;
834 widget_set_size ((Widget *) the_prompt, y, 0, 1, prompt_len);
835 label_set_text (the_prompt, mc_prompt);
836 widget_set_size ((Widget *) cmdline, y, prompt_len, 1, COLS - prompt_len);
837 input_set_origin ((WInput *) cmdline, prompt_len, COLS - prompt_len);
840 /* --------------------------------------------------------------------------------------------- */
842 void
843 use_dash (gboolean flag)
845 if (flag)
846 ok_to_refresh++;
847 else
848 ok_to_refresh--;
851 /* --------------------------------------------------------------------------------------------- */
853 void
854 set_hintbar (const char *str)
856 label_set_text (the_hint, str);
857 if (ok_to_refresh > 0)
858 mc_refresh ();
861 /* --------------------------------------------------------------------------------------------- */
863 void
864 rotate_dash (void)
866 static const char rotating_dash[] = "|/-\\";
867 static size_t pos = 0;
869 if (!nice_rotating_dash || (ok_to_refresh <= 0))
870 return;
872 if (pos >= sizeof (rotating_dash) - 1)
873 pos = 0;
874 tty_gotoyx (0, COLS - 1);
875 tty_setcolor (NORMAL_COLOR);
876 tty_print_char (rotating_dash[pos]);
877 mc_refresh ();
878 pos++;
881 /* --------------------------------------------------------------------------------------------- */
883 const char *
884 get_nth_panel_name (int num)
886 static char buffer[BUF_SMALL];
888 if (!num)
889 return "New Left Panel";
890 else if (num == 1)
891 return "New Right Panel";
892 else
894 g_snprintf (buffer, sizeof (buffer), "%ith Panel", num);
895 return buffer;
899 /* --------------------------------------------------------------------------------------------- */
900 /* I wonder if I should start to use the folding mode than Dugan uses */
901 /* */
902 /* This is the centralized managing of the panel display types */
903 /* This routine takes care of destroying and creating new widgets */
904 /* Please note that it could manage MAX_VIEWS, not just left and right */
905 /* Currently nothing in the code takes advantage of this and has hard- */
906 /* coded values for two panels only */
908 /* Set the num-th panel to the view type: type */
909 /* This routine also keeps at least one WPanel object in the screen */
910 /* since a lot of routines depend on the current_panel variable */
912 void
913 set_display_type (int num, panel_view_mode_t type)
915 int x = 0, y = 0, cols = 0, lines = 0;
916 unsigned int the_other = 0; /* Index to the other panel */
917 const char *file_name = NULL; /* For Quick view */
918 Widget *new_widget = NULL, *old_widget = NULL;
919 panel_view_mode_t old_type = view_listing;
920 WPanel *the_other_panel = NULL;
922 if (num >= MAX_VIEWS)
924 fprintf (stderr, "Cannot allocate more that %d views\n", MAX_VIEWS);
925 abort ();
927 /* Check that we will have a WPanel * at least */
928 if (type != view_listing)
930 the_other = num == 0 ? 1 : 0;
932 if (panels[the_other].type != view_listing)
933 return;
936 /* Get rid of it */
937 if (panels[num].widget != NULL)
939 Widget *w = panels[num].widget;
940 WPanel *panel = (WPanel *) w;
942 x = w->x;
943 y = w->y;
944 cols = w->cols;
945 lines = w->lines;
946 old_widget = w;
947 old_type = panels[num].type;
949 if (old_type == view_listing && panel->frame_size == frame_full && type != view_listing)
951 if (panels_layout.horizontal_split)
953 cols = COLS;
954 x = 0;
956 else
958 cols = COLS - panels_layout.left_panel_size;
959 if (num == 1)
960 x = panels_layout.left_panel_size;
965 /* Restoring saved path from panels.ini for nonlist panel */
966 /* when it's first creation (for example view_info) */
967 if (old_widget == NULL && type != view_listing)
969 char *panel_dir;
971 panel_dir = _vfs_get_cwd ();
972 panels[num].last_saved_dir = g_strdup (panel_dir);
973 g_free (panel_dir);
976 switch (type)
978 case view_nothing:
979 case view_listing:
980 new_widget = restore_into_right_dir_panel (num, old_widget);
981 widget_set_size (new_widget, y, x, lines, cols);
982 break;
984 case view_info:
985 new_widget = (Widget *) info_new (y, x, lines, cols);
986 break;
988 case view_tree:
989 new_widget = (Widget *) tree_new (y, x, lines, cols, TRUE);
990 break;
992 case view_quick:
993 new_widget = (Widget *) mcview_new (y, x, lines, cols, TRUE);
994 the_other_panel = (WPanel *) panels[the_other].widget;
995 if (the_other_panel != NULL)
996 file_name = the_other_panel->dir.list[the_other_panel->selected].fname;
997 else
998 file_name = "";
1000 mcview_load ((struct mcview_struct *) new_widget, 0, file_name, 0);
1001 break;
1004 if (type != view_listing)
1005 /* Must save dir, for restoring after change type to */
1006 /* view_listing */
1007 save_panel_dir (num);
1009 panels[num].type = type;
1010 panels[num].widget = new_widget;
1012 /* We use replace to keep the circular list of the dialog in the */
1013 /* same state. Maybe we could just kill it and then replace it */
1014 if ((midnight_dlg != NULL) && (old_widget != NULL))
1016 if (old_type == view_listing)
1018 /* save and write directory history of panel
1019 * ... and other histories of midnight_dlg */
1020 dlg_save_history (midnight_dlg);
1023 dlg_replace_widget (old_widget, new_widget);
1026 if (type == view_listing)
1028 WPanel *panel = (WPanel *) new_widget;
1030 /* if existing panel changed type to view_listing, then load history */
1031 if (old_widget != NULL)
1033 ev_history_load_save_t event_data = { NULL, new_widget };
1035 mc_event_raise (midnight_dlg->event_group, MCEVENT_HISTORY_LOAD, &event_data);
1038 if (num == 0)
1039 left_panel = panel;
1040 else
1041 right_panel = panel;
1043 /* forced update format after set new sizes */
1044 set_panel_formats (panel);
1047 if (type == view_tree)
1048 the_tree = (WTree *) new_widget;
1050 /* Prevent current_panel's value from becoming invalid.
1051 * It's just a quick hack to prevent segfaults. Comment out and
1052 * try following:
1053 * - select left panel
1054 * - invoke menue left/tree
1055 * - as long as you stay in the left panel almost everything that uses
1056 * current_panel causes segfault, e.g. C-Enter, C-x c, ...
1058 if ((type != view_listing) && (current_panel == (WPanel *) old_widget))
1059 current_panel = num == 0 ? right_panel : left_panel;
1061 g_free (old_widget);
1064 /* --------------------------------------------------------------------------------------------- */
1065 /** This routine is deeply sticked to the two panels idea.
1066 What should it do in more panels. ANSWER - don't use it
1067 in any multiple panels environment. */
1069 void
1070 swap_panels (void)
1072 WPanel *panel1, *panel2;
1073 Widget *tmp_widget;
1075 panel1 = (WPanel *) panels[0].widget;
1076 panel2 = (WPanel *) panels[1].widget;
1078 if (panels[0].type == view_listing && panels[1].type == view_listing &&
1079 !mc_config_get_bool (mc_main_config, CONFIG_PANELS_SECTION, "simple_swap", FALSE))
1081 WPanel panel;
1083 #define panelswap(x) panel.x = panel1->x; panel1->x = panel2->x; panel2->x = panel.x;
1085 #define panelswapstr(e) strcpy (panel.e, panel1->e); \
1086 strcpy (panel1->e, panel2->e); \
1087 strcpy (panel2->e, panel.e);
1088 /* Change content and related stuff */
1089 panelswap (dir);
1090 panelswap (active);
1091 panelswap (cwd_vpath);
1092 panelswap (lwd_vpath);
1093 panelswap (count);
1094 panelswap (marked);
1095 panelswap (dirs_marked);
1096 panelswap (total);
1097 panelswap (top_file);
1098 panelswap (selected);
1099 panelswap (is_panelized);
1100 panelswap (dir_stat);
1101 #undef panelswapstr
1102 #undef panelswap
1104 panel1->searching = FALSE;
1105 panel2->searching = FALSE;
1107 if (current_panel == panel1)
1108 current_panel = panel2;
1109 else
1110 current_panel = panel1;
1112 /* if sort options are different -> resort panels */
1113 if (memcmp (&panel1->sort_info, &panel2->sort_info, sizeof (panel_sort_info_t)) != 0)
1115 panel_re_sort (other_panel);
1116 panel_re_sort (current_panel);
1119 if (dlg_widget_active (panels[0].widget))
1120 dlg_select_widget (panels[1].widget);
1121 else if (dlg_widget_active (panels[1].widget))
1122 dlg_select_widget (panels[0].widget);
1124 else
1126 WPanel *tmp_panel;
1127 int x, y, cols, lines;
1128 int tmp_type;
1130 tmp_panel = right_panel;
1131 right_panel = left_panel;
1132 left_panel = tmp_panel;
1134 if (panels[0].type == view_listing)
1136 if (strcmp (panel1->panel_name, get_nth_panel_name (0)) == 0)
1138 g_free (panel1->panel_name);
1139 panel1->panel_name = g_strdup (get_nth_panel_name (1));
1142 if (panels[1].type == view_listing)
1144 if (strcmp (panel2->panel_name, get_nth_panel_name (1)) == 0)
1146 g_free (panel2->panel_name);
1147 panel2->panel_name = g_strdup (get_nth_panel_name (0));
1151 x = panels[0].widget->x;
1152 y = panels[0].widget->y;
1153 cols = panels[0].widget->cols;
1154 lines = panels[0].widget->lines;
1156 panels[0].widget->x = panels[1].widget->x;
1157 panels[0].widget->y = panels[1].widget->y;
1158 panels[0].widget->cols = panels[1].widget->cols;
1159 panels[0].widget->lines = panels[1].widget->lines;
1161 panels[1].widget->x = x;
1162 panels[1].widget->y = y;
1163 panels[1].widget->cols = cols;
1164 panels[1].widget->lines = lines;
1166 tmp_widget = panels[0].widget;
1167 panels[0].widget = panels[1].widget;
1168 panels[1].widget = tmp_widget;
1169 tmp_type = panels[0].type;
1170 panels[0].type = panels[1].type;
1171 panels[1].type = tmp_type;
1173 /* force update formats because of possible changed sizes */
1174 if (panels[0].type == view_listing)
1175 set_panel_formats ((WPanel *) panels[0].widget);
1176 if (panels[1].type == view_listing)
1177 set_panel_formats ((WPanel *) panels[1].widget);
1181 /* --------------------------------------------------------------------------------------------- */
1183 panel_view_mode_t
1184 get_display_type (int idx)
1186 return panels[idx].type;
1189 /* --------------------------------------------------------------------------------------------- */
1191 struct Widget *
1192 get_panel_widget (int idx)
1194 return panels[idx].widget;
1197 /* --------------------------------------------------------------------------------------------- */
1200 get_current_index (void)
1202 if (panels[0].widget == ((Widget *) current_panel))
1203 return 0;
1204 else
1205 return 1;
1208 /* --------------------------------------------------------------------------------------------- */
1211 get_other_index (void)
1213 return !get_current_index ();
1216 /* --------------------------------------------------------------------------------------------- */
1218 struct WPanel *
1219 get_other_panel (void)
1221 return (struct WPanel *) get_panel_widget (get_other_index ());
1224 /* --------------------------------------------------------------------------------------------- */
1225 /** Returns the view type for the current panel/view */
1227 panel_view_mode_t
1228 get_current_type (void)
1230 if (panels[0].widget == (Widget *) current_panel)
1231 return panels[0].type;
1232 else
1233 return panels[1].type;
1236 /* --------------------------------------------------------------------------------------------- */
1237 /** Returns the view type of the unselected panel */
1239 panel_view_mode_t
1240 get_other_type (void)
1242 if (panels[0].widget == (Widget *) current_panel)
1243 return panels[1].type;
1244 else
1245 return panels[0].type;
1248 /* --------------------------------------------------------------------------------------------- */
1249 /** Save current list_view widget directory into panel */
1251 void
1252 save_panel_dir (int idx)
1254 panel_view_mode_t type = get_display_type (idx);
1255 Widget *widget = get_panel_widget (idx);
1257 if ((type == view_listing) && (widget != NULL))
1259 WPanel *w = (WPanel *) widget;
1261 g_free (panels[idx].last_saved_dir); /* last path no needed */
1262 /* Because path can be nonlocal */
1263 panels[idx].last_saved_dir = vfs_path_to_str (w->cwd_vpath);
1267 /* --------------------------------------------------------------------------------------------- */
1268 /** Return working dir, if it's view_listing - cwd,
1269 but for other types - last_saved_dir */
1271 char *
1272 get_panel_dir_for (const WPanel * widget)
1274 int i;
1276 for (i = 0; i < MAX_VIEWS; i++)
1277 if ((WPanel *) get_panel_widget (i) == widget)
1278 break;
1280 if (i >= MAX_VIEWS)
1281 return g_strdup (".");
1283 if (get_display_type (i) == view_listing)
1284 return vfs_path_to_str (((WPanel *) get_panel_widget (i))->cwd_vpath);
1286 return g_strdup (panels[i].last_saved_dir);
1289 /* --------------------------------------------------------------------------------------------- */
1291 #ifdef HAVE_SUBSHELL_SUPPORT
1292 gboolean
1293 do_load_prompt (void)
1295 gboolean ret = FALSE;
1297 if (!read_subshell_prompt ())
1298 return ret;
1300 /* Don't actually change the prompt if it's invisible */
1301 if (top_dlg != NULL && ((Dlg_head *) top_dlg->data == midnight_dlg) && command_prompt)
1303 setup_cmdline ();
1305 /* since the prompt has changed, and we are called from one of the
1306 * tty_get_event channels, the prompt updating does not take place
1307 * automatically: force a cursor update and a screen refresh
1309 update_cursor (midnight_dlg);
1310 mc_refresh ();
1311 ret = TRUE;
1313 update_subshell_prompt = TRUE;
1314 return ret;
1317 /* --------------------------------------------------------------------------------------------- */
1320 load_prompt (int fd, void *unused)
1322 (void) fd;
1323 (void) unused;
1325 do_load_prompt ();
1326 return 0;
1328 #endif /* HAVE_SUBSHELL_SUPPORT */
1330 /* --------------------------------------------------------------------------------------------- */