1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
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;
55 gboolean use_default_fallbacks;
58 struct _GThemedIconClass
60 GObjectClass parent_class;
68 PROP_USE_DEFAULT_FALLBACKS
71 G_DEFINE_TYPE_WITH_CODE (GThemedIcon, g_themed_icon, G_TYPE_OBJECT,
72 G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
73 g_themed_icon_icon_iface_init))
76 g_themed_icon_get_property (GObject *object,
81 GThemedIcon *icon = G_THEMED_ICON (object);
86 g_value_set_boxed (value, icon->names);
90 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
95 g_themed_icon_set_property (GObject *object,
100 GThemedIcon *icon = G_THEMED_ICON (object);
107 name = g_value_get_string (value);
113 g_strfreev (icon->names);
115 icon->names = g_new (char *, 2);
116 icon->names[0] = g_strdup (name);
117 icon->names[1] = NULL;
121 names = g_value_dup_boxed (value);
127 g_strfreev (icon->names);
132 case PROP_USE_DEFAULT_FALLBACKS:
133 icon->use_default_fallbacks = g_value_get_boolean (value);
137 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142 g_themed_icon_constructed (GObject *object)
144 GThemedIcon *themed = G_THEMED_ICON (object);
146 g_return_if_fail (themed->names != NULL && themed->names[0] != NULL);
148 if (themed->use_default_fallbacks)
150 int i = 0, dashes = 0;
155 p = themed->names[0];
163 last = g_strdup (themed->names[0]);
165 g_strfreev (themed->names);
167 themed->names = g_new (char *, dashes + 1 + 1);
168 themed->names[i++] = last;
170 while ((dashp = strrchr (last, '-')) != NULL)
171 themed->names[i++] = last = g_strndup (last, dashp - last);
173 themed->names[i++] = NULL;
178 g_themed_icon_finalize (GObject *object)
182 themed = G_THEMED_ICON (object);
184 g_strfreev (themed->names);
186 G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize (object);
190 g_themed_icon_class_init (GThemedIconClass *klass)
192 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
194 gobject_class->finalize = g_themed_icon_finalize;
195 gobject_class->constructed = g_themed_icon_constructed;
196 gobject_class->set_property = g_themed_icon_set_property;
197 gobject_class->get_property = g_themed_icon_get_property;
204 g_object_class_install_property (gobject_class, PROP_NAME,
205 g_param_spec_string ("name",
207 _("The name of the icon"),
209 G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
214 * A %NULL-terminated array of icon names.
216 g_object_class_install_property (gobject_class, PROP_NAMES,
217 g_param_spec_boxed ("names",
219 _("An array containing the icon names"),
221 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
224 * GThemedIcon:use-default-fallbacks:
226 * Whether to use the default fallbacks found by shortening the icon name
227 * at '-' characters. If the "names" array has more than one element,
228 * ignores any past the first.
230 * For example, if the icon name was "gnome-dev-cdrom-audio", the array
234 * "gnome-dev-cdrom-audio",
242 g_object_class_install_property (gobject_class, PROP_USE_DEFAULT_FALLBACKS,
243 g_param_spec_boolean ("use-default-fallbacks",
244 _("use default fallbacks"),
245 _("Whether to use default fallbacks found by shortening the name at '-' characters. Ignores names after the first if multiple names are given."),
247 G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
251 g_themed_icon_init (GThemedIcon *themed)
253 themed->names = NULL;
258 * @iconname: a string containing an icon name.
260 * Creates a new themed icon for @iconname.
262 * Returns: a new #GThemedIcon.
265 g_themed_icon_new (const char *iconname)
267 g_return_val_if_fail (iconname != NULL, NULL);
269 return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, NULL));
273 * g_themed_icon_new_from_names:
274 * @iconnames: an array of strings containing icon names.
275 * @len: the length of the @iconnames array, or -1 if @iconnames is
278 * Creates a new themed icon for @iconnames.
280 * Returns: a new #GThemedIcon
283 g_themed_icon_new_from_names (char **iconnames,
288 g_return_val_if_fail (iconnames != NULL, NULL);
295 names = g_new (char *, len + 1);
297 for (i = 0; i < len; i++)
298 names[i] = iconnames[i];
302 icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", names, NULL));
307 icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", iconnames, NULL));
313 * g_themed_icon_new_with_default_fallbacks:
314 * @iconname: a string containing an icon name
316 * Creates a new themed icon for @iconname, and all the names
317 * that can be created by shortening @iconname at '-' characters.
319 * In the following example, @icon1 and @icon2 are equivalent:
321 * const char *names[] = {
322 * "gnome-dev-cdrom-audio",
328 * icon1 = g_themed_icon_new_from_names (names, 4);
329 * icon2 = g_themed_icon_new_with_default_fallbacks ("gnome-dev-cdrom-audio");
332 * Returns: a new #GThemedIcon.
335 g_themed_icon_new_with_default_fallbacks (const char *iconname)
337 g_return_val_if_fail (iconname != NULL, NULL);
339 return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, "use-default-fallbacks", TRUE, NULL));
344 * g_themed_icon_get_names:
345 * @icon: a #GThemedIcon.
347 * Gets the names of icons from within @icon.
349 * Returns: a list of icon names.
352 g_themed_icon_get_names (GThemedIcon *icon)
354 g_return_val_if_fail (G_IS_THEMED_ICON (icon), NULL);
355 return (const char * const *)icon->names;
359 * g_themed_icon_append_name:
360 * @icon: a #GThemedIcon
361 * @iconname: name of icon to append to list of icons from within @icon.
363 * Append a name to the list of icons from within @icon.
366 * Note that doing so invalidates the hash computed by prior calls
371 g_themed_icon_append_name (GThemedIcon *icon,
372 const char *iconname)
376 g_return_if_fail (G_IS_THEMED_ICON (icon));
377 g_return_if_fail (iconname != NULL);
379 num_names = g_strv_length (icon->names);
380 icon->names = g_realloc (icon->names, sizeof (char*) * (num_names + 2));
381 icon->names[num_names] = g_strdup (iconname);
382 icon->names[num_names + 1] = NULL;
384 g_object_notify (G_OBJECT (icon), "names");
388 * g_themed_icon_prepend_name:
389 * @icon: a #GThemedIcon
390 * @iconname: name of icon to prepend to list of icons from within @icon.
392 * Prepend a name to the list of icons from within @icon.
395 * Note that doing so invalidates the hash computed by prior calls
402 g_themed_icon_prepend_name (GThemedIcon *icon,
403 const char *iconname)
409 g_return_if_fail (G_IS_THEMED_ICON (icon));
410 g_return_if_fail (iconname != NULL);
412 num_names = g_strv_length (icon->names);
413 names = g_new (char*, num_names + 2);
414 for (i = 0; icon->names[i]; i++)
415 names[i + 1] = icon->names[i];
416 names[0] = g_strdup (iconname);
417 names[num_names + 1] = NULL;
419 g_free (icon->names);
422 g_object_notify (G_OBJECT (icon), "names");
426 g_themed_icon_hash (GIcon *icon)
428 GThemedIcon *themed = G_THEMED_ICON (icon);
434 for (i = 0; themed->names[i] != NULL; i++)
435 hash ^= g_str_hash (themed->names[i]);
441 g_themed_icon_equal (GIcon *icon1,
444 GThemedIcon *themed1 = G_THEMED_ICON (icon1);
445 GThemedIcon *themed2 = G_THEMED_ICON (icon2);
448 for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
450 if (!g_str_equal (themed1->names[i], themed2->names[i]))
454 return themed1->names[i] == NULL && themed2->names[i] == NULL;
458 g_themed_icon_icon_iface_init (GIconIface *iface)
460 iface->hash = g_themed_icon_hash;
461 iface->equal = g_themed_icon_equal;
464 #define __G_THEMED_ICON_C__
465 #include "gioaliasdef.c"