[latexila] Create MainWindowEdit, for the Edit menu



commit 678f6a8de2b98b4ecb0262f1f5ae44ab3b40caf7
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Tue Jul 31 16:51:58 2012 +0200

    Create MainWindowEdit, for the Edit menu
    
    Reduce the size of the MainWindow class.

 po/POTFILES.in            |    1 +
 src/main_window.vala      |  210 +++++------------------------------
 src/main_window_edit.vala |  268 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 296 insertions(+), 183 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index b47fe33..bd10f50 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -29,6 +29,7 @@ src/latex_menu.vala
 src/latex_post_processor.vala
 src/main.vala
 src/main_window_build_tools.vala
+src/main_window_edit.vala
 src/main_window_structure.vala
 src/main_window.vala
 src/menu_in_toolbar.vala
diff --git a/src/main_window.vala b/src/main_window.vala
index a99a98c..37153c8 100644
--- a/src/main_window.vala
+++ b/src/main_window.vala
@@ -46,37 +46,6 @@ public class MainWindow : Window
         { "FileQuit", Stock.QUIT, null, null,
             N_("Quit the program"), on_quit },
 
-        // Edit
-        { "Edit", null, N_("_Edit") },
-        { "EditUndo", Stock.UNDO, null, "<Control>Z",
-            N_("Undo the last action"), on_edit_undo },
-        { "EditRedo", Stock.REDO, null, "<Shift><Control>Z",
-            N_("Redo the last undone action"), on_edit_redo },
-        { "EditCut", Stock.CUT, null, null,
-            N_("Cut the selection"), on_edit_cut },
-        { "EditCopy", Stock.COPY, null, null,
-            N_("Copy the selection"), on_edit_copy },
-
-        // No shortcut here because if the shortcut is null, Ctrl+V is used for the _all_
-        // the window. In this case Ctrl+V in the search text entry would be broken (the
-        // text is pasted in the document instead of the entry).
-        // Anyway if we press Ctrl+V when the cursor is in the document, no problem.
-        { "EditPaste", Stock.PASTE, null, "",
-            N_("Paste the clipboard"), on_edit_paste },
-
-        { "EditDelete", Stock.DELETE, null, null,
-            N_("Delete the selected text"), on_edit_delete },
-        { "EditSelectAll", Stock.SELECT_ALL, null, "<Control>A",
-            N_("Select the entire document"), on_edit_select_all },
-        { "EditComment", null, N_("_Comment"), "<Control>M",
-            N_("Comment the selected lines (add the character \"%\")"),
-            on_edit_comment },
-        { "EditUncomment", null, N_("_Uncomment"), "<Shift><Control>M",
-            N_("Uncomment the selected lines (remove the character \"%\")"),
-            on_edit_uncomment },
-        { "EditPreferences", Stock.PREFERENCES, null, null,
-            N_("Configure the application"), on_open_preferences },
-
         // View
         { "View", null, N_("_View") },
         { "ViewZoomIn", Stock.ZOOM_IN, N_("Zoom _In"), "<Control>plus",
@@ -133,8 +102,6 @@ public class MainWindow : Window
 
     private const ToggleActionEntry[] toggle_action_entries =
     {
-        { "EditSpellChecking", Stock.SPELL_CHECK, null, "",
-            N_("Activate or disable the spell checking"), on_spell_checking },
         { "ViewMainToolbar", null, N_("_Main Toolbar"), null,
             N_("Show or hide the main toolbar"), null },
         // Translators: "Edit" here is an adjective.
@@ -164,6 +131,7 @@ public class MainWindow : Window
     private Gtk.ActionGroup documents_list_action_group;
     private uint documents_list_menu_ui_id;
 
+    private MainWindowEdit _main_window_edit;
     private MainWindowBuildTools _main_window_build_tools;
     private MainWindowStructure _main_window_structure;
 
@@ -244,6 +212,8 @@ public class MainWindow : Window
         goto_line = new GotoLine (this);
         search_and_replace = new SearchAndReplace (this);
 
+        _main_window_edit = new MainWindowEdit (this, ui_manager);
+
         // File browser
         FileBrowser file_browser = new FileBrowser (this);
 
@@ -344,8 +314,7 @@ public class MainWindow : Window
 
         documents_panel.switch_page.connect ((pg, page_num) =>
         {
-            set_undo_sensitivity ();
-            set_redo_sensitivity ();
+            _main_window_edit.update_sensitivity ();
             update_next_prev_doc_sensitivity ();
             _main_window_build_tools.update_sensitivity ();
             update_config_project_sensitivity ();
@@ -417,16 +386,6 @@ public class MainWindow : Window
             Gtk.drag_finish (dc, true, true, time);
         });
 
-        // spell checking
-        ToggleAction spell_checking_action =
-            action_group.get_action ("EditSpellChecking") as ToggleAction;
-
-        GLib.Settings editor_settings =
-            new GLib.Settings ("org.gnome.latexila.preferences.editor");
-
-        editor_settings.bind ("spell-checking", spell_checking_action, "active",
-            SettingsBindFlags.DEFAULT);
-
         /* packing widgets */
         Grid main_vgrid = new Grid ();
         main_vgrid.orientation = Orientation.VERTICAL;
@@ -702,24 +661,21 @@ public class MainWindow : Window
         /* sensitivity of undo and redo */
         tab.document.notify["can-undo"].connect (() =>
         {
-            if (tab != active_tab)
-                return;
-            set_undo_sensitivity ();
+            if (tab == active_tab)
+                _main_window_edit.update_sensitivity ();
         });
 
         tab.document.notify["can-redo"].connect (() =>
         {
-            if (tab != active_tab)
-                return;
-            set_redo_sensitivity ();
+            if (tab == active_tab)
+                _main_window_edit.update_sensitivity ();
         });
 
         /* sensitivity of cut/copy/delete */
         tab.document.notify["has-selection"].connect (() =>
         {
-            if (tab != active_tab)
-                return;
-            selection_changed ();
+            if (tab == active_tab)
+                _main_window_edit.update_sensitivity ();
         });
 
         tab.document.notify["location"].connect (() =>
@@ -743,9 +699,7 @@ public class MainWindow : Window
         // add the tab at the end of the notebook
         documents_panel.add_tab (tab, -1, jump_to);
 
-        set_undo_sensitivity ();
-        set_redo_sensitivity ();
-        selection_changed ();
+        _main_window_edit.update_sensitivity ();
 
         if (! this.get_visible ())
             this.present ();
@@ -1154,12 +1108,21 @@ public class MainWindow : Window
         // actions that must be insensitive if the notebook is empty
         string[] file_actions =
         {
-            "FileSave", "FileSaveAs", "FileClose", "EditUndo", "EditRedo", "EditCut",
-            "EditCopy", "EditPaste", "EditDelete", "EditSelectAll", "EditComment",
-            "EditUncomment", "ViewZoomIn", "ViewZoomOut", "ViewZoomReset",
-            "DocumentsSaveAll", "DocumentsCloseAll", "DocumentsPrevious", "DocumentsNext",
-            "SearchFind", "SearchReplace", "SearchGoToLine",
-            "ProjectsConfigCurrent", "FileCreateTemplate"
+            "FileSave",
+            "FileSaveAs",
+            "FileClose",
+            "ViewZoomIn",
+            "ViewZoomOut",
+            "ViewZoomReset",
+            "DocumentsSaveAll",
+            "DocumentsCloseAll",
+            "DocumentsPrevious",
+            "DocumentsNext",
+            "SearchFind",
+            "SearchReplace",
+            "SearchGoToLine",
+            "ProjectsConfigCurrent",
+            "FileCreateTemplate"
         };
 
         foreach (string file_action in file_actions)
@@ -1169,27 +1132,10 @@ public class MainWindow : Window
         }
 
         latex_action_group.set_sensitive (sensitive);
+        _main_window_edit.update_sensitivity ();
         _main_window_build_tools.update_sensitivity ();
     }
 
-    private void set_undo_sensitivity ()
-    {
-        if (active_tab != null)
-        {
-            Gtk.Action action = action_group.get_action ("EditUndo");
-            action.set_sensitive (active_document.can_undo);
-        }
-    }
-
-    private void set_redo_sensitivity ()
-    {
-        if (active_tab == null)
-            return;
-
-        Gtk.Action action = action_group.get_action ("EditRedo");
-        action.set_sensitive (active_document.can_redo);
-    }
-
     private void set_documents_move_to_new_window_sensitivity (bool sensitive)
     {
         Gtk.Action action = action_group.get_action ("DocumentsMoveToNewWindow");
@@ -1217,24 +1163,6 @@ public class MainWindow : Window
         action.set_sensitive (active_tab != null && active_document.project_id != -1);
     }
 
-    private void selection_changed ()
-    {
-        if (active_tab != null)
-        {
-            bool has_selection = active_document.has_selection;
-
-            // actions that must be insensitive if there is no selection
-            string[] selection_actions = { "EditCut", "EditCopy", "EditDelete" };
-
-            foreach (string selection_action in selection_actions)
-            {
-                Gtk.Action action = action_group.get_action (selection_action);
-                action.set_sensitive (has_selection);
-            }
-        }
-    }
-
-
     /*******************
      *    CALLBACKS
      ******************/
@@ -1346,90 +1274,6 @@ public class MainWindow : Window
         }
     }
 
-    /* Edit menu */
-
-    public void on_edit_undo ()
-    {
-        return_if_fail (active_tab != null);
-        if (active_document.can_undo)
-        {
-            active_document.undo ();
-            active_view.scroll_to_cursor ();
-            active_view.grab_focus ();
-        }
-    }
-
-    public void on_edit_redo ()
-    {
-        return_if_fail (active_tab != null);
-        if (active_document.can_redo)
-        {
-            active_document.redo ();
-            active_view.scroll_to_cursor ();
-            active_view.grab_focus ();
-        }
-    }
-
-    public void on_edit_cut ()
-    {
-        return_if_fail (active_tab != null);
-        active_view.cut_selection ();
-    }
-
-    public void on_edit_copy ()
-    {
-        return_if_fail (active_tab != null);
-        active_view.copy_selection ();
-    }
-
-    public void on_edit_paste ()
-    {
-        return_if_fail (active_tab != null);
-        active_view.my_paste_clipboard ();
-    }
-
-    public void on_edit_delete ()
-    {
-        return_if_fail (active_tab != null);
-        active_view.delete_selection ();
-    }
-
-    public void on_edit_select_all ()
-    {
-        return_if_fail (active_tab != null);
-        active_view.my_select_all ();
-    }
-
-    public void on_edit_comment ()
-    {
-        return_if_fail (active_tab != null);
-        active_document.comment_selected_lines ();
-    }
-
-    public void on_edit_uncomment ()
-    {
-        return_if_fail (active_tab != null);
-        active_document.uncomment_selected_lines ();
-    }
-
-    public void on_spell_checking (Gtk.Action action)
-    {
-        bool activate = (action as ToggleAction).active;
-
-        foreach (DocumentView view in get_views ())
-        {
-            if (activate)
-                view.activate_spell_checking ();
-            else
-                view.disable_spell_checking ();
-        }
-    }
-
-    public void on_open_preferences ()
-    {
-        PreferencesDialog.show_me (this);
-    }
-
     /* View */
 
     public void on_view_zoom_in ()
diff --git a/src/main_window_edit.vala b/src/main_window_edit.vala
new file mode 100644
index 0000000..3e5739b
--- /dev/null
+++ b/src/main_window_edit.vala
@@ -0,0 +1,268 @@
+/*
+ * This file is part of LaTeXila.
+ *
+ * Copyright  2012 SÃbastien Wilmet
+ *
+ * LaTeXila is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * LaTeXila is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: SÃbastien Wilmet
+ */
+
+using Gtk;
+
+// The Edit menu of a MainWindow
+
+public class MainWindowEdit
+{
+    private const Gtk.ActionEntry[] _action_entries =
+    {
+        { "Edit", null, N_("_Edit") },
+
+        { "EditUndo", Stock.UNDO, null, "<Control>Z",
+            N_("Undo the last action"), on_undo },
+
+        { "EditRedo", Stock.REDO, null, "<Shift><Control>Z",
+            N_("Redo the last undone action"), on_redo },
+
+        { "EditCut", Stock.CUT, null, null,
+            N_("Cut the selection"), on_cut },
+
+        { "EditCopy", Stock.COPY, null, null,
+            N_("Copy the selection"), on_copy },
+
+        // No shortcut here because if the shortcut is null, Ctrl+V is used for the _all_
+        // the window. In this case Ctrl+V in the search text entry would be broken (the
+        // text is pasted in the document instead of the entry).
+        // Anyway if we press Ctrl+V when the cursor is in the document, no problem.
+        { "EditPaste", Stock.PASTE, null, "",
+            N_("Paste the clipboard"), on_paste },
+
+        { "EditDelete", Stock.DELETE, null, null,
+            N_("Delete the selected text"), on_delete },
+
+        { "EditSelectAll", Stock.SELECT_ALL, null, "<Control>A",
+            N_("Select the entire document"), on_select_all },
+
+        { "EditComment", null, N_("_Comment"), "<Control>M",
+            N_("Comment the selected lines (add the character \"%\")"),
+            on_comment },
+
+        { "EditUncomment", null, N_("_Uncomment"), "<Shift><Control>M",
+            N_("Uncomment the selected lines (remove the character \"%\")"),
+            on_uncomment },
+
+        { "EditPreferences", Stock.PREFERENCES, null, null,
+            N_("Configure the application"), on_open_preferences }
+    };
+
+    private const ToggleActionEntry[] _toggle_action_entries =
+    {
+        { "EditSpellChecking", Stock.SPELL_CHECK, null, "",
+            N_("Activate or disable the spell checking"), on_spell_checking }
+    };
+
+    private unowned MainWindow _main_window;
+    private Gtk.ActionGroup _action_group;
+
+    public MainWindowEdit (MainWindow main_window, UIManager ui_manager)
+    {
+        _main_window = main_window;
+
+        _action_group = new Gtk.ActionGroup ("EditMenuActionGroup");
+        _action_group.set_translation_domain (Config.GETTEXT_PACKAGE);
+        _action_group.add_actions (_action_entries, this);
+        _action_group.add_toggle_actions (_toggle_action_entries, this);
+
+        ui_manager.insert_action_group (_action_group, 0);
+
+        /* Bind spell checking setting */
+
+        ToggleAction spell_checking_action =
+            _action_group.get_action ("EditSpellChecking") as ToggleAction;
+
+        GLib.Settings editor_settings =
+            new GLib.Settings ("org.gnome.latexila.preferences.editor");
+
+        editor_settings.bind ("spell-checking", spell_checking_action, "active",
+            SettingsBindFlags.DEFAULT);
+    }
+
+    /* Sensitivity */
+
+    public void update_sensitivity ()
+    {
+        bool sensitive = _main_window.active_tab != null;
+
+        set_edit_actions_sensitivity (sensitive);
+
+        if (sensitive)
+        {
+            set_has_selection_sensitivity ();
+            set_undo_sensitivity ();
+            set_redo_sensitivity ();
+        }
+    }
+
+    private void set_edit_actions_sensitivity (bool sensitive)
+    {
+        string[] action_names =
+        {
+            "EditUndo",
+            "EditRedo",
+            "EditCut",
+            "EditCopy",
+            "EditPaste",
+            "EditDelete",
+            "EditSelectAll",
+            "EditComment",
+            "EditUncomment"
+        };
+
+        foreach (string action_name in action_names)
+        {
+            Gtk.Action action = _action_group.get_action (action_name);
+            action.sensitive = sensitive;
+        }
+    }
+
+    private void set_has_selection_sensitivity ()
+    {
+        bool has_selection = false;
+
+        if (_main_window.active_tab != null)
+            has_selection = _main_window.active_document.has_selection;
+
+        // Actions that must be insensitive if there is no selection.
+        string[] action_names =
+        {
+            "EditCut",
+            "EditCopy",
+            "EditDelete"
+        };
+
+        foreach (string action_name in action_names)
+        {
+            Gtk.Action action = _action_group.get_action (action_name);
+            action.sensitive = has_selection;
+        }
+    }
+
+    private void set_undo_sensitivity ()
+    {
+        bool can_undo = false;
+
+        if (_main_window.active_tab != null)
+            can_undo = _main_window.active_document.can_undo;
+
+        Gtk.Action action = _action_group.get_action ("EditUndo");
+        action.sensitive = can_undo;
+    }
+
+    private void set_redo_sensitivity ()
+    {
+        bool can_redo = false;
+
+        if (_main_window.active_tab != null)
+            can_redo = _main_window.active_document.can_redo;
+
+        Gtk.Action action = _action_group.get_action ("EditRedo");
+        action.sensitive = can_redo;
+    }
+
+    /* Gtk.Action callbacks */
+
+    public void on_undo ()
+    {
+        return_if_fail (_main_window.active_tab != null);
+
+        if (_main_window.active_document.can_undo)
+        {
+            _main_window.active_document.undo ();
+            _main_window.active_view.scroll_to_cursor ();
+            _main_window.active_view.grab_focus ();
+        }
+    }
+
+    public void on_redo ()
+    {
+        return_if_fail (_main_window.active_tab != null);
+
+        if (_main_window.active_document.can_redo)
+        {
+            _main_window.active_document.redo ();
+            _main_window.active_view.scroll_to_cursor ();
+            _main_window.active_view.grab_focus ();
+        }
+    }
+
+    public void on_cut ()
+    {
+        return_if_fail (_main_window.active_tab != null);
+        _main_window.active_view.cut_selection ();
+    }
+
+    public void on_copy ()
+    {
+        return_if_fail (_main_window.active_tab != null);
+        _main_window.active_view.copy_selection ();
+    }
+
+    public void on_paste ()
+    {
+        return_if_fail (_main_window.active_tab != null);
+        _main_window.active_view.my_paste_clipboard ();
+    }
+
+    public void on_delete ()
+    {
+        return_if_fail (_main_window.active_tab != null);
+        _main_window.active_view.delete_selection ();
+    }
+
+    public void on_select_all ()
+    {
+        return_if_fail (_main_window.active_tab != null);
+        _main_window.active_view.my_select_all ();
+    }
+
+    public void on_comment ()
+    {
+        return_if_fail (_main_window.active_tab != null);
+        _main_window.active_document.comment_selected_lines ();
+    }
+
+    public void on_uncomment ()
+    {
+        return_if_fail (_main_window.active_tab != null);
+        _main_window.active_document.uncomment_selected_lines ();
+    }
+
+    public void on_spell_checking (Gtk.Action action)
+    {
+        bool activate = (action as ToggleAction).active;
+
+        foreach (DocumentView view in _main_window.get_views ())
+        {
+            if (activate)
+                view.activate_spell_checking ();
+            else
+                view.disable_spell_checking ();
+        }
+    }
+
+    public void on_open_preferences ()
+    {
+        PreferencesDialog.show_me (_main_window);
+    }
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]