File: gtkaccesskitcontext.c

package info (click to toggle)
gtk4 4.20.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 187,060 kB
  • sloc: ansic: 779,084; xml: 3,093; javascript: 3,054; python: 1,911; java: 752; sh: 682; makefile: 315; perl: 162; cpp: 21
file content (2119 lines) | stat: -rw-r--r-- 71,151 bytes parent folder | download
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
60
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
112
113
114
115
116
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
172
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
/* gtkaccesskitcontext.c: AccessKit GtkATContext implementation
 *
 * Copyright 2024  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/>.
 */

#include "config.h"

#include "gtkaccesskitcontextprivate.h"

#include "gtkaccessibleprivate.h"
#include "gtkaccesskitrootprivate.h"

#include "gtkdebug.h"
#include "gtkprivate.h"
#include "gtkeditable.h"
#include "gtkentryprivate.h"
#include "gtkinscriptionprivate.h"
#include "gtklabelprivate.h"
#include "gtkmodelbuttonprivate.h"
#include "gtkpasswordentry.h"
#include "gtkroot.h"
#include "gtkscrolledwindow.h"
#include "gtkstack.h"
#include "gtktextview.h"
#include "gtktextviewprivate.h"
#include "gtktextbufferprivate.h"
#include "gtktextiterprivate.h"
#include "gtktypebuiltins.h"
#include "gtkwindow.h"

#include "gtkmenubutton.h"
#include "gtkcolordialogbutton.h"
#include "gtkfontdialogbutton.h"
#include "gtkscalebutton.h"
#include "print/gtkprinteroptionwidgetprivate.h"

#include <locale.h>

typedef struct _GtkAccessKitTextLayout
{
  guint32 id;
  double offset_x;
  double offset_y;
  GArray *children;
} GtkAccessKitTextLayout;

struct _GtkAccessKitContext
{
  GtkATContext parent_instance;

  /* Root object for the surface */
  GtkAccessKitRoot *root;

  /* The AccessKit node ID; meaningless if root is null. Note that this ID
     is not a full 64-bit AccessKit node ID, for two reasons:
     1. It isn't possible to convert 64-bit integers to pointers
        (as required to use these IDs as GHashTable keys) on all platforms.
     2. By using only 32 bits for the IDs of AT contexts, we can use the other
        32 bits to identify inline text boxes or other children within
        a given context. */
  guint32 id;

  GtkAccessKitTextLayout single_text_layout;
  GHashTable *text_view_lines;
  GHashTable *text_view_lines_by_id;
};

G_DEFINE_TYPE (GtkAccessKitContext, gtk_accesskit_context, GTK_TYPE_AT_CONTEXT)

static void
gtk_accesskit_context_finalize (GObject *gobject)
{
  GtkAccessKitContext *self = GTK_ACCESSKIT_CONTEXT (gobject);

  g_clear_object (&self->root);
  g_clear_pointer (&self->single_text_layout.children, g_array_unref);
  g_clear_pointer (&self->text_view_lines, g_hash_table_destroy);
  g_clear_pointer (&self->text_view_lines_by_id, g_hash_table_destroy);

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

static void
gtk_accesskit_context_realize (GtkATContext *context)
{
  GtkAccessKitContext *self = GTK_ACCESSKIT_CONTEXT (context);
  GtkAccessible *accessible = gtk_at_context_get_accessible (context);


  if (GTK_IS_ROOT (accessible))
    self->root = gtk_accesskit_root_new (GTK_ROOT (accessible));
  else
    {
      GtkAccessible *parent = gtk_accessible_get_accessible_parent (accessible);
      GtkATContext *parent_ctx = gtk_accessible_get_at_context (parent);
      GtkAccessKitContext *parent_accesskit_ctx = GTK_ACCESSKIT_CONTEXT (parent_ctx);
      gtk_at_context_realize (parent_ctx);
      self->root = g_object_ref (parent_accesskit_ctx->root);
      g_object_unref (parent_ctx);
      g_object_unref (parent);
    }

  self->id = gtk_accesskit_root_add_context (self->root, self);
}

static void
gtk_accesskit_context_unrealize (GtkATContext *context)
{
  GtkAccessKitContext *self = GTK_ACCESSKIT_CONTEXT (context);
  GtkAccessible *accessible = gtk_at_context_get_accessible (context);

  GTK_DEBUG (A11Y, "Unrealizing AccessKit context for accessible '%s'",
                   G_OBJECT_TYPE_NAME (accessible));

  gtk_accesskit_root_remove_context (self->root, self->id);

  g_clear_object (&self->root);
  self->single_text_layout.id = 0;
  g_clear_pointer (&self->single_text_layout.children, g_array_unref);
  g_clear_pointer (&self->text_view_lines, g_hash_table_destroy);
  g_clear_pointer (&self->text_view_lines_by_id, g_hash_table_destroy);
}

static void
queue_update (GtkAccessKitContext *self, gboolean force_to_end)
{
  if (!self->root)
    return;

  gtk_accesskit_root_queue_update (self->root, self->id, force_to_end);
}

static void
gtk_accesskit_context_state_change (GtkATContext                *ctx,
                                    GtkAccessibleStateChange     changed_states,
                                    GtkAccessiblePropertyChange  changed_properties,
                                    GtkAccessibleRelationChange  changed_relations,
                                    GtkAccessibleAttributeSet   *states,
                                    GtkAccessibleAttributeSet   *properties,
                                    GtkAccessibleAttributeSet   *relations)
{
  GtkAccessKitContext *self = GTK_ACCESSKIT_CONTEXT (ctx);

  queue_update (self, FALSE);
}

static void
gtk_accesskit_context_platform_change (GtkATContext                *ctx,
                                       GtkAccessiblePlatformChange  change)
{
  GtkAccessKitContext *self = GTK_ACCESSKIT_CONTEXT (ctx);
  GtkAccessible *accessible = gtk_at_context_get_accessible (ctx);

  queue_update (self, FALSE);

  if (GTK_IS_ROOT (accessible) &&
      change == GTK_ACCESSIBLE_PLATFORM_CHANGE_ACTIVE)
    {
      gboolean active =
        gtk_accessible_get_platform_state (accessible,
                                           GTK_ACCESSIBLE_PLATFORM_STATE_ACTIVE);
      gtk_accesskit_root_update_window_focus_state (self->root, active);
    }
}

static void
invalidate_text_view_line_layout (gpointer key,
                                  gpointer value,
                                  gpointer user_data)
{
  GtkAccessKitTextLayout *layout = value;
  g_clear_pointer (&layout->children, g_array_unref);
}

static GtkAccessible *
editable_ancestor (GtkAccessible *accessible)
{
  GtkAccessible *ancestor = gtk_accessible_get_accessible_parent (accessible);

  while (ancestor)
    {
      GtkAccessible *next;

      if (GTK_IS_EDITABLE (ancestor))
        return ancestor;

      next = gtk_accessible_get_accessible_parent (ancestor);
      g_object_unref (ancestor);
      ancestor = next;
    }

  return NULL;
}

static void
queue_update_on_editable_ancestor (GtkAccessKitContext *self)
{
  GtkATContext *ctx = GTK_AT_CONTEXT (self);
  GtkAccessible *accessible = gtk_at_context_get_accessible (ctx);
  GtkAccessible *ancestor = editable_ancestor (accessible);
  GtkATContext *ancestor_ctx;

  if (!ancestor)
    return;

  ancestor_ctx =gtk_accessible_get_at_context (ancestor);
  /* The editable ancestor must come after the GtkText instance in the
     update queue, to ensure the AccessKit representation of the layout
     is rebuilt before the selection is updated on the ancestor. */
  queue_update (GTK_ACCESSKIT_CONTEXT (ancestor_ctx), TRUE /* force_to_end */);
  g_object_unref (ancestor_ctx);
  g_object_unref (ancestor);
}

static void
gtk_accesskit_context_bounds_change (GtkATContext *ctx)
{
  GtkAccessKitContext *self = GTK_ACCESSKIT_CONTEXT (ctx);

  g_clear_pointer (&self->single_text_layout.children, g_array_unref);
  if (self->text_view_lines)
    g_hash_table_foreach (self->text_view_lines,
                          invalidate_text_view_line_layout, NULL);

  queue_update (self, FALSE);
  queue_update_on_editable_ancestor (self);
}

static void
gtk_accesskit_context_child_change (GtkATContext             *ctx,
                                    GtkAccessibleChildChange  change,
                                    GtkAccessible            *child)
{
  GtkAccessKitContext *self = GTK_ACCESSKIT_CONTEXT (ctx);

  queue_update (self, FALSE);
}

static void
gtk_accesskit_context_announce (GtkATContext                      *context,
                                const char                        *message,
                                GtkAccessibleAnnouncementPriority  priority)
{
  /* TODO */
}

static void
gtk_accesskit_context_update_caret_position (GtkATContext *ctx)
{
  GtkAccessKitContext *self = GTK_ACCESSKIT_CONTEXT (ctx);

  queue_update (self, FALSE);
}

static void
gtk_accesskit_context_update_selection_bound (GtkATContext *ctx)
{
  GtkAccessKitContext *self = GTK_ACCESSKIT_CONTEXT (ctx);

  queue_update (self, FALSE);
  queue_update_on_editable_ancestor (self);
}

static void
gtk_accesskit_context_update_text_contents (GtkATContext *ctx,
                                            GtkAccessibleTextContentChange change,
                                            unsigned int start_offset,
                                            unsigned int end_offset)
{
  GtkAccessKitContext *self = GTK_ACCESSKIT_CONTEXT (ctx);
  GtkAccessible *accessible = gtk_at_context_get_accessible (ctx);

  g_clear_pointer (&self->single_text_layout.children, g_array_unref);

  if (GTK_IS_TEXT_VIEW (accessible) && self->text_view_lines)
    {
      GtkTextView *text_view = GTK_TEXT_VIEW (accessible);
      GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
      GtkTextIter current, end;

      gtk_text_buffer_get_iter_at_offset (buffer, &current, start_offset);
      gtk_text_buffer_get_iter_at_offset (buffer, &end, end_offset);

      do
        {
          GtkTextLine *line = _gtk_text_iter_get_text_line (&current);
          GtkAccessKitTextLayout *layout =
            g_hash_table_lookup (self->text_view_lines, line);

          if (layout)
            {
              if (change == GTK_ACCESSIBLE_TEXT_CONTENT_CHANGE_REMOVE &&
                  gtk_text_iter_get_line_offset (&current) == 0 &&
                  (gtk_text_iter_get_offset (&current) +
                   gtk_text_iter_get_chars_in_line (&current)) <= end_offset)
                {
                  g_hash_table_remove (self->text_view_lines_by_id,
                                       GUINT_TO_POINTER (layout->id));
                  g_hash_table_remove (self->text_view_lines, line);
                }
              else
                g_clear_pointer (&layout->children, g_array_unref);
            }

          if (gtk_text_iter_compare (&current, &end) == 0)
            break;
          gtk_text_iter_forward_line (&current);
        }
      while (gtk_text_iter_compare (&current, &end) <= 0);
    }

  /* TODO: other text widget types */

  queue_update (self, FALSE);
  queue_update_on_editable_ancestor (self);
}

static void
gtk_accesskit_context_class_init (GtkAccessKitContextClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GtkATContextClass *context_class = GTK_AT_CONTEXT_CLASS (klass);

  gobject_class->finalize = gtk_accesskit_context_finalize;

  context_class->realize = gtk_accesskit_context_realize;
  context_class->unrealize = gtk_accesskit_context_unrealize;
  context_class->state_change = gtk_accesskit_context_state_change;
  context_class->platform_change = gtk_accesskit_context_platform_change;
  context_class->bounds_change = gtk_accesskit_context_bounds_change;
  context_class->child_change = gtk_accesskit_context_child_change;
  context_class->announce = gtk_accesskit_context_announce;
  context_class->update_caret_position = gtk_accesskit_context_update_caret_position;
  context_class->update_selection_bound = gtk_accesskit_context_update_selection_bound;
  context_class->update_text_contents = gtk_accesskit_context_update_text_contents;
}

static void
gtk_accesskit_context_init (GtkAccessKitContext *self)
{
}

GtkATContext *
gtk_accesskit_create_context (GtkAccessibleRole  accessible_role,
                              GtkAccessible     *accessible,
                              GdkDisplay        *display)
{
  g_return_val_if_fail (GTK_IS_ACCESSIBLE (accessible), NULL);
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);

  return g_object_new (GTK_TYPE_ACCESSKIT_CONTEXT,
                       "accessible-role", accessible_role,
                       "accessible", accessible,
                       "display", display,
                       NULL);
}

static accesskit_role
gtk_accessible_role_to_accesskit_role (GtkAccessibleRole role)
{
  switch (role)
    {
    case GTK_ACCESSIBLE_ROLE_ALERT:
      return ACCESSKIT_ROLE_ALERT;

    case GTK_ACCESSIBLE_ROLE_ALERT_DIALOG:
      return ACCESSKIT_ROLE_ALERT_DIALOG;

    case GTK_ACCESSIBLE_ROLE_APPLICATION:
      return ACCESSKIT_ROLE_WINDOW;

    case GTK_ACCESSIBLE_ROLE_ARTICLE:
      return ACCESSKIT_ROLE_ARTICLE;

    case GTK_ACCESSIBLE_ROLE_BANNER:
      return ACCESSKIT_ROLE_BANNER;

    case GTK_ACCESSIBLE_ROLE_BLOCK_QUOTE:
      return ACCESSKIT_ROLE_BLOCKQUOTE;

    case GTK_ACCESSIBLE_ROLE_BUTTON:
      return ACCESSKIT_ROLE_BUTTON;

    case GTK_ACCESSIBLE_ROLE_CAPTION:
      return ACCESSKIT_ROLE_CAPTION;

    case GTK_ACCESSIBLE_ROLE_CELL:
      return ACCESSKIT_ROLE_CELL;

    case GTK_ACCESSIBLE_ROLE_CHECKBOX:
      return ACCESSKIT_ROLE_CHECK_BOX;

    case GTK_ACCESSIBLE_ROLE_COLUMN_HEADER:
      return ACCESSKIT_ROLE_COLUMN_HEADER;

    case GTK_ACCESSIBLE_ROLE_COMBO_BOX:
      return ACCESSKIT_ROLE_COMBO_BOX;

    case GTK_ACCESSIBLE_ROLE_COMMAND:
      return ACCESSKIT_ROLE_GENERIC_CONTAINER;

    case GTK_ACCESSIBLE_ROLE_COMMENT:
      return ACCESSKIT_ROLE_COMMENT;

    case GTK_ACCESSIBLE_ROLE_COMPOSITE:
      return ACCESSKIT_ROLE_GENERIC_CONTAINER;

    case GTK_ACCESSIBLE_ROLE_DIALOG:
      return ACCESSKIT_ROLE_DIALOG;

    case GTK_ACCESSIBLE_ROLE_DOCUMENT:
      return ACCESSKIT_ROLE_DOCUMENT;

    case GTK_ACCESSIBLE_ROLE_FEED:
      return ACCESSKIT_ROLE_FEED;

    case GTK_ACCESSIBLE_ROLE_FORM:
      return ACCESSKIT_ROLE_FORM;

    case GTK_ACCESSIBLE_ROLE_GENERIC:
      return ACCESSKIT_ROLE_GENERIC_CONTAINER;

    case GTK_ACCESSIBLE_ROLE_GRID:
      return ACCESSKIT_ROLE_GRID;

    case GTK_ACCESSIBLE_ROLE_GRID_CELL:
      return ACCESSKIT_ROLE_CELL;

    case GTK_ACCESSIBLE_ROLE_GROUP:
      return ACCESSKIT_ROLE_GROUP;

    case GTK_ACCESSIBLE_ROLE_HEADING:
      return ACCESSKIT_ROLE_HEADING;

    case GTK_ACCESSIBLE_ROLE_IMG:
      return ACCESSKIT_ROLE_IMAGE;

    case GTK_ACCESSIBLE_ROLE_INPUT:
      return ACCESSKIT_ROLE_TEXT_INPUT;

    case GTK_ACCESSIBLE_ROLE_LABEL:
      return ACCESSKIT_ROLE_LABEL;

    case GTK_ACCESSIBLE_ROLE_LANDMARK:
      return ACCESSKIT_ROLE_GENERIC_CONTAINER;

    case GTK_ACCESSIBLE_ROLE_LEGEND:
      return ACCESSKIT_ROLE_LEGEND;

    case GTK_ACCESSIBLE_ROLE_LINK:
      return ACCESSKIT_ROLE_LINK;

    case GTK_ACCESSIBLE_ROLE_LIST:
      return ACCESSKIT_ROLE_LIST;

    case GTK_ACCESSIBLE_ROLE_LIST_BOX:
      return ACCESSKIT_ROLE_LIST_BOX;

    case GTK_ACCESSIBLE_ROLE_LIST_ITEM:
      return ACCESSKIT_ROLE_LIST_ITEM;

    case GTK_ACCESSIBLE_ROLE_LOG:
      return ACCESSKIT_ROLE_LOG;

    case GTK_ACCESSIBLE_ROLE_MAIN:
      return ACCESSKIT_ROLE_MAIN;

    case GTK_ACCESSIBLE_ROLE_MARQUEE:
      return ACCESSKIT_ROLE_MARQUEE;

    case GTK_ACCESSIBLE_ROLE_MATH:
      return ACCESSKIT_ROLE_MATH;

    case GTK_ACCESSIBLE_ROLE_METER:
      return ACCESSKIT_ROLE_METER;

    case GTK_ACCESSIBLE_ROLE_MENU:
      return ACCESSKIT_ROLE_MENU;

    case GTK_ACCESSIBLE_ROLE_MENU_BAR:
      return ACCESSKIT_ROLE_MENU_BAR;

    case GTK_ACCESSIBLE_ROLE_MENU_ITEM:
      return ACCESSKIT_ROLE_MENU_ITEM;

    case GTK_ACCESSIBLE_ROLE_MENU_ITEM_CHECKBOX:
      return ACCESSKIT_ROLE_MENU_ITEM_CHECK_BOX;

    case GTK_ACCESSIBLE_ROLE_MENU_ITEM_RADIO:
      return ACCESSKIT_ROLE_MENU_ITEM_RADIO;

    case GTK_ACCESSIBLE_ROLE_NAVIGATION:
      return ACCESSKIT_ROLE_NAVIGATION;

    case GTK_ACCESSIBLE_ROLE_NONE:
      return ACCESSKIT_ROLE_GENERIC_CONTAINER;

    case GTK_ACCESSIBLE_ROLE_NOTE:
      return ACCESSKIT_ROLE_NOTE;

    case GTK_ACCESSIBLE_ROLE_OPTION:
      return ACCESSKIT_ROLE_LIST_BOX_OPTION;

    case GTK_ACCESSIBLE_ROLE_PARAGRAPH:
      return ACCESSKIT_ROLE_PARAGRAPH;

    case GTK_ACCESSIBLE_ROLE_PRESENTATION:
      return ACCESSKIT_ROLE_GENERIC_CONTAINER;

    case GTK_ACCESSIBLE_ROLE_PROGRESS_BAR:
      return ACCESSKIT_ROLE_PROGRESS_INDICATOR;

    case GTK_ACCESSIBLE_ROLE_RADIO:
      return ACCESSKIT_ROLE_RADIO_BUTTON;

    case GTK_ACCESSIBLE_ROLE_RADIO_GROUP:
      return ACCESSKIT_ROLE_RADIO_GROUP;

    case GTK_ACCESSIBLE_ROLE_RANGE:
      return ACCESSKIT_ROLE_GENERIC_CONTAINER;

    case GTK_ACCESSIBLE_ROLE_REGION:
      return ACCESSKIT_ROLE_REGION;

    case GTK_ACCESSIBLE_ROLE_ROW:
      return ACCESSKIT_ROLE_ROW;

    case GTK_ACCESSIBLE_ROLE_ROW_GROUP:
      return ACCESSKIT_ROLE_ROW_GROUP;

    case GTK_ACCESSIBLE_ROLE_ROW_HEADER:
      return ACCESSKIT_ROLE_ROW_HEADER;

    case GTK_ACCESSIBLE_ROLE_SCROLLBAR:
      return ACCESSKIT_ROLE_SCROLL_BAR;

    case GTK_ACCESSIBLE_ROLE_SEARCH:
      return ACCESSKIT_ROLE_SEARCH;

    case GTK_ACCESSIBLE_ROLE_SEARCH_BOX:
      return ACCESSKIT_ROLE_SEARCH_INPUT;

    case GTK_ACCESSIBLE_ROLE_SECTION:
      return ACCESSKIT_ROLE_SECTION;

    case GTK_ACCESSIBLE_ROLE_SECTION_HEAD:
      return ACCESSKIT_ROLE_GENERIC_CONTAINER;

    case GTK_ACCESSIBLE_ROLE_SELECT:
      return ACCESSKIT_ROLE_GENERIC_CONTAINER;

    case GTK_ACCESSIBLE_ROLE_SEPARATOR:
      return ACCESSKIT_ROLE_GENERIC_CONTAINER;

    case GTK_ACCESSIBLE_ROLE_SLIDER:
      return ACCESSKIT_ROLE_SLIDER;

    case GTK_ACCESSIBLE_ROLE_SPIN_BUTTON:
      return ACCESSKIT_ROLE_SPIN_BUTTON;

    case GTK_ACCESSIBLE_ROLE_STATUS:
      return ACCESSKIT_ROLE_STATUS;

    case GTK_ACCESSIBLE_ROLE_STRUCTURE:
      return ACCESSKIT_ROLE_GENERIC_CONTAINER;

    case GTK_ACCESSIBLE_ROLE_SWITCH:
      return ACCESSKIT_ROLE_SWITCH;

    case GTK_ACCESSIBLE_ROLE_TAB:
      return ACCESSKIT_ROLE_TAB;

    case GTK_ACCESSIBLE_ROLE_TABLE:
      return ACCESSKIT_ROLE_TABLE;

    case GTK_ACCESSIBLE_ROLE_TAB_LIST:
      return ACCESSKIT_ROLE_TAB_LIST;

    case GTK_ACCESSIBLE_ROLE_TAB_PANEL:
      return ACCESSKIT_ROLE_TAB_PANEL;

    case GTK_ACCESSIBLE_ROLE_TEXT_BOX:
      return ACCESSKIT_ROLE_TEXT_INPUT;

    case GTK_ACCESSIBLE_ROLE_TIME:
      return ACCESSKIT_ROLE_TIME_INPUT;

    case GTK_ACCESSIBLE_ROLE_TIMER:
      return ACCESSKIT_ROLE_TIMER;

    case GTK_ACCESSIBLE_ROLE_TOOLBAR:
      return ACCESSKIT_ROLE_TOOLBAR;

    case GTK_ACCESSIBLE_ROLE_TOOLTIP:
      return ACCESSKIT_ROLE_TOOLTIP;

    case GTK_ACCESSIBLE_ROLE_TREE:
      return ACCESSKIT_ROLE_TREE;

    case GTK_ACCESSIBLE_ROLE_TREE_GRID:
      return ACCESSKIT_ROLE_TREE_GRID;

    case GTK_ACCESSIBLE_ROLE_TREE_ITEM:
      return ACCESSKIT_ROLE_TREE_ITEM;

    case GTK_ACCESSIBLE_ROLE_WIDGET:
      return ACCESSKIT_ROLE_UNKNOWN;

    case GTK_ACCESSIBLE_ROLE_WINDOW:
      return ACCESSKIT_ROLE_WINDOW;

    case GTK_ACCESSIBLE_ROLE_TOGGLE_BUTTON:
      return ACCESSKIT_ROLE_BUTTON;

    case GTK_ACCESSIBLE_ROLE_TERMINAL:
      return ACCESSKIT_ROLE_TERMINAL;

    default:
      break;
    }

  return ACCESSKIT_ROLE_UNKNOWN;
}

static accesskit_role
accesskit_role_for_context (GtkATContext *context)
{
  GtkAccessible *accessible = gtk_at_context_get_accessible (context);
  GtkAccessibleRole role = gtk_at_context_get_accessible_role (context);
  accesskit_role result;

  if (GTK_IS_MENU_BUTTON (accessible) ||
      GTK_IS_COLOR_DIALOG_BUTTON (accessible) ||
      GTK_IS_FONT_DIALOG_BUTTON (accessible) ||
      GTK_IS_SCALE_BUTTON (accessible)
#ifdef G_OS_UNIX
      || GTK_IS_PRINTER_OPTION_WIDGET (accessible)
#endif
    )
    return ACCESSKIT_ROLE_GENERIC_CONTAINER;

  /* ARIA does not have a "password entry" role, so we need to fudge it here */
  if (GTK_IS_PASSWORD_ENTRY (accessible))
    return ACCESSKIT_ROLE_PASSWORD_INPUT;

  /* ARIA does not have a "scroll area" role */
  if (GTK_IS_SCROLLED_WINDOW (accessible))
    return ACCESSKIT_ROLE_SCROLL_VIEW;

  result = gtk_accessible_role_to_accesskit_role (role);

  if (result == ACCESSKIT_ROLE_TEXT_INPUT &&
      gtk_at_context_has_accessible_property (context, GTK_ACCESSIBLE_PROPERTY_MULTI_LINE))
    {
      GtkAccessibleValue *value;

      value = gtk_at_context_get_accessible_property (context,
                                                      GTK_ACCESSIBLE_PROPERTY_MULTI_LINE);
      if (gtk_boolean_accessible_value_get (value))
        result = ACCESSKIT_ROLE_MULTILINE_TEXT_INPUT;
    }

  return result;
}

guint32
gtk_accesskit_context_get_id (GtkAccessKitContext *self)
{
  g_assert (self->root);
  return self->id;
}

static void
set_bounds (GtkAccessible *accessible, accesskit_node *node)
{
  int x, y, width, height;

  if (gtk_accessible_get_bounds (accessible, &x, &y, &width, &height))
    {
      accesskit_vec2 p;
      accesskit_affine transform;
      accesskit_rect bounds = {0, 0, width, height};

      if (GTK_IS_ROOT (accessible) && GTK_IS_NATIVE (accessible))
        gtk_native_get_surface_transform (GTK_NATIVE (accessible), &p.x, &p.y);
      else
        {
          p.x = x;
          p.y = y;
        }

      transform = accesskit_affine_translate (p);
      accesskit_node_set_transform (node, transform);
      accesskit_node_set_bounds (node, bounds);
    }
}

typedef void (*AccessKitFlagSetter) (accesskit_node *);

static gboolean
set_flag_from_state (GtkATContext        *ctx,
                     GtkAccessibleState   state,
                     AccessKitFlagSetter  setter,
                     accesskit_node      *node)
{
  if (gtk_at_context_has_accessible_state (ctx, state))
    {
      GtkAccessibleValue *value;

      value = gtk_at_context_get_accessible_state (ctx, state);
      if (gtk_boolean_accessible_value_get (value))
        {
          setter (node);
          return TRUE;
        }
    }

  return FALSE;
}

static gboolean
set_flag_from_property (GtkATContext          *ctx,
                        GtkAccessibleProperty  property,
                        AccessKitFlagSetter    setter,
                        accesskit_node        *node)
{
  if (gtk_at_context_has_accessible_property (ctx, property))
    {
      GtkAccessibleValue *value;

      value = gtk_at_context_get_accessible_property (ctx, property);
      if (gtk_boolean_accessible_value_get (value))
        {
          setter (node);
          return TRUE;
        }
    }

  return FALSE;
}

typedef void (*AccessKitOptionalFlagSetter) (accesskit_node *, bool);

static gboolean
set_optional_flag_from_state (GtkATContext                *ctx,
                              GtkAccessibleState           state,
                              AccessKitOptionalFlagSetter  setter,
                              accesskit_node              *node)
{
  if (gtk_at_context_has_accessible_state (ctx, state))
    {
      GtkAccessibleValue *value;

      value = gtk_at_context_get_accessible_state (ctx, state);
      setter (node, gtk_boolean_accessible_value_get (value));
      return TRUE;
    }

  return FALSE;
}

static gboolean
set_toggled (GtkATContext       *ctx,
             GtkAccessibleState  state,
             accesskit_node     *node)
{
  if (gtk_at_context_has_accessible_state (ctx, state))
    {
      GtkAccessibleValue *value;
      accesskit_toggled toggled;

      value = gtk_at_context_get_accessible_state (ctx, state);

      switch (gtk_tristate_accessible_value_get (value))
        {
        case GTK_ACCESSIBLE_TRISTATE_FALSE:
          toggled = ACCESSKIT_TOGGLED_FALSE;
          break;

        case GTK_ACCESSIBLE_TRISTATE_TRUE:
          toggled = ACCESSKIT_TOGGLED_TRUE;
          break;

        case GTK_ACCESSIBLE_TRISTATE_MIXED:
        default:
          toggled = ACCESSKIT_TOGGLED_MIXED;
          break;
        }

      accesskit_node_set_toggled (node, toggled);
      return TRUE;
    }

  return FALSE;
}

typedef void (*AccessKitStringSetter) (accesskit_node *, const char *);

static gboolean
set_string_property (GtkATContext          *ctx,
                     GtkAccessibleProperty  property,
                     AccessKitStringSetter  setter,
                     accesskit_node        *node)
{
  if (gtk_at_context_has_accessible_property (ctx, property))
    {
      GtkAccessibleValue *value;
      const char *str;

      value = gtk_at_context_get_accessible_property (ctx, property);
      str = gtk_string_accessible_value_get (value);
      if (str)
        {
          setter (node, str);
          return TRUE;
        }
    }

  return FALSE;
}

static gboolean
set_string_from_relation (GtkATContext          *ctx,
                          GtkAccessibleRelation  relation,
                          AccessKitStringSetter  setter,
                          accesskit_node        *node)
{
  if (gtk_at_context_has_accessible_relation (ctx, relation))
    {
      GtkAccessibleValue *value;
      const char *str;

      value = gtk_at_context_get_accessible_relation (ctx, relation);
      str = gtk_string_accessible_value_get (value);
      if (str)
        {
          setter (node, str);
          return TRUE;
        }
    }

  return FALSE;
}

typedef void (*AccessKitSizeSetter) (accesskit_node *, size_t);

static gboolean
set_size_from_property (GtkATContext          *ctx,
                        GtkAccessibleProperty  property,
                        AccessKitSizeSetter    setter,
                        accesskit_node        *node)
{
  if (gtk_at_context_has_accessible_property (ctx, property))
    {
      GtkAccessibleValue *value;

      value = gtk_at_context_get_accessible_property (ctx, property);
      setter (node, gtk_int_accessible_value_get (value));
      return TRUE;
    }

  return FALSE;
}

static gboolean
set_size_from_relation (GtkATContext          *ctx,
                        GtkAccessibleRelation  relation,
                        AccessKitSizeSetter    setter,
                        accesskit_node        *node)
{
  if (gtk_at_context_has_accessible_relation (ctx, relation))
    {
      GtkAccessibleValue *value;

      value = gtk_at_context_get_accessible_relation (ctx, relation);
      setter (node, gtk_int_accessible_value_get (value));
      return TRUE;
    }

  return FALSE;
}

typedef void (*AccessKitDoubleSetter) (accesskit_node *, double);

static gboolean
set_double_property (GtkATContext          *ctx,
                     GtkAccessibleProperty  property,
                     AccessKitDoubleSetter  setter,
                     accesskit_node        *node)
{
  if (gtk_at_context_has_accessible_property (ctx, property))
    {
      GtkAccessibleValue *value;

      value = gtk_at_context_get_accessible_property (ctx, property);
      setter (node, gtk_number_accessible_value_get (value));
      return TRUE;
    }

  return FALSE;
}

typedef void (*AccessKitNodeIdSetter) (accesskit_node *, accesskit_node_id);

static gboolean
set_single_relation (GtkATContext          *ctx,
                    GtkAccessibleRelation  relation,
                    AccessKitNodeIdSetter  setter,
                    accesskit_node        *node)
{
  if (gtk_at_context_has_accessible_relation (ctx, relation))
    {
      GtkAccessibleValue *value;
      GtkAccessible *target;

      value = gtk_at_context_get_accessible_relation (ctx, relation);
      target = gtk_reference_accessible_value_get (value);

      if (target)
        {
          GtkATContext *target_ctx = gtk_accessible_get_at_context (target);

          gtk_at_context_realize (target_ctx);
          setter (node, GTK_ACCESSKIT_CONTEXT (target_ctx)->id);
          g_object_unref (target_ctx);

          return TRUE;
        }
    }

  return FALSE;
}

typedef void (*AccessKitNodeIdPusher) (accesskit_node *, accesskit_node_id);

static gboolean
set_multi_relation (GtkATContext          *ctx,
                    GtkAccessibleRelation  relation,
                    AccessKitNodeIdPusher  pusher,
                    accesskit_node        *node)
{
  if (gtk_at_context_has_accessible_relation (ctx, relation))
    {
      GtkAccessibleValue *value;
      GList *l;
      gboolean has_target;

      value = gtk_at_context_get_accessible_relation (ctx, relation);
      l = gtk_reference_list_accessible_value_get (value);
      has_target = (l != NULL);

      for (; l; l = l->next)
        {
          GtkATContext *target_ctx =
            gtk_accessible_get_at_context (GTK_ACCESSIBLE (l->data));

          gtk_at_context_realize (target_ctx);
          pusher (node, GTK_ACCESSKIT_CONTEXT (target_ctx)->id);
          g_object_unref (target_ctx);
        }

      return has_target;
    }

  return FALSE;
}

static void
set_bounds_from_pango (accesskit_node *node,
                       PangoRectangle *pango_rect,
                       double          offset_x,
                       double          offset_y)
{
  accesskit_rect rect;

  rect.x0 = offset_x + (double)(pango_rect->x) / PANGO_SCALE;
  rect.y0 = offset_y + (double)(pango_rect->y) / PANGO_SCALE;
  rect.x1 = offset_x + (double)(pango_rect->x + pango_rect->width) / PANGO_SCALE;
  rect.y1 = offset_y + (double)(pango_rect->y + pango_rect->height) / PANGO_SCALE;
  accesskit_node_set_bounds (node, rect);
}

static accesskit_node_id
run_node_id (GtkAccessKitTextLayout *layout,
             gint                    start_index)
{
  return ((accesskit_node_id)layout->id << 32) | start_index;
}

static void
add_run_node (GtkAccessKitTextLayout *layout,
              accesskit_tree_update  *update,
              gint                    start_index,
              accesskit_node         *node)
{
  accesskit_node_id id = run_node_id (layout, start_index);
  accesskit_tree_update_push_node (update, id, node);
  g_array_append_val (layout->children, id);
}

typedef struct _GtkAccessKitRunInfo
{
  PangoLayoutRun *run;
  PangoRectangle extents;
} GtkAccessKitRunInfo;

static int
compare_run_info (gconstpointer a, gconstpointer b)
{
  const GtkAccessKitRunInfo *run_info_a = a;
  const GtkAccessKitRunInfo *run_info_b = b;
  int a_offset = run_info_a->run->item->offset;
  int b_offset = run_info_b->run->item->offset;

  if (a_offset < b_offset)
    return -1;
  if (a_offset > b_offset)
    return 1;
  return 0;
}

static void
add_text_layout_inner (GtkAccessKitTextLayout *layout,
                       accesskit_tree_update  *update,
                       PangoLayout            *pango_layout,
                       const char             *end_delimiter,
                       double                  offset_x,
                       double                  offset_y)
{
  const char *text = pango_layout_get_text (pango_layout);
  const PangoLogAttr *log_attrs =
    pango_layout_get_log_attrs_readonly (pango_layout, NULL);
  PangoLayoutIter *iter = pango_layout_get_iter (pango_layout);
  GArray *line_runs = NULL;
  guint usv_offset = 0, byte_offset = 0;

  while (true)
    {
      if (pango_layout_iter_get_run_readonly (iter))
        {
          PangoLayoutRun *run = pango_layout_iter_get_run_readonly (iter);
          GtkAccessKitRunInfo run_info;

          run_info.run = run;
          pango_layout_iter_get_run_extents (iter, NULL, &run_info.extents);

          if (!line_runs)
            line_runs = g_array_new (FALSE, FALSE, sizeof (GtkAccessKitRunInfo));
          g_array_append_val (line_runs, run_info);

          /* We can always assume there's at least one more null run. */
          pango_layout_iter_next_run (iter);
        }
      else
        {
          PangoLayoutLine *line = pango_layout_iter_get_line_readonly (iter);
          PangoRectangle extents;
          PangoLayoutLine *next_line;
          guint line_end_byte_offset;

          pango_layout_iter_get_run_extents (iter, NULL, &extents);

          if (pango_layout_iter_next_line (iter))
            {
              next_line = pango_layout_iter_get_line_readonly (iter);
              line_end_byte_offset = next_line->start_index;
            }
          else
            {
              next_line = NULL;
              line_end_byte_offset = line->start_index + line->length;
            }

          if (line_runs)
            {
              guint prev_run_usv_offset = 0;
              guint i;

              g_array_sort (line_runs, compare_run_info);

              for (i = 0; i < line_runs->len; i++)
                {
                  GtkAccessKitRunInfo *run_info =
                    &(g_array_index (line_runs, GtkAccessKitRunInfo, i));
                  PangoLayoutRun *run = run_info->run;
                  PangoItem *item = run->item;
                  accesskit_node *node =
                    accesskit_node_new (ACCESSKIT_ROLE_TEXT_RUN);
                  guint node_text_byte_count;
                  gchar *node_text;
                  accesskit_text_direction dir;
                  int *log_widths = g_new0 (int, item->num_chars);
                  GArray *char_lengths =
                    g_array_new (FALSE, FALSE, sizeof (uint8_t));
                  GArray *word_lengths =
                    g_array_new (FALSE, FALSE, sizeof (uint8_t));
                  GArray *char_positions =
                    g_array_new (FALSE, FALSE, sizeof (float));
                  GArray *char_widths =
                    g_array_new (FALSE, FALSE, sizeof (float));
                  guint run_start_usv_offset = usv_offset;
                  guint last_word_start_char_offset = 0;
                  guint char_count = 0;
                  float char_pos = 0.0f;

                  g_assert (byte_offset == item->offset);

                  if (i > 0)
                    {
                      accesskit_node_id id =
                        run_node_id (layout, prev_run_usv_offset);
                      accesskit_node_set_previous_on_line (node, id);
                    }

                  set_bounds_from_pango (node, &run_info->extents, offset_x, offset_y);

                  if (i == (line_runs->len - 1))
                    node_text_byte_count =
                      line_end_byte_offset - byte_offset;
                  else
                    node_text_byte_count = item->length;
                  node_text = g_strndup (text + item->offset,
                                         node_text_byte_count);

                  if (i == (line_runs->len - 1) && !next_line && end_delimiter)
                    {
                      gchar *new_text = g_strconcat (node_text, end_delimiter,
                                                     NULL);
                      node_text_byte_count += strlen (end_delimiter);
                      g_free (node_text);
                      node_text = new_text;
                    }

                  accesskit_node_set_value (node, node_text);

                  /* The following logic for determining the run's direction
                     is copied from update_run in pango-layout.c. */
                  if ((item->analysis.level % 2) == 0)
                    dir = ACCESSKIT_TEXT_DIRECTION_LEFT_TO_RIGHT;
                  else
                    dir = ACCESSKIT_TEXT_DIRECTION_RIGHT_TO_LEFT;
                  accesskit_node_set_text_direction (node, dir);

                  /* TODO: attributes, once the AccessKit backends support them */

                  pango_glyph_item_get_logical_widths (run, text, log_widths);

                  while (byte_offset < (item->offset + node_text_byte_count))
                    {
                      guint char_start_byte_offset = byte_offset;
                      uint8_t char_len;

                      if (byte_offset >= (item->offset + item->length))
                        {
                          float width = 0.0f;

                          byte_offset =
                            item->offset + node_text_byte_count;
                          usv_offset +=
                            g_utf8_strlen (node_text + item->length,
                                           node_text_byte_count - item->length);
                          g_array_append_val (char_positions, char_pos);
                          g_array_append_val (char_widths, width);
                        }
                      else
                        {
                          float width = 0.0f;

                          if (log_attrs[usv_offset].is_word_start &&
                              (char_count > last_word_start_char_offset))
                            {
                              uint8_t word_len =
                                char_count - last_word_start_char_offset;

                              g_array_append_val (word_lengths, word_len);
                              last_word_start_char_offset = char_count;
                            }

                          do
                            {
                              width +=
                                (float) (log_widths[usv_offset - run_start_usv_offset]) / PANGO_SCALE;
                              byte_offset =
                                g_utf8_next_char (text + byte_offset) - text;
                              usv_offset++;
                            }
                          while (byte_offset < (item->offset + item->length) &&
                                 !log_attrs[usv_offset].is_cursor_position);

                          g_array_append_val (char_positions, char_pos);
                          g_array_append_val (char_widths, width);
                          char_pos += width;
                        }

                      char_len = byte_offset - char_start_byte_offset;
                      g_array_append_val (char_lengths, char_len);
                      char_count++;
                    }

                  if (char_count > last_word_start_char_offset)
                    {
                      uint8_t word_len =
                        char_count - last_word_start_char_offset;

                      g_array_append_val (word_lengths, word_len);
                    }

                  accesskit_node_set_character_lengths (node,
                                                        char_lengths->len,
                                                        (uint8_t *) char_lengths->data);
                  g_array_unref (char_lengths);
                  accesskit_node_set_word_lengths (node,
                                                   word_lengths->len,
                                                   (uint8_t *) word_lengths->data);
                  g_array_unref (word_lengths);
                  accesskit_node_set_character_positions (node,
                                                          char_positions->len,
                                                          (float *) char_positions->data);
                  g_array_unref (char_positions);
                  accesskit_node_set_character_widths (node,
                                                       char_widths->len,
                                                       (float *) char_widths->data);
                  g_array_unref (char_widths);

                  if (i < (line_runs->len - 1))
                    {
                      accesskit_node_id id = run_node_id (layout, usv_offset);
                      accesskit_node_set_next_on_line (node, id);
                    }

                  add_run_node (layout, update, run_start_usv_offset, node);
                  prev_run_usv_offset = run_start_usv_offset;
                  g_free (node_text);
                  g_free (log_widths);
                }

              g_clear_pointer (&line_runs, g_array_unref);
            }
          else
            {
              accesskit_node *node =
                accesskit_node_new (ACCESSKIT_ROLE_TEXT_RUN);
              uint8_t char_len = line_end_byte_offset - line->start_index;
              gchar *line_text = g_strndup (text + line->start_index, char_len);
              accesskit_text_direction dir;
              uint8_t char_count;
              float coord = 0.0f;

              g_assert (byte_offset == line->start_index);

              set_bounds_from_pango (node, &extents, offset_x, offset_y);

              if (!next_line && end_delimiter)
                {
                  gchar *new_text = g_strconcat (line_text, end_delimiter, NULL);
                  char_len += strlen (end_delimiter);
                  g_free (line_text);
                  line_text = new_text;
                }
              char_count = char_len ? 1 : 0;
              accesskit_node_set_value (node, line_text);

              switch (pango_layout_line_get_resolved_direction (line))
                {
                case PANGO_DIRECTION_RTL:
                case PANGO_DIRECTION_TTB_RTL:
                case PANGO_DIRECTION_WEAK_RTL:
                  dir = ACCESSKIT_TEXT_DIRECTION_RIGHT_TO_LEFT;
                  break;

                case PANGO_DIRECTION_LTR:
                case PANGO_DIRECTION_TTB_LTR:
                case PANGO_DIRECTION_WEAK_LTR:
                case PANGO_DIRECTION_NEUTRAL:
                default:
                  dir = ACCESSKIT_TEXT_DIRECTION_LEFT_TO_RIGHT;
                  break;
                }
              accesskit_node_set_text_direction (node, dir);

              accesskit_node_set_character_lengths (node, char_count, &char_len);
              accesskit_node_set_word_lengths (node, 1, &char_count);
              accesskit_node_set_character_positions (node, char_count, &coord);
              accesskit_node_set_character_widths (node, char_count, &coord);

              add_run_node (layout, update, usv_offset, node);
              byte_offset += char_len;
              usv_offset += g_utf8_strlen (line_text, char_len);
              g_free (line_text);
            }

          if (!next_line)
            break;
        }
    }

  /* Iteration should always end with a null run, and processing that null run
     should dispose of line_runs (see above). */
  g_assert (!line_runs);

  pango_layout_iter_free (iter);
}

static void
add_text_layout (GtkAccessKitTextLayout *layout,
                 accesskit_tree_update  *update,
                 accesskit_node         *parent_node,
                 PangoLayout            *pango_layout,
                 const char             *end_delimiter,
                 double                  offset_x,
                 double                  offset_y,
                 double                  inner_offset_x,
                 double                  inner_offset_y)
{
  g_assert (layout->id);

  if (!layout->children || offset_x != layout->offset_x ||
      offset_y != layout->offset_y)
    {
      accesskit_node *container_node =
        accesskit_node_new (ACCESSKIT_ROLE_GENERIC_CONTAINER);
      accesskit_vec2 p = {offset_x, offset_y};

      layout->offset_x = offset_x;
      layout->offset_y = offset_y;

      if (offset_x || offset_y)
        accesskit_node_set_transform (container_node,
                                      accesskit_affine_translate (p));

      if (!layout->children)
        {
          layout->children =
            g_array_new (FALSE, FALSE, sizeof (accesskit_node_id));
          add_text_layout_inner (layout, update, pango_layout, end_delimiter,
                                 inner_offset_x, inner_offset_y);
        }

      accesskit_node_set_children (container_node,
                                   layout->children->len,
                                   (accesskit_node_id *)
                                   layout->children->data);

      accesskit_tree_update_push_node (update, layout->id, container_node);
    }

  accesskit_node_push_child (parent_node, layout->id);
}

static void
add_single_text_layout (GtkAccessKitContext   *self,
                        accesskit_tree_update *update,
                        accesskit_node        *parent_node,
                        PangoLayout           *pango_layout,
                        double                 offset_x,
                        double                 offset_y)
{
  if (!self->single_text_layout.id)
    self->single_text_layout.id = gtk_accesskit_root_new_id (self->root);

  add_text_layout (&self->single_text_layout, update, parent_node,
                   pango_layout, NULL, offset_x, offset_y, 0.0, 0.0);
}

/* Adapted from gtkatspitext.c */
static GtkText *
gtk_editable_get_text_widget (GtkEditable *editable)
{
  guint redirects = 0;

  do {
    if (GTK_IS_TEXT (editable))
      return GTK_TEXT (editable);

    if (++redirects >= 6)
      g_assert_not_reached ();

    editable = gtk_editable_get_delegate (editable);
  } while (editable != NULL);

  return NULL;
}

static void
usv_offset_to_text_position (GtkAccessKitTextLayout  *layout,
                             PangoLayout             *pango_layout,
                             guint                    usv_offset,
                             accesskit_text_position *pos)
{
  gint i;
  accesskit_node_id id = 0;
  guint run_start_usv_offset = 0;
  const PangoLogAttr *attrs =
    pango_layout_get_log_attrs_readonly (pango_layout, NULL);

  for (i = layout->children->len - 1; i >= 0; i--)
    {
      id = g_array_index (layout->children, accesskit_node_id, i);
      run_start_usv_offset = id & 0xffffffff;

      if (run_start_usv_offset <= usv_offset)
        break;
    }

  g_assert (id != 0);

  pos->node = id;
  pos->character_index = 0;

  for (i = run_start_usv_offset; i < usv_offset; i++)
    {
      if (attrs[i].is_cursor_position)
        pos->character_index++;
    }
}

static void
text_view_mark_to_text_position (GtkAccessKitContext     *self,
                                 GtkTextView             *text_view,
                                 GtkTextMark             *mark,
                                 accesskit_text_position *pos)
{
  GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
  GtkTextLayout *layout = gtk_text_view_get_layout (text_view);
  GtkTextIter iter;
  GtkTextLine *line;
  GtkAccessKitTextLayout *line_layout;
  GtkTextLineDisplay *display;

  gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
  line = _gtk_text_iter_get_text_line (&iter);

  g_assert (self->text_view_lines);
  line_layout = g_hash_table_lookup (self->text_view_lines, line);
  g_assert (line_layout);
  g_assert (line_layout->children);

  display = gtk_text_layout_get_line_display (layout, line, FALSE);

  usv_offset_to_text_position (line_layout, display->layout,
                               gtk_text_iter_get_line_offset (&iter), pos);

  gtk_text_line_display_unref (display);
}

static void
destroy_text_view_lines_value (gpointer data)
{
  GtkAccessKitTextLayout *layout = data;

  g_clear_pointer (&layout->children, g_array_unref);
  g_free (layout);
}

void
gtk_accesskit_context_add_to_update (GtkAccessKitContext   *self,
                                     accesskit_tree_update *update)
{
  GtkATContext *ctx = GTK_AT_CONTEXT (self);
  accesskit_role role = accesskit_role_for_context (ctx);
  accesskit_node *node = accesskit_node_new (role);
  GtkAccessible *accessible = gtk_at_context_get_accessible (ctx);
  GtkAccessible *child = gtk_accessible_get_first_accessible_child (accessible);

  if (gtk_accessible_get_platform_state (accessible,
                                         GTK_ACCESSIBLE_PLATFORM_STATE_FOCUSABLE))
    accesskit_node_add_action (node, ACCESSKIT_ACTION_FOCUS);

  if (GTK_IS_BUTTON (accessible) ||
      GTK_IS_MODEL_BUTTON (accessible) ||
      GTK_IS_SWITCH (accessible) ||
      GTK_IS_EXPANDER (accessible))
    accesskit_node_add_action (node, ACCESSKIT_ACTION_CLICK);
  /* TODO: other actions */

  set_bounds (accessible, node);

  while (child)
    {
      GtkATContext *child_ctx = gtk_accessible_get_at_context (child);
      GtkAccessKitContext *child_accesskit_ctx = GTK_ACCESSKIT_CONTEXT (child_ctx);
      GtkAccessible *next = gtk_accessible_get_next_accessible_sibling (child);

      g_assert (gtk_at_context_is_realized (child_ctx));
      accesskit_node_push_child (node, child_accesskit_ctx->id);
      g_object_unref (child_ctx);
      g_object_unref (child);
      child = next;
    }

  set_flag_from_state (ctx, GTK_ACCESSIBLE_STATE_BUSY,
                       accesskit_node_set_busy, node);
  set_flag_from_state (ctx, GTK_ACCESSIBLE_STATE_DISABLED,
                       accesskit_node_set_disabled, node);
  set_flag_from_state (ctx, GTK_ACCESSIBLE_STATE_HIDDEN,
                       accesskit_node_set_hidden, node);
  set_flag_from_state (ctx, GTK_ACCESSIBLE_STATE_VISITED,
                       accesskit_node_set_visited, node);

  set_optional_flag_from_state (ctx, GTK_ACCESSIBLE_STATE_EXPANDED,
                                accesskit_node_set_expanded, node);
  set_optional_flag_from_state (ctx, GTK_ACCESSIBLE_STATE_SELECTED,
                                accesskit_node_set_selected, node);

  if (!set_toggled (ctx, GTK_ACCESSIBLE_STATE_CHECKED, node))
    set_toggled (ctx, GTK_ACCESSIBLE_STATE_PRESSED, node);


  if (gtk_at_context_has_accessible_state (ctx, GTK_ACCESSIBLE_STATE_INVALID))
    {
      GtkAccessibleValue *value;

      value = gtk_at_context_get_accessible_state (ctx, GTK_ACCESSIBLE_STATE_INVALID);

      switch (gtk_invalid_accessible_value_get (value))
        {
        case GTK_ACCESSIBLE_INVALID_TRUE:
          accesskit_node_set_invalid (node, ACCESSKIT_INVALID_TRUE);
          break;

        case GTK_ACCESSIBLE_INVALID_GRAMMAR:
          accesskit_node_set_invalid (node, ACCESSKIT_INVALID_GRAMMAR);
          break;

        case GTK_ACCESSIBLE_INVALID_SPELLING:
          accesskit_node_set_invalid (node, ACCESSKIT_INVALID_SPELLING);
          break;

        case GTK_ACCESSIBLE_INVALID_FALSE:
        default:
          break;
        }
    }

  set_flag_from_property (ctx, GTK_ACCESSIBLE_PROPERTY_MODAL,
                          accesskit_node_set_modal, node);
  set_flag_from_property (ctx, GTK_ACCESSIBLE_PROPERTY_MULTI_SELECTABLE,
                          accesskit_node_set_multiselectable, node);
  set_flag_from_property (ctx, GTK_ACCESSIBLE_PROPERTY_READ_ONLY,
                          accesskit_node_set_read_only, node);
  set_flag_from_property (ctx, GTK_ACCESSIBLE_PROPERTY_REQUIRED,
                          accesskit_node_set_required, node);

  set_string_property (ctx, GTK_ACCESSIBLE_PROPERTY_DESCRIPTION,
                       accesskit_node_set_description, node);
  set_string_property (ctx, GTK_ACCESSIBLE_PROPERTY_KEY_SHORTCUTS,
                       accesskit_node_set_keyboard_shortcut, node);
  set_string_property (ctx, GTK_ACCESSIBLE_PROPERTY_LABEL,
                       accesskit_node_set_label, node);
  set_string_property (ctx, GTK_ACCESSIBLE_PROPERTY_PLACEHOLDER,
                       accesskit_node_set_placeholder, node);
  set_string_property (ctx, GTK_ACCESSIBLE_PROPERTY_ROLE_DESCRIPTION,
                       accesskit_node_set_role_description, node);
  set_string_property (ctx, GTK_ACCESSIBLE_PROPERTY_VALUE_TEXT,
                       accesskit_node_set_value, node);

  set_size_from_property (ctx, GTK_ACCESSIBLE_PROPERTY_LEVEL,
                          accesskit_node_set_level, node);

  set_double_property (ctx, GTK_ACCESSIBLE_PROPERTY_VALUE_MAX,
                       accesskit_node_set_max_numeric_value, node);
  set_double_property (ctx, GTK_ACCESSIBLE_PROPERTY_VALUE_MIN,
                       accesskit_node_set_min_numeric_value, node);
  set_double_property (ctx, GTK_ACCESSIBLE_PROPERTY_VALUE_NOW,
                       accesskit_node_set_numeric_value, node);

  if (gtk_at_context_has_accessible_property (ctx, GTK_ACCESSIBLE_PROPERTY_AUTOCOMPLETE))
    {
      GtkAccessibleValue *value;

      value = gtk_at_context_get_accessible_property (ctx, GTK_ACCESSIBLE_PROPERTY_AUTOCOMPLETE);

      switch (gtk_autocomplete_accessible_value_get (value))
        {
        case GTK_ACCESSIBLE_AUTOCOMPLETE_INLINE:
          accesskit_node_set_auto_complete (node, ACCESSKIT_AUTO_COMPLETE_INLINE);
          break;

        case GTK_ACCESSIBLE_AUTOCOMPLETE_LIST:
          accesskit_node_set_auto_complete (node, ACCESSKIT_AUTO_COMPLETE_LIST);
          break;

        case GTK_ACCESSIBLE_AUTOCOMPLETE_BOTH:
          accesskit_node_set_auto_complete (node, ACCESSKIT_AUTO_COMPLETE_BOTH);
          break;

        case GTK_ACCESSIBLE_AUTOCOMPLETE_NONE:
        default:
          break;
        }
    }

  if (gtk_at_context_has_accessible_property (ctx, GTK_ACCESSIBLE_PROPERTY_HAS_POPUP))
    {
      GtkAccessibleValue *value;

      value = gtk_at_context_get_accessible_property (ctx, GTK_ACCESSIBLE_PROPERTY_HAS_POPUP);
      if (gtk_boolean_accessible_value_get (value))
        accesskit_node_set_has_popup(node, ACCESSKIT_HAS_POPUP_MENU);
    }

  if (gtk_at_context_has_accessible_property (ctx, GTK_ACCESSIBLE_PROPERTY_ORIENTATION))
    {
      GtkAccessibleValue *value;

      value = gtk_at_context_get_accessible_property (ctx, GTK_ACCESSIBLE_PROPERTY_ORIENTATION);

      switch (gtk_orientation_accessible_value_get (value))
        {
        case GTK_ORIENTATION_HORIZONTAL:
          accesskit_node_set_orientation (node, ACCESSKIT_ORIENTATION_HORIZONTAL);
          break;

        case GTK_ORIENTATION_VERTICAL:
          accesskit_node_set_orientation (node, ACCESSKIT_ORIENTATION_VERTICAL);
          break;

        default:
          break;
        }
    }

  if (gtk_at_context_has_accessible_property (ctx, GTK_ACCESSIBLE_PROPERTY_SORT))
    {
      GtkAccessibleValue *value;

      value = gtk_at_context_get_accessible_property (ctx, GTK_ACCESSIBLE_PROPERTY_SORT);

      switch (gtk_sort_accessible_value_get (value))
        {
        case GTK_ACCESSIBLE_SORT_ASCENDING:
          accesskit_node_set_sort_direction (node, ACCESSKIT_SORT_DIRECTION_ASCENDING);
          break;

        case GTK_ACCESSIBLE_SORT_DESCENDING:
          accesskit_node_set_sort_direction (node, ACCESSKIT_SORT_DIRECTION_DESCENDING);
          break;

        case GTK_ACCESSIBLE_SORT_OTHER:
          accesskit_node_set_sort_direction (node, ACCESSKIT_SORT_DIRECTION_OTHER);
          break;

        case GTK_ACCESSIBLE_SORT_NONE:
        default:
          break;
        }
    }

  set_single_relation (ctx, GTK_ACCESSIBLE_RELATION_ACTIVE_DESCENDANT,
                       accesskit_node_set_active_descendant, node);
  set_single_relation (ctx, GTK_ACCESSIBLE_RELATION_ERROR_MESSAGE,
                       accesskit_node_set_error_message, node);

  set_multi_relation (ctx, GTK_ACCESSIBLE_RELATION_CONTROLS,
                      accesskit_node_push_controlled, node);
  set_multi_relation (ctx, GTK_ACCESSIBLE_RELATION_DESCRIBED_BY,
                      accesskit_node_push_described_by, node);
  set_multi_relation (ctx, GTK_ACCESSIBLE_RELATION_DETAILS,
                      accesskit_node_push_detail, node);
  set_multi_relation (ctx, GTK_ACCESSIBLE_RELATION_FLOW_TO,
                      accesskit_node_push_flow_to, node);
  set_multi_relation (ctx, GTK_ACCESSIBLE_RELATION_LABELLED_BY,
                      accesskit_node_push_labelled_by, node);
  set_multi_relation (ctx, GTK_ACCESSIBLE_RELATION_OWNS,
                      accesskit_node_push_owned, node);

  set_size_from_relation (ctx, GTK_ACCESSIBLE_RELATION_COL_COUNT,
                          accesskit_node_set_column_count, node);
  set_size_from_relation (ctx, GTK_ACCESSIBLE_RELATION_COL_INDEX,
                          accesskit_node_set_column_index, node);
  set_size_from_relation (ctx, GTK_ACCESSIBLE_RELATION_COL_SPAN,
                          accesskit_node_set_column_span, node);
  set_size_from_relation (ctx, GTK_ACCESSIBLE_RELATION_POS_IN_SET,
                          accesskit_node_set_position_in_set, node);
  set_size_from_relation (ctx, GTK_ACCESSIBLE_RELATION_ROW_COUNT,
                          accesskit_node_set_row_count, node);
  set_size_from_relation (ctx, GTK_ACCESSIBLE_RELATION_ROW_INDEX,
                          accesskit_node_set_row_index, node);
  set_size_from_relation (ctx, GTK_ACCESSIBLE_RELATION_ROW_SPAN,
                          accesskit_node_set_row_span, node);
  set_size_from_relation (ctx, GTK_ACCESSIBLE_RELATION_SET_SIZE,
                          accesskit_node_set_size_of_set, node);

  set_string_from_relation (ctx, GTK_ACCESSIBLE_RELATION_COL_INDEX_TEXT,
                            accesskit_node_set_column_index_text, node);
  set_string_from_relation (ctx, GTK_ACCESSIBLE_RELATION_ROW_INDEX_TEXT,
                            accesskit_node_set_row_index_text, node);

  accesskit_node_set_class_name (node, g_type_name (G_TYPE_FROM_INSTANCE (accessible)));

  if (!(gtk_at_context_has_accessible_property (ctx, GTK_ACCESSIBLE_PROPERTY_LABEL) ||
        gtk_at_context_has_accessible_relation (ctx, GTK_ACCESSIBLE_RELATION_LABELLED_BY)))
    {
      const char *tooltip_text;

      if (GTK_IS_WIDGET (accessible))
        {
          tooltip_text = gtk_widget_get_tooltip_text (GTK_WIDGET (accessible));
          if (tooltip_text)
            accesskit_node_set_label (node, tooltip_text);
        }
      else
        tooltip_text = NULL;

      if (!tooltip_text && gtk_at_context_is_nested_button (ctx))
        {
          GtkAccessible *parent = gtk_accessible_get_accessible_parent (accessible);
          GtkATContext *parent_ctx = gtk_accessible_get_at_context (parent);

          gtk_at_context_realize (parent_ctx);
          accesskit_node_push_labelled_by (node, GTK_ACCESSKIT_CONTEXT (parent_ctx)->id);
          g_object_unref (parent_ctx);
          g_object_unref (parent);
        }
    }

  if (GTK_IS_LABEL (accessible))
    {
      GtkLabel *label = GTK_LABEL (accessible);
      PangoLayout *layout = gtk_label_get_layout (label);
      float x, y;

      gtk_label_get_layout_location (label, &x, &y);
      add_single_text_layout (self, update, node, layout, x, y);
    }
  else if (GTK_IS_INSCRIPTION (accessible))
    {
      GtkInscription *inscription = GTK_INSCRIPTION (accessible);
      PangoLayout *layout = gtk_inscription_get_layout (inscription);
      float x, y;

      gtk_inscription_get_layout_location (inscription, &x, &y);
      add_single_text_layout (self, update, node, layout, x, y);
    }
  else if (GTK_IS_TEXT (accessible))
    {
      GtkText *text = GTK_TEXT (accessible);
      PangoLayout *layout = gtk_text_get_layout (text);
      int x, y;

      gtk_text_get_layout_offsets (text, &x, &y);
      add_single_text_layout (self, update, node, layout, x, y);
    }
  else if (GTK_IS_TEXT_VIEW (accessible))
    {
      GtkTextView *text_view = GTK_TEXT_VIEW (accessible);
      GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
      GtkTextLayout *layout = gtk_text_view_get_layout (text_view);
      GtkTextBTree *btree = _gtk_text_buffer_get_btree (buffer);
      GtkTextIter current;

      if (!self->text_view_lines)
        {
          self->text_view_lines =
            g_hash_table_new_full (NULL, NULL, NULL, destroy_text_view_lines_value);
          self->text_view_lines_by_id = g_hash_table_new (NULL, NULL);
        }

      gtk_text_buffer_get_start_iter (buffer, &current);

      while (true)
        {
          GtkTextLine *line = _gtk_text_iter_get_text_line (&current);
          GtkAccessKitTextLayout *line_layout =
            g_hash_table_lookup (self->text_view_lines, line);
          GtkTextIter line_end = current;
          GtkTextLineDisplay *display;
          PangoLayout *pango_layout;
          char *end_delimiter;
          int inner_offset_x, inner_offset_y;
          int buffer_offset_y;
          int widget_offset_x, widget_offset_y;

          if (!line_layout)
            {
              line_layout = g_new0 (GtkAccessKitTextLayout, 1);
              line_layout->id = gtk_accesskit_root_new_id (self->root);
              g_hash_table_insert (self->text_view_lines, line, line_layout);
              g_hash_table_insert (self->text_view_lines_by_id,
                                   GUINT_TO_POINTER (line_layout->id), line);
            }

          if (!gtk_text_iter_ends_line (&line_end))
            gtk_text_iter_forward_to_line_end (&line_end);

          if (line_layout->children)
            {
              display = NULL;
              pango_layout = NULL;
              end_delimiter = NULL;
              inner_offset_x = inner_offset_y = 0;
            }
          else
            {
              display = gtk_text_layout_create_display (layout, line, FALSE);
              pango_layout = display->layout;

              if (gtk_text_iter_is_end (&line_end))
                end_delimiter = NULL;
              else
                {
                  GtkTextIter next_line = line_end;
                  gtk_text_iter_forward_line (&next_line);
                  end_delimiter =
                    gtk_text_buffer_get_text (buffer, &line_end, &next_line,
                                              TRUE);
                }

              inner_offset_x = display->x_offset;
              inner_offset_y = display->top_margin;
            }

          buffer_offset_y = _gtk_text_btree_find_line_top (btree, line, layout);
          gtk_text_view_buffer_to_window_coords (text_view,
                                                 GTK_TEXT_WINDOW_WIDGET,
                                                 0, buffer_offset_y,
                                                 &widget_offset_x,
                                                 &widget_offset_y);

          add_text_layout (line_layout, update, node, pango_layout,
                           end_delimiter, widget_offset_x, widget_offset_y,
                           inner_offset_x, inner_offset_y);

          g_clear_pointer (&display, gtk_text_line_display_unref);
          g_clear_pointer (&end_delimiter, g_free);

          if (gtk_text_iter_is_end (&line_end))
            break;
          gtk_text_iter_forward_line (&current);
        }
    }

  if (GTK_IS_TEXT_VIEW (accessible))
    {
      GtkTextView *text_view = GTK_TEXT_VIEW (accessible);
      GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
      GtkTextMark *anchor_mark = gtk_text_buffer_get_selection_bound (buffer);
      GtkTextMark *focus_mark = gtk_text_buffer_get_insert (buffer);
      accesskit_text_selection selection;

      text_view_mark_to_text_position (self, text_view, anchor_mark,
                                       &selection.anchor);
      text_view_mark_to_text_position (self, text_view, focus_mark,
                                       &selection.focus);
      accesskit_node_set_text_selection (node, selection);
      accesskit_node_add_action (node, ACCESSKIT_ACTION_SET_TEXT_SELECTION);
    }
  else if (GTK_IS_LABEL (accessible))
    {
      GtkLabel *label = GTK_LABEL (accessible);

      if (gtk_label_get_selectable (label))
        {
          PangoLayout *layout = gtk_label_get_layout (label);
          int anchor = _gtk_label_get_selection_bound (label);
          int focus = _gtk_label_get_cursor_position (label);
          accesskit_text_selection selection;

          usv_offset_to_text_position (&self->single_text_layout, layout,
                                       anchor, &selection.anchor);
          usv_offset_to_text_position (&self->single_text_layout, layout,
                                       focus, &selection.focus);
          accesskit_node_set_text_selection (node, selection);
          accesskit_node_add_action (node, ACCESSKIT_ACTION_SET_TEXT_SELECTION);
        }
    }
  else if (GTK_IS_EDITABLE (accessible) &&
           role != ACCESSKIT_ROLE_GENERIC_CONTAINER)
    {
      GtkText *text = gtk_editable_get_text_widget (GTK_EDITABLE (accessible));

      if (text)
        {
          GtkATContext *text_ctx = gtk_accessible_get_at_context (GTK_ACCESSIBLE (text));
          GtkAccessKitContext *text_accesskit_ctx = GTK_ACCESSKIT_CONTEXT (text_ctx);
          PangoLayout *layout = gtk_text_get_layout (text);
          int start, end;
          accesskit_text_selection selection;

          g_assert (gtk_at_context_is_realized (text_ctx));
          g_assert (text_accesskit_ctx->single_text_layout.children);

          gtk_editable_get_selection_bounds (GTK_EDITABLE (text), &start, &end);
          usv_offset_to_text_position (&text_accesskit_ctx->single_text_layout,
                                       layout, start, &selection.anchor);
          usv_offset_to_text_position (&text_accesskit_ctx->single_text_layout,
                                       layout, end, &selection.focus);
          accesskit_node_set_text_selection (node, selection);
          accesskit_node_add_action (node, ACCESSKIT_ACTION_SET_TEXT_SELECTION);

          g_object_unref (text_ctx);
        }
    }

  /* TODO: other text widget types */

  accesskit_tree_update_push_node (update, self->id, node);
}

void
gtk_accesskit_context_update_tree (GtkAccessKitContext *self)
{
  if (!gtk_at_context_is_realized (GTK_AT_CONTEXT (self)))
    return;

  gtk_accesskit_root_update_tree (self->root);
}

static gint
text_position_to_usv_offset (GtkAccessKitTextLayout        *layout,
                             PangoLayout                   *pango_layout,
                             const accesskit_text_position *pos)
{
  gint n_attrs;
  const PangoLogAttr *attrs =
    pango_layout_get_log_attrs_readonly (pango_layout, &n_attrs);
  guint offset;
  size_t char_index = 0;

  if ((pos->node >> 32) != layout->id)
    return -1;
  offset = pos->node & 0xffffffff;
  if (offset > n_attrs)
    return -1;

  while (char_index < pos->character_index)
    {
      if (offset == n_attrs)
        return -1;
      ++offset;
      if (offset == n_attrs || attrs[offset].is_cursor_position)
        ++char_index;
    }

  return offset;
}

static gboolean
text_position_to_text_view_iter (GtkAccessKitContext           *self,
                                 GtkTextView                   *text_view,
                                 const accesskit_text_position *pos,
                                 GtkTextIter                   *iter)
{
  GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
  GtkTextBTree *btree = _gtk_text_buffer_get_btree (buffer);
  GtkTextLayout *layout = gtk_text_view_get_layout (text_view);
  guint line_id;
  GtkTextLine *line;
  GtkAccessKitTextLayout *line_layout;
  GtkTextLineDisplay *display;
  gint usv_offset;

  if (pos->node == (pos->node & 0xffffffff))
    return FALSE;
  line_id = pos->node >> 32;

  line = g_hash_table_lookup (self->text_view_lines_by_id,
                              GUINT_TO_POINTER (line_id));
  if (!line)
    return FALSE;
  line_layout = g_hash_table_lookup (self->text_view_lines, line);
  g_assert (line_layout);
  if (!line_layout->children)
    return FALSE;
  display = gtk_text_layout_get_line_display (layout, line, FALSE);

  usv_offset = text_position_to_usv_offset (line_layout, display->layout, pos);
  if (usv_offset == -1)
    return FALSE;

  _gtk_text_btree_get_iter_at_line_ptr_char (btree, iter, line, usv_offset);
  return TRUE;
}

void
gtk_accesskit_context_do_action (GtkAccessKitContext            *self,
                                 const accesskit_action_request *request)
{
  GtkATContext *ctx = GTK_AT_CONTEXT (self);
  GtkAccessible *accessible = gtk_at_context_get_accessible (ctx);
  GtkWidget *widget;
  const accesskit_text_selection *selection;

  switch (request->action)
  {
  case ACCESSKIT_ACTION_CLICK:
    if (!GTK_IS_WIDGET (accessible))
      return;
    widget = GTK_WIDGET (accessible);

    if (!gtk_widget_is_sensitive (widget) || !gtk_widget_is_visible (widget))
      return;

    gtk_widget_activate (widget);
    break;

  case ACCESSKIT_ACTION_FOCUS:
    if (!GTK_IS_WIDGET (accessible))
      return;
    widget = GTK_WIDGET (accessible);

    if (!gtk_widget_is_sensitive (widget) || !gtk_widget_is_visible (widget))
      return;

    gtk_widget_grab_focus (widget);
    break;

  case ACCESSKIT_ACTION_SET_TEXT_SELECTION:
    if (!request->data.has_value ||
        request->data.value.tag != ACCESSKIT_ACTION_DATA_SET_TEXT_SELECTION)
      return;
    selection = &request->data.value.set_text_selection;

    if (GTK_IS_TEXT_VIEW (accessible))
      {
        GtkTextView *text_view = GTK_TEXT_VIEW (accessible);
        GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
        GtkTextIter anchor, focus;

        if (!text_position_to_text_view_iter (self, text_view,
                                              &selection->anchor, &anchor))
          return;
        if (!text_position_to_text_view_iter (self, text_view,
                                              &selection->focus, &focus))
          return;

        gtk_text_buffer_select_range (buffer, &focus, &anchor);
        gtk_text_view_scroll_to_iter (text_view, &focus, 0, FALSE, 0, 0);
      }
    else if (GTK_IS_LABEL (accessible))
      {
        GtkLabel *label = GTK_LABEL (accessible);
        PangoLayout *layout;
        gint anchor, focus;

        if (!gtk_label_get_selectable (label))
          return;

        layout = gtk_label_get_layout (label);

        anchor =
          text_position_to_usv_offset (&self->single_text_layout, layout,
                                       &selection->anchor);
        if (anchor == -1)
          return;
        focus =
          text_position_to_usv_offset (&self->single_text_layout, layout,
                                       &selection->focus);
        if (focus == -1)
          return;

        gtk_label_select_region (label, anchor, focus);
      }
    else if (GTK_IS_EDITABLE (accessible))
      {
        GtkEditable *editable = GTK_EDITABLE (accessible);
        GtkText *text = gtk_editable_get_text_widget (editable);
        GtkATContext *text_ctx;
        GtkAccessKitContext *text_accesskit_ctx;
        PangoLayout *layout;
        gint anchor, focus;

        if (!text)
          return;
        text_ctx = gtk_accessible_get_at_context (GTK_ACCESSIBLE (text));
        text_accesskit_ctx = GTK_ACCESSKIT_CONTEXT (text_ctx);
        if (!gtk_at_context_is_realized (text_ctx) ||
            !text_accesskit_ctx->single_text_layout.children)
          {
            g_object_unref (text_ctx);
            return;
          }
        layout = gtk_text_get_layout (text);

        anchor =
          text_position_to_usv_offset (&text_accesskit_ctx->single_text_layout,
                                       layout, &selection->anchor);
        if (anchor == -1)
          return;
        focus =
          text_position_to_usv_offset (&text_accesskit_ctx->single_text_layout,
                                       layout, &selection->focus);
        if (focus == -1)
          return;

        if (anchor == focus)
          gtk_editable_set_position (editable, focus);
        else if (anchor > focus)
          gtk_editable_select_region (editable, focus, anchor);
        else
          gtk_editable_select_region (editable, anchor, focus);

        g_object_unref (text_ctx);
      }
    /* TODO: other text widgets */

    break;

  /* TODO: other actions */

  default:
    break;
  }
}