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