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 #define WIN32_MEAN_AND_LEAN
38 #include "win32/gdkwin32.h"
39 #endif /* G_OS_WIN32 */
41 #include "gtkicontheme.h"
43 #include "gtkiconfactory.h"
44 #include "gtkiconcache.h"
45 #include "gtkbuiltincache.h"
48 #include "gtksettings.h"
49 #include "gtkprivate.h"
51 #define DEFAULT_THEME_NAME "hicolor"
56 ICON_THEME_DIR_SCALABLE,
57 ICON_THEME_DIR_THRESHOLD,
58 ICON_THEME_DIR_UNTHEMED
61 /* In reverse search order: */
65 ICON_SUFFIX_XPM = 1 << 0,
66 ICON_SUFFIX_SVG = 1 << 1,
67 ICON_SUFFIX_PNG = 1 << 2,
68 HAS_ICON_FILE = 1 << 3
72 struct _GtkIconThemePrivate
74 guint custom_theme : 1;
75 guint is_screen_singleton : 1;
76 guint pixbuf_supports_svg : 1;
77 guint themes_valid : 1;
78 guint check_reload : 1;
79 guint loading_themes : 1;
86 /* A list of all the themes needed to look up icons.
87 * In search order, without duplicates
90 GHashTable *unthemed_icons;
92 /* Note: The keys of this hashtable are owned by the
93 * themedir and unthemed hashtables.
95 GHashTable *all_icons;
97 /* GdkScreen for the icon theme (may be NULL)
101 /* time when we last stat:ed for theme changes */
105 gulong reset_styles_idle;
110 /* Information about the source
113 GLoadableIcon *loadable;
114 GSList *emblem_infos;
116 /* Cache pixbuf (if there is any) */
117 GdkPixbuf *cache_pixbuf;
121 /* Information about the directory where
122 * the source was found
124 IconThemeDirType dir_type;
128 /* Parameters influencing the scaled icon
131 guint raw_coordinates : 1;
132 guint forced_size : 1;
134 /* Cached information if we go ahead and try to load
151 /* In search order */
157 IconThemeDirType type;
172 GHashTable *icon_data;
178 char *no_svg_filename;
190 time_t mtime; /* 0 == not existing or not a dir */
195 static void gtk_icon_theme_finalize (GObject *object);
196 static void theme_dir_destroy (IconThemeDir *dir);
198 static void theme_destroy (IconTheme *theme);
199 static GtkIconInfo *theme_lookup_icon (IconTheme *theme,
200 const char *icon_name,
203 gboolean use_default_icons);
204 static void theme_list_icons (IconTheme *theme,
207 static void theme_list_contexts (IconTheme *theme,
208 GHashTable *contexts);
209 static void theme_subdir_load (GtkIconTheme *icon_theme,
211 GKeyFile *theme_file,
213 static void do_theme_change (GtkIconTheme *icon_theme);
215 static void blow_themes (GtkIconTheme *icon_themes);
216 static gboolean rescan_themes (GtkIconTheme *icon_themes);
218 static void icon_data_free (GtkIconData *icon_data);
219 static void load_icon_data (IconThemeDir *dir,
223 static IconSuffix theme_dir_get_icon_suffix (IconThemeDir *dir,
224 const gchar *icon_name,
225 gboolean *has_icon_file);
228 static GtkIconInfo *icon_info_new (void);
229 static GtkIconInfo *icon_info_new_builtin (BuiltinIcon *icon);
231 static IconSuffix suffix_from_name (const char *name);
233 static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
235 gint *min_difference_p,
236 gboolean *has_larger_p);
238 static guint signal_changed = 0;
240 static GHashTable *icon_theme_builtin_icons;
242 /* also used in gtkiconfactory.c */
243 GtkIconCache *_builtin_cache = NULL;
244 static GList *builtin_dirs = NULL;
246 G_DEFINE_TYPE (GtkIconTheme, gtk_icon_theme, G_TYPE_OBJECT)
249 * gtk_icon_theme_new:
251 * Creates a new icon theme object. Icon theme objects are used
252 * to lookup up an icon by name in a particular icon theme.
253 * Usually, you'll want to use gtk_icon_theme_get_default()
254 * or gtk_icon_theme_get_for_screen() rather than creating
255 * a new icon theme object for scratch.
257 * Return value: the newly created #GtkIconTheme object.
262 gtk_icon_theme_new (void)
264 return g_object_new (GTK_TYPE_ICON_THEME, NULL);
268 * gtk_icon_theme_get_default:
270 * Gets the icon theme for the default screen. See
271 * gtk_icon_theme_get_for_screen().
273 * Return value: (transfer none): A unique #GtkIconTheme associated with
274 * the default screen. This icon theme is associated with
275 * the screen and can be used as long as the screen
276 * is open. Do not ref or unref it.
281 gtk_icon_theme_get_default (void)
283 return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
287 * gtk_icon_theme_get_for_screen:
288 * @screen: a #GdkScreen
290 * Gets the icon theme object associated with @screen; if this
291 * function has not previously been called for the given
292 * screen, a new icon theme object will be created and
293 * associated with the screen. Icon theme objects are
294 * fairly expensive to create, so using this function
295 * is usually a better choice than calling than gtk_icon_theme_new()
296 * and setting the screen yourself; by using this function
297 * a single icon theme object will be shared between users.
299 * Return value: (transfer none): A unique #GtkIconTheme associated with
300 * the given screen. This icon theme is associated with
301 * the screen and can be used as long as the screen
302 * is open. Do not ref or unref it.
307 gtk_icon_theme_get_for_screen (GdkScreen *screen)
309 GtkIconTheme *icon_theme;
311 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
312 g_return_val_if_fail (!screen->closed, NULL);
314 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
317 GtkIconThemePrivate *priv;
319 icon_theme = gtk_icon_theme_new ();
320 gtk_icon_theme_set_screen (icon_theme, screen);
322 priv = icon_theme->priv;
323 priv->is_screen_singleton = TRUE;
325 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), icon_theme);
332 gtk_icon_theme_class_init (GtkIconThemeClass *klass)
334 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
336 gobject_class->finalize = gtk_icon_theme_finalize;
339 * GtkIconTheme::changed
340 * @icon_theme: the icon theme
342 * Emitted when the current icon theme is switched or GTK+ detects
343 * that a change has occurred in the contents of the current
346 signal_changed = g_signal_new (I_("changed"),
347 G_TYPE_FROM_CLASS (klass),
349 G_STRUCT_OFFSET (GtkIconThemeClass, changed),
351 g_cclosure_marshal_VOID__VOID,
354 g_type_class_add_private (klass, sizeof (GtkIconThemePrivate));
358 /* Callback when the display that the icon theme is attached to
359 * is closed; unset the screen, and if it's the unique theme
360 * for the screen, drop the reference
363 display_closed (GdkDisplay *display,
365 GtkIconTheme *icon_theme)
367 GtkIconThemePrivate *priv = icon_theme->priv;
368 GdkScreen *screen = priv->screen;
369 gboolean was_screen_singleton = priv->is_screen_singleton;
371 if (was_screen_singleton)
373 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), NULL);
374 priv->is_screen_singleton = FALSE;
377 gtk_icon_theme_set_screen (icon_theme, NULL);
379 if (was_screen_singleton)
381 g_object_unref (icon_theme);
386 update_current_theme (GtkIconTheme *icon_theme)
388 #define theme_changed(_old, _new) \
389 ((_old && !_new) || (!_old && _new) || \
390 (_old && _new && strcmp (_old, _new) != 0))
391 GtkIconThemePrivate *priv = icon_theme->priv;
393 if (!priv->custom_theme)
396 gchar *fallback_theme = NULL;
397 gboolean changed = FALSE;
401 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
402 g_object_get (settings,
403 "gtk-icon-theme-name", &theme,
404 "gtk-fallback-icon-theme", &fallback_theme, NULL);
407 /* ensure that the current theme (even when just the default)
408 * is searched before any fallback theme
410 if (!theme && fallback_theme)
411 theme = g_strdup (DEFAULT_THEME_NAME);
413 if (theme_changed (priv->current_theme, theme))
415 g_free (priv->current_theme);
416 priv->current_theme = theme;
422 if (theme_changed (priv->fallback_theme, fallback_theme))
424 g_free (priv->fallback_theme);
425 priv->fallback_theme = fallback_theme;
429 g_free (fallback_theme);
432 do_theme_change (icon_theme);
437 /* Callback when the icon theme GtkSetting changes
440 theme_changed (GtkSettings *settings,
442 GtkIconTheme *icon_theme)
444 update_current_theme (icon_theme);
448 unset_screen (GtkIconTheme *icon_theme)
450 GtkIconThemePrivate *priv = icon_theme->priv;
451 GtkSettings *settings;
456 settings = gtk_settings_get_for_screen (priv->screen);
457 display = gdk_screen_get_display (priv->screen);
459 g_signal_handlers_disconnect_by_func (display,
460 (gpointer) display_closed,
462 g_signal_handlers_disconnect_by_func (settings,
463 (gpointer) theme_changed,
471 * gtk_icon_theme_set_screen:
472 * @icon_theme: a #GtkIconTheme
473 * @screen: a #GdkScreen
475 * Sets the screen for an icon theme; the screen is used
476 * to track the user's currently configured icon theme,
477 * which might be different for different screens.
482 gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
485 GtkIconThemePrivate *priv;
486 GtkSettings *settings;
489 g_return_if_fail (GTK_ICON_THEME (icon_theme));
490 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
492 priv = icon_theme->priv;
494 unset_screen (icon_theme);
498 display = gdk_screen_get_display (screen);
499 settings = gtk_settings_get_for_screen (screen);
501 priv->screen = screen;
503 g_signal_connect (display, "closed",
504 G_CALLBACK (display_closed), icon_theme);
505 g_signal_connect (settings, "notify::gtk-icon-theme-name",
506 G_CALLBACK (theme_changed), icon_theme);
507 g_signal_connect (settings, "notify::gtk-fallback-icon-theme-name",
508 G_CALLBACK (theme_changed), icon_theme);
511 update_current_theme (icon_theme);
514 /* Checks whether a loader for SVG files has been registered
518 pixbuf_supports_svg (void)
522 static gint found_svg = -1;
527 formats = gdk_pixbuf_get_formats ();
530 for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
532 gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
535 for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
537 if (strcmp (*mime_type, "image/svg") == 0)
541 g_strfreev (mime_types);
544 g_slist_free (formats);
550 gtk_icon_theme_init (GtkIconTheme *icon_theme)
552 GtkIconThemePrivate *priv;
553 const gchar * const *xdg_data_dirs;
556 priv = G_TYPE_INSTANCE_GET_PRIVATE (icon_theme,
558 GtkIconThemePrivate);
559 icon_theme->priv = priv;
561 priv->custom_theme = FALSE;
563 xdg_data_dirs = g_get_system_data_dirs ();
564 for (i = 0; xdg_data_dirs[i]; i++) ;
566 priv->search_path_len = 2 * i + 2;
568 priv->search_path = g_new (char *, priv->search_path_len);
571 priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
572 priv->search_path[i++] = g_build_filename (g_get_user_data_dir (), "icons", NULL);
574 for (j = 0; xdg_data_dirs[j]; j++)
575 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
577 for (j = 0; xdg_data_dirs[j]; j++)
578 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "pixmaps", NULL);
580 priv->themes_valid = FALSE;
582 priv->unthemed_icons = NULL;
584 priv->pixbuf_supports_svg = pixbuf_supports_svg ();
588 free_dir_mtime (IconThemeDirMtime *dir_mtime)
590 if (dir_mtime->cache)
591 _gtk_icon_cache_unref (dir_mtime->cache);
593 g_free (dir_mtime->dir);
594 g_slice_free (IconThemeDirMtime, dir_mtime);
599 reset_styles_idle (gpointer user_data)
601 GtkIconTheme *icon_theme;
602 GtkIconThemePrivate *priv;
604 icon_theme = GTK_ICON_THEME (user_data);
605 priv = icon_theme->priv;
607 if (priv->screen && priv->is_screen_singleton)
609 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
610 gtk_rc_reset_styles (settings);
613 priv->reset_styles_idle = 0;
619 do_theme_change (GtkIconTheme *icon_theme)
621 GtkIconThemePrivate *priv = icon_theme->priv;
623 if (!priv->themes_valid)
627 g_print ("change to icon theme \"%s\"\n", priv->current_theme));
628 blow_themes (icon_theme);
629 g_signal_emit (icon_theme, signal_changed, 0);
631 if (!priv->reset_styles_idle)
632 priv->reset_styles_idle =
633 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2,
634 reset_styles_idle, icon_theme, NULL);
638 blow_themes (GtkIconTheme *icon_theme)
640 GtkIconThemePrivate *priv = icon_theme->priv;
642 if (priv->themes_valid)
644 g_hash_table_destroy (priv->all_icons);
645 g_list_foreach (priv->themes, (GFunc)theme_destroy, NULL);
646 g_list_free (priv->themes);
647 g_list_foreach (priv->dir_mtimes, (GFunc)free_dir_mtime, NULL);
648 g_list_free (priv->dir_mtimes);
649 g_hash_table_destroy (priv->unthemed_icons);
652 priv->unthemed_icons = NULL;
653 priv->dir_mtimes = NULL;
654 priv->all_icons = NULL;
655 priv->themes_valid = FALSE;
659 gtk_icon_theme_finalize (GObject *object)
661 GtkIconTheme *icon_theme;
662 GtkIconThemePrivate *priv;
665 icon_theme = GTK_ICON_THEME (object);
666 priv = icon_theme->priv;
668 if (priv->reset_styles_idle)
670 g_source_remove (priv->reset_styles_idle);
671 priv->reset_styles_idle = 0;
674 unset_screen (icon_theme);
676 g_free (priv->current_theme);
677 priv->current_theme = NULL;
679 for (i = 0; i < priv->search_path_len; i++)
680 g_free (priv->search_path[i]);
682 g_free (priv->search_path);
683 priv->search_path = NULL;
685 blow_themes (icon_theme);
687 G_OBJECT_CLASS (gtk_icon_theme_parent_class)->finalize (object);
691 * gtk_icon_theme_set_search_path:
692 * @icon_theme: a #GtkIconTheme
693 * @path: array of directories that are searched for icon themes
694 * @n_elements: number of elements in @path.
696 * Sets the search path for the icon theme object. When looking
697 * for an icon theme, GTK+ will search for a subdirectory of
698 * one or more of the directories in @path with the same name
699 * as the icon theme. (Themes from multiple of the path elements
700 * are combined to allow themes to be extended by adding icons
701 * in the user's home directory.)
703 * In addition if an icon found isn't found either in the current
704 * icon theme or the default icon theme, and an image file with
705 * the right name is found directly in one of the elements of
706 * @path, then that image will be used for the icon name.
707 * (This is legacy feature, and new icons should be put
708 * into the default icon theme, which is called DEFAULT_THEME_NAME,
709 * rather than directly on the icon path.)
714 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
718 GtkIconThemePrivate *priv;
721 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
723 priv = icon_theme->priv;
724 for (i = 0; i < priv->search_path_len; i++)
725 g_free (priv->search_path[i]);
727 g_free (priv->search_path);
729 priv->search_path = g_new (gchar *, n_elements);
730 priv->search_path_len = n_elements;
732 for (i = 0; i < priv->search_path_len; i++)
733 priv->search_path[i] = g_strdup (path[i]);
735 do_theme_change (icon_theme);
740 * gtk_icon_theme_get_search_path:
741 * @icon_theme: a #GtkIconTheme
742 * @path: (allow-none) (array length=n_elements) (out): location to store a list of icon theme path directories or %NULL
743 * The stored value should be freed with g_strfreev().
744 * @n_elements: location to store number of elements
747 * Gets the current search path. See gtk_icon_theme_set_search_path().
752 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
756 GtkIconThemePrivate *priv;
759 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
761 priv = icon_theme->priv;
764 *n_elements = priv->search_path_len;
768 *path = g_new (gchar *, priv->search_path_len + 1);
769 for (i = 0; i < priv->search_path_len; i++)
770 (*path)[i] = g_strdup (priv->search_path[i]);
776 * gtk_icon_theme_append_search_path:
777 * @icon_theme: a #GtkIconTheme
778 * @path: directory name to append to the icon path
780 * Appends a directory to the search path.
781 * See gtk_icon_theme_set_search_path().
786 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
789 GtkIconThemePrivate *priv;
791 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
792 g_return_if_fail (path != NULL);
794 priv = icon_theme->priv;
796 priv->search_path_len++;
798 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
799 priv->search_path[priv->search_path_len-1] = g_strdup (path);
801 do_theme_change (icon_theme);
805 * gtk_icon_theme_prepend_search_path:
806 * @icon_theme: a #GtkIconTheme
807 * @path: directory name to prepend to the icon path
809 * Prepends a directory to the search path.
810 * See gtk_icon_theme_set_search_path().
815 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
818 GtkIconThemePrivate *priv;
821 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
822 g_return_if_fail (path != NULL);
824 priv = icon_theme->priv;
826 priv->search_path_len++;
827 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
829 for (i = priv->search_path_len - 1; i > 0; i--)
830 priv->search_path[i] = priv->search_path[i - 1];
832 priv->search_path[0] = g_strdup (path);
834 do_theme_change (icon_theme);
838 * gtk_icon_theme_set_custom_theme:
839 * @icon_theme: a #GtkIconTheme
840 * @theme_name: name of icon theme to use instead of configured theme,
841 * or %NULL to unset a previously set custom theme
843 * Sets the name of the icon theme that the #GtkIconTheme object uses
844 * overriding system configuration. This function cannot be called
845 * on the icon theme objects returned from gtk_icon_theme_get_default()
846 * and gtk_icon_theme_get_for_screen().
851 gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
852 const gchar *theme_name)
854 GtkIconThemePrivate *priv;
856 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
858 priv = icon_theme->priv;
860 g_return_if_fail (!priv->is_screen_singleton);
862 if (theme_name != NULL)
864 priv->custom_theme = TRUE;
865 if (!priv->current_theme || strcmp (theme_name, priv->current_theme) != 0)
867 g_free (priv->current_theme);
868 priv->current_theme = g_strdup (theme_name);
870 do_theme_change (icon_theme);
875 if (priv->custom_theme)
877 priv->custom_theme = FALSE;
879 update_current_theme (icon_theme);
885 insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
891 GtkIconThemePrivate *priv;
892 IconTheme *theme = NULL;
894 GKeyFile *theme_file;
895 GError *error = NULL;
896 IconThemeDirMtime *dir_mtime;
897 struct stat stat_buf;
899 priv = icon_theme->priv;
901 for (l = priv->themes; l != NULL; l = l->next)
904 if (strcmp (theme->name, theme_name) == 0)
908 for (i = 0; i < priv->search_path_len; i++)
910 path = g_build_filename (priv->search_path[i],
913 dir_mtime = g_slice_new (IconThemeDirMtime);
914 dir_mtime->cache = NULL;
915 dir_mtime->dir = path;
916 if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
917 dir_mtime->mtime = stat_buf.st_mtime;
919 dir_mtime->mtime = 0;
921 priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
923 priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
926 for (i = 0; i < priv->search_path_len && !theme_file; i++)
928 path = g_build_filename (priv->search_path[i],
932 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
934 theme_file = g_key_file_new ();
935 g_key_file_set_list_separator (theme_file, ',');
936 g_key_file_load_from_file (theme_file, path, 0, &error);
939 g_key_file_free (theme_file);
941 g_error_free (error);
948 if (theme_file || strcmp (theme_name, DEFAULT_THEME_NAME) == 0)
950 theme = g_new0 (IconTheme, 1);
951 theme->name = g_strdup (theme_name);
952 priv->themes = g_list_prepend (priv->themes, theme);
955 if (theme_file == NULL)
958 theme->display_name =
959 g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
960 if (!theme->display_name)
961 g_warning ("Theme file for %s has no name\n", theme_name);
963 dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
966 g_warning ("Theme file for %s has no directories\n", theme_name);
967 priv->themes = g_list_remove (priv->themes, theme);
968 g_free (theme->name);
969 g_free (theme->display_name);
971 g_key_file_free (theme_file);
976 g_key_file_get_locale_string (theme_file,
977 "Icon Theme", "Comment",
980 g_key_file_get_string (theme_file,
981 "Icon Theme", "Example",
985 for (i = 0; dirs[i] != NULL; i++)
986 theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
990 theme->dirs = g_list_reverse (theme->dirs);
992 themes = g_key_file_get_string_list (theme_file,
999 for (i = 0; themes[i] != NULL; i++)
1000 insert_theme (icon_theme, themes[i]);
1002 g_strfreev (themes);
1005 g_key_file_free (theme_file);
1009 free_unthemed_icon (UnthemedIcon *unthemed_icon)
1011 g_free (unthemed_icon->svg_filename);
1012 g_free (unthemed_icon->no_svg_filename);
1013 g_slice_free (UnthemedIcon, unthemed_icon);
1017 strip_suffix (const char *filename)
1021 dot = strrchr (filename, '.');
1024 return g_strdup (filename);
1026 return g_strndup (filename, dot - filename);
1030 load_themes (GtkIconTheme *icon_theme)
1032 GtkIconThemePrivate *priv;
1037 UnthemedIcon *unthemed_icon;
1038 IconSuffix old_suffix, new_suffix;
1040 IconThemeDirMtime *dir_mtime;
1041 struct stat stat_buf;
1043 priv = icon_theme->priv;
1045 priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
1047 if (priv->current_theme)
1048 insert_theme (icon_theme, priv->current_theme);
1050 /* Always look in the "default" icon theme, and in a fallback theme */
1051 if (priv->fallback_theme)
1052 insert_theme (icon_theme, priv->fallback_theme);
1053 insert_theme (icon_theme, DEFAULT_THEME_NAME);
1054 priv->themes = g_list_reverse (priv->themes);
1057 priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
1058 g_free, (GDestroyNotify)free_unthemed_icon);
1060 for (base = 0; base < icon_theme->priv->search_path_len; base++)
1062 dir = icon_theme->priv->search_path[base];
1064 dir_mtime = g_slice_new (IconThemeDirMtime);
1065 priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
1067 dir_mtime->dir = g_strdup (dir);
1068 dir_mtime->mtime = 0;
1069 dir_mtime->cache = NULL;
1071 if (g_stat (dir, &stat_buf) != 0 || !S_ISDIR (stat_buf.st_mode))
1073 dir_mtime->mtime = stat_buf.st_mtime;
1075 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir);
1076 if (dir_mtime->cache != NULL)
1079 gdir = g_dir_open (dir, 0, NULL);
1083 while ((file = g_dir_read_name (gdir)))
1085 new_suffix = suffix_from_name (file);
1087 if (new_suffix != ICON_SUFFIX_NONE)
1092 abs_file = g_build_filename (dir, file, NULL);
1093 base_name = strip_suffix (file);
1095 if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
1098 if (new_suffix == ICON_SUFFIX_SVG)
1100 if (unthemed_icon->svg_filename)
1103 unthemed_icon->svg_filename = abs_file;
1107 if (unthemed_icon->no_svg_filename)
1109 old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
1110 if (new_suffix > old_suffix)
1112 g_free (unthemed_icon->no_svg_filename);
1113 unthemed_icon->no_svg_filename = abs_file;
1119 unthemed_icon->no_svg_filename = abs_file;
1126 unthemed_icon = g_slice_new0 (UnthemedIcon);
1128 if (new_suffix == ICON_SUFFIX_SVG)
1129 unthemed_icon->svg_filename = abs_file;
1131 unthemed_icon->no_svg_filename = abs_file;
1133 g_hash_table_insert (priv->unthemed_icons,
1136 g_hash_table_insert (priv->all_icons,
1144 priv->themes_valid = TRUE;
1146 g_get_current_time(&tv);
1147 priv->last_stat_time = tv.tv_sec;
1151 _gtk_icon_theme_ensure_builtin_cache (void)
1153 static gboolean initialized = FALSE;
1155 static IconThemeDir dirs[5] =
1157 { ICON_THEME_DIR_THRESHOLD, 0, 16, 16, 16, 2, NULL, "16", -1, NULL, NULL, NULL },
1158 { ICON_THEME_DIR_THRESHOLD, 0, 20, 20, 20, 2, NULL, "20", -1, NULL, NULL, NULL },
1159 { ICON_THEME_DIR_THRESHOLD, 0, 24, 24, 24, 2, NULL, "24", -1, NULL, NULL, NULL },
1160 { ICON_THEME_DIR_THRESHOLD, 0, 32, 32, 32, 2, NULL, "32", -1, NULL, NULL, NULL },
1161 { ICON_THEME_DIR_THRESHOLD, 0, 48, 48, 48, 2, NULL, "48", -1, NULL, NULL, NULL }
1169 _builtin_cache = _gtk_icon_cache_new ((gchar *)builtin_icons);
1171 for (i = 0; i < G_N_ELEMENTS (dirs); i++)
1174 dir->cache = _gtk_icon_cache_ref (_builtin_cache);
1175 dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
1177 builtin_dirs = g_list_append (builtin_dirs, dir);
1183 ensure_valid_themes (GtkIconTheme *icon_theme)
1185 GtkIconThemePrivate *priv = icon_theme->priv;
1187 gboolean was_valid = priv->themes_valid;
1189 if (priv->loading_themes)
1191 priv->loading_themes = TRUE;
1193 _gtk_icon_theme_ensure_builtin_cache ();
1195 if (priv->themes_valid)
1197 g_get_current_time (&tv);
1199 if (ABS (tv.tv_sec - priv->last_stat_time) > 5 &&
1200 rescan_themes (icon_theme))
1201 blow_themes (icon_theme);
1204 if (!priv->themes_valid)
1206 load_themes (icon_theme);
1210 g_signal_emit (icon_theme, signal_changed, 0);
1212 if (!priv->check_reload && priv->screen)
1214 static GdkAtom atom_iconthemes = GDK_NONE;
1215 GdkEvent *event = gdk_event_new (GDK_CLIENT_EVENT);
1218 if (!atom_iconthemes)
1219 atom_iconthemes = gdk_atom_intern_static_string ("_GTK_LOAD_ICONTHEMES");
1221 for (i = 0; i < 5; i++)
1222 event->client.data.l[i] = 0;
1223 event->client.data_format = 32;
1224 event->client.message_type = atom_iconthemes;
1226 gdk_screen_broadcast_client_message (priv->screen, event);
1231 priv->loading_themes = FALSE;
1234 static GtkIconInfo *
1235 choose_icon (GtkIconTheme *icon_theme,
1236 const gchar *icon_names[],
1238 GtkIconLookupFlags flags)
1240 GtkIconThemePrivate *priv;
1242 GtkIconInfo *icon_info = NULL;
1243 UnthemedIcon *unthemed_icon = NULL;
1245 gboolean use_builtin;
1248 priv = icon_theme->priv;
1250 if (flags & GTK_ICON_LOOKUP_NO_SVG)
1252 else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
1255 allow_svg = priv->pixbuf_supports_svg;
1257 use_builtin = flags & GTK_ICON_LOOKUP_USE_BUILTIN;
1259 ensure_valid_themes (icon_theme);
1261 for (l = priv->themes; l; l = l->next)
1263 IconTheme *theme = l->data;
1265 for (i = 0; icon_names[i]; i++)
1267 icon_info = theme_lookup_icon (theme, icon_names[i], size, allow_svg, use_builtin);
1273 for (i = 0; icon_names[i]; i++)
1275 unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_names[i]);
1280 /* Still not found an icon, check if reference to a Win32 resource */
1286 resources = g_strsplit (icon_names[0], ",", 0);
1289 wchar_t *wfile = g_utf8_to_utf16 (resources[0], -1, NULL, NULL, NULL);
1290 ExtractIconExW (wfile, resources[1] ? atoi (resources[1]) : 0, &hIcon, NULL, 1);
1296 icon_info = icon_info_new ();
1297 icon_info->cache_pixbuf = gdk_win32_icon_to_pixbuf_libgtk_only (hIcon);
1298 DestroyIcon (hIcon);
1299 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1300 icon_info->dir_size = size;
1302 g_strfreev (resources);
1308 icon_info = icon_info_new ();
1310 /* A SVG icon, when allowed, beats out a XPM icon, but not
1314 unthemed_icon->svg_filename &&
1315 (!unthemed_icon->no_svg_filename ||
1316 suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
1317 icon_info->filename = g_strdup (unthemed_icon->svg_filename);
1318 else if (unthemed_icon->no_svg_filename)
1319 icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);
1321 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1322 icon_info->dir_size = size;
1328 icon_info->desired_size = size;
1329 icon_info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
1333 static gboolean check_for_default_theme = TRUE;
1334 char *default_theme_path;
1335 gboolean found = FALSE;
1338 if (check_for_default_theme)
1340 check_for_default_theme = FALSE;
1342 for (i = 0; !found && i < priv->search_path_len; i++)
1344 default_theme_path = g_build_filename (priv->search_path[i],
1348 found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
1349 g_free (default_theme_path);
1354 g_warning (_("Could not find the icon '%s'. The '%s' theme\n"
1355 "was not found either, perhaps you need to install it.\n"
1356 "You can get a copy from:\n"
1358 icon_names[0], DEFAULT_THEME_NAME, "http://icon-theme.freedesktop.org/releases");
1368 * gtk_icon_theme_lookup_icon:
1369 * @icon_theme: a #GtkIconTheme
1370 * @icon_name: the name of the icon to lookup
1371 * @size: desired icon size
1372 * @flags: flags modifying the behavior of the icon lookup
1374 * Looks up a named icon and returns a structure containing
1375 * information such as the filename of the icon. The icon
1376 * can then be rendered into a pixbuf using
1377 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1378 * combines these two steps if all you need is the pixbuf.)
1380 * Return value: a #GtkIconInfo structure containing information
1381 * about the icon, or %NULL if the icon wasn't found. Free with
1382 * gtk_icon_info_free()
1387 gtk_icon_theme_lookup_icon (GtkIconTheme *icon_theme,
1388 const gchar *icon_name,
1390 GtkIconLookupFlags flags)
1394 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1395 g_return_val_if_fail (icon_name != NULL, NULL);
1396 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1397 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1399 GTK_NOTE (ICONTHEME,
1400 g_print ("gtk_icon_theme_lookup_icon %s\n", icon_name));
1402 if (flags & GTK_ICON_LOOKUP_GENERIC_FALLBACK)
1409 for (p = (gchar *) icon_name; *p; p++)
1413 names = g_new (gchar *, dashes + 2);
1414 names[0] = g_strdup (icon_name);
1415 for (i = 1; i <= dashes; i++)
1417 names[i] = g_strdup (names[i - 1]);
1418 p = strrchr (names[i], '-');
1421 names[dashes + 1] = NULL;
1423 info = choose_icon (icon_theme, (const gchar **) names, size, flags);
1429 const gchar *names[2];
1431 names[0] = icon_name;
1434 info = choose_icon (icon_theme, names, size, flags);
1441 * gtk_icon_theme_choose_icon:
1442 * @icon_theme: a #GtkIconTheme
1443 * @icon_names: %NULL-terminated array of icon names to lookup
1444 * @size: desired icon size
1445 * @flags: flags modifying the behavior of the icon lookup
1447 * Looks up a named icon and returns a structure containing
1448 * information such as the filename of the icon. The icon
1449 * can then be rendered into a pixbuf using
1450 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1451 * combines these two steps if all you need is the pixbuf.)
1453 * If @icon_names contains more than one name, this function
1454 * tries them all in the given order before falling back to
1455 * inherited icon themes.
1457 * Return value: a #GtkIconInfo structure containing information
1458 * about the icon, or %NULL if the icon wasn't found. Free with
1459 * gtk_icon_info_free()
1464 gtk_icon_theme_choose_icon (GtkIconTheme *icon_theme,
1465 const gchar *icon_names[],
1467 GtkIconLookupFlags flags)
1469 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1470 g_return_val_if_fail (icon_names != NULL, NULL);
1471 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1472 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1474 return choose_icon (icon_theme, icon_names, size, flags);
1479 gtk_icon_theme_error_quark (void)
1481 return g_quark_from_static_string ("gtk-icon-theme-error-quark");
1485 * gtk_icon_theme_load_icon:
1486 * @icon_theme: a #GtkIconTheme
1487 * @icon_name: the name of the icon to lookup
1488 * @size: the desired icon size. The resulting icon may not be
1489 * exactly this size; see gtk_icon_info_load_icon().
1490 * @flags: flags modifying the behavior of the icon lookup
1491 * @error: (allow-none): Location to store error information on failure,
1494 * Looks up an icon in an icon theme, scales it to the given size
1495 * and renders it into a pixbuf. This is a convenience function;
1496 * if more details about the icon are needed, use
1497 * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1499 * Note that you probably want to listen for icon theme changes and
1500 * update the icon. This is usually done by connecting to the
1501 * GtkWidget::style-set signal. If for some reason you do not want to
1502 * update the icon when the icon theme changes, you should consider
1503 * using gdk_pixbuf_copy() to make a private copy of the pixbuf
1504 * returned by this function. Otherwise GTK+ may need to keep the old
1505 * icon theme loaded, which would be a waste of memory.
1507 * Return value: (transfer full): the rendered icon; this may be a
1508 * newly created icon or a new reference to an internal icon, so
1509 * you must not modify the icon. Use g_object_unref() to release
1510 * your reference to the icon. %NULL if the icon isn't found.
1515 gtk_icon_theme_load_icon (GtkIconTheme *icon_theme,
1516 const gchar *icon_name,
1518 GtkIconLookupFlags flags,
1521 GtkIconInfo *icon_info;
1522 GdkPixbuf *pixbuf = NULL;
1524 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1525 g_return_val_if_fail (icon_name != NULL, NULL);
1526 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1527 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1528 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1530 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
1531 flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1534 g_set_error (error, GTK_ICON_THEME_ERROR, GTK_ICON_THEME_NOT_FOUND,
1535 _("Icon '%s' not present in theme"), icon_name);
1539 pixbuf = gtk_icon_info_load_icon (icon_info, error);
1540 gtk_icon_info_free (icon_info);
1546 * gtk_icon_theme_has_icon:
1547 * @icon_theme: a #GtkIconTheme
1548 * @icon_name: the name of an icon
1550 * Checks whether an icon theme includes an icon
1551 * for a particular name.
1553 * Return value: %TRUE if @icon_theme includes an
1554 * icon for @icon_name.
1559 gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
1560 const char *icon_name)
1562 GtkIconThemePrivate *priv;
1565 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1566 g_return_val_if_fail (icon_name != NULL, FALSE);
1568 priv = icon_theme->priv;
1570 ensure_valid_themes (icon_theme);
1572 for (l = priv->dir_mtimes; l; l = l->next)
1574 IconThemeDirMtime *dir_mtime = l->data;
1575 GtkIconCache *cache = dir_mtime->cache;
1577 if (cache && _gtk_icon_cache_has_icon (cache, icon_name))
1581 if (g_hash_table_lookup_extended (priv->all_icons,
1582 icon_name, NULL, NULL))
1585 if (_builtin_cache &&
1586 _gtk_icon_cache_has_icon (_builtin_cache, icon_name))
1589 if (icon_theme_builtin_icons &&
1590 g_hash_table_lookup_extended (icon_theme_builtin_icons,
1591 icon_name, NULL, NULL))
1598 add_size (gpointer key,
1602 gint **res_p = user_data;
1604 **res_p = GPOINTER_TO_INT (key);
1610 * gtk_icon_theme_get_icon_sizes:
1611 * @icon_theme: a #GtkIconTheme
1612 * @icon_name: the name of an icon
1614 * Returns an array of integers describing the sizes at which
1615 * the icon is available without scaling. A size of -1 means
1616 * that the icon is available in a scalable format. The array
1617 * is zero-terminated.
1619 * Return value: An newly allocated array describing the sizes at
1620 * which the icon is available. The array should be freed with g_free()
1621 * when it is no longer needed.
1626 gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
1627 const char *icon_name)
1629 GList *l, *d, *icons;
1633 GtkIconThemePrivate *priv;
1635 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1637 priv = icon_theme->priv;
1639 ensure_valid_themes (icon_theme);
1641 sizes = g_hash_table_new (g_direct_hash, g_direct_equal);
1643 for (l = priv->themes; l; l = l->next)
1645 IconTheme *theme = l->data;
1646 for (d = theme->dirs; d; d = d->next)
1648 IconThemeDir *dir = d->data;
1650 if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
1653 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1654 if (suffix != ICON_SUFFIX_NONE)
1656 if (suffix == ICON_SUFFIX_SVG)
1657 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1659 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1664 for (d = builtin_dirs; d; d = d->next)
1666 IconThemeDir *dir = d->data;
1668 if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
1671 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1672 if (suffix != ICON_SUFFIX_NONE)
1674 if (suffix == ICON_SUFFIX_SVG)
1675 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1677 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1681 if (icon_theme_builtin_icons)
1683 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
1687 BuiltinIcon *icon = icons->data;
1689 g_hash_table_insert (sizes, GINT_TO_POINTER (icon->size), NULL);
1690 icons = icons->next;
1694 r = result = g_new0 (gint, g_hash_table_size (sizes) + 1);
1696 g_hash_table_foreach (sizes, add_size, &r);
1697 g_hash_table_destroy (sizes);
1703 add_key_to_hash (gpointer key,
1707 GHashTable *hash = user_data;
1709 g_hash_table_insert (hash, key, NULL);
1713 add_key_to_list (gpointer key,
1717 GList **list = user_data;
1719 *list = g_list_prepend (*list, g_strdup (key));
1723 * gtk_icon_theme_list_icons:
1724 * @icon_theme: a #GtkIconTheme
1725 * @context: a string identifying a particular type of icon,
1726 * or %NULL to list all icons.
1728 * Lists the icons in the current icon theme. Only a subset
1729 * of the icons can be listed by providing a context string.
1730 * The set of values for the context string is system dependent,
1731 * but will typically include such values as "Applications" and
1734 * Return value: (element-type utf8) (transfer none): a #GList list holding the names of all the
1735 * icons in the theme. You must first free each element
1736 * in the list with g_free(), then free the list itself
1737 * with g_list_free().
1742 gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
1743 const char *context)
1745 GtkIconThemePrivate *priv;
1748 GQuark context_quark;
1750 priv = icon_theme->priv;
1752 ensure_valid_themes (icon_theme);
1756 context_quark = g_quark_try_string (context);
1764 icons = g_hash_table_new (g_str_hash, g_str_equal);
1769 theme_list_icons (l->data, icons, context_quark);
1773 if (context_quark == 0)
1774 g_hash_table_foreach (priv->unthemed_icons,
1780 g_hash_table_foreach (icons,
1784 g_hash_table_destroy (icons);
1790 * gtk_icon_theme_list_contexts:
1791 * @icon_theme: a #GtkIconTheme
1793 * Gets the list of contexts available within the current
1794 * hierarchy of icon themes
1796 * Return value: (element-type utf8) (transfer full): a #GList list holding the names of all the
1797 * contexts in the theme. You must first free each element
1798 * in the list with g_free(), then free the list itself
1799 * with g_list_free().
1804 gtk_icon_theme_list_contexts (GtkIconTheme *icon_theme)
1806 GtkIconThemePrivate *priv;
1807 GHashTable *contexts;
1810 priv = icon_theme->priv;
1812 ensure_valid_themes (icon_theme);
1814 contexts = g_hash_table_new (g_str_hash, g_str_equal);
1819 theme_list_contexts (l->data, contexts);
1825 g_hash_table_foreach (contexts,
1829 g_hash_table_destroy (contexts);
1835 * gtk_icon_theme_get_example_icon_name:
1836 * @icon_theme: a #GtkIconTheme
1838 * Gets the name of an icon that is representative of the
1839 * current theme (for instance, to use when presenting
1840 * a list of themes to the user.)
1842 * Return value: the name of an example icon or %NULL.
1843 * Free with g_free().
1848 gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
1850 GtkIconThemePrivate *priv;
1854 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1856 priv = icon_theme->priv;
1858 ensure_valid_themes (icon_theme);
1865 return g_strdup (theme->example);
1875 rescan_themes (GtkIconTheme *icon_theme)
1877 GtkIconThemePrivate *priv;
1878 IconThemeDirMtime *dir_mtime;
1881 struct stat stat_buf;
1884 priv = icon_theme->priv;
1886 for (d = priv->dir_mtimes; d != NULL; d = d->next)
1888 dir_mtime = d->data;
1890 stat_res = g_stat (dir_mtime->dir, &stat_buf);
1892 /* dir mtime didn't change */
1893 if (stat_res == 0 &&
1894 S_ISDIR (stat_buf.st_mode) &&
1895 dir_mtime->mtime == stat_buf.st_mtime)
1897 /* didn't exist before, and still doesn't */
1898 if (dir_mtime->mtime == 0 &&
1899 (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
1905 g_get_current_time (&tv);
1906 priv->last_stat_time = tv.tv_sec;
1912 * gtk_icon_theme_rescan_if_needed:
1913 * @icon_theme: a #GtkIconTheme
1915 * Checks to see if the icon theme has changed; if it has, any
1916 * currently cached information is discarded and will be reloaded
1917 * next time @icon_theme is accessed.
1919 * Return value: %TRUE if the icon theme has changed and needed
1925 gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
1929 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1931 retval = rescan_themes (icon_theme);
1933 do_theme_change (icon_theme);
1939 theme_destroy (IconTheme *theme)
1941 g_free (theme->display_name);
1942 g_free (theme->comment);
1943 g_free (theme->name);
1944 g_free (theme->example);
1946 g_list_foreach (theme->dirs, (GFunc)theme_dir_destroy, NULL);
1947 g_list_free (theme->dirs);
1953 theme_dir_destroy (IconThemeDir *dir)
1956 _gtk_icon_cache_unref (dir->cache);
1958 g_hash_table_destroy (dir->icons);
1961 g_hash_table_destroy (dir->icon_data);
1963 g_free (dir->subdir);
1968 theme_dir_size_difference (IconThemeDir *dir, int size, gboolean *smaller)
1973 case ICON_THEME_DIR_FIXED:
1974 *smaller = size < dir->size;
1975 return abs (size - dir->size);
1977 case ICON_THEME_DIR_SCALABLE:
1978 *smaller = size < dir->min_size;
1979 if (size < dir->min_size)
1980 return dir->min_size - size;
1981 if (size > dir->max_size)
1982 return size - dir->max_size;
1985 case ICON_THEME_DIR_THRESHOLD:
1986 min = dir->size - dir->threshold;
1987 max = dir->size + dir->threshold;
1988 *smaller = size < min;
1995 case ICON_THEME_DIR_UNTHEMED:
1996 g_assert_not_reached ();
1999 g_assert_not_reached ();
2004 string_from_suffix (IconSuffix suffix)
2008 case ICON_SUFFIX_XPM:
2010 case ICON_SUFFIX_SVG:
2012 case ICON_SUFFIX_PNG:
2015 g_assert_not_reached();
2021 suffix_from_name (const char *name)
2025 if (g_str_has_suffix (name, ".png"))
2026 retval = ICON_SUFFIX_PNG;
2027 else if (g_str_has_suffix (name, ".svg"))
2028 retval = ICON_SUFFIX_SVG;
2029 else if (g_str_has_suffix (name, ".xpm"))
2030 retval = ICON_SUFFIX_XPM;
2032 retval = ICON_SUFFIX_NONE;
2038 best_suffix (IconSuffix suffix,
2041 if ((suffix & ICON_SUFFIX_PNG) != 0)
2042 return ICON_SUFFIX_PNG;
2043 else if (allow_svg && ((suffix & ICON_SUFFIX_SVG) != 0))
2044 return ICON_SUFFIX_SVG;
2045 else if ((suffix & ICON_SUFFIX_XPM) != 0)
2046 return ICON_SUFFIX_XPM;
2048 return ICON_SUFFIX_NONE;
2053 theme_dir_get_icon_suffix (IconThemeDir *dir,
2054 const gchar *icon_name,
2055 gboolean *has_icon_file)
2061 suffix = (IconSuffix)_gtk_icon_cache_get_icon_flags (dir->cache,
2066 *has_icon_file = suffix & HAS_ICON_FILE;
2068 suffix = suffix & ~HAS_ICON_FILE;
2071 suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
2073 GTK_NOTE (ICONTHEME,
2074 g_print ("get_icon_suffix%s %u\n", dir->cache ? " (cached)" : "", suffix));
2079 static GtkIconInfo *
2080 theme_lookup_icon (IconTheme *theme,
2081 const char *icon_name,
2084 gboolean use_builtin)
2087 IconThemeDir *dir, *min_dir;
2089 int min_difference, difference;
2090 BuiltinIcon *closest_builtin = NULL;
2091 gboolean smaller, has_larger, match;
2094 min_difference = G_MAXINT;
2099 /* Builtin icons are logically part of the default theme and
2100 * are searched before other subdirectories of the default theme.
2102 if (use_builtin && strcmp (theme->name, DEFAULT_THEME_NAME) == 0)
2104 closest_builtin = find_builtin_icon (icon_name,
2109 if (min_difference == 0)
2110 return icon_info_new_builtin (closest_builtin);
2112 dirs = builtin_dirs;
2122 GTK_NOTE (ICONTHEME,
2123 g_print ("theme_lookup_icon dir %s\n", dir->dir));
2124 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
2125 if (best_suffix (suffix, allow_svg) != ICON_SUFFIX_NONE)
2127 difference = theme_dir_size_difference (dir, size, &smaller);
2129 if (difference == 0)
2131 if (dir->type == ICON_THEME_DIR_SCALABLE)
2133 /* don't pick scalable if we already found
2134 * a matching non-scalable dir
2144 /* for a matching non-scalable dir keep
2145 * going and look for a closer match
2147 difference = abs (size - dir->size);
2148 if (!match || difference < min_difference)
2151 min_difference = difference;
2154 if (difference == 0)
2163 if (difference < min_difference || smaller)
2165 min_difference = difference;
2167 has_larger = smaller;
2172 if (difference < min_difference && smaller)
2174 min_difference = difference;
2183 if (l == NULL && dirs == builtin_dirs)
2192 GtkIconInfo *icon_info = icon_info_new ();
2193 gboolean has_icon_file = FALSE;
2195 suffix = theme_dir_get_icon_suffix (min_dir, icon_name, &has_icon_file);
2196 suffix = best_suffix (suffix, allow_svg);
2197 g_assert (suffix != ICON_SUFFIX_NONE);
2201 file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
2202 icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
2207 icon_info->filename = NULL;
2210 if (min_dir->icon_data != NULL)
2211 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2213 if (icon_info->data == NULL && min_dir->cache != NULL)
2215 icon_info->data = _gtk_icon_cache_get_icon_data (min_dir->cache, icon_name, min_dir->subdir_index);
2216 if (icon_info->data)
2218 if (min_dir->icon_data == NULL)
2219 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2220 g_free, (GDestroyNotify)icon_data_free);
2222 g_hash_table_replace (min_dir->icon_data, g_strdup (icon_name), icon_info->data);
2226 if (icon_info->data == NULL && has_icon_file)
2228 gchar *icon_file_name, *icon_file_path;
2230 icon_file_name = g_strconcat (icon_name, ".icon", NULL);
2231 icon_file_path = g_build_filename (min_dir->dir, icon_file_name, NULL);
2233 if (g_file_test (icon_file_path, G_FILE_TEST_IS_REGULAR))
2235 if (min_dir->icon_data == NULL)
2236 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2237 g_free, (GDestroyNotify)icon_data_free);
2238 load_icon_data (min_dir, icon_file_path, icon_file_name);
2240 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2242 g_free (icon_file_name);
2243 g_free (icon_file_path);
2248 icon_info->cache_pixbuf = _gtk_icon_cache_get_icon (min_dir->cache, icon_name,
2249 min_dir->subdir_index);
2252 icon_info->dir_type = min_dir->type;
2253 icon_info->dir_size = min_dir->size;
2254 icon_info->threshold = min_dir->threshold;
2259 if (closest_builtin)
2260 return icon_info_new_builtin (closest_builtin);
2266 theme_list_icons (IconTheme *theme,
2270 GList *l = theme->dirs;
2277 if (context == dir->context ||
2282 _gtk_icon_cache_add_icons (dir->cache,
2289 g_hash_table_foreach (dir->icons,
2299 theme_list_contexts (IconTheme *theme,
2300 GHashTable *contexts)
2302 GList *l = theme->dirs;
2304 const char *context;
2310 context = g_quark_to_string (dir->context);
2311 g_hash_table_replace (contexts, (gpointer) context, NULL);
2318 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
2320 GKeyFile *icon_file;
2328 GError *error = NULL;
2332 icon_file = g_key_file_new ();
2333 g_key_file_set_list_separator (icon_file, ',');
2334 g_key_file_load_from_file (icon_file, path, 0, &error);
2337 g_error_free (error);
2338 g_key_file_free (icon_file);
2343 base_name = strip_suffix (name);
2345 data = g_slice_new0 (GtkIconData);
2346 g_hash_table_replace (dir->icon_data, base_name, data);
2348 ivalues = g_key_file_get_integer_list (icon_file,
2349 "Icon Data", "EmbeddedTextRectangle",
2355 data->has_embedded_rect = TRUE;
2356 data->x0 = ivalues[0];
2357 data->y0 = ivalues[1];
2358 data->x1 = ivalues[2];
2359 data->y1 = ivalues[3];
2365 str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
2368 split = g_strsplit (str, "|", -1);
2370 data->n_attach_points = g_strv_length (split);
2371 data->attach_points = g_new (GdkPoint, data->n_attach_points);
2374 while (split[i] != NULL && i < data->n_attach_points)
2376 split_point = strchr (split[i], ',');
2381 data->attach_points[i].x = atoi (split[i]);
2382 data->attach_points[i].y = atoi (split_point);
2391 data->display_name = g_key_file_get_locale_string (icon_file,
2392 "Icon Data", "DisplayName",
2394 g_key_file_free (icon_file);
2399 scan_directory (GtkIconThemePrivate *icon_theme,
2400 IconThemeDir *dir, char *full_dir)
2405 GTK_NOTE (ICONTHEME,
2406 g_print ("scanning directory %s\n", full_dir));
2407 dir->icons = g_hash_table_new_full (g_str_hash, g_str_equal,
2410 gdir = g_dir_open (full_dir, 0, NULL);
2415 while ((name = g_dir_read_name (gdir)))
2419 IconSuffix suffix, hash_suffix;
2421 if (g_str_has_suffix (name, ".icon"))
2423 if (dir->icon_data == NULL)
2424 dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2425 g_free, (GDestroyNotify)icon_data_free);
2427 path = g_build_filename (full_dir, name, NULL);
2428 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
2429 load_icon_data (dir, path, name);
2436 suffix = suffix_from_name (name);
2437 if (suffix == ICON_SUFFIX_NONE)
2440 base_name = strip_suffix (name);
2442 hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
2443 g_hash_table_replace (icon_theme->all_icons, base_name, NULL);
2444 g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
2451 theme_subdir_load (GtkIconTheme *icon_theme,
2453 GKeyFile *theme_file,
2459 IconThemeDirType type;
2460 char *context_string;
2467 GError *error = NULL;
2468 IconThemeDirMtime *dir_mtime;
2470 size = g_key_file_get_integer (theme_file, subdir, "Size", &error);
2473 g_error_free (error);
2474 g_warning ("Theme directory %s of theme %s has no size field\n",
2475 subdir, theme->name);
2479 type = ICON_THEME_DIR_THRESHOLD;
2480 type_string = g_key_file_get_string (theme_file, subdir, "Type", NULL);
2483 if (strcmp (type_string, "Fixed") == 0)
2484 type = ICON_THEME_DIR_FIXED;
2485 else if (strcmp (type_string, "Scalable") == 0)
2486 type = ICON_THEME_DIR_SCALABLE;
2487 else if (strcmp (type_string, "Threshold") == 0)
2488 type = ICON_THEME_DIR_THRESHOLD;
2490 g_free (type_string);
2494 context_string = g_key_file_get_string (theme_file, subdir, "Context", NULL);
2497 context = g_quark_from_string (context_string);
2498 g_free (context_string);
2501 max_size = g_key_file_get_integer (theme_file, subdir, "MaxSize", &error);
2506 g_error_free (error);
2510 min_size = g_key_file_get_integer (theme_file, subdir, "MinSize", &error);
2515 g_error_free (error);
2519 threshold = g_key_file_get_integer (theme_file, subdir, "Threshold", &error);
2524 g_error_free (error);
2528 for (d = icon_theme->priv->dir_mtimes; d; d = d->next)
2530 dir_mtime = (IconThemeDirMtime *)d->data;
2532 if (dir_mtime->mtime == 0)
2533 continue; /* directory doesn't exist */
2535 full_dir = g_build_filename (dir_mtime->dir, subdir, NULL);
2537 /* First, see if we have a cache for the directory */
2538 if (dir_mtime->cache != NULL || g_file_test (full_dir, G_FILE_TEST_IS_DIR))
2540 if (dir_mtime->cache == NULL)
2542 /* This will return NULL if the cache doesn't exist or is outdated */
2543 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir_mtime->dir);
2546 dir = g_new (IconThemeDir, 1);
2548 dir->context = context;
2550 dir->min_size = min_size;
2551 dir->max_size = max_size;
2552 dir->threshold = threshold;
2553 dir->dir = full_dir;
2554 dir->icon_data = NULL;
2555 dir->subdir = g_strdup (subdir);
2556 if (dir_mtime->cache != NULL)
2558 dir->cache = _gtk_icon_cache_ref (dir_mtime->cache);
2559 dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
2564 dir->subdir_index = -1;
2565 scan_directory (icon_theme->priv, dir, full_dir);
2568 theme->dirs = g_list_prepend (theme->dirs, dir);
2576 icon_data_free (GtkIconData *icon_data)
2578 g_free (icon_data->attach_points);
2579 g_free (icon_data->display_name);
2580 g_slice_free (GtkIconData, icon_data);
2587 G_DEFINE_BOXED_TYPE (GtkIconInfo, gtk_icon_info,
2591 static GtkIconInfo *
2592 icon_info_new (void)
2594 GtkIconInfo *icon_info = g_slice_new0 (GtkIconInfo);
2596 icon_info->scale = -1.;
2597 icon_info->ref_count = 1;
2602 static GtkIconInfo *
2603 icon_info_new_builtin (BuiltinIcon *icon)
2605 GtkIconInfo *icon_info = icon_info_new ();
2607 icon_info->cache_pixbuf = g_object_ref (icon->pixbuf);
2608 icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
2609 icon_info->dir_size = icon->size;
2610 icon_info->threshold = 2;
2616 * gtk_icon_info_copy:
2617 * @icon_info: a #GtkIconInfo
2619 * Make a copy of a #GtkIconInfo.
2621 * Return value: the new GtkIconInfo
2626 gtk_icon_info_copy (GtkIconInfo *icon_info)
2629 g_return_val_if_fail (icon_info != NULL, NULL);
2631 icon_info->ref_count++;
2637 * gtk_icon_info_free:
2638 * @icon_info: a #GtkIconInfo
2640 * Free a #GtkIconInfo and associated information
2645 gtk_icon_info_free (GtkIconInfo *icon_info)
2647 g_return_if_fail (icon_info != NULL);
2649 icon_info->ref_count--;
2650 if (icon_info->ref_count > 0)
2653 g_free (icon_info->filename);
2654 if (icon_info->loadable)
2655 g_object_unref (icon_info->loadable);
2656 g_slist_foreach (icon_info->emblem_infos, (GFunc)gtk_icon_info_free, NULL);
2657 g_slist_free (icon_info->emblem_infos);
2658 if (icon_info->pixbuf)
2659 g_object_unref (icon_info->pixbuf);
2660 if (icon_info->cache_pixbuf)
2661 g_object_unref (icon_info->cache_pixbuf);
2663 g_slice_free (GtkIconInfo, icon_info);
2667 * gtk_icon_info_get_base_size:
2668 * @icon_info: a #GtkIconInfo
2670 * Gets the base size for the icon. The base size
2671 * is a size for the icon that was specified by
2672 * the icon theme creator. This may be different
2673 * than the actual size of image; an example of
2674 * this is small emblem icons that can be attached
2675 * to a larger icon. These icons will be given
2676 * the same base size as the larger icons to which
2677 * they are attached.
2679 * Return value: the base size, or 0, if no base
2680 * size is known for the icon.
2685 gtk_icon_info_get_base_size (GtkIconInfo *icon_info)
2687 g_return_val_if_fail (icon_info != NULL, 0);
2689 return icon_info->dir_size;
2693 * gtk_icon_info_get_filename:
2694 * @icon_info: a #GtkIconInfo
2696 * Gets the filename for the icon. If the
2697 * %GTK_ICON_LOOKUP_USE_BUILTIN flag was passed
2698 * to gtk_icon_theme_lookup_icon(), there may be
2699 * no filename if a builtin icon is returned; in this
2700 * case, you should use gtk_icon_info_get_builtin_pixbuf().
2702 * Return value: the filename for the icon, or %NULL
2703 * if gtk_icon_info_get_builtin_pixbuf() should
2704 * be used instead. The return value is owned by
2705 * GTK+ and should not be modified or freed.
2709 G_CONST_RETURN gchar *
2710 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2712 g_return_val_if_fail (icon_info != NULL, NULL);
2714 return icon_info->filename;
2718 * gtk_icon_info_get_builtin_pixbuf:
2719 * @icon_info: a #GtkIconInfo structure
2721 * Gets the built-in image for this icon, if any. To allow
2722 * GTK+ to use built in icon images, you must pass the
2723 * %GTK_ICON_LOOKUP_USE_BUILTIN to
2724 * gtk_icon_theme_lookup_icon().
2726 * Return value: (transfer none): the built-in image pixbuf, or %NULL. No
2727 * extra reference is added to the returned pixbuf, so if
2728 * you want to keep it around, you must use g_object_ref().
2729 * The returned image must not be modified.
2734 gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
2736 g_return_val_if_fail (icon_info != NULL, NULL);
2738 if (icon_info->filename)
2741 return icon_info->cache_pixbuf;
2744 static gboolean icon_info_ensure_scale_and_pixbuf (GtkIconInfo*, gboolean);
2746 /* Combine the icon with all emblems, the first emblem is placed
2747 * in the southeast corner. Scale emblems to be at most 3/4 of the
2748 * size of the icon itself.
2751 apply_emblems (GtkIconInfo *info)
2753 GdkPixbuf *icon = NULL;
2757 if (info->emblem_infos == NULL)
2760 w = gdk_pixbuf_get_width (info->pixbuf);
2761 h = gdk_pixbuf_get_height (info->pixbuf);
2763 for (l = info->emblem_infos, pos = 0; l; l = l->next, pos++)
2765 GtkIconInfo *emblem_info = l->data;
2767 if (icon_info_ensure_scale_and_pixbuf (emblem_info, FALSE))
2769 GdkPixbuf *emblem = emblem_info->pixbuf;
2771 gint x = 0, y = 0; /* silence compiler */
2774 ew = gdk_pixbuf_get_width (emblem);
2775 eh = gdk_pixbuf_get_height (emblem);
2807 icon = gdk_pixbuf_copy (info->pixbuf);
2812 gdk_pixbuf_composite (emblem, icon, x, y, ew, eh, x, y,
2813 scale, scale, GDK_INTERP_BILINEAR, 255);
2819 g_object_unref (info->pixbuf);
2820 info->pixbuf = icon;
2824 /* This function contains the complicated logic for deciding
2825 * on the size at which to load the icon and loading it at
2829 icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
2830 gboolean scale_only)
2832 int image_width, image_height;
2833 GdkPixbuf *source_pixbuf;
2836 /* First check if we already succeeded have the necessary
2837 * information (or failed earlier)
2839 if (scale_only && icon_info->scale >= 0)
2842 if (icon_info->pixbuf)
2845 if (icon_info->load_error)
2848 /* SVG icons are a special case - we just immediately scale them
2849 * to the desired size
2851 if (icon_info->filename && !icon_info->loadable)
2855 file = g_file_new_for_path (icon_info->filename);
2856 icon_info->loadable = G_LOADABLE_ICON (g_file_icon_new (file));
2857 g_object_unref (file);
2861 if (G_IS_FILE_ICON (icon_info->loadable))
2864 GFileInfo *file_info;
2865 const gchar *content_type;
2867 file = g_file_icon_get_file (G_FILE_ICON (icon_info->loadable));
2868 file_info = g_file_query_info (file,
2869 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
2870 G_FILE_QUERY_INFO_NONE,
2874 content_type = g_file_info_get_content_type (file_info);
2876 if (content_type && strcmp (content_type, "image/svg+xml") == 0)
2879 g_object_unref (file_info);
2885 GInputStream *stream;
2887 icon_info->scale = icon_info->desired_size / 1000.;
2892 stream = g_loadable_icon_load (icon_info->loadable,
2893 icon_info->desired_size,
2895 &icon_info->load_error);
2898 icon_info->pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
2899 icon_info->desired_size,
2900 icon_info->desired_size,
2903 &icon_info->load_error);
2904 g_object_unref (stream);
2907 if (!icon_info->pixbuf)
2910 apply_emblems (icon_info);
2915 /* In many cases, the scale can be determined without actual access
2916 * to the icon file. This is generally true when we have a size
2917 * for the directory where the icon is; the image size doesn't
2918 * matter in that case.
2920 if (icon_info->forced_size)
2921 icon_info->scale = -1;
2922 else if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
2923 icon_info->scale = 1.0;
2924 else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
2926 if (icon_info->desired_size >= icon_info->dir_size - icon_info->threshold &&
2927 icon_info->desired_size <= icon_info->dir_size + icon_info->threshold)
2928 icon_info->scale = 1.0;
2929 else if (icon_info->dir_size > 0)
2930 icon_info->scale =(gdouble) icon_info->desired_size / icon_info->dir_size;
2932 else if (icon_info->dir_type == ICON_THEME_DIR_SCALABLE)
2934 if (icon_info->dir_size > 0)
2935 icon_info->scale = (gdouble) icon_info->desired_size / icon_info->dir_size;
2938 if (icon_info->scale >= 0. && scale_only)
2941 /* At this point, we need to actually get the icon; either from the
2942 * builtin image or by loading the file
2944 source_pixbuf = NULL;
2945 if (icon_info->cache_pixbuf)
2946 source_pixbuf = g_object_ref (icon_info->cache_pixbuf);
2949 GInputStream *stream;
2951 stream = g_loadable_icon_load (icon_info->loadable,
2952 icon_info->desired_size,
2954 &icon_info->load_error);
2957 source_pixbuf = gdk_pixbuf_new_from_stream (stream,
2959 &icon_info->load_error);
2960 g_object_unref (stream);
2967 /* Do scale calculations that depend on the image size
2969 image_width = gdk_pixbuf_get_width (source_pixbuf);
2970 image_height = gdk_pixbuf_get_height (source_pixbuf);
2972 if (icon_info->scale < 0.0)
2974 gint image_size = MAX (image_width, image_height);
2976 icon_info->scale = (gdouble)icon_info->desired_size / (gdouble)image_size;
2978 icon_info->scale = 1.0;
2980 if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED &&
2981 !icon_info->forced_size)
2982 icon_info->scale = MIN (icon_info->scale, 1.0);
2985 /* We don't short-circuit out here for scale_only, since, now
2986 * we've loaded the icon, we might as well go ahead and finish
2987 * the job. This is a bit of a waste when we scale here
2988 * and never get the final pixbuf; at the cost of a bit of
2989 * extra complexity, we could keep the source pixbuf around
2990 * but not actually scale it until needed.
2992 if (icon_info->scale == 1.0)
2993 icon_info->pixbuf = source_pixbuf;
2996 icon_info->pixbuf = gdk_pixbuf_scale_simple (source_pixbuf,
2997 0.5 + image_width * icon_info->scale,
2998 0.5 + image_height * icon_info->scale,
2999 GDK_INTERP_BILINEAR);
3000 g_object_unref (source_pixbuf);
3003 apply_emblems (icon_info);
3009 * gtk_icon_info_load_icon:
3010 * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
3011 * @error: (allow-none): location to store error information on failure,
3014 * Renders an icon previously looked up in an icon theme using
3015 * gtk_icon_theme_lookup_icon(); the size will be based on the size
3016 * passed to gtk_icon_theme_lookup_icon(). Note that the resulting
3017 * pixbuf may not be exactly this size; an icon theme may have icons
3018 * that differ slightly from their nominal sizes, and in addition GTK+
3019 * will avoid scaling icons that it considers sufficiently close to the
3020 * requested size or for which the source image would have to be scaled
3021 * up too far. (This maintains sharpness.). This behaviour can be changed
3022 * by passing the %GTK_ICON_LOOKUP_FORCE_SIZE flag when obtaining
3023 * the #GtkIconInfo. If this flag has been specified, the pixbuf
3024 * returned by this function will be scaled to the exact size.
3026 * Return value: (transfer full): the rendered icon; this may be a newly
3027 * created icon or a new reference to an internal icon, so you must
3028 * not modify the icon. Use g_object_unref() to release your reference
3034 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
3037 g_return_val_if_fail (icon_info != NULL, NULL);
3038 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
3040 if (!icon_info_ensure_scale_and_pixbuf (icon_info, FALSE))
3042 if (icon_info->load_error)
3043 g_propagate_error (error, icon_info->load_error);
3045 g_set_error_literal (error,
3046 GTK_ICON_THEME_ERROR,
3047 GTK_ICON_THEME_NOT_FOUND,
3048 _("Failed to load icon"));
3053 return g_object_ref (icon_info->pixbuf);
3057 gdk_color_to_css (GdkColor *color)
3059 return g_strdup_printf ("rgb(%d,%d,%d)",
3066 _gtk_icon_info_load_symbolic_internal (GtkIconInfo *icon_info,
3067 const gchar *css_fg,
3068 const gchar *css_success,
3069 const gchar *css_warning,
3070 const gchar *css_error,
3073 GInputStream *stream;
3077 data = g_strconcat ("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
3078 "<svg version=\"1.1\"\n"
3079 " xmlns=\"http://www.w3.org/2000/svg\"\n"
3080 " xmlns:xi=\"http://www.w3.org/2001/XInclude\"\n"
3083 " <style type=\"text/css\">\n"
3085 " fill: ", css_fg," !important;\n"
3088 " fill: ", css_warning," !important;\n"
3091 " fill: ", css_error," !important;\n"
3094 " fill: ", css_success," !important;\n"
3097 " <xi:include href=\"", icon_info->filename, "\"/>\n"
3101 stream = g_memory_input_stream_new_from_data (data, -1, g_free);
3102 pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
3103 icon_info->desired_size,
3104 icon_info->desired_size,
3108 g_object_unref (stream);
3114 * gtk_icon_info_load_symbolic:
3115 * @icon_info: a #GtkIconInfo
3116 * @fg: a #GdkRGBA representing the foreground color of the icon
3117 * @success_color: (allow-none): a #GdkRGBA representing the warning color
3118 * of the icon or %NULL to use the default color
3119 * @warning_color: (allow-none): a #GdkRGBA representing the warning color
3120 * of the icon or %NULL to use the default color
3121 * @error_color: (allow-none): a #GdkRGBA representing the error color
3122 * of the icon or %NULL to use the default color (allow-none)
3123 * @was_symbolic: (allow-none): a #gboolean, returns whether the loaded icon
3124 * was a symbolic one and whether the @fg color was applied to it.
3125 * @error: (allow-none): location to store error information on failure,
3128 * Loads an icon, modifying it to match the system colours for the foreground,
3129 * success, warning and error colors provided. If the icon is not a symbolic
3130 * one, the function will return the result from gtk_icon_info_load_icon().
3132 * This allows loading symbolic icons that will match the system theme.
3134 * Unless you are implementing a widget, you will want to use
3135 * g_themed_icon_new_with_default_fallbacks() to load the icon.
3137 * As implementation details, the icon loaded needs to be of SVG type,
3138 * contain the "symbolic" term as the last component of the icon name,
3139 * and use the 'fg', 'success', 'warning' and 'error' CSS styles in the
3142 * See the <ulink url="http://www.freedesktop.org/wiki/SymbolicIcons">Symbolic Icons spec</ulink>
3143 * for more information about symbolic icons.
3145 * Return value: (transfer full): a #GdkPixbuf representing the loaded icon
3150 gtk_icon_info_load_symbolic (GtkIconInfo *icon_info,
3152 GdkRGBA *success_color,
3153 GdkRGBA *warning_color,
3154 GdkRGBA *error_color,
3155 gboolean *was_symbolic,
3164 g_return_val_if_fail (fg != NULL, NULL);
3166 if (!icon_info->filename ||
3167 !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3170 *was_symbolic = FALSE;
3171 return gtk_icon_info_load_icon (icon_info, error);
3175 *was_symbolic = TRUE;
3177 css_fg = gdk_rgba_to_string (fg);
3180 GdkColor warning_default_color = { 0, 0xf500, 0x7900, 0x3e00 };
3181 css_warning = gdk_color_to_css (&warning_default_color);
3184 css_warning = gdk_rgba_to_string (warning_color);
3188 GdkColor error_default_color = { 0, 0xcc00, 0x0000, 0x0000 };
3189 css_error = gdk_color_to_css (&error_default_color);
3192 css_error = gdk_rgba_to_string (error_color);
3196 GdkColor success_default_color = { 0, 0x4e00, 0x9a00, 0x0600 };
3197 css_success = gdk_color_to_css (&success_default_color);
3200 css_success = gdk_rgba_to_string (success_color);
3202 pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
3203 css_fg, css_success,
3204 css_warning, css_error,
3207 g_free (css_warning);
3208 g_free (css_success);
3215 * gtk_icon_info_load_symbolic_for_style:
3216 * @icon_info: a #GtkIconInfo
3217 * @style: a #GtkStyle to take the colors from
3218 * @state: the widget state to use for colors
3219 * @was_symbolic: (allow-none): a #gboolean, returns whether the loaded icon
3220 * was a symbolic one and whether the @fg color was applied to it.
3221 * @error: (allow-none): location to store error information on failure,
3224 * Loads an icon, modifying it to match the system colours for the foreground,
3225 * success, warning and error colors provided. If the icon is not a symbolic
3226 * one, the function will return the result from gtk_icon_info_load_icon().
3228 * This allows loading symbolic icons that will match the system theme.
3230 * See gtk_icon_info_load_symbolic() for more details.
3232 * Return value: (transfer full): a #GdkPixbuf representing the loaded icon
3237 gtk_icon_info_load_symbolic_for_style (GtkIconInfo *icon_info,
3240 gboolean *was_symbolic,
3244 GdkColor success_color;
3245 GdkColor warning_color;
3246 GdkColor error_color;
3248 GdkColor *success = NULL;
3249 GdkColor *warning = NULL;
3250 GdkColor *err = NULL;
3251 gchar *css_fg, *css_success;
3252 gchar *css_warning, *css_error;
3254 if (!icon_info->filename ||
3255 !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3258 *was_symbolic = FALSE;
3259 return gtk_icon_info_load_icon (icon_info, error);
3263 *was_symbolic = TRUE;
3265 fg = &style->fg[state];
3266 if (gtk_style_lookup_color (style, "success_color", &success_color))
3267 success = &success_color;
3268 if (gtk_style_lookup_color (style, "warning_color", &warning_color))
3269 warning = &warning_color;
3270 if (gtk_style_lookup_color (style, "error_color", &error_color))
3273 css_fg = gdk_color_to_css (fg);
3274 css_success = gdk_color_to_css (success);
3275 css_warning = gdk_color_to_css (warning);
3276 css_error = gdk_color_to_css (err);
3278 pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
3279 css_fg, css_success,
3280 css_warning, css_error,
3284 g_free (css_success);
3285 g_free (css_warning);
3292 * gtk_icon_info_set_raw_coordinates:
3293 * @icon_info: a #GtkIconInfo
3294 * @raw_coordinates: whether the coordinates of embedded rectangles
3295 * and attached points should be returned in their original
3298 * Sets whether the coordinates returned by gtk_icon_info_get_embedded_rect()
3299 * and gtk_icon_info_get_attach_points() should be returned in their
3300 * original form as specified in the icon theme, instead of scaled
3301 * appropriately for the pixbuf returned by gtk_icon_info_load_icon().
3303 * Raw coordinates are somewhat strange; they are specified to be with
3304 * respect to the unscaled pixmap for PNG and XPM icons, but for SVG
3305 * icons, they are in a 1000x1000 coordinate space that is scaled
3306 * to the final size of the icon. You can determine if the icon is an SVG
3307 * icon by using gtk_icon_info_get_filename(), and seeing if it is non-%NULL
3308 * and ends in '.svg'.
3310 * This function is provided primarily to allow compatibility wrappers
3311 * for older API's, and is not expected to be useful for applications.
3316 gtk_icon_info_set_raw_coordinates (GtkIconInfo *icon_info,
3317 gboolean raw_coordinates)
3319 g_return_if_fail (icon_info != NULL);
3321 icon_info->raw_coordinates = raw_coordinates != FALSE;
3324 /* Scale coordinates from the icon data prior to returning
3328 icon_info_scale_point (GtkIconInfo *icon_info,
3334 if (icon_info->raw_coordinates)
3341 if (!icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3344 *x_out = 0.5 + x * icon_info->scale;
3345 *y_out = 0.5 + y * icon_info->scale;
3352 * gtk_icon_info_get_embedded_rect:
3353 * @icon_info: a #GtkIconInfo
3354 * @rectangle: #GdkRectangle in which to store embedded
3355 * rectangle coordinates; coordinates are only stored
3356 * when this function returns %TRUE.
3358 * Gets the coordinates of a rectangle within the icon
3359 * that can be used for display of information such
3360 * as a preview of the contents of a text file.
3361 * See gtk_icon_info_set_raw_coordinates() for further
3362 * information about the coordinate system.
3364 * Return value: %TRUE if the icon has an embedded rectangle
3369 gtk_icon_info_get_embedded_rect (GtkIconInfo *icon_info,
3370 GdkRectangle *rectangle)
3372 g_return_val_if_fail (icon_info != NULL, FALSE);
3374 if (icon_info->data && icon_info->data->has_embedded_rect &&
3375 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3377 gint scaled_x0, scaled_y0;
3378 gint scaled_x1, scaled_y1;
3382 icon_info_scale_point (icon_info,
3383 icon_info->data->x0, icon_info->data->y0,
3384 &scaled_x0, &scaled_y0);
3385 icon_info_scale_point (icon_info,
3386 icon_info->data->x1, icon_info->data->y1,
3387 &scaled_x1, &scaled_y1);
3389 rectangle->x = scaled_x0;
3390 rectangle->y = scaled_y0;
3391 rectangle->width = scaled_x1 - rectangle->x;
3392 rectangle->height = scaled_y1 - rectangle->y;
3402 * gtk_icon_info_get_attach_points:
3403 * @icon_info: a #GtkIconInfo
3404 * @points: (allow-none) (array length=n_points) (out): location to store pointer to an array of points, or %NULL
3405 * free the array of points with g_free().
3406 * @n_points: (allow-none): location to store the number of points in @points, or %NULL
3408 * Fetches the set of attach points for an icon. An attach point
3409 * is a location in the icon that can be used as anchor points for attaching
3410 * emblems or overlays to the icon.
3412 * Return value: %TRUE if there are any attach points for the icon.
3417 gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
3421 g_return_val_if_fail (icon_info != NULL, FALSE);
3423 if (icon_info->data && icon_info->data->n_attach_points &&
3424 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3430 *points = g_new (GdkPoint, icon_info->data->n_attach_points);
3431 for (i = 0; i < icon_info->data->n_attach_points; i++)
3432 icon_info_scale_point (icon_info,
3433 icon_info->data->attach_points[i].x,
3434 icon_info->data->attach_points[i].y,
3440 *n_points = icon_info->data->n_attach_points;
3456 * gtk_icon_info_get_display_name:
3457 * @icon_info: a #GtkIconInfo
3459 * Gets the display name for an icon. A display name is a
3460 * string to be used in place of the icon name in a user
3461 * visible context like a list of icons.
3463 * Return value: the display name for the icon or %NULL, if
3464 * the icon doesn't have a specified display name. This value
3465 * is owned @icon_info and must not be modified or free.
3469 G_CONST_RETURN gchar *
3470 gtk_icon_info_get_display_name (GtkIconInfo *icon_info)
3472 g_return_val_if_fail (icon_info != NULL, NULL);
3474 if (icon_info->data)
3475 return icon_info->data->display_name;
3486 * gtk_icon_theme_add_builtin_icon:
3487 * @icon_name: the name of the icon to register
3488 * @size: the size at which to register the icon (different
3489 * images can be registered for the same icon name
3490 * at different sizes.)
3491 * @pixbuf: #GdkPixbuf that contains the image to use
3494 * Registers a built-in icon for icon theme lookups. The idea
3495 * of built-in icons is to allow an application or library
3496 * that uses themed icons to function requiring files to
3497 * be present in the file system. For instance, the default
3498 * images for all of GTK+'s stock icons are registered
3501 * In general, if you use gtk_icon_theme_add_builtin_icon()
3502 * you should also install the icon in the icon theme, so
3503 * that the icon is generally available.
3505 * This function will generally be used with pixbufs loaded
3506 * via gdk_pixbuf_new_from_inline().
3511 gtk_icon_theme_add_builtin_icon (const gchar *icon_name,
3515 BuiltinIcon *default_icon;
3519 g_return_if_fail (icon_name != NULL);
3520 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3522 if (!icon_theme_builtin_icons)
3523 icon_theme_builtin_icons = g_hash_table_new (g_str_hash, g_str_equal);
3525 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
3527 key = g_strdup (icon_name);
3529 key = (gpointer)icon_name; /* Won't get stored */
3531 default_icon = g_new (BuiltinIcon, 1);
3532 default_icon->size = size;
3533 default_icon->pixbuf = g_object_ref (pixbuf);
3534 icons = g_slist_prepend (icons, default_icon);
3536 /* Replaces value, leaves key untouched
3538 g_hash_table_insert (icon_theme_builtin_icons, key, icons);
3541 /* Look up a builtin icon; the min_difference_p and
3542 * has_larger_p out parameters allow us to combine
3543 * this lookup with searching through the actual directories
3544 * of the "hicolor" icon theme. See theme_lookup_icon()
3545 * for how they are used.
3547 static BuiltinIcon *
3548 find_builtin_icon (const gchar *icon_name,
3550 gint *min_difference_p,
3551 gboolean *has_larger_p)
3553 GSList *icons = NULL;
3554 gint min_difference = G_MAXINT;
3555 gboolean has_larger = FALSE;
3556 BuiltinIcon *min_icon = NULL;
3558 if (!icon_theme_builtin_icons)
3561 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
3565 BuiltinIcon *default_icon = icons->data;
3566 int min, max, difference;
3569 min = default_icon->size - 2;
3570 max = default_icon->size + 2;
3571 smaller = size < min;
3573 difference = min - size;
3574 else if (size > max)
3575 difference = size - max;
3579 if (difference == 0)
3581 min_icon = default_icon;
3587 if (difference < min_difference || smaller)
3589 min_difference = difference;
3590 min_icon = default_icon;
3591 has_larger = smaller;
3596 if (difference < min_difference && smaller)
3598 min_difference = difference;
3599 min_icon = default_icon;
3603 icons = icons->next;
3606 if (min_difference_p)
3607 *min_difference_p = min_difference;
3609 *has_larger_p = has_larger;
3615 _gtk_icon_theme_check_reload (GdkDisplay *display)
3619 GtkIconTheme *icon_theme;
3621 n_screens = gdk_display_get_n_screens (display);
3623 for (i = 0; i < n_screens; i++)
3625 screen = gdk_display_get_screen (display, i);
3627 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
3630 icon_theme->priv->check_reload = TRUE;
3631 ensure_valid_themes (icon_theme);
3632 icon_theme->priv->check_reload = FALSE;
3639 * gtk_icon_theme_lookup_by_gicon:
3640 * @icon_theme: a #GtkIconTheme
3641 * @icon: the #GIcon to look up
3642 * @size: desired icon size
3643 * @flags: flags modifying the behavior of the icon lookup
3645 * Looks up an icon and returns a structure containing
3646 * information such as the filename of the icon.
3647 * The icon can then be rendered into a pixbuf using
3648 * gtk_icon_info_load_icon().
3650 * Return value: a #GtkIconInfo structure containing
3651 * information about the icon, or %NULL if the icon
3652 * wasn't found. Free with gtk_icon_info_free()
3657 gtk_icon_theme_lookup_by_gicon (GtkIconTheme *icon_theme,
3660 GtkIconLookupFlags flags)
3664 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
3665 g_return_val_if_fail (G_IS_ICON (icon), NULL);
3667 if (G_IS_LOADABLE_ICON (icon))
3669 info = icon_info_new ();
3670 info->loadable = G_LOADABLE_ICON (g_object_ref (icon));
3672 info->dir_type = ICON_THEME_DIR_UNTHEMED;
3673 info->dir_size = size;
3674 info->desired_size = size;
3675 info->threshold = 2;
3676 info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
3680 else if (G_IS_THEMED_ICON (icon))
3682 const gchar **names;
3684 names = (const gchar **)g_themed_icon_get_names (G_THEMED_ICON (icon));
3685 info = gtk_icon_theme_choose_icon (icon_theme, names, size, flags);
3689 else if (G_IS_EMBLEMED_ICON (icon))
3691 GIcon *base, *emblem;
3693 GtkIconInfo *emblem_info;
3695 base = g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon));
3696 info = gtk_icon_theme_lookup_by_gicon (icon_theme, base, size, flags);
3699 list = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon));
3700 for (l = list; l; l = l->next)
3702 emblem = g_emblem_get_icon (G_EMBLEM (l->data));
3703 emblem_info = gtk_icon_theme_lookup_by_gicon (icon_theme, emblem, size / 2, flags);
3705 info->emblem_infos = g_slist_prepend (info->emblem_infos, emblem_info);
3716 * gtk_icon_info_new_for_pixbuf:
3717 * @icon_theme: a #GtkIconTheme
3718 * @pixbuf: the pixbuf to wrap in a #GtkIconInfo
3720 * Creates a #GtkIconInfo for a #GdkPixbuf.
3722 * Returns: a #GtkIconInfo
3727 gtk_icon_info_new_for_pixbuf (GtkIconTheme *icon_theme,
3732 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
3733 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
3735 info = icon_info_new ();
3736 info->pixbuf = g_object_ref (pixbuf);
3738 info->dir_type = ICON_THEME_DIR_UNTHEMED;