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