introspection: add many missing Returns: (nullable) annotations
[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 "config.h"
21
22 #include <glib-object.h>
23
24 #include "atkobject.h"
25 #include "atkstateset.h"
26
27 /**
28  * SECTION:atkstateset
29  * @Short_description: An AtkStateSet determines a component's state set.
30  * @Title:AtkStateSet
31  *
32  * An AtkStateSet determines a component's state set. It is composed
33  * of a set of AtkStates.
34  */
35
36 #define ATK_STATE(state_enum)             ((AtkState)((guint64)1 << ((state_enum)%64)))
37
38 struct _AtkRealStateSet
39 {
40   GObject parent;
41
42   AtkState state;
43 };
44
45 typedef struct _AtkRealStateSet      AtkRealStateSet;
46
47 static void            atk_state_set_class_init       (AtkStateSetClass  *klass);
48
49 GType
50 atk_state_set_get_type (void)
51 {
52   static GType type = 0;
53
54   if (!type)
55     {
56       static const GTypeInfo typeInfo =
57       {
58         sizeof (AtkStateSetClass),
59         (GBaseInitFunc) NULL,
60         (GBaseFinalizeFunc) NULL,
61         (GClassInitFunc) atk_state_set_class_init,
62         (GClassFinalizeFunc) NULL,
63         NULL,
64         sizeof (AtkRealStateSet),
65         0,
66         (GInstanceInitFunc) NULL,
67       } ;
68       type = g_type_register_static (G_TYPE_OBJECT, "AtkStateSet", &typeInfo, 0) ;
69     }
70   return type;
71 }
72
73 static void
74 atk_state_set_class_init (AtkStateSetClass *klass)
75 {
76 }
77
78 /**
79  * atk_state_set_new:
80  * 
81  * Creates a new empty state set.
82  * 
83  * Returns: a new #AtkStateSet 
84  **/
85 AtkStateSet*
86 atk_state_set_new (void)
87 {
88   return (AtkStateSet*) g_object_new (ATK_TYPE_STATE_SET, NULL);
89 }
90
91 /**
92  * atk_state_set_is_empty:
93  * @set: an #AtkStateType
94  *
95  * Checks whether the state set is empty, i.e. has no states set.
96  *
97  * Returns: %TRUE if @set has no states set, otherwise %FALSE
98  **/
99 gboolean
100 atk_state_set_is_empty (AtkStateSet   *set)
101 {
102   AtkRealStateSet *real_set;
103   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
104
105   real_set = (AtkRealStateSet *)set;
106
107   if (real_set->state)
108     return FALSE;
109   else
110     return TRUE;
111 }
112
113 /**
114  * atk_state_set_add_state:
115  * @set: an #AtkStateSet
116  * @type: an #AtkStateType
117  *
118  * Add a new state for the specified type to the current state set if
119  * it is not already present.
120  *
121  * Returns: %TRUE if  the state for @type is not already in @set.
122  **/
123 gboolean
124 atk_state_set_add_state (AtkStateSet   *set,
125                          AtkStateType  type)
126 {
127   AtkRealStateSet *real_set;
128   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
129
130   real_set = (AtkRealStateSet *)set;
131
132   if (real_set->state & ATK_STATE (type))
133     return FALSE;
134   else
135   {
136     real_set->state |= ATK_STATE (type);
137     return TRUE;
138   }
139 }
140 /**
141  * atk_state_set_add_states:
142  * @set: an #AtkStateSet
143  * @types: (array length=n_types): an array of #AtkStateType
144  * @n_types: The number of elements in the array
145  *
146  * Add the states for the specified types to the current state set.
147  **/
148 void
149 atk_state_set_add_states (AtkStateSet   *set,
150                           AtkStateType  *types,
151                           gint          n_types)
152 {
153   AtkRealStateSet *real_set;
154   gint     i;
155   g_return_if_fail (ATK_IS_STATE_SET (set));
156
157   real_set = (AtkRealStateSet *)set;
158
159   for (i = 0; i < n_types; i++)
160   {
161     real_set->state |= ATK_STATE (types[i]);
162   }
163 }
164
165 /**
166  * atk_state_set_clear_states:
167  * @set: an #AtkStateSet
168  *
169  * Removes all states from the state set.
170  **/
171 void
172 atk_state_set_clear_states (AtkStateSet   *set)
173 {
174   AtkRealStateSet *real_set;
175   g_return_if_fail (ATK_IS_STATE_SET (set));
176
177   real_set = (AtkRealStateSet *)set;
178
179   real_set->state = 0;
180 }
181
182 /**
183  * atk_state_set_contains_state:
184  * @set: an #AtkStateSet
185  * @type: an #AtkStateType
186  *
187  * Checks whether the state for the specified type is in the specified set.
188  *
189  * Returns: %TRUE if @type is the state type is in @set.
190  **/
191 gboolean
192 atk_state_set_contains_state (AtkStateSet   *set,
193                               AtkStateType  type)
194 {
195   AtkRealStateSet *real_set;
196   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
197
198   real_set = (AtkRealStateSet *)set;
199
200   if (real_set->state & ATK_STATE (type))
201     return TRUE;
202   else
203     return FALSE;
204 }
205
206 /**
207  * atk_state_set_contains_states:
208  * @set: an #AtkStateSet
209  * @types: (array length=n_types): an array of #AtkStateType
210  * @n_types: The number of elements in the array
211  *
212  * Checks whether the states for all the specified types are in the 
213  * specified set.
214  *
215  * Returns: %TRUE if all the states for @type are in @set.
216  **/
217 gboolean
218 atk_state_set_contains_states (AtkStateSet   *set,
219                                AtkStateType  *types,
220                                gint          n_types)
221 {
222   AtkRealStateSet *real_set;
223   gint i;
224   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
225
226   real_set = (AtkRealStateSet *)set;
227
228   for (i = 0; i < n_types; i++)
229   {
230     if (!(real_set->state & ATK_STATE (types[i])))
231       return FALSE;
232   }
233   return TRUE;
234 }
235
236 /**
237  * atk_state_set_remove_state:
238  * @set: an #AtkStateSet
239  * @type: an #AtkType
240  *
241  * Removes the state for the specified type from the state set.
242  *
243  * Returns: %TRUE if @type was the state type is in @set.
244  **/
245 gboolean
246 atk_state_set_remove_state (AtkStateSet  *set,
247                             AtkStateType type)
248 {
249   AtkRealStateSet *real_set;
250   g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
251
252   real_set = (AtkRealStateSet *)set;
253
254   if (real_set->state & ATK_STATE (type))
255   {
256     real_set->state ^= ATK_STATE (type);
257     return TRUE;
258   }
259   else
260     return FALSE;
261 }
262
263 /**
264  * atk_state_set_and_sets:
265  * @set: an #AtkStateSet
266  * @compare_set: another #AtkStateSet
267  *
268  * Constructs the intersection of the two sets, returning %NULL if the
269  * intersection is empty.
270  *
271  * Returns: (transfer full): a new #AtkStateSet which is the intersection of
272  * the two sets.
273  **/
274 AtkStateSet*
275 atk_state_set_and_sets (AtkStateSet  *set,
276                         AtkStateSet  *compare_set)
277 {
278   AtkRealStateSet *real_set, *real_compare_set;
279   AtkStateSet *return_set = NULL;
280   AtkState state;
281
282   g_return_val_if_fail (ATK_IS_STATE_SET (set), NULL);
283   g_return_val_if_fail (ATK_IS_STATE_SET (compare_set), NULL);
284
285   real_set = (AtkRealStateSet *)set;
286   real_compare_set = (AtkRealStateSet *)compare_set;
287
288   state = real_set->state & real_compare_set->state;
289   if (state)
290   {
291     return_set = atk_state_set_new();
292     ((AtkRealStateSet *) return_set)->state = state;
293   }
294   return return_set;
295 }
296
297 /**
298  * atk_state_set_or_sets:
299  * @set: an #AtkStateSet
300  * @compare_set: another #AtkStateSet
301  *
302  * Constructs the union of the two sets.
303  *
304  * Returns: (nullable) (transfer full): a new #AtkStateSet which is
305  * the union of the two sets, returning %NULL is empty.
306  **/
307 AtkStateSet*
308 atk_state_set_or_sets (AtkStateSet  *set,
309                        AtkStateSet  *compare_set)
310 {
311   AtkRealStateSet *real_set, *real_compare_set;
312   AtkStateSet *return_set = NULL;
313   AtkState state;
314
315   g_return_val_if_fail (ATK_IS_STATE_SET (set), NULL);
316   g_return_val_if_fail (ATK_IS_STATE_SET (compare_set), NULL);
317
318   real_set = (AtkRealStateSet *)set;
319   real_compare_set = (AtkRealStateSet *)compare_set;
320
321   state = real_set->state | real_compare_set->state;
322
323   if (state)
324   {
325     return_set = atk_state_set_new();
326     ((AtkRealStateSet *) return_set)->state = state;
327   }
328
329   return return_set;
330 }
331
332 /**
333  * atk_state_set_xor_sets:
334  * @set: an #AtkStateSet
335  * @compare_set: another #AtkStateSet
336  *
337  * Constructs the exclusive-or of the two sets, returning %NULL is empty.
338  * The set returned by this operation contains the states in exactly
339  * one of the two sets.
340  *
341  * Returns: (transfer full): a new #AtkStateSet which contains the states
342  * which are in exactly one of the two sets.
343  **/
344 AtkStateSet*
345 atk_state_set_xor_sets (AtkStateSet  *set,
346                         AtkStateSet  *compare_set)
347 {
348   AtkRealStateSet *real_set, *real_compare_set;
349   AtkStateSet *return_set = NULL;
350   AtkState state, state1, state2;
351
352   g_return_val_if_fail (ATK_IS_STATE_SET (set), NULL);
353   g_return_val_if_fail (ATK_IS_STATE_SET (compare_set), NULL);
354
355   real_set = (AtkRealStateSet *)set;
356   real_compare_set = (AtkRealStateSet *)compare_set;
357
358   state1 = real_set->state & (~real_compare_set->state);
359   state2 = (~real_set->state) & real_compare_set->state;
360   state = state1 | state2;
361
362   if (state)
363   {
364     return_set = atk_state_set_new();
365     ((AtkRealStateSet *) return_set)->state = state;
366   }
367   return return_set;
368 }