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 #define theme_changed(_old, _new) \
381 ((_old && !_new) || (!_old && _new) || \
382 (_old && _new && strcmp (_old, _new) != 0))
383 GtkIconThemePrivate *priv = icon_theme->priv;
385 if (!priv->custom_theme)
388 gchar *fallback_theme = NULL;
389 gboolean changed = FALSE;
393 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
394 g_object_get (settings,
395 "gtk-icon-theme-name", &theme,
396 "gtk-fallback-icon-theme", &fallback_theme, NULL);
399 /* ensure that the current theme (even when just the default)
400 * is searched before any fallback theme
402 if (!theme && fallback_theme)
403 theme = g_strdup (DEFAULT_THEME_NAME);
405 if (theme_changed (priv->current_theme, theme))
407 g_free (priv->current_theme);
408 priv->current_theme = theme;
414 if (theme_changed (priv->fallback_theme, fallback_theme))
416 g_free (priv->fallback_theme);
417 priv->fallback_theme = fallback_theme;
421 g_free (fallback_theme);
424 do_theme_change (icon_theme);
429 /* Callback when the icon theme GtkSetting changes
432 theme_changed (GtkSettings *settings,
434 GtkIconTheme *icon_theme)
436 update_current_theme (icon_theme);
440 unset_screen (GtkIconTheme *icon_theme)
442 GtkIconThemePrivate *priv = icon_theme->priv;
443 GtkSettings *settings;
448 settings = gtk_settings_get_for_screen (priv->screen);
449 display = gdk_screen_get_display (priv->screen);
451 g_signal_handlers_disconnect_by_func (display,
452 (gpointer) display_closed,
454 g_signal_handlers_disconnect_by_func (settings,
455 (gpointer) theme_changed,
463 * gtk_icon_theme_set_screen:
464 * @icon_theme: a #GtkIconTheme
465 * @screen: a #GdkScreen
467 * Sets the screen for an icon theme; the screen is used
468 * to track the user's currently configured icon theme,
469 * which might be different for different screens.
474 gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
477 GtkIconThemePrivate *priv;
478 GtkSettings *settings;
481 g_return_if_fail (GTK_ICON_THEME (icon_theme));
482 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
484 priv = icon_theme->priv;
486 unset_screen (icon_theme);
490 display = gdk_screen_get_display (screen);
491 settings = gtk_settings_get_for_screen (screen);
493 priv->screen = screen;
495 g_signal_connect (display, "closed",
496 G_CALLBACK (display_closed), icon_theme);
497 g_signal_connect (settings, "notify::gtk-icon-theme-name",
498 G_CALLBACK (theme_changed), icon_theme);
499 g_signal_connect (settings, "notify::gtk-fallback-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)
514 static gint found_svg = -1;
519 formats = gdk_pixbuf_get_formats ();
522 for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
524 gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
527 for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
529 if (strcmp (*mime_type, "image/svg") == 0)
533 g_strfreev (mime_types);
536 g_slist_free (formats);
542 gtk_icon_theme_init (GtkIconTheme *icon_theme)
544 GtkIconThemePrivate *priv;
545 const gchar * const *xdg_data_dirs;
548 priv = g_type_instance_get_private ((GTypeInstance *)icon_theme,
549 GTK_TYPE_ICON_THEME);
550 icon_theme->priv = priv;
552 priv->custom_theme = FALSE;
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;
575 priv->pixbuf_supports_svg = pixbuf_supports_svg ();
579 free_dir_mtime (IconThemeDirMtime *dir_mtime)
581 if (dir_mtime->cache)
582 _gtk_icon_cache_unref (dir_mtime->cache);
584 g_free (dir_mtime->dir);
585 g_slice_free (IconThemeDirMtime, dir_mtime);
590 reset_styles_idle (gpointer user_data)
592 GtkIconTheme *icon_theme;
593 GtkIconThemePrivate *priv;
595 icon_theme = GTK_ICON_THEME (user_data);
596 priv = icon_theme->priv;
598 if (priv->screen && priv->is_screen_singleton)
600 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
601 gtk_rc_reset_styles (settings);
604 priv->reset_styles_idle = 0;
610 do_theme_change (GtkIconTheme *icon_theme)
612 GtkIconThemePrivate *priv = icon_theme->priv;
614 if (!priv->themes_valid)
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 gdk_threads_add_idle_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 (!priv->current_theme || 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 if (priv->custom_theme)
868 priv->custom_theme = FALSE;
870 update_current_theme (icon_theme);
876 insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
882 GtkIconThemePrivate *priv;
883 IconTheme *theme = NULL;
885 GKeyFile *theme_file;
886 GError *error = NULL;
887 IconThemeDirMtime *dir_mtime;
888 struct stat stat_buf;
890 priv = icon_theme->priv;
892 for (l = priv->themes; l != NULL; l = l->next)
895 if (strcmp (theme->name, theme_name) == 0)
899 for (i = 0; i < priv->search_path_len; i++)
901 path = g_build_filename (priv->search_path[i],
904 dir_mtime = g_slice_new (IconThemeDirMtime);
905 dir_mtime->cache = NULL;
906 dir_mtime->dir = path;
907 if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
908 dir_mtime->mtime = stat_buf.st_mtime;
910 dir_mtime->mtime = 0;
912 priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
914 priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
917 for (i = 0; i < priv->search_path_len && !theme_file; i++)
919 path = g_build_filename (priv->search_path[i],
923 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
925 theme_file = g_key_file_new ();
926 g_key_file_set_list_separator (theme_file, ',');
927 g_key_file_load_from_file (theme_file, path, 0, &error);
930 g_key_file_free (theme_file);
932 g_error_free (error);
939 if (theme_file || strcmp (theme_name, DEFAULT_THEME_NAME) == 0)
941 theme = g_new0 (IconTheme, 1);
942 theme->name = g_strdup (theme_name);
943 priv->themes = g_list_prepend (priv->themes, theme);
946 if (theme_file == NULL)
949 theme->display_name =
950 g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
951 if (!theme->display_name)
952 g_warning ("Theme file for %s has no name\n", theme_name);
954 dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
957 g_warning ("Theme file for %s has no directories\n", theme_name);
958 priv->themes = g_list_remove (priv->themes, theme);
959 g_free (theme->name);
960 g_free (theme->display_name);
962 g_key_file_free (theme_file);
967 g_key_file_get_locale_string (theme_file,
968 "Icon Theme", "Comment",
971 g_key_file_get_string (theme_file,
972 "Icon Theme", "Example",
976 for (i = 0; dirs[i] != NULL; i++)
977 theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
981 theme->dirs = g_list_reverse (theme->dirs);
983 themes = g_key_file_get_string_list (theme_file,
990 for (i = 0; themes[i] != NULL; i++)
991 insert_theme (icon_theme, themes[i]);
996 g_key_file_free (theme_file);
1000 free_unthemed_icon (UnthemedIcon *unthemed_icon)
1002 if (unthemed_icon->svg_filename)
1003 g_free (unthemed_icon->svg_filename);
1004 if (unthemed_icon->no_svg_filename)
1005 g_free (unthemed_icon->no_svg_filename);
1006 g_slice_free (UnthemedIcon, unthemed_icon);
1010 strip_suffix (const char *filename)
1014 dot = strrchr (filename, '.');
1017 return g_strdup (filename);
1019 return g_strndup (filename, dot - filename);
1023 load_themes (GtkIconTheme *icon_theme)
1025 GtkIconThemePrivate *priv;
1030 UnthemedIcon *unthemed_icon;
1031 IconSuffix old_suffix, new_suffix;
1033 IconThemeDirMtime *dir_mtime;
1034 struct stat stat_buf;
1036 priv = icon_theme->priv;
1038 priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
1040 if (priv->current_theme)
1041 insert_theme (icon_theme, priv->current_theme);
1043 /* Always look in the "default" icon theme, and in a fallback theme */
1044 if (priv->fallback_theme)
1045 insert_theme (icon_theme, priv->fallback_theme);
1046 insert_theme (icon_theme, DEFAULT_THEME_NAME);
1047 priv->themes = g_list_reverse (priv->themes);
1050 priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
1051 g_free, (GDestroyNotify)free_unthemed_icon);
1053 for (base = 0; base < icon_theme->priv->search_path_len; base++)
1055 dir = icon_theme->priv->search_path[base];
1057 dir_mtime = g_slice_new (IconThemeDirMtime);
1058 priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
1060 dir_mtime->dir = g_strdup (dir);
1061 dir_mtime->mtime = 0;
1062 dir_mtime->cache = NULL;
1064 if (g_stat (dir, &stat_buf) != 0 || !S_ISDIR (stat_buf.st_mode))
1066 dir_mtime->mtime = stat_buf.st_mtime;
1068 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir);
1069 if (dir_mtime->cache != NULL)
1072 gdir = g_dir_open (dir, 0, NULL);
1076 while ((file = g_dir_read_name (gdir)))
1078 new_suffix = suffix_from_name (file);
1080 if (new_suffix != ICON_SUFFIX_NONE)
1085 abs_file = g_build_filename (dir, file, NULL);
1086 base_name = strip_suffix (file);
1088 if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
1091 if (new_suffix == ICON_SUFFIX_SVG)
1093 if (unthemed_icon->svg_filename)
1096 unthemed_icon->svg_filename = abs_file;
1100 if (unthemed_icon->no_svg_filename)
1102 old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
1103 if (new_suffix > old_suffix)
1105 g_free (unthemed_icon->no_svg_filename);
1106 unthemed_icon->no_svg_filename = abs_file;
1112 unthemed_icon->no_svg_filename = abs_file;
1119 unthemed_icon = g_slice_new0 (UnthemedIcon);
1121 if (new_suffix == ICON_SUFFIX_SVG)
1122 unthemed_icon->svg_filename = abs_file;
1124 unthemed_icon->no_svg_filename = abs_file;
1126 g_hash_table_insert (priv->unthemed_icons,
1129 g_hash_table_insert (priv->all_icons,
1137 priv->themes_valid = TRUE;
1139 g_get_current_time(&tv);
1140 priv->last_stat_time = tv.tv_sec;
1144 _gtk_icon_theme_ensure_builtin_cache (void)
1146 static gboolean initialized = FALSE;
1148 static IconThemeDir dirs[5] =
1150 { ICON_THEME_DIR_THRESHOLD, 0, 16, 16, 16, 2, NULL, "16", NULL, NULL, NULL },
1151 { ICON_THEME_DIR_THRESHOLD, 0, 20, 20, 20, 2, NULL, "20", NULL, NULL, NULL },
1152 { ICON_THEME_DIR_THRESHOLD, 0, 24, 24, 24, 2, NULL, "24", NULL, NULL, NULL },
1153 { ICON_THEME_DIR_THRESHOLD, 0, 32, 32, 32, 2, NULL, "32", NULL, NULL, NULL },
1154 { ICON_THEME_DIR_THRESHOLD, 0, 48, 48, 48, 2, NULL, "48", NULL, NULL, NULL }
1162 _builtin_cache = _gtk_icon_cache_new ((gchar *)builtin_icons);
1164 for (i = 0; i < G_N_ELEMENTS (dirs); i++)
1167 dir->cache = _gtk_icon_cache_ref (_builtin_cache);
1169 builtin_dirs = g_list_append (builtin_dirs, dir);
1175 ensure_valid_themes (GtkIconTheme *icon_theme)
1177 GtkIconThemePrivate *priv = icon_theme->priv;
1179 gboolean was_valid = priv->themes_valid;
1181 _gtk_icon_theme_ensure_builtin_cache ();
1183 if (priv->themes_valid)
1185 g_get_current_time (&tv);
1187 if (ABS (tv.tv_sec - priv->last_stat_time) > 5)
1188 gtk_icon_theme_rescan_if_needed (icon_theme);
1191 if (!priv->themes_valid)
1193 load_themes (icon_theme);
1195 if (!priv->check_reload && was_valid && priv->screen)
1197 static GdkAtom atom_iconthemes = GDK_NONE;
1198 GdkEvent *event = gdk_event_new (GDK_CLIENT_EVENT);
1201 if (!atom_iconthemes)
1202 atom_iconthemes = gdk_atom_intern_static_string ("_GTK_LOAD_ICONTHEMES");
1204 for (i = 0; i < 5; i++)
1205 event->client.data.l[i] = 0;
1206 event->client.data_format = 32;
1207 event->client.message_type = atom_iconthemes;
1209 gdk_screen_broadcast_client_message (priv->screen, event);
1215 * gtk_icon_theme_lookup_icon:
1216 * @icon_theme: a #GtkIconTheme
1217 * @icon_name: the name of the icon to lookup
1218 * @size: desired icon size
1219 * @flags: flags modifying the behavior of the icon lookup
1221 * Looks up a named icon and returns a structure containing
1222 * information such as the filename of the icon. The icon
1223 * can then be rendered into a pixbuf using
1224 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1225 * combines these two steps if all you need is the pixbuf.)
1227 * Return value: a #GtkIconInfo structure containing information
1228 * about the icon, or %NULL if the icon wasn't found. Free with
1229 * gtk_icon_info_free()
1234 gtk_icon_theme_lookup_icon (GtkIconTheme *icon_theme,
1235 const gchar *icon_name,
1237 GtkIconLookupFlags flags)
1239 GtkIconThemePrivate *priv;
1241 GtkIconInfo *icon_info = NULL;
1242 UnthemedIcon *unthemed_icon;
1244 gboolean use_builtin;
1246 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1247 g_return_val_if_fail (icon_name != NULL, NULL);
1248 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1249 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1251 priv = icon_theme->priv;
1253 GTK_NOTE (ICONTHEME,
1254 g_print ("gtk_icon_theme_lookup_icon %s\n", icon_name));
1255 if (flags & GTK_ICON_LOOKUP_NO_SVG)
1257 else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
1260 allow_svg = priv->pixbuf_supports_svg;
1262 use_builtin = (flags & GTK_ICON_LOOKUP_USE_BUILTIN);
1264 ensure_valid_themes (icon_theme);
1266 for (l = priv->themes; l; l = l->next)
1268 IconTheme *theme = l->data;
1270 icon_info = theme_lookup_icon (theme, icon_name, size, allow_svg, use_builtin);
1275 unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_name);
1278 icon_info = icon_info_new ();
1280 /* A SVG icon, when allowed, beats out a XPM icon, but not
1284 unthemed_icon->svg_filename &&
1285 (!unthemed_icon->no_svg_filename ||
1286 suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
1287 icon_info->filename = g_strdup (unthemed_icon->svg_filename);
1288 else if (unthemed_icon->no_svg_filename)
1289 icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);
1291 icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
1292 -1, NULL, NULL, NULL);
1295 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1300 icon_info->desired_size = size;
1303 static gboolean check_for_default_theme = TRUE;
1304 char *default_theme_path;
1305 gboolean found = FALSE;
1308 if (check_for_default_theme)
1310 check_for_default_theme = FALSE;
1312 for (i = 0; !found && i < priv->search_path_len; i++)
1314 default_theme_path = g_build_filename (priv->search_path[i],
1318 found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
1319 g_free (default_theme_path);
1323 g_warning (_("Could not find the icon '%s'. The '%s' theme\n"
1324 "was not found either, perhaps you need to install it.\n"
1325 "You can get a copy from:\n"
1327 icon_name, DEFAULT_THEME_NAME, "http://icon-theme.freedesktop.org/releases");
1337 gtk_icon_theme_error_quark (void)
1339 return g_quark_from_static_string ("gtk-icon-theme-error-quark");
1343 * gtk_icon_theme_load_icon:
1344 * @icon_theme: a #GtkIconTheme
1345 * @icon_name: the name of the icon to lookup
1346 * @size: the desired icon size. The resulting icon may not be
1347 * exactly this size; see gtk_icon_info_load_icon().
1348 * @flags: flags modifying the behavior of the icon lookup
1349 * @error: Location to store error information on failure, or %NULL.
1351 * Looks up an icon in an icon theme, scales it to the given size
1352 * and renders it into a pixbuf. This is a convenience function;
1353 * if more details about the icon are needed, use
1354 * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1356 * Note that you probably want to listen for icon theme changes and
1357 * update the icon. This is usually done by connecting to the
1358 * GtkWidget::style-set signal. If for some reason you do not want to
1359 * update the icon when the icon theme changes, you should consider
1360 * using gdk_pixbuf_copy() to make a private copy of the pixbuf
1361 * returned by this function. Otherwise GTK+ may need to keep the old
1362 * icon theme loaded, which would be a waste of memory.
1364 * Return value: the rendered icon; this may be a newly created icon
1365 * or a new reference to an internal icon, so you must not modify
1366 * the icon. Use g_object_unref() to release your reference to the
1367 * icon. %NULL if the icon isn't found.
1372 gtk_icon_theme_load_icon (GtkIconTheme *icon_theme,
1373 const gchar *icon_name,
1375 GtkIconLookupFlags flags,
1378 GtkIconInfo *icon_info;
1379 GdkPixbuf *pixbuf = NULL;
1381 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1382 g_return_val_if_fail (icon_name != NULL, NULL);
1383 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1384 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1385 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1387 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
1388 flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1391 g_set_error (error, GTK_ICON_THEME_ERROR, GTK_ICON_THEME_NOT_FOUND,
1392 _("Icon '%s' not present in theme"), icon_name);
1396 pixbuf = gtk_icon_info_load_icon (icon_info, error);
1397 gtk_icon_info_free (icon_info);
1403 * gtk_icon_theme_has_icon:
1404 * @icon_theme: a #GtkIconTheme
1405 * @icon_name: the name of an icon
1407 * Checks whether an icon theme includes an icon
1408 * for a particular name.
1410 * Return value: %TRUE if @icon_theme includes an
1411 * icon for @icon_name.
1416 gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
1417 const char *icon_name)
1419 GtkIconThemePrivate *priv;
1422 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1424 priv = icon_theme->priv;
1426 ensure_valid_themes (icon_theme);
1428 for (l = priv->dir_mtimes; l; l = l->next)
1430 IconThemeDirMtime *dir_mtime = l->data;
1431 GtkIconCache *cache = dir_mtime->cache;
1433 if (cache && _gtk_icon_cache_has_icon (cache, icon_name))
1437 if (g_hash_table_lookup_extended (priv->all_icons,
1438 icon_name, NULL, NULL))
1441 if (_builtin_cache &&
1442 _gtk_icon_cache_has_icon (_builtin_cache, icon_name))
1445 if (icon_theme_builtin_icons &&
1446 g_hash_table_lookup_extended (icon_theme_builtin_icons,
1447 icon_name, NULL, NULL))
1454 add_size (gpointer key,
1458 gint **res_p = user_data;
1460 **res_p = GPOINTER_TO_INT (key);
1466 * gtk_icon_theme_get_icon_sizes:
1467 * @icon_theme: a #GtkIconTheme
1468 * @icon_name: the name of an icon
1470 * Returns an array of integers describing the sizes at which
1471 * the icon is available without scaling. A size of -1 means
1472 * that the icon is available in a scalable format. The array
1473 * is zero-terminated.
1475 * Return value: An newly allocated array describing the sizes at
1476 * which the icon is available. The array should be freed with g_free()
1477 * when it is no longer needed.
1482 gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
1483 const char *icon_name)
1485 GList *l, *d, *icons;
1489 GtkIconThemePrivate *priv;
1491 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1493 priv = icon_theme->priv;
1495 ensure_valid_themes (icon_theme);
1497 sizes = g_hash_table_new (g_direct_hash, g_direct_equal);
1499 for (l = priv->themes; l; l = l->next)
1501 IconTheme *theme = l->data;
1502 for (d = theme->dirs; d; d = d->next)
1504 IconThemeDir *dir = d->data;
1506 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1507 if (suffix != ICON_SUFFIX_NONE)
1509 if (suffix == ICON_SUFFIX_SVG)
1510 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1512 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1517 for (d = builtin_dirs; d; d = d->next)
1519 IconThemeDir *dir = d->data;
1521 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1522 if (suffix != ICON_SUFFIX_NONE)
1524 if (suffix == ICON_SUFFIX_SVG)
1525 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1527 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1531 if (icon_theme_builtin_icons)
1533 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
1537 BuiltinIcon *icon = icons->data;
1539 g_hash_table_insert (sizes, GINT_TO_POINTER (icon->size), NULL);
1540 icons = icons->next;
1544 r = result = g_new0 (gint, g_hash_table_size (sizes) + 1);
1546 g_hash_table_foreach (sizes, add_size, &r);
1547 g_hash_table_destroy (sizes);
1553 add_key_to_hash (gpointer key,
1557 GHashTable *hash = user_data;
1559 g_hash_table_insert (hash, key, NULL);
1563 add_key_to_list (gpointer key,
1567 GList **list = user_data;
1569 *list = g_list_prepend (*list, g_strdup (key));
1573 * gtk_icon_theme_list_icons:
1574 * @icon_theme: a #GtkIconTheme
1575 * @context: a string identifying a particular type of icon,
1576 * or %NULL to list all icons.
1578 * Lists the icons in the current icon theme. Only a subset
1579 * of the icons can be listed by providing a context string.
1580 * The set of values for the context string is system dependent,
1581 * but will typically include such values as "Applications" and
1584 * Return value: a #GList list holding the names of all the
1585 * icons in the theme. You must first free each element
1586 * in the list with g_free(), then free the list itself
1587 * with g_list_free().
1592 gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
1593 const char *context)
1595 GtkIconThemePrivate *priv;
1598 GQuark context_quark;
1600 priv = icon_theme->priv;
1602 ensure_valid_themes (icon_theme);
1606 context_quark = g_quark_try_string (context);
1614 icons = g_hash_table_new (g_str_hash, g_str_equal);
1619 theme_list_icons (l->data, icons, context_quark);
1623 if (context_quark == 0)
1624 g_hash_table_foreach (priv->unthemed_icons,
1630 g_hash_table_foreach (icons,
1634 g_hash_table_destroy (icons);
1640 * gtk_icon_theme_get_example_icon_name:
1641 * @icon_theme: a #GtkIconTheme
1643 * Gets the name of an icon that is representative of the
1644 * current theme (for instance, to use when presenting
1645 * a list of themes to the user.)
1647 * Return value: the name of an example icon or %NULL.
1648 * Free with g_free().
1653 gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
1655 GtkIconThemePrivate *priv;
1659 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1661 priv = icon_theme->priv;
1663 ensure_valid_themes (icon_theme);
1670 return g_strdup (theme->example);
1679 * gtk_icon_theme_rescan_if_needed:
1680 * @icon_theme: a #GtkIconTheme
1682 * Checks to see if the icon theme has changed; if it has, any
1683 * currently cached information is discarded and will be reloaded
1684 * next time @icon_theme is accessed.
1686 * Return value: %TRUE if the icon theme has changed and needed
1692 gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
1694 GtkIconThemePrivate *priv;
1695 IconThemeDirMtime *dir_mtime;
1698 struct stat stat_buf;
1701 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1703 priv = icon_theme->priv;
1705 for (d = priv->dir_mtimes; d != NULL; d = d->next)
1707 dir_mtime = d->data;
1709 stat_res = g_stat (dir_mtime->dir, &stat_buf);
1711 /* dir mtime didn't change */
1712 if (stat_res == 0 &&
1713 S_ISDIR (stat_buf.st_mode) &&
1714 dir_mtime->mtime == stat_buf.st_mtime)
1716 /* didn't exist before, and still doesn't */
1717 if (dir_mtime->mtime == 0 &&
1718 (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
1721 do_theme_change (icon_theme);
1725 g_get_current_time (&tv);
1726 priv->last_stat_time = tv.tv_sec;
1732 theme_destroy (IconTheme *theme)
1734 g_free (theme->display_name);
1735 g_free (theme->comment);
1736 g_free (theme->name);
1737 g_free (theme->example);
1739 g_list_foreach (theme->dirs, (GFunc)theme_dir_destroy, NULL);
1740 g_list_free (theme->dirs);
1746 theme_dir_destroy (IconThemeDir *dir)
1749 _gtk_icon_cache_unref (dir->cache);
1751 g_hash_table_destroy (dir->icons);
1754 g_hash_table_destroy (dir->icon_data);
1756 g_free (dir->subdir);
1761 theme_dir_size_difference (IconThemeDir *dir, int size, gboolean *smaller)
1766 case ICON_THEME_DIR_FIXED:
1767 *smaller = size < dir->size;
1768 return abs (size - dir->size);
1770 case ICON_THEME_DIR_SCALABLE:
1771 *smaller = size < dir->min_size;
1772 if (size < dir->min_size)
1773 return dir->min_size - size;
1774 if (size > dir->max_size)
1775 return size - dir->max_size;
1778 case ICON_THEME_DIR_THRESHOLD:
1779 min = dir->size - dir->threshold;
1780 max = dir->size + dir->threshold;
1781 *smaller = size < min;
1788 case ICON_THEME_DIR_UNTHEMED:
1789 g_assert_not_reached ();
1792 g_assert_not_reached ();
1797 string_from_suffix (IconSuffix suffix)
1801 case ICON_SUFFIX_XPM:
1803 case ICON_SUFFIX_SVG:
1805 case ICON_SUFFIX_PNG:
1808 g_assert_not_reached();
1814 suffix_from_name (const char *name)
1818 if (g_str_has_suffix (name, ".png"))
1819 retval = ICON_SUFFIX_PNG;
1820 else if (g_str_has_suffix (name, ".svg"))
1821 retval = ICON_SUFFIX_SVG;
1822 else if (g_str_has_suffix (name, ".xpm"))
1823 retval = ICON_SUFFIX_XPM;
1825 retval = ICON_SUFFIX_NONE;
1831 best_suffix (IconSuffix suffix,
1834 if ((suffix & ICON_SUFFIX_PNG) != 0)
1835 return ICON_SUFFIX_PNG;
1836 else if (allow_svg && ((suffix & ICON_SUFFIX_SVG) != 0))
1837 return ICON_SUFFIX_SVG;
1838 else if ((suffix & ICON_SUFFIX_XPM) != 0)
1839 return ICON_SUFFIX_XPM;
1841 return ICON_SUFFIX_NONE;
1846 theme_dir_get_icon_suffix (IconThemeDir *dir,
1847 const gchar *icon_name,
1848 gboolean *has_icon_file)
1854 suffix = (IconSuffix)_gtk_icon_cache_get_icon_flags (dir->cache,
1859 *has_icon_file = suffix & HAS_ICON_FILE;
1861 suffix = suffix & ~HAS_ICON_FILE;
1864 suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
1866 GTK_NOTE (ICONTHEME,
1867 g_print ("get_icon_suffix%s %u\n", dir->cache ? " (cached)" : "", suffix));
1872 static GtkIconInfo *
1873 theme_lookup_icon (IconTheme *theme,
1874 const char *icon_name,
1877 gboolean use_builtin)
1880 IconThemeDir *dir, *min_dir;
1882 int min_difference, difference;
1883 BuiltinIcon *closest_builtin = NULL;
1884 gboolean smaller, has_larger, match;
1887 min_difference = G_MAXINT;
1892 /* Builtin icons are logically part of the default theme and
1893 * are searched before other subdirectories of the default theme.
1895 if (strcmp (theme->name, DEFAULT_THEME_NAME) == 0 && use_builtin)
1897 closest_builtin = find_builtin_icon (icon_name,
1902 if (min_difference == 0)
1903 return icon_info_new_builtin (closest_builtin);
1905 dirs = builtin_dirs;
1915 GTK_NOTE (ICONTHEME,
1916 g_print ("theme_lookup_icon dir %s\n", dir->dir));
1917 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1918 if (best_suffix (suffix, allow_svg) != ICON_SUFFIX_NONE)
1920 difference = theme_dir_size_difference (dir, size, &smaller);
1922 if (difference == 0)
1924 if (dir->type == ICON_THEME_DIR_SCALABLE)
1926 /* don't pick scalable if we already found
1927 * a matching non-scalable dir
1937 /* for a matching non-scalable dir keep
1938 * going and look for a closer match
1940 difference = abs (size - dir->size);
1941 if (!match || difference < min_difference)
1944 min_difference = difference;
1947 if (difference == 0)
1956 if (difference < min_difference || smaller)
1958 min_difference = difference;
1960 has_larger = smaller;
1965 if (difference < min_difference && smaller)
1967 min_difference = difference;
1976 if (l == NULL && dirs == builtin_dirs)
1985 GtkIconInfo *icon_info = icon_info_new ();
1986 gboolean has_icon_file = FALSE;
1988 suffix = theme_dir_get_icon_suffix (min_dir, icon_name, &has_icon_file);
1989 suffix = best_suffix (suffix, allow_svg);
1990 g_assert (suffix != ICON_SUFFIX_NONE);
1992 file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
1993 icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
1996 icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
1997 -1, NULL, NULL, NULL);
2000 if (min_dir->icon_data != NULL)
2001 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2003 if (icon_info->data == NULL && min_dir->cache != NULL)
2005 icon_info->data = _gtk_icon_cache_get_icon_data (min_dir->cache, icon_name, min_dir->subdir);
2006 if (icon_info->data)
2008 if (min_dir->icon_data == NULL)
2009 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2010 g_free, (GDestroyNotify)icon_data_free);
2012 g_hash_table_replace (min_dir->icon_data, g_strdup (icon_name), icon_info->data);
2016 if (icon_info->data == NULL && has_icon_file)
2018 gchar *icon_file_name, *icon_file_path;
2020 icon_file_name = g_strconcat (icon_name, ".icon", NULL);
2021 icon_file_path = g_build_filename (min_dir->dir, icon_file_name, NULL);
2023 if (g_file_test (icon_file_path, G_FILE_TEST_IS_REGULAR))
2025 if (min_dir->icon_data == NULL)
2026 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2027 g_free, (GDestroyNotify)icon_data_free);
2028 load_icon_data (min_dir, icon_file_path, icon_file_name);
2030 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2032 g_free (icon_file_name);
2033 g_free (icon_file_path);
2038 icon_info->cache_pixbuf = _gtk_icon_cache_get_icon (min_dir->cache, icon_name,
2042 icon_info->dir_type = min_dir->type;
2043 icon_info->dir_size = min_dir->size;
2044 icon_info->threshold = min_dir->threshold;
2049 if (closest_builtin)
2050 return icon_info_new_builtin (closest_builtin);
2056 theme_list_icons (IconTheme *theme,
2060 GList *l = theme->dirs;
2067 if (context == dir->context ||
2072 _gtk_icon_cache_add_icons (dir->cache,
2079 g_hash_table_foreach (dir->icons,
2089 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
2091 GKeyFile *icon_file;
2099 GError *error = NULL;
2103 icon_file = g_key_file_new ();
2104 g_key_file_set_list_separator (icon_file, ',');
2105 g_key_file_load_from_file (icon_file, path, 0, &error);
2108 g_error_free (error);
2109 g_key_file_free (icon_file);
2114 base_name = strip_suffix (name);
2116 data = g_slice_new0 (GtkIconData);
2117 g_hash_table_replace (dir->icon_data, base_name, data);
2119 ivalues = g_key_file_get_integer_list (icon_file,
2120 "Icon Data", "EmbeddedTextRectangle",
2126 data->has_embedded_rect = TRUE;
2127 data->x0 = ivalues[0];
2128 data->y0 = ivalues[1];
2129 data->x1 = ivalues[2];
2130 data->y1 = ivalues[3];
2136 str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
2139 split = g_strsplit (str, "|", -1);
2141 data->n_attach_points = g_strv_length (split);
2142 data->attach_points = g_new (GdkPoint, data->n_attach_points);
2145 while (split[i] != NULL && i < data->n_attach_points)
2147 split_point = strchr (split[i], ',');
2152 data->attach_points[i].x = atoi (split[i]);
2153 data->attach_points[i].y = atoi (split_point);
2162 data->display_name = g_key_file_get_locale_string (icon_file,
2163 "Icon Data", "DisplayName",
2165 g_key_file_free (icon_file);
2170 scan_directory (GtkIconThemePrivate *icon_theme,
2171 IconThemeDir *dir, char *full_dir)
2176 GTK_NOTE (ICONTHEME,
2177 g_print ("scanning directory %s\n", full_dir));
2178 dir->icons = g_hash_table_new_full (g_str_hash, g_str_equal,
2181 gdir = g_dir_open (full_dir, 0, NULL);
2186 while ((name = g_dir_read_name (gdir)))
2190 IconSuffix suffix, hash_suffix;
2192 if (g_str_has_suffix (name, ".icon"))
2194 if (dir->icon_data == NULL)
2195 dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2196 g_free, (GDestroyNotify)icon_data_free);
2198 path = g_build_filename (full_dir, name, NULL);
2199 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
2200 load_icon_data (dir, path, name);
2207 suffix = suffix_from_name (name);
2208 if (suffix == ICON_SUFFIX_NONE)
2211 base_name = strip_suffix (name);
2213 hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
2214 g_hash_table_insert (icon_theme->all_icons, base_name, NULL);
2215 g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
2222 theme_subdir_load (GtkIconTheme *icon_theme,
2224 GKeyFile *theme_file,
2230 IconThemeDirType type;
2231 char *context_string;
2238 GError *error = NULL;
2239 IconThemeDirMtime *dir_mtime;
2241 size = g_key_file_get_integer (theme_file, subdir, "Size", &error);
2244 g_error_free (error);
2245 g_warning ("Theme directory %s of theme %s has no size field\n",
2246 subdir, theme->name);
2250 type = ICON_THEME_DIR_THRESHOLD;
2251 type_string = g_key_file_get_string (theme_file, subdir, "Type", NULL);
2254 if (strcmp (type_string, "Fixed") == 0)
2255 type = ICON_THEME_DIR_FIXED;
2256 else if (strcmp (type_string, "Scalable") == 0)
2257 type = ICON_THEME_DIR_SCALABLE;
2258 else if (strcmp (type_string, "Threshold") == 0)
2259 type = ICON_THEME_DIR_THRESHOLD;
2261 g_free (type_string);
2265 context_string = g_key_file_get_string (theme_file, subdir, "Context", NULL);
2268 context = g_quark_from_string (context_string);
2269 g_free (context_string);
2272 max_size = g_key_file_get_integer (theme_file, subdir, "MaxSize", &error);
2277 g_error_free (error);
2281 min_size = g_key_file_get_integer (theme_file, subdir, "MinSize", &error);
2286 g_error_free (error);
2290 threshold = g_key_file_get_integer (theme_file, subdir, "Threshold", &error);
2295 g_error_free (error);
2299 for (d = icon_theme->priv->dir_mtimes; d; d = d->next)
2301 dir_mtime = (IconThemeDirMtime *)d->data;
2303 if (dir_mtime->mtime == 0)
2304 continue; /* directory doesn't exist */
2306 full_dir = g_build_filename (dir_mtime->dir, subdir, NULL);
2308 /* First, see if we have a cache for the directory */
2309 if (dir_mtime->cache != NULL || g_file_test (full_dir, G_FILE_TEST_IS_DIR))
2311 if (dir_mtime->cache == NULL)
2313 /* This will return NULL if the cache doesn't exist or is outdated */
2314 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir_mtime->dir);
2317 dir = g_new (IconThemeDir, 1);
2319 dir->context = context;
2321 dir->min_size = min_size;
2322 dir->max_size = max_size;
2323 dir->threshold = threshold;
2324 dir->dir = full_dir;
2325 dir->icon_data = NULL;
2326 dir->subdir = g_strdup (subdir);
2327 if (dir_mtime->cache != NULL)
2328 dir->cache = _gtk_icon_cache_ref (dir_mtime->cache);
2332 scan_directory (icon_theme->priv, dir, full_dir);
2335 theme->dirs = g_list_prepend (theme->dirs, dir);
2343 icon_data_free (GtkIconData *icon_data)
2345 g_free (icon_data->attach_points);
2346 g_free (icon_data->display_name);
2347 g_slice_free (GtkIconData, icon_data);
2354 gtk_icon_info_get_type (void)
2356 static GType our_type = 0;
2359 our_type = g_boxed_type_register_static (I_("GtkIconInfo"),
2360 (GBoxedCopyFunc) gtk_icon_info_copy,
2361 (GBoxedFreeFunc) gtk_icon_info_free);
2367 static GtkIconInfo *
2368 icon_info_new (void)
2370 GtkIconInfo *icon_info = g_slice_new0 (GtkIconInfo);
2372 icon_info->scale = -1.;
2377 static GtkIconInfo *
2378 icon_info_new_builtin (BuiltinIcon *icon)
2380 GtkIconInfo *icon_info = icon_info_new ();
2382 icon_info->cache_pixbuf = g_object_ref (icon->pixbuf);
2383 icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
2384 icon_info->dir_size = icon->size;
2385 icon_info->threshold = 2;
2391 * gtk_icon_info_copy:
2392 * @icon_info: a #GtkIconInfo
2394 * Make a copy of a #GtkIconInfo.
2396 * Return value: the new GtkIconInfo
2401 gtk_icon_info_copy (GtkIconInfo *icon_info)
2405 g_return_val_if_fail (icon_info != NULL, NULL);
2407 copy = memcpy (g_slice_new (GtkIconInfo), icon_info, sizeof (GtkIconInfo));
2408 if (copy->cache_pixbuf)
2409 g_object_ref (copy->cache_pixbuf);
2411 g_object_ref (copy->pixbuf);
2412 if (copy->load_error)
2413 copy->load_error = g_error_copy (copy->load_error);
2415 copy->filename = g_strdup (copy->filename);
2417 if (copy->cp_filename)
2418 copy->cp_filename = g_strdup (copy->cp_filename);
2425 * gtk_icon_info_free:
2426 * @icon_info: a #GtkIconInfo
2428 * Free a #GtkIconInfo and associated information
2433 gtk_icon_info_free (GtkIconInfo *icon_info)
2435 g_return_if_fail (icon_info != NULL);
2437 if (icon_info->filename)
2438 g_free (icon_info->filename);
2440 if (icon_info->cp_filename)
2441 g_free (icon_info->cp_filename);
2443 if (icon_info->pixbuf)
2444 g_object_unref (icon_info->pixbuf);
2445 if (icon_info->cache_pixbuf)
2446 g_object_unref (icon_info->cache_pixbuf);
2448 g_slice_free (GtkIconInfo, icon_info);
2452 * gtk_icon_info_get_base_size:
2453 * @icon_info: a #GtkIconInfo
2455 * Gets the base size for the icon. The base size
2456 * is a size for the icon that was specified by
2457 * the icon theme creator. This may be different
2458 * than the actual size of image; an example of
2459 * this is small emblem icons that can be attached
2460 * to a larger icon. These icons will be given
2461 * the same base size as the larger icons to which
2462 * they are attached.
2464 * Return value: the base size, or 0, if no base
2465 * size is known for the icon.
2470 gtk_icon_info_get_base_size (GtkIconInfo *icon_info)
2472 g_return_val_if_fail (icon_info != NULL, 0);
2474 return icon_info->dir_size;
2478 * gtk_icon_info_get_filename:
2479 * @icon_info: a #GtkIconInfo
2481 * Gets the filename for the icon. If the
2482 * %GTK_ICON_LOOKUP_USE_BUILTIN flag was passed
2483 * to gtk_icon_theme_lookup_icon(), there may be
2484 * no filename if a builtin icon is returned; in this
2485 * case, you should use gtk_icon_info_get_builtin_pixbuf().
2487 * Return value: the filename for the icon, or %NULL
2488 * if gtk_icon_info_get_builtin_pixbuf() should
2489 * be used instead. The return value is owned by
2490 * GTK+ and should not be modified or freed.
2494 G_CONST_RETURN gchar *
2495 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2497 g_return_val_if_fail (icon_info != NULL, NULL);
2499 return icon_info->filename;
2503 * gtk_icon_info_get_builtin_pixbuf:
2504 * @icon_info: a #GtkIconInfo structure
2506 * Gets the built-in image for this icon, if any. To allow
2507 * GTK+ to use built in icon images, you must pass the
2508 * %GTK_ICON_LOOKUP_USE_BUILTIN to
2509 * gtk_icon_theme_lookup_icon().
2511 * Return value: the built-in image pixbuf, or %NULL. No
2512 * extra reference is added to the returned pixbuf, so if
2513 * you want to keep it around, you must use g_object_ref().
2514 * The returned image must not be modified.
2519 gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
2521 g_return_val_if_fail (icon_info != NULL, NULL);
2523 if (icon_info->filename)
2526 return icon_info->cache_pixbuf;
2530 load_svg_at_size (const gchar *filename,
2534 GdkPixbuf *pixbuf = NULL;
2535 GdkPixbufLoader *loader = NULL;
2536 gchar *contents = NULL;
2539 if (!g_file_get_contents (filename,
2540 &contents, &length, error))
2543 loader = gdk_pixbuf_loader_new_with_type ("svg", error);
2547 gdk_pixbuf_loader_set_size (loader, size, size);
2549 if (!gdk_pixbuf_loader_write (loader, contents, length, error))
2551 gdk_pixbuf_loader_close (loader, NULL);
2555 if (!gdk_pixbuf_loader_close (loader, error))
2558 pixbuf = g_object_ref (gdk_pixbuf_loader_get_pixbuf (loader));
2562 g_object_unref (loader);
2569 /* This function contains the complicatd logic for deciding
2570 * on the size at which to load the icon and loading it at
2574 icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
2575 gboolean scale_only)
2577 int image_width, image_height;
2578 GdkPixbuf *source_pixbuf;
2580 /* First check if we already succeeded have the necessary
2581 * information (or failed earlier)
2583 if (scale_only && icon_info->scale >= 0)
2586 if (icon_info->pixbuf)
2589 if (icon_info->load_error)
2592 /* SVG icons are a special case - we just immediately scale them
2593 * to the desired size
2595 if (icon_info->filename && g_str_has_suffix (icon_info->filename, ".svg"))
2597 icon_info->scale = icon_info->desired_size / 1000.;
2602 icon_info->pixbuf = load_svg_at_size (icon_info->filename,
2603 icon_info->desired_size,
2604 &icon_info->load_error);
2606 return icon_info->pixbuf != NULL;
2609 /* In many cases, the scale can be determined without actual access
2610 * to the icon file. This is generally true when we have a size
2611 * for the directory where the icon is; the image size doesn't
2612 * matter in that case.
2614 if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
2615 icon_info->scale = 1.0;
2616 else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
2618 if (icon_info->desired_size >= icon_info->dir_size - icon_info->threshold &&
2619 icon_info->desired_size <= icon_info->dir_size + icon_info->threshold)
2620 icon_info->scale = 1.0;
2621 else if (icon_info->dir_size > 0)
2622 icon_info->scale =(gdouble) icon_info->desired_size / icon_info->dir_size;
2624 else if (icon_info->dir_type == ICON_THEME_DIR_SCALABLE)
2626 if (icon_info->dir_size > 0)
2627 icon_info->scale = (gdouble) icon_info->desired_size / icon_info->dir_size;
2630 if (icon_info->scale >= 0. && scale_only)
2633 /* At this point, we need to actually get the icon; either from the
2634 * builting image or by loading the file
2636 if (icon_info->cache_pixbuf)
2637 source_pixbuf = g_object_ref (icon_info->cache_pixbuf);
2641 source_pixbuf = gdk_pixbuf_new_from_file (icon_info->filename,
2642 &icon_info->load_error);
2647 /* Do scale calculations that depend on the image size
2649 image_width = gdk_pixbuf_get_width (source_pixbuf);
2650 image_height = gdk_pixbuf_get_height (source_pixbuf);
2652 if (icon_info->scale < 0.0)
2654 gint image_size = MAX (image_width, image_height);
2656 icon_info->scale = icon_info->desired_size / (gdouble)image_size;
2658 icon_info->scale = 1.0;
2660 if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED)
2661 icon_info->scale = MIN (icon_info->scale, 1.0);
2664 /* We don't short-circuit out here for scale_only, since, now
2665 * we've loaded the icon, we might as well go ahead and finish
2666 * the job. This is a bit of a waste when we scale here
2667 * and never get the final pixbuf; at the cost of a bit of
2668 * extra complexity, we could keep the source pixbuf around
2669 * but not actually scale it until neede.
2672 if (icon_info->scale == 1.0)
2673 icon_info->pixbuf = source_pixbuf;
2676 icon_info->pixbuf = gdk_pixbuf_scale_simple (source_pixbuf,
2677 0.5 + image_width * icon_info->scale,
2678 0.5 + image_height * icon_info->scale,
2679 GDK_INTERP_BILINEAR);
2680 g_object_unref (source_pixbuf);
2687 * gtk_icon_info_load_icon:
2688 * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
2689 * @error: location to store error information on failure, or %NULL.
2691 * Renders an icon previously looked up in an icon theme using
2692 * gtk_icon_theme_lookup_icon(); the size will be based on the size
2693 * passed to gtk_icon_theme_lookup_icon(). Note that the resulting
2694 * pixbuf may not be exactly this size; an icon theme may have icons
2695 * that differ slightly from their nominal sizes, and in addition GTK+
2696 * will avoid scaling icons that it considers sufficiently close to the
2697 * requested size or for which the source image would have to be scaled
2698 * up too far. (This maintains sharpness.)
2700 * Return value: the rendered icon; this may be a newly created icon
2701 * or a new reference to an internal icon, so you must not modify
2702 * the icon. Use g_object_unref() to release your reference to the
2708 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
2711 g_return_val_if_fail (icon_info != NULL, NULL);
2713 g_return_val_if_fail (icon_info != NULL, NULL);
2714 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2716 icon_info_ensure_scale_and_pixbuf (icon_info, FALSE);
2718 if (icon_info->load_error)
2720 g_propagate_error (error, icon_info->load_error);
2724 return g_object_ref (icon_info->pixbuf);
2728 * gtk_icon_info_set_raw_coordinates:
2729 * @icon_info: a #GtkIconInfo
2730 * @raw_coordinates: whether the coordinates of embedded rectangles
2731 * and attached points should be returned in their original
2734 * Sets whether the coordinates returned by gtk_icon_info_get_embedded_rect()
2735 * and gtk_icon_info_get_attach_points() should be returned in their
2736 * original form as specified in the icon theme, instead of scaled
2737 * appropriately for the pixbuf returned by gtk_icon_info_load_icon().
2739 * Raw coordinates are somewhat strange; they are specified to be with
2740 * respect to the unscaled pixmap for PNG and XPM icons, but for SVG
2741 * icons, they are in a 1000x1000 coordinate space that is scaled
2742 * to the final size of the icon. You can determine if the icon is an SVG
2743 * icon by using gtk_icon_info_get_filename(), and seeing if it is non-%NULL
2744 * and ends in '.svg'.
2746 * This function is provided primarily to allow compatibility wrappers
2747 * for older API's, and is not expected to be useful for applications.
2752 gtk_icon_info_set_raw_coordinates (GtkIconInfo *icon_info,
2753 gboolean raw_coordinates)
2755 g_return_if_fail (icon_info != NULL);
2757 icon_info->raw_coordinates = raw_coordinates != FALSE;
2760 /* Scale coordinates from the icon data prior to returning
2764 icon_info_scale_point (GtkIconInfo *icon_info,
2770 if (icon_info->raw_coordinates)
2777 if (!icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2780 *x_out = 0.5 + x * icon_info->scale;
2781 *y_out = 0.5 + y * icon_info->scale;
2788 * gtk_icon_info_get_embedded_rect:
2789 * @icon_info: a #GtkIconInfo
2790 * @rectangle: #GdkRectangle in which to store embedded
2791 * rectangle coordinates; coordinates are only stored
2792 * when this function returns %TRUE.
2794 * Gets the coordinates of a rectangle within the icon
2795 * that can be used for display of information such
2796 * as a preview of the contents of a text file.
2797 * See gtk_icon_info_set_raw_coordinates() for further
2798 * information about the coordinate system.
2800 * Return value: %TRUE if the icon has an embedded rectangle
2805 gtk_icon_info_get_embedded_rect (GtkIconInfo *icon_info,
2806 GdkRectangle *rectangle)
2808 g_return_val_if_fail (icon_info != NULL, FALSE);
2810 if (icon_info->data && icon_info->data->has_embedded_rect &&
2811 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2813 gint scaled_x0, scaled_y0;
2814 gint scaled_x1, scaled_y1;
2818 icon_info_scale_point (icon_info,
2819 icon_info->data->x0, icon_info->data->y0,
2820 &scaled_x0, &scaled_y0);
2821 icon_info_scale_point (icon_info,
2822 icon_info->data->x1, icon_info->data->y1,
2823 &scaled_x1, &scaled_y1);
2825 rectangle->x = scaled_x0;
2826 rectangle->y = scaled_y0;
2827 rectangle->width = scaled_x1 - rectangle->x;
2828 rectangle->height = scaled_y1 - rectangle->y;
2838 * gtk_icon_info_get_attach_points:
2839 * @icon_info: a #GtkIconInfo
2840 * @points: location to store pointer to an array of points, or %NULL
2841 * free the array of points with g_free().
2842 * @n_points: location to store the number of points in @points, or %NULL
2844 * Fetches the set of attach points for an icon. An attach point
2845 * is a location in the icon that can be used as anchor points for attaching
2846 * emblems or overlays to the icon.
2848 * Return value: %TRUE if there are any attach points for the icon.
2853 gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
2857 g_return_val_if_fail (icon_info != NULL, FALSE);
2859 if (icon_info->data && icon_info->data->n_attach_points &&
2860 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2866 *points = g_new (GdkPoint, icon_info->data->n_attach_points);
2867 for (i = 0; i < icon_info->data->n_attach_points; i++)
2868 icon_info_scale_point (icon_info,
2869 icon_info->data->attach_points[i].x,
2870 icon_info->data->attach_points[i].y,
2876 *n_points = icon_info->data->n_attach_points;
2892 * gtk_icon_info_get_display_name:
2893 * @icon_info: a #GtkIconInfo
2895 * Gets the display name for an icon. A display name is a
2896 * string to be used in place of the icon name in a user
2897 * visible context like a list of icons.
2899 * Return value: the display name for the icon or %NULL, if
2900 * the icon doesn't have a specified display name. This value
2901 * is owned @icon_info and must not be modified or free.
2905 G_CONST_RETURN gchar *
2906 gtk_icon_info_get_display_name (GtkIconInfo *icon_info)
2908 g_return_val_if_fail (icon_info != NULL, NULL);
2910 if (icon_info->data)
2911 return icon_info->data->display_name;
2922 * gtk_icon_theme_add_builtin_icon:
2923 * @icon_name: the name of the icon to register
2924 * @size: the size at which to register the icon (different
2925 * images can be registered for the same icon name
2926 * at different sizes.)
2927 * @pixbuf: #GdkPixbuf that contains the image to use
2930 * Registers a built-in icon for icon theme lookups. The idea
2931 * of built-in icons is to allow an application or library
2932 * that uses themed icons to function requiring files to
2933 * be present in the file system. For instance, the default
2934 * images for all of GTK+'s stock icons are registered
2937 * In general, if you use gtk_icon_theme_add_builtin_icon()
2938 * you should also install the icon in the icon theme, so
2939 * that the icon is generally available.
2941 * This function will generally be used with pixbufs loaded
2942 * via gdk_pixbuf_new_from_inline().
2947 gtk_icon_theme_add_builtin_icon (const gchar *icon_name,
2951 BuiltinIcon *default_icon;
2955 g_return_if_fail (icon_name != NULL);
2956 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2958 if (!icon_theme_builtin_icons)
2959 icon_theme_builtin_icons = g_hash_table_new (g_str_hash, g_str_equal);
2961 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
2963 key = g_strdup (icon_name);
2965 key = (gpointer)icon_name; /* Won't get stored */
2967 default_icon = g_new (BuiltinIcon, 1);
2968 default_icon->size = size;
2969 default_icon->pixbuf = g_object_ref (pixbuf);
2970 icons = g_slist_prepend (icons, default_icon);
2972 /* Replaces value, leaves key untouched
2974 g_hash_table_insert (icon_theme_builtin_icons, key, icons);
2977 /* Look up a builtin icon; the min_difference_p and
2978 * has_larger_p out parameters allow us to combine
2979 * this lookup with searching through the actual directories
2980 * of the "hicolor" icon theme. See theme_lookup_icon()
2981 * for how they are used.
2983 static BuiltinIcon *
2984 find_builtin_icon (const gchar *icon_name,
2986 gint *min_difference_p,
2987 gboolean *has_larger_p)
2989 GSList *icons = NULL;
2990 gint min_difference = G_MAXINT;
2991 gboolean has_larger = FALSE;
2992 BuiltinIcon *min_icon = NULL;
2994 if (!icon_theme_builtin_icons)
2997 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
3001 BuiltinIcon *default_icon = icons->data;
3002 int min, max, difference;
3005 min = default_icon->size - 2;
3006 max = default_icon->size + 2;
3007 smaller = size < min;
3009 difference = min - size;
3010 else if (size > max)
3011 difference = size - max;
3015 if (difference == 0)
3017 min_icon = default_icon;
3023 if (difference < min_difference || smaller)
3025 min_difference = difference;
3026 min_icon = default_icon;
3027 has_larger = smaller;
3032 if (difference < min_difference && smaller)
3034 min_difference = difference;
3035 min_icon = default_icon;
3039 icons = icons->next;
3042 if (min_difference_p)
3043 *min_difference_p = min_difference;
3045 *has_larger_p = has_larger;
3051 _gtk_icon_theme_check_reload (GdkDisplay *display)
3055 GtkIconTheme *icon_theme;
3057 n_screens = gdk_display_get_n_screens (display);
3059 for (i = 0; i < n_screens; i++)
3061 screen = gdk_display_get_screen (display, i);
3063 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
3066 icon_theme->priv->check_reload = TRUE;
3067 ensure_valid_themes (icon_theme);
3068 icon_theme->priv->check_reload = FALSE;
3075 /* DLL ABI stability backward compatibility versions */
3077 #undef gtk_icon_theme_set_search_path
3080 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
3081 const gchar *path[],
3084 const gchar **utf8_path;
3087 utf8_path = g_new (const gchar *, n_elements);
3089 for (i = 0; i < n_elements; i++)
3090 utf8_path[i] = g_locale_to_utf8 (path[i], -1, NULL, NULL, NULL);
3092 gtk_icon_theme_set_search_path_utf8 (icon_theme, utf8_path, n_elements);
3094 for (i = 0; i < n_elements; i++)
3095 g_free ((gchar *) utf8_path[i]);
3100 #undef gtk_icon_theme_get_search_path
3103 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
3109 gtk_icon_theme_get_search_path_utf8 (icon_theme, path, &n);
3116 for (i = 0; i < n; i++)
3118 gchar *tem = (*path)[i];
3120 (*path)[i] = g_locale_from_utf8 ((*path)[i], -1, NULL, NULL, NULL);
3126 #undef gtk_icon_theme_append_search_path
3129 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
3132 gchar *utf8_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
3134 gtk_icon_theme_append_search_path_utf8 (icon_theme, utf8_path);
3139 #undef gtk_icon_theme_prepend_search_path
3142 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
3145 gchar *utf8_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
3147 gtk_icon_theme_prepend_search_path_utf8 (icon_theme, utf8_path);
3152 #undef gtk_icon_info_get_filename
3154 G_CONST_RETURN gchar *
3155 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
3157 g_return_val_if_fail (icon_info != NULL, NULL);
3159 return icon_info->cp_filename;
3164 #define __GTK_ICON_THEME_C__
3165 #include "gtkaliasdef.c"