Tk Source Code

Check-in [8fb84545]
Login

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

Overview
Comment:Address some runtime errors/crashes with MSVC
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | tka11y
Files: files | file ages | folders
SHA3-256: 8fb84545861b1307c2d66139b20e7744b74ce432e5675f40a417b8c24a7ea2e4
User & Date: kevin_walzer 2025-04-26 02:15:15.329
Context
2025-04-26
02:21
Remove a couple of debugging lines check-in: a674d7e0 user: kevin_walzer tags: tka11y
02:15
Address some runtime errors/crashes with MSVC check-in: 8fb84545 user: kevin_walzer tags: tka11y
2025-04-22
20:55
Minor tweaks check-in: 5acfd120 user: kevin_walzer tags: tka11y
Changes
Unified Diff Ignore Whitespace Patch
Changes to generic/tkAccessibility.c.
93
94
95
96
97
98
99


100
101
102
103
104
105
106
   * Ensure it is unique to that widget.
   */
  hPtr=Tcl_CreateHashEntry(TkAccessibilityObject, win, &isNew);
  if (isNew) {
    AccessibleAttributes = (Tcl_HashTable *)ckalloc(sizeof(Tcl_HashTable));
    Tcl_InitHashTable(AccessibleAttributes,TCL_STRING_KEYS);
    Tcl_SetHashValue(hPtr, AccessibleAttributes);


  }

  /* Set accessible role for window.  */
  hPtr2 =  Tcl_CreateHashEntry(AccessibleAttributes, "role", &isNew);
  if (!isNew) {
    Tcl_DecrRefCount(Tcl_GetHashValue(hPtr2));
  }







>
>







93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
   * Ensure it is unique to that widget.
   */
  hPtr=Tcl_CreateHashEntry(TkAccessibilityObject, win, &isNew);
  if (isNew) {
    AccessibleAttributes = (Tcl_HashTable *)ckalloc(sizeof(Tcl_HashTable));
    Tcl_InitHashTable(AccessibleAttributes,TCL_STRING_KEYS);
    Tcl_SetHashValue(hPtr, AccessibleAttributes);
  } else {
	 AccessibleAttributes = (Tcl_HashTable *)Tcl_GetHashValue(hPtr);
  }

  /* Set accessible role for window.  */
  hPtr2 =  Tcl_CreateHashEntry(AccessibleAttributes, "role", &isNew);
  if (!isNew) {
    Tcl_DecrRefCount(Tcl_GetHashValue(hPtr2));
  }
Changes to win/tkWinAccessibility.c.
22
23
24
25
26
27
28

29
30
31
32
33
34
35

/* Define the GUID for the MSAA interface. */
DEFINE_GUID(IID_IAccessible, 0x618736e0, 0x3c3d, 0x11cf, 0x81, 0xc, 0x0, 0xaa, 0x0, 0x38, 0x9b, 0x71);

/* Data declarations used in this file. */
typedef struct {
  IAccessibleVtbl *lpVtbl;

  Tcl_Interp *interp;
  HWND hwnd;
  char *pathName;
  LONG refCount;
} TkWinAccessible;

/* Map script-level roles to C roles. */







>







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

/* Define the GUID for the MSAA interface. */
DEFINE_GUID(IID_IAccessible, 0x618736e0, 0x3c3d, 0x11cf, 0x81, 0xc, 0x0, 0xaa, 0x0, 0x38, 0x9b, 0x71);

/* Data declarations used in this file. */
typedef struct {
  IAccessibleVtbl *lpVtbl;
  Tk_Window win; 
  Tcl_Interp *interp;
  HWND hwnd;
  char *pathName;
  LONG refCount;
} TkWinAccessible;

/* Map script-level roles to C roles. */
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  {"Tree", ROLE_SYSTEM_OUTLINE},
  {NULL, 0}
};

/* Hash table for managing accessibility attributes. */
extern Tcl_HashTable *TkAccessibilityObject;

/* Tk window with the accessibility attributes. */
Tk_Window accessible_win;

/* Tcl command passed to event procedure. */
char *callback_command;

/* Focused accessible widget object. */
static LONG g_focusedChildId = 0;

/* Map Tk windows to MSAA ID's. */







<
<
<







59
60
61
62
63
64
65



66
67
68
69
70
71
72
  {"Tree", ROLE_SYSTEM_OUTLINE},
  {NULL, 0}
};

/* Hash table for managing accessibility attributes. */
extern Tcl_HashTable *TkAccessibilityObject;




/* Tcl command passed to event procedure. */
char *callback_command;

/* Focused accessible widget object. */
static LONG g_focusedChildId = 0;

/* Map Tk windows to MSAA ID's. */
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
void ForceTkWidgetFocus(HWND hwnd, LONG childId);
LONG RegisterTkWidget(Tk_Window tkwin);
LONG GetChildIdForTkWindow(Tk_Window tkwin);
Tk_Window GetTkWindowForChildId(LONG childId);
static HWND GetWidgetHWNDIfPresent(Tk_Window tkwin);
Tk_Window GetToplevelOfWidget(Tk_Window tkwin);
LRESULT CALLBACK TkWinAccessible_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
void InstallCustomWndProcForMSAA(Tk_Window tkwin);
int IsScreenReaderRunning(ClientData clientData, Tcl_Interp *interp, int argc, Tcl_Obj *const argv[]);
int EmitSelectionChanged(ClientData clientData,Tcl_Interp *ip, int objc, Tcl_Obj *const objv[]);
void TkWinAccessible_RegisterForCleanup(Tk_Window tkwin, void *tkAccessible);
static void TkWinAccessible_DestroyHandler(ClientData clientData, XEvent *eventPtr);
void TkWinAccessible_RegisterForFocus(Tk_Window tkwin, void *tkAccessible);
static void TkWinAccessible_FocusEventHandler (ClientData clientData, XEvent *eventPtr);
int TkWinAccessibleObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]);







<







125
126
127
128
129
130
131

132
133
134
135
136
137
138
void ForceTkWidgetFocus(HWND hwnd, LONG childId);
LONG RegisterTkWidget(Tk_Window tkwin);
LONG GetChildIdForTkWindow(Tk_Window tkwin);
Tk_Window GetTkWindowForChildId(LONG childId);
static HWND GetWidgetHWNDIfPresent(Tk_Window tkwin);
Tk_Window GetToplevelOfWidget(Tk_Window tkwin);
LRESULT CALLBACK TkWinAccessible_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

int IsScreenReaderRunning(ClientData clientData, Tcl_Interp *interp, int argc, Tcl_Obj *const argv[]);
int EmitSelectionChanged(ClientData clientData,Tcl_Interp *ip, int objc, Tcl_Obj *const objv[]);
void TkWinAccessible_RegisterForCleanup(Tk_Window tkwin, void *tkAccessible);
static void TkWinAccessible_DestroyHandler(ClientData clientData, XEvent *eventPtr);
void TkWinAccessible_RegisterForFocus(Tk_Window tkwin, void *tkAccessible);
static void TkWinAccessible_FocusEventHandler (ClientData clientData, XEvent *eventPtr);
int TkWinAccessibleObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]);
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367

368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386




387
388
389
390

391


392
393


394
395
396
397
398
399
400





401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/* Function to map accessible name to MSAA.*/
static HRESULT STDMETHODCALLTYPE TkWinAccessible_get_accName(IAccessible *this, VARIANT varChild, BSTR *pszName)
{
  TkWinAccessible *tkAccessible = (TkWinAccessible *)this;

  if (varChild.vt != VT_I4 || varChild.lVal == CHILDID_SELF) {
		
    Tk_Window win = accessible_win;
    Tcl_HashEntry *hPtr, *hPtr2;
    Tcl_HashTable *AccessibleAttributes;
    Tcl_DString ds;
	
    hPtr=Tcl_FindHashEntry(TkAccessibilityObject, win);
    if (!hPtr) {
      return E_INVALIDARG;
    }
	
    /* 
     * Assign the "description" attribute to the name because it is 
     * more detailed - MSAA generally does not provide both the 
     * name and description. 
     */
    AccessibleAttributes = Tcl_GetHashValue(hPtr);
    hPtr2=Tcl_FindHashEntry(AccessibleAttributes, "description");
    if (!hPtr2) {
      return E_INVALIDARG;
    }
	
    char *result = Tcl_GetString(Tcl_GetHashValue(hPtr2));
    Tcl_DStringInit(&ds);
    if (result) {
      *pszName = SysAllocString(Tcl_UtfToWCharDString(result, -1, &ds));
    } else {
      *pszName = SysAllocString(Tcl_UtfToWCharDString(tkAccessible->pathName, -1, &ds));
    }
    Tcl_DStringFree(&ds);
    return S_OK;
  }
  return E_INVALIDARG;
}

/* Function to map accessible role to MSAA.*/

static HRESULT STDMETHODCALLTYPE TkWinAccessible_get_accRole(IAccessible *this, VARIANT varChild, VARIANT *pvarRole)
{
  TkWinAccessible *tkAccessible = (TkWinAccessible *)this;
  LONG role =  0;

  if (!pvarRole) return E_INVALIDARG;

  if (varChild.vt != VT_I4 || varChild.lVal == CHILDID_SELF) {
    Tk_Window win = accessible_win;
		
    Tcl_HashEntry *hPtr, *hPtr2;
    Tcl_HashTable *AccessibleAttributes;
		
    hPtr=Tcl_FindHashEntry(TkAccessibilityObject, win);
    if (!hPtr) {
      return E_INVALIDARG;
    }
		
    AccessibleAttributes = Tcl_GetHashValue(hPtr);




    hPtr2=Tcl_FindHashEntry(AccessibleAttributes, "role");
    if (!hPtr2) {
      return E_INVALIDARG;
    }

    char *result = Tcl_GetString(Tcl_GetHashValue(hPtr2));


    const char *tkrole = NULL;
    if (tkrole) {


      for (int i = 0; roleMap[i].tkrole !=NULL; i++) {
	if (strcmp(roleMap[i].tkrole, tkrole) == 0) {
	  role = roleMap[i].winrole;
	  break;
	}
      }
    }





    pvarRole->vt = VT_I4;
    pvarRole->lVal = role;
    return S_OK;
  }
  return E_INVALIDARG;
}

/* Function to map accessible state to MSAA. */
static HRESULT STDMETHODCALLTYPE TkWinAccessible_get_accState(IAccessible *this, VARIANT varChild, VARIANT *pvarState)
{
  TkWinAccessible *tkAccessible = (TkWinAccessible *)this;
  LONG state;
	
  if (varChild.vt != VT_I4 || varChild.lVal == CHILDID_SELF) {
    /* Check if Tk state is disabled. If so, ignore accessible atribute. */
    Tk_Window win = accessible_win;
    Tcl_HashEntry *hPtr, *hPtr2;
    Tcl_HashTable *AccessibleAttributes;
		
    hPtr=Tcl_FindHashEntry(TkAccessibilityObject, win);
    if (!hPtr) {
      return E_INVALIDARG;
    }
		
    AccessibleAttributes = Tcl_GetHashValue(hPtr);
    hPtr2=Tcl_FindHashEntry(AccessibleAttributes, "state");
    if (!hPtr2) {
      return E_INVALIDARG;
    }
		
    char *result = Tcl_GetString(Tcl_GetHashValue(hPtr2));
    if (strcmp(result, "disabled") == 0) {







|

<












|



















>
|


<


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









|

<






|







323
324
325
326
327
328
329
330
331

332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367

368
369

370

371
372

373

374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410

411
412
413
414
415
416
417
418
419
420
421
422

423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* Function to map accessible name to MSAA.*/
static HRESULT STDMETHODCALLTYPE TkWinAccessible_get_accName(IAccessible *this, VARIANT varChild, BSTR *pszName)
{
  TkWinAccessible *tkAccessible = (TkWinAccessible *)this;

  if (varChild.vt != VT_I4 || varChild.lVal == CHILDID_SELF) {
		
    Tk_Window win = tkAccessible->win;
    Tcl_HashEntry *hPtr, *hPtr2;

    Tcl_DString ds;
	
    hPtr=Tcl_FindHashEntry(TkAccessibilityObject, win);
    if (!hPtr) {
      return E_INVALIDARG;
    }
	
    /* 
     * Assign the "description" attribute to the name because it is 
     * more detailed - MSAA generally does not provide both the 
     * name and description. 
     */
    Tcl_HashTable *AccessibleAttributes = Tcl_GetHashValue(hPtr);
    hPtr2=Tcl_FindHashEntry(AccessibleAttributes, "description");
    if (!hPtr2) {
      return E_INVALIDARG;
    }
	
    char *result = Tcl_GetString(Tcl_GetHashValue(hPtr2));
    Tcl_DStringInit(&ds);
    if (result) {
      *pszName = SysAllocString(Tcl_UtfToWCharDString(result, -1, &ds));
    } else {
      *pszName = SysAllocString(Tcl_UtfToWCharDString(tkAccessible->pathName, -1, &ds));
    }
    Tcl_DStringFree(&ds);
    return S_OK;
  }
  return E_INVALIDARG;
}

/* Function to map accessible role to MSAA.*/
static HRESULT STDMETHODCALLTYPE
TkWinAccessible_get_accRole(IAccessible *this, VARIANT varChild, VARIANT *pvarRole)
{
  TkWinAccessible *tkAccessible = (TkWinAccessible *)this;


  if (!pvarRole) return E_INVALIDARG;

  if (varChild.vt != VT_I4 || varChild.lVal != CHILDID_SELF) return E_INVALIDARG;


  Tcl_HashEntry *hPtr = Tcl_FindHashEntry(TkAccessibilityObject, tkAccessible->win);

  

  if (!hPtr) {
    return E_INVALIDARG;
  }

  Tcl_HashTable *AccessibleAttributes = Tcl_GetHashValue(hPtr);
  if (!AccessibleAttributes) {
    return E_INVALIDARG;
  }
 
  Tcl_HashEntry *hPtr2 = Tcl_FindHashEntry(AccessibleAttributes, "role");
  if (!hPtr2) {
    return E_INVALIDARG;
  } 

  Tcl_Obj *roleObj = Tcl_GetHashValue(hPtr2);
  if (!roleObj) return E_INVALIDARG;

  const char *tkrole = Tcl_GetString(roleObj);
  if (!tkrole) return E_INVALIDARG;

  LONG role = 0;
  for (int i = 0; roleMap[i].tkrole != NULL; i++) {
    if (strcmp(roleMap[i].tkrole, tkrole) == 0) {
      role = roleMap[i].winrole;
      break;
    }
  }
  
  if (role == ROLE_SYSTEM_PUSHBUTTON) {
    MessageBoxW(NULL, L"Role", L"Role is button", MB_OK | MB_ICONINFORMATION); 
  }


  pvarRole->vt = VT_I4;
  pvarRole->lVal = role;
  return S_OK;
}



/* Function to map accessible state to MSAA. */
static HRESULT STDMETHODCALLTYPE TkWinAccessible_get_accState(IAccessible *this, VARIANT varChild, VARIANT *pvarState)
{
  TkWinAccessible *tkAccessible = (TkWinAccessible *)this;
  LONG state;
	
  if (varChild.vt != VT_I4 || varChild.lVal == CHILDID_SELF) {
    /* Check if Tk state is disabled. If so, ignore accessible atribute. */
    Tk_Window win = tkAccessible->win;
    Tcl_HashEntry *hPtr, *hPtr2;

		
    hPtr=Tcl_FindHashEntry(TkAccessibilityObject, win);
    if (!hPtr) {
      return E_INVALIDARG;
    }
		
    Tcl_HashTable *AccessibleAttributes = Tcl_GetHashValue(hPtr);
    hPtr2=Tcl_FindHashEntry(AccessibleAttributes, "state");
    if (!hPtr2) {
      return E_INVALIDARG;
    }
		
    char *result = Tcl_GetString(Tcl_GetHashValue(hPtr2));
    if (strcmp(result, "disabled") == 0) {
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
static HRESULT STDMETHODCALLTYPE TkWinAccessible_get_accValue(IAccessible *this, VARIANT varChild, BSTR *pszValue)
{
  if (!pszValue) return E_INVALIDARG;
	
  TkWinAccessible *tkAccessible = (TkWinAccessible *)this;
  if (varChild.vt != VT_I4 || varChild.lVal == CHILDID_SELF) {
		
    Tk_Window win = accessible_win;
    Tcl_HashEntry *hPtr, *hPtr2;
    Tcl_HashTable *AccessibleAttributes;
    Tcl_DString ds;
		
    hPtr=Tcl_FindHashEntry(TkAccessibilityObject, win);
    if (!hPtr) {
      return E_INVALIDARG;
    }
		
    AccessibleAttributes = Tcl_GetHashValue(hPtr);
    hPtr2=Tcl_FindHashEntry(AccessibleAttributes, "value");
    if (!hPtr2) {
      return E_INVALIDARG;
    }
		
    char *result = Tcl_GetString(Tcl_GetHashValue(hPtr2));
    if (result) {
      Tcl_DStringInit(&ds);
      *pszValue = SysAllocString(Tcl_UtfToWCharDString(result, -1, &ds));
      Tcl_DStringFree(&ds);
    }
    return S_OK;
  }
  return E_INVALIDARG;
}

/*
 * Function to set Tk toplevel as the primary parent of acccessible 
 * widgets. We want a flat hierarchy to simplify implementation. 
 */
  static HRESULT STDMETHODCALLTYPE TkWinAccessible_get_accParent(IAccessible *this, IDispatch **ppdispParent)
  {
    if (!this || !ppdispParent) return E_INVALIDARG;
  
    TkWinAccessible *tkAccessible = (TkWinAccessible *)this;
    *ppdispParent = NULL;
  
    Tk_Window widget = Tk_NameToWindow(
				       tkAccessible->interp,
				       tkAccessible->pathName,
				       Tk_MainWindow(tkAccessible->interp)
				       );
    if (!widget) return E_FAIL;
  
    /* If this is already a toplevel, it has no parent.*/
    if (Tk_IsTopLevel(widget)) {
      return S_FALSE; /* No parent.*/
    }
  
    /* Otherwise, return the toplevel's accessible object.*/
    Tk_Window toplevel = GetToplevelOfWidget(widget);
    if (!toplevel) return E_FAIL;
  
    const char *toplevelPath = Tk_PathName(toplevel);
    if (!toplevelPath) return E_FAIL;
  
    TkWinAccessible *parentAccessible = create_tk_accessible(
							     tkAccessible->interp,
							     Tk_GetHWND(Tk_WindowId(toplevel)), /* HWND is valid for toplevels.*/
							     toplevelPath
							     );
  
    if (!parentAccessible) return E_OUTOFMEMORY;
  
    *ppdispParent = (IDispatch *)parentAccessible;
  
    ((IAccessible *)parentAccessible)->lpVtbl->AddRef((IAccessible *)parentAccessible);
    return S_OK;
  }
  
/* Function to get number of accessible children to MSAA. */
static HRESULT STDMETHODCALLTYPE TkWinAccessible_get_accChildCount(IAccessible *this, LONG *pcChildren)
{
    if (!pcChildren || !this) return E_INVALIDARG;

    TkWinAccessible *tkAccessible = (TkWinAccessible *)this;

    Tk_Window toplevel = Tk_NameToWindow(
        tkAccessible->interp,
        tkAccessible->pathName,
        Tk_MainWindow(tkAccessible->interp)
    );
    if (!toplevel || !Tk_IsTopLevel(toplevel)) {
        return E_FAIL;
    }

    int count = 0;
    TkWindow *winPtr = (TkWindow *)toplevel;

    for (TkWindow *child = winPtr->childList; child != NULL; child = child->nextPtr) {
        if (!Tk_IsMapped((Tk_Window)child)) continue;

        const char *className = Tk_Class((Tk_Window)child);
        if (className && strcmp(className, "Menu") == 0) continue;

        count++;
    }

    *pcChildren = count;
    return S_OK;
}


/* Function to get accessible children to MSAA. */
static HRESULT STDMETHODCALLTYPE TkWinAccessible_get_accChild(IAccessible *this, VARIANT varChild, IDispatch **ppdispChild)
{
    if (!this || !ppdispChild) {
        return E_INVALIDARG;
    }

    *ppdispChild = NULL;

    if (varChild.vt != VT_I4 || varChild.lVal <= 0) {
        return E_INVALIDARG;
    }

    TkWinAccessible *tkAccessible = (TkWinAccessible *)this;

    /* Resolve the toplevel window from the stored pathName. */
    Tk_Window toplevel = Tk_NameToWindow(tkAccessible->interp, tkAccessible->pathName, Tk_MainWindow(tkAccessible->interp));
    if (!toplevel || !Tk_IsTopLevel(toplevel)) {
        return E_FAIL;
    }

    /* Enumerate mapped, non-menu children. */
    int index = 0;
    TkWindow *winPtr = (TkWindow *)toplevel;

    for (TkWindow *child = winPtr->childList; child != NULL; child = child->nextPtr) {
        if (!Tk_IsMapped((Tk_Window)child)) continue;

        const char *className = Tk_Class((Tk_Window)child);
        if (className && strcmp(className, "Menu") == 0) continue;

        index++;
        if (index == varChild.lVal) {
            TkWinAccessible *childAccessible = create_tk_accessible(
                tkAccessible->interp,
                GetWidgetHWNDIfPresent((Tk_Window)child), /*hwnd if valid*/
                Tk_PathName((Tk_Window)child)
            );

            if (!childAccessible) return E_OUTOFMEMORY;

            *ppdispChild = (IDispatch *)childAccessible;

            /* Increase reference count to match COM expectations. */
            ((IAccessible *)childAccessible)->lpVtbl->AddRef((IAccessible *)childAccessible);

            return S_OK;
        }
    }
    return E_INVALIDARG;
}

/* Function to get accessible frame to MSAA. */
static HRESULT STDMETHODCALLTYPE TkWinAccessible_accLocation(IAccessible *this, LONG *pxLeft, LONG *pyTop, LONG *pcxWidth, LONG *pcyHeight, VARIANT varChild)
{
  TkWinAccessible *tkAccessible = (TkWinAccessible *)this;
  if (varChild.vt != VT_I4 || varChild.lVal == CHILDID_SELF) {







|

<







|




















|
|
|

|
|

|
|
|
|
|
|

|
|
|
|

|
|
|

|
|

|
|
|
|
|
|
|

|

|
|
|




|

|

|
|
|
|
|
|
|
|

|
|

|
|
<
|
|
<
|
|

|
|

<




|
|
|

|

|
|
|

|

|
|
|
|
|

|
|
|

|
|

|
|

|
|
|
|
|
|
|

|

|

|
|

|
|
|
|







449
450
451
452
453
454
455
456
457

458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546

547
548

549
550
551
552
553
554

555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
static HRESULT STDMETHODCALLTYPE TkWinAccessible_get_accValue(IAccessible *this, VARIANT varChild, BSTR *pszValue)
{
  if (!pszValue) return E_INVALIDARG;
	
  TkWinAccessible *tkAccessible = (TkWinAccessible *)this;
  if (varChild.vt != VT_I4 || varChild.lVal == CHILDID_SELF) {
		
    Tk_Window win = tkAccessible->win;
    Tcl_HashEntry *hPtr, *hPtr2;

    Tcl_DString ds;
		
    hPtr=Tcl_FindHashEntry(TkAccessibilityObject, win);
    if (!hPtr) {
      return E_INVALIDARG;
    }
		
    Tcl_HashTable *AccessibleAttributes = Tcl_GetHashValue(hPtr);
    hPtr2=Tcl_FindHashEntry(AccessibleAttributes, "value");
    if (!hPtr2) {
      return E_INVALIDARG;
    }
		
    char *result = Tcl_GetString(Tcl_GetHashValue(hPtr2));
    if (result) {
      Tcl_DStringInit(&ds);
      *pszValue = SysAllocString(Tcl_UtfToWCharDString(result, -1, &ds));
      Tcl_DStringFree(&ds);
    }
    return S_OK;
  }
  return E_INVALIDARG;
}

/*
 * Function to set Tk toplevel as the primary parent of acccessible 
 * widgets. We want a flat hierarchy to simplify implementation. 
 */
static HRESULT STDMETHODCALLTYPE TkWinAccessible_get_accParent(IAccessible *this, IDispatch **ppdispParent)
{
  if (!this || !ppdispParent) return E_INVALIDARG;
  
  TkWinAccessible *tkAccessible = (TkWinAccessible *)this;
  *ppdispParent = NULL;
  
  Tk_Window widget = Tk_NameToWindow(
				     tkAccessible->interp,
				     tkAccessible->pathName,
				     Tk_MainWindow(tkAccessible->interp)
				     );
  if (!widget) return E_FAIL;
  
  /* If this is already a toplevel, it has no parent.*/
  if (Tk_IsTopLevel(widget)) {
    return S_FALSE; /* No parent.*/
  }
  
  /* Otherwise, return the toplevel's accessible object.*/
  Tk_Window toplevel = GetToplevelOfWidget(widget);
  if (!toplevel) return E_FAIL;
  
  const char *toplevelPath = Tk_PathName(toplevel);
  if (!toplevelPath) return E_FAIL;
  
  TkWinAccessible *parentAccessible = create_tk_accessible(
							   tkAccessible->interp,
							   Tk_GetHWND(Tk_WindowId(toplevel)), /* HWND is valid for toplevels.*/
							   toplevelPath
							   );
                
  if (!parentAccessible) return E_OUTOFMEMORY;
  
  *ppdispParent = (IDispatch *)parentAccessible;
  
  ((IAccessible *)parentAccessible)->lpVtbl->AddRef((IAccessible *)parentAccessible);
  return S_OK;
}
  
/* Function to get number of accessible children to MSAA. */
static HRESULT STDMETHODCALLTYPE TkWinAccessible_get_accChildCount(IAccessible *this, LONG *pcChildren)
{
  if (!pcChildren || !this) return E_INVALIDARG;

  TkWinAccessible *tkAccessible = (TkWinAccessible *)this;

  Tk_Window toplevel = Tk_NameToWindow(
				       tkAccessible->interp,
				       tkAccessible->pathName,
				       Tk_MainWindow(tkAccessible->interp)
				       );
  if (!toplevel || !Tk_IsTopLevel(toplevel)) {
    return E_FAIL;
  }

  int count = 0;
  TkWindow *winPtr = (TkWindow *)toplevel;

  for (TkWindow *child = winPtr->childList; child != NULL; child = child->nextPtr) {
    if (!Tk_IsMapped((Tk_Window)child)) continue;

    const char *className = Tk_Class((Tk_Window)child);
    if (className && strcmp(className, "Menu") == 0) continue;

    count++;
  }

  *pcChildren = count;
  return S_OK;
}


/* Function to get accessible children to MSAA. */
static HRESULT STDMETHODCALLTYPE TkWinAccessible_get_accChild(IAccessible *this, VARIANT varChild, IDispatch **ppdispChild)
{
  if (!this || !ppdispChild) {
    return E_INVALIDARG;
  }

  *ppdispChild = NULL;

  if (varChild.vt != VT_I4 || varChild.lVal <= 0) {
    return E_INVALIDARG;
  }

  TkWinAccessible *tkAccessible = (TkWinAccessible *)this;

  /* Resolve the toplevel window from the stored pathName. */
  Tk_Window toplevel = Tk_NameToWindow(tkAccessible->interp, tkAccessible->pathName, Tk_MainWindow(tkAccessible->interp));
  if (!toplevel || !Tk_IsTopLevel(toplevel)) {
    return E_FAIL;
  }

  /* Enumerate mapped, non-menu children. */
  int index = 0;
  TkWindow *winPtr = (TkWindow *)toplevel;

  for (TkWindow *child = winPtr->childList; child != NULL; child = child->nextPtr) {
    if (!Tk_IsMapped((Tk_Window)child)) continue;

    const char *className = Tk_Class((Tk_Window)child);
    if (className && strcmp(className, "Menu") == 0) continue;

    index++;
    if (index == varChild.lVal) {
      TkWinAccessible *childAccessible = create_tk_accessible(
							      tkAccessible->interp,
							      GetWidgetHWNDIfPresent((Tk_Window)child), /*hwnd if valid*/
							      Tk_PathName((Tk_Window)child)
							      );

      if (!childAccessible) return E_OUTOFMEMORY;

      *ppdispChild = (IDispatch *)childAccessible;

      /* Increase reference count to match COM expectations. */
      ((IAccessible *)childAccessible)->lpVtbl->AddRef((IAccessible *)childAccessible);

      return S_OK;
    }
  }
  return E_INVALIDARG;
}

/* Function to get accessible frame to MSAA. */
static HRESULT STDMETHODCALLTYPE TkWinAccessible_accLocation(IAccessible *this, LONG *pxLeft, LONG *pyTop, LONG *pcxWidth, LONG *pcyHeight, VARIANT varChild)
{
  TkWinAccessible *tkAccessible = (TkWinAccessible *)this;
  if (varChild.vt != VT_I4 || varChild.lVal == CHILDID_SELF) {
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
  return E_INVALIDARG;
}

/* Function to get button press to MSAA. */
static HRESULT STDMETHODCALLTYPE TkWinAccessible_accDoDefaultAction(IAccessible *this, VARIANT varChild)
{
  TkWinAccessible *tkAccessible = (TkWinAccessible *)this;  
  Tk_Window win = accessible_win;
  Tcl_HashEntry *hPtr, *hPtr2;
  Tcl_HashTable *AccessibleAttributes;
  Tcl_Event *event; 

  hPtr=Tcl_FindHashEntry(TkAccessibilityObject, win);
  if (!hPtr) {
    return E_INVALIDARG;







|







625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
  return E_INVALIDARG;
}

/* Function to get button press to MSAA. */
static HRESULT STDMETHODCALLTYPE TkWinAccessible_accDoDefaultAction(IAccessible *this, VARIANT varChild)
{
  TkWinAccessible *tkAccessible = (TkWinAccessible *)this;  
  Tk_Window win = tkAccessible->win;
  Tcl_HashEntry *hPtr, *hPtr2;
  Tcl_HashTable *AccessibleAttributes;
  Tcl_Event *event; 

  hPtr=Tcl_FindHashEntry(TkAccessibilityObject, win);
  if (!hPtr) {
    return E_INVALIDARG;
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677

678
679
680
681
682
683
684
685
  return S_OK;  
}

/*
 * Event proc which implements the action event procedure.
 */

 static int TkWinAccessible_ActionEventHandler(Tcl_Event *evtPtr, int flags)
{
  /* MSVC complains about TCL_UNUSED. */
  (void)evtPtr;
  (void)flags;

  TkMainInfo *info = TkGetMainInfoList();
  Tcl_GlobalEval(info->interp, callback_command);
  return 1;
}

/* Function to get accessible help to MSAA. */
static HRESULT STDMETHODCALLTYPE TkWinAccessible_get_accHelp(IAccessible *this, VARIANT varChild, BSTR* pszHelp)
{
  if (varChild.vt != VT_I4 || varChild.lVal != CHILDID_SELF) {
    return E_INVALIDARG;
  }


  Tk_Window win = accessible_win;
  Tcl_HashEntry *hPtr, *hPtr2;
  Tcl_HashTable *AccessibleAttributes;
  Tcl_DString ds;
		
  hPtr=Tcl_FindHashEntry(TkAccessibilityObject, win);
  if (!hPtr) {
    return E_INVALIDARG;







|

















>
|







653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
  return S_OK;  
}

/*
 * Event proc which implements the action event procedure.
 */

static int TkWinAccessible_ActionEventHandler(Tcl_Event *evtPtr, int flags)
{
  /* MSVC complains about TCL_UNUSED. */
  (void)evtPtr;
  (void)flags;

  TkMainInfo *info = TkGetMainInfoList();
  Tcl_GlobalEval(info->interp, callback_command);
  return 1;
}

/* Function to get accessible help to MSAA. */
static HRESULT STDMETHODCALLTYPE TkWinAccessible_get_accHelp(IAccessible *this, VARIANT varChild, BSTR* pszHelp)
{
  if (varChild.vt != VT_I4 || varChild.lVal != CHILDID_SELF) {
    return E_INVALIDARG;
  }

  TkWinAccessible *tkAccessible = (TkWinAccessible *)this; 
  Tk_Window win = tkAccessible->win;
  Tcl_HashEntry *hPtr, *hPtr2;
  Tcl_HashTable *AccessibleAttributes;
  Tcl_DString ds;
		
  hPtr=Tcl_FindHashEntry(TkAccessibilityObject, win);
  if (!hPtr) {
    return E_INVALIDARG;
698
699
700
701
702
703
704

705
706
707
708
709
710
711
  if (!*pszHelp) {
    return E_OUTOFMEMORY;
  }

  return S_OK;
}


static STDMETHODIMP TkWinAccessible_get_accFocus(IAccessible *this, VARIANT *pvarChild)
{
  if (!pvarChild) return E_INVALIDARG;
  VariantInit(pvarChild);

  /* Return the currently focused child ID.*/
  LONG focusedId  = g_focusedChildId;







>







699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
  if (!*pszHelp) {
    return E_OUTOFMEMORY;
  }

  return S_OK;
}

/* Function to get accessible description to MSAA. */
static STDMETHODIMP TkWinAccessible_get_accFocus(IAccessible *this, VARIANT *pvarChild)
{
  if (!pvarChild) return E_INVALIDARG;
  VariantInit(pvarChild);

  /* Return the currently focused child ID.*/
  LONG focusedId  = g_focusedChildId;
721
722
723
724
725
726
727

728
729
730
731
732
733
734
735

  return S_OK;
}

static HRESULT STDMETHODCALLTYPE TkWinAccessible_get_accDescription(IAccessible *this, VARIANT varChild, BSTR *pszDescription)
{


  Tk_Window win = accessible_win;
  Tcl_HashEntry *hPtr, *hPtr2;
  Tcl_HashTable *AccessibleAttributes;
  Tcl_DString ds;
		
  hPtr=Tcl_FindHashEntry(TkAccessibilityObject, win);
  if (!hPtr) {
    return E_INVALIDARG;







>
|







723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738

  return S_OK;
}

static HRESULT STDMETHODCALLTYPE TkWinAccessible_get_accDescription(IAccessible *this, VARIANT varChild, BSTR *pszDescription)
{

  TkWinAccessible *tkAccessible = (TkWinAccessible *)this;  
  Tk_Window win = tkAccessible->win;
  Tcl_HashEntry *hPtr, *hPtr2;
  Tcl_HashTable *AccessibleAttributes;
  Tcl_DString ds;
		
  hPtr=Tcl_FindHashEntry(TkAccessibilityObject, win);
  if (!hPtr) {
    return E_INVALIDARG;
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833

834
835
836
837
838
839
840
841
842
843
844
845
846
/*Function to check if a Tk widget (i.e. ttk widget) has a HWND.*/
static HWND GetWidgetHWNDIfPresent(Tk_Window tkwin) {
  if (!tkwin || !Tk_IsMapped(tkwin)) return NULL;

  Window winId = Tk_WindowId(tkwin);
  if (winId == None) return NULL;

  HWND hwnd = Tk_GetHWND(winId);
  if (hwnd && IsWindow(hwnd)) {
    return hwnd;
  }

  return NULL;
}


/* Function to return the Tk toplevel window that contains a given Tk widget. */
Tk_Window GetToplevelOfWidget(Tk_Window tkwin)
{

  while (!Tk_IsTopLevel(tkwin)) {
    tkwin = Tk_Parent(tkwin);
      if (tkwin == NULL) {
          return NULL; // Reached the top without finding a toplevel window
        }
    }
  return tkwin;
}

/* Custom WndProc to handle WM_GETOBJ so MSAA responds to Tk accessibility. */
LRESULT CALLBACK TkWinAccessible_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  if (msg == WM_GETOBJECT && lParam == OBJID_CLIENT) {







|











>


|
|
|
|







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
/*Function to check if a Tk widget (i.e. ttk widget) has a HWND.*/
static HWND GetWidgetHWNDIfPresent(Tk_Window tkwin) {
  if (!tkwin || !Tk_IsMapped(tkwin)) return NULL;

  Window winId = Tk_WindowId(tkwin);
  if (winId == None) return NULL;

  HWND hwnd = Tk_GetHWND(winId);  
  if (hwnd && IsWindow(hwnd)) {
    return hwnd;
  }

  return NULL;
}


/* Function to return the Tk toplevel window that contains a given Tk widget. */
Tk_Window GetToplevelOfWidget(Tk_Window tkwin)
{
  if (!tkwin) return NULL;
  while (!Tk_IsTopLevel(tkwin)) {
    tkwin = Tk_Parent(tkwin);
    if (tkwin == NULL) {
      return NULL; // Reached the top without finding a toplevel window
    }
  }
  return tkwin;
}

/* Custom WndProc to handle WM_GETOBJ so MSAA responds to Tk accessibility. */
LRESULT CALLBACK TkWinAccessible_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  if (msg == WM_GETOBJECT && lParam == OBJID_CLIENT) {
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
  if (data && data->originalWndProc) {
    return CallWindowProc(data->originalWndProc, hwnd, msg, wParam, lParam);
  }

  return DefWindowProc(hwnd, msg, wParam, lParam);
}

/* Install custom WndProc. */
void InstallCustomWndProcForMSAA(Tk_Window tkwin)
{
  if (!tkwin || !Tk_IsTopLevel(tkwin)) return;

  HWND hwnd = Tk_GetHWND(Tk_WindowId(tkwin));
  if (!hwnd) return;

  /* Check if we've already installed the custom WndProc. */
  TkWinAccessibleWndData *existing = (TkWinAccessibleWndData *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  if (existing && existing->originalWndProc == TkWinAccessible_WndProc) {
    return; // already set
  }

  /* Store original WndProc and hook ours. */
  WNDPROC original = (WNDPROC)GetWindowLongPtr(hwnd, GWLP_WNDPROC);
  TkWinAccessibleWndData *data = (TkWinAccessibleWndData *)ckalloc(sizeof(TkWinAccessibleWndData));
  data->originalWndProc = original;
  data->hwnd = hwnd;

  SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)data);
  SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)TkWinAccessible_WndProc);
}

/*
 *----------------------------------------------------------------------
 *
 * IsScreenReaderRunning --
 *
 * Runtime check to see if screen reader is running. 
 *







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







866
867
868
869
870
871
872
























873
874
875
876
877
878
879
  if (data && data->originalWndProc) {
    return CallWindowProc(data->originalWndProc, hwnd, msg, wParam, lParam);
  }

  return DefWindowProc(hwnd, msg, wParam, lParam);
}

























/*
 *----------------------------------------------------------------------
 *
 * IsScreenReaderRunning --
 *
 * Runtime check to see if screen reader is running. 
 *
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
  Tk_Window path;
	
  path = Tk_NameToWindow(ip, Tcl_GetString(objv[1]), Tk_MainWindow(ip));
  if (path == NULL) {
    Tk_MakeWindowExist(path);
  }
	
  accessible_win = path;
	
  /* Fire notification of new value. */
  HWND hwnd = Tk_GetHWND(Tk_WindowId(accessible_win));
  NotifyWinEvent(EVENT_OBJECT_VALUECHANGE, hwnd, OBJID_CLIENT, 0);
	
  return TCL_OK;
}



/*
 *----------------------------------------------------------------------
 *
 * TkWinAccessible_RegisterForCleanup --
 *
 * Register event handler for destroying accessibility element.







<
<

|




<
<







940
941
942
943
944
945
946


947
948
949
950
951
952


953
954
955
956
957
958
959
  Tk_Window path;
	
  path = Tk_NameToWindow(ip, Tcl_GetString(objv[1]), Tk_MainWindow(ip));
  if (path == NULL) {
    Tk_MakeWindowExist(path);
  }
	


  /* Fire notification of new value. */
  HWND hwnd = Tk_GetHWND(Tk_WindowId(path));   
  NotifyWinEvent(EVENT_OBJECT_VALUECHANGE, hwnd, OBJID_CLIENT, 0);
	
  return TCL_OK;
}



/*
 *----------------------------------------------------------------------
 *
 * TkWinAccessible_RegisterForCleanup --
 *
 * Register event handler for destroying accessibility element.
1095
1096
1097
1098
1099
1100
1101

1102
1103
1104
1105
1106
1107
1108
 */

void TkWinAccessible_RegisterForFocus(Tk_Window tkwin, void *tkAccessible)
{
  Tk_CreateEventHandler(tkwin, FocusChangeMask, 
			TkWinAccessible_FocusEventHandler, tkAccessible);
}


/*
 *----------------------------------------------------------------------
 *
 * TkWinAccessibleObjCmd --
 *
 *	Main command for adding and managing accessibility objects to Tk







>







1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
 */

void TkWinAccessible_RegisterForFocus(Tk_Window tkwin, void *tkAccessible)
{
  Tk_CreateEventHandler(tkwin, FocusChangeMask, 
			TkWinAccessible_FocusEventHandler, tkAccessible);
}


/*
 *----------------------------------------------------------------------
 *
 * TkWinAccessibleObjCmd --
 *
 *	Main command for adding and managing accessibility objects to Tk
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154

1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
  Tk_Window tkwin = Tk_NameToWindow(interp, windowName, Tk_MainWindow(interp));

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

  accessible_win = tkwin;

  HWND hwnd = Tk_GetHWND(Tk_WindowId(tkwin));
  InstallCustomWndProcForMSAA(tkwin);
  TkWinAccessible *accessible = create_tk_accessible(interp, hwnd, windowName);
  TkWinAccessible_RegisterForCleanup(tkwin, accessible);
  TkWinAccessible_RegisterForFocus(tkwin, accessible);

	
  if (accessible == NULL) {		
    Tcl_SetResult(interp, "Failed to create accessible object.", TCL_STATIC);
    return TCL_ERROR;
  }
  return TCL_OK;
} 

/*
 *----------------------------------------------------------------------
 *
 * TkWinAccessibility_Init --
 *
 *	Initializes the accessibility module.







<
<
|
<



>






|







1118
1119
1120
1121
1122
1123
1124


1125

1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
  Tk_Window tkwin = Tk_NameToWindow(interp, windowName, Tk_MainWindow(interp));

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



  HWND hwnd = Tk_GetHWND(Tk_WindowId(tkwin));   

  TkWinAccessible *accessible = create_tk_accessible(interp, hwnd, windowName);
  TkWinAccessible_RegisterForCleanup(tkwin, accessible);
  TkWinAccessible_RegisterForFocus(tkwin, accessible);

	
  if (accessible == NULL) {		
    Tcl_SetResult(interp, "Failed to create accessible object.", TCL_STATIC);
    return TCL_ERROR;
  }
  return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * TkWinAccessibility_Init --
 *
 *	Initializes the accessibility module.