1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2000 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkiconfactory.h"
32 #include "gtkiconcache.h"
34 #include "gtkicontheme.h"
35 #include "gtksettings.h"
37 #include "gtkwidget.h"
39 #include "gtkbuildable.h"
40 #include "gtkbuilderprivate.h"
43 static GSList *all_icon_factories = NULL;
45 struct _GtkIconFactoryPriv
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;
82 #if defined (G_OS_WIN32) && !defined (_WIN64)
83 /* System codepage version of filename, for DLL ABI backward
84 * compatibility functions.
92 gtk_icon_factory_buildable_init (GtkBuildableIface *iface);
94 static gboolean gtk_icon_factory_buildable_custom_tag_start (GtkBuildable *buildable,
98 GMarkupParser *parser,
100 static void gtk_icon_factory_buildable_custom_tag_end (GtkBuildable *buildable,
103 const gchar *tagname,
104 gpointer *user_data);
105 static void gtk_icon_factory_finalize (GObject *object);
106 static void get_default_icons (GtkIconFactory *icon_factory);
107 static void icon_source_clear (GtkIconSource *source);
109 static GtkIconSize icon_size_register_intern (const gchar *name,
113 #define GTK_ICON_SOURCE_INIT(any_direction, any_state, any_size) \
114 { GTK_ICON_SOURCE_EMPTY, { NULL }, NULL, \
116 any_direction, any_state, any_size }
118 G_DEFINE_TYPE_WITH_CODE (GtkIconFactory, gtk_icon_factory, G_TYPE_OBJECT,
119 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
120 gtk_icon_factory_buildable_init))
123 gtk_icon_factory_init (GtkIconFactory *factory)
125 GtkIconFactoryPriv *priv;
127 factory->priv = G_TYPE_INSTANCE_GET_PRIVATE (factory,
128 GTK_TYPE_ICON_FACTORY,
130 priv = factory->priv;
132 priv->icons = g_hash_table_new (g_str_hash, g_str_equal);
133 all_icon_factories = g_slist_prepend (all_icon_factories, factory);
137 gtk_icon_factory_class_init (GtkIconFactoryClass *klass)
139 GObjectClass *object_class = G_OBJECT_CLASS (klass);
141 object_class->finalize = gtk_icon_factory_finalize;
143 g_type_class_add_private (klass, sizeof (GtkIconFactoryPriv));
147 gtk_icon_factory_buildable_init (GtkBuildableIface *iface)
149 iface->custom_tag_start = gtk_icon_factory_buildable_custom_tag_start;
150 iface->custom_tag_end = gtk_icon_factory_buildable_custom_tag_end;
154 free_icon_set (gpointer key, gpointer value, gpointer data)
157 gtk_icon_set_unref (value);
161 gtk_icon_factory_finalize (GObject *object)
163 GtkIconFactory *factory = GTK_ICON_FACTORY (object);
164 GtkIconFactoryPriv *priv = factory->priv;
166 all_icon_factories = g_slist_remove (all_icon_factories, factory);
168 g_hash_table_foreach (priv->icons, free_icon_set, NULL);
170 g_hash_table_destroy (priv->icons);
172 G_OBJECT_CLASS (gtk_icon_factory_parent_class)->finalize (object);
176 * gtk_icon_factory_new:
178 * Creates a new #GtkIconFactory. An icon factory manages a collection
179 * of #GtkIconSet<!-- -->s; a #GtkIconSet manages a set of variants of a
180 * particular icon (i.e. a #GtkIconSet contains variants for different
181 * sizes and widget states). Icons in an icon factory are named by a
182 * stock ID, which is a simple string identifying the icon. Each
183 * #GtkStyle has a list of #GtkIconFactory<!-- -->s derived from the current
184 * theme; those icon factories are consulted first when searching for
185 * an icon. If the theme doesn't set a particular icon, GTK+ looks for
186 * the icon in a list of default icon factories, maintained by
187 * gtk_icon_factory_add_default() and
188 * gtk_icon_factory_remove_default(). Applications with icons should
189 * add a default icon factory with their icons, which will allow
190 * themes to override the icons for the application.
192 * Return value: a new #GtkIconFactory
195 gtk_icon_factory_new (void)
197 return g_object_new (GTK_TYPE_ICON_FACTORY, NULL);
201 * gtk_icon_factory_add:
202 * @factory: a #GtkIconFactory
203 * @stock_id: icon name
204 * @icon_set: icon set
206 * Adds the given @icon_set to the icon factory, under the name
207 * @stock_id. @stock_id should be namespaced for your application,
208 * e.g. "myapp-whatever-icon". Normally applications create a
209 * #GtkIconFactory, then add it to the list of default factories with
210 * gtk_icon_factory_add_default(). Then they pass the @stock_id to
211 * widgets such as #GtkImage to display the icon. Themes can provide
212 * an icon with the same name (such as "myapp-whatever-icon") to
213 * override your application's default icons. If an icon already
214 * existed in @factory for @stock_id, it is unreferenced and replaced
215 * with the new @icon_set.
218 gtk_icon_factory_add (GtkIconFactory *factory,
219 const gchar *stock_id,
220 GtkIconSet *icon_set)
222 GtkIconFactoryPriv *priv = factory->priv;
223 gpointer old_key = NULL;
224 gpointer old_value = NULL;
226 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
227 g_return_if_fail (stock_id != NULL);
228 g_return_if_fail (icon_set != NULL);
230 g_hash_table_lookup_extended (priv->icons, stock_id,
231 &old_key, &old_value);
233 if (old_value == icon_set)
236 gtk_icon_set_ref (icon_set);
238 /* GHashTable key memory management is so fantastically broken. */
240 g_hash_table_insert (priv->icons, old_key, icon_set);
242 g_hash_table_insert (priv->icons, g_strdup (stock_id), icon_set);
245 gtk_icon_set_unref (old_value);
249 * gtk_icon_factory_lookup:
250 * @factory: a #GtkIconFactory
251 * @stock_id: an icon name
253 * Looks up @stock_id in the icon factory, returning an icon set
254 * if found, otherwise %NULL. For display to the user, you should
255 * use gtk_style_lookup_icon_set() on the #GtkStyle for the
256 * widget that will display the icon, instead of using this
257 * function directly, so that themes are taken into account.
259 * Return value: icon set of @stock_id.
262 gtk_icon_factory_lookup (GtkIconFactory *factory,
263 const gchar *stock_id)
265 GtkIconFactoryPriv *priv;
267 g_return_val_if_fail (GTK_IS_ICON_FACTORY (factory), NULL);
268 g_return_val_if_fail (stock_id != NULL, NULL);
270 priv = factory->priv;
272 return g_hash_table_lookup (priv->icons, stock_id);
275 static GtkIconFactory *gtk_default_icons = NULL;
276 static GSList *default_factories = NULL;
279 * gtk_icon_factory_add_default:
280 * @factory: a #GtkIconFactory
282 * Adds an icon factory to the list of icon factories searched by
283 * gtk_style_lookup_icon_set(). This means that, for example,
284 * gtk_image_new_from_stock() will be able to find icons in @factory.
285 * There will normally be an icon factory added for each library or
286 * application that comes with icons. The default icon factories
287 * can be overridden by themes.
290 gtk_icon_factory_add_default (GtkIconFactory *factory)
292 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
294 g_object_ref (factory);
296 default_factories = g_slist_prepend (default_factories, factory);
300 * gtk_icon_factory_remove_default:
301 * @factory: a #GtkIconFactory previously added with gtk_icon_factory_add_default()
303 * Removes an icon factory from the list of default icon
304 * factories. Not normally used; you might use it for a library that
305 * can be unloaded or shut down.
308 gtk_icon_factory_remove_default (GtkIconFactory *factory)
310 g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
312 default_factories = g_slist_remove (default_factories, factory);
314 g_object_unref (factory);
318 _gtk_icon_factory_ensure_default_icons (void)
320 if (gtk_default_icons == NULL)
322 gtk_default_icons = gtk_icon_factory_new ();
324 get_default_icons (gtk_default_icons);
329 * gtk_icon_factory_lookup_default:
330 * @stock_id: an icon name
332 * Looks for an icon in the list of default icon factories. For
333 * display to the user, you should use gtk_style_lookup_icon_set() on
334 * the #GtkStyle for the widget that will display the icon, instead of
335 * using this function directly, so that themes are taken into
338 * Return value: a #GtkIconSet, or %NULL
341 gtk_icon_factory_lookup_default (const gchar *stock_id)
345 g_return_val_if_fail (stock_id != NULL, NULL);
347 tmp_list = default_factories;
348 while (tmp_list != NULL)
350 GtkIconSet *icon_set =
351 gtk_icon_factory_lookup (GTK_ICON_FACTORY (tmp_list->data),
357 tmp_list = g_slist_next (tmp_list);
360 _gtk_icon_factory_ensure_default_icons ();
362 return gtk_icon_factory_lookup (gtk_default_icons, stock_id);
366 register_stock_icon (GtkIconFactory *factory,
367 const gchar *stock_id,
368 const gchar *icon_name)
370 GtkIconSet *set = gtk_icon_set_new ();
371 GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
373 source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
374 source.source.icon_name = (gchar *)icon_name;
375 source.direction = GTK_TEXT_DIR_NONE;
376 gtk_icon_set_add_source (set, &source);
378 gtk_icon_factory_add (factory, stock_id, set);
379 gtk_icon_set_unref (set);
383 register_bidi_stock_icon (GtkIconFactory *factory,
384 const gchar *stock_id,
385 const gchar *icon_name)
387 GtkIconSet *set = gtk_icon_set_new ();
388 GtkIconSource source = GTK_ICON_SOURCE_INIT (FALSE, TRUE, TRUE);
390 source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
391 source.source.icon_name = (gchar *)icon_name;
392 source.direction = GTK_TEXT_DIR_LTR;
393 gtk_icon_set_add_source (set, &source);
395 source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
396 source.source.icon_name = (gchar *)icon_name;
397 source.direction = GTK_TEXT_DIR_RTL;
398 gtk_icon_set_add_source (set, &source);
400 gtk_icon_factory_add (factory, stock_id, set);
401 gtk_icon_set_unref (set);
405 get_default_icons (GtkIconFactory *factory)
407 /* KEEP IN SYNC with gtkstock.c */
409 register_stock_icon (factory, GTK_STOCK_DIALOG_AUTHENTICATION, "dialog-password");
410 register_stock_icon (factory, GTK_STOCK_DIALOG_ERROR, "dialog-error");
411 register_stock_icon (factory, GTK_STOCK_DIALOG_INFO, "dialog-info");
412 register_stock_icon (factory, GTK_STOCK_DIALOG_QUESTION, "dialog-question");
413 register_stock_icon (factory, GTK_STOCK_DIALOG_WARNING, "dialog-warning");
414 register_stock_icon (factory, GTK_STOCK_DND, GTK_STOCK_DND);
415 register_stock_icon (factory, GTK_STOCK_DND_MULTIPLE, GTK_STOCK_DND_MULTIPLE);
416 register_stock_icon (factory, GTK_STOCK_APPLY, GTK_STOCK_APPLY);
417 register_stock_icon (factory, GTK_STOCK_CANCEL, GTK_STOCK_CANCEL);
418 register_stock_icon (factory, GTK_STOCK_NO, GTK_STOCK_NO);
419 register_stock_icon (factory, GTK_STOCK_OK, GTK_STOCK_OK);
420 register_stock_icon (factory, GTK_STOCK_YES, GTK_STOCK_YES);
421 register_stock_icon (factory, GTK_STOCK_CLOSE, "window-close");
422 register_stock_icon (factory, GTK_STOCK_ADD, "list-add");
423 register_stock_icon (factory, GTK_STOCK_JUSTIFY_CENTER, "format-justify-center");
424 register_stock_icon (factory, GTK_STOCK_JUSTIFY_FILL, "format-justify-fill");
425 register_stock_icon (factory, GTK_STOCK_JUSTIFY_LEFT, "format-justify-left");
426 register_stock_icon (factory, GTK_STOCK_JUSTIFY_RIGHT, "format-justify-right");
427 register_stock_icon (factory, GTK_STOCK_GOTO_BOTTOM, "go-bottom");
428 register_stock_icon (factory, GTK_STOCK_CDROM, "media-optical");
429 register_stock_icon (factory, GTK_STOCK_CONVERT, GTK_STOCK_CONVERT);
430 register_stock_icon (factory, GTK_STOCK_COPY, "edit-copy");
431 register_stock_icon (factory, GTK_STOCK_CUT, "edit-cut");
432 register_stock_icon (factory, GTK_STOCK_GO_DOWN, "go-down");
433 register_stock_icon (factory, GTK_STOCK_EXECUTE, "system-run");
434 register_stock_icon (factory, GTK_STOCK_QUIT, "application-exit");
435 register_bidi_stock_icon (factory, GTK_STOCK_GOTO_FIRST, "go-first");
436 register_stock_icon (factory, GTK_STOCK_SELECT_FONT, GTK_STOCK_SELECT_FONT);
437 register_stock_icon (factory, GTK_STOCK_FULLSCREEN, "view-fullscreen");
438 register_stock_icon (factory, GTK_STOCK_LEAVE_FULLSCREEN, "view-restore");
439 register_stock_icon (factory, GTK_STOCK_HARDDISK, "drive-harddisk");
440 register_stock_icon (factory, GTK_STOCK_HELP, "help-contents");
441 register_stock_icon (factory, GTK_STOCK_HOME, "go-home");
442 register_stock_icon (factory, GTK_STOCK_INFO, "dialog-info");
443 register_bidi_stock_icon (factory, GTK_STOCK_JUMP_TO, "go-jump");
444 register_bidi_stock_icon (factory, GTK_STOCK_GOTO_LAST, "go-last");
445 register_bidi_stock_icon (factory, GTK_STOCK_GO_BACK, "go-previous");
446 register_stock_icon (factory, GTK_STOCK_MISSING_IMAGE, "image-missing");
447 register_stock_icon (factory, GTK_STOCK_NETWORK, "network-idle");
448 register_stock_icon (factory, GTK_STOCK_NEW, "document-new");
449 register_stock_icon (factory, GTK_STOCK_OPEN, "document-open");
450 register_stock_icon (factory, GTK_STOCK_ORIENTATION_PORTRAIT, GTK_STOCK_ORIENTATION_PORTRAIT);
451 register_stock_icon (factory, GTK_STOCK_ORIENTATION_LANDSCAPE, GTK_STOCK_ORIENTATION_LANDSCAPE);
452 register_stock_icon (factory, GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT, GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT);
453 register_stock_icon (factory, GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE, GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE);
454 register_stock_icon (factory, GTK_STOCK_PAGE_SETUP, GTK_STOCK_PAGE_SETUP);
455 register_stock_icon (factory, GTK_STOCK_PASTE, "edit-paste");
456 register_stock_icon (factory, GTK_STOCK_PREFERENCES, "edit-preferences");
457 register_stock_icon (factory, GTK_STOCK_PRINT, "document-print");
458 register_stock_icon (factory, GTK_STOCK_PRINT_ERROR, "printer-error");
459 register_stock_icon (factory, GTK_STOCK_PRINT_PAUSED, "printer-paused");
460 register_stock_icon (factory, GTK_STOCK_PRINT_PREVIEW, "document-print-preview");
461 register_stock_icon (factory, GTK_STOCK_PRINT_REPORT, "printer-info");
462 register_stock_icon (factory, GTK_STOCK_PRINT_WARNING, "printer-warning");
463 register_stock_icon (factory, GTK_STOCK_PROPERTIES, "document-properties");
464 register_bidi_stock_icon (factory, GTK_STOCK_REDO, "edit-redo");
465 register_stock_icon (factory, GTK_STOCK_REMOVE, "list-remove");
466 register_stock_icon (factory, GTK_STOCK_REFRESH, "view-refresh");
467 register_bidi_stock_icon (factory, GTK_STOCK_REVERT_TO_SAVED, "document-revert");
468 register_bidi_stock_icon (factory, GTK_STOCK_GO_FORWARD, "go-next");
469 register_stock_icon (factory, GTK_STOCK_SAVE, "document-save");
470 register_stock_icon (factory, GTK_STOCK_FLOPPY, "media-floppy");
471 register_stock_icon (factory, GTK_STOCK_SAVE_AS, "document-save-as");
472 register_stock_icon (factory, GTK_STOCK_FIND, "edit-find");
473 register_stock_icon (factory, GTK_STOCK_FIND_AND_REPLACE, "edit-find-replace");
474 register_stock_icon (factory, GTK_STOCK_SORT_DESCENDING, "view-sort-descending");
475 register_stock_icon (factory, GTK_STOCK_SORT_ASCENDING, "view-sort-ascending");
476 register_stock_icon (factory, GTK_STOCK_SPELL_CHECK, "tools-check-spelling");
477 register_stock_icon (factory, GTK_STOCK_STOP, "process-stop");
478 register_stock_icon (factory, GTK_STOCK_BOLD, "format-text-bold");
479 register_stock_icon (factory, GTK_STOCK_ITALIC, "format-text-italic");
480 register_stock_icon (factory, GTK_STOCK_STRIKETHROUGH, "format-text-strikethrough");
481 register_stock_icon (factory, GTK_STOCK_UNDERLINE, "format-text-underline");
482 register_bidi_stock_icon (factory, GTK_STOCK_INDENT, "format-indent-more");
483 register_bidi_stock_icon (factory, GTK_STOCK_UNINDENT, "format-indent-less");
484 register_stock_icon (factory, GTK_STOCK_GOTO_TOP, "go-top");
485 register_stock_icon (factory, GTK_STOCK_DELETE, "edit-delete");
486 register_bidi_stock_icon (factory, GTK_STOCK_UNDELETE, GTK_STOCK_UNDELETE);
487 register_bidi_stock_icon (factory, GTK_STOCK_UNDO, "edit-undo");
488 register_stock_icon (factory, GTK_STOCK_GO_UP, "go-up");
489 register_stock_icon (factory, GTK_STOCK_FILE, "document-x-generic");
490 register_stock_icon (factory, GTK_STOCK_DIRECTORY, "folder");
491 register_stock_icon (factory, GTK_STOCK_ABOUT, "help-about");
492 register_stock_icon (factory, GTK_STOCK_CONNECT, GTK_STOCK_CONNECT);
493 register_stock_icon (factory, GTK_STOCK_DISCONNECT, GTK_STOCK_DISCONNECT);
494 register_stock_icon (factory, GTK_STOCK_EDIT, GTK_STOCK_EDIT);
495 register_stock_icon (factory, GTK_STOCK_CAPS_LOCK_WARNING, GTK_STOCK_CAPS_LOCK_WARNING);
496 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_FORWARD, "media-seek-forward");
497 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_NEXT, "media-skip-forward");
498 register_stock_icon (factory, GTK_STOCK_MEDIA_PAUSE, "media-playback-pause");
499 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_PLAY, "media-playback-start");
500 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_PREVIOUS, "media-skip-backward");
501 register_stock_icon (factory, GTK_STOCK_MEDIA_RECORD, "media-record");
502 register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_REWIND, "media-seek-backward");
503 register_stock_icon (factory, GTK_STOCK_MEDIA_STOP, "media-playback-stop");
504 register_stock_icon (factory, GTK_STOCK_INDEX, GTK_STOCK_INDEX);
505 register_stock_icon (factory, GTK_STOCK_ZOOM_100, "zoom-original");
506 register_stock_icon (factory, GTK_STOCK_ZOOM_IN, "zoom-in");
507 register_stock_icon (factory, GTK_STOCK_ZOOM_OUT, "zoom-out");
508 register_stock_icon (factory, GTK_STOCK_ZOOM_FIT, "zoom-fit-best");
509 register_stock_icon (factory, GTK_STOCK_SELECT_ALL, "edit-select-all");
510 register_stock_icon (factory, GTK_STOCK_CLEAR, "edit-clear");
511 register_stock_icon (factory, GTK_STOCK_SELECT_COLOR, GTK_STOCK_SELECT_COLOR);
512 register_stock_icon (factory, GTK_STOCK_COLOR_PICKER, GTK_STOCK_COLOR_PICKER);
515 /************************************************************
516 * Icon size handling *
517 ************************************************************/
519 typedef struct _IconSize IconSize;
530 typedef struct _IconAlias IconAlias;
538 typedef struct _SettingsIconSize SettingsIconSize;
540 struct _SettingsIconSize
546 static GHashTable *icon_aliases = NULL;
547 static IconSize *icon_sizes = NULL;
548 static gint icon_sizes_allocated = 0;
549 static gint icon_sizes_used = 0;
552 init_icon_sizes (void)
554 if (icon_sizes == NULL)
556 #define NUM_BUILTIN_SIZES 7
559 icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
561 icon_sizes = g_new (IconSize, NUM_BUILTIN_SIZES);
562 icon_sizes_allocated = NUM_BUILTIN_SIZES;
563 icon_sizes_used = NUM_BUILTIN_SIZES;
565 icon_sizes[GTK_ICON_SIZE_INVALID].size = 0;
566 icon_sizes[GTK_ICON_SIZE_INVALID].name = NULL;
567 icon_sizes[GTK_ICON_SIZE_INVALID].width = 0;
568 icon_sizes[GTK_ICON_SIZE_INVALID].height = 0;
570 /* the name strings aren't copied since we don't ever remove
571 * icon sizes, so we don't need to know whether they're static.
572 * Even if we did I suppose removing the builtin sizes would be
576 icon_sizes[GTK_ICON_SIZE_MENU].size = GTK_ICON_SIZE_MENU;
577 icon_sizes[GTK_ICON_SIZE_MENU].name = "gtk-menu";
578 icon_sizes[GTK_ICON_SIZE_MENU].width = 16;
579 icon_sizes[GTK_ICON_SIZE_MENU].height = 16;
581 icon_sizes[GTK_ICON_SIZE_BUTTON].size = GTK_ICON_SIZE_BUTTON;
582 icon_sizes[GTK_ICON_SIZE_BUTTON].name = "gtk-button";
583 icon_sizes[GTK_ICON_SIZE_BUTTON].width = 20;
584 icon_sizes[GTK_ICON_SIZE_BUTTON].height = 20;
586 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].size = GTK_ICON_SIZE_SMALL_TOOLBAR;
587 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].name = "gtk-small-toolbar";
588 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].width = 18;
589 icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].height = 18;
591 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].size = GTK_ICON_SIZE_LARGE_TOOLBAR;
592 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].name = "gtk-large-toolbar";
593 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].width = 24;
594 icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].height = 24;
596 icon_sizes[GTK_ICON_SIZE_DND].size = GTK_ICON_SIZE_DND;
597 icon_sizes[GTK_ICON_SIZE_DND].name = "gtk-dnd";
598 icon_sizes[GTK_ICON_SIZE_DND].width = 32;
599 icon_sizes[GTK_ICON_SIZE_DND].height = 32;
601 icon_sizes[GTK_ICON_SIZE_DIALOG].size = GTK_ICON_SIZE_DIALOG;
602 icon_sizes[GTK_ICON_SIZE_DIALOG].name = "gtk-dialog";
603 icon_sizes[GTK_ICON_SIZE_DIALOG].width = 48;
604 icon_sizes[GTK_ICON_SIZE_DIALOG].height = 48;
606 g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
608 /* Alias everything to itself. */
609 i = 1; /* skip invalid size */
610 while (i < NUM_BUILTIN_SIZES)
612 gtk_icon_size_register_alias (icon_sizes[i].name, icon_sizes[i].size);
617 #undef NUM_BUILTIN_SIZES
622 free_settings_sizes (gpointer data)
624 g_array_free (data, TRUE);
628 get_settings_sizes (GtkSettings *settings,
631 GArray *settings_sizes;
632 static GQuark sizes_quark = 0;
635 sizes_quark = g_quark_from_static_string ("gtk-icon-sizes");
637 settings_sizes = g_object_get_qdata (G_OBJECT (settings), sizes_quark);
640 settings_sizes = g_array_new (FALSE, FALSE, sizeof (SettingsIconSize));
641 g_object_set_qdata_full (G_OBJECT (settings), sizes_quark,
642 settings_sizes, free_settings_sizes);
647 return settings_sizes;
651 icon_size_set_for_settings (GtkSettings *settings,
652 const gchar *size_name,
657 GArray *settings_sizes;
658 SettingsIconSize *settings_size;
660 g_return_if_fail (size_name != NULL);
662 size = gtk_icon_size_from_name (size_name);
663 if (size == GTK_ICON_SIZE_INVALID)
664 /* Reserve a place */
665 size = icon_size_register_intern (size_name, -1, -1);
667 settings_sizes = get_settings_sizes (settings, NULL);
668 if (size >= settings_sizes->len)
670 SettingsIconSize unset = { -1, -1 };
673 for (i = settings_sizes->len; i <= size; i++)
674 g_array_append_val (settings_sizes, unset);
677 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
679 settings_size->width = width;
680 settings_size->height = height;
683 /* Like pango_parse_word, but accept - as well
686 scan_icon_size_name (const char **pos, GString *out)
688 const char *p = *pos;
690 while (g_ascii_isspace (*p))
693 if (!((*p >= 'A' && *p <= 'Z') ||
694 (*p >= 'a' && *p <= 'z') ||
695 *p == '_' || *p == '-'))
698 g_string_truncate (out, 0);
699 g_string_append_c (out, *p);
702 while ((*p >= 'A' && *p <= 'Z') ||
703 (*p >= 'a' && *p <= 'z') ||
704 (*p >= '0' && *p <= '9') ||
705 *p == '_' || *p == '-')
707 g_string_append_c (out, *p);
717 icon_size_setting_parse (GtkSettings *settings,
718 const gchar *icon_size_string)
720 GString *name_buf = g_string_new (NULL);
721 const gchar *p = icon_size_string;
723 while (pango_skip_space (&p))
727 if (!scan_icon_size_name (&p, name_buf))
730 if (!pango_skip_space (&p))
738 if (!pango_scan_int (&p, &width))
741 if (!pango_skip_space (&p))
749 if (!pango_scan_int (&p, &height))
752 if (width > 0 && height > 0)
754 icon_size_set_for_settings (settings, name_buf->str,
759 g_warning ("Invalid size in gtk-icon-sizes: %d,%d\n", width, height);
762 pango_skip_space (&p);
771 g_string_free (name_buf, TRUE);
775 g_warning ("Error parsing gtk-icon-sizes string:\n\t'%s'", icon_size_string);
776 g_string_free (name_buf, TRUE);
780 icon_size_set_all_from_settings (GtkSettings *settings)
782 GArray *settings_sizes;
783 gchar *icon_size_string;
785 /* Reset old settings */
786 settings_sizes = get_settings_sizes (settings, NULL);
787 g_array_set_size (settings_sizes, 0);
789 g_object_get (settings,
790 "gtk-icon-sizes", &icon_size_string,
793 if (icon_size_string)
795 icon_size_setting_parse (settings, icon_size_string);
796 g_free (icon_size_string);
801 icon_size_settings_changed (GtkSettings *settings,
804 icon_size_set_all_from_settings (settings);
806 gtk_rc_reset_styles (settings);
810 icon_sizes_init_for_settings (GtkSettings *settings)
812 g_signal_connect (settings,
813 "notify::gtk-icon-sizes",
814 G_CALLBACK (icon_size_settings_changed),
817 icon_size_set_all_from_settings (settings);
821 icon_size_lookup_intern (GtkSettings *settings,
826 GArray *settings_sizes;
827 gint width_for_settings = -1;
828 gint height_for_settings = -1;
832 if (size == (GtkIconSize)-1)
835 if (size >= icon_sizes_used)
838 if (size == GTK_ICON_SIZE_INVALID)
843 gboolean initial = FALSE;
845 settings_sizes = get_settings_sizes (settings, &initial);
848 icon_sizes_init_for_settings (settings);
850 if (size < settings_sizes->len)
852 SettingsIconSize *settings_size;
854 settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
856 width_for_settings = settings_size->width;
857 height_for_settings = settings_size->height;
862 *widthp = width_for_settings >= 0 ? width_for_settings : icon_sizes[size].width;
865 *heightp = height_for_settings >= 0 ? height_for_settings : icon_sizes[size].height;
871 * gtk_icon_size_lookup_for_settings:
872 * @settings: a #GtkSettings object, used to determine
873 * which set of user preferences to used.
874 * @size: (type int): an icon size
875 * @width: location to store icon width
876 * @height: location to store icon height
878 * Obtains the pixel size of a semantic icon size, possibly
879 * modified by user preferences for a particular
880 * #GtkSettings. Normally @size would be
881 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
882 * isn't normally needed, gtk_widget_render_icon() is the usual
883 * way to get an icon for rendering, then just look at the size of
884 * the rendered pixbuf. The rendered pixbuf may not even correspond to
885 * the width/height returned by gtk_icon_size_lookup(), because themes
886 * are free to render the pixbuf however they like, including changing
889 * Return value: %TRUE if @size was a valid size
894 gtk_icon_size_lookup_for_settings (GtkSettings *settings,
899 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
901 return icon_size_lookup_intern (settings, size, width, height);
905 * gtk_icon_size_lookup:
906 * @size: (type int): an icon size
907 * @width: location to store icon width
908 * @height: location to store icon height
910 * Obtains the pixel size of a semantic icon size, possibly
911 * modified by user preferences for the default #GtkSettings.
912 * (See gtk_icon_size_lookup_for_settings().)
913 * Normally @size would be
914 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc. This function
915 * isn't normally needed, gtk_widget_render_icon() is the usual
916 * way to get an icon for rendering, then just look at the size of
917 * the rendered pixbuf. The rendered pixbuf may not even correspond to
918 * the width/height returned by gtk_icon_size_lookup(), because themes
919 * are free to render the pixbuf however they like, including changing
922 * Return value: %TRUE if @size was a valid size
925 gtk_icon_size_lookup (GtkIconSize size,
930 g_warning ("gtk_icon_size_lookup ()) is not multihead safe"));
932 return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (),
933 size, widthp, heightp);
937 icon_size_register_intern (const gchar *name,
941 IconAlias *old_alias;
946 old_alias = g_hash_table_lookup (icon_aliases, name);
947 if (old_alias && icon_sizes[old_alias->target].width > 0)
949 g_warning ("Icon size name '%s' already exists", name);
950 return GTK_ICON_SIZE_INVALID;
955 size = old_alias->target;
959 if (icon_sizes_used == icon_sizes_allocated)
961 icon_sizes_allocated *= 2;
962 icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated);
965 size = icon_sizes_used++;
968 gtk_icon_size_register_alias (name, size);
970 icon_sizes[size].size = size;
971 icon_sizes[size].name = g_strdup (name);
974 icon_sizes[size].width = width;
975 icon_sizes[size].height = height;
981 * gtk_icon_size_register:
982 * @name: name of the icon size
983 * @width: the icon width
984 * @height: the icon height
986 * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
987 * etc. Returns the integer value for the size.
989 * Returns: (type int): integer value representing the size
992 gtk_icon_size_register (const gchar *name,
996 g_return_val_if_fail (name != NULL, 0);
997 g_return_val_if_fail (width > 0, 0);
998 g_return_val_if_fail (height > 0, 0);
1000 return icon_size_register_intern (name, width, height);
1004 * gtk_icon_size_register_alias:
1005 * @alias: an alias for @target
1006 * @target: (type int): an existing icon size
1008 * Registers @alias as another name for @target.
1009 * So calling gtk_icon_size_from_name() with @alias as argument
1010 * will return @target.
1013 gtk_icon_size_register_alias (const gchar *alias,
1018 g_return_if_fail (alias != NULL);
1022 if (!icon_size_lookup_intern (NULL, target, NULL, NULL))
1023 g_warning ("gtk_icon_size_register_alias: Icon size %u does not exist", target);
1025 ia = g_hash_table_lookup (icon_aliases, alias);
1028 if (icon_sizes[ia->target].width > 0)
1030 g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias);
1034 ia->target = target;
1039 ia = g_new (IconAlias, 1);
1040 ia->name = g_strdup (alias);
1041 ia->target = target;
1043 g_hash_table_insert (icon_aliases, ia->name, ia);
1048 * gtk_icon_size_from_name:
1049 * @name: the name to look up.
1051 * Looks up the icon size associated with @name.
1053 * Return value: (type int): the icon size
1056 gtk_icon_size_from_name (const gchar *name)
1062 ia = g_hash_table_lookup (icon_aliases, name);
1064 if (ia && icon_sizes[ia->target].width > 0)
1067 return GTK_ICON_SIZE_INVALID;
1071 * gtk_icon_size_get_name:
1072 * @size: (type int): a #GtkIconSize.
1073 * @returns: the name of the given icon size.
1075 * Gets the canonical name of the given icon size. The returned string
1076 * is statically allocated and should not be freed.
1078 G_CONST_RETURN gchar*
1079 gtk_icon_size_get_name (GtkIconSize size)
1081 if (size >= icon_sizes_used)
1084 return icon_sizes[size].name;
1087 /************************************************************/
1092 static GdkPixbuf *find_in_cache (GtkIconSet *icon_set,
1094 GtkTextDirection direction,
1097 static void add_to_cache (GtkIconSet *icon_set,
1099 GtkTextDirection direction,
1103 /* Clear icon set contents, drop references to all contained
1104 * GdkPixbuf objects and forget all GtkIconSources. Used to
1105 * recycle an icon set.
1107 static void clear_cache (GtkIconSet *icon_set,
1108 gboolean style_detach);
1109 static GSList* copy_cache (GtkIconSet *icon_set,
1110 GtkIconSet *copy_recipient);
1111 static void attach_to_style (GtkIconSet *icon_set,
1113 static void detach_from_style (GtkIconSet *icon_set,
1115 static void style_dnotify (gpointer data);
1123 /* Cache of the last few rendered versions of the icon. */
1131 static guint cache_serial = 0;
1136 * Creates a new #GtkIconSet. A #GtkIconSet represents a single icon
1137 * in various sizes and widget states. It can provide a #GdkPixbuf
1138 * for a given size and state on request, and automatically caches
1139 * some of the rendered #GdkPixbuf objects.
1141 * Normally you would use gtk_widget_render_icon() instead of
1142 * using #GtkIconSet directly. The one case where you'd use
1143 * #GtkIconSet is to create application-specific icon sets to place in
1144 * a #GtkIconFactory.
1146 * Return value: a new #GtkIconSet
1149 gtk_icon_set_new (void)
1151 GtkIconSet *icon_set;
1153 icon_set = g_new (GtkIconSet, 1);
1155 icon_set->ref_count = 1;
1156 icon_set->sources = NULL;
1157 icon_set->cache = NULL;
1158 icon_set->cache_size = 0;
1159 icon_set->cache_serial = cache_serial;
1165 * gtk_icon_set_new_from_pixbuf:
1166 * @pixbuf: a #GdkPixbuf
1168 * Creates a new #GtkIconSet with @pixbuf as the default/fallback
1169 * source image. If you don't add any additional #GtkIconSource to the
1170 * icon set, all variants of the icon will be created from @pixbuf,
1171 * using scaling, pixelation, etc. as required to adjust the icon size
1172 * or make the icon look insensitive/prelighted.
1174 * Return value: a new #GtkIconSet
1177 gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
1181 GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1183 g_return_val_if_fail (pixbuf != NULL, NULL);
1185 set = gtk_icon_set_new ();
1187 gtk_icon_source_set_pixbuf (&source, pixbuf);
1188 gtk_icon_set_add_source (set, &source);
1189 gtk_icon_source_set_pixbuf (&source, NULL);
1197 * @icon_set: a #GtkIconSet.
1199 * Increments the reference count on @icon_set.
1201 * Return value: @icon_set.
1204 gtk_icon_set_ref (GtkIconSet *icon_set)
1206 g_return_val_if_fail (icon_set != NULL, NULL);
1207 g_return_val_if_fail (icon_set->ref_count > 0, NULL);
1209 icon_set->ref_count += 1;
1215 * gtk_icon_set_unref:
1216 * @icon_set: a #GtkIconSet
1218 * Decrements the reference count on @icon_set, and frees memory
1219 * if the reference count reaches 0.
1222 gtk_icon_set_unref (GtkIconSet *icon_set)
1224 g_return_if_fail (icon_set != NULL);
1225 g_return_if_fail (icon_set->ref_count > 0);
1227 icon_set->ref_count -= 1;
1229 if (icon_set->ref_count == 0)
1231 GSList *tmp_list = icon_set->sources;
1232 while (tmp_list != NULL)
1234 gtk_icon_source_free (tmp_list->data);
1236 tmp_list = g_slist_next (tmp_list);
1238 g_slist_free (icon_set->sources);
1240 clear_cache (icon_set, TRUE);
1246 G_DEFINE_BOXED_TYPE (GtkIconSet, gtk_icon_set,
1251 * gtk_icon_set_copy:
1252 * @icon_set: a #GtkIconSet
1254 * Copies @icon_set by value.
1256 * Return value: a new #GtkIconSet identical to the first.
1259 gtk_icon_set_copy (GtkIconSet *icon_set)
1264 copy = gtk_icon_set_new ();
1266 tmp_list = icon_set->sources;
1267 while (tmp_list != NULL)
1269 copy->sources = g_slist_prepend (copy->sources,
1270 gtk_icon_source_copy (tmp_list->data));
1272 tmp_list = g_slist_next (tmp_list);
1275 copy->sources = g_slist_reverse (copy->sources);
1277 copy->cache = copy_cache (icon_set, copy);
1278 copy->cache_size = icon_set->cache_size;
1279 copy->cache_serial = icon_set->cache_serial;
1285 sizes_equivalent (GtkIconSize lhs,
1288 /* We used to consider sizes equivalent if they were
1289 * the same pixel size, but we don't have the GtkSettings
1290 * here, so we can't do that. Plus, it's not clear that
1291 * it is right... it was just a workaround for the fact
1292 * that we register icons by logical size, not pixel size.
1298 gint r_w, r_h, l_w, l_h;
1300 icon_size_lookup_intern (NULL, rhs, &r_w, &r_h);
1301 icon_size_lookup_intern (NULL, lhs, &l_w, &l_h);
1303 return r_w == l_w && r_h == l_h;
1307 static GtkIconSource *
1308 find_best_matching_source (GtkIconSet *icon_set,
1309 GtkTextDirection direction,
1314 GtkIconSource *source;
1317 /* We need to find the best icon source. Direction matters more
1318 * than state, state matters more than size. icon_set->sources
1319 * is sorted according to wildness, so if we take the first
1320 * match we find it will be the least-wild match (if there are
1321 * multiple matches for a given "wildness" then the RC file contained
1322 * dumb stuff, and we end up with an arbitrary matching source)
1326 tmp_list = icon_set->sources;
1327 while (tmp_list != NULL)
1329 GtkIconSource *s = tmp_list->data;
1331 if ((s->any_direction || (s->direction == direction)) &&
1332 (s->any_state || (s->state == state)) &&
1333 (s->any_size || size == (GtkIconSize)-1 || (sizes_equivalent (size, s->size))))
1335 if (!g_slist_find (failed, s))
1342 tmp_list = g_slist_next (tmp_list);
1349 ensure_filename_pixbuf (GtkIconSet *icon_set,
1350 GtkIconSource *source)
1352 if (source->filename_pixbuf == NULL)
1354 GError *error = NULL;
1356 source->filename_pixbuf = gdk_pixbuf_new_from_file (source->source.filename, &error);
1358 if (source->filename_pixbuf == NULL)
1360 /* Remove this icon source so we don't keep trying to
1363 g_warning (_("Error loading icon: %s"), error->message);
1364 g_error_free (error);
1366 icon_set->sources = g_slist_remove (icon_set->sources, source);
1368 gtk_icon_source_free (source);
1378 render_icon_name_pixbuf (GtkIconSource *icon_source,
1380 GtkTextDirection direction,
1387 GdkPixbuf *tmp_pixbuf;
1388 GtkIconSource tmp_source;
1390 GtkIconTheme *icon_theme;
1391 GtkSettings *settings;
1392 gint width, height, pixel_size;
1393 gint *sizes, *s, dist;
1394 GError *error = NULL;
1396 if (widget && gtk_widget_has_screen (widget))
1397 screen = gtk_widget_get_screen (widget);
1398 else if (style && style->colormap)
1399 screen = gdk_colormap_get_screen (style->colormap);
1402 screen = gdk_screen_get_default ();
1403 GTK_NOTE (MULTIHEAD,
1404 g_warning ("Using the default screen for gtk_icon_source_render_icon()"));
1407 icon_theme = gtk_icon_theme_get_for_screen (screen);
1408 settings = gtk_settings_get_for_screen (screen);
1410 if (!gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
1412 if (size == (GtkIconSize)-1)
1414 /* Find an available size close to 48 */
1415 sizes = gtk_icon_theme_get_icon_sizes (icon_theme, icon_source->source.icon_name);
1417 width = height = 48;
1418 for (s = sizes; *s; s++)
1422 width = height = 48;
1429 width = height = *s;
1437 width = height = *s;
1447 g_warning ("Invalid icon size %u\n", size);
1448 width = height = 24;
1452 pixel_size = MIN (width, height);
1454 if (icon_source->direction != GTK_TEXT_DIR_NONE)
1456 gchar *suffix[3] = { NULL, "-ltr", "-rtl" };
1460 names[0] = g_strconcat (icon_source->source.icon_name, suffix[icon_source->direction], NULL);
1461 names[1] = icon_source->source.icon_name;
1464 info = gtk_icon_theme_choose_icon (icon_theme,
1466 pixel_size, GTK_ICON_LOOKUP_USE_BUILTIN);
1470 tmp_pixbuf = gtk_icon_info_load_icon (info, &error);
1471 gtk_icon_info_free (info);
1476 tmp_pixbuf = gtk_icon_theme_load_icon (icon_theme,
1477 icon_source->source.icon_name,
1484 g_warning ("Error loading theme icon '%s' for stock: %s",
1485 icon_source->source.icon_name, error ? error->message : "");
1487 g_error_free (error);
1491 tmp_source = *icon_source;
1492 tmp_source.type = GTK_ICON_SOURCE_PIXBUF;
1493 tmp_source.source.pixbuf = tmp_pixbuf;
1495 pixbuf = gtk_style_render_icon (style, &tmp_source,
1496 direction, state, -1,
1500 g_warning ("Failed to render icon");
1502 g_object_unref (tmp_pixbuf);
1508 find_and_render_icon_source (GtkIconSet *icon_set,
1510 GtkTextDirection direction,
1516 GSList *failed = NULL;
1517 GdkPixbuf *pixbuf = NULL;
1519 /* We treat failure in two different ways:
1521 * A) If loading a source that specifies a filename fails,
1522 * we treat that as permanent, and remove the source
1523 * from the GtkIconSet. (in ensure_filename_pixbuf ()
1524 * B) If loading a themed icon fails, or scaling an icon
1525 * fails, we treat that as transient and will try
1526 * again next time the icon falls out of the cache
1527 * and we need to recreate it.
1529 while (pixbuf == NULL)
1531 GtkIconSource *source = find_best_matching_source (icon_set, direction, state, size, failed);
1536 switch (source->type)
1538 case GTK_ICON_SOURCE_FILENAME:
1539 if (!ensure_filename_pixbuf (icon_set, source))
1542 case GTK_ICON_SOURCE_PIXBUF:
1543 pixbuf = gtk_style_render_icon (style, source,
1544 direction, state, size,
1548 g_warning ("Failed to render icon");
1549 failed = g_slist_prepend (failed, source);
1552 case GTK_ICON_SOURCE_ICON_NAME:
1553 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1554 pixbuf = render_icon_name_pixbuf (source, style,
1555 direction, state, size,
1558 failed = g_slist_prepend (failed, source);
1560 case GTK_ICON_SOURCE_EMPTY:
1561 g_assert_not_reached ();
1565 g_slist_free (failed);
1570 extern GtkIconCache *_builtin_cache;
1573 render_fallback_image (GtkStyle *style,
1574 GtkTextDirection direction,
1580 /* This icon can be used for any direction/state/size */
1581 static GtkIconSource fallback_source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1583 if (fallback_source.type == GTK_ICON_SOURCE_EMPTY)
1588 _gtk_icon_theme_ensure_builtin_cache ();
1590 index = _gtk_icon_cache_get_directory_index (_builtin_cache, "24");
1591 pixbuf = _gtk_icon_cache_get_icon (_builtin_cache,
1592 GTK_STOCK_MISSING_IMAGE,
1594 gtk_icon_source_set_pixbuf (&fallback_source, pixbuf);
1595 g_object_unref (pixbuf);
1598 return gtk_style_render_icon (style,
1608 * gtk_icon_set_render_icon:
1609 * @icon_set: a #GtkIconSet
1610 * @style: (allow-none): a #GtkStyle associated with @widget, or %NULL
1611 * @direction: text direction
1612 * @state: widget state
1613 * @size: (type int): icon size. A size of (GtkIconSize)-1
1614 * means render at the size of the source and don't scale.
1615 * @widget: (allow-none): widget that will display the icon, or %NULL.
1616 * The only use that is typically made of this
1617 * is to determine the appropriate #GdkScreen.
1618 * @detail: (allow-none): detail to pass to the theme engine, or %NULL.
1619 * Note that passing a detail of anything but %NULL
1620 * will disable caching.
1622 * Renders an icon using gtk_style_render_icon(). In most cases,
1623 * gtk_widget_render_icon() is better, since it automatically provides
1624 * most of the arguments from the current widget settings. This
1625 * function never returns %NULL; if the icon can't be rendered
1626 * (perhaps because an image file fails to load), a default "missing
1627 * image" icon will be returned instead.
1629 * Return value: a #GdkPixbuf to be displayed
1632 gtk_icon_set_render_icon (GtkIconSet *icon_set,
1634 GtkTextDirection direction,
1642 g_return_val_if_fail (icon_set != NULL, NULL);
1643 g_return_val_if_fail (style == NULL || GTK_IS_STYLE (style), NULL);
1645 if (icon_set->sources == NULL)
1646 return render_fallback_image (style, direction, state, size, widget, detail);
1650 icon = find_in_cache (icon_set, style, direction,
1655 g_object_ref (icon);
1661 icon = find_and_render_icon_source (icon_set, style, direction, state, size,
1665 icon = render_fallback_image (style, direction, state, size, widget, detail);
1668 add_to_cache (icon_set, style, direction, state, size, icon);
1673 /* Order sources by their "wildness", so that "wilder" sources are
1674 * greater than "specific" sources; for determining ordering,
1675 * direction beats state beats size.
1679 icon_source_compare (gconstpointer ap, gconstpointer bp)
1681 const GtkIconSource *a = ap;
1682 const GtkIconSource *b = bp;
1684 if (!a->any_direction && b->any_direction)
1686 else if (a->any_direction && !b->any_direction)
1688 else if (!a->any_state && b->any_state)
1690 else if (a->any_state && !b->any_state)
1692 else if (!a->any_size && b->any_size)
1694 else if (a->any_size && !b->any_size)
1701 * gtk_icon_set_add_source:
1702 * @icon_set: a #GtkIconSet
1703 * @source: a #GtkIconSource
1705 * Icon sets have a list of #GtkIconSource, which they use as base
1706 * icons for rendering icons in different states and sizes. Icons are
1707 * scaled, made to look insensitive, etc. in
1708 * gtk_icon_set_render_icon(), but #GtkIconSet needs base images to
1709 * work with. The base images and when to use them are described by
1712 * This function copies @source, so you can reuse the same source immediately
1713 * without affecting the icon set.
1715 * An example of when you'd use this function: a web browser's "Back
1716 * to Previous Page" icon might point in a different direction in
1717 * Hebrew and in English; it might look different when insensitive;
1718 * and it might change size depending on toolbar mode (small/large
1719 * icons). So a single icon set would contain all those variants of
1720 * the icon, and you might add a separate source for each one.
1722 * You should nearly always add a "default" icon source with all
1723 * fields wildcarded, which will be used as a fallback if no more
1724 * specific source matches. #GtkIconSet always prefers more specific
1725 * icon sources to more generic icon sources. The order in which you
1726 * add the sources to the icon set does not matter.
1728 * gtk_icon_set_new_from_pixbuf() creates a new icon set with a
1729 * default icon source based on the given pixbuf.
1732 gtk_icon_set_add_source (GtkIconSet *icon_set,
1733 const GtkIconSource *source)
1735 g_return_if_fail (icon_set != NULL);
1736 g_return_if_fail (source != NULL);
1738 if (source->type == GTK_ICON_SOURCE_EMPTY)
1740 g_warning ("Useless empty GtkIconSource");
1744 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
1745 gtk_icon_source_copy (source),
1746 icon_source_compare);
1750 * gtk_icon_set_get_sizes:
1751 * @icon_set: a #GtkIconSet
1752 * @sizes: (array length=n_sizes) (out) (type int): return location
1753 * for array of sizes
1754 * @n_sizes: location to store number of elements in returned array
1756 * Obtains a list of icon sizes this icon set can render. The returned
1757 * array must be freed with g_free().
1760 gtk_icon_set_get_sizes (GtkIconSet *icon_set,
1761 GtkIconSize **sizes,
1765 gboolean all_sizes = FALSE;
1766 GSList *specifics = NULL;
1768 g_return_if_fail (icon_set != NULL);
1769 g_return_if_fail (sizes != NULL);
1770 g_return_if_fail (n_sizes != NULL);
1772 tmp_list = icon_set->sources;
1773 while (tmp_list != NULL)
1775 GtkIconSource *source;
1777 source = tmp_list->data;
1779 if (source->any_size)
1785 specifics = g_slist_prepend (specifics, GINT_TO_POINTER (source->size));
1787 tmp_list = g_slist_next (tmp_list);
1792 /* Need to find out what sizes exist */
1797 *sizes = g_new (GtkIconSize, icon_sizes_used);
1798 *n_sizes = icon_sizes_used - 1;
1801 while (i < icon_sizes_used)
1803 (*sizes)[i - 1] = icon_sizes[i].size;
1811 *n_sizes = g_slist_length (specifics);
1812 *sizes = g_new (GtkIconSize, *n_sizes);
1815 tmp_list = specifics;
1816 while (tmp_list != NULL)
1818 (*sizes)[i] = GPOINTER_TO_INT (tmp_list->data);
1821 tmp_list = g_slist_next (tmp_list);
1825 g_slist_free (specifics);
1830 * gtk_icon_source_new:
1832 * Creates a new #GtkIconSource. A #GtkIconSource contains a #GdkPixbuf (or
1833 * image filename) that serves as the base image for one or more of the
1834 * icons in a #GtkIconSet, along with a specification for which icons in the
1835 * icon set will be based on that pixbuf or image file. An icon set contains
1836 * a set of icons that represent "the same" logical concept in different states,
1837 * different global text directions, and different sizes.
1839 * So for example a web browser's "Back to Previous Page" icon might
1840 * point in a different direction in Hebrew and in English; it might
1841 * look different when insensitive; and it might change size depending
1842 * on toolbar mode (small/large icons). So a single icon set would
1843 * contain all those variants of the icon. #GtkIconSet contains a list
1844 * of #GtkIconSource from which it can derive specific icon variants in
1847 * In the simplest case, #GtkIconSet contains one source pixbuf from
1848 * which it derives all variants. The convenience function
1849 * gtk_icon_set_new_from_pixbuf() handles this case; if you only have
1850 * one source pixbuf, just use that function.
1852 * If you want to use a different base pixbuf for different icon
1853 * variants, you create multiple icon sources, mark which variants
1854 * they'll be used to create, and add them to the icon set with
1855 * gtk_icon_set_add_source().
1857 * By default, the icon source has all parameters wildcarded. That is,
1858 * the icon source will be used as the base icon for any desired text
1859 * direction, widget state, or icon size.
1861 * Return value: a new #GtkIconSource
1864 gtk_icon_source_new (void)
1868 src = g_new0 (GtkIconSource, 1);
1870 src->direction = GTK_TEXT_DIR_NONE;
1871 src->size = GTK_ICON_SIZE_INVALID;
1872 src->state = GTK_STATE_NORMAL;
1874 src->any_direction = TRUE;
1875 src->any_state = TRUE;
1876 src->any_size = TRUE;
1882 * gtk_icon_source_copy:
1883 * @source: a #GtkIconSource
1885 * Creates a copy of @source; mostly useful for language bindings.
1887 * Return value: a new #GtkIconSource
1890 gtk_icon_source_copy (const GtkIconSource *source)
1892 GtkIconSource *copy;
1894 g_return_val_if_fail (source != NULL, NULL);
1896 copy = g_new (GtkIconSource, 1);
1902 case GTK_ICON_SOURCE_EMPTY:
1903 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1905 case GTK_ICON_SOURCE_ICON_NAME:
1906 copy->source.icon_name = g_strdup (copy->source.icon_name);
1908 case GTK_ICON_SOURCE_FILENAME:
1909 copy->source.filename = g_strdup (copy->source.filename);
1910 #if defined (G_OS_WIN32) && !defined (_WIN64)
1911 copy->cp_filename = g_strdup (copy->cp_filename);
1913 if (copy->filename_pixbuf)
1914 g_object_ref (copy->filename_pixbuf);
1916 case GTK_ICON_SOURCE_PIXBUF:
1917 g_object_ref (copy->source.pixbuf);
1920 g_assert_not_reached();
1927 * gtk_icon_source_free:
1928 * @source: a #GtkIconSource
1930 * Frees a dynamically-allocated icon source, along with its
1931 * filename, size, and pixbuf fields if those are not %NULL.
1934 gtk_icon_source_free (GtkIconSource *source)
1936 g_return_if_fail (source != NULL);
1938 icon_source_clear (source);
1942 G_DEFINE_BOXED_TYPE (GtkIconSource, gtk_icon_source,
1943 gtk_icon_source_copy,
1944 gtk_icon_source_free)
1947 icon_source_clear (GtkIconSource *source)
1949 switch (source->type)
1951 case GTK_ICON_SOURCE_EMPTY:
1953 case GTK_ICON_SOURCE_ICON_NAME:
1954 g_free (source->source.icon_name);
1956 case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1957 source->source.icon_name = NULL;
1959 case GTK_ICON_SOURCE_FILENAME:
1960 g_free (source->source.filename);
1961 source->source.filename = NULL;
1962 #if defined (G_OS_WIN32) && !defined (_WIN64)
1963 g_free (source->cp_filename);
1964 source->cp_filename = NULL;
1966 if (source->filename_pixbuf)
1967 g_object_unref (source->filename_pixbuf);
1968 source->filename_pixbuf = NULL;
1970 case GTK_ICON_SOURCE_PIXBUF:
1971 g_object_unref (source->source.pixbuf);
1972 source->source.pixbuf = NULL;
1975 g_assert_not_reached();
1978 source->type = GTK_ICON_SOURCE_EMPTY;
1982 * gtk_icon_source_set_filename:
1983 * @source: a #GtkIconSource
1984 * @filename: image file to use
1986 * Sets the name of an image file to use as a base image when creating
1987 * icon variants for #GtkIconSet. The filename must be absolute.
1990 gtk_icon_source_set_filename (GtkIconSource *source,
1991 const gchar *filename)
1993 g_return_if_fail (source != NULL);
1994 g_return_if_fail (filename == NULL || g_path_is_absolute (filename));
1996 if (source->type == GTK_ICON_SOURCE_FILENAME &&
1997 source->source.filename == filename)
2000 icon_source_clear (source);
2002 if (filename != NULL)
2004 source->type = GTK_ICON_SOURCE_FILENAME;
2005 source->source.filename = g_strdup (filename);
2006 #if defined (G_OS_WIN32) && !defined (_WIN64)
2007 source->cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
2013 * gtk_icon_source_set_icon_name
2014 * @source: a #GtkIconSource
2015 * @icon_name: (allow-none): name of icon to use
2017 * Sets the name of an icon to look up in the current icon theme
2018 * to use as a base image when creating icon variants for #GtkIconSet.
2021 gtk_icon_source_set_icon_name (GtkIconSource *source,
2022 const gchar *icon_name)
2024 g_return_if_fail (source != NULL);
2026 if (source->type == GTK_ICON_SOURCE_ICON_NAME &&
2027 source->source.icon_name == icon_name)
2030 icon_source_clear (source);
2032 if (icon_name != NULL)
2034 source->type = GTK_ICON_SOURCE_ICON_NAME;
2035 source->source.icon_name = g_strdup (icon_name);
2040 * gtk_icon_source_set_pixbuf:
2041 * @source: a #GtkIconSource
2042 * @pixbuf: pixbuf to use as a source
2044 * Sets a pixbuf to use as a base image when creating icon variants
2048 gtk_icon_source_set_pixbuf (GtkIconSource *source,
2051 g_return_if_fail (source != NULL);
2052 g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
2054 if (source->type == GTK_ICON_SOURCE_PIXBUF &&
2055 source->source.pixbuf == pixbuf)
2058 icon_source_clear (source);
2062 source->type = GTK_ICON_SOURCE_PIXBUF;
2063 source->source.pixbuf = g_object_ref (pixbuf);
2068 * gtk_icon_source_get_filename:
2069 * @source: a #GtkIconSource
2071 * Retrieves the source filename, or %NULL if none is set. The
2072 * filename is not a copy, and should not be modified or expected to
2073 * persist beyond the lifetime of the icon source.
2075 * Return value: image filename. This string must not be modified
2078 G_CONST_RETURN gchar*
2079 gtk_icon_source_get_filename (const GtkIconSource *source)
2081 g_return_val_if_fail (source != NULL, NULL);
2083 if (source->type == GTK_ICON_SOURCE_FILENAME)
2084 return source->source.filename;
2090 * gtk_icon_source_get_icon_name:
2091 * @source: a #GtkIconSource
2093 * Retrieves the source icon name, or %NULL if none is set. The
2094 * icon_name is not a copy, and should not be modified or expected to
2095 * persist beyond the lifetime of the icon source.
2097 * Return value: icon name. This string must not be modified or freed.
2099 G_CONST_RETURN gchar*
2100 gtk_icon_source_get_icon_name (const GtkIconSource *source)
2102 g_return_val_if_fail (source != NULL, NULL);
2104 if (source->type == GTK_ICON_SOURCE_ICON_NAME ||
2105 source->type == GTK_ICON_SOURCE_STATIC_ICON_NAME)
2106 return source->source.icon_name;
2112 * gtk_icon_source_get_pixbuf:
2113 * @source: a #GtkIconSource
2115 * Retrieves the source pixbuf, or %NULL if none is set.
2116 * In addition, if a filename source is in use, this
2117 * function in some cases will return the pixbuf from
2118 * loaded from the filename. This is, for example, true
2119 * for the GtkIconSource passed to the GtkStyle::render_icon()
2120 * virtual function. The reference count on the pixbuf is
2123 * Return value: source pixbuf
2126 gtk_icon_source_get_pixbuf (const GtkIconSource *source)
2128 g_return_val_if_fail (source != NULL, NULL);
2130 if (source->type == GTK_ICON_SOURCE_PIXBUF)
2131 return source->source.pixbuf;
2132 else if (source->type == GTK_ICON_SOURCE_FILENAME)
2133 return source->filename_pixbuf;
2139 * gtk_icon_source_set_direction_wildcarded:
2140 * @source: a #GtkIconSource
2141 * @setting: %TRUE to wildcard the text direction
2143 * If the text direction is wildcarded, this source can be used
2144 * as the base image for an icon in any #GtkTextDirection.
2145 * If the text direction is not wildcarded, then the
2146 * text direction the icon source applies to should be set
2147 * with gtk_icon_source_set_direction(), and the icon source
2148 * will only be used with that text direction.
2150 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2151 * wildcarded sources, and will use an exact match when possible.
2154 gtk_icon_source_set_direction_wildcarded (GtkIconSource *source,
2157 g_return_if_fail (source != NULL);
2159 source->any_direction = setting != FALSE;
2163 * gtk_icon_source_set_state_wildcarded:
2164 * @source: a #GtkIconSource
2165 * @setting: %TRUE to wildcard the widget state
2167 * If the widget state is wildcarded, this source can be used as the
2168 * base image for an icon in any #GtkStateType. If the widget state
2169 * is not wildcarded, then the state the source applies to should be
2170 * set with gtk_icon_source_set_state() and the icon source will
2171 * only be used with that specific state.
2173 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2174 * wildcarded sources, and will use an exact match when possible.
2176 * #GtkIconSet will normally transform wildcarded source images to
2177 * produce an appropriate icon for a given state, for example
2178 * lightening an image on prelight, but will not modify source images
2179 * that match exactly.
2182 gtk_icon_source_set_state_wildcarded (GtkIconSource *source,
2185 g_return_if_fail (source != NULL);
2187 source->any_state = setting != FALSE;
2192 * gtk_icon_source_set_size_wildcarded:
2193 * @source: a #GtkIconSource
2194 * @setting: %TRUE to wildcard the widget state
2196 * If the icon size is wildcarded, this source can be used as the base
2197 * image for an icon of any size. If the size is not wildcarded, then
2198 * the size the source applies to should be set with
2199 * gtk_icon_source_set_size() and the icon source will only be used
2200 * with that specific size.
2202 * #GtkIconSet prefers non-wildcarded sources (exact matches) over
2203 * wildcarded sources, and will use an exact match when possible.
2205 * #GtkIconSet will normally scale wildcarded source images to produce
2206 * an appropriate icon at a given size, but will not change the size
2207 * of source images that match exactly.
2210 gtk_icon_source_set_size_wildcarded (GtkIconSource *source,
2213 g_return_if_fail (source != NULL);
2215 source->any_size = setting != FALSE;
2219 * gtk_icon_source_get_size_wildcarded:
2220 * @source: a #GtkIconSource
2222 * Gets the value set by gtk_icon_source_set_size_wildcarded().
2224 * Return value: %TRUE if this icon source is a base for any icon size variant
2227 gtk_icon_source_get_size_wildcarded (const GtkIconSource *source)
2229 g_return_val_if_fail (source != NULL, TRUE);
2231 return source->any_size;
2235 * gtk_icon_source_get_state_wildcarded:
2236 * @source: a #GtkIconSource
2238 * Gets the value set by gtk_icon_source_set_state_wildcarded().
2240 * Return value: %TRUE if this icon source is a base for any widget state variant
2243 gtk_icon_source_get_state_wildcarded (const GtkIconSource *source)
2245 g_return_val_if_fail (source != NULL, TRUE);
2247 return source->any_state;
2251 * gtk_icon_source_get_direction_wildcarded:
2252 * @source: a #GtkIconSource
2254 * Gets the value set by gtk_icon_source_set_direction_wildcarded().
2256 * Return value: %TRUE if this icon source is a base for any text direction variant
2259 gtk_icon_source_get_direction_wildcarded (const GtkIconSource *source)
2261 g_return_val_if_fail (source != NULL, TRUE);
2263 return source->any_direction;
2267 * gtk_icon_source_set_direction:
2268 * @source: a #GtkIconSource
2269 * @direction: text direction this source applies to
2271 * Sets the text direction this icon source is intended to be used
2274 * Setting the text direction on an icon source makes no difference
2275 * if the text direction is wildcarded. Therefore, you should usually
2276 * call gtk_icon_source_set_direction_wildcarded() to un-wildcard it
2277 * in addition to calling this function.
2280 gtk_icon_source_set_direction (GtkIconSource *source,
2281 GtkTextDirection direction)
2283 g_return_if_fail (source != NULL);
2285 source->direction = direction;
2289 * gtk_icon_source_set_state:
2290 * @source: a #GtkIconSource
2291 * @state: widget state this source applies to
2293 * Sets the widget state this icon source is intended to be used
2296 * Setting the widget state on an icon source makes no difference
2297 * if the state is wildcarded. Therefore, you should usually
2298 * call gtk_icon_source_set_state_wildcarded() to un-wildcard it
2299 * in addition to calling this function.
2302 gtk_icon_source_set_state (GtkIconSource *source,
2305 g_return_if_fail (source != NULL);
2307 source->state = state;
2311 * gtk_icon_source_set_size:
2312 * @source: a #GtkIconSource
2313 * @size: (type int): icon size this source applies to
2315 * Sets the icon size this icon source is intended to be used
2318 * Setting the icon size on an icon source makes no difference
2319 * if the size is wildcarded. Therefore, you should usually
2320 * call gtk_icon_source_set_size_wildcarded() to un-wildcard it
2321 * in addition to calling this function.
2324 gtk_icon_source_set_size (GtkIconSource *source,
2327 g_return_if_fail (source != NULL);
2329 source->size = size;
2333 * gtk_icon_source_get_direction:
2334 * @source: a #GtkIconSource
2336 * Obtains the text direction this icon source applies to. The return
2337 * value is only useful/meaningful if the text direction is <emphasis>not</emphasis>
2340 * Return value: text direction this source matches
2343 gtk_icon_source_get_direction (const GtkIconSource *source)
2345 g_return_val_if_fail (source != NULL, 0);
2347 return source->direction;
2351 * gtk_icon_source_get_state:
2352 * @source: a #GtkIconSource
2354 * Obtains the widget state this icon source applies to. The return
2355 * value is only useful/meaningful if the widget state is <emphasis>not</emphasis>
2358 * Return value: widget state this source matches
2361 gtk_icon_source_get_state (const GtkIconSource *source)
2363 g_return_val_if_fail (source != NULL, 0);
2365 return source->state;
2369 * gtk_icon_source_get_size:
2370 * @source: a #GtkIconSource
2372 * Obtains the icon size this source applies to. The return value
2373 * is only useful/meaningful if the icon size is <emphasis>not</emphasis> wildcarded.
2375 * Return value: (type int): icon size this source matches.
2378 gtk_icon_source_get_size (const GtkIconSource *source)
2380 g_return_val_if_fail (source != NULL, 0);
2382 return source->size;
2385 #define NUM_CACHED_ICONS 8
2387 typedef struct _CachedIcon CachedIcon;
2391 /* These must all match to use the cached pixbuf.
2392 * If any don't match, we must re-render the pixbuf.
2395 GtkTextDirection direction;
2403 ensure_cache_up_to_date (GtkIconSet *icon_set)
2405 if (icon_set->cache_serial != cache_serial)
2407 clear_cache (icon_set, TRUE);
2408 icon_set->cache_serial = cache_serial;
2413 cached_icon_free (CachedIcon *icon)
2415 g_object_unref (icon->pixbuf);
2418 g_object_unref (icon->style);
2424 find_in_cache (GtkIconSet *icon_set,
2426 GtkTextDirection direction,
2433 ensure_cache_up_to_date (icon_set);
2436 tmp_list = icon_set->cache;
2437 while (tmp_list != NULL)
2439 CachedIcon *icon = tmp_list->data;
2441 if (icon->style == style &&
2442 icon->direction == direction &&
2443 icon->state == state &&
2444 (size == (GtkIconSize)-1 || icon->size == size))
2448 /* Move this icon to the front of the list. */
2449 prev->next = tmp_list->next;
2450 tmp_list->next = icon_set->cache;
2451 icon_set->cache = tmp_list;
2454 return icon->pixbuf;
2458 tmp_list = g_slist_next (tmp_list);
2465 add_to_cache (GtkIconSet *icon_set,
2467 GtkTextDirection direction,
2474 ensure_cache_up_to_date (icon_set);
2476 g_object_ref (pixbuf);
2478 /* We have to ref the style, since if the style was finalized
2479 * its address could be reused by another style, creating a
2484 g_object_ref (style);
2486 icon = g_new (CachedIcon, 1);
2487 icon_set->cache = g_slist_prepend (icon_set->cache, icon);
2488 icon_set->cache_size++;
2490 icon->style = style;
2491 icon->direction = direction;
2492 icon->state = state;
2494 icon->pixbuf = pixbuf;
2497 attach_to_style (icon_set, icon->style);
2499 if (icon_set->cache_size >= NUM_CACHED_ICONS)
2501 /* Remove oldest item in the cache */
2504 tmp_list = icon_set->cache;
2506 /* Find next-to-last link */
2507 g_assert (NUM_CACHED_ICONS > 2);
2508 while (tmp_list->next->next)
2509 tmp_list = tmp_list->next;
2511 g_assert (tmp_list != NULL);
2512 g_assert (tmp_list->next != NULL);
2513 g_assert (tmp_list->next->next == NULL);
2515 /* Free the last icon */
2516 icon = tmp_list->next->data;
2518 g_slist_free (tmp_list->next);
2519 tmp_list->next = NULL;
2521 cached_icon_free (icon);
2526 clear_cache (GtkIconSet *icon_set,
2527 gboolean style_detach)
2529 GSList *cache, *tmp_list;
2530 GtkStyle *last_style = NULL;
2532 cache = icon_set->cache;
2533 icon_set->cache = NULL;
2534 icon_set->cache_size = 0;
2536 while (tmp_list != NULL)
2538 CachedIcon *icon = tmp_list->data;
2542 /* simple optimization for the case where the cache
2543 * contains contiguous icons from the same style.
2544 * it's safe to call detach_from_style more than
2545 * once on the same style though.
2547 if (last_style != icon->style)
2549 detach_from_style (icon_set, icon->style);
2550 last_style = icon->style;
2554 cached_icon_free (icon);
2556 tmp_list = g_slist_next (tmp_list);
2559 g_slist_free (cache);
2563 copy_cache (GtkIconSet *icon_set,
2564 GtkIconSet *copy_recipient)
2567 GSList *copy = NULL;
2569 ensure_cache_up_to_date (icon_set);
2571 tmp_list = icon_set->cache;
2572 while (tmp_list != NULL)
2574 CachedIcon *icon = tmp_list->data;
2575 CachedIcon *icon_copy = g_new (CachedIcon, 1);
2579 if (icon_copy->style)
2581 attach_to_style (copy_recipient, icon_copy->style);
2582 g_object_ref (icon_copy->style);
2585 g_object_ref (icon_copy->pixbuf);
2587 icon_copy->size = icon->size;
2589 copy = g_slist_prepend (copy, icon_copy);
2591 tmp_list = g_slist_next (tmp_list);
2594 return g_slist_reverse (copy);
2598 attach_to_style (GtkIconSet *icon_set,
2603 table = g_object_get_qdata (G_OBJECT (style),
2604 g_quark_try_string ("gtk-style-icon-sets"));
2608 table = g_hash_table_new (NULL, NULL);
2609 g_object_set_qdata_full (G_OBJECT (style),
2610 g_quark_from_static_string ("gtk-style-icon-sets"),
2615 g_hash_table_insert (table, icon_set, icon_set);
2619 detach_from_style (GtkIconSet *icon_set,
2624 table = g_object_get_qdata (G_OBJECT (style),
2625 g_quark_try_string ("gtk-style-icon-sets"));
2628 g_hash_table_remove (table, icon_set);
2632 iconsets_foreach (gpointer key,
2636 GtkIconSet *icon_set = key;
2638 /* We only need to remove cache entries for the given style;
2639 * but that complicates things because in destroy notify
2640 * we don't know which style got destroyed, and 95% of the
2641 * time all cache entries will have the same style,
2642 * so this is faster anyway.
2645 clear_cache (icon_set, FALSE);
2649 style_dnotify (gpointer data)
2651 GHashTable *table = data;
2653 g_hash_table_foreach (table, iconsets_foreach, NULL);
2655 g_hash_table_destroy (table);
2658 /* This allows the icon set to detect that its cache is out of date. */
2660 _gtk_icon_set_invalidate_caches (void)
2666 * _gtk_icon_factory_list_ids:
2668 * Gets all known IDs stored in an existing icon factory.
2669 * The strings in the returned list aren't copied.
2670 * The list itself should be freed.
2672 * Return value: List of ids in icon factories
2675 _gtk_icon_factory_list_ids (void)
2682 _gtk_icon_factory_ensure_default_icons ();
2684 tmp_list = all_icon_factories;
2685 while (tmp_list != NULL)
2688 GtkIconFactory *factory = GTK_ICON_FACTORY (tmp_list->data);
2689 GtkIconFactoryPriv *priv = factory->priv;
2691 these_ids = g_hash_table_get_keys (priv->icons);
2693 ids = g_list_concat (ids, these_ids);
2695 tmp_list = g_slist_next (tmp_list);
2705 } IconFactoryParserData;
2711 GtkTextDirection direction;
2714 } IconSourceParserData;
2717 icon_source_start_element (GMarkupParseContext *context,
2718 const gchar *element_name,
2719 const gchar **names,
2720 const gchar **values,
2725 gchar *stock_id = NULL;
2726 gchar *filename = NULL;
2727 gchar *icon_name = NULL;
2729 gint direction = -1;
2731 IconFactoryParserData *parser_data;
2732 IconSourceParserData *source_data;
2734 GQuark error_domain;
2736 parser_data = (IconFactoryParserData*)user_data;
2738 if (!parser_data->in_source)
2740 if (strcmp (element_name, "sources") != 0)
2742 error_msg = g_strdup_printf ("Unexpected element %s, expected <sources>", element_name);
2743 error_domain = GTK_BUILDER_ERROR_INVALID_TAG;
2746 parser_data->in_source = TRUE;
2751 if (strcmp (element_name, "source") != 0)
2753 error_msg = g_strdup_printf ("Unexpected element %s, expected <source>", element_name);
2754 error_domain = GTK_BUILDER_ERROR_INVALID_TAG;
2759 for (i = 0; names[i]; i++)
2761 if (strcmp (names[i], "stock-id") == 0)
2762 stock_id = g_strdup (values[i]);
2763 else if (strcmp (names[i], "filename") == 0)
2764 filename = g_strdup (values[i]);
2765 else if (strcmp (names[i], "icon-name") == 0)
2766 icon_name = g_strdup (values[i]);
2767 else if (strcmp (names[i], "size") == 0)
2769 if (!_gtk_builder_enum_from_string (GTK_TYPE_ICON_SIZE,
2775 else if (strcmp (names[i], "direction") == 0)
2777 if (!_gtk_builder_enum_from_string (GTK_TYPE_TEXT_DIRECTION,
2783 else if (strcmp (names[i], "state") == 0)
2785 if (!_gtk_builder_enum_from_string (GTK_TYPE_STATE_TYPE,
2793 error_msg = g_strdup_printf ("'%s' is not a valid attribute of <%s>",
2794 names[i], "source");
2795 error_domain = GTK_BUILDER_ERROR_INVALID_ATTRIBUTE;
2802 error_msg = g_strdup_printf ("<source> requires a stock_id");
2803 error_domain = GTK_BUILDER_ERROR_MISSING_ATTRIBUTE;
2807 source_data = g_slice_new (IconSourceParserData);
2808 source_data->stock_id = stock_id;
2809 source_data->filename = filename;
2810 source_data->icon_name = icon_name;
2811 source_data->size = size;
2812 source_data->direction = direction;
2813 source_data->state = state;
2815 parser_data->sources = g_slist_prepend (parser_data->sources, source_data);
2821 gint line_number, char_number;
2823 g_markup_parse_context_get_position (context,
2827 tmp = g_strdup_printf ("%s:%d:%d %s", "input",
2828 line_number, char_number, error_msg);
2830 g_set_error_literal (error,
2835 g_warning ("%s", tmp);
2845 static const GMarkupParser icon_source_parser =
2847 icon_source_start_element,
2851 gtk_icon_factory_buildable_custom_tag_start (GtkBuildable *buildable,
2852 GtkBuilder *builder,
2854 const gchar *tagname,
2855 GMarkupParser *parser,
2858 g_assert (buildable);
2860 if (strcmp (tagname, "sources") == 0)
2862 IconFactoryParserData *parser_data;
2864 parser_data = g_slice_new0 (IconFactoryParserData);
2865 *parser = icon_source_parser;
2866 *data = parser_data;
2873 gtk_icon_factory_buildable_custom_tag_end (GtkBuildable *buildable,
2874 GtkBuilder *builder,
2876 const gchar *tagname,
2877 gpointer *user_data)
2879 GtkIconFactory *icon_factory;
2881 icon_factory = GTK_ICON_FACTORY (buildable);
2883 if (strcmp (tagname, "sources") == 0)
2885 IconFactoryParserData *parser_data;
2886 GtkIconSource *icon_source;
2887 GtkIconSet *icon_set;
2890 parser_data = (IconFactoryParserData*)user_data;
2892 for (l = parser_data->sources; l; l = l->next)
2894 IconSourceParserData *source_data = l->data;
2896 icon_set = gtk_icon_factory_lookup (icon_factory, source_data->stock_id);
2899 icon_set = gtk_icon_set_new ();
2900 gtk_icon_factory_add (icon_factory, source_data->stock_id, icon_set);
2901 gtk_icon_set_unref (icon_set);
2904 icon_source = gtk_icon_source_new ();
2906 if (source_data->filename)
2909 filename = _gtk_builder_get_absolute_filename (builder, source_data->filename);
2910 gtk_icon_source_set_filename (icon_source, filename);
2913 if (source_data->icon_name)
2914 gtk_icon_source_set_icon_name (icon_source, source_data->icon_name);
2915 if (source_data->size != -1)
2917 gtk_icon_source_set_size (icon_source, source_data->size);
2918 gtk_icon_source_set_size_wildcarded (icon_source, FALSE);
2920 if (source_data->direction != -1)
2922 gtk_icon_source_set_direction (icon_source, source_data->direction);
2923 gtk_icon_source_set_direction_wildcarded (icon_source, FALSE);
2925 if (source_data->state != -1)
2927 gtk_icon_source_set_state (icon_source, source_data->state);
2928 gtk_icon_source_set_state_wildcarded (icon_source, FALSE);
2931 /* Inline source_add() to avoid creating a copy */
2932 g_assert (icon_source->type != GTK_ICON_SOURCE_EMPTY);
2933 icon_set->sources = g_slist_insert_sorted (icon_set->sources,
2935 icon_source_compare);
2937 g_free (source_data->stock_id);
2938 g_free (source_data->filename);
2939 g_free (source_data->icon_name);
2940 g_slice_free (IconSourceParserData, source_data);
2942 g_slist_free (parser_data->sources);
2943 g_slice_free (IconFactoryParserData, parser_data);
2945 /* TODO: Add an attribute/tag to prevent this.
2946 * Usually it's the right thing to do though.
2948 gtk_icon_factory_add_default (icon_factory);
2952 #if defined (G_OS_WIN32) && !defined (_WIN64)
2954 /* DLL ABI stability backward compatibility versions */
2956 #undef gtk_icon_source_set_filename
2959 gtk_icon_source_set_filename (GtkIconSource *source,
2960 const gchar *filename)
2962 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
2964 gtk_icon_source_set_filename_utf8 (source, utf8_filename);
2966 g_free (utf8_filename);
2969 #undef gtk_icon_source_get_filename
2971 G_CONST_RETURN gchar*
2972 gtk_icon_source_get_filename (const GtkIconSource *source)
2974 g_return_val_if_fail (source != NULL, NULL);
2976 if (source->type == GTK_ICON_SOURCE_FILENAME)
2977 return source->cp_filename;