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