1 /* GtkIconTheme - a loader for icon themes
2 * gtk-icon-theme.c Copyright (C) 2002, 2003 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include <sys/types.h>
30 #include <glib/gstdio.h>
34 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
36 #define WIN32_MEAN_AND_LEAN
38 #include "win32/gdkwin32.h"
39 #endif /* G_OS_WIN32 */
41 #include "gtkicontheme.h"
42 #include "gtkiconfactory.h"
43 #include "gtkiconcache.h"
44 #include "gtkbuiltincache.h"
47 #include "gtksettings.h"
48 #include "gtkprivate.h"
51 #define DEFAULT_THEME_NAME "hicolor"
56 ICON_THEME_DIR_SCALABLE,
57 ICON_THEME_DIR_THRESHOLD,
58 ICON_THEME_DIR_UNTHEMED
61 /* In reverse search order: */
65 ICON_SUFFIX_XPM = 1 << 0,
66 ICON_SUFFIX_SVG = 1 << 1,
67 ICON_SUFFIX_PNG = 1 << 2,
68 HAS_ICON_FILE = 1 << 3
72 struct _GtkIconThemePrivate
74 guint custom_theme : 1;
75 guint is_screen_singleton : 1;
76 guint pixbuf_supports_svg : 1;
77 guint themes_valid : 1;
78 guint check_reload : 1;
79 guint loading_themes : 1;
86 /* A list of all the themes needed to look up icons.
87 * In search order, without duplicates
90 GHashTable *unthemed_icons;
92 /* Note: The keys of this hashtable are owned by the
93 * themedir and unthemed hashtables.
95 GHashTable *all_icons;
97 /* GdkScreen for the icon theme (may be NULL)
101 /* time when we last stat:ed for theme changes */
105 gulong reset_styles_idle;
110 /* Information about the source
113 #if defined (G_OS_WIN32) && !defined (_WIN64)
114 /* System codepage version of filename, for DLL ABI backward
115 * compatibility functions.
119 GLoadableIcon *loadable;
120 GSList *emblem_infos;
122 /* Cache pixbuf (if there is any) */
123 GdkPixbuf *cache_pixbuf;
127 /* Information about the directory where
128 * the source was found
130 IconThemeDirType dir_type;
134 /* Parameters influencing the scaled icon
137 guint raw_coordinates : 1;
138 guint forced_size : 1;
140 /* Cached information if we go ahead and try to load
157 /* In search order */
163 IconThemeDirType type;
178 GHashTable *icon_data;
184 char *no_svg_filename;
196 time_t mtime; /* 0 == not existing or not a dir */
201 static void gtk_icon_theme_finalize (GObject *object);
202 static void theme_dir_destroy (IconThemeDir *dir);
204 static void theme_destroy (IconTheme *theme);
205 static GtkIconInfo *theme_lookup_icon (IconTheme *theme,
206 const char *icon_name,
209 gboolean use_default_icons);
210 static void theme_list_icons (IconTheme *theme,
213 static void theme_list_contexts (IconTheme *theme,
214 GHashTable *contexts);
215 static void theme_subdir_load (GtkIconTheme *icon_theme,
217 GKeyFile *theme_file,
219 static void do_theme_change (GtkIconTheme *icon_theme);
221 static void blow_themes (GtkIconTheme *icon_themes);
222 static gboolean rescan_themes (GtkIconTheme *icon_themes);
224 static void icon_data_free (GtkIconData *icon_data);
225 static void load_icon_data (IconThemeDir *dir,
229 static IconSuffix theme_dir_get_icon_suffix (IconThemeDir *dir,
230 const gchar *icon_name,
231 gboolean *has_icon_file);
234 static GtkIconInfo *icon_info_new (void);
235 static GtkIconInfo *icon_info_new_builtin (BuiltinIcon *icon);
237 static IconSuffix suffix_from_name (const char *name);
239 static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
241 gint *min_difference_p,
242 gboolean *has_larger_p);
244 static guint signal_changed = 0;
246 static GHashTable *icon_theme_builtin_icons;
248 /* also used in gtkiconfactory.c */
249 GtkIconCache *_builtin_cache = NULL;
250 static GList *builtin_dirs = NULL;
252 G_DEFINE_TYPE (GtkIconTheme, gtk_icon_theme, G_TYPE_OBJECT)
255 * gtk_icon_theme_new:
257 * Creates a new icon theme object. Icon theme objects are used
258 * to lookup up an icon by name in a particular icon theme.
259 * Usually, you'll want to use gtk_icon_theme_get_default()
260 * or gtk_icon_theme_get_for_screen() rather than creating
261 * a new icon theme object for scratch.
263 * Return value: the newly created #GtkIconTheme object.
268 gtk_icon_theme_new (void)
270 return g_object_new (GTK_TYPE_ICON_THEME, NULL);
274 * gtk_icon_theme_get_default:
276 * Gets the icon theme for the default screen. See
277 * gtk_icon_theme_get_for_screen().
279 * Return value: (transfer none): A unique #GtkIconTheme associated with
280 * the default screen. This icon theme is associated with
281 * the screen and can be used as long as the screen
282 * is open. Do not ref or unref it.
287 gtk_icon_theme_get_default (void)
289 return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
293 * gtk_icon_theme_get_for_screen:
294 * @screen: a #GdkScreen
296 * Gets the icon theme object associated with @screen; if this
297 * function has not previously been called for the given
298 * screen, a new icon theme object will be created and
299 * associated with the screen. Icon theme objects are
300 * fairly expensive to create, so using this function
301 * is usually a better choice than calling than gtk_icon_theme_new()
302 * and setting the screen yourself; by using this function
303 * a single icon theme object will be shared between users.
305 * Return value: (transfer none): A unique #GtkIconTheme associated with
306 * the given screen. This icon theme is associated with
307 * the screen and can be used as long as the screen
308 * is open. Do not ref or unref it.
313 gtk_icon_theme_get_for_screen (GdkScreen *screen)
315 GtkIconTheme *icon_theme;
317 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
318 g_return_val_if_fail (!screen->closed, NULL);
320 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
323 GtkIconThemePrivate *priv;
325 icon_theme = gtk_icon_theme_new ();
326 gtk_icon_theme_set_screen (icon_theme, screen);
328 priv = icon_theme->priv;
329 priv->is_screen_singleton = TRUE;
331 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), icon_theme);
338 gtk_icon_theme_class_init (GtkIconThemeClass *klass)
340 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
342 gobject_class->finalize = gtk_icon_theme_finalize;
345 * GtkIconTheme::changed
346 * @icon_theme: the icon theme
348 * Emitted when the current icon theme is switched or GTK+ detects
349 * that a change has occurred in the contents of the current
352 signal_changed = g_signal_new (I_("changed"),
353 G_TYPE_FROM_CLASS (klass),
355 G_STRUCT_OFFSET (GtkIconThemeClass, changed),
357 g_cclosure_marshal_VOID__VOID,
360 g_type_class_add_private (klass, sizeof (GtkIconThemePrivate));
364 /* Callback when the display that the icon theme is attached to
365 * is closed; unset the screen, and if it's the unique theme
366 * for the screen, drop the reference
369 display_closed (GdkDisplay *display,
371 GtkIconTheme *icon_theme)
373 GtkIconThemePrivate *priv = icon_theme->priv;
374 GdkScreen *screen = priv->screen;
375 gboolean was_screen_singleton = priv->is_screen_singleton;
377 if (was_screen_singleton)
379 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), NULL);
380 priv->is_screen_singleton = FALSE;
383 gtk_icon_theme_set_screen (icon_theme, NULL);
385 if (was_screen_singleton)
387 g_object_unref (icon_theme);
392 update_current_theme (GtkIconTheme *icon_theme)
394 #define theme_changed(_old, _new) \
395 ((_old && !_new) || (!_old && _new) || \
396 (_old && _new && strcmp (_old, _new) != 0))
397 GtkIconThemePrivate *priv = icon_theme->priv;
399 if (!priv->custom_theme)
402 gchar *fallback_theme = NULL;
403 gboolean changed = FALSE;
407 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
408 g_object_get (settings,
409 "gtk-icon-theme-name", &theme,
410 "gtk-fallback-icon-theme", &fallback_theme, NULL);
413 /* ensure that the current theme (even when just the default)
414 * is searched before any fallback theme
416 if (!theme && fallback_theme)
417 theme = g_strdup (DEFAULT_THEME_NAME);
419 if (theme_changed (priv->current_theme, theme))
421 g_free (priv->current_theme);
422 priv->current_theme = theme;
428 if (theme_changed (priv->fallback_theme, fallback_theme))
430 g_free (priv->fallback_theme);
431 priv->fallback_theme = fallback_theme;
435 g_free (fallback_theme);
438 do_theme_change (icon_theme);
443 /* Callback when the icon theme GtkSetting changes
446 theme_changed (GtkSettings *settings,
448 GtkIconTheme *icon_theme)
450 update_current_theme (icon_theme);
454 unset_screen (GtkIconTheme *icon_theme)
456 GtkIconThemePrivate *priv = icon_theme->priv;
457 GtkSettings *settings;
462 settings = gtk_settings_get_for_screen (priv->screen);
463 display = gdk_screen_get_display (priv->screen);
465 g_signal_handlers_disconnect_by_func (display,
466 (gpointer) display_closed,
468 g_signal_handlers_disconnect_by_func (settings,
469 (gpointer) theme_changed,
477 * gtk_icon_theme_set_screen:
478 * @icon_theme: a #GtkIconTheme
479 * @screen: a #GdkScreen
481 * Sets the screen for an icon theme; the screen is used
482 * to track the user's currently configured icon theme,
483 * which might be different for different screens.
488 gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
491 GtkIconThemePrivate *priv;
492 GtkSettings *settings;
495 g_return_if_fail (GTK_ICON_THEME (icon_theme));
496 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
498 priv = icon_theme->priv;
500 unset_screen (icon_theme);
504 display = gdk_screen_get_display (screen);
505 settings = gtk_settings_get_for_screen (screen);
507 priv->screen = screen;
509 g_signal_connect (display, "closed",
510 G_CALLBACK (display_closed), icon_theme);
511 g_signal_connect (settings, "notify::gtk-icon-theme-name",
512 G_CALLBACK (theme_changed), icon_theme);
513 g_signal_connect (settings, "notify::gtk-fallback-icon-theme-name",
514 G_CALLBACK (theme_changed), icon_theme);
517 update_current_theme (icon_theme);
520 /* Checks whether a loader for SVG files has been registered
524 pixbuf_supports_svg (void)
528 static gint found_svg = -1;
533 formats = gdk_pixbuf_get_formats ();
536 for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
538 gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
541 for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
543 if (strcmp (*mime_type, "image/svg") == 0)
547 g_strfreev (mime_types);
550 g_slist_free (formats);
556 gtk_icon_theme_init (GtkIconTheme *icon_theme)
558 GtkIconThemePrivate *priv;
559 const gchar * const *xdg_data_dirs;
562 priv = g_type_instance_get_private ((GTypeInstance *)icon_theme,
563 GTK_TYPE_ICON_THEME);
564 icon_theme->priv = priv;
566 priv->custom_theme = FALSE;
568 xdg_data_dirs = g_get_system_data_dirs ();
569 for (i = 0; xdg_data_dirs[i]; i++) ;
571 priv->search_path_len = 2 * i + 2;
573 priv->search_path = g_new (char *, priv->search_path_len);
576 priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
577 priv->search_path[i++] = g_build_filename (g_get_user_data_dir (), "icons", NULL);
579 for (j = 0; xdg_data_dirs[j]; j++)
580 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
582 for (j = 0; xdg_data_dirs[j]; j++)
583 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "pixmaps", NULL);
585 priv->themes_valid = FALSE;
587 priv->unthemed_icons = NULL;
589 priv->pixbuf_supports_svg = pixbuf_supports_svg ();
593 free_dir_mtime (IconThemeDirMtime *dir_mtime)
595 if (dir_mtime->cache)
596 _gtk_icon_cache_unref (dir_mtime->cache);
598 g_free (dir_mtime->dir);
599 g_slice_free (IconThemeDirMtime, dir_mtime);
604 reset_styles_idle (gpointer user_data)
606 GtkIconTheme *icon_theme;
607 GtkIconThemePrivate *priv;
609 icon_theme = GTK_ICON_THEME (user_data);
610 priv = icon_theme->priv;
612 if (priv->screen && priv->is_screen_singleton)
614 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
615 gtk_rc_reset_styles (settings);
618 priv->reset_styles_idle = 0;
624 do_theme_change (GtkIconTheme *icon_theme)
626 GtkIconThemePrivate *priv = icon_theme->priv;
628 if (!priv->themes_valid)
632 g_print ("change to icon theme \"%s\"\n", priv->current_theme));
633 blow_themes (icon_theme);
634 g_signal_emit (icon_theme, signal_changed, 0);
636 if (!priv->reset_styles_idle)
637 priv->reset_styles_idle =
638 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2,
639 reset_styles_idle, icon_theme, NULL);
643 blow_themes (GtkIconTheme *icon_theme)
645 GtkIconThemePrivate *priv = icon_theme->priv;
647 if (priv->themes_valid)
649 g_hash_table_destroy (priv->all_icons);
650 g_list_foreach (priv->themes, (GFunc)theme_destroy, NULL);
651 g_list_free (priv->themes);
652 g_list_foreach (priv->dir_mtimes, (GFunc)free_dir_mtime, NULL);
653 g_list_free (priv->dir_mtimes);
654 g_hash_table_destroy (priv->unthemed_icons);
657 priv->unthemed_icons = NULL;
658 priv->dir_mtimes = NULL;
659 priv->all_icons = NULL;
660 priv->themes_valid = FALSE;
664 gtk_icon_theme_finalize (GObject *object)
666 GtkIconTheme *icon_theme;
667 GtkIconThemePrivate *priv;
670 icon_theme = GTK_ICON_THEME (object);
671 priv = icon_theme->priv;
673 if (priv->reset_styles_idle)
675 g_source_remove (priv->reset_styles_idle);
676 priv->reset_styles_idle = 0;
679 unset_screen (icon_theme);
681 g_free (priv->current_theme);
682 priv->current_theme = NULL;
684 for (i = 0; i < priv->search_path_len; i++)
685 g_free (priv->search_path[i]);
687 g_free (priv->search_path);
688 priv->search_path = NULL;
690 blow_themes (icon_theme);
692 G_OBJECT_CLASS (gtk_icon_theme_parent_class)->finalize (object);
696 * gtk_icon_theme_set_search_path:
697 * @icon_theme: a #GtkIconTheme
698 * @path: array of directories that are searched for icon themes
699 * @n_elements: number of elements in @path.
701 * Sets the search path for the icon theme object. When looking
702 * for an icon theme, GTK+ will search for a subdirectory of
703 * one or more of the directories in @path with the same name
704 * as the icon theme. (Themes from multiple of the path elements
705 * are combined to allow themes to be extended by adding icons
706 * in the user's home directory.)
708 * In addition if an icon found isn't found either in the current
709 * icon theme or the default icon theme, and an image file with
710 * the right name is found directly in one of the elements of
711 * @path, then that image will be used for the icon name.
712 * (This is legacy feature, and new icons should be put
713 * into the default icon theme, which is called DEFAULT_THEME_NAME,
714 * rather than directly on the icon path.)
719 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
723 GtkIconThemePrivate *priv;
726 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
728 priv = icon_theme->priv;
729 for (i = 0; i < priv->search_path_len; i++)
730 g_free (priv->search_path[i]);
732 g_free (priv->search_path);
734 priv->search_path = g_new (gchar *, n_elements);
735 priv->search_path_len = n_elements;
737 for (i = 0; i < priv->search_path_len; i++)
738 priv->search_path[i] = g_strdup (path[i]);
740 do_theme_change (icon_theme);
745 * gtk_icon_theme_get_search_path:
746 * @icon_theme: a #GtkIconTheme
747 * @path: (array length=n_elements) (out): location to store a list of icon theme path directories or %NULL
748 * The stored value should be freed with g_strfreev().
749 * @n_elements: location to store number of elements
752 * Gets the current search path. See gtk_icon_theme_set_search_path().
757 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
761 GtkIconThemePrivate *priv;
764 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
766 priv = icon_theme->priv;
769 *n_elements = priv->search_path_len;
773 *path = g_new (gchar *, priv->search_path_len + 1);
774 for (i = 0; i < priv->search_path_len; i++)
775 (*path)[i] = g_strdup (priv->search_path[i]);
781 * gtk_icon_theme_append_search_path:
782 * @icon_theme: a #GtkIconTheme
783 * @path: directory name to append to the icon path
785 * Appends a directory to the search path.
786 * See gtk_icon_theme_set_search_path().
791 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
794 GtkIconThemePrivate *priv;
796 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
797 g_return_if_fail (path != NULL);
799 priv = icon_theme->priv;
801 priv->search_path_len++;
803 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
804 priv->search_path[priv->search_path_len-1] = g_strdup (path);
806 do_theme_change (icon_theme);
810 * gtk_icon_theme_prepend_search_path:
811 * @icon_theme: a #GtkIconTheme
812 * @path: directory name to prepend to the icon path
814 * Prepends a directory to the search path.
815 * See gtk_icon_theme_set_search_path().
820 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
823 GtkIconThemePrivate *priv;
826 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
827 g_return_if_fail (path != NULL);
829 priv = icon_theme->priv;
831 priv->search_path_len++;
832 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
834 for (i = priv->search_path_len - 1; i > 0; i--)
835 priv->search_path[i] = priv->search_path[i - 1];
837 priv->search_path[0] = g_strdup (path);
839 do_theme_change (icon_theme);
843 * gtk_icon_theme_set_custom_theme:
844 * @icon_theme: a #GtkIconTheme
845 * @theme_name: name of icon theme to use instead of configured theme,
846 * or %NULL to unset a previously set custom theme
848 * Sets the name of the icon theme that the #GtkIconTheme object uses
849 * overriding system configuration. This function cannot be called
850 * on the icon theme objects returned from gtk_icon_theme_get_default()
851 * and gtk_icon_theme_get_for_screen().
856 gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
857 const gchar *theme_name)
859 GtkIconThemePrivate *priv;
861 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
863 priv = icon_theme->priv;
865 g_return_if_fail (!priv->is_screen_singleton);
867 if (theme_name != NULL)
869 priv->custom_theme = TRUE;
870 if (!priv->current_theme || strcmp (theme_name, priv->current_theme) != 0)
872 g_free (priv->current_theme);
873 priv->current_theme = g_strdup (theme_name);
875 do_theme_change (icon_theme);
880 if (priv->custom_theme)
882 priv->custom_theme = FALSE;
884 update_current_theme (icon_theme);
890 insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
896 GtkIconThemePrivate *priv;
897 IconTheme *theme = NULL;
899 GKeyFile *theme_file;
900 GError *error = NULL;
901 IconThemeDirMtime *dir_mtime;
902 struct stat stat_buf;
904 priv = icon_theme->priv;
906 for (l = priv->themes; l != NULL; l = l->next)
909 if (strcmp (theme->name, theme_name) == 0)
913 for (i = 0; i < priv->search_path_len; i++)
915 path = g_build_filename (priv->search_path[i],
918 dir_mtime = g_slice_new (IconThemeDirMtime);
919 dir_mtime->cache = NULL;
920 dir_mtime->dir = path;
921 if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
922 dir_mtime->mtime = stat_buf.st_mtime;
924 dir_mtime->mtime = 0;
926 priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
928 priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
931 for (i = 0; i < priv->search_path_len && !theme_file; i++)
933 path = g_build_filename (priv->search_path[i],
937 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
939 theme_file = g_key_file_new ();
940 g_key_file_set_list_separator (theme_file, ',');
941 g_key_file_load_from_file (theme_file, path, 0, &error);
944 g_key_file_free (theme_file);
946 g_error_free (error);
953 if (theme_file || strcmp (theme_name, DEFAULT_THEME_NAME) == 0)
955 theme = g_new0 (IconTheme, 1);
956 theme->name = g_strdup (theme_name);
957 priv->themes = g_list_prepend (priv->themes, theme);
960 if (theme_file == NULL)
963 theme->display_name =
964 g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
965 if (!theme->display_name)
966 g_warning ("Theme file for %s has no name\n", theme_name);
968 dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
971 g_warning ("Theme file for %s has no directories\n", theme_name);
972 priv->themes = g_list_remove (priv->themes, theme);
973 g_free (theme->name);
974 g_free (theme->display_name);
976 g_key_file_free (theme_file);
981 g_key_file_get_locale_string (theme_file,
982 "Icon Theme", "Comment",
985 g_key_file_get_string (theme_file,
986 "Icon Theme", "Example",
990 for (i = 0; dirs[i] != NULL; i++)
991 theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
995 theme->dirs = g_list_reverse (theme->dirs);
997 themes = g_key_file_get_string_list (theme_file,
1004 for (i = 0; themes[i] != NULL; i++)
1005 insert_theme (icon_theme, themes[i]);
1007 g_strfreev (themes);
1010 g_key_file_free (theme_file);
1014 free_unthemed_icon (UnthemedIcon *unthemed_icon)
1016 g_free (unthemed_icon->svg_filename);
1017 g_free (unthemed_icon->no_svg_filename);
1018 g_slice_free (UnthemedIcon, unthemed_icon);
1022 strip_suffix (const char *filename)
1026 dot = strrchr (filename, '.');
1029 return g_strdup (filename);
1031 return g_strndup (filename, dot - filename);
1035 load_themes (GtkIconTheme *icon_theme)
1037 GtkIconThemePrivate *priv;
1042 UnthemedIcon *unthemed_icon;
1043 IconSuffix old_suffix, new_suffix;
1045 IconThemeDirMtime *dir_mtime;
1046 struct stat stat_buf;
1048 priv = icon_theme->priv;
1050 priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
1052 if (priv->current_theme)
1053 insert_theme (icon_theme, priv->current_theme);
1055 /* Always look in the "default" icon theme, and in a fallback theme */
1056 if (priv->fallback_theme)
1057 insert_theme (icon_theme, priv->fallback_theme);
1058 insert_theme (icon_theme, DEFAULT_THEME_NAME);
1059 priv->themes = g_list_reverse (priv->themes);
1062 priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
1063 g_free, (GDestroyNotify)free_unthemed_icon);
1065 for (base = 0; base < icon_theme->priv->search_path_len; base++)
1067 dir = icon_theme->priv->search_path[base];
1069 dir_mtime = g_slice_new (IconThemeDirMtime);
1070 priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
1072 dir_mtime->dir = g_strdup (dir);
1073 dir_mtime->mtime = 0;
1074 dir_mtime->cache = NULL;
1076 if (g_stat (dir, &stat_buf) != 0 || !S_ISDIR (stat_buf.st_mode))
1078 dir_mtime->mtime = stat_buf.st_mtime;
1080 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir);
1081 if (dir_mtime->cache != NULL)
1084 gdir = g_dir_open (dir, 0, NULL);
1088 while ((file = g_dir_read_name (gdir)))
1090 new_suffix = suffix_from_name (file);
1092 if (new_suffix != ICON_SUFFIX_NONE)
1097 abs_file = g_build_filename (dir, file, NULL);
1098 base_name = strip_suffix (file);
1100 if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
1103 if (new_suffix == ICON_SUFFIX_SVG)
1105 if (unthemed_icon->svg_filename)
1108 unthemed_icon->svg_filename = abs_file;
1112 if (unthemed_icon->no_svg_filename)
1114 old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
1115 if (new_suffix > old_suffix)
1117 g_free (unthemed_icon->no_svg_filename);
1118 unthemed_icon->no_svg_filename = abs_file;
1124 unthemed_icon->no_svg_filename = abs_file;
1131 unthemed_icon = g_slice_new0 (UnthemedIcon);
1133 if (new_suffix == ICON_SUFFIX_SVG)
1134 unthemed_icon->svg_filename = abs_file;
1136 unthemed_icon->no_svg_filename = abs_file;
1138 g_hash_table_insert (priv->unthemed_icons,
1141 g_hash_table_insert (priv->all_icons,
1149 priv->themes_valid = TRUE;
1151 g_get_current_time(&tv);
1152 priv->last_stat_time = tv.tv_sec;
1156 _gtk_icon_theme_ensure_builtin_cache (void)
1158 static gboolean initialized = FALSE;
1160 static IconThemeDir dirs[5] =
1162 { ICON_THEME_DIR_THRESHOLD, 0, 16, 16, 16, 2, NULL, "16", -1, NULL, NULL, NULL },
1163 { ICON_THEME_DIR_THRESHOLD, 0, 20, 20, 20, 2, NULL, "20", -1, NULL, NULL, NULL },
1164 { ICON_THEME_DIR_THRESHOLD, 0, 24, 24, 24, 2, NULL, "24", -1, NULL, NULL, NULL },
1165 { ICON_THEME_DIR_THRESHOLD, 0, 32, 32, 32, 2, NULL, "32", -1, NULL, NULL, NULL },
1166 { ICON_THEME_DIR_THRESHOLD, 0, 48, 48, 48, 2, NULL, "48", -1, NULL, NULL, NULL }
1174 _builtin_cache = _gtk_icon_cache_new ((gchar *)builtin_icons);
1176 for (i = 0; i < G_N_ELEMENTS (dirs); i++)
1179 dir->cache = _gtk_icon_cache_ref (_builtin_cache);
1180 dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
1182 builtin_dirs = g_list_append (builtin_dirs, dir);
1188 ensure_valid_themes (GtkIconTheme *icon_theme)
1190 GtkIconThemePrivate *priv = icon_theme->priv;
1192 gboolean was_valid = priv->themes_valid;
1194 if (priv->loading_themes)
1196 priv->loading_themes = TRUE;
1198 _gtk_icon_theme_ensure_builtin_cache ();
1200 if (priv->themes_valid)
1202 g_get_current_time (&tv);
1204 if (ABS (tv.tv_sec - priv->last_stat_time) > 5 &&
1205 rescan_themes (icon_theme))
1206 blow_themes (icon_theme);
1209 if (!priv->themes_valid)
1211 load_themes (icon_theme);
1215 g_signal_emit (icon_theme, signal_changed, 0);
1217 if (!priv->check_reload && priv->screen)
1219 static GdkAtom atom_iconthemes = GDK_NONE;
1220 GdkEvent *event = gdk_event_new (GDK_CLIENT_EVENT);
1223 if (!atom_iconthemes)
1224 atom_iconthemes = gdk_atom_intern_static_string ("_GTK_LOAD_ICONTHEMES");
1226 for (i = 0; i < 5; i++)
1227 event->client.data.l[i] = 0;
1228 event->client.data_format = 32;
1229 event->client.message_type = atom_iconthemes;
1231 gdk_screen_broadcast_client_message (priv->screen, event);
1236 priv->loading_themes = FALSE;
1239 static GtkIconInfo *
1240 choose_icon (GtkIconTheme *icon_theme,
1241 const gchar *icon_names[],
1243 GtkIconLookupFlags flags)
1245 GtkIconThemePrivate *priv;
1247 GtkIconInfo *icon_info = NULL;
1248 UnthemedIcon *unthemed_icon = NULL;
1250 gboolean use_builtin;
1253 priv = icon_theme->priv;
1255 if (flags & GTK_ICON_LOOKUP_NO_SVG)
1257 else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
1260 allow_svg = priv->pixbuf_supports_svg;
1262 use_builtin = flags & GTK_ICON_LOOKUP_USE_BUILTIN;
1264 ensure_valid_themes (icon_theme);
1266 for (l = priv->themes; l; l = l->next)
1268 IconTheme *theme = l->data;
1270 for (i = 0; icon_names[i]; i++)
1272 icon_info = theme_lookup_icon (theme, icon_names[i], size, allow_svg, use_builtin);
1278 for (i = 0; icon_names[i]; i++)
1280 unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_names[i]);
1285 /* Still not found an icon, check if reference to a Win32 resource */
1291 resources = g_strsplit (icon_names[0], ",", 0);
1294 wchar_t *wfile = g_utf8_to_utf16 (resources[0], -1, NULL, NULL, NULL);
1295 ExtractIconExW (wfile, resources[1] ? atoi (resources[1]) : 0, &hIcon, NULL, 1);
1301 icon_info = icon_info_new ();
1302 icon_info->cache_pixbuf = gdk_win32_icon_to_pixbuf_libgtk_only (hIcon);
1303 DestroyIcon (hIcon);
1304 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1305 icon_info->dir_size = size;
1307 g_strfreev (resources);
1313 icon_info = icon_info_new ();
1315 /* A SVG icon, when allowed, beats out a XPM icon, but not
1319 unthemed_icon->svg_filename &&
1320 (!unthemed_icon->no_svg_filename ||
1321 suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
1322 icon_info->filename = g_strdup (unthemed_icon->svg_filename);
1323 else if (unthemed_icon->no_svg_filename)
1324 icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);
1325 #if defined (G_OS_WIN32) && !defined (_WIN64)
1326 icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
1327 -1, NULL, NULL, NULL);
1330 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1331 icon_info->dir_size = size;
1337 icon_info->desired_size = size;
1338 icon_info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
1342 static gboolean check_for_default_theme = TRUE;
1343 char *default_theme_path;
1344 gboolean found = FALSE;
1347 if (check_for_default_theme)
1349 check_for_default_theme = FALSE;
1351 for (i = 0; !found && i < priv->search_path_len; i++)
1353 default_theme_path = g_build_filename (priv->search_path[i],
1357 found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
1358 g_free (default_theme_path);
1363 g_warning (_("Could not find the icon '%s'. The '%s' theme\n"
1364 "was not found either, perhaps you need to install it.\n"
1365 "You can get a copy from:\n"
1367 icon_names[0], DEFAULT_THEME_NAME, "http://icon-theme.freedesktop.org/releases");
1377 * gtk_icon_theme_lookup_icon:
1378 * @icon_theme: a #GtkIconTheme
1379 * @icon_name: the name of the icon to lookup
1380 * @size: desired icon size
1381 * @flags: flags modifying the behavior of the icon lookup
1383 * Looks up a named icon and returns a structure containing
1384 * information such as the filename of the icon. The icon
1385 * can then be rendered into a pixbuf using
1386 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1387 * combines these two steps if all you need is the pixbuf.)
1389 * Return value: a #GtkIconInfo structure containing information
1390 * about the icon, or %NULL if the icon wasn't found. Free with
1391 * gtk_icon_info_free()
1396 gtk_icon_theme_lookup_icon (GtkIconTheme *icon_theme,
1397 const gchar *icon_name,
1399 GtkIconLookupFlags flags)
1403 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1404 g_return_val_if_fail (icon_name != NULL, NULL);
1405 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1406 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1408 GTK_NOTE (ICONTHEME,
1409 g_print ("gtk_icon_theme_lookup_icon %s\n", icon_name));
1411 if (flags & GTK_ICON_LOOKUP_GENERIC_FALLBACK)
1418 for (p = (gchar *) icon_name; *p; p++)
1422 names = g_new (gchar *, dashes + 2);
1423 names[0] = g_strdup (icon_name);
1424 for (i = 1; i <= dashes; i++)
1426 names[i] = g_strdup (names[i - 1]);
1427 p = strrchr (names[i], '-');
1430 names[dashes + 1] = NULL;
1432 info = choose_icon (icon_theme, (const gchar **) names, size, flags);
1438 const gchar *names[2];
1440 names[0] = icon_name;
1443 info = choose_icon (icon_theme, names, size, flags);
1450 * gtk_icon_theme_choose_icon:
1451 * @icon_theme: a #GtkIconTheme
1452 * @icon_names: %NULL-terminated array of icon names to lookup
1453 * @size: desired icon size
1454 * @flags: flags modifying the behavior of the icon lookup
1456 * Looks up a named icon and returns a structure containing
1457 * information such as the filename of the icon. The icon
1458 * can then be rendered into a pixbuf using
1459 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1460 * combines these two steps if all you need is the pixbuf.)
1462 * If @icon_names contains more than one name, this function
1463 * tries them all in the given order before falling back to
1464 * inherited icon themes.
1466 * Return value: a #GtkIconInfo structure containing information
1467 * about the icon, or %NULL if the icon wasn't found. Free with
1468 * gtk_icon_info_free()
1473 gtk_icon_theme_choose_icon (GtkIconTheme *icon_theme,
1474 const gchar *icon_names[],
1476 GtkIconLookupFlags flags)
1478 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1479 g_return_val_if_fail (icon_names != NULL, NULL);
1480 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1481 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1483 return choose_icon (icon_theme, icon_names, size, flags);
1488 gtk_icon_theme_error_quark (void)
1490 return g_quark_from_static_string ("gtk-icon-theme-error-quark");
1494 * gtk_icon_theme_load_icon:
1495 * @icon_theme: a #GtkIconTheme
1496 * @icon_name: the name of the icon to lookup
1497 * @size: the desired icon size. The resulting icon may not be
1498 * exactly this size; see gtk_icon_info_load_icon().
1499 * @flags: flags modifying the behavior of the icon lookup
1500 * @error: Location to store error information on failure, or %NULL.
1502 * Looks up an icon in an icon theme, scales it to the given size
1503 * and renders it into a pixbuf. This is a convenience function;
1504 * if more details about the icon are needed, use
1505 * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1507 * Note that you probably want to listen for icon theme changes and
1508 * update the icon. This is usually done by connecting to the
1509 * GtkWidget::style-set signal. If for some reason you do not want to
1510 * update the icon when the icon theme changes, you should consider
1511 * using gdk_pixbuf_copy() to make a private copy of the pixbuf
1512 * returned by this function. Otherwise GTK+ may need to keep the old
1513 * icon theme loaded, which would be a waste of memory.
1515 * Return value: the rendered icon; this may be a newly created icon
1516 * or a new reference to an internal icon, so you must not modify
1517 * the icon. Use g_object_unref() to release your reference to the
1518 * icon. %NULL if the icon isn't found.
1523 gtk_icon_theme_load_icon (GtkIconTheme *icon_theme,
1524 const gchar *icon_name,
1526 GtkIconLookupFlags flags,
1529 GtkIconInfo *icon_info;
1530 GdkPixbuf *pixbuf = NULL;
1532 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1533 g_return_val_if_fail (icon_name != NULL, NULL);
1534 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1535 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1536 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1538 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
1539 flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1542 g_set_error (error, GTK_ICON_THEME_ERROR, GTK_ICON_THEME_NOT_FOUND,
1543 _("Icon '%s' not present in theme"), icon_name);
1547 pixbuf = gtk_icon_info_load_icon (icon_info, error);
1548 gtk_icon_info_free (icon_info);
1554 * gtk_icon_theme_has_icon:
1555 * @icon_theme: a #GtkIconTheme
1556 * @icon_name: the name of an icon
1558 * Checks whether an icon theme includes an icon
1559 * for a particular name.
1561 * Return value: %TRUE if @icon_theme includes an
1562 * icon for @icon_name.
1567 gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
1568 const char *icon_name)
1570 GtkIconThemePrivate *priv;
1573 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1574 g_return_val_if_fail (icon_name != NULL, FALSE);
1576 priv = icon_theme->priv;
1578 ensure_valid_themes (icon_theme);
1580 for (l = priv->dir_mtimes; l; l = l->next)
1582 IconThemeDirMtime *dir_mtime = l->data;
1583 GtkIconCache *cache = dir_mtime->cache;
1585 if (cache && _gtk_icon_cache_has_icon (cache, icon_name))
1589 if (g_hash_table_lookup_extended (priv->all_icons,
1590 icon_name, NULL, NULL))
1593 if (_builtin_cache &&
1594 _gtk_icon_cache_has_icon (_builtin_cache, icon_name))
1597 if (icon_theme_builtin_icons &&
1598 g_hash_table_lookup_extended (icon_theme_builtin_icons,
1599 icon_name, NULL, NULL))
1606 add_size (gpointer key,
1610 gint **res_p = user_data;
1612 **res_p = GPOINTER_TO_INT (key);
1618 * gtk_icon_theme_get_icon_sizes:
1619 * @icon_theme: a #GtkIconTheme
1620 * @icon_name: the name of an icon
1622 * Returns an array of integers describing the sizes at which
1623 * the icon is available without scaling. A size of -1 means
1624 * that the icon is available in a scalable format. The array
1625 * is zero-terminated.
1627 * Return value: An newly allocated array describing the sizes at
1628 * which the icon is available. The array should be freed with g_free()
1629 * when it is no longer needed.
1634 gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
1635 const char *icon_name)
1637 GList *l, *d, *icons;
1641 GtkIconThemePrivate *priv;
1643 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1645 priv = icon_theme->priv;
1647 ensure_valid_themes (icon_theme);
1649 sizes = g_hash_table_new (g_direct_hash, g_direct_equal);
1651 for (l = priv->themes; l; l = l->next)
1653 IconTheme *theme = l->data;
1654 for (d = theme->dirs; d; d = d->next)
1656 IconThemeDir *dir = d->data;
1658 if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
1661 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1662 if (suffix != ICON_SUFFIX_NONE)
1664 if (suffix == ICON_SUFFIX_SVG)
1665 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1667 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1672 for (d = builtin_dirs; d; d = d->next)
1674 IconThemeDir *dir = d->data;
1676 if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
1679 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1680 if (suffix != ICON_SUFFIX_NONE)
1682 if (suffix == ICON_SUFFIX_SVG)
1683 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1685 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1689 if (icon_theme_builtin_icons)
1691 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
1695 BuiltinIcon *icon = icons->data;
1697 g_hash_table_insert (sizes, GINT_TO_POINTER (icon->size), NULL);
1698 icons = icons->next;
1702 r = result = g_new0 (gint, g_hash_table_size (sizes) + 1);
1704 g_hash_table_foreach (sizes, add_size, &r);
1705 g_hash_table_destroy (sizes);
1711 add_key_to_hash (gpointer key,
1715 GHashTable *hash = user_data;
1717 g_hash_table_insert (hash, key, NULL);
1721 add_key_to_list (gpointer key,
1725 GList **list = user_data;
1727 *list = g_list_prepend (*list, g_strdup (key));
1731 * gtk_icon_theme_list_icons:
1732 * @icon_theme: a #GtkIconTheme
1733 * @context: a string identifying a particular type of icon,
1734 * or %NULL to list all icons.
1736 * Lists the icons in the current icon theme. Only a subset
1737 * of the icons can be listed by providing a context string.
1738 * The set of values for the context string is system dependent,
1739 * but will typically include such values as "Applications" and
1742 * Return value: (element-type utf8) (transfer none): a #GList list holding the names of all the
1743 * icons in the theme. You must first free each element
1744 * in the list with g_free(), then free the list itself
1745 * with g_list_free().
1750 gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
1751 const char *context)
1753 GtkIconThemePrivate *priv;
1756 GQuark context_quark;
1758 priv = icon_theme->priv;
1760 ensure_valid_themes (icon_theme);
1764 context_quark = g_quark_try_string (context);
1772 icons = g_hash_table_new (g_str_hash, g_str_equal);
1777 theme_list_icons (l->data, icons, context_quark);
1781 if (context_quark == 0)
1782 g_hash_table_foreach (priv->unthemed_icons,
1788 g_hash_table_foreach (icons,
1792 g_hash_table_destroy (icons);
1798 * gtk_icon_theme_list_contexts:
1799 * @icon_theme: a #GtkIconTheme
1801 * Gets the list of contexts available within the current
1802 * hierarchy of icon themes
1804 * Return value: (element-type utf8) (transfer full): a #GList list holding the names of all the
1805 * contexts in the theme. You must first free each element
1806 * in the list with g_free(), then free the list itself
1807 * with g_list_free().
1812 gtk_icon_theme_list_contexts (GtkIconTheme *icon_theme)
1814 GtkIconThemePrivate *priv;
1815 GHashTable *contexts;
1818 priv = icon_theme->priv;
1820 ensure_valid_themes (icon_theme);
1822 contexts = g_hash_table_new (g_str_hash, g_str_equal);
1827 theme_list_contexts (l->data, contexts);
1833 g_hash_table_foreach (contexts,
1837 g_hash_table_destroy (contexts);
1843 * gtk_icon_theme_get_example_icon_name:
1844 * @icon_theme: a #GtkIconTheme
1846 * Gets the name of an icon that is representative of the
1847 * current theme (for instance, to use when presenting
1848 * a list of themes to the user.)
1850 * Return value: the name of an example icon or %NULL.
1851 * Free with g_free().
1856 gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
1858 GtkIconThemePrivate *priv;
1862 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1864 priv = icon_theme->priv;
1866 ensure_valid_themes (icon_theme);
1873 return g_strdup (theme->example);
1883 rescan_themes (GtkIconTheme *icon_theme)
1885 GtkIconThemePrivate *priv;
1886 IconThemeDirMtime *dir_mtime;
1889 struct stat stat_buf;
1892 priv = icon_theme->priv;
1894 for (d = priv->dir_mtimes; d != NULL; d = d->next)
1896 dir_mtime = d->data;
1898 stat_res = g_stat (dir_mtime->dir, &stat_buf);
1900 /* dir mtime didn't change */
1901 if (stat_res == 0 &&
1902 S_ISDIR (stat_buf.st_mode) &&
1903 dir_mtime->mtime == stat_buf.st_mtime)
1905 /* didn't exist before, and still doesn't */
1906 if (dir_mtime->mtime == 0 &&
1907 (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
1913 g_get_current_time (&tv);
1914 priv->last_stat_time = tv.tv_sec;
1920 * gtk_icon_theme_rescan_if_needed:
1921 * @icon_theme: a #GtkIconTheme
1923 * Checks to see if the icon theme has changed; if it has, any
1924 * currently cached information is discarded and will be reloaded
1925 * next time @icon_theme is accessed.
1927 * Return value: %TRUE if the icon theme has changed and needed
1933 gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
1937 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1939 retval = rescan_themes (icon_theme);
1941 do_theme_change (icon_theme);
1947 theme_destroy (IconTheme *theme)
1949 g_free (theme->display_name);
1950 g_free (theme->comment);
1951 g_free (theme->name);
1952 g_free (theme->example);
1954 g_list_foreach (theme->dirs, (GFunc)theme_dir_destroy, NULL);
1955 g_list_free (theme->dirs);
1961 theme_dir_destroy (IconThemeDir *dir)
1964 _gtk_icon_cache_unref (dir->cache);
1966 g_hash_table_destroy (dir->icons);
1969 g_hash_table_destroy (dir->icon_data);
1971 g_free (dir->subdir);
1976 theme_dir_size_difference (IconThemeDir *dir, int size, gboolean *smaller)
1981 case ICON_THEME_DIR_FIXED:
1982 *smaller = size < dir->size;
1983 return abs (size - dir->size);
1985 case ICON_THEME_DIR_SCALABLE:
1986 *smaller = size < dir->min_size;
1987 if (size < dir->min_size)
1988 return dir->min_size - size;
1989 if (size > dir->max_size)
1990 return size - dir->max_size;
1993 case ICON_THEME_DIR_THRESHOLD:
1994 min = dir->size - dir->threshold;
1995 max = dir->size + dir->threshold;
1996 *smaller = size < min;
2003 case ICON_THEME_DIR_UNTHEMED:
2004 g_assert_not_reached ();
2007 g_assert_not_reached ();
2012 string_from_suffix (IconSuffix suffix)
2016 case ICON_SUFFIX_XPM:
2018 case ICON_SUFFIX_SVG:
2020 case ICON_SUFFIX_PNG:
2023 g_assert_not_reached();
2029 suffix_from_name (const char *name)
2033 if (g_str_has_suffix (name, ".png"))
2034 retval = ICON_SUFFIX_PNG;
2035 else if (g_str_has_suffix (name, ".svg"))
2036 retval = ICON_SUFFIX_SVG;
2037 else if (g_str_has_suffix (name, ".xpm"))
2038 retval = ICON_SUFFIX_XPM;
2040 retval = ICON_SUFFIX_NONE;
2046 best_suffix (IconSuffix suffix,
2049 if ((suffix & ICON_SUFFIX_PNG) != 0)
2050 return ICON_SUFFIX_PNG;
2051 else if (allow_svg && ((suffix & ICON_SUFFIX_SVG) != 0))
2052 return ICON_SUFFIX_SVG;
2053 else if ((suffix & ICON_SUFFIX_XPM) != 0)
2054 return ICON_SUFFIX_XPM;
2056 return ICON_SUFFIX_NONE;
2061 theme_dir_get_icon_suffix (IconThemeDir *dir,
2062 const gchar *icon_name,
2063 gboolean *has_icon_file)
2069 suffix = (IconSuffix)_gtk_icon_cache_get_icon_flags (dir->cache,
2074 *has_icon_file = suffix & HAS_ICON_FILE;
2076 suffix = suffix & ~HAS_ICON_FILE;
2079 suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
2081 GTK_NOTE (ICONTHEME,
2082 g_print ("get_icon_suffix%s %u\n", dir->cache ? " (cached)" : "", suffix));
2087 static GtkIconInfo *
2088 theme_lookup_icon (IconTheme *theme,
2089 const char *icon_name,
2092 gboolean use_builtin)
2095 IconThemeDir *dir, *min_dir;
2097 int min_difference, difference;
2098 BuiltinIcon *closest_builtin = NULL;
2099 gboolean smaller, has_larger, match;
2102 min_difference = G_MAXINT;
2107 /* Builtin icons are logically part of the default theme and
2108 * are searched before other subdirectories of the default theme.
2110 if (use_builtin && strcmp (theme->name, DEFAULT_THEME_NAME) == 0)
2112 closest_builtin = find_builtin_icon (icon_name,
2117 if (min_difference == 0)
2118 return icon_info_new_builtin (closest_builtin);
2120 dirs = builtin_dirs;
2130 GTK_NOTE (ICONTHEME,
2131 g_print ("theme_lookup_icon dir %s\n", dir->dir));
2132 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
2133 if (best_suffix (suffix, allow_svg) != ICON_SUFFIX_NONE)
2135 difference = theme_dir_size_difference (dir, size, &smaller);
2137 if (difference == 0)
2139 if (dir->type == ICON_THEME_DIR_SCALABLE)
2141 /* don't pick scalable if we already found
2142 * a matching non-scalable dir
2152 /* for a matching non-scalable dir keep
2153 * going and look for a closer match
2155 difference = abs (size - dir->size);
2156 if (!match || difference < min_difference)
2159 min_difference = difference;
2162 if (difference == 0)
2171 if (difference < min_difference || smaller)
2173 min_difference = difference;
2175 has_larger = smaller;
2180 if (difference < min_difference && smaller)
2182 min_difference = difference;
2191 if (l == NULL && dirs == builtin_dirs)
2200 GtkIconInfo *icon_info = icon_info_new ();
2201 gboolean has_icon_file = FALSE;
2203 suffix = theme_dir_get_icon_suffix (min_dir, icon_name, &has_icon_file);
2204 suffix = best_suffix (suffix, allow_svg);
2205 g_assert (suffix != ICON_SUFFIX_NONE);
2209 file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
2210 icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
2212 #if defined (G_OS_WIN32) && !defined (_WIN64)
2213 icon_info->cp_filename = g_locale_from_utf8 (icon_info->filename,
2214 -1, NULL, NULL, NULL);
2219 icon_info->filename = NULL;
2220 #if defined (G_OS_WIN32) && !defined (_WIN64)
2221 icon_info->cp_filename = NULL;
2225 if (min_dir->icon_data != NULL)
2226 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2228 if (icon_info->data == NULL && min_dir->cache != NULL)
2230 icon_info->data = _gtk_icon_cache_get_icon_data (min_dir->cache, icon_name, min_dir->subdir_index);
2231 if (icon_info->data)
2233 if (min_dir->icon_data == NULL)
2234 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2235 g_free, (GDestroyNotify)icon_data_free);
2237 g_hash_table_replace (min_dir->icon_data, g_strdup (icon_name), icon_info->data);
2241 if (icon_info->data == NULL && has_icon_file)
2243 gchar *icon_file_name, *icon_file_path;
2245 icon_file_name = g_strconcat (icon_name, ".icon", NULL);
2246 icon_file_path = g_build_filename (min_dir->dir, icon_file_name, NULL);
2248 if (g_file_test (icon_file_path, G_FILE_TEST_IS_REGULAR))
2250 if (min_dir->icon_data == NULL)
2251 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2252 g_free, (GDestroyNotify)icon_data_free);
2253 load_icon_data (min_dir, icon_file_path, icon_file_name);
2255 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2257 g_free (icon_file_name);
2258 g_free (icon_file_path);
2263 icon_info->cache_pixbuf = _gtk_icon_cache_get_icon (min_dir->cache, icon_name,
2264 min_dir->subdir_index);
2267 icon_info->dir_type = min_dir->type;
2268 icon_info->dir_size = min_dir->size;
2269 icon_info->threshold = min_dir->threshold;
2274 if (closest_builtin)
2275 return icon_info_new_builtin (closest_builtin);
2281 theme_list_icons (IconTheme *theme,
2285 GList *l = theme->dirs;
2292 if (context == dir->context ||
2297 _gtk_icon_cache_add_icons (dir->cache,
2304 g_hash_table_foreach (dir->icons,
2314 theme_list_contexts (IconTheme *theme,
2315 GHashTable *contexts)
2317 GList *l = theme->dirs;
2319 const char *context;
2325 context = g_quark_to_string (dir->context);
2326 g_hash_table_replace (contexts, (gpointer) context, NULL);
2333 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
2335 GKeyFile *icon_file;
2343 GError *error = NULL;
2347 icon_file = g_key_file_new ();
2348 g_key_file_set_list_separator (icon_file, ',');
2349 g_key_file_load_from_file (icon_file, path, 0, &error);
2352 g_error_free (error);
2353 g_key_file_free (icon_file);
2358 base_name = strip_suffix (name);
2360 data = g_slice_new0 (GtkIconData);
2361 g_hash_table_replace (dir->icon_data, base_name, data);
2363 ivalues = g_key_file_get_integer_list (icon_file,
2364 "Icon Data", "EmbeddedTextRectangle",
2370 data->has_embedded_rect = TRUE;
2371 data->x0 = ivalues[0];
2372 data->y0 = ivalues[1];
2373 data->x1 = ivalues[2];
2374 data->y1 = ivalues[3];
2380 str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
2383 split = g_strsplit (str, "|", -1);
2385 data->n_attach_points = g_strv_length (split);
2386 data->attach_points = g_new (GdkPoint, data->n_attach_points);
2389 while (split[i] != NULL && i < data->n_attach_points)
2391 split_point = strchr (split[i], ',');
2396 data->attach_points[i].x = atoi (split[i]);
2397 data->attach_points[i].y = atoi (split_point);
2406 data->display_name = g_key_file_get_locale_string (icon_file,
2407 "Icon Data", "DisplayName",
2409 g_key_file_free (icon_file);
2414 scan_directory (GtkIconThemePrivate *icon_theme,
2415 IconThemeDir *dir, char *full_dir)
2420 GTK_NOTE (ICONTHEME,
2421 g_print ("scanning directory %s\n", full_dir));
2422 dir->icons = g_hash_table_new_full (g_str_hash, g_str_equal,
2425 gdir = g_dir_open (full_dir, 0, NULL);
2430 while ((name = g_dir_read_name (gdir)))
2434 IconSuffix suffix, hash_suffix;
2436 if (g_str_has_suffix (name, ".icon"))
2438 if (dir->icon_data == NULL)
2439 dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2440 g_free, (GDestroyNotify)icon_data_free);
2442 path = g_build_filename (full_dir, name, NULL);
2443 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
2444 load_icon_data (dir, path, name);
2451 suffix = suffix_from_name (name);
2452 if (suffix == ICON_SUFFIX_NONE)
2455 base_name = strip_suffix (name);
2457 hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
2458 g_hash_table_replace (icon_theme->all_icons, base_name, NULL);
2459 g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
2466 theme_subdir_load (GtkIconTheme *icon_theme,
2468 GKeyFile *theme_file,
2474 IconThemeDirType type;
2475 char *context_string;
2482 GError *error = NULL;
2483 IconThemeDirMtime *dir_mtime;
2485 size = g_key_file_get_integer (theme_file, subdir, "Size", &error);
2488 g_error_free (error);
2489 g_warning ("Theme directory %s of theme %s has no size field\n",
2490 subdir, theme->name);
2494 type = ICON_THEME_DIR_THRESHOLD;
2495 type_string = g_key_file_get_string (theme_file, subdir, "Type", NULL);
2498 if (strcmp (type_string, "Fixed") == 0)
2499 type = ICON_THEME_DIR_FIXED;
2500 else if (strcmp (type_string, "Scalable") == 0)
2501 type = ICON_THEME_DIR_SCALABLE;
2502 else if (strcmp (type_string, "Threshold") == 0)
2503 type = ICON_THEME_DIR_THRESHOLD;
2505 g_free (type_string);
2509 context_string = g_key_file_get_string (theme_file, subdir, "Context", NULL);
2512 context = g_quark_from_string (context_string);
2513 g_free (context_string);
2516 max_size = g_key_file_get_integer (theme_file, subdir, "MaxSize", &error);
2521 g_error_free (error);
2525 min_size = g_key_file_get_integer (theme_file, subdir, "MinSize", &error);
2530 g_error_free (error);
2534 threshold = g_key_file_get_integer (theme_file, subdir, "Threshold", &error);
2539 g_error_free (error);
2543 for (d = icon_theme->priv->dir_mtimes; d; d = d->next)
2545 dir_mtime = (IconThemeDirMtime *)d->data;
2547 if (dir_mtime->mtime == 0)
2548 continue; /* directory doesn't exist */
2550 full_dir = g_build_filename (dir_mtime->dir, subdir, NULL);
2552 /* First, see if we have a cache for the directory */
2553 if (dir_mtime->cache != NULL || g_file_test (full_dir, G_FILE_TEST_IS_DIR))
2555 if (dir_mtime->cache == NULL)
2557 /* This will return NULL if the cache doesn't exist or is outdated */
2558 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir_mtime->dir);
2561 dir = g_new (IconThemeDir, 1);
2563 dir->context = context;
2565 dir->min_size = min_size;
2566 dir->max_size = max_size;
2567 dir->threshold = threshold;
2568 dir->dir = full_dir;
2569 dir->icon_data = NULL;
2570 dir->subdir = g_strdup (subdir);
2571 if (dir_mtime->cache != NULL)
2573 dir->cache = _gtk_icon_cache_ref (dir_mtime->cache);
2574 dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
2579 dir->subdir_index = -1;
2580 scan_directory (icon_theme->priv, dir, full_dir);
2583 theme->dirs = g_list_prepend (theme->dirs, dir);
2591 icon_data_free (GtkIconData *icon_data)
2593 g_free (icon_data->attach_points);
2594 g_free (icon_data->display_name);
2595 g_slice_free (GtkIconData, icon_data);
2602 gtk_icon_info_get_type (void)
2604 static GType our_type = 0;
2607 our_type = g_boxed_type_register_static (I_("GtkIconInfo"),
2608 (GBoxedCopyFunc) gtk_icon_info_copy,
2609 (GBoxedFreeFunc) gtk_icon_info_free);
2615 static GtkIconInfo *
2616 icon_info_new (void)
2618 GtkIconInfo *icon_info = g_slice_new0 (GtkIconInfo);
2620 icon_info->scale = -1.;
2621 icon_info->ref_count = 1;
2626 static GtkIconInfo *
2627 icon_info_new_builtin (BuiltinIcon *icon)
2629 GtkIconInfo *icon_info = icon_info_new ();
2631 icon_info->cache_pixbuf = g_object_ref (icon->pixbuf);
2632 icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
2633 icon_info->dir_size = icon->size;
2634 icon_info->threshold = 2;
2640 * gtk_icon_info_copy:
2641 * @icon_info: a #GtkIconInfo
2643 * Make a copy of a #GtkIconInfo.
2645 * Return value: the new GtkIconInfo
2650 gtk_icon_info_copy (GtkIconInfo *icon_info)
2653 g_return_val_if_fail (icon_info != NULL, NULL);
2655 icon_info->ref_count++;
2661 * gtk_icon_info_free:
2662 * @icon_info: a #GtkIconInfo
2664 * Free a #GtkIconInfo and associated information
2669 gtk_icon_info_free (GtkIconInfo *icon_info)
2671 g_return_if_fail (icon_info != NULL);
2673 icon_info->ref_count--;
2674 if (icon_info->ref_count > 0)
2677 g_free (icon_info->filename);
2678 #if defined (G_OS_WIN32) && !defined (_WIN64)
2679 g_free (icon_info->cp_filename);
2681 if (icon_info->loadable)
2682 g_object_unref (icon_info->loadable);
2683 g_slist_foreach (icon_info->emblem_infos, (GFunc)gtk_icon_info_free, NULL);
2684 g_slist_free (icon_info->emblem_infos);
2685 if (icon_info->pixbuf)
2686 g_object_unref (icon_info->pixbuf);
2687 if (icon_info->cache_pixbuf)
2688 g_object_unref (icon_info->cache_pixbuf);
2690 g_slice_free (GtkIconInfo, icon_info);
2694 * gtk_icon_info_get_base_size:
2695 * @icon_info: a #GtkIconInfo
2697 * Gets the base size for the icon. The base size
2698 * is a size for the icon that was specified by
2699 * the icon theme creator. This may be different
2700 * than the actual size of image; an example of
2701 * this is small emblem icons that can be attached
2702 * to a larger icon. These icons will be given
2703 * the same base size as the larger icons to which
2704 * they are attached.
2706 * Return value: the base size, or 0, if no base
2707 * size is known for the icon.
2712 gtk_icon_info_get_base_size (GtkIconInfo *icon_info)
2714 g_return_val_if_fail (icon_info != NULL, 0);
2716 return icon_info->dir_size;
2720 * gtk_icon_info_get_filename:
2721 * @icon_info: a #GtkIconInfo
2723 * Gets the filename for the icon. If the
2724 * %GTK_ICON_LOOKUP_USE_BUILTIN flag was passed
2725 * to gtk_icon_theme_lookup_icon(), there may be
2726 * no filename if a builtin icon is returned; in this
2727 * case, you should use gtk_icon_info_get_builtin_pixbuf().
2729 * Return value: the filename for the icon, or %NULL
2730 * if gtk_icon_info_get_builtin_pixbuf() should
2731 * be used instead. The return value is owned by
2732 * GTK+ and should not be modified or freed.
2736 G_CONST_RETURN gchar *
2737 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2739 g_return_val_if_fail (icon_info != NULL, NULL);
2741 return icon_info->filename;
2745 * gtk_icon_info_get_builtin_pixbuf:
2746 * @icon_info: a #GtkIconInfo structure
2748 * Gets the built-in image for this icon, if any. To allow
2749 * GTK+ to use built in icon images, you must pass the
2750 * %GTK_ICON_LOOKUP_USE_BUILTIN to
2751 * gtk_icon_theme_lookup_icon().
2753 * Return value: (transfer none): the built-in image pixbuf, or %NULL. No
2754 * extra reference is added to the returned pixbuf, so if
2755 * you want to keep it around, you must use g_object_ref().
2756 * The returned image must not be modified.
2761 gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
2763 g_return_val_if_fail (icon_info != NULL, NULL);
2765 if (icon_info->filename)
2768 return icon_info->cache_pixbuf;
2771 static gboolean icon_info_ensure_scale_and_pixbuf (GtkIconInfo*, gboolean);
2773 /* Combine the icon with all emblems, the first emblem is placed
2774 * in the southeast corner. Scale emblems to be at most 3/4 of the
2775 * size of the icon itself.
2778 apply_emblems (GtkIconInfo *info)
2780 GdkPixbuf *icon = NULL;
2784 if (info->emblem_infos == NULL)
2787 w = gdk_pixbuf_get_width (info->pixbuf);
2788 h = gdk_pixbuf_get_height (info->pixbuf);
2790 for (l = info->emblem_infos, pos = 0; l; l = l->next, pos++)
2792 GtkIconInfo *emblem_info = l->data;
2794 if (icon_info_ensure_scale_and_pixbuf (emblem_info, FALSE))
2796 GdkPixbuf *emblem = emblem_info->pixbuf;
2798 gint x = 0, y = 0; /* silence compiler */
2801 ew = gdk_pixbuf_get_width (emblem);
2802 eh = gdk_pixbuf_get_height (emblem);
2834 icon = gdk_pixbuf_copy (info->pixbuf);
2839 gdk_pixbuf_composite (emblem, icon, x, y, ew, eh, x, y,
2840 scale, scale, GDK_INTERP_BILINEAR, 255);
2846 g_object_unref (info->pixbuf);
2847 info->pixbuf = icon;
2851 /* This function contains the complicated logic for deciding
2852 * on the size at which to load the icon and loading it at
2856 icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
2857 gboolean scale_only)
2859 int image_width, image_height;
2860 GdkPixbuf *source_pixbuf;
2863 /* First check if we already succeeded have the necessary
2864 * information (or failed earlier)
2866 if (scale_only && icon_info->scale >= 0)
2869 if (icon_info->pixbuf)
2872 if (icon_info->load_error)
2875 /* SVG icons are a special case - we just immediately scale them
2876 * to the desired size
2878 if (icon_info->filename && !icon_info->loadable)
2882 file = g_file_new_for_path (icon_info->filename);
2883 icon_info->loadable = G_LOADABLE_ICON (g_file_icon_new (file));
2884 g_object_unref (file);
2888 if (G_IS_FILE_ICON (icon_info->loadable))
2891 GFileInfo *file_info;
2892 const gchar *content_type;
2894 file = g_file_icon_get_file (G_FILE_ICON (icon_info->loadable));
2895 file_info = g_file_query_info (file,
2896 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
2897 G_FILE_QUERY_INFO_NONE,
2901 content_type = g_file_info_get_content_type (file_info);
2903 if (content_type && strcmp (content_type, "image/svg+xml") == 0)
2906 g_object_unref (file_info);
2912 GInputStream *stream;
2914 icon_info->scale = icon_info->desired_size / 1000.;
2919 stream = g_loadable_icon_load (icon_info->loadable,
2920 icon_info->desired_size,
2922 &icon_info->load_error);
2925 icon_info->pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
2926 icon_info->desired_size,
2927 icon_info->desired_size,
2930 &icon_info->load_error);
2931 g_object_unref (stream);
2934 if (!icon_info->pixbuf)
2937 apply_emblems (icon_info);
2942 /* In many cases, the scale can be determined without actual access
2943 * to the icon file. This is generally true when we have a size
2944 * for the directory where the icon is; the image size doesn't
2945 * matter in that case.
2947 if (icon_info->forced_size)
2948 icon_info->scale = -1;
2949 else if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
2950 icon_info->scale = 1.0;
2951 else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
2953 if (icon_info->desired_size >= icon_info->dir_size - icon_info->threshold &&
2954 icon_info->desired_size <= icon_info->dir_size + icon_info->threshold)
2955 icon_info->scale = 1.0;
2956 else if (icon_info->dir_size > 0)
2957 icon_info->scale =(gdouble) icon_info->desired_size / icon_info->dir_size;
2959 else if (icon_info->dir_type == ICON_THEME_DIR_SCALABLE)
2961 if (icon_info->dir_size > 0)
2962 icon_info->scale = (gdouble) icon_info->desired_size / icon_info->dir_size;
2965 if (icon_info->scale >= 0. && scale_only)
2968 /* At this point, we need to actually get the icon; either from the
2969 * builtin image or by loading the file
2971 source_pixbuf = NULL;
2972 if (icon_info->cache_pixbuf)
2973 source_pixbuf = g_object_ref (icon_info->cache_pixbuf);
2976 GInputStream *stream;
2978 stream = g_loadable_icon_load (icon_info->loadable,
2979 icon_info->desired_size,
2981 &icon_info->load_error);
2984 source_pixbuf = gdk_pixbuf_new_from_stream (stream,
2986 &icon_info->load_error);
2987 g_object_unref (stream);
2994 /* Do scale calculations that depend on the image size
2996 image_width = gdk_pixbuf_get_width (source_pixbuf);
2997 image_height = gdk_pixbuf_get_height (source_pixbuf);
2999 if (icon_info->scale < 0.0)
3001 gint image_size = MAX (image_width, image_height);
3003 icon_info->scale = (gdouble)icon_info->desired_size / (gdouble)image_size;
3005 icon_info->scale = 1.0;
3007 if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED &&
3008 !icon_info->forced_size)
3009 icon_info->scale = MIN (icon_info->scale, 1.0);
3012 /* We don't short-circuit out here for scale_only, since, now
3013 * we've loaded the icon, we might as well go ahead and finish
3014 * the job. This is a bit of a waste when we scale here
3015 * and never get the final pixbuf; at the cost of a bit of
3016 * extra complexity, we could keep the source pixbuf around
3017 * but not actually scale it until needed.
3019 if (icon_info->scale == 1.0)
3020 icon_info->pixbuf = source_pixbuf;
3023 icon_info->pixbuf = gdk_pixbuf_scale_simple (source_pixbuf,
3024 0.5 + image_width * icon_info->scale,
3025 0.5 + image_height * icon_info->scale,
3026 GDK_INTERP_BILINEAR);
3027 g_object_unref (source_pixbuf);
3030 apply_emblems (icon_info);
3036 * gtk_icon_info_load_icon:
3037 * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
3038 * @error: location to store error information on failure, or %NULL.
3040 * Renders an icon previously looked up in an icon theme using
3041 * gtk_icon_theme_lookup_icon(); the size will be based on the size
3042 * passed to gtk_icon_theme_lookup_icon(). Note that the resulting
3043 * pixbuf may not be exactly this size; an icon theme may have icons
3044 * that differ slightly from their nominal sizes, and in addition GTK+
3045 * will avoid scaling icons that it considers sufficiently close to the
3046 * requested size or for which the source image would have to be scaled
3047 * up too far. (This maintains sharpness.). This behaviour can be changed
3048 * by passing the %GTK_ICON_LOOKUP_FORCE_SIZE flag when obtaining
3049 * the #GtkIconInfo. If this flag has been specified, the pixbuf
3050 * returned by this function will be scaled to the exact size.
3052 * Return value: the rendered icon; this may be a newly created icon
3053 * or a new reference to an internal icon, so you must not modify
3054 * the icon. Use g_object_unref() to release your reference to the
3060 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
3063 g_return_val_if_fail (icon_info != NULL, NULL);
3064 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
3066 if (!icon_info_ensure_scale_and_pixbuf (icon_info, FALSE))
3068 if (icon_info->load_error)
3069 g_propagate_error (error, icon_info->load_error);
3071 g_set_error_literal (error,
3072 GTK_ICON_THEME_ERROR,
3073 GTK_ICON_THEME_NOT_FOUND,
3074 _("Failed to load icon"));
3079 return g_object_ref (icon_info->pixbuf);
3083 * gtk_icon_info_set_raw_coordinates:
3084 * @icon_info: a #GtkIconInfo
3085 * @raw_coordinates: whether the coordinates of embedded rectangles
3086 * and attached points should be returned in their original
3089 * Sets whether the coordinates returned by gtk_icon_info_get_embedded_rect()
3090 * and gtk_icon_info_get_attach_points() should be returned in their
3091 * original form as specified in the icon theme, instead of scaled
3092 * appropriately for the pixbuf returned by gtk_icon_info_load_icon().
3094 * Raw coordinates are somewhat strange; they are specified to be with
3095 * respect to the unscaled pixmap for PNG and XPM icons, but for SVG
3096 * icons, they are in a 1000x1000 coordinate space that is scaled
3097 * to the final size of the icon. You can determine if the icon is an SVG
3098 * icon by using gtk_icon_info_get_filename(), and seeing if it is non-%NULL
3099 * and ends in '.svg'.
3101 * This function is provided primarily to allow compatibility wrappers
3102 * for older API's, and is not expected to be useful for applications.
3107 gtk_icon_info_set_raw_coordinates (GtkIconInfo *icon_info,
3108 gboolean raw_coordinates)
3110 g_return_if_fail (icon_info != NULL);
3112 icon_info->raw_coordinates = raw_coordinates != FALSE;
3115 /* Scale coordinates from the icon data prior to returning
3119 icon_info_scale_point (GtkIconInfo *icon_info,
3125 if (icon_info->raw_coordinates)
3132 if (!icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3135 *x_out = 0.5 + x * icon_info->scale;
3136 *y_out = 0.5 + y * icon_info->scale;
3143 * gtk_icon_info_get_embedded_rect:
3144 * @icon_info: a #GtkIconInfo
3145 * @rectangle: #GdkRectangle in which to store embedded
3146 * rectangle coordinates; coordinates are only stored
3147 * when this function returns %TRUE.
3149 * Gets the coordinates of a rectangle within the icon
3150 * that can be used for display of information such
3151 * as a preview of the contents of a text file.
3152 * See gtk_icon_info_set_raw_coordinates() for further
3153 * information about the coordinate system.
3155 * Return value: %TRUE if the icon has an embedded rectangle
3160 gtk_icon_info_get_embedded_rect (GtkIconInfo *icon_info,
3161 GdkRectangle *rectangle)
3163 g_return_val_if_fail (icon_info != NULL, FALSE);
3165 if (icon_info->data && icon_info->data->has_embedded_rect &&
3166 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3168 gint scaled_x0, scaled_y0;
3169 gint scaled_x1, scaled_y1;
3173 icon_info_scale_point (icon_info,
3174 icon_info->data->x0, icon_info->data->y0,
3175 &scaled_x0, &scaled_y0);
3176 icon_info_scale_point (icon_info,
3177 icon_info->data->x1, icon_info->data->y1,
3178 &scaled_x1, &scaled_y1);
3180 rectangle->x = scaled_x0;
3181 rectangle->y = scaled_y0;
3182 rectangle->width = scaled_x1 - rectangle->x;
3183 rectangle->height = scaled_y1 - rectangle->y;
3193 * gtk_icon_info_get_attach_points:
3194 * @icon_info: a #GtkIconInfo
3195 * @points: (array length=n_points) (out): location to store pointer to an array of points, or %NULL
3196 * free the array of points with g_free().
3197 * @n_points: location to store the number of points in @points, or %NULL
3199 * Fetches the set of attach points for an icon. An attach point
3200 * is a location in the icon that can be used as anchor points for attaching
3201 * emblems or overlays to the icon.
3203 * Return value: %TRUE if there are any attach points for the icon.
3208 gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
3212 g_return_val_if_fail (icon_info != NULL, FALSE);
3214 if (icon_info->data && icon_info->data->n_attach_points &&
3215 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3221 *points = g_new (GdkPoint, icon_info->data->n_attach_points);
3222 for (i = 0; i < icon_info->data->n_attach_points; i++)
3223 icon_info_scale_point (icon_info,
3224 icon_info->data->attach_points[i].x,
3225 icon_info->data->attach_points[i].y,
3231 *n_points = icon_info->data->n_attach_points;
3247 * gtk_icon_info_get_display_name:
3248 * @icon_info: a #GtkIconInfo
3250 * Gets the display name for an icon. A display name is a
3251 * string to be used in place of the icon name in a user
3252 * visible context like a list of icons.
3254 * Return value: the display name for the icon or %NULL, if
3255 * the icon doesn't have a specified display name. This value
3256 * is owned @icon_info and must not be modified or free.
3260 G_CONST_RETURN gchar *
3261 gtk_icon_info_get_display_name (GtkIconInfo *icon_info)
3263 g_return_val_if_fail (icon_info != NULL, NULL);
3265 if (icon_info->data)
3266 return icon_info->data->display_name;
3277 * gtk_icon_theme_add_builtin_icon:
3278 * @icon_name: the name of the icon to register
3279 * @size: the size at which to register the icon (different
3280 * images can be registered for the same icon name
3281 * at different sizes.)
3282 * @pixbuf: #GdkPixbuf that contains the image to use
3285 * Registers a built-in icon for icon theme lookups. The idea
3286 * of built-in icons is to allow an application or library
3287 * that uses themed icons to function requiring files to
3288 * be present in the file system. For instance, the default
3289 * images for all of GTK+'s stock icons are registered
3292 * In general, if you use gtk_icon_theme_add_builtin_icon()
3293 * you should also install the icon in the icon theme, so
3294 * that the icon is generally available.
3296 * This function will generally be used with pixbufs loaded
3297 * via gdk_pixbuf_new_from_inline().
3302 gtk_icon_theme_add_builtin_icon (const gchar *icon_name,
3306 BuiltinIcon *default_icon;
3310 g_return_if_fail (icon_name != NULL);
3311 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3313 if (!icon_theme_builtin_icons)
3314 icon_theme_builtin_icons = g_hash_table_new (g_str_hash, g_str_equal);
3316 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
3318 key = g_strdup (icon_name);
3320 key = (gpointer)icon_name; /* Won't get stored */
3322 default_icon = g_new (BuiltinIcon, 1);
3323 default_icon->size = size;
3324 default_icon->pixbuf = g_object_ref (pixbuf);
3325 icons = g_slist_prepend (icons, default_icon);
3327 /* Replaces value, leaves key untouched
3329 g_hash_table_insert (icon_theme_builtin_icons, key, icons);
3332 /* Look up a builtin icon; the min_difference_p and
3333 * has_larger_p out parameters allow us to combine
3334 * this lookup with searching through the actual directories
3335 * of the "hicolor" icon theme. See theme_lookup_icon()
3336 * for how they are used.
3338 static BuiltinIcon *
3339 find_builtin_icon (const gchar *icon_name,
3341 gint *min_difference_p,
3342 gboolean *has_larger_p)
3344 GSList *icons = NULL;
3345 gint min_difference = G_MAXINT;
3346 gboolean has_larger = FALSE;
3347 BuiltinIcon *min_icon = NULL;
3349 if (!icon_theme_builtin_icons)
3352 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
3356 BuiltinIcon *default_icon = icons->data;
3357 int min, max, difference;
3360 min = default_icon->size - 2;
3361 max = default_icon->size + 2;
3362 smaller = size < min;
3364 difference = min - size;
3365 else if (size > max)
3366 difference = size - max;
3370 if (difference == 0)
3372 min_icon = default_icon;
3378 if (difference < min_difference || smaller)
3380 min_difference = difference;
3381 min_icon = default_icon;
3382 has_larger = smaller;
3387 if (difference < min_difference && smaller)
3389 min_difference = difference;
3390 min_icon = default_icon;
3394 icons = icons->next;
3397 if (min_difference_p)
3398 *min_difference_p = min_difference;
3400 *has_larger_p = has_larger;
3406 _gtk_icon_theme_check_reload (GdkDisplay *display)
3410 GtkIconTheme *icon_theme;
3412 n_screens = gdk_display_get_n_screens (display);
3414 for (i = 0; i < n_screens; i++)
3416 screen = gdk_display_get_screen (display, i);
3418 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
3421 icon_theme->priv->check_reload = TRUE;
3422 ensure_valid_themes (icon_theme);
3423 icon_theme->priv->check_reload = FALSE;
3430 * gtk_icon_theme_lookup_by_gicon:
3431 * @icon_theme: a #GtkIconTheme
3432 * @icon: the #GIcon to look up
3433 * @size: desired icon size
3434 * @flags: flags modifying the behavior of the icon lookup
3436 * Looks up an icon and returns a structure containing
3437 * information such as the filename of the icon.
3438 * The icon can then be rendered into a pixbuf using
3439 * gtk_icon_info_load_icon().
3441 * Return value: a #GtkIconInfo structure containing
3442 * information about the icon, or %NULL if the icon
3443 * wasn't found. Free with gtk_icon_info_free()
3448 gtk_icon_theme_lookup_by_gicon (GtkIconTheme *icon_theme,
3451 GtkIconLookupFlags flags)
3455 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
3456 g_return_val_if_fail (G_IS_ICON (icon), NULL);
3458 if (G_IS_LOADABLE_ICON (icon))
3460 info = icon_info_new ();
3461 info->loadable = G_LOADABLE_ICON (g_object_ref (icon));
3463 info->dir_type = ICON_THEME_DIR_UNTHEMED;
3464 info->dir_size = size;
3465 info->desired_size = size;
3466 info->threshold = 2;
3467 info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
3471 else if (G_IS_THEMED_ICON (icon))
3473 const gchar **names;
3475 names = (const gchar **)g_themed_icon_get_names (G_THEMED_ICON (icon));
3476 info = gtk_icon_theme_choose_icon (icon_theme, names, size, flags);
3480 else if (G_IS_EMBLEMED_ICON (icon))
3482 GIcon *base, *emblem;
3484 GtkIconInfo *emblem_info;
3486 base = g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon));
3487 info = gtk_icon_theme_lookup_by_gicon (icon_theme, base, size, flags);
3490 list = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon));
3491 for (l = list; l; l = l->next)
3493 emblem = g_emblem_get_icon (G_EMBLEM (l->data));
3494 emblem_info = gtk_icon_theme_lookup_by_gicon (icon_theme, emblem, size / 2, flags);
3496 info->emblem_infos = g_slist_prepend (info->emblem_infos, emblem_info);
3507 * gtk_icon_info_new_for_pixbuf:
3508 * @icon_theme: a #GtkIconTheme
3509 * @pixbuf: the pixbuf to wrap in a #GtkIconInfo
3511 * Creates a #GtkIconInfo for a #GdkPixbuf.
3513 * Returns: a #GtkIconInfo
3518 gtk_icon_info_new_for_pixbuf (GtkIconTheme *icon_theme,
3523 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
3524 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
3526 info = icon_info_new ();
3527 info->pixbuf = g_object_ref (pixbuf);
3529 info->dir_type = ICON_THEME_DIR_UNTHEMED;
3534 #if defined (G_OS_WIN32) && !defined (_WIN64)
3536 /* DLL ABI stability backward compatibility versions */
3538 #undef gtk_icon_theme_set_search_path
3541 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
3542 const gchar *path[],
3545 const gchar **utf8_path;
3548 utf8_path = g_new (const gchar *, n_elements);
3550 for (i = 0; i < n_elements; i++)
3551 utf8_path[i] = g_locale_to_utf8 (path[i], -1, NULL, NULL, NULL);
3553 gtk_icon_theme_set_search_path_utf8 (icon_theme, utf8_path, n_elements);
3555 for (i = 0; i < n_elements; i++)
3556 g_free ((gchar *) utf8_path[i]);
3561 #undef gtk_icon_theme_get_search_path
3564 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
3570 gtk_icon_theme_get_search_path_utf8 (icon_theme, path, &n);
3577 for (i = 0; i < n; i++)
3579 gchar *tem = (*path)[i];
3581 (*path)[i] = g_locale_from_utf8 ((*path)[i], -1, NULL, NULL, NULL);
3587 #undef gtk_icon_theme_append_search_path
3590 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
3593 gchar *utf8_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
3595 gtk_icon_theme_append_search_path_utf8 (icon_theme, utf8_path);
3600 #undef gtk_icon_theme_prepend_search_path
3603 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
3606 gchar *utf8_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
3608 gtk_icon_theme_prepend_search_path_utf8 (icon_theme, utf8_path);
3613 #undef gtk_icon_info_get_filename
3615 G_CONST_RETURN gchar *
3616 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
3618 g_return_val_if_fail (icon_info != NULL, NULL);
3620 return icon_info->cp_filename;
3625 #define __GTK_ICON_THEME_C__
3626 #include "gtkaliasdef.c"