Tk Source Code

Check-in [24e3fe28]
Login

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

Overview
Comment:Better integration of hash table search keys
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | tka11y
Files: files | file ages | folders
SHA3-256: 24e3fe2894a7db271b559fc655510675e33f2743135e542f697a4755b6a38237
User & Date: kevin_walzer 2025-08-15 00:24:27.959
Context
2025-08-15
00:25
Fix typo check-in: 600ab331 user: kevin_walzer tags: tka11y
00:24
Better integration of hash table search keys check-in: 24e3fe28 user: kevin_walzer tags: tka11y
2025-08-14
01:55
More revisions check-in: afe54edc user: kevin_walzer tags: tka11y
Changes
Unified Diff Ignore Whitespace Patch
Changes to unix/tkUnixAccessibility.c.
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
    gchar *cached_name;
    gchar *cached_description;
    gchar *cached_value;
    AtkRole cached_role;
    gint x, y, width, height;
    gboolean is_mapped;
    gboolean has_focus;
    GList *children;  /* Track child accessible objects */
} TkAtkAccessible;

typedef struct _TkAtkAccessibleClass {
    AtkObjectClass parent_class;
} TkAtkAccessibleClass;


/* Structs for passing data to main thread. */
typedef struct {
    gpointer (*func)(gpointer);
    gpointer user_data;
    gpointer result;
    gboolean done;
    GMutex mutex;
    GCond cond;
} TkMainThreadCall;

typedef struct {
    Tk_Window tkwin;
    Tcl_Interp *interp;
    gpointer result;
    const char *windowName;

} MainThreadData;



/* Structs to map Tk roles into Atk roles. */

typedef struct AtkRoleMap {
    const char *tkrole;
    AtkRole atkrole;







|


















|
|
|
|
>

>







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
    gchar *cached_name;
    gchar *cached_description;
    gchar *cached_value;
    AtkRole cached_role;
    gint x, y, width, height;
    gboolean is_mapped;
    gboolean has_focus;
    GList *children;  /* Track child accessible objects. */
} TkAtkAccessible;

typedef struct _TkAtkAccessibleClass {
    AtkObjectClass parent_class;
} TkAtkAccessibleClass;


/* Structs for passing data to main thread. */
typedef struct {
    gpointer (*func)(gpointer);
    gpointer user_data;
    gpointer result;
    gboolean done;
    GMutex mutex;
    GCond cond;
} TkMainThreadCall;

typedef struct {
Tk_Window tkwin;
Tcl_Interp *interp;
gpointer result;
const char *windowName;
const char *key;
} MainThreadData;



/* Structs to map Tk roles into Atk roles. */

typedef struct AtkRoleMap {
    const char *tkrole;
    AtkRole atkrole;
163
164
165
166
167
168
169

170
171
172
173
174
175
176
static gpointer find_hash_entry_main(gpointer data);
static gpointer get_hash_value_main(gpointer data);
static gpointer find_hash_entry_by_key_main(gpointer data);
static gpointer get_hash_string_value_main(gpointer data);
static gpointer name_to_window_main(gpointer data);
static gpointer get_main_window_main(gpointer data);
static gpointer get_window_handle_main(gpointer data);

static gpointer get_root_coords_main(gpointer data);

/* Signal emission helpers. */
static gpointer emit_children_changed_add(gpointer data);
static gpointer emit_children_changed_remove(gpointer data);
static gboolean emit_value_changed(gpointer data);
static gboolean emit_text_selection_changed(gpointer data);







>







165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
static gpointer find_hash_entry_main(gpointer data);
static gpointer get_hash_value_main(gpointer data);
static gpointer find_hash_entry_by_key_main(gpointer data);
static gpointer get_hash_string_value_main(gpointer data);
static gpointer name_to_window_main(gpointer data);
static gpointer get_main_window_main(gpointer data);
static gpointer get_window_handle_main(gpointer data);
/* Added: New helper for root/screen coordinates to fix extents */
static gpointer get_root_coords_main(gpointer data);

/* Signal emission helpers. */
static gpointer emit_children_changed_add(gpointer data);
static gpointer emit_children_changed_remove(gpointer data);
static gboolean emit_value_changed(gpointer data);
static gboolean emit_text_selection_changed(gpointer data);
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
/* Cache update functions. */
static void UpdateGeometryCache(TkAtkAccessible *acc);
static void UpdateNameCache(TkAtkAccessible *acc);
static void UpdateDescriptionCache(TkAtkAccessible *acc);
static void UpdateValueCache(TkAtkAccessible *acc);
static void UpdateRoleCache(TkAtkAccessible *acc);
static void UpdateStateCache(TkAtkAccessible *acc);


/* Event handlers. */
void TkAtkAccessible_RegisterEventHandlers(Tk_Window tkwin, void *tkAccessible);
static void TkAtkAccessible_DestroyHandler(ClientData clientData, XEvent *eventPtr);
static void TkAtkAccessible_ConfigureHandler(ClientData clientData, XEvent *eventPtr);
static void TkAtkAccessible_MapHandler(ClientData clientData, XEvent *eventPtr);
static void TkAtkAccessible_UnmapHandler(ClientData clientData, XEvent *eventPtr);
static void TkAtkAccessible_FocusHandler(ClientData clientData, XEvent *eventPtr);


/* Tcl command implementations. */
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[]);
int TkAtkAccessibleObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]);
int TkAtkAccessibility_Init(Tcl_Interp *interp);

/* GLib-Tcl event loop integration */
static void SetupGlibIntegration(void);
static void ProcessPendingEvents(ClientData clientData);

/* Child management functions */
static void AddChildToParent(TkAtkAccessible *parent, AtkObject *child);
static void RemoveChildFromParent(TkAtkAccessible *parent, AtkObject *child);


/* Define custom Atk object bridged to Tcl/Tk. */
#define TK_ATK_TYPE_ACCESSIBLE (tk_atk_accessible_get_type())
G_DEFINE_TYPE_WITH_CODE(TkAtkAccessible, tk_atk_accessible, ATK_TYPE_OBJECT,







>








>








|



|







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
/* Cache update functions. */
static void UpdateGeometryCache(TkAtkAccessible *acc);
static void UpdateNameCache(TkAtkAccessible *acc);
static void UpdateDescriptionCache(TkAtkAccessible *acc);
static void UpdateValueCache(TkAtkAccessible *acc);
static void UpdateRoleCache(TkAtkAccessible *acc);
static void UpdateStateCache(TkAtkAccessible *acc);
static void UpdateChildrenCache(TkAtkAccessible *acc);

/* Event handlers. */
void TkAtkAccessible_RegisterEventHandlers(Tk_Window tkwin, void *tkAccessible);
static void TkAtkAccessible_DestroyHandler(ClientData clientData, XEvent *eventPtr);
static void TkAtkAccessible_ConfigureHandler(ClientData clientData, XEvent *eventPtr);
static void TkAtkAccessible_MapHandler(ClientData clientData, XEvent *eventPtr);
static void TkAtkAccessible_UnmapHandler(ClientData clientData, XEvent *eventPtr);
static void TkAtkAccessible_FocusHandler(ClientData clientData, XEvent *eventPtr);
static void TkAtkAccessible_CreateHandler(ClientData clientData, XEvent *eventPtr);

/* Tcl command implementations. */
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[]);
int TkAtkAccessibleObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]);
int TkAtkAccessibility_Init(Tcl_Interp *interp);

/* GLib-Tcl event loop integration. */
static void SetupGlibIntegration(void);
static void ProcessPendingEvents(ClientData clientData);

/* Child management functions. */
static void AddChildToParent(TkAtkAccessible *parent, AtkObject *child);
static void RemoveChildFromParent(TkAtkAccessible *parent, AtkObject *child);


/* Define custom Atk object bridged to Tcl/Tk. */
#define TK_ATK_TYPE_ACCESSIBLE (tk_atk_accessible_get_type())
G_DEFINE_TYPE_WITH_CODE(TkAtkAccessible, tk_atk_accessible, ATK_TYPE_OBJECT,
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
    if (glib_context) {
        while (g_main_context_iteration(glib_context, FALSE)) {
        }
    }

    in_process_pending = FALSE;

    /* Re-schedule. */
    Tcl_CreateTimerHandler(10, ProcessPendingEvents, NULL);
}


/*
 *----------------------------------------------------------------------
 *
 * Child management functions.
 *
 *----------------------------------------------------------------------
 */

static void AddChildToParent(TkAtkAccessible *parent, AtkObject *child) 
{
    if (!parent || !child) return;







|







|







385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
    if (glib_context) {
        while (g_main_context_iteration(glib_context, FALSE)) {
        }
    }

    in_process_pending = FALSE;

    /* Re-scheudle. */
    Tcl_CreateTimerHandler(10, ProcessPendingEvents, NULL);
}


/*
 *----------------------------------------------------------------------
 *
 * Child management functions
 *
 *----------------------------------------------------------------------
 */

static void AddChildToParent(TkAtkAccessible *parent, AtkObject *child) 
{
    if (!parent || !child) return;
515
516
517
518
519
520
521

522
523
524
525
526
527
528
529
}

/* Find hash entry by key. */
static gpointer find_hash_entry_by_key_main(gpointer data)
{
    MainThreadData *mt_data = (MainThreadData *)data;
    Tcl_HashTable *table = (Tcl_HashTable *)mt_data->result;

    return (gpointer)Tcl_FindHashEntry(table, "role");
}

/* Obtain the string value for a hash entry. */ 
static gpointer get_hash_string_value_main(gpointer data)
{
    MainThreadData *mt_data = (MainThreadData *)data;
    Tcl_HashEntry *hPtr = (Tcl_HashEntry *)mt_data->result;







>
|







520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
}

/* Find hash entry by key. */
static gpointer find_hash_entry_by_key_main(gpointer data)
{
    MainThreadData *mt_data = (MainThreadData *)data;
    Tcl_HashTable *table = (Tcl_HashTable *)mt_data->result;
    const char *key = mt_data->key; 
    return (gpointer)Tcl_FindHashEntry(table, key);
}

/* Obtain the string value for a hash entry. */ 
static gpointer get_hash_string_value_main(gpointer data)
{
    MainThreadData *mt_data = (MainThreadData *)data;
    Tcl_HashEntry *hPtr = (Tcl_HashEntry *)mt_data->result;
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/* Get the main Tk window.  */
static gpointer get_main_window_main(gpointer data)
{
    MainThreadData *mt_data = (MainThreadData *)data;
    return (gpointer)Tk_MainWindow(mt_data->interp);
}

/* Get root/screen coordinates for correct extents in ATK_XY_SCREEN. */
static gpointer get_root_coords_main(gpointer data)
{
    MainThreadData *mt_data = (MainThreadData *)data;
    gint *coords = g_new(gint, 4);
    Tk_GetRootCoords(mt_data->tkwin, &coords[0], &coords[1]);
    coords[2] = Tk_Width(mt_data->tkwin);
    coords[3] = Tk_Height(mt_data->tkwin);







|







551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/* Get the main Tk window.  */
static gpointer get_main_window_main(gpointer data)
{
    MainThreadData *mt_data = (MainThreadData *)data;
    return (gpointer)Tk_MainWindow(mt_data->interp);
}

/* Get root/screen coordinates for correct extents. */
static gpointer get_root_coords_main(gpointer data)
{
    MainThreadData *mt_data = (MainThreadData *)data;
    gint *coords = g_new(gint, 4);
    Tk_GetRootCoords(mt_data->tkwin, &coords[0], &coords[1]);
    coords[2] = Tk_Width(mt_data->tkwin);
    coords[3] = Tk_Height(mt_data->tkwin);
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
 * lifting of implementing the Tk-ATK interface. 
 *
 *----------------------------------------------------------------------
 */


/*
 * 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) {
	*x = *y = *width = *height = 0;
	return;
    }

    MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL};

    /*
     * Use correct coordinates based on coord_type.
     * - ATK_XY_WINDOW: Cached relative to toplevel window (sum of Tk_X/Tk_Y).
     * - ATK_XY_SCREEN: Absolute screen via Tk_GetRootCoords (fixes navigation issues).
     */
    
    if (coord_type == ATK_XY_SCREEN) {
        gint *coords = (gint *)RunOnMainThread(get_root_coords_main, &data);
        if (coords) {
            *x = coords[0];
            *y = coords[1];
            *width = coords[2];
            *height = coords[3];







|











|

<
|
|
|

<







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
 * lifting of implementing the Tk-ATK interface. 
 *
 *----------------------------------------------------------------------
 */


/*
 * 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) {
	*x = *y = *width = *height = 0;
	return;
    }

    MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL, NULL};


    /* Use correct coordinates based on coord_type.
     * ATK_XY_WINDOW: Cached relative to toplevel window (sum of Tk_X/Tk_Y).
     * ATK_XY_SCREEN: Absolute screen via Tk_GetRootCoords (fixes navigation issues).
     */

    if (coord_type == ATK_XY_SCREEN) {
        gint *coords = (gint *)RunOnMainThread(get_root_coords_main, &data);
        if (coords) {
            *x = coords[0];
            *y = coords[1];
            *width = coords[2];
            *height = coords[3];
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
        g_object_ref(child->data);
        return ATK_OBJECT(child->data);
    }
    return NULL;
}

/*
 * Functions to map accessible role to Atk.
 */

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

    MainThreadData data = {win, NULL, NULL, NULL};
    Tcl_HashEntry *hPtr = (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_main, &data);
   
    /* Check if we have accessibility attributes. */ 
    if (hPtr) {
	Tcl_HashTable *AccessibleAttributes = (Tcl_HashTable *)RunOnMainThread(get_hash_value_main, &data);
	if (AccessibleAttributes) {
	    Tcl_HashEntry *hPtr2 = (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_by_key_main, &data);
	    if (hPtr2) {
		char *result = (char *)RunOnMainThread(get_hash_string_value_main, &data);
		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 if no role attribute set (fixes missing roles like "button"). */
    const char *widgetClass = Tk_Class(win);
    if (widgetClass) {
        for (int i = 0; roleMap[i].tkrole != NULL; i++) {
            if (strcasecmp(roleMap[i].tkrole, widgetClass) == 0) {  /* Case-insensitive for robustness */
                return roleMap[i].atkrole;
            }
        }







|






|




















|







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
        g_object_ref(child->data);
        return ATK_OBJECT(child->data);
    }
    return NULL;
}

/*
 * Functions to map accessible role to ATK.
 */

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

    MainThreadData data = {win, NULL, NULL, NULL, "role"};
    Tcl_HashEntry *hPtr = (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_main, &data);
   
    /* Check if we have accessibility attributes. */ 
    if (hPtr) {
	Tcl_HashTable *AccessibleAttributes = (Tcl_HashTable *)RunOnMainThread(get_hash_value_main, &data);
	if (AccessibleAttributes) {
	    Tcl_HashEntry *hPtr2 = (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_by_key_main, &data);
	    if (hPtr2) {
		char *result = (char *)RunOnMainThread(get_hash_string_value_main, &data);
		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 if no attribute set. */
    const char *widgetClass = Tk_Class(win);
    if (widgetClass) {
        for (int i = 0; roleMap[i].tkrole != NULL; i++) {
            if (strcasecmp(roleMap[i].tkrole, widgetClass) == 0) {  /* Case-insensitive for robustness */
                return roleMap[i].atkrole;
            }
        }
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
 * Name and description getters
 * for Tk-ATK objects.
 */

static const gchar *tk_get_name(AtkObject *obj)
{
    TkAtkAccessible *acc = (TkAtkAccessible *)obj;
    /* 
     * Orca already says "push button" as the role,
     * so return the description to avoid redundancy. 
     */
    return acc->cached_description;
}

static void tk_set_name(AtkObject *obj, const gchar *name)
{
    TkAtkAccessible *acc = (TkAtkAccessible *)obj;

    if (!acc) return; 







<
<
<
<
|







810
811
812
813
814
815
816




817
818
819
820
821
822
823
824
 * Name and description getters
 * for Tk-ATK objects.
 */

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




    return acc->cached_name;
}

static void tk_set_name(AtkObject *obj, const gchar *name)
{
    TkAtkAccessible *acc = (TkAtkAccessible *)obj;

    if (!acc) return; 
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
	}
    }
    return set;
}

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

static gboolean tk_action_do_action(AtkAction *action, gint i)
{
    TkAtkAccessible *acc = (TkAtkAccessible *)action;

    if (!acc || !acc->tkwin || !acc->interp) {
	return FALSE;
    }

    if (i == 0) {
	/* Retrieve the command string.  */
	MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL};
	Tcl_HashEntry *hPtr = (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_main, &data);
	if (!hPtr) {
	    return FALSE;
	}
        
	Tcl_HashTable *AccessibleAttributes = (Tcl_HashTable *)RunOnMainThread(get_hash_value_main, &data);
	if (!AccessibleAttributes) {







|












|







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
	}
    }
    return set;
}

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

static gboolean tk_action_do_action(AtkAction *action, gint i)
{
    TkAtkAccessible *acc = (TkAtkAccessible *)action;

    if (!acc || !acc->tkwin || !acc->interp) {
	return FALSE;
    }

    if (i == 0) {
	/* Retrieve the command string.  */
	MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL, "action"};
	Tcl_HashEntry *hPtr = (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_main, &data);
	if (!hPtr) {
	    return FALSE;
	}
        
	Tcl_HashTable *AccessibleAttributes = (Tcl_HashTable *)RunOnMainThread(get_hash_value_main, &data);
	if (!AccessibleAttributes) {
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
{
    iface->do_action = tk_action_do_action;
    iface->get_n_actions = tk_action_get_n_actions;
    iface->get_name = tk_action_get_name;
}

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







|







947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
{
    iface->do_action = tk_action_do_action;
    iface->get_n_actions = tk_action_get_n_actions;
    iface->get_name = tk_action_get_name;
}

/*
 * 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;
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
}

static void tk_atk_accessible_finalize(GObject *gobject)
{
    TkAtkAccessible *self = (TkAtkAccessible*)gobject;

    if (self->tkwin) {
        if (RunOnMainThread(is_toplevel_main, &(MainThreadData){self->tkwin, NULL, NULL, NULL})) {
            toplevel_accessible_objects = g_list_remove(toplevel_accessible_objects, self);
            /* Remove from root's children. */
            gint index = g_list_index(((TkAtkAccessible *)tk_root_accessible)->children, self);
            if (index >= 0) {
                RemoveChildFromParent((TkAtkAccessible *)tk_root_accessible, ATK_OBJECT(self));
                ChildrenChangedRemoveData *crd = g_new0(ChildrenChangedRemoveData, 1);
                crd->parent = tk_root_accessible;







|







973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
}

static void tk_atk_accessible_finalize(GObject *gobject)
{
    TkAtkAccessible *self = (TkAtkAccessible*)gobject;

    if (self->tkwin) {
        if (RunOnMainThread(is_toplevel_main, &(MainThreadData){self->tkwin, NULL, NULL, NULL, NULL})) {
            toplevel_accessible_objects = g_list_remove(toplevel_accessible_objects, self);
            /* Remove from root's children. */
            gint index = g_list_index(((TkAtkAccessible *)tk_root_accessible)->children, self);
            if (index >= 0) {
                RemoveChildFromParent((TkAtkAccessible *)tk_root_accessible, ATK_OBJECT(self));
                ChildrenChangedRemoveData *crd = g_new0(ChildrenChangedRemoveData, 1);
                crd->parent = tk_root_accessible;
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
        }
    }
    
    /* Check for existing registration. */
    AtkObject *existing = GetAtkObjectForTkWindow(tkwin);
    if (existing && existing != accessible) {
        g_warning("RegisterToplevelWindow: Toplevel %s already registered with different AtkObject",
                  (const char *)RunOnMainThread(get_window_name_main, &(MainThreadData){tkwin, NULL, NULL, NULL}));
        return;
    }

    /* Hold a reference to the accessible object. */
    g_object_ref(accessible);

    /* Set parent and add to root's children. */







|







1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
        }
    }
    
    /* Check for existing registration. */
    AtkObject *existing = GetAtkObjectForTkWindow(tkwin);
    if (existing && existing != accessible) {
        g_warning("RegisterToplevelWindow: Toplevel %s already registered with different AtkObject",
                  (const char *)RunOnMainThread(get_window_name_main, &(MainThreadData){tkwin, NULL, NULL, NULL, NULL}));
        return;
    }

    /* Hold a reference to the accessible object. */
    g_object_ref(accessible);

    /* Set parent and add to root's children. */
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
    }

    /* Set name, role, and description. */
    const gchar *name = tk_get_name(accessible);
    if (name) {
        tk_set_name(accessible, name);
    } else {
        tk_set_name(accessible, (const char *)RunOnMainThread(get_window_name_main, &(MainThreadData){tkwin, NULL, NULL, NULL}));
    }
    atk_object_set_role(accessible, ATK_ROLE_WINDOW);
    atk_object_set_description(accessible, name ? name : ((TkAtkAccessible *)accessible)->path);

    /* Set states and notify. */
    AtkStateSet *state_set = atk_object_ref_state_set(accessible);
    if (state_set != NULL) {







|







1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
    }

    /* Set name, role, and description. */
    const gchar *name = tk_get_name(accessible);
    if (name) {
        tk_set_name(accessible, name);
    } else {
        tk_set_name(accessible, (const char *)RunOnMainThread(get_window_name_main, &(MainThreadData){tkwin, NULL, NULL, NULL, NULL}));
    }
    atk_object_set_role(accessible, ATK_ROLE_WINDOW);
    atk_object_set_description(accessible, name ? name : ((TkAtkAccessible *)accessible)->path);

    /* Set states and notify. */
    AtkStateSet *state_set = atk_object_ref_state_set(accessible);
    if (state_set != NULL) {
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
        crd->index = index;
        crd->child = accessible;
        RunOnMainThread(emit_children_changed_remove, crd);
        
        toplevel_accessible_objects = g_list_remove(toplevel_accessible_objects, accessible);
    }
}


/* 
* Register child widgets of a given window 
* as accessible objects. 
*/

static void RegisterChildWidgets(Tcl_Interp *interp, Tk_Window tkwin, AtkObject *parent_obj)
{
    /* Validate inputs. */
    if (!tkwin || !parent_obj || !G_IS_OBJECT(parent_obj)) {
        g_warning("RegisterChildWidgets: Invalid tkwin or parent_obj");
        return;
    }
    
    MainThreadData data = {tkwin, interp, NULL, NULL};
    TkWindow *winPtr = (TkWindow *)RunOnMainThread(get_window_handle_main, &data);
    TkWindow *childPtr;
    
    for (childPtr = winPtr->childList; childPtr != NULL; childPtr = childPtr->nextPtr) {
        Tk_Window child = (Tk_Window)childPtr;
        MainThreadData child_data = {child, interp, NULL, NULL};
        
        AtkObject *child_obj = GetAtkObjectForTkWindow(child);
        if (!child_obj) {
            const char *child_path = (const char *)RunOnMainThread(get_window_name_main, &child_data);
            child_obj = TkCreateAccessibleAtkObject(interp, child, child_path);
            if (!child_obj) {
                g_warning("RegisterChildWidgets: Failed to create accessible object for %s", child_path);
                continue;
            }
            RegisterAtkObjectForTkWindow(child, child_obj);
            TkAtkAccessible_RegisterEventHandlers(child, (TkAtkAccessible *)child_obj);
            
            /* Register children recursively. */
            RegisterChildWidgets(interp, child, child_obj);
        }
    }
}

/*
 * 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 relationship. */
AtkObject *TkCreateAccessibleAtkObject(Tcl_Interp *interp, Tk_Window tkwin, const char *path)
{
    /* Validate inputs. */
    if (!interp || !tkwin || !path) {
        g_warning("TkCreateAccessibleAtkObject: Invalid parameters");
        return NULL;
    }
    
    /* Create new TkAtkAccessible object. */
    TkAtkAccessible *acc = g_object_new(TK_ATK_TYPE_ACCESSIBLE, NULL);
    acc->interp = interp;
    acc->tkwin = tkwin;
    acc->path = sanitize_utf8(path);
    /* Ensure toplevel windows exist and are mapped. */
    MainThreadData data = {tkwin, interp, NULL, NULL};
    if ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &data) &&
	tkwin != (Tk_Window)RunOnMainThread(get_main_window_main,
					    &(MainThreadData){NULL, interp, NULL, NULL})) {
	RunOnMainThread(make_window_exist_main, &data);
	RunOnMainThread(map_window_main, &data);
    }

    /* Update cached properties. */
    UpdateGeometryCache(acc);
    UpdateNameCache(acc);         /* now fixed with correct thread handoff */
    UpdateDescriptionCache(acc);  /* now fixed with correct thread handoff */
    UpdateValueCache(acc);
    UpdateRoleCache(acc);
    UpdateStateCache(acc);


    /* Initialize AtkObject. */
    AtkObject *obj = ATK_OBJECT(acc);
    AtkRole role = acc->cached_role;
    if (role == ATK_ROLE_UNKNOWN &&
	((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &data) ||
	 tkwin == (Tk_Window)RunOnMainThread(get_main_window_main,
					     &(MainThreadData){NULL, interp, NULL, NULL}))) {
	role = ATK_ROLE_WINDOW;
    }
    atk_object_set_role(obj, role);


    /* Set ATK name from description if available.*/



    if (acc->cached_description && *acc->cached_description) {
	atk_object_set_name(obj, acc->cached_description);
    } else if (acc->cached_name && *acc->cached_name) {
	atk_object_set_name(obj, acc->cached_name);
    } else {
	atk_object_set_name(obj, path);
    }

    /* Always set description property separately. */
    if (acc->cached_description && *acc->cached_description) {
	atk_object_set_description(obj, acc->cached_description);
    } else {
	atk_object_set_description(obj, path);
    }
  
    /* Set states: VISIBLE, SHOWING, ENABLED, and FOCUSABLE where applicable. */
    AtkStateSet *state_set = atk_object_ref_state_set(obj);
    if (state_set != NULL) {
	atk_state_set_add_state(state_set, ATK_STATE_VISIBLE);
	atk_state_set_add_state(state_set, ATK_STATE_SHOWING);
	atk_state_set_add_state(state_set, ATK_STATE_ENABLED);
	if (role == ATK_ROLE_PUSH_BUTTON || role == ATK_ROLE_ENTRY ||
	    role == ATK_ROLE_COMBO_BOX || role == ATK_ROLE_CHECK_BOX ||
	    role == ATK_ROLE_RADIO_BUTTON || role == ATK_ROLE_SLIDER ||
	    role == ATK_ROLE_SPIN_BUTTON) {
	    atk_state_set_add_state(state_set, ATK_STATE_FOCUSABLE);
	}

	/* Notify state changes to ensure Orca receives them. */
	atk_object_notify_state_change(obj, ATK_STATE_VISIBLE, TRUE);
	atk_object_notify_state_change(obj, ATK_STATE_SHOWING, TRUE);
	atk_object_notify_state_change(obj, ATK_STATE_ENABLED, TRUE);
	if (atk_state_set_contains_state(state_set, ATK_STATE_FOCUSABLE)) {
	    atk_object_notify_state_change(obj, ATK_STATE_FOCUSABLE, TRUE);
	}
	g_object_unref(state_set);
    }
    
    /* Get parent window. */
    Tk_Window parent_tkwin = (Tk_Window)RunOnMainThread(get_window_parent_main, &data);
    AtkObject *parent_obj = NULL;
    
    if (parent_tkwin) {
        parent_obj = GetAtkObjectForTkWindow(parent_tkwin);
        if (!parent_obj) {
            /* Create parent object without recursive full registration. */
            TkAtkAccessible *parent_acc = g_object_new(TK_ATK_TYPE_ACCESSIBLE, NULL);
            parent_acc->interp = interp;
            parent_acc->tkwin = parent_tkwin;
            parent_acc->path = sanitize_utf8((const char *)RunOnMainThread(get_window_name_main, &(MainThreadData){parent_tkwin, interp, NULL, NULL}));
            UpdateGeometryCache(parent_acc);
            UpdateNameCache(parent_acc);
            UpdateDescriptionCache(parent_acc);
            UpdateValueCache(parent_acc);
            UpdateRoleCache(parent_acc);
            UpdateStateCache(parent_acc);

            parent_obj = ATK_OBJECT(parent_acc);
            atk_object_set_role(parent_obj, ATK_ROLE_WINDOW);
            if (parent_acc->cached_name) {
                atk_object_set_name(parent_obj, parent_acc->cached_name);
            } else {
                atk_object_set_name(parent_obj, parent_acc->path);
            }
            if (parent_acc->cached_description) {
                atk_object_set_description(parent_obj, parent_acc->cached_description);
            } else {
                atk_object_set_description(parent_obj, parent_acc->path);
            }
            /* Set parent states. */
            state_set = atk_object_ref_state_set(parent_obj);
            if (state_set !=NULL) {
		atk_state_set_add_state(state_set, ATK_STATE_VISIBLE);
		atk_state_set_add_state(state_set, ATK_STATE_SHOWING);
		atk_state_set_add_state(state_set, ATK_STATE_ENABLED);
		
		atk_object_notify_state_change(parent_obj, ATK_STATE_VISIBLE, TRUE);
		atk_object_notify_state_change(parent_obj, ATK_STATE_SHOWING, TRUE);
		atk_object_notify_state_change(parent_obj, ATK_STATE_ENABLED, TRUE);
            
		g_object_unref(state_set);
	    }
            
            RegisterAtkObjectForTkWindow(parent_tkwin, parent_obj);
            TkAtkAccessible_RegisterEventHandlers(parent_tkwin, parent_acc);
            
            /* Set parent of parent_obj. */
            Tk_Window grandparent_tkwin = (Tk_Window)RunOnMainThread(get_window_parent_main, &(MainThreadData){parent_tkwin, interp, NULL, NULL});
            AtkObject *grandparent_obj = grandparent_tkwin ? GetAtkObjectForTkWindow(grandparent_tkwin) : tk_root_accessible;
            if (grandparent_obj && G_IS_OBJECT(grandparent_obj)) {
                atk_object_set_parent(parent_obj, grandparent_obj);
                AddChildToParent((TkAtkAccessible *)grandparent_obj, parent_obj);
                ChildrenChangedAddData *cad = g_new0(ChildrenChangedAddData, 1);
                cad->parent = grandparent_obj;
                cad->index = g_list_length(((TkAtkAccessible *)grandparent_obj)->children) - 1;
                cad->child = parent_obj;
                if (cad->parent) g_object_ref(cad->parent);
                if (cad->child) g_object_ref(cad->child);
                g_debug("TkCreateAccessibleAtkObject: Emitting children-changed::add for parent %s, grandparent=%p, index=%d",
                        parent_acc->path, grandparent_obj, cad->index);
                RunOnMainThread(emit_children_changed_add, cad);
            }
            
            if ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &(MainThreadData){parent_tkwin, interp, NULL, NULL})) {
                if (!g_list_find(toplevel_accessible_objects, parent_obj)) {
                    toplevel_accessible_objects = g_list_append(toplevel_accessible_objects, parent_obj);
                }
            }
        }
    } else {
        parent_obj = tk_root_accessible;
    }
    
    if (parent_obj && G_IS_OBJECT(parent_obj)) {
        atk_object_set_parent(obj, parent_obj);
        g_debug("TkCreateAccessibleAtkObject: Adding %s to parent %p (path=%s)", 
                path, parent_obj, ((TkAtkAccessible *)parent_obj)->path);
        AddChildToParent((TkAtkAccessible *)parent_obj, obj);
        ChildrenChangedAddData *cad = g_new0(ChildrenChangedAddData, 1);
        cad->parent = parent_obj;
        cad->index = g_list_length(((TkAtkAccessible *)parent_obj)->children) - 1;
        cad->child = obj;
        if (cad->parent) g_object_ref(cad->parent);
        if (cad->child) g_object_ref(cad->child);
        g_debug("TkCreateAccessibleAtkObject: Emitting children-changed::add for %s, parent=%p, index=%d",
                path, parent_obj, cad->index);
        RunOnMainThread(emit_children_changed_add, cad);
    } else {
        g_warning("TkCreateAccessibleAtkObject: Invalid parent for %s (parent_tkwin=%p, parent_obj=%p, is_gobject=%d)",
                  path, parent_tkwin, parent_obj, parent_obj ? G_IS_OBJECT(parent_obj) : 0);
        g_object_unref(obj);
        return NULL;
    }
    
    if ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &data)) {
        RegisterToplevelWindow(interp, tkwin, obj);
    }
    
    return obj;
}

/*
 * Functions to map Tk window to its corresponding Atk object.
 */

void InitAtkTkMapping(void)
{
    if (!tk_to_atk_map) {







>






|

<

|
|

|
<
<
<
|
<
<
<
|
<
<
<
<
<
<
<
<
<
<
|
<
<
|
|
<


















|












|


<

|
|

|
<




|
|

|
<






|
|



>

<




|
<




>
|
>
>
>







<
<





|
|











<
<








|
|


|

|
|
<
|
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
|
|
|
<
|
|



<



<


<
|
|
|
<
|
|
|
|
|
|
|
|
|
|
|
<
<
|
|
|
|
|
|
|
|
|

|

|

|
<
<
|
|
|
|
|
|
|
<
<
|

|
<
|
|

|

|

|


>







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
        crd->index = index;
        crd->child = accessible;
        RunOnMainThread(emit_children_changed_remove, crd);
        
        toplevel_accessible_objects = g_list_remove(toplevel_accessible_objects, accessible);
    }
}


/* 
* Register child widgets of a given window 
* as accessible objects. 
*/

static void RegisterChildWidgets(Tcl_Interp *interp, Tk_Window tkwin, AtkObject *parent_obj) 
{

    if (!tkwin || !parent_obj || !G_IS_OBJECT(parent_obj)) {
	g_warning("RegisterChildWidgets: Invalid tkwin or parent_obj");
	return;
    }




    TkAtkAccessible *acc = (TkAtkAccessible *)parent_obj;



    /* Update children cache asynchronously. */










    UpdateChildrenCache(acc);


}



/*
 * 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 || !path) {
	g_warning("TkCreateAccessibleAtkObject: Invalid parameters");
	return NULL;
    }


    TkAtkAccessible *acc = g_object_new(TK_ATK_TYPE_ACCESSIBLE, NULL);
    acc->interp = interp;
    acc->tkwin = tkwin;
    acc->path = sanitize_utf8(path);

    MainThreadData data = {tkwin, interp, NULL, NULL, NULL, NULL};
    if ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &data) &&
	tkwin != (Tk_Window)RunOnMainThread(get_main_window_main, &(MainThreadData){NULL, interp, NULL, NULL, NULL})) {

	RunOnMainThread(make_window_exist_main, &data);
	RunOnMainThread(map_window_main, &data);
    }

    /* Update cached properties. */
    UpdateGeometryCache(acc);
    UpdateNameCache(acc);
    UpdateDescriptionCache(acc);
    UpdateValueCache(acc);
    UpdateRoleCache(acc);
    UpdateStateCache(acc);
    UpdateChildrenCache(acc);


    AtkObject *obj = ATK_OBJECT(acc);
    AtkRole role = acc->cached_role;
    if (role == ATK_ROLE_UNKNOWN &&
	((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &data) ||
	 tkwin == (Tk_Window)RunOnMainThread(get_main_window_main, &(MainThreadData){NULL, interp, NULL, NULL, NULL}))) {

	role = ATK_ROLE_WINDOW;
    }
    atk_object_set_role(obj, role);

    /*
     * Set ATK name and description. Return description for name
     * because name is generally the same as the role, and we do not
     * want redundant labels from screen reader. 
     */
    if (acc->cached_description && *acc->cached_description) {
	atk_object_set_name(obj, acc->cached_description);
    } else if (acc->cached_name && *acc->cached_name) {
	atk_object_set_name(obj, acc->cached_name);
    } else {
	atk_object_set_name(obj, path);
    }


    if (acc->cached_description && *acc->cached_description) {
	atk_object_set_description(obj, acc->cached_description);
    } else {
	atk_object_set_description(obj, path);
    }

    /* Set states. */
    AtkStateSet *state_set = atk_object_ref_state_set(obj);
    if (state_set != NULL) {
	atk_state_set_add_state(state_set, ATK_STATE_VISIBLE);
	atk_state_set_add_state(state_set, ATK_STATE_SHOWING);
	atk_state_set_add_state(state_set, ATK_STATE_ENABLED);
	if (role == ATK_ROLE_PUSH_BUTTON || role == ATK_ROLE_ENTRY ||
	    role == ATK_ROLE_COMBO_BOX || role == ATK_ROLE_CHECK_BOX ||
	    role == ATK_ROLE_RADIO_BUTTON || role == ATK_ROLE_SLIDER ||
	    role == ATK_ROLE_SPIN_BUTTON) {
	    atk_state_set_add_state(state_set, ATK_STATE_FOCUSABLE);
	}


	atk_object_notify_state_change(obj, ATK_STATE_VISIBLE, TRUE);
	atk_object_notify_state_change(obj, ATK_STATE_SHOWING, TRUE);
	atk_object_notify_state_change(obj, ATK_STATE_ENABLED, TRUE);
	if (atk_state_set_contains_state(state_set, ATK_STATE_FOCUSABLE)) {
	    atk_object_notify_state_change(obj, ATK_STATE_FOCUSABLE, TRUE);
	}
	g_object_unref(state_set);
    }

    /* Set parent. */
    Tk_Window parent_tkwin = (Tk_Window)RunOnMainThread(get_window_parent_main, &data);
    AtkObject *parent_obj = NULL;

    if (parent_tkwin) {
	parent_obj = GetAtkObjectForTkWindow(parent_tkwin);
	if (!parent_obj) {

	    TkAtkAccessible *parent_acc = g_object_new(TK_ATK_TYPE_ACCESSIBLE, NULL);
	    parent_acc->interp = interp;
	    parent_acc->tkwin = parent_tkwin;
	    parent_acc->path = sanitize_utf8((const char *)RunOnMainThread(get_window_name_main, &(MainThreadData){parent_tkwin, interp, NULL, NULL, NULL}));
	    UpdateGeometryCache(parent_acc);
	    UpdateNameCache(parent_acc);
	    UpdateDescriptionCache(parent_acc);
	    UpdateValueCache(parent_acc);
	    UpdateRoleCache(parent_acc);
	    UpdateStateCache(parent_acc);
	    UpdateChildrenCache(parent_acc); /* Initialize parent children. */
	    parent_obj = ATK_OBJECT(parent_acc);
	    atk_object_set_role(parent_obj, ATK_ROLE_WINDOW);
	    if (parent_acc->cached_name) {
		atk_object_set_name(parent_obj, parent_acc->cached_name);
	    } else {
		atk_object_set_name(parent_obj, parent_acc->path);
	    }
	    if (parent_acc->cached_description) {
		atk_object_set_description(parent_obj, parent_acc->cached_description);
	    } else {
		atk_object_set_description(parent_obj, parent_acc->path);
	    }

	    state_set = atk_object_ref_state_set(parent_obj);
	    if (state_set != NULL) {
		atk_state_set_add_state(state_set, ATK_STATE_VISIBLE);
		atk_state_set_add_state(state_set, ATK_STATE_SHOWING);
		atk_state_set_add_state(state_set, ATK_STATE_ENABLED);

		atk_object_notify_state_change(parent_obj, ATK_STATE_VISIBLE, TRUE);
		atk_object_notify_state_change(parent_obj, ATK_STATE_SHOWING, TRUE);
		atk_object_notify_state_change(parent_obj, ATK_STATE_ENABLED, TRUE);

		g_object_unref(state_set);
	    }

	    RegisterAtkObjectForTkWindow(parent_tkwin, parent_obj);
	    TkAtkAccessible_RegisterEventHandlers(parent_tkwin, parent_acc);


	    Tk_Window grandparent_tkwin = (Tk_Window)RunOnMainThread(get_window_parent_main, &(MainThreadData){parent_tkwin, interp, NULL, NULL, NULL});
	    AtkObject *grandparent_obj = grandparent_tkwin ? GetAtkObjectForTkWindow(grandparent_tkwin) : tk_root_accessible;
	    if (grandparent_obj && G_IS_OBJECT(grandparent_obj)) {
		atk_object_set_parent(parent_obj, grandparent_obj);
		AddChildToParent((TkAtkAccessible *)grandparent_obj, parent_obj);
		ChildrenChangedAddData *cad = g_new0(ChildrenChangedAddData, 1);
		cad->parent = grandparent_obj;
		cad->index = g_list_length(((TkAtkAccessible *)grandparent_obj)->children) - 1;
		cad->child = parent_obj;
		if (cad->parent) g_object_ref(cad->parent);
		if (cad->child) g_object_ref(cad->child);


		RunOnMainThreadAsync(emit_children_changed_add, cad);
	    }

	    if ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &(MainThreadData){parent_tkwin, interp, NULL, NULL, NULL})) {
		if (!g_list_find(toplevel_accessible_objects, parent_obj)) {
		    toplevel_accessible_objects = g_list_append(toplevel_accessible_objects, parent_obj);
		}
	    }
	}
    } else {
	parent_obj = tk_root_accessible;
    }

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


	AddChildToParent((TkAtkAccessible *)parent_obj, obj);
	ChildrenChangedAddData *cad = g_new0(ChildrenChangedAddData, 1);
	cad->parent = parent_obj;
	cad->index = g_list_length(((TkAtkAccessible *)parent_obj)->children) - 1;
	cad->child = obj;
	if (cad->parent) g_object_ref(cad->parent);
	if (cad->child) g_object_ref(cad->child);


	RunOnMainThreadAsync(emit_children_changed_add, cad);
    } else {
	g_warning("TkCreateAccessibleAtkObject: Invalid parent for %s", path);

	g_object_unref(obj);
	return NULL;
    }

    if ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &data)) {
	RegisterToplevelWindow(interp, tkwin, obj);
    }

    return obj;
}

/*
 * Functions to map Tk window to its corresponding Atk object.
 */

void InitAtkTkMapping(void)
{
    if (!tk_to_atk_map) {
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
 *----------------------------------------------------------------------
 */
 
static void UpdateGeometryCache(TkAtkAccessible *acc) 
{
    if (!acc || !acc->tkwin) return;
    
    MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL};
    gint *geometry = RunOnMainThread(get_window_geometry_main, &data);
    if (geometry) {
        acc->x = geometry[0];
        acc->y = geometry[1];
        acc->width = geometry[2];
        acc->height = geometry[3];
        







|







1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
 *----------------------------------------------------------------------
 */
 
static void UpdateGeometryCache(TkAtkAccessible *acc) 
{
    if (!acc || !acc->tkwin) return;
    
    MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL, NULL};
    gint *geometry = RunOnMainThread(get_window_geometry_main, &data);
    if (geometry) {
        acc->x = geometry[0];
        acc->y = geometry[1];
        acc->width = geometry[2];
        acc->height = geometry[3];
        
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
static void UpdateNameCache(TkAtkAccessible *acc)
{
    if (!acc || !acc->tkwin || !acc->interp) return;

    g_free(acc->cached_name);
    acc->cached_name = NULL;

    MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL};

    /* Get the top-level hash entry for this Tk window. */
    Tcl_HashEntry *hPtr = (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_main, &data);
    if (hPtr) {
        data.result = hPtr;  /* Pass to get_hash_value_main() */
        Tcl_HashTable *AccessibleAttributes =
            (Tcl_HashTable *)RunOnMainThread(get_hash_value_main, &data);
        if (AccessibleAttributes) {
            /* Lookup the "name" attribute key inside this table. */
            data.result = AccessibleAttributes; /* Pass table to find_hash_entry_by_key_main() */
            Tcl_HashEntry *hPtr2 =
                (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_by_key_main, &data);
            if (hPtr2) {
                data.result = hPtr2; /* Pass entry to get_hash_string_value_main() */
                const char *result =
                    (const char *)RunOnMainThread(get_hash_string_value_main, &data);
                if (result) {
                    acc->cached_name = sanitize_utf8(result);
                }
            }
        }
    }

    /* Fallback to the Tk widget name/path */
    if (!acc->cached_name) {
        acc->cached_name =
            sanitize_utf8((const char *)RunOnMainThread(get_window_name_main, &data));
    }
}

static void UpdateDescriptionCache(TkAtkAccessible *acc)
{
    if (!acc || !acc->tkwin || !acc->interp) return;

    g_free(acc->cached_description);
    acc->cached_description = NULL;

    MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL};

    /* Get the top-level hash entry for this Tk window. */
    Tcl_HashEntry *hPtr = (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_main, &data);
    if (hPtr) {
        data.result = hPtr;  /* Pass to get_hash_value_main() */
        Tcl_HashTable *AccessibleAttributes =
            (Tcl_HashTable *)RunOnMainThread(get_hash_value_main, &data);
        if (AccessibleAttributes) {
            /* Step 2: Lookup the "description" attribute key inside this table. */
            data.result = AccessibleAttributes; /* Pass table to find_hash_entry_by_key_main() */
            Tcl_HashEntry *hPtr2 =
                (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_by_key_main, &data);
            if (hPtr2) {
                data.result = hPtr2; /* Pass entry to get_hash_string_value_main() */
                const char *result =
                    (const char *)RunOnMainThread(get_hash_string_value_main, &data);
                    g_warning("result is %s\n", result);
                if (result) {
                    acc->cached_description = sanitize_utf8(result);
                } 
            }
        }
    }
}


static void UpdateValueCache(TkAtkAccessible *acc)
{
    if (!acc || !acc->tkwin) return;

    g_free(acc->cached_value);
    acc->cached_value = NULL;

    MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL};
    Tcl_HashEntry *hPtr = (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_main, &data);
    if (hPtr) {
	Tcl_HashTable *AccessibleAttributes = (Tcl_HashTable *)RunOnMainThread(get_hash_value_main, &data);
	if (AccessibleAttributes) {
	    Tcl_HashEntry *hPtr2 = (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_by_key_main, &data);
	    if (hPtr2) {
		const char *result = (const char *)RunOnMainThread(get_hash_string_value_main, &data);







|
<
<


<
|
<
|
<
<
<
|
|
<
<
|
|
|
|
|
|


<

<
|





|




|
<
<


<
|
<
|
<
<
<
|
|
<
<
|
<
|
|
|
|
|


<








|







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
static void UpdateNameCache(TkAtkAccessible *acc)
{
    if (!acc || !acc->tkwin || !acc->interp) return;

    g_free(acc->cached_name);
    acc->cached_name = NULL;

    MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL, "name"};


    Tcl_HashEntry *hPtr = (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_main, &data);
    if (hPtr) {

	Tcl_HashTable *AccessibleAttributes = (Tcl_HashTable *)RunOnMainThread(get_hash_value_main, &data);

	if (AccessibleAttributes) {



	    Tcl_HashEntry *hPtr2 = (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_by_key_main, &data);
	    if (hPtr2) {


		const char *result = (const char *)RunOnMainThread(get_hash_string_value_main, &data);
		if (result) {
		    acc->cached_name = sanitize_utf8(result);
		}
	    }
	}
    }


    if (!acc->cached_name) {

	acc->cached_name = sanitize_utf8((const char *)RunOnMainThread(get_window_name_main, &data));
    }
}

static void UpdateDescriptionCache(TkAtkAccessible *acc)
{
    if (!acc || !acc->tkwin) return;

    g_free(acc->cached_description);
    acc->cached_description = NULL;

    MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL, "description"};


    Tcl_HashEntry *hPtr = (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_main, &data);
    if (hPtr) {

	Tcl_HashTable *AccessibleAttributes = (Tcl_HashTable *)RunOnMainThread(get_hash_value_main, &data);

	if (AccessibleAttributes) {



	    Tcl_HashEntry *hPtr2 = (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_by_key_main, &data);
	    if (hPtr2) {


		const char *result = (const char *)RunOnMainThread(get_hash_string_value_main, &data);

		if (result) {
		    acc->cached_description = sanitize_utf8(result);
		}
	    }
	}
    }
}


static void UpdateValueCache(TkAtkAccessible *acc)
{
    if (!acc || !acc->tkwin) return;

    g_free(acc->cached_value);
    acc->cached_value = NULL;

    MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL, "value"};
    Tcl_HashEntry *hPtr = (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_main, &data);
    if (hPtr) {
	Tcl_HashTable *AccessibleAttributes = (Tcl_HashTable *)RunOnMainThread(get_hash_value_main, &data);
	if (AccessibleAttributes) {
	    Tcl_HashEntry *hPtr2 = (Tcl_HashEntry *)RunOnMainThread(find_hash_entry_by_key_main, &data);
	    if (hPtr2) {
		const char *result = (const char *)RunOnMainThread(get_hash_string_value_main, &data);
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
	acc->cached_value = sanitize_utf8("");
    }
}

static void UpdateRoleCache(TkAtkAccessible *acc)
{
    if (!acc || !acc->tkwin) return;
    MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL};
    acc->cached_role = (AtkRole)(uintptr_t)RunOnMainThread(get_atk_role_for_widget_main, &data);
}

static void UpdateStateCache(TkAtkAccessible *acc) 
{
    if (!acc || !acc->tkwin) return;
    
    MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL};
    acc->is_mapped = (gboolean)(uintptr_t)RunOnMainThread(is_window_mapped_main, &data);
    
    TkWindow *focuswin = (TkWindow *)RunOnMainThread(get_focus_window_main, &data);
    Tk_Window focus_win = (Tk_Window)focuswin;
    acc->has_focus = (focus_win == acc->tkwin);
    
    if (!acc->has_focus && focus_win) {
	Tk_Window parent = (Tk_Window)RunOnMainThread(get_window_parent_main, &(MainThreadData){focus_win, NULL, NULL, NULL});
	while (parent) {
	    if (parent == acc->tkwin) {
		acc->has_focus = TRUE;
		break;
	    }
	    parent = (Tk_Window)RunOnMainThread(get_window_parent_main, &(MainThreadData){parent, NULL, NULL, NULL});
	}
    }
}

























































/*
 *----------------------------------------------------------------------
 *
 * Event handlers - update Tk and ATK in response to various X events. 
 *
 *----------------------------------------------------------------------







|







|







|





|



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
	acc->cached_value = sanitize_utf8("");
    }
}

static void UpdateRoleCache(TkAtkAccessible *acc)
{
    if (!acc || !acc->tkwin) return;
    MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL, "role"};
    acc->cached_role = (AtkRole)(uintptr_t)RunOnMainThread(get_atk_role_for_widget_main, &data);
}

static void UpdateStateCache(TkAtkAccessible *acc) 
{
    if (!acc || !acc->tkwin) return;
    
    MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL, "state"};
    acc->is_mapped = (gboolean)(uintptr_t)RunOnMainThread(is_window_mapped_main, &data);
    
    TkWindow *focuswin = (TkWindow *)RunOnMainThread(get_focus_window_main, &data);
    Tk_Window focus_win = (Tk_Window)focuswin;
    acc->has_focus = (focus_win == acc->tkwin);
    
    if (!acc->has_focus && focus_win) {
	Tk_Window parent = (Tk_Window)RunOnMainThread(get_window_parent_main, &(MainThreadData){focus_win, NULL, NULL, NULL, NULL});
	while (parent) {
	    if (parent == acc->tkwin) {
		acc->has_focus = TRUE;
		break;
	    }
	    parent = (Tk_Window)RunOnMainThread(get_window_parent_main, &(MainThreadData){parent, NULL, NULL, NULL, NULL});
	}
    }
}

static void UpdateChildrenCache(TkAtkAccessible *acc)
{

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

    MainThreadData data = {acc->tkwin, acc->interp, NULL, NULL, NULL};
    TkWindow *winPtr = (TkWindow *)RunOnMainThread(get_window_handle_main, &data);
    if (!winPtr) return;

    /* Clear existing children list. */
    GList *iter = acc->children;
    while (iter) {
	AtkObject *child = (AtkObject *)iter->data;
	if (child && G_IS_OBJECT(child)) {
	    RemoveChildFromParent(acc, child);
	    ChildrenChangedRemoveData *crd = g_new0(ChildrenChangedRemoveData, 1);
	    crd->parent = ATK_OBJECT(acc);
	    crd->index = g_list_index(acc->children, child);
	    crd->child = child;
	    if (crd->parent) g_object_ref(crd->parent);
	    if (crd->child) g_object_ref(crd->child);
	    RunOnMainThreadAsync(emit_children_changed_remove, crd);
	    g_object_unref(child);
	}
	iter = g_list_next(iter);
    }
    g_list_free(acc->children);
    acc->children = NULL;

    /* Repopulate children list. */
    for (TkWindow *childPtr = winPtr->childList; childPtr != NULL; childPtr = childPtr->nextPtr) {
	Tk_Window child = (Tk_Window)childPtr;
	AtkObject *child_obj = GetAtkObjectForTkWindow(child);
	if (!child_obj) {
	    MainThreadData child_data = {child, acc->interp, NULL, NULL, NULL};
	    const char *child_path = (const char *)RunOnMainThread(get_window_name_main, &child_data);
	    child_obj = TkCreateAccessibleAtkObject(acc->interp, child, child_path);
	    if (child_obj) {
		RegisterAtkObjectForTkWindow(child, child_obj);
		TkAtkAccessible_RegisterEventHandlers(child, (TkAtkAccessible *)child_obj);
	    }
	}
	if (child_obj && G_IS_OBJECT(child_obj)) {
	    AddChildToParent(acc, child_obj);
	    ChildrenChangedAddData *cad = g_new0(ChildrenChangedAddData, 1);
	    cad->parent = ATK_OBJECT(acc);
	    cad->index = g_list_length(acc->children) - 1;
	    cad->child = child_obj;
	    if (cad->parent) g_object_ref(cad->parent);
	    if (cad->child) g_object_ref(cad->child);
	    RunOnMainThreadAsync(emit_children_changed_add, cad);
	}
    }
}


/*
 *----------------------------------------------------------------------
 *
 * Event handlers - update Tk and ATK in response to various X events. 
 *
 *----------------------------------------------------------------------
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
			  TkAtkAccessible_ConfigureHandler, tkAccessible);
    Tk_CreateEventHandler(tkwin, MapNotify,
			  TkAtkAccessible_MapHandler, tkAccessible);
    Tk_CreateEventHandler(tkwin, UnmapNotify,
			  TkAtkAccessible_UnmapHandler, tkAccessible);
    Tk_CreateEventHandler(tkwin, FocusChangeMask,
			  TkAtkAccessible_FocusHandler, tkAccessible);


}


static void TkAtkAccessible_DestroyHandler(ClientData clientData, XEvent *eventPtr)
{
    if (eventPtr->type == DestroyNotify) {
        TkAtkAccessible *tkAccessible = (TkAtkAccessible *)clientData;




        if (tkAccessible && tkAccessible->tkwin) {
            AtkObject *parent = atk_object_get_parent(ATK_OBJECT(tkAccessible));
            if (parent && G_IS_OBJECT(parent)) {
                /* Compute index before removal. */
                gint index = g_list_index(((TkAtkAccessible *)parent)->children, ATK_OBJECT(tkAccessible));
                if (index >= 0) {
                    /* Remove from parent's children list. */
                    RemoveChildFromParent((TkAtkAccessible*)parent, ATK_OBJECT(tkAccessible));
                    
                    /* Emit children-changed::remove signal. */
                    ChildrenChangedRemoveData *crd = g_new0(ChildrenChangedRemoveData, 1);
                    crd->parent = parent;
                    crd->index = index;
                    crd->child = ATK_OBJECT(tkAccessible);
                    if (crd->parent) g_object_ref(crd->parent);
                    if (crd->child) g_object_ref(crd->child);  
                    RunOnMainThread(emit_children_changed_remove, crd);
                } else {
                    g_warning("TkAtkAccessible_DestroyHandler: Object not found in parent's children list");
                }
            } else {
                g_warning("TkAtkAccessible_DestroyHandler: Invalid or null parent object");
            }
            
            /* Unregister from tk_to_atk_map. */
            UnregisterAtkObjectForTkWindow(tkAccessible->tkwin);
            
            /* Unref the accessible object. */
            g_object_unref(tkAccessible);
        } else {
            g_warning("TkAtkAccessible_DestroyHandler: Invalid or null tkAccessible/tkwin");
        }
    }
}

/* Respond to <Confgure> event. */
static void TkAtkAccessible_ConfigureHandler(ClientData clientData, XEvent *eventPtr)
{
    if (eventPtr->type != ConfigureNotify) {
	return;
    }

    
    TkAtkAccessible *acc = (TkAtkAccessible *)clientData;
    if (!acc || !acc->tkwin) {
	g_warning("TkAtkAccessible_ConfigureHandler: Invalid or null acc/tkwin");
	return;
    }
    
    /* Update all caches with additional validation. */
    UpdateGeometryCache(acc);
    UpdateNameCache(acc);
    UpdateDescriptionCache(acc);
    UpdateValueCache(acc);
    UpdateRoleCache(acc);
    UpdateStateCache(acc);

    
    /* Notify ATK of changes only if geometry is valid and non-zero. */
    if (acc->width > 0 && acc->height > 0 && acc->is_mapped) {
        BoundsChangedData *bcd = g_new0(BoundsChangedData, 1);
        bcd->obj = ATK_OBJECT(acc);
        bcd->rect.x = acc->x;
        bcd->rect.y = acc->y;
        bcd->rect.width = acc->width;
        bcd->rect.height = acc->height;
        RunOnMainThreadAsync(emit_bounds_changed, bcd);
    }
    
    if (acc->cached_name) {
	tk_set_name(ATK_OBJECT(acc), acc->cached_name);
    }
    if (acc->cached_description) {
	atk_object_set_description(ATK_OBJECT(acc), acc->cached_description);
    }
    
    StateChangeData *visible_scd = g_new0(StateChangeData, 1);
    visible_scd->obj = ATK_OBJECT(acc);
    visible_scd->name = g_strdup("visible");
    visible_scd->state = acc->is_mapped;
    RunOnMainThreadAsync(emit_state_change, visible_scd);
    
    StateChangeData *showing_scd = g_new0(StateChangeData, 1);
    showing_scd->obj = ATK_OBJECT(acc);
    showing_scd->name = g_strdup("showing");
    showing_scd->state = acc->is_mapped;
    RunOnMainThreadAsync(emit_state_change, showing_scd);
}


/* Respond to <Map> event. */
static void TkAtkAccessible_MapHandler(ClientData clientData, XEvent *eventPtr)
{
    if (eventPtr->type != MapNotify) return;
    TkAtkAccessible *acc = (TkAtkAccessible *)clientData;
    if (!acc || !acc->tkwin) {







>
>


>
|
<

|
>
>
>
>
|
|
|
<
|
|
<
|
<
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
<
|
|
|


|

<
<
|
>
|





|
|






>
|
|
|
|
|
|
|
|
|
|

|






|





|






>







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
			  TkAtkAccessible_ConfigureHandler, tkAccessible);
    Tk_CreateEventHandler(tkwin, MapNotify,
			  TkAtkAccessible_MapHandler, tkAccessible);
    Tk_CreateEventHandler(tkwin, UnmapNotify,
			  TkAtkAccessible_UnmapHandler, tkAccessible);
    Tk_CreateEventHandler(tkwin, FocusChangeMask,
			  TkAtkAccessible_FocusHandler, tkAccessible);
	Tk_CreateEventHandler(tkwin, CreateNotify,
			  TkAtkAccessible_CreateHandler, tkAccessible);
}

/* Respond to <Destroy> events. */
static void TkAtkAccessible_DestroyHandler(ClientData clientData, XEvent *eventPtr) {

    if (eventPtr->type == DestroyNotify) {
	TkAtkAccessible *tkAccessible = (TkAtkAccessible *)clientData;
	if (!tkAccessible || !tkAccessible->tkwin) {
	    g_warning("TkAtkAccessible_DestroyHandler: Invalid or null tkAccessible/tkwin");
	    return;
	}

	AtkObject *parent = atk_object_get_parent(ATK_OBJECT(tkAccessible));
	if (parent && G_IS_OBJECT(parent)) {

	    gint index = g_list_index(((TkAtkAccessible *)parent)->children, tkAccessible);
	    if (index >= 0) {

		RemoveChildFromParent((TkAtkAccessible *)parent, ATK_OBJECT(tkAccessible));


		ChildrenChangedRemoveData *crd = g_new0(ChildrenChangedRemoveData, 1);
		crd->parent = parent;
		crd->index = index;
		crd->child = ATK_OBJECT(tkAccessible);
		if (crd->parent) g_object_ref(crd->parent);
		if (crd->child) g_object_ref(crd->child);
		RunOnMainThreadAsync(emit_children_changed_remove, crd);
	    } else {
		g_warning("TkAtkAccessible_DestroyHandler: Object not found in parent's children list");
	    }
	} else {
	    g_warning("TkAtkAccessible_DestroyHandler: Invalid or null parent object");
	}

	/* Unregister from tk_to_atk_map */
	UnregisterAtkObjectForTkWindow(tkAccessible->tkwin);

	/* Unref the accessible object */
	g_object_unref(tkAccessible);


    }
}


/* Respond to <Confgure> event. */
static void TkAtkAccessible_ConfigureHandler(ClientData clientData, XEvent *eventPtr) 
{


	
    if (eventPtr->type != ConfigureNotify) return;

    TkAtkAccessible *acc = (TkAtkAccessible *)clientData;
    if (!acc || !acc->tkwin) {
	g_warning("TkAtkAccessible_ConfigureHandler: Invalid or null acc/tkwin");
	return;
    }

    /* Update all caches. */
    UpdateGeometryCache(acc);
    UpdateNameCache(acc);
    UpdateDescriptionCache(acc);
    UpdateValueCache(acc);
    UpdateRoleCache(acc);
    UpdateStateCache(acc);
    UpdateChildrenCache(acc); /* Added to update children on configure */

    /* Notify ATK of changes */
    if (acc->width > 0 && a.cc->height > 0 && acc->is_mapped) {
	BoundsChangedData *bcd = g_new0(BoundsChangedData, 1);
	bcd->obj = ATK_OBJECT(acc);
	bcd->rect.x = acc->x;
	bcd->rect.y = acc->y;
	bcd->rect.width = acc->width;
	bcd->rect.height = acc->height;
	RunOnMainThreadAsync(emit_bounds_changed, bcd);
    }

    if (acc->cached_name) {
	tk_set_name(ATK_OBJECT(acc), acc->cached_name);
    }
    if (acc->cached_description) {
	atk_object_set_description(ATK_OBJECT(acc), acc->cached_description);
    }

    StateChangeData *visible_scd = g_new0(StateChangeData, 1);
    visible_scd->obj = ATK_OBJECT(acc);
    visible_scd->name = g_strdup("visible");
    visible_scd->state = acc->is_mapped;
    RunOnMainThreadAsync(emit_state_change, visible_scd);

    StateChangeData *showing_scd = g_new0(StateChangeData, 1);
    showing_scd->obj = ATK_OBJECT(acc);
    showing_scd->name = g_strdup("showing");
    showing_scd->state = acc->is_mapped;
    RunOnMainThreadAsync(emit_state_change, showing_scd);
}


/* Respond to <Map> event. */
static void TkAtkAccessible_MapHandler(ClientData clientData, XEvent *eventPtr)
{
    if (eventPtr->type != MapNotify) return;
    TkAtkAccessible *acc = (TkAtkAccessible *)clientData;
    if (!acc || !acc->tkwin) {
1849
1850
1851
1852
1853
1854
1855
















1856
1857
1858
1859
1860
1861
1862
        scd->state = FALSE;
        RunOnMainThreadAsync(emit_state_change, scd);
    }

    g_object_unref(state_set);
}



















/*
 *----------------------------------------------------------------------
 *
 * Tcl command implementations - expose these API's to Tcl scripts and 
 * initialize ATK integration in Tcl/Tk. 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
        scd->state = FALSE;
        RunOnMainThreadAsync(emit_state_change, scd);
    }

    g_object_unref(state_set);
}

/* Respond to <CreateNotify> events. */
static void TkAtkAccessible_CreateHandler(ClientData clientData, XEvent *eventPtr) 
{
	
    if (eventPtr->type != CreateNotify) return;

    TkAtkAccessible *acc = (TkAtkAccessible *)clientData;
    if (!acc || !acc->tkwin || !acc->interp) {
	g_warning("TkAtkAccessible_CreateHandler: Invalid or null acc/tkwin/interp");
	return;
    }

    /* Update children cache asynchronously. */
    RunOnMainThreadAsync((GSourceFunc)UpdateChildrenCache, acc);
}



/*
 *----------------------------------------------------------------------
 *
 * Tcl command implementations - expose these API's to Tcl scripts and 
 * initialize ATK integration in Tcl/Tk. 
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
    (void) clientData;

    if (objc < 2) {
	Tcl_WrongNumArgs(ip, 1, objv, "window?");
	return TCL_ERROR;
    }
    
    MainThreadData data = {NULL, ip, NULL, NULL};
    Tk_Window path_tkwin = (Tk_Window)RunOnMainThread(name_to_window_main, &data);
    if (path_tkwin == NULL) {
	return TCL_OK;
    }

    AtkObject *acc = GetAtkObjectForTkWindow(path_tkwin);








|







1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
    (void) clientData;

    if (objc < 2) {
	Tcl_WrongNumArgs(ip, 1, objv, "window?");
	return TCL_ERROR;
    }
    
    MainThreadData data = {NULL, ip, NULL, NULL, NULL};
    Tk_Window path_tkwin = (Tk_Window)RunOnMainThread(name_to_window_main, &data);
    if (path_tkwin == NULL) {
	return TCL_OK;
    }

    AtkObject *acc = GetAtkObjectForTkWindow(path_tkwin);

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
{
    (void)clientData;
    if (objc < 2) {
        Tcl_WrongNumArgs(interp, 1, objv, "window");
        return TCL_ERROR;
    }

    MainThreadData data = {NULL, interp, NULL, NULL};
    Tk_Window path_tkwin = (Tk_Window)RunOnMainThread(name_to_window_main, &data);
    if (path_tkwin == NULL) {
	return TCL_OK;
    }

    AtkObject *acc = GetAtkObjectForTkWindow(path_tkwin);
    if (!acc) {
        acc = TkCreateAccessibleAtkObject(interp, path_tkwin, Tcl_GetString(objv[1]));
        if (!acc) {
	    g_warning("Failed to create accessible object\n");
            return TCL_OK;
        }
        
        RegisterAtkObjectForTkWindow(path_tkwin, acc);

        if ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &(MainThreadData){path_tkwin, interp, NULL, NULL})) {
            RegisterToplevelWindow(interp, path_tkwin, acc);
        } else {
            Tk_Window parent_tkwin = (Tk_Window)RunOnMainThread(get_window_parent_main, &(MainThreadData){path_tkwin, interp, NULL, NULL});
            AtkObject *parent_obj = parent_tkwin ? GetAtkObjectForTkWindow(parent_tkwin) : tk_root_accessible;
            if (parent_obj) {
                atk_object_set_parent(acc, parent_obj);
                RunOnMainThread(emit_children_changed_add, acc);
            }
        }
    }







|















|


|







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
{
    (void)clientData;
    if (objc < 2) {
        Tcl_WrongNumArgs(interp, 1, objv, "window");
        return TCL_ERROR;
    }

    MainThreadData data = {NULL, interp, NULL, NULL, NULL};
    Tk_Window path_tkwin = (Tk_Window)RunOnMainThread(name_to_window_main, &data);
    if (path_tkwin == NULL) {
	return TCL_OK;
    }

    AtkObject *acc = GetAtkObjectForTkWindow(path_tkwin);
    if (!acc) {
        acc = TkCreateAccessibleAtkObject(interp, path_tkwin, Tcl_GetString(objv[1]));
        if (!acc) {
	    g_warning("Failed to create accessible object\n");
            return TCL_OK;
        }
        
        RegisterAtkObjectForTkWindow(path_tkwin, acc);

        if ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &(MainThreadData){path_tkwin, interp, NULL, NULL, NULL})) {
            RegisterToplevelWindow(interp, path_tkwin, acc);
        } else {
            Tk_Window parent_tkwin = (Tk_Window)RunOnMainThread(get_window_parent_main, &(MainThreadData){path_tkwin, interp, NULL, NULL, NULL});
            AtkObject *parent_obj = parent_tkwin ? GetAtkObjectForTkWindow(parent_tkwin) : tk_root_accessible;
            if (parent_obj) {
                atk_object_set_parent(acc, parent_obj);
                RunOnMainThread(emit_children_changed_add, acc);
            }
        }
    }
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085

    char *windowName = Tcl_GetString(objv[1]);
    if (!windowName) {
	Tcl_SetResult(interp, "Window name cannot be null.", TCL_STATIC);
	return TCL_ERROR;
    }

    MainThreadData data = {NULL, interp, NULL, windowName};
    Tk_Window tkwin = (Tk_Window)RunOnMainThread(name_to_window_main, &data);


    if (tkwin == NULL) {
	Tcl_SetResult(interp, "Invalid window name.", TCL_STATIC);
	return TCL_ERROR;
    }







|







2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102

    char *windowName = Tcl_GetString(objv[1]);
    if (!windowName) {
	Tcl_SetResult(interp, "Window name cannot be null.", TCL_STATIC);
	return TCL_ERROR;
    }

    MainThreadData data = {NULL, interp, NULL, windowName, NULL};
    Tk_Window tkwin = (Tk_Window)RunOnMainThread(name_to_window_main, &data);


    if (tkwin == NULL) {
	Tcl_SetResult(interp, "Invalid window name.", TCL_STATIC);
	return TCL_ERROR;
    }
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
	return TCL_ERROR;
    }

    /* Register for cleanup, mapping and other events. */
    TkAtkAccessible_RegisterEventHandlers(tkwin, accessible);

    /* Handle toplevels. */
    if ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &(MainThreadData){tkwin, interp, NULL, NULL})) {
	RegisterToplevelWindow(interp, tkwin, (AtkObject*)accessible);
    }

    return TCL_OK;
}
#endif








|







2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
	return TCL_ERROR;
    }

    /* Register for cleanup, mapping and other events. */
    TkAtkAccessible_RegisterEventHandlers(tkwin, accessible);

    /* Handle toplevels. */
    if ((gboolean)(uintptr_t)RunOnMainThread(is_toplevel_main, &(MainThreadData){tkwin, interp, NULL, NULL, NULL})) {
	RegisterToplevelWindow(interp, tkwin, (AtkObject*)accessible);
    }

    return TCL_OK;
}
#endif