summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/windows/uiautomation/qwindowsuiaselectionprovider.cpp
blob: 37148c655a8b31d84ad51ef91a981ab7df7a4621 (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
// 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 "qwindowsuiaselectionprovider.h"
#include "qwindowsuiamainprovider.h"
#include "qwindowsuiautils.h"
#include "qwindowscontext.h"

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

QT_BEGIN_NAMESPACE

using namespace QWindowsUiAutomation;


QWindowsUiaSelectionProvider::QWindowsUiaSelectionProvider(QAccessible::Id id) :
    QWindowsUiaBaseProvider(id)
{
}

QWindowsUiaSelectionProvider::~QWindowsUiaSelectionProvider()
{
}

// Returns an array of providers with the selected items.
HRESULT STDMETHODCALLTYPE QWindowsUiaSelectionProvider::GetSelection(SAFEARRAY **pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__;

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

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

    // First get/create list of selected items, then build a safe array with the right size.
    QList<QAccessibleInterface *> selectedList;
    if (QAccessibleSelectionInterface *selectionInterface = accessible->selectionInterface()) {
        selectedList = selectionInterface->selectedItems();
    } else {
        const int childCount = accessible->childCount();
        selectedList.reserve(childCount);
        for (int i = 0; i < childCount; ++i) {
            if (QAccessibleInterface *child = accessible->child(i)) {
                if (accessible->role() == QAccessible::PageTabList) {
                    if (child->role() == QAccessible::PageTab && child->state().focused) {
                        selectedList.append(child);
                    }
                } else {
                    if (child->state().selected) {
                        selectedList.append(child);
                    }
                }
            }
        }
    }

    if ((*pRetVal = SafeArrayCreateVector(VT_UNKNOWN, 0, selectedList.size()))) {
        for (LONG i = 0; i < selectedList.size(); ++i) {
            if (QWindowsUiaMainProvider *childProvider = QWindowsUiaMainProvider::providerForAccessible(selectedList.at(i))) {
                SafeArrayPutElement(*pRetVal, &i, static_cast<IRawElementProviderSimple *>(childProvider));
                childProvider->Release();
            }
        }
    }
    return S_OK;
}

HRESULT STDMETHODCALLTYPE QWindowsUiaSelectionProvider::get_CanSelectMultiple(BOOL *pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__;

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

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

    *pRetVal = accessible->state().multiSelectable;
    return S_OK;
}

HRESULT STDMETHODCALLTYPE QWindowsUiaSelectionProvider::get_IsSelectionRequired(BOOL *pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__;

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

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

    if (accessible->role() == QAccessible::PageTabList) {
        *pRetVal = TRUE;
    } else {

        // Initially returns false if none are selected. After the first selection, it may be required.
        bool anySelected = false;
        if (QAccessibleSelectionInterface *selectionInterface = accessible->selectionInterface()) {
            anySelected = selectionInterface->selectedItem(0) != nullptr;
        } else {
            for (int i = 0; i < accessible->childCount(); ++i) {
                if (QAccessibleInterface *child = accessible->child(i)) {
                    if (child->state().selected) {
                        anySelected = true;
                        break;
                    }
                }
            }
        }

        *pRetVal = anySelected && !accessible->state().multiSelectable && !accessible->state().extSelectable;
    }
    return S_OK;
}

HRESULT STDMETHODCALLTYPE QWindowsUiaSelectionProvider::get_FirstSelectedItem(__RPC__deref_out_opt IRawElementProviderSimple **pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__;

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

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

    QAccessibleInterface *firstSelectedChild = nullptr;
    if (QAccessibleSelectionInterface *selectionInterface = accessible->selectionInterface()) {
        firstSelectedChild = selectionInterface->selectedItem(0);
        if (!firstSelectedChild)
            return UIA_E_ELEMENTNOTAVAILABLE;
    } else {
        int i = 0;
        while (!firstSelectedChild && i < accessible->childCount()) {
            if (QAccessibleInterface *child = accessible->child(i)) {
                if (accessible->role() == QAccessible::PageTabList) {
                    if (child->role() == QAccessible::PageTab && child->state().focused)
                        firstSelectedChild = child;
                } else if (child->state().selected) {
                    firstSelectedChild = child;
                }
            }
            i++;
        }
    }

    if (!firstSelectedChild)
        return UIA_E_ELEMENTNOTAVAILABLE;

    if (QWindowsUiaMainProvider *childProvider = QWindowsUiaMainProvider::providerForAccessible(firstSelectedChild))
    {
        *pRetVal = static_cast<IRawElementProviderSimple *>(childProvider);
        return S_OK;
    }

    return S_FALSE;
}

HRESULT STDMETHODCALLTYPE QWindowsUiaSelectionProvider::get_LastSelectedItem(__RPC__deref_out_opt IRawElementProviderSimple **pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__;

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

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

    QAccessibleInterface *lastSelectedChild = nullptr;
    if (QAccessibleSelectionInterface *selectionInterface = accessible->selectionInterface()) {
        const int selectedItemCount = selectionInterface->selectedItemCount();
        if (selectedItemCount <= 0)
            return UIA_E_ELEMENTNOTAVAILABLE;
        lastSelectedChild = selectionInterface->selectedItem(selectedItemCount - 1);
    } else {
        int i = accessible->childCount() - 1;
        while (!lastSelectedChild && i >= 0) {
            if (QAccessibleInterface *child = accessible->child(i)) {
                if (accessible->role() == QAccessible::PageTabList) {
                    if (child->role() == QAccessible::PageTab && child->state().focused)
                        lastSelectedChild = child;
                } else if (child->state().selected) {
                    lastSelectedChild = child;
                }
            }
            i--;
        }
    }

    if (!lastSelectedChild)
        return UIA_E_ELEMENTNOTAVAILABLE;

    if (QWindowsUiaMainProvider *childProvider = QWindowsUiaMainProvider::providerForAccessible(lastSelectedChild))
    {
        *pRetVal = static_cast<IRawElementProviderSimple *>(childProvider);
        return S_OK;
    }

    return S_FALSE;
}

HRESULT STDMETHODCALLTYPE QWindowsUiaSelectionProvider::get_CurrentSelectedItem(__RPC__deref_out_opt IRawElementProviderSimple **pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__;
    return get_FirstSelectedItem(pRetVal);
}

HRESULT STDMETHODCALLTYPE QWindowsUiaSelectionProvider::get_ItemCount(__RPC__out int *pRetVal)
{
    qCDebug(lcQpaUiAutomation) << __FUNCTION__;

    if (!pRetVal)
        return E_INVALIDARG;
    *pRetVal = -1;

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


    if (QAccessibleSelectionInterface *selectionInterface = accessible->selectionInterface())
        *pRetVal = selectionInterface->selectedItemCount();
    else {
        int selectedCount = 0;
        for (int i = 0; i < accessible->childCount(); i++) {
            if (QAccessibleInterface *child = accessible->child(i)) {
                if (accessible->role() == QAccessible::PageTabList) {
                    if (child->role() == QAccessible::PageTab && child->state().focused)
                        selectedCount++;
                } else if (child->state().selected) {
                    selectedCount++;
                }
            }
        }
        *pRetVal = selectedCount;
    }

    return S_OK;
}

QT_END_NAMESPACE

#endif // QT_CONFIG(accessibility)