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"
33 * @short_description: Icon theming support
35 * @see_also: #GIcon, #GLoadableIcon
37 * #GThemedIcon is an implementation of #GIcon that supports icon themes.
38 * #GThemedIcon contains a list of all of the icons present in an icon
39 * theme, so that icons can be looked up quickly. #GThemedIcon does
40 * not provide actual pixmaps for icons, just the icon names.
41 * Ideally something like gtk_icon_theme_choose_icon() should be used to
42 * resolve the list of names so that fallback icons work nicely with
43 * themes that inherit other themes.
46 static void g_themed_icon_icon_iface_init (GIconIface *iface);
50 GObject parent_instance;
55 struct _GThemedIconClass
57 GObjectClass parent_class;
60 G_DEFINE_TYPE_WITH_CODE (GThemedIcon, g_themed_icon, G_TYPE_OBJECT,
61 G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
62 g_themed_icon_icon_iface_init))
65 g_themed_icon_finalize (GObject *object)
69 themed = G_THEMED_ICON (object);
71 g_strfreev (themed->names);
73 if (G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize)
74 (*G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize) (object);
78 g_themed_icon_class_init (GThemedIconClass *klass)
80 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
82 gobject_class->finalize = g_themed_icon_finalize;
86 g_themed_icon_init (GThemedIcon *themed)
92 * @iconname: a string containing an icon name.
94 * Creates a new themed icon for @iconname.
96 * Returns: a new #GThemedIcon.
99 g_themed_icon_new (const char *iconname)
103 g_return_val_if_fail (iconname != NULL, NULL);
105 themed = g_object_new (G_TYPE_THEMED_ICON, NULL);
106 themed->names = g_new (char *, 2);
107 themed->names[0] = g_strdup (iconname);
108 themed->names[1] = NULL;
110 return G_ICON (themed);
114 * g_themed_icon_new_from_names:
115 * @iconnames: an array of strings containing icon names.
116 * @len: the number of elements in the @iconnames array.
118 * Creates a new themed icon for @iconnames.
120 * Returns: a new #GThemedIcon.
123 g_themed_icon_new_from_names (char **iconnames,
129 g_return_val_if_fail (iconnames != NULL, NULL);
131 themed = g_object_new (G_TYPE_THEMED_ICON, NULL);
133 themed->names = g_strdupv (iconnames);
136 themed->names = g_new (char *, len + 1);
137 for (i = 0; i < len; i++)
138 themed->names[i] = g_strdup (iconnames[i]);
139 themed->names[i] = NULL;
143 return G_ICON (themed);
147 * g_themed_icon_new_with_default_fallbacks:
148 * @iconname: a string containing an icon name
150 * Creates a new themed icon for @iconname, and all the names
151 * that can be created by shortening @iconname at '-' characters.
153 * In the following example, @icon1 and @icon2 are equivalent:
155 * const char *names[] = {
156 * "gnome-dev-cdrom-audio",
162 * icon1 = g_themed_icon_new_from_names (names, 4);
163 * icon2 = g_themed_icon_new_with_default_fallbacks ("gnome-dev-cdrom-audio");
166 * Returns: a new #GThemedIcon.
169 g_themed_icon_new_with_default_fallbacks (const char *iconname)
177 g_return_val_if_fail (iconname != NULL, NULL);
179 themed = g_object_new (G_TYPE_THEMED_ICON, NULL);
190 themed->names = g_new (char *, dashes + 1 + 1);
192 themed->names[i++] = last = g_strdup (iconname);
194 while ((dashp = strrchr (last, '-')) != NULL)
195 themed->names[i++] = last = g_strndup (last, dashp - last);
197 themed->names[i++] = NULL;
199 return G_ICON (themed);
204 * g_themed_icon_get_names:
205 * @icon: a #GThemedIcon.
207 * Gets the names of icons from within @icon.
209 * Returns: a list of icon names.
212 g_themed_icon_get_names (GThemedIcon *icon)
214 g_return_val_if_fail (G_IS_THEMED_ICON (icon), NULL);
215 return (const char * const *)icon->names;
219 * g_themed_icon_append_name:
220 * @icon: a #GThemedIcon
221 * @iconname: name of icon to append to list of icons from within @icon.
223 * Append a name to the list of icons from within @icon.
226 g_themed_icon_append_name (GThemedIcon *icon, const char *iconname)
231 g_return_if_fail (G_IS_THEMED_ICON (icon));
232 g_return_if_fail (iconname != NULL);
234 num_names = g_strv_length (icon->names);
235 icon->names = g_realloc (icon->names, sizeof (char*) * (num_names + 2));
236 icon->names[num_names] = g_strdup (iconname);
237 icon->names[num_names + 1] = NULL;
241 g_themed_icon_hash (GIcon *icon)
243 GThemedIcon *themed = G_THEMED_ICON (icon);
249 for (i = 0; themed->names[i] != NULL; i++)
250 hash ^= g_str_hash (themed->names[i]);
256 g_themed_icon_equal (GIcon *icon1,
259 GThemedIcon *themed1 = G_THEMED_ICON (icon1);
260 GThemedIcon *themed2 = G_THEMED_ICON (icon2);
263 for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
265 if (!g_str_equal (themed1->names[i], themed2->names[i]))
269 return themed1->names[i] == NULL && themed2->names[i] == NULL;
273 g_themed_icon_icon_iface_init (GIconIface *iface)
275 iface->hash = g_themed_icon_hash;
276 iface->equal = g_themed_icon_equal;
279 #define __G_THEMED_ICON_C__
280 #include "gioaliasdef.c"