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;
84 GList *unthemed_icons_caches;
86 /* Note: The keys of this hashtable are owned by the
87 * themedir and unthemed hashtables.
89 GHashTable *all_icons;
91 /* GdkScreen for the icon theme (may be NULL)
95 /* time when we last stat:ed for theme changes */
102 /* Information about the source
106 /* System codepage version of filename, for DLL ABI backward
107 * compatibility functions.
111 GdkPixbuf *builtin_pixbuf;
113 /* Cache pixbuf (if there is any) */
114 GdkPixbuf *cache_pixbuf;
118 /* Information about the directory where
119 * the source was found
121 IconThemeDirType dir_type;
125 /* Parameters influencing the scaled icon
128 gboolean raw_coordinates;
130 /* Cached information if we go ahead and try to load
145 /* In search order */
151 IconThemeDirType type;
165 GHashTable *icon_data;
171 char *no_svg_filename;
183 time_t mtime; /* 0 == not existing or not a dir */
188 static void gtk_icon_theme_class_init (GtkIconThemeClass *klass);
189 static void gtk_icon_theme_init (GtkIconTheme *icon_theme);
190 static void gtk_icon_theme_finalize (GObject *object);
191 static void theme_dir_destroy (IconThemeDir *dir);
193 static void theme_destroy (IconTheme *theme);
194 static GtkIconInfo *theme_lookup_icon (IconTheme *theme,
195 const char *icon_name,
198 gboolean use_default_icons);
199 static void theme_list_icons (IconTheme *theme,
202 static void theme_subdir_load (GtkIconTheme *icon_theme,
204 GKeyFile *theme_file,
206 static void do_theme_change (GtkIconTheme *icon_theme);
208 static void blow_themes (GtkIconTheme *icon_themes);
210 static void icon_data_free (GtkIconData *icon_data);
211 static void load_icon_data (IconThemeDir *dir,
215 static IconSuffix theme_dir_get_icon_suffix (IconThemeDir *dir,
216 const gchar *icon_name,
217 gboolean *has_icon_file);
220 static GtkIconInfo *icon_info_new (void);
221 static GtkIconInfo *icon_info_new_builtin (BuiltinIcon *icon);
223 static IconSuffix suffix_from_name (const char *name);
225 static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
227 gint *min_difference_p,
228 gboolean *has_larger_p);
230 static GObjectClass *parent_class = NULL;
232 static guint signal_changed = 0;
234 static GHashTable *icon_theme_builtin_icons;
237 gtk_icon_theme_get_type (void)
239 static GType type = 0;
243 static const GTypeInfo info =
245 sizeof (GtkIconThemeClass),
246 NULL, /* base_init */
247 NULL, /* base_finalize */
248 (GClassInitFunc) gtk_icon_theme_class_init,
249 NULL, /* class_finalize */
250 NULL, /* class_data */
251 sizeof (GtkIconTheme),
253 (GInstanceInitFunc) gtk_icon_theme_init,
256 type = g_type_register_static (G_TYPE_OBJECT, "GtkIconTheme", &info, 0);
263 * gtk_icon_theme_new:
265 * Creates a new icon theme object. Icon theme objects are used
266 * to lookup up an icon by name in a particular icon theme.
267 * Usually, you'll want to use gtk_icon_theme_get_default()
268 * or gtk_icon_theme_get_for_screen() rather than creating
269 * a new icon theme object for scratch.
271 * Return value: the newly created #GtkIconTheme object.
276 gtk_icon_theme_new (void)
278 return g_object_new (GTK_TYPE_ICON_THEME, NULL);
282 * gtk_icon_theme_get_default:
284 * Gets the icon theme for the default screen. See
285 * gtk_icon_theme_get_for_screen().
287 * Return value: A unique #GtkIconTheme associated with
288 * the default screen. This icon theme is associated with
289 * the screen and can be used as long as the screen
290 * is open. Do not ref or unref it.
295 gtk_icon_theme_get_default (void)
297 return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
301 * gtk_icon_theme_get_for_screen:
302 * @screen: a #GdkScreen
304 * Gets the icon theme object associated with @screen; if this
305 * function has not previously been called for the given
306 * screen, a new icon theme object will be created and
307 * associated with the screen. Icon theme objects are
308 * fairly expensive to create, so using this function
309 * is usually a better choice than calling than gtk_icon_theme_new()
310 * and setting the screen yourself; by using this function
311 * a single icon theme object will be shared between users.
313 * Return value: A unique #GtkIconTheme associated with
314 * the given screen. This icon theme is associated with
315 * the screen and can be used as long as the screen
316 * is open. Do not ref or unref it.
321 gtk_icon_theme_get_for_screen (GdkScreen *screen)
323 GtkIconTheme *icon_theme;
325 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
326 g_return_val_if_fail (!screen->closed, NULL);
328 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
331 GtkIconThemePrivate *priv;
333 icon_theme = gtk_icon_theme_new ();
334 gtk_icon_theme_set_screen (icon_theme, screen);
336 priv = icon_theme->priv;
337 priv->is_screen_singleton = TRUE;
339 g_object_set_data (G_OBJECT (screen), "gtk-icon-theme", icon_theme);
346 gtk_icon_theme_class_init (GtkIconThemeClass *klass)
348 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
350 parent_class = g_type_class_peek_parent (klass);
352 gobject_class->finalize = gtk_icon_theme_finalize;
355 * GtkIconTheme::changed
356 * @icon_theme: the icon theme
358 * Emitted when the current icon theme is switched or GTK+ detects
359 * that a change has occurred in the contents of the current
362 signal_changed = g_signal_new ("changed",
363 G_TYPE_FROM_CLASS (klass),
365 G_STRUCT_OFFSET (GtkIconThemeClass, changed),
367 g_cclosure_marshal_VOID__VOID,
370 g_type_class_add_private (klass, sizeof (GtkIconThemePrivate));
374 /* Callback when the display that the icon theme is attached to
375 * is closed; unset the screen, and if it's the unique theme
376 * for the screen, drop the reference
379 display_closed (GdkDisplay *display,
381 GtkIconTheme *icon_theme)
383 GtkIconThemePrivate *priv = icon_theme->priv;
384 GdkScreen *screen = priv->screen;
385 gboolean was_screen_singleton = priv->is_screen_singleton;
387 if (was_screen_singleton)
389 g_object_set_data (G_OBJECT (screen), "gtk-icon-theme", NULL);
390 priv->is_screen_singleton = FALSE;
393 gtk_icon_theme_set_screen (icon_theme, NULL);
395 if (was_screen_singleton)
397 g_object_unref (icon_theme);
402 update_current_theme (GtkIconTheme *icon_theme)
404 GtkIconThemePrivate *priv = icon_theme->priv;
406 if (!priv->custom_theme)
412 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
413 g_object_get (settings, "gtk-icon-theme-name", &theme, NULL);
417 theme = g_strdup (DEFAULT_THEME_NAME);
419 if (strcmp (priv->current_theme, theme) != 0)
421 g_free (priv->current_theme);
422 priv->current_theme = theme;
424 do_theme_change (icon_theme);
431 /* Callback when the icon theme GtkSetting changes
434 theme_changed (GtkSettings *settings,
436 GtkIconTheme *icon_theme)
438 update_current_theme (icon_theme);
442 unset_screen (GtkIconTheme *icon_theme)
444 GtkIconThemePrivate *priv = icon_theme->priv;
445 GtkSettings *settings;
450 settings = gtk_settings_get_for_screen (priv->screen);
451 display = gdk_screen_get_display (priv->screen);
453 g_signal_handlers_disconnect_by_func (display,
454 (gpointer) display_closed,
456 g_signal_handlers_disconnect_by_func (settings,
457 (gpointer) theme_changed,
465 * gtk_icon_theme_set_screen:
466 * @icon_theme: a #GtkIconTheme
467 * @screen: a #GdkScreen
469 * Sets the screen for an icon theme; the screen is used
470 * to track the user's currently configured icon theme,
471 * which might be different for different screens.
476 gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
479 GtkIconThemePrivate *priv;
480 GtkSettings *settings;
483 g_return_if_fail (GTK_ICON_THEME (icon_theme));
484 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
486 priv = icon_theme->priv;
488 unset_screen (icon_theme);
492 display = gdk_screen_get_display (screen);
493 settings = gtk_settings_get_for_screen (screen);
495 priv->screen = screen;
497 g_signal_connect (display, "closed",
498 G_CALLBACK (display_closed), icon_theme);
499 g_signal_connect (settings, "notify::gtk-icon-theme-name",
500 G_CALLBACK (theme_changed), icon_theme);
503 update_current_theme (icon_theme);
506 /* Checks whether a loader for SVG files has been registered
510 pixbuf_supports_svg (void)
512 GSList *formats = gdk_pixbuf_get_formats ();
514 static gboolean found_svg = FALSE;
515 static gboolean value_known = FALSE;
520 for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
522 gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
525 for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
527 if (strcmp (*mime_type, "image/svg") == 0)
531 g_strfreev (mime_types);
534 g_slist_free (formats);
541 gtk_icon_theme_init (GtkIconTheme *icon_theme)
543 GtkIconThemePrivate *priv;
544 const gchar * const *xdg_data_dirs;
547 priv = g_type_instance_get_private ((GTypeInstance *)icon_theme,
548 GTK_TYPE_ICON_THEME);
549 icon_theme->priv = priv;
551 priv->custom_theme = FALSE;
552 priv->current_theme = g_strdup (DEFAULT_THEME_NAME);
554 xdg_data_dirs = g_get_system_data_dirs ();
555 for (i = 0; xdg_data_dirs[i]; i++) ;
557 priv->search_path_len = 2 * i + 2;
559 priv->search_path = g_new (char *, priv->search_path_len);
562 priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
563 priv->search_path[i++] = g_build_filename (g_get_user_data_dir (), "icons", NULL);
565 for (j = 0; xdg_data_dirs[j]; j++)
566 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
568 for (j = 0; xdg_data_dirs[j]; j++)
569 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "pixmaps", NULL);
571 priv->themes_valid = FALSE;
573 priv->unthemed_icons = NULL;
574 priv->unthemed_icons_caches = 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 do_theme_change (GtkIconTheme *icon_theme)
593 GtkIconThemePrivate *priv = icon_theme->priv;
596 g_print ("change to icon theme \"%s\"\n", priv->current_theme));
597 blow_themes (icon_theme);
598 g_signal_emit (icon_theme, signal_changed, 0);
600 if (priv->screen && priv->is_screen_singleton)
602 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
603 gtk_rc_reset_styles (settings);
608 free_cache (gpointer data,
611 GtkIconCache *cache = (GtkIconCache *)data;
614 _gtk_icon_cache_unref (cache);
618 blow_themes (GtkIconTheme *icon_theme)
620 GtkIconThemePrivate *priv = icon_theme->priv;
622 if (priv->themes_valid)
624 g_hash_table_destroy (priv->all_icons);
625 g_list_foreach (priv->themes, (GFunc)theme_destroy, NULL);
626 g_list_free (priv->themes);
627 g_list_foreach (priv->dir_mtimes, (GFunc)free_dir_mtime, NULL);
628 g_list_free (priv->dir_mtimes);
629 g_hash_table_destroy (priv->unthemed_icons);
630 if (priv->unthemed_icons_caches)
631 g_list_foreach (priv->unthemed_icons_caches, free_cache, NULL);
632 g_list_free (priv->unthemed_icons_caches);
635 priv->unthemed_icons = NULL;
636 priv->unthemed_icons_caches = NULL;
637 priv->dir_mtimes = NULL;
638 priv->all_icons = NULL;
639 priv->themes_valid = FALSE;
643 gtk_icon_theme_finalize (GObject *object)
645 GtkIconTheme *icon_theme;
646 GtkIconThemePrivate *priv;
649 icon_theme = GTK_ICON_THEME (object);
650 priv = icon_theme->priv;
652 unset_screen (icon_theme);
654 g_free (priv->current_theme);
655 priv->current_theme = NULL;
657 for (i=0; i < priv->search_path_len; i++)
658 g_free (priv->search_path[i]);
660 g_free (priv->search_path);
661 priv->search_path = NULL;
663 blow_themes (icon_theme);
665 G_OBJECT_CLASS (parent_class)->finalize (object);
669 * gtk_icon_theme_set_search_path:
670 * @icon_theme: a #GtkIconTheme
671 * @path: array of directories that are searched for icon themes
672 * @n_elements: number of elements in @path.
674 * Sets the search path for the icon theme object. When looking
675 * for an icon theme, GTK+ will search for a subdirectory of
676 * one or more of the directories in @path with the same name
677 * as the icon theme. (Themes from multiple of the path elements
678 * are combined to allow themes to be extended by adding icons
679 * in the user's home directory.)
681 * In addition if an icon found isn't found either in the current
682 * icon theme or the default icon theme, and an image file with
683 * the right name is found directly in one of the elements of
684 * @path, then that image will be used for the icon name.
685 * (This is legacy feature, and new icons should be put
686 * into the default icon theme, which is called DEFAULT_THEME_NAME,
687 * rather than directly on the icon path.)
692 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
696 GtkIconThemePrivate *priv;
699 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
701 priv = icon_theme->priv;
702 for (i = 0; i < priv->search_path_len; i++)
703 g_free (priv->search_path[i]);
705 g_free (priv->search_path);
707 priv->search_path = g_new (gchar *, n_elements);
708 priv->search_path_len = n_elements;
709 for (i = 0; i < priv->search_path_len; i++)
710 priv->search_path[i] = g_strdup (path[i]);
712 do_theme_change (icon_theme);
717 * gtk_icon_theme_get_search_path:
718 * @icon_theme: a #GtkIconTheme
719 * @path: location to store a list of icon theme path directories or %NULL
720 * The stored value should be freed with g_strfreev().
721 * @n_elements: location to store number of elements
724 * Gets the current search path. See gtk_icon_theme_set_search_path().
729 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
733 GtkIconThemePrivate *priv;
736 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
738 priv = icon_theme->priv;
741 *n_elements = priv->search_path_len;
745 *path = g_new (gchar *, priv->search_path_len + 1);
746 for (i = 0; i < priv->search_path_len; i++)
747 (*path)[i] = g_strdup (priv->search_path[i]);
753 * gtk_icon_theme_append_search_path:
754 * @icon_theme: a #GtkIconTheme
755 * @path: directory name to append to the icon path
757 * Appends a directory to the search path.
758 * See gtk_icon_theme_set_search_path().
763 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
766 GtkIconThemePrivate *priv;
768 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
769 g_return_if_fail (path != NULL);
771 priv = icon_theme->priv;
773 priv->search_path_len++;
774 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
775 priv->search_path[priv->search_path_len-1] = g_strdup (path);
777 do_theme_change (icon_theme);
781 * gtk_icon_theme_prepend_search_path:
782 * @icon_theme: a #GtkIconTheme
783 * @path: directory name to prepend to the icon path
785 * Prepends a directory to the search path.
786 * See gtk_icon_theme_set_search_path().
791 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
794 GtkIconThemePrivate *priv;
797 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
798 g_return_if_fail (path != NULL);
800 priv = icon_theme->priv;
802 priv->search_path_len++;
803 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
805 for (i = priv->search_path_len - 1; i > 0; i--)
806 priv->search_path[i] = priv->search_path[i - 1];
808 priv->search_path[0] = g_strdup (path);
810 do_theme_change (icon_theme);
814 * gtk_icon_theme_set_custom_theme:
815 * @icon_theme: a #GtkIconTheme
816 * @theme_name: name of icon theme to use instead of configured theme,
817 * or %NULL to unset a previously set custom theme
819 * Sets the name of the icon theme that the #GtkIconTheme object uses
820 * overriding system configuration. This function cannot be called
821 * on the icon theme objects returned from gtk_icon_theme_get_default()
822 * and gtk_icon_theme_get_for_screen().
827 gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
828 const gchar *theme_name)
830 GtkIconThemePrivate *priv;
832 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
834 priv = icon_theme->priv;
836 g_return_if_fail (!priv->is_screen_singleton);
838 if (theme_name != NULL)
840 priv->custom_theme = TRUE;
841 if (strcmp (theme_name, priv->current_theme) != 0)
843 g_free (priv->current_theme);
844 priv->current_theme = g_strdup (theme_name);
846 do_theme_change (icon_theme);
851 priv->custom_theme = FALSE;
853 update_current_theme (icon_theme);
858 insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
864 GtkIconThemePrivate *priv;
867 GKeyFile *theme_file;
868 GError *error = NULL;
869 IconThemeDirMtime *dir_mtime;
870 struct stat stat_buf;
872 priv = icon_theme->priv;
874 for (l = priv->themes; l != NULL; l = l->next)
877 if (strcmp (theme->name, theme_name) == 0)
881 for (i = 0; i < priv->search_path_len; i++)
883 path = g_build_filename (priv->search_path[i],
886 dir_mtime = g_new (IconThemeDirMtime, 1);
887 dir_mtime->cache = NULL;
888 dir_mtime->dir = path;
889 if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
890 dir_mtime->mtime = stat_buf.st_mtime;
892 dir_mtime->mtime = 0;
894 priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
896 priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
899 for (i = 0; i < priv->search_path_len && !theme_file; i++)
901 path = g_build_filename (priv->search_path[i],
905 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
907 theme_file = g_key_file_new ();
908 g_key_file_set_list_separator (theme_file, ',');
909 g_key_file_load_from_file (theme_file, path, 0, &error);
912 g_key_file_free (theme_file);
914 g_error_free (error);
921 if (theme_file == NULL)
924 theme = g_new (IconTheme, 1);
925 theme->display_name =
926 g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
927 if (!theme->display_name)
929 g_warning ("Theme file for %s has no name\n", theme_name);
931 g_key_file_free (theme_file);
935 dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
938 g_warning ("Theme file for %s has no directories\n", theme_name);
939 g_free (theme->display_name);
941 g_key_file_free (theme_file);
945 theme->name = g_strdup (theme_name);
947 g_key_file_get_locale_string (theme_file,
948 "Icon Theme", "Comment",
951 g_key_file_get_string (theme_file,
952 "Icon Theme", "Example",
956 for (i = 0; dirs[i] != NULL; i++)
957 theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
961 theme->dirs = g_list_reverse (theme->dirs);
963 /* Prepend the finished theme */
964 priv->themes = g_list_prepend (priv->themes, theme);
966 themes = g_key_file_get_string_list (theme_file,
973 for (i = 0; themes[i] != NULL; i++)
974 insert_theme (icon_theme, themes[i]);
979 g_key_file_free (theme_file);
983 free_unthemed_icon (UnthemedIcon *unthemed_icon)
985 if (unthemed_icon->svg_filename)
986 g_free (unthemed_icon->svg_filename);
987 if (unthemed_icon->no_svg_filename)
988 g_free (unthemed_icon->no_svg_filename);
989 g_free (unthemed_icon);
993 load_themes (GtkIconTheme *icon_theme)
995 GtkIconThemePrivate *priv;
998 char *dir, *base_name, *dot;
1001 UnthemedIcon *unthemed_icon;
1002 IconSuffix old_suffix, new_suffix;
1005 priv = icon_theme->priv;
1007 priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
1009 insert_theme (icon_theme, priv->current_theme);
1011 /* Always look in the "default" icon theme */
1012 insert_theme (icon_theme, DEFAULT_THEME_NAME);
1013 priv->themes = g_list_reverse (priv->themes);
1016 priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
1017 g_free, (GDestroyNotify)free_unthemed_icon);
1019 for (base = 0; base < icon_theme->priv->search_path_len; base++)
1021 GtkIconCache *cache;
1022 dir = icon_theme->priv->search_path[base];
1024 cache = _gtk_icon_cache_new_for_path (dir);
1028 priv->unthemed_icons_caches = g_list_prepend (priv->unthemed_icons_caches, cache);
1033 gdir = g_dir_open (dir, 0, NULL);
1038 while ((file = g_dir_read_name (gdir)))
1040 new_suffix = suffix_from_name (file);
1042 if (new_suffix != ICON_SUFFIX_NONE)
1044 abs_file = g_build_filename (dir, file, NULL);
1046 base_name = g_strdup (file);
1048 dot = strrchr (base_name, '.');
1052 if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
1055 if (new_suffix == ICON_SUFFIX_SVG)
1057 if (unthemed_icon->svg_filename)
1060 unthemed_icon->svg_filename = abs_file;
1064 if (unthemed_icon->no_svg_filename)
1066 old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
1067 if (new_suffix > old_suffix)
1069 g_free (unthemed_icon->no_svg_filename);
1070 unthemed_icon->no_svg_filename = abs_file;
1076 unthemed_icon->no_svg_filename = abs_file;
1083 unthemed_icon = g_new0 (UnthemedIcon, 1);
1085 if (new_suffix == ICON_SUFFIX_SVG)
1086 unthemed_icon->svg_filename = abs_file;
1088 unthemed_icon->no_svg_filename = abs_file;
1090 g_hash_table_insert (priv->unthemed_icons,
1093 g_hash_table_insert (priv->all_icons,
1101 priv->themes_valid = TRUE;
1103 g_get_current_time(&tv);
1104 priv->last_stat_time = tv.tv_sec;
1108 ensure_valid_themes (GtkIconTheme *icon_theme)
1110 GtkIconThemePrivate *priv = icon_theme->priv;
1113 if (priv->themes_valid)
1115 g_get_current_time (&tv);
1117 if (ABS (tv.tv_sec - priv->last_stat_time) > 5)
1118 gtk_icon_theme_rescan_if_needed (icon_theme);
1121 if (!priv->themes_valid)
1122 load_themes (icon_theme);
1126 * gtk_icon_theme_lookup_icon:
1127 * @icon_theme: a #GtkIconTheme
1128 * @icon_name: the name of the icon to lookup
1129 * @size: desired icon size
1130 * @flags: flags modifying the behavior of the icon lookup
1132 * Looks up a named icon and returns a structure containing
1133 * information such as the filename of the icon. The icon
1134 * can then be rendered into a pixbuf using
1135 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1136 * combines these two steps if all you need is the pixbuf.)
1138 * Return value: a #GtkIconInfo structure containing information
1139 * about the icon, or %NULL if the icon wasn't found. Free with
1140 * gtk_icon_info_free()
1145 gtk_icon_theme_lookup_icon (GtkIconTheme *icon_theme,
1146 const gchar *icon_name,
1148 GtkIconLookupFlags flags)
1150 GtkIconThemePrivate *priv;
1152 GtkIconInfo *icon_info = NULL;
1153 UnthemedIcon *unthemed_icon;
1155 gboolean use_builtin;
1156 gboolean found_default;
1158 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1159 g_return_val_if_fail (icon_name != NULL, NULL);
1160 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1161 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1163 priv = icon_theme->priv;
1165 if (flags & GTK_ICON_LOOKUP_NO_SVG)
1167 else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
1170 allow_svg = priv->pixbuf_supports_svg;
1172 use_builtin = (flags & GTK_ICON_LOOKUP_USE_BUILTIN);
1174 ensure_valid_themes (icon_theme);
1176 found_default = FALSE;
1180 IconTheme *theme = l->data;
1182 if (strcmp (theme->name, DEFAULT_THEME_NAME) == 0)
1183 found_default = TRUE;
1185 icon_info = theme_lookup_icon (theme, icon_name, size, allow_svg, use_builtin);
1194 BuiltinIcon *builtin = find_builtin_icon (icon_name, size, NULL, NULL);
1197 icon_info = icon_info_new_builtin (builtin);
1202 unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_name);
1205 icon_info = icon_info_new ();
1207 /* A SVG icon, when allowed, beats out a XPM icon, but not
1211 unthemed_icon->svg_filename &&
1212 (!unthemed_icon->no_svg_filename ||
1213 suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
1214 icon_info->filename = g_strdup (unthemed_icon->svg_filename);
1215 else if (unthemed_icon->no_svg_filename)
1216 icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);
1218 icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
1219 -1, NULL, NULL, NULL);
1222 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1227 icon_info->desired_size = size;
1230 static gboolean check_for_default_theme = TRUE;
1231 char *default_theme_path;
1232 gboolean found = FALSE;
1235 if (check_for_default_theme)
1237 check_for_default_theme = FALSE;
1239 for (i = 0; !found && i < priv->search_path_len; i++)
1241 default_theme_path = g_build_filename (priv->search_path[i],
1245 found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
1246 g_free (default_theme_path);
1250 g_warning (_("Could not find the icon '%s'. The '%s' theme\n"
1251 "was not found either, perhaps you need to install it.\n"
1252 "You can get a copy from:\n"
1254 icon_name, DEFAULT_THEME_NAME, "http://freedesktop.org/Software/icon-theme/releases");
1264 gtk_icon_theme_error_quark (void)
1266 static GQuark q = 0;
1268 q = g_quark_from_static_string ("gtk-icon-theme-error-quark");
1274 * gtk_icon_theme_load_icon:
1275 * @icon_theme: a #GtkIconTheme
1276 * @icon_name: the name of the icon to lookup
1277 * @size: the desired icon size. The resulting icon may not be
1278 * exactly this size; see gtk_icon_info_load_icon().
1279 * @flags: flags modifying the behavior of the icon lookup
1280 * @error: Location to store error information on failure, or %NULL.
1282 * Looks up an icon in an icon theme, scales it to the given size
1283 * and renders it into a pixbuf. This is a convenience function;
1284 * if more details about the icon are needed, use
1285 * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1287 * Return value: the rendered icon; this may be a newly created icon
1288 * or a new reference to an internal icon, so you must not modify
1289 * the icon. Use g_object_unref() to release your reference to the
1290 * icon. %NULL if the icon isn't found.
1295 gtk_icon_theme_load_icon (GtkIconTheme *icon_theme,
1296 const gchar *icon_name,
1298 GtkIconLookupFlags flags,
1301 GtkIconInfo *icon_info;
1302 GdkPixbuf *pixbuf = NULL;
1304 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1305 g_return_val_if_fail (icon_name != NULL, NULL);
1306 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1307 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1308 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1310 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
1311 flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1314 g_set_error (error, GTK_ICON_THEME_ERROR, GTK_ICON_THEME_NOT_FOUND,
1315 _("Icon '%s' not present in theme"), icon_name);
1319 pixbuf = gtk_icon_info_load_icon (icon_info, error);
1320 gtk_icon_info_free (icon_info);
1326 * gtk_icon_theme_has_icon:
1327 * @icon_theme: a #GtkIconTheme
1328 * @icon_name: the name of an icon
1330 * Checks whether an icon theme includes an icon
1331 * for a particular name.
1333 * Return value: %TRUE if @icon_theme includes an
1334 * icon for @icon_name.
1339 gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
1340 const char *icon_name)
1342 GtkIconThemePrivate *priv;
1345 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1347 priv = icon_theme->priv;
1349 ensure_valid_themes (icon_theme);
1351 for (l = priv->dir_mtimes; l; l = l->next)
1353 IconThemeDirMtime *dir_mtime = l->data;
1354 GtkIconCache *cache = dir_mtime->cache;
1356 if (cache && _gtk_icon_cache_has_icon (cache, icon_name))
1360 for (l = priv->unthemed_icons_caches; l; l = l->next)
1362 GtkIconCache *cache = (GtkIconCache *)l->data;
1364 if (_gtk_icon_cache_has_icon (cache, icon_name))
1368 if (g_hash_table_lookup_extended (priv->all_icons,
1369 icon_name, NULL, NULL))
1372 if (icon_theme_builtin_icons &&
1373 g_hash_table_lookup_extended (icon_theme_builtin_icons,
1374 icon_name, NULL, NULL))
1381 add_size (gpointer key,
1385 gint **res_p = user_data;
1387 **res_p = GPOINTER_TO_INT (key);
1393 * gtk_icon_theme_get_icon_sizes:
1394 * @icon_theme: a #GtkIconTheme
1395 * @icon_name: the name of an icon
1397 * Returns an array of integers describing the sizes at which
1398 * the icon is available without scaling. A size of -1 means
1399 * that the icon is available in a scalable format. The array
1400 * is zero-terminated.
1402 * Return value: An newly allocated array describing the sizes at
1403 * which the icon is available. The array should be freed with g_free()
1404 * when it is no longer needed.
1409 gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
1410 const char *icon_name)
1417 GtkIconThemePrivate *priv;
1419 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1421 priv = icon_theme->priv;
1423 ensure_valid_themes (icon_theme);
1425 sizes = g_hash_table_new (g_direct_hash, g_direct_equal);
1427 for (l = priv->themes; l; l = l->next)
1429 IconTheme *theme = l->data;
1430 for (d = theme->dirs; d; d = d->next)
1432 IconThemeDir *dir = d->data;
1434 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1435 if (suffix != ICON_SUFFIX_NONE)
1437 if (suffix == ICON_SUFFIX_SVG)
1438 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1440 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1445 r = result = g_new0 (gint, g_hash_table_size (sizes) + 1);
1447 g_hash_table_foreach (sizes, add_size, &r);
1448 g_hash_table_destroy (sizes);
1454 add_key_to_hash (gpointer key,
1458 GHashTable *hash = user_data;
1460 g_hash_table_insert (hash, key, NULL);
1464 add_key_to_list (gpointer key,
1468 GList **list = user_data;
1470 *list = g_list_prepend (*list, g_strdup (key));
1474 * gtk_icon_theme_list_icons:
1475 * @icon_theme: a #GtkIconTheme
1476 * @context: a string identifying a particular type of icon,
1477 * or %NULL to list all icons.
1479 * Lists the icons in the current icon theme. Only a subset
1480 * of the icons can be listed by providing a context string.
1481 * The set of values for the context string is system dependent,
1482 * but will typically include such values as 'apps' and
1485 * Return value: a #GList list holding the names of all the
1486 * icons in the theme. You must first free each element
1487 * in the list with g_free(), then free the list itself
1488 * with g_list_free().
1493 gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
1494 const char *context)
1496 GtkIconThemePrivate *priv;
1499 GQuark context_quark;
1501 priv = icon_theme->priv;
1503 ensure_valid_themes (icon_theme);
1507 context_quark = g_quark_try_string (context);
1515 icons = g_hash_table_new (g_str_hash, g_str_equal);
1520 theme_list_icons (l->data, icons, context_quark);
1524 if (context_quark == 0)
1525 g_hash_table_foreach (priv->unthemed_icons,
1531 g_hash_table_foreach (icons,
1535 g_hash_table_destroy (icons);
1541 * gtk_icon_theme_get_example_icon_name:
1542 * @icon_theme: a #GtkIconTheme
1544 * Gets the name of an icon that is representative of the
1545 * current theme (for instance, to use when presenting
1546 * a list of themes to the user.)
1548 * Return value: the name of an example icon or %NULL.
1549 * Free with g_free().
1554 gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
1556 GtkIconThemePrivate *priv;
1560 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1562 priv = icon_theme->priv;
1564 ensure_valid_themes (icon_theme);
1571 return g_strdup (theme->example);
1580 * gtk_icon_theme_rescan_if_needed:
1581 * @icon_theme: a #GtkIconTheme
1583 * Checks to see if the icon theme has changed; if it has, any
1584 * currently cached information is discarded and will be reloaded
1585 * next time @icon_theme is accessed.
1587 * Return value: %TRUE if the icon theme has changed and needed
1593 gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
1595 GtkIconThemePrivate *priv;
1596 IconThemeDirMtime *dir_mtime;
1599 struct stat stat_buf;
1602 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1604 priv = icon_theme->priv;
1606 for (d = priv->dir_mtimes; d != NULL; d = d->next)
1608 dir_mtime = d->data;
1610 stat_res = g_stat (dir_mtime->dir, &stat_buf);
1612 /* dir mtime didn't change */
1613 if (stat_res == 0 &&
1614 S_ISDIR (stat_buf.st_mode) &&
1615 dir_mtime->mtime == stat_buf.st_mtime)
1617 /* didn't exist before, and still doesn't */
1618 if (dir_mtime->mtime == 0 &&
1619 (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
1622 do_theme_change (icon_theme);
1626 g_get_current_time (&tv);
1627 priv->last_stat_time = tv.tv_sec;
1633 theme_destroy (IconTheme *theme)
1635 g_free (theme->display_name);
1636 g_free (theme->comment);
1637 g_free (theme->name);
1638 g_free (theme->example);
1640 g_list_foreach (theme->dirs, (GFunc)theme_dir_destroy, NULL);
1641 g_list_free (theme->dirs);
1647 theme_dir_destroy (IconThemeDir *dir)
1650 _gtk_icon_cache_unref (dir->cache);
1652 g_hash_table_destroy (dir->icons);
1655 g_hash_table_destroy (dir->icon_data);
1657 g_free (dir->subdir);
1662 theme_dir_size_difference (IconThemeDir *dir, int size, gboolean *smaller)
1667 case ICON_THEME_DIR_FIXED:
1668 *smaller = size < dir->size;
1669 return abs (size - dir->size);
1671 case ICON_THEME_DIR_SCALABLE:
1672 *smaller = size < dir->min_size;
1673 if (size < dir->min_size)
1674 return dir->min_size - size;
1675 if (size > dir->max_size)
1676 return size - dir->max_size;
1679 case ICON_THEME_DIR_THRESHOLD:
1680 min = dir->size - dir->threshold;
1681 max = dir->size + dir->threshold;
1682 *smaller = size < min;
1689 case ICON_THEME_DIR_UNTHEMED:
1690 g_assert_not_reached ();
1693 g_assert_not_reached ();
1698 string_from_suffix (IconSuffix suffix)
1702 case ICON_SUFFIX_XPM:
1704 case ICON_SUFFIX_SVG:
1706 case ICON_SUFFIX_PNG:
1709 g_assert_not_reached();
1715 suffix_from_name (const char *name)
1719 if (g_str_has_suffix (name, ".png"))
1720 retval = ICON_SUFFIX_PNG;
1721 else if (g_str_has_suffix (name, ".svg"))
1722 retval = ICON_SUFFIX_SVG;
1723 else if (g_str_has_suffix (name, ".xpm"))
1724 retval = ICON_SUFFIX_XPM;
1726 retval = ICON_SUFFIX_NONE;
1732 best_suffix (IconSuffix suffix,
1735 if ((suffix & ICON_SUFFIX_PNG) != 0)
1736 return ICON_SUFFIX_PNG;
1737 else if (allow_svg && ((suffix & ICON_SUFFIX_SVG) != 0))
1738 return ICON_SUFFIX_SVG;
1739 else if ((suffix & ICON_SUFFIX_XPM) != 0)
1740 return ICON_SUFFIX_XPM;
1742 return ICON_SUFFIX_NONE;
1747 theme_dir_get_icon_suffix (IconThemeDir *dir,
1748 const gchar *icon_name,
1749 gboolean *has_icon_file)
1755 suffix = (IconSuffix)_gtk_icon_cache_get_icon_flags (dir->cache,
1760 *has_icon_file = suffix & HAS_ICON_FILE;
1762 suffix = suffix & ~HAS_ICON_FILE;
1765 suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
1770 static GtkIconInfo *
1771 theme_lookup_icon (IconTheme *theme,
1772 const char *icon_name,
1775 gboolean use_builtin)
1778 IconThemeDir *dir, *min_dir;
1780 int min_difference, difference;
1781 BuiltinIcon *closest_builtin = NULL;
1782 gboolean smaller, has_larger;
1785 min_difference = G_MAXINT;
1789 /* Builtin icons are logically part of the default theme and
1790 * are searched before other subdirectories of the default theme.
1792 if (strcmp (theme->name, DEFAULT_THEME_NAME) == 0 && use_builtin)
1794 closest_builtin = find_builtin_icon (icon_name, size,
1798 if (min_difference == 0)
1799 return icon_info_new_builtin (closest_builtin);
1807 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1808 if (best_suffix (suffix, allow_svg) != ICON_SUFFIX_NONE)
1810 difference = theme_dir_size_difference (dir, size, &smaller);
1812 if (difference == 0)
1820 if (difference < min_difference || smaller)
1822 min_difference = difference;
1824 closest_builtin = NULL;
1825 has_larger = smaller;
1830 if (difference < min_difference && smaller)
1832 min_difference = difference;
1834 closest_builtin = NULL;
1843 if (closest_builtin)
1844 return icon_info_new_builtin (closest_builtin);
1848 GtkIconInfo *icon_info = icon_info_new ();
1849 gboolean has_icon_file;
1851 suffix = theme_dir_get_icon_suffix (min_dir, icon_name, &has_icon_file);
1852 suffix = best_suffix (suffix, allow_svg);
1853 g_assert (suffix != ICON_SUFFIX_NONE);
1855 file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
1856 icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
1859 icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
1860 -1, NULL, NULL, NULL);
1863 if (min_dir->icon_data != NULL)
1864 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
1865 else if (min_dir->cache != NULL)
1866 icon_info->data = _gtk_icon_cache_get_icon_data (min_dir->cache, icon_name, min_dir->subdir);
1868 if (icon_info->data == NULL &&
1869 min_dir->cache && has_icon_file)
1871 gchar *icon_file_name, *icon_file_path;
1873 icon_file_name = g_strconcat (icon_name, ".icon", NULL);
1874 icon_file_path = g_build_filename (min_dir->dir, icon_file_name, NULL);
1876 if (g_file_test (icon_file_path, G_FILE_TEST_IS_REGULAR))
1878 if (min_dir->icon_data == NULL)
1879 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
1880 g_free, (GDestroyNotify)icon_data_free);
1881 load_icon_data (min_dir, icon_file_path, icon_file_name);
1883 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
1885 g_free (icon_file_name);
1886 g_free (icon_file_path);
1891 icon_info->cache_pixbuf = _gtk_icon_cache_get_icon (min_dir->cache, icon_name,
1895 icon_info->dir_type = min_dir->type;
1896 icon_info->dir_size = min_dir->size;
1897 icon_info->threshold = min_dir->threshold;
1906 theme_list_icons (IconTheme *theme, GHashTable *icons,
1909 GList *l = theme->dirs;
1916 if (context == dir->context ||
1921 _gtk_icon_cache_add_icons (dir->cache,
1928 g_hash_table_foreach (dir->icons,
1938 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
1940 GKeyFile *icon_file;
1949 GError *error = NULL;
1953 icon_file = g_key_file_new ();
1954 g_key_file_set_list_separator (icon_file, ',');
1955 g_key_file_load_from_file (icon_file, path, 0, &error);
1958 g_error_free (error);
1963 base_name = g_strdup (name);
1964 dot = strrchr (base_name, '.');
1967 data = g_new0 (GtkIconData, 1);
1968 g_hash_table_replace (dir->icon_data, base_name, data);
1970 ivalues = g_key_file_get_integer_list (icon_file,
1971 "Icon Data", "EmbeddedTextRectangle",
1977 data->has_embedded_rect = TRUE;
1978 data->x0 = ivalues[0];
1979 data->y0 = ivalues[1];
1980 data->x1 = ivalues[2];
1981 data->y1 = ivalues[3];
1987 str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
1990 split = g_strsplit (str, "|", -1);
1992 data->n_attach_points = g_strv_length (split);
1993 data->attach_points = g_malloc (sizeof (GdkPoint) * data->n_attach_points);
1996 while (split[i] != NULL && i < data->n_attach_points)
1998 split_point = strchr (split[i], ',');
2003 data->attach_points[i].x = atoi (split[i]);
2004 data->attach_points[i].y = atoi (split_point);
2013 data->display_name = g_key_file_get_locale_string (icon_file,
2014 "Icon Data", "DisplayName",
2016 g_key_file_free (icon_file);
2021 scan_directory (GtkIconThemePrivate *icon_theme,
2022 IconThemeDir *dir, char *full_dir)
2026 char *base_name, *dot;
2028 IconSuffix suffix, hash_suffix;
2030 GTK_NOTE (ICONTHEME,
2031 g_print ("scanning directory %s\n", full_dir));
2032 dir->icons = g_hash_table_new_full (g_str_hash, g_str_equal,
2035 gdir = g_dir_open (full_dir, 0, NULL);
2040 while ((name = g_dir_read_name (gdir)))
2042 if (g_str_has_suffix (name, ".icon"))
2044 if (dir->icon_data == NULL)
2045 dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2046 g_free, (GDestroyNotify)icon_data_free);
2048 path = g_build_filename (full_dir, name, NULL);
2049 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
2050 load_icon_data (dir, path, name);
2057 suffix = suffix_from_name (name);
2058 if (suffix == ICON_SUFFIX_NONE)
2061 base_name = g_strdup (name);
2062 dot = strrchr (base_name, '.');
2065 hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
2066 g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
2067 g_hash_table_insert (icon_theme->all_icons, base_name, NULL);
2074 theme_subdir_load (GtkIconTheme *icon_theme,
2076 GKeyFile *theme_file,
2082 IconThemeDirType type;
2083 char *context_string;
2090 GError *error = NULL;
2091 IconThemeDirMtime *dir_mtime;
2093 size = g_key_file_get_integer (theme_file, subdir, "Size", &error);
2096 g_error_free (error);
2097 g_warning ("Theme directory %s of theme %s has no size field\n",
2098 subdir, theme->name);
2102 type = ICON_THEME_DIR_THRESHOLD;
2103 type_string = g_key_file_get_string (theme_file, subdir, "Type", NULL);
2106 if (strcmp (type_string, "Fixed") == 0)
2107 type = ICON_THEME_DIR_FIXED;
2108 else if (strcmp (type_string, "Scalable") == 0)
2109 type = ICON_THEME_DIR_SCALABLE;
2110 else if (strcmp (type_string, "Threshold") == 0)
2111 type = ICON_THEME_DIR_THRESHOLD;
2113 g_free (type_string);
2117 context_string = g_key_file_get_string (theme_file, subdir, "Context", NULL);
2120 context = g_quark_from_string (context_string);
2121 g_free (context_string);
2124 max_size = g_key_file_get_integer (theme_file, subdir, "MaxSize", &error);
2129 g_error_free (error);
2133 min_size = g_key_file_get_integer (theme_file, subdir, "MinSize", &error);
2138 g_error_free (error);
2142 threshold = g_key_file_get_integer (theme_file, subdir, "Threshold", &error);
2147 g_error_free (error);
2151 for (d = icon_theme->priv->dir_mtimes; d; d = d->next)
2153 dir_mtime = (IconThemeDirMtime *)d->data;
2155 if (dir_mtime->mtime == 0)
2156 continue; /* directory doesn't exist */
2158 full_dir = g_build_filename (dir_mtime->dir, subdir, NULL);
2160 /* First, see if we have a cache for the directory */
2161 if (dir_mtime->cache != NULL || g_file_test (full_dir, G_FILE_TEST_IS_DIR))
2163 if (dir_mtime->cache == NULL)
2165 /* This will return NULL if the cache doesn't exist or is outdated */
2166 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir_mtime->dir);
2169 dir = g_new (IconThemeDir, 1);
2171 dir->context = context;
2173 dir->min_size = min_size;
2174 dir->max_size = max_size;
2175 dir->threshold = threshold;
2176 dir->dir = full_dir;
2177 dir->icon_data = NULL;
2178 dir->subdir = g_strdup (subdir);
2179 if (dir_mtime->cache != NULL)
2180 dir->cache = _gtk_icon_cache_ref (dir_mtime->cache);
2184 scan_directory (icon_theme->priv, dir, full_dir);
2187 theme->dirs = g_list_prepend (theme->dirs, dir);
2195 icon_data_free (GtkIconData *icon_data)
2197 g_free (icon_data->attach_points);
2198 g_free (icon_data->display_name);
2206 gtk_icon_info_get_type (void)
2208 static GType our_type = 0;
2211 our_type = g_boxed_type_register_static ("GtkIconInfo",
2212 (GBoxedCopyFunc) gtk_icon_info_copy,
2213 (GBoxedFreeFunc) gtk_icon_info_free);
2218 static GtkIconInfo *
2219 icon_info_new (void)
2221 GtkIconInfo *icon_info = g_new0 (GtkIconInfo, 1);
2223 icon_info->scale = -1.;
2228 static GtkIconInfo *
2229 icon_info_new_builtin (BuiltinIcon *icon)
2231 GtkIconInfo *icon_info = icon_info_new ();
2233 icon_info->builtin_pixbuf = g_object_ref (icon->pixbuf);
2234 icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
2235 icon_info->dir_size = icon->size;
2236 icon_info->threshold = 2;
2242 * gtk_icon_info_copy:
2243 * @icon_info: a #GtkIconInfo
2245 * Make a copy of a #GtkIconInfo.
2247 * Return value: the new GtkIconInfo
2252 gtk_icon_info_copy (GtkIconInfo *icon_info)
2256 g_return_val_if_fail (icon_info != NULL, NULL);
2258 copy = g_memdup (icon_info, sizeof (GtkIconInfo));
2259 if (copy->builtin_pixbuf)
2260 g_object_ref (copy->builtin_pixbuf);
2262 g_object_ref (copy->pixbuf);
2263 if (copy->load_error)
2264 copy->load_error = g_error_copy (copy->load_error);
2266 copy->filename = g_strdup (copy->filename);
2268 if (copy->cp_filename)
2269 copy->cp_filename = g_strdup (copy->cp_filename);
2276 * gtk_icon_info_free:
2277 * @icon_info: a #GtkIconInfo
2279 * Free a #GtkIconInfo and associated information
2284 gtk_icon_info_free (GtkIconInfo *icon_info)
2286 g_return_if_fail (icon_info != NULL);
2288 if (icon_info->filename)
2289 g_free (icon_info->filename);
2291 if (icon_info->cp_filename)
2292 g_free (icon_info->cp_filename);
2294 if (icon_info->builtin_pixbuf)
2295 g_object_unref (icon_info->builtin_pixbuf);
2296 if (icon_info->pixbuf)
2297 g_object_unref (icon_info->pixbuf);
2298 if (icon_info->cache_pixbuf)
2299 g_object_unref (icon_info->cache_pixbuf);
2305 * gtk_icon_info_get_base_size:
2306 * @icon_info: a #GtkIconInfo
2308 * Gets the base size for the icon. The base size
2309 * is a size for the icon that was specified by
2310 * the icon theme creator. This may be different
2311 * than the actual size of image; an example of
2312 * this is small emblem icons that can be attached
2313 * to a larger icon. These icons will be given
2314 * the same base size as the larger icons to which
2315 * they are attached.
2317 * Return value: the base size, or 0, if no base
2318 * size is known for the icon.
2323 gtk_icon_info_get_base_size (GtkIconInfo *icon_info)
2325 g_return_val_if_fail (icon_info != NULL, 0);
2327 return icon_info->dir_size;
2331 * gtk_icon_info_get_filename:
2332 * @icon_info: a #GtkIconInfo
2334 * Gets the filename for the icon. If the
2335 * %GTK_ICON_LOOKUP_USE_BUILTIN flag was passed
2336 * to gtk_icon_theme_lookup_icon(), there may be
2337 * no filename if a builtin icon is returned; in this
2338 * case, you should use gtk_icon_info_get_builtin_pixbuf().
2340 * Return value: the filename for the icon, or %NULL
2341 * if gtk_icon_info_get_builtin_pixbuf() should
2342 * be used instead. The return value is owned by
2343 * GTK+ and should not be modified or freed.
2347 G_CONST_RETURN gchar *
2348 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2350 g_return_val_if_fail (icon_info != NULL, NULL);
2352 return icon_info->filename;
2356 * gtk_icon_info_get_builtin_pixbuf:
2357 * @icon_info: a #GtkIconInfo structure
2359 * Gets the built-in image for this icon, if any. To allow
2360 * GTK+ to use built in icon images, you must pass the
2361 * %GTK_ICON_LOOKUP_USE_BUILTIN to
2362 * gtk_icon_theme_lookup_icon().
2364 * Return value: the built-in image pixbuf, or %NULL. No
2365 * extra reference is added to the returned pixbuf, so if
2366 * you want to keep it around, you must use g_object_ref().
2367 * The returned image must not be modified.
2372 gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
2374 g_return_val_if_fail (icon_info != NULL, NULL);
2376 return icon_info->builtin_pixbuf;
2380 load_svg_at_size (const gchar *filename,
2384 GdkPixbuf *pixbuf = NULL;
2385 GdkPixbufLoader *loader = NULL;
2386 gchar *contents = NULL;
2389 if (!g_file_get_contents (filename,
2390 &contents, &length, error))
2393 loader = gdk_pixbuf_loader_new ();
2394 gdk_pixbuf_loader_set_size (loader, size, size);
2396 if (!gdk_pixbuf_loader_write (loader, contents, length, error))
2398 gdk_pixbuf_loader_close (loader, NULL);
2402 if (!gdk_pixbuf_loader_close (loader, error))
2405 pixbuf = g_object_ref (gdk_pixbuf_loader_get_pixbuf (loader));
2409 g_object_unref (loader);
2416 /* This function contains the complicatd logic for deciding
2417 * on the size at which to load the icon and loading it at
2421 icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
2422 gboolean scale_only)
2424 int image_width, image_height;
2425 GdkPixbuf *source_pixbuf;
2427 /* First check if we already succeeded have the necessary
2428 * information (or failed earlier)
2430 if (scale_only && icon_info->scale >= 0)
2433 if (icon_info->pixbuf)
2436 if (icon_info->load_error)
2439 /* SVG icons are a special case - we just immediately scale them
2440 * to the desired size
2442 if (icon_info->filename && g_str_has_suffix (icon_info->filename, ".svg"))
2444 icon_info->scale = icon_info->desired_size / 1000.;
2449 icon_info->pixbuf = load_svg_at_size (icon_info->filename,
2450 icon_info->desired_size,
2451 &icon_info->load_error);
2453 return icon_info->pixbuf != NULL;
2456 /* In many cases, the scale can be determined without actual access
2457 * to the icon file. This is generally true when we have a size
2458 * for the directory where the icon is; the image size doesn't
2459 * matter in that case.
2461 if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
2462 icon_info->scale = 1.0;
2463 else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
2465 if (icon_info->desired_size >= icon_info->dir_size - icon_info->threshold &&
2466 icon_info->desired_size <= icon_info->dir_size + icon_info->threshold)
2467 icon_info->scale = 1.0;
2468 else if (icon_info->dir_size > 0)
2469 icon_info->scale =(gdouble) icon_info->desired_size / icon_info->dir_size;
2471 else if (icon_info->dir_type == ICON_THEME_DIR_SCALABLE)
2473 if (icon_info->dir_size > 0)
2474 icon_info->scale = (gdouble) icon_info->desired_size / icon_info->dir_size;
2477 if (icon_info->scale >= 0. && scale_only)
2480 /* At this point, we need to actually get the icon; either from the
2481 * builting image or by loading the file
2483 if (icon_info->builtin_pixbuf)
2484 source_pixbuf = g_object_ref (icon_info->builtin_pixbuf);
2485 else if (icon_info->cache_pixbuf)
2486 source_pixbuf = g_object_ref (icon_info->cache_pixbuf);
2490 source_pixbuf = gdk_pixbuf_new_from_file (icon_info->filename,
2491 &icon_info->load_error);
2496 /* Do scale calculations that depend on the image size
2498 image_width = gdk_pixbuf_get_width (source_pixbuf);
2499 image_height = gdk_pixbuf_get_height (source_pixbuf);
2501 if (icon_info->scale < 0.0)
2503 gint image_size = MAX (image_width, image_height);
2505 icon_info->scale = icon_info->desired_size / (gdouble)image_size;
2507 icon_info->scale = 1.0;
2509 if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED)
2510 icon_info->scale = MIN (icon_info->scale, 1.0);
2513 /* We don't short-circuit out here for scale_only, since, now
2514 * we've loaded the icon, we might as well go ahead and finish
2515 * the job. This is a bit of a waste when we scale here
2516 * and never get the final pixbuf; at the cost of a bit of
2517 * extra complexity, we could keep the source pixbuf around
2518 * but not actually scale it until neede.
2521 if (icon_info->scale == 1.0)
2522 icon_info->pixbuf = source_pixbuf;
2525 icon_info->pixbuf = gdk_pixbuf_scale_simple (source_pixbuf,
2526 0.5 + image_width * icon_info->scale,
2527 0.5 + image_height * icon_info->scale,
2528 GDK_INTERP_BILINEAR);
2529 g_object_unref (source_pixbuf);
2536 * gtk_icon_info_load_icon:
2537 * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
2538 * @error: location to store error information on failure, or %NULL.
2540 * Renders an icon previously looked up in an icon theme using
2541 * gtk_icon_theme_lookup_icon(); the size will be based on the size
2542 * passed to gtk_icon_theme_lookup_icon(). Note that the resulting
2543 * pixbuf may not be exactly this size; an icon theme may have icons
2544 * that differ slightly from their nominal sizes, and in addition GTK+
2545 * will avoid scaling icons that it considers sufficiently close to the
2546 * requested size or for which the source image would have to be scaled
2547 * up too far. (This maintains sharpness.)
2549 * Return value: the rendered icon; this may be a newly created icon
2550 * or a new reference to an internal icon, so you must not modify
2551 * the icon. Use g_object_unref() to release your reference to the
2557 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
2560 g_return_val_if_fail (icon_info != NULL, NULL);
2562 g_return_val_if_fail (icon_info != NULL, NULL);
2563 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2565 icon_info_ensure_scale_and_pixbuf (icon_info, FALSE);
2567 if (icon_info->load_error)
2569 g_propagate_error (error, icon_info->load_error);
2573 return g_object_ref (icon_info->pixbuf);
2577 * gtk_icon_info_set_raw_coordinates:
2578 * @icon_info: a #GtkIconInfo
2579 * @raw_coordinates: whether the coordinates of embedded rectangles
2580 * and attached points should be returned in their original
2583 * Sets whether the coordinates returned by gtk_icon_info_get_embedded_rect()
2584 * and gtk_icon_info_get_attach_points() should be returned in their
2585 * original form as specified in the icon theme, instead of scaled
2586 * appropriately for the pixbuf returned by gtk_icon_info_load_icon().
2588 * Raw coordinates are somewhat strange; they are specified to be with
2589 * respect to the unscaled pixmap for PNG and XPM icons, but for SVG
2590 * icons, they are in a 1000x1000 coordinate space that is scaled
2591 * to the final size of the icon. You can determine if the icon is an SVG
2592 * icon by using gtk_icon_info_get_filename(), and seeing if it is non-%NULL
2593 * and ends in '.svg'.
2595 * This function is provided primarily to allow compatibility wrappers
2596 * for older API's, and is not expected to be useful for applications.
2601 gtk_icon_info_set_raw_coordinates (GtkIconInfo *icon_info,
2602 gboolean raw_coordinates)
2604 g_return_if_fail (icon_info != NULL);
2606 icon_info->raw_coordinates = raw_coordinates != FALSE;
2609 /* Scale coordinates from the icon data prior to returning
2613 icon_info_scale_point (GtkIconInfo *icon_info,
2619 if (icon_info->raw_coordinates)
2626 if (!icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2629 *x_out = 0.5 + x * icon_info->scale;
2630 *y_out = 0.5 + y * icon_info->scale;
2637 * gtk_icon_info_get_embedded_rect:
2638 * @icon_info: a #GtkIconInfo
2639 * @rectangle: #GdkRectangle in which to store embedded
2640 * rectangle coordinates; coordinates are only stored
2641 * when this function returns %TRUE.
2643 * Gets the coordinates of a rectangle within the icon
2644 * that can be used for display of information such
2645 * as a preview of the contents of a text file.
2646 * See gtk_icon_info_set_raw_coordinates() for further
2647 * information about the coordinate system.
2649 * Return value: %TRUE if the icon has an embedded rectangle
2654 gtk_icon_info_get_embedded_rect (GtkIconInfo *icon_info,
2655 GdkRectangle *rectangle)
2657 g_return_val_if_fail (icon_info != NULL, FALSE);
2659 if (icon_info->data && icon_info->data->has_embedded_rect &&
2660 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2662 gint scaled_x0, scaled_y0;
2663 gint scaled_x1, scaled_y1;
2667 icon_info_scale_point (icon_info,
2668 icon_info->data->x0, icon_info->data->y0,
2669 &scaled_x0, &scaled_y0);
2670 icon_info_scale_point (icon_info,
2671 icon_info->data->x1, icon_info->data->y1,
2672 &scaled_x1, &scaled_y1);
2674 rectangle->x = scaled_x0;
2675 rectangle->y = scaled_y0;
2676 rectangle->width = scaled_x1 - rectangle->x;
2677 rectangle->height = scaled_y1 - rectangle->y;
2687 * gtk_icon_info_get_attach_points:
2688 * @icon_info: a #GtkIconInfo
2689 * @points: location to store pointer to an array of points, or %NULL
2690 * free the array of points with g_free().
2691 * @n_points: location to store the number of points in @points, or %NULL
2693 * Fetches the set of attach points for an icon. An attach point
2694 * is a location in the icon that can be used as anchor points for attaching
2695 * emblems or overlays to the icon.
2697 * Return value: %TRUE if there are any attach points for the icon.
2702 gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
2706 g_return_val_if_fail (icon_info != NULL, FALSE);
2708 if (icon_info->data && icon_info->data->n_attach_points &&
2709 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2715 *points = g_new (GdkPoint, icon_info->data->n_attach_points);
2716 for (i = 0; i < icon_info->data->n_attach_points; i++)
2717 icon_info_scale_point (icon_info,
2718 icon_info->data->attach_points[i].x,
2719 icon_info->data->attach_points[i].y,
2725 *n_points = icon_info->data->n_attach_points;
2741 * gtk_icon_info_get_display_name:
2742 * @icon_info: a #GtkIconInfo
2744 * Gets the display name for an icon. A display name is a
2745 * string to be used in place of the icon name in a user
2746 * visible context like a list of icons.
2748 * Return value: the display name for the icon or %NULL, if
2749 * the icon doesn't have a specified display name. This value
2750 * is owned @icon_info and must not be modified or free.
2754 G_CONST_RETURN gchar *
2755 gtk_icon_info_get_display_name (GtkIconInfo *icon_info)
2757 g_return_val_if_fail (icon_info != NULL, NULL);
2759 if (icon_info->data)
2760 return icon_info->data->display_name;
2771 * gtk_icon_theme_add_builtin_icon:
2772 * @icon_name: the name of the icon to register
2773 * @size: the size at which to register the icon (different
2774 * images can be registered for the same icon name
2775 * at different sizes.)
2776 * @pixbuf: #GdkPixbuf that contains the image to use
2779 * Registers a built-in icon for icon theme lookups. The idea
2780 * of built-in icons is to allow an application or library
2781 * that uses themed icons to function requiring files to
2782 * be present in the file system. For instance, the default
2783 * images for all of GTK+'s stock icons are registered
2786 * In general, if you use gtk_icon_theme_add_builtin_icon()
2787 * you should also install the icon in the icon theme, so
2788 * that the icon is generally available.
2790 * This function will generally be used with pixbufs loaded
2791 * via gdk_pixbuf_new_from_inline().
2796 gtk_icon_theme_add_builtin_icon (const gchar *icon_name,
2800 BuiltinIcon *default_icon;
2804 g_return_if_fail (icon_name != NULL);
2805 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2807 if (!icon_theme_builtin_icons)
2808 icon_theme_builtin_icons = g_hash_table_new (g_str_hash, g_str_equal);
2810 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
2812 key = g_strdup (icon_name);
2814 key = (gpointer)icon_name; /* Won't get stored */
2816 default_icon = g_new (BuiltinIcon, 1);
2817 default_icon->size = size;
2818 default_icon->pixbuf = g_object_ref (pixbuf);
2819 icons = g_slist_prepend (icons, default_icon);
2821 /* Replaces value, leaves key untouched
2823 g_hash_table_insert (icon_theme_builtin_icons, key, icons);
2826 /* Look up a builtin icon; the min_difference_p and
2827 * has_larger_p out parameters allow us to combine
2828 * this lookup with searching through the actual directories
2829 * of the "hicolor" icon theme. See theme_lookup_icon()
2830 * for how they are used.
2832 static BuiltinIcon *
2833 find_builtin_icon (const gchar *icon_name,
2835 gint *min_difference_p,
2836 gboolean *has_larger_p)
2838 GSList *icons = NULL;
2839 gint min_difference = G_MAXINT;
2840 gboolean has_larger = FALSE;
2841 BuiltinIcon *min_icon = NULL;
2843 _gtk_icon_factory_ensure_default_icons ();
2845 if (!icon_theme_builtin_icons)
2848 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
2852 BuiltinIcon *default_icon = icons->data;
2853 int min, max, difference;
2856 min = default_icon->size - 2;
2857 max = default_icon->size + 2;
2858 smaller = size < min;
2860 difference = min - size;
2861 else if (size > max)
2862 difference = size - max;
2866 if (difference == 0)
2868 min_icon = default_icon;
2874 if (difference < min_difference || smaller)
2876 min_difference = difference;
2877 min_icon = default_icon;
2878 has_larger = smaller;
2883 if (difference < min_difference && smaller)
2885 min_difference = difference;
2886 min_icon = default_icon;
2890 icons = icons->next;
2893 if (min_difference_p)
2894 *min_difference_p = min_difference;
2896 *has_larger_p = has_larger;
2903 /* DLL ABI stability backward compatibility versions */
2905 #undef gtk_icon_theme_set_search_path
2908 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
2909 const gchar *path[],
2912 const gchar **utf8_path;
2915 utf8_path = g_new (const gchar *, n_elements);
2917 for (i = 0; i < n_elements; i++)
2918 utf8_path[i] = g_locale_to_utf8 (path[i], -1, NULL, NULL, NULL);
2920 gtk_icon_theme_set_search_path_utf8 (icon_theme, utf8_path, n_elements);
2922 for (i = 0; i < n_elements; i++)
2923 g_free ((gchar *) utf8_path[i]);
2928 #undef gtk_icon_theme_get_search_path
2931 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
2937 gtk_icon_theme_get_search_path_utf8 (icon_theme, path, &n);
2944 for (i = 0; i < n; i++)
2946 gchar *tem = (*path)[i];
2948 (*path)[i] = g_locale_from_utf8 ((*path)[i], -1, NULL, NULL, NULL);
2954 #undef gtk_icon_theme_append_search_path
2957 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
2960 gchar *utf8_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
2962 gtk_icon_theme_append_search_path_utf8 (icon_theme, utf8_path);
2967 #undef gtk_icon_theme_prepend_search_path
2970 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
2973 gchar *utf8_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
2975 gtk_icon_theme_prepend_search_path_utf8 (icon_theme, utf8_path);
2980 #undef gtk_icon_info_get_filename
2982 G_CONST_RETURN gchar *
2983 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2985 g_return_val_if_fail (icon_info != NULL, NULL);
2987 return icon_info->cp_filename;
2992 #define __GTK_ICON_THEME_C__
2993 #include "gtkaliasdef.c"