[latexila/gnome-3] Use GtkApplication



commit 6e732e86193118957dbf08ed961111a70c7d98e9
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Mon Apr 2 19:20:52 2012 +0200

    Use GtkApplication

 CMakeLists.txt        |    5 +--
 INSTALL               |    7 ++--
 src/app_settings.vala |   18 +++++-----
 src/build_tools.vala  |    7 +++-
 src/completion.vala   |    4 +-
 src/document.vala     |    2 +-
 src/latexila.vala     |   82 +++++++++++++++++++++++++++++++------------------
 src/main.vala         |   35 ++------------------
 src/main_window.vala  |   12 ++++---
 src/projects.vala     |   15 +++++---
 10 files changed, 94 insertions(+), 93 deletions(-)
---
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9f91bbd..4706497 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -47,12 +47,11 @@ if (BUILD_VALA)
 endif ()
 
 find_package (PkgConfig)
-pkg_check_modules (GTK REQUIRED "gtk+-3.0 >= 3.2")
+pkg_check_modules (GTK REQUIRED "gtk+-3.0 >= 3.4")
 pkg_check_modules (GTKSOURCEVIEW REQUIRED "gtksourceview-3.0")
 #pkg_check_modules (GTKSPELL REQUIRED "gtkspell-2.0")
 pkg_check_modules (GEE REQUIRED "gee-1.0")
-pkg_check_modules (GIO REQUIRED "gio-2.0 >= 2.30")
-#pkg_check_modules (UNIQUE REQUIRED "unique-1.0")
+pkg_check_modules (GIO REQUIRED "gio-2.0 >= 2.32")
 pkg_check_modules (GDK-X11 REQUIRED "gdk-x11-3.0")
 pkg_check_modules (X11 REQUIRED "x11")
 
diff --git a/INSTALL b/INSTALL
index 8127c7c..24d3127 100644
--- a/INSTALL
+++ b/INSTALL
@@ -1,11 +1,10 @@
 Requirements
 ============
 
-GTK+ >= 2.16
-GtkSourceView >= 2.10
-GLib >= 2.30
+GTK+ >= 3.4
+GtkSourceView >= 3.0
+GLib >= 2.32
 libgee
-unique
 GtkSpell
 gdk-x11
 x11
diff --git a/src/app_settings.vala b/src/app_settings.vala
index 05b3c46..99c4dcc 100644
--- a/src/app_settings.vala
+++ b/src/app_settings.vala
@@ -84,7 +84,7 @@ public class AppSettings : GLib.Settings
                 Gtk.SourceStyleSchemeManager.get_default ();
             Gtk.SourceStyleScheme scheme = manager.get_scheme (scheme_id);
 
-            foreach (Document doc in Latexila.get_default ().get_documents ())
+            foreach (Document doc in Latexila.get_instance ().get_documents ())
                 doc.style_scheme = scheme;
 
             // we don't use doc.set_style_scheme_from_string() for performance reason
@@ -95,7 +95,7 @@ public class AppSettings : GLib.Settings
             uint val;
             setting.get (key, "u", out val);
 
-            foreach (DocumentView view in Latexila.get_default ().get_views ())
+            foreach (DocumentView view in Latexila.get_instance ().get_views ())
                 view.tab_width = val;
         });
 
@@ -103,7 +103,7 @@ public class AppSettings : GLib.Settings
         {
             bool val = setting.get_boolean (key);
 
-            foreach (DocumentView view in Latexila.get_default ().get_views ())
+            foreach (DocumentView view in Latexila.get_instance ().get_views ())
                 view.insert_spaces_instead_of_tabs = val;
         });
 
@@ -111,7 +111,7 @@ public class AppSettings : GLib.Settings
         {
             bool val = setting.get_boolean (key);
 
-            foreach (DocumentView view in Latexila.get_default ().get_views ())
+            foreach (DocumentView view in Latexila.get_instance ().get_views ())
                 view.show_line_numbers = val;
         });
 
@@ -119,7 +119,7 @@ public class AppSettings : GLib.Settings
         {
             bool val = setting.get_boolean (key);
 
-            foreach (DocumentView view in Latexila.get_default ().get_views ())
+            foreach (DocumentView view in Latexila.get_instance ().get_views ())
                 view.highlight_current_line = val;
         });
 
@@ -127,7 +127,7 @@ public class AppSettings : GLib.Settings
         {
             bool val = setting.get_boolean (key);
 
-            foreach (Document doc in Latexila.get_default ().get_documents ())
+            foreach (Document doc in Latexila.get_instance ().get_documents ())
                 doc.highlight_matching_brackets = val;
         });
 
@@ -135,7 +135,7 @@ public class AppSettings : GLib.Settings
         {
             bool val = setting.get_boolean (key);
 
-            foreach (Document doc in Latexila.get_default ().get_documents ())
+            foreach (Document doc in Latexila.get_instance ().get_documents ())
                 doc.tab.auto_save = val;
         });
 
@@ -144,7 +144,7 @@ public class AppSettings : GLib.Settings
             uint val;
             setting.get (key, "u", out val);
 
-            foreach (Document doc in Latexila.get_default ().get_documents ())
+            foreach (Document doc in Latexila.get_instance ().get_documents ())
                 doc.tab.auto_save_interval = val;
         });
 
@@ -163,7 +163,7 @@ public class AppSettings : GLib.Settings
 
     private void set_font (string font)
     {
-        foreach (DocumentView view in Latexila.get_default ().get_views ())
+        foreach (DocumentView view in Latexila.get_instance ().get_views ())
             view.set_font_from_string (font);
     }
 }
diff --git a/src/build_tools.vala b/src/build_tools.vala
index ecc7094..b494fd7 100644
--- a/src/build_tools.vala
+++ b/src/build_tools.vala
@@ -231,8 +231,11 @@ public class BuildTools
     private void update_all_menus ()
     {
         _modified = true;
-        foreach (MainWindow window in Latexila.get_default ().get_windows ())
-            window.update_build_tools_menu ();
+        foreach (Gtk.Window window in Latexila.get_instance ().get_windows ())
+        {
+            MainWindow main_window = window as MainWindow;
+            main_window.update_build_tools_menu ();
+        }
     }
 
     private bool is_compilation (string icon)
diff --git a/src/completion.vala b/src/completion.vala
index d487f89..a8ce4cd 100644
--- a/src/completion.vala
+++ b/src/completion.vala
@@ -306,7 +306,7 @@ public class CompletionProvider : GLib.Object, SourceCompletionProvider
 
     private void init_calltip_window ()
     {
-        Latexila app = Latexila.get_default ();
+        Latexila app = Latexila.get_instance ();
         _calltip_window = new SourceCompletionInfo ();
         _calltip_window.set_transient_for (app.active_window);
 //        _calltip_window.set_sizing (800, 200, true, true);
@@ -336,7 +336,7 @@ public class CompletionProvider : GLib.Object, SourceCompletionProvider
 
         _calltip_window_label.set_markup (markup);
 
-        MainWindow window = Latexila.get_default ().active_window;
+        MainWindow window = Latexila.get_instance ().active_window;
         _calltip_window.set_transient_for (window);
 
         // Calltip at a fixed place (after the '{' or '[' of the current argument).
diff --git a/src/document.vala b/src/document.vala
index 16a2485..2b8e657 100644
--- a/src/document.vala
+++ b/src/document.vala
@@ -328,7 +328,7 @@ public class Document : Gtk.SourceBuffer
 
         // get all unsaved document numbers
         uint[] all_nums = {};
-        foreach (Document doc in Latexila.get_default ().get_documents ())
+        foreach (Document doc in Latexila.get_instance ().get_documents ())
         {
             // avoid infinite loop
             if (doc == this)
diff --git a/src/latexila.vala b/src/latexila.vala
index ff5a6cb..cbabb90 100644
--- a/src/latexila.vala
+++ b/src/latexila.vala
@@ -19,26 +19,36 @@
  * Author: SÃbastien Wilmet
  */
 
-public class Latexila : GLib.Object
+public class Latexila : Gtk.Application
 {
     private static Latexila _instance = null;
-    private Gee.List<MainWindow> _windows;
 
     public MainWindow active_window { get; private set; }
 
-    // Latexila is a singleton.
     private Latexila ()
     {
-        _windows = new Gee.LinkedList<MainWindow> ();
+        Object (application_id: "org.gnome.latexila");
+        Environment.set_application_name ("LaTeXila");
 
-        set_application_icons ();
-        StockIcons.add_custom ();
+        startup.connect (init_primary_instance);
+        activate.connect (() => active_window.present ());
 
-        AppSettings.get_default ();
-        create_window ();
+        shutdown.connect (() =>
+        {
+            Projects.get_default ().save ();
+            BuildTools.get_default ().save ();
+            MostUsedSymbols.get_default ().save ();
+        });
+
+        window_removed.connect (() =>
+        {
+            unowned List<weak Gtk.Window> windows = get_windows ();
+            if (0 < windows.length ())
+                active_window = windows.data as MainWindow;
+        });
     }
 
-    public static Latexila get_default ()
+    public static Latexila get_instance ()
     {
         if (_instance == null)
             _instance = new Latexila ();
@@ -46,6 +56,16 @@ public class Latexila : GLib.Object
         return _instance;
     }
 
+    private void init_primary_instance ()
+    {
+        set_application_icons ();
+        StockIcons.add_custom ();
+
+        AppSettings.get_default ();
+        create_window ();
+        reopen_files ();
+    }
+
     private void set_application_icons ()
     {
         string[] sizes = {"16x16", "22x22", "24x24", "32x32", "48x48"};
@@ -70,17 +90,30 @@ public class Latexila : GLib.Object
         Gtk.Window.set_default_icon_list (list);
     }
 
-    public Gee.List<MainWindow> get_windows ()
+    private void reopen_files ()
     {
-        return _windows;
+        GLib.Settings editor_settings =
+            new GLib.Settings ("org.gnome.latexila.preferences.editor");
+
+        if (editor_settings.get_boolean ("reopen-files"))
+        {
+            GLib.Settings window_settings =
+                new GLib.Settings ("org.gnome.latexila.state.window");
+
+            string[] uris = window_settings.get_strv ("documents");
+            open_documents (uris);
+        }
     }
 
     // Get all the documents currently opened.
     public Gee.List<Document> get_documents ()
     {
         Gee.List<Document> all_documents = new Gee.LinkedList<Document> ();
-        foreach (MainWindow window in _windows)
-            all_documents.add_all (window.get_documents ());
+        foreach (Gtk.Window window in get_windows ())
+        {
+            MainWindow main_window = window as MainWindow;
+            all_documents.add_all (main_window.get_documents ());
+        }
 
         return all_documents;
     }
@@ -89,8 +122,11 @@ public class Latexila : GLib.Object
     public Gee.List<DocumentView> get_views ()
     {
         Gee.List<DocumentView> all_views = new Gee.LinkedList<DocumentView> ();
-        foreach (MainWindow window in _windows)
-            all_views.add_all (window.get_views ());
+        foreach (Gtk.Window window in get_windows ())
+        {
+            MainWindow main_window = window as MainWindow;
+            all_views.add_all (main_window.get_views ());
+        }
 
         return all_views;
     }
@@ -101,26 +137,12 @@ public class Latexila : GLib.Object
             active_window.save_state ();
 
         MainWindow window = new MainWindow ();
-        _windows.add (window);
+        add_window (window);
         active_window = window;
 
         if (screen != null)
             window.set_screen (screen);
 
-        window.destroy.connect (() =>
-        {
-            _windows.remove (window);
-            if (_windows.size == 0)
-            {
-                Projects.get_default ().save ();
-                BuildTools.get_default ().save ();
-                MostUsedSymbols.get_default ().save ();
-                Gtk.main_quit ();
-            }
-            else if (window == active_window)
-                active_window = _windows.first ();
-        });
-
         window.focus_in_event.connect (() =>
         {
             active_window = window;
diff --git a/src/main.vala b/src/main.vala
index 4dd9ff3..1f066b6 100644
--- a/src/main.vala
+++ b/src/main.vala
@@ -15,6 +15,8 @@
  *
  * 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;
@@ -98,40 +100,11 @@ private void parse_cmd_line_options (string[] args)
     }
 }
 
-private void reopen_files ()
-{
-    Latexila latexila = Latexila.get_default ();
-
-    GLib.Settings editor_settings =
-        new GLib.Settings ("org.gnome.latexila.preferences.editor");
-    if (editor_settings.get_boolean ("reopen-files"))
-    {
-        GLib.Settings window_settings =
-            new GLib.Settings ("org.gnome.latexila.state.window");
-
-        string[] uris = window_settings.get_strv ("documents");
-        latexila.open_documents (uris);
-    }
-}
-
 int main (string[] args)
 {
     check_xdg_data_dirs ();
     init_i18n ();
 
-    Gtk.init (ref args);
-
-    parse_cmd_line_options (args);
-
-    // Create the application instance
-    Latexila latexila = Latexila.get_default ();
-
-    reopen_files ();
-
-    if (_option_new_document)
-        latexila.create_document ();
-
-    Gtk.main ();
-
-    return 0;
+    Latexila app = Latexila.get_instance ();
+    return app.run (args);
 }
diff --git a/src/main_window.vala b/src/main_window.vala
index ab496c8..236d25d 100644
--- a/src/main_window.vala
+++ b/src/main_window.vala
@@ -432,7 +432,7 @@ public class MainWindow : Window
         Gtk.drag_dest_add_uri_targets (this);
         drag_data_received.connect ((dc, x, y, selection_data, info, time) =>
         {
-            Latexila app = Latexila.get_default ();
+            Latexila app = Latexila.get_instance ();
             app.open_documents (selection_data.get_uris ());
             Gtk.drag_finish (dc, true, true, time);
         });
@@ -696,8 +696,10 @@ public class MainWindow : Window
     public DocumentTab? open_document (File location, bool jump_to = true)
     {
         /* check if the document is already opened */
-        foreach (MainWindow w in Latexila.get_default ().get_windows ())
+        foreach (Window window in Latexila.get_instance ().get_windows ())
         {
+            MainWindow w = window as MainWindow;
+
             foreach (Document doc in w.get_documents ())
             {
                 if (doc.location == null || ! location.equal (doc.location))
@@ -1165,7 +1167,7 @@ public class MainWindow : Window
 
     private void move_tab_to_new_window (DocumentTab tab)
     {
-        MainWindow new_window = Latexila.get_default ().create_window ();
+        MainWindow new_window = Latexila.get_instance ().create_window ();
         DocumentView view = tab.view;
         documents_panel.remove_tab (tab);
 
@@ -1257,7 +1259,7 @@ public class MainWindow : Window
             // save all the documents belonging to the project
             else
             {
-                Gee.List<Document> docs = Latexila.get_default ().get_documents ();
+                Gee.List<Document> docs = Latexila.get_instance ().get_documents ();
                 foreach (Document doc in docs)
                 {
                     if (doc.project_id == project_id)
@@ -1502,7 +1504,7 @@ public class MainWindow : Window
 
     public void on_new_window ()
     {
-        Latexila.get_default ().create_window ();
+        Latexila.get_instance ().create_window ();
     }
 
     public void on_file_open ()
diff --git a/src/projects.vala b/src/projects.vala
index e6895a0..190c0a7 100644
--- a/src/projects.vala
+++ b/src/projects.vala
@@ -80,8 +80,11 @@ public class Projects
 
     private void update_all_menus ()
     {
-        foreach (MainWindow window in Latexila.get_default ().get_windows ())
-            window.update_config_project_sensitivity ();
+        foreach (Gtk.Window window in Latexila.get_instance ().get_windows ())
+        {
+            MainWindow main_window = window as MainWindow;
+            main_window.update_config_project_sensitivity ();
+        }
     }
 
     // returns true if project successfully added
@@ -102,7 +105,7 @@ public class Projects
         modified = true;
 
         // find if some opened documents are belonging to the new project
-        Gee.List<Document> docs = Latexila.get_default ().get_documents ();
+        Gee.List<Document> docs = Latexila.get_instance ().get_documents ();
         foreach (Document doc in docs)
         {
             if (doc.project_id != -1 || doc.location == null)
@@ -133,7 +136,7 @@ public class Projects
         modified = true;
 
         // refresh docs
-        Gee.List<Document> docs = Latexila.get_default ().get_documents ();
+        Gee.List<Document> docs = Latexila.get_instance ().get_documents ();
         foreach (Document doc in docs)
         {
             if (doc.project_id == num)
@@ -150,7 +153,7 @@ public class Projects
         modified = true;
 
         // refresh docs
-        Gee.List<Document> docs = Latexila.get_default ().get_documents ();
+        Gee.List<Document> docs = Latexila.get_instance ().get_documents ();
         foreach (Document doc in docs)
         {
             if (doc.project_id == num)
@@ -172,7 +175,7 @@ public class Projects
 
     private void update_all_documents ()
     {
-        Gee.List<Document> docs = Latexila.get_default ().get_documents ();
+        Gee.List<Document> docs = Latexila.get_instance ().get_documents ();
         foreach (Document doc in docs)
         {
             doc.project_id = -1;



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