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