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