Add g_themed_icon_new_with_default_fallbacks
[platform/upstream/glib.git] / gio / gthemedicon.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26
27 #include "gthemedicon.h"
28
29 #include "gioalias.h"
30
31 /**
32  * SECTION:gthemedicon
33  * @short_description: Icon theming support
34  * @include: gio.h
35  * @see_also: #GIcon, #GLoadableIcon
36  *
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.
44  **/
45
46 static void g_themed_icon_icon_iface_init (GIconIface *iface);
47
48 struct _GThemedIcon
49 {
50   GObject parent_instance;
51   
52   char **names;
53 };
54
55 struct _GThemedIconClass
56 {
57   GObjectClass parent_class;
58 };
59
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))
63   
64 static void
65 g_themed_icon_finalize (GObject *object)
66 {
67   GThemedIcon *themed;
68
69   themed = G_THEMED_ICON (object);
70
71   g_strfreev (themed->names);
72   
73   if (G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize)
74     (*G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize) (object);
75 }
76
77 static void
78 g_themed_icon_class_init (GThemedIconClass *klass)
79 {
80   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
81   
82   gobject_class->finalize = g_themed_icon_finalize;
83 }
84
85 static void
86 g_themed_icon_init (GThemedIcon *themed)
87 {
88 }
89
90 /**
91  * g_themed_icon_new:
92  * @iconname: a string containing an icon name.
93  * 
94  * Creates a new themed icon for @iconname.
95  * 
96  * Returns: a new #GThemedIcon.
97  **/
98 GIcon *
99 g_themed_icon_new (const char *iconname)
100 {
101   GThemedIcon *themed;
102
103   g_return_val_if_fail (iconname != NULL, NULL);
104
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;
109   
110   return G_ICON (themed);
111 }
112
113 /**
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.
117  * 
118  * Creates a new themed icon for @iconnames.
119  * 
120  * Returns: a new #GThemedIcon.
121  **/
122 GIcon *
123 g_themed_icon_new_from_names (char **iconnames, 
124                               int    len)
125 {
126   GThemedIcon *themed;
127   int i;
128
129   g_return_val_if_fail (iconnames != NULL, NULL);
130   
131   themed = g_object_new (G_TYPE_THEMED_ICON, NULL);
132   if (len == -1)
133     themed->names = g_strdupv (iconnames);
134   else
135     {
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;
140     }
141   
142   
143   return G_ICON (themed);
144 }
145
146 GIcon *
147 g_themed_icon_new_with_default_fallbacks (const char *iconname)
148 {
149   GThemedIcon *themed;
150   int i, dashes;
151   const char *p;
152   char *dashp;
153   char *last;
154
155   g_return_val_if_fail (iconname != NULL, NULL);
156   
157   themed = g_object_new (G_TYPE_THEMED_ICON, NULL);
158
159   dashes = 0;
160   p = iconname;
161   while (*p)
162     {
163       if (*p == '-')
164         dashes++;
165       p++;
166     }
167
168   themed->names = g_new (char *, dashes + 1 + 1);
169   i = 0;
170   themed->names[i++] = last = g_strdup (iconname);
171
172   while ((dashp = strrchr (last, '-')) != NULL)
173     themed->names[i++] = last = g_strndup (last, dashp - last);
174   
175   themed->names[i++] = NULL;
176
177   return G_ICON (themed);
178 }
179
180
181 /**
182  * g_themed_icon_get_names:
183  * @icon: a #GThemedIcon.
184  * 
185  * Gets the names of icons from within @icon.
186  * 
187  * Returns: a list of icon names.
188  **/
189 const char * const *
190 g_themed_icon_get_names (GThemedIcon *icon)
191 {
192   g_return_val_if_fail (G_IS_THEMED_ICON (icon), NULL);
193   return (const char * const *)icon->names;
194 }
195
196 static guint
197 g_themed_icon_hash (GIcon *icon)
198 {
199   GThemedIcon *themed = G_THEMED_ICON (icon);
200   guint hash;
201   int i;
202
203   hash = 0;
204
205   for (i = 0; themed->names[i] != NULL; i++)
206     hash ^= g_str_hash (themed->names[i]);
207   
208   return hash;
209 }
210
211 static gboolean
212 g_themed_icon_equal (GIcon *icon1,
213                      GIcon *icon2)
214 {
215   GThemedIcon *themed1 = G_THEMED_ICON (icon1);
216   GThemedIcon *themed2 = G_THEMED_ICON (icon2);
217   int i;
218
219   for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
220     {
221       if (!g_str_equal (themed1->names[i], themed2->names[i]))
222         return FALSE;
223     }
224
225   return themed1->names[i] == NULL && themed2->names[i] == NULL;
226 }
227
228 static void
229 g_themed_icon_icon_iface_init (GIconIface *iface)
230 {
231   iface->hash = g_themed_icon_hash;
232   iface->equal = g_themed_icon_equal;
233 }
234
235 #define __G_THEMED_ICON_C__
236 #include "gioaliasdef.c"