]> Pileus Git - ~andy/gtk/blob - gtk/gtkicontheme.c
Merge ssh://git.gnome.org/git/gtk+
[~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 #define WIN32_MEAN_AND_LEAN
37 #include <windows.h>
38 #include "win32/gdkwin32.h"
39 #endif /* G_OS_WIN32 */
40
41 #include "gtkicontheme.h"
42 #include "gtkdebug.h"
43 #include "gtkiconfactory.h"
44 #include "gtkiconcache.h"
45 #include "gtkbuiltincache.h"
46 #include "gtkintl.h"
47 #include "gtkmain.h"
48 #include "gtknumerableiconprivate.h"
49 #include "gtksettings.h"
50 #include "gtkprivate.h"
51
52 #define DEFAULT_THEME_NAME "hicolor"
53
54 typedef enum
55 {
56   ICON_THEME_DIR_FIXED,  
57   ICON_THEME_DIR_SCALABLE,  
58   ICON_THEME_DIR_THRESHOLD,
59   ICON_THEME_DIR_UNTHEMED
60 } IconThemeDirType;
61
62 /* In reverse search order: */
63 typedef enum
64 {
65   ICON_SUFFIX_NONE = 0,
66   ICON_SUFFIX_XPM = 1 << 0,
67   ICON_SUFFIX_SVG = 1 << 1,
68   ICON_SUFFIX_PNG = 1 << 2,
69   HAS_ICON_FILE = 1 << 3
70 } IconSuffix;
71
72
73 struct _GtkIconThemePrivate
74 {
75   guint custom_theme        : 1;
76   guint is_screen_singleton : 1;
77   guint pixbuf_supports_svg : 1;
78   guint themes_valid        : 1;
79   guint check_reload        : 1;
80   guint loading_themes      : 1;
81   
82   char *current_theme;
83   char *fallback_theme;
84   char **search_path;
85   int search_path_len;
86
87   /* A list of all the themes needed to look up icons.
88    * In search order, without duplicates
89    */
90   GList *themes;
91   GHashTable *unthemed_icons;
92   
93   /* Note: The keys of this hashtable are owned by the
94    * themedir and unthemed hashtables.
95    */
96   GHashTable *all_icons;
97
98   /* GdkScreen for the icon theme (may be NULL)
99    */
100   GdkScreen *screen;
101   
102   /* time when we last stat:ed for theme changes */
103   long last_stat_time;
104   GList *dir_mtimes;
105
106   gulong reset_styles_idle;
107 };
108
109 struct _GtkIconInfo
110 {
111   /* Information about the source
112    */
113   gchar *filename;
114   GLoadableIcon *loadable;
115   GSList *emblem_infos;
116
117   /* Cache pixbuf (if there is any) */
118   GdkPixbuf *cache_pixbuf;
119
120   GtkIconData *data;
121   
122   /* Information about the directory where
123    * the source was found
124    */
125   IconThemeDirType dir_type;
126   gint dir_size;
127   gint threshold;
128
129   /* Parameters influencing the scaled icon
130    */
131   gint desired_size;
132   guint raw_coordinates : 1;
133   guint forced_size     : 1;
134
135   /* Cached information if we go ahead and try to load
136    * the icon.
137    */
138   GdkPixbuf *pixbuf;
139   GError *load_error;
140   gdouble scale;
141   gboolean emblems_applied;
142
143   guint ref_count;
144 };
145
146 typedef struct
147 {
148   char *name;
149   char *display_name;
150   char *comment;
151   char *example;
152
153   /* In search order */
154   GList *dirs;
155 } IconTheme;
156
157 typedef struct
158 {
159   IconThemeDirType type;
160   GQuark context;
161
162   int size;
163   int min_size;
164   int max_size;
165   int threshold;
166
167   char *dir;
168   char *subdir;
169   int subdir_index;
170   
171   GtkIconCache *cache;
172   
173   GHashTable *icons;
174   GHashTable *icon_data;
175 } IconThemeDir;
176
177 typedef struct
178 {
179   char *svg_filename;
180   char *no_svg_filename;
181 } UnthemedIcon;
182
183 typedef struct
184 {
185   gint size;
186   GdkPixbuf *pixbuf;
187 } BuiltinIcon;
188
189 typedef struct 
190 {
191   char *dir;
192   time_t mtime; /* 0 == not existing or not a dir */
193
194   GtkIconCache *cache;
195 } IconThemeDirMtime;
196
197 static void  gtk_icon_theme_finalize   (GObject              *object);
198 static void  theme_dir_destroy         (IconThemeDir         *dir);
199
200 static void         theme_destroy     (IconTheme        *theme);
201 static GtkIconInfo *theme_lookup_icon (IconTheme        *theme,
202                                        const char       *icon_name,
203                                        int               size,
204                                        gboolean          allow_svg,
205                                        gboolean          use_default_icons);
206 static void         theme_list_icons  (IconTheme        *theme,
207                                        GHashTable       *icons,
208                                        GQuark            context);
209 static void         theme_list_contexts  (IconTheme        *theme,
210                                           GHashTable       *contexts);
211 static void         theme_subdir_load (GtkIconTheme     *icon_theme,
212                                        IconTheme        *theme,
213                                        GKeyFile         *theme_file,
214                                        char             *subdir);
215 static void         do_theme_change   (GtkIconTheme     *icon_theme);
216
217 static void     blow_themes               (GtkIconTheme    *icon_themes);
218 static gboolean rescan_themes             (GtkIconTheme    *icon_themes);
219
220 static void  icon_data_free            (GtkIconData     *icon_data);
221 static void load_icon_data             (IconThemeDir    *dir,
222                                         const char      *path,
223                                         const char      *name);
224
225 static IconSuffix theme_dir_get_icon_suffix (IconThemeDir *dir,
226                                              const gchar  *icon_name,
227                                              gboolean     *has_icon_file);
228
229
230 static GtkIconInfo *icon_info_new             (void);
231 static GtkIconInfo *icon_info_new_builtin     (BuiltinIcon *icon);
232
233 static IconSuffix suffix_from_name (const char *name);
234
235 static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
236                                        gint        size,
237                                        gint        *min_difference_p,
238                                        gboolean    *has_larger_p);
239
240 static guint signal_changed = 0;
241
242 static GHashTable *icon_theme_builtin_icons;
243
244 /* also used in gtkiconfactory.c */
245 GtkIconCache *_builtin_cache = NULL;
246 static GList *builtin_dirs = NULL;
247
248 G_DEFINE_TYPE (GtkIconTheme, gtk_icon_theme, G_TYPE_OBJECT)
249
250 /**
251  * gtk_icon_theme_new:
252  * 
253  * Creates a new icon theme object. Icon theme objects are used
254  * to lookup up an icon by name in a particular icon theme.
255  * Usually, you'll want to use gtk_icon_theme_get_default()
256  * or gtk_icon_theme_get_for_screen() rather than creating
257  * a new icon theme object for scratch.
258  * 
259  * Return value: the newly created #GtkIconTheme object.
260  *
261  * Since: 2.4
262  **/
263 GtkIconTheme *
264 gtk_icon_theme_new (void)
265 {
266   return g_object_new (GTK_TYPE_ICON_THEME, NULL);
267 }
268
269 /**
270  * gtk_icon_theme_get_default:
271  * 
272  * Gets the icon theme for the default screen. See
273  * gtk_icon_theme_get_for_screen().
274  *
275  * Return value: (transfer none): A unique #GtkIconTheme associated with
276  *  the default screen. This icon theme is associated with
277  *  the screen and can be used as long as the screen
278  *  is open. Do not ref or unref it.
279  *
280  * Since: 2.4
281  **/
282 GtkIconTheme *
283 gtk_icon_theme_get_default (void)
284 {
285   return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
286 }
287
288 /**
289  * gtk_icon_theme_get_for_screen:
290  * @screen: a #GdkScreen
291  * 
292  * Gets the icon theme object associated with @screen; if this
293  * function has not previously been called for the given
294  * screen, a new icon theme object will be created and
295  * associated with the screen. Icon theme objects are
296  * fairly expensive to create, so using this function
297  * is usually a better choice than calling than gtk_icon_theme_new()
298  * and setting the screen yourself; by using this function
299  * a single icon theme object will be shared between users.
300  *
301  * Return value: (transfer none): A unique #GtkIconTheme associated with
302  *  the given screen. This icon theme is associated with
303  *  the screen and can be used as long as the screen
304  *  is open. Do not ref or unref it.
305  *
306  * Since: 2.4
307  **/
308 GtkIconTheme *
309 gtk_icon_theme_get_for_screen (GdkScreen *screen)
310 {
311   GtkIconTheme *icon_theme;
312
313   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
314
315   icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
316   if (!icon_theme)
317     {
318       GtkIconThemePrivate *priv;
319
320       icon_theme = gtk_icon_theme_new ();
321       gtk_icon_theme_set_screen (icon_theme, screen);
322
323       priv = icon_theme->priv;
324       priv->is_screen_singleton = TRUE;
325
326       g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), icon_theme);
327     }
328
329   return icon_theme;
330 }
331
332 static void
333 gtk_icon_theme_class_init (GtkIconThemeClass *klass)
334 {
335   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
336
337   gobject_class->finalize = gtk_icon_theme_finalize;
338
339 /**
340  * GtkIconTheme::changed
341  * @icon_theme: the icon theme
342  * 
343  * Emitted when the current icon theme is switched or GTK+ detects
344  * that a change has occurred in the contents of the current
345  * icon theme.
346  **/
347   signal_changed = g_signal_new (I_("changed"),
348                                  G_TYPE_FROM_CLASS (klass),
349                                  G_SIGNAL_RUN_LAST,
350                                  G_STRUCT_OFFSET (GtkIconThemeClass, changed),
351                                  NULL, NULL,
352                                  g_cclosure_marshal_VOID__VOID,
353                                  G_TYPE_NONE, 0);
354
355   g_type_class_add_private (klass, sizeof (GtkIconThemePrivate));
356 }
357
358
359 /* Callback when the display that the icon theme is attached to
360  * is closed; unset the screen, and if it's the unique theme
361  * for the screen, drop the reference
362  */
363 static void
364 display_closed (GdkDisplay   *display,
365                 gboolean      is_error,
366                 GtkIconTheme *icon_theme)
367 {
368   GtkIconThemePrivate *priv = icon_theme->priv;
369   GdkScreen *screen = priv->screen;
370   gboolean was_screen_singleton = priv->is_screen_singleton;
371
372   if (was_screen_singleton)
373     {
374       g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), NULL);
375       priv->is_screen_singleton = FALSE;
376     }
377
378   gtk_icon_theme_set_screen (icon_theme, NULL);
379
380   if (was_screen_singleton)
381     {
382       g_object_unref (icon_theme);
383     }
384 }
385
386 static void
387 update_current_theme (GtkIconTheme *icon_theme)
388 {
389 #define theme_changed(_old, _new) \
390   ((_old && !_new) || (!_old && _new) || \
391    (_old && _new && strcmp (_old, _new) != 0))
392   GtkIconThemePrivate *priv = icon_theme->priv;
393
394   if (!priv->custom_theme)
395     {
396       gchar *theme = NULL;
397       gchar *fallback_theme = NULL;
398       gboolean changed = FALSE;
399
400       if (priv->screen)
401         {
402           GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
403           g_object_get (settings, 
404                         "gtk-icon-theme-name", &theme, 
405                         "gtk-fallback-icon-theme", &fallback_theme, NULL);
406         }
407
408       /* ensure that the current theme (even when just the default)
409        * is searched before any fallback theme
410        */
411       if (!theme && fallback_theme)
412         theme = g_strdup (DEFAULT_THEME_NAME);
413
414       if (theme_changed (priv->current_theme, theme))
415         {
416           g_free (priv->current_theme);
417           priv->current_theme = theme;
418           changed = TRUE;
419         }
420       else
421         g_free (theme);
422
423       if (theme_changed (priv->fallback_theme, fallback_theme))
424         {
425           g_free (priv->fallback_theme);
426           priv->fallback_theme = fallback_theme;
427           changed = TRUE;
428         }
429       else
430         g_free (fallback_theme);
431
432       if (changed)
433         do_theme_change (icon_theme);
434     }
435 #undef theme_changed
436 }
437
438 /* Callback when the icon theme GtkSetting changes
439  */
440 static void
441 theme_changed (GtkSettings  *settings,
442                GParamSpec   *pspec,
443                GtkIconTheme *icon_theme)
444 {
445   update_current_theme (icon_theme);
446 }
447
448 static void
449 unset_screen (GtkIconTheme *icon_theme)
450 {
451   GtkIconThemePrivate *priv = icon_theme->priv;
452   GtkSettings *settings;
453   GdkDisplay *display;
454   
455   if (priv->screen)
456     {
457       settings = gtk_settings_get_for_screen (priv->screen);
458       display = gdk_screen_get_display (priv->screen);
459       
460       g_signal_handlers_disconnect_by_func (display,
461                                             (gpointer) display_closed,
462                                             icon_theme);
463       g_signal_handlers_disconnect_by_func (settings,
464                                             (gpointer) theme_changed,
465                                             icon_theme);
466
467       priv->screen = NULL;
468     }
469 }
470
471 /**
472  * gtk_icon_theme_set_screen:
473  * @icon_theme: a #GtkIconTheme
474  * @screen: a #GdkScreen
475  * 
476  * Sets the screen for an icon theme; the screen is used
477  * to track the user's currently configured icon theme,
478  * which might be different for different screens.
479  *
480  * Since: 2.4
481  **/
482 void
483 gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
484                            GdkScreen    *screen)
485 {
486   GtkIconThemePrivate *priv;
487   GtkSettings *settings;
488   GdkDisplay *display;
489
490   g_return_if_fail (GTK_ICON_THEME (icon_theme));
491   g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
492
493   priv = icon_theme->priv;
494
495   unset_screen (icon_theme);
496   
497   if (screen)
498     {
499       display = gdk_screen_get_display (screen);
500       settings = gtk_settings_get_for_screen (screen);
501       
502       priv->screen = screen;
503       
504       g_signal_connect (display, "closed",
505                         G_CALLBACK (display_closed), icon_theme);
506       g_signal_connect (settings, "notify::gtk-icon-theme-name",
507                         G_CALLBACK (theme_changed), icon_theme);
508       g_signal_connect (settings, "notify::gtk-fallback-icon-theme-name",
509                         G_CALLBACK (theme_changed), icon_theme);
510     }
511
512   update_current_theme (icon_theme);
513 }
514
515 /* Checks whether a loader for SVG files has been registered
516  * with GdkPixbuf.
517  */
518 static gboolean
519 pixbuf_supports_svg (void)
520 {
521   GSList *formats;
522   GSList *tmp_list;
523   static gint found_svg = -1;
524
525   if (found_svg != -1)
526     return found_svg;
527
528   formats = gdk_pixbuf_get_formats ();
529
530   found_svg = FALSE; 
531   for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
532     {
533       gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
534       gchar **mime_type;
535       
536       for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
537         {
538           if (strcmp (*mime_type, "image/svg") == 0)
539             found_svg = TRUE;
540         }
541
542       g_strfreev (mime_types);
543     }
544
545   g_slist_free (formats);
546   
547   return found_svg;
548 }
549
550 static void
551 gtk_icon_theme_init (GtkIconTheme *icon_theme)
552 {
553   GtkIconThemePrivate *priv;
554   const gchar * const *xdg_data_dirs;
555   int i, j;
556
557   priv = G_TYPE_INSTANCE_GET_PRIVATE (icon_theme,
558                                       GTK_TYPE_ICON_THEME,
559                                       GtkIconThemePrivate);
560   icon_theme->priv = priv;
561
562   priv->custom_theme = FALSE;
563
564   xdg_data_dirs = g_get_system_data_dirs ();
565   for (i = 0; xdg_data_dirs[i]; i++) ;
566
567   priv->search_path_len = 2 * i + 2;
568   
569   priv->search_path = g_new (char *, priv->search_path_len);
570   
571   i = 0;
572   priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
573   priv->search_path[i++] = g_build_filename (g_get_user_data_dir (), "icons", NULL);
574   
575   for (j = 0; xdg_data_dirs[j]; j++) 
576     priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
577
578   for (j = 0; xdg_data_dirs[j]; j++) 
579     priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "pixmaps", NULL);
580
581   priv->themes_valid = FALSE;
582   priv->themes = NULL;
583   priv->unthemed_icons = NULL;
584   
585   priv->pixbuf_supports_svg = pixbuf_supports_svg ();
586 }
587
588 static void
589 free_dir_mtime (IconThemeDirMtime *dir_mtime)
590 {
591   if (dir_mtime->cache)
592     _gtk_icon_cache_unref (dir_mtime->cache);
593
594   g_free (dir_mtime->dir);
595   g_slice_free (IconThemeDirMtime, dir_mtime);
596
597 }
598
599 static gboolean
600 reset_styles_idle (gpointer user_data)
601 {
602   GtkIconTheme *icon_theme;
603   GtkIconThemePrivate *priv;
604
605   icon_theme = GTK_ICON_THEME (user_data);
606   priv = icon_theme->priv;
607
608   if (priv->screen && priv->is_screen_singleton)
609     gtk_style_context_reset_widgets (priv->screen);
610
611   priv->reset_styles_idle = 0;
612
613   return FALSE;
614 }
615
616 static void
617 do_theme_change (GtkIconTheme *icon_theme)
618 {
619   GtkIconThemePrivate *priv = icon_theme->priv;
620
621   if (!priv->themes_valid)
622     return;
623   
624   GTK_NOTE (ICONTHEME, 
625             g_print ("change to icon theme \"%s\"\n", priv->current_theme));
626   blow_themes (icon_theme);
627   g_signal_emit (icon_theme, signal_changed, 0);
628
629   if (!priv->reset_styles_idle)
630     priv->reset_styles_idle = 
631       gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2, 
632                        reset_styles_idle, icon_theme, NULL);
633 }
634
635 static void
636 blow_themes (GtkIconTheme *icon_theme)
637 {
638   GtkIconThemePrivate *priv = icon_theme->priv;
639   
640   if (priv->themes_valid)
641     {
642       g_hash_table_destroy (priv->all_icons);
643       g_list_foreach (priv->themes, (GFunc)theme_destroy, NULL);
644       g_list_free (priv->themes);
645       g_list_foreach (priv->dir_mtimes, (GFunc)free_dir_mtime, NULL);
646       g_list_free (priv->dir_mtimes);
647       g_hash_table_destroy (priv->unthemed_icons);
648     }
649   priv->themes = NULL;
650   priv->unthemed_icons = NULL;
651   priv->dir_mtimes = NULL;
652   priv->all_icons = NULL;
653   priv->themes_valid = FALSE;
654 }
655
656 static void
657 gtk_icon_theme_finalize (GObject *object)
658 {
659   GtkIconTheme *icon_theme;
660   GtkIconThemePrivate *priv;
661   int i;
662
663   icon_theme = GTK_ICON_THEME (object);
664   priv = icon_theme->priv;
665
666   if (priv->reset_styles_idle)
667     {
668       g_source_remove (priv->reset_styles_idle);
669       priv->reset_styles_idle = 0;
670     }
671
672   unset_screen (icon_theme);
673
674   g_free (priv->current_theme);
675   priv->current_theme = NULL;
676
677   for (i = 0; i < priv->search_path_len; i++)
678     g_free (priv->search_path[i]);
679
680   g_free (priv->search_path);
681   priv->search_path = NULL;
682
683   blow_themes (icon_theme);
684
685   G_OBJECT_CLASS (gtk_icon_theme_parent_class)->finalize (object);  
686 }
687
688 /**
689  * gtk_icon_theme_set_search_path:
690  * @icon_theme: a #GtkIconTheme
691  * @path: array of directories that are searched for icon themes
692  * @n_elements: number of elements in @path.
693  * 
694  * Sets the search path for the icon theme object. When looking
695  * for an icon theme, GTK+ will search for a subdirectory of
696  * one or more of the directories in @path with the same name
697  * as the icon theme. (Themes from multiple of the path elements
698  * are combined to allow themes to be extended by adding icons
699  * in the user's home directory.)
700  *
701  * In addition if an icon found isn't found either in the current
702  * icon theme or the default icon theme, and an image file with
703  * the right name is found directly in one of the elements of
704  * @path, then that image will be used for the icon name.
705  * (This is legacy feature, and new icons should be put
706  * into the default icon theme, which is called DEFAULT_THEME_NAME,
707  * rather than directly on the icon path.)
708  *
709  * Since: 2.4
710  **/
711 void
712 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
713                                 const gchar  *path[],
714                                 gint          n_elements)
715 {
716   GtkIconThemePrivate *priv;
717   gint i;
718
719   g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
720
721   priv = icon_theme->priv;
722   for (i = 0; i < priv->search_path_len; i++)
723     g_free (priv->search_path[i]);
724
725   g_free (priv->search_path);
726
727   priv->search_path = g_new (gchar *, n_elements);
728   priv->search_path_len = n_elements;
729
730   for (i = 0; i < priv->search_path_len; i++)
731     priv->search_path[i] = g_strdup (path[i]);
732
733   do_theme_change (icon_theme);
734 }
735
736
737 /**
738  * gtk_icon_theme_get_search_path:
739  * @icon_theme: a #GtkIconTheme
740  * @path: (allow-none) (array length=n_elements) (out): location to store a list of icon theme path directories or %NULL
741  *        The stored value should be freed with g_strfreev().
742  * @n_elements: location to store number of elements
743  *              in @path, or %NULL
744  * 
745  * Gets the current search path. See gtk_icon_theme_set_search_path().
746  *
747  * Since: 2.4
748  **/
749 void
750 gtk_icon_theme_get_search_path (GtkIconTheme      *icon_theme,
751                                 gchar            **path[],
752                                 gint              *n_elements)
753 {
754   GtkIconThemePrivate *priv;
755   int i;
756
757   g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
758
759   priv = icon_theme->priv;
760
761   if (n_elements)
762     *n_elements = priv->search_path_len;
763   
764   if (path)
765     {
766       *path = g_new (gchar *, priv->search_path_len + 1);
767       for (i = 0; i < priv->search_path_len; i++)
768         (*path)[i] = g_strdup (priv->search_path[i]);
769       (*path)[i] = NULL;
770     }
771 }
772
773 /**
774  * gtk_icon_theme_append_search_path:
775  * @icon_theme: a #GtkIconTheme
776  * @path: directory name to append to the icon path
777  * 
778  * Appends a directory to the search path. 
779  * See gtk_icon_theme_set_search_path(). 
780  *
781  * Since: 2.4
782  **/
783 void
784 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
785                                    const gchar  *path)
786 {
787   GtkIconThemePrivate *priv;
788
789   g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
790   g_return_if_fail (path != NULL);
791
792   priv = icon_theme->priv;
793   
794   priv->search_path_len++;
795
796   priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
797   priv->search_path[priv->search_path_len-1] = g_strdup (path);
798
799   do_theme_change (icon_theme);
800 }
801
802 /**
803  * gtk_icon_theme_prepend_search_path:
804  * @icon_theme: a #GtkIconTheme
805  * @path: directory name to prepend to the icon path
806  * 
807  * Prepends a directory to the search path. 
808  * See gtk_icon_theme_set_search_path().
809  *
810  * Since: 2.4
811  **/
812 void
813 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
814                                     const gchar  *path)
815 {
816   GtkIconThemePrivate *priv;
817   int i;
818
819   g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
820   g_return_if_fail (path != NULL);
821
822   priv = icon_theme->priv;
823   
824   priv->search_path_len++;
825   priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
826
827   for (i = priv->search_path_len - 1; i > 0; i--)
828     priv->search_path[i] = priv->search_path[i - 1];
829   
830   priv->search_path[0] = g_strdup (path);
831
832   do_theme_change (icon_theme);
833 }
834
835 /**
836  * gtk_icon_theme_set_custom_theme:
837  * @icon_theme: a #GtkIconTheme
838  * @theme_name: name of icon theme to use instead of configured theme,
839  *   or %NULL to unset a previously set custom theme
840  * 
841  * Sets the name of the icon theme that the #GtkIconTheme object uses
842  * overriding system configuration. This function cannot be called
843  * on the icon theme objects returned from gtk_icon_theme_get_default()
844  * and gtk_icon_theme_get_for_screen().
845  *
846  * Since: 2.4
847  **/
848 void
849 gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
850                                  const gchar  *theme_name)
851 {
852   GtkIconThemePrivate *priv;
853
854   g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
855
856   priv = icon_theme->priv;
857
858   g_return_if_fail (!priv->is_screen_singleton);
859   
860   if (theme_name != NULL)
861     {
862       priv->custom_theme = TRUE;
863       if (!priv->current_theme || strcmp (theme_name, priv->current_theme) != 0)
864         {
865           g_free (priv->current_theme);
866           priv->current_theme = g_strdup (theme_name);
867
868           do_theme_change (icon_theme);
869         }
870     }
871   else
872     {
873       if (priv->custom_theme)
874         {
875           priv->custom_theme = FALSE;
876
877           update_current_theme (icon_theme);
878         }
879     }
880 }
881
882 static void
883 insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
884 {
885   int i;
886   GList *l;
887   char **dirs;
888   char **themes;
889   GtkIconThemePrivate *priv;
890   IconTheme *theme = NULL;
891   char *path;
892   GKeyFile *theme_file;
893   GError *error = NULL;
894   IconThemeDirMtime *dir_mtime;
895   struct stat stat_buf;
896   
897   priv = icon_theme->priv;
898
899   for (l = priv->themes; l != NULL; l = l->next)
900     {
901       theme = l->data;
902       if (strcmp (theme->name, theme_name) == 0)
903         return;
904     }
905   
906   for (i = 0; i < priv->search_path_len; i++)
907     {
908       path = g_build_filename (priv->search_path[i],
909                                theme_name,
910                                NULL);
911       dir_mtime = g_slice_new (IconThemeDirMtime);
912       dir_mtime->cache = NULL;
913       dir_mtime->dir = path;
914       if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
915         dir_mtime->mtime = stat_buf.st_mtime;
916       else
917         dir_mtime->mtime = 0;
918
919       priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
920     }
921   priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
922
923   theme_file = NULL;
924   for (i = 0; i < priv->search_path_len && !theme_file; i++)
925     {
926       path = g_build_filename (priv->search_path[i],
927                                theme_name,
928                                "index.theme",
929                                NULL);
930       if (g_file_test (path, G_FILE_TEST_IS_REGULAR)) 
931         {
932           theme_file = g_key_file_new ();
933           g_key_file_set_list_separator (theme_file, ',');
934           g_key_file_load_from_file (theme_file, path, 0, &error);
935           if (error)
936             {
937               g_key_file_free (theme_file);
938               theme_file = NULL;
939               g_error_free (error);
940               error = NULL;
941             }
942         }
943       g_free (path);
944     }
945
946   if (theme_file || strcmp (theme_name, DEFAULT_THEME_NAME) == 0)
947     {
948       theme = g_new0 (IconTheme, 1);
949       theme->name = g_strdup (theme_name);
950       priv->themes = g_list_prepend (priv->themes, theme);
951     }
952
953   if (theme_file == NULL)
954     return;
955
956   theme->display_name = 
957     g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
958   if (!theme->display_name)
959     g_warning ("Theme file for %s has no name\n", theme_name);
960
961   dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
962   if (!dirs)
963     {
964       g_warning ("Theme file for %s has no directories\n", theme_name);
965       priv->themes = g_list_remove (priv->themes, theme);
966       g_free (theme->name);
967       g_free (theme->display_name);
968       g_free (theme);
969       g_key_file_free (theme_file);
970       return;
971     }
972   
973   theme->comment = 
974     g_key_file_get_locale_string (theme_file, 
975                                   "Icon Theme", "Comment",
976                                   NULL, NULL);
977   theme->example = 
978     g_key_file_get_string (theme_file, 
979                            "Icon Theme", "Example",
980                            NULL);
981
982   theme->dirs = NULL;
983   for (i = 0; dirs[i] != NULL; i++)
984     theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
985
986   g_strfreev (dirs);
987
988   theme->dirs = g_list_reverse (theme->dirs);
989
990   themes = g_key_file_get_string_list (theme_file,
991                                        "Icon Theme",
992                                        "Inherits",
993                                        NULL,
994                                        NULL);
995   if (themes)
996     {
997       for (i = 0; themes[i] != NULL; i++)
998         insert_theme (icon_theme, themes[i]);
999       
1000       g_strfreev (themes);
1001     }
1002
1003   g_key_file_free (theme_file);
1004 }
1005
1006 static void
1007 free_unthemed_icon (UnthemedIcon *unthemed_icon)
1008 {
1009   g_free (unthemed_icon->svg_filename);
1010   g_free (unthemed_icon->no_svg_filename);
1011   g_slice_free (UnthemedIcon, unthemed_icon);
1012 }
1013
1014 static char *
1015 strip_suffix (const char *filename)
1016 {
1017   const char *dot;
1018
1019   dot = strrchr (filename, '.');
1020
1021   if (dot == NULL)
1022     return g_strdup (filename);
1023
1024   return g_strndup (filename, dot - filename);
1025 }
1026
1027 static void
1028 load_themes (GtkIconTheme *icon_theme)
1029 {
1030   GtkIconThemePrivate *priv;
1031   GDir *gdir;
1032   int base;
1033   char *dir;
1034   const char *file;
1035   UnthemedIcon *unthemed_icon;
1036   IconSuffix old_suffix, new_suffix;
1037   GTimeVal tv;
1038   IconThemeDirMtime *dir_mtime;
1039   struct stat stat_buf;
1040   
1041   priv = icon_theme->priv;
1042
1043   priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
1044   
1045   if (priv->current_theme)
1046     insert_theme (icon_theme, priv->current_theme);
1047
1048   /* Always look in the "default" icon theme, and in a fallback theme */
1049   if (priv->fallback_theme)
1050     insert_theme (icon_theme, priv->fallback_theme);
1051   insert_theme (icon_theme, DEFAULT_THEME_NAME);
1052   priv->themes = g_list_reverse (priv->themes);
1053
1054
1055   priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
1056                                                 g_free, (GDestroyNotify)free_unthemed_icon);
1057
1058   for (base = 0; base < icon_theme->priv->search_path_len; base++)
1059     {
1060       dir = icon_theme->priv->search_path[base];
1061
1062       dir_mtime = g_slice_new (IconThemeDirMtime);
1063       priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
1064       
1065       dir_mtime->dir = g_strdup (dir);
1066       dir_mtime->mtime = 0;
1067       dir_mtime->cache = NULL;
1068
1069       if (g_stat (dir, &stat_buf) != 0 || !S_ISDIR (stat_buf.st_mode))
1070         continue;
1071       dir_mtime->mtime = stat_buf.st_mtime;
1072
1073       dir_mtime->cache = _gtk_icon_cache_new_for_path (dir);
1074       if (dir_mtime->cache != NULL)
1075         continue;
1076
1077       gdir = g_dir_open (dir, 0, NULL);
1078       if (gdir == NULL)
1079         continue;
1080
1081       while ((file = g_dir_read_name (gdir)))
1082         {
1083           new_suffix = suffix_from_name (file);
1084           
1085           if (new_suffix != ICON_SUFFIX_NONE)
1086             {
1087               char *abs_file;
1088               char *base_name;
1089
1090               abs_file = g_build_filename (dir, file, NULL);
1091               base_name = strip_suffix (file);
1092
1093               if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
1094                                                         base_name)))
1095                 {
1096                   if (new_suffix == ICON_SUFFIX_SVG)
1097                     {
1098                       if (unthemed_icon->svg_filename)
1099                         g_free (abs_file);
1100                       else
1101                         unthemed_icon->svg_filename = abs_file;
1102                     }
1103                   else
1104                     {
1105                       if (unthemed_icon->no_svg_filename)
1106                         {
1107                           old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
1108                           if (new_suffix > old_suffix)
1109                             {
1110                               g_free (unthemed_icon->no_svg_filename);
1111                               unthemed_icon->no_svg_filename = abs_file;                              
1112                             }
1113                           else
1114                             g_free (abs_file);
1115                         }
1116                       else
1117                         unthemed_icon->no_svg_filename = abs_file;                            
1118                     }
1119
1120                   g_free (base_name);
1121                 }
1122               else
1123                 {
1124                   unthemed_icon = g_slice_new0 (UnthemedIcon);
1125                   
1126                   if (new_suffix == ICON_SUFFIX_SVG)
1127                     unthemed_icon->svg_filename = abs_file;
1128                   else
1129                     unthemed_icon->no_svg_filename = abs_file;
1130
1131                   g_hash_table_insert (priv->unthemed_icons,
1132                                        base_name,
1133                                        unthemed_icon);
1134                   g_hash_table_insert (priv->all_icons,
1135                                        base_name, NULL);
1136                 }
1137             }
1138         }
1139       g_dir_close (gdir);
1140     }
1141
1142   priv->themes_valid = TRUE;
1143   
1144   g_get_current_time(&tv);
1145   priv->last_stat_time = tv.tv_sec;
1146 }
1147
1148 void
1149 _gtk_icon_theme_ensure_builtin_cache (void)
1150 {
1151   static gboolean initialized = FALSE;
1152   IconThemeDir *dir;
1153   static IconThemeDir dirs[5] = 
1154     {
1155       { ICON_THEME_DIR_THRESHOLD, 0, 16, 16, 16, 2, NULL, "16", -1, NULL, NULL, NULL },
1156       { ICON_THEME_DIR_THRESHOLD, 0, 20, 20, 20, 2, NULL, "20", -1,  NULL, NULL, NULL },
1157       { ICON_THEME_DIR_THRESHOLD, 0, 24, 24, 24, 2, NULL, "24", -1, NULL, NULL, NULL },
1158       { ICON_THEME_DIR_THRESHOLD, 0, 32, 32, 32, 2, NULL, "32", -1, NULL, NULL, NULL },
1159       { ICON_THEME_DIR_THRESHOLD, 0, 48, 48, 48, 2, NULL, "48", -1, NULL, NULL, NULL }
1160     };
1161   gint i;
1162
1163   if (!initialized)
1164     {
1165       initialized = TRUE;
1166
1167       _builtin_cache = _gtk_icon_cache_new ((gchar *)builtin_icons);
1168
1169       for (i = 0; i < G_N_ELEMENTS (dirs); i++)
1170         {
1171           dir = &(dirs[i]);
1172           dir->cache = _gtk_icon_cache_ref (_builtin_cache);
1173           dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
1174
1175           builtin_dirs = g_list_append (builtin_dirs, dir);
1176         }
1177     }
1178 }
1179
1180 static void
1181 ensure_valid_themes (GtkIconTheme *icon_theme)
1182 {
1183   GtkIconThemePrivate *priv = icon_theme->priv;
1184   GTimeVal tv;
1185   gboolean was_valid = priv->themes_valid;
1186
1187   if (priv->loading_themes)
1188     return;
1189   priv->loading_themes = TRUE;
1190
1191   _gtk_icon_theme_ensure_builtin_cache ();
1192
1193   if (priv->themes_valid)
1194     {
1195       g_get_current_time (&tv);
1196
1197       if (ABS (tv.tv_sec - priv->last_stat_time) > 5 &&
1198           rescan_themes (icon_theme))
1199         blow_themes (icon_theme);
1200     }
1201   
1202   if (!priv->themes_valid)
1203     {
1204       load_themes (icon_theme);
1205
1206       if (was_valid)
1207         {
1208           g_signal_emit (icon_theme, signal_changed, 0);
1209
1210           if (!priv->check_reload && priv->screen)
1211             {     
1212               static GdkAtom atom_iconthemes = GDK_NONE;
1213               GdkEvent *event = gdk_event_new (GDK_CLIENT_EVENT);
1214               int i;
1215
1216               if (!atom_iconthemes)
1217                 atom_iconthemes = gdk_atom_intern_static_string ("_GTK_LOAD_ICONTHEMES");
1218
1219               for (i = 0; i < 5; i++)
1220                 event->client.data.l[i] = 0;
1221               event->client.data_format = 32;
1222               event->client.message_type = atom_iconthemes;
1223
1224               gdk_screen_broadcast_client_message (priv->screen, event);
1225             }
1226         }
1227     }
1228
1229   priv->loading_themes = FALSE;
1230 }
1231
1232 static GtkIconInfo *
1233 choose_icon (GtkIconTheme       *icon_theme,
1234              const gchar        *icon_names[],
1235              gint                size,
1236              GtkIconLookupFlags  flags)
1237 {
1238   GtkIconThemePrivate *priv;
1239   GList *l;
1240   GtkIconInfo *icon_info = NULL;
1241   UnthemedIcon *unthemed_icon = NULL;
1242   gboolean allow_svg;
1243   gboolean use_builtin;
1244   gint i;
1245
1246   priv = icon_theme->priv;
1247
1248   if (flags & GTK_ICON_LOOKUP_NO_SVG)
1249     allow_svg = FALSE;
1250   else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
1251     allow_svg = TRUE;
1252   else
1253     allow_svg = priv->pixbuf_supports_svg;
1254
1255   use_builtin = flags & GTK_ICON_LOOKUP_USE_BUILTIN;
1256   
1257   ensure_valid_themes (icon_theme);
1258
1259   for (l = priv->themes; l; l = l->next)
1260     {
1261       IconTheme *theme = l->data;
1262       
1263       for (i = 0; icon_names[i]; i++)
1264         {
1265           icon_info = theme_lookup_icon (theme, icon_names[i], size, allow_svg, use_builtin);
1266           if (icon_info)
1267             goto out;
1268         }
1269     }
1270
1271   for (i = 0; icon_names[i]; i++)
1272     {
1273       unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_names[i]);
1274       if (unthemed_icon)
1275         break;
1276     }
1277 #ifdef G_OS_WIN32
1278   /* Still not found an icon, check if reference to a Win32 resource */
1279   if (!unthemed_icon)
1280     {
1281       gchar **resources;
1282       HICON hIcon = NULL;
1283       
1284       resources = g_strsplit (icon_names[0], ",", 0);
1285       if (resources[0])
1286         {
1287           wchar_t *wfile = g_utf8_to_utf16 (resources[0], -1, NULL, NULL, NULL);
1288           ExtractIconExW (wfile, resources[1] ? atoi (resources[1]) : 0, &hIcon, NULL, 1);
1289           g_free (wfile);
1290         }
1291       
1292       if (hIcon)
1293         {
1294           icon_info = icon_info_new ();
1295           icon_info->cache_pixbuf = gdk_win32_icon_to_pixbuf_libgtk_only (hIcon);
1296           DestroyIcon (hIcon);
1297           icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1298           icon_info->dir_size = size;
1299         }
1300       g_strfreev (resources);
1301     }
1302 #endif
1303
1304   if (unthemed_icon)
1305     {
1306       icon_info = icon_info_new ();
1307
1308       /* A SVG icon, when allowed, beats out a XPM icon, but not
1309        * a PNG icon
1310        */
1311       if (allow_svg &&
1312           unthemed_icon->svg_filename &&
1313           (!unthemed_icon->no_svg_filename ||
1314            suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
1315         icon_info->filename = g_strdup (unthemed_icon->svg_filename);
1316       else if (unthemed_icon->no_svg_filename)
1317         icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);
1318
1319       icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1320       icon_info->dir_size = size;
1321     }
1322
1323  out:
1324   if (icon_info) 
1325     {
1326       icon_info->desired_size = size;
1327       icon_info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
1328     }
1329   else
1330     {
1331       static gboolean check_for_default_theme = TRUE;
1332       char *default_theme_path;
1333       gboolean found = FALSE;
1334       unsigned i;
1335
1336       if (check_for_default_theme)
1337         {
1338           check_for_default_theme = FALSE;
1339
1340           for (i = 0; !found && i < priv->search_path_len; i++)
1341             {
1342               default_theme_path = g_build_filename (priv->search_path[i],
1343                                                      DEFAULT_THEME_NAME,
1344                                                      "index.theme",
1345                                                      NULL);
1346               found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
1347               g_free (default_theme_path);
1348             }
1349
1350           if (!found)
1351             {
1352               g_warning (_("Could not find the icon '%s'. The '%s' theme\n"
1353                            "was not found either, perhaps you need to install it.\n"
1354                            "You can get a copy from:\n"
1355                            "\t%s"),
1356                          icon_names[0], DEFAULT_THEME_NAME, "http://icon-theme.freedesktop.org/releases");
1357             }
1358         }
1359     }
1360
1361   return icon_info;
1362 }
1363
1364
1365 /**
1366  * gtk_icon_theme_lookup_icon:
1367  * @icon_theme: a #GtkIconTheme
1368  * @icon_name: the name of the icon to lookup
1369  * @size: desired icon size
1370  * @flags: flags modifying the behavior of the icon lookup
1371  * 
1372  * Looks up a named icon and returns a structure containing
1373  * information such as the filename of the icon. The icon
1374  * can then be rendered into a pixbuf using
1375  * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1376  * combines these two steps if all you need is the pixbuf.)
1377  * 
1378  * Return value: a #GtkIconInfo structure containing information
1379  * about the icon, or %NULL if the icon wasn't found. Free with
1380  * gtk_icon_info_free()
1381  *
1382  * Since: 2.4
1383  */
1384 GtkIconInfo *
1385 gtk_icon_theme_lookup_icon (GtkIconTheme       *icon_theme,
1386                             const gchar        *icon_name,
1387                             gint                size,
1388                             GtkIconLookupFlags  flags)
1389 {
1390   GtkIconInfo *info;
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
1397   GTK_NOTE (ICONTHEME, 
1398             g_print ("gtk_icon_theme_lookup_icon %s\n", icon_name));
1399
1400   if (flags & GTK_ICON_LOOKUP_GENERIC_FALLBACK)
1401     {
1402       gchar **names;
1403       gint dashes, i;
1404       gchar *p;
1405  
1406       dashes = 0;
1407       for (p = (gchar *) icon_name; *p; p++)
1408         if (*p == '-')
1409           dashes++;
1410
1411       names = g_new (gchar *, dashes + 2);
1412       names[0] = g_strdup (icon_name);
1413       for (i = 1; i <= dashes; i++)
1414         {
1415           names[i] = g_strdup (names[i - 1]);
1416           p = strrchr (names[i], '-');
1417           *p = '\0';
1418         }
1419       names[dashes + 1] = NULL;
1420    
1421       info = choose_icon (icon_theme, (const gchar **) names, size, flags);
1422       
1423       g_strfreev (names);
1424     }
1425   else 
1426     {
1427       const gchar *names[2];
1428       
1429       names[0] = icon_name;
1430       names[1] = NULL;
1431
1432       info = choose_icon (icon_theme, names, size, flags);
1433     }
1434
1435   return info;
1436 }
1437
1438 /**
1439  * gtk_icon_theme_choose_icon:
1440  * @icon_theme: a #GtkIconTheme
1441  * @icon_names: %NULL-terminated array of icon names to lookup
1442  * @size: desired icon size
1443  * @flags: flags modifying the behavior of the icon lookup
1444  * 
1445  * Looks up a named icon and returns a structure containing
1446  * information such as the filename of the icon. The icon
1447  * can then be rendered into a pixbuf using
1448  * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1449  * combines these two steps if all you need is the pixbuf.)
1450  *
1451  * If @icon_names contains more than one name, this function 
1452  * tries them all in the given order before falling back to 
1453  * inherited icon themes.
1454  * 
1455  * Return value: a #GtkIconInfo structure containing information
1456  * about the icon, or %NULL if the icon wasn't found. Free with
1457  * gtk_icon_info_free()
1458  *
1459  * Since: 2.12
1460  */
1461 GtkIconInfo *
1462 gtk_icon_theme_choose_icon (GtkIconTheme       *icon_theme,
1463                             const gchar        *icon_names[],
1464                             gint                size,
1465                             GtkIconLookupFlags  flags)
1466 {
1467   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1468   g_return_val_if_fail (icon_names != NULL, NULL);
1469   g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1470                         (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1471
1472   return choose_icon (icon_theme, icon_names, size, flags);
1473 }
1474
1475 /* Error quark */
1476 GQuark
1477 gtk_icon_theme_error_quark (void)
1478 {
1479   return g_quark_from_static_string ("gtk-icon-theme-error-quark");
1480 }
1481
1482 /**
1483  * gtk_icon_theme_load_icon:
1484  * @icon_theme: a #GtkIconTheme
1485  * @icon_name: the name of the icon to lookup
1486  * @size: the desired icon size. The resulting icon may not be
1487  *     exactly this size; see gtk_icon_info_load_icon().
1488  * @flags: flags modifying the behavior of the icon lookup
1489  * @error: (allow-none): Location to store error information on failure,
1490  *     or %NULL.
1491  *
1492  * Looks up an icon in an icon theme, scales it to the given size
1493  * and renders it into a pixbuf. This is a convenience function;
1494  * if more details about the icon are needed, use
1495  * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1496  *
1497  * Note that you probably want to listen for icon theme changes and
1498  * update the icon. This is usually done by connecting to the
1499  * GtkWidget::style-set signal. If for some reason you do not want to
1500  * update the icon when the icon theme changes, you should consider
1501  * using gdk_pixbuf_copy() to make a private copy of the pixbuf
1502  * returned by this function. Otherwise GTK+ may need to keep the old
1503  * icon theme loaded, which would be a waste of memory.
1504  *
1505  * Return value: (transfer full): the rendered icon; this may be a
1506  *     newly created icon or a new reference to an internal icon, so
1507  *     you must not modify the icon. Use g_object_unref() to release
1508  *     your reference to the icon. %NULL if the icon isn't found.
1509  *
1510  * Since: 2.4
1511  **/
1512 GdkPixbuf *
1513 gtk_icon_theme_load_icon (GtkIconTheme         *icon_theme,
1514                           const gchar          *icon_name,
1515                           gint                  size,
1516                           GtkIconLookupFlags    flags,
1517                           GError              **error)
1518 {
1519   GtkIconInfo *icon_info;
1520   GdkPixbuf *pixbuf = NULL;
1521   
1522   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1523   g_return_val_if_fail (icon_name != NULL, NULL);
1524   g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1525                         (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1526   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1527   
1528   icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
1529                                           flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1530   if (!icon_info)
1531     {
1532       g_set_error (error, GTK_ICON_THEME_ERROR,  GTK_ICON_THEME_NOT_FOUND,
1533                    _("Icon '%s' not present in theme"), icon_name);
1534       return NULL;
1535     }
1536
1537   pixbuf = gtk_icon_info_load_icon (icon_info, error);
1538   gtk_icon_info_free (icon_info);
1539
1540   return pixbuf;
1541 }
1542
1543 /**
1544  * gtk_icon_theme_has_icon:
1545  * @icon_theme: a #GtkIconTheme
1546  * @icon_name: the name of an icon
1547  * 
1548  * Checks whether an icon theme includes an icon
1549  * for a particular name.
1550  * 
1551  * Return value: %TRUE if @icon_theme includes an
1552  *  icon for @icon_name.
1553  *
1554  * Since: 2.4
1555  **/
1556 gboolean 
1557 gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
1558                          const char   *icon_name)
1559 {
1560   GtkIconThemePrivate *priv;
1561   GList *l;
1562
1563   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1564   g_return_val_if_fail (icon_name != NULL, FALSE);
1565
1566   priv = icon_theme->priv;
1567   
1568   ensure_valid_themes (icon_theme);
1569
1570   for (l = priv->dir_mtimes; l; l = l->next)
1571     {
1572       IconThemeDirMtime *dir_mtime = l->data;
1573       GtkIconCache *cache = dir_mtime->cache;
1574       
1575       if (cache && _gtk_icon_cache_has_icon (cache, icon_name))
1576         return TRUE;
1577     }
1578
1579   if (g_hash_table_lookup_extended (priv->all_icons,
1580                                     icon_name, NULL, NULL))
1581     return TRUE;
1582
1583   if (_builtin_cache &&
1584       _gtk_icon_cache_has_icon (_builtin_cache, icon_name))
1585     return TRUE;
1586
1587   if (icon_theme_builtin_icons &&
1588       g_hash_table_lookup_extended (icon_theme_builtin_icons,
1589                                     icon_name, NULL, NULL))
1590     return TRUE;
1591
1592   return FALSE;
1593 }
1594
1595 static void
1596 add_size (gpointer  key,
1597           gpointer  value,
1598           gpointer  user_data)
1599 {
1600   gint **res_p = user_data;
1601
1602   **res_p = GPOINTER_TO_INT (key);
1603
1604   (*res_p)++;
1605 }
1606
1607 /**
1608  * gtk_icon_theme_get_icon_sizes:
1609  * @icon_theme: a #GtkIconTheme
1610  * @icon_name: the name of an icon
1611  * 
1612  * Returns an array of integers describing the sizes at which
1613  * the icon is available without scaling. A size of -1 means 
1614  * that the icon is available in a scalable format. The array 
1615  * is zero-terminated.
1616  * 
1617  * Return value: An newly allocated array describing the sizes at
1618  * which the icon is available. The array should be freed with g_free()
1619  * when it is no longer needed.
1620  *
1621  * Since: 2.6
1622  **/
1623 gint *
1624 gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
1625                                const char   *icon_name)
1626 {
1627   GList *l, *d, *icons;
1628   GHashTable *sizes;
1629   gint *result, *r;
1630   guint suffix;  
1631   GtkIconThemePrivate *priv;
1632
1633   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1634   
1635   priv = icon_theme->priv;
1636
1637   ensure_valid_themes (icon_theme);
1638
1639   sizes = g_hash_table_new (g_direct_hash, g_direct_equal);
1640
1641   for (l = priv->themes; l; l = l->next)
1642     {
1643       IconTheme *theme = l->data;
1644       for (d = theme->dirs; d; d = d->next)
1645         {
1646           IconThemeDir *dir = d->data;
1647
1648           if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
1649             continue;
1650
1651           suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);      
1652           if (suffix != ICON_SUFFIX_NONE)
1653             {
1654               if (suffix == ICON_SUFFIX_SVG)
1655                 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1656               else
1657                 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1658             }
1659         }
1660     }
1661
1662   for (d = builtin_dirs; d; d = d->next)
1663     {
1664       IconThemeDir *dir = d->data;
1665       
1666       if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
1667         continue;
1668
1669       suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);          
1670       if (suffix != ICON_SUFFIX_NONE)
1671         {
1672           if (suffix == ICON_SUFFIX_SVG)
1673             g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1674           else
1675             g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1676         }
1677     }
1678
1679   if (icon_theme_builtin_icons)
1680     {
1681       icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
1682       
1683       while (icons)
1684         {
1685           BuiltinIcon *icon = icons->data;
1686         
1687           g_hash_table_insert (sizes, GINT_TO_POINTER (icon->size), NULL);
1688           icons = icons->next;
1689         }      
1690     }
1691
1692   r = result = g_new0 (gint, g_hash_table_size (sizes) + 1);
1693
1694   g_hash_table_foreach (sizes, add_size, &r);
1695   g_hash_table_destroy (sizes);
1696   
1697   return result;
1698 }
1699
1700 static void
1701 add_key_to_hash (gpointer  key,
1702                  gpointer  value,
1703                  gpointer  user_data)
1704 {
1705   GHashTable *hash = user_data;
1706
1707   g_hash_table_insert (hash, key, NULL);
1708 }
1709
1710 static void
1711 add_key_to_list (gpointer  key,
1712                  gpointer  value,
1713                  gpointer  user_data)
1714 {
1715   GList **list = user_data;
1716
1717   *list = g_list_prepend (*list, g_strdup (key));
1718 }
1719
1720 /**
1721  * gtk_icon_theme_list_icons:
1722  * @icon_theme: a #GtkIconTheme
1723  * @context: a string identifying a particular type of icon,
1724  *           or %NULL to list all icons.
1725  * 
1726  * Lists the icons in the current icon theme. Only a subset
1727  * of the icons can be listed by providing a context string.
1728  * The set of values for the context string is system dependent,
1729  * but will typically include such values as "Applications" and
1730  * "MimeTypes".
1731  *
1732  * Return value: (element-type utf8) (transfer none): a #GList list holding the names of all the
1733  *  icons in the theme. You must first free each element
1734  *  in the list with g_free(), then free the list itself
1735  *  with g_list_free().
1736  *
1737  * Since: 2.4
1738  **/
1739 GList *
1740 gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
1741                            const char   *context)
1742 {
1743   GtkIconThemePrivate *priv;
1744   GHashTable *icons;
1745   GList *list, *l;
1746   GQuark context_quark;
1747   
1748   priv = icon_theme->priv;
1749   
1750   ensure_valid_themes (icon_theme);
1751
1752   if (context)
1753     {
1754       context_quark = g_quark_try_string (context);
1755
1756       if (!context_quark)
1757         return NULL;
1758     }
1759   else
1760     context_quark = 0;
1761
1762   icons = g_hash_table_new (g_str_hash, g_str_equal);
1763   
1764   l = priv->themes;
1765   while (l != NULL)
1766     {
1767       theme_list_icons (l->data, icons, context_quark);
1768       l = l->next;
1769     }
1770
1771   if (context_quark == 0)
1772     g_hash_table_foreach (priv->unthemed_icons,
1773                           add_key_to_hash,
1774                           icons);
1775
1776   list = NULL;
1777   
1778   g_hash_table_foreach (icons,
1779                         add_key_to_list,
1780                         &list);
1781
1782   g_hash_table_destroy (icons);
1783   
1784   return list;
1785 }
1786
1787 /**
1788  * gtk_icon_theme_list_contexts:
1789  * @icon_theme: a #GtkIconTheme
1790  *
1791  * Gets the list of contexts available within the current
1792  * hierarchy of icon themes
1793  *
1794  * Return value: (element-type utf8) (transfer full): a #GList list holding the names of all the
1795  *  contexts in the theme. You must first free each element
1796  *  in the list with g_free(), then free the list itself
1797  *  with g_list_free().
1798  *
1799  * Since: 2.12
1800  **/
1801 GList *
1802 gtk_icon_theme_list_contexts (GtkIconTheme *icon_theme)
1803 {
1804   GtkIconThemePrivate *priv;
1805   GHashTable *contexts;
1806   GList *list, *l;
1807
1808   priv = icon_theme->priv;
1809   
1810   ensure_valid_themes (icon_theme);
1811
1812   contexts = g_hash_table_new (g_str_hash, g_str_equal);
1813
1814   l = priv->themes;
1815   while (l != NULL)
1816     {
1817       theme_list_contexts (l->data, contexts);
1818       l = l->next;
1819     }
1820
1821   list = NULL;
1822
1823   g_hash_table_foreach (contexts,
1824                         add_key_to_list,
1825                         &list);
1826
1827   g_hash_table_destroy (contexts);
1828
1829   return list;
1830 }
1831
1832 /**
1833  * gtk_icon_theme_get_example_icon_name:
1834  * @icon_theme: a #GtkIconTheme
1835  * 
1836  * Gets the name of an icon that is representative of the
1837  * current theme (for instance, to use when presenting
1838  * a list of themes to the user.)
1839  * 
1840  * Return value: the name of an example icon or %NULL.
1841  *  Free with g_free().
1842  *
1843  * Since: 2.4
1844  **/
1845 char *
1846 gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
1847 {
1848   GtkIconThemePrivate *priv;
1849   GList *l;
1850   IconTheme *theme;
1851
1852   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1853   
1854   priv = icon_theme->priv;
1855   
1856   ensure_valid_themes (icon_theme);
1857
1858   l = priv->themes;
1859   while (l != NULL)
1860     {
1861       theme = l->data;
1862       if (theme->example)
1863         return g_strdup (theme->example);
1864       
1865       l = l->next;
1866     }
1867   
1868   return NULL;
1869 }
1870
1871
1872 static gboolean
1873 rescan_themes (GtkIconTheme *icon_theme)
1874 {
1875   GtkIconThemePrivate *priv;
1876   IconThemeDirMtime *dir_mtime;
1877   GList *d;
1878   int stat_res;
1879   struct stat stat_buf;
1880   GTimeVal tv;
1881
1882   priv = icon_theme->priv;
1883
1884   for (d = priv->dir_mtimes; d != NULL; d = d->next)
1885     {
1886       dir_mtime = d->data;
1887
1888       stat_res = g_stat (dir_mtime->dir, &stat_buf);
1889
1890       /* dir mtime didn't change */
1891       if (stat_res == 0 &&
1892           S_ISDIR (stat_buf.st_mode) &&
1893           dir_mtime->mtime == stat_buf.st_mtime)
1894         continue;
1895       /* didn't exist before, and still doesn't */
1896       if (dir_mtime->mtime == 0 &&
1897           (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
1898         continue;
1899
1900       return TRUE;
1901     }
1902
1903   g_get_current_time (&tv);
1904   priv->last_stat_time = tv.tv_sec;
1905
1906   return FALSE;
1907 }
1908
1909 /**
1910  * gtk_icon_theme_rescan_if_needed:
1911  * @icon_theme: a #GtkIconTheme
1912  * 
1913  * Checks to see if the icon theme has changed; if it has, any
1914  * currently cached information is discarded and will be reloaded
1915  * next time @icon_theme is accessed.
1916  * 
1917  * Return value: %TRUE if the icon theme has changed and needed
1918  *   to be reloaded.
1919  *
1920  * Since: 2.4
1921  **/
1922 gboolean
1923 gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
1924 {
1925   gboolean retval;
1926
1927   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1928
1929   retval = rescan_themes (icon_theme);
1930   if (retval)
1931       do_theme_change (icon_theme);
1932
1933   return retval;
1934 }
1935
1936 static void
1937 theme_destroy (IconTheme *theme)
1938 {
1939   g_free (theme->display_name);
1940   g_free (theme->comment);
1941   g_free (theme->name);
1942   g_free (theme->example);
1943
1944   g_list_foreach (theme->dirs, (GFunc)theme_dir_destroy, NULL);
1945   g_list_free (theme->dirs);
1946   
1947   g_free (theme);
1948 }
1949
1950 static void
1951 theme_dir_destroy (IconThemeDir *dir)
1952 {
1953   if (dir->cache)
1954       _gtk_icon_cache_unref (dir->cache);
1955   else
1956     g_hash_table_destroy (dir->icons);
1957   
1958   if (dir->icon_data)
1959     g_hash_table_destroy (dir->icon_data);
1960   g_free (dir->dir);
1961   g_free (dir->subdir);
1962   g_free (dir);
1963 }
1964
1965 static int
1966 theme_dir_size_difference (IconThemeDir *dir, int size, gboolean *smaller)
1967 {
1968   int min, max;
1969   switch (dir->type)
1970     {
1971     case ICON_THEME_DIR_FIXED:
1972       *smaller = size < dir->size;
1973       return abs (size - dir->size);
1974       break;
1975     case ICON_THEME_DIR_SCALABLE:
1976       *smaller = size < dir->min_size;
1977       if (size < dir->min_size)
1978         return dir->min_size - size;
1979       if (size > dir->max_size)
1980         return size - dir->max_size;
1981       return 0;
1982       break;
1983     case ICON_THEME_DIR_THRESHOLD:
1984       min = dir->size - dir->threshold;
1985       max = dir->size + dir->threshold;
1986       *smaller = size < min;
1987       if (size < min)
1988         return min - size;
1989       if (size > max)
1990         return size - max;
1991       return 0;
1992       break;
1993     case ICON_THEME_DIR_UNTHEMED:
1994       g_assert_not_reached ();
1995       break;
1996     }
1997   g_assert_not_reached ();
1998   return 1000;
1999 }
2000
2001 static const char *
2002 string_from_suffix (IconSuffix suffix)
2003 {
2004   switch (suffix)
2005     {
2006     case ICON_SUFFIX_XPM:
2007       return ".xpm";
2008     case ICON_SUFFIX_SVG:
2009       return ".svg";
2010     case ICON_SUFFIX_PNG:
2011       return ".png";
2012     default:
2013       g_assert_not_reached();
2014     }
2015   return NULL;
2016 }
2017
2018 static IconSuffix
2019 suffix_from_name (const char *name)
2020 {
2021   IconSuffix retval;
2022
2023   if (g_str_has_suffix (name, ".png"))
2024     retval = ICON_SUFFIX_PNG;
2025   else if (g_str_has_suffix (name, ".svg"))
2026     retval = ICON_SUFFIX_SVG;
2027   else if (g_str_has_suffix (name, ".xpm"))
2028     retval = ICON_SUFFIX_XPM;
2029   else
2030     retval = ICON_SUFFIX_NONE;
2031
2032   return retval;
2033 }
2034
2035 static IconSuffix
2036 best_suffix (IconSuffix suffix,
2037              gboolean   allow_svg)
2038 {
2039   if ((suffix & ICON_SUFFIX_PNG) != 0)
2040     return ICON_SUFFIX_PNG;
2041   else if (allow_svg && ((suffix & ICON_SUFFIX_SVG) != 0))
2042     return ICON_SUFFIX_SVG;
2043   else if ((suffix & ICON_SUFFIX_XPM) != 0)
2044     return ICON_SUFFIX_XPM;
2045   else
2046     return ICON_SUFFIX_NONE;
2047 }
2048
2049
2050 static IconSuffix
2051 theme_dir_get_icon_suffix (IconThemeDir *dir,
2052                            const gchar  *icon_name,
2053                            gboolean     *has_icon_file)
2054 {
2055   IconSuffix suffix;
2056
2057   if (dir->cache)
2058     {
2059       suffix = (IconSuffix)_gtk_icon_cache_get_icon_flags (dir->cache,
2060                                                            icon_name,
2061                                                            dir->subdir_index);
2062
2063       if (has_icon_file)
2064         *has_icon_file = suffix & HAS_ICON_FILE;
2065
2066       suffix = suffix & ~HAS_ICON_FILE;
2067     }
2068   else
2069     suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
2070
2071   GTK_NOTE (ICONTHEME, 
2072             g_print ("get_icon_suffix%s %u\n", dir->cache ? " (cached)" : "", suffix));
2073
2074   return suffix;
2075 }
2076
2077 static GtkIconInfo *
2078 theme_lookup_icon (IconTheme          *theme,
2079                    const char         *icon_name,
2080                    int                 size,
2081                    gboolean            allow_svg,
2082                    gboolean            use_builtin)
2083 {
2084   GList *dirs, *l;
2085   IconThemeDir *dir, *min_dir;
2086   char *file;
2087   int min_difference, difference;
2088   BuiltinIcon *closest_builtin = NULL;
2089   gboolean smaller, has_larger, match;
2090   IconSuffix suffix;
2091
2092   min_difference = G_MAXINT;
2093   min_dir = NULL;
2094   has_larger = FALSE;
2095   match = FALSE;
2096
2097   /* Builtin icons are logically part of the default theme and
2098    * are searched before other subdirectories of the default theme.
2099    */
2100   if (use_builtin && strcmp (theme->name, DEFAULT_THEME_NAME) == 0)
2101     {
2102       closest_builtin = find_builtin_icon (icon_name, 
2103                                            size,
2104                                            &min_difference,
2105                                            &has_larger);
2106
2107       if (min_difference == 0)
2108         return icon_info_new_builtin (closest_builtin);
2109
2110       dirs = builtin_dirs;
2111     }
2112   else
2113     dirs = theme->dirs;
2114
2115   l = dirs;
2116   while (l != NULL)
2117     {
2118       dir = l->data;
2119
2120       GTK_NOTE (ICONTHEME,
2121                 g_print ("theme_lookup_icon dir %s\n", dir->dir));
2122       suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
2123       if (best_suffix (suffix, allow_svg) != ICON_SUFFIX_NONE)
2124         {
2125           difference = theme_dir_size_difference (dir, size, &smaller);
2126
2127           if (difference == 0)
2128             {
2129               if (dir->type == ICON_THEME_DIR_SCALABLE)
2130                 {
2131                   /* don't pick scalable if we already found
2132                    * a matching non-scalable dir
2133                    */
2134                   if (!match)
2135                     {
2136                       min_dir = dir;
2137                       break;
2138                     }
2139                 }
2140               else
2141                 {
2142                   /* for a matching non-scalable dir keep
2143                    * going and look for a closer match
2144                    */             
2145                   difference = abs (size - dir->size);
2146                   if (!match || difference < min_difference)
2147                     {
2148                       match = TRUE;
2149                       min_difference = difference;
2150                       min_dir = dir;
2151                     }
2152                   if (difference == 0)
2153                     break;
2154                 }
2155             } 
2156   
2157           if (!match)
2158             {
2159               if (!has_larger)
2160                 {
2161                   if (difference < min_difference || smaller)
2162                     {
2163                       min_difference = difference;
2164                       min_dir = dir;
2165                       has_larger = smaller;
2166                     }
2167                 }
2168               else
2169                 {
2170                   if (difference < min_difference && smaller)
2171                     {
2172                       min_difference = difference;
2173                       min_dir = dir;
2174                     }
2175                 }
2176             }
2177         }
2178
2179       l = l->next;
2180
2181       if (l == NULL && dirs == builtin_dirs)
2182         {
2183           dirs = theme->dirs;
2184           l = dirs;
2185         }
2186     }
2187
2188   if (min_dir)
2189     {
2190       GtkIconInfo *icon_info = icon_info_new ();
2191       gboolean has_icon_file = FALSE;
2192       
2193       suffix = theme_dir_get_icon_suffix (min_dir, icon_name, &has_icon_file);
2194       suffix = best_suffix (suffix, allow_svg);
2195       g_assert (suffix != ICON_SUFFIX_NONE);
2196       
2197       if (min_dir->dir)
2198         {
2199           file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
2200           icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
2201           g_free (file);
2202         }
2203       else
2204         {
2205           icon_info->filename = NULL;
2206         }
2207       
2208       if (min_dir->icon_data != NULL)
2209         icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2210
2211       if (icon_info->data == NULL && min_dir->cache != NULL)
2212         {
2213           icon_info->data = _gtk_icon_cache_get_icon_data (min_dir->cache, icon_name, min_dir->subdir_index);
2214           if (icon_info->data)
2215             {
2216               if (min_dir->icon_data == NULL)
2217                 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2218                                                             g_free, (GDestroyNotify)icon_data_free);
2219
2220               g_hash_table_replace (min_dir->icon_data, g_strdup (icon_name), icon_info->data);
2221             }
2222         }
2223
2224       if (icon_info->data == NULL && has_icon_file)
2225         {
2226           gchar *icon_file_name, *icon_file_path;
2227
2228           icon_file_name = g_strconcat (icon_name, ".icon", NULL);
2229           icon_file_path = g_build_filename (min_dir->dir, icon_file_name, NULL);
2230
2231           if (g_file_test (icon_file_path, G_FILE_TEST_IS_REGULAR))
2232             {
2233               if (min_dir->icon_data == NULL)   
2234                 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2235                                                             g_free, (GDestroyNotify)icon_data_free);
2236               load_icon_data (min_dir, icon_file_path, icon_file_name);
2237               
2238               icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2239             }
2240           g_free (icon_file_name);
2241           g_free (icon_file_path);
2242         }
2243
2244       if (min_dir->cache)
2245         {
2246           icon_info->cache_pixbuf = _gtk_icon_cache_get_icon (min_dir->cache, icon_name,
2247                                                               min_dir->subdir_index);
2248         }
2249
2250       icon_info->dir_type = min_dir->type;
2251       icon_info->dir_size = min_dir->size;
2252       icon_info->threshold = min_dir->threshold;
2253       
2254       return icon_info;
2255     }
2256
2257   if (closest_builtin)
2258     return icon_info_new_builtin (closest_builtin);
2259   
2260   return NULL;
2261 }
2262
2263 static void
2264 theme_list_icons (IconTheme  *theme, 
2265                   GHashTable *icons,
2266                   GQuark      context)
2267 {
2268   GList *l = theme->dirs;
2269   IconThemeDir *dir;
2270   
2271   while (l != NULL)
2272     {
2273       dir = l->data;
2274
2275       if (context == dir->context ||
2276           context == 0)
2277         {
2278           if (dir->cache)
2279             {
2280               _gtk_icon_cache_add_icons (dir->cache,
2281                                          dir->subdir,
2282                                          icons);
2283                                          
2284             }
2285           else
2286             {
2287               g_hash_table_foreach (dir->icons,
2288                                     add_key_to_hash,
2289                                     icons);
2290             }
2291         }
2292       l = l->next;
2293     }
2294 }
2295
2296 static void
2297 theme_list_contexts (IconTheme  *theme, 
2298                      GHashTable *contexts)
2299 {
2300   GList *l = theme->dirs;
2301   IconThemeDir *dir;
2302   const char *context;
2303
2304   while (l != NULL)
2305     {
2306       dir = l->data;
2307
2308       context = g_quark_to_string (dir->context);
2309       g_hash_table_replace (contexts, (gpointer) context, NULL);
2310
2311       l = l->next;
2312     }
2313 }
2314
2315 static void
2316 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
2317 {
2318   GKeyFile *icon_file;
2319   char *base_name;
2320   char **split;
2321   gsize length;
2322   char *str;
2323   char *split_point;
2324   int i;
2325   gint *ivalues;
2326   GError *error = NULL;
2327   
2328   GtkIconData *data;
2329
2330   icon_file = g_key_file_new ();
2331   g_key_file_set_list_separator (icon_file, ',');
2332   g_key_file_load_from_file (icon_file, path, 0, &error);
2333   if (error)
2334     {
2335       g_error_free (error);
2336       g_key_file_free (icon_file);      
2337       return;
2338     }
2339   else
2340     {
2341       base_name = strip_suffix (name);
2342       
2343       data = g_slice_new0 (GtkIconData);
2344       g_hash_table_replace (dir->icon_data, base_name, data);
2345       
2346       ivalues = g_key_file_get_integer_list (icon_file, 
2347                                              "Icon Data", "EmbeddedTextRectangle",
2348                                               &length, NULL);
2349       if (ivalues)
2350         {
2351           if (length == 4)
2352             {
2353               data->has_embedded_rect = TRUE;
2354               data->x0 = ivalues[0];
2355               data->y0 = ivalues[1];
2356               data->x1 = ivalues[2];
2357               data->y1 = ivalues[3];
2358             }
2359           
2360           g_free (ivalues);
2361         }
2362       
2363       str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
2364       if (str)
2365         {
2366           split = g_strsplit (str, "|", -1);
2367           
2368           data->n_attach_points = g_strv_length (split);
2369           data->attach_points = g_new (GdkPoint, data->n_attach_points);
2370
2371           i = 0;
2372           while (split[i] != NULL && i < data->n_attach_points)
2373             {
2374               split_point = strchr (split[i], ',');
2375               if (split_point)
2376                 {
2377                   *split_point = 0;
2378                   split_point++;
2379                   data->attach_points[i].x = atoi (split[i]);
2380                   data->attach_points[i].y = atoi (split_point);
2381                 }
2382               i++;
2383             }
2384           
2385           g_strfreev (split);
2386           g_free (str);
2387         }
2388       
2389       data->display_name = g_key_file_get_locale_string (icon_file, 
2390                                                          "Icon Data", "DisplayName",
2391                                                          NULL, NULL);
2392       g_key_file_free (icon_file);
2393     }
2394 }
2395
2396 static void
2397 scan_directory (GtkIconThemePrivate *icon_theme,
2398                 IconThemeDir *dir, char *full_dir)
2399 {
2400   GDir *gdir;
2401   const char *name;
2402
2403   GTK_NOTE (ICONTHEME, 
2404             g_print ("scanning directory %s\n", full_dir));
2405   dir->icons = g_hash_table_new_full (g_str_hash, g_str_equal,
2406                                       g_free, NULL);
2407   
2408   gdir = g_dir_open (full_dir, 0, NULL);
2409
2410   if (gdir == NULL)
2411     return;
2412
2413   while ((name = g_dir_read_name (gdir)))
2414     {
2415       char *path;
2416       char *base_name;
2417       IconSuffix suffix, hash_suffix;
2418
2419       if (g_str_has_suffix (name, ".icon"))
2420         {
2421           if (dir->icon_data == NULL)
2422             dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2423                                                     g_free, (GDestroyNotify)icon_data_free);
2424           
2425           path = g_build_filename (full_dir, name, NULL);
2426           if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
2427             load_icon_data (dir, path, name);
2428           
2429           g_free (path);
2430           
2431           continue;
2432         }
2433
2434       suffix = suffix_from_name (name);
2435       if (suffix == ICON_SUFFIX_NONE)
2436         continue;
2437
2438       base_name = strip_suffix (name);
2439
2440       hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
2441       g_hash_table_replace (icon_theme->all_icons, base_name, NULL);
2442       g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
2443     }
2444   
2445   g_dir_close (gdir);
2446 }
2447
2448 static void
2449 theme_subdir_load (GtkIconTheme *icon_theme,
2450                    IconTheme    *theme,
2451                    GKeyFile     *theme_file,
2452                    char         *subdir)
2453 {
2454   GList *d;
2455   char *type_string;
2456   IconThemeDir *dir;
2457   IconThemeDirType type;
2458   char *context_string;
2459   GQuark context;
2460   int size;
2461   int min_size;
2462   int max_size;
2463   int threshold;
2464   char *full_dir;
2465   GError *error = NULL;
2466   IconThemeDirMtime *dir_mtime;
2467
2468   size = g_key_file_get_integer (theme_file, subdir, "Size", &error);
2469   if (error)
2470     {
2471       g_error_free (error);
2472       g_warning ("Theme directory %s of theme %s has no size field\n", 
2473                  subdir, theme->name);
2474       return;
2475     }
2476   
2477   type = ICON_THEME_DIR_THRESHOLD;
2478   type_string = g_key_file_get_string (theme_file, subdir, "Type", NULL);
2479   if (type_string)
2480     {
2481       if (strcmp (type_string, "Fixed") == 0)
2482         type = ICON_THEME_DIR_FIXED;
2483       else if (strcmp (type_string, "Scalable") == 0)
2484         type = ICON_THEME_DIR_SCALABLE;
2485       else if (strcmp (type_string, "Threshold") == 0)
2486         type = ICON_THEME_DIR_THRESHOLD;
2487
2488       g_free (type_string);
2489     }
2490   
2491   context = 0;
2492   context_string = g_key_file_get_string (theme_file, subdir, "Context", NULL);
2493   if (context_string)
2494     {
2495       context = g_quark_from_string (context_string);
2496       g_free (context_string);
2497     }
2498
2499   max_size = g_key_file_get_integer (theme_file, subdir, "MaxSize", &error);
2500   if (error)
2501     {
2502       max_size = size;
2503
2504       g_error_free (error);
2505       error = NULL;
2506     }
2507
2508   min_size = g_key_file_get_integer (theme_file, subdir, "MinSize", &error);
2509   if (error)
2510     {
2511       min_size = size;
2512
2513       g_error_free (error);
2514       error = NULL;
2515     }
2516   
2517   threshold = g_key_file_get_integer (theme_file, subdir, "Threshold", &error);
2518   if (error)
2519     {
2520       threshold = 2;
2521
2522       g_error_free (error);
2523       error = NULL;
2524     }
2525
2526   for (d = icon_theme->priv->dir_mtimes; d; d = d->next)
2527     {
2528       dir_mtime = (IconThemeDirMtime *)d->data;
2529
2530       if (dir_mtime->mtime == 0)
2531         continue; /* directory doesn't exist */
2532
2533        full_dir = g_build_filename (dir_mtime->dir, subdir, NULL);
2534
2535       /* First, see if we have a cache for the directory */
2536       if (dir_mtime->cache != NULL || g_file_test (full_dir, G_FILE_TEST_IS_DIR))
2537         {
2538           if (dir_mtime->cache == NULL)
2539             {
2540               /* This will return NULL if the cache doesn't exist or is outdated */
2541               dir_mtime->cache = _gtk_icon_cache_new_for_path (dir_mtime->dir);
2542             }
2543           
2544           dir = g_new (IconThemeDir, 1);
2545           dir->type = type;
2546           dir->context = context;
2547           dir->size = size;
2548           dir->min_size = min_size;
2549           dir->max_size = max_size;
2550           dir->threshold = threshold;
2551           dir->dir = full_dir;
2552           dir->icon_data = NULL;
2553           dir->subdir = g_strdup (subdir);
2554           if (dir_mtime->cache != NULL)
2555             {
2556               dir->cache = _gtk_icon_cache_ref (dir_mtime->cache);
2557               dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
2558             }
2559           else
2560             {
2561               dir->cache = NULL;
2562               dir->subdir_index = -1;
2563               scan_directory (icon_theme->priv, dir, full_dir);
2564             }
2565
2566           theme->dirs = g_list_prepend (theme->dirs, dir);
2567         }
2568       else
2569         g_free (full_dir);
2570     }
2571 }
2572
2573 static void
2574 icon_data_free (GtkIconData *icon_data)
2575 {
2576   g_free (icon_data->attach_points);
2577   g_free (icon_data->display_name);
2578   g_slice_free (GtkIconData, icon_data);
2579 }
2580
2581 /*
2582  * GtkIconInfo
2583  */
2584
2585 G_DEFINE_BOXED_TYPE (GtkIconInfo, gtk_icon_info,
2586                      gtk_icon_info_copy,
2587                      gtk_icon_info_free)
2588
2589 static GtkIconInfo *
2590 icon_info_new (void)
2591 {
2592   GtkIconInfo *icon_info = g_slice_new0 (GtkIconInfo);
2593
2594   icon_info->scale = -1.;
2595   icon_info->ref_count = 1;
2596
2597   return icon_info;
2598 }
2599
2600 static GtkIconInfo *
2601 icon_info_new_builtin (BuiltinIcon *icon)
2602 {
2603   GtkIconInfo *icon_info = icon_info_new ();
2604
2605   icon_info->cache_pixbuf = g_object_ref (icon->pixbuf);
2606   icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
2607   icon_info->dir_size = icon->size;
2608   icon_info->threshold = 2;
2609
2610   return icon_info;
2611 }
2612
2613 /**
2614  * gtk_icon_info_copy:
2615  * @icon_info: a #GtkIconInfo
2616  * 
2617  * Make a copy of a #GtkIconInfo.
2618  * 
2619  * Return value: the new GtkIconInfo
2620  *
2621  * Since: 2.4
2622  **/
2623 GtkIconInfo *
2624 gtk_icon_info_copy (GtkIconInfo *icon_info)
2625 {
2626   
2627   g_return_val_if_fail (icon_info != NULL, NULL);
2628
2629   icon_info->ref_count++;
2630
2631   return icon_info;
2632 }
2633
2634 /**
2635  * gtk_icon_info_free:
2636  * @icon_info: a #GtkIconInfo
2637  * 
2638  * Free a #GtkIconInfo and associated information
2639  *
2640  * Since: 2.4
2641  **/
2642 void
2643 gtk_icon_info_free (GtkIconInfo *icon_info)
2644 {
2645   g_return_if_fail (icon_info != NULL);
2646
2647   icon_info->ref_count--;
2648   if (icon_info->ref_count > 0)
2649     return;
2650  
2651   g_free (icon_info->filename);
2652   if (icon_info->loadable)
2653     g_object_unref (icon_info->loadable);
2654   g_slist_foreach (icon_info->emblem_infos, (GFunc)gtk_icon_info_free, NULL);
2655   g_slist_free (icon_info->emblem_infos);
2656   if (icon_info->pixbuf)
2657     g_object_unref (icon_info->pixbuf);
2658   if (icon_info->cache_pixbuf)
2659     g_object_unref (icon_info->cache_pixbuf);
2660
2661   g_slice_free (GtkIconInfo, icon_info);
2662 }
2663
2664 /**
2665  * gtk_icon_info_get_base_size:
2666  * @icon_info: a #GtkIconInfo
2667  * 
2668  * Gets the base size for the icon. The base size
2669  * is a size for the icon that was specified by
2670  * the icon theme creator. This may be different
2671  * than the actual size of image; an example of
2672  * this is small emblem icons that can be attached
2673  * to a larger icon. These icons will be given
2674  * the same base size as the larger icons to which
2675  * they are attached.
2676  * 
2677  * Return value: the base size, or 0, if no base
2678  *  size is known for the icon.
2679  *
2680  * Since: 2.4
2681  **/
2682 gint
2683 gtk_icon_info_get_base_size (GtkIconInfo *icon_info)
2684 {
2685   g_return_val_if_fail (icon_info != NULL, 0);
2686
2687   return icon_info->dir_size;
2688 }
2689
2690 /**
2691  * gtk_icon_info_get_filename:
2692  * @icon_info: a #GtkIconInfo
2693  * 
2694  * Gets the filename for the icon. If the
2695  * %GTK_ICON_LOOKUP_USE_BUILTIN flag was passed
2696  * to gtk_icon_theme_lookup_icon(), there may be
2697  * no filename if a builtin icon is returned; in this
2698  * case, you should use gtk_icon_info_get_builtin_pixbuf().
2699  * 
2700  * Return value: the filename for the icon, or %NULL
2701  *  if gtk_icon_info_get_builtin_pixbuf() should
2702  *  be used instead. The return value is owned by
2703  *  GTK+ and should not be modified or freed.
2704  *
2705  * Since: 2.4
2706  **/
2707 G_CONST_RETURN gchar *
2708 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2709 {
2710   g_return_val_if_fail (icon_info != NULL, NULL);
2711
2712   return icon_info->filename;
2713 }
2714
2715 /**
2716  * gtk_icon_info_get_builtin_pixbuf:
2717  * @icon_info: a #GtkIconInfo structure
2718  * 
2719  * Gets the built-in image for this icon, if any. To allow
2720  * GTK+ to use built in icon images, you must pass the
2721  * %GTK_ICON_LOOKUP_USE_BUILTIN to
2722  * gtk_icon_theme_lookup_icon().
2723  *
2724  * Return value: (transfer none): the built-in image pixbuf, or %NULL. No
2725  *  extra reference is added to the returned pixbuf, so if
2726  *  you want to keep it around, you must use g_object_ref().
2727  *  The returned image must not be modified.
2728  *
2729  * Since: 2.4
2730  **/
2731 GdkPixbuf *
2732 gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
2733 {
2734   g_return_val_if_fail (icon_info != NULL, NULL);
2735
2736   if (icon_info->filename)
2737     return NULL;
2738   
2739   return icon_info->cache_pixbuf;
2740 }
2741
2742 static gboolean icon_info_ensure_scale_and_pixbuf (GtkIconInfo*, gboolean);
2743
2744 /* Combine the icon with all emblems, the first emblem is placed 
2745  * in the southeast corner. Scale emblems to be at most 3/4 of the
2746  * size of the icon itself.
2747  */
2748 static void 
2749 apply_emblems (GtkIconInfo *info)
2750 {
2751   GdkPixbuf *icon = NULL;
2752   gint w, h, pos;
2753   GSList *l;
2754
2755   if (info->emblem_infos == NULL)
2756     return;
2757
2758   if (info->emblems_applied)
2759     return;
2760
2761   w = gdk_pixbuf_get_width (info->pixbuf);
2762   h = gdk_pixbuf_get_height (info->pixbuf);
2763
2764   for (l = info->emblem_infos, pos = 0; l; l = l->next, pos++)
2765     {
2766       GtkIconInfo *emblem_info = l->data;
2767
2768       if (icon_info_ensure_scale_and_pixbuf (emblem_info, FALSE))
2769         {
2770           GdkPixbuf *emblem = emblem_info->pixbuf;
2771           gint ew, eh;
2772           gint x = 0, y = 0; /* silence compiler */
2773           gdouble scale;
2774
2775           ew = gdk_pixbuf_get_width (emblem);
2776           eh = gdk_pixbuf_get_height (emblem);
2777           if (ew >= w)
2778             {
2779               scale = 0.75;
2780               ew = ew * 0.75;
2781               eh = eh * 0.75;
2782             }
2783           else
2784             scale = 1.0;
2785
2786           switch (pos % 4)
2787             {
2788             case 0:
2789               x = w - ew;
2790               y = h - eh;
2791               break;
2792             case 1:
2793               x = w - ew;
2794               y = 0;
2795               break;
2796             case 2:
2797               x = 0;
2798               y = h - eh;
2799               break;
2800             case 3:
2801               x = 0;
2802               y = 0;
2803               break;
2804             }
2805
2806           if (icon == NULL)
2807             {
2808               icon = gdk_pixbuf_copy (info->pixbuf);
2809               if (icon == NULL)
2810                 break;
2811             }
2812
2813           gdk_pixbuf_composite (emblem, icon, x, y, ew, eh, x, y,
2814                                 scale, scale, GDK_INTERP_BILINEAR, 255);
2815        }
2816    }
2817
2818   if (icon)
2819     {
2820       g_object_unref (info->pixbuf);
2821       info->pixbuf = icon;
2822     }
2823
2824   info->emblems_applied = TRUE;
2825 }
2826
2827 /* This function contains the complicated logic for deciding
2828  * on the size at which to load the icon and loading it at
2829  * that size.
2830  */
2831 static gboolean
2832 icon_info_ensure_scale_and_pixbuf (GtkIconInfo  *icon_info,
2833                                    gboolean      scale_only)
2834 {
2835   int image_width, image_height;
2836   GdkPixbuf *source_pixbuf;
2837   gboolean is_svg;
2838
2839   /* First check if we already succeeded have the necessary
2840    * information (or failed earlier)
2841    */
2842   if (scale_only && icon_info->scale >= 0)
2843     return TRUE;
2844
2845   if (icon_info->pixbuf)
2846     {
2847       apply_emblems (icon_info);
2848       return TRUE;
2849     }
2850
2851   if (icon_info->load_error)
2852     return FALSE;
2853
2854   /* SVG icons are a special case - we just immediately scale them
2855    * to the desired size
2856    */
2857   if (icon_info->filename && !icon_info->loadable) 
2858     {
2859       GFile *file;
2860
2861       file = g_file_new_for_path (icon_info->filename);
2862       icon_info->loadable = G_LOADABLE_ICON (g_file_icon_new (file));
2863       g_object_unref (file);
2864     }
2865
2866   is_svg = FALSE;
2867   if (G_IS_FILE_ICON (icon_info->loadable))
2868     {
2869       GFile *file;
2870       GFileInfo *file_info;
2871       const gchar *content_type;
2872
2873       file = g_file_icon_get_file (G_FILE_ICON (icon_info->loadable));
2874       file_info = g_file_query_info (file, 
2875                                      G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
2876                                      G_FILE_QUERY_INFO_NONE,
2877                                      NULL, NULL);
2878       if (file_info) 
2879         {
2880           content_type = g_file_info_get_content_type (file_info);
2881
2882           if (content_type && strcmp (content_type, "image/svg+xml") == 0)
2883             is_svg = TRUE;
2884
2885           g_object_unref (file_info);
2886        }
2887     }
2888
2889   if (is_svg)
2890     {
2891       GInputStream *stream;
2892
2893       icon_info->scale = icon_info->desired_size / 1000.;
2894
2895       if (scale_only)
2896         return TRUE;
2897       
2898       stream = g_loadable_icon_load (icon_info->loadable,
2899                                      icon_info->desired_size,
2900                                      NULL, NULL,
2901                                      &icon_info->load_error);
2902       if (stream)
2903         {
2904           icon_info->pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
2905                                                                    icon_info->desired_size,
2906                                                                    icon_info->desired_size,
2907                                                                    TRUE,
2908                                                                    NULL,
2909                                                                    &icon_info->load_error);
2910           g_object_unref (stream);
2911         }
2912
2913       if (!icon_info->pixbuf)
2914         return FALSE;
2915
2916       apply_emblems (icon_info);
2917         
2918       return TRUE;
2919     }
2920
2921   /* In many cases, the scale can be determined without actual access
2922    * to the icon file. This is generally true when we have a size
2923    * for the directory where the icon is; the image size doesn't
2924    * matter in that case.
2925    */
2926   if (icon_info->forced_size)
2927     icon_info->scale = -1;
2928   else if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
2929     icon_info->scale = 1.0;
2930   else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
2931     {
2932       if (icon_info->desired_size >= icon_info->dir_size - icon_info->threshold &&
2933           icon_info->desired_size <= icon_info->dir_size + icon_info->threshold)
2934         icon_info->scale = 1.0;
2935       else if (icon_info->dir_size > 0)
2936         icon_info->scale =(gdouble) icon_info->desired_size / icon_info->dir_size;
2937     }
2938   else if (icon_info->dir_type == ICON_THEME_DIR_SCALABLE)
2939     {
2940       if (icon_info->dir_size > 0)
2941         icon_info->scale = (gdouble) icon_info->desired_size / icon_info->dir_size;
2942     }
2943
2944   if (icon_info->scale >= 0. && scale_only)
2945     return TRUE;
2946
2947   /* At this point, we need to actually get the icon; either from the
2948    * builtin image or by loading the file
2949    */
2950   source_pixbuf = NULL;
2951   if (icon_info->cache_pixbuf)
2952     source_pixbuf = g_object_ref (icon_info->cache_pixbuf);
2953   else
2954     {
2955       GInputStream *stream;
2956
2957       stream = g_loadable_icon_load (icon_info->loadable,
2958                                      icon_info->desired_size,
2959                                      NULL, NULL,
2960                                      &icon_info->load_error);
2961       if (stream)
2962         {
2963           source_pixbuf = gdk_pixbuf_new_from_stream (stream,
2964                                                       NULL,
2965                                                       &icon_info->load_error);
2966           g_object_unref (stream);
2967         }
2968     }
2969
2970   if (!source_pixbuf)
2971     return FALSE;
2972
2973   /* Do scale calculations that depend on the image size
2974    */
2975   image_width = gdk_pixbuf_get_width (source_pixbuf);
2976   image_height = gdk_pixbuf_get_height (source_pixbuf);
2977
2978   if (icon_info->scale < 0.0)
2979     {
2980       gint image_size = MAX (image_width, image_height);
2981       if (image_size > 0)
2982         icon_info->scale = (gdouble)icon_info->desired_size / (gdouble)image_size;
2983       else
2984         icon_info->scale = 1.0;
2985       
2986       if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED && 
2987           !icon_info->forced_size)
2988         icon_info->scale = MIN (icon_info->scale, 1.0);
2989     }
2990
2991   /* We don't short-circuit out here for scale_only, since, now
2992    * we've loaded the icon, we might as well go ahead and finish
2993    * the job. This is a bit of a waste when we scale here
2994    * and never get the final pixbuf; at the cost of a bit of
2995    * extra complexity, we could keep the source pixbuf around
2996    * but not actually scale it until needed.
2997    */
2998   if (icon_info->scale == 1.0)
2999     icon_info->pixbuf = source_pixbuf;
3000   else
3001     {
3002       icon_info->pixbuf = gdk_pixbuf_scale_simple (source_pixbuf,
3003                                                    0.5 + image_width * icon_info->scale,
3004                                                    0.5 + image_height * icon_info->scale,
3005                                                    GDK_INTERP_BILINEAR);
3006       g_object_unref (source_pixbuf);
3007     }
3008
3009   apply_emblems (icon_info);
3010
3011   return TRUE;
3012 }
3013
3014 /**
3015  * gtk_icon_info_load_icon:
3016  * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
3017  * @error: (allow-none): location to store error information on failure,
3018  *     or %NULL.
3019  *
3020  * Renders an icon previously looked up in an icon theme using
3021  * gtk_icon_theme_lookup_icon(); the size will be based on the size
3022  * passed to gtk_icon_theme_lookup_icon(). Note that the resulting
3023  * pixbuf may not be exactly this size; an icon theme may have icons
3024  * that differ slightly from their nominal sizes, and in addition GTK+
3025  * will avoid scaling icons that it considers sufficiently close to the
3026  * requested size or for which the source image would have to be scaled
3027  * up too far. (This maintains sharpness.). This behaviour can be changed
3028  * by passing the %GTK_ICON_LOOKUP_FORCE_SIZE flag when obtaining
3029  * the #GtkIconInfo. If this flag has been specified, the pixbuf
3030  * returned by this function will be scaled to the exact size.
3031  *
3032  * Return value: (transfer full): the rendered icon; this may be a newly
3033  *     created icon or a new reference to an internal icon, so you must
3034  *     not modify the icon. Use g_object_unref() to release your reference
3035  *     to the icon.
3036  *
3037  * Since: 2.4
3038  **/
3039 GdkPixbuf *
3040 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
3041                          GError     **error)
3042 {
3043   g_return_val_if_fail (icon_info != NULL, NULL);
3044   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
3045
3046   if (!icon_info_ensure_scale_and_pixbuf (icon_info, FALSE))
3047     {
3048       if (icon_info->load_error)
3049         g_propagate_error (error, icon_info->load_error);
3050       else
3051         g_set_error_literal (error,  
3052                              GTK_ICON_THEME_ERROR,  
3053                              GTK_ICON_THEME_NOT_FOUND,
3054                              _("Failed to load icon"));
3055  
3056       return NULL;
3057     }
3058
3059   return g_object_ref (icon_info->pixbuf);
3060 }
3061
3062 static gchar *
3063 gdk_color_to_css (GdkColor *color)
3064 {
3065   return g_strdup_printf ("rgb(%d,%d,%d)",
3066                           color->red >> 8,
3067                           color->green >> 8,
3068                           color->blue >> 8);
3069 }
3070
3071 static gchar *
3072 gdk_rgba_to_css (GdkRGBA *color)
3073 {
3074   return g_strdup_printf ("rgba(%d,%d,%d,%f)",
3075                           (gint)(color->red * 255),
3076                           (gint)(color->green * 255),
3077                           (gint)(color->blue * 255),
3078                           color->alpha);
3079 }
3080
3081 static GdkPixbuf *
3082 _gtk_icon_info_load_symbolic_internal (GtkIconInfo  *icon_info,
3083                                        const gchar  *css_fg,
3084                                        const gchar  *css_success,
3085                                        const gchar  *css_warning,
3086                                        const gchar  *css_error,
3087                                        GError      **error)
3088 {
3089   GInputStream *stream;
3090   GdkPixbuf *pixbuf;
3091   gchar *data;
3092   gchar *success, *warning, *err;
3093
3094   /* css_fg can't possibly have failed, otherwise
3095    * that would mean we have a broken style */
3096   g_return_val_if_fail (css_fg != NULL, NULL);
3097
3098   success = warning = err = NULL;
3099
3100   if (!css_success)
3101     {
3102       GdkColor success_default_color = { 0, 0x4e00, 0x9a00, 0x0600 };
3103       success = gdk_color_to_css (&success_default_color);
3104     }
3105   if (!css_warning)
3106     {
3107       GdkColor warning_default_color = { 0, 0xf500, 0x7900, 0x3e00 };
3108       warning = gdk_color_to_css (&warning_default_color);
3109     }
3110   if (!css_error)
3111     {
3112       GdkColor error_default_color = { 0, 0xcc00, 0x0000, 0x0000 };
3113       err = gdk_color_to_css (&error_default_color);
3114     }
3115
3116
3117   data = g_strconcat ("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
3118                       "<svg version=\"1.1\"\n"
3119                       "     xmlns=\"http://www.w3.org/2000/svg\"\n"
3120                       "     xmlns:xi=\"http://www.w3.org/2001/XInclude\"\n"
3121                       "     width=\"16\"\n"
3122                       "     height=\"16\">\n"
3123                       "  <style type=\"text/css\">\n"
3124                       "    rect,path {\n"
3125                       "      fill: ", css_fg," !important;\n"
3126                       "    }\n"
3127                       "    .warning {\n"
3128                       "      fill: ", css_warning ? css_warning : warning," !important;\n"
3129                       "    }\n"
3130                       "    .error {\n"
3131                       "      fill: ", css_error ? css_error : err," !important;\n"
3132                       "    }\n"
3133                       "    .success {\n"
3134                       "      fill: ", css_success ? css_success : success," !important;\n"
3135                       "    }\n"
3136                       "  </style>\n"
3137                       "  <xi:include href=\"", icon_info->filename, "\"/>\n"
3138                       "</svg>",
3139                       NULL);
3140   g_free (warning);
3141   g_free (err);
3142   g_free (success);
3143
3144   stream = g_memory_input_stream_new_from_data (data, -1, g_free);
3145   pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
3146                                                 icon_info->desired_size,
3147                                                 icon_info->desired_size,
3148                                                 TRUE,
3149                                                 NULL,
3150                                                 error);
3151   g_object_unref (stream);
3152
3153   return pixbuf;
3154 }
3155
3156 /**
3157  * gtk_icon_info_load_symbolic:
3158  * @icon_info: a #GtkIconInfo
3159  * @fg: a #GdkRGBA representing the foreground color of the icon
3160  * @success_color: (allow-none): a #GdkRGBA representing the warning color
3161  *     of the icon or %NULL to use the default color
3162  * @warning_color: (allow-none): a #GdkRGBA representing the warning color
3163  *     of the icon or %NULL to use the default color
3164  * @error_color: (allow-none): a #GdkRGBA representing the error color
3165  *     of the icon or %NULL to use the default color (allow-none)
3166  * @was_symbolic: (allow-none): a #gboolean, returns whether the loaded icon
3167  *     was a symbolic one and whether the @fg color was applied to it.
3168  * @error: (allow-none): location to store error information on failure,
3169  *     or %NULL.
3170  *
3171  * Loads an icon, modifying it to match the system colours for the foreground,
3172  * success, warning and error colors provided. If the icon is not a symbolic
3173  * one, the function will return the result from gtk_icon_info_load_icon().
3174  *
3175  * This allows loading symbolic icons that will match the system theme.
3176  *
3177  * Unless you are implementing a widget, you will want to use
3178  * g_themed_icon_new_with_default_fallbacks() to load the icon.
3179  *
3180  * As implementation details, the icon loaded needs to be of SVG type,
3181  * contain the "symbolic" term as the last component of the icon name,
3182  * and use the 'fg', 'success', 'warning' and 'error' CSS styles in the
3183  * SVG file itself.
3184  *
3185  * See the <ulink url="http://www.freedesktop.org/wiki/SymbolicIcons">Symbolic Icons spec</ulink>
3186  * for more information about symbolic icons.
3187  *
3188  * Return value: (transfer full): a #GdkPixbuf representing the loaded icon
3189  *
3190  * Since: 3.0
3191  **/
3192 GdkPixbuf *
3193 gtk_icon_info_load_symbolic (GtkIconInfo  *icon_info,
3194                              GdkRGBA      *fg,
3195                              GdkRGBA      *success_color,
3196                              GdkRGBA      *warning_color,
3197                              GdkRGBA      *error_color,
3198                              gboolean     *was_symbolic,
3199                              GError      **error)
3200 {
3201   GdkPixbuf *pixbuf;
3202   gchar *css_fg;
3203   gchar *css_success;
3204   gchar *css_warning;
3205   gchar *css_error;
3206
3207   g_return_val_if_fail (fg != NULL, NULL);
3208
3209   if (!icon_info->filename ||
3210       !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3211     {
3212       if (was_symbolic)
3213         *was_symbolic = FALSE;
3214       return gtk_icon_info_load_icon (icon_info, error);
3215     }
3216
3217   if (was_symbolic)
3218     *was_symbolic = TRUE;
3219
3220   css_fg = gdk_rgba_to_string (fg);
3221
3222   css_success = css_warning = css_error = NULL;
3223
3224   if (warning_color)
3225     css_warning = gdk_rgba_to_string (warning_color);
3226
3227   if (error_color)
3228     css_error = gdk_rgba_to_string (error_color);
3229
3230   if (success_color)
3231     css_success = gdk_rgba_to_string (success_color);
3232
3233   pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
3234                                                   css_fg, css_success,
3235                                                   css_warning, css_error,
3236                                                   error);
3237   g_free (css_fg);
3238   g_free (css_warning);
3239   g_free (css_success);
3240   g_free (css_error);
3241
3242   return pixbuf;
3243 }
3244
3245 /**
3246  * gtk_icon_info_load_symbolic_for_context:
3247  * @icon_info: a #GtkIconInfo
3248  * @context: a #GtkStyleContext
3249  * @was_symbolic: (allow-none): a #gboolean, returns whether the loaded icon
3250  *     was a symbolic one and whether the @fg color was applied to it.
3251  * @error: (allow-none): location to store error information on failure,
3252  *     or %NULL.
3253  *
3254  * Loads an icon, modifying it to match the system colors for the foreground,
3255  * success, warning and error colors provided. If the icon is not a symbolic
3256  * one, the function will return the result from gtk_icon_info_load_icon().
3257  * This function uses the regular foreground color and the symbolic colors
3258  * with the names "success_color", "warning_color" and "error_color" from
3259  * the context.
3260  *
3261  * This allows loading symbolic icons that will match the system theme.
3262  *
3263  * See gtk_icon_info_load_symbolic() for more details.
3264  *
3265  * Return value: (transfer full): a #GdkPixbuf representing the loaded icon
3266  *
3267  * Since: 3.0
3268  **/
3269 GdkPixbuf *
3270 gtk_icon_info_load_symbolic_for_context (GtkIconInfo      *icon_info,
3271                                          GtkStyleContext  *context,
3272                                          gboolean         *was_symbolic,
3273                                          GError          **error)
3274 {
3275   GdkPixbuf *pixbuf;
3276   GdkRGBA *color = NULL;
3277   GdkRGBA rgba;
3278   gchar *css_fg = NULL, *css_success;
3279   gchar *css_warning, *css_error;
3280   GtkStateFlags state;
3281
3282   if (!icon_info->filename ||
3283       !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3284     {
3285       if (was_symbolic)
3286         *was_symbolic = FALSE;
3287       return gtk_icon_info_load_icon (icon_info, error);
3288     }
3289
3290   if (was_symbolic)
3291     *was_symbolic = TRUE;
3292
3293   state = gtk_style_context_get_state (context);
3294   gtk_style_context_get (context, state, "color", &color, NULL);
3295   if (color)
3296     {
3297       css_fg = gdk_rgba_to_css (color);
3298       gdk_rgba_free (color);
3299     }
3300
3301   css_success = css_warning = css_error = NULL;
3302
3303   if (gtk_style_context_lookup_color (context, "success_color", &rgba))
3304     css_success = gdk_rgba_to_css (&rgba);
3305
3306   if (gtk_style_context_lookup_color (context, "warning_color", &rgba))
3307     css_warning = gdk_rgba_to_css (&rgba);
3308
3309   if (gtk_style_context_lookup_color (context, "error_color", &rgba))
3310     css_error = gdk_rgba_to_css (&rgba);
3311
3312   pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
3313                                                   css_fg, css_success,
3314                                                   css_warning, css_error,
3315                                                   error);
3316
3317   g_free (css_fg);
3318   g_free (css_success);
3319   g_free (css_warning);
3320   g_free (css_error);
3321
3322   return pixbuf;
3323 }
3324
3325 /**
3326  * gtk_icon_info_load_symbolic_for_style:
3327  * @icon_info: a #GtkIconInfo
3328  * @style: a #GtkStyle to take the colors from
3329  * @state: the widget state to use for colors
3330  * @was_symbolic: (allow-none): a #gboolean, returns whether the loaded icon
3331  *     was a symbolic one and whether the @fg color was applied to it.
3332  * @error: (allow-none): location to store error information on failure,
3333  *     or %NULL.
3334  *
3335  * Loads an icon, modifying it to match the system colours for the foreground,
3336  * success, warning and error colors provided. If the icon is not a symbolic
3337  * one, the function will return the result from gtk_icon_info_load_icon().
3338  *
3339  * This allows loading symbolic icons that will match the system theme.
3340  *
3341  * See gtk_icon_info_load_symbolic() for more details.
3342  *
3343  * Return value: (transfer full): a #GdkPixbuf representing the loaded icon
3344  *
3345  * Since: 3.0
3346  *
3347  * Deprecated: 3.0: Use gtk_icon_info_load_symbolic_for_context() instead
3348  **/
3349 GdkPixbuf *
3350 gtk_icon_info_load_symbolic_for_style (GtkIconInfo   *icon_info,
3351                                        GtkStyle      *style,
3352                                        GtkStateType   state,
3353                                        gboolean      *was_symbolic,
3354                                        GError       **error)
3355 {
3356   GdkPixbuf *pixbuf;
3357   GdkColor success_color;
3358   GdkColor warning_color;
3359   GdkColor error_color;
3360   GdkColor *fg;
3361   gchar *css_fg, *css_success;
3362   gchar *css_warning, *css_error;
3363
3364   if (!icon_info->filename ||
3365       !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3366     {
3367       if (was_symbolic)
3368         *was_symbolic = FALSE;
3369       return gtk_icon_info_load_icon (icon_info, error);
3370     }
3371
3372   if (was_symbolic)
3373     *was_symbolic = TRUE;
3374
3375   fg = &style->fg[state];
3376   css_fg = gdk_color_to_css (fg);
3377
3378   css_success = css_warning = css_error = NULL;
3379
3380   if (gtk_style_lookup_color (style, "success_color", &success_color))
3381     css_success = gdk_color_to_css (&success_color);
3382
3383   if (gtk_style_lookup_color (style, "warning_color", &warning_color))
3384     css_warning = gdk_color_to_css (&warning_color);
3385
3386   if (gtk_style_lookup_color (style, "error_color", &error_color))
3387     css_error = gdk_color_to_css (&error_color);
3388
3389   pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
3390                                                   css_fg, css_success,
3391                                                   css_warning, css_error,
3392                                                   error);
3393
3394   g_free (css_fg);
3395   g_free (css_success);
3396   g_free (css_warning);
3397   g_free (css_error);
3398
3399   return pixbuf;
3400 }
3401
3402 /**
3403  * gtk_icon_info_set_raw_coordinates:
3404  * @icon_info: a #GtkIconInfo
3405  * @raw_coordinates: whether the coordinates of embedded rectangles
3406  *   and attached points should be returned in their original
3407  *   (unscaled) form.
3408  * 
3409  * Sets whether the coordinates returned by gtk_icon_info_get_embedded_rect()
3410  * and gtk_icon_info_get_attach_points() should be returned in their
3411  * original form as specified in the icon theme, instead of scaled
3412  * appropriately for the pixbuf returned by gtk_icon_info_load_icon().
3413  *
3414  * Raw coordinates are somewhat strange; they are specified to be with
3415  * respect to the unscaled pixmap for PNG and XPM icons, but for SVG
3416  * icons, they are in a 1000x1000 coordinate space that is scaled
3417  * to the final size of the icon.  You can determine if the icon is an SVG
3418  * icon by using gtk_icon_info_get_filename(), and seeing if it is non-%NULL
3419  * and ends in '.svg'.
3420  *
3421  * This function is provided primarily to allow compatibility wrappers
3422  * for older API's, and is not expected to be useful for applications.
3423  *
3424  * Since: 2.4
3425  **/
3426 void
3427 gtk_icon_info_set_raw_coordinates (GtkIconInfo *icon_info,
3428                                    gboolean     raw_coordinates)
3429 {
3430   g_return_if_fail (icon_info != NULL);
3431   
3432   icon_info->raw_coordinates = raw_coordinates != FALSE;
3433 }
3434
3435 /* Scale coordinates from the icon data prior to returning
3436  * them to the user.
3437  */
3438 static gboolean
3439 icon_info_scale_point (GtkIconInfo  *icon_info,
3440                        gint          x,
3441                        gint          y,
3442                        gint         *x_out,
3443                        gint         *y_out)
3444 {
3445   if (icon_info->raw_coordinates)
3446     {
3447       *x_out = x;
3448       *y_out = y;
3449     }
3450   else
3451     {
3452       if (!icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3453         return FALSE;
3454
3455       *x_out = 0.5 + x * icon_info->scale;
3456       *y_out = 0.5 + y * icon_info->scale;
3457     }
3458
3459   return TRUE;
3460 }
3461
3462 /**
3463  * gtk_icon_info_get_embedded_rect:
3464  * @icon_info: a #GtkIconInfo
3465  * @rectangle: #GdkRectangle in which to store embedded
3466  *   rectangle coordinates; coordinates are only stored
3467  *   when this function returns %TRUE.
3468  *
3469  * Gets the coordinates of a rectangle within the icon
3470  * that can be used for display of information such
3471  * as a preview of the contents of a text file.
3472  * See gtk_icon_info_set_raw_coordinates() for further
3473  * information about the coordinate system.
3474  * 
3475  * Return value: %TRUE if the icon has an embedded rectangle
3476  *
3477  * Since: 2.4
3478  **/
3479 gboolean
3480 gtk_icon_info_get_embedded_rect (GtkIconInfo  *icon_info,
3481                                  GdkRectangle *rectangle)
3482 {
3483   g_return_val_if_fail (icon_info != NULL, FALSE);
3484
3485   if (icon_info->data && icon_info->data->has_embedded_rect &&
3486       icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3487     {
3488       gint scaled_x0, scaled_y0;
3489       gint scaled_x1, scaled_y1;
3490       
3491       if (rectangle)
3492         {
3493           icon_info_scale_point (icon_info,
3494                                  icon_info->data->x0, icon_info->data->y0,
3495                                  &scaled_x0, &scaled_y0);
3496           icon_info_scale_point (icon_info,
3497                                  icon_info->data->x1, icon_info->data->y1,
3498                                  &scaled_x1, &scaled_y1);
3499           
3500           rectangle->x = scaled_x0;
3501           rectangle->y = scaled_y0;
3502           rectangle->width = scaled_x1 - rectangle->x;
3503           rectangle->height = scaled_y1 - rectangle->y;
3504         }
3505
3506       return TRUE;
3507     }
3508   else
3509     return FALSE;
3510 }
3511
3512 /**
3513  * gtk_icon_info_get_attach_points:
3514  * @icon_info: a #GtkIconInfo
3515  * @points: (allow-none) (array length=n_points) (out): location to store pointer to an array of points, or %NULL
3516  *          free the array of points with g_free().
3517  * @n_points: (allow-none): location to store the number of points in @points, or %NULL
3518  * 
3519  * Fetches the set of attach points for an icon. An attach point
3520  * is a location in the icon that can be used as anchor points for attaching
3521  * emblems or overlays to the icon.
3522  * 
3523  * Return value: %TRUE if there are any attach points for the icon.
3524  *
3525  * Since: 2.4
3526  **/
3527 gboolean
3528 gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
3529                                  GdkPoint   **points,
3530                                  gint        *n_points)
3531 {
3532   g_return_val_if_fail (icon_info != NULL, FALSE);
3533   
3534   if (icon_info->data && icon_info->data->n_attach_points &&
3535       icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3536     {
3537       if (points)
3538         {
3539           gint i;
3540           
3541           *points = g_new (GdkPoint, icon_info->data->n_attach_points);
3542           for (i = 0; i < icon_info->data->n_attach_points; i++)
3543             icon_info_scale_point (icon_info,
3544                                    icon_info->data->attach_points[i].x,
3545                                    icon_info->data->attach_points[i].y,
3546                                    &(*points)[i].x,
3547                                    &(*points)[i].y);
3548         }
3549           
3550       if (n_points)
3551         *n_points = icon_info->data->n_attach_points;
3552
3553       return TRUE;
3554     }
3555   else
3556     {
3557       if (points)
3558         *points = NULL;
3559       if (n_points)
3560         *n_points = 0;
3561       
3562       return FALSE;
3563     }
3564 }
3565
3566 /**
3567  * gtk_icon_info_get_display_name:
3568  * @icon_info: a #GtkIconInfo
3569  * 
3570  * Gets the display name for an icon. A display name is a
3571  * string to be used in place of the icon name in a user
3572  * visible context like a list of icons.
3573  * 
3574  * Return value: the display name for the icon or %NULL, if
3575  *  the icon doesn't have a specified display name. This value
3576  *  is owned @icon_info and must not be modified or free.
3577  *
3578  * Since: 2.4
3579  **/
3580 G_CONST_RETURN gchar *
3581 gtk_icon_info_get_display_name (GtkIconInfo *icon_info)
3582 {
3583   g_return_val_if_fail (icon_info != NULL, NULL);
3584
3585   if (icon_info->data)
3586     return icon_info->data->display_name;
3587   else
3588     return NULL;
3589 }
3590
3591 /*
3592  * Builtin icons
3593  */
3594
3595
3596 /**
3597  * gtk_icon_theme_add_builtin_icon:
3598  * @icon_name: the name of the icon to register
3599  * @size: the size at which to register the icon (different
3600  *        images can be registered for the same icon name
3601  *        at different sizes.)
3602  * @pixbuf: #GdkPixbuf that contains the image to use
3603  *          for @icon_name.
3604  * 
3605  * Registers a built-in icon for icon theme lookups. The idea
3606  * of built-in icons is to allow an application or library
3607  * that uses themed icons to function requiring files to
3608  * be present in the file system. For instance, the default
3609  * images for all of GTK+'s stock icons are registered
3610  * as built-icons.
3611  *
3612  * In general, if you use gtk_icon_theme_add_builtin_icon()
3613  * you should also install the icon in the icon theme, so
3614  * that the icon is generally available.
3615  *
3616  * This function will generally be used with pixbufs loaded
3617  * via gdk_pixbuf_new_from_inline().
3618  *
3619  * Since: 2.4
3620  **/
3621 void
3622 gtk_icon_theme_add_builtin_icon (const gchar *icon_name,
3623                                  gint         size,
3624                                  GdkPixbuf   *pixbuf)
3625 {
3626   BuiltinIcon *default_icon;
3627   GSList *icons;
3628   gpointer key;
3629
3630   g_return_if_fail (icon_name != NULL);
3631   g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3632   
3633   if (!icon_theme_builtin_icons)
3634     icon_theme_builtin_icons = g_hash_table_new (g_str_hash, g_str_equal);
3635
3636   icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
3637   if (!icons)
3638     key = g_strdup (icon_name);
3639   else
3640     key = (gpointer)icon_name;  /* Won't get stored */
3641
3642   default_icon = g_new (BuiltinIcon, 1);
3643   default_icon->size = size;
3644   default_icon->pixbuf = g_object_ref (pixbuf);
3645   icons = g_slist_prepend (icons, default_icon);
3646
3647   /* Replaces value, leaves key untouched
3648    */
3649   g_hash_table_insert (icon_theme_builtin_icons, key, icons);
3650 }
3651
3652 /* Look up a builtin icon; the min_difference_p and
3653  * has_larger_p out parameters allow us to combine
3654  * this lookup with searching through the actual directories
3655  * of the "hicolor" icon theme. See theme_lookup_icon()
3656  * for how they are used.
3657  */
3658 static BuiltinIcon *
3659 find_builtin_icon (const gchar *icon_name,
3660                    gint         size,
3661                    gint        *min_difference_p,
3662                    gboolean    *has_larger_p)
3663 {
3664   GSList *icons = NULL;
3665   gint min_difference = G_MAXINT;
3666   gboolean has_larger = FALSE;
3667   BuiltinIcon *min_icon = NULL;
3668   
3669   if (!icon_theme_builtin_icons)
3670     return NULL;
3671
3672   icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
3673
3674   while (icons)
3675     {
3676       BuiltinIcon *default_icon = icons->data;
3677       int min, max, difference;
3678       gboolean smaller;
3679       
3680       min = default_icon->size - 2;
3681       max = default_icon->size + 2;
3682       smaller = size < min;
3683       if (size < min)
3684         difference = min - size;
3685       else if (size > max)
3686         difference = size - max;
3687       else
3688         difference = 0;
3689       
3690       if (difference == 0)
3691         {
3692           min_icon = default_icon;
3693           break;
3694         }
3695       
3696       if (!has_larger)
3697         {
3698           if (difference < min_difference || smaller)
3699             {
3700               min_difference = difference;
3701               min_icon = default_icon;
3702               has_larger = smaller;
3703             }
3704         }
3705       else
3706         {
3707           if (difference < min_difference && smaller)
3708             {
3709               min_difference = difference;
3710               min_icon = default_icon;
3711             }
3712         }
3713       
3714       icons = icons->next;
3715     }
3716
3717   if (min_difference_p)
3718     *min_difference_p = min_difference;
3719   if (has_larger_p)
3720     *has_larger_p = has_larger;
3721
3722   return min_icon;
3723 }
3724
3725 void
3726 _gtk_icon_theme_check_reload (GdkDisplay *display)
3727 {
3728   gint n_screens, i;
3729   GdkScreen *screen;
3730   GtkIconTheme *icon_theme;
3731
3732   n_screens = gdk_display_get_n_screens (display);
3733   
3734   for (i = 0; i < n_screens; i++)
3735     {
3736       screen = gdk_display_get_screen (display, i);
3737
3738       icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
3739       if (icon_theme)
3740         {
3741           icon_theme->priv->check_reload = TRUE;
3742           ensure_valid_themes (icon_theme);
3743           icon_theme->priv->check_reload = FALSE;
3744         }
3745     }
3746 }
3747
3748
3749 /**
3750  * gtk_icon_theme_lookup_by_gicon:
3751  * @icon_theme: a #GtkIconTheme
3752  * @icon: the #GIcon to look up
3753  * @size: desired icon size
3754  * @flags: flags modifying the behavior of the icon lookup
3755  * 
3756  * Looks up an icon and returns a structure containing
3757  * information such as the filename of the icon. 
3758  * The icon can then be rendered into a pixbuf using
3759  * gtk_icon_info_load_icon().
3760  *
3761  * Return value: a #GtkIconInfo structure containing 
3762  *     information about the icon, or %NULL if the icon 
3763  *     wasn't found. Free with gtk_icon_info_free()
3764  *
3765  * Since: 2.14
3766  */
3767 GtkIconInfo *
3768 gtk_icon_theme_lookup_by_gicon (GtkIconTheme       *icon_theme,
3769                                 GIcon              *icon,
3770                                 gint                size,
3771                                 GtkIconLookupFlags  flags)
3772 {
3773   GtkIconInfo *info;
3774
3775   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
3776   g_return_val_if_fail (G_IS_ICON (icon), NULL);
3777
3778   if (G_IS_LOADABLE_ICON (icon))
3779     {
3780       info = icon_info_new ();
3781       info->loadable = G_LOADABLE_ICON (g_object_ref (icon));
3782
3783       info->dir_type = ICON_THEME_DIR_UNTHEMED;
3784       info->dir_size = size;
3785       info->desired_size = size;
3786       info->threshold = 2;
3787       info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
3788
3789       return info;
3790     }
3791   else if (G_IS_THEMED_ICON (icon))
3792     {
3793       const gchar **names;
3794
3795       names = (const gchar **)g_themed_icon_get_names (G_THEMED_ICON (icon));
3796       info = gtk_icon_theme_choose_icon (icon_theme, names, size, flags);
3797
3798       return info;
3799     }
3800   else if (G_IS_EMBLEMED_ICON (icon))
3801     {
3802       GIcon *base, *emblem;
3803       GList *list, *l;
3804       GtkIconInfo *emblem_info;
3805
3806       if (GTK_IS_NUMERABLE_ICON (icon))
3807         _gtk_numerable_icon_set_background_icon_size (GTK_NUMERABLE_ICON (icon), size / 2);
3808
3809       base = g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon));
3810       info = gtk_icon_theme_lookup_by_gicon (icon_theme, base, size, flags);
3811       if (info)
3812         {
3813           list = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon));
3814           for (l = list; l; l = l->next)
3815             {
3816               emblem = g_emblem_get_icon (G_EMBLEM (l->data));
3817               /* always force size for emblems */
3818               emblem_info = gtk_icon_theme_lookup_by_gicon (icon_theme, emblem, size / 2, flags | GTK_ICON_LOOKUP_FORCE_SIZE);
3819               if (emblem_info)
3820                 info->emblem_infos = g_slist_prepend (info->emblem_infos, emblem_info);
3821             }
3822         }
3823
3824       return info;
3825     }
3826   else if (GDK_IS_PIXBUF (icon))
3827     {
3828       GdkPixbuf *pixbuf;
3829
3830       pixbuf = GDK_PIXBUF (icon);
3831
3832       if ((flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0)
3833         {
3834           gint width, height, max;
3835           gdouble scale;
3836           GdkPixbuf *scaled;
3837
3838           width = gdk_pixbuf_get_width (pixbuf);
3839           height = gdk_pixbuf_get_height (pixbuf);
3840           max = MAX (width, height);
3841           scale = (gdouble) size / (gdouble) max;
3842
3843           scaled = gdk_pixbuf_scale_simple (pixbuf,
3844                                             0.5 + width * scale,
3845                                             0.5 + height * scale,
3846                                             GDK_INTERP_BILINEAR);
3847
3848           info = gtk_icon_info_new_for_pixbuf (icon_theme, scaled);
3849
3850           g_object_unref (scaled);
3851         }
3852       else
3853         {
3854           info = gtk_icon_info_new_for_pixbuf (icon_theme, pixbuf);
3855         }
3856
3857       return info;
3858     }
3859
3860   return NULL;
3861 }
3862
3863 /**
3864  * gtk_icon_info_new_for_pixbuf:
3865  * @icon_theme: a #GtkIconTheme
3866  * @pixbuf: the pixbuf to wrap in a #GtkIconInfo
3867  *
3868  * Creates a #GtkIconInfo for a #GdkPixbuf.
3869  *
3870  * Returns: a #GtkIconInfo
3871  *
3872  * Since: 2.14
3873  */
3874 GtkIconInfo *
3875 gtk_icon_info_new_for_pixbuf (GtkIconTheme *icon_theme,
3876                               GdkPixbuf    *pixbuf)
3877 {
3878   GtkIconInfo *info;
3879
3880   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
3881   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
3882
3883   info = icon_info_new ();
3884   info->pixbuf = g_object_ref (pixbuf);
3885   info->scale = 1.0;
3886   info->dir_type = ICON_THEME_DIR_UNTHEMED;
3887
3888   return info;
3889 }