[latexila] Create MainWindowFile, for the File menu



commit 231cec6e147221b29fe0915b144f9abd76522dce
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Tue Jul 31 21:33:52 2012 +0200

    Create MainWindowFile, for the File menu

 po/POTFILES.in            |    1 +
 src/main_window.vala      |  169 +++--------------------------------
 src/main_window_file.vala |  216 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 230 insertions(+), 156 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index bd10f50..5d8aa44 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -30,6 +30,7 @@ src/latex_post_processor.vala
 src/main.vala
 src/main_window_build_tools.vala
 src/main_window_edit.vala
+src/main_window_file.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 37153c8..ace884d 100644
--- a/src/main_window.vala
+++ b/src/main_window.vala
@@ -25,24 +25,6 @@ public class MainWindow : Window
     // name, stock_id, label, accelerator, tooltip, callback
     private const Gtk.ActionEntry[] action_entries =
     {
-        // File
-        { "File", null, N_("_File") },
-        { "FileNew", Stock.NEW, null, null,
-            N_("New file"), on_file_new },
-        { "FileNewWindow", null, N_("New _Window"), null,
-            N_("Create a new window"), on_new_window },
-        { "FileOpen", Stock.OPEN, null, null,
-            N_("Open a file"), on_file_open },
-        { "FileSave", Stock.SAVE, null, null,
-            N_("Save the current file"), on_file_save },
-        { "FileSaveAs", Stock.SAVE_AS, null, null,
-            N_("Save the current file with a different name"), on_file_save_as },
-        { "FileCreateTemplate", null, N_("Create _Template From Document..."), null,
-            N_("Create a new template from the current document"), on_create_template },
-        { "FileDeleteTemplate", null, N_("_Delete Template..."), null,
-            N_("Delete personal template(s)"), on_delete_template },
-        { "FileClose", Stock.CLOSE, null, null,
-            N_("Close the current file"), on_file_close },
         { "FileQuit", Stock.QUIT, null, null,
             N_("Quit the program"), on_quit },
 
@@ -113,7 +95,7 @@ public class MainWindow : Window
             N_("Show or hide the bottom panel"), null }
     };
 
-    private string file_chooser_current_folder = Environment.get_home_dir ();
+    public string default_location = Environment.get_home_dir ();
     private DocumentsPanel documents_panel;
     private CustomStatusbar statusbar;
     private GotoLine goto_line;
@@ -131,6 +113,7 @@ public class MainWindow : Window
     private Gtk.ActionGroup documents_list_action_group;
     private uint documents_list_menu_ui_id;
 
+    private MainWindowFile _main_window_file;
     private MainWindowEdit _main_window_edit;
     private MainWindowBuildTools _main_window_build_tools;
     private MainWindowStructure _main_window_structure;
@@ -213,6 +196,7 @@ public class MainWindow : Window
         search_and_replace = new SearchAndReplace (this);
 
         _main_window_edit = new MainWindowEdit (this, ui_manager);
+        _main_window_file = new MainWindowFile (this, ui_manager);
 
         // File browser
         FileBrowser file_browser = new FileBrowser (this);
@@ -254,10 +238,12 @@ public class MainWindow : Window
         Widget menu = ui_manager.get_widget ("/MainMenu");
 
         _main_toolbar = ui_manager.get_widget ("/MainToolbar") as Toolbar;
+        ToolItem open_button = _main_window_file.get_toolbar_open_button ();
+        _main_toolbar.insert (open_button, 1);
+
         _main_toolbar.set_style (ToolbarStyle.ICONS);
         StyleContext main_toolbar_context = _main_toolbar.get_style_context ();
         main_toolbar_context.add_class (Gtk.STYLE_CLASS_PRIMARY_TOOLBAR);
-        setup_toolbar_open_button (_main_toolbar);
 
         _edit_toolbar = ui_manager.get_widget ("/EditToolbar") as Toolbar;
         _edit_toolbar.set_style (ToolbarStyle.ICONS);
@@ -487,15 +473,9 @@ public class MainWindow : Window
 
     private void initialize_menubar_and_toolbar ()
     {
-        // recent documents
-        Gtk.Action recent_action = new RecentAction ("FileOpenRecent", _("Open _Recent"),
-            _("Open recently used files"), "");
-        configure_recent_chooser ((RecentChooser) recent_action);
-
         action_group = new Gtk.ActionGroup ("ActionGroup");
         action_group.set_translation_domain (Config.GETTEXT_PACKAGE);
         action_group.add_actions (action_entries, this);
-        action_group.add_action (recent_action);
         action_group.add_toggle_actions (toggle_action_entries, this);
 
         latex_action_group = new LatexMenu (this);
@@ -834,16 +814,16 @@ public class MainWindow : Window
         FileChooserDialog file_chooser = new FileChooserDialog (_("Save File"), this,
             FileChooserAction.SAVE,
             Stock.CANCEL, ResponseType.CANCEL,
-            Stock.SAVE, ResponseType.ACCEPT,
-            null);
+            Stock.SAVE, ResponseType.ACCEPT
+        );
 
         if (doc.location == null)
             file_chooser.set_current_name (doc.tab.label_text + ".tex");
         else
             file_chooser.set_current_name (doc.tab.label_text);
 
-        if (this.file_chooser_current_folder != null)
-            file_chooser.set_current_folder (this.file_chooser_current_folder);
+        if (this.default_location != null)
+            file_chooser.set_current_folder (this.default_location);
 
         if (doc.location != null)
         {
@@ -888,7 +868,7 @@ public class MainWindow : Window
             break;
         }
 
-        this.file_chooser_current_folder = file_chooser.get_current_folder ();
+        this.default_location = file_chooser.get_current_folder ();
         file_chooser.destroy ();
 
         if (doc.location != null)
@@ -949,39 +929,6 @@ public class MainWindow : Window
         statusbar.set_cursor_position (row + 1, col + 1);
     }
 
-    private void setup_toolbar_open_button (Toolbar toolbar)
-    {
-        RecentManager recent_manager = RecentManager.get_default ();
-        Widget toolbar_recent_menu = new RecentChooserMenu.for_manager (recent_manager);
-        configure_recent_chooser ((RecentChooser) toolbar_recent_menu);
-
-        MenuToolButton open_button = new MenuToolButton.from_stock (Stock.OPEN);
-        open_button.set_menu (toolbar_recent_menu);
-        open_button.set_tooltip_text (_("Open a file"));
-        open_button.set_arrow_tooltip_text (_("Open a recently used file"));
-
-        Gtk.Action action = action_group.get_action ("FileOpen");
-        open_button.set_related_action (action);
-
-        toolbar.insert (open_button, 1);
-    }
-
-    private void configure_recent_chooser (RecentChooser recent_chooser)
-    {
-        recent_chooser.set_local_only (false);
-        recent_chooser.set_sort_type (RecentSortType.MRU);
-
-        RecentFilter filter = new RecentFilter ();
-        filter.add_application (Config.APP_NAME);
-        recent_chooser.set_filter (filter);
-
-        recent_chooser.item_activated.connect ((chooser) =>
-        {
-            string uri = chooser.get_current_uri ();
-            open_document (File.new_for_uri (uri));
-        });
-    }
-
     public void save_state ()
     {
         /* state of the window */
@@ -1108,9 +1055,6 @@ public class MainWindow : Window
         // actions that must be insensitive if the notebook is empty
         string[] file_actions =
         {
-            "FileSave",
-            "FileSaveAs",
-            "FileClose",
             "ViewZoomIn",
             "ViewZoomOut",
             "ViewZoomReset",
@@ -1121,8 +1065,7 @@ public class MainWindow : Window
             "SearchFind",
             "SearchReplace",
             "SearchGoToLine",
-            "ProjectsConfigCurrent",
-            "FileCreateTemplate"
+            "ProjectsConfigCurrent"
         };
 
         foreach (string file_action in file_actions)
@@ -1132,6 +1075,7 @@ public class MainWindow : Window
         }
 
         latex_action_group.set_sensitive (sensitive);
+        _main_window_file.update_sensitivity (sensitive);
         _main_window_edit.update_sensitivity ();
         _main_window_build_tools.update_sensitivity ();
     }
@@ -1167,93 +1111,6 @@ public class MainWindow : Window
      *    CALLBACKS
      ******************/
 
-    /* File menu */
-
-    public void on_file_new ()
-    {
-        new OpenTemplateDialog (this);
-    }
-
-    public void on_new_window ()
-    {
-        Latexila.get_instance ().create_window ();
-    }
-
-    public void on_file_open ()
-    {
-        FileChooserDialog file_chooser = new FileChooserDialog (_("Open Files"), this,
-            FileChooserAction.OPEN,
-            Stock.CANCEL, ResponseType.CANCEL,
-            Stock.OPEN, ResponseType.ACCEPT,
-            null);
-
-        if (this.file_chooser_current_folder != null)
-            file_chooser.set_current_folder (this.file_chooser_current_folder);
-
-        file_chooser.select_multiple = true;
-
-        // Filter: by default show only .tex and .bib files
-        FileFilter latex_filter = new FileFilter ();
-        latex_filter.set_filter_name (_("All LaTeX Files"));
-        latex_filter.add_pattern ("*.tex");
-        latex_filter.add_pattern ("*.bib");
-        file_chooser.add_filter (latex_filter);
-
-        // All files filter
-        FileFilter all_files_filter = new FileFilter ();
-        all_files_filter.set_filter_name (_("All Files"));
-        all_files_filter.add_pattern ("*");
-        file_chooser.add_filter (all_files_filter);
-
-        SList<File> files_to_open = null;
-        if (file_chooser.run () == ResponseType.ACCEPT)
-            files_to_open = file_chooser.get_files ();
-
-        this.file_chooser_current_folder = file_chooser.get_current_folder ();
-        file_chooser.destroy ();
-
-        // We open the files after closing the dialog, because open a lot of documents can
-        // take some time (this is not async).
-        bool jump_to = true;
-        foreach (File file in files_to_open)
-        {
-            open_document (file, jump_to);
-            jump_to = false;
-        }
-    }
-
-    public void on_file_save ()
-    {
-        return_if_fail (active_tab != null);
-        save_document (active_document, false);
-    }
-
-    public void on_file_save_as ()
-    {
-        return_if_fail (active_tab != null);
-        save_document (active_document, true);
-    }
-
-    public void on_create_template ()
-    {
-        return_if_fail (active_tab != null);
-
-        CreateTemplateDialog dialog = new CreateTemplateDialog (this);
-        dialog.destroy ();
-    }
-
-    public void on_delete_template ()
-    {
-        DeleteTemplateDialog dialog = new DeleteTemplateDialog (this);
-        dialog.destroy ();
-    }
-
-    public void on_file_close ()
-    {
-        return_if_fail (active_tab != null);
-        close_tab (active_tab);
-    }
-
     public void on_quit ()
     {
         // save documents list
diff --git a/src/main_window_file.vala b/src/main_window_file.vala
new file mode 100644
index 0000000..879c7d0
--- /dev/null
+++ b/src/main_window_file.vala
@@ -0,0 +1,216 @@
+/*
+ * 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 File menu of a MainWindow
+
+public class MainWindowFile
+{
+    private const Gtk.ActionEntry[] _action_entries =
+    {
+        { "File", null, N_("_File") },
+
+        { "FileNew", Stock.NEW, null, null,
+            N_("New file"), on_file_new },
+
+        { "FileNewWindow", null, N_("New _Window"), null,
+            N_("Create a new window"), on_new_window },
+
+        { "FileOpen", Stock.OPEN, null, null,
+            N_("Open a file"), on_file_open },
+
+        { "FileSave", Stock.SAVE, null, null,
+            N_("Save the current file"), on_file_save },
+
+        { "FileSaveAs", Stock.SAVE_AS, null, null,
+            N_("Save the current file with a different name"), on_file_save_as },
+
+        { "FileCreateTemplate", null, N_("Create _Template From Document..."), null,
+            N_("Create a new template from the current document"), on_create_template },
+
+        { "FileDeleteTemplate", null, N_("_Delete Template..."), null,
+            N_("Delete personal template(s)"), on_delete_template },
+
+        { "FileClose", Stock.CLOSE, null, null,
+            N_("Close the current file"), on_file_close }
+    };
+
+    private unowned MainWindow _main_window;
+    private Gtk.ActionGroup _action_group;
+
+    public MainWindowFile (MainWindow main_window, UIManager ui_manager)
+    {
+        _main_window = main_window;
+
+        _action_group = new Gtk.ActionGroup ("FileMenuActionGroup");
+        _action_group.set_translation_domain (Config.GETTEXT_PACKAGE);
+        _action_group.add_actions (_action_entries, this);
+
+        // recent documents
+        Gtk.Action recent_action = new RecentAction ("FileOpenRecent", _("Open _Recent"),
+            _("Open recently used files"), "");
+        configure_recent_chooser (recent_action as RecentChooser);
+        _action_group.add_action (recent_action);
+
+        ui_manager.insert_action_group (_action_group, 0);
+    }
+
+    public ToolItem get_toolbar_open_button ()
+    {
+        RecentManager recent_manager = RecentManager.get_default ();
+        Widget recent_menu = new RecentChooserMenu.for_manager (recent_manager);
+        configure_recent_chooser (recent_menu as RecentChooser);
+
+        MenuToolButton open_button = new MenuToolButton.from_stock (Stock.OPEN);
+        open_button.set_menu (recent_menu);
+        open_button.set_tooltip_text (_("Open a file"));
+        open_button.set_arrow_tooltip_text (_("Open a recently used file"));
+
+        Gtk.Action action = _action_group.get_action ("FileOpen");
+        open_button.set_related_action (action);
+
+        return open_button;
+    }
+
+    private void configure_recent_chooser (RecentChooser recent_chooser)
+    {
+        recent_chooser.set_local_only (false);
+        recent_chooser.set_sort_type (RecentSortType.MRU);
+
+        RecentFilter filter = new RecentFilter ();
+        filter.add_application (Config.APP_NAME);
+        recent_chooser.set_filter (filter);
+
+        recent_chooser.item_activated.connect ((chooser) =>
+        {
+            string uri = chooser.get_current_uri ();
+            _main_window.open_document (File.new_for_uri (uri));
+        });
+    }
+
+    /* Sensitivity */
+
+    public void update_sensitivity (bool sensitive)
+    {
+        string[] action_names =
+        {
+            "FileSave",
+            "FileSaveAs",
+            "FileClose",
+            "FileCreateTemplate"
+        };
+
+        foreach (string action_name in action_names)
+        {
+            Gtk.Action action = _action_group.get_action (action_name);
+            action.sensitive = sensitive;
+        }
+    }
+
+    /* Gtk.Action callbacks */
+
+    public void on_file_new ()
+    {
+        new OpenTemplateDialog (_main_window);
+    }
+
+    public void on_new_window ()
+    {
+        Latexila.get_instance ().create_window ();
+    }
+
+    public void on_file_open ()
+    {
+        FileChooserDialog file_chooser = new FileChooserDialog (_("Open Files"),
+            _main_window,
+            FileChooserAction.OPEN,
+            Stock.CANCEL, ResponseType.CANCEL,
+            Stock.OPEN, ResponseType.ACCEPT
+        );
+
+        if (_main_window.default_location != null)
+            file_chooser.set_current_folder (_main_window.default_location);
+
+        file_chooser.select_multiple = true;
+
+        // Filter: by default show only .tex and .bib files
+        FileFilter latex_filter = new FileFilter ();
+        latex_filter.set_filter_name (_("All LaTeX Files"));
+        latex_filter.add_pattern ("*.tex");
+        latex_filter.add_pattern ("*.bib");
+        file_chooser.add_filter (latex_filter);
+
+        // All files filter
+        FileFilter all_files_filter = new FileFilter ();
+        all_files_filter.set_filter_name (_("All Files"));
+        all_files_filter.add_pattern ("*");
+        file_chooser.add_filter (all_files_filter);
+
+        SList<File> files_to_open = null;
+        if (file_chooser.run () == ResponseType.ACCEPT)
+            files_to_open = file_chooser.get_files ();
+
+        _main_window.default_location = file_chooser.get_current_folder ();
+        file_chooser.destroy ();
+
+        // We open the files after closing the dialog, because open a lot of documents can
+        // take some time (this is not async).
+        bool jump_to = true;
+        foreach (File file in files_to_open)
+        {
+            _main_window.open_document (file, jump_to);
+            jump_to = false;
+        }
+    }
+
+    public void on_file_save ()
+    {
+        return_if_fail (_main_window.active_tab != null);
+        _main_window.save_document (_main_window.active_document, false);
+    }
+
+    public void on_file_save_as ()
+    {
+        return_if_fail (_main_window.active_tab != null);
+        _main_window.save_document (_main_window.active_document, true);
+    }
+
+    public void on_create_template ()
+    {
+        return_if_fail (_main_window.active_tab != null);
+
+        CreateTemplateDialog dialog = new CreateTemplateDialog (_main_window);
+        dialog.destroy ();
+    }
+
+    public void on_delete_template ()
+    {
+        DeleteTemplateDialog dialog = new DeleteTemplateDialog (_main_window);
+        dialog.destroy ();
+    }
+
+    public void on_file_close ()
+    {
+        return_if_fail (_main_window.active_tab != null);
+        _main_window.close_tab (_main_window.active_tab);
+    }
+}



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