Add e-icon-factory.[ch] Use the icon theme via EIconFactory Move

2004-04-16  Michael Terry  <mike@mterry.name>

	* 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
This commit is contained in:
Michael Terry
2004-04-19 15:17:19 +00:00
committed by Rodney Dawes
parent e901dd254f
commit 1ace8a2132
5 changed files with 344 additions and 11 deletions

View File

@ -1,3 +1,10 @@
2004-04-16 Michael Terry <mike@mterry.name>
* 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 <fejj@ximian.com>
* e-signature-list.c (gconf_signatures_changed): Don't add an

View File

@ -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)
cd $(distdir); rm -f $(BUILT_SOURCES)

View File

@ -14,6 +14,7 @@
#include <string.h>
#include "e-gui-utils.h"
#include <e-util/e-icon-factory.h>
#include <glib.h>
#include <gtk/gtkalignment.h>
@ -27,25 +28,23 @@
#ifdef HAVE_LIBGNOMEUI_GNOME_ICON_LOOKUP_H
#include <libgnomeui/gnome-icon-lookup.h>
#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;
if (*string1 == '/')
filename = g_strdup(string1);
else
filename = g_build_filename (EVOLUTION_IMAGES, string1, NULL);
pixbuf = e_icon_factory_get_icon (string1, 48);
w = gtk_image_new_from_file (filename);
w = gtk_image_new_from_pixbuf (pixbuf);
g_object_unref (pixbuf);
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

288
e-util/e-icon-factory.c Normal file
View File

@ -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 <ettore@ximian.com>, Michael Terry <mterry@fastmail.fm>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <libgnomeui/gnome-icon-theme.h>
#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;
};
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;
}

38
e-util/e-icon-factory.h Normal file
View File

@ -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 <ettore@ximian.com>
*/
#ifndef _E_ICON_FACTORY_H_
#define _E_ICON_FACTORY_H_
#include <gdk-pixbuf/gdk-pixbuf.h>
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_ */