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
1217 gtk_icon_size_lookup_for_settings (GtkSettings *settings,
1222 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1224 return icon_size_lookup_intern (settings, size, width, height);
1228 * gtk_icon_size_lookup:
1229 * @size: an icon size
1230 * @width: location to store icon width
1231 * @height: location to store icon height
1233 * Obtains the pixel size of a semantic icon size, possibly
1234 * modified by user preferences for the default #GtkSettings.
1235 * (See gtk_icon_size_lookup_for_settings().)
1236 * Normally @size would be
1237 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
1238 * isn't normally needed, gtk_widget_render_icon() is the usual
1239 * way to get an icon for rendering, then just look at the size of
1240 * the rendered pixbuf. The rendered pixbuf may not even correspond to
1241 * the width/height returned by gtk_icon_size_lookup(), because themes
1242 * are free to render the pixbuf however they like, including changing
1245 * Return value: %TRUE if @size was a valid size
1248 gtk_icon_size_lookup (GtkIconSize size,
1252 GTK_NOTE (MULTIHEAD,
1253 g_warning ("gtk_icon_size_lookup ()) is not multihead safe"));
1255 return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (),
1256 size, widthp, heightp);
1260 icon_size_register_intern (const gchar *name,
1264 IconAlias *old_alias;
1269 old_alias = g_hash_table_lookup (icon_aliases, name);
1270 if (old_alias && icon_sizes[old_alias->target].width > 0)
1272 g_warning ("Icon size name '%s' already exists", name);
1273 return GTK_ICON_SIZE_INVALID;
1278 size = old_alias->target;
1282 if (icon_sizes_used == icon_sizes_allocated)
1284 icon_sizes_allocated *= 2;
1285 icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated);
1288 size = icon_sizes_used++;
1290 /* alias to self. */
1291 gtk_icon_size_register_alias (name, size);
1293 icon_sizes[size].size = size;
1294 icon_sizes[size].name = g_strdup (name);
1297 icon_sizes[size].width = width;
1298 icon_sizes[size].height = height;
1304 * gtk_icon_size_register:
1305 * @name: name of the icon size
1306 * @width: the icon width
1307 * @height: the icon height
1309 * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
1310 * etc. Returns the integer value for the size.
1312 * Returns: integer value representing the size
1316 gtk_icon_size_register (const gchar *name,
1320 g_return_val_if_fail (name != NULL, 0);
1321 g_return_val_if_fail (width > 0, 0);
1322 g_return_val_if_fail (height > 0, 0);
1324 return icon_size_register_intern (name, width, height);
1328 * gtk_icon_size_register_alias:
1329 * @alias: an alias for @target
1330 * @target: an existing icon size
1332 * Registers @alias as another name for @target.
1333 * So calling gtk_icon_size_from_name() with @alias as argument
1334 * will return @target.
1338 gtk_icon_size_register_alias (const gchar *alias,
1343 g_return_if_fail (alias != NULL);
1347 if (!icon_size_lookup_intern (NULL, target, NULL, NULL))
1348 g_warning ("gtk_icon_size_register_alias: Icon size %d does not exist", target);
1350 ia = g_hash_table_lookup (icon_aliases, alias);
1353 if (icon_sizes[ia->target].width > 0)
1355 g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias);
1359 ia->target = target;
1364 ia = g_new (IconAlias, 1);
1365 ia->name = g_strdup (alias);
1366 ia->target = target;
1368 g_hash_table_insert (icon_aliases, ia->name, ia);
1373 * gtk_icon_size_from_name:
1374 * @name: the name to look up.
1375 * @returns: the icon size with the given name.
1377 * Looks up the icon size associated with @name.
1380 gtk_icon_size_from_name (const gchar *name)
1386 ia = g_hash_table_lookup (icon_aliases, name);
1388 if (ia && icon_sizes[ia->target].width > 0)
1391 return GTK_ICON_SIZE_INVALID;
1395 * gtk_icon_size_get_name:
1396 * @size: a #GtkIconSize.
1397 * @returns: the name of the given icon size.
1399 * Gets the canonical name of the given icon size. The returned string
1400 * is statically allocated and should not be freed.
1402 G_CONST_RETURN gchar*
1403 gtk_icon_size_get_name (GtkIconSize size)
1405 if (size >= icon_sizes_used)
1408 return icon_sizes[size].name;
1411 /************************************************************/
1416 /* Clear icon set contents, drop references to all contained
1417 * GdkPixbuf objects and forget all GtkIconSources. Used to
1418 * recycle an icon set.
1420 static GdkPixbuf *find_in_cache (GtkIconSet *icon_set,
1422 GtkTextDirection direction,
1425 static void add_to_cache (GtkIconSet *icon_set,
1427 GtkTextDirection direction,
1431 static void clear_cache (GtkIconSet *icon_set,
1432 gboolean style_detach);
1433 static GSList* copy_cache (GtkIconSet *icon_set,
1434 GtkIconSet *copy_recipient);
1435 static void attach_to_style (GtkIconSet *icon_set,
1437 static void detach_from_style (GtkIconSet *icon_set,
1439 static void style_dnotify (gpointer data);
1447 /* Cache of the last few rendered versions of the icon. */
1455 static guint cache_serial = 0;
1460 * Creates a new #GtkIconSet. A #GtkIconSet represents a single icon
1461 * in various sizes and widget states. It can provide a #GdkPixbuf
1462 * for a given size and state on request, and automatically caches
1463 * some of the rendered #GdkPixbuf objects.
1465 * Normally you would use gtk_widget_render_icon() instead of
1466 * using #GtkIconSet directly. The one case where you'd use
1467 * #GtkIconSet is to create application-specific icon sets to place in
1468 * a #GtkIconFactory.
1470 * Return value: a new #GtkIconSet
1473 gtk_icon_set_new (void)
1475 GtkIconSet *icon_set;
1477 icon_set = g_new (GtkIconSet, 1);
1479 icon_set->ref_count = 1;
1480 icon_set->sources = NULL;
1481 icon_set->cache = NULL;
1482 icon_set->cache_size = 0;
1483 icon_set->cache_serial = cache_serial;
1489 * gtk_icon_set_new_from_pixbuf:
1490 * @pixbuf: a #GdkPixbuf
1492 * Creates a new #GtkIconSet with @pixbuf as the default/fallback
1493 * source image. If you don't add any additional #GtkIconSource to the
1494 * icon set, all variants of the icon will be created from @pixbuf,
1495 * using scaling, pixelation, etc. as required to adjust the icon size
1496 * or make the icon look insensitive/prelighted.
1498 * Return value: a new #GtkIconSet
1501 gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
1505 GtkIconSource source = { NULL, NULL, 0, 0, 0,
1508 g_return_val_if_fail (pixbuf != NULL, NULL);
1510 set = gtk_icon_set_new ();
1512 source.pixbuf = pixbuf;
1514 gtk_icon_set_add_source (set, &source);
1522 * @icon_set: a #GtkIconSet.
1524 * Increments the reference count on @icon_set.
1526 * Return value: @icon_set.
1529 gtk_icon_set_ref (GtkIconSet *icon_set)
1531 g_return_val_if_fail (icon_set != NULL, NULL);
1532 g_return_val_if_fail (icon_set->ref_count > 0, NULL);
1534 icon_set->ref_count += 1;
1540 * gtk_icon_set_unref:
1541 * @icon_set: a #GtkIconSet
1543 * Decrements the reference count on @icon_set, and frees memory
1544 * if the reference count reaches 0.
1547 gtk_icon_set_unref (GtkIconSet *icon_set)
1549 g_return_if_fail (icon_set != NULL);
1550 g_return_if_fail (icon_set->ref_count > 0);
1552 icon_set->ref_count -= 1;
1554 if (icon_set->ref_count == 0)
1556 GSList *tmp_list = icon_set->sources;
1557 while (tmp_list != NULL)
1559 gtk_icon_source_free (tmp_list->data);
1561 tmp_list = g_slist_next (tmp_list);
1564 clear_cache (icon_set, TRUE);
1571 gtk_icon_set_get_type (void)
1573 static GType our_type = 0;
1576 our_type = g_boxed_type_register_static ("GtkIconSet",
1577 (GBoxedCopyFunc) gtk_icon_set_ref,
1578 (GBoxedFreeFunc) gtk_icon_set_unref);
1584 * gtk_icon_set_copy:
1585 * @icon_set: a #GtkIconSet
1587 * Copies @icon_set by value.
1589 * Return value: a new #GtkIconSet identical to the first.
1592 gtk_icon_set_copy (GtkIconSet *icon_set)
1597 copy = gtk_icon_set_new ();
1599 tmp_list = icon_set->sources;
1600 while (tmp_list != NULL)
1602 copy->sources = g_slist_prepend (copy->sources,
1603 gtk_icon_source_copy (tmp_list->data));
1605 tmp_list = g_slist_next (tmp_list);
1608 copy->sources = g_slist_reverse (copy->sources);
1610 copy->cache = copy_cache (icon_set, copy);
1611 copy->cache_size = icon_set->cache_size;
1612 copy->cache_serial = icon_set->cache_serial;
1618 sizes_equivalent (GtkIconSize lhs,
1621 /* We used to consider sizes equivalent if they were
1622 * the same pixel size, but we don't have the GtkSettings
1623 * here, so we can't do that. Plus, it's not clear that
1624 * it is right... it was just a workaround for the fact
1625 * that we register icons by logical size, not pixel size.
1631 gint r_w, r_h, l_w, l_h;
1633 icon_size_lookup_intern (NULL, rhs, &r_w, &r_h);
1634 icon_size_lookup_intern (NULL, lhs, &l_w, &l_h);
1636 return r_w == l_w && r_h == l_h;
1640 static GtkIconSource*
1641 find_and_prep_icon_source (GtkIconSet *icon_set,
1642 GtkTextDirection direction,
1646 GtkIconSource *source;
1650 /* We need to find the best icon source. Direction matters more
1651 * than state, state matters more than size. icon_set->sources
1652 * is sorted according to wildness, so if we take the first
1653 * match we find it will be the least-wild match (if there are
1654 * multiple matches for a given "wildness" then the RC file contained
1655 * dumb stuff, and we end up with an arbitrary matching source)
1659 tmp_list = icon_set->sources;
1660 while (tmp_list != NULL)
1662 GtkIconSource *s = tmp_list->data;
1664 if ((s->any_direction || (s->direction == direction)) &&
1665 (s->any_state || (s->state == state)) &&
1666 (s->any_size || (sizes_equivalent (size, s->size))))
1672 tmp_list = g_slist_next (tmp_list);
1678 if (source->pixbuf == NULL)
1680 GError *error = NULL;
1682 g_assert (source->filename);
1683 source->pixbuf = gdk_pixbuf_new_from_file (source->filename, &error);
1685 if (source->pixbuf == NULL)
1687 /* Remove this icon source so we don't keep trying to
1690 g_warning (_("Error loading icon: %s"), error->message);
1691 g_error_free (error);
1693 icon_set->sources = g_slist_remove (icon_set->sources, source);
1695 gtk_icon_source_free (source);
1697 /* Try to fall back to other sources */
1698 if (icon_set->sources != NULL)
1699 return find_and_prep_icon_source (icon_set,
1712 render_fallback_image (GtkStyle *style,
1713 GtkTextDirection direction,
1719 /* This icon can be used for any direction/state/size */
1720 static GtkIconSource fallback_source = { NULL, NULL, 0, 0, 0, TRUE, TRUE, TRUE };
1722 if (fallback_source.pixbuf == NULL)
1723 fallback_source.pixbuf = gdk_pixbuf_new_from_inline (-1, stock_missing_image_24, FALSE, NULL);
1725 return gtk_style_render_icon (style,
1735 * gtk_icon_set_render_icon:
1736 * @icon_set: a #GtkIconSet
1737 * @style: a #GtkStyle associated with @widget, or %NULL
1738 * @direction: text direction
1739 * @state: widget state
1741 * @widget: widget that will display the icon, or %NULL
1742 * @detail: detail to pass to the theme engine, or %NULL
1744 * Renders an icon using gtk_style_render_icon(). In most cases,
1745 * gtk_widget_render_icon() is better, since it automatically provides
1746 * most of the arguments from the current widget settings. This
1747 * function never returns %NULL; if the icon can't be rendered
1748 * (perhaps because an image file fails to load), a default "missing
1749 * image" icon will be returned instead.
1751 * Return value: a #GdkPixbuf to be displayed
1754 gtk_icon_set_render_icon (GtkIconSet *icon_set,
1756 GtkTextDirection direction,
1763 GtkIconSource *source;
1765 g_return_val_if_fail (icon_set != NULL, NULL);
1766 g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
1768 if (icon_set->sources == NULL)
1769 return render_fallback_image (style, direction, state, size, widget, detail);
1771 icon = find_in_cache (icon_set, style, direction,
1776 g_object_ref (icon);
1781 source = find_and_prep_icon_source (icon_set, direction, state, size);
1784 return render_fallback_image (style, direction, state, size, widget, detail);
1786 g_assert (source->pixbuf != NULL);
1788 icon = gtk_style_render_icon (style,
1798 g_warning ("Theme engine failed to render icon");
1802 add_to_cache (icon_set, style, direction, state, size, icon);
1807 /* Order sources by their "wildness", so that "wilder" sources are
1808 * greater than "specific" sources; for determining ordering,
1809 * direction beats state beats size.
1813 icon_source_compare (gconstpointer ap, gconstpointer bp)
1815 const GtkIconSource *a = ap;
1816 const GtkIconSource *b = bp;
1818 if (!a->any_direction && b->any_direction)
1820 else if (a->any_direction && !b->any_direction)
1822 else if (!a->any_state && b->any_state)
1824 else if (a->any_state && !b->any_state)
1826 else if (!a->any_size && b->any_size)
1828 else if (a->any_size && !b->any_size)
1835 * gtk_icon_set_add_source:
1836 * @icon_set: a #GtkIconSet
1837 * @source: a #GtkIconSource
1839 * Icon sets have a list of #GtkIconSource, which they use as base
1840 * icons for rendering icons in different states and sizes. Icons are
1841 * scaled, made to look insensitive, etc. in
1842 * gtk_icon_set_render_icon(), but #GtkIconSet needs base images to
1843 * work with. The base images and when to use them are described by
1846 * This function copies @source, so you can reuse the same source immediately
1847 * without affecting the icon set.
1849 * An example of when you'd use this function: a web browser's "Back
1850 * to Previous Page" icon might point in a different direction in
1851 * Hebrew and in English; it might look different when insensitive;
1852 * and it might change size depending on toolbar mode (small/large
1853 * icons). So a single icon set would contain all those variants of
1854 * the icon, and you might add a separate source for each one.
1856 * You should nearly always add a "default" icon source with all
1857 * fields wildcarded, which will be used as a fallback if no more
1858 * specific source matches. #GtkIconSet always prefers more specific
1859 * icon sources to more generic icon sources. The order in which you
1860 * add the sources to the icon set does not matter.
1862 * gtk_icon_set_new_from_pixbuf() creates a new icon set with a
1863 * default icon source based on the given pixbuf.
1867 gtk_icon_set_add_source (GtkIconSet *icon_set,
1868 const GtkIconSource *source)
1870 g_return_if_fail (icon_set != NULL);
1871 g_return_if_fail (source != NULL);
1873 if (source->pixbuf == NULL &&
1874 source->filename == NULL)
1876 g_warning ("Useless GtkIconSource contains NULL filename and pixbuf");
1880 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
1881 gtk_icon_source_copy (source),
1882 icon_source_compare);
1886 * gtk_icon_set_get_sizes:
1887 * @icon_set: a #GtkIconSet
1888 * @sizes: return location for array of sizes
1889 * @n_sizes: location to store number of elements in returned array
1891 * Obtains a list of icon sizes this icon set can render. The returned
1892 * array must be freed with g_free().
1896 gtk_icon_set_get_sizes (GtkIconSet *icon_set,
1897 GtkIconSize **sizes,
1901 gboolean all_sizes = FALSE;
1902 GSList *specifics = NULL;
1904 g_return_if_fail (icon_set != NULL);
1905 g_return_if_fail (sizes != NULL);
1906 g_return_if_fail (n_sizes != NULL);
1908 tmp_list = icon_set->sources;
1909 while (tmp_list != NULL)
1911 GtkIconSource *source;
1913 source = tmp_list->data;
1915 if (source->any_size)
1921 specifics = g_slist_prepend (specifics, GINT_TO_POINTER (source->size));
1923 tmp_list = g_slist_next (tmp_list);
1928 /* Need to find out what sizes exist */
1933 *sizes = g_new (GtkIconSize, icon_sizes_used);
1934 *n_sizes = icon_sizes_used - 1;
1937 while (i < icon_sizes_used)
1939 (*sizes)[i - 1] = icon_sizes[i].size;
1947 *n_sizes = g_slist_length (specifics);
1948 *sizes = g_new (GtkIconSize, *n_sizes);
1951 tmp_list = specifics;
1952 while (tmp_list != NULL)
1954 (*sizes)[i] = GPOINTER_TO_INT (tmp_list->data);
1957 tmp_list = g_slist_next (tmp_list);
1961 g_slist_free (specifics);
1966 * gtk_icon_source_new:
1968 * Creates a new #GtkIconSource. A #GtkIconSource contains a #GdkPixbuf (or
1969 * image filename) that serves as the base image for one or more of the
1970 * icons in a #GtkIconSet, along with a specification for which icons in the
1971 * icon set will be based on that pixbuf or image file. An icon set contains
1972 * a set of icons that represent "the same" logical concept in different states,
1973 * different global text directions, and different sizes.
1975 * So for example a web browser's "Back to Previous Page" icon might
1976 * point in a different direction in Hebrew and in English; it might
1977 * look different when insensitive; and it might change size depending
1978 * on toolbar mode (small/large icons). So a single icon set would
1979 * contain all those variants of the icon. #GtkIconSet contains a list
1980 * of #GtkIconSource from which it can derive specific icon variants in
1983 * In the simplest case, #GtkIconSet contains one source pixbuf from
1984 * which it derives all variants. The convenience function
1985 * gtk_icon_set_new_from_pixbuf() handles this case; if you only have
1986 * one source pixbuf, just use that function.
1988 * If you want to use a different base pixbuf for different icon
1989 * variants, you create multiple icon sources, mark which variants
1990 * they'll be used to create, and add them to the icon set with
1991 * gtk_icon_set_add_source().
1993 * By default, the icon source has all parameters wildcarded. That is,
1994 * the icon source will be used as the base icon for any desired text
1995 * direction, widget state, or icon size.
1997 * Return value: a new #GtkIconSource
2000 gtk_icon_source_new (void)
2004 src = g_new0 (GtkIconSource, 1);
2006 src->direction = GTK_TEXT_DIR_NONE;
2007 src->size = GTK_ICON_SIZE_INVALID;
2008 src->state = GTK_STATE_NORMAL;
2010 src->any_direction = TRUE;
2011 src->any_state = TRUE;
2012 src->any_size = TRUE;
2018 * gtk_icon_source_copy:
2019 * @source: a #GtkIconSource
2021 * Creates a copy of @source; mostly useful for language bindings.
2023 * Return value: a new #GtkIconSource
2026 gtk_icon_source_copy (const GtkIconSource *source)
2028 GtkIconSource *copy;
2030 g_return_val_if_fail (source != NULL, NULL);
2032 copy = g_new (GtkIconSource, 1);
2036 copy->filename = g_strdup (source->filename);
2037 copy->size = source->size;
2039 g_object_ref (copy->pixbuf);
2045 * gtk_icon_source_free:
2046 * @source: a #GtkIconSource
2048 * Frees a dynamically-allocated icon source, along with its
2049 * filename, size, and pixbuf fields if those are not %NULL.
2052 gtk_icon_source_free (GtkIconSource *source)
2054 g_return_if_fail (source != NULL);
2056 g_free ((char*) source->filename);
2058 g_object_unref (source->pixbuf);
2064 gtk_icon_source_get_type (void)
2066 static GType our_type = 0;
2069 our_type = g_boxed_type_register_static ("GtkIconSource",
2070 (GBoxedCopyFunc) gtk_icon_source_copy,
2071 (GBoxedFreeFunc) gtk_icon_source_free);
2077 * gtk_icon_source_set_filename:
2078 * @source: a #GtkIconSource
2079 * @filename: image file to use
2081 * Sets the name of an image file to use as a base image when creating
2082 * icon variants for #GtkIconSet. The filename must be absolute.
2085 gtk_icon_source_set_filename (GtkIconSource *source,
2086 const gchar *filename)
2088 g_return_if_fail (source != NULL);
2089 g_return_if_fail (filename == NULL || g_path_is_absolute (filename));
2091 if (source->filename == filename)
2094 if (source->filename)
2095 g_free (source->filename);
2097 source->filename = g_strdup (filename);
2101 * gtk_icon_source_set_pixbuf:
2102 * @source: a #GtkIconSource
2103 * @pixbuf: pixbuf to use as a source
2105 * Sets a pixbuf to use as a base image when creating icon variants
2106 * for #GtkIconSet. If an icon source has both a filename and a pixbuf
2107 * set, the pixbuf will take priority.
2111 gtk_icon_source_set_pixbuf (GtkIconSource *source,
2114 g_return_if_fail (source != NULL);
2117 g_object_ref (pixbuf);
2120 g_object_unref (source->pixbuf);
2122 source->pixbuf = pixbuf;
2126 * gtk_icon_source_get_filename:
2127 * @source: a #GtkIconSource
2129 * Retrieves the source filename, or %NULL if none is set. The
2130 * filename is not a copy, and should not be modified or expected to
2131 * persist beyond the lifetime of the icon source.
2133 * Return value: image filename. This string must not be modified
2136 G_CONST_RETURN gchar*
2137 gtk_icon_source_get_filename (const GtkIconSource *source)
2139 g_return_val_if_fail (source != NULL, NULL);
2141 return source->filename;
2145 * gtk_icon_source_get_pixbuf:
2146 * @source: a #GtkIconSource
2148 * Retrieves the source pixbuf, or %NULL if none is set.
2149 * The reference count on the pixbuf is not incremented.
2151 * Return value: source pixbuf
2154 gtk_icon_source_get_pixbuf (const GtkIconSource *source)
2156 g_return_val_if_fail (source != NULL, NULL);
2158 return source->pixbuf;
2162 * gtk_icon_source_set_direction_wildcarded:
2163 * @source: a #GtkIconSource
2164 * @setting: %TRUE to wildcard the text direction
2166 * If the text direction is wildcarded, this source can be used
2167 * as the base image for an icon in any #GtkTextDirection.
2168 * If the text direction is not wildcarded, then the
2169 * text direction the icon source applies to should be set
2170 * with gtk_icon_source_set_direction(), and the icon source
2171 * will only be used with that text direction.
2173 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2174 * wildcarded sources, and will use an exact match when possible.
2178 gtk_icon_source_set_direction_wildcarded (GtkIconSource *source,
2181 g_return_if_fail (source != NULL);
2183 source->any_direction = setting != FALSE;
2187 * gtk_icon_source_set_state_wildcarded:
2188 * @source: a #GtkIconSource
2189 * @setting: %TRUE to wildcard the widget state
2191 * If the widget state is wildcarded, this source can be used as the
2192 * base image for an icon in any #GtkStateType. If the widget state
2193 * is not wildcarded, then the state the source applies to should be
2194 * set with gtk_icon_source_set_state() and the icon source will
2195 * only be used with that specific state.
2197 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2198 * wildcarded sources, and will use an exact match when possible.
2200 * #GtkIconSet will normally transform wildcarded source images to
2201 * produce an appropriate icon for a given state, for example
2202 * lightening an image on prelight, but will not modify source images
2203 * that match exactly.
2206 gtk_icon_source_set_state_wildcarded (GtkIconSource *source,
2209 g_return_if_fail (source != NULL);
2211 source->any_state = setting != FALSE;
2216 * gtk_icon_source_set_size_wildcarded:
2217 * @source: a #GtkIconSource
2218 * @setting: %TRUE to wildcard the widget state
2220 * If the icon size is wildcarded, this source can be used as the base
2221 * image for an icon of any size. If the size is not wildcarded, then
2222 * the size the source applies to should be set with
2223 * gtk_icon_source_set_size() and the icon source will only be used
2224 * with that specific size.
2226 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2227 * wildcarded sources, and will use an exact match when possible.
2229 * #GtkIconSet will normally scale wildcarded source images to produce
2230 * an appropriate icon at a given size, but will not change the size
2231 * of source images that match exactly.
2234 gtk_icon_source_set_size_wildcarded (GtkIconSource *source,
2237 g_return_if_fail (source != NULL);
2239 source->any_size = setting != FALSE;
2243 * gtk_icon_source_get_size_wildcarded:
2244 * @source: a #GtkIconSource
2246 * Gets the value set by gtk_icon_source_set_size_wildcarded().
2248 * Return value: %TRUE if this icon source is a base for any icon size variant
2251 gtk_icon_source_get_size_wildcarded (const GtkIconSource *source)
2253 g_return_val_if_fail (source != NULL, TRUE);
2255 return source->any_size;
2259 * gtk_icon_source_get_state_wildcarded:
2260 * @source: a #GtkIconSource
2262 * Gets the value set by gtk_icon_source_set_state_wildcarded().
2264 * Return value: %TRUE if this icon source is a base for any widget state variant
2267 gtk_icon_source_get_state_wildcarded (const GtkIconSource *source)
2269 g_return_val_if_fail (source != NULL, TRUE);
2271 return source->any_state;
2275 * gtk_icon_source_get_direction_wildcarded:
2276 * @source: a #GtkIconSource
2278 * Gets the value set by gtk_icon_source_set_direction_wildcarded().
2280 * Return value: %TRUE if this icon source is a base for any text direction variant
2283 gtk_icon_source_get_direction_wildcarded (const GtkIconSource *source)
2285 g_return_val_if_fail (source != NULL, TRUE);
2287 return source->any_direction;
2291 * gtk_icon_source_set_direction:
2292 * @source: a #GtkIconSource
2293 * @direction: text direction this source applies to
2295 * Sets the text direction this icon source is intended to be used
2298 * Setting the text direction on an icon source makes no difference
2299 * if the text direction is wildcarded. Therefore, you should usually
2300 * call gtk_icon_source_set_direction_wildcarded() to un-wildcard it
2301 * in addition to calling this function.
2305 gtk_icon_source_set_direction (GtkIconSource *source,
2306 GtkTextDirection direction)
2308 g_return_if_fail (source != NULL);
2310 source->direction = direction;
2314 * gtk_icon_source_set_state:
2315 * @source: a #GtkIconSource
2316 * @state: widget state this source applies to
2318 * Sets the widget state this icon source is intended to be used
2321 * Setting the widget state on an icon source makes no difference
2322 * if the state is wildcarded. Therefore, you should usually
2323 * call gtk_icon_source_set_state_wildcarded() to un-wildcard it
2324 * in addition to calling this function.
2328 gtk_icon_source_set_state (GtkIconSource *source,
2331 g_return_if_fail (source != NULL);
2333 source->state = state;
2337 * gtk_icon_source_set_size:
2338 * @source: a #GtkIconSource
2339 * @size: icon size this source applies to
2341 * Sets the icon size this icon source is intended to be used
2344 * Setting the icon size on an icon source makes no difference
2345 * if the size is wildcarded. Therefore, you should usually
2346 * call gtk_icon_source_set_size_wildcarded() to un-wildcard it
2347 * in addition to calling this function.
2351 gtk_icon_source_set_size (GtkIconSource *source,
2354 g_return_if_fail (source != NULL);
2356 source->size = size;
2360 * gtk_icon_source_get_direction:
2361 * @source: a #GtkIconSource
2363 * Obtains the text direction this icon source applies to. The return
2364 * value is only useful/meaningful if the text direction is <emphasis>not</emphasis>
2367 * Return value: text direction this source matches
2370 gtk_icon_source_get_direction (const GtkIconSource *source)
2372 g_return_val_if_fail (source != NULL, 0);
2374 return source->direction;
2378 * gtk_icon_source_get_state:
2379 * @source: a #GtkIconSource
2381 * Obtains the widget state this icon source applies to. The return
2382 * value is only useful/meaningful if the widget state is <emphasis>not</emphasis>
2385 * Return value: widget state this source matches
2388 gtk_icon_source_get_state (const GtkIconSource *source)
2390 g_return_val_if_fail (source != NULL, 0);
2392 return source->state;
2396 * gtk_icon_source_get_size:
2397 * @source: a #GtkIconSource
2399 * Obtains the icon size this source applies to. The return value
2400 * is only useful/meaningful if the icon size is <emphasis>not</emphasis> wildcarded.
2402 * Return value: icon size this source matches.
2405 gtk_icon_source_get_size (const GtkIconSource *source)
2407 g_return_val_if_fail (source != NULL, 0);
2409 return source->size;
2412 /* Note that the logical maximum is 20 per GtkTextDirection, so we could
2413 * eventually set this to >20 to never throw anything out.
2415 #define NUM_CACHED_ICONS 8
2417 typedef struct _CachedIcon CachedIcon;
2421 /* These must all match to use the cached pixbuf.
2422 * If any don't match, we must re-render the pixbuf.
2425 GtkTextDirection direction;
2433 ensure_cache_up_to_date (GtkIconSet *icon_set)
2435 if (icon_set->cache_serial != cache_serial)
2436 clear_cache (icon_set, TRUE);
2440 cached_icon_free (CachedIcon *icon)
2442 g_object_unref (icon->pixbuf);
2448 find_in_cache (GtkIconSet *icon_set,
2450 GtkTextDirection direction,
2457 ensure_cache_up_to_date (icon_set);
2460 tmp_list = icon_set->cache;
2461 while (tmp_list != NULL)
2463 CachedIcon *icon = tmp_list->data;
2465 if (icon->style == style &&
2466 icon->direction == direction &&
2467 icon->state == state &&
2472 /* Move this icon to the front of the list. */
2473 prev->next = tmp_list->next;
2474 tmp_list->next = icon_set->cache;
2475 icon_set->cache = tmp_list;
2478 return icon->pixbuf;
2482 tmp_list = g_slist_next (tmp_list);
2489 add_to_cache (GtkIconSet *icon_set,
2491 GtkTextDirection direction,
2498 ensure_cache_up_to_date (icon_set);
2500 g_object_ref (pixbuf);
2502 /* We have to ref the style, since if the style was finalized
2503 * its address could be reused by another style, creating a
2508 g_object_ref (style);
2511 icon = g_new (CachedIcon, 1);
2512 icon_set->cache = g_slist_prepend (icon_set->cache, icon);
2514 icon->style = style;
2515 icon->direction = direction;
2516 icon->state = state;
2518 icon->pixbuf = pixbuf;
2521 attach_to_style (icon_set, icon->style);
2523 if (icon_set->cache_size >= NUM_CACHED_ICONS)
2525 /* Remove oldest item in the cache */
2529 tmp_list = icon_set->cache;
2531 /* Find next-to-last link */
2532 g_assert (NUM_CACHED_ICONS > 2);
2533 while (tmp_list->next->next)
2534 tmp_list = tmp_list->next;
2536 g_assert (tmp_list != NULL);
2537 g_assert (tmp_list->next != NULL);
2538 g_assert (tmp_list->next->next == NULL);
2540 /* Free the last icon */
2541 icon = tmp_list->next->data;
2543 g_slist_free (tmp_list->next);
2544 tmp_list->next = NULL;
2546 cached_icon_free (icon);
2551 clear_cache (GtkIconSet *icon_set,
2552 gboolean style_detach)
2555 GtkStyle *last_style = NULL;
2557 tmp_list = icon_set->cache;
2558 while (tmp_list != NULL)
2560 CachedIcon *icon = tmp_list->data;
2564 /* simple optimization for the case where the cache
2565 * contains contiguous icons from the same style.
2566 * it's safe to call detach_from_style more than
2567 * once on the same style though.
2569 if (last_style != icon->style)
2571 detach_from_style (icon_set, icon->style);
2572 last_style = icon->style;
2576 cached_icon_free (icon);
2578 tmp_list = g_slist_next (tmp_list);
2581 g_slist_free (icon_set->cache);
2582 icon_set->cache = NULL;
2583 icon_set->cache_size = 0;
2587 copy_cache (GtkIconSet *icon_set,
2588 GtkIconSet *copy_recipient)
2591 GSList *copy = NULL;
2593 ensure_cache_up_to_date (icon_set);
2595 tmp_list = icon_set->cache;
2596 while (tmp_list != NULL)
2598 CachedIcon *icon = tmp_list->data;
2599 CachedIcon *icon_copy = g_new (CachedIcon, 1);
2603 if (icon_copy->style)
2604 attach_to_style (copy_recipient, icon_copy->style);
2606 g_object_ref (icon_copy->pixbuf);
2608 icon_copy->size = icon->size;
2610 copy = g_slist_prepend (copy, icon_copy);
2612 tmp_list = g_slist_next (tmp_list);
2615 return g_slist_reverse (copy);
2619 attach_to_style (GtkIconSet *icon_set,
2624 table = g_object_get_qdata (G_OBJECT (style),
2625 g_quark_try_string ("gtk-style-icon-sets"));
2629 table = g_hash_table_new (NULL, NULL);
2630 g_object_set_qdata_full (G_OBJECT (style),
2631 g_quark_from_static_string ("gtk-style-icon-sets"),
2636 g_hash_table_insert (table, icon_set, icon_set);
2640 detach_from_style (GtkIconSet *icon_set,
2645 table = g_object_get_qdata (G_OBJECT (style),
2646 g_quark_try_string ("gtk-style-icon-sets"));
2649 g_hash_table_remove (table, icon_set);
2653 iconsets_foreach (gpointer key,
2657 GtkIconSet *icon_set = key;
2659 /* We only need to remove cache entries for the given style;
2660 * but that complicates things because in destroy notify
2661 * we don't know which style got destroyed, and 95% of the
2662 * time all cache entries will have the same style,
2663 * so this is faster anyway.
2666 clear_cache (icon_set, FALSE);
2670 style_dnotify (gpointer data)
2672 GHashTable *table = data;
2674 g_hash_table_foreach (table, iconsets_foreach, NULL);
2676 g_hash_table_destroy (table);
2679 /* This allows the icon set to detect that its cache is out of date. */
2681 _gtk_icon_set_invalidate_caches (void)
2687 listify_foreach (gpointer key, gpointer value, gpointer data)
2689 GSList **list = data;
2691 *list = g_slist_prepend (*list, key);
2695 g_hash_table_get_keys (GHashTable *table)
2697 GSList *list = NULL;
2699 g_hash_table_foreach (table, listify_foreach, &list);
2705 * _gtk_icon_factory_list_ids:
2707 * Gets all known IDs stored in an existing icon factory.
2708 * The strings in the returned list aren't copied.
2709 * The list itself should be freed.
2711 * Return value: List of ids in icon factories
2714 _gtk_icon_factory_list_ids (void)
2721 ensure_default_icons ();
2723 tmp_list = all_icon_factories;
2724 while (tmp_list != NULL)
2728 GtkIconFactory *factory = GTK_ICON_FACTORY (tmp_list->data);
2730 these_ids = g_hash_table_get_keys (factory->icons);
2732 ids = g_slist_concat (ids, these_ids);
2734 tmp_list = g_slist_next (tmp_list);