aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-icon-factory.c
blob: a558e046b02abd6704f7e6cad90e4e668fc06e71 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* -*- 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 <pthread.h>

#include <libgnomeui/gnome-icon-theme.h>
#include <e-util/e-icon-factory.h>
#include "art/empty.xpm"


static int sizes[E_ICON_NUM_SIZES] = {
    16, /* menu */
    20, /* button */
    18, /* small toolbar */
    24, /* large toolbar */
    32, /* dnd */
    48, /* dialog */
};


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;
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;


/* Creating and destroying icons.  */

/* Note: takes ownership of the pixbufs (eg. does not ref them) */
static Icon *
icon_new (const char *name, GdkPixbuf **pixbufs)
{
    Icon *icon;
    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;
}

#if 0

/* (This is not currently used since we never prune icons out of the
   cache.)  */
static void
icon_free (Icon *icon)
{
    int i;
    
    g_free (icon->name);
    
    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 char *icon_name)
{
    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);
    }
    
    return icon_new (icon_name, pixbufs);
    
 exception:
    
    for (j = 0; j < i; j++)
        g_object_unref (pixbufs[j]);
    
    return NULL;
}


/* termporary workaround for code that has not yet been ported to the new icon_size API */
static int
pixel_size_to_icon_size (int pixel_size)
{
    int i, icon_size = -1;
    
    for (i = 0; i < E_ICON_NUM_SIZES; i++) {
        if (pixel_size == sizes[i]) {
            icon_size = i;
            break;
        }
    }
    
    return icon_size;
}

/* 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);
}


/**
 * 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)
{
    char *filename;
    
    g_return_val_if_fail (icon_name != NULL, NULL);
    g_return_val_if_fail (strcmp (icon_name, ""), NULL);
    
    if (icon_size >= E_ICON_NUM_SIZES) {
        g_warning ("e_icon_factory_get_icon_filename(): Invalid argument for icon_size: use one of the E_ICON_SIZE_* enum values");
        if ((icon_size = pixel_size_to_icon_size (icon_size)) == -1)
            return NULL;
    }
    
    pthread_mutex_lock (&lock);
    filename = gnome_icon_theme_lookup_icon (icon_theme, icon_name, sizes[icon_size], NULL, NULL);
    pthread_mutex_unlock (&lock);
    
    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 char *icon_name, int icon_size)
{
    GdkPixbuf *pixbuf;
    Icon *icon;
    int size;
    
    if (icon_size >= E_ICON_NUM_SIZES) {
        g_warning ("e_icon_factory_get_icon(): Invalid argument for icon_size: use one of the E_ICON_SIZE_* enum values");
        if ((icon_size = pixel_size_to_icon_size (icon_size)) == -1)
            return NULL;
    }
    
    if (icon_name == NULL || !strcmp (icon_name, "")) {
        size = sizes[icon_size];
        return gdk_pixbuf_scale_simple (empty_pixbuf, size, size, GDK_INTERP_NEAREST);
    }
    
    pthread_mutex_lock (&lock);
    
    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);
        }
    }
    
    if ((pixbuf = icon->pixbuf[icon_size]))
        g_object_ref (pixbuf);
    
    pthread_mutex_unlock (&lock);
    
    return pixbuf;
}


GList *
e_icon_factory_get_icon_list (const char *icon_name)
{
    GList *list = NULL;
    Icon *icon;
    int i;
    
    if (!icon_name || !strcmp (icon_name, ""))
        return NULL;
    
    pthread_mutex_lock (&lock);
    
    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);
        }
    }
    
    for (i = 0; i < E_ICON_NUM_SIZES; i++) {
        if (icon->pixbuf[i]) {
            list = g_list_prepend (list, icon->pixbuf[i]);
            g_object_ref (icon->pixbuf[i]);
        }
    }
    
    pthread_mutex_unlock (&lock);
    
    return list;
}