|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +#nullable disable |
| 6 | + |
| 7 | +using System.Drawing; |
| 8 | +using static Interop; |
| 9 | + |
| 10 | +namespace System.Windows.Forms |
| 11 | +{ |
| 12 | + partial class ErrorProvider |
| 13 | + { |
| 14 | + internal partial class ControlItem |
| 15 | + { |
| 16 | + private class ControlItemAccessibleObject : AccessibleObject |
| 17 | + { |
| 18 | + private readonly ControlItem _controlItem; |
| 19 | + private readonly ErrorWindow _window; |
| 20 | + private readonly Control _control; |
| 21 | + private readonly ErrorProvider _provider; |
| 22 | + |
| 23 | + public ControlItemAccessibleObject(ControlItem controlItem, ErrorWindow window, Control control, ErrorProvider provider) |
| 24 | + { |
| 25 | + _controlItem = controlItem; |
| 26 | + _window = window; |
| 27 | + _control = control; |
| 28 | + _provider = provider; |
| 29 | + } |
| 30 | + |
| 31 | + internal override Rectangle BoundingRectangle => Bounds; |
| 32 | + |
| 33 | + public override Rectangle Bounds => _control.IsHandleCreated ? |
| 34 | + _control.RectangleToScreen(_controlItem.GetIconBounds(_provider.Region.Size)) : Rectangle.Empty; |
| 35 | + |
| 36 | + private int ControlItemsCount => _window.ControlItems.Count; |
| 37 | + |
| 38 | + private int CurrentIndex => _window.ControlItems.IndexOf(_controlItem); |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Returns the element in the specified direction. |
| 42 | + /// </summary> |
| 43 | + /// <param name="direction">Indicates the direction in which to navigate.</param> |
| 44 | + /// <returns>Returns the element in the specified direction.</returns> |
| 45 | + internal override UiaCore.IRawElementProviderFragment FragmentNavigate(UiaCore.NavigateDirection direction) |
| 46 | + { |
| 47 | + switch (direction) |
| 48 | + { |
| 49 | + case UiaCore.NavigateDirection.Parent: |
| 50 | + return Parent; |
| 51 | + case UiaCore.NavigateDirection.NextSibling: |
| 52 | + return GetNextSibling(); |
| 53 | + case UiaCore.NavigateDirection.PreviousSibling: |
| 54 | + return GetPreviousSibling(); |
| 55 | + default: |
| 56 | + return base.FragmentNavigate(direction); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + internal override UiaCore.IRawElementProviderFragmentRoot FragmentRoot => Parent; |
| 61 | + |
| 62 | + internal override int GetChildId() |
| 63 | + { |
| 64 | + return CurrentIndex + 1; |
| 65 | + } |
| 66 | + |
| 67 | + private AccessibleObject GetNextSibling() |
| 68 | + { |
| 69 | + int currentIndex = CurrentIndex; |
| 70 | + |
| 71 | + //Is a last child |
| 72 | + if (currentIndex >= ControlItemsCount - 1 || currentIndex < 0) |
| 73 | + { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + return _window.ControlItems[currentIndex + 1].AccessibilityObject; |
| 78 | + } |
| 79 | + |
| 80 | + private AccessibleObject GetPreviousSibling() |
| 81 | + { |
| 82 | + int currentIndex = CurrentIndex; |
| 83 | + |
| 84 | + //Is a first child |
| 85 | + if (currentIndex <= 0 || currentIndex > ControlItemsCount - 1) |
| 86 | + { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + return _window.ControlItems[currentIndex - 1].AccessibilityObject; |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Gets the accessible property value. |
| 95 | + /// </summary> |
| 96 | + /// <param name="propertyID">The accessible property ID.</param> |
| 97 | + /// <returns>The accessible property value.</returns> |
| 98 | + internal override object GetPropertyValue(UiaCore.UIA propertyID) |
| 99 | + { |
| 100 | + switch (propertyID) |
| 101 | + { |
| 102 | + case UiaCore.UIA.RuntimeIdPropertyId: |
| 103 | + return RuntimeId; |
| 104 | + case UiaCore.UIA.ControlTypePropertyId: |
| 105 | + return UiaCore.UIA.ImageControlTypeId; |
| 106 | + case UiaCore.UIA.BoundingRectanglePropertyId: |
| 107 | + return BoundingRectangle; |
| 108 | + case UiaCore.UIA.NamePropertyId: |
| 109 | + return Name; |
| 110 | + case UiaCore.UIA.HelpTextPropertyId: |
| 111 | + return Help; |
| 112 | + case UiaCore.UIA.LegacyIAccessibleStatePropertyId: |
| 113 | + return State; |
| 114 | + case UiaCore.UIA.NativeWindowHandlePropertyId: |
| 115 | + return _window.Handle; |
| 116 | + case UiaCore.UIA.IsLegacyIAccessiblePatternAvailablePropertyId: |
| 117 | + return IsPatternSupported(UiaCore.UIA.LegacyIAccessiblePatternId); |
| 118 | + default: |
| 119 | + return base.GetPropertyValue(propertyID); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + internal override bool IsIAccessibleExSupported() |
| 124 | + { |
| 125 | + if (_controlItem != null) |
| 126 | + { |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + return base.IsIAccessibleExSupported(); |
| 131 | + } |
| 132 | + |
| 133 | + internal override bool IsPatternSupported(UiaCore.UIA patternId) |
| 134 | + { |
| 135 | + if (patternId == UiaCore.UIA.LegacyIAccessiblePatternId) |
| 136 | + { |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + return base.IsPatternSupported(patternId); |
| 141 | + } |
| 142 | + |
| 143 | + internal override bool IsReadOnly => true; |
| 144 | + |
| 145 | + public override string Name |
| 146 | + { |
| 147 | + get => string.IsNullOrEmpty(base.Name) ? _controlItem.Error : base.Name; |
| 148 | + set => base.Name = value; |
| 149 | + } |
| 150 | + |
| 151 | + public override AccessibleObject Parent => _window.AccessibilityObject; |
| 152 | + |
| 153 | + public override AccessibleRole Role => AccessibleRole.Alert; |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Gets the runtime ID. |
| 157 | + /// </summary> |
| 158 | + internal override int[] RuntimeId |
| 159 | + { |
| 160 | + get |
| 161 | + { |
| 162 | + var runtimeId = new int[4]; |
| 163 | + |
| 164 | + runtimeId[0] = _window.AccessibilityObject.RuntimeId[0]; |
| 165 | + runtimeId[1] = _window.AccessibilityObject.RuntimeId[1]; |
| 166 | + runtimeId[2] = _window.AccessibilityObject.RuntimeId[2]; |
| 167 | + runtimeId[3] = _controlItem.GetHashCode(); |
| 168 | + |
| 169 | + return runtimeId; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + public override AccessibleStates State => AccessibleStates.HasPopup | AccessibleStates.ReadOnly; |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments