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