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
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
|
/*
* tkUnixAccessibility.c --
*
* This file implements accessibility/screen-reader support
* on Unix-like systems based on the Gnome Accessibility Toolkit,
* the standard accessibility library for X11 systems.
*
* Copyright (c) 1995 Sun Microsystems, Inc.
* Copyright (c) 2006, Marcus von Appen
* Copyright (c) 2019-2025 Kevin Walzer
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <tcl.h>
#include <tk.h>
#include "tkInt.h"
#ifdef USE_ATK
#include <atk/atk.h>
#include <atk/atktext.h>
#include <atk/atkvalue.h>
#include <atk-bridge.h>
#include <dbus/dbus.h>
#include <glib.h>
/* Structs for custom ATK objects bound to Tk. */
typedef struct _TkAtkAccessible {
AtkObject parent;
Tk_Window tkwin;
Tcl_Interp *interp;
gint x, y, width, height;
char *path;
int is_focused;
int virtual_count;
} TkAtkAccessible;
typedef struct _TkAtkAccessibleClass {
AtkObjectClass parent_class;
} TkAtkAccessibleClass;
/* Structs to map Tk roles into ATK roles. */
typedef struct AtkRoleMap {
const char *tkrole;
AtkRole atkrole;
} AtkRoleMap;
struct AtkRoleMap roleMap[] = {
{"Button", ATK_ROLE_PUSH_BUTTON},
{"Checkbox", ATK_ROLE_CHECK_BOX},
{"Menuitem", ATK_ROLE_CHECK_MENU_ITEM},
{"Combobox", ATK_ROLE_COMBO_BOX},
{"Entry", ATK_ROLE_ENTRY},
{"Label", ATK_ROLE_LABEL},
{"Listbox", ATK_ROLE_LIST_BOX},
{"Menu", ATK_ROLE_MENU},
{"Menubar", ATK_ROLE_MENU_BAR},
{"Tree", ATK_ROLE_TREE},
{"Notebook", ATK_ROLE_PAGE_TAB},
{"Progressbar", ATK_ROLE_PROGRESS_BAR},
{"Radiobutton",ATK_ROLE_RADIO_BUTTON},
{"Scale", ATK_ROLE_SLIDER},
{"Spinbox", ATK_ROLE_SPIN_BUTTON},
{"Table", ATK_ROLE_TREE_TABLE},
{"Text", ATK_ROLE_TEXT},
{"Toplevel", ATK_ROLE_WINDOW},
{"Frame", ATK_ROLE_PANEL},
{"Canvas", ATK_ROLE_CANVAS},
{"Scrollbar", ATK_ROLE_SCROLL_BAR},
{"Menubar", ATK_ROLE_MENU_BAR},
{"Toggleswitch", ATK_ROLE_TOGGLE_BUTTON},
{NULL, 0}
};
#define ATK_CONTEXT g_main_context_default()
/* Variables for managing ATK objects. */
static AtkObject *tk_root_accessible = NULL;
static GList *toplevel_accessible_objects = NULL;
static GHashTable *tk_to_atk_map = NULL;
extern Tcl_HashTable *TkAccessibilityObject;
static GMainContext *acc_context = NULL;
static GHashTable *virtual_child_cache = NULL;
/* GLib-Tcl event loop integration. */
static void Atk_Event_Setup (void *clientData, int flags);
static void Atk_Event_Check(void *clientData, int flags);
static int Atk_Event_Run(Tcl_Event *event, int flags);
static void ignore_atk_critical(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data);
/* ATK component interface. */
static void tk_get_extents(AtkComponent *component, gint *x, gint *y, gint *width, gint *height, AtkCoordType coord_type);
static gboolean tk_contains(AtkComponent *component, gint x, gint y, AtkCoordType coord_type);
static void tk_atk_component_interface_init(AtkComponentIface *iface);
/* ATK child, attribute and state management. */
static gint tk_get_n_children(AtkObject *obj);
static AtkObject *tk_ref_child(AtkObject *obj, gint i);
static AtkObject *TkCreateVirtualChild(Tcl_Interp *interp, Tk_Window parent, int index, AtkRole role);
void InvalidateVirtualChildren(Tk_Window parent);
static char *make_virtual_child_key(const char *parent_path, int index);
static void cleanup_virtual_child_cache(void);
static AtkRole GetAtkRoleForWidget(Tk_Window win);
static AtkRole tk_get_role(AtkObject *obj);
static gchar *GetAtkNameForWidget(Tk_Window win);
static const gchar *tk_get_name(AtkObject *obj);
static void tk_set_name(AtkObject *obj, const gchar *name);
static gchar *GetAtkDescriptionForWidget(Tk_Window win);
static const gchar *tk_get_description(AtkObject *obj);
static AtkStateSet *tk_ref_state_set(AtkObject *obj);
/* ATK value interface. */
static gchar *GetAtkValueForWidget(Tk_Window win);
static void tk_get_value_and_text(AtkValue *obj, gdouble *value, gchar **text);
static AtkRange *tk_get_range(AtkValue *obj);
static void tk_get_current_value(AtkValue *obj, GValue *value);
static void tk_get_minimum_value(AtkValue *obj, GValue *value);
static void tk_get_maximum_value(AtkValue *obj, GValue *value);
static void tk_atk_value_interface_init(AtkValueIface *iface);
/* ATK action interface. */
static gboolean tk_action_do_action(AtkAction *action, gint i);
static gint tk_action_get_n_actions(AtkAction *action);
static const gchar *tk_action_get_name(AtkAction *action, gint i);
static void tk_atk_action_interface_init(AtkActionIface *iface);
/* ATK text interface. */
static gchar *tk_text_get_text(AtkText *text, gint start_offset, gint end_offset);
static gint tk_text_get_caret_offset(AtkText *text);
static gchar *tk_text_get_selection(AtkText *text,gint selection_num, gint *start_offset, gint *end_offset);
static inline gchar *tk_acc_value_dup(Tk_Window win);
static gint tk_text_get_character_count(AtkText *text);
static gchar *tk_text_get_text_at_offset(AtkText *text, gint offset, AtkTextBoundary boundary_type, gint *start_offset, gint *end_offset);
static gchar *tk_text_get_text_after_offset(AtkText *text, gint offset, AtkTextBoundary boundary_type, gint *start_offset, gint *end_offset);
static gchar *tk_text_get_text_before_offset(AtkText *text, gint offset, AtkTextBoundary boundary_type, gint *start_offset, gint *end_offset);
static AtkAttributeSet *tk_text_get_run_attributes(AtkText *text, gint offset, gint *start_offset, gint *end_offset);
static AtkAttributeSet *tk_text_get_default_attributes(AtkText *text);
static void tk_text_get_character_extents(AtkText *text, gint offset, gint *x, gint *y, gint *width, gint *height, AtkCoordType coords);
static void tk_text_get_range_extents(AtkText *text,gint start_offset, gint end_offset, AtkCoordType coords, AtkTextRectangle *rect);
static gint tk_text_get_offset_at_point(AtkText *text, gint x, gint y, AtkCoordType coords);
static gboolean tk_text_set_caret_offset(AtkText *text, gint offset);
static gboolean tk_text_set_selection(AtkText *text, gint selection_num, gint start_offset, gint end_offset);
static gint tk_text_get_n_selections(AtkText *text);
static void tk_atk_text_interface_init(AtkTextIface *iface);
/* ATK selection interface. */
static gboolean tk_selection_add_selection(AtkSelection *selection, gint i);
static gboolean tk_selection_remove_selection(AtkSelection *selection, gint i);
static gboolean tk_selection_clear_selection(AtkSelection *selection);
static gint tk_selection_get_selection_count(AtkSelection *selection);
static gboolean tk_selection_is_child_selected(AtkSelection *selection, gint i);
static AtkObject *tk_selection_ref_selection(AtkSelection *selection, gint i);
static gboolean tk_selection_select_all_selection(AtkSelection *selection);
static void tk_atk_selection_interface_init(AtkSelectionIface *iface);
static void TkAtkNotifySelectionChanged(Tk_Window tkwin);
/* Object lifecycle functions. */
static void tk_atk_accessible_class_init(TkAtkAccessibleClass *klass);
static void tk_atk_accessible_init(TkAtkAccessible *accessible);
static void tk_atk_accessible_finalize(GObject *gobject);
/* Registration and mapping functions. */
static void RegisterToplevelWindow(Tcl_Interp *interp, Tk_Window tkwin, AtkObject *accessible);
static void UnregisterToplevelWindow(AtkObject *accessible);
Tk_Window GetToplevelOfWidget(Tk_Window tkwin);
AtkObject *TkCreateAccessibleAtkObject(Tcl_Interp *interp, Tk_Window tkwin, const char *path);
void InitAtkTkMapping(void);
void RegisterAtkObjectForTkWindow(Tk_Window tkwin, AtkObject *atkobj);
AtkObject *GetAtkObjectForTkWindow(Tk_Window tkwin);
void UnregisterAtkObjectForTkWindow(Tk_Window tkwin);
static AtkObject *tk_util_get_root(void);
AtkObject *atk_get_root(void);
/* Event handlers. */
void TkAtkAccessible_RegisterEventHandlers(Tk_Window tkwin, void *tkAccessible);
static void TkAtkAccessible_DestroyHandler(void *clientData, XEvent *eventPtr);
static void TkAtkAccessible_FocusHandler(void *clientData, XEvent *eventPtr);
static void TkAtkAccessible_CreateHandler(void *clientData, XEvent *eventPtr);
static void TkAtkAccessible_ConfigureHandler(void *clientData, XEvent *eventPtr);
/* Tcl command implementations. */
static int EmitSelectionChanged(void *clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]);
static int EmitFocusChanged(void *clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]);
static int IsScreenReaderRunning(void *clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]);
static int IsScreenReaderActive(void);
int TkAtkAccessibleObjCmd(void *clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]);
int TkAtkAccessibility_Init(Tcl_Interp *interp);
/* Signal IDs for custom AT-SPI signals. */
static guint window_create_signal_id;
static guint window_activate_signal_id;
static guint window_deactivate_signal_id;
/* Define custom ATK object bridged to Tcl/Tk. */
#define TK_ATK_TYPE_ACCESSIBLE (tk_atk_accessible_get_type())
#define TK_ATK_IS_ACCESSIBLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), TK_ATK_TYPE_ACCESSIBLE))
G_DEFINE_TYPE_WITH_CODE(TkAtkAccessible, tk_atk_accessible, ATK_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE(ATK_TYPE_COMPONENT, tk_atk_component_interface_init)
G_IMPLEMENT_INTERFACE(ATK_TYPE_ACTION, tk_atk_action_interface_init)
G_IMPLEMENT_INTERFACE(ATK_TYPE_VALUE, tk_atk_value_interface_init)
G_IMPLEMENT_INTERFACE(ATK_TYPE_TEXT, tk_atk_text_interface_init)
G_IMPLEMENT_INTERFACE(ATK_TYPE_SELECTION, tk_atk_selection_interface_init)
)
/*
*----------------------------------------------------------------------
*
* GLib integration functions. These create a Tcl event source so that
* the GLib event loop can be smoothly integrated with Tcl/Tk.
*
*----------------------------------------------------------------------
*/
/* Configure event loop. */
static void Atk_Event_Setup(
TCL_UNUSED(void *), /* clientData */
int flags)
{
static Tcl_Time block_time = {0, 10000};
if (!(flags & TCL_WINDOW_EVENTS)) {
return;
}
if (g_main_context_pending(acc_context)) {
block_time.usec = 0;
}
Tcl_SetMaxBlockTime(&block_time);
}
/* Check event queue. */
static void Atk_Event_Check(
TCL_UNUSED(void *), /* clientData */
int flags)
{
if (!(flags & TCL_WINDOW_EVENTS)) {
return;
}
if (g_main_context_pending(acc_context)) {
Tcl_Event *event = (Tcl_Event *)ckalloc(sizeof(Tcl_Event));
event->proc = Atk_Event_Run;
Tcl_QueueEvent(event, TCL_QUEUE_TAIL);
}
}
/* Run the event. */
static int Atk_Event_Run(
TCL_UNUSED(Tcl_Event *), /* event */
int flags)
{
if (!(flags & TCL_WINDOW_EVENTS)) {
return 0;
}
while (g_main_context_pending(acc_context)) {
g_main_context_iteration(acc_context, FALSE);
}
return 1;
}
/* Disable GLib warnings that can pollute the console - dummy function. */
static void ignore_atk_critical(
TCL_UNUSED(const gchar *), /* log_domain */
TCL_UNUSED(GLogLevelFlags), /* log_level */
TCL_UNUSED(const gchar *), /* message */
TCL_UNUSED(gpointer)) /* user_data */
{
}
/*
*----------------------------------------------------------------------
*
* ATK interface functions. These do the heavy lifting of mapping Tk to ATK
* functionality. ATK has a more rigid structure than NSAccessibility and
* Microsoft Active Accessibility and requires a great deal more specific
* implementation for accessibility to work properly.
*
*----------------------------------------------------------------------
*/
/*
* ATK component interface. This tracks widget location/geometry.
*/
static void tk_get_extents(AtkComponent *component, gint *x, gint *y, gint *width, gint *height, AtkCoordType coord_type)
{
TkAtkAccessible *acc = (TkAtkAccessible *)component;
if (!acc || !acc->tkwin) {
*x = *y = *width = *height = 0;
return;
}
int wx, wy;
Tk_GetRootCoords(acc->tkwin, &wx, &wy);
int w = Tk_Width(acc->tkwin);
int h = Tk_Height(acc->tkwin);
if (coord_type == ATK_XY_SCREEN) {
/* Absolute screen coords. */
*x = wx;
*y = wy;
} else {
/* Relative to toplevel window. */
Tk_Window top = GetToplevelOfWidget(acc->tkwin);
int tx, ty;
Tk_GetRootCoords(top, &tx, &ty);
*x = wx - tx;
*y = wy - ty;
}
*width = w;
*height = h;
}
static gboolean tk_contains(AtkComponent *component, gint x, gint y, AtkCoordType coord_type)
{
gint comp_x, comp_y, comp_width, comp_height;
if (!component) return FALSE;
tk_get_extents(component, &comp_x, &comp_y, &comp_width, &comp_height, coord_type);
return (x >= comp_x && x < comp_x + comp_width &&
y >= comp_y && y < comp_y + comp_height);
}
static void tk_atk_component_interface_init(AtkComponentIface *iface)
{
iface->get_extents = tk_get_extents;
iface->contains = tk_contains;
}
/*
* Accessible children, attributes and state. Here we create accessible objects
* from "virtual" children (listbox rows, tree rows/cells, menu items), cache
* them, and assign them the appropriate ATK role for selection events and other
* interactions. We also create accessible objects from native Tk widgets (buttons,
* entries, etc.), map them them to the appropriate role, and track them.
*/
static char *make_virtual_child_key(const char *parent_path, int index)
{
gchar *key = g_strdup_printf("%s#%d", parent_path ? parent_path : "<null>", index);
return key;
}
static void cleanup_virtual_child_cache(void)
{
if (virtual_child_cache) {
g_hash_table_destroy(virtual_child_cache);
virtual_child_cache = NULL;
}
}
static AtkObject *TkCreateVirtualChild(Tcl_Interp *interp, Tk_Window parent, int index, AtkRole role)
{
if (!interp || !parent) return NULL;
const char *parent_path = Tk_PathName(parent);
char cmd[512];
const char *label = NULL;
char *label_copy = NULL;
/*
* Retrieve label text depending on role. In other areas we
* pull this data from the core TkAccessibleObject hash table
* via GetAtkValueForWidget, because selection events update
* the "value" field in that hash table with the string value
* from the selection event. However, in virtual widget creation,
* it makes sense to pull the data via direct inquiry of the
* virtual widget so there is no conflict. The hash table will
* automatically be updated by this selection event.
*/
switch (role) {
case ATK_ROLE_LIST_ITEM:
/* Listbox: use get index. */
snprintf(cmd, sizeof(cmd), "%s get %d", parent_path, index);
if (Tcl_Eval(interp, cmd) == TCL_OK) {
label = Tcl_GetString(Tcl_GetObjResult(interp));
}
break;
case ATK_ROLE_MENU_ITEM:
/* Menu: use entrycget -label. */
snprintf(cmd, sizeof(cmd), "%s entrycget %d -label", parent_path, index);
if (Tcl_Eval(interp, cmd) == TCL_OK) {
label = Tcl_GetString(Tcl_GetObjResult(interp));
}
break;
case ATK_ROLE_TREE_ITEM:
{
/* Treeview: map index → item ID → cget -values. */
snprintf(cmd, sizeof(cmd), "%s children {}", parent_path);
if (Tcl_Eval(interp, cmd) == TCL_OK) {
Tcl_Obj *list = Tcl_GetObjResult(interp);
Tcl_Size count;
Tcl_Obj **elems;
if (Tcl_ListObjGetElements(interp, list, &count, &elems) == TCL_OK && index < count) {
const char *itemid = Tcl_GetString(elems[index]);
snprintf(cmd, sizeof(cmd), "%s item [lindex %s 0] -values", parent_path, itemid);
if (Tcl_Eval(interp, cmd) == TCL_OK) {
label = Tcl_GetString(Tcl_GetObjResult(interp));
}
}
}
break;
}
default:
break;
}
if (!label || !*label) {
/* Fallback to generic name. */
char buf[64];
snprintf(buf, sizeof(buf), "Item %d", index);
label_copy = g_strdup(buf);
} else {
label_copy = g_strdup(label);
}
/* Create minimal virtual child. */
TkAtkAccessible *child_acc = g_object_new(TK_ATK_TYPE_ACCESSIBLE, NULL);
child_acc->interp = interp;
child_acc->tkwin = NULL; /* Virtual child - real widgets have a Tk_Window associated. */
child_acc->path = g_strdup_printf("%s#%d", parent_path, index);
child_acc->virtual_count = index;
AtkObject *child = ATK_OBJECT(child_acc);
atk_object_initialize(child, NULL);
atk_object_set_role(child, role);
atk_object_set_name(child, label_copy);
g_free(label_copy);
/* Store index for selection tracking. */
g_object_set_data(G_OBJECT(child), "tk-index", GINT_TO_POINTER(index));
/* Set parent relationship. */
AtkObject *accParent = GetAtkObjectForTkWindow(parent);
if (!accParent) {
Tk_Window toplevel = GetToplevelOfWidget(parent);
if (toplevel) {
accParent = GetAtkObjectForTkWindow(toplevel);
}
}
if (accParent) {
atk_object_set_parent(child, accParent);
}
return child;
}
void InvalidateVirtualChildren(Tk_Window parent)
{
if (!virtual_child_cache) return;
const char *parent_path = Tk_PathName(parent);
GList *to_remove = NULL;
GHashTableIter iter;
gpointer key, value;
g_hash_table_iter_init(&iter, virtual_child_cache);
while (g_hash_table_iter_next(&iter, &key, &value)) {
const char *cache_key = (const char *)key;
if (strstr(cache_key, parent_path) == cache_key) {
to_remove = g_list_prepend(to_remove, g_strdup(cache_key));
}
}
for (GList *l = to_remove; l != NULL; l = l->next) {
g_hash_table_remove(virtual_child_cache, l->data);
g_free(l->data);
}
g_list_free(to_remove);
}
static gint tk_get_n_children(AtkObject *obj)
{
if (obj == tk_root_accessible) {
return g_list_length(toplevel_accessible_objects);
}
TkAtkAccessible *acc = (TkAtkAccessible *)obj;
if (!acc || !acc->tkwin || !acc->interp) return 0;
int virtual_count = 0;
int native_count = 0;
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
/* Only query for virtual children if widget is mapped and ready. */
if (Tk_IsMapped(acc->tkwin) &&
(role == ATK_ROLE_LIST_BOX || role == ATK_ROLE_MENU || role == ATK_ROLE_MENU_BAR ||
role == ATK_ROLE_TREE || role == ATK_ROLE_TREE_TABLE)) {
const char *count_cmd = NULL;
switch (role) {
case ATK_ROLE_LIST_BOX:
count_cmd = "size";
break;
case ATK_ROLE_MENU:
case ATK_ROLE_MENU_BAR:
count_cmd = "index end";
break;
case ATK_ROLE_TREE:
case ATK_ROLE_TREE_TABLE:
/* Tree: get the top-level children list and count its length. */
char tcmd[256];
snprintf(tcmd, sizeof(tcmd), "%s children {}", Tk_PathName(acc->tkwin));
Tcl_Obj *savedResult = Tcl_GetObjResult(acc->interp);
Tcl_IncrRefCount(savedResult);
if (Tcl_Eval(acc->interp, tcmd) == TCL_OK) {
Tcl_Obj *list = Tcl_GetObjResult(acc->interp);
Tcl_Size count;
Tcl_Obj **elems;
if (Tcl_ListObjGetElements(acc->interp, list, &count, &elems) == TCL_OK) {
virtual_count = (int)count;
if (virtual_count < 0) virtual_count = 0;
}
}
Tcl_SetObjResult(acc->interp, savedResult);
Tcl_DecrRefCount(savedResult);
/* We handled the tree case here — don't use the generic integer-path below. */
break;
default:
break;
}
if (count_cmd) {
char cmd[256];
snprintf(cmd, sizeof(cmd), "%s %s", Tk_PathName(acc->tkwin), count_cmd);
/* Try to prevent crashes. */
Tcl_Obj *savedResult = Tcl_GetObjResult(acc->interp);
Tcl_IncrRefCount(savedResult);
if (Tcl_Eval(acc->interp, cmd) == TCL_OK) {
int count;
if (Tcl_GetIntFromObj(acc->interp, Tcl_GetObjResult(acc->interp), &count) == TCL_OK) {
virtual_count = count;
/* Handle menu special case. */
if ((role == ATK_ROLE_MENU || role == ATK_ROLE_MENU_BAR) && count >= 0) {
virtual_count = count + 1;
}
if (virtual_count < 0) virtual_count = 0;
}
}
/* Restore interpreter state. */
Tcl_SetObjResult(acc->interp, savedResult);
Tcl_DecrRefCount(savedResult);
}
}
/* Count native children. */
for (TkWindow *childPtr = ((TkWindow*)acc->tkwin)->childList;
childPtr != NULL; childPtr = childPtr->nextPtr) {
native_count++;
}
return virtual_count + native_count;
}
static AtkObject *tk_ref_child(AtkObject *obj, gint i)
{
if (obj == tk_root_accessible) {
if (i < 0 || i >= (gint)g_list_length(toplevel_accessible_objects)) return NULL;
GList *child = g_list_nth(toplevel_accessible_objects, i);
if (child) {
g_object_ref(child->data);
return ATK_OBJECT(child->data);
}
return NULL;
}
TkAtkAccessible *acc = (TkAtkAccessible *)obj;
if (!acc || !acc->tkwin || !acc->interp || i < 0) return NULL;
/* Only handle virtual children for mapped widgets to avoid startup issues. */
if (!Tk_IsMapped(acc->tkwin)) {
/* During startup, only handle native children. */
TkWindow *childPtr;
gint index = 0;
for (childPtr = ((TkWindow*)acc->tkwin)->childList; childPtr != NULL; childPtr = childPtr->nextPtr, index++) {
if (index == i) {
Tk_Window child_tkwin = (Tk_Window)childPtr;
AtkObject *child_obj = GetAtkObjectForTkWindow(child_tkwin);
if (!child_obj) {
child_obj = TkCreateAccessibleAtkObject(acc->interp, child_tkwin, Tk_PathName(child_tkwin));
if (child_obj) {
atk_object_set_parent(child_obj, obj);
/* Don't register event handlers during startup. */
}
}
if (child_obj) {
g_object_ref(child_obj);
return child_obj;
}
break;
}
}
return NULL;
}
AtkRole parentRole = GetAtkRoleForWidget(acc->tkwin);
int virtual_count = 0;
/* Handle virtual children. */
if ((parentRole == ATK_ROLE_LIST_BOX) || (parentRole == ATK_ROLE_MENU) ||
(parentRole == ATK_ROLE_MENU_BAR) || (parentRole == ATK_ROLE_TREE) ||
(parentRole == ATK_ROLE_TREE_TABLE)) {
/* Get virtual count safely. */
const char *count_cmd = NULL;
AtkRole childRole = ATK_ROLE_UNKNOWN;
switch (parentRole) {
case ATK_ROLE_LIST_BOX:
childRole = ATK_ROLE_LIST_ITEM;
count_cmd = "size";
break;
case ATK_ROLE_MENU:
case ATK_ROLE_MENU_BAR:
childRole = ATK_ROLE_MENU_ITEM;
count_cmd = "index end";
break;
case ATK_ROLE_TREE:
case ATK_ROLE_TREE_TABLE:
childRole = ATK_ROLE_TREE_ITEM;
/* Tree: get the top-level children list and count its length. */
char tcmd[256];
snprintf(tcmd, sizeof(tcmd), "%s children {}", Tk_PathName(acc->tkwin));
Tcl_Obj *savedResult = Tcl_GetObjResult(acc->interp);
Tcl_IncrRefCount(savedResult);
if (Tcl_Eval(acc->interp, tcmd) == TCL_OK) {
Tcl_Obj *list = Tcl_GetObjResult(acc->interp);
Tcl_Size count;
Tcl_Obj **elems;
if (Tcl_ListObjGetElements(acc->interp, list, &count, &elems) == TCL_OK) {
virtual_count = (int)count;
if (virtual_count < 0) virtual_count = 0;
}
}
Tcl_SetObjResult(acc->interp, savedResult);
Tcl_DecrRefCount(savedResult);
break;
default:
break;
}
if (count_cmd) {
char cmd[256];
snprintf(cmd, sizeof(cmd), "%s %s", Tk_PathName(acc->tkwin), count_cmd);
Tcl_Obj *savedResult = Tcl_GetObjResult(acc->interp);
Tcl_IncrRefCount(savedResult);
if (Tcl_Eval(acc->interp, cmd) == TCL_OK) {
int count;
if (Tcl_GetIntFromObj(acc->interp, Tcl_GetObjResult(acc->interp), &count) == TCL_OK) {
virtual_count = count;
if ((parentRole == ATK_ROLE_MENU || parentRole == ATK_ROLE_MENU_BAR) && count >= 0) {
virtual_count = count + 1;
}
if (virtual_count < 0) virtual_count = 0;
}
}
Tcl_SetObjResult(acc->interp, savedResult);
Tcl_DecrRefCount(savedResult);
}
/* Check if requested index is in virtual range. */
if (i < virtual_count && childRole != ATK_ROLE_UNKNOWN) {
/* Initialize cache if needed. */
if (!virtual_child_cache) {
virtual_child_cache = g_hash_table_new_full(
g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_object_unref);
}
/* Check cache first. */
const char *parent_path = Tk_PathName(acc->tkwin);
char *key = make_virtual_child_key(parent_path, i);
AtkObject *child = g_hash_table_lookup(virtual_child_cache, key);
if (child) {
g_object_ref(child);
g_free(key);
return child;
}
/* Create new virtual child. */
child = TkCreateVirtualChild(acc->interp, acc->tkwin, i, childRole);
if (child) {
/* Ensure parent relationship points to this accessible
* object (obj).
*/
atk_object_set_parent(child, obj);
if (acc->tkwin) { /* Only register handlers for real windows. */
TkAtkAccessible_RegisterEventHandlers(acc->tkwin, acc);
}
/* Insert into cache: store a referenced child.
* Use the allocated key as the hash key.
* g_hash_table_insert takes ownership of key and
* value pointers.
*/
g_hash_table_insert(virtual_child_cache, key, g_object_ref(child)); /* Cache holds one ref. */
/* Notify AT clients that a child was added at index i. */
g_signal_emit_by_name(obj, "children-changed::add", i, child);
/* Return a referenced child to the caller (callers expect a ref). */
g_object_ref(child);
return child;
}
/* If child creation failed, free the key we allocated. */
g_free(key);
}
}
/* Handle native children - adjust index for virtual children. */
gint native_index = i - virtual_count;
if (native_index >= 0) {
TkWindow *childPtr;
gint index = 0;
for (childPtr = ((TkWindow*)acc->tkwin)->childList; childPtr != NULL; childPtr = childPtr->nextPtr, index++) {
if (index == native_index) {
Tk_Window child_tkwin = (Tk_Window)childPtr;
AtkObject *child_obj = GetAtkObjectForTkWindow(child_tkwin);
if (!child_obj) {
child_obj = TkCreateAccessibleAtkObject(acc->interp, child_tkwin, Tk_PathName(child_tkwin));
if (child_obj) {
atk_object_set_parent(child_obj, obj);
TkAtkAccessible_RegisterEventHandlers(child_tkwin, (TkAtkAccessible *)child_obj);
}
}
if (child_obj) {
g_object_ref(child_obj);
return child_obj;
}
break;
}
}
}
return NULL;
}
static AtkRole GetAtkRoleForWidget(Tk_Window win)
{
if (!win) return ATK_ROLE_UNKNOWN;
Tcl_HashEntry *hPtr = Tcl_FindHashEntry(TkAccessibilityObject, (char *)win);
if (hPtr) {
Tcl_HashTable *attrs = (Tcl_HashTable *)Tcl_GetHashValue(hPtr);
if (attrs) {
Tcl_HashEntry *roleEntry = Tcl_FindHashEntry(attrs, "role");
if (roleEntry) {
const char *result = Tcl_GetString(Tcl_GetHashValue(roleEntry));
if (result) {
for (int i = 0; roleMap[i].tkrole != NULL; i++) {
if (strcmp(roleMap[i].tkrole, result) == 0) {
return roleMap[i].atkrole;
}
}
}
}
}
}
/* Fallback to widget class. */
const char *widgetClass = Tk_Class(win);
if (widgetClass) {
for (int i = 0; roleMap[i].tkrole != NULL; i++) {
if (strcasecmp(roleMap[i].tkrole, widgetClass) == 0) {
return roleMap[i].atkrole;
}
}
}
if (Tk_IsTopLevel(win)) {
return ATK_ROLE_WINDOW;
}
return ATK_ROLE_UNKNOWN;
}
static AtkRole tk_get_role(AtkObject *obj)
{
if (obj == tk_root_accessible) {
return ATK_ROLE_APPLICATION;
}
TkAtkAccessible *acc = (TkAtkAccessible *)obj;
if (!acc) return ATK_ROLE_UNKNOWN;
if (!acc->tkwin) {
/* Virtual child: return the role already stored in obj->role. */
return obj->role;
}
return GetAtkRoleForWidget(acc->tkwin);
}
static gchar *GetAtkNameForWidget(Tk_Window win)
{
if (!win) return NULL;
AtkRole role = GetAtkRoleForWidget(win);
/* If label, return the value instead of the name so Orca does not say "label" twice. */
if (role == ATK_ROLE_LABEL) {
return GetAtkValueForWidget(win);
}
Tcl_HashEntry *hPtr = Tcl_FindHashEntry(TkAccessibilityObject, (char *)win);
if (!hPtr) return NULL;
Tcl_HashTable *attrs = (Tcl_HashTable *)Tcl_GetHashValue(hPtr);
if (!attrs) return NULL;
Tcl_HashEntry *nameEntry = Tcl_FindHashEntry(attrs, "name");
if (!nameEntry) return NULL;
const char *name = Tcl_GetString(Tcl_GetHashValue(nameEntry));
return name ? g_utf8_make_valid(name, -1) : NULL;
}
static const gchar *tk_get_name(AtkObject *obj)
{
if (obj == tk_root_accessible) {
return "Tk Application";
}
TkAtkAccessible *acc = (TkAtkAccessible *)obj;
if (!acc) return NULL;
if (!acc->tkwin) {
/*
* Virtual child: Return the name set in GetAtkValueForWidget.
* This is written via selection events at the script levels.
* Callbacks into the Tcl interpreter to get the directly selected
* index in the Atk selection functions also retrieve this value.
* However, those calls are necessary for fine-grained index tracking in
* the accessible selection API. Here it's simpler to retrieve
* the string from the value field in the hash table.
*/
AtkObject *parent_obj = atk_object_get_parent(obj);
TkAtkAccessible *parent = (TkAtkAccessible*)parent_obj;
Tk_Window parentwin = parent->tkwin;
gchar *name = GetAtkValueForWidget(parentwin);
return name;
}
/* Real widget: Existing logic.*/
return GetAtkNameForWidget(acc->tkwin);
}
static void tk_set_name(AtkObject *obj, const gchar *name)
{
TkAtkAccessible *acc = (TkAtkAccessible *)obj;
if (!acc) return;
atk_object_set_name(obj, name);
}
static gchar *GetAtkDescriptionForWidget(Tk_Window win)
{
if (!win) return NULL;
Tcl_HashEntry *hPtr = Tcl_FindHashEntry(TkAccessibilityObject, (char *)win);
if (!hPtr) return NULL;
Tcl_HashTable *attrs = (Tcl_HashTable *)Tcl_GetHashValue(hPtr);
if (!attrs) return NULL;
Tcl_HashEntry *descriptionEntry = Tcl_FindHashEntry(attrs, "description");
if (!descriptionEntry) return NULL;
const char *description = Tcl_GetString(Tcl_GetHashValue(descriptionEntry));
return description ? g_utf8_make_valid(description, -1) : NULL;
}
static const gchar *tk_get_description(AtkObject *obj)
{
TkAtkAccessible *acc = (TkAtkAccessible *)obj;
return GetAtkDescriptionForWidget(acc->tkwin);
}
static AtkStateSet *tk_ref_state_set(AtkObject *obj)
{
if (!obj) return atk_state_set_new();
TkAtkAccessible *acc = (TkAtkAccessible *)obj;
AtkStateSet *set = atk_state_set_new();
/* Virtual child: tkwin == NULL. */
if (!acc->tkwin) {
atk_state_set_add_state(set, ATK_STATE_ENABLED);
atk_state_set_add_state(set, ATK_STATE_SENSITIVE);
atk_state_set_add_state(set, ATK_STATE_VISIBLE);
atk_state_set_add_state(set, ATK_STATE_SHOWING);
atk_state_set_add_state(set, ATK_STATE_FOCUSABLE);
/* Check selection and focus states. */
AtkObject *parent_obj = atk_object_get_parent(obj);
if (parent_obj && ATK_IS_SELECTION(parent_obj)) {
gint index = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(obj), "tk-index"));
/* Check if selected. */
if (tk_selection_is_child_selected(ATK_SELECTION(parent_obj), index)) {
atk_state_set_add_state(set, ATK_STATE_SELECTED);
}
/* Check if focused (active item). */
if (tk_selection_is_child_selected(ATK_SELECTION(parent_obj), index)) {
atk_state_set_add_state(set, ATK_STATE_FOCUSED);
}
}
return set;
}
/* Real widget. */
atk_state_set_add_state(set, ATK_STATE_ENABLED);
atk_state_set_add_state(set, ATK_STATE_SENSITIVE);
if (Tk_IsMapped(acc->tkwin)) {
atk_state_set_add_state(set, ATK_STATE_VISIBLE);
atk_state_set_add_state(set, ATK_STATE_SHOWING);
atk_state_set_add_state(set, ATK_STATE_FOCUSABLE);
}
if (acc->is_focused) {
atk_state_set_add_state(set, ATK_STATE_FOCUSED);
}
return set;
}
/*
* ATK value interface.
*/
static gchar *GetAtkValueForWidget(Tk_Window win)
{
if (!win) return NULL;
Tcl_HashEntry *hPtr = Tcl_FindHashEntry(TkAccessibilityObject, (char *)win);
if (!hPtr) return NULL;
Tcl_HashTable *attrs = (Tcl_HashTable *)Tcl_GetHashValue(hPtr);
if (!attrs) return NULL;
Tcl_HashEntry *valueEntry = Tcl_FindHashEntry(attrs, "value");
if (!valueEntry) return NULL;
const char *value = Tcl_GetString(Tcl_GetHashValue(valueEntry));
return value ? g_utf8_make_valid(value, -1) : NULL;
}
/* Modern AtkValue methods (replace deprecated stubs). */
static void tk_get_value_and_text(AtkValue *obj, gdouble *value, gchar **text)
{
TkAtkAccessible *acc = (TkAtkAccessible *)obj;
if (!acc || !acc->tkwin || !acc->interp) {
if (value) *value = 0.0;
if (text) *text = g_strdup("0.0");
return;
}
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
if (role != ATK_ROLE_SPIN_BUTTON) {
if (value) *value = 0.0;
if (text) *text = g_strdup("0");
return;
}
gchar *val = GetAtkValueForWidget(acc->tkwin);
double cur_val = val ? atof(val) : 0.0;
if (value) *value = cur_val;
if (text) *text = g_strdup(val ? val : "0");
}
static AtkRange *tk_get_range(AtkValue *obj)
{
TkAtkAccessible *acc = (TkAtkAccessible *)obj;
if (!acc || !acc->tkwin || !acc->interp) {
return NULL;
}
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
double min_val = 0.0, max_val = 0.0;
char cmd[256];
if (role == ATK_ROLE_SPIN_BUTTON || role == ATK_ROLE_SLIDER) {
/* Spinbox/Scale: -from .. -to. */
snprintf(cmd, sizeof(cmd), "%s cget -from", Tk_PathName(acc->tkwin));
if (Tcl_Eval(acc->interp, cmd) == TCL_OK) {
Tcl_GetDoubleFromObj(acc->interp, Tcl_GetObjResult(acc->interp), &min_val);
}
snprintf(cmd, sizeof(cmd), "%s cget -to", Tk_PathName(acc->tkwin));
if (Tcl_Eval(acc->interp, cmd) == TCL_OK) {
Tcl_GetDoubleFromObj(acc->interp, Tcl_GetObjResult(acc->interp), &max_val);
}
}
else if (role == ATK_ROLE_PROGRESS_BAR || role == ATK_ROLE_SCROLL_BAR) {
/* Progressbar/Scrollbar: 0 .. -maximum (default 100.) */
min_val = 0.0;
max_val = 100.0;
snprintf(cmd, sizeof(cmd), "%s cget -maximum", Tk_PathName(acc->tkwin));
if (Tcl_Eval(acc->interp, cmd) == TCL_OK) {
Tcl_GetDoubleFromObj(acc->interp, Tcl_GetObjResult(acc->interp), &max_val);
}
}
else {
return NULL; /* Not applicable. */
}
return atk_range_new(min_val, max_val, NULL);
}
/* Deprecated methods - updated to call modern ones (for compatibility). */
static void tk_get_current_value(AtkValue *obj, GValue *value)
{
gdouble val = 0.0;
gchar *text = NULL;
tk_get_value_and_text(obj, &val, &text);
g_value_init(value, G_TYPE_DOUBLE);
g_value_set_double(value, val);
g_free(text);
}
static void tk_get_minimum_value(AtkValue *obj, GValue *value)
{
AtkRange *range = tk_get_range(obj);
gdouble min_val = range ? atk_range_get_lower_limit(range) : 0.0;
if (range) g_object_unref(range);
g_value_init(value, G_TYPE_DOUBLE);
g_value_set_double(value, min_val);
}
static void tk_get_maximum_value(AtkValue *obj, GValue *value)
{
AtkRange *range = tk_get_range(obj);
gdouble max_val = range ? atk_range_get_upper_limit(range) : 0.0;
if (range) g_object_unref(range);
g_value_init(value, G_TYPE_DOUBLE);
g_value_set_double(value, max_val);
}
static void tk_atk_value_interface_init(AtkValueIface *iface)
{
iface->get_value_and_text = tk_get_value_and_text;
iface->get_range = tk_get_range;
iface->get_current_value = tk_get_current_value; /* Deprecated fallback. */
iface->get_minimum_value = tk_get_minimum_value; /* Deprecated fallback. */
iface->get_maximum_value = tk_get_maximum_value; /* Deprecated fallback. */
}
/*
* ATK action interface.
*/
static gboolean tk_action_do_action(AtkAction *action, gint i)
{
TkAtkAccessible *acc = (TkAtkAccessible *)action;
if (!acc || !acc->tkwin || !acc->interp) {
return FALSE;
}
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
AtkObject *obj = GetAtkObjectForTkWindow(acc->tkwin);
if (role == ATK_ROLE_SPIN_BUTTON) {
if (i < 0 || i > 1) return FALSE; /* Only support 0 and 1. */
/* Retrieve current value that was updated at script level. */
GValue gval = G_VALUE_INIT;
tk_get_current_value(ATK_VALUE(obj), &gval);
/* Notify ATK clients. */
g_signal_emit_by_name(obj, "value-changed");
g_object_notify(G_OBJECT(obj), "accessible-value");
g_value_unset(&gval);
return TRUE;
}
/* Handle toggle action for toggle buttons, checkboxes, and radio buttons */
if ((role == ATK_ROLE_TOGGLE_BUTTON || role == ATK_ROLE_CHECK_BOX ||
role == ATK_ROLE_RADIO_BUTTON || role == ATK_ROLE_TOGGLE_BUTTON) && i == 0) {
/* Toggle the state */
Tcl_HashEntry *hPtr = Tcl_FindHashEntry(TkAccessibilityObject, (char *)acc->tkwin);
if (!hPtr) return FALSE;
Tcl_HashTable *attrs = (Tcl_HashTable *)Tcl_GetHashValue(hPtr);
if (!attrs) return FALSE;
Tcl_HashEntry *valueEntry = Tcl_FindHashEntry(attrs, "value");
if (valueEntry) {
Tcl_Obj *valObj = Tcl_GetHashValue(valueEntry);
const char *currentVal = Tcl_GetString(valObj);
/* Toggle between "0" and "1" */
const char *newVal = (currentVal && strcmp(currentVal, "1") == 0) ? "0" : "1";
Tcl_SetStringObj(valObj, newVal, -1);
/* Notify value change */
g_signal_emit_by_name(obj, "value-changed");
g_object_notify(G_OBJECT(obj), "accessible-value");
}
}
/* Fallback: Generic "click" action for other roles. */
if (i == 0) {
/* Retrieve the command string. */
Tcl_HashEntry *hPtr = Tcl_FindHashEntry(TkAccessibilityObject, (char *)acc->tkwin);
if (!hPtr) return FALSE;
Tcl_HashTable *attrs = (Tcl_HashTable *)Tcl_GetHashValue(hPtr);
if (!attrs) return FALSE;
Tcl_HashEntry *actionEntry = Tcl_FindHashEntry(attrs, "action");
if (!actionEntry) return false;
const char *actionString = Tcl_GetString(Tcl_GetHashValue(actionEntry));
if (!actionString) return FALSE;
/* Finally, execute command. */
if (Tcl_EvalEx(acc->interp, actionString, -1, TCL_EVAL_GLOBAL) != TCL_OK) {
return FALSE;
}
}
return TRUE;
}
static gint tk_action_get_n_actions(AtkAction *action)
{
TkAtkAccessible *acc = (TkAtkAccessible *)action;
if (!acc || !acc->tkwin) return 0;
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
switch (role) {
case ATK_ROLE_PUSH_BUTTON:
case ATK_ROLE_MENU_ITEM:
case ATK_ROLE_TOGGLE_BUTTON:
return 1; /* Click/activate/toggle. */
case ATK_ROLE_SPIN_BUTTON:
return 2; /* Increment + decrement. */
case ATK_ROLE_CHECK_BOX:
case ATK_ROLE_RADIO_BUTTON:
return 1; /* Toggle/select. */
case ATK_ROLE_SLIDER:
case ATK_ROLE_SCROLL_BAR:
case ATK_ROLE_PROGRESS_BAR:
return 0; /* Value-only controls, no actions. */
default:
return 0;
}
}
static const gchar *tk_action_get_name(AtkAction *action, gint i)
{
TkAtkAccessible *acc = (TkAtkAccessible *)action;
if (!acc || !acc->tkwin) return NULL;
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
switch (role) {
case ATK_ROLE_PUSH_BUTTON:
case ATK_ROLE_MENU_ITEM:
if (i == 0) return "click";
break;
case ATK_ROLE_SPIN_BUTTON:
if (i == 0) return "increment";
if (i == 1) return "decrement";
break;
case ATK_ROLE_CHECK_BOX:
case ATK_ROLE_RADIO_BUTTON:
case ATK_ROLE_TOGGLE_BUTTON:
if (i == 0) return "toggle";
break;
default:
break;
}
return NULL;
}
static void tk_atk_action_interface_init(AtkActionIface *iface)
{
iface->do_action = tk_action_do_action;
iface->get_n_actions = tk_action_get_n_actions;
iface->get_name = tk_action_get_name;
}
/*
* ATK text interface.
*/
static gchar *tk_text_get_text(AtkText *text, gint start_offset, gint end_offset)
{
if (!TK_ATK_IS_ACCESSIBLE(text)) return NULL;
TkAtkAccessible *acc = (TkAtkAccessible *) (ATK_OBJECT(text));
if (!acc || !acc->tkwin || !acc->interp) return NULL;
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
if (role != ATK_ROLE_TEXT && role != ATK_ROLE_ENTRY) return NULL;
gchar *val = tk_acc_value_dup(acc->tkwin);
if (!val) return NULL;
/* Normalize offsets to character indices. */
const gint total = g_utf8_strlen(val, -1);
gint start = start_offset < 0 ? 0 : start_offset;
gint end = (end_offset < 0 || end_offset > total) ? total : end_offset;
if (end < start) { /* Avoid ATK assertions downstream. */
g_free(val);
return g_strdup("");
}
/* Convert char offsets to byte offsets for slicing. */
const gchar *start_p = g_utf8_offset_to_pointer(val, start);
const gchar *end_p = g_utf8_offset_to_pointer(val, end);
/* Return a newly allocated substring (ATK expects caller-owned memory). */
gchar *out = g_strndup(start_p, end_p - start_p);
g_free(val);
return out;
}
static gint tk_text_get_caret_offset(AtkText *text)
{
TkAtkAccessible *acc = (TkAtkAccessible *) ATK_OBJECT(text);
if (!acc || !acc->tkwin || !acc->interp) return 0;
Tcl_Obj *cmd[3];
cmd[0] = Tcl_NewStringObj(Tk_PathName(acc->tkwin), -1);
cmd[1] = Tcl_NewStringObj("index", -1);
cmd[2] = Tcl_NewStringObj("insert", -1);
Tcl_IncrRefCount(cmd[0]);
Tcl_IncrRefCount(cmd[1]);
Tcl_IncrRefCount(cmd[2]);
if (Tcl_EvalObjv(acc->interp, 3, cmd, TCL_EVAL_GLOBAL) != TCL_OK) {
Tcl_DecrRefCount(cmd[0]);
Tcl_DecrRefCount(cmd[1]);
Tcl_DecrRefCount(cmd[2]);
return 0;
}
const char *idx = Tcl_GetStringResult(acc->interp);
Tcl_DecrRefCount(cmd[0]);
Tcl_DecrRefCount(cmd[1]);
Tcl_DecrRefCount(cmd[2]);
/* "line.char" form, convert to offset. */
int line, col;
if (sscanf(idx, "%d.%d", &line, &col) == 2) {
Tcl_Obj *countCmd[5];
countCmd[0] = Tcl_NewStringObj(Tk_PathName(acc->tkwin), -1);
countCmd[1] = Tcl_NewStringObj("count", -1);
countCmd[2] = Tcl_NewStringObj("-chars", -1);
countCmd[3] = Tcl_NewStringObj("1.0", -1);
countCmd[4] = Tcl_NewStringObj("insert", -1);
for (int i=0;i<5;i++) Tcl_IncrRefCount(countCmd[i]);
int offset = 0;
if (Tcl_EvalObjv(acc->interp, 5, countCmd, TCL_EVAL_GLOBAL) == TCL_OK) {
Tcl_GetIntFromObj(acc->interp, Tcl_GetObjResult(acc->interp), &offset);
}
for (int i=0;i<5;i++) Tcl_DecrRefCount(countCmd[i]);
return offset;
}
return 0;
}
static gchar *tk_text_get_selection(
AtkText *text,
TCL_UNUSED(gint), /* selection_num */
gint *start_offset,
gint *end_offset)
{
if (!start_offset || !end_offset) return NULL;
*start_offset = 0;
*end_offset = 0;
if (!TK_ATK_IS_ACCESSIBLE(text)) return NULL;
TkAtkAccessible *acc = (TkAtkAccessible *)(ATK_OBJECT(text));
if (!acc || !acc->tkwin || !acc->interp) return NULL;
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
if (role != ATK_ROLE_TEXT && role != ATK_ROLE_ENTRY) return NULL;
gchar *val = tk_acc_value_dup(acc->tkwin);
if (!val) return NULL;
const gint total = g_utf8_strlen(val, -1);
if (total <= 0) {
g_free(val);
return NULL;
}
/* Return full selection for now. */
*start_offset = 0;
*end_offset = total;
return val; /* Caller will g_free(). */
}
static inline gchar *tk_acc_value_dup(Tk_Window win)
{
/*
* Text data is written to the "value" field of the TkAccessibleObject
* hash table and queried there.
*/
gchar *v = GetAtkValueForWidget(win);
return v;
}
static gint tk_text_get_character_count(AtkText *text)
{
if (!TK_ATK_IS_ACCESSIBLE(text)) return 0;
TkAtkAccessible *acc = (TkAtkAccessible *)(ATK_OBJECT(text));
if (!acc || !acc->tkwin || !acc->interp) return 0;
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
if (role != ATK_ROLE_TEXT && role != ATK_ROLE_ENTRY) return 0;
gchar *val = tk_acc_value_dup(acc->tkwin);
if (!val) return 0;
gint count = g_utf8_strlen(val, -1);
g_free(val);
return count;
}
static gchar *tk_text_get_text_at_offset(
AtkText *text,
TCL_UNUSED(gint), /* offset */
TCL_UNUSED(AtkTextBoundary), /* boundary_type */
gint *start_offset,
gint *end_offset)
{
if (!TK_ATK_IS_ACCESSIBLE(text)) return NULL;
TkAtkAccessible *acc = (TkAtkAccessible *)(ATK_OBJECT(text));
if (!acc || !acc->tkwin || !acc->interp) return NULL;
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
if (role != ATK_ROLE_TEXT && role != ATK_ROLE_ENTRY) return NULL;
/* For simplicity, return the entire text for any boundary type. */
gchar *full_text = tk_text_get_text(text, 0, -1);
if (start_offset) *start_offset = 0;
if (end_offset) *end_offset = g_utf8_strlen(full_text, -1);
return full_text;
}
static gchar *tk_text_get_text_after_offset(
AtkText *text,
gint offset,
TCL_UNUSED(AtkTextBoundary), /* boundary_type, */
gint *start_offset,
gint *end_offset)
{
if (!TK_ATK_IS_ACCESSIBLE(text)) return NULL;
TkAtkAccessible *acc = (TkAtkAccessible *)(ATK_OBJECT(text));
if (!acc || !acc->tkwin || !acc->interp) return NULL;
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
if (role != ATK_ROLE_TEXT && role != ATK_ROLE_ENTRY) return NULL;
gchar *full_text = tk_text_get_text(text, 0, -1);
gint length = g_utf8_strlen(full_text, -1);
if (start_offset) *start_offset = offset + 1;
if (end_offset) *end_offset = length;
g_free(full_text);
return tk_text_get_text(text, offset + 1, -1);
}
static gchar *tk_text_get_text_before_offset(
AtkText *text,
gint offset,
TCL_UNUSED(AtkTextBoundary), /* boundary_type */
gint *start_offset,
gint *end_offset)
{
if (!TK_ATK_IS_ACCESSIBLE(text)) return NULL;
TkAtkAccessible *acc = (TkAtkAccessible *)(ATK_OBJECT(text));
if (!acc || !acc->tkwin || !acc->interp) return NULL;
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
if (role != ATK_ROLE_TEXT && role != ATK_ROLE_ENTRY) return NULL;
if (start_offset) *start_offset = 0;
if (end_offset) *end_offset = offset;
return tk_text_get_text(text, 0, offset);
}
static AtkAttributeSet *tk_text_get_run_attributes(
AtkText *text,
TCL_UNUSED(gint), /* offset */
gint *start_offset,
gint *end_offset)
{
if (!TK_ATK_IS_ACCESSIBLE(text)) return NULL;
TkAtkAccessible *acc = (TkAtkAccessible *)(ATK_OBJECT(text));
if (!acc || !acc->tkwin || !acc->interp) return NULL;
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
if (role != ATK_ROLE_TEXT && role != ATK_ROLE_ENTRY) return NULL;
if (start_offset) *start_offset = 0;
if (end_offset) *end_offset = tk_text_get_character_count(text);
return NULL; /* No attributes for now. */
}
static AtkAttributeSet *tk_text_get_default_attributes(
TCL_UNUSED(AtkText *)) /* text */
{
return NULL; /* No default attributes. */
}
static void tk_text_get_character_extents(
AtkText *text,
gint offset,
gint *x,
gint *y,
gint *width,
gint *height,
TCL_UNUSED(AtkCoordType)) /* coords */
{
TkAtkAccessible *acc = (TkAtkAccessible *)(ATK_OBJECT(text));
if (!acc || !acc->tkwin || !acc->interp) return;
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
if (role != ATK_ROLE_TEXT && role != ATK_ROLE_ENTRY) return;
/* Approximate character extents based on widget size and text length. */
gint char_count = tk_text_get_character_count(text);
if (char_count == 0) return;
gint widget_width = Tk_Width(acc->tkwin);
gint char_width = widget_width / char_count;
if (x) *x = offset * char_width;
if (y) *y = 0;
if (width) *width = char_width;
if (height) *height = Tk_Height(acc->tkwin);
}
static void tk_text_get_range_extents(AtkText *text, gint start_offset, gint end_offset, AtkCoordType coords, AtkTextRectangle *rect)
{
if (!rect) return;
TkAtkAccessible *acc = (TkAtkAccessible *)(ATK_OBJECT(text));
if (!acc || !acc->tkwin || !acc->interp) {
rect->x = rect->y = rect->width = rect->height = 0;
return;
}
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
if (role != ATK_ROLE_TEXT && role != ATK_ROLE_ENTRY) {
rect->x = rect->y = rect->width = rect->height = 0;
return;
}
gint char_count = tk_text_get_character_count(text);
if (char_count <= 0) {
rect->x = rect->y = rect->width = rect->height = 0;
return;
}
/* Clamp offsets. */
if (start_offset < 0) start_offset = 0;
if (end_offset > char_count) end_offset = char_count;
/* Ensure ATK requirement: start < end. */
if (start_offset >= end_offset) {
if (start_offset > 0) {
start_offset--;
end_offset = start_offset + 1;
} else if (char_count > 0) {
start_offset = 0;
end_offset = 1;
} else {
rect->x = rect->y = rect->width = rect->height = 0;
return;
}
}
gint widget_width = Tk_Width(acc->tkwin);
gint widget_height = Tk_Height(acc->tkwin);
gint char_width = (widget_width / char_count);
if (char_width <= 0) char_width = 1;
gint range_width = (end_offset - start_offset) * char_width;
if (range_width <= 0) range_width = 1;
rect->x = start_offset * char_width;
rect->y = 0;
rect->width = range_width;
rect->height = widget_height;
if (coords == ATK_XY_SCREEN) {
int root_x, root_y;
Tk_GetRootCoords(acc->tkwin, &root_x, &root_y);
rect->x += root_x;
rect->y += root_y;
}
}
static gint tk_text_get_offset_at_point(
AtkText *text,
gint x,
TCL_UNUSED(gint), /* y */
TCL_UNUSED(AtkCoordType)) /* coords*/
{
TkAtkAccessible *acc = (TkAtkAccessible *)ATK_OBJECT(text);
if (!acc || !acc->tkwin) return 0;
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
if (role != ATK_ROLE_TEXT && role != ATK_ROLE_ENTRY) return 0;
gint char_count = tk_text_get_character_count(text);
if (char_count == 0) return 0;
gint widget_width = Tk_Width(acc->tkwin);
gint char_width = widget_width / char_count;
return x / char_width;
}
static gboolean tk_text_set_caret_offset(AtkText *text, gint offset)
{
TkAtkAccessible *acc = (TkAtkAccessible *) ATK_OBJECT(text);
if (!acc || !acc->tkwin || !acc->interp) return FALSE;
Tcl_Obj *cmd[3];
cmd[0] = Tcl_NewStringObj(Tk_PathName(acc->tkwin), -1);
cmd[1] = Tcl_NewStringObj("mark", -1);
char buf[64];
snprintf(buf, sizeof(buf), "1.0 + %d chars", offset);
cmd[2] = Tcl_NewStringObj(buf, -1);
Tcl_IncrRefCount(cmd[0]);
Tcl_IncrRefCount(cmd[1]);
Tcl_IncrRefCount(cmd[2]);
Tcl_Obj *args[4] = { cmd[0], cmd[1], Tcl_NewStringObj("set", -1), Tcl_NewStringObj("insert", -1) };
Tcl_IncrRefCount(args[2]);
Tcl_IncrRefCount(args[3]);
int ok = (Tcl_EvalObjv(acc->interp, 4, args, TCL_EVAL_GLOBAL) == TCL_OK);
for (int i=0;i<4;i++) Tcl_DecrRefCount(args[i]);
g_signal_emit_by_name(ATK_OBJECT(acc), "text-caret-moved", offset);
return ok ? TRUE : FALSE;
}
static gboolean tk_text_set_selection(AtkText *text, gint selection_num, gint start_offset, gint end_offset)
{
if (!TK_ATK_IS_ACCESSIBLE(text) || selection_num != 0)
return FALSE;
TkAtkAccessible *acc = (TkAtkAccessible *)ATK_OBJECT(text);
if (!acc || !acc->tkwin)
return FALSE;
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
if (role != ATK_ROLE_TEXT && role != ATK_ROLE_ENTRY)
return FALSE;
gchar *full_text = tk_acc_value_dup(acc->tkwin);
if (!full_text)
return FALSE;
gint char_count = g_utf8_strlen(full_text, -1);
if (start_offset < 0) start_offset = 0;
if (end_offset > char_count) end_offset = char_count;
if (start_offset > end_offset) {
gint tmp = start_offset;
start_offset = end_offset;
end_offset = tmp;
}
const gchar *start_p = g_utf8_offset_to_pointer(full_text, start_offset);
const gchar *end_p = g_utf8_offset_to_pointer(full_text, end_offset);
gchar *selected_text = g_strndup(start_p, end_p - start_p);
g_free(full_text);
/* Actual updating of text selection in the hash table is handled
* at the script level, so just free the text and fire signal.
*/
g_free(selected_text);
g_signal_emit_by_name(ATK_OBJECT(acc), "text-selection-changed");
return TRUE;
}
static gint tk_text_get_n_selections(
TCL_UNUSED(AtkText *)) /* text */
{
return 1; /* One selection supported. */
}
static void tk_atk_text_interface_init(AtkTextIface *iface)
{
iface->get_text = tk_text_get_text;
iface->get_caret_offset = tk_text_get_caret_offset;
iface -> get_selection = tk_text_get_selection;
iface->get_character_count = tk_text_get_character_count;
iface->get_text_at_offset = tk_text_get_text_at_offset;
iface->get_text_after_offset = tk_text_get_text_after_offset;
iface->get_text_before_offset = tk_text_get_text_before_offset;
iface->get_run_attributes = tk_text_get_run_attributes;
iface->get_default_attributes = tk_text_get_default_attributes;
iface->get_character_extents = tk_text_get_character_extents;
iface->get_offset_at_point = tk_text_get_offset_at_point;
iface->set_caret_offset = tk_text_set_caret_offset;
iface->set_selection = tk_text_set_selection;
iface->get_n_selections = tk_text_get_n_selections;
iface->get_range_extents = tk_text_get_range_extents;
iface->get_bounded_ranges = NULL;
}
/*
* ATK select interface.
*/
static gboolean tk_selection_add_selection(AtkSelection *selection, gint i)
{
TkAtkAccessible *acc = (TkAtkAccessible *)ATK_OBJECT(selection);
if (!acc || !acc->tkwin) return FALSE;
AtkRole parentRole = GetAtkRoleForWidget(acc->tkwin);
const char *cmd_name = NULL;
switch (parentRole) {
case ATK_ROLE_LIST:
case ATK_ROLE_LIST_BOX:
case ATK_ROLE_TABLE:
case ATK_ROLE_TREE:
case ATK_ROLE_TREE_TABLE:
cmd_name = "selection set";
break;
case ATK_ROLE_MENU:
case ATK_ROLE_MENU_BAR:
cmd_name = "activate";
break;
default:
return FALSE;
}
char cmd[256];
snprintf(cmd, sizeof(cmd), "%s %s %d", Tk_PathName(acc->tkwin), cmd_name, i);
/* Fire selection event. */
if (Tcl_Eval(acc->interp, cmd) == TCL_OK) {
TkAtkNotifySelectionChanged(acc->tkwin);
return TRUE;
}
return FALSE;
}
static gboolean tk_selection_remove_selection(AtkSelection *selection, gint i)
{
TkAtkAccessible *acc = (TkAtkAccessible *)ATK_OBJECT(selection);
if (!acc || !acc->tkwin) return FALSE;
char cmd[256];
snprintf(cmd, sizeof(cmd), "%s selection clear %d", Tk_PathName(acc->tkwin), i);
/* Fire selection event. */
if (Tcl_Eval(acc->interp, cmd) == TCL_OK) {
TkAtkNotifySelectionChanged(acc->tkwin);
return TRUE;
}
return FALSE;
}
static gboolean tk_selection_clear_selection(AtkSelection *selection)
{
TkAtkAccessible *acc = (TkAtkAccessible *)ATK_OBJECT(selection);
if (!acc || !acc->tkwin) return FALSE;
char cmd[256];
snprintf(cmd, sizeof(cmd), "%s selection clear all", Tk_PathName(acc->tkwin));
/* Fire selection event. */
if (Tcl_Eval(acc->interp, cmd) == TCL_OK) {
TkAtkNotifySelectionChanged(acc->tkwin);
return TRUE;
}
return FALSE;
}
static gboolean tk_selection_select_all_selection(AtkSelection *selection)
{
TkAtkAccessible *acc = (TkAtkAccessible *)ATK_OBJECT(selection);
if (!acc || !acc->tkwin) return FALSE;
char cmd[256];
snprintf(cmd, sizeof(cmd), "%s selection set all", Tk_PathName(acc->tkwin));
/* Fire selection event. */
if (Tcl_Eval(acc->interp, cmd) == TCL_OK) {
TkAtkNotifySelectionChanged(acc->tkwin);
return TRUE;
}
return FALSE;
}
static gint tk_selection_get_selection_count(AtkSelection *selection)
{
TkAtkAccessible *acc = (TkAtkAccessible *)ATK_OBJECT(selection);
if (!acc || !acc->tkwin) return 0;
AtkRole parentRole = GetAtkRoleForWidget(acc->tkwin);
const char *selection_cmd = NULL;
switch (parentRole) {
case ATK_ROLE_LIST:
case ATK_ROLE_LIST_BOX:
selection_cmd = "curselection";
break;
case ATK_ROLE_MENU:
case ATK_ROLE_MENU_BAR:
selection_cmd = "index active";
break;
case ATK_ROLE_TREE:
case ATK_ROLE_TREE_TABLE:
selection_cmd = "selection";
break;
case ATK_ROLE_TABLE:
selection_cmd = "curselection";
break;
default:
return 0;
}
char cmd[256];
snprintf(cmd, sizeof(cmd), "%s %s", Tk_PathName(acc->tkwin), selection_cmd);
if (Tcl_Eval(acc->interp, cmd) != TCL_OK) return 0;
Tcl_Obj *result = Tcl_GetObjResult(acc->interp);
Tcl_Size list_size = 0;
Tcl_Obj **objv = NULL;
if (Tcl_ListObjGetElements(acc->interp, result, &list_size, &objv) == TCL_OK) {
return list_size;
} else {
/* Single selection case. */
int index;
if (Tcl_GetIntFromObj(acc->interp, result, &index) == TCL_OK && index >= 0) {
return 1;
}
}
return 0;
}
static gboolean tk_selection_is_child_selected(AtkSelection *selection, gint i)
{
TkAtkAccessible *acc = (TkAtkAccessible *)ATK_OBJECT(selection);
if (!acc) return FALSE;
/* Virtual child. */
if (!acc->tkwin) {
AtkObject *parent = atk_object_get_parent(ATK_OBJECT(selection));
if (parent && ATK_IS_SELECTION(parent)) {
gpointer index_ptr = g_object_get_data(G_OBJECT(selection), "tk-index");
gint my_index = GPOINTER_TO_INT(index_ptr);
return tk_selection_is_child_selected(ATK_SELECTION(parent), my_index);
}
return FALSE;
}
Tcl_Interp *interp = acc->interp;
if (!interp) return FALSE;
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
char cmd[256];
if (role == ATK_ROLE_LIST || role == ATK_ROLE_LIST_BOX) {
snprintf(cmd, sizeof(cmd), "%s selection includes %d", Tk_PathName(acc->tkwin), i);
if (Tcl_Eval(interp, cmd) == TCL_OK) {
int result;
if (Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &result) == TCL_OK) {
return result ? TRUE : FALSE;
}
}
} else if (role == ATK_ROLE_TREE || role == ATK_ROLE_TREE_TABLE || role == ATK_ROLE_TABLE) {
/* Treeview: need to compare against $tree selection. */
snprintf(cmd, sizeof(cmd), "%s selection", Tk_PathName(acc->tkwin));
if (Tcl_Eval(interp, cmd) == TCL_OK) {
Tcl_Obj *list = Tcl_GetObjResult(interp);
Tcl_Size count;
Tcl_Obj **elems;
if (Tcl_ListObjGetElements(interp, list, &count, &elems) == TCL_OK) {
for (Tcl_Size j = 0; j < count; j++) {
const char *itemid = Tcl_GetString(elems[j]);
int sel_idx = -1;
snprintf(cmd, sizeof(cmd), "%s index %s", Tk_PathName(acc->tkwin), itemid);
if (Tcl_Eval(interp, cmd) == TCL_OK &&
Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &sel_idx) == TCL_OK) {
if (sel_idx == i) return TRUE;
}
}
}
}
} else if (role == ATK_ROLE_MENU || role == ATK_ROLE_MENU_BAR) {
/* Only active index counts. */
int active = -1;
snprintf(cmd, sizeof(cmd), "%s index active", Tk_PathName(acc->tkwin));
if (Tcl_Eval(interp, cmd) == TCL_OK &&
Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &active) == TCL_OK) {
return active == i;
}
}
return FALSE;
}
static AtkObject *tk_selection_ref_selection(AtkSelection *selection, gint i)
{
TkAtkAccessible *acc = (TkAtkAccessible *)ATK_OBJECT(selection);
if (!acc || !acc->tkwin) return NULL;
int sel_index = -1;
Tcl_Interp *interp = acc->interp;
AtkObject *obj = ATK_OBJECT(acc);
char cmd[256];
snprintf(cmd, sizeof(cmd), "%s curselection", Tk_PathName(acc->tkwin));
if (Tcl_Eval(interp, cmd) != TCL_OK) return NULL;
Tcl_Obj *result = Tcl_GetObjResult(interp);
Tcl_Size list_len = 0;
Tcl_Obj **elems = NULL;
AtkRole role = tk_get_role(obj);
/* Listbox or table: numeric indices. */
if (Tcl_ListObjGetElements(interp, result, &list_len, &elems) == TCL_OK) {
if (i < 0 || i >= (gint)list_len) return NULL;
if (role == ATK_ROLE_LIST || role == ATK_ROLE_LIST_BOX || role == ATK_ROLE_TABLE) {
if (Tcl_GetIntFromObj(interp, elems[i], &sel_index) != TCL_OK) return NULL;
} else if (role == ATK_ROLE_TREE || role == ATK_ROLE_TREE_TABLE) {
/* Treeview: get index from item ID. */
const char *itemid = Tcl_GetString(elems[i]);
char idxcmd[512];
snprintf(idxcmd, sizeof(idxcmd), "%s index %s", Tk_PathName(acc->tkwin), itemid);
if (Tcl_Eval(interp, idxcmd) != TCL_OK) return NULL;
if (Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &sel_index) != TCL_OK) return NULL;
} else {
/* Menu: numeric index. */
if (Tcl_GetIntFromObj(interp, elems[i], &sel_index) != TCL_OK) return NULL;
}
} else {
/* Single selection. */
if (Tcl_GetIntFromObj(interp, result, &sel_index) != TCL_OK) return NULL;
}
if (sel_index < 0) return NULL;
/* Set textual value as child name for ATK. */
AtkObject *child = tk_ref_child(obj, sel_index);
if (child) {
gchar *text = GetAtkValueForWidget(acc->tkwin);
if (text) {
atk_object_set_name(child, text);
g_free(text);
}
}
return child;
}
static void tk_atk_selection_interface_init(AtkSelectionIface *iface)
{
iface->add_selection = tk_selection_add_selection;
iface->clear_selection = tk_selection_clear_selection;
iface->get_selection_count = tk_selection_get_selection_count;
iface->is_child_selected = tk_selection_is_child_selected;
iface->ref_selection = tk_selection_ref_selection;
iface->remove_selection = tk_selection_remove_selection;
iface->select_all_selection = tk_selection_select_all_selection;
}
/*
* Notify ATK clients (Orca) about a selection change.
* Updates child names to their textual values and emits selection signals.
*/
void TkAtkNotifySelectionChanged(Tk_Window tkwin)
{
if (!tkwin) return;
AtkObject *obj = GetAtkObjectForTkWindow(tkwin);
if (!obj) return;
AtkRole role = tk_get_role(obj);
if (role != ATK_ROLE_LIST && role != ATK_ROLE_LIST_BOX &&
role != ATK_ROLE_TABLE && role != ATK_ROLE_TREE &&
role != ATK_ROLE_TREE_TABLE && role != ATK_ROLE_MENU &&
role != ATK_ROLE_MENU_BAR) {
return;
}
Tcl_Interp *interp = Tk_Interp(tkwin);
if (!interp) return;
int n_children = tk_get_n_children(obj);
if (n_children <= 0) return;
Tcl_Obj *selection_list = NULL;
Tcl_Obj **elems = NULL;
Tcl_Size selection_count = 0;
char cmd[512] = {0};
/* Get selection list. */
switch (role) {
case ATK_ROLE_LIST:
case ATK_ROLE_LIST_BOX:
case ATK_ROLE_TABLE:
snprintf(cmd, sizeof(cmd) - 1, "%s curselection", Tk_PathName(tkwin));
break;
case ATK_ROLE_MENU:
case ATK_ROLE_MENU_BAR:
case ATK_ROLE_TREE:
case ATK_ROLE_TREE_TABLE:
snprintf(cmd, sizeof(cmd) - 1, "%s selection", Tk_PathName(tkwin));
break;
default:
return;
}
if (Tcl_Eval(interp, cmd) == TCL_OK) {
selection_list = Tcl_GetObjResult(interp);
if (selection_list &&
Tcl_ListObjGetElements(interp, selection_list,
&selection_count, &elems) != TCL_OK) {
selection_count = 0;
elems = NULL;
}
}
/* Find active item. */
int active_index = -1;
const char *active_id = NULL;
if (role == ATK_ROLE_LIST || role == ATK_ROLE_LIST_BOX ||
role == ATK_ROLE_TABLE || role == ATK_ROLE_MENU ||
role == ATK_ROLE_MENU_BAR) {
snprintf(cmd, sizeof(cmd) - 1, "%s index active", Tk_PathName(tkwin));
if (Tcl_Eval(interp, cmd) == TCL_OK) {
Tcl_Obj *result = Tcl_GetObjResult(interp);
if (!result || Tcl_GetIntFromObj(interp, result, &active_index) != TCL_OK) {
active_index = -1;
}
}
} else if (role == ATK_ROLE_TREE || role == ATK_ROLE_TREE_TABLE) {
snprintf(cmd, sizeof(cmd) - 1, "%s focus", Tk_PathName(tkwin));
if (Tcl_Eval(interp, cmd) == TCL_OK) {
Tcl_Obj *result = Tcl_GetObjResult(interp);
if (result) {
active_id = Tcl_GetString(result);
if (active_id && *active_id) {
snprintf(cmd, sizeof(cmd) - 1,
"%s index {%s}", Tk_PathName(tkwin), active_id);
if (Tcl_Eval(interp, cmd) == TCL_OK) {
Tcl_Obj *idxObj = Tcl_GetObjResult(interp);
if (idxObj &&
Tcl_GetIntFromObj(interp, idxObj, &active_index) != TCL_OK) {
active_index = -1;
}
}
}
}
}
}
/* Iterate over children. */
for (int i = 0; i < n_children; i++) {
AtkObject *child = tk_ref_child(obj, i);
if (!child) continue;
gboolean is_selected = FALSE;
if (elems && selection_count > 0) {
if (role == ATK_ROLE_LIST || role == ATK_ROLE_LIST_BOX ||
role == ATK_ROLE_TABLE || role == ATK_ROLE_MENU ||
role == ATK_ROLE_MENU_BAR) {
for (Tcl_Size j = 0; j < selection_count; j++) {
if (elems[j] == NULL) {
continue; /* Skip NULL elements. */
}
int sel_idx = -1;
if (Tcl_GetIntFromObj(interp, elems[j], &sel_idx) == TCL_OK &&
sel_idx == i) {
is_selected = TRUE;
break;
}
}
} else if (role == ATK_ROLE_TREE || role == ATK_ROLE_TREE_TABLE) {
for (Tcl_Size j = 0; j < selection_count; j++) {
if (elems[j] == NULL) {
continue; /* Skip NULL elements. */
}
const char *itemid = Tcl_GetString(elems[j]);
if (itemid && *itemid) {
snprintf(cmd, sizeof(cmd) - 1,
"%s index {%s}", Tk_PathName(tkwin), itemid);
if (Tcl_Eval(interp, cmd) == TCL_OK) {
Tcl_Obj *result = Tcl_GetObjResult(interp);
int sel_idx = -1;
if (result &&
Tcl_GetIntFromObj(interp, result, &sel_idx) == TCL_OK &&
sel_idx == i) {
is_selected = TRUE;
break;
}
}
}
}
}
}
atk_object_notify_state_change(child, ATK_STATE_SELECTED, is_selected);
if (is_selected) {
g_signal_emit_by_name(child, "selection-changed");
}
/* Focus/active state. */
if (i == active_index) {
atk_object_notify_state_change(child, ATK_STATE_FOCUSED, TRUE);
g_signal_emit_by_name(child, "focus-event", TRUE);
g_signal_emit_by_name(obj, "active-descendant-changed", child);
} else {
atk_object_notify_state_change(child, ATK_STATE_FOCUSED, FALSE);
}
gchar *text = GetAtkValueForWidget(tkwin);
if (text) {
atk_object_set_name(child, text);
g_free(text);
}
g_object_unref(child);
}
g_signal_emit_by_name(obj, "selection-changed");
}
/*
* Functions to initialize and manage the parent ATK class and object instances.
*/
static void tk_atk_accessible_init(TkAtkAccessible *self)
{
self->tkwin = NULL;
self->interp = NULL;
self->path = NULL;
}
static void tk_atk_accessible_finalize(GObject *gobject)
{
TkAtkAccessible *self = (TkAtkAccessible*)gobject;
if (!self) return;
if (self->tkwin) {
/* Clean up any virtual children in cache for this window. */
if (virtual_child_cache) {
const char *parent_path = Tk_PathName(self->tkwin);
GHashTableIter iter;
gpointer key, value;
GList *keys_to_remove = NULL;
g_hash_table_iter_init(&iter, virtual_child_cache);
while (g_hash_table_iter_next(&iter, &key, &value)) {
const char *cache_key = (const char *)key;
if (strstr(cache_key, parent_path) == cache_key) {
keys_to_remove = g_list_prepend(keys_to_remove, g_strdup(cache_key));
}
}
for (GList *l = keys_to_remove; l != NULL; l = l->next) {
g_hash_table_remove(virtual_child_cache, l->data);
g_free(l->data);
}
g_list_free(keys_to_remove);
}
InvalidateVirtualChildren(self->tkwin);
cleanup_virtual_child_cache();
/* Unregister from tracking structures. */
UnregisterAtkObjectForTkWindow(self->tkwin);
if (Tk_IsTopLevel(self->tkwin)) {
UnregisterToplevelWindow(ATK_OBJECT(self));
}
self->tkwin = NULL;
}
g_free(self->path);
/* Chain up to parent finalizer. */
G_OBJECT_CLASS(tk_atk_accessible_parent_class)->finalize(gobject);
}
static void tk_atk_accessible_class_init(TkAtkAccessibleClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
AtkObjectClass *atk_class = ATK_OBJECT_CLASS(klass);
/* Register custom AT-SPI signals. */
window_create_signal_id = g_signal_new("window-create",
TK_ATK_TYPE_ACCESSIBLE,
G_SIGNAL_RUN_LAST,
0,
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
window_activate_signal_id = g_signal_new("window-activate",
TK_ATK_TYPE_ACCESSIBLE,
G_SIGNAL_RUN_LAST,
0,
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
window_deactivate_signal_id = g_signal_new("window-deactivate",
TK_ATK_TYPE_ACCESSIBLE,
G_SIGNAL_RUN_LAST,
0,
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
gobject_class->finalize = tk_atk_accessible_finalize;
/* Map ATK class functions to Tk functions. */
atk_class->get_name = tk_get_name;
atk_class->get_description = tk_get_description;
atk_class->get_role = tk_get_role;
atk_class->ref_state_set = tk_ref_state_set;
atk_class->get_n_children = tk_get_n_children;
atk_class->ref_child = tk_ref_child;
}
/*
*----------------------------------------------------------------------
*
* Registration and mapping functions. These functions set, track and update
* the association between Tk windows and ATK objects.
*
*----------------------------------------------------------------------
*/
/* Function to complete toplevel registration with proper hierarchy. */
static void RegisterToplevelWindow(Tcl_Interp *interp, Tk_Window tkwin, AtkObject *accessible)
{
if (!accessible || !tkwin || !G_IS_OBJECT(accessible)) {
g_warning("RegisterToplevelWindow: Invalid tkwin or accessible");
return;
}
if (!tk_root_accessible) {
tk_root_accessible = tk_util_get_root();
if (tk_root_accessible) {
tk_set_name(tk_root_accessible, "Tk Application");
}
}
AtkObject *existing = GetAtkObjectForTkWindow(tkwin);
if (existing && existing != accessible) {
g_warning("RegisterToplevelWindow: Toplevel %s already registered with different AtkObject",
Tk_PathName(tkwin));
return;
}
g_object_ref(accessible);
/* Ensure proper parent hierarchy for all windows. */
AtkObject *parentAcc = NULL;
if (Tk_IsTopLevel(tkwin)) {
/* True toplevels get root as parent. */
parentAcc = tk_root_accessible;
/* Add to toplevel list only for real toplevels. */
if (!g_list_find(toplevel_accessible_objects, accessible)) {
toplevel_accessible_objects = g_list_append(toplevel_accessible_objects, accessible);
gint index = g_list_index(toplevel_accessible_objects, accessible);
if (index >= 0 && tk_root_accessible) {
g_signal_emit_by_name(tk_root_accessible, "children-changed::add", index, accessible);
}
}
} else {
/* Non-toplevels: find and ensure parent is registered first. */
Tk_Window parentWin = Tk_Parent(tkwin);
if (parentWin) {
/* Ensure parent is registered before setting hierarchy. */
parentAcc = GetAtkObjectForTkWindow(parentWin);
if (!parentAcc) {
/* Recursively create parent accessibility object. */
parentAcc = TkCreateAccessibleAtkObject(interp, parentWin, Tk_PathName(parentWin));
if (parentAcc && Tk_IsTopLevel(parentWin)) {
/* If parent is toplevel, register it properly. */
RegisterToplevelWindow(interp, parentWin, parentAcc);
}
}
}
if (!parentAcc) {
/* Last resort fallback. */
parentAcc = tk_root_accessible;
}
/* Emit child-added signal on the actual parent. */
if (parentAcc && parentAcc != tk_root_accessible) {
/* Get current child count before adding. */
gint child_count = atk_object_get_n_accessible_children(parentAcc);
g_signal_emit_by_name(parentAcc, "children-changed::add", child_count, accessible);
}
}
/* Set parent relationship in ATK. */
if (parentAcc) {
atk_object_set_parent(accessible, parentAcc);
}
/* Set accessible properties. */
const gchar *name = tk_get_name(accessible);
if (name) {
tk_set_name(accessible, name);
} else {
tk_set_name(accessible, Tk_PathName(tkwin));
}
AtkRole role = GetAtkRoleForWidget(tkwin);
atk_object_set_role(accessible, role);
atk_object_set_description(accessible, name ? name : ((TkAtkAccessible *)accessible)->path);
TkAtkAccessible_RegisterEventHandlers(tkwin, (TkAtkAccessible *)accessible);
atk_object_notify_state_change(ATK_OBJECT(accessible), ATK_STATE_SHOWING, TRUE);
}
/* Remove toplevel window from ATK object list. */
static void UnregisterToplevelWindow(AtkObject *accessible)
{
if (!accessible) return;
if (g_list_find(toplevel_accessible_objects, accessible)) {
/* Find position before removal. */
gint index = g_list_index(toplevel_accessible_objects, accessible);
/* Remove from toplevel list. */
toplevel_accessible_objects = g_list_remove(toplevel_accessible_objects, accessible);
/* Notify about removed child. */
g_signal_emit_by_name(tk_root_accessible, "children-changed::remove", index, accessible);
}
}
/* Function to return the toplevel window that contains a given Tk widget. */
Tk_Window GetToplevelOfWidget(Tk_Window tkwin)
{
if (!tkwin) return NULL;
Tk_Window current = tkwin;
if (Tk_IsTopLevel(current)) return current;
while (current != NULL && Tk_WindowId(current) != None) {
Tk_Window parent = Tk_Parent(current);
if (parent == NULL || Tk_IsTopLevel(current)) break;
current = parent;
}
return Tk_IsTopLevel(current) ? current : NULL;
}
/*
* Root window setup. These are the foundation of the
* accessibility object system in ATK. atk_get_root() is the
* critical link to at-spi - it is called by the ATK system
* and at-spi bridge initialization will silently fail if this
* function is not implemented. This API is confusing because
* atk_get_root cannot be called directly in our functions, but
* it still must be implemented if we are using a custom setup,
* as we are here.
*/
static AtkObject *tk_util_get_root(void)
{
if (!tk_root_accessible) {
TkAtkAccessible *acc = g_object_new(TK_ATK_TYPE_ACCESSIBLE, NULL);
tk_root_accessible = ATK_OBJECT(acc);
atk_object_initialize(tk_root_accessible, NULL);
/* Set proper name and role. */
atk_object_set_role(tk_root_accessible, ATK_ROLE_APPLICATION);
tk_set_name(tk_root_accessible, "Tk Application");
}
return tk_root_accessible;
}
/* Core function linking Tk objects to the ATK root object and at-spi. */
AtkObject *atk_get_root(void) {
return tk_util_get_root();
}
/* ATK-Tk object creation with proper parent/child relationship. */
AtkObject *TkCreateAccessibleAtkObject(Tcl_Interp *interp, Tk_Window tkwin, const char *path)
{
if (!interp || !tkwin) return NULL;
AtkObject *existing = GetAtkObjectForTkWindow(tkwin);
if (existing) return existing;
TkAtkAccessible *acc = g_object_new(TK_ATK_TYPE_ACCESSIBLE, NULL);
acc->interp = interp;
acc->tkwin = tkwin;
acc->path = g_utf8_make_valid(path, -1);
AtkObject *obj = ATK_OBJECT(acc);
AtkRole role = GetAtkRoleForWidget(tkwin);
atk_object_set_role(obj, role);
gchar *name = GetAtkNameForWidget(tkwin);
if (name) {
atk_object_set_name(obj, name);
g_free(name);
}
/* Check if widget has focus using TkGetFocusWin. */
if (role == ATK_ROLE_PUSH_BUTTON || role == ATK_ROLE_CHECK_BOX ||
role == ATK_ROLE_RADIO_BUTTON || role == ATK_ROLE_TOGGLE_BUTTON ||
role == ATK_ROLE_ENTRY || role == ATK_ROLE_TEXT ||
role == ATK_ROLE_LIST_ITEM || role == ATK_ROLE_MENU_ITEM ||
role == ATK_ROLE_TREE_ITEM || role == ATK_ROLE_COMBO_BOX ||
role == ATK_ROLE_SPIN_BUTTON || role == ATK_ROLE_TOGGLE_BUTTON) {
TkWindow *focusPtr = TkGetFocusWin((TkWindow*)tkwin);
acc->is_focused = (focusPtr == (TkWindow*)tkwin) ? 1 : 0;
}
RegisterAtkObjectForTkWindow(tkwin, obj);
TkAtkAccessible_RegisterEventHandlers(tkwin, acc);
return obj;
}
/*
* Functions to map Tk window to its corresponding ATK object.
*/
void InitAtkTkMapping(void)
{
if (!tk_to_atk_map) {
tk_to_atk_map = g_hash_table_new_full(g_direct_hash, g_direct_equal,
NULL, (GDestroyNotify)g_object_unref);
}
}
void RegisterAtkObjectForTkWindow(Tk_Window tkwin, AtkObject *atkobj)
{
if (!tkwin || !atkobj) return;
InitAtkTkMapping();
g_object_ref(atkobj); /*Increment ref count because hash table takes ownership. */
g_hash_table_insert(tk_to_atk_map, tkwin, atkobj);
}
AtkObject *GetAtkObjectForTkWindow(Tk_Window tkwin)
{
if (!tk_to_atk_map || !tkwin) return NULL;
return (AtkObject *)g_hash_table_lookup(tk_to_atk_map, tkwin);
}
void UnregisterAtkObjectForTkWindow(Tk_Window tkwin)
{
if (!tk_to_atk_map || !tkwin) return;
AtkObject *atkobj = (AtkObject *)g_hash_table_lookup(tk_to_atk_map, tkwin);
if (atkobj) {
/* If toplevel, unregister from toplevel list. */
if (g_list_find(toplevel_accessible_objects, atkobj)) {
UnregisterToplevelWindow(atkobj);
}
g_hash_table_remove(tk_to_atk_map, tkwin);
}
}
/*
*----------------------------------------------------------------------
*
* Event handlers - update Tk and ATK in response to various X events.
*
*----------------------------------------------------------------------
*/
/* Configure event handlers. */
void TkAtkAccessible_RegisterEventHandlers(Tk_Window tkwin, void *tkAccessible)
{
if (!tkwin || !tkAccessible) return;
Tk_CreateEventHandler(tkwin, StructureNotifyMask,
TkAtkAccessible_DestroyHandler, tkAccessible);
Tk_CreateEventHandler(tkwin, FocusChangeMask,
TkAtkAccessible_FocusHandler, tkAccessible);
Tk_CreateEventHandler(tkwin, SubstructureNotifyMask,
TkAtkAccessible_CreateHandler, tkAccessible);
Tk_CreateEventHandler(tkwin, ConfigureNotify,
TkAtkAccessible_ConfigureHandler, tkAccessible);
}
/* Respond to <CreateNotify> events. */
static void TkAtkAccessible_CreateHandler(void *clientData, XEvent *eventPtr)
{
if (!eventPtr || eventPtr->type != CreateNotify) {
return;
}
Tk_Window tkwin = (Tk_Window)clientData;
if (!tkwin) {
return;
}
Tcl_Interp *interp = Tk_Interp(tkwin);
if (!interp) {
return;
}
/* Don’t recreate if we already have an accessible for this window. */
if (GetAtkObjectForTkWindow(tkwin)) {
return;
}
/* Create a new accessible object for this widget. */
AtkObject *accObj = TkCreateAccessibleAtkObject(interp, tkwin, Tk_PathName(tkwin));
if (!accObj) {
return;
}
if (Tk_IsTopLevel(tkwin)) {
/* Root or non-root toplevel window */
RegisterToplevelWindow(interp, tkwin, accObj);
} else {
/* Child widget inside some toplevel. */
Tk_Window parentWin = Tk_Parent(tkwin);
AtkObject *parentAcc = GetAtkObjectForTkWindow(parentWin);
if (!parentAcc && parentWin) {
/* Parent not yet registered: create it now */
parentAcc = TkCreateAccessibleAtkObject(interp, parentWin, Tk_PathName(parentWin));
if (parentAcc) {
atk_object_set_parent(parentAcc, GetAtkObjectForTkWindow(GetToplevelOfWidget(parentWin)));
RegisterAtkObjectForTkWindow(parentWin, parentAcc);
}
}
if (parentAcc) {
atk_object_set_parent(accObj, parentAcc);
/* Insert into our Tk→Atk mapping. */
RegisterAtkObjectForTkWindow(tkwin, accObj);
/* Notify AT clients that a child was added. */
gint idx = atk_object_get_n_accessible_children(parentAcc);
g_signal_emit_by_name(parentAcc, "children-changed::add", idx, accObj);
}
}
}
/* Respond to <DestroyNotify> events. */
static void TkAtkAccessible_DestroyHandler(void *clientData, XEvent *eventPtr)
{
if (eventPtr->type != DestroyNotify) return;
TkAtkAccessible *acc = (TkAtkAccessible *)clientData;
if (!acc) return;
GObject *obj = (GObject*)acc;
tk_atk_accessible_finalize(obj);
}
/* Respond to <Configure> events. */
static void TkAtkAccessible_ConfigureHandler(void *clientData, XEvent *eventPtr)
{
if (!eventPtr || eventPtr->type != ConfigureNotify) {
return;
}
Tk_Window tkwin = (Tk_Window)clientData;
if (!tkwin) {
return;
}
AtkObject *accObj = GetAtkObjectForTkWindow(tkwin);
if (!accObj) {
return;
}
/* Update geometry on configure. */
gint x, y, w, h;
tk_get_extents(ATK_COMPONENT(accObj), &x, &y, &w, &h, ATK_XY_SCREEN);
/* If the widget just became mapped/visible, fire state-change signals. */
if (Tk_IsMapped(tkwin)) {
atk_object_notify_state_change(accObj, ATK_STATE_VISIBLE, TRUE);
atk_object_notify_state_change(accObj, ATK_STATE_SHOWING, TRUE);
/* For child widgets of a non-root toplevel, also nudge with children-changed. */
if (!Tk_IsTopLevel(tkwin)) {
Tk_Window parentWin = Tk_Parent(tkwin);
AtkObject *parentAcc = GetAtkObjectForTkWindow(parentWin);
if (parentAcc) {
gint idx = atk_object_get_n_accessible_children(parentAcc) - 1;
if (idx < 0) idx = 0;
g_signal_emit_by_name(parentAcc, "children-changed::add", idx, accObj);
}
}
} else {
atk_object_notify_state_change(accObj, ATK_STATE_SHOWING, FALSE);
atk_object_notify_state_change(accObj, ATK_STATE_VISIBLE, FALSE);
}
}
/* Respond to <FocusIn/Out> events. */
static void TkAtkAccessible_FocusHandler(void *clientData, XEvent *eventPtr)
{
TkAtkAccessible *acc = (TkAtkAccessible *)clientData;
if (!acc || !acc->tkwin || !Tk_IsMapped(acc->tkwin)) return;
AtkObject *obj = ATK_OBJECT(acc);
gboolean focused = (eventPtr->type == FocusIn);
AtkRole role = GetAtkRoleForWidget(acc->tkwin);
InvalidateVirtualChildren(acc->tkwin);
/* Emit window-level signals for toplevels. */
if (role == ATK_ROLE_WINDOW) {
if (focused) {
g_signal_emit_by_name(obj, "window-activate");
} else {
g_signal_emit_by_name(obj, "window-deactivate");
}
}
/* Update focus state. */
acc->is_focused = focused ? 1 : 0;
atk_object_notify_state_change(obj, ATK_STATE_FOCUSED, focused);
g_signal_emit_by_name(obj, "focus-event", focused);
/* Handle child widget focus */
if (focused && role != ATK_ROLE_WINDOW) {
/* Verify this widget is the focused one using TkGetFocusWin. */
TkWindow *focusPtr = TkGetFocusWin((TkWindow*)acc->tkwin);
if (focusPtr == (TkWindow*)acc->tkwin) {
/* Ensure the child widget's accessible object is created. */
AtkObject *child_obj = GetAtkObjectForTkWindow(acc->tkwin);
if (!child_obj) {
child_obj = TkCreateAccessibleAtkObject(acc->interp, acc->tkwin, Tk_PathName(acc->tkwin));
if (child_obj) {
AtkObject *parent_obj = GetAtkObjectForTkWindow(Tk_Parent(acc->tkwin));
if (parent_obj) {
atk_object_set_parent(child_obj, parent_obj);
TkAtkAccessible_RegisterEventHandlers(acc->tkwin, (TkAtkAccessible *)child_obj);
}
}
}
if (child_obj) {
/* Emit focus signals for the child. */
atk_object_notify_state_change(child_obj, ATK_STATE_FOCUSED, TRUE);
g_signal_emit_by_name(child_obj, "focus-event", TRUE);
/* Notify parent of active descendant change for containers. */
AtkObject *parent_obj = atk_object_get_parent(child_obj);
if (parent_obj && (role == ATK_ROLE_LIST_BOX || role == ATK_ROLE_MENU ||
role == ATK_ROLE_MENU_BAR || role == ATK_ROLE_TREE ||
role == ATK_ROLE_TREE_TABLE)) {
g_signal_emit_by_name(parent_obj, "active-descendant-changed", child_obj);
}
}
}
}
}
/*
*----------------------------------------------------------------------
*
* Tcl command implementations - expose these API's to Tcl scripts and
* initialize ATK integration in Tcl/Tk.
*
*----------------------------------------------------------------------
*/
/*
*----------------------------------------------------------------------
*
* EmitSelectionChanged --
*
* Accessibility system notification when selection changed.
*
* Results:
*
* Accessibility system is made aware when selection or value data is changed.
*
* Side effects:
*
* None.
*
*----------------------------------------------------------------------
*/
static int EmitSelectionChanged(
TCL_UNUSED(void *), /* clientData */
Tcl_Interp *interp,
int objc,
Tcl_Obj *const objv[])
{
/* Validate arguments. */
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "window");
return TCL_ERROR;
}
const char *windowName = Tcl_GetString(objv[1]);
Tk_Window tkwin = Tk_NameToWindow(interp, windowName, Tk_MainWindow(interp));
if (!tkwin) return TCL_OK; /* Window not found, nothing to do. */
InvalidateVirtualChildren(tkwin);
/* Ensure AtkObject exists for this window. */
AtkObject *obj = GetAtkObjectForTkWindow(tkwin);
if (!obj) {
obj = TkCreateAccessibleAtkObject(interp, tkwin, windowName);
if (!obj) return TCL_OK;
TkAtkAccessible_RegisterEventHandlers(tkwin, (TkAtkAccessible *)obj);
}
AtkRole role = tk_get_role(obj);
/* Handle text/entry widgets separately. */
if (role == ATK_ROLE_TEXT || role == ATK_ROLE_ENTRY) {
/* Emit a proper "insert" text change and caret-move for typing. */
g_signal_emit_by_name(obj, "text-changed::insert", 0, 0, NULL);
/* Compute or estimate caret position */
int caret_offset = 0;
if (ATK_IS_TEXT(obj)) {
caret_offset = atk_text_get_caret_offset(ATK_TEXT(obj));
}
g_signal_emit_by_name(obj, "text-caret-moved", caret_offset);
return TCL_OK;
}
/* Call the robust selection-change notifier. */
TkAtkNotifySelectionChanged(tkwin);
/* Handle value-changed for sliders, spin buttons, scrollbars, and progress bars. */
if (role == ATK_ROLE_SCROLL_BAR || role == ATK_ROLE_SLIDER ||
role == ATK_ROLE_SPIN_BUTTON || role == ATK_ROLE_PROGRESS_BAR)
{
GValue gval = G_VALUE_INIT;
tk_get_current_value(ATK_VALUE(obj), &gval);
/* Notify ATK clients. */
g_signal_emit_by_name(obj, "value-changed");
g_object_notify(G_OBJECT(obj), "accessible-value");
g_value_unset(&gval);
}
/* Invalidate virtual children if needed (e.g., for spinbox-linked lists) */
InvalidateVirtualChildren(tkwin);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* EmitFocusChanged --
*
* Accessibility system notification when focus changed.
*
* Results:
*
* Accessibility system is made aware when focus is changed.
*
* Side effects:
*
* None.
*
*----------------------------------------------------------------------
*/
static int EmitFocusChanged(
TCL_UNUSED(void *), /* clientData */
Tcl_Interp *interp,
int objc,
Tcl_Obj *const objv[])
{
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "window");
return TCL_ERROR;
}
/* No-op on X11. All work is done in FocusHandler. */
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* IsScreenReaderRunning --
*
* Runtime check to see if screen reader is running.
*
* Results:
*
* Returns if screen reader is active or not.
*
* Side effects:
*
* None.
*
*----------------------------------------------------------------------
*/
static int IsScreenReaderRunning(
TCL_UNUSED(void *), /* clientData */
Tcl_Interp *interp,
TCL_UNUSED(int), /* objc */
TCL_UNUSED(Tcl_Obj *const *)) /* objv */
{
int result = IsScreenReaderActive();
Tcl_SetObjResult(interp, Tcl_NewIntObj(result));
return TCL_OK;
}
/*
* Helper function to determine if screen reader is running. Separate function
* because it can be called internally as well as a Tcl command.
*/
static int IsScreenReaderActive(void)
{
FILE *fp = popen("pgrep -x orca", "r");
if (!fp) return 0;
char buffer[16];
int running = (fgets(buffer, sizeof(buffer), fp) != NULL);
pclose(fp);
return running ? 1 : 0;
}
/*
*----------------------------------------------------------------------
*
* TkAtkAccessibleObjCmd --
*
* Main command for adding and managing accessibility objects to Tk
* widgets on Linux using the ATK accessibility API.
*
* Results:
*
* A standard Tcl result.
*
* Side effects:
*
* Tk widgets are now accessible to screen readers.
*
*----------------------------------------------------------------------
*/
int TkAtkAccessibleObjCmd(
TCL_UNUSED(void *), /* clientData */
Tcl_Interp *interp,
int objc,
Tcl_Obj *const objv[])
{
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "window");
return TCL_ERROR;
}
char *windowName = Tcl_GetString(objv[1]);
if (!windowName) {
Tcl_SetResult(interp, "Window name cannot be null.", TCL_STATIC);
return TCL_ERROR;
}
Tk_Window tkwin = Tk_NameToWindow(interp, windowName, Tk_MainWindow(interp));
if (tkwin == NULL) {
Tcl_SetResult(interp, "Invalid window name.", TCL_STATIC);
return TCL_ERROR;
}
/* Check if already registered. */
if (GetAtkObjectForTkWindow(tkwin)) {
return TCL_OK;
}
/* Create accessible object. */
TkAtkAccessible *accessible = (TkAtkAccessible*) TkCreateAccessibleAtkObject(interp, tkwin, windowName);
if (accessible == NULL) {
Tcl_SetResult(interp, "Failed to create accessible object.", TCL_STATIC);
return TCL_ERROR;
}
/* Always use RegisterToplevelWindow for proper hierarchy. */
RegisterToplevelWindow(interp, tkwin, ATK_OBJECT(accessible));
return TCL_OK;
}
#endif
/*
*----------------------------------------------------------------------
*
* TkAtkAccessibility_Init --
*
* Initializes the accessibility module.
*
* Results:
*
* A standard Tcl result.
*
* Side effects:
*
* Accessibility module is now activated.
*
*----------------------------------------------------------------------
*/
#ifdef USE_ATK
int TkAtkAccessibility_Init(Tcl_Interp *interp)
{
/* Initialize AT-SPI bridge. */
if (atk_bridge_adaptor_init(NULL, NULL) != 0) {
Tcl_SetResult(interp, "Failed to initialize AT-SPI bridge", TCL_STATIC);
return TCL_ERROR;
}
/* Get and initialize root accessible. */
tk_root_accessible = tk_util_get_root();
if (!tk_root_accessible) {
Tcl_SetResult(interp, "Failed to create root accessible object", TCL_STATIC);
return TCL_ERROR;
}
/* Activate widget-object hash table mapping. */
InitAtkTkMapping();
/*
* Establish GLib context for event loop processing.
* We are creating a custom GLib event source that the
* Tcl event loop will respond to.
*/
acc_context = ATK_CONTEXT;
Tcl_CreateEventSource (Atk_Event_Setup, Atk_Event_Check, 0);
/*
* GLib is VERY noisy. Shut off warnings to prevent logs/console
* from being polluted.
*/
g_log_set_handler("Atk", G_LOG_LEVEL_CRITICAL, ignore_atk_critical, NULL);
/* Initialize main window and create accessible object. */
Tk_Window mainWin = Tk_MainWindow(interp);
if (!mainWin) {
Tcl_SetResult(interp, "Failed to get main window", TCL_STATIC);
return TCL_ERROR;
}
Tk_MakeWindowExist(mainWin);
Tk_MapWindow(mainWin);
AtkObject *main_acc = TkCreateAccessibleAtkObject(interp, mainWin, Tk_PathName(mainWin));
if (!main_acc) {
Tcl_SetResult(interp, "Failed to create AtkObject for root window", TCL_STATIC);
return TCL_ERROR;
}
atk_object_set_role(main_acc, ATK_ROLE_WINDOW);
tk_set_name(main_acc, "Tk Application");
RegisterAtkObjectForTkWindow(mainWin, main_acc);
RegisterToplevelWindow(interp, mainWin, main_acc);
/* Register X event handlers. */
TkAtkAccessible_RegisterEventHandlers(mainWin, (TkAtkAccessible *)main_acc);
/* Register Tcl commands. */
Tcl_CreateObjCommand(interp, "::tk::accessible::add_acc_object",
TkAtkAccessibleObjCmd, NULL, NULL);
Tcl_CreateObjCommand(interp, "::tk::accessible::emit_selection_change",
EmitSelectionChanged, NULL, NULL);
Tcl_CreateObjCommand(interp, "::tk::accessible::emit_focus_change",
EmitFocusChanged, NULL, NULL);
Tcl_CreateObjCommand(interp, "::tk::accessible::check_screenreader",
IsScreenReaderRunning, NULL, NULL);
return TCL_OK;
}
#else
/* Stub command to run if Tk is compiled without accessibility support. */
static int
TkAccessibleStubObjCmd(
TCL_UNUSED(void *), /* clientData */
Tcl_Interp *interp,
TCL_UNUSED(int), /* objc */
TCL_UNUSED(Tcl_Obj *const *)) /* objv */
{
static int warned = 0;
if (!warned) {
Tcl_SetObjResult(interp,
Tcl_NewStringObj("Warning: Tk accessibility support not available in this build.", -1));
warned = 1;
} else {
Tcl_SetObjResult(interp, Tcl_NewObj()); /* Empty string after first warning. */
}
return TCL_OK;
}
int TkAtkAccessibility_Init(Tcl_Interp *interp)
{
Tcl_CreateObjCommand(interp, "::tk::accessible::add_acc_object", TkAccessibleStubObjCmd, NULL, NULL);
Tcl_CreateObjCommand(interp, "::tk::accessible::emit_selection_change", TkAccessibleStubObjCmd, NULL, NULL);
Tcl_CreateObjCommand(interp, "::tk::accessible::emit_focus_change", TkAccessibleStubObjCmd, NULL, NULL);
Tcl_CreateObjCommand(interp, "::tk::accessible::check_screenreader", TkAccessibleStubObjCmd, NULL, NULL);
return TCL_OK;
}
#endif
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|