"Layout" dialog window: remove "Save" button
[midnight-commander.git] / src / filemanager / layout.c
blob5ad7cfbe32b258640921a1e449f6d684ac30f7c3
1 /* Panel layout module for the Midnight Commander
2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2009 Free Software Foundation, Inc.
5 Written: 1995 Janne Kukonlehto
6 1995 Miguel de Icaza
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 /** \file layout.c
24 * \brief Source: panel layout module
27 #include <config.h>
29 #include <signal.h>
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
38 * If TIOCGWINSZ supported, make it available here, because window-
39 * resizing code depends on it...
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
43 #endif
44 #include <termios.h>
45 #include <unistd.h>
47 #include "lib/global.h"
48 #include "lib/tty/tty.h"
49 #include "lib/skin.h"
50 #include "lib/tty/key.h"
51 #include "lib/tty/mouse.h"
52 #include "lib/tty/win.h" /* do_enter_ca_mode() */
53 #include "lib/mcconfig.h"
54 #include "lib/vfs/mc-vfs/vfs.h" /* For vfs_translate_url() */
55 #include "lib/strutil.h"
56 #include "lib/widget.h"
58 #include "src/main.h"
59 #include "src/consaver/cons.saver.h"
60 #include "src/viewer/mcviewer.h" /* The view widget */
61 #include "src/setup.h"
62 #include "src/subshell.h" /* For use_subshell and resize_subshell() */
63 #include "src/background.h" /* we_are_background */
65 #include "command.h"
66 #include "midnight.h"
67 #include "tree.h"
68 /* Needed for the extern declarations of integer parameters */
69 #include "dir.h"
70 #include "layout.h"
71 #include "info.h" /* The Info widget */
74 /*** global variables ****************************************************************************/
76 /* Controls the display of the rotating dash on the verbose mode */
77 int nice_rotating_dash = 1;
79 /* Set if the panels are split horizontally */
80 int horizontal_split = 0;
82 /* Set if the window has changed it's size */
83 int winch_flag = 0;
85 /* Set if the split is the same */
86 int equal_split = 1;
88 /* First panel size if the panel are not split equally */
89 int first_panel_size = 0;
91 /* The number of output lines shown (if available) */
92 int output_lines = 0;
94 /* Set if the command prompt is to be displayed */
95 int command_prompt = 1;
97 /* Set if the main menu is visible */
98 int menubar_visible = 1;
100 /* Set if the nice and useful keybar is visible */
101 int keybar_visible = 1;
103 /* Set if the nice message (hint) bar is visible */
104 int message_visible = 1;
106 /* Set to show current working dir in xterm window title */
107 int xterm_title = 1;
109 /* Set to show free space on device assigned to current directory */
110 int free_space = 1;
112 /* The starting line for the output of the subprogram */
113 int output_start_y = 0;
115 /*** file scope macro definitions ****************************************************************/
117 /* The maximum number of views managed by the set_display_type routine */
118 /* Must be at least two (for current and other). Please note that until */
119 /* Janne gets around this, we will only manage two of them :-) */
120 #define MAX_VIEWS 2
122 /* Width 12 for a wee Quick (Hex) View */
123 #define MINWIDTH 12
124 #define MINHEIGHT 5
126 #define B_2LEFT B_USER
127 #define B_2RIGHT (B_USER + 1)
128 #define B_PLUS (B_USER + 2)
129 #define B_MINUS (B_USER + 3)
131 #define LAYOUT_OPTIONS_COUNT G_N_ELEMENTS (check_options)
132 #define OTHER_OPTIONS_COUNT (LAYOUT_OPTIONS_COUNT - 1)
134 /*** file scope type declarations ****************************************************************/
136 /*** file scope variables ************************************************************************/
138 static struct
140 panel_view_mode_t type;
141 Widget *widget;
142 char *last_saved_dir; /* last view_list working directory */
143 } panels[MAX_VIEWS] =
145 /* *INDENT-OFF* */
146 /* init MAX_VIEWS items */
147 { view_listing, NULL, NULL},
148 { view_listing, NULL, NULL}
149 /* *INDENT-ON* */
152 /* These variables are used to avoid updating the information unless */
153 /* we need it */
154 static int old_first_panel_size;
155 static int old_horizontal_split;
156 static int old_output_lines;
158 /* Internal variables */
159 static int _horizontal_split;
160 static int _equal_split;
161 static int _first_panel_size;
162 static int _menubar_visible;
163 static int _output_lines;
164 static int _command_prompt;
165 static int _keybar_visible;
166 static int _message_visible;
167 static int _xterm_title;
168 static int _free_space;
170 static int height;
172 static WRadio *radio_widget;
174 static struct
176 const char *text;
177 int *variable;
178 WCheck *widget;
179 } check_options[] =
181 /* *INDENT-OFF* */
182 { N_("Show free sp&ace"), &free_space, NULL},
183 { N_("&XTerm window title"), &xterm_title, NULL},
184 { N_("H&intbar visible"), &message_visible, NULL},
185 { N_("&Keybar visible"), &keybar_visible, NULL},
186 { N_("Command &prompt"), &command_prompt, NULL},
187 { N_("Show &mini status"), &show_mini_info, NULL},
188 { N_("Menu&bar visible"), &menubar_visible, NULL},
189 { N_("&Equal split"), &equal_split, NULL}
190 /* *INDENT-ON* */
193 static const char *output_lines_label = NULL;
194 static int output_lines_label_len;
196 static WButton *bleft_widget, *bright_widget;
198 static int ok_to_refresh = 1;
200 /*** file scope functions ************************************************************************/
201 /* --------------------------------------------------------------------------------------------- */
203 /* don't use max() macro to avoid double call of str_term_width1() in widget width calculation */
204 #undef max
206 static int
207 max (int a, int b)
209 return a > b ? a : b;
212 /* --------------------------------------------------------------------------------------------- */
214 static inline void
215 _check_split (void)
217 if (_horizontal_split)
219 if (_equal_split)
220 _first_panel_size = height / 2;
221 else if (_first_panel_size < MINHEIGHT)
222 _first_panel_size = MINHEIGHT;
223 else if (_first_panel_size > height - MINHEIGHT)
224 _first_panel_size = height - MINHEIGHT;
226 else
228 if (_equal_split)
229 _first_panel_size = COLS / 2;
230 else if (_first_panel_size < MINWIDTH)
231 _first_panel_size = MINWIDTH;
232 else if (_first_panel_size > COLS - MINWIDTH)
233 _first_panel_size = COLS - MINWIDTH;
236 old_first_panel_size = _first_panel_size;
237 old_horizontal_split = _horizontal_split;
240 /* --------------------------------------------------------------------------------------------- */
242 static void
243 update_split (const Dlg_head * h)
245 /* Check split has to be done before testing if it changed, since
246 it can change due to calling _check_split() as well */
247 _check_split ();
249 tty_setcolor (check_options[7].widget->state & C_BOOL ? DISABLED_COLOR : COLOR_NORMAL);
251 dlg_move (h, 6, 5);
252 tty_printf ("%03d", _first_panel_size);
254 dlg_move (h, 6, 17);
255 if (_horizontal_split)
256 tty_printf ("%03d", height - _first_panel_size);
257 else
258 tty_printf ("%03d", COLS - _first_panel_size);
260 dlg_move (h, 6, 12);
261 tty_print_char ('=');
264 /* --------------------------------------------------------------------------------------------- */
266 static int
267 b_left_right_cback (WButton * button, int action)
269 (void) action;
271 if (button == bleft_widget)
272 _first_panel_size++;
273 else
274 _first_panel_size--;
276 update_split (button->widget.owner);
277 return 0;
280 /* --------------------------------------------------------------------------------------------- */
282 static int
283 bplus_cback (WButton * button, int action)
285 (void) button;
286 (void) action;
288 if (_output_lines < 99)
289 _output_lines++;
290 return 0;
293 /* --------------------------------------------------------------------------------------------- */
295 static int
296 bminus_cback (WButton * button, int action)
298 (void) button;
299 (void) action;
301 if (_output_lines > 0)
302 _output_lines--;
303 return 0;
306 /* --------------------------------------------------------------------------------------------- */
308 static cb_ret_t
309 layout_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
311 switch (msg)
313 case DLG_DRAW:
314 /*When repainting the whole dialog (e.g. with C-l) we have to
315 update everything */
316 common_dialog_repaint (h);
318 old_first_panel_size = -1;
319 old_horizontal_split = -1;
320 old_output_lines = -1;
322 update_split (h);
324 if (old_output_lines != _output_lines)
326 old_output_lines = _output_lines;
327 tty_setcolor (console_flag ? COLOR_NORMAL : DISABLED_COLOR);
328 dlg_move (h, 9, 5);
329 tty_print_string (output_lines_label);
330 dlg_move (h, 9, 5 + 3 + output_lines_label_len);
331 tty_printf ("%02d", _output_lines);
333 return MSG_HANDLED;
335 case DLG_POST_KEY:
336 _menubar_visible = check_options[6].widget->state & C_BOOL;
337 _command_prompt = check_options[5].widget->state & C_BOOL;
338 _keybar_visible = check_options[3].widget->state & C_BOOL;
339 _message_visible = check_options[2].widget->state & C_BOOL;
340 _xterm_title = check_options[1].widget->state & C_BOOL;
341 _free_space = check_options[0].widget->state & C_BOOL;
343 if (console_flag)
345 int minimum;
346 if (_output_lines < 0)
347 _output_lines = 0;
348 height = LINES - _keybar_visible - _command_prompt -
349 _menubar_visible - _output_lines - _message_visible;
350 minimum = MINHEIGHT * (1 + _horizontal_split);
351 if (height < minimum)
353 _output_lines -= minimum - height;
354 height = minimum;
357 else
358 height = LINES - _keybar_visible - _command_prompt -
359 _menubar_visible - _output_lines - _message_visible;
361 if (old_output_lines != _output_lines)
363 old_output_lines = _output_lines;
364 tty_setcolor (console_flag ? COLOR_NORMAL : DISABLED_COLOR);
365 dlg_move (h, 9, 5 + 3 + output_lines_label_len);
366 tty_printf ("%02d", _output_lines);
368 return MSG_HANDLED;
370 case DLG_ACTION:
371 if (sender == (Widget *) radio_widget)
373 if (_horizontal_split != radio_widget->sel)
375 _horizontal_split = radio_widget->sel;
376 if (_equal_split)
378 if (_horizontal_split)
379 _first_panel_size = height / 2;
380 else
381 _first_panel_size = COLS / 2;
385 update_split (h);
387 return MSG_HANDLED;
390 if (sender == (Widget *) check_options[7].widget)
392 _equal_split = check_options[7].widget->state & C_BOOL;
394 widget_disable (bleft_widget->widget, _equal_split);
395 send_message ((Widget *) bleft_widget, WIDGET_DRAW, 0);
396 widget_disable (bright_widget->widget, _equal_split);
397 send_message ((Widget *) bright_widget, WIDGET_DRAW, 0);
399 update_split (h);
401 return MSG_HANDLED;
404 return MSG_NOT_HANDLED;
406 default:
407 return default_dlg_callback (h, sender, msg, parm, data);
411 /* --------------------------------------------------------------------------------------------- */
413 static Dlg_head *
414 init_layout (void)
416 Dlg_head *layout_dlg;
417 int l1 = 0, width;
418 int b1, b2, b;
419 size_t i;
421 const char *title1 = N_("Panel split");
422 const char *title2 = N_("Console output");
423 const char *title3 = N_("Other options");
425 const char *s_split_direction[2] = {
426 N_("&Vertical"),
427 N_("&Horizontal")
430 const char *ok_button = N_("&OK");
431 const char *cancel_button = N_("&Cancel");
433 output_lines_label = _("Output lines:");
435 /* save old params */
436 _equal_split = equal_split;
437 _menubar_visible = menubar_visible;
438 _command_prompt = command_prompt;
439 _keybar_visible = keybar_visible;
440 _message_visible = message_visible;
441 _xterm_title = xterm_title;
442 _free_space = free_space;
443 old_first_panel_size = -1;
444 old_horizontal_split = -1;
445 old_output_lines = -1;
446 _first_panel_size = first_panel_size;
447 _output_lines = output_lines;
449 #ifdef ENABLE_NLS
451 static gboolean i18n = FALSE;
453 title1 = _(title1);
454 title2 = _(title2);
455 title3 = _(title3);
457 i = G_N_ELEMENTS (s_split_direction);
458 while (i-- != 0)
459 s_split_direction[i] = _(s_split_direction[i]);
461 if (!i18n)
463 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
464 check_options[i].text = _(check_options[i].text);
465 i18n = TRUE;
468 ok_button = _(ok_button);
469 cancel_button = _(cancel_button);
471 #endif
473 /* radiobuttons */
474 i = G_N_ELEMENTS (s_split_direction);
475 while (i-- != 0)
476 l1 = max (l1, str_term_width1 (s_split_direction[i]) + 7);
477 /* checkboxes */
478 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
479 l1 = max (l1, str_term_width1 (check_options[i].text) + 7);
480 /* groupboxes */
481 l1 = max (l1, str_term_width1 (title1) + 4);
482 l1 = max (l1, str_term_width1 (title2) + 4);
483 l1 = max (l1, str_term_width1 (title3) + 4);
484 /* label + "+"/"-" buttons */
485 output_lines_label_len = str_term_width1 (output_lines_label);
486 l1 = max (l1, output_lines_label_len + 12);
487 /* buttons */
488 b1 = str_term_width1 (ok_button) + 5; /* default button */
489 b2 = str_term_width1 (cancel_button) + 3;
490 b = b1 + b2 + 1;
491 /* dialog width */
492 width = max (l1 * 2 + 7, b);
494 layout_dlg =
495 create_dlg (TRUE, 0, 0, 14, width,
496 dialog_colors, layout_callback, "[Layout]",
497 _("Layout"), DLG_CENTER | DLG_REVERSE);
499 /* buttons */
500 add_widget (layout_dlg,
501 button_new (11, (width - b) / 3 * 2 + b1 + 1, B_CANCEL, NORMAL_BUTTON,
502 cancel_button, 0));
503 add_widget (layout_dlg,
504 button_new (11, (width - b) / 3, B_ENTER, DEFPUSH_BUTTON, ok_button, 0));
506 #define XTRACT(i) *check_options[i].variable, check_options[i].text
508 /* "Other options" groupbox */
509 for (i = 0; i < (size_t) OTHER_OPTIONS_COUNT; i++)
511 check_options[i].widget = check_new (OTHER_OPTIONS_COUNT - i + 2, 6 + l1, XTRACT (i));
512 add_widget (layout_dlg, check_options[i].widget);
515 add_widget (layout_dlg, groupbox_new (2, 4 + l1, 9, l1, title3));
517 /* "Console output" groupbox */
519 const int disabled = console_flag ? 0 : W_DISABLED;
520 Widget *w;
522 w = (Widget *) button_new (9, output_lines_label_len + 5 + 5, B_MINUS,
523 NARROW_BUTTON, "&-", bminus_cback);
524 w->options |= disabled;
525 add_widget (layout_dlg, w);
527 w = (Widget *) button_new (9, output_lines_label_len + 5, B_PLUS,
528 NARROW_BUTTON, "&+", bplus_cback);
529 w->options |= disabled;
530 add_widget (layout_dlg, w);
532 w = (Widget *) groupbox_new (8, 3, 3, l1, title2);
533 w->options |= disabled;
534 add_widget (layout_dlg, w);
538 /* "Panel split" groupbox */
539 bright_widget = button_new (6, 14, B_2RIGHT, NARROW_BUTTON, "&>", b_left_right_cback);
540 widget_disable (bright_widget->widget, _equal_split);
541 add_widget (layout_dlg, bright_widget);
543 bleft_widget = button_new (6, 8, B_2LEFT, NARROW_BUTTON, "&<", b_left_right_cback);
544 widget_disable (bleft_widget->widget, _equal_split);
545 add_widget (layout_dlg, bleft_widget);
547 check_options[7].widget = check_new (5, 5, XTRACT (7));
548 add_widget (layout_dlg, check_options[7].widget);
550 radio_widget = radio_new (3, 5, 2, s_split_direction);
551 radio_widget->sel = horizontal_split;
552 add_widget (layout_dlg, radio_widget);
554 add_widget (layout_dlg, groupbox_new (2, 3, 6, l1, title1));
556 #undef XTRACT
558 return layout_dlg;
561 /* --------------------------------------------------------------------------------------------- */
563 static void
564 check_split (void)
566 if (horizontal_split)
568 if (equal_split)
569 first_panel_size = height / 2;
570 else if (first_panel_size < MINHEIGHT)
571 first_panel_size = MINHEIGHT;
572 else if (first_panel_size > height - MINHEIGHT)
573 first_panel_size = height - MINHEIGHT;
575 else
577 if (equal_split)
578 first_panel_size = COLS / 2;
579 else if (first_panel_size < MINWIDTH)
580 first_panel_size = MINWIDTH;
581 else if (first_panel_size > COLS - MINWIDTH)
582 first_panel_size = COLS - MINWIDTH;
586 /* --------------------------------------------------------------------------------------------- */
588 static void
589 panel_do_cols (int idx)
591 if (get_display_type (idx) == view_listing)
592 set_panel_formats ((WPanel *) panels[idx].widget);
593 else
594 panel_update_cols (panels[idx].widget, frame_half);
597 /* --------------------------------------------------------------------------------------------- */
598 /** Save current list_view widget directory into panel */
600 static Widget *
601 restore_into_right_dir_panel (int idx, Widget * from_widget)
603 Widget *new_widget = NULL;
604 const char *saved_dir = panels[idx].last_saved_dir;
605 gboolean last_was_panel = (from_widget && get_display_type (idx) != view_listing);
606 const char *p_name = get_nth_panel_name (idx);
608 if (last_was_panel)
609 new_widget = (Widget *) panel_new_with_dir (p_name, saved_dir);
610 else
611 new_widget = (Widget *) panel_new (p_name);
613 return new_widget;
616 /* --------------------------------------------------------------------------------------------- */
618 static inline void
619 low_level_change_screen_size (void)
621 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
622 #if defined TIOCGWINSZ
623 struct winsize winsz;
625 winsz.ws_col = winsz.ws_row = 0;
626 /* Ioctl on the STDIN_FILENO */
627 ioctl (0, TIOCGWINSZ, &winsz);
628 if (winsz.ws_col && winsz.ws_row)
630 #if defined(NCURSES_VERSION) && defined(HAVE_RESIZETERM)
631 resizeterm (winsz.ws_row, winsz.ws_col);
632 clearok (stdscr, TRUE); /* sigwinch's should use a semaphore! */
633 #else
634 COLS = winsz.ws_col;
635 LINES = winsz.ws_row;
636 #endif
637 #ifdef HAVE_SUBSHELL_SUPPORT
638 resize_subshell ();
639 #endif
641 #endif /* TIOCGWINSZ */
642 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
645 /* --------------------------------------------------------------------------------------------- */
647 static void
648 dlg_resize_cb (void *data, void *user_data)
650 Dlg_head *d = data;
652 (void) user_data;
653 d->callback (d, NULL, DLG_RESIZE, 0, NULL);
656 /* --------------------------------------------------------------------------------------------- */
657 /*** public functions ****************************************************************************/
658 /* --------------------------------------------------------------------------------------------- */
660 void
661 layout_change (void)
663 setup_panels ();
664 /* update the main menu, because perhaps there was a change in the way
665 how the panel are split (horizontal/vertical),
666 and a change of menu visibility. */
667 update_menu ();
668 load_hint (1);
671 /* --------------------------------------------------------------------------------------------- */
673 void
674 layout_box (void)
676 Dlg_head *layout_dlg;
677 gboolean layout_do_change = FALSE;
679 layout_dlg = init_layout ();
681 if (run_dlg (layout_dlg) == B_ENTER)
683 size_t i;
684 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
685 if (check_options[i].widget != NULL)
686 *check_options[i].variable = check_options[i].widget->state & C_BOOL;
687 horizontal_split = radio_widget->sel;
688 first_panel_size = _first_panel_size;
689 output_lines = _output_lines;
690 layout_do_change = TRUE;
693 destroy_dlg (layout_dlg);
694 if (layout_do_change)
695 layout_change ();
698 /* --------------------------------------------------------------------------------------------- */
700 void
701 clr_scr (void)
703 tty_set_normal_attrs ();
704 tty_fill_region (0, 0, LINES, COLS, ' ');
705 tty_refresh ();
708 /* --------------------------------------------------------------------------------------------- */
710 void
711 repaint_screen (void)
713 do_refresh ();
714 tty_refresh ();
717 /* --------------------------------------------------------------------------------------------- */
719 void
720 mc_refresh (void)
722 #ifdef WITH_BACKGROUND
723 if (we_are_background)
724 return;
725 #endif /* WITH_BACKGROUND */
726 if (winch_flag == 0)
727 tty_refresh ();
728 else
730 /* if winch was caugth, we should do not only redraw screen, but
731 reposition/resize all */
732 change_screen_size ();
736 /* --------------------------------------------------------------------------------------------- */
738 void
739 setup_panels (void)
741 int start_y;
742 int promptl; /* the prompt len */
744 if (console_flag)
746 int minimum;
747 if (output_lines < 0)
748 output_lines = 0;
749 height =
750 LINES - keybar_visible - command_prompt - menubar_visible -
751 output_lines - message_visible;
752 minimum = MINHEIGHT * (1 + horizontal_split);
753 if (height < minimum)
755 output_lines -= minimum - height;
756 height = minimum;
759 else
761 height = LINES - menubar_visible - command_prompt - keybar_visible - message_visible;
763 check_split ();
764 start_y = menubar_visible;
766 /* The column computing is defered until panel_do_cols */
767 if (horizontal_split)
769 widget_set_size (panels[0].widget, start_y, 0, first_panel_size, 0);
771 widget_set_size (panels[1].widget, start_y + first_panel_size, 0,
772 height - first_panel_size, 0);
774 else
776 int first_x = first_panel_size;
778 widget_set_size (panels[0].widget, start_y, 0, height, 0);
780 widget_set_size (panels[1].widget, start_y, first_x, height, 0);
783 panel_do_cols (0);
784 panel_do_cols (1);
786 promptl = str_term_width1 (mc_prompt);
788 widget_set_size (&the_menubar->widget, 0, 0, 1, COLS);
790 if (command_prompt)
792 widget_set_size (&cmdline->widget, LINES - 1 - keybar_visible, promptl, 1, COLS - promptl);
793 input_set_origin (cmdline, promptl, COLS - promptl);
794 widget_set_size (&the_prompt->widget, LINES - 1 - keybar_visible, 0, 1, promptl);
796 else
798 widget_set_size (&cmdline->widget, 0, 0, 0, 0);
799 input_set_origin (cmdline, 0, 0);
800 widget_set_size (&the_prompt->widget, LINES, COLS, 0, 0);
803 widget_set_size (&the_bar->widget, LINES - 1, 0, keybar_visible, COLS);
804 buttonbar_set_visible (the_bar, keybar_visible);
806 /* Output window */
807 if (console_flag && output_lines)
809 output_start_y = LINES - command_prompt - keybar_visible - output_lines;
810 show_console_contents (output_start_y,
811 LINES - output_lines - keybar_visible - 1,
812 LINES - keybar_visible - 1);
814 if (message_visible)
815 widget_set_size (&the_hint->widget, height + start_y, 0, 1, COLS);
816 else
817 widget_set_size (&the_hint->widget, 0, 0, 0, 0);
819 update_xterm_title_path ();
822 /* --------------------------------------------------------------------------------------------- */
824 void
825 sigwinch_handler (int dummy)
827 (void) dummy;
828 #if !(defined(USE_NCURSES) || defined(USE_NCURSESW)) /* don't do malloc in a signal handler */
829 low_level_change_screen_size ();
830 #endif
831 winch_flag = 1;
834 /* --------------------------------------------------------------------------------------------- */
836 void
837 change_screen_size (void)
839 winch_flag = 0;
840 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
841 #if defined TIOCGWINSZ
843 #ifndef NCURSES_VERSION
844 tty_noraw_mode ();
845 tty_reset_screen ();
846 #endif
847 low_level_change_screen_size ();
848 #ifdef HAVE_SLANG
849 /* XSI Curses spec states that portable applications shall not invoke
850 * initscr() more than once. This kludge could be done within the scope
851 * of the specification by using endwin followed by a refresh (in fact,
852 * more than one curses implementation does this); it is guaranteed to work
853 * only with slang.
855 SLsmg_init_smg ();
856 do_enter_ca_mode ();
857 tty_keypad (TRUE);
858 tty_nodelay (FALSE);
859 #endif
861 /* Inform all suspending dialogs */
862 dialog_switch_got_winch ();
863 /* Inform all running dialogs */
864 g_list_foreach (top_dlg, (GFunc) dlg_resize_cb, NULL);
866 /* Now, force the redraw */
867 repaint_screen ();
868 #endif /* TIOCGWINSZ */
869 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
873 /* --------------------------------------------------------------------------------------------- */
875 void
876 use_dash (gboolean flag)
878 if (flag)
879 ok_to_refresh++;
880 else
881 ok_to_refresh--;
884 /* --------------------------------------------------------------------------------------------- */
886 void
887 set_hintbar (const char *str)
889 label_set_text (the_hint, str);
890 if (ok_to_refresh > 0)
891 mc_refresh ();
894 /* --------------------------------------------------------------------------------------------- */
896 void
897 print_vfs_message (const char *msg, ...)
899 va_list ap;
900 char str[128];
902 va_start (ap, msg);
903 g_vsnprintf (str, sizeof (str), msg, ap);
904 va_end (ap);
906 if (midnight_shutdown)
907 return;
909 if (!message_visible || !the_hint || !the_hint->widget.owner)
911 int col, row;
913 if (!nice_rotating_dash || (ok_to_refresh <= 0))
914 return;
916 /* Preserve current cursor position */
917 tty_getyx (&row, &col);
919 tty_gotoyx (0, 0);
920 tty_setcolor (NORMAL_COLOR);
921 tty_print_string (str_fit_to_term (str, COLS - 1, J_LEFT));
923 /* Restore cursor position */
924 tty_gotoyx (row, col);
925 mc_refresh ();
926 return;
929 if (message_visible)
930 set_hintbar (str);
933 /* --------------------------------------------------------------------------------------------- */
935 void
936 rotate_dash (void)
938 static const char rotating_dash[] = "|/-\\";
939 static size_t pos = 0;
941 if (!nice_rotating_dash || (ok_to_refresh <= 0))
942 return;
944 if (pos >= sizeof (rotating_dash) - 1)
945 pos = 0;
946 tty_gotoyx (0, COLS - 1);
947 tty_setcolor (NORMAL_COLOR);
948 tty_print_char (rotating_dash[pos]);
949 mc_refresh ();
950 pos++;
953 /* --------------------------------------------------------------------------------------------- */
955 const char *
956 get_nth_panel_name (int num)
958 static char buffer[BUF_SMALL];
960 if (!num)
961 return "New Left Panel";
962 else if (num == 1)
963 return "New Right Panel";
964 else
966 g_snprintf (buffer, sizeof (buffer), "%ith Panel", num);
967 return buffer;
971 /* --------------------------------------------------------------------------------------------- */
972 /* I wonder if I should start to use the folding mode than Dugan uses */
973 /* */
974 /* This is the centralized managing of the panel display types */
975 /* This routine takes care of destroying and creating new widgets */
976 /* Please note that it could manage MAX_VIEWS, not just left and right */
977 /* Currently nothing in the code takes advantage of this and has hard- */
978 /* coded values for two panels only */
980 /* Set the num-th panel to the view type: type */
981 /* This routine also keeps at least one WPanel object in the screen */
982 /* since a lot of routines depend on the current_panel variable */
984 void
985 set_display_type (int num, panel_view_mode_t type)
987 int x = 0, y = 0, cols = 0, lines = 0;
988 unsigned int the_other = 0; /* Index to the other panel */
989 const char *file_name = NULL; /* For Quick view */
990 Widget *new_widget = NULL, *old_widget = NULL;
991 WPanel *the_other_panel = NULL;
993 if (num >= MAX_VIEWS)
995 fprintf (stderr, "Cannot allocate more that %d views\n", MAX_VIEWS);
996 abort ();
998 /* Check that we will have a WPanel * at least */
999 if (type != view_listing)
1001 the_other = num == 0 ? 1 : 0;
1003 if (panels[the_other].type != view_listing)
1004 return;
1007 /* Get rid of it */
1008 if (panels[num].widget != NULL)
1010 Widget *w = panels[num].widget;
1011 WPanel *panel = (WPanel *) w;
1013 x = w->x;
1014 y = w->y;
1015 cols = w->cols;
1016 lines = w->lines;
1017 old_widget = w;
1019 if (panels[num].type == view_listing)
1021 if (panel->frame_size == frame_full && type != view_listing)
1023 cols = COLS - first_panel_size;
1024 if (num == 1)
1025 x = first_panel_size;
1030 /* Restoring saved path from panels.ini for nonlist panel */
1031 /* when it's first creation (for example view_info) */
1032 if (old_widget == NULL && type != view_listing)
1034 char panel_dir[MC_MAXPATHLEN];
1035 mc_get_current_wd (panel_dir, sizeof (panel_dir));
1036 panels[num].last_saved_dir = g_strdup (panel_dir);
1039 switch (type)
1041 case view_nothing:
1042 case view_listing:
1043 new_widget = restore_into_right_dir_panel (num, old_widget);
1044 widget_set_size (new_widget, y, x, lines, cols);
1045 break;
1047 case view_info:
1048 new_widget = (Widget *) info_new (y, x, lines, cols);
1049 break;
1051 case view_tree:
1052 new_widget = (Widget *) tree_new (y, x, lines, cols, TRUE);
1053 break;
1055 case view_quick:
1056 new_widget = (Widget *) mcview_new (y, x, lines, cols, TRUE);
1057 the_other_panel = (WPanel *) panels[the_other].widget;
1058 if (the_other_panel != NULL)
1059 file_name = the_other_panel->dir.list[the_other_panel->selected].fname;
1060 else
1061 file_name = "";
1063 mcview_load ((struct mcview_struct *) new_widget, 0, file_name, 0);
1064 break;
1067 if (type != view_listing)
1068 /* Must save dir, for restoring after change type to */
1069 /* view_listing */
1070 save_panel_dir (num);
1072 panels[num].type = type;
1073 panels[num].widget = new_widget;
1075 /* We use replace to keep the circular list of the dialog in the */
1076 /* same state. Maybe we could just kill it and then replace it */
1077 if ((midnight_dlg != NULL) && (old_widget != NULL))
1078 dlg_replace_widget (old_widget, new_widget);
1080 if (type == view_listing)
1082 WPanel *panel = (WPanel *) new_widget;
1084 if (num == 0)
1085 left_panel = panel;
1086 else
1087 right_panel = panel;
1089 /* forced update format after set new sizes */
1090 set_panel_formats (panel);
1093 if (type == view_tree)
1094 the_tree = (WTree *) new_widget;
1096 /* Prevent current_panel's value from becoming invalid.
1097 * It's just a quick hack to prevent segfaults. Comment out and
1098 * try following:
1099 * - select left panel
1100 * - invoke menue left/tree
1101 * - as long as you stay in the left panel almost everything that uses
1102 * current_panel causes segfault, e.g. C-Enter, C-x c, ...
1104 if ((type != view_listing) && (current_panel == (WPanel *) old_widget))
1105 current_panel = num == 0 ? right_panel : left_panel;
1107 g_free (old_widget);
1109 /* --------------------------------------------------------------------------------------------- */
1111 void
1112 panel_update_cols (Widget * widget, panel_display_t frame_size)
1114 int cols, origin;
1116 /* don't touch panel if it is not in dialog yet */
1117 /* if panel is not in dialog it is not in widgets list
1118 and cannot be compared with get_panel_widget() result */
1119 if (widget->owner == NULL)
1120 return;
1122 if (horizontal_split)
1124 widget->cols = COLS;
1125 return;
1128 if (frame_size == frame_full)
1130 cols = COLS;
1131 origin = 0;
1133 else if (widget == get_panel_widget (0))
1135 cols = first_panel_size;
1136 origin = 0;
1138 else
1140 cols = COLS - first_panel_size;
1141 origin = first_panel_size;
1144 widget->cols = cols;
1145 widget->x = origin;
1148 /* --------------------------------------------------------------------------------------------- */
1149 /** This routine is deeply sticked to the two panels idea.
1150 What should it do in more panels. ANSWER - don't use it
1151 in any multiple panels environment. */
1153 void
1154 swap_panels (void)
1156 WPanel *panel1, *panel2;
1157 Widget *tmp_widget;
1159 panel1 = (WPanel *) panels[0].widget;
1160 panel2 = (WPanel *) panels[1].widget;
1162 if (panels[0].type == view_listing && panels[1].type == view_listing &&
1163 !mc_config_get_bool (mc_main_config, CONFIG_PANELS_SECTION, "simple_swap", FALSE))
1165 WPanel panel;
1167 #define panelswap(x) panel.x = panel1->x; panel1->x = panel2->x; panel2->x = panel.x;
1169 #define panelswapstr(e) strcpy (panel.e, panel1->e); \
1170 strcpy (panel1->e, panel2->e); \
1171 strcpy (panel2->e, panel.e);
1172 /* Change content and related stuff */
1173 panelswap (dir);
1174 panelswap (active);
1175 panelswapstr (cwd);
1176 panelswapstr (lwd);
1177 panelswap (count);
1178 panelswap (marked);
1179 panelswap (dirs_marked);
1180 panelswap (total);
1181 panelswap (top_file);
1182 panelswap (selected);
1183 panelswap (is_panelized);
1184 panelswap (dir_stat);
1185 #undef panelswapstr
1186 #undef panelswap
1188 panel1->searching = FALSE;
1189 panel2->searching = FALSE;
1191 if (current_panel == panel1)
1192 current_panel = panel2;
1193 else
1194 current_panel = panel1;
1196 /* if sort options are different -> resort panels */
1197 if (memcmp (&panel1->sort_info, &panel2->sort_info, sizeof (panel_sort_info_t)) != 0)
1199 panel_re_sort (other_panel);
1200 panel_re_sort (current_panel);
1203 if (dlg_widget_active (panels[0].widget))
1204 dlg_select_widget (panels[1].widget);
1205 else if (dlg_widget_active (panels[1].widget))
1206 dlg_select_widget (panels[0].widget);
1208 else
1210 WPanel *tmp_panel;
1211 int x, y, cols, lines;
1212 int tmp_type;
1214 tmp_panel = right_panel;
1215 right_panel = left_panel;
1216 left_panel = tmp_panel;
1218 if (panels[0].type == view_listing)
1220 if (strcmp (panel1->panel_name, get_nth_panel_name (0)) == 0)
1222 g_free (panel1->panel_name);
1223 panel1->panel_name = g_strdup (get_nth_panel_name (1));
1226 if (panels[1].type == view_listing)
1228 if (strcmp (panel2->panel_name, get_nth_panel_name (1)) == 0)
1230 g_free (panel2->panel_name);
1231 panel2->panel_name = g_strdup (get_nth_panel_name (0));
1235 x = panels[0].widget->x;
1236 y = panels[0].widget->y;
1237 cols = panels[0].widget->cols;
1238 lines = panels[0].widget->lines;
1240 panels[0].widget->x = panels[1].widget->x;
1241 panels[0].widget->y = panels[1].widget->y;
1242 panels[0].widget->cols = panels[1].widget->cols;
1243 panels[0].widget->lines = panels[1].widget->lines;
1245 panels[1].widget->x = x;
1246 panels[1].widget->y = y;
1247 panels[1].widget->cols = cols;
1248 panels[1].widget->lines = lines;
1250 tmp_widget = panels[0].widget;
1251 panels[0].widget = panels[1].widget;
1252 panels[1].widget = tmp_widget;
1253 tmp_type = panels[0].type;
1254 panels[0].type = panels[1].type;
1255 panels[1].type = tmp_type;
1257 /* force update formats because of possible changed sizes */
1258 if (panels[0].type == view_listing)
1259 set_panel_formats ((WPanel *) panels[0].widget);
1260 if (panels[1].type == view_listing)
1261 set_panel_formats ((WPanel *) panels[1].widget);
1265 /* --------------------------------------------------------------------------------------------- */
1267 panel_view_mode_t
1268 get_display_type (int idx)
1270 return panels[idx].type;
1273 /* --------------------------------------------------------------------------------------------- */
1275 struct Widget *
1276 get_panel_widget (int idx)
1278 return panels[idx].widget;
1281 /* --------------------------------------------------------------------------------------------- */
1284 get_current_index (void)
1286 if (panels[0].widget == ((Widget *) current_panel))
1287 return 0;
1288 else
1289 return 1;
1292 /* --------------------------------------------------------------------------------------------- */
1295 get_other_index (void)
1297 return !get_current_index ();
1300 /* --------------------------------------------------------------------------------------------- */
1302 struct WPanel *
1303 get_other_panel (void)
1305 return (struct WPanel *) get_panel_widget (get_other_index ());
1308 /* --------------------------------------------------------------------------------------------- */
1309 /** Returns the view type for the current panel/view */
1311 panel_view_mode_t
1312 get_current_type (void)
1314 if (panels[0].widget == (Widget *) current_panel)
1315 return panels[0].type;
1316 else
1317 return panels[1].type;
1320 /* --------------------------------------------------------------------------------------------- */
1321 /** Returns the view type of the unselected panel */
1323 panel_view_mode_t
1324 get_other_type (void)
1326 if (panels[0].widget == (Widget *) current_panel)
1327 return panels[1].type;
1328 else
1329 return panels[0].type;
1332 /* --------------------------------------------------------------------------------------------- */
1333 /** Save current list_view widget directory into panel */
1335 void
1336 save_panel_dir (int idx)
1338 panel_view_mode_t type = get_display_type (idx);
1339 Widget *widget = get_panel_widget (idx);
1341 if ((type == view_listing) && (widget != NULL))
1343 WPanel *w = (WPanel *) widget;
1344 char *widget_work_dir = w->cwd;
1346 g_free (panels[idx].last_saved_dir); /* last path no needed */
1347 /* Because path can be nonlocal */
1348 panels[idx].last_saved_dir = vfs_translate_url (widget_work_dir);
1352 /* --------------------------------------------------------------------------------------------- */
1353 /** Return working dir, if it's view_listing - cwd,
1354 but for other types - last_saved_dir */
1356 const char *
1357 get_panel_dir_for (const WPanel * widget)
1359 int i;
1361 for (i = 0; i < MAX_VIEWS; i++)
1362 if ((WPanel *) get_panel_widget (i) == widget)
1363 break;
1365 if (i >= MAX_VIEWS)
1366 return ".";
1368 if (get_display_type (i) == view_listing)
1369 return ((WPanel *) get_panel_widget (i))->cwd;
1371 return panels[i].last_saved_dir;
1374 /* --------------------------------------------------------------------------------------------- */