Tk Source Code

Artifact [46156399]
Login

Artifact 461563996011f51483cad914f58b00c57e497194dfd52fed29001120bcc7949b:

Attachment "x11-accessibility-combined.patch" to ticket [74d973b4] added by 0xjnml 2026-10-04 14:07:06. (unpublished)
--- a/unix/tkUnixAccessibility.c
+++ b/unix/tkUnixAccessibility.c
@@ -44,6 +44,7 @@
 } roleMap[] = {
     {"Button", ATK_ROLE_PUSH_BUTTON},
     {"Checkbox", ATK_ROLE_CHECK_BOX},
+    {"Checkbutton", ATK_ROLE_CHECK_BOX},	/* The role accessibility.tcl sets. */
     {"Combobox", ATK_ROLE_COMBO_BOX},
     {"Entry", ATK_ROLE_ENTRY},
     {"Label", ATK_ROLE_LABEL},
@@ -71,6 +72,18 @@
 
 /* Variables for managing ATK objects. */
 static AtkObject *tk_root_accessible = NULL;
+
+/*
+ * The main window, for naming the application object by [tk appname]; reset
+ * when the main window is destroyed.
+ */
+static Tk_Window mainWindow = NULL;
+static void MainWindowEventProc(TCL_UNUSED(void *), XEvent *eventPtr)
+{
+    if (eventPtr->type == DestroyNotify) {
+	mainWindow = NULL;
+    }
+}
 static GList *toplevel_accessible_objects = NULL;
 static GHashTable *tk_to_atk_map = NULL;
 extern Tcl_HashTable *TkAccessibilityObject;
@@ -304,6 +317,28 @@
 	    y >= comp_y && y < comp_y + comp_height);
 }
 
+/*
+ * "active-descendant-changed" is meant for containers that manage their own
+ * items (lists, trees, tables, menus). Emitted from an ordinary parent such as
+ * a frame on every focus change, it makes screen readers (Orca) present the
+ * focused widget a second time, right after the focus event itself.
+ */
+static gboolean ParentManagesDescendants(AtkObject *parent)
+{
+    switch (atk_object_get_role(parent)) {
+    case ATK_ROLE_LIST:
+    case ATK_ROLE_LIST_BOX:
+    case ATK_ROLE_TREE:
+    case ATK_ROLE_TREE_TABLE:
+    case ATK_ROLE_TABLE:
+    case ATK_ROLE_MENU:
+    case ATK_ROLE_MENU_BAR:
+	return TRUE;
+    default:
+	return FALSE;
+    }
+}
+
 /* Force accessible focus on a Tk widget. */
 static gboolean tk_grab_focus(AtkComponent *component)
 {
@@ -327,7 +362,9 @@
    AtkObject *parent = atk_object_get_parent(obj);
    if (parent) {
        /* Notify parent about active descendant */
-       g_signal_emit_by_name(parent, "active-descendant-changed", obj);
+       if (ParentManagesDescendants(parent)) {
+	   g_signal_emit_by_name(parent, "active-descendant-changed", obj);
+       }
 
        /* Also emit children-changed to ensure ATK hierarchy is refreshed. */
        g_signal_emit_by_name(parent, "children-changed::add",
@@ -484,28 +521,53 @@
     if (!win) return NULL;
 
     AtkRole role = GetAtkRoleForWidget(win);
+
+    /*
+     * The Windows and macOS bridges present the "description" attribute as
+     * the accessible name (accName, accessibilityLabel) and "help" as the
+     * hint; the class bindings in accessibility.tcl put the visible text in
+     * "description" and the role word in "name". Do the same here, so a
+     * button reads "Save, push button" rather than "Button, push button,
+     * Save". "name" remains the fallback (toplevels keep their title there).
+     */
+    Tcl_HashEntry *hPtr = Tcl_FindHashEntry(TkAccessibilityObject, (char *)win);
+    Tcl_HashTable *attrs = hPtr ? (Tcl_HashTable *)Tcl_GetHashValue(hPtr) : NULL;
+    if (attrs) {
+	Tcl_HashEntry *descEntry = Tcl_FindHashEntry(attrs, "description");
+	if (descEntry) {
+	    const char *desc = Tcl_GetString((Tcl_Obj *)Tcl_GetHashValue(descEntry));
+	    if (desc && desc[0]) return g_utf8_make_valid(desc, -1);
+	}
+    }
+
     /* 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_Obj *)Tcl_GetHashValue(nameEntry));
-    return name ? g_utf8_make_valid(name, -1) : NULL;
+    if (!name) return NULL;
+
+    /*
+     * The class bindings default "name" to the role word ("Entry", "Scale");
+     * the screen reader announces the role itself, so that is no name.
+     */
+    Tcl_HashEntry *roleEntry = Tcl_FindHashEntry(attrs, "role");
+    if (roleEntry && strcmp(name, Tcl_GetString((Tcl_Obj *)Tcl_GetHashValue(roleEntry))) == 0) {
+	return NULL;
+    }
+    return g_utf8_make_valid(name, -1);
 }
 
 static const gchar *tk_get_name(AtkObject *obj)
 {
     if (obj == tk_root_accessible) {
-	return "Tk Application";
+	return mainWindow ? Tk_Name(mainWindow) : "Tk Application";
     }
 
     TkAtkAccessible *acc = (TkAtkAccessible *)obj;
@@ -532,7 +594,8 @@
     Tcl_HashTable *attrs = (Tcl_HashTable *)Tcl_GetHashValue(hPtr);
     if (!attrs) return NULL;
 
-    Tcl_HashEntry *descriptionEntry = Tcl_FindHashEntry(attrs, "description");
+    /* The ATK description is the hint: "help", as on Windows and macOS. */
+    Tcl_HashEntry *descriptionEntry = Tcl_FindHashEntry(attrs, "help");
     if (!descriptionEntry) return NULL;
 
     const char *description = Tcl_GetString((Tcl_Obj *)Tcl_GetHashValue(descriptionEntry));
@@ -633,10 +696,13 @@
 	    const char *value = GetAtkValueForWidget(acc->tkwin);
 	    /* Check for proper state values. */
 	    if (value) {
-		/* For checkboxes/radiobuttons, check if value equals "selected" or "1" or onvalue. */
+		/*
+		 * The class bindings store "selected" / "not selected" (an
+		 * application may store "1" / "0"); the same test as the
+		 * state-changed event sent by EmitSelectionChanged.
+		 */
 		if (strcmp(value, "selected") == 0 ||
-		    strcmp(value, "1") == 0 ||
-		    (value[0] != '0' && value[0] != '\0')) {
+		    strcmp(value, "1") == 0) {
 		    atk_state_set_add_state(state_set, ATK_STATE_CHECKED);
 		}
 	    }
@@ -1106,6 +1172,7 @@
 
     if (Tk_IsTopLevel(tkwin)) {
 	parentAcc = tk_root_accessible;
+	atk_object_set_parent(accessible, parentAcc);
 
 	if (!g_list_find(toplevel_accessible_objects, accessible)) {
 	    toplevel_accessible_objects = g_list_append(toplevel_accessible_objects, accessible);
@@ -1256,7 +1323,9 @@
 		Tk_Window parent = Tk_Parent(tkwin);
 		AtkObject *parentAc = GetAtkObjectForTkWindow(parent);
 		if (parentAc) {
-		    g_signal_emit_by_name(parentAc, "active-descendant-changed", acc);
+		    if (ParentManagesDescendants(parentAc)) {
+			g_signal_emit_by_name(parentAc, "active-descendant-changed", acc);
+		    }
 		}
 	    }
 	}
@@ -1364,7 +1433,9 @@
 		/* Notify parent about active descendant. */
 		AtkObject *childAcc = GetAtkObjectForTkWindow(current);
 		if (childAcc) {
-		    g_signal_emit_by_name(parentAcc, "active-descendant-changed", childAcc);
+		    if (ParentManagesDescendants(parentAcc)) {
+			g_signal_emit_by_name(parentAcc, "active-descendant-changed", childAcc);
+		    }
 		}
 
 		/* Also emit children-changed to refresh ATK's view. */
@@ -1669,7 +1740,9 @@
 	if (role != ATK_ROLE_WINDOW) {
 	    AtkObject *parent = atk_object_get_parent(obj);
 	    if (parent) {
-		g_signal_emit_by_name(parent, "active-descendant-changed", obj);
+		if (ParentManagesDescendants(parent)) {
+		    g_signal_emit_by_name(parent, "active-descendant-changed", obj);
+		}
 
 		/* Also emit children-changed to refresh ATK's view. */
 		g_signal_emit_by_name(parent, "children-changed::add",
@@ -1959,6 +2032,8 @@
 	Tcl_SetResult(interp, "Failed to get main window", TCL_STATIC);
 	return TCL_ERROR;
     }
+    mainWindow = mainWin;
+    Tk_CreateEventHandler(mainWin, StructureNotifyMask, MainWindowEventProc, NULL);
 
     AtkObject *main_acc = TkCreateAccessibleAtkObject(interp, mainWin, Tk_PathName(mainWin));
     if (!main_acc) {
--- a/library/accessibility.tcl
+++ b/library/accessibility.tcl
@@ -653,15 +653,14 @@
 	    }
 	}
 
-	# 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.
+	# Set the class default accessible attributes; runs on every <Map>
+	# (and <<NotebookTabChanged>>), so defaults such as the current text
+	# are refreshed. An attribute the application has set itself is left
+	# alone: _init overwrites only attributes that are unset or still hold
+	# the value _init set last time.
+	variable _initValues
 	proc _init {w role name description value state action} {
-	    if {[catch {::tk::accessible::get_acc_role $w} msg]} {
-		if {$msg == $role} {
-		    return
-		}
-	    }
+	    variable _initValues
 	    if {[tk windowingsystem] ne "aqua"} {
 		# This is necessary to ensure correct accessible keyboard navigation
 		if [winfo exists $w] {
@@ -669,14 +668,19 @@
 		}
 	    }
 
-	    ::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
-
+	    # Role first: the other set_acc_* commands require it.
+	    foreach {attr new} [list role $role name $name \
+		    description $description value $value \
+		    state $state action $action] {
+		if {[catch {::tk::accessible::get_acc_$attr $w} current]
+			|| ([info exists _initValues($w,$attr)]
+			&& $current eq $_initValues($w,$attr))} {
+		    ::tk::accessible::set_acc_$attr $w $new
+		    set _initValues($w,$attr) $new
+		}
+	    }
 	}
+	bind all <Destroy> {+array unset ::tk::accessible::_initValues [string map {* \\* ? \\? [ \\[ ] \\]} %W],*}
 
 	# Toplevel bindings.
 	bind Toplevel <Map> {+::tk::accessible::_init \
@@ -734,7 +738,7 @@
 			       %W \
 			       Canvas \
 			       Canvas \
-			       Canvas \
+			       {} \
 			       {} \
 			       {} \
 			       {}\
@@ -746,7 +750,7 @@
 				    Checkbutton \
 				    Checkbutton \
 				    [%W cget -text] \
-				    [set [%W cget -variable]] \
+				    [::tk::accessible::_getcheckdata %W] \
 				    [%W cget -state] \
 				    {%W invoke}\
 				}
@@ -755,7 +759,7 @@
 				     Checkbutton \
 				     Checkbutton \
 				     [%W cget -text] \
-				     [set [%W cget -variable]] \
+				     [::tk::accessible::_getcheckdata %W] \
 				     [%W cget -state] \
 				     {%W invoke}\
 				 }
@@ -763,7 +767,7 @@
 				     %W \
 				     Toggleswitch \
 				     Toggleswitch \
-				     Toggleswitch \
+				     {} \
 				     [%W switchstate] \
 				     {} \
 				     {%W toggle}\
@@ -774,7 +778,7 @@
 				  %W \
 				  Combobox \
 				  Combobox \
-				  Combobox \
+				  {} \
 				  [%W get] \
 				  [%W cget -state] \
 				  {} \
@@ -796,7 +800,7 @@
 			      %W \
 			      Entry \
 			      Entry \
-			      Entry \
+			      {} \
 			      [%W get] \
 			      [%W cget -state] \
 			      {} \
@@ -807,7 +811,7 @@
 			       %W \
 			       Entry \
 			       Entry \
-			       Entry \
+			       {} \
 			       [%W get] \
 			       [%W state]\
 			       {} \
@@ -819,7 +823,7 @@
 				%W \
 				Listbox \
 				Listbox \
-				Listbox \
+				{} \
 				[%W get [%W curselection]] \
 				[%W cget -state]\
 				{%W invoke}\
@@ -830,7 +834,7 @@
 				     %W \
 				     Progressbar \
 				     Progressbar \
-				     Progressbar \
+				     {} \
 				     [::tk::accessible::_getpbvalue %W] \
 				     [%W state] \
 				     {}\
@@ -842,7 +846,7 @@
 				    Radiobutton \
 				    Radiobutton \
 				    [%W cget -text] \
-				    [%W cget -variable] \
+				    [::tk::accessible::_getradiodata %W] \
 				    [%W cget -state] \
 				    {%W invoke}\
 				}
@@ -851,7 +855,7 @@
 				     Radiobutton \
 				     Radiobutton \
 				     [%W cget -text] \
-				     [%W cget -variable] \
+				     [::tk::accessible::_getradiodata %W] \
 				     [%W cget -state] \
 				     {%W invoke}\
 				 }
@@ -861,7 +865,7 @@
 			      %W \
 			      Scale \
 			      Scale \
-			      Scale \
+			      {} \
 			      [%W get] \
 			      [%W cget -state]\
 			      {%W set}\
@@ -870,7 +874,7 @@
 			       %W \
 			       Scale \
 			       Scale \
-			       Scale \
+			       {} \
 			       [%W get] \
 			       [%W cget -state] \
 			       {%W set} \
@@ -1037,7 +1041,7 @@
 				  %W \
 				  Scrollbar \
 				  Scrollbar \
-				  Scrollbar \
+				  {} \
 				  {} \
 				  {} \
 				  {}\
@@ -1046,7 +1050,7 @@
 				   %W \
 				   Scrollbar \
 				   Scrollbar \
-				   Scrollbar \
+				   {} \
 				   {} \
 				   {} \
 				   {}\
@@ -1057,7 +1061,7 @@
 				%W \
 				Spinbox \
 				Spinbox \
-				Spinbox \
+				{} \
 				[%W get] \
 				[%W cget -state] \
 				{%W cget -command}\
@@ -1066,7 +1070,7 @@
 				 %W \
 				 Spinbox \
 				 Spinbox \
-				 Spinbox \
+				 {} \
 				 [%W get] \
 				 [%W state] \
 				 {%W cget -command}\
@@ -1089,7 +1093,7 @@
 			     %W \
 			     Text \
 			     Text \
-			     Text \
+			     {} \
 			     [::tk::accessible::_gettext %W] \
 			     [%W cget -state] \
 			     {}\
@@ -1258,16 +1262,20 @@
 	# 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."}
+	# On X11 these become the ATK description, which Orca reads on every
+	# focus change; the instructions are for mouse and trackpad users.
+	if {[tk windowingsystem] ne "x11"} {
+	    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."}