Ticket #1790: mc crashes on start
[midnight-commander.git] / src / layout.c
blob60b7c5fd8d98a6b9f60832349cb4f13b0a937d99
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 #ifdef HAVE_TERMIOS_H
45 #include <termios.h>
46 #endif
47 #include <unistd.h>
49 #include "global.h"
51 #include "../src/tty/tty.h"
52 #include "../src/skin/skin.h"
53 #include "../src/tty/key.h"
54 #include "../src/tty/mouse.h"
55 #include "../src/tty/win.h" /* do_enter_ca_mode() */
57 #include "../src/mcconfig/mcconfig.h"
59 #include "dialog.h"
60 #include "widget.h"
61 #include "command.h"
62 #include "main-widgets.h"
63 #include "main.h"
64 #include "subshell.h" /* For use_subshell and resize_subshell() */
65 #include "tree.h"
66 #include "menu.h"
67 #include "strutil.h"
68 #include "background.h" /* we_are_background */
69 /* Needed for the extern declarations of integer parameters */
70 #include "dir.h"
71 #include "panel.h" /* The Panel widget */
72 #include "cons.saver.h"
73 #include "layout.h"
74 #include "info.h" /* The Info widget */
75 #include "../src/viewer/mcviewer.h" /* The view widget */
77 #include "setup.h" /* For save_setup() */
78 #include "../vfs/vfs.h" /* For vfs_translate_url() */
80 /* Controls the display of the rotating dash on the verbose mode */
81 int nice_rotating_dash = 1;
83 /* Set if the panels are split horizontally */
84 int horizontal_split = 0;
86 /* Set if the window has changed it's size */
87 int winch_flag = 0;
89 /* Set if the split is the same */
90 int equal_split = 1;
92 /* First panel size if the panel are not split equally */
93 int first_panel_size = 0;
95 /* The number of output lines shown (if available) */
96 int output_lines = 0;
98 /* Set if the command prompt is to be displayed */
99 int command_prompt = 1;
101 /* Set if the nice and useful keybar is visible */
102 int keybar_visible = 1;
104 /* Set if the nice message (hint) bar is visible */
105 int message_visible = 1;
107 /* Set to show current working dir in xterm window title */
108 int xterm_title = 1;
110 /* Set to show free space on device assigned to current directory */
111 int free_space = 1;
113 /* The starting line for the output of the subprogram */
114 int output_start_y = 0;
116 /* The maximum number of views managed by the set_display_type routine */
117 /* Must be at least two (for current and other). Please note that until */
118 /* Janne gets around this, we will only manage two of them :-) */
119 #define MAX_VIEWS 2
121 static struct {
122 int type;
123 Widget *widget;
124 char *last_saved_dir; /* last view_list working directory */
125 } panels [MAX_VIEWS] = {
126 /* init MAX_VIEWS items */
127 { view_listing, NULL, NULL },
128 { view_listing, NULL, NULL }
131 /* These variables are used to avoid updating the information unless */
132 /* we need it */
133 static int old_first_panel_size;
134 static int old_horizontal_split;
135 static int old_output_lines;
137 /* Internal variables */
138 static int _horizontal_split;
139 static int _equal_split;
140 static int _first_panel_size;
141 static int _menubar_visible;
142 static int _output_lines;
143 static int _command_prompt;
144 static int _keybar_visible;
145 static int _message_visible;
146 static int _xterm_title;
147 static int _free_space;
148 static int _permission_mode;
149 static int _filetype_mode;
151 static int height;
153 /* Width 12 for a wee Quick (Hex) View */
154 #define MINWIDTH 12
155 #define MINHEIGHT 5
157 #define BY 12
159 #define B_2LEFT B_USER
160 #define B_2RIGHT (B_USER + 1)
161 #define B_PLUS (B_USER + 2)
162 #define B_MINUS (B_USER + 3)
164 static Dlg_head *layout_dlg;
166 static const char *s_split_direction [2] = {
167 N_("&Vertical"),
168 N_("&Horizontal")
171 static WRadio *radio_widget;
173 static struct {
174 const char *text;
175 int *variable;
176 WCheck *widget;
177 } check_options [] = {
178 { N_("show free sp&Ace"), &free_space, 0 },
179 { N_("&Xterm window title"), &xterm_title, 0 },
180 { N_("h&Intbar visible"), &message_visible, 0 },
181 { N_("&Keybar visible"), &keybar_visible, 0 },
182 { N_("command &Prompt"), &command_prompt, 0 },
183 { N_("show &Mini status"), &show_mini_info, 0 },
184 { N_("menu&Bar visible"), &menubar_visible, 0 },
185 { N_("&Equal split"), &equal_split, 0 },
186 { N_("pe&Rmissions"), &permission_mode, 0 },
187 { N_("&File types"), &filetype_mode, 0 },
188 { 0, 0, 0 }
191 #define LAYOUT_OPTIONS_COUNT 10
192 #define HIGHLIGHT_OPTIONS_COUNT 2
193 #define SPLIT_OPTIONS_COUNT 1
194 #define OTHER_OPTIONS_COUNT 7
196 static gsize first_width, second_width;
197 static const char *output_lines_label;
199 static WButton *bleft_widget, *bright_widget;
201 /* Declarations for static functions */
202 static void low_level_change_screen_size (void);
204 static void _check_split (void)
206 if (_horizontal_split){
207 if (_equal_split)
208 _first_panel_size = height / 2;
209 else if (_first_panel_size < MINHEIGHT)
210 _first_panel_size = MINHEIGHT;
211 else if (_first_panel_size > height - MINHEIGHT)
212 _first_panel_size = height - MINHEIGHT;
213 } else {
214 if (_equal_split)
215 _first_panel_size = COLS / 2;
216 else if (_first_panel_size < MINWIDTH)
217 _first_panel_size = MINWIDTH;
218 else if (_first_panel_size > COLS - MINWIDTH)
219 _first_panel_size = COLS - MINWIDTH;
223 static void update_split (void)
225 /* Check split has to be done before testing if it changed, since
226 it can change due to calling _check_split() as well*/
227 _check_split ();
229 /* To avoid setting the cursor to the wrong place */
230 if ((old_first_panel_size == _first_panel_size) &&
231 (old_horizontal_split == _horizontal_split)){
232 return;
235 old_first_panel_size = _first_panel_size;
236 old_horizontal_split = _horizontal_split;
238 tty_setcolor (COLOR_NORMAL);
239 dlg_move (layout_dlg, 6, 6);
240 tty_printf ("%03d", _first_panel_size);
241 dlg_move (layout_dlg, 6, 18);
242 if (_horizontal_split)
243 tty_printf ("%03d", height - _first_panel_size);
244 else
245 tty_printf ("%03d", COLS - _first_panel_size);
248 static int b2left_cback (int action)
250 (void) action;
252 if (_equal_split){
253 /* Turn equal split off */
254 _equal_split = 0;
255 check_options [7].widget->state = check_options [7].widget->state & ~C_BOOL;
256 dlg_select_widget (check_options [7].widget);
257 dlg_select_widget (bleft_widget);
259 _first_panel_size++;
260 return 0;
263 static int b2right_cback (int action)
265 (void) action;
267 if (_equal_split){
268 /* Turn equal split off */
269 _equal_split = 0;
270 check_options [7].widget->state = check_options [7].widget->state & ~C_BOOL;
271 dlg_select_widget (check_options [7].widget);
272 dlg_select_widget (bright_widget);
274 _first_panel_size--;
275 return 0;
278 static int bplus_cback (int action)
280 (void) action;
282 if (_output_lines < 99)
283 _output_lines++;
284 return 0;
287 static int bminus_cback (int action)
289 (void) action;
291 if (_output_lines > 0)
292 _output_lines--;
293 return 0;
296 static cb_ret_t
297 layout_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
299 switch (msg) {
300 case DLG_DRAW:
301 /*When repainting the whole dialog (e.g. with C-l) we have to
302 update everything*/
303 common_dialog_repaint (h);
305 old_first_panel_size = -1;
306 old_horizontal_split = -1;
307 old_output_lines = -1;
309 tty_setcolor (COLOR_HOT_NORMAL);
310 update_split ();
311 dlg_move (h, 6, 13);
312 tty_print_char ('=');
313 if (console_flag){
314 if (old_output_lines != _output_lines){
315 old_output_lines = _output_lines;
316 tty_setcolor (COLOR_NORMAL);
317 dlg_move (h, LAYOUT_OPTIONS_COUNT, 16 + first_width);
318 tty_print_string (output_lines_label);
319 dlg_move (h, LAYOUT_OPTIONS_COUNT, 10 + first_width);
320 tty_printf ("%02d", _output_lines);
323 return MSG_HANDLED;
325 case DLG_POST_KEY:
326 _filetype_mode = check_options [9].widget->state & C_BOOL;
327 _permission_mode = check_options [8].widget->state & C_BOOL;
328 _equal_split = check_options [7].widget->state & C_BOOL;
329 _menubar_visible = check_options [6].widget->state & C_BOOL;
330 _command_prompt = check_options [5].widget->state & C_BOOL;
331 _keybar_visible = check_options [3].widget->state & C_BOOL;
332 _message_visible = check_options [2].widget->state & C_BOOL;
333 _xterm_title = check_options [1].widget->state & C_BOOL;
334 _free_space = check_options [0].widget->state & C_BOOL;
335 if (console_flag){
336 int minimum;
337 if (_output_lines < 0)
338 _output_lines = 0;
339 height = LINES - _keybar_visible - _command_prompt -
340 _menubar_visible - _output_lines - _message_visible;
341 minimum = MINHEIGHT * (1 + _horizontal_split);
342 if (height < minimum){
343 _output_lines -= minimum - height;
344 height = minimum;
346 } else {
347 height = LINES - _keybar_visible - _command_prompt -
348 _menubar_visible - _output_lines - _message_visible;
350 if (_horizontal_split != radio_widget->sel){
351 _horizontal_split = radio_widget->sel;
352 if (_horizontal_split)
353 _first_panel_size = height / 2;
354 else
355 _first_panel_size = COLS / 2;
357 update_split ();
358 if (console_flag){
359 if (old_output_lines != _output_lines){
360 old_output_lines = _output_lines;
361 tty_setcolor (COLOR_NORMAL);
362 dlg_move (h, LAYOUT_OPTIONS_COUNT, 10 + first_width);
363 tty_printf ("%02d", _output_lines);
366 return MSG_HANDLED;
368 default:
369 return default_dlg_callback (h, msg, parm);
373 static void
374 init_layout (void)
376 static int i18n_layt_flag = 0;
377 static int b1, b2, b3;
378 int i = sizeof (s_split_direction) / sizeof (char *);
379 const char *ok_button = _("&OK");
380 const char *cancel_button = _("&Cancel");
381 const char *save_button = _("&Save");
382 static const char *title1, *title2, *title3;
384 if (!i18n_layt_flag) {
385 gsize l1;
387 first_width = 19; /* length of line with '<' '>' buttons */
389 title1 = _(" Panel split ");
390 title2 = _(" Highlight... ");
391 title3 = _(" Other options ");
392 output_lines_label = _("output lines");
394 while (i--) {
395 s_split_direction[i] = _(s_split_direction[i]);
396 l1 = str_term_width1 (s_split_direction[i]) + 7;
397 if (l1 > first_width)
398 first_width = l1;
401 for (i = 0; i < LAYOUT_OPTIONS_COUNT; i++) {
402 check_options[i].text = _(check_options[i].text);
403 l1 = str_term_width1 (check_options[i].text) + 7;
404 if (l1 > first_width)
405 first_width = l1;
408 l1 = str_term_width1 (title1) + 1;
409 if (l1 > first_width)
410 first_width = l1;
412 l1 = str_term_width1 (title2) + 1;
413 if (l1 > first_width)
414 first_width = l1;
416 second_width = str_term_width1 (title3) + 1;
417 for (i = 0; i < OTHER_OPTIONS_COUNT; i++) {
418 check_options[i].text = _(check_options[i].text);
419 l1 = str_term_width1 (check_options[i].text) + 7;
420 if (l1 > second_width)
421 second_width = l1;
423 if (console_flag) {
424 l1 = str_term_width1 (output_lines_label) + 13;
425 if (l1 > second_width)
426 second_width = l1;
430 * alex@bcs.zp.ua:
431 * To be completely correct, one need to check if the title
432 * does not exceed dialog length and total length of 3 buttons
433 * allows their placement in one row. But assuming this dialog
434 * is wide enough, I don't include such a tests.
436 * Now the last thing to do - properly space buttons...
438 l1 = 11 + str_term_width1 (ok_button) /* 14 - all brackets and inner space */
439 + str_term_width1 (save_button) /* notice: it is 3 char less because */
440 + str_term_width1 (cancel_button); /* of '&' char in button text */
442 i = (first_width + second_width - l1) / 4;
443 b1 = 5 + i;
444 b2 = b1 + str_term_width1 (ok_button) + i + 6;
445 b3 = b2 + str_term_width1 (save_button) + i + 4;
447 i18n_layt_flag = 1;
450 layout_dlg =
451 create_dlg (0, 0, 15, first_width + second_width + 9,
452 dialog_colors, layout_callback, "[Layout]",
453 _("Layout"), DLG_CENTER | DLG_REVERSE);
455 add_widget (layout_dlg, groupbox_new (2, 4, 6, first_width, title1));
456 add_widget (layout_dlg, groupbox_new (8, 4, 4, first_width, title2));
457 add_widget (layout_dlg,
458 groupbox_new (2, 5 + first_width, 10, second_width,
459 title3));
461 add_widget (layout_dlg,
462 button_new (BY, b3, B_CANCEL, NORMAL_BUTTON, cancel_button,
463 0));
464 add_widget (layout_dlg,
465 button_new (BY, b2, B_EXIT, NORMAL_BUTTON, save_button,
466 0));
467 add_widget (layout_dlg,
468 button_new (BY, b1, B_ENTER, DEFPUSH_BUTTON, ok_button,
469 0));
470 if (console_flag) {
471 add_widget (layout_dlg,
472 button_new (LAYOUT_OPTIONS_COUNT, 12 + first_width, B_MINUS,
473 NARROW_BUTTON, "&-", bminus_cback));
474 add_widget (layout_dlg,
475 button_new (LAYOUT_OPTIONS_COUNT, 7 + first_width, B_PLUS, NARROW_BUTTON,
476 "&+", bplus_cback));
478 #define XTRACT(i) *check_options[i].variable, check_options[i].text
480 for (i = 0; i < OTHER_OPTIONS_COUNT; i++) {
481 check_options[i].widget =
482 check_new (LAYOUT_OPTIONS_COUNT - i - 1, 7 + first_width, XTRACT (i));
483 add_widget (layout_dlg, check_options[i].widget);
485 check_options[9].widget = check_new (10, 6, XTRACT (9));
486 add_widget (layout_dlg, check_options[9].widget);
487 check_options[8].widget = check_new (9, 6, XTRACT (8));
488 add_widget (layout_dlg, check_options[8].widget);
490 _filetype_mode = filetype_mode;
491 _permission_mode = permission_mode;
492 _equal_split = equal_split;
493 _menubar_visible = menubar_visible;
494 _command_prompt = command_prompt;
495 _keybar_visible = keybar_visible;
496 _message_visible = message_visible;
497 _xterm_title = xterm_title;
498 _free_space = free_space;
499 bright_widget =
500 button_new (6, 15, B_2RIGHT, NARROW_BUTTON, "&>", b2right_cback);
501 add_widget (layout_dlg, bright_widget);
502 bleft_widget =
503 button_new (6, 9, B_2LEFT, NARROW_BUTTON, "&<", b2left_cback);
504 add_widget (layout_dlg, bleft_widget);
505 check_options[7].widget = check_new (5, 6, XTRACT (7));
506 old_first_panel_size = -1;
507 old_horizontal_split = -1;
508 old_output_lines = -1;
510 _first_panel_size = first_panel_size;
511 _output_lines = output_lines;
512 add_widget (layout_dlg, check_options[7].widget);
513 radio_widget = radio_new (3, 6, 2, s_split_direction);
514 add_widget (layout_dlg, radio_widget);
515 radio_widget->sel = horizontal_split;
518 static void
519 layout_change (void)
521 setup_panels ();
522 /* re-init the menu, because perhaps there was a change in the way
523 how the panel are split (horizontal/vertical). */
524 done_menu ();
525 init_menu ();
526 menubar_arrange (the_menubar);
527 load_hint (1);
530 void layout_cmd (void)
532 int result;
533 int i;
534 int layout_do_change = 0;
536 init_layout ();
537 run_dlg (layout_dlg);
538 result = layout_dlg->ret_value;
540 if (result == B_ENTER || result == B_EXIT){
541 for (i = 0; check_options [i].text; i++)
542 if (check_options [i].widget)
543 *check_options [i].variable = check_options [i].widget->state & C_BOOL;
544 horizontal_split = radio_widget->sel;
545 first_panel_size = _first_panel_size;
546 output_lines = _output_lines;
547 layout_do_change = 1;
549 if (result == B_EXIT){
550 save_layout ();
551 mc_config_save_file (mc_main_config, NULL);
554 destroy_dlg (layout_dlg);
555 if (layout_do_change)
556 layout_change ();
559 static void check_split (void)
561 if (horizontal_split){
562 if (equal_split)
563 first_panel_size = height / 2;
564 else if (first_panel_size < MINHEIGHT)
565 first_panel_size = MINHEIGHT;
566 else if (first_panel_size > height - MINHEIGHT)
567 first_panel_size = height - MINHEIGHT;
568 } else {
569 if (equal_split)
570 first_panel_size = COLS / 2;
571 else if (first_panel_size < MINWIDTH)
572 first_panel_size = MINWIDTH;
573 else if (first_panel_size > COLS - MINWIDTH)
574 first_panel_size = COLS - MINWIDTH;
578 void
579 clr_scr (void)
581 tty_set_normal_attrs ();
582 tty_fill_region (0, 0, LINES, COLS, ' ');
583 tty_refresh ();
586 void
587 repaint_screen (void)
589 do_refresh ();
590 tty_refresh ();
593 void
594 mc_refresh (void)
596 #ifdef WITH_BACKGROUND
597 if (we_are_background)
598 return;
599 #endif /* WITH_BACKGROUND */
600 if (winch_flag == 0)
601 tty_refresh ();
602 else {
603 /* if winch was caugth, we should do not only redraw screen, but
604 reposition/resize all */
605 change_screen_size ();
609 static void
610 panel_do_cols (int lc_index)
612 if (get_display_type (lc_index) == view_listing)
613 set_panel_formats ((WPanel *) panels [lc_index].widget);
614 else {
615 panel_update_cols (panels [lc_index].widget, frame_half);
619 void
620 setup_panels (void)
622 int start_y;
623 int promptl; /* the prompt len */
625 if (console_flag) {
626 int minimum;
627 if (output_lines < 0)
628 output_lines = 0;
629 height =
630 LINES - keybar_visible - command_prompt - menubar_visible -
631 output_lines - message_visible;
632 minimum = MINHEIGHT * (1 + horizontal_split);
633 if (height < minimum) {
634 output_lines -= minimum - height;
635 height = minimum;
637 } else {
638 height =
639 LINES - menubar_visible - command_prompt - keybar_visible -
640 message_visible;
642 check_split ();
643 start_y = menubar_visible;
645 /* The column computing is defered until panel_do_cols */
646 if (horizontal_split) {
647 widget_set_size (panels[0].widget, start_y, 0, first_panel_size,
650 widget_set_size (panels[1].widget, start_y + first_panel_size, 0,
651 height - first_panel_size, 0);
652 } else {
653 int first_x = first_panel_size;
655 widget_set_size (panels[0].widget, start_y, 0, height, 0);
657 widget_set_size (panels[1].widget, start_y, first_x, height, 0);
660 panel_do_cols (0);
661 panel_do_cols (1);
663 promptl = str_term_width1 (prompt);
665 widget_set_size (&the_menubar->widget, 0, 0, 1, COLS);
667 if (command_prompt) {
668 widget_set_size (&cmdline->widget, LINES - 1 - keybar_visible,
669 promptl, 1, COLS - promptl);
670 winput_set_origin (cmdline, promptl, COLS - promptl);
671 widget_set_size (&the_prompt->widget, LINES - 1 - keybar_visible,
672 0, 1, promptl);
673 } else {
674 widget_set_size (&cmdline->widget, 0, 0, 0, 0);
675 winput_set_origin (cmdline, 0, 0);
676 widget_set_size (&the_prompt->widget, LINES, COLS, 0, 0);
679 widget_set_size (&the_bar->widget, LINES - 1, 0, keybar_visible, COLS);
680 buttonbar_set_visible (the_bar, keybar_visible);
682 /* Output window */
683 if (console_flag && output_lines) {
684 output_start_y =
685 LINES - command_prompt - keybar_visible - output_lines;
686 show_console_contents (output_start_y,
687 LINES - output_lines - keybar_visible - 1,
688 LINES - keybar_visible - 1);
690 if (message_visible)
691 widget_set_size (&the_hint->widget, height + start_y, 0, 1, COLS);
692 else
693 widget_set_size (&the_hint->widget, 0, 0, 0, 0);
695 update_xterm_title_path ();
698 void
699 sigwinch_handler (int dummy)
701 (void) dummy;
702 #if !(defined(USE_NCURSES) || defined(USE_NCURSESW)) /* don't do malloc in a signal handler */
703 low_level_change_screen_size ();
704 #endif
705 winch_flag = 1;
708 static void
709 low_level_change_screen_size (void)
711 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
712 #if defined TIOCGWINSZ
713 struct winsize winsz;
715 winsz.ws_col = winsz.ws_row = 0;
716 /* Ioctl on the STDIN_FILENO */
717 ioctl (0, TIOCGWINSZ, &winsz);
718 if (winsz.ws_col && winsz.ws_row){
719 #if defined(NCURSES_VERSION) && defined(HAVE_RESIZETERM)
720 resizeterm(winsz.ws_row, winsz.ws_col);
721 clearok(stdscr,TRUE); /* sigwinch's should use a semaphore! */
722 #else
723 COLS = winsz.ws_col;
724 LINES = winsz.ws_row;
725 #endif
726 #ifdef HAVE_SUBSHELL_SUPPORT
727 resize_subshell ();
728 #endif
730 #endif /* TIOCGWINSZ */
731 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
734 void
735 change_screen_size (void)
737 Dlg_head *d;
739 winch_flag = 0;
740 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
741 #if defined TIOCGWINSZ
743 #ifndef NCURSES_VERSION
744 tty_noraw_mode ();
745 tty_reset_screen ();
746 #endif
747 low_level_change_screen_size ();
748 #ifdef HAVE_SLANG
749 /* XSI Curses spec states that portable applications shall not invoke
750 * initscr() more than once. This kludge could be done within the scope
751 * of the specification by using endwin followed by a refresh (in fact,
752 * more than one curses implementation does this); it is guaranteed to work
753 * only with slang.
755 SLsmg_init_smg ();
756 do_enter_ca_mode ();
757 tty_keypad (TRUE);
758 tty_nodelay (FALSE);
759 #endif
760 setup_panels ();
762 /* Inform all running dialogs */
763 d = current_dlg;
764 while (d != NULL) {
765 (*d->callback) (d, DLG_RESIZE, 0);
766 d = d->parent;
769 #ifdef RESIZABLE_MENUBAR
770 menubar_arrange (the_menubar);
771 #endif
773 /* Now, force the redraw */
774 repaint_screen ();
775 #endif /* TIOCGWINSZ */
776 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
779 static int ok_to_refresh = 1;
781 void use_dash (int flag)
783 if (flag)
784 ok_to_refresh++;
785 else
786 ok_to_refresh--;
789 void set_hintbar(const char *str)
791 label_set_text (the_hint, str);
792 if (ok_to_refresh > 0)
793 mc_refresh();
796 void print_vfs_message (const char *msg, ...)
798 va_list ap;
799 char str [128];
801 va_start (ap, msg);
803 g_vsnprintf (str, sizeof (str), msg, ap);
804 va_end (ap);
806 if (midnight_shutdown)
807 return;
809 if (!message_visible || !the_hint || !the_hint->widget.parent) {
810 int col, row;
812 if (!nice_rotating_dash || (ok_to_refresh <= 0))
813 return;
815 /* Preserve current cursor position */
816 tty_getyx (&row, &col);
818 tty_gotoyx (0, 0);
819 tty_setcolor (NORMAL_COLOR);
820 tty_print_string (str_fit_to_term (str, COLS - 1, J_LEFT));
822 /* Restore cursor position */
823 tty_gotoyx (row, col);
824 mc_refresh ();
825 return;
828 if (message_visible) {
829 set_hintbar(str);
833 void rotate_dash (void)
835 static const char rotating_dash [] = "|/-\\";
836 static size_t pos = 0;
838 if (!nice_rotating_dash || (ok_to_refresh <= 0))
839 return;
841 if (pos >= sizeof (rotating_dash)-1)
842 pos = 0;
843 tty_gotoyx (0, COLS - 1);
844 tty_setcolor (NORMAL_COLOR);
845 tty_print_char (rotating_dash [pos]);
846 mc_refresh ();
847 pos++;
850 const char *get_nth_panel_name (int num)
852 static char buffer [BUF_SMALL];
854 if (!num)
855 return "New Left Panel";
856 else if (num == 1)
857 return "New Right Panel";
858 else {
859 g_snprintf (buffer, sizeof (buffer), "%ith Panel", num);
860 return buffer;
864 /* I wonder if I should start to use the folding mode than Dugan uses */
865 /* */
866 /* This is the centralized managing of the panel display types */
867 /* This routine takes care of destroying and creating new widgets */
868 /* Please note that it could manage MAX_VIEWS, not just left and right */
869 /* Currently nothing in the code takes advantage of this and has hard- */
870 /* coded values for two panels only */
872 /* Set the num-th panel to the view type: type */
873 /* This routine also keeps at least one WPanel object in the screen */
874 /* since a lot of routines depend on the current_panel variable */
875 void
876 set_display_type (int num, int type)
878 int x = 0, y = 0, cols = 0, lines = 0;
879 int the_other = 0; /* Index to the other panel */
880 const char *file_name = NULL; /* For Quick view */
881 Widget *new_widget = NULL, *old_widget = NULL;
882 WPanel *the_other_panel = NULL;
884 if (num >= MAX_VIEWS){
885 fprintf (stderr, "Cannot allocate more that %d views\n", MAX_VIEWS);
886 abort ();
888 /* Check that we will have a WPanel * at least */
889 if (type != view_listing){
890 the_other = num == 0 ? 1 : 0;
892 if (panels [the_other].type != view_listing)
893 return;
896 /* Get rid of it */
897 if (panels [num].widget){
898 Widget *w = panels [num].widget;
899 WPanel *panel = (WPanel *) panels [num].widget;
901 x = w->x;
902 y = w->y;
903 cols = w->cols;
904 lines = w->lines;
905 old_widget = panels [num].widget;
907 if (panels [num].type == view_listing){
908 if (panel->frame_size == frame_full && type != view_listing){
909 cols = COLS - first_panel_size;
910 if (num == 1)
911 x = first_panel_size;
916 /* Restoring saved path from panels.ini for nonlist panel */
917 /* when it's first creation (for example view_info) */
918 if (old_widget == NULL && type != view_listing) {
919 char panel_dir[MC_MAXPATHLEN];
920 mc_get_current_wd (panel_dir, sizeof (panel_dir));
921 panels[num].last_saved_dir = g_strdup (panel_dir);
924 switch (type){
925 case view_listing:
926 new_widget = restore_into_right_dir_panel(num, old_widget);
927 break;
929 case view_info:
930 new_widget = (Widget *) info_new ();
931 break;
933 case view_tree:
934 new_widget = (Widget *) tree_new (1, 0, 0, 0, 0);
935 break;
937 case view_quick:
938 new_widget = (Widget *) mcview_new (0, 0, 0, 0, 1);
939 the_other_panel = (WPanel *) panels [the_other].widget;
940 if (the_other_panel)
941 file_name =
942 the_other_panel->dir.list[the_other_panel->selected].fname;
943 else
944 file_name = "";
946 mcview_load ((struct mcview_struct *) new_widget, 0, file_name, 0);
947 break;
950 if (type != view_listing)
951 /* Must save dir, for restoring after change type to */
952 /* view_listing */
953 save_panel_dir(num);
955 panels [num].type = type;
956 panels [num].widget = new_widget;
958 /* We set the same size the old widget had */
959 widget_set_size (new_widget, y, x, lines, cols);
961 /* We use replace to keep the circular list of the dialog in the */
962 /* same state. Maybe we could just kill it and then replace it */
963 if (midnight_dlg && old_widget){
964 dlg_replace_widget (old_widget, panels [num].widget);
966 if (type == view_listing){
967 if (num == 0)
968 left_panel = (WPanel *) new_widget;
969 else
970 right_panel = (WPanel *) new_widget;
973 if (type == view_tree)
974 the_tree = (WTree *) new_widget;
976 /* Prevent current_panel's value from becoming invalid.
977 * It's just a quick hack to prevent segfaults. Comment out and
978 * try following:
979 * - select left panel
980 * - invoke menue left/tree
981 * - as long as you stay in the left panel almost everything that uses
982 * current_panel causes segfault, e.g. C-Enter, C-x c, ...
985 if (type != view_listing)
986 if (current_panel == (WPanel *) old_widget)
987 current_panel = num == 0 ? right_panel : left_panel;
990 /* This routine is deeply sticked to the two panels idea.
991 What should it do in more panels. ANSWER - don't use it
992 in any multiple panels environment. */
993 void swap_panels ()
995 Widget tmp;
996 Widget *tmp_widget;
997 WPanel panel;
998 WPanel *panel1, *panel2;
999 int tmp_type;
1001 #define panelswap(x) panel. x = panel1-> x; panel1-> x = panel2-> x; panel2-> x = panel. x;
1003 #define panelswapstr(e) strcpy (panel. e, panel1-> e); \
1004 strcpy (panel1-> e, panel2-> e); \
1005 strcpy (panel2-> e, panel. e);
1006 panel1 = (WPanel *) panels [0].widget;
1007 panel2 = (WPanel *) panels [1].widget;
1008 if (panels [0].type == view_listing && panels [1].type == view_listing) {
1009 /* Change everything except format/sort/panel_name etc. */
1010 panelswap (dir);
1011 panelswap (active);
1012 panelswapstr (cwd);
1013 panelswapstr (lwd);
1014 panelswap (count);
1015 panelswap (marked);
1016 panelswap (dirs_marked);
1017 panelswap (total);
1018 panelswap (top_file);
1019 panelswap (selected);
1020 panelswap (is_panelized);
1021 panelswap (dir_stat);
1023 panel1->searching = 0;
1024 panel2->searching = 0;
1025 if (current_panel == panel1)
1026 current_panel = panel2;
1027 else
1028 current_panel = panel1;
1030 if (dlg_widget_active (panels[0].widget))
1031 dlg_select_widget (panels [1].widget);
1032 else if (dlg_widget_active (panels[1].widget))
1033 dlg_select_widget (panels [0].widget);
1034 } else {
1035 WPanel *tmp_panel;
1037 tmp_panel=right_panel;
1038 right_panel=left_panel;
1039 left_panel=tmp_panel;
1041 if (panels [0].type == view_listing) {
1042 if (!strcmp (panel1->panel_name, get_nth_panel_name (0))) {
1043 g_free (panel1->panel_name);
1044 panel1->panel_name = g_strdup (get_nth_panel_name (1));
1047 if (panels [1].type == view_listing) {
1048 if (!strcmp (panel2->panel_name, get_nth_panel_name (1))) {
1049 g_free (panel2->panel_name);
1050 panel2->panel_name = g_strdup (get_nth_panel_name (0));
1054 tmp.x = panels [0].widget->x;
1055 tmp.y = panels [0].widget->y;
1056 tmp.cols = panels [0].widget->cols;
1057 tmp.lines = panels [0].widget->lines;
1059 panels [0].widget->x = panels [1].widget->x;
1060 panels [0].widget->y = panels [1].widget->y;
1061 panels [0].widget->cols = panels [1].widget->cols;
1062 panels [0].widget->lines = panels [1].widget->lines;
1064 panels [1].widget->x = tmp.x;
1065 panels [1].widget->y = tmp.y;
1066 panels [1].widget->cols = tmp.cols;
1067 panels [1].widget->lines = tmp.lines;
1069 tmp_widget = panels [0].widget;
1070 panels [0].widget = panels [1].widget;
1071 panels [1].widget = tmp_widget;
1072 tmp_type = panels [0].type;
1073 panels [0].type = panels [1].type;
1074 panels [1].type = tmp_type;
1078 int get_display_type (int lc_index)
1080 return panels [lc_index].type;
1083 struct Widget *
1084 get_panel_widget (int lc_index)
1086 return panels[lc_index].widget;
1089 int get_current_index (void)
1091 if (panels [0].widget == ((Widget *) current_panel))
1092 return 0;
1093 else
1094 return 1;
1097 int get_other_index (void)
1099 return !get_current_index ();
1102 struct WPanel *
1103 get_other_panel (void)
1105 return (struct WPanel *) get_panel_widget (get_other_index ());
1108 /* Returns the view type for the current panel/view */
1109 int get_current_type (void)
1111 if (panels [0].widget == (Widget *) current_panel)
1112 return panels [0].type;
1113 else
1114 return panels [1].type;
1117 /* Returns the view type of the unselected panel */
1118 int get_other_type (void)
1120 if (panels [0].widget == (Widget *) current_panel)
1121 return panels [1].type;
1122 else
1123 return panels [0].type;
1126 /* Save current list_view widget directory into panel */
1127 void
1128 save_panel_dir (int lc_index)
1130 int type = get_display_type (lc_index);
1131 Widget *widget = get_panel_widget (lc_index);
1133 if ((type == view_listing) && (widget != NULL)) {
1134 WPanel *w = (WPanel *) widget;
1135 char *widget_work_dir = w->cwd;
1137 g_free(panels [lc_index].last_saved_dir); /* last path no needed */
1138 /* Because path can be nonlocal */
1139 panels [lc_index].last_saved_dir = vfs_translate_url(widget_work_dir);
1143 /* Save current list_view widget directory into panel */
1144 Widget *
1145 restore_into_right_dir_panel (int lc_index, Widget *from_widget)
1147 Widget *new_widget = 0;
1148 const char *saved_dir = panels [lc_index].last_saved_dir;
1149 gboolean last_was_panel = (from_widget &&
1150 get_display_type(lc_index) != view_listing);
1151 const char *p_name = get_nth_panel_name (lc_index);
1153 if (last_was_panel)
1154 new_widget = (Widget *) panel_new_with_dir (p_name, saved_dir);
1155 else
1156 new_widget = (Widget *) panel_new (p_name);
1158 return new_widget;
1161 /* Return working dir, if it's view_listing - cwd,
1162 but for other types - last_saved_dir */
1163 const char *
1164 get_panel_dir_for (const WPanel *widget)
1166 int i;
1168 for (i = 0; i < MAX_VIEWS; i++)
1169 if ((WPanel *) get_panel_widget (i) == widget)
1170 break;
1172 if (i >= MAX_VIEWS)
1173 return ".";
1175 if (get_display_type (i) == view_listing)
1176 return ((WPanel *) get_panel_widget (i))->cwd;
1178 return panels[i].last_saved_dir;