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