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/.
30 #include <pango/pango-utils.h> /* For pango_scan_* */
31 #include "gtkiconfactory.h"
32 #include "stock-icons/gtkstockpixbufs.h"
34 #include "gtkicontheme.h"
35 #include "gtksettings.h"
37 #include "gtkwidget.h"
40 static GSList *all_icon_factories = NULL;
43 GTK_ICON_SOURCE_EMPTY,
44 GTK_ICON_SOURCE_ICON_NAME,
45 GTK_ICON_SOURCE_FILENAME,
46 GTK_ICON_SOURCE_PIXBUF
51 GtkIconSourceType type;
59 GdkPixbuf *filename_pixbuf;
61 GtkTextDirection direction;
65 /* If TRUE, then the parameter is wildcarded, and the above
66 * fields should be ignored. If FALSE, the parameter is
67 * specified, and the above fields should be valid.
69 guint any_direction : 1;
74 static gpointer parent_class = NULL;
76 static void gtk_icon_factory_init (GtkIconFactory *icon_factory);
77 static void gtk_icon_factory_class_init (GtkIconFactoryClass *klass);
78 static void gtk_icon_factory_finalize (GObject *object);
79 static void get_default_icons (GtkIconFactory *icon_factory);
80 static void icon_source_clear (GtkIconSource *source);
82 static GtkIconSize icon_size_register_intern (const gchar *name,
86 #define GTK_ICON_SOURCE_INIT(any_direction, any_state, any_size) \
87 { GTK_ICON_SOURCE_EMPTY, { NULL }, NULL, \
89 any_direction, any_state, any_size }
92 gtk_icon_factory_get_type (void)
94 static GType icon_factory_type = 0;
96 if (!icon_factory_type)
98 static const GTypeInfo icon_factory_info =
100 sizeof (GtkIconFactoryClass),
101 NULL, /* base_init */
102 NULL, /* base_finalize */
103 (GClassInitFunc) gtk_icon_factory_class_init,
104 NULL, /* class_finalize */
105 NULL, /* class_data */
106 sizeof (GtkIconFactory),
108 (GInstanceInitFunc) gtk_icon_factory_init,
112 g_type_register_static (G_TYPE_OBJECT, "GtkIconFactory",
113 &icon_factory_info, 0);
116 return icon_factory_type;
120 gtk_icon_factory_init (GtkIconFactory *factory)
122 factory->icons = g_hash_table_new (g_str_hash, g_str_equal);
123 all_icon_factories = g_slist_prepend (all_icon_factories, factory);
127 gtk_icon_factory_class_init (GtkIconFactoryClass *klass)
129 GObjectClass *object_class = G_OBJECT_CLASS (klass);
131 parent_class = g_type_class_peek_parent (klass);
133 object_class->finalize = gtk_icon_factory_finalize;
137 free_icon_set (gpointer key, gpointer value, gpointer data)
140 gtk_icon_set_unref (value);
144 gtk_icon_factory_finalize (GObject *object)
146 GtkIconFactory *factory = GTK_ICON_FACTORY (object);
148 all_icon_factories = g_slist_remove (all_icon_factories, factory);
150 g_hash_table_foreach (factory->icons, free_icon_set, NULL);
152 g_hash_table_destroy (factory->icons);
154 G_OBJECT_CLASS (parent_class)->finalize (object);
158 * gtk_icon_factory_new:
160 * Creates a new #GtkIconFactory. An icon factory manages a collection
161 * of #GtkIconSet<!-- -->s; a #GtkIconSet manages a set of variants of a
162 * particular icon (i.e. a #GtkIconSet contains variants for different
163 * sizes and widget states). Icons in an icon factory are named by a
164 * stock ID, which is a simple string identifying the icon. Each
165 * #GtkStyle has a list of #GtkIconFactory<!-- -->s derived from the current
166 * theme; those icon factories are consulted first when searching for
167 * an icon. If the theme doesn't set a particular icon, GTK+ looks for
168 * the icon in a list of default icon factories, maintained by
169 * gtk_icon_factory_add_default() and
170 * gtk_icon_factory_remove_default(). Applications with icons should
171 * add a default icon factory with their icons, which will allow
172 * themes to override the icons for the application.
174 * Return value: a new #GtkIconFactory
177 gtk_icon_factory_new (void)
179 return g_object_new (GTK_TYPE_ICON_FACTORY, NULL);
183 * gtk_icon_factory_add:
184 * @factory: a #GtkIconFactory
185 * @stock_id: icon name
186 * @icon_set: icon set
188 * Adds the given @icon_set to the icon factory, under the name
189 * @stock_id. @stock_id should be namespaced for your application,
190 * e.g. "myapp-whatever-icon". Normally applications create a
191 * #GtkIconFactory, then add it to the list of default factories with
192 * gtk_icon_factory_add_default(). Then they pass the @stock_id to
193 * widgets such as #GtkImage to display the icon. Themes can provide
194 * an icon with the same name (such as "myapp-whatever-icon") to
195 * override your application's default icons. If an icon already
196 * existed in @factory for @stock_id, it is unreferenced and replaced
197 * with the new @icon_set.
201 gtk_icon_factory_add (GtkIconFactory *factory,
202 const gchar *stock_id,
203 GtkIconSet *icon_set)
205 gpointer old_key = NULL;
206 gpointer old_value = NULL;
208 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
209 g_return_if_fail (stock_id != NULL);
210 g_return_if_fail (icon_set != NULL);
212 g_hash_table_lookup_extended (factory->icons, stock_id,
213 &old_key, &old_value);
215 if (old_value == icon_set)
218 gtk_icon_set_ref (icon_set);
220 /* GHashTable key memory management is so fantastically broken. */
222 g_hash_table_insert (factory->icons, old_key, icon_set);
224 g_hash_table_insert (factory->icons, g_strdup (stock_id), icon_set);
227 gtk_icon_set_unref (old_value);
231 * gtk_icon_factory_lookup:
232 * @factory: a #GtkIconFactory
233 * @stock_id: an icon name
235 * Looks up @stock_id in the icon factory, returning an icon set
236 * if found, otherwise %NULL. For display to the user, you should
237 * use gtk_style_lookup_icon_set() on the #GtkStyle for the
238 * widget that will display the icon, instead of using this
239 * function directly, so that themes are taken into account.
241 * Return value: icon set of @stock_id.
244 gtk_icon_factory_lookup (GtkIconFactory *factory,
245 const gchar *stock_id)
247 g_return_val_if_fail (GTK_IS_ICON_FACTORY (factory), NULL);
248 g_return_val_if_fail (stock_id != NULL, NULL);
250 return g_hash_table_lookup (factory->icons, stock_id);
253 static GtkIconFactory *gtk_default_icons = NULL;
254 static GSList *default_factories = NULL;
257 * gtk_icon_factory_add_default:
258 * @factory: a #GtkIconFactory
260 * Adds an icon factory to the list of icon factories searched by
261 * gtk_style_lookup_icon_set(). This means that, for example,
262 * gtk_image_new_from_stock() will be able to find icons in @factory.
263 * There will normally be an icon factory added for each library or
264 * application that comes with icons. The default icon factories
265 * can be overridden by themes.
269 gtk_icon_factory_add_default (GtkIconFactory *factory)
271 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
273 g_object_ref (factory);
275 default_factories = g_slist_prepend (default_factories, factory);
279 * gtk_icon_factory_remove_default:
280 * @factory: a #GtkIconFactory previously added with gtk_icon_factory_add_default()
282 * Removes an icon factory from the list of default icon
283 * factories. Not normally used; you might use it for a library that
284 * can be unloaded or shut down.
288 gtk_icon_factory_remove_default (GtkIconFactory *factory)
290 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
292 default_factories = g_slist_remove (default_factories, factory);
294 g_object_unref (factory);
298 ensure_default_icons (void)
300 if (gtk_default_icons == NULL)
302 gtk_default_icons = gtk_icon_factory_new ();
304 get_default_icons (gtk_default_icons);
309 * gtk_icon_factory_lookup_default:
310 * @stock_id: an icon name
312 * Looks for an icon in the list of default icon factories. For
313 * display to the user, you should use gtk_style_lookup_icon_set() on
314 * the #GtkStyle for the widget that will display the icon, instead of
315 * using this function directly, so that themes are taken into
319 * Return value: a #GtkIconSet, or %NULL
322 gtk_icon_factory_lookup_default (const gchar *stock_id)
326 g_return_val_if_fail (stock_id != NULL, NULL);
328 tmp_list = default_factories;
329 while (tmp_list != NULL)
331 GtkIconSet *icon_set =
332 gtk_icon_factory_lookup (GTK_ICON_FACTORY (tmp_list->data),
338 tmp_list = g_slist_next (tmp_list);
341 ensure_default_icons ();
343 return gtk_icon_factory_lookup (gtk_default_icons, stock_id);
347 register_stock_icon (GtkIconFactory *factory,
348 const gchar *stock_id)
350 GtkIconSet *set = gtk_icon_set_new ();
351 GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
353 source.type = GTK_ICON_SOURCE_ICON_NAME;
354 source.source.icon_name = (gchar *)stock_id;
355 gtk_icon_set_add_source (set, &source);
357 gtk_icon_factory_add (factory, stock_id, set);
358 gtk_icon_set_unref (set);
362 register_bidi_stock_icon (GtkIconFactory *factory,
363 const gchar *stock_id,
364 const gchar *stock_id_ltr,
365 const gchar *stock_id_rtl)
367 GtkIconSet *set = gtk_icon_set_new ();
368 GtkIconSource source = GTK_ICON_SOURCE_INIT (FALSE, TRUE, TRUE);
370 source.type = GTK_ICON_SOURCE_ICON_NAME;
371 source.source.icon_name = (gchar *)stock_id_ltr;
372 source.direction = GTK_TEXT_DIR_LTR;
373 gtk_icon_set_add_source (set, &source);
375 source.type = GTK_ICON_SOURCE_ICON_NAME;
376 source.source.icon_name = (gchar *)stock_id_rtl;
377 source.direction = GTK_TEXT_DIR_RTL;
378 gtk_icon_set_add_source (set, &source);
380 gtk_icon_factory_add (factory, stock_id, set);
381 gtk_icon_set_unref (set);
385 add_default_image (const gchar *stock_id,
387 const guchar *inline_data)
389 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_inline (-1, inline_data, FALSE, NULL);
392 gtk_icon_theme_add_builtin_icon (stock_id, size, pixbuf);
394 g_object_unref (pixbuf);
398 add_icon (GtkIconFactory *factory,
399 const gchar *stock_id,
401 const guchar *inline_data)
403 register_stock_icon (factory, stock_id);
405 add_default_image (stock_id, size, inline_data);
409 add_icon2 (GtkIconFactory *factory,
410 const gchar *stock_id,
412 const guchar *inline_data1,
414 const guchar *inline_data2)
416 register_stock_icon (factory, stock_id);
418 add_default_image (stock_id, size1, inline_data1);
419 add_default_image (stock_id, size2, inline_data2);
423 add_icon_bidi2 (GtkIconFactory *factory,
424 const gchar *stock_id,
426 const guchar *inline_data_ltr1,
427 const guchar *inline_data_rtl1,
429 const guchar *inline_data_ltr2,
430 const guchar *inline_data_rtl2)
432 gchar *stock_id_ltr = g_strconcat (stock_id, "-ltr", NULL);
433 gchar *stock_id_rtl = g_strconcat (stock_id, "-rtl", NULL);
435 register_bidi_stock_icon (factory, stock_id,
436 stock_id_ltr, stock_id_rtl);
438 add_default_image (stock_id_ltr, size1, inline_data_ltr1);
439 add_default_image (stock_id_ltr, size2, inline_data_ltr2);
441 add_default_image (stock_id_rtl, size1, inline_data_rtl1);
442 add_default_image (stock_id_rtl, size2, inline_data_rtl2);
444 g_free (stock_id_ltr);
445 g_free (stock_id_rtl);
449 get_default_icons (GtkIconFactory *factory)
451 /* KEEP IN SYNC with gtkstock.c */
453 /* Have dialog size */
454 add_icon (factory, GTK_STOCK_DIALOG_ERROR, 48, stock_dialog_error_48);
455 add_icon (factory, GTK_STOCK_DIALOG_INFO, 48, stock_dialog_info_48);
456 add_icon (factory, GTK_STOCK_DIALOG_QUESTION, 48, stock_dialog_question_48);
457 add_icon (factory, GTK_STOCK_DIALOG_WARNING, 48, stock_dialog_warning_48);
460 add_icon (factory, GTK_STOCK_DND, 32, stock_dnd_32);
461 add_icon (factory, GTK_STOCK_DND_MULTIPLE, 32, stock_dnd_multiple_32);
463 /* Have button sizes */
464 add_icon (factory, GTK_STOCK_APPLY, 20, stock_apply_20);
465 add_icon (factory, GTK_STOCK_CANCEL, 20, stock_cancel_20);
466 add_icon (factory, GTK_STOCK_NO, 20, stock_no_20);
467 add_icon (factory, GTK_STOCK_OK, 20, stock_ok_20);
468 add_icon (factory, GTK_STOCK_YES, 20, stock_yes_20);
470 /* Generic + button sizes */
471 add_icon2 (factory, GTK_STOCK_CLOSE,
475 /* Generic + menu sizes */
476 add_icon2 (factory, GTK_STOCK_ADD,
480 add_icon2 (factory, GTK_STOCK_JUSTIFY_CENTER,
481 16, stock_align_center_16,
482 24, stock_align_center_24);
484 add_icon2 (factory, GTK_STOCK_JUSTIFY_FILL,
485 16, stock_align_justify_16,
486 24, stock_align_justify_24);
488 add_icon2 (factory, GTK_STOCK_JUSTIFY_LEFT,
489 16, stock_align_left_16,
490 24, stock_align_left_24);
492 add_icon2 (factory, GTK_STOCK_JUSTIFY_RIGHT,
493 16, stock_align_right_16,
494 24, stock_align_right_24);
496 add_icon2 (factory, GTK_STOCK_GOTO_BOTTOM,
498 24, stock_bottom_24);
500 add_icon2 (factory, GTK_STOCK_CDROM,
504 add_icon2 (factory, GTK_STOCK_CONVERT,
505 16, stock_convert_16,
506 24, stock_convert_24);
508 add_icon2 (factory, GTK_STOCK_COPY,
512 add_icon2 (factory, GTK_STOCK_CUT,
516 add_icon2 (factory, GTK_STOCK_GO_DOWN,
517 16, stock_down_arrow_16,
518 24, stock_down_arrow_24);
520 add_icon2 (factory, GTK_STOCK_EXECUTE,
524 add_icon2 (factory, GTK_STOCK_QUIT,
528 add_icon_bidi2 (factory, GTK_STOCK_GOTO_FIRST,
529 16, stock_first_16, stock_last_16,
530 24, stock_first_24, stock_last_24);
532 add_icon2 (factory, GTK_STOCK_SELECT_FONT,
536 add_icon2 (factory, GTK_STOCK_HELP,
540 add_icon2 (factory, GTK_STOCK_HOME,
544 add_icon_bidi2 (factory, GTK_STOCK_JUMP_TO,
545 16, stock_jump_to_16, stock_jump_to_rtl_16,
546 24, stock_jump_to_24, stock_jump_to_rtl_24);
548 add_icon_bidi2 (factory, GTK_STOCK_GOTO_LAST,
549 16, stock_last_16, stock_first_16,
550 24, stock_last_24, stock_first_24);
552 add_icon_bidi2 (factory, GTK_STOCK_GO_BACK,
553 16, stock_left_arrow_16, stock_right_arrow_16,
554 24, stock_left_arrow_24, stock_right_arrow_24);
556 add_icon2 (factory, GTK_STOCK_MISSING_IMAGE,
557 16, stock_missing_image_16,
558 24, stock_missing_image_24);
560 add_icon2 (factory, GTK_STOCK_NEW,
564 add_icon2 (factory, GTK_STOCK_OPEN,
568 add_icon2 (factory, GTK_STOCK_PASTE,
572 add_icon2 (factory, GTK_STOCK_PREFERENCES,
573 16, stock_preferences_16,
574 24, stock_preferences_24);
576 add_icon2 (factory, GTK_STOCK_PRINT,
580 add_icon2 (factory, GTK_STOCK_PRINT_PREVIEW,
581 16, stock_print_preview_16,
582 24, stock_print_preview_24);
584 add_icon2 (factory, GTK_STOCK_PROPERTIES,
585 16, stock_properties_16,
586 24, stock_properties_24);
588 add_icon_bidi2 (factory, GTK_STOCK_REDO,
589 16, stock_redo_16, stock_redo_rtl_16,
590 24, stock_redo_24, stock_redo_rtl_24);
592 add_icon2 (factory, GTK_STOCK_REMOVE,
594 24, stock_remove_24);
596 add_icon2 (factory, GTK_STOCK_REFRESH,
597 16, stock_refresh_16,
598 24, stock_refresh_24);
600 add_icon_bidi2 (factory, GTK_STOCK_REVERT_TO_SAVED,
601 16, stock_revert_16, stock_revert_rtl_16,
602 24, stock_revert_24, stock_revert_rtl_24);
604 add_icon_bidi2 (factory, GTK_STOCK_GO_FORWARD,
605 16, stock_right_arrow_16, stock_left_arrow_16,
606 24, stock_right_arrow_24, stock_left_arrow_24);
608 add_icon2 (factory, GTK_STOCK_SAVE,
612 add_icon2 (factory, GTK_STOCK_FLOPPY,
616 add_icon2 (factory, GTK_STOCK_SAVE_AS,
617 16, stock_save_as_16,
618 24, stock_save_as_24);
620 add_icon2 (factory, GTK_STOCK_FIND,
622 24, stock_search_24);
624 add_icon2 (factory, GTK_STOCK_FIND_AND_REPLACE,
625 16, stock_search_replace_16,
626 24, stock_search_replace_24);
628 add_icon2 (factory, GTK_STOCK_SORT_DESCENDING,
629 16, stock_sort_descending_16,
630 24, stock_sort_descending_24);
632 add_icon2 (factory, GTK_STOCK_SORT_ASCENDING,
633 16, stock_sort_ascending_16,
634 24, stock_sort_ascending_24);
636 add_icon2 (factory, GTK_STOCK_SPELL_CHECK,
637 16, stock_spellcheck_16,
638 24, stock_spellcheck_24);
640 add_icon2 (factory, GTK_STOCK_STOP,
644 add_icon2 (factory, GTK_STOCK_BOLD,
645 16, stock_text_bold_16,
646 24, stock_text_bold_24);
648 add_icon2 (factory, GTK_STOCK_ITALIC,
649 16, stock_text_italic_16,
650 24, stock_text_italic_24);
652 add_icon2 (factory, GTK_STOCK_STRIKETHROUGH,
653 16, stock_text_strikethrough_16,
654 24, stock_text_strikethrough_24);
656 add_icon2 (factory, GTK_STOCK_UNDERLINE,
657 16, stock_text_underline_16,
658 24, stock_text_underline_24);
660 add_icon2 (factory, GTK_STOCK_GOTO_TOP,
664 add_icon2 (factory, GTK_STOCK_DELETE,
668 add_icon_bidi2 (factory, GTK_STOCK_UNDELETE,
669 16, stock_undelete_16, stock_undelete_rtl_16,
670 24, stock_undelete_24, stock_undelete_rtl_24);
672 add_icon_bidi2 (factory, GTK_STOCK_UNDO,
673 16, stock_undo_16, stock_undo_rtl_16,
674 24, stock_undo_24, stock_undo_rtl_24);
676 add_icon2 (factory, GTK_STOCK_GO_UP,
677 16, stock_up_arrow_16,
678 24, stock_up_arrow_24);
680 /* Generic size only */
682 add_icon (factory, GTK_STOCK_CLEAR, 24, stock_clear_24);
683 add_icon (factory, GTK_STOCK_SELECT_COLOR, 24, stock_colorselector_24);
684 add_icon (factory, GTK_STOCK_COLOR_PICKER, 25, stock_color_picker_25);
685 add_icon (factory, GTK_STOCK_INDEX, 24, stock_index_24);
686 add_icon (factory, GTK_STOCK_ZOOM_100, 24, stock_zoom_1_24);
687 add_icon (factory, GTK_STOCK_ZOOM_FIT, 24, stock_zoom_fit_24);
688 add_icon (factory, GTK_STOCK_ZOOM_IN, 24, stock_zoom_in_24);
689 add_icon (factory, GTK_STOCK_ZOOM_OUT, 24, stock_zoom_out_24);
692 /************************************************************
693 * Icon size handling *
694 ************************************************************/
696 typedef struct _IconSize IconSize;
707 typedef struct _IconAlias IconAlias;
715 typedef struct _SettingsIconSize SettingsIconSize;
717 struct _SettingsIconSize
723 static GHashTable *icon_aliases = NULL;
724 static IconSize *icon_sizes = NULL;
725 static gint icon_sizes_allocated = 0;
726 static gint icon_sizes_used = 0;
729 init_icon_sizes (void)
731 if (icon_sizes == NULL)
733 #define NUM_BUILTIN_SIZES 7
736 icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
738 icon_sizes = g_new (IconSize, NUM_BUILTIN_SIZES);
739 icon_sizes_allocated = NUM_BUILTIN_SIZES;
740 icon_sizes_used = NUM_BUILTIN_SIZES;
742 icon_sizes[GTK_ICON_SIZE_INVALID].size = 0;
743 icon_sizes[GTK_ICON_SIZE_INVALID].name = NULL;
744 icon_sizes[GTK_ICON_SIZE_INVALID].width = 0;
745 icon_sizes[GTK_ICON_SIZE_INVALID].height = 0;
747 /* the name strings aren't copied since we don't ever remove
748 * icon sizes, so we don't need to know whether they're static.
749 * Even if we did I suppose removing the builtin sizes would be
753 icon_sizes[GTK_ICON_SIZE_MENU].size = GTK_ICON_SIZE_MENU;
754 icon_sizes[GTK_ICON_SIZE_MENU].name = "gtk-menu";
755 icon_sizes[GTK_ICON_SIZE_MENU].width = 16;
756 icon_sizes[GTK_ICON_SIZE_MENU].height = 16;
758 icon_sizes[GTK_ICON_SIZE_BUTTON].size = GTK_ICON_SIZE_BUTTON;
759 icon_sizes[GTK_ICON_SIZE_BUTTON].name = "gtk-button";
760 icon_sizes[GTK_ICON_SIZE_BUTTON].width = 20;
761 icon_sizes[GTK_ICON_SIZE_BUTTON].height = 20;
763 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].size = GTK_ICON_SIZE_SMALL_TOOLBAR;
764 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].name = "gtk-small-toolbar";
765 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].width = 18;
766 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].height = 18;
768 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].size = GTK_ICON_SIZE_LARGE_TOOLBAR;
769 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].name = "gtk-large-toolbar";
770 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].width = 24;
771 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].height = 24;
773 icon_sizes[GTK_ICON_SIZE_DND].size = GTK_ICON_SIZE_DND;
774 icon_sizes[GTK_ICON_SIZE_DND].name = "gtk-dnd";
775 icon_sizes[GTK_ICON_SIZE_DND].width = 32;
776 icon_sizes[GTK_ICON_SIZE_DND].height = 32;
778 icon_sizes[GTK_ICON_SIZE_DIALOG].size = GTK_ICON_SIZE_DIALOG;
779 icon_sizes[GTK_ICON_SIZE_DIALOG].name = "gtk-dialog";
780 icon_sizes[GTK_ICON_SIZE_DIALOG].width = 48;
781 icon_sizes[GTK_ICON_SIZE_DIALOG].height = 48;
783 g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
785 /* Alias everything to itself. */
786 i = 1; /* skip invalid size */
787 while (i < NUM_BUILTIN_SIZES)
789 gtk_icon_size_register_alias (icon_sizes[i].name, icon_sizes[i].size);
794 #undef NUM_BUILTIN_SIZES
799 free_settings_sizes (gpointer data)
801 g_array_free (data, TRUE);
805 get_settings_sizes (GtkSettings *settings,
808 GArray *settings_sizes;
809 static GQuark sizes_quark = 0;
812 sizes_quark = g_quark_from_static_string ("gtk-icon-sizes");
814 settings_sizes = g_object_get_qdata (G_OBJECT (settings), sizes_quark);
817 settings_sizes = g_array_new (FALSE, FALSE, sizeof (SettingsIconSize));
818 g_object_set_qdata_full (G_OBJECT (settings), sizes_quark,
819 settings_sizes, free_settings_sizes);
824 return settings_sizes;
828 icon_size_set_for_settings (GtkSettings *settings,
829 const gchar *size_name,
834 GArray *settings_sizes;
835 SettingsIconSize *settings_size;
837 g_return_if_fail (size_name != NULL);
839 size = gtk_icon_size_from_name (size_name);
840 if (size == GTK_ICON_SIZE_INVALID)
841 /* Reserve a place */
842 size = icon_size_register_intern (size_name, -1, -1);
844 settings_sizes = get_settings_sizes (settings, NULL);
845 if (size >= settings_sizes->len)
847 SettingsIconSize unset = { -1, -1 };
850 for (i = settings_sizes->len; i <= size; i++)
851 g_array_append_val (settings_sizes, unset);
854 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
856 settings_size->width = width;
857 settings_size->height = height;
860 /* Like pango_parse_word, but accept - as well
863 scan_icon_size_name (const char **pos, GString *out)
865 const char *p = *pos;
867 while (g_ascii_isspace (*p))
870 if (!((*p >= 'A' && *p <= 'Z') ||
871 (*p >= 'a' && *p <= 'z') ||
872 *p == '_' || *p == '-'))
875 g_string_truncate (out, 0);
876 g_string_append_c (out, *p);
879 while ((*p >= 'A' && *p <= 'Z') ||
880 (*p >= 'a' && *p <= 'z') ||
881 (*p >= '0' && *p <= '9') ||
882 *p == '_' || *p == '-')
884 g_string_append_c (out, *p);
894 icon_size_setting_parse (GtkSettings *settings,
895 const gchar *icon_size_string)
897 GString *name_buf = g_string_new (NULL);
898 const gchar *p = icon_size_string;
900 while (pango_skip_space (&p))
904 if (!scan_icon_size_name (&p, name_buf))
907 if (!pango_skip_space (&p))
915 if (!pango_scan_int (&p, &width))
918 if (!pango_skip_space (&p))
926 if (!pango_scan_int (&p, &height))
929 if (width > 0 && height > 0)
931 icon_size_set_for_settings (settings, name_buf->str,
936 g_warning ("Invalid size in gtk-icon-sizes: %d,%d\n", width, height);
939 pango_skip_space (&p);
948 g_string_free (name_buf, TRUE);
952 g_warning ("Error parsing gtk-icon-sizes string:\n\t'%s'", icon_size_string);
953 g_string_free (name_buf, TRUE);
957 icon_size_set_all_from_settings (GtkSettings *settings)
959 GArray *settings_sizes;
960 gchar *icon_size_string;
962 /* Reset old settings */
963 settings_sizes = get_settings_sizes (settings, NULL);
964 g_array_set_size (settings_sizes, 0);
966 g_object_get (settings,
967 "gtk-icon-sizes", &icon_size_string,
970 if (icon_size_string)
972 icon_size_setting_parse (settings, icon_size_string);
973 g_free (icon_size_string);
978 icon_size_settings_changed (GtkSettings *settings,
981 icon_size_set_all_from_settings (settings);
983 _gtk_rc_reset_styles (settings);
987 icon_sizes_init_for_settings (GtkSettings *settings)
989 g_signal_connect (settings,
990 "notify::gtk-icon-sizes",
991 G_CALLBACK (icon_size_settings_changed),
994 icon_size_set_all_from_settings (settings);
998 icon_size_lookup_intern (GtkSettings *settings,
1003 GArray *settings_sizes;
1004 gint width_for_settings = -1;
1005 gint height_for_settings = -1;
1009 if (size >= icon_sizes_used)
1012 if (size == GTK_ICON_SIZE_INVALID)
1017 gboolean initial = FALSE;
1019 settings_sizes = get_settings_sizes (settings, &initial);
1021 icon_sizes_init_for_settings (settings);
1023 if (size < settings_sizes->len)
1025 SettingsIconSize *settings_size;
1027 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
1029 width_for_settings = settings_size->width;
1030 height_for_settings = settings_size->height;
1035 *widthp = width_for_settings >= 0 ? width_for_settings : icon_sizes[size].width;
1038 *heightp = height_for_settings >= 0 ? height_for_settings : icon_sizes[size].height;
1044 * gtk_icon_size_lookup_for_settings:
1045 * @settings: a #GtkSettings object, used to determine
1046 * which set of user preferences to used.
1047 * @size: an icon size
1048 * @width: location to store icon width
1049 * @height: location to store icon height
1051 * Obtains the pixel size of a semantic icon size, possibly
1052 * modified by user preferences for a particular
1053 * #GtkSettings. Normally @size would be
1054 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
1055 * isn't normally needed, gtk_widget_render_icon() is the usual
1056 * way to get an icon for rendering, then just look at the size of
1057 * the rendered pixbuf. The rendered pixbuf may not even correspond to
1058 * the width/height returned by gtk_icon_size_lookup(), because themes
1059 * are free to render the pixbuf however they like, including changing
1062 * Return value: %TRUE if @size was a valid size
1067 gtk_icon_size_lookup_for_settings (GtkSettings *settings,
1072 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1074 return icon_size_lookup_intern (settings, size, width, height);
1078 * gtk_icon_size_lookup:
1079 * @size: an icon size
1080 * @width: location to store icon width
1081 * @height: location to store icon height
1083 * Obtains the pixel size of a semantic icon size, possibly
1084 * modified by user preferences for the default #GtkSettings.
1085 * (See gtk_icon_size_lookup_for_settings().)
1086 * Normally @size would be
1087 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
1088 * isn't normally needed, gtk_widget_render_icon() is the usual
1089 * way to get an icon for rendering, then just look at the size of
1090 * the rendered pixbuf. The rendered pixbuf may not even correspond to
1091 * the width/height returned by gtk_icon_size_lookup(), because themes
1092 * are free to render the pixbuf however they like, including changing
1095 * Return value: %TRUE if @size was a valid size
1098 gtk_icon_size_lookup (GtkIconSize size,
1102 GTK_NOTE (MULTIHEAD,
1103 g_warning ("gtk_icon_size_lookup ()) is not multihead safe"));
1105 return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (),
1106 size, widthp, heightp);
1110 icon_size_register_intern (const gchar *name,
1114 IconAlias *old_alias;
1119 old_alias = g_hash_table_lookup (icon_aliases, name);
1120 if (old_alias && icon_sizes[old_alias->target].width > 0)
1122 g_warning ("Icon size name '%s' already exists", name);
1123 return GTK_ICON_SIZE_INVALID;
1128 size = old_alias->target;
1132 if (icon_sizes_used == icon_sizes_allocated)
1134 icon_sizes_allocated *= 2;
1135 icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated);
1138 size = icon_sizes_used++;
1140 /* alias to self. */
1141 gtk_icon_size_register_alias (name, size);
1143 icon_sizes[size].size = size;
1144 icon_sizes[size].name = g_strdup (name);
1147 icon_sizes[size].width = width;
1148 icon_sizes[size].height = height;
1154 * gtk_icon_size_register:
1155 * @name: name of the icon size
1156 * @width: the icon width
1157 * @height: the icon height
1159 * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
1160 * etc. Returns the integer value for the size.
1162 * Returns: integer value representing the size
1166 gtk_icon_size_register (const gchar *name,
1170 g_return_val_if_fail (name != NULL, 0);
1171 g_return_val_if_fail (width > 0, 0);
1172 g_return_val_if_fail (height > 0, 0);
1174 return icon_size_register_intern (name, width, height);
1178 * gtk_icon_size_register_alias:
1179 * @alias: an alias for @target
1180 * @target: an existing icon size
1182 * Registers @alias as another name for @target.
1183 * So calling gtk_icon_size_from_name() with @alias as argument
1184 * will return @target.
1188 gtk_icon_size_register_alias (const gchar *alias,
1193 g_return_if_fail (alias != NULL);
1197 if (!icon_size_lookup_intern (NULL, target, NULL, NULL))
1198 g_warning ("gtk_icon_size_register_alias: Icon size %d does not exist", target);
1200 ia = g_hash_table_lookup (icon_aliases, alias);
1203 if (icon_sizes[ia->target].width > 0)
1205 g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias);
1209 ia->target = target;
1214 ia = g_new (IconAlias, 1);
1215 ia->name = g_strdup (alias);
1216 ia->target = target;
1218 g_hash_table_insert (icon_aliases, ia->name, ia);
1223 * gtk_icon_size_from_name:
1224 * @name: the name to look up.
1225 * @returns: the icon size with the given name.
1227 * Looks up the icon size associated with @name.
1230 gtk_icon_size_from_name (const gchar *name)
1236 ia = g_hash_table_lookup (icon_aliases, name);
1238 if (ia && icon_sizes[ia->target].width > 0)
1241 return GTK_ICON_SIZE_INVALID;
1245 * gtk_icon_size_get_name:
1246 * @size: a #GtkIconSize.
1247 * @returns: the name of the given icon size.
1249 * Gets the canonical name of the given icon size. The returned string
1250 * is statically allocated and should not be freed.
1252 G_CONST_RETURN gchar*
1253 gtk_icon_size_get_name (GtkIconSize size)
1255 if (size >= icon_sizes_used)
1258 return icon_sizes[size].name;
1261 /************************************************************/
1266 static GdkPixbuf *find_in_cache (GtkIconSet *icon_set,
1268 GtkTextDirection direction,
1271 static void add_to_cache (GtkIconSet *icon_set,
1273 GtkTextDirection direction,
1277 /* Clear icon set contents, drop references to all contained
1278 * GdkPixbuf objects and forget all GtkIconSources. Used to
1279 * recycle an icon set.
1281 static void clear_cache (GtkIconSet *icon_set,
1282 gboolean style_detach);
1283 static GSList* copy_cache (GtkIconSet *icon_set,
1284 GtkIconSet *copy_recipient);
1285 static void attach_to_style (GtkIconSet *icon_set,
1287 static void detach_from_style (GtkIconSet *icon_set,
1289 static void style_dnotify (gpointer data);
1297 /* Cache of the last few rendered versions of the icon. */
1305 static guint cache_serial = 0;
1310 * Creates a new #GtkIconSet. A #GtkIconSet represents a single icon
1311 * in various sizes and widget states. It can provide a #GdkPixbuf
1312 * for a given size and state on request, and automatically caches
1313 * some of the rendered #GdkPixbuf objects.
1315 * Normally you would use gtk_widget_render_icon() instead of
1316 * using #GtkIconSet directly. The one case where you'd use
1317 * #GtkIconSet is to create application-specific icon sets to place in
1318 * a #GtkIconFactory.
1320 * Return value: a new #GtkIconSet
1323 gtk_icon_set_new (void)
1325 GtkIconSet *icon_set;
1327 icon_set = g_new (GtkIconSet, 1);
1329 icon_set->ref_count = 1;
1330 icon_set->sources = NULL;
1331 icon_set->cache = NULL;
1332 icon_set->cache_size = 0;
1333 icon_set->cache_serial = cache_serial;
1339 * gtk_icon_set_new_from_pixbuf:
1340 * @pixbuf: a #GdkPixbuf
1342 * Creates a new #GtkIconSet with @pixbuf as the default/fallback
1343 * source image. If you don't add any additional #GtkIconSource to the
1344 * icon set, all variants of the icon will be created from @pixbuf,
1345 * using scaling, pixelation, etc. as required to adjust the icon size
1346 * or make the icon look insensitive/prelighted.
1348 * Return value: a new #GtkIconSet
1351 gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
1355 GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1357 g_return_val_if_fail (pixbuf != NULL, NULL);
1359 set = gtk_icon_set_new ();
1361 gtk_icon_source_set_pixbuf (&source, pixbuf);
1362 gtk_icon_set_add_source (set, &source);
1363 gtk_icon_source_set_pixbuf (&source, NULL);
1371 * @icon_set: a #GtkIconSet.
1373 * Increments the reference count on @icon_set.
1375 * Return value: @icon_set.
1378 gtk_icon_set_ref (GtkIconSet *icon_set)
1380 g_return_val_if_fail (icon_set != NULL, NULL);
1381 g_return_val_if_fail (icon_set->ref_count > 0, NULL);
1383 icon_set->ref_count += 1;
1389 * gtk_icon_set_unref:
1390 * @icon_set: a #GtkIconSet
1392 * Decrements the reference count on @icon_set, and frees memory
1393 * if the reference count reaches 0.
1396 gtk_icon_set_unref (GtkIconSet *icon_set)
1398 g_return_if_fail (icon_set != NULL);
1399 g_return_if_fail (icon_set->ref_count > 0);
1401 icon_set->ref_count -= 1;
1403 if (icon_set->ref_count == 0)
1405 GSList *tmp_list = icon_set->sources;
1406 while (tmp_list != NULL)
1408 gtk_icon_source_free (tmp_list->data);
1410 tmp_list = g_slist_next (tmp_list);
1412 g_slist_free (icon_set->sources);
1414 clear_cache (icon_set, TRUE);
1421 gtk_icon_set_get_type (void)
1423 static GType our_type = 0;
1426 our_type = g_boxed_type_register_static ("GtkIconSet",
1427 (GBoxedCopyFunc) gtk_icon_set_ref,
1428 (GBoxedFreeFunc) gtk_icon_set_unref);
1434 * gtk_icon_set_copy:
1435 * @icon_set: a #GtkIconSet
1437 * Copies @icon_set by value.
1439 * Return value: a new #GtkIconSet identical to the first.
1442 gtk_icon_set_copy (GtkIconSet *icon_set)
1447 copy = gtk_icon_set_new ();
1449 tmp_list = icon_set->sources;
1450 while (tmp_list != NULL)
1452 copy->sources = g_slist_prepend (copy->sources,
1453 gtk_icon_source_copy (tmp_list->data));
1455 tmp_list = g_slist_next (tmp_list);
1458 copy->sources = g_slist_reverse (copy->sources);
1460 copy->cache = copy_cache (icon_set, copy);
1461 copy->cache_size = icon_set->cache_size;
1462 copy->cache_serial = icon_set->cache_serial;
1468 sizes_equivalent (GtkIconSize lhs,
1471 /* We used to consider sizes equivalent if they were
1472 * the same pixel size, but we don't have the GtkSettings
1473 * here, so we can't do that. Plus, it's not clear that
1474 * it is right... it was just a workaround for the fact
1475 * that we register icons by logical size, not pixel size.
1481 gint r_w, r_h, l_w, l_h;
1483 icon_size_lookup_intern (NULL, rhs, &r_w, &r_h);
1484 icon_size_lookup_intern (NULL, lhs, &l_w, &l_h);
1486 return r_w == l_w && r_h == l_h;
1490 static GtkIconSource *
1491 find_best_matching_source (GtkIconSet *icon_set,
1492 GtkTextDirection direction,
1497 GtkIconSource *source;
1500 /* We need to find the best icon source. Direction matters more
1501 * than state, state matters more than size. icon_set->sources
1502 * is sorted according to wildness, so if we take the first
1503 * match we find it will be the least-wild match (if there are
1504 * multiple matches for a given "wildness" then the RC file contained
1505 * dumb stuff, and we end up with an arbitrary matching source)
1509 tmp_list = icon_set->sources;
1510 while (tmp_list != NULL)
1512 GtkIconSource *s = tmp_list->data;
1514 if ((s->any_direction || (s->direction == direction)) &&
1515 (s->any_state || (s->state == state)) &&
1516 (s->any_size || (sizes_equivalent (size, s->size))))
1518 if (!g_slist_find (failed, s))
1525 tmp_list = g_slist_next (tmp_list);
1532 ensure_filename_pixbuf (GtkIconSet *icon_set,
1533 GtkIconSource *source)
1535 if (source->filename_pixbuf == NULL)
1537 GError *error = NULL;
1539 source->filename_pixbuf = gdk_pixbuf_new_from_file (source->source.filename, &error);
1541 if (source->filename_pixbuf == NULL)
1543 /* Remove this icon source so we don't keep trying to
1546 g_warning (_("Error loading icon: %s"), error->message);
1547 g_error_free (error);
1549 icon_set->sources = g_slist_remove (icon_set->sources, source);
1551 gtk_icon_source_free (source);
1561 render_icon_name_pixbuf (GtkIconSource *icon_source,
1563 GtkTextDirection direction,
1570 GdkPixbuf *tmp_pixbuf;
1571 GtkIconSource tmp_source;
1573 GtkIconTheme *icon_theme;
1574 GtkSettings *settings;
1575 gint width, height, pixel_size;
1576 GError *error = NULL;
1578 if (widget && gtk_widget_has_screen (widget))
1579 screen = gtk_widget_get_screen (widget);
1580 else if (style->colormap)
1581 screen = gdk_colormap_get_screen (style->colormap);
1584 screen = gdk_screen_get_default ();
1585 GTK_NOTE (MULTIHEAD,
1586 g_warning ("Using the default screen for gtk_icon_source_render_icon()"));
1589 icon_theme = gtk_icon_theme_get_for_screen (screen);
1590 settings = gtk_settings_get_for_screen (screen);
1592 if (!gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
1594 g_warning ("Invalid icon size %d\n", size);
1595 width = height = 24;
1598 pixel_size = MIN (width, height);
1600 tmp_pixbuf = gtk_icon_theme_load_icon (icon_theme,
1601 icon_source->source.icon_name,
1607 g_warning ("Error loading theme icon for stock: %s", error->message);
1611 tmp_source = *icon_source;
1612 tmp_source.type = GTK_ICON_SOURCE_PIXBUF;
1613 tmp_source.source.pixbuf = tmp_pixbuf;
1615 pixbuf = gtk_style_render_icon (style, &tmp_source,
1616 direction, state, -1,
1620 g_warning ("Failed to render icon");
1622 g_object_unref (tmp_pixbuf);
1628 find_and_render_icon_source (GtkIconSet *icon_set,
1630 GtkTextDirection direction,
1636 GSList *failed = NULL;
1637 GdkPixbuf *pixbuf = NULL;
1639 /* We treat failure in two different ways:
1641 * A) If loading a source that specifies a filename fails,
1642 * we treat that as permanent, and remove the source
1643 * from the GtkIconSet. (in ensure_filename_pixbuf ()
1644 * B) If loading a themed icon fails, or scaling an icon
1645 * fails, we treat that as transient and will try
1646 * again next time the icon falls out of the cache
1647 * and we need to recreate it.
1649 while (pixbuf == NULL)
1651 GtkIconSource *source = find_best_matching_source (icon_set, direction, state, size, failed);
1656 switch (source->type)
1658 case GTK_ICON_SOURCE_FILENAME:
1659 if (!ensure_filename_pixbuf (icon_set, source))
1662 case GTK_ICON_SOURCE_PIXBUF:
1663 pixbuf = gtk_style_render_icon (style, source,
1664 direction, state, size,
1668 g_warning ("Failed to render icon");
1669 failed = g_slist_prepend (failed, source);
1672 case GTK_ICON_SOURCE_ICON_NAME:
1673 pixbuf = render_icon_name_pixbuf (source, style,
1674 direction, state, size,
1677 failed = g_slist_prepend (failed, source);
1679 case GTK_ICON_SOURCE_EMPTY:
1680 g_assert_not_reached ();
1684 g_slist_free (failed);
1690 render_fallback_image (GtkStyle *style,
1691 GtkTextDirection direction,
1697 /* This icon can be used for any direction/state/size */
1698 static GtkIconSource fallback_source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1700 if (fallback_source.type == GTK_ICON_SOURCE_EMPTY)
1702 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_inline (-1, stock_missing_image_24, FALSE, NULL);
1703 gtk_icon_source_set_pixbuf (&fallback_source, pixbuf);
1704 g_object_unref (pixbuf);
1707 return gtk_style_render_icon (style,
1717 * gtk_icon_set_render_icon:
1718 * @icon_set: a #GtkIconSet
1719 * @style: a #GtkStyle associated with @widget, or %NULL
1720 * @direction: text direction
1721 * @state: widget state
1723 * @widget: widget that will display the icon, or %NULL.
1724 * The only use that is typically made of this
1725 * is to determine the appropriate #GdkScreen.
1726 * @detail: detail to pass to the theme engine, or %NULL.
1727 * Note that passing a detail of anything but %NULL
1728 * will disable caching.
1730 * Renders an icon using gtk_style_render_icon(). In most cases,
1731 * gtk_widget_render_icon() is better, since it automatically provides
1732 * most of the arguments from the current widget settings. This
1733 * function never returns %NULL; if the icon can't be rendered
1734 * (perhaps because an image file fails to load), a default "missing
1735 * image" icon will be returned instead.
1737 * Return value: a #GdkPixbuf to be displayed
1740 gtk_icon_set_render_icon (GtkIconSet *icon_set,
1742 GtkTextDirection direction,
1750 g_return_val_if_fail (icon_set != NULL, NULL);
1751 g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
1753 if (icon_set->sources == NULL)
1754 return render_fallback_image (style, direction, state, size, widget, detail);
1758 icon = find_in_cache (icon_set, style, direction,
1763 g_object_ref (icon);
1769 icon = find_and_render_icon_source (icon_set, style, direction, state, size,
1773 icon = render_fallback_image (style, direction, state, size, widget, detail);
1776 add_to_cache (icon_set, style, direction, state, size, icon);
1781 /* Order sources by their "wildness", so that "wilder" sources are
1782 * greater than "specific" sources; for determining ordering,
1783 * direction beats state beats size.
1787 icon_source_compare (gconstpointer ap, gconstpointer bp)
1789 const GtkIconSource *a = ap;
1790 const GtkIconSource *b = bp;
1792 if (!a->any_direction && b->any_direction)
1794 else if (a->any_direction && !b->any_direction)
1796 else if (!a->any_state && b->any_state)
1798 else if (a->any_state && !b->any_state)
1800 else if (!a->any_size && b->any_size)
1802 else if (a->any_size && !b->any_size)
1809 * gtk_icon_set_add_source:
1810 * @icon_set: a #GtkIconSet
1811 * @source: a #GtkIconSource
1813 * Icon sets have a list of #GtkIconSource, which they use as base
1814 * icons for rendering icons in different states and sizes. Icons are
1815 * scaled, made to look insensitive, etc. in
1816 * gtk_icon_set_render_icon(), but #GtkIconSet needs base images to
1817 * work with. The base images and when to use them are described by
1820 * This function copies @source, so you can reuse the same source immediately
1821 * without affecting the icon set.
1823 * An example of when you'd use this function: a web browser's "Back
1824 * to Previous Page" icon might point in a different direction in
1825 * Hebrew and in English; it might look different when insensitive;
1826 * and it might change size depending on toolbar mode (small/large
1827 * icons). So a single icon set would contain all those variants of
1828 * the icon, and you might add a separate source for each one.
1830 * You should nearly always add a "default" icon source with all
1831 * fields wildcarded, which will be used as a fallback if no more
1832 * specific source matches. #GtkIconSet always prefers more specific
1833 * icon sources to more generic icon sources. The order in which you
1834 * add the sources to the icon set does not matter.
1836 * gtk_icon_set_new_from_pixbuf() creates a new icon set with a
1837 * default icon source based on the given pixbuf.
1841 gtk_icon_set_add_source (GtkIconSet *icon_set,
1842 const GtkIconSource *source)
1844 g_return_if_fail (icon_set != NULL);
1845 g_return_if_fail (source != NULL);
1847 if (source->type == GTK_ICON_SOURCE_EMPTY)
1849 g_warning ("Useless empty GtkIconSource");
1853 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
1854 gtk_icon_source_copy (source),
1855 icon_source_compare);
1859 * gtk_icon_set_get_sizes:
1860 * @icon_set: a #GtkIconSet
1861 * @sizes: return location for array of sizes
1862 * @n_sizes: location to store number of elements in returned array
1864 * Obtains a list of icon sizes this icon set can render. The returned
1865 * array must be freed with g_free().
1869 gtk_icon_set_get_sizes (GtkIconSet *icon_set,
1870 GtkIconSize **sizes,
1874 gboolean all_sizes = FALSE;
1875 GSList *specifics = NULL;
1877 g_return_if_fail (icon_set != NULL);
1878 g_return_if_fail (sizes != NULL);
1879 g_return_if_fail (n_sizes != NULL);
1881 tmp_list = icon_set->sources;
1882 while (tmp_list != NULL)
1884 GtkIconSource *source;
1886 source = tmp_list->data;
1888 if (source->any_size)
1894 specifics = g_slist_prepend (specifics, GINT_TO_POINTER (source->size));
1896 tmp_list = g_slist_next (tmp_list);
1901 /* Need to find out what sizes exist */
1906 *sizes = g_new (GtkIconSize, icon_sizes_used);
1907 *n_sizes = icon_sizes_used - 1;
1910 while (i < icon_sizes_used)
1912 (*sizes)[i - 1] = icon_sizes[i].size;
1920 *n_sizes = g_slist_length (specifics);
1921 *sizes = g_new (GtkIconSize, *n_sizes);
1924 tmp_list = specifics;
1925 while (tmp_list != NULL)
1927 (*sizes)[i] = GPOINTER_TO_INT (tmp_list->data);
1930 tmp_list = g_slist_next (tmp_list);
1934 g_slist_free (specifics);
1939 * gtk_icon_source_new:
1941 * Creates a new #GtkIconSource. A #GtkIconSource contains a #GdkPixbuf (or
1942 * image filename) that serves as the base image for one or more of the
1943 * icons in a #GtkIconSet, along with a specification for which icons in the
1944 * icon set will be based on that pixbuf or image file. An icon set contains
1945 * a set of icons that represent "the same" logical concept in different states,
1946 * different global text directions, and different sizes.
1948 * So for example a web browser's "Back to Previous Page" icon might
1949 * point in a different direction in Hebrew and in English; it might
1950 * look different when insensitive; and it might change size depending
1951 * on toolbar mode (small/large icons). So a single icon set would
1952 * contain all those variants of the icon. #GtkIconSet contains a list
1953 * of #GtkIconSource from which it can derive specific icon variants in
1956 * In the simplest case, #GtkIconSet contains one source pixbuf from
1957 * which it derives all variants. The convenience function
1958 * gtk_icon_set_new_from_pixbuf() handles this case; if you only have
1959 * one source pixbuf, just use that function.
1961 * If you want to use a different base pixbuf for different icon
1962 * variants, you create multiple icon sources, mark which variants
1963 * they'll be used to create, and add them to the icon set with
1964 * gtk_icon_set_add_source().
1966 * By default, the icon source has all parameters wildcarded. That is,
1967 * the icon source will be used as the base icon for any desired text
1968 * direction, widget state, or icon size.
1970 * Return value: a new #GtkIconSource
1973 gtk_icon_source_new (void)
1977 src = g_new0 (GtkIconSource, 1);
1979 src->direction = GTK_TEXT_DIR_NONE;
1980 src->size = GTK_ICON_SIZE_INVALID;
1981 src->state = GTK_STATE_NORMAL;
1983 src->any_direction = TRUE;
1984 src->any_state = TRUE;
1985 src->any_size = TRUE;
1991 * gtk_icon_source_copy:
1992 * @source: a #GtkIconSource
1994 * Creates a copy of @source; mostly useful for language bindings.
1996 * Return value: a new #GtkIconSource
1999 gtk_icon_source_copy (const GtkIconSource *source)
2001 GtkIconSource *copy;
2003 g_return_val_if_fail (source != NULL, NULL);
2005 copy = g_new (GtkIconSource, 1);
2011 case GTK_ICON_SOURCE_EMPTY:
2013 case GTK_ICON_SOURCE_ICON_NAME:
2014 copy->source.icon_name = g_strdup (copy->source.icon_name);
2016 case GTK_ICON_SOURCE_FILENAME:
2017 copy->source.filename = g_strdup (copy->source.filename);
2018 if (copy->filename_pixbuf)
2019 g_object_ref (copy->filename_pixbuf);
2021 case GTK_ICON_SOURCE_PIXBUF:
2022 g_object_ref (copy->source.pixbuf);
2025 g_assert_not_reached();
2032 * gtk_icon_source_free:
2033 * @source: a #GtkIconSource
2035 * Frees a dynamically-allocated icon source, along with its
2036 * filename, size, and pixbuf fields if those are not %NULL.
2039 gtk_icon_source_free (GtkIconSource *source)
2041 g_return_if_fail (source != NULL);
2043 icon_source_clear (source);
2048 gtk_icon_source_get_type (void)
2050 static GType our_type = 0;
2053 our_type = g_boxed_type_register_static ("GtkIconSource",
2054 (GBoxedCopyFunc) gtk_icon_source_copy,
2055 (GBoxedFreeFunc) gtk_icon_source_free);
2061 icon_source_clear (GtkIconSource *source)
2063 switch (source->type)
2065 case GTK_ICON_SOURCE_EMPTY:
2067 case GTK_ICON_SOURCE_ICON_NAME:
2068 g_free (source->source.icon_name);
2069 source->source.icon_name = NULL;
2071 case GTK_ICON_SOURCE_FILENAME:
2072 g_free (source->source.filename);
2073 source->source.filename = NULL;
2074 g_free (source->filename_pixbuf);
2075 source->filename_pixbuf = NULL;
2077 case GTK_ICON_SOURCE_PIXBUF:
2078 g_object_unref (source->source.pixbuf);
2079 source->source.pixbuf = NULL;
2082 g_assert_not_reached();
2085 source->type = GTK_ICON_SOURCE_EMPTY;
2089 * gtk_icon_source_set_filename:
2090 * @source: a #GtkIconSource
2091 * @filename: image file to use
2093 * Sets the name of an image file to use as a base image when creating
2094 * icon variants for #GtkIconSet. The filename must be absolute.
2097 gtk_icon_source_set_filename (GtkIconSource *source,
2098 const gchar *filename)
2100 g_return_if_fail (source != NULL);
2101 g_return_if_fail (filename == NULL || g_path_is_absolute (filename));
2103 if (source->type == GTK_ICON_SOURCE_FILENAME &&
2104 source->source.filename == filename)
2107 icon_source_clear (source);
2109 if (filename != NULL)
2111 source->type = GTK_ICON_SOURCE_FILENAME;
2112 source->source.filename = g_strdup (filename);
2117 * gtk_icon_source_set_icon_name
2118 * @source: a #GtkIconSource
2119 * @icon_name: name of icon to use
2121 * Sets the name of an icon to look up in the current icon theme
2122 * to use as a base image when creating icon variants for #GtkIconSet.
2125 gtk_icon_source_set_icon_name (GtkIconSource *source,
2126 const gchar *icon_name)
2128 g_return_if_fail (source != NULL);
2130 if (source->type == GTK_ICON_SOURCE_ICON_NAME &&
2131 source->source.icon_name == icon_name)
2134 icon_source_clear (source);
2136 if (icon_name != NULL)
2138 source->type = GTK_ICON_SOURCE_ICON_NAME;
2139 source->source.icon_name = g_strdup (icon_name);
2144 * gtk_icon_source_set_pixbuf:
2145 * @source: a #GtkIconSource
2146 * @pixbuf: pixbuf to use as a source
2148 * Sets a pixbuf to use as a base image when creating icon variants
2152 gtk_icon_source_set_pixbuf (GtkIconSource *source,
2155 g_return_if_fail (source != NULL);
2156 g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
2158 if (source->type == GTK_ICON_SOURCE_PIXBUF &&
2159 source->source.pixbuf == pixbuf)
2162 icon_source_clear (source);
2166 source->type = GTK_ICON_SOURCE_PIXBUF;
2167 source->source.pixbuf = g_object_ref (pixbuf);
2172 * gtk_icon_source_get_filename:
2173 * @source: a #GtkIconSource
2175 * Retrieves the source filename, or %NULL if none is set. The
2176 * filename is not a copy, and should not be modified or expected to
2177 * persist beyond the lifetime of the icon source.
2179 * Return value: image filename. This string must not be modified
2182 G_CONST_RETURN gchar*
2183 gtk_icon_source_get_filename (const GtkIconSource *source)
2185 g_return_val_if_fail (source != NULL, NULL);
2187 if (source->type == GTK_ICON_SOURCE_FILENAME)
2188 return source->source.filename;
2194 * gtk_icon_source_get_icon_name:
2195 * @source: a #GtkIconSource
2197 * Retrieves the source icon name, or %NULL if none is set. The
2198 * icon_name is not a copy, and should not be modified or expected to
2199 * persist beyond the lifetime of the icon source.
2201 * Return value: icon name. This string must not be modified or freed.
2203 G_CONST_RETURN gchar*
2204 gtk_icon_source_get_icon_name (const GtkIconSource *source)
2206 g_return_val_if_fail (source != NULL, NULL);
2208 if (source->type == GTK_ICON_SOURCE_ICON_NAME)
2209 return source->source.icon_name;
2215 * gtk_icon_source_get_pixbuf:
2216 * @source: a #GtkIconSource
2218 * Retrieves the source pixbuf, or %NULL if none is set.
2219 * In addition, if a filename source is in use, this
2220 * function in some cases will return the pixbuf from
2221 * loaded from the filename. This is, for example, true
2222 * for the GtkIconSource passed to the GtkStyle::render_icon()
2223 * virtual function. The reference count on the pixbuf is
2226 * Return value: source pixbuf
2229 gtk_icon_source_get_pixbuf (const GtkIconSource *source)
2231 g_return_val_if_fail (source != NULL, NULL);
2233 if (source->type == GTK_ICON_SOURCE_PIXBUF)
2234 return source->source.pixbuf;
2235 else if (source->type == GTK_ICON_SOURCE_FILENAME)
2236 return source->filename_pixbuf;
2242 * gtk_icon_source_set_direction_wildcarded:
2243 * @source: a #GtkIconSource
2244 * @setting: %TRUE to wildcard the text direction
2246 * If the text direction is wildcarded, this source can be used
2247 * as the base image for an icon in any #GtkTextDirection.
2248 * If the text direction is not wildcarded, then the
2249 * text direction the icon source applies to should be set
2250 * with gtk_icon_source_set_direction(), and the icon source
2251 * will only be used with that text direction.
2253 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2254 * wildcarded sources, and will use an exact match when possible.
2258 gtk_icon_source_set_direction_wildcarded (GtkIconSource *source,
2261 g_return_if_fail (source != NULL);
2263 source->any_direction = setting != FALSE;
2267 * gtk_icon_source_set_state_wildcarded:
2268 * @source: a #GtkIconSource
2269 * @setting: %TRUE to wildcard the widget state
2271 * If the widget state is wildcarded, this source can be used as the
2272 * base image for an icon in any #GtkStateType. If the widget state
2273 * is not wildcarded, then the state the source applies to should be
2274 * set with gtk_icon_source_set_state() and the icon source will
2275 * only be used with that specific state.
2277 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2278 * wildcarded sources, and will use an exact match when possible.
2280 * #GtkIconSet will normally transform wildcarded source images to
2281 * produce an appropriate icon for a given state, for example
2282 * lightening an image on prelight, but will not modify source images
2283 * that match exactly.
2286 gtk_icon_source_set_state_wildcarded (GtkIconSource *source,
2289 g_return_if_fail (source != NULL);
2291 source->any_state = setting != FALSE;
2296 * gtk_icon_source_set_size_wildcarded:
2297 * @source: a #GtkIconSource
2298 * @setting: %TRUE to wildcard the widget state
2300 * If the icon size is wildcarded, this source can be used as the base
2301 * image for an icon of any size. If the size is not wildcarded, then
2302 * the size the source applies to should be set with
2303 * gtk_icon_source_set_size() and the icon source will only be used
2304 * with that specific size.
2306 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2307 * wildcarded sources, and will use an exact match when possible.
2309 * #GtkIconSet will normally scale wildcarded source images to produce
2310 * an appropriate icon at a given size, but will not change the size
2311 * of source images that match exactly.
2314 gtk_icon_source_set_size_wildcarded (GtkIconSource *source,
2317 g_return_if_fail (source != NULL);
2319 source->any_size = setting != FALSE;
2323 * gtk_icon_source_get_size_wildcarded:
2324 * @source: a #GtkIconSource
2326 * Gets the value set by gtk_icon_source_set_size_wildcarded().
2328 * Return value: %TRUE if this icon source is a base for any icon size variant
2331 gtk_icon_source_get_size_wildcarded (const GtkIconSource *source)
2333 g_return_val_if_fail (source != NULL, TRUE);
2335 return source->any_size;
2339 * gtk_icon_source_get_state_wildcarded:
2340 * @source: a #GtkIconSource
2342 * Gets the value set by gtk_icon_source_set_state_wildcarded().
2344 * Return value: %TRUE if this icon source is a base for any widget state variant
2347 gtk_icon_source_get_state_wildcarded (const GtkIconSource *source)
2349 g_return_val_if_fail (source != NULL, TRUE);
2351 return source->any_state;
2355 * gtk_icon_source_get_direction_wildcarded:
2356 * @source: a #GtkIconSource
2358 * Gets the value set by gtk_icon_source_set_direction_wildcarded().
2360 * Return value: %TRUE if this icon source is a base for any text direction variant
2363 gtk_icon_source_get_direction_wildcarded (const GtkIconSource *source)
2365 g_return_val_if_fail (source != NULL, TRUE);
2367 return source->any_direction;
2371 * gtk_icon_source_set_direction:
2372 * @source: a #GtkIconSource
2373 * @direction: text direction this source applies to
2375 * Sets the text direction this icon source is intended to be used
2378 * Setting the text direction on an icon source makes no difference
2379 * if the text direction is wildcarded. Therefore, you should usually
2380 * call gtk_icon_source_set_direction_wildcarded() to un-wildcard it
2381 * in addition to calling this function.
2385 gtk_icon_source_set_direction (GtkIconSource *source,
2386 GtkTextDirection direction)
2388 g_return_if_fail (source != NULL);
2390 source->direction = direction;
2394 * gtk_icon_source_set_state:
2395 * @source: a #GtkIconSource
2396 * @state: widget state this source applies to
2398 * Sets the widget state this icon source is intended to be used
2401 * Setting the widget state on an icon source makes no difference
2402 * if the state is wildcarded. Therefore, you should usually
2403 * call gtk_icon_source_set_state_wildcarded() to un-wildcard it
2404 * in addition to calling this function.
2408 gtk_icon_source_set_state (GtkIconSource *source,
2411 g_return_if_fail (source != NULL);
2413 source->state = state;
2417 * gtk_icon_source_set_size:
2418 * @source: a #GtkIconSource
2419 * @size: icon size this source applies to
2421 * Sets the icon size this icon source is intended to be used
2424 * Setting the icon size on an icon source makes no difference
2425 * if the size is wildcarded. Therefore, you should usually
2426 * call gtk_icon_source_set_size_wildcarded() to un-wildcard it
2427 * in addition to calling this function.
2431 gtk_icon_source_set_size (GtkIconSource *source,
2434 g_return_if_fail (source != NULL);
2436 source->size = size;
2440 * gtk_icon_source_get_direction:
2441 * @source: a #GtkIconSource
2443 * Obtains the text direction this icon source applies to. The return
2444 * value is only useful/meaningful if the text direction is <emphasis>not</emphasis>
2447 * Return value: text direction this source matches
2450 gtk_icon_source_get_direction (const GtkIconSource *source)
2452 g_return_val_if_fail (source != NULL, 0);
2454 return source->direction;
2458 * gtk_icon_source_get_state:
2459 * @source: a #GtkIconSource
2461 * Obtains the widget state this icon source applies to. The return
2462 * value is only useful/meaningful if the widget state is <emphasis>not</emphasis>
2465 * Return value: widget state this source matches
2468 gtk_icon_source_get_state (const GtkIconSource *source)
2470 g_return_val_if_fail (source != NULL, 0);
2472 return source->state;
2476 * gtk_icon_source_get_size:
2477 * @source: a #GtkIconSource
2479 * Obtains the icon size this source applies to. The return value
2480 * is only useful/meaningful if the icon size is <emphasis>not</emphasis> wildcarded.
2482 * Return value: icon size this source matches.
2485 gtk_icon_source_get_size (const GtkIconSource *source)
2487 g_return_val_if_fail (source != NULL, 0);
2489 return source->size;
2492 #define NUM_CACHED_ICONS 8
2494 typedef struct _CachedIcon CachedIcon;
2498 /* These must all match to use the cached pixbuf.
2499 * If any don't match, we must re-render the pixbuf.
2502 GtkTextDirection direction;
2510 ensure_cache_up_to_date (GtkIconSet *icon_set)
2512 if (icon_set->cache_serial != cache_serial)
2514 clear_cache (icon_set, TRUE);
2515 icon_set->cache_serial = cache_serial;
2520 cached_icon_free (CachedIcon *icon)
2522 g_object_unref (icon->pixbuf);
2528 find_in_cache (GtkIconSet *icon_set,
2530 GtkTextDirection direction,
2537 ensure_cache_up_to_date (icon_set);
2540 tmp_list = icon_set->cache;
2541 while (tmp_list != NULL)
2543 CachedIcon *icon = tmp_list->data;
2545 if (icon->style == style &&
2546 icon->direction == direction &&
2547 icon->state == state &&
2552 /* Move this icon to the front of the list. */
2553 prev->next = tmp_list->next;
2554 tmp_list->next = icon_set->cache;
2555 icon_set->cache = tmp_list;
2558 return icon->pixbuf;
2562 tmp_list = g_slist_next (tmp_list);
2569 add_to_cache (GtkIconSet *icon_set,
2571 GtkTextDirection direction,
2578 ensure_cache_up_to_date (icon_set);
2580 g_object_ref (pixbuf);
2582 /* We have to ref the style, since if the style was finalized
2583 * its address could be reused by another style, creating a
2588 g_object_ref (style);
2591 icon = g_new (CachedIcon, 1);
2592 icon_set->cache = g_slist_prepend (icon_set->cache, icon);
2594 icon->style = style;
2595 icon->direction = direction;
2596 icon->state = state;
2598 icon->pixbuf = pixbuf;
2601 attach_to_style (icon_set, icon->style);
2603 if (icon_set->cache_size >= NUM_CACHED_ICONS)
2605 /* Remove oldest item in the cache */
2609 tmp_list = icon_set->cache;
2611 /* Find next-to-last link */
2612 g_assert (NUM_CACHED_ICONS > 2);
2613 while (tmp_list->next->next)
2614 tmp_list = tmp_list->next;
2616 g_assert (tmp_list != NULL);
2617 g_assert (tmp_list->next != NULL);
2618 g_assert (tmp_list->next->next == NULL);
2620 /* Free the last icon */
2621 icon = tmp_list->next->data;
2623 g_slist_free (tmp_list->next);
2624 tmp_list->next = NULL;
2626 cached_icon_free (icon);
2631 clear_cache (GtkIconSet *icon_set,
2632 gboolean style_detach)
2635 GtkStyle *last_style = NULL;
2637 tmp_list = icon_set->cache;
2638 while (tmp_list != NULL)
2640 CachedIcon *icon = tmp_list->data;
2644 /* simple optimization for the case where the cache
2645 * contains contiguous icons from the same style.
2646 * it's safe to call detach_from_style more than
2647 * once on the same style though.
2649 if (last_style != icon->style)
2651 detach_from_style (icon_set, icon->style);
2652 last_style = icon->style;
2656 cached_icon_free (icon);
2658 tmp_list = g_slist_next (tmp_list);
2661 g_slist_free (icon_set->cache);
2662 icon_set->cache = NULL;
2663 icon_set->cache_size = 0;
2667 copy_cache (GtkIconSet *icon_set,
2668 GtkIconSet *copy_recipient)
2671 GSList *copy = NULL;
2673 ensure_cache_up_to_date (icon_set);
2675 tmp_list = icon_set->cache;
2676 while (tmp_list != NULL)
2678 CachedIcon *icon = tmp_list->data;
2679 CachedIcon *icon_copy = g_new (CachedIcon, 1);
2683 if (icon_copy->style)
2684 attach_to_style (copy_recipient, icon_copy->style);
2686 g_object_ref (icon_copy->pixbuf);
2688 icon_copy->size = icon->size;
2690 copy = g_slist_prepend (copy, icon_copy);
2692 tmp_list = g_slist_next (tmp_list);
2695 return g_slist_reverse (copy);
2699 attach_to_style (GtkIconSet *icon_set,
2704 table = g_object_get_qdata (G_OBJECT (style),
2705 g_quark_try_string ("gtk-style-icon-sets"));
2709 table = g_hash_table_new (NULL, NULL);
2710 g_object_set_qdata_full (G_OBJECT (style),
2711 g_quark_from_static_string ("gtk-style-icon-sets"),
2716 g_hash_table_insert (table, icon_set, icon_set);
2720 detach_from_style (GtkIconSet *icon_set,
2725 table = g_object_get_qdata (G_OBJECT (style),
2726 g_quark_try_string ("gtk-style-icon-sets"));
2729 g_hash_table_remove (table, icon_set);
2733 iconsets_foreach (gpointer key,
2737 GtkIconSet *icon_set = key;
2739 /* We only need to remove cache entries for the given style;
2740 * but that complicates things because in destroy notify
2741 * we don't know which style got destroyed, and 95% of the
2742 * time all cache entries will have the same style,
2743 * so this is faster anyway.
2746 clear_cache (icon_set, FALSE);
2750 style_dnotify (gpointer data)
2752 GHashTable *table = data;
2754 g_hash_table_foreach (table, iconsets_foreach, NULL);
2756 g_hash_table_destroy (table);
2759 /* This allows the icon set to detect that its cache is out of date. */
2761 _gtk_icon_set_invalidate_caches (void)
2767 listify_foreach (gpointer key, gpointer value, gpointer data)
2769 GSList **list = data;
2771 *list = g_slist_prepend (*list, key);
2775 g_hash_table_get_keys (GHashTable *table)
2777 GSList *list = NULL;
2779 g_hash_table_foreach (table, listify_foreach, &list);
2785 * _gtk_icon_factory_list_ids:
2787 * Gets all known IDs stored in an existing icon factory.
2788 * The strings in the returned list aren't copied.
2789 * The list itself should be freed.
2791 * Return value: List of ids in icon factories
2794 _gtk_icon_factory_list_ids (void)
2801 ensure_default_icons ();
2803 tmp_list = all_icon_factories;
2804 while (tmp_list != NULL)
2808 GtkIconFactory *factory = GTK_ICON_FACTORY (tmp_list->data);
2810 these_ids = g_hash_table_get_keys (factory->icons);
2812 ids = g_slist_concat (ids, these_ids);
2814 tmp_list = g_slist_next (tmp_list);