Fix up a bunch of details in the docs.
[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 "gthemedicon.h"
26
27 #include "gioalias.h"
28
29 /**
30  * SECTION:gthemedicon
31  * @short_description: Icon theming support
32  * @see_also: #GIcon, #GLoadableIcon
33  *
34  * #GThemedIcon is an implementation of #GIcon that supports icon themes.
35  * #GThemedIcon contains a list of all of the icons present in an icon
36  * theme, so that icons can be looked up quickly. #GThemedIcon does
37  * not provide actual pixmaps for icons, just the icon names.
38  * Ideally something like gtk_icon_theme_choose_icon() should be used to
39  * resolve the list of names so that fallback icons work nicely with
40  * themes that inherit other themes.
41  **/
42
43 static void g_themed_icon_icon_iface_init (GIconIface *iface);
44
45 struct _GThemedIcon
46 {
47   GObject parent_instance;
48   
49   char **names;
50 };
51
52 struct _GThemedIconClass
53 {
54   GObjectClass parent_class;
55 };
56
57 G_DEFINE_TYPE_WITH_CODE (GThemedIcon, g_themed_icon, G_TYPE_OBJECT,
58                          G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
59                                                 g_themed_icon_icon_iface_init))
60   
61 static void
62 g_themed_icon_finalize (GObject *object)
63 {
64   GThemedIcon *themed;
65
66   themed = G_THEMED_ICON (object);
67
68   g_strfreev (themed->names);
69   
70   if (G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize)
71     (*G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize) (object);
72 }
73
74 static void
75 g_themed_icon_class_init (GThemedIconClass *klass)
76 {
77   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
78   
79   gobject_class->finalize = g_themed_icon_finalize;
80 }
81
82 static void
83 g_themed_icon_init (GThemedIcon *themed)
84 {
85 }
86
87 /**
88  * g_themed_icon_new:
89  * @iconname: a string containing an icon name.
90  * 
91  * Creates a new themed icon for @iconname.
92  * 
93  * Returns: a new #GThemedIcon.
94  **/
95 GIcon *
96 g_themed_icon_new (const char *iconname)
97 {
98   GThemedIcon *themed;
99
100   g_return_val_if_fail (iconname != NULL, NULL);
101
102   themed = g_object_new (G_TYPE_THEMED_ICON, NULL);
103   themed->names = g_new (char *, 2);
104   themed->names[0] = g_strdup (iconname);
105   themed->names[1] = NULL;
106   
107   return G_ICON (themed);
108 }
109
110 /**
111  * g_themed_icon_new_from_names:
112  * @iconnames: an array of strings containing icon names.
113  * @len: the number of elements in the @iconnames array.
114  * 
115  * Creates a new themed icon for @iconnames.
116  * 
117  * Returns: a new #GThemedIcon.
118  **/
119 GIcon *
120 g_themed_icon_new_from_names (char **iconnames, 
121                               int    len)
122 {
123   GThemedIcon *themed;
124   int i;
125
126   g_return_val_if_fail (iconnames != NULL, NULL);
127   
128   themed = g_object_new (G_TYPE_THEMED_ICON, NULL);
129   if (len == -1)
130     themed->names = g_strdupv (iconnames);
131   else
132     {
133       themed->names = g_new (char *, len + 1);
134       for (i = 0; i < len; i++)
135         themed->names[i] = g_strdup (iconnames[i]);
136       themed->names[i] = NULL;
137     }
138   
139   
140   return G_ICON (themed);
141 }
142
143 /**
144  * g_themed_icon_get_names:
145  * @icon: a #GThemedIcon.
146  * 
147  * Gets the names of icons from within @icon.
148  * 
149  * Returns: a list of icon names.
150  **/
151 const char * const *
152 g_themed_icon_get_names (GThemedIcon *icon)
153 {
154   g_return_val_if_fail (G_IS_THEMED_ICON (icon), NULL);
155   return (const char * const *)icon->names;
156 }
157
158 static guint
159 g_themed_icon_hash (GIcon *icon)
160 {
161   GThemedIcon *themed = G_THEMED_ICON (icon);
162   guint hash;
163   int i;
164
165   hash = 0;
166
167   for (i = 0; themed->names[i] != NULL; i++)
168     hash ^= g_str_hash (themed->names[i]);
169   
170   return hash;
171 }
172
173 static gboolean
174 g_themed_icon_equal (GIcon *icon1,
175                      GIcon *icon2)
176 {
177   GThemedIcon *themed1 = G_THEMED_ICON (icon1);
178   GThemedIcon *themed2 = G_THEMED_ICON (icon2);
179   int i;
180
181   for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
182     {
183       if (!g_str_equal (themed1->names[i], themed2->names[i]))
184         return FALSE;
185     }
186
187   return themed1->names[i] == NULL && themed2->names[i] == NULL;
188 }
189
190 static void
191 g_themed_icon_icon_iface_init (GIconIface *iface)
192 {
193   iface->hash = g_themed_icon_hash;
194   iface->equal = g_themed_icon_equal;
195 }
196
197 #define __G_THEMED_ICON_C__
198 #include "gioaliasdef.c"