1 /* GtkIconTheme - a loader for icon themes
2 * gtk-icon-theme.c Copyright (C) 2002, 2003 Red Hat, Inc.
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.
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.
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.
22 #include <sys/types.h>
30 #include <glib/gstdio.h>
34 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
36 #endif /* G_OS_WIN32 */
38 #include "gtkicontheme.h"
39 #include "gtkiconfactory.h"
40 #include "gtkiconcache.h"
42 #include "gtksettings.h"
43 #include "gtkprivate.h"
47 #define DEFAULT_THEME_NAME "hicolor"
52 ICON_THEME_DIR_SCALABLE,
53 ICON_THEME_DIR_THRESHOLD,
54 ICON_THEME_DIR_UNTHEMED
57 /* In reverse search order: */
61 ICON_SUFFIX_XPM = 1 << 0,
62 ICON_SUFFIX_SVG = 1 << 1,
63 ICON_SUFFIX_PNG = 1 << 2,
64 HAS_ICON_FILE = 1 << 3
68 struct _GtkIconThemePrivate
70 guint custom_theme : 1;
71 guint is_screen_singleton : 1;
72 guint pixbuf_supports_svg : 1;
78 gboolean themes_valid;
79 /* A list of all the themes needed to look up icons.
80 * In search order, without duplicates
83 GHashTable *unthemed_icons;
85 /* Note: The keys of this hashtable are owned by the
86 * themedir and unthemed hashtables.
88 GHashTable *all_icons;
90 /* GdkScreen for the icon theme (may be NULL)
94 /* time when we last stat:ed for theme changes */
98 gulong reset_styles_idle;
103 /* Information about the source
107 /* System codepage version of filename, for DLL ABI backward
108 * compatibility functions.
112 GdkPixbuf *builtin_pixbuf;
114 /* Cache pixbuf (if there is any) */
115 GdkPixbuf *cache_pixbuf;
119 /* Information about the directory where
120 * the source was found
122 IconThemeDirType dir_type;
126 /* Parameters influencing the scaled icon
129 gboolean raw_coordinates;
131 /* Cached information if we go ahead and try to load
146 /* In search order */
152 IconThemeDirType type;
166 GHashTable *icon_data;
172 char *no_svg_filename;
184 time_t mtime; /* 0 == not existing or not a dir */
189 static void gtk_icon_theme_class_init (GtkIconThemeClass *klass);
190 static void gtk_icon_theme_init (GtkIconTheme *icon_theme);
191 static void gtk_icon_theme_finalize (GObject *object);
192 static void theme_dir_destroy (IconThemeDir *dir);
194 static void theme_destroy (IconTheme *theme);
195 static GtkIconInfo *theme_lookup_icon (IconTheme *theme,
196 const char *icon_name,
199 gboolean use_default_icons);
200 static void theme_list_icons (IconTheme *theme,
203 static void theme_subdir_load (GtkIconTheme *icon_theme,
205 GKeyFile *theme_file,
207 static void do_theme_change (GtkIconTheme *icon_theme);
209 static void blow_themes (GtkIconTheme *icon_themes);
211 static void icon_data_free (GtkIconData *icon_data);
212 static void load_icon_data (IconThemeDir *dir,
216 static IconSuffix theme_dir_get_icon_suffix (IconThemeDir *dir,
217 const gchar *icon_name,
218 gboolean *has_icon_file);
221 static GtkIconInfo *icon_info_new (void);
222 static GtkIconInfo *icon_info_new_builtin (BuiltinIcon *icon);
224 static IconSuffix suffix_from_name (const char *name);
226 static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
228 gint *min_difference_p,
229 gboolean *has_larger_p);
231 static GObjectClass *parent_class = NULL;
233 static guint signal_changed = 0;
235 static GHashTable *icon_theme_builtin_icons;
238 gtk_icon_theme_get_type (void)
240 static GType type = 0;
244 static const GTypeInfo info =
246 sizeof (GtkIconThemeClass),
247 NULL, /* base_init */
248 NULL, /* base_finalize */
249 (GClassInitFunc) gtk_icon_theme_class_init,
250 NULL, /* class_finalize */
251 NULL, /* class_data */
252 sizeof (GtkIconTheme),
254 (GInstanceInitFunc) gtk_icon_theme_init,
257 type = g_type_register_static (G_TYPE_OBJECT, "GtkIconTheme", &info, 0);
264 * gtk_icon_theme_new:
266 * Creates a new icon theme object. Icon theme objects are used
267 * to lookup up an icon by name in a particular icon theme.
268 * Usually, you'll want to use gtk_icon_theme_get_default()
269 * or gtk_icon_theme_get_for_screen() rather than creating
270 * a new icon theme object for scratch.
272 * Return value: the newly created #GtkIconTheme object.
277 gtk_icon_theme_new (void)
279 return g_object_new (GTK_TYPE_ICON_THEME, NULL);
283 * gtk_icon_theme_get_default:
285 * Gets the icon theme for the default screen. See
286 * gtk_icon_theme_get_for_screen().
288 * Return value: A unique #GtkIconTheme associated with
289 * the default screen. This icon theme is associated with
290 * the screen and can be used as long as the screen
291 * is open. Do not ref or unref it.
296 gtk_icon_theme_get_default (void)
298 return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
302 * gtk_icon_theme_get_for_screen:
303 * @screen: a #GdkScreen
305 * Gets the icon theme object associated with @screen; if this
306 * function has not previously been called for the given
307 * screen, a new icon theme object will be created and
308 * associated with the screen. Icon theme objects are
309 * fairly expensive to create, so using this function
310 * is usually a better choice than calling than gtk_icon_theme_new()
311 * and setting the screen yourself; by using this function
312 * a single icon theme object will be shared between users.
314 * Return value: A unique #GtkIconTheme associated with
315 * the given screen. This icon theme is associated with
316 * the screen and can be used as long as the screen
317 * is open. Do not ref or unref it.
322 gtk_icon_theme_get_for_screen (GdkScreen *screen)
324 GtkIconTheme *icon_theme;
326 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
327 g_return_val_if_fail (!screen->closed, NULL);
329 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
332 GtkIconThemePrivate *priv;
334 icon_theme = gtk_icon_theme_new ();
335 gtk_icon_theme_set_screen (icon_theme, screen);
337 priv = icon_theme->priv;
338 priv->is_screen_singleton = TRUE;
340 g_object_set_data (G_OBJECT (screen), "gtk-icon-theme", icon_theme);
347 gtk_icon_theme_class_init (GtkIconThemeClass *klass)
349 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
351 parent_class = g_type_class_peek_parent (klass);
353 gobject_class->finalize = gtk_icon_theme_finalize;
356 * GtkIconTheme::changed
357 * @icon_theme: the icon theme
359 * Emitted when the current icon theme is switched or GTK+ detects
360 * that a change has occurred in the contents of the current
363 signal_changed = g_signal_new ("changed",
364 G_TYPE_FROM_CLASS (klass),
366 G_STRUCT_OFFSET (GtkIconThemeClass, changed),
368 g_cclosure_marshal_VOID__VOID,
371 g_type_class_add_private (klass, sizeof (GtkIconThemePrivate));
375 /* Callback when the display that the icon theme is attached to
376 * is closed; unset the screen, and if it's the unique theme
377 * for the screen, drop the reference
380 display_closed (GdkDisplay *display,
382 GtkIconTheme *icon_theme)
384 GtkIconThemePrivate *priv = icon_theme->priv;
385 GdkScreen *screen = priv->screen;
386 gboolean was_screen_singleton = priv->is_screen_singleton;
388 if (was_screen_singleton)
390 g_object_set_data (G_OBJECT (screen), "gtk-icon-theme", NULL);
391 priv->is_screen_singleton = FALSE;
394 gtk_icon_theme_set_screen (icon_theme, NULL);
396 if (was_screen_singleton)
398 g_object_unref (icon_theme);
403 update_current_theme (GtkIconTheme *icon_theme)
405 GtkIconThemePrivate *priv = icon_theme->priv;
407 if (!priv->custom_theme)
413 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
414 g_object_get (settings, "gtk-icon-theme-name", &theme, NULL);
418 theme = g_strdup (DEFAULT_THEME_NAME);
420 if (strcmp (priv->current_theme, theme) != 0)
422 g_free (priv->current_theme);
423 priv->current_theme = theme;
425 do_theme_change (icon_theme);
432 /* Callback when the icon theme GtkSetting changes
435 theme_changed (GtkSettings *settings,
437 GtkIconTheme *icon_theme)
439 update_current_theme (icon_theme);
443 unset_screen (GtkIconTheme *icon_theme)
445 GtkIconThemePrivate *priv = icon_theme->priv;
446 GtkSettings *settings;
451 settings = gtk_settings_get_for_screen (priv->screen);
452 display = gdk_screen_get_display (priv->screen);
454 g_signal_handlers_disconnect_by_func (display,
455 (gpointer) display_closed,
457 g_signal_handlers_disconnect_by_func (settings,
458 (gpointer) theme_changed,
466 * gtk_icon_theme_set_screen:
467 * @icon_theme: a #GtkIconTheme
468 * @screen: a #GdkScreen
470 * Sets the screen for an icon theme; the screen is used
471 * to track the user's currently configured icon theme,
472 * which might be different for different screens.
477 gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
480 GtkIconThemePrivate *priv;
481 GtkSettings *settings;
484 g_return_if_fail (GTK_ICON_THEME (icon_theme));
485 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
487 priv = icon_theme->priv;
489 unset_screen (icon_theme);
493 display = gdk_screen_get_display (screen);
494 settings = gtk_settings_get_for_screen (screen);
496 priv->screen = screen;
498 g_signal_connect (display, "closed",
499 G_CALLBACK (display_closed), icon_theme);
500 g_signal_connect (settings, "notify::gtk-icon-theme-name",
501 G_CALLBACK (theme_changed), icon_theme);
504 update_current_theme (icon_theme);
507 /* Checks whether a loader for SVG files has been registered
511 pixbuf_supports_svg (void)
513 GSList *formats = gdk_pixbuf_get_formats ();
515 static gboolean found_svg = FALSE;
516 static gboolean value_known = FALSE;
521 for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
523 gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
526 for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
528 if (strcmp (*mime_type, "image/svg") == 0)
532 g_strfreev (mime_types);
535 g_slist_free (formats);
542 gtk_icon_theme_init (GtkIconTheme *icon_theme)
544 GtkIconThemePrivate *priv;
545 const gchar * const *xdg_data_dirs;
548 priv = g_type_instance_get_private ((GTypeInstance *)icon_theme,
549 GTK_TYPE_ICON_THEME);
550 icon_theme->priv = priv;
552 priv->custom_theme = FALSE;
553 priv->current_theme = g_strdup (DEFAULT_THEME_NAME);
555 xdg_data_dirs = g_get_system_data_dirs ();
556 for (i = 0; xdg_data_dirs[i]; i++) ;
558 priv->search_path_len = 2 * i + 2;
560 priv->search_path = g_new (char *, priv->search_path_len);
563 priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
564 priv->search_path[i++] = g_build_filename (g_get_user_data_dir (), "icons", NULL);
566 for (j = 0; xdg_data_dirs[j]; j++)
567 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
569 for (j = 0; xdg_data_dirs[j]; j++)
570 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "pixmaps", NULL);
572 priv->themes_valid = FALSE;
574 priv->unthemed_icons = NULL;
576 priv->pixbuf_supports_svg = pixbuf_supports_svg ();
580 free_dir_mtime (IconThemeDirMtime *dir_mtime)
582 if (dir_mtime->cache)
583 _gtk_icon_cache_unref (dir_mtime->cache);
585 g_free (dir_mtime->dir);
591 reset_styles_idle (gpointer user_data)
593 GtkIconTheme *icon_theme;
594 GtkIconThemePrivate *priv;
596 GDK_THREADS_ENTER ();
598 icon_theme = GTK_ICON_THEME (user_data);
599 priv = icon_theme->priv;
601 if (priv->screen && priv->is_screen_singleton)
603 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
604 gtk_rc_reset_styles (settings);
607 priv->reset_styles_idle = 0;
609 GDK_THREADS_LEAVE ();
615 do_theme_change (GtkIconTheme *icon_theme)
617 GtkIconThemePrivate *priv = icon_theme->priv;
620 g_print ("change to icon theme \"%s\"\n", priv->current_theme));
621 blow_themes (icon_theme);
622 g_signal_emit (icon_theme, signal_changed, 0);
624 if (!priv->reset_styles_idle)
625 priv->reset_styles_idle =
626 g_idle_add (reset_styles_idle, icon_theme);
630 blow_themes (GtkIconTheme *icon_theme)
632 GtkIconThemePrivate *priv = icon_theme->priv;
634 if (priv->themes_valid)
636 g_hash_table_destroy (priv->all_icons);
637 g_list_foreach (priv->themes, (GFunc)theme_destroy, NULL);
638 g_list_free (priv->themes);
639 g_list_foreach (priv->dir_mtimes, (GFunc)free_dir_mtime, NULL);
640 g_list_free (priv->dir_mtimes);
641 g_hash_table_destroy (priv->unthemed_icons);
644 priv->unthemed_icons = NULL;
645 priv->dir_mtimes = NULL;
646 priv->all_icons = NULL;
647 priv->themes_valid = FALSE;
651 gtk_icon_theme_finalize (GObject *object)
653 GtkIconTheme *icon_theme;
654 GtkIconThemePrivate *priv;
657 icon_theme = GTK_ICON_THEME (object);
658 priv = icon_theme->priv;
660 if (priv->reset_styles_idle)
662 g_source_remove (priv->reset_styles_idle);
663 priv->reset_styles_idle = 0;
666 unset_screen (icon_theme);
668 g_free (priv->current_theme);
669 priv->current_theme = NULL;
671 for (i=0; i < priv->search_path_len; i++)
672 g_free (priv->search_path[i]);
674 g_free (priv->search_path);
675 priv->search_path = NULL;
677 blow_themes (icon_theme);
679 G_OBJECT_CLASS (parent_class)->finalize (object);
683 * gtk_icon_theme_set_search_path:
684 * @icon_theme: a #GtkIconTheme
685 * @path: array of directories that are searched for icon themes
686 * @n_elements: number of elements in @path.
688 * Sets the search path for the icon theme object. When looking
689 * for an icon theme, GTK+ will search for a subdirectory of
690 * one or more of the directories in @path with the same name
691 * as the icon theme. (Themes from multiple of the path elements
692 * are combined to allow themes to be extended by adding icons
693 * in the user's home directory.)
695 * In addition if an icon found isn't found either in the current
696 * icon theme or the default icon theme, and an image file with
697 * the right name is found directly in one of the elements of
698 * @path, then that image will be used for the icon name.
699 * (This is legacy feature, and new icons should be put
700 * into the default icon theme, which is called DEFAULT_THEME_NAME,
701 * rather than directly on the icon path.)
706 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
710 GtkIconThemePrivate *priv;
713 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
715 priv = icon_theme->priv;
716 for (i = 0; i < priv->search_path_len; i++)
717 g_free (priv->search_path[i]);
719 g_free (priv->search_path);
721 priv->search_path = g_new (gchar *, n_elements);
722 priv->search_path_len = n_elements;
723 for (i = 0; i < priv->search_path_len; i++)
724 priv->search_path[i] = g_strdup (path[i]);
726 do_theme_change (icon_theme);
731 * gtk_icon_theme_get_search_path:
732 * @icon_theme: a #GtkIconTheme
733 * @path: location to store a list of icon theme path directories or %NULL
734 * The stored value should be freed with g_strfreev().
735 * @n_elements: location to store number of elements
738 * Gets the current search path. See gtk_icon_theme_set_search_path().
743 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
747 GtkIconThemePrivate *priv;
750 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
752 priv = icon_theme->priv;
755 *n_elements = priv->search_path_len;
759 *path = g_new (gchar *, priv->search_path_len + 1);
760 for (i = 0; i < priv->search_path_len; i++)
761 (*path)[i] = g_strdup (priv->search_path[i]);
767 * gtk_icon_theme_append_search_path:
768 * @icon_theme: a #GtkIconTheme
769 * @path: directory name to append to the icon path
771 * Appends a directory to the search path.
772 * See gtk_icon_theme_set_search_path().
777 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
780 GtkIconThemePrivate *priv;
782 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
783 g_return_if_fail (path != NULL);
785 priv = icon_theme->priv;
787 priv->search_path_len++;
788 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
789 priv->search_path[priv->search_path_len-1] = g_strdup (path);
791 do_theme_change (icon_theme);
795 * gtk_icon_theme_prepend_search_path:
796 * @icon_theme: a #GtkIconTheme
797 * @path: directory name to prepend to the icon path
799 * Prepends a directory to the search path.
800 * See gtk_icon_theme_set_search_path().
805 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
808 GtkIconThemePrivate *priv;
811 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
812 g_return_if_fail (path != NULL);
814 priv = icon_theme->priv;
816 priv->search_path_len++;
817 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
819 for (i = priv->search_path_len - 1; i > 0; i--)
820 priv->search_path[i] = priv->search_path[i - 1];
822 priv->search_path[0] = g_strdup (path);
824 do_theme_change (icon_theme);
828 * gtk_icon_theme_set_custom_theme:
829 * @icon_theme: a #GtkIconTheme
830 * @theme_name: name of icon theme to use instead of configured theme,
831 * or %NULL to unset a previously set custom theme
833 * Sets the name of the icon theme that the #GtkIconTheme object uses
834 * overriding system configuration. This function cannot be called
835 * on the icon theme objects returned from gtk_icon_theme_get_default()
836 * and gtk_icon_theme_get_for_screen().
841 gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
842 const gchar *theme_name)
844 GtkIconThemePrivate *priv;
846 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
848 priv = icon_theme->priv;
850 g_return_if_fail (!priv->is_screen_singleton);
852 if (theme_name != NULL)
854 priv->custom_theme = TRUE;
855 if (strcmp (theme_name, priv->current_theme) != 0)
857 g_free (priv->current_theme);
858 priv->current_theme = g_strdup (theme_name);
860 do_theme_change (icon_theme);
865 priv->custom_theme = FALSE;
867 update_current_theme (icon_theme);
872 insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
878 GtkIconThemePrivate *priv;
881 GKeyFile *theme_file;
882 GError *error = NULL;
883 IconThemeDirMtime *dir_mtime;
884 struct stat stat_buf;
886 priv = icon_theme->priv;
888 for (l = priv->themes; l != NULL; l = l->next)
891 if (strcmp (theme->name, theme_name) == 0)
895 for (i = 0; i < priv->search_path_len; i++)
897 path = g_build_filename (priv->search_path[i],
900 dir_mtime = g_new (IconThemeDirMtime, 1);
901 dir_mtime->cache = NULL;
902 dir_mtime->dir = path;
903 if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
904 dir_mtime->mtime = stat_buf.st_mtime;
906 dir_mtime->mtime = 0;
908 priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
910 priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
913 for (i = 0; i < priv->search_path_len && !theme_file; i++)
915 path = g_build_filename (priv->search_path[i],
919 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
921 theme_file = g_key_file_new ();
922 g_key_file_set_list_separator (theme_file, ',');
923 g_key_file_load_from_file (theme_file, path, 0, &error);
926 g_key_file_free (theme_file);
928 g_error_free (error);
935 if (theme_file == NULL)
938 theme = g_new (IconTheme, 1);
939 theme->display_name =
940 g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
941 if (!theme->display_name)
943 g_warning ("Theme file for %s has no name\n", theme_name);
945 g_key_file_free (theme_file);
949 dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
952 g_warning ("Theme file for %s has no directories\n", theme_name);
953 g_free (theme->display_name);
955 g_key_file_free (theme_file);
959 theme->name = g_strdup (theme_name);
961 g_key_file_get_locale_string (theme_file,
962 "Icon Theme", "Comment",
965 g_key_file_get_string (theme_file,
966 "Icon Theme", "Example",
970 for (i = 0; dirs[i] != NULL; i++)
971 theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
975 theme->dirs = g_list_reverse (theme->dirs);
977 /* Prepend the finished theme */
978 priv->themes = g_list_prepend (priv->themes, theme);
980 themes = g_key_file_get_string_list (theme_file,
987 for (i = 0; themes[i] != NULL; i++)
988 insert_theme (icon_theme, themes[i]);
993 g_key_file_free (theme_file);
997 free_unthemed_icon (UnthemedIcon *unthemed_icon)
999 if (unthemed_icon->svg_filename)
1000 g_free (unthemed_icon->svg_filename);
1001 if (unthemed_icon->no_svg_filename)
1002 g_free (unthemed_icon->no_svg_filename);
1003 g_free (unthemed_icon);
1007 load_themes (GtkIconTheme *icon_theme)
1009 GtkIconThemePrivate *priv;
1012 char *dir, *base_name, *dot;
1015 UnthemedIcon *unthemed_icon;
1016 IconSuffix old_suffix, new_suffix;
1018 IconThemeDirMtime *dir_mtime;
1019 struct stat stat_buf;
1021 priv = icon_theme->priv;
1023 priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
1025 insert_theme (icon_theme, priv->current_theme);
1027 /* Always look in the "default" icon theme */
1028 insert_theme (icon_theme, DEFAULT_THEME_NAME);
1029 priv->themes = g_list_reverse (priv->themes);
1032 priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
1033 g_free, (GDestroyNotify)free_unthemed_icon);
1035 for (base = 0; base < icon_theme->priv->search_path_len; base++)
1037 dir = icon_theme->priv->search_path[base];
1039 dir_mtime = g_new (IconThemeDirMtime, 1);
1040 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir);
1041 dir_mtime->dir = g_strdup (dir);
1042 if (g_stat (dir, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
1043 dir_mtime->mtime = stat_buf.st_mtime;
1045 dir_mtime->mtime = 0;
1047 priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
1049 if (dir_mtime->cache != NULL)
1052 gdir = g_dir_open (dir, 0, NULL);
1057 while ((file = g_dir_read_name (gdir)))
1059 new_suffix = suffix_from_name (file);
1061 if (new_suffix != ICON_SUFFIX_NONE)
1063 abs_file = g_build_filename (dir, file, NULL);
1065 base_name = g_strdup (file);
1067 dot = strrchr (base_name, '.');
1071 if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
1074 if (new_suffix == ICON_SUFFIX_SVG)
1076 if (unthemed_icon->svg_filename)
1079 unthemed_icon->svg_filename = abs_file;
1083 if (unthemed_icon->no_svg_filename)
1085 old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
1086 if (new_suffix > old_suffix)
1088 g_free (unthemed_icon->no_svg_filename);
1089 unthemed_icon->no_svg_filename = abs_file;
1095 unthemed_icon->no_svg_filename = abs_file;
1102 unthemed_icon = g_new0 (UnthemedIcon, 1);
1104 if (new_suffix == ICON_SUFFIX_SVG)
1105 unthemed_icon->svg_filename = abs_file;
1107 unthemed_icon->no_svg_filename = abs_file;
1109 g_hash_table_insert (priv->unthemed_icons,
1112 g_hash_table_insert (priv->all_icons,
1120 priv->themes_valid = TRUE;
1122 g_get_current_time(&tv);
1123 priv->last_stat_time = tv.tv_sec;
1127 ensure_valid_themes (GtkIconTheme *icon_theme)
1129 GtkIconThemePrivate *priv = icon_theme->priv;
1132 if (priv->themes_valid)
1134 g_get_current_time (&tv);
1136 if (ABS (tv.tv_sec - priv->last_stat_time) > 5)
1137 gtk_icon_theme_rescan_if_needed (icon_theme);
1140 if (!priv->themes_valid)
1141 load_themes (icon_theme);
1145 * gtk_icon_theme_lookup_icon:
1146 * @icon_theme: a #GtkIconTheme
1147 * @icon_name: the name of the icon to lookup
1148 * @size: desired icon size
1149 * @flags: flags modifying the behavior of the icon lookup
1151 * Looks up a named icon and returns a structure containing
1152 * information such as the filename of the icon. The icon
1153 * can then be rendered into a pixbuf using
1154 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1155 * combines these two steps if all you need is the pixbuf.)
1157 * Return value: a #GtkIconInfo structure containing information
1158 * about the icon, or %NULL if the icon wasn't found. Free with
1159 * gtk_icon_info_free()
1164 gtk_icon_theme_lookup_icon (GtkIconTheme *icon_theme,
1165 const gchar *icon_name,
1167 GtkIconLookupFlags flags)
1169 GtkIconThemePrivate *priv;
1171 GtkIconInfo *icon_info = NULL;
1172 UnthemedIcon *unthemed_icon;
1174 gboolean use_builtin;
1175 gboolean found_default;
1177 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1178 g_return_val_if_fail (icon_name != NULL, NULL);
1179 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1180 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1182 priv = icon_theme->priv;
1184 g_print ("gtk_icon_theme_lookup_icon %s\n", icon_name);
1185 if (flags & GTK_ICON_LOOKUP_NO_SVG)
1187 else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
1190 allow_svg = priv->pixbuf_supports_svg;
1192 use_builtin = (flags & GTK_ICON_LOOKUP_USE_BUILTIN);
1194 ensure_valid_themes (icon_theme);
1196 found_default = FALSE;
1200 IconTheme *theme = l->data;
1202 if (strcmp (theme->name, DEFAULT_THEME_NAME) == 0)
1203 found_default = TRUE;
1205 icon_info = theme_lookup_icon (theme, icon_name, size, allow_svg, use_builtin);
1214 BuiltinIcon *builtin = find_builtin_icon (icon_name, size, NULL, NULL);
1217 icon_info = icon_info_new_builtin (builtin);
1222 unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_name);
1225 icon_info = icon_info_new ();
1227 /* A SVG icon, when allowed, beats out a XPM icon, but not
1231 unthemed_icon->svg_filename &&
1232 (!unthemed_icon->no_svg_filename ||
1233 suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
1234 icon_info->filename = g_strdup (unthemed_icon->svg_filename);
1235 else if (unthemed_icon->no_svg_filename)
1236 icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);
1238 icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
1239 -1, NULL, NULL, NULL);
1242 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1247 icon_info->desired_size = size;
1250 static gboolean check_for_default_theme = TRUE;
1251 char *default_theme_path;
1252 gboolean found = FALSE;
1255 if (check_for_default_theme)
1257 check_for_default_theme = FALSE;
1259 for (i = 0; !found && i < priv->search_path_len; i++)
1261 default_theme_path = g_build_filename (priv->search_path[i],
1265 found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
1266 g_free (default_theme_path);
1270 g_warning (_("Could not find the icon '%s'. The '%s' theme\n"
1271 "was not found either, perhaps you need to install it.\n"
1272 "You can get a copy from:\n"
1274 icon_name, DEFAULT_THEME_NAME, "http://icon-theme.freedesktop.org/releases");
1284 gtk_icon_theme_error_quark (void)
1286 static GQuark q = 0;
1288 q = g_quark_from_static_string ("gtk-icon-theme-error-quark");
1294 * gtk_icon_theme_load_icon:
1295 * @icon_theme: a #GtkIconTheme
1296 * @icon_name: the name of the icon to lookup
1297 * @size: the desired icon size. The resulting icon may not be
1298 * exactly this size; see gtk_icon_info_load_icon().
1299 * @flags: flags modifying the behavior of the icon lookup
1300 * @error: Location to store error information on failure, or %NULL.
1302 * Looks up an icon in an icon theme, scales it to the given size
1303 * and renders it into a pixbuf. This is a convenience function;
1304 * if more details about the icon are needed, use
1305 * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1307 * Return value: the rendered icon; this may be a newly created icon
1308 * or a new reference to an internal icon, so you must not modify
1309 * the icon. Use g_object_unref() to release your reference to the
1310 * icon. %NULL if the icon isn't found.
1315 gtk_icon_theme_load_icon (GtkIconTheme *icon_theme,
1316 const gchar *icon_name,
1318 GtkIconLookupFlags flags,
1321 GtkIconInfo *icon_info;
1322 GdkPixbuf *pixbuf = NULL;
1324 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1325 g_return_val_if_fail (icon_name != NULL, NULL);
1326 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1327 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1328 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1330 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
1331 flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1334 g_set_error (error, GTK_ICON_THEME_ERROR, GTK_ICON_THEME_NOT_FOUND,
1335 _("Icon '%s' not present in theme"), icon_name);
1339 pixbuf = gtk_icon_info_load_icon (icon_info, error);
1340 gtk_icon_info_free (icon_info);
1346 * gtk_icon_theme_has_icon:
1347 * @icon_theme: a #GtkIconTheme
1348 * @icon_name: the name of an icon
1350 * Checks whether an icon theme includes an icon
1351 * for a particular name.
1353 * Return value: %TRUE if @icon_theme includes an
1354 * icon for @icon_name.
1359 gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
1360 const char *icon_name)
1362 GtkIconThemePrivate *priv;
1365 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1367 priv = icon_theme->priv;
1369 ensure_valid_themes (icon_theme);
1371 for (l = priv->dir_mtimes; l; l = l->next)
1373 IconThemeDirMtime *dir_mtime = l->data;
1374 GtkIconCache *cache = dir_mtime->cache;
1376 if (cache && _gtk_icon_cache_has_icon (cache, icon_name))
1380 if (g_hash_table_lookup_extended (priv->all_icons,
1381 icon_name, NULL, NULL))
1384 if (icon_theme_builtin_icons &&
1385 g_hash_table_lookup_extended (icon_theme_builtin_icons,
1386 icon_name, NULL, NULL))
1393 add_size (gpointer key,
1397 gint **res_p = user_data;
1399 **res_p = GPOINTER_TO_INT (key);
1405 * gtk_icon_theme_get_icon_sizes:
1406 * @icon_theme: a #GtkIconTheme
1407 * @icon_name: the name of an icon
1409 * Returns an array of integers describing the sizes at which
1410 * the icon is available without scaling. A size of -1 means
1411 * that the icon is available in a scalable format. The array
1412 * is zero-terminated.
1414 * Return value: An newly allocated array describing the sizes at
1415 * which the icon is available. The array should be freed with g_free()
1416 * when it is no longer needed.
1421 gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
1422 const char *icon_name)
1429 GtkIconThemePrivate *priv;
1431 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1433 priv = icon_theme->priv;
1435 ensure_valid_themes (icon_theme);
1437 sizes = g_hash_table_new (g_direct_hash, g_direct_equal);
1439 for (l = priv->themes; l; l = l->next)
1441 IconTheme *theme = l->data;
1442 for (d = theme->dirs; d; d = d->next)
1444 IconThemeDir *dir = d->data;
1446 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1447 if (suffix != ICON_SUFFIX_NONE)
1449 if (suffix == ICON_SUFFIX_SVG)
1450 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1452 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1457 r = result = g_new0 (gint, g_hash_table_size (sizes) + 1);
1459 g_hash_table_foreach (sizes, add_size, &r);
1460 g_hash_table_destroy (sizes);
1466 add_key_to_hash (gpointer key,
1470 GHashTable *hash = user_data;
1472 g_hash_table_insert (hash, key, NULL);
1476 add_key_to_list (gpointer key,
1480 GList **list = user_data;
1482 *list = g_list_prepend (*list, g_strdup (key));
1486 * gtk_icon_theme_list_icons:
1487 * @icon_theme: a #GtkIconTheme
1488 * @context: a string identifying a particular type of icon,
1489 * or %NULL to list all icons.
1491 * Lists the icons in the current icon theme. Only a subset
1492 * of the icons can be listed by providing a context string.
1493 * The set of values for the context string is system dependent,
1494 * but will typically include such values as 'apps' and
1497 * Return value: a #GList list holding the names of all the
1498 * icons in the theme. You must first free each element
1499 * in the list with g_free(), then free the list itself
1500 * with g_list_free().
1505 gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
1506 const char *context)
1508 GtkIconThemePrivate *priv;
1511 GQuark context_quark;
1513 priv = icon_theme->priv;
1515 ensure_valid_themes (icon_theme);
1519 context_quark = g_quark_try_string (context);
1527 icons = g_hash_table_new (g_str_hash, g_str_equal);
1532 theme_list_icons (l->data, icons, context_quark);
1536 if (context_quark == 0)
1537 g_hash_table_foreach (priv->unthemed_icons,
1543 g_hash_table_foreach (icons,
1547 g_hash_table_destroy (icons);
1553 * gtk_icon_theme_get_example_icon_name:
1554 * @icon_theme: a #GtkIconTheme
1556 * Gets the name of an icon that is representative of the
1557 * current theme (for instance, to use when presenting
1558 * a list of themes to the user.)
1560 * Return value: the name of an example icon or %NULL.
1561 * Free with g_free().
1566 gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
1568 GtkIconThemePrivate *priv;
1572 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1574 priv = icon_theme->priv;
1576 ensure_valid_themes (icon_theme);
1583 return g_strdup (theme->example);
1592 * gtk_icon_theme_rescan_if_needed:
1593 * @icon_theme: a #GtkIconTheme
1595 * Checks to see if the icon theme has changed; if it has, any
1596 * currently cached information is discarded and will be reloaded
1597 * next time @icon_theme is accessed.
1599 * Return value: %TRUE if the icon theme has changed and needed
1605 gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
1607 GtkIconThemePrivate *priv;
1608 IconThemeDirMtime *dir_mtime;
1611 struct stat stat_buf;
1614 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1616 priv = icon_theme->priv;
1618 for (d = priv->dir_mtimes; d != NULL; d = d->next)
1620 dir_mtime = d->data;
1622 stat_res = g_stat (dir_mtime->dir, &stat_buf);
1624 /* dir mtime didn't change */
1625 if (stat_res == 0 &&
1626 S_ISDIR (stat_buf.st_mode) &&
1627 dir_mtime->mtime == stat_buf.st_mtime)
1629 /* didn't exist before, and still doesn't */
1630 if (dir_mtime->mtime == 0 &&
1631 (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
1634 do_theme_change (icon_theme);
1638 g_get_current_time (&tv);
1639 priv->last_stat_time = tv.tv_sec;
1645 theme_destroy (IconTheme *theme)
1647 g_free (theme->display_name);
1648 g_free (theme->comment);
1649 g_free (theme->name);
1650 g_free (theme->example);
1652 g_list_foreach (theme->dirs, (GFunc)theme_dir_destroy, NULL);
1653 g_list_free (theme->dirs);
1659 theme_dir_destroy (IconThemeDir *dir)
1662 _gtk_icon_cache_unref (dir->cache);
1664 g_hash_table_destroy (dir->icons);
1667 g_hash_table_destroy (dir->icon_data);
1669 g_free (dir->subdir);
1674 theme_dir_size_difference (IconThemeDir *dir, int size, gboolean *smaller)
1679 case ICON_THEME_DIR_FIXED:
1680 *smaller = size < dir->size;
1681 return abs (size - dir->size);
1683 case ICON_THEME_DIR_SCALABLE:
1684 *smaller = size < dir->min_size;
1685 if (size < dir->min_size)
1686 return dir->min_size - size;
1687 if (size > dir->max_size)
1688 return size - dir->max_size;
1691 case ICON_THEME_DIR_THRESHOLD:
1692 min = dir->size - dir->threshold;
1693 max = dir->size + dir->threshold;
1694 *smaller = size < min;
1701 case ICON_THEME_DIR_UNTHEMED:
1702 g_assert_not_reached ();
1705 g_assert_not_reached ();
1710 string_from_suffix (IconSuffix suffix)
1714 case ICON_SUFFIX_XPM:
1716 case ICON_SUFFIX_SVG:
1718 case ICON_SUFFIX_PNG:
1721 g_assert_not_reached();
1727 suffix_from_name (const char *name)
1731 if (g_str_has_suffix (name, ".png"))
1732 retval = ICON_SUFFIX_PNG;
1733 else if (g_str_has_suffix (name, ".svg"))
1734 retval = ICON_SUFFIX_SVG;
1735 else if (g_str_has_suffix (name, ".xpm"))
1736 retval = ICON_SUFFIX_XPM;
1738 retval = ICON_SUFFIX_NONE;
1744 best_suffix (IconSuffix suffix,
1747 if ((suffix & ICON_SUFFIX_PNG) != 0)
1748 return ICON_SUFFIX_PNG;
1749 else if (allow_svg && ((suffix & ICON_SUFFIX_SVG) != 0))
1750 return ICON_SUFFIX_SVG;
1751 else if ((suffix & ICON_SUFFIX_XPM) != 0)
1752 return ICON_SUFFIX_XPM;
1754 return ICON_SUFFIX_NONE;
1759 theme_dir_get_icon_suffix (IconThemeDir *dir,
1760 const gchar *icon_name,
1761 gboolean *has_icon_file)
1767 suffix = (IconSuffix)_gtk_icon_cache_get_icon_flags (dir->cache,
1772 *has_icon_file = suffix & HAS_ICON_FILE;
1774 suffix = suffix & ~HAS_ICON_FILE;
1777 suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
1779 g_print ("get_icon_suffix%s %d\n", dir->cache ? " (cached)" : "", suffix);
1784 static GtkIconInfo *
1785 theme_lookup_icon (IconTheme *theme,
1786 const char *icon_name,
1789 gboolean use_builtin)
1792 IconThemeDir *dir, *min_dir;
1794 int min_difference, difference;
1795 BuiltinIcon *closest_builtin = NULL;
1796 gboolean smaller, has_larger;
1799 min_difference = G_MAXINT;
1803 /* Builtin icons are logically part of the default theme and
1804 * are searched before other subdirectories of the default theme.
1806 if (strcmp (theme->name, DEFAULT_THEME_NAME) == 0 && use_builtin)
1808 closest_builtin = find_builtin_icon (icon_name, size,
1812 if (min_difference == 0)
1813 return icon_info_new_builtin (closest_builtin);
1821 g_print ("theme_lookup_icon dir %s\n", dir->dir);
1822 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1823 if (best_suffix (suffix, allow_svg) != ICON_SUFFIX_NONE)
1825 difference = theme_dir_size_difference (dir, size, &smaller);
1827 if (difference == 0)
1835 if (difference < min_difference || smaller)
1837 min_difference = difference;
1839 closest_builtin = NULL;
1840 has_larger = smaller;
1845 if (difference < min_difference && smaller)
1847 min_difference = difference;
1849 closest_builtin = NULL;
1858 if (closest_builtin)
1859 return icon_info_new_builtin (closest_builtin);
1863 GtkIconInfo *icon_info = icon_info_new ();
1864 gboolean has_icon_file;
1866 suffix = theme_dir_get_icon_suffix (min_dir, icon_name, &has_icon_file);
1867 suffix = best_suffix (suffix, allow_svg);
1868 g_assert (suffix != ICON_SUFFIX_NONE);
1870 file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
1871 icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
1874 icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
1875 -1, NULL, NULL, NULL);
1878 if (min_dir->icon_data != NULL)
1879 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
1880 else if (min_dir->cache != NULL)
1881 icon_info->data = _gtk_icon_cache_get_icon_data (min_dir->cache, icon_name, min_dir->subdir);
1883 if (icon_info->data == NULL &&
1884 min_dir->cache && has_icon_file)
1886 gchar *icon_file_name, *icon_file_path;
1888 icon_file_name = g_strconcat (icon_name, ".icon", NULL);
1889 icon_file_path = g_build_filename (min_dir->dir, icon_file_name, NULL);
1891 if (g_file_test (icon_file_path, G_FILE_TEST_IS_REGULAR))
1893 if (min_dir->icon_data == NULL)
1894 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
1895 g_free, (GDestroyNotify)icon_data_free);
1896 load_icon_data (min_dir, icon_file_path, icon_file_name);
1898 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
1900 g_free (icon_file_name);
1901 g_free (icon_file_path);
1906 icon_info->cache_pixbuf = _gtk_icon_cache_get_icon (min_dir->cache, icon_name,
1910 icon_info->dir_type = min_dir->type;
1911 icon_info->dir_size = min_dir->size;
1912 icon_info->threshold = min_dir->threshold;
1921 theme_list_icons (IconTheme *theme, GHashTable *icons,
1924 GList *l = theme->dirs;
1931 if (context == dir->context ||
1936 _gtk_icon_cache_add_icons (dir->cache,
1943 g_hash_table_foreach (dir->icons,
1953 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
1955 GKeyFile *icon_file;
1964 GError *error = NULL;
1968 icon_file = g_key_file_new ();
1969 g_key_file_set_list_separator (icon_file, ',');
1970 g_key_file_load_from_file (icon_file, path, 0, &error);
1973 g_error_free (error);
1978 base_name = g_strdup (name);
1979 dot = strrchr (base_name, '.');
1982 data = g_new0 (GtkIconData, 1);
1983 g_hash_table_replace (dir->icon_data, base_name, data);
1985 ivalues = g_key_file_get_integer_list (icon_file,
1986 "Icon Data", "EmbeddedTextRectangle",
1992 data->has_embedded_rect = TRUE;
1993 data->x0 = ivalues[0];
1994 data->y0 = ivalues[1];
1995 data->x1 = ivalues[2];
1996 data->y1 = ivalues[3];
2002 str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
2005 split = g_strsplit (str, "|", -1);
2007 data->n_attach_points = g_strv_length (split);
2008 data->attach_points = g_malloc (sizeof (GdkPoint) * data->n_attach_points);
2011 while (split[i] != NULL && i < data->n_attach_points)
2013 split_point = strchr (split[i], ',');
2018 data->attach_points[i].x = atoi (split[i]);
2019 data->attach_points[i].y = atoi (split_point);
2028 data->display_name = g_key_file_get_locale_string (icon_file,
2029 "Icon Data", "DisplayName",
2031 g_key_file_free (icon_file);
2036 scan_directory (GtkIconThemePrivate *icon_theme,
2037 IconThemeDir *dir, char *full_dir)
2041 char *base_name, *dot;
2043 IconSuffix suffix, hash_suffix;
2045 GTK_NOTE (ICONTHEME,
2046 g_print ("scanning directory %s\n", full_dir));
2047 dir->icons = g_hash_table_new_full (g_str_hash, g_str_equal,
2050 gdir = g_dir_open (full_dir, 0, NULL);
2055 while ((name = g_dir_read_name (gdir)))
2057 if (g_str_has_suffix (name, ".icon"))
2059 if (dir->icon_data == NULL)
2060 dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2061 g_free, (GDestroyNotify)icon_data_free);
2063 path = g_build_filename (full_dir, name, NULL);
2064 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
2065 load_icon_data (dir, path, name);
2072 suffix = suffix_from_name (name);
2073 if (suffix == ICON_SUFFIX_NONE)
2076 base_name = g_strdup (name);
2077 dot = strrchr (base_name, '.');
2080 hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
2081 g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
2082 g_hash_table_insert (icon_theme->all_icons, base_name, NULL);
2089 theme_subdir_load (GtkIconTheme *icon_theme,
2091 GKeyFile *theme_file,
2097 IconThemeDirType type;
2098 char *context_string;
2105 GError *error = NULL;
2106 IconThemeDirMtime *dir_mtime;
2108 size = g_key_file_get_integer (theme_file, subdir, "Size", &error);
2111 g_error_free (error);
2112 g_warning ("Theme directory %s of theme %s has no size field\n",
2113 subdir, theme->name);
2117 type = ICON_THEME_DIR_THRESHOLD;
2118 type_string = g_key_file_get_string (theme_file, subdir, "Type", NULL);
2121 if (strcmp (type_string, "Fixed") == 0)
2122 type = ICON_THEME_DIR_FIXED;
2123 else if (strcmp (type_string, "Scalable") == 0)
2124 type = ICON_THEME_DIR_SCALABLE;
2125 else if (strcmp (type_string, "Threshold") == 0)
2126 type = ICON_THEME_DIR_THRESHOLD;
2128 g_free (type_string);
2132 context_string = g_key_file_get_string (theme_file, subdir, "Context", NULL);
2135 context = g_quark_from_string (context_string);
2136 g_free (context_string);
2139 max_size = g_key_file_get_integer (theme_file, subdir, "MaxSize", &error);
2144 g_error_free (error);
2148 min_size = g_key_file_get_integer (theme_file, subdir, "MinSize", &error);
2153 g_error_free (error);
2157 threshold = g_key_file_get_integer (theme_file, subdir, "Threshold", &error);
2162 g_error_free (error);
2166 for (d = icon_theme->priv->dir_mtimes; d; d = d->next)
2168 dir_mtime = (IconThemeDirMtime *)d->data;
2170 if (dir_mtime->mtime == 0)
2171 continue; /* directory doesn't exist */
2173 full_dir = g_build_filename (dir_mtime->dir, subdir, NULL);
2175 /* First, see if we have a cache for the directory */
2176 if (dir_mtime->cache != NULL || g_file_test (full_dir, G_FILE_TEST_IS_DIR))
2178 if (dir_mtime->cache == NULL)
2180 /* This will return NULL if the cache doesn't exist or is outdated */
2181 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir_mtime->dir);
2184 dir = g_new (IconThemeDir, 1);
2186 dir->context = context;
2188 dir->min_size = min_size;
2189 dir->max_size = max_size;
2190 dir->threshold = threshold;
2191 dir->dir = full_dir;
2192 dir->icon_data = NULL;
2193 dir->subdir = g_strdup (subdir);
2194 if (dir_mtime->cache != NULL)
2195 dir->cache = _gtk_icon_cache_ref (dir_mtime->cache);
2199 scan_directory (icon_theme->priv, dir, full_dir);
2202 theme->dirs = g_list_prepend (theme->dirs, dir);
2210 icon_data_free (GtkIconData *icon_data)
2212 g_free (icon_data->attach_points);
2213 g_free (icon_data->display_name);
2221 gtk_icon_info_get_type (void)
2223 static GType our_type = 0;
2226 our_type = g_boxed_type_register_static ("GtkIconInfo",
2227 (GBoxedCopyFunc) gtk_icon_info_copy,
2228 (GBoxedFreeFunc) gtk_icon_info_free);
2233 static GtkIconInfo *
2234 icon_info_new (void)
2236 GtkIconInfo *icon_info = g_new0 (GtkIconInfo, 1);
2238 icon_info->scale = -1.;
2243 static GtkIconInfo *
2244 icon_info_new_builtin (BuiltinIcon *icon)
2246 GtkIconInfo *icon_info = icon_info_new ();
2248 icon_info->builtin_pixbuf = g_object_ref (icon->pixbuf);
2249 icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
2250 icon_info->dir_size = icon->size;
2251 icon_info->threshold = 2;
2257 * gtk_icon_info_copy:
2258 * @icon_info: a #GtkIconInfo
2260 * Make a copy of a #GtkIconInfo.
2262 * Return value: the new GtkIconInfo
2267 gtk_icon_info_copy (GtkIconInfo *icon_info)
2271 g_return_val_if_fail (icon_info != NULL, NULL);
2273 copy = g_memdup (icon_info, sizeof (GtkIconInfo));
2274 if (copy->builtin_pixbuf)
2275 g_object_ref (copy->builtin_pixbuf);
2277 g_object_ref (copy->pixbuf);
2278 if (copy->load_error)
2279 copy->load_error = g_error_copy (copy->load_error);
2281 copy->filename = g_strdup (copy->filename);
2283 if (copy->cp_filename)
2284 copy->cp_filename = g_strdup (copy->cp_filename);
2291 * gtk_icon_info_free:
2292 * @icon_info: a #GtkIconInfo
2294 * Free a #GtkIconInfo and associated information
2299 gtk_icon_info_free (GtkIconInfo *icon_info)
2301 g_return_if_fail (icon_info != NULL);
2303 if (icon_info->filename)
2304 g_free (icon_info->filename);
2306 if (icon_info->cp_filename)
2307 g_free (icon_info->cp_filename);
2309 if (icon_info->builtin_pixbuf)
2310 g_object_unref (icon_info->builtin_pixbuf);
2311 if (icon_info->pixbuf)
2312 g_object_unref (icon_info->pixbuf);
2313 if (icon_info->cache_pixbuf)
2314 g_object_unref (icon_info->cache_pixbuf);
2320 * gtk_icon_info_get_base_size:
2321 * @icon_info: a #GtkIconInfo
2323 * Gets the base size for the icon. The base size
2324 * is a size for the icon that was specified by
2325 * the icon theme creator. This may be different
2326 * than the actual size of image; an example of
2327 * this is small emblem icons that can be attached
2328 * to a larger icon. These icons will be given
2329 * the same base size as the larger icons to which
2330 * they are attached.
2332 * Return value: the base size, or 0, if no base
2333 * size is known for the icon.
2338 gtk_icon_info_get_base_size (GtkIconInfo *icon_info)
2340 g_return_val_if_fail (icon_info != NULL, 0);
2342 return icon_info->dir_size;
2346 * gtk_icon_info_get_filename:
2347 * @icon_info: a #GtkIconInfo
2349 * Gets the filename for the icon. If the
2350 * %GTK_ICON_LOOKUP_USE_BUILTIN flag was passed
2351 * to gtk_icon_theme_lookup_icon(), there may be
2352 * no filename if a builtin icon is returned; in this
2353 * case, you should use gtk_icon_info_get_builtin_pixbuf().
2355 * Return value: the filename for the icon, or %NULL
2356 * if gtk_icon_info_get_builtin_pixbuf() should
2357 * be used instead. The return value is owned by
2358 * GTK+ and should not be modified or freed.
2362 G_CONST_RETURN gchar *
2363 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2365 g_return_val_if_fail (icon_info != NULL, NULL);
2367 return icon_info->filename;
2371 * gtk_icon_info_get_builtin_pixbuf:
2372 * @icon_info: a #GtkIconInfo structure
2374 * Gets the built-in image for this icon, if any. To allow
2375 * GTK+ to use built in icon images, you must pass the
2376 * %GTK_ICON_LOOKUP_USE_BUILTIN to
2377 * gtk_icon_theme_lookup_icon().
2379 * Return value: the built-in image pixbuf, or %NULL. No
2380 * extra reference is added to the returned pixbuf, so if
2381 * you want to keep it around, you must use g_object_ref().
2382 * The returned image must not be modified.
2387 gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
2389 g_return_val_if_fail (icon_info != NULL, NULL);
2391 return icon_info->builtin_pixbuf;
2395 load_svg_at_size (const gchar *filename,
2399 GdkPixbuf *pixbuf = NULL;
2400 GdkPixbufLoader *loader = NULL;
2401 gchar *contents = NULL;
2404 if (!g_file_get_contents (filename,
2405 &contents, &length, error))
2408 loader = gdk_pixbuf_loader_new_with_type ("svg", error);
2412 gdk_pixbuf_loader_set_size (loader, size, size);
2414 if (!gdk_pixbuf_loader_write (loader, contents, length, error))
2416 gdk_pixbuf_loader_close (loader, NULL);
2420 if (!gdk_pixbuf_loader_close (loader, error))
2423 pixbuf = g_object_ref (gdk_pixbuf_loader_get_pixbuf (loader));
2427 g_object_unref (loader);
2434 /* This function contains the complicatd logic for deciding
2435 * on the size at which to load the icon and loading it at
2439 icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
2440 gboolean scale_only)
2442 int image_width, image_height;
2443 GdkPixbuf *source_pixbuf;
2445 /* First check if we already succeeded have the necessary
2446 * information (or failed earlier)
2448 if (scale_only && icon_info->scale >= 0)
2451 if (icon_info->pixbuf)
2454 if (icon_info->load_error)
2457 /* SVG icons are a special case - we just immediately scale them
2458 * to the desired size
2460 if (icon_info->filename && g_str_has_suffix (icon_info->filename, ".svg"))
2462 icon_info->scale = icon_info->desired_size / 1000.;
2467 icon_info->pixbuf = load_svg_at_size (icon_info->filename,
2468 icon_info->desired_size,
2469 &icon_info->load_error);
2471 return icon_info->pixbuf != NULL;
2474 /* In many cases, the scale can be determined without actual access
2475 * to the icon file. This is generally true when we have a size
2476 * for the directory where the icon is; the image size doesn't
2477 * matter in that case.
2479 if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
2480 icon_info->scale = 1.0;
2481 else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
2483 if (icon_info->desired_size >= icon_info->dir_size - icon_info->threshold &&
2484 icon_info->desired_size <= icon_info->dir_size + icon_info->threshold)
2485 icon_info->scale = 1.0;
2486 else if (icon_info->dir_size > 0)
2487 icon_info->scale =(gdouble) icon_info->desired_size / icon_info->dir_size;
2489 else if (icon_info->dir_type == ICON_THEME_DIR_SCALABLE)
2491 if (icon_info->dir_size > 0)
2492 icon_info->scale = (gdouble) icon_info->desired_size / icon_info->dir_size;
2495 if (icon_info->scale >= 0. && scale_only)
2498 /* At this point, we need to actually get the icon; either from the
2499 * builting image or by loading the file
2501 if (icon_info->builtin_pixbuf)
2502 source_pixbuf = g_object_ref (icon_info->builtin_pixbuf);
2503 else if (icon_info->cache_pixbuf)
2504 source_pixbuf = g_object_ref (icon_info->cache_pixbuf);
2508 source_pixbuf = gdk_pixbuf_new_from_file (icon_info->filename,
2509 &icon_info->load_error);
2514 /* Do scale calculations that depend on the image size
2516 image_width = gdk_pixbuf_get_width (source_pixbuf);
2517 image_height = gdk_pixbuf_get_height (source_pixbuf);
2519 if (icon_info->scale < 0.0)
2521 gint image_size = MAX (image_width, image_height);
2523 icon_info->scale = icon_info->desired_size / (gdouble)image_size;
2525 icon_info->scale = 1.0;
2527 if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED)
2528 icon_info->scale = MIN (icon_info->scale, 1.0);
2531 /* We don't short-circuit out here for scale_only, since, now
2532 * we've loaded the icon, we might as well go ahead and finish
2533 * the job. This is a bit of a waste when we scale here
2534 * and never get the final pixbuf; at the cost of a bit of
2535 * extra complexity, we could keep the source pixbuf around
2536 * but not actually scale it until neede.
2539 if (icon_info->scale == 1.0)
2540 icon_info->pixbuf = source_pixbuf;
2543 icon_info->pixbuf = gdk_pixbuf_scale_simple (source_pixbuf,
2544 0.5 + image_width * icon_info->scale,
2545 0.5 + image_height * icon_info->scale,
2546 GDK_INTERP_BILINEAR);
2547 g_object_unref (source_pixbuf);
2554 * gtk_icon_info_load_icon:
2555 * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
2556 * @error: location to store error information on failure, or %NULL.
2558 * Renders an icon previously looked up in an icon theme using
2559 * gtk_icon_theme_lookup_icon(); the size will be based on the size
2560 * passed to gtk_icon_theme_lookup_icon(). Note that the resulting
2561 * pixbuf may not be exactly this size; an icon theme may have icons
2562 * that differ slightly from their nominal sizes, and in addition GTK+
2563 * will avoid scaling icons that it considers sufficiently close to the
2564 * requested size or for which the source image would have to be scaled
2565 * up too far. (This maintains sharpness.)
2567 * Return value: the rendered icon; this may be a newly created icon
2568 * or a new reference to an internal icon, so you must not modify
2569 * the icon. Use g_object_unref() to release your reference to the
2575 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
2578 g_return_val_if_fail (icon_info != NULL, NULL);
2580 g_return_val_if_fail (icon_info != NULL, NULL);
2581 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2583 icon_info_ensure_scale_and_pixbuf (icon_info, FALSE);
2585 if (icon_info->load_error)
2587 g_propagate_error (error, icon_info->load_error);
2591 return g_object_ref (icon_info->pixbuf);
2595 * gtk_icon_info_set_raw_coordinates:
2596 * @icon_info: a #GtkIconInfo
2597 * @raw_coordinates: whether the coordinates of embedded rectangles
2598 * and attached points should be returned in their original
2601 * Sets whether the coordinates returned by gtk_icon_info_get_embedded_rect()
2602 * and gtk_icon_info_get_attach_points() should be returned in their
2603 * original form as specified in the icon theme, instead of scaled
2604 * appropriately for the pixbuf returned by gtk_icon_info_load_icon().
2606 * Raw coordinates are somewhat strange; they are specified to be with
2607 * respect to the unscaled pixmap for PNG and XPM icons, but for SVG
2608 * icons, they are in a 1000x1000 coordinate space that is scaled
2609 * to the final size of the icon. You can determine if the icon is an SVG
2610 * icon by using gtk_icon_info_get_filename(), and seeing if it is non-%NULL
2611 * and ends in '.svg'.
2613 * This function is provided primarily to allow compatibility wrappers
2614 * for older API's, and is not expected to be useful for applications.
2619 gtk_icon_info_set_raw_coordinates (GtkIconInfo *icon_info,
2620 gboolean raw_coordinates)
2622 g_return_if_fail (icon_info != NULL);
2624 icon_info->raw_coordinates = raw_coordinates != FALSE;
2627 /* Scale coordinates from the icon data prior to returning
2631 icon_info_scale_point (GtkIconInfo *icon_info,
2637 if (icon_info->raw_coordinates)
2644 if (!icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2647 *x_out = 0.5 + x * icon_info->scale;
2648 *y_out = 0.5 + y * icon_info->scale;
2655 * gtk_icon_info_get_embedded_rect:
2656 * @icon_info: a #GtkIconInfo
2657 * @rectangle: #GdkRectangle in which to store embedded
2658 * rectangle coordinates; coordinates are only stored
2659 * when this function returns %TRUE.
2661 * Gets the coordinates of a rectangle within the icon
2662 * that can be used for display of information such
2663 * as a preview of the contents of a text file.
2664 * See gtk_icon_info_set_raw_coordinates() for further
2665 * information about the coordinate system.
2667 * Return value: %TRUE if the icon has an embedded rectangle
2672 gtk_icon_info_get_embedded_rect (GtkIconInfo *icon_info,
2673 GdkRectangle *rectangle)
2675 g_return_val_if_fail (icon_info != NULL, FALSE);
2677 if (icon_info->data && icon_info->data->has_embedded_rect &&
2678 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2680 gint scaled_x0, scaled_y0;
2681 gint scaled_x1, scaled_y1;
2685 icon_info_scale_point (icon_info,
2686 icon_info->data->x0, icon_info->data->y0,
2687 &scaled_x0, &scaled_y0);
2688 icon_info_scale_point (icon_info,
2689 icon_info->data->x1, icon_info->data->y1,
2690 &scaled_x1, &scaled_y1);
2692 rectangle->x = scaled_x0;
2693 rectangle->y = scaled_y0;
2694 rectangle->width = scaled_x1 - rectangle->x;
2695 rectangle->height = scaled_y1 - rectangle->y;
2705 * gtk_icon_info_get_attach_points:
2706 * @icon_info: a #GtkIconInfo
2707 * @points: location to store pointer to an array of points, or %NULL
2708 * free the array of points with g_free().
2709 * @n_points: location to store the number of points in @points, or %NULL
2711 * Fetches the set of attach points for an icon. An attach point
2712 * is a location in the icon that can be used as anchor points for attaching
2713 * emblems or overlays to the icon.
2715 * Return value: %TRUE if there are any attach points for the icon.
2720 gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
2724 g_return_val_if_fail (icon_info != NULL, FALSE);
2726 if (icon_info->data && icon_info->data->n_attach_points &&
2727 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2733 *points = g_new (GdkPoint, icon_info->data->n_attach_points);
2734 for (i = 0; i < icon_info->data->n_attach_points; i++)
2735 icon_info_scale_point (icon_info,
2736 icon_info->data->attach_points[i].x,
2737 icon_info->data->attach_points[i].y,
2743 *n_points = icon_info->data->n_attach_points;
2759 * gtk_icon_info_get_display_name:
2760 * @icon_info: a #GtkIconInfo
2762 * Gets the display name for an icon. A display name is a
2763 * string to be used in place of the icon name in a user
2764 * visible context like a list of icons.
2766 * Return value: the display name for the icon or %NULL, if
2767 * the icon doesn't have a specified display name. This value
2768 * is owned @icon_info and must not be modified or free.
2772 G_CONST_RETURN gchar *
2773 gtk_icon_info_get_display_name (GtkIconInfo *icon_info)
2775 g_return_val_if_fail (icon_info != NULL, NULL);
2777 if (icon_info->data)
2778 return icon_info->data->display_name;
2789 * gtk_icon_theme_add_builtin_icon:
2790 * @icon_name: the name of the icon to register
2791 * @size: the size at which to register the icon (different
2792 * images can be registered for the same icon name
2793 * at different sizes.)
2794 * @pixbuf: #GdkPixbuf that contains the image to use
2797 * Registers a built-in icon for icon theme lookups. The idea
2798 * of built-in icons is to allow an application or library
2799 * that uses themed icons to function requiring files to
2800 * be present in the file system. For instance, the default
2801 * images for all of GTK+'s stock icons are registered
2804 * In general, if you use gtk_icon_theme_add_builtin_icon()
2805 * you should also install the icon in the icon theme, so
2806 * that the icon is generally available.
2808 * This function will generally be used with pixbufs loaded
2809 * via gdk_pixbuf_new_from_inline().
2814 gtk_icon_theme_add_builtin_icon (const gchar *icon_name,
2818 BuiltinIcon *default_icon;
2822 g_return_if_fail (icon_name != NULL);
2823 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2825 if (!icon_theme_builtin_icons)
2826 icon_theme_builtin_icons = g_hash_table_new (g_str_hash, g_str_equal);
2828 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
2830 key = g_strdup (icon_name);
2832 key = (gpointer)icon_name; /* Won't get stored */
2834 default_icon = g_new (BuiltinIcon, 1);
2835 default_icon->size = size;
2836 default_icon->pixbuf = g_object_ref (pixbuf);
2837 icons = g_slist_prepend (icons, default_icon);
2839 /* Replaces value, leaves key untouched
2841 g_hash_table_insert (icon_theme_builtin_icons, key, icons);
2844 /* Look up a builtin icon; the min_difference_p and
2845 * has_larger_p out parameters allow us to combine
2846 * this lookup with searching through the actual directories
2847 * of the "hicolor" icon theme. See theme_lookup_icon()
2848 * for how they are used.
2850 static BuiltinIcon *
2851 find_builtin_icon (const gchar *icon_name,
2853 gint *min_difference_p,
2854 gboolean *has_larger_p)
2856 GSList *icons = NULL;
2857 gint min_difference = G_MAXINT;
2858 gboolean has_larger = FALSE;
2859 BuiltinIcon *min_icon = NULL;
2861 _gtk_icon_factory_ensure_default_icons ();
2863 if (!icon_theme_builtin_icons)
2866 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
2870 BuiltinIcon *default_icon = icons->data;
2871 int min, max, difference;
2874 min = default_icon->size - 2;
2875 max = default_icon->size + 2;
2876 smaller = size < min;
2878 difference = min - size;
2879 else if (size > max)
2880 difference = size - max;
2884 if (difference == 0)
2886 min_icon = default_icon;
2892 if (difference < min_difference || smaller)
2894 min_difference = difference;
2895 min_icon = default_icon;
2896 has_larger = smaller;
2901 if (difference < min_difference && smaller)
2903 min_difference = difference;
2904 min_icon = default_icon;
2908 icons = icons->next;
2911 if (min_difference_p)
2912 *min_difference_p = min_difference;
2914 *has_larger_p = has_larger;
2921 /* DLL ABI stability backward compatibility versions */
2923 #undef gtk_icon_theme_set_search_path
2926 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
2927 const gchar *path[],
2930 const gchar **utf8_path;
2933 utf8_path = g_new (const gchar *, n_elements);
2935 for (i = 0; i < n_elements; i++)
2936 utf8_path[i] = g_locale_to_utf8 (path[i], -1, NULL, NULL, NULL);
2938 gtk_icon_theme_set_search_path_utf8 (icon_theme, utf8_path, n_elements);
2940 for (i = 0; i < n_elements; i++)
2941 g_free ((gchar *) utf8_path[i]);
2946 #undef gtk_icon_theme_get_search_path
2949 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
2955 gtk_icon_theme_get_search_path_utf8 (icon_theme, path, &n);
2962 for (i = 0; i < n; i++)
2964 gchar *tem = (*path)[i];
2966 (*path)[i] = g_locale_from_utf8 ((*path)[i], -1, NULL, NULL, NULL);
2972 #undef gtk_icon_theme_append_search_path
2975 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
2978 gchar *utf8_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
2980 gtk_icon_theme_append_search_path_utf8 (icon_theme, utf8_path);
2985 #undef gtk_icon_theme_prepend_search_path
2988 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
2991 gchar *utf8_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
2993 gtk_icon_theme_prepend_search_path_utf8 (icon_theme, utf8_path);
2998 #undef gtk_icon_info_get_filename
3000 G_CONST_RETURN gchar *
3001 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
3003 g_return_val_if_fail (icon_info != NULL, NULL);
3005 return icon_info->cp_filename;
3010 #define __GTK_ICON_THEME_C__
3011 #include "gtkaliasdef.c"