1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2000 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 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #include "gtkiconfactory.h"
28 #include "stock-icons/gtkstockpixbufs.h"
35 static GSList *all_icon_factories = NULL;
39 /* Either filename or pixbuf can be NULL. If both are non-NULL,
40 * the pixbuf is assumed to be the already-loaded contents of the
46 GtkTextDirection direction;
50 /* If TRUE, then the parameter is wildcarded, and the above
51 * fields should be ignored. If FALSE, the parameter is
52 * specified, and the above fields should be valid.
54 guint any_direction : 1;
59 static gpointer parent_class = NULL;
61 static void gtk_icon_factory_init (GtkIconFactory *icon_factory);
62 static void gtk_icon_factory_class_init (GtkIconFactoryClass *klass);
63 static void gtk_icon_factory_finalize (GObject *object);
64 static void get_default_icons (GtkIconFactory *icon_factory);
67 gtk_icon_factory_get_type (void)
69 static GType object_type = 0;
73 static const GTypeInfo object_info =
75 sizeof (GtkIconFactoryClass),
77 (GBaseFinalizeFunc) NULL,
78 (GClassInitFunc) gtk_icon_factory_class_init,
79 NULL, /* class_finalize */
80 NULL, /* class_data */
81 sizeof (GtkIconFactory),
83 (GInstanceInitFunc) gtk_icon_factory_init,
86 object_type = g_type_register_static (G_TYPE_OBJECT,
95 gtk_icon_factory_init (GtkIconFactory *factory)
97 factory->icons = g_hash_table_new (g_str_hash, g_str_equal);
98 all_icon_factories = g_slist_prepend (all_icon_factories, factory);
102 gtk_icon_factory_class_init (GtkIconFactoryClass *klass)
104 GObjectClass *object_class = G_OBJECT_CLASS (klass);
106 parent_class = g_type_class_peek_parent (klass);
108 object_class->finalize = gtk_icon_factory_finalize;
112 free_icon_set (gpointer key, gpointer value, gpointer data)
115 gtk_icon_set_unref (value);
119 gtk_icon_factory_finalize (GObject *object)
121 GtkIconFactory *factory = GTK_ICON_FACTORY (object);
123 all_icon_factories = g_slist_remove (all_icon_factories, factory);
125 g_hash_table_foreach (factory->icons, free_icon_set, NULL);
127 g_hash_table_destroy (factory->icons);
129 G_OBJECT_CLASS (parent_class)->finalize (object);
133 * gtk_icon_factory_new:
135 * Creates a new #GtkIconFactory. An icon factory manages a collection
136 * of #GtkIconSet<!>s; a #GtkIconSet manages a set of variants of a
137 * particular icon (i.e. a #GtkIconSet contains variants for different
138 * sizes and widget states). Icons in an icon factory are named by a
139 * stock ID, which is a simple string identifying the icon. Each
140 * #GtkStyle has a list of #GtkIconFactory<!>s derived from the current
141 * theme; those icon factories are consulted first when searching for
142 * an icon. If the theme doesn't set a particular icon, GTK+ looks for
143 * the icon in a list of default icon factories, maintained by
144 * gtk_icon_factory_add_default() and
145 * gtk_icon_factory_remove_default(). Applications with icons should
146 * add a default icon factory with their icons, which will allow
147 * themes to override the icons for the application.
149 * Return value: a new #GtkIconFactory
152 gtk_icon_factory_new (void)
154 return GTK_ICON_FACTORY (g_object_new (GTK_TYPE_ICON_FACTORY, NULL));
158 * gtk_icon_factory_add:
159 * @factory: a #GtkIconFactory
160 * @stock_id: icon name
161 * @icon_set: icon set
163 * Adds the given @icon_set to the icon factory, under the name
164 * @stock_id. @stock_id should be namespaced for your application,
165 * e.g. "myapp-whatever-icon". Normally applications create a
166 * #GtkIconFactory, then add it to the list of default factories with
167 * gtk_icon_factory_add_default(). Then they pass the @stock_id to
168 * widgets such as #GtkImage to display the icon. Themes can provide
169 * an icon with the same name (such as "myapp-whatever-icon") to
170 * override your application's default icons. If an icon already
171 * existed in @factory for @stock_id, it is unreferenced and replaced
172 * with the new @icon_set.
176 gtk_icon_factory_add (GtkIconFactory *factory,
177 const gchar *stock_id,
178 GtkIconSet *icon_set)
180 gpointer old_key = NULL;
181 gpointer old_value = NULL;
183 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
184 g_return_if_fail (stock_id != NULL);
185 g_return_if_fail (icon_set != NULL);
187 g_hash_table_lookup_extended (factory->icons, stock_id,
188 &old_key, &old_value);
190 if (old_value == icon_set)
193 gtk_icon_set_ref (icon_set);
195 /* GHashTable key memory management is so fantastically broken. */
197 g_hash_table_insert (factory->icons, old_key, icon_set);
199 g_hash_table_insert (factory->icons, g_strdup (stock_id), icon_set);
202 gtk_icon_set_unref (old_value);
206 * gtk_icon_factory_lookup:
207 * @factory: a #GtkIconFactory
208 * @stock_id: an icon name
210 * Looks up @stock_id in the icon factory, returning an icon set
211 * if found, otherwise %NULL. For display to the user, you should
212 * use gtk_style_lookup_icon_set() on the #GtkStyle for the
213 * widget that will display the icon, instead of using this
214 * function directly, so that themes are taken into account.
216 * Return value: icon set of @stock_id.
219 gtk_icon_factory_lookup (GtkIconFactory *factory,
220 const gchar *stock_id)
222 g_return_val_if_fail (GTK_IS_ICON_FACTORY (factory), NULL);
223 g_return_val_if_fail (stock_id != NULL, NULL);
225 return g_hash_table_lookup (factory->icons, stock_id);
228 static GtkIconFactory *gtk_default_icons = NULL;
229 static GSList *default_factories = NULL;
232 * gtk_icon_factory_add_default:
233 * @factory: a #GtkIconFactory
235 * Adds an icon factory to the list of icon factories searched by
236 * gtk_style_lookup_icon_set(). This means that, for example,
237 * gtk_image_new_from_stock() will be able to find icons in @factory.
238 * There will normally be an icon factory added for each library or
239 * application that comes with icons. The default icon factories
240 * can be overridden by themes.
244 gtk_icon_factory_add_default (GtkIconFactory *factory)
246 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
248 g_object_ref (G_OBJECT (factory));
250 default_factories = g_slist_prepend (default_factories, factory);
254 * gtk_icon_factory_remove_default:
255 * @factory: a #GtkIconFactory previously added with gtk_icon_factory_add_default()
257 * Removes an icon factory from the list of default icon
258 * factories. Not normally used; you might use it for a library that
259 * can be unloaded or shut down.
263 gtk_icon_factory_remove_default (GtkIconFactory *factory)
265 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
267 default_factories = g_slist_remove (default_factories, factory);
269 g_object_unref (G_OBJECT (factory));
273 ensure_default_icons (void)
275 if (gtk_default_icons == NULL)
277 gtk_default_icons = gtk_icon_factory_new ();
279 get_default_icons (gtk_default_icons);
284 * gtk_icon_factory_lookup_default:
285 * @stock_id: an icon name
287 * Looks for an icon in the list of default icon factories. For
288 * display to the user, you should use gtk_style_lookup_icon_set() on
289 * the #GtkStyle for the widget that will display the icon, instead of
290 * using this function directly, so that themes are taken into
294 * Return value: a #GtkIconSet, or %NULL
297 gtk_icon_factory_lookup_default (const gchar *stock_id)
301 g_return_val_if_fail (stock_id != NULL, NULL);
303 tmp_list = default_factories;
304 while (tmp_list != NULL)
306 GtkIconSet *icon_set =
307 gtk_icon_factory_lookup (GTK_ICON_FACTORY (tmp_list->data),
313 tmp_list = g_slist_next (tmp_list);
316 ensure_default_icons ();
318 return gtk_icon_factory_lookup (gtk_default_icons, stock_id);
323 sized_icon_set_from_inline (const guchar *inline_data,
328 GtkIconSource source = { NULL, NULL, 0, 0, 0,
333 set = gtk_icon_set_new ();
335 source.pixbuf = gdk_pixbuf_new_from_inline (-1, inline_data, FALSE, NULL);
337 g_assert (source.pixbuf);
339 gtk_icon_set_add_source (set, &source);
341 g_object_unref (G_OBJECT (source.pixbuf));
348 sized_with_fallback_icon_set_from_inline (const guchar *fallback_data,
349 const guchar *inline_data,
354 GtkIconSource source = { NULL, NULL, 0, 0, 0,
359 set = gtk_icon_set_new ();
361 source.pixbuf = gdk_pixbuf_new_from_inline (-1, inline_data, FALSE, NULL);
363 g_assert (source.pixbuf);
365 gtk_icon_set_add_source (set, &source);
367 g_object_unref (G_OBJECT (source.pixbuf));
369 source.any_size = TRUE;
371 source.pixbuf = gdk_pixbuf_new_from_inline (-1, fallback_data, FALSE, NULL);
373 g_assert (source.pixbuf);
375 gtk_icon_set_add_source (set, &source);
377 g_object_unref (G_OBJECT (source.pixbuf));
383 unsized_icon_set_from_inline (const guchar *inline_data)
387 /* This icon can be used for any direction/state/size */
388 GtkIconSource source = { NULL, NULL, 0, 0, 0,
391 set = gtk_icon_set_new ();
393 source.pixbuf = gdk_pixbuf_new_from_inline (-1, inline_data, FALSE, NULL);
395 g_assert (source.pixbuf);
397 gtk_icon_set_add_source (set, &source);
399 g_object_unref (G_OBJECT (source.pixbuf));
406 add_sized (GtkIconFactory *factory,
407 const guchar *inline_data,
409 const gchar *stock_id)
413 set = sized_icon_set_from_inline (inline_data, size);
415 gtk_icon_factory_add (factory, stock_id, set);
417 gtk_icon_set_unref (set);
422 add_sized_with_fallback (GtkIconFactory *factory,
423 const guchar *fallback_data,
424 const guchar *inline_data,
426 const gchar *stock_id)
430 set = sized_with_fallback_icon_set_from_inline (fallback_data, inline_data, size);
432 gtk_icon_factory_add (factory, stock_id, set);
434 gtk_icon_set_unref (set);
438 add_unsized (GtkIconFactory *factory,
439 const guchar *inline_data,
440 const gchar *stock_id)
444 set = unsized_icon_set_from_inline (inline_data);
446 gtk_icon_factory_add (factory, stock_id, set);
448 gtk_icon_set_unref (set);
452 get_default_icons (GtkIconFactory *factory)
454 /* KEEP IN SYNC with gtkstock.c */
456 /* We add all stock icons unsized, since it's confusing if icons only
457 * can be loaded at certain sizes.
460 /* Have dialog size */
461 add_unsized (factory, stock_dialog_error_48, GTK_STOCK_DIALOG_ERROR);
462 add_unsized (factory, stock_dialog_info_48, GTK_STOCK_DIALOG_INFO);
463 add_unsized (factory, stock_dialog_question_48, GTK_STOCK_DIALOG_QUESTION);
464 add_unsized (factory, stock_dialog_warning_48,GTK_STOCK_DIALOG_WARNING);
467 add_unsized (factory, stock_dnd_32, GTK_STOCK_DND);
468 add_unsized (factory, stock_dnd_multiple_32, GTK_STOCK_DND_MULTIPLE);
470 /* Have button sizes */
471 add_unsized (factory, stock_apply_20, GTK_STOCK_APPLY);
472 add_unsized (factory, stock_cancel_20, GTK_STOCK_CANCEL);
473 add_unsized (factory, stock_no_20, GTK_STOCK_NO);
474 add_unsized (factory, stock_ok_20, GTK_STOCK_OK);
475 add_unsized (factory, stock_yes_20, GTK_STOCK_YES);
477 /* Generic + button sizes */
478 add_sized_with_fallback (factory,
481 GTK_ICON_SIZE_BUTTON,
484 /* Generic + menu sizes */
485 add_sized_with_fallback (factory,
486 stock_align_center_24,
487 stock_align_center_16,
489 GTK_STOCK_JUSTIFY_CENTER);
491 add_sized_with_fallback (factory,
492 stock_align_justify_24,
493 stock_align_justify_16,
495 GTK_STOCK_JUSTIFY_FILL);
497 add_sized_with_fallback (factory,
501 GTK_STOCK_JUSTIFY_LEFT);
503 add_sized_with_fallback (factory,
504 stock_align_right_24,
505 stock_align_right_16,
507 GTK_STOCK_JUSTIFY_RIGHT);
509 add_sized_with_fallback (factory,
513 GTK_STOCK_GOTO_BOTTOM);
515 add_sized_with_fallback (factory,
521 add_sized_with_fallback (factory,
527 add_sized_with_fallback (factory,
533 add_sized_with_fallback (factory,
539 add_sized_with_fallback (factory,
545 add_sized_with_fallback (factory,
551 add_sized_with_fallback (factory,
557 add_sized_with_fallback (factory,
561 GTK_STOCK_GOTO_FIRST);
563 add_sized_with_fallback (factory,
567 GTK_STOCK_SELECT_FONT);
569 add_sized_with_fallback (factory,
575 add_sized_with_fallback (factory,
581 add_sized_with_fallback (factory,
587 add_sized_with_fallback (factory,
591 GTK_STOCK_GOTO_LAST);
593 add_sized_with_fallback (factory,
599 add_sized_with_fallback (factory,
600 stock_missing_image_24,
601 stock_missing_image_16,
603 GTK_STOCK_MISSING_IMAGE);
605 add_sized_with_fallback (factory,
611 add_sized_with_fallback (factory,
617 add_sized_with_fallback (factory,
623 add_sized_with_fallback (factory,
624 stock_preferences_24,
625 stock_preferences_16,
627 GTK_STOCK_PREFERENCES);
629 add_sized_with_fallback (factory,
635 add_sized_with_fallback (factory,
636 stock_print_preview_24,
637 stock_print_preview_16,
639 GTK_STOCK_PRINT_PREVIEW);
641 add_sized_with_fallback (factory,
647 add_sized_with_fallback (factory,
653 add_sized_with_fallback (factory,
657 GTK_STOCK_REVERT_TO_SAVED);
659 add_sized_with_fallback (factory,
660 stock_right_arrow_24,
661 stock_right_arrow_16,
663 GTK_STOCK_GO_FORWARD);
665 add_sized_with_fallback (factory,
671 add_sized_with_fallback (factory,
677 add_sized_with_fallback (factory,
683 add_sized_with_fallback (factory,
689 add_sized_with_fallback (factory,
690 stock_search_replace_24,
691 stock_search_replace_16,
693 GTK_STOCK_FIND_AND_REPLACE);
695 add_sized_with_fallback (factory,
696 stock_sort_descending_24,
697 stock_sort_descending_16,
699 GTK_STOCK_SORT_DESCENDING);
701 add_sized_with_fallback (factory,
702 stock_sort_ascending_24,
703 stock_sort_ascending_16,
705 GTK_STOCK_SORT_ASCENDING);
707 add_sized_with_fallback (factory,
711 GTK_STOCK_SPELL_CHECK);
713 add_sized_with_fallback (factory,
719 add_sized_with_fallback (factory,
725 add_sized_with_fallback (factory,
726 stock_text_italic_24,
727 stock_text_italic_16,
731 add_sized_with_fallback (factory,
732 stock_text_strikethrough_24,
733 stock_text_strikethrough_16,
735 GTK_STOCK_STRIKETHROUGH);
737 add_sized_with_fallback (factory,
738 stock_text_underline_24,
739 stock_text_underline_16,
741 GTK_STOCK_UNDERLINE);
743 add_sized_with_fallback (factory,
749 add_sized_with_fallback (factory,
755 add_sized_with_fallback (factory,
761 add_sized_with_fallback (factory,
767 add_sized_with_fallback (factory,
773 /* Generic size only */
775 add_unsized (factory, stock_add_24, GTK_STOCK_ADD);
776 add_unsized (factory, stock_clear_24, GTK_STOCK_CLEAR);
777 add_unsized (factory, stock_colorselector_24, GTK_STOCK_SELECT_COLOR);
778 add_unsized (factory, stock_index_24, GTK_STOCK_INDEX);
779 add_unsized (factory, stock_properties_24, GTK_STOCK_PROPERTIES);
780 add_unsized (factory, stock_remove_24, GTK_STOCK_REMOVE);
781 add_unsized (factory, stock_zoom_1_24, GTK_STOCK_ZOOM_100);
782 add_unsized (factory, stock_zoom_fit_24, GTK_STOCK_ZOOM_FIT);
783 add_unsized (factory, stock_zoom_in_24, GTK_STOCK_ZOOM_IN);
784 add_unsized (factory, stock_zoom_out_24, GTK_STOCK_ZOOM_OUT);
789 typedef struct _IconSize IconSize;
800 typedef struct _IconAlias IconAlias;
808 static GHashTable *icon_aliases = NULL;
809 static IconSize *icon_sizes = NULL;
810 static gint icon_sizes_allocated = 0;
811 static gint icon_sizes_used = 0;
814 init_icon_sizes (void)
816 if (icon_sizes == NULL)
818 #define NUM_BUILTIN_SIZES 7
821 icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
823 icon_sizes = g_new (IconSize, NUM_BUILTIN_SIZES);
824 icon_sizes_allocated = NUM_BUILTIN_SIZES;
825 icon_sizes_used = NUM_BUILTIN_SIZES;
827 icon_sizes[GTK_ICON_SIZE_INVALID].size = 0;
828 icon_sizes[GTK_ICON_SIZE_INVALID].name = NULL;
829 icon_sizes[GTK_ICON_SIZE_INVALID].width = 0;
830 icon_sizes[GTK_ICON_SIZE_INVALID].height = 0;
832 /* the name strings aren't copied since we don't ever remove
833 * icon sizes, so we don't need to know whether they're static.
834 * Even if we did I suppose removing the builtin sizes would be
838 icon_sizes[GTK_ICON_SIZE_MENU].size = GTK_ICON_SIZE_MENU;
839 icon_sizes[GTK_ICON_SIZE_MENU].name = "gtk-menu";
840 icon_sizes[GTK_ICON_SIZE_MENU].width = 16;
841 icon_sizes[GTK_ICON_SIZE_MENU].height = 16;
843 icon_sizes[GTK_ICON_SIZE_BUTTON].size = GTK_ICON_SIZE_BUTTON;
844 icon_sizes[GTK_ICON_SIZE_BUTTON].name = "gtk-button";
845 icon_sizes[GTK_ICON_SIZE_BUTTON].width = 20;
846 icon_sizes[GTK_ICON_SIZE_BUTTON].height = 20;
848 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].size = GTK_ICON_SIZE_SMALL_TOOLBAR;
849 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].name = "gtk-small-toolbar";
850 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].width = 18;
851 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].height = 18;
853 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].size = GTK_ICON_SIZE_LARGE_TOOLBAR;
854 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].name = "gtk-large-toolbar";
855 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].width = 24;
856 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].height = 24;
858 icon_sizes[GTK_ICON_SIZE_DND].size = GTK_ICON_SIZE_DND;
859 icon_sizes[GTK_ICON_SIZE_DND].name = "gtk-dnd";
860 icon_sizes[GTK_ICON_SIZE_DND].width = 32;
861 icon_sizes[GTK_ICON_SIZE_DND].height = 32;
863 icon_sizes[GTK_ICON_SIZE_DIALOG].size = GTK_ICON_SIZE_DIALOG;
864 icon_sizes[GTK_ICON_SIZE_DIALOG].name = "gtk-dialog";
865 icon_sizes[GTK_ICON_SIZE_DIALOG].width = 48;
866 icon_sizes[GTK_ICON_SIZE_DIALOG].height = 48;
868 g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
870 /* Alias everything to itself. */
871 i = 1; /* skip invalid size */
872 while (i < NUM_BUILTIN_SIZES)
874 gtk_icon_size_register_alias (icon_sizes[i].name, icon_sizes[i].size);
879 #undef NUM_BUILTIN_SIZES
884 * gtk_icon_size_lookup:
885 * @size: an icon size
886 * @width: location to store icon width
887 * @height: location to store icon height
889 * Obtains the pixel size of a semantic icon size, normally @size would be
890 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
891 * isn't normally needed, gtk_widget_render_icon() is the usual
892 * way to get an icon for rendering, then just look at the size of
893 * the rendered pixbuf. The rendered pixbuf may not even correspond to
894 * the width/height returned by gtk_icon_size_lookup(), because themes
895 * are free to render the pixbuf however they like, including changing
898 * Return value: %TRUE if @size was a valid size
901 gtk_icon_size_lookup (GtkIconSize size,
907 if (size >= icon_sizes_used)
910 if (size == GTK_ICON_SIZE_INVALID)
914 *widthp = icon_sizes[size].width;
917 *heightp = icon_sizes[size].height;
923 * gtk_icon_size_register:
924 * @name: name of the icon size
925 * @width: the icon width
926 * @height: the icon height
928 * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
929 * etc. Returns the integer value for the size.
931 * Returns: integer value representing the size
935 gtk_icon_size_register (const gchar *name,
939 g_return_val_if_fail (name != NULL, 0);
940 g_return_val_if_fail (width > 0, 0);
941 g_return_val_if_fail (height > 0, 0);
945 if (icon_sizes_used == icon_sizes_allocated)
947 icon_sizes_allocated *= 2;
948 icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated);
951 icon_sizes[icon_sizes_used].size = icon_sizes_used;
952 icon_sizes[icon_sizes_used].name = g_strdup (name);
953 icon_sizes[icon_sizes_used].width = width;
954 icon_sizes[icon_sizes_used].height = height;
959 gtk_icon_size_register_alias (name, icon_sizes_used - 1);
961 return icon_sizes_used - 1;
965 * gtk_icon_size_register_alias:
966 * @alias: an alias for @target
967 * @target: an existing icon size
969 * Registers @alias as another name for @target.
970 * So calling gtk_icon_size_from_name() with @alias as argument
971 * will return @target.
975 gtk_icon_size_register_alias (const gchar *alias,
980 g_return_if_fail (alias != NULL);
984 if (g_hash_table_lookup (icon_aliases, alias))
985 g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias);
987 if (!gtk_icon_size_lookup (target, NULL, NULL))
988 g_warning ("gtk_icon_size_register_alias: Icon size %d does not exist", target);
990 ia = g_new (IconAlias, 1);
991 ia->name = g_strdup (alias);
994 g_hash_table_insert (icon_aliases, ia->name, ia);
998 * gtk_icon_size_from_name:
999 * @name: the name to look up.
1000 * @returns: the icon size with the given name.
1002 * Looks up the icon size associated with @name.
1005 gtk_icon_size_from_name (const gchar *name)
1011 ia = g_hash_table_lookup (icon_aliases, name);
1016 return GTK_ICON_SIZE_INVALID;
1020 * gtk_icon_size_get_name:
1021 * @size: a #GtkIconSize.
1022 * @returns: the name of the given icon size.
1024 * Gets the canonical name of the given icon size. The returned string
1025 * is statically allocated and should not be freed.
1027 G_CONST_RETURN gchar*
1028 gtk_icon_size_get_name (GtkIconSize size)
1030 if (size >= icon_sizes_used)
1033 return icon_sizes[size].name;
1039 /* Clear icon set contents, drop references to all contained
1040 * GdkPixbuf objects and forget all GtkIconSources. Used to
1041 * recycle an icon set.
1043 static GdkPixbuf *find_in_cache (GtkIconSet *icon_set,
1045 GtkTextDirection direction,
1048 static void add_to_cache (GtkIconSet *icon_set,
1050 GtkTextDirection direction,
1054 static void clear_cache (GtkIconSet *icon_set,
1055 gboolean style_detach);
1056 static GSList* copy_cache (GtkIconSet *icon_set,
1057 GtkIconSet *copy_recipient);
1058 static void attach_to_style (GtkIconSet *icon_set,
1060 static void detach_from_style (GtkIconSet *icon_set,
1062 static void style_dnotify (gpointer data);
1070 /* Cache of the last few rendered versions of the icon. */
1078 static guint cache_serial = 0;
1083 * Creates a new #GtkIconSet. A #GtkIconSet represents a single icon
1084 * in various sizes and widget states. It can provide a #GdkPixbuf
1085 * for a given size and state on request, and automatically caches
1086 * some of the rendered #GdkPixbuf objects.
1088 * Normally you would use gtk_widget_render_icon() instead of
1089 * using #GtkIconSet directly. The one case where you'd use
1090 * #GtkIconSet is to create application-specific icon sets to place in
1091 * a #GtkIconFactory.
1093 * Return value: a new #GtkIconSet
1096 gtk_icon_set_new (void)
1098 GtkIconSet *icon_set;
1100 icon_set = g_new (GtkIconSet, 1);
1102 icon_set->ref_count = 1;
1103 icon_set->sources = NULL;
1104 icon_set->cache = NULL;
1105 icon_set->cache_size = 0;
1106 icon_set->cache_serial = cache_serial;
1112 * gtk_icon_set_new_from_pixbuf:
1113 * @pixbuf: a #GdkPixbuf
1115 * Creates a new #GtkIconSet with @pixbuf as the default/fallback
1116 * source image. If you don't add any additional #GtkIconSource to the
1117 * icon set, all variants of the icon will be created from @pixbuf,
1118 * using scaling, pixelation, etc. as required to adjust the icon size
1119 * or make the icon look insensitive/prelighted.
1121 * Return value: a new #GtkIconSet
1124 gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
1128 GtkIconSource source = { NULL, NULL, 0, 0, 0,
1131 g_return_val_if_fail (pixbuf != NULL, NULL);
1133 set = gtk_icon_set_new ();
1135 source.pixbuf = pixbuf;
1137 gtk_icon_set_add_source (set, &source);
1145 * @icon_set: a #GtkIconSet.
1147 * Increments the reference count on @icon_set.
1149 * Return value: @icon_set.
1152 gtk_icon_set_ref (GtkIconSet *icon_set)
1154 g_return_val_if_fail (icon_set != NULL, NULL);
1155 g_return_val_if_fail (icon_set->ref_count > 0, NULL);
1157 icon_set->ref_count += 1;
1163 * gtk_icon_set_unref:
1164 * @icon_set: a #GtkIconSet
1166 * Decrements the reference count on @icon_set, and frees memory
1167 * if the reference count reaches 0.
1170 gtk_icon_set_unref (GtkIconSet *icon_set)
1172 g_return_if_fail (icon_set != NULL);
1173 g_return_if_fail (icon_set->ref_count > 0);
1175 icon_set->ref_count -= 1;
1177 if (icon_set->ref_count == 0)
1179 GSList *tmp_list = icon_set->sources;
1180 while (tmp_list != NULL)
1182 gtk_icon_source_free (tmp_list->data);
1184 tmp_list = g_slist_next (tmp_list);
1187 clear_cache (icon_set, TRUE);
1194 * gtk_icon_set_copy:
1195 * @icon_set: a #GtkIconSet
1197 * Copies @icon_set by value.
1199 * Return value: a new #GtkIconSet identical to the first.
1202 gtk_icon_set_copy (GtkIconSet *icon_set)
1207 copy = gtk_icon_set_new ();
1209 tmp_list = icon_set->sources;
1210 while (tmp_list != NULL)
1212 copy->sources = g_slist_prepend (copy->sources,
1213 gtk_icon_source_copy (tmp_list->data));
1215 tmp_list = g_slist_next (tmp_list);
1218 copy->sources = g_slist_reverse (copy->sources);
1220 copy->cache = copy_cache (icon_set, copy);
1221 copy->cache_size = icon_set->cache_size;
1222 copy->cache_serial = icon_set->cache_serial;
1229 sizes_equivalent (GtkIconSize lhs,
1232 gint r_w, r_h, l_w, l_h;
1234 gtk_icon_size_lookup (rhs, &r_w, &r_h);
1235 gtk_icon_size_lookup (lhs, &l_w, &l_h);
1237 return r_w == l_w && r_h == l_h;
1240 static GtkIconSource*
1241 find_and_prep_icon_source (GtkIconSet *icon_set,
1242 GtkTextDirection direction,
1246 GtkIconSource *source;
1250 /* We need to find the best icon source. Direction matters more
1251 * than state, state matters more than size. icon_set->sources
1252 * is sorted according to wildness, so if we take the first
1253 * match we find it will be the least-wild match (if there are
1254 * multiple matches for a given "wildness" then the RC file contained
1255 * dumb stuff, and we end up with an arbitrary matching source)
1259 tmp_list = icon_set->sources;
1260 while (tmp_list != NULL)
1262 GtkIconSource *s = tmp_list->data;
1264 if ((s->any_direction || (s->direction == direction)) &&
1265 (s->any_state || (s->state == state)) &&
1266 (s->any_size || (sizes_equivalent (size, s->size))))
1272 tmp_list = g_slist_next (tmp_list);
1278 if (source->pixbuf == NULL)
1280 GError *error = NULL;
1282 g_assert (source->filename);
1283 source->pixbuf = gdk_pixbuf_new_from_file (source->filename, &error);
1285 if (source->pixbuf == NULL)
1287 /* Remove this icon source so we don't keep trying to
1290 g_warning (_("Error loading icon: %s"), error->message);
1291 g_error_free (error);
1293 icon_set->sources = g_slist_remove (icon_set->sources, source);
1295 gtk_icon_source_free (source);
1297 /* Try to fall back to other sources */
1298 if (icon_set->sources != NULL)
1299 return find_and_prep_icon_source (icon_set,
1312 render_fallback_image (GtkStyle *style,
1313 GtkTextDirection direction,
1319 /* This icon can be used for any direction/state/size */
1320 static GtkIconSource fallback_source = { NULL, NULL, 0, 0, 0, TRUE, TRUE, TRUE };
1322 if (fallback_source.pixbuf == NULL)
1323 fallback_source.pixbuf = gdk_pixbuf_new_from_inline (-1, stock_missing_image_24, FALSE, NULL);
1325 return gtk_style_render_icon (style,
1335 * gtk_icon_set_render_icon:
1336 * @icon_set: a #GtkIconSet
1337 * @style: a #GtkStyle associated with @widget, or %NULL
1338 * @direction: text direction
1339 * @state: widget state
1341 * @widget: widget that will display the icon, or %NULL
1342 * @detail: detail to pass to the theme engine, or %NULL
1344 * Renders an icon using gtk_style_render_icon(). In most cases,
1345 * gtk_widget_render_icon() is better, since it automatically provides
1346 * most of the arguments from the current widget settings. This
1347 * function never returns %NULL; if the icon can't be rendered
1348 * (perhaps because an image file fails to load), a default "missing
1349 * image" icon will be returned instead.
1351 * Return value: a #GdkPixbuf to be displayed
1354 gtk_icon_set_render_icon (GtkIconSet *icon_set,
1356 GtkTextDirection direction,
1363 GtkIconSource *source;
1365 g_return_val_if_fail (icon_set != NULL, NULL);
1366 g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
1368 if (icon_set->sources == NULL)
1369 return render_fallback_image (style, direction, state, size, widget, detail);
1371 icon = find_in_cache (icon_set, style, direction,
1376 g_object_ref (G_OBJECT (icon));
1381 source = find_and_prep_icon_source (icon_set, direction, state, size);
1384 return render_fallback_image (style, direction, state, size, widget, detail);
1386 g_assert (source->pixbuf != NULL);
1388 icon = gtk_style_render_icon (style,
1398 g_warning ("Theme engine failed to render icon");
1402 add_to_cache (icon_set, style, direction, state, size, icon);
1407 /* Order sources by their "wildness", so that "wilder" sources are
1408 * greater than "specific" sources; for determining ordering,
1409 * direction beats state beats size.
1413 icon_source_compare (gconstpointer ap, gconstpointer bp)
1415 const GtkIconSource *a = ap;
1416 const GtkIconSource *b = bp;
1418 if (!a->any_direction && b->any_direction)
1420 else if (a->any_direction && !b->any_direction)
1422 else if (!a->any_state && b->any_state)
1424 else if (a->any_state && !b->any_state)
1426 else if (!a->any_size && b->any_size)
1428 else if (a->any_size && !b->any_size)
1435 * gtk_icon_set_add_source:
1436 * @icon_set: a #GtkIconSet
1437 * @source: a #GtkIconSource
1439 * Icon sets have a list of #GtkIconSource, which they use as base
1440 * icons for rendering icons in different states and sizes. Icons are
1441 * scaled, made to look insensitive, etc. in
1442 * gtk_icon_set_render_icon(), but #GtkIconSet needs base images to
1443 * work with. The base images and when to use them are described by
1446 * This function copies @source, so you can reuse the same source immediately
1447 * without affecting the icon set.
1449 * An example of when you'd use this function: a web browser's "Back
1450 * to Previous Page" icon might point in a different direction in
1451 * Hebrew and in English; it might look different when insensitive;
1452 * and it might change size depending on toolbar mode (small/large
1453 * icons). So a single icon set would contain all those variants of
1454 * the icon, and you might add a separate source for each one.
1456 * You should nearly always add a "default" icon source with all
1457 * fields wildcarded, which will be used as a fallback if no more
1458 * specific source matches. #GtkIconSet always prefers more specific
1459 * icon sources to more generic icon sources. The order in which you
1460 * add the sources to the icon set does not matter.
1462 * gtk_icon_set_new_from_pixbuf() creates a new icon set with a
1463 * default icon source based on the given pixbuf.
1467 gtk_icon_set_add_source (GtkIconSet *icon_set,
1468 const GtkIconSource *source)
1470 g_return_if_fail (icon_set != NULL);
1471 g_return_if_fail (source != NULL);
1473 if (source->pixbuf == NULL &&
1474 source->filename == NULL)
1476 g_warning ("Useless GtkIconSource contains NULL filename and pixbuf");
1480 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
1481 gtk_icon_source_copy (source),
1482 icon_source_compare);
1486 * gtk_icon_set_get_sizes:
1487 * @icon_set: a #GtkIconSet
1488 * @sizes: return location for array of sizes
1489 * @n_sizes: location to store number of elements in returned array
1491 * Obtains a list of icon sizes this icon set can render. The returned
1492 * array must be freed with g_free().
1496 gtk_icon_set_get_sizes (GtkIconSet *icon_set,
1497 GtkIconSize **sizes,
1501 gboolean all_sizes = FALSE;
1502 GSList *specifics = NULL;
1504 g_return_if_fail (icon_set != NULL);
1505 g_return_if_fail (sizes != NULL);
1506 g_return_if_fail (n_sizes != NULL);
1508 tmp_list = icon_set->sources;
1509 while (tmp_list != NULL)
1511 GtkIconSource *source;
1513 source = tmp_list->data;
1515 if (source->any_size)
1521 specifics = g_slist_prepend (specifics, GINT_TO_POINTER (source->size));
1523 tmp_list = g_slist_next (tmp_list);
1528 /* Need to find out what sizes exist */
1533 *sizes = g_new (GtkIconSize, icon_sizes_used);
1534 *n_sizes = icon_sizes_used - 1;
1537 while (i < icon_sizes_used)
1539 (*sizes)[i - 1] = icon_sizes[i].size;
1547 *n_sizes = g_slist_length (specifics);
1548 *sizes = g_new (GtkIconSize, *n_sizes);
1551 tmp_list = specifics;
1552 while (tmp_list != NULL)
1554 (*sizes)[i] = GPOINTER_TO_INT (tmp_list->data);
1557 tmp_list = g_slist_next (tmp_list);
1561 g_slist_free (specifics);
1566 * gtk_icon_source_new:
1568 * Creates a new #GtkIconSource. A #GtkIconSource contains a #GdkPixbuf (or
1569 * image filename) that serves as the base image for one or more of the
1570 * icons in a #GtkIconSet, along with a specification for which icons in the
1571 * icon set will be based on that pixbuf or image file. An icon set contains
1572 * a set of icons that represent "the same" logical concept in different states,
1573 * different global text directions, and different sizes.
1575 * So for example a web browser's "Back to Previous Page" icon might
1576 * point in a different direction in Hebrew and in English; it might
1577 * look different when insensitive; and it might change size depending
1578 * on toolbar mode (small/large icons). So a single icon set would
1579 * contain all those variants of the icon. #GtkIconSet contains a list
1580 * of #GtkIconSource from which it can derive specific icon variants in
1583 * In the simplest case, #GtkIconSet contains one source pixbuf from
1584 * which it derives all variants. The convenience function
1585 * gtk_icon_set_new_from_pixbuf() handles this case; if you only have
1586 * one source pixbuf, just use that function.
1588 * If you want to use a different base pixbuf for different icon
1589 * variants, you create multiple icon sources, mark which variants
1590 * they'll be used to create, and add them to the icon set with
1591 * gtk_icon_set_add_source().
1593 * By default, the icon source has all parameters wildcarded. That is,
1594 * the icon source will be used as the base icon for any desired text
1595 * direction, widget state, or icon size.
1597 * Return value: a new #GtkIconSource
1600 gtk_icon_source_new (void)
1604 src = g_new0 (GtkIconSource, 1);
1606 src->direction = GTK_TEXT_DIR_NONE;
1607 src->size = GTK_ICON_SIZE_INVALID;
1608 src->state = GTK_STATE_NORMAL;
1610 src->any_direction = TRUE;
1611 src->any_state = TRUE;
1612 src->any_size = TRUE;
1618 * gtk_icon_source_copy:
1619 * @source: a #GtkIconSource
1621 * Creates a copy of @source; mostly useful for language bindings.
1623 * Return value: a new #GtkIconSource
1626 gtk_icon_source_copy (const GtkIconSource *source)
1628 GtkIconSource *copy;
1630 g_return_val_if_fail (source != NULL, NULL);
1632 copy = g_new (GtkIconSource, 1);
1636 copy->filename = g_strdup (source->filename);
1637 copy->size = source->size;
1639 g_object_ref (G_OBJECT (copy->pixbuf));
1645 * gtk_icon_source_free:
1646 * @source: a #GtkIconSource
1648 * Frees a dynamically-allocated icon source, along with its
1649 * filename, size, and pixbuf fields if those are not %NULL.
1652 gtk_icon_source_free (GtkIconSource *source)
1654 g_return_if_fail (source != NULL);
1656 g_free ((char*) source->filename);
1658 g_object_unref (G_OBJECT (source->pixbuf));
1664 * gtk_icon_source_set_filename:
1665 * @source: a #GtkIconSource
1666 * @filename: image file to use
1668 * Sets the name of an image file to use as a base image when creating
1669 * icon variants for #GtkIconSet. The filename must be absolute.
1672 gtk_icon_source_set_filename (GtkIconSource *source,
1673 const gchar *filename)
1675 g_return_if_fail (source != NULL);
1676 g_return_if_fail (filename == NULL || g_path_is_absolute (filename));
1678 if (source->filename == filename)
1681 if (source->filename)
1682 g_free (source->filename);
1684 source->filename = g_strdup (filename);
1688 * gtk_icon_source_set_pixbuf:
1689 * @source: a #GtkIconSource
1690 * @pixbuf: pixbuf to use as a source
1692 * Sets a pixbuf to use as a base image when creating icon variants
1693 * for #GtkIconSet. If an icon source has both a filename and a pixbuf
1694 * set, the pixbuf will take priority.
1698 gtk_icon_source_set_pixbuf (GtkIconSource *source,
1701 g_return_if_fail (source != NULL);
1704 g_object_ref (G_OBJECT (pixbuf));
1707 g_object_unref (G_OBJECT (source->pixbuf));
1709 source->pixbuf = pixbuf;
1713 * gtk_icon_source_get_filename:
1714 * @source: a #GtkIconSource
1716 * Retrieves the source filename, or %NULL if none is set. The
1717 * filename is not a copy, and should not be modified or expected to
1718 * persist beyond the lifetime of the icon source.
1720 * Return value: image filename
1722 G_CONST_RETURN gchar*
1723 gtk_icon_source_get_filename (const GtkIconSource *source)
1725 g_return_val_if_fail (source != NULL, NULL);
1727 return source->filename;
1731 * gtk_icon_source_get_pixbuf:
1732 * @source: a #GtkIconSource
1734 * Retrieves the source pixbuf, or %NULL if none is set.
1735 * The reference count on the pixbuf is not incremented.
1737 * Return value: source pixbuf
1740 gtk_icon_source_get_pixbuf (const GtkIconSource *source)
1742 g_return_val_if_fail (source != NULL, NULL);
1744 return source->pixbuf;
1748 * gtk_icon_source_set_direction_wildcarded:
1749 * @source: a #GtkIconSource
1750 * @setting: %TRUE to wildcard the text direction
1752 * If the text direction is wildcarded, this source can be used
1753 * as the base image for an icon in any #GtkTextDirection.
1754 * If the text direction is not wildcarded, then the
1755 * text direction the icon source applies to should be set
1756 * with gtk_icon_source_set_direction(), and the icon source
1757 * will only be used with that text direction.
1759 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
1760 * wildcarded sources, and will use an exact match when possible.
1764 gtk_icon_source_set_direction_wildcarded (GtkIconSource *source,
1767 g_return_if_fail (source != NULL);
1769 source->any_direction = setting != FALSE;
1773 * gtk_icon_source_set_state_wildcarded:
1774 * @source: a #GtkIconSource
1775 * @setting: %TRUE to wildcard the widget state
1777 * If the widget state is wildcarded, this source can be used as the
1778 * base image for an icon in any #GtkStateType. If the widget state
1779 * is not wildcarded, then the state the source applies to should be
1780 * set with gtk_icon_source_set_state() and the icon source will
1781 * only be used with that specific state.
1783 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
1784 * wildcarded sources, and will use an exact match when possible.
1786 * #GtkIconSet will normally transform wildcarded source images to
1787 * produce an appropriate icon for a given state, for example
1788 * lightening an image on prelight, but will not modify source images
1789 * that match exactly.
1792 gtk_icon_source_set_state_wildcarded (GtkIconSource *source,
1795 g_return_if_fail (source != NULL);
1797 source->any_state = setting != FALSE;
1802 * gtk_icon_source_set_size_wildcarded:
1803 * @source: a #GtkIconSource
1804 * @setting: %TRUE to wildcard the widget state
1806 * If the icon size is wildcarded, this source can be used as the base
1807 * image for an icon of any size. If the size is not wildcarded, then
1808 * the size the source applies to should be set with
1809 * gtk_icon_source_set_size() and the icon source will only be used
1810 * with that specific size.
1812 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
1813 * wildcarded sources, and will use an exact match when possible.
1815 * #GtkIconSet will normally scale wildcarded source images to produce
1816 * an appropriate icon at a given size, but will not change the size
1817 * of source images that match exactly.
1820 gtk_icon_source_set_size_wildcarded (GtkIconSource *source,
1823 g_return_if_fail (source != NULL);
1825 source->any_size = setting != FALSE;
1829 * gtk_icon_source_get_size_wildcarded:
1830 * @source: a #GtkIconSource
1832 * Gets the value set by gtk_icon_source_set_size_wildcarded().
1834 * Return value: %TRUE if this icon source is a base for any icon size variant
1837 gtk_icon_source_get_size_wildcarded (const GtkIconSource *source)
1839 g_return_val_if_fail (source != NULL, TRUE);
1841 return source->any_size;
1845 * gtk_icon_source_get_state_wildcarded:
1846 * @source: a #GtkIconSource
1848 * Gets the value set by gtk_icon_source_set_state_wildcarded().
1850 * Return value: %TRUE if this icon source is a base for any widget state variant
1853 gtk_icon_source_get_state_wildcarded (const GtkIconSource *source)
1855 g_return_val_if_fail (source != NULL, TRUE);
1857 return source->any_state;
1861 * gtk_icon_source_get_direction_wildcarded:
1862 * @source: a #GtkIconSource
1864 * Gets the value set by gtk_icon_source_set_direction_wildcarded().
1866 * Return value: %TRUE if this icon source is a base for any text direction variant
1869 gtk_icon_source_get_direction_wildcarded (const GtkIconSource *source)
1871 g_return_val_if_fail (source != NULL, TRUE);
1873 return source->any_direction;
1877 * gtk_icon_source_set_direction:
1878 * @source: a #GtkIconSource
1879 * @direction: text direction this source applies to
1881 * Sets the text direction this icon source is intended to be used
1884 * Setting the text direction on an icon source makes no difference
1885 * if the text direction is wildcarded. Therefore, you should usually
1886 * call gtk_icon_source_set_direction_wildcarded() to un-wildcard it
1887 * in addition to calling this function.
1891 gtk_icon_source_set_direction (GtkIconSource *source,
1892 GtkTextDirection direction)
1894 g_return_if_fail (source != NULL);
1896 source->direction = direction;
1900 * gtk_icon_source_set_state:
1901 * @source: a #GtkIconSource
1902 * @state: widget state this source applies to
1904 * Sets the widget state this icon source is intended to be used
1907 * Setting the widget state on an icon source makes no difference
1908 * if the state is wildcarded. Therefore, you should usually
1909 * call gtk_icon_source_set_state_wildcarded() to un-wildcard it
1910 * in addition to calling this function.
1914 gtk_icon_source_set_state (GtkIconSource *source,
1917 g_return_if_fail (source != NULL);
1919 source->state = state;
1923 * gtk_icon_source_set_size:
1924 * @source: a #GtkIconSource
1925 * @size: icon size this source applies to
1927 * Sets the icon size this icon source is intended to be used
1930 * Setting the icon size on an icon source makes no difference
1931 * if the size is wildcarded. Therefore, you should usually
1932 * call gtk_icon_source_set_size_wildcarded() to un-wildcard it
1933 * in addition to calling this function.
1937 gtk_icon_source_set_size (GtkIconSource *source,
1940 g_return_if_fail (source != NULL);
1942 source->size = size;
1946 * gtk_icon_source_get_direction:
1947 * @source: a #GtkIconSource
1949 * Obtains the text direction this icon source applies to. The return
1950 * value is only useful/meaningful if the text direction is <emphasis>not</emphasis>
1953 * Return value: text direction this source matches
1956 gtk_icon_source_get_direction (const GtkIconSource *source)
1958 g_return_val_if_fail (source != NULL, 0);
1960 return source->direction;
1964 * gtk_icon_source_get_state:
1965 * @source: a #GtkIconSource
1967 * Obtains the widget state this icon source applies to. The return
1968 * value is only useful/meaningful if the widget state is <emphasis>not</emphasis>
1971 * Return value: widget state this source matches
1974 gtk_icon_source_get_state (const GtkIconSource *source)
1976 g_return_val_if_fail (source != NULL, 0);
1978 return source->state;
1982 * gtk_icon_source_get_size:
1983 * @source: a #GtkIconSource
1985 * Obtains the icon size this source applies to. The return value
1986 * is only useful/meaningful if the icon size is <emphasis>not</emphasis> wildcarded.
1988 * Return value: icon size this source matches.
1991 gtk_icon_source_get_size (const GtkIconSource *source)
1993 g_return_val_if_fail (source != NULL, 0);
1995 return source->size;
1998 /* Note that the logical maximum is 20 per GtkTextDirection, so we could
1999 * eventually set this to >20 to never throw anything out.
2001 #define NUM_CACHED_ICONS 8
2003 typedef struct _CachedIcon CachedIcon;
2007 /* These must all match to use the cached pixbuf.
2008 * If any don't match, we must re-render the pixbuf.
2011 GtkTextDirection direction;
2019 ensure_cache_up_to_date (GtkIconSet *icon_set)
2021 if (icon_set->cache_serial != cache_serial)
2022 clear_cache (icon_set, TRUE);
2026 cached_icon_free (CachedIcon *icon)
2028 g_object_unref (G_OBJECT (icon->pixbuf));
2034 find_in_cache (GtkIconSet *icon_set,
2036 GtkTextDirection direction,
2043 ensure_cache_up_to_date (icon_set);
2046 tmp_list = icon_set->cache;
2047 while (tmp_list != NULL)
2049 CachedIcon *icon = tmp_list->data;
2051 if (icon->style == style &&
2052 icon->direction == direction &&
2053 icon->state == state &&
2058 /* Move this icon to the front of the list. */
2059 prev->next = tmp_list->next;
2060 tmp_list->next = icon_set->cache;
2061 icon_set->cache = tmp_list;
2064 return icon->pixbuf;
2068 tmp_list = g_slist_next (tmp_list);
2075 add_to_cache (GtkIconSet *icon_set,
2077 GtkTextDirection direction,
2084 ensure_cache_up_to_date (icon_set);
2086 g_object_ref (G_OBJECT (pixbuf));
2088 /* We have to ref the style, since if the style was finalized
2089 * its address could be reused by another style, creating a
2094 g_object_ref (G_OBJECT (style));
2097 icon = g_new (CachedIcon, 1);
2098 icon_set->cache = g_slist_prepend (icon_set->cache, icon);
2100 icon->style = style;
2101 icon->direction = direction;
2102 icon->state = state;
2104 icon->pixbuf = pixbuf;
2107 attach_to_style (icon_set, icon->style);
2109 if (icon_set->cache_size >= NUM_CACHED_ICONS)
2111 /* Remove oldest item in the cache */
2115 tmp_list = icon_set->cache;
2117 /* Find next-to-last link */
2118 g_assert (NUM_CACHED_ICONS > 2);
2119 while (tmp_list->next->next)
2120 tmp_list = tmp_list->next;
2122 g_assert (tmp_list != NULL);
2123 g_assert (tmp_list->next != NULL);
2124 g_assert (tmp_list->next->next == NULL);
2126 /* Free the last icon */
2127 icon = tmp_list->next->data;
2129 g_slist_free (tmp_list->next);
2130 tmp_list->next = NULL;
2132 cached_icon_free (icon);
2137 clear_cache (GtkIconSet *icon_set,
2138 gboolean style_detach)
2141 GtkStyle *last_style = NULL;
2143 tmp_list = icon_set->cache;
2144 while (tmp_list != NULL)
2146 CachedIcon *icon = tmp_list->data;
2150 /* simple optimization for the case where the cache
2151 * contains contiguous icons from the same style.
2152 * it's safe to call detach_from_style more than
2153 * once on the same style though.
2155 if (last_style != icon->style)
2157 detach_from_style (icon_set, icon->style);
2158 last_style = icon->style;
2162 cached_icon_free (icon);
2164 tmp_list = g_slist_next (tmp_list);
2167 g_slist_free (icon_set->cache);
2168 icon_set->cache = NULL;
2169 icon_set->cache_size = 0;
2173 copy_cache (GtkIconSet *icon_set,
2174 GtkIconSet *copy_recipient)
2177 GSList *copy = NULL;
2179 ensure_cache_up_to_date (icon_set);
2181 tmp_list = icon_set->cache;
2182 while (tmp_list != NULL)
2184 CachedIcon *icon = tmp_list->data;
2185 CachedIcon *icon_copy = g_new (CachedIcon, 1);
2189 if (icon_copy->style)
2190 attach_to_style (copy_recipient, icon_copy->style);
2192 g_object_ref (G_OBJECT (icon_copy->pixbuf));
2194 icon_copy->size = icon->size;
2196 copy = g_slist_prepend (copy, icon_copy);
2198 tmp_list = g_slist_next (tmp_list);
2201 return g_slist_reverse (copy);
2205 attach_to_style (GtkIconSet *icon_set,
2210 table = g_object_get_qdata (G_OBJECT (style),
2211 g_quark_try_string ("gtk-style-icon-sets"));
2215 table = g_hash_table_new (NULL, NULL);
2216 g_object_set_qdata_full (G_OBJECT (style),
2217 g_quark_from_static_string ("gtk-style-icon-sets"),
2222 g_hash_table_insert (table, icon_set, icon_set);
2226 detach_from_style (GtkIconSet *icon_set,
2231 table = g_object_get_qdata (G_OBJECT (style),
2232 g_quark_try_string ("gtk-style-icon-sets"));
2235 g_hash_table_remove (table, icon_set);
2239 iconsets_foreach (gpointer key,
2243 GtkIconSet *icon_set = key;
2245 /* We only need to remove cache entries for the given style;
2246 * but that complicates things because in destroy notify
2247 * we don't know which style got destroyed, and 95% of the
2248 * time all cache entries will have the same style,
2249 * so this is faster anyway.
2252 clear_cache (icon_set, FALSE);
2256 style_dnotify (gpointer data)
2258 GHashTable *table = data;
2260 g_hash_table_foreach (table, iconsets_foreach, NULL);
2262 g_hash_table_destroy (table);
2265 /* This allows the icon set to detect that its cache is out of date. */
2267 _gtk_icon_set_invalidate_caches (void)
2273 listify_foreach (gpointer key, gpointer value, gpointer data)
2275 GSList **list = data;
2277 *list = g_slist_prepend (*list, key);
2281 g_hash_table_get_keys (GHashTable *table)
2283 GSList *list = NULL;
2285 g_hash_table_foreach (table, listify_foreach, &list);
2291 * _gtk_icon_factory_list_ids:
2293 * Gets all known IDs stored in an existing icon factory.
2294 * The strings in the returned list aren't copied.
2295 * The list itself should be freed.
2297 * Return value: List of ids in icon factories
2300 _gtk_icon_factory_list_ids (void)
2307 ensure_default_icons ();
2309 tmp_list = all_icon_factories;
2310 while (tmp_list != NULL)
2314 GtkIconFactory *factory = GTK_ICON_FACTORY (tmp_list->data);
2316 these_ids = g_hash_table_get_keys (factory->icons);
2318 ids = g_slist_concat (ids, these_ids);
2320 tmp_list = g_slist_next (tmp_list);