Moved dir $(srcdir)/syntax into $(srcdir)/misc/syntax
[midnight-commander.git] / src / layout.c
blob1570fa6500372fc8df79f7805a84ca05509ecaf3
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 "global.h"
49 #include "../src/tty/tty.h"
50 #include "../src/skin/skin.h"
51 #include "../src/tty/key.h"
52 #include "../src/tty/mouse.h"
53 #include "../src/tty/win.h" /* do_enter_ca_mode() */
55 #include "../src/mcconfig/mcconfig.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 "strutil.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 "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() */
76 #include "../../lib/vfs/mc-vfs/vfs.h" /* For vfs_translate_url() */
78 /* Controls the display of the rotating dash on the verbose mode */
79 int nice_rotating_dash = 1;
81 /* Set if the panels are split horizontally */
82 int horizontal_split = 0;
84 /* Set if the window has changed it's size */
85 int winch_flag = 0;
87 /* Set if the split is the same */
88 int equal_split = 1;
90 /* First panel size if the panel are not split equally */
91 int first_panel_size = 0;
93 /* The number of output lines shown (if available) */
94 int output_lines = 0;
96 /* Set if the command prompt is to be displayed */
97 int command_prompt = 1;
99 /* Set if the nice and useful keybar is visible */
100 int keybar_visible = 1;
102 /* Set if the nice message (hint) bar is visible */
103 int message_visible = 1;
105 /* Set to show current working dir in xterm window title */
106 int xterm_title = 1;
108 /* Set to show free space on device assigned to current directory */
109 int free_space = 1;
111 /* The starting line for the output of the subprogram */
112 int output_start_y = 0;
114 /* The maximum number of views managed by the set_display_type routine */
115 /* Must be at least two (for current and other). Please note that until */
116 /* Janne gets around this, we will only manage two of them :-) */
117 #define MAX_VIEWS 2
119 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] = {
124 /* init MAX_VIEWS items */
125 { view_listing, NULL, NULL },
126 { view_listing, NULL, NULL }
129 /* These variables are used to avoid updating the information unless */
130 /* we need it */
131 static int old_first_panel_size;
132 static int old_horizontal_split;
133 static int old_output_lines;
135 /* Internal variables */
136 static int _horizontal_split;
137 static int _equal_split;
138 static int _first_panel_size;
139 static int _menubar_visible;
140 static int _output_lines;
141 static int _command_prompt;
142 static int _keybar_visible;
143 static int _message_visible;
144 static int _xterm_title;
145 static int _free_space;
146 static int _permission_mode;
147 static int _filetype_mode;
149 static int height;
151 /* Width 12 for a wee Quick (Hex) View */
152 #define MINWIDTH 12
153 #define MINHEIGHT 5
155 #define BY 12
157 #define B_2LEFT B_USER
158 #define B_2RIGHT (B_USER + 1)
159 #define B_PLUS (B_USER + 2)
160 #define B_MINUS (B_USER + 3)
162 static Dlg_head *layout_dlg;
164 static const char *s_split_direction [2] = {
165 N_("&Vertical"),
166 N_("&Horizontal")
169 static WRadio *radio_widget;
171 static struct {
172 const char *text;
173 int *variable;
174 WCheck *widget;
175 } check_options [] = {
176 { N_("show free sp&Ace"), &free_space, 0 },
177 { N_("&Xterm window title"), &xterm_title, 0 },
178 { N_("h&Intbar visible"), &message_visible, 0 },
179 { N_("&Keybar visible"), &keybar_visible, 0 },
180 { N_("command &Prompt"), &command_prompt, 0 },
181 { N_("show &Mini status"), &show_mini_info, 0 },
182 { N_("menu&Bar visible"), &menubar_visible, 0 },
183 { N_("&Equal split"), &equal_split, 0 },
184 { N_("pe&Rmissions"), &permission_mode, 0 },
185 { N_("&File types"), &filetype_mode, 0 },
186 { 0, 0, 0 }
189 #define LAYOUT_OPTIONS_COUNT 10
190 #define HIGHLIGHT_OPTIONS_COUNT 2
191 #define SPLIT_OPTIONS_COUNT 1
192 #define OTHER_OPTIONS_COUNT 7
194 static gsize first_width, second_width;
195 static const char *output_lines_label;
197 static WButton *bleft_widget, *bright_widget;
199 static void _check_split (void)
201 if (_horizontal_split){
202 if (_equal_split)
203 _first_panel_size = height / 2;
204 else if (_first_panel_size < MINHEIGHT)
205 _first_panel_size = MINHEIGHT;
206 else if (_first_panel_size > height - MINHEIGHT)
207 _first_panel_size = height - MINHEIGHT;
208 } else {
209 if (_equal_split)
210 _first_panel_size = COLS / 2;
211 else if (_first_panel_size < MINWIDTH)
212 _first_panel_size = MINWIDTH;
213 else if (_first_panel_size > COLS - MINWIDTH)
214 _first_panel_size = COLS - MINWIDTH;
218 static void update_split (void)
220 /* Check split has to be done before testing if it changed, since
221 it can change due to calling _check_split() as well*/
222 _check_split ();
224 /* To avoid setting the cursor to the wrong place */
225 if ((old_first_panel_size == _first_panel_size) &&
226 (old_horizontal_split == _horizontal_split)){
227 return;
230 old_first_panel_size = _first_panel_size;
231 old_horizontal_split = _horizontal_split;
233 tty_setcolor (COLOR_NORMAL);
234 dlg_move (layout_dlg, 6, 6);
235 tty_printf ("%03d", _first_panel_size);
236 dlg_move (layout_dlg, 6, 18);
237 if (_horizontal_split)
238 tty_printf ("%03d", height - _first_panel_size);
239 else
240 tty_printf ("%03d", COLS - _first_panel_size);
243 static int b2left_cback (int action)
245 (void) action;
247 if (_equal_split){
248 /* Turn equal split off */
249 _equal_split = 0;
250 check_options [7].widget->state = check_options [7].widget->state & ~C_BOOL;
251 dlg_select_widget (check_options [7].widget);
252 dlg_select_widget (bleft_widget);
254 _first_panel_size++;
255 return 0;
258 static int b2right_cback (int action)
260 (void) action;
262 if (_equal_split){
263 /* Turn equal split off */
264 _equal_split = 0;
265 check_options [7].widget->state = check_options [7].widget->state & ~C_BOOL;
266 dlg_select_widget (check_options [7].widget);
267 dlg_select_widget (bright_widget);
269 _first_panel_size--;
270 return 0;
273 static int bplus_cback (int action)
275 (void) action;
277 if (_output_lines < 99)
278 _output_lines++;
279 return 0;
282 static int bminus_cback (int action)
284 (void) action;
286 if (_output_lines > 0)
287 _output_lines--;
288 return 0;
291 static cb_ret_t
292 layout_callback (Dlg_head *h, Widget *sender,
293 dlg_msg_t msg, int parm, void *data)
295 switch (msg) {
296 case DLG_DRAW:
297 /*When repainting the whole dialog (e.g. with C-l) we have to
298 update everything*/
299 common_dialog_repaint (h);
301 old_first_panel_size = -1;
302 old_horizontal_split = -1;
303 old_output_lines = -1;
305 tty_setcolor (COLOR_HOT_NORMAL);
306 update_split ();
307 dlg_move (h, 6, 13);
308 tty_print_char ('=');
309 if (console_flag){
310 if (old_output_lines != _output_lines){
311 old_output_lines = _output_lines;
312 tty_setcolor (COLOR_NORMAL);
313 dlg_move (h, LAYOUT_OPTIONS_COUNT, 16 + first_width);
314 tty_print_string (output_lines_label);
315 dlg_move (h, LAYOUT_OPTIONS_COUNT, 10 + first_width);
316 tty_printf ("%02d", _output_lines);
319 return MSG_HANDLED;
321 case DLG_POST_KEY:
322 _filetype_mode = check_options [9].widget->state & C_BOOL;
323 _permission_mode = check_options [8].widget->state & C_BOOL;
324 _equal_split = check_options [7].widget->state & C_BOOL;
325 _menubar_visible = check_options [6].widget->state & C_BOOL;
326 _command_prompt = check_options [5].widget->state & C_BOOL;
327 _keybar_visible = check_options [3].widget->state & C_BOOL;
328 _message_visible = check_options [2].widget->state & C_BOOL;
329 _xterm_title = check_options [1].widget->state & C_BOOL;
330 _free_space = check_options [0].widget->state & C_BOOL;
331 if (console_flag){
332 int minimum;
333 if (_output_lines < 0)
334 _output_lines = 0;
335 height = LINES - _keybar_visible - _command_prompt -
336 _menubar_visible - _output_lines - _message_visible;
337 minimum = MINHEIGHT * (1 + _horizontal_split);
338 if (height < minimum){
339 _output_lines -= minimum - height;
340 height = minimum;
342 } else {
343 height = LINES - _keybar_visible - _command_prompt -
344 _menubar_visible - _output_lines - _message_visible;
346 if (_horizontal_split != radio_widget->sel){
347 _horizontal_split = radio_widget->sel;
348 if (_horizontal_split)
349 _first_panel_size = height / 2;
350 else
351 _first_panel_size = COLS / 2;
353 update_split ();
354 if (console_flag){
355 if (old_output_lines != _output_lines){
356 old_output_lines = _output_lines;
357 tty_setcolor (COLOR_NORMAL);
358 dlg_move (h, LAYOUT_OPTIONS_COUNT, 10 + first_width);
359 tty_printf ("%02d", _output_lines);
362 return MSG_HANDLED;
364 default:
365 return default_dlg_callback (h, sender, msg, parm, data);
369 static void
370 init_layout (void)
372 static int i18n_layt_flag = 0;
373 static int b1, b2, b3;
374 int i = sizeof (s_split_direction) / sizeof (char *);
375 const char *ok_button = _("&OK");
376 const char *cancel_button = _("&Cancel");
377 const char *save_button = _("&Save");
378 static const char *title1, *title2, *title3;
380 if (!i18n_layt_flag) {
381 gsize l1;
383 first_width = 19; /* length of line with '<' '>' buttons */
385 title1 = _(" Panel split ");
386 title2 = _(" Highlight... ");
387 title3 = _(" Other options ");
388 output_lines_label = _("output lines");
390 while (i--) {
391 s_split_direction[i] = _(s_split_direction[i]);
392 l1 = str_term_width1 (s_split_direction[i]) + 7;
393 if (l1 > first_width)
394 first_width = l1;
397 for (i = 0; i < LAYOUT_OPTIONS_COUNT; i++) {
398 check_options[i].text = _(check_options[i].text);
399 l1 = str_term_width1 (check_options[i].text) + 7;
400 if (l1 > first_width)
401 first_width = l1;
404 l1 = str_term_width1 (title1) + 1;
405 if (l1 > first_width)
406 first_width = l1;
408 l1 = str_term_width1 (title2) + 1;
409 if (l1 > first_width)
410 first_width = l1;
412 second_width = str_term_width1 (title3) + 1;
413 for (i = 0; i < OTHER_OPTIONS_COUNT; i++) {
414 check_options[i].text = _(check_options[i].text);
415 l1 = str_term_width1 (check_options[i].text) + 7;
416 if (l1 > second_width)
417 second_width = l1;
419 if (console_flag) {
420 l1 = str_term_width1 (output_lines_label) + 13;
421 if (l1 > second_width)
422 second_width = l1;
426 * alex@bcs.zp.ua:
427 * To be completely correct, one need to check if the title
428 * does not exceed dialog length and total length of 3 buttons
429 * allows their placement in one row. But assuming this dialog
430 * is wide enough, I don't include such a tests.
432 * Now the last thing to do - properly space buttons...
434 l1 = 11 + str_term_width1 (ok_button) /* 14 - all brackets and inner space */
435 + str_term_width1 (save_button) /* notice: it is 3 char less because */
436 + str_term_width1 (cancel_button); /* of '&' char in button text */
438 i = (first_width + second_width - l1) / 4;
439 b1 = 5 + i;
440 b2 = b1 + str_term_width1 (ok_button) + i + 6;
441 b3 = b2 + str_term_width1 (save_button) + i + 4;
443 i18n_layt_flag = 1;
446 layout_dlg =
447 create_dlg (0, 0, 15, first_width + second_width + 9,
448 dialog_colors, layout_callback, "[Layout]",
449 _("Layout"), DLG_CENTER | DLG_REVERSE);
451 add_widget (layout_dlg, groupbox_new (2, 4, 6, first_width, title1));
452 add_widget (layout_dlg, groupbox_new (8, 4, 4, first_width, title2));
453 add_widget (layout_dlg,
454 groupbox_new (2, 5 + first_width, 10, second_width,
455 title3));
457 add_widget (layout_dlg,
458 button_new (BY, b3, B_CANCEL, NORMAL_BUTTON, cancel_button,
459 0));
460 add_widget (layout_dlg,
461 button_new (BY, b2, B_EXIT, NORMAL_BUTTON, save_button,
462 0));
463 add_widget (layout_dlg,
464 button_new (BY, b1, B_ENTER, DEFPUSH_BUTTON, ok_button,
465 0));
466 if (console_flag) {
467 add_widget (layout_dlg,
468 button_new (LAYOUT_OPTIONS_COUNT, 12 + first_width, B_MINUS,
469 NARROW_BUTTON, "&-", bminus_cback));
470 add_widget (layout_dlg,
471 button_new (LAYOUT_OPTIONS_COUNT, 7 + first_width, B_PLUS, NARROW_BUTTON,
472 "&+", bplus_cback));
474 #define XTRACT(i) *check_options[i].variable, check_options[i].text
476 for (i = 0; i < OTHER_OPTIONS_COUNT; i++) {
477 check_options[i].widget =
478 check_new (LAYOUT_OPTIONS_COUNT - i - 1, 7 + first_width, XTRACT (i));
479 add_widget (layout_dlg, check_options[i].widget);
481 check_options[9].widget = check_new (10, 6, XTRACT (9));
482 add_widget (layout_dlg, check_options[9].widget);
483 check_options[8].widget = check_new (9, 6, XTRACT (8));
484 add_widget (layout_dlg, check_options[8].widget);
486 _filetype_mode = filetype_mode;
487 _permission_mode = permission_mode;
488 _equal_split = equal_split;
489 _menubar_visible = menubar_visible;
490 _command_prompt = command_prompt;
491 _keybar_visible = keybar_visible;
492 _message_visible = message_visible;
493 _xterm_title = xterm_title;
494 _free_space = free_space;
495 bright_widget =
496 button_new (6, 15, B_2RIGHT, NARROW_BUTTON, "&>", b2right_cback);
497 add_widget (layout_dlg, bright_widget);
498 bleft_widget =
499 button_new (6, 9, B_2LEFT, NARROW_BUTTON, "&<", b2left_cback);
500 add_widget (layout_dlg, bleft_widget);
501 check_options[7].widget = check_new (5, 6, XTRACT (7));
502 old_first_panel_size = -1;
503 old_horizontal_split = -1;
504 old_output_lines = -1;
506 _first_panel_size = first_panel_size;
507 _output_lines = output_lines;
508 add_widget (layout_dlg, check_options[7].widget);
509 radio_widget = radio_new (3, 6, 2, s_split_direction);
510 add_widget (layout_dlg, radio_widget);
511 radio_widget->sel = horizontal_split;
514 static void
515 layout_change (void)
517 setup_panels ();
518 /* re-init the menu, because perhaps there was a change in the way
519 how the panel are split (horizontal/vertical). */
520 done_menu ();
521 init_menu ();
522 menubar_arrange (the_menubar);
523 load_hint (1);
526 void layout_cmd (void)
528 int result;
529 int i;
530 int layout_do_change = 0;
532 init_layout ();
533 run_dlg (layout_dlg);
534 result = layout_dlg->ret_value;
536 if (result == B_ENTER || result == B_EXIT){
537 for (i = 0; check_options [i].text; i++)
538 if (check_options [i].widget)
539 *check_options [i].variable = check_options [i].widget->state & C_BOOL;
540 horizontal_split = radio_widget->sel;
541 first_panel_size = _first_panel_size;
542 output_lines = _output_lines;
543 layout_do_change = 1;
545 if (result == B_EXIT){
546 save_layout ();
547 mc_config_save_file (mc_main_config, NULL);
550 destroy_dlg (layout_dlg);
551 if (layout_do_change)
552 layout_change ();
555 static void check_split (void)
557 if (horizontal_split){
558 if (equal_split)
559 first_panel_size = height / 2;
560 else if (first_panel_size < MINHEIGHT)
561 first_panel_size = MINHEIGHT;
562 else if (first_panel_size > height - MINHEIGHT)
563 first_panel_size = height - MINHEIGHT;
564 } else {
565 if (equal_split)
566 first_panel_size = COLS / 2;
567 else if (first_panel_size < MINWIDTH)
568 first_panel_size = MINWIDTH;
569 else if (first_panel_size > COLS - MINWIDTH)
570 first_panel_size = COLS - MINWIDTH;
574 void
575 clr_scr (void)
577 tty_set_normal_attrs ();
578 tty_fill_region (0, 0, LINES, COLS, ' ');
579 tty_refresh ();
582 void
583 repaint_screen (void)
585 do_refresh ();
586 tty_refresh ();
589 void
590 mc_refresh (void)
592 #ifdef WITH_BACKGROUND
593 if (we_are_background)
594 return;
595 #endif /* WITH_BACKGROUND */
596 if (winch_flag == 0)
597 tty_refresh ();
598 else {
599 /* if winch was caugth, we should do not only redraw screen, but
600 reposition/resize all */
601 change_screen_size ();
605 static void
606 panel_do_cols (int lc_index)
608 if (get_display_type (lc_index) == view_listing)
609 set_panel_formats ((WPanel *) panels [lc_index].widget);
610 else
611 panel_update_cols (panels [lc_index].widget, frame_half);
614 void
615 setup_panels (void)
617 int start_y;
618 int promptl; /* the prompt len */
620 if (console_flag) {
621 int minimum;
622 if (output_lines < 0)
623 output_lines = 0;
624 height =
625 LINES - keybar_visible - command_prompt - menubar_visible -
626 output_lines - message_visible;
627 minimum = MINHEIGHT * (1 + horizontal_split);
628 if (height < minimum) {
629 output_lines -= minimum - height;
630 height = minimum;
632 } else {
633 height =
634 LINES - menubar_visible - command_prompt - keybar_visible -
635 message_visible;
637 check_split ();
638 start_y = menubar_visible;
640 /* The column computing is defered until panel_do_cols */
641 if (horizontal_split) {
642 widget_set_size (panels[0].widget, start_y, 0, first_panel_size,
645 widget_set_size (panels[1].widget, start_y + first_panel_size, 0,
646 height - first_panel_size, 0);
647 } else {
648 int first_x = first_panel_size;
650 widget_set_size (panels[0].widget, start_y, 0, height, 0);
652 widget_set_size (panels[1].widget, start_y, first_x, height, 0);
655 panel_do_cols (0);
656 panel_do_cols (1);
658 promptl = str_term_width1 (prompt);
660 widget_set_size (&the_menubar->widget, 0, 0, 1, COLS);
662 if (command_prompt) {
663 widget_set_size (&cmdline->widget, LINES - 1 - keybar_visible,
664 promptl, 1, COLS - promptl);
665 winput_set_origin (cmdline, promptl, COLS - promptl);
666 widget_set_size (&the_prompt->widget, LINES - 1 - keybar_visible,
667 0, 1, promptl);
668 } else {
669 widget_set_size (&cmdline->widget, 0, 0, 0, 0);
670 winput_set_origin (cmdline, 0, 0);
671 widget_set_size (&the_prompt->widget, LINES, COLS, 0, 0);
674 widget_set_size (&the_bar->widget, LINES - 1, 0, keybar_visible, COLS);
675 buttonbar_set_visible (the_bar, keybar_visible);
677 /* Output window */
678 if (console_flag && output_lines) {
679 output_start_y =
680 LINES - command_prompt - keybar_visible - output_lines;
681 show_console_contents (output_start_y,
682 LINES - output_lines - keybar_visible - 1,
683 LINES - keybar_visible - 1);
685 if (message_visible)
686 widget_set_size (&the_hint->widget, height + start_y, 0, 1, COLS);
687 else
688 widget_set_size (&the_hint->widget, 0, 0, 0, 0);
690 update_xterm_title_path ();
693 static inline void
694 low_level_change_screen_size (void)
696 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
697 #if defined TIOCGWINSZ
698 struct winsize winsz;
700 winsz.ws_col = winsz.ws_row = 0;
701 /* Ioctl on the STDIN_FILENO */
702 ioctl (0, TIOCGWINSZ, &winsz);
703 if (winsz.ws_col && winsz.ws_row){
704 #if defined(NCURSES_VERSION) && defined(HAVE_RESIZETERM)
705 resizeterm(winsz.ws_row, winsz.ws_col);
706 clearok(stdscr,TRUE); /* sigwinch's should use a semaphore! */
707 #else
708 COLS = winsz.ws_col;
709 LINES = winsz.ws_row;
710 #endif
711 #ifdef HAVE_SUBSHELL_SUPPORT
712 resize_subshell ();
713 #endif
715 #endif /* TIOCGWINSZ */
716 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
719 void
720 sigwinch_handler (int dummy)
722 (void) dummy;
723 #if !(defined(USE_NCURSES) || defined(USE_NCURSESW)) /* don't do malloc in a signal handler */
724 low_level_change_screen_size ();
725 #endif
726 winch_flag = 1;
729 void
730 change_screen_size (void)
732 Dlg_head *d;
734 winch_flag = 0;
735 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
736 #if defined TIOCGWINSZ
738 #ifndef NCURSES_VERSION
739 tty_noraw_mode ();
740 tty_reset_screen ();
741 #endif
742 low_level_change_screen_size ();
743 #ifdef HAVE_SLANG
744 /* XSI Curses spec states that portable applications shall not invoke
745 * initscr() more than once. This kludge could be done within the scope
746 * of the specification by using endwin followed by a refresh (in fact,
747 * more than one curses implementation does this); it is guaranteed to work
748 * only with slang.
750 SLsmg_init_smg ();
751 do_enter_ca_mode ();
752 tty_keypad (TRUE);
753 tty_nodelay (FALSE);
754 #endif
756 /* Inform all running dialogs */
757 d = current_dlg;
758 while (d != NULL) {
759 (*d->callback) (d, NULL, DLG_RESIZE, 0, NULL);
760 d = d->parent;
763 /* Now, force the redraw */
764 repaint_screen ();
765 #endif /* TIOCGWINSZ */
766 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
769 static int ok_to_refresh = 1;
771 void use_dash (int flag)
773 if (flag)
774 ok_to_refresh++;
775 else
776 ok_to_refresh--;
779 void set_hintbar(const char *str)
781 label_set_text (the_hint, str);
782 if (ok_to_refresh > 0)
783 mc_refresh();
786 void print_vfs_message (const char *msg, ...)
788 va_list ap;
789 char str [128];
791 va_start (ap, msg);
792 g_vsnprintf (str, sizeof (str), msg, ap);
793 va_end (ap);
795 if (midnight_shutdown)
796 return;
798 if (!message_visible || !the_hint || !the_hint->widget.parent) {
799 int col, row;
801 if (!nice_rotating_dash || (ok_to_refresh <= 0))
802 return;
804 /* Preserve current cursor position */
805 tty_getyx (&row, &col);
807 tty_gotoyx (0, 0);
808 tty_setcolor (NORMAL_COLOR);
809 tty_print_string (str_fit_to_term (str, COLS - 1, J_LEFT));
811 /* Restore cursor position */
812 tty_gotoyx (row, col);
813 mc_refresh ();
814 return;
817 if (message_visible) {
818 set_hintbar(str);
822 void rotate_dash (void)
824 static const char rotating_dash [] = "|/-\\";
825 static size_t pos = 0;
827 if (!nice_rotating_dash || (ok_to_refresh <= 0))
828 return;
830 if (pos >= sizeof (rotating_dash)-1)
831 pos = 0;
832 tty_gotoyx (0, COLS - 1);
833 tty_setcolor (NORMAL_COLOR);
834 tty_print_char (rotating_dash [pos]);
835 mc_refresh ();
836 pos++;
839 const char *get_nth_panel_name (int num)
841 static char buffer [BUF_SMALL];
843 if (!num)
844 return "New Left Panel";
845 else if (num == 1)
846 return "New Right Panel";
847 else {
848 g_snprintf (buffer, sizeof (buffer), "%ith Panel", num);
849 return buffer;
853 /* I wonder if I should start to use the folding mode than Dugan uses */
854 /* */
855 /* This is the centralized managing of the panel display types */
856 /* This routine takes care of destroying and creating new widgets */
857 /* Please note that it could manage MAX_VIEWS, not just left and right */
858 /* Currently nothing in the code takes advantage of this and has hard- */
859 /* coded values for two panels only */
861 /* Set the num-th panel to the view type: type */
862 /* This routine also keeps at least one WPanel object in the screen */
863 /* since a lot of routines depend on the current_panel variable */
864 void
865 set_display_type (int num, panel_view_mode_t type)
867 int x = 0, y = 0, cols = 0, lines = 0;
868 int the_other = 0; /* Index to the other panel */
869 const char *file_name = NULL; /* For Quick view */
870 Widget *new_widget = NULL, *old_widget = NULL;
871 WPanel *the_other_panel = NULL;
873 if (num >= MAX_VIEWS){
874 fprintf (stderr, "Cannot allocate more that %d views\n", MAX_VIEWS);
875 abort ();
877 /* Check that we will have a WPanel * at least */
878 if (type != view_listing) {
879 the_other = num == 0 ? 1 : 0;
881 if (panels [the_other].type != view_listing)
882 return;
885 /* Get rid of it */
886 if (panels [num].widget) {
887 Widget *w = panels [num].widget;
888 WPanel *panel = (WPanel *) panels [num].widget;
890 x = w->x;
891 y = w->y;
892 cols = w->cols;
893 lines = w->lines;
894 old_widget = panels [num].widget;
896 if (panels [num].type == view_listing) {
897 if (panel->frame_size == frame_full && type != view_listing) {
898 cols = COLS - first_panel_size;
899 if (num == 1)
900 x = first_panel_size;
905 /* Restoring saved path from panels.ini for nonlist panel */
906 /* when it's first creation (for example view_info) */
907 if (old_widget == NULL && type != view_listing) {
908 char panel_dir [MC_MAXPATHLEN];
909 mc_get_current_wd (panel_dir, sizeof (panel_dir));
910 panels[num].last_saved_dir = g_strdup (panel_dir);
913 switch (type) {
914 case view_listing:
915 new_widget = restore_into_right_dir_panel (num, old_widget);
916 break;
918 case view_info:
919 new_widget = (Widget *) info_new ();
920 break;
922 case view_tree:
923 new_widget = (Widget *) tree_new (1, 0, 0, 0, 0);
924 break;
926 case view_quick:
927 new_widget = (Widget *) mcview_new (0, 0, 0, 0, 1);
928 the_other_panel = (WPanel *) panels [the_other].widget;
929 if (the_other_panel)
930 file_name = the_other_panel->dir.list[the_other_panel->selected].fname;
931 else
932 file_name = "";
934 mcview_load ((struct mcview_struct *) new_widget, 0, file_name, 0);
935 break;
938 if (type != view_listing)
939 /* Must save dir, for restoring after change type to */
940 /* view_listing */
941 save_panel_dir (num);
943 panels [num].type = type;
944 panels [num].widget = new_widget;
946 /* We set the same size the old widget had */
947 widget_set_size (new_widget, y, x, lines, cols);
949 /* We use replace to keep the circular list of the dialog in the */
950 /* same state. Maybe we could just kill it and then replace it */
951 if ((midnight_dlg != NULL) && (old_widget != NULL))
952 dlg_replace_widget (old_widget, panels [num].widget);
954 if (type == view_listing) {
955 if (num == 0)
956 left_panel = (WPanel *) new_widget;
957 else
958 right_panel = (WPanel *) new_widget;
961 if (type == view_tree)
962 the_tree = (WTree *) new_widget;
964 /* Prevent current_panel's value from becoming invalid.
965 * It's just a quick hack to prevent segfaults. Comment out and
966 * try following:
967 * - select left panel
968 * - invoke menue left/tree
969 * - as long as you stay in the left panel almost everything that uses
970 * current_panel causes segfault, e.g. C-Enter, C-x c, ...
973 if ((type != view_listing)
974 && (current_panel == (WPanel *) old_widget))
975 current_panel = num == 0 ? right_panel : left_panel;
978 /* This routine is deeply sticked to the two panels idea.
979 What should it do in more panels. ANSWER - don't use it
980 in any multiple panels environment. */
981 void swap_panels ()
983 Widget tmp;
984 Widget *tmp_widget;
985 WPanel panel;
986 WPanel *panel1, *panel2;
987 int tmp_type;
989 #define panelswap(x) panel. x = panel1-> x; panel1-> x = panel2-> x; panel2-> x = panel. x;
991 #define panelswapstr(e) strcpy (panel. e, panel1-> e); \
992 strcpy (panel1-> e, panel2-> e); \
993 strcpy (panel2-> e, panel. e);
994 panel1 = (WPanel *) panels [0].widget;
995 panel2 = (WPanel *) panels [1].widget;
996 if (panels [0].type == view_listing && panels [1].type == view_listing) {
997 /* Change everything except format/sort/panel_name etc. */
998 panelswap (dir);
999 panelswap (active);
1000 panelswapstr (cwd);
1001 panelswapstr (lwd);
1002 panelswap (count);
1003 panelswap (marked);
1004 panelswap (dirs_marked);
1005 panelswap (total);
1006 panelswap (top_file);
1007 panelswap (selected);
1008 panelswap (is_panelized);
1009 panelswap (dir_stat);
1011 panel1->searching = 0;
1012 panel2->searching = 0;
1013 if (current_panel == panel1)
1014 current_panel = panel2;
1015 else
1016 current_panel = panel1;
1018 if (dlg_widget_active (panels[0].widget))
1019 dlg_select_widget (panels [1].widget);
1020 else if (dlg_widget_active (panels[1].widget))
1021 dlg_select_widget (panels [0].widget);
1022 } else {
1023 WPanel *tmp_panel;
1025 tmp_panel = right_panel;
1026 right_panel = left_panel;
1027 left_panel = tmp_panel;
1029 if (panels [0].type == view_listing) {
1030 if (!strcmp (panel1->panel_name, get_nth_panel_name (0))) {
1031 g_free (panel1->panel_name);
1032 panel1->panel_name = g_strdup (get_nth_panel_name (1));
1035 if (panels [1].type == view_listing) {
1036 if (!strcmp (panel2->panel_name, get_nth_panel_name (1))) {
1037 g_free (panel2->panel_name);
1038 panel2->panel_name = g_strdup (get_nth_panel_name (0));
1042 tmp.x = panels [0].widget->x;
1043 tmp.y = panels [0].widget->y;
1044 tmp.cols = panels [0].widget->cols;
1045 tmp.lines = panels [0].widget->lines;
1047 panels [0].widget->x = panels [1].widget->x;
1048 panels [0].widget->y = panels [1].widget->y;
1049 panels [0].widget->cols = panels [1].widget->cols;
1050 panels [0].widget->lines = panels [1].widget->lines;
1052 panels [1].widget->x = tmp.x;
1053 panels [1].widget->y = tmp.y;
1054 panels [1].widget->cols = tmp.cols;
1055 panels [1].widget->lines = tmp.lines;
1057 tmp_widget = panels [0].widget;
1058 panels [0].widget = panels [1].widget;
1059 panels [1].widget = tmp_widget;
1060 tmp_type = panels [0].type;
1061 panels [0].type = panels [1].type;
1062 panels [1].type = tmp_type;
1066 panel_view_mode_t
1067 get_display_type (int lc_index)
1069 return panels [lc_index].type;
1072 struct Widget *
1073 get_panel_widget (int lc_index)
1075 return panels[lc_index].widget;
1078 int get_current_index (void)
1080 if (panels [0].widget == ((Widget *) current_panel))
1081 return 0;
1082 else
1083 return 1;
1086 int get_other_index (void)
1088 return !get_current_index ();
1091 struct WPanel *
1092 get_other_panel (void)
1094 return (struct WPanel *) get_panel_widget (get_other_index ());
1097 /* Returns the view type for the current panel/view */
1098 panel_view_mode_t
1099 get_current_type (void)
1101 if (panels [0].widget == (Widget *) current_panel)
1102 return panels [0].type;
1103 else
1104 return panels [1].type;
1107 /* Returns the view type of the unselected panel */
1108 panel_view_mode_t
1109 get_other_type (void)
1111 if (panels [0].widget == (Widget *) current_panel)
1112 return panels [1].type;
1113 else
1114 return panels [0].type;
1117 /* Save current list_view widget directory into panel */
1118 void
1119 save_panel_dir (int lc_index)
1121 panel_view_mode_t type = get_display_type (lc_index);
1122 Widget *widget = get_panel_widget (lc_index);
1124 if ((type == view_listing) && (widget != NULL)) {
1125 WPanel *w = (WPanel *) widget;
1126 char *widget_work_dir = w->cwd;
1128 g_free(panels [lc_index].last_saved_dir); /* last path no needed */
1129 /* Because path can be nonlocal */
1130 panels [lc_index].last_saved_dir = vfs_translate_url(widget_work_dir);
1134 /* Save current list_view widget directory into panel */
1135 Widget *
1136 restore_into_right_dir_panel (int lc_index, Widget *from_widget)
1138 Widget *new_widget = NULL;
1139 const char *saved_dir = panels [lc_index].last_saved_dir;
1140 gboolean last_was_panel = (from_widget &&
1141 get_display_type(lc_index) != view_listing);
1142 const char *p_name = get_nth_panel_name (lc_index);
1144 if (last_was_panel)
1145 new_widget = (Widget *) panel_new_with_dir (p_name, saved_dir);
1146 else
1147 new_widget = (Widget *) panel_new (p_name);
1149 return new_widget;
1152 /* Return working dir, if it's view_listing - cwd,
1153 but for other types - last_saved_dir */
1154 const char *
1155 get_panel_dir_for (const WPanel *widget)
1157 int i;
1159 for (i = 0; i < MAX_VIEWS; i++)
1160 if ((WPanel *) get_panel_widget (i) == widget)
1161 break;
1163 if (i >= MAX_VIEWS)
1164 return ".";
1166 if (get_display_type (i) == view_listing)
1167 return ((WPanel *) get_panel_widget (i))->cwd;
1169 return panels[i].last_saved_dir;