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