Tk Source Code

Check-in [09ac3257]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add additional notifications
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | tka11y
Files: files | file ages | folders
SHA3-256: 09ac3257afdd7be9936e8192212620aa6b951ce583da492943f15356e2ec3ce8
User & Date: kevin_walzer 2025-07-26 22:28:34.655
Context
2025-07-26
22:35
Remove compiler errors check-in: 921c37ff user: kevin_walzer tags: tka11y
22:28
Add additional notifications check-in: 09ac3257 user: kevin_walzer tags: tka11y
18:20
Merge trunk check-in: a8e61816 user: kevin_walzer tags: tka11y
Changes
Unified Diff Ignore Whitespace Patch
Changes to unix/tkUnixAccessibility.c.
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
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_component_interface_init(AtkComponentIface *iface);
static void tk_atk_action_interface_init(AtkActionIface *iface);
static void tk_atk_value_interface_init(AtkValueIface *iface);
static gboolean tk_contains(AtkComponent *component, gint x, gint y, AtkCoordType coord_type);
static gchar *tk_get_text(AtkText *text);
static void tk_atk_text_interface_init(AtkTextIface *iface);

/* Lower-level functions providing integration between Atk objects and Tcl/Tk. */
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);
static void RegisterChildWidgets(Tcl_Interp *interp, Tk_Window tkwin, AtkObject *parent_obj);
static void RegisterToplevelWindow(Tcl_Interp *interp, Tk_Window tkwin, AtkObject *accessible);
AtkObject *TkCreateAccessibleAtkObject(Tcl_Interp *interp, Tk_Window tkwin, const char *path);
static void GtkEventLoop(ClientData clientData);
void InstallGtkEventLoop(void);
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);
static gboolean delayed_init(gpointer user_data);

/* Script-level commands and helper functions. */
static int EmitSelectionChanged(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]);
static int EmitFocusChanged(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]);
static int IsScreenReaderRunning(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]);
void TkAtkAccessible_RegisterEventHandlers(Tk_Window tkwin, void *tkAccessible);
static void TkAtkAccessible_DestroyHandler(ClientData clientData, XEvent *eventPtr);







<
















<







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
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_component_interface_init(AtkComponentIface *iface);
static void tk_atk_action_interface_init(AtkActionIface *iface);
static void tk_atk_value_interface_init(AtkValueIface *iface);
static gboolean tk_contains(AtkComponent *component, gint x, gint y, AtkCoordType coord_type);

static void tk_atk_text_interface_init(AtkTextIface *iface);

/* Lower-level functions providing integration between Atk objects and Tcl/Tk. */
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);
static void RegisterChildWidgets(Tcl_Interp *interp, Tk_Window tkwin, AtkObject *parent_obj);
static void RegisterToplevelWindow(Tcl_Interp *interp, Tk_Window tkwin, AtkObject *accessible);
AtkObject *TkCreateAccessibleAtkObject(Tcl_Interp *interp, Tk_Window tkwin, const char *path);
static void GtkEventLoop(ClientData clientData);
void InstallGtkEventLoop(void);
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);


/* Script-level commands and helper functions. */
static int EmitSelectionChanged(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]);
static int EmitFocusChanged(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]);
static int IsScreenReaderRunning(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]);
void TkAtkAccessible_RegisterEventHandlers(Tk_Window tkwin, void *tkAccessible);
static void TkAtkAccessible_DestroyHandler(ClientData clientData, XEvent *eventPtr);
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
			G_IMPLEMENT_INTERFACE(ATK_TYPE_ACTION, tk_atk_action_interface_init)
			G_IMPLEMENT_INTERFACE(ATK_TYPE_VALUE, tk_atk_value_interface_init))

/*
 * Map Atk component interface to Tk.
 */
 
    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;
    }







|







134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
			G_IMPLEMENT_INTERFACE(ATK_TYPE_ACTION, tk_atk_action_interface_init)
			G_IMPLEMENT_INTERFACE(ATK_TYPE_VALUE, tk_atk_value_interface_init))

/*
 * Map Atk component interface to Tk.
 */
 
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;
    }
254
255
256
257
258
259
260


261
262
263
264
265
266
267
 * Functions to map accessible role to Atk.
 */

static AtkRole GetAtkRoleForWidget(Tk_Window win)
{
    if (!win) return ATK_ROLE_UNKNOWN;



    Tcl_HashEntry *hPtr, *hPtr2;
    Tcl_HashTable *AccessibleAttributes;
    AtkRole role = ATK_ROLE_UNKNOWN;

    /* Check if we have accessibility attributes. */
    hPtr = Tcl_FindHashEntry(TkAccessibilityObject, (char *)win);
    if (hPtr) {







>
>







252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
 * Functions to map accessible role to Atk.
 */

static AtkRole GetAtkRoleForWidget(Tk_Window win)
{
    if (!win) return ATK_ROLE_UNKNOWN;

    AtkObject *obj = GetAtkObjectForTkWindow(win);

    Tcl_HashEntry *hPtr, *hPtr2;
    Tcl_HashTable *AccessibleAttributes;
    AtkRole role = ATK_ROLE_UNKNOWN;

    /* Check if we have accessibility attributes. */
    hPtr = Tcl_FindHashEntry(TkAccessibilityObject, (char *)win);
    if (hPtr) {
282
283
284
285
286
287
288



289
290
291
292
293
294
295
        }
    }

    /* Special case for toplevel windows. */
    if (Tk_IsTopLevel(win)) {
	role = ATK_ROLE_WINDOW;
    }



    return role;
}

static AtkRole tk_get_role(AtkObject *obj)
{
    TkAtkAccessible *acc = (TkAtkAccessible *)obj;
    Tk_Window win = acc->tkwin;







>
>
>







282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
        }
    }

    /* Special case for toplevel windows. */
    if (Tk_IsTopLevel(win)) {
	role = ATK_ROLE_WINDOW;
    }
    
    /* Notify system of role attribute. */
    g_object_notify(G_OBJECT(obj), "accessible-role");
    return role;
}

static AtkRole tk_get_role(AtkObject *obj)
{
    TkAtkAccessible *acc = (TkAtkAccessible *)obj;
    Tk_Window win = acc->tkwin;
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
    if (obj == tk_root_accessible) {
	/* Free old cached name, store new one. */
        g_free(acc->cached_name);
        acc->cached_name = g_strdup(name); 
    }
    atk_object_set_name(acc, name);
    g_object_notify(G_OBJECT(acc), "accessible-name");

}


static const gchar *tk_get_description(AtkObject *obj)
{
    TkAtkAccessible *acc = (TkAtkAccessible *)obj;

    if (!acc || !acc->tkwin) return NULL;

    Tcl_HashEntry *hPtr = Tcl_FindHashEntry(TkAccessibilityObject, (char *)acc->tkwin);
    if (!hPtr) return NULL;

    Tcl_HashTable *AccessibleAttributes = (Tcl_HashTable *)Tcl_GetHashValue(hPtr);
    if (!AccessibleAttributes) return NULL;

    Tcl_HashEntry *hPtr2 = Tcl_FindHashEntry(AccessibleAttributes, "description");
    if (!hPtr2) return NULL;

    const char *result = Tcl_GetString(Tcl_GetHashValue(hPtr2));
    if (result) {


        return g_strdup(result); /* ATK expects a newly allocated string. */
    }
    return NULL;
}

/*
 * Functions to map accessible value to Atk using







>




















>
>







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
    if (obj == tk_root_accessible) {
	/* Free old cached name, store new one. */
        g_free(acc->cached_name);
        acc->cached_name = g_strdup(name); 
    }
    atk_object_set_name(acc, name);
    g_object_notify(G_OBJECT(acc), "accessible-name");
    g_signal_emit_by_name(, "name-changed");
}


static const gchar *tk_get_description(AtkObject *obj)
{
    TkAtkAccessible *acc = (TkAtkAccessible *)obj;

    if (!acc || !acc->tkwin) return NULL;

    Tcl_HashEntry *hPtr = Tcl_FindHashEntry(TkAccessibilityObject, (char *)acc->tkwin);
    if (!hPtr) return NULL;

    Tcl_HashTable *AccessibleAttributes = (Tcl_HashTable *)Tcl_GetHashValue(hPtr);
    if (!AccessibleAttributes) return NULL;

    Tcl_HashEntry *hPtr2 = Tcl_FindHashEntry(AccessibleAttributes, "description");
    if (!hPtr2) return NULL;

    const char *result = Tcl_GetString(Tcl_GetHashValue(hPtr2));
    if (result) {
        g_object_notify(G_OBJECT(obj), "accessible-description");
        g_signal_emit_by_name(obj, "description-changed");
        return g_strdup(result); /* ATK expects a newly allocated string. */
    }
    return NULL;
}

/*
 * Functions to map accessible value to Atk using
469
470
471
472
473
474
475

476
477
478
479
480
481
482
        g_value_set_string(value, "");
        return;
    }

    char *result = Tcl_GetString(Tcl_GetHashValue(hPtr2));
    g_value_init(value, G_TYPE_STRING);
    g_value_set_string(value, result ? result : "");

}

static void tk_atk_value_interface_init(AtkValueIface *iface)
{
    iface->get_current_value = tk_get_current_value;
}








>







475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
        g_value_set_string(value, "");
        return;
    }

    char *result = Tcl_GetString(Tcl_GetHashValue(hPtr2));
    g_value_init(value, G_TYPE_STRING);
    g_value_set_string(value, result ? result : "");
    g_object_notify(G_OBJECT(atkObj), "accessible-value");
}

static void tk_atk_value_interface_init(AtkValueIface *iface)
{
    iface->get_current_value = tk_get_current_value;
}

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
            atk_state_set_add_state(set, ATK_STATE_SHOWING);
        }
        atk_state_set_add_state(set, ATK_STATE_FOCUSABLE);
    }
    return set;
}

/* Function to retrieve text from Tk widget. */

static gchar* tk_get_text(AtkText *text)
{
    TkAtkAccessible *acc = (TkAtkAccessible*)text;
    Tcl_Interp *interp = acc->interp;
    Tk_Window tkwin = acc->tkwin;
    const char *path = acc->path;

    if (!interp || !tkwin || !path) {
        g_warning("Invalid interp, tkwin, or path in tk_get_text");
        return g_strdup("");
    }

    AtkRole role = GetAtkRoleForWidget(tkwin);
    if (role != ATK_ROLE_ENTRY && role != ATK_ROLE_TEXT) {
        /* Only ENTRY and TEXT roles support text retrieval */
        g_warning("Text retrieval not supported for widget %s with role %d", path, role);
        return g_strdup("");
    }

    Tcl_DString cmd;
    Tcl_DStringInit(&cmd);
    if (role == ATK_ROLE_ENTRY) {
        Tcl_DStringAppend(&cmd, Tk_PathName(tkwin), -1);
        Tcl_DStringAppend(&cmd, " get", -1);
    } else if (role == ATK_ROLE_TEXT) {
        Tcl_DStringAppend(&cmd, "::tk::accessible::_gettext ", -1);
        Tcl_DStringAppend(&cmd, path, -1);
    }

    if (Tcl_Eval(interp, Tcl_DStringValue(&cmd)) != TCL_OK) {
        g_warning("Failed to execute text retrieval for %s: %s", path, Tcl_GetStringResult(interp));
        Tcl_DStringFree(&cmd);
        return g_strdup("");
    }

    const char *result = Tcl_GetStringResult(interp);
    gchar *ret = g_strdup(result && *result ? result : "");
    Tcl_DStringFree(&cmd);
    return ret;
}

static void tk_atk_text_interface_init(AtkTextIface *iface)
{
    iface->get_text = tk_get_text;
    /* Stub out methods that may cause assertions. */
    iface->get_text_at_offset = NULL;
    iface->get_range_extents = NULL;
    iface->get_character_count = NULL;
    /* Add other AtkText methods as needed, setting to NULL or safe implementations */
}

/*
 * Functions that implement actions (i.e. button press)
 * from Tk to Atk.
 */

static gboolean tk_action_do_action(AtkAction *action, gint i)







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







506
507
508
509
510
511
512




















































513
514
515
516
517
518
519
            atk_state_set_add_state(set, ATK_STATE_SHOWING);
        }
        atk_state_set_add_state(set, ATK_STATE_FOCUSABLE);
    }
    return set;
}






















































/*
 * Functions that implement actions (i.e. button press)
 * from Tk to Atk.
 */

static gboolean tk_action_do_action(AtkAction *action, gint i)
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
    if (self->tkwin) { 
	/*
	 * Only attempt removal if tkwin is still valid.
	 * Remove from toplevel list if it was a toplevel.
	 */
        if (Tk_IsTopLevel(self->tkwin)) {
            toplevel_accessible_objects = g_list_remove(toplevel_accessible_objects, self);
            /* 
	     * No need to unref here, as the object is being finalized,
	     * and the list only held a pointer, not an owning ref.
             * If the list *did* own a ref, it would be unreffed when removed from list.
	     */
        }
        /* Unregister from the Tk_Window to AtkObject map. */
        UnregisterAtkObjectForTkWindow(self->tkwin);
    }

    /* Free path and cached name. */







|


|







598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
    if (self->tkwin) { 
	/*
	 * Only attempt removal if tkwin is still valid.
	 * Remove from toplevel list if it was a toplevel.
	 */
        if (Tk_IsTopLevel(self->tkwin)) {
            toplevel_accessible_objects = g_list_remove(toplevel_accessible_objects, self);
	    /* 
	     * No need to unref here, as the object is being finalized,
	     * and the list only held a pointer, not an owning ref.
	     * If the list *did* own a ref, it would be unreffed when removed from list.
	     */
        }
        /* Unregister from the Tk_Window to AtkObject map. */
        UnregisterAtkObjectForTkWindow(self->tkwin);
    }

    /* Free path and cached name. */
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
    /* Ensure root exists. */
    if (!tk_root_accessible) {
        tk_root_accessible = tk_util_get_root();
    }

    /* Set proper parent-child relationship. */
    atk_object_set_parent(accessible, tk_root_accessible);


    /* 
     * Add to toplevel list if not already present.
     * The list now holds a non-owning reference. 
     * Ownership is with tk_to_atk_map. 
     */
    if (!g_list_find(toplevel_accessible_objects, accessible)) {
        toplevel_accessible_objects = g_list_append(toplevel_accessible_objects, accessible);

        /* 
	 * Critical: Emit children-changed signal for AT-SPI update.
	 * The index should be the position where it was added.
	 */
        int index = g_list_length(toplevel_accessible_objects) - 1;
        g_signal_emit_by_name(tk_root_accessible, "children-changed::add", index, accessible);
    }

    /* Explicitly set and notify accessible name */
    const gchar *name = tk_get_name(accessible); 
    if (name) {
        tk_set_name(accessible, name);


        g_free((gpointer)name); /* Free the string returned by tk_get_name. */
    }

    /* Register child widgets recursively. */
    RegisterChildWidgets(interp, tkwin, accessible);
}








>









|











>
>







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
    /* Ensure root exists. */
    if (!tk_root_accessible) {
        tk_root_accessible = tk_util_get_root();
    }

    /* Set proper parent-child relationship. */
    atk_object_set_parent(accessible, tk_root_accessible);
    g_object_notify(G_OBJECT(accessible), "accessible-parent");

    /* 
     * Add to toplevel list if not already present.
     * The list now holds a non-owning reference. 
     * Ownership is with tk_to_atk_map. 
     */
    if (!g_list_find(toplevel_accessible_objects, accessible)) {
        toplevel_accessible_objects = g_list_append(toplevel_accessible_objects, accessible);

	/* 
	 * Critical: Emit children-changed signal for AT-SPI update.
	 * The index should be the position where it was added.
	 */
        int index = g_list_length(toplevel_accessible_objects) - 1;
        g_signal_emit_by_name(tk_root_accessible, "children-changed::add", index, accessible);
    }

    /* Explicitly set and notify accessible name */
    const gchar *name = tk_get_name(accessible); 
    if (name) {
        tk_set_name(accessible, name);
        g_object_notify(G_OBJECT(accessible), "accessible-name");
        g_signal_emit_by_name(, "name-changed");
        g_free((gpointer)name); /* Free the string returned by tk_get_name. */
    }

    /* Register child widgets recursively. */
    RegisterChildWidgets(interp, tkwin, accessible);
}

750
751
752
753
754
755
756

757
758
759
760
761
762
763


764
765
766
767
768
769
770
        if (current_parent != parent_obj) {
	    /* 
             * If the child was previously parented to something else or unparented,
             * emit a remove signal from the old parent first if known.
	     * For simplicity, we'll just set the parent and emit 'add' for the new parent.
	     */
            atk_object_set_parent(child_obj, parent_obj);

            g_signal_emit_by_name(parent_obj, "children-changed::add", index, child_obj);
        }

        /* Set the name for the child object. */
        const gchar *child_name = tk_get_name(child_obj);
        if (child_name) {
            tk_set_name(child_obj, child_name);


            g_free((gpointer)child_name);
        }

        /* Recursively register children. */
        RegisterChildWidgets(interp, child, child_obj);
        index++;
    }







>







>
>







708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
        if (current_parent != parent_obj) {
	    /* 
             * If the child was previously parented to something else or unparented,
             * emit a remove signal from the old parent first if known.
	     * For simplicity, we'll just set the parent and emit 'add' for the new parent.
	     */
            atk_object_set_parent(child_obj, parent_obj);
	    g_object_notify(G_OBJECT(child_obj), "accessible-parent");
            g_signal_emit_by_name(parent_obj, "children-changed::add", index, child_obj);
        }

        /* Set the name for the child object. */
        const gchar *child_name = tk_get_name(child_obj);
        if (child_name) {
            tk_set_name(child_obj, child_name);
            g_object_notify(G_OBJECT(child_obj), "accessible-name");
            g_signal_emit_by_name(child_obj, "name-changed");
            g_free((gpointer)child_name);
        }

        /* Recursively register children. */
        RegisterChildWidgets(interp, child, child_obj);
        index++;
    }
787
788
789
790
791
792
793


794
795


796
797
798
799
800
801
802
    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 application name. */
	atk_object_set_role(tk_root_accessible, ATK_ROLE_APPLICATION);


        /* Set an initial name for the root, can be updated later. */
        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) {







>
>


>
>







748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
    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 application name. */
	atk_object_set_role(tk_root_accessible, ATK_ROLE_APPLICATION);
        g_object_notify(G_OBJECT(tk_root_accessible), "accessible-role");

        /* Set an initial name for the root, can be updated later. */
        tk_set_name(tk_root_accessible, "Tk Application");
        g_object_notify(G_OBJECT(tk_root_accessible), "accessible-name");
        g_signal_emit_by_name(, "name-changed");
    }

    return tk_root_accessible;
}

/* Core function linking Tk objects to the Atk root object and at-spi. */
AtkObject *atk_get_root(void) {
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
    acc->interp = interp;
    acc->tkwin = tkwin;
    acc->path = g_strdup(path); 

    /* Set initial accessibility properties (role and name). */
    AtkObject *obj = ATK_OBJECT(acc);
    atk_object_set_role(obj, GetAtkRoleForWidget(tkwin));


    /* Initial name setting for the object. */
    const gchar *name = tk_get_name(obj);
    if (name) {
        tk_set_name(obj, name);


        g_free((gpointer)name); /* Free the string returned by tk_get_name. */
    }

    /* Set up parent-child relationships for the widget. */
    if (tkwin) {
        Tk_Window parent_tkwin = Tk_Parent(tkwin);
        AtkObject *parent_obj = NULL;

        if (parent_tkwin) {
            parent_obj = GetAtkObjectForTkWindow(parent_tkwin);
        } else {
            /* If no Tk parent, it's a toplevel, parent it to the root accessible. */
            parent_obj = tk_root_accessible;
        }

        if (parent_obj) {
            atk_object_set_parent(obj, parent_obj);

	    /* 
	     * Emit children-changed signal for the parent to update AT-SPI.
	     * The index here is an approximation; a more precise index would require
	     * knowing the exact position in the parent's child list.
	     */
            g_signal_emit_by_name(parent_obj, "children-changed::add", -1, obj);
        }







>





>
>

















>







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
    acc->interp = interp;
    acc->tkwin = tkwin;
    acc->path = g_strdup(path); 

    /* Set initial accessibility properties (role and name). */
    AtkObject *obj = ATK_OBJECT(acc);
    atk_object_set_role(obj, GetAtkRoleForWidget(tkwin));
    g_object_notify(G_OBJECT(obj), "accessible-role");

    /* Initial name setting for the object. */
    const gchar *name = tk_get_name(obj);
    if (name) {
        tk_set_name(obj, name);
        g_object_notify(G_OBJECT(obj), "accessible-name");
        g_signal_emit_by_name(obj, "name-changed");
        g_free((gpointer)name); /* Free the string returned by tk_get_name. */
    }

    /* Set up parent-child relationships for the widget. */
    if (tkwin) {
        Tk_Window parent_tkwin = Tk_Parent(tkwin);
        AtkObject *parent_obj = NULL;

        if (parent_tkwin) {
            parent_obj = GetAtkObjectForTkWindow(parent_tkwin);
        } else {
            /* If no Tk parent, it's a toplevel, parent it to the root accessible. */
            parent_obj = tk_root_accessible;
        }

        if (parent_obj) {
            atk_object_set_parent(obj, parent_obj);
            g_object_notify(G_OBJECT(obj), "accessible-parent");
	    /* 
	     * Emit children-changed signal for the parent to update AT-SPI.
	     * The index here is an approximation; a more precise index would require
	     * knowing the exact position in the parent's child list.
	     */
            g_signal_emit_by_name(parent_obj, "children-changed::add", -1, obj);
        }
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
void UnregisterAtkObjectForTkWindow(Tk_Window tkwin)
{
    if (tk_to_atk_map && tkwin) { 
	g_hash_table_remove(tk_to_atk_map, tkwin); /* g_object_unref will be called by hash table. */
    }
}

/*Helper function for screen reader initialization. */
static gboolean delayed_init(gpointer user_data) 
{
    Tcl_Interp *interp = (Tcl_Interp *)user_data; 

    if (!tk_root_accessible) return FALSE;

    /* Re-emit all structure signals for Orca. */
    g_signal_emit_by_name(tk_root_accessible, "children-changed", 0, NULL);

    /* Notify that the application is now fully accessible. */
    g_signal_emit_by_name(tk_root_accessible, "state-change", "enabled", TRUE);
    g_signal_emit_by_name(tk_root_accessible, "state-change", "sensitive", TRUE);

    return FALSE;
}

/*
 *----------------------------------------------------------------------
 *
 * EmitSelectionChanged --
 *
 * Accessibility system notification when selection changed.
 *







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







904
905
906
907
908
909
910

















911
912
913
914
915
916
917
void UnregisterAtkObjectForTkWindow(Tk_Window tkwin)
{
    if (tk_to_atk_map && tkwin) { 
	g_hash_table_remove(tk_to_atk_map, tkwin); /* g_object_unref will be called by hash table. */
    }
}


















/*
 *----------------------------------------------------------------------
 *
 * EmitSelectionChanged --
 *
 * Accessibility system notification when selection changed.
 *
995
996
997
998
999
1000
1001

1002
1003
1004
1005
1006
1007
1008
        return TCL_ERROR;
    }

    AtkRole role = atk_object_get_role(acc);

    GValue gval = G_VALUE_INIT;
    tk_get_current_value(ATK_VALUE(acc), &gval);

    g_signal_emit_by_name(G_OBJECT(acc), "value-changed", &gval);
    g_value_unset(&gval); /* Unset the GValue to free any allocated memory. */

    if (role == ATK_ROLE_TEXT || role == ATK_ROLE_ENTRY) {
        g_signal_emit_by_name(acc, "text-selection-changed");
    }
   







>







947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
        return TCL_ERROR;
    }

    AtkRole role = atk_object_get_role(acc);

    GValue gval = G_VALUE_INIT;
    tk_get_current_value(ATK_VALUE(acc), &gval);
    g_object_notify(G_OBJECT(acc), "accessible-value");
    g_signal_emit_by_name(G_OBJECT(acc), "value-changed", &gval);
    g_value_unset(&gval); /* Unset the GValue to free any allocated memory. */

    if (role == ATK_ROLE_TEXT || role == ATK_ROLE_ENTRY) {
        g_signal_emit_by_name(acc, "text-selection-changed");
    }
   
1183
1184
1185
1186
1187
1188
1189


1190
1191
1192
1193
1194
1195
1196

    AtkObject *atk_obj = (AtkObject*) tkAccessible;
    if (atk_obj) {
	/* Get the current name and then set it to trigger notification. */
	const gchar *name = tk_get_name(atk_obj);
	if (name) {
	    tk_set_name(atk_obj, name);


	    g_free((gpointer)name); /* Free the string returned by tk_get_name. */
	}
    }
}


/*







>
>







1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151

    AtkObject *atk_obj = (AtkObject*) tkAccessible;
    if (atk_obj) {
	/* Get the current name and then set it to trigger notification. */
	const gchar *name = tk_get_name(atk_obj);
	if (name) {
	    tk_set_name(atk_obj, name);
	    g_object_notify(G_OBJECT(atk_obj), "accessible-name");
	    g_signal_emit_by_name(_obj, "name-changed");
	    g_free((gpointer)name); /* Free the string returned by tk_get_name. */
	}
    }
}


/*
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
    /* Set environment variables for proper AT-SPI operation. */
    g_setenv("GTK_MODULES", "gail:atk-bridge", FALSE);
    g_setenv("NO_AT_BRIDGE", "0", FALSE);

    /* Initialize Glib type system first. */
    g_type_init();

    /* Initialize AT-SPI bridge. */
    if (atk_bridge_adaptor_init(NULL, NULL) != 0) {
	g_warning("Failed to initialize AT-SPI bridge\n");
	return TCL_ERROR;
    }

    /* Create and configure root object. */
    tk_root_accessible = tk_util_get_root();
    if (tk_root_accessible) {
	const gchar *name = tk_get_name(tk_root_accessible);
	if (name) {
	    tk_set_name(tk_root_accessible, name); /* Set the name and notify. */


	    g_free((gpointer)name); /* Free the string returned by tk_get_name. */
	}
    }







    /* Initialize mapping table. */
    InitAtkTkMapping();

    /* Register main window with root. */
    Tk_Window mainWin = Tk_MainWindow(interp);
    if (mainWin) {







<
<
<
<
<
<






>
>



>
>
>
>
>
>







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
    /* Set environment variables for proper AT-SPI operation. */
    g_setenv("GTK_MODULES", "gail:atk-bridge", FALSE);
    g_setenv("NO_AT_BRIDGE", "0", FALSE);

    /* Initialize Glib type system first. */
    g_type_init();







    /* Create and configure root object. */
    tk_root_accessible = tk_util_get_root();
    if (tk_root_accessible) {
	const gchar *name = tk_get_name(tk_root_accessible);
	if (name) {
	    tk_set_name(tk_root_accessible, name); /* Set the name and notify. */
	    g_object_notify(G_OBJECT(tk_root_accessible), "accessible-name");
	    g_signal_emit_by_name(tk_root_accessible, "name-changed");
	    g_free((gpointer)name); /* Free the string returned by tk_get_name. */
	}
    }

    /* Initialize AT-SPI bridge. */
    if (atk_bridge_adaptor_init(NULL, NULL) != 0) {
	g_warning("Failed to initialize AT-SPI bridge\n");
	return TCL_ERROR;
    }

    /* Initialize mapping table. */
    InitAtkTkMapping();

    /* Register main window with root. */
    Tk_Window mainWin = Tk_MainWindow(interp);
    if (mainWin) {
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
    while (g_main_context_pending(NULL) && iterations < 100) {
	iterations++;
    }

    /* Install event loop integration. */
    InstallGtkEventLoop();

    /* 
     * Add delay for screen reader connection.
     * Pass the interp to delayed_init for potential future use, 
     * though not used in current delayed_init.
     */
    if (IsScreenReaderRunning(NULL, interp, 0, NULL)) {
	g_timeout_add(500, (GSourceFunc)delayed_init, interp);
    } else {
	delayed_init(interp); /* Call directly if no screen reader. */
    }

    /* 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);

    /* 
     * Force initial hierarchy update. This is often done by delayed_init, 
     * but can be here too for immediate setup.
     */
    g_signal_emit_by_name(tk_root_accessible, "children-changed", 0, NULL);

    return TCL_OK;
}

#else







<
<
<
<
<
<
<
<
<
<
<











|
<







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
    while (g_main_context_pending(NULL) && iterations < 100) {
	iterations++;
    }

    /* Install event loop integration. */
    InstallGtkEventLoop();












    /* 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);

    /* 
     * Force initial hierarchy update. 

     */
    g_signal_emit_by_name(tk_root_accessible, "children-changed", 0, NULL);

    return TCL_OK;
}

#else