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 "gtknumerableiconprivate.h"
49 #include "gtksettings.h"
50 #include "gtkprivate.h"
53 #undef GDK_DEPRECATED_FOR
54 #define GDK_DEPRECATED
55 #define GDK_DEPRECATED_FOR(f)
56 #undef GTK_DISABLE_DEPRECATED
58 #include "deprecated/gtkstyle.h"
62 * SECTION:gtkicontheme
63 * @Short_description: Looking up icons by name
64 * @Title: GtkIconTheme
66 * #GtkIconTheme provides a facility for looking up icons by name
67 * and size. The main reason for using a name rather than simply
68 * providing a filename is to allow different icons to be used
69 * depending on what <firstterm>icon theme</firstterm> is selected
70 * by the user. The operation of icon themes on Linux and Unix
72 * url="http://www.freedesktop.org/Standards/icon-theme-spec">Icon
73 * Theme Specification</ulink>. There is a default icon theme,
74 * named <literal>hicolor</literal> where applications should install
75 * their icons, but more additional application themes can be
76 * installed as operating system vendors and users choose.
78 * Named icons are similar to the <xref linkend="gtk3-Themeable-Stock-Images"/>
79 * facility, and the distinction between the two may be a bit confusing.
80 * A few things to keep in mind:
83 * Stock images usually are used in conjunction with
84 * <xref linkend="gtk3-Stock-Items"/>, such as %GTK_STOCK_OK or
85 * %GTK_STOCK_OPEN. Named icons are easier to set up and therefore
86 * are more useful for new icons that an application wants to
87 * add, such as application icons or window icons.
90 * Stock images can only be loaded at the symbolic sizes defined
91 * by the #GtkIconSize enumeration, or by custom sizes defined
92 * by gtk_icon_size_register(), while named icons are more flexible
93 * and any pixel size can be specified.
96 * Because stock images are closely tied to stock items, and thus
97 * to actions in the user interface, stock images may come in
98 * multiple variants for different widget states or writing
102 * A good rule of thumb is that if there is a stock image for what
103 * you want to use, use it, otherwise use a named icon. It turns
104 * out that internally stock images are generally defined in
105 * terms of one or more named icons. (An example of the
106 * more than one case is icons that depend on writing direction;
107 * %GTK_STOCK_GO_FORWARD uses the two themed icons
108 * "gtk-stock-go-forward-ltr" and "gtk-stock-go-forward-rtl".)
110 * In many cases, named themes are used indirectly, via #GtkImage
111 * or stock items, rather than directly, but looking up icons
112 * directly is also simple. The #GtkIconTheme object acts
113 * as a database of all the icons in the current theme. You
114 * can create new #GtkIconTheme objects, but its much more
115 * efficient to use the standard icon theme for the #GdkScreen
116 * so that the icon information is shared with other people
117 * looking up icons. In the case where the default screen is
118 * being used, looking up an icon can be as simple as:
121 * GError *error = NULL;
122 * GtkIconTheme *icon_theme;
125 * icon_theme = gtk_icon_theme_get_default ();
126 * pixbuf = gtk_icon_theme_load_icon (icon_theme,
127 * "my-icon-name", // icon name
133 * g_warning ("Couldn't load icon: %s", error->message);
134 * g_error_free (error);
139 * g_object_unref (pixbuf);
146 #define DEFAULT_THEME_NAME "hicolor"
150 ICON_THEME_DIR_FIXED,
151 ICON_THEME_DIR_SCALABLE,
152 ICON_THEME_DIR_THRESHOLD,
153 ICON_THEME_DIR_UNTHEMED
156 /* In reverse search order: */
159 ICON_SUFFIX_NONE = 0,
160 ICON_SUFFIX_XPM = 1 << 0,
161 ICON_SUFFIX_SVG = 1 << 1,
162 ICON_SUFFIX_PNG = 1 << 2,
163 HAS_ICON_FILE = 1 << 3
167 struct _GtkIconThemePrivate
169 gchar *current_theme;
170 gchar *fallback_theme;
172 gint search_path_len;
174 guint custom_theme : 1;
175 guint is_screen_singleton : 1;
176 guint pixbuf_supports_svg : 1;
177 guint themes_valid : 1;
178 guint check_reload : 1;
179 guint loading_themes : 1;
181 /* A list of all the themes needed to look up icons.
182 * In search order, without duplicates
185 GHashTable *unthemed_icons;
187 /* Note: The keys of this hashtable are owned by the
188 * themedir and unthemed hashtables.
190 GHashTable *all_icons;
192 /* GdkScreen for the icon theme (may be NULL)
196 /* time when we last stat:ed for theme changes */
197 glong last_stat_time;
200 gulong reset_styles_idle;
205 /* Information about the source
208 GLoadableIcon *loadable;
209 GSList *emblem_infos;
211 /* Cache pixbuf (if there is any) */
212 GdkPixbuf *cache_pixbuf;
216 /* Information about the directory where
217 * the source was found
219 IconThemeDirType dir_type;
223 /* Parameters influencing the scaled icon
226 guint raw_coordinates : 1;
227 guint forced_size : 1;
228 guint emblems_applied : 1;
232 /* Cached information if we go ahead and try to load
247 /* In search order */
253 IconThemeDirType type;
268 GHashTable *icon_data;
274 char *no_svg_filename;
286 time_t mtime; /* 0 == not existing or not a dir */
291 static void gtk_icon_theme_finalize (GObject *object);
292 static void theme_dir_destroy (IconThemeDir *dir);
294 static void theme_destroy (IconTheme *theme);
295 static GtkIconInfo *theme_lookup_icon (IconTheme *theme,
296 const char *icon_name,
299 gboolean use_default_icons);
300 static void theme_list_icons (IconTheme *theme,
303 static void theme_list_contexts (IconTheme *theme,
304 GHashTable *contexts);
305 static void theme_subdir_load (GtkIconTheme *icon_theme,
307 GKeyFile *theme_file,
309 static void do_theme_change (GtkIconTheme *icon_theme);
311 static void blow_themes (GtkIconTheme *icon_themes);
312 static gboolean rescan_themes (GtkIconTheme *icon_themes);
314 static void icon_data_free (GtkIconData *icon_data);
315 static void load_icon_data (IconThemeDir *dir,
319 static IconSuffix theme_dir_get_icon_suffix (IconThemeDir *dir,
320 const gchar *icon_name,
321 gboolean *has_icon_file);
324 static GtkIconInfo *icon_info_new (void);
325 static GtkIconInfo *icon_info_new_builtin (BuiltinIcon *icon);
327 static IconSuffix suffix_from_name (const char *name);
329 static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
331 gint *min_difference_p,
332 gboolean *has_larger_p);
334 static guint signal_changed = 0;
336 static GHashTable *icon_theme_builtin_icons;
338 /* also used in gtkiconfactory.c */
339 GtkIconCache *_builtin_cache = NULL;
340 static GList *builtin_dirs = NULL;
342 G_DEFINE_TYPE (GtkIconTheme, gtk_icon_theme, G_TYPE_OBJECT)
345 * gtk_icon_theme_new:
347 * Creates a new icon theme object. Icon theme objects are used
348 * to lookup up an icon by name in a particular icon theme.
349 * Usually, you'll want to use gtk_icon_theme_get_default()
350 * or gtk_icon_theme_get_for_screen() rather than creating
351 * a new icon theme object for scratch.
353 * Return value: the newly created #GtkIconTheme object.
358 gtk_icon_theme_new (void)
360 return g_object_new (GTK_TYPE_ICON_THEME, NULL);
364 * gtk_icon_theme_get_default:
366 * Gets the icon theme for the default screen. See
367 * gtk_icon_theme_get_for_screen().
369 * Return value: (transfer none): A unique #GtkIconTheme associated with
370 * the default screen. This icon theme is associated with
371 * the screen and can be used as long as the screen
372 * is open. Do not ref or unref it.
377 gtk_icon_theme_get_default (void)
379 return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
383 * gtk_icon_theme_get_for_screen:
384 * @screen: a #GdkScreen
386 * Gets the icon theme object associated with @screen; if this
387 * function has not previously been called for the given
388 * screen, a new icon theme object will be created and
389 * associated with the screen. Icon theme objects are
390 * fairly expensive to create, so using this function
391 * is usually a better choice than calling than gtk_icon_theme_new()
392 * and setting the screen yourself; by using this function
393 * a single icon theme object will be shared between users.
395 * Return value: (transfer none): A unique #GtkIconTheme associated with
396 * the given screen. This icon theme is associated with
397 * the screen and can be used as long as the screen
398 * is open. Do not ref or unref it.
403 gtk_icon_theme_get_for_screen (GdkScreen *screen)
405 GtkIconTheme *icon_theme;
407 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
409 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
412 GtkIconThemePrivate *priv;
414 icon_theme = gtk_icon_theme_new ();
415 gtk_icon_theme_set_screen (icon_theme, screen);
417 priv = icon_theme->priv;
418 priv->is_screen_singleton = TRUE;
420 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), icon_theme);
427 gtk_icon_theme_class_init (GtkIconThemeClass *klass)
429 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
431 gobject_class->finalize = gtk_icon_theme_finalize;
434 * GtkIconTheme::changed
435 * @icon_theme: the icon theme
437 * Emitted when the current icon theme is switched or GTK+ detects
438 * that a change has occurred in the contents of the current
441 signal_changed = g_signal_new (I_("changed"),
442 G_TYPE_FROM_CLASS (klass),
444 G_STRUCT_OFFSET (GtkIconThemeClass, changed),
446 g_cclosure_marshal_VOID__VOID,
449 g_type_class_add_private (klass, sizeof (GtkIconThemePrivate));
453 /* Callback when the display that the icon theme is attached to
454 * is closed; unset the screen, and if it's the unique theme
455 * for the screen, drop the reference
458 display_closed (GdkDisplay *display,
460 GtkIconTheme *icon_theme)
462 GtkIconThemePrivate *priv = icon_theme->priv;
463 GdkScreen *screen = priv->screen;
464 gboolean was_screen_singleton = priv->is_screen_singleton;
466 if (was_screen_singleton)
468 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), NULL);
469 priv->is_screen_singleton = FALSE;
472 gtk_icon_theme_set_screen (icon_theme, NULL);
474 if (was_screen_singleton)
476 g_object_unref (icon_theme);
481 update_current_theme (GtkIconTheme *icon_theme)
483 #define theme_changed(_old, _new) \
484 ((_old && !_new) || (!_old && _new) || \
485 (_old && _new && strcmp (_old, _new) != 0))
486 GtkIconThemePrivate *priv = icon_theme->priv;
488 if (!priv->custom_theme)
491 gchar *fallback_theme = NULL;
492 gboolean changed = FALSE;
496 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
497 g_object_get (settings,
498 "gtk-icon-theme-name", &theme,
499 "gtk-fallback-icon-theme", &fallback_theme, NULL);
502 /* ensure that the current theme (even when just the default)
503 * is searched before any fallback theme
505 if (!theme && fallback_theme)
506 theme = g_strdup (DEFAULT_THEME_NAME);
508 if (theme_changed (priv->current_theme, theme))
510 g_free (priv->current_theme);
511 priv->current_theme = theme;
517 if (theme_changed (priv->fallback_theme, fallback_theme))
519 g_free (priv->fallback_theme);
520 priv->fallback_theme = fallback_theme;
524 g_free (fallback_theme);
527 do_theme_change (icon_theme);
532 /* Callback when the icon theme GtkSetting changes
535 theme_changed (GtkSettings *settings,
537 GtkIconTheme *icon_theme)
539 update_current_theme (icon_theme);
543 unset_screen (GtkIconTheme *icon_theme)
545 GtkIconThemePrivate *priv = icon_theme->priv;
546 GtkSettings *settings;
551 settings = gtk_settings_get_for_screen (priv->screen);
552 display = gdk_screen_get_display (priv->screen);
554 g_signal_handlers_disconnect_by_func (display,
555 (gpointer) display_closed,
557 g_signal_handlers_disconnect_by_func (settings,
558 (gpointer) theme_changed,
566 * gtk_icon_theme_set_screen:
567 * @icon_theme: a #GtkIconTheme
568 * @screen: a #GdkScreen
570 * Sets the screen for an icon theme; the screen is used
571 * to track the user's currently configured icon theme,
572 * which might be different for different screens.
577 gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
580 GtkIconThemePrivate *priv;
581 GtkSettings *settings;
584 g_return_if_fail (GTK_ICON_THEME (icon_theme));
585 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
587 priv = icon_theme->priv;
589 unset_screen (icon_theme);
593 display = gdk_screen_get_display (screen);
594 settings = gtk_settings_get_for_screen (screen);
596 priv->screen = screen;
598 g_signal_connect (display, "closed",
599 G_CALLBACK (display_closed), icon_theme);
600 g_signal_connect (settings, "notify::gtk-icon-theme-name",
601 G_CALLBACK (theme_changed), icon_theme);
602 g_signal_connect (settings, "notify::gtk-fallback-icon-theme-name",
603 G_CALLBACK (theme_changed), icon_theme);
606 update_current_theme (icon_theme);
609 /* Checks whether a loader for SVG files has been registered
613 pixbuf_supports_svg (void)
617 static gint found_svg = -1;
622 formats = gdk_pixbuf_get_formats ();
625 for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
627 gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
630 for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
632 if (strcmp (*mime_type, "image/svg") == 0)
636 g_strfreev (mime_types);
639 g_slist_free (formats);
645 gtk_icon_theme_init (GtkIconTheme *icon_theme)
647 GtkIconThemePrivate *priv;
648 const gchar * const *xdg_data_dirs;
651 priv = G_TYPE_INSTANCE_GET_PRIVATE (icon_theme,
653 GtkIconThemePrivate);
654 icon_theme->priv = priv;
656 priv->custom_theme = FALSE;
658 xdg_data_dirs = g_get_system_data_dirs ();
659 for (i = 0; xdg_data_dirs[i]; i++) ;
661 priv->search_path_len = 2 * i + 2;
663 priv->search_path = g_new (char *, priv->search_path_len);
666 priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
667 priv->search_path[i++] = g_build_filename (g_get_user_data_dir (), "icons", NULL);
669 for (j = 0; xdg_data_dirs[j]; j++)
670 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
672 for (j = 0; xdg_data_dirs[j]; j++)
673 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "pixmaps", NULL);
675 priv->themes_valid = FALSE;
677 priv->unthemed_icons = NULL;
679 priv->pixbuf_supports_svg = pixbuf_supports_svg ();
683 free_dir_mtime (IconThemeDirMtime *dir_mtime)
685 if (dir_mtime->cache)
686 _gtk_icon_cache_unref (dir_mtime->cache);
688 g_free (dir_mtime->dir);
689 g_slice_free (IconThemeDirMtime, dir_mtime);
694 reset_styles_idle (gpointer user_data)
696 GtkIconTheme *icon_theme;
697 GtkIconThemePrivate *priv;
699 icon_theme = GTK_ICON_THEME (user_data);
700 priv = icon_theme->priv;
702 if (priv->screen && priv->is_screen_singleton)
703 gtk_style_context_reset_widgets (priv->screen);
705 priv->reset_styles_idle = 0;
711 do_theme_change (GtkIconTheme *icon_theme)
713 GtkIconThemePrivate *priv = icon_theme->priv;
715 if (!priv->themes_valid)
719 g_print ("change to icon theme \"%s\"\n", priv->current_theme));
720 blow_themes (icon_theme);
721 g_signal_emit (icon_theme, signal_changed, 0);
723 if (!priv->reset_styles_idle)
724 priv->reset_styles_idle =
725 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2,
726 reset_styles_idle, icon_theme, NULL);
730 blow_themes (GtkIconTheme *icon_theme)
732 GtkIconThemePrivate *priv = icon_theme->priv;
734 if (priv->themes_valid)
736 g_hash_table_destroy (priv->all_icons);
737 g_list_free_full (priv->themes, (GDestroyNotify) theme_destroy);
738 g_list_free_full (priv->dir_mtimes, (GDestroyNotify) free_dir_mtime);
739 g_hash_table_destroy (priv->unthemed_icons);
742 priv->unthemed_icons = NULL;
743 priv->dir_mtimes = NULL;
744 priv->all_icons = NULL;
745 priv->themes_valid = FALSE;
749 gtk_icon_theme_finalize (GObject *object)
751 GtkIconTheme *icon_theme;
752 GtkIconThemePrivate *priv;
755 icon_theme = GTK_ICON_THEME (object);
756 priv = icon_theme->priv;
758 if (priv->reset_styles_idle)
760 g_source_remove (priv->reset_styles_idle);
761 priv->reset_styles_idle = 0;
764 unset_screen (icon_theme);
766 g_free (priv->current_theme);
767 priv->current_theme = NULL;
769 for (i = 0; i < priv->search_path_len; i++)
770 g_free (priv->search_path[i]);
772 g_free (priv->search_path);
773 priv->search_path = NULL;
775 blow_themes (icon_theme);
777 G_OBJECT_CLASS (gtk_icon_theme_parent_class)->finalize (object);
781 * gtk_icon_theme_set_search_path:
782 * @icon_theme: a #GtkIconTheme
783 * @path: (array length=n_elements) (element-type filename): array of
784 * directories that are searched for icon themes
785 * @n_elements: number of elements in @path.
787 * Sets the search path for the icon theme object. When looking
788 * for an icon theme, GTK+ will search for a subdirectory of
789 * one or more of the directories in @path with the same name
790 * as the icon theme. (Themes from multiple of the path elements
791 * are combined to allow themes to be extended by adding icons
792 * in the user's home directory.)
794 * In addition if an icon found isn't found either in the current
795 * icon theme or the default icon theme, and an image file with
796 * the right name is found directly in one of the elements of
797 * @path, then that image will be used for the icon name.
798 * (This is legacy feature, and new icons should be put
799 * into the default icon theme, which is called DEFAULT_THEME_NAME,
800 * rather than directly on the icon path.)
805 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
809 GtkIconThemePrivate *priv;
812 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
814 priv = icon_theme->priv;
815 for (i = 0; i < priv->search_path_len; i++)
816 g_free (priv->search_path[i]);
818 g_free (priv->search_path);
820 priv->search_path = g_new (gchar *, n_elements);
821 priv->search_path_len = n_elements;
823 for (i = 0; i < priv->search_path_len; i++)
824 priv->search_path[i] = g_strdup (path[i]);
826 do_theme_change (icon_theme);
831 * gtk_icon_theme_get_search_path:
832 * @icon_theme: a #GtkIconTheme
833 * @path: (allow-none) (array length=n_elements) (element-type filename) (out):
834 * location to store a list of icon theme path directories or %NULL.
835 * The stored value should be freed with g_strfreev().
836 * @n_elements: location to store number of elements in @path, or %NULL
838 * Gets the current search path. See gtk_icon_theme_set_search_path().
843 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
847 GtkIconThemePrivate *priv;
850 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
852 priv = icon_theme->priv;
855 *n_elements = priv->search_path_len;
859 *path = g_new (gchar *, priv->search_path_len + 1);
860 for (i = 0; i < priv->search_path_len; i++)
861 (*path)[i] = g_strdup (priv->search_path[i]);
867 * gtk_icon_theme_append_search_path:
868 * @icon_theme: a #GtkIconTheme
869 * @path: (type filename): directory name to append to the icon path
871 * Appends a directory to the search path.
872 * See gtk_icon_theme_set_search_path().
877 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
880 GtkIconThemePrivate *priv;
882 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
883 g_return_if_fail (path != NULL);
885 priv = icon_theme->priv;
887 priv->search_path_len++;
889 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
890 priv->search_path[priv->search_path_len-1] = g_strdup (path);
892 do_theme_change (icon_theme);
896 * gtk_icon_theme_prepend_search_path:
897 * @icon_theme: a #GtkIconTheme
898 * @path: (type filename): directory name to prepend to the icon path
900 * Prepends a directory to the search path.
901 * See gtk_icon_theme_set_search_path().
906 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
909 GtkIconThemePrivate *priv;
912 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
913 g_return_if_fail (path != NULL);
915 priv = icon_theme->priv;
917 priv->search_path_len++;
918 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
920 for (i = priv->search_path_len - 1; i > 0; i--)
921 priv->search_path[i] = priv->search_path[i - 1];
923 priv->search_path[0] = g_strdup (path);
925 do_theme_change (icon_theme);
929 * gtk_icon_theme_set_custom_theme:
930 * @icon_theme: a #GtkIconTheme
931 * @theme_name: (allow-none): name of icon theme to use instead of
932 * configured theme, or %NULL to unset a previously set custom theme
934 * Sets the name of the icon theme that the #GtkIconTheme object uses
935 * overriding system configuration. This function cannot be called
936 * on the icon theme objects returned from gtk_icon_theme_get_default()
937 * and gtk_icon_theme_get_for_screen().
942 gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
943 const gchar *theme_name)
945 GtkIconThemePrivate *priv;
947 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
949 priv = icon_theme->priv;
951 g_return_if_fail (!priv->is_screen_singleton);
953 if (theme_name != NULL)
955 priv->custom_theme = TRUE;
956 if (!priv->current_theme || strcmp (theme_name, priv->current_theme) != 0)
958 g_free (priv->current_theme);
959 priv->current_theme = g_strdup (theme_name);
961 do_theme_change (icon_theme);
966 if (priv->custom_theme)
968 priv->custom_theme = FALSE;
970 update_current_theme (icon_theme);
976 insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
982 GtkIconThemePrivate *priv;
983 IconTheme *theme = NULL;
985 GKeyFile *theme_file;
986 GError *error = NULL;
987 IconThemeDirMtime *dir_mtime;
990 priv = icon_theme->priv;
992 for (l = priv->themes; l != NULL; l = l->next)
995 if (strcmp (theme->name, theme_name) == 0)
999 for (i = 0; i < priv->search_path_len; i++)
1001 path = g_build_filename (priv->search_path[i],
1004 dir_mtime = g_slice_new (IconThemeDirMtime);
1005 dir_mtime->cache = NULL;
1006 dir_mtime->dir = path;
1007 if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
1008 dir_mtime->mtime = stat_buf.st_mtime;
1010 dir_mtime->mtime = 0;
1012 priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
1014 priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
1017 for (i = 0; i < priv->search_path_len && !theme_file; i++)
1019 path = g_build_filename (priv->search_path[i],
1023 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
1025 theme_file = g_key_file_new ();
1026 g_key_file_set_list_separator (theme_file, ',');
1027 g_key_file_load_from_file (theme_file, path, 0, &error);
1030 g_key_file_free (theme_file);
1032 g_error_free (error);
1039 if (theme_file || strcmp (theme_name, DEFAULT_THEME_NAME) == 0)
1041 theme = g_new0 (IconTheme, 1);
1042 theme->name = g_strdup (theme_name);
1043 priv->themes = g_list_prepend (priv->themes, theme);
1046 if (theme_file == NULL)
1049 theme->display_name =
1050 g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
1051 if (!theme->display_name)
1052 g_warning ("Theme file for %s has no name\n", theme_name);
1054 dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
1057 g_warning ("Theme file for %s has no directories\n", theme_name);
1058 priv->themes = g_list_remove (priv->themes, theme);
1059 g_free (theme->name);
1060 g_free (theme->display_name);
1062 g_key_file_free (theme_file);
1067 g_key_file_get_locale_string (theme_file,
1068 "Icon Theme", "Comment",
1071 g_key_file_get_string (theme_file,
1072 "Icon Theme", "Example",
1076 for (i = 0; dirs[i] != NULL; i++)
1077 theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
1081 theme->dirs = g_list_reverse (theme->dirs);
1083 themes = g_key_file_get_string_list (theme_file,
1090 for (i = 0; themes[i] != NULL; i++)
1091 insert_theme (icon_theme, themes[i]);
1093 g_strfreev (themes);
1096 g_key_file_free (theme_file);
1100 free_unthemed_icon (UnthemedIcon *unthemed_icon)
1102 g_free (unthemed_icon->svg_filename);
1103 g_free (unthemed_icon->no_svg_filename);
1104 g_slice_free (UnthemedIcon, unthemed_icon);
1108 strip_suffix (const char *filename)
1112 dot = strrchr (filename, '.');
1115 return g_strdup (filename);
1117 return g_strndup (filename, dot - filename);
1121 load_themes (GtkIconTheme *icon_theme)
1123 GtkIconThemePrivate *priv;
1128 UnthemedIcon *unthemed_icon;
1129 IconSuffix old_suffix, new_suffix;
1131 IconThemeDirMtime *dir_mtime;
1134 priv = icon_theme->priv;
1136 priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
1138 if (priv->current_theme)
1139 insert_theme (icon_theme, priv->current_theme);
1141 /* Always look in the "default" icon theme, and in a fallback theme */
1142 if (priv->fallback_theme)
1143 insert_theme (icon_theme, priv->fallback_theme);
1144 insert_theme (icon_theme, DEFAULT_THEME_NAME);
1145 priv->themes = g_list_reverse (priv->themes);
1148 priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
1149 g_free, (GDestroyNotify)free_unthemed_icon);
1151 for (base = 0; base < icon_theme->priv->search_path_len; base++)
1153 dir = icon_theme->priv->search_path[base];
1155 dir_mtime = g_slice_new (IconThemeDirMtime);
1156 priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
1158 dir_mtime->dir = g_strdup (dir);
1159 dir_mtime->mtime = 0;
1160 dir_mtime->cache = NULL;
1162 if (g_stat (dir, &stat_buf) != 0 || !S_ISDIR (stat_buf.st_mode))
1164 dir_mtime->mtime = stat_buf.st_mtime;
1166 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir);
1167 if (dir_mtime->cache != NULL)
1170 gdir = g_dir_open (dir, 0, NULL);
1174 while ((file = g_dir_read_name (gdir)))
1176 new_suffix = suffix_from_name (file);
1178 if (new_suffix != ICON_SUFFIX_NONE)
1183 abs_file = g_build_filename (dir, file, NULL);
1184 base_name = strip_suffix (file);
1186 if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
1189 if (new_suffix == ICON_SUFFIX_SVG)
1191 if (unthemed_icon->svg_filename)
1194 unthemed_icon->svg_filename = abs_file;
1198 if (unthemed_icon->no_svg_filename)
1200 old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
1201 if (new_suffix > old_suffix)
1203 g_free (unthemed_icon->no_svg_filename);
1204 unthemed_icon->no_svg_filename = abs_file;
1210 unthemed_icon->no_svg_filename = abs_file;
1217 unthemed_icon = g_slice_new0 (UnthemedIcon);
1219 if (new_suffix == ICON_SUFFIX_SVG)
1220 unthemed_icon->svg_filename = abs_file;
1222 unthemed_icon->no_svg_filename = abs_file;
1224 /* takes ownership of base_name */
1225 g_hash_table_replace (priv->unthemed_icons,
1228 g_hash_table_insert (priv->all_icons,
1236 priv->themes_valid = TRUE;
1238 g_get_current_time(&tv);
1239 priv->last_stat_time = tv.tv_sec;
1243 _gtk_icon_theme_ensure_builtin_cache (void)
1245 static gboolean initialized = FALSE;
1247 static IconThemeDir dirs[5] =
1249 { ICON_THEME_DIR_THRESHOLD, 0, 16, 16, 16, 2, NULL, "16", -1, NULL, NULL, NULL },
1250 { ICON_THEME_DIR_THRESHOLD, 0, 20, 20, 20, 2, NULL, "20", -1, NULL, NULL, NULL },
1251 { ICON_THEME_DIR_THRESHOLD, 0, 24, 24, 24, 2, NULL, "24", -1, NULL, NULL, NULL },
1252 { ICON_THEME_DIR_THRESHOLD, 0, 32, 32, 32, 2, NULL, "32", -1, NULL, NULL, NULL },
1253 { ICON_THEME_DIR_THRESHOLD, 0, 48, 48, 48, 2, NULL, "48", -1, NULL, NULL, NULL }
1261 _builtin_cache = _gtk_icon_cache_new ((gchar *)builtin_icons);
1263 for (i = 0; i < G_N_ELEMENTS (dirs); i++)
1266 dir->cache = _gtk_icon_cache_ref (_builtin_cache);
1267 dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
1269 builtin_dirs = g_list_append (builtin_dirs, dir);
1275 ensure_valid_themes (GtkIconTheme *icon_theme)
1277 GtkIconThemePrivate *priv = icon_theme->priv;
1279 gboolean was_valid = priv->themes_valid;
1281 if (priv->loading_themes)
1283 priv->loading_themes = TRUE;
1285 _gtk_icon_theme_ensure_builtin_cache ();
1287 if (priv->themes_valid)
1289 g_get_current_time (&tv);
1291 if (ABS (tv.tv_sec - priv->last_stat_time) > 5 &&
1292 rescan_themes (icon_theme))
1293 blow_themes (icon_theme);
1296 if (!priv->themes_valid)
1298 load_themes (icon_theme);
1302 g_signal_emit (icon_theme, signal_changed, 0);
1306 priv->loading_themes = FALSE;
1309 static GtkIconInfo *
1310 choose_icon (GtkIconTheme *icon_theme,
1311 const gchar *icon_names[],
1313 GtkIconLookupFlags flags)
1315 GtkIconThemePrivate *priv;
1317 GtkIconInfo *icon_info = NULL;
1318 UnthemedIcon *unthemed_icon = NULL;
1320 gboolean use_builtin;
1323 priv = icon_theme->priv;
1325 if (flags & GTK_ICON_LOOKUP_NO_SVG)
1327 else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
1330 allow_svg = priv->pixbuf_supports_svg;
1332 use_builtin = flags & GTK_ICON_LOOKUP_USE_BUILTIN;
1334 ensure_valid_themes (icon_theme);
1336 for (l = priv->themes; l; l = l->next)
1338 IconTheme *theme = l->data;
1340 for (i = 0; icon_names[i]; i++)
1342 icon_info = theme_lookup_icon (theme, icon_names[i], size, allow_svg, use_builtin);
1348 for (i = 0; icon_names[i]; i++)
1350 unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_names[i]);
1355 /* Still not found an icon, check if reference to a Win32 resource */
1361 resources = g_strsplit (icon_names[0], ",", 0);
1364 wchar_t *wfile = g_utf8_to_utf16 (resources[0], -1, NULL, NULL, NULL);
1365 ExtractIconExW (wfile, resources[1] ? atoi (resources[1]) : 0, &hIcon, NULL, 1);
1371 icon_info = icon_info_new ();
1372 icon_info->cache_pixbuf = gdk_win32_icon_to_pixbuf_libgtk_only (hIcon);
1373 DestroyIcon (hIcon);
1374 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1375 icon_info->dir_size = size;
1377 g_strfreev (resources);
1383 icon_info = icon_info_new ();
1385 /* A SVG icon, when allowed, beats out a XPM icon, but not
1389 unthemed_icon->svg_filename &&
1390 (!unthemed_icon->no_svg_filename ||
1391 suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
1392 icon_info->filename = g_strdup (unthemed_icon->svg_filename);
1393 else if (unthemed_icon->no_svg_filename)
1394 icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);
1396 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1397 icon_info->dir_size = size;
1403 icon_info->desired_size = size;
1404 icon_info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
1408 static gboolean check_for_default_theme = TRUE;
1409 char *default_theme_path;
1410 gboolean found = FALSE;
1413 if (check_for_default_theme)
1415 check_for_default_theme = FALSE;
1417 for (i = 0; !found && i < priv->search_path_len; i++)
1419 default_theme_path = g_build_filename (priv->search_path[i],
1423 found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
1424 g_free (default_theme_path);
1429 g_warning ("Could not find the icon '%s'. The '%s' theme\n"
1430 "was not found either, perhaps you need to install it.\n"
1431 "You can get a copy from:\n"
1433 icon_names[0], DEFAULT_THEME_NAME, "http://icon-theme.freedesktop.org/releases");
1443 * gtk_icon_theme_lookup_icon:
1444 * @icon_theme: a #GtkIconTheme
1445 * @icon_name: the name of the icon to lookup
1446 * @size: desired icon size
1447 * @flags: flags modifying the behavior of the icon lookup
1449 * Looks up a named icon and returns a structure containing
1450 * information such as the filename of the icon. The icon
1451 * can then be rendered into a pixbuf using
1452 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1453 * combines these two steps if all you need is the pixbuf.)
1455 * Return value: a #GtkIconInfo structure containing information
1456 * about the icon, or %NULL if the icon wasn't found. Free with
1457 * gtk_icon_info_free()
1462 gtk_icon_theme_lookup_icon (GtkIconTheme *icon_theme,
1463 const gchar *icon_name,
1465 GtkIconLookupFlags flags)
1469 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1470 g_return_val_if_fail (icon_name != 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 GTK_NOTE (ICONTHEME,
1475 g_print ("gtk_icon_theme_lookup_icon %s\n", icon_name));
1477 if (flags & GTK_ICON_LOOKUP_GENERIC_FALLBACK)
1484 for (p = (gchar *) icon_name; *p; p++)
1488 names = g_new (gchar *, dashes + 2);
1489 names[0] = g_strdup (icon_name);
1490 for (i = 1; i <= dashes; i++)
1492 names[i] = g_strdup (names[i - 1]);
1493 p = strrchr (names[i], '-');
1496 names[dashes + 1] = NULL;
1498 info = choose_icon (icon_theme, (const gchar **) names, size, flags);
1504 const gchar *names[2];
1506 names[0] = icon_name;
1509 info = choose_icon (icon_theme, names, size, flags);
1516 * gtk_icon_theme_choose_icon:
1517 * @icon_theme: a #GtkIconTheme
1518 * @icon_names: (array zero-terminated=1): %NULL-terminated array of
1519 * icon names to lookup
1520 * @size: desired icon size
1521 * @flags: flags modifying the behavior of the icon lookup
1523 * Looks up a named icon and returns a structure containing
1524 * information such as the filename of the icon. The icon
1525 * can then be rendered into a pixbuf using
1526 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1527 * combines these two steps if all you need is the pixbuf.)
1529 * If @icon_names contains more than one name, this function
1530 * tries them all in the given order before falling back to
1531 * inherited icon themes.
1533 * Return value: a #GtkIconInfo structure containing information
1534 * about the icon, or %NULL if the icon wasn't found. Free with
1535 * gtk_icon_info_free()
1540 gtk_icon_theme_choose_icon (GtkIconTheme *icon_theme,
1541 const gchar *icon_names[],
1543 GtkIconLookupFlags flags)
1545 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1546 g_return_val_if_fail (icon_names != NULL, NULL);
1547 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1548 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1550 return choose_icon (icon_theme, icon_names, size, flags);
1555 gtk_icon_theme_error_quark (void)
1557 return g_quark_from_static_string ("gtk-icon-theme-error-quark");
1561 * gtk_icon_theme_load_icon:
1562 * @icon_theme: a #GtkIconTheme
1563 * @icon_name: the name of the icon to lookup
1564 * @size: the desired icon size. The resulting icon may not be
1565 * exactly this size; see gtk_icon_info_load_icon().
1566 * @flags: flags modifying the behavior of the icon lookup
1567 * @error: (allow-none): Location to store error information on failure,
1570 * Looks up an icon in an icon theme, scales it to the given size
1571 * and renders it into a pixbuf. This is a convenience function;
1572 * if more details about the icon are needed, use
1573 * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1575 * Note that you probably want to listen for icon theme changes and
1576 * update the icon. This is usually done by connecting to the
1577 * GtkWidget::style-set signal. If for some reason you do not want to
1578 * update the icon when the icon theme changes, you should consider
1579 * using gdk_pixbuf_copy() to make a private copy of the pixbuf
1580 * returned by this function. Otherwise GTK+ may need to keep the old
1581 * icon theme loaded, which would be a waste of memory.
1583 * Return value: (transfer full): the rendered icon; this may be a
1584 * newly created icon or a new reference to an internal icon, so
1585 * you must not modify the icon. Use g_object_unref() to release
1586 * your reference to the icon. %NULL if the icon isn't found.
1591 gtk_icon_theme_load_icon (GtkIconTheme *icon_theme,
1592 const gchar *icon_name,
1594 GtkIconLookupFlags flags,
1597 GtkIconInfo *icon_info;
1598 GdkPixbuf *pixbuf = NULL;
1600 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1601 g_return_val_if_fail (icon_name != NULL, NULL);
1602 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1603 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1604 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1606 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
1607 flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1610 g_set_error (error, GTK_ICON_THEME_ERROR, GTK_ICON_THEME_NOT_FOUND,
1611 _("Icon '%s' not present in theme"), icon_name);
1615 pixbuf = gtk_icon_info_load_icon (icon_info, error);
1616 gtk_icon_info_free (icon_info);
1622 * gtk_icon_theme_has_icon:
1623 * @icon_theme: a #GtkIconTheme
1624 * @icon_name: the name of an icon
1626 * Checks whether an icon theme includes an icon
1627 * for a particular name.
1629 * Return value: %TRUE if @icon_theme includes an
1630 * icon for @icon_name.
1635 gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
1636 const char *icon_name)
1638 GtkIconThemePrivate *priv;
1641 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1642 g_return_val_if_fail (icon_name != NULL, FALSE);
1644 priv = icon_theme->priv;
1646 ensure_valid_themes (icon_theme);
1648 for (l = priv->dir_mtimes; l; l = l->next)
1650 IconThemeDirMtime *dir_mtime = l->data;
1651 GtkIconCache *cache = dir_mtime->cache;
1653 if (cache && _gtk_icon_cache_has_icon (cache, icon_name))
1657 if (g_hash_table_lookup_extended (priv->all_icons,
1658 icon_name, NULL, NULL))
1661 if (_builtin_cache &&
1662 _gtk_icon_cache_has_icon (_builtin_cache, icon_name))
1665 if (icon_theme_builtin_icons &&
1666 g_hash_table_lookup_extended (icon_theme_builtin_icons,
1667 icon_name, NULL, NULL))
1674 add_size (gpointer key,
1678 gint **res_p = user_data;
1680 **res_p = GPOINTER_TO_INT (key);
1686 * gtk_icon_theme_get_icon_sizes:
1687 * @icon_theme: a #GtkIconTheme
1688 * @icon_name: the name of an icon
1690 * Returns an array of integers describing the sizes at which
1691 * the icon is available without scaling. A size of -1 means
1692 * that the icon is available in a scalable format. The array
1693 * is zero-terminated.
1695 * Return value: (array zero-terminated=1): An newly allocated array
1696 * describing the sizes at which the icon is available. The array
1697 * should be freed with g_free() when it is no longer needed.
1702 gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
1703 const char *icon_name)
1705 GList *l, *d, *icons;
1709 GtkIconThemePrivate *priv;
1711 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1713 priv = icon_theme->priv;
1715 ensure_valid_themes (icon_theme);
1717 sizes = g_hash_table_new (g_direct_hash, g_direct_equal);
1719 for (l = priv->themes; l; l = l->next)
1721 IconTheme *theme = l->data;
1722 for (d = theme->dirs; d; d = d->next)
1724 IconThemeDir *dir = d->data;
1726 if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
1729 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1730 if (suffix != ICON_SUFFIX_NONE)
1732 if (suffix == ICON_SUFFIX_SVG)
1733 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1735 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1740 for (d = builtin_dirs; d; d = d->next)
1742 IconThemeDir *dir = d->data;
1744 if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
1747 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1748 if (suffix != ICON_SUFFIX_NONE)
1750 if (suffix == ICON_SUFFIX_SVG)
1751 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1753 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1757 if (icon_theme_builtin_icons)
1759 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
1763 BuiltinIcon *icon = icons->data;
1765 g_hash_table_insert (sizes, GINT_TO_POINTER (icon->size), NULL);
1766 icons = icons->next;
1770 r = result = g_new0 (gint, g_hash_table_size (sizes) + 1);
1772 g_hash_table_foreach (sizes, add_size, &r);
1773 g_hash_table_destroy (sizes);
1779 add_key_to_hash (gpointer key,
1783 GHashTable *hash = user_data;
1785 g_hash_table_insert (hash, key, NULL);
1789 add_key_to_list (gpointer key,
1793 GList **list = user_data;
1795 *list = g_list_prepend (*list, g_strdup (key));
1799 * gtk_icon_theme_list_icons:
1800 * @icon_theme: a #GtkIconTheme
1801 * @context: (allow-none): a string identifying a particular type of
1802 * icon, or %NULL to list all icons.
1804 * Lists the icons in the current icon theme. Only a subset
1805 * of the icons can be listed by providing a context string.
1806 * The set of values for the context string is system dependent,
1807 * but will typically include such values as "Applications" and
1810 * Return value: (element-type utf8) (transfer full): a #GList list
1811 * holding the names of all the icons in the theme. You must first
1812 * free each element in the list with g_free(), then free the list
1813 * itself with g_list_free().
1818 gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
1819 const char *context)
1821 GtkIconThemePrivate *priv;
1824 GQuark context_quark;
1826 priv = icon_theme->priv;
1828 ensure_valid_themes (icon_theme);
1832 context_quark = g_quark_try_string (context);
1840 icons = g_hash_table_new (g_str_hash, g_str_equal);
1845 theme_list_icons (l->data, icons, context_quark);
1849 if (context_quark == 0)
1850 g_hash_table_foreach (priv->unthemed_icons,
1856 g_hash_table_foreach (icons,
1860 g_hash_table_destroy (icons);
1866 * gtk_icon_theme_list_contexts:
1867 * @icon_theme: a #GtkIconTheme
1869 * Gets the list of contexts available within the current
1870 * hierarchy of icon themes
1872 * Return value: (element-type utf8) (transfer full): a #GList list
1873 * holding the names of all the contexts in the theme. You must first
1874 * free each element in the list with g_free(), then free the list
1875 * itself with g_list_free().
1880 gtk_icon_theme_list_contexts (GtkIconTheme *icon_theme)
1882 GtkIconThemePrivate *priv;
1883 GHashTable *contexts;
1886 priv = icon_theme->priv;
1888 ensure_valid_themes (icon_theme);
1890 contexts = g_hash_table_new (g_str_hash, g_str_equal);
1895 theme_list_contexts (l->data, contexts);
1901 g_hash_table_foreach (contexts,
1905 g_hash_table_destroy (contexts);
1911 * gtk_icon_theme_get_example_icon_name:
1912 * @icon_theme: a #GtkIconTheme
1914 * Gets the name of an icon that is representative of the
1915 * current theme (for instance, to use when presenting
1916 * a list of themes to the user.)
1918 * Return value: the name of an example icon or %NULL.
1919 * Free with g_free().
1924 gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
1926 GtkIconThemePrivate *priv;
1930 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1932 priv = icon_theme->priv;
1934 ensure_valid_themes (icon_theme);
1941 return g_strdup (theme->example);
1951 rescan_themes (GtkIconTheme *icon_theme)
1953 GtkIconThemePrivate *priv;
1954 IconThemeDirMtime *dir_mtime;
1960 priv = icon_theme->priv;
1962 for (d = priv->dir_mtimes; d != NULL; d = d->next)
1964 dir_mtime = d->data;
1966 stat_res = g_stat (dir_mtime->dir, &stat_buf);
1968 /* dir mtime didn't change */
1969 if (stat_res == 0 &&
1970 S_ISDIR (stat_buf.st_mode) &&
1971 dir_mtime->mtime == stat_buf.st_mtime)
1973 /* didn't exist before, and still doesn't */
1974 if (dir_mtime->mtime == 0 &&
1975 (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
1981 g_get_current_time (&tv);
1982 priv->last_stat_time = tv.tv_sec;
1988 * gtk_icon_theme_rescan_if_needed:
1989 * @icon_theme: a #GtkIconTheme
1991 * Checks to see if the icon theme has changed; if it has, any
1992 * currently cached information is discarded and will be reloaded
1993 * next time @icon_theme is accessed.
1995 * Return value: %TRUE if the icon theme has changed and needed
2001 gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
2005 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
2007 retval = rescan_themes (icon_theme);
2009 do_theme_change (icon_theme);
2015 theme_destroy (IconTheme *theme)
2017 g_free (theme->display_name);
2018 g_free (theme->comment);
2019 g_free (theme->name);
2020 g_free (theme->example);
2022 g_list_free_full (theme->dirs, (GDestroyNotify) theme_dir_destroy);
2028 theme_dir_destroy (IconThemeDir *dir)
2031 _gtk_icon_cache_unref (dir->cache);
2033 g_hash_table_destroy (dir->icons);
2036 g_hash_table_destroy (dir->icon_data);
2038 g_free (dir->subdir);
2043 theme_dir_size_difference (IconThemeDir *dir, int size, gboolean *smaller)
2048 case ICON_THEME_DIR_FIXED:
2049 *smaller = size < dir->size;
2050 return abs (size - dir->size);
2052 case ICON_THEME_DIR_SCALABLE:
2053 *smaller = size < dir->min_size;
2054 if (size < dir->min_size)
2055 return dir->min_size - size;
2056 if (size > dir->max_size)
2057 return size - dir->max_size;
2060 case ICON_THEME_DIR_THRESHOLD:
2061 min = dir->size - dir->threshold;
2062 max = dir->size + dir->threshold;
2063 *smaller = size < min;
2070 case ICON_THEME_DIR_UNTHEMED:
2071 g_assert_not_reached ();
2074 g_assert_not_reached ();
2079 string_from_suffix (IconSuffix suffix)
2083 case ICON_SUFFIX_XPM:
2085 case ICON_SUFFIX_SVG:
2087 case ICON_SUFFIX_PNG:
2090 g_assert_not_reached();
2096 suffix_from_name (const char *name)
2100 if (g_str_has_suffix (name, ".png"))
2101 retval = ICON_SUFFIX_PNG;
2102 else if (g_str_has_suffix (name, ".svg"))
2103 retval = ICON_SUFFIX_SVG;
2104 else if (g_str_has_suffix (name, ".xpm"))
2105 retval = ICON_SUFFIX_XPM;
2107 retval = ICON_SUFFIX_NONE;
2113 best_suffix (IconSuffix suffix,
2116 if ((suffix & ICON_SUFFIX_PNG) != 0)
2117 return ICON_SUFFIX_PNG;
2118 else if (allow_svg && ((suffix & ICON_SUFFIX_SVG) != 0))
2119 return ICON_SUFFIX_SVG;
2120 else if ((suffix & ICON_SUFFIX_XPM) != 0)
2121 return ICON_SUFFIX_XPM;
2123 return ICON_SUFFIX_NONE;
2128 theme_dir_get_icon_suffix (IconThemeDir *dir,
2129 const gchar *icon_name,
2130 gboolean *has_icon_file)
2136 suffix = (IconSuffix)_gtk_icon_cache_get_icon_flags (dir->cache,
2141 *has_icon_file = suffix & HAS_ICON_FILE;
2143 suffix = suffix & ~HAS_ICON_FILE;
2146 suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
2148 GTK_NOTE (ICONTHEME,
2149 g_print ("get_icon_suffix%s %u\n", dir->cache ? " (cached)" : "", suffix));
2154 static GtkIconInfo *
2155 theme_lookup_icon (IconTheme *theme,
2156 const char *icon_name,
2159 gboolean use_builtin)
2162 IconThemeDir *dir, *min_dir;
2164 int min_difference, difference;
2165 BuiltinIcon *closest_builtin = NULL;
2166 gboolean smaller, has_larger, match;
2169 min_difference = G_MAXINT;
2174 /* Builtin icons are logically part of the default theme and
2175 * are searched before other subdirectories of the default theme.
2177 if (use_builtin && strcmp (theme->name, DEFAULT_THEME_NAME) == 0)
2179 closest_builtin = find_builtin_icon (icon_name,
2184 if (min_difference == 0)
2185 return icon_info_new_builtin (closest_builtin);
2187 dirs = builtin_dirs;
2197 GTK_NOTE (ICONTHEME,
2198 g_print ("theme_lookup_icon dir %s\n", dir->dir));
2199 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
2200 if (best_suffix (suffix, allow_svg) != ICON_SUFFIX_NONE)
2202 difference = theme_dir_size_difference (dir, size, &smaller);
2204 if (difference == 0)
2206 if (dir->type == ICON_THEME_DIR_SCALABLE)
2208 /* don't pick scalable if we already found
2209 * a matching non-scalable dir
2219 /* for a matching non-scalable dir keep
2220 * going and look for a closer match
2222 difference = abs (size - dir->size);
2223 if (!match || difference < min_difference)
2226 min_difference = difference;
2229 if (difference == 0)
2238 if (difference < min_difference || smaller)
2240 min_difference = difference;
2242 has_larger = smaller;
2247 if (difference < min_difference && smaller)
2249 min_difference = difference;
2258 if (l == NULL && dirs == builtin_dirs)
2267 GtkIconInfo *icon_info = icon_info_new ();
2268 gboolean has_icon_file = FALSE;
2270 suffix = theme_dir_get_icon_suffix (min_dir, icon_name, &has_icon_file);
2271 suffix = best_suffix (suffix, allow_svg);
2272 g_assert (suffix != ICON_SUFFIX_NONE);
2276 file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
2277 icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
2282 icon_info->filename = NULL;
2285 if (min_dir->icon_data != NULL)
2286 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2288 if (icon_info->data == NULL && min_dir->cache != NULL)
2290 icon_info->data = _gtk_icon_cache_get_icon_data (min_dir->cache, icon_name, min_dir->subdir_index);
2291 if (icon_info->data)
2293 if (min_dir->icon_data == NULL)
2294 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2295 g_free, (GDestroyNotify)icon_data_free);
2297 g_hash_table_replace (min_dir->icon_data, g_strdup (icon_name), icon_info->data);
2301 if (icon_info->data == NULL && has_icon_file)
2303 gchar *icon_file_name, *icon_file_path;
2305 icon_file_name = g_strconcat (icon_name, ".icon", NULL);
2306 icon_file_path = g_build_filename (min_dir->dir, icon_file_name, NULL);
2308 if (g_file_test (icon_file_path, G_FILE_TEST_IS_REGULAR))
2310 if (min_dir->icon_data == NULL)
2311 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2312 g_free, (GDestroyNotify)icon_data_free);
2313 load_icon_data (min_dir, icon_file_path, icon_file_name);
2315 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2317 g_free (icon_file_name);
2318 g_free (icon_file_path);
2323 icon_info->cache_pixbuf = _gtk_icon_cache_get_icon (min_dir->cache, icon_name,
2324 min_dir->subdir_index);
2327 icon_info->dir_type = min_dir->type;
2328 icon_info->dir_size = min_dir->size;
2329 icon_info->threshold = min_dir->threshold;
2334 if (closest_builtin)
2335 return icon_info_new_builtin (closest_builtin);
2341 theme_list_icons (IconTheme *theme,
2345 GList *l = theme->dirs;
2352 if (context == dir->context ||
2357 _gtk_icon_cache_add_icons (dir->cache,
2364 g_hash_table_foreach (dir->icons,
2374 theme_list_contexts (IconTheme *theme,
2375 GHashTable *contexts)
2377 GList *l = theme->dirs;
2379 const char *context;
2385 context = g_quark_to_string (dir->context);
2386 g_hash_table_replace (contexts, (gpointer) context, NULL);
2393 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
2395 GKeyFile *icon_file;
2403 GError *error = NULL;
2407 icon_file = g_key_file_new ();
2408 g_key_file_set_list_separator (icon_file, ',');
2409 g_key_file_load_from_file (icon_file, path, 0, &error);
2412 g_error_free (error);
2413 g_key_file_free (icon_file);
2418 base_name = strip_suffix (name);
2420 data = g_slice_new0 (GtkIconData);
2421 /* takes ownership of base_name */
2422 g_hash_table_replace (dir->icon_data, base_name, data);
2424 ivalues = g_key_file_get_integer_list (icon_file,
2425 "Icon Data", "EmbeddedTextRectangle",
2431 data->has_embedded_rect = TRUE;
2432 data->x0 = ivalues[0];
2433 data->y0 = ivalues[1];
2434 data->x1 = ivalues[2];
2435 data->y1 = ivalues[3];
2441 str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
2444 split = g_strsplit (str, "|", -1);
2446 data->n_attach_points = g_strv_length (split);
2447 data->attach_points = g_new (GdkPoint, data->n_attach_points);
2450 while (split[i] != NULL && i < data->n_attach_points)
2452 split_point = strchr (split[i], ',');
2457 data->attach_points[i].x = atoi (split[i]);
2458 data->attach_points[i].y = atoi (split_point);
2467 data->display_name = g_key_file_get_locale_string (icon_file,
2468 "Icon Data", "DisplayName",
2470 g_key_file_free (icon_file);
2475 scan_directory (GtkIconThemePrivate *icon_theme,
2476 IconThemeDir *dir, char *full_dir)
2481 GTK_NOTE (ICONTHEME,
2482 g_print ("scanning directory %s\n", full_dir));
2483 dir->icons = g_hash_table_new_full (g_str_hash, g_str_equal,
2486 gdir = g_dir_open (full_dir, 0, NULL);
2491 while ((name = g_dir_read_name (gdir)))
2495 IconSuffix suffix, hash_suffix;
2497 if (g_str_has_suffix (name, ".icon"))
2499 if (dir->icon_data == NULL)
2500 dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2501 g_free, (GDestroyNotify)icon_data_free);
2503 path = g_build_filename (full_dir, name, NULL);
2504 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
2505 load_icon_data (dir, path, name);
2512 suffix = suffix_from_name (name);
2513 if (suffix == ICON_SUFFIX_NONE)
2516 base_name = strip_suffix (name);
2518 hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
2519 g_hash_table_replace (icon_theme->all_icons, base_name, NULL);
2520 /* takes ownership of base_name */
2521 g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
2528 theme_subdir_load (GtkIconTheme *icon_theme,
2530 GKeyFile *theme_file,
2536 IconThemeDirType type;
2537 char *context_string;
2544 GError *error = NULL;
2545 IconThemeDirMtime *dir_mtime;
2547 size = g_key_file_get_integer (theme_file, subdir, "Size", &error);
2550 g_error_free (error);
2551 g_warning ("Theme directory %s of theme %s has no size field\n",
2552 subdir, theme->name);
2556 type = ICON_THEME_DIR_THRESHOLD;
2557 type_string = g_key_file_get_string (theme_file, subdir, "Type", NULL);
2560 if (strcmp (type_string, "Fixed") == 0)
2561 type = ICON_THEME_DIR_FIXED;
2562 else if (strcmp (type_string, "Scalable") == 0)
2563 type = ICON_THEME_DIR_SCALABLE;
2564 else if (strcmp (type_string, "Threshold") == 0)
2565 type = ICON_THEME_DIR_THRESHOLD;
2567 g_free (type_string);
2571 context_string = g_key_file_get_string (theme_file, subdir, "Context", NULL);
2574 context = g_quark_from_string (context_string);
2575 g_free (context_string);
2578 if (g_key_file_has_key (theme_file, subdir, "MaxSize", NULL))
2579 max_size = g_key_file_get_integer (theme_file, subdir, "MaxSize", NULL);
2583 if (g_key_file_has_key (theme_file, subdir, "MinSize", NULL))
2584 min_size = g_key_file_get_integer (theme_file, subdir, "MinSize", NULL);
2588 if (g_key_file_has_key (theme_file, subdir, "Threshold", NULL))
2589 threshold = g_key_file_get_integer (theme_file, subdir, "Threshold", NULL);
2593 for (d = icon_theme->priv->dir_mtimes; d; d = d->next)
2595 dir_mtime = (IconThemeDirMtime *)d->data;
2597 if (dir_mtime->mtime == 0)
2598 continue; /* directory doesn't exist */
2600 full_dir = g_build_filename (dir_mtime->dir, subdir, NULL);
2602 /* First, see if we have a cache for the directory */
2603 if (dir_mtime->cache != NULL || g_file_test (full_dir, G_FILE_TEST_IS_DIR))
2605 if (dir_mtime->cache == NULL)
2607 /* This will return NULL if the cache doesn't exist or is outdated */
2608 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir_mtime->dir);
2611 dir = g_new (IconThemeDir, 1);
2613 dir->context = context;
2615 dir->min_size = min_size;
2616 dir->max_size = max_size;
2617 dir->threshold = threshold;
2618 dir->dir = full_dir;
2619 dir->icon_data = NULL;
2620 dir->subdir = g_strdup (subdir);
2621 if (dir_mtime->cache != NULL)
2623 dir->cache = _gtk_icon_cache_ref (dir_mtime->cache);
2624 dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
2629 dir->subdir_index = -1;
2630 scan_directory (icon_theme->priv, dir, full_dir);
2633 theme->dirs = g_list_prepend (theme->dirs, dir);
2641 icon_data_free (GtkIconData *icon_data)
2643 g_free (icon_data->attach_points);
2644 g_free (icon_data->display_name);
2645 g_slice_free (GtkIconData, icon_data);
2652 G_DEFINE_BOXED_TYPE (GtkIconInfo, gtk_icon_info,
2656 static GtkIconInfo *
2657 icon_info_new (void)
2659 GtkIconInfo *icon_info = g_slice_new0 (GtkIconInfo);
2661 icon_info->scale = -1.;
2662 icon_info->ref_count = 1;
2667 static GtkIconInfo *
2668 icon_info_new_builtin (BuiltinIcon *icon)
2670 GtkIconInfo *icon_info = icon_info_new ();
2672 icon_info->cache_pixbuf = g_object_ref (icon->pixbuf);
2673 icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
2674 icon_info->dir_size = icon->size;
2675 icon_info->threshold = 2;
2681 * gtk_icon_info_copy:
2682 * @icon_info: a #GtkIconInfo
2684 * Make a copy of a #GtkIconInfo.
2686 * Return value: the new GtkIconInfo
2691 gtk_icon_info_copy (GtkIconInfo *icon_info)
2694 g_return_val_if_fail (icon_info != NULL, NULL);
2696 icon_info->ref_count++;
2702 * gtk_icon_info_free:
2703 * @icon_info: a #GtkIconInfo
2705 * Free a #GtkIconInfo and associated information
2710 gtk_icon_info_free (GtkIconInfo *icon_info)
2712 g_return_if_fail (icon_info != NULL);
2714 icon_info->ref_count--;
2715 if (icon_info->ref_count > 0)
2718 g_free (icon_info->filename);
2719 if (icon_info->loadable)
2720 g_object_unref (icon_info->loadable);
2721 g_slist_free_full (icon_info->emblem_infos, (GDestroyNotify) gtk_icon_info_free);
2722 if (icon_info->pixbuf)
2723 g_object_unref (icon_info->pixbuf);
2724 if (icon_info->cache_pixbuf)
2725 g_object_unref (icon_info->cache_pixbuf);
2727 g_slice_free (GtkIconInfo, icon_info);
2731 * gtk_icon_info_get_base_size:
2732 * @icon_info: a #GtkIconInfo
2734 * Gets the base size for the icon. The base size
2735 * is a size for the icon that was specified by
2736 * the icon theme creator. This may be different
2737 * than the actual size of image; an example of
2738 * this is small emblem icons that can be attached
2739 * to a larger icon. These icons will be given
2740 * the same base size as the larger icons to which
2741 * they are attached.
2743 * Return value: the base size, or 0, if no base
2744 * size is known for the icon.
2749 gtk_icon_info_get_base_size (GtkIconInfo *icon_info)
2751 g_return_val_if_fail (icon_info != NULL, 0);
2753 return icon_info->dir_size;
2757 * gtk_icon_info_get_filename:
2758 * @icon_info: a #GtkIconInfo
2760 * Gets the filename for the icon. If the
2761 * %GTK_ICON_LOOKUP_USE_BUILTIN flag was passed
2762 * to gtk_icon_theme_lookup_icon(), there may be
2763 * no filename if a builtin icon is returned; in this
2764 * case, you should use gtk_icon_info_get_builtin_pixbuf().
2766 * Return value: (type filename): the filename for the icon, or %NULL
2767 * if gtk_icon_info_get_builtin_pixbuf() should be used instead. The
2768 * return value is owned by GTK+ and should not be modified or freed.
2773 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2775 g_return_val_if_fail (icon_info != NULL, NULL);
2777 return icon_info->filename;
2781 * gtk_icon_info_get_builtin_pixbuf:
2782 * @icon_info: a #GtkIconInfo structure
2784 * Gets the built-in image for this icon, if any. To allow
2785 * GTK+ to use built in icon images, you must pass the
2786 * %GTK_ICON_LOOKUP_USE_BUILTIN to
2787 * gtk_icon_theme_lookup_icon().
2789 * Return value: (transfer none): the built-in image pixbuf, or %NULL. No
2790 * extra reference is added to the returned pixbuf, so if
2791 * you want to keep it around, you must use g_object_ref().
2792 * The returned image must not be modified.
2797 gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
2799 g_return_val_if_fail (icon_info != NULL, NULL);
2801 if (icon_info->filename)
2804 return icon_info->cache_pixbuf;
2807 static gboolean icon_info_ensure_scale_and_pixbuf (GtkIconInfo*, gboolean);
2809 /* Combine the icon with all emblems, the first emblem is placed
2810 * in the southeast corner. Scale emblems to be at most 3/4 of the
2811 * size of the icon itself.
2814 apply_emblems (GtkIconInfo *info)
2816 GdkPixbuf *icon = NULL;
2820 if (info->emblem_infos == NULL)
2823 if (info->emblems_applied)
2826 w = gdk_pixbuf_get_width (info->pixbuf);
2827 h = gdk_pixbuf_get_height (info->pixbuf);
2829 for (l = info->emblem_infos, pos = 0; l; l = l->next, pos++)
2831 GtkIconInfo *emblem_info = l->data;
2833 if (icon_info_ensure_scale_and_pixbuf (emblem_info, FALSE))
2835 GdkPixbuf *emblem = emblem_info->pixbuf;
2837 gint x = 0, y = 0; /* silence compiler */
2840 ew = gdk_pixbuf_get_width (emblem);
2841 eh = gdk_pixbuf_get_height (emblem);
2873 icon = gdk_pixbuf_copy (info->pixbuf);
2878 gdk_pixbuf_composite (emblem, icon, x, y, ew, eh, x, y,
2879 scale, scale, GDK_INTERP_BILINEAR, 255);
2885 g_object_unref (info->pixbuf);
2886 info->pixbuf = icon;
2889 info->emblems_applied = TRUE;
2892 /* This function contains the complicated logic for deciding
2893 * on the size at which to load the icon and loading it at
2897 icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
2898 gboolean scale_only)
2900 int image_width, image_height;
2901 GdkPixbuf *source_pixbuf;
2904 /* First check if we already succeeded have the necessary
2905 * information (or failed earlier)
2907 if (scale_only && icon_info->scale >= 0)
2910 if (icon_info->pixbuf)
2912 apply_emblems (icon_info);
2916 if (icon_info->load_error)
2919 /* SVG icons are a special case - we just immediately scale them
2920 * to the desired size
2922 if (icon_info->filename && !icon_info->loadable)
2926 file = g_file_new_for_path (icon_info->filename);
2927 icon_info->loadable = G_LOADABLE_ICON (g_file_icon_new (file));
2928 g_object_unref (file);
2932 if (G_IS_FILE_ICON (icon_info->loadable))
2935 GFileInfo *file_info;
2936 const gchar *content_type;
2938 file = g_file_icon_get_file (G_FILE_ICON (icon_info->loadable));
2939 file_info = g_file_query_info (file,
2940 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
2941 G_FILE_QUERY_INFO_NONE,
2945 content_type = g_file_info_get_content_type (file_info);
2947 if (content_type && strcmp (content_type, "image/svg+xml") == 0)
2950 g_object_unref (file_info);
2956 GInputStream *stream;
2958 icon_info->scale = icon_info->desired_size / 1000.;
2963 stream = g_loadable_icon_load (icon_info->loadable,
2964 icon_info->desired_size,
2966 &icon_info->load_error);
2969 icon_info->pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
2970 icon_info->desired_size,
2971 icon_info->desired_size,
2974 &icon_info->load_error);
2975 g_object_unref (stream);
2978 if (!icon_info->pixbuf)
2981 apply_emblems (icon_info);
2986 /* In many cases, the scale can be determined without actual access
2987 * to the icon file. This is generally true when we have a size
2988 * for the directory where the icon is; the image size doesn't
2989 * matter in that case.
2991 if (icon_info->forced_size)
2992 icon_info->scale = -1;
2993 else if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
2994 icon_info->scale = 1.0;
2995 else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
2997 if (icon_info->desired_size >= icon_info->dir_size - icon_info->threshold &&
2998 icon_info->desired_size <= icon_info->dir_size + icon_info->threshold)
2999 icon_info->scale = 1.0;
3000 else if (icon_info->dir_size > 0)
3001 icon_info->scale =(gdouble) icon_info->desired_size / icon_info->dir_size;
3003 else if (icon_info->dir_type == ICON_THEME_DIR_SCALABLE)
3005 if (icon_info->dir_size > 0)
3006 icon_info->scale = (gdouble) icon_info->desired_size / icon_info->dir_size;
3009 if (icon_info->scale >= 0. && scale_only)
3012 /* At this point, we need to actually get the icon; either from the
3013 * builtin image or by loading the file
3015 source_pixbuf = NULL;
3016 if (icon_info->cache_pixbuf)
3017 source_pixbuf = g_object_ref (icon_info->cache_pixbuf);
3020 GInputStream *stream;
3022 stream = g_loadable_icon_load (icon_info->loadable,
3023 icon_info->desired_size,
3025 &icon_info->load_error);
3028 source_pixbuf = gdk_pixbuf_new_from_stream (stream,
3030 &icon_info->load_error);
3031 g_object_unref (stream);
3038 /* Do scale calculations that depend on the image size
3040 image_width = gdk_pixbuf_get_width (source_pixbuf);
3041 image_height = gdk_pixbuf_get_height (source_pixbuf);
3043 if (icon_info->scale < 0.0)
3045 gint image_size = MAX (image_width, image_height);
3047 icon_info->scale = (gdouble)icon_info->desired_size / (gdouble)image_size;
3049 icon_info->scale = 1.0;
3051 if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED &&
3052 !icon_info->forced_size)
3053 icon_info->scale = MIN (icon_info->scale, 1.0);
3056 /* We don't short-circuit out here for scale_only, since, now
3057 * we've loaded the icon, we might as well go ahead and finish
3058 * the job. This is a bit of a waste when we scale here
3059 * and never get the final pixbuf; at the cost of a bit of
3060 * extra complexity, we could keep the source pixbuf around
3061 * but not actually scale it until needed.
3063 if (icon_info->scale == 1.0)
3064 icon_info->pixbuf = source_pixbuf;
3067 icon_info->pixbuf = gdk_pixbuf_scale_simple (source_pixbuf,
3068 0.5 + image_width * icon_info->scale,
3069 0.5 + image_height * icon_info->scale,
3070 GDK_INTERP_BILINEAR);
3071 g_object_unref (source_pixbuf);
3074 apply_emblems (icon_info);
3080 * gtk_icon_info_load_icon:
3081 * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
3082 * @error: (allow-none): location to store error information on failure,
3085 * Renders an icon previously looked up in an icon theme using
3086 * gtk_icon_theme_lookup_icon(); the size will be based on the size
3087 * passed to gtk_icon_theme_lookup_icon(). Note that the resulting
3088 * pixbuf may not be exactly this size; an icon theme may have icons
3089 * that differ slightly from their nominal sizes, and in addition GTK+
3090 * will avoid scaling icons that it considers sufficiently close to the
3091 * requested size or for which the source image would have to be scaled
3092 * up too far. (This maintains sharpness.). This behaviour can be changed
3093 * by passing the %GTK_ICON_LOOKUP_FORCE_SIZE flag when obtaining
3094 * the #GtkIconInfo. If this flag has been specified, the pixbuf
3095 * returned by this function will be scaled to the exact size.
3097 * Return value: (transfer full): the rendered icon; this may be a newly
3098 * created icon or a new reference to an internal icon, so you must
3099 * not modify the icon. Use g_object_unref() to release your reference
3105 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
3108 g_return_val_if_fail (icon_info != NULL, NULL);
3109 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
3111 if (!icon_info_ensure_scale_and_pixbuf (icon_info, FALSE))
3113 if (icon_info->load_error)
3114 g_propagate_error (error, icon_info->load_error);
3116 g_set_error_literal (error,
3117 GTK_ICON_THEME_ERROR,
3118 GTK_ICON_THEME_NOT_FOUND,
3119 _("Failed to load icon"));
3124 return g_object_ref (icon_info->pixbuf);
3128 gdk_color_to_css (GdkColor *color)
3130 return g_strdup_printf ("rgb(%d,%d,%d)",
3137 gdk_rgba_to_css (const GdkRGBA *color)
3139 /* drop alpha for now, since librsvg does not understand rgba() */
3140 return g_strdup_printf ("rgb(%d,%d,%d)",
3141 (gint)(color->red * 255),
3142 (gint)(color->green * 255),
3143 (gint)(color->blue * 255));
3147 _gtk_icon_info_load_symbolic_internal (GtkIconInfo *icon_info,
3148 const gchar *css_fg,
3149 const gchar *css_success,
3150 const gchar *css_warning,
3151 const gchar *css_error,
3154 GInputStream *stream;
3157 gchar *success, *warning, *err;
3159 /* css_fg can't possibly have failed, otherwise
3160 * that would mean we have a broken style */
3161 g_return_val_if_fail (css_fg != NULL, NULL);
3163 success = warning = err = NULL;
3167 GdkColor success_default_color = { 0, 0x4e00, 0x9a00, 0x0600 };
3168 success = gdk_color_to_css (&success_default_color);
3172 GdkColor warning_default_color = { 0, 0xf500, 0x7900, 0x3e00 };
3173 warning = gdk_color_to_css (&warning_default_color);
3177 GdkColor error_default_color = { 0, 0xcc00, 0x0000, 0x0000 };
3178 err = gdk_color_to_css (&error_default_color);
3182 data = g_strconcat ("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
3183 "<svg version=\"1.1\"\n"
3184 " xmlns=\"http://www.w3.org/2000/svg\"\n"
3185 " xmlns:xi=\"http://www.w3.org/2001/XInclude\"\n"
3188 " <style type=\"text/css\">\n"
3190 " fill: ", css_fg," !important;\n"
3193 " fill: ", css_warning ? css_warning : warning," !important;\n"
3196 " fill: ", css_error ? css_error : err," !important;\n"
3199 " fill: ", css_success ? css_success : success," !important;\n"
3202 " <xi:include href=\"", icon_info->filename, "\"/>\n"
3209 stream = g_memory_input_stream_new_from_data (data, -1, g_free);
3210 pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
3211 icon_info->desired_size,
3212 icon_info->desired_size,
3216 g_object_unref (stream);
3222 * gtk_icon_info_load_symbolic:
3223 * @icon_info: a #GtkIconInfo
3224 * @fg: a #GdkRGBA representing the foreground color of the icon
3225 * @success_color: (allow-none): a #GdkRGBA representing the warning color
3226 * of the icon or %NULL to use the default color
3227 * @warning_color: (allow-none): a #GdkRGBA representing the warning color
3228 * of the icon or %NULL to use the default color
3229 * @error_color: (allow-none): a #GdkRGBA representing the error color
3230 * of the icon or %NULL to use the default color (allow-none)
3231 * @was_symbolic: (out) (allow-none): a #gboolean, returns whether the
3232 * loaded icon was a symbolic one and whether the @fg color was
3234 * @error: (allow-none): location to store error information on failure,
3237 * Loads an icon, modifying it to match the system colours for the foreground,
3238 * success, warning and error colors provided. If the icon is not a symbolic
3239 * one, the function will return the result from gtk_icon_info_load_icon().
3241 * This allows loading symbolic icons that will match the system theme.
3243 * Unless you are implementing a widget, you will want to use
3244 * g_themed_icon_new_with_default_fallbacks() to load the icon.
3246 * As implementation details, the icon loaded needs to be of SVG type,
3247 * contain the "symbolic" term as the last component of the icon name,
3248 * and use the 'fg', 'success', 'warning' and 'error' CSS styles in the
3251 * See the <ulink url="http://www.freedesktop.org/wiki/SymbolicIcons">Symbolic Icons spec</ulink>
3252 * for more information about symbolic icons.
3254 * Return value: (transfer full): a #GdkPixbuf representing the loaded icon
3259 gtk_icon_info_load_symbolic (GtkIconInfo *icon_info,
3261 const GdkRGBA *success_color,
3262 const GdkRGBA *warning_color,
3263 const GdkRGBA *error_color,
3264 gboolean *was_symbolic,
3273 g_return_val_if_fail (fg != NULL, NULL);
3275 if (!icon_info->filename ||
3276 !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3279 *was_symbolic = FALSE;
3280 return gtk_icon_info_load_icon (icon_info, error);
3284 *was_symbolic = TRUE;
3286 css_fg = gdk_rgba_to_css (fg);
3288 css_success = css_warning = css_error = NULL;
3291 css_warning = gdk_rgba_to_css (warning_color);
3294 css_error = gdk_rgba_to_css (error_color);
3297 css_success = gdk_rgba_to_css (success_color);
3299 pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
3300 css_fg, css_success,
3301 css_warning, css_error,
3304 g_free (css_warning);
3305 g_free (css_success);
3312 * gtk_icon_info_load_symbolic_for_context:
3313 * @icon_info: a #GtkIconInfo
3314 * @context: a #GtkStyleContext
3315 * @was_symbolic: (out) (allow-none): a #gboolean, returns whether the
3316 * loaded icon was a symbolic one and whether the @fg color was
3318 * @error: (allow-none): location to store error information on failure,
3321 * Loads an icon, modifying it to match the system colors for the foreground,
3322 * success, warning and error colors provided. If the icon is not a symbolic
3323 * one, the function will return the result from gtk_icon_info_load_icon().
3324 * This function uses the regular foreground color and the symbolic colors
3325 * with the names "success_color", "warning_color" and "error_color" from
3328 * This allows loading symbolic icons that will match the system theme.
3330 * See gtk_icon_info_load_symbolic() for more details.
3332 * Return value: (transfer full): a #GdkPixbuf representing the loaded icon
3337 gtk_icon_info_load_symbolic_for_context (GtkIconInfo *icon_info,
3338 GtkStyleContext *context,
3339 gboolean *was_symbolic,
3343 GdkRGBA *color = NULL;
3345 gchar *css_fg = NULL, *css_success;
3346 gchar *css_warning, *css_error;
3347 GtkStateFlags state;
3349 if (!icon_info->filename ||
3350 !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3353 *was_symbolic = FALSE;
3354 return gtk_icon_info_load_icon (icon_info, error);
3358 *was_symbolic = TRUE;
3360 state = gtk_style_context_get_state (context);
3361 gtk_style_context_get (context, state, "color", &color, NULL);
3364 css_fg = gdk_rgba_to_css (color);
3365 gdk_rgba_free (color);
3368 css_success = css_warning = css_error = NULL;
3370 if (gtk_style_context_lookup_color (context, "success_color", &rgba))
3371 css_success = gdk_rgba_to_css (&rgba);
3373 if (gtk_style_context_lookup_color (context, "warning_color", &rgba))
3374 css_warning = gdk_rgba_to_css (&rgba);
3376 if (gtk_style_context_lookup_color (context, "error_color", &rgba))
3377 css_error = gdk_rgba_to_css (&rgba);
3379 pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
3380 css_fg, css_success,
3381 css_warning, css_error,
3385 g_free (css_success);
3386 g_free (css_warning);
3393 * gtk_icon_info_load_symbolic_for_style:
3394 * @icon_info: a #GtkIconInfo
3395 * @style: a #GtkStyle to take the colors from
3396 * @state: the widget state to use for colors
3397 * @was_symbolic: (out) (allow-none): a #gboolean, returns whether the
3398 * loaded icon was a symbolic one and whether the @fg color was
3400 * @error: (allow-none): location to store error information on failure,
3403 * Loads an icon, modifying it to match the system colours for the foreground,
3404 * success, warning and error colors provided. If the icon is not a symbolic
3405 * one, the function will return the result from gtk_icon_info_load_icon().
3407 * This allows loading symbolic icons that will match the system theme.
3409 * See gtk_icon_info_load_symbolic() for more details.
3411 * Return value: (transfer full): a #GdkPixbuf representing the loaded icon
3415 * Deprecated: 3.0: Use gtk_icon_info_load_symbolic_for_context() instead
3418 gtk_icon_info_load_symbolic_for_style (GtkIconInfo *icon_info,
3421 gboolean *was_symbolic,
3425 GdkColor success_color;
3426 GdkColor warning_color;
3427 GdkColor error_color;
3429 gchar *css_fg, *css_success;
3430 gchar *css_warning, *css_error;
3432 if (!icon_info->filename ||
3433 !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3436 *was_symbolic = FALSE;
3437 return gtk_icon_info_load_icon (icon_info, error);
3441 *was_symbolic = TRUE;
3443 fg = &style->fg[state];
3444 css_fg = gdk_color_to_css (fg);
3446 css_success = css_warning = css_error = NULL;
3448 if (gtk_style_lookup_color (style, "success_color", &success_color))
3449 css_success = gdk_color_to_css (&success_color);
3451 if (gtk_style_lookup_color (style, "warning_color", &warning_color))
3452 css_warning = gdk_color_to_css (&warning_color);
3454 if (gtk_style_lookup_color (style, "error_color", &error_color))
3455 css_error = gdk_color_to_css (&error_color);
3457 pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
3458 css_fg, css_success,
3459 css_warning, css_error,
3463 g_free (css_success);
3464 g_free (css_warning);
3471 * gtk_icon_info_set_raw_coordinates:
3472 * @icon_info: a #GtkIconInfo
3473 * @raw_coordinates: whether the coordinates of embedded rectangles
3474 * and attached points should be returned in their original
3477 * Sets whether the coordinates returned by gtk_icon_info_get_embedded_rect()
3478 * and gtk_icon_info_get_attach_points() should be returned in their
3479 * original form as specified in the icon theme, instead of scaled
3480 * appropriately for the pixbuf returned by gtk_icon_info_load_icon().
3482 * Raw coordinates are somewhat strange; they are specified to be with
3483 * respect to the unscaled pixmap for PNG and XPM icons, but for SVG
3484 * icons, they are in a 1000x1000 coordinate space that is scaled
3485 * to the final size of the icon. You can determine if the icon is an SVG
3486 * icon by using gtk_icon_info_get_filename(), and seeing if it is non-%NULL
3487 * and ends in '.svg'.
3489 * This function is provided primarily to allow compatibility wrappers
3490 * for older API's, and is not expected to be useful for applications.
3495 gtk_icon_info_set_raw_coordinates (GtkIconInfo *icon_info,
3496 gboolean raw_coordinates)
3498 g_return_if_fail (icon_info != NULL);
3500 icon_info->raw_coordinates = raw_coordinates != FALSE;
3503 /* Scale coordinates from the icon data prior to returning
3507 icon_info_scale_point (GtkIconInfo *icon_info,
3513 if (icon_info->raw_coordinates)
3520 if (!icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3523 *x_out = 0.5 + x * icon_info->scale;
3524 *y_out = 0.5 + y * icon_info->scale;
3531 * gtk_icon_info_get_embedded_rect:
3532 * @icon_info: a #GtkIconInfo
3533 * @rectangle: (out): #GdkRectangle in which to store embedded
3534 * rectangle coordinates; coordinates are only stored
3535 * when this function returns %TRUE.
3537 * Gets the coordinates of a rectangle within the icon
3538 * that can be used for display of information such
3539 * as a preview of the contents of a text file.
3540 * See gtk_icon_info_set_raw_coordinates() for further
3541 * information about the coordinate system.
3543 * Return value: %TRUE if the icon has an embedded rectangle
3548 gtk_icon_info_get_embedded_rect (GtkIconInfo *icon_info,
3549 GdkRectangle *rectangle)
3551 g_return_val_if_fail (icon_info != NULL, FALSE);
3553 if (icon_info->data && icon_info->data->has_embedded_rect &&
3554 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3556 gint scaled_x0, scaled_y0;
3557 gint scaled_x1, scaled_y1;
3561 icon_info_scale_point (icon_info,
3562 icon_info->data->x0, icon_info->data->y0,
3563 &scaled_x0, &scaled_y0);
3564 icon_info_scale_point (icon_info,
3565 icon_info->data->x1, icon_info->data->y1,
3566 &scaled_x1, &scaled_y1);
3568 rectangle->x = scaled_x0;
3569 rectangle->y = scaled_y0;
3570 rectangle->width = scaled_x1 - rectangle->x;
3571 rectangle->height = scaled_y1 - rectangle->y;
3581 * gtk_icon_info_get_attach_points:
3582 * @icon_info: a #GtkIconInfo
3583 * @points: (allow-none) (array length=n_points) (out): location to store pointer to an array of points, or %NULL
3584 * free the array of points with g_free().
3585 * @n_points: (allow-none): location to store the number of points in @points, or %NULL
3587 * Fetches the set of attach points for an icon. An attach point
3588 * is a location in the icon that can be used as anchor points for attaching
3589 * emblems or overlays to the icon.
3591 * Return value: %TRUE if there are any attach points for the icon.
3596 gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
3600 g_return_val_if_fail (icon_info != NULL, FALSE);
3602 if (icon_info->data && icon_info->data->n_attach_points &&
3603 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3609 *points = g_new (GdkPoint, icon_info->data->n_attach_points);
3610 for (i = 0; i < icon_info->data->n_attach_points; i++)
3611 icon_info_scale_point (icon_info,
3612 icon_info->data->attach_points[i].x,
3613 icon_info->data->attach_points[i].y,
3619 *n_points = icon_info->data->n_attach_points;
3635 * gtk_icon_info_get_display_name:
3636 * @icon_info: a #GtkIconInfo
3638 * Gets the display name for an icon. A display name is a
3639 * string to be used in place of the icon name in a user
3640 * visible context like a list of icons.
3642 * Return value: the display name for the icon or %NULL, if
3643 * the icon doesn't have a specified display name. This value
3644 * is owned @icon_info and must not be modified or free.
3649 gtk_icon_info_get_display_name (GtkIconInfo *icon_info)
3651 g_return_val_if_fail (icon_info != NULL, NULL);
3653 if (icon_info->data)
3654 return icon_info->data->display_name;
3665 * gtk_icon_theme_add_builtin_icon:
3666 * @icon_name: the name of the icon to register
3667 * @size: the size at which to register the icon (different
3668 * images can be registered for the same icon name
3669 * at different sizes.)
3670 * @pixbuf: #GdkPixbuf that contains the image to use
3673 * Registers a built-in icon for icon theme lookups. The idea
3674 * of built-in icons is to allow an application or library
3675 * that uses themed icons to function requiring files to
3676 * be present in the file system. For instance, the default
3677 * images for all of GTK+'s stock icons are registered
3680 * In general, if you use gtk_icon_theme_add_builtin_icon()
3681 * you should also install the icon in the icon theme, so
3682 * that the icon is generally available.
3684 * This function will generally be used with pixbufs loaded
3685 * via gdk_pixbuf_new_from_inline().
3690 gtk_icon_theme_add_builtin_icon (const gchar *icon_name,
3694 BuiltinIcon *default_icon;
3698 g_return_if_fail (icon_name != NULL);
3699 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3701 if (!icon_theme_builtin_icons)
3702 icon_theme_builtin_icons = g_hash_table_new (g_str_hash, g_str_equal);
3704 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
3706 key = g_strdup (icon_name);
3708 key = (gpointer)icon_name; /* Won't get stored */
3710 default_icon = g_new (BuiltinIcon, 1);
3711 default_icon->size = size;
3712 default_icon->pixbuf = g_object_ref (pixbuf);
3713 icons = g_slist_prepend (icons, default_icon);
3715 /* Replaces value, leaves key untouched
3717 g_hash_table_insert (icon_theme_builtin_icons, key, icons);
3720 /* Look up a builtin icon; the min_difference_p and
3721 * has_larger_p out parameters allow us to combine
3722 * this lookup with searching through the actual directories
3723 * of the "hicolor" icon theme. See theme_lookup_icon()
3724 * for how they are used.
3726 static BuiltinIcon *
3727 find_builtin_icon (const gchar *icon_name,
3729 gint *min_difference_p,
3730 gboolean *has_larger_p)
3732 GSList *icons = NULL;
3733 gint min_difference = G_MAXINT;
3734 gboolean has_larger = FALSE;
3735 BuiltinIcon *min_icon = NULL;
3737 if (!icon_theme_builtin_icons)
3740 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
3744 BuiltinIcon *default_icon = icons->data;
3745 int min, max, difference;
3748 min = default_icon->size - 2;
3749 max = default_icon->size + 2;
3750 smaller = size < min;
3752 difference = min - size;
3753 else if (size > max)
3754 difference = size - max;
3758 if (difference == 0)
3760 min_icon = default_icon;
3766 if (difference < min_difference || smaller)
3768 min_difference = difference;
3769 min_icon = default_icon;
3770 has_larger = smaller;
3775 if (difference < min_difference && smaller)
3777 min_difference = difference;
3778 min_icon = default_icon;
3782 icons = icons->next;
3785 if (min_difference_p)
3786 *min_difference_p = min_difference;
3788 *has_larger_p = has_larger;
3794 _gtk_icon_theme_check_reload (GdkDisplay *display)
3798 GtkIconTheme *icon_theme;
3800 n_screens = gdk_display_get_n_screens (display);
3802 for (i = 0; i < n_screens; i++)
3804 screen = gdk_display_get_screen (display, i);
3806 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
3809 icon_theme->priv->check_reload = TRUE;
3810 ensure_valid_themes (icon_theme);
3811 icon_theme->priv->check_reload = FALSE;
3818 * gtk_icon_theme_lookup_by_gicon:
3819 * @icon_theme: a #GtkIconTheme
3820 * @icon: the #GIcon to look up
3821 * @size: desired icon size
3822 * @flags: flags modifying the behavior of the icon lookup
3824 * Looks up an icon and returns a structure containing
3825 * information such as the filename of the icon.
3826 * The icon can then be rendered into a pixbuf using
3827 * gtk_icon_info_load_icon().
3829 * Return value: a #GtkIconInfo structure containing
3830 * information about the icon, or %NULL if the icon
3831 * wasn't found. Free with gtk_icon_info_free()
3836 gtk_icon_theme_lookup_by_gicon (GtkIconTheme *icon_theme,
3839 GtkIconLookupFlags flags)
3843 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
3844 g_return_val_if_fail (G_IS_ICON (icon), NULL);
3846 if (G_IS_LOADABLE_ICON (icon))
3848 info = icon_info_new ();
3849 info->loadable = G_LOADABLE_ICON (g_object_ref (icon));
3851 info->dir_type = ICON_THEME_DIR_UNTHEMED;
3852 info->dir_size = size;
3853 info->desired_size = size;
3854 info->threshold = 2;
3855 info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
3859 else if (G_IS_THEMED_ICON (icon))
3861 const gchar **names;
3863 names = (const gchar **)g_themed_icon_get_names (G_THEMED_ICON (icon));
3864 info = gtk_icon_theme_choose_icon (icon_theme, names, size, flags);
3868 else if (G_IS_EMBLEMED_ICON (icon))
3870 GIcon *base, *emblem;
3872 GtkIconInfo *emblem_info;
3874 if (GTK_IS_NUMERABLE_ICON (icon))
3875 _gtk_numerable_icon_set_background_icon_size (GTK_NUMERABLE_ICON (icon), size / 2);
3877 base = g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon));
3878 info = gtk_icon_theme_lookup_by_gicon (icon_theme, base, size, flags);
3881 list = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon));
3882 for (l = list; l; l = l->next)
3884 emblem = g_emblem_get_icon (G_EMBLEM (l->data));
3885 /* always force size for emblems */
3886 emblem_info = gtk_icon_theme_lookup_by_gicon (icon_theme, emblem, size / 2, flags | GTK_ICON_LOOKUP_FORCE_SIZE);
3888 info->emblem_infos = g_slist_prepend (info->emblem_infos, emblem_info);
3894 else if (GDK_IS_PIXBUF (icon))
3898 pixbuf = GDK_PIXBUF (icon);
3900 if ((flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0)
3902 gint width, height, max;
3906 width = gdk_pixbuf_get_width (pixbuf);
3907 height = gdk_pixbuf_get_height (pixbuf);
3908 max = MAX (width, height);
3909 scale = (gdouble) size / (gdouble) max;
3911 scaled = gdk_pixbuf_scale_simple (pixbuf,
3912 0.5 + width * scale,
3913 0.5 + height * scale,
3914 GDK_INTERP_BILINEAR);
3916 info = gtk_icon_info_new_for_pixbuf (icon_theme, scaled);
3918 g_object_unref (scaled);
3922 info = gtk_icon_info_new_for_pixbuf (icon_theme, pixbuf);
3932 * gtk_icon_info_new_for_pixbuf:
3933 * @icon_theme: a #GtkIconTheme
3934 * @pixbuf: the pixbuf to wrap in a #GtkIconInfo
3936 * Creates a #GtkIconInfo for a #GdkPixbuf.
3938 * Returns: a #GtkIconInfo
3943 gtk_icon_info_new_for_pixbuf (GtkIconTheme *icon_theme,
3948 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
3949 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
3951 info = icon_info_new ();
3952 info->pixbuf = g_object_ref (pixbuf);
3954 info->dir_type = ICON_THEME_DIR_UNTHEMED;