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