Release 2.15.1
[atk.git] / atk / atkstateset.c
blob1740d05a5b4b50b20a7f647c2322f6b8d44b32b7
1 /* ATK - Accessibility Toolkit
2 * Copyright 2001 Sun Microsystems Inc.
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.
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.
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.
20 #include "config.h"
22 #include <glib-object.h>
24 #include "atkobject.h"
25 #include "atkstateset.h"
27 /**
28 * SECTION:atkstateset
29 * @Short_description: An AtkStateSet determines a component's state set.
30 * @Title:AtkStateSet
32 * An AtkStateSet determines a component's state set. It is composed
33 * of a set of AtkStates.
36 #define ATK_STATE(state_enum) ((AtkState)((guint64)1 << ((state_enum)%64)))
38 struct _AtkRealStateSet
40 GObject parent;
42 AtkState state;
45 typedef struct _AtkRealStateSet AtkRealStateSet;
47 static void atk_state_set_class_init (AtkStateSetClass *klass);
49 GType
50 atk_state_set_get_type (void)
52 static GType type = 0;
54 if (!type)
56 static const GTypeInfo typeInfo =
58 sizeof (AtkStateSetClass),
59 (GBaseInitFunc) NULL,
60 (GBaseFinalizeFunc) NULL,
61 (GClassInitFunc) atk_state_set_class_init,
62 (GClassFinalizeFunc) NULL,
63 NULL,
64 sizeof (AtkRealStateSet),
66 (GInstanceInitFunc) NULL,
67 } ;
68 type = g_type_register_static (G_TYPE_OBJECT, "AtkStateSet", &typeInfo, 0) ;
70 return type;
73 static void
74 atk_state_set_class_init (AtkStateSetClass *klass)
78 /**
79 * atk_state_set_new:
81 * Creates a new empty state set.
83 * Returns: a new #AtkStateSet
84 **/
85 AtkStateSet*
86 atk_state_set_new (void)
88 return (AtkStateSet*) g_object_new (ATK_TYPE_STATE_SET, NULL);
91 /**
92 * atk_state_set_is_empty:
93 * @set: an #AtkStateType
95 * Checks whether the state set is empty, i.e. has no states set.
97 * Returns: %TRUE if @set has no states set, otherwise %FALSE
98 **/
99 gboolean
100 atk_state_set_is_empty (AtkStateSet *set)
102 AtkRealStateSet *real_set;
103 g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
105 real_set = (AtkRealStateSet *)set;
107 if (real_set->state)
108 return FALSE;
109 else
110 return TRUE;
114 * atk_state_set_add_state:
115 * @set: an #AtkStateSet
116 * @type: an #AtkStateType
118 * Add a new state for the specified type to the current state set if
119 * it is not already present.
121 * Returns: %TRUE if the state for @type is not already in @set.
123 gboolean
124 atk_state_set_add_state (AtkStateSet *set,
125 AtkStateType type)
127 AtkRealStateSet *real_set;
128 g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
130 real_set = (AtkRealStateSet *)set;
132 if (real_set->state & ATK_STATE (type))
133 return FALSE;
134 else
136 real_set->state |= ATK_STATE (type);
137 return TRUE;
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
146 * Add the states for the specified types to the current state set.
148 void
149 atk_state_set_add_states (AtkStateSet *set,
150 AtkStateType *types,
151 gint n_types)
153 AtkRealStateSet *real_set;
154 gint i;
155 g_return_if_fail (ATK_IS_STATE_SET (set));
157 real_set = (AtkRealStateSet *)set;
159 for (i = 0; i < n_types; i++)
161 real_set->state |= ATK_STATE (types[i]);
166 * atk_state_set_clear_states:
167 * @set: an #AtkStateSet
169 * Removes all states from the state set.
171 void
172 atk_state_set_clear_states (AtkStateSet *set)
174 AtkRealStateSet *real_set;
175 g_return_if_fail (ATK_IS_STATE_SET (set));
177 real_set = (AtkRealStateSet *)set;
179 real_set->state = 0;
183 * atk_state_set_contains_state:
184 * @set: an #AtkStateSet
185 * @type: an #AtkStateType
187 * Checks whether the state for the specified type is in the specified set.
189 * Returns: %TRUE if @type is the state type is in @set.
191 gboolean
192 atk_state_set_contains_state (AtkStateSet *set,
193 AtkStateType type)
195 AtkRealStateSet *real_set;
196 g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
198 real_set = (AtkRealStateSet *)set;
200 if (real_set->state & ATK_STATE (type))
201 return TRUE;
202 else
203 return FALSE;
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
212 * Checks whether the states for all the specified types are in the
213 * specified set.
215 * Returns: %TRUE if all the states for @type are in @set.
217 gboolean
218 atk_state_set_contains_states (AtkStateSet *set,
219 AtkStateType *types,
220 gint n_types)
222 AtkRealStateSet *real_set;
223 gint i;
224 g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
226 real_set = (AtkRealStateSet *)set;
228 for (i = 0; i < n_types; i++)
230 if (!(real_set->state & ATK_STATE (types[i])))
231 return FALSE;
233 return TRUE;
237 * atk_state_set_remove_state:
238 * @set: an #AtkStateSet
239 * @type: an #AtkType
241 * Removes the state for the specified type from the state set.
243 * Returns: %TRUE if @type was the state type is in @set.
245 gboolean
246 atk_state_set_remove_state (AtkStateSet *set,
247 AtkStateType type)
249 AtkRealStateSet *real_set;
250 g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
252 real_set = (AtkRealStateSet *)set;
254 if (real_set->state & ATK_STATE (type))
256 real_set->state ^= ATK_STATE (type);
257 return TRUE;
259 else
260 return FALSE;
264 * atk_state_set_and_sets:
265 * @set: an #AtkStateSet
266 * @compare_set: another #AtkStateSet
268 * Constructs the intersection of the two sets, returning %NULL if the
269 * intersection is empty.
271 * Returns: (transfer full): a new #AtkStateSet which is the intersection of
272 * the two sets.
274 AtkStateSet*
275 atk_state_set_and_sets (AtkStateSet *set,
276 AtkStateSet *compare_set)
278 AtkRealStateSet *real_set, *real_compare_set;
279 AtkStateSet *return_set = NULL;
280 AtkState state;
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);
285 real_set = (AtkRealStateSet *)set;
286 real_compare_set = (AtkRealStateSet *)compare_set;
288 state = real_set->state & real_compare_set->state;
289 if (state)
291 return_set = atk_state_set_new();
292 ((AtkRealStateSet *) return_set)->state = state;
294 return return_set;
298 * atk_state_set_or_sets:
299 * @set: an #AtkStateSet
300 * @compare_set: another #AtkStateSet
302 * Constructs the union of the two sets.
304 * Returns: (nullable) (transfer full): a new #AtkStateSet which is
305 * the union of the two sets, returning %NULL is empty.
307 AtkStateSet*
308 atk_state_set_or_sets (AtkStateSet *set,
309 AtkStateSet *compare_set)
311 AtkRealStateSet *real_set, *real_compare_set;
312 AtkStateSet *return_set = NULL;
313 AtkState state;
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);
318 real_set = (AtkRealStateSet *)set;
319 real_compare_set = (AtkRealStateSet *)compare_set;
321 state = real_set->state | real_compare_set->state;
323 if (state)
325 return_set = atk_state_set_new();
326 ((AtkRealStateSet *) return_set)->state = state;
329 return return_set;
333 * atk_state_set_xor_sets:
334 * @set: an #AtkStateSet
335 * @compare_set: another #AtkStateSet
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.
341 * Returns: (transfer full): a new #AtkStateSet which contains the states
342 * which are in exactly one of the two sets.
344 AtkStateSet*
345 atk_state_set_xor_sets (AtkStateSet *set,
346 AtkStateSet *compare_set)
348 AtkRealStateSet *real_set, *real_compare_set;
349 AtkStateSet *return_set = NULL;
350 AtkState state, state1, state2;
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);
355 real_set = (AtkRealStateSet *)set;
356 real_compare_set = (AtkRealStateSet *)compare_set;
358 state1 = real_set->state & (~real_compare_set->state);
359 state2 = (~real_set->state) & real_compare_set->state;
360 state = state1 | state2;
362 if (state)
364 return_set = atk_state_set_new();
365 ((AtkRealStateSet *) return_set)->state = state;
367 return return_set;