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"
41 #include "gtkbuiltincache.h"
44 #include "gtksettings.h"
45 #include "gtkprivate.h"
48 #define DEFAULT_THEME_NAME "hicolor"
53 ICON_THEME_DIR_SCALABLE,
54 ICON_THEME_DIR_THRESHOLD,
55 ICON_THEME_DIR_UNTHEMED
58 /* In reverse search order: */
62 ICON_SUFFIX_XPM = 1 << 0,
63 ICON_SUFFIX_SVG = 1 << 1,
64 ICON_SUFFIX_PNG = 1 << 2,
65 HAS_ICON_FILE = 1 << 3
69 struct _GtkIconThemePrivate
71 guint custom_theme : 1;
72 guint is_screen_singleton : 1;
73 guint pixbuf_supports_svg : 1;
74 guint themes_valid : 1;
75 guint check_reload : 1;
82 /* A list of all the themes needed to look up icons.
83 * In search order, without duplicates
86 GHashTable *unthemed_icons;
88 /* Note: The keys of this hashtable are owned by the
89 * themedir and unthemed hashtables.
91 GHashTable *all_icons;
93 /* GdkScreen for the icon theme (may be NULL)
97 /* time when we last stat:ed for theme changes */
101 gulong reset_styles_idle;
106 /* Information about the source
110 /* System codepage version of filename, for DLL ABI backward
111 * compatibility functions.
115 /* Cache pixbuf (if there is any) */
116 GdkPixbuf *cache_pixbuf;
120 /* Information about the directory where
121 * the source was found
123 IconThemeDirType dir_type;
127 /* Parameters influencing the scaled icon
130 gboolean raw_coordinates;
132 /* Cached information if we go ahead and try to load
147 /* In search order */
153 IconThemeDirType type;
167 GHashTable *icon_data;
173 char *no_svg_filename;
185 time_t mtime; /* 0 == not existing or not a dir */
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 guint signal_changed = 0;
232 static GHashTable *icon_theme_builtin_icons;
234 /* also used in gtkiconfactory.c */
235 GtkIconCache *_builtin_cache = NULL;
236 static GList *builtin_dirs = NULL;
238 G_DEFINE_TYPE (GtkIconTheme, gtk_icon_theme, G_TYPE_OBJECT)
241 * gtk_icon_theme_new:
243 * Creates a new icon theme object. Icon theme objects are used
244 * to lookup up an icon by name in a particular icon theme.
245 * Usually, you'll want to use gtk_icon_theme_get_default()
246 * or gtk_icon_theme_get_for_screen() rather than creating
247 * a new icon theme object for scratch.
249 * Return value: the newly created #GtkIconTheme object.
254 gtk_icon_theme_new (void)
256 return g_object_new (GTK_TYPE_ICON_THEME, NULL);
260 * gtk_icon_theme_get_default:
262 * Gets the icon theme for the default screen. See
263 * gtk_icon_theme_get_for_screen().
265 * Return value: A unique #GtkIconTheme associated with
266 * the default screen. This icon theme is associated with
267 * the screen and can be used as long as the screen
268 * is open. Do not ref or unref it.
273 gtk_icon_theme_get_default (void)
275 return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
279 * gtk_icon_theme_get_for_screen:
280 * @screen: a #GdkScreen
282 * Gets the icon theme object associated with @screen; if this
283 * function has not previously been called for the given
284 * screen, a new icon theme object will be created and
285 * associated with the screen. Icon theme objects are
286 * fairly expensive to create, so using this function
287 * is usually a better choice than calling than gtk_icon_theme_new()
288 * and setting the screen yourself; by using this function
289 * a single icon theme object will be shared between users.
291 * Return value: A unique #GtkIconTheme associated with
292 * the given screen. This icon theme is associated with
293 * the screen and can be used as long as the screen
294 * is open. Do not ref or unref it.
299 gtk_icon_theme_get_for_screen (GdkScreen *screen)
301 GtkIconTheme *icon_theme;
303 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
304 g_return_val_if_fail (!screen->closed, NULL);
306 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
309 GtkIconThemePrivate *priv;
311 icon_theme = gtk_icon_theme_new ();
312 gtk_icon_theme_set_screen (icon_theme, screen);
314 priv = icon_theme->priv;
315 priv->is_screen_singleton = TRUE;
317 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), icon_theme);
324 gtk_icon_theme_class_init (GtkIconThemeClass *klass)
326 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
328 gobject_class->finalize = gtk_icon_theme_finalize;
331 * GtkIconTheme::changed
332 * @icon_theme: the icon theme
334 * Emitted when the current icon theme is switched or GTK+ detects
335 * that a change has occurred in the contents of the current
338 signal_changed = g_signal_new (I_("changed"),
339 G_TYPE_FROM_CLASS (klass),
341 G_STRUCT_OFFSET (GtkIconThemeClass, changed),
343 g_cclosure_marshal_VOID__VOID,
346 g_type_class_add_private (klass, sizeof (GtkIconThemePrivate));
350 /* Callback when the display that the icon theme is attached to
351 * is closed; unset the screen, and if it's the unique theme
352 * for the screen, drop the reference
355 display_closed (GdkDisplay *display,
357 GtkIconTheme *icon_theme)
359 GtkIconThemePrivate *priv = icon_theme->priv;
360 GdkScreen *screen = priv->screen;
361 gboolean was_screen_singleton = priv->is_screen_singleton;
363 if (was_screen_singleton)
365 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), NULL);
366 priv->is_screen_singleton = FALSE;
369 gtk_icon_theme_set_screen (icon_theme, NULL);
371 if (was_screen_singleton)
373 g_object_unref (icon_theme);
378 update_current_theme (GtkIconTheme *icon_theme)
380 GtkIconThemePrivate *priv = icon_theme->priv;
382 if (!priv->custom_theme)
385 gchar *fallback_theme = NULL;
386 gboolean changed = FALSE;
390 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
391 g_object_get (settings,
392 "gtk-icon-theme-name", &theme,
393 "gtk-fallback-icon-theme", &fallback_theme, NULL);
397 theme = g_strdup (DEFAULT_THEME_NAME);
399 if (strcmp (priv->current_theme, theme) != 0)
401 g_free (priv->current_theme);
402 priv->current_theme = theme;
409 if ((priv->fallback_theme && !fallback_theme) ||
410 (!priv->fallback_theme && fallback_theme) ||
411 (priv->fallback_theme && fallback_theme &&
412 strcmp (priv->fallback_theme, fallback_theme) != 0))
414 g_free (priv->fallback_theme);
415 priv->fallback_theme = fallback_theme;
420 g_free (fallback_theme);
423 do_theme_change (icon_theme);
427 /* Callback when the icon theme GtkSetting changes
430 theme_changed (GtkSettings *settings,
432 GtkIconTheme *icon_theme)
434 update_current_theme (icon_theme);
438 unset_screen (GtkIconTheme *icon_theme)
440 GtkIconThemePrivate *priv = icon_theme->priv;
441 GtkSettings *settings;
446 settings = gtk_settings_get_for_screen (priv->screen);
447 display = gdk_screen_get_display (priv->screen);
449 g_signal_handlers_disconnect_by_func (display,
450 (gpointer) display_closed,
452 g_signal_handlers_disconnect_by_func (settings,
453 (gpointer) theme_changed,
461 * gtk_icon_theme_set_screen:
462 * @icon_theme: a #GtkIconTheme
463 * @screen: a #GdkScreen
465 * Sets the screen for an icon theme; the screen is used
466 * to track the user's currently configured icon theme,
467 * which might be different for different screens.
472 gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
475 GtkIconThemePrivate *priv;
476 GtkSettings *settings;
479 g_return_if_fail (GTK_ICON_THEME (icon_theme));
480 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
482 priv = icon_theme->priv;
484 unset_screen (icon_theme);
488 display = gdk_screen_get_display (screen);
489 settings = gtk_settings_get_for_screen (screen);
491 priv->screen = screen;
493 g_signal_connect (display, "closed",
494 G_CALLBACK (display_closed), icon_theme);
495 g_signal_connect (settings, "notify::gtk-icon-theme-name",
496 G_CALLBACK (theme_changed), icon_theme);
497 g_signal_connect (settings, "notify::gtk-fallback-icon-theme-name",
498 G_CALLBACK (theme_changed), icon_theme);
501 update_current_theme (icon_theme);
504 /* Checks whether a loader for SVG files has been registered
508 pixbuf_supports_svg (void)
512 static gint found_svg = -1;
517 formats = gdk_pixbuf_get_formats ();
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);
540 gtk_icon_theme_init (GtkIconTheme *icon_theme)
542 GtkIconThemePrivate *priv;
543 const gchar * const *xdg_data_dirs;
546 priv = g_type_instance_get_private ((GTypeInstance *)icon_theme,
547 GTK_TYPE_ICON_THEME);
548 icon_theme->priv = priv;
550 priv->custom_theme = FALSE;
551 priv->current_theme = g_strdup (DEFAULT_THEME_NAME);
553 xdg_data_dirs = g_get_system_data_dirs ();
554 for (i = 0; xdg_data_dirs[i]; i++) ;
556 priv->search_path_len = 2 * i + 2;
558 priv->search_path = g_new (char *, priv->search_path_len);
561 priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
562 priv->search_path[i++] = g_build_filename (g_get_user_data_dir (), "icons", NULL);
564 for (j = 0; xdg_data_dirs[j]; j++)
565 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
567 for (j = 0; xdg_data_dirs[j]; j++)
568 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "pixmaps", NULL);
570 priv->themes_valid = FALSE;
572 priv->unthemed_icons = NULL;
574 priv->pixbuf_supports_svg = pixbuf_supports_svg ();
578 free_dir_mtime (IconThemeDirMtime *dir_mtime)
580 if (dir_mtime->cache)
581 _gtk_icon_cache_unref (dir_mtime->cache);
583 g_free (dir_mtime->dir);
584 g_slice_free (IconThemeDirMtime, dir_mtime);
589 reset_styles_idle (gpointer user_data)
591 GtkIconTheme *icon_theme;
592 GtkIconThemePrivate *priv;
594 GDK_THREADS_ENTER ();
596 icon_theme = GTK_ICON_THEME (user_data);
597 priv = icon_theme->priv;
599 if (priv->screen && priv->is_screen_singleton)
601 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
602 gtk_rc_reset_styles (settings);
605 priv->reset_styles_idle = 0;
607 GDK_THREADS_LEAVE ();
613 do_theme_change (GtkIconTheme *icon_theme)
615 GtkIconThemePrivate *priv = icon_theme->priv;
618 g_print ("change to icon theme \"%s\"\n", priv->current_theme));
619 blow_themes (icon_theme);
620 g_signal_emit (icon_theme, signal_changed, 0);
622 if (!priv->reset_styles_idle)
623 priv->reset_styles_idle =
624 g_idle_add_full (GTK_PRIORITY_RESIZE - 2,
625 reset_styles_idle, icon_theme, NULL);
629 blow_themes (GtkIconTheme *icon_theme)
631 GtkIconThemePrivate *priv = icon_theme->priv;
633 if (priv->themes_valid)
635 g_hash_table_destroy (priv->all_icons);
636 g_list_foreach (priv->themes, (GFunc)theme_destroy, NULL);
637 g_list_free (priv->themes);
638 g_list_foreach (priv->dir_mtimes, (GFunc)free_dir_mtime, NULL);
639 g_list_free (priv->dir_mtimes);
640 g_hash_table_destroy (priv->unthemed_icons);
643 priv->unthemed_icons = NULL;
644 priv->dir_mtimes = NULL;
645 priv->all_icons = NULL;
646 priv->themes_valid = FALSE;
650 gtk_icon_theme_finalize (GObject *object)
652 GtkIconTheme *icon_theme;
653 GtkIconThemePrivate *priv;
656 icon_theme = GTK_ICON_THEME (object);
657 priv = icon_theme->priv;
659 if (priv->reset_styles_idle)
661 g_source_remove (priv->reset_styles_idle);
662 priv->reset_styles_idle = 0;
665 unset_screen (icon_theme);
667 g_free (priv->current_theme);
668 priv->current_theme = NULL;
670 for (i = 0; i < priv->search_path_len; i++)
671 g_free (priv->search_path[i]);
673 g_free (priv->search_path);
674 priv->search_path = NULL;
676 blow_themes (icon_theme);
678 G_OBJECT_CLASS (gtk_icon_theme_parent_class)->finalize (object);
682 * gtk_icon_theme_set_search_path:
683 * @icon_theme: a #GtkIconTheme
684 * @path: array of directories that are searched for icon themes
685 * @n_elements: number of elements in @path.
687 * Sets the search path for the icon theme object. When looking
688 * for an icon theme, GTK+ will search for a subdirectory of
689 * one or more of the directories in @path with the same name
690 * as the icon theme. (Themes from multiple of the path elements
691 * are combined to allow themes to be extended by adding icons
692 * in the user's home directory.)
694 * In addition if an icon found isn't found either in the current
695 * icon theme or the default icon theme, and an image file with
696 * the right name is found directly in one of the elements of
697 * @path, then that image will be used for the icon name.
698 * (This is legacy feature, and new icons should be put
699 * into the default icon theme, which is called DEFAULT_THEME_NAME,
700 * rather than directly on the icon path.)
705 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
709 GtkIconThemePrivate *priv;
712 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
714 priv = icon_theme->priv;
715 for (i = 0; i < priv->search_path_len; i++)
716 g_free (priv->search_path[i]);
718 g_free (priv->search_path);
720 priv->search_path = g_new (gchar *, n_elements);
721 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++;
789 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
790 priv->search_path[priv->search_path_len-1] = g_strdup (path);
792 do_theme_change (icon_theme);
796 * gtk_icon_theme_prepend_search_path:
797 * @icon_theme: a #GtkIconTheme
798 * @path: directory name to prepend to the icon path
800 * Prepends a directory to the search path.
801 * See gtk_icon_theme_set_search_path().
806 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
809 GtkIconThemePrivate *priv;
812 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
813 g_return_if_fail (path != NULL);
815 priv = icon_theme->priv;
817 priv->search_path_len++;
818 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
820 for (i = priv->search_path_len - 1; i > 0; i--)
821 priv->search_path[i] = priv->search_path[i - 1];
823 priv->search_path[0] = g_strdup (path);
825 do_theme_change (icon_theme);
829 * gtk_icon_theme_set_custom_theme:
830 * @icon_theme: a #GtkIconTheme
831 * @theme_name: name of icon theme to use instead of configured theme,
832 * or %NULL to unset a previously set custom theme
834 * Sets the name of the icon theme that the #GtkIconTheme object uses
835 * overriding system configuration. This function cannot be called
836 * on the icon theme objects returned from gtk_icon_theme_get_default()
837 * and gtk_icon_theme_get_for_screen().
842 gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
843 const gchar *theme_name)
845 GtkIconThemePrivate *priv;
847 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
849 priv = icon_theme->priv;
851 g_return_if_fail (!priv->is_screen_singleton);
853 if (theme_name != NULL)
855 priv->custom_theme = TRUE;
856 if (strcmp (theme_name, priv->current_theme) != 0)
858 g_free (priv->current_theme);
859 priv->current_theme = g_strdup (theme_name);
861 do_theme_change (icon_theme);
866 priv->custom_theme = FALSE;
868 update_current_theme (icon_theme);
873 insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
879 GtkIconThemePrivate *priv;
880 IconTheme *theme = NULL;
882 GKeyFile *theme_file;
883 GError *error = NULL;
884 IconThemeDirMtime *dir_mtime;
885 struct stat stat_buf;
887 priv = icon_theme->priv;
889 for (l = priv->themes; l != NULL; l = l->next)
892 if (strcmp (theme->name, theme_name) == 0)
896 for (i = 0; i < priv->search_path_len; i++)
898 path = g_build_filename (priv->search_path[i],
901 dir_mtime = g_slice_new (IconThemeDirMtime);
902 dir_mtime->cache = NULL;
903 dir_mtime->dir = path;
904 if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
905 dir_mtime->mtime = stat_buf.st_mtime;
907 dir_mtime->mtime = 0;
909 priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
911 priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
914 for (i = 0; i < priv->search_path_len && !theme_file; i++)
916 path = g_build_filename (priv->search_path[i],
920 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
922 theme_file = g_key_file_new ();
923 g_key_file_set_list_separator (theme_file, ',');
924 g_key_file_load_from_file (theme_file, path, 0, &error);
927 g_key_file_free (theme_file);
929 g_error_free (error);
936 if (theme_file || strcmp (theme_name, DEFAULT_THEME_NAME) == 0)
938 theme = g_new0 (IconTheme, 1);
939 theme->name = g_strdup (theme_name);
940 priv->themes = g_list_prepend (priv->themes, theme);
943 if (theme_file == NULL)
946 theme->display_name =
947 g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
948 if (!theme->display_name)
949 g_warning ("Theme file for %s has no name\n", theme_name);
951 dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
954 g_warning ("Theme file for %s has no directories\n", theme_name);
955 priv->themes = g_list_remove (priv->themes, theme);
956 g_free (theme->name);
957 g_free (theme->display_name);
959 g_key_file_free (theme_file);
964 g_key_file_get_locale_string (theme_file,
965 "Icon Theme", "Comment",
968 g_key_file_get_string (theme_file,
969 "Icon Theme", "Example",
973 for (i = 0; dirs[i] != NULL; i++)
974 theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
978 theme->dirs = g_list_reverse (theme->dirs);
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_slice_free (UnthemedIcon, unthemed_icon);
1007 strip_suffix (const char *filename)
1011 dot = strrchr (filename, '.');
1014 return g_strdup (filename);
1016 return g_strndup (filename, dot - filename);
1020 load_themes (GtkIconTheme *icon_theme)
1022 GtkIconThemePrivate *priv;
1027 UnthemedIcon *unthemed_icon;
1028 IconSuffix old_suffix, new_suffix;
1030 IconThemeDirMtime *dir_mtime;
1031 struct stat stat_buf;
1033 priv = icon_theme->priv;
1035 priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
1037 insert_theme (icon_theme, priv->current_theme);
1039 /* Always look in the "default" icon theme, and in a fallback theme */
1040 if (priv->fallback_theme)
1041 insert_theme (icon_theme, priv->fallback_theme);
1042 insert_theme (icon_theme, DEFAULT_THEME_NAME);
1043 priv->themes = g_list_reverse (priv->themes);
1046 priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
1047 g_free, (GDestroyNotify)free_unthemed_icon);
1049 for (base = 0; base < icon_theme->priv->search_path_len; base++)
1051 dir = icon_theme->priv->search_path[base];
1053 dir_mtime = g_slice_new (IconThemeDirMtime);
1054 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir);
1055 dir_mtime->dir = g_strdup (dir);
1056 if (g_stat (dir, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
1057 dir_mtime->mtime = stat_buf.st_mtime;
1059 dir_mtime->mtime = 0;
1061 priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
1063 if (dir_mtime->cache != NULL)
1066 gdir = g_dir_open (dir, 0, NULL);
1071 while ((file = g_dir_read_name (gdir)))
1073 new_suffix = suffix_from_name (file);
1075 if (new_suffix != ICON_SUFFIX_NONE)
1080 abs_file = g_build_filename (dir, file, NULL);
1081 base_name = strip_suffix (file);
1083 if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
1086 if (new_suffix == ICON_SUFFIX_SVG)
1088 if (unthemed_icon->svg_filename)
1091 unthemed_icon->svg_filename = abs_file;
1095 if (unthemed_icon->no_svg_filename)
1097 old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
1098 if (new_suffix > old_suffix)
1100 g_free (unthemed_icon->no_svg_filename);
1101 unthemed_icon->no_svg_filename = abs_file;
1107 unthemed_icon->no_svg_filename = abs_file;
1114 unthemed_icon = g_slice_new0 (UnthemedIcon);
1116 if (new_suffix == ICON_SUFFIX_SVG)
1117 unthemed_icon->svg_filename = abs_file;
1119 unthemed_icon->no_svg_filename = abs_file;
1121 g_hash_table_insert (priv->unthemed_icons,
1124 g_hash_table_insert (priv->all_icons,
1132 priv->themes_valid = TRUE;
1134 g_get_current_time(&tv);
1135 priv->last_stat_time = tv.tv_sec;
1139 _gtk_icon_theme_ensure_builtin_cache (void)
1141 static gboolean initialized = FALSE;
1143 static IconThemeDir dirs[5] =
1145 { ICON_THEME_DIR_THRESHOLD, 0, 16, 16, 16, 2, NULL, "16", NULL, NULL, NULL },
1146 { ICON_THEME_DIR_THRESHOLD, 0, 20, 20, 20, 2, NULL, "20", NULL, NULL, NULL },
1147 { ICON_THEME_DIR_THRESHOLD, 0, 24, 24, 24, 2, NULL, "24", NULL, NULL, NULL },
1148 { ICON_THEME_DIR_THRESHOLD, 0, 32, 32, 32, 2, NULL, "32", NULL, NULL, NULL },
1149 { ICON_THEME_DIR_THRESHOLD, 0, 48, 48, 48, 2, NULL, "48", NULL, NULL, NULL }
1157 _builtin_cache = _gtk_icon_cache_new ((gchar *)builtin_icons);
1159 for (i = 0; i < G_N_ELEMENTS (dirs); i++)
1162 dir->cache = _gtk_icon_cache_ref (_builtin_cache);
1164 builtin_dirs = g_list_append (builtin_dirs, dir);
1170 ensure_valid_themes (GtkIconTheme *icon_theme)
1172 GtkIconThemePrivate *priv = icon_theme->priv;
1174 gboolean was_valid = priv->themes_valid;
1176 _gtk_icon_theme_ensure_builtin_cache ();
1178 if (priv->themes_valid)
1180 g_get_current_time (&tv);
1182 if (ABS (tv.tv_sec - priv->last_stat_time) > 5)
1183 gtk_icon_theme_rescan_if_needed (icon_theme);
1186 if (!priv->themes_valid)
1188 load_themes (icon_theme);
1190 if (!priv->check_reload && was_valid && priv->screen)
1192 static GdkAtom atom_iconthemes = GDK_NONE;
1193 GdkEvent *event = gdk_event_new (GDK_CLIENT_EVENT);
1196 if (!atom_iconthemes)
1197 atom_iconthemes = gdk_atom_intern_static_string ("_GTK_LOAD_ICONTHEMES");
1199 for (i = 0; i < 5; i++)
1200 event->client.data.l[i] = 0;
1201 event->client.data_format = 32;
1202 event->client.message_type = atom_iconthemes;
1204 gdk_screen_broadcast_client_message (priv->screen, event);
1210 * gtk_icon_theme_lookup_icon:
1211 * @icon_theme: a #GtkIconTheme
1212 * @icon_name: the name of the icon to lookup
1213 * @size: desired icon size
1214 * @flags: flags modifying the behavior of the icon lookup
1216 * Looks up a named icon and returns a structure containing
1217 * information such as the filename of the icon. The icon
1218 * can then be rendered into a pixbuf using
1219 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1220 * combines these two steps if all you need is the pixbuf.)
1222 * Return value: a #GtkIconInfo structure containing information
1223 * about the icon, or %NULL if the icon wasn't found. Free with
1224 * gtk_icon_info_free()
1229 gtk_icon_theme_lookup_icon (GtkIconTheme *icon_theme,
1230 const gchar *icon_name,
1232 GtkIconLookupFlags flags)
1234 GtkIconThemePrivate *priv;
1236 GtkIconInfo *icon_info = NULL;
1237 UnthemedIcon *unthemed_icon;
1239 gboolean use_builtin;
1241 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1242 g_return_val_if_fail (icon_name != NULL, NULL);
1243 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1244 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1246 priv = icon_theme->priv;
1248 GTK_NOTE (ICONTHEME,
1249 g_print ("gtk_icon_theme_lookup_icon %s\n", icon_name));
1250 if (flags & GTK_ICON_LOOKUP_NO_SVG)
1252 else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
1255 allow_svg = priv->pixbuf_supports_svg;
1257 use_builtin = (flags & GTK_ICON_LOOKUP_USE_BUILTIN);
1259 ensure_valid_themes (icon_theme);
1261 for (l = priv->themes; l; l = l->next)
1263 IconTheme *theme = l->data;
1265 icon_info = theme_lookup_icon (theme, icon_name, size, allow_svg, use_builtin);
1270 unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_name);
1273 icon_info = icon_info_new ();
1275 /* A SVG icon, when allowed, beats out a XPM icon, but not
1279 unthemed_icon->svg_filename &&
1280 (!unthemed_icon->no_svg_filename ||
1281 suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
1282 icon_info->filename = g_strdup (unthemed_icon->svg_filename);
1283 else if (unthemed_icon->no_svg_filename)
1284 icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);
1286 icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
1287 -1, NULL, NULL, NULL);
1290 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1295 icon_info->desired_size = size;
1298 static gboolean check_for_default_theme = TRUE;
1299 char *default_theme_path;
1300 gboolean found = FALSE;
1303 if (check_for_default_theme)
1305 check_for_default_theme = FALSE;
1307 for (i = 0; !found && i < priv->search_path_len; i++)
1309 default_theme_path = g_build_filename (priv->search_path[i],
1313 found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
1314 g_free (default_theme_path);
1318 g_warning (_("Could not find the icon '%s'. The '%s' theme\n"
1319 "was not found either, perhaps you need to install it.\n"
1320 "You can get a copy from:\n"
1322 icon_name, DEFAULT_THEME_NAME, "http://icon-theme.freedesktop.org/releases");
1332 gtk_icon_theme_error_quark (void)
1334 return g_quark_from_static_string ("gtk-icon-theme-error-quark");
1338 * gtk_icon_theme_load_icon:
1339 * @icon_theme: a #GtkIconTheme
1340 * @icon_name: the name of the icon to lookup
1341 * @size: the desired icon size. The resulting icon may not be
1342 * exactly this size; see gtk_icon_info_load_icon().
1343 * @flags: flags modifying the behavior of the icon lookup
1344 * @error: Location to store error information on failure, or %NULL.
1346 * Looks up an icon in an icon theme, scales it to the given size
1347 * and renders it into a pixbuf. This is a convenience function;
1348 * if more details about the icon are needed, use
1349 * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1351 * Note that you probably want to listen for icon theme changes and
1352 * update the icon. This is usually done by connecting to the
1353 * GtkWidget::style-set signal. If for some reason you do not want to
1354 * update the icon when the icon theme changes, you should consider
1355 * using gdk_pixbuf_copy() to make a private copy of the pixbuf
1356 * returned by this function. Otherwise GTK+ may need to keep the old
1357 * icon theme loaded, which would be a waste of memory.
1359 * Return value: the rendered icon; this may be a newly created icon
1360 * or a new reference to an internal icon, so you must not modify
1361 * the icon. Use g_object_unref() to release your reference to the
1362 * icon. %NULL if the icon isn't found.
1367 gtk_icon_theme_load_icon (GtkIconTheme *icon_theme,
1368 const gchar *icon_name,
1370 GtkIconLookupFlags flags,
1373 GtkIconInfo *icon_info;
1374 GdkPixbuf *pixbuf = NULL;
1376 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1377 g_return_val_if_fail (icon_name != NULL, NULL);
1378 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1379 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1380 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1382 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
1383 flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1386 g_set_error (error, GTK_ICON_THEME_ERROR, GTK_ICON_THEME_NOT_FOUND,
1387 _("Icon '%s' not present in theme"), icon_name);
1391 pixbuf = gtk_icon_info_load_icon (icon_info, error);
1392 gtk_icon_info_free (icon_info);
1398 * gtk_icon_theme_has_icon:
1399 * @icon_theme: a #GtkIconTheme
1400 * @icon_name: the name of an icon
1402 * Checks whether an icon theme includes an icon
1403 * for a particular name.
1405 * Return value: %TRUE if @icon_theme includes an
1406 * icon for @icon_name.
1411 gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
1412 const char *icon_name)
1414 GtkIconThemePrivate *priv;
1417 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1419 priv = icon_theme->priv;
1421 ensure_valid_themes (icon_theme);
1423 for (l = priv->dir_mtimes; l; l = l->next)
1425 IconThemeDirMtime *dir_mtime = l->data;
1426 GtkIconCache *cache = dir_mtime->cache;
1428 if (cache && _gtk_icon_cache_has_icon (cache, icon_name))
1432 if (g_hash_table_lookup_extended (priv->all_icons,
1433 icon_name, NULL, NULL))
1436 if (_builtin_cache &&
1437 _gtk_icon_cache_has_icon (_builtin_cache, icon_name))
1440 if (icon_theme_builtin_icons &&
1441 g_hash_table_lookup_extended (icon_theme_builtin_icons,
1442 icon_name, NULL, NULL))
1449 add_size (gpointer key,
1453 gint **res_p = user_data;
1455 **res_p = GPOINTER_TO_INT (key);
1461 * gtk_icon_theme_get_icon_sizes:
1462 * @icon_theme: a #GtkIconTheme
1463 * @icon_name: the name of an icon
1465 * Returns an array of integers describing the sizes at which
1466 * the icon is available without scaling. A size of -1 means
1467 * that the icon is available in a scalable format. The array
1468 * is zero-terminated.
1470 * Return value: An newly allocated array describing the sizes at
1471 * which the icon is available. The array should be freed with g_free()
1472 * when it is no longer needed.
1477 gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
1478 const char *icon_name)
1480 GList *l, *d, *icons;
1484 GtkIconThemePrivate *priv;
1486 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1488 priv = icon_theme->priv;
1490 ensure_valid_themes (icon_theme);
1492 sizes = g_hash_table_new (g_direct_hash, g_direct_equal);
1494 for (l = priv->themes; l; l = l->next)
1496 IconTheme *theme = l->data;
1497 for (d = theme->dirs; d; d = d->next)
1499 IconThemeDir *dir = d->data;
1501 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1502 if (suffix != ICON_SUFFIX_NONE)
1504 if (suffix == ICON_SUFFIX_SVG)
1505 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1507 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1512 for (d = builtin_dirs; d; d = d->next)
1514 IconThemeDir *dir = d->data;
1516 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1517 if (suffix != ICON_SUFFIX_NONE)
1519 if (suffix == ICON_SUFFIX_SVG)
1520 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1522 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1526 if (icon_theme_builtin_icons)
1528 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
1532 BuiltinIcon *icon = icons->data;
1534 g_hash_table_insert (sizes, GINT_TO_POINTER (icon->size), NULL);
1535 icons = icons->next;
1539 r = result = g_new0 (gint, g_hash_table_size (sizes) + 1);
1541 g_hash_table_foreach (sizes, add_size, &r);
1542 g_hash_table_destroy (sizes);
1548 add_key_to_hash (gpointer key,
1552 GHashTable *hash = user_data;
1554 g_hash_table_insert (hash, key, NULL);
1558 add_key_to_list (gpointer key,
1562 GList **list = user_data;
1564 *list = g_list_prepend (*list, g_strdup (key));
1568 * gtk_icon_theme_list_icons:
1569 * @icon_theme: a #GtkIconTheme
1570 * @context: a string identifying a particular type of icon,
1571 * or %NULL to list all icons.
1573 * Lists the icons in the current icon theme. Only a subset
1574 * of the icons can be listed by providing a context string.
1575 * The set of values for the context string is system dependent,
1576 * but will typically include such values as "Applications" and
1579 * Return value: a #GList list holding the names of all the
1580 * icons in the theme. You must first free each element
1581 * in the list with g_free(), then free the list itself
1582 * with g_list_free().
1587 gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
1588 const char *context)
1590 GtkIconThemePrivate *priv;
1593 GQuark context_quark;
1595 priv = icon_theme->priv;
1597 ensure_valid_themes (icon_theme);
1601 context_quark = g_quark_try_string (context);
1609 icons = g_hash_table_new (g_str_hash, g_str_equal);
1614 theme_list_icons (l->data, icons, context_quark);
1618 if (context_quark == 0)
1619 g_hash_table_foreach (priv->unthemed_icons,
1625 g_hash_table_foreach (icons,
1629 g_hash_table_destroy (icons);
1635 * gtk_icon_theme_get_example_icon_name:
1636 * @icon_theme: a #GtkIconTheme
1638 * Gets the name of an icon that is representative of the
1639 * current theme (for instance, to use when presenting
1640 * a list of themes to the user.)
1642 * Return value: the name of an example icon or %NULL.
1643 * Free with g_free().
1648 gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
1650 GtkIconThemePrivate *priv;
1654 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1656 priv = icon_theme->priv;
1658 ensure_valid_themes (icon_theme);
1665 return g_strdup (theme->example);
1674 * gtk_icon_theme_rescan_if_needed:
1675 * @icon_theme: a #GtkIconTheme
1677 * Checks to see if the icon theme has changed; if it has, any
1678 * currently cached information is discarded and will be reloaded
1679 * next time @icon_theme is accessed.
1681 * Return value: %TRUE if the icon theme has changed and needed
1687 gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
1689 GtkIconThemePrivate *priv;
1690 IconThemeDirMtime *dir_mtime;
1693 struct stat stat_buf;
1696 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1698 priv = icon_theme->priv;
1700 for (d = priv->dir_mtimes; d != NULL; d = d->next)
1702 dir_mtime = d->data;
1704 stat_res = g_stat (dir_mtime->dir, &stat_buf);
1706 /* dir mtime didn't change */
1707 if (stat_res == 0 &&
1708 S_ISDIR (stat_buf.st_mode) &&
1709 dir_mtime->mtime == stat_buf.st_mtime)
1711 /* didn't exist before, and still doesn't */
1712 if (dir_mtime->mtime == 0 &&
1713 (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
1716 do_theme_change (icon_theme);
1720 g_get_current_time (&tv);
1721 priv->last_stat_time = tv.tv_sec;
1727 theme_destroy (IconTheme *theme)
1729 g_free (theme->display_name);
1730 g_free (theme->comment);
1731 g_free (theme->name);
1732 g_free (theme->example);
1734 g_list_foreach (theme->dirs, (GFunc)theme_dir_destroy, NULL);
1735 g_list_free (theme->dirs);
1741 theme_dir_destroy (IconThemeDir *dir)
1744 _gtk_icon_cache_unref (dir->cache);
1746 g_hash_table_destroy (dir->icons);
1749 g_hash_table_destroy (dir->icon_data);
1751 g_free (dir->subdir);
1756 theme_dir_size_difference (IconThemeDir *dir, int size, gboolean *smaller)
1761 case ICON_THEME_DIR_FIXED:
1762 *smaller = size < dir->size;
1763 return abs (size - dir->size);
1765 case ICON_THEME_DIR_SCALABLE:
1766 *smaller = size < dir->min_size;
1767 if (size < dir->min_size)
1768 return dir->min_size - size;
1769 if (size > dir->max_size)
1770 return size - dir->max_size;
1773 case ICON_THEME_DIR_THRESHOLD:
1774 min = dir->size - dir->threshold;
1775 max = dir->size + dir->threshold;
1776 *smaller = size < min;
1783 case ICON_THEME_DIR_UNTHEMED:
1784 g_assert_not_reached ();
1787 g_assert_not_reached ();
1792 string_from_suffix (IconSuffix suffix)
1796 case ICON_SUFFIX_XPM:
1798 case ICON_SUFFIX_SVG:
1800 case ICON_SUFFIX_PNG:
1803 g_assert_not_reached();
1809 suffix_from_name (const char *name)
1813 if (g_str_has_suffix (name, ".png"))
1814 retval = ICON_SUFFIX_PNG;
1815 else if (g_str_has_suffix (name, ".svg"))
1816 retval = ICON_SUFFIX_SVG;
1817 else if (g_str_has_suffix (name, ".xpm"))
1818 retval = ICON_SUFFIX_XPM;
1820 retval = ICON_SUFFIX_NONE;
1826 best_suffix (IconSuffix suffix,
1829 if ((suffix & ICON_SUFFIX_PNG) != 0)
1830 return ICON_SUFFIX_PNG;
1831 else if (allow_svg && ((suffix & ICON_SUFFIX_SVG) != 0))
1832 return ICON_SUFFIX_SVG;
1833 else if ((suffix & ICON_SUFFIX_XPM) != 0)
1834 return ICON_SUFFIX_XPM;
1836 return ICON_SUFFIX_NONE;
1841 theme_dir_get_icon_suffix (IconThemeDir *dir,
1842 const gchar *icon_name,
1843 gboolean *has_icon_file)
1849 suffix = (IconSuffix)_gtk_icon_cache_get_icon_flags (dir->cache,
1854 *has_icon_file = suffix & HAS_ICON_FILE;
1856 suffix = suffix & ~HAS_ICON_FILE;
1859 suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
1861 GTK_NOTE (ICONTHEME,
1862 g_print ("get_icon_suffix%s %u\n", dir->cache ? " (cached)" : "", suffix));
1867 static GtkIconInfo *
1868 theme_lookup_icon (IconTheme *theme,
1869 const char *icon_name,
1872 gboolean use_builtin)
1875 IconThemeDir *dir, *min_dir;
1877 int min_difference, difference;
1878 BuiltinIcon *closest_builtin = NULL;
1879 gboolean smaller, has_larger;
1882 min_difference = G_MAXINT;
1886 /* Builtin icons are logically part of the default theme and
1887 * are searched before other subdirectories of the default theme.
1889 if (strcmp (theme->name, DEFAULT_THEME_NAME) == 0 && use_builtin)
1891 closest_builtin = find_builtin_icon (icon_name,
1896 if (min_difference == 0)
1897 return icon_info_new_builtin (closest_builtin);
1899 dirs = builtin_dirs;
1909 GTK_NOTE (ICONTHEME,
1910 g_print ("theme_lookup_icon dir %s\n", dir->dir));
1911 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1912 if (best_suffix (suffix, allow_svg) != ICON_SUFFIX_NONE)
1914 difference = theme_dir_size_difference (dir, size, &smaller);
1916 if (difference == 0)
1924 if (difference < min_difference || smaller)
1926 min_difference = difference;
1928 closest_builtin = NULL;
1929 has_larger = smaller;
1934 if (difference < min_difference && smaller)
1936 min_difference = difference;
1938 closest_builtin = NULL;
1946 if (l == NULL && dirs == builtin_dirs)
1953 if (closest_builtin)
1954 return icon_info_new_builtin (closest_builtin);
1958 GtkIconInfo *icon_info = icon_info_new ();
1959 gboolean has_icon_file = FALSE;
1961 suffix = theme_dir_get_icon_suffix (min_dir, icon_name, &has_icon_file);
1962 suffix = best_suffix (suffix, allow_svg);
1963 g_assert (suffix != ICON_SUFFIX_NONE);
1965 file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
1966 icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
1969 icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
1970 -1, NULL, NULL, NULL);
1973 if (min_dir->icon_data != NULL)
1974 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
1976 if (icon_info->data == NULL && min_dir->cache != NULL)
1978 icon_info->data = _gtk_icon_cache_get_icon_data (min_dir->cache, icon_name, min_dir->subdir);
1979 if (icon_info->data)
1981 if (min_dir->icon_data == NULL)
1982 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
1983 g_free, (GDestroyNotify)icon_data_free);
1985 g_hash_table_replace (min_dir->icon_data, g_strdup (icon_name), icon_info->data);
1989 if (icon_info->data == NULL && has_icon_file)
1991 gchar *icon_file_name, *icon_file_path;
1993 icon_file_name = g_strconcat (icon_name, ".icon", NULL);
1994 icon_file_path = g_build_filename (min_dir->dir, icon_file_name, NULL);
1996 if (g_file_test (icon_file_path, G_FILE_TEST_IS_REGULAR))
1998 if (min_dir->icon_data == NULL)
1999 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2000 g_free, (GDestroyNotify)icon_data_free);
2001 load_icon_data (min_dir, icon_file_path, icon_file_name);
2003 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2005 g_free (icon_file_name);
2006 g_free (icon_file_path);
2011 icon_info->cache_pixbuf = _gtk_icon_cache_get_icon (min_dir->cache, icon_name,
2015 icon_info->dir_type = min_dir->type;
2016 icon_info->dir_size = min_dir->size;
2017 icon_info->threshold = min_dir->threshold;
2026 theme_list_icons (IconTheme *theme,
2030 GList *l = theme->dirs;
2037 if (context == dir->context ||
2042 _gtk_icon_cache_add_icons (dir->cache,
2049 g_hash_table_foreach (dir->icons,
2059 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
2061 GKeyFile *icon_file;
2069 GError *error = NULL;
2073 icon_file = g_key_file_new ();
2074 g_key_file_set_list_separator (icon_file, ',');
2075 g_key_file_load_from_file (icon_file, path, 0, &error);
2078 g_error_free (error);
2079 g_key_file_free (icon_file);
2084 base_name = strip_suffix (name);
2086 data = g_slice_new0 (GtkIconData);
2087 g_hash_table_replace (dir->icon_data, base_name, data);
2089 ivalues = g_key_file_get_integer_list (icon_file,
2090 "Icon Data", "EmbeddedTextRectangle",
2096 data->has_embedded_rect = TRUE;
2097 data->x0 = ivalues[0];
2098 data->y0 = ivalues[1];
2099 data->x1 = ivalues[2];
2100 data->y1 = ivalues[3];
2106 str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
2109 split = g_strsplit (str, "|", -1);
2111 data->n_attach_points = g_strv_length (split);
2112 data->attach_points = g_new (GdkPoint, data->n_attach_points);
2115 while (split[i] != NULL && i < data->n_attach_points)
2117 split_point = strchr (split[i], ',');
2122 data->attach_points[i].x = atoi (split[i]);
2123 data->attach_points[i].y = atoi (split_point);
2132 data->display_name = g_key_file_get_locale_string (icon_file,
2133 "Icon Data", "DisplayName",
2135 g_key_file_free (icon_file);
2140 scan_directory (GtkIconThemePrivate *icon_theme,
2141 IconThemeDir *dir, char *full_dir)
2146 GTK_NOTE (ICONTHEME,
2147 g_print ("scanning directory %s\n", full_dir));
2148 dir->icons = g_hash_table_new_full (g_str_hash, g_str_equal,
2151 gdir = g_dir_open (full_dir, 0, NULL);
2156 while ((name = g_dir_read_name (gdir)))
2160 IconSuffix suffix, hash_suffix;
2162 if (g_str_has_suffix (name, ".icon"))
2164 if (dir->icon_data == NULL)
2165 dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2166 g_free, (GDestroyNotify)icon_data_free);
2168 path = g_build_filename (full_dir, name, NULL);
2169 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
2170 load_icon_data (dir, path, name);
2177 suffix = suffix_from_name (name);
2178 if (suffix == ICON_SUFFIX_NONE)
2181 base_name = strip_suffix (name);
2183 hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
2184 g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
2185 g_hash_table_insert (icon_theme->all_icons, base_name, NULL);
2192 theme_subdir_load (GtkIconTheme *icon_theme,
2194 GKeyFile *theme_file,
2200 IconThemeDirType type;
2201 char *context_string;
2208 GError *error = NULL;
2209 IconThemeDirMtime *dir_mtime;
2211 size = g_key_file_get_integer (theme_file, subdir, "Size", &error);
2214 g_error_free (error);
2215 g_warning ("Theme directory %s of theme %s has no size field\n",
2216 subdir, theme->name);
2220 type = ICON_THEME_DIR_THRESHOLD;
2221 type_string = g_key_file_get_string (theme_file, subdir, "Type", NULL);
2224 if (strcmp (type_string, "Fixed") == 0)
2225 type = ICON_THEME_DIR_FIXED;
2226 else if (strcmp (type_string, "Scalable") == 0)
2227 type = ICON_THEME_DIR_SCALABLE;
2228 else if (strcmp (type_string, "Threshold") == 0)
2229 type = ICON_THEME_DIR_THRESHOLD;
2231 g_free (type_string);
2235 context_string = g_key_file_get_string (theme_file, subdir, "Context", NULL);
2238 context = g_quark_from_string (context_string);
2239 g_free (context_string);
2242 max_size = g_key_file_get_integer (theme_file, subdir, "MaxSize", &error);
2247 g_error_free (error);
2251 min_size = g_key_file_get_integer (theme_file, subdir, "MinSize", &error);
2256 g_error_free (error);
2260 threshold = g_key_file_get_integer (theme_file, subdir, "Threshold", &error);
2265 g_error_free (error);
2269 for (d = icon_theme->priv->dir_mtimes; d; d = d->next)
2271 dir_mtime = (IconThemeDirMtime *)d->data;
2273 if (dir_mtime->mtime == 0)
2274 continue; /* directory doesn't exist */
2276 full_dir = g_build_filename (dir_mtime->dir, subdir, NULL);
2278 /* First, see if we have a cache for the directory */
2279 if (dir_mtime->cache != NULL || g_file_test (full_dir, G_FILE_TEST_IS_DIR))
2281 if (dir_mtime->cache == NULL)
2283 /* This will return NULL if the cache doesn't exist or is outdated */
2284 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir_mtime->dir);
2287 dir = g_new (IconThemeDir, 1);
2289 dir->context = context;
2291 dir->min_size = min_size;
2292 dir->max_size = max_size;
2293 dir->threshold = threshold;
2294 dir->dir = full_dir;
2295 dir->icon_data = NULL;
2296 dir->subdir = g_strdup (subdir);
2297 if (dir_mtime->cache != NULL)
2298 dir->cache = _gtk_icon_cache_ref (dir_mtime->cache);
2302 scan_directory (icon_theme->priv, dir, full_dir);
2305 theme->dirs = g_list_prepend (theme->dirs, dir);
2313 icon_data_free (GtkIconData *icon_data)
2315 g_free (icon_data->attach_points);
2316 g_free (icon_data->display_name);
2317 g_slice_free (GtkIconData, icon_data);
2324 gtk_icon_info_get_type (void)
2326 static GType our_type = 0;
2329 our_type = g_boxed_type_register_static (I_("GtkIconInfo"),
2330 (GBoxedCopyFunc) gtk_icon_info_copy,
2331 (GBoxedFreeFunc) gtk_icon_info_free);
2337 static GtkIconInfo *
2338 icon_info_new (void)
2340 GtkIconInfo *icon_info = g_slice_new0 (GtkIconInfo);
2342 icon_info->scale = -1.;
2347 static GtkIconInfo *
2348 icon_info_new_builtin (BuiltinIcon *icon)
2350 GtkIconInfo *icon_info = icon_info_new ();
2352 icon_info->cache_pixbuf = g_object_ref (icon->pixbuf);
2353 icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
2354 icon_info->dir_size = icon->size;
2355 icon_info->threshold = 2;
2361 * gtk_icon_info_copy:
2362 * @icon_info: a #GtkIconInfo
2364 * Make a copy of a #GtkIconInfo.
2366 * Return value: the new GtkIconInfo
2371 gtk_icon_info_copy (GtkIconInfo *icon_info)
2375 g_return_val_if_fail (icon_info != NULL, NULL);
2377 copy = memcpy (g_slice_new (GtkIconInfo), icon_info, sizeof (GtkIconInfo));
2378 if (copy->cache_pixbuf)
2379 g_object_ref (copy->cache_pixbuf);
2381 g_object_ref (copy->pixbuf);
2382 if (copy->load_error)
2383 copy->load_error = g_error_copy (copy->load_error);
2385 copy->filename = g_strdup (copy->filename);
2387 if (copy->cp_filename)
2388 copy->cp_filename = g_strdup (copy->cp_filename);
2395 * gtk_icon_info_free:
2396 * @icon_info: a #GtkIconInfo
2398 * Free a #GtkIconInfo and associated information
2403 gtk_icon_info_free (GtkIconInfo *icon_info)
2405 g_return_if_fail (icon_info != NULL);
2407 if (icon_info->filename)
2408 g_free (icon_info->filename);
2410 if (icon_info->cp_filename)
2411 g_free (icon_info->cp_filename);
2413 if (icon_info->pixbuf)
2414 g_object_unref (icon_info->pixbuf);
2415 if (icon_info->cache_pixbuf)
2416 g_object_unref (icon_info->cache_pixbuf);
2418 g_slice_free (GtkIconInfo, icon_info);
2422 * gtk_icon_info_get_base_size:
2423 * @icon_info: a #GtkIconInfo
2425 * Gets the base size for the icon. The base size
2426 * is a size for the icon that was specified by
2427 * the icon theme creator. This may be different
2428 * than the actual size of image; an example of
2429 * this is small emblem icons that can be attached
2430 * to a larger icon. These icons will be given
2431 * the same base size as the larger icons to which
2432 * they are attached.
2434 * Return value: the base size, or 0, if no base
2435 * size is known for the icon.
2440 gtk_icon_info_get_base_size (GtkIconInfo *icon_info)
2442 g_return_val_if_fail (icon_info != NULL, 0);
2444 return icon_info->dir_size;
2448 * gtk_icon_info_get_filename:
2449 * @icon_info: a #GtkIconInfo
2451 * Gets the filename for the icon. If the
2452 * %GTK_ICON_LOOKUP_USE_BUILTIN flag was passed
2453 * to gtk_icon_theme_lookup_icon(), there may be
2454 * no filename if a builtin icon is returned; in this
2455 * case, you should use gtk_icon_info_get_builtin_pixbuf().
2457 * Return value: the filename for the icon, or %NULL
2458 * if gtk_icon_info_get_builtin_pixbuf() should
2459 * be used instead. The return value is owned by
2460 * GTK+ and should not be modified or freed.
2464 G_CONST_RETURN gchar *
2465 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2467 g_return_val_if_fail (icon_info != NULL, NULL);
2469 return icon_info->filename;
2473 * gtk_icon_info_get_builtin_pixbuf:
2474 * @icon_info: a #GtkIconInfo structure
2476 * Gets the built-in image for this icon, if any. To allow
2477 * GTK+ to use built in icon images, you must pass the
2478 * %GTK_ICON_LOOKUP_USE_BUILTIN to
2479 * gtk_icon_theme_lookup_icon().
2481 * Return value: the built-in image pixbuf, or %NULL. No
2482 * extra reference is added to the returned pixbuf, so if
2483 * you want to keep it around, you must use g_object_ref().
2484 * The returned image must not be modified.
2489 gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
2491 g_return_val_if_fail (icon_info != NULL, NULL);
2493 if (icon_info->filename)
2496 return icon_info->cache_pixbuf;
2500 load_svg_at_size (const gchar *filename,
2504 GdkPixbuf *pixbuf = NULL;
2505 GdkPixbufLoader *loader = NULL;
2506 gchar *contents = NULL;
2509 if (!g_file_get_contents (filename,
2510 &contents, &length, error))
2513 loader = gdk_pixbuf_loader_new_with_type ("svg", error);
2517 gdk_pixbuf_loader_set_size (loader, size, size);
2519 if (!gdk_pixbuf_loader_write (loader, contents, length, error))
2521 gdk_pixbuf_loader_close (loader, NULL);
2525 if (!gdk_pixbuf_loader_close (loader, error))
2528 pixbuf = g_object_ref (gdk_pixbuf_loader_get_pixbuf (loader));
2532 g_object_unref (loader);
2539 /* This function contains the complicatd logic for deciding
2540 * on the size at which to load the icon and loading it at
2544 icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
2545 gboolean scale_only)
2547 int image_width, image_height;
2548 GdkPixbuf *source_pixbuf;
2550 /* First check if we already succeeded have the necessary
2551 * information (or failed earlier)
2553 if (scale_only && icon_info->scale >= 0)
2556 if (icon_info->pixbuf)
2559 if (icon_info->load_error)
2562 /* SVG icons are a special case - we just immediately scale them
2563 * to the desired size
2565 if (icon_info->filename && g_str_has_suffix (icon_info->filename, ".svg"))
2567 icon_info->scale = icon_info->desired_size / 1000.;
2572 icon_info->pixbuf = load_svg_at_size (icon_info->filename,
2573 icon_info->desired_size,
2574 &icon_info->load_error);
2576 return icon_info->pixbuf != NULL;
2579 /* In many cases, the scale can be determined without actual access
2580 * to the icon file. This is generally true when we have a size
2581 * for the directory where the icon is; the image size doesn't
2582 * matter in that case.
2584 if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
2585 icon_info->scale = 1.0;
2586 else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
2588 if (icon_info->desired_size >= icon_info->dir_size - icon_info->threshold &&
2589 icon_info->desired_size <= icon_info->dir_size + icon_info->threshold)
2590 icon_info->scale = 1.0;
2591 else if (icon_info->dir_size > 0)
2592 icon_info->scale =(gdouble) icon_info->desired_size / icon_info->dir_size;
2594 else if (icon_info->dir_type == ICON_THEME_DIR_SCALABLE)
2596 if (icon_info->dir_size > 0)
2597 icon_info->scale = (gdouble) icon_info->desired_size / icon_info->dir_size;
2600 if (icon_info->scale >= 0. && scale_only)
2603 /* At this point, we need to actually get the icon; either from the
2604 * builting image or by loading the file
2606 if (icon_info->cache_pixbuf)
2607 source_pixbuf = g_object_ref (icon_info->cache_pixbuf);
2611 source_pixbuf = gdk_pixbuf_new_from_file (icon_info->filename,
2612 &icon_info->load_error);
2617 /* Do scale calculations that depend on the image size
2619 image_width = gdk_pixbuf_get_width (source_pixbuf);
2620 image_height = gdk_pixbuf_get_height (source_pixbuf);
2622 if (icon_info->scale < 0.0)
2624 gint image_size = MAX (image_width, image_height);
2626 icon_info->scale = icon_info->desired_size / (gdouble)image_size;
2628 icon_info->scale = 1.0;
2630 if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED)
2631 icon_info->scale = MIN (icon_info->scale, 1.0);
2634 /* We don't short-circuit out here for scale_only, since, now
2635 * we've loaded the icon, we might as well go ahead and finish
2636 * the job. This is a bit of a waste when we scale here
2637 * and never get the final pixbuf; at the cost of a bit of
2638 * extra complexity, we could keep the source pixbuf around
2639 * but not actually scale it until neede.
2642 if (icon_info->scale == 1.0)
2643 icon_info->pixbuf = source_pixbuf;
2646 icon_info->pixbuf = gdk_pixbuf_scale_simple (source_pixbuf,
2647 0.5 + image_width * icon_info->scale,
2648 0.5 + image_height * icon_info->scale,
2649 GDK_INTERP_BILINEAR);
2650 g_object_unref (source_pixbuf);
2657 * gtk_icon_info_load_icon:
2658 * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
2659 * @error: location to store error information on failure, or %NULL.
2661 * Renders an icon previously looked up in an icon theme using
2662 * gtk_icon_theme_lookup_icon(); the size will be based on the size
2663 * passed to gtk_icon_theme_lookup_icon(). Note that the resulting
2664 * pixbuf may not be exactly this size; an icon theme may have icons
2665 * that differ slightly from their nominal sizes, and in addition GTK+
2666 * will avoid scaling icons that it considers sufficiently close to the
2667 * requested size or for which the source image would have to be scaled
2668 * up too far. (This maintains sharpness.)
2670 * Return value: the rendered icon; this may be a newly created icon
2671 * or a new reference to an internal icon, so you must not modify
2672 * the icon. Use g_object_unref() to release your reference to the
2678 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
2681 g_return_val_if_fail (icon_info != NULL, NULL);
2683 g_return_val_if_fail (icon_info != NULL, NULL);
2684 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2686 icon_info_ensure_scale_and_pixbuf (icon_info, FALSE);
2688 if (icon_info->load_error)
2690 g_propagate_error (error, icon_info->load_error);
2694 return g_object_ref (icon_info->pixbuf);
2698 * gtk_icon_info_set_raw_coordinates:
2699 * @icon_info: a #GtkIconInfo
2700 * @raw_coordinates: whether the coordinates of embedded rectangles
2701 * and attached points should be returned in their original
2704 * Sets whether the coordinates returned by gtk_icon_info_get_embedded_rect()
2705 * and gtk_icon_info_get_attach_points() should be returned in their
2706 * original form as specified in the icon theme, instead of scaled
2707 * appropriately for the pixbuf returned by gtk_icon_info_load_icon().
2709 * Raw coordinates are somewhat strange; they are specified to be with
2710 * respect to the unscaled pixmap for PNG and XPM icons, but for SVG
2711 * icons, they are in a 1000x1000 coordinate space that is scaled
2712 * to the final size of the icon. You can determine if the icon is an SVG
2713 * icon by using gtk_icon_info_get_filename(), and seeing if it is non-%NULL
2714 * and ends in '.svg'.
2716 * This function is provided primarily to allow compatibility wrappers
2717 * for older API's, and is not expected to be useful for applications.
2722 gtk_icon_info_set_raw_coordinates (GtkIconInfo *icon_info,
2723 gboolean raw_coordinates)
2725 g_return_if_fail (icon_info != NULL);
2727 icon_info->raw_coordinates = raw_coordinates != FALSE;
2730 /* Scale coordinates from the icon data prior to returning
2734 icon_info_scale_point (GtkIconInfo *icon_info,
2740 if (icon_info->raw_coordinates)
2747 if (!icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2750 *x_out = 0.5 + x * icon_info->scale;
2751 *y_out = 0.5 + y * icon_info->scale;
2758 * gtk_icon_info_get_embedded_rect:
2759 * @icon_info: a #GtkIconInfo
2760 * @rectangle: #GdkRectangle in which to store embedded
2761 * rectangle coordinates; coordinates are only stored
2762 * when this function returns %TRUE.
2764 * Gets the coordinates of a rectangle within the icon
2765 * that can be used for display of information such
2766 * as a preview of the contents of a text file.
2767 * See gtk_icon_info_set_raw_coordinates() for further
2768 * information about the coordinate system.
2770 * Return value: %TRUE if the icon has an embedded rectangle
2775 gtk_icon_info_get_embedded_rect (GtkIconInfo *icon_info,
2776 GdkRectangle *rectangle)
2778 g_return_val_if_fail (icon_info != NULL, FALSE);
2780 if (icon_info->data && icon_info->data->has_embedded_rect &&
2781 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2783 gint scaled_x0, scaled_y0;
2784 gint scaled_x1, scaled_y1;
2788 icon_info_scale_point (icon_info,
2789 icon_info->data->x0, icon_info->data->y0,
2790 &scaled_x0, &scaled_y0);
2791 icon_info_scale_point (icon_info,
2792 icon_info->data->x1, icon_info->data->y1,
2793 &scaled_x1, &scaled_y1);
2795 rectangle->x = scaled_x0;
2796 rectangle->y = scaled_y0;
2797 rectangle->width = scaled_x1 - rectangle->x;
2798 rectangle->height = scaled_y1 - rectangle->y;
2808 * gtk_icon_info_get_attach_points:
2809 * @icon_info: a #GtkIconInfo
2810 * @points: location to store pointer to an array of points, or %NULL
2811 * free the array of points with g_free().
2812 * @n_points: location to store the number of points in @points, or %NULL
2814 * Fetches the set of attach points for an icon. An attach point
2815 * is a location in the icon that can be used as anchor points for attaching
2816 * emblems or overlays to the icon.
2818 * Return value: %TRUE if there are any attach points for the icon.
2823 gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
2827 g_return_val_if_fail (icon_info != NULL, FALSE);
2829 if (icon_info->data && icon_info->data->n_attach_points &&
2830 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2836 *points = g_new (GdkPoint, icon_info->data->n_attach_points);
2837 for (i = 0; i < icon_info->data->n_attach_points; i++)
2838 icon_info_scale_point (icon_info,
2839 icon_info->data->attach_points[i].x,
2840 icon_info->data->attach_points[i].y,
2846 *n_points = icon_info->data->n_attach_points;
2862 * gtk_icon_info_get_display_name:
2863 * @icon_info: a #GtkIconInfo
2865 * Gets the display name for an icon. A display name is a
2866 * string to be used in place of the icon name in a user
2867 * visible context like a list of icons.
2869 * Return value: the display name for the icon or %NULL, if
2870 * the icon doesn't have a specified display name. This value
2871 * is owned @icon_info and must not be modified or free.
2875 G_CONST_RETURN gchar *
2876 gtk_icon_info_get_display_name (GtkIconInfo *icon_info)
2878 g_return_val_if_fail (icon_info != NULL, NULL);
2880 if (icon_info->data)
2881 return icon_info->data->display_name;
2892 * gtk_icon_theme_add_builtin_icon:
2893 * @icon_name: the name of the icon to register
2894 * @size: the size at which to register the icon (different
2895 * images can be registered for the same icon name
2896 * at different sizes.)
2897 * @pixbuf: #GdkPixbuf that contains the image to use
2900 * Registers a built-in icon for icon theme lookups. The idea
2901 * of built-in icons is to allow an application or library
2902 * that uses themed icons to function requiring files to
2903 * be present in the file system. For instance, the default
2904 * images for all of GTK+'s stock icons are registered
2907 * In general, if you use gtk_icon_theme_add_builtin_icon()
2908 * you should also install the icon in the icon theme, so
2909 * that the icon is generally available.
2911 * This function will generally be used with pixbufs loaded
2912 * via gdk_pixbuf_new_from_inline().
2917 gtk_icon_theme_add_builtin_icon (const gchar *icon_name,
2921 BuiltinIcon *default_icon;
2925 g_return_if_fail (icon_name != NULL);
2926 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2928 if (!icon_theme_builtin_icons)
2929 icon_theme_builtin_icons = g_hash_table_new (g_str_hash, g_str_equal);
2931 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
2933 key = g_strdup (icon_name);
2935 key = (gpointer)icon_name; /* Won't get stored */
2937 default_icon = g_new (BuiltinIcon, 1);
2938 default_icon->size = size;
2939 default_icon->pixbuf = g_object_ref (pixbuf);
2940 icons = g_slist_prepend (icons, default_icon);
2942 /* Replaces value, leaves key untouched
2944 g_hash_table_insert (icon_theme_builtin_icons, key, icons);
2947 /* Look up a builtin icon; the min_difference_p and
2948 * has_larger_p out parameters allow us to combine
2949 * this lookup with searching through the actual directories
2950 * of the "hicolor" icon theme. See theme_lookup_icon()
2951 * for how they are used.
2953 static BuiltinIcon *
2954 find_builtin_icon (const gchar *icon_name,
2956 gint *min_difference_p,
2957 gboolean *has_larger_p)
2959 GSList *icons = NULL;
2960 gint min_difference = G_MAXINT;
2961 gboolean has_larger = FALSE;
2962 BuiltinIcon *min_icon = NULL;
2964 if (!icon_theme_builtin_icons)
2967 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
2971 BuiltinIcon *default_icon = icons->data;
2972 int min, max, difference;
2975 min = default_icon->size - 2;
2976 max = default_icon->size + 2;
2977 smaller = size < min;
2979 difference = min - size;
2980 else if (size > max)
2981 difference = size - max;
2985 if (difference == 0)
2987 min_icon = default_icon;
2993 if (difference < min_difference || smaller)
2995 min_difference = difference;
2996 min_icon = default_icon;
2997 has_larger = smaller;
3002 if (difference < min_difference && smaller)
3004 min_difference = difference;
3005 min_icon = default_icon;
3009 icons = icons->next;
3012 if (min_difference_p)
3013 *min_difference_p = min_difference;
3015 *has_larger_p = has_larger;
3021 _gtk_icon_theme_check_reload (GdkDisplay *display)
3025 GtkIconTheme *icon_theme;
3027 n_screens = gdk_display_get_n_screens (display);
3029 for (i = 0; i < n_screens; i++)
3031 screen = gdk_display_get_screen (display, i);
3033 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
3036 icon_theme->priv->check_reload = TRUE;
3037 ensure_valid_themes (icon_theme);
3038 icon_theme->priv->check_reload = FALSE;
3045 /* DLL ABI stability backward compatibility versions */
3047 #undef gtk_icon_theme_set_search_path
3050 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
3051 const gchar *path[],
3054 const gchar **utf8_path;
3057 utf8_path = g_new (const gchar *, n_elements);
3059 for (i = 0; i < n_elements; i++)
3060 utf8_path[i] = g_locale_to_utf8 (path[i], -1, NULL, NULL, NULL);
3062 gtk_icon_theme_set_search_path_utf8 (icon_theme, utf8_path, n_elements);
3064 for (i = 0; i < n_elements; i++)
3065 g_free ((gchar *) utf8_path[i]);
3070 #undef gtk_icon_theme_get_search_path
3073 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
3079 gtk_icon_theme_get_search_path_utf8 (icon_theme, path, &n);
3086 for (i = 0; i < n; i++)
3088 gchar *tem = (*path)[i];
3090 (*path)[i] = g_locale_from_utf8 ((*path)[i], -1, NULL, NULL, NULL);
3096 #undef gtk_icon_theme_append_search_path
3099 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
3102 gchar *utf8_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
3104 gtk_icon_theme_append_search_path_utf8 (icon_theme, utf8_path);
3109 #undef gtk_icon_theme_prepend_search_path
3112 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
3115 gchar *utf8_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
3117 gtk_icon_theme_prepend_search_path_utf8 (icon_theme, utf8_path);
3122 #undef gtk_icon_info_get_filename
3124 G_CONST_RETURN gchar *
3125 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
3127 g_return_val_if_fail (icon_info != NULL, NULL);
3129 return icon_info->cp_filename;
3134 #define __GTK_ICON_THEME_C__
3135 #include "gtkaliasdef.c"