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 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
1735 direction = gtk_style_context_get_direction (context);
1736 G_GNUC_END_IGNORE_DEPRECATIONS;
1738 if (icon_set->sources)
1740 icon = find_in_cache (icon_set, context, direction, state, size);
1743 g_object_ref (icon);
1748 if (icon_set->sources)
1749 icon = find_and_render_icon_source (icon_set, context, direction, state, size);
1752 icon = render_fallback_image (context, direction, state, size);
1754 add_to_cache (icon_set, context, direction, state, size, icon);
1760 * gtk_icon_set_render_icon:
1761 * @icon_set: a #GtkIconSet
1762 * @style: (allow-none): a #GtkStyle associated with @widget, or %NULL
1763 * @direction: text direction
1764 * @state: widget state
1765 * @size: (type int): icon size. A size of (GtkIconSize)-1
1766 * means render at the size of the source and don't scale.
1767 * @widget: (allow-none): widget that will display the icon, or %NULL.
1768 * The only use that is typically made of this
1769 * is to determine the appropriate #GdkScreen.
1770 * @detail: (allow-none): detail to pass to the theme engine, or %NULL.
1771 * Note that passing a detail of anything but %NULL
1772 * will disable caching.
1774 * Renders an icon using gtk_style_render_icon(). In most cases,
1775 * gtk_widget_render_icon() is better, since it automatically provides
1776 * most of the arguments from the current widget settings. This
1777 * function never returns %NULL; if the icon can't be rendered
1778 * (perhaps because an image file fails to load), a default "missing
1779 * image" icon will be returned instead.
1781 * Return value: (transfer full): a #GdkPixbuf to be displayed
1783 * Deprecated: 3.0: Use gtk_icon_set_render_icon_pixbuf() instead
1786 gtk_icon_set_render_icon (GtkIconSet *icon_set,
1788 GtkTextDirection direction,
1795 GtkStyleContext *context = NULL;
1796 GtkStateFlags flags = 0;
1798 g_return_val_if_fail (icon_set != NULL, NULL);
1799 g_return_val_if_fail (style == NULL || GTK_IS_STYLE (style), NULL);
1801 if (style && gtk_style_has_context (style))
1803 g_object_get (style, "context", &context, NULL);
1804 /* g_object_get returns a refed object */
1806 g_object_unref (context);
1810 context = gtk_widget_get_style_context (widget);
1814 return render_fallback_image (context, direction, state, size);
1816 gtk_style_context_save (context);
1820 case GTK_STATE_PRELIGHT:
1821 flags |= GTK_STATE_FLAG_PRELIGHT;
1823 case GTK_STATE_INSENSITIVE:
1824 flags |= GTK_STATE_FLAG_INSENSITIVE;
1830 gtk_style_context_set_state (context, flags);
1831 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
1832 gtk_style_context_set_direction (context, direction);
1833 G_GNUC_END_IGNORE_DEPRECATIONS;
1835 icon = gtk_icon_set_render_icon_pixbuf (icon_set, context, size);
1837 gtk_style_context_restore (context);
1842 /* Order sources by their "wildness", so that "wilder" sources are
1843 * greater than "specific" sources; for determining ordering,
1844 * direction beats state beats size.
1848 icon_source_compare (gconstpointer ap, gconstpointer bp)
1850 const GtkIconSource *a = ap;
1851 const GtkIconSource *b = bp;
1853 if (!a->any_direction && b->any_direction)
1855 else if (a->any_direction && !b->any_direction)
1857 else if (!a->any_state && b->any_state)
1859 else if (a->any_state && !b->any_state)
1861 else if (!a->any_size && b->any_size)
1863 else if (a->any_size && !b->any_size)
1870 * gtk_icon_set_add_source:
1871 * @icon_set: a #GtkIconSet
1872 * @source: a #GtkIconSource
1874 * Icon sets have a list of #GtkIconSource, which they use as base
1875 * icons for rendering icons in different states and sizes. Icons are
1876 * scaled, made to look insensitive, etc. in
1877 * gtk_icon_set_render_icon(), but #GtkIconSet needs base images to
1878 * work with. The base images and when to use them are described by
1881 * This function copies @source, so you can reuse the same source immediately
1882 * without affecting the icon set.
1884 * An example of when you'd use this function: a web browser's "Back
1885 * to Previous Page" icon might point in a different direction in
1886 * Hebrew and in English; it might look different when insensitive;
1887 * and it might change size depending on toolbar mode (small/large
1888 * icons). So a single icon set would contain all those variants of
1889 * the icon, and you might add a separate source for each one.
1891 * You should nearly always add a "default" icon source with all
1892 * fields wildcarded, which will be used as a fallback if no more
1893 * specific source matches. #GtkIconSet always prefers more specific
1894 * icon sources to more generic icon sources. The order in which you
1895 * add the sources to the icon set does not matter.
1897 * gtk_icon_set_new_from_pixbuf() creates a new icon set with a
1898 * default icon source based on the given pixbuf.
1901 gtk_icon_set_add_source (GtkIconSet *icon_set,
1902 const GtkIconSource *source)
1904 g_return_if_fail (icon_set != NULL);
1905 g_return_if_fail (source != NULL);
1907 if (source->type == GTK_ICON_SOURCE_EMPTY)
1909 g_warning ("Useless empty GtkIconSource");
1913 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
1914 gtk_icon_source_copy (source),
1915 icon_source_compare);
1919 * gtk_icon_set_get_sizes:
1920 * @icon_set: a #GtkIconSet
1921 * @sizes: (array length=n_sizes) (out) (type int): return location
1922 * for array of sizes
1923 * @n_sizes: location to store number of elements in returned array
1925 * Obtains a list of icon sizes this icon set can render. The returned
1926 * array must be freed with g_free().
1929 gtk_icon_set_get_sizes (GtkIconSet *icon_set,
1930 GtkIconSize **sizes,
1934 gboolean all_sizes = FALSE;
1935 GSList *specifics = NULL;
1937 g_return_if_fail (icon_set != NULL);
1938 g_return_if_fail (sizes != NULL);
1939 g_return_if_fail (n_sizes != NULL);
1941 tmp_list = icon_set->sources;
1942 while (tmp_list != NULL)
1944 GtkIconSource *source;
1946 source = tmp_list->data;
1948 if (source->any_size)
1954 specifics = g_slist_prepend (specifics, GINT_TO_POINTER (source->size));
1956 tmp_list = g_slist_next (tmp_list);
1961 /* Need to find out what sizes exist */
1966 *sizes = g_new (GtkIconSize, icon_sizes_used);
1967 *n_sizes = icon_sizes_used - 1;
1970 while (i < icon_sizes_used)
1972 (*sizes)[i - 1] = icon_sizes[i].size;
1980 *n_sizes = g_slist_length (specifics);
1981 *sizes = g_new (GtkIconSize, *n_sizes);
1984 tmp_list = specifics;
1985 while (tmp_list != NULL)
1987 (*sizes)[i] = GPOINTER_TO_INT (tmp_list->data);
1990 tmp_list = g_slist_next (tmp_list);
1994 g_slist_free (specifics);
1999 * gtk_icon_source_new:
2001 * Creates a new #GtkIconSource. A #GtkIconSource contains a #GdkPixbuf (or
2002 * image filename) that serves as the base image for one or more of the
2003 * icons in a #GtkIconSet, along with a specification for which icons in the
2004 * icon set will be based on that pixbuf or image file. An icon set contains
2005 * a set of icons that represent "the same" logical concept in different states,
2006 * different global text directions, and different sizes.
2008 * So for example a web browser's "Back to Previous Page" icon might
2009 * point in a different direction in Hebrew and in English; it might
2010 * look different when insensitive; and it might change size depending
2011 * on toolbar mode (small/large icons). So a single icon set would
2012 * contain all those variants of the icon. #GtkIconSet contains a list
2013 * of #GtkIconSource from which it can derive specific icon variants in
2016 * In the simplest case, #GtkIconSet contains one source pixbuf from
2017 * which it derives all variants. The convenience function
2018 * gtk_icon_set_new_from_pixbuf() handles this case; if you only have
2019 * one source pixbuf, just use that function.
2021 * If you want to use a different base pixbuf for different icon
2022 * variants, you create multiple icon sources, mark which variants
2023 * they'll be used to create, and add them to the icon set with
2024 * gtk_icon_set_add_source().
2026 * By default, the icon source has all parameters wildcarded. That is,
2027 * the icon source will be used as the base icon for any desired text
2028 * direction, widget state, or icon size.
2030 * Return value: a new #GtkIconSource
2033 gtk_icon_source_new (void)
2037 src = g_new0 (GtkIconSource, 1);
2039 src->direction = GTK_TEXT_DIR_NONE;
2040 src->size = GTK_ICON_SIZE_INVALID;
2041 src->state = GTK_STATE_NORMAL;
2043 src->any_direction = TRUE;
2044 src->any_state = TRUE;
2045 src->any_size = TRUE;
2051 * gtk_icon_source_copy:
2052 * @source: a #GtkIconSource
2054 * Creates a copy of @source; mostly useful for language bindings.
2056 * Return value: a new #GtkIconSource
2059 gtk_icon_source_copy (const GtkIconSource *source)
2061 GtkIconSource *copy;
2063 g_return_val_if_fail (source != NULL, NULL);
2065 copy = g_new (GtkIconSource, 1);
2071 case GTK_ICON_SOURCE_EMPTY:
2072 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
2074 case GTK_ICON_SOURCE_ICON_NAME:
2075 copy->source.icon_name = g_strdup (copy->source.icon_name);
2077 case GTK_ICON_SOURCE_FILENAME:
2078 copy->source.filename = g_strdup (copy->source.filename);
2079 if (copy->filename_pixbuf)
2080 g_object_ref (copy->filename_pixbuf);
2082 case GTK_ICON_SOURCE_PIXBUF:
2083 g_object_ref (copy->source.pixbuf);
2086 g_assert_not_reached();
2093 * gtk_icon_source_free:
2094 * @source: a #GtkIconSource
2096 * Frees a dynamically-allocated icon source, along with its
2097 * filename, size, and pixbuf fields if those are not %NULL.
2100 gtk_icon_source_free (GtkIconSource *source)
2102 g_return_if_fail (source != NULL);
2104 icon_source_clear (source);
2108 G_DEFINE_BOXED_TYPE (GtkIconSource, gtk_icon_source,
2109 gtk_icon_source_copy,
2110 gtk_icon_source_free)
2113 icon_source_clear (GtkIconSource *source)
2115 switch (source->type)
2117 case GTK_ICON_SOURCE_EMPTY:
2119 case GTK_ICON_SOURCE_ICON_NAME:
2120 g_free (source->source.icon_name);
2122 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
2123 source->source.icon_name = NULL;
2125 case GTK_ICON_SOURCE_FILENAME:
2126 g_free (source->source.filename);
2127 source->source.filename = NULL;
2128 if (source->filename_pixbuf)
2129 g_object_unref (source->filename_pixbuf);
2130 source->filename_pixbuf = NULL;
2132 case GTK_ICON_SOURCE_PIXBUF:
2133 g_object_unref (source->source.pixbuf);
2134 source->source.pixbuf = NULL;
2137 g_assert_not_reached();
2140 source->type = GTK_ICON_SOURCE_EMPTY;
2144 * gtk_icon_source_set_filename:
2145 * @source: a #GtkIconSource
2146 * @filename: (type filename): image file to use
2148 * Sets the name of an image file to use as a base image when creating
2149 * icon variants for #GtkIconSet. The filename must be absolute.
2152 gtk_icon_source_set_filename (GtkIconSource *source,
2153 const gchar *filename)
2155 g_return_if_fail (source != NULL);
2156 g_return_if_fail (filename == NULL || g_path_is_absolute (filename));
2158 if (source->type == GTK_ICON_SOURCE_FILENAME &&
2159 source->source.filename == filename)
2162 icon_source_clear (source);
2164 if (filename != NULL)
2166 source->type = GTK_ICON_SOURCE_FILENAME;
2167 source->source.filename = g_strdup (filename);
2172 * gtk_icon_source_set_icon_name:
2173 * @source: a #GtkIconSource
2174 * @icon_name: (allow-none): name of icon to use
2176 * Sets the name of an icon to look up in the current icon theme
2177 * to use as a base image when creating icon variants for #GtkIconSet.
2180 gtk_icon_source_set_icon_name (GtkIconSource *source,
2181 const gchar *icon_name)
2183 g_return_if_fail (source != NULL);
2185 if (source->type == GTK_ICON_SOURCE_ICON_NAME &&
2186 source->source.icon_name == icon_name)
2189 icon_source_clear (source);
2191 if (icon_name != NULL)
2193 source->type = GTK_ICON_SOURCE_ICON_NAME;
2194 source->source.icon_name = g_strdup (icon_name);
2199 * gtk_icon_source_set_pixbuf:
2200 * @source: a #GtkIconSource
2201 * @pixbuf: pixbuf to use as a source
2203 * Sets a pixbuf to use as a base image when creating icon variants
2207 gtk_icon_source_set_pixbuf (GtkIconSource *source,
2210 g_return_if_fail (source != NULL);
2211 g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
2213 if (source->type == GTK_ICON_SOURCE_PIXBUF &&
2214 source->source.pixbuf == pixbuf)
2217 icon_source_clear (source);
2221 source->type = GTK_ICON_SOURCE_PIXBUF;
2222 source->source.pixbuf = g_object_ref (pixbuf);
2227 * gtk_icon_source_get_filename:
2228 * @source: a #GtkIconSource
2230 * Retrieves the source filename, or %NULL if none is set. The
2231 * filename is not a copy, and should not be modified or expected to
2232 * persist beyond the lifetime of the icon source.
2234 * Return value: (type filename): image filename. This string must not
2235 * be modified or freed.
2238 gtk_icon_source_get_filename (const GtkIconSource *source)
2240 g_return_val_if_fail (source != NULL, NULL);
2242 if (source->type == GTK_ICON_SOURCE_FILENAME)
2243 return source->source.filename;
2249 * gtk_icon_source_get_icon_name:
2250 * @source: a #GtkIconSource
2252 * Retrieves the source icon name, or %NULL if none is set. The
2253 * icon_name is not a copy, and should not be modified or expected to
2254 * persist beyond the lifetime of the icon source.
2256 * Return value: icon name. This string must not be modified or freed.
2259 gtk_icon_source_get_icon_name (const GtkIconSource *source)
2261 g_return_val_if_fail (source != NULL, NULL);
2263 if (source->type == GTK_ICON_SOURCE_ICON_NAME ||
2264 source->type == GTK_ICON_SOURCE_STATIC_ICON_NAME)
2265 return source->source.icon_name;
2271 * gtk_icon_source_get_pixbuf:
2272 * @source: a #GtkIconSource
2274 * Retrieves the source pixbuf, or %NULL if none is set.
2275 * In addition, if a filename source is in use, this
2276 * function in some cases will return the pixbuf from
2277 * loaded from the filename. This is, for example, true
2278 * for the GtkIconSource passed to the GtkStyle::render_icon()
2279 * virtual function. The reference count on the pixbuf is
2282 * Return value: (transfer none): source pixbuf
2285 gtk_icon_source_get_pixbuf (const GtkIconSource *source)
2287 g_return_val_if_fail (source != NULL, NULL);
2289 if (source->type == GTK_ICON_SOURCE_PIXBUF)
2290 return source->source.pixbuf;
2291 else if (source->type == GTK_ICON_SOURCE_FILENAME)
2292 return source->filename_pixbuf;
2298 * gtk_icon_source_set_direction_wildcarded:
2299 * @source: a #GtkIconSource
2300 * @setting: %TRUE to wildcard the text direction
2302 * If the text direction is wildcarded, this source can be used
2303 * as the base image for an icon in any #GtkTextDirection.
2304 * If the text direction is not wildcarded, then the
2305 * text direction the icon source applies to should be set
2306 * with gtk_icon_source_set_direction(), and the icon source
2307 * will only be used with that text direction.
2309 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2310 * wildcarded sources, and will use an exact match when possible.
2313 gtk_icon_source_set_direction_wildcarded (GtkIconSource *source,
2316 g_return_if_fail (source != NULL);
2318 source->any_direction = setting != FALSE;
2322 * gtk_icon_source_set_state_wildcarded:
2323 * @source: a #GtkIconSource
2324 * @setting: %TRUE to wildcard the widget state
2326 * If the widget state is wildcarded, this source can be used as the
2327 * base image for an icon in any #GtkStateType. If the widget state
2328 * is not wildcarded, then the state the source applies to should be
2329 * set with gtk_icon_source_set_state() and the icon source will
2330 * only be used with that specific state.
2332 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2333 * wildcarded sources, and will use an exact match when possible.
2335 * #GtkIconSet will normally transform wildcarded source images to
2336 * produce an appropriate icon for a given state, for example
2337 * lightening an image on prelight, but will not modify source images
2338 * that match exactly.
2341 gtk_icon_source_set_state_wildcarded (GtkIconSource *source,
2344 g_return_if_fail (source != NULL);
2346 source->any_state = setting != FALSE;
2351 * gtk_icon_source_set_size_wildcarded:
2352 * @source: a #GtkIconSource
2353 * @setting: %TRUE to wildcard the widget state
2355 * If the icon size is wildcarded, this source can be used as the base
2356 * image for an icon of any size. If the size is not wildcarded, then
2357 * the size the source applies to should be set with
2358 * gtk_icon_source_set_size() and the icon source will only be used
2359 * with that specific size.
2361 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2362 * wildcarded sources, and will use an exact match when possible.
2364 * #GtkIconSet will normally scale wildcarded source images to produce
2365 * an appropriate icon at a given size, but will not change the size
2366 * of source images that match exactly.
2369 gtk_icon_source_set_size_wildcarded (GtkIconSource *source,
2372 g_return_if_fail (source != NULL);
2374 source->any_size = setting != FALSE;
2378 * gtk_icon_source_get_size_wildcarded:
2379 * @source: a #GtkIconSource
2381 * Gets the value set by gtk_icon_source_set_size_wildcarded().
2383 * Return value: %TRUE if this icon source is a base for any icon size variant
2386 gtk_icon_source_get_size_wildcarded (const GtkIconSource *source)
2388 g_return_val_if_fail (source != NULL, TRUE);
2390 return source->any_size;
2394 * gtk_icon_source_get_state_wildcarded:
2395 * @source: a #GtkIconSource
2397 * Gets the value set by gtk_icon_source_set_state_wildcarded().
2399 * Return value: %TRUE if this icon source is a base for any widget state variant
2402 gtk_icon_source_get_state_wildcarded (const GtkIconSource *source)
2404 g_return_val_if_fail (source != NULL, TRUE);
2406 return source->any_state;
2410 * gtk_icon_source_get_direction_wildcarded:
2411 * @source: a #GtkIconSource
2413 * Gets the value set by gtk_icon_source_set_direction_wildcarded().
2415 * Return value: %TRUE if this icon source is a base for any text direction variant
2418 gtk_icon_source_get_direction_wildcarded (const GtkIconSource *source)
2420 g_return_val_if_fail (source != NULL, TRUE);
2422 return source->any_direction;
2426 * gtk_icon_source_set_direction:
2427 * @source: a #GtkIconSource
2428 * @direction: text direction this source applies to
2430 * Sets the text direction this icon source is intended to be used
2433 * Setting the text direction on an icon source makes no difference
2434 * if the text direction is wildcarded. Therefore, you should usually
2435 * call gtk_icon_source_set_direction_wildcarded() to un-wildcard it
2436 * in addition to calling this function.
2439 gtk_icon_source_set_direction (GtkIconSource *source,
2440 GtkTextDirection direction)
2442 g_return_if_fail (source != NULL);
2444 source->direction = direction;
2448 * gtk_icon_source_set_state:
2449 * @source: a #GtkIconSource
2450 * @state: widget state this source applies to
2452 * Sets the widget state this icon source is intended to be used
2455 * Setting the widget state on an icon source makes no difference
2456 * if the state is wildcarded. Therefore, you should usually
2457 * call gtk_icon_source_set_state_wildcarded() to un-wildcard it
2458 * in addition to calling this function.
2461 gtk_icon_source_set_state (GtkIconSource *source,
2464 g_return_if_fail (source != NULL);
2466 source->state = state;
2470 * gtk_icon_source_set_size:
2471 * @source: a #GtkIconSource
2472 * @size: (type int): icon size this source applies to
2474 * Sets the icon size this icon source is intended to be used
2477 * Setting the icon size on an icon source makes no difference
2478 * if the size is wildcarded. Therefore, you should usually
2479 * call gtk_icon_source_set_size_wildcarded() to un-wildcard it
2480 * in addition to calling this function.
2483 gtk_icon_source_set_size (GtkIconSource *source,
2486 g_return_if_fail (source != NULL);
2488 source->size = size;
2492 * gtk_icon_source_get_direction:
2493 * @source: a #GtkIconSource
2495 * Obtains the text direction this icon source applies to. The return
2496 * value is only useful/meaningful if the text direction is <emphasis>not</emphasis>
2499 * Return value: text direction this source matches
2502 gtk_icon_source_get_direction (const GtkIconSource *source)
2504 g_return_val_if_fail (source != NULL, 0);
2506 return source->direction;
2510 * gtk_icon_source_get_state:
2511 * @source: a #GtkIconSource
2513 * Obtains the widget state this icon source applies to. The return
2514 * value is only useful/meaningful if the widget state is <emphasis>not</emphasis>
2517 * Return value: widget state this source matches
2520 gtk_icon_source_get_state (const GtkIconSource *source)
2522 g_return_val_if_fail (source != NULL, 0);
2524 return source->state;
2528 * gtk_icon_source_get_size:
2529 * @source: a #GtkIconSource
2531 * Obtains the icon size this source applies to. The return value
2532 * is only useful/meaningful if the icon size is <emphasis>not</emphasis> wildcarded.
2534 * Return value: (type int): icon size this source matches.
2537 gtk_icon_source_get_size (const GtkIconSource *source)
2539 g_return_val_if_fail (source != NULL, 0);
2541 return source->size;
2544 #define NUM_CACHED_ICONS 8
2546 typedef struct _CachedIcon CachedIcon;
2550 /* These must all match to use the cached pixbuf.
2551 * If any don't match, we must re-render the pixbuf.
2553 GtkStyleContext *style;
2554 GtkTextDirection direction;
2562 ensure_cache_up_to_date (GtkIconSet *icon_set)
2564 if (icon_set->cache_serial != cache_serial)
2566 clear_cache (icon_set, TRUE);
2567 icon_set->cache_serial = cache_serial;
2572 cached_icon_free (CachedIcon *icon)
2574 g_object_unref (icon->pixbuf);
2575 g_object_unref (icon->style);
2581 find_in_cache (GtkIconSet *icon_set,
2582 GtkStyleContext *style_context,
2583 GtkTextDirection direction,
2590 ensure_cache_up_to_date (icon_set);
2593 tmp_list = icon_set->cache;
2594 while (tmp_list != NULL)
2596 CachedIcon *icon = tmp_list->data;
2598 if (icon->style == style_context &&
2599 icon->direction == direction &&
2600 icon->state == state &&
2601 (size == (GtkIconSize)-1 || icon->size == size))
2605 /* Move this icon to the front of the list. */
2606 prev->next = tmp_list->next;
2607 tmp_list->next = icon_set->cache;
2608 icon_set->cache = tmp_list;
2611 return icon->pixbuf;
2615 tmp_list = g_slist_next (tmp_list);
2622 add_to_cache (GtkIconSet *icon_set,
2623 GtkStyleContext *style_context,
2624 GtkTextDirection direction,
2631 ensure_cache_up_to_date (icon_set);
2633 g_object_ref (pixbuf);
2635 icon = g_new (CachedIcon, 1);
2636 icon_set->cache = g_slist_prepend (icon_set->cache, icon);
2637 icon_set->cache_size++;
2639 icon->style = g_object_ref (style_context);
2640 icon->direction = direction;
2641 icon->state = state;
2643 icon->pixbuf = pixbuf;
2644 attach_to_style (icon_set, icon->style);
2646 if (icon_set->cache_size >= NUM_CACHED_ICONS)
2648 /* Remove oldest item in the cache */
2651 tmp_list = icon_set->cache;
2653 /* Find next-to-last link */
2654 g_assert (NUM_CACHED_ICONS > 2);
2655 while (tmp_list->next->next)
2656 tmp_list = tmp_list->next;
2658 g_assert (tmp_list != NULL);
2659 g_assert (tmp_list->next != NULL);
2660 g_assert (tmp_list->next->next == NULL);
2662 /* Free the last icon */
2663 icon = tmp_list->next->data;
2665 g_slist_free (tmp_list->next);
2666 tmp_list->next = NULL;
2668 cached_icon_free (icon);
2673 clear_cache (GtkIconSet *icon_set,
2674 gboolean style_detach)
2676 GSList *cache, *tmp_list;
2677 GtkStyleContext *last_style = NULL;
2679 cache = icon_set->cache;
2680 icon_set->cache = NULL;
2681 icon_set->cache_size = 0;
2683 while (tmp_list != NULL)
2685 CachedIcon *icon = tmp_list->data;
2689 /* simple optimization for the case where the cache
2690 * contains contiguous icons from the same style.
2691 * it's safe to call detach_from_style more than
2692 * once on the same style though.
2694 if (last_style != icon->style)
2696 detach_from_style (icon_set, icon->style);
2697 last_style = icon->style;
2701 cached_icon_free (icon);
2703 tmp_list = g_slist_next (tmp_list);
2706 g_slist_free (cache);
2710 copy_cache (GtkIconSet *icon_set,
2711 GtkIconSet *copy_recipient)
2714 GSList *copy = NULL;
2716 ensure_cache_up_to_date (icon_set);
2718 tmp_list = icon_set->cache;
2719 while (tmp_list != NULL)
2721 CachedIcon *icon = tmp_list->data;
2722 CachedIcon *icon_copy = g_new (CachedIcon, 1);
2726 attach_to_style (copy_recipient, icon_copy->style);
2727 g_object_ref (icon_copy->style);
2729 g_object_ref (icon_copy->pixbuf);
2731 icon_copy->size = icon->size;
2733 copy = g_slist_prepend (copy, icon_copy);
2735 tmp_list = g_slist_next (tmp_list);
2738 return g_slist_reverse (copy);
2742 attach_to_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"));
2752 table = g_hash_table_new (NULL, NULL);
2753 g_object_set_qdata_full (G_OBJECT (style_context),
2754 g_quark_from_static_string ("gtk-style-icon-sets"),
2759 g_hash_table_insert (table, icon_set, icon_set);
2763 detach_from_style (GtkIconSet *icon_set,
2764 GtkStyleContext *style_context)
2768 table = g_object_get_qdata (G_OBJECT (style_context),
2769 g_quark_try_string ("gtk-style-icon-sets"));
2772 g_hash_table_remove (table, icon_set);
2776 iconsets_foreach (gpointer key,
2780 GtkIconSet *icon_set = key;
2782 /* We only need to remove cache entries for the given style;
2783 * but that complicates things because in destroy notify
2784 * we don't know which style got destroyed, and 95% of the
2785 * time all cache entries will have the same style,
2786 * so this is faster anyway.
2789 clear_cache (icon_set, FALSE);
2793 style_dnotify (gpointer data)
2795 GHashTable *table = data;
2797 g_hash_table_foreach (table, iconsets_foreach, NULL);
2799 g_hash_table_destroy (table);
2802 /* This allows the icon set to detect that its cache is out of date. */
2804 _gtk_icon_set_invalidate_caches (void)
2810 * _gtk_icon_factory_list_ids:
2812 * Gets all known IDs stored in an existing icon factory.
2813 * The strings in the returned list aren't copied.
2814 * The list itself should be freed.
2816 * Return value: List of ids in icon factories
2819 _gtk_icon_factory_list_ids (void)
2826 _gtk_icon_factory_get_default_icons ();
2828 tmp_list = all_icon_factories;
2829 while (tmp_list != NULL)
2832 GtkIconFactory *factory = GTK_ICON_FACTORY (tmp_list->data);
2833 GtkIconFactoryPrivate *priv = factory->priv;
2835 these_ids = g_hash_table_get_keys (priv->icons);
2837 ids = g_list_concat (ids, these_ids);
2839 tmp_list = g_slist_next (tmp_list);
2849 } IconFactoryParserData;
2855 GtkTextDirection direction;
2858 } IconSourceParserData;
2861 icon_source_start_element (GMarkupParseContext *context,
2862 const gchar *element_name,
2863 const gchar **names,
2864 const gchar **values,
2869 gchar *stock_id = NULL;
2870 gchar *filename = NULL;
2871 gchar *icon_name = NULL;
2873 gint direction = -1;
2875 IconFactoryParserData *parser_data;
2876 IconSourceParserData *source_data;
2878 GQuark error_domain;
2880 parser_data = (IconFactoryParserData*)user_data;
2882 if (!parser_data->in_source)
2884 if (strcmp (element_name, "sources") != 0)
2886 error_msg = g_strdup_printf ("Unexpected element %s, expected <sources>", element_name);
2887 error_domain = GTK_BUILDER_ERROR_INVALID_TAG;
2890 parser_data->in_source = TRUE;
2895 if (strcmp (element_name, "source") != 0)
2897 error_msg = g_strdup_printf ("Unexpected element %s, expected <source>", element_name);
2898 error_domain = GTK_BUILDER_ERROR_INVALID_TAG;
2903 for (i = 0; names[i]; i++)
2905 if (strcmp (names[i], "stock-id") == 0)
2906 stock_id = g_strdup (values[i]);
2907 else if (strcmp (names[i], "filename") == 0)
2908 filename = g_strdup (values[i]);
2909 else if (strcmp (names[i], "icon-name") == 0)
2910 icon_name = g_strdup (values[i]);
2911 else if (strcmp (names[i], "size") == 0)
2913 if (!_gtk_builder_enum_from_string (GTK_TYPE_ICON_SIZE,
2919 else if (strcmp (names[i], "direction") == 0)
2921 if (!_gtk_builder_enum_from_string (GTK_TYPE_TEXT_DIRECTION,
2927 else if (strcmp (names[i], "state") == 0)
2929 if (!_gtk_builder_enum_from_string (GTK_TYPE_STATE_TYPE,
2937 error_msg = g_strdup_printf ("'%s' is not a valid attribute of <%s>",
2938 names[i], "source");
2939 error_domain = GTK_BUILDER_ERROR_INVALID_ATTRIBUTE;
2946 error_msg = g_strdup_printf ("<source> requires a stock_id");
2947 error_domain = GTK_BUILDER_ERROR_MISSING_ATTRIBUTE;
2951 source_data = g_slice_new (IconSourceParserData);
2952 source_data->stock_id = stock_id;
2953 source_data->filename = filename;
2954 source_data->icon_name = icon_name;
2955 source_data->size = size;
2956 source_data->direction = direction;
2957 source_data->state = state;
2959 parser_data->sources = g_slist_prepend (parser_data->sources, source_data);
2965 gint line_number, char_number;
2967 g_markup_parse_context_get_position (context, &line_number, &char_number);
2969 tmp = g_strdup_printf ("%s:%d:%d %s", "input",
2970 line_number, char_number, error_msg);
2971 g_set_error_literal (error, GTK_BUILDER_ERROR, error_domain, tmp);
2980 static const GMarkupParser icon_source_parser =
2982 icon_source_start_element,
2986 gtk_icon_factory_buildable_custom_tag_start (GtkBuildable *buildable,
2987 GtkBuilder *builder,
2989 const gchar *tagname,
2990 GMarkupParser *parser,
2993 g_assert (buildable);
2995 if (strcmp (tagname, "sources") == 0)
2997 IconFactoryParserData *parser_data;
2999 parser_data = g_slice_new0 (IconFactoryParserData);
3000 *parser = icon_source_parser;
3001 *data = parser_data;
3008 gtk_icon_factory_buildable_custom_tag_end (GtkBuildable *buildable,
3009 GtkBuilder *builder,
3011 const gchar *tagname,
3012 gpointer *user_data)
3014 GtkIconFactory *icon_factory;
3016 icon_factory = GTK_ICON_FACTORY (buildable);
3018 if (strcmp (tagname, "sources") == 0)
3020 IconFactoryParserData *parser_data;
3021 GtkIconSource *icon_source;
3022 GtkIconSet *icon_set;
3025 parser_data = (IconFactoryParserData*)user_data;
3027 for (l = parser_data->sources; l; l = l->next)
3029 IconSourceParserData *source_data = l->data;
3031 icon_set = gtk_icon_factory_lookup (icon_factory, source_data->stock_id);
3034 icon_set = gtk_icon_set_new ();
3035 gtk_icon_factory_add (icon_factory, source_data->stock_id, icon_set);
3036 gtk_icon_set_unref (icon_set);
3039 icon_source = gtk_icon_source_new ();
3041 if (source_data->filename)
3044 filename = _gtk_builder_get_absolute_filename (builder, source_data->filename);
3045 gtk_icon_source_set_filename (icon_source, filename);
3048 if (source_data->icon_name)
3049 gtk_icon_source_set_icon_name (icon_source, source_data->icon_name);
3050 if (source_data->size != -1)
3052 gtk_icon_source_set_size (icon_source, source_data->size);
3053 gtk_icon_source_set_size_wildcarded (icon_source, FALSE);
3055 if (source_data->direction != -1)
3057 gtk_icon_source_set_direction (icon_source, source_data->direction);
3058 gtk_icon_source_set_direction_wildcarded (icon_source, FALSE);
3060 if (source_data->state != -1)
3062 gtk_icon_source_set_state (icon_source, source_data->state);
3063 gtk_icon_source_set_state_wildcarded (icon_source, FALSE);
3066 /* Inline source_add() to avoid creating a copy */
3067 g_assert (icon_source->type != GTK_ICON_SOURCE_EMPTY);
3068 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
3070 icon_source_compare);
3072 g_free (source_data->stock_id);
3073 g_free (source_data->filename);
3074 g_free (source_data->icon_name);
3075 g_slice_free (IconSourceParserData, source_data);
3077 g_slist_free (parser_data->sources);
3078 g_slice_free (IconFactoryParserData, parser_data);
3080 /* TODO: Add an attribute/tag to prevent this.
3081 * Usually it's the right thing to do though.
3083 gtk_icon_factory_add_default (icon_factory);