[gtk+/wip/gapplication] Rework the icon and title setting to set defaults



commit 4bee126f8e45052defd4c1aa24ea3794d2d3caa4
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Jun 7 14:25:23 2010 -0400

    Rework the icon and title setting to set defaults
    
    So that they apply do all windows, but don't override explicitly
    set icons/titles on added windows.
    
    Also add some extra complications to testapplication to excercise
    multiple windows and lauching from a desktop file.

 gtk/gtkapplication.c    |   70 +++++++++++++++++++++++++++-------------------
 tests/testapplication.c |   34 +++++++++++++++++++---
 2 files changed, 70 insertions(+), 34 deletions(-)
---
diff --git a/gtk/gtkapplication.c b/gtk/gtkapplication.c
index 1efe613..962365b 100644
--- a/gtk/gtkapplication.c
+++ b/gtk/gtkapplication.c
@@ -300,6 +300,8 @@ gtk_application_on_window_destroy (GtkWidget *window,
   return FALSE;
 }
 
+static gchar *default_title;
+
 /**
  * gtk_application_add_window:
  * @app: a #GtkApplication
@@ -321,9 +323,8 @@ gtk_application_add_window (GtkApplication *app,
 {
   app->priv->windows = g_slist_prepend (app->priv->windows, window);
 
-  /* TODO: maybe set title and icon here too, but only if the window
-   * doesn't have them set yet.
-   */
+  if (gtk_window_get_title (window) == NULL && default_title != NULL)
+    gtk_window_set_title (window, default_title);
 
   g_signal_connect (window, "destroy",
                     G_CALLBACK (gtk_application_on_window_destroy), app);
@@ -354,38 +355,12 @@ gtk_application_add_window (GtkApplication *app,
 GtkWindow *
 gtk_application_get_window (GtkApplication *app)
 {
-  const gchar *pid;
-  const gchar *filename;
-  GKeyFile *keyfile;
-  gchar *name;
-  gchar *icon;
-
   if (app->priv->default_window != NULL)
     return app->priv->default_window;
 
   app->priv->default_window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
   g_object_ref_sink (app->priv->default_window);
 
-  pid = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE_PID");
-  filename = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
-
-  keyfile = g_key_file_new ();
-
-  if (pid != NULL && filename != NULL && atoi (pid) == getpid () &&
-      g_key_file_load_from_file (keyfile, filename, 0, NULL))
-    {
-      name = g_key_file_get_locale_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
-      icon = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL);
-
-      gtk_window_set_title (app->priv->default_window, name);
-      gtk_window_set_icon_name (app->priv->default_window, icon);
-
-      g_free (name);
-      g_free (icon);
-    }
-
-  g_key_file_free (keyfile);
-
   gtk_application_add_window (app, app->priv->default_window);
 
   return app->priv->default_window;
@@ -456,13 +431,50 @@ gtk_application_set_property (GObject      *object,
     }
 }
 
+static void
+setup_default_window_decorations (void)
+{
+  const gchar *pid;
+  const gchar *filename;
+  GKeyFile *keyfile;
+  gchar *title;
+  gchar *icon_name;
+
+  pid = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE_PID");
+  filename = g_getenv ("GIO_LAUNCHED_DESKTOP_FILE");
+
+  keyfile = g_key_file_new ();
+
+  if (pid != NULL && filename != NULL && atoi (pid) == getpid () &&
+      g_key_file_load_from_file (keyfile, filename, 0, NULL))
+    {
+      title = g_key_file_get_locale_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_NAME, NULL, NULL);
+      icon_name = g_key_file_get_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_ICON, NULL);
+
+      g_print ("default title: %s\n", title);
+      g_print ("default icon: %s\n", icon_name);
+
+      if (default_title == NULL)
+        default_title = title;
+
+      if (gtk_window_get_default_icon_name () == NULL)
+        gtk_window_set_default_icon_name (icon_name);
+
+      g_free (icon_name);
+    }
+
+  g_key_file_free (keyfile);
+}
 
 static void
 gtk_application_init (GtkApplication *application)
 {
   application->priv = G_TYPE_INSTANCE_GET_PRIVATE (application, GTK_TYPE_APPLICATION, GtkApplicationPrivate);
+
+  setup_default_window_decorations ();
 }
 
+
 static GObject*
 gtk_application_constructor (GType                  type,
                              guint                  n_construct_properties,
diff --git a/tests/testapplication.c b/tests/testapplication.c
index ed32388..a2883fa 100644
--- a/tests/testapplication.c
+++ b/tests/testapplication.c
@@ -20,6 +20,7 @@
 
 #include "config.h"
 #include <gtk/gtk.h>
+#include <gio/gdesktopappinfo.h>
 
 static const char *builder_data =
 "<interface>"
@@ -47,34 +48,57 @@ about_activate (GtkAction *action,
   gtk_widget_hide (GTK_WIDGET (about_dialog));
 }
 
+static void
+launch_myself (void)
+{
+  GAppInfo *ai;
+
+  g_type_init ();
+
+  ai = (GAppInfo*)g_desktop_app_info_new_from_filename ("./testapplication.desktop");
+  g_app_info_launch (ai, NULL, NULL, NULL);
+}
+
 int
 main (int argc, char **argv)
 {
   GtkApplication *app;
   GtkWindow *window;
+  GtkWindow *window2;
   GtkBuilder *builder;
   GtkAction *action;
   GtkActionGroup *actions;
 
+  if (argc > 1 && strcmp (argv[1], "--launch-yourself") == 0)
+    {
+      launch_myself ();
+      exit (0);
+    }
+
   app = gtk_application_new (&argc, &argv, "org.gtk.TestApp");
   builder = gtk_builder_new ();
   if (!gtk_builder_add_from_string (builder, builder_data, -1, NULL))
     g_error ("failed to parse UI");
   actions = GTK_ACTION_GROUP (gtk_builder_get_object (builder, "actions"));
   gtk_application_set_action_group (app, actions);
-  
+
   action = gtk_action_group_get_action (actions, "About");
   g_signal_connect (action, "activate", G_CALLBACK (about_activate), app);
-  
+
   about_dialog = GTK_WIDGET (gtk_builder_get_object (builder, "about_dialog"));
-  
+
   gtk_builder_connect_signals (builder, app);
   g_object_unref (builder);
-  
+
   window = gtk_application_get_window (app);
   gtk_container_add (GTK_CONTAINER (window), gtk_label_new ("Hello world"));
   gtk_widget_show_all (GTK_WIDGET (window));
- 
+
+  window2 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+  gtk_container_add (GTK_CONTAINER (window2), gtk_label_new ("Hello again"));
+  gtk_widget_show_all (GTK_WIDGET (window2));
+  gtk_application_add_window (app, window2);
+
   gtk_application_run (app);
 
   return 0;



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