1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
27 #include "gthemedicon.h"
35 * @short_description: Icon theming support
37 * @see_also: #GIcon, #GLoadableIcon
39 * #GThemedIcon is an implementation of #GIcon that supports icon themes.
40 * #GThemedIcon contains a list of all of the icons present in an icon
41 * theme, so that icons can be looked up quickly. #GThemedIcon does
42 * not provide actual pixmaps for icons, just the icon names.
43 * Ideally something like gtk_icon_theme_choose_icon() should be used to
44 * resolve the list of names so that fallback icons work nicely with
45 * themes that inherit other themes.
48 static void g_themed_icon_icon_iface_init (GIconIface *iface);
52 GObject parent_instance;
56 gboolean use_default_fallbacks;
59 struct _GThemedIconClass
61 GObjectClass parent_class;
69 PROP_USE_DEFAULT_FALLBACKS
72 static void g_themed_icon_update_names (GThemedIcon *themed);
74 G_DEFINE_TYPE_WITH_CODE (GThemedIcon, g_themed_icon, G_TYPE_OBJECT,
75 G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
76 g_themed_icon_icon_iface_init))
79 g_themed_icon_get_property (GObject *object,
84 GThemedIcon *icon = G_THEMED_ICON (object);
89 g_value_set_boxed (value, icon->init_names);
92 case PROP_USE_DEFAULT_FALLBACKS:
93 g_value_set_boolean (value, icon->use_default_fallbacks);
97 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
102 g_themed_icon_set_property (GObject *object,
107 GThemedIcon *icon = G_THEMED_ICON (object);
114 name = g_value_get_string (value);
119 if (icon->init_names)
120 g_strfreev (icon->init_names);
122 icon->init_names = g_new (char *, 2);
123 icon->init_names[0] = g_strdup (name);
124 icon->init_names[1] = NULL;
128 names = g_value_dup_boxed (value);
133 if (icon->init_names)
134 g_strfreev (icon->init_names);
136 icon->init_names = names;
139 case PROP_USE_DEFAULT_FALLBACKS:
140 icon->use_default_fallbacks = g_value_get_boolean (value);
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
149 g_themed_icon_constructed (GObject *object)
151 g_themed_icon_update_names (G_THEMED_ICON (object));
155 g_themed_icon_finalize (GObject *object)
159 themed = G_THEMED_ICON (object);
161 g_strfreev (themed->init_names);
162 g_strfreev (themed->names);
164 G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize (object);
168 g_themed_icon_class_init (GThemedIconClass *klass)
170 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
172 gobject_class->finalize = g_themed_icon_finalize;
173 gobject_class->constructed = g_themed_icon_constructed;
174 gobject_class->set_property = g_themed_icon_set_property;
175 gobject_class->get_property = g_themed_icon_get_property;
182 g_object_class_install_property (gobject_class, PROP_NAME,
183 g_param_spec_string ("name",
185 P_("The name of the icon"),
187 G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
192 * A %NULL-terminated array of icon names.
194 g_object_class_install_property (gobject_class, PROP_NAMES,
195 g_param_spec_boxed ("names",
197 P_("An array containing the icon names"),
199 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
202 * GThemedIcon:use-default-fallbacks:
204 * Whether to use the default fallbacks found by shortening the icon name
205 * at '-' characters. If the "names" array has more than one element,
206 * ignores any past the first.
208 * For example, if the icon name was "gnome-dev-cdrom-audio", the array
210 * |[<!-- language="C" -->
212 * "gnome-dev-cdrom-audio",
220 g_object_class_install_property (gobject_class, PROP_USE_DEFAULT_FALLBACKS,
221 g_param_spec_boolean ("use-default-fallbacks",
222 P_("use default fallbacks"),
223 P_("Whether to use default fallbacks found by shortening the name at “-” characters. Ignores names after the first if multiple names are given."),
225 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
229 g_themed_icon_init (GThemedIcon *themed)
231 themed->init_names = NULL;
232 themed->names = NULL;
236 * g_themed_icon_update_names:
237 * @themed: a #GThemedIcon.
239 * Update the actual icon name list, based on the requested names (from
240 * construction, or later added with g_themed_icon_prepend_name() and
241 * g_themed_icon_append_name()).
242 * The order of the list matters, indicating priority:
243 * - The first requested icon is first in priority.
244 * - If "use-default-fallbacks" is #TRUE, then it is followed by all its
245 * fallbacks (starting from top to lower context levels).
246 * - Then next requested icons, and optionally their fallbacks, follow.
247 * - Finally all the style variants (symbolic or regular, opposite to whatever
248 * is the requested style) follow in the same order.
250 * An icon is not added twice in the list if it was previously added.
252 * For instance, if requested names are:
253 * [ "some-icon-symbolic", "some-other-icon" ]
254 * and use-default-fallbacks is TRUE, the final name list shall be:
255 * [ "some-icon-symbolic", "some-symbolic", "some-other-icon",
256 * "some-other", "some", "some-icon", "some-other-icon-symbolic",
257 * "some-other-symbolic" ]
259 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon
262 g_themed_icon_update_names (GThemedIcon *themed)
265 GList *variants = NULL;
269 g_return_if_fail (themed->init_names != NULL && themed->init_names[0] != NULL);
271 for (i = 0; themed->init_names[i]; i++)
274 gboolean is_symbolic;
276 is_symbolic = g_str_has_suffix (themed->init_names[i], "-symbolic");
278 name = g_strndup (themed->init_names[i], strlen (themed->init_names[i]) - 9);
280 name = g_strdup (themed->init_names[i]);
282 if (g_list_find_custom (names, name, (GCompareFunc) g_strcmp0))
289 names = g_list_prepend (names, g_strdup (themed->init_names[i]));
291 names = g_list_prepend (names, name);
293 if (themed->use_default_fallbacks)
300 while ((dashp = strrchr (last, '-')) != NULL)
305 last = g_strndup (last, dashp - last);
309 fallback = g_strdup_printf ("%s-symbolic", last);
313 if (g_list_find_custom (names, fallback, (GCompareFunc) g_strcmp0))
318 names = g_list_prepend (names, fallback);
323 else if (is_symbolic)
326 for (iter = names; iter; iter = iter->next)
328 gchar *name = (gchar *) iter->data;
330 gboolean is_symbolic;
332 is_symbolic = g_str_has_suffix (name, "-symbolic");
334 variant = g_strndup (name, strlen (name) - 9);
336 variant = g_strdup_printf ("%s-symbolic", name);
337 if (g_list_find_custom (names, variant, (GCompareFunc) g_strcmp0) ||
338 g_list_find_custom (variants, variant, (GCompareFunc) g_strcmp0))
344 variants = g_list_prepend (variants, variant);
346 names = g_list_reverse (names);
348 g_strfreev (themed->names);
349 themed->names = g_new (char *, g_list_length (names) + g_list_length (variants) + 1);
351 for (iter = names, i = 0; iter; iter = iter->next, i++)
352 themed->names[i] = iter->data;
353 for (iter = variants; iter; iter = iter->next, i++)
354 themed->names[i] = iter->data;
355 themed->names[i] = NULL;
358 g_list_free (variants);
360 g_object_notify (G_OBJECT (themed), "names");
365 * @iconname: a string containing an icon name.
367 * Creates a new themed icon for @iconname.
369 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
372 g_themed_icon_new (const char *iconname)
374 g_return_val_if_fail (iconname != NULL, NULL);
376 return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, NULL));
380 * g_themed_icon_new_from_names:
381 * @iconnames: (array length=len): an array of strings containing icon names.
382 * @len: the length of the @iconnames array, or -1 if @iconnames is
385 * Creates a new themed icon for @iconnames.
387 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon
390 g_themed_icon_new_from_names (char **iconnames,
395 g_return_val_if_fail (iconnames != NULL, NULL);
402 names = g_new (char *, len + 1);
404 for (i = 0; i < len; i++)
405 names[i] = iconnames[i];
409 icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", names, NULL));
414 icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", iconnames, NULL));
420 * g_themed_icon_new_with_default_fallbacks:
421 * @iconname: a string containing an icon name
423 * Creates a new themed icon for @iconname, and all the names
424 * that can be created by shortening @iconname at '-' characters.
426 * In the following example, @icon1 and @icon2 are equivalent:
427 * |[<!-- language="C" -->
428 * const char *names[] = {
429 * "gnome-dev-cdrom-audio",
435 * icon1 = g_themed_icon_new_from_names (names, 4);
436 * icon2 = g_themed_icon_new_with_default_fallbacks ("gnome-dev-cdrom-audio");
439 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
442 g_themed_icon_new_with_default_fallbacks (const char *iconname)
444 g_return_val_if_fail (iconname != NULL, NULL);
446 return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, "use-default-fallbacks", TRUE, NULL));
451 * g_themed_icon_get_names:
452 * @icon: a #GThemedIcon.
454 * Gets the names of icons from within @icon.
456 * Returns: (transfer none): a list of icon names.
459 g_themed_icon_get_names (GThemedIcon *icon)
461 g_return_val_if_fail (G_IS_THEMED_ICON (icon), NULL);
462 return (const char * const *)icon->names;
466 * g_themed_icon_append_name:
467 * @icon: a #GThemedIcon
468 * @iconname: name of icon to append to list of icons from within @icon.
470 * Append a name to the list of icons from within @icon.
472 * Note that doing so invalidates the hash computed by prior calls
476 g_themed_icon_append_name (GThemedIcon *icon,
477 const char *iconname)
481 g_return_if_fail (G_IS_THEMED_ICON (icon));
482 g_return_if_fail (iconname != NULL);
484 num_names = g_strv_length (icon->init_names);
485 icon->init_names = g_realloc (icon->init_names, sizeof (char*) * (num_names + 2));
486 icon->init_names[num_names] = g_strdup (iconname);
487 icon->init_names[num_names + 1] = NULL;
489 g_themed_icon_update_names (icon);
493 * g_themed_icon_prepend_name:
494 * @icon: a #GThemedIcon
495 * @iconname: name of icon to prepend to list of icons from within @icon.
497 * Prepend a name to the list of icons from within @icon.
499 * Note that doing so invalidates the hash computed by prior calls
505 g_themed_icon_prepend_name (GThemedIcon *icon,
506 const char *iconname)
512 g_return_if_fail (G_IS_THEMED_ICON (icon));
513 g_return_if_fail (iconname != NULL);
515 num_names = g_strv_length (icon->init_names);
516 names = g_new (char*, num_names + 2);
517 for (i = 0; icon->init_names[i]; i++)
518 names[i + 1] = icon->init_names[i];
519 names[0] = g_strdup (iconname);
520 names[num_names + 1] = NULL;
522 g_free (icon->init_names);
523 icon->init_names = names;
525 g_themed_icon_update_names (icon);
529 g_themed_icon_hash (GIcon *icon)
531 GThemedIcon *themed = G_THEMED_ICON (icon);
537 for (i = 0; themed->names[i] != NULL; i++)
538 hash ^= g_str_hash (themed->names[i]);
544 g_themed_icon_equal (GIcon *icon1,
547 GThemedIcon *themed1 = G_THEMED_ICON (icon1);
548 GThemedIcon *themed2 = G_THEMED_ICON (icon2);
551 for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
553 if (!g_str_equal (themed1->names[i], themed2->names[i]))
557 return themed1->names[i] == NULL && themed2->names[i] == NULL;
562 g_themed_icon_to_tokens (GIcon *icon,
566 GThemedIcon *themed_icon = G_THEMED_ICON (icon);
569 g_return_val_if_fail (out_version != NULL, FALSE);
573 for (n = 0; themed_icon->names[n] != NULL; n++)
574 g_ptr_array_add (tokens,
575 g_strdup (themed_icon->names[n]));
581 g_themed_icon_from_tokens (gchar **tokens,
596 G_IO_ERROR_INVALID_ARGUMENT,
597 _("Can’t handle version %d of GThemedIcon encoding"),
602 names = g_new0 (gchar *, num_tokens + 1);
603 for (n = 0; n < num_tokens; n++)
604 names[n] = tokens[n];
607 icon = g_themed_icon_new_from_names (names, num_tokens);
615 g_themed_icon_serialize (GIcon *icon)
617 GThemedIcon *themed_icon = G_THEMED_ICON (icon);
619 return g_variant_new ("(sv)", "themed", g_variant_new ("^as", themed_icon->names));
623 g_themed_icon_icon_iface_init (GIconIface *iface)
625 iface->hash = g_themed_icon_hash;
626 iface->equal = g_themed_icon_equal;
627 iface->to_tokens = g_themed_icon_to_tokens;
628 iface->from_tokens = g_themed_icon_from_tokens;
629 iface->serialize = g_themed_icon_serialize;