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