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"
43 static GSList *all_icon_factories = NULL;
45 struct _GtkIconFactoryPrivate
51 GTK_ICON_SOURCE_EMPTY,
52 GTK_ICON_SOURCE_ICON_NAME,
53 GTK_ICON_SOURCE_STATIC_ICON_NAME,
54 GTK_ICON_SOURCE_FILENAME,
55 GTK_ICON_SOURCE_PIXBUF
60 GtkIconSourceType type;
68 GdkPixbuf *filename_pixbuf;
70 GtkTextDirection direction;
74 /* If TRUE, then the parameter is wildcarded, and the above
75 * fields should be ignored. If FALSE, the parameter is
76 * specified, and the above fields should be valid.
78 guint any_direction : 1;
85 gtk_icon_factory_buildable_init (GtkBuildableIface *iface);
87 static gboolean gtk_icon_factory_buildable_custom_tag_start (GtkBuildable *buildable,
91 GMarkupParser *parser,
93 static void gtk_icon_factory_buildable_custom_tag_end (GtkBuildable *buildable,
98 static void gtk_icon_factory_finalize (GObject *object);
99 static void get_default_icons (GtkIconFactory *icon_factory);
100 static void icon_source_clear (GtkIconSource *source);
102 static GtkIconSize icon_size_register_intern (const gchar *name,
106 #define GTK_ICON_SOURCE_INIT(any_direction, any_state, any_size) \
107 { GTK_ICON_SOURCE_EMPTY, { NULL }, NULL, \
109 any_direction, any_state, any_size }
111 G_DEFINE_TYPE_WITH_CODE (GtkIconFactory, gtk_icon_factory, G_TYPE_OBJECT,
112 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
113 gtk_icon_factory_buildable_init))
116 gtk_icon_factory_init (GtkIconFactory *factory)
118 GtkIconFactoryPrivate *priv;
120 factory->priv = G_TYPE_INSTANCE_GET_PRIVATE (factory,
121 GTK_TYPE_ICON_FACTORY,
122 GtkIconFactoryPrivate);
123 priv = factory->priv;
125 priv->icons = g_hash_table_new (g_str_hash, g_str_equal);
126 all_icon_factories = g_slist_prepend (all_icon_factories, factory);
130 gtk_icon_factory_class_init (GtkIconFactoryClass *klass)
132 GObjectClass *object_class = G_OBJECT_CLASS (klass);
134 object_class->finalize = gtk_icon_factory_finalize;
136 g_type_class_add_private (klass, sizeof (GtkIconFactoryPrivate));
140 gtk_icon_factory_buildable_init (GtkBuildableIface *iface)
142 iface->custom_tag_start = gtk_icon_factory_buildable_custom_tag_start;
143 iface->custom_tag_end = gtk_icon_factory_buildable_custom_tag_end;
147 free_icon_set (gpointer key, gpointer value, gpointer data)
150 gtk_icon_set_unref (value);
154 gtk_icon_factory_finalize (GObject *object)
156 GtkIconFactory *factory = GTK_ICON_FACTORY (object);
157 GtkIconFactoryPrivate *priv = factory->priv;
159 all_icon_factories = g_slist_remove (all_icon_factories, factory);
161 g_hash_table_foreach (priv->icons, free_icon_set, NULL);
163 g_hash_table_destroy (priv->icons);
165 G_OBJECT_CLASS (gtk_icon_factory_parent_class)->finalize (object);
169 * gtk_icon_factory_new:
171 * Creates a new #GtkIconFactory. An icon factory manages a collection
172 * of #GtkIconSet<!-- -->s; a #GtkIconSet manages a set of variants of a
173 * particular icon (i.e. a #GtkIconSet contains variants for different
174 * sizes and widget states). Icons in an icon factory are named by a
175 * stock ID, which is a simple string identifying the icon. Each
176 * #GtkStyle has a list of #GtkIconFactory<!-- -->s derived from the current
177 * theme; those icon factories are consulted first when searching for
178 * an icon. If the theme doesn't set a particular icon, GTK+ looks for
179 * the icon in a list of default icon factories, maintained by
180 * gtk_icon_factory_add_default() and
181 * gtk_icon_factory_remove_default(). Applications with icons should
182 * add a default icon factory with their icons, which will allow
183 * themes to override the icons for the application.
185 * Return value: a new #GtkIconFactory
188 gtk_icon_factory_new (void)
190 return g_object_new (GTK_TYPE_ICON_FACTORY, NULL);
194 * gtk_icon_factory_add:
195 * @factory: a #GtkIconFactory
196 * @stock_id: icon name
197 * @icon_set: icon set
199 * Adds the given @icon_set to the icon factory, under the name
200 * @stock_id. @stock_id should be namespaced for your application,
201 * e.g. "myapp-whatever-icon". Normally applications create a
202 * #GtkIconFactory, then add it to the list of default factories with
203 * gtk_icon_factory_add_default(). Then they pass the @stock_id to
204 * widgets such as #GtkImage to display the icon. Themes can provide
205 * an icon with the same name (such as "myapp-whatever-icon") to
206 * override your application's default icons. If an icon already
207 * existed in @factory for @stock_id, it is unreferenced and replaced
208 * with the new @icon_set.
211 gtk_icon_factory_add (GtkIconFactory *factory,
212 const gchar *stock_id,
213 GtkIconSet *icon_set)
215 GtkIconFactoryPrivate *priv = factory->priv;
216 gpointer old_key = NULL;
217 gpointer old_value = NULL;
219 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
220 g_return_if_fail (stock_id != NULL);
221 g_return_if_fail (icon_set != NULL);
223 g_hash_table_lookup_extended (priv->icons, stock_id,
224 &old_key, &old_value);
226 if (old_value == icon_set)
229 gtk_icon_set_ref (icon_set);
231 /* GHashTable key memory management is so fantastically broken. */
233 g_hash_table_insert (priv->icons, old_key, icon_set);
235 g_hash_table_insert (priv->icons, g_strdup (stock_id), icon_set);
238 gtk_icon_set_unref (old_value);
242 * gtk_icon_factory_lookup:
243 * @factory: a #GtkIconFactory
244 * @stock_id: an icon name
246 * Looks up @stock_id in the icon factory, returning an icon set
247 * if found, otherwise %NULL. For display to the user, you should
248 * use gtk_style_lookup_icon_set() on the #GtkStyle for the
249 * widget that will display the icon, instead of using this
250 * function directly, so that themes are taken into account.
252 * Return value: icon set of @stock_id.
255 gtk_icon_factory_lookup (GtkIconFactory *factory,
256 const gchar *stock_id)
258 GtkIconFactoryPrivate *priv;
260 g_return_val_if_fail (GTK_IS_ICON_FACTORY (factory), NULL);
261 g_return_val_if_fail (stock_id != NULL, NULL);
263 priv = factory->priv;
265 return g_hash_table_lookup (priv->icons, stock_id);
268 static GtkIconFactory *gtk_default_icons = NULL;
269 static GSList *default_factories = NULL;
272 * gtk_icon_factory_add_default:
273 * @factory: a #GtkIconFactory
275 * Adds an icon factory to the list of icon factories searched by
276 * gtk_style_lookup_icon_set(). This means that, for example,
277 * gtk_image_new_from_stock() will be able to find icons in @factory.
278 * There will normally be an icon factory added for each library or
279 * application that comes with icons. The default icon factories
280 * can be overridden by themes.
283 gtk_icon_factory_add_default (GtkIconFactory *factory)
285 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
287 g_object_ref (factory);
289 default_factories = g_slist_prepend (default_factories, factory);
293 * gtk_icon_factory_remove_default:
294 * @factory: a #GtkIconFactory previously added with gtk_icon_factory_add_default()
296 * Removes an icon factory from the list of default icon
297 * factories. Not normally used; you might use it for a library that
298 * can be unloaded or shut down.
301 gtk_icon_factory_remove_default (GtkIconFactory *factory)
303 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
305 default_factories = g_slist_remove (default_factories, factory);
307 g_object_unref (factory);
311 _gtk_icon_factory_ensure_default_icons (void)
313 if (gtk_default_icons == NULL)
315 gtk_default_icons = gtk_icon_factory_new ();
317 get_default_icons (gtk_default_icons);
322 * gtk_icon_factory_lookup_default:
323 * @stock_id: an icon name
325 * Looks for an icon in the list of default icon factories. For
326 * display to the user, you should use gtk_style_lookup_icon_set() on
327 * the #GtkStyle for the widget that will display the icon, instead of
328 * using this function directly, so that themes are taken into
331 * Return value: a #GtkIconSet, or %NULL
334 gtk_icon_factory_lookup_default (const gchar *stock_id)
338 g_return_val_if_fail (stock_id != NULL, NULL);
340 tmp_list = default_factories;
341 while (tmp_list != NULL)
343 GtkIconSet *icon_set =
344 gtk_icon_factory_lookup (GTK_ICON_FACTORY (tmp_list->data),
350 tmp_list = g_slist_next (tmp_list);
353 _gtk_icon_factory_ensure_default_icons ();
355 return gtk_icon_factory_lookup (gtk_default_icons, stock_id);
359 register_stock_icon (GtkIconFactory *factory,
360 const gchar *stock_id,
361 const gchar *icon_name)
363 GtkIconSet *set = gtk_icon_set_new ();
364 GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
366 source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
367 source.source.icon_name = (gchar *)icon_name;
368 source.direction = GTK_TEXT_DIR_NONE;
369 gtk_icon_set_add_source (set, &source);
371 gtk_icon_factory_add (factory, stock_id, set);
372 gtk_icon_set_unref (set);
376 register_bidi_stock_icon (GtkIconFactory *factory,
377 const gchar *stock_id,
378 const gchar *icon_name)
380 GtkIconSet *set = gtk_icon_set_new ();
381 GtkIconSource source = GTK_ICON_SOURCE_INIT (FALSE, TRUE, TRUE);
383 source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
384 source.source.icon_name = (gchar *)icon_name;
385 source.direction = GTK_TEXT_DIR_LTR;
386 gtk_icon_set_add_source (set, &source);
388 source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
389 source.source.icon_name = (gchar *)icon_name;
390 source.direction = GTK_TEXT_DIR_RTL;
391 gtk_icon_set_add_source (set, &source);
393 gtk_icon_factory_add (factory, stock_id, set);
394 gtk_icon_set_unref (set);
398 get_default_icons (GtkIconFactory *factory)
400 /* KEEP IN SYNC with gtkstock.c */
402 register_stock_icon (factory, GTK_STOCK_DIALOG_AUTHENTICATION, "dialog-password");
403 register_stock_icon (factory, GTK_STOCK_DIALOG_ERROR, "dialog-error");
404 register_stock_icon (factory, GTK_STOCK_DIALOG_INFO, "dialog-information");
405 register_stock_icon (factory, GTK_STOCK_DIALOG_QUESTION, "dialog-question");
406 register_stock_icon (factory, GTK_STOCK_DIALOG_WARNING, "dialog-warning");
407 register_stock_icon (factory, GTK_STOCK_DND, GTK_STOCK_DND);
408 register_stock_icon (factory, GTK_STOCK_DND_MULTIPLE, GTK_STOCK_DND_MULTIPLE);
409 register_stock_icon (factory, GTK_STOCK_APPLY, GTK_STOCK_APPLY);
410 register_stock_icon (factory, GTK_STOCK_CANCEL, GTK_STOCK_CANCEL);
411 register_stock_icon (factory, GTK_STOCK_NO, GTK_STOCK_NO);
412 register_stock_icon (factory, GTK_STOCK_OK, GTK_STOCK_OK);
413 register_stock_icon (factory, GTK_STOCK_YES, GTK_STOCK_YES);
414 register_stock_icon (factory, GTK_STOCK_CLOSE, "window-close");
415 register_stock_icon (factory, GTK_STOCK_ADD, "list-add");
416 register_stock_icon (factory, GTK_STOCK_JUSTIFY_CENTER, "format-justify-center");
417 register_stock_icon (factory, GTK_STOCK_JUSTIFY_FILL, "format-justify-fill");
418 register_stock_icon (factory, GTK_STOCK_JUSTIFY_LEFT, "format-justify-left");
419 register_stock_icon (factory, GTK_STOCK_JUSTIFY_RIGHT, "format-justify-right");
420 register_stock_icon (factory, GTK_STOCK_GOTO_BOTTOM, "go-bottom");
421 register_stock_icon (factory, GTK_STOCK_CDROM, "media-optical");
422 register_stock_icon (factory, GTK_STOCK_CONVERT, GTK_STOCK_CONVERT);
423 register_stock_icon (factory, GTK_STOCK_COPY, "edit-copy");
424 register_stock_icon (factory, GTK_STOCK_CUT, "edit-cut");
425 register_stock_icon (factory, GTK_STOCK_GO_DOWN, "go-down");
426 register_stock_icon (factory, GTK_STOCK_EXECUTE, "system-run");
427 register_stock_icon (factory, GTK_STOCK_QUIT, "application-exit");
428 register_bidi_stock_icon (factory, GTK_STOCK_GOTO_FIRST, "go-first");
429 register_stock_icon (factory, GTK_STOCK_SELECT_FONT, GTK_STOCK_SELECT_FONT);
430 register_stock_icon (factory, GTK_STOCK_FULLSCREEN, "view-fullscreen");
431 register_stock_icon (factory, GTK_STOCK_LEAVE_FULLSCREEN, "view-restore");
432 register_stock_icon (factory, GTK_STOCK_HARDDISK, "drive-harddisk");
433 register_stock_icon (factory, GTK_STOCK_HELP, "help-contents");
434 register_stock_icon (factory, GTK_STOCK_HOME, "go-home");
435 register_stock_icon (factory, GTK_STOCK_INFO, "dialog-information");
436 register_bidi_stock_icon (factory, GTK_STOCK_JUMP_TO, "go-jump");
437 register_bidi_stock_icon (factory, GTK_STOCK_GOTO_LAST, "go-last");
438 register_bidi_stock_icon (factory, GTK_STOCK_GO_BACK, "go-previous");
439 register_stock_icon (factory, GTK_STOCK_MISSING_IMAGE, "image-missing");
440 register_stock_icon (factory, GTK_STOCK_NETWORK, "network-idle");
441 register_stock_icon (factory, GTK_STOCK_NEW, "document-new");
442 register_stock_icon (factory, GTK_STOCK_OPEN, "document-open");
443 register_stock_icon (factory, GTK_STOCK_ORIENTATION_PORTRAIT, GTK_STOCK_ORIENTATION_PORTRAIT);
444 register_stock_icon (factory, GTK_STOCK_ORIENTATION_LANDSCAPE, GTK_STOCK_ORIENTATION_LANDSCAPE);
445 register_stock_icon (factory, GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT, GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT);
446 register_stock_icon (factory, GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE, GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE);
447 register_stock_icon (factory, GTK_STOCK_PAGE_SETUP, GTK_STOCK_PAGE_SETUP);
448 register_stock_icon (factory, GTK_STOCK_PASTE, "edit-paste");
449 register_stock_icon (factory, GTK_STOCK_PREFERENCES, GTK_STOCK_PREFERENCES);
450 register_stock_icon (factory, GTK_STOCK_PRINT, "document-print");
451 register_stock_icon (factory, GTK_STOCK_PRINT_ERROR, "printer-error");
452 register_stock_icon (factory, GTK_STOCK_PRINT_PAUSED, "printer-paused");
453 register_stock_icon (factory, GTK_STOCK_PRINT_PREVIEW, "document-print-preview");
454 register_stock_icon (factory, GTK_STOCK_PRINT_REPORT, "printer-info");
455 register_stock_icon (factory, GTK_STOCK_PRINT_WARNING, "printer-warning");
456 register_stock_icon (factory, GTK_STOCK_PROPERTIES, "document-properties");
457 register_bidi_stock_icon (factory, GTK_STOCK_REDO, "edit-redo");
458 register_stock_icon (factory, GTK_STOCK_REMOVE, "list-remove");
459 register_stock_icon (factory, GTK_STOCK_REFRESH, "view-refresh");
460 register_bidi_stock_icon (factory, GTK_STOCK_REVERT_TO_SAVED, "document-revert");
461 register_bidi_stock_icon (factory, GTK_STOCK_GO_FORWARD, "go-next");
462 register_stock_icon (factory, GTK_STOCK_SAVE, "document-save");
463 register_stock_icon (factory, GTK_STOCK_FLOPPY, "media-floppy");
464 register_stock_icon (factory, GTK_STOCK_SAVE_AS, "document-save-as");
465 register_stock_icon (factory, GTK_STOCK_FIND, "edit-find");
466 register_stock_icon (factory, GTK_STOCK_FIND_AND_REPLACE, "edit-find-replace");
467 register_stock_icon (factory, GTK_STOCK_SORT_DESCENDING, "view-sort-descending");
468 register_stock_icon (factory, GTK_STOCK_SORT_ASCENDING, "view-sort-ascending");
469 register_stock_icon (factory, GTK_STOCK_SPELL_CHECK, "tools-check-spelling");
470 register_stock_icon (factory, GTK_STOCK_STOP, "process-stop");
471 register_stock_icon (factory, GTK_STOCK_BOLD, "format-text-bold");
472 register_stock_icon (factory, GTK_STOCK_ITALIC, "format-text-italic");
473 register_stock_icon (factory, GTK_STOCK_STRIKETHROUGH, "format-text-strikethrough");
474 register_stock_icon (factory, GTK_STOCK_UNDERLINE, "format-text-underline");
475 register_bidi_stock_icon (factory, GTK_STOCK_INDENT, "format-indent-more");
476 register_bidi_stock_icon (factory, GTK_STOCK_UNINDENT, "format-indent-less");
477 register_stock_icon (factory, GTK_STOCK_GOTO_TOP, "go-top");
478 register_stock_icon (factory, GTK_STOCK_DELETE, "edit-delete");
479 register_bidi_stock_icon (factory, GTK_STOCK_UNDELETE, GTK_STOCK_UNDELETE);
480 register_bidi_stock_icon (factory, GTK_STOCK_UNDO, "edit-undo");
481 register_stock_icon (factory, GTK_STOCK_GO_UP, "go-up");
482 register_stock_icon (factory, GTK_STOCK_FILE, "document-x-generic");
483 register_stock_icon (factory, GTK_STOCK_DIRECTORY, "folder");
484 register_stock_icon (factory, GTK_STOCK_ABOUT, "help-about");
485 register_stock_icon (factory, GTK_STOCK_CONNECT, GTK_STOCK_CONNECT);
486 register_stock_icon (factory, GTK_STOCK_DISCONNECT, GTK_STOCK_DISCONNECT);
487 register_stock_icon (factory, GTK_STOCK_EDIT, GTK_STOCK_EDIT);
488 register_stock_icon (factory, GTK_STOCK_CAPS_LOCK_WARNING, GTK_STOCK_CAPS_LOCK_WARNING);
489 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_FORWARD, "media-seek-forward");
490 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_NEXT, "media-skip-forward");
491 register_stock_icon (factory, GTK_STOCK_MEDIA_PAUSE, "media-playback-pause");
492 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_PLAY, "media-playback-start");
493 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_PREVIOUS, "media-skip-backward");
494 register_stock_icon (factory, GTK_STOCK_MEDIA_RECORD, "media-record");
495 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_REWIND, "media-seek-backward");
496 register_stock_icon (factory, GTK_STOCK_MEDIA_STOP, "media-playback-stop");
497 register_stock_icon (factory, GTK_STOCK_INDEX, GTK_STOCK_INDEX);
498 register_stock_icon (factory, GTK_STOCK_ZOOM_100, "zoom-original");
499 register_stock_icon (factory, GTK_STOCK_ZOOM_IN, "zoom-in");
500 register_stock_icon (factory, GTK_STOCK_ZOOM_OUT, "zoom-out");
501 register_stock_icon (factory, GTK_STOCK_ZOOM_FIT, "zoom-fit-best");
502 register_stock_icon (factory, GTK_STOCK_SELECT_ALL, "edit-select-all");
503 register_stock_icon (factory, GTK_STOCK_CLEAR, "edit-clear");
504 register_stock_icon (factory, GTK_STOCK_SELECT_COLOR, GTK_STOCK_SELECT_COLOR);
505 register_stock_icon (factory, GTK_STOCK_COLOR_PICKER, GTK_STOCK_COLOR_PICKER);
508 /************************************************************
509 * Icon size handling *
510 ************************************************************/
512 typedef struct _IconSize IconSize;
523 typedef struct _IconAlias IconAlias;
531 typedef struct _SettingsIconSize SettingsIconSize;
533 struct _SettingsIconSize
539 static GHashTable *icon_aliases = NULL;
540 static IconSize *icon_sizes = NULL;
541 static gint icon_sizes_allocated = 0;
542 static gint icon_sizes_used = 0;
545 init_icon_sizes (void)
547 if (icon_sizes == NULL)
549 #define NUM_BUILTIN_SIZES 7
552 icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
554 icon_sizes = g_new (IconSize, NUM_BUILTIN_SIZES);
555 icon_sizes_allocated = NUM_BUILTIN_SIZES;
556 icon_sizes_used = NUM_BUILTIN_SIZES;
558 icon_sizes[GTK_ICON_SIZE_INVALID].size = 0;
559 icon_sizes[GTK_ICON_SIZE_INVALID].name = NULL;
560 icon_sizes[GTK_ICON_SIZE_INVALID].width = 0;
561 icon_sizes[GTK_ICON_SIZE_INVALID].height = 0;
563 /* the name strings aren't copied since we don't ever remove
564 * icon sizes, so we don't need to know whether they're static.
565 * Even if we did I suppose removing the builtin sizes would be
569 icon_sizes[GTK_ICON_SIZE_MENU].size = GTK_ICON_SIZE_MENU;
570 icon_sizes[GTK_ICON_SIZE_MENU].name = "gtk-menu";
571 icon_sizes[GTK_ICON_SIZE_MENU].width = 16;
572 icon_sizes[GTK_ICON_SIZE_MENU].height = 16;
574 icon_sizes[GTK_ICON_SIZE_BUTTON].size = GTK_ICON_SIZE_BUTTON;
575 icon_sizes[GTK_ICON_SIZE_BUTTON].name = "gtk-button";
576 icon_sizes[GTK_ICON_SIZE_BUTTON].width = 20;
577 icon_sizes[GTK_ICON_SIZE_BUTTON].height = 20;
579 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].size = GTK_ICON_SIZE_SMALL_TOOLBAR;
580 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].name = "gtk-small-toolbar";
581 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].width = 18;
582 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].height = 18;
584 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].size = GTK_ICON_SIZE_LARGE_TOOLBAR;
585 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].name = "gtk-large-toolbar";
586 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].width = 24;
587 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].height = 24;
589 icon_sizes[GTK_ICON_SIZE_DND].size = GTK_ICON_SIZE_DND;
590 icon_sizes[GTK_ICON_SIZE_DND].name = "gtk-dnd";
591 icon_sizes[GTK_ICON_SIZE_DND].width = 32;
592 icon_sizes[GTK_ICON_SIZE_DND].height = 32;
594 icon_sizes[GTK_ICON_SIZE_DIALOG].size = GTK_ICON_SIZE_DIALOG;
595 icon_sizes[GTK_ICON_SIZE_DIALOG].name = "gtk-dialog";
596 icon_sizes[GTK_ICON_SIZE_DIALOG].width = 48;
597 icon_sizes[GTK_ICON_SIZE_DIALOG].height = 48;
599 g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
601 /* Alias everything to itself. */
602 i = 1; /* skip invalid size */
603 while (i < NUM_BUILTIN_SIZES)
605 gtk_icon_size_register_alias (icon_sizes[i].name, icon_sizes[i].size);
610 #undef NUM_BUILTIN_SIZES
615 free_settings_sizes (gpointer data)
617 g_array_free (data, TRUE);
621 get_settings_sizes (GtkSettings *settings,
624 GArray *settings_sizes;
625 static GQuark sizes_quark = 0;
628 sizes_quark = g_quark_from_static_string ("gtk-icon-sizes");
630 settings_sizes = g_object_get_qdata (G_OBJECT (settings), sizes_quark);
633 settings_sizes = g_array_new (FALSE, FALSE, sizeof (SettingsIconSize));
634 g_object_set_qdata_full (G_OBJECT (settings), sizes_quark,
635 settings_sizes, free_settings_sizes);
640 return settings_sizes;
644 icon_size_set_for_settings (GtkSettings *settings,
645 const gchar *size_name,
650 GArray *settings_sizes;
651 SettingsIconSize *settings_size;
653 g_return_if_fail (size_name != NULL);
655 size = gtk_icon_size_from_name (size_name);
656 if (size == GTK_ICON_SIZE_INVALID)
657 /* Reserve a place */
658 size = icon_size_register_intern (size_name, -1, -1);
660 settings_sizes = get_settings_sizes (settings, NULL);
661 if (size >= settings_sizes->len)
663 SettingsIconSize unset = { -1, -1 };
666 for (i = settings_sizes->len; i <= size; i++)
667 g_array_append_val (settings_sizes, unset);
670 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
672 settings_size->width = width;
673 settings_size->height = height;
676 /* Like pango_parse_word, but accept - as well
679 scan_icon_size_name (const char **pos, GString *out)
681 const char *p = *pos;
683 while (g_ascii_isspace (*p))
686 if (!((*p >= 'A' && *p <= 'Z') ||
687 (*p >= 'a' && *p <= 'z') ||
688 *p == '_' || *p == '-'))
691 g_string_truncate (out, 0);
692 g_string_append_c (out, *p);
695 while ((*p >= 'A' && *p <= 'Z') ||
696 (*p >= 'a' && *p <= 'z') ||
697 (*p >= '0' && *p <= '9') ||
698 *p == '_' || *p == '-')
700 g_string_append_c (out, *p);
710 icon_size_setting_parse (GtkSettings *settings,
711 const gchar *icon_size_string)
713 GString *name_buf = g_string_new (NULL);
714 const gchar *p = icon_size_string;
716 while (pango_skip_space (&p))
720 if (!scan_icon_size_name (&p, name_buf))
723 if (!pango_skip_space (&p))
731 if (!pango_scan_int (&p, &width))
734 if (!pango_skip_space (&p))
742 if (!pango_scan_int (&p, &height))
745 if (width > 0 && height > 0)
747 icon_size_set_for_settings (settings, name_buf->str,
752 g_warning ("Invalid size in gtk-icon-sizes: %d,%d\n", width, height);
755 pango_skip_space (&p);
764 g_string_free (name_buf, TRUE);
768 g_warning ("Error parsing gtk-icon-sizes string:\n\t'%s'", icon_size_string);
769 g_string_free (name_buf, TRUE);
773 icon_size_set_all_from_settings (GtkSettings *settings)
775 GArray *settings_sizes;
776 gchar *icon_size_string;
778 /* Reset old settings */
779 settings_sizes = get_settings_sizes (settings, NULL);
780 g_array_set_size (settings_sizes, 0);
782 g_object_get (settings,
783 "gtk-icon-sizes", &icon_size_string,
786 if (icon_size_string)
788 icon_size_setting_parse (settings, icon_size_string);
789 g_free (icon_size_string);
794 icon_size_settings_changed (GtkSettings *settings,
797 icon_size_set_all_from_settings (settings);
799 gtk_rc_reset_styles (settings);
803 icon_sizes_init_for_settings (GtkSettings *settings)
805 g_signal_connect (settings,
806 "notify::gtk-icon-sizes",
807 G_CALLBACK (icon_size_settings_changed),
810 icon_size_set_all_from_settings (settings);
814 icon_size_lookup_intern (GtkSettings *settings,
819 GArray *settings_sizes;
820 gint width_for_settings = -1;
821 gint height_for_settings = -1;
825 if (size == (GtkIconSize)-1)
828 if (size >= icon_sizes_used)
831 if (size == GTK_ICON_SIZE_INVALID)
836 gboolean initial = FALSE;
838 settings_sizes = get_settings_sizes (settings, &initial);
841 icon_sizes_init_for_settings (settings);
843 if (size < settings_sizes->len)
845 SettingsIconSize *settings_size;
847 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
849 width_for_settings = settings_size->width;
850 height_for_settings = settings_size->height;
855 *widthp = width_for_settings >= 0 ? width_for_settings : icon_sizes[size].width;
858 *heightp = height_for_settings >= 0 ? height_for_settings : icon_sizes[size].height;
864 * gtk_icon_size_lookup_for_settings:
865 * @settings: a #GtkSettings object, used to determine
866 * which set of user preferences to used.
867 * @size: (type int): an icon size
868 * @width: location to store icon width
869 * @height: location to store icon height
871 * Obtains the pixel size of a semantic icon size, possibly
872 * modified by user preferences for a particular
873 * #GtkSettings. Normally @size would be
874 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
875 * isn't normally needed, gtk_widget_render_icon() is the usual
876 * way to get an icon for rendering, then just look at the size of
877 * the rendered pixbuf. The rendered pixbuf may not even correspond to
878 * the width/height returned by gtk_icon_size_lookup(), because themes
879 * are free to render the pixbuf however they like, including changing
882 * Return value: %TRUE if @size was a valid size
887 gtk_icon_size_lookup_for_settings (GtkSettings *settings,
892 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
894 return icon_size_lookup_intern (settings, size, width, height);
898 * gtk_icon_size_lookup:
899 * @size: (type int): an icon size
900 * @width: location to store icon width
901 * @height: location to store icon height
903 * Obtains the pixel size of a semantic icon size, possibly
904 * modified by user preferences for the default #GtkSettings.
905 * (See gtk_icon_size_lookup_for_settings().)
906 * Normally @size would be
907 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
908 * isn't normally needed, gtk_widget_render_icon() is the usual
909 * way to get an icon for rendering, then just look at the size of
910 * the rendered pixbuf. The rendered pixbuf may not even correspond to
911 * the width/height returned by gtk_icon_size_lookup(), because themes
912 * are free to render the pixbuf however they like, including changing
915 * Return value: %TRUE if @size was a valid size
918 gtk_icon_size_lookup (GtkIconSize size,
923 g_warning ("gtk_icon_size_lookup ()) is not multihead safe"));
925 return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (),
926 size, widthp, heightp);
930 icon_size_register_intern (const gchar *name,
934 IconAlias *old_alias;
939 old_alias = g_hash_table_lookup (icon_aliases, name);
940 if (old_alias && icon_sizes[old_alias->target].width > 0)
942 g_warning ("Icon size name '%s' already exists", name);
943 return GTK_ICON_SIZE_INVALID;
948 size = old_alias->target;
952 if (icon_sizes_used == icon_sizes_allocated)
954 icon_sizes_allocated *= 2;
955 icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated);
958 size = icon_sizes_used++;
961 gtk_icon_size_register_alias (name, size);
963 icon_sizes[size].size = size;
964 icon_sizes[size].name = g_strdup (name);
967 icon_sizes[size].width = width;
968 icon_sizes[size].height = height;
974 * gtk_icon_size_register:
975 * @name: name of the icon size
976 * @width: the icon width
977 * @height: the icon height
979 * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
980 * etc. Returns the integer value for the size.
982 * Returns: (type int): integer value representing the size
985 gtk_icon_size_register (const gchar *name,
989 g_return_val_if_fail (name != NULL, 0);
990 g_return_val_if_fail (width > 0, 0);
991 g_return_val_if_fail (height > 0, 0);
993 return icon_size_register_intern (name, width, height);
997 * gtk_icon_size_register_alias:
998 * @alias: an alias for @target
999 * @target: (type int): an existing icon size
1001 * Registers @alias as another name for @target.
1002 * So calling gtk_icon_size_from_name() with @alias as argument
1003 * will return @target.
1006 gtk_icon_size_register_alias (const gchar *alias,
1011 g_return_if_fail (alias != NULL);
1015 if (!icon_size_lookup_intern (NULL, target, NULL, NULL))
1016 g_warning ("gtk_icon_size_register_alias: Icon size %u does not exist", target);
1018 ia = g_hash_table_lookup (icon_aliases, alias);
1021 if (icon_sizes[ia->target].width > 0)
1023 g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias);
1027 ia->target = target;
1032 ia = g_new (IconAlias, 1);
1033 ia->name = g_strdup (alias);
1034 ia->target = target;
1036 g_hash_table_insert (icon_aliases, ia->name, ia);
1041 * gtk_icon_size_from_name:
1042 * @name: the name to look up.
1044 * Looks up the icon size associated with @name.
1046 * Return value: (type int): the icon size
1049 gtk_icon_size_from_name (const gchar *name)
1055 ia = g_hash_table_lookup (icon_aliases, name);
1057 if (ia && icon_sizes[ia->target].width > 0)
1060 return GTK_ICON_SIZE_INVALID;
1064 * gtk_icon_size_get_name:
1065 * @size: (type int): a #GtkIconSize.
1066 * @returns: the name of the given icon size.
1068 * Gets the canonical name of the given icon size. The returned string
1069 * is statically allocated and should not be freed.
1071 G_CONST_RETURN gchar*
1072 gtk_icon_size_get_name (GtkIconSize size)
1074 if (size >= icon_sizes_used)
1077 return icon_sizes[size].name;
1080 /************************************************************/
1085 static GdkPixbuf *find_in_cache (GtkIconSet *icon_set,
1087 GtkTextDirection direction,
1090 static void add_to_cache (GtkIconSet *icon_set,
1092 GtkTextDirection direction,
1096 /* Clear icon set contents, drop references to all contained
1097 * GdkPixbuf objects and forget all GtkIconSources. Used to
1098 * recycle an icon set.
1100 static void clear_cache (GtkIconSet *icon_set,
1101 gboolean style_detach);
1102 static GSList* copy_cache (GtkIconSet *icon_set,
1103 GtkIconSet *copy_recipient);
1104 static void attach_to_style (GtkIconSet *icon_set,
1106 static void detach_from_style (GtkIconSet *icon_set,
1108 static void style_dnotify (gpointer data);
1116 /* Cache of the last few rendered versions of the icon. */
1124 static guint cache_serial = 0;
1129 * Creates a new #GtkIconSet. A #GtkIconSet represents a single icon
1130 * in various sizes and widget states. It can provide a #GdkPixbuf
1131 * for a given size and state on request, and automatically caches
1132 * some of the rendered #GdkPixbuf objects.
1134 * Normally you would use gtk_widget_render_icon() instead of
1135 * using #GtkIconSet directly. The one case where you'd use
1136 * #GtkIconSet is to create application-specific icon sets to place in
1137 * a #GtkIconFactory.
1139 * Return value: a new #GtkIconSet
1142 gtk_icon_set_new (void)
1144 GtkIconSet *icon_set;
1146 icon_set = g_new (GtkIconSet, 1);
1148 icon_set->ref_count = 1;
1149 icon_set->sources = NULL;
1150 icon_set->cache = NULL;
1151 icon_set->cache_size = 0;
1152 icon_set->cache_serial = cache_serial;
1158 * gtk_icon_set_new_from_pixbuf:
1159 * @pixbuf: a #GdkPixbuf
1161 * Creates a new #GtkIconSet with @pixbuf as the default/fallback
1162 * source image. If you don't add any additional #GtkIconSource to the
1163 * icon set, all variants of the icon will be created from @pixbuf,
1164 * using scaling, pixelation, etc. as required to adjust the icon size
1165 * or make the icon look insensitive/prelighted.
1167 * Return value: a new #GtkIconSet
1170 gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
1174 GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1176 g_return_val_if_fail (pixbuf != NULL, NULL);
1178 set = gtk_icon_set_new ();
1180 gtk_icon_source_set_pixbuf (&source, pixbuf);
1181 gtk_icon_set_add_source (set, &source);
1182 gtk_icon_source_set_pixbuf (&source, NULL);
1190 * @icon_set: a #GtkIconSet.
1192 * Increments the reference count on @icon_set.
1194 * Return value: @icon_set.
1197 gtk_icon_set_ref (GtkIconSet *icon_set)
1199 g_return_val_if_fail (icon_set != NULL, NULL);
1200 g_return_val_if_fail (icon_set->ref_count > 0, NULL);
1202 icon_set->ref_count += 1;
1208 * gtk_icon_set_unref:
1209 * @icon_set: a #GtkIconSet
1211 * Decrements the reference count on @icon_set, and frees memory
1212 * if the reference count reaches 0.
1215 gtk_icon_set_unref (GtkIconSet *icon_set)
1217 g_return_if_fail (icon_set != NULL);
1218 g_return_if_fail (icon_set->ref_count > 0);
1220 icon_set->ref_count -= 1;
1222 if (icon_set->ref_count == 0)
1224 GSList *tmp_list = icon_set->sources;
1225 while (tmp_list != NULL)
1227 gtk_icon_source_free (tmp_list->data);
1229 tmp_list = g_slist_next (tmp_list);
1231 g_slist_free (icon_set->sources);
1233 clear_cache (icon_set, TRUE);
1239 G_DEFINE_BOXED_TYPE (GtkIconSet, gtk_icon_set,
1244 * gtk_icon_set_copy:
1245 * @icon_set: a #GtkIconSet
1247 * Copies @icon_set by value.
1249 * Return value: a new #GtkIconSet identical to the first.
1252 gtk_icon_set_copy (GtkIconSet *icon_set)
1257 copy = gtk_icon_set_new ();
1259 tmp_list = icon_set->sources;
1260 while (tmp_list != NULL)
1262 copy->sources = g_slist_prepend (copy->sources,
1263 gtk_icon_source_copy (tmp_list->data));
1265 tmp_list = g_slist_next (tmp_list);
1268 copy->sources = g_slist_reverse (copy->sources);
1270 copy->cache = copy_cache (icon_set, copy);
1271 copy->cache_size = icon_set->cache_size;
1272 copy->cache_serial = icon_set->cache_serial;
1278 sizes_equivalent (GtkIconSize lhs,
1281 /* We used to consider sizes equivalent if they were
1282 * the same pixel size, but we don't have the GtkSettings
1283 * here, so we can't do that. Plus, it's not clear that
1284 * it is right... it was just a workaround for the fact
1285 * that we register icons by logical size, not pixel size.
1291 gint r_w, r_h, l_w, l_h;
1293 icon_size_lookup_intern (NULL, rhs, &r_w, &r_h);
1294 icon_size_lookup_intern (NULL, lhs, &l_w, &l_h);
1296 return r_w == l_w && r_h == l_h;
1300 static GtkIconSource *
1301 find_best_matching_source (GtkIconSet *icon_set,
1302 GtkTextDirection direction,
1307 GtkIconSource *source;
1310 /* We need to find the best icon source. Direction matters more
1311 * than state, state matters more than size. icon_set->sources
1312 * is sorted according to wildness, so if we take the first
1313 * match we find it will be the least-wild match (if there are
1314 * multiple matches for a given "wildness" then the RC file contained
1315 * dumb stuff, and we end up with an arbitrary matching source)
1319 tmp_list = icon_set->sources;
1320 while (tmp_list != NULL)
1322 GtkIconSource *s = tmp_list->data;
1324 if ((s->any_direction || (s->direction == direction)) &&
1325 (s->any_state || (s->state == state)) &&
1326 (s->any_size || size == (GtkIconSize)-1 || (sizes_equivalent (size, s->size))))
1328 if (!g_slist_find (failed, s))
1335 tmp_list = g_slist_next (tmp_list);
1342 ensure_filename_pixbuf (GtkIconSet *icon_set,
1343 GtkIconSource *source)
1345 if (source->filename_pixbuf == NULL)
1347 GError *error = NULL;
1349 source->filename_pixbuf = gdk_pixbuf_new_from_file (source->source.filename, &error);
1351 if (source->filename_pixbuf == NULL)
1353 /* Remove this icon source so we don't keep trying to
1356 g_warning (_("Error loading icon: %s"), error->message);
1357 g_error_free (error);
1359 icon_set->sources = g_slist_remove (icon_set->sources, source);
1361 gtk_icon_source_free (source);
1371 render_icon_name_pixbuf (GtkIconSource *icon_source,
1373 GtkTextDirection direction,
1380 GdkPixbuf *tmp_pixbuf;
1381 GtkIconSource tmp_source;
1383 GtkIconTheme *icon_theme;
1384 GtkSettings *settings;
1385 gint width, height, pixel_size;
1386 gint *sizes, *s, dist;
1387 GError *error = NULL;
1389 if (widget && gtk_widget_has_screen (widget))
1390 screen = gtk_widget_get_screen (widget);
1391 else if (style && style->colormap)
1392 screen = gdk_colormap_get_screen (style->colormap);
1395 screen = gdk_screen_get_default ();
1396 GTK_NOTE (MULTIHEAD,
1397 g_warning ("Using the default screen for gtk_icon_source_render_icon()"));
1400 icon_theme = gtk_icon_theme_get_for_screen (screen);
1401 settings = gtk_settings_get_for_screen (screen);
1403 if (!gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
1405 if (size == (GtkIconSize)-1)
1407 /* Find an available size close to 48 */
1408 sizes = gtk_icon_theme_get_icon_sizes (icon_theme, icon_source->source.icon_name);
1410 width = height = 48;
1411 for (s = sizes; *s; s++)
1415 width = height = 48;
1422 width = height = *s;
1430 width = height = *s;
1440 g_warning ("Invalid icon size %u\n", size);
1441 width = height = 24;
1445 pixel_size = MIN (width, height);
1447 if (icon_source->direction != GTK_TEXT_DIR_NONE)
1449 gchar *suffix[3] = { NULL, "-ltr", "-rtl" };
1453 names[0] = g_strconcat (icon_source->source.icon_name, suffix[icon_source->direction], NULL);
1454 names[1] = icon_source->source.icon_name;
1457 info = gtk_icon_theme_choose_icon (icon_theme,
1458 (const char **) names,
1459 pixel_size, GTK_ICON_LOOKUP_USE_BUILTIN);
1463 tmp_pixbuf = gtk_icon_info_load_icon (info, &error);
1464 gtk_icon_info_free (info);
1469 tmp_pixbuf = gtk_icon_theme_load_icon (icon_theme,
1470 icon_source->source.icon_name,
1477 g_warning ("Error loading theme icon '%s' for stock: %s",
1478 icon_source->source.icon_name, error ? error->message : "");
1480 g_error_free (error);
1484 tmp_source = *icon_source;
1485 tmp_source.type = GTK_ICON_SOURCE_PIXBUF;
1486 tmp_source.source.pixbuf = tmp_pixbuf;
1488 pixbuf = gtk_style_render_icon (style, &tmp_source,
1489 direction, state, -1,
1493 g_warning ("Failed to render icon");
1495 g_object_unref (tmp_pixbuf);
1501 find_and_render_icon_source (GtkIconSet *icon_set,
1503 GtkTextDirection direction,
1509 GSList *failed = NULL;
1510 GdkPixbuf *pixbuf = NULL;
1512 /* We treat failure in two different ways:
1514 * A) If loading a source that specifies a filename fails,
1515 * we treat that as permanent, and remove the source
1516 * from the GtkIconSet. (in ensure_filename_pixbuf ()
1517 * B) If loading a themed icon fails, or scaling an icon
1518 * fails, we treat that as transient and will try
1519 * again next time the icon falls out of the cache
1520 * and we need to recreate it.
1522 while (pixbuf == NULL)
1524 GtkIconSource *source = find_best_matching_source (icon_set, direction, state, size, failed);
1529 switch (source->type)
1531 case GTK_ICON_SOURCE_FILENAME:
1532 if (!ensure_filename_pixbuf (icon_set, source))
1535 case GTK_ICON_SOURCE_PIXBUF:
1536 pixbuf = gtk_style_render_icon (style, source,
1537 direction, state, size,
1541 g_warning ("Failed to render icon");
1542 failed = g_slist_prepend (failed, source);
1545 case GTK_ICON_SOURCE_ICON_NAME:
1546 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1547 pixbuf = render_icon_name_pixbuf (source, style,
1548 direction, state, size,
1551 failed = g_slist_prepend (failed, source);
1553 case GTK_ICON_SOURCE_EMPTY:
1554 g_assert_not_reached ();
1558 g_slist_free (failed);
1563 extern GtkIconCache *_builtin_cache;
1566 render_fallback_image (GtkStyle *style,
1567 GtkTextDirection direction,
1573 /* This icon can be used for any direction/state/size */
1574 static GtkIconSource fallback_source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1576 if (fallback_source.type == GTK_ICON_SOURCE_EMPTY)
1581 _gtk_icon_theme_ensure_builtin_cache ();
1583 index = _gtk_icon_cache_get_directory_index (_builtin_cache, "24");
1584 pixbuf = _gtk_icon_cache_get_icon (_builtin_cache,
1585 GTK_STOCK_MISSING_IMAGE,
1587 gtk_icon_source_set_pixbuf (&fallback_source, pixbuf);
1588 g_object_unref (pixbuf);
1591 return gtk_style_render_icon (style,
1601 * gtk_icon_set_render_icon:
1602 * @icon_set: a #GtkIconSet
1603 * @style: (allow-none): a #GtkStyle associated with @widget, or %NULL
1604 * @direction: text direction
1605 * @state: widget state
1606 * @size: (type int): icon size. A size of (GtkIconSize)-1
1607 * means render at the size of the source and don't scale.
1608 * @widget: (allow-none): widget that will display the icon, or %NULL.
1609 * The only use that is typically made of this
1610 * is to determine the appropriate #GdkScreen.
1611 * @detail: (allow-none): detail to pass to the theme engine, or %NULL.
1612 * Note that passing a detail of anything but %NULL
1613 * will disable caching.
1615 * Renders an icon using gtk_style_render_icon(). In most cases,
1616 * gtk_widget_render_icon() is better, since it automatically provides
1617 * most of the arguments from the current widget settings. This
1618 * function never returns %NULL; if the icon can't be rendered
1619 * (perhaps because an image file fails to load), a default "missing
1620 * image" icon will be returned instead.
1622 * Return value: (transfer full): a #GdkPixbuf to be displayed
1625 gtk_icon_set_render_icon (GtkIconSet *icon_set,
1627 GtkTextDirection direction,
1635 g_return_val_if_fail (icon_set != NULL, NULL);
1636 g_return_val_if_fail (style == NULL || GTK_IS_STYLE (style), NULL);
1638 if (icon_set->sources == NULL)
1639 return render_fallback_image (style, direction, state, size, widget, detail);
1643 icon = find_in_cache (icon_set, style, direction,
1648 g_object_ref (icon);
1654 icon = find_and_render_icon_source (icon_set, style, direction, state, size,
1658 icon = render_fallback_image (style, direction, state, size, widget, detail);
1661 add_to_cache (icon_set, style, direction, state, size, icon);
1666 /* Order sources by their "wildness", so that "wilder" sources are
1667 * greater than "specific" sources; for determining ordering,
1668 * direction beats state beats size.
1672 icon_source_compare (gconstpointer ap, gconstpointer bp)
1674 const GtkIconSource *a = ap;
1675 const GtkIconSource *b = bp;
1677 if (!a->any_direction && b->any_direction)
1679 else if (a->any_direction && !b->any_direction)
1681 else if (!a->any_state && b->any_state)
1683 else if (a->any_state && !b->any_state)
1685 else if (!a->any_size && b->any_size)
1687 else if (a->any_size && !b->any_size)
1694 * gtk_icon_set_add_source:
1695 * @icon_set: a #GtkIconSet
1696 * @source: a #GtkIconSource
1698 * Icon sets have a list of #GtkIconSource, which they use as base
1699 * icons for rendering icons in different states and sizes. Icons are
1700 * scaled, made to look insensitive, etc. in
1701 * gtk_icon_set_render_icon(), but #GtkIconSet needs base images to
1702 * work with. The base images and when to use them are described by
1705 * This function copies @source, so you can reuse the same source immediately
1706 * without affecting the icon set.
1708 * An example of when you'd use this function: a web browser's "Back
1709 * to Previous Page" icon might point in a different direction in
1710 * Hebrew and in English; it might look different when insensitive;
1711 * and it might change size depending on toolbar mode (small/large
1712 * icons). So a single icon set would contain all those variants of
1713 * the icon, and you might add a separate source for each one.
1715 * You should nearly always add a "default" icon source with all
1716 * fields wildcarded, which will be used as a fallback if no more
1717 * specific source matches. #GtkIconSet always prefers more specific
1718 * icon sources to more generic icon sources. The order in which you
1719 * add the sources to the icon set does not matter.
1721 * gtk_icon_set_new_from_pixbuf() creates a new icon set with a
1722 * default icon source based on the given pixbuf.
1725 gtk_icon_set_add_source (GtkIconSet *icon_set,
1726 const GtkIconSource *source)
1728 g_return_if_fail (icon_set != NULL);
1729 g_return_if_fail (source != NULL);
1731 if (source->type == GTK_ICON_SOURCE_EMPTY)
1733 g_warning ("Useless empty GtkIconSource");
1737 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
1738 gtk_icon_source_copy (source),
1739 icon_source_compare);
1743 * gtk_icon_set_get_sizes:
1744 * @icon_set: a #GtkIconSet
1745 * @sizes: (array length=n_sizes) (out) (type int): return location
1746 * for array of sizes
1747 * @n_sizes: location to store number of elements in returned array
1749 * Obtains a list of icon sizes this icon set can render. The returned
1750 * array must be freed with g_free().
1753 gtk_icon_set_get_sizes (GtkIconSet *icon_set,
1754 GtkIconSize **sizes,
1758 gboolean all_sizes = FALSE;
1759 GSList *specifics = NULL;
1761 g_return_if_fail (icon_set != NULL);
1762 g_return_if_fail (sizes != NULL);
1763 g_return_if_fail (n_sizes != NULL);
1765 tmp_list = icon_set->sources;
1766 while (tmp_list != NULL)
1768 GtkIconSource *source;
1770 source = tmp_list->data;
1772 if (source->any_size)
1778 specifics = g_slist_prepend (specifics, GINT_TO_POINTER (source->size));
1780 tmp_list = g_slist_next (tmp_list);
1785 /* Need to find out what sizes exist */
1790 *sizes = g_new (GtkIconSize, icon_sizes_used);
1791 *n_sizes = icon_sizes_used - 1;
1794 while (i < icon_sizes_used)
1796 (*sizes)[i - 1] = icon_sizes[i].size;
1804 *n_sizes = g_slist_length (specifics);
1805 *sizes = g_new (GtkIconSize, *n_sizes);
1808 tmp_list = specifics;
1809 while (tmp_list != NULL)
1811 (*sizes)[i] = GPOINTER_TO_INT (tmp_list->data);
1814 tmp_list = g_slist_next (tmp_list);
1818 g_slist_free (specifics);
1823 * gtk_icon_source_new:
1825 * Creates a new #GtkIconSource. A #GtkIconSource contains a #GdkPixbuf (or
1826 * image filename) that serves as the base image for one or more of the
1827 * icons in a #GtkIconSet, along with a specification for which icons in the
1828 * icon set will be based on that pixbuf or image file. An icon set contains
1829 * a set of icons that represent "the same" logical concept in different states,
1830 * different global text directions, and different sizes.
1832 * So for example a web browser's "Back to Previous Page" icon might
1833 * point in a different direction in Hebrew and in English; it might
1834 * look different when insensitive; and it might change size depending
1835 * on toolbar mode (small/large icons). So a single icon set would
1836 * contain all those variants of the icon. #GtkIconSet contains a list
1837 * of #GtkIconSource from which it can derive specific icon variants in
1840 * In the simplest case, #GtkIconSet contains one source pixbuf from
1841 * which it derives all variants. The convenience function
1842 * gtk_icon_set_new_from_pixbuf() handles this case; if you only have
1843 * one source pixbuf, just use that function.
1845 * If you want to use a different base pixbuf for different icon
1846 * variants, you create multiple icon sources, mark which variants
1847 * they'll be used to create, and add them to the icon set with
1848 * gtk_icon_set_add_source().
1850 * By default, the icon source has all parameters wildcarded. That is,
1851 * the icon source will be used as the base icon for any desired text
1852 * direction, widget state, or icon size.
1854 * Return value: a new #GtkIconSource
1857 gtk_icon_source_new (void)
1861 src = g_new0 (GtkIconSource, 1);
1863 src->direction = GTK_TEXT_DIR_NONE;
1864 src->size = GTK_ICON_SIZE_INVALID;
1865 src->state = GTK_STATE_NORMAL;
1867 src->any_direction = TRUE;
1868 src->any_state = TRUE;
1869 src->any_size = TRUE;
1875 * gtk_icon_source_copy:
1876 * @source: a #GtkIconSource
1878 * Creates a copy of @source; mostly useful for language bindings.
1880 * Return value: a new #GtkIconSource
1883 gtk_icon_source_copy (const GtkIconSource *source)
1885 GtkIconSource *copy;
1887 g_return_val_if_fail (source != NULL, NULL);
1889 copy = g_new (GtkIconSource, 1);
1895 case GTK_ICON_SOURCE_EMPTY:
1896 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1898 case GTK_ICON_SOURCE_ICON_NAME:
1899 copy->source.icon_name = g_strdup (copy->source.icon_name);
1901 case GTK_ICON_SOURCE_FILENAME:
1902 copy->source.filename = g_strdup (copy->source.filename);
1903 if (copy->filename_pixbuf)
1904 g_object_ref (copy->filename_pixbuf);
1906 case GTK_ICON_SOURCE_PIXBUF:
1907 g_object_ref (copy->source.pixbuf);
1910 g_assert_not_reached();
1917 * gtk_icon_source_free:
1918 * @source: a #GtkIconSource
1920 * Frees a dynamically-allocated icon source, along with its
1921 * filename, size, and pixbuf fields if those are not %NULL.
1924 gtk_icon_source_free (GtkIconSource *source)
1926 g_return_if_fail (source != NULL);
1928 icon_source_clear (source);
1932 G_DEFINE_BOXED_TYPE (GtkIconSource, gtk_icon_source,
1933 gtk_icon_source_copy,
1934 gtk_icon_source_free)
1937 icon_source_clear (GtkIconSource *source)
1939 switch (source->type)
1941 case GTK_ICON_SOURCE_EMPTY:
1943 case GTK_ICON_SOURCE_ICON_NAME:
1944 g_free (source->source.icon_name);
1946 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1947 source->source.icon_name = NULL;
1949 case GTK_ICON_SOURCE_FILENAME:
1950 g_free (source->source.filename);
1951 source->source.filename = NULL;
1952 if (source->filename_pixbuf)
1953 g_object_unref (source->filename_pixbuf);
1954 source->filename_pixbuf = NULL;
1956 case GTK_ICON_SOURCE_PIXBUF:
1957 g_object_unref (source->source.pixbuf);
1958 source->source.pixbuf = NULL;
1961 g_assert_not_reached();
1964 source->type = GTK_ICON_SOURCE_EMPTY;
1968 * gtk_icon_source_set_filename:
1969 * @source: a #GtkIconSource
1970 * @filename: image file to use
1972 * Sets the name of an image file to use as a base image when creating
1973 * icon variants for #GtkIconSet. The filename must be absolute.
1976 gtk_icon_source_set_filename (GtkIconSource *source,
1977 const gchar *filename)
1979 g_return_if_fail (source != NULL);
1980 g_return_if_fail (filename == NULL || g_path_is_absolute (filename));
1982 if (source->type == GTK_ICON_SOURCE_FILENAME &&
1983 source->source.filename == filename)
1986 icon_source_clear (source);
1988 if (filename != NULL)
1990 source->type = GTK_ICON_SOURCE_FILENAME;
1991 source->source.filename = g_strdup (filename);
1996 * gtk_icon_source_set_icon_name
1997 * @source: a #GtkIconSource
1998 * @icon_name: (allow-none): name of icon to use
2000 * Sets the name of an icon to look up in the current icon theme
2001 * to use as a base image when creating icon variants for #GtkIconSet.
2004 gtk_icon_source_set_icon_name (GtkIconSource *source,
2005 const gchar *icon_name)
2007 g_return_if_fail (source != NULL);
2009 if (source->type == GTK_ICON_SOURCE_ICON_NAME &&
2010 source->source.icon_name == icon_name)
2013 icon_source_clear (source);
2015 if (icon_name != NULL)
2017 source->type = GTK_ICON_SOURCE_ICON_NAME;
2018 source->source.icon_name = g_strdup (icon_name);
2023 * gtk_icon_source_set_pixbuf:
2024 * @source: a #GtkIconSource
2025 * @pixbuf: pixbuf to use as a source
2027 * Sets a pixbuf to use as a base image when creating icon variants
2031 gtk_icon_source_set_pixbuf (GtkIconSource *source,
2034 g_return_if_fail (source != NULL);
2035 g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
2037 if (source->type == GTK_ICON_SOURCE_PIXBUF &&
2038 source->source.pixbuf == pixbuf)
2041 icon_source_clear (source);
2045 source->type = GTK_ICON_SOURCE_PIXBUF;
2046 source->source.pixbuf = g_object_ref (pixbuf);
2051 * gtk_icon_source_get_filename:
2052 * @source: a #GtkIconSource
2054 * Retrieves the source filename, or %NULL if none is set. The
2055 * filename is not a copy, and should not be modified or expected to
2056 * persist beyond the lifetime of the icon source.
2058 * Return value: image filename. This string must not be modified
2061 G_CONST_RETURN gchar*
2062 gtk_icon_source_get_filename (const GtkIconSource *source)
2064 g_return_val_if_fail (source != NULL, NULL);
2066 if (source->type == GTK_ICON_SOURCE_FILENAME)
2067 return source->source.filename;
2073 * gtk_icon_source_get_icon_name:
2074 * @source: a #GtkIconSource
2076 * Retrieves the source icon name, or %NULL if none is set. The
2077 * icon_name is not a copy, and should not be modified or expected to
2078 * persist beyond the lifetime of the icon source.
2080 * Return value: icon name. This string must not be modified or freed.
2082 G_CONST_RETURN gchar*
2083 gtk_icon_source_get_icon_name (const GtkIconSource *source)
2085 g_return_val_if_fail (source != NULL, NULL);
2087 if (source->type == GTK_ICON_SOURCE_ICON_NAME ||
2088 source->type == GTK_ICON_SOURCE_STATIC_ICON_NAME)
2089 return source->source.icon_name;
2095 * gtk_icon_source_get_pixbuf:
2096 * @source: a #GtkIconSource
2098 * Retrieves the source pixbuf, or %NULL if none is set.
2099 * In addition, if a filename source is in use, this
2100 * function in some cases will return the pixbuf from
2101 * loaded from the filename. This is, for example, true
2102 * for the GtkIconSource passed to the GtkStyle::render_icon()
2103 * virtual function. The reference count on the pixbuf is
2106 * Return value: (transfer none): source pixbuf
2109 gtk_icon_source_get_pixbuf (const GtkIconSource *source)
2111 g_return_val_if_fail (source != NULL, NULL);
2113 if (source->type == GTK_ICON_SOURCE_PIXBUF)
2114 return source->source.pixbuf;
2115 else if (source->type == GTK_ICON_SOURCE_FILENAME)
2116 return source->filename_pixbuf;
2122 * gtk_icon_source_set_direction_wildcarded:
2123 * @source: a #GtkIconSource
2124 * @setting: %TRUE to wildcard the text direction
2126 * If the text direction is wildcarded, this source can be used
2127 * as the base image for an icon in any #GtkTextDirection.
2128 * If the text direction is not wildcarded, then the
2129 * text direction the icon source applies to should be set
2130 * with gtk_icon_source_set_direction(), and the icon source
2131 * will only be used with that text direction.
2133 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2134 * wildcarded sources, and will use an exact match when possible.
2137 gtk_icon_source_set_direction_wildcarded (GtkIconSource *source,
2140 g_return_if_fail (source != NULL);
2142 source->any_direction = setting != FALSE;
2146 * gtk_icon_source_set_state_wildcarded:
2147 * @source: a #GtkIconSource
2148 * @setting: %TRUE to wildcard the widget state
2150 * If the widget state is wildcarded, this source can be used as the
2151 * base image for an icon in any #GtkStateType. If the widget state
2152 * is not wildcarded, then the state the source applies to should be
2153 * set with gtk_icon_source_set_state() and the icon source will
2154 * only be used with that specific state.
2156 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2157 * wildcarded sources, and will use an exact match when possible.
2159 * #GtkIconSet will normally transform wildcarded source images to
2160 * produce an appropriate icon for a given state, for example
2161 * lightening an image on prelight, but will not modify source images
2162 * that match exactly.
2165 gtk_icon_source_set_state_wildcarded (GtkIconSource *source,
2168 g_return_if_fail (source != NULL);
2170 source->any_state = setting != FALSE;
2175 * gtk_icon_source_set_size_wildcarded:
2176 * @source: a #GtkIconSource
2177 * @setting: %TRUE to wildcard the widget state
2179 * If the icon size is wildcarded, this source can be used as the base
2180 * image for an icon of any size. If the size is not wildcarded, then
2181 * the size the source applies to should be set with
2182 * gtk_icon_source_set_size() and the icon source will only be used
2183 * with that specific size.
2185 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2186 * wildcarded sources, and will use an exact match when possible.
2188 * #GtkIconSet will normally scale wildcarded source images to produce
2189 * an appropriate icon at a given size, but will not change the size
2190 * of source images that match exactly.
2193 gtk_icon_source_set_size_wildcarded (GtkIconSource *source,
2196 g_return_if_fail (source != NULL);
2198 source->any_size = setting != FALSE;
2202 * gtk_icon_source_get_size_wildcarded:
2203 * @source: a #GtkIconSource
2205 * Gets the value set by gtk_icon_source_set_size_wildcarded().
2207 * Return value: %TRUE if this icon source is a base for any icon size variant
2210 gtk_icon_source_get_size_wildcarded (const GtkIconSource *source)
2212 g_return_val_if_fail (source != NULL, TRUE);
2214 return source->any_size;
2218 * gtk_icon_source_get_state_wildcarded:
2219 * @source: a #GtkIconSource
2221 * Gets the value set by gtk_icon_source_set_state_wildcarded().
2223 * Return value: %TRUE if this icon source is a base for any widget state variant
2226 gtk_icon_source_get_state_wildcarded (const GtkIconSource *source)
2228 g_return_val_if_fail (source != NULL, TRUE);
2230 return source->any_state;
2234 * gtk_icon_source_get_direction_wildcarded:
2235 * @source: a #GtkIconSource
2237 * Gets the value set by gtk_icon_source_set_direction_wildcarded().
2239 * Return value: %TRUE if this icon source is a base for any text direction variant
2242 gtk_icon_source_get_direction_wildcarded (const GtkIconSource *source)
2244 g_return_val_if_fail (source != NULL, TRUE);
2246 return source->any_direction;
2250 * gtk_icon_source_set_direction:
2251 * @source: a #GtkIconSource
2252 * @direction: text direction this source applies to
2254 * Sets the text direction this icon source is intended to be used
2257 * Setting the text direction on an icon source makes no difference
2258 * if the text direction is wildcarded. Therefore, you should usually
2259 * call gtk_icon_source_set_direction_wildcarded() to un-wildcard it
2260 * in addition to calling this function.
2263 gtk_icon_source_set_direction (GtkIconSource *source,
2264 GtkTextDirection direction)
2266 g_return_if_fail (source != NULL);
2268 source->direction = direction;
2272 * gtk_icon_source_set_state:
2273 * @source: a #GtkIconSource
2274 * @state: widget state this source applies to
2276 * Sets the widget state this icon source is intended to be used
2279 * Setting the widget state on an icon source makes no difference
2280 * if the state is wildcarded. Therefore, you should usually
2281 * call gtk_icon_source_set_state_wildcarded() to un-wildcard it
2282 * in addition to calling this function.
2285 gtk_icon_source_set_state (GtkIconSource *source,
2288 g_return_if_fail (source != NULL);
2290 source->state = state;
2294 * gtk_icon_source_set_size:
2295 * @source: a #GtkIconSource
2296 * @size: (type int): icon size this source applies to
2298 * Sets the icon size this icon source is intended to be used
2301 * Setting the icon size on an icon source makes no difference
2302 * if the size is wildcarded. Therefore, you should usually
2303 * call gtk_icon_source_set_size_wildcarded() to un-wildcard it
2304 * in addition to calling this function.
2307 gtk_icon_source_set_size (GtkIconSource *source,
2310 g_return_if_fail (source != NULL);
2312 source->size = size;
2316 * gtk_icon_source_get_direction:
2317 * @source: a #GtkIconSource
2319 * Obtains the text direction this icon source applies to. The return
2320 * value is only useful/meaningful if the text direction is <emphasis>not</emphasis>
2323 * Return value: text direction this source matches
2326 gtk_icon_source_get_direction (const GtkIconSource *source)
2328 g_return_val_if_fail (source != NULL, 0);
2330 return source->direction;
2334 * gtk_icon_source_get_state:
2335 * @source: a #GtkIconSource
2337 * Obtains the widget state this icon source applies to. The return
2338 * value is only useful/meaningful if the widget state is <emphasis>not</emphasis>
2341 * Return value: widget state this source matches
2344 gtk_icon_source_get_state (const GtkIconSource *source)
2346 g_return_val_if_fail (source != NULL, 0);
2348 return source->state;
2352 * gtk_icon_source_get_size:
2353 * @source: a #GtkIconSource
2355 * Obtains the icon size this source applies to. The return value
2356 * is only useful/meaningful if the icon size is <emphasis>not</emphasis> wildcarded.
2358 * Return value: (type int): icon size this source matches.
2361 gtk_icon_source_get_size (const GtkIconSource *source)
2363 g_return_val_if_fail (source != NULL, 0);
2365 return source->size;
2368 #define NUM_CACHED_ICONS 8
2370 typedef struct _CachedIcon CachedIcon;
2374 /* These must all match to use the cached pixbuf.
2375 * If any don't match, we must re-render the pixbuf.
2378 GtkTextDirection direction;
2386 ensure_cache_up_to_date (GtkIconSet *icon_set)
2388 if (icon_set->cache_serial != cache_serial)
2390 clear_cache (icon_set, TRUE);
2391 icon_set->cache_serial = cache_serial;
2396 cached_icon_free (CachedIcon *icon)
2398 g_object_unref (icon->pixbuf);
2401 g_object_unref (icon->style);
2407 find_in_cache (GtkIconSet *icon_set,
2409 GtkTextDirection direction,
2416 ensure_cache_up_to_date (icon_set);
2419 tmp_list = icon_set->cache;
2420 while (tmp_list != NULL)
2422 CachedIcon *icon = tmp_list->data;
2424 if (icon->style == style &&
2425 icon->direction == direction &&
2426 icon->state == state &&
2427 (size == (GtkIconSize)-1 || icon->size == size))
2431 /* Move this icon to the front of the list. */
2432 prev->next = tmp_list->next;
2433 tmp_list->next = icon_set->cache;
2434 icon_set->cache = tmp_list;
2437 return icon->pixbuf;
2441 tmp_list = g_slist_next (tmp_list);
2448 add_to_cache (GtkIconSet *icon_set,
2450 GtkTextDirection direction,
2457 ensure_cache_up_to_date (icon_set);
2459 g_object_ref (pixbuf);
2461 /* We have to ref the style, since if the style was finalized
2462 * its address could be reused by another style, creating a
2467 g_object_ref (style);
2469 icon = g_new (CachedIcon, 1);
2470 icon_set->cache = g_slist_prepend (icon_set->cache, icon);
2471 icon_set->cache_size++;
2473 icon->style = style;
2474 icon->direction = direction;
2475 icon->state = state;
2477 icon->pixbuf = pixbuf;
2480 attach_to_style (icon_set, icon->style);
2482 if (icon_set->cache_size >= NUM_CACHED_ICONS)
2484 /* Remove oldest item in the cache */
2487 tmp_list = icon_set->cache;
2489 /* Find next-to-last link */
2490 g_assert (NUM_CACHED_ICONS > 2);
2491 while (tmp_list->next->next)
2492 tmp_list = tmp_list->next;
2494 g_assert (tmp_list != NULL);
2495 g_assert (tmp_list->next != NULL);
2496 g_assert (tmp_list->next->next == NULL);
2498 /* Free the last icon */
2499 icon = tmp_list->next->data;
2501 g_slist_free (tmp_list->next);
2502 tmp_list->next = NULL;
2504 cached_icon_free (icon);
2509 clear_cache (GtkIconSet *icon_set,
2510 gboolean style_detach)
2512 GSList *cache, *tmp_list;
2513 GtkStyle *last_style = NULL;
2515 cache = icon_set->cache;
2516 icon_set->cache = NULL;
2517 icon_set->cache_size = 0;
2519 while (tmp_list != NULL)
2521 CachedIcon *icon = tmp_list->data;
2525 /* simple optimization for the case where the cache
2526 * contains contiguous icons from the same style.
2527 * it's safe to call detach_from_style more than
2528 * once on the same style though.
2530 if (last_style != icon->style)
2532 detach_from_style (icon_set, icon->style);
2533 last_style = icon->style;
2537 cached_icon_free (icon);
2539 tmp_list = g_slist_next (tmp_list);
2542 g_slist_free (cache);
2546 copy_cache (GtkIconSet *icon_set,
2547 GtkIconSet *copy_recipient)
2550 GSList *copy = NULL;
2552 ensure_cache_up_to_date (icon_set);
2554 tmp_list = icon_set->cache;
2555 while (tmp_list != NULL)
2557 CachedIcon *icon = tmp_list->data;
2558 CachedIcon *icon_copy = g_new (CachedIcon, 1);
2562 if (icon_copy->style)
2564 attach_to_style (copy_recipient, icon_copy->style);
2565 g_object_ref (icon_copy->style);
2568 g_object_ref (icon_copy->pixbuf);
2570 icon_copy->size = icon->size;
2572 copy = g_slist_prepend (copy, icon_copy);
2574 tmp_list = g_slist_next (tmp_list);
2577 return g_slist_reverse (copy);
2581 attach_to_style (GtkIconSet *icon_set,
2586 table = g_object_get_qdata (G_OBJECT (style),
2587 g_quark_try_string ("gtk-style-icon-sets"));
2591 table = g_hash_table_new (NULL, NULL);
2592 g_object_set_qdata_full (G_OBJECT (style),
2593 g_quark_from_static_string ("gtk-style-icon-sets"),
2598 g_hash_table_insert (table, icon_set, icon_set);
2602 detach_from_style (GtkIconSet *icon_set,
2607 table = g_object_get_qdata (G_OBJECT (style),
2608 g_quark_try_string ("gtk-style-icon-sets"));
2611 g_hash_table_remove (table, icon_set);
2615 iconsets_foreach (gpointer key,
2619 GtkIconSet *icon_set = key;
2621 /* We only need to remove cache entries for the given style;
2622 * but that complicates things because in destroy notify
2623 * we don't know which style got destroyed, and 95% of the
2624 * time all cache entries will have the same style,
2625 * so this is faster anyway.
2628 clear_cache (icon_set, FALSE);
2632 style_dnotify (gpointer data)
2634 GHashTable *table = data;
2636 g_hash_table_foreach (table, iconsets_foreach, NULL);
2638 g_hash_table_destroy (table);
2641 /* This allows the icon set to detect that its cache is out of date. */
2643 _gtk_icon_set_invalidate_caches (void)
2649 * _gtk_icon_factory_list_ids:
2651 * Gets all known IDs stored in an existing icon factory.
2652 * The strings in the returned list aren't copied.
2653 * The list itself should be freed.
2655 * Return value: List of ids in icon factories
2658 _gtk_icon_factory_list_ids (void)
2665 _gtk_icon_factory_ensure_default_icons ();
2667 tmp_list = all_icon_factories;
2668 while (tmp_list != NULL)
2671 GtkIconFactory *factory = GTK_ICON_FACTORY (tmp_list->data);
2672 GtkIconFactoryPrivate *priv = factory->priv;
2674 these_ids = g_hash_table_get_keys (priv->icons);
2676 ids = g_list_concat (ids, these_ids);
2678 tmp_list = g_slist_next (tmp_list);
2688 } IconFactoryParserData;
2694 GtkTextDirection direction;
2697 } IconSourceParserData;
2700 icon_source_start_element (GMarkupParseContext *context,
2701 const gchar *element_name,
2702 const gchar **names,
2703 const gchar **values,
2708 gchar *stock_id = NULL;
2709 gchar *filename = NULL;
2710 gchar *icon_name = NULL;
2712 gint direction = -1;
2714 IconFactoryParserData *parser_data;
2715 IconSourceParserData *source_data;
2717 GQuark error_domain;
2719 parser_data = (IconFactoryParserData*)user_data;
2721 if (!parser_data->in_source)
2723 if (strcmp (element_name, "sources") != 0)
2725 error_msg = g_strdup_printf ("Unexpected element %s, expected <sources>", element_name);
2726 error_domain = GTK_BUILDER_ERROR_INVALID_TAG;
2729 parser_data->in_source = TRUE;
2734 if (strcmp (element_name, "source") != 0)
2736 error_msg = g_strdup_printf ("Unexpected element %s, expected <source>", element_name);
2737 error_domain = GTK_BUILDER_ERROR_INVALID_TAG;
2742 for (i = 0; names[i]; i++)
2744 if (strcmp (names[i], "stock-id") == 0)
2745 stock_id = g_strdup (values[i]);
2746 else if (strcmp (names[i], "filename") == 0)
2747 filename = g_strdup (values[i]);
2748 else if (strcmp (names[i], "icon-name") == 0)
2749 icon_name = g_strdup (values[i]);
2750 else if (strcmp (names[i], "size") == 0)
2752 if (!_gtk_builder_enum_from_string (GTK_TYPE_ICON_SIZE,
2758 else if (strcmp (names[i], "direction") == 0)
2760 if (!_gtk_builder_enum_from_string (GTK_TYPE_TEXT_DIRECTION,
2766 else if (strcmp (names[i], "state") == 0)
2768 if (!_gtk_builder_enum_from_string (GTK_TYPE_STATE_TYPE,
2776 error_msg = g_strdup_printf ("'%s' is not a valid attribute of <%s>",
2777 names[i], "source");
2778 error_domain = GTK_BUILDER_ERROR_INVALID_ATTRIBUTE;
2785 error_msg = g_strdup_printf ("<source> requires a stock_id");
2786 error_domain = GTK_BUILDER_ERROR_MISSING_ATTRIBUTE;
2790 source_data = g_slice_new (IconSourceParserData);
2791 source_data->stock_id = stock_id;
2792 source_data->filename = filename;
2793 source_data->icon_name = icon_name;
2794 source_data->size = size;
2795 source_data->direction = direction;
2796 source_data->state = state;
2798 parser_data->sources = g_slist_prepend (parser_data->sources, source_data);
2804 gint line_number, char_number;
2806 g_markup_parse_context_get_position (context,
2810 tmp = g_strdup_printf ("%s:%d:%d %s", "input",
2811 line_number, char_number, error_msg);
2813 g_set_error_literal (error,
2818 g_warning ("%s", tmp);
2828 static const GMarkupParser icon_source_parser =
2830 icon_source_start_element,
2834 gtk_icon_factory_buildable_custom_tag_start (GtkBuildable *buildable,
2835 GtkBuilder *builder,
2837 const gchar *tagname,
2838 GMarkupParser *parser,
2841 g_assert (buildable);
2843 if (strcmp (tagname, "sources") == 0)
2845 IconFactoryParserData *parser_data;
2847 parser_data = g_slice_new0 (IconFactoryParserData);
2848 *parser = icon_source_parser;
2849 *data = parser_data;
2856 gtk_icon_factory_buildable_custom_tag_end (GtkBuildable *buildable,
2857 GtkBuilder *builder,
2859 const gchar *tagname,
2860 gpointer *user_data)
2862 GtkIconFactory *icon_factory;
2864 icon_factory = GTK_ICON_FACTORY (buildable);
2866 if (strcmp (tagname, "sources") == 0)
2868 IconFactoryParserData *parser_data;
2869 GtkIconSource *icon_source;
2870 GtkIconSet *icon_set;
2873 parser_data = (IconFactoryParserData*)user_data;
2875 for (l = parser_data->sources; l; l = l->next)
2877 IconSourceParserData *source_data = l->data;
2879 icon_set = gtk_icon_factory_lookup (icon_factory, source_data->stock_id);
2882 icon_set = gtk_icon_set_new ();
2883 gtk_icon_factory_add (icon_factory, source_data->stock_id, icon_set);
2884 gtk_icon_set_unref (icon_set);
2887 icon_source = gtk_icon_source_new ();
2889 if (source_data->filename)
2892 filename = _gtk_builder_get_absolute_filename (builder, source_data->filename);
2893 gtk_icon_source_set_filename (icon_source, filename);
2896 if (source_data->icon_name)
2897 gtk_icon_source_set_icon_name (icon_source, source_data->icon_name);
2898 if (source_data->size != -1)
2900 gtk_icon_source_set_size (icon_source, source_data->size);
2901 gtk_icon_source_set_size_wildcarded (icon_source, FALSE);
2903 if (source_data->direction != -1)
2905 gtk_icon_source_set_direction (icon_source, source_data->direction);
2906 gtk_icon_source_set_direction_wildcarded (icon_source, FALSE);
2908 if (source_data->state != -1)
2910 gtk_icon_source_set_state (icon_source, source_data->state);
2911 gtk_icon_source_set_state_wildcarded (icon_source, FALSE);
2914 /* Inline source_add() to avoid creating a copy */
2915 g_assert (icon_source->type != GTK_ICON_SOURCE_EMPTY);
2916 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
2918 icon_source_compare);
2920 g_free (source_data->stock_id);
2921 g_free (source_data->filename);
2922 g_free (source_data->icon_name);
2923 g_slice_free (IconSourceParserData, source_data);
2925 g_slist_free (parser_data->sources);
2926 g_slice_free (IconFactoryParserData, parser_data);
2928 /* TODO: Add an attribute/tag to prevent this.
2929 * Usually it's the right thing to do though.
2931 gtk_icon_factory_add_default (icon_factory);