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