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
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the
20
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21
 * Boston, MA 02110-1301, USA.
22
 */
23

            
24
/* stateset.c : implements the StateSet interface */
25

            
26
#include "config.h"
27

            
28
#include "accessible-stateset.h"
29
#include "bitarray.h"
30

            
31
#include <stdio.h>
32

            
33
static AtspiStateType *accessible_state_types = NULL;
34
static AtkStateType *atk_state_types = NULL;
35

            
36
static gboolean
37
1585
spi_init_state_type_tables (void)
38
{
39
  gint i;
40

            
41
1585
  if (accessible_state_types || atk_state_types)
42
1428
    return FALSE;
43
157
  if (!accessible_state_types)
44
157
    accessible_state_types = g_new (AtspiStateType, ATK_STATE_LAST_DEFINED);
45
157
  if (!atk_state_types)
46
157
    atk_state_types = g_new (AtkStateType, ATSPI_STATE_LAST_DEFINED);
47
157
  g_return_val_if_fail (accessible_state_types, FALSE);
48
157
  g_return_val_if_fail (atk_state_types, FALSE);
49

            
50
7065
  for (i = 0; i < ATSPI_STATE_LAST_DEFINED; i++)
51
    {
52
6908
      atk_state_types[i] = ATK_STATE_INVALID;
53
    }
54

            
55
7065
  for (i = 0; i < ATK_STATE_LAST_DEFINED; i++)
56
    {
57
6908
      accessible_state_types[i] = ATSPI_STATE_INVALID;
58
    }
59

            
60
157
  accessible_state_types[ATK_STATE_ACTIVE] = ATSPI_STATE_ACTIVE;
61
157
  atk_state_types[ATSPI_STATE_ACTIVE] = ATK_STATE_ACTIVE;
62
157
  accessible_state_types[ATK_STATE_ANIMATED] = ATSPI_STATE_ANIMATED;
63
157
  atk_state_types[ATSPI_STATE_ANIMATED] = ATK_STATE_ANIMATED;
64
157
  accessible_state_types[ATK_STATE_ARMED] = ATSPI_STATE_ARMED;
65
157
  atk_state_types[ATSPI_STATE_ARMED] = ATK_STATE_ARMED;
66
157
  accessible_state_types[ATK_STATE_BUSY] = ATSPI_STATE_BUSY;
67
157
  atk_state_types[ATSPI_STATE_BUSY] = ATK_STATE_BUSY;
68
157
  accessible_state_types[ATK_STATE_CHECKED] = ATSPI_STATE_CHECKED;
69
157
  atk_state_types[ATSPI_STATE_CHECKED] = ATK_STATE_CHECKED;
70
157
  accessible_state_types[ATK_STATE_DEFUNCT] = ATSPI_STATE_DEFUNCT;
71
157
  atk_state_types[ATSPI_STATE_DEFUNCT] = ATK_STATE_DEFUNCT;
72
157
  accessible_state_types[ATK_STATE_EDITABLE] = ATSPI_STATE_EDITABLE;
73
157
  atk_state_types[ATSPI_STATE_EDITABLE] = ATK_STATE_EDITABLE;
74
157
  accessible_state_types[ATK_STATE_ENABLED] = ATSPI_STATE_ENABLED;
75
157
  atk_state_types[ATSPI_STATE_ENABLED] = ATK_STATE_ENABLED;
76
157
  accessible_state_types[ATK_STATE_EXPANDABLE] = ATSPI_STATE_EXPANDABLE;
77
157
  atk_state_types[ATSPI_STATE_EXPANDABLE] = ATK_STATE_EXPANDABLE;
78
157
  accessible_state_types[ATK_STATE_EXPANDED] = ATSPI_STATE_EXPANDED;
79
157
  atk_state_types[ATSPI_STATE_EXPANDED] = ATK_STATE_EXPANDED;
80
157
  accessible_state_types[ATK_STATE_FOCUSABLE] = ATSPI_STATE_FOCUSABLE;
81
157
  atk_state_types[ATSPI_STATE_FOCUSABLE] = ATK_STATE_FOCUSABLE;
82
157
  accessible_state_types[ATK_STATE_FOCUSED] = ATSPI_STATE_FOCUSED;
83
157
  atk_state_types[ATSPI_STATE_FOCUSED] = ATK_STATE_FOCUSED;
84
157
  accessible_state_types[ATK_STATE_HORIZONTAL] = ATSPI_STATE_HORIZONTAL;
85
157
  atk_state_types[ATSPI_STATE_HORIZONTAL] = ATK_STATE_HORIZONTAL;
86
157
  accessible_state_types[ATK_STATE_ICONIFIED] = ATSPI_STATE_ICONIFIED;
87
157
  atk_state_types[ATSPI_STATE_ICONIFIED] = ATK_STATE_ICONIFIED;
88
157
  accessible_state_types[ATK_STATE_MODAL] = ATSPI_STATE_MODAL;
89
157
  atk_state_types[ATSPI_STATE_MODAL] = ATK_STATE_MODAL;
90
157
  accessible_state_types[ATK_STATE_MULTI_LINE] = ATSPI_STATE_MULTI_LINE;
91
157
  atk_state_types[ATSPI_STATE_MULTI_LINE] = ATK_STATE_MULTI_LINE;
92
157
  accessible_state_types[ATK_STATE_MULTISELECTABLE] = ATSPI_STATE_MULTISELECTABLE;
93
157
  atk_state_types[ATSPI_STATE_MULTISELECTABLE] = ATK_STATE_MULTISELECTABLE;
94
157
  accessible_state_types[ATK_STATE_OPAQUE] = ATSPI_STATE_OPAQUE;
95
157
  atk_state_types[ATSPI_STATE_OPAQUE] = ATK_STATE_OPAQUE;
96
157
  accessible_state_types[ATK_STATE_PRESSED] = ATSPI_STATE_PRESSED;
97
157
  atk_state_types[ATSPI_STATE_PRESSED] = ATK_STATE_PRESSED;
98
157
  accessible_state_types[ATK_STATE_RESIZABLE] = ATSPI_STATE_RESIZABLE;
99
157
  atk_state_types[ATSPI_STATE_RESIZABLE] = ATK_STATE_RESIZABLE;
100
157
  accessible_state_types[ATK_STATE_SELECTABLE] = ATSPI_STATE_SELECTABLE;
101
157
  atk_state_types[ATSPI_STATE_SELECTABLE] = ATK_STATE_SELECTABLE;
102
157
  accessible_state_types[ATK_STATE_SELECTED] = ATSPI_STATE_SELECTED;
103
157
  atk_state_types[ATSPI_STATE_SELECTED] = ATK_STATE_SELECTED;
104
157
  accessible_state_types[ATK_STATE_SENSITIVE] = ATSPI_STATE_SENSITIVE;
105
157
  atk_state_types[ATSPI_STATE_SENSITIVE] = ATK_STATE_SENSITIVE;
106
157
  accessible_state_types[ATK_STATE_SHOWING] = ATSPI_STATE_SHOWING;
107
157
  atk_state_types[ATSPI_STATE_SHOWING] = ATK_STATE_SHOWING;
108
157
  accessible_state_types[ATK_STATE_SINGLE_LINE] = ATSPI_STATE_SINGLE_LINE;
109
157
  atk_state_types[ATSPI_STATE_SINGLE_LINE] = ATK_STATE_SINGLE_LINE;
110
157
  accessible_state_types[ATK_STATE_STALE] = ATSPI_STATE_STALE;
111
157
  atk_state_types[ATSPI_STATE_STALE] = ATK_STATE_STALE;
112
157
  accessible_state_types[ATK_STATE_TRANSIENT] = ATSPI_STATE_TRANSIENT;
113
157
  atk_state_types[ATSPI_STATE_TRANSIENT] = ATK_STATE_TRANSIENT;
114
157
  accessible_state_types[ATK_STATE_VERTICAL] = ATSPI_STATE_VERTICAL;
115
157
  atk_state_types[ATSPI_STATE_VERTICAL] = ATK_STATE_VERTICAL;
116
157
  accessible_state_types[ATK_STATE_VISIBLE] = ATSPI_STATE_VISIBLE;
117
157
  atk_state_types[ATSPI_STATE_VISIBLE] = ATK_STATE_VISIBLE;
118
157
  accessible_state_types[ATK_STATE_MANAGES_DESCENDANTS] = ATSPI_STATE_MANAGES_DESCENDANTS;
119
157
  atk_state_types[ATSPI_STATE_MANAGES_DESCENDANTS] = ATK_STATE_MANAGES_DESCENDANTS;
120
157
  accessible_state_types[ATK_STATE_INDETERMINATE] = ATSPI_STATE_INDETERMINATE;
121
157
  atk_state_types[ATSPI_STATE_INDETERMINATE] = ATK_STATE_INDETERMINATE;
122
157
  accessible_state_types[ATK_STATE_TRUNCATED] = ATSPI_STATE_TRUNCATED;
123
157
  atk_state_types[ATSPI_STATE_TRUNCATED] = ATK_STATE_TRUNCATED;
124
157
  accessible_state_types[ATK_STATE_REQUIRED] = ATSPI_STATE_REQUIRED;
125
157
  atk_state_types[ATSPI_STATE_REQUIRED] = ATK_STATE_REQUIRED;
126
157
  accessible_state_types[ATK_STATE_INVALID_ENTRY] = ATSPI_STATE_INVALID_ENTRY;
127
157
  atk_state_types[ATSPI_STATE_INVALID_ENTRY] = ATK_STATE_INVALID_ENTRY;
128
157
  accessible_state_types[ATK_STATE_SUPPORTS_AUTOCOMPLETION] = ATSPI_STATE_SUPPORTS_AUTOCOMPLETION;
129
157
  atk_state_types[ATSPI_STATE_SUPPORTS_AUTOCOMPLETION] = ATK_STATE_SUPPORTS_AUTOCOMPLETION;
130
157
  accessible_state_types[ATK_STATE_SELECTABLE_TEXT] = ATSPI_STATE_SELECTABLE_TEXT;
131
157
  atk_state_types[ATSPI_STATE_SELECTABLE_TEXT] = ATK_STATE_SELECTABLE_TEXT;
132
157
  accessible_state_types[ATK_STATE_DEFAULT] = ATSPI_STATE_IS_DEFAULT;
133
157
  atk_state_types[ATSPI_STATE_IS_DEFAULT] = ATK_STATE_DEFAULT;
134
157
  accessible_state_types[ATK_STATE_VISITED] = ATSPI_STATE_VISITED;
135
157
  atk_state_types[ATSPI_STATE_VISITED] = ATK_STATE_VISITED;
136
157
  accessible_state_types[ATK_STATE_HAS_POPUP] = ATSPI_STATE_HAS_POPUP;
137
157
  atk_state_types[ATSPI_STATE_HAS_POPUP] = ATK_STATE_HAS_POPUP;
138
157
  accessible_state_types[ATK_STATE_CHECKABLE] = ATSPI_STATE_CHECKABLE;
139
157
  atk_state_types[ATSPI_STATE_CHECKABLE] = ATK_STATE_CHECKABLE;
140
157
  accessible_state_types[ATK_STATE_HAS_TOOLTIP] = ATSPI_STATE_HAS_TOOLTIP;
141
157
  atk_state_types[ATSPI_STATE_HAS_TOOLTIP] = ATK_STATE_HAS_TOOLTIP;
142
157
  accessible_state_types[ATK_STATE_READ_ONLY] = ATSPI_STATE_READ_ONLY;
143
157
  atk_state_types[ATSPI_STATE_READ_ONLY] = ATK_STATE_READ_ONLY;
144
157
  accessible_state_types[ATK_STATE_COLLAPSED] = ATSPI_STATE_COLLAPSED;
145
157
  atk_state_types[ATSPI_STATE_COLLAPSED] = ATK_STATE_COLLAPSED;
146

            
147
157
  return TRUE;
148
}
149

            
150
static inline AtkState
151
6
state_spi_to_atk (AtspiStateType state)
152
{
153
6
  guint idx = state;
154
6
  if (idx < ATSPI_STATE_LAST_DEFINED)
155
6
    return atk_state_types[idx];
156
  else
157
    return ATK_STATE_INVALID;
158
}
159

            
160
AtkState
161
6
spi_atk_state_from_spi_state (AtspiStateType state)
162
{
163
6
  spi_init_state_type_tables ();
164
6
  return state_spi_to_atk (state);
165
}
166

            
167
AtkStateSet *
168
spi_state_set_cache_from_sequence (GArray *seq)
169
{
170
  int i;
171
  AtkStateSet *set;
172
  AtkStateType *states;
173

            
174
  spi_init_state_type_tables ();
175

            
176
  states = g_newa (AtkStateType, seq->len);
177
  for (i = 0; i < seq->len; i++)
178
    states[i] = state_spi_to_atk (g_array_index (seq, dbus_int32_t, i));
179

            
180
  set = atk_state_set_new ();
181
  atk_state_set_add_states (set, states, seq->len);
182

            
183
  g_array_free (seq, TRUE);
184
  return set;
185
}
186

            
187
void
188
12
spi_atk_state_to_dbus_array (AtkObject *object, dbus_uint32_t *array)
189
{
190
12
  AtkStateSet *set = atk_object_ref_state_set (object);
191
12
  spi_atk_state_set_to_dbus_array (set, array);
192
12
  g_clear_object (&set);
193
12
}
194

            
195
void
196
1579
spi_atk_state_set_to_dbus_array (AtkStateSet *set, dbus_uint32_t *array)
197
{
198
  int i;
199

            
200
1579
  array[0] = 0;
201
1579
  array[1] = 0;
202
1579
  if (!set)
203
    return;
204
1579
  spi_init_state_type_tables ();
205

            
206
  g_assert (ATK_STATE_LAST_DEFINED <= 64);
207
71055
  for (i = 0; i < ATK_STATE_LAST_DEFINED; i++)
208
    {
209
69476
      if (atk_state_set_contains_state (set, i))
210
        {
211
285
          int a = accessible_state_types[i];
212
285
          g_assert (a < 64);
213
285
          BITARRAY_SET (array, a);
214
        }
215
    }
216
}