Reorder VFS parser to work with VFS parameters:
[midnight-commander.git] / src / filemanager / layout.c
blobce9b2579d468055e36c66e4809dda8c96f625b8a
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 <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <unistd.h>
35 #include "lib/global.h"
36 #include "lib/tty/tty.h"
37 #include "lib/skin.h"
38 #include "lib/tty/key.h"
39 #include "lib/tty/mouse.h"
40 #include "lib/mcconfig.h"
41 #include "lib/vfs/vfs.h" /* For mc_get_current_wd() */
42 #include "lib/strutil.h"
43 #include "lib/widget.h"
44 #include "lib/event.h"
46 #include "src/consaver/cons.saver.h"
47 #include "src/viewer/mcviewer.h" /* The view widget */
48 #include "src/setup.h"
49 #include "src/background.h"
51 #include "command.h"
52 #include "midnight.h"
53 #include "tree.h"
54 /* Needed for the extern declarations of integer parameters */
55 #include "dir.h"
56 #include "layout.h"
57 #include "info.h" /* The Info widget */
60 /*** global variables ****************************************************************************/
62 /* Controls the display of the rotating dash on the verbose mode */
63 int nice_rotating_dash = 1;
65 /* Set if the panels are split horizontally */
66 int horizontal_split = 0;
68 /* Set if the split is the same */
69 int equal_split = 1;
71 /* First panel size if the panel are not split equally */
72 int first_panel_size = 0;
74 /* The number of output lines shown (if available) */
75 int output_lines = 0;
77 /* Set if the command prompt is to be displayed */
78 int command_prompt = 1;
80 /* Set if the main menu is visible */
81 int menubar_visible = 1;
83 /* Set to show current working dir in xterm window title */
84 int xterm_title = 1;
86 /* Set to show free space on device assigned to current directory */
87 int free_space = 1;
89 /* The starting line for the output of the subprogram */
90 int output_start_y = 0;
92 int ok_to_refresh = 1;
94 /*** file scope macro definitions ****************************************************************/
96 /* The maximum number of views managed by the set_display_type routine */
97 /* Must be at least two (for current and other). Please note that until */
98 /* Janne gets around this, we will only manage two of them :-) */
99 #define MAX_VIEWS 2
101 /* Width 12 for a wee Quick (Hex) View */
102 #define MINWIDTH 12
103 #define MINHEIGHT 5
105 #define B_2LEFT B_USER
106 #define B_2RIGHT (B_USER + 1)
107 #define B_PLUS (B_USER + 2)
108 #define B_MINUS (B_USER + 3)
110 #define LAYOUT_OPTIONS_COUNT G_N_ELEMENTS (check_options)
111 #define OTHER_OPTIONS_COUNT (LAYOUT_OPTIONS_COUNT - 1)
113 /*** file scope type declarations ****************************************************************/
115 /*** file scope variables ************************************************************************/
117 static struct
119 panel_view_mode_t type;
120 Widget *widget;
121 char *last_saved_dir; /* last view_list working directory */
122 } panels[MAX_VIEWS] =
124 /* *INDENT-OFF* */
125 /* init MAX_VIEWS items */
126 { view_listing, NULL, NULL},
127 { view_listing, NULL, NULL}
128 /* *INDENT-ON* */
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;
149 static int height;
151 static WRadio *radio_widget;
153 static struct
155 const char *text;
156 int *variable;
157 WCheck *widget;
158 } check_options[] =
160 /* *INDENT-OFF* */
161 { N_("Show free sp&ace"), &free_space, NULL},
162 { N_("&XTerm window title"), &xterm_title, NULL},
163 { N_("H&intbar visible"), &mc_global.message_visible, NULL},
164 { N_("&Keybar visible"), &mc_global.keybar_visible, NULL},
165 { N_("Command &prompt"), &command_prompt, NULL},
166 { N_("Menu&bar visible"), &menubar_visible, NULL},
167 { N_("&Equal split"), &equal_split, NULL}
168 /* *INDENT-ON* */
171 static const char *output_lines_label = NULL;
172 static int output_lines_label_len;
174 static WButton *bleft_widget, *bright_widget;
176 /*** file scope functions ************************************************************************/
177 /* --------------------------------------------------------------------------------------------- */
179 /* don't use max() macro to avoid double call of str_term_width1() in widget width calculation */
180 #undef max
182 static int
183 max (int a, int b)
185 return a > b ? a : b;
188 /* --------------------------------------------------------------------------------------------- */
190 static inline void
191 _check_split (void)
193 if (_horizontal_split)
195 if (_equal_split)
196 _first_panel_size = height / 2;
197 else if (_first_panel_size < MINHEIGHT)
198 _first_panel_size = MINHEIGHT;
199 else if (_first_panel_size > height - MINHEIGHT)
200 _first_panel_size = height - MINHEIGHT;
202 else
204 if (_equal_split)
205 _first_panel_size = COLS / 2;
206 else if (_first_panel_size < MINWIDTH)
207 _first_panel_size = MINWIDTH;
208 else if (_first_panel_size > COLS - MINWIDTH)
209 _first_panel_size = COLS - MINWIDTH;
212 old_first_panel_size = _first_panel_size;
213 old_horizontal_split = _horizontal_split;
216 /* --------------------------------------------------------------------------------------------- */
218 static void
219 update_split (const Dlg_head * h)
221 /* Check split has to be done before testing if it changed, since
222 it can change due to calling _check_split() as well */
223 _check_split ();
225 tty_setcolor (check_options[6].widget->state & C_BOOL ? DISABLED_COLOR : COLOR_NORMAL);
227 dlg_move (h, 6, 5);
228 tty_printf ("%03d", _first_panel_size);
230 dlg_move (h, 6, 17);
231 if (_horizontal_split)
232 tty_printf ("%03d", height - _first_panel_size);
233 else
234 tty_printf ("%03d", COLS - _first_panel_size);
236 dlg_move (h, 6, 12);
237 tty_print_char ('=');
240 /* --------------------------------------------------------------------------------------------- */
242 static int
243 b_left_right_cback (WButton * button, int action)
245 (void) action;
247 if (button == bleft_widget)
248 _first_panel_size++;
249 else
250 _first_panel_size--;
252 update_split (button->widget.owner);
253 return 0;
256 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
271 static int
272 bminus_cback (WButton * button, int action)
274 (void) button;
275 (void) action;
277 if (_output_lines > 0)
278 _output_lines--;
279 return 0;
282 /* --------------------------------------------------------------------------------------------- */
284 static cb_ret_t
285 layout_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
287 switch (msg)
289 case DLG_DRAW:
290 /*When repainting the whole dialog (e.g. with C-l) we have to
291 update everything */
292 common_dialog_repaint (h);
294 old_first_panel_size = -1;
295 old_horizontal_split = -1;
296 old_output_lines = -1;
298 update_split (h);
300 if (old_output_lines != _output_lines)
302 old_output_lines = _output_lines;
303 tty_setcolor (mc_global.tty.console_flag ? COLOR_NORMAL : DISABLED_COLOR);
304 dlg_move (h, 9, 5);
305 tty_print_string (output_lines_label);
306 dlg_move (h, 9, 5 + 3 + output_lines_label_len);
307 tty_printf ("%02d", _output_lines);
309 return MSG_HANDLED;
311 case DLG_POST_KEY:
312 _menubar_visible = check_options[5].widget->state & C_BOOL;
313 _command_prompt = check_options[4].widget->state & C_BOOL;
314 _keybar_visible = check_options[3].widget->state & C_BOOL;
315 _message_visible = check_options[2].widget->state & C_BOOL;
316 _xterm_title = check_options[1].widget->state & C_BOOL;
317 _free_space = check_options[0].widget->state & C_BOOL;
319 if (mc_global.tty.console_flag)
321 int minimum;
322 if (_output_lines < 0)
323 _output_lines = 0;
324 height = LINES - _keybar_visible - _command_prompt -
325 _menubar_visible - _output_lines - _message_visible;
326 minimum = MINHEIGHT * (1 + _horizontal_split);
327 if (height < minimum)
329 _output_lines -= minimum - height;
330 height = minimum;
333 else
334 height = LINES - _keybar_visible - _command_prompt -
335 _menubar_visible - _output_lines - _message_visible;
337 if (old_output_lines != _output_lines)
339 old_output_lines = _output_lines;
340 tty_setcolor (mc_global.tty.console_flag ? COLOR_NORMAL : DISABLED_COLOR);
341 dlg_move (h, 9, 5 + 3 + output_lines_label_len);
342 tty_printf ("%02d", _output_lines);
344 return MSG_HANDLED;
346 case DLG_ACTION:
347 if (sender == (Widget *) radio_widget)
349 if (_horizontal_split != radio_widget->sel)
351 _horizontal_split = radio_widget->sel;
352 if (_equal_split)
354 if (_horizontal_split)
355 _first_panel_size = height / 2;
356 else
357 _first_panel_size = COLS / 2;
361 update_split (h);
363 return MSG_HANDLED;
366 if (sender == (Widget *) check_options[6].widget)
368 _equal_split = check_options[6].widget->state & C_BOOL;
370 widget_disable (bleft_widget->widget, _equal_split);
371 send_message ((Widget *) bleft_widget, WIDGET_DRAW, 0);
372 widget_disable (bright_widget->widget, _equal_split);
373 send_message ((Widget *) bright_widget, WIDGET_DRAW, 0);
375 update_split (h);
377 return MSG_HANDLED;
380 return MSG_NOT_HANDLED;
382 default:
383 return default_dlg_callback (h, sender, msg, parm, data);
387 /* --------------------------------------------------------------------------------------------- */
389 static Dlg_head *
390 init_layout (void)
392 Dlg_head *layout_dlg;
393 int l1 = 0, width;
394 int b1, b2, b;
395 size_t i;
397 const char *title1 = N_("Panel split");
398 const char *title2 = N_("Console output");
399 const char *title3 = N_("Other options");
401 const char *s_split_direction[2] = {
402 N_("&Vertical"),
403 N_("&Horizontal")
406 const char *ok_button = N_("&OK");
407 const char *cancel_button = N_("&Cancel");
409 output_lines_label = _("Output lines:");
411 /* save old params */
412 _equal_split = equal_split;
413 _menubar_visible = menubar_visible;
414 _command_prompt = command_prompt;
415 _keybar_visible = mc_global.keybar_visible;
416 _message_visible = mc_global.message_visible;
417 _xterm_title = xterm_title;
418 _free_space = free_space;
419 old_first_panel_size = -1;
420 old_horizontal_split = -1;
421 old_output_lines = -1;
422 _first_panel_size = first_panel_size;
423 _output_lines = output_lines;
425 #ifdef ENABLE_NLS
427 static gboolean i18n = FALSE;
429 title1 = _(title1);
430 title2 = _(title2);
431 title3 = _(title3);
433 i = G_N_ELEMENTS (s_split_direction);
434 while (i-- != 0)
435 s_split_direction[i] = _(s_split_direction[i]);
437 if (!i18n)
439 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
440 check_options[i].text = _(check_options[i].text);
441 i18n = TRUE;
444 ok_button = _(ok_button);
445 cancel_button = _(cancel_button);
447 #endif
449 /* radiobuttons */
450 i = G_N_ELEMENTS (s_split_direction);
451 while (i-- != 0)
452 l1 = max (l1, str_term_width1 (s_split_direction[i]) + 7);
453 /* checkboxes */
454 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
455 l1 = max (l1, str_term_width1 (check_options[i].text) + 7);
456 /* groupboxes */
457 l1 = max (l1, str_term_width1 (title1) + 4);
458 l1 = max (l1, str_term_width1 (title2) + 4);
459 l1 = max (l1, str_term_width1 (title3) + 4);
460 /* label + "+"/"-" buttons */
461 output_lines_label_len = str_term_width1 (output_lines_label);
462 l1 = max (l1, output_lines_label_len + 12);
463 /* buttons */
464 b1 = str_term_width1 (ok_button) + 5; /* default button */
465 b2 = str_term_width1 (cancel_button) + 3;
466 b = b1 + b2 + 1;
467 /* dialog width */
468 width = max (l1 * 2 + 7, b);
470 layout_dlg =
471 create_dlg (TRUE, 0, 0, 14, width,
472 dialog_colors, layout_callback, "[Layout]",
473 _("Layout"), DLG_CENTER | DLG_REVERSE);
475 /* buttons */
476 add_widget (layout_dlg,
477 button_new (11, (width - b) / 3 * 2 + b1 + 1, B_CANCEL, NORMAL_BUTTON,
478 cancel_button, 0));
479 add_widget (layout_dlg,
480 button_new (11, (width - b) / 3, B_ENTER, DEFPUSH_BUTTON, ok_button, 0));
482 #define XTRACT(i) *check_options[i].variable, check_options[i].text
484 /* "Other options" groupbox */
485 for (i = 0; i < (size_t) OTHER_OPTIONS_COUNT; i++)
487 check_options[i].widget = check_new (OTHER_OPTIONS_COUNT - i + 2, 6 + l1, XTRACT (i));
488 add_widget (layout_dlg, check_options[i].widget);
491 add_widget (layout_dlg, groupbox_new (2, 4 + l1, 9, l1, title3));
493 /* "Console output" groupbox */
495 const int disabled = mc_global.tty.console_flag ? 0 : W_DISABLED;
496 Widget *w;
498 w = (Widget *) button_new (9, output_lines_label_len + 5 + 5, B_MINUS,
499 NARROW_BUTTON, "&-", bminus_cback);
500 w->options |= disabled;
501 add_widget (layout_dlg, w);
503 w = (Widget *) button_new (9, output_lines_label_len + 5, B_PLUS,
504 NARROW_BUTTON, "&+", bplus_cback);
505 w->options |= disabled;
506 add_widget (layout_dlg, w);
508 w = (Widget *) groupbox_new (8, 3, 3, l1, title2);
509 w->options |= disabled;
510 add_widget (layout_dlg, w);
514 /* "Panel split" groupbox */
515 bright_widget = button_new (6, 14, B_2RIGHT, NARROW_BUTTON, "&>", b_left_right_cback);
516 widget_disable (bright_widget->widget, _equal_split);
517 add_widget (layout_dlg, bright_widget);
519 bleft_widget = button_new (6, 8, B_2LEFT, NARROW_BUTTON, "&<", b_left_right_cback);
520 widget_disable (bleft_widget->widget, _equal_split);
521 add_widget (layout_dlg, bleft_widget);
523 check_options[6].widget = check_new (5, 5, XTRACT (6));
524 add_widget (layout_dlg, check_options[6].widget);
526 radio_widget = radio_new (3, 5, 2, s_split_direction);
527 radio_widget->sel = horizontal_split;
528 add_widget (layout_dlg, radio_widget);
530 add_widget (layout_dlg, groupbox_new (2, 3, 6, l1, title1));
532 #undef XTRACT
534 return layout_dlg;
537 /* --------------------------------------------------------------------------------------------- */
539 static void
540 check_split (void)
542 if (horizontal_split)
544 if (equal_split)
545 first_panel_size = height / 2;
546 else if (first_panel_size < MINHEIGHT)
547 first_panel_size = MINHEIGHT;
548 else if (first_panel_size > height - MINHEIGHT)
549 first_panel_size = height - MINHEIGHT;
551 else
553 if (equal_split)
554 first_panel_size = COLS / 2;
555 else if (first_panel_size < MINWIDTH)
556 first_panel_size = MINWIDTH;
557 else if (first_panel_size > COLS - MINWIDTH)
558 first_panel_size = COLS - MINWIDTH;
562 /* --------------------------------------------------------------------------------------------- */
564 static void
565 panel_do_cols (int idx)
567 if (get_display_type (idx) == view_listing)
568 set_panel_formats ((WPanel *) panels[idx].widget);
569 else
570 panel_update_cols (panels[idx].widget, frame_half);
573 /* --------------------------------------------------------------------------------------------- */
574 /** Save current list_view widget directory into panel */
576 static Widget *
577 restore_into_right_dir_panel (int idx, Widget * from_widget)
579 Widget *new_widget = NULL;
580 const char *saved_dir = panels[idx].last_saved_dir;
581 gboolean last_was_panel = (from_widget && get_display_type (idx) != view_listing);
582 const char *p_name = get_nth_panel_name (idx);
584 if (last_was_panel)
585 new_widget = (Widget *) panel_new_with_dir (p_name, saved_dir);
586 else
587 new_widget = (Widget *) panel_new (p_name);
589 return new_widget;
592 /* --------------------------------------------------------------------------------------------- */
593 /*** public functions ****************************************************************************/
594 /* --------------------------------------------------------------------------------------------- */
596 void
597 layout_change (void)
599 setup_panels ();
600 /* update the main menu, because perhaps there was a change in the way
601 how the panel are split (horizontal/vertical),
602 and a change of menu visibility. */
603 update_menu ();
604 load_hint (1);
607 /* --------------------------------------------------------------------------------------------- */
609 void
610 layout_box (void)
612 Dlg_head *layout_dlg;
613 gboolean layout_do_change = FALSE;
615 layout_dlg = init_layout ();
617 if (run_dlg (layout_dlg) == B_ENTER)
619 size_t i;
620 for (i = 0; i < (size_t) LAYOUT_OPTIONS_COUNT; i++)
621 if (check_options[i].widget != NULL)
622 *check_options[i].variable = check_options[i].widget->state & C_BOOL;
623 horizontal_split = radio_widget->sel;
624 first_panel_size = _first_panel_size;
625 output_lines = _output_lines;
626 layout_do_change = TRUE;
629 destroy_dlg (layout_dlg);
630 if (layout_do_change)
631 layout_change ();
634 /* --------------------------------------------------------------------------------------------- */
636 void
637 setup_panels (void)
639 int start_y;
640 int promptl; /* the prompt len */
642 if (mc_global.tty.console_flag)
644 int minimum;
645 if (output_lines < 0)
646 output_lines = 0;
647 height =
648 LINES - mc_global.keybar_visible - command_prompt - menubar_visible -
649 output_lines - mc_global.message_visible;
650 minimum = MINHEIGHT * (1 + horizontal_split);
651 if (height < minimum)
653 output_lines -= minimum - height;
654 height = minimum;
657 else
659 height =
660 LINES - menubar_visible - command_prompt - mc_global.keybar_visible -
661 mc_global.message_visible;
663 check_split ();
664 start_y = menubar_visible;
666 /* The column computing is defered until panel_do_cols */
667 if (horizontal_split)
669 widget_set_size (panels[0].widget, start_y, 0, first_panel_size, 0);
671 widget_set_size (panels[1].widget, start_y + first_panel_size, 0,
672 height - first_panel_size, 0);
674 else
676 int first_x = first_panel_size;
678 widget_set_size (panels[0].widget, start_y, 0, height, 0);
680 widget_set_size (panels[1].widget, start_y, first_x, height, 0);
683 panel_do_cols (0);
684 panel_do_cols (1);
686 promptl = str_term_width1 (mc_prompt);
688 widget_set_size (&the_menubar->widget, 0, 0, 1, COLS);
690 if (command_prompt)
692 widget_set_size (&cmdline->widget, LINES - 1 - mc_global.keybar_visible, promptl, 1,
693 COLS - promptl);
694 input_set_origin (cmdline, promptl, COLS - promptl);
695 widget_set_size (&the_prompt->widget, LINES - 1 - mc_global.keybar_visible, 0, 1, promptl);
697 else
699 widget_set_size (&cmdline->widget, 0, 0, 0, 0);
700 input_set_origin (cmdline, 0, 0);
701 widget_set_size (&the_prompt->widget, LINES, COLS, 0, 0);
704 widget_set_size (&the_bar->widget, LINES - 1, 0, mc_global.keybar_visible, COLS);
705 buttonbar_set_visible (the_bar, mc_global.keybar_visible);
707 /* Output window */
708 if (mc_global.tty.console_flag && output_lines)
710 output_start_y = LINES - command_prompt - mc_global.keybar_visible - output_lines;
711 show_console_contents (output_start_y,
712 LINES - output_lines - mc_global.keybar_visible - 1,
713 LINES - mc_global.keybar_visible - 1);
715 if (mc_global.message_visible)
716 widget_set_size (&the_hint->widget, height + start_y, 0, 1, COLS);
717 else
718 widget_set_size (&the_hint->widget, 0, 0, 0, 0);
720 update_xterm_title_path ();
723 /* --------------------------------------------------------------------------------------------- */
725 void
726 sigwinch_handler (int dummy)
728 (void) dummy;
729 #if !(defined(USE_NCURSES) || defined(USE_NCURSESW)) /* don't do malloc in a signal handler */
730 tty_low_level_change_screen_size ();
731 #endif
732 mc_global.tty.winch_flag = TRUE;
735 /* --------------------------------------------------------------------------------------------- */
737 void
738 use_dash (gboolean flag)
740 if (flag)
741 ok_to_refresh++;
742 else
743 ok_to_refresh--;
746 /* --------------------------------------------------------------------------------------------- */
748 void
749 set_hintbar (const char *str)
751 label_set_text (the_hint, str);
752 if (ok_to_refresh > 0)
753 mc_refresh ();
756 /* --------------------------------------------------------------------------------------------- */
758 void
759 rotate_dash (void)
761 static const char rotating_dash[] = "|/-\\";
762 static size_t pos = 0;
764 if (!nice_rotating_dash || (ok_to_refresh <= 0))
765 return;
767 if (pos >= sizeof (rotating_dash) - 1)
768 pos = 0;
769 tty_gotoyx (0, COLS - 1);
770 tty_setcolor (NORMAL_COLOR);
771 tty_print_char (rotating_dash[pos]);
772 mc_refresh ();
773 pos++;
776 /* --------------------------------------------------------------------------------------------- */
778 const char *
779 get_nth_panel_name (int num)
781 static char buffer[BUF_SMALL];
783 if (!num)
784 return "New Left Panel";
785 else if (num == 1)
786 return "New Right Panel";
787 else
789 g_snprintf (buffer, sizeof (buffer), "%ith Panel", num);
790 return buffer;
794 /* --------------------------------------------------------------------------------------------- */
795 /* I wonder if I should start to use the folding mode than Dugan uses */
796 /* */
797 /* This is the centralized managing of the panel display types */
798 /* This routine takes care of destroying and creating new widgets */
799 /* Please note that it could manage MAX_VIEWS, not just left and right */
800 /* Currently nothing in the code takes advantage of this and has hard- */
801 /* coded values for two panels only */
803 /* Set the num-th panel to the view type: type */
804 /* This routine also keeps at least one WPanel object in the screen */
805 /* since a lot of routines depend on the current_panel variable */
807 void
808 set_display_type (int num, panel_view_mode_t type)
810 int x = 0, y = 0, cols = 0, lines = 0;
811 unsigned int the_other = 0; /* Index to the other panel */
812 const char *file_name = NULL; /* For Quick view */
813 Widget *new_widget = NULL, *old_widget = NULL;
814 panel_view_mode_t old_type;
815 WPanel *the_other_panel = NULL;
817 if (num >= MAX_VIEWS)
819 fprintf (stderr, "Cannot allocate more that %d views\n", MAX_VIEWS);
820 abort ();
822 /* Check that we will have a WPanel * at least */
823 if (type != view_listing)
825 the_other = num == 0 ? 1 : 0;
827 if (panels[the_other].type != view_listing)
828 return;
831 /* Get rid of it */
832 if (panels[num].widget != NULL)
834 Widget *w = panels[num].widget;
835 WPanel *panel = (WPanel *) w;
837 x = w->x;
838 y = w->y;
839 cols = w->cols;
840 lines = w->lines;
841 old_widget = w;
842 old_type = panels[num].type;
844 if (old_type == view_listing && panel->frame_size == frame_full && type != view_listing)
846 cols = COLS - first_panel_size;
847 if (num == 1)
848 x = first_panel_size;
852 /* Restoring saved path from panels.ini for nonlist panel */
853 /* when it's first creation (for example view_info) */
854 if (old_widget == NULL && type != view_listing)
856 char panel_dir[MC_MAXPATHLEN];
857 mc_get_current_wd (panel_dir, sizeof (panel_dir));
858 panels[num].last_saved_dir = g_strdup (panel_dir);
861 switch (type)
863 case view_nothing:
864 case view_listing:
865 new_widget = restore_into_right_dir_panel (num, old_widget);
866 widget_set_size (new_widget, y, x, lines, cols);
867 break;
869 case view_info:
870 new_widget = (Widget *) info_new (y, x, lines, cols);
871 break;
873 case view_tree:
874 new_widget = (Widget *) tree_new (y, x, lines, cols, TRUE);
875 break;
877 case view_quick:
878 new_widget = (Widget *) mcview_new (y, x, lines, cols, TRUE);
879 the_other_panel = (WPanel *) panels[the_other].widget;
880 if (the_other_panel != NULL)
881 file_name = the_other_panel->dir.list[the_other_panel->selected].fname;
882 else
883 file_name = "";
885 mcview_load ((struct mcview_struct *) new_widget, 0, file_name, 0);
886 break;
889 if (type != view_listing)
890 /* Must save dir, for restoring after change type to */
891 /* view_listing */
892 save_panel_dir (num);
894 panels[num].type = type;
895 panels[num].widget = new_widget;
897 /* We use replace to keep the circular list of the dialog in the */
898 /* same state. Maybe we could just kill it and then replace it */
899 if ((midnight_dlg != NULL) && (old_widget != NULL))
901 if (old_widget == view_listing)
903 /* save and write directory history of panel
904 * ... and other histories of midnight_dlg */
905 dlg_save_history (midnight_dlg);
908 dlg_replace_widget (old_widget, new_widget);
911 if (type == view_listing)
913 WPanel *panel = (WPanel *) new_widget;
915 /* if existing panel changed type to view_listing, then load history */
916 if (old_widget != NULL)
918 ev_history_load_save_t event_data = { NULL, new_widget };
920 mc_event_raise (midnight_dlg->event_group, MCEVENT_HISTORY_LOAD, &event_data);
923 if (num == 0)
924 left_panel = panel;
925 else
926 right_panel = panel;
928 /* forced update format after set new sizes */
929 set_panel_formats (panel);
932 if (type == view_tree)
933 the_tree = (WTree *) new_widget;
935 /* Prevent current_panel's value from becoming invalid.
936 * It's just a quick hack to prevent segfaults. Comment out and
937 * try following:
938 * - select left panel
939 * - invoke menue left/tree
940 * - as long as you stay in the left panel almost everything that uses
941 * current_panel causes segfault, e.g. C-Enter, C-x c, ...
943 if ((type != view_listing) && (current_panel == (WPanel *) old_widget))
944 current_panel = num == 0 ? right_panel : left_panel;
946 g_free (old_widget);
949 /* --------------------------------------------------------------------------------------------- */
951 void
952 panel_update_cols (Widget * widget, panel_display_t frame_size)
954 int cols, origin;
956 /* don't touch panel if it is not in dialog yet */
957 /* if panel is not in dialog it is not in widgets list
958 and cannot be compared with get_panel_widget() result */
959 if (widget->owner == NULL)
960 return;
962 if (horizontal_split)
964 widget->cols = COLS;
965 return;
968 if (frame_size == frame_full)
970 cols = COLS;
971 origin = 0;
973 else if (widget == get_panel_widget (0))
975 cols = first_panel_size;
976 origin = 0;
978 else
980 cols = COLS - first_panel_size;
981 origin = first_panel_size;
984 widget->cols = cols;
985 widget->x = origin;
988 /* --------------------------------------------------------------------------------------------- */
989 /** This routine is deeply sticked to the two panels idea.
990 What should it do in more panels. ANSWER - don't use it
991 in any multiple panels environment. */
993 void
994 swap_panels (void)
996 WPanel *panel1, *panel2;
997 Widget *tmp_widget;
999 panel1 = (WPanel *) panels[0].widget;
1000 panel2 = (WPanel *) panels[1].widget;
1002 if (panels[0].type == view_listing && panels[1].type == view_listing &&
1003 !mc_config_get_bool (mc_main_config, CONFIG_PANELS_SECTION, "simple_swap", FALSE))
1005 WPanel panel;
1007 #define panelswap(x) panel.x = panel1->x; panel1->x = panel2->x; panel2->x = panel.x;
1009 #define panelswapstr(e) strcpy (panel.e, panel1->e); \
1010 strcpy (panel1->e, panel2->e); \
1011 strcpy (panel2->e, panel.e);
1012 /* Change content and related stuff */
1013 panelswap (dir);
1014 panelswap (active);
1015 panelswapstr (cwd);
1016 panelswapstr (lwd);
1017 panelswap (count);
1018 panelswap (marked);
1019 panelswap (dirs_marked);
1020 panelswap (total);
1021 panelswap (top_file);
1022 panelswap (selected);
1023 panelswap (is_panelized);
1024 panelswap (dir_stat);
1025 #undef panelswapstr
1026 #undef panelswap
1028 panel1->searching = FALSE;
1029 panel2->searching = FALSE;
1031 if (current_panel == panel1)
1032 current_panel = panel2;
1033 else
1034 current_panel = panel1;
1036 /* if sort options are different -> resort panels */
1037 if (memcmp (&panel1->sort_info, &panel2->sort_info, sizeof (panel_sort_info_t)) != 0)
1039 panel_re_sort (other_panel);
1040 panel_re_sort (current_panel);
1043 if (dlg_widget_active (panels[0].widget))
1044 dlg_select_widget (panels[1].widget);
1045 else if (dlg_widget_active (panels[1].widget))
1046 dlg_select_widget (panels[0].widget);
1048 else
1050 WPanel *tmp_panel;
1051 int x, y, cols, lines;
1052 int tmp_type;
1054 tmp_panel = right_panel;
1055 right_panel = left_panel;
1056 left_panel = tmp_panel;
1058 if (panels[0].type == view_listing)
1060 if (strcmp (panel1->panel_name, get_nth_panel_name (0)) == 0)
1062 g_free (panel1->panel_name);
1063 panel1->panel_name = g_strdup (get_nth_panel_name (1));
1066 if (panels[1].type == view_listing)
1068 if (strcmp (panel2->panel_name, get_nth_panel_name (1)) == 0)
1070 g_free (panel2->panel_name);
1071 panel2->panel_name = g_strdup (get_nth_panel_name (0));
1075 x = panels[0].widget->x;
1076 y = panels[0].widget->y;
1077 cols = panels[0].widget->cols;
1078 lines = panels[0].widget->lines;
1080 panels[0].widget->x = panels[1].widget->x;
1081 panels[0].widget->y = panels[1].widget->y;
1082 panels[0].widget->cols = panels[1].widget->cols;
1083 panels[0].widget->lines = panels[1].widget->lines;
1085 panels[1].widget->x = x;
1086 panels[1].widget->y = y;
1087 panels[1].widget->cols = cols;
1088 panels[1].widget->lines = lines;
1090 tmp_widget = panels[0].widget;
1091 panels[0].widget = panels[1].widget;
1092 panels[1].widget = tmp_widget;
1093 tmp_type = panels[0].type;
1094 panels[0].type = panels[1].type;
1095 panels[1].type = tmp_type;
1097 /* force update formats because of possible changed sizes */
1098 if (panels[0].type == view_listing)
1099 set_panel_formats ((WPanel *) panels[0].widget);
1100 if (panels[1].type == view_listing)
1101 set_panel_formats ((WPanel *) panels[1].widget);
1105 /* --------------------------------------------------------------------------------------------- */
1107 panel_view_mode_t
1108 get_display_type (int idx)
1110 return panels[idx].type;
1113 /* --------------------------------------------------------------------------------------------- */
1115 struct Widget *
1116 get_panel_widget (int idx)
1118 return panels[idx].widget;
1121 /* --------------------------------------------------------------------------------------------- */
1124 get_current_index (void)
1126 if (panels[0].widget == ((Widget *) current_panel))
1127 return 0;
1128 else
1129 return 1;
1132 /* --------------------------------------------------------------------------------------------- */
1135 get_other_index (void)
1137 return !get_current_index ();
1140 /* --------------------------------------------------------------------------------------------- */
1142 struct WPanel *
1143 get_other_panel (void)
1145 return (struct WPanel *) get_panel_widget (get_other_index ());
1148 /* --------------------------------------------------------------------------------------------- */
1149 /** Returns the view type for the current panel/view */
1151 panel_view_mode_t
1152 get_current_type (void)
1154 if (panels[0].widget == (Widget *) current_panel)
1155 return panels[0].type;
1156 else
1157 return panels[1].type;
1160 /* --------------------------------------------------------------------------------------------- */
1161 /** Returns the view type of the unselected panel */
1163 panel_view_mode_t
1164 get_other_type (void)
1166 if (panels[0].widget == (Widget *) current_panel)
1167 return panels[1].type;
1168 else
1169 return panels[0].type;
1172 /* --------------------------------------------------------------------------------------------- */
1173 /** 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 = g_strdup (widget_work_dir);
1192 /* --------------------------------------------------------------------------------------------- */
1193 /** Return working dir, if it's view_listing - cwd,
1194 but for other types - last_saved_dir */
1196 const char *
1197 get_panel_dir_for (const WPanel * widget)
1199 int i;
1201 for (i = 0; i < MAX_VIEWS; i++)
1202 if ((WPanel *) get_panel_widget (i) == widget)
1203 break;
1205 if (i >= MAX_VIEWS)
1206 return ".";
1208 if (get_display_type (i) == view_listing)
1209 return ((WPanel *) get_panel_widget (i))->cwd;
1211 return panels[i].last_saved_dir;
1214 /* --------------------------------------------------------------------------------------------- */