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