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/.
31 #include <pango/pango-utils.h> /* For pango_scan_* */
32 #include "gtkiconfactory.h"
33 #include "stock-icons/gtkstockpixbufs.h"
35 #include "gtkicontheme.h"
36 #include "gtksettings.h"
38 #include "gtkwidget.h"
41 static GSList *all_icon_factories = NULL;
44 GTK_ICON_SOURCE_EMPTY,
45 GTK_ICON_SOURCE_ICON_NAME,
46 GTK_ICON_SOURCE_FILENAME,
47 GTK_ICON_SOURCE_PIXBUF
52 GtkIconSourceType type;
60 GdkPixbuf *filename_pixbuf;
62 GtkTextDirection direction;
66 /* If TRUE, then the parameter is wildcarded, and the above
67 * fields should be ignored. If FALSE, the parameter is
68 * specified, and the above fields should be valid.
70 guint any_direction : 1;
75 static gpointer parent_class = NULL;
77 static void gtk_icon_factory_init (GtkIconFactory *icon_factory);
78 static void gtk_icon_factory_class_init (GtkIconFactoryClass *klass);
79 static void gtk_icon_factory_finalize (GObject *object);
80 static void get_default_icons (GtkIconFactory *icon_factory);
81 static void icon_source_clear (GtkIconSource *source);
83 static GtkIconSize icon_size_register_intern (const gchar *name,
87 #define GTK_ICON_SOURCE_INIT(any_direction, any_state, any_size) \
88 { GTK_ICON_SOURCE_EMPTY, { NULL }, NULL, \
90 any_direction, any_state, any_size }
93 gtk_icon_factory_get_type (void)
95 static GType icon_factory_type = 0;
97 if (!icon_factory_type)
99 static const GTypeInfo icon_factory_info =
101 sizeof (GtkIconFactoryClass),
102 NULL, /* base_init */
103 NULL, /* base_finalize */
104 (GClassInitFunc) gtk_icon_factory_class_init,
105 NULL, /* class_finalize */
106 NULL, /* class_data */
107 sizeof (GtkIconFactory),
109 (GInstanceInitFunc) gtk_icon_factory_init,
113 g_type_register_static (G_TYPE_OBJECT, "GtkIconFactory",
114 &icon_factory_info, 0);
117 return icon_factory_type;
121 gtk_icon_factory_init (GtkIconFactory *factory)
123 factory->icons = g_hash_table_new (g_str_hash, g_str_equal);
124 all_icon_factories = g_slist_prepend (all_icon_factories, factory);
128 gtk_icon_factory_class_init (GtkIconFactoryClass *klass)
130 GObjectClass *object_class = G_OBJECT_CLASS (klass);
132 parent_class = g_type_class_peek_parent (klass);
134 object_class->finalize = gtk_icon_factory_finalize;
138 free_icon_set (gpointer key, gpointer value, gpointer data)
141 gtk_icon_set_unref (value);
145 gtk_icon_factory_finalize (GObject *object)
147 GtkIconFactory *factory = GTK_ICON_FACTORY (object);
149 all_icon_factories = g_slist_remove (all_icon_factories, factory);
151 g_hash_table_foreach (factory->icons, free_icon_set, NULL);
153 g_hash_table_destroy (factory->icons);
155 G_OBJECT_CLASS (parent_class)->finalize (object);
159 * gtk_icon_factory_new:
161 * Creates a new #GtkIconFactory. An icon factory manages a collection
162 * of #GtkIconSet<!-- -->s; a #GtkIconSet manages a set of variants of a
163 * particular icon (i.e. a #GtkIconSet contains variants for different
164 * sizes and widget states). Icons in an icon factory are named by a
165 * stock ID, which is a simple string identifying the icon. Each
166 * #GtkStyle has a list of #GtkIconFactory<!-- -->s derived from the current
167 * theme; those icon factories are consulted first when searching for
168 * an icon. If the theme doesn't set a particular icon, GTK+ looks for
169 * the icon in a list of default icon factories, maintained by
170 * gtk_icon_factory_add_default() and
171 * gtk_icon_factory_remove_default(). Applications with icons should
172 * add a default icon factory with their icons, which will allow
173 * themes to override the icons for the application.
175 * Return value: a new #GtkIconFactory
178 gtk_icon_factory_new (void)
180 return g_object_new (GTK_TYPE_ICON_FACTORY, NULL);
184 * gtk_icon_factory_add:
185 * @factory: a #GtkIconFactory
186 * @stock_id: icon name
187 * @icon_set: icon set
189 * Adds the given @icon_set to the icon factory, under the name
190 * @stock_id. @stock_id should be namespaced for your application,
191 * e.g. "myapp-whatever-icon". Normally applications create a
192 * #GtkIconFactory, then add it to the list of default factories with
193 * gtk_icon_factory_add_default(). Then they pass the @stock_id to
194 * widgets such as #GtkImage to display the icon. Themes can provide
195 * an icon with the same name (such as "myapp-whatever-icon") to
196 * override your application's default icons. If an icon already
197 * existed in @factory for @stock_id, it is unreferenced and replaced
198 * with the new @icon_set.
202 gtk_icon_factory_add (GtkIconFactory *factory,
203 const gchar *stock_id,
204 GtkIconSet *icon_set)
206 gpointer old_key = NULL;
207 gpointer old_value = NULL;
209 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
210 g_return_if_fail (stock_id != NULL);
211 g_return_if_fail (icon_set != NULL);
213 g_hash_table_lookup_extended (factory->icons, stock_id,
214 &old_key, &old_value);
216 if (old_value == icon_set)
219 gtk_icon_set_ref (icon_set);
221 /* GHashTable key memory management is so fantastically broken. */
223 g_hash_table_insert (factory->icons, old_key, icon_set);
225 g_hash_table_insert (factory->icons, g_strdup (stock_id), icon_set);
228 gtk_icon_set_unref (old_value);
232 * gtk_icon_factory_lookup:
233 * @factory: a #GtkIconFactory
234 * @stock_id: an icon name
236 * Looks up @stock_id in the icon factory, returning an icon set
237 * if found, otherwise %NULL. For display to the user, you should
238 * use gtk_style_lookup_icon_set() on the #GtkStyle for the
239 * widget that will display the icon, instead of using this
240 * function directly, so that themes are taken into account.
242 * Return value: icon set of @stock_id.
245 gtk_icon_factory_lookup (GtkIconFactory *factory,
246 const gchar *stock_id)
248 g_return_val_if_fail (GTK_IS_ICON_FACTORY (factory), NULL);
249 g_return_val_if_fail (stock_id != NULL, NULL);
251 return g_hash_table_lookup (factory->icons, stock_id);
254 static GtkIconFactory *gtk_default_icons = NULL;
255 static GSList *default_factories = NULL;
258 * gtk_icon_factory_add_default:
259 * @factory: a #GtkIconFactory
261 * Adds an icon factory to the list of icon factories searched by
262 * gtk_style_lookup_icon_set(). This means that, for example,
263 * gtk_image_new_from_stock() will be able to find icons in @factory.
264 * There will normally be an icon factory added for each library or
265 * application that comes with icons. The default icon factories
266 * can be overridden by themes.
270 gtk_icon_factory_add_default (GtkIconFactory *factory)
272 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
274 g_object_ref (factory);
276 default_factories = g_slist_prepend (default_factories, factory);
280 * gtk_icon_factory_remove_default:
281 * @factory: a #GtkIconFactory previously added with gtk_icon_factory_add_default()
283 * Removes an icon factory from the list of default icon
284 * factories. Not normally used; you might use it for a library that
285 * can be unloaded or shut down.
289 gtk_icon_factory_remove_default (GtkIconFactory *factory)
291 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
293 default_factories = g_slist_remove (default_factories, factory);
295 g_object_unref (factory);
299 ensure_default_icons (void)
301 if (gtk_default_icons == NULL)
303 gtk_default_icons = gtk_icon_factory_new ();
305 get_default_icons (gtk_default_icons);
310 * gtk_icon_factory_lookup_default:
311 * @stock_id: an icon name
313 * Looks for an icon in the list of default icon factories. For
314 * display to the user, you should use gtk_style_lookup_icon_set() on
315 * the #GtkStyle for the widget that will display the icon, instead of
316 * using this function directly, so that themes are taken into
320 * Return value: a #GtkIconSet, or %NULL
323 gtk_icon_factory_lookup_default (const gchar *stock_id)
327 g_return_val_if_fail (stock_id != NULL, NULL);
329 tmp_list = default_factories;
330 while (tmp_list != NULL)
332 GtkIconSet *icon_set =
333 gtk_icon_factory_lookup (GTK_ICON_FACTORY (tmp_list->data),
339 tmp_list = g_slist_next (tmp_list);
342 ensure_default_icons ();
344 return gtk_icon_factory_lookup (gtk_default_icons, stock_id);
348 register_stock_icon (GtkIconFactory *factory,
349 const gchar *stock_id)
351 GtkIconSet *set = gtk_icon_set_new ();
352 GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
354 source.type = GTK_ICON_SOURCE_ICON_NAME;
355 source.source.icon_name = (gchar *)stock_id;
356 gtk_icon_set_add_source (set, &source);
358 gtk_icon_factory_add (factory, stock_id, set);
359 gtk_icon_set_unref (set);
363 register_bidi_stock_icon (GtkIconFactory *factory,
364 const gchar *stock_id,
365 const gchar *stock_id_ltr,
366 const gchar *stock_id_rtl)
368 GtkIconSet *set = gtk_icon_set_new ();
369 GtkIconSource source = GTK_ICON_SOURCE_INIT (FALSE, TRUE, TRUE);
371 source.type = GTK_ICON_SOURCE_ICON_NAME;
372 source.source.icon_name = (gchar *)stock_id_ltr;
373 source.direction = GTK_TEXT_DIR_LTR;
374 gtk_icon_set_add_source (set, &source);
376 source.type = GTK_ICON_SOURCE_ICON_NAME;
377 source.source.icon_name = (gchar *)stock_id_rtl;
378 source.direction = GTK_TEXT_DIR_RTL;
379 gtk_icon_set_add_source (set, &source);
381 gtk_icon_factory_add (factory, stock_id, set);
382 gtk_icon_set_unref (set);
386 add_default_image (const gchar *stock_id,
388 const guchar *inline_data)
390 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_inline (-1, inline_data, FALSE, NULL);
393 gtk_icon_theme_add_builtin_icon (stock_id, size, pixbuf);
395 g_object_unref (pixbuf);
399 add_icon (GtkIconFactory *factory,
400 const gchar *stock_id,
402 const guchar *inline_data)
404 register_stock_icon (factory, stock_id);
406 add_default_image (stock_id, size, inline_data);
410 add_icon2 (GtkIconFactory *factory,
411 const gchar *stock_id,
413 const guchar *inline_data1,
415 const guchar *inline_data2)
417 register_stock_icon (factory, stock_id);
419 add_default_image (stock_id, size1, inline_data1);
420 add_default_image (stock_id, size2, inline_data2);
424 add_icon_bidi2 (GtkIconFactory *factory,
425 const gchar *stock_id,
427 const guchar *inline_data_ltr1,
428 const guchar *inline_data_rtl1,
430 const guchar *inline_data_ltr2,
431 const guchar *inline_data_rtl2)
433 gchar *stock_id_ltr = g_strconcat (stock_id, "-ltr", NULL);
434 gchar *stock_id_rtl = g_strconcat (stock_id, "-rtl", NULL);
436 register_bidi_stock_icon (factory, stock_id,
437 stock_id_ltr, stock_id_rtl);
439 add_default_image (stock_id_ltr, size1, inline_data_ltr1);
440 add_default_image (stock_id_ltr, size2, inline_data_ltr2);
442 add_default_image (stock_id_rtl, size1, inline_data_rtl1);
443 add_default_image (stock_id_rtl, size2, inline_data_rtl2);
445 g_free (stock_id_ltr);
446 g_free (stock_id_rtl);
450 get_default_icons (GtkIconFactory *factory)
452 /* KEEP IN SYNC with gtkstock.c */
454 /* Have dialog size */
455 add_icon (factory, GTK_STOCK_DIALOG_AUTHENTICATION, 48, stock_dialog_authentication_48);
456 add_icon (factory, GTK_STOCK_DIALOG_ERROR, 48, stock_dialog_error_48);
457 add_icon (factory, GTK_STOCK_DIALOG_INFO, 48, stock_dialog_info_48);
458 add_icon (factory, GTK_STOCK_DIALOG_QUESTION, 48, stock_dialog_question_48);
459 add_icon (factory, GTK_STOCK_DIALOG_WARNING, 48, stock_dialog_warning_48);
462 add_icon (factory, GTK_STOCK_DND, 32, stock_dnd_32);
463 add_icon (factory, GTK_STOCK_DND_MULTIPLE, 32, stock_dnd_multiple_32);
465 /* Have button sizes */
466 add_icon (factory, GTK_STOCK_APPLY, 20, stock_apply_20);
467 add_icon (factory, GTK_STOCK_CANCEL, 20, stock_cancel_20);
468 add_icon (factory, GTK_STOCK_NO, 20, stock_no_20);
469 add_icon (factory, GTK_STOCK_OK, 20, stock_ok_20);
470 add_icon (factory, GTK_STOCK_YES, 20, stock_yes_20);
472 /* Generic + button sizes */
473 add_icon2 (factory, GTK_STOCK_CLOSE,
477 /* Generic + menu sizes */
478 add_icon2 (factory, GTK_STOCK_ADD,
482 add_icon2 (factory, GTK_STOCK_JUSTIFY_CENTER,
483 16, stock_align_center_16,
484 24, stock_align_center_24);
486 add_icon2 (factory, GTK_STOCK_JUSTIFY_FILL,
487 16, stock_align_justify_16,
488 24, stock_align_justify_24);
490 add_icon2 (factory, GTK_STOCK_JUSTIFY_LEFT,
491 16, stock_align_left_16,
492 24, stock_align_left_24);
494 add_icon2 (factory, GTK_STOCK_JUSTIFY_RIGHT,
495 16, stock_align_right_16,
496 24, stock_align_right_24);
498 add_icon2 (factory, GTK_STOCK_GOTO_BOTTOM,
500 24, stock_bottom_24);
502 add_icon2 (factory, GTK_STOCK_CDROM,
506 add_icon2 (factory, GTK_STOCK_CONVERT,
507 16, stock_convert_16,
508 24, stock_convert_24);
510 add_icon2 (factory, GTK_STOCK_COPY,
514 add_icon2 (factory, GTK_STOCK_CUT,
518 add_icon2 (factory, GTK_STOCK_GO_DOWN,
519 16, stock_down_arrow_16,
520 24, stock_down_arrow_24);
522 add_icon2 (factory, GTK_STOCK_EXECUTE,
526 add_icon2 (factory, GTK_STOCK_QUIT,
530 add_icon_bidi2 (factory, GTK_STOCK_GOTO_FIRST,
531 16, stock_first_16, stock_last_16,
532 24, stock_first_24, stock_last_24);
534 add_icon2 (factory, GTK_STOCK_SELECT_FONT,
538 add_icon2 (factory, GTK_STOCK_HARDDISK,
539 16, stock_harddisk_16,
540 24, stock_harddisk_24);
542 add_icon2 (factory, GTK_STOCK_HELP,
546 add_icon2 (factory, GTK_STOCK_HOME,
550 add_icon_bidi2 (factory, GTK_STOCK_JUMP_TO,
551 16, stock_jump_to_16, stock_jump_to_rtl_16,
552 24, stock_jump_to_24, stock_jump_to_rtl_24);
554 add_icon_bidi2 (factory, GTK_STOCK_GOTO_LAST,
555 16, stock_last_16, stock_first_16,
556 24, stock_last_24, stock_first_24);
558 add_icon_bidi2 (factory, GTK_STOCK_GO_BACK,
559 16, stock_left_arrow_16, stock_right_arrow_16,
560 24, stock_left_arrow_24, stock_right_arrow_24);
562 add_icon2 (factory, GTK_STOCK_MISSING_IMAGE,
563 16, stock_missing_image_16,
564 24, stock_missing_image_24);
566 add_icon2 (factory, GTK_STOCK_NETWORK,
567 16, stock_network_16,
568 24, stock_network_24);
570 add_icon2 (factory, GTK_STOCK_NEW,
574 add_icon2 (factory, GTK_STOCK_OPEN,
578 add_icon2 (factory, GTK_STOCK_PASTE,
582 add_icon2 (factory, GTK_STOCK_PREFERENCES,
583 16, stock_preferences_16,
584 24, stock_preferences_24);
586 add_icon2 (factory, GTK_STOCK_PRINT,
590 add_icon2 (factory, GTK_STOCK_PRINT_PREVIEW,
591 16, stock_print_preview_16,
592 24, stock_print_preview_24);
594 add_icon2 (factory, GTK_STOCK_PROPERTIES,
595 16, stock_properties_16,
596 24, stock_properties_24);
598 add_icon_bidi2 (factory, GTK_STOCK_REDO,
599 16, stock_redo_16, stock_redo_rtl_16,
600 24, stock_redo_24, stock_redo_rtl_24);
602 add_icon2 (factory, GTK_STOCK_REMOVE,
604 24, stock_remove_24);
606 add_icon2 (factory, GTK_STOCK_REFRESH,
607 16, stock_refresh_16,
608 24, stock_refresh_24);
610 add_icon_bidi2 (factory, GTK_STOCK_REVERT_TO_SAVED,
611 16, stock_revert_16, stock_revert_rtl_16,
612 24, stock_revert_24, stock_revert_rtl_24);
614 add_icon_bidi2 (factory, GTK_STOCK_GO_FORWARD,
615 16, stock_right_arrow_16, stock_left_arrow_16,
616 24, stock_right_arrow_24, stock_left_arrow_24);
618 add_icon2 (factory, GTK_STOCK_SAVE,
622 add_icon2 (factory, GTK_STOCK_FLOPPY,
626 add_icon2 (factory, GTK_STOCK_SAVE_AS,
627 16, stock_save_as_16,
628 24, stock_save_as_24);
630 add_icon2 (factory, GTK_STOCK_FIND,
632 24, stock_search_24);
634 add_icon2 (factory, GTK_STOCK_FIND_AND_REPLACE,
635 16, stock_search_replace_16,
636 24, stock_search_replace_24);
638 add_icon2 (factory, GTK_STOCK_SORT_DESCENDING,
639 16, stock_sort_descending_16,
640 24, stock_sort_descending_24);
642 add_icon2 (factory, GTK_STOCK_SORT_ASCENDING,
643 16, stock_sort_ascending_16,
644 24, stock_sort_ascending_24);
646 add_icon2 (factory, GTK_STOCK_SPELL_CHECK,
647 16, stock_spellcheck_16,
648 24, stock_spellcheck_24);
650 add_icon2 (factory, GTK_STOCK_STOP,
654 add_icon2 (factory, GTK_STOCK_BOLD,
655 16, stock_text_bold_16,
656 24, stock_text_bold_24);
658 add_icon2 (factory, GTK_STOCK_ITALIC,
659 16, stock_text_italic_16,
660 24, stock_text_italic_24);
662 add_icon2 (factory, GTK_STOCK_STRIKETHROUGH,
663 16, stock_text_strikethrough_16,
664 24, stock_text_strikethrough_24);
666 add_icon2 (factory, GTK_STOCK_UNDERLINE,
667 16, stock_text_underline_16,
668 24, stock_text_underline_24);
670 add_icon2 (factory, GTK_STOCK_INDENT,
671 16, stock_text_indent_16,
672 24, stock_text_indent_24);
674 add_icon2 (factory, GTK_STOCK_UNINDENT,
675 16, stock_text_unindent_16,
676 24, stock_text_unindent_24);
678 add_icon2 (factory, GTK_STOCK_GOTO_TOP,
682 add_icon2 (factory, GTK_STOCK_DELETE,
686 add_icon_bidi2 (factory, GTK_STOCK_UNDELETE,
687 16, stock_undelete_16, stock_undelete_rtl_16,
688 24, stock_undelete_24, stock_undelete_rtl_24);
690 add_icon_bidi2 (factory, GTK_STOCK_UNDO,
691 16, stock_undo_16, stock_undo_rtl_16,
692 24, stock_undo_24, stock_undo_rtl_24);
694 add_icon2 (factory, GTK_STOCK_GO_UP,
695 16, stock_up_arrow_16,
696 24, stock_up_arrow_24);
698 add_icon2 (factory, GTK_STOCK_FILE,
702 add_icon2 (factory, GTK_STOCK_DIRECTORY,
703 16, stock_directory_16,
704 24, stock_directory_24);
706 add_icon2 (factory, GTK_STOCK_ABOUT,
710 add_icon2 (factory, GTK_STOCK_CONNECT,
711 16, stock_connect_16,
712 24, stock_connect_24);
714 add_icon2 (factory, GTK_STOCK_DISCONNECT,
715 16, stock_disconnect_16,
716 24, stock_disconnect_24);
718 add_icon2 (factory, GTK_STOCK_EDIT,
722 add_icon2 (factory, GTK_STOCK_MEDIA_FORWARD,
723 16, stock_media_forward_16,
724 24, stock_media_forward_24);
726 add_icon2 (factory, GTK_STOCK_MEDIA_NEXT,
727 16, stock_media_next_16,
728 24, stock_media_next_24);
730 add_icon2 (factory, GTK_STOCK_MEDIA_PAUSE,
731 16, stock_media_pause_16,
732 24, stock_media_pause_24);
734 add_icon2 (factory, GTK_STOCK_MEDIA_PLAY,
735 16, stock_media_play_16,
736 24, stock_media_play_24);
738 add_icon2 (factory, GTK_STOCK_MEDIA_PREVIOUS,
739 16, stock_media_previous_16,
740 24, stock_media_previous_24);
742 add_icon2 (factory, GTK_STOCK_MEDIA_RECORD,
743 16, stock_media_record_16,
744 24, stock_media_record_24);
746 add_icon2 (factory, GTK_STOCK_MEDIA_REWIND,
747 16, stock_media_rewind_16,
748 24, stock_media_rewind_24);
750 add_icon2 (factory, GTK_STOCK_MEDIA_STOP,
751 16, stock_media_stop_16,
752 24, stock_media_stop_24);
754 /* Generic size only */
756 add_icon (factory, GTK_STOCK_CLEAR, 24, stock_clear_24);
757 add_icon (factory, GTK_STOCK_SELECT_COLOR, 24, stock_colorselector_24);
758 add_icon (factory, GTK_STOCK_COLOR_PICKER, 25, stock_color_picker_25);
759 add_icon (factory, GTK_STOCK_INDEX, 24, stock_index_24);
760 add_icon (factory, GTK_STOCK_ZOOM_100, 24, stock_zoom_1_24);
761 add_icon (factory, GTK_STOCK_ZOOM_FIT, 24, stock_zoom_fit_24);
762 add_icon (factory, GTK_STOCK_ZOOM_IN, 24, stock_zoom_in_24);
763 add_icon (factory, GTK_STOCK_ZOOM_OUT, 24, stock_zoom_out_24);
766 /************************************************************
767 * Icon size handling *
768 ************************************************************/
770 typedef struct _IconSize IconSize;
781 typedef struct _IconAlias IconAlias;
789 typedef struct _SettingsIconSize SettingsIconSize;
791 struct _SettingsIconSize
797 static GHashTable *icon_aliases = NULL;
798 static IconSize *icon_sizes = NULL;
799 static gint icon_sizes_allocated = 0;
800 static gint icon_sizes_used = 0;
803 init_icon_sizes (void)
805 if (icon_sizes == NULL)
807 #define NUM_BUILTIN_SIZES 7
810 icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
812 icon_sizes = g_new (IconSize, NUM_BUILTIN_SIZES);
813 icon_sizes_allocated = NUM_BUILTIN_SIZES;
814 icon_sizes_used = NUM_BUILTIN_SIZES;
816 icon_sizes[GTK_ICON_SIZE_INVALID].size = 0;
817 icon_sizes[GTK_ICON_SIZE_INVALID].name = NULL;
818 icon_sizes[GTK_ICON_SIZE_INVALID].width = 0;
819 icon_sizes[GTK_ICON_SIZE_INVALID].height = 0;
821 /* the name strings aren't copied since we don't ever remove
822 * icon sizes, so we don't need to know whether they're static.
823 * Even if we did I suppose removing the builtin sizes would be
827 icon_sizes[GTK_ICON_SIZE_MENU].size = GTK_ICON_SIZE_MENU;
828 icon_sizes[GTK_ICON_SIZE_MENU].name = "gtk-menu";
829 icon_sizes[GTK_ICON_SIZE_MENU].width = 16;
830 icon_sizes[GTK_ICON_SIZE_MENU].height = 16;
832 icon_sizes[GTK_ICON_SIZE_BUTTON].size = GTK_ICON_SIZE_BUTTON;
833 icon_sizes[GTK_ICON_SIZE_BUTTON].name = "gtk-button";
834 icon_sizes[GTK_ICON_SIZE_BUTTON].width = 20;
835 icon_sizes[GTK_ICON_SIZE_BUTTON].height = 20;
837 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].size = GTK_ICON_SIZE_SMALL_TOOLBAR;
838 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].name = "gtk-small-toolbar";
839 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].width = 18;
840 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].height = 18;
842 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].size = GTK_ICON_SIZE_LARGE_TOOLBAR;
843 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].name = "gtk-large-toolbar";
844 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].width = 24;
845 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].height = 24;
847 icon_sizes[GTK_ICON_SIZE_DND].size = GTK_ICON_SIZE_DND;
848 icon_sizes[GTK_ICON_SIZE_DND].name = "gtk-dnd";
849 icon_sizes[GTK_ICON_SIZE_DND].width = 32;
850 icon_sizes[GTK_ICON_SIZE_DND].height = 32;
852 icon_sizes[GTK_ICON_SIZE_DIALOG].size = GTK_ICON_SIZE_DIALOG;
853 icon_sizes[GTK_ICON_SIZE_DIALOG].name = "gtk-dialog";
854 icon_sizes[GTK_ICON_SIZE_DIALOG].width = 48;
855 icon_sizes[GTK_ICON_SIZE_DIALOG].height = 48;
857 g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
859 /* Alias everything to itself. */
860 i = 1; /* skip invalid size */
861 while (i < NUM_BUILTIN_SIZES)
863 gtk_icon_size_register_alias (icon_sizes[i].name, icon_sizes[i].size);
868 #undef NUM_BUILTIN_SIZES
873 free_settings_sizes (gpointer data)
875 g_array_free (data, TRUE);
879 get_settings_sizes (GtkSettings *settings,
882 GArray *settings_sizes;
883 static GQuark sizes_quark = 0;
886 sizes_quark = g_quark_from_static_string ("gtk-icon-sizes");
888 settings_sizes = g_object_get_qdata (G_OBJECT (settings), sizes_quark);
891 settings_sizes = g_array_new (FALSE, FALSE, sizeof (SettingsIconSize));
892 g_object_set_qdata_full (G_OBJECT (settings), sizes_quark,
893 settings_sizes, free_settings_sizes);
898 return settings_sizes;
902 icon_size_set_for_settings (GtkSettings *settings,
903 const gchar *size_name,
908 GArray *settings_sizes;
909 SettingsIconSize *settings_size;
911 g_return_if_fail (size_name != NULL);
913 size = gtk_icon_size_from_name (size_name);
914 if (size == GTK_ICON_SIZE_INVALID)
915 /* Reserve a place */
916 size = icon_size_register_intern (size_name, -1, -1);
918 settings_sizes = get_settings_sizes (settings, NULL);
919 if (size >= settings_sizes->len)
921 SettingsIconSize unset = { -1, -1 };
924 for (i = settings_sizes->len; i <= size; i++)
925 g_array_append_val (settings_sizes, unset);
928 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
930 settings_size->width = width;
931 settings_size->height = height;
934 /* Like pango_parse_word, but accept - as well
937 scan_icon_size_name (const char **pos, GString *out)
939 const char *p = *pos;
941 while (g_ascii_isspace (*p))
944 if (!((*p >= 'A' && *p <= 'Z') ||
945 (*p >= 'a' && *p <= 'z') ||
946 *p == '_' || *p == '-'))
949 g_string_truncate (out, 0);
950 g_string_append_c (out, *p);
953 while ((*p >= 'A' && *p <= 'Z') ||
954 (*p >= 'a' && *p <= 'z') ||
955 (*p >= '0' && *p <= '9') ||
956 *p == '_' || *p == '-')
958 g_string_append_c (out, *p);
968 icon_size_setting_parse (GtkSettings *settings,
969 const gchar *icon_size_string)
971 GString *name_buf = g_string_new (NULL);
972 const gchar *p = icon_size_string;
974 while (pango_skip_space (&p))
978 if (!scan_icon_size_name (&p, name_buf))
981 if (!pango_skip_space (&p))
989 if (!pango_scan_int (&p, &width))
992 if (!pango_skip_space (&p))
1000 if (!pango_scan_int (&p, &height))
1003 if (width > 0 && height > 0)
1005 icon_size_set_for_settings (settings, name_buf->str,
1010 g_warning ("Invalid size in gtk-icon-sizes: %d,%d\n", width, height);
1013 pango_skip_space (&p);
1022 g_string_free (name_buf, TRUE);
1026 g_warning ("Error parsing gtk-icon-sizes string:\n\t'%s'", icon_size_string);
1027 g_string_free (name_buf, TRUE);
1031 icon_size_set_all_from_settings (GtkSettings *settings)
1033 GArray *settings_sizes;
1034 gchar *icon_size_string;
1036 /* Reset old settings */
1037 settings_sizes = get_settings_sizes (settings, NULL);
1038 g_array_set_size (settings_sizes, 0);
1040 g_object_get (settings,
1041 "gtk-icon-sizes", &icon_size_string,
1044 if (icon_size_string)
1046 icon_size_setting_parse (settings, icon_size_string);
1047 g_free (icon_size_string);
1052 icon_size_settings_changed (GtkSettings *settings,
1055 icon_size_set_all_from_settings (settings);
1057 gtk_rc_reset_styles (settings);
1061 icon_sizes_init_for_settings (GtkSettings *settings)
1063 g_signal_connect (settings,
1064 "notify::gtk-icon-sizes",
1065 G_CALLBACK (icon_size_settings_changed),
1068 icon_size_set_all_from_settings (settings);
1072 icon_size_lookup_intern (GtkSettings *settings,
1077 GArray *settings_sizes;
1078 gint width_for_settings = -1;
1079 gint height_for_settings = -1;
1083 if (size >= icon_sizes_used)
1086 if (size == GTK_ICON_SIZE_INVALID)
1091 gboolean initial = FALSE;
1093 settings_sizes = get_settings_sizes (settings, &initial);
1095 icon_sizes_init_for_settings (settings);
1097 if (size < settings_sizes->len)
1099 SettingsIconSize *settings_size;
1101 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
1103 width_for_settings = settings_size->width;
1104 height_for_settings = settings_size->height;
1109 *widthp = width_for_settings >= 0 ? width_for_settings : icon_sizes[size].width;
1112 *heightp = height_for_settings >= 0 ? height_for_settings : icon_sizes[size].height;
1118 * gtk_icon_size_lookup_for_settings:
1119 * @settings: a #GtkSettings object, used to determine
1120 * which set of user preferences to used.
1121 * @size: an icon size
1122 * @width: location to store icon width
1123 * @height: location to store icon height
1125 * Obtains the pixel size of a semantic icon size, possibly
1126 * modified by user preferences for a particular
1127 * #GtkSettings. Normally @size would be
1128 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
1129 * isn't normally needed, gtk_widget_render_icon() is the usual
1130 * way to get an icon for rendering, then just look at the size of
1131 * the rendered pixbuf. The rendered pixbuf may not even correspond to
1132 * the width/height returned by gtk_icon_size_lookup(), because themes
1133 * are free to render the pixbuf however they like, including changing
1136 * Return value: %TRUE if @size was a valid size
1141 gtk_icon_size_lookup_for_settings (GtkSettings *settings,
1146 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1148 return icon_size_lookup_intern (settings, size, width, height);
1152 * gtk_icon_size_lookup:
1153 * @size: an icon size
1154 * @width: location to store icon width
1155 * @height: location to store icon height
1157 * Obtains the pixel size of a semantic icon size, possibly
1158 * modified by user preferences for the default #GtkSettings.
1159 * (See gtk_icon_size_lookup_for_settings().)
1160 * Normally @size would be
1161 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
1162 * isn't normally needed, gtk_widget_render_icon() is the usual
1163 * way to get an icon for rendering, then just look at the size of
1164 * the rendered pixbuf. The rendered pixbuf may not even correspond to
1165 * the width/height returned by gtk_icon_size_lookup(), because themes
1166 * are free to render the pixbuf however they like, including changing
1169 * Return value: %TRUE if @size was a valid size
1172 gtk_icon_size_lookup (GtkIconSize size,
1176 GTK_NOTE (MULTIHEAD,
1177 g_warning ("gtk_icon_size_lookup ()) is not multihead safe"));
1179 return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (),
1180 size, widthp, heightp);
1184 icon_size_register_intern (const gchar *name,
1188 IconAlias *old_alias;
1193 old_alias = g_hash_table_lookup (icon_aliases, name);
1194 if (old_alias && icon_sizes[old_alias->target].width > 0)
1196 g_warning ("Icon size name '%s' already exists", name);
1197 return GTK_ICON_SIZE_INVALID;
1202 size = old_alias->target;
1206 if (icon_sizes_used == icon_sizes_allocated)
1208 icon_sizes_allocated *= 2;
1209 icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated);
1212 size = icon_sizes_used++;
1214 /* alias to self. */
1215 gtk_icon_size_register_alias (name, size);
1217 icon_sizes[size].size = size;
1218 icon_sizes[size].name = g_strdup (name);
1221 icon_sizes[size].width = width;
1222 icon_sizes[size].height = height;
1228 * gtk_icon_size_register:
1229 * @name: name of the icon size
1230 * @width: the icon width
1231 * @height: the icon height
1233 * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
1234 * etc. Returns the integer value for the size.
1236 * Returns: integer value representing the size
1240 gtk_icon_size_register (const gchar *name,
1244 g_return_val_if_fail (name != NULL, 0);
1245 g_return_val_if_fail (width > 0, 0);
1246 g_return_val_if_fail (height > 0, 0);
1248 return icon_size_register_intern (name, width, height);
1252 * gtk_icon_size_register_alias:
1253 * @alias: an alias for @target
1254 * @target: an existing icon size
1256 * Registers @alias as another name for @target.
1257 * So calling gtk_icon_size_from_name() with @alias as argument
1258 * will return @target.
1262 gtk_icon_size_register_alias (const gchar *alias,
1267 g_return_if_fail (alias != NULL);
1271 if (!icon_size_lookup_intern (NULL, target, NULL, NULL))
1272 g_warning ("gtk_icon_size_register_alias: Icon size %d does not exist", target);
1274 ia = g_hash_table_lookup (icon_aliases, alias);
1277 if (icon_sizes[ia->target].width > 0)
1279 g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias);
1283 ia->target = target;
1288 ia = g_new (IconAlias, 1);
1289 ia->name = g_strdup (alias);
1290 ia->target = target;
1292 g_hash_table_insert (icon_aliases, ia->name, ia);
1297 * gtk_icon_size_from_name:
1298 * @name: the name to look up.
1299 * @returns: the icon size with the given name.
1301 * Looks up the icon size associated with @name.
1304 gtk_icon_size_from_name (const gchar *name)
1310 ia = g_hash_table_lookup (icon_aliases, name);
1312 if (ia && icon_sizes[ia->target].width > 0)
1315 return GTK_ICON_SIZE_INVALID;
1319 * gtk_icon_size_get_name:
1320 * @size: a #GtkIconSize.
1321 * @returns: the name of the given icon size.
1323 * Gets the canonical name of the given icon size. The returned string
1324 * is statically allocated and should not be freed.
1326 G_CONST_RETURN gchar*
1327 gtk_icon_size_get_name (GtkIconSize size)
1329 if (size >= icon_sizes_used)
1332 return icon_sizes[size].name;
1335 /************************************************************/
1340 static GdkPixbuf *find_in_cache (GtkIconSet *icon_set,
1342 GtkTextDirection direction,
1345 static void add_to_cache (GtkIconSet *icon_set,
1347 GtkTextDirection direction,
1351 /* Clear icon set contents, drop references to all contained
1352 * GdkPixbuf objects and forget all GtkIconSources. Used to
1353 * recycle an icon set.
1355 static void clear_cache (GtkIconSet *icon_set,
1356 gboolean style_detach);
1357 static GSList* copy_cache (GtkIconSet *icon_set,
1358 GtkIconSet *copy_recipient);
1359 static void attach_to_style (GtkIconSet *icon_set,
1361 static void detach_from_style (GtkIconSet *icon_set,
1363 static void style_dnotify (gpointer data);
1371 /* Cache of the last few rendered versions of the icon. */
1379 static guint cache_serial = 0;
1384 * Creates a new #GtkIconSet. A #GtkIconSet represents a single icon
1385 * in various sizes and widget states. It can provide a #GdkPixbuf
1386 * for a given size and state on request, and automatically caches
1387 * some of the rendered #GdkPixbuf objects.
1389 * Normally you would use gtk_widget_render_icon() instead of
1390 * using #GtkIconSet directly. The one case where you'd use
1391 * #GtkIconSet is to create application-specific icon sets to place in
1392 * a #GtkIconFactory.
1394 * Return value: a new #GtkIconSet
1397 gtk_icon_set_new (void)
1399 GtkIconSet *icon_set;
1401 icon_set = g_new (GtkIconSet, 1);
1403 icon_set->ref_count = 1;
1404 icon_set->sources = NULL;
1405 icon_set->cache = NULL;
1406 icon_set->cache_size = 0;
1407 icon_set->cache_serial = cache_serial;
1413 * gtk_icon_set_new_from_pixbuf:
1414 * @pixbuf: a #GdkPixbuf
1416 * Creates a new #GtkIconSet with @pixbuf as the default/fallback
1417 * source image. If you don't add any additional #GtkIconSource to the
1418 * icon set, all variants of the icon will be created from @pixbuf,
1419 * using scaling, pixelation, etc. as required to adjust the icon size
1420 * or make the icon look insensitive/prelighted.
1422 * Return value: a new #GtkIconSet
1425 gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
1429 GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1431 g_return_val_if_fail (pixbuf != NULL, NULL);
1433 set = gtk_icon_set_new ();
1435 gtk_icon_source_set_pixbuf (&source, pixbuf);
1436 gtk_icon_set_add_source (set, &source);
1437 gtk_icon_source_set_pixbuf (&source, NULL);
1445 * @icon_set: a #GtkIconSet.
1447 * Increments the reference count on @icon_set.
1449 * Return value: @icon_set.
1452 gtk_icon_set_ref (GtkIconSet *icon_set)
1454 g_return_val_if_fail (icon_set != NULL, NULL);
1455 g_return_val_if_fail (icon_set->ref_count > 0, NULL);
1457 icon_set->ref_count += 1;
1463 * gtk_icon_set_unref:
1464 * @icon_set: a #GtkIconSet
1466 * Decrements the reference count on @icon_set, and frees memory
1467 * if the reference count reaches 0.
1470 gtk_icon_set_unref (GtkIconSet *icon_set)
1472 g_return_if_fail (icon_set != NULL);
1473 g_return_if_fail (icon_set->ref_count > 0);
1475 icon_set->ref_count -= 1;
1477 if (icon_set->ref_count == 0)
1479 GSList *tmp_list = icon_set->sources;
1480 while (tmp_list != NULL)
1482 gtk_icon_source_free (tmp_list->data);
1484 tmp_list = g_slist_next (tmp_list);
1486 g_slist_free (icon_set->sources);
1488 clear_cache (icon_set, TRUE);
1495 gtk_icon_set_get_type (void)
1497 static GType our_type = 0;
1500 our_type = g_boxed_type_register_static ("GtkIconSet",
1501 (GBoxedCopyFunc) gtk_icon_set_ref,
1502 (GBoxedFreeFunc) gtk_icon_set_unref);
1508 * gtk_icon_set_copy:
1509 * @icon_set: a #GtkIconSet
1511 * Copies @icon_set by value.
1513 * Return value: a new #GtkIconSet identical to the first.
1516 gtk_icon_set_copy (GtkIconSet *icon_set)
1521 copy = gtk_icon_set_new ();
1523 tmp_list = icon_set->sources;
1524 while (tmp_list != NULL)
1526 copy->sources = g_slist_prepend (copy->sources,
1527 gtk_icon_source_copy (tmp_list->data));
1529 tmp_list = g_slist_next (tmp_list);
1532 copy->sources = g_slist_reverse (copy->sources);
1534 copy->cache = copy_cache (icon_set, copy);
1535 copy->cache_size = icon_set->cache_size;
1536 copy->cache_serial = icon_set->cache_serial;
1542 sizes_equivalent (GtkIconSize lhs,
1545 /* We used to consider sizes equivalent if they were
1546 * the same pixel size, but we don't have the GtkSettings
1547 * here, so we can't do that. Plus, it's not clear that
1548 * it is right... it was just a workaround for the fact
1549 * that we register icons by logical size, not pixel size.
1555 gint r_w, r_h, l_w, l_h;
1557 icon_size_lookup_intern (NULL, rhs, &r_w, &r_h);
1558 icon_size_lookup_intern (NULL, lhs, &l_w, &l_h);
1560 return r_w == l_w && r_h == l_h;
1564 static GtkIconSource *
1565 find_best_matching_source (GtkIconSet *icon_set,
1566 GtkTextDirection direction,
1571 GtkIconSource *source;
1574 /* We need to find the best icon source. Direction matters more
1575 * than state, state matters more than size. icon_set->sources
1576 * is sorted according to wildness, so if we take the first
1577 * match we find it will be the least-wild match (if there are
1578 * multiple matches for a given "wildness" then the RC file contained
1579 * dumb stuff, and we end up with an arbitrary matching source)
1583 tmp_list = icon_set->sources;
1584 while (tmp_list != NULL)
1586 GtkIconSource *s = tmp_list->data;
1588 if ((s->any_direction || (s->direction == direction)) &&
1589 (s->any_state || (s->state == state)) &&
1590 (s->any_size || (sizes_equivalent (size, s->size))))
1592 if (!g_slist_find (failed, s))
1599 tmp_list = g_slist_next (tmp_list);
1606 ensure_filename_pixbuf (GtkIconSet *icon_set,
1607 GtkIconSource *source)
1609 if (source->filename_pixbuf == NULL)
1611 GError *error = NULL;
1613 source->filename_pixbuf = gdk_pixbuf_new_from_file (source->source.filename, &error);
1615 if (source->filename_pixbuf == NULL)
1617 /* Remove this icon source so we don't keep trying to
1620 g_warning (_("Error loading icon: %s"), error->message);
1621 g_error_free (error);
1623 icon_set->sources = g_slist_remove (icon_set->sources, source);
1625 gtk_icon_source_free (source);
1635 render_icon_name_pixbuf (GtkIconSource *icon_source,
1637 GtkTextDirection direction,
1644 GdkPixbuf *tmp_pixbuf;
1645 GtkIconSource tmp_source;
1647 GtkIconTheme *icon_theme;
1648 GtkSettings *settings;
1649 gint width, height, pixel_size;
1650 GError *error = NULL;
1652 if (widget && gtk_widget_has_screen (widget))
1653 screen = gtk_widget_get_screen (widget);
1654 else if (style && style->colormap)
1655 screen = gdk_colormap_get_screen (style->colormap);
1658 screen = gdk_screen_get_default ();
1659 GTK_NOTE (MULTIHEAD,
1660 g_warning ("Using the default screen for gtk_icon_source_render_icon()"));
1663 icon_theme = gtk_icon_theme_get_for_screen (screen);
1664 settings = gtk_settings_get_for_screen (screen);
1666 if (!gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
1668 g_warning ("Invalid icon size %d\n", size);
1669 width = height = 24;
1672 pixel_size = MIN (width, height);
1674 tmp_pixbuf = gtk_icon_theme_load_icon (icon_theme,
1675 icon_source->source.icon_name,
1681 g_warning ("Error loading theme icon for stock: %s", error->message);
1682 g_error_free (error);
1686 tmp_source = *icon_source;
1687 tmp_source.type = GTK_ICON_SOURCE_PIXBUF;
1688 tmp_source.source.pixbuf = tmp_pixbuf;
1690 pixbuf = gtk_style_render_icon (style, &tmp_source,
1691 direction, state, -1,
1695 g_warning ("Failed to render icon");
1697 g_object_unref (tmp_pixbuf);
1703 find_and_render_icon_source (GtkIconSet *icon_set,
1705 GtkTextDirection direction,
1711 GSList *failed = NULL;
1712 GdkPixbuf *pixbuf = NULL;
1714 /* We treat failure in two different ways:
1716 * A) If loading a source that specifies a filename fails,
1717 * we treat that as permanent, and remove the source
1718 * from the GtkIconSet. (in ensure_filename_pixbuf ()
1719 * B) If loading a themed icon fails, or scaling an icon
1720 * fails, we treat that as transient and will try
1721 * again next time the icon falls out of the cache
1722 * and we need to recreate it.
1724 while (pixbuf == NULL)
1726 GtkIconSource *source = find_best_matching_source (icon_set, direction, state, size, failed);
1731 switch (source->type)
1733 case GTK_ICON_SOURCE_FILENAME:
1734 if (!ensure_filename_pixbuf (icon_set, source))
1737 case GTK_ICON_SOURCE_PIXBUF:
1738 pixbuf = gtk_style_render_icon (style, source,
1739 direction, state, size,
1743 g_warning ("Failed to render icon");
1744 failed = g_slist_prepend (failed, source);
1747 case GTK_ICON_SOURCE_ICON_NAME:
1748 pixbuf = render_icon_name_pixbuf (source, style,
1749 direction, state, size,
1752 failed = g_slist_prepend (failed, source);
1754 case GTK_ICON_SOURCE_EMPTY:
1755 g_assert_not_reached ();
1759 g_slist_free (failed);
1765 render_fallback_image (GtkStyle *style,
1766 GtkTextDirection direction,
1772 /* This icon can be used for any direction/state/size */
1773 static GtkIconSource fallback_source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1775 if (fallback_source.type == GTK_ICON_SOURCE_EMPTY)
1777 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_inline (-1, stock_missing_image_24, FALSE, NULL);
1778 gtk_icon_source_set_pixbuf (&fallback_source, pixbuf);
1779 g_object_unref (pixbuf);
1782 return gtk_style_render_icon (style,
1792 * gtk_icon_set_render_icon:
1793 * @icon_set: a #GtkIconSet
1794 * @style: a #GtkStyle associated with @widget, or %NULL
1795 * @direction: text direction
1796 * @state: widget state
1797 * @size: icon size. A size of (GtkIconSize)-1
1798 * means render at the size of the source and don't scale.
1799 * @widget: widget that will display the icon, or %NULL.
1800 * The only use that is typically made of this
1801 * is to determine the appropriate #GdkScreen.
1802 * @detail: detail to pass to the theme engine, or %NULL.
1803 * Note that passing a detail of anything but %NULL
1804 * will disable caching.
1806 * Renders an icon using gtk_style_render_icon(). In most cases,
1807 * gtk_widget_render_icon() is better, since it automatically provides
1808 * most of the arguments from the current widget settings. This
1809 * function never returns %NULL; if the icon can't be rendered
1810 * (perhaps because an image file fails to load), a default "missing
1811 * image" icon will be returned instead.
1813 * Return value: a #GdkPixbuf to be displayed
1816 gtk_icon_set_render_icon (GtkIconSet *icon_set,
1818 GtkTextDirection direction,
1826 g_return_val_if_fail (icon_set != NULL, NULL);
1827 g_return_val_if_fail (style == NULL || GTK_IS_STYLE (style), NULL);
1829 if (icon_set->sources == NULL)
1830 return render_fallback_image (style, direction, state, size, widget, detail);
1834 icon = find_in_cache (icon_set, style, direction,
1839 g_object_ref (icon);
1845 icon = find_and_render_icon_source (icon_set, style, direction, state, size,
1849 icon = render_fallback_image (style, direction, state, size, widget, detail);
1852 add_to_cache (icon_set, style, direction, state, size, icon);
1857 /* Order sources by their "wildness", so that "wilder" sources are
1858 * greater than "specific" sources; for determining ordering,
1859 * direction beats state beats size.
1863 icon_source_compare (gconstpointer ap, gconstpointer bp)
1865 const GtkIconSource *a = ap;
1866 const GtkIconSource *b = bp;
1868 if (!a->any_direction && b->any_direction)
1870 else if (a->any_direction && !b->any_direction)
1872 else if (!a->any_state && b->any_state)
1874 else if (a->any_state && !b->any_state)
1876 else if (!a->any_size && b->any_size)
1878 else if (a->any_size && !b->any_size)
1885 * gtk_icon_set_add_source:
1886 * @icon_set: a #GtkIconSet
1887 * @source: a #GtkIconSource
1889 * Icon sets have a list of #GtkIconSource, which they use as base
1890 * icons for rendering icons in different states and sizes. Icons are
1891 * scaled, made to look insensitive, etc. in
1892 * gtk_icon_set_render_icon(), but #GtkIconSet needs base images to
1893 * work with. The base images and when to use them are described by
1896 * This function copies @source, so you can reuse the same source immediately
1897 * without affecting the icon set.
1899 * An example of when you'd use this function: a web browser's "Back
1900 * to Previous Page" icon might point in a different direction in
1901 * Hebrew and in English; it might look different when insensitive;
1902 * and it might change size depending on toolbar mode (small/large
1903 * icons). So a single icon set would contain all those variants of
1904 * the icon, and you might add a separate source for each one.
1906 * You should nearly always add a "default" icon source with all
1907 * fields wildcarded, which will be used as a fallback if no more
1908 * specific source matches. #GtkIconSet always prefers more specific
1909 * icon sources to more generic icon sources. The order in which you
1910 * add the sources to the icon set does not matter.
1912 * gtk_icon_set_new_from_pixbuf() creates a new icon set with a
1913 * default icon source based on the given pixbuf.
1917 gtk_icon_set_add_source (GtkIconSet *icon_set,
1918 const GtkIconSource *source)
1920 g_return_if_fail (icon_set != NULL);
1921 g_return_if_fail (source != NULL);
1923 if (source->type == GTK_ICON_SOURCE_EMPTY)
1925 g_warning ("Useless empty GtkIconSource");
1929 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
1930 gtk_icon_source_copy (source),
1931 icon_source_compare);
1935 * gtk_icon_set_get_sizes:
1936 * @icon_set: a #GtkIconSet
1937 * @sizes: return location for array of sizes
1938 * @n_sizes: location to store number of elements in returned array
1940 * Obtains a list of icon sizes this icon set can render. The returned
1941 * array must be freed with g_free().
1945 gtk_icon_set_get_sizes (GtkIconSet *icon_set,
1946 GtkIconSize **sizes,
1950 gboolean all_sizes = FALSE;
1951 GSList *specifics = NULL;
1953 g_return_if_fail (icon_set != NULL);
1954 g_return_if_fail (sizes != NULL);
1955 g_return_if_fail (n_sizes != NULL);
1957 tmp_list = icon_set->sources;
1958 while (tmp_list != NULL)
1960 GtkIconSource *source;
1962 source = tmp_list->data;
1964 if (source->any_size)
1970 specifics = g_slist_prepend (specifics, GINT_TO_POINTER (source->size));
1972 tmp_list = g_slist_next (tmp_list);
1977 /* Need to find out what sizes exist */
1982 *sizes = g_new (GtkIconSize, icon_sizes_used);
1983 *n_sizes = icon_sizes_used - 1;
1986 while (i < icon_sizes_used)
1988 (*sizes)[i - 1] = icon_sizes[i].size;
1996 *n_sizes = g_slist_length (specifics);
1997 *sizes = g_new (GtkIconSize, *n_sizes);
2000 tmp_list = specifics;
2001 while (tmp_list != NULL)
2003 (*sizes)[i] = GPOINTER_TO_INT (tmp_list->data);
2006 tmp_list = g_slist_next (tmp_list);
2010 g_slist_free (specifics);
2015 * gtk_icon_source_new:
2017 * Creates a new #GtkIconSource. A #GtkIconSource contains a #GdkPixbuf (or
2018 * image filename) that serves as the base image for one or more of the
2019 * icons in a #GtkIconSet, along with a specification for which icons in the
2020 * icon set will be based on that pixbuf or image file. An icon set contains
2021 * a set of icons that represent "the same" logical concept in different states,
2022 * different global text directions, and different sizes.
2024 * So for example a web browser's "Back to Previous Page" icon might
2025 * point in a different direction in Hebrew and in English; it might
2026 * look different when insensitive; and it might change size depending
2027 * on toolbar mode (small/large icons). So a single icon set would
2028 * contain all those variants of the icon. #GtkIconSet contains a list
2029 * of #GtkIconSource from which it can derive specific icon variants in
2032 * In the simplest case, #GtkIconSet contains one source pixbuf from
2033 * which it derives all variants. The convenience function
2034 * gtk_icon_set_new_from_pixbuf() handles this case; if you only have
2035 * one source pixbuf, just use that function.
2037 * If you want to use a different base pixbuf for different icon
2038 * variants, you create multiple icon sources, mark which variants
2039 * they'll be used to create, and add them to the icon set with
2040 * gtk_icon_set_add_source().
2042 * By default, the icon source has all parameters wildcarded. That is,
2043 * the icon source will be used as the base icon for any desired text
2044 * direction, widget state, or icon size.
2046 * Return value: a new #GtkIconSource
2049 gtk_icon_source_new (void)
2053 src = g_new0 (GtkIconSource, 1);
2055 src->direction = GTK_TEXT_DIR_NONE;
2056 src->size = GTK_ICON_SIZE_INVALID;
2057 src->state = GTK_STATE_NORMAL;
2059 src->any_direction = TRUE;
2060 src->any_state = TRUE;
2061 src->any_size = TRUE;
2067 * gtk_icon_source_copy:
2068 * @source: a #GtkIconSource
2070 * Creates a copy of @source; mostly useful for language bindings.
2072 * Return value: a new #GtkIconSource
2075 gtk_icon_source_copy (const GtkIconSource *source)
2077 GtkIconSource *copy;
2079 g_return_val_if_fail (source != NULL, NULL);
2081 copy = g_new (GtkIconSource, 1);
2087 case GTK_ICON_SOURCE_EMPTY:
2089 case GTK_ICON_SOURCE_ICON_NAME:
2090 copy->source.icon_name = g_strdup (copy->source.icon_name);
2092 case GTK_ICON_SOURCE_FILENAME:
2093 copy->source.filename = g_strdup (copy->source.filename);
2094 if (copy->filename_pixbuf)
2095 g_object_ref (copy->filename_pixbuf);
2097 case GTK_ICON_SOURCE_PIXBUF:
2098 g_object_ref (copy->source.pixbuf);
2101 g_assert_not_reached();
2108 * gtk_icon_source_free:
2109 * @source: a #GtkIconSource
2111 * Frees a dynamically-allocated icon source, along with its
2112 * filename, size, and pixbuf fields if those are not %NULL.
2115 gtk_icon_source_free (GtkIconSource *source)
2117 g_return_if_fail (source != NULL);
2119 icon_source_clear (source);
2124 gtk_icon_source_get_type (void)
2126 static GType our_type = 0;
2129 our_type = g_boxed_type_register_static ("GtkIconSource",
2130 (GBoxedCopyFunc) gtk_icon_source_copy,
2131 (GBoxedFreeFunc) gtk_icon_source_free);
2137 icon_source_clear (GtkIconSource *source)
2139 switch (source->type)
2141 case GTK_ICON_SOURCE_EMPTY:
2143 case GTK_ICON_SOURCE_ICON_NAME:
2144 g_free (source->source.icon_name);
2145 source->source.icon_name = NULL;
2147 case GTK_ICON_SOURCE_FILENAME:
2148 g_free (source->source.filename);
2149 source->source.filename = NULL;
2150 if (source->filename_pixbuf)
2151 g_object_unref (source->filename_pixbuf);
2152 source->filename_pixbuf = NULL;
2154 case GTK_ICON_SOURCE_PIXBUF:
2155 g_object_unref (source->source.pixbuf);
2156 source->source.pixbuf = NULL;
2159 g_assert_not_reached();
2162 source->type = GTK_ICON_SOURCE_EMPTY;
2166 * gtk_icon_source_set_filename:
2167 * @source: a #GtkIconSource
2168 * @filename: image file to use
2170 * Sets the name of an image file to use as a base image when creating
2171 * icon variants for #GtkIconSet. The filename must be absolute.
2174 gtk_icon_source_set_filename (GtkIconSource *source,
2175 const gchar *filename)
2177 g_return_if_fail (source != NULL);
2178 g_return_if_fail (filename == NULL || g_path_is_absolute (filename));
2180 if (source->type == GTK_ICON_SOURCE_FILENAME &&
2181 source->source.filename == filename)
2184 icon_source_clear (source);
2186 if (filename != NULL)
2188 source->type = GTK_ICON_SOURCE_FILENAME;
2189 source->source.filename = g_strdup (filename);
2194 * gtk_icon_source_set_icon_name
2195 * @source: a #GtkIconSource
2196 * @icon_name: name of icon to use
2198 * Sets the name of an icon to look up in the current icon theme
2199 * to use as a base image when creating icon variants for #GtkIconSet.
2202 gtk_icon_source_set_icon_name (GtkIconSource *source,
2203 const gchar *icon_name)
2205 g_return_if_fail (source != NULL);
2207 if (source->type == GTK_ICON_SOURCE_ICON_NAME &&
2208 source->source.icon_name == icon_name)
2211 icon_source_clear (source);
2213 if (icon_name != NULL)
2215 source->type = GTK_ICON_SOURCE_ICON_NAME;
2216 source->source.icon_name = g_strdup (icon_name);
2221 * gtk_icon_source_set_pixbuf:
2222 * @source: a #GtkIconSource
2223 * @pixbuf: pixbuf to use as a source
2225 * Sets a pixbuf to use as a base image when creating icon variants
2229 gtk_icon_source_set_pixbuf (GtkIconSource *source,
2232 g_return_if_fail (source != NULL);
2233 g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
2235 if (source->type == GTK_ICON_SOURCE_PIXBUF &&
2236 source->source.pixbuf == pixbuf)
2239 icon_source_clear (source);
2243 source->type = GTK_ICON_SOURCE_PIXBUF;
2244 source->source.pixbuf = g_object_ref (pixbuf);
2249 * gtk_icon_source_get_filename:
2250 * @source: a #GtkIconSource
2252 * Retrieves the source filename, or %NULL if none is set. The
2253 * filename is not a copy, and should not be modified or expected to
2254 * persist beyond the lifetime of the icon source.
2256 * Return value: image filename. This string must not be modified
2259 G_CONST_RETURN gchar*
2260 gtk_icon_source_get_filename (const GtkIconSource *source)
2262 g_return_val_if_fail (source != NULL, NULL);
2264 if (source->type == GTK_ICON_SOURCE_FILENAME)
2265 return source->source.filename;
2271 * gtk_icon_source_get_icon_name:
2272 * @source: a #GtkIconSource
2274 * Retrieves the source icon name, or %NULL if none is set. The
2275 * icon_name is not a copy, and should not be modified or expected to
2276 * persist beyond the lifetime of the icon source.
2278 * Return value: icon name. This string must not be modified or freed.
2280 G_CONST_RETURN gchar*
2281 gtk_icon_source_get_icon_name (const GtkIconSource *source)
2283 g_return_val_if_fail (source != NULL, NULL);
2285 if (source->type == GTK_ICON_SOURCE_ICON_NAME)
2286 return source->source.icon_name;
2292 * gtk_icon_source_get_pixbuf:
2293 * @source: a #GtkIconSource
2295 * Retrieves the source pixbuf, or %NULL if none is set.
2296 * In addition, if a filename source is in use, this
2297 * function in some cases will return the pixbuf from
2298 * loaded from the filename. This is, for example, true
2299 * for the GtkIconSource passed to the GtkStyle::render_icon()
2300 * virtual function. The reference count on the pixbuf is
2303 * Return value: source pixbuf
2306 gtk_icon_source_get_pixbuf (const GtkIconSource *source)
2308 g_return_val_if_fail (source != NULL, NULL);
2310 if (source->type == GTK_ICON_SOURCE_PIXBUF)
2311 return source->source.pixbuf;
2312 else if (source->type == GTK_ICON_SOURCE_FILENAME)
2313 return source->filename_pixbuf;
2319 * gtk_icon_source_set_direction_wildcarded:
2320 * @source: a #GtkIconSource
2321 * @setting: %TRUE to wildcard the text direction
2323 * If the text direction is wildcarded, this source can be used
2324 * as the base image for an icon in any #GtkTextDirection.
2325 * If the text direction is not wildcarded, then the
2326 * text direction the icon source applies to should be set
2327 * with gtk_icon_source_set_direction(), and the icon source
2328 * will only be used with that text direction.
2330 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2331 * wildcarded sources, and will use an exact match when possible.
2335 gtk_icon_source_set_direction_wildcarded (GtkIconSource *source,
2338 g_return_if_fail (source != NULL);
2340 source->any_direction = setting != FALSE;
2344 * gtk_icon_source_set_state_wildcarded:
2345 * @source: a #GtkIconSource
2346 * @setting: %TRUE to wildcard the widget state
2348 * If the widget state is wildcarded, this source can be used as the
2349 * base image for an icon in any #GtkStateType. If the widget state
2350 * is not wildcarded, then the state the source applies to should be
2351 * set with gtk_icon_source_set_state() and the icon source will
2352 * only be used with that specific state.
2354 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2355 * wildcarded sources, and will use an exact match when possible.
2357 * #GtkIconSet will normally transform wildcarded source images to
2358 * produce an appropriate icon for a given state, for example
2359 * lightening an image on prelight, but will not modify source images
2360 * that match exactly.
2363 gtk_icon_source_set_state_wildcarded (GtkIconSource *source,
2366 g_return_if_fail (source != NULL);
2368 source->any_state = setting != FALSE;
2373 * gtk_icon_source_set_size_wildcarded:
2374 * @source: a #GtkIconSource
2375 * @setting: %TRUE to wildcard the widget state
2377 * If the icon size is wildcarded, this source can be used as the base
2378 * image for an icon of any size. If the size is not wildcarded, then
2379 * the size the source applies to should be set with
2380 * gtk_icon_source_set_size() and the icon source will only be used
2381 * with that specific size.
2383 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2384 * wildcarded sources, and will use an exact match when possible.
2386 * #GtkIconSet will normally scale wildcarded source images to produce
2387 * an appropriate icon at a given size, but will not change the size
2388 * of source images that match exactly.
2391 gtk_icon_source_set_size_wildcarded (GtkIconSource *source,
2394 g_return_if_fail (source != NULL);
2396 source->any_size = setting != FALSE;
2400 * gtk_icon_source_get_size_wildcarded:
2401 * @source: a #GtkIconSource
2403 * Gets the value set by gtk_icon_source_set_size_wildcarded().
2405 * Return value: %TRUE if this icon source is a base for any icon size variant
2408 gtk_icon_source_get_size_wildcarded (const GtkIconSource *source)
2410 g_return_val_if_fail (source != NULL, TRUE);
2412 return source->any_size;
2416 * gtk_icon_source_get_state_wildcarded:
2417 * @source: a #GtkIconSource
2419 * Gets the value set by gtk_icon_source_set_state_wildcarded().
2421 * Return value: %TRUE if this icon source is a base for any widget state variant
2424 gtk_icon_source_get_state_wildcarded (const GtkIconSource *source)
2426 g_return_val_if_fail (source != NULL, TRUE);
2428 return source->any_state;
2432 * gtk_icon_source_get_direction_wildcarded:
2433 * @source: a #GtkIconSource
2435 * Gets the value set by gtk_icon_source_set_direction_wildcarded().
2437 * Return value: %TRUE if this icon source is a base for any text direction variant
2440 gtk_icon_source_get_direction_wildcarded (const GtkIconSource *source)
2442 g_return_val_if_fail (source != NULL, TRUE);
2444 return source->any_direction;
2448 * gtk_icon_source_set_direction:
2449 * @source: a #GtkIconSource
2450 * @direction: text direction this source applies to
2452 * Sets the text direction this icon source is intended to be used
2455 * Setting the text direction on an icon source makes no difference
2456 * if the text direction is wildcarded. Therefore, you should usually
2457 * call gtk_icon_source_set_direction_wildcarded() to un-wildcard it
2458 * in addition to calling this function.
2462 gtk_icon_source_set_direction (GtkIconSource *source,
2463 GtkTextDirection direction)
2465 g_return_if_fail (source != NULL);
2467 source->direction = direction;
2471 * gtk_icon_source_set_state:
2472 * @source: a #GtkIconSource
2473 * @state: widget state this source applies to
2475 * Sets the widget state this icon source is intended to be used
2478 * Setting the widget state on an icon source makes no difference
2479 * if the state is wildcarded. Therefore, you should usually
2480 * call gtk_icon_source_set_state_wildcarded() to un-wildcard it
2481 * in addition to calling this function.
2485 gtk_icon_source_set_state (GtkIconSource *source,
2488 g_return_if_fail (source != NULL);
2490 source->state = state;
2494 * gtk_icon_source_set_size:
2495 * @source: a #GtkIconSource
2496 * @size: icon size this source applies to
2498 * Sets the icon size this icon source is intended to be used
2501 * Setting the icon size on an icon source makes no difference
2502 * if the size is wildcarded. Therefore, you should usually
2503 * call gtk_icon_source_set_size_wildcarded() to un-wildcard it
2504 * in addition to calling this function.
2508 gtk_icon_source_set_size (GtkIconSource *source,
2511 g_return_if_fail (source != NULL);
2513 source->size = size;
2517 * gtk_icon_source_get_direction:
2518 * @source: a #GtkIconSource
2520 * Obtains the text direction this icon source applies to. The return
2521 * value is only useful/meaningful if the text direction is <emphasis>not</emphasis>
2524 * Return value: text direction this source matches
2527 gtk_icon_source_get_direction (const GtkIconSource *source)
2529 g_return_val_if_fail (source != NULL, 0);
2531 return source->direction;
2535 * gtk_icon_source_get_state:
2536 * @source: a #GtkIconSource
2538 * Obtains the widget state this icon source applies to. The return
2539 * value is only useful/meaningful if the widget state is <emphasis>not</emphasis>
2542 * Return value: widget state this source matches
2545 gtk_icon_source_get_state (const GtkIconSource *source)
2547 g_return_val_if_fail (source != NULL, 0);
2549 return source->state;
2553 * gtk_icon_source_get_size:
2554 * @source: a #GtkIconSource
2556 * Obtains the icon size this source applies to. The return value
2557 * is only useful/meaningful if the icon size is <emphasis>not</emphasis> wildcarded.
2559 * Return value: icon size this source matches.
2562 gtk_icon_source_get_size (const GtkIconSource *source)
2564 g_return_val_if_fail (source != NULL, 0);
2566 return source->size;
2569 #define NUM_CACHED_ICONS 8
2571 typedef struct _CachedIcon CachedIcon;
2575 /* These must all match to use the cached pixbuf.
2576 * If any don't match, we must re-render the pixbuf.
2579 GtkTextDirection direction;
2587 ensure_cache_up_to_date (GtkIconSet *icon_set)
2589 if (icon_set->cache_serial != cache_serial)
2591 clear_cache (icon_set, TRUE);
2592 icon_set->cache_serial = cache_serial;
2597 cached_icon_free (CachedIcon *icon)
2599 g_object_unref (icon->pixbuf);
2602 g_object_unref (icon->style);
2608 find_in_cache (GtkIconSet *icon_set,
2610 GtkTextDirection direction,
2617 ensure_cache_up_to_date (icon_set);
2620 tmp_list = icon_set->cache;
2621 while (tmp_list != NULL)
2623 CachedIcon *icon = tmp_list->data;
2625 if (icon->style == style &&
2626 icon->direction == direction &&
2627 icon->state == state &&
2632 /* Move this icon to the front of the list. */
2633 prev->next = tmp_list->next;
2634 tmp_list->next = icon_set->cache;
2635 icon_set->cache = tmp_list;
2638 return icon->pixbuf;
2642 tmp_list = g_slist_next (tmp_list);
2649 add_to_cache (GtkIconSet *icon_set,
2651 GtkTextDirection direction,
2658 ensure_cache_up_to_date (icon_set);
2660 g_object_ref (pixbuf);
2662 /* We have to ref the style, since if the style was finalized
2663 * its address could be reused by another style, creating a
2668 g_object_ref (style);
2670 icon = g_new (CachedIcon, 1);
2671 icon_set->cache = g_slist_prepend (icon_set->cache, icon);
2672 icon_set->cache_size++;
2674 icon->style = style;
2675 icon->direction = direction;
2676 icon->state = state;
2678 icon->pixbuf = pixbuf;
2681 attach_to_style (icon_set, icon->style);
2683 if (icon_set->cache_size >= NUM_CACHED_ICONS)
2685 /* Remove oldest item in the cache */
2688 tmp_list = icon_set->cache;
2690 /* Find next-to-last link */
2691 g_assert (NUM_CACHED_ICONS > 2);
2692 while (tmp_list->next->next)
2693 tmp_list = tmp_list->next;
2695 g_assert (tmp_list != NULL);
2696 g_assert (tmp_list->next != NULL);
2697 g_assert (tmp_list->next->next == NULL);
2699 /* Free the last icon */
2700 icon = tmp_list->next->data;
2702 g_slist_free (tmp_list->next);
2703 tmp_list->next = NULL;
2705 cached_icon_free (icon);
2710 clear_cache (GtkIconSet *icon_set,
2711 gboolean style_detach)
2713 GSList *cache, *tmp_list;
2714 GtkStyle *last_style = NULL;
2716 cache = icon_set->cache;
2717 icon_set->cache = NULL;
2718 icon_set->cache_size = 0;
2720 while (tmp_list != NULL)
2722 CachedIcon *icon = tmp_list->data;
2726 /* simple optimization for the case where the cache
2727 * contains contiguous icons from the same style.
2728 * it's safe to call detach_from_style more than
2729 * once on the same style though.
2731 if (last_style != icon->style)
2733 detach_from_style (icon_set, icon->style);
2734 last_style = icon->style;
2738 cached_icon_free (icon);
2740 tmp_list = g_slist_next (tmp_list);
2743 g_slist_free (cache);
2747 copy_cache (GtkIconSet *icon_set,
2748 GtkIconSet *copy_recipient)
2751 GSList *copy = NULL;
2753 ensure_cache_up_to_date (icon_set);
2755 tmp_list = icon_set->cache;
2756 while (tmp_list != NULL)
2758 CachedIcon *icon = tmp_list->data;
2759 CachedIcon *icon_copy = g_new (CachedIcon, 1);
2763 if (icon_copy->style)
2765 attach_to_style (copy_recipient, icon_copy->style);
2766 g_object_ref (icon_copy->style);
2769 g_object_ref (icon_copy->pixbuf);
2771 icon_copy->size = icon->size;
2773 copy = g_slist_prepend (copy, icon_copy);
2775 tmp_list = g_slist_next (tmp_list);
2778 return g_slist_reverse (copy);
2782 attach_to_style (GtkIconSet *icon_set,
2787 table = g_object_get_qdata (G_OBJECT (style),
2788 g_quark_try_string ("gtk-style-icon-sets"));
2792 table = g_hash_table_new (NULL, NULL);
2793 g_object_set_qdata_full (G_OBJECT (style),
2794 g_quark_from_static_string ("gtk-style-icon-sets"),
2799 g_hash_table_insert (table, icon_set, icon_set);
2803 detach_from_style (GtkIconSet *icon_set,
2808 table = g_object_get_qdata (G_OBJECT (style),
2809 g_quark_try_string ("gtk-style-icon-sets"));
2812 g_hash_table_remove (table, icon_set);
2816 iconsets_foreach (gpointer key,
2820 GtkIconSet *icon_set = key;
2822 /* We only need to remove cache entries for the given style;
2823 * but that complicates things because in destroy notify
2824 * we don't know which style got destroyed, and 95% of the
2825 * time all cache entries will have the same style,
2826 * so this is faster anyway.
2829 clear_cache (icon_set, FALSE);
2833 style_dnotify (gpointer data)
2835 GHashTable *table = data;
2837 g_hash_table_foreach (table, iconsets_foreach, NULL);
2839 g_hash_table_destroy (table);
2842 /* This allows the icon set to detect that its cache is out of date. */
2844 _gtk_icon_set_invalidate_caches (void)
2850 listify_foreach (gpointer key, gpointer value, gpointer data)
2852 GSList **list = data;
2854 *list = g_slist_prepend (*list, key);
2858 g_hash_table_get_keys (GHashTable *table)
2860 GSList *list = NULL;
2862 g_hash_table_foreach (table, listify_foreach, &list);
2868 * _gtk_icon_factory_list_ids:
2870 * Gets all known IDs stored in an existing icon factory.
2871 * The strings in the returned list aren't copied.
2872 * The list itself should be freed.
2874 * Return value: List of ids in icon factories
2877 _gtk_icon_factory_list_ids (void)
2884 ensure_default_icons ();
2886 tmp_list = all_icon_factories;
2887 while (tmp_list != NULL)
2891 GtkIconFactory *factory = GTK_ICON_FACTORY (tmp_list->data);
2893 these_ids = g_hash_table_get_keys (factory->icons);
2895 ids = g_slist_concat (ids, these_ids);
2897 tmp_list = g_slist_next (tmp_list);