mcview: allow set start and end of search result...
[midnight-commander.git] / src / filemanager / layout.c
blobbf6c26a5cebf579591f1146211fd5858c9e1a692
1 /*
2 Panel layout module for the Midnight Commander
4 Copyright (C) 1995-2015
5 Free Software Foundation, Inc.
7 Written by:
8 Janne Kukonlehto, 1995
9 Miguel de Icaza, 1995
10 Andrew Borodin <aborodin@vmail.ru>, 2011, 2012, 2013
11 Slava Zanko <slavazanko@gmail.com>, 2013
12 Avi Kelman <patcherton.fixesthings@gmail.com>, 2013
14 This file is part of the Midnight Commander.
16 The Midnight Commander is free software: you can redistribute it
17 and/or modify it under the terms of the GNU General Public License as
18 published by the Free Software Foundation, either version 3 of the License,
19 or (at your option) any later version.
21 The Midnight Commander is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 /** \file layout.c
31 * \brief Source: panel layout module
34 #include <config.h>
36 #include <pwd.h> /* for username in xterm title */
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <unistd.h>
43 #include "lib/global.h"
44 #include "lib/tty/tty.h"
45 #include "lib/skin.h"
46 #include "lib/tty/key.h"
47 #include "lib/tty/mouse.h"
48 #include "lib/mcconfig.h"
49 #include "lib/vfs/vfs.h" /* For _vfs_get_cwd () */
50 #include "lib/strutil.h"
51 #include "lib/widget.h"
52 #include "lib/event.h"
54 #include "src/consaver/cons.saver.h"
55 #include "src/viewer/mcviewer.h" /* The view widget */
56 #include "src/setup.h"
57 #ifdef ENABLE_SUBSHELL
58 #include "src/subshell.h"
59 #endif
61 #include "command.h"
62 #include "midnight.h"
63 #include "tree.h"
64 /* Needed for the extern declarations of integer parameters */
65 #include "dir.h"
66 #include "layout.h"
67 #include "info.h" /* The Info widget */
69 /*** global variables ****************************************************************************/
71 panels_layout_t panels_layout = {
72 /* Set if the panels are split horizontally */
73 .horizontal_split = 0,
75 /* vertical split */
76 .vertical_equal = 1,
77 .left_panel_size = 0,
79 /* horizontal split */
80 .horizontal_equal = 1,
81 .top_panel_size = 0
84 /* Controls the display of the rotating dash on the verbose mode */
85 int nice_rotating_dash = 1;
87 /* The number of output lines shown (if available) */
88 int output_lines = 0;
90 /* Set if the command prompt is to be displayed */
91 gboolean command_prompt = TRUE;
93 /* Set if the main menu is visible */
94 int menubar_visible = 1;
96 /* Set to show current working dir in xterm window title */
97 gboolean xterm_title = TRUE;
99 /* Set to show free space on device assigned to current directory */
100 int free_space = 1;
102 /* The starting line for the output of the subprogram */
103 int output_start_y = 0;
105 int ok_to_refresh = 1;
107 /*** file scope macro definitions ****************************************************************/
109 /* The maximum number of views managed by the set_display_type routine */
110 /* Must be at least two (for current and other). Please note that until */
111 /* Janne gets around this, we will only manage two of them :-) */
112 #define MAX_VIEWS 2
114 /* Width 12 for a wee Quick (Hex) View */
115 #define MINWIDTH 12
116 #define MINHEIGHT 5
118 #define B_2LEFT B_USER
119 #define B_2RIGHT (B_USER + 1)
120 #define B_PLUS (B_USER + 2)
121 #define B_MINUS (B_USER + 3)
123 #define LAYOUT_OPTIONS_COUNT G_N_ELEMENTS (check_options)
125 /*** file scope type declarations ****************************************************************/
127 /*** file scope variables ************************************************************************/
129 static struct
131 panel_view_mode_t type;
132 Widget *widget;
133 char *last_saved_dir; /* last view_list working directory */
134 } panels[MAX_VIEWS] =
136 /* *INDENT-OFF* */
137 /* init MAX_VIEWS items */
138 { view_listing, NULL, NULL},
139 { view_listing, NULL, NULL}
140 /* *INDENT-ON* */
143 /* These variables are used to avoid updating the information unless */
144 /* we need it */
145 static panels_layout_t old_layout;
146 static int old_output_lines;
148 /* Internal variables */
149 static int equal_split;
150 static int _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_("&Equal split"), &equal_split, NULL },
171 { N_("&Menubar visible"), &menubar_visible, NULL },
172 { N_("Command &prompt"), &command_prompt, NULL },
173 { N_("&Keybar visible"), &mc_global.keybar_visible, NULL },
174 { N_("H&intbar visible"), &mc_global.message_visible, NULL },
175 { N_("&XTerm window title"), &xterm_title, NULL },
176 { N_("&Show free space"), &free_space, 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 WDialog * 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);
231 if (panels_layout.horizontal_split)
232 check_options[0].widget->state = panels_layout.horizontal_equal ? 1 : 0;
233 else
234 check_options[0].widget->state = panels_layout.vertical_equal ? 1 : 0;
235 widget_redraw (WIDGET (check_options[0].widget));
237 tty_setcolor (check_options[0].widget->state & C_BOOL ? DISABLED_COLOR : COLOR_NORMAL);
239 widget_move (h, 6, 5);
240 if (panels_layout.horizontal_split)
241 tty_printf ("%03d", panels_layout.top_panel_size);
242 else
243 tty_printf ("%03d", panels_layout.left_panel_size);
245 widget_move (h, 6, 17);
246 if (panels_layout.horizontal_split)
247 tty_printf ("%03d", height - panels_layout.top_panel_size);
248 else
249 tty_printf ("%03d", COLS - panels_layout.left_panel_size);
251 widget_move (h, 6, 12);
252 tty_print_char ('=');
255 /* --------------------------------------------------------------------------------------------- */
257 static int
258 b_left_right_cback (WButton * button, int action)
260 (void) action;
262 if (button == bright_widget)
264 if (panels_layout.horizontal_split)
265 panels_layout.top_panel_size++;
266 else
267 panels_layout.left_panel_size++;
269 else
271 if (panels_layout.horizontal_split)
272 panels_layout.top_panel_size--;
273 else
274 panels_layout.left_panel_size--;
277 update_split (WIDGET (button)->owner);
278 layout_change ();
279 do_refresh ();
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_output_lines = -1;
325 update_split (h);
327 if (old_output_lines != _output_lines)
329 old_output_lines = _output_lines;
330 tty_setcolor (mc_global.tty.console_flag != '\0' ? COLOR_NORMAL : DISABLED_COLOR);
331 widget_move (h, 9, 5);
332 tty_print_string (output_lines_label);
333 widget_move (h, 9, 5 + 3 + output_lines_label_len);
334 tty_printf ("%02d", _output_lines);
336 return MSG_HANDLED;
338 case MSG_POST_KEY:
339 _menubar_visible = check_options[1].widget->state & C_BOOL;
340 _command_prompt = (check_options[2].widget->state & C_BOOL) != 0;
341 _keybar_visible = check_options[3].widget->state & C_BOOL;
342 _message_visible = check_options[4].widget->state & C_BOOL;
343 _xterm_title = (check_options[5].widget->state & C_BOOL) != 0;
344 _free_space = check_options[6].widget->state & C_BOOL;
346 if (mc_global.tty.console_flag != '\0')
348 int minimum;
350 if (_output_lines < 0)
351 _output_lines = 0;
352 height = LINES - _keybar_visible - (_command_prompt ? 1 : 0) -
353 _menubar_visible - _output_lines - _message_visible;
354 minimum = MINHEIGHT * (1 + panels_layout.horizontal_split);
355 if (height < minimum)
357 _output_lines -= minimum - height;
358 height = minimum;
361 else
362 height = LINES - _keybar_visible - (_command_prompt ? 1 : 0) -
363 _menubar_visible - _output_lines - _message_visible;
365 if (old_output_lines != _output_lines)
367 old_output_lines = _output_lines;
368 tty_setcolor (mc_global.tty.console_flag != '\0' ? COLOR_NORMAL : DISABLED_COLOR);
369 widget_move (h, 9, 5 + 3 + output_lines_label_len);
370 tty_printf ("%02d", _output_lines);
372 return MSG_HANDLED;
374 case MSG_ACTION:
375 if (sender == WIDGET (radio_widget))
377 if (panels_layout.horizontal_split != radio_widget->sel)
379 int eq;
381 panels_layout.horizontal_split = radio_widget->sel;
383 if (panels_layout.horizontal_split)
385 eq = panels_layout.horizontal_equal;
386 if (eq)
387 panels_layout.top_panel_size = height / 2;
389 else
391 eq = panels_layout.vertical_equal;
392 if (eq)
393 panels_layout.left_panel_size = COLS / 2;
396 widget_disable (WIDGET (bleft_widget), eq);
397 widget_disable (WIDGET (bright_widget), eq);
399 update_split (h);
400 layout_change ();
401 do_refresh ();
403 else
404 update_split (h);
406 return MSG_HANDLED;
409 if (sender == WIDGET (check_options[0].widget))
411 int eq;
413 if (panels_layout.horizontal_split)
415 panels_layout.horizontal_equal = check_options[0].widget->state & C_BOOL;
416 eq = panels_layout.horizontal_equal;
418 else
420 panels_layout.vertical_equal = check_options[0].widget->state & C_BOOL;
421 eq = panels_layout.vertical_equal;
424 widget_disable (WIDGET (bleft_widget), eq);
425 widget_disable (WIDGET (bright_widget), eq);
427 update_split (h);
428 layout_change ();
429 do_refresh ();
431 return MSG_HANDLED;
434 return MSG_NOT_HANDLED;
436 default:
437 return dlg_default_callback (w, sender, msg, parm, data);
441 /* --------------------------------------------------------------------------------------------- */
443 static WDialog *
444 init_layout (void)
446 WDialog *layout_dlg;
447 int l1 = 0, width;
448 int b1, b2, b;
449 size_t i;
451 const char *title1 = N_("Panel split");
452 const char *title2 = N_("Console output");
453 const char *title3 = N_("Other options");
455 const char *s_split_direction[2] = {
456 N_("&Vertical"),
457 N_("&Horizontal")
460 const char *ok_button = N_("&OK");
461 const char *cancel_button = N_("&Cancel");
463 output_lines_label = _("Output lines:");
465 /* save old params */
466 _menubar_visible = menubar_visible;
467 _command_prompt = command_prompt;
468 _keybar_visible = mc_global.keybar_visible;
469 _message_visible = mc_global.message_visible;
470 _xterm_title = xterm_title;
471 _free_space = free_space;
472 old_output_lines = -1;
473 _output_lines = output_lines;
475 #ifdef ENABLE_NLS
477 static gboolean i18n = FALSE;
479 title1 = _(title1);
480 title2 = _(title2);
481 title3 = _(title3);
483 i = G_N_ELEMENTS (s_split_direction);
484 while (i-- != 0)
485 s_split_direction[i] = _(s_split_direction[i]);
487 if (!i18n)
489 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
490 check_options[i].text = _(check_options[i].text);
491 i18n = TRUE;
494 ok_button = _(ok_button);
495 cancel_button = _(cancel_button);
497 #endif
499 /* radiobuttons */
500 i = G_N_ELEMENTS (s_split_direction);
501 while (i-- != 0)
502 l1 = max (l1, str_term_width1 (s_split_direction[i]) + 7);
503 /* checkboxes */
504 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
505 l1 = max (l1, str_term_width1 (check_options[i].text) + 7);
506 /* groupboxes */
507 l1 = max (l1, str_term_width1 (title1) + 4);
508 l1 = max (l1, str_term_width1 (title2) + 4);
509 l1 = max (l1, str_term_width1 (title3) + 4);
510 /* label + "+"/"-" buttons */
511 output_lines_label_len = str_term_width1 (output_lines_label);
512 l1 = max (l1, output_lines_label_len + 12);
513 /* buttons */
514 b1 = str_term_width1 (ok_button) + 5; /* default button */
515 b2 = str_term_width1 (cancel_button) + 3;
516 b = b1 + b2 + 1;
517 /* dialog width */
518 width = max (l1 * 2 + 7, b);
520 layout_dlg =
521 dlg_create (TRUE, 0, 0, 15, width, dialog_colors, layout_callback, NULL, "[Layout]",
522 _("Layout"), DLG_CENTER);
524 #define XTRACT(i) *check_options[i].variable, check_options[i].text
526 /* "Panel split" groupbox */
527 add_widget (layout_dlg, groupbox_new (2, 3, 6, l1, title1));
529 radio_widget = radio_new (3, 5, 2, s_split_direction);
530 radio_widget->sel = panels_layout.horizontal_split;
531 add_widget (layout_dlg, radio_widget);
533 check_options[0].widget = check_new (5, 5, XTRACT (0));
534 add_widget (layout_dlg, check_options[0].widget);
536 equal_split = panels_layout.horizontal_split ?
537 panels_layout.horizontal_equal : panels_layout.vertical_equal;
539 bleft_widget = button_new (6, 8, B_2LEFT, NARROW_BUTTON, "&<", b_left_right_cback);
540 widget_disable (WIDGET (bleft_widget), equal_split);
541 add_widget (layout_dlg, bleft_widget);
543 bright_widget = button_new (6, 14, B_2RIGHT, NARROW_BUTTON, "&>", b_left_right_cback);
544 widget_disable (WIDGET (bright_widget), equal_split);
545 add_widget (layout_dlg, bright_widget);
547 /* "Console output" groupbox */
549 const int disabled = mc_global.tty.console_flag != '\0' ? 0 : W_DISABLED;
550 Widget *w;
552 w = WIDGET (groupbox_new (8, 3, 3, l1, title2));
553 w->options |= disabled;
554 add_widget (layout_dlg, w);
556 w = WIDGET (button_new (9, output_lines_label_len + 5, B_PLUS,
557 NARROW_BUTTON, "&+", bplus_cback));
558 w->options |= disabled;
559 add_widget (layout_dlg, w);
561 w = WIDGET (button_new (9, output_lines_label_len + 5 + 5, B_MINUS,
562 NARROW_BUTTON, "&-", bminus_cback));
563 w->options |= disabled;
564 add_widget (layout_dlg, w);
567 /* "Other options" groupbox */
568 add_widget (layout_dlg, groupbox_new (2, 4 + l1, 9, l1, title3));
570 for (i = 1; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
572 check_options[i].widget = check_new (i + 2, 6 + l1, XTRACT (i));
573 add_widget (layout_dlg, check_options[i].widget);
576 #undef XTRACT
578 add_widget (layout_dlg, hline_new (11, -1, -1));
579 /* buttons */
580 add_widget (layout_dlg,
581 button_new (12, (width - b) / 2, B_ENTER, DEFPUSH_BUTTON, ok_button, 0));
582 add_widget (layout_dlg,
583 button_new (12, (width - b) / 2 + b1 + 1, B_CANCEL, NORMAL_BUTTON,
584 cancel_button, 0));
586 dlg_select_widget (radio_widget);
588 return layout_dlg;
591 /* --------------------------------------------------------------------------------------------- */
593 static void
594 panel_do_cols (int idx)
596 if (get_display_type (idx) == view_listing)
597 set_panel_formats (PANEL (panels[idx].widget));
598 else
599 panel_update_cols (panels[idx].widget, frame_half);
602 /* --------------------------------------------------------------------------------------------- */
603 /** Save current list_view widget directory into panel */
605 static Widget *
606 restore_into_right_dir_panel (int idx, Widget * from_widget)
608 WPanel *new_widget;
609 const char *saved_dir = panels[idx].last_saved_dir;
610 gboolean last_was_panel = (from_widget && get_display_type (idx) != view_listing);
611 const char *p_name = get_nth_panel_name (idx);
613 if (last_was_panel)
615 vfs_path_t *saved_dir_vpath;
617 saved_dir_vpath = vfs_path_from_str (saved_dir);
618 new_widget = panel_new_with_dir (p_name, saved_dir_vpath);
619 vfs_path_free (saved_dir_vpath);
621 else
622 new_widget = panel_new (p_name);
624 return WIDGET (new_widget);
627 /* --------------------------------------------------------------------------------------------- */
628 /*** public functions ****************************************************************************/
629 /* --------------------------------------------------------------------------------------------- */
631 void
632 layout_change (void)
634 setup_panels ();
635 /* update the main menu, because perhaps there was a change in the way
636 how the panel are split (horizontal/vertical),
637 and a change of menu visibility. */
638 update_menu ();
639 load_hint (1);
642 /* --------------------------------------------------------------------------------------------- */
644 void
645 layout_box (void)
647 WDialog *layout_dlg;
649 old_layout = panels_layout;
650 old_output_lines = output_lines;
651 layout_dlg = init_layout ();
653 if (dlg_run (layout_dlg) == B_ENTER)
655 size_t i;
657 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
658 if (check_options[i].widget != NULL)
659 *check_options[i].variable = check_options[i].widget->state & C_BOOL;
661 else
663 /* restore layout */
664 panels_layout = old_layout;
665 output_lines = old_output_lines;
666 update_split (layout_dlg);
669 dlg_destroy (layout_dlg);
670 layout_change ();
671 do_refresh ();
674 /* --------------------------------------------------------------------------------------------- */
676 void
677 setup_panels (void)
679 int start_y;
681 if (mc_global.tty.console_flag != '\0')
683 int minimum;
685 if (output_lines < 0)
686 output_lines = 0;
687 height =
688 LINES - mc_global.keybar_visible - (command_prompt ? 1 : 0) - menubar_visible -
689 output_lines - mc_global.message_visible;
690 minimum = MINHEIGHT * (1 + panels_layout.horizontal_split);
691 if (height < minimum)
693 output_lines -= minimum - height;
694 height = minimum;
697 else
699 height =
700 LINES - menubar_visible - (command_prompt ? 1 : 0) - mc_global.keybar_visible -
701 mc_global.message_visible;
704 check_split (&panels_layout);
705 start_y = menubar_visible;
707 /* The column computing is deferred until panel_do_cols */
708 if (panels_layout.horizontal_split)
710 widget_set_size (panels[0].widget, start_y, 0, panels_layout.top_panel_size, 0);
711 widget_set_size (panels[1].widget, start_y + panels_layout.top_panel_size, 0,
712 height - panels_layout.top_panel_size, 0);
714 else
716 widget_set_size (panels[0].widget, start_y, 0, height, 0);
717 widget_set_size (panels[1].widget, start_y, panels_layout.left_panel_size, height, 0);
720 panel_do_cols (0);
721 panel_do_cols (1);
723 widget_set_size (WIDGET (the_menubar), 0, 0, 1, COLS);
725 if (command_prompt)
727 #ifdef ENABLE_SUBSHELL
728 if (!mc_global.tty.use_subshell || !do_load_prompt ())
729 #endif
730 setup_cmdline ();
732 else
734 widget_set_size (WIDGET (cmdline), 0, 0, 0, 0);
735 widget_set_size (WIDGET (the_prompt), LINES, COLS, 0, 0);
738 widget_set_size (WIDGET (the_bar), LINES - 1, 0, mc_global.keybar_visible, COLS);
739 buttonbar_set_visible (the_bar, mc_global.keybar_visible);
741 /* Output window */
742 if (mc_global.tty.console_flag != '\0' && output_lines)
744 output_start_y = LINES - (command_prompt ? 1 : 0) - mc_global.keybar_visible - output_lines;
745 show_console_contents (output_start_y,
746 LINES - output_lines - mc_global.keybar_visible - 1,
747 LINES - mc_global.keybar_visible - 1);
750 if (mc_global.message_visible)
751 widget_set_size (WIDGET (the_hint), height + start_y, 0, 1, COLS);
752 else
753 widget_set_size (WIDGET (the_hint), 0, 0, 0, 0);
755 update_xterm_title_path ();
758 /* --------------------------------------------------------------------------------------------- */
760 void
761 panels_split_equal (void)
763 if (panels_layout.horizontal_split)
764 panels_layout.horizontal_equal = TRUE;
765 else
766 panels_layout.vertical_equal = TRUE;
768 layout_change ();
769 do_refresh ();
772 /* --------------------------------------------------------------------------------------------- */
774 void
775 panels_split_more (void)
777 if (panels_layout.horizontal_split)
779 panels_layout.horizontal_equal = FALSE;
780 panels_layout.top_panel_size++;
782 else
784 panels_layout.vertical_equal = FALSE;
785 panels_layout.left_panel_size++;
788 layout_change ();
791 /* --------------------------------------------------------------------------------------------- */
793 void
794 panels_split_less (void)
796 if (panels_layout.horizontal_split)
798 panels_layout.horizontal_equal = FALSE;
799 panels_layout.top_panel_size--;
801 else
803 panels_layout.vertical_equal = FALSE;
804 panels_layout.left_panel_size--;
807 layout_change ();
810 /* --------------------------------------------------------------------------------------------- */
813 void
814 setup_cmdline (void)
816 int prompt_width;
817 int y;
818 char *tmp_prompt = (char *) mc_prompt;
820 #ifdef ENABLE_SUBSHELL
821 if (mc_global.tty.use_subshell)
823 tmp_prompt = g_string_free (subshell_prompt, FALSE);
824 (void) strip_ctrl_codes (tmp_prompt);
826 #endif
828 prompt_width = str_term_width1 (tmp_prompt);
830 /* Check for prompts too big */
831 if (COLS > 8 && prompt_width > COLS - 8)
833 int prompt_len;
835 prompt_width = COLS - 8;
836 prompt_len = str_offset_to_pos (tmp_prompt, prompt_width);
837 tmp_prompt[prompt_len] = '\0';
840 #ifdef ENABLE_SUBSHELL
841 if (mc_global.tty.use_subshell)
843 subshell_prompt = g_string_new (tmp_prompt);
844 g_free (tmp_prompt);
845 mc_prompt = subshell_prompt->str;
847 #endif
849 y = LINES - 1 - mc_global.keybar_visible;
851 widget_set_size (WIDGET (the_prompt), y, 0, 1, prompt_width);
852 label_set_text (the_prompt, mc_prompt);
853 widget_set_size (WIDGET (cmdline), y, prompt_width, 1, COLS - prompt_width);
856 /* --------------------------------------------------------------------------------------------- */
858 void
859 use_dash (gboolean flag)
861 if (flag)
862 ok_to_refresh++;
863 else
864 ok_to_refresh--;
867 /* --------------------------------------------------------------------------------------------- */
869 void
870 set_hintbar (const char *str)
872 label_set_text (the_hint, str);
873 if (ok_to_refresh > 0)
874 mc_refresh ();
877 /* --------------------------------------------------------------------------------------------- */
879 void
880 rotate_dash (gboolean show)
882 Widget *w = WIDGET (midnight_dlg);
884 if (!nice_rotating_dash || (ok_to_refresh <= 0))
885 return;
887 widget_move (w, (menubar_visible != 0) ? 1 : 0, w->cols - 1);
888 tty_setcolor (NORMAL_COLOR);
890 if (!show)
891 tty_print_alt_char (ACS_URCORNER, FALSE);
892 else
894 static const char rotating_dash[4] = "|/-\\";
895 static size_t pos = 0;
897 tty_print_char (rotating_dash[pos]);
898 pos = (pos + 1) % sizeof (rotating_dash);
901 mc_refresh ();
904 /* --------------------------------------------------------------------------------------------- */
906 const char *
907 get_nth_panel_name (int num)
909 if (!num)
910 return "New Left Panel";
911 else if (num == 1)
912 return "New Right Panel";
913 else
915 static char buffer[BUF_SMALL];
917 g_snprintf (buffer, sizeof (buffer), "%ith Panel", num);
918 return buffer;
922 /* --------------------------------------------------------------------------------------------- */
923 /* I wonder if I should start to use the folding mode than Dugan uses */
924 /* */
925 /* This is the centralized managing of the panel display types */
926 /* This routine takes care of destroying and creating new widgets */
927 /* Please note that it could manage MAX_VIEWS, not just left and right */
928 /* Currently nothing in the code takes advantage of this and has hard- */
929 /* coded values for two panels only */
931 /* Set the num-th panel to the view type: type */
932 /* This routine also keeps at least one WPanel object in the screen */
933 /* since a lot of routines depend on the current_panel variable */
935 void
936 set_display_type (int num, panel_view_mode_t type)
938 int x = 0, y = 0, cols = 0, lines = 0;
939 unsigned int the_other = 0; /* Index to the other panel */
940 const char *file_name = NULL; /* For Quick view */
941 Widget *new_widget = NULL, *old_widget = NULL;
942 panel_view_mode_t old_type = view_listing;
943 WPanel *the_other_panel = NULL;
945 if (num >= MAX_VIEWS)
947 fprintf (stderr, "Cannot allocate more that %d views\n", MAX_VIEWS);
948 abort ();
950 /* Check that we will have a WPanel * at least */
951 if (type != view_listing)
953 the_other = num == 0 ? 1 : 0;
955 if (panels[the_other].type != view_listing)
956 return;
959 /* Get rid of it */
960 if (panels[num].widget != NULL)
962 Widget *w = panels[num].widget;
963 WPanel *panel = PANEL (w);
965 x = w->x;
966 y = w->y;
967 cols = w->cols;
968 lines = w->lines;
969 old_widget = w;
970 old_type = panels[num].type;
972 if (old_type == view_listing && panel->frame_size == frame_full && type != view_listing)
974 if (panels_layout.horizontal_split)
976 cols = COLS;
977 x = 0;
979 else
981 cols = COLS - panels_layout.left_panel_size;
982 if (num == 1)
983 x = panels_layout.left_panel_size;
988 /* Restoring saved path from panels.ini for nonlist panel */
989 /* when it's first creation (for example view_info) */
990 if (old_widget == NULL && type != view_listing)
992 char *panel_dir;
994 panel_dir = _vfs_get_cwd ();
995 panels[num].last_saved_dir = g_strdup (panel_dir);
996 g_free (panel_dir);
999 switch (type)
1001 case view_nothing:
1002 case view_listing:
1003 new_widget = restore_into_right_dir_panel (num, old_widget);
1004 widget_set_size (new_widget, y, x, lines, cols);
1005 break;
1007 case view_info:
1008 new_widget = WIDGET (info_new (y, x, lines, cols));
1009 break;
1011 case view_tree:
1012 new_widget = WIDGET (tree_new (y, x, lines, cols, TRUE));
1013 break;
1015 case view_quick:
1016 new_widget = WIDGET (mcview_new (y, x, lines, cols, TRUE));
1017 the_other_panel = PANEL (panels[the_other].widget);
1018 if (the_other_panel != NULL)
1019 file_name = the_other_panel->dir.list[the_other_panel->selected].fname;
1020 else
1021 file_name = "";
1023 mcview_load ((struct mcview_struct *) new_widget, 0, file_name, 0, 0, 0);
1024 break;
1026 default:
1027 break;
1030 if (type != view_listing)
1031 /* Must save dir, for restoring after change type to */
1032 /* view_listing */
1033 save_panel_dir (num);
1035 panels[num].type = type;
1036 panels[num].widget = new_widget;
1038 /* We use replace to keep the circular list of the dialog in the */
1039 /* same state. Maybe we could just kill it and then replace it */
1040 if ((midnight_dlg != NULL) && (old_widget != NULL))
1042 if (old_type == view_listing)
1044 /* save and write directory history of panel
1045 * ... and other histories of midnight_dlg */
1046 dlg_save_history (midnight_dlg);
1049 widget_replace (old_widget, new_widget);
1052 if (type == view_listing)
1054 WPanel *panel = PANEL (new_widget);
1056 /* if existing panel changed type to view_listing, then load history */
1057 if (old_widget != NULL)
1059 ev_history_load_save_t event_data = { NULL, new_widget };
1061 mc_event_raise (midnight_dlg->event_group, MCEVENT_HISTORY_LOAD, &event_data);
1064 if (num == 0)
1065 left_panel = panel;
1066 else
1067 right_panel = panel;
1069 /* forced update format after set new sizes */
1070 set_panel_formats (panel);
1073 if (type == view_tree)
1074 the_tree = (WTree *) new_widget;
1076 /* Prevent current_panel's value from becoming invalid.
1077 * It's just a quick hack to prevent segfaults. Comment out and
1078 * try following:
1079 * - select left panel
1080 * - invoke menue left/tree
1081 * - as long as you stay in the left panel almost everything that uses
1082 * current_panel causes segfault, e.g. C-Enter, C-x c, ...
1084 if ((type != view_listing) && (current_panel == PANEL (old_widget)))
1085 current_panel = num == 0 ? right_panel : left_panel;
1087 g_free (old_widget);
1090 /* --------------------------------------------------------------------------------------------- */
1091 /** This routine is deeply sticked to the two panels idea.
1092 What should it do in more panels. ANSWER - don't use it
1093 in any multiple panels environment. */
1095 void
1096 swap_panels (void)
1098 WPanel *panel1, *panel2;
1099 Widget *tmp_widget;
1101 panel1 = PANEL (panels[0].widget);
1102 panel2 = PANEL (panels[1].widget);
1104 if (panels[0].type == view_listing && panels[1].type == view_listing &&
1105 !mc_config_get_bool (mc_main_config, CONFIG_PANELS_SECTION, "simple_swap", FALSE))
1107 WPanel panel;
1109 #define panelswap(x) panel.x = panel1->x; panel1->x = panel2->x; panel2->x = panel.x;
1111 #define panelswapstr(e) strcpy (panel.e, panel1->e); \
1112 strcpy (panel1->e, panel2->e); \
1113 strcpy (panel2->e, panel.e);
1114 /* Change content and related stuff */
1115 panelswap (dir);
1116 panelswap (active);
1117 panelswap (cwd_vpath);
1118 panelswap (lwd_vpath);
1119 panelswap (marked);
1120 panelswap (dirs_marked);
1121 panelswap (total);
1122 panelswap (top_file);
1123 panelswap (selected);
1124 panelswap (is_panelized);
1125 panelswap (dir_stat);
1126 #undef panelswapstr
1127 #undef panelswap
1129 panel1->searching = FALSE;
1130 panel2->searching = FALSE;
1132 if (current_panel == panel1)
1133 current_panel = panel2;
1134 else
1135 current_panel = panel1;
1137 /* if sort options are different -> resort panels */
1138 if (memcmp (&panel1->sort_info, &panel2->sort_info, sizeof (dir_sort_options_t)) != 0)
1140 panel_re_sort (other_panel);
1141 panel_re_sort (current_panel);
1144 if (widget_is_active (panels[0].widget))
1145 dlg_select_widget (panels[1].widget);
1146 else if (widget_is_active (panels[1].widget))
1147 dlg_select_widget (panels[0].widget);
1149 else
1151 WPanel *tmp_panel;
1152 int x, y, cols, lines;
1153 int tmp_type;
1155 tmp_panel = right_panel;
1156 right_panel = left_panel;
1157 left_panel = tmp_panel;
1159 if (panels[0].type == view_listing)
1161 if (strcmp (panel1->panel_name, get_nth_panel_name (0)) == 0)
1163 g_free (panel1->panel_name);
1164 panel1->panel_name = g_strdup (get_nth_panel_name (1));
1167 if (panels[1].type == view_listing)
1169 if (strcmp (panel2->panel_name, get_nth_panel_name (1)) == 0)
1171 g_free (panel2->panel_name);
1172 panel2->panel_name = g_strdup (get_nth_panel_name (0));
1176 x = panels[0].widget->x;
1177 y = panels[0].widget->y;
1178 cols = panels[0].widget->cols;
1179 lines = panels[0].widget->lines;
1181 panels[0].widget->x = panels[1].widget->x;
1182 panels[0].widget->y = panels[1].widget->y;
1183 panels[0].widget->cols = panels[1].widget->cols;
1184 panels[0].widget->lines = panels[1].widget->lines;
1186 panels[1].widget->x = x;
1187 panels[1].widget->y = y;
1188 panels[1].widget->cols = cols;
1189 panels[1].widget->lines = lines;
1191 tmp_widget = panels[0].widget;
1192 panels[0].widget = panels[1].widget;
1193 panels[1].widget = tmp_widget;
1194 tmp_type = panels[0].type;
1195 panels[0].type = panels[1].type;
1196 panels[1].type = tmp_type;
1198 /* force update formats because of possible changed sizes */
1199 if (panels[0].type == view_listing)
1200 set_panel_formats (PANEL (panels[0].widget));
1201 if (panels[1].type == view_listing)
1202 set_panel_formats (PANEL (panels[1].widget));
1206 /* --------------------------------------------------------------------------------------------- */
1208 panel_view_mode_t
1209 get_display_type (int idx)
1211 return panels[idx].type;
1214 /* --------------------------------------------------------------------------------------------- */
1216 struct Widget *
1217 get_panel_widget (int idx)
1219 return panels[idx].widget;
1222 /* --------------------------------------------------------------------------------------------- */
1225 get_current_index (void)
1227 if (panels[0].widget == WIDGET (current_panel))
1228 return 0;
1229 else
1230 return 1;
1233 /* --------------------------------------------------------------------------------------------- */
1236 get_other_index (void)
1238 return !get_current_index ();
1241 /* --------------------------------------------------------------------------------------------- */
1243 WPanel *
1244 get_other_panel (void)
1246 return PANEL (get_panel_widget (get_other_index ()));
1249 /* --------------------------------------------------------------------------------------------- */
1250 /** Returns the view type for the current panel/view */
1252 panel_view_mode_t
1253 get_current_type (void)
1255 if (panels[0].widget == WIDGET (current_panel))
1256 return panels[0].type;
1257 else
1258 return panels[1].type;
1261 /* --------------------------------------------------------------------------------------------- */
1262 /** Returns the view type of the unselected panel */
1264 panel_view_mode_t
1265 get_other_type (void)
1267 if (panels[0].widget == WIDGET (current_panel))
1268 return panels[1].type;
1269 else
1270 return panels[0].type;
1273 /* --------------------------------------------------------------------------------------------- */
1274 /** Save current list_view widget directory into panel */
1276 void
1277 save_panel_dir (int idx)
1279 panel_view_mode_t type = get_display_type (idx);
1280 Widget *widget = get_panel_widget (idx);
1282 if ((type == view_listing) && (widget != NULL))
1284 WPanel *w = PANEL (widget);
1286 g_free (panels[idx].last_saved_dir); /* last path no needed */
1287 /* Because path can be nonlocal */
1288 panels[idx].last_saved_dir = g_strdup (vfs_path_as_str (w->cwd_vpath));
1292 /* --------------------------------------------------------------------------------------------- */
1293 /** Return working dir, if it's view_listing - cwd,
1294 but for other types - last_saved_dir */
1296 char *
1297 get_panel_dir_for (const WPanel * widget)
1299 int i;
1301 for (i = 0; i < MAX_VIEWS; i++)
1302 if (PANEL (get_panel_widget (i)) == widget)
1303 break;
1305 if (i >= MAX_VIEWS)
1306 return g_strdup (".");
1308 if (get_display_type (i) == view_listing)
1310 vfs_path_t *cwd_vpath;
1312 cwd_vpath = PANEL (get_panel_widget (i))->cwd_vpath;
1313 return g_strdup (vfs_path_as_str (cwd_vpath));
1316 return g_strdup (panels[i].last_saved_dir);
1319 /* --------------------------------------------------------------------------------------------- */
1321 #ifdef ENABLE_SUBSHELL
1322 gboolean
1323 do_load_prompt (void)
1325 gboolean ret = FALSE;
1327 if (!read_subshell_prompt ())
1328 return ret;
1330 /* Don't actually change the prompt if it's invisible */
1331 if (top_dlg != NULL && DIALOG (top_dlg->data) == midnight_dlg && command_prompt)
1333 setup_cmdline ();
1335 /* since the prompt has changed, and we are called from one of the
1336 * tty_get_event channels, the prompt updating does not take place
1337 * automatically: force a cursor update and a screen refresh
1339 update_cursor (midnight_dlg);
1340 mc_refresh ();
1341 ret = TRUE;
1343 update_subshell_prompt = TRUE;
1344 return ret;
1347 /* --------------------------------------------------------------------------------------------- */
1350 load_prompt (int fd, void *unused)
1352 (void) fd;
1353 (void) unused;
1355 do_load_prompt ();
1356 return 0;
1358 #endif /* ENABLE_SUBSHELL */
1360 /* --------------------------------------------------------------------------------------------- */
1362 void
1363 title_path_prepare (char **path, char **login)
1365 char host[BUF_TINY];
1366 struct passwd *pw = NULL;
1367 int res = 0;
1369 *path =
1370 vfs_path_to_str_flags (current_panel->cwd_vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
1372 res = gethostname (host, sizeof (host));
1373 if (res != 0)
1374 host[0] = '\0';
1375 else
1376 host[sizeof (host) - 1] = '\0';
1378 pw = getpwuid (getuid ());
1379 if (pw != NULL)
1380 *login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1381 else
1382 *login = g_strdup (host);
1385 /* --------------------------------------------------------------------------------------------- */
1387 /** Show current directory in the xterm title */
1388 void
1389 update_xterm_title_path (void)
1391 if (mc_global.tty.xterm_flag && xterm_title)
1393 char *p;
1394 char *path;
1395 char *login;
1397 title_path_prepare (&path, &login);
1399 p = g_strdup_printf ("mc [%s]:%s", login, path);
1400 g_free (login);
1401 g_free (path);
1403 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1404 g_free (p);
1406 if (!mc_global.tty.alternate_plus_minus)
1407 numeric_keypad_mode ();
1408 (void) fflush (stdout);
1412 /* --------------------------------------------------------------------------------------------- */