1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2000 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkiconfactory.h"
32 #include "gtkiconcache.h"
34 #include "gtkicontheme.h"
35 #include "gtksettingsprivate.h"
37 #include "gtkwidget.h"
39 #include "gtkbuildable.h"
40 #include "gtkbuilderprivate.h"
43 static GSList *all_icon_factories = NULL;
45 struct _GtkIconFactoryPrivate
51 GTK_ICON_SOURCE_EMPTY,
52 GTK_ICON_SOURCE_ICON_NAME,
53 GTK_ICON_SOURCE_STATIC_ICON_NAME,
54 GTK_ICON_SOURCE_FILENAME,
55 GTK_ICON_SOURCE_PIXBUF
60 GtkIconSourceType type;
68 GdkPixbuf *filename_pixbuf;
70 GtkTextDirection direction;
74 /* If TRUE, then the parameter is wildcarded, and the above
75 * fields should be ignored. If FALSE, the parameter is
76 * specified, and the above fields should be valid.
78 guint any_direction : 1;
85 gtk_icon_factory_buildable_init (GtkBuildableIface *iface);
87 static gboolean gtk_icon_factory_buildable_custom_tag_start (GtkBuildable *buildable,
91 GMarkupParser *parser,
93 static void gtk_icon_factory_buildable_custom_tag_end (GtkBuildable *buildable,
98 static void gtk_icon_factory_finalize (GObject *object);
99 static void get_default_icons (GtkIconFactory *icon_factory);
100 static void icon_source_clear (GtkIconSource *source);
102 static GtkIconSize icon_size_register_intern (const gchar *name,
106 #define GTK_ICON_SOURCE_INIT(any_direction, any_state, any_size) \
107 { GTK_ICON_SOURCE_EMPTY, { NULL }, NULL, \
109 any_direction, any_state, any_size }
111 G_DEFINE_TYPE_WITH_CODE (GtkIconFactory, gtk_icon_factory, G_TYPE_OBJECT,
112 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
113 gtk_icon_factory_buildable_init))
116 gtk_icon_factory_init (GtkIconFactory *factory)
118 GtkIconFactoryPrivate *priv;
120 factory->priv = G_TYPE_INSTANCE_GET_PRIVATE (factory,
121 GTK_TYPE_ICON_FACTORY,
122 GtkIconFactoryPrivate);
123 priv = factory->priv;
125 priv->icons = g_hash_table_new (g_str_hash, g_str_equal);
126 all_icon_factories = g_slist_prepend (all_icon_factories, factory);
130 gtk_icon_factory_class_init (GtkIconFactoryClass *klass)
132 GObjectClass *object_class = G_OBJECT_CLASS (klass);
134 object_class->finalize = gtk_icon_factory_finalize;
136 g_type_class_add_private (klass, sizeof (GtkIconFactoryPrivate));
140 gtk_icon_factory_buildable_init (GtkBuildableIface *iface)
142 iface->custom_tag_start = gtk_icon_factory_buildable_custom_tag_start;
143 iface->custom_tag_end = gtk_icon_factory_buildable_custom_tag_end;
147 free_icon_set (gpointer key, gpointer value, gpointer data)
150 gtk_icon_set_unref (value);
154 gtk_icon_factory_finalize (GObject *object)
156 GtkIconFactory *factory = GTK_ICON_FACTORY (object);
157 GtkIconFactoryPrivate *priv = factory->priv;
159 all_icon_factories = g_slist_remove (all_icon_factories, factory);
161 g_hash_table_foreach (priv->icons, free_icon_set, NULL);
163 g_hash_table_destroy (priv->icons);
165 G_OBJECT_CLASS (gtk_icon_factory_parent_class)->finalize (object);
169 * gtk_icon_factory_new:
171 * Creates a new #GtkIconFactory. An icon factory manages a collection
172 * of #GtkIconSet<!-- -->s; a #GtkIconSet manages a set of variants of a
173 * particular icon (i.e. a #GtkIconSet contains variants for different
174 * sizes and widget states). Icons in an icon factory are named by a
175 * stock ID, which is a simple string identifying the icon. Each
176 * #GtkStyle has a list of #GtkIconFactory<!-- -->s derived from the current
177 * theme; those icon factories are consulted first when searching for
178 * an icon. If the theme doesn't set a particular icon, GTK+ looks for
179 * the icon in a list of default icon factories, maintained by
180 * gtk_icon_factory_add_default() and
181 * gtk_icon_factory_remove_default(). Applications with icons should
182 * add a default icon factory with their icons, which will allow
183 * themes to override the icons for the application.
185 * Return value: a new #GtkIconFactory
188 gtk_icon_factory_new (void)
190 return g_object_new (GTK_TYPE_ICON_FACTORY, NULL);
194 * gtk_icon_factory_add:
195 * @factory: a #GtkIconFactory
196 * @stock_id: icon name
197 * @icon_set: icon set
199 * Adds the given @icon_set to the icon factory, under the name
200 * @stock_id. @stock_id should be namespaced for your application,
201 * e.g. "myapp-whatever-icon". Normally applications create a
202 * #GtkIconFactory, then add it to the list of default factories with
203 * gtk_icon_factory_add_default(). Then they pass the @stock_id to
204 * widgets such as #GtkImage to display the icon. Themes can provide
205 * an icon with the same name (such as "myapp-whatever-icon") to
206 * override your application's default icons. If an icon already
207 * existed in @factory for @stock_id, it is unreferenced and replaced
208 * with the new @icon_set.
211 gtk_icon_factory_add (GtkIconFactory *factory,
212 const gchar *stock_id,
213 GtkIconSet *icon_set)
215 GtkIconFactoryPrivate *priv = factory->priv;
216 gpointer old_key = NULL;
217 gpointer old_value = NULL;
219 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
220 g_return_if_fail (stock_id != NULL);
221 g_return_if_fail (icon_set != NULL);
223 g_hash_table_lookup_extended (priv->icons, stock_id,
224 &old_key, &old_value);
226 if (old_value == icon_set)
229 gtk_icon_set_ref (icon_set);
231 /* GHashTable key memory management is so fantastically broken. */
233 g_hash_table_insert (priv->icons, old_key, icon_set);
235 g_hash_table_insert (priv->icons, g_strdup (stock_id), icon_set);
238 gtk_icon_set_unref (old_value);
242 * gtk_icon_factory_lookup:
243 * @factory: a #GtkIconFactory
244 * @stock_id: an icon name
246 * Looks up @stock_id in the icon factory, returning an icon set
247 * if found, otherwise %NULL. For display to the user, you should
248 * use gtk_style_lookup_icon_set() on the #GtkStyle for the
249 * widget that will display the icon, instead of using this
250 * function directly, so that themes are taken into account.
252 * Return value: icon set of @stock_id.
255 gtk_icon_factory_lookup (GtkIconFactory *factory,
256 const gchar *stock_id)
258 GtkIconFactoryPrivate *priv;
260 g_return_val_if_fail (GTK_IS_ICON_FACTORY (factory), NULL);
261 g_return_val_if_fail (stock_id != NULL, NULL);
263 priv = factory->priv;
265 return g_hash_table_lookup (priv->icons, stock_id);
268 static GtkIconFactory *gtk_default_icons = NULL;
269 static GSList *default_factories = NULL;
272 * gtk_icon_factory_add_default:
273 * @factory: a #GtkIconFactory
275 * Adds an icon factory to the list of icon factories searched by
276 * gtk_style_lookup_icon_set(). This means that, for example,
277 * gtk_image_new_from_stock() will be able to find icons in @factory.
278 * There will normally be an icon factory added for each library or
279 * application that comes with icons. The default icon factories
280 * can be overridden by themes.
283 gtk_icon_factory_add_default (GtkIconFactory *factory)
285 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
287 g_object_ref (factory);
289 default_factories = g_slist_prepend (default_factories, factory);
293 * gtk_icon_factory_remove_default:
294 * @factory: a #GtkIconFactory previously added with gtk_icon_factory_add_default()
296 * Removes an icon factory from the list of default icon
297 * factories. Not normally used; you might use it for a library that
298 * can be unloaded or shut down.
301 gtk_icon_factory_remove_default (GtkIconFactory *factory)
303 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
305 default_factories = g_slist_remove (default_factories, factory);
307 g_object_unref (factory);
311 _gtk_icon_factory_ensure_default_icons (void)
313 if (gtk_default_icons == NULL)
315 gtk_default_icons = gtk_icon_factory_new ();
317 get_default_icons (gtk_default_icons);
322 * gtk_icon_factory_lookup_default:
323 * @stock_id: an icon name
325 * Looks for an icon in the list of default icon factories. For
326 * display to the user, you should use gtk_style_lookup_icon_set() on
327 * the #GtkStyle for the widget that will display the icon, instead of
328 * using this function directly, so that themes are taken into
331 * Return value: a #GtkIconSet, or %NULL
334 gtk_icon_factory_lookup_default (const gchar *stock_id)
338 g_return_val_if_fail (stock_id != NULL, NULL);
340 tmp_list = default_factories;
341 while (tmp_list != NULL)
343 GtkIconSet *icon_set =
344 gtk_icon_factory_lookup (GTK_ICON_FACTORY (tmp_list->data),
350 tmp_list = g_slist_next (tmp_list);
353 _gtk_icon_factory_ensure_default_icons ();
355 return gtk_icon_factory_lookup (gtk_default_icons, stock_id);
359 register_stock_icon (GtkIconFactory *factory,
360 const gchar *stock_id,
361 const gchar *icon_name)
363 GtkIconSet *set = gtk_icon_set_new ();
364 GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
366 source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
367 source.source.icon_name = (gchar *)icon_name;
368 source.direction = GTK_TEXT_DIR_NONE;
369 gtk_icon_set_add_source (set, &source);
371 gtk_icon_factory_add (factory, stock_id, set);
372 gtk_icon_set_unref (set);
376 register_bidi_stock_icon (GtkIconFactory *factory,
377 const gchar *stock_id,
378 const gchar *icon_name)
380 GtkIconSet *set = gtk_icon_set_new ();
381 GtkIconSource source = GTK_ICON_SOURCE_INIT (FALSE, TRUE, TRUE);
383 source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
384 source.source.icon_name = (gchar *)icon_name;
385 source.direction = GTK_TEXT_DIR_LTR;
386 gtk_icon_set_add_source (set, &source);
388 source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
389 source.source.icon_name = (gchar *)icon_name;
390 source.direction = GTK_TEXT_DIR_RTL;
391 gtk_icon_set_add_source (set, &source);
393 gtk_icon_factory_add (factory, stock_id, set);
394 gtk_icon_set_unref (set);
398 get_default_icons (GtkIconFactory *factory)
400 /* KEEP IN SYNC with gtkstock.c */
402 register_stock_icon (factory, GTK_STOCK_DIALOG_AUTHENTICATION, "dialog-password");
403 register_stock_icon (factory, GTK_STOCK_DIALOG_ERROR, "dialog-error");
404 register_stock_icon (factory, GTK_STOCK_DIALOG_INFO, "dialog-information");
405 register_stock_icon (factory, GTK_STOCK_DIALOG_QUESTION, "dialog-question");
406 register_stock_icon (factory, GTK_STOCK_DIALOG_WARNING, "dialog-warning");
407 register_stock_icon (factory, GTK_STOCK_DND, GTK_STOCK_DND);
408 register_stock_icon (factory, GTK_STOCK_DND_MULTIPLE, GTK_STOCK_DND_MULTIPLE);
409 register_stock_icon (factory, GTK_STOCK_APPLY, GTK_STOCK_APPLY);
410 register_stock_icon (factory, GTK_STOCK_CANCEL, GTK_STOCK_CANCEL);
411 register_stock_icon (factory, GTK_STOCK_NO, GTK_STOCK_NO);
412 register_stock_icon (factory, GTK_STOCK_OK, GTK_STOCK_OK);
413 register_stock_icon (factory, GTK_STOCK_YES, GTK_STOCK_YES);
414 register_stock_icon (factory, GTK_STOCK_CLOSE, "window-close");
415 register_stock_icon (factory, GTK_STOCK_ADD, "list-add");
416 register_stock_icon (factory, GTK_STOCK_JUSTIFY_CENTER, "format-justify-center");
417 register_stock_icon (factory, GTK_STOCK_JUSTIFY_FILL, "format-justify-fill");
418 register_stock_icon (factory, GTK_STOCK_JUSTIFY_LEFT, "format-justify-left");
419 register_stock_icon (factory, GTK_STOCK_JUSTIFY_RIGHT, "format-justify-right");
420 register_stock_icon (factory, GTK_STOCK_GOTO_BOTTOM, "go-bottom");
421 register_stock_icon (factory, GTK_STOCK_CDROM, "media-optical");
422 register_stock_icon (factory, GTK_STOCK_CONVERT, GTK_STOCK_CONVERT);
423 register_stock_icon (factory, GTK_STOCK_COPY, "edit-copy");
424 register_stock_icon (factory, GTK_STOCK_CUT, "edit-cut");
425 register_stock_icon (factory, GTK_STOCK_GO_DOWN, "go-down");
426 register_stock_icon (factory, GTK_STOCK_EXECUTE, "system-run");
427 register_stock_icon (factory, GTK_STOCK_QUIT, "application-exit");
428 register_bidi_stock_icon (factory, GTK_STOCK_GOTO_FIRST, "go-first");
429 register_stock_icon (factory, GTK_STOCK_SELECT_FONT, GTK_STOCK_SELECT_FONT);
430 register_stock_icon (factory, GTK_STOCK_FULLSCREEN, "view-fullscreen");
431 register_stock_icon (factory, GTK_STOCK_LEAVE_FULLSCREEN, "view-restore");
432 register_stock_icon (factory, GTK_STOCK_HARDDISK, "drive-harddisk");
433 register_stock_icon (factory, GTK_STOCK_HELP, "help-contents");
434 register_stock_icon (factory, GTK_STOCK_HOME, "go-home");
435 register_stock_icon (factory, GTK_STOCK_INFO, "dialog-information");
436 register_bidi_stock_icon (factory, GTK_STOCK_JUMP_TO, "go-jump");
437 register_bidi_stock_icon (factory, GTK_STOCK_GOTO_LAST, "go-last");
438 register_bidi_stock_icon (factory, GTK_STOCK_GO_BACK, "go-previous");
439 register_stock_icon (factory, GTK_STOCK_MISSING_IMAGE, "image-missing");
440 register_stock_icon (factory, GTK_STOCK_NETWORK, "network-idle");
441 register_stock_icon (factory, GTK_STOCK_NEW, "document-new");
442 register_stock_icon (factory, GTK_STOCK_OPEN, "document-open");
443 register_stock_icon (factory, GTK_STOCK_ORIENTATION_PORTRAIT, GTK_STOCK_ORIENTATION_PORTRAIT);
444 register_stock_icon (factory, GTK_STOCK_ORIENTATION_LANDSCAPE, GTK_STOCK_ORIENTATION_LANDSCAPE);
445 register_stock_icon (factory, GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT, GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT);
446 register_stock_icon (factory, GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE, GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE);
447 register_stock_icon (factory, GTK_STOCK_PAGE_SETUP, GTK_STOCK_PAGE_SETUP);
448 register_stock_icon (factory, GTK_STOCK_PASTE, "edit-paste");
449 register_stock_icon (factory, GTK_STOCK_PREFERENCES, GTK_STOCK_PREFERENCES);
450 register_stock_icon (factory, GTK_STOCK_PRINT, "document-print");
451 register_stock_icon (factory, GTK_STOCK_PRINT_ERROR, "printer-error");
452 register_stock_icon (factory, GTK_STOCK_PRINT_PAUSED, "printer-paused");
453 register_stock_icon (factory, GTK_STOCK_PRINT_PREVIEW, "document-print-preview");
454 register_stock_icon (factory, GTK_STOCK_PRINT_REPORT, "printer-info");
455 register_stock_icon (factory, GTK_STOCK_PRINT_WARNING, "printer-warning");
456 register_stock_icon (factory, GTK_STOCK_PROPERTIES, "document-properties");
457 register_bidi_stock_icon (factory, GTK_STOCK_REDO, "edit-redo");
458 register_stock_icon (factory, GTK_STOCK_REMOVE, "list-remove");
459 register_stock_icon (factory, GTK_STOCK_REFRESH, "view-refresh");
460 register_bidi_stock_icon (factory, GTK_STOCK_REVERT_TO_SAVED, "document-revert");
461 register_bidi_stock_icon (factory, GTK_STOCK_GO_FORWARD, "go-next");
462 register_stock_icon (factory, GTK_STOCK_SAVE, "document-save");
463 register_stock_icon (factory, GTK_STOCK_FLOPPY, "media-floppy");
464 register_stock_icon (factory, GTK_STOCK_SAVE_AS, "document-save-as");
465 register_stock_icon (factory, GTK_STOCK_FIND, "edit-find");
466 register_stock_icon (factory, GTK_STOCK_FIND_AND_REPLACE, "edit-find-replace");
467 register_stock_icon (factory, GTK_STOCK_SORT_DESCENDING, "view-sort-descending");
468 register_stock_icon (factory, GTK_STOCK_SORT_ASCENDING, "view-sort-ascending");
469 register_stock_icon (factory, GTK_STOCK_SPELL_CHECK, "tools-check-spelling");
470 register_stock_icon (factory, GTK_STOCK_STOP, "process-stop");
471 register_stock_icon (factory, GTK_STOCK_BOLD, "format-text-bold");
472 register_stock_icon (factory, GTK_STOCK_ITALIC, "format-text-italic");
473 register_stock_icon (factory, GTK_STOCK_STRIKETHROUGH, "format-text-strikethrough");
474 register_stock_icon (factory, GTK_STOCK_UNDERLINE, "format-text-underline");
475 register_bidi_stock_icon (factory, GTK_STOCK_INDENT, "format-indent-more");
476 register_bidi_stock_icon (factory, GTK_STOCK_UNINDENT, "format-indent-less");
477 register_stock_icon (factory, GTK_STOCK_GOTO_TOP, "go-top");
478 register_stock_icon (factory, GTK_STOCK_DELETE, "edit-delete");
479 register_bidi_stock_icon (factory, GTK_STOCK_UNDELETE, GTK_STOCK_UNDELETE);
480 register_bidi_stock_icon (factory, GTK_STOCK_UNDO, "edit-undo");
481 register_stock_icon (factory, GTK_STOCK_GO_UP, "go-up");
482 register_stock_icon (factory, GTK_STOCK_FILE, "document-x-generic");
483 register_stock_icon (factory, GTK_STOCK_DIRECTORY, "folder");
484 register_stock_icon (factory, GTK_STOCK_ABOUT, "help-about");
485 register_stock_icon (factory, GTK_STOCK_CONNECT, GTK_STOCK_CONNECT);
486 register_stock_icon (factory, GTK_STOCK_DISCONNECT, GTK_STOCK_DISCONNECT);
487 register_stock_icon (factory, GTK_STOCK_EDIT, GTK_STOCK_EDIT);
488 register_stock_icon (factory, GTK_STOCK_CAPS_LOCK_WARNING, GTK_STOCK_CAPS_LOCK_WARNING);
489 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_FORWARD, "media-seek-forward");
490 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_NEXT, "media-skip-forward");
491 register_stock_icon (factory, GTK_STOCK_MEDIA_PAUSE, "media-playback-pause");
492 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_PLAY, "media-playback-start");
493 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_PREVIOUS, "media-skip-backward");
494 register_stock_icon (factory, GTK_STOCK_MEDIA_RECORD, "media-record");
495 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_REWIND, "media-seek-backward");
496 register_stock_icon (factory, GTK_STOCK_MEDIA_STOP, "media-playback-stop");
497 register_stock_icon (factory, GTK_STOCK_INDEX, GTK_STOCK_INDEX);
498 register_stock_icon (factory, GTK_STOCK_ZOOM_100, "zoom-original");
499 register_stock_icon (factory, GTK_STOCK_ZOOM_IN, "zoom-in");
500 register_stock_icon (factory, GTK_STOCK_ZOOM_OUT, "zoom-out");
501 register_stock_icon (factory, GTK_STOCK_ZOOM_FIT, "zoom-fit-best");
502 register_stock_icon (factory, GTK_STOCK_SELECT_ALL, "edit-select-all");
503 register_stock_icon (factory, GTK_STOCK_CLEAR, "edit-clear");
504 register_stock_icon (factory, GTK_STOCK_SELECT_COLOR, GTK_STOCK_SELECT_COLOR);
505 register_stock_icon (factory, GTK_STOCK_COLOR_PICKER, GTK_STOCK_COLOR_PICKER);
508 /************************************************************
509 * Icon size handling *
510 ************************************************************/
512 typedef struct _IconSize IconSize;
523 typedef struct _IconAlias IconAlias;
531 typedef struct _SettingsIconSize SettingsIconSize;
533 struct _SettingsIconSize
539 static GHashTable *icon_aliases = NULL;
540 static IconSize *icon_sizes = NULL;
541 static gint icon_sizes_allocated = 0;
542 static gint icon_sizes_used = 0;
545 init_icon_sizes (void)
547 if (icon_sizes == NULL)
549 #define NUM_BUILTIN_SIZES 7
552 icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
554 icon_sizes = g_new (IconSize, NUM_BUILTIN_SIZES);
555 icon_sizes_allocated = NUM_BUILTIN_SIZES;
556 icon_sizes_used = NUM_BUILTIN_SIZES;
558 icon_sizes[GTK_ICON_SIZE_INVALID].size = 0;
559 icon_sizes[GTK_ICON_SIZE_INVALID].name = NULL;
560 icon_sizes[GTK_ICON_SIZE_INVALID].width = 0;
561 icon_sizes[GTK_ICON_SIZE_INVALID].height = 0;
563 /* the name strings aren't copied since we don't ever remove
564 * icon sizes, so we don't need to know whether they're static.
565 * Even if we did I suppose removing the builtin sizes would be
569 icon_sizes[GTK_ICON_SIZE_MENU].size = GTK_ICON_SIZE_MENU;
570 icon_sizes[GTK_ICON_SIZE_MENU].name = "gtk-menu";
571 icon_sizes[GTK_ICON_SIZE_MENU].width = 16;
572 icon_sizes[GTK_ICON_SIZE_MENU].height = 16;
574 icon_sizes[GTK_ICON_SIZE_BUTTON].size = GTK_ICON_SIZE_BUTTON;
575 icon_sizes[GTK_ICON_SIZE_BUTTON].name = "gtk-button";
576 icon_sizes[GTK_ICON_SIZE_BUTTON].width = 20;
577 icon_sizes[GTK_ICON_SIZE_BUTTON].height = 20;
579 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].size = GTK_ICON_SIZE_SMALL_TOOLBAR;
580 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].name = "gtk-small-toolbar";
581 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].width = 18;
582 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].height = 18;
584 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].size = GTK_ICON_SIZE_LARGE_TOOLBAR;
585 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].name = "gtk-large-toolbar";
586 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].width = 24;
587 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].height = 24;
589 icon_sizes[GTK_ICON_SIZE_DND].size = GTK_ICON_SIZE_DND;
590 icon_sizes[GTK_ICON_SIZE_DND].name = "gtk-dnd";
591 icon_sizes[GTK_ICON_SIZE_DND].width = 32;
592 icon_sizes[GTK_ICON_SIZE_DND].height = 32;
594 icon_sizes[GTK_ICON_SIZE_DIALOG].size = GTK_ICON_SIZE_DIALOG;
595 icon_sizes[GTK_ICON_SIZE_DIALOG].name = "gtk-dialog";
596 icon_sizes[GTK_ICON_SIZE_DIALOG].width = 48;
597 icon_sizes[GTK_ICON_SIZE_DIALOG].height = 48;
599 g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
601 /* Alias everything to itself. */
602 i = 1; /* skip invalid size */
603 while (i < NUM_BUILTIN_SIZES)
605 gtk_icon_size_register_alias (icon_sizes[i].name, icon_sizes[i].size);
610 #undef NUM_BUILTIN_SIZES
615 free_settings_sizes (gpointer data)
617 g_array_free (data, TRUE);
621 get_settings_sizes (GtkSettings *settings,
624 GArray *settings_sizes;
625 static GQuark sizes_quark = 0;
628 sizes_quark = g_quark_from_static_string ("gtk-icon-sizes");
630 settings_sizes = g_object_get_qdata (G_OBJECT (settings), sizes_quark);
633 settings_sizes = g_array_new (FALSE, FALSE, sizeof (SettingsIconSize));
634 g_object_set_qdata_full (G_OBJECT (settings), sizes_quark,
635 settings_sizes, free_settings_sizes);
640 return settings_sizes;
644 icon_size_set_for_settings (GtkSettings *settings,
645 const gchar *size_name,
650 GArray *settings_sizes;
651 SettingsIconSize *settings_size;
653 g_return_if_fail (size_name != NULL);
655 size = gtk_icon_size_from_name (size_name);
656 if (size == GTK_ICON_SIZE_INVALID)
657 /* Reserve a place */
658 size = icon_size_register_intern (size_name, -1, -1);
660 settings_sizes = get_settings_sizes (settings, NULL);
661 if (size >= settings_sizes->len)
663 SettingsIconSize unset = { -1, -1 };
666 for (i = settings_sizes->len; i <= size; i++)
667 g_array_append_val (settings_sizes, unset);
670 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
672 settings_size->width = width;
673 settings_size->height = height;
676 /* Like pango_parse_word, but accept - as well
679 scan_icon_size_name (const char **pos, GString *out)
681 const char *p = *pos;
683 while (g_ascii_isspace (*p))
686 if (!((*p >= 'A' && *p <= 'Z') ||
687 (*p >= 'a' && *p <= 'z') ||
688 *p == '_' || *p == '-'))
691 g_string_truncate (out, 0);
692 g_string_append_c (out, *p);
695 while ((*p >= 'A' && *p <= 'Z') ||
696 (*p >= 'a' && *p <= 'z') ||
697 (*p >= '0' && *p <= '9') ||
698 *p == '_' || *p == '-')
700 g_string_append_c (out, *p);
710 icon_size_setting_parse (GtkSettings *settings,
711 const gchar *icon_size_string)
713 GString *name_buf = g_string_new (NULL);
714 const gchar *p = icon_size_string;
716 while (pango_skip_space (&p))
720 if (!scan_icon_size_name (&p, name_buf))
723 if (!pango_skip_space (&p))
731 if (!pango_scan_int (&p, &width))
734 if (!pango_skip_space (&p))
742 if (!pango_scan_int (&p, &height))
745 if (width > 0 && height > 0)
747 icon_size_set_for_settings (settings, name_buf->str,
752 g_warning ("Invalid size in gtk-icon-sizes: %d,%d\n", width, height);
755 pango_skip_space (&p);
764 g_string_free (name_buf, TRUE);
768 g_warning ("Error parsing gtk-icon-sizes string:\n\t'%s'", icon_size_string);
769 g_string_free (name_buf, TRUE);
773 icon_size_set_all_from_settings (GtkSettings *settings)
775 GArray *settings_sizes;
776 gchar *icon_size_string;
778 /* Reset old settings */
779 settings_sizes = get_settings_sizes (settings, NULL);
780 g_array_set_size (settings_sizes, 0);
782 g_object_get (settings,
783 "gtk-icon-sizes", &icon_size_string,
786 if (icon_size_string)
788 icon_size_setting_parse (settings, icon_size_string);
789 g_free (icon_size_string);
794 icon_size_settings_changed (GtkSettings *settings,
797 icon_size_set_all_from_settings (settings);
799 gtk_style_context_reset_widgets (_gtk_settings_get_screen (settings));
803 icon_sizes_init_for_settings (GtkSettings *settings)
805 g_signal_connect (settings,
806 "notify::gtk-icon-sizes",
807 G_CALLBACK (icon_size_settings_changed),
810 icon_size_set_all_from_settings (settings);
814 icon_size_lookup_intern (GtkSettings *settings,
819 GArray *settings_sizes;
820 gint width_for_settings = -1;
821 gint height_for_settings = -1;
825 if (size == (GtkIconSize)-1)
828 if (size >= icon_sizes_used)
831 if (size == GTK_ICON_SIZE_INVALID)
836 gboolean initial = FALSE;
838 settings_sizes = get_settings_sizes (settings, &initial);
841 icon_sizes_init_for_settings (settings);
843 if (size < settings_sizes->len)
845 SettingsIconSize *settings_size;
847 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
849 width_for_settings = settings_size->width;
850 height_for_settings = settings_size->height;
855 *widthp = width_for_settings >= 0 ? width_for_settings : icon_sizes[size].width;
858 *heightp = height_for_settings >= 0 ? height_for_settings : icon_sizes[size].height;
864 * gtk_icon_size_lookup_for_settings:
865 * @settings: a #GtkSettings object, used to determine
866 * which set of user preferences to used.
867 * @size: (type int): an icon size
868 * @width: (out): location to store icon width
869 * @height: (out): location to store icon height
871 * Obtains the pixel size of a semantic icon size, possibly
872 * modified by user preferences for a particular
873 * #GtkSettings. Normally @size would be
874 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
875 * isn't normally needed, gtk_widget_render_icon_pixbuf() is the usual
876 * way to get an icon for rendering, then just look at the size of
877 * the rendered pixbuf. The rendered pixbuf may not even correspond to
878 * the width/height returned by gtk_icon_size_lookup(), because themes
879 * are free to render the pixbuf however they like, including changing
882 * Return value: %TRUE if @size was a valid size
887 gtk_icon_size_lookup_for_settings (GtkSettings *settings,
892 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
894 return icon_size_lookup_intern (settings, size, width, height);
898 * gtk_icon_size_lookup:
899 * @size: (type int): an icon size
900 * @width: (out): location to store icon width
901 * @height: (out): location to store icon height
903 * Obtains the pixel size of a semantic icon size, possibly
904 * modified by user preferences for the default #GtkSettings.
905 * (See gtk_icon_size_lookup_for_settings().)
906 * Normally @size would be
907 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
908 * isn't normally needed, gtk_widget_render_icon_pixbuf() is the usual
909 * way to get an icon for rendering, then just look at the size of
910 * the rendered pixbuf. The rendered pixbuf may not even correspond to
911 * the width/height returned by gtk_icon_size_lookup(), because themes
912 * are free to render the pixbuf however they like, including changing
915 * Return value: %TRUE if @size was a valid size
918 gtk_icon_size_lookup (GtkIconSize size,
923 g_warning ("gtk_icon_size_lookup ()) is not multihead safe"));
925 return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (),
926 size, widthp, heightp);
930 icon_size_register_intern (const gchar *name,
934 IconAlias *old_alias;
939 old_alias = g_hash_table_lookup (icon_aliases, name);
940 if (old_alias && icon_sizes[old_alias->target].width > 0)
942 g_warning ("Icon size name '%s' already exists", name);
943 return GTK_ICON_SIZE_INVALID;
948 size = old_alias->target;
952 if (icon_sizes_used == icon_sizes_allocated)
954 icon_sizes_allocated *= 2;
955 icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated);
958 size = icon_sizes_used++;
961 gtk_icon_size_register_alias (name, size);
963 icon_sizes[size].size = size;
964 icon_sizes[size].name = g_strdup (name);
967 icon_sizes[size].width = width;
968 icon_sizes[size].height = height;
974 * gtk_icon_size_register:
975 * @name: name of the icon size
976 * @width: the icon width
977 * @height: the icon height
979 * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
980 * etc. Returns the integer value for the size.
982 * Returns: (type int): integer value representing the size
985 gtk_icon_size_register (const gchar *name,
989 g_return_val_if_fail (name != NULL, 0);
990 g_return_val_if_fail (width > 0, 0);
991 g_return_val_if_fail (height > 0, 0);
993 return icon_size_register_intern (name, width, height);
997 * gtk_icon_size_register_alias:
998 * @alias: an alias for @target
999 * @target: (type int): an existing icon size
1001 * Registers @alias as another name for @target.
1002 * So calling gtk_icon_size_from_name() with @alias as argument
1003 * will return @target.
1006 gtk_icon_size_register_alias (const gchar *alias,
1011 g_return_if_fail (alias != NULL);
1015 if (!icon_size_lookup_intern (NULL, target, NULL, NULL))
1016 g_warning ("gtk_icon_size_register_alias: Icon size %u does not exist", target);
1018 ia = g_hash_table_lookup (icon_aliases, alias);
1021 if (icon_sizes[ia->target].width > 0)
1023 g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias);
1027 ia->target = target;
1032 ia = g_new (IconAlias, 1);
1033 ia->name = g_strdup (alias);
1034 ia->target = target;
1036 g_hash_table_insert (icon_aliases, ia->name, ia);
1041 * gtk_icon_size_from_name:
1042 * @name: the name to look up.
1044 * Looks up the icon size associated with @name.
1046 * Return value: (type int): the icon size
1049 gtk_icon_size_from_name (const gchar *name)
1055 ia = g_hash_table_lookup (icon_aliases, name);
1057 if (ia && icon_sizes[ia->target].width > 0)
1060 return GTK_ICON_SIZE_INVALID;
1064 * gtk_icon_size_get_name:
1065 * @size: (type int): a #GtkIconSize.
1066 * @returns: the name of the given icon size.
1068 * Gets the canonical name of the given icon size. The returned string
1069 * is statically allocated and should not be freed.
1071 G_CONST_RETURN gchar*
1072 gtk_icon_size_get_name (GtkIconSize size)
1074 if (size >= icon_sizes_used)
1077 return icon_sizes[size].name;
1080 /************************************************************/
1085 static GdkPixbuf *find_in_cache (GtkIconSet *icon_set,
1086 GtkStyleContext *style_context,
1087 GtkTextDirection direction,
1090 static void add_to_cache (GtkIconSet *icon_set,
1091 GtkStyleContext *style_context,
1092 GtkTextDirection direction,
1096 /* Clear icon set contents, drop references to all contained
1097 * GdkPixbuf objects and forget all GtkIconSources. Used to
1098 * recycle an icon set.
1100 static void clear_cache (GtkIconSet *icon_set,
1101 gboolean style_detach);
1102 static GSList* copy_cache (GtkIconSet *icon_set,
1103 GtkIconSet *copy_recipient);
1104 static void attach_to_style (GtkIconSet *icon_set,
1105 GtkStyleContext *style_context);
1106 static void detach_from_style (GtkIconSet *icon_set,
1107 GtkStyleContext *style_context);
1108 static void style_dnotify (gpointer data);
1116 /* Cache of the last few rendered versions of the icon. */
1124 static guint cache_serial = 0;
1129 * Creates a new #GtkIconSet. A #GtkIconSet represents a single icon
1130 * in various sizes and widget states. It can provide a #GdkPixbuf
1131 * for a given size and state on request, and automatically caches
1132 * some of the rendered #GdkPixbuf objects.
1134 * Normally you would use gtk_widget_render_icon_pixbuf() instead of
1135 * using #GtkIconSet directly. The one case where you'd use
1136 * #GtkIconSet is to create application-specific icon sets to place in
1137 * a #GtkIconFactory.
1139 * Return value: a new #GtkIconSet
1142 gtk_icon_set_new (void)
1144 GtkIconSet *icon_set;
1146 icon_set = g_new (GtkIconSet, 1);
1148 icon_set->ref_count = 1;
1149 icon_set->sources = NULL;
1150 icon_set->cache = NULL;
1151 icon_set->cache_size = 0;
1152 icon_set->cache_serial = cache_serial;
1158 * gtk_icon_set_new_from_pixbuf:
1159 * @pixbuf: a #GdkPixbuf
1161 * Creates a new #GtkIconSet with @pixbuf as the default/fallback
1162 * source image. If you don't add any additional #GtkIconSource to the
1163 * icon set, all variants of the icon will be created from @pixbuf,
1164 * using scaling, pixelation, etc. as required to adjust the icon size
1165 * or make the icon look insensitive/prelighted.
1167 * Return value: a new #GtkIconSet
1170 gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
1174 GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1176 g_return_val_if_fail (pixbuf != NULL, NULL);
1178 set = gtk_icon_set_new ();
1180 gtk_icon_source_set_pixbuf (&source, pixbuf);
1181 gtk_icon_set_add_source (set, &source);
1182 gtk_icon_source_set_pixbuf (&source, NULL);
1190 * @icon_set: a #GtkIconSet.
1192 * Increments the reference count on @icon_set.
1194 * Return value: @icon_set.
1197 gtk_icon_set_ref (GtkIconSet *icon_set)
1199 g_return_val_if_fail (icon_set != NULL, NULL);
1200 g_return_val_if_fail (icon_set->ref_count > 0, NULL);
1202 icon_set->ref_count += 1;
1208 * gtk_icon_set_unref:
1209 * @icon_set: a #GtkIconSet
1211 * Decrements the reference count on @icon_set, and frees memory
1212 * if the reference count reaches 0.
1215 gtk_icon_set_unref (GtkIconSet *icon_set)
1217 g_return_if_fail (icon_set != NULL);
1218 g_return_if_fail (icon_set->ref_count > 0);
1220 icon_set->ref_count -= 1;
1222 if (icon_set->ref_count == 0)
1224 GSList *tmp_list = icon_set->sources;
1225 while (tmp_list != NULL)
1227 gtk_icon_source_free (tmp_list->data);
1229 tmp_list = g_slist_next (tmp_list);
1231 g_slist_free (icon_set->sources);
1233 clear_cache (icon_set, TRUE);
1239 G_DEFINE_BOXED_TYPE (GtkIconSet, gtk_icon_set,
1244 * gtk_icon_set_copy:
1245 * @icon_set: a #GtkIconSet
1247 * Copies @icon_set by value.
1249 * Return value: a new #GtkIconSet identical to the first.
1252 gtk_icon_set_copy (GtkIconSet *icon_set)
1257 copy = gtk_icon_set_new ();
1259 tmp_list = icon_set->sources;
1260 while (tmp_list != NULL)
1262 copy->sources = g_slist_prepend (copy->sources,
1263 gtk_icon_source_copy (tmp_list->data));
1265 tmp_list = g_slist_next (tmp_list);
1268 copy->sources = g_slist_reverse (copy->sources);
1270 copy->cache = copy_cache (icon_set, copy);
1271 copy->cache_size = icon_set->cache_size;
1272 copy->cache_serial = icon_set->cache_serial;
1278 sizes_equivalent (GtkIconSize lhs,
1281 /* We used to consider sizes equivalent if they were
1282 * the same pixel size, but we don't have the GtkSettings
1283 * here, so we can't do that. Plus, it's not clear that
1284 * it is right... it was just a workaround for the fact
1285 * that we register icons by logical size, not pixel size.
1291 gint r_w, r_h, l_w, l_h;
1293 icon_size_lookup_intern (NULL, rhs, &r_w, &r_h);
1294 icon_size_lookup_intern (NULL, lhs, &l_w, &l_h);
1296 return r_w == l_w && r_h == l_h;
1300 static GtkIconSource *
1301 find_best_matching_source (GtkIconSet *icon_set,
1302 GtkTextDirection direction,
1307 GtkIconSource *source;
1310 /* We need to find the best icon source. Direction matters more
1311 * than state, state matters more than size. icon_set->sources
1312 * is sorted according to wildness, so if we take the first
1313 * match we find it will be the least-wild match (if there are
1314 * multiple matches for a given "wildness" then the RC file contained
1315 * dumb stuff, and we end up with an arbitrary matching source)
1319 tmp_list = icon_set->sources;
1320 while (tmp_list != NULL)
1322 GtkIconSource *s = tmp_list->data;
1324 if ((s->any_direction || (s->direction == direction)) &&
1325 (s->any_state || (s->state == state)) &&
1326 (s->any_size || size == (GtkIconSize)-1 || (sizes_equivalent (size, s->size))))
1328 if (!g_slist_find (failed, s))
1335 tmp_list = g_slist_next (tmp_list);
1342 ensure_filename_pixbuf (GtkIconSet *icon_set,
1343 GtkIconSource *source)
1345 if (source->filename_pixbuf == NULL)
1347 GError *error = NULL;
1349 source->filename_pixbuf = gdk_pixbuf_new_from_file (source->source.filename, &error);
1351 if (source->filename_pixbuf == NULL)
1353 /* Remove this icon source so we don't keep trying to
1356 g_warning (_("Error loading icon: %s"), error->message);
1357 g_error_free (error);
1359 icon_set->sources = g_slist_remove (icon_set->sources, source);
1361 gtk_icon_source_free (source);
1371 render_icon_name_pixbuf (GtkIconSource *icon_source,
1372 GtkStyleContext *context,
1376 GdkPixbuf *tmp_pixbuf;
1377 GtkIconSource tmp_source;
1379 GtkIconTheme *icon_theme;
1380 GtkSettings *settings;
1381 gint width, height, pixel_size;
1382 gint *sizes, *s, dist;
1383 GError *error = NULL;
1385 screen = gtk_style_context_get_screen (context);
1386 icon_theme = gtk_icon_theme_get_for_screen (screen);
1387 settings = gtk_settings_get_for_screen (screen);
1389 if (!gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
1391 if (size == (GtkIconSize)-1)
1393 /* Find an available size close to 48 */
1394 sizes = gtk_icon_theme_get_icon_sizes (icon_theme, icon_source->source.icon_name);
1396 width = height = 48;
1397 for (s = sizes; *s; s++)
1401 width = height = 48;
1408 width = height = *s;
1416 width = height = *s;
1426 g_warning ("Invalid icon size %u\n", size);
1427 width = height = 24;
1431 pixel_size = MIN (width, height);
1433 if (icon_source->direction != GTK_TEXT_DIR_NONE)
1435 gchar *suffix[3] = { NULL, "-ltr", "-rtl" };
1439 names[0] = g_strconcat (icon_source->source.icon_name, suffix[icon_source->direction], NULL);
1440 names[1] = icon_source->source.icon_name;
1443 info = gtk_icon_theme_choose_icon (icon_theme,
1444 (const char **) names,
1445 pixel_size, GTK_ICON_LOOKUP_USE_BUILTIN);
1449 tmp_pixbuf = gtk_icon_info_load_icon (info, &error);
1450 gtk_icon_info_free (info);
1457 tmp_pixbuf = gtk_icon_theme_load_icon (icon_theme,
1458 icon_source->source.icon_name,
1465 g_warning ("Error loading theme icon '%s' for stock: %s",
1466 icon_source->source.icon_name, error ? error->message : "");
1468 g_error_free (error);
1472 tmp_source = *icon_source;
1473 tmp_source.type = GTK_ICON_SOURCE_PIXBUF;
1474 tmp_source.source.pixbuf = tmp_pixbuf;
1476 pixbuf = gtk_render_icon_pixbuf (context, &tmp_source, -1);
1479 g_warning ("Failed to render icon");
1481 g_object_unref (tmp_pixbuf);
1487 find_and_render_icon_source (GtkIconSet *icon_set,
1488 GtkStyleContext *context,
1489 GtkTextDirection direction,
1493 GSList *failed = NULL;
1494 GdkPixbuf *pixbuf = NULL;
1496 /* We treat failure in two different ways:
1498 * A) If loading a source that specifies a filename fails,
1499 * we treat that as permanent, and remove the source
1500 * from the GtkIconSet. (in ensure_filename_pixbuf ()
1501 * B) If loading a themed icon fails, or scaling an icon
1502 * fails, we treat that as transient and will try
1503 * again next time the icon falls out of the cache
1504 * and we need to recreate it.
1506 while (pixbuf == NULL)
1508 GtkIconSource *source = find_best_matching_source (icon_set, direction, state, size, failed);
1513 switch (source->type)
1515 case GTK_ICON_SOURCE_FILENAME:
1516 if (!ensure_filename_pixbuf (icon_set, source))
1519 case GTK_ICON_SOURCE_PIXBUF:
1520 pixbuf = gtk_render_icon_pixbuf (context, source, size);
1523 g_warning ("Failed to render icon");
1524 failed = g_slist_prepend (failed, source);
1527 case GTK_ICON_SOURCE_ICON_NAME:
1528 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1529 pixbuf = render_icon_name_pixbuf (source, context, size);
1531 failed = g_slist_prepend (failed, source);
1533 case GTK_ICON_SOURCE_EMPTY:
1534 g_assert_not_reached ();
1538 g_slist_free (failed);
1543 extern GtkIconCache *_builtin_cache;
1546 render_fallback_image (GtkStyleContext *context,
1547 GtkTextDirection direction,
1551 /* This icon can be used for any direction/state/size */
1552 static GtkIconSource fallback_source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1554 if (fallback_source.type == GTK_ICON_SOURCE_EMPTY)
1559 _gtk_icon_theme_ensure_builtin_cache ();
1561 index = _gtk_icon_cache_get_directory_index (_builtin_cache, "24");
1562 pixbuf = _gtk_icon_cache_get_icon (_builtin_cache,
1563 GTK_STOCK_MISSING_IMAGE,
1565 gtk_icon_source_set_pixbuf (&fallback_source, pixbuf);
1566 g_object_unref (pixbuf);
1569 return gtk_render_icon_pixbuf (context, &fallback_source, size);
1573 * gtk_icon_set_render_icon_pixbuf:
1574 * @icon_set: a #GtkIconSet
1575 * @context: a #GtkStyleContext
1576 * @size: (type int): icon size. A size of (GtkIconSize)-1
1577 * means render at the size of the source and don't scale.
1579 * Renders an icon using gtk_render_icon_pixbuf(). In most cases,
1580 * gtk_widget_render_icon_pixbuf() is better, since it automatically provides
1581 * most of the arguments from the current widget settings. This
1582 * function never returns %NULL; if the icon can't be rendered
1583 * (perhaps because an image file fails to load), a default "missing
1584 * image" icon will be returned instead.
1586 * Return value: (transfer full): a #GdkPixbuf to be displayed
1591 gtk_icon_set_render_icon_pixbuf (GtkIconSet *icon_set,
1592 GtkStyleContext *context,
1595 GdkPixbuf *icon = NULL;
1596 GtkStateFlags flags = 0;
1598 GtkTextDirection direction;
1600 g_return_val_if_fail (icon_set != NULL, NULL);
1601 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1603 flags = gtk_style_context_get_state (context);
1604 if (flags & GTK_STATE_FLAG_INSENSITIVE)
1605 state = GTK_STATE_INSENSITIVE;
1606 else if (flags & GTK_STATE_FLAG_PRELIGHT)
1607 state = GTK_STATE_PRELIGHT;
1609 state = GTK_STATE_NORMAL;
1611 direction = gtk_style_context_get_direction (context);
1613 if (icon_set->sources)
1615 icon = find_in_cache (icon_set, context, direction, state, size);
1618 g_object_ref (icon);
1623 if (icon_set->sources)
1624 icon = find_and_render_icon_source (icon_set, context, direction, state, size);
1627 icon = render_fallback_image (context, direction, state, size);
1629 add_to_cache (icon_set, context, direction, state, size, icon);
1635 * gtk_icon_set_render_icon:
1636 * @icon_set: a #GtkIconSet
1637 * @style: (allow-none): a #GtkStyle associated with @widget, or %NULL
1638 * @direction: text direction
1639 * @state: widget state
1640 * @size: (type int): icon size. A size of (GtkIconSize)-1
1641 * means render at the size of the source and don't scale.
1642 * @widget: (allow-none): widget that will display the icon, or %NULL.
1643 * The only use that is typically made of this
1644 * is to determine the appropriate #GdkScreen.
1645 * @detail: (allow-none): detail to pass to the theme engine, or %NULL.
1646 * Note that passing a detail of anything but %NULL
1647 * will disable caching.
1649 * Renders an icon using gtk_style_render_icon(). In most cases,
1650 * gtk_widget_render_icon() is better, since it automatically provides
1651 * most of the arguments from the current widget settings. This
1652 * function never returns %NULL; if the icon can't be rendered
1653 * (perhaps because an image file fails to load), a default "missing
1654 * image" icon will be returned instead.
1656 * Return value: (transfer full): a #GdkPixbuf to be displayed
1658 * Deprecated: 3.0: Use gtk_icon_set_render_icon_pixbuf() instead
1661 gtk_icon_set_render_icon (GtkIconSet *icon_set,
1663 GtkTextDirection direction,
1670 GtkStyleContext *context = NULL;
1671 GtkStateFlags flags = 0;
1673 g_return_val_if_fail (icon_set != NULL, NULL);
1674 g_return_val_if_fail (style == NULL || GTK_IS_STYLE (style), NULL);
1676 if (style && gtk_style_has_context (style))
1678 g_object_get (style, "context", &context, NULL);
1679 /* g_object_get returns a refed object */
1681 g_object_unref (context);
1685 context = gtk_widget_get_style_context (widget);
1689 return render_fallback_image (context, direction, state, size);
1691 gtk_style_context_save (context);
1695 case GTK_STATE_PRELIGHT:
1696 flags |= GTK_STATE_FLAG_PRELIGHT;
1698 case GTK_STATE_INSENSITIVE:
1699 flags |= GTK_STATE_FLAG_INSENSITIVE;
1705 gtk_style_context_set_state (context, flags);
1706 gtk_style_context_set_direction (context, direction);
1708 icon = gtk_icon_set_render_icon_pixbuf (icon_set, context, size);
1710 gtk_style_context_restore (context);
1715 /* Order sources by their "wildness", so that "wilder" sources are
1716 * greater than "specific" sources; for determining ordering,
1717 * direction beats state beats size.
1721 icon_source_compare (gconstpointer ap, gconstpointer bp)
1723 const GtkIconSource *a = ap;
1724 const GtkIconSource *b = bp;
1726 if (!a->any_direction && b->any_direction)
1728 else if (a->any_direction && !b->any_direction)
1730 else if (!a->any_state && b->any_state)
1732 else if (a->any_state && !b->any_state)
1734 else if (!a->any_size && b->any_size)
1736 else if (a->any_size && !b->any_size)
1743 * gtk_icon_set_add_source:
1744 * @icon_set: a #GtkIconSet
1745 * @source: a #GtkIconSource
1747 * Icon sets have a list of #GtkIconSource, which they use as base
1748 * icons for rendering icons in different states and sizes. Icons are
1749 * scaled, made to look insensitive, etc. in
1750 * gtk_icon_set_render_icon(), but #GtkIconSet needs base images to
1751 * work with. The base images and when to use them are described by
1754 * This function copies @source, so you can reuse the same source immediately
1755 * without affecting the icon set.
1757 * An example of when you'd use this function: a web browser's "Back
1758 * to Previous Page" icon might point in a different direction in
1759 * Hebrew and in English; it might look different when insensitive;
1760 * and it might change size depending on toolbar mode (small/large
1761 * icons). So a single icon set would contain all those variants of
1762 * the icon, and you might add a separate source for each one.
1764 * You should nearly always add a "default" icon source with all
1765 * fields wildcarded, which will be used as a fallback if no more
1766 * specific source matches. #GtkIconSet always prefers more specific
1767 * icon sources to more generic icon sources. The order in which you
1768 * add the sources to the icon set does not matter.
1770 * gtk_icon_set_new_from_pixbuf() creates a new icon set with a
1771 * default icon source based on the given pixbuf.
1774 gtk_icon_set_add_source (GtkIconSet *icon_set,
1775 const GtkIconSource *source)
1777 g_return_if_fail (icon_set != NULL);
1778 g_return_if_fail (source != NULL);
1780 if (source->type == GTK_ICON_SOURCE_EMPTY)
1782 g_warning ("Useless empty GtkIconSource");
1786 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
1787 gtk_icon_source_copy (source),
1788 icon_source_compare);
1792 * gtk_icon_set_get_sizes:
1793 * @icon_set: a #GtkIconSet
1794 * @sizes: (array length=n_sizes) (out) (type int): return location
1795 * for array of sizes
1796 * @n_sizes: location to store number of elements in returned array
1798 * Obtains a list of icon sizes this icon set can render. The returned
1799 * array must be freed with g_free().
1802 gtk_icon_set_get_sizes (GtkIconSet *icon_set,
1803 GtkIconSize **sizes,
1807 gboolean all_sizes = FALSE;
1808 GSList *specifics = NULL;
1810 g_return_if_fail (icon_set != NULL);
1811 g_return_if_fail (sizes != NULL);
1812 g_return_if_fail (n_sizes != NULL);
1814 tmp_list = icon_set->sources;
1815 while (tmp_list != NULL)
1817 GtkIconSource *source;
1819 source = tmp_list->data;
1821 if (source->any_size)
1827 specifics = g_slist_prepend (specifics, GINT_TO_POINTER (source->size));
1829 tmp_list = g_slist_next (tmp_list);
1834 /* Need to find out what sizes exist */
1839 *sizes = g_new (GtkIconSize, icon_sizes_used);
1840 *n_sizes = icon_sizes_used - 1;
1843 while (i < icon_sizes_used)
1845 (*sizes)[i - 1] = icon_sizes[i].size;
1853 *n_sizes = g_slist_length (specifics);
1854 *sizes = g_new (GtkIconSize, *n_sizes);
1857 tmp_list = specifics;
1858 while (tmp_list != NULL)
1860 (*sizes)[i] = GPOINTER_TO_INT (tmp_list->data);
1863 tmp_list = g_slist_next (tmp_list);
1867 g_slist_free (specifics);
1872 * gtk_icon_source_new:
1874 * Creates a new #GtkIconSource. A #GtkIconSource contains a #GdkPixbuf (or
1875 * image filename) that serves as the base image for one or more of the
1876 * icons in a #GtkIconSet, along with a specification for which icons in the
1877 * icon set will be based on that pixbuf or image file. An icon set contains
1878 * a set of icons that represent "the same" logical concept in different states,
1879 * different global text directions, and different sizes.
1881 * So for example a web browser's "Back to Previous Page" icon might
1882 * point in a different direction in Hebrew and in English; it might
1883 * look different when insensitive; and it might change size depending
1884 * on toolbar mode (small/large icons). So a single icon set would
1885 * contain all those variants of the icon. #GtkIconSet contains a list
1886 * of #GtkIconSource from which it can derive specific icon variants in
1889 * In the simplest case, #GtkIconSet contains one source pixbuf from
1890 * which it derives all variants. The convenience function
1891 * gtk_icon_set_new_from_pixbuf() handles this case; if you only have
1892 * one source pixbuf, just use that function.
1894 * If you want to use a different base pixbuf for different icon
1895 * variants, you create multiple icon sources, mark which variants
1896 * they'll be used to create, and add them to the icon set with
1897 * gtk_icon_set_add_source().
1899 * By default, the icon source has all parameters wildcarded. That is,
1900 * the icon source will be used as the base icon for any desired text
1901 * direction, widget state, or icon size.
1903 * Return value: a new #GtkIconSource
1906 gtk_icon_source_new (void)
1910 src = g_new0 (GtkIconSource, 1);
1912 src->direction = GTK_TEXT_DIR_NONE;
1913 src->size = GTK_ICON_SIZE_INVALID;
1914 src->state = GTK_STATE_NORMAL;
1916 src->any_direction = TRUE;
1917 src->any_state = TRUE;
1918 src->any_size = TRUE;
1924 * gtk_icon_source_copy:
1925 * @source: a #GtkIconSource
1927 * Creates a copy of @source; mostly useful for language bindings.
1929 * Return value: a new #GtkIconSource
1932 gtk_icon_source_copy (const GtkIconSource *source)
1934 GtkIconSource *copy;
1936 g_return_val_if_fail (source != NULL, NULL);
1938 copy = g_new (GtkIconSource, 1);
1944 case GTK_ICON_SOURCE_EMPTY:
1945 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1947 case GTK_ICON_SOURCE_ICON_NAME:
1948 copy->source.icon_name = g_strdup (copy->source.icon_name);
1950 case GTK_ICON_SOURCE_FILENAME:
1951 copy->source.filename = g_strdup (copy->source.filename);
1952 if (copy->filename_pixbuf)
1953 g_object_ref (copy->filename_pixbuf);
1955 case GTK_ICON_SOURCE_PIXBUF:
1956 g_object_ref (copy->source.pixbuf);
1959 g_assert_not_reached();
1966 * gtk_icon_source_free:
1967 * @source: a #GtkIconSource
1969 * Frees a dynamically-allocated icon source, along with its
1970 * filename, size, and pixbuf fields if those are not %NULL.
1973 gtk_icon_source_free (GtkIconSource *source)
1975 g_return_if_fail (source != NULL);
1977 icon_source_clear (source);
1981 G_DEFINE_BOXED_TYPE (GtkIconSource, gtk_icon_source,
1982 gtk_icon_source_copy,
1983 gtk_icon_source_free)
1986 icon_source_clear (GtkIconSource *source)
1988 switch (source->type)
1990 case GTK_ICON_SOURCE_EMPTY:
1992 case GTK_ICON_SOURCE_ICON_NAME:
1993 g_free (source->source.icon_name);
1995 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1996 source->source.icon_name = NULL;
1998 case GTK_ICON_SOURCE_FILENAME:
1999 g_free (source->source.filename);
2000 source->source.filename = NULL;
2001 if (source->filename_pixbuf)
2002 g_object_unref (source->filename_pixbuf);
2003 source->filename_pixbuf = NULL;
2005 case GTK_ICON_SOURCE_PIXBUF:
2006 g_object_unref (source->source.pixbuf);
2007 source->source.pixbuf = NULL;
2010 g_assert_not_reached();
2013 source->type = GTK_ICON_SOURCE_EMPTY;
2017 * gtk_icon_source_set_filename:
2018 * @source: a #GtkIconSource
2019 * @filename: image file to use
2021 * Sets the name of an image file to use as a base image when creating
2022 * icon variants for #GtkIconSet. The filename must be absolute.
2025 gtk_icon_source_set_filename (GtkIconSource *source,
2026 const gchar *filename)
2028 g_return_if_fail (source != NULL);
2029 g_return_if_fail (filename == NULL || g_path_is_absolute (filename));
2031 if (source->type == GTK_ICON_SOURCE_FILENAME &&
2032 source->source.filename == filename)
2035 icon_source_clear (source);
2037 if (filename != NULL)
2039 source->type = GTK_ICON_SOURCE_FILENAME;
2040 source->source.filename = g_strdup (filename);
2045 * gtk_icon_source_set_icon_name
2046 * @source: a #GtkIconSource
2047 * @icon_name: (allow-none): name of icon to use
2049 * Sets the name of an icon to look up in the current icon theme
2050 * to use as a base image when creating icon variants for #GtkIconSet.
2053 gtk_icon_source_set_icon_name (GtkIconSource *source,
2054 const gchar *icon_name)
2056 g_return_if_fail (source != NULL);
2058 if (source->type == GTK_ICON_SOURCE_ICON_NAME &&
2059 source->source.icon_name == icon_name)
2062 icon_source_clear (source);
2064 if (icon_name != NULL)
2066 source->type = GTK_ICON_SOURCE_ICON_NAME;
2067 source->source.icon_name = g_strdup (icon_name);
2072 * gtk_icon_source_set_pixbuf:
2073 * @source: a #GtkIconSource
2074 * @pixbuf: pixbuf to use as a source
2076 * Sets a pixbuf to use as a base image when creating icon variants
2080 gtk_icon_source_set_pixbuf (GtkIconSource *source,
2083 g_return_if_fail (source != NULL);
2084 g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
2086 if (source->type == GTK_ICON_SOURCE_PIXBUF &&
2087 source->source.pixbuf == pixbuf)
2090 icon_source_clear (source);
2094 source->type = GTK_ICON_SOURCE_PIXBUF;
2095 source->source.pixbuf = g_object_ref (pixbuf);
2100 * gtk_icon_source_get_filename:
2101 * @source: a #GtkIconSource
2103 * Retrieves the source filename, or %NULL if none is set. The
2104 * filename is not a copy, and should not be modified or expected to
2105 * persist beyond the lifetime of the icon source.
2107 * Return value: image filename. This string must not be modified
2110 G_CONST_RETURN gchar*
2111 gtk_icon_source_get_filename (const GtkIconSource *source)
2113 g_return_val_if_fail (source != NULL, NULL);
2115 if (source->type == GTK_ICON_SOURCE_FILENAME)
2116 return source->source.filename;
2122 * gtk_icon_source_get_icon_name:
2123 * @source: a #GtkIconSource
2125 * Retrieves the source icon name, or %NULL if none is set. The
2126 * icon_name is not a copy, and should not be modified or expected to
2127 * persist beyond the lifetime of the icon source.
2129 * Return value: icon name. This string must not be modified or freed.
2131 G_CONST_RETURN gchar*
2132 gtk_icon_source_get_icon_name (const GtkIconSource *source)
2134 g_return_val_if_fail (source != NULL, NULL);
2136 if (source->type == GTK_ICON_SOURCE_ICON_NAME ||
2137 source->type == GTK_ICON_SOURCE_STATIC_ICON_NAME)
2138 return source->source.icon_name;
2144 * gtk_icon_source_get_pixbuf:
2145 * @source: a #GtkIconSource
2147 * Retrieves the source pixbuf, or %NULL if none is set.
2148 * In addition, if a filename source is in use, this
2149 * function in some cases will return the pixbuf from
2150 * loaded from the filename. This is, for example, true
2151 * for the GtkIconSource passed to the GtkStyle::render_icon()
2152 * virtual function. The reference count on the pixbuf is
2155 * Return value: (transfer none): source pixbuf
2158 gtk_icon_source_get_pixbuf (const GtkIconSource *source)
2160 g_return_val_if_fail (source != NULL, NULL);
2162 if (source->type == GTK_ICON_SOURCE_PIXBUF)
2163 return source->source.pixbuf;
2164 else if (source->type == GTK_ICON_SOURCE_FILENAME)
2165 return source->filename_pixbuf;
2171 * gtk_icon_source_set_direction_wildcarded:
2172 * @source: a #GtkIconSource
2173 * @setting: %TRUE to wildcard the text direction
2175 * If the text direction is wildcarded, this source can be used
2176 * as the base image for an icon in any #GtkTextDirection.
2177 * If the text direction is not wildcarded, then the
2178 * text direction the icon source applies to should be set
2179 * with gtk_icon_source_set_direction(), and the icon source
2180 * will only be used with that text direction.
2182 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2183 * wildcarded sources, and will use an exact match when possible.
2186 gtk_icon_source_set_direction_wildcarded (GtkIconSource *source,
2189 g_return_if_fail (source != NULL);
2191 source->any_direction = setting != FALSE;
2195 * gtk_icon_source_set_state_wildcarded:
2196 * @source: a #GtkIconSource
2197 * @setting: %TRUE to wildcard the widget state
2199 * If the widget state is wildcarded, this source can be used as the
2200 * base image for an icon in any #GtkStateType. If the widget state
2201 * is not wildcarded, then the state the source applies to should be
2202 * set with gtk_icon_source_set_state() and the icon source will
2203 * only be used with that specific state.
2205 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2206 * wildcarded sources, and will use an exact match when possible.
2208 * #GtkIconSet will normally transform wildcarded source images to
2209 * produce an appropriate icon for a given state, for example
2210 * lightening an image on prelight, but will not modify source images
2211 * that match exactly.
2214 gtk_icon_source_set_state_wildcarded (GtkIconSource *source,
2217 g_return_if_fail (source != NULL);
2219 source->any_state = setting != FALSE;
2224 * gtk_icon_source_set_size_wildcarded:
2225 * @source: a #GtkIconSource
2226 * @setting: %TRUE to wildcard the widget state
2228 * If the icon size is wildcarded, this source can be used as the base
2229 * image for an icon of any size. If the size is not wildcarded, then
2230 * the size the source applies to should be set with
2231 * gtk_icon_source_set_size() and the icon source will only be used
2232 * with that specific size.
2234 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2235 * wildcarded sources, and will use an exact match when possible.
2237 * #GtkIconSet will normally scale wildcarded source images to produce
2238 * an appropriate icon at a given size, but will not change the size
2239 * of source images that match exactly.
2242 gtk_icon_source_set_size_wildcarded (GtkIconSource *source,
2245 g_return_if_fail (source != NULL);
2247 source->any_size = setting != FALSE;
2251 * gtk_icon_source_get_size_wildcarded:
2252 * @source: a #GtkIconSource
2254 * Gets the value set by gtk_icon_source_set_size_wildcarded().
2256 * Return value: %TRUE if this icon source is a base for any icon size variant
2259 gtk_icon_source_get_size_wildcarded (const GtkIconSource *source)
2261 g_return_val_if_fail (source != NULL, TRUE);
2263 return source->any_size;
2267 * gtk_icon_source_get_state_wildcarded:
2268 * @source: a #GtkIconSource
2270 * Gets the value set by gtk_icon_source_set_state_wildcarded().
2272 * Return value: %TRUE if this icon source is a base for any widget state variant
2275 gtk_icon_source_get_state_wildcarded (const GtkIconSource *source)
2277 g_return_val_if_fail (source != NULL, TRUE);
2279 return source->any_state;
2283 * gtk_icon_source_get_direction_wildcarded:
2284 * @source: a #GtkIconSource
2286 * Gets the value set by gtk_icon_source_set_direction_wildcarded().
2288 * Return value: %TRUE if this icon source is a base for any text direction variant
2291 gtk_icon_source_get_direction_wildcarded (const GtkIconSource *source)
2293 g_return_val_if_fail (source != NULL, TRUE);
2295 return source->any_direction;
2299 * gtk_icon_source_set_direction:
2300 * @source: a #GtkIconSource
2301 * @direction: text direction this source applies to
2303 * Sets the text direction this icon source is intended to be used
2306 * Setting the text direction on an icon source makes no difference
2307 * if the text direction is wildcarded. Therefore, you should usually
2308 * call gtk_icon_source_set_direction_wildcarded() to un-wildcard it
2309 * in addition to calling this function.
2312 gtk_icon_source_set_direction (GtkIconSource *source,
2313 GtkTextDirection direction)
2315 g_return_if_fail (source != NULL);
2317 source->direction = direction;
2321 * gtk_icon_source_set_state:
2322 * @source: a #GtkIconSource
2323 * @state: widget state this source applies to
2325 * Sets the widget state this icon source is intended to be used
2328 * Setting the widget state on an icon source makes no difference
2329 * if the state is wildcarded. Therefore, you should usually
2330 * call gtk_icon_source_set_state_wildcarded() to un-wildcard it
2331 * in addition to calling this function.
2334 gtk_icon_source_set_state (GtkIconSource *source,
2337 g_return_if_fail (source != NULL);
2339 source->state = state;
2343 * gtk_icon_source_set_size:
2344 * @source: a #GtkIconSource
2345 * @size: (type int): icon size this source applies to
2347 * Sets the icon size this icon source is intended to be used
2350 * Setting the icon size on an icon source makes no difference
2351 * if the size is wildcarded. Therefore, you should usually
2352 * call gtk_icon_source_set_size_wildcarded() to un-wildcard it
2353 * in addition to calling this function.
2356 gtk_icon_source_set_size (GtkIconSource *source,
2359 g_return_if_fail (source != NULL);
2361 source->size = size;
2365 * gtk_icon_source_get_direction:
2366 * @source: a #GtkIconSource
2368 * Obtains the text direction this icon source applies to. The return
2369 * value is only useful/meaningful if the text direction is <emphasis>not</emphasis>
2372 * Return value: text direction this source matches
2375 gtk_icon_source_get_direction (const GtkIconSource *source)
2377 g_return_val_if_fail (source != NULL, 0);
2379 return source->direction;
2383 * gtk_icon_source_get_state:
2384 * @source: a #GtkIconSource
2386 * Obtains the widget state this icon source applies to. The return
2387 * value is only useful/meaningful if the widget state is <emphasis>not</emphasis>
2390 * Return value: widget state this source matches
2393 gtk_icon_source_get_state (const GtkIconSource *source)
2395 g_return_val_if_fail (source != NULL, 0);
2397 return source->state;
2401 * gtk_icon_source_get_size:
2402 * @source: a #GtkIconSource
2404 * Obtains the icon size this source applies to. The return value
2405 * is only useful/meaningful if the icon size is <emphasis>not</emphasis> wildcarded.
2407 * Return value: (type int): icon size this source matches.
2410 gtk_icon_source_get_size (const GtkIconSource *source)
2412 g_return_val_if_fail (source != NULL, 0);
2414 return source->size;
2417 #define NUM_CACHED_ICONS 8
2419 typedef struct _CachedIcon CachedIcon;
2423 /* These must all match to use the cached pixbuf.
2424 * If any don't match, we must re-render the pixbuf.
2426 GtkStyleContext *style;
2427 GtkTextDirection direction;
2435 ensure_cache_up_to_date (GtkIconSet *icon_set)
2437 if (icon_set->cache_serial != cache_serial)
2439 clear_cache (icon_set, TRUE);
2440 icon_set->cache_serial = cache_serial;
2445 cached_icon_free (CachedIcon *icon)
2447 g_object_unref (icon->pixbuf);
2448 g_object_unref (icon->style);
2454 find_in_cache (GtkIconSet *icon_set,
2455 GtkStyleContext *style_context,
2456 GtkTextDirection direction,
2463 ensure_cache_up_to_date (icon_set);
2466 tmp_list = icon_set->cache;
2467 while (tmp_list != NULL)
2469 CachedIcon *icon = tmp_list->data;
2471 if (icon->style == style_context &&
2472 icon->direction == direction &&
2473 icon->state == state &&
2474 (size == (GtkIconSize)-1 || icon->size == size))
2478 /* Move this icon to the front of the list. */
2479 prev->next = tmp_list->next;
2480 tmp_list->next = icon_set->cache;
2481 icon_set->cache = tmp_list;
2484 return icon->pixbuf;
2488 tmp_list = g_slist_next (tmp_list);
2495 add_to_cache (GtkIconSet *icon_set,
2496 GtkStyleContext *style_context,
2497 GtkTextDirection direction,
2504 ensure_cache_up_to_date (icon_set);
2506 g_object_ref (pixbuf);
2508 icon = g_new (CachedIcon, 1);
2509 icon_set->cache = g_slist_prepend (icon_set->cache, icon);
2510 icon_set->cache_size++;
2512 icon->style = g_object_ref (style_context);
2513 icon->direction = direction;
2514 icon->state = state;
2516 icon->pixbuf = pixbuf;
2517 attach_to_style (icon_set, icon->style);
2519 if (icon_set->cache_size >= NUM_CACHED_ICONS)
2521 /* Remove oldest item in the cache */
2524 tmp_list = icon_set->cache;
2526 /* Find next-to-last link */
2527 g_assert (NUM_CACHED_ICONS > 2);
2528 while (tmp_list->next->next)
2529 tmp_list = tmp_list->next;
2531 g_assert (tmp_list != NULL);
2532 g_assert (tmp_list->next != NULL);
2533 g_assert (tmp_list->next->next == NULL);
2535 /* Free the last icon */
2536 icon = tmp_list->next->data;
2538 g_slist_free (tmp_list->next);
2539 tmp_list->next = NULL;
2541 cached_icon_free (icon);
2546 clear_cache (GtkIconSet *icon_set,
2547 gboolean style_detach)
2549 GSList *cache, *tmp_list;
2550 GtkStyleContext *last_style = NULL;
2552 cache = icon_set->cache;
2553 icon_set->cache = NULL;
2554 icon_set->cache_size = 0;
2556 while (tmp_list != NULL)
2558 CachedIcon *icon = tmp_list->data;
2562 /* simple optimization for the case where the cache
2563 * contains contiguous icons from the same style.
2564 * it's safe to call detach_from_style more than
2565 * once on the same style though.
2567 if (last_style != icon->style)
2569 detach_from_style (icon_set, icon->style);
2570 last_style = icon->style;
2574 cached_icon_free (icon);
2576 tmp_list = g_slist_next (tmp_list);
2579 g_slist_free (cache);
2583 copy_cache (GtkIconSet *icon_set,
2584 GtkIconSet *copy_recipient)
2587 GSList *copy = NULL;
2589 ensure_cache_up_to_date (icon_set);
2591 tmp_list = icon_set->cache;
2592 while (tmp_list != NULL)
2594 CachedIcon *icon = tmp_list->data;
2595 CachedIcon *icon_copy = g_new (CachedIcon, 1);
2599 attach_to_style (copy_recipient, icon_copy->style);
2600 g_object_ref (icon_copy->style);
2602 g_object_ref (icon_copy->pixbuf);
2604 icon_copy->size = icon->size;
2606 copy = g_slist_prepend (copy, icon_copy);
2608 tmp_list = g_slist_next (tmp_list);
2611 return g_slist_reverse (copy);
2615 attach_to_style (GtkIconSet *icon_set,
2616 GtkStyleContext *style_context)
2620 table = g_object_get_qdata (G_OBJECT (style_context),
2621 g_quark_try_string ("gtk-style-icon-sets"));
2625 table = g_hash_table_new (NULL, NULL);
2626 g_object_set_qdata_full (G_OBJECT (style_context),
2627 g_quark_from_static_string ("gtk-style-icon-sets"),
2632 g_hash_table_insert (table, icon_set, icon_set);
2636 detach_from_style (GtkIconSet *icon_set,
2637 GtkStyleContext *style_context)
2641 table = g_object_get_qdata (G_OBJECT (style_context),
2642 g_quark_try_string ("gtk-style-icon-sets"));
2645 g_hash_table_remove (table, icon_set);
2649 iconsets_foreach (gpointer key,
2653 GtkIconSet *icon_set = key;
2655 /* We only need to remove cache entries for the given style;
2656 * but that complicates things because in destroy notify
2657 * we don't know which style got destroyed, and 95% of the
2658 * time all cache entries will have the same style,
2659 * so this is faster anyway.
2662 clear_cache (icon_set, FALSE);
2666 style_dnotify (gpointer data)
2668 GHashTable *table = data;
2670 g_hash_table_foreach (table, iconsets_foreach, NULL);
2672 g_hash_table_destroy (table);
2675 /* This allows the icon set to detect that its cache is out of date. */
2677 _gtk_icon_set_invalidate_caches (void)
2683 * _gtk_icon_factory_list_ids:
2685 * Gets all known IDs stored in an existing icon factory.
2686 * The strings in the returned list aren't copied.
2687 * The list itself should be freed.
2689 * Return value: List of ids in icon factories
2692 _gtk_icon_factory_list_ids (void)
2699 _gtk_icon_factory_ensure_default_icons ();
2701 tmp_list = all_icon_factories;
2702 while (tmp_list != NULL)
2705 GtkIconFactory *factory = GTK_ICON_FACTORY (tmp_list->data);
2706 GtkIconFactoryPrivate *priv = factory->priv;
2708 these_ids = g_hash_table_get_keys (priv->icons);
2710 ids = g_list_concat (ids, these_ids);
2712 tmp_list = g_slist_next (tmp_list);
2722 } IconFactoryParserData;
2728 GtkTextDirection direction;
2731 } IconSourceParserData;
2734 icon_source_start_element (GMarkupParseContext *context,
2735 const gchar *element_name,
2736 const gchar **names,
2737 const gchar **values,
2742 gchar *stock_id = NULL;
2743 gchar *filename = NULL;
2744 gchar *icon_name = NULL;
2746 gint direction = -1;
2748 IconFactoryParserData *parser_data;
2749 IconSourceParserData *source_data;
2751 GQuark error_domain;
2753 parser_data = (IconFactoryParserData*)user_data;
2755 if (!parser_data->in_source)
2757 if (strcmp (element_name, "sources") != 0)
2759 error_msg = g_strdup_printf ("Unexpected element %s, expected <sources>", element_name);
2760 error_domain = GTK_BUILDER_ERROR_INVALID_TAG;
2763 parser_data->in_source = TRUE;
2768 if (strcmp (element_name, "source") != 0)
2770 error_msg = g_strdup_printf ("Unexpected element %s, expected <source>", element_name);
2771 error_domain = GTK_BUILDER_ERROR_INVALID_TAG;
2776 for (i = 0; names[i]; i++)
2778 if (strcmp (names[i], "stock-id") == 0)
2779 stock_id = g_strdup (values[i]);
2780 else if (strcmp (names[i], "filename") == 0)
2781 filename = g_strdup (values[i]);
2782 else if (strcmp (names[i], "icon-name") == 0)
2783 icon_name = g_strdup (values[i]);
2784 else if (strcmp (names[i], "size") == 0)
2786 if (!_gtk_builder_enum_from_string (GTK_TYPE_ICON_SIZE,
2792 else if (strcmp (names[i], "direction") == 0)
2794 if (!_gtk_builder_enum_from_string (GTK_TYPE_TEXT_DIRECTION,
2800 else if (strcmp (names[i], "state") == 0)
2802 if (!_gtk_builder_enum_from_string (GTK_TYPE_STATE_TYPE,
2810 error_msg = g_strdup_printf ("'%s' is not a valid attribute of <%s>",
2811 names[i], "source");
2812 error_domain = GTK_BUILDER_ERROR_INVALID_ATTRIBUTE;
2819 error_msg = g_strdup_printf ("<source> requires a stock_id");
2820 error_domain = GTK_BUILDER_ERROR_MISSING_ATTRIBUTE;
2824 source_data = g_slice_new (IconSourceParserData);
2825 source_data->stock_id = stock_id;
2826 source_data->filename = filename;
2827 source_data->icon_name = icon_name;
2828 source_data->size = size;
2829 source_data->direction = direction;
2830 source_data->state = state;
2832 parser_data->sources = g_slist_prepend (parser_data->sources, source_data);
2838 gint line_number, char_number;
2840 g_markup_parse_context_get_position (context,
2844 tmp = g_strdup_printf ("%s:%d:%d %s", "input",
2845 line_number, char_number, error_msg);
2847 g_set_error_literal (error,
2852 g_warning ("%s", tmp);
2862 static const GMarkupParser icon_source_parser =
2864 icon_source_start_element,
2868 gtk_icon_factory_buildable_custom_tag_start (GtkBuildable *buildable,
2869 GtkBuilder *builder,
2871 const gchar *tagname,
2872 GMarkupParser *parser,
2875 g_assert (buildable);
2877 if (strcmp (tagname, "sources") == 0)
2879 IconFactoryParserData *parser_data;
2881 parser_data = g_slice_new0 (IconFactoryParserData);
2882 *parser = icon_source_parser;
2883 *data = parser_data;
2890 gtk_icon_factory_buildable_custom_tag_end (GtkBuildable *buildable,
2891 GtkBuilder *builder,
2893 const gchar *tagname,
2894 gpointer *user_data)
2896 GtkIconFactory *icon_factory;
2898 icon_factory = GTK_ICON_FACTORY (buildable);
2900 if (strcmp (tagname, "sources") == 0)
2902 IconFactoryParserData *parser_data;
2903 GtkIconSource *icon_source;
2904 GtkIconSet *icon_set;
2907 parser_data = (IconFactoryParserData*)user_data;
2909 for (l = parser_data->sources; l; l = l->next)
2911 IconSourceParserData *source_data = l->data;
2913 icon_set = gtk_icon_factory_lookup (icon_factory, source_data->stock_id);
2916 icon_set = gtk_icon_set_new ();
2917 gtk_icon_factory_add (icon_factory, source_data->stock_id, icon_set);
2918 gtk_icon_set_unref (icon_set);
2921 icon_source = gtk_icon_source_new ();
2923 if (source_data->filename)
2926 filename = _gtk_builder_get_absolute_filename (builder, source_data->filename);
2927 gtk_icon_source_set_filename (icon_source, filename);
2930 if (source_data->icon_name)
2931 gtk_icon_source_set_icon_name (icon_source, source_data->icon_name);
2932 if (source_data->size != -1)
2934 gtk_icon_source_set_size (icon_source, source_data->size);
2935 gtk_icon_source_set_size_wildcarded (icon_source, FALSE);
2937 if (source_data->direction != -1)
2939 gtk_icon_source_set_direction (icon_source, source_data->direction);
2940 gtk_icon_source_set_direction_wildcarded (icon_source, FALSE);
2942 if (source_data->state != -1)
2944 gtk_icon_source_set_state (icon_source, source_data->state);
2945 gtk_icon_source_set_state_wildcarded (icon_source, FALSE);
2948 /* Inline source_add() to avoid creating a copy */
2949 g_assert (icon_source->type != GTK_ICON_SOURCE_EMPTY);
2950 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
2952 icon_source_compare);
2954 g_free (source_data->stock_id);
2955 g_free (source_data->filename);
2956 g_free (source_data->icon_name);
2957 g_slice_free (IconSourceParserData, source_data);
2959 g_slist_free (parser_data->sources);
2960 g_slice_free (IconFactoryParserData, parser_data);
2962 /* TODO: Add an attribute/tag to prevent this.
2963 * Usually it's the right thing to do though.
2965 gtk_icon_factory_add_default (icon_factory);