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>
33 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
35 #endif /* G_OS_WIN32 */
37 #include "gtkicontheme.h"
38 #include "gtkiconthemeparser.h"
40 #include "gtksettings.h"
41 #include "gtkprivate.h"
43 #define DEFAULT_THEME_NAME "hicolor"
45 typedef struct _GtkIconData GtkIconData;
50 ICON_THEME_DIR_SCALABLE,
51 ICON_THEME_DIR_THRESHOLD,
52 ICON_THEME_DIR_UNTHEMED
55 /* In reverse search order: */
59 ICON_SUFFIX_XPM = 1 << 0,
60 ICON_SUFFIX_SVG = 1 << 1,
61 ICON_SUFFIX_PNG = 1 << 2,
65 struct _GtkIconThemePrivate
67 guint custom_theme : 1;
68 guint is_screen_singleton : 1;
69 guint pixbuf_supports_svg : 1;
75 gboolean themes_valid;
76 /* A list of all the themes needed to look up icons.
77 * In search order, without duplicates
80 GHashTable *unthemed_icons;
82 /* Note: The keys of this hashtable are owned by the
83 * themedir and unthemed hashtables.
85 GHashTable *all_icons;
87 /* GdkScreen for the icon theme (may be NULL)
91 /* time when we last stat:ed for theme changes */
98 /* Information about the source
101 GdkPixbuf *builtin_pixbuf;
105 /* Information about the directory where
106 * the source was found
108 IconThemeDirType dir_type;
112 /* Parameters influencing the scaled icon
115 gboolean raw_coordinates;
117 /* Cached information if we go ahead and try to load
132 /* In search order */
138 gboolean has_embedded_rect;
141 GdkPoint *attach_points;
142 gint n_attach_points;
149 IconThemeDirType type;
160 GHashTable *icon_data;
166 char *no_svg_filename;
178 time_t mtime; /* 0 == not existing or not a dir */
181 static void gtk_icon_theme_class_init (GtkIconThemeClass *klass);
182 static void gtk_icon_theme_init (GtkIconTheme *icon_theme);
183 static void gtk_icon_theme_finalize (GObject *object);
184 static void theme_dir_destroy (IconThemeDir *dir);
186 static void theme_destroy (IconTheme *theme);
187 static GtkIconInfo *theme_lookup_icon (IconTheme *theme,
188 const char *icon_name,
191 gboolean use_default_icons);
192 static void theme_list_icons (IconTheme *theme,
195 static void theme_subdir_load (GtkIconTheme *icon_theme,
197 GtkIconThemeFile *theme_file,
199 static void do_theme_change (GtkIconTheme *icon_theme);
201 static void blow_themes (GtkIconTheme *icon_themes);
203 static void icon_data_free (GtkIconData *icon_data);
205 static GtkIconInfo *icon_info_new (void);
206 static GtkIconInfo *icon_info_new_builtin (BuiltinIcon *icon);
208 static IconSuffix suffix_from_name (const char *name);
210 static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
212 gint *min_difference_p,
213 gboolean *has_larger_p);
215 static guint signal_changed = 0;
217 static GHashTable *icon_theme_builtin_icons;
220 gtk_icon_theme_get_type (void)
222 static GType type = 0;
226 static const GTypeInfo info =
228 sizeof (GtkIconThemeClass),
229 NULL, /* base_init */
230 NULL, /* base_finalize */
231 (GClassInitFunc) gtk_icon_theme_class_init,
232 NULL, /* class_finalize */
233 NULL, /* class_data */
234 sizeof (GtkIconTheme),
236 (GInstanceInitFunc) gtk_icon_theme_init,
239 type = g_type_register_static (G_TYPE_OBJECT, "GtkIconTheme", &info, 0);
246 * gtk_icon_theme_new:
248 * Creates a new icon theme object. Icon theme objects are used
249 * to lookup up an icon by name in a particular icon theme.
250 * Usually, you'll want to use gtk_icon_theme_get_default()
251 * or gtk_icon_theme_get_for_screen() rather than creating
252 * a new icon theme object for scratch.
254 * Return value: the newly created #GtkIconTheme object.
259 gtk_icon_theme_new (void)
261 return g_object_new (GTK_TYPE_ICON_THEME, NULL);
265 * gtk_icon_theme_get_default:
267 * Gets the icon theme for the default screen. See
268 * gtk_icon_theme_get_for_screen().
270 * Return value: A unique #GtkIconTheme associated with
271 * the default screen. This icon theme is associated with
272 * the screen and can be used as long as the screen
278 gtk_icon_theme_get_default (void)
280 return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
284 * gtk_icon_theme_get_for_screen:
285 * @screen: a #GdkScreen
287 * Gets the icon theme object associated with @screen; if this
288 * function has not previously been called for the given
289 * screen, a new icon theme object will be created and
290 * associated with the screen. Icon theme objects are
291 * fairly expensive to create, so using this function
292 * is usually a better choice than calling than gtk_icon_theme_new()
293 * and setting the screen yourself; by using this function
294 * a single icon theme object will be shared between users.
296 * Return value: A unique #GtkIconTheme associated with
297 * the given screen. This icon theme is associated with
298 * the screen and can be used as long as the screen
304 gtk_icon_theme_get_for_screen (GdkScreen *screen)
306 GtkIconTheme *icon_theme;
308 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
309 g_return_val_if_fail (!screen->closed, NULL);
311 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
314 GtkIconThemePrivate *priv;
316 icon_theme = gtk_icon_theme_new ();
317 gtk_icon_theme_set_screen (icon_theme, screen);
319 priv = icon_theme->priv;
320 priv->is_screen_singleton = TRUE;
322 g_object_set_data (G_OBJECT (screen), "gtk-icon-theme", icon_theme);
329 gtk_icon_theme_class_init (GtkIconThemeClass *klass)
331 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
333 gobject_class->finalize = gtk_icon_theme_finalize;
336 * GtkIconTheme::changed
337 * @icon_theme: the icon theme
339 * Emitted when the current icon theme is switched or GTK+ detects
340 * that a change has occurred in the contents of the current
343 signal_changed = g_signal_new ("changed",
344 G_TYPE_FROM_CLASS (klass),
346 G_STRUCT_OFFSET (GtkIconThemeClass, changed),
348 g_cclosure_marshal_VOID__VOID,
351 g_type_class_add_private (klass, sizeof (GtkIconThemePrivate));
355 /* Callback when the display that the icon theme is attached to
356 * is closed; unset the screen, and if it's the unique theme
357 * for the screen, drop the reference
360 display_closed (GdkDisplay *display,
362 GtkIconTheme *icon_theme)
364 GtkIconThemePrivate *priv = icon_theme->priv;
365 GdkScreen *screen = priv->screen;
366 gboolean was_screen_singleton = priv->is_screen_singleton;
368 if (was_screen_singleton)
370 g_object_set_data (G_OBJECT (screen), "gtk-icon-theme", NULL);
371 priv->is_screen_singleton = FALSE;
374 gtk_icon_theme_set_screen (icon_theme, NULL);
376 if (was_screen_singleton)
378 g_object_unref (icon_theme);
383 update_current_theme (GtkIconTheme *icon_theme)
385 GtkIconThemePrivate *priv = icon_theme->priv;
387 if (!priv->custom_theme)
393 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
394 g_object_get (settings, "gtk-icon-theme-name", &theme, NULL);
398 theme = g_strdup (DEFAULT_THEME_NAME);
400 if (strcmp (priv->current_theme, theme) != 0)
402 g_free (priv->current_theme);
403 priv->current_theme = theme;
405 do_theme_change (icon_theme);
412 /* Callback when the icon theme GtkSetting changes
415 theme_changed (GtkSettings *settings,
417 GtkIconTheme *icon_theme)
419 update_current_theme (icon_theme);
423 unset_screen (GtkIconTheme *icon_theme)
425 GtkIconThemePrivate *priv = icon_theme->priv;
426 GtkSettings *settings;
431 settings = gtk_settings_get_for_screen (priv->screen);
432 display = gdk_screen_get_display (priv->screen);
434 g_signal_handlers_disconnect_by_func (display,
435 (gpointer) display_closed,
437 g_signal_handlers_disconnect_by_func (settings,
438 (gpointer) theme_changed,
446 * gtk_icon_theme_set_screen:
447 * @icon_theme: a #GtkIconTheme
448 * @screen: a #GdkScreen
450 * Sets the screen for an icon theme; the screen is used
451 * to track the user's currently configured icon theme,
452 * which might be different for different screens.
457 gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
460 GtkIconThemePrivate *priv;
461 GtkSettings *settings;
464 g_return_if_fail (GTK_ICON_THEME (icon_theme));
465 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
467 priv = icon_theme->priv;
469 unset_screen (icon_theme);
473 display = gdk_screen_get_display (screen);
474 settings = gtk_settings_get_for_screen (screen);
476 priv->screen = screen;
478 g_signal_connect (display, "closed",
479 G_CALLBACK (display_closed), icon_theme);
480 g_signal_connect (settings, "notify::gtk-icon-theme-name",
481 G_CALLBACK (theme_changed), icon_theme);
484 update_current_theme (icon_theme);
487 /* Checks whether a loader for SVG files has been registered
491 pixbuf_supports_svg ()
493 GSList *formats = gdk_pixbuf_get_formats ();
495 gboolean found_svg = FALSE;
497 for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
499 gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
502 for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
504 if (strcmp (*mime_type, "image/svg") == 0)
508 g_strfreev (mime_types);
511 g_slist_free (formats);
517 gtk_icon_theme_init (GtkIconTheme *icon_theme)
519 GtkIconThemePrivate *priv;
521 priv = g_type_instance_get_private ((GTypeInstance *)icon_theme,
522 GTK_TYPE_ICON_THEME);
523 icon_theme->priv = priv;
525 priv->custom_theme = FALSE;
526 priv->current_theme = g_strdup (DEFAULT_THEME_NAME);
527 priv->search_path = g_new (char *, 5);
530 priv->search_path[0] = g_build_filename (g_get_home_dir (),
533 priv->search_path[1] = g_build_filename (GTK_DATADIR, "pixmaps", NULL);
534 priv->search_path[2] = g_build_filename (GTK_DATADIR, "icons", NULL);
535 priv->search_path[3] = g_strdup ("/usr/share/icons");
536 priv->search_path[4] = g_strdup ("/usr/share/pixmaps");
537 priv->search_path_len = 5;
539 priv->themes_valid = FALSE;
541 priv->unthemed_icons = NULL;
543 priv->pixbuf_supports_svg = pixbuf_supports_svg ();
547 free_dir_mtime (IconThemeDirMtime *dir_mtime)
549 g_free (dir_mtime->dir);
554 do_theme_change (GtkIconTheme *icon_theme)
556 GtkIconThemePrivate *priv = icon_theme->priv;
558 blow_themes (icon_theme);
559 g_signal_emit (G_OBJECT (icon_theme), signal_changed, 0);
561 if (priv->screen && priv->is_screen_singleton)
563 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
564 _gtk_rc_reset_styles (settings);
569 blow_themes (GtkIconTheme *icon_theme)
571 GtkIconThemePrivate *priv = icon_theme->priv;
573 if (priv->themes_valid)
575 g_hash_table_destroy (priv->all_icons);
576 g_list_foreach (priv->themes, (GFunc)theme_destroy, NULL);
577 g_list_free (priv->themes);
578 g_list_foreach (priv->dir_mtimes, (GFunc)free_dir_mtime, NULL);
579 g_list_free (priv->dir_mtimes);
580 g_hash_table_destroy (priv->unthemed_icons);
583 priv->unthemed_icons = NULL;
584 priv->dir_mtimes = NULL;
585 priv->all_icons = NULL;
586 priv->themes_valid = FALSE;
590 gtk_icon_theme_finalize (GObject *object)
592 GtkIconTheme *icon_theme;
593 GtkIconThemePrivate *priv;
596 icon_theme = GTK_ICON_THEME (object);
597 priv = icon_theme->priv;
599 unset_screen (icon_theme);
601 g_free (priv->current_theme);
602 priv->current_theme = NULL;
604 for (i=0; i < priv->search_path_len; i++)
605 g_free (priv->search_path[i]);
607 g_free (priv->search_path);
608 priv->search_path = NULL;
610 blow_themes (icon_theme);
614 * gtk_icon_theme_set_search_path:
615 * @icon_theme: a #GtkIconTheme
616 * @path: array of directories that are searched for icon themes
617 * @n_elements: number of elements in @path.
619 * Sets the search path for the icon theme object. When looking
620 * for an icon theme, GTK+ will search for a subdirectory of
621 * one or more of the directories in @path with the same name
622 * as the icon theme. (Themes from multiple of the path elements
623 * are combined to allow themes to be extended by adding icons
624 * in the user's home directory.)
626 * In addition if an icon found isn't found either in the current
627 * icon theme or the default icon theme, and an image file with
628 * the right name is found directly in one of the elements of
629 * @path, then that image will be used for the icon name.
630 * (This is legacy feature, and new icons should be put
631 * into the default icon theme, which is called DEFAULT_THEME_NAME,
632 * rather than directly on the icon path.)
637 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
641 GtkIconThemePrivate *priv;
644 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
646 priv = icon_theme->priv;
647 for (i = 0; i < priv->search_path_len; i++)
648 g_free (priv->search_path[i]);
650 g_free (priv->search_path);
652 priv->search_path = g_new (gchar *, n_elements);
653 priv->search_path_len = n_elements;
654 for (i = 0; i < priv->search_path_len; i++)
655 priv->search_path[i] = g_strdup (path[i]);
657 do_theme_change (icon_theme);
662 * gtk_icon_theme_get_search_path:
663 * @icon_theme: a #GtkIconTheme
664 * @path: location to store a list of icon theme path directories or %NULL
665 * The stored value should be freed with g_strfreev().
666 * @n_elements: location to store number of elements
669 * Gets the current search path. See gtk_icon_theme_set_search_path().
674 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
678 GtkIconThemePrivate *priv;
681 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
683 priv = icon_theme->priv;
686 *n_elements = priv->search_path_len;
690 *path = g_new (gchar *, priv->search_path_len + 1);
691 for (i = 0; i < priv->search_path_len; i++)
692 (*path)[i] = g_strdup (priv->search_path[i]);
698 * gtk_icon_theme_append_search_path:
699 * @icon_theme: a #GtkIconTheme
700 * @path: directory name to append to the icon path
702 * Appends a directory to the search path. See gtk_icon_theme_set_search_path().
707 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
710 GtkIconThemePrivate *priv;
712 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
713 g_return_if_fail (path != NULL);
715 priv = icon_theme->priv;
717 priv->search_path_len++;
718 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
719 priv->search_path[priv->search_path_len-1] = g_strdup (path);
721 do_theme_change (icon_theme);
725 * gtk_icon_theme_prepend_search_path:
726 * @icon_theme: a #GtkIconTheme
727 * @path: directory name to prepend to the icon path
729 * Prepends a directory to the search path. See gtk_icon_theme_set_search_path().
734 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
737 GtkIconThemePrivate *priv;
740 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
741 g_return_if_fail (path != NULL);
743 priv = icon_theme->priv;
745 priv->search_path_len++;
746 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
748 for (i = priv->search_path_len - 1; i > 0; i--)
749 priv->search_path[i] = priv->search_path[i - 1];
751 priv->search_path[0] = g_strdup (path);
753 do_theme_change (icon_theme);
757 * gtk_icon_theme_set_custom_theme:
758 * @icon_theme: a #GtkIconTheme
759 * @theme_name: name of icon theme to use instead of configured theme
761 * Sets the name of the icon theme that the #GtkIconTheme object uses
762 * overriding system configuration. This function cannot be called
763 * on the icon theme objects returned from gtk_icon_theme_get_default()
764 * and gtk_icon_theme_get_default().
769 gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
770 const gchar *theme_name)
772 GtkIconThemePrivate *priv;
774 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
776 priv = icon_theme->priv;
778 g_return_if_fail (!priv->is_screen_singleton);
780 if (theme_name != NULL)
782 priv->custom_theme = TRUE;
783 if (strcmp (theme_name, priv->current_theme) != 0)
785 g_free (priv->current_theme);
786 priv->current_theme = g_strdup (theme_name);
788 do_theme_change (icon_theme);
793 priv->custom_theme = FALSE;
795 update_current_theme (icon_theme);
800 insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
806 GtkIconThemePrivate *priv;
812 GtkIconThemeFile *theme_file;
813 IconThemeDirMtime *dir_mtime;
814 struct stat stat_buf;
816 priv = icon_theme->priv;
818 for (l = priv->themes; l != NULL; l = l->next)
821 if (strcmp (theme->name, theme_name) == 0)
825 for (i = 0; i < priv->search_path_len; i++)
827 path = g_build_filename (priv->search_path[i],
830 dir_mtime = g_new (IconThemeDirMtime, 1);
831 dir_mtime->dir = path;
832 if (stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
833 dir_mtime->mtime = stat_buf.st_mtime;
835 dir_mtime->mtime = 0;
837 priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
841 for (i = 0; i < priv->search_path_len; i++)
843 path = g_build_filename (priv->search_path[i],
847 if (g_file_test (path, G_FILE_TEST_IS_REGULAR)) {
848 if (g_file_get_contents (path, &contents, NULL, NULL)) {
849 theme_file = _gtk_icon_theme_file_new_from_string (contents, NULL);
858 if (theme_file == NULL)
861 theme = g_new (IconTheme, 1);
862 if (!_gtk_icon_theme_file_get_locale_string (theme_file,
865 &theme->display_name))
867 g_warning ("Theme file for %s has no name\n", theme_name);
869 _gtk_icon_theme_file_free (theme_file);
873 if (!_gtk_icon_theme_file_get_string (theme_file,
878 g_warning ("Theme file for %s has no directories\n", theme_name);
879 g_free (theme->display_name);
881 _gtk_icon_theme_file_free (theme_file);
885 theme->name = g_strdup (theme_name);
886 _gtk_icon_theme_file_get_locale_string (theme_file,
890 _gtk_icon_theme_file_get_string (theme_file,
895 dirs = g_strsplit (directories, ",", 0);
898 for (i = 0; dirs[i] != NULL; i++)
899 theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
903 theme->dirs = g_list_reverse (theme->dirs);
905 g_free (directories);
907 /* Prepend the finished theme */
908 priv->themes = g_list_prepend (priv->themes, theme);
910 if (_gtk_icon_theme_file_get_string (theme_file,
915 themes = g_strsplit (inherits, ",", 0);
917 for (i = 0; themes[i] != NULL; i++)
918 insert_theme (icon_theme, themes[i]);
925 _gtk_icon_theme_file_free (theme_file);
929 free_unthemed_icon (UnthemedIcon *unthemed_icon)
931 if (unthemed_icon->svg_filename)
932 g_free (unthemed_icon->svg_filename);
933 if (unthemed_icon->svg_filename)
934 g_free (unthemed_icon->no_svg_filename);
935 g_free (unthemed_icon);
939 load_themes (GtkIconTheme *icon_theme)
941 GtkIconThemePrivate *priv;
944 char *dir, *base_name, *dot;
947 UnthemedIcon *unthemed_icon;
948 IconSuffix old_suffix, new_suffix;
951 priv = icon_theme->priv;
953 priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
955 insert_theme (icon_theme, priv->current_theme);
957 /* Always look in the "default" icon theme */
958 insert_theme (icon_theme, DEFAULT_THEME_NAME);
959 priv->themes = g_list_reverse (priv->themes);
961 priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
962 g_free, (GDestroyNotify)free_unthemed_icon);
964 for (base = 0; base < icon_theme->priv->search_path_len; base++)
966 dir = icon_theme->priv->search_path[base];
967 gdir = g_dir_open (dir, 0, NULL);
972 while ((file = g_dir_read_name (gdir)))
974 new_suffix = suffix_from_name (file);
976 if (new_suffix != ICON_SUFFIX_NONE)
978 abs_file = g_build_filename (dir, file, NULL);
980 base_name = g_strdup (file);
982 dot = strrchr (base_name, '.');
986 if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
989 if (new_suffix == ICON_SUFFIX_SVG)
991 if (unthemed_icon->no_svg_filename)
994 unthemed_icon->svg_filename = abs_file;
998 if (unthemed_icon->no_svg_filename)
1000 old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
1001 if (new_suffix > old_suffix)
1003 g_free (unthemed_icon->no_svg_filename);
1004 unthemed_icon->no_svg_filename = abs_file;
1010 unthemed_icon->no_svg_filename = abs_file;
1017 unthemed_icon = g_new0 (UnthemedIcon, 1);
1019 if (new_suffix == ICON_SUFFIX_SVG)
1020 unthemed_icon->svg_filename = abs_file;
1022 unthemed_icon->svg_filename = abs_file;
1024 g_hash_table_insert (priv->unthemed_icons,
1027 g_hash_table_insert (priv->all_icons,
1035 priv->themes_valid = TRUE;
1037 g_get_current_time(&tv);
1038 priv->last_stat_time = tv.tv_sec;
1042 ensure_valid_themes (GtkIconTheme *icon_theme)
1044 GtkIconThemePrivate *priv = icon_theme->priv;
1047 if (priv->themes_valid)
1049 g_get_current_time(&tv);
1051 if (ABS (tv.tv_sec - priv->last_stat_time) > 5)
1052 gtk_icon_theme_rescan_if_needed (icon_theme);
1055 if (!priv->themes_valid)
1056 load_themes (icon_theme);
1060 * gtk_icon_theme_lookup_icon:
1061 * @icon_theme: a #GtkIconTheme
1062 * @icon_name: the name of the icon to lookup
1063 * @size: desired icon size
1064 * @flags: flags modifying the behavior of the icon lookup
1066 * Looks up a named icon and returns a structure containing
1067 * information such as the filename of the icon. The icon
1068 * can then be rendered into a pixbuf using
1069 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1070 * combines these two steps if all you need is the pixbuf.)
1072 * Return value: a #GtkIconInfo structure containing information
1073 * about the icon, or %NULL if the icon wasn't found. Free with
1074 * gtk_icon_info_free()
1079 gtk_icon_theme_lookup_icon (GtkIconTheme *icon_theme,
1080 const gchar *icon_name,
1082 GtkIconLookupFlags flags)
1084 GtkIconThemePrivate *priv;
1086 GtkIconInfo *icon_info = NULL;
1087 UnthemedIcon *unthemed_icon;
1089 gboolean use_builtin;
1090 gboolean found_default;
1092 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1093 g_return_val_if_fail (icon_name != NULL, NULL);
1094 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1095 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1097 priv = icon_theme->priv;
1099 if (flags & GTK_ICON_LOOKUP_NO_SVG)
1101 else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
1104 allow_svg = priv->pixbuf_supports_svg;
1106 use_builtin = (flags & GTK_ICON_LOOKUP_USE_BUILTIN);
1108 ensure_valid_themes (icon_theme);
1110 found_default = FALSE;
1114 IconTheme *icon_theme = l->data;
1116 if (strcmp (icon_theme->name, DEFAULT_THEME_NAME) == 0)
1117 found_default = TRUE;
1119 icon_info = theme_lookup_icon (icon_theme, icon_name, size, allow_svg, use_builtin);
1128 BuiltinIcon *builtin = find_builtin_icon (icon_name, size, NULL, NULL);
1131 icon_info = icon_info_new_builtin (builtin);
1136 unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_name);
1139 icon_info = icon_info_new ();
1141 /* A SVG icon, when allowed, beats out a XPM icon, but not
1145 unthemed_icon->svg_filename &&
1146 (!unthemed_icon->no_svg_filename ||
1147 suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
1148 icon_info->filename = g_strdup (unthemed_icon->svg_filename);
1149 else if (unthemed_icon->no_svg_filename)
1150 icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);
1152 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1157 icon_info->desired_size = size;
1160 static gboolean check_for_default_theme = TRUE;
1161 char *default_theme_path;
1162 gboolean found = FALSE;
1165 if (check_for_default_theme)
1167 check_for_default_theme = FALSE;
1169 for (i = 0; !found && i < priv->search_path_len; i++)
1171 default_theme_path = g_build_filename (priv->search_path[i],
1175 found = g_file_test (default_theme_path, G_FILE_TEST_IS_REGULAR);
1176 g_free (default_theme_path);
1180 g_warning (_("Could not find the icon '%s'. The '%s' theme\n"
1181 "was not found either, perhaps you need to install it.\n"
1182 "You can get a copy from:\n"
1184 icon_name, DEFAULT_THEME_NAME, "http://freedesktop.org/Software/icon-theme/releases");
1194 gtk_icon_theme_error_quark (void)
1196 static GQuark q = 0;
1198 q = g_quark_from_static_string ("gtk-icon-theme-error-quark");
1204 * gtk_icon_theme_load_icon:
1205 * @icon_theme: a #GtkIconTheme
1206 * @icon_name: the name of the icon to lookup
1207 * @size: the desired icon size. The resulting icon may not be
1208 * exactly this size; see gtk_icon_info_load_icon().
1209 * @flags: flags modifying the behavior of the icon lookup
1210 * @error: Location to store error information on failure, or %NULL.
1212 * Looks up an icon in an icon theme, scales it to the given size
1213 * and renders it into a pixbuf. This is a convenience function;
1214 * if more details about the icon are needed, use
1215 * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1217 * Return value: the rendered icon; this may be a newly created icon
1218 * or a new reference to an internal icon, so you must not modify
1219 * the icon. Use g_object_unref() to release your reference to the
1220 * icon. %NULL if the icon isn't found.
1225 gtk_icon_theme_load_icon (GtkIconTheme *icon_theme,
1226 const gchar *icon_name,
1228 GtkIconLookupFlags flags,
1231 GtkIconInfo *icon_info;
1232 GdkPixbuf *pixbuf = NULL;
1234 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1235 g_return_val_if_fail (icon_name != NULL, NULL);
1236 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1237 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1238 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1240 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
1241 flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1244 g_set_error (error, GTK_ICON_THEME_ERROR, GTK_ICON_THEME_NOT_FOUND,
1245 _("Icon '%s' not present in theme"), icon_name);
1249 pixbuf = gtk_icon_info_load_icon (icon_info, error);
1250 gtk_icon_info_free (icon_info);
1256 * gtk_icon_theme_has_icon:
1257 * @icon_theme: a #GtkIconTheme
1258 * @icon_name: the name of an icon
1260 * Checks whether an icon theme includes an icon
1261 * for a particular name.
1263 * Return value: %TRUE if @icon_theme includes an
1264 * icon for @icon_name.
1269 gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
1270 const char *icon_name)
1272 GtkIconThemePrivate *priv;
1274 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1276 priv = icon_theme->priv;
1278 ensure_valid_themes (icon_theme);
1280 if (g_hash_table_lookup_extended (priv->all_icons,
1281 icon_name, NULL, NULL))
1284 if (icon_theme_builtin_icons &&
1285 g_hash_table_lookup_extended (icon_theme_builtin_icons,
1286 icon_name, NULL, NULL))
1294 add_key_to_hash (gpointer key,
1298 GHashTable *hash = user_data;
1300 g_hash_table_insert (hash, key, NULL);
1304 add_key_to_list (gpointer key,
1308 GList **list = user_data;
1310 *list = g_list_prepend (*list, g_strdup (key));
1314 * gtk_icon_theme_list_icons:
1315 * @icon_theme: a #GtkIconTheme
1316 * @context: a string identifying a particular type of icon,
1317 * or %NULL to list all icons.
1319 * Lists the icons in the current icon theme. Only a subset
1320 * of the icons can be listed by providing a context string.
1321 * The set of values for the context string is system dependent,
1322 * but will typically include such values as 'apps' and
1325 * Return value: a #GList list holding the names of all the
1326 * icons in the theme. You must first free each element
1327 * in the list with g_free(), then free the list itself
1328 * with g_list_free().
1333 gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
1334 const char *context)
1336 GtkIconThemePrivate *priv;
1339 GQuark context_quark;
1341 priv = icon_theme->priv;
1343 ensure_valid_themes (icon_theme);
1347 context_quark = g_quark_try_string (context);
1355 icons = g_hash_table_new (g_str_hash, g_str_equal);
1360 theme_list_icons (l->data, icons, context_quark);
1364 if (context_quark == 0)
1365 g_hash_table_foreach (priv->unthemed_icons,
1371 g_hash_table_foreach (icons,
1375 g_hash_table_destroy (icons);
1381 * gtk_icon_theme_get_example_icon_name:
1382 * @icon_theme: a #GtkIconTheme
1384 * Gets the name of an icon that is representative of the
1385 * current theme (for instance, to use when presenting
1386 * a list of themes to the user.)
1388 * Return value: the name of an example icon or %NULL.
1389 * Free with g_free().
1394 gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
1396 GtkIconThemePrivate *priv;
1400 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1402 priv = icon_theme->priv;
1404 ensure_valid_themes (icon_theme);
1411 return g_strdup (theme->example);
1420 * gtk_icon_theme_rescan_if_needed:
1421 * @icon_theme: a #GtkIconTheme
1423 * Checks to see if the icon theme has changed; if it has, any
1424 * currently cached information is discarded and will be reloaded
1425 * next time @icon_theme is accessed.
1427 * Return value: %TRUE if the icon theme has changed and needed
1433 gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
1435 GtkIconThemePrivate *priv;
1436 IconThemeDirMtime *dir_mtime;
1439 struct stat stat_buf;
1442 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1444 priv = icon_theme->priv;
1446 for (d = priv->dir_mtimes; d != NULL; d = d->next)
1448 dir_mtime = d->data;
1450 stat_res = stat (dir_mtime->dir, &stat_buf);
1452 /* dir mtime didn't change */
1453 if (stat_res == 0 &&
1454 S_ISDIR (stat_buf.st_mode) &&
1455 dir_mtime->mtime == stat_buf.st_mtime)
1457 /* didn't exist before, and still doesn't */
1458 if (dir_mtime->mtime == 0 &&
1459 (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
1462 do_theme_change (icon_theme);
1466 g_get_current_time (&tv);
1467 priv->last_stat_time = tv.tv_sec;
1473 theme_destroy (IconTheme *theme)
1475 g_free (theme->display_name);
1476 g_free (theme->comment);
1477 g_free (theme->name);
1478 g_free (theme->example);
1480 g_list_foreach (theme->dirs, (GFunc)theme_dir_destroy, NULL);
1481 g_list_free (theme->dirs);
1486 theme_dir_destroy (IconThemeDir *dir)
1488 g_hash_table_destroy (dir->icons);
1490 g_hash_table_destroy (dir->icon_data);
1496 theme_dir_size_difference (IconThemeDir *dir, int size, gboolean *smaller)
1501 case ICON_THEME_DIR_FIXED:
1502 *smaller = size < dir->size;
1503 return abs (size - dir->size);
1505 case ICON_THEME_DIR_SCALABLE:
1506 *smaller = size < dir->min_size;
1507 if (size < dir->min_size)
1508 return dir->min_size - size;
1509 if (size > dir->max_size)
1510 return size - dir->max_size;
1513 case ICON_THEME_DIR_THRESHOLD:
1514 min = dir->size - dir->threshold;
1515 max = dir->size + dir->threshold;
1516 *smaller = size < min;
1523 case ICON_THEME_DIR_UNTHEMED:
1524 g_assert_not_reached ();
1527 g_assert_not_reached ();
1532 string_from_suffix (IconSuffix suffix)
1536 case ICON_SUFFIX_XPM:
1538 case ICON_SUFFIX_SVG:
1540 case ICON_SUFFIX_PNG:
1543 g_assert_not_reached();
1549 suffix_from_name (const char *name)
1553 if (g_str_has_suffix (name, ".png"))
1554 retval = ICON_SUFFIX_PNG;
1555 else if (g_str_has_suffix (name, ".svg"))
1556 retval = ICON_SUFFIX_SVG;
1557 else if (g_str_has_suffix (name, ".xpm"))
1558 retval = ICON_SUFFIX_XPM;
1560 retval = ICON_SUFFIX_NONE;
1566 best_suffix (IconSuffix suffix,
1569 if ((suffix & ICON_SUFFIX_PNG) != 0)
1570 return ICON_SUFFIX_PNG;
1571 else if (allow_svg && ((suffix & ICON_SUFFIX_SVG) != 0))
1572 return ICON_SUFFIX_SVG;
1573 else if ((suffix & ICON_SUFFIX_XPM) != 0)
1574 return ICON_SUFFIX_XPM;
1576 return ICON_SUFFIX_NONE;
1579 static GtkIconInfo *
1580 theme_lookup_icon (IconTheme *theme,
1581 const char *icon_name,
1584 gboolean use_builtin)
1587 IconThemeDir *dir, *min_dir;
1589 int min_difference, difference;
1590 BuiltinIcon *closest_builtin = NULL;
1591 gboolean smaller, has_larger;
1594 min_difference = G_MAXINT;
1598 /* Builtin icons are logically part of the default theme and
1599 * are searched before other subdirectories of the default theme.
1601 if (strcmp (theme->name, DEFAULT_THEME_NAME) == 0 && use_builtin)
1603 closest_builtin = find_builtin_icon (icon_name, size,
1607 if (min_difference == 0)
1608 return icon_info_new_builtin (closest_builtin);
1616 suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
1618 if (suffix != ICON_SUFFIX_NONE &&
1619 (allow_svg || suffix != ICON_SUFFIX_SVG))
1621 difference = theme_dir_size_difference (dir, size, &smaller);
1623 if (difference == 0)
1631 if (difference < min_difference || smaller)
1633 min_difference = difference;
1635 closest_builtin = NULL;
1636 has_larger = smaller;
1641 if (difference < min_difference && smaller)
1643 min_difference = difference;
1645 closest_builtin = NULL;
1654 if (closest_builtin)
1655 return icon_info_new_builtin (closest_builtin);
1659 GtkIconInfo *icon_info = icon_info_new ();
1661 suffix = GPOINTER_TO_UINT (g_hash_table_lookup (min_dir->icons, icon_name));
1662 suffix = best_suffix (suffix, allow_svg);
1663 g_assert (suffix != ICON_SUFFIX_NONE);
1665 file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
1666 icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
1669 if (min_dir->icon_data != NULL)
1670 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
1672 icon_info->dir_type = min_dir->type;
1673 icon_info->dir_size = min_dir->size;
1674 icon_info->threshold = min_dir->threshold;
1683 theme_list_icons (IconTheme *theme, GHashTable *icons,
1686 GList *l = theme->dirs;
1693 if (context == dir->context ||
1695 g_hash_table_foreach (dir->icons,
1704 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
1706 GtkIconThemeFile *icon_file;
1717 if (g_file_get_contents (path, &contents, NULL, NULL))
1719 icon_file = _gtk_icon_theme_file_new_from_string (contents, NULL);
1723 base_name = g_strdup (name);
1724 dot = strrchr (base_name, '.');
1727 data = g_new0 (GtkIconData, 1);
1728 g_hash_table_replace (dir->icon_data, base_name, data);
1730 if (_gtk_icon_theme_file_get_string (icon_file, "Icon Data",
1731 "EmbeddedTextRectangle",
1734 split = g_strsplit (str, ",", 4);
1737 while (split[i] != NULL)
1742 data->has_embedded_rect = TRUE;
1743 data->x0 = atoi (split[0]);
1744 data->y0 = atoi (split[1]);
1745 data->x1 = atoi (split[2]);
1746 data->y1 = atoi (split[3]);
1754 if (_gtk_icon_theme_file_get_string (icon_file, "Icon Data",
1758 split = g_strsplit (str, "|", -1);
1761 while (split[i] != NULL)
1764 data->n_attach_points = i;
1765 data->attach_points = g_malloc (sizeof (GdkPoint) * i);
1768 while (split[i] != NULL && i < data->n_attach_points)
1770 split_point = strchr (split[i], ',');
1775 data->attach_points[i].x = atoi (split[i]);
1776 data->attach_points[i].y = atoi (split_point);
1785 _gtk_icon_theme_file_get_locale_string (icon_file, "Icon Data",
1787 &data->display_name);
1789 _gtk_icon_theme_file_free (icon_file);
1797 scan_directory (GtkIconThemePrivate *icon_theme,
1798 IconThemeDir *dir, char *full_dir)
1802 char *base_name, *dot;
1804 IconSuffix suffix, hash_suffix;
1806 dir->icons = g_hash_table_new_full (g_str_hash, g_str_equal,
1809 gdir = g_dir_open (full_dir, 0, NULL);
1814 while ((name = g_dir_read_name (gdir)))
1816 if (g_str_has_suffix (name, ".icon"))
1818 if (dir->icon_data == NULL)
1819 dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
1820 g_free, (GDestroyNotify)icon_data_free);
1822 path = g_build_filename (full_dir, name, NULL);
1823 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
1824 load_icon_data (dir, path, name);
1831 suffix = suffix_from_name (name);
1832 if (suffix == ICON_SUFFIX_NONE)
1835 base_name = g_strdup (name);
1836 dot = strrchr (base_name, '.');
1839 hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
1840 g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
1841 g_hash_table_insert (icon_theme->all_icons, base_name, NULL);
1848 theme_subdir_load (GtkIconTheme *icon_theme,
1850 GtkIconThemeFile *theme_file,
1856 IconThemeDirType type;
1857 char *context_string;
1865 if (!_gtk_icon_theme_file_get_integer (theme_file,
1870 g_warning ("Theme directory %s of theme %s has no size field\n", subdir, theme->name);
1874 type = ICON_THEME_DIR_THRESHOLD;
1875 if (_gtk_icon_theme_file_get_string (theme_file, subdir, "Type", &type_string))
1877 if (strcmp (type_string, "Fixed") == 0)
1878 type = ICON_THEME_DIR_FIXED;
1879 else if (strcmp (type_string, "Scalable") == 0)
1880 type = ICON_THEME_DIR_SCALABLE;
1881 else if (strcmp (type_string, "Threshold") == 0)
1882 type = ICON_THEME_DIR_THRESHOLD;
1884 g_free (type_string);
1888 if (_gtk_icon_theme_file_get_string (theme_file, subdir, "Context", &context_string))
1890 context = g_quark_from_string (context_string);
1891 g_free (context_string);
1894 if (!_gtk_icon_theme_file_get_integer (theme_file,
1900 if (!_gtk_icon_theme_file_get_integer (theme_file,
1906 if (!_gtk_icon_theme_file_get_integer (theme_file,
1912 for (base = 0; base < icon_theme->priv->search_path_len; base++)
1914 full_dir = g_build_filename (icon_theme->priv->search_path[base],
1918 if (g_file_test (full_dir, G_FILE_TEST_IS_DIR))
1920 dir = g_new (IconThemeDir, 1);
1922 dir->context = context;
1924 dir->min_size = min_size;
1925 dir->max_size = max_size;
1926 dir->threshold = threshold;
1927 dir->dir = full_dir;
1928 dir->icon_data = NULL;
1930 scan_directory (icon_theme->priv, dir, full_dir);
1932 theme->dirs = g_list_append (theme->dirs, dir);
1940 icon_data_free (GtkIconData *icon_data)
1942 g_free (icon_data->attach_points);
1943 g_free (icon_data->display_name);
1951 gtk_icon_info_get_type (void)
1953 static GType our_type = 0;
1956 our_type = g_boxed_type_register_static ("GtkIconInfo",
1957 (GBoxedCopyFunc) gtk_icon_info_copy,
1958 (GBoxedFreeFunc) gtk_icon_info_free);
1963 static GtkIconInfo *
1964 icon_info_new (void)
1966 GtkIconInfo *icon_info = g_new0 (GtkIconInfo, 1);
1968 icon_info->scale = -1.;
1973 static GtkIconInfo *
1974 icon_info_new_builtin (BuiltinIcon *icon)
1976 GtkIconInfo *icon_info = icon_info_new ();
1978 icon_info->builtin_pixbuf = g_object_ref (icon->pixbuf);
1979 icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
1980 icon_info->dir_size = icon->size;
1981 icon_info->threshold = 2;
1987 * gtk_icon_info_copy:
1988 * @icon_info: a #GtkIconInfo
1990 * Make a copy of a #GtkIconInfo.
1992 * Return value: the new GtkIconInfo
1997 gtk_icon_info_copy (GtkIconInfo *icon_info)
2001 g_return_val_if_fail (icon_info != NULL, NULL);
2003 copy = g_memdup (icon_info, sizeof (GtkIconInfo));
2004 if (copy->builtin_pixbuf)
2005 g_object_ref (copy->builtin_pixbuf);
2007 g_object_ref (copy->pixbuf);
2008 if (copy->load_error)
2009 copy->load_error = g_error_copy (copy->load_error);
2011 copy->filename = g_strdup (copy->filename);
2017 * gtk_icon_info_free:
2018 * @icon_info: a #GtkIconInfo
2020 * Free a #GtkIconInfo and associated information
2025 gtk_icon_info_free (GtkIconInfo *icon_info)
2027 g_return_if_fail (icon_info != NULL);
2029 if (icon_info->filename)
2030 g_free (icon_info->filename);
2031 if (icon_info->builtin_pixbuf)
2032 g_object_unref (icon_info->builtin_pixbuf);
2033 if (icon_info->pixbuf)
2034 g_object_unref (icon_info->pixbuf);
2040 * gtk_icon_info_get_base_size:
2041 * @icon_info: a #GtkIconInfo
2043 * Gets the base size for the icon. The base size
2044 * is a size for the icon that was specified by
2045 * the icon theme creator. This may be different
2046 * than the actual size of image; an example of
2047 * this is small emblem icons that can be attached
2048 * to a larger icon. These icons will be given
2049 * the same base size as the larger icons to which
2050 * they are attached.
2052 * Return value: the base size, or 0, if no base
2053 * size is known for the icon.
2058 gtk_icon_info_get_base_size (GtkIconInfo *icon_info)
2060 g_return_val_if_fail (icon_info != NULL, 0);
2062 return icon_info->dir_size;
2066 * gtk_icon_info_get_filename:
2067 * @icon_info: a #GtkIconInfo
2069 * Gets the filename for the icon. If the
2070 * %GTK_ICON_LOOKUP_USE_BUILTIN flag was passed
2071 * to gtk_icon_theme_lookup_icon(), there may be
2072 * no filename if a builtin icon is returned; in this
2073 * case, you should use gtk_icon_info_get_builtin_pixbuf().
2075 * Return value: the filename for the icon, or %NULL
2076 * if gtk_icon_info_get_builtin_pixbuf() should
2077 * be used instead. The return value is owned by
2078 * GTK+ and should not be modified or freed.
2082 G_CONST_RETURN gchar *
2083 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2085 g_return_val_if_fail (icon_info != NULL, NULL);
2087 return icon_info->filename;
2091 * gtk_icon_info_get_builtin_pixbuf:
2092 * @icon_info: a #GtkIconInfo structure
2094 * Gets the built-in image for this icon, if any. To allow
2095 * GTK+ to use built in icon images, you must pass the
2096 * %GTK_ICON_LOOKUP_USE_BUILTIN to
2097 * gtk_icon_theme_lookup_icon().
2099 * Return value: the built-in image pixbuf, or %NULL. No
2100 * extra reference is added to the returned pixbuf, so if
2101 * you want to keep it around, you must use g_object_ref().
2102 * The returned image must not be modified.
2107 gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
2109 g_return_val_if_fail (icon_info != NULL, NULL);
2111 return icon_info->builtin_pixbuf;
2115 load_svg_at_size (const gchar *filename,
2119 GdkPixbuf *pixbuf = NULL;
2120 GdkPixbufLoader *loader = NULL;
2124 if (!g_file_get_contents (filename,
2125 &contents, &length, error))
2128 loader = gdk_pixbuf_loader_new ();
2129 gdk_pixbuf_loader_set_size (loader, size, size);
2131 if (!gdk_pixbuf_loader_write (loader, contents, length, error))
2133 gdk_pixbuf_loader_close (loader, NULL);
2137 if (!gdk_pixbuf_loader_close (loader, error))
2140 pixbuf = g_object_ref (gdk_pixbuf_loader_get_pixbuf (loader));
2144 g_object_unref (loader);
2149 /* This function contains the complicatd logic for deciding
2150 * on the size at which to load the icon and loading it at
2154 icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
2155 gboolean scale_only)
2157 int image_width, image_height;
2158 GdkPixbuf *source_pixbuf;
2160 /* First check if we already succeeded have the necessary
2161 * information (or failed earlier)
2163 if (scale_only && icon_info->scale >= 0)
2166 if (icon_info->pixbuf)
2169 if (icon_info->load_error)
2172 /* SVG icons are a special case - we just immediately scale them
2173 * to the desired size
2175 if (icon_info->filename && g_str_has_suffix (icon_info->filename, ".svg"))
2177 icon_info->scale = icon_info->desired_size / 1000.;
2182 icon_info->pixbuf = load_svg_at_size (icon_info->filename,
2183 icon_info->desired_size,
2184 &icon_info->load_error);
2186 return icon_info->pixbuf != NULL;
2189 /* In many cases, the scale can be determined without actual access
2190 * to the icon file. This is generally true when we have a size
2191 * for the directory where the icon is; the image size doesn't
2192 * matter in that case.
2194 if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
2195 icon_info->scale = 1.0;
2196 else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
2198 if (icon_info->desired_size >= icon_info->dir_size - icon_info->threshold &&
2199 icon_info->desired_size <= icon_info->dir_size + icon_info->threshold)
2200 icon_info->scale = 1.0;
2201 else if (icon_info->dir_size > 0)
2202 icon_info->scale =(gdouble) icon_info->desired_size / icon_info->dir_size;
2204 else if (icon_info->dir_type == ICON_THEME_DIR_SCALABLE)
2206 if (icon_info->dir_size > 0)
2207 icon_info->scale = (gdouble) icon_info->desired_size / icon_info->dir_size;
2210 if (icon_info->scale >= 0. && scale_only)
2213 /* At this point, we need to actually get the icon; either from the
2214 * builting image or by loading the file
2216 if (icon_info->builtin_pixbuf)
2217 source_pixbuf = g_object_ref (icon_info->builtin_pixbuf);
2220 source_pixbuf = gdk_pixbuf_new_from_file (icon_info->filename,
2221 &icon_info->load_error);
2226 /* Do scale calculations that depend on the image size
2228 image_width = gdk_pixbuf_get_width (source_pixbuf);
2229 image_height = gdk_pixbuf_get_height (source_pixbuf);
2231 if (icon_info->scale < 0.0)
2233 gint image_size = MAX (image_width, image_height);
2235 icon_info->scale = icon_info->desired_size / image_size;
2237 icon_info->scale = 1.0;
2239 if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED)
2240 icon_info->scale = MAX (icon_info->scale, 1.0);
2243 /* We don't short-circuit out here for scale_only, since, now
2244 * we've loaded the icon, we might as well go ahead and finish
2245 * the job. This is a bit of a waste when we scale here
2246 * and never get the final pixbuf; at the cost of a bit of
2247 * extra complexity, we could keep the source pixbuf around
2248 * but not actually scale it until neede.
2251 if (icon_info->scale == 1.0)
2252 icon_info->pixbuf = source_pixbuf;
2255 icon_info->pixbuf = gdk_pixbuf_scale_simple (source_pixbuf,
2256 0.5 + image_width * icon_info->scale,
2257 0.5 + image_height * icon_info->scale,
2258 GDK_INTERP_BILINEAR);
2259 g_object_unref (source_pixbuf);
2266 * gtk_icon_info_load_icon:
2267 * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
2270 * Renders an icon previously looked up in an icon theme using
2271 * gtk_icon_theme_lookup_icon(); the size will be based on the size
2272 * pssed to gtk_icon_theme_lookup_icon(). Note that the resulting
2273 * pixbuf may not be exactly this size; an icon theme may have icons
2274 * that differ slightly from their nominal sizes, and in addition GTK+
2275 * will avoid scaling icons that it considers sufficiently close to the
2276 * requested size. (This maintains sharpness.)
2278 * Return value: the rendered icon; this may be a newly created icon
2279 * or a new reference to an internal icon, so you must not modify
2280 * the icon. Use g_object_unref() to release your reference to the
2286 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
2289 g_return_val_if_fail (icon_info != NULL, NULL);
2291 g_return_val_if_fail (icon_info != NULL, NULL);
2292 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2294 icon_info_ensure_scale_and_pixbuf (icon_info, FALSE);
2296 if (icon_info->load_error)
2298 g_propagate_error (error, icon_info->load_error);
2302 return g_object_ref (icon_info->pixbuf);
2306 * gtk_icon_info_set_raw_coordinates:
2307 * @icon_info: a #GtkIconInfo
2308 * @raw_coordinates: whether the coordinates of embedded rectangles
2309 * and attached points should be returned in their original
2312 * Sets whether the coordinates returned by gtk_icon_info_get_embedded_rect()
2313 * and gtk_icon_info_get_attach_points() should be returned in their
2314 * original form as specified in the icon theme, instead of scaled
2315 * appropriately for the pixbuf returned by gtk_icon_info_load_icon().
2317 * Raw coordinates are somewhat strange; they are specified to be with
2318 * respect to the unscaled pixmap for PNG and XPM icons, but for SVG
2319 * icons, they are in a 1000x1000 coordinate space that is scaled
2320 * to the final size of the icon. You can determine if the icon is an SVG
2321 * icon by using gtk_icon_info_get_filename(), and seeing if it is non-%NULL
2322 * and ends in '.svg'.
2324 * This function is provided primarily to allow compatibility wrappers
2325 * for older API's, and is not expected to be useful for applications.
2330 gtk_icon_info_set_raw_coordinates (GtkIconInfo *icon_info,
2331 gboolean raw_coordinates)
2333 g_return_if_fail (icon_info != NULL);
2335 icon_info->raw_coordinates = raw_coordinates != FALSE;
2338 /* Scale coordinates from the icon data prior to returning
2342 icon_info_scale_point (GtkIconInfo *icon_info,
2348 if (icon_info->raw_coordinates)
2355 if (!icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2358 *x_out = 0.5 + x * icon_info->scale;
2359 *y_out = 0.5 + y * icon_info->scale;
2366 * gtk_icon_info_get_embedded_rect:
2367 * @icon_info: a #GtkIconInfo
2368 * @rectangle: #GdkRectangle in which to store embedded
2369 * rectangle coordinates; coordinates are only stored
2370 * when this function returns %TRUE.
2372 * Gets the coordinates of a rectangle within the icon
2373 * that can be used for display of information such
2374 * as a preview of the contents of a text file.
2375 * See gtk_icon_info_set_raw_coordinates() for further
2376 * information about the coordinate system.
2378 * Return value: %TRUE if the icon has an embedded rectangle
2383 gtk_icon_info_get_embedded_rect (GtkIconInfo *icon_info,
2384 GdkRectangle *rectangle)
2386 g_return_val_if_fail (icon_info != NULL, FALSE);
2388 if (icon_info->data && icon_info->data->has_embedded_rect &&
2389 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2391 gint scaled_x0, scaled_y0;
2392 gint scaled_x1, scaled_y1;
2396 icon_info_scale_point (icon_info,
2397 icon_info->data->x0, icon_info->data->y0,
2398 &scaled_x0, &scaled_y0);
2399 icon_info_scale_point (icon_info,
2400 icon_info->data->x1, icon_info->data->y1,
2401 &scaled_x1, &scaled_y1);
2403 rectangle->x = scaled_x0;
2404 rectangle->y = scaled_y0;
2405 rectangle->width = scaled_x1 - rectangle->x;
2406 rectangle->height = scaled_y1 - rectangle->y;
2416 * gtk_icon_info_get_attach_points:
2417 * @icon_info: a #GtkIconInfo
2418 * @points: location to store pointer to an array of points, or %NULL
2419 * free the array of points with g_free().
2420 * @n_points: location to store the number of points in @points, or %NULL
2422 * Fetches the set of attach points for an icon. An attach point
2423 * is a location in the icon that can be used as anchor points for attaching
2424 * emblems or overlays to the icon.
2426 * Return value: %TRUE if there are any attach points for the icon.
2431 gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
2435 g_return_val_if_fail (icon_info != NULL, FALSE);
2437 if (icon_info->data && icon_info->data->n_attach_points &&
2438 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2444 *points = g_new (GdkPoint, icon_info->data->n_attach_points);
2445 for (i = 0; i < icon_info->data->n_attach_points; i++)
2446 icon_info_scale_point (icon_info,
2447 icon_info->data->attach_points[i].x,
2448 icon_info->data->attach_points[i].y,
2454 *n_points = icon_info->data->n_attach_points;
2470 * gtk_icon_info_get_display_name:
2471 * @icon_info: a #GtkIconInfo
2473 * Gets the display name for an icon. A display name is a
2474 * string to be used in place of the icon name in a user
2475 * visible context like a list of icons.
2477 * Return value: the display name for the icon or %NULL, if
2478 * the icon doesn't have a specified display name. This value
2479 * is owned @icon_info and must not be modified or free.
2483 G_CONST_RETURN gchar *
2484 gtk_icon_info_get_display_name (GtkIconInfo *icon_info)
2486 g_return_val_if_fail (icon_info != NULL, NULL);
2488 if (icon_info->data)
2489 return icon_info->data->display_name;
2500 * gtk_icon_theme_add_builtin_icon:
2501 * @icon_name: the name of the icon to register
2502 * @size: the size at which to register the icon (different
2503 * images can be registered for the same icon name
2504 * at different sizes.)
2505 * @pixbuf: #GdkPixbuf that contains the image to use
2508 * Registers a built-in icon for icon theme lookups. The idea
2509 * of built-in icons is to allow an application or library
2510 * that uses themed icons to function requiring files to
2511 * be present in the file system. For instance, the default
2512 * images for all of GTK+'s stock icons are registered
2515 * In general, if you use gtk_icon_theme_add_builtin_icon()
2516 * you should also install the icon in the icon theme, so
2517 * that the icon is generally available.
2519 * This function will generally be used with pixbufs loaded
2520 * via gdk_pixbuf_new_from_inline ().
2525 gtk_icon_theme_add_builtin_icon (const gchar *icon_name,
2529 BuiltinIcon *default_icon;
2533 g_return_if_fail (icon_name != NULL);
2534 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2536 if (!icon_theme_builtin_icons)
2537 icon_theme_builtin_icons = g_hash_table_new (g_str_hash, g_str_equal);
2539 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
2541 key = g_strdup (icon_name);
2543 key = (gpointer)icon_name; /* Won't get stored */
2545 default_icon = g_new (BuiltinIcon, 1);
2546 default_icon->size = size;
2547 default_icon->pixbuf = g_object_ref (pixbuf);
2548 icons = g_slist_prepend (icons, default_icon);
2550 /* Replaces value, leaves key untouched
2552 g_hash_table_insert (icon_theme_builtin_icons, key, icons);
2555 /* Look up a builtin icon; the min_difference_p and
2556 * has_larger_p out parameters allow us to combine
2557 * this lookup with searching through the actual directories
2558 * of the "hicolor" icon theme. See theme_lookup_icon()
2559 * for how they are used.
2561 static BuiltinIcon *
2562 find_builtin_icon (const gchar *icon_name,
2564 gint *min_difference_p,
2565 gboolean *has_larger_p)
2567 GSList *icons = NULL;
2568 gint min_difference = G_MAXINT;
2569 gboolean has_larger = FALSE;
2570 BuiltinIcon *min_icon = NULL;
2572 if (!icon_theme_builtin_icons)
2575 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
2579 BuiltinIcon *default_icon = icons->data;
2580 int min, max, difference;
2583 min = default_icon->size - 2;
2584 max = default_icon->size + 2;
2585 smaller = size < min;
2587 difference = min - size;
2588 else if (size > max)
2589 difference = size - max;
2593 if (difference == 0)
2595 min_icon = default_icon;
2601 if (difference < min_difference || smaller)
2603 min_difference = difference;
2604 min_icon = default_icon;
2605 has_larger = smaller;
2610 if (difference < min_difference && smaller)
2612 min_difference = difference;
2613 min_icon = default_icon;
2617 icons = icons->next;
2620 if (min_difference_p)
2621 *min_difference_p = min_difference;
2623 *has_larger_p = has_larger;