Moved charsets.[ch] from src to lib directory
[midnight-commander.git] / src / layout.c
blob0819b18aa8954a6c05e8d2a70935da0bb2237b56
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 "command.h"
59 #include "main-widgets.h"
60 #include "main.h"
61 #include "subshell.h" /* For use_subshell and resize_subshell() */
62 #include "tree.h"
63 #include "background.h" /* we_are_background */
64 /* Needed for the extern declarations of integer parameters */
65 #include "dir.h"
66 #include "panel.h" /* The Panel widget */
67 #include "consaver/cons.saver.h"
68 #include "layout.h"
69 #include "info.h" /* The Info widget */
70 #include "src/viewer/mcviewer.h" /* The view widget */
72 #include "setup.h" /* For save_setup() */
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)
132 #define LAYOUT_OPTIONS_COUNT (sizeof (check_options) / sizeof (check_options[0]))
133 #define OTHER_OPTIONS_COUNT (LAYOUT_OPTIONS_COUNT - 1)
135 /*** file scope type declarations ****************************************************************/
137 /*** file scope variables ************************************************************************/
139 static struct
141 panel_view_mode_t type;
142 Widget *widget;
143 char *last_saved_dir; /* last view_list working directory */
144 } panels[MAX_VIEWS] =
146 /* *INDENT-OFF* */
147 /* init MAX_VIEWS items */
148 { view_listing, NULL, NULL},
149 { view_listing, NULL, NULL}
150 /* *INDENT-ON* */
153 /* These variables are used to avoid updating the information unless */
154 /* we need it */
155 static int old_first_panel_size;
156 static int old_horizontal_split;
157 static int old_output_lines;
159 /* Internal variables */
160 static int _horizontal_split;
161 static int _equal_split;
162 static int _first_panel_size;
163 static int _menubar_visible;
164 static int _output_lines;
165 static int _command_prompt;
166 static int _keybar_visible;
167 static int _message_visible;
168 static int _xterm_title;
169 static int _free_space;
171 static int height;
173 static const char *s_split_direction[2] = {
174 N_("&Vertical"),
175 N_("&Horizontal")
178 static WRadio *radio_widget;
180 static struct
182 const char *text;
183 int *variable;
184 WCheck *widget;
185 } check_options[] =
187 /* *INDENT-OFF* */
188 { N_("Show free sp&ace"), &free_space, NULL},
189 { N_("&XTerm window title"), &xterm_title, NULL},
190 { N_("H&intbar visible"), &message_visible, NULL},
191 { N_("&Keybar visible"), &keybar_visible, NULL},
192 { N_("Command &prompt"), &command_prompt, NULL},
193 { N_("Show &mini status"), &show_mini_info, NULL},
194 { N_("Menu&bar visible"), &menubar_visible, NULL},
195 { N_("&Equal split"), &equal_split, NULL}
196 /* *INDENT-ON* */
199 static gsize first_width;
200 static const char *output_lines_label = NULL;
201 static int output_lines_label_len;
203 static WButton *bleft_widget, *bright_widget;
205 static int ok_to_refresh = 1;
207 /*** file scope functions ************************************************************************/
208 /* --------------------------------------------------------------------------------------------- */
210 static inline void
211 _check_split (void)
213 if (_horizontal_split)
215 if (_equal_split)
216 _first_panel_size = height / 2;
217 else if (_first_panel_size < MINHEIGHT)
218 _first_panel_size = MINHEIGHT;
219 else if (_first_panel_size > height - MINHEIGHT)
220 _first_panel_size = height - MINHEIGHT;
222 else
224 if (_equal_split)
225 _first_panel_size = COLS / 2;
226 else if (_first_panel_size < MINWIDTH)
227 _first_panel_size = MINWIDTH;
228 else if (_first_panel_size > COLS - MINWIDTH)
229 _first_panel_size = COLS - MINWIDTH;
232 old_first_panel_size = _first_panel_size;
233 old_horizontal_split = _horizontal_split;
236 /* --------------------------------------------------------------------------------------------- */
238 static void
239 update_split (const Dlg_head * h)
241 /* Check split has to be done before testing if it changed, since
242 it can change due to calling _check_split() as well */
243 _check_split ();
245 tty_setcolor (check_options[7].widget->state & C_BOOL ? DISABLED_COLOR : COLOR_NORMAL);
247 dlg_move (h, 6, 6);
248 tty_printf ("%03d", _first_panel_size);
250 dlg_move (h, 6, 18);
251 if (_horizontal_split)
252 tty_printf ("%03d", height - _first_panel_size);
253 else
254 tty_printf ("%03d", COLS - _first_panel_size);
256 dlg_move (h, 6, 13);
257 tty_print_char ('=');
260 /* --------------------------------------------------------------------------------------------- */
262 static int
263 b_left_right_cback (WButton * button, int action)
265 (void) action;
267 if (button == bleft_widget)
268 _first_panel_size++;
269 else
270 _first_panel_size--;
272 update_split (button->widget.owner);
273 return 0;
276 /* --------------------------------------------------------------------------------------------- */
278 static int
279 bplus_cback (WButton * button, int action)
281 (void) button;
282 (void) action;
284 if (_output_lines < 99)
285 _output_lines++;
286 return 0;
289 /* --------------------------------------------------------------------------------------------- */
291 static int
292 bminus_cback (WButton * button, int action)
294 (void) button;
295 (void) action;
297 if (_output_lines > 0)
298 _output_lines--;
299 return 0;
302 /* --------------------------------------------------------------------------------------------- */
304 static cb_ret_t
305 layout_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
307 switch (msg)
309 case DLG_DRAW:
310 /*When repainting the whole dialog (e.g. with C-l) we have to
311 update everything */
312 common_dialog_repaint (h);
314 old_first_panel_size = -1;
315 old_horizontal_split = -1;
316 old_output_lines = -1;
318 update_split (h);
320 if (old_output_lines != _output_lines)
322 old_output_lines = _output_lines;
323 tty_setcolor (console_flag ? COLOR_NORMAL : DISABLED_COLOR);
324 dlg_move (h, 9, 6);
325 tty_print_string (output_lines_label);
326 dlg_move (h, 9, 6 + 3 + output_lines_label_len);
327 tty_printf ("%02d", _output_lines);
329 return MSG_HANDLED;
331 case DLG_POST_KEY:
332 _menubar_visible = check_options[6].widget->state & C_BOOL;
333 _command_prompt = check_options[5].widget->state & C_BOOL;
334 _keybar_visible = check_options[3].widget->state & C_BOOL;
335 _message_visible = check_options[2].widget->state & C_BOOL;
336 _xterm_title = check_options[1].widget->state & C_BOOL;
337 _free_space = check_options[0].widget->state & C_BOOL;
339 if (console_flag)
341 int minimum;
342 if (_output_lines < 0)
343 _output_lines = 0;
344 height = LINES - _keybar_visible - _command_prompt -
345 _menubar_visible - _output_lines - _message_visible;
346 minimum = MINHEIGHT * (1 + _horizontal_split);
347 if (height < minimum)
349 _output_lines -= minimum - height;
350 height = minimum;
353 else
354 height = LINES - _keybar_visible - _command_prompt -
355 _menubar_visible - _output_lines - _message_visible;
357 if (old_output_lines != _output_lines)
359 old_output_lines = _output_lines;
360 tty_setcolor (console_flag ? COLOR_NORMAL : DISABLED_COLOR);
361 dlg_move (h, 9, 6 + 3 + output_lines_label_len);
362 tty_printf ("%02d", _output_lines);
364 return MSG_HANDLED;
366 case DLG_ACTION:
367 if (sender == (Widget *) radio_widget)
369 if (_horizontal_split != radio_widget->sel)
371 _horizontal_split = radio_widget->sel;
372 if (_equal_split)
374 if (_horizontal_split)
375 _first_panel_size = height / 2;
376 else
377 _first_panel_size = COLS / 2;
381 update_split (h);
383 return MSG_HANDLED;
386 if (sender == (Widget *) check_options[7].widget)
388 _equal_split = check_options[7].widget->state & C_BOOL;
390 widget_disable (bleft_widget->widget, _equal_split);
391 send_message ((Widget *) bleft_widget, WIDGET_DRAW, 0);
392 widget_disable (bright_widget->widget, _equal_split);
393 send_message ((Widget *) bright_widget, WIDGET_DRAW, 0);
395 update_split (h);
397 return MSG_HANDLED;
400 return MSG_NOT_HANDLED;
402 default:
403 return default_dlg_callback (h, sender, msg, parm, data);
407 /* --------------------------------------------------------------------------------------------- */
409 static Dlg_head *
410 init_layout (void)
412 static gboolean i18n_layt_flag = FALSE;
413 static int b1, b2, b3;
414 size_t i = sizeof (s_split_direction) / sizeof (char *);
415 const char *ok_button = _("&OK");
416 const char *cancel_button = _("&Cancel");
417 const char *save_button = _("&Save");
418 static const char *title1, *title2, *title3;
420 Dlg_head *layout_dlg;
422 if (!i18n_layt_flag)
424 gsize l1;
426 first_width = 0;
428 title1 = _("Panel split");
429 title2 = _("Console output");
430 title3 = _("Other options");
431 output_lines_label = _("Output lines:");
433 while (i--)
435 s_split_direction[i] = _(s_split_direction[i]);
436 l1 = str_term_width1 (s_split_direction[i]) + 7;
437 if (l1 > first_width)
438 first_width = l1;
441 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
443 check_options[i].text = _(check_options[i].text);
444 l1 = str_term_width1 (check_options[i].text) + 7;
445 if (l1 > first_width)
446 first_width = l1;
449 l1 = str_term_width1 (title1) + 1;
450 if (l1 > first_width)
451 first_width = l1;
453 l1 = str_term_width1 (title2) + 1;
454 if (l1 > first_width)
455 first_width = l1;
457 output_lines_label_len = str_term_width1 (output_lines_label);
458 l1 = output_lines_label_len + 12;
459 if (l1 > first_width)
460 first_width = l1;
463 * alex@bcs.zp.ua:
464 * To be completely correct, one need to check if the title
465 * does not exceed dialog length and total length of 3 buttons
466 * allows their placement in one row. But assuming this dialog
467 * is wide enough, I don't include such a tests.
469 * Now the last thing to do - properly space buttons...
471 l1 = 11 + str_term_width1 (ok_button) /* 14 - all brackets and inner space */
472 + str_term_width1 (save_button) /* notice: it is 3 char less because */
473 + str_term_width1 (cancel_button); /* of '&' char in button text */
475 i = (first_width * 2 - l1) / 4;
476 b1 = 5 + i;
477 b2 = b1 + str_term_width1 (ok_button) + i + 6;
478 b3 = b2 + str_term_width1 (save_button) + i + 4;
480 i18n_layt_flag = TRUE;
483 layout_dlg =
484 create_dlg (TRUE, 0, 0, 14, first_width * 2 + 9,
485 dialog_colors, layout_callback, "[Layout]",
486 _("Layout"), DLG_CENTER | DLG_REVERSE);
488 add_widget (layout_dlg, groupbox_new (2, 4, 6, first_width, title1));
490 add_widget (layout_dlg, groupbox_new (2, 5 + first_width, 9, first_width, title3));
492 add_widget (layout_dlg, button_new (11, b3, B_CANCEL, NORMAL_BUTTON, cancel_button, 0));
493 add_widget (layout_dlg, button_new (11, b2, B_EXIT, NORMAL_BUTTON, save_button, 0));
494 add_widget (layout_dlg, button_new (11, b1, B_ENTER, DEFPUSH_BUTTON, ok_button, 0));
495 #define XTRACT(i) *check_options[i].variable, check_options[i].text
497 for (i = 0; i < (size_t) OTHER_OPTIONS_COUNT; i++)
499 check_options[i].widget =
500 check_new (OTHER_OPTIONS_COUNT - i + 2, 7 + first_width, XTRACT (i));
501 add_widget (layout_dlg, check_options[i].widget);
504 _equal_split = equal_split;
505 _menubar_visible = menubar_visible;
506 _command_prompt = command_prompt;
507 _keybar_visible = keybar_visible;
508 _message_visible = message_visible;
509 _xterm_title = xterm_title;
510 _free_space = free_space;
513 const int disabled = console_flag ? 0 : W_DISABLED;
514 Widget *w;
516 w = (Widget *) groupbox_new (8, 4, 3, first_width, title2);
517 w->options |= disabled;
518 add_widget (layout_dlg, w);
520 w = (Widget *) button_new (9, output_lines_label_len + 6 + 5, B_MINUS,
521 NARROW_BUTTON, "&-", bminus_cback);
522 w->options |= disabled;
523 add_widget (layout_dlg, w);
525 w = (Widget *) button_new (9, output_lines_label_len + 6, B_PLUS,
526 NARROW_BUTTON, "&+", bplus_cback);
527 w->options |= disabled;
528 add_widget (layout_dlg, w);
531 bright_widget = button_new (6, 15, B_2RIGHT, NARROW_BUTTON, "&>", b_left_right_cback);
532 widget_disable (bright_widget->widget, _equal_split);
533 add_widget (layout_dlg, bright_widget);
534 bleft_widget = button_new (6, 9, B_2LEFT, NARROW_BUTTON, "&<", b_left_right_cback);
535 widget_disable (bleft_widget->widget, _equal_split);
536 add_widget (layout_dlg, bleft_widget);
537 check_options[7].widget = check_new (5, 6, XTRACT (7));
539 old_first_panel_size = -1;
540 old_horizontal_split = -1;
541 old_output_lines = -1;
543 _first_panel_size = first_panel_size;
544 _output_lines = output_lines;
546 add_widget (layout_dlg, check_options[7].widget);
547 radio_widget = radio_new (3, 6, 2, s_split_direction);
548 add_widget (layout_dlg, radio_widget);
549 radio_widget->sel = horizontal_split;
551 return layout_dlg;
554 /* --------------------------------------------------------------------------------------------- */
556 static void
557 check_split (void)
559 if (horizontal_split)
561 if (equal_split)
562 first_panel_size = height / 2;
563 else if (first_panel_size < MINHEIGHT)
564 first_panel_size = MINHEIGHT;
565 else if (first_panel_size > height - MINHEIGHT)
566 first_panel_size = height - MINHEIGHT;
568 else
570 if (equal_split)
571 first_panel_size = COLS / 2;
572 else if (first_panel_size < MINWIDTH)
573 first_panel_size = MINWIDTH;
574 else if (first_panel_size > COLS - MINWIDTH)
575 first_panel_size = COLS - MINWIDTH;
579 /* --------------------------------------------------------------------------------------------- */
581 static void
582 panel_do_cols (int idx)
584 if (get_display_type (idx) == view_listing)
585 set_panel_formats ((WPanel *) panels[idx].widget);
586 else
587 panel_update_cols (panels[idx].widget, frame_half);
590 /* --------------------------------------------------------------------------------------------- */
592 static inline void
593 low_level_change_screen_size (void)
595 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
596 #if defined TIOCGWINSZ
597 struct winsize winsz;
599 winsz.ws_col = winsz.ws_row = 0;
600 /* Ioctl on the STDIN_FILENO */
601 ioctl (0, TIOCGWINSZ, &winsz);
602 if (winsz.ws_col && winsz.ws_row)
604 #if defined(NCURSES_VERSION) && defined(HAVE_RESIZETERM)
605 resizeterm (winsz.ws_row, winsz.ws_col);
606 clearok (stdscr, TRUE); /* sigwinch's should use a semaphore! */
607 #else
608 COLS = winsz.ws_col;
609 LINES = winsz.ws_row;
610 #endif
611 #ifdef HAVE_SUBSHELL_SUPPORT
612 resize_subshell ();
613 #endif
615 #endif /* TIOCGWINSZ */
616 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
619 /* --------------------------------------------------------------------------------------------- */
621 static void
622 dlg_resize_cb (void *data, void *user_data)
624 Dlg_head *d = data;
626 (void) user_data;
627 d->callback (d, NULL, DLG_RESIZE, 0, NULL);
630 /* --------------------------------------------------------------------------------------------- */
631 /*** public functions ****************************************************************************/
632 /* --------------------------------------------------------------------------------------------- */
634 void
635 layout_change (void)
637 setup_panels ();
638 /* re-init the menu, because perhaps there was a change in the way
639 how the panel are split (horizontal/vertical). */
640 update_menu ();
641 load_hint (1);
644 /* --------------------------------------------------------------------------------------------- */
646 void
647 layout_box (void)
649 Dlg_head *layout_dlg;
650 int result;
651 gboolean layout_do_change = FALSE;
653 layout_dlg = init_layout ();
654 result = run_dlg (layout_dlg);
656 if (result == B_ENTER || result == B_EXIT)
658 size_t i;
659 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
660 if (check_options[i].widget != NULL)
661 *check_options[i].variable = check_options[i].widget->state & C_BOOL;
662 horizontal_split = radio_widget->sel;
663 first_panel_size = _first_panel_size;
664 output_lines = _output_lines;
665 layout_do_change = TRUE;
667 if (result == B_EXIT)
669 save_layout ();
670 mc_config_save_file (mc_main_config, NULL);
673 destroy_dlg (layout_dlg);
674 if (layout_do_change)
675 layout_change ();
678 /* --------------------------------------------------------------------------------------------- */
680 void
681 clr_scr (void)
683 tty_set_normal_attrs ();
684 tty_fill_region (0, 0, LINES, COLS, ' ');
685 tty_refresh ();
688 /* --------------------------------------------------------------------------------------------- */
690 void
691 repaint_screen (void)
693 do_refresh ();
694 tty_refresh ();
697 /* --------------------------------------------------------------------------------------------- */
699 void
700 mc_refresh (void)
702 #ifdef WITH_BACKGROUND
703 if (we_are_background)
704 return;
705 #endif /* WITH_BACKGROUND */
706 if (winch_flag == 0)
707 tty_refresh ();
708 else
710 /* if winch was caugth, we should do not only redraw screen, but
711 reposition/resize all */
712 change_screen_size ();
716 /* --------------------------------------------------------------------------------------------- */
718 void
719 setup_panels (void)
721 int start_y;
722 int promptl; /* the prompt len */
724 if (console_flag)
726 int minimum;
727 if (output_lines < 0)
728 output_lines = 0;
729 height =
730 LINES - keybar_visible - command_prompt - menubar_visible -
731 output_lines - message_visible;
732 minimum = MINHEIGHT * (1 + horizontal_split);
733 if (height < minimum)
735 output_lines -= minimum - height;
736 height = minimum;
739 else
741 height = LINES - menubar_visible - command_prompt - keybar_visible - message_visible;
743 check_split ();
744 start_y = menubar_visible;
746 /* The column computing is defered until panel_do_cols */
747 if (horizontal_split)
749 widget_set_size (panels[0].widget, start_y, 0, first_panel_size, 0);
751 widget_set_size (panels[1].widget, start_y + first_panel_size, 0,
752 height - first_panel_size, 0);
754 else
756 int first_x = first_panel_size;
758 widget_set_size (panels[0].widget, start_y, 0, height, 0);
760 widget_set_size (panels[1].widget, start_y, first_x, height, 0);
763 panel_do_cols (0);
764 panel_do_cols (1);
766 promptl = str_term_width1 (mc_prompt);
768 widget_set_size (&the_menubar->widget, 0, 0, 1, COLS);
770 if (command_prompt)
772 widget_set_size (&cmdline->widget, LINES - 1 - keybar_visible, promptl, 1, COLS - promptl);
773 input_set_origin (cmdline, promptl, COLS - promptl);
774 widget_set_size (&the_prompt->widget, LINES - 1 - keybar_visible, 0, 1, promptl);
776 else
778 widget_set_size (&cmdline->widget, 0, 0, 0, 0);
779 input_set_origin (cmdline, 0, 0);
780 widget_set_size (&the_prompt->widget, LINES, COLS, 0, 0);
783 widget_set_size (&the_bar->widget, LINES - 1, 0, keybar_visible, COLS);
784 buttonbar_set_visible (the_bar, keybar_visible);
786 /* Output window */
787 if (console_flag && output_lines)
789 output_start_y = LINES - command_prompt - keybar_visible - output_lines;
790 show_console_contents (output_start_y,
791 LINES - output_lines - keybar_visible - 1,
792 LINES - keybar_visible - 1);
794 if (message_visible)
795 widget_set_size (&the_hint->widget, height + start_y, 0, 1, COLS);
796 else
797 widget_set_size (&the_hint->widget, 0, 0, 0, 0);
799 update_xterm_title_path ();
802 /* --------------------------------------------------------------------------------------------- */
804 void
805 sigwinch_handler (int dummy)
807 (void) dummy;
808 #if !(defined(USE_NCURSES) || defined(USE_NCURSESW)) /* don't do malloc in a signal handler */
809 low_level_change_screen_size ();
810 #endif
811 winch_flag = 1;
814 /* --------------------------------------------------------------------------------------------- */
816 void
817 change_screen_size (void)
819 winch_flag = 0;
820 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
821 #if defined TIOCGWINSZ
823 #ifndef NCURSES_VERSION
824 tty_noraw_mode ();
825 tty_reset_screen ();
826 #endif
827 low_level_change_screen_size ();
828 #ifdef HAVE_SLANG
829 /* XSI Curses spec states that portable applications shall not invoke
830 * initscr() more than once. This kludge could be done within the scope
831 * of the specification by using endwin followed by a refresh (in fact,
832 * more than one curses implementation does this); it is guaranteed to work
833 * only with slang.
835 SLsmg_init_smg ();
836 do_enter_ca_mode ();
837 tty_keypad (TRUE);
838 tty_nodelay (FALSE);
839 #endif
841 /* Inform all suspending dialogs */
842 dialog_switch_got_winch ();
843 /* Inform all running dialogs */
844 g_list_foreach (top_dlg, (GFunc) dlg_resize_cb, NULL);
846 /* Now, force the redraw */
847 repaint_screen ();
848 #endif /* TIOCGWINSZ */
849 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
853 /* --------------------------------------------------------------------------------------------- */
855 void
856 use_dash (gboolean flag)
858 if (flag)
859 ok_to_refresh++;
860 else
861 ok_to_refresh--;
864 /* --------------------------------------------------------------------------------------------- */
866 void
867 set_hintbar (const char *str)
869 label_set_text (the_hint, str);
870 if (ok_to_refresh > 0)
871 mc_refresh ();
874 /* --------------------------------------------------------------------------------------------- */
876 void
877 print_vfs_message (const char *msg, ...)
879 va_list ap;
880 char str[128];
882 va_start (ap, msg);
883 g_vsnprintf (str, sizeof (str), msg, ap);
884 va_end (ap);
886 if (midnight_shutdown)
887 return;
889 if (!message_visible || !the_hint || !the_hint->widget.owner)
891 int col, row;
893 if (!nice_rotating_dash || (ok_to_refresh <= 0))
894 return;
896 /* Preserve current cursor position */
897 tty_getyx (&row, &col);
899 tty_gotoyx (0, 0);
900 tty_setcolor (NORMAL_COLOR);
901 tty_print_string (str_fit_to_term (str, COLS - 1, J_LEFT));
903 /* Restore cursor position */
904 tty_gotoyx (row, col);
905 mc_refresh ();
906 return;
909 if (message_visible)
910 set_hintbar (str);
913 /* --------------------------------------------------------------------------------------------- */
915 void
916 rotate_dash (void)
918 static const char rotating_dash[] = "|/-\\";
919 static size_t pos = 0;
921 if (!nice_rotating_dash || (ok_to_refresh <= 0))
922 return;
924 if (pos >= sizeof (rotating_dash) - 1)
925 pos = 0;
926 tty_gotoyx (0, COLS - 1);
927 tty_setcolor (NORMAL_COLOR);
928 tty_print_char (rotating_dash[pos]);
929 mc_refresh ();
930 pos++;
933 /* --------------------------------------------------------------------------------------------- */
935 const char *
936 get_nth_panel_name (int num)
938 static char buffer[BUF_SMALL];
940 if (!num)
941 return "New Left Panel";
942 else if (num == 1)
943 return "New Right Panel";
944 else
946 g_snprintf (buffer, sizeof (buffer), "%ith Panel", num);
947 return buffer;
951 /* --------------------------------------------------------------------------------------------- */
952 /* I wonder if I should start to use the folding mode than Dugan uses */
953 /* */
954 /* This is the centralized managing of the panel display types */
955 /* This routine takes care of destroying and creating new widgets */
956 /* Please note that it could manage MAX_VIEWS, not just left and right */
957 /* Currently nothing in the code takes advantage of this and has hard- */
958 /* coded values for two panels only */
960 /* Set the num-th panel to the view type: type */
961 /* This routine also keeps at least one WPanel object in the screen */
962 /* since a lot of routines depend on the current_panel variable */
964 void
965 set_display_type (int num, panel_view_mode_t type)
967 int x = 0, y = 0, cols = 0, lines = 0;
968 int the_other = 0; /* Index to the other panel */
969 const char *file_name = NULL; /* For Quick view */
970 Widget *new_widget = NULL, *old_widget = NULL;
971 WPanel *the_other_panel = NULL;
973 if (num >= MAX_VIEWS)
975 fprintf (stderr, "Cannot allocate more that %d views\n", MAX_VIEWS);
976 abort ();
978 /* Check that we will have a WPanel * at least */
979 if (type != view_listing)
981 the_other = num == 0 ? 1 : 0;
983 if (panels[the_other].type != view_listing)
984 return;
987 /* Get rid of it */
988 if (panels[num].widget)
990 Widget *w = panels[num].widget;
991 WPanel *panel = (WPanel *) panels[num].widget;
993 x = w->x;
994 y = w->y;
995 cols = w->cols;
996 lines = w->lines;
997 old_widget = panels[num].widget;
999 if (panels[num].type == view_listing)
1001 if (panel->frame_size == frame_full && type != view_listing)
1003 cols = COLS - first_panel_size;
1004 if (num == 1)
1005 x = first_panel_size;
1010 /* Restoring saved path from panels.ini for nonlist panel */
1011 /* when it's first creation (for example view_info) */
1012 if (old_widget == NULL && type != view_listing)
1014 char panel_dir[MC_MAXPATHLEN];
1015 mc_get_current_wd (panel_dir, sizeof (panel_dir));
1016 panels[num].last_saved_dir = g_strdup (panel_dir);
1019 switch (type)
1021 case view_nothing:
1022 case view_listing:
1023 new_widget = restore_into_right_dir_panel (num, old_widget);
1024 widget_set_size (new_widget, y, x, lines, cols);
1025 break;
1027 case view_info:
1028 new_widget = (Widget *) info_new (y, x, lines, cols);
1029 break;
1031 case view_tree:
1032 new_widget = (Widget *) tree_new (y, x, lines, cols, TRUE);
1033 break;
1035 case view_quick:
1036 new_widget = (Widget *) mcview_new (y, x, lines, cols, TRUE);
1037 the_other_panel = (WPanel *) panels[the_other].widget;
1038 if (the_other_panel != NULL)
1039 file_name = the_other_panel->dir.list[the_other_panel->selected].fname;
1040 else
1041 file_name = "";
1043 mcview_load ((struct mcview_struct *) new_widget, 0, file_name, 0);
1044 break;
1047 if (type != view_listing)
1048 /* Must save dir, for restoring after change type to */
1049 /* view_listing */
1050 save_panel_dir (num);
1052 panels[num].type = type;
1053 panels[num].widget = new_widget;
1055 /* We use replace to keep the circular list of the dialog in the */
1056 /* same state. Maybe we could just kill it and then replace it */
1057 if ((midnight_dlg != NULL) && (old_widget != NULL))
1058 dlg_replace_widget (old_widget, panels[num].widget);
1060 if (type == view_listing)
1062 if (num == 0)
1063 left_panel = (WPanel *) new_widget;
1064 else
1065 right_panel = (WPanel *) new_widget;
1068 if (type == view_tree)
1069 the_tree = (WTree *) new_widget;
1071 /* Prevent current_panel's value from becoming invalid.
1072 * It's just a quick hack to prevent segfaults. Comment out and
1073 * try following:
1074 * - select left panel
1075 * - invoke menue left/tree
1076 * - as long as you stay in the left panel almost everything that uses
1077 * current_panel causes segfault, e.g. C-Enter, C-x c, ...
1080 if ((type != view_listing) && (current_panel == (WPanel *) old_widget))
1081 current_panel = num == 0 ? right_panel : left_panel;
1083 g_free (old_widget);
1086 /* --------------------------------------------------------------------------------------------- */
1087 /** This routine is deeply sticked to the two panels idea.
1088 What should it do in more panels. ANSWER - don't use it
1089 in any multiple panels environment. */
1091 void
1092 swap_panels (void)
1094 Widget tmp;
1095 Widget *tmp_widget;
1096 WPanel panel;
1097 WPanel *panel1, *panel2;
1098 int tmp_type;
1100 #define panelswap(x) panel. x = panel1-> x; panel1-> x = panel2-> x; panel2-> x = panel. x;
1102 #define panelswapstr(e) strcpy (panel. e, panel1-> e); \
1103 strcpy (panel1-> e, panel2-> e); \
1104 strcpy (panel2-> e, panel. e);
1105 panel1 = (WPanel *) panels[0].widget;
1106 panel2 = (WPanel *) panels[1].widget;
1107 if (panels[0].type == view_listing && panels[1].type == view_listing)
1109 /* Change everything except format/sort/panel_name etc. */
1110 panelswap (dir);
1111 panelswap (active);
1112 panelswapstr (cwd);
1113 panelswapstr (lwd);
1114 panelswap (count);
1115 panelswap (marked);
1116 panelswap (dirs_marked);
1117 panelswap (total);
1118 panelswap (top_file);
1119 panelswap (selected);
1120 panelswap (is_panelized);
1121 panelswap (dir_stat);
1123 panel1->searching = FALSE;
1124 panel2->searching = FALSE;
1125 if (current_panel == panel1)
1126 current_panel = panel2;
1127 else
1128 current_panel = panel1;
1130 if (dlg_widget_active (panels[0].widget))
1131 dlg_select_widget (panels[1].widget);
1132 else if (dlg_widget_active (panels[1].widget))
1133 dlg_select_widget (panels[0].widget);
1135 else
1137 WPanel *tmp_panel;
1139 tmp_panel = right_panel;
1140 right_panel = left_panel;
1141 left_panel = tmp_panel;
1143 if (panels[0].type == view_listing)
1145 if (!strcmp (panel1->panel_name, get_nth_panel_name (0)))
1147 g_free (panel1->panel_name);
1148 panel1->panel_name = g_strdup (get_nth_panel_name (1));
1151 if (panels[1].type == view_listing)
1153 if (!strcmp (panel2->panel_name, get_nth_panel_name (1)))
1155 g_free (panel2->panel_name);
1156 panel2->panel_name = g_strdup (get_nth_panel_name (0));
1160 tmp.x = panels[0].widget->x;
1161 tmp.y = panels[0].widget->y;
1162 tmp.cols = panels[0].widget->cols;
1163 tmp.lines = panels[0].widget->lines;
1165 panels[0].widget->x = panels[1].widget->x;
1166 panels[0].widget->y = panels[1].widget->y;
1167 panels[0].widget->cols = panels[1].widget->cols;
1168 panels[0].widget->lines = panels[1].widget->lines;
1170 panels[1].widget->x = tmp.x;
1171 panels[1].widget->y = tmp.y;
1172 panels[1].widget->cols = tmp.cols;
1173 panels[1].widget->lines = tmp.lines;
1175 tmp_widget = panels[0].widget;
1176 panels[0].widget = panels[1].widget;
1177 panels[1].widget = tmp_widget;
1178 tmp_type = panels[0].type;
1179 panels[0].type = panels[1].type;
1180 panels[1].type = tmp_type;
1184 /* --------------------------------------------------------------------------------------------- */
1186 panel_view_mode_t
1187 get_display_type (int idx)
1189 return panels[idx].type;
1192 /* --------------------------------------------------------------------------------------------- */
1194 struct Widget *
1195 get_panel_widget (int idx)
1197 return panels[idx].widget;
1200 /* --------------------------------------------------------------------------------------------- */
1203 get_current_index (void)
1205 if (panels[0].widget == ((Widget *) current_panel))
1206 return 0;
1207 else
1208 return 1;
1211 /* --------------------------------------------------------------------------------------------- */
1214 get_other_index (void)
1216 return !get_current_index ();
1219 /* --------------------------------------------------------------------------------------------- */
1221 struct WPanel *
1222 get_other_panel (void)
1224 return (struct WPanel *) get_panel_widget (get_other_index ());
1227 /* --------------------------------------------------------------------------------------------- */
1228 /** Returns the view type for the current panel/view */
1230 panel_view_mode_t
1231 get_current_type (void)
1233 if (panels[0].widget == (Widget *) current_panel)
1234 return panels[0].type;
1235 else
1236 return panels[1].type;
1239 /* --------------------------------------------------------------------------------------------- */
1240 /** Returns the view type of the unselected panel */
1242 panel_view_mode_t
1243 get_other_type (void)
1245 if (panels[0].widget == (Widget *) current_panel)
1246 return panels[1].type;
1247 else
1248 return panels[0].type;
1251 /* --------------------------------------------------------------------------------------------- */
1252 /** Save current list_view widget directory into panel */
1254 void
1255 save_panel_dir (int idx)
1257 panel_view_mode_t type = get_display_type (idx);
1258 Widget *widget = get_panel_widget (idx);
1260 if ((type == view_listing) && (widget != NULL))
1262 WPanel *w = (WPanel *) widget;
1263 char *widget_work_dir = w->cwd;
1265 g_free (panels[idx].last_saved_dir); /* last path no needed */
1266 /* Because path can be nonlocal */
1267 panels[idx].last_saved_dir = vfs_translate_url (widget_work_dir);
1271 /* --------------------------------------------------------------------------------------------- */
1272 /** Save current list_view widget directory into panel */
1274 Widget *
1275 restore_into_right_dir_panel (int idx, Widget * from_widget)
1277 Widget *new_widget = NULL;
1278 const char *saved_dir = panels[idx].last_saved_dir;
1279 gboolean last_was_panel = (from_widget && get_display_type (idx) != view_listing);
1280 const char *p_name = get_nth_panel_name (idx);
1282 if (last_was_panel)
1283 new_widget = (Widget *) panel_new_with_dir (p_name, saved_dir);
1284 else
1285 new_widget = (Widget *) panel_new (p_name);
1287 return new_widget;
1290 /* --------------------------------------------------------------------------------------------- */
1291 /** Return working dir, if it's view_listing - cwd,
1292 but for other types - last_saved_dir */
1294 const char *
1295 get_panel_dir_for (const WPanel * widget)
1297 int i;
1299 for (i = 0; i < MAX_VIEWS; i++)
1300 if ((WPanel *) get_panel_widget (i) == widget)
1301 break;
1303 if (i >= MAX_VIEWS)
1304 return ".";
1306 if (get_display_type (i) == view_listing)
1307 return ((WPanel *) get_panel_widget (i))->cwd;
1309 return panels[i].last_saved_dir;
1312 /* --------------------------------------------------------------------------------------------- */