-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathAccessibleObject.cs
More file actions
2435 lines (2030 loc) · 88.7 KB
/
Copy pathAccessibleObject.cs
File metadata and controls
2435 lines (2030 loc) · 88.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms.Automation;
using Accessibility;
using Windows.Win32.System.Ole;
using Windows.Win32.System.Com;
using Windows.Win32.System.Variant;
using UIA = Windows.Win32.UI.Accessibility;
using ComIServiceProvider = Windows.Win32.System.Com.IServiceProvider;
using static Interop;
namespace System.Windows.Forms;
/// <summary>
/// Provides an implementation for an object that can be inspected by an accessibility application.
/// </summary>
public unsafe partial class AccessibleObject :
StandardOleMarshalObject,
IReflect,
IAccessible,
UiaCore.IAccessibleEx,
ComIServiceProvider.Interface,
UiaCore.IRawElementProviderSimple,
UiaCore.IRawElementProviderFragment,
UiaCore.IRawElementProviderFragmentRoot,
UiaCore.IInvokeProvider,
UiaCore.IValueProvider,
UiaCore.IRangeValueProvider,
UiaCore.IExpandCollapseProvider,
UiaCore.IToggleProvider,
UiaCore.ITableProvider,
UiaCore.ITableItemProvider,
UiaCore.IGridProvider,
UiaCore.IGridItemProvider,
IEnumVARIANT.Interface,
IOleWindow.Interface,
UiaCore.ILegacyIAccessibleProvider,
UiaCore.ISelectionProvider,
UiaCore.ISelectionItemProvider,
UiaCore.IRawElementProviderHwndOverride,
UiaCore.IScrollItemProvider,
UiaCore.IMultipleViewProvider,
UiaCore.ITextProvider,
UiaCore.ITextProvider2
{
/// <summary>
/// The <see cref="UIA.IAccessible"/> as passed in or generated from requesting the standard implementation
/// from Windows. Used for default <see cref="UIA.IAccessible"/> behavior.
/// </summary>
internal AgileComPointer<UIA.IAccessible>? SystemIAccessible { get; private set; }
private protected static VARIANT CHILDID_SELF { get; } = (VARIANT)(int)PInvoke.CHILDID_SELF;
/// <summary>
/// Specifies the <see cref="IEnumVARIANT"/> used by this <see cref="AccessibleObject"/>.
/// </summary>
private AgileComPointer<IEnumVARIANT>? _systemIEnumVariant;
private IEnumVARIANT.Interface? _enumVariant;
// IOleWindow interface of the 'inner' system IAccessible object that we are wrapping
private AgileComPointer<IOleWindow>? _systemIOleWindow;
// Indicates this object is being used ONLY to wrap a system IAccessible
private readonly bool _isSystemWrapper;
// The support for the UIA Notification event begins in RS3.
// Assume the UIA Notification event is available until we learn otherwise.
// If we learn that the UIA Notification event is not available,
// controls should not attempt to raise it.
private static bool s_notificationEventAvailable = true;
private static bool? s_canNotifyClients;
internal const int InvalidIndex = -1;
internal const int RuntimeIDFirstItem = 0x2a;
public AccessibleObject()
{
}
/// <devdoc>
/// This constructor is used ONLY for wrapping system IAccessible objects
/// that are returned by the IAccessible methods.
/// </devdoc>
private AccessibleObject(AgileComPointer<UIA.IAccessible> accessible)
{
SystemIAccessible = accessible;
_isSystemWrapper = true;
}
private protected virtual string? AutomationId => null;
/// <summary>
/// Gets the bounds of the accessible object, in screen coordinates.
/// </summary>
public virtual Rectangle Bounds => SystemIAccessible.TryGetLocation(CHILDID_SELF);
internal static bool CanNotifyClients => s_canNotifyClients ??= InitializeCanNotifyClients();
private static bool InitializeCanNotifyClients()
{
// While handling accessibility events, accessibility clients (JAWS, Inspect),
// can access AccessibleObject associated with the event. In the designer scenario, controls are not
// receiving messages directly and might not respond to messages while in the notification call.
// This will make the server process unresponsive and will cause VisualStudio to become unresponsive.
//
// The following compat switch is set in the designer server process to prevent controls from sending notification.
if (AppContext.TryGetSwitch("Switch.System.Windows.Forms.AccessibleObject.NoClientNotifications", out bool isEnabled))
{
return !isEnabled;
}
return true;
}
/// <summary>
/// Gets a description of the default action for an object.
/// </summary>
public virtual string? DefaultAction => SystemIAccessible.TryGetDefaultAction(CHILDID_SELF);
/// <summary>
/// Gets a description of the object's visual appearance to the user.
/// </summary>
public virtual string? Description => SystemIAccessible.TryGetDescription(CHILDID_SELF);
private IEnumVARIANT.Interface EnumVariant => _enumVariant ??= new EnumVariantObject(this);
/// <summary>
/// Gets a description of what the object does or how the object is used.
/// </summary>
public virtual string? Help => SystemIAccessible.TryGetHelp(CHILDID_SELF);
/// <summary>
/// Gets the object shortcut key or access key for an accessible object.
/// </summary>
public virtual string? KeyboardShortcut => SystemIAccessible.TryGetKeyboardShortcut(CHILDID_SELF);
/// <summary>
/// Gets or sets the object name.
/// </summary>
public virtual string? Name
{
get => SystemIAccessible.TryGetName(CHILDID_SELF);
set => SystemIAccessible.TrySetName(CHILDID_SELF, value);
}
/// <summary>
/// When overridden in a derived class, gets or sets the parent of an accessible object.
/// </summary>
/// <devdoc>
/// Note that the default behavior for <see cref="Control"/> is that it calls base from its override in
/// <see cref="Control.ControlAccessibleObject"/>. <see cref="Control.ControlAccessibleObject"/> always
/// creates the Win32 standard accessible objects so it will hit the Windows implementation of
/// <see cref="IAccessible.accParent"/>.
///
/// For the non-client area (OBJID_WINDOW), the Windows accParent implementation simply calls
/// GetAncestor(GA_PARENT) to find the window it will call WM_GETOBJECT on with OBJID_CLIENT.
///
/// For the client area (OBJID_CLIENT), the Windows accParent implementation calls WM_GETOBJECT directly
/// with OBJID_WINDOW.
///
/// What this means, effectively, is that the non-client area is the parent of the client area, and the parent
/// window's client area is the parent of the non-client area of the current window (at least from an
/// accessiblity object standpoint).
/// </devdoc>
public virtual AccessibleObject? Parent
{
get
{
using var accessible = SystemIAccessible.TryGetIAccessible(out HRESULT result);
if (result.Succeeded)
{
IDispatch* dispatch;
result = accessible.Value->get_accParent(&dispatch);
return TryGetAccessibleObject(dispatch);
}
return null;
}
}
/// <summary>
/// Gets the role of this accessible object.
/// </summary>
public virtual AccessibleRole Role => SystemIAccessible.TryGetRole(CHILDID_SELF);
/// <summary>
/// Gets the state of this accessible object.
/// </summary>
public virtual AccessibleStates State => SystemIAccessible.TryGetState(CHILDID_SELF);
/// <summary>
/// Gets or sets the value of an accessible object.
/// </summary>
public virtual string? Value
{
// This might be better to never return null or return null instead of string.Empty?
get => SystemIAccessible is null ? string.Empty : SystemIAccessible.TryGetValue(CHILDID_SELF);
set => SystemIAccessible.TrySetValue(CHILDID_SELF, value);
}
/// <summary>
/// When overridden in a derived class, gets the accessible child
/// corresponding to the specified index.
/// </summary>
public virtual AccessibleObject? GetChild(int index) => null;
internal virtual int GetChildIndex(AccessibleObject? child) => InvalidIndex;
/// <summary>
/// When overridden in a derived class, gets the number of children
/// belonging to an accessible object.
/// </summary>
public virtual int GetChildCount() => -1;
/// <summary>
/// Mechanism for overriding default IEnumVariant behavior of the 'inner'
/// system accessible object (IEnumVariant is how a system accessible
/// object exposes its ordered list of child objects).
///
/// USAGE: Overridden method in derived class should return array of
/// integers representing new order to be imposed on the child accessible
/// object collection returned by the system (which we assume will be a
/// set of accessible objects that represent the child windows, in z-order).
/// Each array element contains the original z-order based rank of the
/// child window that is to appear at that position in the new ordering.
/// Note: This array could also be used to filter out unwanted child
/// windows too, if necessary (not recommended).
/// </summary>
internal virtual int[]? GetSysChildOrder() => null;
/// <summary>
/// Mechanism for overriding default <see cref="UIA.IAccessible.accNavigate(int, VARIANT, VARIANT*)"/>
/// behavior of the 'inner' system accessible object (accNavigate is how you move between parent, child and
/// sibling accessible objects).
/// </summary>
/// <param name="navdir">
/// Navigation operation to perform, relative to this accessible object.
/// </param>
/// <param name="accessibleObject">
/// The destination object or <see langword="null"/> to indicate 'off end of list'.
/// </param>
/// <returns>
/// <see langword="false"/> to allow fall-back to default system behavior.
/// </returns>
internal virtual bool GetSysChild(AccessibleNavigation navdir, out AccessibleObject? accessibleObject)
{
accessibleObject = null;
return false;
}
/// <summary>
/// When overridden in a derived class, gets the object that has the keyboard focus.
/// </summary>
public virtual AccessibleObject? GetFocused()
{
// Default behavior for objects with AccessibleObject children
if (GetChildCount() >= 0)
{
int count = GetChildCount();
for (int index = 0; index < count; ++index)
{
AccessibleObject? child = GetChild(index);
Debug.Assert(child is not null, $"GetChild({index}) returned null!");
if (child is not null && ((child.State & AccessibleStates.Focused) != 0))
{
return child;
}
}
return State.HasFlag(AccessibleStates.Focused) ? this : null;
}
return TryGetFocus();
}
private AccessibleObject? TryGetFocus()
{
using var accessible = SystemIAccessible.TryGetIAccessible(out HRESULT result);
if (result.Failed)
{
return null;
}
result = accessible.Value->get_accFocus(out VARIANT focus);
if (result.Failed)
{
Debug.Assert(result == HRESULT.DISP_E_MEMBERNOTFOUND, $"{nameof(TryGetFocus)} failed with {result}");
return null;
}
return TryGetAccessibleObject(focus);
}
/// <summary>
/// Gets an identifier for a Help topic and the path to the Help file associated with this accessible object.
/// </summary>
public virtual int GetHelpTopic(out string? fileName) => SystemIAccessible.TryGetHelpTopic(CHILDID_SELF, out fileName);
/// <summary>
/// When overridden in a derived class, gets the currently selected child.
/// </summary>
public virtual AccessibleObject? GetSelected()
{
// Default behavior for objects with AccessibleObject children
if (GetChildCount() >= 0)
{
int count = GetChildCount();
for (int index = 0; index < count; ++index)
{
AccessibleObject? child = GetChild(index);
Debug.Assert(child is not null, $"GetChild({index}) returned null!");
if (child is not null && child.State.HasFlag(AccessibleStates.Selected))
{
return child;
}
}
return State.HasFlag(AccessibleStates.Selected) ? this : null;
}
return TryGetSelection();
}
private AccessibleObject? TryGetSelection()
{
using var accessible = SystemIAccessible.TryGetIAccessible(out HRESULT result);
if (result.Failed)
{
return null;
}
result = accessible.Value->get_accSelection(out VARIANT selection);
if (result.Failed)
{
Debug.Assert(result == HRESULT.DISP_E_MEMBERNOTFOUND, $"{nameof(TryGetSelection)} failed with {result}");
return null;
}
return TryGetAccessibleObject(selection);
}
/// <summary>
/// Return the child object at the given screen coordinates.
/// </summary>
public virtual AccessibleObject? HitTest(int x, int y)
{
// Default behavior for objects with AccessibleObject children
if (GetChildCount() >= 0)
{
int count = GetChildCount();
for (int index = 0; index < count; ++index)
{
AccessibleObject? child = GetChild(index);
Debug.Assert(child is not null, $"GetChild({index}) returned null!");
if (child is not null && child.Bounds.Contains(x, y))
{
return child;
}
}
return this;
}
using var accessible = SystemIAccessible.TryGetIAccessible(out HRESULT result);
if (result.Succeeded)
{
result = accessible.Value->accHitTest(x, y, out VARIANT child);
return result.Failed || result == HRESULT.S_FALSE ? null : TryGetAccessibleObject(child);
}
return Bounds.Contains(x, y) ? this : null;
}
internal virtual bool IsIAccessibleExSupported()
{
// Override this, in your derived class, to enable IAccessibleEx support.
return false;
}
/// <summary>
/// Indicates whether specified pattern is supported.
/// </summary>
/// <param name="patternId">The pattern ID.</param>
/// <returns><see langword="true"/> if <paramref name="patternId"/> is supported.</returns>
internal virtual bool IsPatternSupported(UiaCore.UIA patternId)
{
return patternId == UiaCore.UIA.InvokePatternId ? IsInvokePatternAvailable : false;
}
/// <summary>
/// Gets the runtime ID.
/// </summary>
internal virtual int[] RuntimeId
{
get
{
if (_isSystemWrapper)
{
return new int[] { RuntimeIDFirstItem, GetHashCode() };
}
string message = string.Format(SR.AccessibleObjectRuntimeIdNotSupported, nameof(AccessibleObject), nameof(RuntimeId));
Debug.Fail(message);
throw new NotSupportedException(message);
}
}
internal virtual int ProviderOptions
=> (int)(UiaCore.ProviderOptions.ServerSideProvider | UiaCore.ProviderOptions.UseComThreading);
internal virtual UiaCore.IRawElementProviderSimple? HostRawElementProvider => null;
/// <summary>
/// Returns the value of the specified <paramref name="propertyID"/> from the element.
/// </summary>
/// <param name="propertyID">Identifier indicating the property to return.</param>
/// <returns>The requested value if supported or <see langword="null"/> if it is not.</returns>
internal virtual object? GetPropertyValue(UiaCore.UIA propertyID) =>
propertyID switch
{
UiaCore.UIA.AccessKeyPropertyId => KeyboardShortcut ?? string.Empty,
UiaCore.UIA.AutomationIdPropertyId => AutomationId,
UiaCore.UIA.BoundingRectanglePropertyId => UiaTextProvider.BoundingRectangleAsArray(Bounds),
UiaCore.UIA.FrameworkIdPropertyId => "WinForm",
UiaCore.UIA.IsExpandCollapsePatternAvailablePropertyId => IsPatternSupported(UiaCore.UIA.ExpandCollapsePatternId),
UiaCore.UIA.IsGridItemPatternAvailablePropertyId => IsPatternSupported(UiaCore.UIA.GridItemPatternId),
UiaCore.UIA.IsGridPatternAvailablePropertyId => IsPatternSupported(UiaCore.UIA.GridPatternId),
UiaCore.UIA.IsInvokePatternAvailablePropertyId => IsInvokePatternAvailable,
UiaCore.UIA.IsLegacyIAccessiblePatternAvailablePropertyId => IsPatternSupported(UiaCore.UIA.LegacyIAccessiblePatternId),
UiaCore.UIA.IsMultipleViewPatternAvailablePropertyId => IsPatternSupported(UiaCore.UIA.MultipleViewPatternId),
UiaCore.UIA.IsOffscreenPropertyId => (State & AccessibleStates.Offscreen) == AccessibleStates.Offscreen,
UiaCore.UIA.IsPasswordPropertyId => false,
UiaCore.UIA.IsScrollItemPatternAvailablePropertyId => IsPatternSupported(UiaCore.UIA.ScrollItemPatternId),
UiaCore.UIA.IsScrollPatternAvailablePropertyId => IsPatternSupported(UiaCore.UIA.ScrollPatternId),
UiaCore.UIA.IsSelectionItemPatternAvailablePropertyId => IsPatternSupported(UiaCore.UIA.SelectionItemPatternId),
UiaCore.UIA.IsSelectionPatternAvailablePropertyId => IsPatternSupported(UiaCore.UIA.SelectionPatternId),
UiaCore.UIA.IsTableItemPatternAvailablePropertyId => IsPatternSupported(UiaCore.UIA.TableItemPatternId),
UiaCore.UIA.IsTablePatternAvailablePropertyId => IsPatternSupported(UiaCore.UIA.TablePatternId),
UiaCore.UIA.IsTextPattern2AvailablePropertyId => IsPatternSupported(UiaCore.UIA.TextPattern2Id),
UiaCore.UIA.IsTextPatternAvailablePropertyId => IsPatternSupported(UiaCore.UIA.TextPatternId),
UiaCore.UIA.IsTogglePatternAvailablePropertyId => IsPatternSupported(UiaCore.UIA.TogglePatternId),
UiaCore.UIA.IsValuePatternAvailablePropertyId => IsPatternSupported(UiaCore.UIA.ValuePatternId),
UiaCore.UIA.HelpTextPropertyId => Help ?? string.Empty,
UiaCore.UIA.LegacyIAccessibleDefaultActionPropertyId => !string.IsNullOrEmpty(DefaultAction) ? DefaultAction : null,
UiaCore.UIA.LegacyIAccessibleNamePropertyId => !string.IsNullOrEmpty(Name) ? Name : null,
UiaCore.UIA.LegacyIAccessibleRolePropertyId => Role,
UiaCore.UIA.LegacyIAccessibleStatePropertyId => State,
UiaCore.UIA.NamePropertyId => Name,
UiaCore.UIA.RuntimeIdPropertyId => RuntimeId,
UiaCore.UIA.SelectionCanSelectMultiplePropertyId => CanSelectMultiple,
UiaCore.UIA.SelectionIsSelectionRequiredPropertyId => IsSelectionRequired,
UiaCore.UIA.ValueValuePropertyId => !string.IsNullOrEmpty(Value) ? Value : null,
_ => null
};
private bool IsInvokePatternAvailable
{
get
{
// MSAA Proxy determines the availability of invoke pattern based
// on Role/DefaultAction properties.
// Below code emulates the same rules.
switch (Role)
{
case AccessibleRole.MenuItem:
case AccessibleRole.Link:
case AccessibleRole.PushButton:
case AccessibleRole.ButtonDropDown:
case AccessibleRole.ButtonMenu:
case AccessibleRole.ButtonDropDownGrid:
case AccessibleRole.Clock:
case AccessibleRole.SplitButton:
case AccessibleRole.CheckButton:
case AccessibleRole.Cell:
case AccessibleRole.ListItem:
return true;
case AccessibleRole.Default:
case AccessibleRole.None:
case AccessibleRole.Sound:
case AccessibleRole.Cursor:
case AccessibleRole.Caret:
case AccessibleRole.Alert:
case AccessibleRole.Client:
case AccessibleRole.Chart:
case AccessibleRole.Dialog:
case AccessibleRole.Border:
case AccessibleRole.Column:
case AccessibleRole.Row:
case AccessibleRole.HelpBalloon:
case AccessibleRole.Character:
case AccessibleRole.PageTab:
case AccessibleRole.PropertyPage:
case AccessibleRole.DropList:
case AccessibleRole.Dial:
case AccessibleRole.HotkeyField:
case AccessibleRole.Diagram:
case AccessibleRole.Animation:
case AccessibleRole.Equation:
case AccessibleRole.WhiteSpace:
case AccessibleRole.IpAddress:
case AccessibleRole.OutlineButton:
return false;
default:
return !string.IsNullOrEmpty(DefaultAction);
}
}
}
/// <summary>
/// Gets the child accessible object ID.
/// </summary>
/// <returns>The child accessible object ID.</returns>
internal virtual int GetChildId() => (int)PInvoke.CHILDID_SELF;
/// <summary>
/// Returns the element in the specified <paramref name="direction"/>.
/// </summary>
/// <param name="direction">Indicates the direction in which to navigate.</param>
/// <returns>The element in the specified direction if it exists.</returns>
internal virtual UiaCore.IRawElementProviderFragment? FragmentNavigate(UiaCore.NavigateDirection direction) => null;
internal virtual UiaCore.IRawElementProviderSimple[]? GetEmbeddedFragmentRoots() => null;
internal virtual void SetFocus()
{
}
internal virtual Rectangle BoundingRectangle => Bounds;
/// <summary>
/// Gets the top level element.
/// </summary>
internal virtual UiaCore.IRawElementProviderFragmentRoot? FragmentRoot => null;
/// <summary>
/// Return the child object at the given screen coordinates.
/// </summary>
/// <param name="x">X coordinate.</param>
/// <param name="y">Y coordinate.</param>
/// <returns>The accessible object of corresponding element in the provided coordinates.</returns>
internal virtual UiaCore.IRawElementProviderFragment? ElementProviderFromPoint(double x, double y) => this;
internal virtual UiaCore.IRawElementProviderFragment? GetFocus() => null;
internal virtual void Expand()
{
}
internal virtual void Collapse()
{
}
internal virtual UiaCore.ExpandCollapseState ExpandCollapseState => UiaCore.ExpandCollapseState.Collapsed;
internal virtual void Toggle()
{
}
internal virtual UiaCore.ToggleState ToggleState => UiaCore.ToggleState.Indeterminate;
private protected virtual UiaCore.IRawElementProviderFragmentRoot? ToolStripFragmentRoot => null;
internal virtual UiaCore.IRawElementProviderSimple[]? GetRowHeaders() => null;
internal virtual UiaCore.IRawElementProviderSimple[]? GetColumnHeaders() => null;
internal virtual UiaCore.RowOrColumnMajor RowOrColumnMajor => UiaCore.RowOrColumnMajor.RowMajor;
internal virtual UiaCore.IRawElementProviderSimple[]? GetRowHeaderItems() => null;
internal virtual UiaCore.IRawElementProviderSimple[]? GetColumnHeaderItems() => null;
internal virtual UiaCore.IRawElementProviderSimple? GetItem(int row, int column) => null;
internal virtual int RowCount => -1;
internal virtual int ColumnCount => -1;
internal virtual int Row => -1;
internal virtual int Column => -1;
internal virtual int RowSpan => 1;
internal virtual int ColumnSpan => 1;
internal virtual UiaCore.IRawElementProviderSimple? ContainingGrid => null;
internal virtual void Invoke() => DoDefaultAction();
internal virtual UiaCore.ITextRangeProvider? DocumentRangeInternal
{
get
{
Debug.Fail("Not implemented. DocumentRangeInternal property should be overridden.");
return null;
}
}
internal virtual UiaCore.ITextRangeProvider[]? GetTextSelection()
{
Debug.Fail("Not implemented. GetTextSelection method should be overridden.");
return null;
}
internal virtual UiaCore.ITextRangeProvider[]? GetTextVisibleRanges()
{
Debug.Fail("Not implemented. GetTextVisibleRanges method should be overridden.");
return null;
}
internal virtual UiaCore.ITextRangeProvider? GetTextRangeFromChild(UiaCore.IRawElementProviderSimple childElement)
{
Debug.Fail("Not implemented. GetTextRangeFromChild method should be overridden.");
return null;
}
internal virtual UiaCore.ITextRangeProvider? GetTextRangeFromPoint(Point screenLocation)
{
Debug.Fail("Not implemented. GetTextRangeFromPoint method should be overridden.");
return null;
}
internal virtual UiaCore.SupportedTextSelection SupportedTextSelectionInternal
{
get
{
Debug.Fail("Not implemented. SupportedTextSelectionInternal property should be overridden.");
return UiaCore.SupportedTextSelection.None;
}
}
internal virtual UiaCore.ITextRangeProvider? GetTextCaretRange(out BOOL isActive)
{
isActive = false;
Debug.Fail("Not implemented. GetTextCaretRange method should be overridden.");
return null;
}
internal virtual UiaCore.ITextRangeProvider? GetRangeFromAnnotation(UiaCore.IRawElementProviderSimple annotationElement)
{
Debug.Fail("Not implemented. GetRangeFromAnnotation method should be overridden.");
return null;
}
internal virtual bool IsReadOnly => false;
internal virtual void SetValue(string? newValue)
{
Value = newValue;
}
internal virtual UiaCore.IRawElementProviderSimple? GetOverrideProviderForHwnd(IntPtr hwnd) => null;
internal virtual int GetMultiViewProviderCurrentView() => 0;
internal virtual int[]? GetMultiViewProviderSupportedViews() => Array.Empty<int>();
internal virtual string GetMultiViewProviderViewName(int viewId) => string.Empty;
internal virtual void SetMultiViewProviderCurrentView(int viewId)
{
}
internal virtual void SetValue(double newValue)
{
}
internal virtual double LargeChange => double.NaN;
internal virtual double Maximum => double.NaN;
internal virtual double Minimum => double.NaN;
internal virtual double SmallChange => double.NaN;
internal virtual double RangeValue => double.NaN;
internal virtual UiaCore.IRawElementProviderSimple[]? GetSelection() => null;
internal virtual bool CanSelectMultiple => false;
internal virtual bool IsSelectionRequired => false;
internal virtual void SelectItem()
{
}
internal virtual void AddToSelection()
{
}
internal virtual void RemoveFromSelection()
{
}
internal virtual bool IsItemSelected => false;
internal virtual UiaCore.IRawElementProviderSimple? ItemSelectionContainer => null;
/// <summary>
/// Sets the parent accessible object for the node which can be added or removed to/from hierarchy nodes.
/// </summary>
/// <param name="parent">The parent accessible object.</param>
internal virtual void SetParent(AccessibleObject? parent)
{
}
/// <summary>
/// Sets the detachable child accessible object which may be added or removed to/from hierarchy nodes.
/// </summary>
/// <param name="child">The child accessible object.</param>
internal virtual void SetDetachableChild(AccessibleObject? child)
{
}
unsafe HRESULT ComIServiceProvider.Interface.QueryService(Guid* service, Guid* riid, void** ppvObject)
{
if (service is null || riid is null)
{
return HRESULT.E_NOINTERFACE;
}
if (ppvObject is null)
{
return HRESULT.E_POINTER;
}
if (IsIAccessibleExSupported())
{
Guid IID_IAccessibleEx = typeof(UiaCore.IAccessibleEx).GUID;
if (service->Equals(IID_IAccessibleEx) && riid->Equals(IID_IAccessibleEx))
{
// We want to return the internal, secure, object, which we don't have access here
// Return non-null, which will be interpreted in internal method, to mean returning casted object to IAccessibleEx
*ppvObject = (void*)Marshal.GetComInterfaceForObject(this, typeof(UiaCore.IAccessibleEx));
return HRESULT.S_OK;
}
}
return HRESULT.E_NOINTERFACE;
}
UiaCore.IAccessibleEx? UiaCore.IAccessibleEx.GetObjectForChild(int idChild) => null;
unsafe HRESULT UiaCore.IAccessibleEx.GetIAccessiblePair(out object? ppAcc, int* pidChild)
{
if (pidChild is null)
{
ppAcc = null;
return HRESULT.E_INVALIDARG;
}
ppAcc = this;
*pidChild = (int)PInvoke.CHILDID_SELF;
return HRESULT.S_OK;
}
int[]? UiaCore.IAccessibleEx.GetRuntimeId() => RuntimeId;
unsafe HRESULT UiaCore.IAccessibleEx.ConvertReturnedElement(UiaCore.IRawElementProviderSimple pIn, IntPtr* ppRetValOut)
{
if (ppRetValOut == null)
{
return HRESULT.E_POINTER;
}
// No need to implement this for patterns and properties
*ppRetValOut = IntPtr.Zero;
return HRESULT.E_NOTIMPL;
}
UiaCore.ProviderOptions UiaCore.IRawElementProviderSimple.ProviderOptions => (UiaCore.ProviderOptions)ProviderOptions;
UiaCore.IRawElementProviderSimple? UiaCore.IRawElementProviderSimple.HostRawElementProvider => HostRawElementProvider;
object? UiaCore.IRawElementProviderSimple.GetPatternProvider(UiaCore.UIA patternId)
{
if (IsPatternSupported(patternId))
{
return this;
}
return null;
}
object? UiaCore.IRawElementProviderSimple.GetPropertyValue(UiaCore.UIA propertyID)
{
object? value = GetPropertyValue(propertyID);
#if DEBUG
if (value?.GetType() is { } type && type.IsValueType && !type.IsPrimitive && !type.IsEnum)
{
// Check to make sure we can actually convert this to a VARIANT.
//
// Our interop handle structs (such as HWND) cannot be marshalled directly and will fail "silently" on
// callbacks (they will throw but the marshaller will convert that to an HRESULT that we won't see unless
// first-chance exceptions are on when we're debugging).
using VARIANT variant = default;
Marshal.GetNativeVariantForObject(value, (nint)(void*)&variant);
}
#endif
return value;
}
object? UiaCore.IRawElementProviderFragment.Navigate(UiaCore.NavigateDirection direction) => FragmentNavigate(direction);
int[]? UiaCore.IRawElementProviderFragment.GetRuntimeId() => RuntimeId;
object[]? UiaCore.IRawElementProviderFragment.GetEmbeddedFragmentRoots() => GetEmbeddedFragmentRoots();
void UiaCore.IRawElementProviderFragment.SetFocus() => SetFocus();
UiaCore.UiaRect UiaCore.IRawElementProviderFragment.BoundingRectangle => new(BoundingRectangle);
// An accessible object should provide info about its correct root object,
// even its owner is used like a ToolStrip item via ToolStripControlHost.
// This change was made here to not to rework FragmentRoot implementations
// for all accessible object. Moreover, this change will work for new accessible object
// classes, where it is enough to implement FragmentRoot for a common case.
UiaCore.IRawElementProviderFragmentRoot? UiaCore.IRawElementProviderFragment.FragmentRoot
=> ToolStripFragmentRoot ?? FragmentRoot;
object? UiaCore.IRawElementProviderFragmentRoot.ElementProviderFromPoint(double x, double y) => ElementProviderFromPoint(x, y);
object? UiaCore.IRawElementProviderFragmentRoot.GetFocus() => GetFocus();
string? UiaCore.ILegacyIAccessibleProvider.DefaultAction => DefaultAction;
string? UiaCore.ILegacyIAccessibleProvider.Description => Description;
string? UiaCore.ILegacyIAccessibleProvider.Help => Help;
string? UiaCore.ILegacyIAccessibleProvider.KeyboardShortcut => KeyboardShortcut;
string? UiaCore.ILegacyIAccessibleProvider.Name => Name;
uint UiaCore.ILegacyIAccessibleProvider.Role => (uint)Role;
uint UiaCore.ILegacyIAccessibleProvider.State => (uint)State;
string? UiaCore.ILegacyIAccessibleProvider.Value => Value;
int UiaCore.ILegacyIAccessibleProvider.ChildId => GetChildId();
void UiaCore.ILegacyIAccessibleProvider.DoDefaultAction() => DoDefaultAction();
HRESULT UiaCore.ILegacyIAccessibleProvider.GetIAccessible(UIA.IAccessible** ppAccessible)
{
if (ppAccessible is null)
{
return HRESULT.E_POINTER;
}
if (_isSystemWrapper)
{
// If all we were doing was wrapping a provided IAccessible, there is no need to marshal
// our wrapper.
*ppAccessible = SystemIAccessible is { } accessible
? accessible.GetInterface().Value
: null;
}
else
{
// Ideally we'll implement UIA.IAccessible directly on this class. Currently there is a [ComImport]
// on the CsWin32 generated interface so we'll need to figure out how collisions are handled or if
// we need a feature from CsWin32 to skip the attribute.
*ppAccessible = (UIA.IAccessible*)Marshal.GetComInterfaceForObject<AccessibleObject, IAccessible>(this);
}
return HRESULT.S_OK;
}
UiaCore.IRawElementProviderSimple[] UiaCore.ILegacyIAccessibleProvider.GetSelection()
{
if (GetSelected() is UiaCore.IRawElementProviderSimple selected)
{
return new UiaCore.IRawElementProviderSimple[] { selected };
}
return Array.Empty<UiaCore.IRawElementProviderSimple>();
}
void UiaCore.ILegacyIAccessibleProvider.Select(int flagsSelect) => Select((AccessibleSelection)flagsSelect);
void UiaCore.ILegacyIAccessibleProvider.SetValue(string szValue) => SetValue(szValue);
void UiaCore.IExpandCollapseProvider.Expand() => Expand();
void UiaCore.IExpandCollapseProvider.Collapse() => Collapse();
UiaCore.ExpandCollapseState UiaCore.IExpandCollapseProvider.ExpandCollapseState => ExpandCollapseState;
void UiaCore.IInvokeProvider.Invoke() => Invoke();
UiaCore.ITextRangeProvider? UiaCore.ITextProvider.DocumentRange => DocumentRangeInternal;
UiaCore.ITextRangeProvider[]? UiaCore.ITextProvider.GetSelection() => GetTextSelection();
UiaCore.ITextRangeProvider[]? UiaCore.ITextProvider.GetVisibleRanges() => GetTextVisibleRanges();
UiaCore.ITextRangeProvider? UiaCore.ITextProvider.RangeFromChild(UiaCore.IRawElementProviderSimple childElement) =>
GetTextRangeFromChild(childElement);
UiaCore.ITextRangeProvider? UiaCore.ITextProvider.RangeFromPoint(Point screenLocation) => GetTextRangeFromPoint(screenLocation);
UiaCore.SupportedTextSelection UiaCore.ITextProvider.SupportedTextSelection => SupportedTextSelectionInternal;
UiaCore.ITextRangeProvider? UiaCore.ITextProvider2.DocumentRange => DocumentRangeInternal;
UiaCore.ITextRangeProvider[]? UiaCore.ITextProvider2.GetSelection() => GetTextSelection();
UiaCore.ITextRangeProvider[]? UiaCore.ITextProvider2.GetVisibleRanges() => GetTextVisibleRanges();
UiaCore.ITextRangeProvider? UiaCore.ITextProvider2.RangeFromChild(UiaCore.IRawElementProviderSimple childElement) =>
GetTextRangeFromChild(childElement);
UiaCore.ITextRangeProvider? UiaCore.ITextProvider2.RangeFromPoint(Point screenLocation) => GetTextRangeFromPoint(screenLocation);
UiaCore.SupportedTextSelection UiaCore.ITextProvider2.SupportedTextSelection => SupportedTextSelectionInternal;
UiaCore.ITextRangeProvider? UiaCore.ITextProvider2.GetCaretRange(out BOOL isActive) => GetTextCaretRange(out isActive);
UiaCore.ITextRangeProvider? UiaCore.ITextProvider2.RangeFromAnnotation(UiaCore.IRawElementProviderSimple annotationElement) =>
GetRangeFromAnnotation(annotationElement);
BOOL UiaCore.IValueProvider.IsReadOnly => IsReadOnly ? true : false;
string? UiaCore.IValueProvider.Value => Value;
void UiaCore.IValueProvider.SetValue(string? newValue) => SetValue(newValue);
void UiaCore.IToggleProvider.Toggle() => Toggle();
UiaCore.ToggleState UiaCore.IToggleProvider.ToggleState => ToggleState;
object[]? UiaCore.ITableProvider.GetRowHeaders() => GetRowHeaders();
object[]? UiaCore.ITableProvider.GetColumnHeaders() => GetColumnHeaders();
UiaCore.RowOrColumnMajor UiaCore.ITableProvider.RowOrColumnMajor => RowOrColumnMajor;
object[]? UiaCore.ITableItemProvider.GetRowHeaderItems() => GetRowHeaderItems();
object[]? UiaCore.ITableItemProvider.GetColumnHeaderItems() => GetColumnHeaderItems();
object? UiaCore.IGridProvider.GetItem(int row, int column) => GetItem(row, column);
int UiaCore.IGridProvider.RowCount => RowCount;
int UiaCore.IGridProvider.ColumnCount => ColumnCount;
int UiaCore.IGridItemProvider.Row => Row;
int UiaCore.IGridItemProvider.Column => Column;
int UiaCore.IGridItemProvider.RowSpan => RowSpan;
int UiaCore.IGridItemProvider.ColumnSpan => ColumnSpan;
UiaCore.IRawElementProviderSimple? UiaCore.IGridItemProvider.ContainingGrid => ContainingGrid;
/// <summary>
/// Perform the default action
/// </summary>
void IAccessible.accDoDefaultAction(object childID)
{
if (IsClientObject)
{
ValidateChildID(ref childID);
Debug.WriteLineIf(
CompModSwitches.MSAA.TraceInfo,
$"AccessibleObject.AccDoDefaultAction: this = {ToString()}, childID = {childID}");
// If the default action is to be performed on self, do it.
if (childID.Equals((int)PInvoke.CHILDID_SELF))
{
DoDefaultAction();
return;
}
// If we have an accessible object collection, get the appropriate child
AccessibleObject? child = GetAccessibleChild(childID);
if (child is not null)
{
child.DoDefaultAction();
return;
}