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/.
32 #include <pango/pango-utils.h> /* For pango_scan_* */
33 #include "gtkiconfactory.h"
34 #include "stock-icons/gtkstockpixbufs.h"
36 #include "gtkicontheme.h"
37 #include "gtksettings.h"
39 #include "gtkwidget.h"
43 static GSList *all_icon_factories = NULL;
46 GTK_ICON_SOURCE_EMPTY,
47 GTK_ICON_SOURCE_ICON_NAME,
48 GTK_ICON_SOURCE_FILENAME,
49 GTK_ICON_SOURCE_PIXBUF
54 GtkIconSourceType type;
62 GdkPixbuf *filename_pixbuf;
64 GtkTextDirection direction;
68 /* If TRUE, then the parameter is wildcarded, and the above
69 * fields should be ignored. If FALSE, the parameter is
70 * specified, and the above fields should be valid.
72 guint any_direction : 1;
77 static gpointer parent_class = NULL;
79 static void gtk_icon_factory_init (GtkIconFactory *icon_factory);
80 static void gtk_icon_factory_class_init (GtkIconFactoryClass *klass);
81 static void gtk_icon_factory_finalize (GObject *object);
82 static void get_default_icons (GtkIconFactory *icon_factory);
83 static void icon_source_clear (GtkIconSource *source);
85 static GtkIconSize icon_size_register_intern (const gchar *name,
89 #define GTK_ICON_SOURCE_INIT(any_direction, any_state, any_size) \
90 { GTK_ICON_SOURCE_EMPTY, { NULL }, NULL, \
92 any_direction, any_state, any_size }
95 gtk_icon_factory_get_type (void)
97 static GType icon_factory_type = 0;
99 if (!icon_factory_type)
101 static const GTypeInfo icon_factory_info =
103 sizeof (GtkIconFactoryClass),
104 NULL, /* base_init */
105 NULL, /* base_finalize */
106 (GClassInitFunc) gtk_icon_factory_class_init,
107 NULL, /* class_finalize */
108 NULL, /* class_data */
109 sizeof (GtkIconFactory),
111 (GInstanceInitFunc) gtk_icon_factory_init,
115 g_type_register_static (G_TYPE_OBJECT, "GtkIconFactory",
116 &icon_factory_info, 0);
119 return icon_factory_type;
123 gtk_icon_factory_init (GtkIconFactory *factory)
125 factory->icons = g_hash_table_new (g_str_hash, g_str_equal);
126 all_icon_factories = g_slist_prepend (all_icon_factories, factory);
130 gtk_icon_factory_class_init (GtkIconFactoryClass *klass)
132 GObjectClass *object_class = G_OBJECT_CLASS (klass);
134 parent_class = g_type_class_peek_parent (klass);
136 object_class->finalize = gtk_icon_factory_finalize;
140 free_icon_set (gpointer key, gpointer value, gpointer data)
143 gtk_icon_set_unref (value);
147 gtk_icon_factory_finalize (GObject *object)
149 GtkIconFactory *factory = GTK_ICON_FACTORY (object);
151 all_icon_factories = g_slist_remove (all_icon_factories, factory);
153 g_hash_table_foreach (factory->icons, free_icon_set, NULL);
155 g_hash_table_destroy (factory->icons);
157 G_OBJECT_CLASS (parent_class)->finalize (object);
161 * gtk_icon_factory_new:
163 * Creates a new #GtkIconFactory. An icon factory manages a collection
164 * of #GtkIconSet<!-- -->s; a #GtkIconSet manages a set of variants of a
165 * particular icon (i.e. a #GtkIconSet contains variants for different
166 * sizes and widget states). Icons in an icon factory are named by a
167 * stock ID, which is a simple string identifying the icon. Each
168 * #GtkStyle has a list of #GtkIconFactory<!-- -->s derived from the current
169 * theme; those icon factories are consulted first when searching for
170 * an icon. If the theme doesn't set a particular icon, GTK+ looks for
171 * the icon in a list of default icon factories, maintained by
172 * gtk_icon_factory_add_default() and
173 * gtk_icon_factory_remove_default(). Applications with icons should
174 * add a default icon factory with their icons, which will allow
175 * themes to override the icons for the application.
177 * Return value: a new #GtkIconFactory
180 gtk_icon_factory_new (void)
182 return g_object_new (GTK_TYPE_ICON_FACTORY, NULL);
186 * gtk_icon_factory_add:
187 * @factory: a #GtkIconFactory
188 * @stock_id: icon name
189 * @icon_set: icon set
191 * Adds the given @icon_set to the icon factory, under the name
192 * @stock_id. @stock_id should be namespaced for your application,
193 * e.g. "myapp-whatever-icon". Normally applications create a
194 * #GtkIconFactory, then add it to the list of default factories with
195 * gtk_icon_factory_add_default(). Then they pass the @stock_id to
196 * widgets such as #GtkImage to display the icon. Themes can provide
197 * an icon with the same name (such as "myapp-whatever-icon") to
198 * override your application's default icons. If an icon already
199 * existed in @factory for @stock_id, it is unreferenced and replaced
200 * with the new @icon_set.
204 gtk_icon_factory_add (GtkIconFactory *factory,
205 const gchar *stock_id,
206 GtkIconSet *icon_set)
208 gpointer old_key = NULL;
209 gpointer old_value = NULL;
211 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
212 g_return_if_fail (stock_id != NULL);
213 g_return_if_fail (icon_set != NULL);
215 g_hash_table_lookup_extended (factory->icons, stock_id,
216 &old_key, &old_value);
218 if (old_value == icon_set)
221 gtk_icon_set_ref (icon_set);
223 /* GHashTable key memory management is so fantastically broken. */
225 g_hash_table_insert (factory->icons, old_key, icon_set);
227 g_hash_table_insert (factory->icons, g_strdup (stock_id), icon_set);
230 gtk_icon_set_unref (old_value);
234 * gtk_icon_factory_lookup:
235 * @factory: a #GtkIconFactory
236 * @stock_id: an icon name
238 * Looks up @stock_id in the icon factory, returning an icon set
239 * if found, otherwise %NULL. For display to the user, you should
240 * use gtk_style_lookup_icon_set() on the #GtkStyle for the
241 * widget that will display the icon, instead of using this
242 * function directly, so that themes are taken into account.
244 * Return value: icon set of @stock_id.
247 gtk_icon_factory_lookup (GtkIconFactory *factory,
248 const gchar *stock_id)
250 g_return_val_if_fail (GTK_IS_ICON_FACTORY (factory), NULL);
251 g_return_val_if_fail (stock_id != NULL, NULL);
253 return g_hash_table_lookup (factory->icons, stock_id);
256 static GtkIconFactory *gtk_default_icons = NULL;
257 static GSList *default_factories = NULL;
260 * gtk_icon_factory_add_default:
261 * @factory: a #GtkIconFactory
263 * Adds an icon factory to the list of icon factories searched by
264 * gtk_style_lookup_icon_set(). This means that, for example,
265 * gtk_image_new_from_stock() will be able to find icons in @factory.
266 * There will normally be an icon factory added for each library or
267 * application that comes with icons. The default icon factories
268 * can be overridden by themes.
272 gtk_icon_factory_add_default (GtkIconFactory *factory)
274 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
276 g_object_ref (factory);
278 default_factories = g_slist_prepend (default_factories, factory);
282 * gtk_icon_factory_remove_default:
283 * @factory: a #GtkIconFactory previously added with gtk_icon_factory_add_default()
285 * Removes an icon factory from the list of default icon
286 * factories. Not normally used; you might use it for a library that
287 * can be unloaded or shut down.
291 gtk_icon_factory_remove_default (GtkIconFactory *factory)
293 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
295 default_factories = g_slist_remove (default_factories, factory);
297 g_object_unref (factory);
301 ensure_default_icons (void)
303 if (gtk_default_icons == NULL)
305 gtk_default_icons = gtk_icon_factory_new ();
307 get_default_icons (gtk_default_icons);
312 * gtk_icon_factory_lookup_default:
313 * @stock_id: an icon name
315 * Looks for an icon in the list of default icon factories. For
316 * display to the user, you should use gtk_style_lookup_icon_set() on
317 * the #GtkStyle for the widget that will display the icon, instead of
318 * using this function directly, so that themes are taken into
322 * Return value: a #GtkIconSet, or %NULL
325 gtk_icon_factory_lookup_default (const gchar *stock_id)
329 g_return_val_if_fail (stock_id != NULL, NULL);
331 tmp_list = default_factories;
332 while (tmp_list != NULL)
334 GtkIconSet *icon_set =
335 gtk_icon_factory_lookup (GTK_ICON_FACTORY (tmp_list->data),
341 tmp_list = g_slist_next (tmp_list);
344 ensure_default_icons ();
346 return gtk_icon_factory_lookup (gtk_default_icons, stock_id);
350 register_stock_icon (GtkIconFactory *factory,
351 const gchar *stock_id)
353 GtkIconSet *set = gtk_icon_set_new ();
354 GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
356 source.type = GTK_ICON_SOURCE_ICON_NAME;
357 source.source.icon_name = (gchar *)stock_id;
358 gtk_icon_set_add_source (set, &source);
360 gtk_icon_factory_add (factory, stock_id, set);
361 gtk_icon_set_unref (set);
365 register_bidi_stock_icon (GtkIconFactory *factory,
366 const gchar *stock_id,
367 const gchar *stock_id_ltr,
368 const gchar *stock_id_rtl)
370 GtkIconSet *set = gtk_icon_set_new ();
371 GtkIconSource source = GTK_ICON_SOURCE_INIT (FALSE, TRUE, TRUE);
373 source.type = GTK_ICON_SOURCE_ICON_NAME;
374 source.source.icon_name = (gchar *)stock_id_ltr;
375 source.direction = GTK_TEXT_DIR_LTR;
376 gtk_icon_set_add_source (set, &source);
378 source.type = GTK_ICON_SOURCE_ICON_NAME;
379 source.source.icon_name = (gchar *)stock_id_rtl;
380 source.direction = GTK_TEXT_DIR_RTL;
381 gtk_icon_set_add_source (set, &source);
383 gtk_icon_factory_add (factory, stock_id, set);
384 gtk_icon_set_unref (set);
388 add_default_image (const gchar *stock_id,
390 const guchar *inline_data)
392 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_inline (-1, inline_data, FALSE, NULL);
395 gtk_icon_theme_add_builtin_icon (stock_id, size, pixbuf);
397 g_object_unref (pixbuf);
401 add_icon (GtkIconFactory *factory,
402 const gchar *stock_id,
404 const guchar *inline_data)
406 register_stock_icon (factory, stock_id);
408 add_default_image (stock_id, size, inline_data);
412 add_icon2 (GtkIconFactory *factory,
413 const gchar *stock_id,
415 const guchar *inline_data1,
417 const guchar *inline_data2)
419 register_stock_icon (factory, stock_id);
421 add_default_image (stock_id, size1, inline_data1);
422 add_default_image (stock_id, size2, inline_data2);
426 add_icon_bidi2 (GtkIconFactory *factory,
427 const gchar *stock_id,
429 const guchar *inline_data_ltr1,
430 const guchar *inline_data_rtl1,
432 const guchar *inline_data_ltr2,
433 const guchar *inline_data_rtl2)
435 gchar *stock_id_ltr = g_strconcat (stock_id, "-ltr", NULL);
436 gchar *stock_id_rtl = g_strconcat (stock_id, "-rtl", NULL);
438 register_bidi_stock_icon (factory, stock_id,
439 stock_id_ltr, stock_id_rtl);
441 add_default_image (stock_id_ltr, size1, inline_data_ltr1);
442 add_default_image (stock_id_ltr, size2, inline_data_ltr2);
444 add_default_image (stock_id_rtl, size1, inline_data_rtl1);
445 add_default_image (stock_id_rtl, size2, inline_data_rtl2);
447 g_free (stock_id_ltr);
448 g_free (stock_id_rtl);
452 get_default_icons (GtkIconFactory *factory)
454 /* KEEP IN SYNC with gtkstock.c */
456 /* Have dialog size */
457 add_icon (factory, GTK_STOCK_DIALOG_AUTHENTICATION, 48, stock_dialog_authentication_48);
458 add_icon (factory, GTK_STOCK_DIALOG_ERROR, 48, stock_dialog_error_48);
459 add_icon (factory, GTK_STOCK_DIALOG_INFO, 48, stock_dialog_info_48);
460 add_icon (factory, GTK_STOCK_DIALOG_QUESTION, 48, stock_dialog_question_48);
461 add_icon (factory, GTK_STOCK_DIALOG_WARNING, 48, stock_dialog_warning_48);
464 add_icon (factory, GTK_STOCK_DND, 32, stock_dnd_32);
465 add_icon (factory, GTK_STOCK_DND_MULTIPLE, 32, stock_dnd_multiple_32);
467 /* Have button sizes */
468 add_icon (factory, GTK_STOCK_APPLY, 20, stock_apply_20);
469 add_icon (factory, GTK_STOCK_CANCEL, 20, stock_cancel_20);
470 add_icon (factory, GTK_STOCK_NO, 20, stock_no_20);
471 add_icon (factory, GTK_STOCK_OK, 20, stock_ok_20);
472 add_icon (factory, GTK_STOCK_YES, 20, stock_yes_20);
474 /* Generic + button sizes */
475 add_icon2 (factory, GTK_STOCK_CLOSE,
479 /* Generic + menu sizes */
480 add_icon2 (factory, GTK_STOCK_ADD,
484 add_icon2 (factory, GTK_STOCK_JUSTIFY_CENTER,
485 16, stock_align_center_16,
486 24, stock_align_center_24);
488 add_icon2 (factory, GTK_STOCK_JUSTIFY_FILL,
489 16, stock_align_justify_16,
490 24, stock_align_justify_24);
492 add_icon2 (factory, GTK_STOCK_JUSTIFY_LEFT,
493 16, stock_align_left_16,
494 24, stock_align_left_24);
496 add_icon2 (factory, GTK_STOCK_JUSTIFY_RIGHT,
497 16, stock_align_right_16,
498 24, stock_align_right_24);
500 add_icon2 (factory, GTK_STOCK_GOTO_BOTTOM,
502 24, stock_bottom_24);
504 add_icon2 (factory, GTK_STOCK_CDROM,
508 add_icon2 (factory, GTK_STOCK_CONVERT,
509 16, stock_convert_16,
510 24, stock_convert_24);
512 add_icon2 (factory, GTK_STOCK_COPY,
516 add_icon2 (factory, GTK_STOCK_CUT,
520 add_icon2 (factory, GTK_STOCK_GO_DOWN,
521 16, stock_down_arrow_16,
522 24, stock_down_arrow_24);
524 add_icon2 (factory, GTK_STOCK_EXECUTE,
528 add_icon2 (factory, GTK_STOCK_QUIT,
532 add_icon_bidi2 (factory, GTK_STOCK_GOTO_FIRST,
533 16, stock_first_16, stock_last_16,
534 24, stock_first_24, stock_last_24);
536 add_icon2 (factory, GTK_STOCK_SELECT_FONT,
540 add_icon2 (factory, GTK_STOCK_HARDDISK,
541 16, stock_harddisk_16,
542 24, stock_harddisk_24);
544 add_icon2 (factory, GTK_STOCK_HELP,
548 add_icon2 (factory, GTK_STOCK_HOME,
552 add_icon_bidi2 (factory, GTK_STOCK_JUMP_TO,
553 16, stock_jump_to_16, stock_jump_to_rtl_16,
554 24, stock_jump_to_24, stock_jump_to_rtl_24);
556 add_icon_bidi2 (factory, GTK_STOCK_GOTO_LAST,
557 16, stock_last_16, stock_first_16,
558 24, stock_last_24, stock_first_24);
560 add_icon_bidi2 (factory, GTK_STOCK_GO_BACK,
561 16, stock_left_arrow_16, stock_right_arrow_16,
562 24, stock_left_arrow_24, stock_right_arrow_24);
564 add_icon2 (factory, GTK_STOCK_MISSING_IMAGE,
565 16, stock_missing_image_16,
566 24, stock_missing_image_24);
568 add_icon2 (factory, GTK_STOCK_NETWORK,
569 16, stock_network_16,
570 24, stock_network_24);
572 add_icon2 (factory, GTK_STOCK_NEW,
576 add_icon2 (factory, GTK_STOCK_OPEN,
580 add_icon2 (factory, GTK_STOCK_PASTE,
584 add_icon2 (factory, GTK_STOCK_PREFERENCES,
585 16, stock_preferences_16,
586 24, stock_preferences_24);
588 add_icon2 (factory, GTK_STOCK_PRINT,
592 add_icon2 (factory, GTK_STOCK_PRINT_PREVIEW,
593 16, stock_print_preview_16,
594 24, stock_print_preview_24);
596 add_icon2 (factory, GTK_STOCK_PROPERTIES,
597 16, stock_properties_16,
598 24, stock_properties_24);
600 add_icon_bidi2 (factory, GTK_STOCK_REDO,
601 16, stock_redo_16, stock_redo_rtl_16,
602 24, stock_redo_24, stock_redo_rtl_24);
604 add_icon2 (factory, GTK_STOCK_REMOVE,
606 24, stock_remove_24);
608 add_icon2 (factory, GTK_STOCK_REFRESH,
609 16, stock_refresh_16,
610 24, stock_refresh_24);
612 add_icon_bidi2 (factory, GTK_STOCK_REVERT_TO_SAVED,
613 16, stock_revert_16, stock_revert_rtl_16,
614 24, stock_revert_24, stock_revert_rtl_24);
616 add_icon_bidi2 (factory, GTK_STOCK_GO_FORWARD,
617 16, stock_right_arrow_16, stock_left_arrow_16,
618 24, stock_right_arrow_24, stock_left_arrow_24);
620 add_icon2 (factory, GTK_STOCK_SAVE,
624 add_icon2 (factory, GTK_STOCK_FLOPPY,
628 add_icon2 (factory, GTK_STOCK_SAVE_AS,
629 16, stock_save_as_16,
630 24, stock_save_as_24);
632 add_icon2 (factory, GTK_STOCK_FIND,
634 24, stock_search_24);
636 add_icon2 (factory, GTK_STOCK_FIND_AND_REPLACE,
637 16, stock_search_replace_16,
638 24, stock_search_replace_24);
640 add_icon2 (factory, GTK_STOCK_SORT_DESCENDING,
641 16, stock_sort_descending_16,
642 24, stock_sort_descending_24);
644 add_icon2 (factory, GTK_STOCK_SORT_ASCENDING,
645 16, stock_sort_ascending_16,
646 24, stock_sort_ascending_24);
648 add_icon2 (factory, GTK_STOCK_SPELL_CHECK,
649 16, stock_spellcheck_16,
650 24, stock_spellcheck_24);
652 add_icon2 (factory, GTK_STOCK_STOP,
656 add_icon2 (factory, GTK_STOCK_BOLD,
657 16, stock_text_bold_16,
658 24, stock_text_bold_24);
660 add_icon2 (factory, GTK_STOCK_ITALIC,
661 16, stock_text_italic_16,
662 24, stock_text_italic_24);
664 add_icon2 (factory, GTK_STOCK_STRIKETHROUGH,
665 16, stock_text_strikethrough_16,
666 24, stock_text_strikethrough_24);
668 add_icon2 (factory, GTK_STOCK_UNDERLINE,
669 16, stock_text_underline_16,
670 24, stock_text_underline_24);
672 add_icon2 (factory, GTK_STOCK_INDENT,
673 16, stock_text_indent_16,
674 24, stock_text_indent_24);
676 add_icon2 (factory, GTK_STOCK_UNINDENT,
677 16, stock_text_unindent_16,
678 24, stock_text_unindent_24);
680 add_icon2 (factory, GTK_STOCK_GOTO_TOP,
684 add_icon2 (factory, GTK_STOCK_DELETE,
688 add_icon_bidi2 (factory, GTK_STOCK_UNDELETE,
689 16, stock_undelete_16, stock_undelete_rtl_16,
690 24, stock_undelete_24, stock_undelete_rtl_24);
692 add_icon_bidi2 (factory, GTK_STOCK_UNDO,
693 16, stock_undo_16, stock_undo_rtl_16,
694 24, stock_undo_24, stock_undo_rtl_24);
696 add_icon2 (factory, GTK_STOCK_GO_UP,
697 16, stock_up_arrow_16,
698 24, stock_up_arrow_24);
700 add_icon2 (factory, GTK_STOCK_FILE,
704 add_icon2 (factory, GTK_STOCK_DIRECTORY,
705 16, stock_directory_16,
706 24, stock_directory_24);
708 add_icon2 (factory, GTK_STOCK_ABOUT,
712 add_icon2 (factory, GTK_STOCK_CONNECT,
713 16, stock_connect_16,
714 24, stock_connect_24);
716 add_icon2 (factory, GTK_STOCK_DISCONNECT,
717 16, stock_disconnect_16,
718 24, stock_disconnect_24);
720 add_icon2 (factory, GTK_STOCK_EDIT,
724 add_icon2 (factory, GTK_STOCK_MEDIA_FORWARD,
725 16, stock_media_forward_16,
726 24, stock_media_forward_24);
728 add_icon2 (factory, GTK_STOCK_MEDIA_NEXT,
729 16, stock_media_next_16,
730 24, stock_media_next_24);
732 add_icon2 (factory, GTK_STOCK_MEDIA_PAUSE,
733 16, stock_media_pause_16,
734 24, stock_media_pause_24);
736 add_icon2 (factory, GTK_STOCK_MEDIA_PLAY,
737 16, stock_media_play_16,
738 24, stock_media_play_24);
740 add_icon2 (factory, GTK_STOCK_MEDIA_PREVIOUS,
741 16, stock_media_previous_16,
742 24, stock_media_previous_24);
744 add_icon2 (factory, GTK_STOCK_MEDIA_RECORD,
745 16, stock_media_record_16,
746 24, stock_media_record_24);
748 add_icon2 (factory, GTK_STOCK_MEDIA_REWIND,
749 16, stock_media_rewind_16,
750 24, stock_media_rewind_24);
752 add_icon2 (factory, GTK_STOCK_MEDIA_STOP,
753 16, stock_media_stop_16,
754 24, stock_media_stop_24);
756 /* Generic size only */
758 add_icon (factory, GTK_STOCK_CLEAR, 24, stock_clear_24);
759 add_icon (factory, GTK_STOCK_SELECT_COLOR, 24, stock_colorselector_24);
760 add_icon (factory, GTK_STOCK_COLOR_PICKER, 25, stock_color_picker_25);
761 add_icon (factory, GTK_STOCK_INDEX, 24, stock_index_24);
762 add_icon (factory, GTK_STOCK_ZOOM_100, 24, stock_zoom_1_24);
763 add_icon (factory, GTK_STOCK_ZOOM_FIT, 24, stock_zoom_fit_24);
764 add_icon (factory, GTK_STOCK_ZOOM_IN, 24, stock_zoom_in_24);
765 add_icon (factory, GTK_STOCK_ZOOM_OUT, 24, stock_zoom_out_24);
768 /************************************************************
769 * Icon size handling *
770 ************************************************************/
772 typedef struct _IconSize IconSize;
783 typedef struct _IconAlias IconAlias;
791 typedef struct _SettingsIconSize SettingsIconSize;
793 struct _SettingsIconSize
799 static GHashTable *icon_aliases = NULL;
800 static IconSize *icon_sizes = NULL;
801 static gint icon_sizes_allocated = 0;
802 static gint icon_sizes_used = 0;
805 init_icon_sizes (void)
807 if (icon_sizes == NULL)
809 #define NUM_BUILTIN_SIZES 7
812 icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
814 icon_sizes = g_new (IconSize, NUM_BUILTIN_SIZES);
815 icon_sizes_allocated = NUM_BUILTIN_SIZES;
816 icon_sizes_used = NUM_BUILTIN_SIZES;
818 icon_sizes[GTK_ICON_SIZE_INVALID].size = 0;
819 icon_sizes[GTK_ICON_SIZE_INVALID].name = NULL;
820 icon_sizes[GTK_ICON_SIZE_INVALID].width = 0;
821 icon_sizes[GTK_ICON_SIZE_INVALID].height = 0;
823 /* the name strings aren't copied since we don't ever remove
824 * icon sizes, so we don't need to know whether they're static.
825 * Even if we did I suppose removing the builtin sizes would be
829 icon_sizes[GTK_ICON_SIZE_MENU].size = GTK_ICON_SIZE_MENU;
830 icon_sizes[GTK_ICON_SIZE_MENU].name = "gtk-menu";
831 icon_sizes[GTK_ICON_SIZE_MENU].width = 16;
832 icon_sizes[GTK_ICON_SIZE_MENU].height = 16;
834 icon_sizes[GTK_ICON_SIZE_BUTTON].size = GTK_ICON_SIZE_BUTTON;
835 icon_sizes[GTK_ICON_SIZE_BUTTON].name = "gtk-button";
836 icon_sizes[GTK_ICON_SIZE_BUTTON].width = 20;
837 icon_sizes[GTK_ICON_SIZE_BUTTON].height = 20;
839 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].size = GTK_ICON_SIZE_SMALL_TOOLBAR;
840 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].name = "gtk-small-toolbar";
841 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].width = 18;
842 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].height = 18;
844 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].size = GTK_ICON_SIZE_LARGE_TOOLBAR;
845 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].name = "gtk-large-toolbar";
846 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].width = 24;
847 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].height = 24;
849 icon_sizes[GTK_ICON_SIZE_DND].size = GTK_ICON_SIZE_DND;
850 icon_sizes[GTK_ICON_SIZE_DND].name = "gtk-dnd";
851 icon_sizes[GTK_ICON_SIZE_DND].width = 32;
852 icon_sizes[GTK_ICON_SIZE_DND].height = 32;
854 icon_sizes[GTK_ICON_SIZE_DIALOG].size = GTK_ICON_SIZE_DIALOG;
855 icon_sizes[GTK_ICON_SIZE_DIALOG].name = "gtk-dialog";
856 icon_sizes[GTK_ICON_SIZE_DIALOG].width = 48;
857 icon_sizes[GTK_ICON_SIZE_DIALOG].height = 48;
859 g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
861 /* Alias everything to itself. */
862 i = 1; /* skip invalid size */
863 while (i < NUM_BUILTIN_SIZES)
865 gtk_icon_size_register_alias (icon_sizes[i].name, icon_sizes[i].size);
870 #undef NUM_BUILTIN_SIZES
875 free_settings_sizes (gpointer data)
877 g_array_free (data, TRUE);
881 get_settings_sizes (GtkSettings *settings,
884 GArray *settings_sizes;
885 static GQuark sizes_quark = 0;
888 sizes_quark = g_quark_from_static_string ("gtk-icon-sizes");
890 settings_sizes = g_object_get_qdata (G_OBJECT (settings), sizes_quark);
893 settings_sizes = g_array_new (FALSE, FALSE, sizeof (SettingsIconSize));
894 g_object_set_qdata_full (G_OBJECT (settings), sizes_quark,
895 settings_sizes, free_settings_sizes);
900 return settings_sizes;
904 icon_size_set_for_settings (GtkSettings *settings,
905 const gchar *size_name,
910 GArray *settings_sizes;
911 SettingsIconSize *settings_size;
913 g_return_if_fail (size_name != NULL);
915 size = gtk_icon_size_from_name (size_name);
916 if (size == GTK_ICON_SIZE_INVALID)
917 /* Reserve a place */
918 size = icon_size_register_intern (size_name, -1, -1);
920 settings_sizes = get_settings_sizes (settings, NULL);
921 if (size >= settings_sizes->len)
923 SettingsIconSize unset = { -1, -1 };
926 for (i = settings_sizes->len; i <= size; i++)
927 g_array_append_val (settings_sizes, unset);
930 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
932 settings_size->width = width;
933 settings_size->height = height;
936 /* Like pango_parse_word, but accept - as well
939 scan_icon_size_name (const char **pos, GString *out)
941 const char *p = *pos;
943 while (g_ascii_isspace (*p))
946 if (!((*p >= 'A' && *p <= 'Z') ||
947 (*p >= 'a' && *p <= 'z') ||
948 *p == '_' || *p == '-'))
951 g_string_truncate (out, 0);
952 g_string_append_c (out, *p);
955 while ((*p >= 'A' && *p <= 'Z') ||
956 (*p >= 'a' && *p <= 'z') ||
957 (*p >= '0' && *p <= '9') ||
958 *p == '_' || *p == '-')
960 g_string_append_c (out, *p);
970 icon_size_setting_parse (GtkSettings *settings,
971 const gchar *icon_size_string)
973 GString *name_buf = g_string_new (NULL);
974 const gchar *p = icon_size_string;
976 while (pango_skip_space (&p))
980 if (!scan_icon_size_name (&p, name_buf))
983 if (!pango_skip_space (&p))
991 if (!pango_scan_int (&p, &width))
994 if (!pango_skip_space (&p))
1002 if (!pango_scan_int (&p, &height))
1005 if (width > 0 && height > 0)
1007 icon_size_set_for_settings (settings, name_buf->str,
1012 g_warning ("Invalid size in gtk-icon-sizes: %d,%d\n", width, height);
1015 pango_skip_space (&p);
1024 g_string_free (name_buf, TRUE);
1028 g_warning ("Error parsing gtk-icon-sizes string:\n\t'%s'", icon_size_string);
1029 g_string_free (name_buf, TRUE);
1033 icon_size_set_all_from_settings (GtkSettings *settings)
1035 GArray *settings_sizes;
1036 gchar *icon_size_string;
1038 /* Reset old settings */
1039 settings_sizes = get_settings_sizes (settings, NULL);
1040 g_array_set_size (settings_sizes, 0);
1042 g_object_get (settings,
1043 "gtk-icon-sizes", &icon_size_string,
1046 if (icon_size_string)
1048 icon_size_setting_parse (settings, icon_size_string);
1049 g_free (icon_size_string);
1054 icon_size_settings_changed (GtkSettings *settings,
1057 icon_size_set_all_from_settings (settings);
1059 gtk_rc_reset_styles (settings);
1063 icon_sizes_init_for_settings (GtkSettings *settings)
1065 g_signal_connect (settings,
1066 "notify::gtk-icon-sizes",
1067 G_CALLBACK (icon_size_settings_changed),
1070 icon_size_set_all_from_settings (settings);
1074 icon_size_lookup_intern (GtkSettings *settings,
1079 GArray *settings_sizes;
1080 gint width_for_settings = -1;
1081 gint height_for_settings = -1;
1085 if (size >= icon_sizes_used)
1088 if (size == GTK_ICON_SIZE_INVALID)
1093 gboolean initial = FALSE;
1095 settings_sizes = get_settings_sizes (settings, &initial);
1097 icon_sizes_init_for_settings (settings);
1099 if (size < settings_sizes->len)
1101 SettingsIconSize *settings_size;
1103 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
1105 width_for_settings = settings_size->width;
1106 height_for_settings = settings_size->height;
1111 *widthp = width_for_settings >= 0 ? width_for_settings : icon_sizes[size].width;
1114 *heightp = height_for_settings >= 0 ? height_for_settings : icon_sizes[size].height;
1120 * gtk_icon_size_lookup_for_settings:
1121 * @settings: a #GtkSettings object, used to determine
1122 * which set of user preferences to used.
1123 * @size: an icon size
1124 * @width: location to store icon width
1125 * @height: location to store icon height
1127 * Obtains the pixel size of a semantic icon size, possibly
1128 * modified by user preferences for a particular
1129 * #GtkSettings. Normally @size would be
1130 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
1131 * isn't normally needed, gtk_widget_render_icon() is the usual
1132 * way to get an icon for rendering, then just look at the size of
1133 * the rendered pixbuf. The rendered pixbuf may not even correspond to
1134 * the width/height returned by gtk_icon_size_lookup(), because themes
1135 * are free to render the pixbuf however they like, including changing
1138 * Return value: %TRUE if @size was a valid size
1143 gtk_icon_size_lookup_for_settings (GtkSettings *settings,
1148 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1150 return icon_size_lookup_intern (settings, size, width, height);
1154 * gtk_icon_size_lookup:
1155 * @size: an icon size
1156 * @width: location to store icon width
1157 * @height: location to store icon height
1159 * Obtains the pixel size of a semantic icon size, possibly
1160 * modified by user preferences for the default #GtkSettings.
1161 * (See gtk_icon_size_lookup_for_settings().)
1162 * Normally @size would be
1163 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
1164 * isn't normally needed, gtk_widget_render_icon() is the usual
1165 * way to get an icon for rendering, then just look at the size of
1166 * the rendered pixbuf. The rendered pixbuf may not even correspond to
1167 * the width/height returned by gtk_icon_size_lookup(), because themes
1168 * are free to render the pixbuf however they like, including changing
1171 * Return value: %TRUE if @size was a valid size
1174 gtk_icon_size_lookup (GtkIconSize size,
1178 GTK_NOTE (MULTIHEAD,
1179 g_warning ("gtk_icon_size_lookup ()) is not multihead safe"));
1181 return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (),
1182 size, widthp, heightp);
1186 icon_size_register_intern (const gchar *name,
1190 IconAlias *old_alias;
1195 old_alias = g_hash_table_lookup (icon_aliases, name);
1196 if (old_alias && icon_sizes[old_alias->target].width > 0)
1198 g_warning ("Icon size name '%s' already exists", name);
1199 return GTK_ICON_SIZE_INVALID;
1204 size = old_alias->target;
1208 if (icon_sizes_used == icon_sizes_allocated)
1210 icon_sizes_allocated *= 2;
1211 icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated);
1214 size = icon_sizes_used++;
1216 /* alias to self. */
1217 gtk_icon_size_register_alias (name, size);
1219 icon_sizes[size].size = size;
1220 icon_sizes[size].name = g_strdup (name);
1223 icon_sizes[size].width = width;
1224 icon_sizes[size].height = height;
1230 * gtk_icon_size_register:
1231 * @name: name of the icon size
1232 * @width: the icon width
1233 * @height: the icon height
1235 * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
1236 * etc. Returns the integer value for the size.
1238 * Returns: integer value representing the size
1242 gtk_icon_size_register (const gchar *name,
1246 g_return_val_if_fail (name != NULL, 0);
1247 g_return_val_if_fail (width > 0, 0);
1248 g_return_val_if_fail (height > 0, 0);
1250 return icon_size_register_intern (name, width, height);
1254 * gtk_icon_size_register_alias:
1255 * @alias: an alias for @target
1256 * @target: an existing icon size
1258 * Registers @alias as another name for @target.
1259 * So calling gtk_icon_size_from_name() with @alias as argument
1260 * will return @target.
1264 gtk_icon_size_register_alias (const gchar *alias,
1269 g_return_if_fail (alias != NULL);
1273 if (!icon_size_lookup_intern (NULL, target, NULL, NULL))
1274 g_warning ("gtk_icon_size_register_alias: Icon size %d does not exist", target);
1276 ia = g_hash_table_lookup (icon_aliases, alias);
1279 if (icon_sizes[ia->target].width > 0)
1281 g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias);
1285 ia->target = target;
1290 ia = g_new (IconAlias, 1);
1291 ia->name = g_strdup (alias);
1292 ia->target = target;
1294 g_hash_table_insert (icon_aliases, ia->name, ia);
1299 * gtk_icon_size_from_name:
1300 * @name: the name to look up.
1301 * @returns: the icon size with the given name.
1303 * Looks up the icon size associated with @name.
1306 gtk_icon_size_from_name (const gchar *name)
1312 ia = g_hash_table_lookup (icon_aliases, name);
1314 if (ia && icon_sizes[ia->target].width > 0)
1317 return GTK_ICON_SIZE_INVALID;
1321 * gtk_icon_size_get_name:
1322 * @size: a #GtkIconSize.
1323 * @returns: the name of the given icon size.
1325 * Gets the canonical name of the given icon size. The returned string
1326 * is statically allocated and should not be freed.
1328 G_CONST_RETURN gchar*
1329 gtk_icon_size_get_name (GtkIconSize size)
1331 if (size >= icon_sizes_used)
1334 return icon_sizes[size].name;
1337 /************************************************************/
1342 static GdkPixbuf *find_in_cache (GtkIconSet *icon_set,
1344 GtkTextDirection direction,
1347 static void add_to_cache (GtkIconSet *icon_set,
1349 GtkTextDirection direction,
1353 /* Clear icon set contents, drop references to all contained
1354 * GdkPixbuf objects and forget all GtkIconSources. Used to
1355 * recycle an icon set.
1357 static void clear_cache (GtkIconSet *icon_set,
1358 gboolean style_detach);
1359 static GSList* copy_cache (GtkIconSet *icon_set,
1360 GtkIconSet *copy_recipient);
1361 static void attach_to_style (GtkIconSet *icon_set,
1363 static void detach_from_style (GtkIconSet *icon_set,
1365 static void style_dnotify (gpointer data);
1373 /* Cache of the last few rendered versions of the icon. */
1381 static guint cache_serial = 0;
1386 * Creates a new #GtkIconSet. A #GtkIconSet represents a single icon
1387 * in various sizes and widget states. It can provide a #GdkPixbuf
1388 * for a given size and state on request, and automatically caches
1389 * some of the rendered #GdkPixbuf objects.
1391 * Normally you would use gtk_widget_render_icon() instead of
1392 * using #GtkIconSet directly. The one case where you'd use
1393 * #GtkIconSet is to create application-specific icon sets to place in
1394 * a #GtkIconFactory.
1396 * Return value: a new #GtkIconSet
1399 gtk_icon_set_new (void)
1401 GtkIconSet *icon_set;
1403 icon_set = g_new (GtkIconSet, 1);
1405 icon_set->ref_count = 1;
1406 icon_set->sources = NULL;
1407 icon_set->cache = NULL;
1408 icon_set->cache_size = 0;
1409 icon_set->cache_serial = cache_serial;
1415 * gtk_icon_set_new_from_pixbuf:
1416 * @pixbuf: a #GdkPixbuf
1418 * Creates a new #GtkIconSet with @pixbuf as the default/fallback
1419 * source image. If you don't add any additional #GtkIconSource to the
1420 * icon set, all variants of the icon will be created from @pixbuf,
1421 * using scaling, pixelation, etc. as required to adjust the icon size
1422 * or make the icon look insensitive/prelighted.
1424 * Return value: a new #GtkIconSet
1427 gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
1431 GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1433 g_return_val_if_fail (pixbuf != NULL, NULL);
1435 set = gtk_icon_set_new ();
1437 gtk_icon_source_set_pixbuf (&source, pixbuf);
1438 gtk_icon_set_add_source (set, &source);
1439 gtk_icon_source_set_pixbuf (&source, NULL);
1447 * @icon_set: a #GtkIconSet.
1449 * Increments the reference count on @icon_set.
1451 * Return value: @icon_set.
1454 gtk_icon_set_ref (GtkIconSet *icon_set)
1456 g_return_val_if_fail (icon_set != NULL, NULL);
1457 g_return_val_if_fail (icon_set->ref_count > 0, NULL);
1459 icon_set->ref_count += 1;
1465 * gtk_icon_set_unref:
1466 * @icon_set: a #GtkIconSet
1468 * Decrements the reference count on @icon_set, and frees memory
1469 * if the reference count reaches 0.
1472 gtk_icon_set_unref (GtkIconSet *icon_set)
1474 g_return_if_fail (icon_set != NULL);
1475 g_return_if_fail (icon_set->ref_count > 0);
1477 icon_set->ref_count -= 1;
1479 if (icon_set->ref_count == 0)
1481 GSList *tmp_list = icon_set->sources;
1482 while (tmp_list != NULL)
1484 gtk_icon_source_free (tmp_list->data);
1486 tmp_list = g_slist_next (tmp_list);
1488 g_slist_free (icon_set->sources);
1490 clear_cache (icon_set, TRUE);
1497 gtk_icon_set_get_type (void)
1499 static GType our_type = 0;
1502 our_type = g_boxed_type_register_static ("GtkIconSet",
1503 (GBoxedCopyFunc) gtk_icon_set_ref,
1504 (GBoxedFreeFunc) gtk_icon_set_unref);
1510 * gtk_icon_set_copy:
1511 * @icon_set: a #GtkIconSet
1513 * Copies @icon_set by value.
1515 * Return value: a new #GtkIconSet identical to the first.
1518 gtk_icon_set_copy (GtkIconSet *icon_set)
1523 copy = gtk_icon_set_new ();
1525 tmp_list = icon_set->sources;
1526 while (tmp_list != NULL)
1528 copy->sources = g_slist_prepend (copy->sources,
1529 gtk_icon_source_copy (tmp_list->data));
1531 tmp_list = g_slist_next (tmp_list);
1534 copy->sources = g_slist_reverse (copy->sources);
1536 copy->cache = copy_cache (icon_set, copy);
1537 copy->cache_size = icon_set->cache_size;
1538 copy->cache_serial = icon_set->cache_serial;
1544 sizes_equivalent (GtkIconSize lhs,
1547 /* We used to consider sizes equivalent if they were
1548 * the same pixel size, but we don't have the GtkSettings
1549 * here, so we can't do that. Plus, it's not clear that
1550 * it is right... it was just a workaround for the fact
1551 * that we register icons by logical size, not pixel size.
1557 gint r_w, r_h, l_w, l_h;
1559 icon_size_lookup_intern (NULL, rhs, &r_w, &r_h);
1560 icon_size_lookup_intern (NULL, lhs, &l_w, &l_h);
1562 return r_w == l_w && r_h == l_h;
1566 static GtkIconSource *
1567 find_best_matching_source (GtkIconSet *icon_set,
1568 GtkTextDirection direction,
1573 GtkIconSource *source;
1576 /* We need to find the best icon source. Direction matters more
1577 * than state, state matters more than size. icon_set->sources
1578 * is sorted according to wildness, so if we take the first
1579 * match we find it will be the least-wild match (if there are
1580 * multiple matches for a given "wildness" then the RC file contained
1581 * dumb stuff, and we end up with an arbitrary matching source)
1585 tmp_list = icon_set->sources;
1586 while (tmp_list != NULL)
1588 GtkIconSource *s = tmp_list->data;
1590 if ((s->any_direction || (s->direction == direction)) &&
1591 (s->any_state || (s->state == state)) &&
1592 (s->any_size || (sizes_equivalent (size, s->size))))
1594 if (!g_slist_find (failed, s))
1601 tmp_list = g_slist_next (tmp_list);
1608 ensure_filename_pixbuf (GtkIconSet *icon_set,
1609 GtkIconSource *source)
1611 if (source->filename_pixbuf == NULL)
1613 GError *error = NULL;
1615 source->filename_pixbuf = gdk_pixbuf_new_from_file (source->source.filename, &error);
1617 if (source->filename_pixbuf == NULL)
1619 /* Remove this icon source so we don't keep trying to
1622 g_warning (_("Error loading icon: %s"), error->message);
1623 g_error_free (error);
1625 icon_set->sources = g_slist_remove (icon_set->sources, source);
1627 gtk_icon_source_free (source);
1637 render_icon_name_pixbuf (GtkIconSource *icon_source,
1639 GtkTextDirection direction,
1646 GdkPixbuf *tmp_pixbuf;
1647 GtkIconSource tmp_source;
1649 GtkIconTheme *icon_theme;
1650 GtkSettings *settings;
1651 gint width, height, pixel_size;
1652 gint *sizes, *s, dist;
1653 GError *error = NULL;
1655 if (widget && gtk_widget_has_screen (widget))
1656 screen = gtk_widget_get_screen (widget);
1657 else if (style && style->colormap)
1658 screen = gdk_colormap_get_screen (style->colormap);
1661 screen = gdk_screen_get_default ();
1662 GTK_NOTE (MULTIHEAD,
1663 g_warning ("Using the default screen for gtk_icon_source_render_icon()"));
1666 icon_theme = gtk_icon_theme_get_for_screen (screen);
1667 settings = gtk_settings_get_for_screen (screen);
1669 if (!gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
1673 /* Find an available size close to 48
1675 sizes = gtk_icon_theme_get_icon_sizes (icon_theme, icon_source->source.icon_name);
1677 width = height = 48;
1678 for (s = sizes; *s; s++)
1682 width = height = 48;
1689 width = height = *s;
1697 width = height = *s;
1707 g_warning ("Invalid icon size %d\n", size);
1708 width = height = 24;
1712 pixel_size = MIN (width, height);
1714 tmp_pixbuf = gtk_icon_theme_load_icon (icon_theme,
1715 icon_source->source.icon_name,
1721 g_warning ("Error loading theme icon for stock: %s", error->message);
1722 g_error_free (error);
1726 tmp_source = *icon_source;
1727 tmp_source.type = GTK_ICON_SOURCE_PIXBUF;
1728 tmp_source.source.pixbuf = tmp_pixbuf;
1730 pixbuf = gtk_style_render_icon (style, &tmp_source,
1731 direction, state, -1,
1735 g_warning ("Failed to render icon");
1737 g_object_unref (tmp_pixbuf);
1743 find_and_render_icon_source (GtkIconSet *icon_set,
1745 GtkTextDirection direction,
1751 GSList *failed = NULL;
1752 GdkPixbuf *pixbuf = NULL;
1754 /* We treat failure in two different ways:
1756 * A) If loading a source that specifies a filename fails,
1757 * we treat that as permanent, and remove the source
1758 * from the GtkIconSet. (in ensure_filename_pixbuf ()
1759 * B) If loading a themed icon fails, or scaling an icon
1760 * fails, we treat that as transient and will try
1761 * again next time the icon falls out of the cache
1762 * and we need to recreate it.
1764 while (pixbuf == NULL)
1766 GtkIconSource *source = find_best_matching_source (icon_set, direction, state, size, failed);
1771 switch (source->type)
1773 case GTK_ICON_SOURCE_FILENAME:
1774 if (!ensure_filename_pixbuf (icon_set, source))
1777 case GTK_ICON_SOURCE_PIXBUF:
1778 pixbuf = gtk_style_render_icon (style, source,
1779 direction, state, size,
1783 g_warning ("Failed to render icon");
1784 failed = g_slist_prepend (failed, source);
1787 case GTK_ICON_SOURCE_ICON_NAME:
1788 pixbuf = render_icon_name_pixbuf (source, style,
1789 direction, state, size,
1792 failed = g_slist_prepend (failed, source);
1794 case GTK_ICON_SOURCE_EMPTY:
1795 g_assert_not_reached ();
1799 g_slist_free (failed);
1805 render_fallback_image (GtkStyle *style,
1806 GtkTextDirection direction,
1812 /* This icon can be used for any direction/state/size */
1813 static GtkIconSource fallback_source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1815 if (fallback_source.type == GTK_ICON_SOURCE_EMPTY)
1817 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_inline (-1, stock_missing_image_24, FALSE, NULL);
1818 gtk_icon_source_set_pixbuf (&fallback_source, pixbuf);
1819 g_object_unref (pixbuf);
1822 return gtk_style_render_icon (style,
1832 * gtk_icon_set_render_icon:
1833 * @icon_set: a #GtkIconSet
1834 * @style: a #GtkStyle associated with @widget, or %NULL
1835 * @direction: text direction
1836 * @state: widget state
1837 * @size: icon size. A size of (GtkIconSize)-1
1838 * means render at the size of the source and don't scale.
1839 * @widget: widget that will display the icon, or %NULL.
1840 * The only use that is typically made of this
1841 * is to determine the appropriate #GdkScreen.
1842 * @detail: detail to pass to the theme engine, or %NULL.
1843 * Note that passing a detail of anything but %NULL
1844 * will disable caching.
1846 * Renders an icon using gtk_style_render_icon(). In most cases,
1847 * gtk_widget_render_icon() is better, since it automatically provides
1848 * most of the arguments from the current widget settings. This
1849 * function never returns %NULL; if the icon can't be rendered
1850 * (perhaps because an image file fails to load), a default "missing
1851 * image" icon will be returned instead.
1853 * Return value: a #GdkPixbuf to be displayed
1856 gtk_icon_set_render_icon (GtkIconSet *icon_set,
1858 GtkTextDirection direction,
1866 g_return_val_if_fail (icon_set != NULL, NULL);
1867 g_return_val_if_fail (style == NULL || GTK_IS_STYLE (style), NULL);
1869 if (icon_set->sources == NULL)
1870 return render_fallback_image (style, direction, state, size, widget, detail);
1874 icon = find_in_cache (icon_set, style, direction,
1879 g_object_ref (icon);
1885 icon = find_and_render_icon_source (icon_set, style, direction, state, size,
1889 icon = render_fallback_image (style, direction, state, size, widget, detail);
1892 add_to_cache (icon_set, style, direction, state, size, icon);
1897 /* Order sources by their "wildness", so that "wilder" sources are
1898 * greater than "specific" sources; for determining ordering,
1899 * direction beats state beats size.
1903 icon_source_compare (gconstpointer ap, gconstpointer bp)
1905 const GtkIconSource *a = ap;
1906 const GtkIconSource *b = bp;
1908 if (!a->any_direction && b->any_direction)
1910 else if (a->any_direction && !b->any_direction)
1912 else if (!a->any_state && b->any_state)
1914 else if (a->any_state && !b->any_state)
1916 else if (!a->any_size && b->any_size)
1918 else if (a->any_size && !b->any_size)
1925 * gtk_icon_set_add_source:
1926 * @icon_set: a #GtkIconSet
1927 * @source: a #GtkIconSource
1929 * Icon sets have a list of #GtkIconSource, which they use as base
1930 * icons for rendering icons in different states and sizes. Icons are
1931 * scaled, made to look insensitive, etc. in
1932 * gtk_icon_set_render_icon(), but #GtkIconSet needs base images to
1933 * work with. The base images and when to use them are described by
1936 * This function copies @source, so you can reuse the same source immediately
1937 * without affecting the icon set.
1939 * An example of when you'd use this function: a web browser's "Back
1940 * to Previous Page" icon might point in a different direction in
1941 * Hebrew and in English; it might look different when insensitive;
1942 * and it might change size depending on toolbar mode (small/large
1943 * icons). So a single icon set would contain all those variants of
1944 * the icon, and you might add a separate source for each one.
1946 * You should nearly always add a "default" icon source with all
1947 * fields wildcarded, which will be used as a fallback if no more
1948 * specific source matches. #GtkIconSet always prefers more specific
1949 * icon sources to more generic icon sources. The order in which you
1950 * add the sources to the icon set does not matter.
1952 * gtk_icon_set_new_from_pixbuf() creates a new icon set with a
1953 * default icon source based on the given pixbuf.
1957 gtk_icon_set_add_source (GtkIconSet *icon_set,
1958 const GtkIconSource *source)
1960 g_return_if_fail (icon_set != NULL);
1961 g_return_if_fail (source != NULL);
1963 if (source->type == GTK_ICON_SOURCE_EMPTY)
1965 g_warning ("Useless empty GtkIconSource");
1969 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
1970 gtk_icon_source_copy (source),
1971 icon_source_compare);
1975 * gtk_icon_set_get_sizes:
1976 * @icon_set: a #GtkIconSet
1977 * @sizes: return location for array of sizes
1978 * @n_sizes: location to store number of elements in returned array
1980 * Obtains a list of icon sizes this icon set can render. The returned
1981 * array must be freed with g_free().
1985 gtk_icon_set_get_sizes (GtkIconSet *icon_set,
1986 GtkIconSize **sizes,
1990 gboolean all_sizes = FALSE;
1991 GSList *specifics = NULL;
1993 g_return_if_fail (icon_set != NULL);
1994 g_return_if_fail (sizes != NULL);
1995 g_return_if_fail (n_sizes != NULL);
1997 tmp_list = icon_set->sources;
1998 while (tmp_list != NULL)
2000 GtkIconSource *source;
2002 source = tmp_list->data;
2004 if (source->any_size)
2010 specifics = g_slist_prepend (specifics, GINT_TO_POINTER (source->size));
2012 tmp_list = g_slist_next (tmp_list);
2017 /* Need to find out what sizes exist */
2022 *sizes = g_new (GtkIconSize, icon_sizes_used);
2023 *n_sizes = icon_sizes_used - 1;
2026 while (i < icon_sizes_used)
2028 (*sizes)[i - 1] = icon_sizes[i].size;
2036 *n_sizes = g_slist_length (specifics);
2037 *sizes = g_new (GtkIconSize, *n_sizes);
2040 tmp_list = specifics;
2041 while (tmp_list != NULL)
2043 (*sizes)[i] = GPOINTER_TO_INT (tmp_list->data);
2046 tmp_list = g_slist_next (tmp_list);
2050 g_slist_free (specifics);
2055 * gtk_icon_source_new:
2057 * Creates a new #GtkIconSource. A #GtkIconSource contains a #GdkPixbuf (or
2058 * image filename) that serves as the base image for one or more of the
2059 * icons in a #GtkIconSet, along with a specification for which icons in the
2060 * icon set will be based on that pixbuf or image file. An icon set contains
2061 * a set of icons that represent "the same" logical concept in different states,
2062 * different global text directions, and different sizes.
2064 * So for example a web browser's "Back to Previous Page" icon might
2065 * point in a different direction in Hebrew and in English; it might
2066 * look different when insensitive; and it might change size depending
2067 * on toolbar mode (small/large icons). So a single icon set would
2068 * contain all those variants of the icon. #GtkIconSet contains a list
2069 * of #GtkIconSource from which it can derive specific icon variants in
2072 * In the simplest case, #GtkIconSet contains one source pixbuf from
2073 * which it derives all variants. The convenience function
2074 * gtk_icon_set_new_from_pixbuf() handles this case; if you only have
2075 * one source pixbuf, just use that function.
2077 * If you want to use a different base pixbuf for different icon
2078 * variants, you create multiple icon sources, mark which variants
2079 * they'll be used to create, and add them to the icon set with
2080 * gtk_icon_set_add_source().
2082 * By default, the icon source has all parameters wildcarded. That is,
2083 * the icon source will be used as the base icon for any desired text
2084 * direction, widget state, or icon size.
2086 * Return value: a new #GtkIconSource
2089 gtk_icon_source_new (void)
2093 src = g_new0 (GtkIconSource, 1);
2095 src->direction = GTK_TEXT_DIR_NONE;
2096 src->size = GTK_ICON_SIZE_INVALID;
2097 src->state = GTK_STATE_NORMAL;
2099 src->any_direction = TRUE;
2100 src->any_state = TRUE;
2101 src->any_size = TRUE;
2107 * gtk_icon_source_copy:
2108 * @source: a #GtkIconSource
2110 * Creates a copy of @source; mostly useful for language bindings.
2112 * Return value: a new #GtkIconSource
2115 gtk_icon_source_copy (const GtkIconSource *source)
2117 GtkIconSource *copy;
2119 g_return_val_if_fail (source != NULL, NULL);
2121 copy = g_new (GtkIconSource, 1);
2127 case GTK_ICON_SOURCE_EMPTY:
2129 case GTK_ICON_SOURCE_ICON_NAME:
2130 copy->source.icon_name = g_strdup (copy->source.icon_name);
2132 case GTK_ICON_SOURCE_FILENAME:
2133 copy->source.filename = g_strdup (copy->source.filename);
2134 if (copy->filename_pixbuf)
2135 g_object_ref (copy->filename_pixbuf);
2137 case GTK_ICON_SOURCE_PIXBUF:
2138 g_object_ref (copy->source.pixbuf);
2141 g_assert_not_reached();
2148 * gtk_icon_source_free:
2149 * @source: a #GtkIconSource
2151 * Frees a dynamically-allocated icon source, along with its
2152 * filename, size, and pixbuf fields if those are not %NULL.
2155 gtk_icon_source_free (GtkIconSource *source)
2157 g_return_if_fail (source != NULL);
2159 icon_source_clear (source);
2164 gtk_icon_source_get_type (void)
2166 static GType our_type = 0;
2169 our_type = g_boxed_type_register_static ("GtkIconSource",
2170 (GBoxedCopyFunc) gtk_icon_source_copy,
2171 (GBoxedFreeFunc) gtk_icon_source_free);
2177 icon_source_clear (GtkIconSource *source)
2179 switch (source->type)
2181 case GTK_ICON_SOURCE_EMPTY:
2183 case GTK_ICON_SOURCE_ICON_NAME:
2184 g_free (source->source.icon_name);
2185 source->source.icon_name = NULL;
2187 case GTK_ICON_SOURCE_FILENAME:
2188 g_free (source->source.filename);
2189 source->source.filename = NULL;
2190 if (source->filename_pixbuf)
2191 g_object_unref (source->filename_pixbuf);
2192 source->filename_pixbuf = NULL;
2194 case GTK_ICON_SOURCE_PIXBUF:
2195 g_object_unref (source->source.pixbuf);
2196 source->source.pixbuf = NULL;
2199 g_assert_not_reached();
2202 source->type = GTK_ICON_SOURCE_EMPTY;
2206 * gtk_icon_source_set_filename:
2207 * @source: a #GtkIconSource
2208 * @filename: image file to use
2210 * Sets the name of an image file to use as a base image when creating
2211 * icon variants for #GtkIconSet. The filename must be absolute.
2214 gtk_icon_source_set_filename (GtkIconSource *source,
2215 const gchar *filename)
2217 g_return_if_fail (source != NULL);
2218 g_return_if_fail (filename == NULL || g_path_is_absolute (filename));
2220 if (source->type == GTK_ICON_SOURCE_FILENAME &&
2221 source->source.filename == filename)
2224 icon_source_clear (source);
2226 if (filename != NULL)
2228 source->type = GTK_ICON_SOURCE_FILENAME;
2229 source->source.filename = g_strdup (filename);
2234 * gtk_icon_source_set_icon_name
2235 * @source: a #GtkIconSource
2236 * @icon_name: name of icon to use
2238 * Sets the name of an icon to look up in the current icon theme
2239 * to use as a base image when creating icon variants for #GtkIconSet.
2242 gtk_icon_source_set_icon_name (GtkIconSource *source,
2243 const gchar *icon_name)
2245 g_return_if_fail (source != NULL);
2247 if (source->type == GTK_ICON_SOURCE_ICON_NAME &&
2248 source->source.icon_name == icon_name)
2251 icon_source_clear (source);
2253 if (icon_name != NULL)
2255 source->type = GTK_ICON_SOURCE_ICON_NAME;
2256 source->source.icon_name = g_strdup (icon_name);
2261 * gtk_icon_source_set_pixbuf:
2262 * @source: a #GtkIconSource
2263 * @pixbuf: pixbuf to use as a source
2265 * Sets a pixbuf to use as a base image when creating icon variants
2269 gtk_icon_source_set_pixbuf (GtkIconSource *source,
2272 g_return_if_fail (source != NULL);
2273 g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
2275 if (source->type == GTK_ICON_SOURCE_PIXBUF &&
2276 source->source.pixbuf == pixbuf)
2279 icon_source_clear (source);
2283 source->type = GTK_ICON_SOURCE_PIXBUF;
2284 source->source.pixbuf = g_object_ref (pixbuf);
2289 * gtk_icon_source_get_filename:
2290 * @source: a #GtkIconSource
2292 * Retrieves the source filename, or %NULL if none is set. The
2293 * filename is not a copy, and should not be modified or expected to
2294 * persist beyond the lifetime of the icon source.
2296 * Return value: image filename. This string must not be modified
2299 G_CONST_RETURN gchar*
2300 gtk_icon_source_get_filename (const GtkIconSource *source)
2302 g_return_val_if_fail (source != NULL, NULL);
2304 if (source->type == GTK_ICON_SOURCE_FILENAME)
2305 return source->source.filename;
2311 * gtk_icon_source_get_icon_name:
2312 * @source: a #GtkIconSource
2314 * Retrieves the source icon name, or %NULL if none is set. The
2315 * icon_name is not a copy, and should not be modified or expected to
2316 * persist beyond the lifetime of the icon source.
2318 * Return value: icon name. This string must not be modified or freed.
2320 G_CONST_RETURN gchar*
2321 gtk_icon_source_get_icon_name (const GtkIconSource *source)
2323 g_return_val_if_fail (source != NULL, NULL);
2325 if (source->type == GTK_ICON_SOURCE_ICON_NAME)
2326 return source->source.icon_name;
2332 * gtk_icon_source_get_pixbuf:
2333 * @source: a #GtkIconSource
2335 * Retrieves the source pixbuf, or %NULL if none is set.
2336 * In addition, if a filename source is in use, this
2337 * function in some cases will return the pixbuf from
2338 * loaded from the filename. This is, for example, true
2339 * for the GtkIconSource passed to the GtkStyle::render_icon()
2340 * virtual function. The reference count on the pixbuf is
2343 * Return value: source pixbuf
2346 gtk_icon_source_get_pixbuf (const GtkIconSource *source)
2348 g_return_val_if_fail (source != NULL, NULL);
2350 if (source->type == GTK_ICON_SOURCE_PIXBUF)
2351 return source->source.pixbuf;
2352 else if (source->type == GTK_ICON_SOURCE_FILENAME)
2353 return source->filename_pixbuf;
2359 * gtk_icon_source_set_direction_wildcarded:
2360 * @source: a #GtkIconSource
2361 * @setting: %TRUE to wildcard the text direction
2363 * If the text direction is wildcarded, this source can be used
2364 * as the base image for an icon in any #GtkTextDirection.
2365 * If the text direction is not wildcarded, then the
2366 * text direction the icon source applies to should be set
2367 * with gtk_icon_source_set_direction(), and the icon source
2368 * will only be used with that text direction.
2370 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2371 * wildcarded sources, and will use an exact match when possible.
2375 gtk_icon_source_set_direction_wildcarded (GtkIconSource *source,
2378 g_return_if_fail (source != NULL);
2380 source->any_direction = setting != FALSE;
2384 * gtk_icon_source_set_state_wildcarded:
2385 * @source: a #GtkIconSource
2386 * @setting: %TRUE to wildcard the widget state
2388 * If the widget state is wildcarded, this source can be used as the
2389 * base image for an icon in any #GtkStateType. If the widget state
2390 * is not wildcarded, then the state the source applies to should be
2391 * set with gtk_icon_source_set_state() and the icon source will
2392 * only be used with that specific state.
2394 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2395 * wildcarded sources, and will use an exact match when possible.
2397 * #GtkIconSet will normally transform wildcarded source images to
2398 * produce an appropriate icon for a given state, for example
2399 * lightening an image on prelight, but will not modify source images
2400 * that match exactly.
2403 gtk_icon_source_set_state_wildcarded (GtkIconSource *source,
2406 g_return_if_fail (source != NULL);
2408 source->any_state = setting != FALSE;
2413 * gtk_icon_source_set_size_wildcarded:
2414 * @source: a #GtkIconSource
2415 * @setting: %TRUE to wildcard the widget state
2417 * If the icon size is wildcarded, this source can be used as the base
2418 * image for an icon of any size. If the size is not wildcarded, then
2419 * the size the source applies to should be set with
2420 * gtk_icon_source_set_size() and the icon source will only be used
2421 * with that specific size.
2423 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2424 * wildcarded sources, and will use an exact match when possible.
2426 * #GtkIconSet will normally scale wildcarded source images to produce
2427 * an appropriate icon at a given size, but will not change the size
2428 * of source images that match exactly.
2431 gtk_icon_source_set_size_wildcarded (GtkIconSource *source,
2434 g_return_if_fail (source != NULL);
2436 source->any_size = setting != FALSE;
2440 * gtk_icon_source_get_size_wildcarded:
2441 * @source: a #GtkIconSource
2443 * Gets the value set by gtk_icon_source_set_size_wildcarded().
2445 * Return value: %TRUE if this icon source is a base for any icon size variant
2448 gtk_icon_source_get_size_wildcarded (const GtkIconSource *source)
2450 g_return_val_if_fail (source != NULL, TRUE);
2452 return source->any_size;
2456 * gtk_icon_source_get_state_wildcarded:
2457 * @source: a #GtkIconSource
2459 * Gets the value set by gtk_icon_source_set_state_wildcarded().
2461 * Return value: %TRUE if this icon source is a base for any widget state variant
2464 gtk_icon_source_get_state_wildcarded (const GtkIconSource *source)
2466 g_return_val_if_fail (source != NULL, TRUE);
2468 return source->any_state;
2472 * gtk_icon_source_get_direction_wildcarded:
2473 * @source: a #GtkIconSource
2475 * Gets the value set by gtk_icon_source_set_direction_wildcarded().
2477 * Return value: %TRUE if this icon source is a base for any text direction variant
2480 gtk_icon_source_get_direction_wildcarded (const GtkIconSource *source)
2482 g_return_val_if_fail (source != NULL, TRUE);
2484 return source->any_direction;
2488 * gtk_icon_source_set_direction:
2489 * @source: a #GtkIconSource
2490 * @direction: text direction this source applies to
2492 * Sets the text direction this icon source is intended to be used
2495 * Setting the text direction on an icon source makes no difference
2496 * if the text direction is wildcarded. Therefore, you should usually
2497 * call gtk_icon_source_set_direction_wildcarded() to un-wildcard it
2498 * in addition to calling this function.
2502 gtk_icon_source_set_direction (GtkIconSource *source,
2503 GtkTextDirection direction)
2505 g_return_if_fail (source != NULL);
2507 source->direction = direction;
2511 * gtk_icon_source_set_state:
2512 * @source: a #GtkIconSource
2513 * @state: widget state this source applies to
2515 * Sets the widget state this icon source is intended to be used
2518 * Setting the widget state on an icon source makes no difference
2519 * if the state is wildcarded. Therefore, you should usually
2520 * call gtk_icon_source_set_state_wildcarded() to un-wildcard it
2521 * in addition to calling this function.
2525 gtk_icon_source_set_state (GtkIconSource *source,
2528 g_return_if_fail (source != NULL);
2530 source->state = state;
2534 * gtk_icon_source_set_size:
2535 * @source: a #GtkIconSource
2536 * @size: icon size this source applies to
2538 * Sets the icon size this icon source is intended to be used
2541 * Setting the icon size on an icon source makes no difference
2542 * if the size is wildcarded. Therefore, you should usually
2543 * call gtk_icon_source_set_size_wildcarded() to un-wildcard it
2544 * in addition to calling this function.
2548 gtk_icon_source_set_size (GtkIconSource *source,
2551 g_return_if_fail (source != NULL);
2553 source->size = size;
2557 * gtk_icon_source_get_direction:
2558 * @source: a #GtkIconSource
2560 * Obtains the text direction this icon source applies to. The return
2561 * value is only useful/meaningful if the text direction is <emphasis>not</emphasis>
2564 * Return value: text direction this source matches
2567 gtk_icon_source_get_direction (const GtkIconSource *source)
2569 g_return_val_if_fail (source != NULL, 0);
2571 return source->direction;
2575 * gtk_icon_source_get_state:
2576 * @source: a #GtkIconSource
2578 * Obtains the widget state this icon source applies to. The return
2579 * value is only useful/meaningful if the widget state is <emphasis>not</emphasis>
2582 * Return value: widget state this source matches
2585 gtk_icon_source_get_state (const GtkIconSource *source)
2587 g_return_val_if_fail (source != NULL, 0);
2589 return source->state;
2593 * gtk_icon_source_get_size:
2594 * @source: a #GtkIconSource
2596 * Obtains the icon size this source applies to. The return value
2597 * is only useful/meaningful if the icon size is <emphasis>not</emphasis> wildcarded.
2599 * Return value: icon size this source matches.
2602 gtk_icon_source_get_size (const GtkIconSource *source)
2604 g_return_val_if_fail (source != NULL, 0);
2606 return source->size;
2609 #define NUM_CACHED_ICONS 8
2611 typedef struct _CachedIcon CachedIcon;
2615 /* These must all match to use the cached pixbuf.
2616 * If any don't match, we must re-render the pixbuf.
2619 GtkTextDirection direction;
2627 ensure_cache_up_to_date (GtkIconSet *icon_set)
2629 if (icon_set->cache_serial != cache_serial)
2631 clear_cache (icon_set, TRUE);
2632 icon_set->cache_serial = cache_serial;
2637 cached_icon_free (CachedIcon *icon)
2639 g_object_unref (icon->pixbuf);
2642 g_object_unref (icon->style);
2648 find_in_cache (GtkIconSet *icon_set,
2650 GtkTextDirection direction,
2657 ensure_cache_up_to_date (icon_set);
2660 tmp_list = icon_set->cache;
2661 while (tmp_list != NULL)
2663 CachedIcon *icon = tmp_list->data;
2665 if (icon->style == style &&
2666 icon->direction == direction &&
2667 icon->state == state &&
2672 /* Move this icon to the front of the list. */
2673 prev->next = tmp_list->next;
2674 tmp_list->next = icon_set->cache;
2675 icon_set->cache = tmp_list;
2678 return icon->pixbuf;
2682 tmp_list = g_slist_next (tmp_list);
2689 add_to_cache (GtkIconSet *icon_set,
2691 GtkTextDirection direction,
2698 ensure_cache_up_to_date (icon_set);
2700 g_object_ref (pixbuf);
2702 /* We have to ref the style, since if the style was finalized
2703 * its address could be reused by another style, creating a
2708 g_object_ref (style);
2710 icon = g_new (CachedIcon, 1);
2711 icon_set->cache = g_slist_prepend (icon_set->cache, icon);
2712 icon_set->cache_size++;
2714 icon->style = style;
2715 icon->direction = direction;
2716 icon->state = state;
2718 icon->pixbuf = pixbuf;
2721 attach_to_style (icon_set, icon->style);
2723 if (icon_set->cache_size >= NUM_CACHED_ICONS)
2725 /* Remove oldest item in the cache */
2728 tmp_list = icon_set->cache;
2730 /* Find next-to-last link */
2731 g_assert (NUM_CACHED_ICONS > 2);
2732 while (tmp_list->next->next)
2733 tmp_list = tmp_list->next;
2735 g_assert (tmp_list != NULL);
2736 g_assert (tmp_list->next != NULL);
2737 g_assert (tmp_list->next->next == NULL);
2739 /* Free the last icon */
2740 icon = tmp_list->next->data;
2742 g_slist_free (tmp_list->next);
2743 tmp_list->next = NULL;
2745 cached_icon_free (icon);
2750 clear_cache (GtkIconSet *icon_set,
2751 gboolean style_detach)
2753 GSList *cache, *tmp_list;
2754 GtkStyle *last_style = NULL;
2756 cache = icon_set->cache;
2757 icon_set->cache = NULL;
2758 icon_set->cache_size = 0;
2760 while (tmp_list != NULL)
2762 CachedIcon *icon = tmp_list->data;
2766 /* simple optimization for the case where the cache
2767 * contains contiguous icons from the same style.
2768 * it's safe to call detach_from_style more than
2769 * once on the same style though.
2771 if (last_style != icon->style)
2773 detach_from_style (icon_set, icon->style);
2774 last_style = icon->style;
2778 cached_icon_free (icon);
2780 tmp_list = g_slist_next (tmp_list);
2783 g_slist_free (cache);
2787 copy_cache (GtkIconSet *icon_set,
2788 GtkIconSet *copy_recipient)
2791 GSList *copy = NULL;
2793 ensure_cache_up_to_date (icon_set);
2795 tmp_list = icon_set->cache;
2796 while (tmp_list != NULL)
2798 CachedIcon *icon = tmp_list->data;
2799 CachedIcon *icon_copy = g_new (CachedIcon, 1);
2803 if (icon_copy->style)
2805 attach_to_style (copy_recipient, icon_copy->style);
2806 g_object_ref (icon_copy->style);
2809 g_object_ref (icon_copy->pixbuf);
2811 icon_copy->size = icon->size;
2813 copy = g_slist_prepend (copy, icon_copy);
2815 tmp_list = g_slist_next (tmp_list);
2818 return g_slist_reverse (copy);
2822 attach_to_style (GtkIconSet *icon_set,
2827 table = g_object_get_qdata (G_OBJECT (style),
2828 g_quark_try_string ("gtk-style-icon-sets"));
2832 table = g_hash_table_new (NULL, NULL);
2833 g_object_set_qdata_full (G_OBJECT (style),
2834 g_quark_from_static_string ("gtk-style-icon-sets"),
2839 g_hash_table_insert (table, icon_set, icon_set);
2843 detach_from_style (GtkIconSet *icon_set,
2848 table = g_object_get_qdata (G_OBJECT (style),
2849 g_quark_try_string ("gtk-style-icon-sets"));
2852 g_hash_table_remove (table, icon_set);
2856 iconsets_foreach (gpointer key,
2860 GtkIconSet *icon_set = key;
2862 /* We only need to remove cache entries for the given style;
2863 * but that complicates things because in destroy notify
2864 * we don't know which style got destroyed, and 95% of the
2865 * time all cache entries will have the same style,
2866 * so this is faster anyway.
2869 clear_cache (icon_set, FALSE);
2873 style_dnotify (gpointer data)
2875 GHashTable *table = data;
2877 g_hash_table_foreach (table, iconsets_foreach, NULL);
2879 g_hash_table_destroy (table);
2882 /* This allows the icon set to detect that its cache is out of date. */
2884 _gtk_icon_set_invalidate_caches (void)
2890 listify_foreach (gpointer key, gpointer value, gpointer data)
2892 GSList **list = data;
2894 *list = g_slist_prepend (*list, key);
2898 g_hash_table_get_keys (GHashTable *table)
2900 GSList *list = NULL;
2902 g_hash_table_foreach (table, listify_foreach, &list);
2908 * _gtk_icon_factory_list_ids:
2910 * Gets all known IDs stored in an existing icon factory.
2911 * The strings in the returned list aren't copied.
2912 * The list itself should be freed.
2914 * Return value: List of ids in icon factories
2917 _gtk_icon_factory_list_ids (void)
2924 ensure_default_icons ();
2926 tmp_list = all_icon_factories;
2927 while (tmp_list != NULL)
2931 GtkIconFactory *factory = GTK_ICON_FACTORY (tmp_list->data);
2933 these_ids = g_hash_table_get_keys (factory->icons);
2935 ids = g_slist_concat (ids, these_ids);
2937 tmp_list = g_slist_next (tmp_list);