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