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"
43 #include "gtksettings.h"
44 #include "gtkprivate.h"
47 #define DEFAULT_THEME_NAME "hicolor"
52 ICON_THEME_DIR_SCALABLE,
53 ICON_THEME_DIR_THRESHOLD,
54 ICON_THEME_DIR_UNTHEMED
57 /* In reverse search order: */
61 ICON_SUFFIX_XPM = 1 << 0,
62 ICON_SUFFIX_SVG = 1 << 1,
63 ICON_SUFFIX_PNG = 1 << 2,
64 HAS_ICON_FILE = 1 << 3
68 struct _GtkIconThemePrivate
70 guint custom_theme : 1;
71 guint is_screen_singleton : 1;
72 guint pixbuf_supports_svg : 1;
79 gboolean themes_valid;
80 /* A list of all the themes needed to look up icons.
81 * In search order, without duplicates
84 GHashTable *unthemed_icons;
86 /* Note: The keys of this hashtable are owned by the
87 * themedir and unthemed hashtables.
89 GHashTable *all_icons;
91 /* GdkScreen for the icon theme (may be NULL)
95 /* time when we last stat:ed for theme changes */
99 gulong reset_styles_idle;
101 gboolean check_reload;
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_class_init (GtkIconThemeClass *klass);
191 static void gtk_icon_theme_init (GtkIconTheme *icon_theme);
192 static void gtk_icon_theme_finalize (GObject *object);
193 static void theme_dir_destroy (IconThemeDir *dir);
195 static void theme_destroy (IconTheme *theme);
196 static GtkIconInfo *theme_lookup_icon (IconTheme *theme,
197 const char *icon_name,
200 gboolean use_default_icons);
201 static void theme_list_icons (IconTheme *theme,
204 static void theme_subdir_load (GtkIconTheme *icon_theme,
206 GKeyFile *theme_file,
208 static void do_theme_change (GtkIconTheme *icon_theme);
210 static void blow_themes (GtkIconTheme *icon_themes);
212 static void icon_data_free (GtkIconData *icon_data);
213 static void load_icon_data (IconThemeDir *dir,
217 static IconSuffix theme_dir_get_icon_suffix (IconThemeDir *dir,
218 const gchar *icon_name,
219 gboolean *has_icon_file);
222 static GtkIconInfo *icon_info_new (void);
223 static GtkIconInfo *icon_info_new_builtin (BuiltinIcon *icon);
225 static IconSuffix suffix_from_name (const char *name);
227 static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
229 gint *min_difference_p,
230 gboolean *has_larger_p);
232 static GObjectClass *parent_class = NULL;
234 static guint signal_changed = 0;
236 static GHashTable *icon_theme_builtin_icons;
238 /* also used in gtkiconfactory.c */
239 GtkIconCache *_builtin_cache = NULL;
240 static GList *builtin_dirs = NULL;
244 gtk_icon_theme_get_type (void)
246 static GType type = 0;
250 static const GTypeInfo info =
252 sizeof (GtkIconThemeClass),
253 NULL, /* base_init */
254 NULL, /* base_finalize */
255 (GClassInitFunc) gtk_icon_theme_class_init,
256 NULL, /* class_finalize */
257 NULL, /* class_data */
258 sizeof (GtkIconTheme),
260 (GInstanceInitFunc) gtk_icon_theme_init,
263 type = g_type_register_static (G_TYPE_OBJECT, I_("GtkIconTheme"), &info, 0);
270 * gtk_icon_theme_new:
272 * Creates a new icon theme object. Icon theme objects are used
273 * to lookup up an icon by name in a particular icon theme.
274 * Usually, you'll want to use gtk_icon_theme_get_default()
275 * or gtk_icon_theme_get_for_screen() rather than creating
276 * a new icon theme object for scratch.
278 * Return value: the newly created #GtkIconTheme object.
283 gtk_icon_theme_new (void)
285 return g_object_new (GTK_TYPE_ICON_THEME, NULL);
289 * gtk_icon_theme_get_default:
291 * Gets the icon theme for the default screen. See
292 * gtk_icon_theme_get_for_screen().
294 * Return value: A unique #GtkIconTheme associated with
295 * the default screen. This icon theme is associated with
296 * the screen and can be used as long as the screen
297 * is open. Do not ref or unref it.
302 gtk_icon_theme_get_default (void)
304 return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
308 * gtk_icon_theme_get_for_screen:
309 * @screen: a #GdkScreen
311 * Gets the icon theme object associated with @screen; if this
312 * function has not previously been called for the given
313 * screen, a new icon theme object will be created and
314 * associated with the screen. Icon theme objects are
315 * fairly expensive to create, so using this function
316 * is usually a better choice than calling than gtk_icon_theme_new()
317 * and setting the screen yourself; by using this function
318 * a single icon theme object will be shared between users.
320 * Return value: A unique #GtkIconTheme associated with
321 * the given screen. This icon theme is associated with
322 * the screen and can be used as long as the screen
323 * is open. Do not ref or unref it.
328 gtk_icon_theme_get_for_screen (GdkScreen *screen)
330 GtkIconTheme *icon_theme;
332 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
333 g_return_val_if_fail (!screen->closed, NULL);
335 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
338 GtkIconThemePrivate *priv;
340 icon_theme = gtk_icon_theme_new ();
341 gtk_icon_theme_set_screen (icon_theme, screen);
343 priv = icon_theme->priv;
344 priv->is_screen_singleton = TRUE;
346 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), icon_theme);
353 gtk_icon_theme_class_init (GtkIconThemeClass *klass)
355 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
357 parent_class = g_type_class_peek_parent (klass);
359 gobject_class->finalize = gtk_icon_theme_finalize;
362 * GtkIconTheme::changed
363 * @icon_theme: the icon theme
365 * Emitted when the current icon theme is switched or GTK+ detects
366 * that a change has occurred in the contents of the current
369 signal_changed = g_signal_new (I_("changed"),
370 G_TYPE_FROM_CLASS (klass),
372 G_STRUCT_OFFSET (GtkIconThemeClass, changed),
374 g_cclosure_marshal_VOID__VOID,
377 g_type_class_add_private (klass, sizeof (GtkIconThemePrivate));
381 /* Callback when the display that the icon theme is attached to
382 * is closed; unset the screen, and if it's the unique theme
383 * for the screen, drop the reference
386 display_closed (GdkDisplay *display,
388 GtkIconTheme *icon_theme)
390 GtkIconThemePrivate *priv = icon_theme->priv;
391 GdkScreen *screen = priv->screen;
392 gboolean was_screen_singleton = priv->is_screen_singleton;
394 if (was_screen_singleton)
396 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), NULL);
397 priv->is_screen_singleton = FALSE;
400 gtk_icon_theme_set_screen (icon_theme, NULL);
402 if (was_screen_singleton)
404 g_object_unref (icon_theme);
409 update_current_theme (GtkIconTheme *icon_theme)
411 GtkIconThemePrivate *priv = icon_theme->priv;
413 if (!priv->custom_theme)
416 gchar *fallback_theme = NULL;
417 gboolean changed = FALSE;
421 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
422 g_object_get (settings,
423 "gtk-icon-theme-name", &theme,
424 "gtk-fallback-icon-theme", &fallback_theme, NULL);
428 theme = g_strdup (DEFAULT_THEME_NAME);
430 if (strcmp (priv->current_theme, theme) != 0)
432 g_free (priv->current_theme);
433 priv->current_theme = theme;
440 if ((priv->fallback_theme && !fallback_theme) ||
441 (!priv->fallback_theme && fallback_theme) ||
442 (priv->fallback_theme && fallback_theme &&
443 strcmp (priv->fallback_theme, fallback_theme) != 0))
445 g_free (priv->fallback_theme);
446 priv->fallback_theme = fallback_theme;
451 g_free (fallback_theme);
454 do_theme_change (icon_theme);
458 /* Callback when the icon theme GtkSetting changes
461 theme_changed (GtkSettings *settings,
463 GtkIconTheme *icon_theme)
465 update_current_theme (icon_theme);
469 unset_screen (GtkIconTheme *icon_theme)
471 GtkIconThemePrivate *priv = icon_theme->priv;
472 GtkSettings *settings;
477 settings = gtk_settings_get_for_screen (priv->screen);
478 display = gdk_screen_get_display (priv->screen);
480 g_signal_handlers_disconnect_by_func (display,
481 (gpointer) display_closed,
483 g_signal_handlers_disconnect_by_func (settings,
484 (gpointer) theme_changed,
492 * gtk_icon_theme_set_screen:
493 * @icon_theme: a #GtkIconTheme
494 * @screen: a #GdkScreen
496 * Sets the screen for an icon theme; the screen is used
497 * to track the user's currently configured icon theme,
498 * which might be different for different screens.
503 gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
506 GtkIconThemePrivate *priv;
507 GtkSettings *settings;
510 g_return_if_fail (GTK_ICON_THEME (icon_theme));
511 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
513 priv = icon_theme->priv;
515 unset_screen (icon_theme);
519 display = gdk_screen_get_display (screen);
520 settings = gtk_settings_get_for_screen (screen);
522 priv->screen = screen;
524 g_signal_connect (display, "closed",
525 G_CALLBACK (display_closed), icon_theme);
526 g_signal_connect (settings, "notify::gtk-icon-theme-name",
527 G_CALLBACK (theme_changed), icon_theme);
528 g_signal_connect (settings, "notify::gtk-fallback-icon-theme-name",
529 G_CALLBACK (theme_changed), icon_theme);
532 update_current_theme (icon_theme);
535 /* Checks whether a loader for SVG files has been registered
539 pixbuf_supports_svg (void)
541 GSList *formats = gdk_pixbuf_get_formats ();
543 static gboolean found_svg = FALSE;
544 static gboolean value_known = FALSE;
549 for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
551 gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
554 for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
556 if (strcmp (*mime_type, "image/svg") == 0)
560 g_strfreev (mime_types);
563 g_slist_free (formats);
570 gtk_icon_theme_init (GtkIconTheme *icon_theme)
572 GtkIconThemePrivate *priv;
573 const gchar * const *xdg_data_dirs;
576 priv = g_type_instance_get_private ((GTypeInstance *)icon_theme,
577 GTK_TYPE_ICON_THEME);
578 icon_theme->priv = priv;
580 priv->custom_theme = FALSE;
581 priv->current_theme = g_strdup (DEFAULT_THEME_NAME);
583 xdg_data_dirs = g_get_system_data_dirs ();
584 for (i = 0; xdg_data_dirs[i]; i++) ;
586 priv->search_path_len = 2 * i + 2;
588 priv->search_path = g_new (char *, priv->search_path_len);
591 priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
592 priv->search_path[i++] = g_build_filename (g_get_user_data_dir (), "icons", NULL);
594 for (j = 0; xdg_data_dirs[j]; j++)
595 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
597 for (j = 0; xdg_data_dirs[j]; j++)
598 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "pixmaps", NULL);
600 priv->themes_valid = FALSE;
602 priv->unthemed_icons = NULL;
604 priv->pixbuf_supports_svg = pixbuf_supports_svg ();
608 free_dir_mtime (IconThemeDirMtime *dir_mtime)
610 if (dir_mtime->cache)
611 _gtk_icon_cache_unref (dir_mtime->cache);
613 g_free (dir_mtime->dir);
619 reset_styles_idle (gpointer user_data)
621 GtkIconTheme *icon_theme;
622 GtkIconThemePrivate *priv;
624 GDK_THREADS_ENTER ();
626 icon_theme = GTK_ICON_THEME (user_data);
627 priv = icon_theme->priv;
629 if (priv->screen && priv->is_screen_singleton)
631 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
632 gtk_rc_reset_styles (settings);
635 priv->reset_styles_idle = 0;
637 GDK_THREADS_LEAVE ();
643 do_theme_change (GtkIconTheme *icon_theme)
645 GtkIconThemePrivate *priv = icon_theme->priv;
648 g_print ("change to icon theme \"%s\"\n", priv->current_theme));
649 blow_themes (icon_theme);
650 g_signal_emit (icon_theme, signal_changed, 0);
652 if (!priv->reset_styles_idle)
653 priv->reset_styles_idle =
654 g_idle_add (reset_styles_idle, icon_theme);
658 blow_themes (GtkIconTheme *icon_theme)
660 GtkIconThemePrivate *priv = icon_theme->priv;
662 if (priv->themes_valid)
664 g_hash_table_destroy (priv->all_icons);
665 g_list_foreach (priv->themes, (GFunc)theme_destroy, NULL);
666 g_list_free (priv->themes);
667 g_list_foreach (priv->dir_mtimes, (GFunc)free_dir_mtime, NULL);
668 g_list_free (priv->dir_mtimes);
669 g_hash_table_destroy (priv->unthemed_icons);
672 priv->unthemed_icons = NULL;
673 priv->dir_mtimes = NULL;
674 priv->all_icons = NULL;
675 priv->themes_valid = FALSE;
679 gtk_icon_theme_finalize (GObject *object)
681 GtkIconTheme *icon_theme;
682 GtkIconThemePrivate *priv;
685 icon_theme = GTK_ICON_THEME (object);
686 priv = icon_theme->priv;
688 if (priv->reset_styles_idle)
690 g_source_remove (priv->reset_styles_idle);
691 priv->reset_styles_idle = 0;
694 unset_screen (icon_theme);
696 g_free (priv->current_theme);
697 priv->current_theme = NULL;
699 for (i=0; i < priv->search_path_len; i++)
700 g_free (priv->search_path[i]);
702 g_free (priv->search_path);
703 priv->search_path = NULL;
705 blow_themes (icon_theme);
707 G_OBJECT_CLASS (parent_class)->finalize (object);
711 * gtk_icon_theme_set_search_path:
712 * @icon_theme: a #GtkIconTheme
713 * @path: array of directories that are searched for icon themes
714 * @n_elements: number of elements in @path.
716 * Sets the search path for the icon theme object. When looking
717 * for an icon theme, GTK+ will search for a subdirectory of
718 * one or more of the directories in @path with the same name
719 * as the icon theme. (Themes from multiple of the path elements
720 * are combined to allow themes to be extended by adding icons
721 * in the user's home directory.)
723 * In addition if an icon found isn't found either in the current
724 * icon theme or the default icon theme, and an image file with
725 * the right name is found directly in one of the elements of
726 * @path, then that image will be used for the icon name.
727 * (This is legacy feature, and new icons should be put
728 * into the default icon theme, which is called DEFAULT_THEME_NAME,
729 * rather than directly on the icon path.)
734 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
738 GtkIconThemePrivate *priv;
741 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
743 priv = icon_theme->priv;
744 for (i = 0; i < priv->search_path_len; i++)
745 g_free (priv->search_path[i]);
747 g_free (priv->search_path);
749 priv->search_path = g_new (gchar *, n_elements);
750 priv->search_path_len = n_elements;
751 for (i = 0; i < priv->search_path_len; i++)
752 priv->search_path[i] = g_strdup (path[i]);
754 do_theme_change (icon_theme);
759 * gtk_icon_theme_get_search_path:
760 * @icon_theme: a #GtkIconTheme
761 * @path: location to store a list of icon theme path directories or %NULL
762 * The stored value should be freed with g_strfreev().
763 * @n_elements: location to store number of elements
766 * Gets the current search path. See gtk_icon_theme_set_search_path().
771 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
775 GtkIconThemePrivate *priv;
778 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
780 priv = icon_theme->priv;
783 *n_elements = priv->search_path_len;
787 *path = g_new (gchar *, priv->search_path_len + 1);
788 for (i = 0; i < priv->search_path_len; i++)
789 (*path)[i] = g_strdup (priv->search_path[i]);
795 * gtk_icon_theme_append_search_path:
796 * @icon_theme: a #GtkIconTheme
797 * @path: directory name to append to the icon path
799 * Appends a directory to the search path.
800 * See gtk_icon_theme_set_search_path().
805 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
808 GtkIconThemePrivate *priv;
810 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
811 g_return_if_fail (path != NULL);
813 priv = icon_theme->priv;
815 priv->search_path_len++;
816 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
817 priv->search_path[priv->search_path_len-1] = g_strdup (path);
819 do_theme_change (icon_theme);
823 * gtk_icon_theme_prepend_search_path:
824 * @icon_theme: a #GtkIconTheme
825 * @path: directory name to prepend to the icon path
827 * Prepends a directory to the search path.
828 * See gtk_icon_theme_set_search_path().
833 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
836 GtkIconThemePrivate *priv;
839 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
840 g_return_if_fail (path != NULL);
842 priv = icon_theme->priv;
844 priv->search_path_len++;
845 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
847 for (i = priv->search_path_len - 1; i > 0; i--)
848 priv->search_path[i] = priv->search_path[i - 1];
850 priv->search_path[0] = g_strdup (path);
852 do_theme_change (icon_theme);
856 * gtk_icon_theme_set_custom_theme:
857 * @icon_theme: a #GtkIconTheme
858 * @theme_name: name of icon theme to use instead of configured theme,
859 * or %NULL to unset a previously set custom theme
861 * Sets the name of the icon theme that the #GtkIconTheme object uses
862 * overriding system configuration. This function cannot be called
863 * on the icon theme objects returned from gtk_icon_theme_get_default()
864 * and gtk_icon_theme_get_for_screen().
869 gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
870 const gchar *theme_name)
872 GtkIconThemePrivate *priv;
874 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
876 priv = icon_theme->priv;
878 g_return_if_fail (!priv->is_screen_singleton);
880 if (theme_name != NULL)
882 priv->custom_theme = TRUE;
883 if (strcmp (theme_name, priv->current_theme) != 0)
885 g_free (priv->current_theme);
886 priv->current_theme = g_strdup (theme_name);
888 do_theme_change (icon_theme);
893 priv->custom_theme = FALSE;
895 update_current_theme (icon_theme);
900 insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
906 GtkIconThemePrivate *priv;
909 GKeyFile *theme_file;
910 GError *error = NULL;
911 IconThemeDirMtime *dir_mtime;
912 struct stat stat_buf;
914 priv = icon_theme->priv;
916 for (l = priv->themes; l != NULL; l = l->next)
919 if (strcmp (theme->name, theme_name) == 0)
923 for (i = 0; i < priv->search_path_len; i++)
925 path = g_build_filename (priv->search_path[i],
928 dir_mtime = g_new (IconThemeDirMtime, 1);
929 dir_mtime->cache = NULL;
930 dir_mtime->dir = path;
931 if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
932 dir_mtime->mtime = stat_buf.st_mtime;
934 dir_mtime->mtime = 0;
936 priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
938 priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
941 for (i = 0; i < priv->search_path_len && !theme_file; i++)
943 path = g_build_filename (priv->search_path[i],
947 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
949 theme_file = g_key_file_new ();
950 g_key_file_set_list_separator (theme_file, ',');
951 g_key_file_load_from_file (theme_file, path, 0, &error);
954 g_key_file_free (theme_file);
956 g_error_free (error);
963 if (theme_file || strcmp (theme_name, DEFAULT_THEME_NAME) == 0)
965 theme = g_new0 (IconTheme, 1);
966 theme->name = g_strdup (theme_name);
967 priv->themes = g_list_prepend (priv->themes, theme);
970 if (theme_file == NULL)
973 theme->display_name =
974 g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
975 if (!theme->display_name)
976 g_warning ("Theme file for %s has no name\n", theme_name);
978 dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
980 g_warning ("Theme file for %s has no directories\n", theme_name);
983 g_key_file_get_locale_string (theme_file,
984 "Icon Theme", "Comment",
987 g_key_file_get_string (theme_file,
988 "Icon Theme", "Example",
992 for (i = 0; dirs[i] != NULL; i++)
993 theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
997 theme->dirs = g_list_reverse (theme->dirs);
999 themes = g_key_file_get_string_list (theme_file,
1006 for (i = 0; themes[i] != NULL; i++)
1007 insert_theme (icon_theme, themes[i]);
1009 g_strfreev (themes);
1012 g_key_file_free (theme_file);
1016 free_unthemed_icon (UnthemedIcon *unthemed_icon)
1018 if (unthemed_icon->svg_filename)
1019 g_free (unthemed_icon->svg_filename);
1020 if (unthemed_icon->no_svg_filename)
1021 g_free (unthemed_icon->no_svg_filename);
1022 g_free (unthemed_icon);
1026 load_themes (GtkIconTheme *icon_theme)
1028 GtkIconThemePrivate *priv;
1031 char *dir, *base_name, *dot;
1034 UnthemedIcon *unthemed_icon;
1035 IconSuffix old_suffix, new_suffix;
1037 IconThemeDirMtime *dir_mtime;
1038 struct stat stat_buf;
1040 priv = icon_theme->priv;
1042 priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
1044 insert_theme (icon_theme, priv->current_theme);
1046 /* Always look in the "default" icon theme, and in a fallback theme */
1047 if (priv->fallback_theme)
1048 insert_theme (icon_theme, priv->fallback_theme);
1049 insert_theme (icon_theme, DEFAULT_THEME_NAME);
1050 priv->themes = g_list_reverse (priv->themes);
1053 priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
1054 g_free, (GDestroyNotify)free_unthemed_icon);
1056 for (base = 0; base < icon_theme->priv->search_path_len; base++)
1058 dir = icon_theme->priv->search_path[base];
1060 dir_mtime = g_new (IconThemeDirMtime, 1);
1061 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir);
1062 dir_mtime->dir = g_strdup (dir);
1063 if (g_stat (dir, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
1064 dir_mtime->mtime = stat_buf.st_mtime;
1066 dir_mtime->mtime = 0;
1068 priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
1070 if (dir_mtime->cache != NULL)
1073 gdir = g_dir_open (dir, 0, NULL);
1078 while ((file = g_dir_read_name (gdir)))
1080 new_suffix = suffix_from_name (file);
1082 if (new_suffix != ICON_SUFFIX_NONE)
1084 abs_file = g_build_filename (dir, file, NULL);
1086 base_name = g_strdup (file);
1088 dot = strrchr (base_name, '.');
1092 if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
1095 if (new_suffix == ICON_SUFFIX_SVG)
1097 if (unthemed_icon->svg_filename)
1100 unthemed_icon->svg_filename = abs_file;
1104 if (unthemed_icon->no_svg_filename)
1106 old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
1107 if (new_suffix > old_suffix)
1109 g_free (unthemed_icon->no_svg_filename);
1110 unthemed_icon->no_svg_filename = abs_file;
1116 unthemed_icon->no_svg_filename = abs_file;
1123 unthemed_icon = g_new0 (UnthemedIcon, 1);
1125 if (new_suffix == ICON_SUFFIX_SVG)
1126 unthemed_icon->svg_filename = abs_file;
1128 unthemed_icon->no_svg_filename = abs_file;
1130 g_hash_table_insert (priv->unthemed_icons,
1133 g_hash_table_insert (priv->all_icons,
1141 priv->themes_valid = TRUE;
1143 g_get_current_time(&tv);
1144 priv->last_stat_time = tv.tv_sec;
1148 _gtk_icon_theme_ensure_builtin_cache (void)
1150 static gboolean initialized = FALSE;
1152 gint sizes[5] = { 16, 20, 24, 32, 48 };
1153 gint n_sizes = G_N_ELEMENTS (sizes);
1160 _builtin_cache = _gtk_icon_cache_new ((gchar *)builtin_icons);
1162 for (i = 0; i < n_sizes; i++)
1164 dir = g_new (IconThemeDir, 1);
1165 dir->type = ICON_THEME_DIR_THRESHOLD;
1167 dir->size = sizes[i];
1168 dir->min_size = sizes[i];
1169 dir->max_size = sizes[i];
1172 dir->icon_data = NULL;
1173 dir->subdir = g_strdup_printf ("%d", sizes[i]);
1174 dir->cache = _gtk_icon_cache_ref (_builtin_cache);
1176 builtin_dirs = g_list_append (builtin_dirs, dir);
1182 ensure_valid_themes (GtkIconTheme *icon_theme)
1184 GtkIconThemePrivate *priv = icon_theme->priv;
1186 gboolean was_valid = priv->themes_valid;
1188 _gtk_icon_theme_ensure_builtin_cache ();
1190 if (priv->themes_valid)
1192 g_get_current_time (&tv);
1194 if (ABS (tv.tv_sec - priv->last_stat_time) > 5)
1195 gtk_icon_theme_rescan_if_needed (icon_theme);
1198 if (!priv->themes_valid)
1200 load_themes (icon_theme);
1202 if (!priv->check_reload && was_valid && priv->screen)
1204 static GdkAtom atom_iconthemes = GDK_NONE;
1205 GdkEvent *event = gdk_event_new (GDK_CLIENT_EVENT);
1208 if (!atom_iconthemes)
1209 atom_iconthemes = gdk_atom_intern_static_string ("_GTK_LOAD_ICONTHEMES");
1211 for (i = 0; i < 5; i++)
1212 event->client.data.l[i] = 0;
1213 event->client.data_format = 32;
1214 event->client.message_type = atom_iconthemes;
1216 gdk_screen_broadcast_client_message (priv->screen, event);
1222 * gtk_icon_theme_lookup_icon:
1223 * @icon_theme: a #GtkIconTheme
1224 * @icon_name: the name of the icon to lookup
1225 * @size: desired icon size
1226 * @flags: flags modifying the behavior of the icon lookup
1228 * Looks up a named icon and returns a structure containing
1229 * information such as the filename of the icon. The icon
1230 * can then be rendered into a pixbuf using
1231 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1232 * combines these two steps if all you need is the pixbuf.)
1234 * Return value: a #GtkIconInfo structure containing information
1235 * about the icon, or %NULL if the icon wasn't found. Free with
1236 * gtk_icon_info_free()
1241 gtk_icon_theme_lookup_icon (GtkIconTheme *icon_theme,
1242 const gchar *icon_name,
1244 GtkIconLookupFlags flags)
1246 GtkIconThemePrivate *priv;
1248 GtkIconInfo *icon_info = NULL;
1249 UnthemedIcon *unthemed_icon;
1251 gboolean use_builtin;
1253 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1254 g_return_val_if_fail (icon_name != NULL, NULL);
1255 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1256 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1258 priv = icon_theme->priv;
1260 GTK_NOTE (ICONTHEME,
1261 g_print ("gtk_icon_theme_lookup_icon %s\n", icon_name));
1262 if (flags & GTK_ICON_LOOKUP_NO_SVG)
1264 else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
1267 allow_svg = priv->pixbuf_supports_svg;
1269 use_builtin = (flags & GTK_ICON_LOOKUP_USE_BUILTIN);
1271 ensure_valid_themes (icon_theme);
1273 for (l = priv->themes; l; l = l->next)
1275 IconTheme *theme = l->data;
1277 icon_info = theme_lookup_icon (theme, icon_name, size, allow_svg, use_builtin);
1282 unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_name);
1285 icon_info = icon_info_new ();
1287 /* A SVG icon, when allowed, beats out a XPM icon, but not
1291 unthemed_icon->svg_filename &&
1292 (!unthemed_icon->no_svg_filename ||
1293 suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
1294 icon_info->filename = g_strdup (unthemed_icon->svg_filename);
1295 else if (unthemed_icon->no_svg_filename)
1296 icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);
1298 icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
1299 -1, NULL, NULL, NULL);
1302 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1307 icon_info->desired_size = size;
1310 static gboolean check_for_default_theme = TRUE;
1311 char *default_theme_path;
1312 gboolean found = FALSE;
1315 if (check_for_default_theme)
1317 check_for_default_theme = FALSE;
1319 for (i = 0; !found && i < priv->search_path_len; i++)
1321 default_theme_path = g_build_filename (priv->search_path[i],
1325 found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
1326 g_free (default_theme_path);
1330 g_warning (_("Could not find the icon '%s'. The '%s' theme\n"
1331 "was not found either, perhaps you need to install it.\n"
1332 "You can get a copy from:\n"
1334 icon_name, DEFAULT_THEME_NAME, "http://icon-theme.freedesktop.org/releases");
1344 gtk_icon_theme_error_quark (void)
1346 static GQuark q = 0;
1348 q = g_quark_from_static_string ("gtk-icon-theme-error-quark");
1354 * gtk_icon_theme_load_icon:
1355 * @icon_theme: a #GtkIconTheme
1356 * @icon_name: the name of the icon to lookup
1357 * @size: the desired icon size. The resulting icon may not be
1358 * exactly this size; see gtk_icon_info_load_icon().
1359 * @flags: flags modifying the behavior of the icon lookup
1360 * @error: Location to store error information on failure, or %NULL.
1362 * Looks up an icon in an icon theme, scales it to the given size
1363 * and renders it into a pixbuf. This is a convenience function;
1364 * if more details about the icon are needed, use
1365 * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1367 * Note that you probably want to listen for icon theme changes and
1368 * update the icon. This is usually done by connecting to the
1369 * GtkWidget::style-set signal. If for some reason you do not want to
1370 * update the icon when the icon theme changes, you should consider
1371 * using gdk_pixbuf_copy() to make a private copy of the pixbuf
1372 * returned by this function. Otherwise GTK+ may need to keep the old
1373 * icon theme loaded, which would be a waste of memory.
1375 * Return value: the rendered icon; this may be a newly created icon
1376 * or a new reference to an internal icon, so you must not modify
1377 * the icon. Use g_object_unref() to release your reference to the
1378 * icon. %NULL if the icon isn't found.
1383 gtk_icon_theme_load_icon (GtkIconTheme *icon_theme,
1384 const gchar *icon_name,
1386 GtkIconLookupFlags flags,
1389 GtkIconInfo *icon_info;
1390 GdkPixbuf *pixbuf = NULL;
1392 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1393 g_return_val_if_fail (icon_name != NULL, NULL);
1394 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1395 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1396 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1398 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
1399 flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1402 g_set_error (error, GTK_ICON_THEME_ERROR, GTK_ICON_THEME_NOT_FOUND,
1403 _("Icon '%s' not present in theme"), icon_name);
1407 pixbuf = gtk_icon_info_load_icon (icon_info, error);
1408 gtk_icon_info_free (icon_info);
1414 * gtk_icon_theme_has_icon:
1415 * @icon_theme: a #GtkIconTheme
1416 * @icon_name: the name of an icon
1418 * Checks whether an icon theme includes an icon
1419 * for a particular name.
1421 * Return value: %TRUE if @icon_theme includes an
1422 * icon for @icon_name.
1427 gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
1428 const char *icon_name)
1430 GtkIconThemePrivate *priv;
1433 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1435 priv = icon_theme->priv;
1437 ensure_valid_themes (icon_theme);
1439 for (l = priv->dir_mtimes; l; l = l->next)
1441 IconThemeDirMtime *dir_mtime = l->data;
1442 GtkIconCache *cache = dir_mtime->cache;
1444 if (cache && _gtk_icon_cache_has_icon (cache, icon_name))
1448 if (g_hash_table_lookup_extended (priv->all_icons,
1449 icon_name, NULL, NULL))
1452 if (_builtin_cache &&
1453 _gtk_icon_cache_has_icon (_builtin_cache, icon_name))
1456 if (icon_theme_builtin_icons &&
1457 g_hash_table_lookup_extended (icon_theme_builtin_icons,
1458 icon_name, NULL, NULL))
1465 add_size (gpointer key,
1469 gint **res_p = user_data;
1471 **res_p = GPOINTER_TO_INT (key);
1477 * gtk_icon_theme_get_icon_sizes:
1478 * @icon_theme: a #GtkIconTheme
1479 * @icon_name: the name of an icon
1481 * Returns an array of integers describing the sizes at which
1482 * the icon is available without scaling. A size of -1 means
1483 * that the icon is available in a scalable format. The array
1484 * is zero-terminated.
1486 * Return value: An newly allocated array describing the sizes at
1487 * which the icon is available. The array should be freed with g_free()
1488 * when it is no longer needed.
1493 gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
1494 const char *icon_name)
1496 GList *l, *d, *icons;
1500 GtkIconThemePrivate *priv;
1502 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1504 priv = icon_theme->priv;
1506 ensure_valid_themes (icon_theme);
1508 sizes = g_hash_table_new (g_direct_hash, g_direct_equal);
1510 for (l = priv->themes; l; l = l->next)
1512 IconTheme *theme = l->data;
1513 for (d = theme->dirs; d; d = d->next)
1515 IconThemeDir *dir = d->data;
1517 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1518 if (suffix != ICON_SUFFIX_NONE)
1520 if (suffix == ICON_SUFFIX_SVG)
1521 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1523 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1528 for (d = builtin_dirs; d; d = d->next)
1530 IconThemeDir *dir = d->data;
1532 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1533 if (suffix != ICON_SUFFIX_NONE)
1535 if (suffix == ICON_SUFFIX_SVG)
1536 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1538 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1542 if (icon_theme_builtin_icons)
1544 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
1548 BuiltinIcon *icon = icons->data;
1550 g_hash_table_insert (sizes, GINT_TO_POINTER (icon->size), NULL);
1551 icons = icons->next;
1555 r = result = g_new0 (gint, g_hash_table_size (sizes) + 1);
1557 g_hash_table_foreach (sizes, add_size, &r);
1558 g_hash_table_destroy (sizes);
1564 add_key_to_hash (gpointer key,
1568 GHashTable *hash = user_data;
1570 g_hash_table_insert (hash, key, NULL);
1574 add_key_to_list (gpointer key,
1578 GList **list = user_data;
1580 *list = g_list_prepend (*list, g_strdup (key));
1584 * gtk_icon_theme_list_icons:
1585 * @icon_theme: a #GtkIconTheme
1586 * @context: a string identifying a particular type of icon,
1587 * or %NULL to list all icons.
1589 * Lists the icons in the current icon theme. Only a subset
1590 * of the icons can be listed by providing a context string.
1591 * The set of values for the context string is system dependent,
1592 * but will typically include such values as "Applications" and
1595 * Return value: a #GList list holding the names of all the
1596 * icons in the theme. You must first free each element
1597 * in the list with g_free(), then free the list itself
1598 * with g_list_free().
1603 gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
1604 const char *context)
1606 GtkIconThemePrivate *priv;
1609 GQuark context_quark;
1611 priv = icon_theme->priv;
1613 ensure_valid_themes (icon_theme);
1617 context_quark = g_quark_try_string (context);
1625 icons = g_hash_table_new (g_str_hash, g_str_equal);
1630 theme_list_icons (l->data, icons, context_quark);
1634 if (context_quark == 0)
1635 g_hash_table_foreach (priv->unthemed_icons,
1641 g_hash_table_foreach (icons,
1645 g_hash_table_destroy (icons);
1651 * gtk_icon_theme_get_example_icon_name:
1652 * @icon_theme: a #GtkIconTheme
1654 * Gets the name of an icon that is representative of the
1655 * current theme (for instance, to use when presenting
1656 * a list of themes to the user.)
1658 * Return value: the name of an example icon or %NULL.
1659 * Free with g_free().
1664 gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
1666 GtkIconThemePrivate *priv;
1670 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1672 priv = icon_theme->priv;
1674 ensure_valid_themes (icon_theme);
1681 return g_strdup (theme->example);
1690 * gtk_icon_theme_rescan_if_needed:
1691 * @icon_theme: a #GtkIconTheme
1693 * Checks to see if the icon theme has changed; if it has, any
1694 * currently cached information is discarded and will be reloaded
1695 * next time @icon_theme is accessed.
1697 * Return value: %TRUE if the icon theme has changed and needed
1703 gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
1705 GtkIconThemePrivate *priv;
1706 IconThemeDirMtime *dir_mtime;
1709 struct stat stat_buf;
1712 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1714 priv = icon_theme->priv;
1716 for (d = priv->dir_mtimes; d != NULL; d = d->next)
1718 dir_mtime = d->data;
1720 stat_res = g_stat (dir_mtime->dir, &stat_buf);
1722 /* dir mtime didn't change */
1723 if (stat_res == 0 &&
1724 S_ISDIR (stat_buf.st_mode) &&
1725 dir_mtime->mtime == stat_buf.st_mtime)
1727 /* didn't exist before, and still doesn't */
1728 if (dir_mtime->mtime == 0 &&
1729 (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
1732 do_theme_change (icon_theme);
1736 g_get_current_time (&tv);
1737 priv->last_stat_time = tv.tv_sec;
1743 theme_destroy (IconTheme *theme)
1745 g_free (theme->display_name);
1746 g_free (theme->comment);
1747 g_free (theme->name);
1748 g_free (theme->example);
1750 g_list_foreach (theme->dirs, (GFunc)theme_dir_destroy, NULL);
1751 g_list_free (theme->dirs);
1757 theme_dir_destroy (IconThemeDir *dir)
1760 _gtk_icon_cache_unref (dir->cache);
1762 g_hash_table_destroy (dir->icons);
1765 g_hash_table_destroy (dir->icon_data);
1767 g_free (dir->subdir);
1772 theme_dir_size_difference (IconThemeDir *dir, int size, gboolean *smaller)
1777 case ICON_THEME_DIR_FIXED:
1778 *smaller = size < dir->size;
1779 return abs (size - dir->size);
1781 case ICON_THEME_DIR_SCALABLE:
1782 *smaller = size < dir->min_size;
1783 if (size < dir->min_size)
1784 return dir->min_size - size;
1785 if (size > dir->max_size)
1786 return size - dir->max_size;
1789 case ICON_THEME_DIR_THRESHOLD:
1790 min = dir->size - dir->threshold;
1791 max = dir->size + dir->threshold;
1792 *smaller = size < min;
1799 case ICON_THEME_DIR_UNTHEMED:
1800 g_assert_not_reached ();
1803 g_assert_not_reached ();
1808 string_from_suffix (IconSuffix suffix)
1812 case ICON_SUFFIX_XPM:
1814 case ICON_SUFFIX_SVG:
1816 case ICON_SUFFIX_PNG:
1819 g_assert_not_reached();
1825 suffix_from_name (const char *name)
1829 if (g_str_has_suffix (name, ".png"))
1830 retval = ICON_SUFFIX_PNG;
1831 else if (g_str_has_suffix (name, ".svg"))
1832 retval = ICON_SUFFIX_SVG;
1833 else if (g_str_has_suffix (name, ".xpm"))
1834 retval = ICON_SUFFIX_XPM;
1836 retval = ICON_SUFFIX_NONE;
1842 best_suffix (IconSuffix suffix,
1845 if ((suffix & ICON_SUFFIX_PNG) != 0)
1846 return ICON_SUFFIX_PNG;
1847 else if (allow_svg && ((suffix & ICON_SUFFIX_SVG) != 0))
1848 return ICON_SUFFIX_SVG;
1849 else if ((suffix & ICON_SUFFIX_XPM) != 0)
1850 return ICON_SUFFIX_XPM;
1852 return ICON_SUFFIX_NONE;
1857 theme_dir_get_icon_suffix (IconThemeDir *dir,
1858 const gchar *icon_name,
1859 gboolean *has_icon_file)
1865 suffix = (IconSuffix)_gtk_icon_cache_get_icon_flags (dir->cache,
1870 *has_icon_file = suffix & HAS_ICON_FILE;
1872 suffix = suffix & ~HAS_ICON_FILE;
1875 suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
1877 GTK_NOTE (ICONTHEME,
1878 g_print ("get_icon_suffix%s %d\n", dir->cache ? " (cached)" : "", suffix));
1883 static GtkIconInfo *
1884 theme_lookup_icon (IconTheme *theme,
1885 const char *icon_name,
1888 gboolean use_builtin)
1891 IconThemeDir *dir, *min_dir;
1893 int min_difference, difference;
1894 BuiltinIcon *closest_builtin = NULL;
1895 gboolean smaller, has_larger;
1898 min_difference = G_MAXINT;
1902 /* Builtin icons are logically part of the default theme and
1903 * are searched before other subdirectories of the default theme.
1905 if (strcmp (theme->name, DEFAULT_THEME_NAME) == 0 && use_builtin)
1907 closest_builtin = find_builtin_icon (icon_name,
1912 if (min_difference == 0)
1913 return icon_info_new_builtin (closest_builtin);
1915 dirs = builtin_dirs;
1925 GTK_NOTE (ICONTHEME,
1926 g_print ("theme_lookup_icon dir %s\n", dir->dir));
1927 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1928 if (best_suffix (suffix, allow_svg) != ICON_SUFFIX_NONE)
1930 difference = theme_dir_size_difference (dir, size, &smaller);
1932 if (difference == 0)
1940 if (difference < min_difference || smaller)
1942 min_difference = difference;
1944 closest_builtin = NULL;
1945 has_larger = smaller;
1950 if (difference < min_difference && smaller)
1952 min_difference = difference;
1954 closest_builtin = NULL;
1962 if (l == NULL && dirs == builtin_dirs)
1969 if (closest_builtin)
1970 return icon_info_new_builtin (closest_builtin);
1974 GtkIconInfo *icon_info = icon_info_new ();
1975 gboolean has_icon_file = FALSE;
1977 suffix = theme_dir_get_icon_suffix (min_dir, icon_name, &has_icon_file);
1978 suffix = best_suffix (suffix, allow_svg);
1979 g_assert (suffix != ICON_SUFFIX_NONE);
1981 file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
1982 icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
1985 icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
1986 -1, NULL, NULL, NULL);
1989 if (min_dir->icon_data != NULL)
1990 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
1992 if (icon_info->data == NULL && min_dir->cache != NULL)
1994 icon_info->data = _gtk_icon_cache_get_icon_data (min_dir->cache, icon_name, min_dir->subdir);
1995 if (icon_info->data)
1997 if (min_dir->icon_data == NULL)
1998 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
1999 g_free, (GDestroyNotify)icon_data_free);
2001 g_hash_table_replace (min_dir->icon_data, g_strdup (icon_name), icon_info->data);
2005 if (icon_info->data == NULL && has_icon_file)
2007 gchar *icon_file_name, *icon_file_path;
2009 icon_file_name = g_strconcat (icon_name, ".icon", NULL);
2010 icon_file_path = g_build_filename (min_dir->dir, icon_file_name, NULL);
2012 if (g_file_test (icon_file_path, G_FILE_TEST_IS_REGULAR))
2014 if (min_dir->icon_data == NULL)
2015 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2016 g_free, (GDestroyNotify)icon_data_free);
2017 load_icon_data (min_dir, icon_file_path, icon_file_name);
2019 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2021 g_free (icon_file_name);
2022 g_free (icon_file_path);
2027 icon_info->cache_pixbuf = _gtk_icon_cache_get_icon (min_dir->cache, icon_name,
2031 icon_info->dir_type = min_dir->type;
2032 icon_info->dir_size = min_dir->size;
2033 icon_info->threshold = min_dir->threshold;
2042 theme_list_icons (IconTheme *theme,
2046 GList *l = theme->dirs;
2053 if (context == dir->context ||
2058 _gtk_icon_cache_add_icons (dir->cache,
2065 g_hash_table_foreach (dir->icons,
2075 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
2077 GKeyFile *icon_file;
2086 GError *error = NULL;
2090 icon_file = g_key_file_new ();
2091 g_key_file_set_list_separator (icon_file, ',');
2092 g_key_file_load_from_file (icon_file, path, 0, &error);
2095 g_error_free (error);
2100 base_name = g_strdup (name);
2101 dot = strrchr (base_name, '.');
2104 data = g_new0 (GtkIconData, 1);
2105 g_hash_table_replace (dir->icon_data, base_name, data);
2107 ivalues = g_key_file_get_integer_list (icon_file,
2108 "Icon Data", "EmbeddedTextRectangle",
2114 data->has_embedded_rect = TRUE;
2115 data->x0 = ivalues[0];
2116 data->y0 = ivalues[1];
2117 data->x1 = ivalues[2];
2118 data->y1 = ivalues[3];
2124 str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
2127 split = g_strsplit (str, "|", -1);
2129 data->n_attach_points = g_strv_length (split);
2130 data->attach_points = g_malloc (sizeof (GdkPoint) * data->n_attach_points);
2133 while (split[i] != NULL && i < data->n_attach_points)
2135 split_point = strchr (split[i], ',');
2140 data->attach_points[i].x = atoi (split[i]);
2141 data->attach_points[i].y = atoi (split_point);
2150 data->display_name = g_key_file_get_locale_string (icon_file,
2151 "Icon Data", "DisplayName",
2153 g_key_file_free (icon_file);
2158 scan_directory (GtkIconThemePrivate *icon_theme,
2159 IconThemeDir *dir, char *full_dir)
2163 char *base_name, *dot;
2165 IconSuffix suffix, hash_suffix;
2167 GTK_NOTE (ICONTHEME,
2168 g_print ("scanning directory %s\n", full_dir));
2169 dir->icons = g_hash_table_new_full (g_str_hash, g_str_equal,
2172 gdir = g_dir_open (full_dir, 0, NULL);
2177 while ((name = g_dir_read_name (gdir)))
2179 if (g_str_has_suffix (name, ".icon"))
2181 if (dir->icon_data == NULL)
2182 dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2183 g_free, (GDestroyNotify)icon_data_free);
2185 path = g_build_filename (full_dir, name, NULL);
2186 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
2187 load_icon_data (dir, path, name);
2194 suffix = suffix_from_name (name);
2195 if (suffix == ICON_SUFFIX_NONE)
2198 base_name = g_strdup (name);
2199 dot = strrchr (base_name, '.');
2202 hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
2203 g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
2204 g_hash_table_insert (icon_theme->all_icons, base_name, NULL);
2211 theme_subdir_load (GtkIconTheme *icon_theme,
2213 GKeyFile *theme_file,
2219 IconThemeDirType type;
2220 char *context_string;
2227 GError *error = NULL;
2228 IconThemeDirMtime *dir_mtime;
2230 size = g_key_file_get_integer (theme_file, subdir, "Size", &error);
2233 g_error_free (error);
2234 g_warning ("Theme directory %s of theme %s has no size field\n",
2235 subdir, theme->name);
2239 type = ICON_THEME_DIR_THRESHOLD;
2240 type_string = g_key_file_get_string (theme_file, subdir, "Type", NULL);
2243 if (strcmp (type_string, "Fixed") == 0)
2244 type = ICON_THEME_DIR_FIXED;
2245 else if (strcmp (type_string, "Scalable") == 0)
2246 type = ICON_THEME_DIR_SCALABLE;
2247 else if (strcmp (type_string, "Threshold") == 0)
2248 type = ICON_THEME_DIR_THRESHOLD;
2250 g_free (type_string);
2254 context_string = g_key_file_get_string (theme_file, subdir, "Context", NULL);
2257 context = g_quark_from_string (context_string);
2258 g_free (context_string);
2261 max_size = g_key_file_get_integer (theme_file, subdir, "MaxSize", &error);
2266 g_error_free (error);
2270 min_size = g_key_file_get_integer (theme_file, subdir, "MinSize", &error);
2275 g_error_free (error);
2279 threshold = g_key_file_get_integer (theme_file, subdir, "Threshold", &error);
2284 g_error_free (error);
2288 for (d = icon_theme->priv->dir_mtimes; d; d = d->next)
2290 dir_mtime = (IconThemeDirMtime *)d->data;
2292 if (dir_mtime->mtime == 0)
2293 continue; /* directory doesn't exist */
2295 full_dir = g_build_filename (dir_mtime->dir, subdir, NULL);
2297 /* First, see if we have a cache for the directory */
2298 if (dir_mtime->cache != NULL || g_file_test (full_dir, G_FILE_TEST_IS_DIR))
2300 if (dir_mtime->cache == NULL)
2302 /* This will return NULL if the cache doesn't exist or is outdated */
2303 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir_mtime->dir);
2306 dir = g_new (IconThemeDir, 1);
2308 dir->context = context;
2310 dir->min_size = min_size;
2311 dir->max_size = max_size;
2312 dir->threshold = threshold;
2313 dir->dir = full_dir;
2314 dir->icon_data = NULL;
2315 dir->subdir = g_strdup (subdir);
2316 if (dir_mtime->cache != NULL)
2317 dir->cache = _gtk_icon_cache_ref (dir_mtime->cache);
2321 scan_directory (icon_theme->priv, dir, full_dir);
2324 theme->dirs = g_list_prepend (theme->dirs, dir);
2332 icon_data_free (GtkIconData *icon_data)
2334 g_free (icon_data->attach_points);
2335 g_free (icon_data->display_name);
2343 gtk_icon_info_get_type (void)
2345 static GType our_type = 0;
2348 our_type = g_boxed_type_register_static (I_("GtkIconInfo"),
2349 (GBoxedCopyFunc) gtk_icon_info_copy,
2350 (GBoxedFreeFunc) gtk_icon_info_free);
2355 static GtkIconInfo *
2356 icon_info_new (void)
2358 GtkIconInfo *icon_info = g_new0 (GtkIconInfo, 1);
2360 icon_info->scale = -1.;
2365 static GtkIconInfo *
2366 icon_info_new_builtin (BuiltinIcon *icon)
2368 GtkIconInfo *icon_info = icon_info_new ();
2370 icon_info->cache_pixbuf = g_object_ref (icon->pixbuf);
2371 icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
2372 icon_info->dir_size = icon->size;
2373 icon_info->threshold = 2;
2379 * gtk_icon_info_copy:
2380 * @icon_info: a #GtkIconInfo
2382 * Make a copy of a #GtkIconInfo.
2384 * Return value: the new GtkIconInfo
2389 gtk_icon_info_copy (GtkIconInfo *icon_info)
2393 g_return_val_if_fail (icon_info != NULL, NULL);
2395 copy = g_memdup (icon_info, sizeof (GtkIconInfo));
2396 if (copy->cache_pixbuf)
2397 g_object_ref (copy->cache_pixbuf);
2399 g_object_ref (copy->pixbuf);
2400 if (copy->load_error)
2401 copy->load_error = g_error_copy (copy->load_error);
2403 copy->filename = g_strdup (copy->filename);
2405 if (copy->cp_filename)
2406 copy->cp_filename = g_strdup (copy->cp_filename);
2413 * gtk_icon_info_free:
2414 * @icon_info: a #GtkIconInfo
2416 * Free a #GtkIconInfo and associated information
2421 gtk_icon_info_free (GtkIconInfo *icon_info)
2423 g_return_if_fail (icon_info != NULL);
2425 if (icon_info->filename)
2426 g_free (icon_info->filename);
2428 if (icon_info->cp_filename)
2429 g_free (icon_info->cp_filename);
2431 if (icon_info->pixbuf)
2432 g_object_unref (icon_info->pixbuf);
2433 if (icon_info->cache_pixbuf)
2434 g_object_unref (icon_info->cache_pixbuf);
2440 * gtk_icon_info_get_base_size:
2441 * @icon_info: a #GtkIconInfo
2443 * Gets the base size for the icon. The base size
2444 * is a size for the icon that was specified by
2445 * the icon theme creator. This may be different
2446 * than the actual size of image; an example of
2447 * this is small emblem icons that can be attached
2448 * to a larger icon. These icons will be given
2449 * the same base size as the larger icons to which
2450 * they are attached.
2452 * Return value: the base size, or 0, if no base
2453 * size is known for the icon.
2458 gtk_icon_info_get_base_size (GtkIconInfo *icon_info)
2460 g_return_val_if_fail (icon_info != NULL, 0);
2462 return icon_info->dir_size;
2466 * gtk_icon_info_get_filename:
2467 * @icon_info: a #GtkIconInfo
2469 * Gets the filename for the icon. If the
2470 * %GTK_ICON_LOOKUP_USE_BUILTIN flag was passed
2471 * to gtk_icon_theme_lookup_icon(), there may be
2472 * no filename if a builtin icon is returned; in this
2473 * case, you should use gtk_icon_info_get_builtin_pixbuf().
2475 * Return value: the filename for the icon, or %NULL
2476 * if gtk_icon_info_get_builtin_pixbuf() should
2477 * be used instead. The return value is owned by
2478 * GTK+ and should not be modified or freed.
2482 G_CONST_RETURN gchar *
2483 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2485 g_return_val_if_fail (icon_info != NULL, NULL);
2487 return icon_info->filename;
2491 * gtk_icon_info_get_builtin_pixbuf:
2492 * @icon_info: a #GtkIconInfo structure
2494 * Gets the built-in image for this icon, if any. To allow
2495 * GTK+ to use built in icon images, you must pass the
2496 * %GTK_ICON_LOOKUP_USE_BUILTIN to
2497 * gtk_icon_theme_lookup_icon().
2499 * Return value: the built-in image pixbuf, or %NULL. No
2500 * extra reference is added to the returned pixbuf, so if
2501 * you want to keep it around, you must use g_object_ref().
2502 * The returned image must not be modified.
2507 gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
2509 g_return_val_if_fail (icon_info != NULL, NULL);
2511 if (icon_info->filename)
2514 return icon_info->cache_pixbuf;
2518 load_svg_at_size (const gchar *filename,
2522 GdkPixbuf *pixbuf = NULL;
2523 GdkPixbufLoader *loader = NULL;
2524 gchar *contents = NULL;
2527 if (!g_file_get_contents (filename,
2528 &contents, &length, error))
2531 loader = gdk_pixbuf_loader_new_with_type ("svg", error);
2535 gdk_pixbuf_loader_set_size (loader, size, size);
2537 if (!gdk_pixbuf_loader_write (loader, contents, length, error))
2539 gdk_pixbuf_loader_close (loader, NULL);
2543 if (!gdk_pixbuf_loader_close (loader, error))
2546 pixbuf = g_object_ref (gdk_pixbuf_loader_get_pixbuf (loader));
2550 g_object_unref (loader);
2557 /* This function contains the complicatd logic for deciding
2558 * on the size at which to load the icon and loading it at
2562 icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
2563 gboolean scale_only)
2565 int image_width, image_height;
2566 GdkPixbuf *source_pixbuf;
2568 /* First check if we already succeeded have the necessary
2569 * information (or failed earlier)
2571 if (scale_only && icon_info->scale >= 0)
2574 if (icon_info->pixbuf)
2577 if (icon_info->load_error)
2580 /* SVG icons are a special case - we just immediately scale them
2581 * to the desired size
2583 if (icon_info->filename && g_str_has_suffix (icon_info->filename, ".svg"))
2585 icon_info->scale = icon_info->desired_size / 1000.;
2590 icon_info->pixbuf = load_svg_at_size (icon_info->filename,
2591 icon_info->desired_size,
2592 &icon_info->load_error);
2594 return icon_info->pixbuf != NULL;
2597 /* In many cases, the scale can be determined without actual access
2598 * to the icon file. This is generally true when we have a size
2599 * for the directory where the icon is; the image size doesn't
2600 * matter in that case.
2602 if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
2603 icon_info->scale = 1.0;
2604 else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
2606 if (icon_info->desired_size >= icon_info->dir_size - icon_info->threshold &&
2607 icon_info->desired_size <= icon_info->dir_size + icon_info->threshold)
2608 icon_info->scale = 1.0;
2609 else if (icon_info->dir_size > 0)
2610 icon_info->scale =(gdouble) icon_info->desired_size / icon_info->dir_size;
2612 else if (icon_info->dir_type == ICON_THEME_DIR_SCALABLE)
2614 if (icon_info->dir_size > 0)
2615 icon_info->scale = (gdouble) icon_info->desired_size / icon_info->dir_size;
2618 if (icon_info->scale >= 0. && scale_only)
2621 /* At this point, we need to actually get the icon; either from the
2622 * builting image or by loading the file
2624 if (icon_info->cache_pixbuf)
2625 source_pixbuf = g_object_ref (icon_info->cache_pixbuf);
2629 source_pixbuf = gdk_pixbuf_new_from_file (icon_info->filename,
2630 &icon_info->load_error);
2635 /* Do scale calculations that depend on the image size
2637 image_width = gdk_pixbuf_get_width (source_pixbuf);
2638 image_height = gdk_pixbuf_get_height (source_pixbuf);
2640 if (icon_info->scale < 0.0)
2642 gint image_size = MAX (image_width, image_height);
2644 icon_info->scale = icon_info->desired_size / (gdouble)image_size;
2646 icon_info->scale = 1.0;
2648 if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED)
2649 icon_info->scale = MIN (icon_info->scale, 1.0);
2652 /* We don't short-circuit out here for scale_only, since, now
2653 * we've loaded the icon, we might as well go ahead and finish
2654 * the job. This is a bit of a waste when we scale here
2655 * and never get the final pixbuf; at the cost of a bit of
2656 * extra complexity, we could keep the source pixbuf around
2657 * but not actually scale it until neede.
2660 if (icon_info->scale == 1.0)
2661 icon_info->pixbuf = source_pixbuf;
2664 icon_info->pixbuf = gdk_pixbuf_scale_simple (source_pixbuf,
2665 0.5 + image_width * icon_info->scale,
2666 0.5 + image_height * icon_info->scale,
2667 GDK_INTERP_BILINEAR);
2668 g_object_unref (source_pixbuf);
2675 * gtk_icon_info_load_icon:
2676 * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
2677 * @error: location to store error information on failure, or %NULL.
2679 * Renders an icon previously looked up in an icon theme using
2680 * gtk_icon_theme_lookup_icon(); the size will be based on the size
2681 * passed to gtk_icon_theme_lookup_icon(). Note that the resulting
2682 * pixbuf may not be exactly this size; an icon theme may have icons
2683 * that differ slightly from their nominal sizes, and in addition GTK+
2684 * will avoid scaling icons that it considers sufficiently close to the
2685 * requested size or for which the source image would have to be scaled
2686 * up too far. (This maintains sharpness.)
2688 * Return value: the rendered icon; this may be a newly created icon
2689 * or a new reference to an internal icon, so you must not modify
2690 * the icon. Use g_object_unref() to release your reference to the
2696 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
2699 g_return_val_if_fail (icon_info != NULL, NULL);
2701 g_return_val_if_fail (icon_info != NULL, NULL);
2702 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2704 icon_info_ensure_scale_and_pixbuf (icon_info, FALSE);
2706 if (icon_info->load_error)
2708 g_propagate_error (error, icon_info->load_error);
2712 return g_object_ref (icon_info->pixbuf);
2716 * gtk_icon_info_set_raw_coordinates:
2717 * @icon_info: a #GtkIconInfo
2718 * @raw_coordinates: whether the coordinates of embedded rectangles
2719 * and attached points should be returned in their original
2722 * Sets whether the coordinates returned by gtk_icon_info_get_embedded_rect()
2723 * and gtk_icon_info_get_attach_points() should be returned in their
2724 * original form as specified in the icon theme, instead of scaled
2725 * appropriately for the pixbuf returned by gtk_icon_info_load_icon().
2727 * Raw coordinates are somewhat strange; they are specified to be with
2728 * respect to the unscaled pixmap for PNG and XPM icons, but for SVG
2729 * icons, they are in a 1000x1000 coordinate space that is scaled
2730 * to the final size of the icon. You can determine if the icon is an SVG
2731 * icon by using gtk_icon_info_get_filename(), and seeing if it is non-%NULL
2732 * and ends in '.svg'.
2734 * This function is provided primarily to allow compatibility wrappers
2735 * for older API's, and is not expected to be useful for applications.
2740 gtk_icon_info_set_raw_coordinates (GtkIconInfo *icon_info,
2741 gboolean raw_coordinates)
2743 g_return_if_fail (icon_info != NULL);
2745 icon_info->raw_coordinates = raw_coordinates != FALSE;
2748 /* Scale coordinates from the icon data prior to returning
2752 icon_info_scale_point (GtkIconInfo *icon_info,
2758 if (icon_info->raw_coordinates)
2765 if (!icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2768 *x_out = 0.5 + x * icon_info->scale;
2769 *y_out = 0.5 + y * icon_info->scale;
2776 * gtk_icon_info_get_embedded_rect:
2777 * @icon_info: a #GtkIconInfo
2778 * @rectangle: #GdkRectangle in which to store embedded
2779 * rectangle coordinates; coordinates are only stored
2780 * when this function returns %TRUE.
2782 * Gets the coordinates of a rectangle within the icon
2783 * that can be used for display of information such
2784 * as a preview of the contents of a text file.
2785 * See gtk_icon_info_set_raw_coordinates() for further
2786 * information about the coordinate system.
2788 * Return value: %TRUE if the icon has an embedded rectangle
2793 gtk_icon_info_get_embedded_rect (GtkIconInfo *icon_info,
2794 GdkRectangle *rectangle)
2796 g_return_val_if_fail (icon_info != NULL, FALSE);
2798 if (icon_info->data && icon_info->data->has_embedded_rect &&
2799 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2801 gint scaled_x0, scaled_y0;
2802 gint scaled_x1, scaled_y1;
2806 icon_info_scale_point (icon_info,
2807 icon_info->data->x0, icon_info->data->y0,
2808 &scaled_x0, &scaled_y0);
2809 icon_info_scale_point (icon_info,
2810 icon_info->data->x1, icon_info->data->y1,
2811 &scaled_x1, &scaled_y1);
2813 rectangle->x = scaled_x0;
2814 rectangle->y = scaled_y0;
2815 rectangle->width = scaled_x1 - rectangle->x;
2816 rectangle->height = scaled_y1 - rectangle->y;
2826 * gtk_icon_info_get_attach_points:
2827 * @icon_info: a #GtkIconInfo
2828 * @points: location to store pointer to an array of points, or %NULL
2829 * free the array of points with g_free().
2830 * @n_points: location to store the number of points in @points, or %NULL
2832 * Fetches the set of attach points for an icon. An attach point
2833 * is a location in the icon that can be used as anchor points for attaching
2834 * emblems or overlays to the icon.
2836 * Return value: %TRUE if there are any attach points for the icon.
2841 gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
2845 g_return_val_if_fail (icon_info != NULL, FALSE);
2847 if (icon_info->data && icon_info->data->n_attach_points &&
2848 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2854 *points = g_new (GdkPoint, icon_info->data->n_attach_points);
2855 for (i = 0; i < icon_info->data->n_attach_points; i++)
2856 icon_info_scale_point (icon_info,
2857 icon_info->data->attach_points[i].x,
2858 icon_info->data->attach_points[i].y,
2864 *n_points = icon_info->data->n_attach_points;
2880 * gtk_icon_info_get_display_name:
2881 * @icon_info: a #GtkIconInfo
2883 * Gets the display name for an icon. A display name is a
2884 * string to be used in place of the icon name in a user
2885 * visible context like a list of icons.
2887 * Return value: the display name for the icon or %NULL, if
2888 * the icon doesn't have a specified display name. This value
2889 * is owned @icon_info and must not be modified or free.
2893 G_CONST_RETURN gchar *
2894 gtk_icon_info_get_display_name (GtkIconInfo *icon_info)
2896 g_return_val_if_fail (icon_info != NULL, NULL);
2898 if (icon_info->data)
2899 return icon_info->data->display_name;
2910 * gtk_icon_theme_add_builtin_icon:
2911 * @icon_name: the name of the icon to register
2912 * @size: the size at which to register the icon (different
2913 * images can be registered for the same icon name
2914 * at different sizes.)
2915 * @pixbuf: #GdkPixbuf that contains the image to use
2918 * Registers a built-in icon for icon theme lookups. The idea
2919 * of built-in icons is to allow an application or library
2920 * that uses themed icons to function requiring files to
2921 * be present in the file system. For instance, the default
2922 * images for all of GTK+'s stock icons are registered
2925 * In general, if you use gtk_icon_theme_add_builtin_icon()
2926 * you should also install the icon in the icon theme, so
2927 * that the icon is generally available.
2929 * This function will generally be used with pixbufs loaded
2930 * via gdk_pixbuf_new_from_inline().
2935 gtk_icon_theme_add_builtin_icon (const gchar *icon_name,
2939 BuiltinIcon *default_icon;
2943 g_return_if_fail (icon_name != NULL);
2944 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2946 if (!icon_theme_builtin_icons)
2947 icon_theme_builtin_icons = g_hash_table_new (g_str_hash, g_str_equal);
2949 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
2951 key = g_strdup (icon_name);
2953 key = (gpointer)icon_name; /* Won't get stored */
2955 default_icon = g_new (BuiltinIcon, 1);
2956 default_icon->size = size;
2957 default_icon->pixbuf = g_object_ref (pixbuf);
2958 icons = g_slist_prepend (icons, default_icon);
2960 /* Replaces value, leaves key untouched
2962 g_hash_table_insert (icon_theme_builtin_icons, key, icons);
2965 /* Look up a builtin icon; the min_difference_p and
2966 * has_larger_p out parameters allow us to combine
2967 * this lookup with searching through the actual directories
2968 * of the "hicolor" icon theme. See theme_lookup_icon()
2969 * for how they are used.
2971 static BuiltinIcon *
2972 find_builtin_icon (const gchar *icon_name,
2974 gint *min_difference_p,
2975 gboolean *has_larger_p)
2977 GSList *icons = NULL;
2978 gint min_difference = G_MAXINT;
2979 gboolean has_larger = FALSE;
2980 BuiltinIcon *min_icon = NULL;
2982 if (!icon_theme_builtin_icons)
2985 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
2989 BuiltinIcon *default_icon = icons->data;
2990 int min, max, difference;
2993 min = default_icon->size - 2;
2994 max = default_icon->size + 2;
2995 smaller = size < min;
2997 difference = min - size;
2998 else if (size > max)
2999 difference = size - max;
3003 if (difference == 0)
3005 min_icon = default_icon;
3011 if (difference < min_difference || smaller)
3013 min_difference = difference;
3014 min_icon = default_icon;
3015 has_larger = smaller;
3020 if (difference < min_difference && smaller)
3022 min_difference = difference;
3023 min_icon = default_icon;
3027 icons = icons->next;
3030 if (min_difference_p)
3031 *min_difference_p = min_difference;
3033 *has_larger_p = has_larger;
3039 _gtk_icon_theme_check_reload (GdkDisplay *display)
3043 GtkIconTheme *icon_theme;
3045 n_screens = gdk_display_get_n_screens (display);
3047 for (i = 0; i < n_screens; i++)
3049 screen = gdk_display_get_screen (display, i);
3051 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
3054 icon_theme->priv->check_reload = TRUE;
3055 ensure_valid_themes (icon_theme);
3056 icon_theme->priv->check_reload = FALSE;
3063 /* DLL ABI stability backward compatibility versions */
3065 #undef gtk_icon_theme_set_search_path
3068 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
3069 const gchar *path[],
3072 const gchar **utf8_path;
3075 utf8_path = g_new (const gchar *, n_elements);
3077 for (i = 0; i < n_elements; i++)
3078 utf8_path[i] = g_locale_to_utf8 (path[i], -1, NULL, NULL, NULL);
3080 gtk_icon_theme_set_search_path_utf8 (icon_theme, utf8_path, n_elements);
3082 for (i = 0; i < n_elements; i++)
3083 g_free ((gchar *) utf8_path[i]);
3088 #undef gtk_icon_theme_get_search_path
3091 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
3097 gtk_icon_theme_get_search_path_utf8 (icon_theme, path, &n);
3104 for (i = 0; i < n; i++)
3106 gchar *tem = (*path)[i];
3108 (*path)[i] = g_locale_from_utf8 ((*path)[i], -1, NULL, NULL, NULL);
3114 #undef gtk_icon_theme_append_search_path
3117 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
3120 gchar *utf8_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
3122 gtk_icon_theme_append_search_path_utf8 (icon_theme, utf8_path);
3127 #undef gtk_icon_theme_prepend_search_path
3130 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
3133 gchar *utf8_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
3135 gtk_icon_theme_prepend_search_path_utf8 (icon_theme, utf8_path);
3140 #undef gtk_icon_info_get_filename
3142 G_CONST_RETURN gchar *
3143 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
3145 g_return_val_if_fail (icon_info != NULL, NULL);
3147 return icon_info->cp_filename;
3152 #define __GTK_ICON_THEME_C__
3153 #include "gtkaliasdef.c"