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