]> Pileus Git - ~andy/gtk/blob - gtk/gtkicontheme.c
Add a warning about unreffing icon themes. (#310163, Rodney Dawes)
[~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 #endif /* G_OS_WIN32 */
37
38 #include "gtkicontheme.h"
39 #include "gtkiconfactory.h"
40 #include "gtkiconcache.h"
41 #include "gtkintl.h"
42 #include "gtksettings.h"
43 #include "gtkprivate.h"
44 #include "gtkalias.h"
45
46
47 #define DEFAULT_THEME_NAME "hicolor"
48
49 typedef enum
50 {
51   ICON_THEME_DIR_FIXED,  
52   ICON_THEME_DIR_SCALABLE,  
53   ICON_THEME_DIR_THRESHOLD,
54   ICON_THEME_DIR_UNTHEMED
55 } IconThemeDirType;
56
57 /* In reverse search order: */
58 typedef enum
59 {
60   ICON_SUFFIX_NONE = 0,
61   ICON_SUFFIX_XPM = 1 << 0,
62   ICON_SUFFIX_SVG = 1 << 1,
63   ICON_SUFFIX_PNG = 1 << 2,
64   HAS_ICON_FILE = 1 << 3
65 } IconSuffix;
66
67
68 struct _GtkIconThemePrivate
69 {
70   guint custom_theme : 1;
71   guint is_screen_singleton : 1;
72   guint pixbuf_supports_svg : 1;
73   
74   char *current_theme;
75   char **search_path;
76   int search_path_len;
77
78   gboolean themes_valid;
79   /* A list of all the themes needed to look up icons.
80    * In search order, without duplicates
81    */
82   GList *themes;
83   GHashTable *unthemed_icons;
84   GList *unthemed_icons_caches;
85   
86   /* Note: The keys of this hashtable are owned by the
87    * themedir and unthemed hashtables.
88    */
89   GHashTable *all_icons;
90
91   /* GdkScreen for the icon theme (may be NULL)
92    */
93   GdkScreen *screen;
94   
95   /* time when we last stat:ed for theme changes */
96   long last_stat_time;
97   GList *dir_mtimes;
98 };
99
100 struct _GtkIconInfo
101 {
102   /* Information about the source
103    */
104   gchar *filename;
105 #ifdef G_OS_WIN32
106   /* System codepage version of filename, for DLL ABI backward
107    * compatibility functions.
108    */
109   gchar *cp_filename;
110 #endif
111   GdkPixbuf *builtin_pixbuf;
112
113   /* Cache pixbuf (if there is any) */
114   GdkPixbuf *cache_pixbuf;
115
116   GtkIconData *data;
117   
118   /* Information about the directory where
119    * the source was found
120    */
121   IconThemeDirType dir_type;
122   gint dir_size;
123   gint threshold;
124
125   /* Parameters influencing the scaled icon
126    */
127   gint desired_size;
128   gboolean raw_coordinates;
129
130   /* Cached information if we go ahead and try to load
131    * the icon.
132    */
133   GdkPixbuf *pixbuf;
134   GError *load_error;
135   gdouble scale;
136 };
137
138 typedef struct
139 {
140   char *name;
141   char *display_name;
142   char *comment;
143   char *example;
144
145   /* In search order */
146   GList *dirs;
147 } IconTheme;
148
149 typedef struct
150 {
151   IconThemeDirType type;
152   GQuark context;
153
154   int size;
155   int min_size;
156   int max_size;
157   int threshold;
158
159   char *dir;
160   char *subdir;
161   
162   GtkIconCache *cache;
163   
164   GHashTable *icons;
165   GHashTable *icon_data;
166 } IconThemeDir;
167
168 typedef struct
169 {
170   char *svg_filename;
171   char *no_svg_filename;
172 } UnthemedIcon;
173
174 typedef struct
175 {
176   gint size;
177   GdkPixbuf *pixbuf;
178 } BuiltinIcon;
179
180 typedef struct 
181 {
182   char *dir;
183   time_t mtime; /* 0 == not existing or not a dir */
184
185   GtkIconCache *cache;
186 } IconThemeDirMtime;
187
188 static void  gtk_icon_theme_class_init (GtkIconThemeClass    *klass);
189 static void  gtk_icon_theme_init       (GtkIconTheme         *icon_theme);
190 static void  gtk_icon_theme_finalize   (GObject              *object);
191 static void  theme_dir_destroy         (IconThemeDir         *dir);
192
193 static void         theme_destroy     (IconTheme        *theme);
194 static GtkIconInfo *theme_lookup_icon (IconTheme        *theme,
195                                        const char       *icon_name,
196                                        int               size,
197                                        gboolean          allow_svg,
198                                        gboolean          use_default_icons);
199 static void         theme_list_icons  (IconTheme        *theme,
200                                        GHashTable       *icons,
201                                        GQuark            context);
202 static void         theme_subdir_load (GtkIconTheme     *icon_theme,
203                                        IconTheme        *theme,
204                                        GKeyFile         *theme_file,
205                                        char             *subdir);
206 static void         do_theme_change   (GtkIconTheme     *icon_theme);
207
208 static void  blow_themes               (GtkIconTheme    *icon_themes);
209
210 static void  icon_data_free            (GtkIconData          *icon_data);
211 static void load_icon_data (IconThemeDir *dir,
212                             const char   *path,
213                             const char   *name);
214
215 static IconSuffix theme_dir_get_icon_suffix (IconThemeDir *dir,
216                                              const gchar  *icon_name,
217                                              gboolean     *has_icon_file);
218
219
220 static GtkIconInfo *icon_info_new             (void);
221 static GtkIconInfo *icon_info_new_builtin     (BuiltinIcon *icon);
222
223 static IconSuffix suffix_from_name (const char *name);
224
225 static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
226                                        gint         size,
227                                        gint        *min_difference_p,
228                                        gboolean    *has_larger_p);
229
230 static GObjectClass *parent_class = NULL;
231
232 static guint signal_changed = 0;
233
234 static GHashTable *icon_theme_builtin_icons;
235
236 GType
237 gtk_icon_theme_get_type (void)
238 {
239   static GType type = 0;
240
241   if (type == 0)
242     {
243       static const GTypeInfo info =
244         {
245           sizeof (GtkIconThemeClass),
246           NULL,           /* base_init */
247           NULL,           /* base_finalize */
248           (GClassInitFunc) gtk_icon_theme_class_init,
249           NULL,           /* class_finalize */
250           NULL,           /* class_data */
251           sizeof (GtkIconTheme),
252           0,              /* n_preallocs */
253           (GInstanceInitFunc) gtk_icon_theme_init,
254         };
255
256       type = g_type_register_static (G_TYPE_OBJECT, "GtkIconTheme", &info, 0);
257     }
258
259   return type;
260 }
261
262 /**
263  * gtk_icon_theme_new:
264  * 
265  * Creates a new icon theme object. Icon theme objects are used
266  * to lookup up an icon by name in a particular icon theme.
267  * Usually, you'll want to use gtk_icon_theme_get_default()
268  * or gtk_icon_theme_get_for_screen() rather than creating
269  * a new icon theme object for scratch.
270  * 
271  * Return value: the newly created #GtkIconTheme object.
272  *
273  * Since: 2.4
274  **/
275 GtkIconTheme *
276 gtk_icon_theme_new (void)
277 {
278   return g_object_new (GTK_TYPE_ICON_THEME, NULL);
279 }
280
281 /**
282  * gtk_icon_theme_get_default:
283  * 
284  * Gets the icon theme for the default screen. See
285  * gtk_icon_theme_get_for_screen().
286  * 
287  * Return value: A unique #GtkIconTheme associated with
288  *  the default screen. This icon theme is associated with
289  *  the screen and can be used as long as the screen
290  *  is open. Do not ref or unref it.
291  *
292  * Since: 2.4
293  **/
294 GtkIconTheme *
295 gtk_icon_theme_get_default (void)
296 {
297   return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
298 }
299
300 /**
301  * gtk_icon_theme_get_for_screen:
302  * @screen: a #GdkScreen
303  * 
304  * Gets the icon theme object associated with @screen; if this
305  * function has not previously been called for the given
306  * screen, a new icon theme object will be created and
307  * associated with the screen. Icon theme objects are
308  * fairly expensive to create, so using this function
309  * is usually a better choice than calling than gtk_icon_theme_new()
310  * and setting the screen yourself; by using this function
311  * a single icon theme object will be shared between users.
312  * 
313  * Return value: A unique #GtkIconTheme associated with
314  *  the given screen. This icon theme is associated with
315  *  the screen and can be used as long as the screen
316  *  is open. Do not ref or unref it.
317  *
318  * Since: 2.4
319  **/
320 GtkIconTheme *
321 gtk_icon_theme_get_for_screen (GdkScreen *screen)
322 {
323   GtkIconTheme *icon_theme;
324
325   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
326   g_return_val_if_fail (!screen->closed, NULL);
327
328   icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
329   if (!icon_theme)
330     {
331       GtkIconThemePrivate *priv;
332
333       icon_theme = gtk_icon_theme_new ();
334       gtk_icon_theme_set_screen (icon_theme, screen);
335
336       priv = icon_theme->priv;
337       priv->is_screen_singleton = TRUE;
338
339       g_object_set_data (G_OBJECT (screen), "gtk-icon-theme", icon_theme);
340     }
341
342   return icon_theme;
343 }
344
345 static void
346 gtk_icon_theme_class_init (GtkIconThemeClass *klass)
347 {
348   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
349
350   parent_class = g_type_class_peek_parent (klass);
351
352   gobject_class->finalize = gtk_icon_theme_finalize;
353
354 /**
355  * GtkIconTheme::changed
356  * @icon_theme: the icon theme
357  * 
358  * Emitted when the current icon theme is switched or GTK+ detects
359  * that a change has occurred in the contents of the current
360  * icon theme.
361  **/
362   signal_changed = g_signal_new ("changed",
363                                  G_TYPE_FROM_CLASS (klass),
364                                  G_SIGNAL_RUN_LAST,
365                                  G_STRUCT_OFFSET (GtkIconThemeClass, changed),
366                                  NULL, NULL,
367                                  g_cclosure_marshal_VOID__VOID,
368                                  G_TYPE_NONE, 0);
369
370   g_type_class_add_private (klass, sizeof (GtkIconThemePrivate));
371 }
372
373
374 /* Callback when the display that the icon theme is attached to
375  * is closed; unset the screen, and if it's the unique theme
376  * for the screen, drop the reference
377  */
378 static void
379 display_closed (GdkDisplay   *display,
380                 gboolean      is_error,
381                 GtkIconTheme *icon_theme)
382 {
383   GtkIconThemePrivate *priv = icon_theme->priv;
384   GdkScreen *screen = priv->screen;
385   gboolean was_screen_singleton = priv->is_screen_singleton;
386
387   if (was_screen_singleton)
388     {
389       g_object_set_data (G_OBJECT (screen), "gtk-icon-theme", NULL);
390       priv->is_screen_singleton = FALSE;
391     }
392
393   gtk_icon_theme_set_screen (icon_theme, NULL);
394
395   if (was_screen_singleton)
396     {
397       g_object_unref (icon_theme);
398     }
399 }
400
401 static void
402 update_current_theme (GtkIconTheme *icon_theme)
403 {
404   GtkIconThemePrivate *priv = icon_theme->priv;
405
406   if (!priv->custom_theme)
407     {
408       gchar *theme = NULL;
409
410       if (priv->screen)
411         {
412           GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
413           g_object_get (settings, "gtk-icon-theme-name", &theme, NULL);
414         }
415
416       if (!theme)
417         theme = g_strdup (DEFAULT_THEME_NAME);
418
419       if (strcmp (priv->current_theme, theme) != 0)
420         {
421           g_free (priv->current_theme);
422           priv->current_theme = theme;
423
424           do_theme_change (icon_theme);
425         }
426       else
427         g_free (theme);
428     }
429 }
430
431 /* Callback when the icon theme GtkSetting changes
432  */
433 static void
434 theme_changed (GtkSettings  *settings,
435                GParamSpec   *pspec,
436                GtkIconTheme *icon_theme)
437 {
438   update_current_theme (icon_theme);
439 }
440
441 static void
442 unset_screen (GtkIconTheme *icon_theme)
443 {
444   GtkIconThemePrivate *priv = icon_theme->priv;
445   GtkSettings *settings;
446   GdkDisplay *display;
447   
448   if (priv->screen)
449     {
450       settings = gtk_settings_get_for_screen (priv->screen);
451       display = gdk_screen_get_display (priv->screen);
452       
453       g_signal_handlers_disconnect_by_func (display,
454                                             (gpointer) display_closed,
455                                             icon_theme);
456       g_signal_handlers_disconnect_by_func (settings,
457                                             (gpointer) theme_changed,
458                                             icon_theme);
459
460       priv->screen = NULL;
461     }
462 }
463
464 /**
465  * gtk_icon_theme_set_screen:
466  * @icon_theme: a #GtkIconTheme
467  * @screen: a #GdkScreen
468  * 
469  * Sets the screen for an icon theme; the screen is used
470  * to track the user's currently configured icon theme,
471  * which might be different for different screens.
472  *
473  * Since: 2.4
474  **/
475 void
476 gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
477                            GdkScreen    *screen)
478 {
479   GtkIconThemePrivate *priv;
480   GtkSettings *settings;
481   GdkDisplay *display;
482
483   g_return_if_fail (GTK_ICON_THEME (icon_theme));
484   g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
485
486   priv = icon_theme->priv;
487
488   unset_screen (icon_theme);
489   
490   if (screen)
491     {
492       display = gdk_screen_get_display (screen);
493       settings = gtk_settings_get_for_screen (screen);
494       
495       priv->screen = screen;
496       
497       g_signal_connect (display, "closed",
498                         G_CALLBACK (display_closed), icon_theme);
499       g_signal_connect (settings, "notify::gtk-icon-theme-name",
500                         G_CALLBACK (theme_changed), icon_theme);
501     }
502
503   update_current_theme (icon_theme);
504 }
505
506 /* Checks whether a loader for SVG files has been registered
507  * with GdkPixbuf.
508  */
509 static gboolean
510 pixbuf_supports_svg (void)
511 {
512   GSList *formats = gdk_pixbuf_get_formats ();
513   GSList *tmp_list;
514   static gboolean found_svg = FALSE;
515   static gboolean value_known = FALSE;
516
517   if (value_known)
518     return found_svg;
519   
520   for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
521     {
522       gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
523       gchar **mime_type;
524       
525       for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
526         {
527           if (strcmp (*mime_type, "image/svg") == 0)
528             found_svg = TRUE;
529         }
530
531       g_strfreev (mime_types);
532     }
533
534   g_slist_free (formats);
535   value_known = TRUE;
536   
537   return found_svg;
538 }
539
540 static void
541 gtk_icon_theme_init (GtkIconTheme *icon_theme)
542 {
543   GtkIconThemePrivate *priv;
544   const gchar * const *xdg_data_dirs;
545   int i, j;
546   
547   priv = g_type_instance_get_private ((GTypeInstance *)icon_theme,
548                                       GTK_TYPE_ICON_THEME);
549   icon_theme->priv = priv;
550
551   priv->custom_theme = FALSE;
552   priv->current_theme = g_strdup (DEFAULT_THEME_NAME);
553
554   xdg_data_dirs = g_get_system_data_dirs ();
555   for (i = 0; xdg_data_dirs[i]; i++) ;
556
557   priv->search_path_len = 2 * i + 2;
558   
559   priv->search_path = g_new (char *, priv->search_path_len);
560   
561   i = 0;
562   priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
563   priv->search_path[i++] = g_build_filename (g_get_user_data_dir (), "icons", NULL);
564   
565   for (j = 0; xdg_data_dirs[j]; j++) 
566     priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
567
568   for (j = 0; xdg_data_dirs[j]; j++) 
569     priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "pixmaps", NULL);
570
571   priv->themes_valid = FALSE;
572   priv->themes = NULL;
573   priv->unthemed_icons = NULL;
574   priv->unthemed_icons_caches = NULL;
575   
576   priv->pixbuf_supports_svg = pixbuf_supports_svg ();
577 }
578
579 static void
580 free_dir_mtime (IconThemeDirMtime *dir_mtime)
581 {
582   if (dir_mtime->cache)
583     _gtk_icon_cache_unref (dir_mtime->cache);
584
585   g_free (dir_mtime->dir);
586   g_free (dir_mtime);
587
588 }
589
590 static void
591 do_theme_change (GtkIconTheme *icon_theme)
592 {
593   GtkIconThemePrivate *priv = icon_theme->priv;
594   
595   GTK_NOTE (ICONTHEME, 
596             g_print ("change to icon theme \"%s\"\n", priv->current_theme));
597   blow_themes (icon_theme);
598   g_signal_emit (icon_theme, signal_changed, 0);
599   
600   if (priv->screen && priv->is_screen_singleton)
601     {
602       GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
603       gtk_rc_reset_styles (settings);
604     }
605 }
606
607 static void 
608 free_cache (gpointer data, 
609             gpointer user_data)
610 {
611   GtkIconCache *cache = (GtkIconCache *)data;
612
613   if (cache)
614     _gtk_icon_cache_unref (cache);
615 }
616
617 static void
618 blow_themes (GtkIconTheme *icon_theme)
619 {
620   GtkIconThemePrivate *priv = icon_theme->priv;
621   
622   if (priv->themes_valid)
623     {
624       g_hash_table_destroy (priv->all_icons);
625       g_list_foreach (priv->themes, (GFunc)theme_destroy, NULL);
626       g_list_free (priv->themes);
627       g_list_foreach (priv->dir_mtimes, (GFunc)free_dir_mtime, NULL);
628       g_list_free (priv->dir_mtimes);
629       g_hash_table_destroy (priv->unthemed_icons);
630       if (priv->unthemed_icons_caches)
631         g_list_foreach (priv->unthemed_icons_caches, free_cache, NULL);
632       g_list_free (priv->unthemed_icons_caches);
633     }
634   priv->themes = NULL;
635   priv->unthemed_icons = NULL;
636   priv->unthemed_icons_caches = NULL;
637   priv->dir_mtimes = NULL;
638   priv->all_icons = NULL;
639   priv->themes_valid = FALSE;
640 }
641
642 static void
643 gtk_icon_theme_finalize (GObject *object)
644 {
645   GtkIconTheme *icon_theme;
646   GtkIconThemePrivate *priv;
647   int i;
648
649   icon_theme = GTK_ICON_THEME (object);
650   priv = icon_theme->priv;
651
652   unset_screen (icon_theme);
653
654   g_free (priv->current_theme);
655   priv->current_theme = NULL;
656
657   for (i=0; i < priv->search_path_len; i++)
658     g_free (priv->search_path[i]);
659
660   g_free (priv->search_path);
661   priv->search_path = NULL;
662
663   blow_themes (icon_theme);
664
665   G_OBJECT_CLASS (parent_class)->finalize (object);  
666 }
667
668 /**
669  * gtk_icon_theme_set_search_path:
670  * @icon_theme: a #GtkIconTheme
671  * @path: array of directories that are searched for icon themes
672  * @n_elements: number of elements in @path.
673  * 
674  * Sets the search path for the icon theme object. When looking
675  * for an icon theme, GTK+ will search for a subdirectory of
676  * one or more of the directories in @path with the same name
677  * as the icon theme. (Themes from multiple of the path elements
678  * are combined to allow themes to be extended by adding icons
679  * in the user's home directory.)
680  *
681  * In addition if an icon found isn't found either in the current
682  * icon theme or the default icon theme, and an image file with
683  * the right name is found directly in one of the elements of
684  * @path, then that image will be used for the icon name.
685  * (This is legacy feature, and new icons should be put
686  * into the default icon theme, which is called DEFAULT_THEME_NAME,
687  * rather than directly on the icon path.)
688  *
689  * Since: 2.4
690  **/
691 void
692 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
693                                 const gchar  *path[],
694                                 gint          n_elements)
695 {
696   GtkIconThemePrivate *priv;
697   gint i;
698
699   g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
700
701   priv = icon_theme->priv;
702   for (i = 0; i < priv->search_path_len; i++)
703     g_free (priv->search_path[i]);
704
705   g_free (priv->search_path);
706
707   priv->search_path = g_new (gchar *, n_elements);
708   priv->search_path_len = n_elements;
709   for (i = 0; i < priv->search_path_len; i++)
710     priv->search_path[i] = g_strdup (path[i]);
711
712   do_theme_change (icon_theme);
713 }
714
715
716 /**
717  * gtk_icon_theme_get_search_path:
718  * @icon_theme: a #GtkIconTheme
719  * @path: location to store a list of icon theme path directories or %NULL
720  *        The stored value should be freed with g_strfreev().
721  * @n_elements: location to store number of elements
722  *              in @path, or %NULL
723  * 
724  * Gets the current search path. See gtk_icon_theme_set_search_path().
725  *
726  * Since: 2.4
727  **/
728 void
729 gtk_icon_theme_get_search_path (GtkIconTheme      *icon_theme,
730                                 gchar            **path[],
731                                 gint              *n_elements)
732 {
733   GtkIconThemePrivate *priv;
734   int i;
735
736   g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
737
738   priv = icon_theme->priv;
739
740   if (n_elements)
741     *n_elements = priv->search_path_len;
742   
743   if (path)
744     {
745       *path = g_new (gchar *, priv->search_path_len + 1);
746       for (i = 0; i < priv->search_path_len; i++)
747         (*path)[i] = g_strdup (priv->search_path[i]);
748       (*path)[i] = NULL;
749     }
750 }
751
752 /**
753  * gtk_icon_theme_append_search_path:
754  * @icon_theme: a #GtkIconTheme
755  * @path: directory name to append to the icon path
756  * 
757  * Appends a directory to the search path. 
758  * See gtk_icon_theme_set_search_path(). 
759  *
760  * Since: 2.4
761  **/
762 void
763 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
764                                    const gchar  *path)
765 {
766   GtkIconThemePrivate *priv;
767
768   g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
769   g_return_if_fail (path != NULL);
770
771   priv = icon_theme->priv;
772   
773   priv->search_path_len++;
774   priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
775   priv->search_path[priv->search_path_len-1] = g_strdup (path);
776
777   do_theme_change (icon_theme);
778 }
779
780 /**
781  * gtk_icon_theme_prepend_search_path:
782  * @icon_theme: a #GtkIconTheme
783  * @path: directory name to prepend to the icon path
784  * 
785  * Prepends a directory to the search path. 
786  * See gtk_icon_theme_set_search_path().
787  *
788  * Since: 2.4
789  **/
790 void
791 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
792                                     const gchar  *path)
793 {
794   GtkIconThemePrivate *priv;
795   int i;
796
797   g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
798   g_return_if_fail (path != NULL);
799
800   priv = icon_theme->priv;
801   
802   priv->search_path_len++;
803   priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
804
805   for (i = priv->search_path_len - 1; i > 0; i--)
806     priv->search_path[i] = priv->search_path[i - 1];
807   
808   priv->search_path[0] = g_strdup (path);
809
810   do_theme_change (icon_theme);
811 }
812
813 /**
814  * gtk_icon_theme_set_custom_theme:
815  * @icon_theme: a #GtkIconTheme
816  * @theme_name: name of icon theme to use instead of configured theme,
817  *   or %NULL to unset a previously set custom theme
818  * 
819  * Sets the name of the icon theme that the #GtkIconTheme object uses
820  * overriding system configuration. This function cannot be called
821  * on the icon theme objects returned from gtk_icon_theme_get_default()
822  * and gtk_icon_theme_get_for_screen().
823  *
824  * Since: 2.4
825  **/
826 void
827 gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
828                                  const gchar  *theme_name)
829 {
830   GtkIconThemePrivate *priv;
831
832   g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
833
834   priv = icon_theme->priv;
835
836   g_return_if_fail (!priv->is_screen_singleton);
837   
838   if (theme_name != NULL)
839     {
840       priv->custom_theme = TRUE;
841       if (strcmp (theme_name, priv->current_theme) != 0)
842         {
843           g_free (priv->current_theme);
844           priv->current_theme = g_strdup (theme_name);
845
846           do_theme_change (icon_theme);
847         }
848     }
849   else
850     {
851       priv->custom_theme = FALSE;
852
853       update_current_theme (icon_theme);
854     }
855 }
856
857 static void
858 insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
859 {
860   int i;
861   GList *l;
862   char **dirs;
863   char **themes;
864   GtkIconThemePrivate *priv;
865   IconTheme *theme;
866   char *path;
867   GKeyFile *theme_file;
868   GError *error = NULL;
869   IconThemeDirMtime *dir_mtime;
870   struct stat stat_buf;
871   
872   priv = icon_theme->priv;
873
874   for (l = priv->themes; l != NULL; l = l->next)
875     {
876       theme = l->data;
877       if (strcmp (theme->name, theme_name) == 0)
878         return;
879     }
880   
881   for (i = 0; i < priv->search_path_len; i++)
882     {
883       path = g_build_filename (priv->search_path[i],
884                                theme_name,
885                                NULL);
886       dir_mtime = g_new (IconThemeDirMtime, 1);
887       dir_mtime->cache = NULL;
888       dir_mtime->dir = path;
889       if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
890         dir_mtime->mtime = stat_buf.st_mtime;
891       else
892         dir_mtime->mtime = 0;
893
894       priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
895     }
896   priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
897
898   theme_file = NULL;
899   for (i = 0; i < priv->search_path_len && !theme_file; i++)
900     {
901       path = g_build_filename (priv->search_path[i],
902                                theme_name,
903                                "index.theme",
904                                NULL);
905       if (g_file_test (path, G_FILE_TEST_IS_REGULAR)) 
906         {
907           theme_file = g_key_file_new ();
908           g_key_file_set_list_separator (theme_file, ',');
909           g_key_file_load_from_file (theme_file, path, 0, &error);
910           if (error)
911             {
912               g_key_file_free (theme_file);
913               theme_file = NULL;
914               g_error_free (error);
915               error = NULL;
916             }
917         }
918       g_free (path);
919     }
920
921   if (theme_file == NULL)
922     return;
923   
924   theme = g_new (IconTheme, 1);
925   theme->display_name = 
926     g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
927   if (!theme->display_name)
928     {
929       g_warning ("Theme file for %s has no name\n", theme_name);
930       g_free (theme);
931       g_key_file_free (theme_file);
932       return;
933     }
934
935   dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
936   if (!dirs)
937     {
938       g_warning ("Theme file for %s has no directories\n", theme_name);
939       g_free (theme->display_name);
940       g_free (theme);
941       g_key_file_free (theme_file);
942       return;
943     }
944   
945   theme->name = g_strdup (theme_name);
946   theme->comment = 
947     g_key_file_get_locale_string (theme_file, 
948                                   "Icon Theme", "Comment",
949                                   NULL, NULL);
950   theme->example = 
951     g_key_file_get_string (theme_file, 
952                            "Icon Theme", "Example",
953                            NULL);
954
955   theme->dirs = NULL;
956   for (i = 0; dirs[i] != NULL; i++)
957     theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
958   
959   g_strfreev (dirs);
960   
961   theme->dirs = g_list_reverse (theme->dirs);
962
963   /* Prepend the finished theme */
964   priv->themes = g_list_prepend (priv->themes, theme);
965
966   themes = g_key_file_get_string_list (theme_file,
967                                        "Icon Theme",
968                                        "Inherits",
969                                        NULL,
970                                        NULL);
971   if (themes)
972     {
973       for (i = 0; themes[i] != NULL; i++)
974         insert_theme (icon_theme, themes[i]);
975       
976       g_strfreev (themes);
977     }
978
979   g_key_file_free (theme_file);
980 }
981
982 static void
983 free_unthemed_icon (UnthemedIcon *unthemed_icon)
984 {
985   if (unthemed_icon->svg_filename)
986     g_free (unthemed_icon->svg_filename);
987   if (unthemed_icon->no_svg_filename)
988     g_free (unthemed_icon->no_svg_filename);
989   g_free (unthemed_icon);
990 }
991
992 static void
993 load_themes (GtkIconTheme *icon_theme)
994 {
995   GtkIconThemePrivate *priv;
996   GDir *gdir;
997   int base;
998   char *dir, *base_name, *dot;
999   const char *file;
1000   char *abs_file;
1001   UnthemedIcon *unthemed_icon;
1002   IconSuffix old_suffix, new_suffix;
1003   GTimeVal tv;
1004   
1005   priv = icon_theme->priv;
1006
1007   priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
1008   
1009   insert_theme (icon_theme, priv->current_theme);
1010   
1011   /* Always look in the "default" icon theme */
1012   insert_theme (icon_theme, DEFAULT_THEME_NAME);
1013   priv->themes = g_list_reverse (priv->themes);
1014
1015
1016   priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
1017                                                 g_free, (GDestroyNotify)free_unthemed_icon);
1018
1019   for (base = 0; base < icon_theme->priv->search_path_len; base++)
1020     {
1021       GtkIconCache *cache;
1022       dir = icon_theme->priv->search_path[base];
1023
1024       cache = _gtk_icon_cache_new_for_path (dir);
1025
1026       if (cache != NULL)
1027         {
1028           priv->unthemed_icons_caches = g_list_prepend (priv->unthemed_icons_caches, cache);
1029
1030           continue;
1031         }
1032            
1033       gdir = g_dir_open (dir, 0, NULL);
1034
1035       if (gdir == NULL)
1036         continue;
1037       
1038       while ((file = g_dir_read_name (gdir)))
1039         {
1040           new_suffix = suffix_from_name (file);
1041
1042           if (new_suffix != ICON_SUFFIX_NONE)
1043             {
1044               abs_file = g_build_filename (dir, file, NULL);
1045
1046               base_name = g_strdup (file);
1047                   
1048               dot = strrchr (base_name, '.');
1049               if (dot)
1050                 *dot = 0;
1051
1052               if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
1053                                                         base_name)))
1054                 {
1055                   if (new_suffix == ICON_SUFFIX_SVG)
1056                     {
1057                       if (unthemed_icon->svg_filename)
1058                         g_free (abs_file);
1059                       else
1060                         unthemed_icon->svg_filename = abs_file;
1061                     }
1062                   else
1063                     {
1064                       if (unthemed_icon->no_svg_filename)
1065                         {
1066                           old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
1067                           if (new_suffix > old_suffix)
1068                             {
1069                               g_free (unthemed_icon->no_svg_filename);
1070                               unthemed_icon->no_svg_filename = abs_file;                              
1071                             }
1072                           else
1073                             g_free (abs_file);
1074                         }
1075                       else
1076                         unthemed_icon->no_svg_filename = abs_file;                            
1077                     }
1078
1079                   g_free (base_name);
1080                 }
1081               else
1082                 {
1083                   unthemed_icon = g_new0 (UnthemedIcon, 1);
1084                   
1085                   if (new_suffix == ICON_SUFFIX_SVG)
1086                     unthemed_icon->svg_filename = abs_file;
1087                   else
1088                     unthemed_icon->no_svg_filename = abs_file;
1089
1090                   g_hash_table_insert (priv->unthemed_icons,
1091                                        base_name,
1092                                        unthemed_icon);
1093                   g_hash_table_insert (priv->all_icons,
1094                                        base_name, NULL);
1095                 }
1096             }
1097         }
1098       g_dir_close (gdir);
1099     }
1100
1101   priv->themes_valid = TRUE;
1102   
1103   g_get_current_time(&tv);
1104   priv->last_stat_time = tv.tv_sec;
1105 }
1106
1107 static void
1108 ensure_valid_themes (GtkIconTheme *icon_theme)
1109 {
1110   GtkIconThemePrivate *priv = icon_theme->priv;
1111   GTimeVal tv;
1112   
1113   if (priv->themes_valid)
1114     {
1115       g_get_current_time (&tv);
1116
1117       if (ABS (tv.tv_sec - priv->last_stat_time) > 5)
1118         gtk_icon_theme_rescan_if_needed (icon_theme);
1119     }
1120   
1121   if (!priv->themes_valid)
1122     load_themes (icon_theme);
1123 }
1124
1125 /**
1126  * gtk_icon_theme_lookup_icon:
1127  * @icon_theme: a #GtkIconTheme
1128  * @icon_name: the name of the icon to lookup
1129  * @size: desired icon size
1130  * @flags: flags modifying the behavior of the icon lookup
1131  * 
1132  * Looks up a named icon and returns a structure containing
1133  * information such as the filename of the icon. The icon
1134  * can then be rendered into a pixbuf using
1135  * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1136  * combines these two steps if all you need is the pixbuf.)
1137  * 
1138  * Return value: a #GtkIconInfo structure containing information
1139  * about the icon, or %NULL if the icon wasn't found. Free with
1140  * gtk_icon_info_free()
1141  *
1142  * Since: 2.4
1143  **/
1144 GtkIconInfo *
1145 gtk_icon_theme_lookup_icon (GtkIconTheme       *icon_theme,
1146                             const gchar        *icon_name,
1147                             gint                size,
1148                             GtkIconLookupFlags  flags)
1149 {
1150   GtkIconThemePrivate *priv;
1151   GList *l;
1152   GtkIconInfo *icon_info = NULL;
1153   UnthemedIcon *unthemed_icon;
1154   gboolean allow_svg;
1155   gboolean use_builtin;
1156   gboolean found_default;
1157
1158   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1159   g_return_val_if_fail (icon_name != NULL, NULL);
1160   g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1161                         (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1162
1163   priv = icon_theme->priv;
1164
1165   if (flags & GTK_ICON_LOOKUP_NO_SVG)
1166     allow_svg = FALSE;
1167   else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
1168     allow_svg = TRUE;
1169   else
1170     allow_svg = priv->pixbuf_supports_svg;
1171
1172   use_builtin = (flags & GTK_ICON_LOOKUP_USE_BUILTIN);
1173
1174   ensure_valid_themes (icon_theme);
1175
1176   found_default = FALSE;
1177   l = priv->themes;
1178   while (l != NULL)
1179     {
1180       IconTheme *theme = l->data;
1181       
1182       if (strcmp (theme->name, DEFAULT_THEME_NAME) == 0)
1183         found_default = TRUE;
1184       
1185       icon_info = theme_lookup_icon (theme, icon_name, size, allow_svg, use_builtin);
1186       if (icon_info)
1187         goto out;
1188       
1189       l = l->next;
1190     }
1191
1192   if (!found_default)
1193     {
1194       BuiltinIcon *builtin = find_builtin_icon (icon_name, size, NULL, NULL);
1195       if (builtin)
1196         {
1197           icon_info = icon_info_new_builtin (builtin);
1198           goto out;
1199         }
1200     }
1201   
1202   unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_name);
1203   if (unthemed_icon)
1204     {
1205       icon_info = icon_info_new ();
1206
1207       /* A SVG icon, when allowed, beats out a XPM icon, but not
1208        * a PNG icon
1209        */
1210       if (allow_svg &&
1211           unthemed_icon->svg_filename &&
1212           (!unthemed_icon->no_svg_filename ||
1213            suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
1214         icon_info->filename = g_strdup (unthemed_icon->svg_filename);
1215       else if (unthemed_icon->no_svg_filename)
1216         icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);
1217 #ifdef G_OS_WIN32
1218       icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
1219                                                    -1, NULL, NULL, NULL);
1220 #endif
1221
1222       icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1223     }
1224
1225  out:
1226   if (icon_info)
1227     icon_info->desired_size = size;
1228   else
1229     {
1230       static gboolean check_for_default_theme = TRUE;
1231       char *default_theme_path;
1232       gboolean found = FALSE;
1233       unsigned i;
1234
1235       if (check_for_default_theme)
1236         {
1237           check_for_default_theme = FALSE;
1238
1239           for (i = 0; !found && i < priv->search_path_len; i++)
1240             {
1241               default_theme_path = g_build_filename (priv->search_path[i],
1242                                                      DEFAULT_THEME_NAME,
1243                                                      "index.theme",
1244                                                      NULL);
1245               found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
1246               g_free (default_theme_path);
1247             }
1248           if (!found)
1249             {
1250               g_warning (_("Could not find the icon '%s'. The '%s' theme\n"
1251                            "was not found either, perhaps you need to install it.\n"
1252                            "You can get a copy from:\n"
1253                            "\t%s"),
1254                          icon_name, DEFAULT_THEME_NAME, "http://freedesktop.org/Software/icon-theme/releases");
1255             }
1256         }
1257     }
1258
1259   return icon_info;
1260 }
1261
1262 /* Error quark */
1263 GQuark
1264 gtk_icon_theme_error_quark (void)
1265 {
1266   static GQuark q = 0;
1267   if (q == 0)
1268     q = g_quark_from_static_string ("gtk-icon-theme-error-quark");
1269
1270   return q;
1271 }
1272
1273 /**
1274  * gtk_icon_theme_load_icon:
1275  * @icon_theme: a #GtkIconTheme
1276  * @icon_name: the name of the icon to lookup
1277  * @size: the desired icon size. The resulting icon may not be
1278  *        exactly this size; see gtk_icon_info_load_icon().
1279  * @flags: flags modifying the behavior of the icon lookup
1280  * @error: Location to store error information on failure, or %NULL.
1281  * 
1282  * Looks up an icon in an icon theme, scales it to the given size
1283  * and renders it into a pixbuf. This is a convenience function;
1284  * if more details about the icon are needed, use
1285  * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1286  * 
1287  * Return value: the rendered icon; this may be a newly created icon
1288  *  or a new reference to an internal icon, so you must not modify
1289  *  the icon. Use g_object_unref() to release your reference to the
1290  *  icon. %NULL if the icon isn't found.
1291  *
1292  * Since: 2.4
1293  **/
1294 GdkPixbuf *
1295 gtk_icon_theme_load_icon (GtkIconTheme         *icon_theme,
1296                           const gchar          *icon_name,
1297                           gint                  size,
1298                           GtkIconLookupFlags    flags,
1299                           GError              **error)
1300 {
1301   GtkIconInfo *icon_info;
1302   GdkPixbuf *pixbuf = NULL;
1303   
1304   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1305   g_return_val_if_fail (icon_name != NULL, NULL);
1306   g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1307                         (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1308   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1309   
1310   icon_info  = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
1311                                            flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1312   if (!icon_info)
1313     {
1314       g_set_error (error, GTK_ICON_THEME_ERROR,  GTK_ICON_THEME_NOT_FOUND,
1315                    _("Icon '%s' not present in theme"), icon_name);
1316       return NULL;
1317     }
1318
1319   pixbuf = gtk_icon_info_load_icon (icon_info, error);
1320   gtk_icon_info_free (icon_info);
1321
1322   return pixbuf;
1323 }
1324
1325 /**
1326  * gtk_icon_theme_has_icon:
1327  * @icon_theme: a #GtkIconTheme
1328  * @icon_name: the name of an icon
1329  * 
1330  * Checks whether an icon theme includes an icon
1331  * for a particular name.
1332  * 
1333  * Return value: %TRUE if @icon_theme includes an
1334  *  icon for @icon_name.
1335  *
1336  * Since: 2.4
1337  **/
1338 gboolean 
1339 gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
1340                          const char   *icon_name)
1341 {
1342   GtkIconThemePrivate *priv;
1343   GList *l;
1344
1345   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1346   
1347   priv = icon_theme->priv;
1348   
1349   ensure_valid_themes (icon_theme);
1350
1351   for (l = priv->dir_mtimes; l; l = l->next)
1352     {
1353       IconThemeDirMtime *dir_mtime = l->data;
1354       GtkIconCache *cache = dir_mtime->cache;
1355       
1356       if (cache && _gtk_icon_cache_has_icon (cache, icon_name))
1357         return TRUE;
1358     }
1359
1360   for (l = priv->unthemed_icons_caches; l; l = l->next)
1361     {
1362       GtkIconCache *cache = (GtkIconCache *)l->data;
1363
1364       if (_gtk_icon_cache_has_icon (cache, icon_name))
1365         return TRUE;
1366     }
1367
1368   if (g_hash_table_lookup_extended (priv->all_icons,
1369                                     icon_name, NULL, NULL))
1370     return TRUE;
1371
1372   if (icon_theme_builtin_icons &&
1373       g_hash_table_lookup_extended (icon_theme_builtin_icons,
1374                                     icon_name, NULL, NULL))
1375     return TRUE;
1376
1377   return FALSE;
1378 }
1379
1380 static void
1381 add_size (gpointer  key,
1382           gpointer  value,
1383           gpointer  user_data)
1384 {
1385   gint **res_p = user_data;
1386
1387   **res_p = GPOINTER_TO_INT (key);
1388
1389   (*res_p)++;
1390 }
1391
1392 /**
1393  * gtk_icon_theme_get_icon_sizes:
1394  * @icon_theme: a #GtkIconTheme
1395  * @icon_name: the name of an icon
1396  * 
1397  * Returns an array of integers describing the sizes at which
1398  * the icon is available without scaling. A size of -1 means 
1399  * that the icon is available in a scalable format. The array 
1400  * is zero-terminated.
1401  * 
1402  * Return value: An newly allocated array describing the sizes at
1403  * which the icon is available. The array should be freed with g_free()
1404  * when it is no longer needed.
1405  *
1406  * Since: 2.6
1407  **/
1408 gint *
1409 gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
1410                                const char   *icon_name)
1411 {
1412   GList *l, *d;
1413   GHashTable *sizes;
1414   gint *result, *r;
1415   guint suffix;
1416   
1417   GtkIconThemePrivate *priv;
1418
1419   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1420   
1421   priv = icon_theme->priv;
1422
1423   ensure_valid_themes (icon_theme);
1424
1425   sizes = g_hash_table_new (g_direct_hash, g_direct_equal);
1426
1427   for (l = priv->themes; l; l = l->next)
1428     {
1429       IconTheme *theme = l->data;
1430       for (d = theme->dirs; d; d = d->next)
1431         {
1432           IconThemeDir *dir = d->data;
1433
1434           suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);      
1435           if (suffix != ICON_SUFFIX_NONE)
1436             {
1437               if (suffix == ICON_SUFFIX_SVG)
1438                 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1439               else
1440                 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1441             }
1442         }
1443     }
1444
1445   r = result = g_new0 (gint, g_hash_table_size (sizes) + 1);
1446
1447   g_hash_table_foreach (sizes, add_size, &r);
1448   g_hash_table_destroy (sizes);
1449   
1450   return result;
1451 }
1452
1453 static void
1454 add_key_to_hash (gpointer  key,
1455                  gpointer  value,
1456                  gpointer  user_data)
1457 {
1458   GHashTable *hash = user_data;
1459
1460   g_hash_table_insert (hash, key, NULL);
1461 }
1462
1463 static void
1464 add_key_to_list (gpointer  key,
1465                  gpointer  value,
1466                  gpointer  user_data)
1467 {
1468   GList **list = user_data;
1469
1470   *list = g_list_prepend (*list, g_strdup (key));
1471 }
1472
1473 /**
1474  * gtk_icon_theme_list_icons:
1475  * @icon_theme: a #GtkIconTheme
1476  * @context: a string identifying a particular type of icon,
1477  *           or %NULL to list all icons.
1478  * 
1479  * Lists the icons in the current icon theme. Only a subset
1480  * of the icons can be listed by providing a context string.
1481  * The set of values for the context string is system dependent,
1482  * but will typically include such values as 'apps' and
1483  * 'mimetypes'.
1484  * 
1485  * Return value: a #GList list holding the names of all the
1486  *  icons in the theme. You must first free each element
1487  *  in the list with g_free(), then free the list itself
1488  *  with g_list_free().
1489  *
1490  * Since: 2.4
1491  **/
1492 GList *
1493 gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
1494                            const char   *context)
1495 {
1496   GtkIconThemePrivate *priv;
1497   GHashTable *icons;
1498   GList *list, *l;
1499   GQuark context_quark;
1500   
1501   priv = icon_theme->priv;
1502   
1503   ensure_valid_themes (icon_theme);
1504
1505   if (context)
1506     {
1507       context_quark = g_quark_try_string (context);
1508
1509       if (!context_quark)
1510         return NULL;
1511     }
1512   else
1513     context_quark = 0;
1514
1515   icons = g_hash_table_new (g_str_hash, g_str_equal);
1516   
1517   l = priv->themes;
1518   while (l != NULL)
1519     {
1520       theme_list_icons (l->data, icons, context_quark);
1521       l = l->next;
1522     }
1523
1524   if (context_quark == 0)
1525     g_hash_table_foreach (priv->unthemed_icons,
1526                           add_key_to_hash,
1527                           icons);
1528
1529   list = NULL;
1530   
1531   g_hash_table_foreach (icons,
1532                         add_key_to_list,
1533                         &list);
1534
1535   g_hash_table_destroy (icons);
1536   
1537   return list;
1538 }
1539
1540 /**
1541  * gtk_icon_theme_get_example_icon_name:
1542  * @icon_theme: a #GtkIconTheme
1543  * 
1544  * Gets the name of an icon that is representative of the
1545  * current theme (for instance, to use when presenting
1546  * a list of themes to the user.)
1547  * 
1548  * Return value: the name of an example icon or %NULL.
1549  *  Free with g_free().
1550  *
1551  * Since: 2.4
1552  **/
1553 char *
1554 gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
1555 {
1556   GtkIconThemePrivate *priv;
1557   GList *l;
1558   IconTheme *theme;
1559
1560   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1561   
1562   priv = icon_theme->priv;
1563   
1564   ensure_valid_themes (icon_theme);
1565
1566   l = priv->themes;
1567   while (l != NULL)
1568     {
1569       theme = l->data;
1570       if (theme->example)
1571         return g_strdup (theme->example);
1572       
1573       l = l->next;
1574     }
1575   
1576   return NULL;
1577 }
1578
1579 /**
1580  * gtk_icon_theme_rescan_if_needed:
1581  * @icon_theme: a #GtkIconTheme
1582  * 
1583  * Checks to see if the icon theme has changed; if it has, any
1584  * currently cached information is discarded and will be reloaded
1585  * next time @icon_theme is accessed.
1586  * 
1587  * Return value: %TRUE if the icon theme has changed and needed
1588  *   to be reloaded.
1589  *
1590  * Since: 2.4
1591  **/
1592 gboolean
1593 gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
1594 {
1595   GtkIconThemePrivate *priv;
1596   IconThemeDirMtime *dir_mtime;
1597   GList *d;
1598   int stat_res;
1599   struct stat stat_buf;
1600   GTimeVal tv;
1601
1602   g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1603
1604   priv = icon_theme->priv;
1605   
1606   for (d = priv->dir_mtimes; d != NULL; d = d->next)
1607     {
1608       dir_mtime = d->data;
1609
1610       stat_res = g_stat (dir_mtime->dir, &stat_buf);
1611
1612       /* dir mtime didn't change */
1613       if (stat_res == 0 && 
1614           S_ISDIR (stat_buf.st_mode) &&
1615           dir_mtime->mtime == stat_buf.st_mtime)
1616         continue;
1617       /* didn't exist before, and still doesn't */
1618       if (dir_mtime->mtime == 0 &&
1619           (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
1620         continue;
1621           
1622       do_theme_change (icon_theme);
1623       return TRUE;
1624     }
1625   
1626   g_get_current_time (&tv);
1627   priv->last_stat_time = tv.tv_sec;
1628
1629   return FALSE;
1630 }
1631
1632 static void
1633 theme_destroy (IconTheme *theme)
1634 {
1635   g_free (theme->display_name);
1636   g_free (theme->comment);
1637   g_free (theme->name);
1638   g_free (theme->example);
1639
1640   g_list_foreach (theme->dirs, (GFunc)theme_dir_destroy, NULL);
1641   g_list_free (theme->dirs);
1642   
1643   g_free (theme);
1644 }
1645
1646 static void
1647 theme_dir_destroy (IconThemeDir *dir)
1648 {
1649   if (dir->cache)
1650       _gtk_icon_cache_unref (dir->cache);
1651   else
1652     g_hash_table_destroy (dir->icons);
1653   
1654   if (dir->icon_data)
1655     g_hash_table_destroy (dir->icon_data);
1656   g_free (dir->dir);
1657   g_free (dir->subdir);
1658   g_free (dir);
1659 }
1660
1661 static int
1662 theme_dir_size_difference (IconThemeDir *dir, int size, gboolean *smaller)
1663 {
1664   int min, max;
1665   switch (dir->type)
1666     {
1667     case ICON_THEME_DIR_FIXED:
1668       *smaller = size < dir->size;
1669       return abs (size - dir->size);
1670       break;
1671     case ICON_THEME_DIR_SCALABLE:
1672       *smaller = size < dir->min_size;
1673       if (size < dir->min_size)
1674         return dir->min_size - size;
1675       if (size > dir->max_size)
1676         return size - dir->max_size;
1677       return 0;
1678       break;
1679     case ICON_THEME_DIR_THRESHOLD:
1680       min = dir->size - dir->threshold;
1681       max = dir->size + dir->threshold;
1682       *smaller = size < min;
1683       if (size < min)
1684         return min - size;
1685       if (size > max)
1686         return size - max;
1687       return 0;
1688       break;
1689     case ICON_THEME_DIR_UNTHEMED:
1690       g_assert_not_reached ();
1691       break;
1692     }
1693   g_assert_not_reached ();
1694   return 1000;
1695 }
1696
1697 static const char *
1698 string_from_suffix (IconSuffix suffix)
1699 {
1700   switch (suffix)
1701     {
1702     case ICON_SUFFIX_XPM:
1703       return ".xpm";
1704     case ICON_SUFFIX_SVG:
1705       return ".svg";
1706     case ICON_SUFFIX_PNG:
1707       return ".png";
1708     default:
1709       g_assert_not_reached();
1710     }
1711   return NULL;
1712 }
1713
1714 static IconSuffix
1715 suffix_from_name (const char *name)
1716 {
1717   IconSuffix retval;
1718
1719   if (g_str_has_suffix (name, ".png"))
1720     retval = ICON_SUFFIX_PNG;
1721   else if (g_str_has_suffix (name, ".svg"))
1722     retval = ICON_SUFFIX_SVG;
1723   else if (g_str_has_suffix (name, ".xpm"))
1724     retval = ICON_SUFFIX_XPM;
1725   else
1726     retval = ICON_SUFFIX_NONE;
1727
1728   return retval;
1729 }
1730
1731 static IconSuffix
1732 best_suffix (IconSuffix suffix,
1733              gboolean   allow_svg)
1734 {
1735   if ((suffix & ICON_SUFFIX_PNG) != 0)
1736     return ICON_SUFFIX_PNG;
1737   else if (allow_svg && ((suffix & ICON_SUFFIX_SVG) != 0))
1738     return ICON_SUFFIX_SVG;
1739   else if ((suffix & ICON_SUFFIX_XPM) != 0)
1740     return ICON_SUFFIX_XPM;
1741   else
1742     return ICON_SUFFIX_NONE;
1743 }
1744
1745
1746 static IconSuffix
1747 theme_dir_get_icon_suffix (IconThemeDir *dir,
1748                            const gchar  *icon_name,
1749                            gboolean     *has_icon_file)
1750 {
1751   IconSuffix suffix;
1752
1753   if (dir->cache)
1754     {
1755       suffix = (IconSuffix)_gtk_icon_cache_get_icon_flags (dir->cache,
1756                                                            icon_name,
1757                                                            dir->subdir);
1758
1759       if (has_icon_file)
1760         *has_icon_file = suffix & HAS_ICON_FILE;
1761
1762       suffix = suffix & ~HAS_ICON_FILE;
1763     }
1764   else
1765       suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
1766
1767   return suffix;
1768 }
1769
1770 static GtkIconInfo *
1771 theme_lookup_icon (IconTheme          *theme,
1772                    const char         *icon_name,
1773                    int                 size,
1774                    gboolean            allow_svg,
1775                    gboolean            use_builtin)
1776 {
1777   GList *l;
1778   IconThemeDir *dir, *min_dir;
1779   char *file;
1780   int min_difference, difference;
1781   BuiltinIcon *closest_builtin = NULL;
1782   gboolean smaller, has_larger;
1783   IconSuffix suffix;
1784
1785   min_difference = G_MAXINT;
1786   min_dir = NULL;
1787   has_larger = FALSE;
1788
1789   /* Builtin icons are logically part of the default theme and
1790    * are searched before other subdirectories of the default theme.
1791    */
1792   if (strcmp (theme->name, DEFAULT_THEME_NAME) == 0 && use_builtin)
1793     {
1794       closest_builtin = find_builtin_icon (icon_name, size,
1795                                            &min_difference,
1796                                            &has_larger);
1797
1798       if (min_difference == 0)
1799         return icon_info_new_builtin (closest_builtin);
1800     }
1801
1802   l = theme->dirs;
1803   while (l != NULL)
1804     {
1805       dir = l->data;
1806
1807       suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1808       if (best_suffix (suffix, allow_svg) != ICON_SUFFIX_NONE)
1809         {
1810           difference = theme_dir_size_difference (dir, size, &smaller);
1811
1812           if (difference == 0)
1813             {
1814               min_dir = dir;
1815               break;
1816             }
1817
1818           if (!has_larger)
1819             {
1820               if (difference < min_difference || smaller)
1821                 {
1822                   min_difference = difference;
1823                   min_dir = dir;
1824                   closest_builtin = NULL;
1825                   has_larger = smaller;
1826                 }
1827             }
1828           else
1829             {
1830               if (difference < min_difference && smaller)
1831                 {
1832                   min_difference = difference;
1833                   min_dir = dir;
1834                   closest_builtin = NULL;
1835                 }
1836             }
1837
1838         }
1839
1840       l = l->next;
1841     }
1842
1843   if (closest_builtin)
1844     return icon_info_new_builtin (closest_builtin);
1845   
1846   if (min_dir)
1847     {
1848       GtkIconInfo *icon_info = icon_info_new ();
1849       gboolean has_icon_file;
1850       
1851       suffix = theme_dir_get_icon_suffix (min_dir, icon_name, &has_icon_file);
1852       suffix = best_suffix (suffix, allow_svg);
1853       g_assert (suffix != ICON_SUFFIX_NONE);
1854       
1855       file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
1856       icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
1857       g_free (file);
1858 #ifdef G_OS_WIN32
1859       icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
1860                                                    -1, NULL, NULL, NULL);
1861 #endif
1862       
1863       if (min_dir->icon_data != NULL)
1864         icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
1865       else if (min_dir->cache != NULL)
1866         icon_info->data = _gtk_icon_cache_get_icon_data (min_dir->cache, icon_name, min_dir->subdir);
1867
1868       if (icon_info->data == NULL &&
1869           min_dir->cache && has_icon_file)
1870         {
1871           gchar *icon_file_name, *icon_file_path;
1872
1873           icon_file_name = g_strconcat (icon_name, ".icon", NULL);
1874           icon_file_path = g_build_filename (min_dir->dir, icon_file_name, NULL);
1875
1876           if (g_file_test (icon_file_path, G_FILE_TEST_IS_REGULAR))
1877             {
1878               if (min_dir->icon_data == NULL)
1879                 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
1880                                                             g_free, (GDestroyNotify)icon_data_free);
1881               load_icon_data (min_dir, icon_file_path, icon_file_name);
1882               
1883               icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
1884             }
1885           g_free (icon_file_name);
1886           g_free (icon_file_path);
1887         }
1888
1889       if (min_dir->cache)
1890         {
1891           icon_info->cache_pixbuf = _gtk_icon_cache_get_icon (min_dir->cache, icon_name,
1892                                                               min_dir->subdir);
1893         }
1894
1895       icon_info->dir_type = min_dir->type;
1896       icon_info->dir_size = min_dir->size;
1897       icon_info->threshold = min_dir->threshold;
1898       
1899       return icon_info;
1900     }
1901  
1902   return NULL;
1903 }
1904
1905 static void
1906 theme_list_icons (IconTheme *theme, GHashTable *icons,
1907                   GQuark context)
1908 {
1909   GList *l = theme->dirs;
1910   IconThemeDir *dir;
1911   
1912   while (l != NULL)
1913     {
1914       dir = l->data;
1915
1916       if (context == dir->context ||
1917           context == 0)
1918         {
1919           if (dir->cache)
1920             {
1921               _gtk_icon_cache_add_icons (dir->cache,
1922                                          dir->subdir,
1923                                          icons);
1924                                          
1925             }
1926           else
1927             {
1928               g_hash_table_foreach (dir->icons,
1929                                     add_key_to_hash,
1930                                     icons);
1931             }
1932         }
1933       l = l->next;
1934     }
1935 }
1936
1937 static void
1938 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
1939 {
1940   GKeyFile *icon_file;
1941   char *base_name;
1942   char **split;
1943   gsize length;
1944   char *dot;
1945   char *str;
1946   char *split_point;
1947   int i;
1948   gint *ivalues;
1949   GError *error = NULL;
1950   
1951   GtkIconData *data;
1952
1953   icon_file = g_key_file_new ();
1954   g_key_file_set_list_separator (icon_file, ',');
1955   g_key_file_load_from_file (icon_file, path, 0, &error);
1956   if (error)
1957     {
1958       g_error_free (error);
1959       return;
1960     }
1961   else
1962     {
1963       base_name = g_strdup (name);
1964       dot = strrchr (base_name, '.');
1965       *dot = 0;
1966       
1967       data = g_new0 (GtkIconData, 1);
1968       g_hash_table_replace (dir->icon_data, base_name, data);
1969       
1970       ivalues = g_key_file_get_integer_list (icon_file, 
1971                                              "Icon Data", "EmbeddedTextRectangle",
1972                                               &length, NULL);
1973       if (ivalues)
1974         {
1975           if (length == 4)
1976             {
1977               data->has_embedded_rect = TRUE;
1978               data->x0 = ivalues[0];
1979               data->y0 = ivalues[1];
1980               data->x1 = ivalues[2];
1981               data->y1 = ivalues[3];
1982             }
1983           
1984           g_free (ivalues);
1985         }
1986       
1987       str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
1988       if (str)
1989         {
1990           split = g_strsplit (str, "|", -1);
1991           
1992           data->n_attach_points = g_strv_length (split);
1993           data->attach_points = g_malloc (sizeof (GdkPoint) * data->n_attach_points);
1994
1995           i = 0;
1996           while (split[i] != NULL && i < data->n_attach_points)
1997             {
1998               split_point = strchr (split[i], ',');
1999               if (split_point)
2000                 {
2001                   *split_point = 0;
2002                   split_point++;
2003                   data->attach_points[i].x = atoi (split[i]);
2004                   data->attach_points[i].y = atoi (split_point);
2005                 }
2006               i++;
2007             }
2008           
2009           g_strfreev (split);
2010           g_free (str);
2011         }
2012       
2013       data->display_name = g_key_file_get_locale_string (icon_file, 
2014                                                          "Icon Data", "DisplayName",
2015                                                          NULL, NULL);
2016       g_key_file_free (icon_file);
2017     }
2018 }
2019
2020 static void
2021 scan_directory (GtkIconThemePrivate *icon_theme,
2022                 IconThemeDir *dir, char *full_dir)
2023 {
2024   GDir *gdir;
2025   const char *name;
2026   char *base_name, *dot;
2027   char *path;
2028   IconSuffix suffix, hash_suffix;
2029
2030   GTK_NOTE (ICONTHEME, 
2031             g_print ("scanning directory %s\n", full_dir));
2032   dir->icons = g_hash_table_new_full (g_str_hash, g_str_equal,
2033                                       g_free, NULL);
2034   
2035   gdir = g_dir_open (full_dir, 0, NULL);
2036
2037   if (gdir == NULL)
2038     return;
2039
2040   while ((name = g_dir_read_name (gdir)))
2041     {
2042       if (g_str_has_suffix (name, ".icon"))
2043         {
2044           if (dir->icon_data == NULL)
2045             dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2046                                                     g_free, (GDestroyNotify)icon_data_free);
2047           
2048           path = g_build_filename (full_dir, name, NULL);
2049           if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
2050             load_icon_data (dir, path, name);
2051           
2052           g_free (path);
2053           
2054           continue;
2055         }
2056
2057       suffix = suffix_from_name (name);
2058       if (suffix == ICON_SUFFIX_NONE)
2059         continue;
2060       
2061       base_name = g_strdup (name);
2062       dot = strrchr (base_name, '.');
2063       *dot = 0;
2064       
2065       hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
2066       g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
2067       g_hash_table_insert (icon_theme->all_icons, base_name, NULL);
2068     }
2069   
2070   g_dir_close (gdir);
2071 }
2072
2073 static void
2074 theme_subdir_load (GtkIconTheme *icon_theme,
2075                    IconTheme    *theme,
2076                    GKeyFile     *theme_file,
2077                    char         *subdir)
2078 {
2079   GList *d;
2080   char *type_string;
2081   IconThemeDir *dir;
2082   IconThemeDirType type;
2083   char *context_string;
2084   GQuark context;
2085   int size;
2086   int min_size;
2087   int max_size;
2088   int threshold;
2089   char *full_dir;
2090   GError *error = NULL;
2091   IconThemeDirMtime *dir_mtime;
2092
2093   size = g_key_file_get_integer (theme_file, subdir, "Size", &error);
2094   if (error)
2095     {
2096       g_error_free (error);
2097       g_warning ("Theme directory %s of theme %s has no size field\n", 
2098                  subdir, theme->name);
2099       return;
2100     }
2101   
2102   type = ICON_THEME_DIR_THRESHOLD;
2103   type_string = g_key_file_get_string (theme_file, subdir, "Type", NULL);
2104   if (type_string)
2105     {
2106       if (strcmp (type_string, "Fixed") == 0)
2107         type = ICON_THEME_DIR_FIXED;
2108       else if (strcmp (type_string, "Scalable") == 0)
2109         type = ICON_THEME_DIR_SCALABLE;
2110       else if (strcmp (type_string, "Threshold") == 0)
2111         type = ICON_THEME_DIR_THRESHOLD;
2112
2113       g_free (type_string);
2114     }
2115   
2116   context = 0;
2117   context_string = g_key_file_get_string (theme_file, subdir, "Context", NULL);
2118   if (context_string)
2119     {
2120       context = g_quark_from_string (context_string);
2121       g_free (context_string);
2122     }
2123
2124   max_size = g_key_file_get_integer (theme_file, subdir, "MaxSize", &error);
2125   if (error)
2126     {
2127       max_size = size;
2128
2129       g_error_free (error);
2130       error = NULL;
2131     }
2132
2133   min_size = g_key_file_get_integer (theme_file, subdir, "MinSize", &error);
2134   if (error)
2135     {
2136       min_size = size;
2137
2138       g_error_free (error);
2139       error = NULL;
2140     }
2141   
2142   threshold = g_key_file_get_integer (theme_file, subdir, "Threshold", &error);
2143   if (error)
2144     {
2145       threshold = 2;
2146
2147       g_error_free (error);
2148       error = NULL;
2149     }
2150
2151   for (d = icon_theme->priv->dir_mtimes; d; d = d->next)
2152     {
2153       dir_mtime = (IconThemeDirMtime *)d->data;
2154
2155       if (dir_mtime->mtime == 0)
2156         continue; /* directory doesn't exist */
2157
2158        full_dir = g_build_filename (dir_mtime->dir, subdir, NULL);
2159
2160       /* First, see if we have a cache for the directory */
2161       if (dir_mtime->cache != NULL || g_file_test (full_dir, G_FILE_TEST_IS_DIR))
2162         {
2163           if (dir_mtime->cache == NULL)
2164             {
2165               /* This will return NULL if the cache doesn't exist or is outdated */
2166               dir_mtime->cache = _gtk_icon_cache_new_for_path (dir_mtime->dir);
2167             }
2168           
2169           dir = g_new (IconThemeDir, 1);
2170           dir->type = type;
2171           dir->context = context;
2172           dir->size = size;
2173           dir->min_size = min_size;
2174           dir->max_size = max_size;
2175           dir->threshold = threshold;
2176           dir->dir = full_dir;
2177           dir->icon_data = NULL;
2178           dir->subdir = g_strdup (subdir);
2179           if (dir_mtime->cache != NULL)
2180             dir->cache = _gtk_icon_cache_ref (dir_mtime->cache);
2181           else
2182             {
2183               dir->cache = NULL;
2184               scan_directory (icon_theme->priv, dir, full_dir);
2185             }
2186
2187           theme->dirs = g_list_prepend (theme->dirs, dir);
2188         }
2189       else
2190         g_free (full_dir);
2191     }
2192 }
2193
2194 static void
2195 icon_data_free (GtkIconData *icon_data)
2196 {
2197   g_free (icon_data->attach_points);
2198   g_free (icon_data->display_name);
2199   g_free (icon_data);
2200 }
2201
2202 /*
2203  * GtkIconInfo
2204  */
2205 GType
2206 gtk_icon_info_get_type (void)
2207 {
2208   static GType our_type = 0;
2209   
2210   if (our_type == 0)
2211     our_type = g_boxed_type_register_static ("GtkIconInfo",
2212                                              (GBoxedCopyFunc) gtk_icon_info_copy,
2213                                              (GBoxedFreeFunc) gtk_icon_info_free);
2214
2215   return our_type;
2216 }
2217
2218 static GtkIconInfo *
2219 icon_info_new (void)
2220 {
2221   GtkIconInfo *icon_info = g_new0 (GtkIconInfo, 1);
2222
2223   icon_info->scale = -1.;
2224
2225   return icon_info;
2226 }
2227
2228 static GtkIconInfo *
2229 icon_info_new_builtin (BuiltinIcon *icon)
2230 {
2231   GtkIconInfo *icon_info = icon_info_new ();
2232
2233   icon_info->builtin_pixbuf = g_object_ref (icon->pixbuf);
2234   icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
2235   icon_info->dir_size = icon->size;
2236   icon_info->threshold = 2;
2237   
2238   return icon_info;
2239 }
2240
2241 /**
2242  * gtk_icon_info_copy:
2243  * @icon_info: a #GtkIconInfo
2244  * 
2245  * Make a copy of a #GtkIconInfo.
2246  * 
2247  * Return value: the new GtkIconInfo
2248  *
2249  * Since: 2.4
2250  **/
2251 GtkIconInfo *
2252 gtk_icon_info_copy (GtkIconInfo *icon_info)
2253 {
2254   GtkIconInfo *copy;
2255   
2256   g_return_val_if_fail (icon_info != NULL, NULL);
2257
2258   copy = g_memdup (icon_info, sizeof (GtkIconInfo));
2259   if (copy->builtin_pixbuf)
2260     g_object_ref (copy->builtin_pixbuf);
2261   if (copy->pixbuf)
2262     g_object_ref (copy->pixbuf);
2263   if (copy->load_error)
2264     copy->load_error = g_error_copy (copy->load_error);
2265   if (copy->filename)
2266     copy->filename = g_strdup (copy->filename);
2267 #ifdef G_OS_WIN32
2268   if (copy->cp_filename)
2269     copy->cp_filename = g_strdup (copy->cp_filename);
2270 #endif
2271
2272   return copy;
2273 }
2274
2275 /**
2276  * gtk_icon_info_free:
2277  * @icon_info: a #GtkIconInfo
2278  * 
2279  * Free a #GtkIconInfo and associated information
2280  *
2281  * Since: 2.4
2282  **/
2283 void
2284 gtk_icon_info_free (GtkIconInfo *icon_info)
2285 {
2286   g_return_if_fail (icon_info != NULL);
2287
2288   if (icon_info->filename)
2289     g_free (icon_info->filename);
2290 #ifdef G_OS_WIN32
2291   if (icon_info->cp_filename)
2292     g_free (icon_info->cp_filename);
2293 #endif
2294   if (icon_info->builtin_pixbuf)
2295     g_object_unref (icon_info->builtin_pixbuf);
2296   if (icon_info->pixbuf)
2297     g_object_unref (icon_info->pixbuf);
2298   if (icon_info->cache_pixbuf)
2299     g_object_unref (icon_info->cache_pixbuf);
2300   
2301   g_free (icon_info);
2302 }
2303
2304 /**
2305  * gtk_icon_info_get_base_size:
2306  * @icon_info: a #GtkIconInfo
2307  * 
2308  * Gets the base size for the icon. The base size
2309  * is a size for the icon that was specified by
2310  * the icon theme creator. This may be different
2311  * than the actual size of image; an example of
2312  * this is small emblem icons that can be attached
2313  * to a larger icon. These icons will be given
2314  * the same base size as the larger icons to which
2315  * they are attached.
2316  * 
2317  * Return value: the base size, or 0, if no base
2318  *  size is known for the icon.
2319  *
2320  * Since: 2.4
2321  **/
2322 gint
2323 gtk_icon_info_get_base_size (GtkIconInfo *icon_info)
2324 {
2325   g_return_val_if_fail (icon_info != NULL, 0);
2326
2327   return icon_info->dir_size;
2328 }
2329
2330 /**
2331  * gtk_icon_info_get_filename:
2332  * @icon_info: a #GtkIconInfo
2333  * 
2334  * Gets the filename for the icon. If the
2335  * %GTK_ICON_LOOKUP_USE_BUILTIN flag was passed
2336  * to gtk_icon_theme_lookup_icon(), there may be
2337  * no filename if a builtin icon is returned; in this
2338  * case, you should use gtk_icon_info_get_builtin_pixbuf().
2339  * 
2340  * Return value: the filename for the icon, or %NULL
2341  *  if gtk_icon_info_get_builtin_pixbuf() should
2342  *  be used instead. The return value is owned by
2343  *  GTK+ and should not be modified or freed.
2344  *
2345  * Since: 2.4
2346  **/
2347 G_CONST_RETURN gchar *
2348 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2349 {
2350   g_return_val_if_fail (icon_info != NULL, NULL);
2351
2352   return icon_info->filename;
2353 }
2354
2355 /**
2356  * gtk_icon_info_get_builtin_pixbuf:
2357  * @icon_info: a #GtkIconInfo structure
2358  * 
2359  * Gets the built-in image for this icon, if any. To allow
2360  * GTK+ to use built in icon images, you must pass the
2361  * %GTK_ICON_LOOKUP_USE_BUILTIN to
2362  * gtk_icon_theme_lookup_icon().
2363  * 
2364  * Return value: the built-in image pixbuf, or %NULL. No
2365  *  extra reference is added to the returned pixbuf, so if
2366  *  you want to keep it around, you must use g_object_ref().
2367  *  The returned image must not be modified.
2368  *
2369  * Since: 2.4
2370  **/
2371 GdkPixbuf *
2372 gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
2373 {
2374   g_return_val_if_fail (icon_info != NULL, NULL);
2375
2376   return icon_info->builtin_pixbuf;
2377 }
2378
2379 static GdkPixbuf *
2380 load_svg_at_size (const gchar *filename,
2381                   gint         size,
2382                   GError      **error)
2383 {
2384   GdkPixbuf *pixbuf = NULL;
2385   GdkPixbufLoader *loader = NULL;
2386   gchar *contents = NULL;
2387   gsize length;
2388   
2389   if (!g_file_get_contents (filename,
2390                             &contents, &length, error))
2391     goto bail;
2392   
2393   loader = gdk_pixbuf_loader_new ();
2394   gdk_pixbuf_loader_set_size (loader, size, size);
2395   
2396   if (!gdk_pixbuf_loader_write (loader, contents, length, error))
2397     {
2398       gdk_pixbuf_loader_close (loader, NULL);
2399       goto bail;
2400     }
2401   
2402   if (!gdk_pixbuf_loader_close (loader, error))
2403     goto bail;
2404   
2405   pixbuf = g_object_ref (gdk_pixbuf_loader_get_pixbuf (loader));
2406   
2407  bail:
2408   if (loader)
2409     g_object_unref (loader);
2410   if (contents)
2411     g_free (contents);
2412   
2413   return pixbuf;
2414 }
2415
2416 /* This function contains the complicatd logic for deciding
2417  * on the size at which to load the icon and loading it at
2418  * that size.
2419  */
2420 static gboolean
2421 icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
2422                                    gboolean     scale_only)
2423 {
2424   int image_width, image_height;
2425   GdkPixbuf *source_pixbuf;
2426
2427   /* First check if we already succeeded have the necessary
2428    * information (or failed earlier)
2429    */
2430   if (scale_only && icon_info->scale >= 0)
2431     return TRUE;
2432
2433   if (icon_info->pixbuf)
2434     return TRUE;
2435
2436   if (icon_info->load_error)
2437     return FALSE;
2438
2439   /* SVG icons are a special case - we just immediately scale them
2440    * to the desired size
2441    */
2442   if (icon_info->filename && g_str_has_suffix (icon_info->filename, ".svg"))
2443     {
2444       icon_info->scale = icon_info->desired_size / 1000.;
2445
2446       if (scale_only)
2447         return TRUE;
2448       
2449       icon_info->pixbuf = load_svg_at_size (icon_info->filename,
2450                                             icon_info->desired_size,
2451                                             &icon_info->load_error);
2452
2453       return icon_info->pixbuf != NULL;
2454     }
2455
2456   /* In many cases, the scale can be determined without actual access
2457    * to the icon file. This is generally true when we have a size
2458    * for the directory where the icon is; the image size doesn't
2459    * matter in that case.
2460    */
2461   if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
2462     icon_info->scale = 1.0;
2463   else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
2464     {
2465       if (icon_info->desired_size >= icon_info->dir_size - icon_info->threshold &&
2466           icon_info->desired_size <= icon_info->dir_size + icon_info->threshold)
2467         icon_info->scale = 1.0;
2468       else if (icon_info->dir_size > 0)
2469         icon_info->scale =(gdouble) icon_info->desired_size / icon_info->dir_size;
2470     }
2471   else if (icon_info->dir_type == ICON_THEME_DIR_SCALABLE)
2472     {
2473       if (icon_info->dir_size > 0)
2474         icon_info->scale = (gdouble) icon_info->desired_size / icon_info->dir_size;
2475     }
2476
2477   if (icon_info->scale >= 0. && scale_only)
2478     return TRUE;
2479
2480   /* At this point, we need to actually get the icon; either from the
2481    * builting image or by loading the file
2482    */
2483   if (icon_info->builtin_pixbuf)
2484     source_pixbuf = g_object_ref (icon_info->builtin_pixbuf);
2485   else if (icon_info->cache_pixbuf)
2486     source_pixbuf = g_object_ref (icon_info->cache_pixbuf);
2487   else
2488     {
2489
2490       source_pixbuf = gdk_pixbuf_new_from_file (icon_info->filename,
2491                                                 &icon_info->load_error);
2492       if (!source_pixbuf)
2493         return FALSE;
2494     }
2495
2496   /* Do scale calculations that depend on the image size
2497    */
2498   image_width = gdk_pixbuf_get_width (source_pixbuf);
2499   image_height = gdk_pixbuf_get_height (source_pixbuf);
2500
2501   if (icon_info->scale < 0.0)
2502     {
2503       gint image_size = MAX (image_width, image_height);
2504       if (image_size > 0)
2505         icon_info->scale = icon_info->desired_size / (gdouble)image_size;
2506       else
2507         icon_info->scale = 1.0;
2508       
2509       if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED)
2510         icon_info->scale = MIN (icon_info->scale, 1.0);
2511     }
2512
2513   /* We don't short-circuit out here for scale_only, since, now
2514    * we've loaded the icon, we might as well go ahead and finish
2515    * the job. This is a bit of a waste when we scale here
2516    * and never get the final pixbuf; at the cost of a bit of
2517    * extra complexity, we could keep the source pixbuf around
2518    * but not actually scale it until neede.
2519    */
2520     
2521   if (icon_info->scale == 1.0)
2522     icon_info->pixbuf = source_pixbuf;
2523   else
2524     {
2525       icon_info->pixbuf = gdk_pixbuf_scale_simple (source_pixbuf,
2526                                                    0.5 + image_width * icon_info->scale,
2527                                                    0.5 + image_height * icon_info->scale,
2528                                                    GDK_INTERP_BILINEAR);
2529       g_object_unref (source_pixbuf);
2530     }
2531
2532   return TRUE;
2533 }
2534
2535 /**
2536  * gtk_icon_info_load_icon:
2537  * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
2538  * @error: location to store error information on failure, or %NULL.
2539  * 
2540  * Renders an icon previously looked up in an icon theme using
2541  * gtk_icon_theme_lookup_icon(); the size will be based on the size
2542  * passed to gtk_icon_theme_lookup_icon(). Note that the resulting
2543  * pixbuf may not be exactly this size; an icon theme may have icons
2544  * that differ slightly from their nominal sizes, and in addition GTK+
2545  * will avoid scaling icons that it considers sufficiently close to the
2546  * requested size or for which the source image would have to be scaled
2547  * up too far. (This maintains sharpness.) 
2548  * 
2549  * Return value: the rendered icon; this may be a newly created icon
2550  *  or a new reference to an internal icon, so you must not modify
2551  *  the icon. Use g_object_unref() to release your reference to the
2552  *  icon.
2553  *
2554  * Since: 2.4
2555  **/
2556 GdkPixbuf *
2557 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
2558                          GError     **error)
2559 {
2560   g_return_val_if_fail (icon_info != NULL, NULL);
2561
2562   g_return_val_if_fail (icon_info != NULL, NULL);
2563   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2564
2565   icon_info_ensure_scale_and_pixbuf (icon_info, FALSE);
2566
2567   if (icon_info->load_error)
2568     {
2569       g_propagate_error (error, icon_info->load_error);
2570       return NULL;
2571     }
2572
2573   return g_object_ref (icon_info->pixbuf);
2574 }
2575
2576 /**
2577  * gtk_icon_info_set_raw_coordinates:
2578  * @icon_info: a #GtkIconInfo
2579  * @raw_coordinates: whether the coordinates of embedded rectangles
2580  *   and attached points should be returned in their original
2581  *   (unscaled) form.
2582  * 
2583  * Sets whether the coordinates returned by gtk_icon_info_get_embedded_rect()
2584  * and gtk_icon_info_get_attach_points() should be returned in their
2585  * original form as specified in the icon theme, instead of scaled
2586  * appropriately for the pixbuf returned by gtk_icon_info_load_icon().
2587  *
2588  * Raw coordinates are somewhat strange; they are specified to be with
2589  * respect to the unscaled pixmap for PNG and XPM icons, but for SVG
2590  * icons, they are in a 1000x1000 coordinate space that is scaled
2591  * to the final size of the icon.  You can determine if the icon is an SVG
2592  * icon by using gtk_icon_info_get_filename(), and seeing if it is non-%NULL
2593  * and ends in '.svg'.
2594  *
2595  * This function is provided primarily to allow compatibility wrappers
2596  * for older API's, and is not expected to be useful for applications.
2597  *
2598  * Since: 2.4
2599  **/
2600 void
2601 gtk_icon_info_set_raw_coordinates (GtkIconInfo *icon_info,
2602                                    gboolean     raw_coordinates)
2603 {
2604   g_return_if_fail (icon_info != NULL);
2605   
2606   icon_info->raw_coordinates = raw_coordinates != FALSE;
2607 }
2608
2609 /* Scale coordinates from the icon data prior to returning
2610  * them to the user.
2611  */
2612 static gboolean
2613 icon_info_scale_point (GtkIconInfo  *icon_info,
2614                        gint          x,
2615                        gint          y,
2616                        gint         *x_out,
2617                        gint         *y_out)
2618 {
2619   if (icon_info->raw_coordinates)
2620     {
2621       *x_out = x;
2622       *y_out = y;
2623     }
2624   else
2625     {
2626       if (!icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2627         return FALSE;
2628
2629       *x_out = 0.5 + x * icon_info->scale;
2630       *y_out = 0.5 + y * icon_info->scale;
2631     }
2632
2633   return TRUE;
2634 }
2635
2636 /**
2637  * gtk_icon_info_get_embedded_rect:
2638  * @icon_info: a #GtkIconInfo
2639  * @rectangle: #GdkRectangle in which to store embedded
2640  *   rectangle coordinates; coordinates are only stored
2641  *   when this function returns %TRUE.
2642  *
2643  * Gets the coordinates of a rectangle within the icon
2644  * that can be used for display of information such
2645  * as a preview of the contents of a text file.
2646  * See gtk_icon_info_set_raw_coordinates() for further
2647  * information about the coordinate system.
2648  * 
2649  * Return value: %TRUE if the icon has an embedded rectangle
2650  *
2651  * Since: 2.4
2652  **/
2653 gboolean
2654 gtk_icon_info_get_embedded_rect (GtkIconInfo  *icon_info,
2655                                  GdkRectangle *rectangle)
2656 {
2657   g_return_val_if_fail (icon_info != NULL, FALSE);
2658
2659   if (icon_info->data && icon_info->data->has_embedded_rect &&
2660       icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2661     {
2662       gint scaled_x0, scaled_y0;
2663       gint scaled_x1, scaled_y1;
2664       
2665       if (rectangle)
2666         {
2667           icon_info_scale_point (icon_info,
2668                                  icon_info->data->x0, icon_info->data->y0,
2669                                  &scaled_x0, &scaled_y0);
2670           icon_info_scale_point (icon_info,
2671                                  icon_info->data->x1, icon_info->data->y1,
2672                                  &scaled_x1, &scaled_y1);
2673           
2674           rectangle->x = scaled_x0;
2675           rectangle->y = scaled_y0;
2676           rectangle->width = scaled_x1 - rectangle->x;
2677           rectangle->height = scaled_y1 - rectangle->y;
2678         }
2679
2680       return TRUE;
2681     }
2682   else
2683     return FALSE;
2684 }
2685
2686 /**
2687  * gtk_icon_info_get_attach_points:
2688  * @icon_info: a #GtkIconInfo
2689  * @points: location to store pointer to an array of points, or %NULL
2690  *          free the array of points with g_free().
2691  * @n_points: location to store the number of points in @points, or %NULL
2692  * 
2693  * Fetches the set of attach points for an icon. An attach point
2694  * is a location in the icon that can be used as anchor points for attaching
2695  * emblems or overlays to the icon.
2696  * 
2697  * Return value: %TRUE if there are any attach points for the icon.
2698  *
2699  * Since: 2.4
2700  **/
2701 gboolean
2702 gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
2703                                  GdkPoint   **points,
2704                                  gint        *n_points)
2705 {
2706   g_return_val_if_fail (icon_info != NULL, FALSE);
2707   
2708   if (icon_info->data && icon_info->data->n_attach_points &&
2709       icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2710     {
2711       if (points)
2712         {
2713           gint i;
2714           
2715           *points = g_new (GdkPoint, icon_info->data->n_attach_points);
2716           for (i = 0; i < icon_info->data->n_attach_points; i++)
2717             icon_info_scale_point (icon_info,
2718                                    icon_info->data->attach_points[i].x,
2719                                    icon_info->data->attach_points[i].y,
2720                                    &(*points)[i].x,
2721                                    &(*points)[i].y);
2722         }
2723           
2724       if (n_points)
2725         *n_points = icon_info->data->n_attach_points;
2726
2727       return TRUE;
2728     }
2729   else
2730     {
2731       if (points)
2732         *points = NULL;
2733       if (n_points)
2734         *n_points = 0;
2735       
2736       return FALSE;
2737     }
2738 }
2739
2740 /**
2741  * gtk_icon_info_get_display_name:
2742  * @icon_info: a #GtkIconInfo
2743  * 
2744  * Gets the display name for an icon. A display name is a
2745  * string to be used in place of the icon name in a user
2746  * visible context like a list of icons.
2747  * 
2748  * Return value: the display name for the icon or %NULL, if
2749  *  the icon doesn't have a specified display name. This value
2750  *  is owned @icon_info and must not be modified or free.
2751  *
2752  * Since: 2.4
2753  **/
2754 G_CONST_RETURN gchar *
2755 gtk_icon_info_get_display_name  (GtkIconInfo *icon_info)
2756 {
2757   g_return_val_if_fail (icon_info != NULL, NULL);
2758
2759   if (icon_info->data)
2760     return icon_info->data->display_name;
2761   else
2762     return NULL;
2763 }
2764
2765 /*
2766  * Builtin icons
2767  */
2768
2769
2770 /**
2771  * gtk_icon_theme_add_builtin_icon:
2772  * @icon_name: the name of the icon to register
2773  * @size: the size at which to register the icon (different
2774  *        images can be registered for the same icon name
2775  *        at different sizes.)
2776  * @pixbuf: #GdkPixbuf that contains the image to use
2777  *          for @icon_name.
2778  * 
2779  * Registers a built-in icon for icon theme lookups. The idea
2780  * of built-in icons is to allow an application or library
2781  * that uses themed icons to function requiring files to
2782  * be present in the file system. For instance, the default
2783  * images for all of GTK+'s stock icons are registered
2784  * as built-icons.
2785  *
2786  * In general, if you use gtk_icon_theme_add_builtin_icon()
2787  * you should also install the icon in the icon theme, so
2788  * that the icon is generally available.
2789  *
2790  * This function will generally be used with pixbufs loaded
2791  * via gdk_pixbuf_new_from_inline().
2792  *
2793  * Since: 2.4
2794  **/
2795 void
2796 gtk_icon_theme_add_builtin_icon (const gchar *icon_name,
2797                                  gint         size,
2798                                  GdkPixbuf   *pixbuf)
2799 {
2800   BuiltinIcon *default_icon;
2801   GSList *icons;
2802   gpointer key;
2803
2804   g_return_if_fail (icon_name != NULL);
2805   g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2806   
2807   if (!icon_theme_builtin_icons)
2808     icon_theme_builtin_icons = g_hash_table_new (g_str_hash, g_str_equal);
2809
2810   icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
2811   if (!icons)
2812     key = g_strdup (icon_name);
2813   else
2814     key = (gpointer)icon_name;  /* Won't get stored */
2815
2816   default_icon = g_new (BuiltinIcon, 1);
2817   default_icon->size = size;
2818   default_icon->pixbuf = g_object_ref (pixbuf);
2819   icons = g_slist_prepend (icons, default_icon);
2820
2821   /* Replaces value, leaves key untouched
2822    */
2823   g_hash_table_insert (icon_theme_builtin_icons, key, icons);
2824 }
2825
2826 /* Look up a builtin icon; the min_difference_p and
2827  * has_larger_p out parameters allow us to combine
2828  * this lookup with searching through the actual directories
2829  * of the "hicolor" icon theme. See theme_lookup_icon()
2830  * for how they are used.
2831  */
2832 static BuiltinIcon *
2833 find_builtin_icon (const gchar *icon_name,
2834                    gint         size,
2835                    gint        *min_difference_p,
2836                    gboolean    *has_larger_p)
2837 {
2838   GSList *icons = NULL;
2839   gint min_difference = G_MAXINT;
2840   gboolean has_larger = FALSE;
2841   BuiltinIcon *min_icon = NULL;
2842   
2843   _gtk_icon_factory_ensure_default_icons ();
2844   
2845   if (!icon_theme_builtin_icons)
2846     return NULL;
2847
2848   icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
2849
2850   while (icons)
2851     {
2852       BuiltinIcon *default_icon = icons->data;
2853       int min, max, difference;
2854       gboolean smaller;
2855       
2856       min = default_icon->size - 2;
2857       max = default_icon->size + 2;
2858       smaller = size < min;
2859       if (size < min)
2860         difference = min - size;
2861       else if (size > max)
2862         difference = size - max;
2863       else
2864         difference = 0;
2865       
2866       if (difference == 0)
2867         {
2868           min_icon = default_icon;
2869           break;
2870         }
2871       
2872       if (!has_larger)
2873         {
2874           if (difference < min_difference || smaller)
2875             {
2876               min_difference = difference;
2877               min_icon = default_icon;
2878               has_larger = smaller;
2879             }
2880         }
2881       else
2882         {
2883           if (difference < min_difference && smaller)
2884             {
2885               min_difference = difference;
2886               min_icon = default_icon;
2887             }
2888         }
2889       
2890       icons = icons->next;
2891     }
2892
2893   if (min_difference_p)
2894     *min_difference_p = min_difference;
2895   if (has_larger_p)
2896     *has_larger_p = has_larger;
2897
2898   return min_icon;
2899 }
2900
2901 #ifdef G_OS_WIN32
2902
2903 /* DLL ABI stability backward compatibility versions */
2904
2905 #undef gtk_icon_theme_set_search_path
2906
2907 void
2908 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
2909                                 const gchar  *path[],
2910                                 gint          n_elements)
2911 {
2912   const gchar **utf8_path;
2913   gint i;
2914
2915   utf8_path = g_new (const gchar *, n_elements);
2916
2917   for (i = 0; i < n_elements; i++)
2918     utf8_path[i] = g_locale_to_utf8 (path[i], -1, NULL, NULL, NULL);
2919
2920   gtk_icon_theme_set_search_path_utf8 (icon_theme, utf8_path, n_elements);
2921
2922   for (i = 0; i < n_elements; i++)
2923     g_free ((gchar *) utf8_path[i]);
2924
2925   g_free (utf8_path);
2926 }
2927
2928 #undef gtk_icon_theme_get_search_path
2929
2930 void
2931 gtk_icon_theme_get_search_path (GtkIconTheme      *icon_theme,
2932                                 gchar            **path[],
2933                                 gint              *n_elements)
2934 {
2935   gint i, n;
2936
2937   gtk_icon_theme_get_search_path_utf8 (icon_theme, path, &n);
2938
2939   if (n_elements)
2940     *n_elements = n;
2941
2942   if (path)
2943     {
2944       for (i = 0; i < n; i++)
2945         {
2946           gchar *tem = (*path)[i];
2947
2948           (*path)[i] = g_locale_from_utf8 ((*path)[i], -1, NULL, NULL, NULL);
2949           g_free (tem);
2950         }
2951     }
2952 }
2953
2954 #undef gtk_icon_theme_append_search_path
2955
2956 void
2957 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
2958                                    const gchar  *path)
2959 {
2960   gchar *utf8_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
2961
2962   gtk_icon_theme_append_search_path_utf8 (icon_theme, utf8_path);
2963
2964   g_free (utf8_path);
2965 }
2966
2967 #undef gtk_icon_theme_prepend_search_path
2968
2969 void
2970 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
2971                                     const gchar  *path)
2972 {
2973   gchar *utf8_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
2974
2975   gtk_icon_theme_prepend_search_path_utf8 (icon_theme, utf8_path);
2976
2977   g_free (utf8_path);
2978 }
2979
2980 #undef gtk_icon_info_get_filename
2981
2982 G_CONST_RETURN gchar *
2983 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2984 {
2985   g_return_val_if_fail (icon_info != NULL, NULL);
2986
2987   return icon_info->cp_filename;
2988 }
2989
2990 #endif
2991
2992 #define __GTK_ICON_THEME_C__
2993 #include "gtkaliasdef.c"