gtkatcontext.c 24.7 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
/* 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"
39
#include "gtkdebug.h"
Emmanuele Bassi's avatar
Emmanuele Bassi committed
40
41
42
#include "gtktestatcontextprivate.h"
#include "gtktypebuiltins.h"

43
44
45
46
#if defined(GDK_WINDOWING_X11) || defined(GDK_WINDOWING_WAYLAND)
#include "a11y/gtkatspicontextprivate.h"
#endif

Emmanuele Bassi's avatar
Emmanuele Bassi committed
47
48
49
50
51
52
G_DEFINE_ABSTRACT_TYPE (GtkATContext, gtk_at_context, G_TYPE_OBJECT)

enum
{
  PROP_ACCESSIBLE_ROLE = 1,
  PROP_ACCESSIBLE,
53
  PROP_DISPLAY,
Emmanuele Bassi's avatar
Emmanuele Bassi committed
54
55
56
57

  N_PROPS
};

58
59
60
61
62
63
64
enum
{
  STATE_CHANGE,

  LAST_SIGNAL
};

Emmanuele Bassi's avatar
Emmanuele Bassi committed
65
66
static GParamSpec *obj_props[N_PROPS];

67
68
static guint obj_signals[LAST_SIGNAL];

Emmanuele Bassi's avatar
Emmanuele Bassi committed
69
70
71
72
73
static void
gtk_at_context_finalize (GObject *gobject)
{
  GtkATContext *self = GTK_AT_CONTEXT (gobject);

74
75
76
  gtk_accessible_attribute_set_unref (self->properties);
  gtk_accessible_attribute_set_unref (self->relations);
  gtk_accessible_attribute_set_unref (self->states);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

  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;

99
100
101
102
    case PROP_DISPLAY:
      self->display = g_value_get_object (value);
      break;

Emmanuele Bassi's avatar
Emmanuele Bassi committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    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;

126
127
128
129
    case PROP_DISPLAY:
      g_value_set_object (value, self->display);
      break;

Emmanuele Bassi's avatar
Emmanuele Bassi committed
130
131
132
133
134
135
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
    }
}

static void
136
137
gtk_at_context_real_state_change (GtkATContext                *self,
                                  GtkAccessibleStateChange     changed_states,
Emmanuele Bassi's avatar
Emmanuele Bassi committed
138
139
                                  GtkAccessiblePropertyChange  changed_properties,
                                  GtkAccessibleRelationChange  changed_relations,
140
141
142
                                  GtkAccessibleAttributeSet   *states,
                                  GtkAccessibleAttributeSet   *properties,
                                  GtkAccessibleAttributeSet   *relations)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
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
{
}

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,
170
                       GTK_ACCESSIBLE_ROLE_NONE,
Emmanuele Bassi's avatar
Emmanuele Bassi committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
                       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);

189
190
191
192
193
194
195
196
197
198
199
200
201
202
  /**
   * GtkATContext:display:
   *
   * The #GdkDisplay for the #GtkATContext.
   */
  obj_props[PROP_DISPLAY] =
    g_param_spec_object ("display",
                         "Display",
                         "The display connection",
                         GDK_TYPE_DISPLAY,
                         G_PARAM_READWRITE |
                         G_PARAM_CONSTRUCT_ONLY |
                         G_PARAM_STATIC_STRINGS);

203
204
205
206
207
208
209
  /**
   * GtkATContext::state-change:
   * @self: the #GtkATContext
   *
   * Emitted when the attributes of the accessible for the
   * #GtkATContext instance change.
   */
210
211
212
213
  obj_signals[STATE_CHANGE] =
    g_signal_new ("state-change",
                  G_TYPE_FROM_CLASS (gobject_class),
                  G_SIGNAL_RUN_FIRST,
214
                  0,
215
216
                  NULL, NULL,
                  NULL,
217
                  G_TYPE_NONE, 0);
218

Emmanuele Bassi's avatar
Emmanuele Bassi committed
219
220
221
  g_object_class_install_properties (gobject_class, N_PROPS, obj_props);
}

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
#define N_PROPERTIES    (GTK_ACCESSIBLE_PROPERTY_VALUE_TEXT + 1)
#define N_RELATIONS     (GTK_ACCESSIBLE_RELATION_SET_SIZE + 1)
#define N_STATES        (GTK_ACCESSIBLE_STATE_SELECTED + 1)

static const char *property_attrs[] = {
  [GTK_ACCESSIBLE_PROPERTY_AUTOCOMPLETE]        = "autocomplete",
  [GTK_ACCESSIBLE_PROPERTY_DESCRIPTION]         = "description",
  [GTK_ACCESSIBLE_PROPERTY_HAS_POPUP]           = "haspopup",
  [GTK_ACCESSIBLE_PROPERTY_KEY_SHORTCUTS]       = "keyshortcuts",
  [GTK_ACCESSIBLE_PROPERTY_LABEL]               = "label",
  [GTK_ACCESSIBLE_PROPERTY_LEVEL]               = "level",
  [GTK_ACCESSIBLE_PROPERTY_MODAL]               = "modal",
  [GTK_ACCESSIBLE_PROPERTY_MULTI_LINE]          = "multiline",
  [GTK_ACCESSIBLE_PROPERTY_MULTI_SELECTABLE]    = "multiselectable",
  [GTK_ACCESSIBLE_PROPERTY_ORIENTATION]         = "orientation",
  [GTK_ACCESSIBLE_PROPERTY_PLACEHOLDER]         = "placeholder",
  [GTK_ACCESSIBLE_PROPERTY_READ_ONLY]           = "readonly",
  [GTK_ACCESSIBLE_PROPERTY_REQUIRED]            = "required",
  [GTK_ACCESSIBLE_PROPERTY_ROLE_DESCRIPTION]    = "roledescription",
  [GTK_ACCESSIBLE_PROPERTY_SORT]                = "sort",
  [GTK_ACCESSIBLE_PROPERTY_VALUE_MAX]           = "valuemax",
  [GTK_ACCESSIBLE_PROPERTY_VALUE_MIN]           = "valuemin",
  [GTK_ACCESSIBLE_PROPERTY_VALUE_NOW]           = "valuenow",
  [GTK_ACCESSIBLE_PROPERTY_VALUE_TEXT]          = "valuetext",
};

Emmanuele Bassi's avatar
Emmanuele Bassi committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*< private >
 * gtk_accessible_property_get_attribute_name:
 * @property: a #GtkAccessibleProperty
 *
 * Retrieves the name of a #GtkAccessibleProperty.
 *
 * Returns: (transfer none): the name of the accessible property
 */
const char *
gtk_accessible_property_get_attribute_name (GtkAccessibleProperty property)
{
  g_return_val_if_fail (property >= GTK_ACCESSIBLE_PROPERTY_AUTOCOMPLETE &&
                        property <= GTK_ACCESSIBLE_PROPERTY_VALUE_TEXT,
                        "<none>");

  return property_attrs[property];
}

266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
static const char *relation_attrs[] = {
  [GTK_ACCESSIBLE_RELATION_ACTIVE_DESCENDANT]   = "activedescendant",
  [GTK_ACCESSIBLE_RELATION_COL_COUNT]           = "colcount",
  [GTK_ACCESSIBLE_RELATION_COL_INDEX]           = "colindex",
  [GTK_ACCESSIBLE_RELATION_COL_INDEX_TEXT]      = "colindextext",
  [GTK_ACCESSIBLE_RELATION_COL_SPAN]            = "colspan",
  [GTK_ACCESSIBLE_RELATION_CONTROLS]            = "controls",
  [GTK_ACCESSIBLE_RELATION_DESCRIBED_BY]        = "describedby",
  [GTK_ACCESSIBLE_RELATION_DETAILS]             = "details",
  [GTK_ACCESSIBLE_RELATION_ERROR_MESSAGE]       = "errormessage",
  [GTK_ACCESSIBLE_RELATION_FLOW_TO]             = "flowto",
  [GTK_ACCESSIBLE_RELATION_LABELLED_BY]         = "labelledby",
  [GTK_ACCESSIBLE_RELATION_OWNS]                = "owns",
  [GTK_ACCESSIBLE_RELATION_POS_IN_SET]          = "posinset",
  [GTK_ACCESSIBLE_RELATION_ROW_COUNT]           = "rowcount",
  [GTK_ACCESSIBLE_RELATION_ROW_INDEX]           = "rowindex",
  [GTK_ACCESSIBLE_RELATION_ROW_INDEX_TEXT]      = "rowindextext",
  [GTK_ACCESSIBLE_RELATION_ROW_SPAN]            = "rowspan",
  [GTK_ACCESSIBLE_RELATION_SET_SIZE]            = "setsize",
};

Emmanuele Bassi's avatar
Emmanuele Bassi committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*< private >
 * gtk_accessible_relation_get_attribute_name:
 * @relation: a #GtkAccessibleRelation
 *
 * Retrieves the name of a #GtkAccessibleRelation.
 *
 * Returns: (transfer none): the name of the accessible relation
 */
const char *
gtk_accessible_relation_get_attribute_name (GtkAccessibleRelation relation)
{
  g_return_val_if_fail (relation >= GTK_ACCESSIBLE_RELATION_ACTIVE_DESCENDANT &&
                        relation <= GTK_ACCESSIBLE_RELATION_SET_SIZE,
                        "<none>");

  return relation_attrs[relation];
}

305
306
307
308
309
310
311
312
313
314
315
static const char *state_attrs[] = {
  [GTK_ACCESSIBLE_STATE_BUSY]           = "busy",
  [GTK_ACCESSIBLE_STATE_CHECKED]        = "checked",
  [GTK_ACCESSIBLE_STATE_DISABLED]       = "disabled",
  [GTK_ACCESSIBLE_STATE_EXPANDED]       = "expanded",
  [GTK_ACCESSIBLE_STATE_HIDDEN]         = "hidden",
  [GTK_ACCESSIBLE_STATE_INVALID]        = "invalid",
  [GTK_ACCESSIBLE_STATE_PRESSED]        = "pressed",
  [GTK_ACCESSIBLE_STATE_SELECTED]       = "selected",
};

Emmanuele Bassi's avatar
Emmanuele Bassi committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*< private >
 * gtk_accessible_state_get_attribute_name:
 * @state: a #GtkAccessibleState
 *
 * Retrieves the name of a #GtkAccessibleState.
 *
 * Returns: (transfer none): the name of the accessible state
 */
const char *
gtk_accessible_state_get_attribute_name (GtkAccessibleState state)
{
  g_return_val_if_fail (state >= GTK_ACCESSIBLE_STATE_BUSY &&
                        state <= GTK_ACCESSIBLE_STATE_SELECTED,
                        "<none>");

  return state_attrs[state];
}

Emmanuele Bassi's avatar
Emmanuele Bassi committed
334
335
336
static void
gtk_at_context_init (GtkATContext *self)
{
337
  self->accessible_role = GTK_ACCESSIBLE_ROLE_NONE;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
338

339
340
341
342
343
344
345
346
347
348
349
350
  self->properties =
    gtk_accessible_attribute_set_new (N_PROPERTIES,
                                      property_attrs,
                                      (GtkAccessibleAttributeDefaultFunc) gtk_accessible_value_get_default_for_property);
  self->relations =
    gtk_accessible_attribute_set_new (N_RELATIONS,
                                      relation_attrs,
                                      (GtkAccessibleAttributeDefaultFunc) gtk_accessible_value_get_default_for_relation);
  self->states =
    gtk_accessible_attribute_set_new (N_STATES,
                                      state_attrs,
                                      (GtkAccessibleAttributeDefaultFunc) gtk_accessible_value_get_default_for_state);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
}

/**
 * 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)
{
380
  g_return_val_if_fail (GTK_IS_AT_CONTEXT (self), GTK_ACCESSIBLE_ROLE_NONE);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
381
382
383
384

  return self->accessible_role;
}

385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*< private >
 * gtk_at_context_get_display:
 * @self: a #GtkATContext
 *
 * Retrieves the #GdkDisplay used to create the context.
 *
 * Returns: (transfer none): a #GdkDisplay
 */
GdkDisplay *
gtk_at_context_get_display (GtkATContext *self)
{
  g_return_val_if_fail (GTK_IS_AT_CONTEXT (self), NULL);

  return self->display;
}

401
402
403
static const struct {
  const char *name;
  GtkATContext * (* create_context) (GtkAccessibleRole accessible_role,
404
405
                                     GtkAccessible    *accessible,
                                     GdkDisplay       *display);
406
407
} a11y_backends[] = {
#if defined(GDK_WINDOWING_WAYLAND)
408
  { "AT-SPI (Wayland)", gtk_at_spi_create_context },
409
410
#endif
#if defined(GDK_WINDOWING_X11)
411
  { "AT-SPI (X11)", gtk_at_spi_create_context },
412
413
414
415
#endif
  { NULL, NULL },
};

416
417
/**
 * gtk_at_context_create: (constructor)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
418
419
 * @accessible_role: the accessible role used by the #GtkATContext
 * @accessible: the #GtkAccessible implementation using the #GtkATContext
420
 * @display: the #GdkDisplay used by the #GtkATContext
Emmanuele Bassi's avatar
Emmanuele Bassi committed
421
 *
422
423
 * Creates a new #GtkATContext instance for the given accessible role,
 * accessible instance, and display connection.
Emmanuele Bassi's avatar
Emmanuele Bassi committed
424
425
426
427
 *
 * The #GtkATContext implementation being instantiated will depend on the
 * platform.
 *
428
 * Returns: (nullable) (transfer full): the #GtkATContext
Emmanuele Bassi's avatar
Emmanuele Bassi committed
429
430
431
 */
GtkATContext *
gtk_at_context_create (GtkAccessibleRole  accessible_role,
432
433
                       GtkAccessible     *accessible,
                       GdkDisplay        *display)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
{
  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;

465
466
467
468
  GtkATContext *res = NULL;

  for (guint i = 0; i < G_N_ELEMENTS (a11y_backends); i++)
    {
469
470
471
      if (a11y_backends[i].name == NULL)
        break;

472
473
474
      GTK_NOTE (A11Y, g_message ("Trying %s a11y backend", a11y_backends[i].name));
      if (a11y_backends[i].create_context != NULL)
        {
475
476
477
          res = a11y_backends[i].create_context (accessible_role, accessible, display);
          if (res != NULL)
            break;
478
479
480
        }
    }

481
  /* Fall back to the test context, so we can get debugging data */
482
  if (res == NULL)
483
484
485
486
487
    res = g_object_new (GTK_TYPE_TEST_AT_CONTEXT,
                        "accessible_role", accessible_role,
                        "accessible", accessible,
                        "display", display,
                        NULL);
488

Emmanuele Bassi's avatar
Emmanuele Bassi committed
489
  /* FIXME: Add GIOExtension for AT contexts */
490
  return res;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
491
492
493
}

/*< private >
494
 * gtk_at_context_update:
Emmanuele Bassi's avatar
Emmanuele Bassi committed
495
496
497
 * @self: a #GtkATContext
 *
 * Notifies the AT connected to this #GtkATContext that the accessible
498
 * state and its properties have changed.
Emmanuele Bassi's avatar
Emmanuele Bassi committed
499
500
 */
void
501
gtk_at_context_update (GtkATContext *self)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
502
503
504
{
  g_return_if_fail (GTK_IS_AT_CONTEXT (self));

505
506
507
508
509
510
  /* There's no point in notifying of state changes if there weren't any */
  if (self->updated_properties == 0 &&
      self->updated_relations == 0 &&
      self->updated_states == 0)
    return;

511
512
513
514
515
516
  GtkAccessibleStateChange changed_states =
    gtk_accessible_attribute_set_get_changed (self->states);
  GtkAccessiblePropertyChange changed_properties =
    gtk_accessible_attribute_set_get_changed (self->properties);
  GtkAccessibleRelationChange changed_relations =
    gtk_accessible_attribute_set_get_changed (self->relations);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
517

518
519
520
521
  GTK_AT_CONTEXT_GET_CLASS (self)->state_change (self,
                                                 changed_states, changed_properties, changed_relations,
                                                 self->states, self->properties, self->relations);
  g_signal_emit (self, obj_signals[STATE_CHANGE], 0);
522
523
524
525

  self->updated_properties = 0;
  self->updated_relations = 0;
  self->updated_states = 0;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
526
527
}

528
/*< private >
Emmanuele Bassi's avatar
Emmanuele Bassi committed
529
 * gtk_at_context_set_accessible_state:
530
531
532
533
534
535
536
537
 * @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.
 *
538
 * This function will accumulate state changes until gtk_at_context_update()
539
540
541
542
543
544
545
546
547
 * is called.
 */
void
gtk_at_context_set_accessible_state (GtkATContext       *self,
                                     GtkAccessibleState  state,
                                     GtkAccessibleValue *value)
{
  g_return_if_fail (GTK_IS_AT_CONTEXT (self));

548
549
  gboolean res = FALSE;

550
  if (value != NULL)
551
    res = gtk_accessible_attribute_set_add (self->states, state, value);
552
  else
553
554
555
556
    res = gtk_accessible_attribute_set_remove (self->states, state);

  if (res)
    self->updated_states |= (1 << state);
557
558
}

Emmanuele Bassi's avatar
Emmanuele Bassi committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*< private >
 * gtk_at_context_has_accessible_state:
 * @self: a #GtkATContext
 * @state: a #GtkAccessibleState
 *
 * Checks whether a #GtkATContext has the given @state set.
 *
 * Returns: %TRUE, if the accessible state is set
 */
gboolean
gtk_at_context_has_accessible_state (GtkATContext       *self,
                                     GtkAccessibleState  state)
{
  g_return_val_if_fail (GTK_IS_AT_CONTEXT (self), FALSE);

  return gtk_accessible_attribute_set_contains (self->states, state);
}

577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*< private >
 * gtk_at_context_get_accessible_state:
 * @self: a #GtkATContext
 * @state: a #GtkAccessibleState
 *
 * Retrieves the value for the accessible state of a #GtkATContext.
 *
 * Returns: (transfer none): the value for the given state
 */
GtkAccessibleValue *
gtk_at_context_get_accessible_state (GtkATContext       *self,
                                     GtkAccessibleState  state)
{
  g_return_val_if_fail (GTK_IS_AT_CONTEXT (self), NULL);

  return gtk_accessible_attribute_set_get_value (self->states, state);
}

595
596
597
598
599
600
601
602
603
604
/*< 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.
 *
605
 * This function will accumulate property changes until gtk_at_context_update()
606
607
 * is called.
 */
Emmanuele Bassi's avatar
Emmanuele Bassi committed
608
void
609
610
611
gtk_at_context_set_accessible_property (GtkATContext          *self,
                                        GtkAccessibleProperty  property,
                                        GtkAccessibleValue    *value)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
612
613
614
{
  g_return_if_fail (GTK_IS_AT_CONTEXT (self));

615
616
  gboolean res = FALSE;

617
  if (value != NULL)
618
    res = gtk_accessible_attribute_set_add (self->properties, property, value);
619
  else
620
621
622
623
    res = gtk_accessible_attribute_set_remove (self->properties, property);

  if (res)
    self->updated_properties |= (1 << property);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
624
}
Emmanuele Bassi's avatar
Emmanuele Bassi committed
625

Emmanuele Bassi's avatar
Emmanuele Bassi committed
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/*< private >
 * gtk_at_context_has_accessible_property:
 * @self: a #GtkATContext
 * @property: a #GtkAccessibleProperty
 *
 * Checks whether a #GtkATContext has the given @property set.
 *
 * Returns: %TRUE, if the accessible property is set
 */
gboolean
gtk_at_context_has_accessible_property (GtkATContext          *self,
                                        GtkAccessibleProperty  property)
{
  g_return_val_if_fail (GTK_IS_AT_CONTEXT (self), FALSE);

  return gtk_accessible_attribute_set_contains (self->properties, property);
}

644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*< private >
 * gtk_at_context_get_accessible_property:
 * @self: a #GtkATContext
 * @property: a #GtkAccessibleProperty
 *
 * Retrieves the value for the accessible property of a #GtkATContext.
 *
 * Returns: (transfer none): the value for the given property
 */
GtkAccessibleValue *
gtk_at_context_get_accessible_property (GtkATContext          *self,
                                        GtkAccessibleProperty  property)
{
  g_return_val_if_fail (GTK_IS_AT_CONTEXT (self), NULL);

  return gtk_accessible_attribute_set_get_value (self->properties, property);
}

Emmanuele Bassi's avatar
Emmanuele Bassi committed
662
663
664
665
666
667
668
669
670
671
/*< private >
 * gtk_at_context_set_accessible_relation:
 * @self: a #GtkATContext
 * @relation: a #GtkAccessibleRelation
 * @value: (nullable): #GtkAccessibleValue
 *
 * Sets the @value for the given @relation of a #GtkATContext.
 *
 * If @value is %NULL, the relation is unset.
 *
672
 * This function will accumulate relation changes until gtk_at_context_update()
Emmanuele Bassi's avatar
Emmanuele Bassi committed
673
674
675
676
677
678
679
680
681
 * is called.
 */
void
gtk_at_context_set_accessible_relation (GtkATContext          *self,
                                        GtkAccessibleRelation  relation,
                                        GtkAccessibleValue    *value)
{
  g_return_if_fail (GTK_IS_AT_CONTEXT (self));

682
683
  gboolean res = FALSE;

Emmanuele Bassi's avatar
Emmanuele Bassi committed
684
  if (value != NULL)
685
    res = gtk_accessible_attribute_set_add (self->relations, relation, value);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
686
  else
687
688
689
690
    res = gtk_accessible_attribute_set_remove (self->relations, relation);

  if (res)
    self->updated_relations |= (1 << relation);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
691
}
Emmanuele Bassi's avatar
Emmanuele Bassi committed
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709

/*< private >
 * gtk_at_context_has_accessible_relation:
 * @self: a #GtkATContext
 * @relation: a #GtkAccessibleRelation
 *
 * Checks whether a #GtkATContext has the given @relation set.
 *
 * Returns: %TRUE, if the accessible relation is set
 */
gboolean
gtk_at_context_has_accessible_relation (GtkATContext          *self,
                                        GtkAccessibleRelation  relation)
{
  g_return_val_if_fail (GTK_IS_AT_CONTEXT (self), FALSE);

  return gtk_accessible_attribute_set_contains (self->relations, relation);
}
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727

/*< private >
 * gtk_at_context_get_accessible_relation:
 * @self: a #GtkATContext
 * @relation: a #GtkAccessibleRelation
 *
 * Retrieves the value for the accessible relation of a #GtkATContext.
 *
 * Returns: (transfer none): the value for the given relation
 */
GtkAccessibleValue *
gtk_at_context_get_accessible_relation (GtkATContext          *self,
                                        GtkAccessibleRelation  relation)
{
  g_return_val_if_fail (GTK_IS_AT_CONTEXT (self), NULL);

  return gtk_accessible_attribute_set_get_value (self->relations, relation);
}
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807

/*< private >
 * gtk_at_context_get_label:
 * @self: a #GtkATContext
 *
 * Retrieves the accessible label of the #GtkATContext.
 *
 * This is a convenience function meant to be used by #GtkATContext implementations.
 *
 * Returns: (transfer full): the label of the #GtkATContext
 */
char *
gtk_at_context_get_label (GtkATContext *self)
{
  g_return_val_if_fail (GTK_IS_AT_CONTEXT (self), NULL);

  GtkAccessibleValue *value = NULL;

  if (gtk_accessible_attribute_set_contains (self->states, GTK_ACCESSIBLE_STATE_HIDDEN))
    {
      value = gtk_accessible_attribute_set_get_value (self->states, GTK_ACCESSIBLE_STATE_HIDDEN);

      if (gtk_boolean_accessible_value_get (value))
        return g_strdup ("");
    }

  if (gtk_accessible_attribute_set_contains (self->properties, GTK_ACCESSIBLE_PROPERTY_LABEL))
    {
      value = gtk_accessible_attribute_set_get_value (self->properties, GTK_ACCESSIBLE_PROPERTY_LABEL);

      return g_strdup (gtk_string_accessible_value_get (value));
    }

  if (gtk_accessible_attribute_set_contains (self->relations, GTK_ACCESSIBLE_RELATION_LABELLED_BY))
    {
      value = gtk_accessible_attribute_set_get_value (self->relations, GTK_ACCESSIBLE_RELATION_LABELLED_BY);

      GtkAccessible *rel = gtk_reference_accessible_value_get (value);
      GtkATContext *rel_context = gtk_accessible_get_at_context (rel);

      return gtk_at_context_get_label (rel_context);
    }

  GtkAccessibleRole role = gtk_at_context_get_accessible_role (self);

  switch ((int) role)
    {
    case GTK_ACCESSIBLE_ROLE_RANGE:
      {
        int range_attrs[] = {
          GTK_ACCESSIBLE_PROPERTY_VALUE_TEXT,
          GTK_ACCESSIBLE_PROPERTY_VALUE_NOW,
        };

        for (int i = 0; i < G_N_ELEMENTS (range_attrs); i++)
          {
            if (gtk_accessible_attribute_set_contains (self->properties, range_attrs[i]))
              {
                value = gtk_accessible_attribute_set_get_value (self->properties, range_attrs[i]);
                break;
              }
          }

        if (value != NULL)
          return g_strdup (gtk_string_accessible_value_get (value));
      }
      break;

    default:
      break;
    }

  GEnumClass *enum_class = g_type_class_peek (GTK_TYPE_ACCESSIBLE_ROLE);
  GEnumValue *enum_value = g_enum_get_value (enum_class, role);

  if (enum_value != NULL)
    return g_strdup (enum_value->value_nick);

  return g_strdup ("widget");
}