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"
34 * @short_description: Icon theming support
36 * @see_also: #GIcon, #GLoadableIcon
38 * #GThemedIcon is an implementation of #GIcon that supports icon themes.
39 * #GThemedIcon contains a list of all of the icons present in an icon
40 * theme, so that icons can be looked up quickly. #GThemedIcon does
41 * not provide actual pixmaps for icons, just the icon names.
42 * Ideally something like gtk_icon_theme_choose_icon() should be used to
43 * resolve the list of names so that fallback icons work nicely with
44 * themes that inherit other themes.
47 static void g_themed_icon_icon_iface_init (GIconIface *iface);
51 GObject parent_instance;
54 gboolean use_default_fallbacks;
57 struct _GThemedIconClass
59 GObjectClass parent_class;
67 PROP_USE_DEFAULT_FALLBACKS
70 G_DEFINE_TYPE_WITH_CODE (GThemedIcon, g_themed_icon, G_TYPE_OBJECT,
71 G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
72 g_themed_icon_icon_iface_init))
75 g_themed_icon_get_property (GObject *object,
80 GThemedIcon *icon = G_THEMED_ICON (object);
85 g_value_set_boxed (value, icon->names);
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
94 g_themed_icon_set_property (GObject *object,
99 GThemedIcon *icon = G_THEMED_ICON (object);
105 g_strfreev (icon->names);
106 icon->names = g_new (char *, 2);
107 icon->names[0] = g_value_dup_string (value);
108 icon->names[1] = NULL;
113 g_strfreev (icon->names);
114 icon->names = g_value_dup_boxed (value);
117 case PROP_USE_DEFAULT_FALLBACKS:
118 icon->use_default_fallbacks = g_value_get_boolean (value);
122 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127 g_themed_icon_constructed (GObject *object)
129 GThemedIcon *themed = G_THEMED_ICON (object);
131 g_return_if_fail (themed->names != NULL && themed->names[0] != NULL);
133 if (themed->use_default_fallbacks)
135 int i = 0, dashes = 0;
140 p = themed->names[0];
148 last = g_strdup (themed->names[0]);
150 g_strfreev (themed->names);
152 themed->names = g_new (char *, dashes + 1 + 1);
153 themed->names[i++] = last;
155 while ((dashp = strrchr (last, '-')) != NULL)
156 themed->names[i++] = last = g_strndup (last, dashp - last);
158 themed->names[i++] = NULL;
163 g_themed_icon_finalize (GObject *object)
167 themed = G_THEMED_ICON (object);
169 g_strfreev (themed->names);
171 if (G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize)
172 (*G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize) (object);
176 g_themed_icon_class_init (GThemedIconClass *klass)
178 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
180 gobject_class->finalize = g_themed_icon_finalize;
181 gobject_class->constructed = g_themed_icon_constructed;
182 gobject_class->set_property = g_themed_icon_set_property;
183 gobject_class->get_property = g_themed_icon_get_property;
190 g_object_class_install_property (gobject_class, PROP_NAMES,
191 g_param_spec_string ("name",
193 _("The name of the icon"),
195 G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
200 * A %NULL-terminated array of icon names.
202 g_object_class_install_property (gobject_class, PROP_NAMES,
203 g_param_spec_boxed ("names",
205 _("An array containing the icon names"),
207 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
210 * GThemedIcon:use-default-fallbacks:
212 * Whether to use the default fallbacks found by shortening the icon name
213 * at '-' characters. If the "names" array has more than one element,
214 * ignores any past the first.
216 * For example, if the icon name was "gnome-dev-cdrom-audio", the array
220 * "gnome-dev-cdrom-audio",
228 g_object_class_install_property (gobject_class, PROP_USE_DEFAULT_FALLBACKS,
229 g_param_spec_boolean ("use-default-fallbacks",
230 _("use default fallbacks"),
231 _("Whether to use default fallbacks found by shortening the name at '-' characters. Ignores names after the first if multiple names are given."),
233 G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
237 g_themed_icon_init (GThemedIcon *themed)
239 themed->names = NULL;
244 * @iconname: a string containing an icon name.
246 * Creates a new themed icon for @iconname.
248 * Returns: a new #GThemedIcon.
251 g_themed_icon_new (const char *iconname)
253 g_return_val_if_fail (iconname != NULL, NULL);
255 return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, NULL));
259 * g_themed_icon_new_from_names:
260 * @iconnames: an array of strings containing icon names.
261 * @len: the length of the @iconnames array, or -1 if @iconnames is
264 * Creates a new themed icon for @iconnames.
266 * Returns: a new #GThemedIcon
269 g_themed_icon_new_from_names (char **iconnames,
274 g_return_val_if_fail (iconnames != NULL, NULL);
281 names = g_malloc (len + 1);
283 for (i = 0; i < len; i++)
284 names[i] = iconnames[i];
288 icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", names, NULL));
293 icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", iconnames, NULL));
299 * g_themed_icon_new_with_default_fallbacks:
300 * @iconname: a string containing an icon name
302 * Creates a new themed icon for @iconname, and all the names
303 * that can be created by shortening @iconname at '-' characters.
305 * In the following example, @icon1 and @icon2 are equivalent:
307 * const char *names[] = {
308 * "gnome-dev-cdrom-audio",
314 * icon1 = g_themed_icon_new_from_names (names, 4);
315 * icon2 = g_themed_icon_new_with_default_fallbacks ("gnome-dev-cdrom-audio");
318 * Returns: a new #GThemedIcon.
321 g_themed_icon_new_with_default_fallbacks (const char *iconname)
323 g_return_val_if_fail (iconname != NULL, NULL);
325 return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, "use-default-fallbacks", TRUE, NULL));
330 * g_themed_icon_get_names:
331 * @icon: a #GThemedIcon.
333 * Gets the names of icons from within @icon.
335 * Returns: a list of icon names.
338 g_themed_icon_get_names (GThemedIcon *icon)
340 g_return_val_if_fail (G_IS_THEMED_ICON (icon), NULL);
341 return (const char * const *)icon->names;
345 * g_themed_icon_append_name:
346 * @icon: a #GThemedIcon
347 * @iconname: name of icon to append to list of icons from within @icon.
349 * Append a name to the list of icons from within @icon.
352 * Note that doing so invalidates the hash computed by prior calls
357 g_themed_icon_append_name (GThemedIcon *icon, const char *iconname)
361 g_return_if_fail (G_IS_THEMED_ICON (icon));
362 g_return_if_fail (iconname != NULL);
364 num_names = g_strv_length (icon->names);
365 icon->names = g_realloc (icon->names, sizeof (char*) * (num_names + 2));
366 icon->names[num_names] = g_strdup (iconname);
367 icon->names[num_names + 1] = NULL;
369 g_object_notify (G_OBJECT (icon), "names");
373 g_themed_icon_hash (GIcon *icon)
375 GThemedIcon *themed = G_THEMED_ICON (icon);
381 for (i = 0; themed->names[i] != NULL; i++)
382 hash ^= g_str_hash (themed->names[i]);
388 g_themed_icon_equal (GIcon *icon1,
391 GThemedIcon *themed1 = G_THEMED_ICON (icon1);
392 GThemedIcon *themed2 = G_THEMED_ICON (icon2);
395 for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
397 if (!g_str_equal (themed1->names[i], themed2->names[i]))
401 return themed1->names[i] == NULL && themed2->names[i] == NULL;
405 g_themed_icon_icon_iface_init (GIconIface *iface)
407 iface->hash = g_themed_icon_hash;
408 iface->equal = g_themed_icon_equal;
411 #define __G_THEMED_ICON_C__
412 #include "gioaliasdef.c"