done_screen() is moved from src/layout.c to src/main.c.
[midnight-commander.git] / src / layout.c
blob85b4820f9bbd8e6a6a9602acf52e59535a9d1999
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 #ifdef HAVE_TERMIOS_H
45 #include <termios.h>
46 #endif
47 #include <unistd.h>
49 #include "global.h"
51 #include "../src/tty/tty.h"
52 #include "../src/tty/color.h"
53 #include "../src/tty/key.h"
54 #include "../src/tty/mouse.h"
56 #include "dialog.h"
57 #include "widget.h"
58 #include "command.h"
59 #include "../src/mcconfig/mcconfig.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"
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 "view.h" /* The view widget */
75 #include "setup.h" /* For save_setup() */
77 /* Controls the display of the rotating dash on the verbose mode */
78 int nice_rotating_dash = 1;
80 /* Set if the panels are split horizontally */
81 int horizontal_split = 0;
83 /* Set if the window has changed it's size */
84 int winch_flag = 0;
86 /* Set if the split is the same */
87 int equal_split = 1;
89 /* First panel size if the panel are not split equally */
90 int first_panel_size = 0;
92 /* The number of output lines shown (if available) */
93 int output_lines = 0;
95 /* Set if the command prompt is to be displayed */
96 int command_prompt = 1;
98 /* Set if the nice and useful keybar is visible */
99 int keybar_visible = 1;
101 /* Set if the nice message (hint) bar is visible */
102 int message_visible = 1;
104 /* Set to show current working dir in xterm window title */
105 int xterm_title = 1;
107 /* Set to show free space on device assigned to current directory */
108 int free_space = 1;
110 /* The starting line for the output of the subprogram */
111 int output_start_y = 0;
113 /* The maximum number of views managed by the set_display_type routine */
114 /* Must be at least two (for current and other). Please note that until */
115 /* Janne gets around this, we will only manage two of them :-) */
116 #define MAX_VIEWS 2
118 static struct {
119 int type;
120 Widget *widget;
121 } panels [MAX_VIEWS];
123 /* These variables are used to avoid updating the information unless */
124 /* we need it */
125 static int old_first_panel_size;
126 static int old_horizontal_split;
127 static int old_output_lines;
129 /* Internal variables */
130 static int _horizontal_split;
131 static int _equal_split;
132 static int _first_panel_size;
133 static int _menubar_visible;
134 static int _output_lines;
135 static int _command_prompt;
136 static int _keybar_visible;
137 static int _message_visible;
138 static int _xterm_title;
139 static int _free_space;
140 static int _permission_mode;
141 static int _filetype_mode;
143 static int height;
145 /* Width 12 for a wee Quick (Hex) View */
146 #define MINWIDTH 12
147 #define MINHEIGHT 5
149 #define BY 12
151 #define B_2LEFT B_USER
152 #define B_2RIGHT (B_USER + 1)
153 #define B_PLUS (B_USER + 2)
154 #define B_MINUS (B_USER + 3)
156 static Dlg_head *layout_dlg;
158 static const char *s_split_direction [2] = {
159 N_("&Vertical"),
160 N_("&Horizontal")
163 static WRadio *radio_widget;
165 static struct {
166 const char *text;
167 int *variable;
168 WCheck *widget;
169 } check_options [] = {
170 { N_("show free sp&Ace"), &free_space, 0 },
171 { N_("&Xterm window title"), &xterm_title, 0 },
172 { N_("h&Intbar visible"), &message_visible, 0 },
173 { N_("&Keybar visible"), &keybar_visible, 0 },
174 { N_("command &Prompt"), &command_prompt, 0 },
175 { N_("show &Mini status"), &show_mini_info, 0 },
176 { N_("menu&Bar visible"), &menubar_visible, 0 },
177 { N_("&Equal split"), &equal_split, 0 },
178 { N_("pe&Rmissions"), &permission_mode, 0 },
179 { N_("&File types"), &filetype_mode, 0 },
180 { 0, 0, 0 }
183 #define LAYOUT_OPTIONS_COUNT 10
184 #define HIGHLIGHT_OPTIONS_COUNT 2
185 #define SPLIT_OPTIONS_COUNT 1
186 #define OTHER_OPTIONS_COUNT 7
188 static gsize first_width, second_width;
189 static const char *output_lines_label;
191 static WButton *bleft_widget, *bright_widget;
193 /* Declarations for static functions */
194 static void low_level_change_screen_size (void);
196 static void _check_split (void)
198 if (_horizontal_split){
199 if (_equal_split)
200 _first_panel_size = height / 2;
201 else if (_first_panel_size < MINHEIGHT)
202 _first_panel_size = MINHEIGHT;
203 else if (_first_panel_size > height - MINHEIGHT)
204 _first_panel_size = height - MINHEIGHT;
205 } else {
206 if (_equal_split)
207 _first_panel_size = COLS / 2;
208 else if (_first_panel_size < MINWIDTH)
209 _first_panel_size = MINWIDTH;
210 else if (_first_panel_size > COLS - MINWIDTH)
211 _first_panel_size = COLS - MINWIDTH;
215 static void update_split (void)
217 /* Check split has to be done before testing if it changed, since
218 it can change due to calling _check_split() as well*/
219 _check_split ();
221 /* To avoid setting the cursor to the wrong place */
222 if ((old_first_panel_size == _first_panel_size) &&
223 (old_horizontal_split == _horizontal_split)){
224 return;
227 old_first_panel_size = _first_panel_size;
228 old_horizontal_split = _horizontal_split;
230 tty_setcolor (COLOR_NORMAL);
231 dlg_move (layout_dlg, 6, 6);
232 tty_printf ("%03d", _first_panel_size);
233 dlg_move (layout_dlg, 6, 18);
234 if (_horizontal_split)
235 tty_printf ("%03d", height - _first_panel_size);
236 else
237 tty_printf ("%03d", COLS - _first_panel_size);
240 static int b2left_cback (int action)
242 (void) action;
244 if (_equal_split){
245 /* Turn equal split off */
246 _equal_split = 0;
247 check_options [7].widget->state = check_options [7].widget->state & ~C_BOOL;
248 dlg_select_widget (check_options [7].widget);
249 dlg_select_widget (bleft_widget);
251 _first_panel_size++;
252 return 0;
255 static int b2right_cback (int action)
257 (void) action;
259 if (_equal_split){
260 /* Turn equal split off */
261 _equal_split = 0;
262 check_options [7].widget->state = check_options [7].widget->state & ~C_BOOL;
263 dlg_select_widget (check_options [7].widget);
264 dlg_select_widget (bright_widget);
266 _first_panel_size--;
267 return 0;
270 static int bplus_cback (int action)
272 (void) action;
274 if (_output_lines < 99)
275 _output_lines++;
276 return 0;
279 static int bminus_cback (int action)
281 (void) action;
283 if (_output_lines > 0)
284 _output_lines--;
285 return 0;
288 static cb_ret_t
289 layout_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
291 switch (msg) {
292 case DLG_DRAW:
293 /*When repainting the whole dialog (e.g. with C-l) we have to
294 update everything*/
295 common_dialog_repaint (h);
297 old_first_panel_size = -1;
298 old_horizontal_split = -1;
299 old_output_lines = -1;
301 tty_setcolor (COLOR_HOT_NORMAL);
302 update_split ();
303 dlg_move (h, 6, 13);
304 tty_print_char ('=');
305 if (console_flag){
306 if (old_output_lines != _output_lines){
307 old_output_lines = _output_lines;
308 tty_setcolor (COLOR_NORMAL);
309 dlg_move (h, LAYOUT_OPTIONS_COUNT, 16 + first_width);
310 tty_print_string (output_lines_label);
311 dlg_move (h, LAYOUT_OPTIONS_COUNT, 10 + first_width);
312 tty_printf ("%02d", _output_lines);
315 return MSG_HANDLED;
317 case DLG_POST_KEY:
318 _filetype_mode = check_options [9].widget->state & C_BOOL;
319 _permission_mode = check_options [8].widget->state & C_BOOL;
320 _equal_split = check_options [7].widget->state & C_BOOL;
321 _menubar_visible = check_options [6].widget->state & C_BOOL;
322 _command_prompt = check_options [5].widget->state & C_BOOL;
323 _keybar_visible = check_options [3].widget->state & C_BOOL;
324 _message_visible = check_options [2].widget->state & C_BOOL;
325 _xterm_title = check_options [1].widget->state & C_BOOL;
326 _free_space = check_options [0].widget->state & C_BOOL;
327 if (console_flag){
328 int minimum;
329 if (_output_lines < 0)
330 _output_lines = 0;
331 height = LINES - _keybar_visible - _command_prompt -
332 _menubar_visible - _output_lines - _message_visible;
333 minimum = MINHEIGHT * (1 + _horizontal_split);
334 if (height < minimum){
335 _output_lines -= minimum - height;
336 height = minimum;
338 } else {
339 height = LINES - _keybar_visible - _command_prompt -
340 _menubar_visible - _output_lines - _message_visible;
342 if (_horizontal_split != radio_widget->sel){
343 _horizontal_split = radio_widget->sel;
344 if (_horizontal_split)
345 _first_panel_size = height / 2;
346 else
347 _first_panel_size = COLS / 2;
349 update_split ();
350 if (console_flag){
351 if (old_output_lines != _output_lines){
352 old_output_lines = _output_lines;
353 tty_setcolor (COLOR_NORMAL);
354 dlg_move (h, LAYOUT_OPTIONS_COUNT, 10 + first_width);
355 tty_printf ("%02d", _output_lines);
358 return MSG_HANDLED;
360 default:
361 return default_dlg_callback (h, msg, parm);
365 static void
366 init_layout (void)
368 static int i18n_layt_flag = 0;
369 static int b1, b2, b3;
370 int i = sizeof (s_split_direction) / sizeof (char *);
371 const char *ok_button = _("&OK");
372 const char *cancel_button = _("&Cancel");
373 const char *save_button = _("&Save");
374 static const char *title1, *title2, *title3;
376 if (!i18n_layt_flag) {
377 gsize l1;
379 first_width = 19; /* length of line with '<' '>' buttons */
381 title1 = _(" Panel split ");
382 title2 = _(" Highlight... ");
383 title3 = _(" Other options ");
384 output_lines_label = _("output lines");
386 while (i--) {
387 s_split_direction[i] = _(s_split_direction[i]);
388 l1 = str_term_width1 (s_split_direction[i]) + 7;
389 if (l1 > first_width)
390 first_width = l1;
393 for (i = 0; i < LAYOUT_OPTIONS_COUNT; i++) {
394 check_options[i].text = _(check_options[i].text);
395 l1 = str_term_width1 (check_options[i].text) + 7;
396 if (l1 > first_width)
397 first_width = l1;
400 l1 = str_term_width1 (title1) + 1;
401 if (l1 > first_width)
402 first_width = l1;
404 l1 = str_term_width1 (title2) + 1;
405 if (l1 > first_width)
406 first_width = l1;
408 second_width = str_term_width1 (title3) + 1;
409 for (i = 0; i < OTHER_OPTIONS_COUNT; i++) {
410 check_options[i].text = _(check_options[i].text);
411 l1 = str_term_width1 (check_options[i].text) + 7;
412 if (l1 > second_width)
413 second_width = l1;
415 if (console_flag) {
416 l1 = str_term_width1 (output_lines_label) + 13;
417 if (l1 > second_width)
418 second_width = l1;
422 * alex@bcs.zp.ua:
423 * To be completely correct, one need to check if the title
424 * does not exceed dialog length and total length of 3 buttons
425 * allows their placement in one row. But assuming this dialog
426 * is wide enough, I don't include such a tests.
428 * Now the last thing to do - properly space buttons...
430 l1 = 11 + str_term_width1 (ok_button) /* 14 - all brackets and inner space */
431 + str_term_width1 (save_button) /* notice: it is 3 char less because */
432 + str_term_width1 (cancel_button); /* of '&' char in button text */
434 i = (first_width + second_width - l1) / 4;
435 b1 = 5 + i;
436 b2 = b1 + str_term_width1 (ok_button) + i + 6;
437 b3 = b2 + str_term_width1 (save_button) + i + 4;
439 i18n_layt_flag = 1;
442 layout_dlg =
443 create_dlg (0, 0, 15, first_width + second_width + 9,
444 dialog_colors, layout_callback, "[Layout]",
445 _("Layout"), DLG_CENTER | DLG_REVERSE);
447 add_widget (layout_dlg, groupbox_new (2, 4, 6, first_width, title1));
448 add_widget (layout_dlg, groupbox_new (8, 4, 4, first_width, title2));
449 add_widget (layout_dlg,
450 groupbox_new (2, 5 + first_width, 10, second_width,
451 title3));
453 add_widget (layout_dlg,
454 button_new (BY, b3, B_CANCEL, NORMAL_BUTTON, cancel_button,
455 0));
456 add_widget (layout_dlg,
457 button_new (BY, b2, B_EXIT, NORMAL_BUTTON, save_button,
458 0));
459 add_widget (layout_dlg,
460 button_new (BY, b1, B_ENTER, DEFPUSH_BUTTON, ok_button,
461 0));
462 if (console_flag) {
463 add_widget (layout_dlg,
464 button_new (LAYOUT_OPTIONS_COUNT, 12 + first_width, B_MINUS,
465 NARROW_BUTTON, "&-", bminus_cback));
466 add_widget (layout_dlg,
467 button_new (LAYOUT_OPTIONS_COUNT, 7 + first_width, B_PLUS, NARROW_BUTTON,
468 "&+", bplus_cback));
470 #define XTRACT(i) *check_options[i].variable, check_options[i].text
472 for (i = 0; i < OTHER_OPTIONS_COUNT; i++) {
473 check_options[i].widget =
474 check_new (LAYOUT_OPTIONS_COUNT - i - 1, 7 + first_width, XTRACT (i));
475 add_widget (layout_dlg, check_options[i].widget);
477 check_options[9].widget = check_new (10, 6, XTRACT (9));
478 add_widget (layout_dlg, check_options[9].widget);
479 check_options[8].widget = check_new (9, 6, XTRACT (8));
480 add_widget (layout_dlg, check_options[8].widget);
482 _filetype_mode = filetype_mode;
483 _permission_mode = permission_mode;
484 _equal_split = equal_split;
485 _menubar_visible = menubar_visible;
486 _command_prompt = command_prompt;
487 _keybar_visible = keybar_visible;
488 _message_visible = message_visible;
489 _xterm_title = xterm_title;
490 _free_space = free_space;
491 bright_widget =
492 button_new (6, 15, B_2RIGHT, NARROW_BUTTON, "&>", b2right_cback);
493 add_widget (layout_dlg, bright_widget);
494 bleft_widget =
495 button_new (6, 9, B_2LEFT, NARROW_BUTTON, "&<", b2left_cback);
496 add_widget (layout_dlg, bleft_widget);
497 check_options[7].widget = check_new (5, 6, XTRACT (7));
498 old_first_panel_size = -1;
499 old_horizontal_split = -1;
500 old_output_lines = -1;
502 _first_panel_size = first_panel_size;
503 _output_lines = output_lines;
504 add_widget (layout_dlg, check_options[7].widget);
505 radio_widget = radio_new (3, 6, 2, s_split_direction);
506 add_widget (layout_dlg, radio_widget);
507 radio_widget->sel = horizontal_split;
510 static void
511 layout_change (void)
513 setup_panels ();
514 /* re-init the menu, because perhaps there was a change in the way
515 how the panel are split (horizontal/vertical). */
516 done_menu ();
517 init_menu ();
518 menubar_arrange (the_menubar);
521 void layout_cmd (void)
523 int result;
524 int i;
525 int layout_do_change = 0;
527 init_layout ();
528 run_dlg (layout_dlg);
529 result = layout_dlg->ret_value;
531 if (result == B_ENTER || result == B_EXIT){
532 for (i = 0; check_options [i].text; i++)
533 if (check_options [i].widget)
534 *check_options [i].variable = check_options [i].widget->state & C_BOOL;
535 horizontal_split = radio_widget->sel;
536 first_panel_size = _first_panel_size;
537 output_lines = _output_lines;
538 layout_do_change = 1;
540 if (result == B_EXIT){
541 save_layout ();
542 mc_config_save_file (mc_main_config);
545 destroy_dlg (layout_dlg);
546 if (layout_do_change)
547 layout_change ();
550 static void check_split (void)
552 if (horizontal_split){
553 if (equal_split)
554 first_panel_size = height / 2;
555 else if (first_panel_size < MINHEIGHT)
556 first_panel_size = MINHEIGHT;
557 else if (first_panel_size > height - MINHEIGHT)
558 first_panel_size = height - MINHEIGHT;
559 } else {
560 if (equal_split)
561 first_panel_size = COLS / 2;
562 else if (first_panel_size < MINWIDTH)
563 first_panel_size = MINWIDTH;
564 else if (first_panel_size > COLS - MINWIDTH)
565 first_panel_size = COLS - MINWIDTH;
569 void
570 clr_scr (void)
572 tty_set_normal_attrs ();
573 dlg_erase (midnight_dlg);
574 tty_refresh ();
577 static void
578 panel_do_cols (int index)
580 if (get_display_type (index) == view_listing)
581 set_panel_formats ((WPanel *) panels [index].widget);
582 else {
583 panel_update_cols (panels [index].widget, frame_half);
587 void
588 setup_panels (void)
590 int start_y;
591 int promptl; /* the prompt len */
593 if (console_flag) {
594 int minimum;
595 if (output_lines < 0)
596 output_lines = 0;
597 height =
598 LINES - keybar_visible - command_prompt - menubar_visible -
599 output_lines - message_visible;
600 minimum = MINHEIGHT * (1 + horizontal_split);
601 if (height < minimum) {
602 output_lines -= minimum - height;
603 height = minimum;
605 } else {
606 height =
607 LINES - menubar_visible - command_prompt - keybar_visible -
608 message_visible;
610 check_split ();
611 start_y = menubar_visible;
613 /* The column computing is defered until panel_do_cols */
614 if (horizontal_split) {
615 widget_set_size (panels[0].widget, start_y, 0, first_panel_size,
618 widget_set_size (panels[1].widget, start_y + first_panel_size, 0,
619 height - first_panel_size, 0);
620 } else {
621 int first_x = first_panel_size;
623 widget_set_size (panels[0].widget, start_y, 0, height, 0);
625 widget_set_size (panels[1].widget, start_y, first_x, height, 0);
628 panel_do_cols (0);
629 panel_do_cols (1);
631 promptl = str_term_width1 (prompt);
633 widget_set_size (&the_menubar->widget, 0, 0, 1, COLS);
635 if (command_prompt) {
636 widget_set_size (&cmdline->widget, LINES - 1 - keybar_visible,
637 promptl, 1,
638 COLS - promptl - (keybar_visible ? 0 : 1));
639 winput_set_origin (cmdline, promptl,
640 COLS - promptl - (keybar_visible ? 0 : 1));
641 widget_set_size (&the_prompt->widget, LINES - 1 - keybar_visible,
642 0, 1, promptl);
643 } else {
644 widget_set_size (&cmdline->widget, 0, 0, 0, 0);
645 winput_set_origin (cmdline, 0, 0);
646 widget_set_size (&the_prompt->widget, LINES, COLS, 0, 0);
649 widget_set_size ((Widget *) the_bar, LINES - 1, 0, keybar_visible, COLS);
650 buttonbar_set_visible (the_bar, keybar_visible);
652 /* Output window */
653 if (console_flag && output_lines) {
654 output_start_y =
655 LINES - command_prompt - keybar_visible - output_lines;
656 show_console_contents (output_start_y,
657 LINES - output_lines - keybar_visible - 1,
658 LINES - keybar_visible - 1);
660 if (message_visible) {
661 widget_set_size (&the_hint->widget, height + start_y, 0, 1, COLS);
662 set_hintbar (""); /* clean up the line */
663 } else
664 widget_set_size (&the_hint->widget, 0, 0, 0, 0);
666 load_hint (1);
667 update_xterm_title_path ();
670 void flag_winch (int dummy)
672 (void) dummy;
673 #if !(defined(USE_NCURSES) || defined(USE_NCURSESW)) /* don't do malloc in a signal handler */
674 low_level_change_screen_size ();
675 #endif
676 winch_flag = 1;
679 static void
680 low_level_change_screen_size (void)
682 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
683 #if defined TIOCGWINSZ
684 struct winsize winsz;
686 winsz.ws_col = winsz.ws_row = 0;
687 /* Ioctl on the STDIN_FILENO */
688 ioctl (0, TIOCGWINSZ, &winsz);
689 if (winsz.ws_col && winsz.ws_row){
690 #if defined(NCURSES_VERSION) && defined(HAVE_RESIZETERM)
691 resizeterm(winsz.ws_row, winsz.ws_col);
692 clearok(stdscr,TRUE); /* sigwinch's should use a semaphore! */
693 #else
694 COLS = winsz.ws_col;
695 LINES = winsz.ws_row;
696 #endif
697 #ifdef HAVE_SUBSHELL_SUPPORT
698 resize_subshell ();
699 #endif
701 #endif /* TIOCGWINSZ */
702 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
705 void
706 change_screen_size (void)
708 winch_flag = 0;
709 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
710 #if defined TIOCGWINSZ
712 #ifndef NCURSES_VERSION
713 tty_noraw_mode ();
714 tty_reset_screen ();
715 #endif
716 low_level_change_screen_size ();
717 check_split ();
718 #ifndef NCURSES_VERSION
719 /* XSI Curses spec states that portable applications shall not invoke
720 * initscr() more than once. This kludge could be done within the scope
721 * of the specification by using endwin followed by a refresh (in fact,
722 * more than one curses implementation does this); it is guaranteed to work
723 * only with slang.
725 init_curses ();
726 #endif
727 setup_panels ();
729 /* Inform currently running dialog */
730 (*current_dlg->callback) (current_dlg, DLG_RESIZE, 0);
732 #ifdef RESIZABLE_MENUBAR
733 menubar_arrange (the_menubar);
734 #endif
736 /* Now, force the redraw */
737 do_refresh ();
738 tty_touch_screen ();
739 #endif /* TIOCGWINSZ */
740 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
743 static int ok_to_refresh = 1;
745 void use_dash (int flag)
747 if (flag)
748 ok_to_refresh++;
749 else
750 ok_to_refresh--;
753 void set_hintbar(const char *str)
755 label_set_text (the_hint, str);
756 if (ok_to_refresh > 0)
757 tty_refresh();
760 void print_vfs_message (const char *msg, ...)
762 va_list ap;
763 char str [128];
765 va_start (ap, msg);
767 g_vsnprintf (str, sizeof (str), msg, ap);
768 va_end (ap);
770 if (midnight_shutdown)
771 return;
773 if (!message_visible || !the_hint || !the_hint->widget.parent) {
774 int col, row;
776 if (!nice_rotating_dash || (ok_to_refresh <= 0))
777 return;
779 /* Preserve current cursor position */
780 tty_getyx (&row, &col);
782 tty_gotoyx (0, 0);
783 tty_setcolor (NORMAL_COLOR);
784 tty_print_string (str_fit_to_term (str, COLS - 1, J_LEFT));
786 /* Restore cursor position */
787 tty_gotoyx (row, col);
788 tty_refresh ();
789 return;
792 if (message_visible) {
793 set_hintbar(str);
797 void rotate_dash (void)
799 static const char rotating_dash [] = "|/-\\";
800 static size_t pos = 0;
802 if (!nice_rotating_dash || (ok_to_refresh <= 0))
803 return;
805 if (pos >= sizeof (rotating_dash)-1)
806 pos = 0;
807 tty_gotoyx (0, COLS - 1);
808 tty_setcolor (NORMAL_COLOR);
809 tty_print_char (rotating_dash [pos]);
810 tty_refresh ();
811 pos++;
814 const char *get_nth_panel_name (int num)
816 static char buffer [BUF_SMALL];
818 if (!num)
819 return "New Left Panel";
820 else if (num == 1)
821 return "New Right Panel";
822 else {
823 g_snprintf (buffer, sizeof (buffer), "%ith Panel", num);
824 return buffer;
828 /* I wonder if I should start to use the folding mode than Dugan uses */
829 /* */
830 /* This is the centralized managing of the panel display types */
831 /* This routine takes care of destroying and creating new widgets */
832 /* Please note that it could manage MAX_VIEWS, not just left and right */
833 /* Currently nothing in the code takes advantage of this and has hard- */
834 /* coded values for two panels only */
836 /* Set the num-th panel to the view type: type */
837 /* This routine also keeps at least one WPanel object in the screen */
838 /* since a lot of routines depend on the current_panel variable */
839 void set_display_type (int num, int type)
841 int x, y, cols, lines;
842 int the_other; /* Index to the other panel */
843 const char *file_name = NULL; /* For Quick view */
844 Widget *new_widget, *old_widget;
845 WPanel *the_other_panel;
847 x = y = cols = lines = 0;
848 old_widget = 0;
849 if (num >= MAX_VIEWS){
850 fprintf (stderr, "Cannot allocate more that %d views\n", MAX_VIEWS);
851 abort ();
854 /* Check that we will have a WPanel * at least */
855 the_other = 0;
856 if (type != view_listing){
857 the_other = num == 0 ? 1 : 0;
859 if (panels [the_other].type != view_listing)
860 return;
864 /* Get rid of it */
865 if (panels [num].widget){
866 Widget *w = panels [num].widget;
867 WPanel *panel = (WPanel *) panels [num].widget;
869 x = w->x;
870 y = w->y;
871 cols = w->cols;
872 lines = w->lines;
873 old_widget = panels [num].widget;
875 if (panels [num].type == view_listing){
876 if (panel->frame_size == frame_full && type != view_listing){
877 cols = COLS - first_panel_size;
878 if (num == 1)
879 x = first_panel_size;
884 new_widget = 0;
886 switch (type){
887 case view_listing:
888 new_widget = (Widget *) panel_new (get_nth_panel_name (num));
889 break;
891 case view_info:
892 new_widget = (Widget *) info_new ();
894 break;
896 case view_tree:
897 new_widget = (Widget *) tree_new (1, 0, 0, 0, 0);
898 break;
900 case view_quick:
901 new_widget = (Widget *) view_new (0, 0, 0, 0, 1);
902 the_other_panel = (WPanel *) panels [the_other].widget;
903 if (the_other_panel)
904 file_name =
905 the_other_panel->dir.list[the_other_panel->selected].fname;
906 else
907 file_name = "";
909 view_load ((WView *) new_widget, 0, file_name, 0);
910 break;
912 panels [num].type = type;
913 panels [num].widget = (Widget *) new_widget;
915 /* We set the same size the old widget had */
916 widget_set_size ((Widget *) new_widget, y, x, lines, cols);
918 /* We use replace to keep the circular list of the dialog in the */
919 /* same state. Maybe we could just kill it and then replace it */
920 if (midnight_dlg && old_widget){
921 dlg_replace_widget (old_widget, panels [num].widget);
923 if (type == view_listing){
924 if (num == 0)
925 left_panel = (WPanel *) new_widget;
926 else
927 right_panel = (WPanel *) new_widget;
930 if (type == view_tree)
931 the_tree = (WTree *) new_widget;
933 /* Prevent current_panel's value from becoming invalid.
934 * It's just a quick hack to prevent segfaults. Comment out and
935 * try following:
936 * - select left panel
937 * - invoke menue left/tree
938 * - as long as you stay in the left panel almost everything that uses
939 * current_panel causes segfault, e.g. C-Enter, C-x c, ...
942 if (type != view_listing)
943 if (current_panel == (WPanel *) old_widget)
944 current_panel = num == 0 ? right_panel : left_panel;
947 /* This routine is deeply sticked to the two panels idea.
948 What should it do in more panels. ANSWER - don't use it
949 in any multiple panels environment. */
950 void swap_panels ()
952 Widget tmp;
953 Widget *tmp_widget;
954 WPanel panel;
955 WPanel *panel1, *panel2;
956 int tmp_type;
958 #define panelswap(x) panel. x = panel1-> x; panel1-> x = panel2-> x; panel2-> x = panel. x;
960 #define panelswapstr(e) strcpy (panel. e, panel1-> e); \
961 strcpy (panel1-> e, panel2-> e); \
962 strcpy (panel2-> e, panel. e);
963 panel1 = (WPanel *) panels [0].widget;
964 panel2 = (WPanel *) panels [1].widget;
965 if (panels [0].type == view_listing && panels [1].type == view_listing) {
966 /* Change everything except format/sort/panel_name etc. */
967 panelswap (dir);
968 panelswap (active);
969 panelswapstr (cwd);
970 panelswapstr (lwd);
971 panelswap (count);
972 panelswap (marked);
973 panelswap (dirs_marked);
974 panelswap (total);
975 panelswap (top_file);
976 panelswap (selected);
977 panelswap (is_panelized);
978 panelswap (dir_stat);
980 panel1->searching = 0;
981 panel2->searching = 0;
982 if (current_panel == panel1)
983 current_panel = panel2;
984 else
985 current_panel = panel1;
987 if (dlg_widget_active (panels[0].widget))
988 dlg_select_widget (panels [1].widget);
989 else if (dlg_widget_active (panels[1].widget))
990 dlg_select_widget (panels [0].widget);
991 } else {
992 WPanel *tmp_panel;
994 tmp_panel=right_panel;
995 right_panel=left_panel;
996 left_panel=tmp_panel;
998 if (panels [0].type == view_listing) {
999 if (!strcmp (panel1->panel_name, get_nth_panel_name (0))) {
1000 g_free (panel1->panel_name);
1001 panel1->panel_name = g_strdup (get_nth_panel_name (1));
1004 if (panels [1].type == view_listing) {
1005 if (!strcmp (panel2->panel_name, get_nth_panel_name (1))) {
1006 g_free (panel2->panel_name);
1007 panel2->panel_name = g_strdup (get_nth_panel_name (0));
1011 tmp.x = panels [0].widget->x;
1012 tmp.y = panels [0].widget->y;
1013 tmp.cols = panels [0].widget->cols;
1014 tmp.lines = panels [0].widget->lines;
1016 panels [0].widget->x = panels [1].widget->x;
1017 panels [0].widget->y = panels [1].widget->y;
1018 panels [0].widget->cols = panels [1].widget->cols;
1019 panels [0].widget->lines = panels [1].widget->lines;
1021 panels [1].widget->x = tmp.x;
1022 panels [1].widget->y = tmp.y;
1023 panels [1].widget->cols = tmp.cols;
1024 panels [1].widget->lines = tmp.lines;
1026 tmp_widget = panels [0].widget;
1027 panels [0].widget = panels [1].widget;
1028 panels [1].widget = tmp_widget;
1029 tmp_type = panels [0].type;
1030 panels [0].type = panels [1].type;
1031 panels [1].type = tmp_type;
1035 int get_display_type (int index)
1037 return panels [index].type;
1040 struct Widget *
1041 get_panel_widget (int index)
1043 return panels[index].widget;
1046 int get_current_index (void)
1048 if (panels [0].widget == ((Widget *) current_panel))
1049 return 0;
1050 else
1051 return 1;
1054 int get_other_index (void)
1056 return !get_current_index ();
1059 struct WPanel *
1060 get_other_panel (void)
1062 return (struct WPanel *) get_panel_widget (get_other_index ());
1065 /* Returns the view type for the current panel/view */
1066 int get_current_type (void)
1068 if (panels [0].widget == (Widget *) current_panel)
1069 return panels [0].type;
1070 else
1071 return panels [1].type;
1074 /* Returns the view type of the unselected panel */
1075 int get_other_type (void)
1077 if (panels [0].widget == (Widget *) current_panel)
1078 return panels [1].type;
1079 else
1080 return panels [0].type;