summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/windows/uiautomation/qwindowsuiatextrangeprovider.cpp
blob: 4d0203619633532afc4b8772c8262445134b24ea (plain)
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
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include <QtGui/qtguiglobal.h>
#if QT_CONFIG(accessibility)

#include "qwindowsuiatextrangeprovider.h"
#include "qwindowsuiamainprovider.h"
#include "qwindowsuiautils.h"
#include "qwindowscontext.h"

#include <QtGui/qaccessible.h>
#include <QtCore/qloggingcategory.h>
#include <QtCore/qstring.h>
#include <QtCore/qvarlengtharray.h>

QT_BEGIN_NAMESPACE

using namespace QWindowsUiAutomation;


QWindowsUiaTextRangeProvider::QWindowsUiaTextRangeProvider(QAccessible::Id id, int startOffset, int endOffset) :
    QWindowsUiaBaseProvider(id),
    m_startOffset(startOffset),
    m_endOffset(endOffset)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this << startOffset << endOffset;
}

QWindowsUiaTextRangeProvider::~QWindowsUiaTextRangeProvider()
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
}

HRESULT QWindowsUiaTextRangeProvider::AddToSelection()
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
    return Select();
}

HRESULT QWindowsUiaTextRangeProvider::Clone(ITextRangeProvider **pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;

    if (!pRetVal)
        return E_INVALIDARG;

    *pRetVal = new QWindowsUiaTextRangeProvider(id(), m_startOffset, m_endOffset);
    return S_OK;
}

// Two ranges are considered equal if their start/end points are the same.
HRESULT QWindowsUiaTextRangeProvider::Compare(ITextRangeProvider *range, BOOL *pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;

    if (!range || !pRetVal)
        return E_INVALIDARG;

    auto *targetProvider = static_cast<QWindowsUiaTextRangeProvider *>(range);
    *pRetVal = ((targetProvider->m_startOffset == m_startOffset) && (targetProvider->m_endOffset == m_endOffset));
    return S_OK;
}

// Compare different endpoinds between two providers.
HRESULT QWindowsUiaTextRangeProvider::CompareEndpoints(TextPatternRangeEndpoint endpoint,
                                                       ITextRangeProvider *targetRange,
                                                       TextPatternRangeEndpoint targetEndpoint,
                                                       int *pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__
        << "endpoint=" << endpoint << "targetRange=" << targetRange
        << "targetEndpoint=" << targetEndpoint << "this: " << this;

    if (!targetRange || !pRetVal)
        return E_INVALIDARG;

    auto *targetProvider = static_cast<QWindowsUiaTextRangeProvider *>(targetRange);

    int point = (endpoint == TextPatternRangeEndpoint_Start) ? m_startOffset : m_endOffset;
    int targetPoint = (targetEndpoint == TextPatternRangeEndpoint_Start) ?
                targetProvider->m_startOffset : targetProvider->m_endOffset;
    *pRetVal = point - targetPoint;
    return S_OK;
}

// Expands/normalizes the range for a given text unit.
HRESULT QWindowsUiaTextRangeProvider::ExpandToEnclosingUnit(TextUnit unit)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__ << "unit=" << unit << "this: " << this;

    QAccessibleInterface *accessible = accessibleInterface();
    if (!accessible)
        return UIA_E_ELEMENTNOTAVAILABLE;

    QAccessibleTextInterface *textInterface = accessible->textInterface();
    if (!textInterface)
        return UIA_E_ELEMENTNOTAVAILABLE;

    int len = textInterface->characterCount();
    if (len < 1) {
        m_startOffset = 0;
        m_endOffset = 0;
    } else {
        if (unit == TextUnit_Character) {
            m_startOffset = qBound(0, m_startOffset, len - 1);
            m_endOffset = m_startOffset + 1;
        } else {
            QString text = textInterface->text(0, len);
            const int start = m_startOffset >= 0 && m_startOffset < len
                              ? m_startOffset : len - 1;
            for (int t = start; t >= 0; --t) {
                if (!isTextUnitSeparator(unit, text[t]) && ((t == 0) || isTextUnitSeparator(unit, text[t - 1]))) {
                    m_startOffset = t;
                    break;
                }
            }
            for (int t = m_startOffset; t < len; ++t) {
                if ((t == len - 1) || (isTextUnitSeparator(unit, text[t]) && ((unit == TextUnit_Word) || !isTextUnitSeparator(unit, text[t + 1])))) {
                    m_endOffset = t + 1;
                    break;
                }
            }
        }
    }
    return S_OK;
}

// Not supported.
HRESULT QWindowsUiaTextRangeProvider::FindAttribute(TEXTATTRIBUTEID /* attributeId */,
                                                    VARIANT /* val */, BOOL /* backward */,
                                                    ITextRangeProvider **pRetVal)
{
    if (!pRetVal)
        return E_INVALIDARG;
    *pRetVal = nullptr;
    return S_OK;
}

// Returns the value of a given attribute.
HRESULT STDMETHODCALLTYPE QWindowsUiaTextRangeProvider::GetAttributeValue(TEXTATTRIBUTEID attributeId,
                                                                          VARIANT *pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__ << "attributeId=" << attributeId << "this: " << this;

    if (!pRetVal)
        return E_INVALIDARG;
    clearVariant(pRetVal);

    QAccessibleInterface *accessible = accessibleInterface();
    if (!accessible)
        return UIA_E_ELEMENTNOTAVAILABLE;

    QAccessibleTextInterface *textInterface = accessible->textInterface();
    if (!textInterface)
        return UIA_E_ELEMENTNOTAVAILABLE;

    switch (attributeId) {
    case UIA_IsReadOnlyAttributeId:
        setVariantBool(accessible->state().readOnly, pRetVal);
        break;
    case UIA_CaretPositionAttributeId:
        if (textInterface->cursorPosition() == 0)
            setVariantI4(CaretPosition_BeginningOfLine, pRetVal);
        else if (textInterface->cursorPosition() == textInterface->characterCount())
            setVariantI4(CaretPosition_EndOfLine, pRetVal);
        else
            setVariantI4(CaretPosition_Unknown, pRetVal);
        break;
    default:
        break;
    }
    return S_OK;
}

// Returns an array of bounding rectangles for text lines within the range.
HRESULT QWindowsUiaTextRangeProvider::GetBoundingRectangles(SAFEARRAY **pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;

    if (!pRetVal)
        return E_INVALIDARG;

    QAccessibleInterface *accessible = accessibleInterface();
    if (!accessible)
        return UIA_E_ELEMENTNOTAVAILABLE;

    QAccessibleTextInterface *textInterface = accessible->textInterface();
    if (!textInterface)
        return UIA_E_ELEMENTNOTAVAILABLE;

    QWindow *window = windowForAccessible(accessible);
    if (!window)
        return UIA_E_ELEMENTNOTAVAILABLE;

    int len = textInterface->characterCount();
    QVarLengthArray<QRect> rectList;

    if ((m_startOffset >= 0) && (m_endOffset <= len) && (m_startOffset < m_endOffset)) {
        int start, end;
        textInterface->textAtOffset(m_startOffset, QAccessible::LineBoundary, &start, &end);
        while ((start >= 0) && (end >= 0)) {
            int startRange = qMax(start, m_startOffset);
            int endRange = qMin(end, m_endOffset);
            if (startRange < endRange) {
                // Calculates a bounding rectangle for the line and adds it to the list.
                const QRect startRect = textInterface->characterRect(startRange);
                const QRect endRect = textInterface->characterRect(endRange - 1);
                const QRect lineRect(qMin(startRect.x(), endRect.x()),
                                     qMin(startRect.y(), endRect.y()),
                                     qMax(startRect.x() + startRect.width(), endRect.x() + endRect.width()) - qMin(startRect.x(), endRect.x()),
                                     qMax(startRect.y() + startRect.height(), endRect.y() + endRect.height()) - qMin(startRect.y(), endRect.y()));
                rectList.append(lineRect);
            }
            if (end >= len) break;
            textInterface->textAfterOffset(end + 1, QAccessible::LineBoundary, &start, &end);
        }
    }

    if ((*pRetVal = SafeArrayCreateVector(VT_R8, 0, 4 * rectList.size()))) {
        for (int i = 0; i < rectList.size(); ++i) {
            // Scale rect for high DPI screens.
            UiaRect uiaRect;
            rectToNativeUiaRect(rectList[i], window, &uiaRect);
            double coords[4] = { uiaRect.left, uiaRect.top, uiaRect.width, uiaRect.height };
            for (int j = 0; j < 4; ++j) {
                LONG idx = 4 * i + j;
                SafeArrayPutElement(*pRetVal, &idx, &coords[j]);
            }
        }
    }
    return S_OK;
}

// Returns an array of children elements embedded within the range.
HRESULT QWindowsUiaTextRangeProvider::GetChildren(SAFEARRAY **pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;

    if (!pRetVal)
        return E_INVALIDARG;
    // Not supporting any children.
    *pRetVal = SafeArrayCreateVector(VT_UNKNOWN, 0, 0);
    return S_OK;
}

// Returns a provider for the enclosing element (text to which the range belongs).
HRESULT QWindowsUiaTextRangeProvider::GetEnclosingElement(IRawElementProviderSimple **pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;

    if (!pRetVal)
        return E_INVALIDARG;

    QAccessibleInterface *accessible = accessibleInterface();
    if (!accessible)
        return UIA_E_ELEMENTNOTAVAILABLE;

    *pRetVal = QWindowsUiaMainProvider::providerForAccessible(accessible);
    return S_OK;
}

// Gets the text within the range.
HRESULT QWindowsUiaTextRangeProvider::GetText(int maxLength, BSTR *pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__ << maxLength << "this: " << this;

    if (!pRetVal)
        return E_INVALIDARG;
    *pRetVal = nullptr;

    QAccessibleInterface *accessible = accessibleInterface();
    if (!accessible)
        return UIA_E_ELEMENTNOTAVAILABLE;

    QAccessibleTextInterface *textInterface = accessible->textInterface();
    if (!textInterface)
        return UIA_E_ELEMENTNOTAVAILABLE;

    int len = textInterface->characterCount();
    QString rangeText;
    if ((m_startOffset >= 0) && (m_endOffset <= len) && (m_startOffset < m_endOffset))
        rangeText = textInterface->text(m_startOffset, m_endOffset);

    if ((maxLength > -1) && (rangeText.size() > maxLength))
        rangeText.truncate(maxLength);
    *pRetVal = bStrFromQString(rangeText);
    return S_OK;
}

// Moves the range a specified number of units (and normalizes it).
HRESULT QWindowsUiaTextRangeProvider::Move(TextUnit unit, int count, int *pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__ << "unit=" << unit << "count=" << count << "this: " << this;

    if (!pRetVal)
        return E_INVALIDARG;
    *pRetVal = 0;

    QAccessibleInterface *accessible = accessibleInterface();
    if (!accessible)
        return UIA_E_ELEMENTNOTAVAILABLE;

    QAccessibleTextInterface *textInterface = accessible->textInterface();
    if (!textInterface)
        return UIA_E_ELEMENTNOTAVAILABLE;

    int len = textInterface->characterCount();

    if (len < 1)
        return S_OK;

    if (unit == TextUnit_Character) {
        // Moves the start point, ensuring it lies within the bounds.
        int start = qBound(0, m_startOffset + count, len - 1);
        // If range was initially empty, leaves it as is; otherwise, normalizes it to one char.
        m_endOffset = (m_endOffset > m_startOffset) ? start + 1 : start;
        *pRetVal = start - m_startOffset; // Returns the actually moved distance.
        m_startOffset = start;
    } else {
        if (count > 0) {
            MoveEndpointByUnit(TextPatternRangeEndpoint_End, unit, count, pRetVal);
            MoveEndpointByUnit(TextPatternRangeEndpoint_Start, unit, count, pRetVal);
        } else {
            MoveEndpointByUnit(TextPatternRangeEndpoint_Start, unit, count, pRetVal);
            MoveEndpointByUnit(TextPatternRangeEndpoint_End, unit, count, pRetVal);
        }
    }
    return S_OK;
}

// Copies the value of an end point from one range to another.
HRESULT QWindowsUiaTextRangeProvider::MoveEndpointByRange(TextPatternRangeEndpoint endpoint,
                                                          ITextRangeProvider *targetRange,
                                                          TextPatternRangeEndpoint targetEndpoint)
{
    if (!targetRange)
        return E_INVALIDARG;

    qCDebug(lcQpaUiAutomation) << __FUNCTION__
        << "endpoint=" << endpoint << "targetRange=" << targetRange << "targetEndpoint=" << targetEndpoint << "this: " << this;

    auto *targetProvider = static_cast<QWindowsUiaTextRangeProvider *>(targetRange);

    int targetPoint = (targetEndpoint == TextPatternRangeEndpoint_Start) ?
                targetProvider->m_startOffset : targetProvider->m_endOffset;

    // If the moved endpoint crosses the other endpoint, that one is moved too.
    if (endpoint == TextPatternRangeEndpoint_Start) {
        m_startOffset = targetPoint;
        if (m_endOffset < m_startOffset)
            m_endOffset = m_startOffset;
    } else {
        m_endOffset = targetPoint;
        if (m_endOffset < m_startOffset)
            m_startOffset = m_endOffset;
    }
    return S_OK;
}

// Moves an endpoint an specific number of units.
HRESULT QWindowsUiaTextRangeProvider::MoveEndpointByUnit(TextPatternRangeEndpoint endpoint,
                                                         TextUnit unit, int count,
                                                         int *pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__
        << "endpoint=" << endpoint << "unit=" << unit << "count=" << count << "this: " << this;

    if (!pRetVal)
        return E_INVALIDARG;
    *pRetVal = 0;

    QAccessibleInterface *accessible = accessibleInterface();
    if (!accessible)
        return UIA_E_ELEMENTNOTAVAILABLE;

    QAccessibleTextInterface *textInterface = accessible->textInterface();
    if (!textInterface)
        return UIA_E_ELEMENTNOTAVAILABLE;

    int len = textInterface->characterCount();

    if (len < 1)
        return S_OK;

    if (unit == TextUnit_Character) {
        if (endpoint == TextPatternRangeEndpoint_Start) {
            int boundedValue = qBound(0, m_startOffset + count, len - 1);
            *pRetVal = boundedValue - m_startOffset;
            m_startOffset = boundedValue;
            m_endOffset = qBound(m_startOffset, m_endOffset, len);
        } else {
            int boundedValue = qBound(0, m_endOffset + count, len);
            *pRetVal = boundedValue - m_endOffset;
            m_endOffset = boundedValue;
            m_startOffset = qBound(0, m_startOffset, m_endOffset);
        }
    } else {
        QString text = textInterface->text(0, len);
        int moved = 0;

        if (endpoint == TextPatternRangeEndpoint_Start) {
            if (count > 0) {
                for (int t = m_startOffset; (t < len - 1) && (moved < count); ++t) {
                    if (isTextUnitSeparator(unit, text[t]) && !isTextUnitSeparator(unit, text[t + 1])) {
                        m_startOffset = t + 1;
                        ++moved;
                    }
                }
                m_endOffset = qBound(m_startOffset, m_endOffset, len);
            } else {
                const int start = m_startOffset >= 0 && m_startOffset <= len
                                  ? m_startOffset : len;
                for (int t = start - 1; (t >= 0) && (moved > count); --t) {
                    if (!isTextUnitSeparator(unit, text[t]) && ((t == 0) || isTextUnitSeparator(unit, text[t - 1]))) {
                        m_startOffset = t;
                        --moved;
                    }
                }
            }
        } else {
            if (count > 0) {
                for (int t = m_endOffset; (t < len) && (moved < count); ++t) {
                    if ((t == len - 1) || (isTextUnitSeparator(unit, text[t]) && ((unit == TextUnit_Word) || !isTextUnitSeparator(unit, text[t + 1])))) {
                        m_endOffset = t + 1;
                        ++moved;
                    }
                }
            } else {
                int end = 0;
                for (int t = m_endOffset - 2; (t > 0) && (moved > count); --t) {
                    if (isTextUnitSeparator(unit, text[t]) && ((unit == TextUnit_Word) || !isTextUnitSeparator(unit, text[t + 1]))) {
                        end = t + 1;
                        --moved;
                    }
                }
                m_endOffset = end;
                m_startOffset = qBound(0, m_startOffset, m_endOffset);
            }
        }
        *pRetVal = moved;
    }
    return S_OK;
}

HRESULT QWindowsUiaTextRangeProvider::RemoveFromSelection()
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__;
    // unselects all
    return unselect();
}

// Scrolls the range into view.
HRESULT QWindowsUiaTextRangeProvider::ScrollIntoView(BOOL alignToTop)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__ << "alignToTop=" << alignToTop << "this: " << this;

    QAccessibleInterface *accessible = accessibleInterface();
    if (!accessible)
        return UIA_E_ELEMENTNOTAVAILABLE;

    QAccessibleTextInterface *textInterface = accessible->textInterface();
    if (!textInterface)
        return UIA_E_ELEMENTNOTAVAILABLE;

    textInterface->scrollToSubstring(m_startOffset, m_endOffset);
    return S_OK;
}

// Selects the range.
HRESULT QWindowsUiaTextRangeProvider::Select()
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;

    QAccessibleInterface *accessible = accessibleInterface();
    if (!accessible)
        return UIA_E_ELEMENTNOTAVAILABLE;

    QAccessibleTextInterface *textInterface = accessible->textInterface();
    if (!textInterface)
        return UIA_E_ELEMENTNOTAVAILABLE;

    // unselects all and adds a new selection
    unselect();
    textInterface->addSelection(m_startOffset, m_endOffset);
    return S_OK;
}

// Not supported.
HRESULT QWindowsUiaTextRangeProvider::FindText(BSTR /* text */, BOOL /* backward */,
                                               BOOL /* ignoreCase */,
                                               ITextRangeProvider **pRetVal)
{
    if (!pRetVal)
        return E_INVALIDARG;
    *pRetVal = nullptr;
    return S_OK;
}

// Removes all selected ranges from the text element.
HRESULT QWindowsUiaTextRangeProvider::unselect()
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;

    QAccessibleInterface *accessible = accessibleInterface();
    if (!accessible)
        return UIA_E_ELEMENTNOTAVAILABLE;

    QAccessibleTextInterface *textInterface = accessible->textInterface();
    if (!textInterface)
        return UIA_E_ELEMENTNOTAVAILABLE;

    int selCount = textInterface->selectionCount();

    for (int i = selCount - 1; i >= 0; --i)
        textInterface->removeSelection(i);
    return S_OK;
}

QT_END_NAMESPACE

#endif // QT_CONFIG(accessibility)