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/.
33 #include "gtkiconfactory.h"
34 #include "gtkiconcache.h"
36 #include "gtkicontheme.h"
37 #include "gtksettingsprivate.h"
39 #include "gtkwidget.h"
41 #include "gtkbuildable.h"
42 #include "gtkbuilderprivate.h"
43 #include "gtktypebuiltins.h"
44 #include "deprecated/gtkstyle.h"
48 * SECTION:gtkiconfactory
49 * @Short_description: Manipulating stock icons
50 * @Title: Themeable Stock Images
52 * Browse the available stock icons in the list of stock IDs found <link
53 * linkend="gtk-Stock-Items">here</link>. You can also use
54 * the <application>gtk-demo</application> application for this purpose.
56 * An icon factory manages a collection of #GtkIconSet; a #GtkIconSet manages a
57 * set of variants of a particular icon (i.e. a #GtkIconSet contains variants for
58 * different sizes and widget states). Icons in an icon factory are named by a
59 * stock ID, which is a simple string identifying the icon. Each #GtkStyle has a
60 * list of #GtkIconFactory derived from the current theme; those icon factories
61 * are consulted first when searching for an icon. If the theme doesn't set a
62 * particular icon, GTK+ looks for the icon in a list of default icon factories,
63 * maintained by gtk_icon_factory_add_default() and
64 * gtk_icon_factory_remove_default(). Applications with icons should add a default
65 * icon factory with their icons, which will allow themes to override the icons
66 * for the application.
68 * To display an icon, always use gtk_style_lookup_icon_set() on the widget that
69 * will display the icon, or the convenience function
70 * gtk_widget_render_icon(). These functions take the theme into account when
71 * looking up the icon to use for a given stock ID.
73 * <refsect2 id="GtkIconFactory-BUILDER-UI">
74 * <title>GtkIconFactory as GtkBuildable</title>
76 * GtkIconFactory supports a custom <sources> element, which can contain
77 * multiple <source> elements.
78 * The following attributes are allowed:
81 * <term>stock-id</term>
83 * The stock id of the source, a string.
84 * This attribute is mandatory
88 * <term>filename</term>
90 * The filename of the source, a string.
91 * This attribute is optional
95 * <term>icon-name</term>
97 * The icon name for the source, a string.
98 * This attribute is optional.
104 * Size of the icon, a #GtkIconSize enum value.
105 * This attribute is optional.
109 * <term>direction</term>
111 * Direction of the source, a #GtkTextDirection enum value.
112 * This attribute is optional.
118 * State of the source, a #GtkStateType enum value.
119 * This attribute is optional.
124 * <title>A #GtkIconFactory UI definition fragment.</title>
125 * <programlisting><![CDATA[
126 * <object class="GtkIconFactory" id="iconfactory1">
128 * <source stock-id="apple-red" filename="apple-red.png"/>
131 * <object class="GtkWindow" id="window1">
133 * <object class="GtkButton" id="apple_button">
134 * <property name="label">apple-red</property>
135 * <property name="use-stock">True</property>
147 static GSList *all_icon_factories = NULL;
149 struct _GtkIconFactoryPrivate
155 GTK_ICON_SOURCE_EMPTY,
156 GTK_ICON_SOURCE_ICON_NAME,
157 GTK_ICON_SOURCE_STATIC_ICON_NAME,
158 GTK_ICON_SOURCE_FILENAME,
159 GTK_ICON_SOURCE_PIXBUF
162 struct _GtkIconSource
164 GtkIconSourceType type;
172 GdkPixbuf *filename_pixbuf;
174 GtkTextDirection direction;
178 /* If TRUE, then the parameter is wildcarded, and the above
179 * fields should be ignored. If FALSE, the parameter is
180 * specified, and the above fields should be valid.
182 guint any_direction : 1;
189 gtk_icon_factory_buildable_init (GtkBuildableIface *iface);
191 static gboolean gtk_icon_factory_buildable_custom_tag_start (GtkBuildable *buildable,
194 const gchar *tagname,
195 GMarkupParser *parser,
197 static void gtk_icon_factory_buildable_custom_tag_end (GtkBuildable *buildable,
200 const gchar *tagname,
201 gpointer *user_data);
202 static void gtk_icon_factory_finalize (GObject *object);
203 static void get_default_icons (GtkIconFactory *icon_factory);
204 static void icon_source_clear (GtkIconSource *source);
206 static GtkIconSize icon_size_register_intern (const gchar *name,
210 #define GTK_ICON_SOURCE_INIT(any_direction, any_state, any_size) \
211 { GTK_ICON_SOURCE_EMPTY, { NULL }, NULL, \
213 any_direction, any_state, any_size }
215 G_DEFINE_TYPE_WITH_CODE (GtkIconFactory, gtk_icon_factory, G_TYPE_OBJECT,
216 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
217 gtk_icon_factory_buildable_init))
220 gtk_icon_factory_init (GtkIconFactory *factory)
222 GtkIconFactoryPrivate *priv;
224 factory->priv = G_TYPE_INSTANCE_GET_PRIVATE (factory,
225 GTK_TYPE_ICON_FACTORY,
226 GtkIconFactoryPrivate);
227 priv = factory->priv;
229 priv->icons = g_hash_table_new (g_str_hash, g_str_equal);
230 all_icon_factories = g_slist_prepend (all_icon_factories, factory);
234 gtk_icon_factory_class_init (GtkIconFactoryClass *klass)
236 GObjectClass *object_class = G_OBJECT_CLASS (klass);
238 object_class->finalize = gtk_icon_factory_finalize;
240 g_type_class_add_private (klass, sizeof (GtkIconFactoryPrivate));
244 gtk_icon_factory_buildable_init (GtkBuildableIface *iface)
246 iface->custom_tag_start = gtk_icon_factory_buildable_custom_tag_start;
247 iface->custom_tag_end = gtk_icon_factory_buildable_custom_tag_end;
251 free_icon_set (gpointer key, gpointer value, gpointer data)
254 gtk_icon_set_unref (value);
258 gtk_icon_factory_finalize (GObject *object)
260 GtkIconFactory *factory = GTK_ICON_FACTORY (object);
261 GtkIconFactoryPrivate *priv = factory->priv;
263 all_icon_factories = g_slist_remove (all_icon_factories, factory);
265 g_hash_table_foreach (priv->icons, free_icon_set, NULL);
267 g_hash_table_destroy (priv->icons);
269 G_OBJECT_CLASS (gtk_icon_factory_parent_class)->finalize (object);
273 * gtk_icon_factory_new:
275 * Creates a new #GtkIconFactory. An icon factory manages a collection
276 * of #GtkIconSet<!-- -->s; a #GtkIconSet manages a set of variants of a
277 * particular icon (i.e. a #GtkIconSet contains variants for different
278 * sizes and widget states). Icons in an icon factory are named by a
279 * stock ID, which is a simple string identifying the icon. Each
280 * #GtkStyle has a list of #GtkIconFactory<!-- -->s derived from the current
281 * theme; those icon factories are consulted first when searching for
282 * an icon. If the theme doesn't set a particular icon, GTK+ looks for
283 * the icon in a list of default icon factories, maintained by
284 * gtk_icon_factory_add_default() and
285 * gtk_icon_factory_remove_default(). Applications with icons should
286 * add a default icon factory with their icons, which will allow
287 * themes to override the icons for the application.
289 * Return value: a new #GtkIconFactory
292 gtk_icon_factory_new (void)
294 return g_object_new (GTK_TYPE_ICON_FACTORY, NULL);
298 * gtk_icon_factory_add:
299 * @factory: a #GtkIconFactory
300 * @stock_id: icon name
301 * @icon_set: icon set
303 * Adds the given @icon_set to the icon factory, under the name
304 * @stock_id. @stock_id should be namespaced for your application,
305 * e.g. "myapp-whatever-icon". Normally applications create a
306 * #GtkIconFactory, then add it to the list of default factories with
307 * gtk_icon_factory_add_default(). Then they pass the @stock_id to
308 * widgets such as #GtkImage to display the icon. Themes can provide
309 * an icon with the same name (such as "myapp-whatever-icon") to
310 * override your application's default icons. If an icon already
311 * existed in @factory for @stock_id, it is unreferenced and replaced
312 * with the new @icon_set.
315 gtk_icon_factory_add (GtkIconFactory *factory,
316 const gchar *stock_id,
317 GtkIconSet *icon_set)
319 GtkIconFactoryPrivate *priv = factory->priv;
320 gpointer old_key = NULL;
321 gpointer old_value = NULL;
323 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
324 g_return_if_fail (stock_id != NULL);
325 g_return_if_fail (icon_set != NULL);
327 g_hash_table_lookup_extended (priv->icons, stock_id,
328 &old_key, &old_value);
330 if (old_value == icon_set)
333 gtk_icon_set_ref (icon_set);
335 /* GHashTable key memory management is so fantastically broken. */
337 g_hash_table_insert (priv->icons, old_key, icon_set);
339 g_hash_table_insert (priv->icons, g_strdup (stock_id), icon_set);
342 gtk_icon_set_unref (old_value);
346 * gtk_icon_factory_lookup:
347 * @factory: a #GtkIconFactory
348 * @stock_id: an icon name
350 * Looks up @stock_id in the icon factory, returning an icon set
351 * if found, otherwise %NULL. For display to the user, you should
352 * use gtk_style_lookup_icon_set() on the #GtkStyle for the
353 * widget that will display the icon, instead of using this
354 * function directly, so that themes are taken into account.
356 * Return value: (transfer none): icon set of @stock_id.
359 gtk_icon_factory_lookup (GtkIconFactory *factory,
360 const gchar *stock_id)
362 GtkIconFactoryPrivate *priv;
364 g_return_val_if_fail (GTK_IS_ICON_FACTORY (factory), NULL);
365 g_return_val_if_fail (stock_id != NULL, NULL);
367 priv = factory->priv;
369 return g_hash_table_lookup (priv->icons, stock_id);
372 static GtkIconFactory *gtk_default_icons = NULL;
373 static GSList *default_factories = NULL;
376 * gtk_icon_factory_add_default:
377 * @factory: a #GtkIconFactory
379 * Adds an icon factory to the list of icon factories searched by
380 * gtk_style_lookup_icon_set(). This means that, for example,
381 * gtk_image_new_from_stock() will be able to find icons in @factory.
382 * There will normally be an icon factory added for each library or
383 * application that comes with icons. The default icon factories
384 * can be overridden by themes.
387 gtk_icon_factory_add_default (GtkIconFactory *factory)
389 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
391 g_object_ref (factory);
393 default_factories = g_slist_prepend (default_factories, factory);
397 * gtk_icon_factory_remove_default:
398 * @factory: a #GtkIconFactory previously added with gtk_icon_factory_add_default()
400 * Removes an icon factory from the list of default icon
401 * factories. Not normally used; you might use it for a library that
402 * can be unloaded or shut down.
405 gtk_icon_factory_remove_default (GtkIconFactory *factory)
407 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
409 default_factories = g_slist_remove (default_factories, factory);
411 g_object_unref (factory);
415 _gtk_icon_factory_ensure_default_icons (void)
417 if (gtk_default_icons == NULL)
419 gtk_default_icons = gtk_icon_factory_new ();
421 get_default_icons (gtk_default_icons);
426 * gtk_icon_factory_lookup_default:
427 * @stock_id: an icon name
429 * Looks for an icon in the list of default icon factories. For
430 * display to the user, you should use gtk_style_lookup_icon_set() on
431 * the #GtkStyle for the widget that will display the icon, instead of
432 * using this function directly, so that themes are taken into
435 * Return value: (transfer none): a #GtkIconSet, or %NULL
438 gtk_icon_factory_lookup_default (const gchar *stock_id)
442 g_return_val_if_fail (stock_id != NULL, NULL);
444 tmp_list = default_factories;
445 while (tmp_list != NULL)
447 GtkIconSet *icon_set =
448 gtk_icon_factory_lookup (GTK_ICON_FACTORY (tmp_list->data),
454 tmp_list = g_slist_next (tmp_list);
457 _gtk_icon_factory_ensure_default_icons ();
459 return gtk_icon_factory_lookup (gtk_default_icons, stock_id);
463 register_stock_icon (GtkIconFactory *factory,
464 const gchar *stock_id,
465 const gchar *icon_name)
467 GtkIconSet *set = gtk_icon_set_new ();
468 GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
470 source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
471 source.source.icon_name = (gchar *)icon_name;
472 source.direction = GTK_TEXT_DIR_NONE;
473 gtk_icon_set_add_source (set, &source);
475 gtk_icon_factory_add (factory, stock_id, set);
476 gtk_icon_set_unref (set);
480 register_bidi_stock_icon (GtkIconFactory *factory,
481 const gchar *stock_id,
482 const gchar *icon_name)
484 GtkIconSet *set = gtk_icon_set_new ();
485 GtkIconSource source = GTK_ICON_SOURCE_INIT (FALSE, TRUE, TRUE);
487 source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
488 source.source.icon_name = (gchar *)icon_name;
489 source.direction = GTK_TEXT_DIR_LTR;
490 gtk_icon_set_add_source (set, &source);
492 source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
493 source.source.icon_name = (gchar *)icon_name;
494 source.direction = GTK_TEXT_DIR_RTL;
495 gtk_icon_set_add_source (set, &source);
497 gtk_icon_factory_add (factory, stock_id, set);
498 gtk_icon_set_unref (set);
502 get_default_icons (GtkIconFactory *factory)
504 /* KEEP IN SYNC with gtkstock.c */
506 register_stock_icon (factory, GTK_STOCK_DIALOG_AUTHENTICATION, "dialog-password");
507 register_stock_icon (factory, GTK_STOCK_DIALOG_ERROR, "dialog-error");
508 register_stock_icon (factory, GTK_STOCK_DIALOG_INFO, "dialog-information");
509 register_stock_icon (factory, GTK_STOCK_DIALOG_QUESTION, "dialog-question");
510 register_stock_icon (factory, GTK_STOCK_DIALOG_WARNING, "dialog-warning");
511 register_stock_icon (factory, GTK_STOCK_DND, GTK_STOCK_DND);
512 register_stock_icon (factory, GTK_STOCK_DND_MULTIPLE, GTK_STOCK_DND_MULTIPLE);
513 register_stock_icon (factory, GTK_STOCK_APPLY, GTK_STOCK_APPLY);
514 register_stock_icon (factory, GTK_STOCK_CANCEL, GTK_STOCK_CANCEL);
515 register_stock_icon (factory, GTK_STOCK_NO, GTK_STOCK_NO);
516 register_stock_icon (factory, GTK_STOCK_OK, GTK_STOCK_OK);
517 register_stock_icon (factory, GTK_STOCK_YES, GTK_STOCK_YES);
518 register_stock_icon (factory, GTK_STOCK_CLOSE, "window-close");
519 register_stock_icon (factory, GTK_STOCK_ADD, "list-add");
520 register_stock_icon (factory, GTK_STOCK_JUSTIFY_CENTER, "format-justify-center");
521 register_stock_icon (factory, GTK_STOCK_JUSTIFY_FILL, "format-justify-fill");
522 register_stock_icon (factory, GTK_STOCK_JUSTIFY_LEFT, "format-justify-left");
523 register_stock_icon (factory, GTK_STOCK_JUSTIFY_RIGHT, "format-justify-right");
524 register_stock_icon (factory, GTK_STOCK_GOTO_BOTTOM, "go-bottom");
525 register_stock_icon (factory, GTK_STOCK_CDROM, "media-optical");
526 register_stock_icon (factory, GTK_STOCK_CONVERT, GTK_STOCK_CONVERT);
527 register_stock_icon (factory, GTK_STOCK_COPY, "edit-copy");
528 register_stock_icon (factory, GTK_STOCK_CUT, "edit-cut");
529 register_stock_icon (factory, GTK_STOCK_GO_DOWN, "go-down");
530 register_stock_icon (factory, GTK_STOCK_EXECUTE, "system-run");
531 register_stock_icon (factory, GTK_STOCK_QUIT, "application-exit");
532 register_bidi_stock_icon (factory, GTK_STOCK_GOTO_FIRST, "go-first");
533 register_stock_icon (factory, GTK_STOCK_SELECT_FONT, GTK_STOCK_SELECT_FONT);
534 register_stock_icon (factory, GTK_STOCK_FULLSCREEN, "view-fullscreen");
535 register_stock_icon (factory, GTK_STOCK_LEAVE_FULLSCREEN, "view-restore");
536 register_stock_icon (factory, GTK_STOCK_HARDDISK, "drive-harddisk");
537 register_stock_icon (factory, GTK_STOCK_HELP, "help-contents");
538 register_stock_icon (factory, GTK_STOCK_HOME, "go-home");
539 register_stock_icon (factory, GTK_STOCK_INFO, "dialog-information");
540 register_bidi_stock_icon (factory, GTK_STOCK_JUMP_TO, "go-jump");
541 register_bidi_stock_icon (factory, GTK_STOCK_GOTO_LAST, "go-last");
542 register_bidi_stock_icon (factory, GTK_STOCK_GO_BACK, "go-previous");
543 register_stock_icon (factory, GTK_STOCK_MISSING_IMAGE, "image-missing");
544 register_stock_icon (factory, GTK_STOCK_NETWORK, "network-idle");
545 register_stock_icon (factory, GTK_STOCK_NEW, "document-new");
546 register_stock_icon (factory, GTK_STOCK_OPEN, "document-open");
547 register_stock_icon (factory, GTK_STOCK_ORIENTATION_PORTRAIT, GTK_STOCK_ORIENTATION_PORTRAIT);
548 register_stock_icon (factory, GTK_STOCK_ORIENTATION_LANDSCAPE, GTK_STOCK_ORIENTATION_LANDSCAPE);
549 register_stock_icon (factory, GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT, GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT);
550 register_stock_icon (factory, GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE, GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE);
551 register_stock_icon (factory, GTK_STOCK_PAGE_SETUP, GTK_STOCK_PAGE_SETUP);
552 register_stock_icon (factory, GTK_STOCK_PASTE, "edit-paste");
553 register_stock_icon (factory, GTK_STOCK_PREFERENCES, GTK_STOCK_PREFERENCES);
554 register_stock_icon (factory, GTK_STOCK_PRINT, "document-print");
555 register_stock_icon (factory, GTK_STOCK_PRINT_ERROR, "printer-error");
556 register_stock_icon (factory, GTK_STOCK_PRINT_PAUSED, "printer-paused");
557 register_stock_icon (factory, GTK_STOCK_PRINT_PREVIEW, "document-print-preview");
558 register_stock_icon (factory, GTK_STOCK_PRINT_REPORT, "printer-info");
559 register_stock_icon (factory, GTK_STOCK_PRINT_WARNING, "printer-warning");
560 register_stock_icon (factory, GTK_STOCK_PROPERTIES, "document-properties");
561 register_bidi_stock_icon (factory, GTK_STOCK_REDO, "edit-redo");
562 register_stock_icon (factory, GTK_STOCK_REMOVE, "list-remove");
563 register_stock_icon (factory, GTK_STOCK_REFRESH, "view-refresh");
564 register_bidi_stock_icon (factory, GTK_STOCK_REVERT_TO_SAVED, "document-revert");
565 register_bidi_stock_icon (factory, GTK_STOCK_GO_FORWARD, "go-next");
566 register_stock_icon (factory, GTK_STOCK_SAVE, "document-save");
567 register_stock_icon (factory, GTK_STOCK_FLOPPY, "media-floppy");
568 register_stock_icon (factory, GTK_STOCK_SAVE_AS, "document-save-as");
569 register_stock_icon (factory, GTK_STOCK_FIND, "edit-find");
570 register_stock_icon (factory, GTK_STOCK_FIND_AND_REPLACE, "edit-find-replace");
571 register_stock_icon (factory, GTK_STOCK_SORT_DESCENDING, "view-sort-descending");
572 register_stock_icon (factory, GTK_STOCK_SORT_ASCENDING, "view-sort-ascending");
573 register_stock_icon (factory, GTK_STOCK_SPELL_CHECK, "tools-check-spelling");
574 register_stock_icon (factory, GTK_STOCK_STOP, "process-stop");
575 register_stock_icon (factory, GTK_STOCK_BOLD, "format-text-bold");
576 register_stock_icon (factory, GTK_STOCK_ITALIC, "format-text-italic");
577 register_stock_icon (factory, GTK_STOCK_STRIKETHROUGH, "format-text-strikethrough");
578 register_stock_icon (factory, GTK_STOCK_UNDERLINE, "format-text-underline");
579 register_bidi_stock_icon (factory, GTK_STOCK_INDENT, "format-indent-more");
580 register_bidi_stock_icon (factory, GTK_STOCK_UNINDENT, "format-indent-less");
581 register_stock_icon (factory, GTK_STOCK_GOTO_TOP, "go-top");
582 register_stock_icon (factory, GTK_STOCK_DELETE, "edit-delete");
583 register_bidi_stock_icon (factory, GTK_STOCK_UNDELETE, GTK_STOCK_UNDELETE);
584 register_bidi_stock_icon (factory, GTK_STOCK_UNDO, "edit-undo");
585 register_stock_icon (factory, GTK_STOCK_GO_UP, "go-up");
586 register_stock_icon (factory, GTK_STOCK_FILE, "text-x-generic");
587 register_stock_icon (factory, GTK_STOCK_DIRECTORY, "folder");
588 register_stock_icon (factory, GTK_STOCK_ABOUT, "help-about");
589 register_stock_icon (factory, GTK_STOCK_CONNECT, GTK_STOCK_CONNECT);
590 register_stock_icon (factory, GTK_STOCK_DISCONNECT, GTK_STOCK_DISCONNECT);
591 register_stock_icon (factory, GTK_STOCK_EDIT, GTK_STOCK_EDIT);
592 register_stock_icon (factory, GTK_STOCK_CAPS_LOCK_WARNING, GTK_STOCK_CAPS_LOCK_WARNING);
593 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_FORWARD, "media-seek-forward");
594 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_NEXT, "media-skip-forward");
595 register_stock_icon (factory, GTK_STOCK_MEDIA_PAUSE, "media-playback-pause");
596 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_PLAY, "media-playback-start");
597 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_PREVIOUS, "media-skip-backward");
598 register_stock_icon (factory, GTK_STOCK_MEDIA_RECORD, "media-record");
599 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_REWIND, "media-seek-backward");
600 register_stock_icon (factory, GTK_STOCK_MEDIA_STOP, "media-playback-stop");
601 register_stock_icon (factory, GTK_STOCK_INDEX, GTK_STOCK_INDEX);
602 register_stock_icon (factory, GTK_STOCK_ZOOM_100, "zoom-original");
603 register_stock_icon (factory, GTK_STOCK_ZOOM_IN, "zoom-in");
604 register_stock_icon (factory, GTK_STOCK_ZOOM_OUT, "zoom-out");
605 register_stock_icon (factory, GTK_STOCK_ZOOM_FIT, "zoom-fit-best");
606 register_stock_icon (factory, GTK_STOCK_SELECT_ALL, "edit-select-all");
607 register_stock_icon (factory, GTK_STOCK_CLEAR, "edit-clear");
608 register_stock_icon (factory, GTK_STOCK_SELECT_COLOR, GTK_STOCK_SELECT_COLOR);
609 register_stock_icon (factory, GTK_STOCK_COLOR_PICKER, GTK_STOCK_COLOR_PICKER);
612 /************************************************************
613 * Icon size handling *
614 ************************************************************/
616 typedef struct _IconSize IconSize;
627 typedef struct _IconAlias IconAlias;
635 typedef struct _SettingsIconSize SettingsIconSize;
637 struct _SettingsIconSize
643 static GHashTable *icon_aliases = NULL;
644 static IconSize *icon_sizes = NULL;
645 static gint icon_sizes_allocated = 0;
646 static gint icon_sizes_used = 0;
649 init_icon_sizes (void)
651 if (icon_sizes == NULL)
653 #define NUM_BUILTIN_SIZES 7
656 icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
658 icon_sizes = g_new (IconSize, NUM_BUILTIN_SIZES);
659 icon_sizes_allocated = NUM_BUILTIN_SIZES;
660 icon_sizes_used = NUM_BUILTIN_SIZES;
662 icon_sizes[GTK_ICON_SIZE_INVALID].size = 0;
663 icon_sizes[GTK_ICON_SIZE_INVALID].name = NULL;
664 icon_sizes[GTK_ICON_SIZE_INVALID].width = 0;
665 icon_sizes[GTK_ICON_SIZE_INVALID].height = 0;
667 /* the name strings aren't copied since we don't ever remove
668 * icon sizes, so we don't need to know whether they're static.
669 * Even if we did I suppose removing the builtin sizes would be
673 icon_sizes[GTK_ICON_SIZE_MENU].size = GTK_ICON_SIZE_MENU;
674 icon_sizes[GTK_ICON_SIZE_MENU].name = "gtk-menu";
675 icon_sizes[GTK_ICON_SIZE_MENU].width = 16;
676 icon_sizes[GTK_ICON_SIZE_MENU].height = 16;
678 icon_sizes[GTK_ICON_SIZE_BUTTON].size = GTK_ICON_SIZE_BUTTON;
679 icon_sizes[GTK_ICON_SIZE_BUTTON].name = "gtk-button";
680 icon_sizes[GTK_ICON_SIZE_BUTTON].width = 20;
681 icon_sizes[GTK_ICON_SIZE_BUTTON].height = 20;
683 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].size = GTK_ICON_SIZE_SMALL_TOOLBAR;
684 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].name = "gtk-small-toolbar";
685 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].width = 18;
686 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].height = 18;
688 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].size = GTK_ICON_SIZE_LARGE_TOOLBAR;
689 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].name = "gtk-large-toolbar";
690 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].width = 24;
691 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].height = 24;
693 icon_sizes[GTK_ICON_SIZE_DND].size = GTK_ICON_SIZE_DND;
694 icon_sizes[GTK_ICON_SIZE_DND].name = "gtk-dnd";
695 icon_sizes[GTK_ICON_SIZE_DND].width = 32;
696 icon_sizes[GTK_ICON_SIZE_DND].height = 32;
698 icon_sizes[GTK_ICON_SIZE_DIALOG].size = GTK_ICON_SIZE_DIALOG;
699 icon_sizes[GTK_ICON_SIZE_DIALOG].name = "gtk-dialog";
700 icon_sizes[GTK_ICON_SIZE_DIALOG].width = 48;
701 icon_sizes[GTK_ICON_SIZE_DIALOG].height = 48;
703 g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
705 /* Alias everything to itself. */
706 i = 1; /* skip invalid size */
707 while (i < NUM_BUILTIN_SIZES)
709 gtk_icon_size_register_alias (icon_sizes[i].name, icon_sizes[i].size);
714 #undef NUM_BUILTIN_SIZES
719 free_settings_sizes (gpointer data)
721 g_array_free (data, TRUE);
725 get_settings_sizes (GtkSettings *settings,
728 GArray *settings_sizes;
729 static GQuark sizes_quark = 0;
732 sizes_quark = g_quark_from_static_string ("gtk-icon-sizes");
734 settings_sizes = g_object_get_qdata (G_OBJECT (settings), sizes_quark);
737 settings_sizes = g_array_new (FALSE, FALSE, sizeof (SettingsIconSize));
738 g_object_set_qdata_full (G_OBJECT (settings), sizes_quark,
739 settings_sizes, free_settings_sizes);
744 return settings_sizes;
748 icon_size_set_for_settings (GtkSettings *settings,
749 const gchar *size_name,
754 GArray *settings_sizes;
755 SettingsIconSize *settings_size;
757 g_return_if_fail (size_name != NULL);
759 size = gtk_icon_size_from_name (size_name);
760 if (size == GTK_ICON_SIZE_INVALID)
761 /* Reserve a place */
762 size = icon_size_register_intern (size_name, -1, -1);
764 settings_sizes = get_settings_sizes (settings, NULL);
765 if (size >= settings_sizes->len)
767 SettingsIconSize unset = { -1, -1 };
770 for (i = settings_sizes->len; i <= size; i++)
771 g_array_append_val (settings_sizes, unset);
774 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
776 settings_size->width = width;
777 settings_size->height = height;
780 /* Like pango_parse_word, but accept - as well
783 scan_icon_size_name (const char **pos, GString *out)
785 const char *p = *pos;
787 while (g_ascii_isspace (*p))
790 if (!((*p >= 'A' && *p <= 'Z') ||
791 (*p >= 'a' && *p <= 'z') ||
792 *p == '_' || *p == '-'))
795 g_string_truncate (out, 0);
796 g_string_append_c (out, *p);
799 while ((*p >= 'A' && *p <= 'Z') ||
800 (*p >= 'a' && *p <= 'z') ||
801 (*p >= '0' && *p <= '9') ||
802 *p == '_' || *p == '-')
804 g_string_append_c (out, *p);
814 icon_size_setting_parse (GtkSettings *settings,
815 const gchar *icon_size_string)
817 GString *name_buf = g_string_new (NULL);
818 const gchar *p = icon_size_string;
820 while (pango_skip_space (&p))
824 if (!scan_icon_size_name (&p, name_buf))
827 if (!pango_skip_space (&p))
835 if (!pango_scan_int (&p, &width))
838 if (!pango_skip_space (&p))
846 if (!pango_scan_int (&p, &height))
849 if (width > 0 && height > 0)
851 icon_size_set_for_settings (settings, name_buf->str,
856 g_warning ("Invalid size in gtk-icon-sizes: %d,%d\n", width, height);
859 pango_skip_space (&p);
868 g_string_free (name_buf, TRUE);
872 g_warning ("Error parsing gtk-icon-sizes string:\n\t'%s'", icon_size_string);
873 g_string_free (name_buf, TRUE);
877 icon_size_set_all_from_settings (GtkSettings *settings)
879 GArray *settings_sizes;
880 gchar *icon_size_string;
882 /* Reset old settings */
883 settings_sizes = get_settings_sizes (settings, NULL);
884 g_array_set_size (settings_sizes, 0);
886 g_object_get (settings,
887 "gtk-icon-sizes", &icon_size_string,
890 if (icon_size_string)
892 icon_size_setting_parse (settings, icon_size_string);
893 g_free (icon_size_string);
898 icon_size_settings_changed (GtkSettings *settings,
901 icon_size_set_all_from_settings (settings);
903 gtk_style_context_reset_widgets (_gtk_settings_get_screen (settings));
907 icon_sizes_init_for_settings (GtkSettings *settings)
909 g_signal_connect (settings,
910 "notify::gtk-icon-sizes",
911 G_CALLBACK (icon_size_settings_changed),
914 icon_size_set_all_from_settings (settings);
918 icon_size_lookup_intern (GtkSettings *settings,
923 GArray *settings_sizes;
924 gint width_for_settings = -1;
925 gint height_for_settings = -1;
929 if (size == (GtkIconSize)-1)
932 if (size >= icon_sizes_used)
935 if (size == GTK_ICON_SIZE_INVALID)
940 gboolean initial = FALSE;
942 settings_sizes = get_settings_sizes (settings, &initial);
945 icon_sizes_init_for_settings (settings);
947 if (size < settings_sizes->len)
949 SettingsIconSize *settings_size;
951 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
953 width_for_settings = settings_size->width;
954 height_for_settings = settings_size->height;
959 *widthp = width_for_settings >= 0 ? width_for_settings : icon_sizes[size].width;
962 *heightp = height_for_settings >= 0 ? height_for_settings : icon_sizes[size].height;
968 * gtk_icon_size_lookup_for_settings:
969 * @settings: a #GtkSettings object, used to determine
970 * which set of user preferences to used.
971 * @size: (type int): an icon size
972 * @width: (out): location to store icon width
973 * @height: (out): location to store icon height
975 * Obtains the pixel size of a semantic icon size, possibly
976 * modified by user preferences for a particular
977 * #GtkSettings. Normally @size would be
978 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
979 * isn't normally needed, gtk_widget_render_icon_pixbuf() is the usual
980 * way to get an icon for rendering, then just look at the size of
981 * the rendered pixbuf. The rendered pixbuf may not even correspond to
982 * the width/height returned by gtk_icon_size_lookup(), because themes
983 * are free to render the pixbuf however they like, including changing
986 * Return value: %TRUE if @size was a valid size
991 gtk_icon_size_lookup_for_settings (GtkSettings *settings,
996 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
998 return icon_size_lookup_intern (settings, size, width, height);
1002 * gtk_icon_size_lookup:
1003 * @size: (type int): an icon size
1004 * @width: (out): location to store icon width
1005 * @height: (out): location to store icon height
1007 * Obtains the pixel size of a semantic icon size, possibly
1008 * modified by user preferences for the default #GtkSettings.
1009 * (See gtk_icon_size_lookup_for_settings().)
1010 * Normally @size would be
1011 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
1012 * isn't normally needed, gtk_widget_render_icon_pixbuf() is the usual
1013 * way to get an icon for rendering, then just look at the size of
1014 * the rendered pixbuf. The rendered pixbuf may not even correspond to
1015 * the width/height returned by gtk_icon_size_lookup(), because themes
1016 * are free to render the pixbuf however they like, including changing
1019 * Return value: %TRUE if @size was a valid size
1022 gtk_icon_size_lookup (GtkIconSize size,
1026 GTK_NOTE (MULTIHEAD,
1027 g_warning ("gtk_icon_size_lookup ()) is not multihead safe"));
1029 return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (),
1030 size, widthp, heightp);
1034 icon_size_register_intern (const gchar *name,
1038 IconAlias *old_alias;
1043 old_alias = g_hash_table_lookup (icon_aliases, name);
1044 if (old_alias && icon_sizes[old_alias->target].width > 0)
1046 g_warning ("Icon size name '%s' already exists", name);
1047 return GTK_ICON_SIZE_INVALID;
1052 size = old_alias->target;
1056 if (icon_sizes_used == icon_sizes_allocated)
1058 icon_sizes_allocated *= 2;
1059 icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated);
1062 size = icon_sizes_used++;
1064 /* alias to self. */
1065 gtk_icon_size_register_alias (name, size);
1067 icon_sizes[size].size = size;
1068 icon_sizes[size].name = g_strdup (name);
1071 icon_sizes[size].width = width;
1072 icon_sizes[size].height = height;
1078 * gtk_icon_size_register:
1079 * @name: name of the icon size
1080 * @width: the icon width
1081 * @height: the icon height
1083 * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
1084 * etc. Returns the integer value for the size.
1086 * Returns: (type int): integer value representing the size
1089 gtk_icon_size_register (const gchar *name,
1093 g_return_val_if_fail (name != NULL, 0);
1094 g_return_val_if_fail (width > 0, 0);
1095 g_return_val_if_fail (height > 0, 0);
1097 return icon_size_register_intern (name, width, height);
1101 * gtk_icon_size_register_alias:
1102 * @alias: an alias for @target
1103 * @target: (type int): an existing icon size
1105 * Registers @alias as another name for @target.
1106 * So calling gtk_icon_size_from_name() with @alias as argument
1107 * will return @target.
1110 gtk_icon_size_register_alias (const gchar *alias,
1115 g_return_if_fail (alias != NULL);
1119 if (!icon_size_lookup_intern (NULL, target, NULL, NULL))
1120 g_warning ("gtk_icon_size_register_alias: Icon size %u does not exist", target);
1122 ia = g_hash_table_lookup (icon_aliases, alias);
1125 if (icon_sizes[ia->target].width > 0)
1127 g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias);
1131 ia->target = target;
1136 ia = g_new (IconAlias, 1);
1137 ia->name = g_strdup (alias);
1138 ia->target = target;
1140 g_hash_table_insert (icon_aliases, ia->name, ia);
1145 * gtk_icon_size_from_name:
1146 * @name: the name to look up.
1148 * Looks up the icon size associated with @name.
1150 * Return value: (type int): the icon size
1153 gtk_icon_size_from_name (const gchar *name)
1159 ia = g_hash_table_lookup (icon_aliases, name);
1161 if (ia && icon_sizes[ia->target].width > 0)
1164 return GTK_ICON_SIZE_INVALID;
1168 * gtk_icon_size_get_name:
1169 * @size: (type int): a #GtkIconSize.
1171 * Gets the canonical name of the given icon size. The returned string
1172 * is statically allocated and should not be freed.
1174 * Returns: the name of the given icon size.
1177 gtk_icon_size_get_name (GtkIconSize size)
1179 if (size >= icon_sizes_used)
1182 return icon_sizes[size].name;
1185 /************************************************************/
1190 static GdkPixbuf *find_in_cache (GtkIconSet *icon_set,
1191 GtkStyleContext *style_context,
1192 GtkTextDirection direction,
1195 static void add_to_cache (GtkIconSet *icon_set,
1196 GtkStyleContext *style_context,
1197 GtkTextDirection direction,
1201 /* Clear icon set contents, drop references to all contained
1202 * GdkPixbuf objects and forget all GtkIconSources. Used to
1203 * recycle an icon set.
1205 static void clear_cache (GtkIconSet *icon_set,
1206 gboolean style_detach);
1207 static GSList* copy_cache (GtkIconSet *icon_set,
1208 GtkIconSet *copy_recipient);
1209 static void attach_to_style (GtkIconSet *icon_set,
1210 GtkStyleContext *style_context);
1211 static void detach_from_style (GtkIconSet *icon_set,
1212 GtkStyleContext *style_context);
1213 static void style_dnotify (gpointer data);
1221 /* Cache of the last few rendered versions of the icon. */
1229 static guint cache_serial = 0;
1234 * Creates a new #GtkIconSet. A #GtkIconSet represents a single icon
1235 * in various sizes and widget states. It can provide a #GdkPixbuf
1236 * for a given size and state on request, and automatically caches
1237 * some of the rendered #GdkPixbuf objects.
1239 * Normally you would use gtk_widget_render_icon_pixbuf() instead of
1240 * using #GtkIconSet directly. The one case where you'd use
1241 * #GtkIconSet is to create application-specific icon sets to place in
1242 * a #GtkIconFactory.
1244 * Return value: a new #GtkIconSet
1247 gtk_icon_set_new (void)
1249 GtkIconSet *icon_set;
1251 icon_set = g_new (GtkIconSet, 1);
1253 icon_set->ref_count = 1;
1254 icon_set->sources = NULL;
1255 icon_set->cache = NULL;
1256 icon_set->cache_size = 0;
1257 icon_set->cache_serial = cache_serial;
1263 * gtk_icon_set_new_from_pixbuf:
1264 * @pixbuf: a #GdkPixbuf
1266 * Creates a new #GtkIconSet with @pixbuf as the default/fallback
1267 * source image. If you don't add any additional #GtkIconSource to the
1268 * icon set, all variants of the icon will be created from @pixbuf,
1269 * using scaling, pixelation, etc. as required to adjust the icon size
1270 * or make the icon look insensitive/prelighted.
1272 * Return value: a new #GtkIconSet
1275 gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
1279 GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1281 g_return_val_if_fail (pixbuf != NULL, NULL);
1283 set = gtk_icon_set_new ();
1285 gtk_icon_source_set_pixbuf (&source, pixbuf);
1286 gtk_icon_set_add_source (set, &source);
1287 gtk_icon_source_set_pixbuf (&source, NULL);
1295 * @icon_set: a #GtkIconSet.
1297 * Increments the reference count on @icon_set.
1299 * Return value: @icon_set.
1302 gtk_icon_set_ref (GtkIconSet *icon_set)
1304 g_return_val_if_fail (icon_set != NULL, NULL);
1305 g_return_val_if_fail (icon_set->ref_count > 0, NULL);
1307 icon_set->ref_count += 1;
1313 * gtk_icon_set_unref:
1314 * @icon_set: a #GtkIconSet
1316 * Decrements the reference count on @icon_set, and frees memory
1317 * if the reference count reaches 0.
1320 gtk_icon_set_unref (GtkIconSet *icon_set)
1322 g_return_if_fail (icon_set != NULL);
1323 g_return_if_fail (icon_set->ref_count > 0);
1325 icon_set->ref_count -= 1;
1327 if (icon_set->ref_count == 0)
1329 GSList *tmp_list = icon_set->sources;
1330 while (tmp_list != NULL)
1332 gtk_icon_source_free (tmp_list->data);
1334 tmp_list = g_slist_next (tmp_list);
1336 g_slist_free (icon_set->sources);
1338 clear_cache (icon_set, TRUE);
1344 G_DEFINE_BOXED_TYPE (GtkIconSet, gtk_icon_set,
1349 * gtk_icon_set_copy:
1350 * @icon_set: a #GtkIconSet
1352 * Copies @icon_set by value.
1354 * Return value: a new #GtkIconSet identical to the first.
1357 gtk_icon_set_copy (GtkIconSet *icon_set)
1362 copy = gtk_icon_set_new ();
1364 tmp_list = icon_set->sources;
1365 while (tmp_list != NULL)
1367 copy->sources = g_slist_prepend (copy->sources,
1368 gtk_icon_source_copy (tmp_list->data));
1370 tmp_list = g_slist_next (tmp_list);
1373 copy->sources = g_slist_reverse (copy->sources);
1375 copy->cache = copy_cache (icon_set, copy);
1376 copy->cache_size = icon_set->cache_size;
1377 copy->cache_serial = icon_set->cache_serial;
1383 sizes_equivalent (GtkIconSize lhs,
1386 /* We used to consider sizes equivalent if they were
1387 * the same pixel size, but we don't have the GtkSettings
1388 * here, so we can't do that. Plus, it's not clear that
1389 * it is right... it was just a workaround for the fact
1390 * that we register icons by logical size, not pixel size.
1396 gint r_w, r_h, l_w, l_h;
1398 icon_size_lookup_intern (NULL, rhs, &r_w, &r_h);
1399 icon_size_lookup_intern (NULL, lhs, &l_w, &l_h);
1401 return r_w == l_w && r_h == l_h;
1405 static GtkIconSource *
1406 find_best_matching_source (GtkIconSet *icon_set,
1407 GtkTextDirection direction,
1412 GtkIconSource *source;
1415 /* We need to find the best icon source. Direction matters more
1416 * than state, state matters more than size. icon_set->sources
1417 * is sorted according to wildness, so if we take the first
1418 * match we find it will be the least-wild match (if there are
1419 * multiple matches for a given "wildness" then the RC file contained
1420 * dumb stuff, and we end up with an arbitrary matching source)
1424 tmp_list = icon_set->sources;
1425 while (tmp_list != NULL)
1427 GtkIconSource *s = tmp_list->data;
1429 if ((s->any_direction || (s->direction == direction)) &&
1430 (s->any_state || (s->state == state)) &&
1431 (s->any_size || size == (GtkIconSize)-1 || (sizes_equivalent (size, s->size))))
1433 if (!g_slist_find (failed, s))
1440 tmp_list = g_slist_next (tmp_list);
1447 ensure_filename_pixbuf (GtkIconSet *icon_set,
1448 GtkIconSource *source)
1450 if (source->filename_pixbuf == NULL)
1452 GError *error = NULL;
1454 source->filename_pixbuf = gdk_pixbuf_new_from_file (source->source.filename, &error);
1456 if (source->filename_pixbuf == NULL)
1458 /* Remove this icon source so we don't keep trying to
1461 g_warning ("Error loading icon: %s", error->message);
1462 g_error_free (error);
1464 icon_set->sources = g_slist_remove (icon_set->sources, source);
1466 gtk_icon_source_free (source);
1476 render_icon_name_pixbuf (GtkIconSource *icon_source,
1477 GtkStyleContext *context,
1481 GdkPixbuf *tmp_pixbuf;
1482 GtkIconSource tmp_source;
1484 GtkIconTheme *icon_theme;
1485 GtkSettings *settings;
1486 gint width, height, pixel_size;
1487 gint *sizes, *s, dist;
1488 GError *error = NULL;
1490 screen = gtk_style_context_get_screen (context);
1491 icon_theme = gtk_icon_theme_get_for_screen (screen);
1492 settings = gtk_settings_get_for_screen (screen);
1494 if (!gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
1496 if (size == (GtkIconSize)-1)
1498 /* Find an available size close to 48 */
1499 sizes = gtk_icon_theme_get_icon_sizes (icon_theme, icon_source->source.icon_name);
1501 width = height = 48;
1502 for (s = sizes; *s; s++)
1506 width = height = 48;
1513 width = height = *s;
1521 width = height = *s;
1531 g_warning ("Invalid icon size %u\n", size);
1532 width = height = 24;
1536 pixel_size = MIN (width, height);
1538 if (icon_source->direction != GTK_TEXT_DIR_NONE)
1540 gchar *suffix[3] = { NULL, "-ltr", "-rtl" };
1544 names[0] = g_strconcat (icon_source->source.icon_name, suffix[icon_source->direction], NULL);
1545 names[1] = icon_source->source.icon_name;
1548 info = gtk_icon_theme_choose_icon (icon_theme,
1549 (const char **) names,
1550 pixel_size, GTK_ICON_LOOKUP_USE_BUILTIN);
1554 tmp_pixbuf = gtk_icon_info_load_icon (info, &error);
1555 gtk_icon_info_free (info);
1562 tmp_pixbuf = gtk_icon_theme_load_icon (icon_theme,
1563 icon_source->source.icon_name,
1570 g_warning ("Error loading theme icon '%s' for stock: %s",
1571 icon_source->source.icon_name, error ? error->message : "");
1573 g_error_free (error);
1577 tmp_source = *icon_source;
1578 tmp_source.type = GTK_ICON_SOURCE_PIXBUF;
1579 tmp_source.source.pixbuf = tmp_pixbuf;
1581 pixbuf = gtk_render_icon_pixbuf (context, &tmp_source, -1);
1584 g_warning ("Failed to render icon");
1586 g_object_unref (tmp_pixbuf);
1592 find_and_render_icon_source (GtkIconSet *icon_set,
1593 GtkStyleContext *context,
1594 GtkTextDirection direction,
1598 GSList *failed = NULL;
1599 GdkPixbuf *pixbuf = NULL;
1601 /* We treat failure in two different ways:
1603 * A) If loading a source that specifies a filename fails,
1604 * we treat that as permanent, and remove the source
1605 * from the GtkIconSet. (in ensure_filename_pixbuf ()
1606 * B) If loading a themed icon fails, or scaling an icon
1607 * fails, we treat that as transient and will try
1608 * again next time the icon falls out of the cache
1609 * and we need to recreate it.
1611 while (pixbuf == NULL)
1613 GtkIconSource *source = find_best_matching_source (icon_set, direction, state, size, failed);
1618 switch (source->type)
1620 case GTK_ICON_SOURCE_FILENAME:
1621 if (!ensure_filename_pixbuf (icon_set, source))
1624 case GTK_ICON_SOURCE_PIXBUF:
1625 pixbuf = gtk_render_icon_pixbuf (context, source, size);
1628 g_warning ("Failed to render icon");
1629 failed = g_slist_prepend (failed, source);
1632 case GTK_ICON_SOURCE_ICON_NAME:
1633 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1634 pixbuf = render_icon_name_pixbuf (source, context, size);
1636 failed = g_slist_prepend (failed, source);
1638 case GTK_ICON_SOURCE_EMPTY:
1639 g_assert_not_reached ();
1643 g_slist_free (failed);
1648 extern GtkIconCache *_builtin_cache;
1651 render_fallback_image (GtkStyleContext *context,
1652 GtkTextDirection direction,
1656 /* This icon can be used for any direction/state/size */
1657 static GtkIconSource fallback_source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1659 if (fallback_source.type == GTK_ICON_SOURCE_EMPTY)
1664 _gtk_icon_theme_ensure_builtin_cache ();
1666 index = _gtk_icon_cache_get_directory_index (_builtin_cache, "24");
1667 pixbuf = _gtk_icon_cache_get_icon (_builtin_cache, "image-missing", index);
1669 g_return_val_if_fail(pixbuf != NULL, NULL);
1671 gtk_icon_source_set_pixbuf (&fallback_source, pixbuf);
1672 g_object_unref (pixbuf);
1675 return gtk_render_icon_pixbuf (context, &fallback_source, size);
1679 * gtk_icon_set_render_icon_pixbuf:
1680 * @icon_set: a #GtkIconSet
1681 * @context: a #GtkStyleContext
1682 * @size: (type int): icon size. A size of (GtkIconSize)-1
1683 * means render at the size of the source and don't scale.
1685 * Renders an icon using gtk_render_icon_pixbuf(). In most cases,
1686 * gtk_widget_render_icon_pixbuf() is better, since it automatically provides
1687 * most of the arguments from the current widget settings. This
1688 * function never returns %NULL; if the icon can't be rendered
1689 * (perhaps because an image file fails to load), a default "missing
1690 * image" icon will be returned instead.
1692 * Return value: (transfer full): a #GdkPixbuf to be displayed
1697 gtk_icon_set_render_icon_pixbuf (GtkIconSet *icon_set,
1698 GtkStyleContext *context,
1701 GdkPixbuf *icon = NULL;
1702 GtkStateFlags flags = 0;
1704 GtkTextDirection direction;
1706 g_return_val_if_fail (icon_set != NULL, NULL);
1707 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1709 flags = gtk_style_context_get_state (context);
1710 if (flags & GTK_STATE_FLAG_INSENSITIVE)
1711 state = GTK_STATE_INSENSITIVE;
1712 else if (flags & GTK_STATE_FLAG_PRELIGHT)
1713 state = GTK_STATE_PRELIGHT;
1715 state = GTK_STATE_NORMAL;
1717 direction = gtk_style_context_get_direction (context);
1719 if (icon_set->sources)
1721 icon = find_in_cache (icon_set, context, direction, state, size);
1724 g_object_ref (icon);
1729 if (icon_set->sources)
1730 icon = find_and_render_icon_source (icon_set, context, direction, state, size);
1733 icon = render_fallback_image (context, direction, state, size);
1735 add_to_cache (icon_set, context, direction, state, size, icon);
1741 * gtk_icon_set_render_icon:
1742 * @icon_set: a #GtkIconSet
1743 * @style: (allow-none): a #GtkStyle associated with @widget, or %NULL
1744 * @direction: text direction
1745 * @state: widget state
1746 * @size: (type int): icon size. A size of (GtkIconSize)-1
1747 * means render at the size of the source and don't scale.
1748 * @widget: (allow-none): widget that will display the icon, or %NULL.
1749 * The only use that is typically made of this
1750 * is to determine the appropriate #GdkScreen.
1751 * @detail: (allow-none): detail to pass to the theme engine, or %NULL.
1752 * Note that passing a detail of anything but %NULL
1753 * will disable caching.
1755 * Renders an icon using gtk_style_render_icon(). In most cases,
1756 * gtk_widget_render_icon() is better, since it automatically provides
1757 * most of the arguments from the current widget settings. This
1758 * function never returns %NULL; if the icon can't be rendered
1759 * (perhaps because an image file fails to load), a default "missing
1760 * image" icon will be returned instead.
1762 * Return value: (transfer full): a #GdkPixbuf to be displayed
1764 * Deprecated: 3.0: Use gtk_icon_set_render_icon_pixbuf() instead
1767 gtk_icon_set_render_icon (GtkIconSet *icon_set,
1769 GtkTextDirection direction,
1776 GtkStyleContext *context = NULL;
1777 GtkStateFlags flags = 0;
1779 g_return_val_if_fail (icon_set != NULL, NULL);
1780 g_return_val_if_fail (style == NULL || GTK_IS_STYLE (style), NULL);
1782 if (style && gtk_style_has_context (style))
1784 g_object_get (style, "context", &context, NULL);
1785 /* g_object_get returns a refed object */
1787 g_object_unref (context);
1791 context = gtk_widget_get_style_context (widget);
1795 return render_fallback_image (context, direction, state, size);
1797 gtk_style_context_save (context);
1801 case GTK_STATE_PRELIGHT:
1802 flags |= GTK_STATE_FLAG_PRELIGHT;
1804 case GTK_STATE_INSENSITIVE:
1805 flags |= GTK_STATE_FLAG_INSENSITIVE;
1811 gtk_style_context_set_state (context, flags);
1812 gtk_style_context_set_direction (context, direction);
1814 icon = gtk_icon_set_render_icon_pixbuf (icon_set, context, size);
1816 gtk_style_context_restore (context);
1821 /* Order sources by their "wildness", so that "wilder" sources are
1822 * greater than "specific" sources; for determining ordering,
1823 * direction beats state beats size.
1827 icon_source_compare (gconstpointer ap, gconstpointer bp)
1829 const GtkIconSource *a = ap;
1830 const GtkIconSource *b = bp;
1832 if (!a->any_direction && b->any_direction)
1834 else if (a->any_direction && !b->any_direction)
1836 else if (!a->any_state && b->any_state)
1838 else if (a->any_state && !b->any_state)
1840 else if (!a->any_size && b->any_size)
1842 else if (a->any_size && !b->any_size)
1849 * gtk_icon_set_add_source:
1850 * @icon_set: a #GtkIconSet
1851 * @source: a #GtkIconSource
1853 * Icon sets have a list of #GtkIconSource, which they use as base
1854 * icons for rendering icons in different states and sizes. Icons are
1855 * scaled, made to look insensitive, etc. in
1856 * gtk_icon_set_render_icon(), but #GtkIconSet needs base images to
1857 * work with. The base images and when to use them are described by
1860 * This function copies @source, so you can reuse the same source immediately
1861 * without affecting the icon set.
1863 * An example of when you'd use this function: a web browser's "Back
1864 * to Previous Page" icon might point in a different direction in
1865 * Hebrew and in English; it might look different when insensitive;
1866 * and it might change size depending on toolbar mode (small/large
1867 * icons). So a single icon set would contain all those variants of
1868 * the icon, and you might add a separate source for each one.
1870 * You should nearly always add a "default" icon source with all
1871 * fields wildcarded, which will be used as a fallback if no more
1872 * specific source matches. #GtkIconSet always prefers more specific
1873 * icon sources to more generic icon sources. The order in which you
1874 * add the sources to the icon set does not matter.
1876 * gtk_icon_set_new_from_pixbuf() creates a new icon set with a
1877 * default icon source based on the given pixbuf.
1880 gtk_icon_set_add_source (GtkIconSet *icon_set,
1881 const GtkIconSource *source)
1883 g_return_if_fail (icon_set != NULL);
1884 g_return_if_fail (source != NULL);
1886 if (source->type == GTK_ICON_SOURCE_EMPTY)
1888 g_warning ("Useless empty GtkIconSource");
1892 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
1893 gtk_icon_source_copy (source),
1894 icon_source_compare);
1898 * gtk_icon_set_get_sizes:
1899 * @icon_set: a #GtkIconSet
1900 * @sizes: (array length=n_sizes) (out) (type int): return location
1901 * for array of sizes
1902 * @n_sizes: location to store number of elements in returned array
1904 * Obtains a list of icon sizes this icon set can render. The returned
1905 * array must be freed with g_free().
1908 gtk_icon_set_get_sizes (GtkIconSet *icon_set,
1909 GtkIconSize **sizes,
1913 gboolean all_sizes = FALSE;
1914 GSList *specifics = NULL;
1916 g_return_if_fail (icon_set != NULL);
1917 g_return_if_fail (sizes != NULL);
1918 g_return_if_fail (n_sizes != NULL);
1920 tmp_list = icon_set->sources;
1921 while (tmp_list != NULL)
1923 GtkIconSource *source;
1925 source = tmp_list->data;
1927 if (source->any_size)
1933 specifics = g_slist_prepend (specifics, GINT_TO_POINTER (source->size));
1935 tmp_list = g_slist_next (tmp_list);
1940 /* Need to find out what sizes exist */
1945 *sizes = g_new (GtkIconSize, icon_sizes_used);
1946 *n_sizes = icon_sizes_used - 1;
1949 while (i < icon_sizes_used)
1951 (*sizes)[i - 1] = icon_sizes[i].size;
1959 *n_sizes = g_slist_length (specifics);
1960 *sizes = g_new (GtkIconSize, *n_sizes);
1963 tmp_list = specifics;
1964 while (tmp_list != NULL)
1966 (*sizes)[i] = GPOINTER_TO_INT (tmp_list->data);
1969 tmp_list = g_slist_next (tmp_list);
1973 g_slist_free (specifics);
1978 * gtk_icon_source_new:
1980 * Creates a new #GtkIconSource. A #GtkIconSource contains a #GdkPixbuf (or
1981 * image filename) that serves as the base image for one or more of the
1982 * icons in a #GtkIconSet, along with a specification for which icons in the
1983 * icon set will be based on that pixbuf or image file. An icon set contains
1984 * a set of icons that represent "the same" logical concept in different states,
1985 * different global text directions, and different sizes.
1987 * So for example a web browser's "Back to Previous Page" icon might
1988 * point in a different direction in Hebrew and in English; it might
1989 * look different when insensitive; and it might change size depending
1990 * on toolbar mode (small/large icons). So a single icon set would
1991 * contain all those variants of the icon. #GtkIconSet contains a list
1992 * of #GtkIconSource from which it can derive specific icon variants in
1995 * In the simplest case, #GtkIconSet contains one source pixbuf from
1996 * which it derives all variants. The convenience function
1997 * gtk_icon_set_new_from_pixbuf() handles this case; if you only have
1998 * one source pixbuf, just use that function.
2000 * If you want to use a different base pixbuf for different icon
2001 * variants, you create multiple icon sources, mark which variants
2002 * they'll be used to create, and add them to the icon set with
2003 * gtk_icon_set_add_source().
2005 * By default, the icon source has all parameters wildcarded. That is,
2006 * the icon source will be used as the base icon for any desired text
2007 * direction, widget state, or icon size.
2009 * Return value: a new #GtkIconSource
2012 gtk_icon_source_new (void)
2016 src = g_new0 (GtkIconSource, 1);
2018 src->direction = GTK_TEXT_DIR_NONE;
2019 src->size = GTK_ICON_SIZE_INVALID;
2020 src->state = GTK_STATE_NORMAL;
2022 src->any_direction = TRUE;
2023 src->any_state = TRUE;
2024 src->any_size = TRUE;
2030 * gtk_icon_source_copy:
2031 * @source: a #GtkIconSource
2033 * Creates a copy of @source; mostly useful for language bindings.
2035 * Return value: a new #GtkIconSource
2038 gtk_icon_source_copy (const GtkIconSource *source)
2040 GtkIconSource *copy;
2042 g_return_val_if_fail (source != NULL, NULL);
2044 copy = g_new (GtkIconSource, 1);
2050 case GTK_ICON_SOURCE_EMPTY:
2051 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
2053 case GTK_ICON_SOURCE_ICON_NAME:
2054 copy->source.icon_name = g_strdup (copy->source.icon_name);
2056 case GTK_ICON_SOURCE_FILENAME:
2057 copy->source.filename = g_strdup (copy->source.filename);
2058 if (copy->filename_pixbuf)
2059 g_object_ref (copy->filename_pixbuf);
2061 case GTK_ICON_SOURCE_PIXBUF:
2062 g_object_ref (copy->source.pixbuf);
2065 g_assert_not_reached();
2072 * gtk_icon_source_free:
2073 * @source: a #GtkIconSource
2075 * Frees a dynamically-allocated icon source, along with its
2076 * filename, size, and pixbuf fields if those are not %NULL.
2079 gtk_icon_source_free (GtkIconSource *source)
2081 g_return_if_fail (source != NULL);
2083 icon_source_clear (source);
2087 G_DEFINE_BOXED_TYPE (GtkIconSource, gtk_icon_source,
2088 gtk_icon_source_copy,
2089 gtk_icon_source_free)
2092 icon_source_clear (GtkIconSource *source)
2094 switch (source->type)
2096 case GTK_ICON_SOURCE_EMPTY:
2098 case GTK_ICON_SOURCE_ICON_NAME:
2099 g_free (source->source.icon_name);
2101 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
2102 source->source.icon_name = NULL;
2104 case GTK_ICON_SOURCE_FILENAME:
2105 g_free (source->source.filename);
2106 source->source.filename = NULL;
2107 if (source->filename_pixbuf)
2108 g_object_unref (source->filename_pixbuf);
2109 source->filename_pixbuf = NULL;
2111 case GTK_ICON_SOURCE_PIXBUF:
2112 g_object_unref (source->source.pixbuf);
2113 source->source.pixbuf = NULL;
2116 g_assert_not_reached();
2119 source->type = GTK_ICON_SOURCE_EMPTY;
2123 * gtk_icon_source_set_filename:
2124 * @source: a #GtkIconSource
2125 * @filename: (type filename): image file to use
2127 * Sets the name of an image file to use as a base image when creating
2128 * icon variants for #GtkIconSet. The filename must be absolute.
2131 gtk_icon_source_set_filename (GtkIconSource *source,
2132 const gchar *filename)
2134 g_return_if_fail (source != NULL);
2135 g_return_if_fail (filename == NULL || g_path_is_absolute (filename));
2137 if (source->type == GTK_ICON_SOURCE_FILENAME &&
2138 source->source.filename == filename)
2141 icon_source_clear (source);
2143 if (filename != NULL)
2145 source->type = GTK_ICON_SOURCE_FILENAME;
2146 source->source.filename = g_strdup (filename);
2151 * gtk_icon_source_set_icon_name
2152 * @source: a #GtkIconSource
2153 * @icon_name: (allow-none): name of icon to use
2155 * Sets the name of an icon to look up in the current icon theme
2156 * to use as a base image when creating icon variants for #GtkIconSet.
2159 gtk_icon_source_set_icon_name (GtkIconSource *source,
2160 const gchar *icon_name)
2162 g_return_if_fail (source != NULL);
2164 if (source->type == GTK_ICON_SOURCE_ICON_NAME &&
2165 source->source.icon_name == icon_name)
2168 icon_source_clear (source);
2170 if (icon_name != NULL)
2172 source->type = GTK_ICON_SOURCE_ICON_NAME;
2173 source->source.icon_name = g_strdup (icon_name);
2178 * gtk_icon_source_set_pixbuf:
2179 * @source: a #GtkIconSource
2180 * @pixbuf: pixbuf to use as a source
2182 * Sets a pixbuf to use as a base image when creating icon variants
2186 gtk_icon_source_set_pixbuf (GtkIconSource *source,
2189 g_return_if_fail (source != NULL);
2190 g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
2192 if (source->type == GTK_ICON_SOURCE_PIXBUF &&
2193 source->source.pixbuf == pixbuf)
2196 icon_source_clear (source);
2200 source->type = GTK_ICON_SOURCE_PIXBUF;
2201 source->source.pixbuf = g_object_ref (pixbuf);
2206 * gtk_icon_source_get_filename:
2207 * @source: a #GtkIconSource
2209 * Retrieves the source filename, or %NULL if none is set. The
2210 * filename is not a copy, and should not be modified or expected to
2211 * persist beyond the lifetime of the icon source.
2213 * Return value: (type filename): image filename. This string must not
2214 * be modified or freed.
2217 gtk_icon_source_get_filename (const GtkIconSource *source)
2219 g_return_val_if_fail (source != NULL, NULL);
2221 if (source->type == GTK_ICON_SOURCE_FILENAME)
2222 return source->source.filename;
2228 * gtk_icon_source_get_icon_name:
2229 * @source: a #GtkIconSource
2231 * Retrieves the source icon name, or %NULL if none is set. The
2232 * icon_name is not a copy, and should not be modified or expected to
2233 * persist beyond the lifetime of the icon source.
2235 * Return value: icon name. This string must not be modified or freed.
2238 gtk_icon_source_get_icon_name (const GtkIconSource *source)
2240 g_return_val_if_fail (source != NULL, NULL);
2242 if (source->type == GTK_ICON_SOURCE_ICON_NAME ||
2243 source->type == GTK_ICON_SOURCE_STATIC_ICON_NAME)
2244 return source->source.icon_name;
2250 * gtk_icon_source_get_pixbuf:
2251 * @source: a #GtkIconSource
2253 * Retrieves the source pixbuf, or %NULL if none is set.
2254 * In addition, if a filename source is in use, this
2255 * function in some cases will return the pixbuf from
2256 * loaded from the filename. This is, for example, true
2257 * for the GtkIconSource passed to the GtkStyle::render_icon()
2258 * virtual function. The reference count on the pixbuf is
2261 * Return value: (transfer none): source pixbuf
2264 gtk_icon_source_get_pixbuf (const GtkIconSource *source)
2266 g_return_val_if_fail (source != NULL, NULL);
2268 if (source->type == GTK_ICON_SOURCE_PIXBUF)
2269 return source->source.pixbuf;
2270 else if (source->type == GTK_ICON_SOURCE_FILENAME)
2271 return source->filename_pixbuf;
2277 * gtk_icon_source_set_direction_wildcarded:
2278 * @source: a #GtkIconSource
2279 * @setting: %TRUE to wildcard the text direction
2281 * If the text direction is wildcarded, this source can be used
2282 * as the base image for an icon in any #GtkTextDirection.
2283 * If the text direction is not wildcarded, then the
2284 * text direction the icon source applies to should be set
2285 * with gtk_icon_source_set_direction(), and the icon source
2286 * will only be used with that text direction.
2288 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2289 * wildcarded sources, and will use an exact match when possible.
2292 gtk_icon_source_set_direction_wildcarded (GtkIconSource *source,
2295 g_return_if_fail (source != NULL);
2297 source->any_direction = setting != FALSE;
2301 * gtk_icon_source_set_state_wildcarded:
2302 * @source: a #GtkIconSource
2303 * @setting: %TRUE to wildcard the widget state
2305 * If the widget state is wildcarded, this source can be used as the
2306 * base image for an icon in any #GtkStateType. If the widget state
2307 * is not wildcarded, then the state the source applies to should be
2308 * set with gtk_icon_source_set_state() and the icon source will
2309 * only be used with that specific state.
2311 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2312 * wildcarded sources, and will use an exact match when possible.
2314 * #GtkIconSet will normally transform wildcarded source images to
2315 * produce an appropriate icon for a given state, for example
2316 * lightening an image on prelight, but will not modify source images
2317 * that match exactly.
2320 gtk_icon_source_set_state_wildcarded (GtkIconSource *source,
2323 g_return_if_fail (source != NULL);
2325 source->any_state = setting != FALSE;
2330 * gtk_icon_source_set_size_wildcarded:
2331 * @source: a #GtkIconSource
2332 * @setting: %TRUE to wildcard the widget state
2334 * If the icon size is wildcarded, this source can be used as the base
2335 * image for an icon of any size. If the size is not wildcarded, then
2336 * the size the source applies to should be set with
2337 * gtk_icon_source_set_size() and the icon source will only be used
2338 * with that specific size.
2340 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2341 * wildcarded sources, and will use an exact match when possible.
2343 * #GtkIconSet will normally scale wildcarded source images to produce
2344 * an appropriate icon at a given size, but will not change the size
2345 * of source images that match exactly.
2348 gtk_icon_source_set_size_wildcarded (GtkIconSource *source,
2351 g_return_if_fail (source != NULL);
2353 source->any_size = setting != FALSE;
2357 * gtk_icon_source_get_size_wildcarded:
2358 * @source: a #GtkIconSource
2360 * Gets the value set by gtk_icon_source_set_size_wildcarded().
2362 * Return value: %TRUE if this icon source is a base for any icon size variant
2365 gtk_icon_source_get_size_wildcarded (const GtkIconSource *source)
2367 g_return_val_if_fail (source != NULL, TRUE);
2369 return source->any_size;
2373 * gtk_icon_source_get_state_wildcarded:
2374 * @source: a #GtkIconSource
2376 * Gets the value set by gtk_icon_source_set_state_wildcarded().
2378 * Return value: %TRUE if this icon source is a base for any widget state variant
2381 gtk_icon_source_get_state_wildcarded (const GtkIconSource *source)
2383 g_return_val_if_fail (source != NULL, TRUE);
2385 return source->any_state;
2389 * gtk_icon_source_get_direction_wildcarded:
2390 * @source: a #GtkIconSource
2392 * Gets the value set by gtk_icon_source_set_direction_wildcarded().
2394 * Return value: %TRUE if this icon source is a base for any text direction variant
2397 gtk_icon_source_get_direction_wildcarded (const GtkIconSource *source)
2399 g_return_val_if_fail (source != NULL, TRUE);
2401 return source->any_direction;
2405 * gtk_icon_source_set_direction:
2406 * @source: a #GtkIconSource
2407 * @direction: text direction this source applies to
2409 * Sets the text direction this icon source is intended to be used
2412 * Setting the text direction on an icon source makes no difference
2413 * if the text direction is wildcarded. Therefore, you should usually
2414 * call gtk_icon_source_set_direction_wildcarded() to un-wildcard it
2415 * in addition to calling this function.
2418 gtk_icon_source_set_direction (GtkIconSource *source,
2419 GtkTextDirection direction)
2421 g_return_if_fail (source != NULL);
2423 source->direction = direction;
2427 * gtk_icon_source_set_state:
2428 * @source: a #GtkIconSource
2429 * @state: widget state this source applies to
2431 * Sets the widget state this icon source is intended to be used
2434 * Setting the widget state on an icon source makes no difference
2435 * if the state is wildcarded. Therefore, you should usually
2436 * call gtk_icon_source_set_state_wildcarded() to un-wildcard it
2437 * in addition to calling this function.
2440 gtk_icon_source_set_state (GtkIconSource *source,
2443 g_return_if_fail (source != NULL);
2445 source->state = state;
2449 * gtk_icon_source_set_size:
2450 * @source: a #GtkIconSource
2451 * @size: (type int): icon size this source applies to
2453 * Sets the icon size this icon source is intended to be used
2456 * Setting the icon size on an icon source makes no difference
2457 * if the size is wildcarded. Therefore, you should usually
2458 * call gtk_icon_source_set_size_wildcarded() to un-wildcard it
2459 * in addition to calling this function.
2462 gtk_icon_source_set_size (GtkIconSource *source,
2465 g_return_if_fail (source != NULL);
2467 source->size = size;
2471 * gtk_icon_source_get_direction:
2472 * @source: a #GtkIconSource
2474 * Obtains the text direction this icon source applies to. The return
2475 * value is only useful/meaningful if the text direction is <emphasis>not</emphasis>
2478 * Return value: text direction this source matches
2481 gtk_icon_source_get_direction (const GtkIconSource *source)
2483 g_return_val_if_fail (source != NULL, 0);
2485 return source->direction;
2489 * gtk_icon_source_get_state:
2490 * @source: a #GtkIconSource
2492 * Obtains the widget state this icon source applies to. The return
2493 * value is only useful/meaningful if the widget state is <emphasis>not</emphasis>
2496 * Return value: widget state this source matches
2499 gtk_icon_source_get_state (const GtkIconSource *source)
2501 g_return_val_if_fail (source != NULL, 0);
2503 return source->state;
2507 * gtk_icon_source_get_size:
2508 * @source: a #GtkIconSource
2510 * Obtains the icon size this source applies to. The return value
2511 * is only useful/meaningful if the icon size is <emphasis>not</emphasis> wildcarded.
2513 * Return value: (type int): icon size this source matches.
2516 gtk_icon_source_get_size (const GtkIconSource *source)
2518 g_return_val_if_fail (source != NULL, 0);
2520 return source->size;
2523 #define NUM_CACHED_ICONS 8
2525 typedef struct _CachedIcon CachedIcon;
2529 /* These must all match to use the cached pixbuf.
2530 * If any don't match, we must re-render the pixbuf.
2532 GtkStyleContext *style;
2533 GtkTextDirection direction;
2541 ensure_cache_up_to_date (GtkIconSet *icon_set)
2543 if (icon_set->cache_serial != cache_serial)
2545 clear_cache (icon_set, TRUE);
2546 icon_set->cache_serial = cache_serial;
2551 cached_icon_free (CachedIcon *icon)
2553 g_object_unref (icon->pixbuf);
2554 g_object_unref (icon->style);
2560 find_in_cache (GtkIconSet *icon_set,
2561 GtkStyleContext *style_context,
2562 GtkTextDirection direction,
2569 ensure_cache_up_to_date (icon_set);
2572 tmp_list = icon_set->cache;
2573 while (tmp_list != NULL)
2575 CachedIcon *icon = tmp_list->data;
2577 if (icon->style == style_context &&
2578 icon->direction == direction &&
2579 icon->state == state &&
2580 (size == (GtkIconSize)-1 || icon->size == size))
2584 /* Move this icon to the front of the list. */
2585 prev->next = tmp_list->next;
2586 tmp_list->next = icon_set->cache;
2587 icon_set->cache = tmp_list;
2590 return icon->pixbuf;
2594 tmp_list = g_slist_next (tmp_list);
2601 add_to_cache (GtkIconSet *icon_set,
2602 GtkStyleContext *style_context,
2603 GtkTextDirection direction,
2610 ensure_cache_up_to_date (icon_set);
2612 g_object_ref (pixbuf);
2614 icon = g_new (CachedIcon, 1);
2615 icon_set->cache = g_slist_prepend (icon_set->cache, icon);
2616 icon_set->cache_size++;
2618 icon->style = g_object_ref (style_context);
2619 icon->direction = direction;
2620 icon->state = state;
2622 icon->pixbuf = pixbuf;
2623 attach_to_style (icon_set, icon->style);
2625 if (icon_set->cache_size >= NUM_CACHED_ICONS)
2627 /* Remove oldest item in the cache */
2630 tmp_list = icon_set->cache;
2632 /* Find next-to-last link */
2633 g_assert (NUM_CACHED_ICONS > 2);
2634 while (tmp_list->next->next)
2635 tmp_list = tmp_list->next;
2637 g_assert (tmp_list != NULL);
2638 g_assert (tmp_list->next != NULL);
2639 g_assert (tmp_list->next->next == NULL);
2641 /* Free the last icon */
2642 icon = tmp_list->next->data;
2644 g_slist_free (tmp_list->next);
2645 tmp_list->next = NULL;
2647 cached_icon_free (icon);
2652 clear_cache (GtkIconSet *icon_set,
2653 gboolean style_detach)
2655 GSList *cache, *tmp_list;
2656 GtkStyleContext *last_style = NULL;
2658 cache = icon_set->cache;
2659 icon_set->cache = NULL;
2660 icon_set->cache_size = 0;
2662 while (tmp_list != NULL)
2664 CachedIcon *icon = tmp_list->data;
2668 /* simple optimization for the case where the cache
2669 * contains contiguous icons from the same style.
2670 * it's safe to call detach_from_style more than
2671 * once on the same style though.
2673 if (last_style != icon->style)
2675 detach_from_style (icon_set, icon->style);
2676 last_style = icon->style;
2680 cached_icon_free (icon);
2682 tmp_list = g_slist_next (tmp_list);
2685 g_slist_free (cache);
2689 copy_cache (GtkIconSet *icon_set,
2690 GtkIconSet *copy_recipient)
2693 GSList *copy = NULL;
2695 ensure_cache_up_to_date (icon_set);
2697 tmp_list = icon_set->cache;
2698 while (tmp_list != NULL)
2700 CachedIcon *icon = tmp_list->data;
2701 CachedIcon *icon_copy = g_new (CachedIcon, 1);
2705 attach_to_style (copy_recipient, icon_copy->style);
2706 g_object_ref (icon_copy->style);
2708 g_object_ref (icon_copy->pixbuf);
2710 icon_copy->size = icon->size;
2712 copy = g_slist_prepend (copy, icon_copy);
2714 tmp_list = g_slist_next (tmp_list);
2717 return g_slist_reverse (copy);
2721 attach_to_style (GtkIconSet *icon_set,
2722 GtkStyleContext *style_context)
2726 table = g_object_get_qdata (G_OBJECT (style_context),
2727 g_quark_try_string ("gtk-style-icon-sets"));
2731 table = g_hash_table_new (NULL, NULL);
2732 g_object_set_qdata_full (G_OBJECT (style_context),
2733 g_quark_from_static_string ("gtk-style-icon-sets"),
2738 g_hash_table_insert (table, icon_set, icon_set);
2742 detach_from_style (GtkIconSet *icon_set,
2743 GtkStyleContext *style_context)
2747 table = g_object_get_qdata (G_OBJECT (style_context),
2748 g_quark_try_string ("gtk-style-icon-sets"));
2751 g_hash_table_remove (table, icon_set);
2755 iconsets_foreach (gpointer key,
2759 GtkIconSet *icon_set = key;
2761 /* We only need to remove cache entries for the given style;
2762 * but that complicates things because in destroy notify
2763 * we don't know which style got destroyed, and 95% of the
2764 * time all cache entries will have the same style,
2765 * so this is faster anyway.
2768 clear_cache (icon_set, FALSE);
2772 style_dnotify (gpointer data)
2774 GHashTable *table = data;
2776 g_hash_table_foreach (table, iconsets_foreach, NULL);
2778 g_hash_table_destroy (table);
2781 /* This allows the icon set to detect that its cache is out of date. */
2783 _gtk_icon_set_invalidate_caches (void)
2789 * _gtk_icon_factory_list_ids:
2791 * Gets all known IDs stored in an existing icon factory.
2792 * The strings in the returned list aren't copied.
2793 * The list itself should be freed.
2795 * Return value: List of ids in icon factories
2798 _gtk_icon_factory_list_ids (void)
2805 _gtk_icon_factory_ensure_default_icons ();
2807 tmp_list = all_icon_factories;
2808 while (tmp_list != NULL)
2811 GtkIconFactory *factory = GTK_ICON_FACTORY (tmp_list->data);
2812 GtkIconFactoryPrivate *priv = factory->priv;
2814 these_ids = g_hash_table_get_keys (priv->icons);
2816 ids = g_list_concat (ids, these_ids);
2818 tmp_list = g_slist_next (tmp_list);
2828 } IconFactoryParserData;
2834 GtkTextDirection direction;
2837 } IconSourceParserData;
2840 icon_source_start_element (GMarkupParseContext *context,
2841 const gchar *element_name,
2842 const gchar **names,
2843 const gchar **values,
2848 gchar *stock_id = NULL;
2849 gchar *filename = NULL;
2850 gchar *icon_name = NULL;
2852 gint direction = -1;
2854 IconFactoryParserData *parser_data;
2855 IconSourceParserData *source_data;
2857 GQuark error_domain;
2859 parser_data = (IconFactoryParserData*)user_data;
2861 if (!parser_data->in_source)
2863 if (strcmp (element_name, "sources") != 0)
2865 error_msg = g_strdup_printf ("Unexpected element %s, expected <sources>", element_name);
2866 error_domain = GTK_BUILDER_ERROR_INVALID_TAG;
2869 parser_data->in_source = TRUE;
2874 if (strcmp (element_name, "source") != 0)
2876 error_msg = g_strdup_printf ("Unexpected element %s, expected <source>", element_name);
2877 error_domain = GTK_BUILDER_ERROR_INVALID_TAG;
2882 for (i = 0; names[i]; i++)
2884 if (strcmp (names[i], "stock-id") == 0)
2885 stock_id = g_strdup (values[i]);
2886 else if (strcmp (names[i], "filename") == 0)
2887 filename = g_strdup (values[i]);
2888 else if (strcmp (names[i], "icon-name") == 0)
2889 icon_name = g_strdup (values[i]);
2890 else if (strcmp (names[i], "size") == 0)
2892 if (!_gtk_builder_enum_from_string (GTK_TYPE_ICON_SIZE,
2898 else if (strcmp (names[i], "direction") == 0)
2900 if (!_gtk_builder_enum_from_string (GTK_TYPE_TEXT_DIRECTION,
2906 else if (strcmp (names[i], "state") == 0)
2908 if (!_gtk_builder_enum_from_string (GTK_TYPE_STATE_TYPE,
2916 error_msg = g_strdup_printf ("'%s' is not a valid attribute of <%s>",
2917 names[i], "source");
2918 error_domain = GTK_BUILDER_ERROR_INVALID_ATTRIBUTE;
2925 error_msg = g_strdup_printf ("<source> requires a stock_id");
2926 error_domain = GTK_BUILDER_ERROR_MISSING_ATTRIBUTE;
2930 source_data = g_slice_new (IconSourceParserData);
2931 source_data->stock_id = stock_id;
2932 source_data->filename = filename;
2933 source_data->icon_name = icon_name;
2934 source_data->size = size;
2935 source_data->direction = direction;
2936 source_data->state = state;
2938 parser_data->sources = g_slist_prepend (parser_data->sources, source_data);
2944 gint line_number, char_number;
2946 g_markup_parse_context_get_position (context, &line_number, &char_number);
2948 tmp = g_strdup_printf ("%s:%d:%d %s", "input",
2949 line_number, char_number, error_msg);
2950 g_set_error_literal (error, GTK_BUILDER_ERROR, error_domain, tmp);
2959 static const GMarkupParser icon_source_parser =
2961 icon_source_start_element,
2965 gtk_icon_factory_buildable_custom_tag_start (GtkBuildable *buildable,
2966 GtkBuilder *builder,
2968 const gchar *tagname,
2969 GMarkupParser *parser,
2972 g_assert (buildable);
2974 if (strcmp (tagname, "sources") == 0)
2976 IconFactoryParserData *parser_data;
2978 parser_data = g_slice_new0 (IconFactoryParserData);
2979 *parser = icon_source_parser;
2980 *data = parser_data;
2987 gtk_icon_factory_buildable_custom_tag_end (GtkBuildable *buildable,
2988 GtkBuilder *builder,
2990 const gchar *tagname,
2991 gpointer *user_data)
2993 GtkIconFactory *icon_factory;
2995 icon_factory = GTK_ICON_FACTORY (buildable);
2997 if (strcmp (tagname, "sources") == 0)
2999 IconFactoryParserData *parser_data;
3000 GtkIconSource *icon_source;
3001 GtkIconSet *icon_set;
3004 parser_data = (IconFactoryParserData*)user_data;
3006 for (l = parser_data->sources; l; l = l->next)
3008 IconSourceParserData *source_data = l->data;
3010 icon_set = gtk_icon_factory_lookup (icon_factory, source_data->stock_id);
3013 icon_set = gtk_icon_set_new ();
3014 gtk_icon_factory_add (icon_factory, source_data->stock_id, icon_set);
3015 gtk_icon_set_unref (icon_set);
3018 icon_source = gtk_icon_source_new ();
3020 if (source_data->filename)
3023 filename = _gtk_builder_get_absolute_filename (builder, source_data->filename);
3024 gtk_icon_source_set_filename (icon_source, filename);
3027 if (source_data->icon_name)
3028 gtk_icon_source_set_icon_name (icon_source, source_data->icon_name);
3029 if (source_data->size != -1)
3031 gtk_icon_source_set_size (icon_source, source_data->size);
3032 gtk_icon_source_set_size_wildcarded (icon_source, FALSE);
3034 if (source_data->direction != -1)
3036 gtk_icon_source_set_direction (icon_source, source_data->direction);
3037 gtk_icon_source_set_direction_wildcarded (icon_source, FALSE);
3039 if (source_data->state != -1)
3041 gtk_icon_source_set_state (icon_source, source_data->state);
3042 gtk_icon_source_set_state_wildcarded (icon_source, FALSE);
3045 /* Inline source_add() to avoid creating a copy */
3046 g_assert (icon_source->type != GTK_ICON_SOURCE_EMPTY);
3047 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
3049 icon_source_compare);
3051 g_free (source_data->stock_id);
3052 g_free (source_data->filename);
3053 g_free (source_data->icon_name);
3054 g_slice_free (IconSourceParserData, source_data);
3056 g_slist_free (parser_data->sources);
3057 g_slice_free (IconFactoryParserData, parser_data);
3059 /* TODO: Add an attribute/tag to prevent this.
3060 * Usually it's the right thing to do though.
3062 gtk_icon_factory_add_default (icon_factory);