Ticket #2237: Automatic date and version substitution for man pages
[midnight-commander.git] / src / layout.c
blobf773758b5e35936a0b674d618bb9bc724cfc7227
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"
57 #include "dialog.h"
58 #include "dialog-switch.h" /* dialog_switch_got_winch() */
59 #include "widget.h"
60 #include "command.h"
61 #include "main-widgets.h"
62 #include "main.h"
63 #include "subshell.h" /* For use_subshell and resize_subshell() */
64 #include "tree.h"
65 #include "menu.h"
66 #include "background.h" /* we_are_background */
67 /* Needed for the extern declarations of integer parameters */
68 #include "dir.h"
69 #include "panel.h" /* The Panel widget */
70 #include "consaver/cons.saver.h"
71 #include "layout.h"
72 #include "info.h" /* The Info widget */
73 #include "src/viewer/mcviewer.h" /* The view widget */
75 #include "setup.h" /* For save_setup() */
77 /* Controls the display of the rotating dash on the verbose mode */
78 int nice_rotating_dash = 1;
80 /* Set if the panels are split horizontally */
81 int horizontal_split = 0;
83 /* Set if the window has changed it's size */
84 int winch_flag = 0;
86 /* Set if the split is the same */
87 int equal_split = 1;
89 /* First panel size if the panel are not split equally */
90 int first_panel_size = 0;
92 /* The number of output lines shown (if available) */
93 int output_lines = 0;
95 /* Set if the command prompt is to be displayed */
96 int command_prompt = 1;
98 /* Set if the nice and useful keybar is visible */
99 int keybar_visible = 1;
101 /* Set if the nice message (hint) bar is visible */
102 int message_visible = 1;
104 /* Set to show current working dir in xterm window title */
105 int xterm_title = 1;
107 /* Set to show free space on device assigned to current directory */
108 int free_space = 1;
110 /* The starting line for the output of the subprogram */
111 int output_start_y = 0;
113 /* The maximum number of views managed by the set_display_type routine */
114 /* Must be at least two (for current and other). Please note that until */
115 /* Janne gets around this, we will only manage two of them :-) */
116 #define MAX_VIEWS 2
118 static struct
120 panel_view_mode_t type;
121 Widget *widget;
122 char *last_saved_dir; /* last view_list working directory */
123 } panels[MAX_VIEWS] =
125 /* *INDENT-OFF* */
126 /* init MAX_VIEWS items */
127 { view_listing, NULL, NULL},
128 { view_listing, NULL, NULL}
129 /* *INDENT-ON* */
132 /* These variables are used to avoid updating the information unless */
133 /* we need it */
134 static int old_first_panel_size;
135 static int old_horizontal_split;
136 static int old_output_lines;
138 /* Internal variables */
139 static int _horizontal_split;
140 static int _equal_split;
141 static int _first_panel_size;
142 static int _menubar_visible;
143 static int _output_lines;
144 static int _command_prompt;
145 static int _keybar_visible;
146 static int _message_visible;
147 static int _xterm_title;
148 static int _free_space;
150 static int height;
152 /* Width 12 for a wee Quick (Hex) View */
153 #define MINWIDTH 12
154 #define MINHEIGHT 5
156 #define B_2LEFT B_USER
157 #define B_2RIGHT (B_USER + 1)
158 #define B_PLUS (B_USER + 2)
159 #define B_MINUS (B_USER + 3)
161 static const char *s_split_direction[2] = {
162 N_("&Vertical"),
163 N_("&Horizontal")
166 static WRadio *radio_widget;
168 static struct
170 const char *text;
171 int *variable;
172 WCheck *widget;
173 } check_options[] =
175 /* *INDENT-OFF* */
176 { N_("Show free sp&ace"), &free_space, NULL},
177 { N_("&XTerm window title"), &xterm_title, NULL},
178 { N_("H&intbar visible"), &message_visible, NULL},
179 { N_("&Keybar visible"), &keybar_visible, NULL},
180 { N_("Command &prompt"), &command_prompt, NULL},
181 { N_("Show &mini status"), &show_mini_info, NULL},
182 { N_("Menu&bar visible"), &menubar_visible, NULL},
183 { N_("&Equal split"), &equal_split, NULL}
184 /* *INDENT-ON* */
187 #define LAYOUT_OPTIONS_COUNT (sizeof (check_options) / sizeof (check_options[0]))
188 #define OTHER_OPTIONS_COUNT (LAYOUT_OPTIONS_COUNT - 1)
190 static gsize first_width;
191 static const char *output_lines_label = NULL;
192 static int output_lines_label_len;
194 static WButton *bleft_widget, *bright_widget;
196 static inline void
197 _check_split (void)
199 if (_horizontal_split)
201 if (_equal_split)
202 _first_panel_size = height / 2;
203 else if (_first_panel_size < MINHEIGHT)
204 _first_panel_size = MINHEIGHT;
205 else if (_first_panel_size > height - MINHEIGHT)
206 _first_panel_size = height - MINHEIGHT;
208 else
210 if (_equal_split)
211 _first_panel_size = COLS / 2;
212 else if (_first_panel_size < MINWIDTH)
213 _first_panel_size = MINWIDTH;
214 else if (_first_panel_size > COLS - MINWIDTH)
215 _first_panel_size = COLS - MINWIDTH;
218 old_first_panel_size = _first_panel_size;
219 old_horizontal_split = _horizontal_split;
222 static void
223 update_split (const Dlg_head *h)
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 tty_setcolor (check_options[7].widget->state & C_BOOL ? DISABLED_COLOR : COLOR_NORMAL);
231 dlg_move (h, 6, 6);
232 tty_printf ("%03d", _first_panel_size);
234 dlg_move (h, 6, 18);
235 if (_horizontal_split)
236 tty_printf ("%03d", height - _first_panel_size);
237 else
238 tty_printf ("%03d", COLS - _first_panel_size);
240 dlg_move (h, 6, 13);
241 tty_print_char ('=');
244 static int
245 b_left_right_cback (WButton *button, int action)
247 (void) action;
249 if (button == bleft_widget)
250 _first_panel_size++;
251 else
252 _first_panel_size--;
254 update_split (button->widget.owner);
255 return 0;
258 static int
259 bplus_cback (WButton *button, int action)
261 (void) button;
262 (void) action;
264 if (_output_lines < 99)
265 _output_lines++;
266 return 0;
269 static int
270 bminus_cback (WButton *button, int action)
272 (void) button;
273 (void) action;
275 if (_output_lines > 0)
276 _output_lines--;
277 return 0;
280 static cb_ret_t
281 layout_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
283 switch (msg)
285 case DLG_DRAW:
286 /*When repainting the whole dialog (e.g. with C-l) we have to
287 update everything */
288 common_dialog_repaint (h);
290 old_first_panel_size = -1;
291 old_horizontal_split = -1;
292 old_output_lines = -1;
294 update_split (h);
296 if (old_output_lines != _output_lines)
298 old_output_lines = _output_lines;
299 tty_setcolor (console_flag ? COLOR_NORMAL : DISABLED_COLOR);
300 dlg_move (h, 9, 6);
301 tty_print_string (output_lines_label);
302 dlg_move (h, 9, 6 + 3 + output_lines_label_len);
303 tty_printf ("%02d", _output_lines);
305 return MSG_HANDLED;
307 case DLG_POST_KEY:
308 _menubar_visible = check_options[6].widget->state & C_BOOL;
309 _command_prompt = check_options[5].widget->state & C_BOOL;
310 _keybar_visible = check_options[3].widget->state & C_BOOL;
311 _message_visible = check_options[2].widget->state & C_BOOL;
312 _xterm_title = check_options[1].widget->state & C_BOOL;
313 _free_space = check_options[0].widget->state & C_BOOL;
315 if (console_flag)
317 int minimum;
318 if (_output_lines < 0)
319 _output_lines = 0;
320 height = LINES - _keybar_visible - _command_prompt -
321 _menubar_visible - _output_lines - _message_visible;
322 minimum = MINHEIGHT * (1 + _horizontal_split);
323 if (height < minimum)
325 _output_lines -= minimum - height;
326 height = minimum;
329 else
330 height = LINES - _keybar_visible - _command_prompt -
331 _menubar_visible - _output_lines - _message_visible;
333 if (old_output_lines != _output_lines)
335 old_output_lines = _output_lines;
336 tty_setcolor (console_flag ? COLOR_NORMAL : DISABLED_COLOR);
337 dlg_move (h, 9, 6 + 3 + output_lines_label_len);
338 tty_printf ("%02d", _output_lines);
340 return MSG_HANDLED;
342 case DLG_ACTION:
343 if (sender == (Widget *) radio_widget)
345 if (_horizontal_split != radio_widget->sel)
347 _horizontal_split = radio_widget->sel;
348 if (_equal_split)
350 if (_horizontal_split)
351 _first_panel_size = height / 2;
352 else
353 _first_panel_size = COLS / 2;
357 update_split (h);
359 return MSG_HANDLED;
362 if (sender == (Widget *) check_options[7].widget)
364 _equal_split = check_options[7].widget->state & C_BOOL;
366 widget_disable (bleft_widget->widget, _equal_split);
367 send_message ((Widget *) bleft_widget, WIDGET_DRAW, 0);
368 widget_disable (bright_widget->widget, _equal_split);
369 send_message ((Widget *) bright_widget, WIDGET_DRAW, 0);
371 update_split (h);
373 return MSG_HANDLED;
376 return MSG_NOT_HANDLED;
378 default:
379 return default_dlg_callback (h, sender, msg, parm, data);
383 static Dlg_head *
384 init_layout (void)
386 static gboolean i18n_layt_flag = FALSE;
387 static int b1, b2, b3;
388 size_t i = sizeof (s_split_direction) / sizeof (char *);
389 const char *ok_button = _("&OK");
390 const char *cancel_button = _("&Cancel");
391 const char *save_button = _("&Save");
392 static const char *title1, *title2, *title3;
394 Dlg_head *layout_dlg;
396 if (!i18n_layt_flag)
398 gsize l1;
400 first_width = 0;
402 title1 = _("Panel split");
403 title2 = _("Console output");
404 title3 = _("Other options");
405 output_lines_label = _("Output lines:");
407 while (i--)
409 s_split_direction[i] = _(s_split_direction[i]);
410 l1 = str_term_width1 (s_split_direction[i]) + 7;
411 if (l1 > first_width)
412 first_width = l1;
415 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
417 check_options[i].text = _(check_options[i].text);
418 l1 = str_term_width1 (check_options[i].text) + 7;
419 if (l1 > first_width)
420 first_width = l1;
423 l1 = str_term_width1 (title1) + 1;
424 if (l1 > first_width)
425 first_width = l1;
427 l1 = str_term_width1 (title2) + 1;
428 if (l1 > first_width)
429 first_width = l1;
431 output_lines_label_len = str_term_width1 (output_lines_label);
432 l1 = output_lines_label_len + 12;
433 if (l1 > first_width)
434 first_width = l1;
437 * alex@bcs.zp.ua:
438 * To be completely correct, one need to check if the title
439 * does not exceed dialog length and total length of 3 buttons
440 * allows their placement in one row. But assuming this dialog
441 * is wide enough, I don't include such a tests.
443 * Now the last thing to do - properly space buttons...
445 l1 = 11 + str_term_width1 (ok_button) /* 14 - all brackets and inner space */
446 + str_term_width1 (save_button) /* notice: it is 3 char less because */
447 + str_term_width1 (cancel_button); /* of '&' char in button text */
449 i = (first_width * 2 - l1) / 4;
450 b1 = 5 + i;
451 b2 = b1 + str_term_width1 (ok_button) + i + 6;
452 b3 = b2 + str_term_width1 (save_button) + i + 4;
454 i18n_layt_flag = TRUE;
457 layout_dlg =
458 create_dlg (TRUE, 0, 0, 14, first_width * 2 + 9,
459 dialog_colors, layout_callback, "[Layout]",
460 _("Layout"), DLG_CENTER | DLG_REVERSE);
462 add_widget (layout_dlg, groupbox_new (2, 4, 6, first_width, title1));
464 add_widget (layout_dlg, groupbox_new (2, 5 + first_width, 9, first_width, title3));
466 add_widget (layout_dlg, button_new (11, b3, B_CANCEL, NORMAL_BUTTON, cancel_button, 0));
467 add_widget (layout_dlg, button_new (11, b2, B_EXIT, NORMAL_BUTTON, save_button, 0));
468 add_widget (layout_dlg, button_new (11, b1, B_ENTER, DEFPUSH_BUTTON, ok_button, 0));
469 #define XTRACT(i) *check_options[i].variable, check_options[i].text
471 for (i = 0; i < (size_t) OTHER_OPTIONS_COUNT; i++)
473 check_options[i].widget =
474 check_new (OTHER_OPTIONS_COUNT - i + 2, 7 + first_width, XTRACT (i));
475 add_widget (layout_dlg, check_options[i].widget);
478 _equal_split = equal_split;
479 _menubar_visible = menubar_visible;
480 _command_prompt = command_prompt;
481 _keybar_visible = keybar_visible;
482 _message_visible = message_visible;
483 _xterm_title = xterm_title;
484 _free_space = free_space;
487 const int disabled = console_flag ? 0 : W_DISABLED;
488 Widget *w;
490 w = (Widget *) groupbox_new (8, 4, 3, first_width, title2);
491 w->options |= disabled;
492 add_widget (layout_dlg, w);
494 w = (Widget *) button_new (9, output_lines_label_len + 6 + 5, B_MINUS,
495 NARROW_BUTTON, "&-", bminus_cback);
496 w->options |= disabled;
497 add_widget (layout_dlg, w);
499 w = (Widget *) button_new (9, output_lines_label_len + 6, B_PLUS,
500 NARROW_BUTTON, "&+", bplus_cback);
501 w->options |= disabled;
502 add_widget (layout_dlg, w);
505 bright_widget = button_new (6, 15, B_2RIGHT, NARROW_BUTTON, "&>", b_left_right_cback);
506 widget_disable (bright_widget->widget, _equal_split);
507 add_widget (layout_dlg, bright_widget);
508 bleft_widget = button_new (6, 9, B_2LEFT, NARROW_BUTTON, "&<", b_left_right_cback);
509 widget_disable (bleft_widget->widget, _equal_split);
510 add_widget (layout_dlg, bleft_widget);
511 check_options[7].widget = check_new (5, 6, XTRACT (7));
513 old_first_panel_size = -1;
514 old_horizontal_split = -1;
515 old_output_lines = -1;
517 _first_panel_size = first_panel_size;
518 _output_lines = output_lines;
520 add_widget (layout_dlg, check_options[7].widget);
521 radio_widget = radio_new (3, 6, 2, s_split_direction);
522 add_widget (layout_dlg, radio_widget);
523 radio_widget->sel = horizontal_split;
525 return layout_dlg;
528 void
529 layout_change (void)
531 setup_panels ();
532 /* re-init the menu, because perhaps there was a change in the way
533 how the panel are split (horizontal/vertical). */
534 done_menu ();
535 init_menu ();
536 menubar_arrange (the_menubar);
537 load_hint (1);
540 void
541 layout_box (void)
543 Dlg_head *layout_dlg;
544 int result;
545 gboolean layout_do_change = FALSE;
547 layout_dlg = init_layout ();
548 result = run_dlg (layout_dlg);
550 if (result == B_ENTER || result == B_EXIT)
552 size_t i;
553 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
554 if (check_options[i].widget != NULL)
555 *check_options[i].variable = check_options[i].widget->state & C_BOOL;
556 horizontal_split = radio_widget->sel;
557 first_panel_size = _first_panel_size;
558 output_lines = _output_lines;
559 layout_do_change = TRUE;
561 if (result == B_EXIT)
563 save_layout ();
564 mc_config_save_file (mc_main_config, NULL);
567 destroy_dlg (layout_dlg);
568 if (layout_do_change)
569 layout_change ();
572 static void
573 check_split (void)
575 if (horizontal_split)
577 if (equal_split)
578 first_panel_size = height / 2;
579 else if (first_panel_size < MINHEIGHT)
580 first_panel_size = MINHEIGHT;
581 else if (first_panel_size > height - MINHEIGHT)
582 first_panel_size = height - MINHEIGHT;
584 else
586 if (equal_split)
587 first_panel_size = COLS / 2;
588 else if (first_panel_size < MINWIDTH)
589 first_panel_size = MINWIDTH;
590 else if (first_panel_size > COLS - MINWIDTH)
591 first_panel_size = COLS - MINWIDTH;
595 void
596 clr_scr (void)
598 tty_set_normal_attrs ();
599 tty_fill_region (0, 0, LINES, COLS, ' ');
600 tty_refresh ();
603 void
604 repaint_screen (void)
606 do_refresh ();
607 tty_refresh ();
610 void
611 mc_refresh (void)
613 #ifdef WITH_BACKGROUND
614 if (we_are_background)
615 return;
616 #endif /* WITH_BACKGROUND */
617 if (winch_flag == 0)
618 tty_refresh ();
619 else
621 /* if winch was caugth, we should do not only redraw screen, but
622 reposition/resize all */
623 change_screen_size ();
627 static void
628 panel_do_cols (int idx)
630 if (get_display_type (idx) == view_listing)
631 set_panel_formats ((WPanel *) panels[idx].widget);
632 else
633 panel_update_cols (panels[idx].widget, frame_half);
636 void
637 setup_panels (void)
639 int start_y;
640 int promptl; /* the prompt len */
642 if (console_flag)
644 int minimum;
645 if (output_lines < 0)
646 output_lines = 0;
647 height =
648 LINES - keybar_visible - command_prompt - menubar_visible -
649 output_lines - message_visible;
650 minimum = MINHEIGHT * (1 + horizontal_split);
651 if (height < minimum)
653 output_lines -= minimum - height;
654 height = minimum;
657 else
659 height = LINES - menubar_visible - command_prompt - keybar_visible - message_visible;
661 check_split ();
662 start_y = menubar_visible;
664 /* The column computing is defered until panel_do_cols */
665 if (horizontal_split)
667 widget_set_size (panels[0].widget, start_y, 0, first_panel_size, 0);
669 widget_set_size (panels[1].widget, start_y + first_panel_size, 0,
670 height - first_panel_size, 0);
672 else
674 int first_x = first_panel_size;
676 widget_set_size (panels[0].widget, start_y, 0, height, 0);
678 widget_set_size (panels[1].widget, start_y, first_x, height, 0);
681 panel_do_cols (0);
682 panel_do_cols (1);
684 promptl = str_term_width1 (mc_prompt);
686 widget_set_size (&the_menubar->widget, 0, 0, 1, COLS);
688 if (command_prompt)
690 widget_set_size (&cmdline->widget, LINES - 1 - keybar_visible, promptl, 1, COLS - promptl);
691 winput_set_origin (cmdline, promptl, COLS - promptl);
692 widget_set_size (&the_prompt->widget, LINES - 1 - keybar_visible, 0, 1, promptl);
694 else
696 widget_set_size (&cmdline->widget, 0, 0, 0, 0);
697 winput_set_origin (cmdline, 0, 0);
698 widget_set_size (&the_prompt->widget, LINES, COLS, 0, 0);
701 widget_set_size (&the_bar->widget, LINES - 1, 0, keybar_visible, COLS);
702 buttonbar_set_visible (the_bar, keybar_visible);
704 /* Output window */
705 if (console_flag && output_lines)
707 output_start_y = LINES - command_prompt - keybar_visible - output_lines;
708 show_console_contents (output_start_y,
709 LINES - output_lines - keybar_visible - 1,
710 LINES - keybar_visible - 1);
712 if (message_visible)
713 widget_set_size (&the_hint->widget, height + start_y, 0, 1, COLS);
714 else
715 widget_set_size (&the_hint->widget, 0, 0, 0, 0);
717 update_xterm_title_path ();
720 static inline void
721 low_level_change_screen_size (void)
723 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
724 #if defined TIOCGWINSZ
725 struct winsize winsz;
727 winsz.ws_col = winsz.ws_row = 0;
728 /* Ioctl on the STDIN_FILENO */
729 ioctl (0, TIOCGWINSZ, &winsz);
730 if (winsz.ws_col && winsz.ws_row)
732 #if defined(NCURSES_VERSION) && defined(HAVE_RESIZETERM)
733 resizeterm (winsz.ws_row, winsz.ws_col);
734 clearok (stdscr, TRUE); /* sigwinch's should use a semaphore! */
735 #else
736 COLS = winsz.ws_col;
737 LINES = winsz.ws_row;
738 #endif
739 #ifdef HAVE_SUBSHELL_SUPPORT
740 resize_subshell ();
741 #endif
743 #endif /* TIOCGWINSZ */
744 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
747 static void
748 dlg_resize_cb (void *data, void *user_data)
750 Dlg_head *d = data;
752 (void) user_data;
753 d->callback (d, NULL, DLG_RESIZE, 0, NULL);
756 void
757 sigwinch_handler (int dummy)
759 (void) dummy;
760 #if !(defined(USE_NCURSES) || defined(USE_NCURSESW)) /* don't do malloc in a signal handler */
761 low_level_change_screen_size ();
762 #endif
763 winch_flag = 1;
766 void
767 change_screen_size (void)
769 winch_flag = 0;
770 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
771 #if defined TIOCGWINSZ
773 #ifndef NCURSES_VERSION
774 tty_noraw_mode ();
775 tty_reset_screen ();
776 #endif
777 low_level_change_screen_size ();
778 #ifdef HAVE_SLANG
779 /* XSI Curses spec states that portable applications shall not invoke
780 * initscr() more than once. This kludge could be done within the scope
781 * of the specification by using endwin followed by a refresh (in fact,
782 * more than one curses implementation does this); it is guaranteed to work
783 * only with slang.
785 SLsmg_init_smg ();
786 do_enter_ca_mode ();
787 tty_keypad (TRUE);
788 tty_nodelay (FALSE);
789 #endif
791 /* Inform all suspending dialogs */
792 dialog_switch_got_winch ();
793 /* Inform all running dialogs */
794 g_list_foreach (top_dlg, (GFunc) dlg_resize_cb, NULL);
796 /* Now, force the redraw */
797 repaint_screen ();
798 #endif /* TIOCGWINSZ */
799 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
802 static int ok_to_refresh = 1;
804 void
805 use_dash (int flag)
807 if (flag)
808 ok_to_refresh++;
809 else
810 ok_to_refresh--;
813 void
814 set_hintbar (const char *str)
816 label_set_text (the_hint, str);
817 if (ok_to_refresh > 0)
818 mc_refresh ();
821 void
822 print_vfs_message (const char *msg, ...)
824 va_list ap;
825 char str[128];
827 va_start (ap, msg);
828 g_vsnprintf (str, sizeof (str), msg, ap);
829 va_end (ap);
831 if (midnight_shutdown)
832 return;
834 if (!message_visible || !the_hint || !the_hint->widget.owner)
836 int col, row;
838 if (!nice_rotating_dash || (ok_to_refresh <= 0))
839 return;
841 /* Preserve current cursor position */
842 tty_getyx (&row, &col);
844 tty_gotoyx (0, 0);
845 tty_setcolor (NORMAL_COLOR);
846 tty_print_string (str_fit_to_term (str, COLS - 1, J_LEFT));
848 /* Restore cursor position */
849 tty_gotoyx (row, col);
850 mc_refresh ();
851 return;
854 if (message_visible)
855 set_hintbar (str);
858 void
859 rotate_dash (void)
861 static const char rotating_dash[] = "|/-\\";
862 static size_t pos = 0;
864 if (!nice_rotating_dash || (ok_to_refresh <= 0))
865 return;
867 if (pos >= sizeof (rotating_dash) - 1)
868 pos = 0;
869 tty_gotoyx (0, COLS - 1);
870 tty_setcolor (NORMAL_COLOR);
871 tty_print_char (rotating_dash[pos]);
872 mc_refresh ();
873 pos++;
876 const char *
877 get_nth_panel_name (int num)
879 static char buffer[BUF_SMALL];
881 if (!num)
882 return "New Left Panel";
883 else if (num == 1)
884 return "New Right Panel";
885 else
887 g_snprintf (buffer, sizeof (buffer), "%ith Panel", num);
888 return buffer;
892 /* I wonder if I should start to use the folding mode than Dugan uses */
893 /* */
894 /* This is the centralized managing of the panel display types */
895 /* This routine takes care of destroying and creating new widgets */
896 /* Please note that it could manage MAX_VIEWS, not just left and right */
897 /* Currently nothing in the code takes advantage of this and has hard- */
898 /* coded values for two panels only */
900 /* Set the num-th panel to the view type: type */
901 /* This routine also keeps at least one WPanel object in the screen */
902 /* since a lot of routines depend on the current_panel variable */
903 void
904 set_display_type (int num, panel_view_mode_t type)
906 int x = 0, y = 0, cols = 0, lines = 0;
907 int the_other = 0; /* Index to the other panel */
908 const char *file_name = NULL; /* For Quick view */
909 Widget *new_widget = NULL, *old_widget = NULL;
910 WPanel *the_other_panel = NULL;
912 if (num >= MAX_VIEWS)
914 fprintf (stderr, "Cannot allocate more that %d views\n", MAX_VIEWS);
915 abort ();
917 /* Check that we will have a WPanel * at least */
918 if (type != view_listing)
920 the_other = num == 0 ? 1 : 0;
922 if (panels[the_other].type != view_listing)
923 return;
926 /* Get rid of it */
927 if (panels[num].widget)
929 Widget *w = panels[num].widget;
930 WPanel *panel = (WPanel *) panels[num].widget;
932 x = w->x;
933 y = w->y;
934 cols = w->cols;
935 lines = w->lines;
936 old_widget = panels[num].widget;
938 if (panels[num].type == view_listing)
940 if (panel->frame_size == frame_full && type != view_listing)
942 cols = COLS - first_panel_size;
943 if (num == 1)
944 x = first_panel_size;
949 /* Restoring saved path from panels.ini for nonlist panel */
950 /* when it's first creation (for example view_info) */
951 if (old_widget == NULL && type != view_listing)
953 char panel_dir[MC_MAXPATHLEN];
954 mc_get_current_wd (panel_dir, sizeof (panel_dir));
955 panels[num].last_saved_dir = g_strdup (panel_dir);
958 switch (type)
960 case view_nothing:
961 case view_listing:
962 new_widget = restore_into_right_dir_panel (num, old_widget);
963 widget_set_size (new_widget, y, x, lines, cols);
964 break;
966 case view_info:
967 new_widget = (Widget *) info_new (y, x, lines, cols);
968 break;
970 case view_tree:
971 new_widget = (Widget *) tree_new (y, x, lines, cols, TRUE);
972 break;
974 case view_quick:
975 new_widget = (Widget *) mcview_new (y, x, lines, cols, TRUE);
976 the_other_panel = (WPanel *) panels [the_other].widget;
977 if (the_other_panel != NULL)
978 file_name = the_other_panel->dir.list[the_other_panel->selected].fname;
979 else
980 file_name = "";
982 mcview_load ((struct mcview_struct *) new_widget, 0, file_name, 0);
983 break;
986 if (type != view_listing)
987 /* Must save dir, for restoring after change type to */
988 /* view_listing */
989 save_panel_dir (num);
991 panels[num].type = type;
992 panels[num].widget = new_widget;
994 /* We use replace to keep the circular list of the dialog in the */
995 /* same state. Maybe we could just kill it and then replace it */
996 if ((midnight_dlg != NULL) && (old_widget != NULL))
997 dlg_replace_widget (old_widget, panels[num].widget);
999 if (type == view_listing)
1001 if (num == 0)
1002 left_panel = (WPanel *) new_widget;
1003 else
1004 right_panel = (WPanel *) new_widget;
1007 if (type == view_tree)
1008 the_tree = (WTree *) new_widget;
1010 /* Prevent current_panel's value from becoming invalid.
1011 * It's just a quick hack to prevent segfaults. Comment out and
1012 * try following:
1013 * - select left panel
1014 * - invoke menue left/tree
1015 * - as long as you stay in the left panel almost everything that uses
1016 * current_panel causes segfault, e.g. C-Enter, C-x c, ...
1019 if ((type != view_listing) && (current_panel == (WPanel *) old_widget))
1020 current_panel = num == 0 ? right_panel : left_panel;
1022 g_free (old_widget);
1025 /* This routine is deeply sticked to the two panels idea.
1026 What should it do in more panels. ANSWER - don't use it
1027 in any multiple panels environment. */
1028 void
1029 swap_panels (void)
1031 Widget tmp;
1032 Widget *tmp_widget;
1033 WPanel panel;
1034 WPanel *panel1, *panel2;
1035 int tmp_type;
1037 #define panelswap(x) panel. x = panel1-> x; panel1-> x = panel2-> x; panel2-> x = panel. x;
1039 #define panelswapstr(e) strcpy (panel. e, panel1-> e); \
1040 strcpy (panel1-> e, panel2-> e); \
1041 strcpy (panel2-> e, panel. e);
1042 panel1 = (WPanel *) panels[0].widget;
1043 panel2 = (WPanel *) panels[1].widget;
1044 if (panels[0].type == view_listing && panels[1].type == view_listing)
1046 /* Change everything except format/sort/panel_name etc. */
1047 panelswap (dir);
1048 panelswap (active);
1049 panelswapstr (cwd);
1050 panelswapstr (lwd);
1051 panelswap (count);
1052 panelswap (marked);
1053 panelswap (dirs_marked);
1054 panelswap (total);
1055 panelswap (top_file);
1056 panelswap (selected);
1057 panelswap (is_panelized);
1058 panelswap (dir_stat);
1060 panel1->searching = FALSE;
1061 panel2->searching = FALSE;
1062 if (current_panel == panel1)
1063 current_panel = panel2;
1064 else
1065 current_panel = panel1;
1067 if (dlg_widget_active (panels[0].widget))
1068 dlg_select_widget (panels[1].widget);
1069 else if (dlg_widget_active (panels[1].widget))
1070 dlg_select_widget (panels[0].widget);
1072 else
1074 WPanel *tmp_panel;
1076 tmp_panel = right_panel;
1077 right_panel = left_panel;
1078 left_panel = tmp_panel;
1080 if (panels[0].type == view_listing)
1082 if (!strcmp (panel1->panel_name, get_nth_panel_name (0)))
1084 g_free (panel1->panel_name);
1085 panel1->panel_name = g_strdup (get_nth_panel_name (1));
1088 if (panels[1].type == view_listing)
1090 if (!strcmp (panel2->panel_name, get_nth_panel_name (1)))
1092 g_free (panel2->panel_name);
1093 panel2->panel_name = g_strdup (get_nth_panel_name (0));
1097 tmp.x = panels[0].widget->x;
1098 tmp.y = panels[0].widget->y;
1099 tmp.cols = panels[0].widget->cols;
1100 tmp.lines = panels[0].widget->lines;
1102 panels[0].widget->x = panels[1].widget->x;
1103 panels[0].widget->y = panels[1].widget->y;
1104 panels[0].widget->cols = panels[1].widget->cols;
1105 panels[0].widget->lines = panels[1].widget->lines;
1107 panels[1].widget->x = tmp.x;
1108 panels[1].widget->y = tmp.y;
1109 panels[1].widget->cols = tmp.cols;
1110 panels[1].widget->lines = tmp.lines;
1112 tmp_widget = panels[0].widget;
1113 panels[0].widget = panels[1].widget;
1114 panels[1].widget = tmp_widget;
1115 tmp_type = panels[0].type;
1116 panels[0].type = panels[1].type;
1117 panels[1].type = tmp_type;
1121 panel_view_mode_t
1122 get_display_type (int idx)
1124 return panels[idx].type;
1127 struct Widget *
1128 get_panel_widget (int idx)
1130 return panels[idx].widget;
1134 get_current_index (void)
1136 if (panels[0].widget == ((Widget *) current_panel))
1137 return 0;
1138 else
1139 return 1;
1143 get_other_index (void)
1145 return !get_current_index ();
1148 struct WPanel *
1149 get_other_panel (void)
1151 return (struct WPanel *) get_panel_widget (get_other_index ());
1154 /* Returns the view type for the current panel/view */
1155 panel_view_mode_t
1156 get_current_type (void)
1158 if (panels[0].widget == (Widget *) current_panel)
1159 return panels[0].type;
1160 else
1161 return panels[1].type;
1164 /* Returns the view type of the unselected panel */
1165 panel_view_mode_t
1166 get_other_type (void)
1168 if (panels[0].widget == (Widget *) current_panel)
1169 return panels[1].type;
1170 else
1171 return panels[0].type;
1174 /* Save current list_view widget directory into panel */
1175 void
1176 save_panel_dir (int idx)
1178 panel_view_mode_t type = get_display_type (idx);
1179 Widget *widget = get_panel_widget (idx);
1181 if ((type == view_listing) && (widget != NULL))
1183 WPanel *w = (WPanel *) widget;
1184 char *widget_work_dir = w->cwd;
1186 g_free (panels[idx].last_saved_dir); /* last path no needed */
1187 /* Because path can be nonlocal */
1188 panels[idx].last_saved_dir = vfs_translate_url (widget_work_dir);
1192 /* Save current list_view widget directory into panel */
1193 Widget *
1194 restore_into_right_dir_panel (int idx, Widget * from_widget)
1196 Widget *new_widget = NULL;
1197 const char *saved_dir = panels[idx].last_saved_dir;
1198 gboolean last_was_panel = (from_widget && get_display_type (idx) != view_listing);
1199 const char *p_name = get_nth_panel_name (idx);
1201 if (last_was_panel)
1202 new_widget = (Widget *) panel_new_with_dir (p_name, saved_dir);
1203 else
1204 new_widget = (Widget *) panel_new (p_name);
1206 return new_widget;
1209 /* Return working dir, if it's view_listing - cwd,
1210 but for other types - last_saved_dir */
1211 const char *
1212 get_panel_dir_for (const WPanel * widget)
1214 int i;
1216 for (i = 0; i < MAX_VIEWS; i++)
1217 if ((WPanel *) get_panel_widget (i) == widget)
1218 break;
1220 if (i >= MAX_VIEWS)
1221 return ".";
1223 if (get_display_type (i) == view_listing)
1224 return ((WPanel *) get_panel_widget (i))->cwd;
1226 return panels[i].last_saved_dir;