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);
106 name = g_value_get_string (value);
112 g_strfreev (icon->names);
114 icon->names = g_new (char *, 2);
115 icon->names[0] = g_strdup (name);
116 icon->names[1] = NULL;
120 names = g_value_dup_boxed (value);
126 g_strfreev (icon->names);
131 case PROP_USE_DEFAULT_FALLBACKS:
132 icon->use_default_fallbacks = g_value_get_boolean (value);
136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141 g_themed_icon_constructed (GObject *object)
143 GThemedIcon *themed = G_THEMED_ICON (object);
145 g_return_if_fail (themed->names != NULL && themed->names[0] != NULL);
147 if (themed->use_default_fallbacks)
149 int i = 0, dashes = 0;
154 p = themed->names[0];
162 last = g_strdup (themed->names[0]);
164 g_strfreev (themed->names);
166 themed->names = g_new (char *, dashes + 1 + 1);
167 themed->names[i++] = last;
169 while ((dashp = strrchr (last, '-')) != NULL)
170 themed->names[i++] = last = g_strndup (last, dashp - last);
172 themed->names[i++] = NULL;
177 g_themed_icon_finalize (GObject *object)
181 themed = G_THEMED_ICON (object);
183 g_strfreev (themed->names);
185 if (G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize)
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, const char *iconname)
375 g_return_if_fail (G_IS_THEMED_ICON (icon));
376 g_return_if_fail (iconname != NULL);
378 num_names = g_strv_length (icon->names);
379 icon->names = g_realloc (icon->names, sizeof (char*) * (num_names + 2));
380 icon->names[num_names] = g_strdup (iconname);
381 icon->names[num_names + 1] = NULL;
383 g_object_notify (G_OBJECT (icon), "names");
387 g_themed_icon_hash (GIcon *icon)
389 GThemedIcon *themed = G_THEMED_ICON (icon);
395 for (i = 0; themed->names[i] != NULL; i++)
396 hash ^= g_str_hash (themed->names[i]);
402 g_themed_icon_equal (GIcon *icon1,
405 GThemedIcon *themed1 = G_THEMED_ICON (icon1);
406 GThemedIcon *themed2 = G_THEMED_ICON (icon2);
409 for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
411 if (!g_str_equal (themed1->names[i], themed2->names[i]))
415 return themed1->names[i] == NULL && themed2->names[i] == NULL;
419 g_themed_icon_icon_iface_init (GIconIface *iface)
421 iface->hash = g_themed_icon_hash;
422 iface->equal = g_themed_icon_equal;
425 #define __G_THEMED_ICON_C__
426 #include "gioaliasdef.c"