(dlg_replace_widget): rename to widget_replace()
[midnight-commander.git] / src / filemanager / layout.c
blob01afca68b1d60fc4385c7dcd15b93023715b52dc
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, 2012, 2013
6 The Free Software Foundation, Inc.
8 Written by:
9 Janne Kukonlehto, 1995
10 Miguel de Icaza, 1995
11 Andrew Borodin <aborodin@vmail.ru>, 2011, 2012, 2013
12 Slava Zanko <slavazanko@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.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 panels_layout_t _panels_layout;
150 static int equal_split;
151 static int _menubar_visible;
152 static int _output_lines;
153 static gboolean _command_prompt;
154 static int _keybar_visible;
155 static int _message_visible;
156 static gboolean _xterm_title;
157 static int _free_space;
159 static int height;
161 static WRadio *radio_widget;
163 static struct
165 const char *text;
166 int *variable;
167 WCheck *widget;
168 } check_options[] =
170 /* *INDENT-OFF* */
171 { N_("&Equal split"), &equal_split, NULL },
172 { N_("&Menubar visible"), &menubar_visible, NULL },
173 { N_("Command &prompt"), &command_prompt, NULL },
174 { N_("&Keybar visible"), &mc_global.keybar_visible, NULL },
175 { N_("H&intbar visible"), &mc_global.message_visible, NULL },
176 { N_("&XTerm window title"), &xterm_title, NULL },
177 { N_("&Show free space"), &free_space, NULL }
178 /* *INDENT-ON* */
181 static const char *output_lines_label = NULL;
182 static int output_lines_label_len;
184 static WButton *bleft_widget, *bright_widget;
186 /*** file scope functions ************************************************************************/
187 /* --------------------------------------------------------------------------------------------- */
189 /* don't use max() macro to avoid double call of str_term_width1() in widget width calculation */
190 #undef max
192 static int
193 max (int a, int b)
195 return a > b ? a : b;
198 /* --------------------------------------------------------------------------------------------- */
200 static void
201 check_split (panels_layout_t * layout)
203 if (layout->horizontal_split)
205 if (layout->horizontal_equal)
206 layout->top_panel_size = height / 2;
207 else if (layout->top_panel_size < MINHEIGHT)
208 layout->top_panel_size = MINHEIGHT;
209 else if (layout->top_panel_size > height - MINHEIGHT)
210 layout->top_panel_size = height - MINHEIGHT;
212 else
214 if (layout->vertical_equal)
215 layout->left_panel_size = COLS / 2;
216 else if (layout->left_panel_size < MINWIDTH)
217 layout->left_panel_size = MINWIDTH;
218 else if (layout->left_panel_size > COLS - MINWIDTH)
219 layout->left_panel_size = COLS - MINWIDTH;
223 /* --------------------------------------------------------------------------------------------- */
225 static void
226 update_split (const WDialog * h)
228 /* Check split has to be done before testing if it changed, since
229 it can change due to calling check_split() as well */
230 check_split (&_panels_layout);
231 old_layout = _panels_layout;
233 if (_panels_layout.horizontal_split)
234 check_options[0].widget->state = _panels_layout.horizontal_equal ? 1 : 0;
235 else
236 check_options[0].widget->state = _panels_layout.vertical_equal ? 1 : 0;
237 widget_redraw (WIDGET (check_options[0].widget));
239 tty_setcolor (check_options[0].widget->state & C_BOOL ? DISABLED_COLOR : COLOR_NORMAL);
241 widget_move (h, 6, 5);
242 if (_panels_layout.horizontal_split)
243 tty_printf ("%03d", _panels_layout.top_panel_size);
244 else
245 tty_printf ("%03d", _panels_layout.left_panel_size);
247 widget_move (h, 6, 17);
248 if (_panels_layout.horizontal_split)
249 tty_printf ("%03d", height - _panels_layout.top_panel_size);
250 else
251 tty_printf ("%03d", COLS - _panels_layout.left_panel_size);
253 widget_move (h, 6, 12);
254 tty_print_char ('=');
257 /* --------------------------------------------------------------------------------------------- */
259 static int
260 b_left_right_cback (WButton * button, int action)
262 (void) action;
264 if (button == bleft_widget)
266 if (_panels_layout.horizontal_split)
267 _panels_layout.top_panel_size++;
268 else
269 _panels_layout.left_panel_size++;
271 else
273 if (_panels_layout.horizontal_split)
274 _panels_layout.top_panel_size--;
275 else
276 _panels_layout.left_panel_size--;
279 update_split (WIDGET (button)->owner);
280 return 0;
283 /* --------------------------------------------------------------------------------------------- */
285 static int
286 bplus_cback (WButton * button, int action)
288 (void) button;
289 (void) action;
291 if (_output_lines < 99)
292 _output_lines++;
293 return 0;
296 /* --------------------------------------------------------------------------------------------- */
298 static int
299 bminus_cback (WButton * button, int action)
301 (void) button;
302 (void) action;
304 if (_output_lines > 0)
305 _output_lines--;
306 return 0;
309 /* --------------------------------------------------------------------------------------------- */
311 static cb_ret_t
312 layout_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
314 WDialog *h = DIALOG (w);
316 switch (msg)
318 case MSG_DRAW:
319 /* When repainting the whole dialog (e.g. with C-l) we have to
320 update everything */
321 dlg_default_repaint (h);
323 old_layout.horizontal_split = -1;
324 old_layout.left_panel_size = -1;
325 old_layout.top_panel_size = -1;
327 old_output_lines = -1;
329 update_split (h);
331 if (old_output_lines != _output_lines)
333 old_output_lines = _output_lines;
334 tty_setcolor (mc_global.tty.console_flag != '\0' ? COLOR_NORMAL : DISABLED_COLOR);
335 widget_move (h, 9, 5);
336 tty_print_string (output_lines_label);
337 widget_move (h, 9, 5 + 3 + output_lines_label_len);
338 tty_printf ("%02d", _output_lines);
340 return MSG_HANDLED;
342 case MSG_POST_KEY:
343 _menubar_visible = check_options[1].widget->state & C_BOOL;
344 _command_prompt = (check_options[2].widget->state & C_BOOL) != 0;
345 _keybar_visible = check_options[3].widget->state & C_BOOL;
346 _message_visible = check_options[4].widget->state & C_BOOL;
347 _xterm_title = (check_options[5].widget->state & C_BOOL) != 0;
348 _free_space = check_options[6].widget->state & C_BOOL;
350 if (mc_global.tty.console_flag != '\0')
352 int minimum;
354 if (_output_lines < 0)
355 _output_lines = 0;
356 height = LINES - _keybar_visible - (_command_prompt ? 1 : 0) -
357 _menubar_visible - _output_lines - _message_visible;
358 minimum = MINHEIGHT * (1 + _panels_layout.horizontal_split);
359 if (height < minimum)
361 _output_lines -= minimum - height;
362 height = minimum;
365 else
366 height = LINES - _keybar_visible - (_command_prompt ? 1 : 0) -
367 _menubar_visible - _output_lines - _message_visible;
369 if (old_output_lines != _output_lines)
371 old_output_lines = _output_lines;
372 tty_setcolor (mc_global.tty.console_flag != '\0' ? COLOR_NORMAL : DISABLED_COLOR);
373 widget_move (h, 9, 5 + 3 + output_lines_label_len);
374 tty_printf ("%02d", _output_lines);
376 return MSG_HANDLED;
378 case MSG_ACTION:
379 if (sender == WIDGET (radio_widget))
381 if (_panels_layout.horizontal_split != radio_widget->sel)
383 _panels_layout.horizontal_split = radio_widget->sel;
385 if (_panels_layout.horizontal_split)
387 if (_panels_layout.horizontal_equal)
388 _panels_layout.top_panel_size = height / 2;
390 else
392 if (_panels_layout.vertical_equal)
393 _panels_layout.left_panel_size = COLS / 2;
397 update_split (h);
399 return MSG_HANDLED;
402 if (sender == WIDGET (check_options[0].widget))
404 int eq;
406 if (_panels_layout.horizontal_split)
408 _panels_layout.horizontal_equal = check_options[0].widget->state & C_BOOL;
409 eq = _panels_layout.horizontal_equal;
411 else
413 _panels_layout.vertical_equal = check_options[0].widget->state & C_BOOL;
414 eq = _panels_layout.vertical_equal;
417 widget_disable (WIDGET (bleft_widget), eq);
418 widget_disable (WIDGET (bright_widget), eq);
420 update_split (h);
422 return MSG_HANDLED;
425 return MSG_NOT_HANDLED;
427 default:
428 return dlg_default_callback (w, sender, msg, parm, data);
432 /* --------------------------------------------------------------------------------------------- */
434 static WDialog *
435 init_layout (void)
437 WDialog *layout_dlg;
438 int l1 = 0, width;
439 int b1, b2, b;
440 size_t i;
442 const char *title1 = N_("Panel split");
443 const char *title2 = N_("Console output");
444 const char *title3 = N_("Other options");
446 const char *s_split_direction[2] = {
447 N_("&Vertical"),
448 N_("&Horizontal")
451 const char *ok_button = N_("&OK");
452 const char *cancel_button = N_("&Cancel");
454 output_lines_label = _("Output lines:");
456 /* save old params */
457 _panels_layout = panels_layout;
458 _menubar_visible = menubar_visible;
459 _command_prompt = command_prompt;
460 _keybar_visible = mc_global.keybar_visible;
461 _message_visible = mc_global.message_visible;
462 _xterm_title = xterm_title;
463 _free_space = free_space;
465 old_layout.horizontal_split = -1;
466 old_layout.left_panel_size = -1;
467 old_layout.top_panel_size = -1;
469 old_output_lines = -1;
470 _output_lines = output_lines;
472 #ifdef ENABLE_NLS
474 static gboolean i18n = FALSE;
476 title1 = _(title1);
477 title2 = _(title2);
478 title3 = _(title3);
480 i = G_N_ELEMENTS (s_split_direction);
481 while (i-- != 0)
482 s_split_direction[i] = _(s_split_direction[i]);
484 if (!i18n)
486 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
487 check_options[i].text = _(check_options[i].text);
488 i18n = TRUE;
491 ok_button = _(ok_button);
492 cancel_button = _(cancel_button);
494 #endif
496 /* radiobuttons */
497 i = G_N_ELEMENTS (s_split_direction);
498 while (i-- != 0)
499 l1 = max (l1, str_term_width1 (s_split_direction[i]) + 7);
500 /* checkboxes */
501 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
502 l1 = max (l1, str_term_width1 (check_options[i].text) + 7);
503 /* groupboxes */
504 l1 = max (l1, str_term_width1 (title1) + 4);
505 l1 = max (l1, str_term_width1 (title2) + 4);
506 l1 = max (l1, str_term_width1 (title3) + 4);
507 /* label + "+"/"-" buttons */
508 output_lines_label_len = str_term_width1 (output_lines_label);
509 l1 = max (l1, output_lines_label_len + 12);
510 /* buttons */
511 b1 = str_term_width1 (ok_button) + 5; /* default button */
512 b2 = str_term_width1 (cancel_button) + 3;
513 b = b1 + b2 + 1;
514 /* dialog width */
515 width = max (l1 * 2 + 7, b);
517 layout_dlg =
518 create_dlg (TRUE, 0, 0, 15, width, dialog_colors, layout_callback, NULL, "[Layout]",
519 _("Layout"), DLG_CENTER);
521 #define XTRACT(i) *check_options[i].variable, check_options[i].text
523 /* "Panel split" groupbox */
524 add_widget (layout_dlg, groupbox_new (2, 3, 6, l1, title1));
526 radio_widget = radio_new (3, 5, 2, s_split_direction);
527 radio_widget->sel = panels_layout.horizontal_split;
528 add_widget (layout_dlg, radio_widget);
530 check_options[0].widget = check_new (5, 5, XTRACT (0));
531 add_widget (layout_dlg, check_options[0].widget);
533 equal_split = panels_layout.horizontal_split ?
534 panels_layout.horizontal_equal : panels_layout.vertical_equal;
536 bleft_widget = button_new (6, 8, B_2LEFT, NARROW_BUTTON, "&<", b_left_right_cback);
537 widget_disable (WIDGET (bleft_widget), equal_split);
538 add_widget (layout_dlg, bleft_widget);
540 bright_widget = button_new (6, 14, B_2RIGHT, NARROW_BUTTON, "&>", b_left_right_cback);
541 widget_disable (WIDGET (bright_widget), equal_split);
542 add_widget (layout_dlg, bright_widget);
544 /* "Console output" groupbox */
546 const int disabled = mc_global.tty.console_flag != '\0' ? 0 : W_DISABLED;
547 Widget *w;
549 w = WIDGET (groupbox_new (8, 3, 3, l1, title2));
550 w->options |= disabled;
551 add_widget (layout_dlg, w);
553 w = WIDGET (button_new (9, output_lines_label_len + 5, B_PLUS,
554 NARROW_BUTTON, "&+", bplus_cback));
555 w->options |= disabled;
556 add_widget (layout_dlg, w);
558 w = WIDGET (button_new (9, output_lines_label_len + 5 + 5, B_MINUS,
559 NARROW_BUTTON, "&-", bminus_cback));
560 w->options |= disabled;
561 add_widget (layout_dlg, w);
564 /* "Other options" groupbox */
565 add_widget (layout_dlg, groupbox_new (2, 4 + l1, 9, l1, title3));
567 for (i = 1; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
569 check_options[i].widget = check_new (i + 2, 6 + l1, XTRACT (i));
570 add_widget (layout_dlg, check_options[i].widget);
573 #undef XTRACT
575 add_widget (layout_dlg, hline_new (11, -1, -1));
576 /* buttons */
577 add_widget (layout_dlg,
578 button_new (12, (width - b) / 2, B_ENTER, DEFPUSH_BUTTON, ok_button, 0));
579 add_widget (layout_dlg,
580 button_new (12, (width - b) / 2 + b1 + 1, B_CANCEL, NORMAL_BUTTON,
581 cancel_button, 0));
583 dlg_select_widget (radio_widget);
585 return layout_dlg;
588 /* --------------------------------------------------------------------------------------------- */
590 static void
591 panel_do_cols (int idx)
593 if (get_display_type (idx) == view_listing)
594 set_panel_formats ((WPanel *) panels[idx].widget);
595 else
596 panel_update_cols (panels[idx].widget, frame_half);
599 /* --------------------------------------------------------------------------------------------- */
600 /** Save current list_view widget directory into panel */
602 static Widget *
603 restore_into_right_dir_panel (int idx, Widget * from_widget)
605 WPanel *new_widget;
606 const char *saved_dir = panels[idx].last_saved_dir;
607 gboolean last_was_panel = (from_widget && get_display_type (idx) != view_listing);
608 const char *p_name = get_nth_panel_name (idx);
610 if (last_was_panel)
612 vfs_path_t *saved_dir_vpath;
614 saved_dir_vpath = vfs_path_from_str (saved_dir);
615 new_widget = panel_new_with_dir (p_name, saved_dir_vpath);
616 vfs_path_free (saved_dir_vpath);
618 else
619 new_widget = panel_new (p_name);
621 return WIDGET (new_widget);
624 /* --------------------------------------------------------------------------------------------- */
625 /*** public functions ****************************************************************************/
626 /* --------------------------------------------------------------------------------------------- */
628 void
629 layout_change (void)
631 setup_panels ();
632 /* update the main menu, because perhaps there was a change in the way
633 how the panel are split (horizontal/vertical),
634 and a change of menu visibility. */
635 update_menu ();
636 load_hint (1);
639 /* --------------------------------------------------------------------------------------------- */
641 void
642 layout_box (void)
644 WDialog *layout_dlg;
645 gboolean layout_do_change = FALSE;
647 layout_dlg = init_layout ();
649 if (run_dlg (layout_dlg) == B_ENTER)
651 size_t i;
653 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
654 if (check_options[i].widget != NULL)
655 *check_options[i].variable = check_options[i].widget->state & C_BOOL;
657 panels_layout.horizontal_split = radio_widget->sel;
658 if (panels_layout.horizontal_split)
660 panels_layout.horizontal_equal = *check_options[0].variable;
661 panels_layout.top_panel_size = _panels_layout.top_panel_size;
663 else
665 panels_layout.vertical_equal = *check_options[0].variable;
666 panels_layout.left_panel_size = _panels_layout.left_panel_size;
668 output_lines = _output_lines;
669 layout_do_change = TRUE;
672 destroy_dlg (layout_dlg);
673 if (layout_do_change)
674 layout_change ();
677 /* --------------------------------------------------------------------------------------------- */
679 void
680 setup_panels (void)
682 int start_y;
684 if (mc_global.tty.console_flag != '\0')
686 int minimum;
688 if (output_lines < 0)
689 output_lines = 0;
690 height =
691 LINES - mc_global.keybar_visible - (command_prompt ? 1 : 0) - menubar_visible -
692 output_lines - mc_global.message_visible;
693 minimum = MINHEIGHT * (1 + panels_layout.horizontal_split);
694 if (height < minimum)
696 output_lines -= minimum - height;
697 height = minimum;
700 else
702 height =
703 LINES - menubar_visible - (command_prompt ? 1 : 0) - mc_global.keybar_visible -
704 mc_global.message_visible;
707 check_split (&panels_layout);
708 start_y = menubar_visible;
710 /* The column computing is defered until panel_do_cols */
711 if (panels_layout.horizontal_split)
713 widget_set_size (panels[0].widget, start_y, 0, panels_layout.top_panel_size, 0);
714 widget_set_size (panels[1].widget, start_y + panels_layout.top_panel_size, 0,
715 height - panels_layout.top_panel_size, 0);
717 else
719 widget_set_size (panels[0].widget, start_y, 0, height, 0);
720 widget_set_size (panels[1].widget, start_y, panels_layout.left_panel_size, height, 0);
723 panel_do_cols (0);
724 panel_do_cols (1);
726 widget_set_size (WIDGET (the_menubar), 0, 0, 1, COLS);
728 if (command_prompt)
730 #ifdef ENABLE_SUBSHELL
731 if (!mc_global.tty.use_subshell || !do_load_prompt ())
732 #endif
733 setup_cmdline ();
735 else
737 widget_set_size (WIDGET (cmdline), 0, 0, 0, 0);
738 input_set_origin (cmdline, 0, 0);
739 widget_set_size (WIDGET (the_prompt), LINES, COLS, 0, 0);
742 widget_set_size (WIDGET (the_bar), LINES - 1, 0, mc_global.keybar_visible, COLS);
743 buttonbar_set_visible (the_bar, mc_global.keybar_visible);
745 /* Output window */
746 if (mc_global.tty.console_flag != '\0' && output_lines)
748 output_start_y = LINES - (command_prompt ? 1 : 0) - mc_global.keybar_visible - output_lines;
749 show_console_contents (output_start_y,
750 LINES - output_lines - mc_global.keybar_visible - 1,
751 LINES - mc_global.keybar_visible - 1);
754 if (mc_global.message_visible)
755 widget_set_size (WIDGET (the_hint), height + start_y, 0, 1, COLS);
756 else
757 widget_set_size (WIDGET (the_hint), 0, 0, 0, 0);
759 update_xterm_title_path ();
762 /* --------------------------------------------------------------------------------------------- */
764 void
765 panels_split_equal (void)
767 if (panels_layout.horizontal_split)
768 panels_layout.horizontal_equal = TRUE;
769 else
770 panels_layout.vertical_equal = TRUE;
772 layout_change ();
773 do_refresh ();
776 /* --------------------------------------------------------------------------------------------- */
778 void
779 panels_split_more (void)
781 if (panels_layout.horizontal_split)
783 panels_layout.horizontal_equal = FALSE;
784 panels_layout.top_panel_size++;
786 else
788 panels_layout.vertical_equal = FALSE;
789 panels_layout.left_panel_size++;
792 layout_change ();
795 /* --------------------------------------------------------------------------------------------- */
797 void
798 panels_split_less (void)
800 if (panels_layout.horizontal_split)
802 panels_layout.horizontal_equal = FALSE;
803 panels_layout.top_panel_size--;
805 else
807 panels_layout.vertical_equal = FALSE;
808 panels_layout.left_panel_size--;
811 layout_change ();
814 /* --------------------------------------------------------------------------------------------- */
817 void
818 setup_cmdline (void)
820 int prompt_len;
821 int y;
822 char *tmp_prompt = (char *) mc_prompt;
824 #ifdef ENABLE_SUBSHELL
825 if (mc_global.tty.use_subshell)
827 tmp_prompt = g_string_free (subshell_prompt, FALSE);
828 (void) strip_ctrl_codes (tmp_prompt);
830 #endif
832 prompt_len = str_term_width1 (tmp_prompt);
834 /* Check for prompts too big */
835 if (COLS > 8 && prompt_len > COLS - 8)
837 prompt_len = COLS - 8;
838 tmp_prompt[prompt_len] = '\0';
841 #ifdef ENABLE_SUBSHELL
842 if (mc_global.tty.use_subshell)
844 subshell_prompt = g_string_new (tmp_prompt);
845 g_free (tmp_prompt);
846 mc_prompt = subshell_prompt->str;
848 #endif
850 y = LINES - 1 - mc_global.keybar_visible;
852 widget_set_size (WIDGET (the_prompt), y, 0, 1, prompt_len);
853 label_set_text (the_prompt, mc_prompt);
854 widget_set_size (WIDGET (cmdline), y, prompt_len, 1, COLS - prompt_len);
855 input_set_origin (cmdline, prompt_len, COLS - prompt_len);
858 /* --------------------------------------------------------------------------------------------- */
860 void
861 use_dash (gboolean flag)
863 if (flag)
864 ok_to_refresh++;
865 else
866 ok_to_refresh--;
869 /* --------------------------------------------------------------------------------------------- */
871 void
872 set_hintbar (const char *str)
874 label_set_text (the_hint, str);
875 if (ok_to_refresh > 0)
876 mc_refresh ();
879 /* --------------------------------------------------------------------------------------------- */
881 void
882 rotate_dash (void)
884 static const char rotating_dash[] = "|/-\\";
885 static size_t pos = 0;
887 if (!nice_rotating_dash || (ok_to_refresh <= 0))
888 return;
890 if (pos >= sizeof (rotating_dash) - 1)
891 pos = 0;
892 tty_gotoyx (0, COLS - 1);
893 tty_setcolor (NORMAL_COLOR);
894 tty_print_char (rotating_dash[pos]);
895 mc_refresh ();
896 pos++;
899 /* --------------------------------------------------------------------------------------------- */
901 const char *
902 get_nth_panel_name (int num)
904 static char buffer[BUF_SMALL];
906 if (!num)
907 return "New Left Panel";
908 else if (num == 1)
909 return "New Right Panel";
910 else
912 g_snprintf (buffer, sizeof (buffer), "%ith Panel", num);
913 return buffer;
917 /* --------------------------------------------------------------------------------------------- */
918 /* I wonder if I should start to use the folding mode than Dugan uses */
919 /* */
920 /* This is the centralized managing of the panel display types */
921 /* This routine takes care of destroying and creating new widgets */
922 /* Please note that it could manage MAX_VIEWS, not just left and right */
923 /* Currently nothing in the code takes advantage of this and has hard- */
924 /* coded values for two panels only */
926 /* Set the num-th panel to the view type: type */
927 /* This routine also keeps at least one WPanel object in the screen */
928 /* since a lot of routines depend on the current_panel variable */
930 void
931 set_display_type (int num, panel_view_mode_t type)
933 int x = 0, y = 0, cols = 0, lines = 0;
934 unsigned int the_other = 0; /* Index to the other panel */
935 const char *file_name = NULL; /* For Quick view */
936 Widget *new_widget = NULL, *old_widget = NULL;
937 panel_view_mode_t old_type = view_listing;
938 WPanel *the_other_panel = NULL;
940 if (num >= MAX_VIEWS)
942 fprintf (stderr, "Cannot allocate more that %d views\n", MAX_VIEWS);
943 abort ();
945 /* Check that we will have a WPanel * at least */
946 if (type != view_listing)
948 the_other = num == 0 ? 1 : 0;
950 if (panels[the_other].type != view_listing)
951 return;
954 /* Get rid of it */
955 if (panels[num].widget != NULL)
957 Widget *w = panels[num].widget;
958 WPanel *panel = (WPanel *) w;
960 x = w->x;
961 y = w->y;
962 cols = w->cols;
963 lines = w->lines;
964 old_widget = w;
965 old_type = panels[num].type;
967 if (old_type == view_listing && panel->frame_size == frame_full && type != view_listing)
969 if (panels_layout.horizontal_split)
971 cols = COLS;
972 x = 0;
974 else
976 cols = COLS - panels_layout.left_panel_size;
977 if (num == 1)
978 x = panels_layout.left_panel_size;
983 /* Restoring saved path from panels.ini for nonlist panel */
984 /* when it's first creation (for example view_info) */
985 if (old_widget == NULL && type != view_listing)
987 char *panel_dir;
989 panel_dir = _vfs_get_cwd ();
990 panels[num].last_saved_dir = g_strdup (panel_dir);
991 g_free (panel_dir);
994 switch (type)
996 case view_nothing:
997 case view_listing:
998 new_widget = restore_into_right_dir_panel (num, old_widget);
999 widget_set_size (new_widget, y, x, lines, cols);
1000 break;
1002 case view_info:
1003 new_widget = WIDGET (info_new (y, x, lines, cols));
1004 break;
1006 case view_tree:
1007 new_widget = WIDGET (tree_new (y, x, lines, cols, TRUE));
1008 break;
1010 case view_quick:
1011 new_widget = WIDGET (mcview_new (y, x, lines, cols, TRUE));
1012 the_other_panel = (WPanel *) panels[the_other].widget;
1013 if (the_other_panel != NULL)
1014 file_name = the_other_panel->dir.list[the_other_panel->selected].fname;
1015 else
1016 file_name = "";
1018 mcview_load ((struct mcview_struct *) new_widget, 0, file_name, 0);
1019 break;
1022 if (type != view_listing)
1023 /* Must save dir, for restoring after change type to */
1024 /* view_listing */
1025 save_panel_dir (num);
1027 panels[num].type = type;
1028 panels[num].widget = new_widget;
1030 /* We use replace to keep the circular list of the dialog in the */
1031 /* same state. Maybe we could just kill it and then replace it */
1032 if ((midnight_dlg != NULL) && (old_widget != NULL))
1034 if (old_type == view_listing)
1036 /* save and write directory history of panel
1037 * ... and other histories of midnight_dlg */
1038 dlg_save_history (midnight_dlg);
1041 widget_replace (old_widget, new_widget);
1044 if (type == view_listing)
1046 WPanel *panel = (WPanel *) new_widget;
1048 /* if existing panel changed type to view_listing, then load history */
1049 if (old_widget != NULL)
1051 ev_history_load_save_t event_data = { NULL, new_widget };
1053 mc_event_raise (midnight_dlg->event_group, MCEVENT_HISTORY_LOAD, &event_data);
1056 if (num == 0)
1057 left_panel = panel;
1058 else
1059 right_panel = panel;
1061 /* forced update format after set new sizes */
1062 set_panel_formats (panel);
1065 if (type == view_tree)
1066 the_tree = (WTree *) new_widget;
1068 /* Prevent current_panel's value from becoming invalid.
1069 * It's just a quick hack to prevent segfaults. Comment out and
1070 * try following:
1071 * - select left panel
1072 * - invoke menue left/tree
1073 * - as long as you stay in the left panel almost everything that uses
1074 * current_panel causes segfault, e.g. C-Enter, C-x c, ...
1076 if ((type != view_listing) && (current_panel == (WPanel *) old_widget))
1077 current_panel = num == 0 ? right_panel : left_panel;
1079 g_free (old_widget);
1082 /* --------------------------------------------------------------------------------------------- */
1083 /** This routine is deeply sticked to the two panels idea.
1084 What should it do in more panels. ANSWER - don't use it
1085 in any multiple panels environment. */
1087 void
1088 swap_panels (void)
1090 WPanel *panel1, *panel2;
1091 Widget *tmp_widget;
1093 panel1 = (WPanel *) panels[0].widget;
1094 panel2 = (WPanel *) panels[1].widget;
1096 if (panels[0].type == view_listing && panels[1].type == view_listing &&
1097 !mc_config_get_bool (mc_main_config, CONFIG_PANELS_SECTION, "simple_swap", FALSE))
1099 WPanel panel;
1101 #define panelswap(x) panel.x = panel1->x; panel1->x = panel2->x; panel2->x = panel.x;
1103 #define panelswapstr(e) strcpy (panel.e, panel1->e); \
1104 strcpy (panel1->e, panel2->e); \
1105 strcpy (panel2->e, panel.e);
1106 /* Change content and related stuff */
1107 panelswap (dir);
1108 panelswap (active);
1109 panelswap (cwd_vpath);
1110 panelswap (lwd_vpath);
1111 panelswap (count);
1112 panelswap (marked);
1113 panelswap (dirs_marked);
1114 panelswap (total);
1115 panelswap (top_file);
1116 panelswap (selected);
1117 panelswap (is_panelized);
1118 panelswap (dir_stat);
1119 #undef panelswapstr
1120 #undef panelswap
1122 panel1->searching = FALSE;
1123 panel2->searching = FALSE;
1125 if (current_panel == panel1)
1126 current_panel = panel2;
1127 else
1128 current_panel = panel1;
1130 /* if sort options are different -> resort panels */
1131 if (memcmp (&panel1->sort_info, &panel2->sort_info, sizeof (panel_sort_info_t)) != 0)
1133 panel_re_sort (other_panel);
1134 panel_re_sort (current_panel);
1137 if (widget_is_active (panels[0].widget))
1138 dlg_select_widget (panels[1].widget);
1139 else if (widget_is_active (panels[1].widget))
1140 dlg_select_widget (panels[0].widget);
1142 else
1144 WPanel *tmp_panel;
1145 int x, y, cols, lines;
1146 int tmp_type;
1148 tmp_panel = right_panel;
1149 right_panel = left_panel;
1150 left_panel = tmp_panel;
1152 if (panels[0].type == view_listing)
1154 if (strcmp (panel1->panel_name, get_nth_panel_name (0)) == 0)
1156 g_free (panel1->panel_name);
1157 panel1->panel_name = g_strdup (get_nth_panel_name (1));
1160 if (panels[1].type == view_listing)
1162 if (strcmp (panel2->panel_name, get_nth_panel_name (1)) == 0)
1164 g_free (panel2->panel_name);
1165 panel2->panel_name = g_strdup (get_nth_panel_name (0));
1169 x = panels[0].widget->x;
1170 y = panels[0].widget->y;
1171 cols = panels[0].widget->cols;
1172 lines = panels[0].widget->lines;
1174 panels[0].widget->x = panels[1].widget->x;
1175 panels[0].widget->y = panels[1].widget->y;
1176 panels[0].widget->cols = panels[1].widget->cols;
1177 panels[0].widget->lines = panels[1].widget->lines;
1179 panels[1].widget->x = x;
1180 panels[1].widget->y = y;
1181 panels[1].widget->cols = cols;
1182 panels[1].widget->lines = lines;
1184 tmp_widget = panels[0].widget;
1185 panels[0].widget = panels[1].widget;
1186 panels[1].widget = tmp_widget;
1187 tmp_type = panels[0].type;
1188 panels[0].type = panels[1].type;
1189 panels[1].type = tmp_type;
1191 /* force update formats because of possible changed sizes */
1192 if (panels[0].type == view_listing)
1193 set_panel_formats ((WPanel *) panels[0].widget);
1194 if (panels[1].type == view_listing)
1195 set_panel_formats ((WPanel *) panels[1].widget);
1199 /* --------------------------------------------------------------------------------------------- */
1201 panel_view_mode_t
1202 get_display_type (int idx)
1204 return panels[idx].type;
1207 /* --------------------------------------------------------------------------------------------- */
1209 struct Widget *
1210 get_panel_widget (int idx)
1212 return panels[idx].widget;
1215 /* --------------------------------------------------------------------------------------------- */
1218 get_current_index (void)
1220 if (panels[0].widget == WIDGET (current_panel))
1221 return 0;
1222 else
1223 return 1;
1226 /* --------------------------------------------------------------------------------------------- */
1229 get_other_index (void)
1231 return !get_current_index ();
1234 /* --------------------------------------------------------------------------------------------- */
1236 struct WPanel *
1237 get_other_panel (void)
1239 return (struct WPanel *) get_panel_widget (get_other_index ());
1242 /* --------------------------------------------------------------------------------------------- */
1243 /** Returns the view type for the current panel/view */
1245 panel_view_mode_t
1246 get_current_type (void)
1248 if (panels[0].widget == WIDGET (current_panel))
1249 return panels[0].type;
1250 else
1251 return panels[1].type;
1254 /* --------------------------------------------------------------------------------------------- */
1255 /** Returns the view type of the unselected panel */
1257 panel_view_mode_t
1258 get_other_type (void)
1260 if (panels[0].widget == WIDGET (current_panel))
1261 return panels[1].type;
1262 else
1263 return panels[0].type;
1266 /* --------------------------------------------------------------------------------------------- */
1267 /** Save current list_view widget directory into panel */
1269 void
1270 save_panel_dir (int idx)
1272 panel_view_mode_t type = get_display_type (idx);
1273 Widget *widget = get_panel_widget (idx);
1275 if ((type == view_listing) && (widget != NULL))
1277 WPanel *w = (WPanel *) widget;
1279 g_free (panels[idx].last_saved_dir); /* last path no needed */
1280 /* Because path can be nonlocal */
1281 panels[idx].last_saved_dir = g_strdup (vfs_path_as_str (w->cwd_vpath));
1285 /* --------------------------------------------------------------------------------------------- */
1286 /** Return working dir, if it's view_listing - cwd,
1287 but for other types - last_saved_dir */
1289 char *
1290 get_panel_dir_for (const WPanel * widget)
1292 int i;
1294 for (i = 0; i < MAX_VIEWS; i++)
1295 if ((WPanel *) get_panel_widget (i) == widget)
1296 break;
1298 if (i >= MAX_VIEWS)
1299 return g_strdup (".");
1301 if (get_display_type (i) == view_listing)
1303 vfs_path_t *cwd_vpath;
1305 cwd_vpath = ((WPanel *) get_panel_widget (i))->cwd_vpath;
1306 return g_strdup (vfs_path_as_str (cwd_vpath));
1309 return g_strdup (panels[i].last_saved_dir);
1312 /* --------------------------------------------------------------------------------------------- */
1314 #ifdef ENABLE_SUBSHELL
1315 gboolean
1316 do_load_prompt (void)
1318 gboolean ret = FALSE;
1320 if (!read_subshell_prompt ())
1321 return ret;
1323 /* Don't actually change the prompt if it's invisible */
1324 if (top_dlg != NULL && DIALOG (top_dlg->data) == midnight_dlg && command_prompt)
1326 setup_cmdline ();
1328 /* since the prompt has changed, and we are called from one of the
1329 * tty_get_event channels, the prompt updating does not take place
1330 * automatically: force a cursor update and a screen refresh
1332 update_cursor (midnight_dlg);
1333 mc_refresh ();
1334 ret = TRUE;
1336 update_subshell_prompt = TRUE;
1337 return ret;
1340 /* --------------------------------------------------------------------------------------------- */
1343 load_prompt (int fd, void *unused)
1345 (void) fd;
1346 (void) unused;
1348 do_load_prompt ();
1349 return 0;
1351 #endif /* ENABLE_SUBSHELL */
1353 /* --------------------------------------------------------------------------------------------- */
1355 void
1356 title_path_prepare (char **path, char **login)
1358 char host[BUF_TINY];
1359 struct passwd *pw = NULL;
1360 int res = 0;
1362 *path =
1363 vfs_path_to_str_flags (current_panel->cwd_vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
1365 res = gethostname (host, sizeof (host));
1366 if (res != 0)
1367 host[0] = '\0';
1368 else
1369 host[sizeof (host) - 1] = '\0';
1371 pw = getpwuid (getuid ());
1372 if (pw != NULL)
1373 *login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1374 else
1375 *login = g_strdup (host);
1378 /* --------------------------------------------------------------------------------------------- */
1380 /** Show current directory in the xterm title */
1381 void
1382 update_xterm_title_path (void)
1384 if (mc_global.tty.xterm_flag && xterm_title)
1386 char *p;
1387 char *path;
1388 char *login;
1390 title_path_prepare (&path, &login);
1392 p = g_strdup_printf ("mc [%s]:%s", login, path);
1393 g_free (login);
1394 g_free (path);
1396 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1397 g_free (p);
1399 if (!mc_global.tty.alternate_plus_minus)
1400 numeric_keypad_mode ();
1401 (void) fflush (stdout);
1405 /* --------------------------------------------------------------------------------------------- */