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