summaryrefslogtreecommitdiff
path: root/atk/atkstateset.c
blob: eba179a98345af2335a66de4a4c237911aa8598f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* ATK -  Accessibility Toolkit
 * Copyright 2001 Sun Microsystems Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <glib-object.h>

#include "atkobject.h"
#include "atkstateset.h"

#define ATK_STATE(state_enum)             ((AtkState)(1 << ((guint64)(state_enum)%64)))

struct _AtkRealStateSet
{
  GObject parent;

  AtkState state;
};

typedef struct _AtkRealStateSet      AtkRealStateSet;

static void            atk_state_set_class_init       (AtkStateSetClass  *klass);

GType
atk_state_set_get_type (void)
{
  static GType type = 0;

  if (!type)
    {
      static const GTypeInfo typeInfo =
      {
        sizeof (AtkStateSetClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) atk_state_set_class_init,
        (GClassFinalizeFunc) NULL,
        NULL,
        sizeof (AtkRealStateSet),
        0,
        (GInstanceInitFunc) NULL,
      } ;
      type = g_type_register_static (G_TYPE_OBJECT, "AtkStateSet", &typeInfo, 0) ;
    }
  return type;
}

static void
atk_state_set_class_init (AtkStateSetClass *klass)
{
}

/**
 * atk_state_set_new:
 * 
 * Creates a new empty state set.
 * 
 * Returns: a new #AtkStateSet 
 **/
AtkStateSet*
atk_state_set_new (void)
{
  return (AtkStateSet*) g_object_new (ATK_TYPE_STATE_SET, NULL);
}

/**
 * atk_state_set_is_empty:
 * @set: an #AtkStateType
 *
 * Checks whether the state set is empty, i.e. has no states set.
 *
 * Returns: %TRUE if @set has no states set, otherwise %FALSE
 **/
gboolean
atk_state_set_is_empty (AtkStateSet   *set)
{
  AtkRealStateSet *real_set;
  g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);

  real_set = (AtkRealStateSet *)set;

  if (real_set->state)
    return TRUE;
  else
    return FALSE;
}

/**
 * atk_state_set_add_state:
 * @set: an #AtkStateSet
 * @type: an #AtkStateType
 *
 * Add a new state for the specified type to the current state set if
 * it is not already present.
 *
 * Returns: %TRUE if  the state for @type is not already in @set.
 **/
gboolean
atk_state_set_add_state (AtkStateSet   *set,
                         AtkStateType  type)
{
  AtkRealStateSet *real_set;
  g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);

  real_set = (AtkRealStateSet *)set;

  if (real_set->state & ATK_STATE (type))
    return FALSE;
  else
  {
    real_set->state |= ATK_STATE (type);
    return TRUE;
  }
}
/**
 * atk_state_set_add_states:
 * @set: an #AtkStateSet
 * @types: an array of #AtkStateType
 * @n_types: The number of elements in the array
 *
 * Add the states for the specified types to the current state set.
 **/
void
atk_state_set_add_states (AtkStateSet   *set,
                          AtkStateType  *types,
                          gint          n_types)
{
  AtkRealStateSet *real_set;
  gint     i;
  g_return_if_fail (ATK_IS_STATE_SET (set));

  real_set = (AtkRealStateSet *)set;

  for (i = 0; i < n_types; i++)
  {
    real_set->state |= ATK_STATE (types[i]);
  }
}

/**
 * atk_state_set_clear_states:
 * @set: an #AtkStateSet
 *
 * Removes all states from the state set.
 **/
void
atk_state_set_clear_states (AtkStateSet   *set)
{
  AtkRealStateSet *real_set;
  g_return_if_fail (ATK_IS_STATE_SET (set));

  real_set = (AtkRealStateSet *)set;

  real_set->state = 0;
}

/**
 * atk_state_set_contains_state:
 * @set: an #AtkStateSet
 * @type: an #AtkStateType
 *
 * Checks whether the state for the specified type is in the specified set.
 *
 * Returns: %TRUE if @type is the state type is in @set.
 **/
gboolean
atk_state_set_contains_state (AtkStateSet   *set,
                              AtkStateType  type)
{
  AtkRealStateSet *real_set;
  g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);

  real_set = (AtkRealStateSet *)set;

  if (real_set->state & ATK_STATE (type))
    return TRUE;
  else
    return FALSE;
}

/**
 * atk_state_set_contains_states:
 * @set: an #AtkStateSet
 * @types: an array of #AtkStateType
 * @n_types: The number of elements in the array
 *
 * Checks whether the states for all the specified types are in the 
 * specified set.
 *
 * Returns: %TRUE if all the states for @type are in @set.
 **/
gboolean
atk_state_set_contains_states (AtkStateSet   *set,
                               AtkStateType  *types,
                               gint          n_types)
{
  AtkRealStateSet *real_set;
  gint i;
  g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);

  real_set = (AtkRealStateSet *)set;

  for (i = 0; i < n_types; i++)
  {
    if (!(real_set->state & ATK_STATE (types[i])))
      return FALSE;
  }
  return TRUE;
}

/**
 * atk_state_set_remove_state:
 * @set: an #AtkStateSet
 * @type: an #AtkType
 *
 * Removes the state for the specified type from the state set.
 *
 * Returns: %TRUE if @type was the state type is in @set.
 **/
gboolean
atk_state_set_remove_state (AtkStateSet  *set,
                            AtkStateType type)
{
  AtkRealStateSet *real_set;
  g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);

  real_set = (AtkRealStateSet *)set;

  if (real_set->state & ATK_STATE (type))
  {
    real_set->state ^= ATK_STATE (type);
    return TRUE;
  }
  else
    return FALSE;
}

/**
 * atk_state_set_and_sets:
 * @set: an #AtkStateSet
 * @compare_set: another #AtkStateSet
 *
 * Constructs the intersection of the two sets, returning %NULL if the
 * intersection is empty.
 *
 * Returns: a new #AtkStateSet which is the intersection of the two sets.
 **/
AtkStateSet*
atk_state_set_and_sets (AtkStateSet  *set,
                        AtkStateSet  *compare_set)
{
  AtkRealStateSet *real_set, *real_compare_set;
  AtkStateSet *return_set = NULL;
  AtkState state;

  g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
  g_return_val_if_fail (ATK_IS_STATE_SET (compare_set), FALSE);

  real_set = (AtkRealStateSet *)set;
  real_compare_set = (AtkRealStateSet *)compare_set;

  state = real_set->state & real_compare_set->state;
  if (state)
  {
    return_set = atk_state_set_new();
    ((AtkRealStateSet *) return_set)->state = state;
  }
  return return_set;
}

/**
 * atk_state_set_or_sets:
 * @set: an #AtkStateSet
 * @compare_set: another #AtkStateSet
 *
 * Constructs the union of the two sets.
 *
 * Returns: a new #AtkStateSet which is the union of the two sets,
 * returning %NULL is empty.
 **/
AtkStateSet*
atk_state_set_or_sets (AtkStateSet  *set,
                       AtkStateSet  *compare_set)
{
  AtkRealStateSet *real_set, *real_compare_set;
  AtkStateSet *return_set = NULL;
  AtkState state;

  g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
  g_return_val_if_fail (ATK_IS_STATE_SET (compare_set), FALSE);

  real_set = (AtkRealStateSet *)set;
  real_compare_set = (AtkRealStateSet *)compare_set;

  state = real_set->state | real_compare_set->state;

  return_set = atk_state_set_new();
  ((AtkRealStateSet *) return_set)->state = state;

  return return_set;
}

/**
 * atk_state_set_xor_sets:
 * @set: an #AtkStateSet
 * @compare_set: another #AtkStateSet
 *
 * Constructs the exclusive-or of the two sets, returning %NULL is empty.
 * The set returned by this operation contains the states in exactly
 * one of the two sets.
 *
 * Returns: a new #AtkStateSet which contains the states which are 
 * in exactly one of the two sets.
 **/
AtkStateSet*
atk_state_set_xor_sets (AtkStateSet  *set,
                        AtkStateSet  *compare_set)
{
  AtkRealStateSet *real_set, *real_compare_set;
  AtkStateSet *return_set = NULL;
  AtkState state, state1, state2;

  g_return_val_if_fail (ATK_IS_STATE_SET (set), FALSE);
  g_return_val_if_fail (ATK_IS_STATE_SET (compare_set), FALSE);

  real_set = (AtkRealStateSet *)set;
  real_compare_set = (AtkRealStateSet *)compare_set;

  state1 = real_set->state & (~real_compare_set->state);
  state2 = (~real_set->state) & real_compare_set->state;
  state = state1 | state2;

  if (state)
  {
    return_set = atk_state_set_new();
    ((AtkRealStateSet *) return_set)->state = state;
  }
  return return_set;
}