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"
54 * SECTION:gtkicontheme
55 * @Short_description: Looking up icons by name
56 * @Title: GtkIconTheme
58 * #GtkIconTheme provides a facility for looking up icons by name
59 * and size. The main reason for using a name rather than simply
60 * providing a filename is to allow different icons to be used
61 * depending on what <firstterm>icon theme</firstterm> is selected
62 * by the user. The operation of icon themes on Linux and Unix
64 * url="http://www.freedesktop.org/Standards/icon-theme-spec">Icon
65 * Theme Specification</ulink>. There is a default icon theme,
66 * named <literal>hicolor</literal> where applications should install
67 * their icons, but more additional application themes can be
68 * installed as operating system vendors and users choose.
70 * Named icons are similar to the <xref linkend="gtk3-Themeable-Stock-Images"/>
71 * facility, and the distinction between the two may be a bit confusing.
72 * A few things to keep in mind:
75 * Stock images usually are used in conjunction with
76 * <xref linkend="gtk3-Stock-Items"/>, such as %GTK_STOCK_OK or
77 * %GTK_STOCK_OPEN. Named icons are easier to set up and therefore
78 * are more useful for new icons that an application wants to
79 * add, such as application icons or window icons.
82 * Stock images can only be loaded at the symbolic sizes defined
83 * by the #GtkIconSize enumeration, or by custom sizes defined
84 * by gtk_icon_size_register(), while named icons are more flexible
85 * and any pixel size can be specified.
88 * Because stock images are closely tied to stock items, and thus
89 * to actions in the user interface, stock images may come in
90 * multiple variants for different widget states or writing
94 * A good rule of thumb is that if there is a stock image for what
95 * you want to use, use it, otherwise use a named icon. It turns
96 * out that internally stock images are generally defined in
97 * terms of one or more named icons. (An example of the
98 * more than one case is icons that depend on writing direction;
99 * %GTK_STOCK_GO_FORWARD uses the two themed icons
100 * "gtk-stock-go-forward-ltr" and "gtk-stock-go-forward-rtl".)
102 * In many cases, named themes are used indirectly, via #GtkImage
103 * or stock items, rather than directly, but looking up icons
104 * directly is also simple. The #GtkIconTheme object acts
105 * as a database of all the icons in the current theme. You
106 * can create new #GtkIconTheme objects, but its much more
107 * efficient to use the standard icon theme for the #GdkScreen
108 * so that the icon information is shared with other people
109 * looking up icons. In the case where the default screen is
110 * being used, looking up an icon can be as simple as:
113 * GError *error = NULL;
114 * GtkIconTheme *icon_theme;
117 * icon_theme = gtk_icon_theme_get_default ();
118 * pixbuf = gtk_icon_theme_load_icon (icon_theme,
119 * "my-icon-name", // icon name
125 * g_warning ("Couldn't load icon: %s", error->message);
126 * g_error_free (error);
131 * g_object_unref (pixbuf);
138 #define DEFAULT_THEME_NAME "hicolor"
142 ICON_THEME_DIR_FIXED,
143 ICON_THEME_DIR_SCALABLE,
144 ICON_THEME_DIR_THRESHOLD,
145 ICON_THEME_DIR_UNTHEMED
148 /* In reverse search order: */
151 ICON_SUFFIX_NONE = 0,
152 ICON_SUFFIX_XPM = 1 << 0,
153 ICON_SUFFIX_SVG = 1 << 1,
154 ICON_SUFFIX_PNG = 1 << 2,
155 HAS_ICON_FILE = 1 << 3
159 struct _GtkIconThemePrivate
161 gchar *current_theme;
162 gchar *fallback_theme;
164 gint search_path_len;
166 guint custom_theme : 1;
167 guint is_screen_singleton : 1;
168 guint pixbuf_supports_svg : 1;
169 guint themes_valid : 1;
170 guint check_reload : 1;
171 guint loading_themes : 1;
173 /* A list of all the themes needed to look up icons.
174 * In search order, without duplicates
177 GHashTable *unthemed_icons;
179 /* Note: The keys of this hashtable are owned by the
180 * themedir and unthemed hashtables.
182 GHashTable *all_icons;
184 /* GdkScreen for the icon theme (may be NULL)
188 /* time when we last stat:ed for theme changes */
189 glong last_stat_time;
192 gulong reset_styles_idle;
197 /* Information about the source
200 GLoadableIcon *loadable;
201 GSList *emblem_infos;
203 /* Cache pixbuf (if there is any) */
204 GdkPixbuf *cache_pixbuf;
208 /* Information about the directory where
209 * the source was found
211 IconThemeDirType dir_type;
215 /* Parameters influencing the scaled icon
218 guint raw_coordinates : 1;
219 guint forced_size : 1;
220 guint emblems_applied : 1;
224 /* Cached information if we go ahead and try to load
239 /* In search order */
245 IconThemeDirType type;
260 GHashTable *icon_data;
266 char *no_svg_filename;
278 time_t mtime; /* 0 == not existing or not a dir */
283 static void gtk_icon_theme_finalize (GObject *object);
284 static void theme_dir_destroy (IconThemeDir *dir);
286 static void theme_destroy (IconTheme *theme);
287 static GtkIconInfo *theme_lookup_icon (IconTheme *theme,
288 const char *icon_name,
291 gboolean use_default_icons);
292 static void theme_list_icons (IconTheme *theme,
295 static void theme_list_contexts (IconTheme *theme,
296 GHashTable *contexts);
297 static void theme_subdir_load (GtkIconTheme *icon_theme,
299 GKeyFile *theme_file,
301 static void do_theme_change (GtkIconTheme *icon_theme);
303 static void blow_themes (GtkIconTheme *icon_themes);
304 static gboolean rescan_themes (GtkIconTheme *icon_themes);
306 static void icon_data_free (GtkIconData *icon_data);
307 static void load_icon_data (IconThemeDir *dir,
311 static IconSuffix theme_dir_get_icon_suffix (IconThemeDir *dir,
312 const gchar *icon_name,
313 gboolean *has_icon_file);
316 static GtkIconInfo *icon_info_new (void);
317 static GtkIconInfo *icon_info_new_builtin (BuiltinIcon *icon);
319 static IconSuffix suffix_from_name (const char *name);
321 static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
323 gint *min_difference_p,
324 gboolean *has_larger_p);
326 static guint signal_changed = 0;
328 static GHashTable *icon_theme_builtin_icons;
330 /* also used in gtkiconfactory.c */
331 GtkIconCache *_builtin_cache = NULL;
332 static GList *builtin_dirs = NULL;
334 G_DEFINE_TYPE (GtkIconTheme, gtk_icon_theme, G_TYPE_OBJECT)
337 * gtk_icon_theme_new:
339 * Creates a new icon theme object. Icon theme objects are used
340 * to lookup up an icon by name in a particular icon theme.
341 * Usually, you'll want to use gtk_icon_theme_get_default()
342 * or gtk_icon_theme_get_for_screen() rather than creating
343 * a new icon theme object for scratch.
345 * Return value: the newly created #GtkIconTheme object.
350 gtk_icon_theme_new (void)
352 return g_object_new (GTK_TYPE_ICON_THEME, NULL);
356 * gtk_icon_theme_get_default:
358 * Gets the icon theme for the default screen. See
359 * gtk_icon_theme_get_for_screen().
361 * Return value: (transfer none): A unique #GtkIconTheme associated with
362 * the default screen. This icon theme is associated with
363 * the screen and can be used as long as the screen
364 * is open. Do not ref or unref it.
369 gtk_icon_theme_get_default (void)
371 return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
375 * gtk_icon_theme_get_for_screen:
376 * @screen: a #GdkScreen
378 * Gets the icon theme object associated with @screen; if this
379 * function has not previously been called for the given
380 * screen, a new icon theme object will be created and
381 * associated with the screen. Icon theme objects are
382 * fairly expensive to create, so using this function
383 * is usually a better choice than calling than gtk_icon_theme_new()
384 * and setting the screen yourself; by using this function
385 * a single icon theme object will be shared between users.
387 * Return value: (transfer none): A unique #GtkIconTheme associated with
388 * the given screen. This icon theme is associated with
389 * the screen and can be used as long as the screen
390 * is open. Do not ref or unref it.
395 gtk_icon_theme_get_for_screen (GdkScreen *screen)
397 GtkIconTheme *icon_theme;
399 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
401 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
404 GtkIconThemePrivate *priv;
406 icon_theme = gtk_icon_theme_new ();
407 gtk_icon_theme_set_screen (icon_theme, screen);
409 priv = icon_theme->priv;
410 priv->is_screen_singleton = TRUE;
412 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), icon_theme);
419 gtk_icon_theme_class_init (GtkIconThemeClass *klass)
421 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
423 gobject_class->finalize = gtk_icon_theme_finalize;
426 * GtkIconTheme::changed
427 * @icon_theme: the icon theme
429 * Emitted when the current icon theme is switched or GTK+ detects
430 * that a change has occurred in the contents of the current
433 signal_changed = g_signal_new (I_("changed"),
434 G_TYPE_FROM_CLASS (klass),
436 G_STRUCT_OFFSET (GtkIconThemeClass, changed),
438 g_cclosure_marshal_VOID__VOID,
441 g_type_class_add_private (klass, sizeof (GtkIconThemePrivate));
445 /* Callback when the display that the icon theme is attached to
446 * is closed; unset the screen, and if it's the unique theme
447 * for the screen, drop the reference
450 display_closed (GdkDisplay *display,
452 GtkIconTheme *icon_theme)
454 GtkIconThemePrivate *priv = icon_theme->priv;
455 GdkScreen *screen = priv->screen;
456 gboolean was_screen_singleton = priv->is_screen_singleton;
458 if (was_screen_singleton)
460 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), NULL);
461 priv->is_screen_singleton = FALSE;
464 gtk_icon_theme_set_screen (icon_theme, NULL);
466 if (was_screen_singleton)
468 g_object_unref (icon_theme);
473 update_current_theme (GtkIconTheme *icon_theme)
475 #define theme_changed(_old, _new) \
476 ((_old && !_new) || (!_old && _new) || \
477 (_old && _new && strcmp (_old, _new) != 0))
478 GtkIconThemePrivate *priv = icon_theme->priv;
480 if (!priv->custom_theme)
483 gchar *fallback_theme = NULL;
484 gboolean changed = FALSE;
488 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
489 g_object_get (settings,
490 "gtk-icon-theme-name", &theme,
491 "gtk-fallback-icon-theme", &fallback_theme, NULL);
494 /* ensure that the current theme (even when just the default)
495 * is searched before any fallback theme
497 if (!theme && fallback_theme)
498 theme = g_strdup (DEFAULT_THEME_NAME);
500 if (theme_changed (priv->current_theme, theme))
502 g_free (priv->current_theme);
503 priv->current_theme = theme;
509 if (theme_changed (priv->fallback_theme, fallback_theme))
511 g_free (priv->fallback_theme);
512 priv->fallback_theme = fallback_theme;
516 g_free (fallback_theme);
519 do_theme_change (icon_theme);
524 /* Callback when the icon theme GtkSetting changes
527 theme_changed (GtkSettings *settings,
529 GtkIconTheme *icon_theme)
531 update_current_theme (icon_theme);
535 unset_screen (GtkIconTheme *icon_theme)
537 GtkIconThemePrivate *priv = icon_theme->priv;
538 GtkSettings *settings;
543 settings = gtk_settings_get_for_screen (priv->screen);
544 display = gdk_screen_get_display (priv->screen);
546 g_signal_handlers_disconnect_by_func (display,
547 (gpointer) display_closed,
549 g_signal_handlers_disconnect_by_func (settings,
550 (gpointer) theme_changed,
558 * gtk_icon_theme_set_screen:
559 * @icon_theme: a #GtkIconTheme
560 * @screen: a #GdkScreen
562 * Sets the screen for an icon theme; the screen is used
563 * to track the user's currently configured icon theme,
564 * which might be different for different screens.
569 gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
572 GtkIconThemePrivate *priv;
573 GtkSettings *settings;
576 g_return_if_fail (GTK_ICON_THEME (icon_theme));
577 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
579 priv = icon_theme->priv;
581 unset_screen (icon_theme);
585 display = gdk_screen_get_display (screen);
586 settings = gtk_settings_get_for_screen (screen);
588 priv->screen = screen;
590 g_signal_connect (display, "closed",
591 G_CALLBACK (display_closed), icon_theme);
592 g_signal_connect (settings, "notify::gtk-icon-theme-name",
593 G_CALLBACK (theme_changed), icon_theme);
594 g_signal_connect (settings, "notify::gtk-fallback-icon-theme-name",
595 G_CALLBACK (theme_changed), icon_theme);
598 update_current_theme (icon_theme);
601 /* Checks whether a loader for SVG files has been registered
605 pixbuf_supports_svg (void)
609 static gint found_svg = -1;
614 formats = gdk_pixbuf_get_formats ();
617 for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
619 gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
622 for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
624 if (strcmp (*mime_type, "image/svg") == 0)
628 g_strfreev (mime_types);
631 g_slist_free (formats);
637 gtk_icon_theme_init (GtkIconTheme *icon_theme)
639 GtkIconThemePrivate *priv;
640 const gchar * const *xdg_data_dirs;
643 priv = G_TYPE_INSTANCE_GET_PRIVATE (icon_theme,
645 GtkIconThemePrivate);
646 icon_theme->priv = priv;
648 priv->custom_theme = FALSE;
650 xdg_data_dirs = g_get_system_data_dirs ();
651 for (i = 0; xdg_data_dirs[i]; i++) ;
653 priv->search_path_len = 2 * i + 2;
655 priv->search_path = g_new (char *, priv->search_path_len);
658 priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
659 priv->search_path[i++] = g_build_filename (g_get_user_data_dir (), "icons", NULL);
661 for (j = 0; xdg_data_dirs[j]; j++)
662 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
664 for (j = 0; xdg_data_dirs[j]; j++)
665 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "pixmaps", NULL);
667 priv->themes_valid = FALSE;
669 priv->unthemed_icons = NULL;
671 priv->pixbuf_supports_svg = pixbuf_supports_svg ();
675 free_dir_mtime (IconThemeDirMtime *dir_mtime)
677 if (dir_mtime->cache)
678 _gtk_icon_cache_unref (dir_mtime->cache);
680 g_free (dir_mtime->dir);
681 g_slice_free (IconThemeDirMtime, dir_mtime);
686 reset_styles_idle (gpointer user_data)
688 GtkIconTheme *icon_theme;
689 GtkIconThemePrivate *priv;
691 icon_theme = GTK_ICON_THEME (user_data);
692 priv = icon_theme->priv;
694 if (priv->screen && priv->is_screen_singleton)
695 gtk_style_context_reset_widgets (priv->screen);
697 priv->reset_styles_idle = 0;
703 do_theme_change (GtkIconTheme *icon_theme)
705 GtkIconThemePrivate *priv = icon_theme->priv;
707 if (!priv->themes_valid)
711 g_print ("change to icon theme \"%s\"\n", priv->current_theme));
712 blow_themes (icon_theme);
713 g_signal_emit (icon_theme, signal_changed, 0);
715 if (!priv->reset_styles_idle)
716 priv->reset_styles_idle =
717 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2,
718 reset_styles_idle, icon_theme, NULL);
722 blow_themes (GtkIconTheme *icon_theme)
724 GtkIconThemePrivate *priv = icon_theme->priv;
726 if (priv->themes_valid)
728 g_hash_table_destroy (priv->all_icons);
729 g_list_foreach (priv->themes, (GFunc)theme_destroy, NULL);
730 g_list_free (priv->themes);
731 g_list_foreach (priv->dir_mtimes, (GFunc)free_dir_mtime, NULL);
732 g_list_free (priv->dir_mtimes);
733 g_hash_table_destroy (priv->unthemed_icons);
736 priv->unthemed_icons = NULL;
737 priv->dir_mtimes = NULL;
738 priv->all_icons = NULL;
739 priv->themes_valid = FALSE;
743 gtk_icon_theme_finalize (GObject *object)
745 GtkIconTheme *icon_theme;
746 GtkIconThemePrivate *priv;
749 icon_theme = GTK_ICON_THEME (object);
750 priv = icon_theme->priv;
752 if (priv->reset_styles_idle)
754 g_source_remove (priv->reset_styles_idle);
755 priv->reset_styles_idle = 0;
758 unset_screen (icon_theme);
760 g_free (priv->current_theme);
761 priv->current_theme = NULL;
763 for (i = 0; i < priv->search_path_len; i++)
764 g_free (priv->search_path[i]);
766 g_free (priv->search_path);
767 priv->search_path = NULL;
769 blow_themes (icon_theme);
771 G_OBJECT_CLASS (gtk_icon_theme_parent_class)->finalize (object);
775 * gtk_icon_theme_set_search_path:
776 * @icon_theme: a #GtkIconTheme
777 * @path: (array length=n_elements) (element-type filename): array of
778 * directories that are searched for icon themes
779 * @n_elements: number of elements in @path.
781 * Sets the search path for the icon theme object. When looking
782 * for an icon theme, GTK+ will search for a subdirectory of
783 * one or more of the directories in @path with the same name
784 * as the icon theme. (Themes from multiple of the path elements
785 * are combined to allow themes to be extended by adding icons
786 * in the user's home directory.)
788 * In addition if an icon found isn't found either in the current
789 * icon theme or the default icon theme, and an image file with
790 * the right name is found directly in one of the elements of
791 * @path, then that image will be used for the icon name.
792 * (This is legacy feature, and new icons should be put
793 * into the default icon theme, which is called DEFAULT_THEME_NAME,
794 * rather than directly on the icon path.)
799 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
803 GtkIconThemePrivate *priv;
806 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
808 priv = icon_theme->priv;
809 for (i = 0; i < priv->search_path_len; i++)
810 g_free (priv->search_path[i]);
812 g_free (priv->search_path);
814 priv->search_path = g_new (gchar *, n_elements);
815 priv->search_path_len = n_elements;
817 for (i = 0; i < priv->search_path_len; i++)
818 priv->search_path[i] = g_strdup (path[i]);
820 do_theme_change (icon_theme);
825 * gtk_icon_theme_get_search_path:
826 * @icon_theme: a #GtkIconTheme
827 * @path: (allow-none) (array length=n_elements) (element-type filename) (out):
828 * location to store a list of icon theme path directories or %NULL.
829 * The stored value should be freed with g_strfreev().
830 * @n_elements: location to store number of elements in @path, or %NULL
832 * Gets the current search path. See gtk_icon_theme_set_search_path().
837 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
841 GtkIconThemePrivate *priv;
844 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
846 priv = icon_theme->priv;
849 *n_elements = priv->search_path_len;
853 *path = g_new (gchar *, priv->search_path_len + 1);
854 for (i = 0; i < priv->search_path_len; i++)
855 (*path)[i] = g_strdup (priv->search_path[i]);
861 * gtk_icon_theme_append_search_path:
862 * @icon_theme: a #GtkIconTheme
863 * @path: (type filename): directory name to append to the icon path
865 * Appends a directory to the search path.
866 * See gtk_icon_theme_set_search_path().
871 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
874 GtkIconThemePrivate *priv;
876 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
877 g_return_if_fail (path != NULL);
879 priv = icon_theme->priv;
881 priv->search_path_len++;
883 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
884 priv->search_path[priv->search_path_len-1] = g_strdup (path);
886 do_theme_change (icon_theme);
890 * gtk_icon_theme_prepend_search_path:
891 * @icon_theme: a #GtkIconTheme
892 * @path: (type filename): directory name to prepend to the icon path
894 * Prepends a directory to the search path.
895 * See gtk_icon_theme_set_search_path().
900 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
903 GtkIconThemePrivate *priv;
906 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
907 g_return_if_fail (path != NULL);
909 priv = icon_theme->priv;
911 priv->search_path_len++;
912 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
914 for (i = priv->search_path_len - 1; i > 0; i--)
915 priv->search_path[i] = priv->search_path[i - 1];
917 priv->search_path[0] = g_strdup (path);
919 do_theme_change (icon_theme);
923 * gtk_icon_theme_set_custom_theme:
924 * @icon_theme: a #GtkIconTheme
925 * @theme_name: (allow-none): name of icon theme to use instead of
926 * configured theme, or %NULL to unset a previously set custom theme
928 * Sets the name of the icon theme that the #GtkIconTheme object uses
929 * overriding system configuration. This function cannot be called
930 * on the icon theme objects returned from gtk_icon_theme_get_default()
931 * and gtk_icon_theme_get_for_screen().
936 gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
937 const gchar *theme_name)
939 GtkIconThemePrivate *priv;
941 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
943 priv = icon_theme->priv;
945 g_return_if_fail (!priv->is_screen_singleton);
947 if (theme_name != NULL)
949 priv->custom_theme = TRUE;
950 if (!priv->current_theme || strcmp (theme_name, priv->current_theme) != 0)
952 g_free (priv->current_theme);
953 priv->current_theme = g_strdup (theme_name);
955 do_theme_change (icon_theme);
960 if (priv->custom_theme)
962 priv->custom_theme = FALSE;
964 update_current_theme (icon_theme);
970 insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
976 GtkIconThemePrivate *priv;
977 IconTheme *theme = NULL;
979 GKeyFile *theme_file;
980 GError *error = NULL;
981 IconThemeDirMtime *dir_mtime;
984 priv = icon_theme->priv;
986 for (l = priv->themes; l != NULL; l = l->next)
989 if (strcmp (theme->name, theme_name) == 0)
993 for (i = 0; i < priv->search_path_len; i++)
995 path = g_build_filename (priv->search_path[i],
998 dir_mtime = g_slice_new (IconThemeDirMtime);
999 dir_mtime->cache = NULL;
1000 dir_mtime->dir = path;
1001 if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
1002 dir_mtime->mtime = stat_buf.st_mtime;
1004 dir_mtime->mtime = 0;
1006 priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
1008 priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
1011 for (i = 0; i < priv->search_path_len && !theme_file; i++)
1013 path = g_build_filename (priv->search_path[i],
1017 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
1019 theme_file = g_key_file_new ();
1020 g_key_file_set_list_separator (theme_file, ',');
1021 g_key_file_load_from_file (theme_file, path, 0, &error);
1024 g_key_file_free (theme_file);
1026 g_error_free (error);
1033 if (theme_file || strcmp (theme_name, DEFAULT_THEME_NAME) == 0)
1035 theme = g_new0 (IconTheme, 1);
1036 theme->name = g_strdup (theme_name);
1037 priv->themes = g_list_prepend (priv->themes, theme);
1040 if (theme_file == NULL)
1043 theme->display_name =
1044 g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
1045 if (!theme->display_name)
1046 g_warning ("Theme file for %s has no name\n", theme_name);
1048 dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
1051 g_warning ("Theme file for %s has no directories\n", theme_name);
1052 priv->themes = g_list_remove (priv->themes, theme);
1053 g_free (theme->name);
1054 g_free (theme->display_name);
1056 g_key_file_free (theme_file);
1061 g_key_file_get_locale_string (theme_file,
1062 "Icon Theme", "Comment",
1065 g_key_file_get_string (theme_file,
1066 "Icon Theme", "Example",
1070 for (i = 0; dirs[i] != NULL; i++)
1071 theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
1075 theme->dirs = g_list_reverse (theme->dirs);
1077 themes = g_key_file_get_string_list (theme_file,
1084 for (i = 0; themes[i] != NULL; i++)
1085 insert_theme (icon_theme, themes[i]);
1087 g_strfreev (themes);
1090 g_key_file_free (theme_file);
1094 free_unthemed_icon (UnthemedIcon *unthemed_icon)
1096 g_free (unthemed_icon->svg_filename);
1097 g_free (unthemed_icon->no_svg_filename);
1098 g_slice_free (UnthemedIcon, unthemed_icon);
1102 strip_suffix (const char *filename)
1106 dot = strrchr (filename, '.');
1109 return g_strdup (filename);
1111 return g_strndup (filename, dot - filename);
1115 load_themes (GtkIconTheme *icon_theme)
1117 GtkIconThemePrivate *priv;
1122 UnthemedIcon *unthemed_icon;
1123 IconSuffix old_suffix, new_suffix;
1125 IconThemeDirMtime *dir_mtime;
1128 priv = icon_theme->priv;
1130 priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
1132 if (priv->current_theme)
1133 insert_theme (icon_theme, priv->current_theme);
1135 /* Always look in the "default" icon theme, and in a fallback theme */
1136 if (priv->fallback_theme)
1137 insert_theme (icon_theme, priv->fallback_theme);
1138 insert_theme (icon_theme, DEFAULT_THEME_NAME);
1139 priv->themes = g_list_reverse (priv->themes);
1142 priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
1143 g_free, (GDestroyNotify)free_unthemed_icon);
1145 for (base = 0; base < icon_theme->priv->search_path_len; base++)
1147 dir = icon_theme->priv->search_path[base];
1149 dir_mtime = g_slice_new (IconThemeDirMtime);
1150 priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
1152 dir_mtime->dir = g_strdup (dir);
1153 dir_mtime->mtime = 0;
1154 dir_mtime->cache = NULL;
1156 if (g_stat (dir, &stat_buf) != 0 || !S_ISDIR (stat_buf.st_mode))
1158 dir_mtime->mtime = stat_buf.st_mtime;
1160 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir);
1161 if (dir_mtime->cache != NULL)
1164 gdir = g_dir_open (dir, 0, NULL);
1168 while ((file = g_dir_read_name (gdir)))
1170 new_suffix = suffix_from_name (file);
1172 if (new_suffix != ICON_SUFFIX_NONE)
1177 abs_file = g_build_filename (dir, file, NULL);
1178 base_name = strip_suffix (file);
1180 if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
1183 if (new_suffix == ICON_SUFFIX_SVG)
1185 if (unthemed_icon->svg_filename)
1188 unthemed_icon->svg_filename = abs_file;
1192 if (unthemed_icon->no_svg_filename)
1194 old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
1195 if (new_suffix > old_suffix)
1197 g_free (unthemed_icon->no_svg_filename);
1198 unthemed_icon->no_svg_filename = abs_file;
1204 unthemed_icon->no_svg_filename = abs_file;
1211 unthemed_icon = g_slice_new0 (UnthemedIcon);
1213 if (new_suffix == ICON_SUFFIX_SVG)
1214 unthemed_icon->svg_filename = abs_file;
1216 unthemed_icon->no_svg_filename = abs_file;
1218 /* takes ownership of base_name */
1219 g_hash_table_replace (priv->unthemed_icons,
1222 g_hash_table_insert (priv->all_icons,
1230 priv->themes_valid = TRUE;
1232 g_get_current_time(&tv);
1233 priv->last_stat_time = tv.tv_sec;
1237 _gtk_icon_theme_ensure_builtin_cache (void)
1239 static gboolean initialized = FALSE;
1241 static IconThemeDir dirs[5] =
1243 { ICON_THEME_DIR_THRESHOLD, 0, 16, 16, 16, 2, NULL, "16", -1, NULL, NULL, NULL },
1244 { ICON_THEME_DIR_THRESHOLD, 0, 20, 20, 20, 2, NULL, "20", -1, NULL, NULL, NULL },
1245 { ICON_THEME_DIR_THRESHOLD, 0, 24, 24, 24, 2, NULL, "24", -1, NULL, NULL, NULL },
1246 { ICON_THEME_DIR_THRESHOLD, 0, 32, 32, 32, 2, NULL, "32", -1, NULL, NULL, NULL },
1247 { ICON_THEME_DIR_THRESHOLD, 0, 48, 48, 48, 2, NULL, "48", -1, NULL, NULL, NULL }
1255 _builtin_cache = _gtk_icon_cache_new ((gchar *)builtin_icons);
1257 for (i = 0; i < G_N_ELEMENTS (dirs); i++)
1260 dir->cache = _gtk_icon_cache_ref (_builtin_cache);
1261 dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
1263 builtin_dirs = g_list_append (builtin_dirs, dir);
1269 ensure_valid_themes (GtkIconTheme *icon_theme)
1271 GtkIconThemePrivate *priv = icon_theme->priv;
1273 gboolean was_valid = priv->themes_valid;
1275 if (priv->loading_themes)
1277 priv->loading_themes = TRUE;
1279 _gtk_icon_theme_ensure_builtin_cache ();
1281 if (priv->themes_valid)
1283 g_get_current_time (&tv);
1285 if (ABS (tv.tv_sec - priv->last_stat_time) > 5 &&
1286 rescan_themes (icon_theme))
1287 blow_themes (icon_theme);
1290 if (!priv->themes_valid)
1292 load_themes (icon_theme);
1296 g_signal_emit (icon_theme, signal_changed, 0);
1300 priv->loading_themes = FALSE;
1303 static GtkIconInfo *
1304 choose_icon (GtkIconTheme *icon_theme,
1305 const gchar *icon_names[],
1307 GtkIconLookupFlags flags)
1309 GtkIconThemePrivate *priv;
1311 GtkIconInfo *icon_info = NULL;
1312 UnthemedIcon *unthemed_icon = NULL;
1314 gboolean use_builtin;
1317 priv = icon_theme->priv;
1319 if (flags & GTK_ICON_LOOKUP_NO_SVG)
1321 else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
1324 allow_svg = priv->pixbuf_supports_svg;
1326 use_builtin = flags & GTK_ICON_LOOKUP_USE_BUILTIN;
1328 ensure_valid_themes (icon_theme);
1330 for (l = priv->themes; l; l = l->next)
1332 IconTheme *theme = l->data;
1334 for (i = 0; icon_names[i]; i++)
1336 icon_info = theme_lookup_icon (theme, icon_names[i], size, allow_svg, use_builtin);
1342 for (i = 0; icon_names[i]; i++)
1344 unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_names[i]);
1349 /* Still not found an icon, check if reference to a Win32 resource */
1355 resources = g_strsplit (icon_names[0], ",", 0);
1358 wchar_t *wfile = g_utf8_to_utf16 (resources[0], -1, NULL, NULL, NULL);
1359 ExtractIconExW (wfile, resources[1] ? atoi (resources[1]) : 0, &hIcon, NULL, 1);
1365 icon_info = icon_info_new ();
1366 icon_info->cache_pixbuf = gdk_win32_icon_to_pixbuf_libgtk_only (hIcon);
1367 DestroyIcon (hIcon);
1368 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1369 icon_info->dir_size = size;
1371 g_strfreev (resources);
1377 icon_info = icon_info_new ();
1379 /* A SVG icon, when allowed, beats out a XPM icon, but not
1383 unthemed_icon->svg_filename &&
1384 (!unthemed_icon->no_svg_filename ||
1385 suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
1386 icon_info->filename = g_strdup (unthemed_icon->svg_filename);
1387 else if (unthemed_icon->no_svg_filename)
1388 icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);
1390 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1391 icon_info->dir_size = size;
1397 icon_info->desired_size = size;
1398 icon_info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
1402 static gboolean check_for_default_theme = TRUE;
1403 char *default_theme_path;
1404 gboolean found = FALSE;
1407 if (check_for_default_theme)
1409 check_for_default_theme = FALSE;
1411 for (i = 0; !found && i < priv->search_path_len; i++)
1413 default_theme_path = g_build_filename (priv->search_path[i],
1417 found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
1418 g_free (default_theme_path);
1423 g_warning ("Could not find the icon '%s'. The '%s' theme\n"
1424 "was not found either, perhaps you need to install it.\n"
1425 "You can get a copy from:\n"
1427 icon_names[0], DEFAULT_THEME_NAME, "http://icon-theme.freedesktop.org/releases");
1437 * gtk_icon_theme_lookup_icon:
1438 * @icon_theme: a #GtkIconTheme
1439 * @icon_name: the name of the icon to lookup
1440 * @size: desired icon size
1441 * @flags: flags modifying the behavior of the icon lookup
1443 * Looks up a named icon and returns a structure containing
1444 * information such as the filename of the icon. The icon
1445 * can then be rendered into a pixbuf using
1446 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1447 * combines these two steps if all you need is the pixbuf.)
1449 * Return value: a #GtkIconInfo structure containing information
1450 * about the icon, or %NULL if the icon wasn't found. Free with
1451 * gtk_icon_info_free()
1456 gtk_icon_theme_lookup_icon (GtkIconTheme *icon_theme,
1457 const gchar *icon_name,
1459 GtkIconLookupFlags flags)
1463 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1464 g_return_val_if_fail (icon_name != NULL, NULL);
1465 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1466 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1468 GTK_NOTE (ICONTHEME,
1469 g_print ("gtk_icon_theme_lookup_icon %s\n", icon_name));
1471 if (flags & GTK_ICON_LOOKUP_GENERIC_FALLBACK)
1478 for (p = (gchar *) icon_name; *p; p++)
1482 names = g_new (gchar *, dashes + 2);
1483 names[0] = g_strdup (icon_name);
1484 for (i = 1; i <= dashes; i++)
1486 names[i] = g_strdup (names[i - 1]);
1487 p = strrchr (names[i], '-');
1490 names[dashes + 1] = NULL;
1492 info = choose_icon (icon_theme, (const gchar **) names, size, flags);
1498 const gchar *names[2];
1500 names[0] = icon_name;
1503 info = choose_icon (icon_theme, names, size, flags);
1510 * gtk_icon_theme_choose_icon:
1511 * @icon_theme: a #GtkIconTheme
1512 * @icon_names: (array zero-terminated=1): %NULL-terminated array of
1513 * icon names to lookup
1514 * @size: desired icon size
1515 * @flags: flags modifying the behavior of the icon lookup
1517 * Looks up a named icon and returns a structure containing
1518 * information such as the filename of the icon. The icon
1519 * can then be rendered into a pixbuf using
1520 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1521 * combines these two steps if all you need is the pixbuf.)
1523 * If @icon_names contains more than one name, this function
1524 * tries them all in the given order before falling back to
1525 * inherited icon themes.
1527 * Return value: a #GtkIconInfo structure containing information
1528 * about the icon, or %NULL if the icon wasn't found. Free with
1529 * gtk_icon_info_free()
1534 gtk_icon_theme_choose_icon (GtkIconTheme *icon_theme,
1535 const gchar *icon_names[],
1537 GtkIconLookupFlags flags)
1539 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1540 g_return_val_if_fail (icon_names != NULL, NULL);
1541 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1542 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1544 return choose_icon (icon_theme, icon_names, size, flags);
1549 gtk_icon_theme_error_quark (void)
1551 return g_quark_from_static_string ("gtk-icon-theme-error-quark");
1555 * gtk_icon_theme_load_icon:
1556 * @icon_theme: a #GtkIconTheme
1557 * @icon_name: the name of the icon to lookup
1558 * @size: the desired icon size. The resulting icon may not be
1559 * exactly this size; see gtk_icon_info_load_icon().
1560 * @flags: flags modifying the behavior of the icon lookup
1561 * @error: (allow-none): Location to store error information on failure,
1564 * Looks up an icon in an icon theme, scales it to the given size
1565 * and renders it into a pixbuf. This is a convenience function;
1566 * if more details about the icon are needed, use
1567 * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1569 * Note that you probably want to listen for icon theme changes and
1570 * update the icon. This is usually done by connecting to the
1571 * GtkWidget::style-set signal. If for some reason you do not want to
1572 * update the icon when the icon theme changes, you should consider
1573 * using gdk_pixbuf_copy() to make a private copy of the pixbuf
1574 * returned by this function. Otherwise GTK+ may need to keep the old
1575 * icon theme loaded, which would be a waste of memory.
1577 * Return value: (transfer full): the rendered icon; this may be a
1578 * newly created icon or a new reference to an internal icon, so
1579 * you must not modify the icon. Use g_object_unref() to release
1580 * your reference to the icon. %NULL if the icon isn't found.
1585 gtk_icon_theme_load_icon (GtkIconTheme *icon_theme,
1586 const gchar *icon_name,
1588 GtkIconLookupFlags flags,
1591 GtkIconInfo *icon_info;
1592 GdkPixbuf *pixbuf = NULL;
1594 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1595 g_return_val_if_fail (icon_name != NULL, NULL);
1596 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1597 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1598 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1600 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
1601 flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1604 g_set_error (error, GTK_ICON_THEME_ERROR, GTK_ICON_THEME_NOT_FOUND,
1605 _("Icon '%s' not present in theme"), icon_name);
1609 pixbuf = gtk_icon_info_load_icon (icon_info, error);
1610 gtk_icon_info_free (icon_info);
1616 * gtk_icon_theme_has_icon:
1617 * @icon_theme: a #GtkIconTheme
1618 * @icon_name: the name of an icon
1620 * Checks whether an icon theme includes an icon
1621 * for a particular name.
1623 * Return value: %TRUE if @icon_theme includes an
1624 * icon for @icon_name.
1629 gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
1630 const char *icon_name)
1632 GtkIconThemePrivate *priv;
1635 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1636 g_return_val_if_fail (icon_name != NULL, FALSE);
1638 priv = icon_theme->priv;
1640 ensure_valid_themes (icon_theme);
1642 for (l = priv->dir_mtimes; l; l = l->next)
1644 IconThemeDirMtime *dir_mtime = l->data;
1645 GtkIconCache *cache = dir_mtime->cache;
1647 if (cache && _gtk_icon_cache_has_icon (cache, icon_name))
1651 if (g_hash_table_lookup_extended (priv->all_icons,
1652 icon_name, NULL, NULL))
1655 if (_builtin_cache &&
1656 _gtk_icon_cache_has_icon (_builtin_cache, icon_name))
1659 if (icon_theme_builtin_icons &&
1660 g_hash_table_lookup_extended (icon_theme_builtin_icons,
1661 icon_name, NULL, NULL))
1668 add_size (gpointer key,
1672 gint **res_p = user_data;
1674 **res_p = GPOINTER_TO_INT (key);
1680 * gtk_icon_theme_get_icon_sizes:
1681 * @icon_theme: a #GtkIconTheme
1682 * @icon_name: the name of an icon
1684 * Returns an array of integers describing the sizes at which
1685 * the icon is available without scaling. A size of -1 means
1686 * that the icon is available in a scalable format. The array
1687 * is zero-terminated.
1689 * Return value: (array zero-terminated=1): An newly allocated array
1690 * describing the sizes at which the icon is available. The array
1691 * should be freed with g_free() when it is no longer needed.
1696 gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
1697 const char *icon_name)
1699 GList *l, *d, *icons;
1703 GtkIconThemePrivate *priv;
1705 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1707 priv = icon_theme->priv;
1709 ensure_valid_themes (icon_theme);
1711 sizes = g_hash_table_new (g_direct_hash, g_direct_equal);
1713 for (l = priv->themes; l; l = l->next)
1715 IconTheme *theme = l->data;
1716 for (d = theme->dirs; d; d = d->next)
1718 IconThemeDir *dir = d->data;
1720 if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
1723 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1724 if (suffix != ICON_SUFFIX_NONE)
1726 if (suffix == ICON_SUFFIX_SVG)
1727 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1729 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1734 for (d = builtin_dirs; d; d = d->next)
1736 IconThemeDir *dir = d->data;
1738 if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
1741 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1742 if (suffix != ICON_SUFFIX_NONE)
1744 if (suffix == ICON_SUFFIX_SVG)
1745 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1747 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1751 if (icon_theme_builtin_icons)
1753 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
1757 BuiltinIcon *icon = icons->data;
1759 g_hash_table_insert (sizes, GINT_TO_POINTER (icon->size), NULL);
1760 icons = icons->next;
1764 r = result = g_new0 (gint, g_hash_table_size (sizes) + 1);
1766 g_hash_table_foreach (sizes, add_size, &r);
1767 g_hash_table_destroy (sizes);
1773 add_key_to_hash (gpointer key,
1777 GHashTable *hash = user_data;
1779 g_hash_table_insert (hash, key, NULL);
1783 add_key_to_list (gpointer key,
1787 GList **list = user_data;
1789 *list = g_list_prepend (*list, g_strdup (key));
1793 * gtk_icon_theme_list_icons:
1794 * @icon_theme: a #GtkIconTheme
1795 * @context: (allow-none): a string identifying a particular type of
1796 * icon, or %NULL to list all icons.
1798 * Lists the icons in the current icon theme. Only a subset
1799 * of the icons can be listed by providing a context string.
1800 * The set of values for the context string is system dependent,
1801 * but will typically include such values as "Applications" and
1804 * Return value: (element-type utf8) (transfer full): a #GList list
1805 * holding the names of all the icons in the theme. You must first
1806 * free each element in the list with g_free(), then free the list
1807 * itself with g_list_free().
1812 gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
1813 const char *context)
1815 GtkIconThemePrivate *priv;
1818 GQuark context_quark;
1820 priv = icon_theme->priv;
1822 ensure_valid_themes (icon_theme);
1826 context_quark = g_quark_try_string (context);
1834 icons = g_hash_table_new (g_str_hash, g_str_equal);
1839 theme_list_icons (l->data, icons, context_quark);
1843 if (context_quark == 0)
1844 g_hash_table_foreach (priv->unthemed_icons,
1850 g_hash_table_foreach (icons,
1854 g_hash_table_destroy (icons);
1860 * gtk_icon_theme_list_contexts:
1861 * @icon_theme: a #GtkIconTheme
1863 * Gets the list of contexts available within the current
1864 * hierarchy of icon themes
1866 * Return value: (element-type utf8) (transfer full): a #GList list
1867 * holding the names of all the contexts in the theme. You must first
1868 * free each element in the list with g_free(), then free the list
1869 * itself with g_list_free().
1874 gtk_icon_theme_list_contexts (GtkIconTheme *icon_theme)
1876 GtkIconThemePrivate *priv;
1877 GHashTable *contexts;
1880 priv = icon_theme->priv;
1882 ensure_valid_themes (icon_theme);
1884 contexts = g_hash_table_new (g_str_hash, g_str_equal);
1889 theme_list_contexts (l->data, contexts);
1895 g_hash_table_foreach (contexts,
1899 g_hash_table_destroy (contexts);
1905 * gtk_icon_theme_get_example_icon_name:
1906 * @icon_theme: a #GtkIconTheme
1908 * Gets the name of an icon that is representative of the
1909 * current theme (for instance, to use when presenting
1910 * a list of themes to the user.)
1912 * Return value: the name of an example icon or %NULL.
1913 * Free with g_free().
1918 gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
1920 GtkIconThemePrivate *priv;
1924 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1926 priv = icon_theme->priv;
1928 ensure_valid_themes (icon_theme);
1935 return g_strdup (theme->example);
1945 rescan_themes (GtkIconTheme *icon_theme)
1947 GtkIconThemePrivate *priv;
1948 IconThemeDirMtime *dir_mtime;
1954 priv = icon_theme->priv;
1956 for (d = priv->dir_mtimes; d != NULL; d = d->next)
1958 dir_mtime = d->data;
1960 stat_res = g_stat (dir_mtime->dir, &stat_buf);
1962 /* dir mtime didn't change */
1963 if (stat_res == 0 &&
1964 S_ISDIR (stat_buf.st_mode) &&
1965 dir_mtime->mtime == stat_buf.st_mtime)
1967 /* didn't exist before, and still doesn't */
1968 if (dir_mtime->mtime == 0 &&
1969 (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
1975 g_get_current_time (&tv);
1976 priv->last_stat_time = tv.tv_sec;
1982 * gtk_icon_theme_rescan_if_needed:
1983 * @icon_theme: a #GtkIconTheme
1985 * Checks to see if the icon theme has changed; if it has, any
1986 * currently cached information is discarded and will be reloaded
1987 * next time @icon_theme is accessed.
1989 * Return value: %TRUE if the icon theme has changed and needed
1995 gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
1999 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
2001 retval = rescan_themes (icon_theme);
2003 do_theme_change (icon_theme);
2009 theme_destroy (IconTheme *theme)
2011 g_free (theme->display_name);
2012 g_free (theme->comment);
2013 g_free (theme->name);
2014 g_free (theme->example);
2016 g_list_foreach (theme->dirs, (GFunc)theme_dir_destroy, NULL);
2017 g_list_free (theme->dirs);
2023 theme_dir_destroy (IconThemeDir *dir)
2026 _gtk_icon_cache_unref (dir->cache);
2028 g_hash_table_destroy (dir->icons);
2031 g_hash_table_destroy (dir->icon_data);
2033 g_free (dir->subdir);
2038 theme_dir_size_difference (IconThemeDir *dir, int size, gboolean *smaller)
2043 case ICON_THEME_DIR_FIXED:
2044 *smaller = size < dir->size;
2045 return abs (size - dir->size);
2047 case ICON_THEME_DIR_SCALABLE:
2048 *smaller = size < dir->min_size;
2049 if (size < dir->min_size)
2050 return dir->min_size - size;
2051 if (size > dir->max_size)
2052 return size - dir->max_size;
2055 case ICON_THEME_DIR_THRESHOLD:
2056 min = dir->size - dir->threshold;
2057 max = dir->size + dir->threshold;
2058 *smaller = size < min;
2065 case ICON_THEME_DIR_UNTHEMED:
2066 g_assert_not_reached ();
2069 g_assert_not_reached ();
2074 string_from_suffix (IconSuffix suffix)
2078 case ICON_SUFFIX_XPM:
2080 case ICON_SUFFIX_SVG:
2082 case ICON_SUFFIX_PNG:
2085 g_assert_not_reached();
2091 suffix_from_name (const char *name)
2095 if (g_str_has_suffix (name, ".png"))
2096 retval = ICON_SUFFIX_PNG;
2097 else if (g_str_has_suffix (name, ".svg"))
2098 retval = ICON_SUFFIX_SVG;
2099 else if (g_str_has_suffix (name, ".xpm"))
2100 retval = ICON_SUFFIX_XPM;
2102 retval = ICON_SUFFIX_NONE;
2108 best_suffix (IconSuffix suffix,
2111 if ((suffix & ICON_SUFFIX_PNG) != 0)
2112 return ICON_SUFFIX_PNG;
2113 else if (allow_svg && ((suffix & ICON_SUFFIX_SVG) != 0))
2114 return ICON_SUFFIX_SVG;
2115 else if ((suffix & ICON_SUFFIX_XPM) != 0)
2116 return ICON_SUFFIX_XPM;
2118 return ICON_SUFFIX_NONE;
2123 theme_dir_get_icon_suffix (IconThemeDir *dir,
2124 const gchar *icon_name,
2125 gboolean *has_icon_file)
2131 suffix = (IconSuffix)_gtk_icon_cache_get_icon_flags (dir->cache,
2136 *has_icon_file = suffix & HAS_ICON_FILE;
2138 suffix = suffix & ~HAS_ICON_FILE;
2141 suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
2143 GTK_NOTE (ICONTHEME,
2144 g_print ("get_icon_suffix%s %u\n", dir->cache ? " (cached)" : "", suffix));
2149 static GtkIconInfo *
2150 theme_lookup_icon (IconTheme *theme,
2151 const char *icon_name,
2154 gboolean use_builtin)
2157 IconThemeDir *dir, *min_dir;
2159 int min_difference, difference;
2160 BuiltinIcon *closest_builtin = NULL;
2161 gboolean smaller, has_larger, match;
2164 min_difference = G_MAXINT;
2169 /* Builtin icons are logically part of the default theme and
2170 * are searched before other subdirectories of the default theme.
2172 if (use_builtin && strcmp (theme->name, DEFAULT_THEME_NAME) == 0)
2174 closest_builtin = find_builtin_icon (icon_name,
2179 if (min_difference == 0)
2180 return icon_info_new_builtin (closest_builtin);
2182 dirs = builtin_dirs;
2192 GTK_NOTE (ICONTHEME,
2193 g_print ("theme_lookup_icon dir %s\n", dir->dir));
2194 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
2195 if (best_suffix (suffix, allow_svg) != ICON_SUFFIX_NONE)
2197 difference = theme_dir_size_difference (dir, size, &smaller);
2199 if (difference == 0)
2201 if (dir->type == ICON_THEME_DIR_SCALABLE)
2203 /* don't pick scalable if we already found
2204 * a matching non-scalable dir
2214 /* for a matching non-scalable dir keep
2215 * going and look for a closer match
2217 difference = abs (size - dir->size);
2218 if (!match || difference < min_difference)
2221 min_difference = difference;
2224 if (difference == 0)
2233 if (difference < min_difference || smaller)
2235 min_difference = difference;
2237 has_larger = smaller;
2242 if (difference < min_difference && smaller)
2244 min_difference = difference;
2253 if (l == NULL && dirs == builtin_dirs)
2262 GtkIconInfo *icon_info = icon_info_new ();
2263 gboolean has_icon_file = FALSE;
2265 suffix = theme_dir_get_icon_suffix (min_dir, icon_name, &has_icon_file);
2266 suffix = best_suffix (suffix, allow_svg);
2267 g_assert (suffix != ICON_SUFFIX_NONE);
2271 file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
2272 icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
2277 icon_info->filename = NULL;
2280 if (min_dir->icon_data != NULL)
2281 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2283 if (icon_info->data == NULL && min_dir->cache != NULL)
2285 icon_info->data = _gtk_icon_cache_get_icon_data (min_dir->cache, icon_name, min_dir->subdir_index);
2286 if (icon_info->data)
2288 if (min_dir->icon_data == NULL)
2289 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2290 g_free, (GDestroyNotify)icon_data_free);
2292 g_hash_table_replace (min_dir->icon_data, g_strdup (icon_name), icon_info->data);
2296 if (icon_info->data == NULL && has_icon_file)
2298 gchar *icon_file_name, *icon_file_path;
2300 icon_file_name = g_strconcat (icon_name, ".icon", NULL);
2301 icon_file_path = g_build_filename (min_dir->dir, icon_file_name, NULL);
2303 if (g_file_test (icon_file_path, G_FILE_TEST_IS_REGULAR))
2305 if (min_dir->icon_data == NULL)
2306 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2307 g_free, (GDestroyNotify)icon_data_free);
2308 load_icon_data (min_dir, icon_file_path, icon_file_name);
2310 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2312 g_free (icon_file_name);
2313 g_free (icon_file_path);
2318 icon_info->cache_pixbuf = _gtk_icon_cache_get_icon (min_dir->cache, icon_name,
2319 min_dir->subdir_index);
2322 icon_info->dir_type = min_dir->type;
2323 icon_info->dir_size = min_dir->size;
2324 icon_info->threshold = min_dir->threshold;
2329 if (closest_builtin)
2330 return icon_info_new_builtin (closest_builtin);
2336 theme_list_icons (IconTheme *theme,
2340 GList *l = theme->dirs;
2347 if (context == dir->context ||
2352 _gtk_icon_cache_add_icons (dir->cache,
2359 g_hash_table_foreach (dir->icons,
2369 theme_list_contexts (IconTheme *theme,
2370 GHashTable *contexts)
2372 GList *l = theme->dirs;
2374 const char *context;
2380 context = g_quark_to_string (dir->context);
2381 g_hash_table_replace (contexts, (gpointer) context, NULL);
2388 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
2390 GKeyFile *icon_file;
2398 GError *error = NULL;
2402 icon_file = g_key_file_new ();
2403 g_key_file_set_list_separator (icon_file, ',');
2404 g_key_file_load_from_file (icon_file, path, 0, &error);
2407 g_error_free (error);
2408 g_key_file_free (icon_file);
2413 base_name = strip_suffix (name);
2415 data = g_slice_new0 (GtkIconData);
2416 /* takes ownership of base_name */
2417 g_hash_table_replace (dir->icon_data, base_name, data);
2419 ivalues = g_key_file_get_integer_list (icon_file,
2420 "Icon Data", "EmbeddedTextRectangle",
2426 data->has_embedded_rect = TRUE;
2427 data->x0 = ivalues[0];
2428 data->y0 = ivalues[1];
2429 data->x1 = ivalues[2];
2430 data->y1 = ivalues[3];
2436 str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
2439 split = g_strsplit (str, "|", -1);
2441 data->n_attach_points = g_strv_length (split);
2442 data->attach_points = g_new (GdkPoint, data->n_attach_points);
2445 while (split[i] != NULL && i < data->n_attach_points)
2447 split_point = strchr (split[i], ',');
2452 data->attach_points[i].x = atoi (split[i]);
2453 data->attach_points[i].y = atoi (split_point);
2462 data->display_name = g_key_file_get_locale_string (icon_file,
2463 "Icon Data", "DisplayName",
2465 g_key_file_free (icon_file);
2470 scan_directory (GtkIconThemePrivate *icon_theme,
2471 IconThemeDir *dir, char *full_dir)
2476 GTK_NOTE (ICONTHEME,
2477 g_print ("scanning directory %s\n", full_dir));
2478 dir->icons = g_hash_table_new_full (g_str_hash, g_str_equal,
2481 gdir = g_dir_open (full_dir, 0, NULL);
2486 while ((name = g_dir_read_name (gdir)))
2490 IconSuffix suffix, hash_suffix;
2492 if (g_str_has_suffix (name, ".icon"))
2494 if (dir->icon_data == NULL)
2495 dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2496 g_free, (GDestroyNotify)icon_data_free);
2498 path = g_build_filename (full_dir, name, NULL);
2499 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
2500 load_icon_data (dir, path, name);
2507 suffix = suffix_from_name (name);
2508 if (suffix == ICON_SUFFIX_NONE)
2511 base_name = strip_suffix (name);
2513 hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
2514 g_hash_table_replace (icon_theme->all_icons, base_name, NULL);
2515 /* takes ownership of base_name */
2516 g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
2523 theme_subdir_load (GtkIconTheme *icon_theme,
2525 GKeyFile *theme_file,
2531 IconThemeDirType type;
2532 char *context_string;
2539 GError *error = NULL;
2540 IconThemeDirMtime *dir_mtime;
2542 size = g_key_file_get_integer (theme_file, subdir, "Size", &error);
2545 g_error_free (error);
2546 g_warning ("Theme directory %s of theme %s has no size field\n",
2547 subdir, theme->name);
2551 type = ICON_THEME_DIR_THRESHOLD;
2552 type_string = g_key_file_get_string (theme_file, subdir, "Type", NULL);
2555 if (strcmp (type_string, "Fixed") == 0)
2556 type = ICON_THEME_DIR_FIXED;
2557 else if (strcmp (type_string, "Scalable") == 0)
2558 type = ICON_THEME_DIR_SCALABLE;
2559 else if (strcmp (type_string, "Threshold") == 0)
2560 type = ICON_THEME_DIR_THRESHOLD;
2562 g_free (type_string);
2566 context_string = g_key_file_get_string (theme_file, subdir, "Context", NULL);
2569 context = g_quark_from_string (context_string);
2570 g_free (context_string);
2573 if (g_key_file_has_key (theme_file, subdir, "MaxSize", NULL))
2574 max_size = g_key_file_get_integer (theme_file, subdir, "MaxSize", NULL);
2578 if (g_key_file_has_key (theme_file, subdir, "MinSize", NULL))
2579 min_size = g_key_file_get_integer (theme_file, subdir, "MinSize", NULL);
2583 if (g_key_file_has_key (theme_file, subdir, "Threshold", NULL))
2584 threshold = g_key_file_get_integer (theme_file, subdir, "Threshold", NULL);
2588 for (d = icon_theme->priv->dir_mtimes; d; d = d->next)
2590 dir_mtime = (IconThemeDirMtime *)d->data;
2592 if (dir_mtime->mtime == 0)
2593 continue; /* directory doesn't exist */
2595 full_dir = g_build_filename (dir_mtime->dir, subdir, NULL);
2597 /* First, see if we have a cache for the directory */
2598 if (dir_mtime->cache != NULL || g_file_test (full_dir, G_FILE_TEST_IS_DIR))
2600 if (dir_mtime->cache == NULL)
2602 /* This will return NULL if the cache doesn't exist or is outdated */
2603 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir_mtime->dir);
2606 dir = g_new (IconThemeDir, 1);
2608 dir->context = context;
2610 dir->min_size = min_size;
2611 dir->max_size = max_size;
2612 dir->threshold = threshold;
2613 dir->dir = full_dir;
2614 dir->icon_data = NULL;
2615 dir->subdir = g_strdup (subdir);
2616 if (dir_mtime->cache != NULL)
2618 dir->cache = _gtk_icon_cache_ref (dir_mtime->cache);
2619 dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
2624 dir->subdir_index = -1;
2625 scan_directory (icon_theme->priv, dir, full_dir);
2628 theme->dirs = g_list_prepend (theme->dirs, dir);
2636 icon_data_free (GtkIconData *icon_data)
2638 g_free (icon_data->attach_points);
2639 g_free (icon_data->display_name);
2640 g_slice_free (GtkIconData, icon_data);
2647 G_DEFINE_BOXED_TYPE (GtkIconInfo, gtk_icon_info,
2651 static GtkIconInfo *
2652 icon_info_new (void)
2654 GtkIconInfo *icon_info = g_slice_new0 (GtkIconInfo);
2656 icon_info->scale = -1.;
2657 icon_info->ref_count = 1;
2662 static GtkIconInfo *
2663 icon_info_new_builtin (BuiltinIcon *icon)
2665 GtkIconInfo *icon_info = icon_info_new ();
2667 icon_info->cache_pixbuf = g_object_ref (icon->pixbuf);
2668 icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
2669 icon_info->dir_size = icon->size;
2670 icon_info->threshold = 2;
2676 * gtk_icon_info_copy:
2677 * @icon_info: a #GtkIconInfo
2679 * Make a copy of a #GtkIconInfo.
2681 * Return value: the new GtkIconInfo
2686 gtk_icon_info_copy (GtkIconInfo *icon_info)
2689 g_return_val_if_fail (icon_info != NULL, NULL);
2691 icon_info->ref_count++;
2697 * gtk_icon_info_free:
2698 * @icon_info: a #GtkIconInfo
2700 * Free a #GtkIconInfo and associated information
2705 gtk_icon_info_free (GtkIconInfo *icon_info)
2707 g_return_if_fail (icon_info != NULL);
2709 icon_info->ref_count--;
2710 if (icon_info->ref_count > 0)
2713 g_free (icon_info->filename);
2714 if (icon_info->loadable)
2715 g_object_unref (icon_info->loadable);
2716 g_slist_foreach (icon_info->emblem_infos, (GFunc)gtk_icon_info_free, NULL);
2717 g_slist_free (icon_info->emblem_infos);
2718 if (icon_info->pixbuf)
2719 g_object_unref (icon_info->pixbuf);
2720 if (icon_info->cache_pixbuf)
2721 g_object_unref (icon_info->cache_pixbuf);
2723 g_slice_free (GtkIconInfo, icon_info);
2727 * gtk_icon_info_get_base_size:
2728 * @icon_info: a #GtkIconInfo
2730 * Gets the base size for the icon. The base size
2731 * is a size for the icon that was specified by
2732 * the icon theme creator. This may be different
2733 * than the actual size of image; an example of
2734 * this is small emblem icons that can be attached
2735 * to a larger icon. These icons will be given
2736 * the same base size as the larger icons to which
2737 * they are attached.
2739 * Return value: the base size, or 0, if no base
2740 * size is known for the icon.
2745 gtk_icon_info_get_base_size (GtkIconInfo *icon_info)
2747 g_return_val_if_fail (icon_info != NULL, 0);
2749 return icon_info->dir_size;
2753 * gtk_icon_info_get_filename:
2754 * @icon_info: a #GtkIconInfo
2756 * Gets the filename for the icon. If the
2757 * %GTK_ICON_LOOKUP_USE_BUILTIN flag was passed
2758 * to gtk_icon_theme_lookup_icon(), there may be
2759 * no filename if a builtin icon is returned; in this
2760 * case, you should use gtk_icon_info_get_builtin_pixbuf().
2762 * Return value: (type filename): the filename for the icon, or %NULL
2763 * if gtk_icon_info_get_builtin_pixbuf() should be used instead. The
2764 * return value is owned by GTK+ and should not be modified or freed.
2769 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2771 g_return_val_if_fail (icon_info != NULL, NULL);
2773 return icon_info->filename;
2777 * gtk_icon_info_get_builtin_pixbuf:
2778 * @icon_info: a #GtkIconInfo structure
2780 * Gets the built-in image for this icon, if any. To allow
2781 * GTK+ to use built in icon images, you must pass the
2782 * %GTK_ICON_LOOKUP_USE_BUILTIN to
2783 * gtk_icon_theme_lookup_icon().
2785 * Return value: (transfer none): the built-in image pixbuf, or %NULL. No
2786 * extra reference is added to the returned pixbuf, so if
2787 * you want to keep it around, you must use g_object_ref().
2788 * The returned image must not be modified.
2793 gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
2795 g_return_val_if_fail (icon_info != NULL, NULL);
2797 if (icon_info->filename)
2800 return icon_info->cache_pixbuf;
2803 static gboolean icon_info_ensure_scale_and_pixbuf (GtkIconInfo*, gboolean);
2805 /* Combine the icon with all emblems, the first emblem is placed
2806 * in the southeast corner. Scale emblems to be at most 3/4 of the
2807 * size of the icon itself.
2810 apply_emblems (GtkIconInfo *info)
2812 GdkPixbuf *icon = NULL;
2816 if (info->emblem_infos == NULL)
2819 if (info->emblems_applied)
2822 w = gdk_pixbuf_get_width (info->pixbuf);
2823 h = gdk_pixbuf_get_height (info->pixbuf);
2825 for (l = info->emblem_infos, pos = 0; l; l = l->next, pos++)
2827 GtkIconInfo *emblem_info = l->data;
2829 if (icon_info_ensure_scale_and_pixbuf (emblem_info, FALSE))
2831 GdkPixbuf *emblem = emblem_info->pixbuf;
2833 gint x = 0, y = 0; /* silence compiler */
2836 ew = gdk_pixbuf_get_width (emblem);
2837 eh = gdk_pixbuf_get_height (emblem);
2869 icon = gdk_pixbuf_copy (info->pixbuf);
2874 gdk_pixbuf_composite (emblem, icon, x, y, ew, eh, x, y,
2875 scale, scale, GDK_INTERP_BILINEAR, 255);
2881 g_object_unref (info->pixbuf);
2882 info->pixbuf = icon;
2885 info->emblems_applied = TRUE;
2888 /* This function contains the complicated logic for deciding
2889 * on the size at which to load the icon and loading it at
2893 icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
2894 gboolean scale_only)
2896 int image_width, image_height;
2897 GdkPixbuf *source_pixbuf;
2900 /* First check if we already succeeded have the necessary
2901 * information (or failed earlier)
2903 if (scale_only && icon_info->scale >= 0)
2906 if (icon_info->pixbuf)
2908 apply_emblems (icon_info);
2912 if (icon_info->load_error)
2915 /* SVG icons are a special case - we just immediately scale them
2916 * to the desired size
2918 if (icon_info->filename && !icon_info->loadable)
2922 file = g_file_new_for_path (icon_info->filename);
2923 icon_info->loadable = G_LOADABLE_ICON (g_file_icon_new (file));
2924 g_object_unref (file);
2928 if (G_IS_FILE_ICON (icon_info->loadable))
2931 GFileInfo *file_info;
2932 const gchar *content_type;
2934 file = g_file_icon_get_file (G_FILE_ICON (icon_info->loadable));
2935 file_info = g_file_query_info (file,
2936 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
2937 G_FILE_QUERY_INFO_NONE,
2941 content_type = g_file_info_get_content_type (file_info);
2943 if (content_type && strcmp (content_type, "image/svg+xml") == 0)
2946 g_object_unref (file_info);
2952 GInputStream *stream;
2954 icon_info->scale = icon_info->desired_size / 1000.;
2959 stream = g_loadable_icon_load (icon_info->loadable,
2960 icon_info->desired_size,
2962 &icon_info->load_error);
2965 icon_info->pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
2966 icon_info->desired_size,
2967 icon_info->desired_size,
2970 &icon_info->load_error);
2971 g_object_unref (stream);
2974 if (!icon_info->pixbuf)
2977 apply_emblems (icon_info);
2982 /* In many cases, the scale can be determined without actual access
2983 * to the icon file. This is generally true when we have a size
2984 * for the directory where the icon is; the image size doesn't
2985 * matter in that case.
2987 if (icon_info->forced_size)
2988 icon_info->scale = -1;
2989 else if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
2990 icon_info->scale = 1.0;
2991 else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
2993 if (icon_info->desired_size >= icon_info->dir_size - icon_info->threshold &&
2994 icon_info->desired_size <= icon_info->dir_size + icon_info->threshold)
2995 icon_info->scale = 1.0;
2996 else if (icon_info->dir_size > 0)
2997 icon_info->scale =(gdouble) icon_info->desired_size / icon_info->dir_size;
2999 else if (icon_info->dir_type == ICON_THEME_DIR_SCALABLE)
3001 if (icon_info->dir_size > 0)
3002 icon_info->scale = (gdouble) icon_info->desired_size / icon_info->dir_size;
3005 if (icon_info->scale >= 0. && scale_only)
3008 /* At this point, we need to actually get the icon; either from the
3009 * builtin image or by loading the file
3011 source_pixbuf = NULL;
3012 if (icon_info->cache_pixbuf)
3013 source_pixbuf = g_object_ref (icon_info->cache_pixbuf);
3016 GInputStream *stream;
3018 stream = g_loadable_icon_load (icon_info->loadable,
3019 icon_info->desired_size,
3021 &icon_info->load_error);
3024 source_pixbuf = gdk_pixbuf_new_from_stream (stream,
3026 &icon_info->load_error);
3027 g_object_unref (stream);
3034 /* Do scale calculations that depend on the image size
3036 image_width = gdk_pixbuf_get_width (source_pixbuf);
3037 image_height = gdk_pixbuf_get_height (source_pixbuf);
3039 if (icon_info->scale < 0.0)
3041 gint image_size = MAX (image_width, image_height);
3043 icon_info->scale = (gdouble)icon_info->desired_size / (gdouble)image_size;
3045 icon_info->scale = 1.0;
3047 if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED &&
3048 !icon_info->forced_size)
3049 icon_info->scale = MIN (icon_info->scale, 1.0);
3052 /* We don't short-circuit out here for scale_only, since, now
3053 * we've loaded the icon, we might as well go ahead and finish
3054 * the job. This is a bit of a waste when we scale here
3055 * and never get the final pixbuf; at the cost of a bit of
3056 * extra complexity, we could keep the source pixbuf around
3057 * but not actually scale it until needed.
3059 if (icon_info->scale == 1.0)
3060 icon_info->pixbuf = source_pixbuf;
3063 icon_info->pixbuf = gdk_pixbuf_scale_simple (source_pixbuf,
3064 0.5 + image_width * icon_info->scale,
3065 0.5 + image_height * icon_info->scale,
3066 GDK_INTERP_BILINEAR);
3067 g_object_unref (source_pixbuf);
3070 apply_emblems (icon_info);
3076 * gtk_icon_info_load_icon:
3077 * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
3078 * @error: (allow-none): location to store error information on failure,
3081 * Renders an icon previously looked up in an icon theme using
3082 * gtk_icon_theme_lookup_icon(); the size will be based on the size
3083 * passed to gtk_icon_theme_lookup_icon(). Note that the resulting
3084 * pixbuf may not be exactly this size; an icon theme may have icons
3085 * that differ slightly from their nominal sizes, and in addition GTK+
3086 * will avoid scaling icons that it considers sufficiently close to the
3087 * requested size or for which the source image would have to be scaled
3088 * up too far. (This maintains sharpness.). This behaviour can be changed
3089 * by passing the %GTK_ICON_LOOKUP_FORCE_SIZE flag when obtaining
3090 * the #GtkIconInfo. If this flag has been specified, the pixbuf
3091 * returned by this function will be scaled to the exact size.
3093 * Return value: (transfer full): the rendered icon; this may be a newly
3094 * created icon or a new reference to an internal icon, so you must
3095 * not modify the icon. Use g_object_unref() to release your reference
3101 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
3104 g_return_val_if_fail (icon_info != NULL, NULL);
3105 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
3107 if (!icon_info_ensure_scale_and_pixbuf (icon_info, FALSE))
3109 if (icon_info->load_error)
3110 g_propagate_error (error, icon_info->load_error);
3112 g_set_error_literal (error,
3113 GTK_ICON_THEME_ERROR,
3114 GTK_ICON_THEME_NOT_FOUND,
3115 _("Failed to load icon"));
3120 return g_object_ref (icon_info->pixbuf);
3124 gdk_color_to_css (GdkColor *color)
3126 return g_strdup_printf ("rgb(%d,%d,%d)",
3133 gdk_rgba_to_css (const GdkRGBA *color)
3135 /* drop alpha for now, since librsvg does not understand rgba() */
3136 return g_strdup_printf ("rgb(%d,%d,%d)",
3137 (gint)(color->red * 255),
3138 (gint)(color->green * 255),
3139 (gint)(color->blue * 255));
3143 _gtk_icon_info_load_symbolic_internal (GtkIconInfo *icon_info,
3144 const gchar *css_fg,
3145 const gchar *css_success,
3146 const gchar *css_warning,
3147 const gchar *css_error,
3150 GInputStream *stream;
3153 gchar *success, *warning, *err;
3155 /* css_fg can't possibly have failed, otherwise
3156 * that would mean we have a broken style */
3157 g_return_val_if_fail (css_fg != NULL, NULL);
3159 success = warning = err = NULL;
3163 GdkColor success_default_color = { 0, 0x4e00, 0x9a00, 0x0600 };
3164 success = gdk_color_to_css (&success_default_color);
3168 GdkColor warning_default_color = { 0, 0xf500, 0x7900, 0x3e00 };
3169 warning = gdk_color_to_css (&warning_default_color);
3173 GdkColor error_default_color = { 0, 0xcc00, 0x0000, 0x0000 };
3174 err = gdk_color_to_css (&error_default_color);
3178 data = g_strconcat ("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
3179 "<svg version=\"1.1\"\n"
3180 " xmlns=\"http://www.w3.org/2000/svg\"\n"
3181 " xmlns:xi=\"http://www.w3.org/2001/XInclude\"\n"
3184 " <style type=\"text/css\">\n"
3186 " fill: ", css_fg," !important;\n"
3189 " fill: ", css_warning ? css_warning : warning," !important;\n"
3192 " fill: ", css_error ? css_error : err," !important;\n"
3195 " fill: ", css_success ? css_success : success," !important;\n"
3198 " <xi:include href=\"", icon_info->filename, "\"/>\n"
3205 stream = g_memory_input_stream_new_from_data (data, -1, g_free);
3206 pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
3207 icon_info->desired_size,
3208 icon_info->desired_size,
3212 g_object_unref (stream);
3218 * gtk_icon_info_load_symbolic:
3219 * @icon_info: a #GtkIconInfo
3220 * @fg: a #GdkRGBA representing the foreground color of the icon
3221 * @success_color: (allow-none): a #GdkRGBA representing the warning color
3222 * of the icon or %NULL to use the default color
3223 * @warning_color: (allow-none): a #GdkRGBA representing the warning color
3224 * of the icon or %NULL to use the default color
3225 * @error_color: (allow-none): a #GdkRGBA representing the error color
3226 * of the icon or %NULL to use the default color (allow-none)
3227 * @was_symbolic: (out) (allow-none): a #gboolean, returns whether the
3228 * loaded icon was a symbolic one and whether the @fg color was
3230 * @error: (allow-none): location to store error information on failure,
3233 * Loads an icon, modifying it to match the system colours for the foreground,
3234 * success, warning and error colors provided. If the icon is not a symbolic
3235 * one, the function will return the result from gtk_icon_info_load_icon().
3237 * This allows loading symbolic icons that will match the system theme.
3239 * Unless you are implementing a widget, you will want to use
3240 * g_themed_icon_new_with_default_fallbacks() to load the icon.
3242 * As implementation details, the icon loaded needs to be of SVG type,
3243 * contain the "symbolic" term as the last component of the icon name,
3244 * and use the 'fg', 'success', 'warning' and 'error' CSS styles in the
3247 * See the <ulink url="http://www.freedesktop.org/wiki/SymbolicIcons">Symbolic Icons spec</ulink>
3248 * for more information about symbolic icons.
3250 * Return value: (transfer full): a #GdkPixbuf representing the loaded icon
3255 gtk_icon_info_load_symbolic (GtkIconInfo *icon_info,
3257 const GdkRGBA *success_color,
3258 const GdkRGBA *warning_color,
3259 const GdkRGBA *error_color,
3260 gboolean *was_symbolic,
3269 g_return_val_if_fail (fg != NULL, NULL);
3271 if (!icon_info->filename ||
3272 !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3275 *was_symbolic = FALSE;
3276 return gtk_icon_info_load_icon (icon_info, error);
3280 *was_symbolic = TRUE;
3282 css_fg = gdk_rgba_to_css (fg);
3284 css_success = css_warning = css_error = NULL;
3287 css_warning = gdk_rgba_to_css (warning_color);
3290 css_error = gdk_rgba_to_css (error_color);
3293 css_success = gdk_rgba_to_css (success_color);
3295 pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
3296 css_fg, css_success,
3297 css_warning, css_error,
3300 g_free (css_warning);
3301 g_free (css_success);
3308 * gtk_icon_info_load_symbolic_for_context:
3309 * @icon_info: a #GtkIconInfo
3310 * @context: a #GtkStyleContext
3311 * @was_symbolic: (out) (allow-none): a #gboolean, returns whether the
3312 * loaded icon was a symbolic one and whether the @fg color was
3314 * @error: (allow-none): location to store error information on failure,
3317 * Loads an icon, modifying it to match the system colors for the foreground,
3318 * success, warning and error colors provided. If the icon is not a symbolic
3319 * one, the function will return the result from gtk_icon_info_load_icon().
3320 * This function uses the regular foreground color and the symbolic colors
3321 * with the names "success_color", "warning_color" and "error_color" from
3324 * This allows loading symbolic icons that will match the system theme.
3326 * See gtk_icon_info_load_symbolic() for more details.
3328 * Return value: (transfer full): a #GdkPixbuf representing the loaded icon
3333 gtk_icon_info_load_symbolic_for_context (GtkIconInfo *icon_info,
3334 GtkStyleContext *context,
3335 gboolean *was_symbolic,
3339 GdkRGBA *color = NULL;
3341 gchar *css_fg = NULL, *css_success;
3342 gchar *css_warning, *css_error;
3343 GtkStateFlags state;
3345 if (!icon_info->filename ||
3346 !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3349 *was_symbolic = FALSE;
3350 return gtk_icon_info_load_icon (icon_info, error);
3354 *was_symbolic = TRUE;
3356 state = gtk_style_context_get_state (context);
3357 gtk_style_context_get (context, state, "color", &color, NULL);
3360 css_fg = gdk_rgba_to_css (color);
3361 gdk_rgba_free (color);
3364 css_success = css_warning = css_error = NULL;
3366 if (gtk_style_context_lookup_color (context, "success_color", &rgba))
3367 css_success = gdk_rgba_to_css (&rgba);
3369 if (gtk_style_context_lookup_color (context, "warning_color", &rgba))
3370 css_warning = gdk_rgba_to_css (&rgba);
3372 if (gtk_style_context_lookup_color (context, "error_color", &rgba))
3373 css_error = gdk_rgba_to_css (&rgba);
3375 pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
3376 css_fg, css_success,
3377 css_warning, css_error,
3381 g_free (css_success);
3382 g_free (css_warning);
3389 * gtk_icon_info_load_symbolic_for_style:
3390 * @icon_info: a #GtkIconInfo
3391 * @style: a #GtkStyle to take the colors from
3392 * @state: the widget state to use for colors
3393 * @was_symbolic: (out) (allow-none): a #gboolean, returns whether the
3394 * loaded icon was a symbolic one and whether the @fg color was
3396 * @error: (allow-none): location to store error information on failure,
3399 * Loads an icon, modifying it to match the system colours for the foreground,
3400 * success, warning and error colors provided. If the icon is not a symbolic
3401 * one, the function will return the result from gtk_icon_info_load_icon().
3403 * This allows loading symbolic icons that will match the system theme.
3405 * See gtk_icon_info_load_symbolic() for more details.
3407 * Return value: (transfer full): a #GdkPixbuf representing the loaded icon
3411 * Deprecated: 3.0: Use gtk_icon_info_load_symbolic_for_context() instead
3414 gtk_icon_info_load_symbolic_for_style (GtkIconInfo *icon_info,
3417 gboolean *was_symbolic,
3421 GdkColor success_color;
3422 GdkColor warning_color;
3423 GdkColor error_color;
3425 gchar *css_fg, *css_success;
3426 gchar *css_warning, *css_error;
3428 if (!icon_info->filename ||
3429 !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3432 *was_symbolic = FALSE;
3433 return gtk_icon_info_load_icon (icon_info, error);
3437 *was_symbolic = TRUE;
3439 fg = &style->fg[state];
3440 css_fg = gdk_color_to_css (fg);
3442 css_success = css_warning = css_error = NULL;
3444 if (gtk_style_lookup_color (style, "success_color", &success_color))
3445 css_success = gdk_color_to_css (&success_color);
3447 if (gtk_style_lookup_color (style, "warning_color", &warning_color))
3448 css_warning = gdk_color_to_css (&warning_color);
3450 if (gtk_style_lookup_color (style, "error_color", &error_color))
3451 css_error = gdk_color_to_css (&error_color);
3453 pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
3454 css_fg, css_success,
3455 css_warning, css_error,
3459 g_free (css_success);
3460 g_free (css_warning);
3467 * gtk_icon_info_set_raw_coordinates:
3468 * @icon_info: a #GtkIconInfo
3469 * @raw_coordinates: whether the coordinates of embedded rectangles
3470 * and attached points should be returned in their original
3473 * Sets whether the coordinates returned by gtk_icon_info_get_embedded_rect()
3474 * and gtk_icon_info_get_attach_points() should be returned in their
3475 * original form as specified in the icon theme, instead of scaled
3476 * appropriately for the pixbuf returned by gtk_icon_info_load_icon().
3478 * Raw coordinates are somewhat strange; they are specified to be with
3479 * respect to the unscaled pixmap for PNG and XPM icons, but for SVG
3480 * icons, they are in a 1000x1000 coordinate space that is scaled
3481 * to the final size of the icon. You can determine if the icon is an SVG
3482 * icon by using gtk_icon_info_get_filename(), and seeing if it is non-%NULL
3483 * and ends in '.svg'.
3485 * This function is provided primarily to allow compatibility wrappers
3486 * for older API's, and is not expected to be useful for applications.
3491 gtk_icon_info_set_raw_coordinates (GtkIconInfo *icon_info,
3492 gboolean raw_coordinates)
3494 g_return_if_fail (icon_info != NULL);
3496 icon_info->raw_coordinates = raw_coordinates != FALSE;
3499 /* Scale coordinates from the icon data prior to returning
3503 icon_info_scale_point (GtkIconInfo *icon_info,
3509 if (icon_info->raw_coordinates)
3516 if (!icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3519 *x_out = 0.5 + x * icon_info->scale;
3520 *y_out = 0.5 + y * icon_info->scale;
3527 * gtk_icon_info_get_embedded_rect:
3528 * @icon_info: a #GtkIconInfo
3529 * @rectangle: (out): #GdkRectangle in which to store embedded
3530 * rectangle coordinates; coordinates are only stored
3531 * when this function returns %TRUE.
3533 * Gets the coordinates of a rectangle within the icon
3534 * that can be used for display of information such
3535 * as a preview of the contents of a text file.
3536 * See gtk_icon_info_set_raw_coordinates() for further
3537 * information about the coordinate system.
3539 * Return value: %TRUE if the icon has an embedded rectangle
3544 gtk_icon_info_get_embedded_rect (GtkIconInfo *icon_info,
3545 GdkRectangle *rectangle)
3547 g_return_val_if_fail (icon_info != NULL, FALSE);
3549 if (icon_info->data && icon_info->data->has_embedded_rect &&
3550 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3552 gint scaled_x0, scaled_y0;
3553 gint scaled_x1, scaled_y1;
3557 icon_info_scale_point (icon_info,
3558 icon_info->data->x0, icon_info->data->y0,
3559 &scaled_x0, &scaled_y0);
3560 icon_info_scale_point (icon_info,
3561 icon_info->data->x1, icon_info->data->y1,
3562 &scaled_x1, &scaled_y1);
3564 rectangle->x = scaled_x0;
3565 rectangle->y = scaled_y0;
3566 rectangle->width = scaled_x1 - rectangle->x;
3567 rectangle->height = scaled_y1 - rectangle->y;
3577 * gtk_icon_info_get_attach_points:
3578 * @icon_info: a #GtkIconInfo
3579 * @points: (allow-none) (array length=n_points) (out): location to store pointer to an array of points, or %NULL
3580 * free the array of points with g_free().
3581 * @n_points: (allow-none): location to store the number of points in @points, or %NULL
3583 * Fetches the set of attach points for an icon. An attach point
3584 * is a location in the icon that can be used as anchor points for attaching
3585 * emblems or overlays to the icon.
3587 * Return value: %TRUE if there are any attach points for the icon.
3592 gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
3596 g_return_val_if_fail (icon_info != NULL, FALSE);
3598 if (icon_info->data && icon_info->data->n_attach_points &&
3599 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3605 *points = g_new (GdkPoint, icon_info->data->n_attach_points);
3606 for (i = 0; i < icon_info->data->n_attach_points; i++)
3607 icon_info_scale_point (icon_info,
3608 icon_info->data->attach_points[i].x,
3609 icon_info->data->attach_points[i].y,
3615 *n_points = icon_info->data->n_attach_points;
3631 * gtk_icon_info_get_display_name:
3632 * @icon_info: a #GtkIconInfo
3634 * Gets the display name for an icon. A display name is a
3635 * string to be used in place of the icon name in a user
3636 * visible context like a list of icons.
3638 * Return value: the display name for the icon or %NULL, if
3639 * the icon doesn't have a specified display name. This value
3640 * is owned @icon_info and must not be modified or free.
3645 gtk_icon_info_get_display_name (GtkIconInfo *icon_info)
3647 g_return_val_if_fail (icon_info != NULL, NULL);
3649 if (icon_info->data)
3650 return icon_info->data->display_name;
3661 * gtk_icon_theme_add_builtin_icon:
3662 * @icon_name: the name of the icon to register
3663 * @size: the size at which to register the icon (different
3664 * images can be registered for the same icon name
3665 * at different sizes.)
3666 * @pixbuf: #GdkPixbuf that contains the image to use
3669 * Registers a built-in icon for icon theme lookups. The idea
3670 * of built-in icons is to allow an application or library
3671 * that uses themed icons to function requiring files to
3672 * be present in the file system. For instance, the default
3673 * images for all of GTK+'s stock icons are registered
3676 * In general, if you use gtk_icon_theme_add_builtin_icon()
3677 * you should also install the icon in the icon theme, so
3678 * that the icon is generally available.
3680 * This function will generally be used with pixbufs loaded
3681 * via gdk_pixbuf_new_from_inline().
3686 gtk_icon_theme_add_builtin_icon (const gchar *icon_name,
3690 BuiltinIcon *default_icon;
3694 g_return_if_fail (icon_name != NULL);
3695 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3697 if (!icon_theme_builtin_icons)
3698 icon_theme_builtin_icons = g_hash_table_new (g_str_hash, g_str_equal);
3700 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
3702 key = g_strdup (icon_name);
3704 key = (gpointer)icon_name; /* Won't get stored */
3706 default_icon = g_new (BuiltinIcon, 1);
3707 default_icon->size = size;
3708 default_icon->pixbuf = g_object_ref (pixbuf);
3709 icons = g_slist_prepend (icons, default_icon);
3711 /* Replaces value, leaves key untouched
3713 g_hash_table_insert (icon_theme_builtin_icons, key, icons);
3716 /* Look up a builtin icon; the min_difference_p and
3717 * has_larger_p out parameters allow us to combine
3718 * this lookup with searching through the actual directories
3719 * of the "hicolor" icon theme. See theme_lookup_icon()
3720 * for how they are used.
3722 static BuiltinIcon *
3723 find_builtin_icon (const gchar *icon_name,
3725 gint *min_difference_p,
3726 gboolean *has_larger_p)
3728 GSList *icons = NULL;
3729 gint min_difference = G_MAXINT;
3730 gboolean has_larger = FALSE;
3731 BuiltinIcon *min_icon = NULL;
3733 if (!icon_theme_builtin_icons)
3736 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
3740 BuiltinIcon *default_icon = icons->data;
3741 int min, max, difference;
3744 min = default_icon->size - 2;
3745 max = default_icon->size + 2;
3746 smaller = size < min;
3748 difference = min - size;
3749 else if (size > max)
3750 difference = size - max;
3754 if (difference == 0)
3756 min_icon = default_icon;
3762 if (difference < min_difference || smaller)
3764 min_difference = difference;
3765 min_icon = default_icon;
3766 has_larger = smaller;
3771 if (difference < min_difference && smaller)
3773 min_difference = difference;
3774 min_icon = default_icon;
3778 icons = icons->next;
3781 if (min_difference_p)
3782 *min_difference_p = min_difference;
3784 *has_larger_p = has_larger;
3790 _gtk_icon_theme_check_reload (GdkDisplay *display)
3794 GtkIconTheme *icon_theme;
3796 n_screens = gdk_display_get_n_screens (display);
3798 for (i = 0; i < n_screens; i++)
3800 screen = gdk_display_get_screen (display, i);
3802 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
3805 icon_theme->priv->check_reload = TRUE;
3806 ensure_valid_themes (icon_theme);
3807 icon_theme->priv->check_reload = FALSE;
3814 * gtk_icon_theme_lookup_by_gicon:
3815 * @icon_theme: a #GtkIconTheme
3816 * @icon: the #GIcon to look up
3817 * @size: desired icon size
3818 * @flags: flags modifying the behavior of the icon lookup
3820 * Looks up an icon and returns a structure containing
3821 * information such as the filename of the icon.
3822 * The icon can then be rendered into a pixbuf using
3823 * gtk_icon_info_load_icon().
3825 * Return value: a #GtkIconInfo structure containing
3826 * information about the icon, or %NULL if the icon
3827 * wasn't found. Free with gtk_icon_info_free()
3832 gtk_icon_theme_lookup_by_gicon (GtkIconTheme *icon_theme,
3835 GtkIconLookupFlags flags)
3839 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
3840 g_return_val_if_fail (G_IS_ICON (icon), NULL);
3842 if (G_IS_LOADABLE_ICON (icon))
3844 info = icon_info_new ();
3845 info->loadable = G_LOADABLE_ICON (g_object_ref (icon));
3847 info->dir_type = ICON_THEME_DIR_UNTHEMED;
3848 info->dir_size = size;
3849 info->desired_size = size;
3850 info->threshold = 2;
3851 info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
3855 else if (G_IS_THEMED_ICON (icon))
3857 const gchar **names;
3859 names = (const gchar **)g_themed_icon_get_names (G_THEMED_ICON (icon));
3860 info = gtk_icon_theme_choose_icon (icon_theme, names, size, flags);
3864 else if (G_IS_EMBLEMED_ICON (icon))
3866 GIcon *base, *emblem;
3868 GtkIconInfo *emblem_info;
3870 if (GTK_IS_NUMERABLE_ICON (icon))
3871 _gtk_numerable_icon_set_background_icon_size (GTK_NUMERABLE_ICON (icon), size / 2);
3873 base = g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon));
3874 info = gtk_icon_theme_lookup_by_gicon (icon_theme, base, size, flags);
3877 list = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon));
3878 for (l = list; l; l = l->next)
3880 emblem = g_emblem_get_icon (G_EMBLEM (l->data));
3881 /* always force size for emblems */
3882 emblem_info = gtk_icon_theme_lookup_by_gicon (icon_theme, emblem, size / 2, flags | GTK_ICON_LOOKUP_FORCE_SIZE);
3884 info->emblem_infos = g_slist_prepend (info->emblem_infos, emblem_info);
3890 else if (GDK_IS_PIXBUF (icon))
3894 pixbuf = GDK_PIXBUF (icon);
3896 if ((flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0)
3898 gint width, height, max;
3902 width = gdk_pixbuf_get_width (pixbuf);
3903 height = gdk_pixbuf_get_height (pixbuf);
3904 max = MAX (width, height);
3905 scale = (gdouble) size / (gdouble) max;
3907 scaled = gdk_pixbuf_scale_simple (pixbuf,
3908 0.5 + width * scale,
3909 0.5 + height * scale,
3910 GDK_INTERP_BILINEAR);
3912 info = gtk_icon_info_new_for_pixbuf (icon_theme, scaled);
3914 g_object_unref (scaled);
3918 info = gtk_icon_info_new_for_pixbuf (icon_theme, pixbuf);
3928 * gtk_icon_info_new_for_pixbuf:
3929 * @icon_theme: a #GtkIconTheme
3930 * @pixbuf: the pixbuf to wrap in a #GtkIconInfo
3932 * Creates a #GtkIconInfo for a #GdkPixbuf.
3934 * Returns: a #GtkIconInfo
3939 gtk_icon_info_new_for_pixbuf (GtkIconTheme *icon_theme,
3944 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
3945 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
3947 info = icon_info_new ();
3948 info->pixbuf = g_object_ref (pixbuf);
3950 info->dir_type = ICON_THEME_DIR_UNTHEMED;