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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkiconfactory.h"
32 #include "gtkiconcache.h"
34 #include "gtkicontheme.h"
35 #include "gtksettingsprivate.h"
37 #include "gtkwidget.h"
39 #include "gtkbuildable.h"
40 #include "gtkbuilderprivate.h"
41 #include "gtktypebuiltins.h"
42 #include "deprecated/gtkstyle.h"
46 * SECTION:gtkiconfactory
47 * @Short_description: Manipulating stock icons
48 * @Title: Themeable Stock Images
50 * Browse the available stock icons in the list of stock IDs found <link
51 * linkend="gtk-Stock-Items">here</link>. You can also use
52 * the <application>gtk-demo</application> application for this purpose.
54 * An icon factory manages a collection of #GtkIconSet; a #GtkIconSet manages a
55 * set of variants of a particular icon (i.e. a #GtkIconSet contains variants for
56 * different sizes and widget states). Icons in an icon factory are named by a
57 * stock ID, which is a simple string identifying the icon. Each #GtkStyle has a
58 * list of #GtkIconFactory derived from the current theme; those icon factories
59 * are consulted first when searching for an icon. If the theme doesn't set a
60 * particular icon, GTK+ looks for the icon in a list of default icon factories,
61 * maintained by gtk_icon_factory_add_default() and
62 * gtk_icon_factory_remove_default(). Applications with icons should add a default
63 * icon factory with their icons, which will allow themes to override the icons
64 * for the application.
66 * To display an icon, always use gtk_style_lookup_icon_set() on the widget that
67 * will display the icon, or the convenience function
68 * gtk_widget_render_icon(). These functions take the theme into account when
69 * looking up the icon to use for a given stock ID.
71 * <refsect2 id="GtkIconFactory-BUILDER-UI">
72 * <title>GtkIconFactory as GtkBuildable</title>
74 * GtkIconFactory supports a custom <sources> element, which can contain
75 * multiple <source> elements.
76 * The following attributes are allowed:
79 * <term>stock-id</term>
81 * The stock id of the source, a string.
82 * This attribute is mandatory
86 * <term>filename</term>
88 * The filename of the source, a string.
89 * This attribute is optional
93 * <term>icon-name</term>
95 * The icon name for the source, a string.
96 * This attribute is optional.
102 * Size of the icon, a #GtkIconSize enum value.
103 * This attribute is optional.
107 * <term>direction</term>
109 * Direction of the source, a #GtkTextDirection enum value.
110 * This attribute is optional.
116 * State of the source, a #GtkStateType enum value.
117 * This attribute is optional.
122 * <title>A #GtkIconFactory UI definition fragment.</title>
123 * <programlisting><![CDATA[
124 * <object class="GtkIconFactory" id="iconfactory1">
126 * <source stock-id="apple-red" filename="apple-red.png"/>
129 * <object class="GtkWindow" id="window1">
131 * <object class="GtkButton" id="apple_button">
132 * <property name="label">apple-red</property>
133 * <property name="use-stock">True</property>
145 static GSList *all_icon_factories = NULL;
147 struct _GtkIconFactoryPrivate
153 GTK_ICON_SOURCE_EMPTY,
154 GTK_ICON_SOURCE_ICON_NAME,
155 GTK_ICON_SOURCE_STATIC_ICON_NAME,
156 GTK_ICON_SOURCE_FILENAME,
157 GTK_ICON_SOURCE_PIXBUF
160 struct _GtkIconSource
162 GtkIconSourceType type;
170 GdkPixbuf *filename_pixbuf;
172 GtkTextDirection direction;
176 /* If TRUE, then the parameter is wildcarded, and the above
177 * fields should be ignored. If FALSE, the parameter is
178 * specified, and the above fields should be valid.
180 guint any_direction : 1;
187 gtk_icon_factory_buildable_init (GtkBuildableIface *iface);
189 static gboolean gtk_icon_factory_buildable_custom_tag_start (GtkBuildable *buildable,
192 const gchar *tagname,
193 GMarkupParser *parser,
195 static void gtk_icon_factory_buildable_custom_tag_end (GtkBuildable *buildable,
198 const gchar *tagname,
199 gpointer *user_data);
200 static void gtk_icon_factory_finalize (GObject *object);
201 static void get_default_icons (GtkIconFactory *icon_factory);
202 static void icon_source_clear (GtkIconSource *source);
204 static GtkIconSize icon_size_register_intern (const gchar *name,
208 #define GTK_ICON_SOURCE_INIT(any_direction, any_state, any_size) \
209 { GTK_ICON_SOURCE_EMPTY, { NULL }, NULL, \
211 any_direction, any_state, any_size }
213 G_DEFINE_TYPE_WITH_CODE (GtkIconFactory, gtk_icon_factory, G_TYPE_OBJECT,
214 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
215 gtk_icon_factory_buildable_init))
218 gtk_icon_factory_init (GtkIconFactory *factory)
220 GtkIconFactoryPrivate *priv;
222 factory->priv = G_TYPE_INSTANCE_GET_PRIVATE (factory,
223 GTK_TYPE_ICON_FACTORY,
224 GtkIconFactoryPrivate);
225 priv = factory->priv;
227 priv->icons = g_hash_table_new (g_str_hash, g_str_equal);
228 all_icon_factories = g_slist_prepend (all_icon_factories, factory);
232 gtk_icon_factory_class_init (GtkIconFactoryClass *klass)
234 GObjectClass *object_class = G_OBJECT_CLASS (klass);
236 object_class->finalize = gtk_icon_factory_finalize;
238 g_type_class_add_private (klass, sizeof (GtkIconFactoryPrivate));
242 gtk_icon_factory_buildable_init (GtkBuildableIface *iface)
244 iface->custom_tag_start = gtk_icon_factory_buildable_custom_tag_start;
245 iface->custom_tag_end = gtk_icon_factory_buildable_custom_tag_end;
249 free_icon_set (gpointer key, gpointer value, gpointer data)
252 gtk_icon_set_unref (value);
256 gtk_icon_factory_finalize (GObject *object)
258 GtkIconFactory *factory = GTK_ICON_FACTORY (object);
259 GtkIconFactoryPrivate *priv = factory->priv;
261 all_icon_factories = g_slist_remove (all_icon_factories, factory);
263 g_hash_table_foreach (priv->icons, free_icon_set, NULL);
265 g_hash_table_destroy (priv->icons);
267 G_OBJECT_CLASS (gtk_icon_factory_parent_class)->finalize (object);
271 * gtk_icon_factory_new:
273 * Creates a new #GtkIconFactory. An icon factory manages a collection
274 * of #GtkIconSet<!-- -->s; a #GtkIconSet manages a set of variants of a
275 * particular icon (i.e. a #GtkIconSet contains variants for different
276 * sizes and widget states). Icons in an icon factory are named by a
277 * stock ID, which is a simple string identifying the icon. Each
278 * #GtkStyle has a list of #GtkIconFactory<!-- -->s derived from the current
279 * theme; those icon factories are consulted first when searching for
280 * an icon. If the theme doesn't set a particular icon, GTK+ looks for
281 * the icon in a list of default icon factories, maintained by
282 * gtk_icon_factory_add_default() and
283 * gtk_icon_factory_remove_default(). Applications with icons should
284 * add a default icon factory with their icons, which will allow
285 * themes to override the icons for the application.
287 * Return value: a new #GtkIconFactory
290 gtk_icon_factory_new (void)
292 return g_object_new (GTK_TYPE_ICON_FACTORY, NULL);
296 * gtk_icon_factory_add:
297 * @factory: a #GtkIconFactory
298 * @stock_id: icon name
299 * @icon_set: icon set
301 * Adds the given @icon_set to the icon factory, under the name
302 * @stock_id. @stock_id should be namespaced for your application,
303 * e.g. "myapp-whatever-icon". Normally applications create a
304 * #GtkIconFactory, then add it to the list of default factories with
305 * gtk_icon_factory_add_default(). Then they pass the @stock_id to
306 * widgets such as #GtkImage to display the icon. Themes can provide
307 * an icon with the same name (such as "myapp-whatever-icon") to
308 * override your application's default icons. If an icon already
309 * existed in @factory for @stock_id, it is unreferenced and replaced
310 * with the new @icon_set.
313 gtk_icon_factory_add (GtkIconFactory *factory,
314 const gchar *stock_id,
315 GtkIconSet *icon_set)
317 GtkIconFactoryPrivate *priv = factory->priv;
318 gpointer old_key = NULL;
319 gpointer old_value = NULL;
321 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
322 g_return_if_fail (stock_id != NULL);
323 g_return_if_fail (icon_set != NULL);
325 g_hash_table_lookup_extended (priv->icons, stock_id,
326 &old_key, &old_value);
328 if (old_value == icon_set)
331 gtk_icon_set_ref (icon_set);
333 /* GHashTable key memory management is so fantastically broken. */
335 g_hash_table_insert (priv->icons, old_key, icon_set);
337 g_hash_table_insert (priv->icons, g_strdup (stock_id), icon_set);
340 gtk_icon_set_unref (old_value);
344 * gtk_icon_factory_lookup:
345 * @factory: a #GtkIconFactory
346 * @stock_id: an icon name
348 * Looks up @stock_id in the icon factory, returning an icon set
349 * if found, otherwise %NULL. For display to the user, you should
350 * use gtk_style_lookup_icon_set() on the #GtkStyle for the
351 * widget that will display the icon, instead of using this
352 * function directly, so that themes are taken into account.
354 * Return value: (transfer none): icon set of @stock_id.
357 gtk_icon_factory_lookup (GtkIconFactory *factory,
358 const gchar *stock_id)
360 GtkIconFactoryPrivate *priv;
362 g_return_val_if_fail (GTK_IS_ICON_FACTORY (factory), NULL);
363 g_return_val_if_fail (stock_id != NULL, NULL);
365 priv = factory->priv;
367 return g_hash_table_lookup (priv->icons, stock_id);
370 static GSList *default_factories = NULL;
373 * gtk_icon_factory_add_default:
374 * @factory: a #GtkIconFactory
376 * Adds an icon factory to the list of icon factories searched by
377 * gtk_style_lookup_icon_set(). This means that, for example,
378 * gtk_image_new_from_stock() will be able to find icons in @factory.
379 * There will normally be an icon factory added for each library or
380 * application that comes with icons. The default icon factories
381 * can be overridden by themes.
384 gtk_icon_factory_add_default (GtkIconFactory *factory)
386 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
388 g_object_ref (factory);
390 default_factories = g_slist_prepend (default_factories, factory);
394 * gtk_icon_factory_remove_default:
395 * @factory: a #GtkIconFactory previously added with gtk_icon_factory_add_default()
397 * Removes an icon factory from the list of default icon
398 * factories. Not normally used; you might use it for a library that
399 * can be unloaded or shut down.
402 gtk_icon_factory_remove_default (GtkIconFactory *factory)
404 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
406 default_factories = g_slist_remove (default_factories, factory);
408 g_object_unref (factory);
411 static GtkIconFactory *
412 _gtk_icon_factory_get_default_icons (void)
414 static GtkIconFactory *default_icons = NULL;
415 GtkIconFactory *icons = NULL;
416 GdkScreen *screen = gdk_screen_get_default ();
419 icons = g_object_get_data (G_OBJECT (screen), "gtk-default-icons");
423 if (default_icons == NULL)
425 default_icons = gtk_icon_factory_new ();
426 get_default_icons (default_icons);
429 g_object_set_data_full (G_OBJECT (screen),
430 I_("gtk-default-icons"),
433 icons = default_icons;
440 * gtk_icon_factory_lookup_default:
441 * @stock_id: an icon name
443 * Looks for an icon in the list of default icon factories. For
444 * display to the user, you should use gtk_style_lookup_icon_set() on
445 * the #GtkStyle for the widget that will display the icon, instead of
446 * using this function directly, so that themes are taken into
449 * Return value: (transfer none): a #GtkIconSet, or %NULL
452 gtk_icon_factory_lookup_default (const gchar *stock_id)
455 GtkIconFactory *default_icons;
457 g_return_val_if_fail (stock_id != NULL, NULL);
459 tmp_list = default_factories;
460 while (tmp_list != NULL)
462 GtkIconSet *icon_set =
463 gtk_icon_factory_lookup (GTK_ICON_FACTORY (tmp_list->data),
469 tmp_list = g_slist_next (tmp_list);
472 default_icons = _gtk_icon_factory_get_default_icons ();
474 return gtk_icon_factory_lookup (default_icons, stock_id);
480 register_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 (TRUE, 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_NONE;
490 gtk_icon_set_add_source (set, &source);
492 gtk_icon_factory_add (factory, stock_id, set);
493 gtk_icon_set_unref (set);
497 register_bidi_stock_icon (GtkIconFactory *factory,
498 const gchar *stock_id,
499 const gchar *icon_name)
501 GtkIconSet *set = gtk_icon_set_new ();
502 GtkIconSource source = GTK_ICON_SOURCE_INIT (FALSE, TRUE, TRUE);
504 source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
505 source.source.icon_name = (gchar *)icon_name;
506 source.direction = GTK_TEXT_DIR_LTR;
507 gtk_icon_set_add_source (set, &source);
509 source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
510 source.source.icon_name = (gchar *)icon_name;
511 source.direction = GTK_TEXT_DIR_RTL;
512 gtk_icon_set_add_source (set, &source);
514 gtk_icon_factory_add (factory, stock_id, set);
515 gtk_icon_set_unref (set);
519 get_default_icons (GtkIconFactory *factory)
521 /* KEEP IN SYNC with gtkstock.c */
523 register_stock_icon (factory, GTK_STOCK_DIALOG_AUTHENTICATION, "dialog-password");
524 register_stock_icon (factory, GTK_STOCK_DIALOG_ERROR, "dialog-error");
525 register_stock_icon (factory, GTK_STOCK_DIALOG_INFO, "dialog-information");
526 register_stock_icon (factory, GTK_STOCK_DIALOG_QUESTION, "dialog-question");
527 register_stock_icon (factory, GTK_STOCK_DIALOG_WARNING, "dialog-warning");
528 register_stock_icon (factory, GTK_STOCK_DND, GTK_STOCK_DND);
529 register_stock_icon (factory, GTK_STOCK_DND_MULTIPLE, GTK_STOCK_DND_MULTIPLE);
530 register_stock_icon (factory, GTK_STOCK_APPLY, GTK_STOCK_APPLY);
531 register_stock_icon (factory, GTK_STOCK_CANCEL, GTK_STOCK_CANCEL);
532 register_stock_icon (factory, GTK_STOCK_NO, GTK_STOCK_NO);
533 register_stock_icon (factory, GTK_STOCK_OK, GTK_STOCK_OK);
534 register_stock_icon (factory, GTK_STOCK_YES, GTK_STOCK_YES);
535 register_stock_icon (factory, GTK_STOCK_CLOSE, "window-close");
536 register_stock_icon (factory, GTK_STOCK_ADD, "list-add");
537 register_stock_icon (factory, GTK_STOCK_JUSTIFY_CENTER, "format-justify-center");
538 register_stock_icon (factory, GTK_STOCK_JUSTIFY_FILL, "format-justify-fill");
539 register_stock_icon (factory, GTK_STOCK_JUSTIFY_LEFT, "format-justify-left");
540 register_stock_icon (factory, GTK_STOCK_JUSTIFY_RIGHT, "format-justify-right");
541 register_stock_icon (factory, GTK_STOCK_GOTO_BOTTOM, "go-bottom");
542 register_stock_icon (factory, GTK_STOCK_CDROM, "media-optical");
543 register_stock_icon (factory, GTK_STOCK_CONVERT, GTK_STOCK_CONVERT);
544 register_stock_icon (factory, GTK_STOCK_COPY, "edit-copy");
545 register_stock_icon (factory, GTK_STOCK_CUT, "edit-cut");
546 register_stock_icon (factory, GTK_STOCK_GO_DOWN, "go-down");
547 register_stock_icon (factory, GTK_STOCK_EXECUTE, "system-run");
548 register_stock_icon (factory, GTK_STOCK_QUIT, "application-exit");
549 register_bidi_stock_icon (factory, GTK_STOCK_GOTO_FIRST, "go-first");
550 register_stock_icon (factory, GTK_STOCK_SELECT_FONT, GTK_STOCK_SELECT_FONT);
551 register_stock_icon (factory, GTK_STOCK_FULLSCREEN, "view-fullscreen");
552 register_stock_icon (factory, GTK_STOCK_LEAVE_FULLSCREEN, "view-restore");
553 register_stock_icon (factory, GTK_STOCK_HARDDISK, "drive-harddisk");
554 register_stock_icon (factory, GTK_STOCK_HELP, "help-contents");
555 register_stock_icon (factory, GTK_STOCK_HOME, "go-home");
556 register_stock_icon (factory, GTK_STOCK_INFO, "dialog-information");
557 register_bidi_stock_icon (factory, GTK_STOCK_JUMP_TO, "go-jump");
558 register_bidi_stock_icon (factory, GTK_STOCK_GOTO_LAST, "go-last");
559 register_bidi_stock_icon (factory, GTK_STOCK_GO_BACK, "go-previous");
560 register_stock_icon (factory, GTK_STOCK_MISSING_IMAGE, "image-missing");
561 register_stock_icon (factory, GTK_STOCK_NETWORK, "network-idle");
562 register_stock_icon (factory, GTK_STOCK_NEW, "document-new");
563 register_stock_icon (factory, GTK_STOCK_OPEN, "document-open");
564 register_stock_icon (factory, GTK_STOCK_ORIENTATION_PORTRAIT, GTK_STOCK_ORIENTATION_PORTRAIT);
565 register_stock_icon (factory, GTK_STOCK_ORIENTATION_LANDSCAPE, GTK_STOCK_ORIENTATION_LANDSCAPE);
566 register_stock_icon (factory, GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT, GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT);
567 register_stock_icon (factory, GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE, GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE);
568 register_stock_icon (factory, GTK_STOCK_PAGE_SETUP, GTK_STOCK_PAGE_SETUP);
569 register_stock_icon (factory, GTK_STOCK_PASTE, "edit-paste");
570 register_stock_icon (factory, GTK_STOCK_PREFERENCES, GTK_STOCK_PREFERENCES);
571 register_stock_icon (factory, GTK_STOCK_PRINT, "document-print");
572 register_stock_icon (factory, GTK_STOCK_PRINT_ERROR, "printer-error");
573 register_stock_icon (factory, GTK_STOCK_PRINT_PAUSED, "printer-paused");
574 register_stock_icon (factory, GTK_STOCK_PRINT_PREVIEW, "document-print-preview");
575 register_stock_icon (factory, GTK_STOCK_PRINT_REPORT, "printer-info");
576 register_stock_icon (factory, GTK_STOCK_PRINT_WARNING, "printer-warning");
577 register_stock_icon (factory, GTK_STOCK_PROPERTIES, "document-properties");
578 register_bidi_stock_icon (factory, GTK_STOCK_REDO, "edit-redo");
579 register_stock_icon (factory, GTK_STOCK_REMOVE, "list-remove");
580 register_stock_icon (factory, GTK_STOCK_REFRESH, "view-refresh");
581 register_bidi_stock_icon (factory, GTK_STOCK_REVERT_TO_SAVED, "document-revert");
582 register_bidi_stock_icon (factory, GTK_STOCK_GO_FORWARD, "go-next");
583 register_stock_icon (factory, GTK_STOCK_SAVE, "document-save");
584 register_stock_icon (factory, GTK_STOCK_FLOPPY, "media-floppy");
585 register_stock_icon (factory, GTK_STOCK_SAVE_AS, "document-save-as");
586 register_stock_icon (factory, GTK_STOCK_FIND, "edit-find");
587 register_stock_icon (factory, GTK_STOCK_FIND_AND_REPLACE, "edit-find-replace");
588 register_stock_icon (factory, GTK_STOCK_SORT_DESCENDING, "view-sort-descending");
589 register_stock_icon (factory, GTK_STOCK_SORT_ASCENDING, "view-sort-ascending");
590 register_stock_icon (factory, GTK_STOCK_SPELL_CHECK, "tools-check-spelling");
591 register_stock_icon (factory, GTK_STOCK_STOP, "process-stop");
592 register_stock_icon (factory, GTK_STOCK_BOLD, "format-text-bold");
593 register_stock_icon (factory, GTK_STOCK_ITALIC, "format-text-italic");
594 register_stock_icon (factory, GTK_STOCK_STRIKETHROUGH, "format-text-strikethrough");
595 register_stock_icon (factory, GTK_STOCK_UNDERLINE, "format-text-underline");
596 register_bidi_stock_icon (factory, GTK_STOCK_INDENT, "format-indent-more");
597 register_bidi_stock_icon (factory, GTK_STOCK_UNINDENT, "format-indent-less");
598 register_stock_icon (factory, GTK_STOCK_GOTO_TOP, "go-top");
599 register_stock_icon (factory, GTK_STOCK_DELETE, "edit-delete");
600 register_bidi_stock_icon (factory, GTK_STOCK_UNDELETE, GTK_STOCK_UNDELETE);
601 register_bidi_stock_icon (factory, GTK_STOCK_UNDO, "edit-undo");
602 register_stock_icon (factory, GTK_STOCK_GO_UP, "go-up");
603 register_stock_icon (factory, GTK_STOCK_FILE, "text-x-generic");
604 register_stock_icon (factory, GTK_STOCK_DIRECTORY, "folder");
605 register_stock_icon (factory, GTK_STOCK_ABOUT, "help-about");
606 register_stock_icon (factory, GTK_STOCK_CONNECT, GTK_STOCK_CONNECT);
607 register_stock_icon (factory, GTK_STOCK_DISCONNECT, GTK_STOCK_DISCONNECT);
608 register_stock_icon (factory, GTK_STOCK_EDIT, GTK_STOCK_EDIT);
609 register_stock_icon (factory, GTK_STOCK_CAPS_LOCK_WARNING, GTK_STOCK_CAPS_LOCK_WARNING);
610 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_FORWARD, "media-seek-forward");
611 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_NEXT, "media-skip-forward");
612 register_stock_icon (factory, GTK_STOCK_MEDIA_PAUSE, "media-playback-pause");
613 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_PLAY, "media-playback-start");
614 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_PREVIOUS, "media-skip-backward");
615 register_stock_icon (factory, GTK_STOCK_MEDIA_RECORD, "media-record");
616 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_REWIND, "media-seek-backward");
617 register_stock_icon (factory, GTK_STOCK_MEDIA_STOP, "media-playback-stop");
618 register_stock_icon (factory, GTK_STOCK_INDEX, GTK_STOCK_INDEX);
619 register_stock_icon (factory, GTK_STOCK_ZOOM_100, "zoom-original");
620 register_stock_icon (factory, GTK_STOCK_ZOOM_IN, "zoom-in");
621 register_stock_icon (factory, GTK_STOCK_ZOOM_OUT, "zoom-out");
622 register_stock_icon (factory, GTK_STOCK_ZOOM_FIT, "zoom-fit-best");
623 register_stock_icon (factory, GTK_STOCK_SELECT_ALL, "edit-select-all");
624 register_stock_icon (factory, GTK_STOCK_CLEAR, "edit-clear");
625 register_stock_icon (factory, GTK_STOCK_SELECT_COLOR, GTK_STOCK_SELECT_COLOR);
626 register_stock_icon (factory, GTK_STOCK_COLOR_PICKER, GTK_STOCK_COLOR_PICKER);
629 /************************************************************
630 * Icon size handling *
631 ************************************************************/
633 typedef struct _IconSize IconSize;
644 typedef struct _IconAlias IconAlias;
652 typedef struct _SettingsIconSize SettingsIconSize;
654 struct _SettingsIconSize
660 static GHashTable *icon_aliases = NULL;
661 static IconSize *icon_sizes = NULL;
662 static gint icon_sizes_allocated = 0;
663 static gint icon_sizes_used = 0;
666 init_icon_sizes (void)
668 if (icon_sizes == NULL)
670 #define NUM_BUILTIN_SIZES 7
673 icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
675 icon_sizes = g_new (IconSize, NUM_BUILTIN_SIZES);
676 icon_sizes_allocated = NUM_BUILTIN_SIZES;
677 icon_sizes_used = NUM_BUILTIN_SIZES;
679 icon_sizes[GTK_ICON_SIZE_INVALID].size = 0;
680 icon_sizes[GTK_ICON_SIZE_INVALID].name = NULL;
681 icon_sizes[GTK_ICON_SIZE_INVALID].width = 0;
682 icon_sizes[GTK_ICON_SIZE_INVALID].height = 0;
684 /* the name strings aren't copied since we don't ever remove
685 * icon sizes, so we don't need to know whether they're static.
686 * Even if we did I suppose removing the builtin sizes would be
690 icon_sizes[GTK_ICON_SIZE_MENU].size = GTK_ICON_SIZE_MENU;
691 icon_sizes[GTK_ICON_SIZE_MENU].name = "gtk-menu";
692 icon_sizes[GTK_ICON_SIZE_MENU].width = 16;
693 icon_sizes[GTK_ICON_SIZE_MENU].height = 16;
695 icon_sizes[GTK_ICON_SIZE_BUTTON].size = GTK_ICON_SIZE_BUTTON;
696 icon_sizes[GTK_ICON_SIZE_BUTTON].name = "gtk-button";
697 icon_sizes[GTK_ICON_SIZE_BUTTON].width = 20;
698 icon_sizes[GTK_ICON_SIZE_BUTTON].height = 20;
700 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].size = GTK_ICON_SIZE_SMALL_TOOLBAR;
701 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].name = "gtk-small-toolbar";
702 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].width = 18;
703 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].height = 18;
705 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].size = GTK_ICON_SIZE_LARGE_TOOLBAR;
706 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].name = "gtk-large-toolbar";
707 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].width = 24;
708 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].height = 24;
710 icon_sizes[GTK_ICON_SIZE_DND].size = GTK_ICON_SIZE_DND;
711 icon_sizes[GTK_ICON_SIZE_DND].name = "gtk-dnd";
712 icon_sizes[GTK_ICON_SIZE_DND].width = 32;
713 icon_sizes[GTK_ICON_SIZE_DND].height = 32;
715 icon_sizes[GTK_ICON_SIZE_DIALOG].size = GTK_ICON_SIZE_DIALOG;
716 icon_sizes[GTK_ICON_SIZE_DIALOG].name = "gtk-dialog";
717 icon_sizes[GTK_ICON_SIZE_DIALOG].width = 48;
718 icon_sizes[GTK_ICON_SIZE_DIALOG].height = 48;
720 g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
722 /* Alias everything to itself. */
723 i = 1; /* skip invalid size */
724 while (i < NUM_BUILTIN_SIZES)
726 gtk_icon_size_register_alias (icon_sizes[i].name, icon_sizes[i].size);
731 #undef NUM_BUILTIN_SIZES
736 free_settings_sizes (gpointer data)
738 g_array_free (data, TRUE);
742 get_settings_sizes (GtkSettings *settings,
745 GArray *settings_sizes;
746 static GQuark sizes_quark = 0;
749 sizes_quark = g_quark_from_static_string ("gtk-icon-sizes");
751 settings_sizes = g_object_get_qdata (G_OBJECT (settings), sizes_quark);
754 settings_sizes = g_array_new (FALSE, FALSE, sizeof (SettingsIconSize));
755 g_object_set_qdata_full (G_OBJECT (settings), sizes_quark,
756 settings_sizes, free_settings_sizes);
761 return settings_sizes;
765 icon_size_set_for_settings (GtkSettings *settings,
766 const gchar *size_name,
771 GArray *settings_sizes;
772 SettingsIconSize *settings_size;
774 g_return_if_fail (size_name != NULL);
776 size = gtk_icon_size_from_name (size_name);
777 if (size == GTK_ICON_SIZE_INVALID)
778 /* Reserve a place */
779 size = icon_size_register_intern (size_name, -1, -1);
781 settings_sizes = get_settings_sizes (settings, NULL);
782 if (size >= settings_sizes->len)
784 SettingsIconSize unset = { -1, -1 };
787 for (i = settings_sizes->len; i <= size; i++)
788 g_array_append_val (settings_sizes, unset);
791 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
793 settings_size->width = width;
794 settings_size->height = height;
797 /* Like pango_parse_word, but accept - as well
800 scan_icon_size_name (const char **pos, GString *out)
802 const char *p = *pos;
804 while (g_ascii_isspace (*p))
807 if (!((*p >= 'A' && *p <= 'Z') ||
808 (*p >= 'a' && *p <= 'z') ||
809 *p == '_' || *p == '-'))
812 g_string_truncate (out, 0);
813 g_string_append_c (out, *p);
816 while ((*p >= 'A' && *p <= 'Z') ||
817 (*p >= 'a' && *p <= 'z') ||
818 (*p >= '0' && *p <= '9') ||
819 *p == '_' || *p == '-')
821 g_string_append_c (out, *p);
831 icon_size_setting_parse (GtkSettings *settings,
832 const gchar *icon_size_string)
834 GString *name_buf = g_string_new (NULL);
835 const gchar *p = icon_size_string;
837 while (pango_skip_space (&p))
841 if (!scan_icon_size_name (&p, name_buf))
844 if (!pango_skip_space (&p))
852 if (!pango_scan_int (&p, &width))
855 if (!pango_skip_space (&p))
863 if (!pango_scan_int (&p, &height))
866 if (width > 0 && height > 0)
868 icon_size_set_for_settings (settings, name_buf->str,
873 g_warning ("Invalid size in gtk-icon-sizes: %d,%d\n", width, height);
876 pango_skip_space (&p);
885 g_string_free (name_buf, TRUE);
889 g_warning ("Error parsing gtk-icon-sizes string:\n\t'%s'", icon_size_string);
890 g_string_free (name_buf, TRUE);
894 icon_size_set_all_from_settings (GtkSettings *settings)
896 GArray *settings_sizes;
897 gchar *icon_size_string;
899 /* Reset old settings */
900 settings_sizes = get_settings_sizes (settings, NULL);
901 g_array_set_size (settings_sizes, 0);
903 g_object_get (settings,
904 "gtk-icon-sizes", &icon_size_string,
907 if (icon_size_string)
909 icon_size_setting_parse (settings, icon_size_string);
910 g_free (icon_size_string);
915 icon_size_settings_changed (GtkSettings *settings,
918 icon_size_set_all_from_settings (settings);
920 gtk_style_context_reset_widgets (_gtk_settings_get_screen (settings));
924 icon_sizes_init_for_settings (GtkSettings *settings)
926 g_signal_connect (settings,
927 "notify::gtk-icon-sizes",
928 G_CALLBACK (icon_size_settings_changed),
931 icon_size_set_all_from_settings (settings);
935 icon_size_lookup_intern (GtkSettings *settings,
940 GArray *settings_sizes;
941 gint width_for_settings = -1;
942 gint height_for_settings = -1;
946 if (size == (GtkIconSize)-1)
949 if (size >= icon_sizes_used)
952 if (size == GTK_ICON_SIZE_INVALID)
957 gboolean initial = FALSE;
959 settings_sizes = get_settings_sizes (settings, &initial);
962 icon_sizes_init_for_settings (settings);
964 if (size < settings_sizes->len)
966 SettingsIconSize *settings_size;
968 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
970 width_for_settings = settings_size->width;
971 height_for_settings = settings_size->height;
976 *widthp = width_for_settings >= 0 ? width_for_settings : icon_sizes[size].width;
979 *heightp = height_for_settings >= 0 ? height_for_settings : icon_sizes[size].height;
985 * gtk_icon_size_lookup_for_settings:
986 * @settings: a #GtkSettings object, used to determine
987 * which set of user preferences to used.
988 * @size: (type int): an icon size
989 * @width: (out): location to store icon width
990 * @height: (out): location to store icon height
992 * Obtains the pixel size of a semantic icon size, possibly
993 * modified by user preferences for a particular
994 * #GtkSettings. Normally @size would be
995 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
996 * isn't normally needed, gtk_widget_render_icon_pixbuf() is the usual
997 * way to get an icon for rendering, then just look at the size of
998 * the rendered pixbuf. The rendered pixbuf may not even correspond to
999 * the width/height returned by gtk_icon_size_lookup(), because themes
1000 * are free to render the pixbuf however they like, including changing
1003 * Return value: %TRUE if @size was a valid size
1008 gtk_icon_size_lookup_for_settings (GtkSettings *settings,
1013 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1015 return icon_size_lookup_intern (settings, size, width, height);
1019 * gtk_icon_size_lookup:
1020 * @size: (type int): an icon size
1021 * @width: (out): location to store icon width
1022 * @height: (out): location to store icon height
1024 * Obtains the pixel size of a semantic icon size, possibly
1025 * modified by user preferences for the default #GtkSettings.
1026 * (See gtk_icon_size_lookup_for_settings().)
1027 * Normally @size would be
1028 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
1029 * isn't normally needed, gtk_widget_render_icon_pixbuf() is the usual
1030 * way to get an icon for rendering, then just look at the size of
1031 * the rendered pixbuf. The rendered pixbuf may not even correspond to
1032 * the width/height returned by gtk_icon_size_lookup(), because themes
1033 * are free to render the pixbuf however they like, including changing
1036 * Return value: %TRUE if @size was a valid size
1039 gtk_icon_size_lookup (GtkIconSize size,
1043 GTK_NOTE (MULTIHEAD,
1044 g_warning ("gtk_icon_size_lookup ()) is not multihead safe"));
1046 return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (),
1047 size, widthp, heightp);
1051 icon_size_register_intern (const gchar *name,
1055 IconAlias *old_alias;
1060 old_alias = g_hash_table_lookup (icon_aliases, name);
1061 if (old_alias && icon_sizes[old_alias->target].width > 0)
1063 g_warning ("Icon size name '%s' already exists", name);
1064 return GTK_ICON_SIZE_INVALID;
1069 size = old_alias->target;
1073 if (icon_sizes_used == icon_sizes_allocated)
1075 icon_sizes_allocated *= 2;
1076 icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated);
1079 size = icon_sizes_used++;
1081 /* alias to self. */
1082 gtk_icon_size_register_alias (name, size);
1084 icon_sizes[size].size = size;
1085 icon_sizes[size].name = g_strdup (name);
1088 icon_sizes[size].width = width;
1089 icon_sizes[size].height = height;
1095 * gtk_icon_size_register:
1096 * @name: name of the icon size
1097 * @width: the icon width
1098 * @height: the icon height
1100 * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
1101 * etc. Returns the integer value for the size.
1103 * Returns: (type int): integer value representing the size
1106 gtk_icon_size_register (const gchar *name,
1110 g_return_val_if_fail (name != NULL, 0);
1111 g_return_val_if_fail (width > 0, 0);
1112 g_return_val_if_fail (height > 0, 0);
1114 return icon_size_register_intern (name, width, height);
1118 * gtk_icon_size_register_alias:
1119 * @alias: an alias for @target
1120 * @target: (type int): an existing icon size
1122 * Registers @alias as another name for @target.
1123 * So calling gtk_icon_size_from_name() with @alias as argument
1124 * will return @target.
1127 gtk_icon_size_register_alias (const gchar *alias,
1132 g_return_if_fail (alias != NULL);
1136 if (!icon_size_lookup_intern (NULL, target, NULL, NULL))
1137 g_warning ("gtk_icon_size_register_alias: Icon size %u does not exist", target);
1139 ia = g_hash_table_lookup (icon_aliases, alias);
1142 if (icon_sizes[ia->target].width > 0)
1144 g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias);
1148 ia->target = target;
1153 ia = g_new (IconAlias, 1);
1154 ia->name = g_strdup (alias);
1155 ia->target = target;
1157 g_hash_table_insert (icon_aliases, ia->name, ia);
1162 * gtk_icon_size_from_name:
1163 * @name: the name to look up.
1165 * Looks up the icon size associated with @name.
1167 * Return value: (type int): the icon size
1170 gtk_icon_size_from_name (const gchar *name)
1176 ia = g_hash_table_lookup (icon_aliases, name);
1178 if (ia && icon_sizes[ia->target].width > 0)
1181 return GTK_ICON_SIZE_INVALID;
1185 * gtk_icon_size_get_name:
1186 * @size: (type int): a #GtkIconSize.
1188 * Gets the canonical name of the given icon size. The returned string
1189 * is statically allocated and should not be freed.
1191 * Returns: the name of the given icon size.
1194 gtk_icon_size_get_name (GtkIconSize size)
1196 if (size >= icon_sizes_used)
1199 return icon_sizes[size].name;
1202 /************************************************************/
1207 static GdkPixbuf *find_in_cache (GtkIconSet *icon_set,
1208 GtkStyleContext *style_context,
1209 GtkTextDirection direction,
1212 static void add_to_cache (GtkIconSet *icon_set,
1213 GtkStyleContext *style_context,
1214 GtkTextDirection direction,
1218 /* Clear icon set contents, drop references to all contained
1219 * GdkPixbuf objects and forget all GtkIconSources. Used to
1220 * recycle an icon set.
1222 static void clear_cache (GtkIconSet *icon_set,
1223 gboolean style_detach);
1224 static GSList* copy_cache (GtkIconSet *icon_set,
1225 GtkIconSet *copy_recipient);
1226 static void attach_to_style (GtkIconSet *icon_set,
1227 GtkStyleContext *style_context);
1228 static void detach_from_style (GtkIconSet *icon_set,
1229 GtkStyleContext *style_context);
1230 static void style_dnotify (gpointer data);
1238 /* Cache of the last few rendered versions of the icon. */
1246 static guint cache_serial = 0;
1251 * Creates a new #GtkIconSet. A #GtkIconSet represents a single icon
1252 * in various sizes and widget states. It can provide a #GdkPixbuf
1253 * for a given size and state on request, and automatically caches
1254 * some of the rendered #GdkPixbuf objects.
1256 * Normally you would use gtk_widget_render_icon_pixbuf() instead of
1257 * using #GtkIconSet directly. The one case where you'd use
1258 * #GtkIconSet is to create application-specific icon sets to place in
1259 * a #GtkIconFactory.
1261 * Return value: a new #GtkIconSet
1264 gtk_icon_set_new (void)
1266 GtkIconSet *icon_set;
1268 icon_set = g_new (GtkIconSet, 1);
1270 icon_set->ref_count = 1;
1271 icon_set->sources = NULL;
1272 icon_set->cache = NULL;
1273 icon_set->cache_size = 0;
1274 icon_set->cache_serial = cache_serial;
1280 * gtk_icon_set_new_from_pixbuf:
1281 * @pixbuf: a #GdkPixbuf
1283 * Creates a new #GtkIconSet with @pixbuf as the default/fallback
1284 * source image. If you don't add any additional #GtkIconSource to the
1285 * icon set, all variants of the icon will be created from @pixbuf,
1286 * using scaling, pixelation, etc. as required to adjust the icon size
1287 * or make the icon look insensitive/prelighted.
1289 * Return value: a new #GtkIconSet
1292 gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
1296 GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1298 g_return_val_if_fail (pixbuf != NULL, NULL);
1300 set = gtk_icon_set_new ();
1302 gtk_icon_source_set_pixbuf (&source, pixbuf);
1303 gtk_icon_set_add_source (set, &source);
1304 gtk_icon_source_set_pixbuf (&source, NULL);
1312 * @icon_set: a #GtkIconSet.
1314 * Increments the reference count on @icon_set.
1316 * Return value: @icon_set.
1319 gtk_icon_set_ref (GtkIconSet *icon_set)
1321 g_return_val_if_fail (icon_set != NULL, NULL);
1322 g_return_val_if_fail (icon_set->ref_count > 0, NULL);
1324 icon_set->ref_count += 1;
1330 * gtk_icon_set_unref:
1331 * @icon_set: a #GtkIconSet
1333 * Decrements the reference count on @icon_set, and frees memory
1334 * if the reference count reaches 0.
1337 gtk_icon_set_unref (GtkIconSet *icon_set)
1339 g_return_if_fail (icon_set != NULL);
1340 g_return_if_fail (icon_set->ref_count > 0);
1342 icon_set->ref_count -= 1;
1344 if (icon_set->ref_count == 0)
1346 GSList *tmp_list = icon_set->sources;
1347 while (tmp_list != NULL)
1349 gtk_icon_source_free (tmp_list->data);
1351 tmp_list = g_slist_next (tmp_list);
1353 g_slist_free (icon_set->sources);
1355 clear_cache (icon_set, TRUE);
1361 G_DEFINE_BOXED_TYPE (GtkIconSet, gtk_icon_set,
1366 * gtk_icon_set_copy:
1367 * @icon_set: a #GtkIconSet
1369 * Copies @icon_set by value.
1371 * Return value: a new #GtkIconSet identical to the first.
1374 gtk_icon_set_copy (GtkIconSet *icon_set)
1379 copy = gtk_icon_set_new ();
1381 tmp_list = icon_set->sources;
1382 while (tmp_list != NULL)
1384 copy->sources = g_slist_prepend (copy->sources,
1385 gtk_icon_source_copy (tmp_list->data));
1387 tmp_list = g_slist_next (tmp_list);
1390 copy->sources = g_slist_reverse (copy->sources);
1392 copy->cache = copy_cache (icon_set, copy);
1393 copy->cache_size = icon_set->cache_size;
1394 copy->cache_serial = icon_set->cache_serial;
1400 sizes_equivalent (GtkIconSize lhs,
1403 /* We used to consider sizes equivalent if they were
1404 * the same pixel size, but we don't have the GtkSettings
1405 * here, so we can't do that. Plus, it's not clear that
1406 * it is right... it was just a workaround for the fact
1407 * that we register icons by logical size, not pixel size.
1413 gint r_w, r_h, l_w, l_h;
1415 icon_size_lookup_intern (NULL, rhs, &r_w, &r_h);
1416 icon_size_lookup_intern (NULL, lhs, &l_w, &l_h);
1418 return r_w == l_w && r_h == l_h;
1422 static GtkIconSource *
1423 find_best_matching_source (GtkIconSet *icon_set,
1424 GtkTextDirection direction,
1429 GtkIconSource *source;
1432 /* We need to find the best icon source. Direction matters more
1433 * than state, state matters more than size. icon_set->sources
1434 * is sorted according to wildness, so if we take the first
1435 * match we find it will be the least-wild match (if there are
1436 * multiple matches for a given "wildness" then the RC file contained
1437 * dumb stuff, and we end up with an arbitrary matching source)
1441 tmp_list = icon_set->sources;
1442 while (tmp_list != NULL)
1444 GtkIconSource *s = tmp_list->data;
1446 if ((s->any_direction || (s->direction == direction)) &&
1447 (s->any_state || (s->state == state)) &&
1448 (s->any_size || size == (GtkIconSize)-1 || (sizes_equivalent (size, s->size))))
1450 if (!g_slist_find (failed, s))
1457 tmp_list = g_slist_next (tmp_list);
1464 ensure_filename_pixbuf (GtkIconSet *icon_set,
1465 GtkIconSource *source)
1467 if (source->filename_pixbuf == NULL)
1469 GError *error = NULL;
1471 source->filename_pixbuf = gdk_pixbuf_new_from_file (source->source.filename, &error);
1473 if (source->filename_pixbuf == NULL)
1475 /* Remove this icon source so we don't keep trying to
1478 g_warning ("Error loading icon: %s", error->message);
1479 g_error_free (error);
1481 icon_set->sources = g_slist_remove (icon_set->sources, source);
1483 gtk_icon_source_free (source);
1493 render_icon_name_pixbuf (GtkIconSource *icon_source,
1494 GtkStyleContext *context,
1498 GdkPixbuf *tmp_pixbuf;
1499 GtkIconSource tmp_source;
1501 GtkIconTheme *icon_theme;
1502 GtkSettings *settings;
1503 gint width, height, pixel_size;
1504 gint *sizes, *s, dist;
1505 GError *error = NULL;
1507 screen = gtk_style_context_get_screen (context);
1508 icon_theme = gtk_icon_theme_get_for_screen (screen);
1509 settings = gtk_settings_get_for_screen (screen);
1511 if (!gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
1513 if (size == (GtkIconSize)-1)
1515 /* Find an available size close to 48 */
1516 sizes = gtk_icon_theme_get_icon_sizes (icon_theme, icon_source->source.icon_name);
1518 width = height = 48;
1519 for (s = sizes; *s; s++)
1523 width = height = 48;
1530 width = height = *s;
1538 width = height = *s;
1548 g_warning ("Invalid icon size %u\n", size);
1549 width = height = 24;
1553 pixel_size = MIN (width, height);
1555 if (icon_source->direction != GTK_TEXT_DIR_NONE)
1557 gchar *suffix[3] = { NULL, "-ltr", "-rtl" };
1561 names[0] = g_strconcat (icon_source->source.icon_name, suffix[icon_source->direction], NULL);
1562 names[1] = icon_source->source.icon_name;
1565 info = gtk_icon_theme_choose_icon (icon_theme,
1566 (const char **) names,
1567 pixel_size, GTK_ICON_LOOKUP_USE_BUILTIN);
1571 tmp_pixbuf = gtk_icon_info_load_icon (info, &error);
1572 gtk_icon_info_free (info);
1579 tmp_pixbuf = gtk_icon_theme_load_icon (icon_theme,
1580 icon_source->source.icon_name,
1587 g_warning ("Error loading theme icon '%s' for stock: %s",
1588 icon_source->source.icon_name, error ? error->message : "");
1590 g_error_free (error);
1594 tmp_source = *icon_source;
1595 tmp_source.type = GTK_ICON_SOURCE_PIXBUF;
1596 tmp_source.source.pixbuf = tmp_pixbuf;
1598 pixbuf = gtk_render_icon_pixbuf (context, &tmp_source, -1);
1601 g_warning ("Failed to render icon");
1603 g_object_unref (tmp_pixbuf);
1609 find_and_render_icon_source (GtkIconSet *icon_set,
1610 GtkStyleContext *context,
1611 GtkTextDirection direction,
1615 GSList *failed = NULL;
1616 GdkPixbuf *pixbuf = NULL;
1618 /* We treat failure in two different ways:
1620 * A) If loading a source that specifies a filename fails,
1621 * we treat that as permanent, and remove the source
1622 * from the GtkIconSet. (in ensure_filename_pixbuf ()
1623 * B) If loading a themed icon fails, or scaling an icon
1624 * fails, we treat that as transient and will try
1625 * again next time the icon falls out of the cache
1626 * and we need to recreate it.
1628 while (pixbuf == NULL)
1630 GtkIconSource *source = find_best_matching_source (icon_set, direction, state, size, failed);
1635 switch (source->type)
1637 case GTK_ICON_SOURCE_FILENAME:
1638 if (!ensure_filename_pixbuf (icon_set, source))
1641 case GTK_ICON_SOURCE_PIXBUF:
1642 pixbuf = gtk_render_icon_pixbuf (context, source, size);
1645 g_warning ("Failed to render icon");
1646 failed = g_slist_prepend (failed, source);
1649 case GTK_ICON_SOURCE_ICON_NAME:
1650 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1651 pixbuf = render_icon_name_pixbuf (source, context, size);
1653 failed = g_slist_prepend (failed, source);
1655 case GTK_ICON_SOURCE_EMPTY:
1656 g_assert_not_reached ();
1660 g_slist_free (failed);
1665 extern GtkIconCache *_builtin_cache;
1668 render_fallback_image (GtkStyleContext *context,
1669 GtkTextDirection direction,
1673 /* This icon can be used for any direction/state/size */
1674 static GtkIconSource fallback_source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1676 if (fallback_source.type == GTK_ICON_SOURCE_EMPTY)
1681 _gtk_icon_theme_ensure_builtin_cache ();
1683 index = _gtk_icon_cache_get_directory_index (_builtin_cache, "24");
1684 pixbuf = _gtk_icon_cache_get_icon (_builtin_cache, "image-missing", index);
1686 g_return_val_if_fail(pixbuf != NULL, NULL);
1688 gtk_icon_source_set_pixbuf (&fallback_source, pixbuf);
1689 g_object_unref (pixbuf);
1692 return gtk_render_icon_pixbuf (context, &fallback_source, size);
1696 * gtk_icon_set_render_icon_pixbuf:
1697 * @icon_set: a #GtkIconSet
1698 * @context: a #GtkStyleContext
1699 * @size: (type int): icon size. A size of (GtkIconSize)-1
1700 * means render at the size of the source and don't scale.
1702 * Renders an icon using gtk_render_icon_pixbuf(). In most cases,
1703 * gtk_widget_render_icon_pixbuf() is better, since it automatically provides
1704 * most of the arguments from the current widget settings. This
1705 * function never returns %NULL; if the icon can't be rendered
1706 * (perhaps because an image file fails to load), a default "missing
1707 * image" icon will be returned instead.
1709 * Return value: (transfer full): a #GdkPixbuf to be displayed
1714 gtk_icon_set_render_icon_pixbuf (GtkIconSet *icon_set,
1715 GtkStyleContext *context,
1718 GdkPixbuf *icon = NULL;
1719 GtkStateFlags flags = 0;
1721 GtkTextDirection direction;
1723 g_return_val_if_fail (icon_set != NULL, NULL);
1724 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1726 flags = gtk_style_context_get_state (context);
1727 if (flags & GTK_STATE_FLAG_INSENSITIVE)
1728 state = GTK_STATE_INSENSITIVE;
1729 else if (flags & GTK_STATE_FLAG_PRELIGHT)
1730 state = GTK_STATE_PRELIGHT;
1732 state = GTK_STATE_NORMAL;
1734 direction = gtk_style_context_get_direction (context);
1736 if (icon_set->sources)
1738 icon = find_in_cache (icon_set, context, direction, state, size);
1741 g_object_ref (icon);
1746 if (icon_set->sources)
1747 icon = find_and_render_icon_source (icon_set, context, direction, state, size);
1750 icon = render_fallback_image (context, direction, state, size);
1752 add_to_cache (icon_set, context, direction, state, size, icon);
1758 * gtk_icon_set_render_icon:
1759 * @icon_set: a #GtkIconSet
1760 * @style: (allow-none): a #GtkStyle associated with @widget, or %NULL
1761 * @direction: text direction
1762 * @state: widget state
1763 * @size: (type int): icon size. A size of (GtkIconSize)-1
1764 * means render at the size of the source and don't scale.
1765 * @widget: (allow-none): widget that will display the icon, or %NULL.
1766 * The only use that is typically made of this
1767 * is to determine the appropriate #GdkScreen.
1768 * @detail: (allow-none): detail to pass to the theme engine, or %NULL.
1769 * Note that passing a detail of anything but %NULL
1770 * will disable caching.
1772 * Renders an icon using gtk_style_render_icon(). In most cases,
1773 * gtk_widget_render_icon() is better, since it automatically provides
1774 * most of the arguments from the current widget settings. This
1775 * function never returns %NULL; if the icon can't be rendered
1776 * (perhaps because an image file fails to load), a default "missing
1777 * image" icon will be returned instead.
1779 * Return value: (transfer full): a #GdkPixbuf to be displayed
1781 * Deprecated: 3.0: Use gtk_icon_set_render_icon_pixbuf() instead
1784 gtk_icon_set_render_icon (GtkIconSet *icon_set,
1786 GtkTextDirection direction,
1793 GtkStyleContext *context = NULL;
1794 GtkStateFlags flags = 0;
1796 g_return_val_if_fail (icon_set != NULL, NULL);
1797 g_return_val_if_fail (style == NULL || GTK_IS_STYLE (style), NULL);
1799 if (style && gtk_style_has_context (style))
1801 g_object_get (style, "context", &context, NULL);
1802 /* g_object_get returns a refed object */
1804 g_object_unref (context);
1808 context = gtk_widget_get_style_context (widget);
1812 return render_fallback_image (context, direction, state, size);
1814 gtk_style_context_save (context);
1818 case GTK_STATE_PRELIGHT:
1819 flags |= GTK_STATE_FLAG_PRELIGHT;
1821 case GTK_STATE_INSENSITIVE:
1822 flags |= GTK_STATE_FLAG_INSENSITIVE;
1828 gtk_style_context_set_state (context, flags);
1829 gtk_style_context_set_direction (context, direction);
1831 icon = gtk_icon_set_render_icon_pixbuf (icon_set, context, size);
1833 gtk_style_context_restore (context);
1838 /* Order sources by their "wildness", so that "wilder" sources are
1839 * greater than "specific" sources; for determining ordering,
1840 * direction beats state beats size.
1844 icon_source_compare (gconstpointer ap, gconstpointer bp)
1846 const GtkIconSource *a = ap;
1847 const GtkIconSource *b = bp;
1849 if (!a->any_direction && b->any_direction)
1851 else if (a->any_direction && !b->any_direction)
1853 else if (!a->any_state && b->any_state)
1855 else if (a->any_state && !b->any_state)
1857 else if (!a->any_size && b->any_size)
1859 else if (a->any_size && !b->any_size)
1866 * gtk_icon_set_add_source:
1867 * @icon_set: a #GtkIconSet
1868 * @source: a #GtkIconSource
1870 * Icon sets have a list of #GtkIconSource, which they use as base
1871 * icons for rendering icons in different states and sizes. Icons are
1872 * scaled, made to look insensitive, etc. in
1873 * gtk_icon_set_render_icon(), but #GtkIconSet needs base images to
1874 * work with. The base images and when to use them are described by
1877 * This function copies @source, so you can reuse the same source immediately
1878 * without affecting the icon set.
1880 * An example of when you'd use this function: a web browser's "Back
1881 * to Previous Page" icon might point in a different direction in
1882 * Hebrew and in English; it might look different when insensitive;
1883 * and it might change size depending on toolbar mode (small/large
1884 * icons). So a single icon set would contain all those variants of
1885 * the icon, and you might add a separate source for each one.
1887 * You should nearly always add a "default" icon source with all
1888 * fields wildcarded, which will be used as a fallback if no more
1889 * specific source matches. #GtkIconSet always prefers more specific
1890 * icon sources to more generic icon sources. The order in which you
1891 * add the sources to the icon set does not matter.
1893 * gtk_icon_set_new_from_pixbuf() creates a new icon set with a
1894 * default icon source based on the given pixbuf.
1897 gtk_icon_set_add_source (GtkIconSet *icon_set,
1898 const GtkIconSource *source)
1900 g_return_if_fail (icon_set != NULL);
1901 g_return_if_fail (source != NULL);
1903 if (source->type == GTK_ICON_SOURCE_EMPTY)
1905 g_warning ("Useless empty GtkIconSource");
1909 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
1910 gtk_icon_source_copy (source),
1911 icon_source_compare);
1915 * gtk_icon_set_get_sizes:
1916 * @icon_set: a #GtkIconSet
1917 * @sizes: (array length=n_sizes) (out) (type int): return location
1918 * for array of sizes
1919 * @n_sizes: location to store number of elements in returned array
1921 * Obtains a list of icon sizes this icon set can render. The returned
1922 * array must be freed with g_free().
1925 gtk_icon_set_get_sizes (GtkIconSet *icon_set,
1926 GtkIconSize **sizes,
1930 gboolean all_sizes = FALSE;
1931 GSList *specifics = NULL;
1933 g_return_if_fail (icon_set != NULL);
1934 g_return_if_fail (sizes != NULL);
1935 g_return_if_fail (n_sizes != NULL);
1937 tmp_list = icon_set->sources;
1938 while (tmp_list != NULL)
1940 GtkIconSource *source;
1942 source = tmp_list->data;
1944 if (source->any_size)
1950 specifics = g_slist_prepend (specifics, GINT_TO_POINTER (source->size));
1952 tmp_list = g_slist_next (tmp_list);
1957 /* Need to find out what sizes exist */
1962 *sizes = g_new (GtkIconSize, icon_sizes_used);
1963 *n_sizes = icon_sizes_used - 1;
1966 while (i < icon_sizes_used)
1968 (*sizes)[i - 1] = icon_sizes[i].size;
1976 *n_sizes = g_slist_length (specifics);
1977 *sizes = g_new (GtkIconSize, *n_sizes);
1980 tmp_list = specifics;
1981 while (tmp_list != NULL)
1983 (*sizes)[i] = GPOINTER_TO_INT (tmp_list->data);
1986 tmp_list = g_slist_next (tmp_list);
1990 g_slist_free (specifics);
1995 * gtk_icon_source_new:
1997 * Creates a new #GtkIconSource. A #GtkIconSource contains a #GdkPixbuf (or
1998 * image filename) that serves as the base image for one or more of the
1999 * icons in a #GtkIconSet, along with a specification for which icons in the
2000 * icon set will be based on that pixbuf or image file. An icon set contains
2001 * a set of icons that represent "the same" logical concept in different states,
2002 * different global text directions, and different sizes.
2004 * So for example a web browser's "Back to Previous Page" icon might
2005 * point in a different direction in Hebrew and in English; it might
2006 * look different when insensitive; and it might change size depending
2007 * on toolbar mode (small/large icons). So a single icon set would
2008 * contain all those variants of the icon. #GtkIconSet contains a list
2009 * of #GtkIconSource from which it can derive specific icon variants in
2012 * In the simplest case, #GtkIconSet contains one source pixbuf from
2013 * which it derives all variants. The convenience function
2014 * gtk_icon_set_new_from_pixbuf() handles this case; if you only have
2015 * one source pixbuf, just use that function.
2017 * If you want to use a different base pixbuf for different icon
2018 * variants, you create multiple icon sources, mark which variants
2019 * they'll be used to create, and add them to the icon set with
2020 * gtk_icon_set_add_source().
2022 * By default, the icon source has all parameters wildcarded. That is,
2023 * the icon source will be used as the base icon for any desired text
2024 * direction, widget state, or icon size.
2026 * Return value: a new #GtkIconSource
2029 gtk_icon_source_new (void)
2033 src = g_new0 (GtkIconSource, 1);
2035 src->direction = GTK_TEXT_DIR_NONE;
2036 src->size = GTK_ICON_SIZE_INVALID;
2037 src->state = GTK_STATE_NORMAL;
2039 src->any_direction = TRUE;
2040 src->any_state = TRUE;
2041 src->any_size = TRUE;
2047 * gtk_icon_source_copy:
2048 * @source: a #GtkIconSource
2050 * Creates a copy of @source; mostly useful for language bindings.
2052 * Return value: a new #GtkIconSource
2055 gtk_icon_source_copy (const GtkIconSource *source)
2057 GtkIconSource *copy;
2059 g_return_val_if_fail (source != NULL, NULL);
2061 copy = g_new (GtkIconSource, 1);
2067 case GTK_ICON_SOURCE_EMPTY:
2068 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
2070 case GTK_ICON_SOURCE_ICON_NAME:
2071 copy->source.icon_name = g_strdup (copy->source.icon_name);
2073 case GTK_ICON_SOURCE_FILENAME:
2074 copy->source.filename = g_strdup (copy->source.filename);
2075 if (copy->filename_pixbuf)
2076 g_object_ref (copy->filename_pixbuf);
2078 case GTK_ICON_SOURCE_PIXBUF:
2079 g_object_ref (copy->source.pixbuf);
2082 g_assert_not_reached();
2089 * gtk_icon_source_free:
2090 * @source: a #GtkIconSource
2092 * Frees a dynamically-allocated icon source, along with its
2093 * filename, size, and pixbuf fields if those are not %NULL.
2096 gtk_icon_source_free (GtkIconSource *source)
2098 g_return_if_fail (source != NULL);
2100 icon_source_clear (source);
2104 G_DEFINE_BOXED_TYPE (GtkIconSource, gtk_icon_source,
2105 gtk_icon_source_copy,
2106 gtk_icon_source_free)
2109 icon_source_clear (GtkIconSource *source)
2111 switch (source->type)
2113 case GTK_ICON_SOURCE_EMPTY:
2115 case GTK_ICON_SOURCE_ICON_NAME:
2116 g_free (source->source.icon_name);
2118 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
2119 source->source.icon_name = NULL;
2121 case GTK_ICON_SOURCE_FILENAME:
2122 g_free (source->source.filename);
2123 source->source.filename = NULL;
2124 if (source->filename_pixbuf)
2125 g_object_unref (source->filename_pixbuf);
2126 source->filename_pixbuf = NULL;
2128 case GTK_ICON_SOURCE_PIXBUF:
2129 g_object_unref (source->source.pixbuf);
2130 source->source.pixbuf = NULL;
2133 g_assert_not_reached();
2136 source->type = GTK_ICON_SOURCE_EMPTY;
2140 * gtk_icon_source_set_filename:
2141 * @source: a #GtkIconSource
2142 * @filename: (type filename): image file to use
2144 * Sets the name of an image file to use as a base image when creating
2145 * icon variants for #GtkIconSet. The filename must be absolute.
2148 gtk_icon_source_set_filename (GtkIconSource *source,
2149 const gchar *filename)
2151 g_return_if_fail (source != NULL);
2152 g_return_if_fail (filename == NULL || g_path_is_absolute (filename));
2154 if (source->type == GTK_ICON_SOURCE_FILENAME &&
2155 source->source.filename == filename)
2158 icon_source_clear (source);
2160 if (filename != NULL)
2162 source->type = GTK_ICON_SOURCE_FILENAME;
2163 source->source.filename = g_strdup (filename);
2168 * gtk_icon_source_set_icon_name:
2169 * @source: a #GtkIconSource
2170 * @icon_name: (allow-none): name of icon to use
2172 * Sets the name of an icon to look up in the current icon theme
2173 * to use as a base image when creating icon variants for #GtkIconSet.
2176 gtk_icon_source_set_icon_name (GtkIconSource *source,
2177 const gchar *icon_name)
2179 g_return_if_fail (source != NULL);
2181 if (source->type == GTK_ICON_SOURCE_ICON_NAME &&
2182 source->source.icon_name == icon_name)
2185 icon_source_clear (source);
2187 if (icon_name != NULL)
2189 source->type = GTK_ICON_SOURCE_ICON_NAME;
2190 source->source.icon_name = g_strdup (icon_name);
2195 * gtk_icon_source_set_pixbuf:
2196 * @source: a #GtkIconSource
2197 * @pixbuf: pixbuf to use as a source
2199 * Sets a pixbuf to use as a base image when creating icon variants
2203 gtk_icon_source_set_pixbuf (GtkIconSource *source,
2206 g_return_if_fail (source != NULL);
2207 g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
2209 if (source->type == GTK_ICON_SOURCE_PIXBUF &&
2210 source->source.pixbuf == pixbuf)
2213 icon_source_clear (source);
2217 source->type = GTK_ICON_SOURCE_PIXBUF;
2218 source->source.pixbuf = g_object_ref (pixbuf);
2223 * gtk_icon_source_get_filename:
2224 * @source: a #GtkIconSource
2226 * Retrieves the source filename, or %NULL if none is set. The
2227 * filename is not a copy, and should not be modified or expected to
2228 * persist beyond the lifetime of the icon source.
2230 * Return value: (type filename): image filename. This string must not
2231 * be modified or freed.
2234 gtk_icon_source_get_filename (const GtkIconSource *source)
2236 g_return_val_if_fail (source != NULL, NULL);
2238 if (source->type == GTK_ICON_SOURCE_FILENAME)
2239 return source->source.filename;
2245 * gtk_icon_source_get_icon_name:
2246 * @source: a #GtkIconSource
2248 * Retrieves the source icon name, or %NULL if none is set. The
2249 * icon_name is not a copy, and should not be modified or expected to
2250 * persist beyond the lifetime of the icon source.
2252 * Return value: icon name. This string must not be modified or freed.
2255 gtk_icon_source_get_icon_name (const GtkIconSource *source)
2257 g_return_val_if_fail (source != NULL, NULL);
2259 if (source->type == GTK_ICON_SOURCE_ICON_NAME ||
2260 source->type == GTK_ICON_SOURCE_STATIC_ICON_NAME)
2261 return source->source.icon_name;
2267 * gtk_icon_source_get_pixbuf:
2268 * @source: a #GtkIconSource
2270 * Retrieves the source pixbuf, or %NULL if none is set.
2271 * In addition, if a filename source is in use, this
2272 * function in some cases will return the pixbuf from
2273 * loaded from the filename. This is, for example, true
2274 * for the GtkIconSource passed to the GtkStyle::render_icon()
2275 * virtual function. The reference count on the pixbuf is
2278 * Return value: (transfer none): source pixbuf
2281 gtk_icon_source_get_pixbuf (const GtkIconSource *source)
2283 g_return_val_if_fail (source != NULL, NULL);
2285 if (source->type == GTK_ICON_SOURCE_PIXBUF)
2286 return source->source.pixbuf;
2287 else if (source->type == GTK_ICON_SOURCE_FILENAME)
2288 return source->filename_pixbuf;
2294 * gtk_icon_source_set_direction_wildcarded:
2295 * @source: a #GtkIconSource
2296 * @setting: %TRUE to wildcard the text direction
2298 * If the text direction is wildcarded, this source can be used
2299 * as the base image for an icon in any #GtkTextDirection.
2300 * If the text direction is not wildcarded, then the
2301 * text direction the icon source applies to should be set
2302 * with gtk_icon_source_set_direction(), and the icon source
2303 * will only be used with that text direction.
2305 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2306 * wildcarded sources, and will use an exact match when possible.
2309 gtk_icon_source_set_direction_wildcarded (GtkIconSource *source,
2312 g_return_if_fail (source != NULL);
2314 source->any_direction = setting != FALSE;
2318 * gtk_icon_source_set_state_wildcarded:
2319 * @source: a #GtkIconSource
2320 * @setting: %TRUE to wildcard the widget state
2322 * If the widget state is wildcarded, this source can be used as the
2323 * base image for an icon in any #GtkStateType. If the widget state
2324 * is not wildcarded, then the state the source applies to should be
2325 * set with gtk_icon_source_set_state() and the icon source will
2326 * only be used with that specific state.
2328 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2329 * wildcarded sources, and will use an exact match when possible.
2331 * #GtkIconSet will normally transform wildcarded source images to
2332 * produce an appropriate icon for a given state, for example
2333 * lightening an image on prelight, but will not modify source images
2334 * that match exactly.
2337 gtk_icon_source_set_state_wildcarded (GtkIconSource *source,
2340 g_return_if_fail (source != NULL);
2342 source->any_state = setting != FALSE;
2347 * gtk_icon_source_set_size_wildcarded:
2348 * @source: a #GtkIconSource
2349 * @setting: %TRUE to wildcard the widget state
2351 * If the icon size is wildcarded, this source can be used as the base
2352 * image for an icon of any size. If the size is not wildcarded, then
2353 * the size the source applies to should be set with
2354 * gtk_icon_source_set_size() and the icon source will only be used
2355 * with that specific size.
2357 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2358 * wildcarded sources, and will use an exact match when possible.
2360 * #GtkIconSet will normally scale wildcarded source images to produce
2361 * an appropriate icon at a given size, but will not change the size
2362 * of source images that match exactly.
2365 gtk_icon_source_set_size_wildcarded (GtkIconSource *source,
2368 g_return_if_fail (source != NULL);
2370 source->any_size = setting != FALSE;
2374 * gtk_icon_source_get_size_wildcarded:
2375 * @source: a #GtkIconSource
2377 * Gets the value set by gtk_icon_source_set_size_wildcarded().
2379 * Return value: %TRUE if this icon source is a base for any icon size variant
2382 gtk_icon_source_get_size_wildcarded (const GtkIconSource *source)
2384 g_return_val_if_fail (source != NULL, TRUE);
2386 return source->any_size;
2390 * gtk_icon_source_get_state_wildcarded:
2391 * @source: a #GtkIconSource
2393 * Gets the value set by gtk_icon_source_set_state_wildcarded().
2395 * Return value: %TRUE if this icon source is a base for any widget state variant
2398 gtk_icon_source_get_state_wildcarded (const GtkIconSource *source)
2400 g_return_val_if_fail (source != NULL, TRUE);
2402 return source->any_state;
2406 * gtk_icon_source_get_direction_wildcarded:
2407 * @source: a #GtkIconSource
2409 * Gets the value set by gtk_icon_source_set_direction_wildcarded().
2411 * Return value: %TRUE if this icon source is a base for any text direction variant
2414 gtk_icon_source_get_direction_wildcarded (const GtkIconSource *source)
2416 g_return_val_if_fail (source != NULL, TRUE);
2418 return source->any_direction;
2422 * gtk_icon_source_set_direction:
2423 * @source: a #GtkIconSource
2424 * @direction: text direction this source applies to
2426 * Sets the text direction this icon source is intended to be used
2429 * Setting the text direction on an icon source makes no difference
2430 * if the text direction is wildcarded. Therefore, you should usually
2431 * call gtk_icon_source_set_direction_wildcarded() to un-wildcard it
2432 * in addition to calling this function.
2435 gtk_icon_source_set_direction (GtkIconSource *source,
2436 GtkTextDirection direction)
2438 g_return_if_fail (source != NULL);
2440 source->direction = direction;
2444 * gtk_icon_source_set_state:
2445 * @source: a #GtkIconSource
2446 * @state: widget state this source applies to
2448 * Sets the widget state this icon source is intended to be used
2451 * Setting the widget state on an icon source makes no difference
2452 * if the state is wildcarded. Therefore, you should usually
2453 * call gtk_icon_source_set_state_wildcarded() to un-wildcard it
2454 * in addition to calling this function.
2457 gtk_icon_source_set_state (GtkIconSource *source,
2460 g_return_if_fail (source != NULL);
2462 source->state = state;
2466 * gtk_icon_source_set_size:
2467 * @source: a #GtkIconSource
2468 * @size: (type int): icon size this source applies to
2470 * Sets the icon size this icon source is intended to be used
2473 * Setting the icon size on an icon source makes no difference
2474 * if the size is wildcarded. Therefore, you should usually
2475 * call gtk_icon_source_set_size_wildcarded() to un-wildcard it
2476 * in addition to calling this function.
2479 gtk_icon_source_set_size (GtkIconSource *source,
2482 g_return_if_fail (source != NULL);
2484 source->size = size;
2488 * gtk_icon_source_get_direction:
2489 * @source: a #GtkIconSource
2491 * Obtains the text direction this icon source applies to. The return
2492 * value is only useful/meaningful if the text direction is <emphasis>not</emphasis>
2495 * Return value: text direction this source matches
2498 gtk_icon_source_get_direction (const GtkIconSource *source)
2500 g_return_val_if_fail (source != NULL, 0);
2502 return source->direction;
2506 * gtk_icon_source_get_state:
2507 * @source: a #GtkIconSource
2509 * Obtains the widget state this icon source applies to. The return
2510 * value is only useful/meaningful if the widget state is <emphasis>not</emphasis>
2513 * Return value: widget state this source matches
2516 gtk_icon_source_get_state (const GtkIconSource *source)
2518 g_return_val_if_fail (source != NULL, 0);
2520 return source->state;
2524 * gtk_icon_source_get_size:
2525 * @source: a #GtkIconSource
2527 * Obtains the icon size this source applies to. The return value
2528 * is only useful/meaningful if the icon size is <emphasis>not</emphasis> wildcarded.
2530 * Return value: (type int): icon size this source matches.
2533 gtk_icon_source_get_size (const GtkIconSource *source)
2535 g_return_val_if_fail (source != NULL, 0);
2537 return source->size;
2540 #define NUM_CACHED_ICONS 8
2542 typedef struct _CachedIcon CachedIcon;
2546 /* These must all match to use the cached pixbuf.
2547 * If any don't match, we must re-render the pixbuf.
2549 GtkStyleContext *style;
2550 GtkTextDirection direction;
2558 ensure_cache_up_to_date (GtkIconSet *icon_set)
2560 if (icon_set->cache_serial != cache_serial)
2562 clear_cache (icon_set, TRUE);
2563 icon_set->cache_serial = cache_serial;
2568 cached_icon_free (CachedIcon *icon)
2570 g_object_unref (icon->pixbuf);
2571 g_object_unref (icon->style);
2577 find_in_cache (GtkIconSet *icon_set,
2578 GtkStyleContext *style_context,
2579 GtkTextDirection direction,
2586 ensure_cache_up_to_date (icon_set);
2589 tmp_list = icon_set->cache;
2590 while (tmp_list != NULL)
2592 CachedIcon *icon = tmp_list->data;
2594 if (icon->style == style_context &&
2595 icon->direction == direction &&
2596 icon->state == state &&
2597 (size == (GtkIconSize)-1 || icon->size == size))
2601 /* Move this icon to the front of the list. */
2602 prev->next = tmp_list->next;
2603 tmp_list->next = icon_set->cache;
2604 icon_set->cache = tmp_list;
2607 return icon->pixbuf;
2611 tmp_list = g_slist_next (tmp_list);
2618 add_to_cache (GtkIconSet *icon_set,
2619 GtkStyleContext *style_context,
2620 GtkTextDirection direction,
2627 ensure_cache_up_to_date (icon_set);
2629 g_object_ref (pixbuf);
2631 icon = g_new (CachedIcon, 1);
2632 icon_set->cache = g_slist_prepend (icon_set->cache, icon);
2633 icon_set->cache_size++;
2635 icon->style = g_object_ref (style_context);
2636 icon->direction = direction;
2637 icon->state = state;
2639 icon->pixbuf = pixbuf;
2640 attach_to_style (icon_set, icon->style);
2642 if (icon_set->cache_size >= NUM_CACHED_ICONS)
2644 /* Remove oldest item in the cache */
2647 tmp_list = icon_set->cache;
2649 /* Find next-to-last link */
2650 g_assert (NUM_CACHED_ICONS > 2);
2651 while (tmp_list->next->next)
2652 tmp_list = tmp_list->next;
2654 g_assert (tmp_list != NULL);
2655 g_assert (tmp_list->next != NULL);
2656 g_assert (tmp_list->next->next == NULL);
2658 /* Free the last icon */
2659 icon = tmp_list->next->data;
2661 g_slist_free (tmp_list->next);
2662 tmp_list->next = NULL;
2664 cached_icon_free (icon);
2669 clear_cache (GtkIconSet *icon_set,
2670 gboolean style_detach)
2672 GSList *cache, *tmp_list;
2673 GtkStyleContext *last_style = NULL;
2675 cache = icon_set->cache;
2676 icon_set->cache = NULL;
2677 icon_set->cache_size = 0;
2679 while (tmp_list != NULL)
2681 CachedIcon *icon = tmp_list->data;
2685 /* simple optimization for the case where the cache
2686 * contains contiguous icons from the same style.
2687 * it's safe to call detach_from_style more than
2688 * once on the same style though.
2690 if (last_style != icon->style)
2692 detach_from_style (icon_set, icon->style);
2693 last_style = icon->style;
2697 cached_icon_free (icon);
2699 tmp_list = g_slist_next (tmp_list);
2702 g_slist_free (cache);
2706 copy_cache (GtkIconSet *icon_set,
2707 GtkIconSet *copy_recipient)
2710 GSList *copy = NULL;
2712 ensure_cache_up_to_date (icon_set);
2714 tmp_list = icon_set->cache;
2715 while (tmp_list != NULL)
2717 CachedIcon *icon = tmp_list->data;
2718 CachedIcon *icon_copy = g_new (CachedIcon, 1);
2722 attach_to_style (copy_recipient, icon_copy->style);
2723 g_object_ref (icon_copy->style);
2725 g_object_ref (icon_copy->pixbuf);
2727 icon_copy->size = icon->size;
2729 copy = g_slist_prepend (copy, icon_copy);
2731 tmp_list = g_slist_next (tmp_list);
2734 return g_slist_reverse (copy);
2738 attach_to_style (GtkIconSet *icon_set,
2739 GtkStyleContext *style_context)
2743 table = g_object_get_qdata (G_OBJECT (style_context),
2744 g_quark_try_string ("gtk-style-icon-sets"));
2748 table = g_hash_table_new (NULL, NULL);
2749 g_object_set_qdata_full (G_OBJECT (style_context),
2750 g_quark_from_static_string ("gtk-style-icon-sets"),
2755 g_hash_table_insert (table, icon_set, icon_set);
2759 detach_from_style (GtkIconSet *icon_set,
2760 GtkStyleContext *style_context)
2764 table = g_object_get_qdata (G_OBJECT (style_context),
2765 g_quark_try_string ("gtk-style-icon-sets"));
2768 g_hash_table_remove (table, icon_set);
2772 iconsets_foreach (gpointer key,
2776 GtkIconSet *icon_set = key;
2778 /* We only need to remove cache entries for the given style;
2779 * but that complicates things because in destroy notify
2780 * we don't know which style got destroyed, and 95% of the
2781 * time all cache entries will have the same style,
2782 * so this is faster anyway.
2785 clear_cache (icon_set, FALSE);
2789 style_dnotify (gpointer data)
2791 GHashTable *table = data;
2793 g_hash_table_foreach (table, iconsets_foreach, NULL);
2795 g_hash_table_destroy (table);
2798 /* This allows the icon set to detect that its cache is out of date. */
2800 _gtk_icon_set_invalidate_caches (void)
2806 * _gtk_icon_factory_list_ids:
2808 * Gets all known IDs stored in an existing icon factory.
2809 * The strings in the returned list aren't copied.
2810 * The list itself should be freed.
2812 * Return value: List of ids in icon factories
2815 _gtk_icon_factory_list_ids (void)
2822 _gtk_icon_factory_get_default_icons ();
2824 tmp_list = all_icon_factories;
2825 while (tmp_list != NULL)
2828 GtkIconFactory *factory = GTK_ICON_FACTORY (tmp_list->data);
2829 GtkIconFactoryPrivate *priv = factory->priv;
2831 these_ids = g_hash_table_get_keys (priv->icons);
2833 ids = g_list_concat (ids, these_ids);
2835 tmp_list = g_slist_next (tmp_list);
2845 } IconFactoryParserData;
2851 GtkTextDirection direction;
2854 } IconSourceParserData;
2857 icon_source_start_element (GMarkupParseContext *context,
2858 const gchar *element_name,
2859 const gchar **names,
2860 const gchar **values,
2865 gchar *stock_id = NULL;
2866 gchar *filename = NULL;
2867 gchar *icon_name = NULL;
2869 gint direction = -1;
2871 IconFactoryParserData *parser_data;
2872 IconSourceParserData *source_data;
2874 GQuark error_domain;
2876 parser_data = (IconFactoryParserData*)user_data;
2878 if (!parser_data->in_source)
2880 if (strcmp (element_name, "sources") != 0)
2882 error_msg = g_strdup_printf ("Unexpected element %s, expected <sources>", element_name);
2883 error_domain = GTK_BUILDER_ERROR_INVALID_TAG;
2886 parser_data->in_source = TRUE;
2891 if (strcmp (element_name, "source") != 0)
2893 error_msg = g_strdup_printf ("Unexpected element %s, expected <source>", element_name);
2894 error_domain = GTK_BUILDER_ERROR_INVALID_TAG;
2899 for (i = 0; names[i]; i++)
2901 if (strcmp (names[i], "stock-id") == 0)
2902 stock_id = g_strdup (values[i]);
2903 else if (strcmp (names[i], "filename") == 0)
2904 filename = g_strdup (values[i]);
2905 else if (strcmp (names[i], "icon-name") == 0)
2906 icon_name = g_strdup (values[i]);
2907 else if (strcmp (names[i], "size") == 0)
2909 if (!_gtk_builder_enum_from_string (GTK_TYPE_ICON_SIZE,
2915 else if (strcmp (names[i], "direction") == 0)
2917 if (!_gtk_builder_enum_from_string (GTK_TYPE_TEXT_DIRECTION,
2923 else if (strcmp (names[i], "state") == 0)
2925 if (!_gtk_builder_enum_from_string (GTK_TYPE_STATE_TYPE,
2933 error_msg = g_strdup_printf ("'%s' is not a valid attribute of <%s>",
2934 names[i], "source");
2935 error_domain = GTK_BUILDER_ERROR_INVALID_ATTRIBUTE;
2942 error_msg = g_strdup_printf ("<source> requires a stock_id");
2943 error_domain = GTK_BUILDER_ERROR_MISSING_ATTRIBUTE;
2947 source_data = g_slice_new (IconSourceParserData);
2948 source_data->stock_id = stock_id;
2949 source_data->filename = filename;
2950 source_data->icon_name = icon_name;
2951 source_data->size = size;
2952 source_data->direction = direction;
2953 source_data->state = state;
2955 parser_data->sources = g_slist_prepend (parser_data->sources, source_data);
2961 gint line_number, char_number;
2963 g_markup_parse_context_get_position (context, &line_number, &char_number);
2965 tmp = g_strdup_printf ("%s:%d:%d %s", "input",
2966 line_number, char_number, error_msg);
2967 g_set_error_literal (error, GTK_BUILDER_ERROR, error_domain, tmp);
2976 static const GMarkupParser icon_source_parser =
2978 icon_source_start_element,
2982 gtk_icon_factory_buildable_custom_tag_start (GtkBuildable *buildable,
2983 GtkBuilder *builder,
2985 const gchar *tagname,
2986 GMarkupParser *parser,
2989 g_assert (buildable);
2991 if (strcmp (tagname, "sources") == 0)
2993 IconFactoryParserData *parser_data;
2995 parser_data = g_slice_new0 (IconFactoryParserData);
2996 *parser = icon_source_parser;
2997 *data = parser_data;
3004 gtk_icon_factory_buildable_custom_tag_end (GtkBuildable *buildable,
3005 GtkBuilder *builder,
3007 const gchar *tagname,
3008 gpointer *user_data)
3010 GtkIconFactory *icon_factory;
3012 icon_factory = GTK_ICON_FACTORY (buildable);
3014 if (strcmp (tagname, "sources") == 0)
3016 IconFactoryParserData *parser_data;
3017 GtkIconSource *icon_source;
3018 GtkIconSet *icon_set;
3021 parser_data = (IconFactoryParserData*)user_data;
3023 for (l = parser_data->sources; l; l = l->next)
3025 IconSourceParserData *source_data = l->data;
3027 icon_set = gtk_icon_factory_lookup (icon_factory, source_data->stock_id);
3030 icon_set = gtk_icon_set_new ();
3031 gtk_icon_factory_add (icon_factory, source_data->stock_id, icon_set);
3032 gtk_icon_set_unref (icon_set);
3035 icon_source = gtk_icon_source_new ();
3037 if (source_data->filename)
3040 filename = _gtk_builder_get_absolute_filename (builder, source_data->filename);
3041 gtk_icon_source_set_filename (icon_source, filename);
3044 if (source_data->icon_name)
3045 gtk_icon_source_set_icon_name (icon_source, source_data->icon_name);
3046 if (source_data->size != -1)
3048 gtk_icon_source_set_size (icon_source, source_data->size);
3049 gtk_icon_source_set_size_wildcarded (icon_source, FALSE);
3051 if (source_data->direction != -1)
3053 gtk_icon_source_set_direction (icon_source, source_data->direction);
3054 gtk_icon_source_set_direction_wildcarded (icon_source, FALSE);
3056 if (source_data->state != -1)
3058 gtk_icon_source_set_state (icon_source, source_data->state);
3059 gtk_icon_source_set_state_wildcarded (icon_source, FALSE);
3062 /* Inline source_add() to avoid creating a copy */
3063 g_assert (icon_source->type != GTK_ICON_SOURCE_EMPTY);
3064 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
3066 icon_source_compare);
3068 g_free (source_data->stock_id);
3069 g_free (source_data->filename);
3070 g_free (source_data->icon_name);
3071 g_slice_free (IconSourceParserData, source_data);
3073 g_slist_free (parser_data->sources);
3074 g_slice_free (IconFactoryParserData, parser_data);
3076 /* TODO: Add an attribute/tag to prevent this.
3077 * Usually it's the right thing to do though.
3079 gtk_icon_factory_add_default (icon_factory);