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