diffviewer: Added libmc for build depends
[midnight-commander.git] / src / layout.c
bloba24f05436147c44b5d1e77c1b055685536bd7fb1
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 "widget.h"
59 #include "command.h"
60 #include "main-widgets.h"
61 #include "main.h"
62 #include "subshell.h" /* For use_subshell and resize_subshell() */
63 #include "tree.h"
64 #include "menu.h"
65 #include "background.h" /* we_are_background */
66 /* Needed for the extern declarations of integer parameters */
67 #include "dir.h"
68 #include "panel.h" /* The Panel widget */
69 #include "consaver/cons.saver.h"
70 #include "layout.h"
71 #include "info.h" /* The Info widget */
72 #include "src/viewer/mcviewer.h" /* The view widget */
74 #include "setup.h" /* For save_setup() */
76 /* Controls the display of the rotating dash on the verbose mode */
77 int nice_rotating_dash = 1;
79 /* Set if the panels are split horizontally */
80 int horizontal_split = 0;
82 /* Set if the window has changed it's size */
83 int winch_flag = 0;
85 /* Set if the split is the same */
86 int equal_split = 1;
88 /* First panel size if the panel are not split equally */
89 int first_panel_size = 0;
91 /* The number of output lines shown (if available) */
92 int output_lines = 0;
94 /* Set if the command prompt is to be displayed */
95 int command_prompt = 1;
97 /* Set if the nice and useful keybar is visible */
98 int keybar_visible = 1;
100 /* Set if the nice message (hint) bar is visible */
101 int message_visible = 1;
103 /* Set to show current working dir in xterm window title */
104 int xterm_title = 1;
106 /* Set to show free space on device assigned to current directory */
107 int free_space = 1;
109 /* The starting line for the output of the subprogram */
110 int output_start_y = 0;
112 /* The maximum number of views managed by the set_display_type routine */
113 /* Must be at least two (for current and other). Please note that until */
114 /* Janne gets around this, we will only manage two of them :-) */
115 #define MAX_VIEWS 2
117 static struct {
118 panel_view_mode_t type;
119 Widget *widget;
120 char *last_saved_dir; /* last view_list working directory */
121 } panels [MAX_VIEWS] = {
122 /* init MAX_VIEWS items */
123 { view_listing, NULL, NULL },
124 { view_listing, NULL, NULL }
127 /* These variables are used to avoid updating the information unless */
128 /* we need it */
129 static int old_first_panel_size;
130 static int old_horizontal_split;
131 static int old_output_lines;
133 /* Internal variables */
134 static int _horizontal_split;
135 static int _equal_split;
136 static int _first_panel_size;
137 static int _menubar_visible;
138 static int _output_lines;
139 static int _command_prompt;
140 static int _keybar_visible;
141 static int _message_visible;
142 static int _xterm_title;
143 static int _free_space;
144 static int _permission_mode;
145 static int _filetype_mode;
147 static int height;
149 /* Width 12 for a wee Quick (Hex) View */
150 #define MINWIDTH 12
151 #define MINHEIGHT 5
153 #define BY 12
155 #define B_2LEFT B_USER
156 #define B_2RIGHT (B_USER + 1)
157 #define B_PLUS (B_USER + 2)
158 #define B_MINUS (B_USER + 3)
160 static Dlg_head *layout_dlg;
162 static const char *s_split_direction [2] = {
163 N_("&Vertical"),
164 N_("&Horizontal")
167 static WRadio *radio_widget;
169 static struct {
170 const char *text;
171 int *variable;
172 WCheck *widget;
173 } check_options [] = {
174 { N_("show free sp&Ace"), &free_space, 0 },
175 { N_("&Xterm window title"), &xterm_title, 0 },
176 { N_("h&Intbar visible"), &message_visible, 0 },
177 { N_("&Keybar visible"), &keybar_visible, 0 },
178 { N_("command &Prompt"), &command_prompt, 0 },
179 { N_("show &Mini status"), &show_mini_info, 0 },
180 { N_("menu&Bar visible"), &menubar_visible, 0 },
181 { N_("&Equal split"), &equal_split, 0 },
182 { N_("pe&Rmissions"), &permission_mode, 0 },
183 { N_("&File types"), &filetype_mode, 0 },
184 { 0, 0, 0 }
187 #define LAYOUT_OPTIONS_COUNT 10
188 #define HIGHLIGHT_OPTIONS_COUNT 2
189 #define SPLIT_OPTIONS_COUNT 1
190 #define OTHER_OPTIONS_COUNT 7
192 static gsize first_width, second_width;
193 static const char *output_lines_label;
195 static WButton *bleft_widget, *bright_widget;
197 static void _check_split (void)
199 if (_horizontal_split){
200 if (_equal_split)
201 _first_panel_size = height / 2;
202 else if (_first_panel_size < MINHEIGHT)
203 _first_panel_size = MINHEIGHT;
204 else if (_first_panel_size > height - MINHEIGHT)
205 _first_panel_size = height - MINHEIGHT;
206 } else {
207 if (_equal_split)
208 _first_panel_size = COLS / 2;
209 else if (_first_panel_size < MINWIDTH)
210 _first_panel_size = MINWIDTH;
211 else if (_first_panel_size > COLS - MINWIDTH)
212 _first_panel_size = COLS - MINWIDTH;
216 static void update_split (void)
218 /* Check split has to be done before testing if it changed, since
219 it can change due to calling _check_split() as well*/
220 _check_split ();
222 /* To avoid setting the cursor to the wrong place */
223 if ((old_first_panel_size == _first_panel_size) &&
224 (old_horizontal_split == _horizontal_split)){
225 return;
228 old_first_panel_size = _first_panel_size;
229 old_horizontal_split = _horizontal_split;
231 tty_setcolor (COLOR_NORMAL);
232 dlg_move (layout_dlg, 6, 6);
233 tty_printf ("%03d", _first_panel_size);
234 dlg_move (layout_dlg, 6, 18);
235 if (_horizontal_split)
236 tty_printf ("%03d", height - _first_panel_size);
237 else
238 tty_printf ("%03d", COLS - _first_panel_size);
241 static int b2left_cback (int action)
243 (void) action;
245 if (_equal_split){
246 /* Turn equal split off */
247 _equal_split = 0;
248 check_options [7].widget->state = check_options [7].widget->state & ~C_BOOL;
249 dlg_select_widget (check_options [7].widget);
250 dlg_select_widget (bleft_widget);
252 _first_panel_size++;
253 return 0;
256 static int b2right_cback (int action)
258 (void) action;
260 if (_equal_split){
261 /* Turn equal split off */
262 _equal_split = 0;
263 check_options [7].widget->state = check_options [7].widget->state & ~C_BOOL;
264 dlg_select_widget (check_options [7].widget);
265 dlg_select_widget (bright_widget);
267 _first_panel_size--;
268 return 0;
271 static int bplus_cback (int action)
273 (void) action;
275 if (_output_lines < 99)
276 _output_lines++;
277 return 0;
280 static int bminus_cback (int action)
282 (void) action;
284 if (_output_lines > 0)
285 _output_lines--;
286 return 0;
289 static cb_ret_t
290 layout_callback (Dlg_head *h, Widget *sender,
291 dlg_msg_t msg, int parm, void *data)
293 switch (msg) {
294 case DLG_DRAW:
295 /*When repainting the whole dialog (e.g. with C-l) we have to
296 update everything*/
297 common_dialog_repaint (h);
299 old_first_panel_size = -1;
300 old_horizontal_split = -1;
301 old_output_lines = -1;
303 tty_setcolor (COLOR_HOT_NORMAL);
304 update_split ();
305 dlg_move (h, 6, 13);
306 tty_print_char ('=');
307 if (console_flag){
308 if (old_output_lines != _output_lines){
309 old_output_lines = _output_lines;
310 tty_setcolor (COLOR_NORMAL);
311 dlg_move (h, LAYOUT_OPTIONS_COUNT, 16 + first_width);
312 tty_print_string (output_lines_label);
313 dlg_move (h, LAYOUT_OPTIONS_COUNT, 10 + first_width);
314 tty_printf ("%02d", _output_lines);
317 return MSG_HANDLED;
319 case DLG_POST_KEY:
320 _filetype_mode = check_options [9].widget->state & C_BOOL;
321 _permission_mode = check_options [8].widget->state & C_BOOL;
322 _equal_split = check_options [7].widget->state & C_BOOL;
323 _menubar_visible = check_options [6].widget->state & C_BOOL;
324 _command_prompt = check_options [5].widget->state & C_BOOL;
325 _keybar_visible = check_options [3].widget->state & C_BOOL;
326 _message_visible = check_options [2].widget->state & C_BOOL;
327 _xterm_title = check_options [1].widget->state & C_BOOL;
328 _free_space = check_options [0].widget->state & C_BOOL;
329 if (console_flag){
330 int minimum;
331 if (_output_lines < 0)
332 _output_lines = 0;
333 height = LINES - _keybar_visible - _command_prompt -
334 _menubar_visible - _output_lines - _message_visible;
335 minimum = MINHEIGHT * (1 + _horizontal_split);
336 if (height < minimum){
337 _output_lines -= minimum - height;
338 height = minimum;
340 } else {
341 height = LINES - _keybar_visible - _command_prompt -
342 _menubar_visible - _output_lines - _message_visible;
344 if (_horizontal_split != radio_widget->sel){
345 _horizontal_split = radio_widget->sel;
346 if (_horizontal_split)
347 _first_panel_size = height / 2;
348 else
349 _first_panel_size = COLS / 2;
351 update_split ();
352 if (console_flag){
353 if (old_output_lines != _output_lines){
354 old_output_lines = _output_lines;
355 tty_setcolor (COLOR_NORMAL);
356 dlg_move (h, LAYOUT_OPTIONS_COUNT, 10 + first_width);
357 tty_printf ("%02d", _output_lines);
360 return MSG_HANDLED;
362 default:
363 return default_dlg_callback (h, sender, msg, parm, data);
367 static void
368 init_layout (void)
370 static int i18n_layt_flag = 0;
371 static int b1, b2, b3;
372 int i = sizeof (s_split_direction) / sizeof (char *);
373 const char *ok_button = _("&OK");
374 const char *cancel_button = _("&Cancel");
375 const char *save_button = _("&Save");
376 static const char *title1, *title2, *title3;
378 if (!i18n_layt_flag) {
379 gsize l1;
381 first_width = 19; /* length of line with '<' '>' buttons */
383 title1 = _(" Panel split ");
384 title2 = _(" Highlight... ");
385 title3 = _(" Other options ");
386 output_lines_label = _("output lines");
388 while (i--) {
389 s_split_direction[i] = _(s_split_direction[i]);
390 l1 = str_term_width1 (s_split_direction[i]) + 7;
391 if (l1 > first_width)
392 first_width = l1;
395 for (i = 0; i < LAYOUT_OPTIONS_COUNT; i++) {
396 check_options[i].text = _(check_options[i].text);
397 l1 = str_term_width1 (check_options[i].text) + 7;
398 if (l1 > first_width)
399 first_width = l1;
402 l1 = str_term_width1 (title1) + 1;
403 if (l1 > first_width)
404 first_width = l1;
406 l1 = str_term_width1 (title2) + 1;
407 if (l1 > first_width)
408 first_width = l1;
410 second_width = str_term_width1 (title3) + 1;
411 for (i = 0; i < OTHER_OPTIONS_COUNT; i++) {
412 check_options[i].text = _(check_options[i].text);
413 l1 = str_term_width1 (check_options[i].text) + 7;
414 if (l1 > second_width)
415 second_width = l1;
417 if (console_flag) {
418 l1 = str_term_width1 (output_lines_label) + 13;
419 if (l1 > second_width)
420 second_width = l1;
424 * alex@bcs.zp.ua:
425 * To be completely correct, one need to check if the title
426 * does not exceed dialog length and total length of 3 buttons
427 * allows their placement in one row. But assuming this dialog
428 * is wide enough, I don't include such a tests.
430 * Now the last thing to do - properly space buttons...
432 l1 = 11 + str_term_width1 (ok_button) /* 14 - all brackets and inner space */
433 + str_term_width1 (save_button) /* notice: it is 3 char less because */
434 + str_term_width1 (cancel_button); /* of '&' char in button text */
436 i = (first_width + second_width - l1) / 4;
437 b1 = 5 + i;
438 b2 = b1 + str_term_width1 (ok_button) + i + 6;
439 b3 = b2 + str_term_width1 (save_button) + i + 4;
441 i18n_layt_flag = 1;
444 layout_dlg =
445 create_dlg (0, 0, 15, first_width + second_width + 9,
446 dialog_colors, layout_callback, "[Layout]",
447 _("Layout"), DLG_CENTER | DLG_REVERSE);
449 add_widget (layout_dlg, groupbox_new (2, 4, 6, first_width, title1));
450 add_widget (layout_dlg, groupbox_new (8, 4, 4, first_width, title2));
451 add_widget (layout_dlg,
452 groupbox_new (2, 5 + first_width, 10, second_width,
453 title3));
455 add_widget (layout_dlg,
456 button_new (BY, b3, B_CANCEL, NORMAL_BUTTON, cancel_button,
457 0));
458 add_widget (layout_dlg,
459 button_new (BY, b2, B_EXIT, NORMAL_BUTTON, save_button,
460 0));
461 add_widget (layout_dlg,
462 button_new (BY, b1, B_ENTER, DEFPUSH_BUTTON, ok_button,
463 0));
464 if (console_flag) {
465 add_widget (layout_dlg,
466 button_new (LAYOUT_OPTIONS_COUNT, 12 + first_width, B_MINUS,
467 NARROW_BUTTON, "&-", bminus_cback));
468 add_widget (layout_dlg,
469 button_new (LAYOUT_OPTIONS_COUNT, 7 + first_width, B_PLUS, NARROW_BUTTON,
470 "&+", bplus_cback));
472 #define XTRACT(i) *check_options[i].variable, check_options[i].text
474 for (i = 0; i < OTHER_OPTIONS_COUNT; i++) {
475 check_options[i].widget =
476 check_new (LAYOUT_OPTIONS_COUNT - i - 1, 7 + first_width, XTRACT (i));
477 add_widget (layout_dlg, check_options[i].widget);
479 check_options[9].widget = check_new (10, 6, XTRACT (9));
480 add_widget (layout_dlg, check_options[9].widget);
481 check_options[8].widget = check_new (9, 6, XTRACT (8));
482 add_widget (layout_dlg, check_options[8].widget);
484 _filetype_mode = filetype_mode;
485 _permission_mode = permission_mode;
486 _equal_split = equal_split;
487 _menubar_visible = menubar_visible;
488 _command_prompt = command_prompt;
489 _keybar_visible = keybar_visible;
490 _message_visible = message_visible;
491 _xterm_title = xterm_title;
492 _free_space = free_space;
493 bright_widget =
494 button_new (6, 15, B_2RIGHT, NARROW_BUTTON, "&>", b2right_cback);
495 add_widget (layout_dlg, bright_widget);
496 bleft_widget =
497 button_new (6, 9, B_2LEFT, NARROW_BUTTON, "&<", b2left_cback);
498 add_widget (layout_dlg, bleft_widget);
499 check_options[7].widget = check_new (5, 6, XTRACT (7));
500 old_first_panel_size = -1;
501 old_horizontal_split = -1;
502 old_output_lines = -1;
504 _first_panel_size = first_panel_size;
505 _output_lines = output_lines;
506 add_widget (layout_dlg, check_options[7].widget);
507 radio_widget = radio_new (3, 6, 2, s_split_direction);
508 add_widget (layout_dlg, radio_widget);
509 radio_widget->sel = horizontal_split;
512 void
513 layout_change (void)
515 setup_panels ();
516 /* re-init the menu, because perhaps there was a change in the way
517 how the panel are split (horizontal/vertical). */
518 done_menu ();
519 init_menu ();
520 menubar_arrange (the_menubar);
521 load_hint (1);
524 void layout_cmd (void)
526 int result;
527 int i;
528 int layout_do_change = 0;
530 init_layout ();
531 run_dlg (layout_dlg);
532 result = layout_dlg->ret_value;
534 if (result == B_ENTER || result == B_EXIT){
535 for (i = 0; check_options [i].text; i++)
536 if (check_options [i].widget)
537 *check_options [i].variable = check_options [i].widget->state & C_BOOL;
538 horizontal_split = radio_widget->sel;
539 first_panel_size = _first_panel_size;
540 output_lines = _output_lines;
541 layout_do_change = 1;
543 if (result == B_EXIT){
544 save_layout ();
545 mc_config_save_file (mc_main_config, NULL);
548 destroy_dlg (layout_dlg);
549 if (layout_do_change)
550 layout_change ();
553 static void check_split (void)
555 if (horizontal_split){
556 if (equal_split)
557 first_panel_size = height / 2;
558 else if (first_panel_size < MINHEIGHT)
559 first_panel_size = MINHEIGHT;
560 else if (first_panel_size > height - MINHEIGHT)
561 first_panel_size = height - MINHEIGHT;
562 } else {
563 if (equal_split)
564 first_panel_size = COLS / 2;
565 else if (first_panel_size < MINWIDTH)
566 first_panel_size = MINWIDTH;
567 else if (first_panel_size > COLS - MINWIDTH)
568 first_panel_size = COLS - MINWIDTH;
572 void
573 clr_scr (void)
575 tty_set_normal_attrs ();
576 tty_fill_region (0, 0, LINES, COLS, ' ');
577 tty_refresh ();
580 void
581 repaint_screen (void)
583 do_refresh ();
584 tty_refresh ();
587 void
588 mc_refresh (void)
590 #ifdef WITH_BACKGROUND
591 if (we_are_background)
592 return;
593 #endif /* WITH_BACKGROUND */
594 if (winch_flag == 0)
595 tty_refresh ();
596 else {
597 /* if winch was caugth, we should do not only redraw screen, but
598 reposition/resize all */
599 change_screen_size ();
603 static void
604 panel_do_cols (int idx)
606 if (get_display_type (idx) == view_listing)
607 set_panel_formats ((WPanel *) panels [idx].widget);
608 else
609 panel_update_cols (panels [idx].widget, frame_half);
612 void
613 setup_panels (void)
615 int start_y;
616 int promptl; /* the prompt len */
618 if (console_flag) {
619 int minimum;
620 if (output_lines < 0)
621 output_lines = 0;
622 height =
623 LINES - keybar_visible - command_prompt - menubar_visible -
624 output_lines - message_visible;
625 minimum = MINHEIGHT * (1 + horizontal_split);
626 if (height < minimum) {
627 output_lines -= minimum - height;
628 height = minimum;
630 } else {
631 height =
632 LINES - menubar_visible - command_prompt - keybar_visible -
633 message_visible;
635 check_split ();
636 start_y = menubar_visible;
638 /* The column computing is defered until panel_do_cols */
639 if (horizontal_split) {
640 widget_set_size (panels[0].widget, start_y, 0, first_panel_size,
643 widget_set_size (panels[1].widget, start_y + first_panel_size, 0,
644 height - first_panel_size, 0);
645 } else {
646 int first_x = first_panel_size;
648 widget_set_size (panels[0].widget, start_y, 0, height, 0);
650 widget_set_size (panels[1].widget, start_y, first_x, height, 0);
653 panel_do_cols (0);
654 panel_do_cols (1);
656 promptl = str_term_width1 (mc_prompt);
658 widget_set_size (&the_menubar->widget, 0, 0, 1, COLS);
660 if (command_prompt) {
661 widget_set_size (&cmdline->widget, LINES - 1 - keybar_visible,
662 promptl, 1, COLS - promptl);
663 winput_set_origin (cmdline, promptl, COLS - promptl);
664 widget_set_size (&the_prompt->widget, LINES - 1 - keybar_visible,
665 0, 1, promptl);
666 } else {
667 widget_set_size (&cmdline->widget, 0, 0, 0, 0);
668 winput_set_origin (cmdline, 0, 0);
669 widget_set_size (&the_prompt->widget, LINES, COLS, 0, 0);
672 widget_set_size (&the_bar->widget, LINES - 1, 0, keybar_visible, COLS);
673 buttonbar_set_visible (the_bar, keybar_visible);
675 /* Output window */
676 if (console_flag && output_lines) {
677 output_start_y =
678 LINES - command_prompt - keybar_visible - output_lines;
679 show_console_contents (output_start_y,
680 LINES - output_lines - keybar_visible - 1,
681 LINES - keybar_visible - 1);
683 if (message_visible)
684 widget_set_size (&the_hint->widget, height + start_y, 0, 1, COLS);
685 else
686 widget_set_size (&the_hint->widget, 0, 0, 0, 0);
688 update_xterm_title_path ();
691 static inline void
692 low_level_change_screen_size (void)
694 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
695 #if defined TIOCGWINSZ
696 struct winsize winsz;
698 winsz.ws_col = winsz.ws_row = 0;
699 /* Ioctl on the STDIN_FILENO */
700 ioctl (0, TIOCGWINSZ, &winsz);
701 if (winsz.ws_col && winsz.ws_row){
702 #if defined(NCURSES_VERSION) && defined(HAVE_RESIZETERM)
703 resizeterm(winsz.ws_row, winsz.ws_col);
704 clearok(stdscr,TRUE); /* sigwinch's should use a semaphore! */
705 #else
706 COLS = winsz.ws_col;
707 LINES = winsz.ws_row;
708 #endif
709 #ifdef HAVE_SUBSHELL_SUPPORT
710 resize_subshell ();
711 #endif
713 #endif /* TIOCGWINSZ */
714 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
717 void
718 sigwinch_handler (int dummy)
720 (void) dummy;
721 #if !(defined(USE_NCURSES) || defined(USE_NCURSESW)) /* don't do malloc in a signal handler */
722 low_level_change_screen_size ();
723 #endif
724 winch_flag = 1;
727 void
728 change_screen_size (void)
730 Dlg_head *d;
732 winch_flag = 0;
733 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
734 #if defined TIOCGWINSZ
736 #ifndef NCURSES_VERSION
737 tty_noraw_mode ();
738 tty_reset_screen ();
739 #endif
740 low_level_change_screen_size ();
741 #ifdef HAVE_SLANG
742 /* XSI Curses spec states that portable applications shall not invoke
743 * initscr() more than once. This kludge could be done within the scope
744 * of the specification by using endwin followed by a refresh (in fact,
745 * more than one curses implementation does this); it is guaranteed to work
746 * only with slang.
748 SLsmg_init_smg ();
749 do_enter_ca_mode ();
750 tty_keypad (TRUE);
751 tty_nodelay (FALSE);
752 #endif
754 /* Inform all running dialogs */
755 d = current_dlg;
756 while (d != NULL) {
757 (*d->callback) (d, NULL, DLG_RESIZE, 0, NULL);
758 d = d->parent;
761 /* Now, force the redraw */
762 repaint_screen ();
763 #endif /* TIOCGWINSZ */
764 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
767 static int ok_to_refresh = 1;
769 void use_dash (int flag)
771 if (flag)
772 ok_to_refresh++;
773 else
774 ok_to_refresh--;
777 void set_hintbar(const char *str)
779 label_set_text (the_hint, str);
780 if (ok_to_refresh > 0)
781 mc_refresh();
784 void print_vfs_message (const char *msg, ...)
786 va_list ap;
787 char str [128];
789 va_start (ap, msg);
790 g_vsnprintf (str, sizeof (str), msg, ap);
791 va_end (ap);
793 if (midnight_shutdown)
794 return;
796 if (!message_visible || !the_hint || !the_hint->widget.parent) {
797 int col, row;
799 if (!nice_rotating_dash || (ok_to_refresh <= 0))
800 return;
802 /* Preserve current cursor position */
803 tty_getyx (&row, &col);
805 tty_gotoyx (0, 0);
806 tty_setcolor (NORMAL_COLOR);
807 tty_print_string (str_fit_to_term (str, COLS - 1, J_LEFT));
809 /* Restore cursor position */
810 tty_gotoyx (row, col);
811 mc_refresh ();
812 return;
815 if (message_visible) {
816 set_hintbar(str);
820 void rotate_dash (void)
822 static const char rotating_dash [] = "|/-\\";
823 static size_t pos = 0;
825 if (!nice_rotating_dash || (ok_to_refresh <= 0))
826 return;
828 if (pos >= sizeof (rotating_dash)-1)
829 pos = 0;
830 tty_gotoyx (0, COLS - 1);
831 tty_setcolor (NORMAL_COLOR);
832 tty_print_char (rotating_dash [pos]);
833 mc_refresh ();
834 pos++;
837 const char *get_nth_panel_name (int num)
839 static char buffer [BUF_SMALL];
841 if (!num)
842 return "New Left Panel";
843 else if (num == 1)
844 return "New Right Panel";
845 else {
846 g_snprintf (buffer, sizeof (buffer), "%ith Panel", num);
847 return buffer;
851 /* I wonder if I should start to use the folding mode than Dugan uses */
852 /* */
853 /* This is the centralized managing of the panel display types */
854 /* This routine takes care of destroying and creating new widgets */
855 /* Please note that it could manage MAX_VIEWS, not just left and right */
856 /* Currently nothing in the code takes advantage of this and has hard- */
857 /* coded values for two panels only */
859 /* Set the num-th panel to the view type: type */
860 /* This routine also keeps at least one WPanel object in the screen */
861 /* since a lot of routines depend on the current_panel variable */
862 void
863 set_display_type (int num, panel_view_mode_t type)
865 int x = 0, y = 0, cols = 0, lines = 0;
866 int the_other = 0; /* Index to the other panel */
867 const char *file_name = NULL; /* For Quick view */
868 Widget *new_widget = NULL, *old_widget = NULL;
869 WPanel *the_other_panel = NULL;
871 if (num >= MAX_VIEWS){
872 fprintf (stderr, "Cannot allocate more that %d views\n", MAX_VIEWS);
873 abort ();
875 /* Check that we will have a WPanel * at least */
876 if (type != view_listing) {
877 the_other = num == 0 ? 1 : 0;
879 if (panels [the_other].type != view_listing)
880 return;
883 /* Get rid of it */
884 if (panels [num].widget) {
885 Widget *w = panels [num].widget;
886 WPanel *panel = (WPanel *) panels [num].widget;
888 x = w->x;
889 y = w->y;
890 cols = w->cols;
891 lines = w->lines;
892 old_widget = panels [num].widget;
894 if (panels [num].type == view_listing) {
895 if (panel->frame_size == frame_full && type != view_listing) {
896 cols = COLS - first_panel_size;
897 if (num == 1)
898 x = first_panel_size;
903 /* Restoring saved path from panels.ini for nonlist panel */
904 /* when it's first creation (for example view_info) */
905 if (old_widget == NULL && type != view_listing) {
906 char panel_dir [MC_MAXPATHLEN];
907 mc_get_current_wd (panel_dir, sizeof (panel_dir));
908 panels[num].last_saved_dir = g_strdup (panel_dir);
911 switch (type) {
912 case view_nothing:
913 case view_listing:
914 new_widget = restore_into_right_dir_panel (num, old_widget);
915 break;
917 case view_info:
918 new_widget = (Widget *) info_new ();
919 break;
921 case view_tree:
922 new_widget = (Widget *) tree_new (1, 0, 0, 0, 0);
923 break;
925 case view_quick:
926 new_widget = (Widget *) mcview_new (0, 0, 0, 0, 1);
927 the_other_panel = (WPanel *) panels [the_other].widget;
928 if (the_other_panel)
929 file_name = the_other_panel->dir.list[the_other_panel->selected].fname;
930 else
931 file_name = "";
933 mcview_load ((struct mcview_struct *) new_widget, 0, file_name, 0);
934 break;
937 if (type != view_listing)
938 /* Must save dir, for restoring after change type to */
939 /* view_listing */
940 save_panel_dir (num);
942 panels [num].type = type;
943 panels [num].widget = new_widget;
945 /* We set the same size the old widget had */
946 widget_set_size (new_widget, y, x, lines, cols);
948 /* We use replace to keep the circular list of the dialog in the */
949 /* same state. Maybe we could just kill it and then replace it */
950 if ((midnight_dlg != NULL) && (old_widget != NULL))
951 dlg_replace_widget (old_widget, panels [num].widget);
953 if (type == view_listing) {
954 if (num == 0)
955 left_panel = (WPanel *) new_widget;
956 else
957 right_panel = (WPanel *) new_widget;
960 if (type == view_tree)
961 the_tree = (WTree *) new_widget;
963 /* Prevent current_panel's value from becoming invalid.
964 * It's just a quick hack to prevent segfaults. Comment out and
965 * try following:
966 * - select left panel
967 * - invoke menue left/tree
968 * - as long as you stay in the left panel almost everything that uses
969 * current_panel causes segfault, e.g. C-Enter, C-x c, ...
972 if ((type != view_listing)
973 && (current_panel == (WPanel *) old_widget))
974 current_panel = num == 0 ? right_panel : left_panel;
977 /* This routine is deeply sticked to the two panels idea.
978 What should it do in more panels. ANSWER - don't use it
979 in any multiple panels environment. */
980 void swap_panels ()
982 Widget tmp;
983 Widget *tmp_widget;
984 WPanel panel;
985 WPanel *panel1, *panel2;
986 int tmp_type;
988 #define panelswap(x) panel. x = panel1-> x; panel1-> x = panel2-> x; panel2-> x = panel. x;
990 #define panelswapstr(e) strcpy (panel. e, panel1-> e); \
991 strcpy (panel1-> e, panel2-> e); \
992 strcpy (panel2-> e, panel. e);
993 panel1 = (WPanel *) panels [0].widget;
994 panel2 = (WPanel *) panels [1].widget;
995 if (panels [0].type == view_listing && panels [1].type == view_listing) {
996 /* Change everything except format/sort/panel_name etc. */
997 panelswap (dir);
998 panelswap (active);
999 panelswapstr (cwd);
1000 panelswapstr (lwd);
1001 panelswap (count);
1002 panelswap (marked);
1003 panelswap (dirs_marked);
1004 panelswap (total);
1005 panelswap (top_file);
1006 panelswap (selected);
1007 panelswap (is_panelized);
1008 panelswap (dir_stat);
1010 panel1->searching = 0;
1011 panel2->searching = 0;
1012 if (current_panel == panel1)
1013 current_panel = panel2;
1014 else
1015 current_panel = panel1;
1017 if (dlg_widget_active (panels[0].widget))
1018 dlg_select_widget (panels [1].widget);
1019 else if (dlg_widget_active (panels[1].widget))
1020 dlg_select_widget (panels [0].widget);
1021 } else {
1022 WPanel *tmp_panel;
1024 tmp_panel = right_panel;
1025 right_panel = left_panel;
1026 left_panel = tmp_panel;
1028 if (panels [0].type == view_listing) {
1029 if (!strcmp (panel1->panel_name, get_nth_panel_name (0))) {
1030 g_free (panel1->panel_name);
1031 panel1->panel_name = g_strdup (get_nth_panel_name (1));
1034 if (panels [1].type == view_listing) {
1035 if (!strcmp (panel2->panel_name, get_nth_panel_name (1))) {
1036 g_free (panel2->panel_name);
1037 panel2->panel_name = g_strdup (get_nth_panel_name (0));
1041 tmp.x = panels [0].widget->x;
1042 tmp.y = panels [0].widget->y;
1043 tmp.cols = panels [0].widget->cols;
1044 tmp.lines = panels [0].widget->lines;
1046 panels [0].widget->x = panels [1].widget->x;
1047 panels [0].widget->y = panels [1].widget->y;
1048 panels [0].widget->cols = panels [1].widget->cols;
1049 panels [0].widget->lines = panels [1].widget->lines;
1051 panels [1].widget->x = tmp.x;
1052 panels [1].widget->y = tmp.y;
1053 panels [1].widget->cols = tmp.cols;
1054 panels [1].widget->lines = tmp.lines;
1056 tmp_widget = panels [0].widget;
1057 panels [0].widget = panels [1].widget;
1058 panels [1].widget = tmp_widget;
1059 tmp_type = panels [0].type;
1060 panels [0].type = panels [1].type;
1061 panels [1].type = tmp_type;
1065 panel_view_mode_t
1066 get_display_type (int idx)
1068 return panels [idx].type;
1071 struct Widget *
1072 get_panel_widget (int idx)
1074 return panels[idx].widget;
1077 int get_current_index (void)
1079 if (panels [0].widget == ((Widget *) current_panel))
1080 return 0;
1081 else
1082 return 1;
1085 int get_other_index (void)
1087 return !get_current_index ();
1090 struct WPanel *
1091 get_other_panel (void)
1093 return (struct WPanel *) get_panel_widget (get_other_index ());
1096 /* Returns the view type for the current panel/view */
1097 panel_view_mode_t
1098 get_current_type (void)
1100 if (panels [0].widget == (Widget *) current_panel)
1101 return panels [0].type;
1102 else
1103 return panels [1].type;
1106 /* Returns the view type of the unselected panel */
1107 panel_view_mode_t
1108 get_other_type (void)
1110 if (panels [0].widget == (Widget *) current_panel)
1111 return panels [1].type;
1112 else
1113 return panels [0].type;
1116 /* Save current list_view widget directory into panel */
1117 void
1118 save_panel_dir (int idx)
1120 panel_view_mode_t type = get_display_type (idx);
1121 Widget *widget = get_panel_widget (idx);
1123 if ((type == view_listing) && (widget != NULL)) {
1124 WPanel *w = (WPanel *) widget;
1125 char *widget_work_dir = w->cwd;
1127 g_free(panels [idx].last_saved_dir); /* last path no needed */
1128 /* Because path can be nonlocal */
1129 panels [idx].last_saved_dir = vfs_translate_url (widget_work_dir);
1133 /* Save current list_view widget directory into panel */
1134 Widget *
1135 restore_into_right_dir_panel (int idx, Widget *from_widget)
1137 Widget *new_widget = NULL;
1138 const char *saved_dir = panels [idx].last_saved_dir;
1139 gboolean last_was_panel = (from_widget &&
1140 get_display_type(idx) != view_listing);
1141 const char *p_name = get_nth_panel_name (idx);
1143 if (last_was_panel)
1144 new_widget = (Widget *) panel_new_with_dir (p_name, saved_dir);
1145 else
1146 new_widget = (Widget *) panel_new (p_name);
1148 return new_widget;
1151 /* Return working dir, if it's view_listing - cwd,
1152 but for other types - last_saved_dir */
1153 const char *
1154 get_panel_dir_for (const WPanel *widget)
1156 int i;
1158 for (i = 0; i < MAX_VIEWS; i++)
1159 if ((WPanel *) get_panel_widget (i) == widget)
1160 break;
1162 if (i >= MAX_VIEWS)
1163 return ".";
1165 if (get_display_type (i) == view_listing)
1166 return ((WPanel *) get_panel_widget (i))->cwd;
1168 return panels[i].last_saved_dir;