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