BGO#657051: Fix build error on Solaris
[platform/upstream/at-spi2-core.git] / atspi / atspi-stateset.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  * Copyright 2010, 2011 Novell, Inc.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include "atspi-private.h"
26
27 static void atspi_state_set_class_init (AtspiStateSetClass *klass);
28
29 G_DEFINE_TYPE (AtspiStateSet, atspi_state_set, G_TYPE_OBJECT)
30
31 static const char *state_names [] =
32 {
33   "invalid",
34   "active",
35   "armed",
36   "busy",
37   "checked",
38   "collapsed",
39   "defunct",
40   "editable",
41   "enabled",
42   "expandable",
43   "expanded",
44   "focusable",
45   "focused",
46   "has-tool-tip",
47   "horizontal",
48   "iconified",
49   "modal",
50   "multi-line",
51   "multiselectable",
52   "opaque",
53   "pressed",
54   "resizable",
55   "selectable",
56   "selected",
57   "sensitive",
58   "showing",
59   "singleLine",
60   "stale",
61   "transient",
62   "vertical",
63   "visible",
64   "manages-descendants",
65   "indeterminate",
66   "required",
67   "truncated",
68   "animated",
69   "invalid-entry",
70   "supports-autocompletion",
71   "selectable-text",
72   "is-default",
73   "visited",
74   NULL
75 };
76
77 static void
78 atspi_state_set_init (AtspiStateSet *set)
79 {
80   set->states = 0;
81 }
82
83 static void
84 atspi_state_set_class_init (AtspiStateSetClass* klass)
85 {
86 }
87
88 /**
89  * atspi_state_set_new:
90  *
91  * @states: (element-type AtspiStateType): An array of states with which the
92  * method initializes the state set.
93  *
94  * Generates an #AtspiStateSet with the given @states.
95  *
96  * Returns: A new #AtspiStateSet with the given states.
97  **/
98 AtspiStateSet *
99 atspi_state_set_new (GArray *states)
100 {
101   AtspiStateSet *set = g_object_new (ATSPI_TYPE_STATE_SET, NULL);
102   gint i;
103
104   if (!set || !states)
105     return set;
106
107   for (i = 0; i < states->len; i++)
108     atspi_state_set_add (set, g_array_index (states, AtspiStateType, i));
109   return set;
110 }
111
112 AtspiStateSet *
113 _atspi_state_set_new_internal (AtspiAccessible *accessible, gint64 states)
114 {
115   AtspiStateSet *set;
116   
117   set = g_object_new (ATSPI_TYPE_STATE_SET, NULL);
118   g_return_val_if_fail (set != NULL, NULL);
119
120   set->accessible = accessible;
121   set->states = states;
122   return set;
123 }
124
125 /**
126  * atspi_state_set_set_by_name:
127  *
128  * @set: a pointer to the #AtspiStateSet object on which to operate.
129  *
130  * @name: a string corresponding to a state name.
131  * 
132  * @enabled: if #TRUE, @name should be enabled in the @set in question; otherwise, it
133  * should be disabled. 
134  *
135  * Enables/disables a state in an #AtspiStateSet according to its @name.
136  **/
137 void
138 atspi_state_set_set_by_name (AtspiStateSet *set, const gchar *name, gboolean enabled)
139 {
140   gint i = 0;
141
142   if (set->accessible &&
143       !(set->accessible->cached_properties & ATSPI_CACHE_STATES))
144     return;
145
146   /* TODO: This could perhaps be optimized */
147   for (i = 0; state_names [i]; i++)
148   {
149     if (!strcmp (state_names [i], name))
150     {
151       if (enabled)
152         set->states |= ((gint64)1 << i);
153       else
154         set->states &= ~((gint64)1 << i);
155       return;
156     }
157   }
158   g_warning ("at-spi: Attempt to set unknown state '%s'", name);
159 }
160
161 static void
162 refresh_states (AtspiStateSet *set)
163 {
164   GArray *state_array;
165   dbus_uint32_t *states;
166
167   if (!set->accessible ||
168       (set->accessible->cached_properties & ATSPI_CACHE_STATES))
169     return;
170
171   if (!_atspi_dbus_call (set->accessible, atspi_interface_accessible, "GetState", NULL, "=>au", &state_array))
172     return;
173
174   states = (dbus_uint32_t *) state_array->data;
175
176   set->states = ((gint64)states [1]) << 32;
177   set->states |= (gint64) states [0];
178   g_array_free (state_array, TRUE);
179 }
180
181 /**
182  * atspi_state_set_add:
183  *
184  * @set: a pointer to the #AtspiStateSet object on which to operate.
185  *
186  * @state: an #AtspiStateType to be added to the specified #AtspiStateSet.
187  *
188  * Adds a particular #AtspiState to an #AtspiStateSet (i.e. sets the
189  *       given state to #TRUE in the stateset).
190  *
191  **/
192 void
193 atspi_state_set_add (AtspiStateSet *set, AtspiStateType state)
194 {
195   g_return_if_fail (set != NULL);
196   set->states |= (((gint64)1) << state);
197 }
198
199 /**
200  * atspi_state_set_compare:
201  *
202  * @set: a pointer to the first #AtspiStateSet object on which to operate.
203  *
204  * @set2: a pointer to the second #AtspiStateSet object on which to operate.
205  *
206  * Determines the differences between two instances of #AtspiStateSet.
207  *
208  * @see #atspi_state_set_equals.
209  *
210  * Returns: (transfer full): an #AtspiStateSet object containing all states
211  * contained on one of the two sets but not the other.
212  *
213  **/
214 AtspiStateSet *
215 atspi_state_set_compare (AtspiStateSet *set,
216                          AtspiStateSet *set2)
217 {
218   g_return_val_if_fail (set != NULL, NULL);
219   g_return_val_if_fail (set2 != NULL, NULL);
220
221   return _atspi_state_set_new_internal (NULL, set->states ^ set2->states);
222 }
223
224 /**
225  * atspi_state_set_contains:
226  *
227  * @set: a pointer to the #AtspiStateSet object on which to operate.
228  *
229  * @state: an #AtspiStateType for which the specified #AtspiStateSet
230  *       will be queried.
231  *
232  * Determines whether a given #AtspiStateSet includes a given state; that is,
233  *       whether @state is true for the @set in question.
234  *
235  * Returns: #TRUE if @state is true/included in the given #AtspiStateSet,
236  *          otherwise #FALSE.
237  *
238  **/
239 gboolean
240 atspi_state_set_contains (AtspiStateSet *set,
241                              AtspiStateType state)
242 {
243   if (!set)
244     return FALSE;
245   refresh_states (set);
246   return (set->states & ((gint64)1 << state)) ? TRUE : FALSE;
247 }
248
249 /**
250  * atspi_state_set_equals:
251  *
252  * @set: a pointer to the first #AtspiStateSet object on which to operate.
253  *
254  * @set2: a pointer to the second #AtspiStateSet object on which to operate.
255  *
256  * Determines whether two instances of #AtspiStateSet are equivalent (i.e.
257  *         consist of the same #AtspiStates).  Useful for checking multiple
258  *         state variables at once.
259  *
260  * @see #atspi_state_set_compare.
261  *
262  * Returns: #TRUE if the two #AtspiStateSets are equivalent,
263  * otherwise #FALSE.
264  *
265  **/
266 gboolean
267 atspi_state_set_equals (AtspiStateSet *set,
268                            AtspiStateSet *set2)
269 {
270   if (set == set2)
271     return TRUE;
272   if (set == NULL || set2 == NULL)
273     return FALSE;
274   return (set->states == set2->states);
275 }
276
277 /**
278  * atspi_state_set_get_states:
279  *
280  * @set: The #AtspiStateSet to be queried.
281  *
282  * Returns the states in an #AtspiStateSet as an array.
283  *
284  * Returns: (element-type AtspiStateType) (transfer full): A #GArray of state
285  *          types representing the current state.
286  **/
287 GArray *
288 atspi_state_set_get_states (AtspiStateSet *set)
289 {
290   gint i = 0;
291   guint64 val = 1;
292   GArray *ret;
293
294   g_return_val_if_fail (set != NULL, NULL);
295   refresh_states (set);
296   ret = g_array_new (TRUE, TRUE, sizeof (AtspiStateType));
297   if (!ret)
298     return NULL;
299   for (i = 0; i < 64; i++)
300   {
301     if (set->states & val)
302     {
303       GArray *new_array = g_array_append_val (ret, i);
304       if (new_array)
305         ret = new_array;
306     }
307     val <<= 1;
308   }
309   return ret;
310 }
311
312 /**
313  * atspi_state_set_is_empty:
314  *
315  * @set: The #AtspiStateSet to query.
316  *
317  * Returns: #TRUE if the state set contains no states; #FALSE otherwise.
318  **/
319 gboolean
320 atspi_state_set_is_empty (AtspiStateSet *set)
321 {
322   return (set->states == 0);
323 }
324
325 /**
326  * atspi_state_set_remove:
327  *
328  * @set: a pointer to the #AtspiStateSet object on which to operate.
329  *
330  * @state: an #AtspiStateType to remove from the specified @set.
331  *
332  * Removes a particular #AtspiState to an #AtspiStateSet (i.e. sets the
333  *       given state to #FALSE in the stateset.)
334  *
335  **/
336 void
337 atspi_state_set_remove (AtspiStateSet *set, AtspiStateType state)
338 {
339   g_return_if_fail (set != NULL);
340   set->states &= ~((gint64)1 << state);
341 }
342