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