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