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"
52 #define DEFAULT_THEME_NAME "hicolor"
57 ICON_THEME_DIR_SCALABLE,
58 ICON_THEME_DIR_THRESHOLD,
59 ICON_THEME_DIR_UNTHEMED
62 /* In reverse search order: */
66 ICON_SUFFIX_XPM = 1 << 0,
67 ICON_SUFFIX_SVG = 1 << 1,
68 ICON_SUFFIX_PNG = 1 << 2,
69 HAS_ICON_FILE = 1 << 3
73 struct _GtkIconThemePrivate
75 guint custom_theme : 1;
76 guint is_screen_singleton : 1;
77 guint pixbuf_supports_svg : 1;
78 guint themes_valid : 1;
79 guint check_reload : 1;
80 guint loading_themes : 1;
87 /* A list of all the themes needed to look up icons.
88 * In search order, without duplicates
91 GHashTable *unthemed_icons;
93 /* Note: The keys of this hashtable are owned by the
94 * themedir and unthemed hashtables.
96 GHashTable *all_icons;
98 /* GdkScreen for the icon theme (may be NULL)
102 /* time when we last stat:ed for theme changes */
106 gulong reset_styles_idle;
111 /* Information about the source
114 GLoadableIcon *loadable;
115 GSList *emblem_infos;
117 /* Cache pixbuf (if there is any) */
118 GdkPixbuf *cache_pixbuf;
122 /* Information about the directory where
123 * the source was found
125 IconThemeDirType dir_type;
129 /* Parameters influencing the scaled icon
132 guint raw_coordinates : 1;
133 guint forced_size : 1;
135 /* Cached information if we go ahead and try to load
141 gboolean emblems_applied;
153 /* In search order */
159 IconThemeDirType type;
174 GHashTable *icon_data;
180 char *no_svg_filename;
192 time_t mtime; /* 0 == not existing or not a dir */
197 static void gtk_icon_theme_finalize (GObject *object);
198 static void theme_dir_destroy (IconThemeDir *dir);
200 static void theme_destroy (IconTheme *theme);
201 static GtkIconInfo *theme_lookup_icon (IconTheme *theme,
202 const char *icon_name,
205 gboolean use_default_icons);
206 static void theme_list_icons (IconTheme *theme,
209 static void theme_list_contexts (IconTheme *theme,
210 GHashTable *contexts);
211 static void theme_subdir_load (GtkIconTheme *icon_theme,
213 GKeyFile *theme_file,
215 static void do_theme_change (GtkIconTheme *icon_theme);
217 static void blow_themes (GtkIconTheme *icon_themes);
218 static gboolean rescan_themes (GtkIconTheme *icon_themes);
220 static void icon_data_free (GtkIconData *icon_data);
221 static void load_icon_data (IconThemeDir *dir,
225 static IconSuffix theme_dir_get_icon_suffix (IconThemeDir *dir,
226 const gchar *icon_name,
227 gboolean *has_icon_file);
230 static GtkIconInfo *icon_info_new (void);
231 static GtkIconInfo *icon_info_new_builtin (BuiltinIcon *icon);
233 static IconSuffix suffix_from_name (const char *name);
235 static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
237 gint *min_difference_p,
238 gboolean *has_larger_p);
240 static guint signal_changed = 0;
242 static GHashTable *icon_theme_builtin_icons;
244 /* also used in gtkiconfactory.c */
245 GtkIconCache *_builtin_cache = NULL;
246 static GList *builtin_dirs = NULL;
248 G_DEFINE_TYPE (GtkIconTheme, gtk_icon_theme, G_TYPE_OBJECT)
251 * gtk_icon_theme_new:
253 * Creates a new icon theme object. Icon theme objects are used
254 * to lookup up an icon by name in a particular icon theme.
255 * Usually, you'll want to use gtk_icon_theme_get_default()
256 * or gtk_icon_theme_get_for_screen() rather than creating
257 * a new icon theme object for scratch.
259 * Return value: the newly created #GtkIconTheme object.
264 gtk_icon_theme_new (void)
266 return g_object_new (GTK_TYPE_ICON_THEME, NULL);
270 * gtk_icon_theme_get_default:
272 * Gets the icon theme for the default screen. See
273 * gtk_icon_theme_get_for_screen().
275 * Return value: (transfer none): A unique #GtkIconTheme associated with
276 * the default screen. This icon theme is associated with
277 * the screen and can be used as long as the screen
278 * is open. Do not ref or unref it.
283 gtk_icon_theme_get_default (void)
285 return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
289 * gtk_icon_theme_get_for_screen:
290 * @screen: a #GdkScreen
292 * Gets the icon theme object associated with @screen; if this
293 * function has not previously been called for the given
294 * screen, a new icon theme object will be created and
295 * associated with the screen. Icon theme objects are
296 * fairly expensive to create, so using this function
297 * is usually a better choice than calling than gtk_icon_theme_new()
298 * and setting the screen yourself; by using this function
299 * a single icon theme object will be shared between users.
301 * Return value: (transfer none): A unique #GtkIconTheme associated with
302 * the given screen. This icon theme is associated with
303 * the screen and can be used as long as the screen
304 * is open. Do not ref or unref it.
309 gtk_icon_theme_get_for_screen (GdkScreen *screen)
311 GtkIconTheme *icon_theme;
313 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
315 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
318 GtkIconThemePrivate *priv;
320 icon_theme = gtk_icon_theme_new ();
321 gtk_icon_theme_set_screen (icon_theme, screen);
323 priv = icon_theme->priv;
324 priv->is_screen_singleton = TRUE;
326 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), icon_theme);
333 gtk_icon_theme_class_init (GtkIconThemeClass *klass)
335 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
337 gobject_class->finalize = gtk_icon_theme_finalize;
340 * GtkIconTheme::changed
341 * @icon_theme: the icon theme
343 * Emitted when the current icon theme is switched or GTK+ detects
344 * that a change has occurred in the contents of the current
347 signal_changed = g_signal_new (I_("changed"),
348 G_TYPE_FROM_CLASS (klass),
350 G_STRUCT_OFFSET (GtkIconThemeClass, changed),
352 g_cclosure_marshal_VOID__VOID,
355 g_type_class_add_private (klass, sizeof (GtkIconThemePrivate));
359 /* Callback when the display that the icon theme is attached to
360 * is closed; unset the screen, and if it's the unique theme
361 * for the screen, drop the reference
364 display_closed (GdkDisplay *display,
366 GtkIconTheme *icon_theme)
368 GtkIconThemePrivate *priv = icon_theme->priv;
369 GdkScreen *screen = priv->screen;
370 gboolean was_screen_singleton = priv->is_screen_singleton;
372 if (was_screen_singleton)
374 g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), NULL);
375 priv->is_screen_singleton = FALSE;
378 gtk_icon_theme_set_screen (icon_theme, NULL);
380 if (was_screen_singleton)
382 g_object_unref (icon_theme);
387 update_current_theme (GtkIconTheme *icon_theme)
389 #define theme_changed(_old, _new) \
390 ((_old && !_new) || (!_old && _new) || \
391 (_old && _new && strcmp (_old, _new) != 0))
392 GtkIconThemePrivate *priv = icon_theme->priv;
394 if (!priv->custom_theme)
397 gchar *fallback_theme = NULL;
398 gboolean changed = FALSE;
402 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
403 g_object_get (settings,
404 "gtk-icon-theme-name", &theme,
405 "gtk-fallback-icon-theme", &fallback_theme, NULL);
408 /* ensure that the current theme (even when just the default)
409 * is searched before any fallback theme
411 if (!theme && fallback_theme)
412 theme = g_strdup (DEFAULT_THEME_NAME);
414 if (theme_changed (priv->current_theme, theme))
416 g_free (priv->current_theme);
417 priv->current_theme = theme;
423 if (theme_changed (priv->fallback_theme, fallback_theme))
425 g_free (priv->fallback_theme);
426 priv->fallback_theme = fallback_theme;
430 g_free (fallback_theme);
433 do_theme_change (icon_theme);
438 /* Callback when the icon theme GtkSetting changes
441 theme_changed (GtkSettings *settings,
443 GtkIconTheme *icon_theme)
445 update_current_theme (icon_theme);
449 unset_screen (GtkIconTheme *icon_theme)
451 GtkIconThemePrivate *priv = icon_theme->priv;
452 GtkSettings *settings;
457 settings = gtk_settings_get_for_screen (priv->screen);
458 display = gdk_screen_get_display (priv->screen);
460 g_signal_handlers_disconnect_by_func (display,
461 (gpointer) display_closed,
463 g_signal_handlers_disconnect_by_func (settings,
464 (gpointer) theme_changed,
472 * gtk_icon_theme_set_screen:
473 * @icon_theme: a #GtkIconTheme
474 * @screen: a #GdkScreen
476 * Sets the screen for an icon theme; the screen is used
477 * to track the user's currently configured icon theme,
478 * which might be different for different screens.
483 gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
486 GtkIconThemePrivate *priv;
487 GtkSettings *settings;
490 g_return_if_fail (GTK_ICON_THEME (icon_theme));
491 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
493 priv = icon_theme->priv;
495 unset_screen (icon_theme);
499 display = gdk_screen_get_display (screen);
500 settings = gtk_settings_get_for_screen (screen);
502 priv->screen = screen;
504 g_signal_connect (display, "closed",
505 G_CALLBACK (display_closed), icon_theme);
506 g_signal_connect (settings, "notify::gtk-icon-theme-name",
507 G_CALLBACK (theme_changed), icon_theme);
508 g_signal_connect (settings, "notify::gtk-fallback-icon-theme-name",
509 G_CALLBACK (theme_changed), icon_theme);
512 update_current_theme (icon_theme);
515 /* Checks whether a loader for SVG files has been registered
519 pixbuf_supports_svg (void)
523 static gint found_svg = -1;
528 formats = gdk_pixbuf_get_formats ();
531 for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
533 gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
536 for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
538 if (strcmp (*mime_type, "image/svg") == 0)
542 g_strfreev (mime_types);
545 g_slist_free (formats);
551 gtk_icon_theme_init (GtkIconTheme *icon_theme)
553 GtkIconThemePrivate *priv;
554 const gchar * const *xdg_data_dirs;
557 priv = G_TYPE_INSTANCE_GET_PRIVATE (icon_theme,
559 GtkIconThemePrivate);
560 icon_theme->priv = priv;
562 priv->custom_theme = FALSE;
564 xdg_data_dirs = g_get_system_data_dirs ();
565 for (i = 0; xdg_data_dirs[i]; i++) ;
567 priv->search_path_len = 2 * i + 2;
569 priv->search_path = g_new (char *, priv->search_path_len);
572 priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
573 priv->search_path[i++] = g_build_filename (g_get_user_data_dir (), "icons", NULL);
575 for (j = 0; xdg_data_dirs[j]; j++)
576 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
578 for (j = 0; xdg_data_dirs[j]; j++)
579 priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "pixmaps", NULL);
581 priv->themes_valid = FALSE;
583 priv->unthemed_icons = NULL;
585 priv->pixbuf_supports_svg = pixbuf_supports_svg ();
589 free_dir_mtime (IconThemeDirMtime *dir_mtime)
591 if (dir_mtime->cache)
592 _gtk_icon_cache_unref (dir_mtime->cache);
594 g_free (dir_mtime->dir);
595 g_slice_free (IconThemeDirMtime, dir_mtime);
600 reset_styles_idle (gpointer user_data)
602 GtkIconTheme *icon_theme;
603 GtkIconThemePrivate *priv;
605 icon_theme = GTK_ICON_THEME (user_data);
606 priv = icon_theme->priv;
608 if (priv->screen && priv->is_screen_singleton)
609 gtk_style_context_reset_widgets (priv->screen);
611 priv->reset_styles_idle = 0;
617 do_theme_change (GtkIconTheme *icon_theme)
619 GtkIconThemePrivate *priv = icon_theme->priv;
621 if (!priv->themes_valid)
625 g_print ("change to icon theme \"%s\"\n", priv->current_theme));
626 blow_themes (icon_theme);
627 g_signal_emit (icon_theme, signal_changed, 0);
629 if (!priv->reset_styles_idle)
630 priv->reset_styles_idle =
631 gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2,
632 reset_styles_idle, icon_theme, NULL);
636 blow_themes (GtkIconTheme *icon_theme)
638 GtkIconThemePrivate *priv = icon_theme->priv;
640 if (priv->themes_valid)
642 g_hash_table_destroy (priv->all_icons);
643 g_list_foreach (priv->themes, (GFunc)theme_destroy, NULL);
644 g_list_free (priv->themes);
645 g_list_foreach (priv->dir_mtimes, (GFunc)free_dir_mtime, NULL);
646 g_list_free (priv->dir_mtimes);
647 g_hash_table_destroy (priv->unthemed_icons);
650 priv->unthemed_icons = NULL;
651 priv->dir_mtimes = NULL;
652 priv->all_icons = NULL;
653 priv->themes_valid = FALSE;
657 gtk_icon_theme_finalize (GObject *object)
659 GtkIconTheme *icon_theme;
660 GtkIconThemePrivate *priv;
663 icon_theme = GTK_ICON_THEME (object);
664 priv = icon_theme->priv;
666 if (priv->reset_styles_idle)
668 g_source_remove (priv->reset_styles_idle);
669 priv->reset_styles_idle = 0;
672 unset_screen (icon_theme);
674 g_free (priv->current_theme);
675 priv->current_theme = NULL;
677 for (i = 0; i < priv->search_path_len; i++)
678 g_free (priv->search_path[i]);
680 g_free (priv->search_path);
681 priv->search_path = NULL;
683 blow_themes (icon_theme);
685 G_OBJECT_CLASS (gtk_icon_theme_parent_class)->finalize (object);
689 * gtk_icon_theme_set_search_path:
690 * @icon_theme: a #GtkIconTheme
691 * @path: (array length=n_elements) (element-type filename): array of
692 * directories that are searched for icon themes
693 * @n_elements: number of elements in @path.
695 * Sets the search path for the icon theme object. When looking
696 * for an icon theme, GTK+ will search for a subdirectory of
697 * one or more of the directories in @path with the same name
698 * as the icon theme. (Themes from multiple of the path elements
699 * are combined to allow themes to be extended by adding icons
700 * in the user's home directory.)
702 * In addition if an icon found isn't found either in the current
703 * icon theme or the default icon theme, and an image file with
704 * the right name is found directly in one of the elements of
705 * @path, then that image will be used for the icon name.
706 * (This is legacy feature, and new icons should be put
707 * into the default icon theme, which is called DEFAULT_THEME_NAME,
708 * rather than directly on the icon path.)
713 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
717 GtkIconThemePrivate *priv;
720 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
722 priv = icon_theme->priv;
723 for (i = 0; i < priv->search_path_len; i++)
724 g_free (priv->search_path[i]);
726 g_free (priv->search_path);
728 priv->search_path = g_new (gchar *, n_elements);
729 priv->search_path_len = n_elements;
731 for (i = 0; i < priv->search_path_len; i++)
732 priv->search_path[i] = g_strdup (path[i]);
734 do_theme_change (icon_theme);
739 * gtk_icon_theme_get_search_path:
740 * @icon_theme: a #GtkIconTheme
742 * @path: (allow-none) (array length=n_elements) (element-type filename) (out):
743 * location to store a list of icon theme path directories or %NULL .
744 * The stored value should be freed with g_strfreev().
745 * @n_elements: location to store number of elements
748 * Gets the current search path. See gtk_icon_theme_set_search_path().
753 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
757 GtkIconThemePrivate *priv;
760 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
762 priv = icon_theme->priv;
765 *n_elements = priv->search_path_len;
769 *path = g_new (gchar *, priv->search_path_len + 1);
770 for (i = 0; i < priv->search_path_len; i++)
771 (*path)[i] = g_strdup (priv->search_path[i]);
777 * gtk_icon_theme_append_search_path:
778 * @icon_theme: a #GtkIconTheme
779 * @path: (type filename): directory name to append to the icon path
781 * Appends a directory to the search path.
782 * See gtk_icon_theme_set_search_path().
787 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
790 GtkIconThemePrivate *priv;
792 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
793 g_return_if_fail (path != NULL);
795 priv = icon_theme->priv;
797 priv->search_path_len++;
799 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
800 priv->search_path[priv->search_path_len-1] = g_strdup (path);
802 do_theme_change (icon_theme);
806 * gtk_icon_theme_prepend_search_path:
807 * @icon_theme: a #GtkIconTheme
808 * @path: (type filename): directory name to prepend to the icon path
810 * Prepends a directory to the search path.
811 * See gtk_icon_theme_set_search_path().
816 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
819 GtkIconThemePrivate *priv;
822 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
823 g_return_if_fail (path != NULL);
825 priv = icon_theme->priv;
827 priv->search_path_len++;
828 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
830 for (i = priv->search_path_len - 1; i > 0; i--)
831 priv->search_path[i] = priv->search_path[i - 1];
833 priv->search_path[0] = g_strdup (path);
835 do_theme_change (icon_theme);
839 * gtk_icon_theme_set_custom_theme:
840 * @icon_theme: a #GtkIconTheme
841 * @theme_name: (allow-none): name of icon theme to use instead of
842 * configured theme, or %NULL to unset a previously set custom theme
844 * Sets the name of the icon theme that the #GtkIconTheme object uses
845 * overriding system configuration. This function cannot be called
846 * on the icon theme objects returned from gtk_icon_theme_get_default()
847 * and gtk_icon_theme_get_for_screen().
852 gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
853 const gchar *theme_name)
855 GtkIconThemePrivate *priv;
857 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
859 priv = icon_theme->priv;
861 g_return_if_fail (!priv->is_screen_singleton);
863 if (theme_name != NULL)
865 priv->custom_theme = TRUE;
866 if (!priv->current_theme || strcmp (theme_name, priv->current_theme) != 0)
868 g_free (priv->current_theme);
869 priv->current_theme = g_strdup (theme_name);
871 do_theme_change (icon_theme);
876 if (priv->custom_theme)
878 priv->custom_theme = FALSE;
880 update_current_theme (icon_theme);
886 insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
892 GtkIconThemePrivate *priv;
893 IconTheme *theme = NULL;
895 GKeyFile *theme_file;
896 GError *error = NULL;
897 IconThemeDirMtime *dir_mtime;
898 struct stat stat_buf;
900 priv = icon_theme->priv;
902 for (l = priv->themes; l != NULL; l = l->next)
905 if (strcmp (theme->name, theme_name) == 0)
909 for (i = 0; i < priv->search_path_len; i++)
911 path = g_build_filename (priv->search_path[i],
914 dir_mtime = g_slice_new (IconThemeDirMtime);
915 dir_mtime->cache = NULL;
916 dir_mtime->dir = path;
917 if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
918 dir_mtime->mtime = stat_buf.st_mtime;
920 dir_mtime->mtime = 0;
922 priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
924 priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
927 for (i = 0; i < priv->search_path_len && !theme_file; i++)
929 path = g_build_filename (priv->search_path[i],
933 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
935 theme_file = g_key_file_new ();
936 g_key_file_set_list_separator (theme_file, ',');
937 g_key_file_load_from_file (theme_file, path, 0, &error);
940 g_key_file_free (theme_file);
942 g_error_free (error);
949 if (theme_file || strcmp (theme_name, DEFAULT_THEME_NAME) == 0)
951 theme = g_new0 (IconTheme, 1);
952 theme->name = g_strdup (theme_name);
953 priv->themes = g_list_prepend (priv->themes, theme);
956 if (theme_file == NULL)
959 theme->display_name =
960 g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
961 if (!theme->display_name)
962 g_warning ("Theme file for %s has no name\n", theme_name);
964 dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
967 g_warning ("Theme file for %s has no directories\n", theme_name);
968 priv->themes = g_list_remove (priv->themes, theme);
969 g_free (theme->name);
970 g_free (theme->display_name);
972 g_key_file_free (theme_file);
977 g_key_file_get_locale_string (theme_file,
978 "Icon Theme", "Comment",
981 g_key_file_get_string (theme_file,
982 "Icon Theme", "Example",
986 for (i = 0; dirs[i] != NULL; i++)
987 theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
991 theme->dirs = g_list_reverse (theme->dirs);
993 themes = g_key_file_get_string_list (theme_file,
1000 for (i = 0; themes[i] != NULL; i++)
1001 insert_theme (icon_theme, themes[i]);
1003 g_strfreev (themes);
1006 g_key_file_free (theme_file);
1010 free_unthemed_icon (UnthemedIcon *unthemed_icon)
1012 g_free (unthemed_icon->svg_filename);
1013 g_free (unthemed_icon->no_svg_filename);
1014 g_slice_free (UnthemedIcon, unthemed_icon);
1018 strip_suffix (const char *filename)
1022 dot = strrchr (filename, '.');
1025 return g_strdup (filename);
1027 return g_strndup (filename, dot - filename);
1031 load_themes (GtkIconTheme *icon_theme)
1033 GtkIconThemePrivate *priv;
1038 UnthemedIcon *unthemed_icon;
1039 IconSuffix old_suffix, new_suffix;
1041 IconThemeDirMtime *dir_mtime;
1042 struct stat stat_buf;
1044 priv = icon_theme->priv;
1046 priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
1048 if (priv->current_theme)
1049 insert_theme (icon_theme, priv->current_theme);
1051 /* Always look in the "default" icon theme, and in a fallback theme */
1052 if (priv->fallback_theme)
1053 insert_theme (icon_theme, priv->fallback_theme);
1054 insert_theme (icon_theme, DEFAULT_THEME_NAME);
1055 priv->themes = g_list_reverse (priv->themes);
1058 priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
1059 g_free, (GDestroyNotify)free_unthemed_icon);
1061 for (base = 0; base < icon_theme->priv->search_path_len; base++)
1063 dir = icon_theme->priv->search_path[base];
1065 dir_mtime = g_slice_new (IconThemeDirMtime);
1066 priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
1068 dir_mtime->dir = g_strdup (dir);
1069 dir_mtime->mtime = 0;
1070 dir_mtime->cache = NULL;
1072 if (g_stat (dir, &stat_buf) != 0 || !S_ISDIR (stat_buf.st_mode))
1074 dir_mtime->mtime = stat_buf.st_mtime;
1076 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir);
1077 if (dir_mtime->cache != NULL)
1080 gdir = g_dir_open (dir, 0, NULL);
1084 while ((file = g_dir_read_name (gdir)))
1086 new_suffix = suffix_from_name (file);
1088 if (new_suffix != ICON_SUFFIX_NONE)
1093 abs_file = g_build_filename (dir, file, NULL);
1094 base_name = strip_suffix (file);
1096 if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
1099 if (new_suffix == ICON_SUFFIX_SVG)
1101 if (unthemed_icon->svg_filename)
1104 unthemed_icon->svg_filename = abs_file;
1108 if (unthemed_icon->no_svg_filename)
1110 old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
1111 if (new_suffix > old_suffix)
1113 g_free (unthemed_icon->no_svg_filename);
1114 unthemed_icon->no_svg_filename = abs_file;
1120 unthemed_icon->no_svg_filename = abs_file;
1127 unthemed_icon = g_slice_new0 (UnthemedIcon);
1129 if (new_suffix == ICON_SUFFIX_SVG)
1130 unthemed_icon->svg_filename = abs_file;
1132 unthemed_icon->no_svg_filename = abs_file;
1134 g_hash_table_insert (priv->unthemed_icons,
1137 g_hash_table_insert (priv->all_icons,
1145 priv->themes_valid = TRUE;
1147 g_get_current_time(&tv);
1148 priv->last_stat_time = tv.tv_sec;
1152 _gtk_icon_theme_ensure_builtin_cache (void)
1154 static gboolean initialized = FALSE;
1156 static IconThemeDir dirs[5] =
1158 { ICON_THEME_DIR_THRESHOLD, 0, 16, 16, 16, 2, NULL, "16", -1, NULL, NULL, NULL },
1159 { ICON_THEME_DIR_THRESHOLD, 0, 20, 20, 20, 2, NULL, "20", -1, NULL, NULL, NULL },
1160 { ICON_THEME_DIR_THRESHOLD, 0, 24, 24, 24, 2, NULL, "24", -1, NULL, NULL, NULL },
1161 { ICON_THEME_DIR_THRESHOLD, 0, 32, 32, 32, 2, NULL, "32", -1, NULL, NULL, NULL },
1162 { ICON_THEME_DIR_THRESHOLD, 0, 48, 48, 48, 2, NULL, "48", -1, NULL, NULL, NULL }
1170 _builtin_cache = _gtk_icon_cache_new ((gchar *)builtin_icons);
1172 for (i = 0; i < G_N_ELEMENTS (dirs); i++)
1175 dir->cache = _gtk_icon_cache_ref (_builtin_cache);
1176 dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
1178 builtin_dirs = g_list_append (builtin_dirs, dir);
1184 ensure_valid_themes (GtkIconTheme *icon_theme)
1186 GtkIconThemePrivate *priv = icon_theme->priv;
1188 gboolean was_valid = priv->themes_valid;
1190 if (priv->loading_themes)
1192 priv->loading_themes = TRUE;
1194 _gtk_icon_theme_ensure_builtin_cache ();
1196 if (priv->themes_valid)
1198 g_get_current_time (&tv);
1200 if (ABS (tv.tv_sec - priv->last_stat_time) > 5 &&
1201 rescan_themes (icon_theme))
1202 blow_themes (icon_theme);
1205 if (!priv->themes_valid)
1207 load_themes (icon_theme);
1211 g_signal_emit (icon_theme, signal_changed, 0);
1213 if (!priv->check_reload && priv->screen)
1215 static GdkAtom atom_iconthemes = GDK_NONE;
1216 GdkEvent *event = gdk_event_new (GDK_CLIENT_EVENT);
1219 if (!atom_iconthemes)
1220 atom_iconthemes = gdk_atom_intern_static_string ("_GTK_LOAD_ICONTHEMES");
1222 for (i = 0; i < 5; i++)
1223 event->client.data.l[i] = 0;
1224 event->client.data_format = 32;
1225 event->client.message_type = atom_iconthemes;
1227 gdk_screen_broadcast_client_message (priv->screen, event);
1232 priv->loading_themes = FALSE;
1235 static GtkIconInfo *
1236 choose_icon (GtkIconTheme *icon_theme,
1237 const gchar *icon_names[],
1239 GtkIconLookupFlags flags)
1241 GtkIconThemePrivate *priv;
1243 GtkIconInfo *icon_info = NULL;
1244 UnthemedIcon *unthemed_icon = NULL;
1246 gboolean use_builtin;
1249 priv = icon_theme->priv;
1251 if (flags & GTK_ICON_LOOKUP_NO_SVG)
1253 else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
1256 allow_svg = priv->pixbuf_supports_svg;
1258 use_builtin = flags & GTK_ICON_LOOKUP_USE_BUILTIN;
1260 ensure_valid_themes (icon_theme);
1262 for (l = priv->themes; l; l = l->next)
1264 IconTheme *theme = l->data;
1266 for (i = 0; icon_names[i]; i++)
1268 icon_info = theme_lookup_icon (theme, icon_names[i], size, allow_svg, use_builtin);
1274 for (i = 0; icon_names[i]; i++)
1276 unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_names[i]);
1281 /* Still not found an icon, check if reference to a Win32 resource */
1287 resources = g_strsplit (icon_names[0], ",", 0);
1290 wchar_t *wfile = g_utf8_to_utf16 (resources[0], -1, NULL, NULL, NULL);
1291 ExtractIconExW (wfile, resources[1] ? atoi (resources[1]) : 0, &hIcon, NULL, 1);
1297 icon_info = icon_info_new ();
1298 icon_info->cache_pixbuf = gdk_win32_icon_to_pixbuf_libgtk_only (hIcon);
1299 DestroyIcon (hIcon);
1300 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1301 icon_info->dir_size = size;
1303 g_strfreev (resources);
1309 icon_info = icon_info_new ();
1311 /* A SVG icon, when allowed, beats out a XPM icon, but not
1315 unthemed_icon->svg_filename &&
1316 (!unthemed_icon->no_svg_filename ||
1317 suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
1318 icon_info->filename = g_strdup (unthemed_icon->svg_filename);
1319 else if (unthemed_icon->no_svg_filename)
1320 icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);
1322 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1323 icon_info->dir_size = size;
1329 icon_info->desired_size = size;
1330 icon_info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
1334 static gboolean check_for_default_theme = TRUE;
1335 char *default_theme_path;
1336 gboolean found = FALSE;
1339 if (check_for_default_theme)
1341 check_for_default_theme = FALSE;
1343 for (i = 0; !found && i < priv->search_path_len; i++)
1345 default_theme_path = g_build_filename (priv->search_path[i],
1349 found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
1350 g_free (default_theme_path);
1355 g_warning (_("Could not find the icon '%s'. The '%s' theme\n"
1356 "was not found either, perhaps you need to install it.\n"
1357 "You can get a copy from:\n"
1359 icon_names[0], DEFAULT_THEME_NAME, "http://icon-theme.freedesktop.org/releases");
1369 * gtk_icon_theme_lookup_icon:
1370 * @icon_theme: a #GtkIconTheme
1371 * @icon_name: the name of the icon to lookup
1372 * @size: desired icon size
1373 * @flags: flags modifying the behavior of the icon lookup
1375 * Looks up a named icon and returns a structure containing
1376 * information such as the filename of the icon. The icon
1377 * can then be rendered into a pixbuf using
1378 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1379 * combines these two steps if all you need is the pixbuf.)
1381 * Return value: a #GtkIconInfo structure containing information
1382 * about the icon, or %NULL if the icon wasn't found. Free with
1383 * gtk_icon_info_free()
1388 gtk_icon_theme_lookup_icon (GtkIconTheme *icon_theme,
1389 const gchar *icon_name,
1391 GtkIconLookupFlags flags)
1395 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1396 g_return_val_if_fail (icon_name != NULL, NULL);
1397 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1398 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1400 GTK_NOTE (ICONTHEME,
1401 g_print ("gtk_icon_theme_lookup_icon %s\n", icon_name));
1403 if (flags & GTK_ICON_LOOKUP_GENERIC_FALLBACK)
1410 for (p = (gchar *) icon_name; *p; p++)
1414 names = g_new (gchar *, dashes + 2);
1415 names[0] = g_strdup (icon_name);
1416 for (i = 1; i <= dashes; i++)
1418 names[i] = g_strdup (names[i - 1]);
1419 p = strrchr (names[i], '-');
1422 names[dashes + 1] = NULL;
1424 info = choose_icon (icon_theme, (const gchar **) names, size, flags);
1430 const gchar *names[2];
1432 names[0] = icon_name;
1435 info = choose_icon (icon_theme, names, size, flags);
1442 * gtk_icon_theme_choose_icon:
1443 * @icon_theme: a #GtkIconTheme
1444 * @icon_names: (array zero-terminated=1): %NULL-terminated array of
1445 * icon names to lookup
1446 * @size: desired icon size
1447 * @flags: flags modifying the behavior of the icon lookup
1449 * Looks up a named icon and returns a structure containing
1450 * information such as the filename of the icon. The icon
1451 * can then be rendered into a pixbuf using
1452 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1453 * combines these two steps if all you need is the pixbuf.)
1455 * If @icon_names contains more than one name, this function
1456 * tries them all in the given order before falling back to
1457 * inherited icon themes.
1459 * Return value: a #GtkIconInfo structure containing information
1460 * about the icon, or %NULL if the icon wasn't found. Free with
1461 * gtk_icon_info_free()
1466 gtk_icon_theme_choose_icon (GtkIconTheme *icon_theme,
1467 const gchar *icon_names[],
1469 GtkIconLookupFlags flags)
1471 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1472 g_return_val_if_fail (icon_names != NULL, NULL);
1473 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1474 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1476 return choose_icon (icon_theme, icon_names, size, flags);
1481 gtk_icon_theme_error_quark (void)
1483 return g_quark_from_static_string ("gtk-icon-theme-error-quark");
1487 * gtk_icon_theme_load_icon:
1488 * @icon_theme: a #GtkIconTheme
1489 * @icon_name: the name of the icon to lookup
1490 * @size: the desired icon size. The resulting icon may not be
1491 * exactly this size; see gtk_icon_info_load_icon().
1492 * @flags: flags modifying the behavior of the icon lookup
1493 * @error: (allow-none): Location to store error information on failure,
1496 * Looks up an icon in an icon theme, scales it to the given size
1497 * and renders it into a pixbuf. This is a convenience function;
1498 * if more details about the icon are needed, use
1499 * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1501 * Note that you probably want to listen for icon theme changes and
1502 * update the icon. This is usually done by connecting to the
1503 * GtkWidget::style-set signal. If for some reason you do not want to
1504 * update the icon when the icon theme changes, you should consider
1505 * using gdk_pixbuf_copy() to make a private copy of the pixbuf
1506 * returned by this function. Otherwise GTK+ may need to keep the old
1507 * icon theme loaded, which would be a waste of memory.
1509 * Return value: (transfer full): the rendered icon; this may be a
1510 * newly created icon or a new reference to an internal icon, so
1511 * you must not modify the icon. Use g_object_unref() to release
1512 * your reference to the icon. %NULL if the icon isn't found.
1517 gtk_icon_theme_load_icon (GtkIconTheme *icon_theme,
1518 const gchar *icon_name,
1520 GtkIconLookupFlags flags,
1523 GtkIconInfo *icon_info;
1524 GdkPixbuf *pixbuf = NULL;
1526 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1527 g_return_val_if_fail (icon_name != NULL, NULL);
1528 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1529 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1530 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1532 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
1533 flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1536 g_set_error (error, GTK_ICON_THEME_ERROR, GTK_ICON_THEME_NOT_FOUND,
1537 _("Icon '%s' not present in theme"), icon_name);
1541 pixbuf = gtk_icon_info_load_icon (icon_info, error);
1542 gtk_icon_info_free (icon_info);
1548 * gtk_icon_theme_has_icon:
1549 * @icon_theme: a #GtkIconTheme
1550 * @icon_name: the name of an icon
1552 * Checks whether an icon theme includes an icon
1553 * for a particular name.
1555 * Return value: %TRUE if @icon_theme includes an
1556 * icon for @icon_name.
1561 gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
1562 const char *icon_name)
1564 GtkIconThemePrivate *priv;
1567 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1568 g_return_val_if_fail (icon_name != NULL, FALSE);
1570 priv = icon_theme->priv;
1572 ensure_valid_themes (icon_theme);
1574 for (l = priv->dir_mtimes; l; l = l->next)
1576 IconThemeDirMtime *dir_mtime = l->data;
1577 GtkIconCache *cache = dir_mtime->cache;
1579 if (cache && _gtk_icon_cache_has_icon (cache, icon_name))
1583 if (g_hash_table_lookup_extended (priv->all_icons,
1584 icon_name, NULL, NULL))
1587 if (_builtin_cache &&
1588 _gtk_icon_cache_has_icon (_builtin_cache, icon_name))
1591 if (icon_theme_builtin_icons &&
1592 g_hash_table_lookup_extended (icon_theme_builtin_icons,
1593 icon_name, NULL, NULL))
1600 add_size (gpointer key,
1604 gint **res_p = user_data;
1606 **res_p = GPOINTER_TO_INT (key);
1612 * gtk_icon_theme_get_icon_sizes:
1613 * @icon_theme: a #GtkIconTheme
1614 * @icon_name: the name of an icon
1616 * Returns an array of integers describing the sizes at which
1617 * the icon is available without scaling. A size of -1 means
1618 * that the icon is available in a scalable format. The array
1619 * is zero-terminated.
1621 * Return value: (array zero-terminated=1): An newly allocated array
1622 * describing the sizes at which the icon is available. The array
1623 * should be freed with g_free() when it is no longer needed.
1628 gtk_icon_theme_get_icon_sizes (GtkIconTheme *icon_theme,
1629 const char *icon_name)
1631 GList *l, *d, *icons;
1635 GtkIconThemePrivate *priv;
1637 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1639 priv = icon_theme->priv;
1641 ensure_valid_themes (icon_theme);
1643 sizes = g_hash_table_new (g_direct_hash, g_direct_equal);
1645 for (l = priv->themes; l; l = l->next)
1647 IconTheme *theme = l->data;
1648 for (d = theme->dirs; d; d = d->next)
1650 IconThemeDir *dir = d->data;
1652 if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
1655 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1656 if (suffix != ICON_SUFFIX_NONE)
1658 if (suffix == ICON_SUFFIX_SVG)
1659 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1661 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1666 for (d = builtin_dirs; d; d = d->next)
1668 IconThemeDir *dir = d->data;
1670 if (dir->type != ICON_THEME_DIR_SCALABLE && g_hash_table_lookup_extended (sizes, GINT_TO_POINTER (dir->size), NULL, NULL))
1673 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
1674 if (suffix != ICON_SUFFIX_NONE)
1676 if (suffix == ICON_SUFFIX_SVG)
1677 g_hash_table_insert (sizes, GINT_TO_POINTER (-1), NULL);
1679 g_hash_table_insert (sizes, GINT_TO_POINTER (dir->size), NULL);
1683 if (icon_theme_builtin_icons)
1685 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
1689 BuiltinIcon *icon = icons->data;
1691 g_hash_table_insert (sizes, GINT_TO_POINTER (icon->size), NULL);
1692 icons = icons->next;
1696 r = result = g_new0 (gint, g_hash_table_size (sizes) + 1);
1698 g_hash_table_foreach (sizes, add_size, &r);
1699 g_hash_table_destroy (sizes);
1705 add_key_to_hash (gpointer key,
1709 GHashTable *hash = user_data;
1711 g_hash_table_insert (hash, key, NULL);
1715 add_key_to_list (gpointer key,
1719 GList **list = user_data;
1721 *list = g_list_prepend (*list, g_strdup (key));
1725 * gtk_icon_theme_list_icons:
1726 * @icon_theme: a #GtkIconTheme
1727 * @context: (allow-none): a string identifying a particular type of
1728 * icon, or %NULL to list all icons.
1730 * Lists the icons in the current icon theme. Only a subset
1731 * of the icons can be listed by providing a context string.
1732 * The set of values for the context string is system dependent,
1733 * but will typically include such values as "Applications" and
1736 * Return value: (element-type utf8) (transfer full): a #GList list
1737 * holding the names of all the icons in the theme. You must first
1738 * free each element in the list with g_free(), then free the list
1739 * itself with g_list_free().
1744 gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
1745 const char *context)
1747 GtkIconThemePrivate *priv;
1750 GQuark context_quark;
1752 priv = icon_theme->priv;
1754 ensure_valid_themes (icon_theme);
1758 context_quark = g_quark_try_string (context);
1766 icons = g_hash_table_new (g_str_hash, g_str_equal);
1771 theme_list_icons (l->data, icons, context_quark);
1775 if (context_quark == 0)
1776 g_hash_table_foreach (priv->unthemed_icons,
1782 g_hash_table_foreach (icons,
1786 g_hash_table_destroy (icons);
1792 * gtk_icon_theme_list_contexts:
1793 * @icon_theme: a #GtkIconTheme
1795 * Gets the list of contexts available within the current
1796 * hierarchy of icon themes
1798 * Return value: (element-type utf8) (transfer full): a #GList list
1799 * holding the names of all the contexts in the theme. You must first
1800 * free each element in the list with g_free(), then free the list
1801 * itself with g_list_free().
1806 gtk_icon_theme_list_contexts (GtkIconTheme *icon_theme)
1808 GtkIconThemePrivate *priv;
1809 GHashTable *contexts;
1812 priv = icon_theme->priv;
1814 ensure_valid_themes (icon_theme);
1816 contexts = g_hash_table_new (g_str_hash, g_str_equal);
1821 theme_list_contexts (l->data, contexts);
1827 g_hash_table_foreach (contexts,
1831 g_hash_table_destroy (contexts);
1837 * gtk_icon_theme_get_example_icon_name:
1838 * @icon_theme: a #GtkIconTheme
1840 * Gets the name of an icon that is representative of the
1841 * current theme (for instance, to use when presenting
1842 * a list of themes to the user.)
1844 * Return value: the name of an example icon or %NULL.
1845 * Free with g_free().
1850 gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
1852 GtkIconThemePrivate *priv;
1856 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1858 priv = icon_theme->priv;
1860 ensure_valid_themes (icon_theme);
1867 return g_strdup (theme->example);
1877 rescan_themes (GtkIconTheme *icon_theme)
1879 GtkIconThemePrivate *priv;
1880 IconThemeDirMtime *dir_mtime;
1883 struct stat stat_buf;
1886 priv = icon_theme->priv;
1888 for (d = priv->dir_mtimes; d != NULL; d = d->next)
1890 dir_mtime = d->data;
1892 stat_res = g_stat (dir_mtime->dir, &stat_buf);
1894 /* dir mtime didn't change */
1895 if (stat_res == 0 &&
1896 S_ISDIR (stat_buf.st_mode) &&
1897 dir_mtime->mtime == stat_buf.st_mtime)
1899 /* didn't exist before, and still doesn't */
1900 if (dir_mtime->mtime == 0 &&
1901 (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
1907 g_get_current_time (&tv);
1908 priv->last_stat_time = tv.tv_sec;
1914 * gtk_icon_theme_rescan_if_needed:
1915 * @icon_theme: a #GtkIconTheme
1917 * Checks to see if the icon theme has changed; if it has, any
1918 * currently cached information is discarded and will be reloaded
1919 * next time @icon_theme is accessed.
1921 * Return value: %TRUE if the icon theme has changed and needed
1927 gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
1931 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1933 retval = rescan_themes (icon_theme);
1935 do_theme_change (icon_theme);
1941 theme_destroy (IconTheme *theme)
1943 g_free (theme->display_name);
1944 g_free (theme->comment);
1945 g_free (theme->name);
1946 g_free (theme->example);
1948 g_list_foreach (theme->dirs, (GFunc)theme_dir_destroy, NULL);
1949 g_list_free (theme->dirs);
1955 theme_dir_destroy (IconThemeDir *dir)
1958 _gtk_icon_cache_unref (dir->cache);
1960 g_hash_table_destroy (dir->icons);
1963 g_hash_table_destroy (dir->icon_data);
1965 g_free (dir->subdir);
1970 theme_dir_size_difference (IconThemeDir *dir, int size, gboolean *smaller)
1975 case ICON_THEME_DIR_FIXED:
1976 *smaller = size < dir->size;
1977 return abs (size - dir->size);
1979 case ICON_THEME_DIR_SCALABLE:
1980 *smaller = size < dir->min_size;
1981 if (size < dir->min_size)
1982 return dir->min_size - size;
1983 if (size > dir->max_size)
1984 return size - dir->max_size;
1987 case ICON_THEME_DIR_THRESHOLD:
1988 min = dir->size - dir->threshold;
1989 max = dir->size + dir->threshold;
1990 *smaller = size < min;
1997 case ICON_THEME_DIR_UNTHEMED:
1998 g_assert_not_reached ();
2001 g_assert_not_reached ();
2006 string_from_suffix (IconSuffix suffix)
2010 case ICON_SUFFIX_XPM:
2012 case ICON_SUFFIX_SVG:
2014 case ICON_SUFFIX_PNG:
2017 g_assert_not_reached();
2023 suffix_from_name (const char *name)
2027 if (g_str_has_suffix (name, ".png"))
2028 retval = ICON_SUFFIX_PNG;
2029 else if (g_str_has_suffix (name, ".svg"))
2030 retval = ICON_SUFFIX_SVG;
2031 else if (g_str_has_suffix (name, ".xpm"))
2032 retval = ICON_SUFFIX_XPM;
2034 retval = ICON_SUFFIX_NONE;
2040 best_suffix (IconSuffix suffix,
2043 if ((suffix & ICON_SUFFIX_PNG) != 0)
2044 return ICON_SUFFIX_PNG;
2045 else if (allow_svg && ((suffix & ICON_SUFFIX_SVG) != 0))
2046 return ICON_SUFFIX_SVG;
2047 else if ((suffix & ICON_SUFFIX_XPM) != 0)
2048 return ICON_SUFFIX_XPM;
2050 return ICON_SUFFIX_NONE;
2055 theme_dir_get_icon_suffix (IconThemeDir *dir,
2056 const gchar *icon_name,
2057 gboolean *has_icon_file)
2063 suffix = (IconSuffix)_gtk_icon_cache_get_icon_flags (dir->cache,
2068 *has_icon_file = suffix & HAS_ICON_FILE;
2070 suffix = suffix & ~HAS_ICON_FILE;
2073 suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
2075 GTK_NOTE (ICONTHEME,
2076 g_print ("get_icon_suffix%s %u\n", dir->cache ? " (cached)" : "", suffix));
2081 static GtkIconInfo *
2082 theme_lookup_icon (IconTheme *theme,
2083 const char *icon_name,
2086 gboolean use_builtin)
2089 IconThemeDir *dir, *min_dir;
2091 int min_difference, difference;
2092 BuiltinIcon *closest_builtin = NULL;
2093 gboolean smaller, has_larger, match;
2096 min_difference = G_MAXINT;
2101 /* Builtin icons are logically part of the default theme and
2102 * are searched before other subdirectories of the default theme.
2104 if (use_builtin && strcmp (theme->name, DEFAULT_THEME_NAME) == 0)
2106 closest_builtin = find_builtin_icon (icon_name,
2111 if (min_difference == 0)
2112 return icon_info_new_builtin (closest_builtin);
2114 dirs = builtin_dirs;
2124 GTK_NOTE (ICONTHEME,
2125 g_print ("theme_lookup_icon dir %s\n", dir->dir));
2126 suffix = theme_dir_get_icon_suffix (dir, icon_name, NULL);
2127 if (best_suffix (suffix, allow_svg) != ICON_SUFFIX_NONE)
2129 difference = theme_dir_size_difference (dir, size, &smaller);
2131 if (difference == 0)
2133 if (dir->type == ICON_THEME_DIR_SCALABLE)
2135 /* don't pick scalable if we already found
2136 * a matching non-scalable dir
2146 /* for a matching non-scalable dir keep
2147 * going and look for a closer match
2149 difference = abs (size - dir->size);
2150 if (!match || difference < min_difference)
2153 min_difference = difference;
2156 if (difference == 0)
2165 if (difference < min_difference || smaller)
2167 min_difference = difference;
2169 has_larger = smaller;
2174 if (difference < min_difference && smaller)
2176 min_difference = difference;
2185 if (l == NULL && dirs == builtin_dirs)
2194 GtkIconInfo *icon_info = icon_info_new ();
2195 gboolean has_icon_file = FALSE;
2197 suffix = theme_dir_get_icon_suffix (min_dir, icon_name, &has_icon_file);
2198 suffix = best_suffix (suffix, allow_svg);
2199 g_assert (suffix != ICON_SUFFIX_NONE);
2203 file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
2204 icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
2209 icon_info->filename = NULL;
2212 if (min_dir->icon_data != NULL)
2213 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2215 if (icon_info->data == NULL && min_dir->cache != NULL)
2217 icon_info->data = _gtk_icon_cache_get_icon_data (min_dir->cache, icon_name, min_dir->subdir_index);
2218 if (icon_info->data)
2220 if (min_dir->icon_data == NULL)
2221 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2222 g_free, (GDestroyNotify)icon_data_free);
2224 g_hash_table_replace (min_dir->icon_data, g_strdup (icon_name), icon_info->data);
2228 if (icon_info->data == NULL && has_icon_file)
2230 gchar *icon_file_name, *icon_file_path;
2232 icon_file_name = g_strconcat (icon_name, ".icon", NULL);
2233 icon_file_path = g_build_filename (min_dir->dir, icon_file_name, NULL);
2235 if (g_file_test (icon_file_path, G_FILE_TEST_IS_REGULAR))
2237 if (min_dir->icon_data == NULL)
2238 min_dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2239 g_free, (GDestroyNotify)icon_data_free);
2240 load_icon_data (min_dir, icon_file_path, icon_file_name);
2242 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
2244 g_free (icon_file_name);
2245 g_free (icon_file_path);
2250 icon_info->cache_pixbuf = _gtk_icon_cache_get_icon (min_dir->cache, icon_name,
2251 min_dir->subdir_index);
2254 icon_info->dir_type = min_dir->type;
2255 icon_info->dir_size = min_dir->size;
2256 icon_info->threshold = min_dir->threshold;
2261 if (closest_builtin)
2262 return icon_info_new_builtin (closest_builtin);
2268 theme_list_icons (IconTheme *theme,
2272 GList *l = theme->dirs;
2279 if (context == dir->context ||
2284 _gtk_icon_cache_add_icons (dir->cache,
2291 g_hash_table_foreach (dir->icons,
2301 theme_list_contexts (IconTheme *theme,
2302 GHashTable *contexts)
2304 GList *l = theme->dirs;
2306 const char *context;
2312 context = g_quark_to_string (dir->context);
2313 g_hash_table_replace (contexts, (gpointer) context, NULL);
2320 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
2322 GKeyFile *icon_file;
2330 GError *error = NULL;
2334 icon_file = g_key_file_new ();
2335 g_key_file_set_list_separator (icon_file, ',');
2336 g_key_file_load_from_file (icon_file, path, 0, &error);
2339 g_error_free (error);
2340 g_key_file_free (icon_file);
2345 base_name = strip_suffix (name);
2347 data = g_slice_new0 (GtkIconData);
2348 g_hash_table_replace (dir->icon_data, base_name, data);
2350 ivalues = g_key_file_get_integer_list (icon_file,
2351 "Icon Data", "EmbeddedTextRectangle",
2357 data->has_embedded_rect = TRUE;
2358 data->x0 = ivalues[0];
2359 data->y0 = ivalues[1];
2360 data->x1 = ivalues[2];
2361 data->y1 = ivalues[3];
2367 str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
2370 split = g_strsplit (str, "|", -1);
2372 data->n_attach_points = g_strv_length (split);
2373 data->attach_points = g_new (GdkPoint, data->n_attach_points);
2376 while (split[i] != NULL && i < data->n_attach_points)
2378 split_point = strchr (split[i], ',');
2383 data->attach_points[i].x = atoi (split[i]);
2384 data->attach_points[i].y = atoi (split_point);
2393 data->display_name = g_key_file_get_locale_string (icon_file,
2394 "Icon Data", "DisplayName",
2396 g_key_file_free (icon_file);
2401 scan_directory (GtkIconThemePrivate *icon_theme,
2402 IconThemeDir *dir, char *full_dir)
2407 GTK_NOTE (ICONTHEME,
2408 g_print ("scanning directory %s\n", full_dir));
2409 dir->icons = g_hash_table_new_full (g_str_hash, g_str_equal,
2412 gdir = g_dir_open (full_dir, 0, NULL);
2417 while ((name = g_dir_read_name (gdir)))
2421 IconSuffix suffix, hash_suffix;
2423 if (g_str_has_suffix (name, ".icon"))
2425 if (dir->icon_data == NULL)
2426 dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
2427 g_free, (GDestroyNotify)icon_data_free);
2429 path = g_build_filename (full_dir, name, NULL);
2430 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
2431 load_icon_data (dir, path, name);
2438 suffix = suffix_from_name (name);
2439 if (suffix == ICON_SUFFIX_NONE)
2442 base_name = strip_suffix (name);
2444 hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
2445 g_hash_table_replace (icon_theme->all_icons, base_name, NULL);
2446 g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
2453 theme_subdir_load (GtkIconTheme *icon_theme,
2455 GKeyFile *theme_file,
2461 IconThemeDirType type;
2462 char *context_string;
2469 GError *error = NULL;
2470 IconThemeDirMtime *dir_mtime;
2472 size = g_key_file_get_integer (theme_file, subdir, "Size", &error);
2475 g_error_free (error);
2476 g_warning ("Theme directory %s of theme %s has no size field\n",
2477 subdir, theme->name);
2481 type = ICON_THEME_DIR_THRESHOLD;
2482 type_string = g_key_file_get_string (theme_file, subdir, "Type", NULL);
2485 if (strcmp (type_string, "Fixed") == 0)
2486 type = ICON_THEME_DIR_FIXED;
2487 else if (strcmp (type_string, "Scalable") == 0)
2488 type = ICON_THEME_DIR_SCALABLE;
2489 else if (strcmp (type_string, "Threshold") == 0)
2490 type = ICON_THEME_DIR_THRESHOLD;
2492 g_free (type_string);
2496 context_string = g_key_file_get_string (theme_file, subdir, "Context", NULL);
2499 context = g_quark_from_string (context_string);
2500 g_free (context_string);
2503 max_size = g_key_file_get_integer (theme_file, subdir, "MaxSize", &error);
2508 g_error_free (error);
2512 min_size = g_key_file_get_integer (theme_file, subdir, "MinSize", &error);
2517 g_error_free (error);
2521 threshold = g_key_file_get_integer (theme_file, subdir, "Threshold", &error);
2526 g_error_free (error);
2530 for (d = icon_theme->priv->dir_mtimes; d; d = d->next)
2532 dir_mtime = (IconThemeDirMtime *)d->data;
2534 if (dir_mtime->mtime == 0)
2535 continue; /* directory doesn't exist */
2537 full_dir = g_build_filename (dir_mtime->dir, subdir, NULL);
2539 /* First, see if we have a cache for the directory */
2540 if (dir_mtime->cache != NULL || g_file_test (full_dir, G_FILE_TEST_IS_DIR))
2542 if (dir_mtime->cache == NULL)
2544 /* This will return NULL if the cache doesn't exist or is outdated */
2545 dir_mtime->cache = _gtk_icon_cache_new_for_path (dir_mtime->dir);
2548 dir = g_new (IconThemeDir, 1);
2550 dir->context = context;
2552 dir->min_size = min_size;
2553 dir->max_size = max_size;
2554 dir->threshold = threshold;
2555 dir->dir = full_dir;
2556 dir->icon_data = NULL;
2557 dir->subdir = g_strdup (subdir);
2558 if (dir_mtime->cache != NULL)
2560 dir->cache = _gtk_icon_cache_ref (dir_mtime->cache);
2561 dir->subdir_index = _gtk_icon_cache_get_directory_index (dir->cache, dir->subdir);
2566 dir->subdir_index = -1;
2567 scan_directory (icon_theme->priv, dir, full_dir);
2570 theme->dirs = g_list_prepend (theme->dirs, dir);
2578 icon_data_free (GtkIconData *icon_data)
2580 g_free (icon_data->attach_points);
2581 g_free (icon_data->display_name);
2582 g_slice_free (GtkIconData, icon_data);
2589 G_DEFINE_BOXED_TYPE (GtkIconInfo, gtk_icon_info,
2593 static GtkIconInfo *
2594 icon_info_new (void)
2596 GtkIconInfo *icon_info = g_slice_new0 (GtkIconInfo);
2598 icon_info->scale = -1.;
2599 icon_info->ref_count = 1;
2604 static GtkIconInfo *
2605 icon_info_new_builtin (BuiltinIcon *icon)
2607 GtkIconInfo *icon_info = icon_info_new ();
2609 icon_info->cache_pixbuf = g_object_ref (icon->pixbuf);
2610 icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
2611 icon_info->dir_size = icon->size;
2612 icon_info->threshold = 2;
2618 * gtk_icon_info_copy:
2619 * @icon_info: a #GtkIconInfo
2621 * Make a copy of a #GtkIconInfo.
2623 * Return value: the new GtkIconInfo
2628 gtk_icon_info_copy (GtkIconInfo *icon_info)
2631 g_return_val_if_fail (icon_info != NULL, NULL);
2633 icon_info->ref_count++;
2639 * gtk_icon_info_free:
2640 * @icon_info: a #GtkIconInfo
2642 * Free a #GtkIconInfo and associated information
2647 gtk_icon_info_free (GtkIconInfo *icon_info)
2649 g_return_if_fail (icon_info != NULL);
2651 icon_info->ref_count--;
2652 if (icon_info->ref_count > 0)
2655 g_free (icon_info->filename);
2656 if (icon_info->loadable)
2657 g_object_unref (icon_info->loadable);
2658 g_slist_foreach (icon_info->emblem_infos, (GFunc)gtk_icon_info_free, NULL);
2659 g_slist_free (icon_info->emblem_infos);
2660 if (icon_info->pixbuf)
2661 g_object_unref (icon_info->pixbuf);
2662 if (icon_info->cache_pixbuf)
2663 g_object_unref (icon_info->cache_pixbuf);
2665 g_slice_free (GtkIconInfo, icon_info);
2669 * gtk_icon_info_get_base_size:
2670 * @icon_info: a #GtkIconInfo
2672 * Gets the base size for the icon. The base size
2673 * is a size for the icon that was specified by
2674 * the icon theme creator. This may be different
2675 * than the actual size of image; an example of
2676 * this is small emblem icons that can be attached
2677 * to a larger icon. These icons will be given
2678 * the same base size as the larger icons to which
2679 * they are attached.
2681 * Return value: the base size, or 0, if no base
2682 * size is known for the icon.
2687 gtk_icon_info_get_base_size (GtkIconInfo *icon_info)
2689 g_return_val_if_fail (icon_info != NULL, 0);
2691 return icon_info->dir_size;
2695 * gtk_icon_info_get_filename:
2696 * @icon_info: a #GtkIconInfo
2698 * Gets the filename for the icon. If the
2699 * %GTK_ICON_LOOKUP_USE_BUILTIN flag was passed
2700 * to gtk_icon_theme_lookup_icon(), there may be
2701 * no filename if a builtin icon is returned; in this
2702 * case, you should use gtk_icon_info_get_builtin_pixbuf().
2704 * Return value: (type filename): the filename for the icon, or %NULL
2705 * if gtk_icon_info_get_builtin_pixbuf() should be used instead. The
2706 * return value is owned by GTK+ and should not be modified or freed.
2710 G_CONST_RETURN gchar *
2711 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2713 g_return_val_if_fail (icon_info != NULL, NULL);
2715 return icon_info->filename;
2719 * gtk_icon_info_get_builtin_pixbuf:
2720 * @icon_info: a #GtkIconInfo structure
2722 * Gets the built-in image for this icon, if any. To allow
2723 * GTK+ to use built in icon images, you must pass the
2724 * %GTK_ICON_LOOKUP_USE_BUILTIN to
2725 * gtk_icon_theme_lookup_icon().
2727 * Return value: (transfer none): the built-in image pixbuf, or %NULL. No
2728 * extra reference is added to the returned pixbuf, so if
2729 * you want to keep it around, you must use g_object_ref().
2730 * The returned image must not be modified.
2735 gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
2737 g_return_val_if_fail (icon_info != NULL, NULL);
2739 if (icon_info->filename)
2742 return icon_info->cache_pixbuf;
2745 static gboolean icon_info_ensure_scale_and_pixbuf (GtkIconInfo*, gboolean);
2747 /* Combine the icon with all emblems, the first emblem is placed
2748 * in the southeast corner. Scale emblems to be at most 3/4 of the
2749 * size of the icon itself.
2752 apply_emblems (GtkIconInfo *info)
2754 GdkPixbuf *icon = NULL;
2758 if (info->emblem_infos == NULL)
2761 if (info->emblems_applied)
2764 w = gdk_pixbuf_get_width (info->pixbuf);
2765 h = gdk_pixbuf_get_height (info->pixbuf);
2767 for (l = info->emblem_infos, pos = 0; l; l = l->next, pos++)
2769 GtkIconInfo *emblem_info = l->data;
2771 if (icon_info_ensure_scale_and_pixbuf (emblem_info, FALSE))
2773 GdkPixbuf *emblem = emblem_info->pixbuf;
2775 gint x = 0, y = 0; /* silence compiler */
2778 ew = gdk_pixbuf_get_width (emblem);
2779 eh = gdk_pixbuf_get_height (emblem);
2811 icon = gdk_pixbuf_copy (info->pixbuf);
2816 gdk_pixbuf_composite (emblem, icon, x, y, ew, eh, x, y,
2817 scale, scale, GDK_INTERP_BILINEAR, 255);
2823 g_object_unref (info->pixbuf);
2824 info->pixbuf = icon;
2827 info->emblems_applied = TRUE;
2830 /* This function contains the complicated logic for deciding
2831 * on the size at which to load the icon and loading it at
2835 icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
2836 gboolean scale_only)
2838 int image_width, image_height;
2839 GdkPixbuf *source_pixbuf;
2842 /* First check if we already succeeded have the necessary
2843 * information (or failed earlier)
2845 if (scale_only && icon_info->scale >= 0)
2848 if (icon_info->pixbuf)
2850 apply_emblems (icon_info);
2854 if (icon_info->load_error)
2857 /* SVG icons are a special case - we just immediately scale them
2858 * to the desired size
2860 if (icon_info->filename && !icon_info->loadable)
2864 file = g_file_new_for_path (icon_info->filename);
2865 icon_info->loadable = G_LOADABLE_ICON (g_file_icon_new (file));
2866 g_object_unref (file);
2870 if (G_IS_FILE_ICON (icon_info->loadable))
2873 GFileInfo *file_info;
2874 const gchar *content_type;
2876 file = g_file_icon_get_file (G_FILE_ICON (icon_info->loadable));
2877 file_info = g_file_query_info (file,
2878 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
2879 G_FILE_QUERY_INFO_NONE,
2883 content_type = g_file_info_get_content_type (file_info);
2885 if (content_type && strcmp (content_type, "image/svg+xml") == 0)
2888 g_object_unref (file_info);
2894 GInputStream *stream;
2896 icon_info->scale = icon_info->desired_size / 1000.;
2901 stream = g_loadable_icon_load (icon_info->loadable,
2902 icon_info->desired_size,
2904 &icon_info->load_error);
2907 icon_info->pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
2908 icon_info->desired_size,
2909 icon_info->desired_size,
2912 &icon_info->load_error);
2913 g_object_unref (stream);
2916 if (!icon_info->pixbuf)
2919 apply_emblems (icon_info);
2924 /* In many cases, the scale can be determined without actual access
2925 * to the icon file. This is generally true when we have a size
2926 * for the directory where the icon is; the image size doesn't
2927 * matter in that case.
2929 if (icon_info->forced_size)
2930 icon_info->scale = -1;
2931 else if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
2932 icon_info->scale = 1.0;
2933 else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
2935 if (icon_info->desired_size >= icon_info->dir_size - icon_info->threshold &&
2936 icon_info->desired_size <= icon_info->dir_size + icon_info->threshold)
2937 icon_info->scale = 1.0;
2938 else if (icon_info->dir_size > 0)
2939 icon_info->scale =(gdouble) icon_info->desired_size / icon_info->dir_size;
2941 else if (icon_info->dir_type == ICON_THEME_DIR_SCALABLE)
2943 if (icon_info->dir_size > 0)
2944 icon_info->scale = (gdouble) icon_info->desired_size / icon_info->dir_size;
2947 if (icon_info->scale >= 0. && scale_only)
2950 /* At this point, we need to actually get the icon; either from the
2951 * builtin image or by loading the file
2953 source_pixbuf = NULL;
2954 if (icon_info->cache_pixbuf)
2955 source_pixbuf = g_object_ref (icon_info->cache_pixbuf);
2958 GInputStream *stream;
2960 stream = g_loadable_icon_load (icon_info->loadable,
2961 icon_info->desired_size,
2963 &icon_info->load_error);
2966 source_pixbuf = gdk_pixbuf_new_from_stream (stream,
2968 &icon_info->load_error);
2969 g_object_unref (stream);
2976 /* Do scale calculations that depend on the image size
2978 image_width = gdk_pixbuf_get_width (source_pixbuf);
2979 image_height = gdk_pixbuf_get_height (source_pixbuf);
2981 if (icon_info->scale < 0.0)
2983 gint image_size = MAX (image_width, image_height);
2985 icon_info->scale = (gdouble)icon_info->desired_size / (gdouble)image_size;
2987 icon_info->scale = 1.0;
2989 if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED &&
2990 !icon_info->forced_size)
2991 icon_info->scale = MIN (icon_info->scale, 1.0);
2994 /* We don't short-circuit out here for scale_only, since, now
2995 * we've loaded the icon, we might as well go ahead and finish
2996 * the job. This is a bit of a waste when we scale here
2997 * and never get the final pixbuf; at the cost of a bit of
2998 * extra complexity, we could keep the source pixbuf around
2999 * but not actually scale it until needed.
3001 if (icon_info->scale == 1.0)
3002 icon_info->pixbuf = source_pixbuf;
3005 icon_info->pixbuf = gdk_pixbuf_scale_simple (source_pixbuf,
3006 0.5 + image_width * icon_info->scale,
3007 0.5 + image_height * icon_info->scale,
3008 GDK_INTERP_BILINEAR);
3009 g_object_unref (source_pixbuf);
3012 apply_emblems (icon_info);
3018 * gtk_icon_info_load_icon:
3019 * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
3020 * @error: (allow-none): location to store error information on failure,
3023 * Renders an icon previously looked up in an icon theme using
3024 * gtk_icon_theme_lookup_icon(); the size will be based on the size
3025 * passed to gtk_icon_theme_lookup_icon(). Note that the resulting
3026 * pixbuf may not be exactly this size; an icon theme may have icons
3027 * that differ slightly from their nominal sizes, and in addition GTK+
3028 * will avoid scaling icons that it considers sufficiently close to the
3029 * requested size or for which the source image would have to be scaled
3030 * up too far. (This maintains sharpness.). This behaviour can be changed
3031 * by passing the %GTK_ICON_LOOKUP_FORCE_SIZE flag when obtaining
3032 * the #GtkIconInfo. If this flag has been specified, the pixbuf
3033 * returned by this function will be scaled to the exact size.
3035 * Return value: (transfer full): the rendered icon; this may be a newly
3036 * created icon or a new reference to an internal icon, so you must
3037 * not modify the icon. Use g_object_unref() to release your reference
3043 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
3046 g_return_val_if_fail (icon_info != NULL, NULL);
3047 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
3049 if (!icon_info_ensure_scale_and_pixbuf (icon_info, FALSE))
3051 if (icon_info->load_error)
3052 g_propagate_error (error, icon_info->load_error);
3054 g_set_error_literal (error,
3055 GTK_ICON_THEME_ERROR,
3056 GTK_ICON_THEME_NOT_FOUND,
3057 _("Failed to load icon"));
3062 return g_object_ref (icon_info->pixbuf);
3066 gdk_color_to_css (GdkColor *color)
3068 return g_strdup_printf ("rgb(%d,%d,%d)",
3075 gdk_rgba_to_css (GdkRGBA *color)
3077 /* drop alpha for now, since librsvg does not understand rgba() */
3078 return g_strdup_printf ("rgb(%d,%d,%d)",
3079 (gint)(color->red * 255),
3080 (gint)(color->green * 255),
3081 (gint)(color->blue * 255));
3085 _gtk_icon_info_load_symbolic_internal (GtkIconInfo *icon_info,
3086 const gchar *css_fg,
3087 const gchar *css_success,
3088 const gchar *css_warning,
3089 const gchar *css_error,
3092 GInputStream *stream;
3095 gchar *success, *warning, *err;
3097 /* css_fg can't possibly have failed, otherwise
3098 * that would mean we have a broken style */
3099 g_return_val_if_fail (css_fg != NULL, NULL);
3101 success = warning = err = NULL;
3105 GdkColor success_default_color = { 0, 0x4e00, 0x9a00, 0x0600 };
3106 success = gdk_color_to_css (&success_default_color);
3110 GdkColor warning_default_color = { 0, 0xf500, 0x7900, 0x3e00 };
3111 warning = gdk_color_to_css (&warning_default_color);
3115 GdkColor error_default_color = { 0, 0xcc00, 0x0000, 0x0000 };
3116 err = gdk_color_to_css (&error_default_color);
3120 data = g_strconcat ("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
3121 "<svg version=\"1.1\"\n"
3122 " xmlns=\"http://www.w3.org/2000/svg\"\n"
3123 " xmlns:xi=\"http://www.w3.org/2001/XInclude\"\n"
3126 " <style type=\"text/css\">\n"
3128 " fill: ", css_fg," !important;\n"
3131 " fill: ", css_warning ? css_warning : warning," !important;\n"
3134 " fill: ", css_error ? css_error : err," !important;\n"
3137 " fill: ", css_success ? css_success : success," !important;\n"
3140 " <xi:include href=\"", icon_info->filename, "\"/>\n"
3147 stream = g_memory_input_stream_new_from_data (data, -1, g_free);
3148 pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
3149 icon_info->desired_size,
3150 icon_info->desired_size,
3154 g_object_unref (stream);
3160 * gtk_icon_info_load_symbolic:
3161 * @icon_info: a #GtkIconInfo
3162 * @fg: a #GdkRGBA representing the foreground color of the icon
3163 * @success_color: (allow-none): a #GdkRGBA representing the warning color
3164 * of the icon or %NULL to use the default color
3165 * @warning_color: (allow-none): a #GdkRGBA representing the warning color
3166 * of the icon or %NULL to use the default color
3167 * @error_color: (allow-none): a #GdkRGBA representing the error color
3168 * of the icon or %NULL to use the default color (allow-none)
3169 * @was_symbolic: (out) (allow-none): a #gboolean, returns whether the
3170 * loaded icon was a symbolic one and whether the @fg color was
3172 * @error: (allow-none): location to store error information on failure,
3175 * Loads an icon, modifying it to match the system colours for the foreground,
3176 * success, warning and error colors provided. If the icon is not a symbolic
3177 * one, the function will return the result from gtk_icon_info_load_icon().
3179 * This allows loading symbolic icons that will match the system theme.
3181 * Unless you are implementing a widget, you will want to use
3182 * g_themed_icon_new_with_default_fallbacks() to load the icon.
3184 * As implementation details, the icon loaded needs to be of SVG type,
3185 * contain the "symbolic" term as the last component of the icon name,
3186 * and use the 'fg', 'success', 'warning' and 'error' CSS styles in the
3189 * See the <ulink url="http://www.freedesktop.org/wiki/SymbolicIcons">Symbolic Icons spec</ulink>
3190 * for more information about symbolic icons.
3192 * Return value: (transfer full): a #GdkPixbuf representing the loaded icon
3197 gtk_icon_info_load_symbolic (GtkIconInfo *icon_info,
3199 GdkRGBA *success_color,
3200 GdkRGBA *warning_color,
3201 GdkRGBA *error_color,
3202 gboolean *was_symbolic,
3211 g_return_val_if_fail (fg != NULL, NULL);
3213 if (!icon_info->filename ||
3214 !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3217 *was_symbolic = FALSE;
3218 return gtk_icon_info_load_icon (icon_info, error);
3222 *was_symbolic = TRUE;
3224 css_fg = gdk_rgba_to_css (fg);
3226 css_success = css_warning = css_error = NULL;
3229 css_warning = gdk_rgba_to_css (warning_color);
3232 css_error = gdk_rgba_to_css (error_color);
3235 css_success = gdk_rgba_to_css (success_color);
3237 pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
3238 css_fg, css_success,
3239 css_warning, css_error,
3242 g_free (css_warning);
3243 g_free (css_success);
3250 * gtk_icon_info_load_symbolic_for_context:
3251 * @icon_info: a #GtkIconInfo
3252 * @context: a #GtkStyleContext
3253 * @was_symbolic: (out) (allow-none): a #gboolean, returns whether the
3254 * loaded icon was a symbolic one and whether the @fg color was
3256 * @error: (allow-none): location to store error information on failure,
3259 * Loads an icon, modifying it to match the system colors for the foreground,
3260 * success, warning and error colors provided. If the icon is not a symbolic
3261 * one, the function will return the result from gtk_icon_info_load_icon().
3262 * This function uses the regular foreground color and the symbolic colors
3263 * with the names "success_color", "warning_color" and "error_color" from
3266 * This allows loading symbolic icons that will match the system theme.
3268 * See gtk_icon_info_load_symbolic() for more details.
3270 * Return value: (transfer full): a #GdkPixbuf representing the loaded icon
3275 gtk_icon_info_load_symbolic_for_context (GtkIconInfo *icon_info,
3276 GtkStyleContext *context,
3277 gboolean *was_symbolic,
3281 GdkRGBA *color = NULL;
3283 gchar *css_fg = NULL, *css_success;
3284 gchar *css_warning, *css_error;
3285 GtkStateFlags state;
3287 if (!icon_info->filename ||
3288 !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3291 *was_symbolic = FALSE;
3292 return gtk_icon_info_load_icon (icon_info, error);
3296 *was_symbolic = TRUE;
3298 state = gtk_style_context_get_state (context);
3299 gtk_style_context_get (context, state, "color", &color, NULL);
3302 css_fg = gdk_rgba_to_css (color);
3303 gdk_rgba_free (color);
3306 css_success = css_warning = css_error = NULL;
3308 if (gtk_style_context_lookup_color (context, "success_color", &rgba))
3309 css_success = gdk_rgba_to_css (&rgba);
3311 if (gtk_style_context_lookup_color (context, "warning_color", &rgba))
3312 css_warning = gdk_rgba_to_css (&rgba);
3314 if (gtk_style_context_lookup_color (context, "error_color", &rgba))
3315 css_error = gdk_rgba_to_css (&rgba);
3317 pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
3318 css_fg, css_success,
3319 css_warning, css_error,
3323 g_free (css_success);
3324 g_free (css_warning);
3331 * gtk_icon_info_load_symbolic_for_style:
3332 * @icon_info: a #GtkIconInfo
3333 * @style: a #GtkStyle to take the colors from
3334 * @state: the widget state to use for colors
3335 * @was_symbolic: (out) (allow-none): a #gboolean, returns whether the
3336 * loaded icon was a symbolic one and whether the @fg color was
3338 * @error: (allow-none): location to store error information on failure,
3341 * Loads an icon, modifying it to match the system colours for the foreground,
3342 * success, warning and error colors provided. If the icon is not a symbolic
3343 * one, the function will return the result from gtk_icon_info_load_icon().
3345 * This allows loading symbolic icons that will match the system theme.
3347 * See gtk_icon_info_load_symbolic() for more details.
3349 * Return value: (transfer full): a #GdkPixbuf representing the loaded icon
3353 * Deprecated: 3.0: Use gtk_icon_info_load_symbolic_for_context() instead
3356 gtk_icon_info_load_symbolic_for_style (GtkIconInfo *icon_info,
3359 gboolean *was_symbolic,
3363 GdkColor success_color;
3364 GdkColor warning_color;
3365 GdkColor error_color;
3367 gchar *css_fg, *css_success;
3368 gchar *css_warning, *css_error;
3370 if (!icon_info->filename ||
3371 !g_str_has_suffix (icon_info->filename, "-symbolic.svg"))
3374 *was_symbolic = FALSE;
3375 return gtk_icon_info_load_icon (icon_info, error);
3379 *was_symbolic = TRUE;
3381 fg = &style->fg[state];
3382 css_fg = gdk_color_to_css (fg);
3384 css_success = css_warning = css_error = NULL;
3386 if (gtk_style_lookup_color (style, "success_color", &success_color))
3387 css_success = gdk_color_to_css (&success_color);
3389 if (gtk_style_lookup_color (style, "warning_color", &warning_color))
3390 css_warning = gdk_color_to_css (&warning_color);
3392 if (gtk_style_lookup_color (style, "error_color", &error_color))
3393 css_error = gdk_color_to_css (&error_color);
3395 pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
3396 css_fg, css_success,
3397 css_warning, css_error,
3401 g_free (css_success);
3402 g_free (css_warning);
3409 * gtk_icon_info_set_raw_coordinates:
3410 * @icon_info: a #GtkIconInfo
3411 * @raw_coordinates: whether the coordinates of embedded rectangles
3412 * and attached points should be returned in their original
3415 * Sets whether the coordinates returned by gtk_icon_info_get_embedded_rect()
3416 * and gtk_icon_info_get_attach_points() should be returned in their
3417 * original form as specified in the icon theme, instead of scaled
3418 * appropriately for the pixbuf returned by gtk_icon_info_load_icon().
3420 * Raw coordinates are somewhat strange; they are specified to be with
3421 * respect to the unscaled pixmap for PNG and XPM icons, but for SVG
3422 * icons, they are in a 1000x1000 coordinate space that is scaled
3423 * to the final size of the icon. You can determine if the icon is an SVG
3424 * icon by using gtk_icon_info_get_filename(), and seeing if it is non-%NULL
3425 * and ends in '.svg'.
3427 * This function is provided primarily to allow compatibility wrappers
3428 * for older API's, and is not expected to be useful for applications.
3433 gtk_icon_info_set_raw_coordinates (GtkIconInfo *icon_info,
3434 gboolean raw_coordinates)
3436 g_return_if_fail (icon_info != NULL);
3438 icon_info->raw_coordinates = raw_coordinates != FALSE;
3441 /* Scale coordinates from the icon data prior to returning
3445 icon_info_scale_point (GtkIconInfo *icon_info,
3451 if (icon_info->raw_coordinates)
3458 if (!icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3461 *x_out = 0.5 + x * icon_info->scale;
3462 *y_out = 0.5 + y * icon_info->scale;
3469 * gtk_icon_info_get_embedded_rect:
3470 * @icon_info: a #GtkIconInfo
3471 * @rectangle: (out): #GdkRectangle in which to store embedded
3472 * rectangle coordinates; coordinates are only stored
3473 * when this function returns %TRUE.
3475 * Gets the coordinates of a rectangle within the icon
3476 * that can be used for display of information such
3477 * as a preview of the contents of a text file.
3478 * See gtk_icon_info_set_raw_coordinates() for further
3479 * information about the coordinate system.
3481 * Return value: %TRUE if the icon has an embedded rectangle
3486 gtk_icon_info_get_embedded_rect (GtkIconInfo *icon_info,
3487 GdkRectangle *rectangle)
3489 g_return_val_if_fail (icon_info != NULL, FALSE);
3491 if (icon_info->data && icon_info->data->has_embedded_rect &&
3492 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3494 gint scaled_x0, scaled_y0;
3495 gint scaled_x1, scaled_y1;
3499 icon_info_scale_point (icon_info,
3500 icon_info->data->x0, icon_info->data->y0,
3501 &scaled_x0, &scaled_y0);
3502 icon_info_scale_point (icon_info,
3503 icon_info->data->x1, icon_info->data->y1,
3504 &scaled_x1, &scaled_y1);
3506 rectangle->x = scaled_x0;
3507 rectangle->y = scaled_y0;
3508 rectangle->width = scaled_x1 - rectangle->x;
3509 rectangle->height = scaled_y1 - rectangle->y;
3519 * gtk_icon_info_get_attach_points:
3520 * @icon_info: a #GtkIconInfo
3521 * @points: (allow-none) (array length=n_points) (out): location to store pointer to an array of points, or %NULL
3522 * free the array of points with g_free().
3523 * @n_points: (allow-none): location to store the number of points in @points, or %NULL
3525 * Fetches the set of attach points for an icon. An attach point
3526 * is a location in the icon that can be used as anchor points for attaching
3527 * emblems or overlays to the icon.
3529 * Return value: %TRUE if there are any attach points for the icon.
3534 gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
3538 g_return_val_if_fail (icon_info != NULL, FALSE);
3540 if (icon_info->data && icon_info->data->n_attach_points &&
3541 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
3547 *points = g_new (GdkPoint, icon_info->data->n_attach_points);
3548 for (i = 0; i < icon_info->data->n_attach_points; i++)
3549 icon_info_scale_point (icon_info,
3550 icon_info->data->attach_points[i].x,
3551 icon_info->data->attach_points[i].y,
3557 *n_points = icon_info->data->n_attach_points;
3573 * gtk_icon_info_get_display_name:
3574 * @icon_info: a #GtkIconInfo
3576 * Gets the display name for an icon. A display name is a
3577 * string to be used in place of the icon name in a user
3578 * visible context like a list of icons.
3580 * Return value: the display name for the icon or %NULL, if
3581 * the icon doesn't have a specified display name. This value
3582 * is owned @icon_info and must not be modified or free.
3586 G_CONST_RETURN gchar *
3587 gtk_icon_info_get_display_name (GtkIconInfo *icon_info)
3589 g_return_val_if_fail (icon_info != NULL, NULL);
3591 if (icon_info->data)
3592 return icon_info->data->display_name;
3603 * gtk_icon_theme_add_builtin_icon:
3604 * @icon_name: the name of the icon to register
3605 * @size: the size at which to register the icon (different
3606 * images can be registered for the same icon name
3607 * at different sizes.)
3608 * @pixbuf: #GdkPixbuf that contains the image to use
3611 * Registers a built-in icon for icon theme lookups. The idea
3612 * of built-in icons is to allow an application or library
3613 * that uses themed icons to function requiring files to
3614 * be present in the file system. For instance, the default
3615 * images for all of GTK+'s stock icons are registered
3618 * In general, if you use gtk_icon_theme_add_builtin_icon()
3619 * you should also install the icon in the icon theme, so
3620 * that the icon is generally available.
3622 * This function will generally be used with pixbufs loaded
3623 * via gdk_pixbuf_new_from_inline().
3628 gtk_icon_theme_add_builtin_icon (const gchar *icon_name,
3632 BuiltinIcon *default_icon;
3636 g_return_if_fail (icon_name != NULL);
3637 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
3639 if (!icon_theme_builtin_icons)
3640 icon_theme_builtin_icons = g_hash_table_new (g_str_hash, g_str_equal);
3642 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
3644 key = g_strdup (icon_name);
3646 key = (gpointer)icon_name; /* Won't get stored */
3648 default_icon = g_new (BuiltinIcon, 1);
3649 default_icon->size = size;
3650 default_icon->pixbuf = g_object_ref (pixbuf);
3651 icons = g_slist_prepend (icons, default_icon);
3653 /* Replaces value, leaves key untouched
3655 g_hash_table_insert (icon_theme_builtin_icons, key, icons);
3658 /* Look up a builtin icon; the min_difference_p and
3659 * has_larger_p out parameters allow us to combine
3660 * this lookup with searching through the actual directories
3661 * of the "hicolor" icon theme. See theme_lookup_icon()
3662 * for how they are used.
3664 static BuiltinIcon *
3665 find_builtin_icon (const gchar *icon_name,
3667 gint *min_difference_p,
3668 gboolean *has_larger_p)
3670 GSList *icons = NULL;
3671 gint min_difference = G_MAXINT;
3672 gboolean has_larger = FALSE;
3673 BuiltinIcon *min_icon = NULL;
3675 if (!icon_theme_builtin_icons)
3678 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
3682 BuiltinIcon *default_icon = icons->data;
3683 int min, max, difference;
3686 min = default_icon->size - 2;
3687 max = default_icon->size + 2;
3688 smaller = size < min;
3690 difference = min - size;
3691 else if (size > max)
3692 difference = size - max;
3696 if (difference == 0)
3698 min_icon = default_icon;
3704 if (difference < min_difference || smaller)
3706 min_difference = difference;
3707 min_icon = default_icon;
3708 has_larger = smaller;
3713 if (difference < min_difference && smaller)
3715 min_difference = difference;
3716 min_icon = default_icon;
3720 icons = icons->next;
3723 if (min_difference_p)
3724 *min_difference_p = min_difference;
3726 *has_larger_p = has_larger;
3732 _gtk_icon_theme_check_reload (GdkDisplay *display)
3736 GtkIconTheme *icon_theme;
3738 n_screens = gdk_display_get_n_screens (display);
3740 for (i = 0; i < n_screens; i++)
3742 screen = gdk_display_get_screen (display, i);
3744 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
3747 icon_theme->priv->check_reload = TRUE;
3748 ensure_valid_themes (icon_theme);
3749 icon_theme->priv->check_reload = FALSE;
3756 * gtk_icon_theme_lookup_by_gicon:
3757 * @icon_theme: a #GtkIconTheme
3758 * @icon: the #GIcon to look up
3759 * @size: desired icon size
3760 * @flags: flags modifying the behavior of the icon lookup
3762 * Looks up an icon and returns a structure containing
3763 * information such as the filename of the icon.
3764 * The icon can then be rendered into a pixbuf using
3765 * gtk_icon_info_load_icon().
3767 * Return value: a #GtkIconInfo structure containing
3768 * information about the icon, or %NULL if the icon
3769 * wasn't found. Free with gtk_icon_info_free()
3774 gtk_icon_theme_lookup_by_gicon (GtkIconTheme *icon_theme,
3777 GtkIconLookupFlags flags)
3781 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
3782 g_return_val_if_fail (G_IS_ICON (icon), NULL);
3784 if (G_IS_LOADABLE_ICON (icon))
3786 info = icon_info_new ();
3787 info->loadable = G_LOADABLE_ICON (g_object_ref (icon));
3789 info->dir_type = ICON_THEME_DIR_UNTHEMED;
3790 info->dir_size = size;
3791 info->desired_size = size;
3792 info->threshold = 2;
3793 info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
3797 else if (G_IS_THEMED_ICON (icon))
3799 const gchar **names;
3801 names = (const gchar **)g_themed_icon_get_names (G_THEMED_ICON (icon));
3802 info = gtk_icon_theme_choose_icon (icon_theme, names, size, flags);
3806 else if (G_IS_EMBLEMED_ICON (icon))
3808 GIcon *base, *emblem;
3810 GtkIconInfo *emblem_info;
3812 if (GTK_IS_NUMERABLE_ICON (icon))
3813 _gtk_numerable_icon_set_background_icon_size (GTK_NUMERABLE_ICON (icon), size / 2);
3815 base = g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon));
3816 info = gtk_icon_theme_lookup_by_gicon (icon_theme, base, size, flags);
3819 list = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon));
3820 for (l = list; l; l = l->next)
3822 emblem = g_emblem_get_icon (G_EMBLEM (l->data));
3823 /* always force size for emblems */
3824 emblem_info = gtk_icon_theme_lookup_by_gicon (icon_theme, emblem, size / 2, flags | GTK_ICON_LOOKUP_FORCE_SIZE);
3826 info->emblem_infos = g_slist_prepend (info->emblem_infos, emblem_info);
3832 else if (GDK_IS_PIXBUF (icon))
3836 pixbuf = GDK_PIXBUF (icon);
3838 if ((flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0)
3840 gint width, height, max;
3844 width = gdk_pixbuf_get_width (pixbuf);
3845 height = gdk_pixbuf_get_height (pixbuf);
3846 max = MAX (width, height);
3847 scale = (gdouble) size / (gdouble) max;
3849 scaled = gdk_pixbuf_scale_simple (pixbuf,
3850 0.5 + width * scale,
3851 0.5 + height * scale,
3852 GDK_INTERP_BILINEAR);
3854 info = gtk_icon_info_new_for_pixbuf (icon_theme, scaled);
3856 g_object_unref (scaled);
3860 info = gtk_icon_info_new_for_pixbuf (icon_theme, pixbuf);
3870 * gtk_icon_info_new_for_pixbuf:
3871 * @icon_theme: a #GtkIconTheme
3872 * @pixbuf: the pixbuf to wrap in a #GtkIconInfo
3874 * Creates a #GtkIconInfo for a #GdkPixbuf.
3876 * Returns: a #GtkIconInfo
3881 gtk_icon_info_new_for_pixbuf (GtkIconTheme *icon_theme,
3886 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
3887 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
3889 info = icon_info_new ();
3890 info->pixbuf = g_object_ref (pixbuf);
3892 info->dir_type = ICON_THEME_DIR_UNTHEMED;