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"
42 #include "gtkiconfactory.h"
43 #include "gtkiconcache.h"
44 #include "gtkbuiltincache.h"
47 #include "gtksettings.h"
48 #include "gtkprivate.h"
50 #define DEFAULT_THEME_NAME "hicolor"
55 ICON_THEME_DIR_SCALABLE,
56 ICON_THEME_DIR_THRESHOLD,
57 ICON_THEME_DIR_UNTHEMED
60 /* In reverse search order: */
64 ICON_SUFFIX_XPM = 1 << 0,
65 ICON_SUFFIX_SVG = 1 << 1,
66 ICON_SUFFIX_PNG = 1 << 2,
67 HAS_ICON_FILE = 1 << 3
71 struct _GtkIconThemePrivate
73 guint custom_theme : 1;
74 guint is_screen_singleton : 1;
75 guint pixbuf_supports_svg : 1;
76 guint themes_valid : 1;
77 guint check_reload : 1;
78 guint loading_themes : 1;
85 /* A list of all the themes needed to look up icons.
86 * In search order, without duplicates
89 GHashTable *unthemed_icons;
91 /* Note: The keys of this hashtable are owned by the
92 * themedir and unthemed hashtables.
94 GHashTable *all_icons;
96 /* GdkScreen for the icon theme (may be NULL)
100 /* time when we last stat:ed for theme changes */
104 gulong reset_styles_idle;
109 /* Information about the source
112 GLoadableIcon *loadable;
113 GSList *emblem_infos;
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 guint raw_coordinates : 1;
131 guint forced_size : 1;
133 /* Cached information if we go ahead and try to load
150 /* In search order */
156 IconThemeDirType type;
171 GHashTable *icon_data;
177 char *no_svg_filename;
189 time_t mtime; /* 0 == not existing or not a dir */
194 static void gtk_icon_theme_finalize (GObject *object);
195 static void theme_dir_destroy (IconThemeDir *dir);
197 static void theme_destroy (IconTheme *theme);
198 static GtkIconInfo *theme_lookup_icon (IconTheme *theme,
199 const char *icon_name,
202 gboolean use_default_icons);
203 static void theme_list_icons (IconTheme *theme,
206 static void theme_list_contexts (IconTheme *theme,
207 GHashTable *contexts);
208 static void theme_subdir_load (GtkIconTheme *icon_theme,
210 GKeyFile *theme_file,
212 static void do_theme_change (GtkIconTheme *icon_theme);
214 static void blow_themes (GtkIconTheme *icon_themes);
215 static gboolean rescan_themes (GtkIconTheme *icon_themes);
217 static void icon_data_free (GtkIconData *icon_data);
218 static void load_icon_data (IconThemeDir *dir,
222 static IconSuffix theme_dir_get_icon_suffix (IconThemeDir *dir,
223 const gchar *icon_name,
224 gboolean *has_icon_file);
227 static GtkIconInfo *icon_info_new (void);
228 static GtkIconInfo *icon_info_new_builtin (BuiltinIcon *icon);
230 static IconSuffix suffix_from_name (const char *name);
232 static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
234 gint *min_difference_p,
235 gboolean *has_larger_p);
237 static guint signal_changed = 0;
239 static GHashTable *icon_theme_builtin_icons;
241 /* also used in gtkiconfactory.c */
242 GtkIconCache *_builtin_cache = NULL;
243 static GList *builtin_dirs = NULL;
245 G_DEFINE_TYPE (GtkIconTheme, gtk_icon_theme, G_TYPE_OBJECT)
248 * gtk_icon_theme_new:
250 * Creates a new icon theme object. Icon theme objects are used
251 * to lookup up an icon by name in a particular icon theme.
252 * Usually, you'll want to use gtk_icon_theme_get_default()
253 * or gtk_icon_theme_get_for_screen() rather than creating
254 * a new icon theme object for scratch.
256 * Return value: the newly created #GtkIconTheme object.
261 gtk_icon_theme_new (void)
263 return g_object_new (GTK_TYPE_ICON_THEME, NULL);
267 * gtk_icon_theme_get_default:
269 * Gets the icon theme for the default screen. See
270 * gtk_icon_theme_get_for_screen().
272 * Return value: (transfer none): A unique #GtkIconTheme associated with
273 * the default screen. This icon theme is associated with
274 * the screen and can be used as long as the screen
275 * is open. Do not ref or unref it.
280 gtk_icon_theme_get_default (void)
282 return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
286 * gtk_icon_theme_get_for_screen:
287 * @screen: a #GdkScreen
289 * Gets the icon theme object associated with @screen; if this
290 * function has not previously been called for the given
291 * screen, a new icon theme object will be created and
292 * associated with the screen. Icon theme objects are
293 * fairly expensive to create, so using this function
294 * is usually a better choice than calling than gtk_icon_theme_new()
295 * and setting the screen yourself; by using this function
296 * a single icon theme object will be shared between users.
298 * Return value: (transfer none): A unique #GtkIconTheme associated with
299 * the given screen. This icon theme is associated with
300 * the screen and can be used as long as the screen
301 * is open. Do not ref or unref it.
306 gtk_icon_theme_get_for_screen (GdkScreen *screen)
308 GtkIconTheme *icon_theme;
310 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
311 g_return_val_if_fail (!screen->closed, NULL);
313 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
316 GtkIconThemePrivate *priv;
318 icon_theme = gtk_icon_theme_new ();
319 gtk_icon_theme_set_screen (icon_theme, screen);
321 priv = icon_theme->priv;
322 priv->is_screen_singleton = TRUE;
324 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), icon_theme);
331 gtk_icon_theme_class_init (GtkIconThemeClass *klass)
333 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
335 gobject_class->finalize = gtk_icon_theme_finalize;
338 * GtkIconTheme::changed
339 * @icon_theme: the icon theme
341 * Emitted when the current icon theme is switched or GTK+ detects
342 * that a change has occurred in the contents of the current
345 signal_changed = g_signal_new (I_("changed"),
346 G_TYPE_FROM_CLASS (klass),
348 G_STRUCT_OFFSET (GtkIconThemeClass, changed),
350 g_cclosure_marshal_VOID__VOID,
353 g_type_class_add_private (klass, sizeof (GtkIconThemePrivate));
357 /* Callback when the display that the icon theme is attached to
358 * is closed; unset the screen, and if it's the unique theme
359 * for the screen, drop the reference
362 display_closed (GdkDisplay *display,
364 GtkIconTheme *icon_theme)
366 GtkIconThemePrivate *priv = icon_theme->priv;
367 GdkScreen *screen = priv->screen;
368 gboolean was_screen_singleton = priv->is_screen_singleton;
370 if (was_screen_singleton)
372 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), NULL);
373 priv->is_screen_singleton = FALSE;
376 gtk_icon_theme_set_screen (icon_theme, NULL);
378 if (was_screen_singleton)
380 g_object_unref (icon_theme);
385 update_current_theme (GtkIconTheme *icon_theme)
387 #define theme_changed(_old, _new) \
388 ((_old && !_new) || (!_old && _new) || \
389 (_old && _new && strcmp (_old, _new) != 0))
390 GtkIconThemePrivate *priv = icon_theme->priv;
392 if (!priv->custom_theme)
395 gchar *fallback_theme = NULL;
396 gboolean changed = FALSE;
400 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
401 g_object_get (settings,
402 "gtk-icon-theme-name", &theme,
403 "gtk-fallback-icon-theme", &fallback_theme, NULL);
406 /* ensure that the current theme (even when just the default)
407 * is searched before any fallback theme
409 if (!theme && fallback_theme)
410 theme = g_strdup (DEFAULT_THEME_NAME);
412 if (theme_changed (priv->current_theme, theme))
414 g_free (priv->current_theme);
415 priv->current_theme = theme;
421 if (theme_changed (priv->fallback_theme, fallback_theme))
423 g_free (priv->fallback_theme);
424 priv->fallback_theme = fallback_theme;
428 g_free (fallback_theme);
431 do_theme_change (icon_theme);
436 /* Callback when the icon theme GtkSetting changes
439 theme_changed (GtkSettings *settings,
441 GtkIconTheme *icon_theme)
443 update_current_theme (icon_theme);
447 unset_screen (GtkIconTheme *icon_theme)
449 GtkIconThemePrivate *priv = icon_theme->priv;
450 GtkSettings *settings;
455 settings = gtk_settings_get_for_screen (priv->screen);
456 display = gdk_screen_get_display (priv->screen);
458 g_signal_handlers_disconnect_by_func (display,
459 (gpointer) display_closed,
461 g_signal_handlers_disconnect_by_func (settings,
462 (gpointer) theme_changed,
470 * gtk_icon_theme_set_screen:
471 * @icon_theme: a #GtkIconTheme
472 * @screen: a #GdkScreen
474 * Sets the screen for an icon theme; the screen is used
475 * to track the user's currently configured icon theme,
476 * which might be different for different screens.
481 gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
484 GtkIconThemePrivate *priv;
485 GtkSettings *settings;
488 g_return_if_fail (GTK_ICON_THEME (icon_theme));
489 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
491 priv = icon_theme->priv;
493 unset_screen (icon_theme);
497 display = gdk_screen_get_display (screen);
498 settings = gtk_settings_get_for_screen (screen);
500 priv->screen = screen;
502 g_signal_connect (display, "closed",
503 G_CALLBACK (display_closed), icon_theme);
504 g_signal_connect (settings, "notify::gtk-icon-theme-name",
505 G_CALLBACK (theme_changed), icon_theme);
506 g_signal_connect (settings, "notify::gtk-fallback-icon-theme-name",
507 G_CALLBACK (theme_changed), icon_theme);
510 update_current_theme (icon_theme);
513 /* Checks whether a loader for SVG files has been registered
517 pixbuf_supports_svg (void)
521 static gint found_svg = -1;
526 formats = gdk_pixbuf_get_formats ();
529 for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
531 gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
534 for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
536 if (strcmp (*mime_type, "image/svg") == 0)
540 g_strfreev (mime_types);
543 g_slist_free (formats);
549 gtk_icon_theme_init (GtkIconTheme *icon_theme)
551 GtkIconThemePrivate *priv;
552 const gchar * const *xdg_data_dirs;
555 priv = G_TYPE_INSTANCE_GET_PRIVATE (icon_theme,
557 GtkIconThemePrivate);
558 icon_theme->priv = priv;
560 priv->custom_theme = FALSE;
562 xdg_data_dirs = g_get_system_data_dirs ();
563 for (i = 0; xdg_data_dirs[i]; i++) ;
565 priv->search_path_len = 2 * i + 2;
567 priv->search_path = g_new (char *, priv->search_path_len);
570 priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
571 priv->search_path[i++] = g_build_filename (g_get_user_data_dir (), "icons", NULL);
573 for (j = 0; xdg_data_dirs[j]; j++)
574 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
576 for (j = 0; xdg_data_dirs[j]; j++)
577 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "pixmaps", NULL);
579 priv->themes_valid = FALSE;
581 priv->unthemed_icons = NULL;
583 priv->pixbuf_supports_svg = pixbuf_supports_svg ();
587 free_dir_mtime (IconThemeDirMtime *dir_mtime)
589 if (dir_mtime->cache)
590 _gtk_icon_cache_unref (dir_mtime->cache);
592 g_free (dir_mtime->dir);
593 g_slice_free (IconThemeDirMtime, dir_mtime);
598 reset_styles_idle (gpointer user_data)
600 GtkIconTheme *icon_theme;
601 GtkIconThemePrivate *priv;
603 icon_theme = GTK_ICON_THEME (user_data);
604 priv = icon_theme->priv;
606 if (priv->screen && priv->is_screen_singleton)
608 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
609 gtk_rc_reset_styles (settings);
612 priv->reset_styles_idle = 0;
618 do_theme_change (GtkIconTheme *icon_theme)
620 GtkIconThemePrivate *priv = icon_theme->priv;
622 if (!priv->themes_valid)
626 g_print ("change to icon theme \"%s\"\n", priv->current_theme));
627 blow_themes (icon_theme);
628 g_signal_emit (icon_theme, signal_changed, 0);
630 if (!priv->reset_styles_idle)
631 priv->reset_styles_idle =
632 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2,
633 reset_styles_idle, icon_theme, NULL);
637 blow_themes (GtkIconTheme *icon_theme)
639 GtkIconThemePrivate *priv = icon_theme->priv;
641 if (priv->themes_valid)
643 g_hash_table_destroy (priv->all_icons);
644 g_list_foreach (priv->themes, (GFunc)theme_destroy, NULL);
645 g_list_free (priv->themes);
646 g_list_foreach (priv->dir_mtimes, (GFunc)free_dir_mtime, NULL);
647 g_list_free (priv->dir_mtimes);
648 g_hash_table_destroy (priv->unthemed_icons);
651 priv->unthemed_icons = NULL;
652 priv->dir_mtimes = NULL;
653 priv->all_icons = NULL;
654 priv->themes_valid = FALSE;
658 gtk_icon_theme_finalize (GObject *object)
660 GtkIconTheme *icon_theme;
661 GtkIconThemePrivate *priv;
664 icon_theme = GTK_ICON_THEME (object);
665 priv = icon_theme->priv;
667 if (priv->reset_styles_idle)
669 g_source_remove (priv->reset_styles_idle);
670 priv->reset_styles_idle = 0;
673 unset_screen (icon_theme);
675 g_free (priv->current_theme);
676 priv->current_theme = NULL;
678 for (i = 0; i < priv->search_path_len; i++)
679 g_free (priv->search_path[i]);
681 g_free (priv->search_path);
682 priv->search_path = NULL;
684 blow_themes (icon_theme);
686 G_OBJECT_CLASS (gtk_icon_theme_parent_class)->finalize (object);
690 * gtk_icon_theme_set_search_path:
691 * @icon_theme: a #GtkIconTheme
692 * @path: array of directories that are searched for icon themes
693 * @n_elements: number of elements in @path.
695 * Sets the search path for the icon theme object. When looking
696 * for an icon theme, GTK+ will search for a subdirectory of
697 * one or more of the directories in @path with the same name
698 * as the icon theme. (Themes from multiple of the path elements
699 * are combined to allow themes to be extended by adding icons
700 * in the user's home directory.)
702 * In addition if an icon found isn't found either in the current
703 * icon theme or the default icon theme, and an image file with
704 * the right name is found directly in one of the elements of
705 * @path, then that image will be used for the icon name.
706 * (This is legacy feature, and new icons should be put
707 * into the default icon theme, which is called DEFAULT_THEME_NAME,
708 * rather than directly on the icon path.)
713 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
717 GtkIconThemePrivate *priv;
720 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
722 priv = icon_theme->priv;
723 for (i = 0; i < priv->search_path_len; i++)
724 g_free (priv->search_path[i]);
726 g_free (priv->search_path);
728 priv->search_path = g_new (gchar *, n_elements);
729 priv->search_path_len = n_elements;
731 for (i = 0; i < priv->search_path_len; i++)
732 priv->search_path[i] = g_strdup (path[i]);
734 do_theme_change (icon_theme);
739 * gtk_icon_theme_get_search_path:
740 * @icon_theme: a #GtkIconTheme
741 * @path: (allow-none): (array length=n_elements) (out): location to store a list of icon theme path directories or %NULL
742 * The stored value should be freed with g_strfreev().
743 * @n_elements: location to store number of elements
746 * Gets the current search path. See gtk_icon_theme_set_search_path().
751 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
755 GtkIconThemePrivate *priv;
758 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
760 priv = icon_theme->priv;
763 *n_elements = priv->search_path_len;
767 *path = g_new (gchar *, priv->search_path_len + 1);
768 for (i = 0; i < priv->search_path_len; i++)
769 (*path)[i] = g_strdup (priv->search_path[i]);
775 * gtk_icon_theme_append_search_path:
776 * @icon_theme: a #GtkIconTheme
777 * @path: directory name to append to the icon path
779 * Appends a directory to the search path.
780 * See gtk_icon_theme_set_search_path().
785 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
788 GtkIconThemePrivate *priv;
790 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
791 g_return_if_fail (path != NULL);
793 priv = icon_theme->priv;
795 priv->search_path_len++;
797 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
798 priv->search_path[priv->search_path_len-1] = g_strdup (path);
800 do_theme_change (icon_theme);
804 * gtk_icon_theme_prepend_search_path:
805 * @icon_theme: a #GtkIconTheme
806 * @path: directory name to prepend to the icon path
808 * Prepends a directory to the search path.
809 * See gtk_icon_theme_set_search_path().
814 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
817 GtkIconThemePrivate *priv;
820 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
821 g_return_if_fail (path != NULL);
823 priv = icon_theme->priv;
825 priv->search_path_len++;
826 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
828 for (i = priv->search_path_len - 1; i > 0; i--)
829 priv->search_path[i] = priv->search_path[i - 1];
831 priv->search_path[0] = g_strdup (path);
833 do_theme_change (icon_theme);
837 * gtk_icon_theme_set_custom_theme:
838 * @icon_theme: a #GtkIconTheme
839 * @theme_name: name of icon theme to use instead of configured theme,
840 * or %NULL to unset a previously set custom theme
842 * Sets the name of the icon theme that the #GtkIconTheme object uses
843 * overriding system configuration. This function cannot be called
844 * on the icon theme objects returned from gtk_icon_theme_get_default()
845 * and gtk_icon_theme_get_for_screen().
850 gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
851 const gchar *theme_name)
853 GtkIconThemePrivate *priv;
855 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
857 priv = icon_theme->priv;
859 g_return_if_fail (!priv->is_screen_singleton);
861 if (theme_name != NULL)
863 priv->custom_theme = TRUE;
864 if (!priv->current_theme || strcmp (theme_name, priv->current_theme) != 0)
866 g_free (priv->current_theme);
867 priv->current_theme = g_strdup (theme_name);
869 do_theme_change (icon_theme);
874 if (priv->custom_theme)
876 priv->custom_theme = FALSE;
878 update_current_theme (icon_theme);
884 insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
890 GtkIconThemePrivate *priv;
891 IconTheme *theme = NULL;
893 GKeyFile *theme_file;
894 GError *error = NULL;
895 IconThemeDirMtime *dir_mtime;
896 struct stat stat_buf;
898 priv = icon_theme->priv;
900 for (l = priv->themes; l != NULL; l = l->next)
903 if (strcmp (theme->name, theme_name) == 0)
907 for (i = 0; i < priv->search_path_len; i++)
909 path = g_build_filename (priv->search_path[i],
912 dir_mtime = g_slice_new (IconThemeDirMtime);
913 dir_mtime->cache = NULL;
914 dir_mtime->dir = path;
915 if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
916 dir_mtime->mtime = stat_buf.st_mtime;
918 dir_mtime->mtime = 0;
920 priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
922 priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
925 for (i = 0; i < priv->search_path_len && !theme_file; i++)
927 path = g_build_filename (priv->search_path[i],
931 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
933 theme_file = g_key_file_new ();
934 g_key_file_set_list_separator (theme_file, ',');
935 g_key_file_load_from_file (theme_file, path, 0, &error);
938 g_key_file_free (theme_file);
940 g_error_free (error);
947 if (theme_file || strcmp (theme_name, DEFAULT_THEME_NAME) == 0)
949 theme = g_new0 (IconTheme, 1);
950 theme->name = g_strdup (theme_name);
951 priv->themes = g_list_prepend (priv->themes, theme);
954 if (theme_file == NULL)
957 theme->display_name =
958 g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
959 if (!theme->display_name)
960 g_warning ("Theme file for %s has no name\n", theme_name);
962 dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
965 g_warning ("Theme file for %s has no directories\n", theme_name);
966 priv->themes = g_list_remove (priv->themes, theme);
967 g_free (theme->name);
968 g_free (theme->display_name);
970 g_key_file_free (theme_file);
975 g_key_file_get_locale_string (theme_file,
976 "Icon Theme", "Comment",
979 g_key_file_get_string (theme_file,
980 "Icon Theme", "Example",
984 for (i = 0; dirs[i] != NULL; i++)
985 theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
989 theme->dirs = g_list_reverse (theme->dirs);
991 themes = g_key_file_get_string_list (theme_file,
998 for (i = 0; themes[i] != NULL; i++)
999 insert_theme (icon_theme, themes[i]);
1001 g_strfreev (themes);
1004 g_key_file_free (theme_file);
1008 free_unthemed_icon (UnthemedIcon *unthemed_icon)
1010 g_free (unthemed_icon->svg_filename);
1011 g_free (unthemed_icon->no_svg_filename);
1012 g_slice_free (UnthemedIcon, unthemed_icon);
1016 strip_suffix (const char *filename)
1020 dot = strrchr (filename, '.');
1023 return g_strdup (filename);
1025 return g_strndup (filename, dot - filename);
1029 load_themes (GtkIconTheme *icon_theme)
1031 GtkIconThemePrivate *priv;
1036 UnthemedIcon *unthemed_icon;
1037 IconSuffix old_suffix, new_suffix;
1039 IconThemeDirMtime *dir_mtime;
1040 struct stat stat_buf;
1042 priv = icon_theme->priv;
1044 priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
1046 if (priv->current_theme)
1047 insert_theme (icon_theme, priv->current_theme);
1049 /* Always look in the "default" icon theme, and in a fallback theme */
1050 if (priv->fallback_theme)
1051 insert_theme (icon_theme, priv->fallback_theme);
1052 insert_theme (icon_theme, DEFAULT_THEME_NAME);
1053 priv->themes = g_list_reverse (priv->themes);
1056 priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
1057 g_free, (GDestroyNotify)free_unthemed_icon);
1059 for (base = 0; base < icon_theme->priv->search_path_len; base++)
1061 dir = icon_theme->priv->search_path[base];
1063 dir_mtime = g_slice_new (IconThemeDirMtime);
1064 priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
1066 dir_mtime->dir = g_strdup (dir);
1067 dir_mtime->mtime = 0;
1068 dir_mtime->cache = NULL;
1070 if (g_stat (dir, &stat_buf) != 0 || !S_ISDIR (stat_buf.st_mode))
1072 dir_mtime->mtime = stat_buf.st_mtime;
1074 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir);
1075 if (dir_mtime->cache != NULL)
1078 gdir = g_dir_open (dir, 0, NULL);
1082 while ((file = g_dir_read_name (gdir)))
1084 new_suffix = suffix_from_name (file);
1086 if (new_suffix != ICON_SUFFIX_NONE)
1091 abs_file = g_build_filename (dir, file, NULL);
1092 base_name = strip_suffix (file);
1094 if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
1097 if (new_suffix == ICON_SUFFIX_SVG)
1099 if (unthemed_icon->svg_filename)
1102 unthemed_icon->svg_filename = abs_file;
1106 if (unthemed_icon->no_svg_filename)
1108 old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
1109 if (new_suffix > old_suffix)
1111 g_free (unthemed_icon->no_svg_filename);
1112 unthemed_icon->no_svg_filename = abs_file;
1118 unthemed_icon->no_svg_filename = abs_file;
1125 unthemed_icon = g_slice_new0 (UnthemedIcon);
1127 if (new_suffix == ICON_SUFFIX_SVG)
1128 unthemed_icon->svg_filename = abs_file;
1130 unthemed_icon->no_svg_filename = abs_file;
1132 g_hash_table_insert (priv->unthemed_icons,
1135 g_hash_table_insert (priv->all_icons,
1143 priv->themes_valid = TRUE;
1145 g_get_current_time(&tv);
1146 priv->last_stat_time = tv.tv_sec;
1150 _gtk_icon_theme_ensure_builtin_cache (void)
1152 static gboolean initialized = FALSE;
1154 static IconThemeDir dirs[5] =
1156 { ICON_THEME_DIR_THRESHOLD, 0, 16, 16, 16, 2, NULL, "16", -1, NULL, NULL, NULL },
1157 { ICON_THEME_DIR_THRESHOLD, 0, 20, 20, 20, 2, NULL, "20", -1, NULL, NULL, NULL },
1158 { ICON_THEME_DIR_THRESHOLD, 0, 24, 24, 24, 2, NULL, "24", -1, NULL, NULL, NULL },
1159 { ICON_THEME_DIR_THRESHOLD, 0, 32, 32, 32, 2, NULL, "32", -1, NULL, NULL, NULL },
1160 { ICON_THEME_DIR_THRESHOLD, 0, 48, 48, 48, 2, NULL, "48", -1, NULL, NULL, NULL }
1168 _builtin_cache = _gtk_icon_cache_new ((gchar *)builtin_icons);
1170 for (i = 0; i < G_N_ELEMENTS (dirs); i++)
1173 dir->cache = _gtk_icon_cache_ref (_builtin_cache);
1174 dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
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 if (priv->loading_themes)
1190 priv->loading_themes = TRUE;
1192 _gtk_icon_theme_ensure_builtin_cache ();
1194 if (priv->themes_valid)
1196 g_get_current_time (&tv);
1198 if (ABS (tv.tv_sec - priv->last_stat_time) > 5 &&
1199 rescan_themes (icon_theme))
1200 blow_themes (icon_theme);
1203 if (!priv->themes_valid)
1205 load_themes (icon_theme);
1209 g_signal_emit (icon_theme, signal_changed, 0);
1211 if (!priv->check_reload && priv->screen)
1213 static GdkAtom atom_iconthemes = GDK_NONE;
1214 GdkEvent *event = gdk_event_new (GDK_CLIENT_EVENT);
1217 if (!atom_iconthemes)
1218 atom_iconthemes = gdk_atom_intern_static_string ("_GTK_LOAD_ICONTHEMES");
1220 for (i = 0; i < 5; i++)
1221 event->client.data.l[i] = 0;
1222 event->client.data_format = 32;
1223 event->client.message_type = atom_iconthemes;
1225 gdk_screen_broadcast_client_message (priv->screen, event);
1230 priv->loading_themes = FALSE;
1233 static GtkIconInfo *
1234 choose_icon (GtkIconTheme *icon_theme,
1235 const gchar *icon_names[],
1237 GtkIconLookupFlags flags)
1239 GtkIconThemePrivate *priv;
1241 GtkIconInfo *icon_info = NULL;
1242 UnthemedIcon *unthemed_icon = NULL;
1244 gboolean use_builtin;
1247 priv = icon_theme->priv;
1249 if (flags & GTK_ICON_LOOKUP_NO_SVG)
1251 else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
1254 allow_svg = priv->pixbuf_supports_svg;
1256 use_builtin = flags & GTK_ICON_LOOKUP_USE_BUILTIN;
1258 ensure_valid_themes (icon_theme);
1260 for (l = priv->themes; l; l = l->next)
1262 IconTheme *theme = l->data;
1264 for (i = 0; icon_names[i]; i++)
1266 icon_info = theme_lookup_icon (theme, icon_names[i], size, allow_svg, use_builtin);
1272 for (i = 0; icon_names[i]; i++)
1274 unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_names[i]);
1279 /* Still not found an icon, check if reference to a Win32 resource */
1285 resources = g_strsplit (icon_names[0], ",", 0);
1288 wchar_t *wfile = g_utf8_to_utf16 (resources[0], -1, NULL, NULL, NULL);
1289 ExtractIconExW (wfile, resources[1] ? atoi (resources[1]) : 0, &hIcon, NULL, 1);
1295 icon_info = icon_info_new ();
1296 icon_info->cache_pixbuf = gdk_win32_icon_to_pixbuf_libgtk_only (hIcon);
1297 DestroyIcon (hIcon);
1298 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1299 icon_info->dir_size = size;
1301 g_strfreev (resources);
1307 icon_info = icon_info_new ();
1309 /* A SVG icon, when allowed, beats out a XPM icon, but not
1313 unthemed_icon->svg_filename &&
1314 (!unthemed_icon->no_svg_filename ||
1315 suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
1316 icon_info->filename = g_strdup (unthemed_icon->svg_filename);
1317 else if (unthemed_icon->no_svg_filename)
1318 icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);
1320 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1321 icon_info->dir_size = size;
1327 icon_info->desired_size = size;
1328 icon_info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
1332 static gboolean check_for_default_theme = TRUE;
1333 char *default_theme_path;
1334 gboolean found = FALSE;
1337 if (check_for_default_theme)
1339 check_for_default_theme = FALSE;
1341 for (i = 0; !found && i < priv->search_path_len; i++)
1343 default_theme_path = g_build_filename (priv->search_path[i],
1347 found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
1348 g_free (default_theme_path);
1353 g_warning (_("Could not find the icon '%s'. The '%s' theme\n"
1354 "was not found either, perhaps you need to install it.\n"
1355 "You can get a copy from:\n"
1357 icon_names[0], DEFAULT_THEME_NAME, "http://icon-theme.freedesktop.org/releases");
1367 * gtk_icon_theme_lookup_icon:
1368 * @icon_theme: a #GtkIconTheme
1369 * @icon_name: the name of the icon to lookup
1370 * @size: desired icon size
1371 * @flags: flags modifying the behavior of the icon lookup
1373 * Looks up a named icon and returns a structure containing
1374 * information such as the filename of the icon. The icon
1375 * can then be rendered into a pixbuf using
1376 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1377 * combines these two steps if all you need is the pixbuf.)
1379 * Return value: a #GtkIconInfo structure containing information
1380 * about the icon, or %NULL if the icon wasn't found. Free with
1381 * gtk_icon_info_free()
1386 gtk_icon_theme_lookup_icon (GtkIconTheme *icon_theme,
1387 const gchar *icon_name,
1389 GtkIconLookupFlags flags)
1393 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1394 g_return_val_if_fail (icon_name != NULL, NULL);
1395 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1396 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1398 GTK_NOTE (ICONTHEME,
1399 g_print ("gtk_icon_theme_lookup_icon %s\n", icon_name));
1401 if (flags & GTK_ICON_LOOKUP_GENERIC_FALLBACK)
1408 for (p = (gchar *) icon_name; *p; p++)
1412 names = g_new (gchar *, dashes + 2);
1413 names[0] = g_strdup (icon_name);
1414 for (i = 1; i <= dashes; i++)
1416 names[i] = g_strdup (names[i - 1]);
1417 p = strrchr (names[i], '-');
1420 names[dashes + 1] = NULL;
1422 info = choose_icon (icon_theme, (const gchar **) names, size, flags);
1428 const gchar *names[2];
1430 names[0] = icon_name;
1433 info = choose_icon (icon_theme, names, size, flags);
1440 * gtk_icon_theme_choose_icon:
1441 * @icon_theme: a #GtkIconTheme
1442 * @icon_names: %NULL-terminated array of icon names to lookup
1443 * @size: desired icon size
1444 * @flags: flags modifying the behavior of the icon lookup
1446 * Looks up a named icon and returns a structure containing
1447 * information such as the filename of the icon. The icon
1448 * can then be rendered into a pixbuf using
1449 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1450 * combines these two steps if all you need is the pixbuf.)
1452 * If @icon_names contains more than one name, this function
1453 * tries them all in the given order before falling back to
1454 * inherited icon themes.
1456 * Return value: a #GtkIconInfo structure containing information
1457 * about the icon, or %NULL if the icon wasn't found. Free with
1458 * gtk_icon_info_free()
1463 gtk_icon_theme_choose_icon (GtkIconTheme *icon_theme,
1464 const gchar *icon_names[],
1466 GtkIconLookupFlags flags)
1468 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1469 g_return_val_if_fail (icon_names != NULL, NULL);
1470 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1471 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1473 return choose_icon (icon_theme, icon_names, size, flags);
1478 gtk_icon_theme_error_quark (void)
1480 return g_quark_from_static_string ("gtk-icon-theme-error-quark");
1484 * gtk_icon_theme_load_icon:
1485 * @icon_theme: a #GtkIconTheme
1486 * @icon_name: the name of the icon to lookup
1487 * @size: the desired icon size. The resulting icon may not be
1488 * exactly this size; see gtk_icon_info_load_icon().
1489 * @flags: flags modifying the behavior of the icon lookup
1490 * @error: (allow-none): Location to store error information on failure, or %NULL.
1492 * Looks up an icon in an icon theme, scales it to the given size
1493 * and renders it into a pixbuf. This is a convenience function;
1494 * if more details about the icon are needed, use
1495 * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1497 * Note that you probably want to listen for icon theme changes and
1498 * update the icon. This is usually done by connecting to the
1499 * GtkWidget::style-set signal. If for some reason you do not want to
1500 * update the icon when the icon theme changes, you should consider
1501 * using gdk_pixbuf_copy() to make a private copy of the pixbuf
1502 * returned by this function. Otherwise GTK+ may need to keep the old
1503 * icon theme loaded, which would be a waste of memory.
1505 * Return value: the rendered icon; this may be a newly created icon
1506 * or a new reference to an internal icon, so you must not modify
1507 * the icon. Use g_object_unref() to release your reference to the
1508 * icon. %NULL if the icon isn't found.
1513 gtk_icon_theme_load_icon (GtkIconTheme *icon_theme,
1514 const gchar *icon_name,
1516 GtkIconLookupFlags flags,
1519 GtkIconInfo *icon_info;
1520 GdkPixbuf *pixbuf = NULL;
1522 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1523 g_return_val_if_fail (icon_name != NULL, NULL);
1524 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1525 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1526 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1528 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
1529 flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1532 g_set_error (error, GTK_ICON_THEME_ERROR, GTK_ICON_THEME_NOT_FOUND,
1533 _("Icon '%s' not present in theme"), icon_name);
1537 pixbuf = gtk_icon_info_load_icon (icon_info, error);
1538 gtk_icon_info_free (icon_info);
1544 * gtk_icon_theme_has_icon:
1545 * @icon_theme: a #GtkIconTheme
1546 * @icon_name: the name of an icon
1548 * Checks whether an icon theme includes an icon
1549 * for a particular name.
1551 * Return value: %TRUE if @icon_theme includes an
1552 * icon for @icon_name.
1557 gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
1558 const char *icon_name)
1560 GtkIconThemePrivate *priv;
1563 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1564 g_return_val_if_fail (icon_name != NULL, FALSE);
1566 priv = icon_theme->priv;
1568 ensure_valid_themes (icon_theme);
1570 for (l = priv->dir_mtimes; l; l = l->next)
1572 IconThemeDirMtime *dir_mtime = l->data;
1573 GtkIconCache *cache = dir_mtime->cache;
1575 if (cache && _gtk_icon_cache_has_icon (cache, icon_name))
1579 if (g_hash_table_lookup_extended (priv->all_icons,
1580 icon_name, NULL, NULL))
1583 if (_builtin_cache &&
1584 _gtk_icon_cache_has_icon (_builtin_cache, icon_name))
1587 if (icon_theme_builtin_icons &&
1588 g_hash_table_lookup_extended (icon_theme_builtin_icons,
1589 icon_name, NULL, NULL))
1596 add_size (gpointer key,
1600 gint **res_p = user_data;
1602 **res_p = GPOINTER_TO_INT (key);
1608 * gtk_icon_theme_get_icon_sizes:
1609 * @icon_theme: a #GtkIconTheme
1610 * @icon_name: the name of an icon
1612 * Returns an array of integers describing the sizes at which
1613 * the icon is available without scaling. A size of -1 means
1614 * that the icon is available in a scalable format. The array
1615 * is zero-terminated.
1617 * Return value: An newly allocated array describing the sizes at
1618 * which the icon is available. The array should be freed with g_free()
1619 * when it is no longer needed.
1624 gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
1625 const char *icon_name)
1627 GList *l, *d, *icons;
1631 GtkIconThemePrivate *priv;
1633 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1635 priv = icon_theme->priv;
1637 ensure_valid_themes (icon_theme);
1639 sizes = g_hash_table_new (g_direct_hash, g_direct_equal);
1641 for (l = priv->themes; l; l = l->next)
1643 IconTheme *theme = l->data;
1644 for (d = theme->dirs; d; d = d->next)
1646 IconThemeDir *dir = d->data;
1648 if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
1651 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1652 if (suffix != ICON_SUFFIX_NONE)
1654 if (suffix == ICON_SUFFIX_SVG)
1655 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1657 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1662 for (d = builtin_dirs; d; d = d->next)
1664 IconThemeDir *dir = d->data;
1666 if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
1669 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1670 if (suffix != ICON_SUFFIX_NONE)
1672 if (suffix == ICON_SUFFIX_SVG)
1673 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1675 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1679 if (icon_theme_builtin_icons)
1681 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
1685 BuiltinIcon *icon = icons->data;
1687 g_hash_table_insert (sizes, GINT_TO_POINTER (icon->size), NULL);
1688 icons = icons->next;
1692 r = result = g_new0 (gint, g_hash_table_size (sizes) + 1);
1694 g_hash_table_foreach (sizes, add_size, &r);
1695 g_hash_table_destroy (sizes);
1701 add_key_to_hash (gpointer key,
1705 GHashTable *hash = user_data;
1707 g_hash_table_insert (hash, key, NULL);
1711 add_key_to_list (gpointer key,
1715 GList **list = user_data;
1717 *list = g_list_prepend (*list, g_strdup (key));
1721 * gtk_icon_theme_list_icons:
1722 * @icon_theme: a #GtkIconTheme
1723 * @context: a string identifying a particular type of icon,
1724 * or %NULL to list all icons.
1726 * Lists the icons in the current icon theme. Only a subset
1727 * of the icons can be listed by providing a context string.
1728 * The set of values for the context string is system dependent,
1729 * but will typically include such values as "Applications" and
1732 * Return value: (element-type utf8) (transfer none): a #GList list holding the names of all the
1733 * icons in the theme. You must first free each element
1734 * in the list with g_free(), then free the list itself
1735 * with g_list_free().
1740 gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
1741 const char *context)
1743 GtkIconThemePrivate *priv;
1746 GQuark context_quark;
1748 priv = icon_theme->priv;
1750 ensure_valid_themes (icon_theme);
1754 context_quark = g_quark_try_string (context);
1762 icons = g_hash_table_new (g_str_hash, g_str_equal);
1767 theme_list_icons (l->data, icons, context_quark);
1771 if (context_quark == 0)
1772 g_hash_table_foreach (priv->unthemed_icons,
1778 g_hash_table_foreach (icons,
1782 g_hash_table_destroy (icons);
1788 * gtk_icon_theme_list_contexts:
1789 * @icon_theme: a #GtkIconTheme
1791 * Gets the list of contexts available within the current
1792 * hierarchy of icon themes
1794 * Return value: (element-type utf8) (transfer full): a #GList list holding the names of all the
1795 * contexts in the theme. You must first free each element
1796 * in the list with g_free(), then free the list itself
1797 * with g_list_free().
1802 gtk_icon_theme_list_contexts (GtkIconTheme *icon_theme)
1804 GtkIconThemePrivate *priv;
1805 GHashTable *contexts;
1808 priv = icon_theme->priv;
1810 ensure_valid_themes (icon_theme);
1812 contexts = g_hash_table_new (g_str_hash, g_str_equal);
1817 theme_list_contexts (l->data, contexts);
1823 g_hash_table_foreach (contexts,
1827 g_hash_table_destroy (contexts);
1833 * gtk_icon_theme_get_example_icon_name:
1834 * @icon_theme: a #GtkIconTheme
1836 * Gets the name of an icon that is representative of the
1837 * current theme (for instance, to use when presenting
1838 * a list of themes to the user.)
1840 * Return value: the name of an example icon or %NULL.
1841 * Free with g_free().
1846 gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
1848 GtkIconThemePrivate *priv;
1852 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1854 priv = icon_theme->priv;
1856 ensure_valid_themes (icon_theme);
1863 return g_strdup (theme->example);
1873 rescan_themes (GtkIconTheme *icon_theme)
1875 GtkIconThemePrivate *priv;
1876 IconThemeDirMtime *dir_mtime;
1879 struct stat stat_buf;
1882 priv = icon_theme->priv;
1884 for (d = priv->dir_mtimes; d != NULL; d = d->next)
1886 dir_mtime = d->data;
1888 stat_res = g_stat (dir_mtime->dir, &stat_buf);
1890 /* dir mtime didn't change */
1891 if (stat_res == 0 &&
1892 S_ISDIR (stat_buf.st_mode) &&
1893 dir_mtime->mtime == stat_buf.st_mtime)
1895 /* didn't exist before, and still doesn't */
1896 if (dir_mtime->mtime == 0 &&
1897 (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
1903 g_get_current_time (&tv);
1904 priv->last_stat_time = tv.tv_sec;
1910 * gtk_icon_theme_rescan_if_needed:
1911 * @icon_theme: a #GtkIconTheme
1913 * Checks to see if the icon theme has changed; if it has, any
1914 * currently cached information is discarded and will be reloaded
1915 * next time @icon_theme is accessed.
1917 * Return value: %TRUE if the icon theme has changed and needed
1923 gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
1927 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1929 retval = rescan_themes (icon_theme);
1931 do_theme_change (icon_theme);
1937 theme_destroy (IconTheme *theme)
1939 g_free (theme->display_name);
1940 g_free (theme->comment);
1941 g_free (theme->name);
1942 g_free (theme->example);
1944 g_list_foreach (theme->dirs, (GFunc)theme_dir_destroy, NULL);
1945 g_list_free (theme->dirs);
1951 theme_dir_destroy (IconThemeDir *dir)
1954 _gtk_icon_cache_unref (dir->cache);
1956 g_hash_table_destroy (dir->icons);
1959 g_hash_table_destroy (dir->icon_data);
1961 g_free (dir->subdir);
1966 theme_dir_size_difference (IconThemeDir *dir, int size, gboolean *smaller)
1971 case ICON_THEME_DIR_FIXED:
1972 *smaller = size < dir->size;
1973 return abs (size - dir->size);
1975 case ICON_THEME_DIR_SCALABLE:
1976 *smaller = size < dir->min_size;
1977 if (size < dir->min_size)
1978 return dir->min_size - size;
1979 if (size > dir->max_size)
1980 return size - dir->max_size;
1983 case ICON_THEME_DIR_THRESHOLD:
1984 min = dir->size - dir->threshold;
1985 max = dir->size + dir->threshold;
1986 *smaller = size < min;
1993 case ICON_THEME_DIR_UNTHEMED:
1994 g_assert_not_reached ();
1997 g_assert_not_reached ();
2002 string_from_suffix (IconSuffix suffix)
2006 case ICON_SUFFIX_XPM:
2008 case ICON_SUFFIX_SVG:
2010 case ICON_SUFFIX_PNG:
2013 g_assert_not_reached();
2019 suffix_from_name (const char *name)
2023 if (g_str_has_suffix (name, ".png"))
2024 retval = ICON_SUFFIX_PNG;
2025 else if (g_str_has_suffix (name, ".svg"))
2026 retval = ICON_SUFFIX_SVG;
2027 else if (g_str_has_suffix (name, ".xpm"))
2028 retval = ICON_SUFFIX_XPM;
2030 retval = ICON_SUFFIX_NONE;
2036 best_suffix (IconSuffix suffix,
2039 if ((suffix & ICON_SUFFIX_PNG) != 0)
2040 return ICON_SUFFIX_PNG;
2041 else if (allow_svg && ((suffix & ICON_SUFFIX_SVG) != 0))
2042 return ICON_SUFFIX_SVG;
2043 else if ((suffix & ICON_SUFFIX_XPM) != 0)
2044 return ICON_SUFFIX_XPM;
2046 return ICON_SUFFIX_NONE;
2051 theme_dir_get_icon_suffix (IconThemeDir *dir,
2052 const gchar *icon_name,
2053 gboolean *has_icon_file)
2059 suffix = (IconSuffix)_gtk_icon_cache_get_icon_flags (dir->cache,
2064 *has_icon_file = suffix & HAS_ICON_FILE;
2066 suffix = suffix & ~HAS_ICON_FILE;
2069 suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
2071 GTK_NOTE (ICONTHEME,
2072 g_print ("get_icon_suffix%s %u\n", dir->cache ? " (cached)" : "", suffix));
2077 static GtkIconInfo *
2078 theme_lookup_icon (IconTheme *theme,
2079 const char *icon_name,
2082 gboolean use_builtin)
2085 IconThemeDir *dir, *min_dir;
2087 int min_difference, difference;
2088 BuiltinIcon *closest_builtin = NULL;
2089 gboolean smaller, has_larger, match;
2092 min_difference = G_MAXINT;
2097 /* Builtin icons are logically part of the default theme and
2098 * are searched before other subdirectories of the default theme.
2100 if (use_builtin && strcmp (theme->name, DEFAULT_THEME_NAME) == 0)
2102 closest_builtin = find_builtin_icon (icon_name,
2107 if (min_difference == 0)
2108 return icon_info_new_builtin (closest_builtin);
2110 dirs = builtin_dirs;
2120 GTK_NOTE (ICONTHEME,
2121 g_print ("theme_lookup_icon dir %s\n", dir->dir));
2122 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
2123 if (best_suffix (suffix, allow_svg) != ICON_SUFFIX_NONE)
2125 difference = theme_dir_size_difference (dir, size, &smaller);
2127 if (difference == 0)
2129 if (dir->type == ICON_THEME_DIR_SCALABLE)
2131 /* don't pick scalable if we already found
2132 * a matching non-scalable dir
2142 /* for a matching non-scalable dir keep
2143 * going and look for a closer match
2145 difference = abs (size - dir->size);
2146 if (!match || difference < min_difference)
2149 min_difference = difference;
2152 if (difference == 0)
2161 if (difference < min_difference || smaller)
2163 min_difference = difference;
2165 has_larger = smaller;
2170 if (difference < min_difference && smaller)
2172 min_difference = difference;
2181 if (l == NULL && dirs == builtin_dirs)
2190 GtkIconInfo *icon_info = icon_info_new ();
2191 gboolean has_icon_file = FALSE;
2193 suffix = theme_dir_get_icon_suffix (min_dir, icon_name, &has_icon_file);
2194 suffix = best_suffix (suffix, allow_svg);
2195 g_assert (suffix != ICON_SUFFIX_NONE);
2199 file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
2200 icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
2205 icon_info->filename = NULL;
2208 if (min_dir->icon_data != NULL)
2209 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2211 if (icon_info->data == NULL && min_dir->cache != NULL)
2213 icon_info->data = _gtk_icon_cache_get_icon_data (min_dir->cache, icon_name, min_dir->subdir_index);
2214 if (icon_info->data)
2216 if (min_dir->icon_data == NULL)
2217 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2218 g_free, (GDestroyNotify)icon_data_free);
2220 g_hash_table_replace (min_dir->icon_data, g_strdup (icon_name), icon_info->data);
2224 if (icon_info->data == NULL && has_icon_file)
2226 gchar *icon_file_name, *icon_file_path;
2228 icon_file_name = g_strconcat (icon_name, ".icon", NULL);
2229 icon_file_path = g_build_filename (min_dir->dir, icon_file_name, NULL);
2231 if (g_file_test (icon_file_path, G_FILE_TEST_IS_REGULAR))
2233 if (min_dir->icon_data == NULL)
2234 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2235 g_free, (GDestroyNotify)icon_data_free);
2236 load_icon_data (min_dir, icon_file_path, icon_file_name);
2238 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2240 g_free (icon_file_name);
2241 g_free (icon_file_path);
2246 icon_info->cache_pixbuf = _gtk_icon_cache_get_icon (min_dir->cache, icon_name,
2247 min_dir->subdir_index);
2250 icon_info->dir_type = min_dir->type;
2251 icon_info->dir_size = min_dir->size;
2252 icon_info->threshold = min_dir->threshold;
2257 if (closest_builtin)
2258 return icon_info_new_builtin (closest_builtin);
2264 theme_list_icons (IconTheme *theme,
2268 GList *l = theme->dirs;
2275 if (context == dir->context ||
2280 _gtk_icon_cache_add_icons (dir->cache,
2287 g_hash_table_foreach (dir->icons,
2297 theme_list_contexts (IconTheme *theme,
2298 GHashTable *contexts)
2300 GList *l = theme->dirs;
2302 const char *context;
2308 context = g_quark_to_string (dir->context);
2309 g_hash_table_replace (contexts, (gpointer) context, NULL);
2316 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
2318 GKeyFile *icon_file;
2326 GError *error = NULL;
2330 icon_file = g_key_file_new ();
2331 g_key_file_set_list_separator (icon_file, ',');
2332 g_key_file_load_from_file (icon_file, path, 0, &error);
2335 g_error_free (error);
2336 g_key_file_free (icon_file);
2341 base_name = strip_suffix (name);
2343 data = g_slice_new0 (GtkIconData);
2344 g_hash_table_replace (dir->icon_data, base_name, data);
2346 ivalues = g_key_file_get_integer_list (icon_file,
2347 "Icon Data", "EmbeddedTextRectangle",
2353 data->has_embedded_rect = TRUE;
2354 data->x0 = ivalues[0];
2355 data->y0 = ivalues[1];
2356 data->x1 = ivalues[2];
2357 data->y1 = ivalues[3];
2363 str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
2366 split = g_strsplit (str, "|", -1);
2368 data->n_attach_points = g_strv_length (split);
2369 data->attach_points = g_new (GdkPoint, data->n_attach_points);
2372 while (split[i] != NULL && i < data->n_attach_points)
2374 split_point = strchr (split[i], ',');
2379 data->attach_points[i].x = atoi (split[i]);
2380 data->attach_points[i].y = atoi (split_point);
2389 data->display_name = g_key_file_get_locale_string (icon_file,
2390 "Icon Data", "DisplayName",
2392 g_key_file_free (icon_file);
2397 scan_directory (GtkIconThemePrivate *icon_theme,
2398 IconThemeDir *dir, char *full_dir)
2403 GTK_NOTE (ICONTHEME,
2404 g_print ("scanning directory %s\n", full_dir));
2405 dir->icons = g_hash_table_new_full (g_str_hash, g_str_equal,
2408 gdir = g_dir_open (full_dir, 0, NULL);
2413 while ((name = g_dir_read_name (gdir)))
2417 IconSuffix suffix, hash_suffix;
2419 if (g_str_has_suffix (name, ".icon"))
2421 if (dir->icon_data == NULL)
2422 dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2423 g_free, (GDestroyNotify)icon_data_free);
2425 path = g_build_filename (full_dir, name, NULL);
2426 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
2427 load_icon_data (dir, path, name);
2434 suffix = suffix_from_name (name);
2435 if (suffix == ICON_SUFFIX_NONE)
2438 base_name = strip_suffix (name);
2440 hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
2441 g_hash_table_replace (icon_theme->all_icons, base_name, NULL);
2442 g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
2449 theme_subdir_load (GtkIconTheme *icon_theme,
2451 GKeyFile *theme_file,
2457 IconThemeDirType type;
2458 char *context_string;
2465 GError *error = NULL;
2466 IconThemeDirMtime *dir_mtime;
2468 size = g_key_file_get_integer (theme_file, subdir, "Size", &error);
2471 g_error_free (error);
2472 g_warning ("Theme directory %s of theme %s has no size field\n",
2473 subdir, theme->name);
2477 type = ICON_THEME_DIR_THRESHOLD;
2478 type_string = g_key_file_get_string (theme_file, subdir, "Type", NULL);
2481 if (strcmp (type_string, "Fixed") == 0)
2482 type = ICON_THEME_DIR_FIXED;
2483 else if (strcmp (type_string, "Scalable") == 0)
2484 type = ICON_THEME_DIR_SCALABLE;
2485 else if (strcmp (type_string, "Threshold") == 0)
2486 type = ICON_THEME_DIR_THRESHOLD;
2488 g_free (type_string);
2492 context_string = g_key_file_get_string (theme_file, subdir, "Context", NULL);
2495 context = g_quark_from_string (context_string);
2496 g_free (context_string);
2499 max_size = g_key_file_get_integer (theme_file, subdir, "MaxSize", &error);
2504 g_error_free (error);
2508 min_size = g_key_file_get_integer (theme_file, subdir, "MinSize", &error);
2513 g_error_free (error);
2517 threshold = g_key_file_get_integer (theme_file, subdir, "Threshold", &error);
2522 g_error_free (error);
2526 for (d = icon_theme->priv->dir_mtimes; d; d = d->next)
2528 dir_mtime = (IconThemeDirMtime *)d->data;
2530 if (dir_mtime->mtime == 0)
2531 continue; /* directory doesn't exist */
2533 full_dir = g_build_filename (dir_mtime->dir, subdir, NULL);
2535 /* First, see if we have a cache for the directory */
2536 if (dir_mtime->cache != NULL || g_file_test (full_dir, G_FILE_TEST_IS_DIR))
2538 if (dir_mtime->cache == NULL)
2540 /* This will return NULL if the cache doesn't exist or is outdated */
2541 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir_mtime->dir);
2544 dir = g_new (IconThemeDir, 1);
2546 dir->context = context;
2548 dir->min_size = min_size;
2549 dir->max_size = max_size;
2550 dir->threshold = threshold;
2551 dir->dir = full_dir;
2552 dir->icon_data = NULL;
2553 dir->subdir = g_strdup (subdir);
2554 if (dir_mtime->cache != NULL)
2556 dir->cache = _gtk_icon_cache_ref (dir_mtime->cache);
2557 dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
2562 dir->subdir_index = -1;
2563 scan_directory (icon_theme->priv, dir, full_dir);
2566 theme->dirs = g_list_prepend (theme->dirs, dir);
2574 icon_data_free (GtkIconData *icon_data)
2576 g_free (icon_data->attach_points);
2577 g_free (icon_data->display_name);
2578 g_slice_free (GtkIconData, icon_data);
2585 G_DEFINE_BOXED_TYPE (GtkIconInfo, gtk_icon_info,
2589 static GtkIconInfo *
2590 icon_info_new (void)
2592 GtkIconInfo *icon_info = g_slice_new0 (GtkIconInfo);
2594 icon_info->scale = -1.;
2595 icon_info->ref_count = 1;
2600 static GtkIconInfo *
2601 icon_info_new_builtin (BuiltinIcon *icon)
2603 GtkIconInfo *icon_info = icon_info_new ();
2605 icon_info->cache_pixbuf = g_object_ref (icon->pixbuf);
2606 icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
2607 icon_info->dir_size = icon->size;
2608 icon_info->threshold = 2;
2614 * gtk_icon_info_copy:
2615 * @icon_info: a #GtkIconInfo
2617 * Make a copy of a #GtkIconInfo.
2619 * Return value: the new GtkIconInfo
2624 gtk_icon_info_copy (GtkIconInfo *icon_info)
2627 g_return_val_if_fail (icon_info != NULL, NULL);
2629 icon_info->ref_count++;
2635 * gtk_icon_info_free:
2636 * @icon_info: a #GtkIconInfo
2638 * Free a #GtkIconInfo and associated information
2643 gtk_icon_info_free (GtkIconInfo *icon_info)
2645 g_return_if_fail (icon_info != NULL);
2647 icon_info->ref_count--;
2648 if (icon_info->ref_count > 0)
2651 g_free (icon_info->filename);
2652 if (icon_info->loadable)
2653 g_object_unref (icon_info->loadable);
2654 g_slist_foreach (icon_info->emblem_infos, (GFunc)gtk_icon_info_free, NULL);
2655 g_slist_free (icon_info->emblem_infos);
2656 if (icon_info->pixbuf)
2657 g_object_unref (icon_info->pixbuf);
2658 if (icon_info->cache_pixbuf)
2659 g_object_unref (icon_info->cache_pixbuf);
2661 g_slice_free (GtkIconInfo, icon_info);
2665 * gtk_icon_info_get_base_size:
2666 * @icon_info: a #GtkIconInfo
2668 * Gets the base size for the icon. The base size
2669 * is a size for the icon that was specified by
2670 * the icon theme creator. This may be different
2671 * than the actual size of image; an example of
2672 * this is small emblem icons that can be attached
2673 * to a larger icon. These icons will be given
2674 * the same base size as the larger icons to which
2675 * they are attached.
2677 * Return value: the base size, or 0, if no base
2678 * size is known for the icon.
2683 gtk_icon_info_get_base_size (GtkIconInfo *icon_info)
2685 g_return_val_if_fail (icon_info != NULL, 0);
2687 return icon_info->dir_size;
2691 * gtk_icon_info_get_filename:
2692 * @icon_info: a #GtkIconInfo
2694 * Gets the filename for the icon. If the
2695 * %GTK_ICON_LOOKUP_USE_BUILTIN flag was passed
2696 * to gtk_icon_theme_lookup_icon(), there may be
2697 * no filename if a builtin icon is returned; in this
2698 * case, you should use gtk_icon_info_get_builtin_pixbuf().
2700 * Return value: the filename for the icon, or %NULL
2701 * if gtk_icon_info_get_builtin_pixbuf() should
2702 * be used instead. The return value is owned by
2703 * GTK+ and should not be modified or freed.
2707 G_CONST_RETURN gchar *
2708 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2710 g_return_val_if_fail (icon_info != NULL, NULL);
2712 return icon_info->filename;
2716 * gtk_icon_info_get_builtin_pixbuf:
2717 * @icon_info: a #GtkIconInfo structure
2719 * Gets the built-in image for this icon, if any. To allow
2720 * GTK+ to use built in icon images, you must pass the
2721 * %GTK_ICON_LOOKUP_USE_BUILTIN to
2722 * gtk_icon_theme_lookup_icon().
2724 * Return value: (transfer none): the built-in image pixbuf, or %NULL. No
2725 * extra reference is added to the returned pixbuf, so if
2726 * you want to keep it around, you must use g_object_ref().
2727 * The returned image must not be modified.
2732 gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
2734 g_return_val_if_fail (icon_info != NULL, NULL);
2736 if (icon_info->filename)
2739 return icon_info->cache_pixbuf;
2742 static gboolean icon_info_ensure_scale_and_pixbuf (GtkIconInfo*, gboolean);
2744 /* Combine the icon with all emblems, the first emblem is placed
2745 * in the southeast corner. Scale emblems to be at most 3/4 of the
2746 * size of the icon itself.
2749 apply_emblems (GtkIconInfo *info)
2751 GdkPixbuf *icon = NULL;
2755 if (info->emblem_infos == NULL)
2758 w = gdk_pixbuf_get_width (info->pixbuf);
2759 h = gdk_pixbuf_get_height (info->pixbuf);
2761 for (l = info->emblem_infos, pos = 0; l; l = l->next, pos++)
2763 GtkIconInfo *emblem_info = l->data;
2765 if (icon_info_ensure_scale_and_pixbuf (emblem_info, FALSE))
2767 GdkPixbuf *emblem = emblem_info->pixbuf;
2769 gint x = 0, y = 0; /* silence compiler */
2772 ew = gdk_pixbuf_get_width (emblem);
2773 eh = gdk_pixbuf_get_height (emblem);
2805 icon = gdk_pixbuf_copy (info->pixbuf);
2810 gdk_pixbuf_composite (emblem, icon, x, y, ew, eh, x, y,
2811 scale, scale, GDK_INTERP_BILINEAR, 255);
2817 g_object_unref (info->pixbuf);
2818 info->pixbuf = icon;
2822 /* This function contains the complicated logic for deciding
2823 * on the size at which to load the icon and loading it at
2827 icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
2828 gboolean scale_only)
2830 int image_width, image_height;
2831 GdkPixbuf *source_pixbuf;
2834 /* First check if we already succeeded have the necessary
2835 * information (or failed earlier)
2837 if (scale_only && icon_info->scale >= 0)
2840 if (icon_info->pixbuf)
2843 if (icon_info->load_error)
2846 /* SVG icons are a special case - we just immediately scale them
2847 * to the desired size
2849 if (icon_info->filename && !icon_info->loadable)
2853 file = g_file_new_for_path (icon_info->filename);
2854 icon_info->loadable = G_LOADABLE_ICON (g_file_icon_new (file));
2855 g_object_unref (file);
2859 if (G_IS_FILE_ICON (icon_info->loadable))
2862 GFileInfo *file_info;
2863 const gchar *content_type;
2865 file = g_file_icon_get_file (G_FILE_ICON (icon_info->loadable));
2866 file_info = g_file_query_info (file,
2867 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
2868 G_FILE_QUERY_INFO_NONE,
2872 content_type = g_file_info_get_content_type (file_info);
2874 if (content_type && strcmp (content_type, "image/svg+xml") == 0)
2877 g_object_unref (file_info);
2883 GInputStream *stream;
2885 icon_info->scale = icon_info->desired_size / 1000.;
2890 stream = g_loadable_icon_load (icon_info->loadable,
2891 icon_info->desired_size,
2893 &icon_info->load_error);
2896 icon_info->pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
2897 icon_info->desired_size,
2898 icon_info->desired_size,
2901 &icon_info->load_error);
2902 g_object_unref (stream);
2905 if (!icon_info->pixbuf)
2908 apply_emblems (icon_info);
2913 /* In many cases, the scale can be determined without actual access
2914 * to the icon file. This is generally true when we have a size
2915 * for the directory where the icon is; the image size doesn't
2916 * matter in that case.
2918 if (icon_info->forced_size)
2919 icon_info->scale = -1;
2920 else if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
2921 icon_info->scale = 1.0;
2922 else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
2924 if (icon_info->desired_size >= icon_info->dir_size - icon_info->threshold &&
2925 icon_info->desired_size <= icon_info->dir_size + icon_info->threshold)
2926 icon_info->scale = 1.0;
2927 else if (icon_info->dir_size > 0)
2928 icon_info->scale =(gdouble) icon_info->desired_size / icon_info->dir_size;
2930 else if (icon_info->dir_type == ICON_THEME_DIR_SCALABLE)
2932 if (icon_info->dir_size > 0)
2933 icon_info->scale = (gdouble) icon_info->desired_size / icon_info->dir_size;
2936 if (icon_info->scale >= 0. && scale_only)
2939 /* At this point, we need to actually get the icon; either from the
2940 * builtin image or by loading the file
2942 source_pixbuf = NULL;
2943 if (icon_info->cache_pixbuf)
2944 source_pixbuf = g_object_ref (icon_info->cache_pixbuf);
2947 GInputStream *stream;
2949 stream = g_loadable_icon_load (icon_info->loadable,
2950 icon_info->desired_size,
2952 &icon_info->load_error);
2955 source_pixbuf = gdk_pixbuf_new_from_stream (stream,
2957 &icon_info->load_error);
2958 g_object_unref (stream);
2965 /* Do scale calculations that depend on the image size
2967 image_width = gdk_pixbuf_get_width (source_pixbuf);
2968 image_height = gdk_pixbuf_get_height (source_pixbuf);
2970 if (icon_info->scale < 0.0)
2972 gint image_size = MAX (image_width, image_height);
2974 icon_info->scale = (gdouble)icon_info->desired_size / (gdouble)image_size;
2976 icon_info->scale = 1.0;
2978 if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED &&
2979 !icon_info->forced_size)
2980 icon_info->scale = MIN (icon_info->scale, 1.0);
2983 /* We don't short-circuit out here for scale_only, since, now
2984 * we've loaded the icon, we might as well go ahead and finish
2985 * the job. This is a bit of a waste when we scale here
2986 * and never get the final pixbuf; at the cost of a bit of
2987 * extra complexity, we could keep the source pixbuf around
2988 * but not actually scale it until needed.
2990 if (icon_info->scale == 1.0)
2991 icon_info->pixbuf = source_pixbuf;
2994 icon_info->pixbuf = gdk_pixbuf_scale_simple (source_pixbuf,
2995 0.5 + image_width * icon_info->scale,
2996 0.5 + image_height * icon_info->scale,
2997 GDK_INTERP_BILINEAR);
2998 g_object_unref (source_pixbuf);
3001 apply_emblems (icon_info);
3007 * gtk_icon_info_load_icon:
3008 * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
3009 * @error: (allow-none): location to store error information on failure, or %NULL.
3011 * Renders an icon previously looked up in an icon theme using
3012 * gtk_icon_theme_lookup_icon(); the size will be based on the size
3013 * passed to gtk_icon_theme_lookup_icon(). Note that the resulting
3014 * pixbuf may not be exactly this size; an icon theme may have icons
3015 * that differ slightly from their nominal sizes, and in addition GTK+
3016 * will avoid scaling icons that it considers sufficiently close to the
3017 * requested size or for which the source image would have to be scaled
3018 * up too far. (This maintains sharpness.). This behaviour can be changed
3019 * by passing the %GTK_ICON_LOOKUP_FORCE_SIZE flag when obtaining
3020 * the #GtkIconInfo. If this flag has been specified, the pixbuf
3021 * returned by this function will be scaled to the exact size.
3023 * Return value: the rendered icon; this may be a newly created icon
3024 * or a new reference to an internal icon, so you must not modify
3025 * the icon. Use g_object_unref() to release your reference to the
3031 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
3034 g_return_val_if_fail (icon_info != NULL, NULL);
3035 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
3037 if (!icon_info_ensure_scale_and_pixbuf (icon_info, FALSE))
3039 if (icon_info->load_error)
3040 g_propagate_error (error, icon_info->load_error);
3042 g_set_error_literal (error,
3043 GTK_ICON_THEME_ERROR,
3044 GTK_ICON_THEME_NOT_FOUND,
3045 _("Failed to load icon"));
3050 return g_object_ref (icon_info->pixbuf);
3054 gdk_color_to_css (GdkColor *color)
3056 return g_strdup_printf ("rgb(%d,%d,%d)",
3063 * gtk_icon_info_load_symbolic:
3064 * @icon_info: a #GtkIconInfo
3065 * @fg: a #GdkColor representing the foreground color of the icon
3066 * @success_color: (allow-none): a #GdkColor representing the warning color
3067 * of the icon or %NULL to use the default color
3068 * @warning_color: (allow-none): a #GdkColor representing the warning color
3069 * of the icon or %NULL to use the default color
3070 * @error_color: (allow-none): a #GdkColor representing the error color
3071 * of the icon or %NULL to use the default color (allow-none)
3072 * @was_symbolic: (allow-none): a #gboolean, returns whether the loaded icon
3073 * was a symbolic one and whether the @fg color was applied to it.
3074 * @error: (allow-none): location to store error information on failure,
3077 * Loads an icon, modifying it to match the system colours for the foreground,
3078 * success, warning and error colors provided. If the icon is not a symbolic
3079 * one, the function will return the result from gtk_icon_info_load_icon().
3081 * This allows loading symbolic icons that will match the system theme.
3083 * Unless you are implementing a widget, you will want to use
3084 * g_themed_icon_new_with_default_fallbacks() to load the icon.
3086 * As implementation details, the icon loaded needs to be of SVG type,
3087 * contain the "symbolic" term as the last component of the icon name,
3088 * and use the 'fg', 'success', 'warning' and 'error' CSS styles in the
3091 * See the <ulink url="http://www.freedesktop.org/wiki/SymbolicIcons">Symbolic Icons spec</ulink>
3092 * for more information about symbolic icons.
3094 * Return value: a #GdkPixbuf representing the loaded icon
3099 gtk_icon_info_load_symbolic (GtkIconInfo *icon_info,
3101 GdkColor *success_color,
3102 GdkColor *warning_color,
3103 GdkColor *error_color,
3104 gboolean *was_symbolic,
3108 GInputStream *stream;
3115 g_return_val_if_fail (fg != NULL, NULL);
3117 if (!icon_info->filename ||
3118 !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3121 *was_symbolic = FALSE;
3122 return gtk_icon_info_load_icon (icon_info, error);
3126 *was_symbolic = TRUE;
3128 css_fg = gdk_color_to_css (fg);
3131 GdkColor warning_default_color = { 0, 0xf500, 0x7900, 0x3e00 };
3132 css_warning = gdk_color_to_css (&warning_default_color);
3135 css_warning = gdk_color_to_css (warning_color);
3138 GdkColor error_default_color = { 0, 0xcc00, 0x0000, 0x0000 };
3139 css_error = gdk_color_to_css (&error_default_color);
3142 css_error = gdk_color_to_css (error_color);
3145 GdkColor success_default_color = { 0, 0x4e00, 0x9a00, 0x0600 };
3146 css_success = gdk_color_to_css (&success_default_color);
3149 css_success = gdk_color_to_css (success_color);
3151 data = g_strconcat (
3152 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
3153 "<svg version=\"1.1\"\n"
3154 " xmlns=\"http://www.w3.org/2000/svg\"\n"
3155 " xmlns:xi=\"http://www.w3.org/2001/XInclude\"\n"
3158 " <style type=\"text/css\">\n"
3160 " fill: ", css_fg," !important;\n"
3163 " fill: ", css_warning," !important;\n"
3166 " fill: ", css_error," !important;\n"
3169 " fill: ", css_success," !important;\n"
3172 " <xi:include href=\"", icon_info->filename, "\"/>\n"
3176 g_free (css_warning);
3177 g_free (css_success);
3180 stream = g_memory_input_stream_new_from_data (data, -1, g_free);
3182 pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
3183 icon_info->desired_size,
3184 icon_info->desired_size,
3188 g_object_unref (stream);
3194 * gtk_icon_info_load_symbolic_for_style:
3195 * @icon_info: a #GtkIconInfo
3196 * @style: a #GtkStyle to take the colors from
3197 * @state: the widget state to use for colors
3198 * @was_symbolic: (allow-none): a #gboolean, returns whether the loaded icon
3199 * was a symbolic one and whether the @fg color was applied to it.
3200 * @error: (allow-none): location to store error information on failure,
3203 * Loads an icon, modifying it to match the system colours for the foreground,
3204 * success, warning and error colors provided. If the icon is not a symbolic
3205 * one, the function will return the result from gtk_icon_info_load_icon().
3207 * This allows loading symbolic icons that will match the system theme.
3209 * See gtk_icon_info_load_symbolic() for more details.
3211 * Return value: a #GdkPixbuf representing the loaded icon
3216 gtk_icon_info_load_symbolic_for_style (GtkIconInfo *icon_info,
3219 gboolean *was_symbolic,
3222 GdkColor success_color;
3223 GdkColor warning_color;
3224 GdkColor error_color;
3226 GdkColor *success = NULL;
3227 GdkColor *warning = NULL;
3228 GdkColor *err = NULL;
3230 fg = &style->fg[state];
3231 if (gtk_style_lookup_color (style, "success_color", &success_color))
3232 success = &success_color;
3233 if (gtk_style_lookup_color (style, "warning_color", &warning_color))
3234 warning = &warning_color;
3235 if (gtk_style_lookup_color (style, "error_color", &error_color))
3238 return gtk_icon_info_load_symbolic (icon_info, fg, success, warning, err,
3239 was_symbolic, error);
3243 * gtk_icon_info_set_raw_coordinates:
3244 * @icon_info: a #GtkIconInfo
3245 * @raw_coordinates: whether the coordinates of embedded rectangles
3246 * and attached points should be returned in their original
3249 * Sets whether the coordinates returned by gtk_icon_info_get_embedded_rect()
3250 * and gtk_icon_info_get_attach_points() should be returned in their
3251 * original form as specified in the icon theme, instead of scaled
3252 * appropriately for the pixbuf returned by gtk_icon_info_load_icon().
3254 * Raw coordinates are somewhat strange; they are specified to be with
3255 * respect to the unscaled pixmap for PNG and XPM icons, but for SVG
3256 * icons, they are in a 1000x1000 coordinate space that is scaled
3257 * to the final size of the icon. You can determine if the icon is an SVG
3258 * icon by using gtk_icon_info_get_filename(), and seeing if it is non-%NULL
3259 * and ends in '.svg'.
3261 * This function is provided primarily to allow compatibility wrappers
3262 * for older API's, and is not expected to be useful for applications.
3267 gtk_icon_info_set_raw_coordinates (GtkIconInfo *icon_info,
3268 gboolean raw_coordinates)
3270 g_return_if_fail (icon_info != NULL);
3272 icon_info->raw_coordinates = raw_coordinates != FALSE;
3275 /* Scale coordinates from the icon data prior to returning
3279 icon_info_scale_point (GtkIconInfo *icon_info,
3285 if (icon_info->raw_coordinates)
3292 if (!icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3295 *x_out = 0.5 + x * icon_info->scale;
3296 *y_out = 0.5 + y * icon_info->scale;
3303 * gtk_icon_info_get_embedded_rect:
3304 * @icon_info: a #GtkIconInfo
3305 * @rectangle: #GdkRectangle in which to store embedded
3306 * rectangle coordinates; coordinates are only stored
3307 * when this function returns %TRUE.
3309 * Gets the coordinates of a rectangle within the icon
3310 * that can be used for display of information such
3311 * as a preview of the contents of a text file.
3312 * See gtk_icon_info_set_raw_coordinates() for further
3313 * information about the coordinate system.
3315 * Return value: %TRUE if the icon has an embedded rectangle
3320 gtk_icon_info_get_embedded_rect (GtkIconInfo *icon_info,
3321 GdkRectangle *rectangle)
3323 g_return_val_if_fail (icon_info != NULL, FALSE);
3325 if (icon_info->data && icon_info->data->has_embedded_rect &&
3326 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3328 gint scaled_x0, scaled_y0;
3329 gint scaled_x1, scaled_y1;
3333 icon_info_scale_point (icon_info,
3334 icon_info->data->x0, icon_info->data->y0,
3335 &scaled_x0, &scaled_y0);
3336 icon_info_scale_point (icon_info,
3337 icon_info->data->x1, icon_info->data->y1,
3338 &scaled_x1, &scaled_y1);
3340 rectangle->x = scaled_x0;
3341 rectangle->y = scaled_y0;
3342 rectangle->width = scaled_x1 - rectangle->x;
3343 rectangle->height = scaled_y1 - rectangle->y;
3353 * gtk_icon_info_get_attach_points:
3354 * @icon_info: a #GtkIconInfo
3355 * @points: (allow-none): (array length=n_points) (out): location to store pointer to an array of points, or %NULL
3356 * free the array of points with g_free().
3357 * @n_points: (allow-none): location to store the number of points in @points, or %NULL
3359 * Fetches the set of attach points for an icon. An attach point
3360 * is a location in the icon that can be used as anchor points for attaching
3361 * emblems or overlays to the icon.
3363 * Return value: %TRUE if there are any attach points for the icon.
3368 gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
3372 g_return_val_if_fail (icon_info != NULL, FALSE);
3374 if (icon_info->data && icon_info->data->n_attach_points &&
3375 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3381 *points = g_new (GdkPoint, icon_info->data->n_attach_points);
3382 for (i = 0; i < icon_info->data->n_attach_points; i++)
3383 icon_info_scale_point (icon_info,
3384 icon_info->data->attach_points[i].x,
3385 icon_info->data->attach_points[i].y,
3391 *n_points = icon_info->data->n_attach_points;
3407 * gtk_icon_info_get_display_name:
3408 * @icon_info: a #GtkIconInfo
3410 * Gets the display name for an icon. A display name is a
3411 * string to be used in place of the icon name in a user
3412 * visible context like a list of icons.
3414 * Return value: the display name for the icon or %NULL, if
3415 * the icon doesn't have a specified display name. This value
3416 * is owned @icon_info and must not be modified or free.
3420 G_CONST_RETURN gchar *
3421 gtk_icon_info_get_display_name (GtkIconInfo *icon_info)
3423 g_return_val_if_fail (icon_info != NULL, NULL);
3425 if (icon_info->data)
3426 return icon_info->data->display_name;
3437 * gtk_icon_theme_add_builtin_icon:
3438 * @icon_name: the name of the icon to register
3439 * @size: the size at which to register the icon (different
3440 * images can be registered for the same icon name
3441 * at different sizes.)
3442 * @pixbuf: #GdkPixbuf that contains the image to use
3445 * Registers a built-in icon for icon theme lookups. The idea
3446 * of built-in icons is to allow an application or library
3447 * that uses themed icons to function requiring files to
3448 * be present in the file system. For instance, the default
3449 * images for all of GTK+'s stock icons are registered
3452 * In general, if you use gtk_icon_theme_add_builtin_icon()
3453 * you should also install the icon in the icon theme, so
3454 * that the icon is generally available.
3456 * This function will generally be used with pixbufs loaded
3457 * via gdk_pixbuf_new_from_inline().
3462 gtk_icon_theme_add_builtin_icon (const gchar *icon_name,
3466 BuiltinIcon *default_icon;
3470 g_return_if_fail (icon_name != NULL);
3471 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3473 if (!icon_theme_builtin_icons)
3474 icon_theme_builtin_icons = g_hash_table_new (g_str_hash, g_str_equal);
3476 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
3478 key = g_strdup (icon_name);
3480 key = (gpointer)icon_name; /* Won't get stored */
3482 default_icon = g_new (BuiltinIcon, 1);
3483 default_icon->size = size;
3484 default_icon->pixbuf = g_object_ref (pixbuf);
3485 icons = g_slist_prepend (icons, default_icon);
3487 /* Replaces value, leaves key untouched
3489 g_hash_table_insert (icon_theme_builtin_icons, key, icons);
3492 /* Look up a builtin icon; the min_difference_p and
3493 * has_larger_p out parameters allow us to combine
3494 * this lookup with searching through the actual directories
3495 * of the "hicolor" icon theme. See theme_lookup_icon()
3496 * for how they are used.
3498 static BuiltinIcon *
3499 find_builtin_icon (const gchar *icon_name,
3501 gint *min_difference_p,
3502 gboolean *has_larger_p)
3504 GSList *icons = NULL;
3505 gint min_difference = G_MAXINT;
3506 gboolean has_larger = FALSE;
3507 BuiltinIcon *min_icon = NULL;
3509 if (!icon_theme_builtin_icons)
3512 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
3516 BuiltinIcon *default_icon = icons->data;
3517 int min, max, difference;
3520 min = default_icon->size - 2;
3521 max = default_icon->size + 2;
3522 smaller = size < min;
3524 difference = min - size;
3525 else if (size > max)
3526 difference = size - max;
3530 if (difference == 0)
3532 min_icon = default_icon;
3538 if (difference < min_difference || smaller)
3540 min_difference = difference;
3541 min_icon = default_icon;
3542 has_larger = smaller;
3547 if (difference < min_difference && smaller)
3549 min_difference = difference;
3550 min_icon = default_icon;
3554 icons = icons->next;
3557 if (min_difference_p)
3558 *min_difference_p = min_difference;
3560 *has_larger_p = has_larger;
3566 _gtk_icon_theme_check_reload (GdkDisplay *display)
3570 GtkIconTheme *icon_theme;
3572 n_screens = gdk_display_get_n_screens (display);
3574 for (i = 0; i < n_screens; i++)
3576 screen = gdk_display_get_screen (display, i);
3578 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
3581 icon_theme->priv->check_reload = TRUE;
3582 ensure_valid_themes (icon_theme);
3583 icon_theme->priv->check_reload = FALSE;
3590 * gtk_icon_theme_lookup_by_gicon:
3591 * @icon_theme: a #GtkIconTheme
3592 * @icon: the #GIcon to look up
3593 * @size: desired icon size
3594 * @flags: flags modifying the behavior of the icon lookup
3596 * Looks up an icon and returns a structure containing
3597 * information such as the filename of the icon.
3598 * The icon can then be rendered into a pixbuf using
3599 * gtk_icon_info_load_icon().
3601 * Return value: a #GtkIconInfo structure containing
3602 * information about the icon, or %NULL if the icon
3603 * wasn't found. Free with gtk_icon_info_free()
3608 gtk_icon_theme_lookup_by_gicon (GtkIconTheme *icon_theme,
3611 GtkIconLookupFlags flags)
3615 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
3616 g_return_val_if_fail (G_IS_ICON (icon), NULL);
3618 if (G_IS_LOADABLE_ICON (icon))
3620 info = icon_info_new ();
3621 info->loadable = G_LOADABLE_ICON (g_object_ref (icon));
3623 info->dir_type = ICON_THEME_DIR_UNTHEMED;
3624 info->dir_size = size;
3625 info->desired_size = size;
3626 info->threshold = 2;
3627 info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
3631 else if (G_IS_THEMED_ICON (icon))
3633 const gchar **names;
3635 names = (const gchar **)g_themed_icon_get_names (G_THEMED_ICON (icon));
3636 info = gtk_icon_theme_choose_icon (icon_theme, names, size, flags);
3640 else if (G_IS_EMBLEMED_ICON (icon))
3642 GIcon *base, *emblem;
3644 GtkIconInfo *emblem_info;
3646 base = g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon));
3647 info = gtk_icon_theme_lookup_by_gicon (icon_theme, base, size, flags);
3650 list = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon));
3651 for (l = list; l; l = l->next)
3653 emblem = g_emblem_get_icon (G_EMBLEM (l->data));
3654 emblem_info = gtk_icon_theme_lookup_by_gicon (icon_theme, emblem, size / 2, flags);
3656 info->emblem_infos = g_slist_prepend (info->emblem_infos, emblem_info);
3667 * gtk_icon_info_new_for_pixbuf:
3668 * @icon_theme: a #GtkIconTheme
3669 * @pixbuf: the pixbuf to wrap in a #GtkIconInfo
3671 * Creates a #GtkIconInfo for a #GdkPixbuf.
3673 * Returns: a #GtkIconInfo
3678 gtk_icon_info_new_for_pixbuf (GtkIconTheme *icon_theme,
3683 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
3684 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
3686 info = icon_info_new ();
3687 info->pixbuf = g_object_ref (pixbuf);
3689 info->dir_type = ICON_THEME_DIR_UNTHEMED;