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, see <http://www.gnu.org/licenses/>.
20 #include <sys/types.h>
28 #include <glib/gstdio.h>
32 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
34 #define WIN32_MEAN_AND_LEAN
36 #include "win32/gdkwin32.h"
37 #endif /* G_OS_WIN32 */
39 #include "gtkicontheme.h"
41 #include "gtkiconfactory.h"
42 #include "gtkiconcache.h"
43 #include "gtkbuiltincache.h"
46 #include "gtknumerableiconprivate.h"
47 #include "gtksettings.h"
48 #include "gtkprivate.h"
51 #undef GDK_DEPRECATED_FOR
52 #define GDK_DEPRECATED
53 #define GDK_DEPRECATED_FOR(f)
54 #undef GTK_DISABLE_DEPRECATED
56 #include "deprecated/gtkstyle.h"
60 * SECTION:gtkicontheme
61 * @Short_description: Looking up icons by name
62 * @Title: GtkIconTheme
64 * #GtkIconTheme provides a facility for looking up icons by name
65 * and size. The main reason for using a name rather than simply
66 * providing a filename is to allow different icons to be used
67 * depending on what <firstterm>icon theme</firstterm> is selected
68 * by the user. The operation of icon themes on Linux and Unix
70 * url="http://www.freedesktop.org/Standards/icon-theme-spec">Icon
71 * Theme Specification</ulink>. There is a default icon theme,
72 * named <literal>hicolor</literal> where applications should install
73 * their icons, but more additional application themes can be
74 * installed as operating system vendors and users choose.
76 * Named icons are similar to the <xref linkend="gtk3-Themeable-Stock-Images"/>
77 * facility, and the distinction between the two may be a bit confusing.
78 * A few things to keep in mind:
81 * Stock images usually are used in conjunction with
82 * <xref linkend="gtk3-Stock-Items"/>, such as %GTK_STOCK_OK or
83 * %GTK_STOCK_OPEN. Named icons are easier to set up and therefore
84 * are more useful for new icons that an application wants to
85 * add, such as application icons or window icons.
88 * Stock images can only be loaded at the symbolic sizes defined
89 * by the #GtkIconSize enumeration, or by custom sizes defined
90 * by gtk_icon_size_register(), while named icons are more flexible
91 * and any pixel size can be specified.
94 * Because stock images are closely tied to stock items, and thus
95 * to actions in the user interface, stock images may come in
96 * multiple variants for different widget states or writing
100 * A good rule of thumb is that if there is a stock image for what
101 * you want to use, use it, otherwise use a named icon. It turns
102 * out that internally stock images are generally defined in
103 * terms of one or more named icons. (An example of the
104 * more than one case is icons that depend on writing direction;
105 * %GTK_STOCK_GO_FORWARD uses the two themed icons
106 * "gtk-stock-go-forward-ltr" and "gtk-stock-go-forward-rtl".)
108 * In many cases, named themes are used indirectly, via #GtkImage
109 * or stock items, rather than directly, but looking up icons
110 * directly is also simple. The #GtkIconTheme object acts
111 * as a database of all the icons in the current theme. You
112 * can create new #GtkIconTheme objects, but its much more
113 * efficient to use the standard icon theme for the #GdkScreen
114 * so that the icon information is shared with other people
115 * looking up icons. In the case where the default screen is
116 * being used, looking up an icon can be as simple as:
119 * GError *error = NULL;
120 * GtkIconTheme *icon_theme;
123 * icon_theme = gtk_icon_theme_get_default ();
124 * pixbuf = gtk_icon_theme_load_icon (icon_theme,
125 * "my-icon-name", // icon name
131 * g_warning ("Couldn't load icon: %s", error->message);
132 * g_error_free (error);
137 * g_object_unref (pixbuf);
144 #define DEFAULT_THEME_NAME "hicolor"
148 ICON_THEME_DIR_FIXED,
149 ICON_THEME_DIR_SCALABLE,
150 ICON_THEME_DIR_THRESHOLD,
151 ICON_THEME_DIR_UNTHEMED
154 /* In reverse search order: */
157 ICON_SUFFIX_NONE = 0,
158 ICON_SUFFIX_XPM = 1 << 0,
159 ICON_SUFFIX_SVG = 1 << 1,
160 ICON_SUFFIX_PNG = 1 << 2,
161 HAS_ICON_FILE = 1 << 3
165 struct _GtkIconThemePrivate
167 gchar *current_theme;
168 gchar *fallback_theme;
170 gint search_path_len;
172 guint custom_theme : 1;
173 guint is_screen_singleton : 1;
174 guint pixbuf_supports_svg : 1;
175 guint themes_valid : 1;
176 guint check_reload : 1;
177 guint loading_themes : 1;
179 /* A list of all the themes needed to look up icons.
180 * In search order, without duplicates
183 GHashTable *unthemed_icons;
185 /* Note: The keys of this hashtable are owned by the
186 * themedir and unthemed hashtables.
188 GHashTable *all_icons;
190 /* GdkScreen for the icon theme (may be NULL)
194 /* time when we last stat:ed for theme changes */
195 glong last_stat_time;
198 gulong reset_styles_idle;
203 /* Information about the source
206 GLoadableIcon *loadable;
207 GSList *emblem_infos;
209 /* Cache pixbuf (if there is any) */
210 GdkPixbuf *cache_pixbuf;
214 /* Information about the directory where
215 * the source was found
217 IconThemeDirType dir_type;
221 /* Parameters influencing the scaled icon
224 guint raw_coordinates : 1;
225 guint forced_size : 1;
226 guint emblems_applied : 1;
230 /* Cached information if we go ahead and try to load
245 /* In search order */
251 IconThemeDirType type;
266 GHashTable *icon_data;
272 char *no_svg_filename;
284 time_t mtime; /* 0 == not existing or not a dir */
289 static void gtk_icon_theme_finalize (GObject *object);
290 static void theme_dir_destroy (IconThemeDir *dir);
292 static void theme_destroy (IconTheme *theme);
293 static GtkIconInfo *theme_lookup_icon (IconTheme *theme,
294 const char *icon_name,
297 gboolean use_default_icons);
298 static void theme_list_icons (IconTheme *theme,
301 static void theme_list_contexts (IconTheme *theme,
302 GHashTable *contexts);
303 static void theme_subdir_load (GtkIconTheme *icon_theme,
305 GKeyFile *theme_file,
307 static void do_theme_change (GtkIconTheme *icon_theme);
309 static void blow_themes (GtkIconTheme *icon_themes);
310 static gboolean rescan_themes (GtkIconTheme *icon_themes);
312 static void icon_data_free (GtkIconData *icon_data);
313 static void load_icon_data (IconThemeDir *dir,
317 static IconSuffix theme_dir_get_icon_suffix (IconThemeDir *dir,
318 const gchar *icon_name,
319 gboolean *has_icon_file);
322 static GtkIconInfo *icon_info_new (void);
323 static GtkIconInfo *icon_info_new_builtin (BuiltinIcon *icon);
325 static IconSuffix suffix_from_name (const char *name);
327 static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
329 gint *min_difference_p,
330 gboolean *has_larger_p);
332 static guint signal_changed = 0;
334 static GHashTable *icon_theme_builtin_icons;
336 /* also used in gtkiconfactory.c */
337 GtkIconCache *_builtin_cache = NULL;
338 static GList *builtin_dirs = NULL;
340 G_DEFINE_TYPE (GtkIconTheme, gtk_icon_theme, G_TYPE_OBJECT)
343 * gtk_icon_theme_new:
345 * Creates a new icon theme object. Icon theme objects are used
346 * to lookup up an icon by name in a particular icon theme.
347 * Usually, you'll want to use gtk_icon_theme_get_default()
348 * or gtk_icon_theme_get_for_screen() rather than creating
349 * a new icon theme object for scratch.
351 * Return value: the newly created #GtkIconTheme object.
356 gtk_icon_theme_new (void)
358 return g_object_new (GTK_TYPE_ICON_THEME, NULL);
362 * gtk_icon_theme_get_default:
364 * Gets the icon theme for the default screen. See
365 * gtk_icon_theme_get_for_screen().
367 * Return value: (transfer none): A unique #GtkIconTheme associated with
368 * the default screen. This icon theme is associated with
369 * the screen and can be used as long as the screen
370 * is open. Do not ref or unref it.
375 gtk_icon_theme_get_default (void)
377 return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
381 * gtk_icon_theme_get_for_screen:
382 * @screen: a #GdkScreen
384 * Gets the icon theme object associated with @screen; if this
385 * function has not previously been called for the given
386 * screen, a new icon theme object will be created and
387 * associated with the screen. Icon theme objects are
388 * fairly expensive to create, so using this function
389 * is usually a better choice than calling than gtk_icon_theme_new()
390 * and setting the screen yourself; by using this function
391 * a single icon theme object will be shared between users.
393 * Return value: (transfer none): A unique #GtkIconTheme associated with
394 * the given screen. This icon theme is associated with
395 * the screen and can be used as long as the screen
396 * is open. Do not ref or unref it.
401 gtk_icon_theme_get_for_screen (GdkScreen *screen)
403 GtkIconTheme *icon_theme;
405 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
407 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
410 GtkIconThemePrivate *priv;
412 icon_theme = gtk_icon_theme_new ();
413 gtk_icon_theme_set_screen (icon_theme, screen);
415 priv = icon_theme->priv;
416 priv->is_screen_singleton = TRUE;
418 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), icon_theme);
425 gtk_icon_theme_class_init (GtkIconThemeClass *klass)
427 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
429 gobject_class->finalize = gtk_icon_theme_finalize;
432 * GtkIconTheme::changed
433 * @icon_theme: the icon theme
435 * Emitted when the current icon theme is switched or GTK+ detects
436 * that a change has occurred in the contents of the current
439 signal_changed = g_signal_new (I_("changed"),
440 G_TYPE_FROM_CLASS (klass),
442 G_STRUCT_OFFSET (GtkIconThemeClass, changed),
444 g_cclosure_marshal_VOID__VOID,
447 g_type_class_add_private (klass, sizeof (GtkIconThemePrivate));
451 /* Callback when the display that the icon theme is attached to
452 * is closed; unset the screen, and if it's the unique theme
453 * for the screen, drop the reference
456 display_closed (GdkDisplay *display,
458 GtkIconTheme *icon_theme)
460 GtkIconThemePrivate *priv = icon_theme->priv;
461 GdkScreen *screen = priv->screen;
462 gboolean was_screen_singleton = priv->is_screen_singleton;
464 if (was_screen_singleton)
466 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), NULL);
467 priv->is_screen_singleton = FALSE;
470 gtk_icon_theme_set_screen (icon_theme, NULL);
472 if (was_screen_singleton)
474 g_object_unref (icon_theme);
479 update_current_theme (GtkIconTheme *icon_theme)
481 #define theme_changed(_old, _new) \
482 ((_old && !_new) || (!_old && _new) || \
483 (_old && _new && strcmp (_old, _new) != 0))
484 GtkIconThemePrivate *priv = icon_theme->priv;
486 if (!priv->custom_theme)
489 gchar *fallback_theme = NULL;
490 gboolean changed = FALSE;
494 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
495 g_object_get (settings,
496 "gtk-icon-theme-name", &theme,
497 "gtk-fallback-icon-theme", &fallback_theme, NULL);
500 /* ensure that the current theme (even when just the default)
501 * is searched before any fallback theme
503 if (!theme && fallback_theme)
504 theme = g_strdup (DEFAULT_THEME_NAME);
506 if (theme_changed (priv->current_theme, theme))
508 g_free (priv->current_theme);
509 priv->current_theme = theme;
515 if (theme_changed (priv->fallback_theme, fallback_theme))
517 g_free (priv->fallback_theme);
518 priv->fallback_theme = fallback_theme;
522 g_free (fallback_theme);
525 do_theme_change (icon_theme);
530 /* Callback when the icon theme GtkSetting changes
533 theme_changed (GtkSettings *settings,
535 GtkIconTheme *icon_theme)
537 update_current_theme (icon_theme);
541 unset_screen (GtkIconTheme *icon_theme)
543 GtkIconThemePrivate *priv = icon_theme->priv;
544 GtkSettings *settings;
549 settings = gtk_settings_get_for_screen (priv->screen);
550 display = gdk_screen_get_display (priv->screen);
552 g_signal_handlers_disconnect_by_func (display,
553 (gpointer) display_closed,
555 g_signal_handlers_disconnect_by_func (settings,
556 (gpointer) theme_changed,
564 * gtk_icon_theme_set_screen:
565 * @icon_theme: a #GtkIconTheme
566 * @screen: a #GdkScreen
568 * Sets the screen for an icon theme; the screen is used
569 * to track the user's currently configured icon theme,
570 * which might be different for different screens.
575 gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
578 GtkIconThemePrivate *priv;
579 GtkSettings *settings;
582 g_return_if_fail (GTK_ICON_THEME (icon_theme));
583 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
585 priv = icon_theme->priv;
587 unset_screen (icon_theme);
591 display = gdk_screen_get_display (screen);
592 settings = gtk_settings_get_for_screen (screen);
594 priv->screen = screen;
596 g_signal_connect (display, "closed",
597 G_CALLBACK (display_closed), icon_theme);
598 g_signal_connect (settings, "notify::gtk-icon-theme-name",
599 G_CALLBACK (theme_changed), icon_theme);
600 g_signal_connect (settings, "notify::gtk-fallback-icon-theme-name",
601 G_CALLBACK (theme_changed), icon_theme);
604 update_current_theme (icon_theme);
607 /* Checks whether a loader for SVG files has been registered
611 pixbuf_supports_svg (void)
615 static gint found_svg = -1;
620 formats = gdk_pixbuf_get_formats ();
623 for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
625 gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
628 for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
630 if (strcmp (*mime_type, "image/svg") == 0)
634 g_strfreev (mime_types);
637 g_slist_free (formats);
643 gtk_icon_theme_init (GtkIconTheme *icon_theme)
645 GtkIconThemePrivate *priv;
646 const gchar * const *xdg_data_dirs;
649 priv = G_TYPE_INSTANCE_GET_PRIVATE (icon_theme,
651 GtkIconThemePrivate);
652 icon_theme->priv = priv;
654 priv->custom_theme = FALSE;
656 xdg_data_dirs = g_get_system_data_dirs ();
657 for (i = 0; xdg_data_dirs[i]; i++) ;
659 priv->search_path_len = 2 * i + 2;
661 priv->search_path = g_new (char *, priv->search_path_len);
664 priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
665 priv->search_path[i++] = g_build_filename (g_get_user_data_dir (), "icons", NULL);
667 for (j = 0; xdg_data_dirs[j]; j++)
668 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
670 for (j = 0; xdg_data_dirs[j]; j++)
671 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "pixmaps", NULL);
673 priv->themes_valid = FALSE;
675 priv->unthemed_icons = NULL;
677 priv->pixbuf_supports_svg = pixbuf_supports_svg ();
681 free_dir_mtime (IconThemeDirMtime *dir_mtime)
683 if (dir_mtime->cache)
684 _gtk_icon_cache_unref (dir_mtime->cache);
686 g_free (dir_mtime->dir);
687 g_slice_free (IconThemeDirMtime, dir_mtime);
692 reset_styles_idle (gpointer user_data)
694 GtkIconTheme *icon_theme;
695 GtkIconThemePrivate *priv;
697 icon_theme = GTK_ICON_THEME (user_data);
698 priv = icon_theme->priv;
700 if (priv->screen && priv->is_screen_singleton)
701 gtk_style_context_reset_widgets (priv->screen);
703 priv->reset_styles_idle = 0;
709 do_theme_change (GtkIconTheme *icon_theme)
711 GtkIconThemePrivate *priv = icon_theme->priv;
713 if (!priv->themes_valid)
717 g_print ("change to icon theme \"%s\"\n", priv->current_theme));
718 blow_themes (icon_theme);
719 g_signal_emit (icon_theme, signal_changed, 0);
721 if (!priv->reset_styles_idle)
722 priv->reset_styles_idle =
723 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2,
724 reset_styles_idle, icon_theme, NULL);
728 blow_themes (GtkIconTheme *icon_theme)
730 GtkIconThemePrivate *priv = icon_theme->priv;
732 if (priv->themes_valid)
734 g_hash_table_destroy (priv->all_icons);
735 g_list_free_full (priv->themes, (GDestroyNotify) theme_destroy);
736 g_list_free_full (priv->dir_mtimes, (GDestroyNotify) free_dir_mtime);
737 g_hash_table_destroy (priv->unthemed_icons);
740 priv->unthemed_icons = NULL;
741 priv->dir_mtimes = NULL;
742 priv->all_icons = NULL;
743 priv->themes_valid = FALSE;
747 gtk_icon_theme_finalize (GObject *object)
749 GtkIconTheme *icon_theme;
750 GtkIconThemePrivate *priv;
753 icon_theme = GTK_ICON_THEME (object);
754 priv = icon_theme->priv;
756 if (priv->reset_styles_idle)
758 g_source_remove (priv->reset_styles_idle);
759 priv->reset_styles_idle = 0;
762 unset_screen (icon_theme);
764 g_free (priv->current_theme);
765 priv->current_theme = NULL;
767 for (i = 0; i < priv->search_path_len; i++)
768 g_free (priv->search_path[i]);
770 g_free (priv->search_path);
771 priv->search_path = NULL;
773 blow_themes (icon_theme);
775 G_OBJECT_CLASS (gtk_icon_theme_parent_class)->finalize (object);
779 * gtk_icon_theme_set_search_path:
780 * @icon_theme: a #GtkIconTheme
781 * @path: (array length=n_elements) (element-type filename): array of
782 * directories that are searched for icon themes
783 * @n_elements: number of elements in @path.
785 * Sets the search path for the icon theme object. When looking
786 * for an icon theme, GTK+ will search for a subdirectory of
787 * one or more of the directories in @path with the same name
788 * as the icon theme. (Themes from multiple of the path elements
789 * are combined to allow themes to be extended by adding icons
790 * in the user's home directory.)
792 * In addition if an icon found isn't found either in the current
793 * icon theme or the default icon theme, and an image file with
794 * the right name is found directly in one of the elements of
795 * @path, then that image will be used for the icon name.
796 * (This is legacy feature, and new icons should be put
797 * into the default icon theme, which is called DEFAULT_THEME_NAME,
798 * rather than directly on the icon path.)
803 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
807 GtkIconThemePrivate *priv;
810 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
812 priv = icon_theme->priv;
813 for (i = 0; i < priv->search_path_len; i++)
814 g_free (priv->search_path[i]);
816 g_free (priv->search_path);
818 priv->search_path = g_new (gchar *, n_elements);
819 priv->search_path_len = n_elements;
821 for (i = 0; i < priv->search_path_len; i++)
822 priv->search_path[i] = g_strdup (path[i]);
824 do_theme_change (icon_theme);
829 * gtk_icon_theme_get_search_path:
830 * @icon_theme: a #GtkIconTheme
831 * @path: (allow-none) (array length=n_elements) (element-type filename) (out):
832 * location to store a list of icon theme path directories or %NULL.
833 * The stored value should be freed with g_strfreev().
834 * @n_elements: location to store number of elements in @path, or %NULL
836 * Gets the current search path. See gtk_icon_theme_set_search_path().
841 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
845 GtkIconThemePrivate *priv;
848 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
850 priv = icon_theme->priv;
853 *n_elements = priv->search_path_len;
857 *path = g_new (gchar *, priv->search_path_len + 1);
858 for (i = 0; i < priv->search_path_len; i++)
859 (*path)[i] = g_strdup (priv->search_path[i]);
865 * gtk_icon_theme_append_search_path:
866 * @icon_theme: a #GtkIconTheme
867 * @path: (type filename): directory name to append to the icon path
869 * Appends a directory to the search path.
870 * See gtk_icon_theme_set_search_path().
875 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
878 GtkIconThemePrivate *priv;
880 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
881 g_return_if_fail (path != NULL);
883 priv = icon_theme->priv;
885 priv->search_path_len++;
887 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
888 priv->search_path[priv->search_path_len-1] = g_strdup (path);
890 do_theme_change (icon_theme);
894 * gtk_icon_theme_prepend_search_path:
895 * @icon_theme: a #GtkIconTheme
896 * @path: (type filename): directory name to prepend to the icon path
898 * Prepends a directory to the search path.
899 * See gtk_icon_theme_set_search_path().
904 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
907 GtkIconThemePrivate *priv;
910 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
911 g_return_if_fail (path != NULL);
913 priv = icon_theme->priv;
915 priv->search_path_len++;
916 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
918 for (i = priv->search_path_len - 1; i > 0; i--)
919 priv->search_path[i] = priv->search_path[i - 1];
921 priv->search_path[0] = g_strdup (path);
923 do_theme_change (icon_theme);
927 * gtk_icon_theme_set_custom_theme:
928 * @icon_theme: a #GtkIconTheme
929 * @theme_name: (allow-none): name of icon theme to use instead of
930 * configured theme, or %NULL to unset a previously set custom theme
932 * Sets the name of the icon theme that the #GtkIconTheme object uses
933 * overriding system configuration. This function cannot be called
934 * on the icon theme objects returned from gtk_icon_theme_get_default()
935 * and gtk_icon_theme_get_for_screen().
940 gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
941 const gchar *theme_name)
943 GtkIconThemePrivate *priv;
945 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
947 priv = icon_theme->priv;
949 g_return_if_fail (!priv->is_screen_singleton);
951 if (theme_name != NULL)
953 priv->custom_theme = TRUE;
954 if (!priv->current_theme || strcmp (theme_name, priv->current_theme) != 0)
956 g_free (priv->current_theme);
957 priv->current_theme = g_strdup (theme_name);
959 do_theme_change (icon_theme);
964 if (priv->custom_theme)
966 priv->custom_theme = FALSE;
968 update_current_theme (icon_theme);
974 insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
980 GtkIconThemePrivate *priv;
981 IconTheme *theme = NULL;
983 GKeyFile *theme_file;
984 GError *error = NULL;
985 IconThemeDirMtime *dir_mtime;
988 priv = icon_theme->priv;
990 for (l = priv->themes; l != NULL; l = l->next)
993 if (strcmp (theme->name, theme_name) == 0)
997 for (i = 0; i < priv->search_path_len; i++)
999 path = g_build_filename (priv->search_path[i],
1002 dir_mtime = g_slice_new (IconThemeDirMtime);
1003 dir_mtime->cache = NULL;
1004 dir_mtime->dir = path;
1005 if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
1006 dir_mtime->mtime = stat_buf.st_mtime;
1008 dir_mtime->mtime = 0;
1010 priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
1012 priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
1015 for (i = 0; i < priv->search_path_len && !theme_file; i++)
1017 path = g_build_filename (priv->search_path[i],
1021 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
1023 theme_file = g_key_file_new ();
1024 g_key_file_set_list_separator (theme_file, ',');
1025 g_key_file_load_from_file (theme_file, path, 0, &error);
1028 g_key_file_free (theme_file);
1030 g_error_free (error);
1037 if (theme_file || strcmp (theme_name, DEFAULT_THEME_NAME) == 0)
1039 theme = g_new0 (IconTheme, 1);
1040 theme->name = g_strdup (theme_name);
1041 priv->themes = g_list_prepend (priv->themes, theme);
1044 if (theme_file == NULL)
1047 theme->display_name =
1048 g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
1049 if (!theme->display_name)
1050 g_warning ("Theme file for %s has no name\n", theme_name);
1052 dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
1055 g_warning ("Theme file for %s has no directories\n", theme_name);
1056 priv->themes = g_list_remove (priv->themes, theme);
1057 g_free (theme->name);
1058 g_free (theme->display_name);
1060 g_key_file_free (theme_file);
1065 g_key_file_get_locale_string (theme_file,
1066 "Icon Theme", "Comment",
1069 g_key_file_get_string (theme_file,
1070 "Icon Theme", "Example",
1074 for (i = 0; dirs[i] != NULL; i++)
1075 theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
1079 theme->dirs = g_list_reverse (theme->dirs);
1081 themes = g_key_file_get_string_list (theme_file,
1088 for (i = 0; themes[i] != NULL; i++)
1089 insert_theme (icon_theme, themes[i]);
1091 g_strfreev (themes);
1094 g_key_file_free (theme_file);
1098 free_unthemed_icon (UnthemedIcon *unthemed_icon)
1100 g_free (unthemed_icon->svg_filename);
1101 g_free (unthemed_icon->no_svg_filename);
1102 g_slice_free (UnthemedIcon, unthemed_icon);
1106 strip_suffix (const char *filename)
1110 dot = strrchr (filename, '.');
1113 return g_strdup (filename);
1115 return g_strndup (filename, dot - filename);
1119 load_themes (GtkIconTheme *icon_theme)
1121 GtkIconThemePrivate *priv;
1126 UnthemedIcon *unthemed_icon;
1127 IconSuffix old_suffix, new_suffix;
1129 IconThemeDirMtime *dir_mtime;
1132 priv = icon_theme->priv;
1134 priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
1136 if (priv->current_theme)
1137 insert_theme (icon_theme, priv->current_theme);
1139 /* Always look in the "default" icon theme, and in a fallback theme */
1140 if (priv->fallback_theme)
1141 insert_theme (icon_theme, priv->fallback_theme);
1142 insert_theme (icon_theme, DEFAULT_THEME_NAME);
1143 priv->themes = g_list_reverse (priv->themes);
1146 priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
1147 g_free, (GDestroyNotify)free_unthemed_icon);
1149 for (base = 0; base < icon_theme->priv->search_path_len; base++)
1151 dir = icon_theme->priv->search_path[base];
1153 dir_mtime = g_slice_new (IconThemeDirMtime);
1154 priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
1156 dir_mtime->dir = g_strdup (dir);
1157 dir_mtime->mtime = 0;
1158 dir_mtime->cache = NULL;
1160 if (g_stat (dir, &stat_buf) != 0 || !S_ISDIR (stat_buf.st_mode))
1162 dir_mtime->mtime = stat_buf.st_mtime;
1164 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir);
1165 if (dir_mtime->cache != NULL)
1168 gdir = g_dir_open (dir, 0, NULL);
1172 while ((file = g_dir_read_name (gdir)))
1174 new_suffix = suffix_from_name (file);
1176 if (new_suffix != ICON_SUFFIX_NONE)
1181 abs_file = g_build_filename (dir, file, NULL);
1182 base_name = strip_suffix (file);
1184 if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
1187 if (new_suffix == ICON_SUFFIX_SVG)
1189 if (unthemed_icon->svg_filename)
1192 unthemed_icon->svg_filename = abs_file;
1196 if (unthemed_icon->no_svg_filename)
1198 old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
1199 if (new_suffix > old_suffix)
1201 g_free (unthemed_icon->no_svg_filename);
1202 unthemed_icon->no_svg_filename = abs_file;
1208 unthemed_icon->no_svg_filename = abs_file;
1215 unthemed_icon = g_slice_new0 (UnthemedIcon);
1217 if (new_suffix == ICON_SUFFIX_SVG)
1218 unthemed_icon->svg_filename = abs_file;
1220 unthemed_icon->no_svg_filename = abs_file;
1222 /* takes ownership of base_name */
1223 g_hash_table_replace (priv->unthemed_icons,
1226 g_hash_table_insert (priv->all_icons,
1234 priv->themes_valid = TRUE;
1236 g_get_current_time(&tv);
1237 priv->last_stat_time = tv.tv_sec;
1241 _gtk_icon_theme_ensure_builtin_cache (void)
1243 static gboolean initialized = FALSE;
1245 static IconThemeDir dirs[5] =
1247 { ICON_THEME_DIR_THRESHOLD, 0, 16, 16, 16, 2, NULL, "16", -1, NULL, NULL, NULL },
1248 { ICON_THEME_DIR_THRESHOLD, 0, 20, 20, 20, 2, NULL, "20", -1, NULL, NULL, NULL },
1249 { ICON_THEME_DIR_THRESHOLD, 0, 24, 24, 24, 2, NULL, "24", -1, NULL, NULL, NULL },
1250 { ICON_THEME_DIR_THRESHOLD, 0, 32, 32, 32, 2, NULL, "32", -1, NULL, NULL, NULL },
1251 { ICON_THEME_DIR_THRESHOLD, 0, 48, 48, 48, 2, NULL, "48", -1, NULL, NULL, NULL }
1259 _builtin_cache = _gtk_icon_cache_new ((gchar *)builtin_icons);
1261 for (i = 0; i < G_N_ELEMENTS (dirs); i++)
1264 dir->cache = _gtk_icon_cache_ref (_builtin_cache);
1265 dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
1267 builtin_dirs = g_list_append (builtin_dirs, dir);
1273 ensure_valid_themes (GtkIconTheme *icon_theme)
1275 GtkIconThemePrivate *priv = icon_theme->priv;
1277 gboolean was_valid = priv->themes_valid;
1279 if (priv->loading_themes)
1281 priv->loading_themes = TRUE;
1283 _gtk_icon_theme_ensure_builtin_cache ();
1285 if (priv->themes_valid)
1287 g_get_current_time (&tv);
1289 if (ABS (tv.tv_sec - priv->last_stat_time) > 5 &&
1290 rescan_themes (icon_theme))
1291 blow_themes (icon_theme);
1294 if (!priv->themes_valid)
1296 load_themes (icon_theme);
1300 g_signal_emit (icon_theme, signal_changed, 0);
1304 priv->loading_themes = FALSE;
1307 static GtkIconInfo *
1308 choose_icon (GtkIconTheme *icon_theme,
1309 const gchar *icon_names[],
1311 GtkIconLookupFlags flags)
1313 GtkIconThemePrivate *priv;
1315 GtkIconInfo *icon_info = NULL;
1316 UnthemedIcon *unthemed_icon = NULL;
1318 gboolean use_builtin;
1321 priv = icon_theme->priv;
1323 if (flags & GTK_ICON_LOOKUP_NO_SVG)
1325 else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
1328 allow_svg = priv->pixbuf_supports_svg;
1330 use_builtin = flags & GTK_ICON_LOOKUP_USE_BUILTIN;
1332 ensure_valid_themes (icon_theme);
1334 for (l = priv->themes; l; l = l->next)
1336 IconTheme *theme = l->data;
1338 for (i = 0; icon_names[i]; i++)
1340 icon_info = theme_lookup_icon (theme, icon_names[i], size, allow_svg, use_builtin);
1346 for (i = 0; icon_names[i]; i++)
1348 unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_names[i]);
1353 /* Still not found an icon, check if reference to a Win32 resource */
1359 resources = g_strsplit (icon_names[0], ",", 0);
1362 wchar_t *wfile = g_utf8_to_utf16 (resources[0], -1, NULL, NULL, NULL);
1363 ExtractIconExW (wfile, resources[1] ? atoi (resources[1]) : 0, &hIcon, NULL, 1);
1369 icon_info = icon_info_new ();
1370 icon_info->cache_pixbuf = gdk_win32_icon_to_pixbuf_libgtk_only (hIcon);
1371 DestroyIcon (hIcon);
1372 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1373 icon_info->dir_size = size;
1375 g_strfreev (resources);
1381 icon_info = icon_info_new ();
1383 /* A SVG icon, when allowed, beats out a XPM icon, but not
1387 unthemed_icon->svg_filename &&
1388 (!unthemed_icon->no_svg_filename ||
1389 suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
1390 icon_info->filename = g_strdup (unthemed_icon->svg_filename);
1391 else if (unthemed_icon->no_svg_filename)
1392 icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);
1394 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1395 icon_info->dir_size = size;
1401 icon_info->desired_size = size;
1402 icon_info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
1406 static gboolean check_for_default_theme = TRUE;
1407 char *default_theme_path;
1408 gboolean found = FALSE;
1411 if (check_for_default_theme)
1413 check_for_default_theme = FALSE;
1415 for (i = 0; !found && i < priv->search_path_len; i++)
1417 default_theme_path = g_build_filename (priv->search_path[i],
1421 found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
1422 g_free (default_theme_path);
1427 g_warning ("Could not find the icon '%s'. The '%s' theme\n"
1428 "was not found either, perhaps you need to install it.\n"
1429 "You can get a copy from:\n"
1431 icon_names[0], DEFAULT_THEME_NAME, "http://icon-theme.freedesktop.org/releases");
1441 * gtk_icon_theme_lookup_icon:
1442 * @icon_theme: a #GtkIconTheme
1443 * @icon_name: the name of the icon to lookup
1444 * @size: desired icon size
1445 * @flags: flags modifying the behavior of the icon lookup
1447 * Looks up a named icon and returns a structure containing
1448 * information such as the filename of the icon. The icon
1449 * can then be rendered into a pixbuf using
1450 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1451 * combines these two steps if all you need is the pixbuf.)
1453 * Return value: a #GtkIconInfo structure containing information
1454 * about the icon, or %NULL if the icon wasn't found. Free with
1455 * gtk_icon_info_free()
1460 gtk_icon_theme_lookup_icon (GtkIconTheme *icon_theme,
1461 const gchar *icon_name,
1463 GtkIconLookupFlags flags)
1467 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1468 g_return_val_if_fail (icon_name != NULL, NULL);
1469 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1470 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1472 GTK_NOTE (ICONTHEME,
1473 g_print ("gtk_icon_theme_lookup_icon %s\n", icon_name));
1475 if (flags & GTK_ICON_LOOKUP_GENERIC_FALLBACK)
1482 for (p = (gchar *) icon_name; *p; p++)
1486 names = g_new (gchar *, dashes + 2);
1487 names[0] = g_strdup (icon_name);
1488 for (i = 1; i <= dashes; i++)
1490 names[i] = g_strdup (names[i - 1]);
1491 p = strrchr (names[i], '-');
1494 names[dashes + 1] = NULL;
1496 info = choose_icon (icon_theme, (const gchar **) names, size, flags);
1502 const gchar *names[2];
1504 names[0] = icon_name;
1507 info = choose_icon (icon_theme, names, size, flags);
1514 * gtk_icon_theme_choose_icon:
1515 * @icon_theme: a #GtkIconTheme
1516 * @icon_names: (array zero-terminated=1): %NULL-terminated array of
1517 * icon names to lookup
1518 * @size: desired icon size
1519 * @flags: flags modifying the behavior of the icon lookup
1521 * Looks up a named icon and returns a structure containing
1522 * information such as the filename of the icon. The icon
1523 * can then be rendered into a pixbuf using
1524 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1525 * combines these two steps if all you need is the pixbuf.)
1527 * If @icon_names contains more than one name, this function
1528 * tries them all in the given order before falling back to
1529 * inherited icon themes.
1531 * Return value: a #GtkIconInfo structure containing information
1532 * about the icon, or %NULL if the icon wasn't found. Free with
1533 * gtk_icon_info_free()
1538 gtk_icon_theme_choose_icon (GtkIconTheme *icon_theme,
1539 const gchar *icon_names[],
1541 GtkIconLookupFlags flags)
1543 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1544 g_return_val_if_fail (icon_names != NULL, NULL);
1545 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1546 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1548 return choose_icon (icon_theme, icon_names, size, flags);
1553 gtk_icon_theme_error_quark (void)
1555 return g_quark_from_static_string ("gtk-icon-theme-error-quark");
1559 * gtk_icon_theme_load_icon:
1560 * @icon_theme: a #GtkIconTheme
1561 * @icon_name: the name of the icon to lookup
1562 * @size: the desired icon size. The resulting icon may not be
1563 * exactly this size; see gtk_icon_info_load_icon().
1564 * @flags: flags modifying the behavior of the icon lookup
1565 * @error: (allow-none): Location to store error information on failure,
1568 * Looks up an icon in an icon theme, scales it to the given size
1569 * and renders it into a pixbuf. This is a convenience function;
1570 * if more details about the icon are needed, use
1571 * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1573 * Note that you probably want to listen for icon theme changes and
1574 * update the icon. This is usually done by connecting to the
1575 * GtkWidget::style-set signal. If for some reason you do not want to
1576 * update the icon when the icon theme changes, you should consider
1577 * using gdk_pixbuf_copy() to make a private copy of the pixbuf
1578 * returned by this function. Otherwise GTK+ may need to keep the old
1579 * icon theme loaded, which would be a waste of memory.
1581 * Return value: (transfer full): the rendered icon; this may be a
1582 * newly created icon or a new reference to an internal icon, so
1583 * you must not modify the icon. Use g_object_unref() to release
1584 * your reference to the icon. %NULL if the icon isn't found.
1589 gtk_icon_theme_load_icon (GtkIconTheme *icon_theme,
1590 const gchar *icon_name,
1592 GtkIconLookupFlags flags,
1595 GtkIconInfo *icon_info;
1596 GdkPixbuf *pixbuf = NULL;
1598 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1599 g_return_val_if_fail (icon_name != NULL, NULL);
1600 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1601 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1602 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1604 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
1605 flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1608 g_set_error (error, GTK_ICON_THEME_ERROR, GTK_ICON_THEME_NOT_FOUND,
1609 _("Icon '%s' not present in theme"), icon_name);
1613 pixbuf = gtk_icon_info_load_icon (icon_info, error);
1614 gtk_icon_info_free (icon_info);
1620 * gtk_icon_theme_has_icon:
1621 * @icon_theme: a #GtkIconTheme
1622 * @icon_name: the name of an icon
1624 * Checks whether an icon theme includes an icon
1625 * for a particular name.
1627 * Return value: %TRUE if @icon_theme includes an
1628 * icon for @icon_name.
1633 gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
1634 const char *icon_name)
1636 GtkIconThemePrivate *priv;
1639 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1640 g_return_val_if_fail (icon_name != NULL, FALSE);
1642 priv = icon_theme->priv;
1644 ensure_valid_themes (icon_theme);
1646 for (l = priv->dir_mtimes; l; l = l->next)
1648 IconThemeDirMtime *dir_mtime = l->data;
1649 GtkIconCache *cache = dir_mtime->cache;
1651 if (cache && _gtk_icon_cache_has_icon (cache, icon_name))
1655 if (g_hash_table_lookup_extended (priv->all_icons,
1656 icon_name, NULL, NULL))
1659 if (_builtin_cache &&
1660 _gtk_icon_cache_has_icon (_builtin_cache, icon_name))
1663 if (icon_theme_builtin_icons &&
1664 g_hash_table_lookup_extended (icon_theme_builtin_icons,
1665 icon_name, NULL, NULL))
1672 add_size (gpointer key,
1676 gint **res_p = user_data;
1678 **res_p = GPOINTER_TO_INT (key);
1684 * gtk_icon_theme_get_icon_sizes:
1685 * @icon_theme: a #GtkIconTheme
1686 * @icon_name: the name of an icon
1688 * Returns an array of integers describing the sizes at which
1689 * the icon is available without scaling. A size of -1 means
1690 * that the icon is available in a scalable format. The array
1691 * is zero-terminated.
1693 * Return value: (array zero-terminated=1): An newly allocated array
1694 * describing the sizes at which the icon is available. The array
1695 * should be freed with g_free() when it is no longer needed.
1700 gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
1701 const char *icon_name)
1703 GList *l, *d, *icons;
1707 GtkIconThemePrivate *priv;
1709 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1711 priv = icon_theme->priv;
1713 ensure_valid_themes (icon_theme);
1715 sizes = g_hash_table_new (g_direct_hash, g_direct_equal);
1717 for (l = priv->themes; l; l = l->next)
1719 IconTheme *theme = l->data;
1720 for (d = theme->dirs; d; d = d->next)
1722 IconThemeDir *dir = d->data;
1724 if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
1727 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1728 if (suffix != ICON_SUFFIX_NONE)
1730 if (suffix == ICON_SUFFIX_SVG)
1731 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1733 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1738 for (d = builtin_dirs; d; d = d->next)
1740 IconThemeDir *dir = d->data;
1742 if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
1745 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1746 if (suffix != ICON_SUFFIX_NONE)
1748 if (suffix == ICON_SUFFIX_SVG)
1749 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1751 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1755 if (icon_theme_builtin_icons)
1757 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
1761 BuiltinIcon *icon = icons->data;
1763 g_hash_table_insert (sizes, GINT_TO_POINTER (icon->size), NULL);
1764 icons = icons->next;
1768 r = result = g_new0 (gint, g_hash_table_size (sizes) + 1);
1770 g_hash_table_foreach (sizes, add_size, &r);
1771 g_hash_table_destroy (sizes);
1777 add_key_to_hash (gpointer key,
1781 GHashTable *hash = user_data;
1783 g_hash_table_insert (hash, key, NULL);
1787 add_key_to_list (gpointer key,
1791 GList **list = user_data;
1793 *list = g_list_prepend (*list, g_strdup (key));
1797 * gtk_icon_theme_list_icons:
1798 * @icon_theme: a #GtkIconTheme
1799 * @context: (allow-none): a string identifying a particular type of
1800 * icon, or %NULL to list all icons.
1802 * Lists the icons in the current icon theme. Only a subset
1803 * of the icons can be listed by providing a context string.
1804 * The set of values for the context string is system dependent,
1805 * but will typically include such values as "Applications" and
1808 * Return value: (element-type utf8) (transfer full): a #GList list
1809 * holding the names of all the icons in the theme. You must first
1810 * free each element in the list with g_free(), then free the list
1811 * itself with g_list_free().
1816 gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
1817 const char *context)
1819 GtkIconThemePrivate *priv;
1822 GQuark context_quark;
1824 priv = icon_theme->priv;
1826 ensure_valid_themes (icon_theme);
1830 context_quark = g_quark_try_string (context);
1838 icons = g_hash_table_new (g_str_hash, g_str_equal);
1843 theme_list_icons (l->data, icons, context_quark);
1847 if (context_quark == 0)
1848 g_hash_table_foreach (priv->unthemed_icons,
1854 g_hash_table_foreach (icons,
1858 g_hash_table_destroy (icons);
1864 * gtk_icon_theme_list_contexts:
1865 * @icon_theme: a #GtkIconTheme
1867 * Gets the list of contexts available within the current
1868 * hierarchy of icon themes
1870 * Return value: (element-type utf8) (transfer full): a #GList list
1871 * holding the names of all the contexts in the theme. You must first
1872 * free each element in the list with g_free(), then free the list
1873 * itself with g_list_free().
1878 gtk_icon_theme_list_contexts (GtkIconTheme *icon_theme)
1880 GtkIconThemePrivate *priv;
1881 GHashTable *contexts;
1884 priv = icon_theme->priv;
1886 ensure_valid_themes (icon_theme);
1888 contexts = g_hash_table_new (g_str_hash, g_str_equal);
1893 theme_list_contexts (l->data, contexts);
1899 g_hash_table_foreach (contexts,
1903 g_hash_table_destroy (contexts);
1909 * gtk_icon_theme_get_example_icon_name:
1910 * @icon_theme: a #GtkIconTheme
1912 * Gets the name of an icon that is representative of the
1913 * current theme (for instance, to use when presenting
1914 * a list of themes to the user.)
1916 * Return value: the name of an example icon or %NULL.
1917 * Free with g_free().
1922 gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
1924 GtkIconThemePrivate *priv;
1928 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1930 priv = icon_theme->priv;
1932 ensure_valid_themes (icon_theme);
1939 return g_strdup (theme->example);
1949 rescan_themes (GtkIconTheme *icon_theme)
1951 GtkIconThemePrivate *priv;
1952 IconThemeDirMtime *dir_mtime;
1958 priv = icon_theme->priv;
1960 for (d = priv->dir_mtimes; d != NULL; d = d->next)
1962 dir_mtime = d->data;
1964 stat_res = g_stat (dir_mtime->dir, &stat_buf);
1966 /* dir mtime didn't change */
1967 if (stat_res == 0 &&
1968 S_ISDIR (stat_buf.st_mode) &&
1969 dir_mtime->mtime == stat_buf.st_mtime)
1971 /* didn't exist before, and still doesn't */
1972 if (dir_mtime->mtime == 0 &&
1973 (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
1979 g_get_current_time (&tv);
1980 priv->last_stat_time = tv.tv_sec;
1986 * gtk_icon_theme_rescan_if_needed:
1987 * @icon_theme: a #GtkIconTheme
1989 * Checks to see if the icon theme has changed; if it has, any
1990 * currently cached information is discarded and will be reloaded
1991 * next time @icon_theme is accessed.
1993 * Return value: %TRUE if the icon theme has changed and needed
1999 gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
2003 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
2005 retval = rescan_themes (icon_theme);
2007 do_theme_change (icon_theme);
2013 theme_destroy (IconTheme *theme)
2015 g_free (theme->display_name);
2016 g_free (theme->comment);
2017 g_free (theme->name);
2018 g_free (theme->example);
2020 g_list_free_full (theme->dirs, (GDestroyNotify) theme_dir_destroy);
2026 theme_dir_destroy (IconThemeDir *dir)
2029 _gtk_icon_cache_unref (dir->cache);
2031 g_hash_table_destroy (dir->icons);
2034 g_hash_table_destroy (dir->icon_data);
2036 g_free (dir->subdir);
2041 theme_dir_size_difference (IconThemeDir *dir, int size, gboolean *smaller)
2046 case ICON_THEME_DIR_FIXED:
2047 *smaller = size < dir->size;
2048 return abs (size - dir->size);
2050 case ICON_THEME_DIR_SCALABLE:
2051 *smaller = size < dir->min_size;
2052 if (size < dir->min_size)
2053 return dir->min_size - size;
2054 if (size > dir->max_size)
2055 return size - dir->max_size;
2058 case ICON_THEME_DIR_THRESHOLD:
2059 min = dir->size - dir->threshold;
2060 max = dir->size + dir->threshold;
2061 *smaller = size < min;
2068 case ICON_THEME_DIR_UNTHEMED:
2069 g_assert_not_reached ();
2072 g_assert_not_reached ();
2077 string_from_suffix (IconSuffix suffix)
2081 case ICON_SUFFIX_XPM:
2083 case ICON_SUFFIX_SVG:
2085 case ICON_SUFFIX_PNG:
2088 g_assert_not_reached();
2094 suffix_from_name (const char *name)
2098 if (g_str_has_suffix (name, ".png"))
2099 retval = ICON_SUFFIX_PNG;
2100 else if (g_str_has_suffix (name, ".svg"))
2101 retval = ICON_SUFFIX_SVG;
2102 else if (g_str_has_suffix (name, ".xpm"))
2103 retval = ICON_SUFFIX_XPM;
2105 retval = ICON_SUFFIX_NONE;
2111 best_suffix (IconSuffix suffix,
2114 if ((suffix & ICON_SUFFIX_PNG) != 0)
2115 return ICON_SUFFIX_PNG;
2116 else if (allow_svg && ((suffix & ICON_SUFFIX_SVG) != 0))
2117 return ICON_SUFFIX_SVG;
2118 else if ((suffix & ICON_SUFFIX_XPM) != 0)
2119 return ICON_SUFFIX_XPM;
2121 return ICON_SUFFIX_NONE;
2126 theme_dir_get_icon_suffix (IconThemeDir *dir,
2127 const gchar *icon_name,
2128 gboolean *has_icon_file)
2134 suffix = (IconSuffix)_gtk_icon_cache_get_icon_flags (dir->cache,
2139 *has_icon_file = suffix & HAS_ICON_FILE;
2141 suffix = suffix & ~HAS_ICON_FILE;
2144 suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
2146 GTK_NOTE (ICONTHEME,
2147 g_print ("get_icon_suffix%s %u\n", dir->cache ? " (cached)" : "", suffix));
2152 static GtkIconInfo *
2153 theme_lookup_icon (IconTheme *theme,
2154 const char *icon_name,
2157 gboolean use_builtin)
2160 IconThemeDir *dir, *min_dir;
2162 int min_difference, difference;
2163 BuiltinIcon *closest_builtin = NULL;
2164 gboolean smaller, has_larger, match;
2167 min_difference = G_MAXINT;
2172 /* Builtin icons are logically part of the default theme and
2173 * are searched before other subdirectories of the default theme.
2175 if (use_builtin && strcmp (theme->name, DEFAULT_THEME_NAME) == 0)
2177 closest_builtin = find_builtin_icon (icon_name,
2182 if (min_difference == 0)
2183 return icon_info_new_builtin (closest_builtin);
2185 dirs = builtin_dirs;
2195 GTK_NOTE (ICONTHEME,
2196 g_print ("theme_lookup_icon dir %s\n", dir->dir));
2197 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
2198 if (best_suffix (suffix, allow_svg) != ICON_SUFFIX_NONE)
2200 difference = theme_dir_size_difference (dir, size, &smaller);
2202 if (difference == 0)
2204 if (dir->type == ICON_THEME_DIR_SCALABLE)
2206 /* don't pick scalable if we already found
2207 * a matching non-scalable dir
2217 /* for a matching non-scalable dir keep
2218 * going and look for a closer match
2220 difference = abs (size - dir->size);
2221 if (!match || difference < min_difference)
2224 min_difference = difference;
2227 if (difference == 0)
2236 if (difference < min_difference || smaller)
2238 min_difference = difference;
2240 has_larger = smaller;
2245 if (difference < min_difference && smaller)
2247 min_difference = difference;
2256 if (l == NULL && dirs == builtin_dirs)
2265 GtkIconInfo *icon_info = icon_info_new ();
2266 gboolean has_icon_file = FALSE;
2268 suffix = theme_dir_get_icon_suffix (min_dir, icon_name, &has_icon_file);
2269 suffix = best_suffix (suffix, allow_svg);
2270 g_assert (suffix != ICON_SUFFIX_NONE);
2274 file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
2275 icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
2280 icon_info->filename = NULL;
2283 if (min_dir->icon_data != NULL)
2284 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2286 if (icon_info->data == NULL && min_dir->cache != NULL)
2288 icon_info->data = _gtk_icon_cache_get_icon_data (min_dir->cache, icon_name, min_dir->subdir_index);
2289 if (icon_info->data)
2291 if (min_dir->icon_data == NULL)
2292 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2293 g_free, (GDestroyNotify)icon_data_free);
2295 g_hash_table_replace (min_dir->icon_data, g_strdup (icon_name), icon_info->data);
2299 if (icon_info->data == NULL && has_icon_file)
2301 gchar *icon_file_name, *icon_file_path;
2303 icon_file_name = g_strconcat (icon_name, ".icon", NULL);
2304 icon_file_path = g_build_filename (min_dir->dir, icon_file_name, NULL);
2306 if (g_file_test (icon_file_path, G_FILE_TEST_IS_REGULAR))
2308 if (min_dir->icon_data == NULL)
2309 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2310 g_free, (GDestroyNotify)icon_data_free);
2311 load_icon_data (min_dir, icon_file_path, icon_file_name);
2313 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2315 g_free (icon_file_name);
2316 g_free (icon_file_path);
2321 icon_info->cache_pixbuf = _gtk_icon_cache_get_icon (min_dir->cache, icon_name,
2322 min_dir->subdir_index);
2325 icon_info->dir_type = min_dir->type;
2326 icon_info->dir_size = min_dir->size;
2327 icon_info->threshold = min_dir->threshold;
2332 if (closest_builtin)
2333 return icon_info_new_builtin (closest_builtin);
2339 theme_list_icons (IconTheme *theme,
2343 GList *l = theme->dirs;
2350 if (context == dir->context ||
2355 _gtk_icon_cache_add_icons (dir->cache,
2362 g_hash_table_foreach (dir->icons,
2372 theme_list_contexts (IconTheme *theme,
2373 GHashTable *contexts)
2375 GList *l = theme->dirs;
2377 const char *context;
2383 context = g_quark_to_string (dir->context);
2384 g_hash_table_replace (contexts, (gpointer) context, NULL);
2391 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
2393 GKeyFile *icon_file;
2401 GError *error = NULL;
2405 icon_file = g_key_file_new ();
2406 g_key_file_set_list_separator (icon_file, ',');
2407 g_key_file_load_from_file (icon_file, path, 0, &error);
2410 g_error_free (error);
2411 g_key_file_free (icon_file);
2416 base_name = strip_suffix (name);
2418 data = g_slice_new0 (GtkIconData);
2419 /* takes ownership of base_name */
2420 g_hash_table_replace (dir->icon_data, base_name, data);
2422 ivalues = g_key_file_get_integer_list (icon_file,
2423 "Icon Data", "EmbeddedTextRectangle",
2429 data->has_embedded_rect = TRUE;
2430 data->x0 = ivalues[0];
2431 data->y0 = ivalues[1];
2432 data->x1 = ivalues[2];
2433 data->y1 = ivalues[3];
2439 str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
2442 split = g_strsplit (str, "|", -1);
2444 data->n_attach_points = g_strv_length (split);
2445 data->attach_points = g_new (GdkPoint, data->n_attach_points);
2448 while (split[i] != NULL && i < data->n_attach_points)
2450 split_point = strchr (split[i], ',');
2455 data->attach_points[i].x = atoi (split[i]);
2456 data->attach_points[i].y = atoi (split_point);
2465 data->display_name = g_key_file_get_locale_string (icon_file,
2466 "Icon Data", "DisplayName",
2468 g_key_file_free (icon_file);
2473 scan_directory (GtkIconThemePrivate *icon_theme,
2474 IconThemeDir *dir, char *full_dir)
2479 GTK_NOTE (ICONTHEME,
2480 g_print ("scanning directory %s\n", full_dir));
2481 dir->icons = g_hash_table_new_full (g_str_hash, g_str_equal,
2484 gdir = g_dir_open (full_dir, 0, NULL);
2489 while ((name = g_dir_read_name (gdir)))
2493 IconSuffix suffix, hash_suffix;
2495 if (g_str_has_suffix (name, ".icon"))
2497 if (dir->icon_data == NULL)
2498 dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2499 g_free, (GDestroyNotify)icon_data_free);
2501 path = g_build_filename (full_dir, name, NULL);
2502 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
2503 load_icon_data (dir, path, name);
2510 suffix = suffix_from_name (name);
2511 if (suffix == ICON_SUFFIX_NONE)
2514 base_name = strip_suffix (name);
2516 hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
2517 g_hash_table_replace (icon_theme->all_icons, base_name, NULL);
2518 /* takes ownership of base_name */
2519 g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
2526 theme_subdir_load (GtkIconTheme *icon_theme,
2528 GKeyFile *theme_file,
2534 IconThemeDirType type;
2535 char *context_string;
2542 GError *error = NULL;
2543 IconThemeDirMtime *dir_mtime;
2545 size = g_key_file_get_integer (theme_file, subdir, "Size", &error);
2548 g_error_free (error);
2549 g_warning ("Theme directory %s of theme %s has no size field\n",
2550 subdir, theme->name);
2554 type = ICON_THEME_DIR_THRESHOLD;
2555 type_string = g_key_file_get_string (theme_file, subdir, "Type", NULL);
2558 if (strcmp (type_string, "Fixed") == 0)
2559 type = ICON_THEME_DIR_FIXED;
2560 else if (strcmp (type_string, "Scalable") == 0)
2561 type = ICON_THEME_DIR_SCALABLE;
2562 else if (strcmp (type_string, "Threshold") == 0)
2563 type = ICON_THEME_DIR_THRESHOLD;
2565 g_free (type_string);
2569 context_string = g_key_file_get_string (theme_file, subdir, "Context", NULL);
2572 context = g_quark_from_string (context_string);
2573 g_free (context_string);
2576 if (g_key_file_has_key (theme_file, subdir, "MaxSize", NULL))
2577 max_size = g_key_file_get_integer (theme_file, subdir, "MaxSize", NULL);
2581 if (g_key_file_has_key (theme_file, subdir, "MinSize", NULL))
2582 min_size = g_key_file_get_integer (theme_file, subdir, "MinSize", NULL);
2586 if (g_key_file_has_key (theme_file, subdir, "Threshold", NULL))
2587 threshold = g_key_file_get_integer (theme_file, subdir, "Threshold", NULL);
2591 for (d = icon_theme->priv->dir_mtimes; d; d = d->next)
2593 dir_mtime = (IconThemeDirMtime *)d->data;
2595 if (dir_mtime->mtime == 0)
2596 continue; /* directory doesn't exist */
2598 full_dir = g_build_filename (dir_mtime->dir, subdir, NULL);
2600 /* First, see if we have a cache for the directory */
2601 if (dir_mtime->cache != NULL || g_file_test (full_dir, G_FILE_TEST_IS_DIR))
2603 if (dir_mtime->cache == NULL)
2605 /* This will return NULL if the cache doesn't exist or is outdated */
2606 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir_mtime->dir);
2609 dir = g_new (IconThemeDir, 1);
2611 dir->context = context;
2613 dir->min_size = min_size;
2614 dir->max_size = max_size;
2615 dir->threshold = threshold;
2616 dir->dir = full_dir;
2617 dir->icon_data = NULL;
2618 dir->subdir = g_strdup (subdir);
2619 if (dir_mtime->cache != NULL)
2621 dir->cache = _gtk_icon_cache_ref (dir_mtime->cache);
2622 dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
2627 dir->subdir_index = -1;
2628 scan_directory (icon_theme->priv, dir, full_dir);
2631 theme->dirs = g_list_prepend (theme->dirs, dir);
2639 icon_data_free (GtkIconData *icon_data)
2641 g_free (icon_data->attach_points);
2642 g_free (icon_data->display_name);
2643 g_slice_free (GtkIconData, icon_data);
2650 G_DEFINE_BOXED_TYPE (GtkIconInfo, gtk_icon_info,
2654 static GtkIconInfo *
2655 icon_info_new (void)
2657 GtkIconInfo *icon_info = g_slice_new0 (GtkIconInfo);
2659 icon_info->scale = -1.;
2660 icon_info->ref_count = 1;
2665 static GtkIconInfo *
2666 icon_info_new_builtin (BuiltinIcon *icon)
2668 GtkIconInfo *icon_info = icon_info_new ();
2670 icon_info->cache_pixbuf = g_object_ref (icon->pixbuf);
2671 icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
2672 icon_info->dir_size = icon->size;
2673 icon_info->threshold = 2;
2679 * gtk_icon_info_copy:
2680 * @icon_info: a #GtkIconInfo
2682 * Make a copy of a #GtkIconInfo.
2684 * Return value: the new GtkIconInfo
2689 gtk_icon_info_copy (GtkIconInfo *icon_info)
2692 g_return_val_if_fail (icon_info != NULL, NULL);
2694 icon_info->ref_count++;
2700 * gtk_icon_info_free:
2701 * @icon_info: a #GtkIconInfo
2703 * Free a #GtkIconInfo and associated information
2708 gtk_icon_info_free (GtkIconInfo *icon_info)
2710 g_return_if_fail (icon_info != NULL);
2712 icon_info->ref_count--;
2713 if (icon_info->ref_count > 0)
2716 g_free (icon_info->filename);
2717 if (icon_info->loadable)
2718 g_object_unref (icon_info->loadable);
2719 g_slist_free_full (icon_info->emblem_infos, (GDestroyNotify) gtk_icon_info_free);
2720 if (icon_info->pixbuf)
2721 g_object_unref (icon_info->pixbuf);
2722 if (icon_info->cache_pixbuf)
2723 g_object_unref (icon_info->cache_pixbuf);
2725 g_slice_free (GtkIconInfo, icon_info);
2729 * gtk_icon_info_get_base_size:
2730 * @icon_info: a #GtkIconInfo
2732 * Gets the base size for the icon. The base size
2733 * is a size for the icon that was specified by
2734 * the icon theme creator. This may be different
2735 * than the actual size of image; an example of
2736 * this is small emblem icons that can be attached
2737 * to a larger icon. These icons will be given
2738 * the same base size as the larger icons to which
2739 * they are attached.
2741 * Return value: the base size, or 0, if no base
2742 * size is known for the icon.
2747 gtk_icon_info_get_base_size (GtkIconInfo *icon_info)
2749 g_return_val_if_fail (icon_info != NULL, 0);
2751 return icon_info->dir_size;
2755 * gtk_icon_info_get_filename:
2756 * @icon_info: a #GtkIconInfo
2758 * Gets the filename for the icon. If the
2759 * %GTK_ICON_LOOKUP_USE_BUILTIN flag was passed
2760 * to gtk_icon_theme_lookup_icon(), there may be
2761 * no filename if a builtin icon is returned; in this
2762 * case, you should use gtk_icon_info_get_builtin_pixbuf().
2764 * Return value: (type filename): the filename for the icon, or %NULL
2765 * if gtk_icon_info_get_builtin_pixbuf() should be used instead. The
2766 * return value is owned by GTK+ and should not be modified or freed.
2771 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2773 g_return_val_if_fail (icon_info != NULL, NULL);
2775 return icon_info->filename;
2779 * gtk_icon_info_get_builtin_pixbuf:
2780 * @icon_info: a #GtkIconInfo structure
2782 * Gets the built-in image for this icon, if any. To allow
2783 * GTK+ to use built in icon images, you must pass the
2784 * %GTK_ICON_LOOKUP_USE_BUILTIN to
2785 * gtk_icon_theme_lookup_icon().
2787 * Return value: (transfer none): the built-in image pixbuf, or %NULL. No
2788 * extra reference is added to the returned pixbuf, so if
2789 * you want to keep it around, you must use g_object_ref().
2790 * The returned image must not be modified.
2795 gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
2797 g_return_val_if_fail (icon_info != NULL, NULL);
2799 if (icon_info->filename)
2802 return icon_info->cache_pixbuf;
2805 static gboolean icon_info_ensure_scale_and_pixbuf (GtkIconInfo*, gboolean);
2807 /* Combine the icon with all emblems, the first emblem is placed
2808 * in the southeast corner. Scale emblems to be at most 3/4 of the
2809 * size of the icon itself.
2812 apply_emblems (GtkIconInfo *info)
2814 GdkPixbuf *icon = NULL;
2818 if (info->emblem_infos == NULL)
2821 if (info->emblems_applied)
2824 w = gdk_pixbuf_get_width (info->pixbuf);
2825 h = gdk_pixbuf_get_height (info->pixbuf);
2827 for (l = info->emblem_infos, pos = 0; l; l = l->next, pos++)
2829 GtkIconInfo *emblem_info = l->data;
2831 if (icon_info_ensure_scale_and_pixbuf (emblem_info, FALSE))
2833 GdkPixbuf *emblem = emblem_info->pixbuf;
2835 gint x = 0, y = 0; /* silence compiler */
2838 ew = gdk_pixbuf_get_width (emblem);
2839 eh = gdk_pixbuf_get_height (emblem);
2871 icon = gdk_pixbuf_copy (info->pixbuf);
2876 gdk_pixbuf_composite (emblem, icon, x, y, ew, eh, x, y,
2877 scale, scale, GDK_INTERP_BILINEAR, 255);
2883 g_object_unref (info->pixbuf);
2884 info->pixbuf = icon;
2887 info->emblems_applied = TRUE;
2890 /* This function contains the complicated logic for deciding
2891 * on the size at which to load the icon and loading it at
2895 icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
2896 gboolean scale_only)
2898 int image_width, image_height;
2899 GdkPixbuf *source_pixbuf;
2902 /* First check if we already succeeded have the necessary
2903 * information (or failed earlier)
2905 if (scale_only && icon_info->scale >= 0)
2908 if (icon_info->pixbuf)
2910 apply_emblems (icon_info);
2914 if (icon_info->load_error)
2917 /* SVG icons are a special case - we just immediately scale them
2918 * to the desired size
2920 if (icon_info->filename && !icon_info->loadable)
2924 file = g_file_new_for_path (icon_info->filename);
2925 icon_info->loadable = G_LOADABLE_ICON (g_file_icon_new (file));
2926 g_object_unref (file);
2930 if (G_IS_FILE_ICON (icon_info->loadable))
2933 GFileInfo *file_info;
2934 const gchar *content_type;
2936 file = g_file_icon_get_file (G_FILE_ICON (icon_info->loadable));
2937 file_info = g_file_query_info (file,
2938 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
2939 G_FILE_QUERY_INFO_NONE,
2943 content_type = g_file_info_get_content_type (file_info);
2945 if (content_type && strcmp (content_type, "image/svg+xml") == 0)
2948 g_object_unref (file_info);
2954 GInputStream *stream;
2956 icon_info->scale = icon_info->desired_size / 1000.;
2961 stream = g_loadable_icon_load (icon_info->loadable,
2962 icon_info->desired_size,
2964 &icon_info->load_error);
2967 icon_info->pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
2968 icon_info->desired_size,
2969 icon_info->desired_size,
2972 &icon_info->load_error);
2973 g_object_unref (stream);
2976 if (!icon_info->pixbuf)
2979 apply_emblems (icon_info);
2984 /* In many cases, the scale can be determined without actual access
2985 * to the icon file. This is generally true when we have a size
2986 * for the directory where the icon is; the image size doesn't
2987 * matter in that case.
2989 if (icon_info->forced_size)
2990 icon_info->scale = -1;
2991 else if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
2992 icon_info->scale = 1.0;
2993 else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
2995 if (icon_info->desired_size >= icon_info->dir_size - icon_info->threshold &&
2996 icon_info->desired_size <= icon_info->dir_size + icon_info->threshold)
2997 icon_info->scale = 1.0;
2998 else if (icon_info->dir_size > 0)
2999 icon_info->scale =(gdouble) icon_info->desired_size / icon_info->dir_size;
3001 else if (icon_info->dir_type == ICON_THEME_DIR_SCALABLE)
3003 if (icon_info->dir_size > 0)
3004 icon_info->scale = (gdouble) icon_info->desired_size / icon_info->dir_size;
3007 if (icon_info->scale >= 0. && scale_only)
3010 /* At this point, we need to actually get the icon; either from the
3011 * builtin image or by loading the file
3013 source_pixbuf = NULL;
3014 if (icon_info->cache_pixbuf)
3015 source_pixbuf = g_object_ref (icon_info->cache_pixbuf);
3018 GInputStream *stream;
3020 stream = g_loadable_icon_load (icon_info->loadable,
3021 icon_info->desired_size,
3023 &icon_info->load_error);
3026 source_pixbuf = gdk_pixbuf_new_from_stream (stream,
3028 &icon_info->load_error);
3029 g_object_unref (stream);
3036 /* Do scale calculations that depend on the image size
3038 image_width = gdk_pixbuf_get_width (source_pixbuf);
3039 image_height = gdk_pixbuf_get_height (source_pixbuf);
3041 if (icon_info->scale < 0.0)
3043 gint image_size = MAX (image_width, image_height);
3045 icon_info->scale = (gdouble)icon_info->desired_size / (gdouble)image_size;
3047 icon_info->scale = 1.0;
3049 if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED &&
3050 !icon_info->forced_size)
3051 icon_info->scale = MIN (icon_info->scale, 1.0);
3054 /* We don't short-circuit out here for scale_only, since, now
3055 * we've loaded the icon, we might as well go ahead and finish
3056 * the job. This is a bit of a waste when we scale here
3057 * and never get the final pixbuf; at the cost of a bit of
3058 * extra complexity, we could keep the source pixbuf around
3059 * but not actually scale it until needed.
3061 if (icon_info->scale == 1.0)
3062 icon_info->pixbuf = source_pixbuf;
3065 icon_info->pixbuf = gdk_pixbuf_scale_simple (source_pixbuf,
3066 0.5 + image_width * icon_info->scale,
3067 0.5 + image_height * icon_info->scale,
3068 GDK_INTERP_BILINEAR);
3069 g_object_unref (source_pixbuf);
3072 apply_emblems (icon_info);
3078 * gtk_icon_info_load_icon:
3079 * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
3080 * @error: (allow-none): location to store error information on failure,
3083 * Renders an icon previously looked up in an icon theme using
3084 * gtk_icon_theme_lookup_icon(); the size will be based on the size
3085 * passed to gtk_icon_theme_lookup_icon(). Note that the resulting
3086 * pixbuf may not be exactly this size; an icon theme may have icons
3087 * that differ slightly from their nominal sizes, and in addition GTK+
3088 * will avoid scaling icons that it considers sufficiently close to the
3089 * requested size or for which the source image would have to be scaled
3090 * up too far. (This maintains sharpness.). This behaviour can be changed
3091 * by passing the %GTK_ICON_LOOKUP_FORCE_SIZE flag when obtaining
3092 * the #GtkIconInfo. If this flag has been specified, the pixbuf
3093 * returned by this function will be scaled to the exact size.
3095 * Return value: (transfer full): the rendered icon; this may be a newly
3096 * created icon or a new reference to an internal icon, so you must
3097 * not modify the icon. Use g_object_unref() to release your reference
3103 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
3106 g_return_val_if_fail (icon_info != NULL, NULL);
3107 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
3109 if (!icon_info_ensure_scale_and_pixbuf (icon_info, FALSE))
3111 if (icon_info->load_error)
3112 g_propagate_error (error, icon_info->load_error);
3114 g_set_error_literal (error,
3115 GTK_ICON_THEME_ERROR,
3116 GTK_ICON_THEME_NOT_FOUND,
3117 _("Failed to load icon"));
3122 return g_object_ref (icon_info->pixbuf);
3126 gdk_color_to_css (GdkColor *color)
3128 return g_strdup_printf ("rgb(%d,%d,%d)",
3135 gdk_rgba_to_css (const GdkRGBA *color)
3137 /* drop alpha for now, since librsvg does not understand rgba() */
3138 return g_strdup_printf ("rgb(%d,%d,%d)",
3139 (gint)(color->red * 255),
3140 (gint)(color->green * 255),
3141 (gint)(color->blue * 255));
3145 _gtk_icon_info_load_symbolic_internal (GtkIconInfo *icon_info,
3146 const gchar *css_fg,
3147 const gchar *css_success,
3148 const gchar *css_warning,
3149 const gchar *css_error,
3152 GInputStream *stream;
3155 gchar *success, *warning, *err;
3157 /* css_fg can't possibly have failed, otherwise
3158 * that would mean we have a broken style */
3159 g_return_val_if_fail (css_fg != NULL, NULL);
3161 success = warning = err = NULL;
3165 GdkColor success_default_color = { 0, 0x4e00, 0x9a00, 0x0600 };
3166 success = gdk_color_to_css (&success_default_color);
3170 GdkColor warning_default_color = { 0, 0xf500, 0x7900, 0x3e00 };
3171 warning = gdk_color_to_css (&warning_default_color);
3175 GdkColor error_default_color = { 0, 0xcc00, 0x0000, 0x0000 };
3176 err = gdk_color_to_css (&error_default_color);
3180 data = g_strconcat ("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
3181 "<svg version=\"1.1\"\n"
3182 " xmlns=\"http://www.w3.org/2000/svg\"\n"
3183 " xmlns:xi=\"http://www.w3.org/2001/XInclude\"\n"
3186 " <style type=\"text/css\">\n"
3188 " fill: ", css_fg," !important;\n"
3191 " fill: ", css_warning ? css_warning : warning," !important;\n"
3194 " fill: ", css_error ? css_error : err," !important;\n"
3197 " fill: ", css_success ? css_success : success," !important;\n"
3200 " <xi:include href=\"", icon_info->filename, "\"/>\n"
3207 stream = g_memory_input_stream_new_from_data (data, -1, g_free);
3208 pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
3209 icon_info->desired_size,
3210 icon_info->desired_size,
3214 g_object_unref (stream);
3220 * gtk_icon_info_load_symbolic:
3221 * @icon_info: a #GtkIconInfo
3222 * @fg: a #GdkRGBA representing the foreground color of the icon
3223 * @success_color: (allow-none): a #GdkRGBA representing the warning color
3224 * of the icon or %NULL to use the default color
3225 * @warning_color: (allow-none): a #GdkRGBA representing the warning color
3226 * of the icon or %NULL to use the default color
3227 * @error_color: (allow-none): a #GdkRGBA representing the error color
3228 * of the icon or %NULL to use the default color (allow-none)
3229 * @was_symbolic: (out) (allow-none): a #gboolean, returns whether the
3230 * loaded icon was a symbolic one and whether the @fg color was
3232 * @error: (allow-none): location to store error information on failure,
3235 * Loads an icon, modifying it to match the system colours for the foreground,
3236 * success, warning and error colors provided. If the icon is not a symbolic
3237 * one, the function will return the result from gtk_icon_info_load_icon().
3239 * This allows loading symbolic icons that will match the system theme.
3241 * Unless you are implementing a widget, you will want to use
3242 * g_themed_icon_new_with_default_fallbacks() to load the icon.
3244 * As implementation details, the icon loaded needs to be of SVG type,
3245 * contain the "symbolic" term as the last component of the icon name,
3246 * and use the 'fg', 'success', 'warning' and 'error' CSS styles in the
3249 * See the <ulink url="http://www.freedesktop.org/wiki/SymbolicIcons">Symbolic Icons spec</ulink>
3250 * for more information about symbolic icons.
3252 * Return value: (transfer full): a #GdkPixbuf representing the loaded icon
3257 gtk_icon_info_load_symbolic (GtkIconInfo *icon_info,
3259 const GdkRGBA *success_color,
3260 const GdkRGBA *warning_color,
3261 const GdkRGBA *error_color,
3262 gboolean *was_symbolic,
3271 g_return_val_if_fail (fg != NULL, NULL);
3273 if (!icon_info->filename ||
3274 !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3277 *was_symbolic = FALSE;
3278 return gtk_icon_info_load_icon (icon_info, error);
3282 *was_symbolic = TRUE;
3284 css_fg = gdk_rgba_to_css (fg);
3286 css_success = css_warning = css_error = NULL;
3289 css_warning = gdk_rgba_to_css (warning_color);
3292 css_error = gdk_rgba_to_css (error_color);
3295 css_success = gdk_rgba_to_css (success_color);
3297 pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
3298 css_fg, css_success,
3299 css_warning, css_error,
3302 g_free (css_warning);
3303 g_free (css_success);
3310 * gtk_icon_info_load_symbolic_for_context:
3311 * @icon_info: a #GtkIconInfo
3312 * @context: a #GtkStyleContext
3313 * @was_symbolic: (out) (allow-none): a #gboolean, returns whether the
3314 * loaded icon was a symbolic one and whether the @fg color was
3316 * @error: (allow-none): location to store error information on failure,
3319 * Loads an icon, modifying it to match the system colors for the foreground,
3320 * success, warning and error colors provided. If the icon is not a symbolic
3321 * one, the function will return the result from gtk_icon_info_load_icon().
3322 * This function uses the regular foreground color and the symbolic colors
3323 * with the names "success_color", "warning_color" and "error_color" from
3326 * This allows loading symbolic icons that will match the system theme.
3328 * See gtk_icon_info_load_symbolic() for more details.
3330 * Return value: (transfer full): a #GdkPixbuf representing the loaded icon
3335 gtk_icon_info_load_symbolic_for_context (GtkIconInfo *icon_info,
3336 GtkStyleContext *context,
3337 gboolean *was_symbolic,
3341 GdkRGBA *color = NULL;
3343 gchar *css_fg = NULL, *css_success;
3344 gchar *css_warning, *css_error;
3345 GtkStateFlags state;
3347 if (!icon_info->filename ||
3348 !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3351 *was_symbolic = FALSE;
3352 return gtk_icon_info_load_icon (icon_info, error);
3356 *was_symbolic = TRUE;
3358 state = gtk_style_context_get_state (context);
3359 gtk_style_context_get (context, state, "color", &color, NULL);
3362 css_fg = gdk_rgba_to_css (color);
3363 gdk_rgba_free (color);
3366 css_success = css_warning = css_error = NULL;
3368 if (gtk_style_context_lookup_color (context, "success_color", &rgba))
3369 css_success = gdk_rgba_to_css (&rgba);
3371 if (gtk_style_context_lookup_color (context, "warning_color", &rgba))
3372 css_warning = gdk_rgba_to_css (&rgba);
3374 if (gtk_style_context_lookup_color (context, "error_color", &rgba))
3375 css_error = gdk_rgba_to_css (&rgba);
3377 pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
3378 css_fg, css_success,
3379 css_warning, css_error,
3383 g_free (css_success);
3384 g_free (css_warning);
3391 * gtk_icon_info_load_symbolic_for_style:
3392 * @icon_info: a #GtkIconInfo
3393 * @style: a #GtkStyle to take the colors from
3394 * @state: the widget state to use for colors
3395 * @was_symbolic: (out) (allow-none): a #gboolean, returns whether the
3396 * loaded icon was a symbolic one and whether the @fg color was
3398 * @error: (allow-none): location to store error information on failure,
3401 * Loads an icon, modifying it to match the system colours for the foreground,
3402 * success, warning and error colors provided. If the icon is not a symbolic
3403 * one, the function will return the result from gtk_icon_info_load_icon().
3405 * This allows loading symbolic icons that will match the system theme.
3407 * See gtk_icon_info_load_symbolic() for more details.
3409 * Return value: (transfer full): a #GdkPixbuf representing the loaded icon
3413 * Deprecated: 3.0: Use gtk_icon_info_load_symbolic_for_context() instead
3416 gtk_icon_info_load_symbolic_for_style (GtkIconInfo *icon_info,
3419 gboolean *was_symbolic,
3423 GdkColor success_color;
3424 GdkColor warning_color;
3425 GdkColor error_color;
3427 gchar *css_fg, *css_success;
3428 gchar *css_warning, *css_error;
3430 if (!icon_info->filename ||
3431 !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3434 *was_symbolic = FALSE;
3435 return gtk_icon_info_load_icon (icon_info, error);
3439 *was_symbolic = TRUE;
3441 fg = &style->fg[state];
3442 css_fg = gdk_color_to_css (fg);
3444 css_success = css_warning = css_error = NULL;
3446 if (gtk_style_lookup_color (style, "success_color", &success_color))
3447 css_success = gdk_color_to_css (&success_color);
3449 if (gtk_style_lookup_color (style, "warning_color", &warning_color))
3450 css_warning = gdk_color_to_css (&warning_color);
3452 if (gtk_style_lookup_color (style, "error_color", &error_color))
3453 css_error = gdk_color_to_css (&error_color);
3455 pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
3456 css_fg, css_success,
3457 css_warning, css_error,
3461 g_free (css_success);
3462 g_free (css_warning);
3469 * gtk_icon_info_set_raw_coordinates:
3470 * @icon_info: a #GtkIconInfo
3471 * @raw_coordinates: whether the coordinates of embedded rectangles
3472 * and attached points should be returned in their original
3475 * Sets whether the coordinates returned by gtk_icon_info_get_embedded_rect()
3476 * and gtk_icon_info_get_attach_points() should be returned in their
3477 * original form as specified in the icon theme, instead of scaled
3478 * appropriately for the pixbuf returned by gtk_icon_info_load_icon().
3480 * Raw coordinates are somewhat strange; they are specified to be with
3481 * respect to the unscaled pixmap for PNG and XPM icons, but for SVG
3482 * icons, they are in a 1000x1000 coordinate space that is scaled
3483 * to the final size of the icon. You can determine if the icon is an SVG
3484 * icon by using gtk_icon_info_get_filename(), and seeing if it is non-%NULL
3485 * and ends in '.svg'.
3487 * This function is provided primarily to allow compatibility wrappers
3488 * for older API's, and is not expected to be useful for applications.
3493 gtk_icon_info_set_raw_coordinates (GtkIconInfo *icon_info,
3494 gboolean raw_coordinates)
3496 g_return_if_fail (icon_info != NULL);
3498 icon_info->raw_coordinates = raw_coordinates != FALSE;
3501 /* Scale coordinates from the icon data prior to returning
3505 icon_info_scale_point (GtkIconInfo *icon_info,
3511 if (icon_info->raw_coordinates)
3518 if (!icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3521 *x_out = 0.5 + x * icon_info->scale;
3522 *y_out = 0.5 + y * icon_info->scale;
3529 * gtk_icon_info_get_embedded_rect:
3530 * @icon_info: a #GtkIconInfo
3531 * @rectangle: (out): #GdkRectangle in which to store embedded
3532 * rectangle coordinates; coordinates are only stored
3533 * when this function returns %TRUE.
3535 * Gets the coordinates of a rectangle within the icon
3536 * that can be used for display of information such
3537 * as a preview of the contents of a text file.
3538 * See gtk_icon_info_set_raw_coordinates() for further
3539 * information about the coordinate system.
3541 * Return value: %TRUE if the icon has an embedded rectangle
3546 gtk_icon_info_get_embedded_rect (GtkIconInfo *icon_info,
3547 GdkRectangle *rectangle)
3549 g_return_val_if_fail (icon_info != NULL, FALSE);
3551 if (icon_info->data && icon_info->data->has_embedded_rect &&
3552 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3554 gint scaled_x0, scaled_y0;
3555 gint scaled_x1, scaled_y1;
3559 icon_info_scale_point (icon_info,
3560 icon_info->data->x0, icon_info->data->y0,
3561 &scaled_x0, &scaled_y0);
3562 icon_info_scale_point (icon_info,
3563 icon_info->data->x1, icon_info->data->y1,
3564 &scaled_x1, &scaled_y1);
3566 rectangle->x = scaled_x0;
3567 rectangle->y = scaled_y0;
3568 rectangle->width = scaled_x1 - rectangle->x;
3569 rectangle->height = scaled_y1 - rectangle->y;
3579 * gtk_icon_info_get_attach_points:
3580 * @icon_info: a #GtkIconInfo
3581 * @points: (allow-none) (array length=n_points) (out): location to store pointer to an array of points, or %NULL
3582 * free the array of points with g_free().
3583 * @n_points: (allow-none): location to store the number of points in @points, or %NULL
3585 * Fetches the set of attach points for an icon. An attach point
3586 * is a location in the icon that can be used as anchor points for attaching
3587 * emblems or overlays to the icon.
3589 * Return value: %TRUE if there are any attach points for the icon.
3594 gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
3598 g_return_val_if_fail (icon_info != NULL, FALSE);
3600 if (icon_info->data && icon_info->data->n_attach_points &&
3601 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3607 *points = g_new (GdkPoint, icon_info->data->n_attach_points);
3608 for (i = 0; i < icon_info->data->n_attach_points; i++)
3609 icon_info_scale_point (icon_info,
3610 icon_info->data->attach_points[i].x,
3611 icon_info->data->attach_points[i].y,
3617 *n_points = icon_info->data->n_attach_points;
3633 * gtk_icon_info_get_display_name:
3634 * @icon_info: a #GtkIconInfo
3636 * Gets the display name for an icon. A display name is a
3637 * string to be used in place of the icon name in a user
3638 * visible context like a list of icons.
3640 * Return value: the display name for the icon or %NULL, if
3641 * the icon doesn't have a specified display name. This value
3642 * is owned @icon_info and must not be modified or free.
3647 gtk_icon_info_get_display_name (GtkIconInfo *icon_info)
3649 g_return_val_if_fail (icon_info != NULL, NULL);
3651 if (icon_info->data)
3652 return icon_info->data->display_name;
3663 * gtk_icon_theme_add_builtin_icon:
3664 * @icon_name: the name of the icon to register
3665 * @size: the size at which to register the icon (different
3666 * images can be registered for the same icon name
3667 * at different sizes.)
3668 * @pixbuf: #GdkPixbuf that contains the image to use
3671 * Registers a built-in icon for icon theme lookups. The idea
3672 * of built-in icons is to allow an application or library
3673 * that uses themed icons to function requiring files to
3674 * be present in the file system. For instance, the default
3675 * images for all of GTK+'s stock icons are registered
3678 * In general, if you use gtk_icon_theme_add_builtin_icon()
3679 * you should also install the icon in the icon theme, so
3680 * that the icon is generally available.
3682 * This function will generally be used with pixbufs loaded
3683 * via gdk_pixbuf_new_from_inline().
3688 gtk_icon_theme_add_builtin_icon (const gchar *icon_name,
3692 BuiltinIcon *default_icon;
3696 g_return_if_fail (icon_name != NULL);
3697 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3699 if (!icon_theme_builtin_icons)
3700 icon_theme_builtin_icons = g_hash_table_new (g_str_hash, g_str_equal);
3702 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
3704 key = g_strdup (icon_name);
3706 key = (gpointer)icon_name; /* Won't get stored */
3708 default_icon = g_new (BuiltinIcon, 1);
3709 default_icon->size = size;
3710 default_icon->pixbuf = g_object_ref (pixbuf);
3711 icons = g_slist_prepend (icons, default_icon);
3713 /* Replaces value, leaves key untouched
3715 g_hash_table_insert (icon_theme_builtin_icons, key, icons);
3718 /* Look up a builtin icon; the min_difference_p and
3719 * has_larger_p out parameters allow us to combine
3720 * this lookup with searching through the actual directories
3721 * of the "hicolor" icon theme. See theme_lookup_icon()
3722 * for how they are used.
3724 static BuiltinIcon *
3725 find_builtin_icon (const gchar *icon_name,
3727 gint *min_difference_p,
3728 gboolean *has_larger_p)
3730 GSList *icons = NULL;
3731 gint min_difference = G_MAXINT;
3732 gboolean has_larger = FALSE;
3733 BuiltinIcon *min_icon = NULL;
3735 if (!icon_theme_builtin_icons)
3738 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
3742 BuiltinIcon *default_icon = icons->data;
3743 int min, max, difference;
3746 min = default_icon->size - 2;
3747 max = default_icon->size + 2;
3748 smaller = size < min;
3750 difference = min - size;
3751 else if (size > max)
3752 difference = size - max;
3756 if (difference == 0)
3758 min_icon = default_icon;
3764 if (difference < min_difference || smaller)
3766 min_difference = difference;
3767 min_icon = default_icon;
3768 has_larger = smaller;
3773 if (difference < min_difference && smaller)
3775 min_difference = difference;
3776 min_icon = default_icon;
3780 icons = icons->next;
3783 if (min_difference_p)
3784 *min_difference_p = min_difference;
3786 *has_larger_p = has_larger;
3792 _gtk_icon_theme_check_reload (GdkDisplay *display)
3796 GtkIconTheme *icon_theme;
3798 n_screens = gdk_display_get_n_screens (display);
3800 for (i = 0; i < n_screens; i++)
3802 screen = gdk_display_get_screen (display, i);
3804 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
3807 icon_theme->priv->check_reload = TRUE;
3808 ensure_valid_themes (icon_theme);
3809 icon_theme->priv->check_reload = FALSE;
3816 * gtk_icon_theme_lookup_by_gicon:
3817 * @icon_theme: a #GtkIconTheme
3818 * @icon: the #GIcon to look up
3819 * @size: desired icon size
3820 * @flags: flags modifying the behavior of the icon lookup
3822 * Looks up an icon and returns a structure containing
3823 * information such as the filename of the icon.
3824 * The icon can then be rendered into a pixbuf using
3825 * gtk_icon_info_load_icon().
3827 * Return value: a #GtkIconInfo structure containing
3828 * information about the icon, or %NULL if the icon
3829 * wasn't found. Free with gtk_icon_info_free()
3834 gtk_icon_theme_lookup_by_gicon (GtkIconTheme *icon_theme,
3837 GtkIconLookupFlags flags)
3841 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
3842 g_return_val_if_fail (G_IS_ICON (icon), NULL);
3844 if (G_IS_LOADABLE_ICON (icon))
3846 info = icon_info_new ();
3847 info->loadable = G_LOADABLE_ICON (g_object_ref (icon));
3849 info->dir_type = ICON_THEME_DIR_UNTHEMED;
3850 info->dir_size = size;
3851 info->desired_size = size;
3852 info->threshold = 2;
3853 info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
3857 else if (G_IS_THEMED_ICON (icon))
3859 const gchar **names;
3861 names = (const gchar **)g_themed_icon_get_names (G_THEMED_ICON (icon));
3862 info = gtk_icon_theme_choose_icon (icon_theme, names, size, flags);
3866 else if (G_IS_EMBLEMED_ICON (icon))
3868 GIcon *base, *emblem;
3870 GtkIconInfo *emblem_info;
3872 if (GTK_IS_NUMERABLE_ICON (icon))
3873 _gtk_numerable_icon_set_background_icon_size (GTK_NUMERABLE_ICON (icon), size / 2);
3875 base = g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon));
3876 info = gtk_icon_theme_lookup_by_gicon (icon_theme, base, size, flags);
3879 list = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon));
3880 for (l = list; l; l = l->next)
3882 emblem = g_emblem_get_icon (G_EMBLEM (l->data));
3883 /* always force size for emblems */
3884 emblem_info = gtk_icon_theme_lookup_by_gicon (icon_theme, emblem, size / 2, flags | GTK_ICON_LOOKUP_FORCE_SIZE);
3886 info->emblem_infos = g_slist_prepend (info->emblem_infos, emblem_info);
3892 else if (GDK_IS_PIXBUF (icon))
3896 pixbuf = GDK_PIXBUF (icon);
3898 if ((flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0)
3900 gint width, height, max;
3904 width = gdk_pixbuf_get_width (pixbuf);
3905 height = gdk_pixbuf_get_height (pixbuf);
3906 max = MAX (width, height);
3907 scale = (gdouble) size / (gdouble) max;
3909 scaled = gdk_pixbuf_scale_simple (pixbuf,
3910 0.5 + width * scale,
3911 0.5 + height * scale,
3912 GDK_INTERP_BILINEAR);
3914 info = gtk_icon_info_new_for_pixbuf (icon_theme, scaled);
3916 g_object_unref (scaled);
3920 info = gtk_icon_info_new_for_pixbuf (icon_theme, pixbuf);
3930 * gtk_icon_info_new_for_pixbuf:
3931 * @icon_theme: a #GtkIconTheme
3932 * @pixbuf: the pixbuf to wrap in a #GtkIconInfo
3934 * Creates a #GtkIconInfo for a #GdkPixbuf.
3936 * Returns: a #GtkIconInfo
3941 gtk_icon_info_new_for_pixbuf (GtkIconTheme *icon_theme,
3946 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
3947 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
3949 info = icon_info_new ();
3950 info->pixbuf = g_object_ref (pixbuf);
3952 info->dir_type = ICON_THEME_DIR_UNTHEMED;