From 3b72e4275bb1ec3ae86d0c0744d990123861d67e Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Mon, 1 Aug 2011 13:04:28 +0400 Subject: [PATCH] Switch windows using keyboard. Signed-off-by: Andrew Borodin --- lib/keybind.c | 3 ++ lib/keybind.h | 3 ++ misc/mc.keymap.default | 3 ++ misc/mc.keymap.emacs | 3 ++ src/editor/edit.c | 7 ----- src/editor/editmenu.c | 4 +++ src/editor/editwidget.c | 69 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 85 insertions(+), 7 deletions(-) diff --git a/lib/keybind.c b/lib/keybind.c index b47b82b4d..4d8e3bc78 100644 --- a/lib/keybind.c +++ b/lib/keybind.c @@ -314,6 +314,9 @@ static name_keymap_t command_names[] = { {"ExecuteScript", CK_PipeBlock (0)}, {"WindowMove", CK_WindowMove}, {"WindowResize", CK_WindowResize}, + {"WindowList", CK_WindowList}, + {"WindowNext", CK_WindowNext}, + {"WindowPrev", CK_WindowPrev}, #endif /* USE_INTERNAL_EDIT */ /* viewer */ diff --git a/lib/keybind.h b/lib/keybind.h index 939f2e56c..efa931eae 100644 --- a/lib/keybind.h +++ b/lib/keybind.h @@ -282,6 +282,9 @@ enum /* window commands */ CK_WindowMove, CK_WindowResize, + CK_WindowList, + CK_WindowNext, + CK_WindowPrev, /* misc commands */ CK_InsertOverwrite, CK_ParagraphFormat, diff --git a/misc/mc.keymap.default b/misc/mc.keymap.default index 630c3767d..75f243099 100644 --- a/misc/mc.keymap.default +++ b/misc/mc.keymap.default @@ -340,6 +340,9 @@ OptionsSaveMode = LearnKeys = WindowMove = WindowResize = +WindowList = +WindowNext = +WindowPrev = ExtendedKeyMap = [viewer] diff --git a/misc/mc.keymap.emacs b/misc/mc.keymap.emacs index 1b57b6e7e..ad8995638 100644 --- a/misc/mc.keymap.emacs +++ b/misc/mc.keymap.emacs @@ -340,6 +340,9 @@ OptionsSaveMode = LearnKeys = WindowMove = WindowResize = +WindowList = +WindowNext = +WindowPrev = ExtendedKeyMap = ctrl-x [editor:xmap] diff --git a/src/editor/edit.c b/src/editor/edit.c index d73152b55..4c02132d1 100644 --- a/src/editor/edit.c +++ b/src/editor/edit.c @@ -3453,13 +3453,6 @@ edit_execute_key_command (WEdit * edit, unsigned long command, int char_for_inse void edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) { - /* at first, handle CK_Quit command */ - if (command == CK_Quit) - { - dlg_stop (((Widget *) edit)->owner); - return; - } - /* handle window state */ if (edit_handle_move_resize (edit, command)) return; diff --git a/src/editor/editmenu.c b/src/editor/editmenu.c index 24a46d3e6..c2cb00413 100644 --- a/src/editor/editmenu.c +++ b/src/editor/editmenu.c @@ -216,6 +216,10 @@ create_window_menu (void) entries = g_list_prepend (entries, menu_entry_create (_("&Move"), CK_WindowMove)); entries = g_list_prepend (entries, menu_entry_create (_("&Resize"), CK_WindowResize)); + entries = g_list_prepend (entries, menu_separator_create ()); + entries = g_list_prepend (entries, menu_entry_create (_("&Next"), CK_WindowNext)); + entries = g_list_prepend (entries, menu_entry_create (_("&Previous"), CK_WindowPrev)); + entries = g_list_prepend (entries, menu_entry_create (_("&List..."), CK_WindowList)); return g_list_reverse (entries); } diff --git a/src/editor/editwidget.c b/src/editor/editwidget.c index 5c3773af2..a027ac2ae 100644 --- a/src/editor/editwidget.c +++ b/src/editor/editwidget.c @@ -240,6 +240,64 @@ edit_window_resize (WEdit * edit, unsigned long command) /* --------------------------------------------------------------------------------------------- */ +static unsigned char +get_hotkey (int n) +{ + return (n <= 9) ? '0' + n : 'a' + n - 10; +} + +/* --------------------------------------------------------------------------------------------- */ + +static void +edit_window_list (const Dlg_head * h) +{ + const size_t offset = 2; /* skip menu and buttonbar */ + const size_t dlg_num = g_list_length (h->widgets) - offset; + int lines, cols; + Listbox *listbox; + GList *w; + int i = 0; + int rv; + + lines = min ((size_t) (LINES * 2 / 3), dlg_num); + cols = COLS * 2 / 3; + + listbox = create_listbox_window (lines, cols, _("Open files"), "[Open files]"); + + for (w = h->widgets; w != NULL; w = g_list_next (w)) + if (edit_widget_is_editor ((Widget *) w->data)) + { + WEdit *e = (WEdit *) w->data; + char *fname; + + if (e->filename_vpath == NULL) + fname = g_strdup_printf ("%c [%s]", e->modified ? '*' : ' ', _("NoName")); + else + { + char *fname2; + + fname2 = vfs_path_to_str (e->filename_vpath); + fname = g_strdup_printf ("%c%s", e->modified ? '*' : ' ', fname2); + g_free (fname2); + } + + listbox_add_item (listbox->list, LISTBOX_APPEND_AT_END, get_hotkey (i++), + str_term_trim (fname, listbox->list->widget.cols - 2), NULL); + g_free (fname); + } + + rv = g_list_position (h->widgets, h->current) - offset; + listbox_select_entry (listbox->list, rv); + rv = run_listbox (listbox); + if (rv >= 0) + { + w = g_list_nth (h->widgets, rv + offset); + dlg_set_top_widget (w->data); + } +} + +/* --------------------------------------------------------------------------------------------- */ + static char * edit_get_shortcut (unsigned long command) { @@ -534,6 +592,17 @@ edit_dialog_command_execute (Dlg_head * h, unsigned long command) if (edit_widget_is_editor ((Widget *) h->current->data)) edit_handle_move_resize ((WEdit *) h->current->data, command); break; + case CK_WindowList: + edit_window_list (h); + break; + case CK_WindowNext: + dlg_one_down (h); + dlg_set_top_widget (h->current->data); + break; + case CK_WindowPrev: + dlg_one_up (h); + dlg_set_top_widget (h->current->data); + break; case CK_OptionsSaveMode: edit_save_mode_cmd (); break;