[glade/wip/xjuan/gtk-application] GladeWindow: port to GtkApplication



commit 81c041938e46c735dce73d8c8d03460a68d45d7b
Author: Juan Pablo Ugarte <juanpablougarte gmail com>
Date:   Wed Aug 1 18:50:04 2018 -0300

    GladeWindow: port to GtkApplication

 src/glade-window.c |  26 +++-----
 src/glade-window.h |   9 ++-
 src/glade.glade    |   4 +-
 src/main.c         | 180 +++++++++++++++++++++++++----------------------------
 4 files changed, 102 insertions(+), 117 deletions(-)
---
diff --git a/src/glade-window.c b/src/glade-window.c
index 0f274b67..b7d4ce3c 100644
--- a/src/glade-window.c
+++ b/src/glade-window.c
@@ -103,8 +103,6 @@ struct _GladeWindowPrivate
   guint statusbar_menu_context_id;      /* The context id of the menu bar */
   guint statusbar_actions_context_id;   /* The context id of actions messages */
 
-  GActionGroup *actions;
-
   GtkRecentManager *recent_manager;
   GtkWidget *recent_menu;
 
@@ -131,7 +129,7 @@ struct _GladeWindowPrivate
 
 static void check_reload_project (GladeWindow *window, GladeProject *project);
 
-G_DEFINE_TYPE_WITH_PRIVATE (GladeWindow, glade_window, GTK_TYPE_WINDOW)
+G_DEFINE_TYPE_WITH_PRIVATE (GladeWindow, glade_window, GTK_TYPE_APPLICATION_WINDOW)
 
 static void
 refresh_title (GladeWindow *window)
@@ -408,7 +406,7 @@ project_targets_changed_cb (GladeProject *project, GladeWindow *window)
 static void
 actions_set_enabled (GladeWindow *window, const gchar *name, gboolean enabled)
 {
-  GAction *action = g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), name);
+  GAction *action = g_action_map_lookup_action (G_ACTION_MAP (window), name);
   g_simple_action_set_enabled (G_SIMPLE_ACTION (action), enabled);
 }
 
@@ -1540,7 +1538,7 @@ drag_data_received (GtkWidget *widget,
 static gboolean
 delete_event (GtkWindow *w, GdkEvent *event, GladeWindow *window)
 {
-  g_action_group_activate_action (window->priv->actions, "quit", NULL);
+  g_action_group_activate_action (G_ACTION_GROUP (window), "quit", NULL);
 
   /* return TRUE to stop other handlers */
   return TRUE;
@@ -2079,8 +2077,6 @@ on_quit_action_activate (GSimpleAction *action,
   glade_window_config_save (window);
 
   g_list_free (projects);
-
-  gtk_main_quit ();
 }
 
 static void
@@ -2148,10 +2144,9 @@ glade_window_init (GladeWindow *window)
 static void
 glade_window_action_handler (GladeWindow *window, const gchar *name)
 {
-  GladeWindowPrivate *priv = window->priv;
   GAction *action;
 
-  if ((action = g_action_map_lookup_action (G_ACTION_MAP (priv->actions), name)))
+  if ((action = g_action_map_lookup_action (G_ACTION_MAP (window), name)))
     g_action_activate (action, NULL);
 }
 
@@ -2221,12 +2216,12 @@ on_intro_show_node (GladeIntro  *intro,
   if (!g_strcmp0 (node, "new-project"))
     {
       /* Create two new project to make the project switcher visible */
-      g_action_group_activate_action (window->priv->actions, "new", NULL);
-      g_action_group_activate_action (window->priv->actions, "new", NULL);
+      g_action_group_activate_action (G_ACTION_GROUP (window), "new", NULL);
+      g_action_group_activate_action (G_ACTION_GROUP (window), "new", NULL);
     }
   else if (!g_strcmp0 (node, "add-project"))
     {
-      GAction *new_action = g_action_map_lookup_action (G_ACTION_MAP (priv->actions), "new");
+      GAction *new_action = g_action_map_lookup_action (G_ACTION_MAP (window), "new");
 
       g_signal_connect (new_action, "activate",
                         G_CALLBACK (on_user_new_action_activate),
@@ -2391,11 +2386,10 @@ glade_window_constructed (GObject *object)
 
   /* recent files */
   priv->recent_manager = gtk_recent_manager_get_default ();
-
+g_message ("%s", __func__);
   /* Setup Actions */
-  priv->actions = (GActionGroup *) g_simple_action_group_new ();
-  g_action_map_add_action_entries (G_ACTION_MAP (priv->actions), actions, G_N_ELEMENTS (actions), window);
-  gtk_widget_insert_action_group (GTK_WIDGET (window), "app", priv->actions);
+  g_action_map_add_action_entries (G_ACTION_MAP (window), actions, G_N_ELEMENTS (actions), window);
+  gtk_widget_insert_action_group (GTK_WIDGET (window), "app", G_ACTION_GROUP(window));
 
   /* status bar */
   priv->statusbar_context_id = gtk_statusbar_get_context_id (GTK_STATUSBAR (priv->statusbar), "general");
diff --git a/src/glade-window.h b/src/glade-window.h
index f72323da..cc5cce08 100644
--- a/src/glade-window.h
+++ b/src/glade-window.h
@@ -38,14 +38,13 @@ typedef struct _GladeWindowClass    GladeWindowClass;
 
 struct _GladeWindow
 {
-       GtkWindow parent_instance;
-       
-       GladeWindowPrivate *priv;
+  GtkApplicationWindow parent_instance;
+  GladeWindowPrivate *priv;
 };
 
 struct _GladeWindowClass
 {
-       GtkWindowClass parent_class;
+  GtkApplicationWindowClass parent_class;
 };
 
 GType       glade_window_get_type      (void) G_GNUC_CONST;
@@ -55,7 +54,7 @@ GtkWidget  *glade_window_new           (void);
 void        glade_window_new_project   (GladeWindow *window);
 
 gboolean    glade_window_open_project  (GladeWindow *window,
-                                       const gchar *path);
+                                        const gchar *path);
 
 void        glade_window_check_devhelp (GladeWindow *window);
 
diff --git a/src/glade.glade b/src/glade.glade
index d16431a2..4d2509ae 100644
--- a/src/glade.glade
+++ b/src/glade.glade
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<!-- Generated with glade 3.21.0 
+<!-- Generated with glade 3.22.0 
 
 Glade - A user interface designer for GTK+ and GNOME.
 Copyright (C) 2012-2017 Juan Pablo Ugarte
@@ -166,7 +166,7 @@ Author: Juan Pablo Ugarte
     <property name="show_numbers">True</property>
     <signal name="item-activated" handler="on_open_recent_action_item_activated" swapped="no"/>
   </object>
-  <template class="GladeWindow" parent="GtkWindow">
+  <template class="GladeWindow" parent="GtkApplicationWindow">
     <property name="can_focus">False</property>
     <child>
       <object class="GtkStack" id="stack">
diff --git a/src/main.c b/src/main.c
index 555b4375..cb9b05a2 100644
--- a/src/main.c
+++ b/src/main.c
@@ -18,6 +18,7 @@
  * Authors:
  *   Chema Celorio <chema celorio com>
  *   Vincent Geddes <vgeddes gnome org>
+ *   Juan Pablo Ugarte <juanpablougarte gmail com>
  */
 
 #include <config.h>
@@ -44,7 +45,7 @@
 
 /* Application arguments */
 static gboolean version = FALSE, without_devhelp = FALSE;
-static gchar **files = NULL;
+static gboolean verbose = FALSE;
 
 static GOptionEntry option_entries[] = {
   {"version", '\0', 0, G_OPTION_ARG_NONE, &version,
@@ -53,29 +54,14 @@ static GOptionEntry option_entries[] = {
   {"without-devhelp", '\0', 0, G_OPTION_ARG_NONE, &without_devhelp,
    N_("Disable Devhelp integration"), NULL},
 
-  {G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &files,
-   NULL, N_("[FILE...]")},
-
-  {NULL}
-};
-
-/* Debugging arguments */
-static gboolean verbose = FALSE;
-
-static GOptionEntry debug_option_entries[] = {
   {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, N_("be verbose"), NULL},
+
   {NULL}
 };
 
-int
-main (int argc, char *argv[])
+static void
+startup (GApplication *application)
 {
-  GladeWindow *window;
-  GOptionContext *option_context;
-  GOptionGroup *option_group;
-  GError *error = NULL;
-  GTimer *timer = NULL;
-
 #ifdef ENABLE_NLS
   setlocale (LC_ALL, "");
   bindtextdomain (GETTEXT_PACKAGE, glade_app_get_locale_dir ());
@@ -83,119 +69,125 @@ main (int argc, char *argv[])
   textdomain (GETTEXT_PACKAGE);
 #endif
 
-  /* Set up option groups */
-  option_context = g_option_context_new (NULL);
-
-  g_option_context_set_summary (option_context,
-                                N_("Create or edit user interface designs for GTK+ or GNOME applications."));
-  g_option_context_set_translation_domain (option_context, GETTEXT_PACKAGE);
-
-  option_group = g_option_group_new ("glade",
-                                     N_("Glade options"),
-                                     N_("Glade options"), NULL, NULL);
-  g_option_group_add_entries (option_group, option_entries);
-  g_option_context_set_main_group (option_context, option_group);
-  g_option_group_set_translation_domain (option_group, GETTEXT_PACKAGE);
-
-  option_group = g_option_group_new ("debug",
-                                     N_("Glade debug options"),
-                                     N_("Show Glade debug options"),
-                                     NULL, NULL);
-  g_option_group_add_entries (option_group, debug_option_entries);
-  g_option_group_set_translation_domain (option_group, GETTEXT_PACKAGE);
-  g_option_context_add_group (option_context, option_group);
-
-  /* Add Gtk option group */
-  g_option_context_add_group (option_context, gtk_get_option_group (FALSE));
-
-  /* Parse command line */
-  if (!g_option_context_parse (option_context, &argc, &argv, &error))
-    {
-      g_option_context_free (option_context);
+  glade_setup_log_handlers ();
 
-      if (error)
-        {
-          g_print ("%s\n", error->message);
-          g_error_free (error);
-        }
-      else
-        g_print ("An unknown error occurred\n");
+  g_set_application_name (APPLICATION_NAME);
 
-      return -1;
-    }
+  gtk_window_set_default_icon_name ("glade");
 
-  g_option_context_free (option_context);
-  option_context = NULL;
+}
+
+static void
+on_quit_activate (GAction      *action,
+                  GVariant     *parameter,
+                  GApplication *application)
+{
+  g_application_quit (application);
+}
+
+static void
+activate (GApplication *application)
+
+{
+  GladeWindow *window;
+  GAction *quit;
 
   if (version != FALSE)
     {
       /* Print version information and exit */
       g_print ("%s\n", PACKAGE_STRING);
-      return 0;
-    }
-
-  /* Pass NULL here since we parsed the gtk+ args already...
-   * from this point on we need a DISPLAY variable to be set.
-   */
-  gtk_init (NULL, NULL);
-
-  /* Check for gmodule support */
-  if (!g_module_supported ())
-    {
-      g_warning (_("gmodule support not found. gmodule support is required "
-                   "for glade to work"));
-      return -1;
+      g_application_quit (application);
+      return;
     }
 
-  g_set_application_name (APPLICATION_NAME);
-  gtk_window_set_default_icon_name ("glade");
-
-  glade_setup_log_handlers ();
-
   window = GLADE_WINDOW (glade_window_new ());
+  gtk_application_add_window (GTK_APPLICATION (application),
+                              GTK_WINDOW (window));
 
   if (without_devhelp == FALSE)
     glade_window_check_devhelp (window);
 
   gtk_widget_show (GTK_WIDGET (window));
 
-  /* Update UI before loading files */
-  while (gtk_events_pending ()) gtk_main_iteration ();
+  glade_window_registration_notify_user (window);
 
-  if (verbose) timer = g_timer_new ();
+
+  quit = g_action_map_lookup_action (G_ACTION_MAP (window), "quit");
+  g_signal_connect (quit, "activate", G_CALLBACK (on_quit_activate), application);
+}
+
+static void
+open (GApplication  *application,
+      GFile        **files,
+      gint           n_files,
+      const gchar   *hint)
+{
+  GTimer *timer = NULL;
+  GtkWindow *window;
+  gint i;
+
+  g_application_activate (application);
+
+  window = gtk_application_get_active_window (GTK_APPLICATION (application));
   
-  /* load files specified on commandline */
-  if (files != NULL)
+  if (verbose) timer = g_timer_new ();
+
+  for (i = 0; i < n_files; i++)
     {
-      guint i;
+      gchar *path = g_file_get_path (files[i]);
 
       for (i = 0; files[i]; ++i)
         {
           if (verbose) g_timer_start (timer);
-          
-          if (g_file_test (files[i], G_FILE_TEST_EXISTS) != FALSE)
-           glade_window_open_project (window, files[i]);
+
+          if (g_file_test (path, G_FILE_TEST_EXISTS) != FALSE)
+            glade_window_open_project (GLADE_WINDOW (window), path);
           else
             g_warning (_("Unable to open '%s', the file does not exist.\n"),
-                       files[i]);
+                       path);
 
           if (verbose)
             {
               g_timer_stop (timer);
-              g_message ("Loading '%s' took %lf seconds", files[i],
+              g_message ("Loading '%s' took %lf seconds", path,
                          g_timer_elapsed (timer, NULL));
             }
         }
-      g_strfreev (files);
+
+      g_free (path);
     }
 
   if (verbose) g_timer_destroy (timer);
+}
 
-  glade_window_registration_notify_user (window);
-  
-  gtk_main ();
+int
+main (int argc, char *argv[])
+{
+  GtkApplication *app;
+  int status;
+
+  /* Check for gmodule support */
+  if (!g_module_supported ())
+    {
+      g_warning (_("gmodule support not found. gmodule support is required "
+                   "for glade to work"));
+      return -1;
+    }
+
+  app = gtk_application_new ("org.gnome.Glade", G_APPLICATION_HANDLES_OPEN);
+
+  g_application_set_option_context_summary (G_APPLICATION (app),
+                                            N_("Create or edit user interface designs for GTK+ or GNOME 
applications."));
+  g_application_add_main_option_entries (G_APPLICATION (app), option_entries);
+
+  g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
+  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
+  g_signal_connect (app, "open", G_CALLBACK (open), NULL);
+
+  status = g_application_run (G_APPLICATION (app), argc, argv);
+  g_object_unref (app);
 
-  return 0;
+  return status;
 }
 
 #ifdef G_OS_WIN32


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