6c800217f53be345cdbb68e381c77cdf066f0618
[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  * return values: a new #AtkStateSet 
70  * 
71  * Creates a new empty state set.
72  **/
73 AtkStateSet*
74 atk_state_set_new (void)
75 {
76   return (AtkStateSet*) g_object_new (ATK_TYPE_STATE_SET, NULL);
77 }
78
79 /**
80  * atk_state_set_is_empty
81  * @type: a #AtkStateType
82  * return values: %TRUE if @set has no states set
83  *
84  * Checks whether the state set is empty, i.e. has no states set.
85  **/
86 gboolean
87 atk_state_set_is_empty (AtkStateSet   *set)
88 {
89   AtkRealStateSet *real_set;
90   g_return_val_if_fail (set != NULL, FALSE);
91   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
92
93   real_set = (AtkRealStateSet *)set;
94
95   if (real_set->state)
96     return TRUE;
97   else
98     return FALSE;
99 }
100
101 /**
102  * atk_state_set_add_state
103  * @set: a #AtkStateSet
104  * @type: a #AtkStateType
105  * return values: %TRUE if  the state for @type is not already in @set.
106  *
107  * Add a new state for the specified type to the current state set if
108  * it is not already present
109  **/
110 gboolean
111 atk_state_set_add_state (AtkStateSet   *set,
112                          AtkStateType  type)
113 {
114   AtkRealStateSet *real_set;
115   g_return_val_if_fail (set != NULL, FALSE);
116   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
117
118   real_set = (AtkRealStateSet *)set;
119
120   if (real_set->state & ATK_STATE (type))
121     return FALSE;
122   else
123   {
124     real_set->state |= ATK_STATE (type);
125     return TRUE;
126   }
127 }
128 /**
129  * atk_state_set_add_states
130  * @set: a #AtkStateSet
131  * @types: a array of #AtkStateType
132  * @n_types: The number of elements in the array
133  *
134  * Add the states for the specified types to the current state set
135  **/
136 void
137 atk_state_set_add_states (AtkStateSet   *set,
138                           AtkStateType  *types,
139                           gint          n_types)
140 {
141   AtkRealStateSet *real_set;
142   gint     i;
143   g_return_if_fail (set != NULL);
144   g_return_if_fail (ATK_IS_STATE_SET (set));
145
146   real_set = (AtkRealStateSet *)set;
147
148   for (i = 0; i < n_types; i++)
149   {
150     real_set->state |= ATK_STATE (types[i]);
151   }
152 }
153
154 /**
155  * atk_state_set_clear_states
156  * @set: a #AtkStateSet
157  *
158  * Removes all states from the state set.
159  **/
160 void
161 atk_state_set_clear_states (AtkStateSet   *set)
162 {
163   AtkRealStateSet *real_set;
164   g_return_if_fail (set != NULL);
165   g_return_if_fail (ATK_IS_STATE_SET (set));
166
167   real_set = (AtkRealStateSet *)set;
168
169   real_set->state = 0;
170 }
171
172 /**
173  * atk_state_set_contains_state
174  * @set: a #AtkStateSet
175  * @type: a #AtkStateType
176  * return values: %TRUE if @type is the state type is in @set.
177  *
178  * Checks whether the state for the specified type is in the specified set.
179  **/
180 gboolean
181 atk_state_set_contains_state (AtkStateSet   *set,
182                               AtkStateType  type)
183 {
184   AtkRealStateSet *real_set;
185   g_return_val_if_fail (set != NULL, FALSE);
186   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
187
188   real_set = (AtkRealStateSet *)set;
189
190   if (real_set->state & ATK_STATE (type))
191     return TRUE;
192   else
193     return FALSE;
194 }
195
196 /**
197  * atk_state_set_contains_states
198  * @set: a #AtkStateSet
199  * @types: a array of #AtkStateType
200  * @n_types: The number of elements in the array
201  * return values: %TRUE if all the states for @type are in @set.
202  *
203  * Checks whether the states for all the specified types are in the 
204  * specified set.
205  **/
206 gboolean
207 atk_state_set_contains_states (AtkStateSet   *set,
208                                AtkStateType  *types,
209                                gint          n_types)
210 {
211   AtkRealStateSet *real_set;
212   gint i;
213   g_return_val_if_fail (set != NULL, FALSE);
214   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
215
216   real_set = (AtkRealStateSet *)set;
217
218   for (i = 0; i < n_types; i++)
219   {
220     if (!(real_set->state & ATK_STATE (types[i])))
221       return FALSE;
222   }
223   return TRUE;
224 }
225
226 /**
227  * atk_state_set_remove_state
228  * @set: a #AtkStateSet
229  * @type: a #AtkType
230  * return values: %TRUE if @type was the state type is in @set.
231  *
232  * Removes the state for the specified type from the state set.
233  **/
234 gboolean
235 atk_state_set_remove_state (AtkStateSet  *set,
236                             AtkStateType type)
237 {
238   AtkRealStateSet *real_set;
239   g_return_val_if_fail (set != NULL, FALSE);
240   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
241
242   real_set = (AtkRealStateSet *)set;
243
244   if (real_set->state & ATK_STATE (type))
245   {
246     real_set->state ^= ATK_STATE (type);
247     return TRUE;
248   }
249   else
250     return FALSE;
251 }
252
253 /**
254  * atk_state_set_and_sets
255  * @set: a #AtkStateSet
256  * @compare_set: another #AtkStateSet
257  * return values: a new #AtkStateSet which is the intersection of the two sets.
258  *
259  * Constructs the intersection of the two sets, returning NULL if the
260  * intersection is empty.
261  **/
262 AtkStateSet*
263 atk_state_set_and_sets (AtkStateSet  *set,
264                         AtkStateSet  *compare_set)
265 {
266   AtkRealStateSet *real_set, *real_compare_set;
267   AtkStateSet *return_set = NULL;
268   AtkState state;
269
270   g_return_val_if_fail (set != NULL, FALSE);
271   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
272   g_return_val_if_fail (compare_set != NULL, FALSE);
273   g_return_val_if_fail (ATK_IS_STATE_SET (compare_set), FALSE);
274
275   real_set = (AtkRealStateSet *)set;
276   real_compare_set = (AtkRealStateSet *)compare_set;
277
278   state = real_set->state & real_compare_set->state;
279   if (state)
280   {
281     return_set = atk_state_set_new();
282     ((AtkRealStateSet *) return_set)->state = state;
283   }
284   return return_set;
285 }
286
287 /**
288  * atk_state_set_or_sets
289  * @set: a #AtkStateSet
290  * @compare_set: another #AtkStateSet
291  * return values: a new #AtkStateSet which is the union of the two sets.
292  *
293  * Constructs the union of the two sets.
294  **/
295 AtkStateSet*
296 atk_state_set_or_sets (AtkStateSet  *set,
297                        AtkStateSet  *compare_set)
298 {
299   AtkRealStateSet *real_set, *real_compare_set;
300   AtkStateSet *return_set = NULL;
301   AtkState state;
302
303   g_return_val_if_fail (set != NULL, FALSE);
304   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
305   g_return_val_if_fail (compare_set != NULL, FALSE);
306   g_return_val_if_fail (ATK_IS_STATE_SET (compare_set), FALSE);
307
308   real_set = (AtkRealStateSet *)set;
309   real_compare_set = (AtkRealStateSet *)compare_set;
310
311   state = real_set->state | real_compare_set->state;
312
313   return_set = atk_state_set_new();
314   ((AtkRealStateSet *) return_set)->state = state;
315
316   return return_set;
317 }
318
319 /**
320  * atk_state_set_xor_sets
321  * @set: a #AtkStateSet
322  * @compare_set: another #AtkStateSet
323  * return values: a new #AtkStateSet which contains the states which are in exactly one of ht two sets.
324  *
325  * 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.
326  **/
327 AtkStateSet*
328 atk_state_set_xor_sets (AtkStateSet  *set,
329                         AtkStateSet  *compare_set)
330 {
331   AtkRealStateSet *real_set, *real_compare_set;
332   AtkStateSet *return_set = NULL;
333   AtkState state, state1, state2;
334
335   g_return_val_if_fail (set != NULL, FALSE);
336   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
337   g_return_val_if_fail (compare_set != NULL, FALSE);
338   g_return_val_if_fail (ATK_IS_STATE_SET (compare_set), FALSE);
339
340   real_set = (AtkRealStateSet *)set;
341   real_compare_set = (AtkRealStateSet *)compare_set;
342
343   state1 = real_set->state & (~real_compare_set->state);
344   state2 = (~real_set->state) & real_compare_set->state;
345   state = state1 | state2;
346
347   if (state)
348   {
349     return_set = atk_state_set_new();
350     ((AtkRealStateSet *) return_set)->state = state;
351   }
352   return return_set;
353 }