docs: Fixed mention to atkobject methods
[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)((guint64)1 << ((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: an #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, otherwise %FALSE
87  **/
88 gboolean
89 atk_state_set_is_empty (AtkStateSet   *set)
90 {
91   AtkRealStateSet *real_set;
92   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
93
94   real_set = (AtkRealStateSet *)set;
95
96   if (real_set->state)
97     return FALSE;
98   else
99     return TRUE;
100 }
101
102 /**
103  * atk_state_set_add_state:
104  * @set: an #AtkStateSet
105  * @type: an #AtkStateType
106  *
107  * Add a new state for the specified type to the current state set if
108  * it is not already present.
109  *
110  * Returns: %TRUE if  the state for @type is not already in @set.
111  **/
112 gboolean
113 atk_state_set_add_state (AtkStateSet   *set,
114                          AtkStateType  type)
115 {
116   AtkRealStateSet *real_set;
117   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
118
119   real_set = (AtkRealStateSet *)set;
120
121   if (real_set->state & ATK_STATE (type))
122     return FALSE;
123   else
124   {
125     real_set->state |= ATK_STATE (type);
126     return TRUE;
127   }
128 }
129 /**
130  * atk_state_set_add_states:
131  * @set: an #AtkStateSet
132  * @types: (array length=n_types): an array of #AtkStateType
133  * @n_types: The number of elements in the array
134  *
135  * Add the states for the specified types to the current state set.
136  **/
137 void
138 atk_state_set_add_states (AtkStateSet   *set,
139                           AtkStateType  *types,
140                           gint          n_types)
141 {
142   AtkRealStateSet *real_set;
143   gint     i;
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: an #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 (ATK_IS_STATE_SET (set));
165
166   real_set = (AtkRealStateSet *)set;
167
168   real_set->state = 0;
169 }
170
171 /**
172  * atk_state_set_contains_state:
173  * @set: an #AtkStateSet
174  * @type: an #AtkStateType
175  *
176  * Checks whether the state for the specified type is in the specified set.
177  *
178  * Returns: %TRUE if @type is the state type is in @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 (ATK_IS_STATE_SET (set), FALSE);
186
187   real_set = (AtkRealStateSet *)set;
188
189   if (real_set->state & ATK_STATE (type))
190     return TRUE;
191   else
192     return FALSE;
193 }
194
195 /**
196  * atk_state_set_contains_states:
197  * @set: an #AtkStateSet
198  * @types: (array length=n_types): an array of #AtkStateType
199  * @n_types: The number of elements in the array
200  *
201  * Checks whether the states for all the specified types are in the 
202  * specified set.
203  *
204  * Returns: %TRUE if all the states for @type are in @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 (ATK_IS_STATE_SET (set), FALSE);
214
215   real_set = (AtkRealStateSet *)set;
216
217   for (i = 0; i < n_types; i++)
218   {
219     if (!(real_set->state & ATK_STATE (types[i])))
220       return FALSE;
221   }
222   return TRUE;
223 }
224
225 /**
226  * atk_state_set_remove_state:
227  * @set: an #AtkStateSet
228  * @type: an #AtkType
229  *
230  * Removes the state for the specified type from the state set.
231  *
232  * Returns: %TRUE if @type was the state type is in @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 (ATK_IS_STATE_SET (set), FALSE);
240
241   real_set = (AtkRealStateSet *)set;
242
243   if (real_set->state & ATK_STATE (type))
244   {
245     real_set->state ^= ATK_STATE (type);
246     return TRUE;
247   }
248   else
249     return FALSE;
250 }
251
252 /**
253  * atk_state_set_and_sets:
254  * @set: an #AtkStateSet
255  * @compare_set: another #AtkStateSet
256  *
257  * Constructs the intersection of the two sets, returning %NULL if the
258  * intersection is empty.
259  *
260  * Returns: (transfer full): a new #AtkStateSet which is the intersection of
261  * the two sets.
262  **/
263 AtkStateSet*
264 atk_state_set_and_sets (AtkStateSet  *set,
265                         AtkStateSet  *compare_set)
266 {
267   AtkRealStateSet *real_set, *real_compare_set;
268   AtkStateSet *return_set = NULL;
269   AtkState state;
270
271   g_return_val_if_fail (ATK_IS_STATE_SET (set), NULL);
272   g_return_val_if_fail (ATK_IS_STATE_SET (compare_set), NULL);
273
274   real_set = (AtkRealStateSet *)set;
275   real_compare_set = (AtkRealStateSet *)compare_set;
276
277   state = real_set->state & real_compare_set->state;
278   if (state)
279   {
280     return_set = atk_state_set_new();
281     ((AtkRealStateSet *) return_set)->state = state;
282   }
283   return return_set;
284 }
285
286 /**
287  * atk_state_set_or_sets:
288  * @set: an #AtkStateSet
289  * @compare_set: another #AtkStateSet
290  *
291  * Constructs the union of the two sets.
292  *
293  * Returns: (transfer full): a new #AtkStateSet which is the union of the two
294  * sets, returning %NULL is empty.
295  **/
296 AtkStateSet*
297 atk_state_set_or_sets (AtkStateSet  *set,
298                        AtkStateSet  *compare_set)
299 {
300   AtkRealStateSet *real_set, *real_compare_set;
301   AtkStateSet *return_set = NULL;
302   AtkState state;
303
304   g_return_val_if_fail (ATK_IS_STATE_SET (set), NULL);
305   g_return_val_if_fail (ATK_IS_STATE_SET (compare_set), NULL);
306
307   real_set = (AtkRealStateSet *)set;
308   real_compare_set = (AtkRealStateSet *)compare_set;
309
310   state = real_set->state | real_compare_set->state;
311
312   if (state)
313   {
314     return_set = atk_state_set_new();
315     ((AtkRealStateSet *) return_set)->state = state;
316   }
317
318   return return_set;
319 }
320
321 /**
322  * atk_state_set_xor_sets:
323  * @set: an #AtkStateSet
324  * @compare_set: another #AtkStateSet
325  *
326  * Constructs the exclusive-or of the two sets, returning %NULL is empty.
327  * The set returned by this operation contains the states in exactly
328  * one of the two sets.
329  *
330  * Returns: (transfer full): a new #AtkStateSet which contains the states
331  * which are in exactly one of the two sets.
332  **/
333 AtkStateSet*
334 atk_state_set_xor_sets (AtkStateSet  *set,
335                         AtkStateSet  *compare_set)
336 {
337   AtkRealStateSet *real_set, *real_compare_set;
338   AtkStateSet *return_set = NULL;
339   AtkState state, state1, state2;
340
341   g_return_val_if_fail (ATK_IS_STATE_SET (set), NULL);
342   g_return_val_if_fail (ATK_IS_STATE_SET (compare_set), NULL);
343
344   real_set = (AtkRealStateSet *)set;
345   real_compare_set = (AtkRealStateSet *)compare_set;
346
347   state1 = real_set->state & (~real_compare_set->state);
348   state2 = (~real_set->state) & real_compare_set->state;
349   state = state1 | state2;
350
351   if (state)
352   {
353     return_set = atk_state_set_new();
354     ((AtkRealStateSet *) return_set)->state = state;
355   }
356   return return_set;
357 }