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