Original patch as attached on the bugreport
[midnight-commander.git] / src / layout.c
blob9f3616f7a7ca9abac2f92ae97eee18b3a61a7f11
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>
33 * If TIOCGWINSZ supported, make it available here, because window-
34 * resizing code depends on it...
36 #ifdef HAVE_SYS_IOCTL_H
37 # include <sys/ioctl.h>
38 #endif
39 #ifdef HAVE_TERMIOS_H
40 #include <termios.h>
41 #endif
42 #include <unistd.h>
44 #include "global.h"
45 #include "tty.h" /* COLS */
46 #include "win.h"
47 #include "color.h"
48 #include "key.h"
49 #include "dialog.h"
50 #include "widget.h"
51 #include "command.h"
52 #include "profile.h" /* For sync_profiles() */
53 #include "mouse.h"
54 #define WANT_WIDGETS
55 #include "main.h"
56 #include "subshell.h" /* For use_subshell and resize_subshell() */
57 #include "tree.h"
58 #include "menu.h"
60 /* Needed for the extern declarations of integer parameters */
61 #include "dir.h"
62 #include "panel.h" /* The Panel widget */
63 #include "cons.saver.h"
64 #include "layout.h"
65 #include "info.h" /* The Info widget */
66 #include "view.h" /* The view widget */
68 #include "setup.h" /* For save_setup() */
70 /* Controls the display of the rotating dash on the verbose mode */
71 int nice_rotating_dash = 1;
73 /* Set if the panels are split horizontally */
74 int horizontal_split = 0;
76 /* Set if the window has changed it's size */
77 int winch_flag = 0;
79 /* Set if the split is the same */
80 int equal_split = 1;
82 /* First panel size if the panel are not split equally */
83 int first_panel_size = 0;
85 /* The number of output lines shown (if available) */
86 int output_lines = 0;
88 /* Set if the command prompt is to be displayed */
89 int command_prompt = 1;
91 /* Set if the nice and useful keybar is visible */
92 int keybar_visible = 1;
94 /* Set if the nice message (hint) bar is visible */
95 int message_visible = 1;
97 /* Set to show current working dir in xterm window title */
98 int xterm_title = 1;
100 /* The starting line for the output of the subprogram */
101 int output_start_y = 0;
103 /* The maximum number of views managed by the set_display_type routine */
104 /* Must be at least two (for current and other). Please note that until */
105 /* Janne gets around this, we will only manage two of them :-) */
106 #define MAX_VIEWS 2
108 static struct {
109 int type;
110 Widget *widget;
111 } panels [MAX_VIEWS];
113 /* These variables are used to avoid updating the information unless */
114 /* we need it */
115 static int old_first_panel_size;
116 static int old_horizontal_split;
117 static int old_output_lines;
119 /* Internal variables */
120 static int _horizontal_split;
121 static int _equal_split;
122 static int _first_panel_size;
123 static int _menubar_visible;
124 static int _output_lines;
125 static int _command_prompt;
126 static int _keybar_visible;
127 static int _message_visible;
128 static int _xterm_title;
129 static int _permission_mode;
130 static int _filetype_mode;
132 static int height;
134 /* Width 12 for a wee Quick (Hex) View */
135 #define MINWIDTH 12
136 #define MINHEIGHT 5
138 #define BY 12
140 #define B_2LEFT B_USER
141 #define B_2RIGHT (B_USER + 1)
142 #define B_PLUS (B_USER + 2)
143 #define B_MINUS (B_USER + 3)
145 static Dlg_head *layout_dlg;
147 static const char *s_split_direction [2] = {
148 N_("&Vertical"),
149 N_("&Horizontal")
152 static WRadio *radio_widget;
154 static struct {
155 const char *text;
156 int *variable;
157 WCheck *widget;
158 } check_options [] = {
159 { N_("&Xterm window title"), &xterm_title, 0 },
160 { N_("h&Intbar visible"), &message_visible, 0 },
161 { N_("&Keybar visible"), &keybar_visible, 0 },
162 { N_("command &Prompt"), &command_prompt, 0 },
163 { N_("show &Mini status"), &show_mini_info, 0 },
164 { N_("menu&Bar visible"), &menubar_visible, 0 },
165 { N_("&Equal split"), &equal_split, 0 },
166 { N_("pe&Rmissions"), &permission_mode, 0 },
167 { N_("&File types"), &filetype_mode, 0 },
168 { 0, 0, 0 }
171 static int first_width, second_width;
172 static const char *output_lines_label;
174 static WButton *bleft_widget, *bright_widget;
176 /* Declarations for static functions */
177 static void low_level_change_screen_size (void);
179 static void _check_split (void)
181 if (_horizontal_split){
182 if (_equal_split)
183 _first_panel_size = height / 2;
184 else if (_first_panel_size < MINHEIGHT)
185 _first_panel_size = MINHEIGHT;
186 else if (_first_panel_size > height - MINHEIGHT)
187 _first_panel_size = height - MINHEIGHT;
188 } else {
189 if (_equal_split)
190 _first_panel_size = COLS / 2;
191 else if (_first_panel_size < MINWIDTH)
192 _first_panel_size = MINWIDTH;
193 else if (_first_panel_size > COLS - MINWIDTH)
194 _first_panel_size = COLS - MINWIDTH;
198 static void update_split (void)
200 /* Check split has to be done before testing if it changed, since
201 it can change due to calling _check_split() as well*/
202 _check_split ();
204 /* To avoid setting the cursor to the wrong place */
205 if ((old_first_panel_size == _first_panel_size) &&
206 (old_horizontal_split == _horizontal_split)){
207 return;
210 old_first_panel_size = _first_panel_size;
211 old_horizontal_split = _horizontal_split;
213 attrset (COLOR_NORMAL);
214 dlg_move (layout_dlg, 6, 6);
215 tty_printf ("%03d", _first_panel_size);
216 dlg_move (layout_dlg, 6, 18);
217 if (_horizontal_split)
218 tty_printf ("%03d", height - _first_panel_size);
219 else
220 tty_printf ("%03d", COLS - _first_panel_size);
223 static int b2left_cback (int action)
225 (void) action;
227 if (_equal_split){
228 /* Turn equal split off */
229 _equal_split = 0;
230 check_options [6].widget->state = check_options [6].widget->state & ~C_BOOL;
231 dlg_select_widget (check_options [6].widget);
232 dlg_select_widget (bleft_widget);
234 _first_panel_size++;
235 return 0;
238 static int b2right_cback (int action)
240 (void) action;
242 if (_equal_split){
243 /* Turn equal split off */
244 _equal_split = 0;
245 check_options [6].widget->state = check_options [6].widget->state & ~C_BOOL;
246 dlg_select_widget (check_options [6].widget);
247 dlg_select_widget (bright_widget);
249 _first_panel_size--;
250 return 0;
253 static int bplus_cback (int action)
255 (void) action;
257 if (_output_lines < 99)
258 _output_lines++;
259 return 0;
262 static int bminus_cback (int action)
264 (void) action;
266 if (_output_lines > 0)
267 _output_lines--;
268 return 0;
271 static cb_ret_t
272 layout_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
274 switch (msg) {
275 case DLG_DRAW:
276 /*When repainting the whole dialog (e.g. with C-l) we have to
277 update everything*/
278 common_dialog_repaint (h);
280 old_first_panel_size = -1;
281 old_horizontal_split = -1;
282 old_output_lines = -1;
284 attrset (COLOR_HOT_NORMAL);
285 update_split ();
286 dlg_move (h, 6, 13);
287 addch ('=');
288 if (console_flag){
289 if (old_output_lines != _output_lines){
290 old_output_lines = _output_lines;
291 attrset (COLOR_NORMAL);
292 dlg_move (h, 9, 16 + first_width);
293 addstr (output_lines_label);
294 dlg_move (h, 9, 10 + first_width);
295 tty_printf ("%02d", _output_lines);
298 return MSG_HANDLED;
300 case DLG_POST_KEY:
301 _filetype_mode = check_options [8].widget->state & C_BOOL;
302 _permission_mode = check_options [7].widget->state & C_BOOL;
303 _equal_split = check_options [6].widget->state & C_BOOL;
304 _menubar_visible = check_options [5].widget->state & C_BOOL;
305 _command_prompt = check_options [4].widget->state & C_BOOL;
306 _keybar_visible = check_options [2].widget->state & C_BOOL;
307 _message_visible = check_options [1].widget->state & C_BOOL;
308 _xterm_title = check_options [0].widget->state & C_BOOL;
309 if (console_flag){
310 int minimum;
311 if (_output_lines < 0)
312 _output_lines = 0;
313 height = LINES - _keybar_visible - _command_prompt -
314 _menubar_visible - _output_lines - _message_visible;
315 minimum = MINHEIGHT * (1 + _horizontal_split);
316 if (height < minimum){
317 _output_lines -= minimum - height;
318 height = minimum;
320 } else {
321 height = LINES - _keybar_visible - _command_prompt -
322 _menubar_visible - _output_lines - _message_visible;
324 if (_horizontal_split != radio_widget->sel){
325 _horizontal_split = radio_widget->sel;
326 if (_horizontal_split)
327 _first_panel_size = height / 2;
328 else
329 _first_panel_size = COLS / 2;
331 update_split ();
332 if (console_flag){
333 if (old_output_lines != _output_lines){
334 old_output_lines = _output_lines;
335 attrset (COLOR_NORMAL);
336 dlg_move (h, 9, 10 + first_width);
337 tty_printf ("%02d", _output_lines);
340 return MSG_HANDLED;
342 default:
343 return default_dlg_callback (h, msg, parm);
347 static void
348 init_layout (void)
350 static int i18n_layt_flag = 0;
351 static int b1, b2, b3;
352 int i = sizeof (s_split_direction) / sizeof (char *);
353 const char *ok_button = _("&OK");
354 const char *cancel_button = _("&Cancel");
355 const char *save_button = _("&Save");
356 static const char *title1, *title2, *title3;
358 if (!i18n_layt_flag) {
359 register int l1;
361 first_width = 19; /* length of line with '<' '>' buttons */
363 title1 = _(" Panel split ");
364 title2 = _(" Highlight... ");
365 title3 = _(" Other options ");
366 output_lines_label = _("output lines");
368 while (i--) {
369 s_split_direction[i] = _(s_split_direction[i]);
370 l1 = strlen (s_split_direction[i]) + 7;
371 if (l1 > first_width)
372 first_width = l1;
375 for (i = 0; i <= 8; i++) {
376 check_options[i].text = _(check_options[i].text);
377 l1 = strlen (check_options[i].text) + 7;
378 if (l1 > first_width)
379 first_width = l1;
382 l1 = strlen (title1) + 1;
383 if (l1 > first_width)
384 first_width = l1;
386 l1 = strlen (title2) + 1;
387 if (l1 > first_width)
388 first_width = l1;
391 second_width = strlen (title3) + 1;
392 for (i = 0; i < 6; i++) {
393 check_options[i].text = _(check_options[i].text);
394 l1 = strlen (check_options[i].text) + 7;
395 if (l1 > second_width)
396 second_width = l1;
398 if (console_flag) {
399 l1 = strlen (output_lines_label) + 13;
400 if (l1 > second_width)
401 second_width = l1;
405 * alex@bcs.zp.ua:
406 * To be completely correct, one need to check if the title
407 * does not exceed dialog length and total length of 3 buttons
408 * allows their placement in one row. But assuming this dialog
409 * is wide enough, I don't include such a tests.
411 * Now the last thing to do - properly space buttons...
413 l1 = 11 + strlen (ok_button) /* 14 - all brackets and inner space */
414 +strlen (save_button) /* notice: it is 3 char less because */
415 +strlen (cancel_button); /* of '&' char in button text */
417 i = (first_width + second_width - l1) / 4;
418 b1 = 5 + i;
419 b2 = b1 + strlen (ok_button) + i + 6;
420 b3 = b2 + strlen (save_button) + i + 4;
422 i18n_layt_flag = 1;
425 layout_dlg =
426 create_dlg (0, 0, 15, first_width + second_width + 9,
427 dialog_colors, layout_callback, "[Layout]",
428 _("Layout"), DLG_CENTER | DLG_REVERSE);
430 add_widget (layout_dlg, groupbox_new (4, 2, first_width, 6, title1));
431 add_widget (layout_dlg, groupbox_new (4, 8, first_width, 4, title2));
432 add_widget (layout_dlg,
433 groupbox_new (5 + first_width, 2, second_width, 10,
434 title3));
436 add_widget (layout_dlg,
437 button_new (BY, b3, B_CANCEL, NORMAL_BUTTON, cancel_button,
438 0));
439 add_widget (layout_dlg,
440 button_new (BY, b2, B_EXIT, NORMAL_BUTTON, save_button,
441 0));
442 add_widget (layout_dlg,
443 button_new (BY, b1, B_ENTER, DEFPUSH_BUTTON, ok_button,
444 0));
445 if (console_flag) {
446 add_widget (layout_dlg,
447 button_new (9, 12 + first_width, B_MINUS,
448 NARROW_BUTTON, "&-", bminus_cback));
449 add_widget (layout_dlg,
450 button_new (9, 7 + first_width, B_PLUS, NARROW_BUTTON,
451 "&+", bplus_cback));
453 #define XTRACT(i) *check_options[i].variable, check_options[i].text
455 for (i = 0; i < 6; i++) {
456 check_options[i].widget =
457 check_new (8 - i, 7 + first_width, XTRACT (i));
458 add_widget (layout_dlg, check_options[i].widget);
460 check_options[8].widget = check_new (10, 6, XTRACT (8));
461 add_widget (layout_dlg, check_options[8].widget);
462 check_options[7].widget = check_new (9, 6, XTRACT (7));
463 add_widget (layout_dlg, check_options[7].widget);
465 _filetype_mode = filetype_mode;
466 _permission_mode = permission_mode;
467 _equal_split = equal_split;
468 _menubar_visible = menubar_visible;
469 _command_prompt = command_prompt;
470 _keybar_visible = keybar_visible;
471 _message_visible = message_visible;
472 _xterm_title = xterm_title;
473 bright_widget =
474 button_new (6, 15, B_2RIGHT, NARROW_BUTTON, "&>", b2right_cback);
475 add_widget (layout_dlg, bright_widget);
476 bleft_widget =
477 button_new (6, 9, B_2LEFT, NARROW_BUTTON, "&<", b2left_cback);
478 add_widget (layout_dlg, bleft_widget);
479 check_options[6].widget = check_new (5, 6, XTRACT (6));
480 old_first_panel_size = -1;
481 old_horizontal_split = -1;
482 old_output_lines = -1;
484 _first_panel_size = first_panel_size;
485 _output_lines = output_lines;
486 add_widget (layout_dlg, check_options[6].widget);
487 radio_widget = radio_new (3, 6, 2, s_split_direction);
488 add_widget (layout_dlg, radio_widget);
489 radio_widget->sel = horizontal_split;
492 static void
493 layout_change (void)
495 setup_panels ();
496 /* re-init the menu, because perhaps there was a change in the way
497 how the panel are split (horizontal/vertical). */
498 done_menu ();
499 init_menu ();
500 menubar_arrange (the_menubar);
503 void layout_cmd (void)
505 int result;
506 int i;
507 int layout_do_change = 0;
509 init_layout ();
510 run_dlg (layout_dlg);
511 result = layout_dlg->ret_value;
513 if (result == B_ENTER || result == B_EXIT){
514 for (i = 0; check_options [i].text; i++)
515 if (check_options [i].widget)
516 *check_options [i].variable = check_options [i].widget->state & C_BOOL;
517 horizontal_split = radio_widget->sel;
518 first_panel_size = _first_panel_size;
519 output_lines = _output_lines;
520 layout_do_change = 1;
522 if (result == B_EXIT){
523 save_layout ();
524 sync_profiles ();
527 destroy_dlg (layout_dlg);
528 if (layout_do_change)
529 layout_change ();
532 static void check_split (void)
534 if (horizontal_split){
535 if (equal_split)
536 first_panel_size = height / 2;
537 else if (first_panel_size < MINHEIGHT)
538 first_panel_size = MINHEIGHT;
539 else if (first_panel_size > height - MINHEIGHT)
540 first_panel_size = height - MINHEIGHT;
541 } else {
542 if (equal_split)
543 first_panel_size = COLS / 2;
544 else if (first_panel_size < MINWIDTH)
545 first_panel_size = MINWIDTH;
546 else if (first_panel_size > COLS - MINWIDTH)
547 first_panel_size = COLS - MINWIDTH;
551 #ifdef HAVE_SLANG
552 void
553 init_curses ()
555 SLsmg_init_smg ();
556 do_enter_ca_mode ();
557 init_colors ();
558 keypad (stdscr, TRUE);
559 nodelay (stdscr, FALSE);
561 #else
562 static const struct {
563 int acscode;
564 int character;
565 } acs_approx [] = {
566 { 'q', '-' }, /* ACS_HLINE */
567 { 'x', '|' }, /* ACS_VLINE */
568 { 'l', '+' }, /* ACS_ULCORNER */
569 { 'k', '+' }, /* ACS_URCORNER */
570 { 'm', '+' }, /* ACS_LLCORNER */
571 { 'j', '+' }, /* ACS_LRCORNER */
572 { 'a', '#' }, /* ACS_CKBOARD */
573 { 'u', '+' }, /* ACS_RTEE */
574 { 't', '+' }, /* ACS_LTEE */
575 { 'w', '+' }, /* ACS_TTEE */
576 { 'v', '+' }, /* ACS_BTEE */
577 { 'n', '+' }, /* ACS_PLUS */
578 { 0, 0 } };
580 void init_curses (void)
582 int i;
583 initscr();
584 #ifdef HAVE_ESCDELAY
586 * If ncurses exports the ESCDELAY variable, it should be set to
587 * a low value, or you'll experience a delay in processing escape
588 * sequences that are recognized by mc (e.g. Esc-Esc). On the other
589 * hand, making ESCDELAY too small can result in some sequences
590 * (e.g. cursor arrows) being reported as separate keys under heavy
591 * processor load, and this can be a problem if mc hasn't learned
592 * them in the "Learn Keys" dialog. The value is in milliseconds.
594 ESCDELAY = 200;
595 #endif /* HAVE_ESCDELAY */
596 do_enter_ca_mode ();
597 mc_raw_mode ();
598 noecho ();
599 keypad (stdscr, TRUE);
600 nodelay (stdscr, FALSE);
601 init_colors ();
602 if (force_ugly_line_drawing) {
603 for (i = 0; acs_approx[i].acscode != 0; i++) {
604 acs_map[acs_approx[i].acscode] = acs_approx[i].character;
608 #endif /* ! HAVE_SLANG */
610 void
611 clr_scr (void)
613 standend ();
614 dlg_erase (midnight_dlg);
615 mc_refresh ();
616 doupdate ();
619 void
620 done_screen ()
622 if (!(quit & SUBSHELL_EXIT))
623 clr_scr ();
624 reset_shell_mode ();
625 mc_noraw_mode ();
626 keypad (stdscr, FALSE);
627 done_colors ();
630 static void
631 panel_do_cols (int index)
633 if (get_display_type (index) == view_listing)
634 set_panel_formats ((WPanel *) panels [index].widget);
635 else {
636 panel_update_cols (panels [index].widget, frame_half);
640 void
641 setup_panels (void)
643 int start_y;
644 int promptl; /* the prompt len */
646 if (console_flag) {
647 int minimum;
648 if (output_lines < 0)
649 output_lines = 0;
650 height =
651 LINES - keybar_visible - command_prompt - menubar_visible -
652 output_lines - message_visible;
653 minimum = MINHEIGHT * (1 + horizontal_split);
654 if (height < minimum) {
655 output_lines -= minimum - height;
656 height = minimum;
658 } else {
659 height =
660 LINES - menubar_visible - command_prompt - keybar_visible -
661 message_visible;
663 check_split ();
664 start_y = menubar_visible;
666 /* The column computing is defered until panel_do_cols */
667 if (horizontal_split) {
668 widget_set_size (panels[0].widget, start_y, 0, first_panel_size,
671 widget_set_size (panels[1].widget, start_y + first_panel_size, 0,
672 height - first_panel_size, 0);
673 } else {
674 int first_x = first_panel_size;
676 widget_set_size (panels[0].widget, start_y, 0, height, 0);
678 widget_set_size (panels[1].widget, start_y, first_x, height, 0);
681 panel_do_cols (0);
682 panel_do_cols (1);
684 promptl = strlen (prompt);
686 widget_set_size (&the_menubar->widget, 0, 0, 1, COLS);
688 if (command_prompt) {
689 widget_set_size (&cmdline->widget, LINES - 1 - keybar_visible,
690 promptl, 1,
691 COLS - promptl - (keybar_visible ? 0 : 1));
692 winput_set_origin (cmdline, promptl,
693 COLS - promptl - (keybar_visible ? 0 : 1));
694 widget_set_size (&the_prompt->widget, LINES - 1 - keybar_visible,
695 0, 1, promptl);
696 } else {
697 widget_set_size (&cmdline->widget, 0, 0, 0, 0);
698 winput_set_origin (cmdline, 0, 0);
699 widget_set_size (&the_prompt->widget, LINES, COLS, 0, 0);
702 widget_set_size ((Widget *) the_bar, LINES - 1, 0, keybar_visible, COLS);
703 buttonbar_set_visible (the_bar, keybar_visible);
705 /* Output window */
706 if (console_flag && output_lines) {
707 output_start_y =
708 LINES - command_prompt - keybar_visible - output_lines;
709 show_console_contents (output_start_y,
710 LINES - output_lines - keybar_visible - 1,
711 LINES - keybar_visible - 1);
713 if (message_visible) {
714 widget_set_size (&the_hint->widget, height + start_y, 0, 1, COLS);
715 set_hintbar (""); /* clean up the line */
716 } else
717 widget_set_size (&the_hint->widget, 0, 0, 0, 0);
719 load_hint (1);
720 update_xterm_title_path ();
723 void flag_winch (int dummy)
725 (void) dummy;
726 #ifndef USE_NCURSES /* don't do malloc in a signal handler */
727 low_level_change_screen_size ();
728 #endif
729 winch_flag = 1;
732 static void
733 low_level_change_screen_size (void)
735 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
736 #if defined TIOCGWINSZ
737 struct winsize winsz;
739 winsz.ws_col = winsz.ws_row = 0;
740 /* Ioctl on the STDIN_FILENO */
741 ioctl (0, TIOCGWINSZ, &winsz);
742 if (winsz.ws_col && winsz.ws_row){
743 #if defined(NCURSES_VERSION) && defined(HAVE_RESIZETERM)
744 resizeterm(winsz.ws_row, winsz.ws_col);
745 clearok(stdscr,TRUE); /* sigwinch's should use a semaphore! */
746 #else
747 COLS = winsz.ws_col;
748 LINES = winsz.ws_row;
749 #endif
750 #ifdef HAVE_SUBSHELL_SUPPORT
751 resize_subshell ();
752 #endif
754 #endif /* TIOCGWINSZ */
755 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
758 void
759 change_screen_size (void)
761 winch_flag = 0;
762 #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
763 #if defined TIOCGWINSZ
765 #ifndef NCURSES_VERSION
766 mc_noraw_mode ();
767 endwin ();
768 #endif
769 low_level_change_screen_size ();
770 check_split ();
771 #ifndef NCURSES_VERSION
772 /* XSI Curses spec states that portable applications shall not invoke
773 * initscr() more than once. This kludge could be done within the scope
774 * of the specification by using endwin followed by a refresh (in fact,
775 * more than one curses implementation does this); it is guaranteed to work
776 * only with slang.
778 init_curses ();
779 #endif
780 setup_panels ();
782 /* Inform currently running dialog */
783 (*current_dlg->callback) (current_dlg, DLG_RESIZE, 0);
785 #ifdef RESIZABLE_MENUBAR
786 menubar_arrange (the_menubar);
787 #endif
789 /* Now, force the redraw */
790 do_refresh ();
791 touchwin (stdscr);
792 #endif /* TIOCGWINSZ */
793 #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
796 static int ok_to_refresh = 1;
798 void use_dash (int flag)
800 if (flag)
801 ok_to_refresh++;
802 else
803 ok_to_refresh--;
806 void set_hintbar(const char *str)
808 label_set_text (the_hint, str);
809 if (ok_to_refresh > 0)
810 refresh();
813 void print_vfs_message (const char *msg, ...)
815 va_list ap;
816 char str [128];
818 va_start (ap, msg);
820 g_vsnprintf (str, sizeof (str), msg, ap);
821 va_end (ap);
823 if (midnight_shutdown)
824 return;
826 if (!message_visible || !the_hint || !the_hint->widget.parent) {
827 int col, row;
829 if (!nice_rotating_dash || (ok_to_refresh <= 0))
830 return;
832 /* Preserve current cursor position */
833 getyx (stdscr, row, col);
835 move (0, 0);
836 attrset (NORMAL_COLOR);
837 tty_printf ("%-*s", COLS-1, str);
839 /* Restore cursor position */
840 move(row, col);
841 mc_refresh ();
842 return;
845 if (message_visible) {
846 set_hintbar(str);
850 void rotate_dash (void)
852 static const char rotating_dash [] = "|/-\\";
853 static size_t pos = 0;
855 if (!nice_rotating_dash || (ok_to_refresh <= 0))
856 return;
858 if (pos >= sizeof (rotating_dash)-1)
859 pos = 0;
860 move (0, COLS-1);
861 attrset (NORMAL_COLOR);
862 addch (rotating_dash [pos]);
863 mc_refresh ();
864 pos++;
867 const char *get_nth_panel_name (int num)
869 static char buffer [BUF_SMALL];
871 if (!num)
872 return "New Left Panel";
873 else if (num == 1)
874 return "New Right Panel";
875 else {
876 g_snprintf (buffer, sizeof (buffer), "%ith Panel", num);
877 return buffer;
881 /* I wonder if I should start to use the folding mode than Dugan uses */
882 /* */
883 /* This is the centralized managing of the panel display types */
884 /* This routine takes care of destroying and creating new widgets */
885 /* Please note that it could manage MAX_VIEWS, not just left and right */
886 /* Currently nothing in the code takes advantage of this and has hard- */
887 /* coded values for two panels only */
889 /* Set the num-th panel to the view type: type */
890 /* This routine also keeps at least one WPanel object in the screen */
891 /* since a lot of routines depend on the current_panel variable */
892 void set_display_type (int num, int type)
894 int x, y, cols, lines;
895 int the_other; /* Index to the other panel */
896 const char *file_name = NULL; /* For Quick view */
897 Widget *new_widget, *old_widget;
898 WPanel *the_other_panel;
900 x = y = cols = lines = 0;
901 old_widget = 0;
902 if (num >= MAX_VIEWS){
903 fprintf (stderr, "Cannot allocate more that %d views\n", MAX_VIEWS);
904 abort ();
907 /* Check that we will have a WPanel * at least */
908 the_other = 0;
909 if (type != view_listing){
910 the_other = num == 0 ? 1 : 0;
912 if (panels [the_other].type != view_listing)
913 return;
917 /* Get rid of it */
918 if (panels [num].widget){
919 Widget *w = panels [num].widget;
920 WPanel *panel = (WPanel *) panels [num].widget;
922 x = w->x;
923 y = w->y;
924 cols = w->cols;
925 lines = w->lines;
926 old_widget = panels [num].widget;
928 if (panels [num].type == view_listing){
929 if (panel->frame_size == frame_full && type != view_listing){
930 cols = COLS - first_panel_size;
931 if (num == 1)
932 x = first_panel_size;
937 new_widget = 0;
939 switch (type){
940 case view_listing:
941 new_widget = (Widget *) panel_new (get_nth_panel_name (num));
942 break;
944 case view_info:
945 new_widget = (Widget *) info_new ();
947 break;
949 case view_tree:
950 new_widget = (Widget *) tree_new (1, 0, 0, 0, 0);
951 break;
953 case view_quick:
954 new_widget = (Widget *) view_new (0, 0, 0, 0, 1);
955 the_other_panel = (WPanel *) panels [the_other].widget;
956 if (the_other_panel)
957 file_name =
958 the_other_panel->dir.list[the_other_panel->selected].fname;
959 else
960 file_name = "";
962 view_load ((WView *) new_widget, 0, file_name, 0);
963 break;
965 panels [num].type = type;
966 panels [num].widget = (Widget *) new_widget;
968 /* We set the same size the old widget had */
969 widget_set_size ((Widget *) new_widget, y, x, lines, cols);
971 /* We use replace to keep the circular list of the dialog in the */
972 /* same state. Maybe we could just kill it and then replace it */
973 if (midnight_dlg && old_widget){
974 dlg_replace_widget (old_widget, panels [num].widget);
976 if (type == view_listing){
977 if (num == 0)
978 left_panel = (WPanel *) new_widget;
979 else
980 right_panel = (WPanel *) new_widget;
983 if (type == view_tree)
984 the_tree = (WTree *) new_widget;
986 /* Prevent current_panel's value from becoming invalid.
987 * It's just a quick hack to prevent segfaults. Comment out and
988 * try following:
989 * - select left panel
990 * - invoke menue left/tree
991 * - as long as you stay in the left panel almost everything that uses
992 * current_panel causes segfault, e.g. C-Enter, C-x c, ...
995 if (type != view_listing)
996 if (current_panel == (WPanel *) old_widget)
997 current_panel = num == 0 ? right_panel : left_panel;
1000 /* This routine is deeply sticked to the two panels idea.
1001 What should it do in more panels. ANSWER - don't use it
1002 in any multiple panels environment. */
1003 void swap_panels ()
1005 Widget tmp;
1006 Widget *tmp_widget;
1007 WPanel panel;
1008 WPanel *panel1, *panel2;
1009 int tmp_type;
1011 #define panelswap(x) panel. x = panel1-> x; panel1-> x = panel2-> x; panel2-> x = panel. x;
1013 #define panelswapstr(e) strcpy (panel. e, panel1-> e); \
1014 strcpy (panel1-> e, panel2-> e); \
1015 strcpy (panel2-> e, panel. e);
1016 panel1 = (WPanel *) panels [0].widget;
1017 panel2 = (WPanel *) panels [1].widget;
1018 if (panels [0].type == view_listing && panels [1].type == view_listing) {
1019 /* Change everything except format/sort/panel_name etc. */
1020 panelswap (dir);
1021 panelswap (active);
1022 panelswapstr (cwd);
1023 panelswapstr (lwd);
1024 panelswap (count);
1025 panelswap (marked);
1026 panelswap (dirs_marked);
1027 panelswap (total);
1028 panelswap (top_file);
1029 panelswap (selected);
1030 panelswap (is_panelized);
1031 panelswap (dir_stat);
1033 panel1->searching = 0;
1034 panel2->searching = 0;
1035 if (current_panel == panel1)
1036 current_panel = panel2;
1037 else
1038 current_panel = panel1;
1040 if (dlg_widget_active (panels[0].widget))
1041 dlg_select_widget (panels [1].widget);
1042 else if (dlg_widget_active (panels[1].widget))
1043 dlg_select_widget (panels [0].widget);
1044 } else {
1045 WPanel *tmp_panel;
1047 tmp_panel=right_panel;
1048 right_panel=left_panel;
1049 left_panel=tmp_panel;
1051 if (panels [0].type == view_listing) {
1052 if (!strcmp (panel1->panel_name, get_nth_panel_name (0))) {
1053 g_free (panel1->panel_name);
1054 panel1->panel_name = g_strdup (get_nth_panel_name (1));
1057 if (panels [1].type == view_listing) {
1058 if (!strcmp (panel2->panel_name, get_nth_panel_name (1))) {
1059 g_free (panel2->panel_name);
1060 panel2->panel_name = g_strdup (get_nth_panel_name (0));
1064 tmp.x = panels [0].widget->x;
1065 tmp.y = panels [0].widget->y;
1066 tmp.cols = panels [0].widget->cols;
1067 tmp.lines = panels [0].widget->lines;
1069 panels [0].widget->x = panels [1].widget->x;
1070 panels [0].widget->y = panels [1].widget->y;
1071 panels [0].widget->cols = panels [1].widget->cols;
1072 panels [0].widget->lines = panels [1].widget->lines;
1074 panels [1].widget->x = tmp.x;
1075 panels [1].widget->y = tmp.y;
1076 panels [1].widget->cols = tmp.cols;
1077 panels [1].widget->lines = tmp.lines;
1079 tmp_widget = panels [0].widget;
1080 panels [0].widget = panels [1].widget;
1081 panels [1].widget = tmp_widget;
1082 tmp_type = panels [0].type;
1083 panels [0].type = panels [1].type;
1084 panels [1].type = tmp_type;
1088 int get_display_type (int index)
1090 return panels [index].type;
1093 struct Widget *
1094 get_panel_widget (int index)
1096 return panels[index].widget;
1099 int get_current_index (void)
1101 if (panels [0].widget == ((Widget *) current_panel))
1102 return 0;
1103 else
1104 return 1;
1107 int get_other_index (void)
1109 return !get_current_index ();
1112 struct WPanel *
1113 get_other_panel (void)
1115 return (struct WPanel *) get_panel_widget (get_other_index ());
1118 /* Returns the view type for the current panel/view */
1119 int get_current_type (void)
1121 if (panels [0].widget == (Widget *) current_panel)
1122 return panels [0].type;
1123 else
1124 return panels [1].type;
1127 /* Returns the view type of the unselected panel */
1128 int get_other_type (void)
1130 if (panels [0].widget == (Widget *) current_panel)
1131 return panels [1].type;
1132 else
1133 return panels [0].type;