1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2000 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkiconfactory.h"
32 #include "gtkiconcache.h"
34 #include "gtkicontheme.h"
35 #include "gtksettings.h"
37 #include "gtkwidget.h"
39 #include "gtkbuildable.h"
40 #include "gtkbuilderprivate.h"
44 static GSList *all_icon_factories = NULL;
47 GTK_ICON_SOURCE_EMPTY,
48 GTK_ICON_SOURCE_ICON_NAME,
49 GTK_ICON_SOURCE_STATIC_ICON_NAME,
50 GTK_ICON_SOURCE_FILENAME,
51 GTK_ICON_SOURCE_PIXBUF
56 GtkIconSourceType type;
64 GdkPixbuf *filename_pixbuf;
66 GtkTextDirection direction;
70 /* If TRUE, then the parameter is wildcarded, and the above
71 * fields should be ignored. If FALSE, the parameter is
72 * specified, and the above fields should be valid.
74 guint any_direction : 1;
78 #if defined (G_OS_WIN32) && !defined (_WIN64)
79 /* System codepage version of filename, for DLL ABI backward
80 * compatibility functions.
88 gtk_icon_factory_buildable_init (GtkBuildableIface *iface);
90 static gboolean gtk_icon_factory_buildable_custom_tag_start (GtkBuildable *buildable,
94 GMarkupParser *parser,
96 static void gtk_icon_factory_buildable_custom_tag_end (GtkBuildable *buildable,
100 gpointer *user_data);
101 static void gtk_icon_factory_finalize (GObject *object);
102 static void get_default_icons (GtkIconFactory *icon_factory);
103 static void icon_source_clear (GtkIconSource *source);
105 static GtkIconSize icon_size_register_intern (const gchar *name,
109 #define GTK_ICON_SOURCE_INIT(any_direction, any_state, any_size) \
110 { GTK_ICON_SOURCE_EMPTY, { NULL }, NULL, \
112 any_direction, any_state, any_size }
114 G_DEFINE_TYPE_WITH_CODE (GtkIconFactory, gtk_icon_factory, G_TYPE_OBJECT,
115 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
116 gtk_icon_factory_buildable_init))
119 gtk_icon_factory_init (GtkIconFactory *factory)
121 factory->icons = g_hash_table_new (g_str_hash, g_str_equal);
122 all_icon_factories = g_slist_prepend (all_icon_factories, factory);
126 gtk_icon_factory_class_init (GtkIconFactoryClass *klass)
128 GObjectClass *object_class = G_OBJECT_CLASS (klass);
130 object_class->finalize = gtk_icon_factory_finalize;
134 gtk_icon_factory_buildable_init (GtkBuildableIface *iface)
136 iface->custom_tag_start = gtk_icon_factory_buildable_custom_tag_start;
137 iface->custom_tag_end = gtk_icon_factory_buildable_custom_tag_end;
141 free_icon_set (gpointer key, gpointer value, gpointer data)
144 gtk_icon_set_unref (value);
148 gtk_icon_factory_finalize (GObject *object)
150 GtkIconFactory *factory = GTK_ICON_FACTORY (object);
152 all_icon_factories = g_slist_remove (all_icon_factories, factory);
154 g_hash_table_foreach (factory->icons, free_icon_set, NULL);
156 g_hash_table_destroy (factory->icons);
158 G_OBJECT_CLASS (gtk_icon_factory_parent_class)->finalize (object);
162 * gtk_icon_factory_new:
164 * Creates a new #GtkIconFactory. An icon factory manages a collection
165 * of #GtkIconSet<!-- -->s; a #GtkIconSet manages a set of variants of a
166 * particular icon (i.e. a #GtkIconSet contains variants for different
167 * sizes and widget states). Icons in an icon factory are named by a
168 * stock ID, which is a simple string identifying the icon. Each
169 * #GtkStyle has a list of #GtkIconFactory<!-- -->s derived from the current
170 * theme; those icon factories are consulted first when searching for
171 * an icon. If the theme doesn't set a particular icon, GTK+ looks for
172 * the icon in a list of default icon factories, maintained by
173 * gtk_icon_factory_add_default() and
174 * gtk_icon_factory_remove_default(). Applications with icons should
175 * add a default icon factory with their icons, which will allow
176 * themes to override the icons for the application.
178 * Return value: a new #GtkIconFactory
181 gtk_icon_factory_new (void)
183 return g_object_new (GTK_TYPE_ICON_FACTORY, NULL);
187 * gtk_icon_factory_add:
188 * @factory: a #GtkIconFactory
189 * @stock_id: icon name
190 * @icon_set: icon set
192 * Adds the given @icon_set to the icon factory, under the name
193 * @stock_id. @stock_id should be namespaced for your application,
194 * e.g. "myapp-whatever-icon". Normally applications create a
195 * #GtkIconFactory, then add it to the list of default factories with
196 * gtk_icon_factory_add_default(). Then they pass the @stock_id to
197 * widgets such as #GtkImage to display the icon. Themes can provide
198 * an icon with the same name (such as "myapp-whatever-icon") to
199 * override your application's default icons. If an icon already
200 * existed in @factory for @stock_id, it is unreferenced and replaced
201 * with the new @icon_set.
204 gtk_icon_factory_add (GtkIconFactory *factory,
205 const gchar *stock_id,
206 GtkIconSet *icon_set)
208 gpointer old_key = NULL;
209 gpointer old_value = NULL;
211 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
212 g_return_if_fail (stock_id != NULL);
213 g_return_if_fail (icon_set != NULL);
215 g_hash_table_lookup_extended (factory->icons, stock_id,
216 &old_key, &old_value);
218 if (old_value == icon_set)
221 gtk_icon_set_ref (icon_set);
223 /* GHashTable key memory management is so fantastically broken. */
225 g_hash_table_insert (factory->icons, old_key, icon_set);
227 g_hash_table_insert (factory->icons, g_strdup (stock_id), icon_set);
230 gtk_icon_set_unref (old_value);
234 * gtk_icon_factory_lookup:
235 * @factory: a #GtkIconFactory
236 * @stock_id: an icon name
238 * Looks up @stock_id in the icon factory, returning an icon set
239 * if found, otherwise %NULL. For display to the user, you should
240 * use gtk_style_lookup_icon_set() on the #GtkStyle for the
241 * widget that will display the icon, instead of using this
242 * function directly, so that themes are taken into account.
244 * Return value: icon set of @stock_id.
247 gtk_icon_factory_lookup (GtkIconFactory *factory,
248 const gchar *stock_id)
250 g_return_val_if_fail (GTK_IS_ICON_FACTORY (factory), NULL);
251 g_return_val_if_fail (stock_id != NULL, NULL);
253 return g_hash_table_lookup (factory->icons, stock_id);
256 static GtkIconFactory *gtk_default_icons = NULL;
257 static GSList *default_factories = NULL;
260 * gtk_icon_factory_add_default:
261 * @factory: a #GtkIconFactory
263 * Adds an icon factory to the list of icon factories searched by
264 * gtk_style_lookup_icon_set(). This means that, for example,
265 * gtk_image_new_from_stock() will be able to find icons in @factory.
266 * There will normally be an icon factory added for each library or
267 * application that comes with icons. The default icon factories
268 * can be overridden by themes.
271 gtk_icon_factory_add_default (GtkIconFactory *factory)
273 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
275 g_object_ref (factory);
277 default_factories = g_slist_prepend (default_factories, factory);
281 * gtk_icon_factory_remove_default:
282 * @factory: a #GtkIconFactory previously added with gtk_icon_factory_add_default()
284 * Removes an icon factory from the list of default icon
285 * factories. Not normally used; you might use it for a library that
286 * can be unloaded or shut down.
289 gtk_icon_factory_remove_default (GtkIconFactory *factory)
291 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
293 default_factories = g_slist_remove (default_factories, factory);
295 g_object_unref (factory);
299 _gtk_icon_factory_ensure_default_icons (void)
301 if (gtk_default_icons == NULL)
303 gtk_default_icons = gtk_icon_factory_new ();
305 get_default_icons (gtk_default_icons);
310 * gtk_icon_factory_lookup_default:
311 * @stock_id: an icon name
313 * Looks for an icon in the list of default icon factories. For
314 * display to the user, you should use gtk_style_lookup_icon_set() on
315 * the #GtkStyle for the widget that will display the icon, instead of
316 * using this function directly, so that themes are taken into
319 * Return value: a #GtkIconSet, or %NULL
322 gtk_icon_factory_lookup_default (const gchar *stock_id)
326 g_return_val_if_fail (stock_id != NULL, NULL);
328 tmp_list = default_factories;
329 while (tmp_list != NULL)
331 GtkIconSet *icon_set =
332 gtk_icon_factory_lookup (GTK_ICON_FACTORY (tmp_list->data),
338 tmp_list = g_slist_next (tmp_list);
341 _gtk_icon_factory_ensure_default_icons ();
343 return gtk_icon_factory_lookup (gtk_default_icons, stock_id);
347 register_stock_icon (GtkIconFactory *factory,
348 const gchar *stock_id)
350 GtkIconSet *set = gtk_icon_set_new ();
351 GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
353 source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
354 source.source.icon_name = (gchar *)stock_id;
355 gtk_icon_set_add_source (set, &source);
357 gtk_icon_factory_add (factory, stock_id, set);
358 gtk_icon_set_unref (set);
362 register_bidi_stock_icon (GtkIconFactory *factory,
363 const gchar *stock_id,
364 const gchar *stock_id_ltr,
365 const gchar *stock_id_rtl)
367 GtkIconSet *set = gtk_icon_set_new ();
368 GtkIconSource source = GTK_ICON_SOURCE_INIT (FALSE, TRUE, TRUE);
370 source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
371 source.source.icon_name = (gchar *)stock_id_ltr;
372 source.direction = GTK_TEXT_DIR_LTR;
373 gtk_icon_set_add_source (set, &source);
375 source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
376 source.source.icon_name = (gchar *)stock_id_rtl;
377 source.direction = GTK_TEXT_DIR_RTL;
378 gtk_icon_set_add_source (set, &source);
380 gtk_icon_factory_add (factory, stock_id, set);
381 gtk_icon_set_unref (set);
385 get_default_icons (GtkIconFactory *factory)
387 /* KEEP IN SYNC with gtkstock.c */
389 register_stock_icon (factory, GTK_STOCK_DIALOG_AUTHENTICATION);
390 register_stock_icon (factory, GTK_STOCK_DIALOG_ERROR);
391 register_stock_icon (factory, GTK_STOCK_DIALOG_INFO);
392 register_stock_icon (factory, GTK_STOCK_DIALOG_QUESTION);
393 register_stock_icon (factory, GTK_STOCK_DIALOG_WARNING);
394 register_stock_icon (factory, GTK_STOCK_DND);
395 register_stock_icon (factory, GTK_STOCK_DND_MULTIPLE);
396 register_stock_icon (factory, GTK_STOCK_APPLY);
397 register_stock_icon (factory, GTK_STOCK_CANCEL);
398 register_stock_icon (factory, GTK_STOCK_NO);
399 register_stock_icon (factory, GTK_STOCK_OK);
400 register_stock_icon (factory, GTK_STOCK_YES);
401 register_stock_icon (factory, GTK_STOCK_CLOSE);
402 register_stock_icon (factory, GTK_STOCK_ADD);
403 register_stock_icon (factory, GTK_STOCK_JUSTIFY_CENTER);
404 register_stock_icon (factory, GTK_STOCK_JUSTIFY_FILL);
405 register_stock_icon (factory, GTK_STOCK_JUSTIFY_LEFT);
406 register_stock_icon (factory, GTK_STOCK_JUSTIFY_RIGHT);
407 register_stock_icon (factory, GTK_STOCK_GOTO_BOTTOM);
408 register_stock_icon (factory, GTK_STOCK_CDROM);
409 register_stock_icon (factory, GTK_STOCK_CONVERT);
410 register_stock_icon (factory, GTK_STOCK_COPY);
411 register_stock_icon (factory, GTK_STOCK_CUT);
412 register_stock_icon (factory, GTK_STOCK_GO_DOWN);
413 register_stock_icon (factory, GTK_STOCK_EXECUTE);
414 register_stock_icon (factory, GTK_STOCK_QUIT);
415 register_bidi_stock_icon (factory,
416 GTK_STOCK_GOTO_FIRST,
417 GTK_STOCK_GOTO_FIRST "-ltr",
418 GTK_STOCK_GOTO_FIRST "-rtl");
419 register_stock_icon (factory, GTK_STOCK_SELECT_FONT);
420 register_stock_icon (factory, GTK_STOCK_FULLSCREEN);
421 register_stock_icon (factory, GTK_STOCK_LEAVE_FULLSCREEN);
422 register_stock_icon (factory, GTK_STOCK_HARDDISK);
423 register_stock_icon (factory, GTK_STOCK_HELP);
424 register_stock_icon (factory, GTK_STOCK_HOME);
425 register_stock_icon (factory, GTK_STOCK_INFO);
426 register_bidi_stock_icon (factory,
428 GTK_STOCK_JUMP_TO "-ltr",
429 GTK_STOCK_JUMP_TO "-rtl");
430 register_bidi_stock_icon (factory,
432 GTK_STOCK_GOTO_LAST "-ltr",
433 GTK_STOCK_GOTO_LAST "-rtl");
434 register_bidi_stock_icon (factory,
436 GTK_STOCK_GO_BACK "-ltr",
437 GTK_STOCK_GO_BACK "-rtl");
438 register_stock_icon (factory, GTK_STOCK_MISSING_IMAGE);
439 register_stock_icon (factory, GTK_STOCK_NETWORK);
440 register_stock_icon (factory, GTK_STOCK_NEW);
441 register_stock_icon (factory, GTK_STOCK_OPEN);
442 register_stock_icon (factory, GTK_STOCK_ORIENTATION_PORTRAIT);
443 register_stock_icon (factory, GTK_STOCK_ORIENTATION_LANDSCAPE);
444 register_stock_icon (factory, GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT);
445 register_stock_icon (factory, GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE);
446 register_stock_icon (factory, GTK_STOCK_PAGE_SETUP);
447 register_stock_icon (factory, GTK_STOCK_PASTE);
448 register_stock_icon (factory, GTK_STOCK_PREFERENCES);
449 register_stock_icon (factory, GTK_STOCK_PRINT);
450 register_stock_icon (factory, GTK_STOCK_PRINT_ERROR);
451 register_stock_icon (factory, GTK_STOCK_PRINT_PAUSED);
452 register_stock_icon (factory, GTK_STOCK_PRINT_PREVIEW);
453 register_stock_icon (factory, GTK_STOCK_PRINT_REPORT);
454 register_stock_icon (factory, GTK_STOCK_PRINT_WARNING);
455 register_stock_icon (factory, GTK_STOCK_PROPERTIES);
456 register_bidi_stock_icon (factory,
458 GTK_STOCK_REDO "-ltr",
459 GTK_STOCK_REDO "-rtl");
460 register_stock_icon (factory, GTK_STOCK_REMOVE);
461 register_stock_icon (factory, GTK_STOCK_REFRESH);
462 register_bidi_stock_icon (factory,
463 GTK_STOCK_REVERT_TO_SAVED,
464 GTK_STOCK_REVERT_TO_SAVED "-ltr",
465 GTK_STOCK_REVERT_TO_SAVED "-rtl");
466 register_bidi_stock_icon (factory,
467 GTK_STOCK_GO_FORWARD,
468 GTK_STOCK_GO_FORWARD "-ltr",
469 GTK_STOCK_GO_FORWARD "-rtl");
470 register_stock_icon (factory, GTK_STOCK_SAVE);
471 register_stock_icon (factory, GTK_STOCK_FLOPPY);
472 register_stock_icon (factory, GTK_STOCK_SAVE_AS);
473 register_stock_icon (factory, GTK_STOCK_FIND);
474 register_stock_icon (factory, GTK_STOCK_FIND_AND_REPLACE);
475 register_stock_icon (factory, GTK_STOCK_SORT_DESCENDING);
476 register_stock_icon (factory, GTK_STOCK_SORT_ASCENDING);
477 register_stock_icon (factory, GTK_STOCK_SPELL_CHECK);
478 register_stock_icon (factory, GTK_STOCK_STOP);
479 register_stock_icon (factory, GTK_STOCK_BOLD);
480 register_stock_icon (factory, GTK_STOCK_ITALIC);
481 register_stock_icon (factory, GTK_STOCK_STRIKETHROUGH);
482 register_stock_icon (factory, GTK_STOCK_UNDERLINE);
483 register_bidi_stock_icon (factory,
485 GTK_STOCK_INDENT "-ltr",
486 GTK_STOCK_INDENT "-rtl");
487 register_bidi_stock_icon (factory,
489 GTK_STOCK_UNINDENT "-ltr",
490 GTK_STOCK_UNINDENT "-rtl");
491 register_stock_icon (factory, GTK_STOCK_GOTO_TOP);
492 register_stock_icon (factory, GTK_STOCK_DELETE);
493 register_bidi_stock_icon (factory,
495 GTK_STOCK_UNDELETE "-ltr",
496 GTK_STOCK_UNDELETE "-rtl");
497 register_bidi_stock_icon (factory,
499 GTK_STOCK_UNDO "-ltr",
500 GTK_STOCK_UNDO "-rtl");
501 register_stock_icon (factory, GTK_STOCK_GO_UP);
502 register_stock_icon (factory, GTK_STOCK_FILE);
503 register_stock_icon (factory, GTK_STOCK_DIRECTORY);
504 register_stock_icon (factory, GTK_STOCK_ABOUT);
505 register_stock_icon (factory, GTK_STOCK_CONNECT);
506 register_stock_icon (factory, GTK_STOCK_DISCONNECT);
507 register_stock_icon (factory, GTK_STOCK_EDIT);
508 register_stock_icon (factory, GTK_STOCK_CAPS_LOCK_WARNING);
509 register_bidi_stock_icon (factory,
510 GTK_STOCK_MEDIA_FORWARD,
511 GTK_STOCK_MEDIA_FORWARD "-ltr",
512 GTK_STOCK_MEDIA_FORWARD "-rtl");
513 register_bidi_stock_icon (factory,
514 GTK_STOCK_MEDIA_NEXT,
515 GTK_STOCK_MEDIA_NEXT "-ltr",
516 GTK_STOCK_MEDIA_NEXT "-rtl");
517 register_stock_icon (factory, GTK_STOCK_MEDIA_PAUSE);
518 register_bidi_stock_icon (factory,
519 GTK_STOCK_MEDIA_PLAY,
520 GTK_STOCK_MEDIA_PLAY "-ltr",
521 GTK_STOCK_MEDIA_PLAY "-rtl");
522 register_bidi_stock_icon (factory,
523 GTK_STOCK_MEDIA_PREVIOUS,
524 GTK_STOCK_MEDIA_PREVIOUS "-ltr",
525 GTK_STOCK_MEDIA_PREVIOUS "-rtl");
526 register_stock_icon (factory, GTK_STOCK_MEDIA_RECORD);
527 register_bidi_stock_icon (factory,
528 GTK_STOCK_MEDIA_REWIND,
529 GTK_STOCK_MEDIA_REWIND "-ltr",
530 GTK_STOCK_MEDIA_REWIND "-rtl");
531 register_stock_icon (factory, GTK_STOCK_MEDIA_STOP);
532 register_stock_icon (factory, GTK_STOCK_INDEX);
533 register_stock_icon (factory, GTK_STOCK_ZOOM_100);
534 register_stock_icon (factory, GTK_STOCK_ZOOM_IN);
535 register_stock_icon (factory, GTK_STOCK_ZOOM_OUT);
536 register_stock_icon (factory, GTK_STOCK_ZOOM_FIT);
537 register_stock_icon (factory, GTK_STOCK_SELECT_ALL);
538 register_stock_icon (factory, GTK_STOCK_CLEAR);
539 register_stock_icon (factory, GTK_STOCK_SELECT_COLOR);
540 register_stock_icon (factory, GTK_STOCK_COLOR_PICKER);
543 /************************************************************
544 * Icon size handling *
545 ************************************************************/
547 typedef struct _IconSize IconSize;
558 typedef struct _IconAlias IconAlias;
566 typedef struct _SettingsIconSize SettingsIconSize;
568 struct _SettingsIconSize
574 static GHashTable *icon_aliases = NULL;
575 static IconSize *icon_sizes = NULL;
576 static gint icon_sizes_allocated = 0;
577 static gint icon_sizes_used = 0;
580 init_icon_sizes (void)
582 if (icon_sizes == NULL)
584 #define NUM_BUILTIN_SIZES 7
587 icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
589 icon_sizes = g_new (IconSize, NUM_BUILTIN_SIZES);
590 icon_sizes_allocated = NUM_BUILTIN_SIZES;
591 icon_sizes_used = NUM_BUILTIN_SIZES;
593 icon_sizes[GTK_ICON_SIZE_INVALID].size = 0;
594 icon_sizes[GTK_ICON_SIZE_INVALID].name = NULL;
595 icon_sizes[GTK_ICON_SIZE_INVALID].width = 0;
596 icon_sizes[GTK_ICON_SIZE_INVALID].height = 0;
598 /* the name strings aren't copied since we don't ever remove
599 * icon sizes, so we don't need to know whether they're static.
600 * Even if we did I suppose removing the builtin sizes would be
604 icon_sizes[GTK_ICON_SIZE_MENU].size = GTK_ICON_SIZE_MENU;
605 icon_sizes[GTK_ICON_SIZE_MENU].name = "gtk-menu";
606 icon_sizes[GTK_ICON_SIZE_MENU].width = 16;
607 icon_sizes[GTK_ICON_SIZE_MENU].height = 16;
609 icon_sizes[GTK_ICON_SIZE_BUTTON].size = GTK_ICON_SIZE_BUTTON;
610 icon_sizes[GTK_ICON_SIZE_BUTTON].name = "gtk-button";
611 icon_sizes[GTK_ICON_SIZE_BUTTON].width = 20;
612 icon_sizes[GTK_ICON_SIZE_BUTTON].height = 20;
614 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].size = GTK_ICON_SIZE_SMALL_TOOLBAR;
615 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].name = "gtk-small-toolbar";
616 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].width = 18;
617 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].height = 18;
619 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].size = GTK_ICON_SIZE_LARGE_TOOLBAR;
620 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].name = "gtk-large-toolbar";
621 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].width = 24;
622 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].height = 24;
624 icon_sizes[GTK_ICON_SIZE_DND].size = GTK_ICON_SIZE_DND;
625 icon_sizes[GTK_ICON_SIZE_DND].name = "gtk-dnd";
626 icon_sizes[GTK_ICON_SIZE_DND].width = 32;
627 icon_sizes[GTK_ICON_SIZE_DND].height = 32;
629 icon_sizes[GTK_ICON_SIZE_DIALOG].size = GTK_ICON_SIZE_DIALOG;
630 icon_sizes[GTK_ICON_SIZE_DIALOG].name = "gtk-dialog";
631 icon_sizes[GTK_ICON_SIZE_DIALOG].width = 48;
632 icon_sizes[GTK_ICON_SIZE_DIALOG].height = 48;
634 g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
636 /* Alias everything to itself. */
637 i = 1; /* skip invalid size */
638 while (i < NUM_BUILTIN_SIZES)
640 gtk_icon_size_register_alias (icon_sizes[i].name, icon_sizes[i].size);
645 #undef NUM_BUILTIN_SIZES
650 free_settings_sizes (gpointer data)
652 g_array_free (data, TRUE);
656 get_settings_sizes (GtkSettings *settings,
659 GArray *settings_sizes;
660 static GQuark sizes_quark = 0;
663 sizes_quark = g_quark_from_static_string ("gtk-icon-sizes");
665 settings_sizes = g_object_get_qdata (G_OBJECT (settings), sizes_quark);
668 settings_sizes = g_array_new (FALSE, FALSE, sizeof (SettingsIconSize));
669 g_object_set_qdata_full (G_OBJECT (settings), sizes_quark,
670 settings_sizes, free_settings_sizes);
675 return settings_sizes;
679 icon_size_set_for_settings (GtkSettings *settings,
680 const gchar *size_name,
685 GArray *settings_sizes;
686 SettingsIconSize *settings_size;
688 g_return_if_fail (size_name != NULL);
690 size = gtk_icon_size_from_name (size_name);
691 if (size == GTK_ICON_SIZE_INVALID)
692 /* Reserve a place */
693 size = icon_size_register_intern (size_name, -1, -1);
695 settings_sizes = get_settings_sizes (settings, NULL);
696 if (size >= settings_sizes->len)
698 SettingsIconSize unset = { -1, -1 };
701 for (i = settings_sizes->len; i <= size; i++)
702 g_array_append_val (settings_sizes, unset);
705 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
707 settings_size->width = width;
708 settings_size->height = height;
711 /* Like pango_parse_word, but accept - as well
714 scan_icon_size_name (const char **pos, GString *out)
716 const char *p = *pos;
718 while (g_ascii_isspace (*p))
721 if (!((*p >= 'A' && *p <= 'Z') ||
722 (*p >= 'a' && *p <= 'z') ||
723 *p == '_' || *p == '-'))
726 g_string_truncate (out, 0);
727 g_string_append_c (out, *p);
730 while ((*p >= 'A' && *p <= 'Z') ||
731 (*p >= 'a' && *p <= 'z') ||
732 (*p >= '0' && *p <= '9') ||
733 *p == '_' || *p == '-')
735 g_string_append_c (out, *p);
745 icon_size_setting_parse (GtkSettings *settings,
746 const gchar *icon_size_string)
748 GString *name_buf = g_string_new (NULL);
749 const gchar *p = icon_size_string;
751 while (pango_skip_space (&p))
755 if (!scan_icon_size_name (&p, name_buf))
758 if (!pango_skip_space (&p))
766 if (!pango_scan_int (&p, &width))
769 if (!pango_skip_space (&p))
777 if (!pango_scan_int (&p, &height))
780 if (width > 0 && height > 0)
782 icon_size_set_for_settings (settings, name_buf->str,
787 g_warning ("Invalid size in gtk-icon-sizes: %d,%d\n", width, height);
790 pango_skip_space (&p);
799 g_string_free (name_buf, TRUE);
803 g_warning ("Error parsing gtk-icon-sizes string:\n\t'%s'", icon_size_string);
804 g_string_free (name_buf, TRUE);
808 icon_size_set_all_from_settings (GtkSettings *settings)
810 GArray *settings_sizes;
811 gchar *icon_size_string;
813 /* Reset old settings */
814 settings_sizes = get_settings_sizes (settings, NULL);
815 g_array_set_size (settings_sizes, 0);
817 g_object_get (settings,
818 "gtk-icon-sizes", &icon_size_string,
821 if (icon_size_string)
823 icon_size_setting_parse (settings, icon_size_string);
824 g_free (icon_size_string);
829 icon_size_settings_changed (GtkSettings *settings,
832 icon_size_set_all_from_settings (settings);
834 gtk_rc_reset_styles (settings);
838 icon_sizes_init_for_settings (GtkSettings *settings)
840 g_signal_connect (settings,
841 "notify::gtk-icon-sizes",
842 G_CALLBACK (icon_size_settings_changed),
845 icon_size_set_all_from_settings (settings);
849 icon_size_lookup_intern (GtkSettings *settings,
854 GArray *settings_sizes;
855 gint width_for_settings = -1;
856 gint height_for_settings = -1;
860 if (size == (GtkIconSize)-1)
863 if (size >= icon_sizes_used)
866 if (size == GTK_ICON_SIZE_INVALID)
871 gboolean initial = FALSE;
873 settings_sizes = get_settings_sizes (settings, &initial);
876 icon_sizes_init_for_settings (settings);
878 if (size < settings_sizes->len)
880 SettingsIconSize *settings_size;
882 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
884 width_for_settings = settings_size->width;
885 height_for_settings = settings_size->height;
890 *widthp = width_for_settings >= 0 ? width_for_settings : icon_sizes[size].width;
893 *heightp = height_for_settings >= 0 ? height_for_settings : icon_sizes[size].height;
899 * gtk_icon_size_lookup_for_settings:
900 * @settings: a #GtkSettings object, used to determine
901 * which set of user preferences to used.
902 * @size: (type int): an icon size
903 * @width: location to store icon width
904 * @height: location to store icon height
906 * Obtains the pixel size of a semantic icon size, possibly
907 * modified by user preferences for a particular
908 * #GtkSettings. Normally @size would be
909 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
910 * isn't normally needed, gtk_widget_render_icon() is the usual
911 * way to get an icon for rendering, then just look at the size of
912 * the rendered pixbuf. The rendered pixbuf may not even correspond to
913 * the width/height returned by gtk_icon_size_lookup(), because themes
914 * are free to render the pixbuf however they like, including changing
917 * Return value: %TRUE if @size was a valid size
922 gtk_icon_size_lookup_for_settings (GtkSettings *settings,
927 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
929 return icon_size_lookup_intern (settings, size, width, height);
933 * gtk_icon_size_lookup:
934 * @size: (type int): an icon size
935 * @width: location to store icon width
936 * @height: location to store icon height
938 * Obtains the pixel size of a semantic icon size, possibly
939 * modified by user preferences for the default #GtkSettings.
940 * (See gtk_icon_size_lookup_for_settings().)
941 * Normally @size would be
942 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
943 * isn't normally needed, gtk_widget_render_icon() is the usual
944 * way to get an icon for rendering, then just look at the size of
945 * the rendered pixbuf. The rendered pixbuf may not even correspond to
946 * the width/height returned by gtk_icon_size_lookup(), because themes
947 * are free to render the pixbuf however they like, including changing
950 * Return value: %TRUE if @size was a valid size
953 gtk_icon_size_lookup (GtkIconSize size,
958 g_warning ("gtk_icon_size_lookup ()) is not multihead safe"));
960 return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (),
961 size, widthp, heightp);
965 icon_size_register_intern (const gchar *name,
969 IconAlias *old_alias;
974 old_alias = g_hash_table_lookup (icon_aliases, name);
975 if (old_alias && icon_sizes[old_alias->target].width > 0)
977 g_warning ("Icon size name '%s' already exists", name);
978 return GTK_ICON_SIZE_INVALID;
983 size = old_alias->target;
987 if (icon_sizes_used == icon_sizes_allocated)
989 icon_sizes_allocated *= 2;
990 icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated);
993 size = icon_sizes_used++;
996 gtk_icon_size_register_alias (name, size);
998 icon_sizes[size].size = size;
999 icon_sizes[size].name = g_strdup (name);
1002 icon_sizes[size].width = width;
1003 icon_sizes[size].height = height;
1009 * gtk_icon_size_register:
1010 * @name: name of the icon size
1011 * @width: the icon width
1012 * @height: the icon height
1014 * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
1015 * etc. Returns the integer value for the size.
1017 * Returns: (type int): integer value representing the size
1020 gtk_icon_size_register (const gchar *name,
1024 g_return_val_if_fail (name != NULL, 0);
1025 g_return_val_if_fail (width > 0, 0);
1026 g_return_val_if_fail (height > 0, 0);
1028 return icon_size_register_intern (name, width, height);
1032 * gtk_icon_size_register_alias:
1033 * @alias: an alias for @target
1034 * @target: (type int): an existing icon size
1036 * Registers @alias as another name for @target.
1037 * So calling gtk_icon_size_from_name() with @alias as argument
1038 * will return @target.
1041 gtk_icon_size_register_alias (const gchar *alias,
1046 g_return_if_fail (alias != NULL);
1050 if (!icon_size_lookup_intern (NULL, target, NULL, NULL))
1051 g_warning ("gtk_icon_size_register_alias: Icon size %u does not exist", target);
1053 ia = g_hash_table_lookup (icon_aliases, alias);
1056 if (icon_sizes[ia->target].width > 0)
1058 g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias);
1062 ia->target = target;
1067 ia = g_new (IconAlias, 1);
1068 ia->name = g_strdup (alias);
1069 ia->target = target;
1071 g_hash_table_insert (icon_aliases, ia->name, ia);
1076 * gtk_icon_size_from_name:
1077 * @name: the name to look up.
1079 * Looks up the icon size associated with @name.
1081 * Return value: (type int): the icon size
1084 gtk_icon_size_from_name (const gchar *name)
1090 ia = g_hash_table_lookup (icon_aliases, name);
1092 if (ia && icon_sizes[ia->target].width > 0)
1095 return GTK_ICON_SIZE_INVALID;
1099 * gtk_icon_size_get_name:
1100 * @size: (type int): a #GtkIconSize.
1101 * @returns: the name of the given icon size.
1103 * Gets the canonical name of the given icon size. The returned string
1104 * is statically allocated and should not be freed.
1106 G_CONST_RETURN gchar*
1107 gtk_icon_size_get_name (GtkIconSize size)
1109 if (size >= icon_sizes_used)
1112 return icon_sizes[size].name;
1115 /************************************************************/
1120 static GdkPixbuf *find_in_cache (GtkIconSet *icon_set,
1122 GtkTextDirection direction,
1125 static void add_to_cache (GtkIconSet *icon_set,
1127 GtkTextDirection direction,
1131 /* Clear icon set contents, drop references to all contained
1132 * GdkPixbuf objects and forget all GtkIconSources. Used to
1133 * recycle an icon set.
1135 static void clear_cache (GtkIconSet *icon_set,
1136 gboolean style_detach);
1137 static GSList* copy_cache (GtkIconSet *icon_set,
1138 GtkIconSet *copy_recipient);
1139 static void attach_to_style (GtkIconSet *icon_set,
1141 static void detach_from_style (GtkIconSet *icon_set,
1143 static void style_dnotify (gpointer data);
1151 /* Cache of the last few rendered versions of the icon. */
1159 static guint cache_serial = 0;
1164 * Creates a new #GtkIconSet. A #GtkIconSet represents a single icon
1165 * in various sizes and widget states. It can provide a #GdkPixbuf
1166 * for a given size and state on request, and automatically caches
1167 * some of the rendered #GdkPixbuf objects.
1169 * Normally you would use gtk_widget_render_icon() instead of
1170 * using #GtkIconSet directly. The one case where you'd use
1171 * #GtkIconSet is to create application-specific icon sets to place in
1172 * a #GtkIconFactory.
1174 * Return value: a new #GtkIconSet
1177 gtk_icon_set_new (void)
1179 GtkIconSet *icon_set;
1181 icon_set = g_new (GtkIconSet, 1);
1183 icon_set->ref_count = 1;
1184 icon_set->sources = NULL;
1185 icon_set->cache = NULL;
1186 icon_set->cache_size = 0;
1187 icon_set->cache_serial = cache_serial;
1193 * gtk_icon_set_new_from_pixbuf:
1194 * @pixbuf: a #GdkPixbuf
1196 * Creates a new #GtkIconSet with @pixbuf as the default/fallback
1197 * source image. If you don't add any additional #GtkIconSource to the
1198 * icon set, all variants of the icon will be created from @pixbuf,
1199 * using scaling, pixelation, etc. as required to adjust the icon size
1200 * or make the icon look insensitive/prelighted.
1202 * Return value: a new #GtkIconSet
1205 gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
1209 GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1211 g_return_val_if_fail (pixbuf != NULL, NULL);
1213 set = gtk_icon_set_new ();
1215 gtk_icon_source_set_pixbuf (&source, pixbuf);
1216 gtk_icon_set_add_source (set, &source);
1217 gtk_icon_source_set_pixbuf (&source, NULL);
1225 * @icon_set: a #GtkIconSet.
1227 * Increments the reference count on @icon_set.
1229 * Return value: @icon_set.
1232 gtk_icon_set_ref (GtkIconSet *icon_set)
1234 g_return_val_if_fail (icon_set != NULL, NULL);
1235 g_return_val_if_fail (icon_set->ref_count > 0, NULL);
1237 icon_set->ref_count += 1;
1243 * gtk_icon_set_unref:
1244 * @icon_set: a #GtkIconSet
1246 * Decrements the reference count on @icon_set, and frees memory
1247 * if the reference count reaches 0.
1250 gtk_icon_set_unref (GtkIconSet *icon_set)
1252 g_return_if_fail (icon_set != NULL);
1253 g_return_if_fail (icon_set->ref_count > 0);
1255 icon_set->ref_count -= 1;
1257 if (icon_set->ref_count == 0)
1259 GSList *tmp_list = icon_set->sources;
1260 while (tmp_list != NULL)
1262 gtk_icon_source_free (tmp_list->data);
1264 tmp_list = g_slist_next (tmp_list);
1266 g_slist_free (icon_set->sources);
1268 clear_cache (icon_set, TRUE);
1275 gtk_icon_set_get_type (void)
1277 static GType our_type = 0;
1280 our_type = g_boxed_type_register_static (I_("GtkIconSet"),
1281 (GBoxedCopyFunc) gtk_icon_set_ref,
1282 (GBoxedFreeFunc) gtk_icon_set_unref);
1288 * gtk_icon_set_copy:
1289 * @icon_set: a #GtkIconSet
1291 * Copies @icon_set by value.
1293 * Return value: a new #GtkIconSet identical to the first.
1296 gtk_icon_set_copy (GtkIconSet *icon_set)
1301 copy = gtk_icon_set_new ();
1303 tmp_list = icon_set->sources;
1304 while (tmp_list != NULL)
1306 copy->sources = g_slist_prepend (copy->sources,
1307 gtk_icon_source_copy (tmp_list->data));
1309 tmp_list = g_slist_next (tmp_list);
1312 copy->sources = g_slist_reverse (copy->sources);
1314 copy->cache = copy_cache (icon_set, copy);
1315 copy->cache_size = icon_set->cache_size;
1316 copy->cache_serial = icon_set->cache_serial;
1322 sizes_equivalent (GtkIconSize lhs,
1325 /* We used to consider sizes equivalent if they were
1326 * the same pixel size, but we don't have the GtkSettings
1327 * here, so we can't do that. Plus, it's not clear that
1328 * it is right... it was just a workaround for the fact
1329 * that we register icons by logical size, not pixel size.
1335 gint r_w, r_h, l_w, l_h;
1337 icon_size_lookup_intern (NULL, rhs, &r_w, &r_h);
1338 icon_size_lookup_intern (NULL, lhs, &l_w, &l_h);
1340 return r_w == l_w && r_h == l_h;
1344 static GtkIconSource *
1345 find_best_matching_source (GtkIconSet *icon_set,
1346 GtkTextDirection direction,
1351 GtkIconSource *source;
1354 /* We need to find the best icon source. Direction matters more
1355 * than state, state matters more than size. icon_set->sources
1356 * is sorted according to wildness, so if we take the first
1357 * match we find it will be the least-wild match (if there are
1358 * multiple matches for a given "wildness" then the RC file contained
1359 * dumb stuff, and we end up with an arbitrary matching source)
1363 tmp_list = icon_set->sources;
1364 while (tmp_list != NULL)
1366 GtkIconSource *s = tmp_list->data;
1368 if ((s->any_direction || (s->direction == direction)) &&
1369 (s->any_state || (s->state == state)) &&
1370 (s->any_size || size == (GtkIconSize)-1 || (sizes_equivalent (size, s->size))))
1372 if (!g_slist_find (failed, s))
1379 tmp_list = g_slist_next (tmp_list);
1386 ensure_filename_pixbuf (GtkIconSet *icon_set,
1387 GtkIconSource *source)
1389 if (source->filename_pixbuf == NULL)
1391 GError *error = NULL;
1393 source->filename_pixbuf = gdk_pixbuf_new_from_file (source->source.filename, &error);
1395 if (source->filename_pixbuf == NULL)
1397 /* Remove this icon source so we don't keep trying to
1400 g_warning (_("Error loading icon: %s"), error->message);
1401 g_error_free (error);
1403 icon_set->sources = g_slist_remove (icon_set->sources, source);
1405 gtk_icon_source_free (source);
1415 render_icon_name_pixbuf (GtkIconSource *icon_source,
1417 GtkTextDirection direction,
1424 GdkPixbuf *tmp_pixbuf;
1425 GtkIconSource tmp_source;
1427 GtkIconTheme *icon_theme;
1428 GtkSettings *settings;
1429 gint width, height, pixel_size;
1430 gint *sizes, *s, dist;
1431 GError *error = NULL;
1433 if (widget && gtk_widget_has_screen (widget))
1434 screen = gtk_widget_get_screen (widget);
1435 else if (style && style->colormap)
1436 screen = gdk_colormap_get_screen (style->colormap);
1439 screen = gdk_screen_get_default ();
1440 GTK_NOTE (MULTIHEAD,
1441 g_warning ("Using the default screen for gtk_icon_source_render_icon()"));
1444 icon_theme = gtk_icon_theme_get_for_screen (screen);
1445 settings = gtk_settings_get_for_screen (screen);
1447 if (!gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
1449 if (size == (GtkIconSize)-1)
1451 /* Find an available size close to 48 */
1452 sizes = gtk_icon_theme_get_icon_sizes (icon_theme, icon_source->source.icon_name);
1454 width = height = 48;
1455 for (s = sizes; *s; s++)
1459 width = height = 48;
1466 width = height = *s;
1474 width = height = *s;
1484 g_warning ("Invalid icon size %u\n", size);
1485 width = height = 24;
1489 pixel_size = MIN (width, height);
1491 tmp_pixbuf = gtk_icon_theme_load_icon (icon_theme,
1492 icon_source->source.icon_name,
1498 g_warning ("Error loading theme icon '%s' for stock: %s",
1499 icon_source->source.icon_name, error ? error->message : "");
1501 g_error_free (error);
1505 tmp_source = *icon_source;
1506 tmp_source.type = GTK_ICON_SOURCE_PIXBUF;
1507 tmp_source.source.pixbuf = tmp_pixbuf;
1509 pixbuf = gtk_style_render_icon (style, &tmp_source,
1510 direction, state, -1,
1514 g_warning ("Failed to render icon");
1516 g_object_unref (tmp_pixbuf);
1522 find_and_render_icon_source (GtkIconSet *icon_set,
1524 GtkTextDirection direction,
1530 GSList *failed = NULL;
1531 GdkPixbuf *pixbuf = NULL;
1533 /* We treat failure in two different ways:
1535 * A) If loading a source that specifies a filename fails,
1536 * we treat that as permanent, and remove the source
1537 * from the GtkIconSet. (in ensure_filename_pixbuf ()
1538 * B) If loading a themed icon fails, or scaling an icon
1539 * fails, we treat that as transient and will try
1540 * again next time the icon falls out of the cache
1541 * and we need to recreate it.
1543 while (pixbuf == NULL)
1545 GtkIconSource *source = find_best_matching_source (icon_set, direction, state, size, failed);
1550 switch (source->type)
1552 case GTK_ICON_SOURCE_FILENAME:
1553 if (!ensure_filename_pixbuf (icon_set, source))
1556 case GTK_ICON_SOURCE_PIXBUF:
1557 pixbuf = gtk_style_render_icon (style, source,
1558 direction, state, size,
1562 g_warning ("Failed to render icon");
1563 failed = g_slist_prepend (failed, source);
1566 case GTK_ICON_SOURCE_ICON_NAME:
1567 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1568 pixbuf = render_icon_name_pixbuf (source, style,
1569 direction, state, size,
1572 failed = g_slist_prepend (failed, source);
1574 case GTK_ICON_SOURCE_EMPTY:
1575 g_assert_not_reached ();
1579 g_slist_free (failed);
1584 extern GtkIconCache *_builtin_cache;
1587 render_fallback_image (GtkStyle *style,
1588 GtkTextDirection direction,
1594 /* This icon can be used for any direction/state/size */
1595 static GtkIconSource fallback_source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1597 if (fallback_source.type == GTK_ICON_SOURCE_EMPTY)
1602 _gtk_icon_theme_ensure_builtin_cache ();
1604 index = _gtk_icon_cache_get_directory_index (_builtin_cache, "24");
1605 pixbuf = _gtk_icon_cache_get_icon (_builtin_cache,
1606 GTK_STOCK_MISSING_IMAGE,
1608 gtk_icon_source_set_pixbuf (&fallback_source, pixbuf);
1609 g_object_unref (pixbuf);
1612 return gtk_style_render_icon (style,
1622 * gtk_icon_set_render_icon:
1623 * @icon_set: a #GtkIconSet
1624 * @style: (allow-none): a #GtkStyle associated with @widget, or %NULL
1625 * @direction: text direction
1626 * @state: widget state
1627 * @size: (type int): icon size. A size of (GtkIconSize)-1
1628 * means render at the size of the source and don't scale.
1629 * @widget: (allow-none): widget that will display the icon, or %NULL.
1630 * The only use that is typically made of this
1631 * is to determine the appropriate #GdkScreen.
1632 * @detail: (allow-none): detail to pass to the theme engine, or %NULL.
1633 * Note that passing a detail of anything but %NULL
1634 * will disable caching.
1636 * Renders an icon using gtk_style_render_icon(). In most cases,
1637 * gtk_widget_render_icon() is better, since it automatically provides
1638 * most of the arguments from the current widget settings. This
1639 * function never returns %NULL; if the icon can't be rendered
1640 * (perhaps because an image file fails to load), a default "missing
1641 * image" icon will be returned instead.
1643 * Return value: a #GdkPixbuf to be displayed
1646 gtk_icon_set_render_icon (GtkIconSet *icon_set,
1648 GtkTextDirection direction,
1656 g_return_val_if_fail (icon_set != NULL, NULL);
1657 g_return_val_if_fail (style == NULL || GTK_IS_STYLE (style), NULL);
1659 if (icon_set->sources == NULL)
1660 return render_fallback_image (style, direction, state, size, widget, detail);
1664 icon = find_in_cache (icon_set, style, direction,
1669 g_object_ref (icon);
1675 icon = find_and_render_icon_source (icon_set, style, direction, state, size,
1679 icon = render_fallback_image (style, direction, state, size, widget, detail);
1682 add_to_cache (icon_set, style, direction, state, size, icon);
1687 /* Order sources by their "wildness", so that "wilder" sources are
1688 * greater than "specific" sources; for determining ordering,
1689 * direction beats state beats size.
1693 icon_source_compare (gconstpointer ap, gconstpointer bp)
1695 const GtkIconSource *a = ap;
1696 const GtkIconSource *b = bp;
1698 if (!a->any_direction && b->any_direction)
1700 else if (a->any_direction && !b->any_direction)
1702 else if (!a->any_state && b->any_state)
1704 else if (a->any_state && !b->any_state)
1706 else if (!a->any_size && b->any_size)
1708 else if (a->any_size && !b->any_size)
1715 * gtk_icon_set_add_source:
1716 * @icon_set: a #GtkIconSet
1717 * @source: a #GtkIconSource
1719 * Icon sets have a list of #GtkIconSource, which they use as base
1720 * icons for rendering icons in different states and sizes. Icons are
1721 * scaled, made to look insensitive, etc. in
1722 * gtk_icon_set_render_icon(), but #GtkIconSet needs base images to
1723 * work with. The base images and when to use them are described by
1726 * This function copies @source, so you can reuse the same source immediately
1727 * without affecting the icon set.
1729 * An example of when you'd use this function: a web browser's "Back
1730 * to Previous Page" icon might point in a different direction in
1731 * Hebrew and in English; it might look different when insensitive;
1732 * and it might change size depending on toolbar mode (small/large
1733 * icons). So a single icon set would contain all those variants of
1734 * the icon, and you might add a separate source for each one.
1736 * You should nearly always add a "default" icon source with all
1737 * fields wildcarded, which will be used as a fallback if no more
1738 * specific source matches. #GtkIconSet always prefers more specific
1739 * icon sources to more generic icon sources. The order in which you
1740 * add the sources to the icon set does not matter.
1742 * gtk_icon_set_new_from_pixbuf() creates a new icon set with a
1743 * default icon source based on the given pixbuf.
1746 gtk_icon_set_add_source (GtkIconSet *icon_set,
1747 const GtkIconSource *source)
1749 g_return_if_fail (icon_set != NULL);
1750 g_return_if_fail (source != NULL);
1752 if (source->type == GTK_ICON_SOURCE_EMPTY)
1754 g_warning ("Useless empty GtkIconSource");
1758 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
1759 gtk_icon_source_copy (source),
1760 icon_source_compare);
1764 * gtk_icon_set_get_sizes:
1765 * @icon_set: a #GtkIconSet
1766 * @sizes: (array length=n_sizes) (out) (type int): return location
1767 * for array of sizes
1768 * @n_sizes: location to store number of elements in returned array
1770 * Obtains a list of icon sizes this icon set can render. The returned
1771 * array must be freed with g_free().
1774 gtk_icon_set_get_sizes (GtkIconSet *icon_set,
1775 GtkIconSize **sizes,
1779 gboolean all_sizes = FALSE;
1780 GSList *specifics = NULL;
1782 g_return_if_fail (icon_set != NULL);
1783 g_return_if_fail (sizes != NULL);
1784 g_return_if_fail (n_sizes != NULL);
1786 tmp_list = icon_set->sources;
1787 while (tmp_list != NULL)
1789 GtkIconSource *source;
1791 source = tmp_list->data;
1793 if (source->any_size)
1799 specifics = g_slist_prepend (specifics, GINT_TO_POINTER (source->size));
1801 tmp_list = g_slist_next (tmp_list);
1806 /* Need to find out what sizes exist */
1811 *sizes = g_new (GtkIconSize, icon_sizes_used);
1812 *n_sizes = icon_sizes_used - 1;
1815 while (i < icon_sizes_used)
1817 (*sizes)[i - 1] = icon_sizes[i].size;
1825 *n_sizes = g_slist_length (specifics);
1826 *sizes = g_new (GtkIconSize, *n_sizes);
1829 tmp_list = specifics;
1830 while (tmp_list != NULL)
1832 (*sizes)[i] = GPOINTER_TO_INT (tmp_list->data);
1835 tmp_list = g_slist_next (tmp_list);
1839 g_slist_free (specifics);
1844 * gtk_icon_source_new:
1846 * Creates a new #GtkIconSource. A #GtkIconSource contains a #GdkPixbuf (or
1847 * image filename) that serves as the base image for one or more of the
1848 * icons in a #GtkIconSet, along with a specification for which icons in the
1849 * icon set will be based on that pixbuf or image file. An icon set contains
1850 * a set of icons that represent "the same" logical concept in different states,
1851 * different global text directions, and different sizes.
1853 * So for example a web browser's "Back to Previous Page" icon might
1854 * point in a different direction in Hebrew and in English; it might
1855 * look different when insensitive; and it might change size depending
1856 * on toolbar mode (small/large icons). So a single icon set would
1857 * contain all those variants of the icon. #GtkIconSet contains a list
1858 * of #GtkIconSource from which it can derive specific icon variants in
1861 * In the simplest case, #GtkIconSet contains one source pixbuf from
1862 * which it derives all variants. The convenience function
1863 * gtk_icon_set_new_from_pixbuf() handles this case; if you only have
1864 * one source pixbuf, just use that function.
1866 * If you want to use a different base pixbuf for different icon
1867 * variants, you create multiple icon sources, mark which variants
1868 * they'll be used to create, and add them to the icon set with
1869 * gtk_icon_set_add_source().
1871 * By default, the icon source has all parameters wildcarded. That is,
1872 * the icon source will be used as the base icon for any desired text
1873 * direction, widget state, or icon size.
1875 * Return value: a new #GtkIconSource
1878 gtk_icon_source_new (void)
1882 src = g_new0 (GtkIconSource, 1);
1884 src->direction = GTK_TEXT_DIR_NONE;
1885 src->size = GTK_ICON_SIZE_INVALID;
1886 src->state = GTK_STATE_NORMAL;
1888 src->any_direction = TRUE;
1889 src->any_state = TRUE;
1890 src->any_size = TRUE;
1896 * gtk_icon_source_copy:
1897 * @source: a #GtkIconSource
1899 * Creates a copy of @source; mostly useful for language bindings.
1901 * Return value: a new #GtkIconSource
1904 gtk_icon_source_copy (const GtkIconSource *source)
1906 GtkIconSource *copy;
1908 g_return_val_if_fail (source != NULL, NULL);
1910 copy = g_new (GtkIconSource, 1);
1916 case GTK_ICON_SOURCE_EMPTY:
1917 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1919 case GTK_ICON_SOURCE_ICON_NAME:
1920 copy->source.icon_name = g_strdup (copy->source.icon_name);
1922 case GTK_ICON_SOURCE_FILENAME:
1923 copy->source.filename = g_strdup (copy->source.filename);
1924 #if defined (G_OS_WIN32) && !defined (_WIN64)
1925 copy->cp_filename = g_strdup (copy->cp_filename);
1927 if (copy->filename_pixbuf)
1928 g_object_ref (copy->filename_pixbuf);
1930 case GTK_ICON_SOURCE_PIXBUF:
1931 g_object_ref (copy->source.pixbuf);
1934 g_assert_not_reached();
1941 * gtk_icon_source_free:
1942 * @source: a #GtkIconSource
1944 * Frees a dynamically-allocated icon source, along with its
1945 * filename, size, and pixbuf fields if those are not %NULL.
1948 gtk_icon_source_free (GtkIconSource *source)
1950 g_return_if_fail (source != NULL);
1952 icon_source_clear (source);
1957 gtk_icon_source_get_type (void)
1959 static GType our_type = 0;
1962 our_type = g_boxed_type_register_static (I_("GtkIconSource"),
1963 (GBoxedCopyFunc) gtk_icon_source_copy,
1964 (GBoxedFreeFunc) gtk_icon_source_free);
1970 icon_source_clear (GtkIconSource *source)
1972 switch (source->type)
1974 case GTK_ICON_SOURCE_EMPTY:
1976 case GTK_ICON_SOURCE_ICON_NAME:
1977 g_free (source->source.icon_name);
1979 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1980 source->source.icon_name = NULL;
1982 case GTK_ICON_SOURCE_FILENAME:
1983 g_free (source->source.filename);
1984 source->source.filename = NULL;
1985 #if defined (G_OS_WIN32) && !defined (_WIN64)
1986 g_free (source->cp_filename);
1987 source->cp_filename = NULL;
1989 if (source->filename_pixbuf)
1990 g_object_unref (source->filename_pixbuf);
1991 source->filename_pixbuf = NULL;
1993 case GTK_ICON_SOURCE_PIXBUF:
1994 g_object_unref (source->source.pixbuf);
1995 source->source.pixbuf = NULL;
1998 g_assert_not_reached();
2001 source->type = GTK_ICON_SOURCE_EMPTY;
2005 * gtk_icon_source_set_filename:
2006 * @source: a #GtkIconSource
2007 * @filename: image file to use
2009 * Sets the name of an image file to use as a base image when creating
2010 * icon variants for #GtkIconSet. The filename must be absolute.
2013 gtk_icon_source_set_filename (GtkIconSource *source,
2014 const gchar *filename)
2016 g_return_if_fail (source != NULL);
2017 g_return_if_fail (filename == NULL || g_path_is_absolute (filename));
2019 if (source->type == GTK_ICON_SOURCE_FILENAME &&
2020 source->source.filename == filename)
2023 icon_source_clear (source);
2025 if (filename != NULL)
2027 source->type = GTK_ICON_SOURCE_FILENAME;
2028 source->source.filename = g_strdup (filename);
2029 #if defined (G_OS_WIN32) && !defined (_WIN64)
2030 source->cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
2036 * gtk_icon_source_set_icon_name
2037 * @source: a #GtkIconSource
2038 * @icon_name: (allow-none): name of icon to use
2040 * Sets the name of an icon to look up in the current icon theme
2041 * to use as a base image when creating icon variants for #GtkIconSet.
2044 gtk_icon_source_set_icon_name (GtkIconSource *source,
2045 const gchar *icon_name)
2047 g_return_if_fail (source != NULL);
2049 if (source->type == GTK_ICON_SOURCE_ICON_NAME &&
2050 source->source.icon_name == icon_name)
2053 icon_source_clear (source);
2055 if (icon_name != NULL)
2057 source->type = GTK_ICON_SOURCE_ICON_NAME;
2058 source->source.icon_name = g_strdup (icon_name);
2063 * gtk_icon_source_set_pixbuf:
2064 * @source: a #GtkIconSource
2065 * @pixbuf: pixbuf to use as a source
2067 * Sets a pixbuf to use as a base image when creating icon variants
2071 gtk_icon_source_set_pixbuf (GtkIconSource *source,
2074 g_return_if_fail (source != NULL);
2075 g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
2077 if (source->type == GTK_ICON_SOURCE_PIXBUF &&
2078 source->source.pixbuf == pixbuf)
2081 icon_source_clear (source);
2085 source->type = GTK_ICON_SOURCE_PIXBUF;
2086 source->source.pixbuf = g_object_ref (pixbuf);
2091 * gtk_icon_source_get_filename:
2092 * @source: a #GtkIconSource
2094 * Retrieves the source filename, or %NULL if none is set. The
2095 * filename is not a copy, and should not be modified or expected to
2096 * persist beyond the lifetime of the icon source.
2098 * Return value: image filename. This string must not be modified
2101 G_CONST_RETURN gchar*
2102 gtk_icon_source_get_filename (const GtkIconSource *source)
2104 g_return_val_if_fail (source != NULL, NULL);
2106 if (source->type == GTK_ICON_SOURCE_FILENAME)
2107 return source->source.filename;
2113 * gtk_icon_source_get_icon_name:
2114 * @source: a #GtkIconSource
2116 * Retrieves the source icon name, or %NULL if none is set. The
2117 * icon_name is not a copy, and should not be modified or expected to
2118 * persist beyond the lifetime of the icon source.
2120 * Return value: icon name. This string must not be modified or freed.
2122 G_CONST_RETURN gchar*
2123 gtk_icon_source_get_icon_name (const GtkIconSource *source)
2125 g_return_val_if_fail (source != NULL, NULL);
2127 if (source->type == GTK_ICON_SOURCE_ICON_NAME ||
2128 source->type == GTK_ICON_SOURCE_STATIC_ICON_NAME)
2129 return source->source.icon_name;
2135 * gtk_icon_source_get_pixbuf:
2136 * @source: a #GtkIconSource
2138 * Retrieves the source pixbuf, or %NULL if none is set.
2139 * In addition, if a filename source is in use, this
2140 * function in some cases will return the pixbuf from
2141 * loaded from the filename. This is, for example, true
2142 * for the GtkIconSource passed to the GtkStyle::render_icon()
2143 * virtual function. The reference count on the pixbuf is
2146 * Return value: source pixbuf
2149 gtk_icon_source_get_pixbuf (const GtkIconSource *source)
2151 g_return_val_if_fail (source != NULL, NULL);
2153 if (source->type == GTK_ICON_SOURCE_PIXBUF)
2154 return source->source.pixbuf;
2155 else if (source->type == GTK_ICON_SOURCE_FILENAME)
2156 return source->filename_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.
2177 gtk_icon_source_set_direction_wildcarded (GtkIconSource *source,
2180 g_return_if_fail (source != NULL);
2182 source->any_direction = setting != FALSE;
2186 * gtk_icon_source_set_state_wildcarded:
2187 * @source: a #GtkIconSource
2188 * @setting: %TRUE to wildcard the widget state
2190 * If the widget state is wildcarded, this source can be used as the
2191 * base image for an icon in any #GtkStateType. If the widget state
2192 * is not wildcarded, then the state the source applies to should be
2193 * set with gtk_icon_source_set_state() and the icon source will
2194 * only be used with that specific state.
2196 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2197 * wildcarded sources, and will use an exact match when possible.
2199 * #GtkIconSet will normally transform wildcarded source images to
2200 * produce an appropriate icon for a given state, for example
2201 * lightening an image on prelight, but will not modify source images
2202 * that match exactly.
2205 gtk_icon_source_set_state_wildcarded (GtkIconSource *source,
2208 g_return_if_fail (source != NULL);
2210 source->any_state = setting != FALSE;
2215 * gtk_icon_source_set_size_wildcarded:
2216 * @source: a #GtkIconSource
2217 * @setting: %TRUE to wildcard the widget state
2219 * If the icon size is wildcarded, this source can be used as the base
2220 * image for an icon of any size. If the size is not wildcarded, then
2221 * the size the source applies to should be set with
2222 * gtk_icon_source_set_size() and the icon source will only be used
2223 * with that specific size.
2225 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2226 * wildcarded sources, and will use an exact match when possible.
2228 * #GtkIconSet will normally scale wildcarded source images to produce
2229 * an appropriate icon at a given size, but will not change the size
2230 * of source images that match exactly.
2233 gtk_icon_source_set_size_wildcarded (GtkIconSource *source,
2236 g_return_if_fail (source != NULL);
2238 source->any_size = setting != FALSE;
2242 * gtk_icon_source_get_size_wildcarded:
2243 * @source: a #GtkIconSource
2245 * Gets the value set by gtk_icon_source_set_size_wildcarded().
2247 * Return value: %TRUE if this icon source is a base for any icon size variant
2250 gtk_icon_source_get_size_wildcarded (const GtkIconSource *source)
2252 g_return_val_if_fail (source != NULL, TRUE);
2254 return source->any_size;
2258 * gtk_icon_source_get_state_wildcarded:
2259 * @source: a #GtkIconSource
2261 * Gets the value set by gtk_icon_source_set_state_wildcarded().
2263 * Return value: %TRUE if this icon source is a base for any widget state variant
2266 gtk_icon_source_get_state_wildcarded (const GtkIconSource *source)
2268 g_return_val_if_fail (source != NULL, TRUE);
2270 return source->any_state;
2274 * gtk_icon_source_get_direction_wildcarded:
2275 * @source: a #GtkIconSource
2277 * Gets the value set by gtk_icon_source_set_direction_wildcarded().
2279 * Return value: %TRUE if this icon source is a base for any text direction variant
2282 gtk_icon_source_get_direction_wildcarded (const GtkIconSource *source)
2284 g_return_val_if_fail (source != NULL, TRUE);
2286 return source->any_direction;
2290 * gtk_icon_source_set_direction:
2291 * @source: a #GtkIconSource
2292 * @direction: text direction this source applies to
2294 * Sets the text direction this icon source is intended to be used
2297 * Setting the text direction on an icon source makes no difference
2298 * if the text direction is wildcarded. Therefore, you should usually
2299 * call gtk_icon_source_set_direction_wildcarded() to un-wildcard it
2300 * in addition to calling this function.
2303 gtk_icon_source_set_direction (GtkIconSource *source,
2304 GtkTextDirection direction)
2306 g_return_if_fail (source != NULL);
2308 source->direction = direction;
2312 * gtk_icon_source_set_state:
2313 * @source: a #GtkIconSource
2314 * @state: widget state this source applies to
2316 * Sets the widget state this icon source is intended to be used
2319 * Setting the widget state on an icon source makes no difference
2320 * if the state is wildcarded. Therefore, you should usually
2321 * call gtk_icon_source_set_state_wildcarded() to un-wildcard it
2322 * in addition to calling this function.
2325 gtk_icon_source_set_state (GtkIconSource *source,
2328 g_return_if_fail (source != NULL);
2330 source->state = state;
2334 * gtk_icon_source_set_size:
2335 * @source: a #GtkIconSource
2336 * @size: (type int): icon size this source applies to
2338 * Sets the icon size this icon source is intended to be used
2341 * Setting the icon size on an icon source makes no difference
2342 * if the size is wildcarded. Therefore, you should usually
2343 * call gtk_icon_source_set_size_wildcarded() to un-wildcard it
2344 * in addition to calling this function.
2347 gtk_icon_source_set_size (GtkIconSource *source,
2350 g_return_if_fail (source != NULL);
2352 source->size = size;
2356 * gtk_icon_source_get_direction:
2357 * @source: a #GtkIconSource
2359 * Obtains the text direction this icon source applies to. The return
2360 * value is only useful/meaningful if the text direction is <emphasis>not</emphasis>
2363 * Return value: text direction this source matches
2366 gtk_icon_source_get_direction (const GtkIconSource *source)
2368 g_return_val_if_fail (source != NULL, 0);
2370 return source->direction;
2374 * gtk_icon_source_get_state:
2375 * @source: a #GtkIconSource
2377 * Obtains the widget state this icon source applies to. The return
2378 * value is only useful/meaningful if the widget state is <emphasis>not</emphasis>
2381 * Return value: widget state this source matches
2384 gtk_icon_source_get_state (const GtkIconSource *source)
2386 g_return_val_if_fail (source != NULL, 0);
2388 return source->state;
2392 * gtk_icon_source_get_size:
2393 * @source: a #GtkIconSource
2395 * Obtains the icon size this source applies to. The return value
2396 * is only useful/meaningful if the icon size is <emphasis>not</emphasis> wildcarded.
2398 * Return value: (type int): icon size this source matches.
2401 gtk_icon_source_get_size (const GtkIconSource *source)
2403 g_return_val_if_fail (source != NULL, 0);
2405 return source->size;
2408 #define NUM_CACHED_ICONS 8
2410 typedef struct _CachedIcon CachedIcon;
2414 /* These must all match to use the cached pixbuf.
2415 * If any don't match, we must re-render the pixbuf.
2418 GtkTextDirection direction;
2426 ensure_cache_up_to_date (GtkIconSet *icon_set)
2428 if (icon_set->cache_serial != cache_serial)
2430 clear_cache (icon_set, TRUE);
2431 icon_set->cache_serial = cache_serial;
2436 cached_icon_free (CachedIcon *icon)
2438 g_object_unref (icon->pixbuf);
2441 g_object_unref (icon->style);
2447 find_in_cache (GtkIconSet *icon_set,
2449 GtkTextDirection direction,
2456 ensure_cache_up_to_date (icon_set);
2459 tmp_list = icon_set->cache;
2460 while (tmp_list != NULL)
2462 CachedIcon *icon = tmp_list->data;
2464 if (icon->style == style &&
2465 icon->direction == direction &&
2466 icon->state == state &&
2467 (size == (GtkIconSize)-1 || icon->size == size))
2471 /* Move this icon to the front of the list. */
2472 prev->next = tmp_list->next;
2473 tmp_list->next = icon_set->cache;
2474 icon_set->cache = tmp_list;
2477 return icon->pixbuf;
2481 tmp_list = g_slist_next (tmp_list);
2488 add_to_cache (GtkIconSet *icon_set,
2490 GtkTextDirection direction,
2497 ensure_cache_up_to_date (icon_set);
2499 g_object_ref (pixbuf);
2501 /* We have to ref the style, since if the style was finalized
2502 * its address could be reused by another style, creating a
2507 g_object_ref (style);
2509 icon = g_new (CachedIcon, 1);
2510 icon_set->cache = g_slist_prepend (icon_set->cache, icon);
2511 icon_set->cache_size++;
2513 icon->style = style;
2514 icon->direction = direction;
2515 icon->state = state;
2517 icon->pixbuf = pixbuf;
2520 attach_to_style (icon_set, icon->style);
2522 if (icon_set->cache_size >= NUM_CACHED_ICONS)
2524 /* Remove oldest item in the cache */
2527 tmp_list = icon_set->cache;
2529 /* Find next-to-last link */
2530 g_assert (NUM_CACHED_ICONS > 2);
2531 while (tmp_list->next->next)
2532 tmp_list = tmp_list->next;
2534 g_assert (tmp_list != NULL);
2535 g_assert (tmp_list->next != NULL);
2536 g_assert (tmp_list->next->next == NULL);
2538 /* Free the last icon */
2539 icon = tmp_list->next->data;
2541 g_slist_free (tmp_list->next);
2542 tmp_list->next = NULL;
2544 cached_icon_free (icon);
2549 clear_cache (GtkIconSet *icon_set,
2550 gboolean style_detach)
2552 GSList *cache, *tmp_list;
2553 GtkStyle *last_style = NULL;
2555 cache = icon_set->cache;
2556 icon_set->cache = NULL;
2557 icon_set->cache_size = 0;
2559 while (tmp_list != NULL)
2561 CachedIcon *icon = tmp_list->data;
2565 /* simple optimization for the case where the cache
2566 * contains contiguous icons from the same style.
2567 * it's safe to call detach_from_style more than
2568 * once on the same style though.
2570 if (last_style != icon->style)
2572 detach_from_style (icon_set, icon->style);
2573 last_style = icon->style;
2577 cached_icon_free (icon);
2579 tmp_list = g_slist_next (tmp_list);
2582 g_slist_free (cache);
2586 copy_cache (GtkIconSet *icon_set,
2587 GtkIconSet *copy_recipient)
2590 GSList *copy = NULL;
2592 ensure_cache_up_to_date (icon_set);
2594 tmp_list = icon_set->cache;
2595 while (tmp_list != NULL)
2597 CachedIcon *icon = tmp_list->data;
2598 CachedIcon *icon_copy = g_new (CachedIcon, 1);
2602 if (icon_copy->style)
2604 attach_to_style (copy_recipient, icon_copy->style);
2605 g_object_ref (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 * _gtk_icon_factory_list_ids:
2691 * Gets all known IDs stored in an existing icon factory.
2692 * The strings in the returned list aren't copied.
2693 * The list itself should be freed.
2695 * Return value: List of ids in icon factories
2698 _gtk_icon_factory_list_ids (void)
2705 _gtk_icon_factory_ensure_default_icons ();
2707 tmp_list = all_icon_factories;
2708 while (tmp_list != NULL)
2712 GtkIconFactory *factory = GTK_ICON_FACTORY (tmp_list->data);
2714 these_ids = g_hash_table_get_keys (factory->icons);
2716 ids = g_list_concat (ids, these_ids);
2718 tmp_list = g_slist_next (tmp_list);
2728 } IconFactoryParserData;
2734 GtkTextDirection direction;
2737 } IconSourceParserData;
2740 icon_source_start_element (GMarkupParseContext *context,
2741 const gchar *element_name,
2742 const gchar **names,
2743 const gchar **values,
2748 gchar *stock_id = NULL;
2749 gchar *filename = NULL;
2750 gchar *icon_name = NULL;
2752 gint direction = -1;
2754 IconFactoryParserData *parser_data;
2755 IconSourceParserData *source_data;
2757 GQuark error_domain;
2759 parser_data = (IconFactoryParserData*)user_data;
2761 if (!parser_data->in_source)
2763 if (strcmp (element_name, "sources") != 0)
2765 error_msg = g_strdup_printf ("Unexpected element %s, expected <sources>", element_name);
2766 error_domain = GTK_BUILDER_ERROR_INVALID_TAG;
2769 parser_data->in_source = TRUE;
2774 if (strcmp (element_name, "source") != 0)
2776 error_msg = g_strdup_printf ("Unexpected element %s, expected <source>", element_name);
2777 error_domain = GTK_BUILDER_ERROR_INVALID_TAG;
2782 for (i = 0; names[i]; i++)
2784 if (strcmp (names[i], "stock-id") == 0)
2785 stock_id = g_strdup (values[i]);
2786 else if (strcmp (names[i], "filename") == 0)
2787 filename = g_strdup (values[i]);
2788 else if (strcmp (names[i], "icon-name") == 0)
2789 icon_name = g_strdup (values[i]);
2790 else if (strcmp (names[i], "size") == 0)
2792 if (!_gtk_builder_enum_from_string (GTK_TYPE_ICON_SIZE,
2798 else if (strcmp (names[i], "direction") == 0)
2800 if (!_gtk_builder_enum_from_string (GTK_TYPE_TEXT_DIRECTION,
2806 else if (strcmp (names[i], "state") == 0)
2808 if (!_gtk_builder_enum_from_string (GTK_TYPE_STATE_TYPE,
2816 error_msg = g_strdup_printf ("'%s' is not a valid attribute of <%s>",
2817 names[i], "source");
2818 error_domain = GTK_BUILDER_ERROR_INVALID_ATTRIBUTE;
2825 error_msg = g_strdup_printf ("<source> requires a stock_id");
2826 error_domain = GTK_BUILDER_ERROR_MISSING_ATTRIBUTE;
2830 source_data = g_slice_new (IconSourceParserData);
2831 source_data->stock_id = stock_id;
2832 source_data->filename = filename;
2833 source_data->icon_name = icon_name;
2834 source_data->size = size;
2835 source_data->direction = direction;
2836 source_data->state = state;
2838 parser_data->sources = g_slist_prepend (parser_data->sources, source_data);
2844 gint line_number, char_number;
2846 g_markup_parse_context_get_position (context,
2850 tmp = g_strdup_printf ("%s:%d:%d %s", "input",
2851 line_number, char_number, error_msg);
2853 g_set_error_literal (error,
2858 g_warning ("%s", tmp);
2868 static const GMarkupParser icon_source_parser =
2870 icon_source_start_element,
2874 gtk_icon_factory_buildable_custom_tag_start (GtkBuildable *buildable,
2875 GtkBuilder *builder,
2877 const gchar *tagname,
2878 GMarkupParser *parser,
2881 g_assert (buildable);
2883 if (strcmp (tagname, "sources") == 0)
2885 IconFactoryParserData *parser_data;
2887 parser_data = g_slice_new0 (IconFactoryParserData);
2888 *parser = icon_source_parser;
2889 *data = parser_data;
2896 gtk_icon_factory_buildable_custom_tag_end (GtkBuildable *buildable,
2897 GtkBuilder *builder,
2899 const gchar *tagname,
2900 gpointer *user_data)
2902 GtkIconFactory *icon_factory;
2904 icon_factory = GTK_ICON_FACTORY (buildable);
2906 if (strcmp (tagname, "sources") == 0)
2908 IconFactoryParserData *parser_data;
2909 GtkIconSource *icon_source;
2910 GtkIconSet *icon_set;
2913 parser_data = (IconFactoryParserData*)user_data;
2915 for (l = parser_data->sources; l; l = l->next)
2917 IconSourceParserData *source_data = l->data;
2919 icon_set = gtk_icon_factory_lookup (icon_factory, source_data->stock_id);
2922 icon_set = gtk_icon_set_new ();
2923 gtk_icon_factory_add (icon_factory, source_data->stock_id, icon_set);
2924 gtk_icon_set_unref (icon_set);
2927 icon_source = gtk_icon_source_new ();
2929 if (source_data->filename)
2932 filename = _gtk_builder_get_absolute_filename (builder, source_data->filename);
2933 gtk_icon_source_set_filename (icon_source, filename);
2936 if (source_data->icon_name)
2937 gtk_icon_source_set_icon_name (icon_source, source_data->icon_name);
2938 if (source_data->size != -1)
2940 gtk_icon_source_set_size (icon_source, source_data->size);
2941 gtk_icon_source_set_size_wildcarded (icon_source, FALSE);
2943 if (source_data->direction != -1)
2945 gtk_icon_source_set_direction (icon_source, source_data->direction);
2946 gtk_icon_source_set_direction_wildcarded (icon_source, FALSE);
2948 if (source_data->state != -1)
2950 gtk_icon_source_set_state (icon_source, source_data->state);
2951 gtk_icon_source_set_state_wildcarded (icon_source, FALSE);
2954 /* Inline source_add() to avoid creating a copy */
2955 g_assert (icon_source->type != GTK_ICON_SOURCE_EMPTY);
2956 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
2958 icon_source_compare);
2960 g_free (source_data->stock_id);
2961 g_free (source_data->filename);
2962 g_free (source_data->icon_name);
2963 g_slice_free (IconSourceParserData, source_data);
2965 g_slist_free (parser_data->sources);
2966 g_slice_free (IconFactoryParserData, parser_data);
2968 /* TODO: Add an attribute/tag to prevent this.
2969 * Usually it's the right thing to do though.
2971 gtk_icon_factory_add_default (icon_factory);
2975 #if defined (G_OS_WIN32) && !defined (_WIN64)
2977 /* DLL ABI stability backward compatibility versions */
2979 #undef gtk_icon_source_set_filename
2982 gtk_icon_source_set_filename (GtkIconSource *source,
2983 const gchar *filename)
2985 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2987 gtk_icon_source_set_filename_utf8 (source, utf8_filename);
2989 g_free (utf8_filename);
2992 #undef gtk_icon_source_get_filename
2994 G_CONST_RETURN gchar*
2995 gtk_icon_source_get_filename (const GtkIconSource *source)
2997 g_return_val_if_fail (source != NULL, NULL);
2999 if (source->type == GTK_ICON_SOURCE_FILENAME)
3000 return source->cp_filename;
3007 #define __GTK_ICON_FACTORY_C__
3008 #include "gtkaliasdef.c"