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