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