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 "gtksettings.h"
38 static GSList *all_icon_factories = NULL;
42 /* Either filename or pixbuf can be NULL. If both are non-NULL,
43 * the pixbuf is assumed to be the already-loaded contents of the
49 GtkTextDirection direction;
53 /* If TRUE, then the parameter is wildcarded, and the above
54 * fields should be ignored. If FALSE, the parameter is
55 * specified, and the above fields should be valid.
57 guint any_direction : 1;
62 static gpointer parent_class = NULL;
64 static void gtk_icon_factory_init (GtkIconFactory *icon_factory);
65 static void gtk_icon_factory_class_init (GtkIconFactoryClass *klass);
66 static void gtk_icon_factory_finalize (GObject *object);
67 static void get_default_icons (GtkIconFactory *icon_factory);
69 static GtkIconSize icon_size_register_intern (const gchar *name,
74 gtk_icon_factory_get_type (void)
76 static GType icon_factory_type = 0;
78 if (!icon_factory_type)
80 static const GTypeInfo icon_factory_info =
82 sizeof (GtkIconFactoryClass),
84 NULL, /* base_finalize */
85 (GClassInitFunc) gtk_icon_factory_class_init,
86 NULL, /* class_finalize */
87 NULL, /* class_data */
88 sizeof (GtkIconFactory),
90 (GInstanceInitFunc) gtk_icon_factory_init,
94 g_type_register_static (G_TYPE_OBJECT, "GtkIconFactory",
95 &icon_factory_info, 0);
98 return icon_factory_type;
102 gtk_icon_factory_init (GtkIconFactory *factory)
104 factory->icons = g_hash_table_new (g_str_hash, g_str_equal);
105 all_icon_factories = g_slist_prepend (all_icon_factories, factory);
109 gtk_icon_factory_class_init (GtkIconFactoryClass *klass)
111 GObjectClass *object_class = G_OBJECT_CLASS (klass);
113 parent_class = g_type_class_peek_parent (klass);
115 object_class->finalize = gtk_icon_factory_finalize;
119 free_icon_set (gpointer key, gpointer value, gpointer data)
122 gtk_icon_set_unref (value);
126 gtk_icon_factory_finalize (GObject *object)
128 GtkIconFactory *factory = GTK_ICON_FACTORY (object);
130 all_icon_factories = g_slist_remove (all_icon_factories, factory);
132 g_hash_table_foreach (factory->icons, free_icon_set, NULL);
134 g_hash_table_destroy (factory->icons);
136 G_OBJECT_CLASS (parent_class)->finalize (object);
140 * gtk_icon_factory_new:
142 * Creates a new #GtkIconFactory. An icon factory manages a collection
143 * of #GtkIconSet<!-- -->s; a #GtkIconSet manages a set of variants of a
144 * particular icon (i.e. a #GtkIconSet contains variants for different
145 * sizes and widget states). Icons in an icon factory are named by a
146 * stock ID, which is a simple string identifying the icon. Each
147 * #GtkStyle has a list of #GtkIconFactory<!-- -->s derived from the current
148 * theme; those icon factories are consulted first when searching for
149 * an icon. If the theme doesn't set a particular icon, GTK+ looks for
150 * the icon in a list of default icon factories, maintained by
151 * gtk_icon_factory_add_default() and
152 * gtk_icon_factory_remove_default(). Applications with icons should
153 * add a default icon factory with their icons, which will allow
154 * themes to override the icons for the application.
156 * Return value: a new #GtkIconFactory
159 gtk_icon_factory_new (void)
161 return g_object_new (GTK_TYPE_ICON_FACTORY, NULL);
165 * gtk_icon_factory_add:
166 * @factory: a #GtkIconFactory
167 * @stock_id: icon name
168 * @icon_set: icon set
170 * Adds the given @icon_set to the icon factory, under the name
171 * @stock_id. @stock_id should be namespaced for your application,
172 * e.g. "myapp-whatever-icon". Normally applications create a
173 * #GtkIconFactory, then add it to the list of default factories with
174 * gtk_icon_factory_add_default(). Then they pass the @stock_id to
175 * widgets such as #GtkImage to display the icon. Themes can provide
176 * an icon with the same name (such as "myapp-whatever-icon") to
177 * override your application's default icons. If an icon already
178 * existed in @factory for @stock_id, it is unreferenced and replaced
179 * with the new @icon_set.
183 gtk_icon_factory_add (GtkIconFactory *factory,
184 const gchar *stock_id,
185 GtkIconSet *icon_set)
187 gpointer old_key = NULL;
188 gpointer old_value = NULL;
190 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
191 g_return_if_fail (stock_id != NULL);
192 g_return_if_fail (icon_set != NULL);
194 g_hash_table_lookup_extended (factory->icons, stock_id,
195 &old_key, &old_value);
197 if (old_value == icon_set)
200 gtk_icon_set_ref (icon_set);
202 /* GHashTable key memory management is so fantastically broken. */
204 g_hash_table_insert (factory->icons, old_key, icon_set);
206 g_hash_table_insert (factory->icons, g_strdup (stock_id), icon_set);
209 gtk_icon_set_unref (old_value);
213 * gtk_icon_factory_lookup:
214 * @factory: a #GtkIconFactory
215 * @stock_id: an icon name
217 * Looks up @stock_id in the icon factory, returning an icon set
218 * if found, otherwise %NULL. For display to the user, you should
219 * use gtk_style_lookup_icon_set() on the #GtkStyle for the
220 * widget that will display the icon, instead of using this
221 * function directly, so that themes are taken into account.
223 * Return value: icon set of @stock_id.
226 gtk_icon_factory_lookup (GtkIconFactory *factory,
227 const gchar *stock_id)
229 g_return_val_if_fail (GTK_IS_ICON_FACTORY (factory), NULL);
230 g_return_val_if_fail (stock_id != NULL, NULL);
232 return g_hash_table_lookup (factory->icons, stock_id);
235 static GtkIconFactory *gtk_default_icons = NULL;
236 static GSList *default_factories = NULL;
239 * gtk_icon_factory_add_default:
240 * @factory: a #GtkIconFactory
242 * Adds an icon factory to the list of icon factories searched by
243 * gtk_style_lookup_icon_set(). This means that, for example,
244 * gtk_image_new_from_stock() will be able to find icons in @factory.
245 * There will normally be an icon factory added for each library or
246 * application that comes with icons. The default icon factories
247 * can be overridden by themes.
251 gtk_icon_factory_add_default (GtkIconFactory *factory)
253 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
255 g_object_ref (factory);
257 default_factories = g_slist_prepend (default_factories, factory);
261 * gtk_icon_factory_remove_default:
262 * @factory: a #GtkIconFactory previously added with gtk_icon_factory_add_default()
264 * Removes an icon factory from the list of default icon
265 * factories. Not normally used; you might use it for a library that
266 * can be unloaded or shut down.
270 gtk_icon_factory_remove_default (GtkIconFactory *factory)
272 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
274 default_factories = g_slist_remove (default_factories, factory);
276 g_object_unref (factory);
280 ensure_default_icons (void)
282 if (gtk_default_icons == NULL)
284 gtk_default_icons = gtk_icon_factory_new ();
286 get_default_icons (gtk_default_icons);
291 * gtk_icon_factory_lookup_default:
292 * @stock_id: an icon name
294 * Looks for an icon in the list of default icon factories. For
295 * display to the user, you should use gtk_style_lookup_icon_set() on
296 * the #GtkStyle for the widget that will display the icon, instead of
297 * using this function directly, so that themes are taken into
301 * Return value: a #GtkIconSet, or %NULL
304 gtk_icon_factory_lookup_default (const gchar *stock_id)
308 g_return_val_if_fail (stock_id != NULL, NULL);
310 tmp_list = default_factories;
311 while (tmp_list != NULL)
313 GtkIconSet *icon_set =
314 gtk_icon_factory_lookup (GTK_ICON_FACTORY (tmp_list->data),
320 tmp_list = g_slist_next (tmp_list);
323 ensure_default_icons ();
325 return gtk_icon_factory_lookup (gtk_default_icons, stock_id);
329 add_source (GtkIconSet *set,
330 GtkIconSource *source,
331 const gchar *inline_data)
333 source->pixbuf = gdk_pixbuf_new_from_inline (-1, inline_data, FALSE, NULL);
334 g_assert (source->pixbuf);
336 gtk_icon_set_add_source (set, source);
338 g_object_unref (source->pixbuf);
343 sized_icon_set_from_inline (const guchar *inline_data,
348 GtkIconSource source = { NULL, NULL, 0, 0, 0,
353 set = gtk_icon_set_new ();
355 add_source (set, source, inline_data);
362 sized_with_fallback_icon_set_from_inline (const guchar *fallback_data_ltr,
363 const guchar *fallback_data_rtl,
364 const guchar *inline_data_ltr,
365 const guchar *inline_data_rtl,
370 GtkIconSource source = { NULL, NULL, 0, 0, 0,
373 set = gtk_icon_set_new ();
376 source.any_direction = inline_data_rtl == NULL;
378 source.direction = GTK_TEXT_DIR_LTR;
379 add_source (set, &source, inline_data_ltr);
381 if (inline_data_rtl != NULL)
383 source.direction = GTK_TEXT_DIR_RTL;
384 add_source (set, &source, inline_data_rtl);
387 source.any_size = TRUE;
388 source.any_direction = fallback_data_rtl == NULL;
390 source.direction = GTK_TEXT_DIR_LTR;
391 add_source (set, &source, fallback_data_ltr);
393 if (fallback_data_rtl != NULL)
395 source.direction = GTK_TEXT_DIR_RTL;
396 add_source (set, &source, fallback_data_rtl);
403 unsized_icon_set_from_inline (const guchar *inline_data)
407 /* This icon can be used for any direction/state/size */
408 GtkIconSource source = { NULL, NULL, 0, 0, 0,
411 set = gtk_icon_set_new ();
413 add_source (set, &source, inline_data);
420 add_sized (GtkIconFactory *factory,
421 const guchar *inline_data,
423 const gchar *stock_id)
427 set = sized_icon_set_from_inline (inline_data, size);
429 gtk_icon_factory_add (factory, stock_id, set);
431 gtk_icon_set_unref (set);
436 add_sized_with_fallback_and_rtl (GtkIconFactory *factory,
437 const guchar *fallback_data_ltr,
438 const guchar *fallback_data_rtl,
439 const guchar *inline_data_ltr,
440 const guchar *inline_data_rtl,
442 const gchar *stock_id)
446 set = sized_with_fallback_icon_set_from_inline (fallback_data_ltr, fallback_data_rtl,
447 inline_data_ltr, inline_data_rtl,
450 gtk_icon_factory_add (factory, stock_id, set);
452 gtk_icon_set_unref (set);
456 add_sized_with_fallback (GtkIconFactory *factory,
457 const guchar *fallback_data,
458 const guchar *inline_data,
460 const gchar *stock_id)
462 add_sized_with_fallback_and_rtl (factory,
469 add_unsized (GtkIconFactory *factory,
470 const guchar *inline_data,
471 const gchar *stock_id)
475 set = unsized_icon_set_from_inline (inline_data);
477 gtk_icon_factory_add (factory, stock_id, set);
479 gtk_icon_set_unref (set);
483 get_default_icons (GtkIconFactory *factory)
485 /* KEEP IN SYNC with gtkstock.c */
487 /* We add all stock icons unsized, since it's confusing if icons only
488 * can be loaded at certain sizes.
491 /* Have dialog size */
492 add_unsized (factory, stock_dialog_error_48, GTK_STOCK_DIALOG_ERROR);
493 add_unsized (factory, stock_dialog_info_48, GTK_STOCK_DIALOG_INFO);
494 add_unsized (factory, stock_dialog_question_48, GTK_STOCK_DIALOG_QUESTION);
495 add_unsized (factory, stock_dialog_warning_48,GTK_STOCK_DIALOG_WARNING);
498 add_unsized (factory, stock_dnd_32, GTK_STOCK_DND);
499 add_unsized (factory, stock_dnd_multiple_32, GTK_STOCK_DND_MULTIPLE);
501 /* Have button sizes */
502 add_unsized (factory, stock_apply_20, GTK_STOCK_APPLY);
503 add_unsized (factory, stock_cancel_20, GTK_STOCK_CANCEL);
504 add_unsized (factory, stock_no_20, GTK_STOCK_NO);
505 add_unsized (factory, stock_ok_20, GTK_STOCK_OK);
506 add_unsized (factory, stock_yes_20, GTK_STOCK_YES);
508 /* Generic + button sizes */
509 add_sized_with_fallback (factory,
512 GTK_ICON_SIZE_BUTTON,
515 /* Generic + menu sizes */
516 add_sized_with_fallback (factory,
522 add_sized_with_fallback (factory,
523 stock_align_center_24,
524 stock_align_center_16,
526 GTK_STOCK_JUSTIFY_CENTER);
528 add_sized_with_fallback (factory,
529 stock_align_justify_24,
530 stock_align_justify_16,
532 GTK_STOCK_JUSTIFY_FILL);
534 add_sized_with_fallback (factory,
538 GTK_STOCK_JUSTIFY_LEFT);
540 add_sized_with_fallback (factory,
541 stock_align_right_24,
542 stock_align_right_16,
544 GTK_STOCK_JUSTIFY_RIGHT);
546 add_sized_with_fallback (factory,
550 GTK_STOCK_GOTO_BOTTOM);
552 add_sized_with_fallback (factory,
558 add_sized_with_fallback (factory,
564 add_sized_with_fallback (factory,
570 add_sized_with_fallback (factory,
576 add_sized_with_fallback (factory,
582 add_sized_with_fallback (factory,
588 add_sized_with_fallback (factory,
594 add_sized_with_fallback_and_rtl (factory,
600 GTK_STOCK_GOTO_FIRST);
602 add_sized_with_fallback (factory,
606 GTK_STOCK_SELECT_FONT);
608 add_sized_with_fallback (factory,
614 add_sized_with_fallback (factory,
620 add_sized_with_fallback_and_rtl (factory,
622 stock_jump_to_rtl_24,
624 stock_jump_to_rtl_16,
628 add_sized_with_fallback_and_rtl (factory,
634 GTK_STOCK_GOTO_LAST);
636 add_sized_with_fallback_and_rtl (factory,
638 stock_right_arrow_24,
640 stock_right_arrow_16,
644 add_sized_with_fallback (factory,
645 stock_missing_image_24,
646 stock_missing_image_16,
648 GTK_STOCK_MISSING_IMAGE);
650 add_sized_with_fallback (factory,
656 add_sized_with_fallback (factory,
662 add_sized_with_fallback (factory,
668 add_sized_with_fallback (factory,
669 stock_preferences_24,
670 stock_preferences_16,
672 GTK_STOCK_PREFERENCES);
674 add_sized_with_fallback (factory,
680 add_sized_with_fallback (factory,
681 stock_print_preview_24,
682 stock_print_preview_16,
684 GTK_STOCK_PRINT_PREVIEW);
686 add_sized_with_fallback (factory,
690 GTK_STOCK_PROPERTIES);
692 add_sized_with_fallback (factory,
698 add_sized_with_fallback (factory,
704 add_sized_with_fallback (factory,
710 add_sized_with_fallback (factory,
714 GTK_STOCK_REVERT_TO_SAVED);
716 add_sized_with_fallback_and_rtl (factory,
717 stock_right_arrow_24,
719 stock_right_arrow_16,
722 GTK_STOCK_GO_FORWARD);
724 add_sized_with_fallback (factory,
730 add_sized_with_fallback (factory,
736 add_sized_with_fallback (factory,
742 add_sized_with_fallback (factory,
748 add_sized_with_fallback (factory,
749 stock_search_replace_24,
750 stock_search_replace_16,
752 GTK_STOCK_FIND_AND_REPLACE);
754 add_sized_with_fallback (factory,
755 stock_sort_descending_24,
756 stock_sort_descending_16,
758 GTK_STOCK_SORT_DESCENDING);
760 add_sized_with_fallback (factory,
761 stock_sort_ascending_24,
762 stock_sort_ascending_16,
764 GTK_STOCK_SORT_ASCENDING);
766 add_sized_with_fallback (factory,
770 GTK_STOCK_SPELL_CHECK);
772 add_sized_with_fallback (factory,
778 add_sized_with_fallback (factory,
784 add_sized_with_fallback (factory,
785 stock_text_italic_24,
786 stock_text_italic_16,
790 add_sized_with_fallback (factory,
791 stock_text_strikethrough_24,
792 stock_text_strikethrough_16,
794 GTK_STOCK_STRIKETHROUGH);
796 add_sized_with_fallback (factory,
797 stock_text_underline_24,
798 stock_text_underline_16,
800 GTK_STOCK_UNDERLINE);
802 add_sized_with_fallback (factory,
808 add_sized_with_fallback (factory,
814 add_sized_with_fallback (factory,
820 add_sized_with_fallback (factory,
826 add_sized_with_fallback (factory,
832 /* Generic size only */
834 add_unsized (factory, stock_clear_24, GTK_STOCK_CLEAR);
835 add_unsized (factory, stock_colorselector_24, GTK_STOCK_SELECT_COLOR);
836 add_unsized (factory, stock_color_picker_25, GTK_STOCK_COLOR_PICKER);
837 add_unsized (factory, stock_index_24, GTK_STOCK_INDEX);
838 add_unsized (factory, stock_zoom_1_24, GTK_STOCK_ZOOM_100);
839 add_unsized (factory, stock_zoom_fit_24, GTK_STOCK_ZOOM_FIT);
840 add_unsized (factory, stock_zoom_in_24, GTK_STOCK_ZOOM_IN);
841 add_unsized (factory, stock_zoom_out_24, GTK_STOCK_ZOOM_OUT);
844 /************************************************************
845 * Icon size handling *
846 ************************************************************/
848 typedef struct _IconSize IconSize;
859 typedef struct _IconAlias IconAlias;
867 typedef struct _SettingsIconSize SettingsIconSize;
869 struct _SettingsIconSize
875 static GHashTable *icon_aliases = NULL;
876 static IconSize *icon_sizes = NULL;
877 static gint icon_sizes_allocated = 0;
878 static gint icon_sizes_used = 0;
881 init_icon_sizes (void)
883 if (icon_sizes == NULL)
885 #define NUM_BUILTIN_SIZES 7
888 icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
890 icon_sizes = g_new (IconSize, NUM_BUILTIN_SIZES);
891 icon_sizes_allocated = NUM_BUILTIN_SIZES;
892 icon_sizes_used = NUM_BUILTIN_SIZES;
894 icon_sizes[GTK_ICON_SIZE_INVALID].size = 0;
895 icon_sizes[GTK_ICON_SIZE_INVALID].name = NULL;
896 icon_sizes[GTK_ICON_SIZE_INVALID].width = 0;
897 icon_sizes[GTK_ICON_SIZE_INVALID].height = 0;
899 /* the name strings aren't copied since we don't ever remove
900 * icon sizes, so we don't need to know whether they're static.
901 * Even if we did I suppose removing the builtin sizes would be
905 icon_sizes[GTK_ICON_SIZE_MENU].size = GTK_ICON_SIZE_MENU;
906 icon_sizes[GTK_ICON_SIZE_MENU].name = "gtk-menu";
907 icon_sizes[GTK_ICON_SIZE_MENU].width = 16;
908 icon_sizes[GTK_ICON_SIZE_MENU].height = 16;
910 icon_sizes[GTK_ICON_SIZE_BUTTON].size = GTK_ICON_SIZE_BUTTON;
911 icon_sizes[GTK_ICON_SIZE_BUTTON].name = "gtk-button";
912 icon_sizes[GTK_ICON_SIZE_BUTTON].width = 20;
913 icon_sizes[GTK_ICON_SIZE_BUTTON].height = 20;
915 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].size = GTK_ICON_SIZE_SMALL_TOOLBAR;
916 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].name = "gtk-small-toolbar";
917 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].width = 18;
918 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].height = 18;
920 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].size = GTK_ICON_SIZE_LARGE_TOOLBAR;
921 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].name = "gtk-large-toolbar";
922 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].width = 24;
923 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].height = 24;
925 icon_sizes[GTK_ICON_SIZE_DND].size = GTK_ICON_SIZE_DND;
926 icon_sizes[GTK_ICON_SIZE_DND].name = "gtk-dnd";
927 icon_sizes[GTK_ICON_SIZE_DND].width = 32;
928 icon_sizes[GTK_ICON_SIZE_DND].height = 32;
930 icon_sizes[GTK_ICON_SIZE_DIALOG].size = GTK_ICON_SIZE_DIALOG;
931 icon_sizes[GTK_ICON_SIZE_DIALOG].name = "gtk-dialog";
932 icon_sizes[GTK_ICON_SIZE_DIALOG].width = 48;
933 icon_sizes[GTK_ICON_SIZE_DIALOG].height = 48;
935 g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
937 /* Alias everything to itself. */
938 i = 1; /* skip invalid size */
939 while (i < NUM_BUILTIN_SIZES)
941 gtk_icon_size_register_alias (icon_sizes[i].name, icon_sizes[i].size);
946 #undef NUM_BUILTIN_SIZES
951 free_settings_sizes (gpointer data)
953 g_array_free (data, TRUE);
957 get_settings_sizes (GtkSettings *settings,
960 GArray *settings_sizes;
961 static GQuark sizes_quark = 0;
964 sizes_quark = g_quark_from_static_string ("gtk-icon-sizes");
966 settings_sizes = g_object_get_qdata (G_OBJECT (settings), sizes_quark);
969 settings_sizes = g_array_new (FALSE, FALSE, sizeof (SettingsIconSize));
970 g_object_set_qdata_full (G_OBJECT (settings), sizes_quark,
971 settings_sizes, free_settings_sizes);
976 return settings_sizes;
980 icon_size_set_for_settings (GtkSettings *settings,
981 const gchar *size_name,
986 GArray *settings_sizes;
987 SettingsIconSize *settings_size;
989 g_return_if_fail (size_name != NULL);
991 size = gtk_icon_size_from_name (size_name);
992 if (size == GTK_ICON_SIZE_INVALID)
993 /* Reserve a place */
994 size = icon_size_register_intern (size_name, -1, -1);
996 settings_sizes = get_settings_sizes (settings, NULL);
997 if (size >= settings_sizes->len)
999 SettingsIconSize unset = { -1, -1 };
1002 for (i = settings_sizes->len; i <= size; i++)
1003 g_array_append_val (settings_sizes, unset);
1006 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
1008 settings_size->width = width;
1009 settings_size->height = height;
1012 /* Like pango_parse_word, but accept - as well
1015 scan_icon_size_name (const char **pos, GString *out)
1017 const char *p = *pos;
1019 while (g_ascii_isspace (*p))
1022 if (!((*p >= 'A' && *p <= 'Z') ||
1023 (*p >= 'a' && *p <= 'z') ||
1024 *p == '_' || *p == '-'))
1027 g_string_truncate (out, 0);
1028 g_string_append_c (out, *p);
1031 while ((*p >= 'A' && *p <= 'Z') ||
1032 (*p >= 'a' && *p <= 'z') ||
1033 (*p >= '0' && *p <= '9') ||
1034 *p == '_' || *p == '-')
1036 g_string_append_c (out, *p);
1046 icon_size_setting_parse (GtkSettings *settings,
1047 const gchar *icon_size_string)
1049 GString *name_buf = g_string_new (NULL);
1050 const gchar *p = icon_size_string;
1052 while (pango_skip_space (&p))
1056 if (!scan_icon_size_name (&p, name_buf))
1059 if (!pango_skip_space (&p))
1067 if (!pango_scan_int (&p, &width))
1070 if (!pango_skip_space (&p))
1078 if (!pango_scan_int (&p, &height))
1081 if (width > 0 && height > 0)
1083 icon_size_set_for_settings (settings, name_buf->str,
1088 g_warning ("Invalid size in gtk-icon-sizes: %d,%d\n", width, height);
1091 pango_skip_space (&p);
1100 g_string_free (name_buf, TRUE);
1104 g_warning ("Error parsing gtk-icon-sizes string:\n\t'%s'", icon_size_string);
1105 g_string_free (name_buf, TRUE);
1109 icon_size_set_all_from_settings (GtkSettings *settings)
1111 GArray *settings_sizes;
1112 gchar *icon_size_string;
1114 /* Reset old settings */
1115 settings_sizes = get_settings_sizes (settings, NULL);
1116 g_array_set_size (settings_sizes, 0);
1118 g_object_get (settings,
1119 "gtk-icon-sizes", &icon_size_string,
1122 if (icon_size_string)
1124 icon_size_setting_parse (settings, icon_size_string);
1125 g_free (icon_size_string);
1130 icon_size_settings_changed (GtkSettings *settings,
1133 icon_size_set_all_from_settings (settings);
1135 _gtk_rc_reset_styles (settings);
1139 icon_sizes_init_for_settings (GtkSettings *settings)
1141 g_signal_connect (settings,
1142 "notify::gtk-icon-sizes",
1143 G_CALLBACK (icon_size_settings_changed),
1146 icon_size_set_all_from_settings (settings);
1150 icon_size_lookup_intern (GtkSettings *settings,
1155 GArray *settings_sizes;
1156 gint width_for_settings = -1;
1157 gint height_for_settings = -1;
1161 if (size >= icon_sizes_used || icon_sizes[size].width < 0)
1164 if (size == GTK_ICON_SIZE_INVALID)
1169 gboolean initial = FALSE;
1171 settings_sizes = get_settings_sizes (settings, &initial);
1173 icon_sizes_init_for_settings (settings);
1175 if (size < settings_sizes->len)
1177 SettingsIconSize *settings_size;
1179 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
1181 width_for_settings = settings_size->width;
1182 height_for_settings = settings_size->height;
1187 *widthp = width_for_settings >= 0 ? width_for_settings : icon_sizes[size].width;
1190 *heightp = height_for_settings >= 0 ? height_for_settings : icon_sizes[size].height;
1196 * gtk_icon_size_lookup_for_settings:
1197 * @settings: a #GtkSettings object, used to determine
1198 * which set of user preferences to used.
1199 * @size: an icon size
1200 * @width: location to store icon width
1201 * @height: location to store icon height
1203 * Obtains the pixel size of a semantic icon size, possibly
1204 * modified by user preferences for a particular
1205 * #GtkSettings. Normally @size would be
1206 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
1207 * isn't normally needed, gtk_widget_render_icon() is the usual
1208 * way to get an icon for rendering, then just look at the size of
1209 * the rendered pixbuf. The rendered pixbuf may not even correspond to
1210 * the width/height returned by gtk_icon_size_lookup(), because themes
1211 * are free to render the pixbuf however they like, including changing
1214 * Return value: %TRUE if @size was a valid size
1219 gtk_icon_size_lookup_for_settings (GtkSettings *settings,
1224 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1226 return icon_size_lookup_intern (settings, size, width, height);
1230 * gtk_icon_size_lookup:
1231 * @size: an icon size
1232 * @width: location to store icon width
1233 * @height: location to store icon height
1235 * Obtains the pixel size of a semantic icon size, possibly
1236 * modified by user preferences for the default #GtkSettings.
1237 * (See gtk_icon_size_lookup_for_settings().)
1238 * Normally @size would be
1239 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
1240 * isn't normally needed, gtk_widget_render_icon() is the usual
1241 * way to get an icon for rendering, then just look at the size of
1242 * the rendered pixbuf. The rendered pixbuf may not even correspond to
1243 * the width/height returned by gtk_icon_size_lookup(), because themes
1244 * are free to render the pixbuf however they like, including changing
1247 * Return value: %TRUE if @size was a valid size
1250 gtk_icon_size_lookup (GtkIconSize size,
1254 GTK_NOTE (MULTIHEAD,
1255 g_warning ("gtk_icon_size_lookup ()) is not multihead safe"));
1257 return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (),
1258 size, widthp, heightp);
1262 icon_size_register_intern (const gchar *name,
1266 IconAlias *old_alias;
1271 old_alias = g_hash_table_lookup (icon_aliases, name);
1272 if (old_alias && icon_sizes[old_alias->target].width > 0)
1274 g_warning ("Icon size name '%s' already exists", name);
1275 return GTK_ICON_SIZE_INVALID;
1280 size = old_alias->target;
1284 if (icon_sizes_used == icon_sizes_allocated)
1286 icon_sizes_allocated *= 2;
1287 icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated);
1290 size = icon_sizes_used++;
1292 /* alias to self. */
1293 gtk_icon_size_register_alias (name, size);
1295 icon_sizes[size].size = size;
1296 icon_sizes[size].name = g_strdup (name);
1299 icon_sizes[size].width = width;
1300 icon_sizes[size].height = height;
1306 * gtk_icon_size_register:
1307 * @name: name of the icon size
1308 * @width: the icon width
1309 * @height: the icon height
1311 * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
1312 * etc. Returns the integer value for the size.
1314 * Returns: integer value representing the size
1318 gtk_icon_size_register (const gchar *name,
1322 g_return_val_if_fail (name != NULL, 0);
1323 g_return_val_if_fail (width > 0, 0);
1324 g_return_val_if_fail (height > 0, 0);
1326 return icon_size_register_intern (name, width, height);
1330 * gtk_icon_size_register_alias:
1331 * @alias: an alias for @target
1332 * @target: an existing icon size
1334 * Registers @alias as another name for @target.
1335 * So calling gtk_icon_size_from_name() with @alias as argument
1336 * will return @target.
1340 gtk_icon_size_register_alias (const gchar *alias,
1345 g_return_if_fail (alias != NULL);
1349 if (!icon_size_lookup_intern (NULL, target, NULL, NULL))
1350 g_warning ("gtk_icon_size_register_alias: Icon size %d does not exist", target);
1352 ia = g_hash_table_lookup (icon_aliases, alias);
1355 if (icon_sizes[ia->target].width > 0)
1357 g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias);
1361 ia->target = target;
1366 ia = g_new (IconAlias, 1);
1367 ia->name = g_strdup (alias);
1368 ia->target = target;
1370 g_hash_table_insert (icon_aliases, ia->name, ia);
1375 * gtk_icon_size_from_name:
1376 * @name: the name to look up.
1377 * @returns: the icon size with the given name.
1379 * Looks up the icon size associated with @name.
1382 gtk_icon_size_from_name (const gchar *name)
1388 ia = g_hash_table_lookup (icon_aliases, name);
1390 if (ia && icon_sizes[ia->target].width > 0)
1393 return GTK_ICON_SIZE_INVALID;
1397 * gtk_icon_size_get_name:
1398 * @size: a #GtkIconSize.
1399 * @returns: the name of the given icon size.
1401 * Gets the canonical name of the given icon size. The returned string
1402 * is statically allocated and should not be freed.
1404 G_CONST_RETURN gchar*
1405 gtk_icon_size_get_name (GtkIconSize size)
1407 if (size >= icon_sizes_used)
1410 return icon_sizes[size].name;
1413 /************************************************************/
1418 /* Clear icon set contents, drop references to all contained
1419 * GdkPixbuf objects and forget all GtkIconSources. Used to
1420 * recycle an icon set.
1422 static GdkPixbuf *find_in_cache (GtkIconSet *icon_set,
1424 GtkTextDirection direction,
1427 static void add_to_cache (GtkIconSet *icon_set,
1429 GtkTextDirection direction,
1433 static void clear_cache (GtkIconSet *icon_set,
1434 gboolean style_detach);
1435 static GSList* copy_cache (GtkIconSet *icon_set,
1436 GtkIconSet *copy_recipient);
1437 static void attach_to_style (GtkIconSet *icon_set,
1439 static void detach_from_style (GtkIconSet *icon_set,
1441 static void style_dnotify (gpointer data);
1449 /* Cache of the last few rendered versions of the icon. */
1457 static guint cache_serial = 0;
1462 * Creates a new #GtkIconSet. A #GtkIconSet represents a single icon
1463 * in various sizes and widget states. It can provide a #GdkPixbuf
1464 * for a given size and state on request, and automatically caches
1465 * some of the rendered #GdkPixbuf objects.
1467 * Normally you would use gtk_widget_render_icon() instead of
1468 * using #GtkIconSet directly. The one case where you'd use
1469 * #GtkIconSet is to create application-specific icon sets to place in
1470 * a #GtkIconFactory.
1472 * Return value: a new #GtkIconSet
1475 gtk_icon_set_new (void)
1477 GtkIconSet *icon_set;
1479 icon_set = g_new (GtkIconSet, 1);
1481 icon_set->ref_count = 1;
1482 icon_set->sources = NULL;
1483 icon_set->cache = NULL;
1484 icon_set->cache_size = 0;
1485 icon_set->cache_serial = cache_serial;
1491 * gtk_icon_set_new_from_pixbuf:
1492 * @pixbuf: a #GdkPixbuf
1494 * Creates a new #GtkIconSet with @pixbuf as the default/fallback
1495 * source image. If you don't add any additional #GtkIconSource to the
1496 * icon set, all variants of the icon will be created from @pixbuf,
1497 * using scaling, pixelation, etc. as required to adjust the icon size
1498 * or make the icon look insensitive/prelighted.
1500 * Return value: a new #GtkIconSet
1503 gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
1507 GtkIconSource source = { NULL, NULL, 0, 0, 0,
1510 g_return_val_if_fail (pixbuf != NULL, NULL);
1512 set = gtk_icon_set_new ();
1514 source.pixbuf = pixbuf;
1516 gtk_icon_set_add_source (set, &source);
1524 * @icon_set: a #GtkIconSet.
1526 * Increments the reference count on @icon_set.
1528 * Return value: @icon_set.
1531 gtk_icon_set_ref (GtkIconSet *icon_set)
1533 g_return_val_if_fail (icon_set != NULL, NULL);
1534 g_return_val_if_fail (icon_set->ref_count > 0, NULL);
1536 icon_set->ref_count += 1;
1542 * gtk_icon_set_unref:
1543 * @icon_set: a #GtkIconSet
1545 * Decrements the reference count on @icon_set, and frees memory
1546 * if the reference count reaches 0.
1549 gtk_icon_set_unref (GtkIconSet *icon_set)
1551 g_return_if_fail (icon_set != NULL);
1552 g_return_if_fail (icon_set->ref_count > 0);
1554 icon_set->ref_count -= 1;
1556 if (icon_set->ref_count == 0)
1558 GSList *tmp_list = icon_set->sources;
1559 while (tmp_list != NULL)
1561 gtk_icon_source_free (tmp_list->data);
1563 tmp_list = g_slist_next (tmp_list);
1566 clear_cache (icon_set, TRUE);
1573 gtk_icon_set_get_type (void)
1575 static GType our_type = 0;
1578 our_type = g_boxed_type_register_static ("GtkIconSet",
1579 (GBoxedCopyFunc) gtk_icon_set_ref,
1580 (GBoxedFreeFunc) gtk_icon_set_unref);
1586 * gtk_icon_set_copy:
1587 * @icon_set: a #GtkIconSet
1589 * Copies @icon_set by value.
1591 * Return value: a new #GtkIconSet identical to the first.
1594 gtk_icon_set_copy (GtkIconSet *icon_set)
1599 copy = gtk_icon_set_new ();
1601 tmp_list = icon_set->sources;
1602 while (tmp_list != NULL)
1604 copy->sources = g_slist_prepend (copy->sources,
1605 gtk_icon_source_copy (tmp_list->data));
1607 tmp_list = g_slist_next (tmp_list);
1610 copy->sources = g_slist_reverse (copy->sources);
1612 copy->cache = copy_cache (icon_set, copy);
1613 copy->cache_size = icon_set->cache_size;
1614 copy->cache_serial = icon_set->cache_serial;
1620 sizes_equivalent (GtkIconSize lhs,
1623 /* We used to consider sizes equivalent if they were
1624 * the same pixel size, but we don't have the GtkSettings
1625 * here, so we can't do that. Plus, it's not clear that
1626 * it is right... it was just a workaround for the fact
1627 * that we register icons by logical size, not pixel size.
1633 gint r_w, r_h, l_w, l_h;
1635 icon_size_lookup_intern (NULL, rhs, &r_w, &r_h);
1636 icon_size_lookup_intern (NULL, lhs, &l_w, &l_h);
1638 return r_w == l_w && r_h == l_h;
1642 static GtkIconSource*
1643 find_and_prep_icon_source (GtkIconSet *icon_set,
1644 GtkTextDirection direction,
1648 GtkIconSource *source;
1652 /* We need to find the best icon source. Direction matters more
1653 * than state, state matters more than size. icon_set->sources
1654 * is sorted according to wildness, so if we take the first
1655 * match we find it will be the least-wild match (if there are
1656 * multiple matches for a given "wildness" then the RC file contained
1657 * dumb stuff, and we end up with an arbitrary matching source)
1661 tmp_list = icon_set->sources;
1662 while (tmp_list != NULL)
1664 GtkIconSource *s = tmp_list->data;
1666 if ((s->any_direction || (s->direction == direction)) &&
1667 (s->any_state || (s->state == state)) &&
1668 (s->any_size || (sizes_equivalent (size, s->size))))
1674 tmp_list = g_slist_next (tmp_list);
1680 if (source->pixbuf == NULL)
1682 GError *error = NULL;
1684 g_assert (source->filename);
1685 source->pixbuf = gdk_pixbuf_new_from_file (source->filename, &error);
1687 if (source->pixbuf == NULL)
1689 /* Remove this icon source so we don't keep trying to
1692 g_warning (_("Error loading icon: %s"), error->message);
1693 g_error_free (error);
1695 icon_set->sources = g_slist_remove (icon_set->sources, source);
1697 gtk_icon_source_free (source);
1699 /* Try to fall back to other sources */
1700 if (icon_set->sources != NULL)
1701 return find_and_prep_icon_source (icon_set,
1714 render_fallback_image (GtkStyle *style,
1715 GtkTextDirection direction,
1721 /* This icon can be used for any direction/state/size */
1722 static GtkIconSource fallback_source = { NULL, NULL, 0, 0, 0, TRUE, TRUE, TRUE };
1724 if (fallback_source.pixbuf == NULL)
1725 fallback_source.pixbuf = gdk_pixbuf_new_from_inline (-1, stock_missing_image_24, FALSE, NULL);
1727 return gtk_style_render_icon (style,
1737 * gtk_icon_set_render_icon:
1738 * @icon_set: a #GtkIconSet
1739 * @style: a #GtkStyle associated with @widget, or %NULL
1740 * @direction: text direction
1741 * @state: widget state
1743 * @widget: widget that will display the icon, or %NULL
1744 * @detail: detail to pass to the theme engine, or %NULL
1746 * Renders an icon using gtk_style_render_icon(). In most cases,
1747 * gtk_widget_render_icon() is better, since it automatically provides
1748 * most of the arguments from the current widget settings. This
1749 * function never returns %NULL; if the icon can't be rendered
1750 * (perhaps because an image file fails to load), a default "missing
1751 * image" icon will be returned instead.
1753 * Return value: a #GdkPixbuf to be displayed
1756 gtk_icon_set_render_icon (GtkIconSet *icon_set,
1758 GtkTextDirection direction,
1765 GtkIconSource *source;
1767 g_return_val_if_fail (icon_set != NULL, NULL);
1768 g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
1770 if (icon_set->sources == NULL)
1771 return render_fallback_image (style, direction, state, size, widget, detail);
1773 icon = find_in_cache (icon_set, style, direction,
1778 g_object_ref (icon);
1783 source = find_and_prep_icon_source (icon_set, direction, state, size);
1786 return render_fallback_image (style, direction, state, size, widget, detail);
1788 g_assert (source->pixbuf != NULL);
1790 icon = gtk_style_render_icon (style,
1800 g_warning ("Theme engine failed to render icon");
1804 add_to_cache (icon_set, style, direction, state, size, icon);
1809 /* Order sources by their "wildness", so that "wilder" sources are
1810 * greater than "specific" sources; for determining ordering,
1811 * direction beats state beats size.
1815 icon_source_compare (gconstpointer ap, gconstpointer bp)
1817 const GtkIconSource *a = ap;
1818 const GtkIconSource *b = bp;
1820 if (!a->any_direction && b->any_direction)
1822 else if (a->any_direction && !b->any_direction)
1824 else if (!a->any_state && b->any_state)
1826 else if (a->any_state && !b->any_state)
1828 else if (!a->any_size && b->any_size)
1830 else if (a->any_size && !b->any_size)
1837 * gtk_icon_set_add_source:
1838 * @icon_set: a #GtkIconSet
1839 * @source: a #GtkIconSource
1841 * Icon sets have a list of #GtkIconSource, which they use as base
1842 * icons for rendering icons in different states and sizes. Icons are
1843 * scaled, made to look insensitive, etc. in
1844 * gtk_icon_set_render_icon(), but #GtkIconSet needs base images to
1845 * work with. The base images and when to use them are described by
1848 * This function copies @source, so you can reuse the same source immediately
1849 * without affecting the icon set.
1851 * An example of when you'd use this function: a web browser's "Back
1852 * to Previous Page" icon might point in a different direction in
1853 * Hebrew and in English; it might look different when insensitive;
1854 * and it might change size depending on toolbar mode (small/large
1855 * icons). So a single icon set would contain all those variants of
1856 * the icon, and you might add a separate source for each one.
1858 * You should nearly always add a "default" icon source with all
1859 * fields wildcarded, which will be used as a fallback if no more
1860 * specific source matches. #GtkIconSet always prefers more specific
1861 * icon sources to more generic icon sources. The order in which you
1862 * add the sources to the icon set does not matter.
1864 * gtk_icon_set_new_from_pixbuf() creates a new icon set with a
1865 * default icon source based on the given pixbuf.
1869 gtk_icon_set_add_source (GtkIconSet *icon_set,
1870 const GtkIconSource *source)
1872 g_return_if_fail (icon_set != NULL);
1873 g_return_if_fail (source != NULL);
1875 if (source->pixbuf == NULL &&
1876 source->filename == NULL)
1878 g_warning ("Useless GtkIconSource contains NULL filename and pixbuf");
1882 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
1883 gtk_icon_source_copy (source),
1884 icon_source_compare);
1888 * gtk_icon_set_get_sizes:
1889 * @icon_set: a #GtkIconSet
1890 * @sizes: return location for array of sizes
1891 * @n_sizes: location to store number of elements in returned array
1893 * Obtains a list of icon sizes this icon set can render. The returned
1894 * array must be freed with g_free().
1898 gtk_icon_set_get_sizes (GtkIconSet *icon_set,
1899 GtkIconSize **sizes,
1903 gboolean all_sizes = FALSE;
1904 GSList *specifics = NULL;
1906 g_return_if_fail (icon_set != NULL);
1907 g_return_if_fail (sizes != NULL);
1908 g_return_if_fail (n_sizes != NULL);
1910 tmp_list = icon_set->sources;
1911 while (tmp_list != NULL)
1913 GtkIconSource *source;
1915 source = tmp_list->data;
1917 if (source->any_size)
1923 specifics = g_slist_prepend (specifics, GINT_TO_POINTER (source->size));
1925 tmp_list = g_slist_next (tmp_list);
1930 /* Need to find out what sizes exist */
1935 *sizes = g_new (GtkIconSize, icon_sizes_used);
1936 *n_sizes = icon_sizes_used - 1;
1939 while (i < icon_sizes_used)
1941 (*sizes)[i - 1] = icon_sizes[i].size;
1949 *n_sizes = g_slist_length (specifics);
1950 *sizes = g_new (GtkIconSize, *n_sizes);
1953 tmp_list = specifics;
1954 while (tmp_list != NULL)
1956 (*sizes)[i] = GPOINTER_TO_INT (tmp_list->data);
1959 tmp_list = g_slist_next (tmp_list);
1963 g_slist_free (specifics);
1968 * gtk_icon_source_new:
1970 * Creates a new #GtkIconSource. A #GtkIconSource contains a #GdkPixbuf (or
1971 * image filename) that serves as the base image for one or more of the
1972 * icons in a #GtkIconSet, along with a specification for which icons in the
1973 * icon set will be based on that pixbuf or image file. An icon set contains
1974 * a set of icons that represent "the same" logical concept in different states,
1975 * different global text directions, and different sizes.
1977 * So for example a web browser's "Back to Previous Page" icon might
1978 * point in a different direction in Hebrew and in English; it might
1979 * look different when insensitive; and it might change size depending
1980 * on toolbar mode (small/large icons). So a single icon set would
1981 * contain all those variants of the icon. #GtkIconSet contains a list
1982 * of #GtkIconSource from which it can derive specific icon variants in
1985 * In the simplest case, #GtkIconSet contains one source pixbuf from
1986 * which it derives all variants. The convenience function
1987 * gtk_icon_set_new_from_pixbuf() handles this case; if you only have
1988 * one source pixbuf, just use that function.
1990 * If you want to use a different base pixbuf for different icon
1991 * variants, you create multiple icon sources, mark which variants
1992 * they'll be used to create, and add them to the icon set with
1993 * gtk_icon_set_add_source().
1995 * By default, the icon source has all parameters wildcarded. That is,
1996 * the icon source will be used as the base icon for any desired text
1997 * direction, widget state, or icon size.
1999 * Return value: a new #GtkIconSource
2002 gtk_icon_source_new (void)
2006 src = g_new0 (GtkIconSource, 1);
2008 src->direction = GTK_TEXT_DIR_NONE;
2009 src->size = GTK_ICON_SIZE_INVALID;
2010 src->state = GTK_STATE_NORMAL;
2012 src->any_direction = TRUE;
2013 src->any_state = TRUE;
2014 src->any_size = TRUE;
2020 * gtk_icon_source_copy:
2021 * @source: a #GtkIconSource
2023 * Creates a copy of @source; mostly useful for language bindings.
2025 * Return value: a new #GtkIconSource
2028 gtk_icon_source_copy (const GtkIconSource *source)
2030 GtkIconSource *copy;
2032 g_return_val_if_fail (source != NULL, NULL);
2034 copy = g_new (GtkIconSource, 1);
2038 copy->filename = g_strdup (source->filename);
2039 copy->size = source->size;
2041 g_object_ref (copy->pixbuf);
2047 * gtk_icon_source_free:
2048 * @source: a #GtkIconSource
2050 * Frees a dynamically-allocated icon source, along with its
2051 * filename, size, and pixbuf fields if those are not %NULL.
2054 gtk_icon_source_free (GtkIconSource *source)
2056 g_return_if_fail (source != NULL);
2058 g_free ((char*) source->filename);
2060 g_object_unref (source->pixbuf);
2066 gtk_icon_source_get_type (void)
2068 static GType our_type = 0;
2071 our_type = g_boxed_type_register_static ("GtkIconSource",
2072 (GBoxedCopyFunc) gtk_icon_source_copy,
2073 (GBoxedFreeFunc) gtk_icon_source_free);
2079 * gtk_icon_source_set_filename:
2080 * @source: a #GtkIconSource
2081 * @filename: image file to use
2083 * Sets the name of an image file to use as a base image when creating
2084 * icon variants for #GtkIconSet. The filename must be absolute.
2087 gtk_icon_source_set_filename (GtkIconSource *source,
2088 const gchar *filename)
2090 g_return_if_fail (source != NULL);
2091 g_return_if_fail (filename == NULL || g_path_is_absolute (filename));
2093 if (source->filename == filename)
2096 if (source->filename)
2097 g_free (source->filename);
2099 source->filename = g_strdup (filename);
2103 * gtk_icon_source_set_pixbuf:
2104 * @source: a #GtkIconSource
2105 * @pixbuf: pixbuf to use as a source
2107 * Sets a pixbuf to use as a base image when creating icon variants
2108 * for #GtkIconSet. If an icon source has both a filename and a pixbuf
2109 * set, the pixbuf will take priority.
2113 gtk_icon_source_set_pixbuf (GtkIconSource *source,
2116 g_return_if_fail (source != NULL);
2119 g_object_ref (pixbuf);
2122 g_object_unref (source->pixbuf);
2124 source->pixbuf = pixbuf;
2128 * gtk_icon_source_get_filename:
2129 * @source: a #GtkIconSource
2131 * Retrieves the source filename, or %NULL if none is set. The
2132 * filename is not a copy, and should not be modified or expected to
2133 * persist beyond the lifetime of the icon source.
2135 * Return value: image filename. This string must not be modified
2138 G_CONST_RETURN gchar*
2139 gtk_icon_source_get_filename (const GtkIconSource *source)
2141 g_return_val_if_fail (source != NULL, NULL);
2143 return source->filename;
2147 * gtk_icon_source_get_pixbuf:
2148 * @source: a #GtkIconSource
2150 * Retrieves the source pixbuf, or %NULL if none is set.
2151 * The reference count on the pixbuf is not incremented.
2153 * Return value: source pixbuf
2156 gtk_icon_source_get_pixbuf (const GtkIconSource *source)
2158 g_return_val_if_fail (source != NULL, NULL);
2160 return source->pixbuf;
2164 * gtk_icon_source_set_direction_wildcarded:
2165 * @source: a #GtkIconSource
2166 * @setting: %TRUE to wildcard the text direction
2168 * If the text direction is wildcarded, this source can be used
2169 * as the base image for an icon in any #GtkTextDirection.
2170 * If the text direction is not wildcarded, then the
2171 * text direction the icon source applies to should be set
2172 * with gtk_icon_source_set_direction(), and the icon source
2173 * will only be used with that text direction.
2175 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2176 * wildcarded sources, and will use an exact match when possible.
2180 gtk_icon_source_set_direction_wildcarded (GtkIconSource *source,
2183 g_return_if_fail (source != NULL);
2185 source->any_direction = setting != FALSE;
2189 * gtk_icon_source_set_state_wildcarded:
2190 * @source: a #GtkIconSource
2191 * @setting: %TRUE to wildcard the widget state
2193 * If the widget state is wildcarded, this source can be used as the
2194 * base image for an icon in any #GtkStateType. If the widget state
2195 * is not wildcarded, then the state the source applies to should be
2196 * set with gtk_icon_source_set_state() and the icon source will
2197 * only be used with that specific state.
2199 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2200 * wildcarded sources, and will use an exact match when possible.
2202 * #GtkIconSet will normally transform wildcarded source images to
2203 * produce an appropriate icon for a given state, for example
2204 * lightening an image on prelight, but will not modify source images
2205 * that match exactly.
2208 gtk_icon_source_set_state_wildcarded (GtkIconSource *source,
2211 g_return_if_fail (source != NULL);
2213 source->any_state = setting != FALSE;
2218 * gtk_icon_source_set_size_wildcarded:
2219 * @source: a #GtkIconSource
2220 * @setting: %TRUE to wildcard the widget state
2222 * If the icon size is wildcarded, this source can be used as the base
2223 * image for an icon of any size. If the size is not wildcarded, then
2224 * the size the source applies to should be set with
2225 * gtk_icon_source_set_size() and the icon source will only be used
2226 * with that specific size.
2228 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2229 * wildcarded sources, and will use an exact match when possible.
2231 * #GtkIconSet will normally scale wildcarded source images to produce
2232 * an appropriate icon at a given size, but will not change the size
2233 * of source images that match exactly.
2236 gtk_icon_source_set_size_wildcarded (GtkIconSource *source,
2239 g_return_if_fail (source != NULL);
2241 source->any_size = setting != FALSE;
2245 * gtk_icon_source_get_size_wildcarded:
2246 * @source: a #GtkIconSource
2248 * Gets the value set by gtk_icon_source_set_size_wildcarded().
2250 * Return value: %TRUE if this icon source is a base for any icon size variant
2253 gtk_icon_source_get_size_wildcarded (const GtkIconSource *source)
2255 g_return_val_if_fail (source != NULL, TRUE);
2257 return source->any_size;
2261 * gtk_icon_source_get_state_wildcarded:
2262 * @source: a #GtkIconSource
2264 * Gets the value set by gtk_icon_source_set_state_wildcarded().
2266 * Return value: %TRUE if this icon source is a base for any widget state variant
2269 gtk_icon_source_get_state_wildcarded (const GtkIconSource *source)
2271 g_return_val_if_fail (source != NULL, TRUE);
2273 return source->any_state;
2277 * gtk_icon_source_get_direction_wildcarded:
2278 * @source: a #GtkIconSource
2280 * Gets the value set by gtk_icon_source_set_direction_wildcarded().
2282 * Return value: %TRUE if this icon source is a base for any text direction variant
2285 gtk_icon_source_get_direction_wildcarded (const GtkIconSource *source)
2287 g_return_val_if_fail (source != NULL, TRUE);
2289 return source->any_direction;
2293 * gtk_icon_source_set_direction:
2294 * @source: a #GtkIconSource
2295 * @direction: text direction this source applies to
2297 * Sets the text direction this icon source is intended to be used
2300 * Setting the text direction on an icon source makes no difference
2301 * if the text direction is wildcarded. Therefore, you should usually
2302 * call gtk_icon_source_set_direction_wildcarded() to un-wildcard it
2303 * in addition to calling this function.
2307 gtk_icon_source_set_direction (GtkIconSource *source,
2308 GtkTextDirection direction)
2310 g_return_if_fail (source != NULL);
2312 source->direction = direction;
2316 * gtk_icon_source_set_state:
2317 * @source: a #GtkIconSource
2318 * @state: widget state this source applies to
2320 * Sets the widget state this icon source is intended to be used
2323 * Setting the widget state on an icon source makes no difference
2324 * if the state is wildcarded. Therefore, you should usually
2325 * call gtk_icon_source_set_state_wildcarded() to un-wildcard it
2326 * in addition to calling this function.
2330 gtk_icon_source_set_state (GtkIconSource *source,
2333 g_return_if_fail (source != NULL);
2335 source->state = state;
2339 * gtk_icon_source_set_size:
2340 * @source: a #GtkIconSource
2341 * @size: icon size this source applies to
2343 * Sets the icon size this icon source is intended to be used
2346 * Setting the icon size on an icon source makes no difference
2347 * if the size is wildcarded. Therefore, you should usually
2348 * call gtk_icon_source_set_size_wildcarded() to un-wildcard it
2349 * in addition to calling this function.
2353 gtk_icon_source_set_size (GtkIconSource *source,
2356 g_return_if_fail (source != NULL);
2358 source->size = size;
2362 * gtk_icon_source_get_direction:
2363 * @source: a #GtkIconSource
2365 * Obtains the text direction this icon source applies to. The return
2366 * value is only useful/meaningful if the text direction is <emphasis>not</emphasis>
2369 * Return value: text direction this source matches
2372 gtk_icon_source_get_direction (const GtkIconSource *source)
2374 g_return_val_if_fail (source != NULL, 0);
2376 return source->direction;
2380 * gtk_icon_source_get_state:
2381 * @source: a #GtkIconSource
2383 * Obtains the widget state this icon source applies to. The return
2384 * value is only useful/meaningful if the widget state is <emphasis>not</emphasis>
2387 * Return value: widget state this source matches
2390 gtk_icon_source_get_state (const GtkIconSource *source)
2392 g_return_val_if_fail (source != NULL, 0);
2394 return source->state;
2398 * gtk_icon_source_get_size:
2399 * @source: a #GtkIconSource
2401 * Obtains the icon size this source applies to. The return value
2402 * is only useful/meaningful if the icon size is <emphasis>not</emphasis> wildcarded.
2404 * Return value: icon size this source matches.
2407 gtk_icon_source_get_size (const GtkIconSource *source)
2409 g_return_val_if_fail (source != NULL, 0);
2411 return source->size;
2414 /* Note that the logical maximum is 20 per GtkTextDirection, so we could
2415 * eventually set this to >20 to never throw anything out.
2417 #define NUM_CACHED_ICONS 8
2419 typedef struct _CachedIcon CachedIcon;
2423 /* These must all match to use the cached pixbuf.
2424 * If any don't match, we must re-render the pixbuf.
2427 GtkTextDirection direction;
2435 ensure_cache_up_to_date (GtkIconSet *icon_set)
2437 if (icon_set->cache_serial != cache_serial)
2438 clear_cache (icon_set, TRUE);
2442 cached_icon_free (CachedIcon *icon)
2444 g_object_unref (icon->pixbuf);
2450 find_in_cache (GtkIconSet *icon_set,
2452 GtkTextDirection direction,
2459 ensure_cache_up_to_date (icon_set);
2462 tmp_list = icon_set->cache;
2463 while (tmp_list != NULL)
2465 CachedIcon *icon = tmp_list->data;
2467 if (icon->style == style &&
2468 icon->direction == direction &&
2469 icon->state == state &&
2474 /* Move this icon to the front of the list. */
2475 prev->next = tmp_list->next;
2476 tmp_list->next = icon_set->cache;
2477 icon_set->cache = tmp_list;
2480 return icon->pixbuf;
2484 tmp_list = g_slist_next (tmp_list);
2491 add_to_cache (GtkIconSet *icon_set,
2493 GtkTextDirection direction,
2500 ensure_cache_up_to_date (icon_set);
2502 g_object_ref (pixbuf);
2504 /* We have to ref the style, since if the style was finalized
2505 * its address could be reused by another style, creating a
2510 g_object_ref (style);
2513 icon = g_new (CachedIcon, 1);
2514 icon_set->cache = g_slist_prepend (icon_set->cache, icon);
2516 icon->style = style;
2517 icon->direction = direction;
2518 icon->state = state;
2520 icon->pixbuf = pixbuf;
2523 attach_to_style (icon_set, icon->style);
2525 if (icon_set->cache_size >= NUM_CACHED_ICONS)
2527 /* Remove oldest item in the cache */
2531 tmp_list = icon_set->cache;
2533 /* Find next-to-last link */
2534 g_assert (NUM_CACHED_ICONS > 2);
2535 while (tmp_list->next->next)
2536 tmp_list = tmp_list->next;
2538 g_assert (tmp_list != NULL);
2539 g_assert (tmp_list->next != NULL);
2540 g_assert (tmp_list->next->next == NULL);
2542 /* Free the last icon */
2543 icon = tmp_list->next->data;
2545 g_slist_free (tmp_list->next);
2546 tmp_list->next = NULL;
2548 cached_icon_free (icon);
2553 clear_cache (GtkIconSet *icon_set,
2554 gboolean style_detach)
2557 GtkStyle *last_style = NULL;
2559 tmp_list = icon_set->cache;
2560 while (tmp_list != NULL)
2562 CachedIcon *icon = tmp_list->data;
2566 /* simple optimization for the case where the cache
2567 * contains contiguous icons from the same style.
2568 * it's safe to call detach_from_style more than
2569 * once on the same style though.
2571 if (last_style != icon->style)
2573 detach_from_style (icon_set, icon->style);
2574 last_style = icon->style;
2578 cached_icon_free (icon);
2580 tmp_list = g_slist_next (tmp_list);
2583 g_slist_free (icon_set->cache);
2584 icon_set->cache = NULL;
2585 icon_set->cache_size = 0;
2589 copy_cache (GtkIconSet *icon_set,
2590 GtkIconSet *copy_recipient)
2593 GSList *copy = NULL;
2595 ensure_cache_up_to_date (icon_set);
2597 tmp_list = icon_set->cache;
2598 while (tmp_list != NULL)
2600 CachedIcon *icon = tmp_list->data;
2601 CachedIcon *icon_copy = g_new (CachedIcon, 1);
2605 if (icon_copy->style)
2606 attach_to_style (copy_recipient, icon_copy->style);
2608 g_object_ref (icon_copy->pixbuf);
2610 icon_copy->size = icon->size;
2612 copy = g_slist_prepend (copy, icon_copy);
2614 tmp_list = g_slist_next (tmp_list);
2617 return g_slist_reverse (copy);
2621 attach_to_style (GtkIconSet *icon_set,
2626 table = g_object_get_qdata (G_OBJECT (style),
2627 g_quark_try_string ("gtk-style-icon-sets"));
2631 table = g_hash_table_new (NULL, NULL);
2632 g_object_set_qdata_full (G_OBJECT (style),
2633 g_quark_from_static_string ("gtk-style-icon-sets"),
2638 g_hash_table_insert (table, icon_set, icon_set);
2642 detach_from_style (GtkIconSet *icon_set,
2647 table = g_object_get_qdata (G_OBJECT (style),
2648 g_quark_try_string ("gtk-style-icon-sets"));
2651 g_hash_table_remove (table, icon_set);
2655 iconsets_foreach (gpointer key,
2659 GtkIconSet *icon_set = key;
2661 /* We only need to remove cache entries for the given style;
2662 * but that complicates things because in destroy notify
2663 * we don't know which style got destroyed, and 95% of the
2664 * time all cache entries will have the same style,
2665 * so this is faster anyway.
2668 clear_cache (icon_set, FALSE);
2672 style_dnotify (gpointer data)
2674 GHashTable *table = data;
2676 g_hash_table_foreach (table, iconsets_foreach, NULL);
2678 g_hash_table_destroy (table);
2681 /* This allows the icon set to detect that its cache is out of date. */
2683 _gtk_icon_set_invalidate_caches (void)
2689 listify_foreach (gpointer key, gpointer value, gpointer data)
2691 GSList **list = data;
2693 *list = g_slist_prepend (*list, key);
2697 g_hash_table_get_keys (GHashTable *table)
2699 GSList *list = NULL;
2701 g_hash_table_foreach (table, listify_foreach, &list);
2707 * _gtk_icon_factory_list_ids:
2709 * Gets all known IDs stored in an existing icon factory.
2710 * The strings in the returned list aren't copied.
2711 * The list itself should be freed.
2713 * Return value: List of ids in icon factories
2716 _gtk_icon_factory_list_ids (void)
2723 ensure_default_icons ();
2725 tmp_list = all_icon_factories;
2726 while (tmp_list != NULL)
2730 GtkIconFactory *factory = GTK_ICON_FACTORY (tmp_list->data);
2732 these_ids = g_hash_table_get_keys (factory->icons);
2734 ids = g_slist_concat (ids, these_ids);
2736 tmp_list = g_slist_next (tmp_list);