Move widget add/del API from WDialog to WGroup.
[midnight-commander.git] / src / filemanager / midnight.c
blob66fe3ff2bc3f923f902443d44564535af3ab2bac
1 /*
2 Main dialog (file panels) of the Midnight Commander
4 Copyright (C) 1994-2020
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/execute.h" /* toggle_subshell */
62 #include "src/setup.h" /* variables */
63 #include "src/learn.h" /* learn_keys() */
64 #include "src/keybind-defaults.h"
65 #include "lib/fileloc.h" /* MC_FILEPOS_FILE */
66 #include "lib/keybind.h"
67 #include "lib/event.h"
69 #include "tree.h"
70 #include "boxes.h" /* sort_box(), tree_box() */
71 #include "layout.h"
72 #include "cmd.h" /* commands */
73 #include "hotlist.h"
74 #include "panelize.h"
75 #include "command.h" /* cmdline */
76 #include "dir.h" /* dir_list_clean() */
78 #include "chmod.h"
79 #include "chown.h"
80 #include "achown.h"
82 #ifdef USE_INTERNAL_EDIT
83 #include "src/editor/edit.h"
84 #endif
86 #ifdef USE_DIFF_VIEW
87 #include "src/diffviewer/ydiff.h"
88 #endif
90 #include "src/consaver/cons.saver.h" /* show_console_contents */
91 #include "src/file_history.h" /* show_file_history() */
93 #include "midnight.h"
95 /*** global variables ****************************************************************************/
97 /* When the modes are active, left_panel, right_panel and tree_panel */
98 /* point to a proper data structure. You should check with the functions */
99 /* get_current_type and get_other_type the types of the panels before using */
100 /* this pointer variables */
102 /* The structures for the panels */
103 WPanel *left_panel = NULL;
104 WPanel *right_panel = NULL;
105 /* Pointer to the selected and unselected panel */
106 WPanel *current_panel = NULL;
108 /* The Menubar */
109 WMenuBar *the_menubar = NULL;
110 /* The widget where we draw the prompt */
111 WLabel *the_prompt;
112 /* The hint bar */
113 WLabel *the_hint;
114 /* The button bar */
115 WButtonBar *the_bar;
117 /* The prompt */
118 const char *mc_prompt = NULL;
120 /*** file scope macro definitions ****************************************************************/
122 #ifdef HAVE_CHARSET
124 * Don't restrict the output on the screen manager level,
125 * the translation tables take care of it.
127 #endif /* !HAVE_CHARSET */
129 /*** file scope type declarations ****************************************************************/
131 /*** file scope variables ************************************************************************/
133 static menu_t *left_menu, *right_menu;
135 static gboolean ctl_x_map_enabled = FALSE;
137 /*** file scope functions ************************************************************************/
139 /** Stop MC main dialog and the current dialog if it exists.
140 * Needed to provide fast exit from MC viewer or editor on shell exit */
141 static void
142 stop_dialogs (void)
144 dlg_stop (midnight_dlg);
146 if (top_dlg != NULL)
147 dlg_stop (DIALOG (top_dlg->data));
150 /* --------------------------------------------------------------------------------------------- */
152 static void
153 treebox_cmd (void)
155 char *sel_dir;
157 sel_dir = tree_box (selection (current_panel)->fname);
158 if (sel_dir != NULL)
160 vfs_path_t *sel_vdir;
162 sel_vdir = vfs_path_from_str (sel_dir);
163 do_cd (sel_vdir, cd_exact);
164 vfs_path_free (sel_vdir);
165 g_free (sel_dir);
169 /* --------------------------------------------------------------------------------------------- */
171 #ifdef LISTMODE_EDITOR
172 static void
173 listmode_cmd (void)
175 char *newmode;
177 if (get_current_type () != view_listing)
178 return;
180 newmode = listmode_edit (current_panel->user_format);
181 if (!newmode)
182 return;
184 g_free (current_panel->user_format);
185 current_panel->list_format = list_user;
186 current_panel->user_format = newmode;
187 set_panel_formats (current_panel);
189 do_refresh ();
191 #endif /* LISTMODE_EDITOR */
193 /* --------------------------------------------------------------------------------------------- */
195 static GList *
196 create_panel_menu (void)
198 GList *entries = NULL;
200 entries = g_list_prepend (entries, menu_entry_create (_("File listin&g"), CK_PanelListing));
201 entries = g_list_prepend (entries, menu_entry_create (_("&Quick view"), CK_PanelQuickView));
202 entries = g_list_prepend (entries, menu_entry_create (_("&Info"), CK_PanelInfo));
203 entries = g_list_prepend (entries, menu_entry_create (_("&Tree"), CK_PanelTree));
204 entries = g_list_prepend (entries, menu_separator_create ());
205 entries =
206 g_list_prepend (entries,
207 menu_entry_create (_("&Listing format..."), CK_SetupListingFormat));
208 entries = g_list_prepend (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
209 entries = g_list_prepend (entries, menu_entry_create (_("&Filter..."), CK_Filter));
210 #ifdef HAVE_CHARSET
211 entries = g_list_prepend (entries, menu_entry_create (_("&Encoding..."), CK_SelectCodepage));
212 #endif
213 entries = g_list_prepend (entries, menu_separator_create ());
214 #ifdef ENABLE_VFS_FTP
215 entries = g_list_prepend (entries, menu_entry_create (_("FT&P link..."), CK_ConnectFtp));
216 #endif
217 #ifdef ENABLE_VFS_FISH
218 entries = g_list_prepend (entries, menu_entry_create (_("S&hell link..."), CK_ConnectFish));
219 #endif
220 #ifdef ENABLE_VFS_SFTP
221 entries = g_list_prepend (entries, menu_entry_create (_("S&FTP link..."), CK_ConnectSftp));
222 #endif
223 #ifdef ENABLE_VFS_SMB
224 entries = g_list_prepend (entries, menu_entry_create (_("SM&B link..."), CK_ConnectSmb));
225 #endif
226 entries = g_list_prepend (entries, menu_entry_create (_("Paneli&ze"), CK_Panelize));
227 entries = g_list_prepend (entries, menu_separator_create ());
228 entries = g_list_prepend (entries, menu_entry_create (_("&Rescan"), CK_Reread));
230 return g_list_reverse (entries);
233 /* --------------------------------------------------------------------------------------------- */
235 static GList *
236 create_file_menu (void)
238 GList *entries = NULL;
240 entries = g_list_prepend (entries, menu_entry_create (_("&View"), CK_View));
241 entries = g_list_prepend (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFile));
242 entries = g_list_prepend (entries, menu_entry_create (_("&Filtered view"), CK_ViewFiltered));
243 entries = g_list_prepend (entries, menu_entry_create (_("&Edit"), CK_Edit));
244 entries = g_list_prepend (entries, menu_entry_create (_("&Copy"), CK_Copy));
245 entries = g_list_prepend (entries, menu_entry_create (_("C&hmod"), CK_ChangeMode));
246 entries = g_list_prepend (entries, menu_entry_create (_("&Link"), CK_Link));
247 entries = g_list_prepend (entries, menu_entry_create (_("&Symlink"), CK_LinkSymbolic));
248 entries =
249 g_list_prepend (entries,
250 menu_entry_create (_("Relative symlin&k"), CK_LinkSymbolicRelative));
251 entries = g_list_prepend (entries, menu_entry_create (_("Edit s&ymlink"), CK_LinkSymbolicEdit));
252 entries = g_list_prepend (entries, menu_entry_create (_("Ch&own"), CK_ChangeOwn));
253 entries =
254 g_list_prepend (entries, menu_entry_create (_("&Advanced chown"), CK_ChangeOwnAdvanced));
255 entries = g_list_prepend (entries, menu_entry_create (_("&Rename/Move"), CK_Move));
256 entries = g_list_prepend (entries, menu_entry_create (_("&Mkdir"), CK_MakeDir));
257 entries = g_list_prepend (entries, menu_entry_create (_("&Delete"), CK_Delete));
258 entries = g_list_prepend (entries, menu_entry_create (_("&Quick cd"), CK_CdQuick));
259 entries = g_list_prepend (entries, menu_separator_create ());
260 entries = g_list_prepend (entries, menu_entry_create (_("Select &group"), CK_Select));
261 entries = g_list_prepend (entries, menu_entry_create (_("U&nselect group"), CK_Unselect));
262 entries = g_list_prepend (entries, menu_entry_create (_("&Invert selection"), CK_SelectInvert));
263 entries = g_list_prepend (entries, menu_separator_create ());
264 entries = g_list_prepend (entries, menu_entry_create (_("E&xit"), CK_Quit));
266 return g_list_reverse (entries);
269 /* --------------------------------------------------------------------------------------------- */
271 static GList *
272 create_command_menu (void)
274 /* I know, I'm lazy, but the tree widget when it's not running
275 * as a panel still has some problems, I have not yet finished
276 * the WTree widget port, sorry.
278 GList *entries = NULL;
280 entries = g_list_prepend (entries, menu_entry_create (_("&User menu"), CK_UserMenu));
281 entries = g_list_prepend (entries, menu_entry_create (_("&Directory tree"), CK_Tree));
282 entries = g_list_prepend (entries, menu_entry_create (_("&Find file"), CK_Find));
283 entries = g_list_prepend (entries, menu_entry_create (_("S&wap panels"), CK_Swap));
284 entries = g_list_prepend (entries, menu_entry_create (_("Switch &panels on/off"), CK_Shell));
285 entries =
286 g_list_prepend (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirs));
287 #ifdef USE_DIFF_VIEW
288 entries = g_list_prepend (entries, menu_entry_create (_("C&ompare files"), CK_CompareFiles));
289 #endif
290 entries =
291 g_list_prepend (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
292 entries = g_list_prepend (entries, menu_entry_create (_("Show directory s&izes"), CK_DirSize));
293 entries = g_list_prepend (entries, menu_separator_create ());
294 entries = g_list_prepend (entries, menu_entry_create (_("Command &history"), CK_History));
295 entries =
296 g_list_prepend (entries,
297 menu_entry_create (_("Viewed/edited files hi&story"),
298 CK_EditorViewerHistory));
299 entries = g_list_prepend (entries, menu_entry_create (_("Di&rectory hotlist"), CK_HotList));
300 #ifdef ENABLE_VFS
301 entries = g_list_prepend (entries, menu_entry_create (_("&Active VFS list"), CK_VfsList));
302 #endif
303 #ifdef ENABLE_BACKGROUND
304 entries = g_list_prepend (entries, menu_entry_create (_("&Background jobs"), CK_Jobs));
305 #endif
306 entries = g_list_prepend (entries, menu_entry_create (_("Screen lis&t"), CK_ScreenList));
307 entries = g_list_prepend (entries, menu_separator_create ());
308 #ifdef ENABLE_VFS_UNDELFS
309 entries =
310 g_list_prepend (entries,
311 menu_entry_create (_("&Undelete files (ext2fs only)"), CK_Undelete));
312 #endif
313 #ifdef LISTMODE_EDITOR
314 entries = g_list_prepend (entries, menu_entry_create (_("&Listing format edit"), CK_ListMode));
315 #endif
316 #if defined (ENABLE_VFS_UNDELFS) || defined (LISTMODE_EDITOR)
317 entries = g_list_prepend (entries, menu_separator_create ());
318 #endif
319 entries =
320 g_list_prepend (entries,
321 menu_entry_create (_("Edit &extension file"), CK_EditExtensionsFile));
322 entries = g_list_prepend (entries, menu_entry_create (_("Edit &menu file"), CK_EditUserMenu));
323 entries =
324 g_list_prepend (entries,
325 menu_entry_create (_("Edit hi&ghlighting group file"),
326 CK_EditFileHighlightFile));
328 return g_list_reverse (entries);
331 /* --------------------------------------------------------------------------------------------- */
333 static GList *
334 create_options_menu (void)
336 GList *entries = NULL;
338 entries = g_list_prepend (entries, menu_entry_create (_("&Configuration..."), CK_Options));
339 entries = g_list_prepend (entries, menu_entry_create (_("&Layout..."), CK_OptionsLayout));
340 entries = g_list_prepend (entries, menu_entry_create (_("&Panel options..."), CK_OptionsPanel));
341 entries =
342 g_list_prepend (entries, menu_entry_create (_("C&onfirmation..."), CK_OptionsConfirm));
343 entries =
344 g_list_prepend (entries, menu_entry_create (_("&Appearance..."), CK_OptionsAppearance));
345 entries =
346 g_list_prepend (entries, menu_entry_create (_("&Display bits..."), CK_OptionsDisplayBits));
347 entries = g_list_prepend (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
348 #ifdef ENABLE_VFS
349 entries = g_list_prepend (entries, menu_entry_create (_("&Virtual FS..."), CK_OptionsVfs));
350 #endif
351 entries = g_list_prepend (entries, menu_separator_create ());
352 entries = g_list_prepend (entries, menu_entry_create (_("&Save setup"), CK_SaveSetup));
354 return g_list_reverse (entries);
357 /* --------------------------------------------------------------------------------------------- */
359 static void
360 init_menu (void)
362 left_menu = create_menu ("", create_panel_menu (), "[Left and Right Menus]");
363 menubar_add_menu (the_menubar, left_menu);
364 menubar_add_menu (the_menubar, create_menu (_("&File"), create_file_menu (), "[File Menu]"));
365 menubar_add_menu (the_menubar,
366 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
367 menubar_add_menu (the_menubar,
368 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
369 right_menu = create_menu ("", create_panel_menu (), "[Left and Right Menus]");
370 menubar_add_menu (the_menubar, right_menu);
371 update_menu ();
374 /* --------------------------------------------------------------------------------------------- */
376 static void
377 menu_last_selected_cmd (void)
379 menubar_activate (the_menubar, drop_menus, -1);
382 /* --------------------------------------------------------------------------------------------- */
384 static void
385 menu_cmd (void)
387 int selected;
389 if ((get_current_index () == 0) == (current_panel->active != 0))
390 selected = 0;
391 else
392 selected = g_list_length (the_menubar->menu) - 1;
394 menubar_activate (the_menubar, drop_menus, selected);
397 /* --------------------------------------------------------------------------------------------- */
399 static void
400 sort_cmd (void)
402 WPanel *p;
403 const panel_field_t *sort_order;
405 if (!SELECTED_IS_PANEL)
406 return;
408 p = MENU_PANEL;
409 sort_order = sort_box (&p->sort_info, p->sort_field);
410 panel_set_sort_order (p, sort_order);
413 /* --------------------------------------------------------------------------------------------- */
415 static char *
416 midnight_get_shortcut (long command)
418 const char *ext_map;
419 const char *shortcut = NULL;
421 shortcut = keybind_lookup_keymap_shortcut (main_map, command);
422 if (shortcut != NULL)
423 return g_strdup (shortcut);
425 shortcut = keybind_lookup_keymap_shortcut (panel_map, command);
426 if (shortcut != NULL)
427 return g_strdup (shortcut);
429 ext_map = keybind_lookup_keymap_shortcut (main_map, CK_ExtendedKeyMap);
430 if (ext_map != NULL)
431 shortcut = keybind_lookup_keymap_shortcut (main_x_map, command);
432 if (shortcut != NULL)
433 return g_strdup_printf ("%s %s", ext_map, shortcut);
435 return NULL;
438 /* --------------------------------------------------------------------------------------------- */
440 static char *
441 midnight_get_title (const WDialog * h, size_t len)
443 char *path;
444 char *login;
445 char *p;
447 (void) h;
449 title_path_prepare (&path, &login);
451 p = g_strdup_printf ("%s [%s]:%s", _("Panels:"), login, path);
452 g_free (path);
453 g_free (login);
454 path = g_strdup (str_trunc (p, len - 4));
455 g_free (p);
457 return path;
460 /* --------------------------------------------------------------------------------------------- */
462 static void
463 toggle_panels_split (void)
465 panels_layout.horizontal_split = !panels_layout.horizontal_split;
466 layout_change ();
467 do_refresh ();
470 /* --------------------------------------------------------------------------------------------- */
472 #ifdef ENABLE_VFS
473 /* event helper */
474 static gboolean
475 check_panel_timestamp (const WPanel * panel, panel_view_mode_t mode, struct vfs_class *vclass,
476 vfsid id)
478 if (mode == view_listing)
480 const vfs_path_element_t *path_element;
482 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
484 if (path_element->class != vclass)
485 return FALSE;
487 if (vfs_getid (panel->cwd_vpath) != id)
488 return FALSE;
490 return TRUE;
493 /* --------------------------------------------------------------------------------------------- */
495 /* event callback */
496 static gboolean
497 check_current_panel_timestamp (const gchar * event_group_name, const gchar * event_name,
498 gpointer init_data, gpointer data)
500 ev_vfs_stamp_create_t *event_data = (ev_vfs_stamp_create_t *) data;
502 (void) event_group_name;
503 (void) event_name;
504 (void) init_data;
506 event_data->ret =
507 check_panel_timestamp (current_panel, get_current_type (), event_data->vclass,
508 event_data->id);
509 return !event_data->ret;
512 /* --------------------------------------------------------------------------------------------- */
514 /* event callback */
515 static gboolean
516 check_other_panel_timestamp (const gchar * event_group_name, const gchar * event_name,
517 gpointer init_data, gpointer data)
519 ev_vfs_stamp_create_t *event_data = (ev_vfs_stamp_create_t *) data;
521 (void) event_group_name;
522 (void) event_name;
523 (void) init_data;
525 event_data->ret =
526 check_panel_timestamp (other_panel, get_other_type (), event_data->vclass, event_data->id);
527 return !event_data->ret;
529 #endif /* ENABLE_VFS */
531 /* --------------------------------------------------------------------------------------------- */
533 /* event callback */
534 static gboolean
535 print_vfs_message (const gchar * event_group_name, const gchar * event_name,
536 gpointer init_data, gpointer data)
538 ev_vfs_print_message_t *event_data = (ev_vfs_print_message_t *) data;
540 (void) event_group_name;
541 (void) event_name;
542 (void) init_data;
544 if (mc_global.midnight_shutdown)
545 goto ret;
547 if (!mc_global.message_visible || the_hint == NULL || WIDGET (the_hint)->owner == NULL)
549 int col, row;
551 if (!nice_rotating_dash || (ok_to_refresh <= 0))
552 goto ret;
554 /* Preserve current cursor position */
555 tty_getyx (&row, &col);
557 tty_gotoyx (0, 0);
558 tty_setcolor (NORMAL_COLOR);
559 tty_print_string (str_fit_to_term (event_data->msg, COLS - 1, J_LEFT));
561 /* Restore cursor position */
562 tty_gotoyx (row, col);
563 mc_refresh ();
564 goto ret;
567 if (mc_global.message_visible)
568 set_hintbar (event_data->msg);
570 ret:
571 MC_PTR_FREE (event_data->msg);
572 return TRUE;
575 /* --------------------------------------------------------------------------------------------- */
577 static void
578 create_panels (void)
580 int current_index, other_index;
581 panel_view_mode_t current_mode, other_mode;
582 char *current_dir, *other_dir;
583 vfs_path_t *original_dir;
586 * Following cases from command line are possible:
587 * 'mc' (no arguments): mc_run_param0 == NULL, mc_run_param1 == NULL
588 * active panel uses current directory
589 * passive panel uses "other_dir" from panels.ini
591 * 'mc dir1 dir2' (two arguments): mc_run_param0 != NULL, mc_run_param1 != NULL
592 * active panel uses mc_run_param0
593 * passive panel uses mc_run_param1
595 * 'mc dir1' (single argument): mc_run_param0 != NULL, mc_run_param1 == NULL
596 * active panel uses mc_run_param0
597 * passive panel uses "other_dir" from panels.ini
600 /* Set up panel directories */
601 if (boot_current_is_left)
603 /* left panel is active */
604 current_index = 0;
605 other_index = 1;
606 current_mode = startup_left_mode;
607 other_mode = startup_right_mode;
609 if (mc_run_param0 == NULL && mc_run_param1 == NULL)
611 /* no arguments */
612 current_dir = NULL; /* assume current dir */
613 other_dir = saved_other_dir; /* from ini */
615 else if (mc_run_param0 != NULL && mc_run_param1 != NULL)
617 /* two arguments */
618 current_dir = (char *) mc_run_param0;
619 other_dir = mc_run_param1;
621 else /* mc_run_param0 != NULL && mc_run_param1 == NULL */
623 /* one argument */
624 current_dir = (char *) mc_run_param0;
625 other_dir = saved_other_dir; /* from ini */
628 else
630 /* right panel is active */
631 current_index = 1;
632 other_index = 0;
633 current_mode = startup_right_mode;
634 other_mode = startup_left_mode;
636 if (mc_run_param0 == NULL && mc_run_param1 == NULL)
638 /* no arguments */
639 current_dir = NULL; /* assume current dir */
640 other_dir = saved_other_dir; /* from ini */
642 else if (mc_run_param0 != NULL && mc_run_param1 != NULL)
644 /* two arguments */
645 current_dir = (char *) mc_run_param0;
646 other_dir = mc_run_param1;
648 else /* mc_run_param0 != NULL && mc_run_param1 == NULL */
650 /* one argument */
651 current_dir = (char *) mc_run_param0;
652 other_dir = saved_other_dir; /* from ini */
656 /* 1. Get current dir */
657 original_dir = vfs_path_clone (vfs_get_raw_current_dir ());
659 /* 2. Create passive panel */
660 if (other_dir != NULL)
662 vfs_path_t *vpath;
664 if (g_path_is_absolute (other_dir))
665 vpath = vfs_path_from_str (other_dir);
666 else
667 vpath = vfs_path_append_new (original_dir, other_dir, (char *) NULL);
668 mc_chdir (vpath);
669 vfs_path_free (vpath);
671 create_panel (other_index, other_mode);
673 /* 3. Create active panel */
674 if (current_dir == NULL)
675 mc_chdir (original_dir);
676 else
678 vfs_path_t *vpath;
680 if (g_path_is_absolute (current_dir))
681 vpath = vfs_path_from_str (current_dir);
682 else
683 vpath = vfs_path_append_new (original_dir, current_dir, (char *) NULL);
684 mc_chdir (vpath);
685 vfs_path_free (vpath);
687 create_panel (current_index, current_mode);
689 if (startup_left_mode == view_listing)
690 current_panel = left_panel;
691 else if (right_panel != NULL)
692 current_panel = right_panel;
693 else
694 current_panel = left_panel;
696 vfs_path_free (original_dir);
698 #ifdef ENABLE_VFS
699 mc_event_add (MCEVENT_GROUP_CORE, "vfs_timestamp", check_other_panel_timestamp, NULL, NULL);
700 mc_event_add (MCEVENT_GROUP_CORE, "vfs_timestamp", check_current_panel_timestamp, NULL, NULL);
701 #endif /* ENABLE_VFS */
703 mc_event_add (MCEVENT_GROUP_CORE, "vfs_print_message", print_vfs_message, NULL, NULL);
706 /* --------------------------------------------------------------------------------------------- */
708 static void
709 midnight_put_panel_path (WPanel * panel)
711 vfs_path_t *cwd_vpath;
712 const char *cwd_vpath_str;
714 if (!command_prompt)
715 return;
717 #ifdef HAVE_CHARSET
718 cwd_vpath = remove_encoding_from_path (panel->cwd_vpath);
719 #else
720 cwd_vpath = vfs_path_clone (panel->cwd_vpath);
721 #endif
723 cwd_vpath_str = vfs_path_as_str (cwd_vpath);
725 command_insert (cmdline, cwd_vpath_str, FALSE);
727 if (!IS_PATH_SEP (cwd_vpath_str[strlen (cwd_vpath_str) - 1]))
728 command_insert (cmdline, PATH_SEP_STR, FALSE);
730 vfs_path_free (cwd_vpath);
733 /* --------------------------------------------------------------------------------------------- */
735 static void
736 put_link (WPanel * panel)
738 if (!command_prompt)
739 return;
740 if (S_ISLNK (selection (panel)->st.st_mode))
742 char buffer[MC_MAXPATHLEN];
743 vfs_path_t *vpath;
744 int i;
746 vpath = vfs_path_append_new (panel->cwd_vpath, selection (panel)->fname, (char *) NULL);
747 i = mc_readlink (vpath, buffer, sizeof (buffer) - 1);
748 vfs_path_free (vpath);
750 if (i > 0)
752 buffer[i] = '\0';
753 command_insert (cmdline, buffer, TRUE);
758 /* --------------------------------------------------------------------------------------------- */
760 static void
761 put_current_link (void)
763 put_link (current_panel);
766 /* --------------------------------------------------------------------------------------------- */
768 static void
769 put_other_link (void)
771 if (get_other_type () == view_listing)
772 put_link (other_panel);
775 /* --------------------------------------------------------------------------------------------- */
777 /** Insert the selected file name into the input line */
778 static void
779 put_current_selected (void)
781 const char *tmp;
783 if (!command_prompt)
784 return;
786 if (get_current_type () == view_tree)
788 WTree *tree;
789 const vfs_path_t *selected_name;
791 tree = (WTree *) get_panel_widget (get_current_index ());
792 selected_name = tree_selected_name (tree);
793 tmp = vfs_path_as_str (selected_name);
795 else
796 tmp = selection (current_panel)->fname;
798 command_insert (cmdline, tmp, TRUE);
801 /* --------------------------------------------------------------------------------------------- */
803 static void
804 put_tagged (WPanel * panel)
806 if (!command_prompt)
807 return;
808 input_disable_update (cmdline);
809 if (panel->marked)
811 int i;
813 for (i = 0; i < panel->dir.len; i++)
815 if (panel->dir.list[i].f.marked)
816 command_insert (cmdline, panel->dir.list[i].fname, TRUE);
819 else
821 command_insert (cmdline, panel->dir.list[panel->selected].fname, TRUE);
823 input_enable_update (cmdline);
826 /* --------------------------------------------------------------------------------------------- */
828 static void
829 put_current_tagged (void)
831 put_tagged (current_panel);
834 /* --------------------------------------------------------------------------------------------- */
836 static void
837 put_other_tagged (void)
839 if (get_other_type () == view_listing)
840 put_tagged (other_panel);
843 /* --------------------------------------------------------------------------------------------- */
845 static void
846 ctl_x_cmd (void)
848 ctl_x_map_enabled = TRUE;
851 /* --------------------------------------------------------------------------------------------- */
853 static void
854 setup_mc (void)
856 #ifdef HAVE_SLANG
857 #ifdef HAVE_CHARSET
858 tty_display_8bit (TRUE);
859 #else
860 tty_display_8bit (mc_global.full_eight_bits);
861 #endif /* HAVE_CHARSET */
863 #else /* HAVE_SLANG */
865 #ifdef HAVE_CHARSET
866 tty_display_8bit (TRUE);
867 #else
868 tty_display_8bit (mc_global.eight_bit_clean);
869 #endif /* HAVE_CHARSET */
870 #endif /* HAVE_SLANG */
872 #ifdef ENABLE_SUBSHELL
873 if (mc_global.tty.use_subshell)
874 add_select_channel (mc_global.tty.subshell_pty, load_prompt, NULL);
875 #endif /* !ENABLE_SUBSHELL */
877 if ((tty_baudrate () < 9600) || mc_global.tty.slow_terminal)
878 verbose = FALSE;
881 /* --------------------------------------------------------------------------------------------- */
883 static void
884 setup_dummy_mc (void)
886 vfs_path_t *vpath;
887 char *d;
888 int ret;
890 d = _vfs_get_cwd ();
891 setup_mc ();
892 vpath = vfs_path_from_str (d);
893 ret = mc_chdir (vpath);
894 (void) ret;
895 vfs_path_free (vpath);
896 g_free (d);
899 /* --------------------------------------------------------------------------------------------- */
901 static void
902 done_mc (void)
904 /* Setup shutdown
906 * We sync the profiles since the hotlist may have changed, while
907 * we only change the setup data if we have the auto save feature set
910 save_setup (auto_save_setup, panels_options.auto_save_setup);
912 vfs_stamp_path (vfs_get_raw_current_dir ());
915 /* --------------------------------------------------------------------------------------------- */
917 static void
918 create_file_manager (void)
920 WGroup *g = GROUP (midnight_dlg);
922 midnight_dlg->get_shortcut = midnight_get_shortcut;
923 midnight_dlg->get_title = midnight_get_title;
924 /* allow rebind tab */
925 widget_want_tab (WIDGET (midnight_dlg), TRUE);
927 the_menubar = menubar_new (NULL, menubar_visible);
928 group_add_widget (g, the_menubar);
929 init_menu ();
931 create_panels ();
932 group_add_widget (g, get_panel_widget (0));
933 group_add_widget (g, get_panel_widget (1));
935 the_hint = label_new (0, 0, 0);
936 the_hint->transparent = TRUE;
937 the_hint->auto_adjust_cols = 0;
938 WIDGET (the_hint)->cols = COLS;
939 group_add_widget (g, the_hint);
941 cmdline = command_new (0, 0, 0);
942 group_add_widget (g, cmdline);
944 the_prompt = label_new (0, 0, mc_prompt);
945 the_prompt->transparent = TRUE;
946 group_add_widget (g, the_prompt);
948 the_bar = buttonbar_new (mc_global.keybar_visible);
949 group_add_widget (g, the_bar);
950 midnight_set_buttonbar (the_bar);
953 /* --------------------------------------------------------------------------------------------- */
955 /** result must be free'd (I think this should go in util.c) */
956 static vfs_path_t *
957 prepend_cwd_on_local (const char *filename)
959 vfs_path_t *vpath;
961 vpath = vfs_path_from_str (filename);
962 if (!vfs_file_is_local (vpath) || g_path_is_absolute (filename))
963 return vpath;
965 vfs_path_free (vpath);
967 return vfs_path_append_new (vfs_get_raw_current_dir (), filename, (char *) NULL);
970 /* --------------------------------------------------------------------------------------------- */
972 /** Invoke the internal view/edit routine with:
973 * the default processing and forcing the internal viewer/editor
975 static gboolean
976 mc_maybe_editor_or_viewer (void)
978 gboolean ret;
980 switch (mc_global.mc_run_mode)
982 #ifdef USE_INTERNAL_EDIT
983 case MC_RUN_EDITOR:
984 ret = edit_files ((GList *) mc_run_param0);
985 break;
986 #endif /* USE_INTERNAL_EDIT */
987 case MC_RUN_VIEWER:
989 vfs_path_t *vpath = NULL;
991 if (mc_run_param0 != NULL && *(char *) mc_run_param0 != '\0')
992 vpath = prepend_cwd_on_local ((char *) mc_run_param0);
994 ret = view_file (vpath, FALSE, TRUE);
995 vfs_path_free (vpath);
996 break;
998 #ifdef USE_DIFF_VIEW
999 case MC_RUN_DIFFVIEWER:
1000 ret = dview_diff_cmd (mc_run_param0, mc_run_param1);
1001 break;
1002 #endif /* USE_DIFF_VIEW */
1003 default:
1004 ret = FALSE;
1007 return ret;
1010 /* --------------------------------------------------------------------------------------------- */
1012 static void
1013 show_editor_viewer_history (void)
1015 char *s;
1016 int act;
1018 s = show_file_history (WIDGET (midnight_dlg), &act);
1019 if (s != NULL)
1021 vfs_path_t *s_vpath;
1023 switch (act)
1025 case CK_Edit:
1026 s_vpath = vfs_path_from_str (s);
1027 edit_file_at_line (s_vpath, use_internal_edit, 0);
1028 break;
1030 case CK_View:
1031 s_vpath = vfs_path_from_str (s);
1032 view_file (s_vpath, use_internal_view, 0);
1033 break;
1035 default:
1037 char *d;
1039 d = g_path_get_dirname (s);
1040 s_vpath = vfs_path_from_str (d);
1041 do_cd (s_vpath, cd_exact);
1042 try_to_select (current_panel, s);
1043 g_free (d);
1047 g_free (s);
1048 vfs_path_free (s_vpath);
1052 /* --------------------------------------------------------------------------------------------- */
1054 static gboolean
1055 quit_cmd_internal (int quiet)
1057 int q = quit;
1058 size_t n;
1060 n = dialog_switch_num () - 1;
1061 if (n != 0)
1063 char msg[BUF_MEDIUM];
1065 g_snprintf (msg, sizeof (msg),
1066 ngettext ("You have %zu opened screen. Quit anyway?",
1067 "You have %zu opened screens. Quit anyway?", n), n);
1069 if (query_dialog (_("The Midnight Commander"), msg, D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
1070 return FALSE;
1071 q = 1;
1073 else if (quiet || !confirm_exit)
1074 q = 1;
1075 else if (query_dialog (_("The Midnight Commander"),
1076 _("Do you really want to quit the Midnight Commander?"),
1077 D_NORMAL, 2, _("&Yes"), _("&No")) == 0)
1078 q = 1;
1080 if (q != 0)
1082 #ifdef ENABLE_SUBSHELL
1083 if (!mc_global.tty.use_subshell)
1084 stop_dialogs ();
1085 else if ((q = exit_subshell ()? 1 : 0) != 0)
1086 #endif
1087 stop_dialogs ();
1090 if (q != 0)
1091 quit |= 1;
1092 return (quit != 0);
1095 /* --------------------------------------------------------------------------------------------- */
1097 static gboolean
1098 quit_cmd (void)
1100 return quit_cmd_internal (0);
1103 /* --------------------------------------------------------------------------------------------- */
1106 * Repaint the contents of the panels without frames. To schedule panel
1107 * for repainting, set panel->dirty to 1. There are many reasons why
1108 * the panels need to be repainted, and this is a costly operation, so
1109 * it's done once per event.
1112 static void
1113 update_dirty_panels (void)
1115 if (get_current_type () == view_listing && current_panel->dirty)
1116 widget_draw (WIDGET (current_panel));
1118 if (get_other_type () == view_listing && other_panel->dirty)
1119 widget_draw (WIDGET (other_panel));
1122 /* --------------------------------------------------------------------------------------------- */
1124 static void
1125 toggle_show_hidden (void)
1127 panels_options.show_dot_files = !panels_options.show_dot_files;
1128 update_panels (UP_RELOAD, UP_KEEPSEL);
1129 /* redraw panels forced */
1130 update_dirty_panels ();
1133 /* --------------------------------------------------------------------------------------------- */
1135 static cb_ret_t
1136 midnight_execute_cmd (Widget * sender, long command)
1138 cb_ret_t res = MSG_HANDLED;
1140 (void) sender;
1142 /* stop quick search before executing any command */
1143 send_message (current_panel, NULL, MSG_ACTION, CK_SearchStop, NULL);
1145 switch (command)
1147 case CK_ChangePanel:
1148 change_panel ();
1149 break;
1150 case CK_HotListAdd:
1151 add2hotlist_cmd ();
1152 break;
1153 case CK_SetupListingFormat:
1154 setup_listing_format_cmd ();
1155 break;
1156 case CK_ChangeMode:
1157 chmod_cmd ();
1158 break;
1159 case CK_ChangeOwn:
1160 chown_cmd ();
1161 break;
1162 case CK_ChangeOwnAdvanced:
1163 advanced_chown_cmd ();
1164 break;
1165 case CK_CompareDirs:
1166 compare_dirs_cmd ();
1167 break;
1168 case CK_Options:
1169 configure_box ();
1170 break;
1171 #ifdef ENABLE_VFS
1172 case CK_OptionsVfs:
1173 configure_vfs_box ();
1174 break;
1175 #endif
1176 case CK_OptionsConfirm:
1177 confirm_box ();
1178 break;
1179 case CK_Copy:
1180 copy_cmd ();
1181 break;
1182 case CK_PutCurrentPath:
1183 midnight_put_panel_path (current_panel);
1184 break;
1185 case CK_PutCurrentSelected:
1186 put_current_selected ();
1187 break;
1188 case CK_PutCurrentFullSelected:
1189 midnight_put_panel_path (current_panel);
1190 put_current_selected ();
1191 break;
1192 case CK_PutCurrentLink:
1193 put_current_link ();
1194 break;
1195 case CK_PutCurrentTagged:
1196 put_current_tagged ();
1197 break;
1198 case CK_PutOtherPath:
1199 if (get_other_type () == view_listing)
1200 midnight_put_panel_path (other_panel);
1201 break;
1202 case CK_PutOtherLink:
1203 put_other_link ();
1204 break;
1205 case CK_PutOtherTagged:
1206 put_other_tagged ();
1207 break;
1208 case CK_Delete:
1209 delete_cmd ();
1210 break;
1211 case CK_ScreenList:
1212 dialog_switch_list ();
1213 break;
1214 #ifdef USE_DIFF_VIEW
1215 case CK_CompareFiles:
1216 diff_view_cmd ();
1217 break;
1218 #endif
1219 case CK_OptionsDisplayBits:
1220 display_bits_box ();
1221 break;
1222 case CK_Edit:
1223 edit_cmd ();
1224 break;
1225 #ifdef USE_INTERNAL_EDIT
1226 case CK_EditForceInternal:
1227 edit_cmd_force_internal ();
1228 break;
1229 #endif
1230 case CK_EditExtensionsFile:
1231 ext_cmd ();
1232 break;
1233 case CK_EditFileHighlightFile:
1234 edit_fhl_cmd ();
1235 break;
1236 case CK_EditUserMenu:
1237 edit_mc_menu_cmd ();
1238 break;
1239 case CK_LinkSymbolicEdit:
1240 edit_symlink_cmd ();
1241 break;
1242 case CK_ExternalPanelize:
1243 external_panelize ();
1244 break;
1245 case CK_Filter:
1246 filter_cmd ();
1247 break;
1248 case CK_ViewFiltered:
1249 view_filtered_cmd ();
1250 break;
1251 case CK_Find:
1252 find_cmd ();
1253 break;
1254 #ifdef ENABLE_VFS_FISH
1255 case CK_ConnectFish:
1256 fishlink_cmd ();
1257 break;
1258 #endif
1259 #ifdef ENABLE_VFS_FTP
1260 case CK_ConnectFtp:
1261 ftplink_cmd ();
1262 break;
1263 #endif
1264 #ifdef ENABLE_VFS_SFTP
1265 case CK_ConnectSftp:
1266 sftplink_cmd ();
1267 break;
1268 #endif
1269 #ifdef ENABLE_VFS_SMB
1270 case CK_ConnectSmb:
1271 smblink_cmd ();
1272 break;
1273 #endif /* ENABLE_VFS_SMB */
1274 case CK_Panelize:
1275 cd_panelize_cmd ();
1276 break;
1277 case CK_Help:
1278 help_cmd ();
1279 break;
1280 case CK_History:
1281 /* show the history of command line widget */
1282 send_message (cmdline, NULL, MSG_ACTION, CK_History, NULL);
1283 break;
1284 case CK_PanelInfo:
1285 if (sender == WIDGET (the_menubar))
1286 info_cmd (); /* menu */
1287 else
1288 info_cmd_no_menu (); /* shortcut or buttonbar */
1289 break;
1290 #ifdef ENABLE_BACKGROUND
1291 case CK_Jobs:
1292 jobs_box ();
1293 break;
1294 #endif
1295 case CK_OptionsLayout:
1296 layout_box ();
1297 break;
1298 case CK_OptionsAppearance:
1299 appearance_box ();
1300 break;
1301 case CK_LearnKeys:
1302 learn_keys ();
1303 break;
1304 case CK_Link:
1305 link_cmd (LINK_HARDLINK);
1306 break;
1307 case CK_PanelListing:
1308 listing_cmd ();
1309 break;
1310 #ifdef LISTMODE_EDITOR
1311 case CK_ListMode:
1312 listmode_cmd ();
1313 break;
1314 #endif
1315 case CK_Menu:
1316 menu_cmd ();
1317 break;
1318 case CK_MenuLastSelected:
1319 menu_last_selected_cmd ();
1320 break;
1321 case CK_MakeDir:
1322 mkdir_cmd ();
1323 break;
1324 case CK_OptionsPanel:
1325 panel_options_box ();
1326 break;
1327 #ifdef HAVE_CHARSET
1328 case CK_SelectCodepage:
1329 encoding_cmd ();
1330 break;
1331 #endif
1332 case CK_CdQuick:
1333 quick_cd_cmd ();
1334 break;
1335 case CK_HotList:
1336 hotlist_cmd ();
1337 break;
1338 case CK_PanelQuickView:
1339 if (sender == WIDGET (the_menubar))
1340 quick_view_cmd (); /* menu */
1341 else
1342 quick_cmd_no_menu (); /* shortcut or buttonabr */
1343 break;
1344 case CK_QuitQuiet:
1345 quiet_quit_cmd ();
1346 break;
1347 case CK_Quit:
1348 quit_cmd ();
1349 break;
1350 case CK_LinkSymbolicRelative:
1351 link_cmd (LINK_SYMLINK_RELATIVE);
1352 break;
1353 case CK_Move:
1354 rename_cmd ();
1355 break;
1356 case CK_Reread:
1357 reread_cmd ();
1358 break;
1359 #ifdef ENABLE_VFS
1360 case CK_VfsList:
1361 vfs_list ();
1362 break;
1363 #endif
1364 case CK_SaveSetup:
1365 save_setup_cmd ();
1366 break;
1367 case CK_Select:
1368 case CK_Unselect:
1369 case CK_SelectInvert:
1370 res = send_message (current_panel, midnight_dlg, MSG_ACTION, command, NULL);
1371 break;
1372 case CK_Shell:
1373 toggle_subshell ();
1374 break;
1375 case CK_DirSize:
1376 smart_dirsize_cmd ();
1377 break;
1378 case CK_Sort:
1379 sort_cmd ();
1380 break;
1381 case CK_ExtendedKeyMap:
1382 ctl_x_cmd ();
1383 break;
1384 case CK_Suspend:
1385 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
1386 break;
1387 case CK_Swap:
1388 swap_cmd ();
1389 break;
1390 case CK_LinkSymbolic:
1391 link_cmd (LINK_SYMLINK_ABSOLUTE);
1392 break;
1393 case CK_ShowHidden:
1394 toggle_show_hidden ();
1395 break;
1396 case CK_SplitVertHoriz:
1397 toggle_panels_split ();
1398 break;
1399 case CK_SplitEqual:
1400 panels_split_equal ();
1401 break;
1402 case CK_SplitMore:
1403 panels_split_more ();
1404 break;
1405 case CK_SplitLess:
1406 panels_split_less ();
1407 break;
1408 case CK_PanelTree:
1409 panel_tree_cmd ();
1410 break;
1411 case CK_Tree:
1412 treebox_cmd ();
1413 break;
1414 #ifdef ENABLE_VFS_UNDELFS
1415 case CK_Undelete:
1416 undelete_cmd ();
1417 break;
1418 #endif
1419 case CK_UserMenu:
1420 user_file_menu_cmd ();
1421 break;
1422 case CK_View:
1423 view_cmd ();
1424 break;
1425 case CK_ViewFile:
1426 view_file_cmd ();
1427 break;
1428 case CK_EditorViewerHistory:
1429 show_editor_viewer_history ();
1430 break;
1431 case CK_Cancel:
1432 /* don't close panels due to SIGINT */
1433 break;
1434 default:
1435 res = MSG_NOT_HANDLED;
1438 return res;
1441 /* --------------------------------------------------------------------------------------------- */
1444 * Whether the command-line should not respond to key events.
1446 * This is TRUE if a QuickView or TreeView have the focus, as they're going
1447 * to consume some keys and there's no sense in passing to the command-line
1448 * just the leftovers.
1450 static gboolean
1451 is_cmdline_mute (void)
1453 /* When one of panels is other than view_listing,
1454 current_panel points to view_listing panel all time independently of
1455 it's activity. Thus, we can't use get_current_type() here.
1456 current_panel should point to actualy current active panel
1457 independently of it's type. */
1458 return (current_panel->active == 0
1459 && (get_other_type () == view_quick || get_other_type () == view_tree));
1462 /* --------------------------------------------------------------------------------------------- */
1465 * Handles the Enter key on the command-line.
1467 * Returns TRUE if non-whitespace was indeed processed.
1469 static gboolean
1470 handle_cmdline_enter (void)
1472 size_t i;
1474 for (i = 0; cmdline->buffer[i] != '\0' && whitespace (cmdline->buffer[i]); i++)
1477 if (cmdline->buffer[i] != '\0')
1479 send_message (cmdline, NULL, MSG_KEY, '\n', NULL);
1480 return TRUE;
1483 input_insert (cmdline, "", FALSE);
1484 cmdline->point = 0;
1486 return FALSE;
1489 /* --------------------------------------------------------------------------------------------- */
1491 static cb_ret_t
1492 midnight_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
1494 long command;
1496 switch (msg)
1498 case MSG_INIT:
1499 panel_init ();
1500 setup_panels ();
1501 return MSG_HANDLED;
1503 case MSG_DRAW:
1504 load_hint (TRUE);
1505 /* We handle the special case of the output lines */
1506 if (mc_global.tty.console_flag != '\0' && output_lines != 0)
1508 unsigned char end_line;
1510 end_line = LINES - (mc_global.keybar_visible ? 1 : 0) - 1;
1511 show_console_contents (output_start_y, end_line - output_lines, end_line);
1513 return MSG_HANDLED;
1515 case MSG_RESIZE:
1516 widget_adjust_position (w->pos_flags, &w->y, &w->x, &w->lines, &w->cols);
1517 setup_panels ();
1518 menubar_arrange (the_menubar);
1519 return MSG_HANDLED;
1521 case MSG_IDLE:
1522 /* We only need the first idle event to show user menu after start */
1523 widget_idle (w, FALSE);
1525 if (boot_current_is_left)
1526 widget_select (get_panel_widget (0));
1527 else
1528 widget_select (get_panel_widget (1));
1530 if (auto_menu)
1531 midnight_execute_cmd (NULL, CK_UserMenu);
1532 return MSG_HANDLED;
1534 case MSG_KEY:
1535 if (ctl_x_map_enabled)
1537 ctl_x_map_enabled = FALSE;
1538 command = keybind_lookup_keymap_command (main_x_map, parm);
1539 if (command != CK_IgnoreKey)
1540 return midnight_execute_cmd (NULL, command);
1543 /* FIXME: should handle all menu shortcuts before this point */
1544 if (widget_get_state (WIDGET (the_menubar), WST_FOCUSED))
1545 return MSG_NOT_HANDLED;
1547 if (parm == '\n' && !is_cmdline_mute ())
1549 if (handle_cmdline_enter ())
1550 return MSG_HANDLED;
1551 /* Else: the panel will handle it. */
1554 if ((!mc_global.tty.alternate_plus_minus
1555 || !(mc_global.tty.console_flag != '\0' || mc_global.tty.xterm_flag)) && !quote
1556 && !current_panel->searching)
1558 if (!only_leading_plus_minus)
1560 /* Special treatement, since the input line will eat them */
1561 if (parm == '+')
1562 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Select, NULL);
1564 if (parm == '\\' || parm == '-')
1565 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Unselect,
1566 NULL);
1568 if (parm == '*')
1569 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_SelectInvert,
1570 NULL);
1572 else if (!command_prompt || input_is_empty (cmdline))
1574 /* Special treatement '+', '-', '\', '*' only when this is
1575 * first char on input line
1577 if (parm == '+')
1578 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Select, NULL);
1580 if (parm == '\\' || parm == '-')
1581 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Unselect,
1582 NULL);
1584 if (parm == '*')
1585 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_SelectInvert,
1586 NULL);
1589 return MSG_NOT_HANDLED;
1591 case MSG_HOTKEY_HANDLED:
1592 if ((get_current_type () == view_listing) && current_panel->searching)
1594 current_panel->dirty = 1; /* FIXME: unneeded? */
1595 send_message (current_panel, NULL, MSG_ACTION, CK_SearchStop, NULL);
1597 return MSG_HANDLED;
1599 case MSG_UNHANDLED_KEY:
1601 cb_ret_t v = MSG_NOT_HANDLED;
1603 if (ctl_x_map_enabled)
1605 ctl_x_map_enabled = FALSE;
1606 command = keybind_lookup_keymap_command (main_x_map, parm);
1608 else
1609 command = keybind_lookup_keymap_command (main_map, parm);
1611 if (command != CK_IgnoreKey)
1612 v = midnight_execute_cmd (NULL, command);
1614 if (v == MSG_NOT_HANDLED && command_prompt && !is_cmdline_mute ())
1615 v = send_message (cmdline, NULL, MSG_KEY, parm, NULL);
1617 return v;
1620 case MSG_POST_KEY:
1621 if (!widget_get_state (WIDGET (the_menubar), WST_FOCUSED))
1622 update_dirty_panels ();
1623 return MSG_HANDLED;
1625 case MSG_ACTION:
1626 /* Handle shortcuts, menu, and buttonbar. */
1627 return midnight_execute_cmd (sender, parm);
1629 case MSG_END:
1630 panel_deinit ();
1631 return MSG_HANDLED;
1633 default:
1634 return dlg_default_callback (w, sender, msg, parm, data);
1638 /* --------------------------------------------------------------------------------------------- */
1639 /*** public functions ****************************************************************************/
1640 /* --------------------------------------------------------------------------------------------- */
1642 void
1643 update_menu (void)
1645 menu_set_name (left_menu, panels_layout.horizontal_split ? _("&Above") : _("&Left"));
1646 menu_set_name (right_menu, panels_layout.horizontal_split ? _("&Below") : _("&Right"));
1647 menubar_arrange (the_menubar);
1648 menubar_set_visible (the_menubar, menubar_visible);
1651 /* --------------------------------------------------------------------------------------------- */
1653 void
1654 midnight_set_buttonbar (WButtonBar * b)
1656 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), main_map, NULL);
1657 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Menu"), main_map, NULL);
1658 buttonbar_set_label (b, 3, Q_ ("ButtonBar|View"), main_map, NULL);
1659 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), main_map, NULL);
1660 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), main_map, NULL);
1661 buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), main_map, NULL);
1662 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), main_map, NULL);
1663 buttonbar_set_label (b, 8, Q_ ("ButtonBar|Delete"), main_map, NULL);
1664 buttonbar_set_label (b, 9, Q_ ("ButtonBar|PullDn"), main_map, NULL);
1665 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), main_map, NULL);
1668 /* --------------------------------------------------------------------------------------------- */
1670 * Return a random hint. If force is TRUE, ignore the timeout.
1673 char *
1674 get_random_hint (gboolean force)
1676 static const guint64 update_period = 60 * G_USEC_PER_SEC;
1677 static guint64 tv = 0;
1679 char *data, *result = NULL, *eop;
1680 size_t len, start;
1681 GIConv conv;
1683 /* Do not change hints more often than one minute */
1684 if (!force && !mc_time_elapsed (&tv, update_period))
1685 return g_strdup ("");
1687 data = load_mc_home_file (mc_global.share_data_dir, MC_HINT, NULL, &len);
1688 if (data == NULL)
1689 return NULL;
1691 /* get a random entry */
1692 srand ((unsigned int) (tv / G_USEC_PER_SEC));
1693 start = ((size_t) rand ()) % (len - 1);
1695 /* Search the start of paragraph */
1696 for (; start != 0; start--)
1697 if (data[start] == '\n' && data[start + 1] == '\n')
1699 start += 2;
1700 break;
1703 /* Search the end of paragraph */
1704 for (eop = data + start; *eop != '\0'; eop++)
1706 if (*eop == '\n' && *(eop + 1) == '\n')
1708 *eop = '\0';
1709 break;
1711 if (*eop == '\n')
1712 *eop = ' ';
1715 /* hint files are stored in utf-8 */
1716 /* try convert hint file from utf-8 to terminal encoding */
1717 conv = str_crt_conv_from ("UTF-8");
1718 if (conv != INVALID_CONV)
1720 GString *buffer;
1722 buffer = g_string_sized_new (len - start);
1723 if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE)
1724 result = g_string_free (buffer, FALSE);
1725 else
1726 g_string_free (buffer, TRUE);
1727 str_close_conv (conv);
1729 else
1730 result = g_strndup (data + start, len - start);
1732 g_free (data);
1733 return result;
1737 /* --------------------------------------------------------------------------------------------- */
1739 * Load new hint and display it.
1740 * IF force is not 0, ignore the timeout.
1743 void
1744 load_hint (gboolean force)
1746 char *hint;
1748 if (WIDGET (the_hint)->owner == NULL)
1749 return;
1751 if (!mc_global.message_visible)
1753 label_set_text (the_hint, NULL);
1754 return;
1757 hint = get_random_hint (force);
1759 if (hint != NULL)
1761 if (*hint != '\0')
1762 set_hintbar (hint);
1763 g_free (hint);
1765 else
1767 char text[BUF_SMALL];
1769 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"), VERSION);
1770 set_hintbar (text);
1774 /* --------------------------------------------------------------------------------------------- */
1776 void
1777 change_panel (void)
1779 input_free_completions (cmdline);
1780 group_select_next_widget (GROUP (midnight_dlg));
1783 /* --------------------------------------------------------------------------------------------- */
1785 /** Save current stat of directories to avoid reloading the panels
1786 * when no modifications have taken place
1788 void
1789 save_cwds_stat (void)
1791 if (panels_options.fast_reload)
1793 mc_stat (current_panel->cwd_vpath, &(current_panel->dir_stat));
1794 if (get_other_type () == view_listing)
1795 mc_stat (other_panel->cwd_vpath, &(other_panel->dir_stat));
1799 /* --------------------------------------------------------------------------------------------- */
1801 gboolean
1802 quiet_quit_cmd (void)
1804 print_last_revert = TRUE;
1805 return quit_cmd_internal (1);
1808 /* --------------------------------------------------------------------------------------------- */
1810 /** Run the main dialog that occupies the whole screen */
1811 gboolean
1812 do_nc (void)
1814 gboolean ret;
1816 #ifdef USE_INTERNAL_EDIT
1817 edit_stack_init ();
1818 #endif
1820 midnight_dlg = dlg_create (FALSE, 0, 0, 1, 1, WPOS_FULLSCREEN, FALSE, dialog_colors,
1821 midnight_callback, NULL, "[main]", NULL);
1823 /* Check if we were invoked as an editor or file viewer */
1824 if (mc_global.mc_run_mode != MC_RUN_FULL)
1826 setup_dummy_mc ();
1827 ret = mc_maybe_editor_or_viewer ();
1829 else
1831 /* We only need the first idle event to show user menu after start */
1832 widget_idle (WIDGET (midnight_dlg), TRUE);
1834 setup_mc ();
1835 mc_filehighlight = mc_fhl_new (TRUE);
1837 create_file_manager ();
1838 (void) dlg_run (midnight_dlg);
1840 mc_fhl_free (&mc_filehighlight);
1842 ret = TRUE;
1844 /* dlg_destroy destroys even current_panel->cwd_vpath, so we have to save a copy :) */
1845 if (mc_args__last_wd_file != NULL && vfs_current_is_local ())
1846 last_wd_string = g_strdup (vfs_path_as_str (current_panel->cwd_vpath));
1848 /* don't handle VFS timestamps for dirs opened in panels */
1849 mc_event_destroy (MCEVENT_GROUP_CORE, "vfs_timestamp");
1851 dir_list_free_list (&panelized_panel.list);
1854 /* Program end */
1855 mc_global.midnight_shutdown = TRUE;
1856 dialog_switch_shutdown ();
1857 done_mc ();
1858 dlg_destroy (midnight_dlg);
1859 current_panel = NULL;
1861 #ifdef USE_INTERNAL_EDIT
1862 edit_stack_free ();
1863 #endif
1865 if ((quit & SUBSHELL_EXIT) == 0)
1866 clr_scr ();
1868 return ret;
1871 /* --------------------------------------------------------------------------------------------- */