[Evolution-hackers] e-icon-factory



Attached is a patch against current CVS head that fixes some bugs in e-icon-factory.c as well as simplifies it a lot and makes it sue an enum to specify the icons izes rather than using hard-coded absolute pixel sizes everywhere (ie. this is a lot closer to NotZed's thoughts on how it should be done).

I'll let mr terry make the rest of the code use it and to make SVG work and whatever else needs to be done.

Jeff
? e-icon.patch
? gethostbyaddr.patch
Index: e-icon-factory.c
===================================================================
RCS file: /cvs/gnome/evolution/e-util/e-icon-factory.c,v
retrieving revision 1.1
diff -u -r1.1 e-icon-factory.c
--- e-icon-factory.c	19 Apr 2004 15:17:19 -0000	1.1
+++ e-icon-factory.c	19 Apr 2004 22:02:20 -0000
@@ -29,45 +29,45 @@
 #include <e-util/e-icon-factory.h>
 #include "art/empty.xpm"
 
-
-/* One icon.  Keep both a small (16x16) and a large (48x48) version around.  */
-struct _Icon {
-	char *name;
-	GdkPixbuf *pixbuf_16;
-	GdkPixbuf *pixbuf_24;
-	GdkPixbuf *pixbuf_48;
+
+static int sizes[E_ICON_NUM_SIZES] = {
+	16, /* menu */
+	20, /* button */
+	18, /* small toolbar */
+	24, /* large toolbar */
+	32, /* dnd */
+	48, /* dialog */
 };
-typedef struct _Icon Icon;
+
+
+typedef struct {
+	char *name;
+	GdkPixbuf *pixbuf[E_ICON_NUM_SIZES];
+} Icon;
 
 /* Hash of all the icons.  */
 static GHashTable     *name_to_icon = NULL;
 static GnomeIconTheme *icon_theme   = NULL;
 static GdkPixbuf      *empty_pixbuf = NULL;
 
-
+
 /* Creating and destroying icons.  */
 
+/* Note: takes ownership of the pixbufs (eg. does not ref them) */
 static Icon *
-icon_new (const gchar *name,
-	  GdkPixbuf *pixbuf_16,
-	  GdkPixbuf *pixbuf_24,
-	  GdkPixbuf *pixbuf_48)
+icon_new (const char *name, GdkPixbuf **pixbufs)
 {
 	Icon *icon;
-
-	icon = g_new (Icon, 1);
-	icon->name      = g_strdup (name);
-	icon->pixbuf_16 = pixbuf_16;
-	icon->pixbuf_24 = pixbuf_24;
-	icon->pixbuf_48 = pixbuf_48;
-
-	if (pixbuf_16 != NULL)
-		g_object_ref (pixbuf_16);
-	if (pixbuf_24 != NULL)
-		g_object_ref (pixbuf_24);
-	if (pixbuf_48 != NULL)
-		g_object_ref (pixbuf_48);
-
+	int i;
+	
+	icon = g_malloc0 (sizeof (Icon));
+	icon->name = g_strdup (name);
+	
+	if (pixbufs != NULL) {
+		for (i = 0; i < E_ICON_NUM_SIZES; i++)
+			icon->pixbuf[i] = pixbufs[i];
+	}
+	
 	return icon;
 }
 
@@ -78,73 +78,53 @@
 static void
 icon_free (Icon *icon)
 {
+	int i;
+	
 	g_free (icon->name);
-
-	if (icon->pixbuf_16 != NULL)
-		g_object_unref (icon->pixbuf_16);
-	if (icon->pixbuf_24 != NULL)
-		g_object_unref (icon->pixbuf_24);
-	if (icon->pixbuf_48 != NULL)
-		g_object_unref (icon->pixbuf_48);
-
+	
+	for (i = 0; i < E_ICON_NUM_SIZES; i++) {
+		if (icon->pixbuf[i] != NULL)
+			g_object_unref (icon->pixbuf[i]);
+	}
+	
 	g_free (icon);
 }
 
 #endif
 
-
 /* Loading icons.  */
 
 static Icon *
-load_icon (const gchar *icon_name)
+load_icon (const char *icon_name)
 {
-	GdkPixbuf *unscaled;
-	GdkPixbuf *pixbuf_16;
-	GdkPixbuf *pixbuf_24;
-	GdkPixbuf *pixbuf_48;
-	gchar *filename;
-	Icon *icon;
-
-	filename = gnome_icon_theme_lookup_icon (icon_theme, icon_name, 16, 
-	                                         NULL, NULL);
-
-	if (filename == NULL)
-		return NULL;
-	unscaled = gdk_pixbuf_new_from_file (filename, NULL);
-	pixbuf_16 = gdk_pixbuf_scale_simple (unscaled, 16, 16, GDK_INTERP_BILINEAR);
-	g_object_unref (unscaled);
-	g_free (filename);
+	GdkPixbuf *pixbufs[E_ICON_NUM_SIZES];
+	char *filename;
+	int i, j;
+	
+	for (i = 0; i < E_ICON_NUM_SIZES; i++) {
+		GdkPixbuf *unscaled;
+		int size = sizes[i];
+		
+		if (!(filename = gnome_icon_theme_lookup_icon (icon_theme, icon_name, size, NULL, NULL)))
+			goto exception;
+		
+		unscaled = gdk_pixbuf_new_from_file (filename, NULL);
+		pixbufs[i] = gdk_pixbuf_scale_simple (unscaled, size, size, GDK_INTERP_BILINEAR);
+		g_object_unref (unscaled);
+		g_free (filename);
+	}
 	
-	filename = gnome_icon_theme_lookup_icon (icon_theme, icon_name, 24, 
-	                                         NULL, NULL);
-
-	if (filename == NULL)
-		return NULL;
-	unscaled = gdk_pixbuf_new_from_file (filename, NULL);
-	pixbuf_24 = gdk_pixbuf_scale_simple (unscaled, 24, 24, GDK_INTERP_BILINEAR);
-	g_object_unref (unscaled);
-	g_free (filename);
+	return icon_new (icon_name, pixbufs);
 	
-	filename = gnome_icon_theme_lookup_icon (icon_theme, icon_name, 48, 
-	                                         NULL, NULL);
-
-	if (filename == NULL)
-		return NULL;
-	unscaled = gdk_pixbuf_new_from_file (filename, NULL);
-	pixbuf_48 = gdk_pixbuf_scale_simple (unscaled, 48, 48, GDK_INTERP_BILINEAR);
-	g_object_unref (unscaled);
-	g_free (filename);
-
-	icon = icon_new (icon_name, pixbuf_16, pixbuf_24, pixbuf_48);
-
-	g_object_unref (pixbuf_16);
-	g_object_unref (pixbuf_24);
-	g_object_unref (pixbuf_48);
-
-	return icon;
+ exception:
+	
+	for (j = 0; j < i; j++)
+		g_object_unref (pixbufs[j]);
+	
+	return NULL;
 }
 
-
+
 /* Public API.  */
 
 void
@@ -154,26 +134,31 @@
 		/* Already initialized.  */
 		return;
 	}
-
+	
 	name_to_icon = g_hash_table_new (g_str_hash, g_str_equal);
 	icon_theme   = gnome_icon_theme_new ();
 	empty_pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) empty_xpm);
 }
 
-gchar *
-e_icon_factory_get_icon_filename (const gchar *icon_name,
-			          gint        icon_size)
+
+/**
+ * e_icon_factory_get_icon_filename:
+ * @icon_name: name of the icon
+ * @size: MENU/SMALL_TOOLBAR/etc
+ *
+ * Looks up the icon to use based on name and size.
+ *
+ * Returns the requested icon pixbuf.
+ **/
+char *
+e_icon_factory_get_icon_filename (const char *icon_name, int icon_size)
 {
-	gchar *filename;
+	char *filename;
 	
 	g_return_val_if_fail (icon_name != NULL, NULL);
 	g_return_val_if_fail (strcmp (icon_name, ""), NULL);
 	
-	filename = gnome_icon_theme_lookup_icon (icon_theme,
-	                                         icon_name,
-	                                         icon_size,
-	                                         NULL,
-	                                         NULL);
+	filename = gnome_icon_theme_lookup_icon (icon_theme, icon_name, sizes[icon_size], NULL, NULL);
 	
 	return filename;
 }
@@ -182,107 +167,69 @@
    The returned icon is guaranteed to be the requested size and exist.  If
    the themed icon cannot be found, an empty icon is returned. */
 GdkPixbuf *
-e_icon_factory_get_icon (const gchar *icon_name,
-			 gint        icon_size)
+e_icon_factory_get_icon (const char *icon_name, int icon_size)
 {
-	if (icon_name != NULL && strcmp (icon_name, "")) {
-		Icon *icon;
-
-		icon = g_hash_table_lookup (name_to_icon, icon_name);
-		if (icon == NULL) {
-			icon = load_icon (icon_name);
-			if (icon == NULL) {
-				g_warning ("Icon not found -- %s", icon_name);
-
-				/* Create an empty icon so that we don't keep spitting
-				   out the same warning over and over, every time this
-				   icon is requested.  */
-
-				icon = icon_new (icon_name, NULL, NULL, NULL);
-				g_hash_table_insert (name_to_icon, icon->name, icon);
-			}
-			else {
-				g_hash_table_insert (name_to_icon, icon->name, icon);
-			}
-		}
-
-		if (icon->pixbuf_16) {
-			gchar *filename;
-			GdkPixbuf *pixbuf, *scaled;
-			
-			switch (icon_size) {
-			case 16:
-				return g_object_ref (icon->pixbuf_16);
-			
-			case 24:
-				return g_object_ref (icon->pixbuf_24);
-			
-			case 48:
-				return g_object_ref (icon->pixbuf_48);
-			
-			default:
-				/* Non-standard size.  Do a non-cached load. */
-				
-				filename = gnome_icon_theme_lookup_icon (icon_theme,
-				                                         icon_name,
-				                                         icon_size,
-				                                         NULL,
-				                                         NULL);
-				if (filename == NULL)
-					break;
-				
-				pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
-				g_free (filename);
-				if (pixbuf == NULL)
-					break;
-				
-				scaled = gdk_pixbuf_scale_simple (pixbuf, icon_size, icon_size, GDK_INTERP_BILINEAR);
-				g_object_unref (pixbuf);
-				
-				return scaled;
-			}
+	int size = sizes[icon_size];
+	GdkPixbuf *pixbuf;
+	Icon *icon;
+	
+	if (icon_name == NULL || !strcmp (icon_name, ""))
+		return gdk_pixbuf_scale_simple (empty_pixbuf, size, size, GDK_INTERP_NEAREST);
+	
+	if (!(icon = g_hash_table_lookup (name_to_icon, icon_name))) {
+		if (!(icon = load_icon (icon_name))) {
+			g_warning ("Icon not found -- %s", icon_name);
+			
+			/* Create an empty icon so that we don't keep spitting
+			   out the same warning over and over, every time this
+			   icon is requested.  */
+			
+			icon = icon_new (icon_name, NULL);
+			g_hash_table_insert (name_to_icon, icon->name, icon);
+		} else {
+			g_hash_table_insert (name_to_icon, icon->name, icon);
 		}
 	}
 	
-	/* icon not found -- create an empty icon */
-	return gdk_pixbuf_scale_simple (empty_pixbuf, icon_size, icon_size, GDK_INTERP_NEAREST);
+	if ((pixbuf = icon->pixbufs[icon_size]))
+		g_object_ref (pixbuf);
+	
+	return pixbuf;
 }
 
+
 GList *
-e_icon_factory_get_icon_list (const gchar *icon_name)
+e_icon_factory_get_icon_list (const char *icon_name)
 {
-	if (icon_name != NULL && strcmp (icon_name, "")) {
-		Icon *icon;
-
-		icon = g_hash_table_lookup (name_to_icon, icon_name);
-		if (icon == NULL) {
-			icon = load_icon (icon_name);
-			if (icon == NULL) {
-				g_warning ("Icon not found -- %s", icon_name);
-
-				/* Create an empty icon so that we don't keep spitting
-				   out the same warning over and over, every time this
-				   icon is requested.  */
-
-				icon = icon_new (icon_name, NULL, NULL, NULL);
-				g_hash_table_insert (name_to_icon, icon->name, icon);
-			}
-			else {
-				g_hash_table_insert (name_to_icon, icon->name, icon);
-			}
+	GList *list = NULL;
+	Icon *icon;
+	int i;
+	
+	if (!icon_name || !strcmp (icon_name, ""))
+		return NULL;
+	
+	if (!(icon = g_hash_table_lookup (name_to_icon, icon_name))) {
+		if (!(icon = load_icon (icon_name))) {
+			g_warning ("Icon not found -- %s", icon_name);
+			
+			/* Create an empty icon so that we don't keep spitting
+			   out the same warning over and over, every time this
+			   icon is requested.  */
+			
+			icon = icon_new (icon_name, NULL);
+			g_hash_table_insert (name_to_icon, icon->name, icon);
+			return NULL;
+		} else {
+			g_hash_table_insert (name_to_icon, icon->name, icon);
 		}
-		
-		if (icon->pixbuf_16) {
-			GList *list = NULL;
-			
-			list = g_list_append (list, g_object_ref (icon->pixbuf_48));
-			list = g_list_append (list, g_object_ref (icon->pixbuf_24));
-			list = g_list_append (list, g_object_ref (icon->pixbuf_16));
-			
-			return list;
+	}
+	
+	for (i = 0; i < E_ICON_NUM_SIZES; i++) {
+		if (icon->pixbufs[i]) {
+			list = g_list_prepend (list, icon->pixbufs[i]);
+			g_object_ref (icon->pixbufs[i]);
 		}
 	}
 	
-	/* icons not found */
-	return NULL;
+	return list;
 }
Index: e-icon-factory.h
===================================================================
RCS file: /cvs/gnome/evolution/e-util/e-icon-factory.h,v
retrieving revision 1.2
diff -u -r1.2 e-icon-factory.h
--- e-icon-factory.h	19 Apr 2004 21:44:37 -0000	1.2
+++ e-icon-factory.h	19 Apr 2004 22:02:20 -0000
@@ -26,12 +26,12 @@
 #include <gdk-pixbuf/gdk-pixbuf.h>
 
 enum {
-	E_ICON_SIZE_MENU          = 16,
-	E_ICON_SIZE_BUTTON        = 20,
-	E_ICON_SIZE_SMALL_TOOLBAR = 18,
-	E_ICON_SIZE_LARGE_TOOLBAR = 24,
-	E_ICON_SIZE_DND           = 32,
-	E_ICON_SIZE_DIALOG        = 48,
+	E_ICON_SIZE_MENU,
+	E_ICON_SIZE_BUTTON,
+	E_ICON_SIZE_SMALL_TOOLBAR,
+	E_ICON_SIZE_LARGE_TOOLBAR,
+	E_ICON_SIZE_DND,
+	E_ICON_SIZE_DIALOG,
 	E_ICON_NUM_SIZES
 };
 


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