Browse Source

Add CK_Close action to close current open file.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
Andrew Borodin 13 years ago
parent
commit
1c33972ee5

+ 1 - 0
lib/keybind.c

@@ -250,6 +250,7 @@ static name_keymap_t command_names[] = {
 #endif
 
 #ifdef USE_INTERNAL_EDIT
+    {"Close", CK_Close},
     {"Tab", CK_Tab},
     {"Undo", CK_Undo},
     {"ScrollUp", CK_ScrollUp},

+ 1 - 0
lib/keybind.h

@@ -234,6 +234,7 @@ enum
     CK_EditFile,
     CK_InsertFile,
     CK_EditSyntaxFile,
+    CK_Close,
     /* block commands */
     CK_BlockSave,
     CK_BlockShiftLeft,

+ 1 - 0
misc/mc.keymap.default

@@ -254,6 +254,7 @@ Save = f2
 EditFile =
 EditNew = ctrl-n
 SaveAs = f12; ctrl-f2
+Close =
 Mark = f3
 Copy = f5
 Move = f6

+ 1 - 0
misc/mc.keymap.emacs

@@ -254,6 +254,7 @@ ParagraphDown =
 Save = f2
 EditFile =
 SaveAs = f12; ctrl-f2
+Close =
 Mark = f3; ctrl-at
 Copy = f5
 Move = f6

+ 2 - 0
src/editor/edit-impl.h

@@ -178,6 +178,7 @@ extern gboolean search_create_bookmark;
 
 /*** declarations of public functions ************************************************************/
 
+WEdit *find_editor (const Dlg_head * h);
 gboolean edit_widget_is_editor (const Widget * w);
 gboolean edit_drop_hotkey_menu (Dlg_head * h, int key);
 void edit_menu_cmd (Dlg_head * h);
@@ -233,6 +234,7 @@ gboolean edit_ok_to_exit (WEdit * edit);
 gboolean edit_renew (WEdit * edit);
 gboolean edit_new_cmd (WEdit * edit);
 gboolean edit_load_cmd (WEdit * edit, edit_current_file_t what);
+gboolean edit_close_cmd (WEdit * edit);
 void edit_mark_cmd (WEdit * edit, int unmark);
 void edit_mark_current_word_cmd (WEdit * edit);
 void edit_mark_current_line_cmd (WEdit * edit);

+ 39 - 0
src/editor/editcmd.c

@@ -2138,6 +2138,45 @@ edit_load_cmd (WEdit * edit, edit_current_file_t what)
     return ret;
 }
 
+/* --------------------------------------------------------------------------------------------- */
+/**
+  * Close window with opened file.
+  *
+  * @returns TRUE if file was closed.
+  */
+
+gboolean
+edit_close_cmd (WEdit * edit)
+{
+    gboolean ret;
+
+    ret = (edit != NULL) && edit_ok_to_exit (edit);
+
+    if (ret)
+    {
+        Dlg_head *h = ((Widget *) edit)->owner;
+
+        if (edit->locked != 0)
+            unlock_file (edit->filename_vpath);
+
+        del_widget (edit);
+
+        if (edit_widget_is_editor ((Widget *) h->current->data))
+            edit = (WEdit *) h->current->data;
+        else
+        {
+            edit = find_editor (h);
+            if (edit != NULL)
+                dlg_set_top_widget (edit);
+        }
+    }
+
+    if (edit != NULL)
+        edit->force |= REDRAW_COMPLETELY;
+
+    return ret;
+}
+
 /* --------------------------------------------------------------------------------------------- */
 /**
    if mark2 is -1 then marking is from mark1 to the cursor.

+ 4 - 3
src/editor/editmenu.c

@@ -72,6 +72,7 @@ create_file_menu (void)
 
     entries = g_list_prepend (entries, menu_entry_create (_("&Open file..."), CK_EditFile));
     entries = g_list_prepend (entries, menu_entry_create (_("&New"), CK_EditNew));
+    entries = g_list_prepend (entries, menu_entry_create (_("&Close"), CK_Close));
     entries = g_list_prepend (entries, menu_separator_create ());
     entries = g_list_prepend (entries, menu_entry_create (_("&Save"), CK_Save));
     entries = g_list_prepend (entries, menu_entry_create (_("Save &as..."), CK_SaveAs));
@@ -213,10 +214,10 @@ create_window_menu (void)
 {
     GList *entries = NULL;
 
-    entries = g_list_append (entries, menu_entry_create (_("&Move"), CK_WindowMove));
-    entries = g_list_append (entries, menu_entry_create (_("&Resize"), CK_WindowResize));
+    entries = g_list_prepend (entries, menu_entry_create (_("&Move"), CK_WindowMove));
+    entries = g_list_prepend (entries, menu_entry_create (_("&Resize"), CK_WindowResize));
 
-    return entries;
+    return g_list_reverse (entries);
 }
 
 /* --------------------------------------------------------------------------------------------- */

+ 20 - 3
src/editor/editwidget.c

@@ -264,7 +264,7 @@ edit_get_shortcut (unsigned long command)
 static char *
 edit_get_title (const Dlg_head * h, size_t len)
 {
-    const WEdit *edit = (const WEdit *) find_widget_type (h, edit_callback);
+    const WEdit *edit = find_editor (h);
     const char *modified = edit->modified ? "(*) " : "    ";
     const char *file_label;
     char *filename;
@@ -482,6 +482,12 @@ edit_dialog_command_execute (Dlg_head * h, unsigned long command)
 
     switch (command)
     {
+    case CK_Close:
+        /* if there are no opened files anymore, close MC editor */
+        if (edit_widget_is_editor ((Widget *) h->current->data) &&
+            edit_close_cmd ((WEdit *) h->current->data) && find_editor (h) == NULL)
+            dlg_stop (h);
+        break;
     case CK_Help:
         edit_help ();
         /* edit->force |= REDRAW_COMPLETELY; */
@@ -555,13 +561,13 @@ edit_dialog_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, vo
     WMenuBar *menubar;
     WButtonBar *buttonbar;
 
-    edit = (WEdit *) find_widget_type (h, edit_callback);
     menubar = find_menubar (h);
     buttonbar = find_buttonbar (h);
 
     switch (msg)
     {
     case DLG_INIT:
+        edit = find_editor (h);
         edit_set_buttonbar (edit, buttonbar);
         return MSG_HANDLED;
 
@@ -632,7 +638,10 @@ edit_dialog_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, vo
 
     case DLG_VALIDATE:
         h->state = DLG_ACTIVE;  /* don't stop the dialog before final decision */
-        if (edit->drag_state != MCEDIT_DRAG_NORMAL)
+        edit = find_editor (h);
+        if (edit == NULL)
+            h->state = DLG_CLOSED;
+        else if (edit->drag_state != MCEDIT_DRAG_NORMAL)
             edit_restore_size (edit);
         else if (edit_ok_to_exit (edit))
             h->state = DLG_CLOSED;
@@ -780,6 +789,14 @@ edit_get_file_name (const WEdit * edit)
  * @return TRUE if widget is an WEdit class, FALSE otherwise
  */
 
+WEdit *
+find_editor (const Dlg_head * h)
+{
+    return (WEdit *) find_widget_type (h, edit_callback);
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
 gboolean
 edit_widget_is_editor (const Widget * w)
 {