-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathTextRange.cs
More file actions
339 lines (309 loc) · 11.6 KB
/
TextRange.cs
File metadata and controls
339 lines (309 loc) · 11.6 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
// (c) Copyright Microsoft, 2012.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.
using System;
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using UIAComWrapperInternal;
using UIAutomationClient = Interop.UIAutomationClient;
namespace System.Windows.Automation.Text
{
public class TextPatternRange
{
private UIAutomationClient.IUIAutomationTextRange _range;
private TextPattern _pattern;
internal TextPatternRange(UIAutomationClient.IUIAutomationTextRange range, TextPattern pattern)
{
Debug.Assert(range != null);
Debug.Assert(pattern != null);
this._range = range;
this._pattern = pattern;
}
internal static TextPatternRange Wrap(UIAutomationClient.IUIAutomationTextRange range, TextPattern pattern)
{
Debug.Assert(pattern != null);
if (range == null)
{
return null;
}
else
{
return new TextPatternRange(range, pattern);
}
}
public void AddToSelection()
{
try
{
_range.AddToSelection();
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
public TextPatternRange Clone()
{
try
{
return TextPatternRange.Wrap(_range.Clone(), this._pattern);
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
public bool Compare(TextPatternRange range)
{
try
{
return 0 != this._range.Compare(range.NativeRange);
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
public int CompareEndpoints(TextPatternRangeEndpoint endpoint, TextPatternRange targetRange, TextPatternRangeEndpoint targetEndpoint)
{
try
{
return this._range.CompareEndpoints(
(UIAutomationClient.TextPatternRangeEndpoint)endpoint,
targetRange.NativeRange,
(UIAutomationClient.TextPatternRangeEndpoint)targetEndpoint);
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
public void ExpandToEnclosingUnit(TextUnit unit)
{
try
{
this._range.ExpandToEnclosingUnit((UIAutomationClient.TextUnit)unit);
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
public TextPatternRange FindAttribute(AutomationTextAttribute attribute, object value, bool backward)
{
Utility.ValidateArgumentNonNull(attribute, "attribute");
Utility.ValidateArgumentNonNull(value, "value");
if ((attribute == TextPattern.CultureAttribute) && (value is CultureInfo))
{
value = ((CultureInfo)value).LCID;
}
try
{
return TextPatternRange.Wrap(
this._range.FindAttribute(attribute.Id, value, Utility.ConvertToInt(backward)), this._pattern);
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
public TextPatternRange FindText(string text, bool backward, bool ignoreCase)
{
try
{
return TextPatternRange.Wrap(
this._range.FindText(text, Utility.ConvertToInt(backward), Utility.ConvertToInt(ignoreCase)), this._pattern);
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
public object GetAttributeValue(AutomationTextAttribute attribute)
{
Utility.ValidateArgumentNonNull(attribute, "attribute");
try
{
PropertyTypeInfo info;
if (!Schema.GetPropertyTypeInfo(attribute, out info))
{
throw new ArgumentException("Unsupported Attribute");
}
object valueAsObject = this._range.GetAttributeValue(attribute.Id);
if (info.Type.IsEnum && (valueAsObject is int))
{
return Enum.ToObject(info.Type, (int)valueAsObject);
}
if ((valueAsObject != AutomationElement.NotSupported) && (info.ObjectConverter != null))
{
valueAsObject = info.ObjectConverter(valueAsObject);
}
return valueAsObject;
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
public Rect[] GetBoundingRectangles()
{
try
{
double[] unrolledRects = (double[])this._range.GetBoundingRectangles();
Rect[] result = null;
if (unrolledRects != null)
{
Debug.Assert(unrolledRects.Length % 4 == 0);
// If unrolledRects is somehow not a multiple of 4, we still will not
// overrun it, since (x / 4) * 4 <= x for C# integer math.
result = new Rect[unrolledRects.Length / 4];
for (int i = 0; i < result.Length; i++)
{
int j = i * 4; ;
result[i] = new Rect(unrolledRects[j], unrolledRects[j + 1], unrolledRects[j + 2], unrolledRects[j + 3]);
}
}
return result;
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
public AutomationElement[] GetChildren()
{
try
{
return Utility.ConvertToElementArray(this._range.GetChildren());
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
public AutomationElement GetEnclosingElement()
{
try
{
return AutomationElement.Wrap(this._range.GetEnclosingElement());
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
public string GetText(int maxLength)
{
try
{
return this._range.GetText(maxLength);
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
public int Move(TextUnit unit, int count)
{
try
{
return this._range.Move((UIAutomationClient.TextUnit)unit, count);
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
public void MoveEndpointByRange(TextPatternRangeEndpoint endpoint, TextPatternRange targetRange, TextPatternRangeEndpoint targetEndpoint)
{
try
{
this._range.MoveEndpointByRange(
(UIAutomationClient.TextPatternRangeEndpoint)endpoint,
targetRange.NativeRange,
(UIAutomationClient.TextPatternRangeEndpoint)targetEndpoint);
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
public int MoveEndpointByUnit(TextPatternRangeEndpoint endpoint, TextUnit unit, int count)
{
try
{
return this._range.MoveEndpointByUnit(
(UIAutomationClient.TextPatternRangeEndpoint)endpoint,
(UIAutomationClient.TextUnit)unit,
count);
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
public void RemoveFromSelection()
{
try
{
this._range.RemoveFromSelection();
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
public void ScrollIntoView(bool alignToTop)
{
try
{
this._range.ScrollIntoView(Utility.ConvertToInt(alignToTop));
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
public void Select()
{
try
{
this._range.Select();
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
internal static TextPatternRange[] Wrap(UIAutomationClient.IUIAutomationTextRangeArray ranges, TextPattern pattern)
{
if (ranges == null)
{
return null;
}
TextPatternRange[] rangeArray = new TextPatternRange[ranges.Length];
for (int i = 0; i < ranges.Length; i++)
{
rangeArray[i] = new TextPatternRange(ranges.GetElement(i), pattern);
}
return rangeArray;
}
internal UIAutomationClient.IUIAutomationTextRange NativeRange
{
get
{
return this._range;
}
}
public TextPattern TextPattern
{
get
{
return this._pattern;
}
}
}
}