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 G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize (object);
189 g_themed_icon_class_init (GThemedIconClass *klass)
191 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
193 gobject_class->finalize = g_themed_icon_finalize;
194 gobject_class->constructed = g_themed_icon_constructed;
195 gobject_class->set_property = g_themed_icon_set_property;
196 gobject_class->get_property = g_themed_icon_get_property;
203 g_object_class_install_property (gobject_class, PROP_NAME,
204 g_param_spec_string ("name",
206 _("The name of the icon"),
208 G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
213 * A %NULL-terminated array of icon names.
215 g_object_class_install_property (gobject_class, PROP_NAMES,
216 g_param_spec_boxed ("names",
218 _("An array containing the icon names"),
220 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
223 * GThemedIcon:use-default-fallbacks:
225 * Whether to use the default fallbacks found by shortening the icon name
226 * at '-' characters. If the "names" array has more than one element,
227 * ignores any past the first.
229 * For example, if the icon name was "gnome-dev-cdrom-audio", the array
233 * "gnome-dev-cdrom-audio",
241 g_object_class_install_property (gobject_class, PROP_USE_DEFAULT_FALLBACKS,
242 g_param_spec_boolean ("use-default-fallbacks",
243 _("use default fallbacks"),
244 _("Whether to use default fallbacks found by shortening the name at '-' characters. Ignores names after the first if multiple names are given."),
246 G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
250 g_themed_icon_init (GThemedIcon *themed)
252 themed->names = NULL;
257 * @iconname: a string containing an icon name.
259 * Creates a new themed icon for @iconname.
261 * Returns: a new #GThemedIcon.
264 g_themed_icon_new (const char *iconname)
266 g_return_val_if_fail (iconname != NULL, NULL);
268 return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, NULL));
272 * g_themed_icon_new_from_names:
273 * @iconnames: an array of strings containing icon names.
274 * @len: the length of the @iconnames array, or -1 if @iconnames is
277 * Creates a new themed icon for @iconnames.
279 * Returns: a new #GThemedIcon
282 g_themed_icon_new_from_names (char **iconnames,
287 g_return_val_if_fail (iconnames != NULL, NULL);
294 names = g_new (char *, len + 1);
296 for (i = 0; i < len; i++)
297 names[i] = iconnames[i];
301 icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", names, NULL));
306 icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", iconnames, NULL));
312 * g_themed_icon_new_with_default_fallbacks:
313 * @iconname: a string containing an icon name
315 * Creates a new themed icon for @iconname, and all the names
316 * that can be created by shortening @iconname at '-' characters.
318 * In the following example, @icon1 and @icon2 are equivalent:
320 * const char *names[] = {
321 * "gnome-dev-cdrom-audio",
327 * icon1 = g_themed_icon_new_from_names (names, 4);
328 * icon2 = g_themed_icon_new_with_default_fallbacks ("gnome-dev-cdrom-audio");
331 * Returns: a new #GThemedIcon.
334 g_themed_icon_new_with_default_fallbacks (const char *iconname)
336 g_return_val_if_fail (iconname != NULL, NULL);
338 return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, "use-default-fallbacks", TRUE, NULL));
343 * g_themed_icon_get_names:
344 * @icon: a #GThemedIcon.
346 * Gets the names of icons from within @icon.
348 * Returns: a list of icon names.
351 g_themed_icon_get_names (GThemedIcon *icon)
353 g_return_val_if_fail (G_IS_THEMED_ICON (icon), NULL);
354 return (const char * const *)icon->names;
358 * g_themed_icon_append_name:
359 * @icon: a #GThemedIcon
360 * @iconname: name of icon to append to list of icons from within @icon.
362 * Append a name to the list of icons from within @icon.
365 * Note that doing so invalidates the hash computed by prior calls
370 g_themed_icon_append_name (GThemedIcon *icon,
371 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_prepend_name:
388 * @icon: a #GThemedIcon
389 * @iconname: name of icon to prepend to list of icons from within @icon.
391 * Prepend a name to the list of icons from within @icon.
394 * Note that doing so invalidates the hash computed by prior calls
401 g_themed_icon_prepend_name (GThemedIcon *icon,
402 const char *iconname)
408 g_return_if_fail (G_IS_THEMED_ICON (icon));
409 g_return_if_fail (iconname != NULL);
411 num_names = g_strv_length (icon->names);
412 names = g_new (char*, num_names + 2);
413 for (i = 0; icon->names[i]; i++)
414 names[i + 1] = icon->names[i];
415 names[0] = g_strdup (iconname);
416 names[num_names + 1] = NULL;
418 g_free (icon->names);
421 g_object_notify (G_OBJECT (icon), "names");
425 g_themed_icon_hash (GIcon *icon)
427 GThemedIcon *themed = G_THEMED_ICON (icon);
433 for (i = 0; themed->names[i] != NULL; i++)
434 hash ^= g_str_hash (themed->names[i]);
440 g_themed_icon_equal (GIcon *icon1,
443 GThemedIcon *themed1 = G_THEMED_ICON (icon1);
444 GThemedIcon *themed2 = G_THEMED_ICON (icon2);
447 for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
449 if (!g_str_equal (themed1->names[i], themed2->names[i]))
453 return themed1->names[i] == NULL && themed2->names[i] == NULL;
457 g_themed_icon_icon_iface_init (GIconIface *iface)
459 iface->hash = g_themed_icon_hash;
460 iface->equal = g_themed_icon_equal;
463 #define __G_THEMED_ICON_C__
464 #include "gioaliasdef.c"