]> Pileus Git - ~andy/gtk/blob - gtk/gtkiconfactory.c
Fixed typo in icon_size_lookup_intern() - colon instead of semicolon
[~andy/gtk] / gtk / gtkiconfactory.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2000 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include <config.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <pango/pango-utils.h>  /* For pango_scan_* */
32 #include "gtkiconfactory.h"
33 #include "gtkiconcache.h"
34 #include "gtkdebug.h"
35 #include "gtkicontheme.h"
36 #include "gtksettings.h"
37 #include "gtkstock.h"
38 #include "gtkwidget.h"
39 #include "gtkintl.h"
40 #include "gtkalias.h"
41
42
43 static GSList *all_icon_factories = NULL;
44
45 typedef enum {
46   GTK_ICON_SOURCE_EMPTY,
47   GTK_ICON_SOURCE_ICON_NAME,
48   GTK_ICON_SOURCE_STATIC_ICON_NAME,
49   GTK_ICON_SOURCE_FILENAME,
50   GTK_ICON_SOURCE_PIXBUF
51 } GtkIconSourceType;
52
53 struct _GtkIconSource
54 {
55   GtkIconSourceType type;
56   
57   union {
58     gchar *icon_name;
59     gchar *filename;
60     GdkPixbuf *pixbuf;
61   } source;
62
63   GdkPixbuf *filename_pixbuf;
64
65   GtkTextDirection direction;
66   GtkStateType state;
67   GtkIconSize size;
68
69   /* If TRUE, then the parameter is wildcarded, and the above
70    * fields should be ignored. If FALSE, the parameter is
71    * specified, and the above fields should be valid.
72    */
73   guint any_direction : 1;
74   guint any_state : 1;
75   guint any_size : 1;
76
77 #ifdef G_OS_WIN32
78   /* System codepage version of filename, for DLL ABI backward
79    * compatibility functions.
80    */
81   gchar *cp_filename;
82 #endif
83 };
84
85
86 static void gtk_icon_factory_finalize   (GObject             *object);
87 static void get_default_icons           (GtkIconFactory      *icon_factory);
88 static void icon_source_clear           (GtkIconSource       *source);
89
90 static GtkIconSize icon_size_register_intern (const gchar *name,
91                                               gint         width,
92                                               gint         height);
93
94 #define GTK_ICON_SOURCE_INIT(any_direction, any_state, any_size)        \
95   { GTK_ICON_SOURCE_EMPTY, { NULL }, NULL,                              \
96    0, 0, 0,                                                             \
97    any_direction, any_state, any_size }
98
99 G_DEFINE_TYPE (GtkIconFactory, gtk_icon_factory, G_TYPE_OBJECT)
100
101 static void
102 gtk_icon_factory_init (GtkIconFactory *factory)
103 {
104   factory->icons = g_hash_table_new (g_str_hash, g_str_equal);
105   all_icon_factories = g_slist_prepend (all_icon_factories, factory);
106 }
107
108 static void
109 gtk_icon_factory_class_init (GtkIconFactoryClass *klass)
110 {
111   GObjectClass *object_class = G_OBJECT_CLASS (klass);
112   
113   object_class->finalize = gtk_icon_factory_finalize;
114 }
115
116 static void
117 free_icon_set (gpointer key, gpointer value, gpointer data)
118 {
119   g_free (key);
120   gtk_icon_set_unref (value);
121 }
122
123 static void
124 gtk_icon_factory_finalize (GObject *object)
125 {
126   GtkIconFactory *factory = GTK_ICON_FACTORY (object);
127
128   all_icon_factories = g_slist_remove (all_icon_factories, factory);
129   
130   g_hash_table_foreach (factory->icons, free_icon_set, NULL);
131   
132   g_hash_table_destroy (factory->icons);
133   
134   G_OBJECT_CLASS (gtk_icon_factory_parent_class)->finalize (object);
135 }
136
137 /**
138  * gtk_icon_factory_new:
139  *
140  * Creates a new #GtkIconFactory. An icon factory manages a collection
141  * of #GtkIconSet<!-- -->s; a #GtkIconSet manages a set of variants of a
142  * particular icon (i.e. a #GtkIconSet contains variants for different
143  * sizes and widget states). Icons in an icon factory are named by a
144  * stock ID, which is a simple string identifying the icon. Each
145  * #GtkStyle has a list of #GtkIconFactory<!-- -->s derived from the current
146  * theme; those icon factories are consulted first when searching for
147  * an icon. If the theme doesn't set a particular icon, GTK+ looks for
148  * the icon in a list of default icon factories, maintained by
149  * gtk_icon_factory_add_default() and
150  * gtk_icon_factory_remove_default(). Applications with icons should
151  * add a default icon factory with their icons, which will allow
152  * themes to override the icons for the application.
153  * 
154  * Return value: a new #GtkIconFactory
155  **/
156 GtkIconFactory*
157 gtk_icon_factory_new (void)
158 {
159   return g_object_new (GTK_TYPE_ICON_FACTORY, NULL);
160 }
161
162 /**
163  * gtk_icon_factory_add:
164  * @factory: a #GtkIconFactory
165  * @stock_id: icon name
166  * @icon_set: icon set
167  *
168  * Adds the given @icon_set to the icon factory, under the name
169  * @stock_id.  @stock_id should be namespaced for your application,
170  * e.g. "myapp-whatever-icon".  Normally applications create a
171  * #GtkIconFactory, then add it to the list of default factories with
172  * gtk_icon_factory_add_default(). Then they pass the @stock_id to
173  * widgets such as #GtkImage to display the icon. Themes can provide
174  * an icon with the same name (such as "myapp-whatever-icon") to
175  * override your application's default icons. If an icon already
176  * existed in @factory for @stock_id, it is unreferenced and replaced
177  * with the new @icon_set.
178  * 
179  **/
180 void
181 gtk_icon_factory_add (GtkIconFactory *factory,
182                       const gchar    *stock_id,
183                       GtkIconSet     *icon_set)
184 {
185   gpointer old_key = NULL;
186   gpointer old_value = NULL;
187
188   g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
189   g_return_if_fail (stock_id != NULL);
190   g_return_if_fail (icon_set != NULL);  
191
192   g_hash_table_lookup_extended (factory->icons, stock_id,
193                                 &old_key, &old_value);
194
195   if (old_value == icon_set)
196     return;
197   
198   gtk_icon_set_ref (icon_set);
199
200   /* GHashTable key memory management is so fantastically broken. */
201   if (old_key)
202     g_hash_table_insert (factory->icons, old_key, icon_set);
203   else
204     g_hash_table_insert (factory->icons, g_strdup (stock_id), icon_set);
205
206   if (old_value)
207     gtk_icon_set_unref (old_value);
208 }
209
210 /**
211  * gtk_icon_factory_lookup:
212  * @factory: a #GtkIconFactory
213  * @stock_id: an icon name
214  * 
215  * Looks up @stock_id in the icon factory, returning an icon set
216  * if found, otherwise %NULL. For display to the user, you should
217  * use gtk_style_lookup_icon_set() on the #GtkStyle for the
218  * widget that will display the icon, instead of using this
219  * function directly, so that themes are taken into account.
220  * 
221  * Return value: icon set of @stock_id.
222  **/
223 GtkIconSet *
224 gtk_icon_factory_lookup (GtkIconFactory *factory,
225                          const gchar    *stock_id)
226 {
227   g_return_val_if_fail (GTK_IS_ICON_FACTORY (factory), NULL);
228   g_return_val_if_fail (stock_id != NULL, NULL);
229   
230   return g_hash_table_lookup (factory->icons, stock_id);
231 }
232
233 static GtkIconFactory *gtk_default_icons = NULL;
234 static GSList *default_factories = NULL;
235
236 /**
237  * gtk_icon_factory_add_default:
238  * @factory: a #GtkIconFactory
239  * 
240  * Adds an icon factory to the list of icon factories searched by
241  * gtk_style_lookup_icon_set(). This means that, for example,
242  * gtk_image_new_from_stock() will be able to find icons in @factory.
243  * There will normally be an icon factory added for each library or
244  * application that comes with icons. The default icon factories
245  * can be overridden by themes.
246  * 
247  **/
248 void
249 gtk_icon_factory_add_default (GtkIconFactory *factory)
250 {
251   g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
252
253   g_object_ref (factory);
254   
255   default_factories = g_slist_prepend (default_factories, factory);
256 }
257
258 /**
259  * gtk_icon_factory_remove_default:
260  * @factory: a #GtkIconFactory previously added with gtk_icon_factory_add_default()
261  *
262  * Removes an icon factory from the list of default icon
263  * factories. Not normally used; you might use it for a library that
264  * can be unloaded or shut down.
265  * 
266  **/
267 void
268 gtk_icon_factory_remove_default (GtkIconFactory  *factory)
269 {
270   g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
271
272   default_factories = g_slist_remove (default_factories, factory);
273
274   g_object_unref (factory);
275 }
276
277 void
278 _gtk_icon_factory_ensure_default_icons (void)
279 {
280   if (gtk_default_icons == NULL)
281     {
282       gtk_default_icons = gtk_icon_factory_new ();
283
284       get_default_icons (gtk_default_icons);
285     }
286 }
287
288 /**
289  * gtk_icon_factory_lookup_default:
290  * @stock_id: an icon name
291  *
292  * Looks for an icon in the list of default icon factories.  For
293  * display to the user, you should use gtk_style_lookup_icon_set() on
294  * the #GtkStyle for the widget that will display the icon, instead of
295  * using this function directly, so that themes are taken into
296  * account.
297  * 
298  * 
299  * Return value: a #GtkIconSet, or %NULL
300  **/
301 GtkIconSet *
302 gtk_icon_factory_lookup_default (const gchar *stock_id)
303 {
304   GSList *tmp_list;
305
306   g_return_val_if_fail (stock_id != NULL, NULL);
307   
308   tmp_list = default_factories;
309   while (tmp_list != NULL)
310     {
311       GtkIconSet *icon_set =
312         gtk_icon_factory_lookup (GTK_ICON_FACTORY (tmp_list->data),
313                                  stock_id);
314
315       if (icon_set)
316         return icon_set;
317       
318       tmp_list = g_slist_next (tmp_list);
319     }
320
321   _gtk_icon_factory_ensure_default_icons ();
322   
323   return gtk_icon_factory_lookup (gtk_default_icons, stock_id);
324 }
325
326 static void
327 register_stock_icon (GtkIconFactory *factory,
328                      const gchar    *stock_id)
329 {
330   GtkIconSet *set = gtk_icon_set_new ();
331   GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
332
333   source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
334   source.source.icon_name = (gchar *)stock_id;
335   gtk_icon_set_add_source (set, &source);
336   
337   gtk_icon_factory_add (factory, stock_id, set);
338   gtk_icon_set_unref (set);
339 }
340
341 static void
342 register_bidi_stock_icon (GtkIconFactory *factory,
343                           const gchar    *stock_id,
344                           const gchar    *stock_id_ltr,
345                           const gchar    *stock_id_rtl)
346 {
347   GtkIconSet *set = gtk_icon_set_new ();
348   GtkIconSource source = GTK_ICON_SOURCE_INIT (FALSE, TRUE, TRUE);
349
350   source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
351   source.source.icon_name = (gchar *)stock_id_ltr;
352   source.direction = GTK_TEXT_DIR_LTR;
353   gtk_icon_set_add_source (set, &source);
354   
355   source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
356   source.source.icon_name = (gchar *)stock_id_rtl;
357   source.direction = GTK_TEXT_DIR_RTL;
358   gtk_icon_set_add_source (set, &source);
359   
360   gtk_icon_factory_add (factory, stock_id, set);
361   gtk_icon_set_unref (set);
362 }
363
364 static void
365 get_default_icons (GtkIconFactory *factory)
366 {
367   /* KEEP IN SYNC with gtkstock.c */
368
369   register_stock_icon (factory, GTK_STOCK_DIALOG_AUTHENTICATION);
370   register_stock_icon (factory, GTK_STOCK_DIALOG_ERROR);
371   register_stock_icon (factory, GTK_STOCK_DIALOG_INFO);
372   register_stock_icon (factory, GTK_STOCK_DIALOG_QUESTION);
373   register_stock_icon (factory, GTK_STOCK_DIALOG_WARNING);
374   register_stock_icon (factory, GTK_STOCK_DND);
375   register_stock_icon (factory, GTK_STOCK_DND_MULTIPLE);
376   register_stock_icon (factory, GTK_STOCK_APPLY);
377   register_stock_icon (factory, GTK_STOCK_CANCEL);
378   register_stock_icon (factory, GTK_STOCK_NO);
379   register_stock_icon (factory, GTK_STOCK_OK);
380   register_stock_icon (factory, GTK_STOCK_YES);
381   register_stock_icon (factory, GTK_STOCK_CLOSE);
382   register_stock_icon (factory, GTK_STOCK_ADD);
383   register_stock_icon (factory, GTK_STOCK_JUSTIFY_CENTER);
384   register_stock_icon (factory, GTK_STOCK_JUSTIFY_FILL);
385   register_stock_icon (factory, GTK_STOCK_JUSTIFY_LEFT);
386   register_stock_icon (factory, GTK_STOCK_JUSTIFY_RIGHT);
387   register_stock_icon (factory, GTK_STOCK_GOTO_BOTTOM);
388   register_stock_icon (factory, GTK_STOCK_CDROM);
389   register_stock_icon (factory, GTK_STOCK_CONVERT);
390   register_stock_icon (factory, GTK_STOCK_COPY);
391   register_stock_icon (factory, GTK_STOCK_CUT);
392   register_stock_icon (factory, GTK_STOCK_GO_DOWN);
393   register_stock_icon (factory, GTK_STOCK_EXECUTE);
394   register_stock_icon (factory, GTK_STOCK_QUIT);
395   register_bidi_stock_icon (factory,  
396                             GTK_STOCK_GOTO_FIRST, 
397                             GTK_STOCK_GOTO_FIRST "-ltr", 
398                             GTK_STOCK_GOTO_FIRST "-rtl");
399   register_stock_icon (factory, GTK_STOCK_SELECT_FONT);
400   register_stock_icon (factory, GTK_STOCK_FULLSCREEN);
401   register_stock_icon (factory, GTK_STOCK_LEAVE_FULLSCREEN);
402   register_stock_icon (factory, GTK_STOCK_HARDDISK);
403   register_stock_icon (factory, GTK_STOCK_HELP);
404   register_stock_icon (factory, GTK_STOCK_HOME);
405   register_stock_icon (factory, GTK_STOCK_INFO);
406   register_bidi_stock_icon (factory, 
407                             GTK_STOCK_JUMP_TO,
408                             GTK_STOCK_JUMP_TO "-ltr",
409                             GTK_STOCK_JUMP_TO "-rtl");
410   register_bidi_stock_icon (factory, 
411                             GTK_STOCK_GOTO_LAST,
412                             GTK_STOCK_GOTO_LAST "-ltr",
413                             GTK_STOCK_GOTO_LAST "-rtl");
414   register_bidi_stock_icon (factory, 
415                             GTK_STOCK_GO_BACK,
416                             GTK_STOCK_GO_BACK "-ltr",
417                             GTK_STOCK_GO_BACK "-rtl");
418   register_stock_icon (factory, GTK_STOCK_MISSING_IMAGE);
419   register_stock_icon (factory, GTK_STOCK_NETWORK);
420   register_stock_icon (factory, GTK_STOCK_NEW);
421   register_stock_icon (factory, GTK_STOCK_OPEN);
422   register_stock_icon (factory, GTK_STOCK_ORIENTATION_PORTRAIT);
423   register_stock_icon (factory, GTK_STOCK_ORIENTATION_LANDSCAPE);
424   register_stock_icon (factory, GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT);
425   register_stock_icon (factory, GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE);
426   register_stock_icon (factory, GTK_STOCK_PASTE);
427   register_stock_icon (factory, GTK_STOCK_PREFERENCES);
428   register_stock_icon (factory, GTK_STOCK_PRINT);
429   register_stock_icon (factory, GTK_STOCK_PRINT_PREVIEW);
430   register_stock_icon (factory, GTK_STOCK_PROPERTIES);
431   register_bidi_stock_icon (factory, 
432                             GTK_STOCK_REDO,
433                             GTK_STOCK_REDO "-ltr",
434                             GTK_STOCK_REDO "-rtl");
435   register_stock_icon (factory, GTK_STOCK_REMOVE);
436   register_stock_icon (factory, GTK_STOCK_REFRESH);
437   register_bidi_stock_icon (factory, 
438                             GTK_STOCK_REVERT_TO_SAVED,
439                             GTK_STOCK_REVERT_TO_SAVED "-ltr",
440                             GTK_STOCK_REVERT_TO_SAVED "-rtl");
441   register_bidi_stock_icon (factory, 
442                             GTK_STOCK_GO_FORWARD,
443                             GTK_STOCK_GO_FORWARD "-ltr",
444                             GTK_STOCK_GO_FORWARD "-rtl");
445   register_stock_icon (factory, GTK_STOCK_SAVE);
446   register_stock_icon (factory, GTK_STOCK_FLOPPY);
447   register_stock_icon (factory, GTK_STOCK_SAVE_AS);
448   register_stock_icon (factory, GTK_STOCK_FIND);
449   register_stock_icon (factory, GTK_STOCK_FIND_AND_REPLACE);
450   register_stock_icon (factory, GTK_STOCK_SORT_DESCENDING);
451   register_stock_icon (factory, GTK_STOCK_SORT_ASCENDING);
452   register_stock_icon (factory, GTK_STOCK_SPELL_CHECK);
453   register_stock_icon (factory, GTK_STOCK_STOP);
454   register_stock_icon (factory, GTK_STOCK_BOLD);
455   register_stock_icon (factory, GTK_STOCK_ITALIC);
456   register_stock_icon (factory, GTK_STOCK_STRIKETHROUGH);
457   register_stock_icon (factory, GTK_STOCK_UNDERLINE);
458   register_bidi_stock_icon (factory, 
459                             GTK_STOCK_INDENT,
460                             GTK_STOCK_INDENT "-ltr",
461                             GTK_STOCK_INDENT "-rtl");
462   register_bidi_stock_icon (factory, 
463                             GTK_STOCK_UNINDENT,
464                             GTK_STOCK_UNINDENT "-ltr",
465                             GTK_STOCK_UNINDENT "-rtl");
466   register_stock_icon (factory, GTK_STOCK_GOTO_TOP);
467   register_stock_icon (factory, GTK_STOCK_DELETE);
468   register_bidi_stock_icon (factory, 
469                             GTK_STOCK_UNDELETE,
470                             GTK_STOCK_UNDELETE "-ltr",
471                             GTK_STOCK_UNDELETE "-rtl");
472   register_bidi_stock_icon (factory, 
473                             GTK_STOCK_UNDO,
474                             GTK_STOCK_UNDO "-ltr",
475                             GTK_STOCK_UNDO "-rtl");
476   register_stock_icon (factory, GTK_STOCK_GO_UP);
477   register_stock_icon (factory, GTK_STOCK_FILE);
478   register_stock_icon (factory, GTK_STOCK_DIRECTORY);
479   register_stock_icon (factory, GTK_STOCK_ABOUT);
480   register_stock_icon (factory, GTK_STOCK_CONNECT);
481   register_stock_icon (factory, GTK_STOCK_DISCONNECT);
482   register_stock_icon (factory, GTK_STOCK_EDIT);
483   register_bidi_stock_icon (factory, 
484                             GTK_STOCK_MEDIA_FORWARD,
485                             GTK_STOCK_MEDIA_FORWARD "-ltr",
486                             GTK_STOCK_MEDIA_FORWARD "-rtl");
487   register_bidi_stock_icon (factory, 
488                             GTK_STOCK_MEDIA_NEXT,
489                             GTK_STOCK_MEDIA_NEXT "-ltr",
490                             GTK_STOCK_MEDIA_NEXT "-rtl");
491   register_stock_icon (factory, GTK_STOCK_MEDIA_PAUSE);
492   register_bidi_stock_icon (factory, 
493                             GTK_STOCK_MEDIA_PLAY,
494                             GTK_STOCK_MEDIA_PLAY "-ltr",
495                             GTK_STOCK_MEDIA_PLAY "-rtl");
496   register_bidi_stock_icon (factory, 
497                             GTK_STOCK_MEDIA_PREVIOUS,
498                             GTK_STOCK_MEDIA_PREVIOUS "-ltr",
499                             GTK_STOCK_MEDIA_PREVIOUS "-rtl");
500   register_stock_icon (factory, GTK_STOCK_MEDIA_RECORD);
501   register_bidi_stock_icon (factory, 
502                             GTK_STOCK_MEDIA_REWIND,
503                             GTK_STOCK_MEDIA_REWIND "-ltr",
504                             GTK_STOCK_MEDIA_REWIND "-rtl");
505   register_stock_icon (factory, GTK_STOCK_MEDIA_STOP);
506   register_stock_icon (factory, GTK_STOCK_INDEX);
507   register_stock_icon (factory, GTK_STOCK_ZOOM_100);
508   register_stock_icon (factory, GTK_STOCK_ZOOM_IN);
509   register_stock_icon (factory, GTK_STOCK_ZOOM_OUT);
510   register_stock_icon (factory, GTK_STOCK_ZOOM_FIT);
511   register_stock_icon (factory, GTK_STOCK_SELECT_ALL);
512   register_stock_icon (factory, GTK_STOCK_CLEAR);
513   register_stock_icon (factory, GTK_STOCK_SELECT_COLOR);
514   register_stock_icon (factory, GTK_STOCK_COLOR_PICKER);
515 }
516
517 /************************************************************
518  *                    Icon size handling                    *
519  ************************************************************/
520
521 typedef struct _IconSize IconSize;
522
523 struct _IconSize
524 {
525   gint size;
526   gchar *name;
527   
528   gint width;
529   gint height;
530 };
531
532 typedef struct _IconAlias IconAlias;
533
534 struct _IconAlias
535 {
536   gchar *name;
537   gint   target;
538 };
539
540 typedef struct _SettingsIconSize SettingsIconSize;
541
542 struct _SettingsIconSize
543 {
544   gint width;
545   gint height;
546 };
547
548 static GHashTable *icon_aliases = NULL;
549 static IconSize *icon_sizes = NULL;
550 static gint      icon_sizes_allocated = 0;
551 static gint      icon_sizes_used = 0;
552
553 static void
554 init_icon_sizes (void)
555 {
556   if (icon_sizes == NULL)
557     {
558 #define NUM_BUILTIN_SIZES 7
559       gint i;
560
561       icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
562       
563       icon_sizes = g_new (IconSize, NUM_BUILTIN_SIZES);
564       icon_sizes_allocated = NUM_BUILTIN_SIZES;
565       icon_sizes_used = NUM_BUILTIN_SIZES;
566
567       icon_sizes[GTK_ICON_SIZE_INVALID].size = 0;
568       icon_sizes[GTK_ICON_SIZE_INVALID].name = NULL;
569       icon_sizes[GTK_ICON_SIZE_INVALID].width = 0;
570       icon_sizes[GTK_ICON_SIZE_INVALID].height = 0;
571
572       /* the name strings aren't copied since we don't ever remove
573        * icon sizes, so we don't need to know whether they're static.
574        * Even if we did I suppose removing the builtin sizes would be
575        * disallowed.
576        */
577       
578       icon_sizes[GTK_ICON_SIZE_MENU].size = GTK_ICON_SIZE_MENU;
579       icon_sizes[GTK_ICON_SIZE_MENU].name = "gtk-menu";
580       icon_sizes[GTK_ICON_SIZE_MENU].width = 16;
581       icon_sizes[GTK_ICON_SIZE_MENU].height = 16;
582
583       icon_sizes[GTK_ICON_SIZE_BUTTON].size = GTK_ICON_SIZE_BUTTON;
584       icon_sizes[GTK_ICON_SIZE_BUTTON].name = "gtk-button";
585       icon_sizes[GTK_ICON_SIZE_BUTTON].width = 20;
586       icon_sizes[GTK_ICON_SIZE_BUTTON].height = 20;
587
588       icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].size = GTK_ICON_SIZE_SMALL_TOOLBAR;
589       icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].name = "gtk-small-toolbar";
590       icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].width = 18;
591       icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].height = 18;
592       
593       icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].size = GTK_ICON_SIZE_LARGE_TOOLBAR;
594       icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].name = "gtk-large-toolbar";
595       icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].width = 24;
596       icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].height = 24;
597
598       icon_sizes[GTK_ICON_SIZE_DND].size = GTK_ICON_SIZE_DND;
599       icon_sizes[GTK_ICON_SIZE_DND].name = "gtk-dnd";
600       icon_sizes[GTK_ICON_SIZE_DND].width = 32;
601       icon_sizes[GTK_ICON_SIZE_DND].height = 32;
602
603       icon_sizes[GTK_ICON_SIZE_DIALOG].size = GTK_ICON_SIZE_DIALOG;
604       icon_sizes[GTK_ICON_SIZE_DIALOG].name = "gtk-dialog";
605       icon_sizes[GTK_ICON_SIZE_DIALOG].width = 48;
606       icon_sizes[GTK_ICON_SIZE_DIALOG].height = 48;
607
608       g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
609
610       /* Alias everything to itself. */
611       i = 1; /* skip invalid size */
612       while (i < NUM_BUILTIN_SIZES)
613         {
614           gtk_icon_size_register_alias (icon_sizes[i].name, icon_sizes[i].size);
615           
616           ++i;
617         }
618       
619 #undef NUM_BUILTIN_SIZES
620     }
621 }
622
623 static void
624 free_settings_sizes (gpointer data)
625 {
626   g_array_free (data, TRUE);
627 }
628
629 static GArray *
630 get_settings_sizes (GtkSettings *settings,
631                     gboolean    *created)
632 {
633   GArray *settings_sizes;
634   static GQuark sizes_quark = 0;
635
636   if (!sizes_quark)
637     sizes_quark = g_quark_from_static_string ("gtk-icon-sizes");
638
639   settings_sizes = g_object_get_qdata (G_OBJECT (settings), sizes_quark);
640   if (!settings_sizes)
641     {
642       settings_sizes = g_array_new (FALSE, FALSE, sizeof (SettingsIconSize));
643       g_object_set_qdata_full (G_OBJECT (settings), sizes_quark,
644                                settings_sizes, free_settings_sizes);
645       if (created)
646         *created = TRUE;
647     }
648
649   return settings_sizes;
650 }
651
652 static void
653 icon_size_set_for_settings (GtkSettings *settings,
654                             const gchar *size_name,
655                             gint         width,
656                             gint         height)
657 {
658   GtkIconSize size;
659   GArray *settings_sizes;
660   SettingsIconSize *settings_size;
661
662   g_return_if_fail (size_name != NULL);
663
664   size = gtk_icon_size_from_name (size_name);
665   if (size == GTK_ICON_SIZE_INVALID)
666     /* Reserve a place */
667     size = icon_size_register_intern (size_name, -1, -1);
668   
669   settings_sizes = get_settings_sizes (settings, NULL);
670   if (size >= settings_sizes->len)
671     {
672       SettingsIconSize unset = { -1, -1 };
673       gint i;
674
675       for (i = settings_sizes->len; i <= size; i++)
676         g_array_append_val (settings_sizes, unset);
677     }
678
679   settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
680   
681   settings_size->width = width;
682   settings_size->height = height;
683 }
684
685 /* Like pango_parse_word, but accept - as well
686  */
687 static gboolean
688 scan_icon_size_name (const char **pos, GString *out)
689 {
690   const char *p = *pos;
691
692   while (g_ascii_isspace (*p))
693     p++;
694   
695   if (!((*p >= 'A' && *p <= 'Z') ||
696         (*p >= 'a' && *p <= 'z') ||
697         *p == '_' || *p == '-'))
698     return FALSE;
699
700   g_string_truncate (out, 0);
701   g_string_append_c (out, *p);
702   p++;
703
704   while ((*p >= 'A' && *p <= 'Z') ||
705          (*p >= 'a' && *p <= 'z') ||
706          (*p >= '0' && *p <= '9') ||
707          *p == '_' || *p == '-')
708     {
709       g_string_append_c (out, *p);
710       p++;
711     }
712
713   *pos = p;
714
715   return TRUE;
716 }
717
718 static void
719 icon_size_setting_parse (GtkSettings *settings,
720                          const gchar *icon_size_string)
721 {
722   GString *name_buf = g_string_new (NULL);
723   const gchar *p = icon_size_string;
724
725   while (pango_skip_space (&p))
726     {
727       gint width, height;
728       
729       if (!scan_icon_size_name (&p, name_buf))
730         goto err;
731
732       if (!pango_skip_space (&p))
733         goto err;
734
735       if (*p != '=')
736         goto err;
737
738       p++;
739
740       if (!pango_scan_int (&p, &width))
741         goto err;
742
743       if (!pango_skip_space (&p))
744         goto err;
745
746       if (*p != ',')
747         goto err;
748
749       p++;
750
751       if (!pango_scan_int (&p, &height))
752         goto err;
753
754       if (width > 0 && height > 0)
755         {
756           icon_size_set_for_settings (settings, name_buf->str,
757                                       width, height);
758         }
759       else
760         {
761           g_warning ("Invalid size in gtk-icon-sizes: %d,%d\n", width, height);
762         }
763
764       pango_skip_space (&p);
765       if (*p == '\0')
766         break;
767       if (*p == ':')
768         p++;
769       else
770         goto err;
771     }
772
773   g_string_free (name_buf, TRUE);
774   return;
775
776  err:
777   g_warning ("Error parsing gtk-icon-sizes string:\n\t'%s'", icon_size_string);
778   g_string_free (name_buf, TRUE);
779 }
780
781 static void
782 icon_size_set_all_from_settings (GtkSettings *settings)
783 {
784   GArray *settings_sizes;
785   gchar *icon_size_string;
786
787   /* Reset old settings */
788   settings_sizes = get_settings_sizes (settings, NULL);
789   g_array_set_size (settings_sizes, 0);
790
791   g_object_get (settings,
792                 "gtk-icon-sizes", &icon_size_string,
793                 NULL);
794
795   if (icon_size_string)
796     {
797       icon_size_setting_parse (settings, icon_size_string);
798       g_free (icon_size_string);
799     }
800 }
801
802 static void
803 icon_size_settings_changed (GtkSettings  *settings,
804                             GParamSpec   *pspec)
805 {
806   icon_size_set_all_from_settings (settings);
807
808   gtk_rc_reset_styles (settings);
809 }
810
811 static void
812 icon_sizes_init_for_settings (GtkSettings *settings)
813 {
814   g_signal_connect (settings,
815                     "notify::gtk-icon-sizes",
816                     G_CALLBACK (icon_size_settings_changed),
817                     NULL);
818   
819   icon_size_set_all_from_settings (settings);
820 }
821      
822 static gboolean
823 icon_size_lookup_intern (GtkSettings *settings,
824                          GtkIconSize  size,
825                          gint        *widthp,
826                          gint        *heightp)
827 {
828   GArray *settings_sizes;
829   gint width_for_settings = -1;
830   gint height_for_settings = -1;
831   
832   init_icon_sizes ();
833
834   if (size == (GtkIconSize)-1)
835     return FALSE;
836
837   if (size >= icon_sizes_used)
838     return FALSE;
839
840   if (size == GTK_ICON_SIZE_INVALID)
841     return FALSE;
842
843   if (settings)
844     {
845       gboolean initial = FALSE;
846       
847       settings_sizes = get_settings_sizes (settings, &initial);
848       if (initial)
849         icon_sizes_init_for_settings (settings);
850   
851       if (size < settings_sizes->len)
852         {
853           SettingsIconSize *settings_size;
854           
855           settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
856           
857           width_for_settings = settings_size->width;
858           height_for_settings = settings_size->height;
859         }
860     }
861
862   if (widthp)
863     *widthp = width_for_settings >= 0 ? width_for_settings : icon_sizes[size].width;
864
865   if (heightp)
866     *heightp = height_for_settings >= 0 ? height_for_settings : icon_sizes[size].height;
867
868   return TRUE;
869 }
870
871 /**
872  * gtk_icon_size_lookup_for_settings:
873  * @settings: a #GtkSettings object, used to determine
874  *   which set of user preferences to used.
875  * @size: an icon size
876  * @width: location to store icon width
877  * @height: location to store icon height
878  *
879  * Obtains the pixel size of a semantic icon size, possibly
880  * modified by user preferences for a particular 
881  * #GtkSettings. Normally @size would be
882  * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc.  This function
883  * isn't normally needed, gtk_widget_render_icon() is the usual
884  * way to get an icon for rendering, then just look at the size of
885  * the rendered pixbuf. The rendered pixbuf may not even correspond to
886  * the width/height returned by gtk_icon_size_lookup(), because themes
887  * are free to render the pixbuf however they like, including changing
888  * the usual size.
889  * 
890  * Return value: %TRUE if @size was a valid size
891  *
892  * Since: 2.2
893  **/
894 gboolean
895 gtk_icon_size_lookup_for_settings (GtkSettings *settings,
896                                    GtkIconSize  size,
897                                    gint        *width,
898                                    gint        *height)
899 {
900   g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
901
902   return icon_size_lookup_intern (settings, size, width, height);
903 }
904
905 /**
906  * gtk_icon_size_lookup:
907  * @size: an icon size
908  * @width: location to store icon width
909  * @height: location to store icon height
910  *
911  * Obtains the pixel size of a semantic icon size, possibly
912  * modified by user preferences for the default #GtkSettings.
913  * (See gtk_icon_size_lookup_for_settings().)
914  * Normally @size would be
915  * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc.  This function
916  * isn't normally needed, gtk_widget_render_icon() is the usual
917  * way to get an icon for rendering, then just look at the size of
918  * the rendered pixbuf. The rendered pixbuf may not even correspond to
919  * the width/height returned by gtk_icon_size_lookup(), because themes
920  * are free to render the pixbuf however they like, including changing
921  * the usual size.
922  * 
923  * Return value: %TRUE if @size was a valid size
924  **/
925 gboolean
926 gtk_icon_size_lookup (GtkIconSize  size,
927                       gint        *widthp,
928                       gint        *heightp)
929 {
930   GTK_NOTE (MULTIHEAD,
931             g_warning ("gtk_icon_size_lookup ()) is not multihead safe"));
932
933   return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (),
934                                             size, widthp, heightp);
935 }
936
937 static GtkIconSize
938 icon_size_register_intern (const gchar *name,
939                            gint         width,
940                            gint         height)
941 {
942   IconAlias *old_alias;
943   GtkIconSize size;
944   
945   init_icon_sizes ();
946
947   old_alias = g_hash_table_lookup (icon_aliases, name);
948   if (old_alias && icon_sizes[old_alias->target].width > 0)
949     {
950       g_warning ("Icon size name '%s' already exists", name);
951       return GTK_ICON_SIZE_INVALID;
952     }
953
954   if (old_alias)
955     {
956       size = old_alias->target;
957     }
958   else
959     {
960       if (icon_sizes_used == icon_sizes_allocated)
961         {
962           icon_sizes_allocated *= 2;
963           icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated);
964         }
965
966       size = icon_sizes_used++;
967
968       /* alias to self. */
969       gtk_icon_size_register_alias (name, size);
970
971       icon_sizes[size].size = size;
972       icon_sizes[size].name = g_strdup (name);
973     }
974
975   icon_sizes[size].width = width;
976   icon_sizes[size].height = height;
977
978   return size;
979 }
980
981 /**
982  * gtk_icon_size_register:
983  * @name: name of the icon size
984  * @width: the icon width
985  * @height: the icon height
986  *
987  * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
988  * etc. Returns the integer value for the size.
989  *
990  * Returns: integer value representing the size
991  * 
992  **/
993 GtkIconSize
994 gtk_icon_size_register (const gchar *name,
995                         gint         width,
996                         gint         height)
997 {
998   g_return_val_if_fail (name != NULL, 0);
999   g_return_val_if_fail (width > 0, 0);
1000   g_return_val_if_fail (height > 0, 0);
1001   
1002   return icon_size_register_intern (name, width, height);
1003 }
1004
1005 /**
1006  * gtk_icon_size_register_alias:
1007  * @alias: an alias for @target
1008  * @target: an existing icon size
1009  *
1010  * Registers @alias as another name for @target.
1011  * So calling gtk_icon_size_from_name() with @alias as argument
1012  * will return @target.
1013  *
1014  **/
1015 void
1016 gtk_icon_size_register_alias (const gchar *alias,
1017                               GtkIconSize  target)
1018 {
1019   IconAlias *ia;
1020   
1021   g_return_if_fail (alias != NULL);
1022
1023   init_icon_sizes ();
1024
1025   if (!icon_size_lookup_intern (NULL, target, NULL, NULL))
1026     g_warning ("gtk_icon_size_register_alias: Icon size %u does not exist", target);
1027
1028   ia = g_hash_table_lookup (icon_aliases, alias);
1029   if (ia)
1030     {
1031       if (icon_sizes[ia->target].width > 0)
1032         {
1033           g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias);
1034           return;
1035         }
1036
1037       ia->target = target;
1038     }
1039
1040   if (!ia)
1041     {
1042       ia = g_new (IconAlias, 1);
1043       ia->name = g_strdup (alias);
1044       ia->target = target;
1045
1046       g_hash_table_insert (icon_aliases, ia->name, ia);
1047     }
1048 }
1049
1050 /** 
1051  * gtk_icon_size_from_name:
1052  * @name: the name to look up.
1053  * @returns: the icon size with the given name.
1054  * 
1055  * Looks up the icon size associated with @name.
1056  **/
1057 GtkIconSize
1058 gtk_icon_size_from_name (const gchar *name)
1059 {
1060   IconAlias *ia;
1061
1062   init_icon_sizes ();
1063   
1064   ia = g_hash_table_lookup (icon_aliases, name);
1065
1066   if (ia && icon_sizes[ia->target].width > 0)
1067     return ia->target;
1068   else
1069     return GTK_ICON_SIZE_INVALID;
1070 }
1071
1072 /**
1073  * gtk_icon_size_get_name:
1074  * @size: a #GtkIconSize.
1075  * @returns: the name of the given icon size.
1076  * 
1077  * Gets the canonical name of the given icon size. The returned string 
1078  * is statically allocated and should not be freed.
1079  **/
1080 G_CONST_RETURN gchar*
1081 gtk_icon_size_get_name (GtkIconSize  size)
1082 {
1083   if (size >= icon_sizes_used)
1084     return NULL;
1085   else
1086     return icon_sizes[size].name;
1087 }
1088
1089 /************************************************************/
1090
1091 /* Icon Set */
1092
1093
1094 static GdkPixbuf *find_in_cache     (GtkIconSet       *icon_set,
1095                                      GtkStyle         *style,
1096                                      GtkTextDirection  direction,
1097                                      GtkStateType      state,
1098                                      GtkIconSize       size);
1099 static void       add_to_cache      (GtkIconSet       *icon_set,
1100                                      GtkStyle         *style,
1101                                      GtkTextDirection  direction,
1102                                      GtkStateType      state,
1103                                      GtkIconSize       size,
1104                                      GdkPixbuf        *pixbuf);
1105 /* Clear icon set contents, drop references to all contained
1106  * GdkPixbuf objects and forget all GtkIconSources. Used to
1107  * recycle an icon set.
1108  */
1109 static void       clear_cache       (GtkIconSet       *icon_set,
1110                                      gboolean          style_detach);
1111 static GSList*    copy_cache        (GtkIconSet       *icon_set,
1112                                      GtkIconSet       *copy_recipient);
1113 static void       attach_to_style   (GtkIconSet       *icon_set,
1114                                      GtkStyle         *style);
1115 static void       detach_from_style (GtkIconSet       *icon_set,
1116                                      GtkStyle         *style);
1117 static void       style_dnotify     (gpointer          data);
1118
1119 struct _GtkIconSet
1120 {
1121   guint ref_count;
1122
1123   GSList *sources;
1124
1125   /* Cache of the last few rendered versions of the icon. */
1126   GSList *cache;
1127
1128   guint cache_size;
1129
1130   guint cache_serial;
1131 };
1132
1133 static guint cache_serial = 0;
1134
1135 /**
1136  * gtk_icon_set_new:
1137  * 
1138  * Creates a new #GtkIconSet. A #GtkIconSet represents a single icon
1139  * in various sizes and widget states. It can provide a #GdkPixbuf
1140  * for a given size and state on request, and automatically caches
1141  * some of the rendered #GdkPixbuf objects.
1142  *
1143  * Normally you would use gtk_widget_render_icon() instead of
1144  * using #GtkIconSet directly. The one case where you'd use
1145  * #GtkIconSet is to create application-specific icon sets to place in
1146  * a #GtkIconFactory.
1147  * 
1148  * Return value: a new #GtkIconSet
1149  **/
1150 GtkIconSet*
1151 gtk_icon_set_new (void)
1152 {
1153   GtkIconSet *icon_set;
1154
1155   icon_set = g_new (GtkIconSet, 1);
1156
1157   icon_set->ref_count = 1;
1158   icon_set->sources = NULL;
1159   icon_set->cache = NULL;
1160   icon_set->cache_size = 0;
1161   icon_set->cache_serial = cache_serial;
1162   
1163   return icon_set;
1164 }
1165
1166 /**
1167  * gtk_icon_set_new_from_pixbuf:
1168  * @pixbuf: a #GdkPixbuf
1169  * 
1170  * Creates a new #GtkIconSet with @pixbuf as the default/fallback
1171  * source image. If you don't add any additional #GtkIconSource to the
1172  * icon set, all variants of the icon will be created from @pixbuf,
1173  * using scaling, pixelation, etc. as required to adjust the icon size
1174  * or make the icon look insensitive/prelighted.
1175  * 
1176  * Return value: a new #GtkIconSet
1177  **/
1178 GtkIconSet *
1179 gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
1180 {
1181   GtkIconSet *set;
1182
1183   GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1184
1185   g_return_val_if_fail (pixbuf != NULL, NULL);
1186
1187   set = gtk_icon_set_new ();
1188
1189   gtk_icon_source_set_pixbuf (&source, pixbuf);
1190   gtk_icon_set_add_source (set, &source);
1191   gtk_icon_source_set_pixbuf (&source, NULL);
1192   
1193   return set;
1194 }
1195
1196
1197 /**
1198  * gtk_icon_set_ref:
1199  * @icon_set: a #GtkIconSet.
1200  * 
1201  * Increments the reference count on @icon_set.
1202  * 
1203  * Return value: @icon_set.
1204  **/
1205 GtkIconSet*
1206 gtk_icon_set_ref (GtkIconSet *icon_set)
1207 {
1208   g_return_val_if_fail (icon_set != NULL, NULL);
1209   g_return_val_if_fail (icon_set->ref_count > 0, NULL);
1210
1211   icon_set->ref_count += 1;
1212
1213   return icon_set;
1214 }
1215
1216 /**
1217  * gtk_icon_set_unref:
1218  * @icon_set: a #GtkIconSet
1219  * 
1220  * Decrements the reference count on @icon_set, and frees memory
1221  * if the reference count reaches 0.
1222  **/
1223 void
1224 gtk_icon_set_unref (GtkIconSet *icon_set)
1225 {
1226   g_return_if_fail (icon_set != NULL);
1227   g_return_if_fail (icon_set->ref_count > 0);
1228
1229   icon_set->ref_count -= 1;
1230
1231   if (icon_set->ref_count == 0)
1232     {
1233       GSList *tmp_list = icon_set->sources;
1234       while (tmp_list != NULL)
1235         {
1236           gtk_icon_source_free (tmp_list->data);
1237
1238           tmp_list = g_slist_next (tmp_list);
1239         }
1240       g_slist_free (icon_set->sources);
1241
1242       clear_cache (icon_set, TRUE);
1243
1244       g_free (icon_set);
1245     }
1246 }
1247
1248 GType
1249 gtk_icon_set_get_type (void)
1250 {
1251   static GType our_type = 0;
1252   
1253   if (our_type == 0)
1254     our_type = g_boxed_type_register_static (I_("GtkIconSet"),
1255                                              (GBoxedCopyFunc) gtk_icon_set_ref,
1256                                              (GBoxedFreeFunc) gtk_icon_set_unref);
1257
1258   return our_type;
1259 }
1260
1261 /**
1262  * gtk_icon_set_copy:
1263  * @icon_set: a #GtkIconSet
1264  * 
1265  * Copies @icon_set by value. 
1266  * 
1267  * Return value: a new #GtkIconSet identical to the first.
1268  **/
1269 GtkIconSet*
1270 gtk_icon_set_copy (GtkIconSet *icon_set)
1271 {
1272   GtkIconSet *copy;
1273   GSList *tmp_list;
1274   
1275   copy = gtk_icon_set_new ();
1276
1277   tmp_list = icon_set->sources;
1278   while (tmp_list != NULL)
1279     {
1280       copy->sources = g_slist_prepend (copy->sources,
1281                                        gtk_icon_source_copy (tmp_list->data));
1282
1283       tmp_list = g_slist_next (tmp_list);
1284     }
1285
1286   copy->sources = g_slist_reverse (copy->sources);
1287
1288   copy->cache = copy_cache (icon_set, copy);
1289   copy->cache_size = icon_set->cache_size;
1290   copy->cache_serial = icon_set->cache_serial;
1291   
1292   return copy;
1293 }
1294
1295 static gboolean
1296 sizes_equivalent (GtkIconSize lhs,
1297                   GtkIconSize rhs)
1298 {
1299   /* We used to consider sizes equivalent if they were
1300    * the same pixel size, but we don't have the GtkSettings
1301    * here, so we can't do that. Plus, it's not clear that
1302    * it is right... it was just a workaround for the fact
1303    * that we register icons by logical size, not pixel size.
1304    */
1305 #if 1
1306   return lhs == rhs;
1307 #else  
1308   
1309   gint r_w, r_h, l_w, l_h;
1310
1311   icon_size_lookup_intern (NULL, rhs, &r_w, &r_h);
1312   icon_size_lookup_intern (NULL, lhs, &l_w, &l_h);
1313
1314   return r_w == l_w && r_h == l_h;
1315 #endif
1316 }
1317
1318 static GtkIconSource *
1319 find_best_matching_source (GtkIconSet       *icon_set,
1320                            GtkTextDirection  direction,
1321                            GtkStateType      state,
1322                            GtkIconSize       size,
1323                            GSList           *failed)
1324 {
1325   GtkIconSource *source;
1326   GSList *tmp_list;
1327   
1328   /* We need to find the best icon source.  Direction matters more
1329    * than state, state matters more than size. icon_set->sources
1330    * is sorted according to wildness, so if we take the first
1331    * match we find it will be the least-wild match (if there are
1332    * multiple matches for a given "wildness" then the RC file contained
1333    * dumb stuff, and we end up with an arbitrary matching source)
1334    */
1335   
1336   source = NULL;
1337   tmp_list = icon_set->sources;
1338   while (tmp_list != NULL)
1339     {
1340       GtkIconSource *s = tmp_list->data;
1341       
1342       if ((s->any_direction || (s->direction == direction)) &&
1343           (s->any_state || (s->state == state)) &&
1344           (s->any_size || size == (GtkIconSize)-1 || (sizes_equivalent (size, s->size))))
1345         {
1346           if (!g_slist_find (failed, s))
1347             {
1348               source = s;
1349               break;
1350             }
1351         }
1352           
1353       tmp_list = g_slist_next (tmp_list);
1354     }
1355
1356   return source;
1357 }
1358   
1359 static gboolean
1360 ensure_filename_pixbuf (GtkIconSet    *icon_set,
1361                         GtkIconSource *source)
1362 {
1363   if (source->filename_pixbuf == NULL)
1364     {
1365       GError *error = NULL;
1366       
1367       source->filename_pixbuf = gdk_pixbuf_new_from_file (source->source.filename, &error);
1368       
1369       if (source->filename_pixbuf == NULL)
1370         {
1371           /* Remove this icon source so we don't keep trying to
1372            * load it.
1373            */
1374           g_warning (_("Error loading icon: %s"), error->message);
1375           g_error_free (error);
1376           
1377           icon_set->sources = g_slist_remove (icon_set->sources, source);
1378           
1379           gtk_icon_source_free (source);
1380
1381           return FALSE;
1382         }
1383     }
1384   
1385   return TRUE;
1386 }
1387  
1388 static GdkPixbuf *
1389 render_icon_name_pixbuf (GtkIconSource    *icon_source,
1390                          GtkStyle         *style,
1391                          GtkTextDirection  direction,
1392                          GtkStateType      state,
1393                          GtkIconSize       size,
1394                          GtkWidget         *widget,
1395                          const char        *detail)
1396 {
1397   GdkPixbuf *pixbuf;
1398   GdkPixbuf *tmp_pixbuf;
1399   GtkIconSource tmp_source;
1400   GdkScreen *screen;
1401   GtkIconTheme *icon_theme;
1402   GtkSettings *settings;
1403   gint width, height, pixel_size;
1404   gint *sizes, *s, dist;
1405   GError *error = NULL;
1406   
1407   if (widget && gtk_widget_has_screen (widget))
1408     screen = gtk_widget_get_screen (widget);
1409   else if (style && style->colormap)
1410     screen = gdk_colormap_get_screen (style->colormap);
1411   else
1412     {
1413       screen = gdk_screen_get_default ();
1414       GTK_NOTE (MULTIHEAD,
1415                 g_warning ("Using the default screen for gtk_icon_source_render_icon()"));
1416     }
1417
1418   icon_theme = gtk_icon_theme_get_for_screen (screen);
1419   settings = gtk_settings_get_for_screen (screen);
1420
1421   if (!gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
1422     {
1423       if (size == (GtkIconSize)-1)
1424         {
1425           /* Find an available size close to 48 
1426            */
1427           sizes = gtk_icon_theme_get_icon_sizes (icon_theme, icon_source->source.icon_name);
1428           dist = 1000;
1429           width = height = 48;
1430           for (s = sizes; *s; s++)
1431             {
1432               if (*s == -1)
1433                 {
1434                   width = height = 48;
1435                   break;
1436                 }
1437               if (*s < 48)
1438                 {
1439                   if (48 - *s < dist)
1440                     {
1441                       width = height = *s;
1442                       dist = 48 - *s;
1443                     }
1444                 }
1445               else 
1446                 {
1447                   if (*s - 48 < dist)
1448                     {
1449                       width = height = *s;
1450                       dist = *s - 48;
1451                     }
1452                 }
1453             }
1454           
1455           g_free (sizes);
1456         }
1457       else
1458         {
1459           g_warning ("Invalid icon size %u\n", size);
1460           width = height = 24;
1461         }
1462     }
1463
1464   pixel_size = MIN (width, height);
1465
1466   tmp_pixbuf = gtk_icon_theme_load_icon (icon_theme,
1467                                          icon_source->source.icon_name,
1468                                          pixel_size, 0,
1469                                          &error);
1470
1471   if (!tmp_pixbuf)
1472     {
1473       g_warning ("Error loading theme icon '%s' for stock: %s", 
1474                  icon_source->source.icon_name, error->message);
1475       g_error_free (error);
1476       return NULL;
1477     }
1478   
1479   tmp_source = *icon_source;
1480   tmp_source.type = GTK_ICON_SOURCE_PIXBUF;
1481   tmp_source.source.pixbuf = tmp_pixbuf;
1482
1483   pixbuf = gtk_style_render_icon (style, &tmp_source,
1484                                   direction, state, -1,
1485                                   widget, detail);
1486
1487   if (!pixbuf)
1488     g_warning ("Failed to render icon");
1489
1490   g_object_unref (tmp_pixbuf);
1491
1492   return pixbuf;
1493 }
1494
1495 static GdkPixbuf *
1496 find_and_render_icon_source (GtkIconSet       *icon_set,
1497                              GtkStyle         *style,
1498                              GtkTextDirection  direction,
1499                              GtkStateType      state,
1500                              GtkIconSize       size,
1501                              GtkWidget         *widget,
1502                              const char        *detail)
1503 {
1504   GSList *failed = NULL;
1505   GdkPixbuf *pixbuf = NULL;
1506
1507   /* We treat failure in two different ways:
1508    *
1509    *  A) If loading a source that specifies a filename fails,
1510    *     we treat that as permanent, and remove the source
1511    *     from the GtkIconSet. (in ensure_filename_pixbuf ()
1512    *  B) If loading a themed icon fails, or scaling an icon
1513    *     fails, we treat that as transient and will try
1514    *     again next time the icon falls out of the cache
1515    *     and we need to recreate it.
1516    */
1517   while (pixbuf == NULL)
1518     {
1519       GtkIconSource *source = find_best_matching_source (icon_set, direction, state, size, failed);
1520       
1521       if (source == NULL)
1522         break;
1523
1524       switch (source->type)
1525         {
1526         case GTK_ICON_SOURCE_FILENAME:
1527           if (!ensure_filename_pixbuf (icon_set, source))
1528             break;
1529           /* Fall through */
1530         case GTK_ICON_SOURCE_PIXBUF:
1531           pixbuf = gtk_style_render_icon (style, source,
1532                                           direction, state, size,
1533                                           widget, detail);
1534           if (!pixbuf)
1535             {
1536               g_warning ("Failed to render icon");
1537               failed = g_slist_prepend (failed, source);
1538             }
1539           break;
1540         case GTK_ICON_SOURCE_ICON_NAME:
1541         case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1542           pixbuf = render_icon_name_pixbuf (source, style,
1543                                             direction, state, size,
1544                                             widget, detail);
1545           if (!pixbuf)
1546             failed = g_slist_prepend (failed, source);
1547           break;
1548         case GTK_ICON_SOURCE_EMPTY:
1549           g_assert_not_reached ();
1550         }
1551     }
1552
1553   g_slist_free (failed);
1554
1555   return pixbuf;
1556 }
1557
1558 extern GtkIconCache *_builtin_cache;
1559
1560 static GdkPixbuf*
1561 render_fallback_image (GtkStyle          *style,
1562                        GtkTextDirection   direction,
1563                        GtkStateType       state,
1564                        GtkIconSize        size,
1565                        GtkWidget         *widget,
1566                        const char        *detail)
1567 {
1568   /* This icon can be used for any direction/state/size */
1569   static GtkIconSource fallback_source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1570
1571   if (fallback_source.type == GTK_ICON_SOURCE_EMPTY)
1572     {
1573       GdkPixbuf *pixbuf;
1574
1575       _gtk_icon_theme_ensure_builtin_cache ();
1576
1577       pixbuf = _gtk_icon_cache_get_icon (_builtin_cache,
1578                                          GTK_STOCK_MISSING_IMAGE,
1579                                          "24");
1580       gtk_icon_source_set_pixbuf (&fallback_source, pixbuf);
1581       g_object_unref (pixbuf);
1582     }
1583   
1584   return gtk_style_render_icon (style,
1585                                 &fallback_source,
1586                                 direction,
1587                                 state,
1588                                 size,
1589                                 widget,
1590                                 detail);
1591 }
1592
1593 /**
1594  * gtk_icon_set_render_icon:
1595  * @icon_set: a #GtkIconSet
1596  * @style: a #GtkStyle associated with @widget, or %NULL
1597  * @direction: text direction
1598  * @state: widget state
1599  * @size: icon size. A size of (GtkIconSize)-1
1600  *        means render at the size of the source and don't scale.
1601  * @widget: widget that will display the icon, or %NULL.
1602  *          The only use that is typically made of this
1603  *          is to determine the appropriate #GdkScreen.
1604  * @detail: detail to pass to the theme engine, or %NULL.
1605  *          Note that passing a detail of anything but %NULL
1606  *          will disable caching.
1607  * 
1608  * Renders an icon using gtk_style_render_icon(). In most cases,
1609  * gtk_widget_render_icon() is better, since it automatically provides
1610  * most of the arguments from the current widget settings.  This
1611  * function never returns %NULL; if the icon can't be rendered
1612  * (perhaps because an image file fails to load), a default "missing
1613  * image" icon will be returned instead.
1614  * 
1615  * Return value: a #GdkPixbuf to be displayed
1616  **/
1617 GdkPixbuf*
1618 gtk_icon_set_render_icon (GtkIconSet        *icon_set,
1619                           GtkStyle          *style,
1620                           GtkTextDirection   direction,
1621                           GtkStateType       state,
1622                           GtkIconSize        size,
1623                           GtkWidget         *widget,
1624                           const char        *detail)
1625 {
1626   GdkPixbuf *icon;
1627   
1628   g_return_val_if_fail (icon_set != NULL, NULL);
1629   g_return_val_if_fail (style == NULL || GTK_IS_STYLE (style), NULL);
1630
1631   if (icon_set->sources == NULL)
1632     return render_fallback_image (style, direction, state, size, widget, detail);
1633
1634   if (detail == NULL)
1635     {
1636       icon = find_in_cache (icon_set, style, direction,
1637                         state, size);
1638       
1639       if (icon)
1640         {
1641           g_object_ref (icon);
1642           return icon;
1643         }
1644     }
1645
1646
1647   icon = find_and_render_icon_source (icon_set, style, direction, state, size,
1648                                       widget, detail);
1649
1650   if (icon == NULL)
1651     icon = render_fallback_image (style, direction, state, size, widget, detail);
1652
1653   if (detail == NULL)
1654     add_to_cache (icon_set, style, direction, state, size, icon);
1655   
1656   return icon;
1657 }
1658
1659 /* Order sources by their "wildness", so that "wilder" sources are
1660  * greater than "specific" sources; for determining ordering,
1661  * direction beats state beats size.
1662  */
1663
1664 static int
1665 icon_source_compare (gconstpointer ap, gconstpointer bp)
1666 {
1667   const GtkIconSource *a = ap;
1668   const GtkIconSource *b = bp;
1669
1670   if (!a->any_direction && b->any_direction)
1671     return -1;
1672   else if (a->any_direction && !b->any_direction)
1673     return 1;
1674   else if (!a->any_state && b->any_state)
1675     return -1;
1676   else if (a->any_state && !b->any_state)
1677     return 1;
1678   else if (!a->any_size && b->any_size)
1679     return -1;
1680   else if (a->any_size && !b->any_size)
1681     return 1;
1682   else
1683     return 0;
1684 }
1685
1686 /**
1687  * gtk_icon_set_add_source:
1688  * @icon_set: a #GtkIconSet
1689  * @source: a #GtkIconSource
1690  *
1691  * Icon sets have a list of #GtkIconSource, which they use as base
1692  * icons for rendering icons in different states and sizes. Icons are
1693  * scaled, made to look insensitive, etc. in
1694  * gtk_icon_set_render_icon(), but #GtkIconSet needs base images to
1695  * work with. The base images and when to use them are described by
1696  * a #GtkIconSource.
1697  * 
1698  * This function copies @source, so you can reuse the same source immediately
1699  * without affecting the icon set.
1700  *
1701  * An example of when you'd use this function: a web browser's "Back
1702  * to Previous Page" icon might point in a different direction in
1703  * Hebrew and in English; it might look different when insensitive;
1704  * and it might change size depending on toolbar mode (small/large
1705  * icons). So a single icon set would contain all those variants of
1706  * the icon, and you might add a separate source for each one.
1707  *
1708  * You should nearly always add a "default" icon source with all
1709  * fields wildcarded, which will be used as a fallback if no more
1710  * specific source matches. #GtkIconSet always prefers more specific
1711  * icon sources to more generic icon sources. The order in which you
1712  * add the sources to the icon set does not matter.
1713  *
1714  * gtk_icon_set_new_from_pixbuf() creates a new icon set with a
1715  * default icon source based on the given pixbuf.
1716  * 
1717  **/
1718 void
1719 gtk_icon_set_add_source (GtkIconSet *icon_set,
1720                          const GtkIconSource *source)
1721 {
1722   g_return_if_fail (icon_set != NULL);
1723   g_return_if_fail (source != NULL);
1724
1725   if (source->type == GTK_ICON_SOURCE_EMPTY)
1726     {
1727       g_warning ("Useless empty GtkIconSource");
1728       return;
1729     }
1730   
1731   icon_set->sources = g_slist_insert_sorted (icon_set->sources,
1732                                              gtk_icon_source_copy (source),
1733                                              icon_source_compare);
1734 }
1735
1736 /**
1737  * gtk_icon_set_get_sizes:
1738  * @icon_set: a #GtkIconSet
1739  * @sizes: return location for array of sizes
1740  * @n_sizes: location to store number of elements in returned array
1741  *
1742  * Obtains a list of icon sizes this icon set can render. The returned
1743  * array must be freed with g_free().
1744  * 
1745  **/
1746 void
1747 gtk_icon_set_get_sizes (GtkIconSet   *icon_set,
1748                         GtkIconSize **sizes,
1749                         gint         *n_sizes)
1750 {
1751   GSList *tmp_list;
1752   gboolean all_sizes = FALSE;
1753   GSList *specifics = NULL;
1754   
1755   g_return_if_fail (icon_set != NULL);
1756   g_return_if_fail (sizes != NULL);
1757   g_return_if_fail (n_sizes != NULL);
1758   
1759   tmp_list = icon_set->sources;
1760   while (tmp_list != NULL)
1761     {
1762       GtkIconSource *source;
1763
1764       source = tmp_list->data;
1765
1766       if (source->any_size)
1767         {
1768           all_sizes = TRUE;
1769           break;
1770         }
1771       else
1772         specifics = g_slist_prepend (specifics, GINT_TO_POINTER (source->size));
1773       
1774       tmp_list = g_slist_next (tmp_list);
1775     }
1776
1777   if (all_sizes)
1778     {
1779       /* Need to find out what sizes exist */
1780       gint i;
1781
1782       init_icon_sizes ();
1783       
1784       *sizes = g_new (GtkIconSize, icon_sizes_used);
1785       *n_sizes = icon_sizes_used - 1;
1786       
1787       i = 1;      
1788       while (i < icon_sizes_used)
1789         {
1790           (*sizes)[i - 1] = icon_sizes[i].size;
1791           ++i;
1792         }
1793     }
1794   else
1795     {
1796       gint i;
1797       
1798       *n_sizes = g_slist_length (specifics);
1799       *sizes = g_new (GtkIconSize, *n_sizes);
1800
1801       i = 0;
1802       tmp_list = specifics;
1803       while (tmp_list != NULL)
1804         {
1805           (*sizes)[i] = GPOINTER_TO_INT (tmp_list->data);
1806
1807           ++i;
1808           tmp_list = g_slist_next (tmp_list);
1809         }
1810     }
1811
1812   g_slist_free (specifics);
1813 }
1814
1815
1816 /**
1817  * gtk_icon_source_new:
1818  * 
1819  * Creates a new #GtkIconSource. A #GtkIconSource contains a #GdkPixbuf (or
1820  * image filename) that serves as the base image for one or more of the
1821  * icons in a #GtkIconSet, along with a specification for which icons in the
1822  * icon set will be based on that pixbuf or image file. An icon set contains
1823  * a set of icons that represent "the same" logical concept in different states,
1824  * different global text directions, and different sizes.
1825  * 
1826  * So for example a web browser's "Back to Previous Page" icon might
1827  * point in a different direction in Hebrew and in English; it might
1828  * look different when insensitive; and it might change size depending
1829  * on toolbar mode (small/large icons). So a single icon set would
1830  * contain all those variants of the icon. #GtkIconSet contains a list
1831  * of #GtkIconSource from which it can derive specific icon variants in
1832  * the set. 
1833  *
1834  * In the simplest case, #GtkIconSet contains one source pixbuf from
1835  * which it derives all variants. The convenience function
1836  * gtk_icon_set_new_from_pixbuf() handles this case; if you only have
1837  * one source pixbuf, just use that function.
1838  *
1839  * If you want to use a different base pixbuf for different icon
1840  * variants, you create multiple icon sources, mark which variants
1841  * they'll be used to create, and add them to the icon set with
1842  * gtk_icon_set_add_source().
1843  *
1844  * By default, the icon source has all parameters wildcarded. That is,
1845  * the icon source will be used as the base icon for any desired text
1846  * direction, widget state, or icon size.
1847  * 
1848  * Return value: a new #GtkIconSource
1849  **/
1850 GtkIconSource*
1851 gtk_icon_source_new (void)
1852 {
1853   GtkIconSource *src;
1854   
1855   src = g_new0 (GtkIconSource, 1);
1856
1857   src->direction = GTK_TEXT_DIR_NONE;
1858   src->size = GTK_ICON_SIZE_INVALID;
1859   src->state = GTK_STATE_NORMAL;
1860   
1861   src->any_direction = TRUE;
1862   src->any_state = TRUE;
1863   src->any_size = TRUE;
1864   
1865   return src;
1866 }
1867
1868 /**
1869  * gtk_icon_source_copy:
1870  * @source: a #GtkIconSource
1871  * 
1872  * Creates a copy of @source; mostly useful for language bindings.
1873  * 
1874  * Return value: a new #GtkIconSource
1875  **/
1876 GtkIconSource*
1877 gtk_icon_source_copy (const GtkIconSource *source)
1878 {
1879   GtkIconSource *copy;
1880   
1881   g_return_val_if_fail (source != NULL, NULL);
1882
1883   copy = g_new (GtkIconSource, 1);
1884
1885   *copy = *source;
1886   
1887   switch (copy->type)
1888     {
1889     case GTK_ICON_SOURCE_EMPTY:
1890     case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1891       break;
1892     case GTK_ICON_SOURCE_ICON_NAME:
1893       copy->source.icon_name = g_strdup (copy->source.icon_name);
1894       break;
1895     case GTK_ICON_SOURCE_FILENAME:
1896       copy->source.filename = g_strdup (copy->source.filename);
1897 #ifdef G_OS_WIN32
1898       copy->cp_filename = g_strdup (copy->cp_filename);
1899 #endif
1900       if (copy->filename_pixbuf)
1901         g_object_ref (copy->filename_pixbuf);
1902       break;
1903     case GTK_ICON_SOURCE_PIXBUF:
1904       g_object_ref (copy->source.pixbuf);
1905       break;
1906     default:
1907       g_assert_not_reached();
1908     }
1909
1910   return copy;
1911 }
1912
1913 /**
1914  * gtk_icon_source_free:
1915  * @source: a #GtkIconSource
1916  * 
1917  * Frees a dynamically-allocated icon source, along with its
1918  * filename, size, and pixbuf fields if those are not %NULL.
1919  **/
1920 void
1921 gtk_icon_source_free (GtkIconSource *source)
1922 {
1923   g_return_if_fail (source != NULL);
1924
1925   icon_source_clear (source);
1926   g_free (source);
1927 }
1928
1929 GType
1930 gtk_icon_source_get_type (void)
1931 {
1932   static GType our_type = 0;
1933   
1934   if (our_type == 0)
1935     our_type = g_boxed_type_register_static (I_("GtkIconSource"),
1936                                              (GBoxedCopyFunc) gtk_icon_source_copy,
1937                                              (GBoxedFreeFunc) gtk_icon_source_free);
1938
1939   return our_type;
1940 }
1941
1942 static void
1943 icon_source_clear (GtkIconSource *source)
1944 {
1945   switch (source->type)
1946     {
1947     case GTK_ICON_SOURCE_EMPTY:
1948       break;
1949     case GTK_ICON_SOURCE_ICON_NAME:
1950       g_free (source->source.icon_name);
1951       /* fall thru */
1952     case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1953       source->source.icon_name = NULL;
1954       break;
1955     case GTK_ICON_SOURCE_FILENAME:
1956       g_free (source->source.filename);
1957       source->source.filename = NULL;
1958 #ifdef G_OS_WIN32
1959       g_free (source->cp_filename);
1960       source->cp_filename = NULL;
1961 #endif
1962       if (source->filename_pixbuf) 
1963         g_object_unref (source->filename_pixbuf);
1964       source->filename_pixbuf = NULL;
1965       break;
1966     case GTK_ICON_SOURCE_PIXBUF:
1967       g_object_unref (source->source.pixbuf);
1968       source->source.pixbuf = NULL;
1969       break;
1970     default:
1971       g_assert_not_reached();
1972     }
1973
1974   source->type = GTK_ICON_SOURCE_EMPTY;
1975 }
1976
1977 /**
1978  * gtk_icon_source_set_filename:
1979  * @source: a #GtkIconSource
1980  * @filename: image file to use
1981  *
1982  * Sets the name of an image file to use as a base image when creating
1983  * icon variants for #GtkIconSet. The filename must be absolute. 
1984  **/
1985 void
1986 gtk_icon_source_set_filename (GtkIconSource *source,
1987                               const gchar   *filename)
1988 {
1989   g_return_if_fail (source != NULL);
1990   g_return_if_fail (filename == NULL || g_path_is_absolute (filename));
1991
1992   if (source->type == GTK_ICON_SOURCE_FILENAME &&
1993       source->source.filename == filename)
1994     return;
1995   
1996   icon_source_clear (source);
1997   
1998   if (filename != NULL)
1999     {
2000       source->type = GTK_ICON_SOURCE_FILENAME;
2001       source->source.filename = g_strdup (filename);
2002 #ifdef G_OS_WIN32
2003       source->cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
2004 #endif
2005     }
2006 }
2007
2008 /**
2009  * gtk_icon_source_set_icon_name
2010  * @source: a #GtkIconSource
2011  * @icon_name: name of icon to use
2012  *
2013  * Sets the name of an icon to look up in the current icon theme
2014  * to use as a base image when creating icon variants for #GtkIconSet.
2015  **/
2016 void
2017 gtk_icon_source_set_icon_name (GtkIconSource *source,
2018                                const gchar   *icon_name)
2019 {
2020   g_return_if_fail (source != NULL);
2021
2022   if (source->type == GTK_ICON_SOURCE_ICON_NAME &&
2023       source->source.icon_name == icon_name)
2024     return;
2025
2026   icon_source_clear (source);
2027   
2028   if (icon_name != NULL)
2029     {
2030       source->type = GTK_ICON_SOURCE_ICON_NAME;
2031       source->source.icon_name = g_strdup (icon_name);
2032     }
2033 }
2034
2035 /**
2036  * gtk_icon_source_set_pixbuf:
2037  * @source: a #GtkIconSource
2038  * @pixbuf: pixbuf to use as a source
2039  *
2040  * Sets a pixbuf to use as a base image when creating icon variants
2041  * for #GtkIconSet.
2042  **/
2043 void
2044 gtk_icon_source_set_pixbuf (GtkIconSource *source,
2045                             GdkPixbuf     *pixbuf)
2046 {
2047   g_return_if_fail (source != NULL);
2048   g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
2049   
2050   if (source->type == GTK_ICON_SOURCE_PIXBUF &&
2051       source->source.pixbuf == pixbuf)
2052     return;
2053
2054   icon_source_clear (source);
2055   
2056   if (pixbuf != NULL)
2057     {
2058       source->type = GTK_ICON_SOURCE_PIXBUF;
2059       source->source.pixbuf = g_object_ref (pixbuf);
2060     }
2061 }
2062
2063 /**
2064  * gtk_icon_source_get_filename:
2065  * @source: a #GtkIconSource
2066  * 
2067  * Retrieves the source filename, or %NULL if none is set. The
2068  * filename is not a copy, and should not be modified or expected to
2069  * persist beyond the lifetime of the icon source.
2070  * 
2071  * Return value: image filename. This string must not be modified
2072  * or freed.
2073  **/
2074 G_CONST_RETURN gchar*
2075 gtk_icon_source_get_filename (const GtkIconSource *source)
2076 {
2077   g_return_val_if_fail (source != NULL, NULL);
2078
2079   if (source->type == GTK_ICON_SOURCE_FILENAME)
2080     return source->source.filename;
2081   else
2082     return NULL;
2083 }
2084
2085 /**
2086  * gtk_icon_source_get_icon_name:
2087  * @source: a #GtkIconSource
2088  * 
2089  * Retrieves the source icon name, or %NULL if none is set. The
2090  * icon_name is not a copy, and should not be modified or expected to
2091  * persist beyond the lifetime of the icon source.
2092  * 
2093  * Return value: icon name. This string must not be modified or freed.
2094  **/
2095 G_CONST_RETURN gchar*
2096 gtk_icon_source_get_icon_name (const GtkIconSource *source)
2097 {
2098   g_return_val_if_fail (source != NULL, NULL);
2099
2100   if (source->type == GTK_ICON_SOURCE_ICON_NAME ||
2101      source->type == GTK_ICON_SOURCE_STATIC_ICON_NAME)
2102     return source->source.icon_name;
2103   else
2104     return NULL;
2105 }
2106
2107 /**
2108  * gtk_icon_source_get_pixbuf:
2109  * @source: a #GtkIconSource
2110  * 
2111  * Retrieves the source pixbuf, or %NULL if none is set.
2112  * In addition, if a filename source is in use, this
2113  * function in some cases will return the pixbuf from
2114  * loaded from the filename. This is, for example, true
2115  * for the GtkIconSource passed to the GtkStyle::render_icon()
2116  * virtual function. The reference count on the pixbuf is
2117  * not incremented.
2118  * 
2119  * Return value: source pixbuf
2120  **/
2121 GdkPixbuf*
2122 gtk_icon_source_get_pixbuf (const GtkIconSource *source)
2123 {
2124   g_return_val_if_fail (source != NULL, NULL);
2125   
2126   if (source->type == GTK_ICON_SOURCE_PIXBUF)
2127     return source->source.pixbuf;
2128   else if (source->type == GTK_ICON_SOURCE_FILENAME)
2129     return source->filename_pixbuf;
2130   else
2131     return NULL;
2132 }
2133
2134 /**
2135  * gtk_icon_source_set_direction_wildcarded:
2136  * @source: a #GtkIconSource
2137  * @setting: %TRUE to wildcard the text direction
2138  *
2139  * If the text direction is wildcarded, this source can be used
2140  * as the base image for an icon in any #GtkTextDirection.
2141  * If the text direction is not wildcarded, then the
2142  * text direction the icon source applies to should be set
2143  * with gtk_icon_source_set_direction(), and the icon source
2144  * will only be used with that text direction.
2145  *
2146  * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2147  * wildcarded sources, and will use an exact match when possible.
2148  * 
2149  **/
2150 void
2151 gtk_icon_source_set_direction_wildcarded (GtkIconSource *source,
2152                                           gboolean       setting)
2153 {
2154   g_return_if_fail (source != NULL);
2155
2156   source->any_direction = setting != FALSE;
2157 }
2158
2159 /**
2160  * gtk_icon_source_set_state_wildcarded:
2161  * @source: a #GtkIconSource
2162  * @setting: %TRUE to wildcard the widget state
2163  *
2164  * If the widget state is wildcarded, this source can be used as the
2165  * base image for an icon in any #GtkStateType.  If the widget state
2166  * is not wildcarded, then the state the source applies to should be
2167  * set with gtk_icon_source_set_state() and the icon source will
2168  * only be used with that specific state.
2169  *
2170  * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2171  * wildcarded sources, and will use an exact match when possible.
2172  *
2173  * #GtkIconSet will normally transform wildcarded source images to
2174  * produce an appropriate icon for a given state, for example
2175  * lightening an image on prelight, but will not modify source images
2176  * that match exactly.
2177  **/
2178 void
2179 gtk_icon_source_set_state_wildcarded (GtkIconSource *source,
2180                                       gboolean       setting)
2181 {
2182   g_return_if_fail (source != NULL);
2183
2184   source->any_state = setting != FALSE;
2185 }
2186
2187
2188 /**
2189  * gtk_icon_source_set_size_wildcarded:
2190  * @source: a #GtkIconSource
2191  * @setting: %TRUE to wildcard the widget state
2192  *
2193  * If the icon size is wildcarded, this source can be used as the base
2194  * image for an icon of any size.  If the size is not wildcarded, then
2195  * the size the source applies to should be set with
2196  * gtk_icon_source_set_size() and the icon source will only be used
2197  * with that specific size.
2198  *
2199  * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2200  * wildcarded sources, and will use an exact match when possible.
2201  *
2202  * #GtkIconSet will normally scale wildcarded source images to produce
2203  * an appropriate icon at a given size, but will not change the size
2204  * of source images that match exactly.
2205  **/
2206 void
2207 gtk_icon_source_set_size_wildcarded (GtkIconSource *source,
2208                                      gboolean       setting)
2209 {
2210   g_return_if_fail (source != NULL);
2211
2212   source->any_size = setting != FALSE;  
2213 }
2214
2215 /**
2216  * gtk_icon_source_get_size_wildcarded:
2217  * @source: a #GtkIconSource
2218  * 
2219  * Gets the value set by gtk_icon_source_set_size_wildcarded().
2220  * 
2221  * Return value: %TRUE if this icon source is a base for any icon size variant
2222  **/
2223 gboolean
2224 gtk_icon_source_get_size_wildcarded (const GtkIconSource *source)
2225 {
2226   g_return_val_if_fail (source != NULL, TRUE);
2227   
2228   return source->any_size;
2229 }
2230
2231 /**
2232  * gtk_icon_source_get_state_wildcarded:
2233  * @source: a #GtkIconSource
2234  * 
2235  * Gets the value set by gtk_icon_source_set_state_wildcarded().
2236  * 
2237  * Return value: %TRUE if this icon source is a base for any widget state variant
2238  **/
2239 gboolean
2240 gtk_icon_source_get_state_wildcarded (const GtkIconSource *source)
2241 {
2242   g_return_val_if_fail (source != NULL, TRUE);
2243
2244   return source->any_state;
2245 }
2246
2247 /**
2248  * gtk_icon_source_get_direction_wildcarded:
2249  * @source: a #GtkIconSource
2250  * 
2251  * Gets the value set by gtk_icon_source_set_direction_wildcarded().
2252  * 
2253  * Return value: %TRUE if this icon source is a base for any text direction variant
2254  **/
2255 gboolean
2256 gtk_icon_source_get_direction_wildcarded (const GtkIconSource *source)
2257 {
2258   g_return_val_if_fail (source != NULL, TRUE);
2259
2260   return source->any_direction;
2261 }
2262
2263 /**
2264  * gtk_icon_source_set_direction:
2265  * @source: a #GtkIconSource
2266  * @direction: text direction this source applies to
2267  *
2268  * Sets the text direction this icon source is intended to be used
2269  * with.
2270  * 
2271  * Setting the text direction on an icon source makes no difference
2272  * if the text direction is wildcarded. Therefore, you should usually
2273  * call gtk_icon_source_set_direction_wildcarded() to un-wildcard it
2274  * in addition to calling this function.
2275  * 
2276  **/
2277 void
2278 gtk_icon_source_set_direction (GtkIconSource   *source,
2279                                GtkTextDirection direction)
2280 {
2281   g_return_if_fail (source != NULL);
2282
2283   source->direction = direction;
2284 }
2285
2286 /**
2287  * gtk_icon_source_set_state:
2288  * @source: a #GtkIconSource
2289  * @state: widget state this source applies to
2290  *
2291  * Sets the widget state this icon source is intended to be used
2292  * with.
2293  * 
2294  * Setting the widget state on an icon source makes no difference
2295  * if the state is wildcarded. Therefore, you should usually
2296  * call gtk_icon_source_set_state_wildcarded() to un-wildcard it
2297  * in addition to calling this function.
2298  * 
2299  **/
2300 void
2301 gtk_icon_source_set_state (GtkIconSource *source,
2302                            GtkStateType   state)
2303 {
2304   g_return_if_fail (source != NULL);
2305
2306   source->state = state;
2307 }
2308
2309 /**
2310  * gtk_icon_source_set_size:
2311  * @source: a #GtkIconSource
2312  * @size: icon size this source applies to
2313  *
2314  * Sets the icon size this icon source is intended to be used
2315  * with.
2316  * 
2317  * Setting the icon size on an icon source makes no difference
2318  * if the size is wildcarded. Therefore, you should usually
2319  * call gtk_icon_source_set_size_wildcarded() to un-wildcard it
2320  * in addition to calling this function.
2321  * 
2322  **/
2323 void
2324 gtk_icon_source_set_size (GtkIconSource *source,
2325                           GtkIconSize    size)
2326 {
2327   g_return_if_fail (source != NULL);
2328
2329   source->size = size;
2330 }
2331
2332 /**
2333  * gtk_icon_source_get_direction:
2334  * @source: a #GtkIconSource
2335  * 
2336  * Obtains the text direction this icon source applies to. The return
2337  * value is only useful/meaningful if the text direction is <emphasis>not</emphasis> 
2338  * wildcarded.
2339  * 
2340  * Return value: text direction this source matches
2341  **/
2342 GtkTextDirection
2343 gtk_icon_source_get_direction (const GtkIconSource *source)
2344 {
2345   g_return_val_if_fail (source != NULL, 0);
2346
2347   return source->direction;
2348 }
2349
2350 /**
2351  * gtk_icon_source_get_state:
2352  * @source: a #GtkIconSource
2353  * 
2354  * Obtains the widget state this icon source applies to. The return
2355  * value is only useful/meaningful if the widget state is <emphasis>not</emphasis>
2356  * wildcarded.
2357  * 
2358  * Return value: widget state this source matches
2359  **/
2360 GtkStateType
2361 gtk_icon_source_get_state (const GtkIconSource *source)
2362 {
2363   g_return_val_if_fail (source != NULL, 0);
2364
2365   return source->state;
2366 }
2367
2368 /**
2369  * gtk_icon_source_get_size:
2370  * @source: a #GtkIconSource
2371  * 
2372  * Obtains the icon size this source applies to. The return value
2373  * is only useful/meaningful if the icon size is <emphasis>not</emphasis> wildcarded.
2374  * 
2375  * Return value: icon size this source matches.
2376  **/
2377 GtkIconSize
2378 gtk_icon_source_get_size (const GtkIconSource *source)
2379 {
2380   g_return_val_if_fail (source != NULL, 0);
2381
2382   return source->size;
2383 }
2384
2385 #define NUM_CACHED_ICONS 8
2386
2387 typedef struct _CachedIcon CachedIcon;
2388
2389 struct _CachedIcon
2390 {
2391   /* These must all match to use the cached pixbuf.
2392    * If any don't match, we must re-render the pixbuf.
2393    */
2394   GtkStyle *style;
2395   GtkTextDirection direction;
2396   GtkStateType state;
2397   GtkIconSize size;
2398
2399   GdkPixbuf *pixbuf;
2400 };
2401
2402 static void
2403 ensure_cache_up_to_date (GtkIconSet *icon_set)
2404 {
2405   if (icon_set->cache_serial != cache_serial)
2406     {
2407       clear_cache (icon_set, TRUE);
2408       icon_set->cache_serial = cache_serial;
2409     }
2410 }
2411
2412 static void
2413 cached_icon_free (CachedIcon *icon)
2414 {
2415   g_object_unref (icon->pixbuf);
2416
2417   if (icon->style)
2418     g_object_unref (icon->style);
2419
2420   g_free (icon);
2421 }
2422
2423 static GdkPixbuf *
2424 find_in_cache (GtkIconSet      *icon_set,
2425                GtkStyle        *style,
2426                GtkTextDirection direction,
2427                GtkStateType     state,
2428                GtkIconSize      size)
2429 {
2430   GSList *tmp_list;
2431   GSList *prev;
2432
2433   ensure_cache_up_to_date (icon_set);
2434   
2435   prev = NULL;
2436   tmp_list = icon_set->cache;
2437   while (tmp_list != NULL)
2438     {
2439       CachedIcon *icon = tmp_list->data;
2440
2441       if (icon->style == style &&
2442           icon->direction == direction &&
2443           icon->state == state &&
2444           (size == (GtkIconSize)-1 || icon->size == size))
2445         {
2446           if (prev)
2447             {
2448               /* Move this icon to the front of the list. */
2449               prev->next = tmp_list->next;
2450               tmp_list->next = icon_set->cache;
2451               icon_set->cache = tmp_list;
2452             }
2453           
2454           return icon->pixbuf;
2455         }
2456           
2457       prev = tmp_list;
2458       tmp_list = g_slist_next (tmp_list);
2459     }
2460
2461   return NULL;
2462 }
2463
2464 static void
2465 add_to_cache (GtkIconSet      *icon_set,
2466               GtkStyle        *style,
2467               GtkTextDirection direction,
2468               GtkStateType     state,
2469               GtkIconSize      size,
2470               GdkPixbuf       *pixbuf)
2471 {
2472   CachedIcon *icon;
2473
2474   ensure_cache_up_to_date (icon_set);
2475
2476   g_object_ref (pixbuf);
2477
2478   /* We have to ref the style, since if the style was finalized
2479    * its address could be reused by another style, creating a
2480    * really weird bug
2481    */
2482   
2483   if (style)
2484     g_object_ref (style);
2485
2486   icon = g_new (CachedIcon, 1);
2487   icon_set->cache = g_slist_prepend (icon_set->cache, icon);
2488   icon_set->cache_size++;
2489
2490   icon->style = style;
2491   icon->direction = direction;
2492   icon->state = state;
2493   icon->size = size;
2494   icon->pixbuf = pixbuf;
2495
2496   if (icon->style)
2497     attach_to_style (icon_set, icon->style);
2498   
2499   if (icon_set->cache_size >= NUM_CACHED_ICONS)
2500     {
2501       /* Remove oldest item in the cache */
2502       GSList *tmp_list;
2503       
2504       tmp_list = icon_set->cache;
2505
2506       /* Find next-to-last link */
2507       g_assert (NUM_CACHED_ICONS > 2);
2508       while (tmp_list->next->next)
2509         tmp_list = tmp_list->next;
2510
2511       g_assert (tmp_list != NULL);
2512       g_assert (tmp_list->next != NULL);
2513       g_assert (tmp_list->next->next == NULL);
2514
2515       /* Free the last icon */
2516       icon = tmp_list->next->data;
2517
2518       g_slist_free (tmp_list->next);
2519       tmp_list->next = NULL;
2520
2521       cached_icon_free (icon);
2522     }
2523 }
2524
2525 static void
2526 clear_cache (GtkIconSet *icon_set,
2527              gboolean    style_detach)
2528 {
2529   GSList *cache, *tmp_list;
2530   GtkStyle *last_style = NULL;
2531
2532   cache = icon_set->cache;
2533   icon_set->cache = NULL;
2534   icon_set->cache_size = 0;
2535   tmp_list = cache;
2536   while (tmp_list != NULL)
2537     {
2538       CachedIcon *icon = tmp_list->data;
2539
2540       if (style_detach)
2541         {
2542           /* simple optimization for the case where the cache
2543            * contains contiguous icons from the same style.
2544            * it's safe to call detach_from_style more than
2545            * once on the same style though.
2546            */
2547           if (last_style != icon->style)
2548             {
2549               detach_from_style (icon_set, icon->style);
2550               last_style = icon->style;
2551             }
2552         }
2553       
2554       cached_icon_free (icon);      
2555       
2556       tmp_list = g_slist_next (tmp_list);
2557     }
2558
2559   g_slist_free (cache);
2560 }
2561
2562 static GSList*
2563 copy_cache (GtkIconSet *icon_set,
2564             GtkIconSet *copy_recipient)
2565 {
2566   GSList *tmp_list;
2567   GSList *copy = NULL;
2568
2569   ensure_cache_up_to_date (icon_set);
2570   
2571   tmp_list = icon_set->cache;
2572   while (tmp_list != NULL)
2573     {
2574       CachedIcon *icon = tmp_list->data;
2575       CachedIcon *icon_copy = g_new (CachedIcon, 1);
2576
2577       *icon_copy = *icon;
2578
2579       if (icon_copy->style)
2580         {
2581           attach_to_style (copy_recipient, icon_copy->style);
2582           g_object_ref (icon_copy->style);
2583         }
2584         
2585       g_object_ref (icon_copy->pixbuf);
2586
2587       icon_copy->size = icon->size;
2588       
2589       copy = g_slist_prepend (copy, icon_copy);      
2590       
2591       tmp_list = g_slist_next (tmp_list);
2592     }
2593
2594   return g_slist_reverse (copy);
2595 }
2596
2597 static void
2598 attach_to_style (GtkIconSet *icon_set,
2599                  GtkStyle   *style)
2600 {
2601   GHashTable *table;
2602
2603   table = g_object_get_qdata (G_OBJECT (style),
2604                               g_quark_try_string ("gtk-style-icon-sets"));
2605
2606   if (table == NULL)
2607     {
2608       table = g_hash_table_new (NULL, NULL);
2609       g_object_set_qdata_full (G_OBJECT (style),
2610                                g_quark_from_static_string ("gtk-style-icon-sets"),
2611                                table,
2612                                style_dnotify);
2613     }
2614
2615   g_hash_table_insert (table, icon_set, icon_set);
2616 }
2617
2618 static void
2619 detach_from_style (GtkIconSet *icon_set,
2620                    GtkStyle   *style)
2621 {
2622   GHashTable *table;
2623
2624   table = g_object_get_qdata (G_OBJECT (style),
2625                               g_quark_try_string ("gtk-style-icon-sets"));
2626
2627   if (table != NULL)
2628     g_hash_table_remove (table, icon_set);
2629 }
2630
2631 static void
2632 iconsets_foreach (gpointer key,
2633                   gpointer value,
2634                   gpointer user_data)
2635 {
2636   GtkIconSet *icon_set = key;
2637
2638   /* We only need to remove cache entries for the given style;
2639    * but that complicates things because in destroy notify
2640    * we don't know which style got destroyed, and 95% of the
2641    * time all cache entries will have the same style,
2642    * so this is faster anyway.
2643    */
2644   
2645   clear_cache (icon_set, FALSE);
2646 }
2647
2648 static void
2649 style_dnotify (gpointer data)
2650 {
2651   GHashTable *table = data;
2652   
2653   g_hash_table_foreach (table, iconsets_foreach, NULL);
2654
2655   g_hash_table_destroy (table);
2656 }
2657
2658 /* This allows the icon set to detect that its cache is out of date. */
2659 void
2660 _gtk_icon_set_invalidate_caches (void)
2661 {
2662   ++cache_serial;
2663 }
2664
2665 /**
2666  * _gtk_icon_factory_list_ids:
2667  * 
2668  * Gets all known IDs stored in an existing icon factory.
2669  * The strings in the returned list aren't copied.
2670  * The list itself should be freed.
2671  * 
2672  * Return value: List of ids in icon factories
2673  **/
2674 GList*
2675 _gtk_icon_factory_list_ids (void)
2676 {
2677   GSList *tmp_list;
2678   GList *ids;
2679
2680   ids = NULL;
2681
2682   _gtk_icon_factory_ensure_default_icons ();
2683   
2684   tmp_list = all_icon_factories;
2685   while (tmp_list != NULL)
2686     {
2687       GList *these_ids;
2688       
2689       GtkIconFactory *factory = GTK_ICON_FACTORY (tmp_list->data);
2690
2691       these_ids = g_hash_table_get_keys (factory->icons);
2692       
2693       ids = g_list_concat (ids, these_ids);
2694       
2695       tmp_list = g_slist_next (tmp_list);
2696     }
2697
2698   return ids;
2699 }
2700
2701 #ifdef G_OS_WIN32
2702
2703 /* DLL ABI stability backward compatibility versions */
2704
2705 #undef gtk_icon_source_set_filename
2706
2707 void
2708 gtk_icon_source_set_filename (GtkIconSource *source,
2709                               const gchar   *filename)
2710 {
2711   gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2712
2713   gtk_icon_source_set_filename_utf8 (source, utf8_filename);
2714
2715   g_free (utf8_filename);
2716 }
2717
2718 #undef gtk_icon_source_get_filename
2719
2720 G_CONST_RETURN gchar*
2721 gtk_icon_source_get_filename (const GtkIconSource *source)
2722 {
2723   g_return_val_if_fail (source != NULL, NULL);
2724
2725   if (source->type == GTK_ICON_SOURCE_FILENAME)
2726     return source->cp_filename;
2727   else
2728     return NULL;
2729 }
2730
2731 #endif
2732
2733 #define __GTK_ICON_FACTORY_C__
2734 #include "gtkaliasdef.c"