]> Pileus Git - ~andy/gtk/blob - gtk/gtkiconfactory.c
Export this privately.
[~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 >= icon_sizes_used)
835     return FALSE;
836
837   if (size == GTK_ICON_SIZE_INVALID)
838     return FALSE;
839
840   if (settings)
841     {
842       gboolean initial = FALSE;
843       
844       settings_sizes = get_settings_sizes (settings, &initial);
845       if (initial)
846         icon_sizes_init_for_settings (settings);
847   
848       if (size < settings_sizes->len)
849         {
850           SettingsIconSize *settings_size;
851           
852           settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
853           
854           width_for_settings = settings_size->width;
855           height_for_settings = settings_size->height;
856         }
857     }
858
859   if (widthp)
860     *widthp = width_for_settings >= 0 ? width_for_settings : icon_sizes[size].width;
861
862   if (heightp)
863     *heightp = height_for_settings >= 0 ? height_for_settings : icon_sizes[size].height;
864
865   return TRUE;
866 }
867
868 /**
869  * gtk_icon_size_lookup_for_settings:
870  * @settings: a #GtkSettings object, used to determine
871  *   which set of user preferences to used.
872  * @size: an icon size
873  * @width: location to store icon width
874  * @height: location to store icon height
875  *
876  * Obtains the pixel size of a semantic icon size, possibly
877  * modified by user preferences for a particular 
878  * #GtkSettings. Normally @size would be
879  * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc.  This function
880  * isn't normally needed, gtk_widget_render_icon() is the usual
881  * way to get an icon for rendering, then just look at the size of
882  * the rendered pixbuf. The rendered pixbuf may not even correspond to
883  * the width/height returned by gtk_icon_size_lookup(), because themes
884  * are free to render the pixbuf however they like, including changing
885  * the usual size.
886  * 
887  * Return value: %TRUE if @size was a valid size
888  *
889  * Since: 2.2
890  **/
891 gboolean
892 gtk_icon_size_lookup_for_settings (GtkSettings *settings,
893                                    GtkIconSize  size,
894                                    gint        *width,
895                                    gint        *height)
896 {
897   g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
898
899   return icon_size_lookup_intern (settings, size, width, height);
900 }
901
902 /**
903  * gtk_icon_size_lookup:
904  * @size: an icon size
905  * @width: location to store icon width
906  * @height: location to store icon height
907  *
908  * Obtains the pixel size of a semantic icon size, possibly
909  * modified by user preferences for the default #GtkSettings.
910  * (See gtk_icon_size_lookup_for_settings().)
911  * Normally @size would be
912  * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc.  This function
913  * isn't normally needed, gtk_widget_render_icon() is the usual
914  * way to get an icon for rendering, then just look at the size of
915  * the rendered pixbuf. The rendered pixbuf may not even correspond to
916  * the width/height returned by gtk_icon_size_lookup(), because themes
917  * are free to render the pixbuf however they like, including changing
918  * the usual size.
919  * 
920  * Return value: %TRUE if @size was a valid size
921  **/
922 gboolean
923 gtk_icon_size_lookup (GtkIconSize  size,
924                       gint        *widthp,
925                       gint        *heightp)
926 {
927   GTK_NOTE (MULTIHEAD,
928             g_warning ("gtk_icon_size_lookup ()) is not multihead safe"));
929
930   return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (),
931                                             size, widthp, heightp);
932 }
933
934 static GtkIconSize
935 icon_size_register_intern (const gchar *name,
936                            gint         width,
937                            gint         height)
938 {
939   IconAlias *old_alias;
940   GtkIconSize size;
941   
942   init_icon_sizes ();
943
944   old_alias = g_hash_table_lookup (icon_aliases, name);
945   if (old_alias && icon_sizes[old_alias->target].width > 0)
946     {
947       g_warning ("Icon size name '%s' already exists", name);
948       return GTK_ICON_SIZE_INVALID;
949     }
950
951   if (old_alias)
952     {
953       size = old_alias->target;
954     }
955   else
956     {
957       if (icon_sizes_used == icon_sizes_allocated)
958         {
959           icon_sizes_allocated *= 2;
960           icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated);
961         }
962
963       size = icon_sizes_used++;
964
965       /* alias to self. */
966       gtk_icon_size_register_alias (name, size);
967
968       icon_sizes[size].size = size;
969       icon_sizes[size].name = g_strdup (name);
970     }
971
972   icon_sizes[size].width = width;
973   icon_sizes[size].height = height;
974
975   return size;
976 }
977
978 /**
979  * gtk_icon_size_register:
980  * @name: name of the icon size
981  * @width: the icon width
982  * @height: the icon height
983  *
984  * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
985  * etc. Returns the integer value for the size.
986  *
987  * Returns: integer value representing the size
988  * 
989  **/
990 GtkIconSize
991 gtk_icon_size_register (const gchar *name,
992                         gint         width,
993                         gint         height)
994 {
995   g_return_val_if_fail (name != NULL, 0);
996   g_return_val_if_fail (width > 0, 0);
997   g_return_val_if_fail (height > 0, 0);
998   
999   return icon_size_register_intern (name, width, height);
1000 }
1001
1002 /**
1003  * gtk_icon_size_register_alias:
1004  * @alias: an alias for @target
1005  * @target: an existing icon size
1006  *
1007  * Registers @alias as another name for @target.
1008  * So calling gtk_icon_size_from_name() with @alias as argument
1009  * will return @target.
1010  *
1011  **/
1012 void
1013 gtk_icon_size_register_alias (const gchar *alias,
1014                               GtkIconSize  target)
1015 {
1016   IconAlias *ia;
1017   
1018   g_return_if_fail (alias != NULL);
1019
1020   init_icon_sizes ();
1021
1022   if (!icon_size_lookup_intern (NULL, target, NULL, NULL))
1023     g_warning ("gtk_icon_size_register_alias: Icon size %d does not exist", target);
1024
1025   ia = g_hash_table_lookup (icon_aliases, alias);
1026   if (ia)
1027     {
1028       if (icon_sizes[ia->target].width > 0)
1029         {
1030           g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias);
1031           return;
1032         }
1033
1034       ia->target = target;
1035     }
1036
1037   if (!ia)
1038     {
1039       ia = g_new (IconAlias, 1);
1040       ia->name = g_strdup (alias);
1041       ia->target = target;
1042
1043       g_hash_table_insert (icon_aliases, ia->name, ia);
1044     }
1045 }
1046
1047 /** 
1048  * gtk_icon_size_from_name:
1049  * @name: the name to look up.
1050  * @returns: the icon size with the given name.
1051  * 
1052  * Looks up the icon size associated with @name.
1053  **/
1054 GtkIconSize
1055 gtk_icon_size_from_name (const gchar *name)
1056 {
1057   IconAlias *ia;
1058
1059   init_icon_sizes ();
1060   
1061   ia = g_hash_table_lookup (icon_aliases, name);
1062
1063   if (ia && icon_sizes[ia->target].width > 0)
1064     return ia->target;
1065   else
1066     return GTK_ICON_SIZE_INVALID;
1067 }
1068
1069 /**
1070  * gtk_icon_size_get_name:
1071  * @size: a #GtkIconSize.
1072  * @returns: the name of the given icon size.
1073  * 
1074  * Gets the canonical name of the given icon size. The returned string 
1075  * is statically allocated and should not be freed.
1076  **/
1077 G_CONST_RETURN gchar*
1078 gtk_icon_size_get_name (GtkIconSize  size)
1079 {
1080   if (size >= icon_sizes_used)
1081     return NULL;
1082   else
1083     return icon_sizes[size].name;
1084 }
1085
1086 /************************************************************/
1087
1088 /* Icon Set */
1089
1090
1091 static GdkPixbuf *find_in_cache     (GtkIconSet       *icon_set,
1092                                      GtkStyle         *style,
1093                                      GtkTextDirection  direction,
1094                                      GtkStateType      state,
1095                                      GtkIconSize       size);
1096 static void       add_to_cache      (GtkIconSet       *icon_set,
1097                                      GtkStyle         *style,
1098                                      GtkTextDirection  direction,
1099                                      GtkStateType      state,
1100                                      GtkIconSize       size,
1101                                      GdkPixbuf        *pixbuf);
1102 /* Clear icon set contents, drop references to all contained
1103  * GdkPixbuf objects and forget all GtkIconSources. Used to
1104  * recycle an icon set.
1105  */
1106 static void       clear_cache       (GtkIconSet       *icon_set,
1107                                      gboolean          style_detach);
1108 static GSList*    copy_cache        (GtkIconSet       *icon_set,
1109                                      GtkIconSet       *copy_recipient);
1110 static void       attach_to_style   (GtkIconSet       *icon_set,
1111                                      GtkStyle         *style);
1112 static void       detach_from_style (GtkIconSet       *icon_set,
1113                                      GtkStyle         *style);
1114 static void       style_dnotify     (gpointer          data);
1115
1116 struct _GtkIconSet
1117 {
1118   guint ref_count;
1119
1120   GSList *sources;
1121
1122   /* Cache of the last few rendered versions of the icon. */
1123   GSList *cache;
1124
1125   guint cache_size;
1126
1127   guint cache_serial;
1128 };
1129
1130 static guint cache_serial = 0;
1131
1132 /**
1133  * gtk_icon_set_new:
1134  * 
1135  * Creates a new #GtkIconSet. A #GtkIconSet represents a single icon
1136  * in various sizes and widget states. It can provide a #GdkPixbuf
1137  * for a given size and state on request, and automatically caches
1138  * some of the rendered #GdkPixbuf objects.
1139  *
1140  * Normally you would use gtk_widget_render_icon() instead of
1141  * using #GtkIconSet directly. The one case where you'd use
1142  * #GtkIconSet is to create application-specific icon sets to place in
1143  * a #GtkIconFactory.
1144  * 
1145  * Return value: a new #GtkIconSet
1146  **/
1147 GtkIconSet*
1148 gtk_icon_set_new (void)
1149 {
1150   GtkIconSet *icon_set;
1151
1152   icon_set = g_new (GtkIconSet, 1);
1153
1154   icon_set->ref_count = 1;
1155   icon_set->sources = NULL;
1156   icon_set->cache = NULL;
1157   icon_set->cache_size = 0;
1158   icon_set->cache_serial = cache_serial;
1159   
1160   return icon_set;
1161 }
1162
1163 /**
1164  * gtk_icon_set_new_from_pixbuf:
1165  * @pixbuf: a #GdkPixbuf
1166  * 
1167  * Creates a new #GtkIconSet with @pixbuf as the default/fallback
1168  * source image. If you don't add any additional #GtkIconSource to the
1169  * icon set, all variants of the icon will be created from @pixbuf,
1170  * using scaling, pixelation, etc. as required to adjust the icon size
1171  * or make the icon look insensitive/prelighted.
1172  * 
1173  * Return value: a new #GtkIconSet
1174  **/
1175 GtkIconSet *
1176 gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
1177 {
1178   GtkIconSet *set;
1179
1180   GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1181
1182   g_return_val_if_fail (pixbuf != NULL, NULL);
1183
1184   set = gtk_icon_set_new ();
1185
1186   gtk_icon_source_set_pixbuf (&source, pixbuf);
1187   gtk_icon_set_add_source (set, &source);
1188   gtk_icon_source_set_pixbuf (&source, NULL);
1189   
1190   return set;
1191 }
1192
1193
1194 /**
1195  * gtk_icon_set_ref:
1196  * @icon_set: a #GtkIconSet.
1197  * 
1198  * Increments the reference count on @icon_set.
1199  * 
1200  * Return value: @icon_set.
1201  **/
1202 GtkIconSet*
1203 gtk_icon_set_ref (GtkIconSet *icon_set)
1204 {
1205   g_return_val_if_fail (icon_set != NULL, NULL);
1206   g_return_val_if_fail (icon_set->ref_count > 0, NULL);
1207
1208   icon_set->ref_count += 1;
1209
1210   return icon_set;
1211 }
1212
1213 /**
1214  * gtk_icon_set_unref:
1215  * @icon_set: a #GtkIconSet
1216  * 
1217  * Decrements the reference count on @icon_set, and frees memory
1218  * if the reference count reaches 0.
1219  **/
1220 void
1221 gtk_icon_set_unref (GtkIconSet *icon_set)
1222 {
1223   g_return_if_fail (icon_set != NULL);
1224   g_return_if_fail (icon_set->ref_count > 0);
1225
1226   icon_set->ref_count -= 1;
1227
1228   if (icon_set->ref_count == 0)
1229     {
1230       GSList *tmp_list = icon_set->sources;
1231       while (tmp_list != NULL)
1232         {
1233           gtk_icon_source_free (tmp_list->data);
1234
1235           tmp_list = g_slist_next (tmp_list);
1236         }
1237       g_slist_free (icon_set->sources);
1238
1239       clear_cache (icon_set, TRUE);
1240
1241       g_free (icon_set);
1242     }
1243 }
1244
1245 GType
1246 gtk_icon_set_get_type (void)
1247 {
1248   static GType our_type = 0;
1249   
1250   if (our_type == 0)
1251     our_type = g_boxed_type_register_static (I_("GtkIconSet"),
1252                                              (GBoxedCopyFunc) gtk_icon_set_ref,
1253                                              (GBoxedFreeFunc) gtk_icon_set_unref);
1254
1255   return our_type;
1256 }
1257
1258 /**
1259  * gtk_icon_set_copy:
1260  * @icon_set: a #GtkIconSet
1261  * 
1262  * Copies @icon_set by value. 
1263  * 
1264  * Return value: a new #GtkIconSet identical to the first.
1265  **/
1266 GtkIconSet*
1267 gtk_icon_set_copy (GtkIconSet *icon_set)
1268 {
1269   GtkIconSet *copy;
1270   GSList *tmp_list;
1271   
1272   copy = gtk_icon_set_new ();
1273
1274   tmp_list = icon_set->sources;
1275   while (tmp_list != NULL)
1276     {
1277       copy->sources = g_slist_prepend (copy->sources,
1278                                        gtk_icon_source_copy (tmp_list->data));
1279
1280       tmp_list = g_slist_next (tmp_list);
1281     }
1282
1283   copy->sources = g_slist_reverse (copy->sources);
1284
1285   copy->cache = copy_cache (icon_set, copy);
1286   copy->cache_size = icon_set->cache_size;
1287   copy->cache_serial = icon_set->cache_serial;
1288   
1289   return copy;
1290 }
1291
1292 static gboolean
1293 sizes_equivalent (GtkIconSize lhs,
1294                   GtkIconSize rhs)
1295 {
1296   /* We used to consider sizes equivalent if they were
1297    * the same pixel size, but we don't have the GtkSettings
1298    * here, so we can't do that. Plus, it's not clear that
1299    * it is right... it was just a workaround for the fact
1300    * that we register icons by logical size, not pixel size.
1301    */
1302 #if 1
1303   return lhs == rhs;
1304 #else  
1305   
1306   gint r_w, r_h, l_w, l_h;
1307
1308   icon_size_lookup_intern (NULL, rhs, &r_w, &r_h);
1309   icon_size_lookup_intern (NULL, lhs, &l_w, &l_h);
1310
1311   return r_w == l_w && r_h == l_h;
1312 #endif
1313 }
1314
1315 static GtkIconSource *
1316 find_best_matching_source (GtkIconSet       *icon_set,
1317                            GtkTextDirection  direction,
1318                            GtkStateType      state,
1319                            GtkIconSize       size,
1320                            GSList           *failed)
1321 {
1322   GtkIconSource *source;
1323   GSList *tmp_list;
1324   
1325   /* We need to find the best icon source.  Direction matters more
1326    * than state, state matters more than size. icon_set->sources
1327    * is sorted according to wildness, so if we take the first
1328    * match we find it will be the least-wild match (if there are
1329    * multiple matches for a given "wildness" then the RC file contained
1330    * dumb stuff, and we end up with an arbitrary matching source)
1331    */
1332   
1333   source = NULL;
1334   tmp_list = icon_set->sources;
1335   while (tmp_list != NULL)
1336     {
1337       GtkIconSource *s = tmp_list->data;
1338       
1339       if ((s->any_direction || (s->direction == direction)) &&
1340           (s->any_state || (s->state == state)) &&
1341           (s->any_size || (sizes_equivalent (size, s->size))))
1342         {
1343           if (!g_slist_find (failed, s))
1344             {
1345               source = s;
1346               break;
1347             }
1348         }
1349           
1350       tmp_list = g_slist_next (tmp_list);
1351     }
1352
1353   return source;
1354 }
1355   
1356 static gboolean
1357 ensure_filename_pixbuf (GtkIconSet    *icon_set,
1358                         GtkIconSource *source)
1359 {
1360   if (source->filename_pixbuf == NULL)
1361     {
1362       GError *error = NULL;
1363       
1364       source->filename_pixbuf = gdk_pixbuf_new_from_file (source->source.filename, &error);
1365       
1366       if (source->filename_pixbuf == NULL)
1367         {
1368           /* Remove this icon source so we don't keep trying to
1369            * load it.
1370            */
1371           g_warning (_("Error loading icon: %s"), error->message);
1372           g_error_free (error);
1373           
1374           icon_set->sources = g_slist_remove (icon_set->sources, source);
1375           
1376           gtk_icon_source_free (source);
1377
1378           return FALSE;
1379         }
1380     }
1381   
1382   return TRUE;
1383 }
1384  
1385 static GdkPixbuf *
1386 render_icon_name_pixbuf (GtkIconSource    *icon_source,
1387                          GtkStyle         *style,
1388                          GtkTextDirection  direction,
1389                          GtkStateType      state,
1390                          GtkIconSize       size,
1391                          GtkWidget         *widget,
1392                          const char        *detail)
1393 {
1394   GdkPixbuf *pixbuf;
1395   GdkPixbuf *tmp_pixbuf;
1396   GtkIconSource tmp_source;
1397   GdkScreen *screen;
1398   GtkIconTheme *icon_theme;
1399   GtkSettings *settings;
1400   gint width, height, pixel_size;
1401   gint *sizes, *s, dist;
1402   GError *error = NULL;
1403   
1404   if (widget && gtk_widget_has_screen (widget))
1405     screen = gtk_widget_get_screen (widget);
1406   else if (style && style->colormap)
1407     screen = gdk_colormap_get_screen (style->colormap);
1408   else
1409     {
1410       screen = gdk_screen_get_default ();
1411       GTK_NOTE (MULTIHEAD,
1412                 g_warning ("Using the default screen for gtk_icon_source_render_icon()"));
1413     }
1414
1415   icon_theme = gtk_icon_theme_get_for_screen (screen);
1416   settings = gtk_settings_get_for_screen (screen);
1417
1418   if (!gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
1419     {
1420       if (size == -1)
1421         {
1422           /* Find an available size close to 48 
1423            */
1424           sizes = gtk_icon_theme_get_icon_sizes (icon_theme, icon_source->source.icon_name);
1425           dist = 1000;
1426           width = height = 48;
1427           for (s = sizes; *s; s++)
1428             {
1429               if (*s == -1)
1430                 {
1431                   width = height = 48;
1432                   break;
1433                 }
1434               if (*s < 48)
1435                 {
1436                   if (48 - *s < dist)
1437                     {
1438                       width = height = *s;
1439                       dist = 48 - *s;
1440                     }
1441                 }
1442               else 
1443                 {
1444                   if (*s - 48 < dist)
1445                     {
1446                       width = height = *s;
1447                       dist = *s - 48;
1448                     }
1449                 }
1450             }
1451           
1452           g_free (sizes);
1453         }
1454       else
1455         {
1456           g_warning ("Invalid icon size %d\n", size);
1457           width = height = 24;
1458         }
1459     }
1460
1461   pixel_size = MIN (width, height);
1462
1463   tmp_pixbuf = gtk_icon_theme_load_icon (icon_theme,
1464                                          icon_source->source.icon_name,
1465                                          pixel_size, 0,
1466                                          &error);
1467
1468   if (!tmp_pixbuf)
1469     {
1470       g_warning ("Error loading theme icon for stock: %s", error->message);
1471       g_error_free (error);
1472       return NULL;
1473     }
1474   
1475   tmp_source = *icon_source;
1476   tmp_source.type = GTK_ICON_SOURCE_PIXBUF;
1477   tmp_source.source.pixbuf = tmp_pixbuf;
1478
1479   pixbuf = gtk_style_render_icon (style, &tmp_source,
1480                                   direction, state, -1,
1481                                   widget, detail);
1482
1483   if (!pixbuf)
1484     g_warning ("Failed to render icon");
1485
1486   g_object_unref (tmp_pixbuf);
1487
1488   return pixbuf;
1489 }
1490
1491 static GdkPixbuf *
1492 find_and_render_icon_source (GtkIconSet       *icon_set,
1493                              GtkStyle         *style,
1494                              GtkTextDirection  direction,
1495                              GtkStateType      state,
1496                              GtkIconSize       size,
1497                              GtkWidget         *widget,
1498                              const char        *detail)
1499 {
1500   GSList *failed = NULL;
1501   GdkPixbuf *pixbuf = NULL;
1502
1503   /* We treat failure in two different ways:
1504    *
1505    *  A) If loading a source that specifies a filename fails,
1506    *     we treat that as permanent, and remove the source
1507    *     from the GtkIconSet. (in ensure_filename_pixbuf ()
1508    *  B) If loading a themed icon fails, or scaling an icon
1509    *     fails, we treat that as transient and will try
1510    *     again next time the icon falls out of the cache
1511    *     and we need to recreate it.
1512    */
1513   while (pixbuf == NULL)
1514     {
1515       GtkIconSource *source = find_best_matching_source (icon_set, direction, state, size, failed);
1516       
1517       if (source == NULL)
1518         break;
1519
1520       switch (source->type)
1521         {
1522         case GTK_ICON_SOURCE_FILENAME:
1523           if (!ensure_filename_pixbuf (icon_set, source))
1524             break;
1525           /* Fall through */
1526         case GTK_ICON_SOURCE_PIXBUF:
1527           pixbuf = gtk_style_render_icon (style, source,
1528                                           direction, state, size,
1529                                           widget, detail);
1530           if (!pixbuf)
1531             {
1532               g_warning ("Failed to render icon");
1533               failed = g_slist_prepend (failed, source);
1534             }
1535           break;
1536         case GTK_ICON_SOURCE_ICON_NAME:
1537         case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1538           pixbuf = render_icon_name_pixbuf (source, style,
1539                                             direction, state, size,
1540                                             widget, detail);
1541           if (!pixbuf)
1542             failed = g_slist_prepend (failed, source);
1543           break;
1544         case GTK_ICON_SOURCE_EMPTY:
1545           g_assert_not_reached ();
1546         }
1547     }
1548
1549   g_slist_free (failed);
1550
1551   return pixbuf;
1552 }
1553
1554 extern GtkIconCache *_builtin_cache;
1555
1556 static GdkPixbuf*
1557 render_fallback_image (GtkStyle          *style,
1558                        GtkTextDirection   direction,
1559                        GtkStateType       state,
1560                        GtkIconSize        size,
1561                        GtkWidget         *widget,
1562                        const char        *detail)
1563 {
1564   /* This icon can be used for any direction/state/size */
1565   static GtkIconSource fallback_source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1566
1567   if (fallback_source.type == GTK_ICON_SOURCE_EMPTY)
1568     {
1569       GdkPixbuf *pixbuf;
1570
1571       _gtk_icon_theme_ensure_builtin_cache ();
1572
1573       pixbuf = _gtk_icon_cache_get_icon (_builtin_cache,
1574                                          GTK_STOCK_MISSING_IMAGE,
1575                                          "24");
1576       gtk_icon_source_set_pixbuf (&fallback_source, pixbuf);
1577       g_object_unref (pixbuf);
1578     }
1579   
1580   return gtk_style_render_icon (style,
1581                                 &fallback_source,
1582                                 direction,
1583                                 state,
1584                                 size,
1585                                 widget,
1586                                 detail);
1587 }
1588
1589 /**
1590  * gtk_icon_set_render_icon:
1591  * @icon_set: a #GtkIconSet
1592  * @style: a #GtkStyle associated with @widget, or %NULL
1593  * @direction: text direction
1594  * @state: widget state
1595  * @size: icon size. A size of (GtkIconSize)-1
1596  *        means render at the size of the source and don't scale.
1597  * @widget: widget that will display the icon, or %NULL.
1598  *          The only use that is typically made of this
1599  *          is to determine the appropriate #GdkScreen.
1600  * @detail: detail to pass to the theme engine, or %NULL.
1601  *          Note that passing a detail of anything but %NULL
1602  *          will disable caching.
1603  * 
1604  * Renders an icon using gtk_style_render_icon(). In most cases,
1605  * gtk_widget_render_icon() is better, since it automatically provides
1606  * most of the arguments from the current widget settings.  This
1607  * function never returns %NULL; if the icon can't be rendered
1608  * (perhaps because an image file fails to load), a default "missing
1609  * image" icon will be returned instead.
1610  * 
1611  * Return value: a #GdkPixbuf to be displayed
1612  **/
1613 GdkPixbuf*
1614 gtk_icon_set_render_icon (GtkIconSet        *icon_set,
1615                           GtkStyle          *style,
1616                           GtkTextDirection   direction,
1617                           GtkStateType       state,
1618                           GtkIconSize        size,
1619                           GtkWidget         *widget,
1620                           const char        *detail)
1621 {
1622   GdkPixbuf *icon;
1623   
1624   g_return_val_if_fail (icon_set != NULL, NULL);
1625   g_return_val_if_fail (style == NULL || GTK_IS_STYLE (style), NULL);
1626
1627   if (icon_set->sources == NULL)
1628     return render_fallback_image (style, direction, state, size, widget, detail);
1629
1630   if (detail == NULL)
1631     {
1632       icon = find_in_cache (icon_set, style, direction,
1633                         state, size);
1634       
1635       if (icon)
1636         {
1637           g_object_ref (icon);
1638           return icon;
1639         }
1640     }
1641
1642
1643   icon = find_and_render_icon_source (icon_set, style, direction, state, size,
1644                                       widget, detail);
1645
1646   if (icon == NULL)
1647     icon = render_fallback_image (style, direction, state, size, widget, detail);
1648
1649   if (detail == NULL)
1650     add_to_cache (icon_set, style, direction, state, size, icon);
1651   
1652   return icon;
1653 }
1654
1655 /* Order sources by their "wildness", so that "wilder" sources are
1656  * greater than "specific" sources; for determining ordering,
1657  * direction beats state beats size.
1658  */
1659
1660 static int
1661 icon_source_compare (gconstpointer ap, gconstpointer bp)
1662 {
1663   const GtkIconSource *a = ap;
1664   const GtkIconSource *b = bp;
1665
1666   if (!a->any_direction && b->any_direction)
1667     return -1;
1668   else if (a->any_direction && !b->any_direction)
1669     return 1;
1670   else if (!a->any_state && b->any_state)
1671     return -1;
1672   else if (a->any_state && !b->any_state)
1673     return 1;
1674   else if (!a->any_size && b->any_size)
1675     return -1;
1676   else if (a->any_size && !b->any_size)
1677     return 1;
1678   else
1679     return 0;
1680 }
1681
1682 /**
1683  * gtk_icon_set_add_source:
1684  * @icon_set: a #GtkIconSet
1685  * @source: a #GtkIconSource
1686  *
1687  * Icon sets have a list of #GtkIconSource, which they use as base
1688  * icons for rendering icons in different states and sizes. Icons are
1689  * scaled, made to look insensitive, etc. in
1690  * gtk_icon_set_render_icon(), but #GtkIconSet needs base images to
1691  * work with. The base images and when to use them are described by
1692  * a #GtkIconSource.
1693  * 
1694  * This function copies @source, so you can reuse the same source immediately
1695  * without affecting the icon set.
1696  *
1697  * An example of when you'd use this function: a web browser's "Back
1698  * to Previous Page" icon might point in a different direction in
1699  * Hebrew and in English; it might look different when insensitive;
1700  * and it might change size depending on toolbar mode (small/large
1701  * icons). So a single icon set would contain all those variants of
1702  * the icon, and you might add a separate source for each one.
1703  *
1704  * You should nearly always add a "default" icon source with all
1705  * fields wildcarded, which will be used as a fallback if no more
1706  * specific source matches. #GtkIconSet always prefers more specific
1707  * icon sources to more generic icon sources. The order in which you
1708  * add the sources to the icon set does not matter.
1709  *
1710  * gtk_icon_set_new_from_pixbuf() creates a new icon set with a
1711  * default icon source based on the given pixbuf.
1712  * 
1713  **/
1714 void
1715 gtk_icon_set_add_source (GtkIconSet *icon_set,
1716                          const GtkIconSource *source)
1717 {
1718   g_return_if_fail (icon_set != NULL);
1719   g_return_if_fail (source != NULL);
1720
1721   if (source->type == GTK_ICON_SOURCE_EMPTY)
1722     {
1723       g_warning ("Useless empty GtkIconSource");
1724       return;
1725     }
1726   
1727   icon_set->sources = g_slist_insert_sorted (icon_set->sources,
1728                                              gtk_icon_source_copy (source),
1729                                              icon_source_compare);
1730 }
1731
1732 /**
1733  * gtk_icon_set_get_sizes:
1734  * @icon_set: a #GtkIconSet
1735  * @sizes: return location for array of sizes
1736  * @n_sizes: location to store number of elements in returned array
1737  *
1738  * Obtains a list of icon sizes this icon set can render. The returned
1739  * array must be freed with g_free().
1740  * 
1741  **/
1742 void
1743 gtk_icon_set_get_sizes (GtkIconSet   *icon_set,
1744                         GtkIconSize **sizes,
1745                         gint         *n_sizes)
1746 {
1747   GSList *tmp_list;
1748   gboolean all_sizes = FALSE;
1749   GSList *specifics = NULL;
1750   
1751   g_return_if_fail (icon_set != NULL);
1752   g_return_if_fail (sizes != NULL);
1753   g_return_if_fail (n_sizes != NULL);
1754   
1755   tmp_list = icon_set->sources;
1756   while (tmp_list != NULL)
1757     {
1758       GtkIconSource *source;
1759
1760       source = tmp_list->data;
1761
1762       if (source->any_size)
1763         {
1764           all_sizes = TRUE;
1765           break;
1766         }
1767       else
1768         specifics = g_slist_prepend (specifics, GINT_TO_POINTER (source->size));
1769       
1770       tmp_list = g_slist_next (tmp_list);
1771     }
1772
1773   if (all_sizes)
1774     {
1775       /* Need to find out what sizes exist */
1776       gint i;
1777
1778       init_icon_sizes ();
1779       
1780       *sizes = g_new (GtkIconSize, icon_sizes_used);
1781       *n_sizes = icon_sizes_used - 1;
1782       
1783       i = 1;      
1784       while (i < icon_sizes_used)
1785         {
1786           (*sizes)[i - 1] = icon_sizes[i].size;
1787           ++i;
1788         }
1789     }
1790   else
1791     {
1792       gint i;
1793       
1794       *n_sizes = g_slist_length (specifics);
1795       *sizes = g_new (GtkIconSize, *n_sizes);
1796
1797       i = 0;
1798       tmp_list = specifics;
1799       while (tmp_list != NULL)
1800         {
1801           (*sizes)[i] = GPOINTER_TO_INT (tmp_list->data);
1802
1803           ++i;
1804           tmp_list = g_slist_next (tmp_list);
1805         }
1806     }
1807
1808   g_slist_free (specifics);
1809 }
1810
1811
1812 /**
1813  * gtk_icon_source_new:
1814  * 
1815  * Creates a new #GtkIconSource. A #GtkIconSource contains a #GdkPixbuf (or
1816  * image filename) that serves as the base image for one or more of the
1817  * icons in a #GtkIconSet, along with a specification for which icons in the
1818  * icon set will be based on that pixbuf or image file. An icon set contains
1819  * a set of icons that represent "the same" logical concept in different states,
1820  * different global text directions, and different sizes.
1821  * 
1822  * So for example a web browser's "Back to Previous Page" icon might
1823  * point in a different direction in Hebrew and in English; it might
1824  * look different when insensitive; and it might change size depending
1825  * on toolbar mode (small/large icons). So a single icon set would
1826  * contain all those variants of the icon. #GtkIconSet contains a list
1827  * of #GtkIconSource from which it can derive specific icon variants in
1828  * the set. 
1829  *
1830  * In the simplest case, #GtkIconSet contains one source pixbuf from
1831  * which it derives all variants. The convenience function
1832  * gtk_icon_set_new_from_pixbuf() handles this case; if you only have
1833  * one source pixbuf, just use that function.
1834  *
1835  * If you want to use a different base pixbuf for different icon
1836  * variants, you create multiple icon sources, mark which variants
1837  * they'll be used to create, and add them to the icon set with
1838  * gtk_icon_set_add_source().
1839  *
1840  * By default, the icon source has all parameters wildcarded. That is,
1841  * the icon source will be used as the base icon for any desired text
1842  * direction, widget state, or icon size.
1843  * 
1844  * Return value: a new #GtkIconSource
1845  **/
1846 GtkIconSource*
1847 gtk_icon_source_new (void)
1848 {
1849   GtkIconSource *src;
1850   
1851   src = g_new0 (GtkIconSource, 1);
1852
1853   src->direction = GTK_TEXT_DIR_NONE;
1854   src->size = GTK_ICON_SIZE_INVALID;
1855   src->state = GTK_STATE_NORMAL;
1856   
1857   src->any_direction = TRUE;
1858   src->any_state = TRUE;
1859   src->any_size = TRUE;
1860   
1861   return src;
1862 }
1863
1864 /**
1865  * gtk_icon_source_copy:
1866  * @source: a #GtkIconSource
1867  * 
1868  * Creates a copy of @source; mostly useful for language bindings.
1869  * 
1870  * Return value: a new #GtkIconSource
1871  **/
1872 GtkIconSource*
1873 gtk_icon_source_copy (const GtkIconSource *source)
1874 {
1875   GtkIconSource *copy;
1876   
1877   g_return_val_if_fail (source != NULL, NULL);
1878
1879   copy = g_new (GtkIconSource, 1);
1880
1881   *copy = *source;
1882   
1883   switch (copy->type)
1884     {
1885     case GTK_ICON_SOURCE_EMPTY:
1886     case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1887       break;
1888     case GTK_ICON_SOURCE_ICON_NAME:
1889       copy->source.icon_name = g_strdup (copy->source.icon_name);
1890       break;
1891     case GTK_ICON_SOURCE_FILENAME:
1892       copy->source.filename = g_strdup (copy->source.filename);
1893 #ifdef G_OS_WIN32
1894       copy->cp_filename = g_strdup (copy->cp_filename);
1895 #endif
1896       if (copy->filename_pixbuf)
1897         g_object_ref (copy->filename_pixbuf);
1898       break;
1899     case GTK_ICON_SOURCE_PIXBUF:
1900       g_object_ref (copy->source.pixbuf);
1901       break;
1902     default:
1903       g_assert_not_reached();
1904     }
1905
1906   return copy;
1907 }
1908
1909 /**
1910  * gtk_icon_source_free:
1911  * @source: a #GtkIconSource
1912  * 
1913  * Frees a dynamically-allocated icon source, along with its
1914  * filename, size, and pixbuf fields if those are not %NULL.
1915  **/
1916 void
1917 gtk_icon_source_free (GtkIconSource *source)
1918 {
1919   g_return_if_fail (source != NULL);
1920
1921   icon_source_clear (source);
1922   g_free (source);
1923 }
1924
1925 GType
1926 gtk_icon_source_get_type (void)
1927 {
1928   static GType our_type = 0;
1929   
1930   if (our_type == 0)
1931     our_type = g_boxed_type_register_static ("GtkIconSource",
1932                                              (GBoxedCopyFunc) gtk_icon_source_copy,
1933                                              (GBoxedFreeFunc) gtk_icon_source_free);
1934
1935   return our_type;
1936 }
1937
1938 static void
1939 icon_source_clear (GtkIconSource *source)
1940 {
1941   switch (source->type)
1942     {
1943     case GTK_ICON_SOURCE_EMPTY:
1944       break;
1945     case GTK_ICON_SOURCE_ICON_NAME:
1946       g_free (source->source.icon_name);
1947       /* fall thru */
1948     case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1949       source->source.icon_name = NULL;
1950       break;
1951     case GTK_ICON_SOURCE_FILENAME:
1952       g_free (source->source.filename);
1953       source->source.filename = NULL;
1954 #ifdef G_OS_WIN32
1955       g_free (source->cp_filename);
1956       source->cp_filename = NULL;
1957 #endif
1958       if (source->filename_pixbuf) 
1959         g_object_unref (source->filename_pixbuf);
1960       source->filename_pixbuf = NULL;
1961       break;
1962     case GTK_ICON_SOURCE_PIXBUF:
1963       g_object_unref (source->source.pixbuf);
1964       source->source.pixbuf = NULL;
1965       break;
1966     default:
1967       g_assert_not_reached();
1968     }
1969
1970   source->type = GTK_ICON_SOURCE_EMPTY;
1971 }
1972
1973 /**
1974  * gtk_icon_source_set_filename:
1975  * @source: a #GtkIconSource
1976  * @filename: image file to use
1977  *
1978  * Sets the name of an image file to use as a base image when creating
1979  * icon variants for #GtkIconSet. The filename must be absolute. 
1980  **/
1981 void
1982 gtk_icon_source_set_filename (GtkIconSource *source,
1983                               const gchar   *filename)
1984 {
1985   g_return_if_fail (source != NULL);
1986   g_return_if_fail (filename == NULL || g_path_is_absolute (filename));
1987
1988   if (source->type == GTK_ICON_SOURCE_FILENAME &&
1989       source->source.filename == filename)
1990     return;
1991   
1992   icon_source_clear (source);
1993   
1994   if (filename != NULL)
1995     {
1996       source->type = GTK_ICON_SOURCE_FILENAME;
1997       source->source.filename = g_strdup (filename);
1998 #ifdef G_OS_WIN32
1999       source->cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
2000 #endif
2001     }
2002 }
2003
2004 /**
2005  * gtk_icon_source_set_icon_name
2006  * @source: a #GtkIconSource
2007  * @icon_name: name of icon to use
2008  *
2009  * Sets the name of an icon to look up in the current icon theme
2010  * to use as a base image when creating icon variants for #GtkIconSet.
2011  **/
2012 void
2013 gtk_icon_source_set_icon_name (GtkIconSource *source,
2014                                const gchar   *icon_name)
2015 {
2016   g_return_if_fail (source != NULL);
2017
2018   if (source->type == GTK_ICON_SOURCE_ICON_NAME &&
2019       source->source.icon_name == icon_name)
2020     return;
2021
2022   icon_source_clear (source);
2023   
2024   if (icon_name != NULL)
2025     {
2026       source->type = GTK_ICON_SOURCE_ICON_NAME;
2027       source->source.icon_name = g_strdup (icon_name);
2028     }
2029 }
2030
2031 /**
2032  * gtk_icon_source_set_pixbuf:
2033  * @source: a #GtkIconSource
2034  * @pixbuf: pixbuf to use as a source
2035  *
2036  * Sets a pixbuf to use as a base image when creating icon variants
2037  * for #GtkIconSet.
2038  **/
2039 void
2040 gtk_icon_source_set_pixbuf (GtkIconSource *source,
2041                             GdkPixbuf     *pixbuf)
2042 {
2043   g_return_if_fail (source != NULL);
2044   g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
2045   
2046   if (source->type == GTK_ICON_SOURCE_PIXBUF &&
2047       source->source.pixbuf == pixbuf)
2048     return;
2049
2050   icon_source_clear (source);
2051   
2052   if (pixbuf != NULL)
2053     {
2054       source->type = GTK_ICON_SOURCE_PIXBUF;
2055       source->source.pixbuf = g_object_ref (pixbuf);
2056     }
2057 }
2058
2059 /**
2060  * gtk_icon_source_get_filename:
2061  * @source: a #GtkIconSource
2062  * 
2063  * Retrieves the source filename, or %NULL if none is set. The
2064  * filename is not a copy, and should not be modified or expected to
2065  * persist beyond the lifetime of the icon source.
2066  * 
2067  * Return value: image filename. This string must not be modified
2068  * or freed.
2069  **/
2070 G_CONST_RETURN gchar*
2071 gtk_icon_source_get_filename (const GtkIconSource *source)
2072 {
2073   g_return_val_if_fail (source != NULL, NULL);
2074
2075   if (source->type == GTK_ICON_SOURCE_FILENAME)
2076     return source->source.filename;
2077   else
2078     return NULL;
2079 }
2080
2081 /**
2082  * gtk_icon_source_get_icon_name:
2083  * @source: a #GtkIconSource
2084  * 
2085  * Retrieves the source icon name, or %NULL if none is set. The
2086  * icon_name is not a copy, and should not be modified or expected to
2087  * persist beyond the lifetime of the icon source.
2088  * 
2089  * Return value: icon name. This string must not be modified or freed.
2090  **/
2091 G_CONST_RETURN gchar*
2092 gtk_icon_source_get_icon_name (const GtkIconSource *source)
2093 {
2094   g_return_val_if_fail (source != NULL, NULL);
2095
2096   if (source->type == GTK_ICON_SOURCE_ICON_NAME ||
2097      source->type == GTK_ICON_SOURCE_STATIC_ICON_NAME)
2098     return source->source.icon_name;
2099   else
2100     return NULL;
2101 }
2102
2103 /**
2104  * gtk_icon_source_get_pixbuf:
2105  * @source: a #GtkIconSource
2106  * 
2107  * Retrieves the source pixbuf, or %NULL if none is set.
2108  * In addition, if a filename source is in use, this
2109  * function in some cases will return the pixbuf from
2110  * loaded from the filename. This is, for example, true
2111  * for the GtkIconSource passed to the GtkStyle::render_icon()
2112  * virtual function. The reference count on the pixbuf is
2113  * not incremented.
2114  * 
2115  * Return value: source pixbuf
2116  **/
2117 GdkPixbuf*
2118 gtk_icon_source_get_pixbuf (const GtkIconSource *source)
2119 {
2120   g_return_val_if_fail (source != NULL, NULL);
2121   
2122   if (source->type == GTK_ICON_SOURCE_PIXBUF)
2123     return source->source.pixbuf;
2124   else if (source->type == GTK_ICON_SOURCE_FILENAME)
2125     return source->filename_pixbuf;
2126   else
2127     return NULL;
2128 }
2129
2130 /**
2131  * gtk_icon_source_set_direction_wildcarded:
2132  * @source: a #GtkIconSource
2133  * @setting: %TRUE to wildcard the text direction
2134  *
2135  * If the text direction is wildcarded, this source can be used
2136  * as the base image for an icon in any #GtkTextDirection.
2137  * If the text direction is not wildcarded, then the
2138  * text direction the icon source applies to should be set
2139  * with gtk_icon_source_set_direction(), and the icon source
2140  * will only be used with that text direction.
2141  *
2142  * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2143  * wildcarded sources, and will use an exact match when possible.
2144  * 
2145  **/
2146 void
2147 gtk_icon_source_set_direction_wildcarded (GtkIconSource *source,
2148                                           gboolean       setting)
2149 {
2150   g_return_if_fail (source != NULL);
2151
2152   source->any_direction = setting != FALSE;
2153 }
2154
2155 /**
2156  * gtk_icon_source_set_state_wildcarded:
2157  * @source: a #GtkIconSource
2158  * @setting: %TRUE to wildcard the widget state
2159  *
2160  * If the widget state is wildcarded, this source can be used as the
2161  * base image for an icon in any #GtkStateType.  If the widget state
2162  * is not wildcarded, then the state the source applies to should be
2163  * set with gtk_icon_source_set_state() and the icon source will
2164  * only be used with that specific state.
2165  *
2166  * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2167  * wildcarded sources, and will use an exact match when possible.
2168  *
2169  * #GtkIconSet will normally transform wildcarded source images to
2170  * produce an appropriate icon for a given state, for example
2171  * lightening an image on prelight, but will not modify source images
2172  * that match exactly.
2173  **/
2174 void
2175 gtk_icon_source_set_state_wildcarded (GtkIconSource *source,
2176                                       gboolean       setting)
2177 {
2178   g_return_if_fail (source != NULL);
2179
2180   source->any_state = setting != FALSE;
2181 }
2182
2183
2184 /**
2185  * gtk_icon_source_set_size_wildcarded:
2186  * @source: a #GtkIconSource
2187  * @setting: %TRUE to wildcard the widget state
2188  *
2189  * If the icon size is wildcarded, this source can be used as the base
2190  * image for an icon of any size.  If the size is not wildcarded, then
2191  * the size the source applies to should be set with
2192  * gtk_icon_source_set_size() and the icon source will only be used
2193  * with that specific size.
2194  *
2195  * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2196  * wildcarded sources, and will use an exact match when possible.
2197  *
2198  * #GtkIconSet will normally scale wildcarded source images to produce
2199  * an appropriate icon at a given size, but will not change the size
2200  * of source images that match exactly.
2201  **/
2202 void
2203 gtk_icon_source_set_size_wildcarded (GtkIconSource *source,
2204                                      gboolean       setting)
2205 {
2206   g_return_if_fail (source != NULL);
2207
2208   source->any_size = setting != FALSE;  
2209 }
2210
2211 /**
2212  * gtk_icon_source_get_size_wildcarded:
2213  * @source: a #GtkIconSource
2214  * 
2215  * Gets the value set by gtk_icon_source_set_size_wildcarded().
2216  * 
2217  * Return value: %TRUE if this icon source is a base for any icon size variant
2218  **/
2219 gboolean
2220 gtk_icon_source_get_size_wildcarded (const GtkIconSource *source)
2221 {
2222   g_return_val_if_fail (source != NULL, TRUE);
2223   
2224   return source->any_size;
2225 }
2226
2227 /**
2228  * gtk_icon_source_get_state_wildcarded:
2229  * @source: a #GtkIconSource
2230  * 
2231  * Gets the value set by gtk_icon_source_set_state_wildcarded().
2232  * 
2233  * Return value: %TRUE if this icon source is a base for any widget state variant
2234  **/
2235 gboolean
2236 gtk_icon_source_get_state_wildcarded (const GtkIconSource *source)
2237 {
2238   g_return_val_if_fail (source != NULL, TRUE);
2239
2240   return source->any_state;
2241 }
2242
2243 /**
2244  * gtk_icon_source_get_direction_wildcarded:
2245  * @source: a #GtkIconSource
2246  * 
2247  * Gets the value set by gtk_icon_source_set_direction_wildcarded().
2248  * 
2249  * Return value: %TRUE if this icon source is a base for any text direction variant
2250  **/
2251 gboolean
2252 gtk_icon_source_get_direction_wildcarded (const GtkIconSource *source)
2253 {
2254   g_return_val_if_fail (source != NULL, TRUE);
2255
2256   return source->any_direction;
2257 }
2258
2259 /**
2260  * gtk_icon_source_set_direction:
2261  * @source: a #GtkIconSource
2262  * @direction: text direction this source applies to
2263  *
2264  * Sets the text direction this icon source is intended to be used
2265  * with.
2266  * 
2267  * Setting the text direction on an icon source makes no difference
2268  * if the text direction is wildcarded. Therefore, you should usually
2269  * call gtk_icon_source_set_direction_wildcarded() to un-wildcard it
2270  * in addition to calling this function.
2271  * 
2272  **/
2273 void
2274 gtk_icon_source_set_direction (GtkIconSource   *source,
2275                                GtkTextDirection direction)
2276 {
2277   g_return_if_fail (source != NULL);
2278
2279   source->direction = direction;
2280 }
2281
2282 /**
2283  * gtk_icon_source_set_state:
2284  * @source: a #GtkIconSource
2285  * @state: widget state this source applies to
2286  *
2287  * Sets the widget state this icon source is intended to be used
2288  * with.
2289  * 
2290  * Setting the widget state on an icon source makes no difference
2291  * if the state is wildcarded. Therefore, you should usually
2292  * call gtk_icon_source_set_state_wildcarded() to un-wildcard it
2293  * in addition to calling this function.
2294  * 
2295  **/
2296 void
2297 gtk_icon_source_set_state (GtkIconSource *source,
2298                            GtkStateType   state)
2299 {
2300   g_return_if_fail (source != NULL);
2301
2302   source->state = state;
2303 }
2304
2305 /**
2306  * gtk_icon_source_set_size:
2307  * @source: a #GtkIconSource
2308  * @size: icon size this source applies to
2309  *
2310  * Sets the icon size this icon source is intended to be used
2311  * with.
2312  * 
2313  * Setting the icon size on an icon source makes no difference
2314  * if the size is wildcarded. Therefore, you should usually
2315  * call gtk_icon_source_set_size_wildcarded() to un-wildcard it
2316  * in addition to calling this function.
2317  * 
2318  **/
2319 void
2320 gtk_icon_source_set_size (GtkIconSource *source,
2321                           GtkIconSize    size)
2322 {
2323   g_return_if_fail (source != NULL);
2324
2325   source->size = size;
2326 }
2327
2328 /**
2329  * gtk_icon_source_get_direction:
2330  * @source: a #GtkIconSource
2331  * 
2332  * Obtains the text direction this icon source applies to. The return
2333  * value is only useful/meaningful if the text direction is <emphasis>not</emphasis> 
2334  * wildcarded.
2335  * 
2336  * Return value: text direction this source matches
2337  **/
2338 GtkTextDirection
2339 gtk_icon_source_get_direction (const GtkIconSource *source)
2340 {
2341   g_return_val_if_fail (source != NULL, 0);
2342
2343   return source->direction;
2344 }
2345
2346 /**
2347  * gtk_icon_source_get_state:
2348  * @source: a #GtkIconSource
2349  * 
2350  * Obtains the widget state this icon source applies to. The return
2351  * value is only useful/meaningful if the widget state is <emphasis>not</emphasis>
2352  * wildcarded.
2353  * 
2354  * Return value: widget state this source matches
2355  **/
2356 GtkStateType
2357 gtk_icon_source_get_state (const GtkIconSource *source)
2358 {
2359   g_return_val_if_fail (source != NULL, 0);
2360
2361   return source->state;
2362 }
2363
2364 /**
2365  * gtk_icon_source_get_size:
2366  * @source: a #GtkIconSource
2367  * 
2368  * Obtains the icon size this source applies to. The return value
2369  * is only useful/meaningful if the icon size is <emphasis>not</emphasis> wildcarded.
2370  * 
2371  * Return value: icon size this source matches.
2372  **/
2373 GtkIconSize
2374 gtk_icon_source_get_size (const GtkIconSource *source)
2375 {
2376   g_return_val_if_fail (source != NULL, 0);
2377
2378   return source->size;
2379 }
2380
2381 #define NUM_CACHED_ICONS 8
2382
2383 typedef struct _CachedIcon CachedIcon;
2384
2385 struct _CachedIcon
2386 {
2387   /* These must all match to use the cached pixbuf.
2388    * If any don't match, we must re-render the pixbuf.
2389    */
2390   GtkStyle *style;
2391   GtkTextDirection direction;
2392   GtkStateType state;
2393   GtkIconSize size;
2394
2395   GdkPixbuf *pixbuf;
2396 };
2397
2398 static void
2399 ensure_cache_up_to_date (GtkIconSet *icon_set)
2400 {
2401   if (icon_set->cache_serial != cache_serial)
2402     {
2403       clear_cache (icon_set, TRUE);
2404       icon_set->cache_serial = cache_serial;
2405     }
2406 }
2407
2408 static void
2409 cached_icon_free (CachedIcon *icon)
2410 {
2411   g_object_unref (icon->pixbuf);
2412
2413   if (icon->style)
2414     g_object_unref (icon->style);
2415
2416   g_free (icon);
2417 }
2418
2419 static GdkPixbuf *
2420 find_in_cache (GtkIconSet      *icon_set,
2421                GtkStyle        *style,
2422                GtkTextDirection direction,
2423                GtkStateType     state,
2424                GtkIconSize      size)
2425 {
2426   GSList *tmp_list;
2427   GSList *prev;
2428
2429   ensure_cache_up_to_date (icon_set);
2430   
2431   prev = NULL;
2432   tmp_list = icon_set->cache;
2433   while (tmp_list != NULL)
2434     {
2435       CachedIcon *icon = tmp_list->data;
2436
2437       if (icon->style == style &&
2438           icon->direction == direction &&
2439           icon->state == state &&
2440           icon->size == size)
2441         {
2442           if (prev)
2443             {
2444               /* Move this icon to the front of the list. */
2445               prev->next = tmp_list->next;
2446               tmp_list->next = icon_set->cache;
2447               icon_set->cache = tmp_list;
2448             }
2449           
2450           return icon->pixbuf;
2451         }
2452           
2453       prev = tmp_list;
2454       tmp_list = g_slist_next (tmp_list);
2455     }
2456
2457   return NULL;
2458 }
2459
2460 static void
2461 add_to_cache (GtkIconSet      *icon_set,
2462               GtkStyle        *style,
2463               GtkTextDirection direction,
2464               GtkStateType     state,
2465               GtkIconSize      size,
2466               GdkPixbuf       *pixbuf)
2467 {
2468   CachedIcon *icon;
2469
2470   ensure_cache_up_to_date (icon_set);
2471
2472   g_object_ref (pixbuf);
2473
2474   /* We have to ref the style, since if the style was finalized
2475    * its address could be reused by another style, creating a
2476    * really weird bug
2477    */
2478   
2479   if (style)
2480     g_object_ref (style);
2481
2482   icon = g_new (CachedIcon, 1);
2483   icon_set->cache = g_slist_prepend (icon_set->cache, icon);
2484   icon_set->cache_size++;
2485
2486   icon->style = style;
2487   icon->direction = direction;
2488   icon->state = state;
2489   icon->size = size;
2490   icon->pixbuf = pixbuf;
2491
2492   if (icon->style)
2493     attach_to_style (icon_set, icon->style);
2494   
2495   if (icon_set->cache_size >= NUM_CACHED_ICONS)
2496     {
2497       /* Remove oldest item in the cache */
2498       GSList *tmp_list;
2499       
2500       tmp_list = icon_set->cache;
2501
2502       /* Find next-to-last link */
2503       g_assert (NUM_CACHED_ICONS > 2);
2504       while (tmp_list->next->next)
2505         tmp_list = tmp_list->next;
2506
2507       g_assert (tmp_list != NULL);
2508       g_assert (tmp_list->next != NULL);
2509       g_assert (tmp_list->next->next == NULL);
2510
2511       /* Free the last icon */
2512       icon = tmp_list->next->data;
2513
2514       g_slist_free (tmp_list->next);
2515       tmp_list->next = NULL;
2516
2517       cached_icon_free (icon);
2518     }
2519 }
2520
2521 static void
2522 clear_cache (GtkIconSet *icon_set,
2523              gboolean    style_detach)
2524 {
2525   GSList *cache, *tmp_list;
2526   GtkStyle *last_style = NULL;
2527
2528   cache = icon_set->cache;
2529   icon_set->cache = NULL;
2530   icon_set->cache_size = 0;
2531   tmp_list = cache;
2532   while (tmp_list != NULL)
2533     {
2534       CachedIcon *icon = tmp_list->data;
2535
2536       if (style_detach)
2537         {
2538           /* simple optimization for the case where the cache
2539            * contains contiguous icons from the same style.
2540            * it's safe to call detach_from_style more than
2541            * once on the same style though.
2542            */
2543           if (last_style != icon->style)
2544             {
2545               detach_from_style (icon_set, icon->style);
2546               last_style = icon->style;
2547             }
2548         }
2549       
2550       cached_icon_free (icon);      
2551       
2552       tmp_list = g_slist_next (tmp_list);
2553     }
2554
2555   g_slist_free (cache);
2556 }
2557
2558 static GSList*
2559 copy_cache (GtkIconSet *icon_set,
2560             GtkIconSet *copy_recipient)
2561 {
2562   GSList *tmp_list;
2563   GSList *copy = NULL;
2564
2565   ensure_cache_up_to_date (icon_set);
2566   
2567   tmp_list = icon_set->cache;
2568   while (tmp_list != NULL)
2569     {
2570       CachedIcon *icon = tmp_list->data;
2571       CachedIcon *icon_copy = g_new (CachedIcon, 1);
2572
2573       *icon_copy = *icon;
2574
2575       if (icon_copy->style)
2576         {
2577           attach_to_style (copy_recipient, icon_copy->style);
2578           g_object_ref (icon_copy->style);
2579         }
2580         
2581       g_object_ref (icon_copy->pixbuf);
2582
2583       icon_copy->size = icon->size;
2584       
2585       copy = g_slist_prepend (copy, icon_copy);      
2586       
2587       tmp_list = g_slist_next (tmp_list);
2588     }
2589
2590   return g_slist_reverse (copy);
2591 }
2592
2593 static void
2594 attach_to_style (GtkIconSet *icon_set,
2595                  GtkStyle   *style)
2596 {
2597   GHashTable *table;
2598
2599   table = g_object_get_qdata (G_OBJECT (style),
2600                               g_quark_try_string ("gtk-style-icon-sets"));
2601
2602   if (table == NULL)
2603     {
2604       table = g_hash_table_new (NULL, NULL);
2605       g_object_set_qdata_full (G_OBJECT (style),
2606                                g_quark_from_static_string ("gtk-style-icon-sets"),
2607                                table,
2608                                style_dnotify);
2609     }
2610
2611   g_hash_table_insert (table, icon_set, icon_set);
2612 }
2613
2614 static void
2615 detach_from_style (GtkIconSet *icon_set,
2616                    GtkStyle   *style)
2617 {
2618   GHashTable *table;
2619
2620   table = g_object_get_qdata (G_OBJECT (style),
2621                               g_quark_try_string ("gtk-style-icon-sets"));
2622
2623   if (table != NULL)
2624     g_hash_table_remove (table, icon_set);
2625 }
2626
2627 static void
2628 iconsets_foreach (gpointer key,
2629                   gpointer value,
2630                   gpointer user_data)
2631 {
2632   GtkIconSet *icon_set = key;
2633
2634   /* We only need to remove cache entries for the given style;
2635    * but that complicates things because in destroy notify
2636    * we don't know which style got destroyed, and 95% of the
2637    * time all cache entries will have the same style,
2638    * so this is faster anyway.
2639    */
2640   
2641   clear_cache (icon_set, FALSE);
2642 }
2643
2644 static void
2645 style_dnotify (gpointer data)
2646 {
2647   GHashTable *table = data;
2648   
2649   g_hash_table_foreach (table, iconsets_foreach, NULL);
2650
2651   g_hash_table_destroy (table);
2652 }
2653
2654 /* This allows the icon set to detect that its cache is out of date. */
2655 void
2656 _gtk_icon_set_invalidate_caches (void)
2657 {
2658   ++cache_serial;
2659 }
2660
2661 static void
2662 listify_foreach (gpointer key, gpointer value, gpointer data)
2663 {
2664   GSList **list = data;
2665
2666   *list = g_slist_prepend (*list, key);
2667 }
2668
2669 static GSList *
2670 g_hash_table_get_keys (GHashTable *table)
2671 {
2672   GSList *list = NULL;
2673
2674   g_hash_table_foreach (table, listify_foreach, &list);
2675
2676   return list;
2677 }
2678
2679 /**
2680  * _gtk_icon_factory_list_ids:
2681  * 
2682  * Gets all known IDs stored in an existing icon factory.
2683  * The strings in the returned list aren't copied.
2684  * The list itself should be freed.
2685  * 
2686  * Return value: List of ids in icon factories
2687  **/
2688 GSList*
2689 _gtk_icon_factory_list_ids (void)
2690 {
2691   GSList *tmp_list;
2692   GSList *ids;
2693
2694   ids = NULL;
2695
2696   _gtk_icon_factory_ensure_default_icons ();
2697   
2698   tmp_list = all_icon_factories;
2699   while (tmp_list != NULL)
2700     {
2701       GSList *these_ids;
2702       
2703       GtkIconFactory *factory = GTK_ICON_FACTORY (tmp_list->data);
2704
2705       these_ids = g_hash_table_get_keys (factory->icons);
2706       
2707       ids = g_slist_concat (ids, these_ids);
2708       
2709       tmp_list = g_slist_next (tmp_list);
2710     }
2711
2712   return ids;
2713 }
2714
2715 #ifdef G_OS_WIN32
2716
2717 /* DLL ABI stability backward compatibility versions */
2718
2719 #undef gtk_icon_source_set_filename
2720
2721 void
2722 gtk_icon_source_set_filename (GtkIconSource *source,
2723                               const gchar   *filename)
2724 {
2725   gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2726
2727   gtk_icon_source_set_filename_utf8 (source, utf8_filename);
2728
2729   g_free (utf8_filename);
2730 }
2731
2732 #undef gtk_icon_source_get_filename
2733
2734 G_CONST_RETURN gchar*
2735 gtk_icon_source_get_filename (const GtkIconSource *source)
2736 {
2737   g_return_val_if_fail (source != NULL, NULL);
2738
2739   if (source->type == GTK_ICON_SOURCE_FILENAME)
2740     return source->cp_filename;
2741   else
2742     return NULL;
2743 }
2744
2745 #endif
2746
2747 #define __GTK_ICON_FACTORY_C__
2748 #include "gtkaliasdef.c"