Reorganize WDialog flags.
[midnight-commander.git] / src / filemanager / midnight.c
blob68d12dac0aa1015efb91617583fff5fc1da2b3f7
1 /*
2 Main dialog (file panels) of the Midnight Commander
4 Copyright (C) 1994-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Miguel de Icaza, 1994, 1995, 1996, 1997
9 Janne Kukonlehto, 1994, 1995
10 Norbert Warmuth, 1997
11 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2012, 2013
12 Slava Zanko <slavazanko@gmail.com>, 2013
14 This file is part of the Midnight Commander.
16 The Midnight Commander is free software: you can redistribute it
17 and/or modify it under the terms of the GNU General Public License as
18 published by the Free Software Foundation, either version 3 of the License,
19 or (at your option) any later version.
21 The Midnight Commander is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 /** \file main.c
31 * \brief Source: main dialog (file panels) of the Midnight Commander
34 #include <config.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <locale.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/wait.h>
45 #include <pwd.h> /* for username in xterm title */
47 #include "lib/global.h"
48 #include "lib/fileloc.h" /* MC_HINT */
50 #include "lib/tty/tty.h"
51 #include "lib/tty/key.h" /* KEY_M_* masks */
52 #include "lib/skin.h"
53 #include "lib/util.h"
55 #include "lib/vfs/vfs.h"
57 #include "src/args.h"
58 #ifdef ENABLE_SUBSHELL
59 #include "src/subshell/subshell.h"
60 #endif
61 #include "src/setup.h" /* variables */
62 #include "src/learn.h" /* learn_keys() */
63 #include "src/keybind-defaults.h"
64 #include "lib/keybind.h"
65 #include "lib/event.h"
67 #include "tree.h"
68 #include "boxes.h" /* sort_box(), tree_box() */
69 #include "layout.h"
70 #include "cmd.h" /* commands */
71 #include "hotlist.h"
72 #include "panelize.h"
73 #include "command.h" /* cmdline */
74 #include "dir.h" /* dir_list_clean() */
76 #include "chmod.h"
77 #include "chown.h"
78 #include "achown.h"
80 #ifdef USE_INTERNAL_EDIT
81 #include "src/editor/edit.h"
82 #endif
84 #ifdef USE_DIFF_VIEW
85 #include "src/diffviewer/ydiff.h"
86 #endif
88 #include "src/consaver/cons.saver.h" /* show_console_contents */
90 #include "midnight.h"
92 /*** global variables ****************************************************************************/
94 /* When the modes are active, left_panel, right_panel and tree_panel */
95 /* point to a proper data structure. You should check with the functions */
96 /* get_current_type and get_other_type the types of the panels before using */
97 /* this pointer variables */
99 /* The structures for the panels */
100 WPanel *left_panel = NULL;
101 WPanel *right_panel = NULL;
102 /* Pointer to the selected and unselected panel */
103 WPanel *current_panel = NULL;
105 /* The Menubar */
106 WMenuBar *the_menubar = NULL;
107 /* The widget where we draw the prompt */
108 WLabel *the_prompt;
109 /* The hint bar */
110 WLabel *the_hint;
111 /* The button bar */
112 WButtonBar *the_bar;
114 /* The prompt */
115 const char *mc_prompt = NULL;
117 /*** file scope macro definitions ****************************************************************/
119 #ifdef HAVE_CHARSET
121 * Don't restrict the output on the screen manager level,
122 * the translation tables take care of it.
124 #endif /* !HAVE_CHARSET */
126 /*** file scope type declarations ****************************************************************/
128 /*** file scope variables ************************************************************************/
130 static menu_t *left_menu, *right_menu;
132 static gboolean ctl_x_map_enabled = FALSE;
134 /*** file scope functions ************************************************************************/
136 /** Stop MC main dialog and the current dialog if it exists.
137 * Needed to provide fast exit from MC viewer or editor on shell exit */
138 static void
139 stop_dialogs (void)
141 dlg_stop (midnight_dlg);
143 if ((top_dlg != NULL) && (top_dlg->data != NULL))
144 dlg_stop (DIALOG (top_dlg->data));
147 /* --------------------------------------------------------------------------------------------- */
149 static void
150 treebox_cmd (void)
152 char *sel_dir;
154 sel_dir = tree_box (selection (current_panel)->fname);
155 if (sel_dir != NULL)
157 vfs_path_t *sel_vdir;
159 sel_vdir = vfs_path_from_str (sel_dir);
160 do_cd (sel_vdir, cd_exact);
161 vfs_path_free (sel_vdir);
162 g_free (sel_dir);
166 /* --------------------------------------------------------------------------------------------- */
168 #ifdef LISTMODE_EDITOR
169 static void
170 listmode_cmd (void)
172 char *newmode;
174 if (get_current_type () != view_listing)
175 return;
177 newmode = listmode_edit (current_panel->user_format);
178 if (!newmode)
179 return;
181 g_free (current_panel->user_format);
182 current_panel->list_type = list_user;
183 current_panel->user_format = newmode;
184 set_panel_formats (current_panel);
186 do_refresh ();
188 #endif /* LISTMODE_EDITOR */
190 /* --------------------------------------------------------------------------------------------- */
192 static GList *
193 create_panel_menu (void)
195 GList *entries = NULL;
197 entries = g_list_prepend (entries, menu_entry_create (_("File listin&g"), CK_PanelListing));
198 entries = g_list_prepend (entries, menu_entry_create (_("&Quick view"), CK_PanelQuickView));
199 entries = g_list_prepend (entries, menu_entry_create (_("&Info"), CK_PanelInfo));
200 entries = g_list_prepend (entries, menu_entry_create (_("&Tree"), CK_PanelTree));
201 entries = g_list_prepend (entries, menu_separator_create ());
202 entries =
203 g_list_prepend (entries, menu_entry_create (_("&Listing mode..."), CK_PanelListingChange));
204 entries = g_list_prepend (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
205 entries = g_list_prepend (entries, menu_entry_create (_("&Filter..."), CK_Filter));
206 #ifdef HAVE_CHARSET
207 entries = g_list_prepend (entries, menu_entry_create (_("&Encoding..."), CK_SelectCodepage));
208 #endif
209 entries = g_list_prepend (entries, menu_separator_create ());
210 #ifdef ENABLE_VFS_FTP
211 entries = g_list_prepend (entries, menu_entry_create (_("FT&P link..."), CK_ConnectFtp));
212 #endif
213 #ifdef ENABLE_VFS_FISH
214 entries = g_list_prepend (entries, menu_entry_create (_("S&hell link..."), CK_ConnectFish));
215 #endif
216 #ifdef ENABLE_VFS_SFTP
217 entries = g_list_prepend (entries, menu_entry_create (_("S&FTP link..."), CK_ConnectSftp));
218 #endif
219 #ifdef ENABLE_VFS_SMB
220 entries = g_list_prepend (entries, menu_entry_create (_("SM&B link..."), CK_ConnectSmb));
221 #endif
222 entries = g_list_prepend (entries, menu_entry_create (_("Paneli&ze"), CK_Panelize));
223 entries = g_list_prepend (entries, menu_separator_create ());
224 entries = g_list_prepend (entries, menu_entry_create (_("&Rescan"), CK_Reread));
226 return g_list_reverse (entries);
229 /* --------------------------------------------------------------------------------------------- */
231 static GList *
232 create_file_menu (void)
234 GList *entries = NULL;
236 entries = g_list_prepend (entries, menu_entry_create (_("&View"), CK_View));
237 entries = g_list_prepend (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFile));
238 entries = g_list_prepend (entries, menu_entry_create (_("&Filtered view"), CK_ViewFiltered));
239 entries = g_list_prepend (entries, menu_entry_create (_("&Edit"), CK_Edit));
240 entries = g_list_prepend (entries, menu_entry_create (_("&Copy"), CK_Copy));
241 entries = g_list_prepend (entries, menu_entry_create (_("C&hmod"), CK_ChangeMode));
242 entries = g_list_prepend (entries, menu_entry_create (_("&Link"), CK_Link));
243 entries = g_list_prepend (entries, menu_entry_create (_("&Symlink"), CK_LinkSymbolic));
244 entries =
245 g_list_prepend (entries,
246 menu_entry_create (_("Relative symlin&k"), CK_LinkSymbolicRelative));
247 entries = g_list_prepend (entries, menu_entry_create (_("Edit s&ymlink"), CK_LinkSymbolicEdit));
248 entries = g_list_prepend (entries, menu_entry_create (_("Ch&own"), CK_ChangeOwn));
249 entries =
250 g_list_prepend (entries, menu_entry_create (_("&Advanced chown"), CK_ChangeOwnAdvanced));
251 entries = g_list_prepend (entries, menu_entry_create (_("&Rename/Move"), CK_Move));
252 entries = g_list_prepend (entries, menu_entry_create (_("&Mkdir"), CK_MakeDir));
253 entries = g_list_prepend (entries, menu_entry_create (_("&Delete"), CK_Delete));
254 entries = g_list_prepend (entries, menu_entry_create (_("&Quick cd"), CK_CdQuick));
255 entries = g_list_prepend (entries, menu_separator_create ());
256 entries = g_list_prepend (entries, menu_entry_create (_("Select &group"), CK_Select));
257 entries = g_list_prepend (entries, menu_entry_create (_("U&nselect group"), CK_Unselect));
258 entries = g_list_prepend (entries, menu_entry_create (_("&Invert selection"), CK_SelectInvert));
259 entries = g_list_prepend (entries, menu_separator_create ());
260 entries = g_list_prepend (entries, menu_entry_create (_("E&xit"), CK_Quit));
262 return g_list_reverse (entries);
265 /* --------------------------------------------------------------------------------------------- */
267 static GList *
268 create_command_menu (void)
270 /* I know, I'm lazy, but the tree widget when it's not running
271 * as a panel still has some problems, I have not yet finished
272 * the WTree widget port, sorry.
274 GList *entries = NULL;
276 entries = g_list_prepend (entries, menu_entry_create (_("&User menu"), CK_UserMenu));
277 entries = g_list_prepend (entries, menu_entry_create (_("&Directory tree"), CK_Tree));
278 entries = g_list_prepend (entries, menu_entry_create (_("&Find file"), CK_Find));
279 entries = g_list_prepend (entries, menu_entry_create (_("S&wap panels"), CK_Swap));
280 entries = g_list_prepend (entries, menu_entry_create (_("Switch &panels on/off"), CK_Shell));
281 entries =
282 g_list_prepend (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirs));
283 #ifdef USE_DIFF_VIEW
284 entries = g_list_prepend (entries, menu_entry_create (_("C&ompare files"), CK_CompareFiles));
285 #endif
286 entries =
287 g_list_prepend (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
288 entries = g_list_prepend (entries, menu_entry_create (_("Show directory s&izes"), CK_DirSize));
289 entries = g_list_prepend (entries, menu_separator_create ());
290 entries = g_list_prepend (entries, menu_entry_create (_("Command &history"), CK_History));
291 entries = g_list_prepend (entries, menu_entry_create (_("Di&rectory hotlist"), CK_HotList));
292 #ifdef ENABLE_VFS
293 entries = g_list_prepend (entries, menu_entry_create (_("&Active VFS list"), CK_VfsList));
294 #endif
295 #ifdef ENABLE_BACKGROUND
296 entries = g_list_prepend (entries, menu_entry_create (_("&Background jobs"), CK_Jobs));
297 #endif
298 entries = g_list_prepend (entries, menu_entry_create (_("Screen lis&t"), CK_ScreenList));
299 entries = g_list_prepend (entries, menu_separator_create ());
300 #ifdef ENABLE_VFS_UNDELFS
301 entries =
302 g_list_prepend (entries,
303 menu_entry_create (_("&Undelete files (ext2fs only)"), CK_Undelete));
304 #endif
305 #ifdef LISTMODE_EDITOR
306 entries = g_list_prepend (entries, menu_entry_create (_("&Listing format edit"), CK_ListMode));
307 #endif
308 #if defined (ENABLE_VFS_UNDELFS) || defined (LISTMODE_EDITOR)
309 entries = g_list_prepend (entries, menu_separator_create ());
310 #endif
311 entries =
312 g_list_prepend (entries,
313 menu_entry_create (_("Edit &extension file"), CK_EditExtensionsFile));
314 entries = g_list_prepend (entries, menu_entry_create (_("Edit &menu file"), CK_EditUserMenu));
315 entries =
316 g_list_prepend (entries,
317 menu_entry_create (_("Edit hi&ghlighting group file"),
318 CK_EditFileHighlightFile));
320 return g_list_reverse (entries);
323 /* --------------------------------------------------------------------------------------------- */
325 static GList *
326 create_options_menu (void)
328 GList *entries = NULL;
330 entries = g_list_prepend (entries, menu_entry_create (_("&Configuration..."), CK_Options));
331 entries = g_list_prepend (entries, menu_entry_create (_("&Layout..."), CK_OptionsLayout));
332 entries = g_list_prepend (entries, menu_entry_create (_("&Panel options..."), CK_OptionsPanel));
333 entries =
334 g_list_prepend (entries, menu_entry_create (_("C&onfirmation..."), CK_OptionsConfirm));
335 entries =
336 g_list_prepend (entries, menu_entry_create (_("&Appearance..."), CK_OptionsAppearance));
337 entries =
338 g_list_prepend (entries, menu_entry_create (_("&Display bits..."), CK_OptionsDisplayBits));
339 entries = g_list_prepend (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
340 #ifdef ENABLE_VFS
341 entries = g_list_prepend (entries, menu_entry_create (_("&Virtual FS..."), CK_OptionsVfs));
342 #endif
343 entries = g_list_prepend (entries, menu_separator_create ());
344 entries = g_list_prepend (entries, menu_entry_create (_("&Save setup"), CK_SaveSetup));
346 return g_list_reverse (entries);
349 /* --------------------------------------------------------------------------------------------- */
351 static void
352 init_menu (void)
354 left_menu = create_menu ("", create_panel_menu (), "[Left and Right Menus]");
355 menubar_add_menu (the_menubar, left_menu);
356 menubar_add_menu (the_menubar, create_menu (_("&File"), create_file_menu (), "[File Menu]"));
357 menubar_add_menu (the_menubar,
358 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
359 menubar_add_menu (the_menubar,
360 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
361 right_menu = create_menu ("", create_panel_menu (), "[Left and Right Menus]");
362 menubar_add_menu (the_menubar, right_menu);
363 update_menu ();
366 /* --------------------------------------------------------------------------------------------- */
368 static void
369 menu_last_selected_cmd (void)
371 menubar_activate (the_menubar, drop_menus != 0, -1);
374 /* --------------------------------------------------------------------------------------------- */
376 static void
377 menu_cmd (void)
379 int selected;
381 if ((get_current_index () == 0) == (current_panel->active != 0))
382 selected = 0;
383 else
384 selected = g_list_length (the_menubar->menu) - 1;
386 menubar_activate (the_menubar, drop_menus != 0, selected);
389 /* --------------------------------------------------------------------------------------------- */
391 static void
392 sort_cmd (void)
394 WPanel *p;
395 const panel_field_t *sort_order;
397 if (!SELECTED_IS_PANEL)
398 return;
400 p = MENU_PANEL;
401 sort_order = sort_box (&p->sort_info, p->sort_field);
402 panel_set_sort_order (p, sort_order);
405 /* --------------------------------------------------------------------------------------------- */
407 static char *
408 midnight_get_shortcut (long command)
410 const char *ext_map;
411 const char *shortcut = NULL;
413 shortcut = keybind_lookup_keymap_shortcut (main_map, command);
414 if (shortcut != NULL)
415 return g_strdup (shortcut);
417 shortcut = keybind_lookup_keymap_shortcut (panel_map, command);
418 if (shortcut != NULL)
419 return g_strdup (shortcut);
421 ext_map = keybind_lookup_keymap_shortcut (main_map, CK_ExtendedKeyMap);
422 if (ext_map != NULL)
423 shortcut = keybind_lookup_keymap_shortcut (main_x_map, command);
424 if (shortcut != NULL)
425 return g_strdup_printf ("%s %s", ext_map, shortcut);
427 return NULL;
430 /* --------------------------------------------------------------------------------------------- */
432 static char *
433 midnight_get_title (const WDialog * h, size_t len)
435 char *path;
436 char *login;
437 char *p;
439 (void) h;
441 title_path_prepare (&path, &login);
443 p = g_strdup_printf ("%s [%s]:%s", _("Panels:"), login, path);
444 g_free (path);
445 g_free (login);
446 path = g_strdup (str_trunc (p, len - 4));
447 g_free (p);
449 return path;
452 /* --------------------------------------------------------------------------------------------- */
454 static void
455 toggle_panels_split (void)
457 panels_layout.horizontal_split = !panels_layout.horizontal_split;
458 layout_change ();
459 do_refresh ();
462 /* --------------------------------------------------------------------------------------------- */
464 #ifdef ENABLE_VFS
465 /* event helper */
466 static gboolean
467 check_panel_timestamp (const WPanel * panel, panel_view_mode_t mode, struct vfs_class *vclass,
468 vfsid id)
470 if (mode == view_listing)
472 const vfs_path_element_t *path_element;
474 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
476 if (path_element->class != vclass)
477 return FALSE;
479 if (vfs_getid (panel->cwd_vpath) != id)
480 return FALSE;
482 return TRUE;
485 /* --------------------------------------------------------------------------------------------- */
487 /* event callback */
488 static gboolean
489 check_current_panel_timestamp (const gchar * event_group_name, const gchar * event_name,
490 gpointer init_data, gpointer data)
492 ev_vfs_stamp_create_t *event_data = (ev_vfs_stamp_create_t *) data;
494 (void) event_group_name;
495 (void) event_name;
496 (void) init_data;
498 event_data->ret =
499 check_panel_timestamp (current_panel, get_current_type (), event_data->vclass,
500 event_data->id);
501 return !event_data->ret;
504 /* --------------------------------------------------------------------------------------------- */
506 /* event callback */
507 static gboolean
508 check_other_panel_timestamp (const gchar * event_group_name, const gchar * event_name,
509 gpointer init_data, gpointer data)
511 ev_vfs_stamp_create_t *event_data = (ev_vfs_stamp_create_t *) data;
513 (void) event_group_name;
514 (void) event_name;
515 (void) init_data;
517 event_data->ret =
518 check_panel_timestamp (other_panel, get_other_type (), event_data->vclass, event_data->id);
519 return !event_data->ret;
521 #endif /* ENABLE_VFS */
523 /* --------------------------------------------------------------------------------------------- */
525 /* event callback */
526 static gboolean
527 print_vfs_message (const gchar * event_group_name, const gchar * event_name,
528 gpointer init_data, gpointer data)
530 ev_vfs_print_message_t *event_data = (ev_vfs_print_message_t *) data;
532 (void) event_group_name;
533 (void) event_name;
534 (void) init_data;
536 if (mc_global.midnight_shutdown)
537 goto ret;
539 if (!mc_global.message_visible || the_hint == NULL || WIDGET (the_hint)->owner == NULL)
541 int col, row;
543 if (!nice_rotating_dash || (ok_to_refresh <= 0))
544 goto ret;
546 /* Preserve current cursor position */
547 tty_getyx (&row, &col);
549 tty_gotoyx (0, 0);
550 tty_setcolor (NORMAL_COLOR);
551 tty_print_string (str_fit_to_term (event_data->msg, COLS - 1, J_LEFT));
553 /* Restore cursor position */
554 tty_gotoyx (row, col);
555 mc_refresh ();
556 goto ret;
559 if (mc_global.message_visible)
560 set_hintbar (event_data->msg);
562 ret:
563 MC_PTR_FREE (event_data->msg);
564 return TRUE;
567 /* --------------------------------------------------------------------------------------------- */
569 static void
570 create_panels (void)
572 int current_index, other_index;
573 panel_view_mode_t current_mode, other_mode;
574 char *current_dir, *other_dir;
575 vfs_path_t *original_dir;
578 * Following cases from command line are possible:
579 * 'mc' (no arguments): mc_run_param0 == NULL, mc_run_param1 == NULL
580 * active panel uses current directory
581 * passive panel uses "other_dir" from panels.ini
583 * 'mc dir1 dir2' (two arguments): mc_run_param0 != NULL, mc_run_param1 != NULL
584 * active panel uses mc_run_param0
585 * passive panel uses mc_run_param1
587 * 'mc dir1' (single argument): mc_run_param0 != NULL, mc_run_param1 == NULL
588 * active panel uses mc_run_param0
589 * passive panel uses "other_dir" from panels.ini
592 /* Set up panel directories */
593 if (boot_current_is_left)
595 /* left panel is active */
596 current_index = 0;
597 other_index = 1;
598 current_mode = startup_left_mode;
599 other_mode = startup_right_mode;
601 if (mc_run_param0 == NULL && mc_run_param1 == NULL)
603 /* no arguments */
604 current_dir = NULL; /* assume current dir */
605 other_dir = saved_other_dir; /* from ini */
607 else if (mc_run_param0 != NULL && mc_run_param1 != NULL)
609 /* two arguments */
610 current_dir = (char *) mc_run_param0;
611 other_dir = mc_run_param1;
613 else /* mc_run_param0 != NULL && mc_run_param1 == NULL */
615 /* one argument */
616 current_dir = (char *) mc_run_param0;
617 other_dir = saved_other_dir; /* from ini */
620 else
622 /* right panel is active */
623 current_index = 1;
624 other_index = 0;
625 current_mode = startup_right_mode;
626 other_mode = startup_left_mode;
628 if (mc_run_param0 == NULL && mc_run_param1 == NULL)
630 /* no arguments */
631 current_dir = NULL; /* assume current dir */
632 other_dir = saved_other_dir; /* from ini */
634 else if (mc_run_param0 != NULL && mc_run_param1 != NULL)
636 /* two arguments */
637 current_dir = (char *) mc_run_param0;
638 other_dir = mc_run_param1;
640 else /* mc_run_param0 != NULL && mc_run_param1 == NULL */
642 /* one argument */
643 current_dir = (char *) mc_run_param0;
644 other_dir = saved_other_dir; /* from ini */
648 /* 1. Get current dir */
649 original_dir = vfs_path_clone (vfs_get_raw_current_dir ());
651 /* 2. Create passive panel */
652 if (other_dir != NULL)
654 vfs_path_t *vpath;
656 if (g_path_is_absolute (other_dir))
657 vpath = vfs_path_from_str (other_dir);
658 else
659 vpath = vfs_path_append_new (original_dir, other_dir, (char *) NULL);
660 mc_chdir (vpath);
661 vfs_path_free (vpath);
663 set_display_type (other_index, other_mode);
665 /* 3. Create active panel */
666 if (current_dir == NULL)
667 mc_chdir (original_dir);
668 else
670 vfs_path_t *vpath;
672 if (g_path_is_absolute (current_dir))
673 vpath = vfs_path_from_str (current_dir);
674 else
675 vpath = vfs_path_append_new (original_dir, current_dir, (char *) NULL);
676 mc_chdir (vpath);
677 vfs_path_free (vpath);
679 set_display_type (current_index, current_mode);
681 if (startup_left_mode == view_listing)
682 current_panel = left_panel;
683 else if (right_panel != NULL)
684 current_panel = right_panel;
685 else
686 current_panel = left_panel;
688 vfs_path_free (original_dir);
690 #ifdef ENABLE_VFS
691 mc_event_add (MCEVENT_GROUP_CORE, "vfs_timestamp", check_other_panel_timestamp, NULL, NULL);
692 mc_event_add (MCEVENT_GROUP_CORE, "vfs_timestamp", check_current_panel_timestamp, NULL, NULL);
693 #endif /* ENABLE_VFS */
695 mc_event_add (MCEVENT_GROUP_CORE, "vfs_print_message", print_vfs_message, NULL, NULL);
697 /* Create the nice widgets */
698 cmdline = command_new (0, 0, 0);
699 the_prompt = label_new (0, 0, mc_prompt);
700 the_prompt->transparent = 1;
701 the_bar = buttonbar_new (mc_global.keybar_visible);
703 the_hint = label_new (0, 0, 0);
704 the_hint->transparent = 1;
705 the_hint->auto_adjust_cols = 0;
706 WIDGET (the_hint)->cols = COLS;
708 the_menubar = menubar_new (0, 0, COLS, NULL, menubar_visible);
711 /* --------------------------------------------------------------------------------------------- */
713 static void
714 midnight_put_panel_path (WPanel * panel)
716 vfs_path_t *cwd_vpath;
717 const char *cwd_vpath_str;
719 if (!command_prompt)
720 return;
722 #ifdef HAVE_CHARSET
723 cwd_vpath = remove_encoding_from_path (panel->cwd_vpath);
724 #else
725 cwd_vpath = vfs_path_clone (panel->cwd_vpath);
726 #endif
728 cwd_vpath_str = vfs_path_as_str (cwd_vpath);
730 command_insert (cmdline, cwd_vpath_str, FALSE);
732 if (!IS_PATH_SEP (cwd_vpath_str[strlen (cwd_vpath_str) - 1]))
733 command_insert (cmdline, PATH_SEP_STR, FALSE);
735 vfs_path_free (cwd_vpath);
738 /* --------------------------------------------------------------------------------------------- */
740 static void
741 put_link (WPanel * panel)
743 if (!command_prompt)
744 return;
745 if (S_ISLNK (selection (panel)->st.st_mode))
747 char buffer[MC_MAXPATHLEN];
748 vfs_path_t *vpath;
749 int i;
751 vpath = vfs_path_append_new (panel->cwd_vpath, selection (panel)->fname, (char *) NULL);
752 i = mc_readlink (vpath, buffer, sizeof (buffer) - 1);
753 vfs_path_free (vpath);
755 if (i > 0)
757 buffer[i] = '\0';
758 command_insert (cmdline, buffer, TRUE);
763 /* --------------------------------------------------------------------------------------------- */
765 static void
766 put_current_link (void)
768 put_link (current_panel);
771 /* --------------------------------------------------------------------------------------------- */
773 static void
774 put_other_link (void)
776 if (get_other_type () == view_listing)
777 put_link (other_panel);
780 /* --------------------------------------------------------------------------------------------- */
782 /** Insert the selected file name into the input line */
783 static void
784 put_prog_name (void)
786 const char *tmp;
788 if (!command_prompt)
789 return;
791 if (get_current_type () == view_tree)
793 WTree *tree;
794 const vfs_path_t *selected_name;
796 tree = (WTree *) get_panel_widget (get_current_index ());
797 selected_name = tree_selected_name (tree);
798 tmp = vfs_path_as_str (selected_name);
800 else
801 tmp = selection (current_panel)->fname;
803 command_insert (cmdline, tmp, TRUE);
806 /* --------------------------------------------------------------------------------------------- */
808 static void
809 put_tagged (WPanel * panel)
811 if (!command_prompt)
812 return;
813 input_disable_update (cmdline);
814 if (panel->marked)
816 int i;
818 for (i = 0; i < panel->dir.len; i++)
820 if (panel->dir.list[i].f.marked)
821 command_insert (cmdline, panel->dir.list[i].fname, TRUE);
824 else
826 command_insert (cmdline, panel->dir.list[panel->selected].fname, TRUE);
828 input_enable_update (cmdline);
831 /* --------------------------------------------------------------------------------------------- */
833 static void
834 put_current_tagged (void)
836 put_tagged (current_panel);
839 /* --------------------------------------------------------------------------------------------- */
841 static void
842 put_other_tagged (void)
844 if (get_other_type () == view_listing)
845 put_tagged (other_panel);
848 /* --------------------------------------------------------------------------------------------- */
850 static void
851 ctl_x_cmd (void)
853 ctl_x_map_enabled = TRUE;
856 /* --------------------------------------------------------------------------------------------- */
858 static void
859 setup_mc (void)
861 #ifdef HAVE_SLANG
862 #ifdef HAVE_CHARSET
863 tty_display_8bit (TRUE);
864 #else
865 tty_display_8bit (mc_global.full_eight_bits != 0);
866 #endif /* HAVE_CHARSET */
868 #else /* HAVE_SLANG */
870 #ifdef HAVE_CHARSET
871 tty_display_8bit (TRUE);
872 #else
873 tty_display_8bit (mc_global.eight_bit_clean != 0);
874 #endif /* HAVE_CHARSET */
875 #endif /* HAVE_SLANG */
877 #ifdef ENABLE_SUBSHELL
878 if (mc_global.tty.use_subshell)
879 add_select_channel (mc_global.tty.subshell_pty, load_prompt, 0);
880 #endif /* !ENABLE_SUBSHELL */
882 if ((tty_baudrate () < 9600) || mc_global.tty.slow_terminal)
883 verbose = 0;
886 /* --------------------------------------------------------------------------------------------- */
888 static void
889 setup_dummy_mc (void)
891 vfs_path_t *vpath;
892 char *d;
893 int ret;
895 d = _vfs_get_cwd ();
896 setup_mc ();
897 vpath = vfs_path_from_str (d);
898 ret = mc_chdir (vpath);
899 (void) ret;
900 vfs_path_free (vpath);
901 g_free (d);
904 /* --------------------------------------------------------------------------------------------- */
906 static void
907 done_mc (void)
909 /* Setup shutdown
911 * We sync the profiles since the hotlist may have changed, while
912 * we only change the setup data if we have the auto save feature set
914 const char *curr_dir;
916 save_setup (auto_save_setup, panels_options.auto_save_setup);
918 curr_dir = vfs_get_current_dir ();
919 vfs_stamp_path (curr_dir);
921 if ((current_panel != NULL) && (get_current_type () == view_listing))
922 vfs_stamp_path (vfs_path_as_str (current_panel->cwd_vpath));
924 if ((other_panel != NULL) && (get_other_type () == view_listing))
925 vfs_stamp_path (vfs_path_as_str (other_panel->cwd_vpath));
928 /* --------------------------------------------------------------------------------------------- */
930 static void
931 create_panels_and_run_mc (void)
933 midnight_dlg->get_shortcut = midnight_get_shortcut;
934 midnight_dlg->get_title = midnight_get_title;
936 create_panels ();
938 add_widget (midnight_dlg, the_menubar);
939 init_menu ();
941 add_widget (midnight_dlg, get_panel_widget (0));
942 add_widget (midnight_dlg, get_panel_widget (1));
944 add_widget (midnight_dlg, the_hint);
945 add_widget (midnight_dlg, cmdline);
946 add_widget (midnight_dlg, the_prompt);
948 add_widget (midnight_dlg, the_bar);
949 midnight_set_buttonbar (the_bar);
951 /* Run the Midnight Commander if no file was specified in the command line */
952 dlg_run (midnight_dlg);
955 /* --------------------------------------------------------------------------------------------- */
957 /** result must be free'd (I think this should go in util.c) */
958 static vfs_path_t *
959 prepend_cwd_on_local (const char *filename)
961 vfs_path_t *vpath;
963 vpath = vfs_path_from_str (filename);
964 if (!vfs_file_is_local (vpath) || g_path_is_absolute (filename))
965 return vpath;
967 vfs_path_free (vpath);
969 return vfs_path_append_new (vfs_get_raw_current_dir (), filename, (char *) NULL);
972 /* --------------------------------------------------------------------------------------------- */
974 /** Invoke the internal view/edit routine with:
975 * the default processing and forcing the internal viewer/editor
977 static gboolean
978 mc_maybe_editor_or_viewer (void)
980 gboolean ret;
982 switch (mc_global.mc_run_mode)
984 #ifdef USE_INTERNAL_EDIT
985 case MC_RUN_EDITOR:
986 ret = edit_files ((GList *) mc_run_param0);
987 break;
988 #endif /* USE_INTERNAL_EDIT */
989 case MC_RUN_VIEWER:
991 vfs_path_t *vpath = NULL;
993 if (mc_run_param0 != NULL && *(char *) mc_run_param0 != '\0')
994 vpath = prepend_cwd_on_local ((char *) mc_run_param0);
996 ret = view_file (vpath, FALSE, TRUE);
997 vfs_path_free (vpath);
998 break;
1000 #ifdef USE_DIFF_VIEW
1001 case MC_RUN_DIFFVIEWER:
1002 ret = dview_diff_cmd (mc_run_param0, mc_run_param1);
1003 break;
1004 #endif /* USE_DIFF_VIEW */
1005 default:
1006 ret = FALSE;
1009 return ret;
1012 /* --------------------------------------------------------------------------------------------- */
1014 static gboolean
1015 quit_cmd_internal (int quiet)
1017 int q = quit;
1018 size_t n;
1020 n = dialog_switch_num () - 1;
1021 if (n != 0)
1023 char msg[BUF_MEDIUM];
1025 g_snprintf (msg, sizeof (msg),
1026 ngettext ("You have %zd opened screen. Quit anyway?",
1027 "You have %zd opened screens. Quit anyway?", n), n);
1029 if (query_dialog (_("The Midnight Commander"), msg, D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
1030 return FALSE;
1031 q = 1;
1033 else if (quiet || !confirm_exit)
1034 q = 1;
1035 else if (query_dialog (_("The Midnight Commander"),
1036 _("Do you really want to quit the Midnight Commander?"),
1037 D_NORMAL, 2, _("&Yes"), _("&No")) == 0)
1038 q = 1;
1040 if (q != 0)
1042 #ifdef ENABLE_SUBSHELL
1043 if (!mc_global.tty.use_subshell)
1044 stop_dialogs ();
1045 else if ((q = exit_subshell ()? 1 : 0) != 0)
1046 #endif
1047 stop_dialogs ();
1050 if (q != 0)
1051 quit |= 1;
1052 return (quit != 0);
1055 /* --------------------------------------------------------------------------------------------- */
1057 static gboolean
1058 quit_cmd (void)
1060 return quit_cmd_internal (0);
1063 /* --------------------------------------------------------------------------------------------- */
1066 * Repaint the contents of the panels without frames. To schedule panel
1067 * for repainting, set panel->dirty to 1. There are many reasons why
1068 * the panels need to be repainted, and this is a costly operation, so
1069 * it's done once per event.
1072 static void
1073 update_dirty_panels (void)
1075 if (get_current_type () == view_listing && current_panel->dirty)
1076 widget_redraw (WIDGET (current_panel));
1078 if (get_other_type () == view_listing && other_panel->dirty)
1079 widget_redraw (WIDGET (other_panel));
1082 /* --------------------------------------------------------------------------------------------- */
1084 static void
1085 toggle_show_hidden (void)
1087 panels_options.show_dot_files = !panels_options.show_dot_files;
1088 update_panels (UP_RELOAD, UP_KEEPSEL);
1089 /* redraw panels forced */
1090 update_dirty_panels ();
1093 /* --------------------------------------------------------------------------------------------- */
1095 static cb_ret_t
1096 midnight_execute_cmd (Widget * sender, long command)
1098 cb_ret_t res = MSG_HANDLED;
1100 (void) sender;
1102 /* stop quick search before executing any command */
1103 send_message (current_panel, NULL, MSG_ACTION, CK_SearchStop, NULL);
1105 switch (command)
1107 case CK_HotListAdd:
1108 add2hotlist_cmd ();
1109 break;
1110 case CK_PanelListingChange:
1111 change_listing_cmd ();
1112 break;
1113 case CK_ChangeMode:
1114 chmod_cmd ();
1115 break;
1116 case CK_ChangeOwn:
1117 chown_cmd ();
1118 break;
1119 case CK_ChangeOwnAdvanced:
1120 chown_advanced_cmd ();
1121 break;
1122 case CK_CompareDirs:
1123 compare_dirs_cmd ();
1124 break;
1125 case CK_Options:
1126 configure_box ();
1127 break;
1128 #ifdef ENABLE_VFS
1129 case CK_OptionsVfs:
1130 configure_vfs ();
1131 break;
1132 #endif
1133 case CK_OptionsConfirm:
1134 confirm_box ();
1135 break;
1136 case CK_Copy:
1137 copy_cmd ();
1138 break;
1139 case CK_PutCurrentPath:
1140 midnight_put_panel_path (current_panel);
1141 break;
1142 case CK_PutCurrentLink:
1143 put_current_link ();
1144 break;
1145 case CK_PutCurrentTagged:
1146 put_current_tagged ();
1147 break;
1148 case CK_PutOtherPath:
1149 if (get_other_type () == view_listing)
1150 midnight_put_panel_path (other_panel);
1151 break;
1152 case CK_PutOtherLink:
1153 put_other_link ();
1154 break;
1155 case CK_PutOtherTagged:
1156 put_other_tagged ();
1157 break;
1158 case CK_Delete:
1159 delete_cmd ();
1160 break;
1161 case CK_ScreenList:
1162 dialog_switch_list ();
1163 break;
1164 #ifdef USE_DIFF_VIEW
1165 case CK_CompareFiles:
1166 diff_view_cmd ();
1167 break;
1168 #endif
1169 case CK_OptionsDisplayBits:
1170 display_bits_box ();
1171 break;
1172 case CK_Edit:
1173 edit_cmd ();
1174 break;
1175 #ifdef USE_INTERNAL_EDIT
1176 case CK_EditForceInternal:
1177 edit_cmd_force_internal ();
1178 break;
1179 #endif
1180 case CK_EditExtensionsFile:
1181 ext_cmd ();
1182 break;
1183 case CK_EditFileHighlightFile:
1184 edit_fhl_cmd ();
1185 break;
1186 case CK_EditUserMenu:
1187 edit_mc_menu_cmd ();
1188 break;
1189 case CK_LinkSymbolicEdit:
1190 edit_symlink_cmd ();
1191 break;
1192 case CK_ExternalPanelize:
1193 external_panelize ();
1194 break;
1195 case CK_Filter:
1196 filter_cmd ();
1197 break;
1198 case CK_ViewFiltered:
1199 view_filtered_cmd ();
1200 break;
1201 case CK_Find:
1202 find_cmd ();
1203 break;
1204 #ifdef ENABLE_VFS_FISH
1205 case CK_ConnectFish:
1206 fishlink_cmd ();
1207 break;
1208 #endif
1209 #ifdef ENABLE_VFS_FTP
1210 case CK_ConnectFtp:
1211 ftplink_cmd ();
1212 break;
1213 #endif
1214 #ifdef ENABLE_VFS_SFTP
1215 case CK_ConnectSftp:
1216 sftplink_cmd ();
1217 break;
1218 #endif
1219 #ifdef ENABLE_VFS_SMB
1220 case CK_ConnectSmb:
1221 smblink_cmd ();
1222 break;
1223 #endif /* ENABLE_VFS_SMB */
1224 case CK_Panelize:
1225 cd_panelize_cmd ();
1226 break;
1227 case CK_Help:
1228 help_cmd ();
1229 break;
1230 case CK_History:
1231 /* show the history of command line widget */
1232 send_message (cmdline, NULL, MSG_ACTION, CK_History, NULL);
1233 break;
1234 case CK_PanelInfo:
1235 if (sender == WIDGET (the_menubar))
1236 info_cmd (); /* menu */
1237 else
1238 info_cmd_no_menu (); /* shortcut or buttonbar */
1239 break;
1240 #ifdef ENABLE_BACKGROUND
1241 case CK_Jobs:
1242 jobs_cmd ();
1243 break;
1244 #endif
1245 case CK_OptionsLayout:
1246 layout_box ();
1247 break;
1248 case CK_OptionsAppearance:
1249 appearance_box ();
1250 break;
1251 case CK_LearnKeys:
1252 learn_keys ();
1253 break;
1254 case CK_Link:
1255 link_cmd (LINK_HARDLINK);
1256 break;
1257 case CK_PanelListing:
1258 listing_cmd ();
1259 break;
1260 #ifdef LISTMODE_EDITOR
1261 case CK_ListMode:
1262 listmode_cmd ();
1263 break;
1264 #endif
1265 case CK_Menu:
1266 menu_cmd ();
1267 break;
1268 case CK_MenuLastSelected:
1269 menu_last_selected_cmd ();
1270 break;
1271 case CK_MakeDir:
1272 mkdir_cmd ();
1273 break;
1274 case CK_OptionsPanel:
1275 panel_options_box ();
1276 break;
1277 #ifdef HAVE_CHARSET
1278 case CK_SelectCodepage:
1279 encoding_cmd ();
1280 break;
1281 #endif
1282 case CK_CdQuick:
1283 quick_cd_cmd ();
1284 break;
1285 case CK_HotList:
1286 hotlist_cmd ();
1287 break;
1288 case CK_PanelQuickView:
1289 if (sender == WIDGET (the_menubar))
1290 quick_view_cmd (); /* menu */
1291 else
1292 quick_cmd_no_menu (); /* shortcut or buttonabr */
1293 break;
1294 case CK_QuitQuiet:
1295 quiet_quit_cmd ();
1296 break;
1297 case CK_Quit:
1298 quit_cmd ();
1299 break;
1300 case CK_LinkSymbolicRelative:
1301 link_cmd (LINK_SYMLINK_RELATIVE);
1302 break;
1303 case CK_Move:
1304 rename_cmd ();
1305 break;
1306 case CK_Reread:
1307 reread_cmd ();
1308 break;
1309 #ifdef ENABLE_VFS
1310 case CK_VfsList:
1311 vfs_list ();
1312 break;
1313 #endif
1314 case CK_SaveSetup:
1315 save_setup_cmd ();
1316 break;
1317 case CK_Select:
1318 case CK_Unselect:
1319 case CK_SelectInvert:
1320 res = send_message (current_panel, midnight_dlg, MSG_ACTION, command, NULL);
1321 break;
1322 case CK_Shell:
1323 view_other_cmd ();
1324 break;
1325 case CK_DirSize:
1326 smart_dirsize_cmd ();
1327 break;
1328 case CK_Sort:
1329 sort_cmd ();
1330 break;
1331 case CK_ExtendedKeyMap:
1332 ctl_x_cmd ();
1333 break;
1334 case CK_Suspend:
1335 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
1336 break;
1337 case CK_Swap:
1338 swap_cmd ();
1339 break;
1340 case CK_LinkSymbolic:
1341 link_cmd (LINK_SYMLINK_ABSOLUTE);
1342 break;
1343 case CK_PanelListingSwitch:
1344 toggle_listing_cmd ();
1345 break;
1346 case CK_ShowHidden:
1347 toggle_show_hidden ();
1348 break;
1349 case CK_SplitVertHoriz:
1350 toggle_panels_split ();
1351 break;
1352 case CK_SplitEqual:
1353 panels_split_equal ();
1354 break;
1355 case CK_SplitMore:
1356 panels_split_more ();
1357 break;
1358 case CK_SplitLess:
1359 panels_split_less ();
1360 break;
1361 case CK_PanelTree:
1362 panel_tree_cmd ();
1363 break;
1364 case CK_Tree:
1365 treebox_cmd ();
1366 break;
1367 #ifdef ENABLE_VFS_UNDELFS
1368 case CK_Undelete:
1369 undelete_cmd ();
1370 break;
1371 #endif
1372 case CK_UserMenu:
1373 user_file_menu_cmd ();
1374 break;
1375 case CK_View:
1376 view_cmd ();
1377 break;
1378 case CK_ViewFile:
1379 view_file_cmd ();
1380 break;
1381 case CK_Cancel:
1382 /* don't close panels due to SIGINT */
1383 break;
1384 default:
1385 res = MSG_NOT_HANDLED;
1388 return res;
1391 /* --------------------------------------------------------------------------------------------- */
1393 static cb_ret_t
1394 midnight_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
1396 long command;
1398 switch (msg)
1400 case MSG_INIT:
1401 panel_init ();
1402 setup_panels ();
1403 return MSG_HANDLED;
1405 case MSG_DRAW:
1406 load_hint (TRUE);
1407 /* We handle the special case of the output lines */
1408 if (mc_global.tty.console_flag != '\0' && output_lines)
1409 show_console_contents (output_start_y,
1410 LINES - output_lines - mc_global.keybar_visible -
1411 1, LINES - mc_global.keybar_visible - 1);
1412 return MSG_HANDLED;
1414 case MSG_RESIZE:
1415 /* dlg_set_size() is surplus for this case */
1416 w->lines = LINES;
1417 w->cols = COLS;
1418 setup_panels ();
1419 menubar_arrange (the_menubar);
1420 return MSG_HANDLED;
1422 case MSG_IDLE:
1423 /* We only need the first idle event to show user menu after start */
1424 widget_idle (w, FALSE);
1426 if (boot_current_is_left)
1427 dlg_select_widget (get_panel_widget (0));
1428 else
1429 dlg_select_widget (get_panel_widget (1));
1431 if (auto_menu)
1432 midnight_execute_cmd (NULL, CK_UserMenu);
1433 return MSG_HANDLED;
1435 case MSG_KEY:
1436 if (ctl_x_map_enabled)
1438 ctl_x_map_enabled = FALSE;
1439 command = keybind_lookup_keymap_command (main_x_map, parm);
1440 if (command != CK_IgnoreKey)
1441 return midnight_execute_cmd (NULL, command);
1444 /* FIXME: should handle all menu shortcuts before this point */
1445 if (the_menubar->is_active)
1446 return MSG_NOT_HANDLED;
1448 if (parm == '\t')
1449 input_free_completions (cmdline);
1451 if (parm == '\n')
1453 size_t i;
1455 /* HACK: don't execute command in the command line if Enter was pressed
1456 in the quick viewer panel. */
1457 /* TODO: currently, when one of panels is other than view_listing,
1458 current_panel points to view_listing panel all time independently of
1459 it's activity. Thus, we can't use get_current_type() here.
1460 current_panel should point to actualy current active panel
1461 independently of it's type. */
1462 if (current_panel->active == 0 && get_other_type () == view_quick)
1463 return MSG_NOT_HANDLED;
1465 for (i = 0; cmdline->buffer[i] != '\0' &&
1466 (cmdline->buffer[i] == ' ' || cmdline->buffer[i] == '\t'); i++)
1469 if (cmdline->buffer[i] != '\0')
1471 send_message (cmdline, NULL, MSG_KEY, parm, NULL);
1472 return MSG_HANDLED;
1475 input_insert (cmdline, "", FALSE);
1476 cmdline->point = 0;
1479 /* Ctrl-Enter and Alt-Enter */
1480 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n') && (parm & (KEY_M_CTRL | KEY_M_ALT)))
1482 put_prog_name ();
1483 return MSG_HANDLED;
1486 /* Ctrl-Shift-Enter */
1487 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n'))
1489 midnight_put_panel_path (current_panel);
1490 put_prog_name ();
1491 return MSG_HANDLED;
1494 if ((!mc_global.tty.alternate_plus_minus
1495 || !(mc_global.tty.console_flag != '\0' || mc_global.tty.xterm_flag)) && !quote
1496 && !current_panel->searching)
1498 if (!only_leading_plus_minus)
1500 /* Special treatement, since the input line will eat them */
1501 if (parm == '+')
1502 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Select, NULL);
1504 if (parm == '\\' || parm == '-')
1505 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Unselect,
1506 NULL);
1508 if (parm == '*')
1509 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_SelectInvert,
1510 NULL);
1512 else if (!command_prompt || cmdline->buffer[0] == '\0')
1514 /* Special treatement '+', '-', '\', '*' only when this is
1515 * first char on input line
1517 if (parm == '+')
1518 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Select, NULL);
1520 if (parm == '\\' || parm == '-')
1521 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Unselect,
1522 NULL);
1524 if (parm == '*')
1525 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_SelectInvert,
1526 NULL);
1529 return MSG_NOT_HANDLED;
1531 case MSG_HOTKEY_HANDLED:
1532 if ((get_current_type () == view_listing) && current_panel->searching)
1534 current_panel->dirty = 1; /* FIXME: unneeded? */
1535 send_message (current_panel, NULL, MSG_ACTION, CK_SearchStop, NULL);
1537 return MSG_HANDLED;
1539 case MSG_UNHANDLED_KEY:
1541 cb_ret_t v = MSG_NOT_HANDLED;
1543 if (ctl_x_map_enabled)
1545 ctl_x_map_enabled = FALSE;
1546 command = keybind_lookup_keymap_command (main_x_map, parm);
1548 else
1549 command = keybind_lookup_keymap_command (main_map, parm);
1551 if (command != CK_IgnoreKey)
1552 v = midnight_execute_cmd (NULL, command);
1554 if (v == MSG_NOT_HANDLED && command_prompt)
1555 v = send_message (cmdline, NULL, MSG_KEY, parm, NULL);
1557 return v;
1560 case MSG_POST_KEY:
1561 if (!the_menubar->is_active)
1562 update_dirty_panels ();
1563 return MSG_HANDLED;
1565 case MSG_ACTION:
1566 /* Handle shortcuts, menu, and buttonbar. */
1567 return midnight_execute_cmd (sender, parm);
1569 case MSG_END:
1570 panel_deinit ();
1571 return MSG_HANDLED;
1573 default:
1574 return dlg_default_callback (w, sender, msg, parm, data);
1578 /* --------------------------------------------------------------------------------------------- */
1579 /*** public functions ****************************************************************************/
1580 /* --------------------------------------------------------------------------------------------- */
1582 void
1583 update_menu (void)
1585 menu_set_name (left_menu, panels_layout.horizontal_split ? _("&Above") : _("&Left"));
1586 menu_set_name (right_menu, panels_layout.horizontal_split ? _("&Below") : _("&Right"));
1587 menubar_arrange (the_menubar);
1588 menubar_set_visible (the_menubar, menubar_visible);
1591 /* --------------------------------------------------------------------------------------------- */
1593 void
1594 midnight_set_buttonbar (WButtonBar * b)
1596 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), main_map, NULL);
1597 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Menu"), main_map, NULL);
1598 buttonbar_set_label (b, 3, Q_ ("ButtonBar|View"), main_map, NULL);
1599 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), main_map, NULL);
1600 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), main_map, NULL);
1601 buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), main_map, NULL);
1602 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), main_map, NULL);
1603 buttonbar_set_label (b, 8, Q_ ("ButtonBar|Delete"), main_map, NULL);
1604 buttonbar_set_label (b, 9, Q_ ("ButtonBar|PullDn"), main_map, NULL);
1605 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), main_map, NULL);
1608 /* --------------------------------------------------------------------------------------------- */
1610 * Return a random hint. If force is TRUE, ignore the timeout.
1613 char *
1614 get_random_hint (gboolean force)
1616 static const guint64 update_period = 60 * G_USEC_PER_SEC;
1617 static guint64 tv = 0;
1619 char *data, *result = NULL, *eop;
1620 size_t len, start;
1621 GIConv conv;
1623 /* Do not change hints more often than one minute */
1624 if (!force && !mc_time_elapsed (&tv, update_period))
1625 return g_strdup ("");
1627 data = load_mc_home_file (mc_global.share_data_dir, MC_HINT, NULL, &len);
1628 if (data == NULL)
1629 return NULL;
1631 /* get a random entry */
1632 srand ((unsigned int) (tv / G_USEC_PER_SEC));
1633 start = ((size_t) rand ()) % (len - 1);
1635 /* Search the start of paragraph */
1636 for (; start != 0; start--)
1637 if (data[start] == '\n' && data[start + 1] == '\n')
1639 start += 2;
1640 break;
1643 /* Search the end of paragraph */
1644 for (eop = data + start; *eop != '\0'; eop++)
1646 if (*eop == '\n' && *(eop + 1) == '\n')
1648 *eop = '\0';
1649 break;
1651 if (*eop == '\n')
1652 *eop = ' ';
1655 /* hint files are stored in utf-8 */
1656 /* try convert hint file from utf-8 to terminal encoding */
1657 conv = str_crt_conv_from ("UTF-8");
1658 if (conv != INVALID_CONV)
1660 GString *buffer;
1662 buffer = g_string_sized_new (len - start);
1663 if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE)
1664 result = g_string_free (buffer, FALSE);
1665 else
1666 g_string_free (buffer, TRUE);
1667 str_close_conv (conv);
1669 else
1670 result = g_strndup (data + start, len - start);
1672 g_free (data);
1673 return result;
1677 /* --------------------------------------------------------------------------------------------- */
1679 * Load new hint and display it.
1680 * IF force is not 0, ignore the timeout.
1683 void
1684 load_hint (gboolean force)
1686 char *hint;
1688 if (WIDGET (the_hint)->owner == NULL)
1689 return;
1691 if (!mc_global.message_visible)
1693 label_set_text (the_hint, NULL);
1694 return;
1697 hint = get_random_hint (force);
1699 if (hint != NULL)
1701 if (*hint != '\0')
1702 set_hintbar (hint);
1703 g_free (hint);
1705 else
1707 char text[BUF_SMALL];
1709 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"), VERSION);
1710 set_hintbar (text);
1714 /* --------------------------------------------------------------------------------------------- */
1716 void
1717 change_panel (void)
1719 input_free_completions (cmdline);
1720 dlg_one_down (midnight_dlg);
1723 /* --------------------------------------------------------------------------------------------- */
1725 /** Save current stat of directories to avoid reloading the panels
1726 * when no modifications have taken place
1728 void
1729 save_cwds_stat (void)
1731 if (panels_options.fast_reload)
1733 mc_stat (current_panel->cwd_vpath, &(current_panel->dir_stat));
1734 if (get_other_type () == view_listing)
1735 mc_stat (other_panel->cwd_vpath, &(other_panel->dir_stat));
1739 /* --------------------------------------------------------------------------------------------- */
1741 gboolean
1742 quiet_quit_cmd (void)
1744 print_last_revert = TRUE;
1745 return quit_cmd_internal (1);
1748 /* --------------------------------------------------------------------------------------------- */
1750 /** Run the main dialog that occupies the whole screen */
1751 gboolean
1752 do_nc (void)
1754 gboolean ret;
1756 #ifdef USE_INTERNAL_EDIT
1757 edit_stack_init ();
1758 #endif
1760 midnight_dlg = dlg_create (FALSE, 0, 0, 1, 1, WPOS_FULLSCREEN, FALSE, dialog_colors,
1761 midnight_callback, NULL, "[main]", NULL);
1763 /* Check if we were invoked as an editor or file viewer */
1764 if (mc_global.mc_run_mode != MC_RUN_FULL)
1766 setup_dummy_mc ();
1767 ret = mc_maybe_editor_or_viewer ();
1769 else
1771 /* We only need the first idle event to show user menu after start */
1772 widget_idle (WIDGET (midnight_dlg), TRUE);
1774 setup_mc ();
1775 mc_filehighlight = mc_fhl_new (TRUE);
1776 create_panels_and_run_mc ();
1777 mc_fhl_free (&mc_filehighlight);
1779 ret = TRUE;
1781 /* dlg_destroy destroys even current_panel->cwd_vpath, so we have to save a copy :) */
1782 if (mc_args__last_wd_file != NULL && vfs_current_is_local ())
1783 last_wd_string = g_strdup (vfs_path_as_str (current_panel->cwd_vpath));
1785 /* don't handle VFS timestamps for dirs opened in panels */
1786 mc_event_destroy (MCEVENT_GROUP_CORE, "vfs_timestamp");
1788 dir_list_clean (&panelized_panel.list);
1791 /* Program end */
1792 mc_global.midnight_shutdown = TRUE;
1793 dialog_switch_shutdown ();
1794 done_mc ();
1795 dlg_destroy (midnight_dlg);
1796 current_panel = NULL;
1798 #ifdef USE_INTERNAL_EDIT
1799 edit_stack_free ();
1800 #endif
1802 if ((quit & SUBSHELL_EXIT) == 0)
1803 clr_scr ();
1805 return ret;
1808 /* --------------------------------------------------------------------------------------------- */