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"
36 static GSList *all_icon_factories = NULL;
40 /* Either filename or pixbuf can be NULL. If both are non-NULL,
41 * the pixbuf is assumed to be the already-loaded contents of the
47 GtkTextDirection direction;
51 /* If TRUE, then the parameter is wildcarded, and the above
52 * fields should be ignored. If FALSE, the parameter is
53 * specified, and the above fields should be valid.
55 guint any_direction : 1;
60 /* FIXME use a better icon for this */
61 #define MISSING_IMAGE_INLINE dialog_error
63 static gpointer parent_class = NULL;
65 static void gtk_icon_factory_init (GtkIconFactory *icon_factory);
66 static void gtk_icon_factory_class_init (GtkIconFactoryClass *klass);
67 static void gtk_icon_factory_finalize (GObject *object);
68 static void get_default_icons (GtkIconFactory *icon_factory);
71 gtk_icon_factory_get_type (void)
73 static GType object_type = 0;
77 static const GTypeInfo object_info =
79 sizeof (GtkIconFactoryClass),
81 (GBaseFinalizeFunc) NULL,
82 (GClassInitFunc) gtk_icon_factory_class_init,
83 NULL, /* class_finalize */
84 NULL, /* class_data */
85 sizeof (GtkIconFactory),
87 (GInstanceInitFunc) gtk_icon_factory_init,
90 object_type = g_type_register_static (G_TYPE_OBJECT,
99 gtk_icon_factory_init (GtkIconFactory *factory)
101 factory->icons = g_hash_table_new (g_str_hash, g_str_equal);
102 all_icon_factories = g_slist_prepend (all_icon_factories, factory);
106 gtk_icon_factory_class_init (GtkIconFactoryClass *klass)
108 GObjectClass *object_class = G_OBJECT_CLASS (klass);
110 parent_class = g_type_class_peek_parent (klass);
112 object_class->finalize = gtk_icon_factory_finalize;
116 free_icon_set (gpointer key, gpointer value, gpointer data)
119 gtk_icon_set_unref (value);
123 gtk_icon_factory_finalize (GObject *object)
125 GtkIconFactory *factory = GTK_ICON_FACTORY (object);
127 all_icon_factories = g_slist_remove (all_icon_factories, factory);
129 g_hash_table_foreach (factory->icons, free_icon_set, NULL);
131 g_hash_table_destroy (factory->icons);
133 G_OBJECT_CLASS (parent_class)->finalize (object);
137 * gtk_icon_factory_new:
139 * Creates a new #GtkIconFactory. An icon factory manages a collection
140 * of #GtkIconSet; a #GtkIconSet manages a set of variants of a
141 * particular icon (i.e. a #GtkIconSet contains variants for different
142 * sizes and widget states). Icons in an icon factory are named by a
143 * stock ID, which is a simple string identifying the icon. Each
144 * #GtkStyle has a list of #GtkIconFactory derived from the current
145 * theme; those icon factories are consulted first when searching for
146 * an icon. If the theme doesn't set a particular icon, GTK+ looks for
147 * the icon in a list of default icon factories, maintained by
148 * gtk_icon_factory_add_default() and
149 * gtk_icon_factory_remove_default(). Applications with icons should
150 * add a default icon factory with their icons, which will allow
151 * themes to override the icons for the application.
153 * Return value: a new #GtkIconFactory
156 gtk_icon_factory_new (void)
158 return GTK_ICON_FACTORY (g_object_new (GTK_TYPE_ICON_FACTORY, NULL));
162 * gtk_icon_factory_add:
163 * @factory: a #GtkIconFactory
164 * @stock_id: icon name
165 * @icon_set: icon set
167 * Adds the given @icon_set to the icon factory, under the name
168 * @stock_id. @stock_id should be namespaced for your application,
169 * e.g. "myapp-whatever-icon". Normally applications create a
170 * #GtkIconFactory, then add it to the list of default factories with
171 * gtk_icon_factory_add_default(). Then they pass the @stock_id to
172 * widgets such as #GtkImage to display the icon. Themes can provide
173 * an icon with the same name (such as "myapp-whatever-icon") to
174 * override your application's default icons. If an icon already
175 * existed in @factory for @stock_id, it is unreferenced and replaced
176 * with the new @icon_set.
180 gtk_icon_factory_add (GtkIconFactory *factory,
181 const gchar *stock_id,
182 GtkIconSet *icon_set)
184 gpointer old_key = NULL;
185 gpointer old_value = NULL;
187 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
188 g_return_if_fail (stock_id != NULL);
189 g_return_if_fail (icon_set != NULL);
191 g_hash_table_lookup_extended (factory->icons, stock_id,
192 &old_key, &old_value);
194 if (old_value == icon_set)
197 gtk_icon_set_ref (icon_set);
199 /* GHashTable key memory management is so fantastically broken. */
201 g_hash_table_insert (factory->icons, old_key, icon_set);
203 g_hash_table_insert (factory->icons, g_strdup (stock_id), icon_set);
206 gtk_icon_set_unref (old_value);
210 * gtk_icon_factory_lookup:
211 * @factory: a #GtkIconFactory
212 * @stock_id: an icon name
214 * Looks up @stock_id in the icon factory, returning an icon set
215 * if found, otherwise %NULL. For display to the user, you should
216 * use gtk_style_lookup_icon_set() on the #GtkStyle for the
217 * widget that will display the icon, instead of using this
218 * function directly, so that themes are taken into account.
220 * Return value: icon set of @stock_id.
223 gtk_icon_factory_lookup (GtkIconFactory *factory,
224 const gchar *stock_id)
226 g_return_val_if_fail (GTK_IS_ICON_FACTORY (factory), NULL);
227 g_return_val_if_fail (stock_id != NULL, NULL);
229 return g_hash_table_lookup (factory->icons, stock_id);
232 static GtkIconFactory *gtk_default_icons = NULL;
233 static GSList *default_factories = NULL;
236 * gtk_icon_factory_add_default:
237 * @factory: a #GtkIconFactory
239 * Adds an icon factory to the list of icon factories searched by
240 * gtk_style_lookup_icon_set(). This means that, for example,
241 * gtk_image_new_from_stock() will be able to find icons in @factory.
242 * There will normally be an icon factory added for each library or
243 * application that comes with icons. The default icon factories
244 * can be overridden by themes.
248 gtk_icon_factory_add_default (GtkIconFactory *factory)
250 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
252 g_object_ref (G_OBJECT (factory));
254 default_factories = g_slist_prepend (default_factories, factory);
258 * gtk_icon_factory_remove_default:
259 * @factory: a #GtkIconFactory previously added with gtk_icon_factory_add_default()
261 * Removes an icon factory from the list of default icon
262 * factories. Not normally used; you might use it for a library that
263 * can be unloaded or shut down.
267 gtk_icon_factory_remove_default (GtkIconFactory *factory)
269 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
271 default_factories = g_slist_remove (default_factories, factory);
273 g_object_unref (G_OBJECT (factory));
277 ensure_default_icons (void)
279 if (gtk_default_icons == NULL)
281 gtk_default_icons = gtk_icon_factory_new ();
283 get_default_icons (gtk_default_icons);
288 * gtk_icon_factory_lookup_default:
289 * @stock_id: an icon name
291 * Looks for an icon in the list of default icon factories. For
292 * display to the user, you should use gtk_style_lookup_icon_set() on
293 * the #GtkStyle for the widget that will display the icon, instead of
294 * using this function directly, so that themes are taken into
298 * Return value: a #GtkIconSet, or %NULL
301 gtk_icon_factory_lookup_default (const gchar *stock_id)
305 g_return_val_if_fail (stock_id != NULL, NULL);
307 tmp_list = default_factories;
308 while (tmp_list != NULL)
310 GtkIconSet *icon_set =
311 gtk_icon_factory_lookup (GTK_ICON_FACTORY (tmp_list->data),
317 tmp_list = g_slist_next (tmp_list);
320 ensure_default_icons ();
322 return gtk_icon_factory_lookup (gtk_default_icons, stock_id);
326 sized_icon_set_from_inline (const guchar *inline_data,
331 GtkIconSource source = { NULL, NULL, 0, 0, 0,
336 set = gtk_icon_set_new ();
338 source.pixbuf = gdk_pixbuf_new_from_stream (-1, inline_data, FALSE, NULL);
340 g_assert (source.pixbuf);
342 gtk_icon_set_add_source (set, &source);
344 g_object_unref (G_OBJECT (source.pixbuf));
351 sized_with_fallback_icon_set_from_inline (const guchar *fallback_data,
352 const guchar *inline_data,
357 GtkIconSource source = { NULL, NULL, 0, 0, 0,
362 set = gtk_icon_set_new ();
364 source.pixbuf = gdk_pixbuf_new_from_stream (-1, inline_data, FALSE, NULL);
366 g_assert (source.pixbuf);
368 gtk_icon_set_add_source (set, &source);
370 g_object_unref (G_OBJECT (source.pixbuf));
372 source.any_size = TRUE;
374 source.pixbuf = gdk_pixbuf_new_from_stream (-1, fallback_data, FALSE, NULL);
376 g_assert (source.pixbuf);
378 gtk_icon_set_add_source (set, &source);
380 g_object_unref (G_OBJECT (source.pixbuf));
386 unsized_icon_set_from_inline (const guchar *inline_data)
390 /* This icon can be used for any direction/state/size */
391 GtkIconSource source = { NULL, NULL, 0, 0, 0,
394 set = gtk_icon_set_new ();
396 source.pixbuf = gdk_pixbuf_new_from_stream (-1, inline_data, FALSE, NULL);
398 g_assert (source.pixbuf);
400 gtk_icon_set_add_source (set, &source);
402 g_object_unref (G_OBJECT (source.pixbuf));
408 add_sized (GtkIconFactory *factory,
409 const guchar *inline_data,
411 const gchar *stock_id)
415 set = sized_icon_set_from_inline (inline_data, size);
417 gtk_icon_factory_add (factory, stock_id, set);
419 gtk_icon_set_unref (set);
423 add_sized_with_fallback (GtkIconFactory *factory,
424 const guchar *fallback_data,
425 const guchar *inline_data,
427 const gchar *stock_id)
431 set = sized_with_fallback_icon_set_from_inline (fallback_data, inline_data, size);
433 gtk_icon_factory_add (factory, stock_id, set);
435 gtk_icon_set_unref (set);
439 add_unsized (GtkIconFactory *factory,
440 const guchar *inline_data,
441 const gchar *stock_id)
445 set = unsized_icon_set_from_inline (inline_data);
447 gtk_icon_factory_add (factory, stock_id, set);
449 gtk_icon_set_unref (set);
453 get_default_icons (GtkIconFactory *factory)
455 /* KEEP IN SYNC with gtkstock.c */
457 add_unsized (factory, MISSING_IMAGE_INLINE, GTK_STOCK_MISSING_IMAGE);
459 add_sized (factory, dialog_error, GTK_ICON_SIZE_DIALOG, GTK_STOCK_DIALOG_ERROR);
460 add_sized (factory, dialog_info, GTK_ICON_SIZE_DIALOG, GTK_STOCK_DIALOG_INFO);
461 add_sized (factory, dialog_question, GTK_ICON_SIZE_DIALOG, GTK_STOCK_DIALOG_QUESTION);
462 add_sized (factory, dialog_warning, GTK_ICON_SIZE_DIALOG, GTK_STOCK_DIALOG_WARNING);
465 add_sized (factory, stock_new, GTK_ICON_SIZE_DND, GTK_STOCK_DND);
466 add_sized (factory, stock_dnd_multiple, GTK_ICON_SIZE_DND, GTK_STOCK_DND_MULTIPLE);
468 /* Only have button sizes */
469 add_sized (factory, stock_button_apply, GTK_ICON_SIZE_BUTTON, GTK_STOCK_APPLY);
470 add_sized (factory, stock_button_cancel, GTK_ICON_SIZE_BUTTON, GTK_STOCK_CANCEL);
471 add_sized (factory, stock_button_no, GTK_ICON_SIZE_BUTTON, GTK_STOCK_NO);
472 add_sized (factory, stock_button_ok, GTK_ICON_SIZE_BUTTON, GTK_STOCK_OK);
473 add_sized (factory, stock_button_yes, GTK_ICON_SIZE_BUTTON, GTK_STOCK_YES);
475 /* Generic + button sizes */
476 add_sized_with_fallback (factory,
479 GTK_ICON_SIZE_BUTTON,
482 /* Generic + menu sizes */
484 add_sized_with_fallback (factory,
486 stock_menu_print_preview,
488 GTK_STOCK_PRINT_PREVIEW);
490 add_sized_with_fallback (factory,
491 stock_sort_descending,
492 stock_menu_sort_descending,
494 GTK_STOCK_SORT_DESCENDING);
497 add_sized_with_fallback (factory,
498 stock_sort_ascending,
499 stock_menu_sort_ascending,
501 GTK_STOCK_SORT_ASCENDING);
503 /* Generic size only */
505 add_unsized (factory, stock_add, GTK_STOCK_ADD);
506 add_unsized (factory, stock_align_center, GTK_STOCK_JUSTIFY_CENTER);
507 add_unsized (factory, stock_align_justify, GTK_STOCK_JUSTIFY_FILL);
508 add_unsized (factory, stock_align_left, GTK_STOCK_JUSTIFY_LEFT);
509 add_unsized (factory, stock_align_right, GTK_STOCK_JUSTIFY_RIGHT);
510 add_unsized (factory, stock_bottom, GTK_STOCK_GOTO_BOTTOM);
511 add_unsized (factory, stock_cdrom, GTK_STOCK_CDROM);
512 add_unsized (factory, stock_clear, GTK_STOCK_CLEAR);
513 add_unsized (factory, stock_colorselector, GTK_STOCK_SELECT_COLOR);
514 add_unsized (factory, stock_convert, GTK_STOCK_CONVERT);
515 add_unsized (factory, stock_copy, GTK_STOCK_COPY);
516 add_unsized (factory, stock_cut, GTK_STOCK_CUT);
517 add_unsized (factory, stock_down_arrow, GTK_STOCK_GO_DOWN);
518 add_unsized (factory, stock_exec, GTK_STOCK_EXECUTE);
519 add_unsized (factory, stock_exit, GTK_STOCK_QUIT);
520 add_unsized (factory, stock_first, GTK_STOCK_GOTO_FIRST);
521 add_unsized (factory, stock_font, GTK_STOCK_SELECT_FONT);
522 add_unsized (factory, stock_help, GTK_STOCK_HELP);
523 add_unsized (factory, stock_home, GTK_STOCK_HOME);
524 add_unsized (factory, stock_index, GTK_STOCK_INDEX);
525 add_unsized (factory, stock_jump_to, GTK_STOCK_JUMP_TO);
526 add_unsized (factory, stock_last, GTK_STOCK_GOTO_LAST);
527 add_unsized (factory, stock_left_arrow, GTK_STOCK_GO_BACK);
528 add_unsized (factory, stock_new, GTK_STOCK_NEW);
529 add_unsized (factory, stock_open, GTK_STOCK_OPEN);
530 add_unsized (factory, stock_paste, GTK_STOCK_PASTE);
531 add_unsized (factory, stock_preferences, GTK_STOCK_PREFERENCES);
532 add_unsized (factory, stock_print, GTK_STOCK_PRINT);
533 add_unsized (factory, stock_properties, GTK_STOCK_PROPERTIES);
534 add_unsized (factory, stock_redo, GTK_STOCK_REDO);
535 add_unsized (factory, stock_refresh, GTK_STOCK_REFRESH);
536 add_unsized (factory, stock_remove, GTK_STOCK_REMOVE);
537 add_unsized (factory, stock_revert, GTK_STOCK_REVERT_TO_SAVED);
538 add_unsized (factory, stock_right_arrow, GTK_STOCK_GO_FORWARD);
539 add_unsized (factory, stock_save, GTK_STOCK_FLOPPY);
540 add_unsized (factory, stock_save, GTK_STOCK_SAVE);
541 add_unsized (factory, stock_save_as, GTK_STOCK_SAVE_AS);
542 add_unsized (factory, stock_search, GTK_STOCK_FIND);
543 add_unsized (factory, stock_search_replace, GTK_STOCK_FIND_AND_REPLACE);
544 add_unsized (factory, stock_spellcheck, GTK_STOCK_SPELL_CHECK);
545 add_unsized (factory, stock_stop, GTK_STOCK_STOP);
546 add_unsized (factory, stock_text_bold, GTK_STOCK_BOLD);
547 add_unsized (factory, stock_text_italic, GTK_STOCK_ITALIC);
548 add_unsized (factory, stock_text_strikeout, GTK_STOCK_STRIKETHROUGH);
549 add_unsized (factory, stock_text_underline, GTK_STOCK_UNDERLINE);
550 add_unsized (factory, stock_top, GTK_STOCK_GOTO_TOP);
551 add_unsized (factory, stock_trash, GTK_STOCK_DELETE);
552 add_unsized (factory, stock_undelete, GTK_STOCK_UNDELETE);
553 add_unsized (factory, stock_undo, GTK_STOCK_UNDO);
554 add_unsized (factory, stock_up_arrow, GTK_STOCK_GO_UP);
555 add_unsized (factory, stock_zoom_1, GTK_STOCK_ZOOM_100);
556 add_unsized (factory, stock_zoom_fit, GTK_STOCK_ZOOM_FIT);
557 add_unsized (factory, stock_zoom_in, GTK_STOCK_ZOOM_IN);
558 add_unsized (factory, stock_zoom_out, GTK_STOCK_ZOOM_OUT);
563 typedef struct _IconSize IconSize;
574 typedef struct _IconAlias IconAlias;
582 static GHashTable *icon_aliases = NULL;
583 static IconSize *icon_sizes = NULL;
584 static gint icon_sizes_allocated = 0;
585 static gint icon_sizes_used = 0;
588 init_icon_sizes (void)
590 if (icon_sizes == NULL)
592 #define NUM_BUILTIN_SIZES 7
595 icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
597 icon_sizes = g_new (IconSize, NUM_BUILTIN_SIZES);
598 icon_sizes_allocated = NUM_BUILTIN_SIZES;
599 icon_sizes_used = NUM_BUILTIN_SIZES;
601 icon_sizes[GTK_ICON_SIZE_INVALID].size = 0;
602 icon_sizes[GTK_ICON_SIZE_INVALID].name = NULL;
603 icon_sizes[GTK_ICON_SIZE_INVALID].width = 0;
604 icon_sizes[GTK_ICON_SIZE_INVALID].height = 0;
606 /* the name strings aren't copied since we don't ever remove
607 * icon sizes, so we don't need to know whether they're static.
608 * Even if we did I suppose removing the builtin sizes would be
612 icon_sizes[GTK_ICON_SIZE_MENU].size = GTK_ICON_SIZE_MENU;
613 icon_sizes[GTK_ICON_SIZE_MENU].name = "gtk-menu";
614 icon_sizes[GTK_ICON_SIZE_MENU].width = 16;
615 icon_sizes[GTK_ICON_SIZE_MENU].height = 16;
617 icon_sizes[GTK_ICON_SIZE_BUTTON].size = GTK_ICON_SIZE_BUTTON;
618 icon_sizes[GTK_ICON_SIZE_BUTTON].name = "gtk-button";
619 icon_sizes[GTK_ICON_SIZE_BUTTON].width = 24;
620 icon_sizes[GTK_ICON_SIZE_BUTTON].height = 24;
622 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].size = GTK_ICON_SIZE_SMALL_TOOLBAR;
623 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].name = "gtk-small-toolbar";
624 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].width = 18;
625 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].height = 18;
627 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].size = GTK_ICON_SIZE_LARGE_TOOLBAR;
628 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].name = "gtk-large-toolbar";
629 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].width = 24;
630 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].height = 24;
632 icon_sizes[GTK_ICON_SIZE_DND].size = GTK_ICON_SIZE_DND;
633 icon_sizes[GTK_ICON_SIZE_DND].name = "gtk-dnd";
634 icon_sizes[GTK_ICON_SIZE_DND].width = 32;
635 icon_sizes[GTK_ICON_SIZE_DND].height = 32;
637 icon_sizes[GTK_ICON_SIZE_DIALOG].size = GTK_ICON_SIZE_DIALOG;
638 icon_sizes[GTK_ICON_SIZE_DIALOG].name = "gtk-dialog";
639 icon_sizes[GTK_ICON_SIZE_DIALOG].width = 48;
640 icon_sizes[GTK_ICON_SIZE_DIALOG].height = 48;
642 g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
644 /* Alias everything to itself. */
645 i = 1; /* skip invalid size */
646 while (i < NUM_BUILTIN_SIZES)
648 gtk_icon_size_register_alias (icon_sizes[i].name, icon_sizes[i].size);
653 #undef NUM_BUILTIN_SIZES
658 * gtk_icon_size_lookup:
659 * @size: an icon size
660 * @width: location to store icon width
661 * @height: location to store icon height
663 * Obtains the pixel size of a semantic icon size, normally @size would be
664 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
665 * isn't normally needed, gtk_widget_render_icon() is the usual
666 * way to get an icon for rendering, then just look at the size of
667 * the rendered pixbuf. The rendered pixbuf may not even correspond to
668 * the width/height returned by gtk_icon_size_lookup(), because themes
669 * are free to render the pixbuf however they like, including changing
672 * Return value: %TRUE if @size was a valid size
675 gtk_icon_size_lookup (GtkIconSize size,
681 if (size >= icon_sizes_used)
684 if (size == GTK_ICON_SIZE_INVALID)
688 *widthp = icon_sizes[size].width;
691 *heightp = icon_sizes[size].height;
697 * gtk_icon_size_register:
698 * @name: name of the icon size
699 * @width: the icon width
700 * @height: the icon height
702 * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
703 * etc. Returns the integer value for the size.
705 * Returns: integer value representing the size
709 gtk_icon_size_register (const gchar *name,
713 g_return_val_if_fail (name != NULL, 0);
714 g_return_val_if_fail (width > 0, 0);
715 g_return_val_if_fail (height > 0, 0);
719 if (icon_sizes_used == icon_sizes_allocated)
721 icon_sizes_allocated *= 2;
722 icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated);
725 icon_sizes[icon_sizes_used].size = icon_sizes_used;
726 icon_sizes[icon_sizes_used].name = g_strdup (name);
727 icon_sizes[icon_sizes_used].width = width;
728 icon_sizes[icon_sizes_used].height = height;
733 gtk_icon_size_register_alias (name, icon_sizes_used - 1);
735 return icon_sizes_used - 1;
739 * gtk_icon_size_register_alias:
740 * @alias: an alias for @target
741 * @target: an existing icon size
743 * Registers @alias as another name for @target.
744 * So calling gtk_icon_size_from_name() with @alias as argument
745 * will return @target.
749 gtk_icon_size_register_alias (const gchar *alias,
754 g_return_if_fail (alias != NULL);
758 if (g_hash_table_lookup (icon_aliases, alias))
759 g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias);
761 if (!gtk_icon_size_lookup (target, NULL, NULL))
762 g_warning ("gtk_icon_size_register_alias: Icon size %d does not exist", target);
764 ia = g_new (IconAlias, 1);
765 ia->name = g_strdup (alias);
768 g_hash_table_insert (icon_aliases, ia->name, ia);
772 gtk_icon_size_from_name (const gchar *name)
778 ia = g_hash_table_lookup (icon_aliases, name);
783 return GTK_ICON_SIZE_INVALID;
786 G_CONST_RETURN gchar*
787 gtk_icon_size_get_name (GtkIconSize size)
789 if (size >= icon_sizes_used)
792 return icon_sizes[size].name;
798 /* Clear icon set contents, drop references to all contained
799 * GdkPixbuf objects and forget all GtkIconSources. Used to
800 * recycle an icon set.
802 static GdkPixbuf *find_in_cache (GtkIconSet *icon_set,
804 GtkTextDirection direction,
807 static void add_to_cache (GtkIconSet *icon_set,
809 GtkTextDirection direction,
813 static void clear_cache (GtkIconSet *icon_set,
814 gboolean style_detach);
815 static GSList* copy_cache (GtkIconSet *icon_set,
816 GtkIconSet *copy_recipient);
817 static void attach_to_style (GtkIconSet *icon_set,
819 static void detach_from_style (GtkIconSet *icon_set,
821 static void style_dnotify (gpointer data);
829 /* Cache of the last few rendered versions of the icon. */
837 static guint cache_serial = 0;
842 * Creates a new #GtkIconSet. A #GtkIconSet represents a single icon
843 * in various sizes and widget states. It can provide a #GdkPixbuf
844 * for a given size and state on request, and automatically caches
845 * some of the rendered #GdkPixbuf objects.
847 * Normally you would use gtk_widget_render_icon() instead of
848 * using #GtkIconSet directly. The one case where you'd use
849 * #GtkIconSet is to create application-specific icon sets to place in
852 * Return value: a new #GtkIconSet
855 gtk_icon_set_new (void)
857 GtkIconSet *icon_set;
859 icon_set = g_new (GtkIconSet, 1);
861 icon_set->ref_count = 1;
862 icon_set->sources = NULL;
863 icon_set->cache = NULL;
864 icon_set->cache_size = 0;
865 icon_set->cache_serial = cache_serial;
871 * gtk_icon_set_new_from_pixbuf:
872 * @pixbuf: a #GdkPixbuf
874 * Creates a new #GtkIconSet with @pixbuf as the default/fallback
875 * source image. If you don't add any additional #GtkIconSource to the
876 * icon set, all variants of the icon will be created from @pixbuf,
877 * using scaling, pixelation, etc. as required to adjust the icon size
878 * or make the icon look insensitive/prelighted.
880 * Return value: a new #GtkIconSet
883 gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
887 GtkIconSource source = { NULL, NULL, 0, 0, 0,
890 g_return_val_if_fail (pixbuf != NULL, NULL);
892 set = gtk_icon_set_new ();
894 source.pixbuf = pixbuf;
896 gtk_icon_set_add_source (set, &source);
904 * @icon_set: a #GtkIconSet
906 * Increments the reference count on @icon_set
908 * Return value: @icon_set is returned
911 gtk_icon_set_ref (GtkIconSet *icon_set)
913 g_return_val_if_fail (icon_set != NULL, NULL);
914 g_return_val_if_fail (icon_set->ref_count > 0, NULL);
916 icon_set->ref_count += 1;
922 * gtk_icon_set_unref:
923 * @icon_set: a #GtkIconSet
925 * Decrements the reference count on @icon_set, and frees memory
926 * if the reference count reaches 0.
929 gtk_icon_set_unref (GtkIconSet *icon_set)
931 g_return_if_fail (icon_set != NULL);
932 g_return_if_fail (icon_set->ref_count > 0);
934 icon_set->ref_count -= 1;
936 if (icon_set->ref_count == 0)
938 GSList *tmp_list = icon_set->sources;
939 while (tmp_list != NULL)
941 gtk_icon_source_free (tmp_list->data);
943 tmp_list = g_slist_next (tmp_list);
946 clear_cache (icon_set, TRUE);
954 * @icon_set: a #GtkIconSet
956 * Copies @icon_set by value.
958 * Return value: a new #GtkIconSet identical to the first.
961 gtk_icon_set_copy (GtkIconSet *icon_set)
966 copy = gtk_icon_set_new ();
968 tmp_list = icon_set->sources;
969 while (tmp_list != NULL)
971 copy->sources = g_slist_prepend (copy->sources,
972 gtk_icon_source_copy (tmp_list->data));
974 tmp_list = g_slist_next (tmp_list);
977 copy->sources = g_slist_reverse (copy->sources);
979 copy->cache = copy_cache (icon_set, copy);
980 copy->cache_size = icon_set->cache_size;
981 copy->cache_serial = icon_set->cache_serial;
988 sizes_equivalent (GtkIconSize lhs,
991 gint r_w, r_h, l_w, l_h;
993 gtk_icon_size_lookup (rhs, &r_w, &r_h);
994 gtk_icon_size_lookup (lhs, &l_w, &l_h);
996 return r_w == l_w && r_h == l_h;
999 static GtkIconSource*
1000 find_and_prep_icon_source (GtkIconSet *icon_set,
1001 GtkTextDirection direction,
1005 GtkIconSource *source;
1009 /* We need to find the best icon source. Direction matters more
1010 * than state, state matters more than size. icon_set->sources
1011 * is sorted according to wildness, so if we take the first
1012 * match we find it will be the least-wild match (if there are
1013 * multiple matches for a given "wildness" then the RC file contained
1014 * dumb stuff, and we end up with an arbitrary matching source)
1018 tmp_list = icon_set->sources;
1019 while (tmp_list != NULL)
1021 GtkIconSource *s = tmp_list->data;
1023 if ((s->any_direction || (s->direction == direction)) &&
1024 (s->any_state || (s->state == state)) &&
1025 (s->any_size || (sizes_equivalent (size, s->size))))
1031 tmp_list = g_slist_next (tmp_list);
1037 if (source->pixbuf == NULL)
1039 GError *error = NULL;
1041 g_assert (source->filename);
1042 source->pixbuf = gdk_pixbuf_new_from_file (source->filename, &error);
1044 if (source->pixbuf == NULL)
1046 /* Remove this icon source so we don't keep trying to
1049 g_warning (_("Error loading icon: %s"), error->message);
1050 g_error_free (error);
1052 icon_set->sources = g_slist_remove (icon_set->sources, source);
1054 gtk_icon_source_free (source);
1056 /* Try to fall back to other sources */
1057 if (icon_set->sources != NULL)
1058 return find_and_prep_icon_source (icon_set,
1071 render_fallback_image (GtkStyle *style,
1072 GtkTextDirection direction,
1078 /* This icon can be used for any direction/state/size */
1079 static GtkIconSource fallback_source = { NULL, NULL, 0, 0, 0, TRUE, TRUE, TRUE };
1081 if (fallback_source.pixbuf == NULL)
1082 fallback_source.pixbuf = gdk_pixbuf_new_from_stream (-1, MISSING_IMAGE_INLINE, FALSE, NULL);
1084 return gtk_style_render_icon (style,
1094 * gtk_icon_set_render_icon:
1095 * @icon_set: a #GtkIconSet
1096 * @style: a #GtkStyle associated with @widget, or %NULL
1097 * @direction: text direction
1098 * @state: widget state
1100 * @widget: widget that will display the icon, or %NULL
1101 * @detail: detail to pass to the theme engine, or %NULL
1103 * Renders an icon using gtk_style_render_icon(). In most cases,
1104 * gtk_widget_render_icon() is better, since it automatically provides
1105 * most of the arguments from the current widget settings. This
1106 * function never returns %NULL; if the icon can't be rendered
1107 * (perhaps because an image file fails to load), a default "missing
1108 * image" icon will be returned instead.
1110 * Return value: a #GdkPixbuf to be displayed
1113 gtk_icon_set_render_icon (GtkIconSet *icon_set,
1115 GtkTextDirection direction,
1122 GtkIconSource *source;
1124 g_return_val_if_fail (icon_set != NULL, NULL);
1125 g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
1127 if (icon_set->sources == NULL)
1128 return render_fallback_image (style, direction, state, size, widget, detail);
1130 icon = find_in_cache (icon_set, style, direction,
1135 g_object_ref (G_OBJECT (icon));
1140 source = find_and_prep_icon_source (icon_set, direction, state, size);
1143 return render_fallback_image (style, direction, state, size, widget, detail);
1145 g_assert (source->pixbuf != NULL);
1147 icon = gtk_style_render_icon (style,
1157 g_warning ("Theme engine failed to render icon");
1161 add_to_cache (icon_set, style, direction, state, size, icon);
1166 /* Order sources by their "wildness", so that "wilder" sources are
1167 * greater than "specific" sources; for determining ordering,
1168 * direction beats state beats size.
1172 icon_source_compare (gconstpointer ap, gconstpointer bp)
1174 const GtkIconSource *a = ap;
1175 const GtkIconSource *b = bp;
1177 if (!a->any_direction && b->any_direction)
1179 else if (a->any_direction && !b->any_direction)
1181 else if (!a->any_state && b->any_state)
1183 else if (a->any_state && !b->any_state)
1185 else if (!a->any_size && b->any_size)
1187 else if (a->any_size && !b->any_size)
1194 * gtk_icon_set_add_source:
1195 * @icon_set: a #GtkIconSet
1196 * @source: a #GtkIconSource
1198 * Icon sets have a list of #GtkIconSource, which they use as base
1199 * icons for rendering icons in different states and sizes. Icons are
1200 * scaled, made to look insensitive, etc. in
1201 * gtk_icon_set_render_icon(), but #GtkIconSet needs base images to
1202 * work with. The base images and when to use them are described by
1205 * This function copies @source, so you can reuse the same source immediately
1206 * without affecting the icon set.
1208 * An example of when you'd use this function: a web browser's "Back
1209 * to Previous Page" icon might point in a different direction in
1210 * Hebrew and in English; it might look different when insensitive;
1211 * and it might change size depending on toolbar mode (small/large
1212 * icons). So a single icon set would contain all those variants of
1213 * the icon, and you might add a separate source for each one.
1215 * You should nearly always add a "default" icon source with all
1216 * fields wildcarded, which will be used as a fallback if no more
1217 * specific source matches. #GtkIconSet always prefers more specific
1218 * icon sources to more generic icon sources. The order in which you
1219 * add the sources to the icon set does not matter.
1221 * gtk_icon_set_new_from_pixbuf() creates a new icon set with a
1222 * default icon source based on the given pixbuf.
1226 gtk_icon_set_add_source (GtkIconSet *icon_set,
1227 const GtkIconSource *source)
1229 g_return_if_fail (icon_set != NULL);
1230 g_return_if_fail (source != NULL);
1232 if (source->pixbuf == NULL &&
1233 source->filename == NULL)
1235 g_warning ("Useless GtkIconSource contains NULL filename and pixbuf");
1239 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
1240 gtk_icon_source_copy (source),
1241 icon_source_compare);
1245 * gtk_icon_set_get_sizes:
1246 * @icon_set: a #GtkIconSet
1247 * @sizes: return location for array of sizes
1248 * @n_sizes: location to store number of elements in returned array
1250 * Obtains a list of icon sizes this icon set can render. The returned
1251 * array must be freed with g_free().
1255 gtk_icon_set_get_sizes (GtkIconSet *icon_set,
1256 GtkIconSize **sizes,
1260 gboolean all_sizes = FALSE;
1261 GSList *specifics = NULL;
1263 g_return_if_fail (icon_set != NULL);
1264 g_return_if_fail (sizes != NULL);
1265 g_return_if_fail (n_sizes != NULL);
1267 tmp_list = icon_set->sources;
1268 while (tmp_list != NULL)
1270 GtkIconSource *source;
1272 source = tmp_list->data;
1274 if (source->any_size)
1280 specifics = g_slist_prepend (specifics, GINT_TO_POINTER (source->size));
1282 tmp_list = g_slist_next (tmp_list);
1287 /* Need to find out what sizes exist */
1292 *sizes = g_new (GtkIconSize, icon_sizes_used);
1293 *n_sizes = icon_sizes_used;
1296 while (i < icon_sizes_used)
1298 (*sizes)[i] = icon_sizes[i].size;
1306 *n_sizes = g_slist_length (specifics);
1307 *sizes = g_new (GtkIconSize, *n_sizes);
1310 tmp_list = specifics;
1311 while (tmp_list != NULL)
1313 (*sizes)[i] = GPOINTER_TO_INT (tmp_list->data);
1316 tmp_list = g_slist_next (tmp_list);
1320 g_slist_free (specifics);
1325 * gtk_icon_source_new:
1327 * Creates a new #GtkIconSource. A #GtkIconSource contains a #GdkPixbuf (or
1328 * image filename) that serves as the base image for one or more of the
1329 * icons in a #GtkIconSet, along with a specification for which icons in the
1330 * icon set will be based on that pixbuf or image file. An icon set contains
1331 * a set of icons that represent "the same" logical concept in different states,
1332 * different global text directions, and different sizes.
1334 * So for example a web browser's "Back to Previous Page" icon might
1335 * point in a different direction in Hebrew and in English; it might
1336 * look different when insensitive; and it might change size depending
1337 * on toolbar mode (small/large icons). So a single icon set would
1338 * contain all those variants of the icon. #GtkIconSet contains a list
1339 * of #GtkIconSource from which it can derive specific icon variants in
1342 * In the simplest case, #GtkIconSet contains one source pixbuf from
1343 * which it derives all variants. The convenience function
1344 * gtk_icon_set_new_from_pixbuf() handles this case; if you only have
1345 * one source pixbuf, just use that function.
1347 * If you want to use a different base pixbuf for different icon
1348 * variants, you create multiple icon sources, mark which variants
1349 * they'll be used to create, and add them to the icon set with
1350 * gtk_icon_set_add_source().
1352 * By default, the icon source has all parameters wildcarded. That is,
1353 * the icon source will be used as the base icon for any desired text
1354 * direction, widget state, or icon size.
1356 * Return value: a new #GtkIconSource
1359 gtk_icon_source_new (void)
1363 src = g_new0 (GtkIconSource, 1);
1365 src->direction = GTK_TEXT_DIR_NONE;
1366 src->size = GTK_ICON_SIZE_INVALID;
1367 src->state = GTK_STATE_NORMAL;
1369 src->any_direction = TRUE;
1370 src->any_state = TRUE;
1371 src->any_size = TRUE;
1377 * gtk_icon_source_copy:
1378 * @source: a #GtkIconSource
1380 * Creates a copy of @source; mostly useful for language bindings.
1382 * Return value: a new #GtkIconSource
1385 gtk_icon_source_copy (const GtkIconSource *source)
1387 GtkIconSource *copy;
1389 g_return_val_if_fail (source != NULL, NULL);
1391 copy = g_new (GtkIconSource, 1);
1395 copy->filename = g_strdup (source->filename);
1396 copy->size = source->size;
1398 g_object_ref (G_OBJECT (copy->pixbuf));
1404 * gtk_icon_source_free:
1405 * @source: a #GtkIconSource
1407 * Frees a dynamically-allocated icon source, along with its
1408 * filename, size, and pixbuf fields if those are not %NULL.
1411 gtk_icon_source_free (GtkIconSource *source)
1413 g_return_if_fail (source != NULL);
1415 g_free ((char*) source->filename);
1417 g_object_unref (G_OBJECT (source->pixbuf));
1423 * gtk_icon_source_set_filename:
1424 * @source: a #GtkIconSource
1425 * @filename: image file to use
1427 * Sets the name of an image file to use as a base image when creating
1428 * icon variants for #GtkIconSet. The filename must be absolute.
1431 gtk_icon_source_set_filename (GtkIconSource *source,
1432 const gchar *filename)
1434 g_return_if_fail (source != NULL);
1435 g_return_if_fail (filename == NULL || g_path_is_absolute (filename));
1437 if (source->filename == filename)
1440 if (source->filename)
1441 g_free (source->filename);
1443 source->filename = g_strdup (filename);
1447 * gtk_icon_source_set_pixbuf:
1448 * @source: a #GtkIconSource
1449 * @pixbuf: pixbuf to use as a source
1451 * Sets a pixbuf to use as a base image when creating icon variants
1452 * for #GtkIconSet. If an icon source has both a filename and a pixbuf
1453 * set, the pixbuf will take priority.
1457 gtk_icon_source_set_pixbuf (GtkIconSource *source,
1460 g_return_if_fail (source != NULL);
1463 g_object_ref (G_OBJECT (pixbuf));
1466 g_object_unref (G_OBJECT (source->pixbuf));
1468 source->pixbuf = pixbuf;
1472 * gtk_icon_source_get_filename:
1473 * @source: a #GtkIconSource
1475 * Retrieves the source filename, or %NULL if none is set. The
1476 * filename is not a copy, and should not be modified or expected to
1477 * persist beyond the lifetime of the icon source.
1479 * Return value: image filename
1481 G_CONST_RETURN gchar*
1482 gtk_icon_source_get_filename (const GtkIconSource *source)
1484 g_return_val_if_fail (source != NULL, NULL);
1486 return source->filename;
1490 * gtk_icon_source_get_pixbuf:
1491 * @source: a #GtkIconSource
1493 * Retrieves the source pixbuf, or %NULL if none is set.
1494 * The reference count on the pixbuf is not incremented.
1496 * Return value: source pixbuf
1499 gtk_icon_source_get_pixbuf (const GtkIconSource *source)
1501 g_return_val_if_fail (source != NULL, NULL);
1503 return source->pixbuf;
1507 * gtk_icon_source_set_direction_wildcarded:
1508 * @source: a #GtkIconSource
1509 * @setting: %TRUE to wildcard the text direction
1511 * If the text direction is wildcarded, this source can be used
1512 * as the base image for an icon in any #GtkTextDirection.
1513 * If the text direction is not wildcarded, then the
1514 * text direction the icon source applies to should be set
1515 * with gtk_icon_source_set_direction(), and the icon source
1516 * will only be used with that text direction.
1518 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
1519 * wildcarded sources, and will use an exact match when possible.
1523 gtk_icon_source_set_direction_wildcarded (GtkIconSource *source,
1526 g_return_if_fail (source != NULL);
1528 source->any_direction = setting != FALSE;
1532 * gtk_icon_source_set_state_wildcarded:
1533 * @source: a #GtkIconSource
1534 * @setting: %TRUE to wildcard the widget state
1536 * If the widget state is wildcarded, this source can be used as the
1537 * base image for an icon in any #GtkStateType. If the widget state
1538 * is not wildcarded, then the state the source applies to should be
1539 * set with gtk_icon_source_set_state() and the icon source will
1540 * only be used with that specific state.
1542 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
1543 * wildcarded sources, and will use an exact match when possible.
1545 * #GtkIconSet will normally transform wildcarded source images to
1546 * produce an appropriate icon for a given state, for example
1547 * lightening an image on prelight, but will not modify source images
1548 * that match exactly.
1551 gtk_icon_source_set_state_wildcarded (GtkIconSource *source,
1554 g_return_if_fail (source != NULL);
1556 source->any_state = setting != FALSE;
1561 * gtk_icon_source_set_size_wildcarded:
1562 * @source: a #GtkIconSource
1563 * @setting: %TRUE to wildcard the widget state
1565 * If the icon size is wildcarded, this source can be used as the base
1566 * image for an icon of any size. If the size is not wildcarded, then
1567 * the size the source applies to should be set with
1568 * gtk_icon_source_set_size() and the icon source will only be used
1569 * with that specific size.
1571 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
1572 * wildcarded sources, and will use an exact match when possible.
1574 * #GtkIconSet will normally scale wildcarded source images to produce
1575 * an appropriate icon at a given size, but will not change the size
1576 * of source images that match exactly.
1579 gtk_icon_source_set_size_wildcarded (GtkIconSource *source,
1582 g_return_if_fail (source != NULL);
1584 source->any_size = setting != FALSE;
1588 * gtk_icon_source_get_size_wildcarded:
1589 * @source: a #GtkIconSource
1591 * Gets the value set by gtk_icon_source_set_size_wildcarded().
1593 * Return value: %TRUE if this icon source is a base for any icon size variant
1596 gtk_icon_source_get_size_wildcarded (const GtkIconSource *source)
1598 g_return_val_if_fail (source != NULL, TRUE);
1600 return source->any_size;
1604 * gtk_icon_source_get_state_wildcarded:
1605 * @source: a #GtkIconSource
1607 * Gets the value set by gtk_icon_source_set_state_wildcarded().
1609 * Return value: %TRUE if this icon source is a base for any widget state variant
1612 gtk_icon_source_get_state_wildcarded (const GtkIconSource *source)
1614 g_return_val_if_fail (source != NULL, TRUE);
1616 return source->any_state;
1620 * gtk_icon_source_get_direction_wildcarded:
1621 * @source: a #GtkIconSource
1623 * Gets the value set by gtk_icon_source_set_direction_wildcarded().
1625 * Return value: %TRUE if this icon source is a base for any text direction variant
1628 gtk_icon_source_get_direction_wildcarded (const GtkIconSource *source)
1630 g_return_val_if_fail (source != NULL, TRUE);
1632 return source->any_direction;
1636 * gtk_icon_source_set_direction:
1637 * @source: a #GtkIconSource
1638 * @direction: text direction this source applies to
1640 * Sets the text direction this icon source is intended to be used
1643 * Setting the text direction on an icon source makes no difference
1644 * if the text direction is wildcarded. Therefore, you should usually
1645 * call gtk_icon_source_set_direction_wildcarded() to un-wildcard it
1646 * in addition to calling this function.
1650 gtk_icon_source_set_direction (GtkIconSource *source,
1651 GtkTextDirection direction)
1653 g_return_if_fail (source != NULL);
1655 source->direction = direction;
1659 * gtk_icon_source_set_state:
1660 * @source: a #GtkIconSource
1661 * @state: widget state this source applies to
1663 * Sets the widget state this icon source is intended to be used
1666 * Setting the widget state on an icon source makes no difference
1667 * if the state is wildcarded. Therefore, you should usually
1668 * call gtk_icon_source_set_state_wildcarded() to un-wildcard it
1669 * in addition to calling this function.
1673 gtk_icon_source_set_state (GtkIconSource *source,
1676 g_return_if_fail (source != NULL);
1678 source->state = state;
1682 * gtk_icon_source_set_size:
1683 * @source: a #GtkIconSource
1684 * @size: icon size this source applies to
1686 * Sets the icon size this icon source is intended to be used
1689 * Setting the icon size on an icon source makes no difference
1690 * if the size is wildcarded. Therefore, you should usually
1691 * call gtk_icon_source_set_size_wildcarded() to un-wildcard it
1692 * in addition to calling this function.
1696 gtk_icon_source_set_size (GtkIconSource *source,
1699 g_return_if_fail (source != NULL);
1701 source->size = size;
1705 * gtk_icon_source_get_direction:
1706 * @source: a #GtkIconSource
1708 * Obtains the text direction this icon source applies to. The return
1709 * value is only useful/meaningful if the text direction is NOT
1712 * Return value: text direction this source matches
1715 gtk_icon_source_get_direction (const GtkIconSource *source)
1717 g_return_val_if_fail (source != NULL, 0);
1719 return source->direction;
1723 * gtk_icon_source_get_state:
1724 * @source: a #GtkIconSource
1726 * Obtains the widget state this icon source applies to. The return
1727 * value is only useful/meaningful if the widget state is NOT
1730 * Return value: widget state this source matches
1733 gtk_icon_source_get_state (const GtkIconSource *source)
1735 g_return_val_if_fail (source != NULL, 0);
1737 return source->state;
1741 * gtk_icon_source_get_size:
1742 * @source: a #GtkIconSource
1744 * Obtains the icon size this source applies to. The return value
1745 * is only useful/meaningful if the icon size is NOT wildcarded.
1747 * Return value: icon size this source matches.
1750 gtk_icon_source_get_size (const GtkIconSource *source)
1752 g_return_val_if_fail (source != NULL, 0);
1754 return source->size;
1757 /* Note that the logical maximum is 20 per GtkTextDirection, so we could
1758 * eventually set this to >20 to never throw anything out.
1760 #define NUM_CACHED_ICONS 8
1762 typedef struct _CachedIcon CachedIcon;
1766 /* These must all match to use the cached pixbuf.
1767 * If any don't match, we must re-render the pixbuf.
1770 GtkTextDirection direction;
1778 ensure_cache_up_to_date (GtkIconSet *icon_set)
1780 if (icon_set->cache_serial != cache_serial)
1781 clear_cache (icon_set, TRUE);
1785 cached_icon_free (CachedIcon *icon)
1787 g_object_unref (G_OBJECT (icon->pixbuf));
1793 find_in_cache (GtkIconSet *icon_set,
1795 GtkTextDirection direction,
1802 ensure_cache_up_to_date (icon_set);
1805 tmp_list = icon_set->cache;
1806 while (tmp_list != NULL)
1808 CachedIcon *icon = tmp_list->data;
1810 if (icon->style == style &&
1811 icon->direction == direction &&
1812 icon->state == state &&
1817 /* Move this icon to the front of the list. */
1818 prev->next = tmp_list->next;
1819 tmp_list->next = icon_set->cache;
1820 icon_set->cache = tmp_list;
1823 return icon->pixbuf;
1827 tmp_list = g_slist_next (tmp_list);
1834 add_to_cache (GtkIconSet *icon_set,
1836 GtkTextDirection direction,
1843 ensure_cache_up_to_date (icon_set);
1845 g_object_ref (G_OBJECT (pixbuf));
1847 /* We have to ref the style, since if the style was finalized
1848 * its address could be reused by another style, creating a
1853 g_object_ref (G_OBJECT (style));
1856 icon = g_new (CachedIcon, 1);
1857 icon_set->cache = g_slist_prepend (icon_set->cache, icon);
1859 icon->style = style;
1860 icon->direction = direction;
1861 icon->state = state;
1863 icon->pixbuf = pixbuf;
1866 attach_to_style (icon_set, icon->style);
1868 if (icon_set->cache_size >= NUM_CACHED_ICONS)
1870 /* Remove oldest item in the cache */
1874 tmp_list = icon_set->cache;
1876 /* Find next-to-last link */
1877 g_assert (NUM_CACHED_ICONS > 2);
1878 while (tmp_list->next->next)
1879 tmp_list = tmp_list->next;
1881 g_assert (tmp_list != NULL);
1882 g_assert (tmp_list->next != NULL);
1883 g_assert (tmp_list->next->next == NULL);
1885 /* Free the last icon */
1886 icon = tmp_list->next->data;
1888 g_slist_free (tmp_list->next);
1889 tmp_list->next = NULL;
1891 cached_icon_free (icon);
1896 clear_cache (GtkIconSet *icon_set,
1897 gboolean style_detach)
1900 GtkStyle *last_style = NULL;
1902 tmp_list = icon_set->cache;
1903 while (tmp_list != NULL)
1905 CachedIcon *icon = tmp_list->data;
1909 /* simple optimization for the case where the cache
1910 * contains contiguous icons from the same style.
1911 * it's safe to call detach_from_style more than
1912 * once on the same style though.
1914 if (last_style != icon->style)
1916 detach_from_style (icon_set, icon->style);
1917 last_style = icon->style;
1921 cached_icon_free (icon);
1923 tmp_list = g_slist_next (tmp_list);
1926 g_slist_free (icon_set->cache);
1927 icon_set->cache = NULL;
1928 icon_set->cache_size = 0;
1932 copy_cache (GtkIconSet *icon_set,
1933 GtkIconSet *copy_recipient)
1936 GSList *copy = NULL;
1938 ensure_cache_up_to_date (icon_set);
1940 tmp_list = icon_set->cache;
1941 while (tmp_list != NULL)
1943 CachedIcon *icon = tmp_list->data;
1944 CachedIcon *icon_copy = g_new (CachedIcon, 1);
1948 if (icon_copy->style)
1949 attach_to_style (copy_recipient, icon_copy->style);
1951 g_object_ref (G_OBJECT (icon_copy->pixbuf));
1953 icon_copy->size = icon->size;
1955 copy = g_slist_prepend (copy, icon_copy);
1957 tmp_list = g_slist_next (tmp_list);
1960 return g_slist_reverse (copy);
1964 attach_to_style (GtkIconSet *icon_set,
1969 table = g_object_get_qdata (G_OBJECT (style),
1970 g_quark_try_string ("gtk-style-icon-sets"));
1974 table = g_hash_table_new (NULL, NULL);
1975 g_object_set_qdata_full (G_OBJECT (style),
1976 g_quark_from_static_string ("gtk-style-icon-sets"),
1981 g_hash_table_insert (table, icon_set, icon_set);
1985 detach_from_style (GtkIconSet *icon_set,
1990 table = g_object_get_qdata (G_OBJECT (style),
1991 g_quark_try_string ("gtk-style-icon-sets"));
1994 g_hash_table_remove (table, icon_set);
1998 iconsets_foreach (gpointer key,
2002 GtkIconSet *icon_set = key;
2004 /* We only need to remove cache entries for the given style;
2005 * but that complicates things because in destroy notify
2006 * we don't know which style got destroyed, and 95% of the
2007 * time all cache entries will have the same style,
2008 * so this is faster anyway.
2011 clear_cache (icon_set, FALSE);
2015 style_dnotify (gpointer data)
2017 GHashTable *table = data;
2019 g_hash_table_foreach (table, iconsets_foreach, NULL);
2021 g_hash_table_destroy (table);
2024 /* This allows the icon set to detect that its cache is out of date. */
2026 _gtk_icon_set_invalidate_caches (void)
2032 listify_foreach (gpointer key, gpointer value, gpointer data)
2034 GSList **list = data;
2036 *list = g_slist_prepend (*list, key);
2040 g_hash_table_get_keys (GHashTable *table)
2042 GSList *list = NULL;
2044 g_hash_table_foreach (table, listify_foreach, &list);
2050 * _gtk_icon_factory_list_ids:
2052 * Gets all known IDs stored in an existing icon factory.
2053 * The strings in the returned list aren't copied.
2054 * The list itself should be freed.
2056 * Return value: List of ids in icon factories
2059 _gtk_icon_factory_list_ids (void)
2066 ensure_default_icons ();
2068 tmp_list = all_icon_factories;
2069 while (tmp_list != NULL)
2073 GtkIconFactory *factory = GTK_ICON_FACTORY (tmp_list->data);
2075 these_ids = g_hash_table_get_keys (factory->icons);
2077 ids = g_slist_concat (ids, these_ids);
2079 tmp_list = g_slist_next (tmp_list);