Rename WDialog APIs:
[midnight-commander.git] / src / filemanager / midnight.c
blobc252276ea55f973e32ee8e29ad1be62de9980f9e
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_current_selected (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_PutCurrentSelected:
1143 put_current_selected ();
1144 break;
1145 case CK_PutCurrentFullSelected:
1146 midnight_put_panel_path (current_panel);
1147 put_current_selected ();
1148 break;
1149 case CK_PutCurrentLink:
1150 put_current_link ();
1151 break;
1152 case CK_PutCurrentTagged:
1153 put_current_tagged ();
1154 break;
1155 case CK_PutOtherPath:
1156 if (get_other_type () == view_listing)
1157 midnight_put_panel_path (other_panel);
1158 break;
1159 case CK_PutOtherLink:
1160 put_other_link ();
1161 break;
1162 case CK_PutOtherTagged:
1163 put_other_tagged ();
1164 break;
1165 case CK_Delete:
1166 delete_cmd ();
1167 break;
1168 case CK_ScreenList:
1169 dialog_switch_list ();
1170 break;
1171 #ifdef USE_DIFF_VIEW
1172 case CK_CompareFiles:
1173 diff_view_cmd ();
1174 break;
1175 #endif
1176 case CK_OptionsDisplayBits:
1177 display_bits_box ();
1178 break;
1179 case CK_Edit:
1180 edit_cmd ();
1181 break;
1182 #ifdef USE_INTERNAL_EDIT
1183 case CK_EditForceInternal:
1184 edit_cmd_force_internal ();
1185 break;
1186 #endif
1187 case CK_EditExtensionsFile:
1188 ext_cmd ();
1189 break;
1190 case CK_EditFileHighlightFile:
1191 edit_fhl_cmd ();
1192 break;
1193 case CK_EditUserMenu:
1194 edit_mc_menu_cmd ();
1195 break;
1196 case CK_LinkSymbolicEdit:
1197 edit_symlink_cmd ();
1198 break;
1199 case CK_ExternalPanelize:
1200 external_panelize ();
1201 break;
1202 case CK_Filter:
1203 filter_cmd ();
1204 break;
1205 case CK_ViewFiltered:
1206 view_filtered_cmd ();
1207 break;
1208 case CK_Find:
1209 find_cmd ();
1210 break;
1211 #ifdef ENABLE_VFS_FISH
1212 case CK_ConnectFish:
1213 fishlink_cmd ();
1214 break;
1215 #endif
1216 #ifdef ENABLE_VFS_FTP
1217 case CK_ConnectFtp:
1218 ftplink_cmd ();
1219 break;
1220 #endif
1221 #ifdef ENABLE_VFS_SFTP
1222 case CK_ConnectSftp:
1223 sftplink_cmd ();
1224 break;
1225 #endif
1226 #ifdef ENABLE_VFS_SMB
1227 case CK_ConnectSmb:
1228 smblink_cmd ();
1229 break;
1230 #endif /* ENABLE_VFS_SMB */
1231 case CK_Panelize:
1232 cd_panelize_cmd ();
1233 break;
1234 case CK_Help:
1235 help_cmd ();
1236 break;
1237 case CK_History:
1238 /* show the history of command line widget */
1239 send_message (cmdline, NULL, MSG_ACTION, CK_History, NULL);
1240 break;
1241 case CK_PanelInfo:
1242 if (sender == WIDGET (the_menubar))
1243 info_cmd (); /* menu */
1244 else
1245 info_cmd_no_menu (); /* shortcut or buttonbar */
1246 break;
1247 #ifdef ENABLE_BACKGROUND
1248 case CK_Jobs:
1249 jobs_cmd ();
1250 break;
1251 #endif
1252 case CK_OptionsLayout:
1253 layout_box ();
1254 break;
1255 case CK_OptionsAppearance:
1256 appearance_box ();
1257 break;
1258 case CK_LearnKeys:
1259 learn_keys ();
1260 break;
1261 case CK_Link:
1262 link_cmd (LINK_HARDLINK);
1263 break;
1264 case CK_PanelListing:
1265 listing_cmd ();
1266 break;
1267 #ifdef LISTMODE_EDITOR
1268 case CK_ListMode:
1269 listmode_cmd ();
1270 break;
1271 #endif
1272 case CK_Menu:
1273 menu_cmd ();
1274 break;
1275 case CK_MenuLastSelected:
1276 menu_last_selected_cmd ();
1277 break;
1278 case CK_MakeDir:
1279 mkdir_cmd ();
1280 break;
1281 case CK_OptionsPanel:
1282 panel_options_box ();
1283 break;
1284 #ifdef HAVE_CHARSET
1285 case CK_SelectCodepage:
1286 encoding_cmd ();
1287 break;
1288 #endif
1289 case CK_CdQuick:
1290 quick_cd_cmd ();
1291 break;
1292 case CK_HotList:
1293 hotlist_cmd ();
1294 break;
1295 case CK_PanelQuickView:
1296 if (sender == WIDGET (the_menubar))
1297 quick_view_cmd (); /* menu */
1298 else
1299 quick_cmd_no_menu (); /* shortcut or buttonabr */
1300 break;
1301 case CK_QuitQuiet:
1302 quiet_quit_cmd ();
1303 break;
1304 case CK_Quit:
1305 quit_cmd ();
1306 break;
1307 case CK_LinkSymbolicRelative:
1308 link_cmd (LINK_SYMLINK_RELATIVE);
1309 break;
1310 case CK_Move:
1311 rename_cmd ();
1312 break;
1313 case CK_Reread:
1314 reread_cmd ();
1315 break;
1316 #ifdef ENABLE_VFS
1317 case CK_VfsList:
1318 vfs_list ();
1319 break;
1320 #endif
1321 case CK_SaveSetup:
1322 save_setup_cmd ();
1323 break;
1324 case CK_Select:
1325 case CK_Unselect:
1326 case CK_SelectInvert:
1327 res = send_message (current_panel, midnight_dlg, MSG_ACTION, command, NULL);
1328 break;
1329 case CK_Shell:
1330 view_other_cmd ();
1331 break;
1332 case CK_DirSize:
1333 smart_dirsize_cmd ();
1334 break;
1335 case CK_Sort:
1336 sort_cmd ();
1337 break;
1338 case CK_ExtendedKeyMap:
1339 ctl_x_cmd ();
1340 break;
1341 case CK_Suspend:
1342 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
1343 break;
1344 case CK_Swap:
1345 swap_cmd ();
1346 break;
1347 case CK_LinkSymbolic:
1348 link_cmd (LINK_SYMLINK_ABSOLUTE);
1349 break;
1350 case CK_PanelListingSwitch:
1351 toggle_listing_cmd ();
1352 break;
1353 case CK_ShowHidden:
1354 toggle_show_hidden ();
1355 break;
1356 case CK_SplitVertHoriz:
1357 toggle_panels_split ();
1358 break;
1359 case CK_SplitEqual:
1360 panels_split_equal ();
1361 break;
1362 case CK_SplitMore:
1363 panels_split_more ();
1364 break;
1365 case CK_SplitLess:
1366 panels_split_less ();
1367 break;
1368 case CK_PanelTree:
1369 panel_tree_cmd ();
1370 break;
1371 case CK_Tree:
1372 treebox_cmd ();
1373 break;
1374 #ifdef ENABLE_VFS_UNDELFS
1375 case CK_Undelete:
1376 undelete_cmd ();
1377 break;
1378 #endif
1379 case CK_UserMenu:
1380 user_file_menu_cmd ();
1381 break;
1382 case CK_View:
1383 view_cmd ();
1384 break;
1385 case CK_ViewFile:
1386 view_file_cmd ();
1387 break;
1388 case CK_Cancel:
1389 /* don't close panels due to SIGINT */
1390 break;
1391 default:
1392 res = MSG_NOT_HANDLED;
1395 return res;
1398 /* --------------------------------------------------------------------------------------------- */
1400 static cb_ret_t
1401 midnight_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
1403 long command;
1405 switch (msg)
1407 case MSG_INIT:
1408 panel_init ();
1409 setup_panels ();
1410 return MSG_HANDLED;
1412 case MSG_DRAW:
1413 load_hint (TRUE);
1414 /* We handle the special case of the output lines */
1415 if (mc_global.tty.console_flag != '\0' && output_lines)
1416 show_console_contents (output_start_y,
1417 LINES - output_lines - mc_global.keybar_visible -
1418 1, LINES - mc_global.keybar_visible - 1);
1419 return MSG_HANDLED;
1421 case MSG_RESIZE:
1422 /* dlg_set_size() is surplus for this case */
1423 w->lines = LINES;
1424 w->cols = COLS;
1425 setup_panels ();
1426 menubar_arrange (the_menubar);
1427 return MSG_HANDLED;
1429 case MSG_IDLE:
1430 /* We only need the first idle event to show user menu after start */
1431 widget_idle (w, FALSE);
1433 if (boot_current_is_left)
1434 dlg_select_widget (get_panel_widget (0));
1435 else
1436 dlg_select_widget (get_panel_widget (1));
1438 if (auto_menu)
1439 midnight_execute_cmd (NULL, CK_UserMenu);
1440 return MSG_HANDLED;
1442 case MSG_KEY:
1443 if (ctl_x_map_enabled)
1445 ctl_x_map_enabled = FALSE;
1446 command = keybind_lookup_keymap_command (main_x_map, parm);
1447 if (command != CK_IgnoreKey)
1448 return midnight_execute_cmd (NULL, command);
1451 /* FIXME: should handle all menu shortcuts before this point */
1452 if (widget_get_state (WIDGET (the_menubar), WST_FOCUSED))
1453 return MSG_NOT_HANDLED;
1455 if (parm == '\t')
1456 input_free_completions (cmdline);
1458 if (parm == '\n')
1460 size_t i;
1462 /* HACK: don't execute command in the command line if Enter was pressed
1463 in the quick viewer panel. */
1464 /* TODO: currently, when one of panels is other than view_listing,
1465 current_panel points to view_listing panel all time independently of
1466 it's activity. Thus, we can't use get_current_type() here.
1467 current_panel should point to actualy current active panel
1468 independently of it's type. */
1469 if (current_panel->active == 0 && get_other_type () == view_quick)
1470 return MSG_NOT_HANDLED;
1472 for (i = 0; cmdline->buffer[i] != '\0' &&
1473 (cmdline->buffer[i] == ' ' || cmdline->buffer[i] == '\t'); i++)
1476 if (cmdline->buffer[i] != '\0')
1478 send_message (cmdline, NULL, MSG_KEY, parm, NULL);
1479 return MSG_HANDLED;
1482 input_insert (cmdline, "", FALSE);
1483 cmdline->point = 0;
1486 if ((!mc_global.tty.alternate_plus_minus
1487 || !(mc_global.tty.console_flag != '\0' || mc_global.tty.xterm_flag)) && !quote
1488 && !current_panel->searching)
1490 if (!only_leading_plus_minus)
1492 /* Special treatement, since the input line will eat them */
1493 if (parm == '+')
1494 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Select, NULL);
1496 if (parm == '\\' || parm == '-')
1497 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Unselect,
1498 NULL);
1500 if (parm == '*')
1501 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_SelectInvert,
1502 NULL);
1504 else if (!command_prompt || cmdline->buffer[0] == '\0')
1506 /* Special treatement '+', '-', '\', '*' only when this is
1507 * first char on input line
1509 if (parm == '+')
1510 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Select, NULL);
1512 if (parm == '\\' || parm == '-')
1513 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Unselect,
1514 NULL);
1516 if (parm == '*')
1517 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_SelectInvert,
1518 NULL);
1521 return MSG_NOT_HANDLED;
1523 case MSG_HOTKEY_HANDLED:
1524 if ((get_current_type () == view_listing) && current_panel->searching)
1526 current_panel->dirty = 1; /* FIXME: unneeded? */
1527 send_message (current_panel, NULL, MSG_ACTION, CK_SearchStop, NULL);
1529 return MSG_HANDLED;
1531 case MSG_UNHANDLED_KEY:
1533 cb_ret_t v = MSG_NOT_HANDLED;
1535 if (ctl_x_map_enabled)
1537 ctl_x_map_enabled = FALSE;
1538 command = keybind_lookup_keymap_command (main_x_map, parm);
1540 else
1541 command = keybind_lookup_keymap_command (main_map, parm);
1543 if (command != CK_IgnoreKey)
1544 v = midnight_execute_cmd (NULL, command);
1546 if (v == MSG_NOT_HANDLED && command_prompt)
1547 v = send_message (cmdline, NULL, MSG_KEY, parm, NULL);
1549 return v;
1552 case MSG_POST_KEY:
1553 if (!widget_get_state (WIDGET (the_menubar), WST_FOCUSED))
1554 update_dirty_panels ();
1555 return MSG_HANDLED;
1557 case MSG_ACTION:
1558 /* Handle shortcuts, menu, and buttonbar. */
1559 return midnight_execute_cmd (sender, parm);
1561 case MSG_END:
1562 panel_deinit ();
1563 return MSG_HANDLED;
1565 default:
1566 return dlg_default_callback (w, sender, msg, parm, data);
1570 /* --------------------------------------------------------------------------------------------- */
1571 /*** public functions ****************************************************************************/
1572 /* --------------------------------------------------------------------------------------------- */
1574 void
1575 update_menu (void)
1577 menu_set_name (left_menu, panels_layout.horizontal_split ? _("&Above") : _("&Left"));
1578 menu_set_name (right_menu, panels_layout.horizontal_split ? _("&Below") : _("&Right"));
1579 menubar_arrange (the_menubar);
1580 menubar_set_visible (the_menubar, menubar_visible);
1583 /* --------------------------------------------------------------------------------------------- */
1585 void
1586 midnight_set_buttonbar (WButtonBar * b)
1588 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), main_map, NULL);
1589 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Menu"), main_map, NULL);
1590 buttonbar_set_label (b, 3, Q_ ("ButtonBar|View"), main_map, NULL);
1591 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), main_map, NULL);
1592 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), main_map, NULL);
1593 buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), main_map, NULL);
1594 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), main_map, NULL);
1595 buttonbar_set_label (b, 8, Q_ ("ButtonBar|Delete"), main_map, NULL);
1596 buttonbar_set_label (b, 9, Q_ ("ButtonBar|PullDn"), main_map, NULL);
1597 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), main_map, NULL);
1600 /* --------------------------------------------------------------------------------------------- */
1602 * Return a random hint. If force is TRUE, ignore the timeout.
1605 char *
1606 get_random_hint (gboolean force)
1608 static const guint64 update_period = 60 * G_USEC_PER_SEC;
1609 static guint64 tv = 0;
1611 char *data, *result = NULL, *eop;
1612 size_t len, start;
1613 GIConv conv;
1615 /* Do not change hints more often than one minute */
1616 if (!force && !mc_time_elapsed (&tv, update_period))
1617 return g_strdup ("");
1619 data = load_mc_home_file (mc_global.share_data_dir, MC_HINT, NULL, &len);
1620 if (data == NULL)
1621 return NULL;
1623 /* get a random entry */
1624 srand ((unsigned int) (tv / G_USEC_PER_SEC));
1625 start = ((size_t) rand ()) % (len - 1);
1627 /* Search the start of paragraph */
1628 for (; start != 0; start--)
1629 if (data[start] == '\n' && data[start + 1] == '\n')
1631 start += 2;
1632 break;
1635 /* Search the end of paragraph */
1636 for (eop = data + start; *eop != '\0'; eop++)
1638 if (*eop == '\n' && *(eop + 1) == '\n')
1640 *eop = '\0';
1641 break;
1643 if (*eop == '\n')
1644 *eop = ' ';
1647 /* hint files are stored in utf-8 */
1648 /* try convert hint file from utf-8 to terminal encoding */
1649 conv = str_crt_conv_from ("UTF-8");
1650 if (conv != INVALID_CONV)
1652 GString *buffer;
1654 buffer = g_string_sized_new (len - start);
1655 if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE)
1656 result = g_string_free (buffer, FALSE);
1657 else
1658 g_string_free (buffer, TRUE);
1659 str_close_conv (conv);
1661 else
1662 result = g_strndup (data + start, len - start);
1664 g_free (data);
1665 return result;
1669 /* --------------------------------------------------------------------------------------------- */
1671 * Load new hint and display it.
1672 * IF force is not 0, ignore the timeout.
1675 void
1676 load_hint (gboolean force)
1678 char *hint;
1680 if (WIDGET (the_hint)->owner == NULL)
1681 return;
1683 if (!mc_global.message_visible)
1685 label_set_text (the_hint, NULL);
1686 return;
1689 hint = get_random_hint (force);
1691 if (hint != NULL)
1693 if (*hint != '\0')
1694 set_hintbar (hint);
1695 g_free (hint);
1697 else
1699 char text[BUF_SMALL];
1701 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"), VERSION);
1702 set_hintbar (text);
1706 /* --------------------------------------------------------------------------------------------- */
1708 void
1709 change_panel (void)
1711 input_free_completions (cmdline);
1712 dlg_select_next_widget (midnight_dlg);
1715 /* --------------------------------------------------------------------------------------------- */
1717 /** Save current stat of directories to avoid reloading the panels
1718 * when no modifications have taken place
1720 void
1721 save_cwds_stat (void)
1723 if (panels_options.fast_reload)
1725 mc_stat (current_panel->cwd_vpath, &(current_panel->dir_stat));
1726 if (get_other_type () == view_listing)
1727 mc_stat (other_panel->cwd_vpath, &(other_panel->dir_stat));
1731 /* --------------------------------------------------------------------------------------------- */
1733 gboolean
1734 quiet_quit_cmd (void)
1736 print_last_revert = TRUE;
1737 return quit_cmd_internal (1);
1740 /* --------------------------------------------------------------------------------------------- */
1742 /** Run the main dialog that occupies the whole screen */
1743 gboolean
1744 do_nc (void)
1746 gboolean ret;
1748 #ifdef USE_INTERNAL_EDIT
1749 edit_stack_init ();
1750 #endif
1752 midnight_dlg = dlg_create (FALSE, 0, 0, 1, 1, WPOS_FULLSCREEN, FALSE, dialog_colors,
1753 midnight_callback, NULL, "[main]", NULL);
1755 /* Check if we were invoked as an editor or file viewer */
1756 if (mc_global.mc_run_mode != MC_RUN_FULL)
1758 setup_dummy_mc ();
1759 ret = mc_maybe_editor_or_viewer ();
1761 else
1763 /* We only need the first idle event to show user menu after start */
1764 widget_idle (WIDGET (midnight_dlg), TRUE);
1766 setup_mc ();
1767 mc_filehighlight = mc_fhl_new (TRUE);
1768 create_panels_and_run_mc ();
1769 mc_fhl_free (&mc_filehighlight);
1771 ret = TRUE;
1773 /* dlg_destroy destroys even current_panel->cwd_vpath, so we have to save a copy :) */
1774 if (mc_args__last_wd_file != NULL && vfs_current_is_local ())
1775 last_wd_string = g_strdup (vfs_path_as_str (current_panel->cwd_vpath));
1777 /* don't handle VFS timestamps for dirs opened in panels */
1778 mc_event_destroy (MCEVENT_GROUP_CORE, "vfs_timestamp");
1780 dir_list_clean (&panelized_panel.list);
1783 /* Program end */
1784 mc_global.midnight_shutdown = TRUE;
1785 dialog_switch_shutdown ();
1786 done_mc ();
1787 dlg_destroy (midnight_dlg);
1788 current_panel = NULL;
1790 #ifdef USE_INTERNAL_EDIT
1791 edit_stack_free ();
1792 #endif
1794 if ((quit & SUBSHELL_EXIT) == 0)
1795 clr_scr ();
1797 return ret;
1800 /* --------------------------------------------------------------------------------------------- */