Implemented file locking in hex editor.
[midnight-commander.git] / src / layout.c
blob3a74a7ef831ace0ec4ed562dc6b229f414f02996
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 Dlg_head *layout_dlg;
163 static const char *s_split_direction[2] = {
164 N_("&Vertical"),
165 N_("&Horizontal")
168 static WRadio *radio_widget;
170 static struct
172 const char *text;
173 int *variable;
174 WCheck *widget;
175 } check_options[] =
177 /* *INDENT-OFF* */
178 { N_("Show free sp&ace"), &free_space, NULL},
179 { N_("&XTerm window title"), &xterm_title, NULL},
180 { N_("H&intbar visible"), &message_visible, NULL},
181 { N_("&Keybar visible"), &keybar_visible, NULL},
182 { N_("Command &prompt"), &command_prompt, NULL},
183 { N_("Show &mini status"), &show_mini_info, NULL},
184 { N_("Menu&bar visible"), &menubar_visible, NULL},
185 { N_("&Equal split"), &equal_split, NULL}
186 /* *INDENT-ON* */
189 #define LAYOUT_OPTIONS_COUNT (sizeof (check_options) / sizeof (check_options[0]))
190 #define OTHER_OPTIONS_COUNT (LAYOUT_OPTIONS_COUNT - 1)
192 static gsize first_width;
193 static const char *output_lines_label = 0;
194 static int output_lines_label_len;
196 static WButton *bleft_widget, *bright_widget;
198 static void
199 _check_split (void)
201 if (_horizontal_split)
203 if (_equal_split)
204 _first_panel_size = height / 2;
205 else if (_first_panel_size < MINHEIGHT)
206 _first_panel_size = MINHEIGHT;
207 else if (_first_panel_size > height - MINHEIGHT)
208 _first_panel_size = height - MINHEIGHT;
210 else
212 if (_equal_split)
213 _first_panel_size = COLS / 2;
214 else if (_first_panel_size < MINWIDTH)
215 _first_panel_size = MINWIDTH;
216 else if (_first_panel_size > COLS - MINWIDTH)
217 _first_panel_size = COLS - MINWIDTH;
221 static void
222 update_split (void)
224 /* Check split has to be done before testing if it changed, since
225 it can change due to calling _check_split() as well */
226 _check_split ();
228 /* To avoid setting the cursor to the wrong place */
229 if ((old_first_panel_size == _first_panel_size) && (old_horizontal_split == _horizontal_split))
230 return;
232 old_first_panel_size = _first_panel_size;
233 old_horizontal_split = _horizontal_split;
235 tty_setcolor (COLOR_NORMAL);
236 dlg_move (layout_dlg, 6, 6);
237 tty_printf ("%03d", _first_panel_size);
238 dlg_move (layout_dlg, 6, 18);
239 if (_horizontal_split)
240 tty_printf ("%03d", height - _first_panel_size);
241 else
242 tty_printf ("%03d", COLS - _first_panel_size);
245 static int
246 b2left_cback (int action)
248 (void) action;
250 if (_equal_split)
252 /* Turn equal split off */
253 _equal_split = 0;
254 check_options[7].widget->state = check_options[7].widget->state & ~C_BOOL;
255 dlg_select_widget (check_options[7].widget);
256 dlg_select_widget (bleft_widget);
258 _first_panel_size++;
259 return 0;
262 static int
263 b2right_cback (int action)
265 (void) action;
267 if (_equal_split)
269 /* Turn equal split off */
270 _equal_split = 0;
271 check_options[7].widget->state = check_options[7].widget->state & ~C_BOOL;
272 dlg_select_widget (check_options[7].widget);
273 dlg_select_widget (bright_widget);
275 _first_panel_size--;
276 return 0;
279 static int
280 bplus_cback (int action)
282 (void) action;
284 if (_output_lines < 99)
285 _output_lines++;
286 return 0;
289 static int
290 bminus_cback (int action)
292 (void) action;
294 if (_output_lines > 0)
295 _output_lines--;
296 return 0;
299 static cb_ret_t
300 layout_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
302 switch (msg)
304 case DLG_DRAW:
305 /*When repainting the whole dialog (e.g. with C-l) we have to
306 update everything */
307 common_dialog_repaint (h);
309 old_first_panel_size = -1;
310 old_horizontal_split = -1;
311 old_output_lines = -1;
313 tty_setcolor (COLOR_HOT_NORMAL);
314 update_split ();
315 dlg_move (h, 6, 13);
316 tty_print_char ('=');
317 if (console_flag)
319 if (old_output_lines != _output_lines)
321 old_output_lines = _output_lines;
322 tty_setcolor (COLOR_NORMAL);
323 dlg_move (h, 9, 6);
324 tty_print_string (output_lines_label);
325 dlg_move (h, 9, 6 + 3 + output_lines_label_len);
326 tty_printf ("%02d", _output_lines);
329 return MSG_HANDLED;
331 case DLG_POST_KEY:
332 _equal_split = check_options[7].widget->state & C_BOOL;
333 _menubar_visible = check_options[6].widget->state & C_BOOL;
334 _command_prompt = check_options[5].widget->state & C_BOOL;
335 _keybar_visible = check_options[3].widget->state & C_BOOL;
336 _message_visible = check_options[2].widget->state & C_BOOL;
337 _xterm_title = check_options[1].widget->state & C_BOOL;
338 _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
355 height = LINES - _keybar_visible - _command_prompt -
356 _menubar_visible - _output_lines - _message_visible;
358 if (_horizontal_split != radio_widget->sel)
360 _horizontal_split = radio_widget->sel;
361 if (_horizontal_split)
362 _first_panel_size = height / 2;
363 else
364 _first_panel_size = COLS / 2;
366 update_split ();
367 if (console_flag)
369 if (old_output_lines != _output_lines)
371 old_output_lines = _output_lines;
372 tty_setcolor (COLOR_NORMAL);
373 dlg_move (h, 9, 6 + 3 + output_lines_label_len);
374 tty_printf ("%02d", _output_lines);
377 return MSG_HANDLED;
379 default:
380 return default_dlg_callback (h, sender, msg, parm, data);
384 static void
385 init_layout (void)
387 static int i18n_layt_flag = 0;
388 static int b1, b2, b3;
389 size_t i = sizeof (s_split_direction) / sizeof (char *);
390 const char *ok_button = _("&OK");
391 const char *cancel_button = _("&Cancel");
392 const char *save_button = _("&Save");
393 static const char *title1, *title2, *title3;
395 if (!i18n_layt_flag)
397 gsize l1;
399 first_width = 0;
401 title1 = _("Panel split");
402 title2 = _("Console output");
403 title3 = _("Other options");
404 output_lines_label = _("Output lines:");
406 while (i--)
408 s_split_direction[i] = _(s_split_direction[i]);
409 l1 = str_term_width1 (s_split_direction[i]) + 7;
410 if (l1 > first_width)
411 first_width = l1;
414 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
416 check_options[i].text = _(check_options[i].text);
417 l1 = str_term_width1 (check_options[i].text) + 7;
418 if (l1 > first_width)
419 first_width = l1;
422 l1 = str_term_width1 (title1) + 1;
423 if (l1 > first_width)
424 first_width = l1;
426 l1 = str_term_width1 (title2) + 1;
427 if (l1 > first_width)
428 first_width = l1;
430 if (console_flag)
432 output_lines_label_len = str_term_width1 (output_lines_label);
433 l1 = output_lines_label_len + 12;
434 if (l1 > first_width)
435 first_width = l1;
439 * alex@bcs.zp.ua:
440 * To be completely correct, one need to check if the title
441 * does not exceed dialog length and total length of 3 buttons
442 * allows their placement in one row. But assuming this dialog
443 * is wide enough, I don't include such a tests.
445 * Now the last thing to do - properly space buttons...
447 l1 = 11 + str_term_width1 (ok_button) /* 14 - all brackets and inner space */
448 + str_term_width1 (save_button) /* notice: it is 3 char less because */
449 + str_term_width1 (cancel_button); /* of '&' char in button text */
451 i = (first_width * 2 - l1) / 4;
452 b1 = 5 + i;
453 b2 = b1 + str_term_width1 (ok_button) + i + 6;
454 b3 = b2 + str_term_width1 (save_button) + i + 4;
456 i18n_layt_flag = 1;
459 layout_dlg =
460 create_dlg (TRUE, 0, 0, 14, first_width * 2 + 9,
461 dialog_colors, layout_callback, "[Layout]",
462 _("Layout"), DLG_CENTER | DLG_REVERSE);
464 add_widget (layout_dlg, groupbox_new (2, 4, 6, first_width, title1));
466 add_widget (layout_dlg, groupbox_new (2, 5 + first_width, 9, first_width, title3));
468 add_widget (layout_dlg, button_new (11, b3, B_CANCEL, NORMAL_BUTTON, cancel_button, 0));
469 add_widget (layout_dlg, button_new (11, b2, B_EXIT, NORMAL_BUTTON, save_button, 0));
470 add_widget (layout_dlg, button_new (11, b1, B_ENTER, DEFPUSH_BUTTON, ok_button, 0));
471 #define XTRACT(i) *check_options[i].variable, check_options[i].text
473 for (i = 0; i < (size_t) OTHER_OPTIONS_COUNT; i++)
475 check_options[i].widget =
476 check_new (OTHER_OPTIONS_COUNT - i + 2, 7 + first_width, XTRACT (i));
477 add_widget (layout_dlg, check_options[i].widget);
480 _equal_split = equal_split;
481 _menubar_visible = menubar_visible;
482 _command_prompt = command_prompt;
483 _keybar_visible = keybar_visible;
484 _message_visible = message_visible;
485 _xterm_title = xterm_title;
486 _free_space = free_space;
488 if (console_flag)
490 add_widget (layout_dlg, groupbox_new (8, 4, 3, first_width, title2));
492 add_widget (layout_dlg,
493 button_new (9, output_lines_label_len + 6 + 5, B_MINUS,
494 NARROW_BUTTON, "&-", bminus_cback));
495 add_widget (layout_dlg,
496 button_new (9, output_lines_label_len + 6, B_PLUS,
497 NARROW_BUTTON, "&+", bplus_cback));
500 bright_widget = button_new (6, 15, B_2RIGHT, NARROW_BUTTON, "&>", b2right_cback);
501 add_widget (layout_dlg, bright_widget);
502 bleft_widget = button_new (6, 9, B_2LEFT, NARROW_BUTTON, "&<", b2left_cback);
503 add_widget (layout_dlg, bleft_widget);
504 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;
513 add_widget (layout_dlg, check_options[7].widget);
514 radio_widget = radio_new (3, 6, 2, s_split_direction);
515 add_widget (layout_dlg, radio_widget);
516 radio_widget->sel = horizontal_split;
519 void
520 layout_change (void)
522 setup_panels ();
523 /* re-init the menu, because perhaps there was a change in the way
524 how the panel are split (horizontal/vertical). */
525 done_menu ();
526 init_menu ();
527 menubar_arrange (the_menubar);
528 load_hint (1);
531 void
532 layout_box (void)
534 int result;
535 size_t i;
536 int layout_do_change = 0;
538 init_layout ();
539 run_dlg (layout_dlg);
540 result = layout_dlg->ret_value;
542 if (result == B_ENTER || result == B_EXIT)
544 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
545 if (check_options[i].widget != NULL)
546 *check_options[i].variable = check_options[i].widget->state & C_BOOL;
547 horizontal_split = radio_widget->sel;
548 first_panel_size = _first_panel_size;
549 output_lines = _output_lines;
550 layout_do_change = 1;
552 if (result == B_EXIT)
554 save_layout ();
555 mc_config_save_file (mc_main_config, NULL);
558 destroy_dlg (layout_dlg);
559 if (layout_do_change)
560 layout_change ();
563 static void
564 check_split (void)
566 if (horizontal_split)
568 if (equal_split)
569 first_panel_size = height / 2;
570 else if (first_panel_size < MINHEIGHT)
571 first_panel_size = MINHEIGHT;
572 else if (first_panel_size > height - MINHEIGHT)
573 first_panel_size = height - MINHEIGHT;
575 else
577 if (equal_split)
578 first_panel_size = COLS / 2;
579 else if (first_panel_size < MINWIDTH)
580 first_panel_size = MINWIDTH;
581 else if (first_panel_size > COLS - MINWIDTH)
582 first_panel_size = COLS - MINWIDTH;
586 void
587 clr_scr (void)
589 tty_set_normal_attrs ();
590 tty_fill_region (0, 0, LINES, COLS, ' ');
591 tty_refresh ();
594 void
595 repaint_screen (void)
597 do_refresh ();
598 tty_refresh ();
601 void
602 mc_refresh (void)
604 #ifdef WITH_BACKGROUND
605 if (we_are_background)
606 return;
607 #endif /* WITH_BACKGROUND */
608 if (winch_flag == 0)
609 tty_refresh ();
610 else
612 /* if winch was caugth, we should do not only redraw screen, but
613 reposition/resize all */
614 change_screen_size ();
618 static void
619 panel_do_cols (int idx)
621 if (get_display_type (idx) == view_listing)
622 set_panel_formats ((WPanel *) panels[idx].widget);
623 else
624 panel_update_cols (panels[idx].widget, frame_half);
627 void
628 setup_panels (void)
630 int start_y;
631 int promptl; /* the prompt len */
633 if (console_flag)
635 int minimum;
636 if (output_lines < 0)
637 output_lines = 0;
638 height =
639 LINES - keybar_visible - command_prompt - menubar_visible -
640 output_lines - message_visible;
641 minimum = MINHEIGHT * (1 + horizontal_split);
642 if (height < minimum)
644 output_lines -= minimum - height;
645 height = minimum;
648 else
650 height = LINES - menubar_visible - command_prompt - keybar_visible - message_visible;
652 check_split ();
653 start_y = menubar_visible;
655 /* The column computing is defered until panel_do_cols */
656 if (horizontal_split)
658 widget_set_size (panels[0].widget, start_y, 0, first_panel_size, 0);
660 widget_set_size (panels[1].widget, start_y + first_panel_size, 0,
661 height - first_panel_size, 0);
663 else
665 int first_x = first_panel_size;
667 widget_set_size (panels[0].widget, start_y, 0, height, 0);
669 widget_set_size (panels[1].widget, start_y, first_x, height, 0);
672 panel_do_cols (0);
673 panel_do_cols (1);
675 promptl = str_term_width1 (mc_prompt);
677 widget_set_size (&the_menubar->widget, 0, 0, 1, COLS);
679 if (command_prompt)
681 widget_set_size (&cmdline->widget, LINES - 1 - keybar_visible, promptl, 1, COLS - promptl);
682 winput_set_origin (cmdline, promptl, COLS - promptl);
683 widget_set_size (&the_prompt->widget, LINES - 1 - keybar_visible, 0, 1, promptl);
685 else
687 widget_set_size (&cmdline->widget, 0, 0, 0, 0);
688 winput_set_origin (cmdline, 0, 0);
689 widget_set_size (&the_prompt->widget, LINES, COLS, 0, 0);
692 widget_set_size (&the_bar->widget, LINES - 1, 0, keybar_visible, COLS);
693 buttonbar_set_visible (the_bar, keybar_visible);
695 /* Output window */
696 if (console_flag && output_lines)
698 output_start_y = LINES - command_prompt - keybar_visible - output_lines;
699 show_console_contents (output_start_y,
700 LINES - output_lines - keybar_visible - 1,
701 LINES - keybar_visible - 1);
703 if (message_visible)
704 widget_set_size (&the_hint->widget, height + start_y, 0, 1, COLS);
705 else
706 widget_set_size (&the_hint->widget, 0, 0, 0, 0);
708 update_xterm_title_path ();
711 static inline void
712 low_level_change_screen_size (void)
714 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
715 #if defined TIOCGWINSZ
716 struct winsize winsz;
718 winsz.ws_col = winsz.ws_row = 0;
719 /* Ioctl on the STDIN_FILENO */
720 ioctl (0, TIOCGWINSZ, &winsz);
721 if (winsz.ws_col && winsz.ws_row)
723 #if defined(NCURSES_VERSION) && defined(HAVE_RESIZETERM)
724 resizeterm (winsz.ws_row, winsz.ws_col);
725 clearok (stdscr, TRUE); /* sigwinch's should use a semaphore! */
726 #else
727 COLS = winsz.ws_col;
728 LINES = winsz.ws_row;
729 #endif
730 #ifdef HAVE_SUBSHELL_SUPPORT
731 resize_subshell ();
732 #endif
734 #endif /* TIOCGWINSZ */
735 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
738 static void
739 dlg_resize_cb (void *data, void *user_data)
741 Dlg_head *d = data;
743 (void) user_data;
744 d->callback (d, NULL, DLG_RESIZE, 0, NULL);
747 void
748 sigwinch_handler (int dummy)
750 (void) dummy;
751 #if !(defined(USE_NCURSES) || defined(USE_NCURSESW)) /* don't do malloc in a signal handler */
752 low_level_change_screen_size ();
753 #endif
754 winch_flag = 1;
757 void
758 change_screen_size (void)
760 winch_flag = 0;
761 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
762 #if defined TIOCGWINSZ
764 #ifndef NCURSES_VERSION
765 tty_noraw_mode ();
766 tty_reset_screen ();
767 #endif
768 low_level_change_screen_size ();
769 #ifdef HAVE_SLANG
770 /* XSI Curses spec states that portable applications shall not invoke
771 * initscr() more than once. This kludge could be done within the scope
772 * of the specification by using endwin followed by a refresh (in fact,
773 * more than one curses implementation does this); it is guaranteed to work
774 * only with slang.
776 SLsmg_init_smg ();
777 do_enter_ca_mode ();
778 tty_keypad (TRUE);
779 tty_nodelay (FALSE);
780 #endif
782 /* Inform all suspending dialogs */
783 dialog_switch_got_winch ();
784 /* Inform all running dialogs */
785 g_list_foreach (top_dlg, (GFunc) dlg_resize_cb, NULL);
787 /* Now, force the redraw */
788 repaint_screen ();
789 #endif /* TIOCGWINSZ */
790 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
793 static int ok_to_refresh = 1;
795 void
796 use_dash (int flag)
798 if (flag)
799 ok_to_refresh++;
800 else
801 ok_to_refresh--;
804 void
805 set_hintbar (const char *str)
807 label_set_text (the_hint, str);
808 if (ok_to_refresh > 0)
809 mc_refresh ();
812 void
813 print_vfs_message (const char *msg, ...)
815 va_list ap;
816 char str[128];
818 va_start (ap, msg);
819 g_vsnprintf (str, sizeof (str), msg, ap);
820 va_end (ap);
822 if (midnight_shutdown)
823 return;
825 if (!message_visible || !the_hint || !the_hint->widget.owner)
827 int col, row;
829 if (!nice_rotating_dash || (ok_to_refresh <= 0))
830 return;
832 /* Preserve current cursor position */
833 tty_getyx (&row, &col);
835 tty_gotoyx (0, 0);
836 tty_setcolor (NORMAL_COLOR);
837 tty_print_string (str_fit_to_term (str, COLS - 1, J_LEFT));
839 /* Restore cursor position */
840 tty_gotoyx (row, col);
841 mc_refresh ();
842 return;
845 if (message_visible)
846 set_hintbar (str);
849 void
850 rotate_dash (void)
852 static const char rotating_dash[] = "|/-\\";
853 static size_t pos = 0;
855 if (!nice_rotating_dash || (ok_to_refresh <= 0))
856 return;
858 if (pos >= sizeof (rotating_dash) - 1)
859 pos = 0;
860 tty_gotoyx (0, COLS - 1);
861 tty_setcolor (NORMAL_COLOR);
862 tty_print_char (rotating_dash[pos]);
863 mc_refresh ();
864 pos++;
867 const char *
868 get_nth_panel_name (int num)
870 static char buffer[BUF_SMALL];
872 if (!num)
873 return "New Left Panel";
874 else if (num == 1)
875 return "New Right Panel";
876 else
878 g_snprintf (buffer, sizeof (buffer), "%ith Panel", num);
879 return buffer;
883 /* I wonder if I should start to use the folding mode than Dugan uses */
884 /* */
885 /* This is the centralized managing of the panel display types */
886 /* This routine takes care of destroying and creating new widgets */
887 /* Please note that it could manage MAX_VIEWS, not just left and right */
888 /* Currently nothing in the code takes advantage of this and has hard- */
889 /* coded values for two panels only */
891 /* Set the num-th panel to the view type: type */
892 /* This routine also keeps at least one WPanel object in the screen */
893 /* since a lot of routines depend on the current_panel variable */
894 void
895 set_display_type (int num, panel_view_mode_t type)
897 int x = 0, y = 0, cols = 0, lines = 0;
898 int the_other = 0; /* Index to the other panel */
899 const char *file_name = NULL; /* For Quick view */
900 Widget *new_widget = NULL, *old_widget = NULL;
901 WPanel *the_other_panel = NULL;
903 if (num >= MAX_VIEWS)
905 fprintf (stderr, "Cannot allocate more that %d views\n", MAX_VIEWS);
906 abort ();
908 /* Check that we will have a WPanel * at least */
909 if (type != view_listing)
911 the_other = num == 0 ? 1 : 0;
913 if (panels[the_other].type != view_listing)
914 return;
917 /* Get rid of it */
918 if (panels[num].widget)
920 Widget *w = panels[num].widget;
921 WPanel *panel = (WPanel *) panels[num].widget;
923 x = w->x;
924 y = w->y;
925 cols = w->cols;
926 lines = w->lines;
927 old_widget = panels[num].widget;
929 if (panels[num].type == view_listing)
931 if (panel->frame_size == frame_full && type != view_listing)
933 cols = COLS - first_panel_size;
934 if (num == 1)
935 x = first_panel_size;
940 /* Restoring saved path from panels.ini for nonlist panel */
941 /* when it's first creation (for example view_info) */
942 if (old_widget == NULL && type != view_listing)
944 char panel_dir[MC_MAXPATHLEN];
945 mc_get_current_wd (panel_dir, sizeof (panel_dir));
946 panels[num].last_saved_dir = g_strdup (panel_dir);
949 switch (type)
951 case view_nothing:
952 case view_listing:
953 new_widget = restore_into_right_dir_panel (num, old_widget);
954 widget_set_size (new_widget, y, x, lines, cols);
955 break;
957 case view_info:
958 new_widget = (Widget *) info_new (y, x, lines, cols);
959 break;
961 case view_tree:
962 new_widget = (Widget *) tree_new (y, x, lines, cols, TRUE);
963 break;
965 case view_quick:
966 new_widget = (Widget *) mcview_new (y, x, lines, cols, TRUE);
967 the_other_panel = (WPanel *) panels [the_other].widget;
968 if (the_other_panel != NULL)
969 file_name = the_other_panel->dir.list[the_other_panel->selected].fname;
970 else
971 file_name = "";
973 mcview_load ((struct mcview_struct *) new_widget, 0, file_name, 0);
974 break;
977 if (type != view_listing)
978 /* Must save dir, for restoring after change type to */
979 /* view_listing */
980 save_panel_dir (num);
982 panels[num].type = type;
983 panels[num].widget = new_widget;
985 /* We use replace to keep the circular list of the dialog in the */
986 /* same state. Maybe we could just kill it and then replace it */
987 if ((midnight_dlg != NULL) && (old_widget != NULL))
988 dlg_replace_widget (old_widget, panels[num].widget);
990 if (type == view_listing)
992 if (num == 0)
993 left_panel = (WPanel *) new_widget;
994 else
995 right_panel = (WPanel *) new_widget;
998 if (type == view_tree)
999 the_tree = (WTree *) new_widget;
1001 /* Prevent current_panel's value from becoming invalid.
1002 * It's just a quick hack to prevent segfaults. Comment out and
1003 * try following:
1004 * - select left panel
1005 * - invoke menue left/tree
1006 * - as long as you stay in the left panel almost everything that uses
1007 * current_panel causes segfault, e.g. C-Enter, C-x c, ...
1010 if ((type != view_listing) && (current_panel == (WPanel *) old_widget))
1011 current_panel = num == 0 ? right_panel : left_panel;
1013 g_free (old_widget);
1016 /* This routine is deeply sticked to the two panels idea.
1017 What should it do in more panels. ANSWER - don't use it
1018 in any multiple panels environment. */
1019 void
1020 swap_panels (void)
1022 Widget tmp;
1023 Widget *tmp_widget;
1024 WPanel panel;
1025 WPanel *panel1, *panel2;
1026 int tmp_type;
1028 #define panelswap(x) panel. x = panel1-> x; panel1-> x = panel2-> x; panel2-> x = panel. x;
1030 #define panelswapstr(e) strcpy (panel. e, panel1-> e); \
1031 strcpy (panel1-> e, panel2-> e); \
1032 strcpy (panel2-> e, panel. e);
1033 panel1 = (WPanel *) panels[0].widget;
1034 panel2 = (WPanel *) panels[1].widget;
1035 if (panels[0].type == view_listing && panels[1].type == view_listing)
1037 /* Change everything except format/sort/panel_name etc. */
1038 panelswap (dir);
1039 panelswap (active);
1040 panelswapstr (cwd);
1041 panelswapstr (lwd);
1042 panelswap (count);
1043 panelswap (marked);
1044 panelswap (dirs_marked);
1045 panelswap (total);
1046 panelswap (top_file);
1047 panelswap (selected);
1048 panelswap (is_panelized);
1049 panelswap (dir_stat);
1051 panel1->searching = FALSE;
1052 panel2->searching = FALSE;
1053 if (current_panel == panel1)
1054 current_panel = panel2;
1055 else
1056 current_panel = panel1;
1058 if (dlg_widget_active (panels[0].widget))
1059 dlg_select_widget (panels[1].widget);
1060 else if (dlg_widget_active (panels[1].widget))
1061 dlg_select_widget (panels[0].widget);
1063 else
1065 WPanel *tmp_panel;
1067 tmp_panel = right_panel;
1068 right_panel = left_panel;
1069 left_panel = tmp_panel;
1071 if (panels[0].type == view_listing)
1073 if (!strcmp (panel1->panel_name, get_nth_panel_name (0)))
1075 g_free (panel1->panel_name);
1076 panel1->panel_name = g_strdup (get_nth_panel_name (1));
1079 if (panels[1].type == view_listing)
1081 if (!strcmp (panel2->panel_name, get_nth_panel_name (1)))
1083 g_free (panel2->panel_name);
1084 panel2->panel_name = g_strdup (get_nth_panel_name (0));
1088 tmp.x = panels[0].widget->x;
1089 tmp.y = panels[0].widget->y;
1090 tmp.cols = panels[0].widget->cols;
1091 tmp.lines = panels[0].widget->lines;
1093 panels[0].widget->x = panels[1].widget->x;
1094 panels[0].widget->y = panels[1].widget->y;
1095 panels[0].widget->cols = panels[1].widget->cols;
1096 panels[0].widget->lines = panels[1].widget->lines;
1098 panels[1].widget->x = tmp.x;
1099 panels[1].widget->y = tmp.y;
1100 panels[1].widget->cols = tmp.cols;
1101 panels[1].widget->lines = tmp.lines;
1103 tmp_widget = panels[0].widget;
1104 panels[0].widget = panels[1].widget;
1105 panels[1].widget = tmp_widget;
1106 tmp_type = panels[0].type;
1107 panels[0].type = panels[1].type;
1108 panels[1].type = tmp_type;
1112 panel_view_mode_t
1113 get_display_type (int idx)
1115 return panels[idx].type;
1118 struct Widget *
1119 get_panel_widget (int idx)
1121 return panels[idx].widget;
1125 get_current_index (void)
1127 if (panels[0].widget == ((Widget *) current_panel))
1128 return 0;
1129 else
1130 return 1;
1134 get_other_index (void)
1136 return !get_current_index ();
1139 struct WPanel *
1140 get_other_panel (void)
1142 return (struct WPanel *) get_panel_widget (get_other_index ());
1145 /* Returns the view type for the current panel/view */
1146 panel_view_mode_t
1147 get_current_type (void)
1149 if (panels[0].widget == (Widget *) current_panel)
1150 return panels[0].type;
1151 else
1152 return panels[1].type;
1155 /* Returns the view type of the unselected panel */
1156 panel_view_mode_t
1157 get_other_type (void)
1159 if (panels[0].widget == (Widget *) current_panel)
1160 return panels[1].type;
1161 else
1162 return panels[0].type;
1165 /* Save current list_view widget directory into panel */
1166 void
1167 save_panel_dir (int idx)
1169 panel_view_mode_t type = get_display_type (idx);
1170 Widget *widget = get_panel_widget (idx);
1172 if ((type == view_listing) && (widget != NULL))
1174 WPanel *w = (WPanel *) widget;
1175 char *widget_work_dir = w->cwd;
1177 g_free (panels[idx].last_saved_dir); /* last path no needed */
1178 /* Because path can be nonlocal */
1179 panels[idx].last_saved_dir = vfs_translate_url (widget_work_dir);
1183 /* Save current list_view widget directory into panel */
1184 Widget *
1185 restore_into_right_dir_panel (int idx, Widget * from_widget)
1187 Widget *new_widget = NULL;
1188 const char *saved_dir = panels[idx].last_saved_dir;
1189 gboolean last_was_panel = (from_widget && get_display_type (idx) != view_listing);
1190 const char *p_name = get_nth_panel_name (idx);
1192 if (last_was_panel)
1193 new_widget = (Widget *) panel_new_with_dir (p_name, saved_dir);
1194 else
1195 new_widget = (Widget *) panel_new (p_name);
1197 return new_widget;
1200 /* Return working dir, if it's view_listing - cwd,
1201 but for other types - last_saved_dir */
1202 const char *
1203 get_panel_dir_for (const WPanel * widget)
1205 int i;
1207 for (i = 0; i < MAX_VIEWS; i++)
1208 if ((WPanel *) get_panel_widget (i) == widget)
1209 break;
1211 if (i >= MAX_VIEWS)
1212 return ".";
1214 if (get_display_type (i) == view_listing)
1215 return ((WPanel *) get_panel_widget (i))->cwd;
1217 return panels[i].last_saved_dir;