]> Pileus Git - ~andy/gtk/blob - gtk/gtkicontheme.c
Give working examples in the docs. (#330944, John Spray)
[~andy/gtk] / gtk / gtkicontheme.c
1 /* GtkIconTheme - a loader for icon themes
2  * gtk-icon-theme.c Copyright (C) 2002, 2003 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 #include "config.h"
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 #include <string.h>
28 #include <stdlib.h>
29 #include <glib.h>
30 #include <glib/gstdio.h>
31
32 #ifdef G_OS_WIN32
33 #ifndef S_ISDIR
34 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
35 #endif
36 #endif /* G_OS_WIN32 */
37
38 #include "gtkicontheme.h"
39 #include "gtkiconfactory.h"
40 #include "gtkiconcache.h"
41 #include "gtkbuiltincache.h"
42 #include "gtkintl.h"
43 #include "gtksettings.h"
44 #include "gtkprivate.h"
45 #include "gtkalias.h"
46
47 #define DEFAULT_THEME_NAME "hicolor"
48
49 typedef enum
50 {
51   ICON_THEME_DIR_FIXED,  
52   ICON_THEME_DIR_SCALABLE,  
53   ICON_THEME_DIR_THRESHOLD,
54   ICON_THEME_DIR_UNTHEMED
55 } IconThemeDirType;
56
57 /* In reverse search order: */
58 typedef enum
59 {
60   ICON_SUFFIX_NONE = 0,
61   ICON_SUFFIX_XPM = 1 << 0,
62   ICON_SUFFIX_SVG = 1 << 1,
63   ICON_SUFFIX_PNG = 1 << 2,
64   HAS_ICON_FILE = 1 << 3
65 } IconSuffix;
66
67
68 struct _GtkIconThemePrivate
69 {
70   guint custom_theme : 1;
71   guint is_screen_singleton : 1;
72   guint pixbuf_supports_svg : 1;
73   
74   char *current_theme;
75   char *fallback_theme;
76   char **search_path;
77   int search_path_len;
78
79   gboolean themes_valid;
80   /* A list of all the themes needed to look up icons.
81    * In search order, without duplicates
82    */
83   GList *themes;
84   GHashTable *unthemed_icons;
85   
86   /* Note: The keys of this hashtable are owned by the
87    * themedir and unthemed hashtables.
88    */
89   GHashTable *all_icons;
90
91   /* GdkScreen for the icon theme (may be NULL)
92    */
93   GdkScreen *screen;
94   
95   /* time when we last stat:ed for theme changes */
96   long last_stat_time;
97   GList *dir_mtimes;
98
99   gulong reset_styles_idle;
100
101   gboolean check_reload;
102 };
103
104 struct _GtkIconInfo
105 {
106   /* Information about the source
107    */
108   gchar *filename;
109 #ifdef G_OS_WIN32
110   /* System codepage version of filename, for DLL ABI backward
111    * compatibility functions.
112    */
113   gchar *cp_filename;
114 #endif
115   /* Cache pixbuf (if there is any) */
116   GdkPixbuf *cache_pixbuf;
117
118   GtkIconData *data;
119   
120   /* Information about the directory where
121    * the source was found
122    */
123   IconThemeDirType dir_type;
124   gint dir_size;
125   gint threshold;
126
127   /* Parameters influencing the scaled icon
128    */
129   gint desired_size;
130   gboolean raw_coordinates;
131
132   /* Cached information if we go ahead and try to load
133    * the icon.
134    */
135   GdkPixbuf *pixbuf;
136   GError *load_error;
137   gdouble scale;
138 };
139
140 typedef struct
141 {
142   char *name;
143   char *display_name;
144   char *comment;
145   char *example;
146
147   /* In search order */
148   GList *dirs;
149 } IconTheme;
150
151 typedef struct
152 {
153   IconThemeDirType type;
154   GQuark context;
155
156   int size;
157   int min_size;
158   int max_size;
159   int threshold;
160
161   char *dir;
162   char *subdir;
163   
164   GtkIconCache *cache;
165   
166   GHashTable *icons;
167   GHashTable *icon_data;
168 } IconThemeDir;
169
170 typedef struct
171 {
172   char *svg_filename;
173   char *no_svg_filename;
174 } UnthemedIcon;
175
176 typedef struct
177 {
178   gint size;
179   GdkPixbuf *pixbuf;
180 } BuiltinIcon;
181
182 typedef struct 
183 {
184   char *dir;
185   time_t mtime; /* 0 == not existing or not a dir */
186
187   GtkIconCache *cache;
188 } IconThemeDirMtime;
189
190 static void  gtk_icon_theme_class_init (GtkIconThemeClass    *klass);
191 static void  gtk_icon_theme_init       (GtkIconTheme         *icon_theme);
192 static void  gtk_icon_theme_finalize   (GObject              *object);
193 static void  theme_dir_destroy         (IconThemeDir         *dir);
194
195 static void         theme_destroy     (IconTheme        *theme);
196 static GtkIconInfo *theme_lookup_icon (IconTheme        *theme,
197                                        const char       *icon_name,
198                                        int               size,
199                                        gboolean          allow_svg,
200                                        gboolean          use_default_icons);
201 static void         theme_list_icons  (IconTheme        *theme,
202                                        GHashTable       *icons,
203                                        GQuark            context);
204 static void         theme_subdir_load (GtkIconTheme     *icon_theme,
205                                        IconTheme        *theme,
206                                        GKeyFile         *theme_file,
207                                        char             *subdir);
208 static void         do_theme_change   (GtkIconTheme     *icon_theme);
209
210 static void  blow_themes               (GtkIconTheme    *icon_themes);
211
212 static void  icon_data_free            (GtkIconData     *icon_data);
213 static void load_icon_data             (IconThemeDir    *dir,
214                                         const char      *path,
215                                         const char      *name);
216
217 static IconSuffix theme_dir_get_icon_suffix (IconThemeDir *dir,
218                                              const gchar  *icon_name,
219                                              gboolean     *has_icon_file);
220
221
222 static GtkIconInfo *icon_info_new             (void);
223 static GtkIconInfo *icon_info_new_builtin     (BuiltinIcon *icon);
224
225 static IconSuffix suffix_from_name (const char *name);
226
227 static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
228                                        gint        size,
229                                        gint        *min_difference_p,
230                                        gboolean    *has_larger_p);
231
232 static GObjectClass *parent_class = NULL;
233
234 static guint signal_changed = 0;
235
236 static GHashTable *icon_theme_builtin_icons;
237
238 /* also used in gtkiconfactory.c */
239 GtkIconCache *_builtin_cache = NULL;
240 static GList *builtin_dirs = NULL;
241
242
243 GType
244 gtk_icon_theme_get_type (void)
245 {
246   static GType type = 0;
247
248   if (type == 0)
249     {
250       static const GTypeInfo info =
251         {
252           sizeof (GtkIconThemeClass),
253           NULL,           /* base_init */
254           NULL,           /* base_finalize */
255           (GClassInitFunc) gtk_icon_theme_class_init,
256           NULL,           /* class_finalize */
257           NULL,           /* class_data */
258           sizeof (GtkIconTheme),
259           0,              /* n_preallocs */
260           (GInstanceInitFunc) gtk_icon_theme_init,
261         };
262
263       type = g_type_register_static (G_TYPE_OBJECT, I_("GtkIconTheme"), &info, 0);
264     }
265
266   return type;
267 }
268
269 /**
270  * gtk_icon_theme_new:
271  * 
272  * Creates a new icon theme object. Icon theme objects are used
273  * to lookup up an icon by name in a particular icon theme.
274  * Usually, you'll want to use gtk_icon_theme_get_default()
275  * or gtk_icon_theme_get_for_screen() rather than creating
276  * a new icon theme object for scratch.
277  * 
278  * Return value: the newly created #GtkIconTheme object.
279  *
280  * Since: 2.4
281  **/
282 GtkIconTheme *
283 gtk_icon_theme_new (void)
284 {
285   return g_object_new (GTK_TYPE_ICON_THEME, NULL);
286 }
287
288 /**
289  * gtk_icon_theme_get_default:
290  * 
291  * Gets the icon theme for the default screen. See
292  * gtk_icon_theme_get_for_screen().
293  * 
294  * Return value: A unique #GtkIconTheme associated with
295  *  the default screen. This icon theme is associated with
296  *  the screen and can be used as long as the screen
297  *  is open. Do not ref or unref it.
298  *
299  * Since: 2.4
300  **/
301 GtkIconTheme *
302 gtk_icon_theme_get_default (void)
303 {
304   return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
305 }
306
307 /**
308  * gtk_icon_theme_get_for_screen:
309  * @screen: a #GdkScreen
310  * 
311  * Gets the icon theme object associated with @screen; if this
312  * function has not previously been called for the given
313  * screen, a new icon theme object will be created and
314  * associated with the screen. Icon theme objects are
315  * fairly expensive to create, so using this function
316  * is usually a better choice than calling than gtk_icon_theme_new()
317  * and setting the screen yourself; by using this function
318  * a single icon theme object will be shared between users.
319  * 
320  * Return value: A unique #GtkIconTheme associated with
321  *  the given screen. This icon theme is associated with
322  *  the screen and can be used as long as the screen
323  *  is open. Do not ref or unref it.
324  *
325  * Since: 2.4
326  **/
327 GtkIconTheme *
328 gtk_icon_theme_get_for_screen (GdkScreen *screen)
329 {
330   GtkIconTheme *icon_theme;
331
332   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
333   g_return_val_if_fail (!screen->closed, NULL);
334
335   icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
336   if (!icon_theme)
337     {
338       GtkIconThemePrivate *priv;
339
340       icon_theme = gtk_icon_theme_new ();
341       gtk_icon_theme_set_screen (icon_theme, screen);
342
343       priv = icon_theme->priv;
344       priv->is_screen_singleton = TRUE;
345
346       g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), icon_theme);
347     }
348
349   return icon_theme;
350 }
351
352 static void
353 gtk_icon_theme_class_init (GtkIconThemeClass *klass)
354 {
355   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
356
357   parent_class = g_type_class_peek_parent (klass);
358
359   gobject_class->finalize = gtk_icon_theme_finalize;
360
361 /**
362  * GtkIconTheme::changed
363  * @icon_theme: the icon theme
364  * 
365  * Emitted when the current icon theme is switched or GTK+ detects
366  * that a change has occurred in the contents of the current
367  * icon theme.
368  **/
369   signal_changed = g_signal_new (I_("changed"),
370                                  G_TYPE_FROM_CLASS (klass),
371                                  G_SIGNAL_RUN_LAST,
372                                  G_STRUCT_OFFSET (GtkIconThemeClass, changed),
373                                  NULL, NULL,
374                                  g_cclosure_marshal_VOID__VOID,
375                                  G_TYPE_NONE, 0);
376
377   g_type_class_add_private (klass, sizeof (GtkIconThemePrivate));
378 }
379
380
381 /* Callback when the display that the icon theme is attached to
382  * is closed; unset the screen, and if it's the unique theme
383  * for the screen, drop the reference
384  */
385 static void
386 display_closed (GdkDisplay   *display,
387                 gboolean      is_error,
388                 GtkIconTheme *icon_theme)
389 {
390   GtkIconThemePrivate *priv = icon_theme->priv;
391   GdkScreen *screen = priv->screen;
392   gboolean was_screen_singleton = priv->is_screen_singleton;
393
394   if (was_screen_singleton)
395     {
396       g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), NULL);
397       priv->is_screen_singleton = FALSE;
398     }
399
400   gtk_icon_theme_set_screen (icon_theme, NULL);
401
402   if (was_screen_singleton)
403     {
404       g_object_unref (icon_theme);
405     }
406 }
407
408 static void
409 update_current_theme (GtkIconTheme *icon_theme)
410 {
411   GtkIconThemePrivate *priv = icon_theme->priv;
412
413   if (!priv->custom_theme)
414     {
415       gchar *theme = NULL;
416       gchar *fallback_theme = NULL;
417       gboolean changed = FALSE;
418
419       if (priv->screen)
420         {
421           GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
422           g_object_get (settings, 
423                         "gtk-icon-theme-name", &theme, 
424                         "gtk-fallback-icon-theme", &fallback_theme, NULL);
425         }
426
427       if (!theme)
428         theme = g_strdup (DEFAULT_THEME_NAME);
429
430       if (strcmp (priv->current_theme, theme) != 0)
431         {
432           g_free (priv->current_theme);
433           priv->current_theme = theme;
434
435           changed = TRUE;
436         }
437       else
438         g_free (theme);
439
440       if ((priv->fallback_theme && !fallback_theme) ||
441           (!priv->fallback_theme && fallback_theme) ||
442           (priv->fallback_theme && fallback_theme &&
443            strcmp (priv->fallback_theme, fallback_theme) != 0))
444         {
445           g_free (priv->fallback_theme);
446           priv->fallback_theme = fallback_theme;
447
448           changed = TRUE;
449         }
450       else
451         g_free (fallback_theme);
452
453       if (changed)
454         do_theme_change (icon_theme);
455     }
456 }
457
458 /* Callback when the icon theme GtkSetting changes
459  */
460 static void
461 theme_changed (GtkSettings  *settings,
462                GParamSpec   *pspec,
463                GtkIconTheme *icon_theme)
464 {
465   update_current_theme (icon_theme);
466 }
467
468 static void
469 unset_screen (GtkIconTheme *icon_theme)
470 {
471   GtkIconThemePrivate *priv = icon_theme->priv;
472   GtkSettings *settings;
473   GdkDisplay *display;
474   
475   if (priv->screen)
476     {
477       settings = gtk_settings_get_for_screen (priv->screen);
478       display = gdk_screen_get_display (priv->screen);
479       
480       g_signal_handlers_disconnect_by_func (display,
481                                             (gpointer) display_closed,
482                                             icon_theme);
483       g_signal_handlers_disconnect_by_func (settings,
484                                             (gpointer) theme_changed,
485                                             icon_theme);
486
487       priv->screen = NULL;
488     }
489 }
490
491 /**
492  * gtk_icon_theme_set_screen:
493  * @icon_theme: a #GtkIconTheme
494  * @screen: a #GdkScreen
495  * 
496  * Sets the screen for an icon theme; the screen is used
497  * to track the user's currently configured icon theme,
498  * which might be different for different screens.
499  *
500  * Since: 2.4
501  **/
502 void
503 gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
504                            GdkScreen    *screen)
505 {
506   GtkIconThemePrivate *priv;
507   GtkSettings *settings;
508   GdkDisplay *display;
509
510   g_return_if_fail (GTK_ICON_THEME (icon_theme));
511   g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
512
513   priv = icon_theme->priv;
514
515   unset_screen (icon_theme);
516   
517   if (screen)
518     {
519       display = gdk_screen_get_display (screen);
520       settings = gtk_settings_get_for_screen (screen);
521       
522       priv->screen = screen;
523       
524       g_signal_connect (display, "closed",
525                         G_CALLBACK (display_closed), icon_theme);
526       g_signal_connect (settings, "notify::gtk-icon-theme-name",
527                         G_CALLBACK (theme_changed), icon_theme);
528       g_signal_connect (settings, "notify::gtk-fallback-icon-theme-name",
529                         G_CALLBACK (theme_changed), icon_theme);
530     }
531
532   update_current_theme (icon_theme);
533 }
534
535 /* Checks whether a loader for SVG files has been registered
536  * with GdkPixbuf.
537  */
538 static gboolean
539 pixbuf_supports_svg (void)
540 {
541   GSList *formats = gdk_pixbuf_get_formats ();
542   GSList *tmp_list;
543   static gboolean found_svg = FALSE;
544   static gboolean value_known = FALSE;
545
546   if (value_known)
547     return found_svg;
548   
549   for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
550     {
551       gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
552       gchar **mime_type;
553       
554       for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
555         {
556           if (strcmp (*mime_type, "image/svg") == 0)
557             found_svg = TRUE;
558         }
559
560       g_strfreev (mime_types);
561     }
562
563   g_slist_free (formats);
564   value_known = TRUE;
565   
566   return found_svg;
567 }
568
569 static void
570 gtk_icon_theme_init (GtkIconTheme *icon_theme)
571 {
572   GtkIconThemePrivate *priv;
573   const gchar * const *xdg_data_dirs;
574   int i, j;
575   
576   priv = g_type_instance_get_private ((GTypeInstance *)icon_theme,
577                                       GTK_TYPE_ICON_THEME);
578   icon_theme->priv = priv;
579
580   priv->custom_theme = FALSE;
581   priv->current_theme = g_strdup (DEFAULT_THEME_NAME);
582
583   xdg_data_dirs = g_get_system_data_dirs ();
584   for (i = 0; xdg_data_dirs[i]; i++) ;
585
586   priv->search_path_len = 2 * i + 2;
587   
588   priv->search_path = g_new (char *, priv->search_path_len);
589   
590   i = 0;
591   priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
592   priv->search_path[i++] = g_build_filename (g_get_user_data_dir (), "icons", NULL);
593   
594   for (j = 0; xdg_data_dirs[j]; j++) 
595     priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
596
597   for (j = 0; xdg_data_dirs[j]; j++) 
598     priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "pixmaps", NULL);
599
600   priv->themes_valid = FALSE;
601   priv->themes = NULL;
602   priv->unthemed_icons = NULL;
603   
604   priv->pixbuf_supports_svg = pixbuf_supports_svg ();
605 }
606
607 static void
608 free_dir_mtime (IconThemeDirMtime *dir_mtime)
609 {
610   if (dir_mtime->cache)
611     _gtk_icon_cache_unref (dir_mtime->cache);
612
613   g_free (dir_mtime->dir);
614   g_free (dir_mtime);
615
616 }
617
618 static gboolean
619 reset_styles_idle (gpointer user_data)
620 {
621   GtkIconTheme *icon_theme;
622   GtkIconThemePrivate *priv;
623
624   GDK_THREADS_ENTER ();
625
626   icon_theme = GTK_ICON_THEME (user_data);
627   priv = icon_theme->priv;
628
629   if (priv->screen && priv->is_screen_singleton)
630     {
631       GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
632       gtk_rc_reset_styles (settings);
633     }
634
635   priv->reset_styles_idle = 0;
636
637   GDK_THREADS_LEAVE ();
638
639   return FALSE;
640 }
641
642 static void
643 do_theme_change (GtkIconTheme *icon_theme)
644 {
645   GtkIconThemePrivate *priv = icon_theme->priv;
646   
647   GTK_NOTE (ICONTHEME, 
648             g_print ("change to icon theme \"%s\"\n", priv->current_theme));
649   blow_themes (icon_theme);
650   g_signal_emit (icon_theme, signal_changed, 0);
651
652   if (!priv->reset_styles_idle)
653     priv->reset_styles_idle = 
654       g_idle_add (reset_styles_idle, icon_theme);
655 }
656
657 static void
658 blow_themes (GtkIconTheme *icon_theme)
659 {
660   GtkIconThemePrivate *priv = icon_theme->priv;
661   
662   if (priv->themes_valid)
663     {
664       g_hash_table_destroy (priv->all_icons);
665       g_list_foreach (priv->themes, (GFunc)theme_destroy, NULL);
666       g_list_free (priv->themes);
667       g_list_foreach (priv->dir_mtimes, (GFunc)free_dir_mtime, NULL);
668       g_list_free (priv->dir_mtimes);
669       g_hash_table_destroy (priv->unthemed_icons);
670     }
671   priv->themes = NULL;
672   priv->unthemed_icons = NULL;
673   priv->dir_mtimes = NULL;
674   priv->all_icons = NULL;
675   priv->themes_valid = FALSE;
676 }
677
678 static void
679 gtk_icon_theme_finalize (GObject *object)
680 {
681   GtkIconTheme *icon_theme;
682   GtkIconThemePrivate *priv;
683   int i;
684
685   icon_theme = GTK_ICON_THEME (object);
686   priv = icon_theme->priv;
687
688   if (priv->reset_styles_idle)
689     {
690       g_source_remove (priv->reset_styles_idle);
691       priv->reset_styles_idle = 0;
692     }
693
694   unset_screen (icon_theme);
695
696   g_free (priv->current_theme);
697   priv->current_theme = NULL;
698
699   for (i=0; i < priv->search_path_len; i++)
700     g_free (priv->search_path[i]);
701
702   g_free (priv->search_path);
703   priv->search_path = NULL;
704
705   blow_themes (icon_theme);
706
707   G_OBJECT_CLASS (parent_class)->finalize (object);  
708 }
709
710 /**
711  * gtk_icon_theme_set_search_path:
712  * @icon_theme: a #GtkIconTheme
713  * @path: array of directories that are searched for icon themes
714  * @n_elements: number of elements in @path.
715  * 
716  * Sets the search path for the icon theme object. When looking
717  * for an icon theme, GTK+ will search for a subdirectory of
718  * one or more of the directories in @path with the same name
719  * as the icon theme. (Themes from multiple of the path elements
720  * are combined to allow themes to be extended by adding icons
721  * in the user's home directory.)
722  *
723  * In addition if an icon found isn't found either in the current
724  * icon theme or the default icon theme, and an image file with
725  * the right name is found directly in one of the elements of
726  * @path, then that image will be used for the icon name.
727  * (This is legacy feature, and new icons should be put
728  * into the default icon theme, which is called DEFAULT_THEME_NAME,
729  * rather than directly on the icon path.)
730  *
731  * Since: 2.4
732  **/
733 void
734 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
735                                 const gchar  *path[],
736                                 gint          n_elements)
737 {
738   GtkIconThemePrivate *priv;
739   gint i;
740
741   g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
742
743   priv = icon_theme->priv;
744   for (i = 0; i < priv->search_path_len; i++)
745     g_free (priv->search_path[i]);
746
747   g_free (priv->search_path);
748
749   priv->search_path = g_new (gchar *, n_elements);
750   priv->search_path_len = n_elements;
751   for (i = 0; i < priv->search_path_len; i++)
752     priv->search_path[i] = g_strdup (path[i]);
753
754   do_theme_change (icon_theme);
755 }
756
757
758 /**
759  * gtk_icon_theme_get_search_path:
760  * @icon_theme: a #GtkIconTheme
761  * @path: location to store a list of icon theme path directories or %NULL
762  *        The stored value should be freed with g_strfreev().
763  * @n_elements: location to store number of elements
764  *              in @path, or %NULL
765  * 
766  * Gets the current search path. See gtk_icon_theme_set_search_path().
767  *
768  * Since: 2.4
769  **/
770 void
771 gtk_icon_theme_get_search_path (GtkIconTheme      *icon_theme,
772                                 gchar            **path[],
773                                 gint              *n_elements)
774 {
775   GtkIconThemePrivate *priv;
776   int i;
777
778   g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
779
780   priv = icon_theme->priv;
781
782   if (n_elements)
783     *n_elements = priv->search_path_len;
784   
785   if (path)
786     {
787       *path = g_new (gchar *, priv->search_path_len + 1);
788       for (i = 0; i < priv->search_path_len; i++)
789         (*path)[i] = g_strdup (priv->search_path[i]);
790       (*path)[i] = NULL;
791     }
792 }
793
794 /**
795  * gtk_icon_theme_append_search_path:
796  * @icon_theme: a #GtkIconTheme
797  * @path: directory name to append to the icon path
798  * 
799  * Appends a directory to the search path. 
800  * See gtk_icon_theme_set_search_path(). 
801  *
802  * Since: 2.4
803  **/
804 void
805 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
806                                    const gchar  *path)
807 {
808   GtkIconThemePrivate *priv;
809
810   g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
811   g_return_if_fail (path != NULL);
812
813   priv = icon_theme->priv;
814   
815   priv->search_path_len++;
816   priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
817   priv->search_path[priv->search_path_len-1] = g_strdup (path);
818
819   do_theme_change (icon_theme);
820 }
821
822 /**
823  * gtk_icon_theme_prepend_search_path:
824  * @icon_theme: a #GtkIconTheme
825  * @path: directory name to prepend to the icon path
826  * 
827  * Prepends a directory to the search path. 
828  * See gtk_icon_theme_set_search_path().
829  *
830  * Since: 2.4
831  **/
832 void
833 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
834                                     const gchar  *path)
835 {
836   GtkIconThemePrivate *priv;
837   int i;
838
839   g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
840   g_return_if_fail (path != NULL);
841
842   priv = icon_theme->priv;
843   
844   priv->search_path_len++;
845   priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
846
847   for (i = priv->search_path_len - 1; i > 0; i--)
848     priv->search_path[i] = priv->search_path[i - 1];
849   
850   priv->search_path[0] = g_strdup (path);
851
852   do_theme_change (icon_theme);
853 }
854
855 /**
856  * gtk_icon_theme_set_custom_theme:
857  * @icon_theme: a #GtkIconTheme
858  * @theme_name: name of icon theme to use instead of configured theme,
859  *   or %NULL to unset a previously set custom theme
860  * 
861  * Sets the name of the icon theme that the #GtkIconTheme object uses
862  * overriding system configuration. This function cannot be called
863  * on the icon theme objects returned from gtk_icon_theme_get_default()
864  * and gtk_icon_theme_get_for_screen().
865  *
866  * Since: 2.4
867  **/
868 void
869 gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
870                                  const gchar  *theme_name)
871 {
872   GtkIconThemePrivate *priv;
873
874   g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
875
876   priv = icon_theme->priv;
877
878   g_return_if_fail (!priv->is_screen_singleton);
879   
880   if (theme_name != NULL)
881     {
882       priv->custom_theme = TRUE;
883       if (strcmp (theme_name, priv->current_theme) != 0)
884         {
885           g_free (priv->current_theme);
886           priv->current_theme = g_strdup (theme_name);
887
888           do_theme_change (icon_theme);
889         }
890     }
891   else
892     {
893       priv->custom_theme = FALSE;
894
895       update_current_theme (icon_theme);
896     }
897 }
898
899 static void
900 insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
901 {
902   int i;
903   GList *l;
904   char **dirs;
905   char **themes;
906   GtkIconThemePrivate *priv;
907   IconTheme *theme;
908   char *path;
909   GKeyFile *theme_file;
910   GError *error = NULL;
911   IconThemeDirMtime *dir_mtime;
912   struct stat stat_buf;
913   
914   priv = icon_theme->priv;
915
916   for (l = priv->themes; l != NULL; l = l->next)
917     {
918       theme = l->data;
919       if (strcmp (theme->name, theme_name) == 0)
920         return;
921     }
922   
923   for (i = 0; i < priv->search_path_len; i++)
924     {
925       path = g_build_filename (priv->search_path[i],
926                                theme_name,
927                                NULL);
928       dir_mtime = g_new (IconThemeDirMtime, 1);
929       dir_mtime->cache = NULL;
930       dir_mtime->dir = path;
931       if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
932         dir_mtime->mtime = stat_buf.st_mtime;
933       else
934         dir_mtime->mtime = 0;
935
936       priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
937     }
938   priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
939
940   theme_file = NULL;
941   for (i = 0; i < priv->search_path_len && !theme_file; i++)
942     {
943       path = g_build_filename (priv->search_path[i],
944                                theme_name,
945                                "index.theme",
946                                NULL);
947       if (g_file_test (path, G_FILE_TEST_IS_REGULAR)) 
948         {
949           theme_file = g_key_file_new ();
950           g_key_file_set_list_separator (theme_file, ',');
951           g_key_file_load_from_file (theme_file, path, 0, &error);
952           if (error)
953             {
954               g_key_file_free (theme_file);
955               theme_file = NULL;
956               g_error_free (error);
957               error = NULL;
958             }
959         }
960       g_free (path);
961     }
962
963   if (theme_file || strcmp (theme_name, DEFAULT_THEME_NAME) == 0)
964     {
965       theme = g_new0 (IconTheme, 1);
966       theme->name = g_strdup (theme_name);
967       priv->themes = g_list_prepend (priv->themes, theme);
968     }
969
970   if (theme_file == NULL)
971     return;
972
973   theme->display_name = 
974     g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
975   if (!theme->display_name)
976     g_warning ("Theme file for %s has no name\n", theme_name);
977
978   dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
979   if (!dirs)
980     g_warning ("Theme file for %s has no directories\n", theme_name);
981   
982   theme->comment = 
983     g_key_file_get_locale_string (theme_file, 
984                                   "Icon Theme", "Comment",
985                                   NULL, NULL);
986   theme->example = 
987     g_key_file_get_string (theme_file, 
988                            "Icon Theme", "Example",
989                            NULL);
990
991   theme->dirs = NULL;
992   for (i = 0; dirs[i] != NULL; i++)
993     theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
994   
995   g_strfreev (dirs);
996   
997   theme->dirs = g_list_reverse (theme->dirs);
998
999   themes = g_key_file_get_string_list (theme_file,
1000                                        "Icon Theme",
1001                                        "Inherits",
1002                                        NULL,
1003                                        NULL);
1004   if (themes)
1005     {
1006       for (i = 0; themes[i] != NULL; i++)
1007         insert_theme (icon_theme, themes[i]);
1008       
1009       g_strfreev (themes);
1010     }
1011
1012   g_key_file_free (theme_file);
1013 }
1014
1015 static void
1016 free_unthemed_icon (UnthemedIcon *unthemed_icon)
1017 {
1018   if (unthemed_icon->svg_filename)
1019     g_free (unthemed_icon->svg_filename);
1020   if (unthemed_icon->no_svg_filename)
1021     g_free (unthemed_icon->no_svg_filename);
1022   g_free (unthemed_icon);
1023 }
1024
1025 static void
1026 load_themes (GtkIconTheme *icon_theme)
1027 {
1028   GtkIconThemePrivate *priv;
1029   GDir *gdir;
1030   int base;
1031   char *dir, *base_name, *dot;
1032   const char *file;
1033   char *abs_file;
1034   UnthemedIcon *unthemed_icon;
1035   IconSuffix old_suffix, new_suffix;
1036   GTimeVal tv;
1037   IconThemeDirMtime *dir_mtime;
1038   struct stat stat_buf;
1039   
1040   priv = icon_theme->priv;
1041
1042   priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
1043   
1044   insert_theme (icon_theme, priv->current_theme);
1045
1046   /* Always look in the "default" icon theme, and in a fallback theme */
1047   if (priv->fallback_theme)
1048     insert_theme (icon_theme, priv->fallback_theme);
1049   insert_theme (icon_theme, DEFAULT_THEME_NAME);
1050   priv->themes = g_list_reverse (priv->themes);
1051
1052
1053   priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
1054                                                 g_free, (GDestroyNotify)free_unthemed_icon);
1055
1056   for (base = 0; base < icon_theme->priv->search_path_len; base++)
1057     {
1058       dir = icon_theme->priv->search_path[base];
1059
1060       dir_mtime = g_new (IconThemeDirMtime, 1);
1061       dir_mtime->cache = _gtk_icon_cache_new_for_path (dir);
1062       dir_mtime->dir = g_strdup (dir);
1063       if (g_stat (dir, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
1064         dir_mtime->mtime = stat_buf.st_mtime;
1065       else
1066         dir_mtime->mtime = 0;
1067
1068       priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
1069
1070       if (dir_mtime->cache != NULL)
1071         continue;
1072
1073       gdir = g_dir_open (dir, 0, NULL);
1074
1075       if (gdir == NULL)
1076         continue;
1077       
1078       while ((file = g_dir_read_name (gdir)))
1079         {
1080           new_suffix = suffix_from_name (file);
1081
1082           if (new_suffix != ICON_SUFFIX_NONE)
1083             {
1084               abs_file = g_build_filename (dir, file, NULL);
1085
1086               base_name = g_strdup (file);
1087                   
1088               dot = strrchr (base_name, '.');
1089               if (dot)
1090                 *dot = 0;
1091
1092               if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
1093                                                         base_name)))
1094                 {
1095                   if (new_suffix == ICON_SUFFIX_SVG)
1096                     {
1097                       if (unthemed_icon->svg_filename)
1098                         g_free (abs_file);
1099                       else
1100                         unthemed_icon->svg_filename = abs_file;
1101                     }
1102                   else
1103                     {
1104                       if (unthemed_icon->no_svg_filename)
1105                         {
1106                           old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
1107                           if (new_suffix > old_suffix)
1108                             {
1109                               g_free (unthemed_icon->no_svg_filename);
1110                               unthemed_icon->no_svg_filename = abs_file;                              
1111                             }
1112                           else
1113                             g_free (abs_file);
1114                         }
1115                       else
1116                         unthemed_icon->no_svg_filename = abs_file;                            
1117                     }
1118
1119                   g_free (base_name);
1120                 }
1121               else
1122                 {
1123                   unthemed_icon = g_new0 (UnthemedIcon, 1);
1124                   
1125                   if (new_suffix == ICON_SUFFIX_SVG)
1126                     unthemed_icon->svg_filename = abs_file;
1127                   else
1128                     unthemed_icon->no_svg_filename = abs_file;
1129
1130                   g_hash_table_insert (priv->unthemed_icons,
1131                                        base_name,
1132                                        unthemed_icon);
1133                   g_hash_table_insert (priv->all_icons,
1134                                        base_name, NULL);
1135                 }
1136             }
1137         }
1138       g_dir_close (gdir);
1139     }
1140
1141   priv->themes_valid = TRUE;
1142   
1143   g_get_current_time(&tv);
1144   priv->last_stat_time = tv.tv_sec;
1145 }
1146
1147 static void
1148 _gtk_icon_theme_ensure_builtin_cache (void)
1149 {
1150   static gboolean initialized = FALSE;
1151   IconThemeDir *dir;
1152   gint sizes[5] = { 16, 20, 24, 32, 48 };
1153   gint n_sizes = G_N_ELEMENTS (sizes);
1154   gint i;
1155
1156   if (!initialized)
1157     {
1158       initialized = TRUE;
1159
1160       _builtin_cache = _gtk_icon_cache_new ((gchar *)builtin_icons);
1161
1162       for (i = 0; i < n_sizes; i++)
1163         {
1164           dir = g_new (IconThemeDir, 1);
1165           dir->type = ICON_THEME_DIR_THRESHOLD;
1166           dir->context = 0;
1167           dir->size = sizes[i];
1168           dir->min_size = sizes[i];
1169           dir->max_size = sizes[i];
1170           dir->threshold = 2;
1171           dir->dir = NULL;
1172           dir->icon_data = NULL;
1173           dir->subdir = g_strdup_printf ("%d", sizes[i]);
1174           dir->cache = _gtk_icon_cache_ref (_builtin_cache);
1175
1176           builtin_dirs = g_list_append (builtin_dirs, dir);
1177         }
1178     }
1179 }
1180
1181 static void
1182 ensure_valid_themes (GtkIconTheme *icon_theme)
1183 {
1184   GtkIconThemePrivate *priv = icon_theme->priv;
1185   GTimeVal tv;
1186   gboolean was_valid = priv->themes_valid;
1187
1188   _gtk_icon_theme_ensure_builtin_cache ();
1189
1190   if (priv->themes_valid)
1191     {
1192       g_get_current_time (&tv);
1193
1194       if (ABS (tv.tv_sec - priv->last_stat_time) > 5)
1195         gtk_icon_theme_rescan_if_needed (icon_theme);
1196     }
1197   
1198   if (!priv->themes_valid)
1199     {
1200       load_themes (icon_theme);
1201       
1202       if (!priv->check_reload && was_valid && priv->screen)
1203         {         
1204           static GdkAtom atom_iconthemes = GDK_NONE;
1205           GdkEvent *event = gdk_event_new (GDK_CLIENT_EVENT);
1206           int i;
1207
1208           if (!atom_iconthemes)
1209             atom_iconthemes = gdk_atom_intern_static_string ("_GTK_LOAD_ICONTHEMES");
1210
1211           for (i = 0; i < 5; i++)
1212             event->client.data.l[i] = 0;
1213           event->client.data_format = 32;
1214           event->client.message_type = atom_iconthemes;
1215
1216           gdk_screen_broadcast_client_message (priv->screen, event);
1217         }
1218     }
1219 }
1220
1221 /**
1222  * gtk_icon_theme_lookup_icon:
1223  * @icon_theme: a #GtkIconTheme
1224  * @icon_name: the name of the icon to lookup
1225  * @size: desired icon size
1226  * @flags: flags modifying the behavior of the icon lookup
1227  * 
1228  * Looks up a named icon and returns a structure containing
1229  * information such as the filename of the icon. The icon
1230  * can then be rendered into a pixbuf using
1231  * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1232  * combines these two steps if all you need is the pixbuf.)
1233  * 
1234  * Return value: a #GtkIconInfo structure containing information
1235  * about the icon, or %NULL if the icon wasn't found. Free with
1236  * gtk_icon_info_free()
1237  *
1238  * Since: 2.4
1239  **/
1240 GtkIconInfo *
1241 gtk_icon_theme_lookup_icon (GtkIconTheme       *icon_theme,
1242                             const gchar        *icon_name,
1243                             gint                size,
1244                             GtkIconLookupFlags  flags)
1245 {
1246   GtkIconThemePrivate *priv;
1247   GList *l;
1248   GtkIconInfo *icon_info = NULL;
1249   UnthemedIcon *unthemed_icon;
1250   gboolean allow_svg;
1251   gboolean use_builtin;
1252
1253   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1254   g_return_val_if_fail (icon_name != NULL, NULL);
1255   g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1256                         (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1257
1258   priv = icon_theme->priv;
1259
1260   GTK_NOTE (ICONTHEME, 
1261             g_print ("gtk_icon_theme_lookup_icon %s\n", icon_name));
1262   if (flags & GTK_ICON_LOOKUP_NO_SVG)
1263     allow_svg = FALSE;
1264   else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
1265     allow_svg = TRUE;
1266   else
1267     allow_svg = priv->pixbuf_supports_svg;
1268
1269   use_builtin = (flags & GTK_ICON_LOOKUP_USE_BUILTIN);
1270
1271   ensure_valid_themes (icon_theme);
1272
1273   for (l = priv->themes; l; l = l->next)
1274     {
1275       IconTheme *theme = l->data;
1276       
1277       icon_info = theme_lookup_icon (theme, icon_name, size, allow_svg, use_builtin);
1278       if (icon_info)
1279         goto out;
1280     }
1281
1282   unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_name);
1283   if (unthemed_icon)
1284     {
1285       icon_info = icon_info_new ();
1286
1287       /* A SVG icon, when allowed, beats out a XPM icon, but not
1288        * a PNG icon
1289        */
1290       if (allow_svg &&
1291           unthemed_icon->svg_filename &&
1292           (!unthemed_icon->no_svg_filename ||
1293            suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
1294         icon_info->filename = g_strdup (unthemed_icon->svg_filename);
1295       else if (unthemed_icon->no_svg_filename)
1296         icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);
1297 #ifdef G_OS_WIN32
1298       icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
1299                                                    -1, NULL, NULL, NULL);
1300 #endif
1301
1302       icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1303     }
1304
1305  out:
1306   if (icon_info)
1307     icon_info->desired_size = size;
1308   else
1309     {
1310       static gboolean check_for_default_theme = TRUE;
1311       char *default_theme_path;
1312       gboolean found = FALSE;
1313       unsigned i;
1314
1315       if (check_for_default_theme)
1316         {
1317           check_for_default_theme = FALSE;
1318
1319           for (i = 0; !found && i < priv->search_path_len; i++)
1320             {
1321               default_theme_path = g_build_filename (priv->search_path[i],
1322                                                      DEFAULT_THEME_NAME,
1323                                                      "index.theme",
1324                                                      NULL);
1325               found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
1326               g_free (default_theme_path);
1327             }
1328           if (!found)
1329             {
1330               g_warning (_("Could not find the icon '%s'. The '%s' theme\n"
1331                            "was not found either, perhaps you need to install it.\n"
1332                            "You can get a copy from:\n"
1333                            "\t%s"),
1334                          icon_name, DEFAULT_THEME_NAME, "http://icon-theme.freedesktop.org/releases");
1335             }
1336         }
1337     }
1338
1339   return icon_info;
1340 }
1341
1342 /* Error quark */
1343 GQuark
1344 gtk_icon_theme_error_quark (void)
1345 {
1346   static GQuark q = 0;
1347   if (q == 0)
1348     q = g_quark_from_static_string ("gtk-icon-theme-error-quark");
1349
1350   return q;
1351 }
1352
1353 /**
1354  * gtk_icon_theme_load_icon:
1355  * @icon_theme: a #GtkIconTheme
1356  * @icon_name: the name of the icon to lookup
1357  * @size: the desired icon size. The resulting icon may not be
1358  *        exactly this size; see gtk_icon_info_load_icon().
1359  * @flags: flags modifying the behavior of the icon lookup
1360  * @error: Location to store error information on failure, or %NULL.
1361  * 
1362  * Looks up an icon in an icon theme, scales it to the given size
1363  * and renders it into a pixbuf. This is a convenience function;
1364  * if more details about the icon are needed, use
1365  * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1366  *
1367  * Note that you probably want to listen for icon theme changes and
1368  * update the icon. This is usually done by connecting to the 
1369  * GtkWidget::style-set signal. If for some reason you do not want to
1370  * update the icon when the icon theme changes, you should consider
1371  * using gdk_pixbuf_copy() to make a private copy of the pixbuf
1372  * returned by this function. Otherwise GTK+ may need to keep the old 
1373  * icon theme loaded, which would be a waste of memory.
1374  * 
1375  * Return value: the rendered icon; this may be a newly created icon
1376  *  or a new reference to an internal icon, so you must not modify
1377  *  the icon. Use g_object_unref() to release your reference to the
1378  *  icon. %NULL if the icon isn't found.
1379  *
1380  * Since: 2.4
1381  **/
1382 GdkPixbuf *
1383 gtk_icon_theme_load_icon (GtkIconTheme         *icon_theme,
1384                           const gchar          *icon_name,
1385                           gint                  size,
1386                           GtkIconLookupFlags    flags,
1387                           GError              **error)
1388 {
1389   GtkIconInfo *icon_info;
1390   GdkPixbuf *pixbuf = NULL;
1391   
1392   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1393   g_return_val_if_fail (icon_name != NULL, NULL);
1394   g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1395                         (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1396   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1397   
1398   icon_info  = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
1399                                            flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1400   if (!icon_info)
1401     {
1402       g_set_error (error, GTK_ICON_THEME_ERROR,  GTK_ICON_THEME_NOT_FOUND,
1403                    _("Icon '%s' not present in theme"), icon_name);
1404       return NULL;
1405     }
1406
1407   pixbuf = gtk_icon_info_load_icon (icon_info, error);
1408   gtk_icon_info_free (icon_info);
1409
1410   return pixbuf;
1411 }
1412
1413 /**
1414  * gtk_icon_theme_has_icon:
1415  * @icon_theme: a #GtkIconTheme
1416  * @icon_name: the name of an icon
1417  * 
1418  * Checks whether an icon theme includes an icon
1419  * for a particular name.
1420  * 
1421  * Return value: %TRUE if @icon_theme includes an
1422  *  icon for @icon_name.
1423  *
1424  * Since: 2.4
1425  **/
1426 gboolean 
1427 gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
1428                          const char   *icon_name)
1429 {
1430   GtkIconThemePrivate *priv;
1431   GList *l;
1432
1433   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1434   
1435   priv = icon_theme->priv;
1436   
1437   ensure_valid_themes (icon_theme);
1438
1439   for (l = priv->dir_mtimes; l; l = l->next)
1440     {
1441       IconThemeDirMtime *dir_mtime = l->data;
1442       GtkIconCache *cache = dir_mtime->cache;
1443       
1444       if (cache && _gtk_icon_cache_has_icon (cache, icon_name))
1445         return TRUE;
1446     }
1447
1448   if (g_hash_table_lookup_extended (priv->all_icons,
1449                                     icon_name, NULL, NULL))
1450     return TRUE;
1451
1452   if (_builtin_cache &&
1453       _gtk_icon_cache_has_icon (_builtin_cache, icon_name))
1454     return TRUE;
1455
1456   if (icon_theme_builtin_icons &&
1457       g_hash_table_lookup_extended (icon_theme_builtin_icons,
1458                                     icon_name, NULL, NULL))
1459     return TRUE;
1460
1461   return FALSE;
1462 }
1463
1464 static void
1465 add_size (gpointer  key,
1466           gpointer  value,
1467           gpointer  user_data)
1468 {
1469   gint **res_p = user_data;
1470
1471   **res_p = GPOINTER_TO_INT (key);
1472
1473   (*res_p)++;
1474 }
1475
1476 /**
1477  * gtk_icon_theme_get_icon_sizes:
1478  * @icon_theme: a #GtkIconTheme
1479  * @icon_name: the name of an icon
1480  * 
1481  * Returns an array of integers describing the sizes at which
1482  * the icon is available without scaling. A size of -1 means 
1483  * that the icon is available in a scalable format. The array 
1484  * is zero-terminated.
1485  * 
1486  * Return value: An newly allocated array describing the sizes at
1487  * which the icon is available. The array should be freed with g_free()
1488  * when it is no longer needed.
1489  *
1490  * Since: 2.6
1491  **/
1492 gint *
1493 gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
1494                                const char   *icon_name)
1495 {
1496   GList *l, *d, *icons;
1497   GHashTable *sizes;
1498   gint *result, *r;
1499   guint suffix;  
1500   GtkIconThemePrivate *priv;
1501
1502   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1503   
1504   priv = icon_theme->priv;
1505
1506   ensure_valid_themes (icon_theme);
1507
1508   sizes = g_hash_table_new (g_direct_hash, g_direct_equal);
1509
1510   for (l = priv->themes; l; l = l->next)
1511     {
1512       IconTheme *theme = l->data;
1513       for (d = theme->dirs; d; d = d->next)
1514         {
1515           IconThemeDir *dir = d->data;
1516
1517           suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);      
1518           if (suffix != ICON_SUFFIX_NONE)
1519             {
1520               if (suffix == ICON_SUFFIX_SVG)
1521                 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1522               else
1523                 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1524             }
1525         }
1526     }
1527
1528   for (d = builtin_dirs; d; d = d->next)
1529     {
1530       IconThemeDir *dir = d->data;
1531       
1532       suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);          
1533       if (suffix != ICON_SUFFIX_NONE)
1534         {
1535           if (suffix == ICON_SUFFIX_SVG)
1536             g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1537           else
1538             g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1539         }
1540     }
1541
1542   if (icon_theme_builtin_icons)
1543     {
1544       icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
1545       
1546       while (icons)
1547         {
1548           BuiltinIcon *icon = icons->data;
1549         
1550           g_hash_table_insert (sizes, GINT_TO_POINTER (icon->size), NULL);
1551           icons = icons->next;
1552         }      
1553     }
1554
1555   r = result = g_new0 (gint, g_hash_table_size (sizes) + 1);
1556
1557   g_hash_table_foreach (sizes, add_size, &r);
1558   g_hash_table_destroy (sizes);
1559   
1560   return result;
1561 }
1562
1563 static void
1564 add_key_to_hash (gpointer  key,
1565                  gpointer  value,
1566                  gpointer  user_data)
1567 {
1568   GHashTable *hash = user_data;
1569
1570   g_hash_table_insert (hash, key, NULL);
1571 }
1572
1573 static void
1574 add_key_to_list (gpointer  key,
1575                  gpointer  value,
1576                  gpointer  user_data)
1577 {
1578   GList **list = user_data;
1579
1580   *list = g_list_prepend (*list, g_strdup (key));
1581 }
1582
1583 /**
1584  * gtk_icon_theme_list_icons:
1585  * @icon_theme: a #GtkIconTheme
1586  * @context: a string identifying a particular type of icon,
1587  *           or %NULL to list all icons.
1588  * 
1589  * Lists the icons in the current icon theme. Only a subset
1590  * of the icons can be listed by providing a context string.
1591  * The set of values for the context string is system dependent,
1592  * but will typically include such values as "Applications" and
1593  * "MimeTypes".
1594  * 
1595  * Return value: a #GList list holding the names of all the
1596  *  icons in the theme. You must first free each element
1597  *  in the list with g_free(), then free the list itself
1598  *  with g_list_free().
1599  *
1600  * Since: 2.4
1601  **/
1602 GList *
1603 gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
1604                            const char   *context)
1605 {
1606   GtkIconThemePrivate *priv;
1607   GHashTable *icons;
1608   GList *list, *l;
1609   GQuark context_quark;
1610   
1611   priv = icon_theme->priv;
1612   
1613   ensure_valid_themes (icon_theme);
1614
1615   if (context)
1616     {
1617       context_quark = g_quark_try_string (context);
1618
1619       if (!context_quark)
1620         return NULL;
1621     }
1622   else
1623     context_quark = 0;
1624
1625   icons = g_hash_table_new (g_str_hash, g_str_equal);
1626   
1627   l = priv->themes;
1628   while (l != NULL)
1629     {
1630       theme_list_icons (l->data, icons, context_quark);
1631       l = l->next;
1632     }
1633
1634   if (context_quark == 0)
1635     g_hash_table_foreach (priv->unthemed_icons,
1636                           add_key_to_hash,
1637                           icons);
1638
1639   list = NULL;
1640   
1641   g_hash_table_foreach (icons,
1642                         add_key_to_list,
1643                         &list);
1644
1645   g_hash_table_destroy (icons);
1646   
1647   return list;
1648 }
1649
1650 /**
1651  * gtk_icon_theme_get_example_icon_name:
1652  * @icon_theme: a #GtkIconTheme
1653  * 
1654  * Gets the name of an icon that is representative of the
1655  * current theme (for instance, to use when presenting
1656  * a list of themes to the user.)
1657  * 
1658  * Return value: the name of an example icon or %NULL.
1659  *  Free with g_free().
1660  *
1661  * Since: 2.4
1662  **/
1663 char *
1664 gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
1665 {
1666   GtkIconThemePrivate *priv;
1667   GList *l;
1668   IconTheme *theme;
1669
1670   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1671   
1672   priv = icon_theme->priv;
1673   
1674   ensure_valid_themes (icon_theme);
1675
1676   l = priv->themes;
1677   while (l != NULL)
1678     {
1679       theme = l->data;
1680       if (theme->example)
1681         return g_strdup (theme->example);
1682       
1683       l = l->next;
1684     }
1685   
1686   return NULL;
1687 }
1688
1689 /**
1690  * gtk_icon_theme_rescan_if_needed:
1691  * @icon_theme: a #GtkIconTheme
1692  * 
1693  * Checks to see if the icon theme has changed; if it has, any
1694  * currently cached information is discarded and will be reloaded
1695  * next time @icon_theme is accessed.
1696  * 
1697  * Return value: %TRUE if the icon theme has changed and needed
1698  *   to be reloaded.
1699  *
1700  * Since: 2.4
1701  **/
1702 gboolean
1703 gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
1704 {
1705   GtkIconThemePrivate *priv;
1706   IconThemeDirMtime *dir_mtime;
1707   GList *d;
1708   int stat_res;
1709   struct stat stat_buf;
1710   GTimeVal tv;
1711
1712   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1713
1714   priv = icon_theme->priv;
1715   
1716   for (d = priv->dir_mtimes; d != NULL; d = d->next)
1717     {
1718       dir_mtime = d->data;
1719
1720       stat_res = g_stat (dir_mtime->dir, &stat_buf);
1721
1722       /* dir mtime didn't change */
1723       if (stat_res == 0 && 
1724           S_ISDIR (stat_buf.st_mode) &&
1725           dir_mtime->mtime == stat_buf.st_mtime)
1726         continue;
1727       /* didn't exist before, and still doesn't */
1728       if (dir_mtime->mtime == 0 &&
1729           (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
1730         continue;
1731           
1732       do_theme_change (icon_theme);
1733       return TRUE;
1734     }
1735   
1736   g_get_current_time (&tv);
1737   priv->last_stat_time = tv.tv_sec;
1738
1739   return FALSE;
1740 }
1741
1742 static void
1743 theme_destroy (IconTheme *theme)
1744 {
1745   g_free (theme->display_name);
1746   g_free (theme->comment);
1747   g_free (theme->name);
1748   g_free (theme->example);
1749
1750   g_list_foreach (theme->dirs, (GFunc)theme_dir_destroy, NULL);
1751   g_list_free (theme->dirs);
1752   
1753   g_free (theme);
1754 }
1755
1756 static void
1757 theme_dir_destroy (IconThemeDir *dir)
1758 {
1759   if (dir->cache)
1760       _gtk_icon_cache_unref (dir->cache);
1761   else
1762     g_hash_table_destroy (dir->icons);
1763   
1764   if (dir->icon_data)
1765     g_hash_table_destroy (dir->icon_data);
1766   g_free (dir->dir);
1767   g_free (dir->subdir);
1768   g_free (dir);
1769 }
1770
1771 static int
1772 theme_dir_size_difference (IconThemeDir *dir, int size, gboolean *smaller)
1773 {
1774   int min, max;
1775   switch (dir->type)
1776     {
1777     case ICON_THEME_DIR_FIXED:
1778       *smaller = size < dir->size;
1779       return abs (size - dir->size);
1780       break;
1781     case ICON_THEME_DIR_SCALABLE:
1782       *smaller = size < dir->min_size;
1783       if (size < dir->min_size)
1784         return dir->min_size - size;
1785       if (size > dir->max_size)
1786         return size - dir->max_size;
1787       return 0;
1788       break;
1789     case ICON_THEME_DIR_THRESHOLD:
1790       min = dir->size - dir->threshold;
1791       max = dir->size + dir->threshold;
1792       *smaller = size < min;
1793       if (size < min)
1794         return min - size;
1795       if (size > max)
1796         return size - max;
1797       return 0;
1798       break;
1799     case ICON_THEME_DIR_UNTHEMED:
1800       g_assert_not_reached ();
1801       break;
1802     }
1803   g_assert_not_reached ();
1804   return 1000;
1805 }
1806
1807 static const char *
1808 string_from_suffix (IconSuffix suffix)
1809 {
1810   switch (suffix)
1811     {
1812     case ICON_SUFFIX_XPM:
1813       return ".xpm";
1814     case ICON_SUFFIX_SVG:
1815       return ".svg";
1816     case ICON_SUFFIX_PNG:
1817       return ".png";
1818     default:
1819       g_assert_not_reached();
1820     }
1821   return NULL;
1822 }
1823
1824 static IconSuffix
1825 suffix_from_name (const char *name)
1826 {
1827   IconSuffix retval;
1828
1829   if (g_str_has_suffix (name, ".png"))
1830     retval = ICON_SUFFIX_PNG;
1831   else if (g_str_has_suffix (name, ".svg"))
1832     retval = ICON_SUFFIX_SVG;
1833   else if (g_str_has_suffix (name, ".xpm"))
1834     retval = ICON_SUFFIX_XPM;
1835   else
1836     retval = ICON_SUFFIX_NONE;
1837
1838   return retval;
1839 }
1840
1841 static IconSuffix
1842 best_suffix (IconSuffix suffix,
1843              gboolean   allow_svg)
1844 {
1845   if ((suffix & ICON_SUFFIX_PNG) != 0)
1846     return ICON_SUFFIX_PNG;
1847   else if (allow_svg && ((suffix & ICON_SUFFIX_SVG) != 0))
1848     return ICON_SUFFIX_SVG;
1849   else if ((suffix & ICON_SUFFIX_XPM) != 0)
1850     return ICON_SUFFIX_XPM;
1851   else
1852     return ICON_SUFFIX_NONE;
1853 }
1854
1855
1856 static IconSuffix
1857 theme_dir_get_icon_suffix (IconThemeDir *dir,
1858                            const gchar  *icon_name,
1859                            gboolean     *has_icon_file)
1860 {
1861   IconSuffix suffix;
1862
1863   if (dir->cache)
1864     {
1865       suffix = (IconSuffix)_gtk_icon_cache_get_icon_flags (dir->cache,
1866                                                            icon_name,
1867                                                            dir->subdir);
1868
1869       if (has_icon_file)
1870         *has_icon_file = suffix & HAS_ICON_FILE;
1871
1872       suffix = suffix & ~HAS_ICON_FILE;
1873     }
1874   else
1875     suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
1876
1877   GTK_NOTE (ICONTHEME, 
1878             g_print ("get_icon_suffix%s %d\n", dir->cache ? " (cached)" : "", suffix));
1879
1880   return suffix;
1881 }
1882
1883 static GtkIconInfo *
1884 theme_lookup_icon (IconTheme          *theme,
1885                    const char         *icon_name,
1886                    int                 size,
1887                    gboolean            allow_svg,
1888                    gboolean            use_builtin)
1889 {
1890   GList *dirs, *l;
1891   IconThemeDir *dir, *min_dir;
1892   char *file;
1893   int min_difference, difference;
1894   BuiltinIcon *closest_builtin = NULL;
1895   gboolean smaller, has_larger;
1896   IconSuffix suffix;
1897
1898   min_difference = G_MAXINT;
1899   min_dir = NULL;
1900   has_larger = FALSE;
1901
1902   /* Builtin icons are logically part of the default theme and
1903    * are searched before other subdirectories of the default theme.
1904    */
1905   if (strcmp (theme->name, DEFAULT_THEME_NAME) == 0 && use_builtin)
1906     {
1907       closest_builtin = find_builtin_icon (icon_name, 
1908                                            size,
1909                                            &min_difference,
1910                                            &has_larger);
1911
1912       if (min_difference == 0)
1913         return icon_info_new_builtin (closest_builtin);
1914
1915       dirs = builtin_dirs;
1916     }
1917   else
1918     dirs = theme->dirs;
1919
1920   l = dirs;
1921   while (l != NULL)
1922     {
1923       dir = l->data;
1924
1925       GTK_NOTE (ICONTHEME, 
1926                 g_print ("theme_lookup_icon dir %s\n", dir->dir));
1927       suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1928       if (best_suffix (suffix, allow_svg) != ICON_SUFFIX_NONE)
1929         {
1930           difference = theme_dir_size_difference (dir, size, &smaller);
1931
1932           if (difference == 0)
1933             {
1934               min_dir = dir;
1935               break;
1936             }
1937
1938           if (!has_larger)
1939             {
1940               if (difference < min_difference || smaller)
1941                 {
1942                   min_difference = difference;
1943                   min_dir = dir;
1944                   closest_builtin = NULL;
1945                   has_larger = smaller;
1946                 }
1947             }
1948           else
1949             {
1950               if (difference < min_difference && smaller)
1951                 {
1952                   min_difference = difference;
1953                   min_dir = dir;
1954                   closest_builtin = NULL;
1955                 }
1956             }
1957
1958         }
1959
1960       l = l->next;
1961
1962       if (l == NULL && dirs == builtin_dirs)
1963         {
1964           dirs = theme->dirs;
1965           l = dirs;
1966         }
1967     }
1968
1969   if (closest_builtin)
1970     return icon_info_new_builtin (closest_builtin);
1971   
1972   if (min_dir)
1973     {
1974       GtkIconInfo *icon_info = icon_info_new ();
1975       gboolean has_icon_file = FALSE;
1976       
1977       suffix = theme_dir_get_icon_suffix (min_dir, icon_name, &has_icon_file);
1978       suffix = best_suffix (suffix, allow_svg);
1979       g_assert (suffix != ICON_SUFFIX_NONE);
1980       
1981       file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
1982       icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
1983       g_free (file);
1984 #ifdef G_OS_WIN32
1985       icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
1986                                                    -1, NULL, NULL, NULL);
1987 #endif
1988       
1989       if (min_dir->icon_data != NULL)
1990         icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
1991
1992       if (icon_info->data == NULL && min_dir->cache != NULL)
1993         {
1994           icon_info->data = _gtk_icon_cache_get_icon_data (min_dir->cache, icon_name, min_dir->subdir);
1995           if (icon_info->data)
1996             {
1997               if (min_dir->icon_data == NULL)
1998                 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
1999                                                             g_free, (GDestroyNotify)icon_data_free);
2000
2001               g_hash_table_replace (min_dir->icon_data, g_strdup (icon_name), icon_info->data);
2002             }
2003         }
2004
2005       if (icon_info->data == NULL && has_icon_file)
2006         {
2007           gchar *icon_file_name, *icon_file_path;
2008
2009           icon_file_name = g_strconcat (icon_name, ".icon", NULL);
2010           icon_file_path = g_build_filename (min_dir->dir, icon_file_name, NULL);
2011
2012           if (g_file_test (icon_file_path, G_FILE_TEST_IS_REGULAR))
2013             {
2014               if (min_dir->icon_data == NULL)   
2015                 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2016                                                             g_free, (GDestroyNotify)icon_data_free);
2017               load_icon_data (min_dir, icon_file_path, icon_file_name);
2018               
2019               icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2020             }
2021           g_free (icon_file_name);
2022           g_free (icon_file_path);
2023         }
2024
2025       if (min_dir->cache)
2026         {
2027           icon_info->cache_pixbuf = _gtk_icon_cache_get_icon (min_dir->cache, icon_name,
2028                                                               min_dir->subdir);
2029         }
2030
2031       icon_info->dir_type = min_dir->type;
2032       icon_info->dir_size = min_dir->size;
2033       icon_info->threshold = min_dir->threshold;
2034       
2035       return icon_info;
2036     }
2037  
2038   return NULL;
2039 }
2040
2041 static void
2042 theme_list_icons (IconTheme  *theme, 
2043                   GHashTable *icons,
2044                   GQuark      context)
2045 {
2046   GList *l = theme->dirs;
2047   IconThemeDir *dir;
2048   
2049   while (l != NULL)
2050     {
2051       dir = l->data;
2052
2053       if (context == dir->context ||
2054           context == 0)
2055         {
2056           if (dir->cache)
2057             {
2058               _gtk_icon_cache_add_icons (dir->cache,
2059                                          dir->subdir,
2060                                          icons);
2061                                          
2062             }
2063           else
2064             {
2065               g_hash_table_foreach (dir->icons,
2066                                     add_key_to_hash,
2067                                     icons);
2068             }
2069         }
2070       l = l->next;
2071     }
2072 }
2073
2074 static void
2075 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
2076 {
2077   GKeyFile *icon_file;
2078   char *base_name;
2079   char **split;
2080   gsize length;
2081   char *dot;
2082   char *str;
2083   char *split_point;
2084   int i;
2085   gint *ivalues;
2086   GError *error = NULL;
2087   
2088   GtkIconData *data;
2089
2090   icon_file = g_key_file_new ();
2091   g_key_file_set_list_separator (icon_file, ',');
2092   g_key_file_load_from_file (icon_file, path, 0, &error);
2093   if (error)
2094     {
2095       g_error_free (error);
2096       return;
2097     }
2098   else
2099     {
2100       base_name = g_strdup (name);
2101       dot = strrchr (base_name, '.');
2102       *dot = 0;
2103       
2104       data = g_new0 (GtkIconData, 1);
2105       g_hash_table_replace (dir->icon_data, base_name, data);
2106       
2107       ivalues = g_key_file_get_integer_list (icon_file, 
2108                                              "Icon Data", "EmbeddedTextRectangle",
2109                                               &length, NULL);
2110       if (ivalues)
2111         {
2112           if (length == 4)
2113             {
2114               data->has_embedded_rect = TRUE;
2115               data->x0 = ivalues[0];
2116               data->y0 = ivalues[1];
2117               data->x1 = ivalues[2];
2118               data->y1 = ivalues[3];
2119             }
2120           
2121           g_free (ivalues);
2122         }
2123       
2124       str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
2125       if (str)
2126         {
2127           split = g_strsplit (str, "|", -1);
2128           
2129           data->n_attach_points = g_strv_length (split);
2130           data->attach_points = g_malloc (sizeof (GdkPoint) * data->n_attach_points);
2131
2132           i = 0;
2133           while (split[i] != NULL && i < data->n_attach_points)
2134             {
2135               split_point = strchr (split[i], ',');
2136               if (split_point)
2137                 {
2138                   *split_point = 0;
2139                   split_point++;
2140                   data->attach_points[i].x = atoi (split[i]);
2141                   data->attach_points[i].y = atoi (split_point);
2142                 }
2143               i++;
2144             }
2145           
2146           g_strfreev (split);
2147           g_free (str);
2148         }
2149       
2150       data->display_name = g_key_file_get_locale_string (icon_file, 
2151                                                          "Icon Data", "DisplayName",
2152                                                          NULL, NULL);
2153       g_key_file_free (icon_file);
2154     }
2155 }
2156
2157 static void
2158 scan_directory (GtkIconThemePrivate *icon_theme,
2159                 IconThemeDir *dir, char *full_dir)
2160 {
2161   GDir *gdir;
2162   const char *name;
2163   char *base_name, *dot;
2164   char *path;
2165   IconSuffix suffix, hash_suffix;
2166
2167   GTK_NOTE (ICONTHEME, 
2168             g_print ("scanning directory %s\n", full_dir));
2169   dir->icons = g_hash_table_new_full (g_str_hash, g_str_equal,
2170                                       g_free, NULL);
2171   
2172   gdir = g_dir_open (full_dir, 0, NULL);
2173
2174   if (gdir == NULL)
2175     return;
2176
2177   while ((name = g_dir_read_name (gdir)))
2178     {
2179       if (g_str_has_suffix (name, ".icon"))
2180         {
2181           if (dir->icon_data == NULL)
2182             dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2183                                                     g_free, (GDestroyNotify)icon_data_free);
2184           
2185           path = g_build_filename (full_dir, name, NULL);
2186           if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
2187             load_icon_data (dir, path, name);
2188           
2189           g_free (path);
2190           
2191           continue;
2192         }
2193
2194       suffix = suffix_from_name (name);
2195       if (suffix == ICON_SUFFIX_NONE)
2196         continue;
2197       
2198       base_name = g_strdup (name);
2199       dot = strrchr (base_name, '.');
2200       *dot = 0;
2201       
2202       hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
2203       g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
2204       g_hash_table_insert (icon_theme->all_icons, base_name, NULL);
2205     }
2206   
2207   g_dir_close (gdir);
2208 }
2209
2210 static void
2211 theme_subdir_load (GtkIconTheme *icon_theme,
2212                    IconTheme    *theme,
2213                    GKeyFile     *theme_file,
2214                    char         *subdir)
2215 {
2216   GList *d;
2217   char *type_string;
2218   IconThemeDir *dir;
2219   IconThemeDirType type;
2220   char *context_string;
2221   GQuark context;
2222   int size;
2223   int min_size;
2224   int max_size;
2225   int threshold;
2226   char *full_dir;
2227   GError *error = NULL;
2228   IconThemeDirMtime *dir_mtime;
2229
2230   size = g_key_file_get_integer (theme_file, subdir, "Size", &error);
2231   if (error)
2232     {
2233       g_error_free (error);
2234       g_warning ("Theme directory %s of theme %s has no size field\n", 
2235                  subdir, theme->name);
2236       return;
2237     }
2238   
2239   type = ICON_THEME_DIR_THRESHOLD;
2240   type_string = g_key_file_get_string (theme_file, subdir, "Type", NULL);
2241   if (type_string)
2242     {
2243       if (strcmp (type_string, "Fixed") == 0)
2244         type = ICON_THEME_DIR_FIXED;
2245       else if (strcmp (type_string, "Scalable") == 0)
2246         type = ICON_THEME_DIR_SCALABLE;
2247       else if (strcmp (type_string, "Threshold") == 0)
2248         type = ICON_THEME_DIR_THRESHOLD;
2249
2250       g_free (type_string);
2251     }
2252   
2253   context = 0;
2254   context_string = g_key_file_get_string (theme_file, subdir, "Context", NULL);
2255   if (context_string)
2256     {
2257       context = g_quark_from_string (context_string);
2258       g_free (context_string);
2259     }
2260
2261   max_size = g_key_file_get_integer (theme_file, subdir, "MaxSize", &error);
2262   if (error)
2263     {
2264       max_size = size;
2265
2266       g_error_free (error);
2267       error = NULL;
2268     }
2269
2270   min_size = g_key_file_get_integer (theme_file, subdir, "MinSize", &error);
2271   if (error)
2272     {
2273       min_size = size;
2274
2275       g_error_free (error);
2276       error = NULL;
2277     }
2278   
2279   threshold = g_key_file_get_integer (theme_file, subdir, "Threshold", &error);
2280   if (error)
2281     {
2282       threshold = 2;
2283
2284       g_error_free (error);
2285       error = NULL;
2286     }
2287
2288   for (d = icon_theme->priv->dir_mtimes; d; d = d->next)
2289     {
2290       dir_mtime = (IconThemeDirMtime *)d->data;
2291
2292       if (dir_mtime->mtime == 0)
2293         continue; /* directory doesn't exist */
2294
2295        full_dir = g_build_filename (dir_mtime->dir, subdir, NULL);
2296
2297       /* First, see if we have a cache for the directory */
2298       if (dir_mtime->cache != NULL || g_file_test (full_dir, G_FILE_TEST_IS_DIR))
2299         {
2300           if (dir_mtime->cache == NULL)
2301             {
2302               /* This will return NULL if the cache doesn't exist or is outdated */
2303               dir_mtime->cache = _gtk_icon_cache_new_for_path (dir_mtime->dir);
2304             }
2305           
2306           dir = g_new (IconThemeDir, 1);
2307           dir->type = type;
2308           dir->context = context;
2309           dir->size = size;
2310           dir->min_size = min_size;
2311           dir->max_size = max_size;
2312           dir->threshold = threshold;
2313           dir->dir = full_dir;
2314           dir->icon_data = NULL;
2315           dir->subdir = g_strdup (subdir);
2316           if (dir_mtime->cache != NULL)
2317             dir->cache = _gtk_icon_cache_ref (dir_mtime->cache);
2318           else
2319             {
2320               dir->cache = NULL;
2321               scan_directory (icon_theme->priv, dir, full_dir);
2322             }
2323
2324           theme->dirs = g_list_prepend (theme->dirs, dir);
2325         }
2326       else
2327         g_free (full_dir);
2328     }
2329 }
2330
2331 static void
2332 icon_data_free (GtkIconData *icon_data)
2333 {
2334   g_free (icon_data->attach_points);
2335   g_free (icon_data->display_name);
2336   g_free (icon_data);
2337 }
2338
2339 /*
2340  * GtkIconInfo
2341  */
2342 GType
2343 gtk_icon_info_get_type (void)
2344 {
2345   static GType our_type = 0;
2346   
2347   if (our_type == 0)
2348     our_type = g_boxed_type_register_static (I_("GtkIconInfo"),
2349                                              (GBoxedCopyFunc) gtk_icon_info_copy,
2350                                              (GBoxedFreeFunc) gtk_icon_info_free);
2351
2352   return our_type;
2353 }
2354
2355 static GtkIconInfo *
2356 icon_info_new (void)
2357 {
2358   GtkIconInfo *icon_info = g_new0 (GtkIconInfo, 1);
2359
2360   icon_info->scale = -1.;
2361
2362   return icon_info;
2363 }
2364
2365 static GtkIconInfo *
2366 icon_info_new_builtin (BuiltinIcon *icon)
2367 {
2368   GtkIconInfo *icon_info = icon_info_new ();
2369
2370   icon_info->cache_pixbuf = g_object_ref (icon->pixbuf);
2371   icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
2372   icon_info->dir_size = icon->size;
2373   icon_info->threshold = 2;
2374   
2375   return icon_info;
2376 }
2377
2378 /**
2379  * gtk_icon_info_copy:
2380  * @icon_info: a #GtkIconInfo
2381  * 
2382  * Make a copy of a #GtkIconInfo.
2383  * 
2384  * Return value: the new GtkIconInfo
2385  *
2386  * Since: 2.4
2387  **/
2388 GtkIconInfo *
2389 gtk_icon_info_copy (GtkIconInfo *icon_info)
2390 {
2391   GtkIconInfo *copy;
2392   
2393   g_return_val_if_fail (icon_info != NULL, NULL);
2394
2395   copy = g_memdup (icon_info, sizeof (GtkIconInfo));
2396   if (copy->cache_pixbuf)
2397     g_object_ref (copy->cache_pixbuf);
2398   if (copy->pixbuf)
2399     g_object_ref (copy->pixbuf);
2400   if (copy->load_error)
2401     copy->load_error = g_error_copy (copy->load_error);
2402   if (copy->filename)
2403     copy->filename = g_strdup (copy->filename);
2404 #ifdef G_OS_WIN32
2405   if (copy->cp_filename)
2406     copy->cp_filename = g_strdup (copy->cp_filename);
2407 #endif
2408
2409   return copy;
2410 }
2411
2412 /**
2413  * gtk_icon_info_free:
2414  * @icon_info: a #GtkIconInfo
2415  * 
2416  * Free a #GtkIconInfo and associated information
2417  *
2418  * Since: 2.4
2419  **/
2420 void
2421 gtk_icon_info_free (GtkIconInfo *icon_info)
2422 {
2423   g_return_if_fail (icon_info != NULL);
2424
2425   if (icon_info->filename)
2426     g_free (icon_info->filename);
2427 #ifdef G_OS_WIN32
2428   if (icon_info->cp_filename)
2429     g_free (icon_info->cp_filename);
2430 #endif
2431   if (icon_info->pixbuf)
2432     g_object_unref (icon_info->pixbuf);
2433   if (icon_info->cache_pixbuf)
2434     g_object_unref (icon_info->cache_pixbuf);
2435
2436   g_free (icon_info);
2437 }
2438
2439 /**
2440  * gtk_icon_info_get_base_size:
2441  * @icon_info: a #GtkIconInfo
2442  * 
2443  * Gets the base size for the icon. The base size
2444  * is a size for the icon that was specified by
2445  * the icon theme creator. This may be different
2446  * than the actual size of image; an example of
2447  * this is small emblem icons that can be attached
2448  * to a larger icon. These icons will be given
2449  * the same base size as the larger icons to which
2450  * they are attached.
2451  * 
2452  * Return value: the base size, or 0, if no base
2453  *  size is known for the icon.
2454  *
2455  * Since: 2.4
2456  **/
2457 gint
2458 gtk_icon_info_get_base_size (GtkIconInfo *icon_info)
2459 {
2460   g_return_val_if_fail (icon_info != NULL, 0);
2461
2462   return icon_info->dir_size;
2463 }
2464
2465 /**
2466  * gtk_icon_info_get_filename:
2467  * @icon_info: a #GtkIconInfo
2468  * 
2469  * Gets the filename for the icon. If the
2470  * %GTK_ICON_LOOKUP_USE_BUILTIN flag was passed
2471  * to gtk_icon_theme_lookup_icon(), there may be
2472  * no filename if a builtin icon is returned; in this
2473  * case, you should use gtk_icon_info_get_builtin_pixbuf().
2474  * 
2475  * Return value: the filename for the icon, or %NULL
2476  *  if gtk_icon_info_get_builtin_pixbuf() should
2477  *  be used instead. The return value is owned by
2478  *  GTK+ and should not be modified or freed.
2479  *
2480  * Since: 2.4
2481  **/
2482 G_CONST_RETURN gchar *
2483 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2484 {
2485   g_return_val_if_fail (icon_info != NULL, NULL);
2486
2487   return icon_info->filename;
2488 }
2489
2490 /**
2491  * gtk_icon_info_get_builtin_pixbuf:
2492  * @icon_info: a #GtkIconInfo structure
2493  * 
2494  * Gets the built-in image for this icon, if any. To allow
2495  * GTK+ to use built in icon images, you must pass the
2496  * %GTK_ICON_LOOKUP_USE_BUILTIN to
2497  * gtk_icon_theme_lookup_icon().
2498  * 
2499  * Return value: the built-in image pixbuf, or %NULL. No
2500  *  extra reference is added to the returned pixbuf, so if
2501  *  you want to keep it around, you must use g_object_ref().
2502  *  The returned image must not be modified.
2503  *
2504  * Since: 2.4
2505  **/
2506 GdkPixbuf *
2507 gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
2508 {
2509   g_return_val_if_fail (icon_info != NULL, NULL);
2510
2511   if (icon_info->filename)
2512     return NULL;
2513   
2514   return icon_info->cache_pixbuf;
2515 }
2516
2517 static GdkPixbuf *
2518 load_svg_at_size (const gchar *filename,
2519                   gint         size,
2520                   GError      **error)
2521 {
2522   GdkPixbuf *pixbuf = NULL;
2523   GdkPixbufLoader *loader = NULL;
2524   gchar *contents = NULL;
2525   gsize length;
2526   
2527   if (!g_file_get_contents (filename,
2528                             &contents, &length, error))
2529     goto bail;
2530   
2531   loader = gdk_pixbuf_loader_new_with_type ("svg", error);
2532   if (loader == NULL)
2533     goto bail;
2534
2535   gdk_pixbuf_loader_set_size (loader, size, size);
2536   
2537   if (!gdk_pixbuf_loader_write (loader, contents, length, error))
2538     {
2539       gdk_pixbuf_loader_close (loader, NULL);
2540       goto bail;
2541     }
2542   
2543   if (!gdk_pixbuf_loader_close (loader, error))
2544     goto bail;
2545   
2546   pixbuf = g_object_ref (gdk_pixbuf_loader_get_pixbuf (loader));
2547   
2548  bail:
2549   if (loader)
2550     g_object_unref (loader);
2551   if (contents)
2552     g_free (contents);
2553   
2554   return pixbuf;
2555 }
2556
2557 /* This function contains the complicatd logic for deciding
2558  * on the size at which to load the icon and loading it at
2559  * that size.
2560  */
2561 static gboolean
2562 icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
2563                                    gboolean     scale_only)
2564 {
2565   int image_width, image_height;
2566   GdkPixbuf *source_pixbuf;
2567
2568   /* First check if we already succeeded have the necessary
2569    * information (or failed earlier)
2570    */
2571   if (scale_only && icon_info->scale >= 0)
2572     return TRUE;
2573
2574   if (icon_info->pixbuf)
2575     return TRUE;
2576
2577   if (icon_info->load_error)
2578     return FALSE;
2579
2580   /* SVG icons are a special case - we just immediately scale them
2581    * to the desired size
2582    */
2583   if (icon_info->filename && g_str_has_suffix (icon_info->filename, ".svg"))
2584     {
2585       icon_info->scale = icon_info->desired_size / 1000.;
2586
2587       if (scale_only)
2588         return TRUE;
2589       
2590       icon_info->pixbuf = load_svg_at_size (icon_info->filename,
2591                                             icon_info->desired_size,
2592                                             &icon_info->load_error);
2593
2594       return icon_info->pixbuf != NULL;
2595     }
2596
2597   /* In many cases, the scale can be determined without actual access
2598    * to the icon file. This is generally true when we have a size
2599    * for the directory where the icon is; the image size doesn't
2600    * matter in that case.
2601    */
2602   if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
2603     icon_info->scale = 1.0;
2604   else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
2605     {
2606       if (icon_info->desired_size >= icon_info->dir_size - icon_info->threshold &&
2607           icon_info->desired_size <= icon_info->dir_size + icon_info->threshold)
2608         icon_info->scale = 1.0;
2609       else if (icon_info->dir_size > 0)
2610         icon_info->scale =(gdouble) icon_info->desired_size / icon_info->dir_size;
2611     }
2612   else if (icon_info->dir_type == ICON_THEME_DIR_SCALABLE)
2613     {
2614       if (icon_info->dir_size > 0)
2615         icon_info->scale = (gdouble) icon_info->desired_size / icon_info->dir_size;
2616     }
2617
2618   if (icon_info->scale >= 0. && scale_only)
2619     return TRUE;
2620
2621   /* At this point, we need to actually get the icon; either from the
2622    * builting image or by loading the file
2623    */
2624   if (icon_info->cache_pixbuf)
2625     source_pixbuf = g_object_ref (icon_info->cache_pixbuf);
2626   else
2627     {
2628
2629       source_pixbuf = gdk_pixbuf_new_from_file (icon_info->filename,
2630                                                 &icon_info->load_error);
2631       if (!source_pixbuf)
2632         return FALSE;
2633     }
2634
2635   /* Do scale calculations that depend on the image size
2636    */
2637   image_width = gdk_pixbuf_get_width (source_pixbuf);
2638   image_height = gdk_pixbuf_get_height (source_pixbuf);
2639
2640   if (icon_info->scale < 0.0)
2641     {
2642       gint image_size = MAX (image_width, image_height);
2643       if (image_size > 0)
2644         icon_info->scale = icon_info->desired_size / (gdouble)image_size;
2645       else
2646         icon_info->scale = 1.0;
2647       
2648       if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED)
2649         icon_info->scale = MIN (icon_info->scale, 1.0);
2650     }
2651
2652   /* We don't short-circuit out here for scale_only, since, now
2653    * we've loaded the icon, we might as well go ahead and finish
2654    * the job. This is a bit of a waste when we scale here
2655    * and never get the final pixbuf; at the cost of a bit of
2656    * extra complexity, we could keep the source pixbuf around
2657    * but not actually scale it until neede.
2658    */
2659     
2660   if (icon_info->scale == 1.0)
2661     icon_info->pixbuf = source_pixbuf;
2662   else
2663     {
2664       icon_info->pixbuf = gdk_pixbuf_scale_simple (source_pixbuf,
2665                                                    0.5 + image_width * icon_info->scale,
2666                                                    0.5 + image_height * icon_info->scale,
2667                                                    GDK_INTERP_BILINEAR);
2668       g_object_unref (source_pixbuf);
2669     }
2670
2671   return TRUE;
2672 }
2673
2674 /**
2675  * gtk_icon_info_load_icon:
2676  * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
2677  * @error: location to store error information on failure, or %NULL.
2678  * 
2679  * Renders an icon previously looked up in an icon theme using
2680  * gtk_icon_theme_lookup_icon(); the size will be based on the size
2681  * passed to gtk_icon_theme_lookup_icon(). Note that the resulting
2682  * pixbuf may not be exactly this size; an icon theme may have icons
2683  * that differ slightly from their nominal sizes, and in addition GTK+
2684  * will avoid scaling icons that it considers sufficiently close to the
2685  * requested size or for which the source image would have to be scaled
2686  * up too far. (This maintains sharpness.) 
2687  * 
2688  * Return value: the rendered icon; this may be a newly created icon
2689  *  or a new reference to an internal icon, so you must not modify
2690  *  the icon. Use g_object_unref() to release your reference to the
2691  *  icon.
2692  *
2693  * Since: 2.4
2694  **/
2695 GdkPixbuf *
2696 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
2697                          GError     **error)
2698 {
2699   g_return_val_if_fail (icon_info != NULL, NULL);
2700
2701   g_return_val_if_fail (icon_info != NULL, NULL);
2702   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2703
2704   icon_info_ensure_scale_and_pixbuf (icon_info, FALSE);
2705
2706   if (icon_info->load_error)
2707     {
2708       g_propagate_error (error, icon_info->load_error);
2709       return NULL;
2710     }
2711
2712   return g_object_ref (icon_info->pixbuf);
2713 }
2714
2715 /**
2716  * gtk_icon_info_set_raw_coordinates:
2717  * @icon_info: a #GtkIconInfo
2718  * @raw_coordinates: whether the coordinates of embedded rectangles
2719  *   and attached points should be returned in their original
2720  *   (unscaled) form.
2721  * 
2722  * Sets whether the coordinates returned by gtk_icon_info_get_embedded_rect()
2723  * and gtk_icon_info_get_attach_points() should be returned in their
2724  * original form as specified in the icon theme, instead of scaled
2725  * appropriately for the pixbuf returned by gtk_icon_info_load_icon().
2726  *
2727  * Raw coordinates are somewhat strange; they are specified to be with
2728  * respect to the unscaled pixmap for PNG and XPM icons, but for SVG
2729  * icons, they are in a 1000x1000 coordinate space that is scaled
2730  * to the final size of the icon.  You can determine if the icon is an SVG
2731  * icon by using gtk_icon_info_get_filename(), and seeing if it is non-%NULL
2732  * and ends in '.svg'.
2733  *
2734  * This function is provided primarily to allow compatibility wrappers
2735  * for older API's, and is not expected to be useful for applications.
2736  *
2737  * Since: 2.4
2738  **/
2739 void
2740 gtk_icon_info_set_raw_coordinates (GtkIconInfo *icon_info,
2741                                    gboolean     raw_coordinates)
2742 {
2743   g_return_if_fail (icon_info != NULL);
2744   
2745   icon_info->raw_coordinates = raw_coordinates != FALSE;
2746 }
2747
2748 /* Scale coordinates from the icon data prior to returning
2749  * them to the user.
2750  */
2751 static gboolean
2752 icon_info_scale_point (GtkIconInfo  *icon_info,
2753                        gint          x,
2754                        gint          y,
2755                        gint         *x_out,
2756                        gint         *y_out)
2757 {
2758   if (icon_info->raw_coordinates)
2759     {
2760       *x_out = x;
2761       *y_out = y;
2762     }
2763   else
2764     {
2765       if (!icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2766         return FALSE;
2767
2768       *x_out = 0.5 + x * icon_info->scale;
2769       *y_out = 0.5 + y * icon_info->scale;
2770     }
2771
2772   return TRUE;
2773 }
2774
2775 /**
2776  * gtk_icon_info_get_embedded_rect:
2777  * @icon_info: a #GtkIconInfo
2778  * @rectangle: #GdkRectangle in which to store embedded
2779  *   rectangle coordinates; coordinates are only stored
2780  *   when this function returns %TRUE.
2781  *
2782  * Gets the coordinates of a rectangle within the icon
2783  * that can be used for display of information such
2784  * as a preview of the contents of a text file.
2785  * See gtk_icon_info_set_raw_coordinates() for further
2786  * information about the coordinate system.
2787  * 
2788  * Return value: %TRUE if the icon has an embedded rectangle
2789  *
2790  * Since: 2.4
2791  **/
2792 gboolean
2793 gtk_icon_info_get_embedded_rect (GtkIconInfo  *icon_info,
2794                                  GdkRectangle *rectangle)
2795 {
2796   g_return_val_if_fail (icon_info != NULL, FALSE);
2797
2798   if (icon_info->data && icon_info->data->has_embedded_rect &&
2799       icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2800     {
2801       gint scaled_x0, scaled_y0;
2802       gint scaled_x1, scaled_y1;
2803       
2804       if (rectangle)
2805         {
2806           icon_info_scale_point (icon_info,
2807                                  icon_info->data->x0, icon_info->data->y0,
2808                                  &scaled_x0, &scaled_y0);
2809           icon_info_scale_point (icon_info,
2810                                  icon_info->data->x1, icon_info->data->y1,
2811                                  &scaled_x1, &scaled_y1);
2812           
2813           rectangle->x = scaled_x0;
2814           rectangle->y = scaled_y0;
2815           rectangle->width = scaled_x1 - rectangle->x;
2816           rectangle->height = scaled_y1 - rectangle->y;
2817         }
2818
2819       return TRUE;
2820     }
2821   else
2822     return FALSE;
2823 }
2824
2825 /**
2826  * gtk_icon_info_get_attach_points:
2827  * @icon_info: a #GtkIconInfo
2828  * @points: location to store pointer to an array of points, or %NULL
2829  *          free the array of points with g_free().
2830  * @n_points: location to store the number of points in @points, or %NULL
2831  * 
2832  * Fetches the set of attach points for an icon. An attach point
2833  * is a location in the icon that can be used as anchor points for attaching
2834  * emblems or overlays to the icon.
2835  * 
2836  * Return value: %TRUE if there are any attach points for the icon.
2837  *
2838  * Since: 2.4
2839  **/
2840 gboolean
2841 gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
2842                                  GdkPoint   **points,
2843                                  gint        *n_points)
2844 {
2845   g_return_val_if_fail (icon_info != NULL, FALSE);
2846   
2847   if (icon_info->data && icon_info->data->n_attach_points &&
2848       icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2849     {
2850       if (points)
2851         {
2852           gint i;
2853           
2854           *points = g_new (GdkPoint, icon_info->data->n_attach_points);
2855           for (i = 0; i < icon_info->data->n_attach_points; i++)
2856             icon_info_scale_point (icon_info,
2857                                    icon_info->data->attach_points[i].x,
2858                                    icon_info->data->attach_points[i].y,
2859                                    &(*points)[i].x,
2860                                    &(*points)[i].y);
2861         }
2862           
2863       if (n_points)
2864         *n_points = icon_info->data->n_attach_points;
2865
2866       return TRUE;
2867     }
2868   else
2869     {
2870       if (points)
2871         *points = NULL;
2872       if (n_points)
2873         *n_points = 0;
2874       
2875       return FALSE;
2876     }
2877 }
2878
2879 /**
2880  * gtk_icon_info_get_display_name:
2881  * @icon_info: a #GtkIconInfo
2882  * 
2883  * Gets the display name for an icon. A display name is a
2884  * string to be used in place of the icon name in a user
2885  * visible context like a list of icons.
2886  * 
2887  * Return value: the display name for the icon or %NULL, if
2888  *  the icon doesn't have a specified display name. This value
2889  *  is owned @icon_info and must not be modified or free.
2890  *
2891  * Since: 2.4
2892  **/
2893 G_CONST_RETURN gchar *
2894 gtk_icon_info_get_display_name  (GtkIconInfo *icon_info)
2895 {
2896   g_return_val_if_fail (icon_info != NULL, NULL);
2897
2898   if (icon_info->data)
2899     return icon_info->data->display_name;
2900   else
2901     return NULL;
2902 }
2903
2904 /*
2905  * Builtin icons
2906  */
2907
2908
2909 /**
2910  * gtk_icon_theme_add_builtin_icon:
2911  * @icon_name: the name of the icon to register
2912  * @size: the size at which to register the icon (different
2913  *        images can be registered for the same icon name
2914  *        at different sizes.)
2915  * @pixbuf: #GdkPixbuf that contains the image to use
2916  *          for @icon_name.
2917  * 
2918  * Registers a built-in icon for icon theme lookups. The idea
2919  * of built-in icons is to allow an application or library
2920  * that uses themed icons to function requiring files to
2921  * be present in the file system. For instance, the default
2922  * images for all of GTK+'s stock icons are registered
2923  * as built-icons.
2924  *
2925  * In general, if you use gtk_icon_theme_add_builtin_icon()
2926  * you should also install the icon in the icon theme, so
2927  * that the icon is generally available.
2928  *
2929  * This function will generally be used with pixbufs loaded
2930  * via gdk_pixbuf_new_from_inline().
2931  *
2932  * Since: 2.4
2933  **/
2934 void
2935 gtk_icon_theme_add_builtin_icon (const gchar *icon_name,
2936                                  gint         size,
2937                                  GdkPixbuf   *pixbuf)
2938 {
2939   BuiltinIcon *default_icon;
2940   GSList *icons;
2941   gpointer key;
2942
2943   g_return_if_fail (icon_name != NULL);
2944   g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2945   
2946   if (!icon_theme_builtin_icons)
2947     icon_theme_builtin_icons = g_hash_table_new (g_str_hash, g_str_equal);
2948
2949   icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
2950   if (!icons)
2951     key = g_strdup (icon_name);
2952   else
2953     key = (gpointer)icon_name;  /* Won't get stored */
2954
2955   default_icon = g_new (BuiltinIcon, 1);
2956   default_icon->size = size;
2957   default_icon->pixbuf = g_object_ref (pixbuf);
2958   icons = g_slist_prepend (icons, default_icon);
2959
2960   /* Replaces value, leaves key untouched
2961    */
2962   g_hash_table_insert (icon_theme_builtin_icons, key, icons);
2963 }
2964
2965 /* Look up a builtin icon; the min_difference_p and
2966  * has_larger_p out parameters allow us to combine
2967  * this lookup with searching through the actual directories
2968  * of the "hicolor" icon theme. See theme_lookup_icon()
2969  * for how they are used.
2970  */
2971 static BuiltinIcon *
2972 find_builtin_icon (const gchar *icon_name,
2973                    gint         size,
2974                    gint        *min_difference_p,
2975                    gboolean    *has_larger_p)
2976 {
2977   GSList *icons = NULL;
2978   gint min_difference = G_MAXINT;
2979   gboolean has_larger = FALSE;
2980   BuiltinIcon *min_icon = NULL;
2981   
2982   if (!icon_theme_builtin_icons)
2983     return NULL;
2984
2985   icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
2986
2987   while (icons)
2988     {
2989       BuiltinIcon *default_icon = icons->data;
2990       int min, max, difference;
2991       gboolean smaller;
2992       
2993       min = default_icon->size - 2;
2994       max = default_icon->size + 2;
2995       smaller = size < min;
2996       if (size < min)
2997         difference = min - size;
2998       else if (size > max)
2999         difference = size - max;
3000       else
3001         difference = 0;
3002       
3003       if (difference == 0)
3004         {
3005           min_icon = default_icon;
3006           break;
3007         }
3008       
3009       if (!has_larger)
3010         {
3011           if (difference < min_difference || smaller)
3012             {
3013               min_difference = difference;
3014               min_icon = default_icon;
3015               has_larger = smaller;
3016             }
3017         }
3018       else
3019         {
3020           if (difference < min_difference && smaller)
3021             {
3022               min_difference = difference;
3023               min_icon = default_icon;
3024             }
3025         }
3026       
3027       icons = icons->next;
3028     }
3029
3030   if (min_difference_p)
3031     *min_difference_p = min_difference;
3032   if (has_larger_p)
3033     *has_larger_p = has_larger;
3034
3035   return min_icon;
3036 }
3037
3038 void
3039 _gtk_icon_theme_check_reload (GdkDisplay *display)
3040 {
3041   gint n_screens, i;
3042   GdkScreen *screen;
3043   GtkIconTheme *icon_theme;
3044
3045   n_screens = gdk_display_get_n_screens (display);
3046   
3047   for (i = 0; i < n_screens; i++)
3048     {
3049       screen = gdk_display_get_screen (display, i);
3050
3051       icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
3052       if (icon_theme)
3053         {
3054           icon_theme->priv->check_reload = TRUE;
3055           ensure_valid_themes (icon_theme);
3056           icon_theme->priv->check_reload = FALSE;
3057         }
3058     }
3059 }
3060
3061 #ifdef G_OS_WIN32
3062
3063 /* DLL ABI stability backward compatibility versions */
3064
3065 #undef gtk_icon_theme_set_search_path
3066
3067 void
3068 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
3069                                 const gchar  *path[],
3070                                 gint          n_elements)
3071 {
3072   const gchar **utf8_path;
3073   gint i;
3074
3075   utf8_path = g_new (const gchar *, n_elements);
3076
3077   for (i = 0; i < n_elements; i++)
3078     utf8_path[i] = g_locale_to_utf8 (path[i], -1, NULL, NULL, NULL);
3079
3080   gtk_icon_theme_set_search_path_utf8 (icon_theme, utf8_path, n_elements);
3081
3082   for (i = 0; i < n_elements; i++)
3083     g_free ((gchar *) utf8_path[i]);
3084
3085   g_free (utf8_path);
3086 }
3087
3088 #undef gtk_icon_theme_get_search_path
3089
3090 void
3091 gtk_icon_theme_get_search_path (GtkIconTheme      *icon_theme,
3092                                 gchar            **path[],
3093                                 gint              *n_elements)
3094 {
3095   gint i, n;
3096
3097   gtk_icon_theme_get_search_path_utf8 (icon_theme, path, &n);
3098
3099   if (n_elements)
3100     *n_elements = n;
3101
3102   if (path)
3103     {
3104       for (i = 0; i < n; i++)
3105         {
3106           gchar *tem = (*path)[i];
3107
3108           (*path)[i] = g_locale_from_utf8 ((*path)[i], -1, NULL, NULL, NULL);
3109           g_free (tem);
3110         }
3111     }
3112 }
3113
3114 #undef gtk_icon_theme_append_search_path
3115
3116 void
3117 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
3118                                    const gchar  *path)
3119 {
3120   gchar *utf8_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
3121
3122   gtk_icon_theme_append_search_path_utf8 (icon_theme, utf8_path);
3123
3124   g_free (utf8_path);
3125 }
3126
3127 #undef gtk_icon_theme_prepend_search_path
3128
3129 void
3130 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
3131                                     const gchar  *path)
3132 {
3133   gchar *utf8_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
3134
3135   gtk_icon_theme_prepend_search_path_utf8 (icon_theme, utf8_path);
3136
3137   g_free (utf8_path);
3138 }
3139
3140 #undef gtk_icon_info_get_filename
3141
3142 G_CONST_RETURN gchar *
3143 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
3144 {
3145   g_return_val_if_fail (icon_info != NULL, NULL);
3146
3147   return icon_info->cp_filename;
3148 }
3149
3150 #endif
3151
3152 #define __GTK_ICON_THEME_C__
3153 #include "gtkaliasdef.c"