|
| 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.Diagnostics; |
| 8 | +using System.Drawing; |
| 9 | +using System.Runtime.InteropServices; |
| 10 | +using System.Windows.Forms.VisualStyles; |
| 11 | +using static Interop; |
| 12 | + |
| 13 | +namespace System.Windows.Forms |
| 14 | +{ |
| 15 | + public abstract partial class UpDownBase |
| 16 | + { |
| 17 | + internal partial class UpDownButtons |
| 18 | + { |
| 19 | + internal partial class UpDownButtonsAccessibleObject : ControlAccessibleObject |
| 20 | + { |
| 21 | + private DirectionButtonAccessibleObject upButton; |
| 22 | + private DirectionButtonAccessibleObject downButton; |
| 23 | + |
| 24 | + private UpDownButtons _owner; |
| 25 | + |
| 26 | + public UpDownButtonsAccessibleObject(UpDownButtons owner) : base(owner) |
| 27 | + { |
| 28 | + _owner = owner; |
| 29 | + } |
| 30 | + |
| 31 | + internal override UiaCore.IRawElementProviderFragment ElementProviderFromPoint(double x, double y) |
| 32 | + { |
| 33 | + AccessibleObject element = HitTest((int)x, (int)y); |
| 34 | + |
| 35 | + if (element != null) |
| 36 | + { |
| 37 | + return element; |
| 38 | + } |
| 39 | + |
| 40 | + return base.ElementProviderFromPoint(x, y); |
| 41 | + } |
| 42 | + |
| 43 | + internal override UiaCore.IRawElementProviderFragment FragmentNavigate( |
| 44 | + UiaCore.NavigateDirection direction) => direction switch |
| 45 | + { |
| 46 | + UiaCore.NavigateDirection.FirstChild => GetChild(0), |
| 47 | + UiaCore.NavigateDirection.LastChild => GetChild(1), |
| 48 | + _ => base.FragmentNavigate(direction), |
| 49 | + }; |
| 50 | + |
| 51 | + internal override UiaCore.IRawElementProviderFragmentRoot FragmentRoot => this; |
| 52 | + |
| 53 | + private DirectionButtonAccessibleObject UpButton |
| 54 | + => upButton ??= new DirectionButtonAccessibleObject(this, true); |
| 55 | + |
| 56 | + private DirectionButtonAccessibleObject DownButton |
| 57 | + => downButton ??= new DirectionButtonAccessibleObject(this, false); |
| 58 | + |
| 59 | + public override AccessibleObject GetChild(int index) => index switch |
| 60 | + { |
| 61 | + 0 => UpButton, |
| 62 | + 1 => DownButton, |
| 63 | + _ => null, |
| 64 | + }; |
| 65 | + |
| 66 | + public override int GetChildCount() => 2; |
| 67 | + |
| 68 | + internal override object GetPropertyValue(UiaCore.UIA propertyID) => propertyID switch |
| 69 | + { |
| 70 | + UiaCore.UIA.NamePropertyId => Name, |
| 71 | + UiaCore.UIA.RuntimeIdPropertyId => RuntimeId, |
| 72 | + UiaCore.UIA.BoundingRectanglePropertyId => Bounds, |
| 73 | + UiaCore.UIA.LegacyIAccessibleStatePropertyId => State, |
| 74 | + UiaCore.UIA.LegacyIAccessibleRolePropertyId => Role, |
| 75 | + _ => base.GetPropertyValue(propertyID), |
| 76 | + }; |
| 77 | + |
| 78 | + public override AccessibleObject HitTest(int x, int y) |
| 79 | + { |
| 80 | + if (UpButton.Bounds.Contains(x, y)) |
| 81 | + { |
| 82 | + return UpButton; |
| 83 | + } |
| 84 | + |
| 85 | + if (DownButton.Bounds.Contains(x, y)) |
| 86 | + { |
| 87 | + return DownButton; |
| 88 | + } |
| 89 | + |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + internal override UiaCore.IRawElementProviderSimple HostRawElementProvider |
| 94 | + { |
| 95 | + get |
| 96 | + { |
| 97 | + if (HandleInternal == IntPtr.Zero) |
| 98 | + { |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + UiaCore.UiaHostProviderFromHwnd(new HandleRef(this, HandleInternal), out UiaCore.IRawElementProviderSimple provider); |
| 103 | + return provider; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public override string Name |
| 108 | + { |
| 109 | + get |
| 110 | + { |
| 111 | + string baseName = base.Name; |
| 112 | + if (string.IsNullOrEmpty(baseName)) |
| 113 | + { |
| 114 | + return SR.DefaultUpDownButtonsAccessibleName; |
| 115 | + } |
| 116 | + |
| 117 | + return baseName; |
| 118 | + } |
| 119 | + set => base.Name = value; |
| 120 | + } |
| 121 | + |
| 122 | + public override AccessibleObject Parent => _owner.AccessibilityObject; |
| 123 | + |
| 124 | + public override AccessibleRole Role |
| 125 | + { |
| 126 | + get |
| 127 | + { |
| 128 | + AccessibleRole role = Owner.AccessibleRole; |
| 129 | + if (role != AccessibleRole.Default) |
| 130 | + { |
| 131 | + return role; |
| 132 | + } |
| 133 | + |
| 134 | + return AccessibleRole.SpinButton; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Gets the runtime ID. |
| 140 | + /// </summary> |
| 141 | + internal override int[] RuntimeId |
| 142 | + { |
| 143 | + get |
| 144 | + { |
| 145 | + if (_owner is null) |
| 146 | + { |
| 147 | + return base.RuntimeId; |
| 148 | + } |
| 149 | + |
| 150 | + // We need to provide a unique ID others are implementing this in the same manner first item |
| 151 | + // is static - 0x2a (RuntimeIDFirstItem) second item can be anything, but here it is a hash. |
| 152 | + |
| 153 | + var runtimeId = new int[3]; |
| 154 | + runtimeId[0] = RuntimeIDFirstItem; |
| 155 | + runtimeId[1] = (int)(long)_owner.InternalHandle; |
| 156 | + runtimeId[2] = _owner.GetHashCode(); |
| 157 | + |
| 158 | + return runtimeId; |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments