From 1ace8a21329554efffa1d1185befee021cedd1a7 Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Mon, 19 Apr 2004 15:17:19 +0000 Subject: Add e-icon-factory.[ch] Use the icon theme via EIconFactory Move 2004-04-16 Michael Terry * Makefile.am: Add e-icon-factory.[ch] * e-gui-utils.c: Use the icon theme via EIconFactory * e-icon-factory.[ch]: Move EIconFactory to e-util, and add icon theme support to the object so evolution uses them svn path=/trunk/; revision=25511 --- e-util/ChangeLog | 7 ++ e-util/Makefile.am | 4 +- e-util/e-gui-utils.c | 18 ++- e-util/e-icon-factory.c | 288 ++++++++++++++++++++++++++++++++++++++++++++++++ e-util/e-icon-factory.h | 38 +++++++ 5 files changed, 344 insertions(+), 11 deletions(-) create mode 100644 e-util/e-icon-factory.c create mode 100644 e-util/e-icon-factory.h (limited to 'e-util') diff --git a/e-util/ChangeLog b/e-util/ChangeLog index 7be2932b89..e4b3172e5a 100644 --- a/e-util/ChangeLog +++ b/e-util/ChangeLog @@ -1,3 +1,10 @@ +2004-04-16 Michael Terry + + * Makefile.am: Add e-icon-factory.[ch] + * e-gui-utils.c: Use the icon theme via EIconFactory + * e-icon-factory.[ch]: Move EIconFactory to e-util, and add icon theme + support to the object so evolution uses them + 2004-04-13 Jeffrey Stedfast * e-signature-list.c (gconf_signatures_changed): Don't add an diff --git a/e-util/Makefile.am b/e-util/Makefile.am index 70cb9ab836..70af472abc 100644 --- a/e-util/Makefile.am +++ b/e-util/Makefile.am @@ -27,6 +27,7 @@ eutilinclude_HEADERS = \ e-gui-utils.h \ e-host-utils.h \ e-html-utils.h \ + e-icon-factory.h \ e-iterator.h \ e-lang-utils.h \ e-list-iterator.h \ @@ -65,6 +66,7 @@ libeutil_la_SOURCES = \ e-gui-utils.c \ e-host-utils.c \ e-html-utils.c \ + e-icon-factory.c \ e-iterator.c \ e-lang-utils.c \ e-list-iterator.c \ @@ -120,4 +122,4 @@ BUILT_SOURCES = $(MARSHAL_GENERATED) CLEANFILES = $(BUILT_SOURCES) dist-hook: - cd $(distdir); rm -f $(BUILT_SOURCES) \ No newline at end of file + cd $(distdir); rm -f $(BUILT_SOURCES) diff --git a/e-util/e-gui-utils.c b/e-util/e-gui-utils.c index 9f80610063..40d32de4d1 100644 --- a/e-util/e-gui-utils.c +++ b/e-util/e-gui-utils.c @@ -14,6 +14,7 @@ #include #include "e-gui-utils.h" +#include #include #include @@ -27,25 +28,23 @@ #ifdef HAVE_LIBGNOMEUI_GNOME_ICON_LOOKUP_H #include -#else -#include "art/empty.xpm" #endif GtkWidget *e_create_image_widget(gchar *name, gchar *string1, gchar *string2, gint int1, gint int2) { - char *filename; GtkWidget *alignment = NULL; if (string1) { GtkWidget *w; + GdkPixbuf *pixbuf; + + pixbuf = e_icon_factory_get_icon (string1, 48); - if (*string1 == '/') - filename = g_strdup(string1); - else - filename = g_build_filename (EVOLUTION_IMAGES, string1, NULL); + w = gtk_image_new_from_pixbuf (pixbuf); + g_object_unref (pixbuf); - w = gtk_image_new_from_file (filename); + gtk_misc_set_alignment (GTK_MISC (w), 0.5, 0.5); alignment = gtk_widget_new(gtk_alignment_get_type(), "child", w, @@ -56,7 +55,6 @@ GtkWidget *e_create_image_widget(gchar *name, NULL); gtk_widget_show_all (alignment); - g_free (filename); } return alignment; @@ -159,7 +157,7 @@ e_icon_for_mime_type (const char *mime_type, int size_hint) "document-icons/i-regular.png", TRUE, NULL); if (!icon_path) { g_warning ("Could not get any icon for %s!",mime_type); - return gdk_pixbuf_new_from_xpm_data((const char **)empty_xpm); + return e_icon_factory_get_icon (NULL, size_hint); } } #endif diff --git a/e-util/e-icon-factory.c b/e-util/e-icon-factory.c new file mode 100644 index 0000000000..11b1ee7b5b --- /dev/null +++ b/e-util/e-icon-factory.c @@ -0,0 +1,288 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* e-icon-factory.c - Icon factory for the Evolution components. + * + * Copyright (C) 2002 Ximian, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: Ettore Perazzoli , Michael Terry + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#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; +}; +typedef struct _Icon 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. */ + +static Icon * +icon_new (const gchar *name, + GdkPixbuf *pixbuf_16, + GdkPixbuf *pixbuf_24, + GdkPixbuf *pixbuf_48) +{ + 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); + + return icon; +} + +#if 0 + +/* (This is not currently used since we never prune icons out of the + cache.) */ +static void +icon_free (Icon *icon) +{ + 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); + + g_free (icon); +} + +#endif + + +/* Loading icons. */ + +static Icon * +load_icon (const gchar *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); + + 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); + + 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; +} + + +/* Public API. */ + +void +e_icon_factory_init (void) +{ + if (name_to_icon != NULL) { + /* 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) +{ + gchar *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); + + return filename; +} + +/* Loads the themed version of the icon name at the appropriate size. + 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) +{ + 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; + } + } + } + + /* icon not found -- create an empty icon */ + return gdk_pixbuf_scale_simple (empty_pixbuf, icon_size, icon_size, GDK_INTERP_NEAREST); +} + +GList * +e_icon_factory_get_icon_list (const gchar *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); + } + } + + 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; + } + } + + /* icons not found */ + return NULL; +} diff --git a/e-util/e-icon-factory.h b/e-util/e-icon-factory.h new file mode 100644 index 0000000000..8dede96ca4 --- /dev/null +++ b/e-util/e-icon-factory.h @@ -0,0 +1,38 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* e-icon-factory.h - Icon factory for the Evolution shell. + * + * Copyright (C) 2002 Ximian, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: Ettore Perazzoli + */ + +#ifndef _E_ICON_FACTORY_H_ +#define _E_ICON_FACTORY_H_ + +#include + +void e_icon_factory_init (void); + +gchar *e_icon_factory_get_icon_filename (const gchar *icon_name, + gint icon_size); + +GdkPixbuf *e_icon_factory_get_icon (const gchar *icon_name, + gint icon_size); + +GList *e_icon_factory_get_icon_list (const gchar *icon_name); + +#endif /* _E_ICON_FACTORY_H_ */ -- cgit