summaryrefslogtreecommitdiffstats
path: root/library/accessibility.tcl
blob: a0a485cd15913a2836c0922e569450fb24bdc559 (plain)
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
# accessibility.tcl --

# This file defines the 'tk accessible' command for screen reader support
# on X11, Windows, and macOS. It implements an abstraction layer that
# presents a consistent API across the three platforms.

# Copyright © 2009 Allen B. Taylor.
# Copyright © 2024-2025 Kevin Walzer.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#

if {[info commands ::tk::accessible::check_screenreader] eq "" || [::tk::accessible::check_screenreader] eq 0 || [::tk::accessible::check_screenreader] eq ""} {
    # Do not load if screen reader is not running or command is unavailable
    proc ::tk::accessible args {
	return 0
    }
} else {
    if {[tk windowingsystem] eq "x11" && [::tk::accessible::check_screenreader] eq 1} {

	# Add border to all X11 widgets with accessible focus. A highlight rectangle
	# is drawn over focused widgets by the screen reader app on
	# macOS and Windows (VoiceOver, NVDA), but not on X11. Configuring
	# "-relief groove" and binding to FocusIn/Out events is the cleanest
	# way to accomplish this.

	namespace eval ::tk::accessible {
	    variable origConfig
	    array set origConfig {}

	    # Apply to classic Tk widgets
	    proc ClassicFocusIn {w} {
		variable origConfig
		if {![info exists origConfig($w)]} {
		    set origConfig($w) [list [$w cget -relief] [$w cget -borderwidth]]
		}
		$w configure -relief groove -borderwidth 2
	    }

	    proc ClassicFocusOut {w} {
		variable origConfig
		if {[info exists origConfig($w)]} {
		    lassign $origConfig($w) relief border
		    $w configure -relief $relief -borderwidth $border
		}
	    }

	    # Apply focus bindings to a widget
	    proc AddClassic {w} {
		bindtags $w [linsert [bindtags $w] 0 FocusBorder]
	    }

	    # Setup global ttk styles
	    proc InitTtk {} {
		foreach class {TButton TEntry TCombobox TCheckbutton TRadiobutton \
				   Treeview TScrollbar TScale TSpinbox TLabel} {
		    ttk::style map $class \
			-relief {focus groove !focus flat} \
			-borderwidth {focus 2 !focus 1}
		}
	    }

	    # Install focusborder bindtag for classic widgets
	    bind FocusBorder <FocusIn>  {+::tk::accessible::ClassicFocusIn %W}
	    bind FocusBorder <FocusOut> {+::tk::accessible::ClassicFocusOut %W}

	    # Install ttk mappings
	    ::tk::accessible::InitTtk

	    # Save the original widget commands and replace with wrappers
	    foreach wtype {button entry text listbox scale spinbox scrollbar label radiobutton checkbutton} {
		if {[llength [info commands ::tk::accessible::orig_$wtype]] == 0} {
		    rename ::$wtype ::tk::accessible::orig_$wtype
		    proc ::$wtype {args} [string map [list %TYPE% $wtype] {
			# Create the widget with the original command
			set w [::tk::accessible::orig_%TYPE% {*}$args]
			# Add focus bindings
			::tk::accessible::AddClassic $w
			return $w
		    }]
		}
	    }
	}
    }

    namespace eval ::tk::accessible {

	if {[tk windowingsystem] eq "x11" } {
	    # ATK/Orca has trouble with some data, especially with fine-grained
	    # text operations. In those cases when Orca is not picking up on
	    # text/selection updates from Tk, try shelling out to the
	    # command-line voice that comes with Orca.
	    proc speak {text} {
		if {[::tk::accessible::check_screenreader] eq "1"} {
		    # Escape quotes in the text.
		    set safe_text [string map {"\"" "\\\""} $text]

		    # Try spd-say first.
		    if {[catch {exec spd-say $safe_text} result]} {
			# fallback to espeak if spd-say fails
			catch {exec espeak $safe_text} result
		    }
		}
	    }
	}

	# Attach a variable trace to run _updateselection when a button changes.
	proc _attach_trace {w} {
	    # Radiobuttons/Checkbuttons always have a -variable.
	    set var [$w cget -variable]
	    if {$var ne ""} {
		# Avoid multiple traces on the same variable.
		catch {trace remove variable $var write [list ::tk::accessible::_vartrace $w]}
		trace add variable ::$var write [list ::tk::accessible::_vartrace $w]
	    }
	}

	# Trace handler.
	proc _vartrace {w args} {
	    if {[winfo exists $w]} {
		::tk::accessible::_updateselection $w
	    }
	}

	# Get button text
	proc _getbuttontext {w} {
	    set txt [$w cget -text]
	    ::tk::accessible::speak $txt
	}

	# Check message text on dialog.
	proc _getdialogtext {w} {
	    if {[winfo exists $w.msg]} {
		return [$w.msg cget -text]
	    }
	}

	# Get text in text widget.
	proc _gettext {w} {
	    if {[winfo class $w] ne "Text"} {
		return ""
	    }
	    if {[$w tag ranges sel] eq ""} {
		set data [$w get 1.0 end]
	    }  else {
		set data [$w get sel.first sel.last]
	    }
	    return $data
	}

	# Get text in entry widget.
	proc _getentrytext {w} {
	    set data [$w get]
	    return $data
	}

	# Create keypress/word bindings once for all supported widgets.
	proc install_keycapture {w} {
	    # Ensure we don’t double-bind
	    if {[lsearch -exact [bindtags $w] KeyCaptureTag] == -1} {
		bindtags $w [linsert [bindtags $w] 1 KeyCaptureTag]
	    }
	}

	# Core binding tag (shared by text, entry, ttk::entry).
	bind KeyCaptureTag <KeyPress> {+::tk::accessible::_handle_keypress %W %K}
	bind KeyCaptureTag <KeyRelease-space> {+ after 10 [list ::tk::accessible::_get_prev_word %W]}

	# Handle each keypress event.
	proc _handle_keypress {w key} {
	    # Ignore modifier keys and non-printables
	    if {$key eq "" || [string length $key] > 1} {
		return
	    }

	    # If user pressed space, do nothing here — handled on KeyRelease.
	    if {$key eq "space"} {
		return
	    }

	    # Otherwise emit single-character updates.
	    ::tk::accessible::set_acc_value $w $key
	    if {[tk windowingsystem] eq "x11"} {
		::tk::accessible::speak $key
		# Windows speaks the individual keypress by default
	    } elseif {[tk windowingsystem] eq "aqua"}  {
		::tk::accessible::emit_selection_change $w
	    }
	}

	# Retrieve the previous word before the cursor (called after space release).
	proc _get_prev_word {w} {
	    update idletasks   ;# ensure buffer is current
	    set class [winfo class $w]
	    set before ""

	    if {$class eq "Text"} {
		# For text widgets.
		set start [$w index "insert linestart"]
		if {[$w compare insert == $start]} {
		    # nothing before cursor at start of line.
		    return
		}
		set before [$w get $start "insert -1c"]
	    } elseif {$class eq "Entry" || $class eq "TEntry"} {
		# For entry/ttk::entry widgets.
		set full [$w get]
		set pos [$w index insert]
		if {$pos > 0} {
		    set before [string range $full 0 [expr {$pos - 2}]]
		}
	    } else {
		return
	    }

	    # Extract last word before the space.
	    if {[regexp -nocase -- {\S+$} $before match]} {
		::tk::accessible::set_acc_value $w $match
		if {[tk windowingsystem] eq "x11"} {
		    ::tk::accessible::speak $match
		} else {
		    ::tk::accessible::emit_selection_change $w
		}
	    }
	}

	# Attempt to verify if treeview is tree or table. This works
	# for simple cases but may not be perfect.
	proc _checktree {w} {
	    if {[expr {"tree" in [$w cget -show]}] eq 1} {
		return "Tree"
	    } else {
		return "Table"
	    }
	}

	# Get data from ttk::treeview.
	proc _gettreeviewdata {w} {
	    set values [$w item [lindex [$w selection] 0] -values]
	    return $values
	}

	# Get treeview column names.
	proc _getcolumnnames {w} {
	    set columns [$w cget -columns]
	    foreach col $columns {
		set text [$w heading $col -text]
		lappend headerlist $text
	    }
	    return $headerlist
	}

	# Get selection status from radiobuttons.
	proc _getradiodata {w} {
	    set var [$w cget -variable]
	    if {$var ne "" && [uplevel #0 info exists $var]} {
		set value [uplevel #0 set $var]
		set result [expr {$value eq [$w cget -value]}]
		if {$result eq 1} {
		    return "selected"
		} else {
		    return "not selected"
		}
		return 0
	    }
	}

	# Get selection status from checkbuttons.
	proc _getcheckdata {w} {
	    set var [$w cget -variable]
	    if {$var ne "" && [uplevel #0 info exists $var]} {
		set value [uplevel #0 set $var]
		set result [expr {$value eq [$w cget -onvalue]}]
		if {$result eq 1} {
		    return "selected"
		} else {
		    return "not selected"
		}
		return 0
	    }
	}

	# Update data selection for various widgets.
	proc _updateselection {w} {
	    if {[winfo class $w] eq "Radiobutton" || [winfo class $w] eq "TRadiobutton"} {
		set data [::tk::accessible::_getradiodata $w]
		set role [::tk::accessible::get_acc_role $w]
		set description [::tk::accessible::get_acc_description $w]
		::tk::accessible::set_acc_value $w $data
		::tk::accessible::emit_selection_change $w
		if {[tk windowingsystem] eq "x11"} {
		    ::tk::accessible::speak "$role $description $data"
		}
	    }
	    if {[winfo class $w] eq "Checkbutton" || [winfo class $w] eq "TCheckbutton" || [winfo class $w] eq "Toggleswitch"} {
		set data [::tk::accessible::_getcheckdata $w]
		set role [::tk::accessible::get_acc_role $w]
		set description [::tk::accessible::get_acc_description $w]
		::tk::accessible::set_acc_value $w $data
		::tk::accessible::emit_selection_change $w
		if {[tk windowingsystem] eq "x11"} {
		    ::tk::accessible::speak "$role $description $data"
		}
	    }

	    if {[winfo class $w] eq "Listbox"} {
		set data [$w get [$w curselection]]
		::tk::accessible::set_acc_value $w $data
		::tk::accessible::emit_selection_change $w
	    }
	    if {[winfo class $w] eq "Treeview"} {
		set data [::tk::accessible::_gettreeviewdata $w]
		::tk::accessible::set_acc_value $w $data
		::tk::accessible::emit_selection_change $w
	    }
	    if {[winfo class $w] eq "Entry" || [winfo class $w] eq "TEntry"}  {
		set data [::tk::accessible::_getentrytext $w]
		::tk::accessible::set_acc_value $w $data
		if {[tk windowingsystem] eq "x11"} {
		    ::tk::accessible::speak $data
		} else {
		    ::tk::accessible::emit_selection_change $w
		}
	    }
	    if {[winfo class $w] eq "TCombobox"} {
		set data [$w get]
		::tk::accessible::set_acc_value $w $data
		::tk::accessible::emit_selection_change $w
	    }
	    if {[winfo class $w] eq "TNotebook"}  {
		set data  [$w tab current -text]
		::tk::accessible::set_acc_value $w $data
		::tk::accessible::emit_selection_change $w
	    }
	    if {[winfo class $w] eq "Text"}  {
		set data [::tk::accessible::_gettext $w]
		::tk::accessible::set_acc_value $w $data
		::tk::accessible::emit_selection_change $w
	    }
	    if {[winfo class $w] eq "TProgressbar"}  {
		set data [::tk::accessible::_getpbvalue $w]
		::tk::accessible::set_acc_value $w $data
		::tk::accessible::emit_selection_change $w
	    }
	    # Menu only needs to be tracked on X11 - native on Aqua and Windows.
	    if {[tk windowingsystem] eq "x11"} {
		if {[winfo class $w] eq "Menu"} {
		    set data [$w entrycget active -label]
		    ::tk::accessible::set_acc_value $w $data
		    ::tk::accessible::emit_selection_change $w
		    ::tk::accessible::emit_focus_change $w
		}
	    }
	}

	# Increment values in various widgets in response to keypress events.
	proc _updatescale {w key} {
	    if {[winfo class $w] eq "Scale"} {
		switch -- $key {
		    Right {
			tk::ScaleIncrement $w down little noRepeat
			set data [$w get]
			::tk::accessible::set_acc_value $w $data
			::tk::accessible::emit_selection_change $w
		    }
		    Left {
			tk::ScaleIncrement $w up little noRepeat
			set data [$w get]
			::tk::accessible::set_acc_value $w $data
			::tk::accessible::emit_selection_change $w
		    }
		}
	    }
	    if {[winfo class $w] eq "TScale"} {
		switch -- $key {
		    Right {
			ttk::scale::Increment $w 1
			set data [$w get]
			::tk::accessible::set_acc_value $w $data
			::tk::accessible::emit_selection_change $w
		    }
		    Left {
			ttk::scale::Increment $w -1
			set data [$w get]
			::tk::accessible::set_acc_value $w $data
			::tk::accessible::emit_selection_change $w
		    }
		}
	    }

	    if {[winfo class $w] eq "Spinbox"} {
		switch -- $key {
		    Up {
			$w invoke buttonup
			set data [$w get]
			::tk::accessible::set_acc_value $w $data
			::tk::accessible::emit_selection_change $w
		    }
		    Down {
			$w invoke buttondown
			set data [$w get]
			::tk::accessible::set_acc_value $w $data
			::tk::accessible::emit_selection_change $w
		    }
		}
	    }
	    if {[winfo class $w] eq "TSpinbox"} {
		switch -- $key {
		    Up {
			ttk::spinbox::Spin $w +1
			set data [$w get]
			::tk::accessible::set_acc_value $w $data
			::tk::accessible::emit_selection_change $w
		    }
		    Down {
			ttk::spinbox::Spin $w -1
			set data [$w get]
			::tk::accessible::set_acc_value $w $data
			::tk::accessible::emit_selection_change $w
		    }
		}
	    }
	    if {[winfo class $w] eq "TCombobox"} {
		switch -- $key {
		    Down {
			ttk::combobox::Post $w
			set data [$w get]
		    }
		    Escape {
			ttk::combobox::Unpost $w
			$w selection range 0 end
			if {[tk windowingsystem] eq "aqua"} {
			    event generate <Command-a>
			} else {
			    event generate <Control-a>
			}
			set data [$w get]
			::tk::accessible::set_acc_value $w $data
			::tk::accessible::emit_selection_change $w
		    }
		}
	    }
	    if {[winfo class $w] eq "TNotebook"} {
		switch -- $key {
		    Right {
			{ ttk::notebook::CycleTab %W  1; break }
			set data [$w get]
			::tk::accessible::set_acc_value $w $data
			::tk::accessible::emit_selection_change $w
		    }
		    Left {
			ttk::scale::Increment $w -1
			set data [$w get]
			::tk::accessible::set_acc_value $w $data
			::tk::accessible::emit_selection_change $w
		    }
		}
	    }
	}

	# Get value of progress bar.
	proc _getpbvalue  {pb} {
	    # Make sure widget exists
	    if {![winfo exists $pb]} { return "" }

	    set mode [$pb cget -mode]

	    if {$mode eq "indeterminate"} {
		# Any indeterminate bar is considered busy
		return "busy"
	    } else {
		# Determinate: busy if value < maximum
		if {[$pb cget -value] < [$pb cget -maximum]} {
		    return "busy"
		} else {
		    return ""
		}
	    }
	}


	# Some widgets will not respond to keypress events unless
	# they have focus. Force Tk focus on the widget that currently has
	# accessibility focus if needed.

	proc _forceTkFocus {w} {
	    # Check to make sure window is not destroyed.
	    if {[winfo exists $w]} {
		if {[tk windowingsystem] eq "aqua"} {
		    if {[winfo class $w] in {Scale TScale Spinbox TSpinbox Listbox Treeview TProgressbar}} {
			if {[focus] ne $w} {
			    focus -force $w
			}
		    }
		} elseif {[tk windowingsystem] eq "win32"} {
		    ::tk::accessible::emit_focus_change $w
		}
	    }
	}

	# Set initial accessible attributes and add binding to <Map> event.
	# If the accessibility role is already set, return because
	# we only want these to fire once.
	proc _init {w role name description value state action} {
	    if {[catch {::tk::accessible::get_acc_role $w} msg]} {
		if {$msg == $role} {
		    return
		}
	    }
	    if {[tk windowingsystem] ne "aqua"} {
		# This is necessary to ensure correct accessible keyboard navigation
		if [winfo exists $w] {
		    $w configure -takefocus 1
		}
	    }

	    ::tk::accessible::set_acc_role $w $role
	    ::tk::accessible::set_acc_name $w $name
	    ::tk::accessible::set_acc_description $w $description
	    ::tk::accessible::set_acc_value $w $value
	    ::tk::accessible::set_acc_state $w $state
	    ::tk::accessible::set_acc_action $w $action

	}

	# Toplevel bindings.
	bind Toplevel <Map> {+::tk::accessible::_init \
				 %W \
				 Toplevel \
				 [wm title %W] \
				 {}  \
				 {} \
				 {} \
				 {} \
			     }

	# Button/TButton bindings.
	bind Button <Map> {+::tk::accessible::_init \
			       %W \
			       Button \
			       Button \
			       [%W cget -text] \
			       {} \
			       [%W cget -state] \
			       {%W invoke}\
			   }
	bind TButton <Map> {+::tk::accessible::_init \
				%W \
				Button \
				Button \
				[%W cget -text] \
				{}\
				[%W cget -state] \
				{%W invoke}\
			    }

	# Menubutton/TMButton bindings.
	bind Menubutton <Map> {+::tk::accessible::_init \
				   %W \
				   Button \
				   Button \
				   [%W cget -text] \
				   {} \
				   [%W cget -state] \
				   {%W invoke}\
			       }
	bind TMenubutton <Map> {+::tk::accessible::_init \
				    %W \
				    Button \
				    Button \
				    [%W cget -text] \
				    {} \
				    [%W cget -state] \
				    {%W invoke}\
				}

	# Canvas bindings.
	bind Canvas <Map> {+::tk::accessible::_init \
			       %W \
			       Canvas \
			       Canvas \
			       Canvas \
			       {} \
			       {} \
			       {}\
			   }

	# Checkbutton/TCheckbutton/Toggleswitch bindings.
	bind Checkbutton <Map> {+::tk::accessible::_init \
				    %W \
				    Checkbutton \
				    Checkbutton \
				    [%W cget -text] \
				    [set [%W cget -variable]] \
				    [%W cget -state] \
				    {%W invoke}\
				}
	bind TCheckbutton <Map> {+::tk::accessible::_init \
				     %W \
				     Checkbutton \
				     Checkbutton \
				     [%W cget -text] \
				     [set [%W cget -variable]] \
				     [%W cget -state] \
				     {%W invoke}\
				 }
	bind Toggleswitch <Map> {+::tk::accessible::_init \
				     %W \
				     Toggleswitch \
				     Toggleswitch \
				     Toggleswitch \
				     [%W switchstate] \
				     {} \
				     {%W toggle}\
				 }

	# Combobox bindings.
	bind TCombobox <Map> {+::tk::accessible::_init \
				  %W \
				  Combobox \
				  Combobox \
				  Combobox \
				  [%W get] \
				  [%W cget -state] \
				  {} \
			      }

	# Dialog bindings.
	bind Dialog <Map> {+::tk::accessible::_init\
			       %W \
			       Dialog \
			       [wm title %W] \
			       [::tk::accessible::_getdialogtext %W] \
			       {} \
			       {} \
			       {}\
			   }

	# Entry/TEntry bindings.
	bind Entry <Map> {+::tk::accessible::_init \
			      %W \
			      Entry \
			      Entry \
			      Entry \
			      [%W get] \
			      [%W cget -state] \
			      {} \
			      ; ::tk::accessible::install_keycapture %W}


	bind TEntry <Map> {+::tk::accessible::_init \
			       %W \
			       Entry \
			       Entry \
			       Entry \
			       [%W get] \
			       [%W state]\
			       {} \
			       ; ::tk::accessible::install_keycapture %W}


	# Listbox bindings.
	bind Listbox <Map> {+::tk::accessible::_init \
				%W \
				Listbox \
				Listbox \
				Listbox \
				[%W get [%W curselection]] \
				[%W cget -state]\
				{%W invoke}\
			    }

	# Progressbar bindings.
	bind TProgressbar <Map> {+::tk::accessible::_init \
				     %W \
				     Progressbar \
				     Progressbar \
				     Progressbar \
				     [::tk::accessible::_getpbvalue %W] \
				     [%W state] \
				     {}\
				 }

	# Radiobutton/TRadiobutton bindings.
	bind Radiobutton <Map> {+::tk::accessible::_init \
				    %W \
				    Radiobutton \
				    Radiobutton \
				    [%W cget -text] \
				    [%W cget -variable] \
				    [%W cget -state] \
				    {%W invoke}\
				}
	bind TRadiobutton <Map> {+::tk::accessible::_init \
				     %W \
				     Radiobutton \
				     Radiobutton \
				     [%W cget -text] \
				     [%W cget -variable] \
				     [%W cget -state] \
				     {%W invoke}\
				 }

	# Scale/TScale bindings.
	bind Scale <Map> {+::tk::accessible::_init \
			      %W \
			      Scale \
			      Scale \
			      Scale \
			      [%W get] \
			      [%W cget -state]\
			      {%W set}\
			  }
	bind TScale <Map> {+::tk::accessible::_init \
			       %W \
			       Scale \
			       Scale \
			       Scale \
			       [%W get] \
			       [%W cget -state] \
			       {%W set} \
			   }

	# Menu accessibility bindings for X11 only. Menus are native
	# on macOS/Windows, so we don’t expose them here.

	if {[tk windowingsystem] eq "x11"} {
	    variable prevActiveIndex
	    set prevActiveIndex ""

	    # Update the accessible notifications after idle.
	    proc _update_active_entry {menuWidget} {
		variable prevActiveIndex

		# Determine if this is a menubar or submenu.
		set isMenubar [expr {[winfo manager $menuWidget] eq "menubar"}]

		# Get current active index.
		set idx [$menuWidget index active]
		if {$idx eq "none" || $idx eq ""} {
		    return
		}

		# Build a safer unique key (include parent to avoid collisions).
		set currentKey "[winfo parent $menuWidget]-$menuWidget-$idx"

		# Prevent duplicate processing of the same index.
		if {[info exists prevActiveIndex] && $prevActiveIndex eq $currentKey} {
		    return
		}
		set prevActiveIndex $currentKey

		# Get the label of the active entry safely.
		if {[catch {set label [$menuWidget entrycget $idx -label]} err]} {
		    set label ""
		}

		# Announce menubar immediately.
		if $isMenubar {
		    ::tk::accessible::speak $label

		    # Clear dedupe cache so submenu index 1 is NOT skipped.
		    unset -nocomplain prevActiveIndex
		} else {
		    # Submenu - add gentle pause before announcing.
		    if {$label ne "" && [$menuWidget type $idx] ne "separator"} {
			after 100 [list ::tk::accessible::speak $label]
		    }
		}

		# Update accessible object.
		::tk::accessible::set_acc_name   $menuWidget $label
		::tk::accessible::set_acc_action $menuWidget [list $menuWidget invoke $idx]
		::tk::accessible::emit_selection_change $menuWidget
		::tk::accessible::emit_focus_change     $menuWidget
	    }


	    # Bind <<MenuSelect>> for mouse/keyboard navigation.
	    bind Menu <<MenuSelect>> {
		after idle [list ::tk::accessible::_update_active_entry %W]
	    }

	    # Key bindings - handle navigation AND announcement together
	    bind Menu <Up> {+
		# Only process if this is a submenu (not menubar)
		if {[winfo manager %W] ne "menubar"} {
		    set current [%W index active]
		    if {$current eq ""} {
			set idx [%W index last]
		    } else {
			set idx [expr {$current - 1}]
		    }
		    set lastIndex [%W index last]
		    while {$idx >= 0} {
			if {[%W type $idx] ne "separator" && [%W entrycget $idx -state] ne "disabled"} {
			    %W activate $idx
			    after idle [list ::tk::accessible::_update_active_entry %W]
			    break
			}
			incr idx -1
		    }
		}
	    }

	    bind Menu <Down> {+
		# Only process if this is a submenu (not menubar)
		if {[winfo manager %W] ne "menubar"} {
		    set current [%W index active]
		    if {$current eq ""} {
			set idx 0
		    } else {
			set idx [expr {$current + 1}]
		    }
		    set lastIndex [%W index last]
		    while {$idx <= $lastIndex} {
			if {[%W type $idx] ne "separator" && [%W entrycget $idx -state] ne "disabled"} {
			    %W activate $idx
			    after idle [list ::tk::accessible::_update_active_entry %W]
			    break
			}
			incr idx
		    }
		}
	    }

	    bind Menu <Return> {+
		set idx [%W index active]
		if {$idx ne "" && [%W type $idx] ne "separator" && [%W entrycget $idx -state] ne "disabled"} {
		    %W invoke $idx
		}
	    }

	    # Add bindings to catch when menus are posted/unposted.
	    bind Menu <Map> {+
		# Determine role based on whether this is a menubar.
		if {[winfo manager %W] eq "menubar"} {
		    set role Menubar ;# ATK_ROLE_MENU_BAR
		} else {
		    set role Menu ;# ATK_ROLE_MENU
		}

		::tk::accessible::_init \
				 %W \
				 $role \
				 $role \
				 {} \
				 {} \
				 {} \
				 {}

		# Handle initial vocalization based on menu type.
		if {$role eq "Menubar"} {
		    focus %W
		    after idle {
			set idx [%W index active]
			if {$idx eq "none" || $idx eq ""} {
			    %W activate 0
			}
			::tk::accessible::_update_active_entry %W
		    }
		} else {
		    # For submenus, announce the active entry when mapped.
		    after idle [list ::tk::accessible::_update_active_entry %W]
		}
	    }

	    # Handle initial menubar focus.
	    bind Menu <FocusIn> {+
		if {[winfo manager %W] eq "menubar"} {
		    set idx [%W index active]
		    if {$idx eq "none" || $idx eq ""} {
			%W activate 0
		    }
		    after idle [list ::tk::accessible::_update_active_entry %W]
		}
	    }
	}

	# Scrollbar/TScrollbar bindings.
	bind Scrollbar <Map> {+::tk::accessible::_init \
				  %W \
				  Scrollbar \
				  Scrollbar \
				  Scrollbar \
				  {} \
				  {} \
				  {}\
			      }
	bind TScrollbar <Map> {+::tk::accessible::_init \
				   %W \
				   Scrollbar \
				   Scrollbar \
				   Scrollbar \
				   {} \
				   {} \
				   {}\
			       }

	# Spinbox/TSpinbox bindings.
	bind Spinbox <Map> {+::tk::accessible::_init \
				%W \
				Spinbox \
				Spinbox \
				Spinbox \
				[%W get] \
				[%W cget -state] \
				{%W cget -command}\
			    }
	bind TSpinbox <Map> {+::tk::accessible::_init \
				 %W \
				 Spinbox \
				 Spinbox \
				 Spinbox \
				 [%W get] \
				 [%W state] \
				 {%W cget -command}\
			     }


	# Treeview bindings.
	bind Treeview <Map> {+::tk::accessible::_init \
				 %W \
				 [::tk::accessible::_checktree %W] \
				 [::tk::accessible::_checktree %W] \
				 [::tk::accessible::_getcolumnnames %W] \
				 [::tk::accessible::_gettreeviewdata %W] \
				 [%W state] \
				 {ttk::treeview::Press %W %x %y }\
			     }

	# Text bindings.
	bind Text <Map> {+::tk::accessible::_init \
			     %W \
			     Text \
			     Text \
			     Text \
			     [::tk::accessible::_gettext %W] \
			     [%W cget -state] \
			     {}\
			     ; ::tk::accessible::install_keycapture %W}

	# Label/TLabel bindings.
	bind Label <Map>  {+::tk::accessible::_init \
			       %W \
			       Label \
			       Label \
			       {} \
			       [%W cget -text] \
			       {}\
			       {}\
			   }

	bind TLabel <Map>    {+::tk::accessible::_init \
				  %W \
				  Label \
				  Label \
				  {} \
				  [%W cget -text] \
				  {}\
				  {}\
			      }

	# Notebook bindings - bind to the <<NotebookTabChanged>> event rather
	# than <Map> because this event is generated before the <Map> event,
	# which returns an error because the accessibility data has not been
	# initialized yet.
	bind TNotebook <<NotebookTabChanged>> {+::tk::accessible::_init \
						   %W \
						   Notebook \
						   Notebook \
						   Notebook \
						   [%W tab current -text] \
						   {} \
						   {}\
					       }

	bind all <Map> {+::tk::accessible::add_acc_object %W}

	# Various bindings to capture data/selection changes for
	# widgets that support returning a value.

	# Selection changes.
	bind Listbox <<ListboxSelect>> {+::tk::accessible::_updateselection %W}
	bind Treeview <<TreeviewSelect>> {+::tk::accessible::_updateselection %W}
	bind TCombobox <<ComboboxSelected>> {+::tk::accessible::_updateselection %W}
	bind Text <<Selection>> {+::tk::accessible::_updateselection %W}

	if {[tk windowingsystem] eq "x11"} {
	    # Automatically hook up new checkbuttons/radiobuttons
	    # to notify the accessibility system when their selection
	    #state changes.
	    bind Radiobutton   <Map> {+::tk::accessible::_attach_trace %W}
	    bind TRadiobutton  <Map> {+::tk::accessible::_attach_trace %W}
	    bind Checkbutton   <Map> {+::tk::accessible::_attach_trace %W}
	    bind TCheckbutton  <Map> {+::tk::accessible::_attach_trace %W}
	    bind Toggleswitch  <Map> {+::tk::accessible::_attach_trace %W}
	}

	# Capture value changes from scale widgets.
	bind Scale <Right> {+::tk::accessible::_updatescale %W Right}
	bind Scale <Left> {+::tk::accessible::_updatescale %W Left}
	bind TScale <Right> {+::tk::accessible::_updatescale %W Right}
	bind TScale <Left> {+::tk::accessible::_updatescale %W Left}

	# In some contexts, the accessibility API is confused about widget
	# roles because of the way the widget is constructed. For instance,
	# VoiceOver and Orca misread the ttk::spinbox as an entry because
	# of how it is constructed. In such cases, let's re-use an old trick
	# that we used with the Aqua scrollbar when the ttk widgets were first
	# developed - map the ttk widget to its classic equivalent. There may
	# be a visual conflict but it is more important that the AT be able
	# to correctly identify widget and its value.

	if {[tk windowingsystem] eq "aqua" || [tk windowingsystem] eq "x11"} {
	    set result [::tk::accessible::check_screenreader]
	    if {$result > 0} {
		interp alias {} ::ttk::spinbox {} ::tk::spinbox
	    }
	}
	if {[tk windowingsystem] eq "x11"} {
	    set result [::tk::accessible::check_screenreader]
	    if {$result > 0} {
		interp alias {} ::ttk::radiobutton {} ::tk::radiobutton
		interp alias {} ::ttk::checkbutton {} ::tk::checkbutton
		interp alias {} ::ttk::scale {} ::tk::scale

	    }
	}

	if {[tk windowingsystem] eq "win32"} {
	    set result [::tk::accessible::check_screenreader]
	    if {$result > 0} {
		interp alias {} ::ttk::radiobutton {} ::tk::radiobutton
		interp alias {} ::ttk::checkbutton {} ::tk::checkbutton
		interp alias {} ::ttk::spinbox {} ::tk::spinbox
	    }
	}

	# Capture value changes from spinbox widgets.
	bind Spinbox <Up> {+::tk::accessible::_updatescale %W Up}
	bind Spinbox <Down> {+::tk::accessible::_updatescale %W Down}
	bind TSpinbox <Up> {+::tk::accessible::_updatescale %W Up}
	bind TSpinbox <Down> {+::tk::accessible::_updatescale %W Down}

	# Capture notebook selection.
	bind TNotebook <Map> {+::ttk::notebook::enableTraversal %W}
	bind TNotebook <<NotebookTabChanged>> {+::tk::accessible::_updateselection %W}

	# Capture text selection in entry widgets.
	bind Entry <KeyPress> {+::tk::accessible::_updateselection %W}
	bind TEntry <KeyPress> {+::tk::accessible::_updateselection %W}
	bind Entry <Left> {+::tk::accessible::_updateselection %W}
	bind TEntry <Left> {+::tk::accessible::_updateselection %W}
	bind Entry <Right> {+::tk::accessible::_updateselection %W}
	bind TEntry <Right> {+::tk::accessible::_updateselection %W}
	bind Entry <<Selection>> {+::tk::accessible::_updateselection %W}
	bind TEntry <<Selection>> {+::tk::accessible::_updateselection %W}

	# Progressbar updates.
	bind TProgressbar <FocusIn> {+::tk::accessible::_updateselection %W}

	bind Scrollbar <Up> {+%W set [expr {[%W get] - 0.1}]; ::tk::accessible::emit_selection_change %W}
	bind Scrollbar <Down> {+%W set [expr {[%W get] + 0.1}]; ::tk::accessible::emit_selection_change %W}
	bind TScrollbar <Up> {+%W set [expr {[%W get] - 0.1}]; ::tk::accessible::emit_selection_change %W}
	bind TScrollbar <Down> {+%W set [expr {[%W get] + 0.1}]; ::tk::accessible::emit_selection_change %W}

	bind Dialog <Return> {+::tk::dialog::OK %W; ::tk::accessible::emit_selection_change %W}
	bind Dialog <Escape> {+::tk::dialog::Cancel %W; ::tk::accessible::emit_selection_change %W}

	# Help text for widgets that require additional direction
	# on keyboard navigation - these widgets will use standard keyboard
	# navigation when they obtain focus rather than the accessibility
	# keyboard shortcuts. We are mostly limiting the accessibility tree to one
	# level - toplevel window and child windows - to reduce the complexity of
	# the implementation, which is tied tighly to Tk windows. Component
	# elements of many widgets such listbox or treeview rows are not exposed as
	# Tk windows, and there is no simple way to expose them to the platforms'
	# accessibility API's directly, but they can be navigated via the keyboard
	# and their data (obtained via selection events) can be piped to the
	# screen reader for vocalization. The help text here assists the user
	# in switching to the standard keys for navigation as needed.

	bind Listbox <Map> {+::tk::accessible::set_acc_help %W "To navigate, click the mouse or trackpad and then use the standard Up-Arrow and Down-Arrow keys."}
	bind Treeview <Map> {+::tk::accessible::set_acc_help %W "To navigate, click the mouse or trackpad and then use the standard Up-Arrow and Down-Arrow keys. To open or close a tree node, click the Space key."}
	bind Entry <Map> {+::tk::accessible::set_acc_help %W "To navigate, click the mouse or trackpad and then use standard keyboard navigation. To hear the contents of the entry field, select all."}
	bind TEntry <Map> {+::tk::accessible::set_acc_help %W "To navigate, click the mouse or trackpad and then use standard keyboard navigation. To hear the contents of the entry field, select all."}
	bind Scale <Map> {+::tk::accessible::set_acc_help %W "Click the right or left arrows to move the scale."}
	bind TScale <Map> {+::tk::accessible::set_acc_help %W "Click the right or left arrows to move the scale."}
	bind Spinbox <Map> {+::tk::accessible::set_acc_help %W "Click the up or down arrows to change the value."}
	bind TSpinbox <Map> {+::tk::accessible::set_acc_help %W "Click the up or down arrows to change the value."}
	bind Canvas <Map> {+::tk::accessible::set_acc_help %W "The canvas widget is not accessible."}
	bind Scrollbar <Map> {+::tk::accessible::set_acc_help %W "Use the touchpad or mouse wheel to move the scrollbar."}
	bind TScrollbar <Map> {+::tk::accessible::set_acc_help %W "Use the touchpad or mouse wheel to move the scrollbar."}
	bind Menubutton <Map> {+::tk::accessible::set_acc_help %W "Use the touchpad or mouse wheel to pop up the menu."}
	bind TMenubutton <Map> {+::tk::accessible::set_acc_help %W "Use the touchpad or mouse wheel to pop up the menu."}
	bind TNotebook <Map> {+::tk::accessible::set_acc_help %W "Use the Tab and Right/Left arrow keys to navigate between notebook tabs."}
	bind Text <Map> {+::tk::accessible::set_acc_help %W "Use normal keyboard shortcuts to navigate the text widget."}

	if {[tk windowingsystem] eq "win32"} {
	    bind all <FocusIn> {+::tk::accessible::_forceTkFocus %W}
	}

	if {[tk windowingsystem] eq "x11"} {
	    bind Button <FocusIn> {+::tk::accessible::_getbuttontext %W}
	    bind TButton <FocusIn> {+::tk::accessible::_getbuttontext %W}
	}

	# Finally, export the main commands.
	namespace export set_acc_role set_acc_name set_acc_description set_acc_value set_acc_state set_acc_action set_acc_help get_acc_role get_acc_name get_acc_description get_acc_value get_acc_state get_acc_action get_acc_help add_acc_object emit_selection_change check_screenreader emit_focus_change
	namespace ensemble create
    }

}
# Add these commands to the tk command ensemble: tk accessible.
namespace ensemble configure tk -map \
    [dict merge [namespace ensemble configure tk -map] \
	 {accessible ::tk::accessible}]