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.
21 #include <sys/types.h>
28 #include "gtkicontheme.h"
29 #include "gtkiconthemeparser.h"
31 #include "gtksettings.h"
33 #define DEFAULT_THEME_NAME "hicolor"
35 typedef struct _GtkIconData GtkIconData;
40 ICON_THEME_DIR_SCALABLE,
41 ICON_THEME_DIR_THRESHOLD,
42 ICON_THEME_DIR_UNTHEMED
45 /* In reverse search order: */
49 ICON_SUFFIX_XPM = 1 << 0,
50 ICON_SUFFIX_SVG = 1 << 1,
51 ICON_SUFFIX_PNG = 1 << 2,
55 struct _GtkIconThemePrivate
57 guint custom_theme : 1;
58 guint is_screen_singleton : 1;
59 guint pixbuf_supports_svg : 1;
65 gboolean themes_valid;
66 /* A list of all the themes needed to look up icons.
67 * In search order, without duplicates
70 GHashTable *unthemed_icons;
72 /* Note: The keys of this hashtable are owned by the
73 * themedir and unthemed hashtables.
75 GHashTable *all_icons;
77 /* GdkScreen for the icon theme (may be NULL)
81 /* time when we last stat:ed for theme changes */
90 /* Information about the source
93 GdkPixbuf *builtin_pixbuf;
97 /* Information about the directory where
98 * the source was found
100 IconThemeDirType dir_type;
104 /* Parameters influencing the scaled icon
107 gboolean raw_coordinates;
109 /* Cached information if we go ahead and try to load
124 /* In search order */
130 gboolean has_embedded_rect;
133 GdkPoint *attach_points;
134 gint n_attach_points;
141 IconThemeDirType type;
152 GHashTable *icon_data;
158 char *no_svg_filename;
170 time_t mtime; /* 0 == not existing or not a dir */
173 static void gtk_icon_theme_class_init (GtkIconThemeClass *klass);
174 static void gtk_icon_theme_init (GtkIconTheme *icon_theme);
175 static void gtk_icon_theme_finalize (GObject *object);
176 static void theme_dir_destroy (IconThemeDir *dir);
178 static void theme_destroy (IconTheme *theme);
179 static GtkIconInfo *theme_lookup_icon (IconTheme *theme,
180 const char *icon_name,
183 gboolean use_default_icons);
184 static void theme_list_icons (IconTheme *theme,
187 static void theme_subdir_load (GtkIconTheme *icon_theme,
189 GtkIconThemeFile *theme_file,
191 static void do_theme_change (GtkIconTheme *icon_theme);
193 static void blow_themes (GtkIconTheme *icon_themes);
195 static void icon_data_free (GtkIconData *icon_data);
197 static GtkIconInfo *icon_info_new (void);
198 static GtkIconInfo *icon_info_new_builtin (BuiltinIcon *icon);
200 static IconSuffix suffix_from_name (const char *name);
202 static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
204 gint *min_difference_p,
205 gboolean *has_larger_p);
207 static guint signal_changed = 0;
209 static GHashTable *icon_theme_builtin_icons;
212 gtk_icon_theme_get_type (void)
214 static GType type = 0;
218 static const GTypeInfo info =
220 sizeof (GtkIconThemeClass),
221 NULL, /* base_init */
222 NULL, /* base_finalize */
223 (GClassInitFunc) gtk_icon_theme_class_init,
224 NULL, /* class_finalize */
225 NULL, /* class_data */
226 sizeof (GtkIconTheme),
228 (GInstanceInitFunc) gtk_icon_theme_init,
231 type = g_type_register_static (G_TYPE_OBJECT, "GtkIconTheme", &info, 0);
238 * gtk_icon_theme_new:
240 * Creates a new icon theme object. Icon theme objects are used
241 * to lookup up an icon by name in a particular icon theme.
242 * Usually, you'll want to use gtk_icon_theme_get_default()
243 * or gtk_icon_theme_get_for_screen() rather than creating
244 * a new icon theme object for scratch.
246 * Return value: the newly created #GtkIconTheme object.
249 gtk_icon_theme_new (void)
251 return g_object_new (GTK_TYPE_ICON_THEME, NULL);
255 * gtk_icon_theme_get_default:
257 * Gets the icon theme for the default screen. See
258 * gtk_icon_theme_get_for_screen().
260 * Return value: A unique #GtkIconTheme associated with
261 * the default screen. This icon theme is associated with
262 * the screen and can be used as long as the screen
266 gtk_icon_theme_get_default (void)
268 return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
272 * gtk_icon_theme_get_for_screen:
273 * @screen: a #GdkScreen
275 * Gets the icon theme object associated with @screen; if this
276 * function has not previously been called for the given
277 * screen, a new icon theme object will be created and
278 * associated with the screen. Icon theme objects are
279 * fairly expensive to create, so using this function
280 * is usually a better choice than calling than gtk_icon_theme_new()
281 * and setting the screen yourself; by using this function
282 * a single icon theme object will be shared between users.
284 * Return value: A unique #GtkIconTheme associated with
285 * the given screen. This icon theme is associated with
286 * the screen and can be used as long as the screen
290 gtk_icon_theme_get_for_screen (GdkScreen *screen)
292 GtkIconTheme *icon_theme;
294 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
295 g_return_val_if_fail (!screen->closed, NULL);
297 icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
300 GtkIconThemePrivate *priv;
302 icon_theme = gtk_icon_theme_new ();
303 gtk_icon_theme_set_screen (icon_theme, screen);
305 priv = icon_theme->priv;
306 priv->is_screen_singleton = TRUE;
308 g_object_set_data (G_OBJECT (screen), "gtk-icon-theme", icon_theme);
315 gtk_icon_theme_class_init (GtkIconThemeClass *klass)
317 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
319 gobject_class->finalize = gtk_icon_theme_finalize;
322 * GtkIconTheme::changed
323 * @icon_theme: the icon theme
325 * Emitted when the current icon theme is switched or GTK+ detects
326 * that a change has occurred in the contents of the current
329 signal_changed = g_signal_new ("changed",
330 G_TYPE_FROM_CLASS (klass),
332 G_STRUCT_OFFSET (GtkIconThemeClass, changed),
334 g_cclosure_marshal_VOID__VOID,
337 g_type_class_add_private (klass, sizeof (GtkIconThemePrivate));
341 /* Callback when the display that the icon theme is attached to
342 * is closed; unset the screen, and if it's the unique theme
343 * for the screen, drop the reference
346 display_closed (GdkDisplay *display,
348 GtkIconTheme *icon_theme)
350 GtkIconThemePrivate *priv = icon_theme->priv;
351 GdkScreen *screen = priv->screen;
352 gboolean was_screen_singleton = priv->is_screen_singleton;
354 if (was_screen_singleton)
356 g_object_set_data (G_OBJECT (screen), "gtk-icon-theme", NULL);
357 priv->is_screen_singleton = FALSE;
360 gtk_icon_theme_set_screen (icon_theme, NULL);
362 if (was_screen_singleton)
364 g_object_unref (icon_theme);
369 update_current_theme (GtkIconTheme *icon_theme)
371 GtkIconThemePrivate *priv = icon_theme->priv;
373 if (!priv->custom_theme)
379 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
380 g_object_get (settings, "gtk-icon-theme-name", &theme, NULL);
384 theme = g_strdup (DEFAULT_THEME_NAME);
386 if (strcmp (priv->current_theme, theme) != 0)
388 g_free (priv->current_theme);
389 priv->current_theme = theme;
391 do_theme_change (icon_theme);
398 /* Callback when the icon theme GtkSetting changes
401 theme_changed (GtkSettings *settings,
403 GtkIconTheme *icon_theme)
405 update_current_theme (icon_theme);
409 unset_screen (GtkIconTheme *icon_theme)
411 GtkIconThemePrivate *priv = icon_theme->priv;
412 GtkSettings *settings;
417 settings = gtk_settings_get_for_screen (priv->screen);
418 display = gdk_screen_get_display (priv->screen);
420 g_signal_handlers_disconnect_by_func (display,
421 (gpointer) display_closed,
423 g_signal_handlers_disconnect_by_func (settings,
424 (gpointer) theme_changed,
432 * gtk_icon_theme_set_screen:
433 * @icon_theme: a #GtkIconTheme
434 * @screen: a #GdkScreen
436 * Sets the screen for an icon theme; the screen is used
437 * to track the user's currently configured icon theme,
438 * which might be different for different screens.
441 gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
444 GtkIconThemePrivate *priv;
445 GtkSettings *settings;
448 g_return_if_fail (GTK_ICON_THEME (icon_theme));
449 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
451 priv = icon_theme->priv;
453 unset_screen (icon_theme);
457 display = gdk_screen_get_display (screen);
458 settings = gtk_settings_get_for_screen (screen);
460 priv->screen = screen;
462 g_signal_connect (display, "closed",
463 G_CALLBACK (display_closed), icon_theme);
464 g_signal_connect (settings, "notify::gtk-icon-theme-name",
465 G_CALLBACK (theme_changed), icon_theme);
468 update_current_theme (icon_theme);
471 /* Checks whether a loader for SVG files has been registered
475 pixbuf_supports_svg ()
477 GSList *formats = gdk_pixbuf_get_formats ();
479 gboolean found_svg = FALSE;
481 for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
483 gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
486 for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
488 if (strcmp (*mime_type, "image/svg") == 0)
495 g_slist_free (formats);
501 gtk_icon_theme_init (GtkIconTheme *icon_theme)
503 GtkIconThemePrivate *priv;
505 priv = g_type_instance_get_private ((GTypeInstance *)icon_theme,
506 GTK_TYPE_ICON_THEME);
507 icon_theme->priv = priv;
509 priv->custom_theme = FALSE;
510 priv->current_theme = g_strdup (DEFAULT_THEME_NAME);
511 priv->search_path = g_new (char *, 5);
514 priv->search_path[0] = g_build_filename (g_get_home_dir (),
517 priv->search_path[1] = g_strdup (GTK_DATADIR "/pixmaps");
518 priv->search_path[2] = g_strdup (GTK_DATADIR "/icons");
519 priv->search_path[3] = g_strdup ("/usr/share/icons");
520 priv->search_path[4] = g_strdup ("/usr/share/pixmaps");
521 priv->search_path_len = 5;
523 priv->themes_valid = FALSE;
525 priv->unthemed_icons = NULL;
527 priv->pixbuf_supports_svg = pixbuf_supports_svg ();
531 free_dir_mtime (IconThemeDirMtime *dir_mtime)
533 g_free (dir_mtime->dir);
538 do_theme_change (GtkIconTheme *icon_theme)
540 GtkIconThemePrivate *priv = icon_theme->priv;
542 blow_themes (icon_theme);
543 g_signal_emit (G_OBJECT (icon_theme), signal_changed, 0);
545 if (priv->screen && priv->is_screen_singleton)
547 GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
548 _gtk_rc_reset_styles (settings);
553 blow_themes (GtkIconTheme *icon_theme)
555 GtkIconThemePrivate *priv = icon_theme->priv;
557 if (priv->themes_valid)
559 g_hash_table_destroy (priv->all_icons);
560 g_list_foreach (priv->themes, (GFunc)theme_destroy, NULL);
561 g_list_free (priv->themes);
562 g_list_foreach (priv->dir_mtimes, (GFunc)free_dir_mtime, NULL);
563 g_list_free (priv->dir_mtimes);
564 g_hash_table_destroy (priv->unthemed_icons);
567 priv->unthemed_icons = NULL;
568 priv->dir_mtimes = NULL;
569 priv->all_icons = NULL;
570 priv->themes_valid = FALSE;
574 gtk_icon_theme_finalize (GObject *object)
576 GtkIconTheme *icon_theme;
577 GtkIconThemePrivate *priv;
580 icon_theme = GTK_ICON_THEME (object);
581 priv = icon_theme->priv;
583 unset_screen (icon_theme);
585 g_free (priv->current_theme);
586 priv->current_theme = NULL;
588 for (i=0; i < priv->search_path_len; i++)
589 g_free (priv->search_path[i]);
591 g_free (priv->search_path);
592 priv->search_path = NULL;
594 blow_themes (icon_theme);
598 * gtk_icon_theme_set_search_path:
599 * @icon_theme: a #GtkIconTheme
600 * @path: array of directories that are searched for icon themes
601 * @n_elements: number of elements in @path.
603 * Sets the search path for the icon theme object. When looking
604 * for an icon theme, GTK+ will search for a subdirectory of
605 * one or more of the directories in @path with the same name
606 * as the icon theme. (Themes from multiple of the path elements
607 * are combined to allow themes to be extended by adding icons
608 * in the user's home directory.)
610 * In addition if an icon found isn't found either in the current
611 * icon theme or the default icon theme, and an image file with
612 * the right name is found directly in one of the elements of
613 * @path, then that image will be used for the icon name.
614 * (This is legacy feature, and new icons should be put
615 * into the default icon theme, which is called "hicolor", rather than
616 * directly on the icon path.)
619 gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
623 GtkIconThemePrivate *priv;
626 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
628 priv = icon_theme->priv;
629 for (i = 0; i < priv->search_path_len; i++)
630 g_free (priv->search_path[i]);
632 g_free (priv->search_path);
634 priv->search_path = g_new (gchar *, n_elements);
635 priv->search_path_len = n_elements;
636 for (i = 0; i < priv->search_path_len; i++)
637 priv->search_path[i] = g_strdup (path[i]);
639 do_theme_change (icon_theme);
644 * gtk_icon_theme_get_search_path:
645 * @icon_theme: a #GtkIconTheme
646 * @path: location to store a list of icon theme path directories or %NULL
647 * The stored value should be freed with g_strfreev().
648 * @n_elements: location to store number of elements
651 * Gets the current search path. See gtk_icon_theme_set_search_path().
654 gtk_icon_theme_get_search_path (GtkIconTheme *icon_theme,
658 GtkIconThemePrivate *priv;
661 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
663 priv = icon_theme->priv;
666 *n_elements = priv->search_path_len;
670 *path = g_new (gchar *, priv->search_path_len + 1);
671 for (i = 0; i < priv->search_path_len; i++)
672 (*path)[i] = g_strdup (priv->search_path[i] + 1);
678 * gtk_icon_theme_append_search_path:
679 * @icon_theme: a #GtkIconTheme
680 * @path: directory name to append to the icon path
682 * Appends a directory to the search path. See gtk_icon_theme_set_search_path().
685 gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
688 GtkIconThemePrivate *priv;
690 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
691 g_return_if_fail (path != NULL);
693 priv = icon_theme->priv;
695 priv->search_path_len++;
696 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
697 priv->search_path[priv->search_path_len-1] = g_strdup (path);
699 do_theme_change (icon_theme);
703 * gtk_icon_theme_prepend_search_path:
704 * @icon_theme: a #GtkIconTheme
705 * @path: directory name to prepend to the icon path
707 * Prepends a directory to the search path. See gtk_icon_theme_set_search_path().
710 gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
713 GtkIconThemePrivate *priv;
716 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
717 g_return_if_fail (path != NULL);
719 priv = icon_theme->priv;
721 priv->search_path_len++;
722 priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
724 for (i = 0; i < priv->search_path_len - 1; i++)
725 priv->search_path[i+1] = priv->search_path[i];
727 priv->search_path[0] = g_strdup (path);
729 do_theme_change (icon_theme);
733 * gtk_icon_theme_set_custom_theme:
734 * @icon_theme: a #GtkIconTheme
735 * @theme_name: name of icon theme to use instead of configured theme
737 * Sets the name of the icon theme that the #GtkIconTheme object uses
738 * overriding system configuration. This function cannot be called
739 * on the icon theme objects returned from gtk_icon_theme_get_default()
740 * and gtk_icon_theme_get_default().
743 gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
744 const gchar *theme_name)
746 GtkIconThemePrivate *priv;
748 g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
750 priv = icon_theme->priv;
752 g_return_if_fail (!priv->is_screen_singleton);
754 if (theme_name != NULL)
756 priv->custom_theme = TRUE;
757 if (strcmp (theme_name, priv->current_theme) != 0)
759 g_free (priv->current_theme);
760 priv->current_theme = g_strdup (theme_name);
762 do_theme_change (icon_theme);
767 priv->custom_theme = FALSE;
769 update_current_theme (icon_theme);
774 insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
780 GtkIconThemePrivate *priv;
786 GtkIconThemeFile *theme_file;
787 IconThemeDirMtime *dir_mtime;
788 struct stat stat_buf;
790 priv = icon_theme->priv;
792 for (l = priv->themes; l != NULL; l = l->next)
795 if (strcmp (theme->name, theme_name) == 0)
799 for (i = 0; i < priv->search_path_len; i++)
801 path = g_build_filename (priv->search_path[i],
804 dir_mtime = g_new (IconThemeDirMtime, 1);
805 dir_mtime->dir = path;
806 if (stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode))
807 dir_mtime->mtime = stat_buf.st_mtime;
809 dir_mtime->mtime = 0;
811 priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
815 for (i = 0; i < priv->search_path_len; i++)
817 path = g_build_filename (priv->search_path[i],
821 if (g_file_test (path, G_FILE_TEST_IS_REGULAR)) {
822 if (g_file_get_contents (path, &contents, NULL, NULL)) {
823 theme_file = _gtk_icon_theme_file_new_from_string (contents, NULL);
832 if (theme_file == NULL)
835 theme = g_new (IconTheme, 1);
836 if (!_gtk_icon_theme_file_get_locale_string (theme_file,
839 &theme->display_name))
841 g_warning ("Theme file for %s has no name\n", theme_name);
843 _gtk_icon_theme_file_free (theme_file);
847 if (!_gtk_icon_theme_file_get_string (theme_file,
852 g_warning ("Theme file for %s has no directories\n", theme_name);
853 g_free (theme->display_name);
855 _gtk_icon_theme_file_free (theme_file);
859 theme->name = g_strdup (theme_name);
860 _gtk_icon_theme_file_get_locale_string (theme_file,
864 _gtk_icon_theme_file_get_string (theme_file,
869 dirs = g_strsplit (directories, ",", 0);
872 for (i = 0; dirs[i] != NULL; i++)
873 theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);
877 theme->dirs = g_list_reverse (theme->dirs);
879 g_free (directories);
881 /* Prepend the finished theme */
882 priv->themes = g_list_prepend (priv->themes, theme);
884 if (_gtk_icon_theme_file_get_string (theme_file,
889 themes = g_strsplit (inherits, ",", 0);
891 for (i = 0; themes[i] != NULL; i++)
892 insert_theme (icon_theme, themes[i]);
899 _gtk_icon_theme_file_free (theme_file);
903 free_unthemed_icon (UnthemedIcon *unthemed_icon)
905 if (unthemed_icon->svg_filename)
906 g_free (unthemed_icon->svg_filename);
907 if (unthemed_icon->svg_filename)
908 g_free (unthemed_icon->no_svg_filename);
909 g_free (unthemed_icon);
913 load_themes (GtkIconTheme *icon_theme)
915 GtkIconThemePrivate *priv;
918 char *dir, *base_name, *dot;
921 UnthemedIcon *unthemed_icon;
922 IconSuffix old_suffix, new_suffix;
925 priv = icon_theme->priv;
927 priv->all_icons = g_hash_table_new (g_str_hash, g_str_equal);
929 insert_theme (icon_theme, priv->current_theme);
931 /* Always look in the "default" icon theme */
932 insert_theme (icon_theme, DEFAULT_THEME_NAME);
933 priv->themes = g_list_reverse (priv->themes);
935 priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
936 g_free, (GDestroyNotify)free_unthemed_icon);
938 for (base = 0; base < icon_theme->priv->search_path_len; base++)
940 dir = icon_theme->priv->search_path[base];
941 gdir = g_dir_open (dir, 0, NULL);
946 while ((file = g_dir_read_name (gdir)))
948 new_suffix = suffix_from_name (file);
950 if (new_suffix != ICON_SUFFIX_NONE)
952 abs_file = g_build_filename (dir, file, NULL);
954 base_name = g_strdup (file);
956 dot = strrchr (base_name, '.');
960 if ((unthemed_icon = g_hash_table_lookup (priv->unthemed_icons,
963 if (new_suffix == ICON_SUFFIX_SVG)
965 if (unthemed_icon->no_svg_filename)
968 unthemed_icon->svg_filename = abs_file;
972 if (unthemed_icon->no_svg_filename)
974 old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
975 if (new_suffix > old_suffix)
977 g_free (unthemed_icon->no_svg_filename);
978 unthemed_icon->no_svg_filename = abs_file;
984 unthemed_icon->no_svg_filename = abs_file;
991 unthemed_icon = g_new0 (UnthemedIcon, 1);
993 if (new_suffix == ICON_SUFFIX_SVG)
994 unthemed_icon->svg_filename = abs_file;
996 unthemed_icon->svg_filename = abs_file;
998 g_hash_table_insert (priv->unthemed_icons,
1001 g_hash_table_insert (priv->all_icons,
1009 priv->themes_valid = TRUE;
1011 gettimeofday(&tv, NULL);
1012 priv->last_stat_time = tv.tv_sec;
1016 ensure_valid_themes (GtkIconTheme *icon_theme)
1018 GtkIconThemePrivate *priv = icon_theme->priv;
1021 if (priv->themes_valid)
1023 gettimeofday(&tv, NULL);
1025 if (ABS (tv.tv_sec - priv->last_stat_time) > 5)
1026 gtk_icon_theme_rescan_if_needed (icon_theme);
1029 if (!priv->themes_valid)
1030 load_themes (icon_theme);
1034 * gtk_icon_theme_lookup_icon:
1035 * @icon_theme: a #GtkIconTheme
1036 * @icon_name: the name of the icon to lookup
1037 * @size: desired icon size
1038 * @flags: flags modifying the behavior of the icon lookup
1040 * Looks up a named icon and returns a structure containing
1041 * information such as the filename of the icon. The icon
1042 * can then be rendered into a pixbuf using
1043 * gtk_icon_info_load_icon(). (gtk_icon_theme_load_icon()
1044 * combines these two steps if all you need is the pixbuf.)
1046 * Return value: a #GtkIconInfo structure containing information
1047 * about the icon, or %NULL if the icon wasn't found. Free with
1048 * gtk_icon_info_free()
1051 gtk_icon_theme_lookup_icon (GtkIconTheme *icon_theme,
1052 const gchar *icon_name,
1054 GtkIconLookupFlags flags)
1056 GtkIconThemePrivate *priv;
1058 GtkIconInfo *icon_info = NULL;
1059 UnthemedIcon *unthemed_icon;
1061 gboolean use_builtin;
1062 gboolean found_default;
1064 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1065 g_return_val_if_fail (icon_name != NULL, NULL);
1066 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1067 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1069 priv = icon_theme->priv;
1071 if (flags & GTK_ICON_LOOKUP_NO_SVG)
1073 else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
1076 allow_svg = priv->pixbuf_supports_svg;
1078 use_builtin = (flags & GTK_ICON_LOOKUP_USE_BUILTIN);
1080 ensure_valid_themes (icon_theme);
1082 found_default = FALSE;
1086 IconTheme *icon_theme = l->data;
1088 if (strcmp (icon_theme->name, DEFAULT_THEME_NAME) == 0)
1089 found_default = TRUE;
1091 icon_info = theme_lookup_icon (icon_theme, icon_name, size, allow_svg, use_builtin);
1100 BuiltinIcon *builtin = find_builtin_icon (icon_name, size, NULL, NULL);
1103 icon_info = icon_info_new_builtin (builtin);
1108 unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, icon_name);
1111 icon_info = icon_info_new ();
1113 /* A SVG icon, when allowed, beats out a XPM icon, but not
1117 unthemed_icon->svg_filename &&
1118 (!unthemed_icon->no_svg_filename ||
1119 suffix_from_name (unthemed_icon->no_svg_filename) != ICON_SUFFIX_PNG))
1120 icon_info->filename = g_strdup (unthemed_icon->svg_filename);
1121 else if (unthemed_icon->no_svg_filename)
1122 icon_info->filename = g_strdup (unthemed_icon->no_svg_filename);
1124 icon_info->dir_type = ICON_THEME_DIR_UNTHEMED;
1129 icon_info->desired_size = size;
1136 gtk_icon_theme_error_quark (void)
1138 static GQuark q = 0;
1140 q = g_quark_from_static_string ("gtk-icon-theme-error-quark");
1146 * gtk_icon_theme_load_icon:
1147 * @icon_theme: a #GtkIconTheme
1148 * @icon_name: the name of the icon to lookup
1149 * @size: the desired icon size. The resulting icon may not be
1150 * exactly this size; see gtk_icon_info_load_icon().
1151 * @flags: flags modifying the behavior of the icon lookup
1152 * @error: Location to store error information on failure, or %NULL.
1154 * Looks up an icon in an icon theme, scales it to the given size
1155 * and renders it into a pixbuf. This is a convenience function;
1156 * if more details about the icon are needed, use
1157 * gtk_icon_theme_lookup_icon() followed by gtk_icon_info_load_icon().
1159 * Return value: the rendered icon; this may be a newly created icon
1160 * or a new reference to an internal icon, so you must not modify
1161 * the icon. Use g_object_unref() to release your reference to the
1162 * icon. %NULL if the icon isn't found.
1165 gtk_icon_theme_load_icon (GtkIconTheme *icon_theme,
1166 const gchar *icon_name,
1168 GtkIconLookupFlags flags,
1171 GtkIconInfo *icon_info;
1172 GdkPixbuf *pixbuf = NULL;
1174 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1175 g_return_val_if_fail (icon_name != NULL, NULL);
1176 g_return_val_if_fail ((flags & GTK_ICON_LOOKUP_NO_SVG) == 0 ||
1177 (flags & GTK_ICON_LOOKUP_FORCE_SVG) == 0, NULL);
1178 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1180 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size,
1181 flags | GTK_ICON_LOOKUP_USE_BUILTIN);
1184 g_set_error (error, GTK_ICON_THEME_ERROR, GTK_ICON_THEME_NOT_FOUND,
1185 _("Icon '%s' not present in theme"), icon_name);
1189 pixbuf = gtk_icon_info_load_icon (icon_info, error);
1190 gtk_icon_info_free (icon_info);
1196 * gtk_icon_theme_has_icon:
1197 * @icon_theme: a #GtkIconTheme
1198 * @icon_name: the name of an icon
1200 * Checks whether an icon theme includes an icon
1201 * for a particular name.
1203 * Return value: %TRUE if @icon_theme includes an
1204 * icon for @icon_name.
1207 gtk_icon_theme_has_icon (GtkIconTheme *icon_theme,
1208 const char *icon_name)
1210 GtkIconThemePrivate *priv;
1212 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1214 priv = icon_theme->priv;
1216 ensure_valid_themes (icon_theme);
1218 if (g_hash_table_lookup_extended (priv->all_icons,
1219 icon_name, NULL, NULL))
1221 if (g_hash_table_lookup_extended (icon_theme_builtin_icons,
1222 icon_name, NULL, NULL))
1230 add_key_to_hash (gpointer key,
1234 GHashTable *hash = user_data;
1236 g_hash_table_insert (hash, key, NULL);
1240 add_key_to_list (gpointer key,
1244 GList **list = user_data;
1246 *list = g_list_prepend (*list, g_strdup (key));
1250 * gtk_icon_theme_list_icons:
1251 * @icon_theme: a #GtkIconTheme
1252 * @context: a string identifying a particular type of icon,
1253 * or %NULL to list all icons.
1255 * Lists the icons in the current icon theme. Only a subset
1256 * of the icons can be listed by providing a context string.
1257 * The set of values for the context string is system dependent,
1258 * but will typically include such values as 'apps' and
1261 * Return value: a #GList list holding the names of all the
1262 * icons in the theme. You must first free each element
1263 * in the list with g_free(), then free the list itself
1264 * with g_list_free().
1267 gtk_icon_theme_list_icons (GtkIconTheme *icon_theme,
1268 const char *context)
1270 GtkIconThemePrivate *priv;
1273 GQuark context_quark;
1275 priv = icon_theme->priv;
1277 ensure_valid_themes (icon_theme);
1281 context_quark = g_quark_try_string (context);
1289 icons = g_hash_table_new (g_str_hash, g_str_equal);
1294 theme_list_icons (l->data, icons, context_quark);
1298 if (context_quark == 0)
1299 g_hash_table_foreach (priv->unthemed_icons,
1305 g_hash_table_foreach (icons,
1309 g_hash_table_destroy (icons);
1315 * gtk_icon_theme_get_example_icon_name:
1316 * @icon_theme: a #GtkIconTheme
1318 * Gets the name of an icon that is representative of the
1319 * current theme (for instance, to use when presenting
1320 * a list of themes to the user.)
1322 * Return value: the name of an example icon or %NULL.
1323 * Free with g_free().
1326 gtk_icon_theme_get_example_icon_name (GtkIconTheme *icon_theme)
1328 GtkIconThemePrivate *priv;
1332 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), NULL);
1334 priv = icon_theme->priv;
1336 ensure_valid_themes (icon_theme);
1343 return g_strdup (theme->example);
1352 * gtk_icon_theme_rescan_if_needed:
1353 * @icon_theme: a #GtkIconTheme
1355 * Checks to see if the icon theme has changed; if it has, any
1356 * currently cached information is discarded and will be reloaded
1357 * next time @icon_theme is accessed.
1359 * Return value: %TRUE if the icon theme has changed and needed
1363 gtk_icon_theme_rescan_if_needed (GtkIconTheme *icon_theme)
1365 GtkIconThemePrivate *priv;
1366 IconThemeDirMtime *dir_mtime;
1369 struct stat stat_buf;
1372 g_return_val_if_fail (GTK_IS_ICON_THEME (icon_theme), FALSE);
1374 priv = icon_theme->priv;
1376 for (d = priv->dir_mtimes; d != NULL; d = d->next)
1378 dir_mtime = d->data;
1380 stat_res = stat (dir_mtime->dir, &stat_buf);
1382 /* dir mtime didn't change */
1383 if (stat_res == 0 &&
1384 S_ISDIR (stat_buf.st_mode) &&
1385 dir_mtime->mtime == stat_buf.st_mtime)
1387 /* didn't exist before, and still doesn't */
1388 if (dir_mtime->mtime == 0 &&
1389 (stat_res != 0 || !S_ISDIR (stat_buf.st_mode)))
1392 do_theme_change (icon_theme);
1396 gettimeofday (&tv, NULL);
1397 priv->last_stat_time = tv.tv_sec;
1403 theme_destroy (IconTheme *theme)
1405 g_free (theme->display_name);
1406 g_free (theme->comment);
1407 g_free (theme->name);
1408 g_free (theme->example);
1410 g_list_foreach (theme->dirs, (GFunc)theme_dir_destroy, NULL);
1411 g_list_free (theme->dirs);
1416 theme_dir_destroy (IconThemeDir *dir)
1418 g_hash_table_destroy (dir->icons);
1420 g_hash_table_destroy (dir->icon_data);
1426 theme_dir_size_difference (IconThemeDir *dir, int size, gboolean *smaller)
1431 case ICON_THEME_DIR_FIXED:
1432 *smaller = size < dir->size;
1433 return abs (size - dir->size);
1435 case ICON_THEME_DIR_SCALABLE:
1436 *smaller = size < dir->min_size;
1437 if (size < dir->min_size)
1438 return dir->min_size - size;
1439 if (size > dir->max_size)
1440 return size - dir->max_size;
1443 case ICON_THEME_DIR_THRESHOLD:
1444 min = dir->size - dir->threshold;
1445 max = dir->size + dir->threshold;
1446 *smaller = size < min;
1453 case ICON_THEME_DIR_UNTHEMED:
1454 g_assert_not_reached ();
1457 g_assert_not_reached ();
1462 string_from_suffix (IconSuffix suffix)
1466 case ICON_SUFFIX_XPM:
1468 case ICON_SUFFIX_SVG:
1470 case ICON_SUFFIX_PNG:
1473 g_assert_not_reached();
1479 suffix_from_name (const char *name)
1483 if (g_str_has_suffix (name, ".png"))
1484 retval = ICON_SUFFIX_PNG;
1485 else if (g_str_has_suffix (name, ".svg"))
1486 retval = ICON_SUFFIX_SVG;
1487 else if (g_str_has_suffix (name, ".xpm"))
1488 retval = ICON_SUFFIX_XPM;
1490 retval = ICON_SUFFIX_NONE;
1496 best_suffix (IconSuffix suffix,
1499 if ((suffix & ICON_SUFFIX_PNG) != 0)
1500 return ICON_SUFFIX_PNG;
1501 else if (allow_svg && ((suffix & ICON_SUFFIX_SVG) != 0))
1502 return ICON_SUFFIX_SVG;
1503 else if ((suffix & ICON_SUFFIX_XPM) != 0)
1504 return ICON_SUFFIX_XPM;
1506 return ICON_SUFFIX_NONE;
1509 static GtkIconInfo *
1510 theme_lookup_icon (IconTheme *theme,
1511 const char *icon_name,
1514 gboolean use_builtin)
1517 IconThemeDir *dir, *min_dir;
1519 int min_difference, difference;
1520 BuiltinIcon *closest_builtin = NULL;
1521 gboolean smaller, has_larger;
1524 min_difference = G_MAXINT;
1528 /* Builtin icons are logically part of the default theme and
1529 * are searched before other subdirectories of the default theme.
1531 if (strcmp (theme->name, DEFAULT_THEME_NAME) == 0 && use_builtin)
1533 closest_builtin = find_builtin_icon (icon_name, size,
1537 if (min_difference == 0)
1538 return icon_info_new_builtin (closest_builtin);
1546 suffix = GPOINTER_TO_UINT (g_hash_table_lookup (dir->icons, icon_name));
1548 if (suffix != ICON_SUFFIX_NONE &&
1549 (allow_svg || suffix != ICON_SUFFIX_SVG))
1551 difference = theme_dir_size_difference (dir, size, &smaller);
1553 if (difference == 0)
1561 if (difference < min_difference || smaller)
1563 min_difference = difference;
1565 closest_builtin = NULL;
1566 has_larger = smaller;
1571 if (difference < min_difference && smaller)
1573 min_difference = difference;
1575 closest_builtin = NULL;
1584 if (closest_builtin)
1585 return icon_info_new_builtin (closest_builtin);
1589 GtkIconInfo *icon_info = icon_info_new ();
1591 suffix = GPOINTER_TO_UINT (g_hash_table_lookup (min_dir->icons, icon_name));
1592 suffix = best_suffix (suffix, allow_svg);
1593 g_assert (suffix != ICON_SUFFIX_NONE);
1595 file = g_strconcat (icon_name, string_from_suffix (suffix), NULL);
1596 icon_info->filename = g_build_filename (min_dir->dir, file, NULL);
1599 if (min_dir->icon_data != NULL)
1600 icon_info->data = g_hash_table_lookup (min_dir->icon_data, icon_name);
1602 icon_info->dir_type = min_dir->type;
1603 icon_info->dir_size = min_dir->size;
1604 icon_info->threshold = min_dir->threshold;
1613 theme_list_icons (IconTheme *theme, GHashTable *icons,
1616 GList *l = theme->dirs;
1623 if (context == dir->context ||
1625 g_hash_table_foreach (dir->icons,
1634 load_icon_data (IconThemeDir *dir, const char *path, const char *name)
1636 GtkIconThemeFile *icon_file;
1647 if (g_file_get_contents (path, &contents, NULL, NULL))
1649 icon_file = _gtk_icon_theme_file_new_from_string (contents, NULL);
1653 base_name = g_strdup (name);
1654 dot = strrchr (base_name, '.');
1657 data = g_new0 (GtkIconData, 1);
1658 g_hash_table_replace (dir->icon_data, base_name, data);
1660 if (_gtk_icon_theme_file_get_string (icon_file, "Icon Data",
1661 "EmbeddedTextRectangle",
1664 split = g_strsplit (str, ",", 4);
1667 while (split[i] != NULL)
1672 data->has_embedded_rect = TRUE;
1673 data->x0 = atoi (split[0]);
1674 data->y0 = atoi (split[1]);
1675 data->x1 = atoi (split[2]);
1676 data->y1 = atoi (split[3]);
1684 if (_gtk_icon_theme_file_get_string (icon_file, "Icon Data",
1688 split = g_strsplit (str, "|", -1);
1691 while (split[i] != NULL)
1694 data->n_attach_points = i;
1695 data->attach_points = g_malloc (sizeof (GdkPoint) * i);
1698 while (split[i] != NULL && i < data->n_attach_points)
1700 split_point = strchr (split[i], ',');
1705 data->attach_points[i].x = atoi (split[i]);
1706 data->attach_points[i].y = atoi (split_point);
1715 _gtk_icon_theme_file_get_locale_string (icon_file, "Icon Data",
1717 &data->display_name);
1719 _gtk_icon_theme_file_free (icon_file);
1727 scan_directory (GtkIconThemePrivate *icon_theme,
1728 IconThemeDir *dir, char *full_dir)
1732 char *base_name, *dot;
1734 IconSuffix suffix, hash_suffix;
1736 dir->icons = g_hash_table_new_full (g_str_hash, g_str_equal,
1739 gdir = g_dir_open (full_dir, 0, NULL);
1744 while ((name = g_dir_read_name (gdir)))
1746 if (g_str_has_suffix (name, ".icon"))
1748 if (dir->icon_data == NULL)
1749 dir->icon_data = g_hash_table_new_full (g_str_hash, g_str_equal,
1750 g_free, (GDestroyNotify)icon_data_free);
1752 path = g_build_filename (full_dir, name, NULL);
1753 if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
1754 load_icon_data (dir, path, name);
1761 suffix = suffix_from_name (name);
1762 if (suffix == ICON_SUFFIX_NONE)
1765 base_name = g_strdup (name);
1766 dot = strrchr (base_name, '.');
1769 hash_suffix = GPOINTER_TO_INT (g_hash_table_lookup (dir->icons, base_name));
1770 g_hash_table_replace (dir->icons, base_name, GUINT_TO_POINTER (hash_suffix| suffix));
1771 g_hash_table_insert (icon_theme->all_icons, base_name, NULL);
1778 theme_subdir_load (GtkIconTheme *icon_theme,
1780 GtkIconThemeFile *theme_file,
1786 IconThemeDirType type;
1787 char *context_string;
1795 if (!_gtk_icon_theme_file_get_integer (theme_file,
1800 g_warning ("Theme directory %s of theme %s has no size field\n", subdir, theme->name);
1804 type = ICON_THEME_DIR_THRESHOLD;
1805 if (_gtk_icon_theme_file_get_string (theme_file, subdir, "Type", &type_string))
1807 if (strcmp (type_string, "Fixed") == 0)
1808 type = ICON_THEME_DIR_FIXED;
1809 else if (strcmp (type_string, "Scalable") == 0)
1810 type = ICON_THEME_DIR_SCALABLE;
1811 else if (strcmp (type_string, "Threshold") == 0)
1812 type = ICON_THEME_DIR_THRESHOLD;
1814 g_free (type_string);
1818 if (_gtk_icon_theme_file_get_string (theme_file, subdir, "Context", &context_string))
1820 context = g_quark_from_string (context_string);
1821 g_free (context_string);
1824 if (!_gtk_icon_theme_file_get_integer (theme_file,
1830 if (!_gtk_icon_theme_file_get_integer (theme_file,
1836 if (!_gtk_icon_theme_file_get_integer (theme_file,
1842 for (base = 0; base < icon_theme->priv->search_path_len; base++)
1844 full_dir = g_build_filename (icon_theme->priv->search_path[base],
1848 if (g_file_test (full_dir, G_FILE_TEST_IS_DIR))
1850 dir = g_new (IconThemeDir, 1);
1852 dir->context = context;
1854 dir->min_size = min_size;
1855 dir->max_size = max_size;
1856 dir->threshold = threshold;
1857 dir->dir = full_dir;
1858 dir->icon_data = NULL;
1860 scan_directory (icon_theme->priv, dir, full_dir);
1862 theme->dirs = g_list_append (theme->dirs, dir);
1870 icon_data_free (GtkIconData *icon_data)
1872 g_free (icon_data->attach_points);
1873 g_free (icon_data->display_name);
1881 gtk_icon_info_get_type (void)
1883 static GType our_type = 0;
1886 our_type = g_boxed_type_register_static ("GtkIconInfo",
1887 (GBoxedCopyFunc) gtk_icon_info_copy,
1888 (GBoxedFreeFunc) gtk_icon_info_free);
1893 static GtkIconInfo *
1894 icon_info_new (void)
1896 GtkIconInfo *icon_info = g_new0 (GtkIconInfo, 1);
1898 icon_info->ref_count = 1;
1899 icon_info->scale = -1.;
1904 static GtkIconInfo *
1905 icon_info_new_builtin (BuiltinIcon *icon)
1907 GtkIconInfo *icon_info = icon_info_new ();
1909 icon_info->builtin_pixbuf = g_object_ref (icon->pixbuf);
1910 icon_info->dir_type = ICON_THEME_DIR_THRESHOLD;
1911 icon_info->dir_size = icon->size;
1912 icon_info->threshold = 2;
1918 * gtk_icon_info_copy:
1919 * @icon_info: a #GtkIconInfo
1921 * Make a copy of a #GtkIconInfo.
1923 * Return value: the new GtkIconInfo
1926 gtk_icon_info_copy (GtkIconInfo *icon_info)
1930 g_return_val_if_fail (icon_info != NULL, NULL);
1932 copy = g_memdup (icon_info, sizeof (GtkIconInfo));
1933 if (copy->builtin_pixbuf)
1934 g_object_ref (copy->builtin_pixbuf);
1936 g_object_ref (copy->pixbuf);
1937 if (copy->load_error)
1938 copy->load_error = g_error_copy (copy->load_error);
1940 copy->filename = g_strdup (copy->filename);
1946 * gtk_icon_info_free:
1947 * @icon_info: a #GtkIconInfo
1949 * Free a #GtkIconInfo and associated information
1952 gtk_icon_info_free (GtkIconInfo *icon_info)
1954 g_return_if_fail (icon_info != NULL);
1956 if (icon_info->filename)
1957 g_free (icon_info->filename);
1958 if (icon_info->builtin_pixbuf)
1959 g_object_unref (icon_info->builtin_pixbuf);
1960 if (icon_info->pixbuf)
1961 g_object_unref (icon_info->pixbuf);
1967 * gtk_icon_info_get_base_size:
1968 * @icon_info: a #GtkIconInfo
1970 * Gets the base size for the icon. The base size
1971 * is a size for the icon that was specified by
1972 * the icon theme creator. This may be different
1973 * than the actual size of image; an example of
1974 * this is small emblem icons that can be attached
1975 * to a larger icon. These icons will be given
1976 * the same base size as the larger icons to which
1977 * they are attached.
1979 * Return value: the base size, or 0, if no base
1980 * size is known for the icon.
1983 gtk_icon_info_get_base_size (GtkIconInfo *icon_info)
1985 g_return_val_if_fail (icon_info != NULL, 0);
1987 return icon_info->dir_size;
1991 * gtk_icon_info_get_filename:
1992 * @icon_info: a #GtkIconInfo
1994 * Gets the filename for the icon. If the
1995 * %GTK_ICON_LOOKUP_USE_BUILTIN flag was passed
1996 * to gtk_icon_theme_lookup_icon(), there may be
1997 * no filename if a builtin icon is returned; in this
1998 * case, you should use gtk_icon_info_get_builtin_pixbuf().
2000 * Return value: the filename for the icon, or %NULL
2001 * if gtk_icon_info_get_builtin_pixbuf() should
2002 * be used instead. The return value is owned by
2003 * GTK+ and should not be modified or freed.
2005 G_CONST_RETURN gchar *
2006 gtk_icon_info_get_filename (GtkIconInfo *icon_info)
2008 g_return_val_if_fail (icon_info != NULL, NULL);
2010 return icon_info->filename;
2014 * gtk_icon_info_get_builtin_pixbuf:
2015 * @icon_info: a #GtkIconInfo structure
2017 * Gets the built-in image for this icon, if any. To allow
2018 * GTK+ to use built in icon images, you must pass the
2019 * %GTK_ICON_LOOKUP_USE_BUILTIN to
2020 * gtk_icon_theme_lookup_icon().
2022 * Return value: the built-in image pixbuf, or %NULL. No
2023 * extra reference is added to the returned pixbuf, so if
2024 * you want to keep it around, you must use g_object_ref().
2025 * The returned image must not be modified.
2028 gtk_icon_info_get_builtin_pixbuf (GtkIconInfo *icon_info)
2030 g_return_val_if_fail (icon_info != NULL, NULL);
2032 return icon_info->builtin_pixbuf;
2036 load_svg_at_size (const gchar *filename,
2040 GdkPixbuf *pixbuf = NULL;
2041 GdkPixbufLoader *loader = NULL;
2045 if (!g_file_get_contents (filename,
2046 &contents, &length, error))
2049 loader = gdk_pixbuf_loader_new ();
2050 gdk_pixbuf_loader_set_size (loader, size, size);
2052 if (!gdk_pixbuf_loader_write (loader, contents, length, error))
2054 gdk_pixbuf_loader_close (loader, NULL);
2058 if (!gdk_pixbuf_loader_close (loader, error))
2061 pixbuf = g_object_ref (gdk_pixbuf_loader_get_pixbuf (loader));
2065 g_object_unref (loader);
2070 /* This function contains the complicatd logic for deciding
2071 * on the size at which to load the icon and loading it at
2075 icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
2076 gboolean scale_only)
2078 int image_width, image_height;
2079 GdkPixbuf *source_pixbuf;
2081 /* First check if we already succeeded have the necessary
2082 * information (or failed earlier)
2084 if (scale_only && icon_info->scale >= 0)
2087 if (icon_info->pixbuf)
2090 if (icon_info->load_error)
2093 /* SVG icons are a special case - we just immediately scale them
2094 * to the desired size
2096 if (icon_info->filename && g_str_has_suffix (icon_info->filename, ".svg"))
2098 icon_info->scale = icon_info->desired_size / 1000.;
2103 icon_info->pixbuf = load_svg_at_size (icon_info->filename,
2104 icon_info->desired_size,
2105 &icon_info->load_error);
2107 return icon_info->pixbuf != NULL;
2110 /* In many cases, the scale can be determined without actual access
2111 * to the icon file. This is generally true when we have a size
2112 * for the directory where the icon is; the image size doesn't
2113 * matter in that case.
2115 if (icon_info->dir_type == ICON_THEME_DIR_FIXED)
2116 icon_info->scale = 1.0;
2117 else if (icon_info->dir_type == ICON_THEME_DIR_THRESHOLD)
2119 if (icon_info->desired_size >= icon_info->dir_size - icon_info->threshold &&
2120 icon_info->desired_size <= icon_info->dir_size + icon_info->threshold)
2121 icon_info->scale = 1.0;
2122 else if (icon_info->dir_size > 0)
2123 icon_info->scale =(gdouble) icon_info->desired_size / icon_info->dir_size;
2125 else if (icon_info->dir_type == ICON_THEME_DIR_SCALABLE)
2127 if (icon_info->dir_size > 0)
2128 icon_info->scale = (gdouble) icon_info->desired_size / icon_info->dir_size;
2131 if (icon_info->scale >= 0. && scale_only)
2134 /* At this point, we need to actually get the icon; either from the
2135 * builting image or by loading the file
2137 if (icon_info->builtin_pixbuf)
2138 source_pixbuf = g_object_ref (icon_info->builtin_pixbuf);
2141 source_pixbuf = gdk_pixbuf_new_from_file (icon_info->filename,
2142 &icon_info->load_error);
2147 /* Do scale calculations that depend on the image size
2149 image_width = gdk_pixbuf_get_width (source_pixbuf);
2150 image_height = gdk_pixbuf_get_height (source_pixbuf);
2152 if (icon_info->scale < 0.0)
2154 gint image_size = MAX (image_width, image_height);
2156 icon_info->scale = icon_info->desired_size / image_size;
2158 icon_info->scale = 1.0;
2160 if (icon_info->dir_type == ICON_THEME_DIR_UNTHEMED)
2161 icon_info->scale = MAX (icon_info->scale, 1.0);
2164 /* We don't short-circuit out here for scale_only, since, now
2165 * we've loaded the icon, we might as well go ahead and finish
2166 * the job. This is a bit of a waste when we scale here
2167 * and never get the final pixbuf; at the cost of a bit of
2168 * extra complexity, we could keep the source pixbuf around
2169 * but not actually scale it until neede.
2172 if (icon_info->scale == 1.0)
2173 icon_info->pixbuf = source_pixbuf;
2176 icon_info->pixbuf = gdk_pixbuf_scale_simple (source_pixbuf,
2177 0.5 + image_width * icon_info->scale,
2178 0.5 + image_height * icon_info->scale,
2179 GDK_INTERP_BILINEAR);
2180 g_object_unref (source_pixbuf);
2187 * gtk_icon_info_load_icon:
2188 * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
2191 * Renders an icon previously looked up in an icon theme using
2192 * gtk_icon_theme_lookup_icon(); the size will be based on the size
2193 * pssed to gtk_icon_theme_lookup_icon(). Note that the resulting
2194 * pixbuf may not be exactly this size; an icon theme may have icons
2195 * that differ slightly from their nominal sizes, and in addition GTK+
2196 * will avoid scaling icons that it considers sufficiently close to the
2197 * requested size. (This maintains sharpness.)
2199 * Return value: the rendered icon; this may be a newly created icon
2200 * or a new reference to an internal icon, so you must not modify
2201 * the icon. Use g_object_unref() to release your reference to the
2205 gtk_icon_info_load_icon (GtkIconInfo *icon_info,
2208 g_return_val_if_fail (icon_info != NULL, NULL);
2210 g_return_val_if_fail (icon_info != NULL, NULL);
2211 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2213 icon_info_ensure_scale_and_pixbuf (icon_info, FALSE);
2215 if (icon_info->load_error)
2217 g_propagate_error (error, icon_info->load_error);
2221 return g_object_ref (icon_info->pixbuf);
2225 * gtk_icon_info_set_raw_coordinates:
2226 * @icon_info: a #GtkIconInfo
2227 * @raw_coordinates: whether the coordinates of embedded rectangles
2228 * and attached points should be returned in their original
2231 * Sets whether the coordinates returned by gtk_icon_info_get_embedded_rect()
2232 * and gtk_icon_info_get_attach_points() should be returned in their
2233 * original form as specified in the icon theme, instead of scaled
2234 * appropriately for the pixbuf returned by gtk_icon_info_load_icon().
2236 * Raw coordinates are somewhat strange; they are specified to be with
2237 * respect to the unscaled pixmap for PNG and XPM icons, but for SVG
2238 * icons, they are in a 1000x1000 coordinate space that is scaled
2239 * to the final size of the icon. You can determine if the icon is an SVG
2240 * icon by using gtk_icon_info_get_filename(), and seeing if it is non-%NULL
2241 * and ends in '.svg'.
2243 * This function is provided primarily to allow compatibility wrappers
2244 * for older API's, and is not expected to be useful for applications.
2247 gtk_icon_info_set_raw_coordinates (GtkIconInfo *icon_info,
2248 gboolean raw_coordinates)
2250 g_return_if_fail (icon_info != NULL);
2252 icon_info->raw_coordinates = raw_coordinates != FALSE;
2255 /* Scale coordinates from the icon data prior to returning
2259 icon_info_scale_point (GtkIconInfo *icon_info,
2265 if (icon_info->raw_coordinates)
2272 if (!icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2275 *x_out = 0.5 + x * icon_info->scale;
2276 *y_out = 0.5 + y * icon_info->scale;
2283 * gtk_icon_info_get_embedded_rect:
2284 * @icon_info: a #GtkIconInfo
2285 * @rectangle: #GdkRectangle in which to store embedded
2286 * rectangle coordinates; coordinates are only stored
2287 * when this function returns %TRUE.
2289 * Gets the coordinates of a rectangle within the icon
2290 * that can be used for display of information such
2291 * as a preview of the contents of a text file.
2292 * See gtk_icon_info_set_raw_coordinates() for further
2293 * information about the coordinate system.
2295 * Return value: %TRUE if the icon has an embedded rectangle
2298 gtk_icon_info_get_embedded_rect (GtkIconInfo *icon_info,
2299 GdkRectangle *rectangle)
2301 g_return_val_if_fail (icon_info != NULL, FALSE);
2303 if (icon_info->data && icon_info->data->has_embedded_rect &&
2304 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2306 gint scaled_x0, scaled_y0;
2307 gint scaled_x1, scaled_y1;
2311 icon_info_scale_point (icon_info,
2312 icon_info->data->x0, icon_info->data->y0,
2313 &scaled_x0, &scaled_y0);
2314 icon_info_scale_point (icon_info,
2315 icon_info->data->x0, icon_info->data->y0,
2316 &scaled_x1, &scaled_y1);
2318 rectangle->x = scaled_x0;
2319 rectangle->y = scaled_y0;
2320 rectangle->width = scaled_x1 - rectangle->x;
2321 rectangle->height = scaled_y1 - rectangle->y;
2331 * gtk_icon_info_get_attach_points:
2332 * @icon_info: a #GtkIconInfo
2333 * @points: location to store pointer to an array of points, or %NULL
2334 * free the array of points with g_free().
2335 * @n_points: location to store the number of points in @points, or %NULL
2337 * Fetches the set of attach points for an icon. An attach point
2338 * is a location in the icon that can be used as anchor points for attaching
2339 * emblems or overlays to the icon.
2341 * Return value: %TRUE if there are any attach points for the icon.
2344 gtk_icon_info_get_attach_points (GtkIconInfo *icon_info,
2348 g_return_val_if_fail (icon_info != NULL, FALSE);
2350 if (icon_info->data && icon_info->data->n_attach_points &&
2351 icon_info_ensure_scale_and_pixbuf (icon_info, TRUE))
2357 *points = g_new (GdkPoint, icon_info->data->n_attach_points);
2358 for (i = 0; i < icon_info->data->n_attach_points; i++)
2359 icon_info_scale_point (icon_info,
2360 icon_info->data->attach_points[i].x,
2361 icon_info->data->attach_points[i].y,
2367 *n_points = icon_info->data->n_attach_points;
2383 * gtk_icon_info_get_display_name:
2384 * @icon_info: a #GtkIconInfo
2386 * Gets the display name for an icon. A display name is a
2387 * string to be used in place of the icon name in a user
2388 * visible context like a list of icons.
2390 * Return value: the display name for the icon or %NULL, if
2391 * the icon doesn't have a specified display name. This value
2392 * is owned @icon_info and must not be modified or free.
2394 G_CONST_RETURN gchar *
2395 gtk_icon_info_get_display_name (GtkIconInfo *icon_info)
2397 g_return_val_if_fail (icon_info != NULL, NULL);
2399 if (icon_info->data)
2400 return icon_info->data->display_name;
2411 * gtk_icon_theme_add_builtin_icon:
2412 * @icon_name: the name of the icon to register
2413 * @size: the size at which to register the icon (different
2414 * images can be registered for the same icon name
2415 * at different sizes.)
2416 * @pixbuf: #GdkPixbuf that contains the image to use
2419 * Registers a built-in icon for icon theme lookups. The idea
2420 * of built-in icons is to allow an application or library
2421 * that uses themed icons to function requiring files to
2422 * be present in the file system. For instance, the default
2423 * images for all of GTK+'s stock icons are registered
2426 * In general, if you use gtk_icon_theme_add_builtin_icon()
2427 * you should also install the icon in the icon theme, so
2428 * that the icon is generally available.
2430 * This function will generally be used with pixbufs loaded
2431 * via gdk_pixbuf_new_from_inline ().
2434 gtk_icon_theme_add_builtin_icon (const gchar *icon_name,
2438 BuiltinIcon *default_icon;
2442 g_return_if_fail (icon_name != NULL);
2443 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2445 if (!icon_theme_builtin_icons)
2446 icon_theme_builtin_icons = g_hash_table_new (g_str_hash, g_str_equal);
2448 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
2450 key = g_strdup (icon_name);
2452 key = (gpointer)icon_name; /* Won't get stored */
2454 default_icon = g_new (BuiltinIcon, 1);
2455 default_icon->size = size;
2456 default_icon->pixbuf = g_object_ref (pixbuf);
2457 icons = g_slist_prepend (icons, default_icon);
2459 /* Replaces value, leaves key untouched
2461 g_hash_table_insert (icon_theme_builtin_icons, key, icons);
2464 /* Look up a builtin icon; the min_difference_p and
2465 * has_larger_p out parameters allow us to combine
2466 * this lookup with searching through the actual directories
2467 * of the "hicolor" icon theme. See theme_lookup_icon()
2468 * for how they are used.
2470 static BuiltinIcon *
2471 find_builtin_icon (const gchar *icon_name,
2473 gint *min_difference_p,
2474 gboolean *has_larger_p)
2476 GSList *icons = NULL;
2477 gint min_difference = G_MAXINT;
2478 gboolean has_larger = FALSE;
2479 BuiltinIcon *min_icon = NULL;
2481 if (!icon_theme_builtin_icons)
2484 icons = g_hash_table_lookup (icon_theme_builtin_icons, icon_name);
2488 BuiltinIcon *default_icon = icons->data;
2489 int min, max, difference;
2492 min = default_icon->size - 2;
2493 max = default_icon->size + 2;
2494 smaller = size < min;
2496 difference = min - size;
2498 difference = size - max;
2502 if (difference == 0)
2504 min_icon = default_icon;
2510 if (difference < min_difference || smaller)
2512 min_difference = difference;
2513 min_icon = default_icon;
2514 has_larger = smaller;
2519 if (difference < min_difference && smaller)
2521 min_difference = difference;
2522 min_icon = default_icon;
2526 icons = icons->next;
2529 if (min_difference_p)
2530 *min_difference_p = min_difference;
2532 *has_larger_p = has_larger;