[libgd] GdStackSwitcher: add symbolic-icon-name property



commit 0db57b90dbdfe6b7cdfd7bee99b3c6f32ebbfac9
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Mon Feb 25 20:18:52 2013 +0100

    GdStackSwitcher: add symbolic-icon-name property

 libgd/gd-stack-switcher.c |   89 ++++++++++++++++++++++++++++++++++++---------
 libgd/gd-stack.c          |   28 +++++++++++++--
 test-stack.c              |   16 ++++++++-
 3 files changed, 111 insertions(+), 22 deletions(-)
---
diff --git a/libgd/gd-stack-switcher.c b/libgd/gd-stack-switcher.c
index 0c11f34..015ca33 100644
--- a/libgd/gd-stack-switcher.c
+++ b/libgd/gd-stack-switcher.c
@@ -71,20 +71,76 @@ on_button_clicked (GtkWidget       *widget,
 }
 
 static void
-on_title_updated (GtkWidget       *widget,
-                  GParamSpec      *pspec,
-                  GdStackSwitcher *self)
-
+update_button (GdStackSwitcher *self,
+               GtkWidget       *widget,
+               GtkWidget       *button)
 {
   char *title;
-  GtkWidget *button;
+  char *symbolic_icon_name;
+  GtkStyleContext *context;
+  GtkWidget *button_child;
 
   gtk_container_child_get (GTK_CONTAINER (self->priv->stack), widget,
                            "title", &title,
+                           "symbolic-icon-name", &symbolic_icon_name,
                            NULL);
-  button = g_hash_table_lookup (self->priv->buttons, widget);
-  gtk_button_set_label (GTK_BUTTON (button), title);
+
+  context = gtk_widget_get_style_context (button);
+  button_child = gtk_bin_get_child (GTK_BIN (button));
+
+  if (symbolic_icon_name != NULL && symbolic_icon_name[0] != '\0')
+    {
+      if (button_child != NULL && !GTK_IS_IMAGE (button_child))
+        {
+          gtk_widget_destroy (button_child);
+          button_child = NULL;
+        }
+
+      if (button_child == NULL)
+        {
+          button_child = gtk_image_new ();
+          gtk_container_add (GTK_CONTAINER (button), button_child);
+          gtk_widget_show (button_child);
+        }
+
+      gtk_image_set_from_icon_name (GTK_IMAGE (button_child), symbolic_icon_name, GTK_ICON_SIZE_MENU);
+      gtk_widget_set_tooltip_text (button, title);
+
+      gtk_style_context_add_class (context, "image-button");
+      gtk_style_context_remove_class (context, "text-button");
+    }
+  else
+    {
+      if (button_child != NULL && GTK_IS_LABEL (button_child))
+        gtk_label_set_text (GTK_LABEL (button_child), title);
+      else
+        {
+          if (button_child)
+            gtk_widget_destroy (button_child);
+          button_child = gtk_label_new (title);
+          gtk_widget_show (button_child);
+          gtk_container_add (GTK_CONTAINER (button), button_child);
+          gtk_widget_set_size_request (button, 100, -1);
+
+          gtk_style_context_add_class (context, "text-button");
+          gtk_style_context_remove_class (context, "image-button");
+        }
+    }
+
   g_free (title);
+  g_free (symbolic_icon_name);
+}
+
+static void
+on_title_icon_updated (GtkWidget       *widget,
+                       GParamSpec      *pspec,
+                       GdStackSwitcher *self)
+
+{
+  GtkWidget *button;
+
+  button = g_hash_table_lookup (self->priv->buttons, widget);
+  update_button (self, widget, button);
 }
 
 static void
@@ -92,24 +148,20 @@ add_child (GdStackSwitcher *self,
            GtkWidget       *widget)
 {
   GtkWidget *button;
+  GtkWidget *button_child = NULL;
   GList *group;
+  GtkStyleContext *context;
   char *title = NULL;
-
-  gtk_container_child_get (GTK_CONTAINER (self->priv->stack), widget,
-                           "title", &title,
-                           NULL);
+  char *symbolic_icon_name = NULL;
 
   button = gtk_radio_button_new (NULL);
-  if (title != NULL && title[0] != '\0')
-    gtk_button_set_label (GTK_BUTTON (button), title);
+  context = gtk_widget_get_style_context (button);
+  gtk_style_context_add_class (context, "raised");
 
-  g_free (title);
+  update_button (self, widget, button);
 
   gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (button), FALSE);
-  gtk_widget_set_size_request (button, 100, -1);
   gtk_widget_set_vexpand (button, TRUE);
-  gtk_style_context_add_class (gtk_widget_get_style_context (button), "raised");
-  gtk_style_context_add_class (gtk_widget_get_style_context (button), "text-button");
 
   group = gtk_container_get_children (GTK_CONTAINER (self));
   if (group != NULL)
@@ -123,7 +175,8 @@ add_child (GdStackSwitcher *self,
 
   g_object_set_data (G_OBJECT (button), "stack-child", widget);
   g_signal_connect (button, "clicked", G_CALLBACK (on_button_clicked), self);
-  g_signal_connect (widget, "child-notify::title", G_CALLBACK (on_title_updated), self);
+  g_signal_connect (widget, "child-notify::title", G_CALLBACK (on_title_icon_updated), self);
+  g_signal_connect (widget, "child-notify::symbolic-icon-name", G_CALLBACK (on_title_icon_updated), self);
 
   g_hash_table_insert (self->priv->buttons, widget, button);
 }
diff --git a/libgd/gd-stack.c b/libgd/gd-stack.c
index 16a234b..f0adadf 100644
--- a/libgd/gd-stack.c
+++ b/libgd/gd-stack.c
@@ -43,7 +43,8 @@ enum
 {
   CHILD_PROP_0,
   CHILD_PROP_NAME,
-  CHILD_PROP_TITLE
+  CHILD_PROP_TITLE,
+  CHILD_PROP_SYMBOLIC_ICON_NAME
 };
 
 typedef struct _GdStackChildInfo GdStackChildInfo;
@@ -52,6 +53,7 @@ struct _GdStackChildInfo {
   GtkWidget *widget;
   char *name;
   char *title;
+  char *symbolic_icon_name;
 };
 
 struct _GdStackPrivate {
@@ -283,6 +285,13 @@ gd_stack_class_init (GdStackClass * klass)
                          NULL,
                          GTK_PARAM_READWRITE));
 
+  gtk_container_class_install_child_property (container_class, CHILD_PROP_SYMBOLIC_ICON_NAME,
+    g_param_spec_string ("symbolic-icon-name",
+                         "Symbolic icon name",
+                         "The symbolic icon name of the child page",
+                         NULL,
+                         GTK_PARAM_READWRITE));
+
   g_type_class_add_private (klass, sizeof (GdStackPrivate));
 }
 
@@ -338,6 +347,10 @@ gd_stack_get_child_property (GtkContainer *container,
       g_value_set_string (value, info->title);
       break;
 
+    case CHILD_PROP_SYMBOLIC_ICON_NAME:
+      g_value_set_string (value, info->symbolic_icon_name);
+      break;
+
     default:
       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
       break;
@@ -381,6 +394,12 @@ gd_stack_set_child_property (GtkContainer *container,
       gtk_container_child_notify (container, child, "title");
       break;
 
+    case CHILD_PROP_SYMBOLIC_ICON_NAME:
+      g_free (info->symbolic_icon_name);
+      info->symbolic_icon_name = g_value_dup_string (value);
+      gtk_container_child_notify (container, child, "symbolic-icon-name");
+      break;
+
     default:
       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
       break;
@@ -424,8 +443,8 @@ gd_stack_set_transition_position (GdStack *stack,
 
 static gboolean
 gd_stack_transition_cb (GdStack *stack,
-                   GdkFrameClock *frame_clock,
-                   gpointer user_data)
+                        GdkFrameClock *frame_clock,
+                        gpointer user_data)
 {
   GdStackPrivate *priv = stack->priv;
   gint64 now;
@@ -619,6 +638,7 @@ gd_stack_add (GtkContainer *container,
   child_info->widget = child;
   child_info->name = NULL;
   child_info->title = NULL;
+  child_info->symbolic_icon_name = NULL;
 
   priv->children = g_list_append (priv->children, child_info);
 
@@ -668,7 +688,9 @@ gd_stack_remove (GtkContainer *container,
 
   gtk_widget_unparent (child);
 
+  g_free (child_info->name);
   g_free (child_info->title);
+  g_free (child_info->symbolic_icon_name);
   g_slice_free (GdStackChildInfo, child_info);
 
   if (priv->homogeneous && was_visible)
diff --git a/test-stack.c b/test-stack.c
index 986e3a6..cf12f79 100644
--- a/test-stack.c
+++ b/test-stack.c
@@ -4,6 +4,7 @@
 
 GtkWidget *stack;
 GtkWidget *switcher;
+GtkWidget *b1;
 
 static void
 set_visible_child (GtkWidget *button, gpointer data)
@@ -25,12 +26,21 @@ toggle_homogeneous (GtkWidget *button, gpointer data)
   gd_stack_set_homogeneous (GD_STACK (stack), active);
 }
 
+static void
+toggle_icon_name (GtkWidget *button, gpointer data)
+{
+  gboolean active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
+  gtk_container_child_set (GTK_CONTAINER (stack), b1,
+                          "symbolic-icon-name", active ? "edit-find-symbolic" : NULL,
+                          NULL);
+}
+
 gint
 main (gint argc,
       gchar ** argv)
 {
   GtkWidget *window, *box, *button, *hbox;
-  GtkWidget *b1, *b2, *b3;
+  GtkWidget *b2, *b3;
 
   gtk_init (&argc, &argv);
 
@@ -98,6 +108,10 @@ main (gint argc,
   gtk_container_add (GTK_CONTAINER (hbox), button);
   g_signal_connect (button, "clicked", (GCallback) toggle_homogeneous, NULL);
 
+  button = gtk_toggle_button_new_with_label ("Add symbolic icon");
+  g_signal_connect (button, "toggled", (GCallback) toggle_icon_name, NULL);
+  gtk_container_add (GTK_CONTAINER (hbox), button);
+
   gtk_widget_show_all (window);
   gtk_main ();
 


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