aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-icon-factory.c
blob: 11b1ee7b5bc0f9f2dd7380a269723e4c0a4a7559 (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
279
280
281
282
283
284
285
286
287
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;
}