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