]> Pileus Git - ~andy/gtk/blob - gtk/gtkiconfactory.c
d581d605b2e8144d26afa885d63830172e7e3bb2
[~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 %u 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 %u\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 '%s' for stock: %s", 
1471                  icon_source->source.icon_name, error->message);
1472       g_error_free (error);
1473       return NULL;
1474     }
1475   
1476   tmp_source = *icon_source;
1477   tmp_source.type = GTK_ICON_SOURCE_PIXBUF;
1478   tmp_source.source.pixbuf = tmp_pixbuf;
1479
1480   pixbuf = gtk_style_render_icon (style, &tmp_source,
1481                                   direction, state, -1,
1482                                   widget, detail);
1483
1484   if (!pixbuf)
1485     g_warning ("Failed to render icon");
1486
1487   g_object_unref (tmp_pixbuf);
1488
1489   return pixbuf;
1490 }
1491
1492 static GdkPixbuf *
1493 find_and_render_icon_source (GtkIconSet       *icon_set,
1494                              GtkStyle         *style,
1495                              GtkTextDirection  direction,
1496                              GtkStateType      state,
1497                              GtkIconSize       size,
1498                              GtkWidget         *widget,
1499                              const char        *detail)
1500 {
1501   GSList *failed = NULL;
1502   GdkPixbuf *pixbuf = NULL;
1503
1504   /* We treat failure in two different ways:
1505    *
1506    *  A) If loading a source that specifies a filename fails,
1507    *     we treat that as permanent, and remove the source
1508    *     from the GtkIconSet. (in ensure_filename_pixbuf ()
1509    *  B) If loading a themed icon fails, or scaling an icon
1510    *     fails, we treat that as transient and will try
1511    *     again next time the icon falls out of the cache
1512    *     and we need to recreate it.
1513    */
1514   while (pixbuf == NULL)
1515     {
1516       GtkIconSource *source = find_best_matching_source (icon_set, direction, state, size, failed);
1517       
1518       if (source == NULL)
1519         break;
1520
1521       switch (source->type)
1522         {
1523         case GTK_ICON_SOURCE_FILENAME:
1524           if (!ensure_filename_pixbuf (icon_set, source))
1525             break;
1526           /* Fall through */
1527         case GTK_ICON_SOURCE_PIXBUF:
1528           pixbuf = gtk_style_render_icon (style, source,
1529                                           direction, state, size,
1530                                           widget, detail);
1531           if (!pixbuf)
1532             {
1533               g_warning ("Failed to render icon");
1534               failed = g_slist_prepend (failed, source);
1535             }
1536           break;
1537         case GTK_ICON_SOURCE_ICON_NAME:
1538         case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1539           pixbuf = render_icon_name_pixbuf (source, style,
1540                                             direction, state, size,
1541                                             widget, detail);
1542           if (!pixbuf)
1543             failed = g_slist_prepend (failed, source);
1544           break;
1545         case GTK_ICON_SOURCE_EMPTY:
1546           g_assert_not_reached ();
1547         }
1548     }
1549
1550   g_slist_free (failed);
1551
1552   return pixbuf;
1553 }
1554
1555 extern GtkIconCache *_builtin_cache;
1556
1557 static GdkPixbuf*
1558 render_fallback_image (GtkStyle          *style,
1559                        GtkTextDirection   direction,
1560                        GtkStateType       state,
1561                        GtkIconSize        size,
1562                        GtkWidget         *widget,
1563                        const char        *detail)
1564 {
1565   /* This icon can be used for any direction/state/size */
1566   static GtkIconSource fallback_source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1567
1568   if (fallback_source.type == GTK_ICON_SOURCE_EMPTY)
1569     {
1570       GdkPixbuf *pixbuf;
1571
1572       _gtk_icon_theme_ensure_builtin_cache ();
1573
1574       pixbuf = _gtk_icon_cache_get_icon (_builtin_cache,
1575                                          GTK_STOCK_MISSING_IMAGE,
1576                                          "24");
1577       gtk_icon_source_set_pixbuf (&fallback_source, pixbuf);
1578       g_object_unref (pixbuf);
1579     }
1580   
1581   return gtk_style_render_icon (style,
1582                                 &fallback_source,
1583                                 direction,
1584                                 state,
1585                                 size,
1586                                 widget,
1587                                 detail);
1588 }
1589
1590 /**
1591  * gtk_icon_set_render_icon:
1592  * @icon_set: a #GtkIconSet
1593  * @style: a #GtkStyle associated with @widget, or %NULL
1594  * @direction: text direction
1595  * @state: widget state
1596  * @size: icon size. A size of (GtkIconSize)-1
1597  *        means render at the size of the source and don't scale.
1598  * @widget: widget that will display the icon, or %NULL.
1599  *          The only use that is typically made of this
1600  *          is to determine the appropriate #GdkScreen.
1601  * @detail: detail to pass to the theme engine, or %NULL.
1602  *          Note that passing a detail of anything but %NULL
1603  *          will disable caching.
1604  * 
1605  * Renders an icon using gtk_style_render_icon(). In most cases,
1606  * gtk_widget_render_icon() is better, since it automatically provides
1607  * most of the arguments from the current widget settings.  This
1608  * function never returns %NULL; if the icon can't be rendered
1609  * (perhaps because an image file fails to load), a default "missing
1610  * image" icon will be returned instead.
1611  * 
1612  * Return value: a #GdkPixbuf to be displayed
1613  **/
1614 GdkPixbuf*
1615 gtk_icon_set_render_icon (GtkIconSet        *icon_set,
1616                           GtkStyle          *style,
1617                           GtkTextDirection   direction,
1618                           GtkStateType       state,
1619                           GtkIconSize        size,
1620                           GtkWidget         *widget,
1621                           const char        *detail)
1622 {
1623   GdkPixbuf *icon;
1624   
1625   g_return_val_if_fail (icon_set != NULL, NULL);
1626   g_return_val_if_fail (style == NULL || GTK_IS_STYLE (style), NULL);
1627
1628   if (icon_set->sources == NULL)
1629     return render_fallback_image (style, direction, state, size, widget, detail);
1630
1631   if (detail == NULL)
1632     {
1633       icon = find_in_cache (icon_set, style, direction,
1634                         state, size);
1635       
1636       if (icon)
1637         {
1638           g_object_ref (icon);
1639           return icon;
1640         }
1641     }
1642
1643
1644   icon = find_and_render_icon_source (icon_set, style, direction, state, size,
1645                                       widget, detail);
1646
1647   if (icon == NULL)
1648     icon = render_fallback_image (style, direction, state, size, widget, detail);
1649
1650   if (detail == NULL)
1651     add_to_cache (icon_set, style, direction, state, size, icon);
1652   
1653   return icon;
1654 }
1655
1656 /* Order sources by their "wildness", so that "wilder" sources are
1657  * greater than "specific" sources; for determining ordering,
1658  * direction beats state beats size.
1659  */
1660
1661 static int
1662 icon_source_compare (gconstpointer ap, gconstpointer bp)
1663 {
1664   const GtkIconSource *a = ap;
1665   const GtkIconSource *b = bp;
1666
1667   if (!a->any_direction && b->any_direction)
1668     return -1;
1669   else if (a->any_direction && !b->any_direction)
1670     return 1;
1671   else if (!a->any_state && b->any_state)
1672     return -1;
1673   else if (a->any_state && !b->any_state)
1674     return 1;
1675   else if (!a->any_size && b->any_size)
1676     return -1;
1677   else if (a->any_size && !b->any_size)
1678     return 1;
1679   else
1680     return 0;
1681 }
1682
1683 /**
1684  * gtk_icon_set_add_source:
1685  * @icon_set: a #GtkIconSet
1686  * @source: a #GtkIconSource
1687  *
1688  * Icon sets have a list of #GtkIconSource, which they use as base
1689  * icons for rendering icons in different states and sizes. Icons are
1690  * scaled, made to look insensitive, etc. in
1691  * gtk_icon_set_render_icon(), but #GtkIconSet needs base images to
1692  * work with. The base images and when to use them are described by
1693  * a #GtkIconSource.
1694  * 
1695  * This function copies @source, so you can reuse the same source immediately
1696  * without affecting the icon set.
1697  *
1698  * An example of when you'd use this function: a web browser's "Back
1699  * to Previous Page" icon might point in a different direction in
1700  * Hebrew and in English; it might look different when insensitive;
1701  * and it might change size depending on toolbar mode (small/large
1702  * icons). So a single icon set would contain all those variants of
1703  * the icon, and you might add a separate source for each one.
1704  *
1705  * You should nearly always add a "default" icon source with all
1706  * fields wildcarded, which will be used as a fallback if no more
1707  * specific source matches. #GtkIconSet always prefers more specific
1708  * icon sources to more generic icon sources. The order in which you
1709  * add the sources to the icon set does not matter.
1710  *
1711  * gtk_icon_set_new_from_pixbuf() creates a new icon set with a
1712  * default icon source based on the given pixbuf.
1713  * 
1714  **/
1715 void
1716 gtk_icon_set_add_source (GtkIconSet *icon_set,
1717                          const GtkIconSource *source)
1718 {
1719   g_return_if_fail (icon_set != NULL);
1720   g_return_if_fail (source != NULL);
1721
1722   if (source->type == GTK_ICON_SOURCE_EMPTY)
1723     {
1724       g_warning ("Useless empty GtkIconSource");
1725       return;
1726     }
1727   
1728   icon_set->sources = g_slist_insert_sorted (icon_set->sources,
1729                                              gtk_icon_source_copy (source),
1730                                              icon_source_compare);
1731 }
1732
1733 /**
1734  * gtk_icon_set_get_sizes:
1735  * @icon_set: a #GtkIconSet
1736  * @sizes: return location for array of sizes
1737  * @n_sizes: location to store number of elements in returned array
1738  *
1739  * Obtains a list of icon sizes this icon set can render. The returned
1740  * array must be freed with g_free().
1741  * 
1742  **/
1743 void
1744 gtk_icon_set_get_sizes (GtkIconSet   *icon_set,
1745                         GtkIconSize **sizes,
1746                         gint         *n_sizes)
1747 {
1748   GSList *tmp_list;
1749   gboolean all_sizes = FALSE;
1750   GSList *specifics = NULL;
1751   
1752   g_return_if_fail (icon_set != NULL);
1753   g_return_if_fail (sizes != NULL);
1754   g_return_if_fail (n_sizes != NULL);
1755   
1756   tmp_list = icon_set->sources;
1757   while (tmp_list != NULL)
1758     {
1759       GtkIconSource *source;
1760
1761       source = tmp_list->data;
1762
1763       if (source->any_size)
1764         {
1765           all_sizes = TRUE;
1766           break;
1767         }
1768       else
1769         specifics = g_slist_prepend (specifics, GINT_TO_POINTER (source->size));
1770       
1771       tmp_list = g_slist_next (tmp_list);
1772     }
1773
1774   if (all_sizes)
1775     {
1776       /* Need to find out what sizes exist */
1777       gint i;
1778
1779       init_icon_sizes ();
1780       
1781       *sizes = g_new (GtkIconSize, icon_sizes_used);
1782       *n_sizes = icon_sizes_used - 1;
1783       
1784       i = 1;      
1785       while (i < icon_sizes_used)
1786         {
1787           (*sizes)[i - 1] = icon_sizes[i].size;
1788           ++i;
1789         }
1790     }
1791   else
1792     {
1793       gint i;
1794       
1795       *n_sizes = g_slist_length (specifics);
1796       *sizes = g_new (GtkIconSize, *n_sizes);
1797
1798       i = 0;
1799       tmp_list = specifics;
1800       while (tmp_list != NULL)
1801         {
1802           (*sizes)[i] = GPOINTER_TO_INT (tmp_list->data);
1803
1804           ++i;
1805           tmp_list = g_slist_next (tmp_list);
1806         }
1807     }
1808
1809   g_slist_free (specifics);
1810 }
1811
1812
1813 /**
1814  * gtk_icon_source_new:
1815  * 
1816  * Creates a new #GtkIconSource. A #GtkIconSource contains a #GdkPixbuf (or
1817  * image filename) that serves as the base image for one or more of the
1818  * icons in a #GtkIconSet, along with a specification for which icons in the
1819  * icon set will be based on that pixbuf or image file. An icon set contains
1820  * a set of icons that represent "the same" logical concept in different states,
1821  * different global text directions, and different sizes.
1822  * 
1823  * So for example a web browser's "Back to Previous Page" icon might
1824  * point in a different direction in Hebrew and in English; it might
1825  * look different when insensitive; and it might change size depending
1826  * on toolbar mode (small/large icons). So a single icon set would
1827  * contain all those variants of the icon. #GtkIconSet contains a list
1828  * of #GtkIconSource from which it can derive specific icon variants in
1829  * the set. 
1830  *
1831  * In the simplest case, #GtkIconSet contains one source pixbuf from
1832  * which it derives all variants. The convenience function
1833  * gtk_icon_set_new_from_pixbuf() handles this case; if you only have
1834  * one source pixbuf, just use that function.
1835  *
1836  * If you want to use a different base pixbuf for different icon
1837  * variants, you create multiple icon sources, mark which variants
1838  * they'll be used to create, and add them to the icon set with
1839  * gtk_icon_set_add_source().
1840  *
1841  * By default, the icon source has all parameters wildcarded. That is,
1842  * the icon source will be used as the base icon for any desired text
1843  * direction, widget state, or icon size.
1844  * 
1845  * Return value: a new #GtkIconSource
1846  **/
1847 GtkIconSource*
1848 gtk_icon_source_new (void)
1849 {
1850   GtkIconSource *src;
1851   
1852   src = g_new0 (GtkIconSource, 1);
1853
1854   src->direction = GTK_TEXT_DIR_NONE;
1855   src->size = GTK_ICON_SIZE_INVALID;
1856   src->state = GTK_STATE_NORMAL;
1857   
1858   src->any_direction = TRUE;
1859   src->any_state = TRUE;
1860   src->any_size = TRUE;
1861   
1862   return src;
1863 }
1864
1865 /**
1866  * gtk_icon_source_copy:
1867  * @source: a #GtkIconSource
1868  * 
1869  * Creates a copy of @source; mostly useful for language bindings.
1870  * 
1871  * Return value: a new #GtkIconSource
1872  **/
1873 GtkIconSource*
1874 gtk_icon_source_copy (const GtkIconSource *source)
1875 {
1876   GtkIconSource *copy;
1877   
1878   g_return_val_if_fail (source != NULL, NULL);
1879
1880   copy = g_new (GtkIconSource, 1);
1881
1882   *copy = *source;
1883   
1884   switch (copy->type)
1885     {
1886     case GTK_ICON_SOURCE_EMPTY:
1887     case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1888       break;
1889     case GTK_ICON_SOURCE_ICON_NAME:
1890       copy->source.icon_name = g_strdup (copy->source.icon_name);
1891       break;
1892     case GTK_ICON_SOURCE_FILENAME:
1893       copy->source.filename = g_strdup (copy->source.filename);
1894 #ifdef G_OS_WIN32
1895       copy->cp_filename = g_strdup (copy->cp_filename);
1896 #endif
1897       if (copy->filename_pixbuf)
1898         g_object_ref (copy->filename_pixbuf);
1899       break;
1900     case GTK_ICON_SOURCE_PIXBUF:
1901       g_object_ref (copy->source.pixbuf);
1902       break;
1903     default:
1904       g_assert_not_reached();
1905     }
1906
1907   return copy;
1908 }
1909
1910 /**
1911  * gtk_icon_source_free:
1912  * @source: a #GtkIconSource
1913  * 
1914  * Frees a dynamically-allocated icon source, along with its
1915  * filename, size, and pixbuf fields if those are not %NULL.
1916  **/
1917 void
1918 gtk_icon_source_free (GtkIconSource *source)
1919 {
1920   g_return_if_fail (source != NULL);
1921
1922   icon_source_clear (source);
1923   g_free (source);
1924 }
1925
1926 GType
1927 gtk_icon_source_get_type (void)
1928 {
1929   static GType our_type = 0;
1930   
1931   if (our_type == 0)
1932     our_type = g_boxed_type_register_static (I_("GtkIconSource"),
1933                                              (GBoxedCopyFunc) gtk_icon_source_copy,
1934                                              (GBoxedFreeFunc) gtk_icon_source_free);
1935
1936   return our_type;
1937 }
1938
1939 static void
1940 icon_source_clear (GtkIconSource *source)
1941 {
1942   switch (source->type)
1943     {
1944     case GTK_ICON_SOURCE_EMPTY:
1945       break;
1946     case GTK_ICON_SOURCE_ICON_NAME:
1947       g_free (source->source.icon_name);
1948       /* fall thru */
1949     case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1950       source->source.icon_name = NULL;
1951       break;
1952     case GTK_ICON_SOURCE_FILENAME:
1953       g_free (source->source.filename);
1954       source->source.filename = NULL;
1955 #ifdef G_OS_WIN32
1956       g_free (source->cp_filename);
1957       source->cp_filename = NULL;
1958 #endif
1959       if (source->filename_pixbuf) 
1960         g_object_unref (source->filename_pixbuf);
1961       source->filename_pixbuf = NULL;
1962       break;
1963     case GTK_ICON_SOURCE_PIXBUF:
1964       g_object_unref (source->source.pixbuf);
1965       source->source.pixbuf = NULL;
1966       break;
1967     default:
1968       g_assert_not_reached();
1969     }
1970
1971   source->type = GTK_ICON_SOURCE_EMPTY;
1972 }
1973
1974 /**
1975  * gtk_icon_source_set_filename:
1976  * @source: a #GtkIconSource
1977  * @filename: image file to use
1978  *
1979  * Sets the name of an image file to use as a base image when creating
1980  * icon variants for #GtkIconSet. The filename must be absolute. 
1981  **/
1982 void
1983 gtk_icon_source_set_filename (GtkIconSource *source,
1984                               const gchar   *filename)
1985 {
1986   g_return_if_fail (source != NULL);
1987   g_return_if_fail (filename == NULL || g_path_is_absolute (filename));
1988
1989   if (source->type == GTK_ICON_SOURCE_FILENAME &&
1990       source->source.filename == filename)
1991     return;
1992   
1993   icon_source_clear (source);
1994   
1995   if (filename != NULL)
1996     {
1997       source->type = GTK_ICON_SOURCE_FILENAME;
1998       source->source.filename = g_strdup (filename);
1999 #ifdef G_OS_WIN32
2000       source->cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
2001 #endif
2002     }
2003 }
2004
2005 /**
2006  * gtk_icon_source_set_icon_name
2007  * @source: a #GtkIconSource
2008  * @icon_name: name of icon to use
2009  *
2010  * Sets the name of an icon to look up in the current icon theme
2011  * to use as a base image when creating icon variants for #GtkIconSet.
2012  **/
2013 void
2014 gtk_icon_source_set_icon_name (GtkIconSource *source,
2015                                const gchar   *icon_name)
2016 {
2017   g_return_if_fail (source != NULL);
2018
2019   if (source->type == GTK_ICON_SOURCE_ICON_NAME &&
2020       source->source.icon_name == icon_name)
2021     return;
2022
2023   icon_source_clear (source);
2024   
2025   if (icon_name != NULL)
2026     {
2027       source->type = GTK_ICON_SOURCE_ICON_NAME;
2028       source->source.icon_name = g_strdup (icon_name);
2029     }
2030 }
2031
2032 /**
2033  * gtk_icon_source_set_pixbuf:
2034  * @source: a #GtkIconSource
2035  * @pixbuf: pixbuf to use as a source
2036  *
2037  * Sets a pixbuf to use as a base image when creating icon variants
2038  * for #GtkIconSet.
2039  **/
2040 void
2041 gtk_icon_source_set_pixbuf (GtkIconSource *source,
2042                             GdkPixbuf     *pixbuf)
2043 {
2044   g_return_if_fail (source != NULL);
2045   g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
2046   
2047   if (source->type == GTK_ICON_SOURCE_PIXBUF &&
2048       source->source.pixbuf == pixbuf)
2049     return;
2050
2051   icon_source_clear (source);
2052   
2053   if (pixbuf != NULL)
2054     {
2055       source->type = GTK_ICON_SOURCE_PIXBUF;
2056       source->source.pixbuf = g_object_ref (pixbuf);
2057     }
2058 }
2059
2060 /**
2061  * gtk_icon_source_get_filename:
2062  * @source: a #GtkIconSource
2063  * 
2064  * Retrieves the source filename, or %NULL if none is set. The
2065  * filename is not a copy, and should not be modified or expected to
2066  * persist beyond the lifetime of the icon source.
2067  * 
2068  * Return value: image filename. This string must not be modified
2069  * or freed.
2070  **/
2071 G_CONST_RETURN gchar*
2072 gtk_icon_source_get_filename (const GtkIconSource *source)
2073 {
2074   g_return_val_if_fail (source != NULL, NULL);
2075
2076   if (source->type == GTK_ICON_SOURCE_FILENAME)
2077     return source->source.filename;
2078   else
2079     return NULL;
2080 }
2081
2082 /**
2083  * gtk_icon_source_get_icon_name:
2084  * @source: a #GtkIconSource
2085  * 
2086  * Retrieves the source icon name, or %NULL if none is set. The
2087  * icon_name is not a copy, and should not be modified or expected to
2088  * persist beyond the lifetime of the icon source.
2089  * 
2090  * Return value: icon name. This string must not be modified or freed.
2091  **/
2092 G_CONST_RETURN gchar*
2093 gtk_icon_source_get_icon_name (const GtkIconSource *source)
2094 {
2095   g_return_val_if_fail (source != NULL, NULL);
2096
2097   if (source->type == GTK_ICON_SOURCE_ICON_NAME ||
2098      source->type == GTK_ICON_SOURCE_STATIC_ICON_NAME)
2099     return source->source.icon_name;
2100   else
2101     return NULL;
2102 }
2103
2104 /**
2105  * gtk_icon_source_get_pixbuf:
2106  * @source: a #GtkIconSource
2107  * 
2108  * Retrieves the source pixbuf, or %NULL if none is set.
2109  * In addition, if a filename source is in use, this
2110  * function in some cases will return the pixbuf from
2111  * loaded from the filename. This is, for example, true
2112  * for the GtkIconSource passed to the GtkStyle::render_icon()
2113  * virtual function. The reference count on the pixbuf is
2114  * not incremented.
2115  * 
2116  * Return value: source pixbuf
2117  **/
2118 GdkPixbuf*
2119 gtk_icon_source_get_pixbuf (const GtkIconSource *source)
2120 {
2121   g_return_val_if_fail (source != NULL, NULL);
2122   
2123   if (source->type == GTK_ICON_SOURCE_PIXBUF)
2124     return source->source.pixbuf;
2125   else if (source->type == GTK_ICON_SOURCE_FILENAME)
2126     return source->filename_pixbuf;
2127   else
2128     return NULL;
2129 }
2130
2131 /**
2132  * gtk_icon_source_set_direction_wildcarded:
2133  * @source: a #GtkIconSource
2134  * @setting: %TRUE to wildcard the text direction
2135  *
2136  * If the text direction is wildcarded, this source can be used
2137  * as the base image for an icon in any #GtkTextDirection.
2138  * If the text direction is not wildcarded, then the
2139  * text direction the icon source applies to should be set
2140  * with gtk_icon_source_set_direction(), and the icon source
2141  * will only be used with that text direction.
2142  *
2143  * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2144  * wildcarded sources, and will use an exact match when possible.
2145  * 
2146  **/
2147 void
2148 gtk_icon_source_set_direction_wildcarded (GtkIconSource *source,
2149                                           gboolean       setting)
2150 {
2151   g_return_if_fail (source != NULL);
2152
2153   source->any_direction = setting != FALSE;
2154 }
2155
2156 /**
2157  * gtk_icon_source_set_state_wildcarded:
2158  * @source: a #GtkIconSource
2159  * @setting: %TRUE to wildcard the widget state
2160  *
2161  * If the widget state is wildcarded, this source can be used as the
2162  * base image for an icon in any #GtkStateType.  If the widget state
2163  * is not wildcarded, then the state the source applies to should be
2164  * set with gtk_icon_source_set_state() and the icon source will
2165  * only be used with that specific state.
2166  *
2167  * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2168  * wildcarded sources, and will use an exact match when possible.
2169  *
2170  * #GtkIconSet will normally transform wildcarded source images to
2171  * produce an appropriate icon for a given state, for example
2172  * lightening an image on prelight, but will not modify source images
2173  * that match exactly.
2174  **/
2175 void
2176 gtk_icon_source_set_state_wildcarded (GtkIconSource *source,
2177                                       gboolean       setting)
2178 {
2179   g_return_if_fail (source != NULL);
2180
2181   source->any_state = setting != FALSE;
2182 }
2183
2184
2185 /**
2186  * gtk_icon_source_set_size_wildcarded:
2187  * @source: a #GtkIconSource
2188  * @setting: %TRUE to wildcard the widget state
2189  *
2190  * If the icon size is wildcarded, this source can be used as the base
2191  * image for an icon of any size.  If the size is not wildcarded, then
2192  * the size the source applies to should be set with
2193  * gtk_icon_source_set_size() and the icon source will only be used
2194  * with that specific size.
2195  *
2196  * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2197  * wildcarded sources, and will use an exact match when possible.
2198  *
2199  * #GtkIconSet will normally scale wildcarded source images to produce
2200  * an appropriate icon at a given size, but will not change the size
2201  * of source images that match exactly.
2202  **/
2203 void
2204 gtk_icon_source_set_size_wildcarded (GtkIconSource *source,
2205                                      gboolean       setting)
2206 {
2207   g_return_if_fail (source != NULL);
2208
2209   source->any_size = setting != FALSE;  
2210 }
2211
2212 /**
2213  * gtk_icon_source_get_size_wildcarded:
2214  * @source: a #GtkIconSource
2215  * 
2216  * Gets the value set by gtk_icon_source_set_size_wildcarded().
2217  * 
2218  * Return value: %TRUE if this icon source is a base for any icon size variant
2219  **/
2220 gboolean
2221 gtk_icon_source_get_size_wildcarded (const GtkIconSource *source)
2222 {
2223   g_return_val_if_fail (source != NULL, TRUE);
2224   
2225   return source->any_size;
2226 }
2227
2228 /**
2229  * gtk_icon_source_get_state_wildcarded:
2230  * @source: a #GtkIconSource
2231  * 
2232  * Gets the value set by gtk_icon_source_set_state_wildcarded().
2233  * 
2234  * Return value: %TRUE if this icon source is a base for any widget state variant
2235  **/
2236 gboolean
2237 gtk_icon_source_get_state_wildcarded (const GtkIconSource *source)
2238 {
2239   g_return_val_if_fail (source != NULL, TRUE);
2240
2241   return source->any_state;
2242 }
2243
2244 /**
2245  * gtk_icon_source_get_direction_wildcarded:
2246  * @source: a #GtkIconSource
2247  * 
2248  * Gets the value set by gtk_icon_source_set_direction_wildcarded().
2249  * 
2250  * Return value: %TRUE if this icon source is a base for any text direction variant
2251  **/
2252 gboolean
2253 gtk_icon_source_get_direction_wildcarded (const GtkIconSource *source)
2254 {
2255   g_return_val_if_fail (source != NULL, TRUE);
2256
2257   return source->any_direction;
2258 }
2259
2260 /**
2261  * gtk_icon_source_set_direction:
2262  * @source: a #GtkIconSource
2263  * @direction: text direction this source applies to
2264  *
2265  * Sets the text direction this icon source is intended to be used
2266  * with.
2267  * 
2268  * Setting the text direction on an icon source makes no difference
2269  * if the text direction is wildcarded. Therefore, you should usually
2270  * call gtk_icon_source_set_direction_wildcarded() to un-wildcard it
2271  * in addition to calling this function.
2272  * 
2273  **/
2274 void
2275 gtk_icon_source_set_direction (GtkIconSource   *source,
2276                                GtkTextDirection direction)
2277 {
2278   g_return_if_fail (source != NULL);
2279
2280   source->direction = direction;
2281 }
2282
2283 /**
2284  * gtk_icon_source_set_state:
2285  * @source: a #GtkIconSource
2286  * @state: widget state this source applies to
2287  *
2288  * Sets the widget state this icon source is intended to be used
2289  * with.
2290  * 
2291  * Setting the widget state on an icon source makes no difference
2292  * if the state is wildcarded. Therefore, you should usually
2293  * call gtk_icon_source_set_state_wildcarded() to un-wildcard it
2294  * in addition to calling this function.
2295  * 
2296  **/
2297 void
2298 gtk_icon_source_set_state (GtkIconSource *source,
2299                            GtkStateType   state)
2300 {
2301   g_return_if_fail (source != NULL);
2302
2303   source->state = state;
2304 }
2305
2306 /**
2307  * gtk_icon_source_set_size:
2308  * @source: a #GtkIconSource
2309  * @size: icon size this source applies to
2310  *
2311  * Sets the icon size this icon source is intended to be used
2312  * with.
2313  * 
2314  * Setting the icon size on an icon source makes no difference
2315  * if the size is wildcarded. Therefore, you should usually
2316  * call gtk_icon_source_set_size_wildcarded() to un-wildcard it
2317  * in addition to calling this function.
2318  * 
2319  **/
2320 void
2321 gtk_icon_source_set_size (GtkIconSource *source,
2322                           GtkIconSize    size)
2323 {
2324   g_return_if_fail (source != NULL);
2325
2326   source->size = size;
2327 }
2328
2329 /**
2330  * gtk_icon_source_get_direction:
2331  * @source: a #GtkIconSource
2332  * 
2333  * Obtains the text direction this icon source applies to. The return
2334  * value is only useful/meaningful if the text direction is <emphasis>not</emphasis> 
2335  * wildcarded.
2336  * 
2337  * Return value: text direction this source matches
2338  **/
2339 GtkTextDirection
2340 gtk_icon_source_get_direction (const GtkIconSource *source)
2341 {
2342   g_return_val_if_fail (source != NULL, 0);
2343
2344   return source->direction;
2345 }
2346
2347 /**
2348  * gtk_icon_source_get_state:
2349  * @source: a #GtkIconSource
2350  * 
2351  * Obtains the widget state this icon source applies to. The return
2352  * value is only useful/meaningful if the widget state is <emphasis>not</emphasis>
2353  * wildcarded.
2354  * 
2355  * Return value: widget state this source matches
2356  **/
2357 GtkStateType
2358 gtk_icon_source_get_state (const GtkIconSource *source)
2359 {
2360   g_return_val_if_fail (source != NULL, 0);
2361
2362   return source->state;
2363 }
2364
2365 /**
2366  * gtk_icon_source_get_size:
2367  * @source: a #GtkIconSource
2368  * 
2369  * Obtains the icon size this source applies to. The return value
2370  * is only useful/meaningful if the icon size is <emphasis>not</emphasis> wildcarded.
2371  * 
2372  * Return value: icon size this source matches.
2373  **/
2374 GtkIconSize
2375 gtk_icon_source_get_size (const GtkIconSource *source)
2376 {
2377   g_return_val_if_fail (source != NULL, 0);
2378
2379   return source->size;
2380 }
2381
2382 #define NUM_CACHED_ICONS 8
2383
2384 typedef struct _CachedIcon CachedIcon;
2385
2386 struct _CachedIcon
2387 {
2388   /* These must all match to use the cached pixbuf.
2389    * If any don't match, we must re-render the pixbuf.
2390    */
2391   GtkStyle *style;
2392   GtkTextDirection direction;
2393   GtkStateType state;
2394   GtkIconSize size;
2395
2396   GdkPixbuf *pixbuf;
2397 };
2398
2399 static void
2400 ensure_cache_up_to_date (GtkIconSet *icon_set)
2401 {
2402   if (icon_set->cache_serial != cache_serial)
2403     {
2404       clear_cache (icon_set, TRUE);
2405       icon_set->cache_serial = cache_serial;
2406     }
2407 }
2408
2409 static void
2410 cached_icon_free (CachedIcon *icon)
2411 {
2412   g_object_unref (icon->pixbuf);
2413
2414   if (icon->style)
2415     g_object_unref (icon->style);
2416
2417   g_free (icon);
2418 }
2419
2420 static GdkPixbuf *
2421 find_in_cache (GtkIconSet      *icon_set,
2422                GtkStyle        *style,
2423                GtkTextDirection direction,
2424                GtkStateType     state,
2425                GtkIconSize      size)
2426 {
2427   GSList *tmp_list;
2428   GSList *prev;
2429
2430   ensure_cache_up_to_date (icon_set);
2431   
2432   prev = NULL;
2433   tmp_list = icon_set->cache;
2434   while (tmp_list != NULL)
2435     {
2436       CachedIcon *icon = tmp_list->data;
2437
2438       if (icon->style == style &&
2439           icon->direction == direction &&
2440           icon->state == state &&
2441           icon->size == size)
2442         {
2443           if (prev)
2444             {
2445               /* Move this icon to the front of the list. */
2446               prev->next = tmp_list->next;
2447               tmp_list->next = icon_set->cache;
2448               icon_set->cache = tmp_list;
2449             }
2450           
2451           return icon->pixbuf;
2452         }
2453           
2454       prev = tmp_list;
2455       tmp_list = g_slist_next (tmp_list);
2456     }
2457
2458   return NULL;
2459 }
2460
2461 static void
2462 add_to_cache (GtkIconSet      *icon_set,
2463               GtkStyle        *style,
2464               GtkTextDirection direction,
2465               GtkStateType     state,
2466               GtkIconSize      size,
2467               GdkPixbuf       *pixbuf)
2468 {
2469   CachedIcon *icon;
2470
2471   ensure_cache_up_to_date (icon_set);
2472
2473   g_object_ref (pixbuf);
2474
2475   /* We have to ref the style, since if the style was finalized
2476    * its address could be reused by another style, creating a
2477    * really weird bug
2478    */
2479   
2480   if (style)
2481     g_object_ref (style);
2482
2483   icon = g_new (CachedIcon, 1);
2484   icon_set->cache = g_slist_prepend (icon_set->cache, icon);
2485   icon_set->cache_size++;
2486
2487   icon->style = style;
2488   icon->direction = direction;
2489   icon->state = state;
2490   icon->size = size;
2491   icon->pixbuf = pixbuf;
2492
2493   if (icon->style)
2494     attach_to_style (icon_set, icon->style);
2495   
2496   if (icon_set->cache_size >= NUM_CACHED_ICONS)
2497     {
2498       /* Remove oldest item in the cache */
2499       GSList *tmp_list;
2500       
2501       tmp_list = icon_set->cache;
2502
2503       /* Find next-to-last link */
2504       g_assert (NUM_CACHED_ICONS > 2);
2505       while (tmp_list->next->next)
2506         tmp_list = tmp_list->next;
2507
2508       g_assert (tmp_list != NULL);
2509       g_assert (tmp_list->next != NULL);
2510       g_assert (tmp_list->next->next == NULL);
2511
2512       /* Free the last icon */
2513       icon = tmp_list->next->data;
2514
2515       g_slist_free (tmp_list->next);
2516       tmp_list->next = NULL;
2517
2518       cached_icon_free (icon);
2519     }
2520 }
2521
2522 static void
2523 clear_cache (GtkIconSet *icon_set,
2524              gboolean    style_detach)
2525 {
2526   GSList *cache, *tmp_list;
2527   GtkStyle *last_style = NULL;
2528
2529   cache = icon_set->cache;
2530   icon_set->cache = NULL;
2531   icon_set->cache_size = 0;
2532   tmp_list = cache;
2533   while (tmp_list != NULL)
2534     {
2535       CachedIcon *icon = tmp_list->data;
2536
2537       if (style_detach)
2538         {
2539           /* simple optimization for the case where the cache
2540            * contains contiguous icons from the same style.
2541            * it's safe to call detach_from_style more than
2542            * once on the same style though.
2543            */
2544           if (last_style != icon->style)
2545             {
2546               detach_from_style (icon_set, icon->style);
2547               last_style = icon->style;
2548             }
2549         }
2550       
2551       cached_icon_free (icon);      
2552       
2553       tmp_list = g_slist_next (tmp_list);
2554     }
2555
2556   g_slist_free (cache);
2557 }
2558
2559 static GSList*
2560 copy_cache (GtkIconSet *icon_set,
2561             GtkIconSet *copy_recipient)
2562 {
2563   GSList *tmp_list;
2564   GSList *copy = NULL;
2565
2566   ensure_cache_up_to_date (icon_set);
2567   
2568   tmp_list = icon_set->cache;
2569   while (tmp_list != NULL)
2570     {
2571       CachedIcon *icon = tmp_list->data;
2572       CachedIcon *icon_copy = g_new (CachedIcon, 1);
2573
2574       *icon_copy = *icon;
2575
2576       if (icon_copy->style)
2577         {
2578           attach_to_style (copy_recipient, icon_copy->style);
2579           g_object_ref (icon_copy->style);
2580         }
2581         
2582       g_object_ref (icon_copy->pixbuf);
2583
2584       icon_copy->size = icon->size;
2585       
2586       copy = g_slist_prepend (copy, icon_copy);      
2587       
2588       tmp_list = g_slist_next (tmp_list);
2589     }
2590
2591   return g_slist_reverse (copy);
2592 }
2593
2594 static void
2595 attach_to_style (GtkIconSet *icon_set,
2596                  GtkStyle   *style)
2597 {
2598   GHashTable *table;
2599
2600   table = g_object_get_qdata (G_OBJECT (style),
2601                               g_quark_try_string ("gtk-style-icon-sets"));
2602
2603   if (table == NULL)
2604     {
2605       table = g_hash_table_new (NULL, NULL);
2606       g_object_set_qdata_full (G_OBJECT (style),
2607                                g_quark_from_static_string ("gtk-style-icon-sets"),
2608                                table,
2609                                style_dnotify);
2610     }
2611
2612   g_hash_table_insert (table, icon_set, icon_set);
2613 }
2614
2615 static void
2616 detach_from_style (GtkIconSet *icon_set,
2617                    GtkStyle   *style)
2618 {
2619   GHashTable *table;
2620
2621   table = g_object_get_qdata (G_OBJECT (style),
2622                               g_quark_try_string ("gtk-style-icon-sets"));
2623
2624   if (table != NULL)
2625     g_hash_table_remove (table, icon_set);
2626 }
2627
2628 static void
2629 iconsets_foreach (gpointer key,
2630                   gpointer value,
2631                   gpointer user_data)
2632 {
2633   GtkIconSet *icon_set = key;
2634
2635   /* We only need to remove cache entries for the given style;
2636    * but that complicates things because in destroy notify
2637    * we don't know which style got destroyed, and 95% of the
2638    * time all cache entries will have the same style,
2639    * so this is faster anyway.
2640    */
2641   
2642   clear_cache (icon_set, FALSE);
2643 }
2644
2645 static void
2646 style_dnotify (gpointer data)
2647 {
2648   GHashTable *table = data;
2649   
2650   g_hash_table_foreach (table, iconsets_foreach, NULL);
2651
2652   g_hash_table_destroy (table);
2653 }
2654
2655 /* This allows the icon set to detect that its cache is out of date. */
2656 void
2657 _gtk_icon_set_invalidate_caches (void)
2658 {
2659   ++cache_serial;
2660 }
2661
2662 /**
2663  * _gtk_icon_factory_list_ids:
2664  * 
2665  * Gets all known IDs stored in an existing icon factory.
2666  * The strings in the returned list aren't copied.
2667  * The list itself should be freed.
2668  * 
2669  * Return value: List of ids in icon factories
2670  **/
2671 GList*
2672 _gtk_icon_factory_list_ids (void)
2673 {
2674   GSList *tmp_list;
2675   GList *ids;
2676
2677   ids = NULL;
2678
2679   _gtk_icon_factory_ensure_default_icons ();
2680   
2681   tmp_list = all_icon_factories;
2682   while (tmp_list != NULL)
2683     {
2684       GList *these_ids;
2685       
2686       GtkIconFactory *factory = GTK_ICON_FACTORY (tmp_list->data);
2687
2688       these_ids = g_hash_table_get_keys (factory->icons);
2689       
2690       ids = g_list_concat (ids, these_ids);
2691       
2692       tmp_list = g_slist_next (tmp_list);
2693     }
2694
2695   return ids;
2696 }
2697
2698 #ifdef G_OS_WIN32
2699
2700 /* DLL ABI stability backward compatibility versions */
2701
2702 #undef gtk_icon_source_set_filename
2703
2704 void
2705 gtk_icon_source_set_filename (GtkIconSource *source,
2706                               const gchar   *filename)
2707 {
2708   gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2709
2710   gtk_icon_source_set_filename_utf8 (source, utf8_filename);
2711
2712   g_free (utf8_filename);
2713 }
2714
2715 #undef gtk_icon_source_get_filename
2716
2717 G_CONST_RETURN gchar*
2718 gtk_icon_source_get_filename (const GtkIconSource *source)
2719 {
2720   g_return_val_if_fail (source != NULL, NULL);
2721
2722   if (source->type == GTK_ICON_SOURCE_FILENAME)
2723     return source->cp_filename;
2724   else
2725     return NULL;
2726 }
2727
2728 #endif
2729
2730 #define __GTK_ICON_FACTORY_C__
2731 #include "gtkaliasdef.c"