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