[gnome-contacts: 3/35] Use a pixbuf cache



commit 2e49eaf2dc8f58b71ea1fda5a9593f996c8e18cc
Author: Xavier Claessens <xavier claessens collabora co uk>
Date:   Thu Nov 8 18:05:41 2012 +0100

    Use a pixbuf cache
    
    https://bugzilla.gnome.org/show_bug.cgi?id=687911

 test-focus.c |   86 ++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 78 insertions(+), 8 deletions(-)
---
diff --git a/test-focus.c b/test-focus.c
index 54d4273..a1c0c3d 100644
--- a/test-focus.c
+++ b/test-focus.c
@@ -1,6 +1,75 @@
 #include <gtk/gtk.h>
 #include <egg-list-box.h>
 
+static GdkPixbuf *
+get_pix (GtkStyleContext *context,
+    const gchar *icon_name,
+    gint icon_size)
+{
+  static GHashTable *cache = NULL;
+  GtkStateFlags state;
+  gchar *key;
+  GtkIconInfo *icon_info;
+  GdkPixbuf *pix;
+  GError *error = NULL;
+
+  if (cache == NULL)
+    {
+      cache = g_hash_table_new_full (g_str_hash, g_str_equal,
+          g_free, g_object_unref);
+    }
+
+  state = gtk_style_context_get_state (context);
+  key = g_strdup_printf ("%s-%u-%u", icon_name, icon_size, state);
+  pix = g_hash_table_lookup (cache, key);
+  if (pix != NULL)
+    {
+      g_free (key);
+      return pix;
+    }
+
+  icon_info = gtk_icon_theme_lookup_icon (gtk_icon_theme_get_default (),
+      icon_name, icon_size, 0);
+  pix = gtk_icon_info_load_symbolic_for_context (icon_info, context, NULL, &error);
+  g_assert_no_error (error);
+
+  /* Takes ownership of key and pix */
+  g_hash_table_insert (cache, key, pix);
+
+  return pix;
+}
+
+static void
+image_update_pixbuf (GtkWidget *image)
+{
+  const gchar *icon_name;
+  gint icon_size;
+  GdkPixbuf *pix;
+
+  icon_name = g_object_get_data ((GObject *) image, "icon-name");
+  icon_size = GPOINTER_TO_INT (g_object_get_data ((GObject *) image, "icon-size"));
+  pix = get_pix (gtk_widget_get_style_context (image), icon_name, icon_size);
+
+  gtk_image_set_from_pixbuf ((GtkImage *) image, pix);
+}
+
+static GtkWidget *
+new_image (const gchar *icon_name,
+    gint icon_size)
+{
+  GtkWidget *image;
+
+  image = gtk_image_new ();
+  g_object_set_data ((GObject *) image, "icon-name", (gchar *) icon_name);
+  g_object_set_data ((GObject *) image, "icon-size", GINT_TO_POINTER (icon_size));
+  g_signal_connect (image, "style-updated",
+      G_CALLBACK (image_update_pixbuf), NULL);
+
+  image_update_pixbuf (image);
+
+  return image;
+}
+
 static void
 add_row (EggListBox *view)
 {
@@ -8,15 +77,15 @@ add_row (EggListBox *view)
   GtkStyleContext *context;
   GtkWidget *avatar;
   GtkWidget *first_line_alig;
-  GtkWidget *alias;
+  //GtkWidget *alias;
   GtkWidget *phone_icon;
-  GtkWidget *presence_msg;
+  //GtkWidget *presence_msg;
   GtkWidget *presence_icon;
 
   main_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 8);
 
   /* Avatar */
-  avatar = gtk_image_new_from_icon_name ("avatar-default-symbolic", GTK_ICON_SIZE_LARGE_TOOLBAR);
+  avatar = new_image ("avatar-default-symbolic", 48);
 
   gtk_widget_set_size_request (avatar, 48, 48);
 
@@ -29,15 +98,15 @@ add_row (EggListBox *view)
   first_line_alig = gtk_alignment_new (0, 0.5, 1, 1);
   first_line_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
 
+/*
   alias = gtk_label_new ("My Cool Alias");
   gtk_label_set_ellipsize (GTK_LABEL (alias), PANGO_ELLIPSIZE_END);
   gtk_box_pack_start (GTK_BOX (first_line_box), alias,
       FALSE, FALSE, 0);
   gtk_misc_set_alignment (GTK_MISC (alias), 0, 0.5);
   gtk_widget_show (alias);
-
-  phone_icon = gtk_image_new_from_icon_name ("phone-symbolic",
-      GTK_ICON_SIZE_MENU);
+*/
+  phone_icon = new_image ("phone-symbolic", 24);
   gtk_misc_set_alignment (GTK_MISC (phone_icon), 0, 0.5);
   gtk_box_pack_start (GTK_BOX (first_line_box), phone_icon,
       TRUE, TRUE, 0);
@@ -54,6 +123,7 @@ add_row (EggListBox *view)
   gtk_widget_show (box);
 
   /* Presence */
+/*
   presence_msg = gtk_label_new ("My Cool Presence Message");
   gtk_label_set_ellipsize (GTK_LABEL (presence_msg),
       PANGO_ELLIPSIZE_END);
@@ -62,9 +132,9 @@ add_row (EggListBox *view)
 
   context = gtk_widget_get_style_context (presence_msg);
   gtk_style_context_add_class (context, GTK_STYLE_CLASS_DIM_LABEL);
-
+*/
   /* Presence icon */
-  presence_icon = gtk_image_new_from_icon_name ("user-available", GTK_ICON_SIZE_MENU);
+  presence_icon = new_image ("user-available", 16);
 
   gtk_box_pack_start (GTK_BOX (main_box), presence_icon,
       FALSE, FALSE, 0);


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