gtkatcontext.c 9.67 KB
Newer Older
Emmanuele Bassi's avatar
Emmanuele Bassi committed
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
/* gtkatcontext.c: Assistive technology context
 *
 * Copyright 2020  GNOME Foundation
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * 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.1 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, see <http://www.gnu.org/licenses/>.
 */

/**
 * SECTION:gtkatcontext
 * @Title: GtkATContext
 * @Short_description: An object communicating to Assistive Technologies
 *
 * GtkATContext is an abstract class provided by GTK to communicate to
 * platform-specific assistive technologies API.
 *
 * Each platform supported by GTK implements a #GtkATContext subclass, and
 * is responsible for updating the accessible state in response to state
 * changes in #GtkAccessible.
 */

#include "config.h"

#include "gtkatcontextprivate.h"

#include "gtkaccessiblevalueprivate.h"
#include "gtktestatcontextprivate.h"
#include "gtktypebuiltins.h"

G_DEFINE_ABSTRACT_TYPE (GtkATContext, gtk_at_context, G_TYPE_OBJECT)

enum
{
  PROP_ACCESSIBLE_ROLE = 1,
  PROP_ACCESSIBLE,

  N_PROPS
};

static GParamSpec *obj_props[N_PROPS];

static void
gtk_at_context_finalize (GObject *gobject)
{
  GtkATContext *self = GTK_AT_CONTEXT (gobject);

  gtk_accessible_state_set_unref (self->states);
60
  gtk_accessible_property_set_unref (self->properties);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
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

  G_OBJECT_CLASS (gtk_at_context_parent_class)->finalize (gobject);
}

static void
gtk_at_context_set_property (GObject      *gobject,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  GtkATContext *self = GTK_AT_CONTEXT (gobject);

  switch (prop_id)
    {
    case PROP_ACCESSIBLE_ROLE:
      self->accessible_role = g_value_get_enum (value);
      break;

    case PROP_ACCESSIBLE:
      self->accessible = g_value_get_object (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
    }
}

static void
gtk_at_context_get_property (GObject    *gobject,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  GtkATContext *self = GTK_AT_CONTEXT (gobject);

  switch (prop_id)
    {
    case PROP_ACCESSIBLE_ROLE:
      g_value_set_enum (value, self->accessible_role);
      break;

    case PROP_ACCESSIBLE:
      g_value_set_object (value, self->accessible);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
    }
}

static void
112
113
114
115
116
gtk_at_context_real_state_change (GtkATContext                *self,
                                  GtkAccessibleStateChange     changed_states,
                                  GtkAccessiblePropertyChange  changed_properies,
                                  GtkAccessibleStateSet       *states,
                                  GtkAccessiblePropertySet    *properties)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
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
{
}

static void
gtk_at_context_class_init (GtkATContextClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->set_property = gtk_at_context_set_property;
  gobject_class->get_property = gtk_at_context_get_property;
  gobject_class->finalize = gtk_at_context_finalize;

  klass->state_change = gtk_at_context_real_state_change;

  /**
   * GtkATContext:accessible-role:
   *
   * The accessible role used by the AT context.
   *
   * Depending on the given role, different states and properties can be
   * set or retrieved.
   */
  obj_props[PROP_ACCESSIBLE_ROLE] =
    g_param_spec_enum ("accessible-role",
                       "Accessible Role",
                       "The accessible role of the AT context",
                       GTK_TYPE_ACCESSIBLE_ROLE,
                       GTK_ACCESSIBLE_ROLE_WIDGET,
                       G_PARAM_READWRITE |
                       G_PARAM_CONSTRUCT_ONLY |
                       G_PARAM_STATIC_STRINGS);

  /**
   * GtkATContext:accessible:
   *
   * The #GtkAccessible that created the #GtkATContext instance.
   */
  obj_props[PROP_ACCESSIBLE] =
    g_param_spec_object ("accessible",
                         "Accessible",
                         "The accessible implementation",
                         GTK_TYPE_ACCESSIBLE,
                         G_PARAM_READWRITE |
                         G_PARAM_CONSTRUCT_ONLY |
                         G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (gobject_class, N_PROPS, obj_props);
}

static void
gtk_at_context_init (GtkATContext *self)
{
  self->accessible_role = GTK_ACCESSIBLE_ROLE_WIDGET;

  self->states = gtk_accessible_state_set_new ();
172
  self->properties = gtk_accessible_property_set_new ();
Emmanuele Bassi's avatar
Emmanuele Bassi committed
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
}

/**
 * gtk_at_context_get_accessible:
 * @self: a #GtkATContext
 *
 * Retrieves the #GtkAccessible using this context.
 *
 * Returns: (transfer none): a #GtkAccessible
 */
GtkAccessible *
gtk_at_context_get_accessible (GtkATContext *self)
{
  g_return_val_if_fail (GTK_IS_AT_CONTEXT (self), NULL);

  return self->accessible;
}

/**
 * gtk_at_context_get_accessible_role:
 * @self: a #GtkATContext
 *
 * Retrieves the accessible role of this context.
 *
 * Returns: a #GtkAccessibleRole
 */
GtkAccessibleRole
gtk_at_context_get_accessible_role (GtkATContext *self)
{
  g_return_val_if_fail (GTK_IS_AT_CONTEXT (self), GTK_ACCESSIBLE_ROLE_WIDGET);

  return self->accessible_role;
}

/*< private >
 * gtk_at_context_create:
 * @accessible_role: the accessible role used by the #GtkATContext
 * @accessible: the #GtkAccessible implementation using the #GtkATContext
 *
 * Creates a new #GtkATContext instance for the given accessible role and
 * accessible instance.
 *
 * The #GtkATContext implementation being instantiated will depend on the
 * platform.
 *
 * Returns: (nullable): the #GtkATContext
 */
GtkATContext *
gtk_at_context_create (GtkAccessibleRole  accessible_role,
                       GtkAccessible     *accessible)
{
  static const char *gtk_test_accessible;
  static const char *gtk_no_a11y;

  if (G_UNLIKELY (gtk_test_accessible == NULL))
    {
      const char *env = g_getenv ("GTK_TEST_ACCESSIBLE");

      if (env != NULL && *env !='\0')
        gtk_test_accessible = "1";
      else
        gtk_test_accessible = "0";
    }

  if (G_UNLIKELY (gtk_no_a11y == NULL))
    {
      const char *env = g_getenv ("GTK_NO_A11Y");

      if (env != NULL && *env != '\0')
        gtk_no_a11y = "1";
      else
        gtk_no_a11y = "0";
    }

  /* Shortcut everything if we're running with the test AT context */
  if (gtk_test_accessible[0] == '1')
    return gtk_test_at_context_new (accessible_role, accessible);

  if (gtk_no_a11y[0] == '1')
    return NULL;

  /* FIXME: Add GIOExtension for AT contexts */
  return gtk_test_at_context_new (accessible_role, accessible);
}

/*< private >
259
 * gtk_at_context_update:
Emmanuele Bassi's avatar
Emmanuele Bassi committed
260
261
262
 * @self: a #GtkATContext
 *
 * Notifies the AT connected to this #GtkATContext that the accessible
263
 * state and its properties have changed.
Emmanuele Bassi's avatar
Emmanuele Bassi committed
264
265
 */
void
266
gtk_at_context_update (GtkATContext *self)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
267
268
269
{
  g_return_if_fail (GTK_IS_AT_CONTEXT (self));

270
271
  GtkAccessibleStateChange changed_states = 0;
  GtkAccessiblePropertyChange changed_properties = 0;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
272
273
274
275

  for (int i = 0; i < GTK_ACCESSIBLE_STATE_SELECTED; i++)
    {
      if (gtk_accessible_state_set_contains (self->states, i))
276
        changed_states |= (1 << i);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
277
278
    }

279
280
281
282
283
284
285
286
287
288
289
  for (int i = 0; i < GTK_ACCESSIBLE_PROPERTY_VALUE_TEXT; i++)
    {
      if (gtk_accessible_property_set_contains (self->properties, i))
        changed_properties |= (1 << i);
    }

  GTK_AT_CONTEXT_GET_CLASS (self)->state_change (self,
                                                 changed_states,
                                                 changed_properties,
                                                 self->states,
                                                 self->properties);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
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
/*< private >
 * gtk_at_context_set_state:
 * @self: a #GtkATContext
 * @state: a #GtkAccessibleState
 * @value: (nullable): #GtkAccessibleValue
 *
 * Sets the @value for the given @state of a #GtkATContext.
 *
 * If @value is %NULL, the state is unset.
 *
 * This function will accumulate state changes until gtk_at_context_update_state()
 * is called.
 */
void
gtk_at_context_set_accessible_state (GtkATContext       *self,
                                     GtkAccessibleState  state,
                                     GtkAccessibleValue *value)
{
  g_return_if_fail (GTK_IS_AT_CONTEXT (self));

  if (value != NULL)
    gtk_accessible_state_set_add (self->states, state, value);
  else
    gtk_accessible_state_set_remove (self->states, state);
}

/*< private >
 * gtk_at_context_set_accessible_property:
 * @self: a #GtkATContext
 * @property: a #GtkAccessibleProperty
 * @value: (nullable): #GtkAccessibleValue
 *
 * Sets the @value for the given @property of a #GtkATContext.
 *
 * If @value is %NULL, the property is unset.
 *
 * This function will accumulate property changes until gtk_at_context_update_state()
 * is called.
 */
Emmanuele Bassi's avatar
Emmanuele Bassi committed
331
void
332
333
334
gtk_at_context_set_accessible_property (GtkATContext          *self,
                                        GtkAccessibleProperty  property,
                                        GtkAccessibleValue    *value)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
335
336
337
{
  g_return_if_fail (GTK_IS_AT_CONTEXT (self));

338
339
340
341
  if (value != NULL)
    gtk_accessible_property_set_add (self->properties, property, value);
  else
    gtk_accessible_property_set_remove (self->properties, property);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
342
}