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