Reverted the use of bool in favour of gboolean
[midnight-commander.git] / src / layout.c
blob9d91845b70bc1c5d54a49d0ef2bbafdbe2833fe6
1 /* Panel layout module for the Midnight Commander
2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 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 #include <config.h>
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
34 * If TIOCGWINSZ supported, make it available here, because window-
35 * resizing code depends on it...
37 #ifdef HAVE_SYS_IOCTL_H
38 # include <sys/ioctl.h>
39 #endif
40 #ifdef HAVE_TERMIOS_H
41 #include <termios.h>
42 #endif
43 #include <unistd.h>
45 #include <mhl/memory.h>
46 #include <mhl/string.h>
48 #include "global.h"
49 #include "tty.h" /* COLS */
50 #include "win.h"
51 #include "color.h"
52 #include "key.h"
53 #include "dialog.h"
54 #include "widget.h"
55 #include "command.h"
56 #include "profile.h" /* For sync_profiles() */
57 #include "mouse.h"
58 #include "main-widgets.h"
59 #include "main.h"
60 #include "subshell.h" /* For use_subshell and resize_subshell() */
61 #include "tree.h"
62 #include "menu.h"
64 /* Needed for the extern declarations of integer parameters */
65 #include "dir.h"
66 #include "panel.h" /* The Panel widget */
67 #include "cons.saver.h"
68 #include "layout.h"
69 #include "info.h" /* The Info widget */
70 #include "view.h" /* The view widget */
72 #include "setup.h" /* For save_setup() */
74 /* Controls the display of the rotating dash on the verbose mode */
75 int nice_rotating_dash = 1;
77 /* Set if the panels are split horizontally */
78 int horizontal_split = 0;
80 /* Set if the window has changed it's size */
81 int winch_flag = 0;
83 /* Set if the split is the same */
84 int equal_split = 1;
86 /* First panel size if the panel are not split equally */
87 int first_panel_size = 0;
89 /* The number of output lines shown (if available) */
90 int output_lines = 0;
92 /* Set if the command prompt is to be displayed */
93 int command_prompt = 1;
95 /* Set if the nice and useful keybar is visible */
96 int keybar_visible = 1;
98 /* Set if the nice message (hint) bar is visible */
99 int message_visible = 1;
101 /* Set to show current working dir in xterm window title */
102 int xterm_title = 1;
104 /* The starting line for the output of the subprogram */
105 int output_start_y = 0;
107 /* The maximum number of views managed by the set_display_type routine */
108 /* Must be at least two (for current and other). Please note that until */
109 /* Janne gets around this, we will only manage two of them :-) */
110 #define MAX_VIEWS 2
112 static struct {
113 int type;
114 Widget *widget;
115 } panels [MAX_VIEWS];
117 /* These variables are used to avoid updating the information unless */
118 /* we need it */
119 static int old_first_panel_size;
120 static int old_horizontal_split;
121 static int old_output_lines;
123 /* Internal variables */
124 static int _horizontal_split;
125 static int _equal_split;
126 static int _first_panel_size;
127 static int _menubar_visible;
128 static int _output_lines;
129 static int _command_prompt;
130 static int _keybar_visible;
131 static int _message_visible;
132 static int _xterm_title;
133 static int _permission_mode;
134 static int _filetype_mode;
136 static int height;
138 /* Width 12 for a wee Quick (Hex) View */
139 #define MINWIDTH 12
140 #define MINHEIGHT 5
142 #define BY 12
144 #define B_2LEFT B_USER
145 #define B_2RIGHT (B_USER + 1)
146 #define B_PLUS (B_USER + 2)
147 #define B_MINUS (B_USER + 3)
149 static Dlg_head *layout_dlg;
151 static const char *s_split_direction [2] = {
152 N_("&Vertical"),
153 N_("&Horizontal")
156 static WRadio *radio_widget;
158 static struct {
159 const char *text;
160 int *variable;
161 WCheck *widget;
162 } check_options [] = {
163 { N_("&Xterm window title"), &xterm_title, 0 },
164 { N_("h&Intbar visible"), &message_visible, 0 },
165 { N_("&Keybar visible"), &keybar_visible, 0 },
166 { N_("command &Prompt"), &command_prompt, 0 },
167 { N_("show &Mini status"), &show_mini_info, 0 },
168 { N_("menu&Bar visible"), &menubar_visible, 0 },
169 { N_("&Equal split"), &equal_split, 0 },
170 { N_("pe&Rmissions"), &permission_mode, 0 },
171 { N_("&File types"), &filetype_mode, 0 },
172 { 0, 0, 0 }
175 static int first_width, second_width;
176 static const char *output_lines_label;
178 static WButton *bleft_widget, *bright_widget;
180 /* Declarations for static functions */
181 static void low_level_change_screen_size (void);
183 static void _check_split (void)
185 if (_horizontal_split){
186 if (_equal_split)
187 _first_panel_size = height / 2;
188 else if (_first_panel_size < MINHEIGHT)
189 _first_panel_size = MINHEIGHT;
190 else if (_first_panel_size > height - MINHEIGHT)
191 _first_panel_size = height - MINHEIGHT;
192 } else {
193 if (_equal_split)
194 _first_panel_size = COLS / 2;
195 else if (_first_panel_size < MINWIDTH)
196 _first_panel_size = MINWIDTH;
197 else if (_first_panel_size > COLS - MINWIDTH)
198 _first_panel_size = COLS - MINWIDTH;
202 static void update_split (void)
204 /* Check split has to be done before testing if it changed, since
205 it can change due to calling _check_split() as well*/
206 _check_split ();
208 /* To avoid setting the cursor to the wrong place */
209 if ((old_first_panel_size == _first_panel_size) &&
210 (old_horizontal_split == _horizontal_split)){
211 return;
214 old_first_panel_size = _first_panel_size;
215 old_horizontal_split = _horizontal_split;
217 attrset (COLOR_NORMAL);
218 dlg_move (layout_dlg, 6, 6);
219 tty_printf ("%03d", _first_panel_size);
220 dlg_move (layout_dlg, 6, 18);
221 if (_horizontal_split)
222 tty_printf ("%03d", height - _first_panel_size);
223 else
224 tty_printf ("%03d", COLS - _first_panel_size);
227 static int b2left_cback (int action)
229 (void) action;
231 if (_equal_split){
232 /* Turn equal split off */
233 _equal_split = 0;
234 check_options [6].widget->state = check_options [6].widget->state & ~C_BOOL;
235 dlg_select_widget (check_options [6].widget);
236 dlg_select_widget (bleft_widget);
238 _first_panel_size++;
239 return 0;
242 static int b2right_cback (int action)
244 (void) action;
246 if (_equal_split){
247 /* Turn equal split off */
248 _equal_split = 0;
249 check_options [6].widget->state = check_options [6].widget->state & ~C_BOOL;
250 dlg_select_widget (check_options [6].widget);
251 dlg_select_widget (bright_widget);
253 _first_panel_size--;
254 return 0;
257 static int bplus_cback (int action)
259 (void) action;
261 if (_output_lines < 99)
262 _output_lines++;
263 return 0;
266 static int bminus_cback (int action)
268 (void) action;
270 if (_output_lines > 0)
271 _output_lines--;
272 return 0;
275 static cb_ret_t
276 layout_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
278 switch (msg) {
279 case DLG_DRAW:
280 /*When repainting the whole dialog (e.g. with C-l) we have to
281 update everything*/
282 common_dialog_repaint (h);
284 old_first_panel_size = -1;
285 old_horizontal_split = -1;
286 old_output_lines = -1;
288 attrset (COLOR_HOT_NORMAL);
289 update_split ();
290 dlg_move (h, 6, 13);
291 addch ('=');
292 if (console_flag){
293 if (old_output_lines != _output_lines){
294 old_output_lines = _output_lines;
295 attrset (COLOR_NORMAL);
296 dlg_move (h, 9, 16 + first_width);
297 addstr (output_lines_label);
298 dlg_move (h, 9, 10 + first_width);
299 tty_printf ("%02d", _output_lines);
302 return MSG_HANDLED;
304 case DLG_POST_KEY:
305 _filetype_mode = check_options [8].widget->state & C_BOOL;
306 _permission_mode = check_options [7].widget->state & C_BOOL;
307 _equal_split = check_options [6].widget->state & C_BOOL;
308 _menubar_visible = check_options [5].widget->state & C_BOOL;
309 _command_prompt = check_options [4].widget->state & C_BOOL;
310 _keybar_visible = check_options [2].widget->state & C_BOOL;
311 _message_visible = check_options [1].widget->state & C_BOOL;
312 _xterm_title = check_options [0].widget->state & C_BOOL;
313 if (console_flag){
314 int minimum;
315 if (_output_lines < 0)
316 _output_lines = 0;
317 height = LINES - _keybar_visible - _command_prompt -
318 _menubar_visible - _output_lines - _message_visible;
319 minimum = MINHEIGHT * (1 + _horizontal_split);
320 if (height < minimum){
321 _output_lines -= minimum - height;
322 height = minimum;
324 } else {
325 height = LINES - _keybar_visible - _command_prompt -
326 _menubar_visible - _output_lines - _message_visible;
328 if (_horizontal_split != radio_widget->sel){
329 _horizontal_split = radio_widget->sel;
330 if (_horizontal_split)
331 _first_panel_size = height / 2;
332 else
333 _first_panel_size = COLS / 2;
335 update_split ();
336 if (console_flag){
337 if (old_output_lines != _output_lines){
338 old_output_lines = _output_lines;
339 attrset (COLOR_NORMAL);
340 dlg_move (h, 9, 10 + first_width);
341 tty_printf ("%02d", _output_lines);
344 return MSG_HANDLED;
346 default:
347 return default_dlg_callback (h, msg, parm);
351 static void
352 init_layout (void)
354 static int i18n_layt_flag = 0;
355 static int b1, b2, b3;
356 int i = sizeof (s_split_direction) / sizeof (char *);
357 const char *ok_button = _("&OK");
358 const char *cancel_button = _("&Cancel");
359 const char *save_button = _("&Save");
360 static const char *title1, *title2, *title3;
362 if (!i18n_layt_flag) {
363 register int l1;
365 first_width = 19; /* length of line with '<' '>' buttons */
367 title1 = _(" Panel split ");
368 title2 = _(" Highlight... ");
369 title3 = _(" Other options ");
370 output_lines_label = _("output lines");
372 while (i--) {
373 s_split_direction[i] = _(s_split_direction[i]);
374 l1 = strlen (s_split_direction[i]) + 7;
375 if (l1 > first_width)
376 first_width = l1;
379 for (i = 0; i <= 8; i++) {
380 check_options[i].text = _(check_options[i].text);
381 l1 = strlen (check_options[i].text) + 7;
382 if (l1 > first_width)
383 first_width = l1;
386 l1 = strlen (title1) + 1;
387 if (l1 > first_width)
388 first_width = l1;
390 l1 = strlen (title2) + 1;
391 if (l1 > first_width)
392 first_width = l1;
395 second_width = strlen (title3) + 1;
396 for (i = 0; i < 6; i++) {
397 check_options[i].text = _(check_options[i].text);
398 l1 = strlen (check_options[i].text) + 7;
399 if (l1 > second_width)
400 second_width = l1;
402 if (console_flag) {
403 l1 = strlen (output_lines_label) + 13;
404 if (l1 > second_width)
405 second_width = l1;
409 * alex@bcs.zp.ua:
410 * To be completely correct, one need to check if the title
411 * does not exceed dialog length and total length of 3 buttons
412 * allows their placement in one row. But assuming this dialog
413 * is wide enough, I don't include such a tests.
415 * Now the last thing to do - properly space buttons...
417 l1 = 11 + strlen (ok_button) /* 14 - all brackets and inner space */
418 +strlen (save_button) /* notice: it is 3 char less because */
419 +strlen (cancel_button); /* of '&' char in button text */
421 i = (first_width + second_width - l1) / 4;
422 b1 = 5 + i;
423 b2 = b1 + strlen (ok_button) + i + 6;
424 b3 = b2 + strlen (save_button) + i + 4;
426 i18n_layt_flag = 1;
429 layout_dlg =
430 create_dlg (0, 0, 15, first_width + second_width + 9,
431 dialog_colors, layout_callback, "[Layout]",
432 _("Layout"), DLG_CENTER | DLG_REVERSE);
434 add_widget (layout_dlg, groupbox_new (4, 2, first_width, 6, title1));
435 add_widget (layout_dlg, groupbox_new (4, 8, first_width, 4, title2));
436 add_widget (layout_dlg,
437 groupbox_new (5 + first_width, 2, second_width, 10,
438 title3));
440 add_widget (layout_dlg,
441 button_new (BY, b3, B_CANCEL, NORMAL_BUTTON, cancel_button,
442 0));
443 add_widget (layout_dlg,
444 button_new (BY, b2, B_EXIT, NORMAL_BUTTON, save_button,
445 0));
446 add_widget (layout_dlg,
447 button_new (BY, b1, B_ENTER, DEFPUSH_BUTTON, ok_button,
448 0));
449 if (console_flag) {
450 add_widget (layout_dlg,
451 button_new (9, 12 + first_width, B_MINUS,
452 NARROW_BUTTON, "&-", bminus_cback));
453 add_widget (layout_dlg,
454 button_new (9, 7 + first_width, B_PLUS, NARROW_BUTTON,
455 "&+", bplus_cback));
457 #define XTRACT(i) *check_options[i].variable, check_options[i].text
459 for (i = 0; i < 6; i++) {
460 check_options[i].widget =
461 check_new (8 - i, 7 + first_width, XTRACT (i));
462 add_widget (layout_dlg, check_options[i].widget);
464 check_options[8].widget = check_new (10, 6, XTRACT (8));
465 add_widget (layout_dlg, check_options[8].widget);
466 check_options[7].widget = check_new (9, 6, XTRACT (7));
467 add_widget (layout_dlg, check_options[7].widget);
469 _filetype_mode = filetype_mode;
470 _permission_mode = permission_mode;
471 _equal_split = equal_split;
472 _menubar_visible = menubar_visible;
473 _command_prompt = command_prompt;
474 _keybar_visible = keybar_visible;
475 _message_visible = message_visible;
476 _xterm_title = xterm_title;
477 bright_widget =
478 button_new (6, 15, B_2RIGHT, NARROW_BUTTON, "&>", b2right_cback);
479 add_widget (layout_dlg, bright_widget);
480 bleft_widget =
481 button_new (6, 9, B_2LEFT, NARROW_BUTTON, "&<", b2left_cback);
482 add_widget (layout_dlg, bleft_widget);
483 check_options[6].widget = check_new (5, 6, XTRACT (6));
484 old_first_panel_size = -1;
485 old_horizontal_split = -1;
486 old_output_lines = -1;
488 _first_panel_size = first_panel_size;
489 _output_lines = output_lines;
490 add_widget (layout_dlg, check_options[6].widget);
491 radio_widget = radio_new (3, 6, 2, s_split_direction);
492 add_widget (layout_dlg, radio_widget);
493 radio_widget->sel = horizontal_split;
496 static void
497 layout_change (void)
499 setup_panels ();
500 /* re-init the menu, because perhaps there was a change in the way
501 how the panel are split (horizontal/vertical). */
502 done_menu ();
503 init_menu ();
504 menubar_arrange (the_menubar);
507 void layout_cmd (void)
509 int result;
510 int i;
511 int layout_do_change = 0;
513 init_layout ();
514 run_dlg (layout_dlg);
515 result = layout_dlg->ret_value;
517 if (result == B_ENTER || result == B_EXIT){
518 for (i = 0; check_options [i].text; i++)
519 if (check_options [i].widget)
520 *check_options [i].variable = check_options [i].widget->state & C_BOOL;
521 horizontal_split = radio_widget->sel;
522 first_panel_size = _first_panel_size;
523 output_lines = _output_lines;
524 layout_do_change = 1;
526 if (result == B_EXIT){
527 save_layout ();
528 sync_profiles ();
531 destroy_dlg (layout_dlg);
532 if (layout_do_change)
533 layout_change ();
536 static void check_split (void)
538 if (horizontal_split){
539 if (equal_split)
540 first_panel_size = height / 2;
541 else if (first_panel_size < MINHEIGHT)
542 first_panel_size = MINHEIGHT;
543 else if (first_panel_size > height - MINHEIGHT)
544 first_panel_size = height - MINHEIGHT;
545 } else {
546 if (equal_split)
547 first_panel_size = COLS / 2;
548 else if (first_panel_size < MINWIDTH)
549 first_panel_size = MINWIDTH;
550 else if (first_panel_size > COLS - MINWIDTH)
551 first_panel_size = COLS - MINWIDTH;
555 #ifdef HAVE_SLANG
556 void
557 init_curses ()
559 SLsmg_init_smg ();
560 do_enter_ca_mode ();
561 init_colors ();
562 keypad (stdscr, TRUE);
563 nodelay (stdscr, FALSE);
565 #else
566 static const struct {
567 int acscode;
568 int character;
569 } acs_approx [] = {
570 { 'q', '-' }, /* ACS_HLINE */
571 { 'x', '|' }, /* ACS_VLINE */
572 { 'l', '+' }, /* ACS_ULCORNER */
573 { 'k', '+' }, /* ACS_URCORNER */
574 { 'm', '+' }, /* ACS_LLCORNER */
575 { 'j', '+' }, /* ACS_LRCORNER */
576 { 'a', '#' }, /* ACS_CKBOARD */
577 { 'u', '+' }, /* ACS_RTEE */
578 { 't', '+' }, /* ACS_LTEE */
579 { 'w', '+' }, /* ACS_TTEE */
580 { 'v', '+' }, /* ACS_BTEE */
581 { 'n', '+' }, /* ACS_PLUS */
582 { 0, 0 } };
584 void init_curses (void)
586 int i;
587 initscr();
588 #ifdef HAVE_ESCDELAY
590 * If ncurses exports the ESCDELAY variable, it should be set to
591 * a low value, or you'll experience a delay in processing escape
592 * sequences that are recognized by mc (e.g. Esc-Esc). On the other
593 * hand, making ESCDELAY too small can result in some sequences
594 * (e.g. cursor arrows) being reported as separate keys under heavy
595 * processor load, and this can be a problem if mc hasn't learned
596 * them in the "Learn Keys" dialog. The value is in milliseconds.
598 ESCDELAY = 200;
599 #endif /* HAVE_ESCDELAY */
600 do_enter_ca_mode ();
601 mc_raw_mode ();
602 noecho ();
603 keypad (stdscr, TRUE);
604 nodelay (stdscr, FALSE);
605 init_colors ();
606 if (force_ugly_line_drawing) {
607 for (i = 0; acs_approx[i].acscode != 0; i++) {
608 acs_map[acs_approx[i].acscode] = acs_approx[i].character;
612 #endif /* ! HAVE_SLANG */
614 void
615 clr_scr (void)
617 standend ();
618 dlg_erase (midnight_dlg);
619 mc_refresh ();
620 doupdate ();
623 void
624 done_screen ()
626 if (!(quit & SUBSHELL_EXIT))
627 clr_scr ();
628 reset_shell_mode ();
629 mc_noraw_mode ();
630 keypad (stdscr, FALSE);
631 done_colors ();
634 static void
635 panel_do_cols (int index)
637 if (get_display_type (index) == view_listing)
638 set_panel_formats ((WPanel *) panels [index].widget);
639 else {
640 panel_update_cols (panels [index].widget, frame_half);
644 void
645 setup_panels (void)
647 int start_y;
648 int promptl; /* the prompt len */
650 if (console_flag) {
651 int minimum;
652 if (output_lines < 0)
653 output_lines = 0;
654 height =
655 LINES - keybar_visible - command_prompt - menubar_visible -
656 output_lines - message_visible;
657 minimum = MINHEIGHT * (1 + horizontal_split);
658 if (height < minimum) {
659 output_lines -= minimum - height;
660 height = minimum;
662 } else {
663 height =
664 LINES - menubar_visible - command_prompt - keybar_visible -
665 message_visible;
667 check_split ();
668 start_y = menubar_visible;
670 /* The column computing is defered until panel_do_cols */
671 if (horizontal_split) {
672 widget_set_size (panels[0].widget, start_y, 0, first_panel_size,
675 widget_set_size (panels[1].widget, start_y + first_panel_size, 0,
676 height - first_panel_size, 0);
677 } else {
678 int first_x = first_panel_size;
680 widget_set_size (panels[0].widget, start_y, 0, height, 0);
682 widget_set_size (panels[1].widget, start_y, first_x, height, 0);
685 panel_do_cols (0);
686 panel_do_cols (1);
688 promptl = strlen (prompt);
690 widget_set_size (&the_menubar->widget, 0, 0, 1, COLS);
692 if (command_prompt) {
693 widget_set_size (&cmdline->widget, LINES - 1 - keybar_visible,
694 promptl, 1,
695 COLS - promptl - (keybar_visible ? 0 : 1));
696 winput_set_origin (cmdline, promptl,
697 COLS - promptl - (keybar_visible ? 0 : 1));
698 widget_set_size (&the_prompt->widget, LINES - 1 - keybar_visible,
699 0, 1, promptl);
700 } else {
701 widget_set_size (&cmdline->widget, 0, 0, 0, 0);
702 winput_set_origin (cmdline, 0, 0);
703 widget_set_size (&the_prompt->widget, LINES, COLS, 0, 0);
706 widget_set_size ((Widget *) the_bar, LINES - 1, 0, keybar_visible, COLS);
707 buttonbar_set_visible (the_bar, keybar_visible);
709 /* Output window */
710 if (console_flag && output_lines) {
711 output_start_y =
712 LINES - command_prompt - keybar_visible - output_lines;
713 show_console_contents (output_start_y,
714 LINES - output_lines - keybar_visible - 1,
715 LINES - keybar_visible - 1);
717 if (message_visible) {
718 widget_set_size (&the_hint->widget, height + start_y, 0, 1, COLS);
719 set_hintbar (""); /* clean up the line */
720 } else
721 widget_set_size (&the_hint->widget, 0, 0, 0, 0);
723 load_hint (1);
724 update_xterm_title_path ();
727 void flag_winch (int dummy)
729 (void) dummy;
730 #ifndef USE_NCURSES /* don't do malloc in a signal handler */
731 low_level_change_screen_size ();
732 #endif
733 winch_flag = 1;
736 static void
737 low_level_change_screen_size (void)
739 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
740 #if defined TIOCGWINSZ
741 struct winsize winsz;
743 winsz.ws_col = winsz.ws_row = 0;
744 /* Ioctl on the STDIN_FILENO */
745 ioctl (0, TIOCGWINSZ, &winsz);
746 if (winsz.ws_col && winsz.ws_row){
747 #if defined(NCURSES_VERSION) && defined(HAVE_RESIZETERM)
748 resizeterm(winsz.ws_row, winsz.ws_col);
749 clearok(stdscr,TRUE); /* sigwinch's should use a semaphore! */
750 #else
751 COLS = winsz.ws_col;
752 LINES = winsz.ws_row;
753 #endif
754 #ifdef HAVE_SUBSHELL_SUPPORT
755 resize_subshell ();
756 #endif
758 #endif /* TIOCGWINSZ */
759 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
762 void
763 change_screen_size (void)
765 winch_flag = 0;
766 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
767 #if defined TIOCGWINSZ
769 #ifndef NCURSES_VERSION
770 mc_noraw_mode ();
771 endwin ();
772 #endif
773 low_level_change_screen_size ();
774 check_split ();
775 #ifndef NCURSES_VERSION
776 /* XSI Curses spec states that portable applications shall not invoke
777 * initscr() more than once. This kludge could be done within the scope
778 * of the specification by using endwin followed by a refresh (in fact,
779 * more than one curses implementation does this); it is guaranteed to work
780 * only with slang.
782 init_curses ();
783 #endif
784 setup_panels ();
786 /* Inform currently running dialog */
787 (*current_dlg->callback) (current_dlg, DLG_RESIZE, 0);
789 #ifdef RESIZABLE_MENUBAR
790 menubar_arrange (the_menubar);
791 #endif
793 /* Now, force the redraw */
794 do_refresh ();
795 touchwin (stdscr);
796 #endif /* TIOCGWINSZ */
797 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
800 static int ok_to_refresh = 1;
802 void use_dash (int flag)
804 if (flag)
805 ok_to_refresh++;
806 else
807 ok_to_refresh--;
810 void set_hintbar(const char *str)
812 label_set_text (the_hint, str);
813 if (ok_to_refresh > 0)
814 refresh();
817 void print_vfs_message (const char *msg, ...)
819 va_list ap;
820 char str [128];
822 va_start (ap, msg);
824 g_vsnprintf (str, sizeof (str), msg, ap);
825 va_end (ap);
827 if (midnight_shutdown)
828 return;
830 if (!message_visible || !the_hint || !the_hint->widget.parent) {
831 int col, row;
833 if (!nice_rotating_dash || (ok_to_refresh <= 0))
834 return;
836 /* Preserve current cursor position */
837 getyx (stdscr, row, col);
839 move (0, 0);
840 attrset (NORMAL_COLOR);
841 tty_printf ("%-*s", COLS-1, str);
843 /* Restore cursor position */
844 move(row, col);
845 mc_refresh ();
846 return;
849 if (message_visible) {
850 set_hintbar(str);
854 void rotate_dash (void)
856 static const char rotating_dash [] = "|/-\\";
857 static size_t pos = 0;
859 if (!nice_rotating_dash || (ok_to_refresh <= 0))
860 return;
862 if (pos >= sizeof (rotating_dash)-1)
863 pos = 0;
864 move (0, COLS-1);
865 attrset (NORMAL_COLOR);
866 addch (rotating_dash [pos]);
867 mc_refresh ();
868 pos++;
871 const char *get_nth_panel_name (int num)
873 static char buffer [BUF_SMALL];
875 if (!num)
876 return "New Left Panel";
877 else if (num == 1)
878 return "New Right Panel";
879 else {
880 snprintf (buffer, sizeof (buffer), "%ith Panel", num);
881 return buffer;
885 /* I wonder if I should start to use the folding mode than Dugan uses */
886 /* */
887 /* This is the centralized managing of the panel display types */
888 /* This routine takes care of destroying and creating new widgets */
889 /* Please note that it could manage MAX_VIEWS, not just left and right */
890 /* Currently nothing in the code takes advantage of this and has hard- */
891 /* coded values for two panels only */
893 /* Set the num-th panel to the view type: type */
894 /* This routine also keeps at least one WPanel object in the screen */
895 /* since a lot of routines depend on the current_panel variable */
896 void set_display_type (int num, int type)
898 int x, y, cols, lines;
899 int the_other; /* Index to the other panel */
900 const char *file_name = NULL; /* For Quick view */
901 Widget *new_widget, *old_widget;
902 WPanel *the_other_panel;
904 x = y = cols = lines = 0;
905 old_widget = 0;
906 if (num >= MAX_VIEWS){
907 fprintf (stderr, "Cannot allocate more that %d views\n", MAX_VIEWS);
908 abort ();
911 /* Check that we will have a WPanel * at least */
912 the_other = 0;
913 if (type != view_listing){
914 the_other = num == 0 ? 1 : 0;
916 if (panels [the_other].type != view_listing)
917 return;
921 /* Get rid of it */
922 if (panels [num].widget){
923 Widget *w = panels [num].widget;
924 WPanel *panel = (WPanel *) panels [num].widget;
926 x = w->x;
927 y = w->y;
928 cols = w->cols;
929 lines = w->lines;
930 old_widget = panels [num].widget;
932 if (panels [num].type == view_listing){
933 if (panel->frame_size == frame_full && type != view_listing){
934 cols = COLS - first_panel_size;
935 if (num == 1)
936 x = first_panel_size;
941 new_widget = 0;
943 switch (type){
944 case view_listing:
945 new_widget = (Widget *) panel_new (get_nth_panel_name (num));
946 break;
948 case view_info:
949 new_widget = (Widget *) info_new ();
951 break;
953 case view_tree:
954 new_widget = (Widget *) tree_new (1, 0, 0, 0, 0);
955 break;
957 case view_quick:
958 new_widget = (Widget *) view_new (0, 0, 0, 0, 1);
959 the_other_panel = (WPanel *) panels [the_other].widget;
960 if (the_other_panel)
961 file_name =
962 the_other_panel->dir.list[the_other_panel->selected].fname;
963 else
964 file_name = "";
966 view_load ((WView *) new_widget, 0, file_name, 0);
967 break;
969 panels [num].type = type;
970 panels [num].widget = (Widget *) new_widget;
972 /* We set the same size the old widget had */
973 widget_set_size ((Widget *) new_widget, y, x, lines, cols);
975 /* We use replace to keep the circular list of the dialog in the */
976 /* same state. Maybe we could just kill it and then replace it */
977 if (midnight_dlg && old_widget){
978 dlg_replace_widget (old_widget, panels [num].widget);
980 if (type == view_listing){
981 if (num == 0)
982 left_panel = (WPanel *) new_widget;
983 else
984 right_panel = (WPanel *) new_widget;
987 if (type == view_tree)
988 the_tree = (WTree *) new_widget;
990 /* Prevent current_panel's value from becoming invalid.
991 * It's just a quick hack to prevent segfaults. Comment out and
992 * try following:
993 * - select left panel
994 * - invoke menue left/tree
995 * - as long as you stay in the left panel almost everything that uses
996 * current_panel causes segfault, e.g. C-Enter, C-x c, ...
999 if (type != view_listing)
1000 if (current_panel == (WPanel *) old_widget)
1001 current_panel = num == 0 ? right_panel : left_panel;
1004 /* This routine is deeply sticked to the two panels idea.
1005 What should it do in more panels. ANSWER - don't use it
1006 in any multiple panels environment. */
1007 void swap_panels ()
1009 Widget tmp;
1010 Widget *tmp_widget;
1011 WPanel panel;
1012 WPanel *panel1, *panel2;
1013 int tmp_type;
1015 #define panelswap(x) panel. x = panel1-> x; panel1-> x = panel2-> x; panel2-> x = panel. x;
1017 #define panelswapstr(e) strcpy (panel. e, panel1-> e); \
1018 strcpy (panel1-> e, panel2-> e); \
1019 strcpy (panel2-> e, panel. e);
1020 panel1 = (WPanel *) panels [0].widget;
1021 panel2 = (WPanel *) panels [1].widget;
1022 if (panels [0].type == view_listing && panels [1].type == view_listing) {
1023 /* Change everything except format/sort/panel_name etc. */
1024 panelswap (dir);
1025 panelswap (active);
1026 panelswapstr (cwd);
1027 panelswapstr (lwd);
1028 panelswap (count);
1029 panelswap (marked);
1030 panelswap (dirs_marked);
1031 panelswap (total);
1032 panelswap (top_file);
1033 panelswap (selected);
1034 panelswap (is_panelized);
1035 panelswap (dir_stat);
1037 panel1->searching = 0;
1038 panel2->searching = 0;
1039 if (current_panel == panel1)
1040 current_panel = panel2;
1041 else
1042 current_panel = panel1;
1044 if (dlg_widget_active (panels[0].widget))
1045 dlg_select_widget (panels [1].widget);
1046 else if (dlg_widget_active (panels[1].widget))
1047 dlg_select_widget (panels [0].widget);
1048 } else {
1049 WPanel *tmp_panel;
1051 tmp_panel=right_panel;
1052 right_panel=left_panel;
1053 left_panel=tmp_panel;
1055 if (panels [0].type == view_listing) {
1056 if (!strcmp (panel1->panel_name, get_nth_panel_name (0))) {
1057 g_free (panel1->panel_name);
1058 panel1->panel_name = g_strdup (get_nth_panel_name (1));
1061 if (panels [1].type == view_listing) {
1062 if (!strcmp (panel2->panel_name, get_nth_panel_name (1))) {
1063 g_free (panel2->panel_name);
1064 panel2->panel_name = g_strdup (get_nth_panel_name (0));
1068 tmp.x = panels [0].widget->x;
1069 tmp.y = panels [0].widget->y;
1070 tmp.cols = panels [0].widget->cols;
1071 tmp.lines = panels [0].widget->lines;
1073 panels [0].widget->x = panels [1].widget->x;
1074 panels [0].widget->y = panels [1].widget->y;
1075 panels [0].widget->cols = panels [1].widget->cols;
1076 panels [0].widget->lines = panels [1].widget->lines;
1078 panels [1].widget->x = tmp.x;
1079 panels [1].widget->y = tmp.y;
1080 panels [1].widget->cols = tmp.cols;
1081 panels [1].widget->lines = tmp.lines;
1083 tmp_widget = panels [0].widget;
1084 panels [0].widget = panels [1].widget;
1085 panels [1].widget = tmp_widget;
1086 tmp_type = panels [0].type;
1087 panels [0].type = panels [1].type;
1088 panels [1].type = tmp_type;
1092 int get_display_type (int index)
1094 return panels [index].type;
1097 struct Widget *
1098 get_panel_widget (int index)
1100 return panels[index].widget;
1103 int get_current_index (void)
1105 if (panels [0].widget == ((Widget *) current_panel))
1106 return 0;
1107 else
1108 return 1;
1111 int get_other_index (void)
1113 return !get_current_index ();
1116 struct WPanel *
1117 get_other_panel (void)
1119 return (struct WPanel *) get_panel_widget (get_other_index ());
1122 /* Returns the view type for the current panel/view */
1123 int get_current_type (void)
1125 if (panels [0].widget == (Widget *) current_panel)
1126 return panels [0].type;
1127 else
1128 return panels [1].type;
1131 /* Returns the view type of the unselected panel */
1132 int get_other_type (void)
1134 if (panels [0].widget == (Widget *) current_panel)
1135 return panels [1].type;
1136 else
1137 return panels [0].type;