Added better gtk-doc comments.
[platform/upstream/atk.git] / atk / atkstateset.c
1 /* ATK -  Accessibility Toolkit
2  * Copyright 2001 Sun Microsystems Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <glib-object.h>
21
22 #include "atkobject.h"
23 #include "atkstateset.h"
24
25 #define ATK_STATE(state_enum)             ((AtkState)(1 << ((guint64)(state_enum)%64)))
26
27 struct _AtkRealStateSet
28 {
29   GObject parent;
30
31   AtkState state;
32 };
33
34 typedef struct _AtkRealStateSet      AtkRealStateSet;
35
36 static void            atk_state_set_class_init       (AtkStateSetClass  *klass);
37
38 GType
39 atk_state_set_get_type (void)
40 {
41   static GType type = 0;
42
43   if (!type)
44     {
45       static const GTypeInfo typeInfo =
46       {
47         sizeof (AtkStateSetClass),
48         (GBaseInitFunc) NULL,
49         (GBaseFinalizeFunc) NULL,
50         (GClassInitFunc) atk_state_set_class_init,
51         (GClassFinalizeFunc) NULL,
52         NULL,
53         sizeof (AtkRealStateSet),
54         0,
55         (GInstanceInitFunc) NULL,
56       } ;
57       type = g_type_register_static (G_TYPE_OBJECT, "AtkStateSet", &typeInfo, 0) ;
58     }
59   return type;
60 }
61
62 static void
63 atk_state_set_class_init (AtkStateSetClass *klass)
64 {
65 }
66
67 /**
68  * atk_state_set_new:
69  * 
70  * Creates a new empty state set.
71  * 
72  * Returns: a new #AtkStateSet 
73  **/
74 AtkStateSet*
75 atk_state_set_new (void)
76 {
77   return (AtkStateSet*) g_object_new (ATK_TYPE_STATE_SET, NULL);
78 }
79
80 /**
81  * atk_state_set_is_empty:
82  * @set: a #AtkStateType
83  *
84  * Checks whether the state set is empty, i.e. has no states set.
85  *
86  * Returns: %TRUE if @set has no states set
87  **/
88 gboolean
89 atk_state_set_is_empty (AtkStateSet   *set)
90 {
91   AtkRealStateSet *real_set;
92   g_return_val_if_fail (set != NULL, FALSE);
93   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
94
95   real_set = (AtkRealStateSet *)set;
96
97   if (real_set->state)
98     return TRUE;
99   else
100     return FALSE;
101 }
102
103 /**
104  * atk_state_set_add_state:
105  * @set: a #AtkStateSet
106  * @type: a #AtkStateType
107  *
108  * Add a new state for the specified type to the current state set if
109  * it is not already present
110  *
111  * Returns: %TRUE if  the state for @type is not already in @set.
112  **/
113 gboolean
114 atk_state_set_add_state (AtkStateSet   *set,
115                          AtkStateType  type)
116 {
117   AtkRealStateSet *real_set;
118   g_return_val_if_fail (set != NULL, FALSE);
119   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
120
121   real_set = (AtkRealStateSet *)set;
122
123   if (real_set->state & ATK_STATE (type))
124     return FALSE;
125   else
126   {
127     real_set->state |= ATK_STATE (type);
128     return TRUE;
129   }
130 }
131 /**
132  * atk_state_set_add_states:
133  * @set: a #AtkStateSet
134  * @types: a array of #AtkStateType
135  * @n_types: The number of elements in the array
136  *
137  * Add the states for the specified types to the current state set
138  **/
139 void
140 atk_state_set_add_states (AtkStateSet   *set,
141                           AtkStateType  *types,
142                           gint          n_types)
143 {
144   AtkRealStateSet *real_set;
145   gint     i;
146   g_return_if_fail (set != NULL);
147   g_return_if_fail (ATK_IS_STATE_SET (set));
148
149   real_set = (AtkRealStateSet *)set;
150
151   for (i = 0; i < n_types; i++)
152   {
153     real_set->state |= ATK_STATE (types[i]);
154   }
155 }
156
157 /**
158  * atk_state_set_clear_states:
159  * @set: a #AtkStateSet
160  *
161  * Removes all states from the state set.
162  **/
163 void
164 atk_state_set_clear_states (AtkStateSet   *set)
165 {
166   AtkRealStateSet *real_set;
167   g_return_if_fail (set != NULL);
168   g_return_if_fail (ATK_IS_STATE_SET (set));
169
170   real_set = (AtkRealStateSet *)set;
171
172   real_set->state = 0;
173 }
174
175 /**
176  * atk_state_set_contains_state:
177  * @set: a #AtkStateSet
178  * @type: a #AtkStateType
179  *
180  * Checks whether the state for the specified type is in the specified set.
181  *
182  * Returns: %TRUE if @type is the state type is in @set.
183  **/
184 gboolean
185 atk_state_set_contains_state (AtkStateSet   *set,
186                               AtkStateType  type)
187 {
188   AtkRealStateSet *real_set;
189   g_return_val_if_fail (set != NULL, FALSE);
190   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
191
192   real_set = (AtkRealStateSet *)set;
193
194   if (real_set->state & ATK_STATE (type))
195     return TRUE;
196   else
197     return FALSE;
198 }
199
200 /**
201  * atk_state_set_contains_states:
202  * @set: a #AtkStateSet
203  * @types: a array of #AtkStateType
204  * @n_types: The number of elements in the array
205  *
206  * Checks whether the states for all the specified types are in the 
207  * specified set.
208  *
209  * Returns: %TRUE if all the states for @type are in @set.
210  **/
211 gboolean
212 atk_state_set_contains_states (AtkStateSet   *set,
213                                AtkStateType  *types,
214                                gint          n_types)
215 {
216   AtkRealStateSet *real_set;
217   gint i;
218   g_return_val_if_fail (set != NULL, FALSE);
219   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
220
221   real_set = (AtkRealStateSet *)set;
222
223   for (i = 0; i < n_types; i++)
224   {
225     if (!(real_set->state & ATK_STATE (types[i])))
226       return FALSE;
227   }
228   return TRUE;
229 }
230
231 /**
232  * atk_state_set_remove_state:
233  * @set: a #AtkStateSet
234  * @type: a #AtkType
235  *
236  * Removes the state for the specified type from the state set.
237  *
238  * Returns: %TRUE if @type was the state type is in @set.
239  **/
240 gboolean
241 atk_state_set_remove_state (AtkStateSet  *set,
242                             AtkStateType type)
243 {
244   AtkRealStateSet *real_set;
245   g_return_val_if_fail (set != NULL, FALSE);
246   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
247
248   real_set = (AtkRealStateSet *)set;
249
250   if (real_set->state & ATK_STATE (type))
251   {
252     real_set->state ^= ATK_STATE (type);
253     return TRUE;
254   }
255   else
256     return FALSE;
257 }
258
259 /**
260  * atk_state_set_and_sets:
261  * @set: a #AtkStateSet
262  * @compare_set: another #AtkStateSet
263  *
264  * Constructs the intersection of the two sets, returning NULL if the
265  * intersection is empty.
266  *
267  * Returns: a new #AtkStateSet which is the intersection of the two sets.
268  **/
269 AtkStateSet*
270 atk_state_set_and_sets (AtkStateSet  *set,
271                         AtkStateSet  *compare_set)
272 {
273   AtkRealStateSet *real_set, *real_compare_set;
274   AtkStateSet *return_set = NULL;
275   AtkState state;
276
277   g_return_val_if_fail (set != NULL, FALSE);
278   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
279   g_return_val_if_fail (compare_set != NULL, FALSE);
280   g_return_val_if_fail (ATK_IS_STATE_SET (compare_set), FALSE);
281
282   real_set = (AtkRealStateSet *)set;
283   real_compare_set = (AtkRealStateSet *)compare_set;
284
285   state = real_set->state & real_compare_set->state;
286   if (state)
287   {
288     return_set = atk_state_set_new();
289     ((AtkRealStateSet *) return_set)->state = state;
290   }
291   return return_set;
292 }
293
294 /**
295  * atk_state_set_or_sets:
296  * @set: a #AtkStateSet
297  * @compare_set: another #AtkStateSet
298  *
299  * Constructs the union of the two sets.
300  *
301  * Returns: a new #AtkStateSet which is the union of the two sets.
302  **/
303 AtkStateSet*
304 atk_state_set_or_sets (AtkStateSet  *set,
305                        AtkStateSet  *compare_set)
306 {
307   AtkRealStateSet *real_set, *real_compare_set;
308   AtkStateSet *return_set = NULL;
309   AtkState state;
310
311   g_return_val_if_fail (set != NULL, FALSE);
312   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
313   g_return_val_if_fail (compare_set != NULL, FALSE);
314   g_return_val_if_fail (ATK_IS_STATE_SET (compare_set), FALSE);
315
316   real_set = (AtkRealStateSet *)set;
317   real_compare_set = (AtkRealStateSet *)compare_set;
318
319   state = real_set->state | real_compare_set->state;
320
321   return_set = atk_state_set_new();
322   ((AtkRealStateSet *) return_set)->state = state;
323
324   return return_set;
325 }
326
327 /**
328  * atk_state_set_xor_sets:
329  * @set: a #AtkStateSet
330  * @compare_set: another #AtkStateSet
331  *
332  * Constructs the xor of the two sets, returing NULL is empty. The set returned by this operation contains the4 sattes in exactly one of the two sets.
333  *
334  * Returns: a new #AtkStateSet which contains the states which are in exactly one of ht two sets.
335  **/
336 AtkStateSet*
337 atk_state_set_xor_sets (AtkStateSet  *set,
338                         AtkStateSet  *compare_set)
339 {
340   AtkRealStateSet *real_set, *real_compare_set;
341   AtkStateSet *return_set = NULL;
342   AtkState state, state1, state2;
343
344   g_return_val_if_fail (set != NULL, FALSE);
345   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
346   g_return_val_if_fail (compare_set != NULL, FALSE);
347   g_return_val_if_fail (ATK_IS_STATE_SET (compare_set), FALSE);
348
349   real_set = (AtkRealStateSet *)set;
350   real_compare_set = (AtkRealStateSet *)compare_set;
351
352   state1 = real_set->state & (~real_compare_set->state);
353   state2 = (~real_set->state) & real_compare_set->state;
354   state = state1 | state2;
355
356   if (state)
357   {
358     return_set = atk_state_set_new();
359     ((AtkRealStateSet *) return_set)->state = state;
360   }
361   return return_set;
362 }