Some work on event support; many bug fixes
[platform/upstream/at-spi2-core.git] / atspi / atspi-stateset.c
1 #include "atspi-private.h"
2
3 static void atspi_state_set_class_init (AtspiStateSetClass *klass);
4
5 G_DEFINE_TYPE (AtspiStateSet, atspi_state_set, G_TYPE_OBJECT)
6
7 static const char *state_names [] =
8 {
9   "invalid",
10   "active",
11   "armed",
12   "busy",
13   "checked",
14   "collapsed",
15   "defunct",
16   "editable",
17   "enabled",
18   "expandable",
19   "expanded",
20   "focusable",
21   "focused",
22   "has-tool-tip",
23   "horizontal",
24   "iconified",
25   "modal",
26   "multi-line",
27   "multiselectable",
28   "opaque",
29   "pressed",
30   "resizable",
31   "selectable",
32   "selected",
33   "sensitive",
34   "showing",
35   "singleLine",
36   "stale",
37   "transient",
38   "vertical",
39   "visible",
40   "manages-descendants",
41   "indeterminate",
42   "required",
43   "truncated",
44   "animated",
45   "invalid-entry",
46   "supports-autocompletion",
47   "selectable-text",
48   "is-default",
49   "visited",
50   NULL
51 };
52
53 static void
54 atspi_state_set_init (AtspiStateSet *set)
55 {
56   set->states = 0;
57 }
58
59 static void
60 atspi_state_set_class_init (AtspiStateSetClass* klass)
61 {
62 }
63
64 AtspiStateSet *
65 atspi_state_set_new (AtspiAccessible *accessible, gint64 states)
66 {
67   AtspiStateSet *set;
68   
69   set = g_object_new (ATSPI_TYPE_STATE_SET, NULL);
70   g_return_val_if_fail (set != NULL, NULL);
71
72   set->accessible = accessible;
73   set->states = states;
74   return set;
75 }
76
77 void atspi_state_set_set_by_name (AtspiStateSet *set, const gchar *name, gboolean enabled)
78 {
79   gint i = 0;
80
81   if (!(set->accessible->cached_properties & ATSPI_CACHE_STATES))
82     return;
83
84   /* TODO: This could perhaps be optimized */
85   for (i = 0; state_names [i]; i++)
86   {
87     if (!strcmp (state_names [i], name))
88     {
89       if (enabled)
90         set->states |= (1 << i);
91       else
92         set->states &= ~(1 << i);
93       return;
94     }
95   }
96   g_warning ("at-spi: Attempt to set unknown state '%s'", name);
97 }
98
99 static void
100 refresh_states (AtspiStateSet *set)
101 {
102   GArray *state_array;
103   dbus_uint32_t *states;
104
105   if ((set->accessible->cached_properties & ATSPI_CACHE_STATES))
106     return;
107
108   if (!_atspi_dbus_call (set->accessible, atspi_interface_accessible, "GetState", NULL, "=>au", &state_array))
109     return;
110
111   states = (dbus_uint32_t *) state_array->data;
112
113   set->states = ((gint64)states [1]) << 32;
114   set->states += states [0];
115   g_array_free (state_array, TRUE);
116 }
117
118 /**
119  * atspi_state_set_contains:
120  * @set: a pointer to the #AtspiStateSet object on which to operate.
121  * @state: an #AtspiStateType for which the specified #AtspiStateSet
122  *       will be queried.
123  *
124  * Determine whether a given #AtspiStateSet includes a given state; that is,
125  *       whether @state is true for the stateset in question.
126  *
127  * Returns: #TRUE if @state is true/included in the given #AtspiStateSet,
128  *          otherwise #FALSE.
129  *
130  **/
131 gboolean
132 atspi_state_set_contains (AtspiStateSet *set,
133                              AtspiStateType state)
134 {
135   refresh_states (set);
136   return (set->states & (1 << state)) ? TRUE : FALSE;
137 }