1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include <QtGui/qtguiglobal.h>
41 #if QT_CONFIG(accessibility)
42 
43 #include "qwinrtuiaselectionprovider.h"
44 #include "qwinrtuiametadatacache.h"
45 #include "qwinrtuiamainprovider.h"
46 #include "qwinrtuiautils.h"
47 
48 #include <QtGui/QAccessible>
49 #include <QtGui/QAccessibleInterface>
50 #include <QtCore/QLoggingCategory>
51 #include <QtCore/QString>
52 #include <QtCore/private/qeventdispatcher_winrt_p.h>
53 
54 #include <memory>
55 
56 QT_BEGIN_NAMESPACE
57 
58 using namespace QWinRTUiAutomation;
59 using namespace ABI::Windows::UI::Xaml::Automation;
60 using namespace ABI::Windows::UI::Xaml::Automation::Provider;
61 using namespace ABI::Windows::UI::Xaml::Automation::Peers;
62 
QWinRTUiaSelectionProvider(QAccessible::Id id)63 QWinRTUiaSelectionProvider::QWinRTUiaSelectionProvider(QAccessible::Id id) :
64     QWinRTUiaBaseProvider(id)
65 {
66     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
67 }
68 
~QWinRTUiaSelectionProvider()69 QWinRTUiaSelectionProvider::~QWinRTUiaSelectionProvider()
70 {
71     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
72 }
73 
get_CanSelectMultiple(boolean * value)74 HRESULT STDMETHODCALLTYPE QWinRTUiaSelectionProvider::get_CanSelectMultiple(boolean *value)
75 {
76     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
77 
78     if (!value)
79         return E_INVALIDARG;
80     QSharedPointer<QWinRTUiaControlMetadata> metadata = QWinRTUiaMetadataCache::instance()->metadataForId(id());
81     *value = (metadata->state().multiSelectable != 0);
82     return S_OK;
83 }
84 
get_IsSelectionRequired(boolean * value)85 HRESULT STDMETHODCALLTYPE QWinRTUiaSelectionProvider::get_IsSelectionRequired(boolean *value)
86 {
87     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
88 
89     if (!value)
90         return E_INVALIDARG;
91     *value = false;
92 
93     auto accid = id();
94     auto selectionRequired = std::make_shared<bool>(false);
95 
96     if (!SUCCEEDED(QEventDispatcherWinRT::runOnMainThread([accid, selectionRequired]() {
97         // Initially returns false if none are selected. After the first selection, it may be required.
98         bool anySelected = false;
99         if (QAccessibleInterface *accessible = accessibleForId(accid)) {
100             int childCount = accessible->childCount();
101             for (int i = 0; i < childCount; ++i) {
102                 if (QAccessibleInterface *childAcc = accessible->child(i)) {
103                      if (childAcc->state().selected) {
104                          anySelected = true;
105                          break;
106                      }
107                 }
108             }
109             *selectionRequired = anySelected && !accessible->state().multiSelectable && !accessible->state().extSelectable;
110         }
111         return S_OK;
112     }))) {
113         return E_FAIL;
114     }
115 
116     *value = *selectionRequired;
117     return S_OK;
118 }
119 
120 // Returns an array of providers with the selected items.
GetSelection(UINT32 * returnValueSize,IIRawElementProviderSimple *** returnValue)121 HRESULT STDMETHODCALLTYPE QWinRTUiaSelectionProvider::GetSelection(UINT32 *returnValueSize, IIRawElementProviderSimple ***returnValue)
122 {
123     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
124 
125     if (!returnValueSize || !returnValue)
126         return E_INVALIDARG;
127     *returnValueSize = 0;
128     *returnValue = nullptr;
129 
130     auto accid = id();
131     auto elementIds = std::make_shared<QVarLengthArray<QAccessible::Id>>();
132 
133     if (!SUCCEEDED(QEventDispatcherWinRT::runOnMainThread([accid, elementIds]() {
134         if (QAccessibleInterface *accessible = accessibleForId(accid)) {
135             int childCount = accessible->childCount();
136             for (int i = 0; i < childCount; ++i) {
137                 if (QAccessibleInterface *childAcc = accessible->child(i)) {
138                      if (childAcc->state().selected) {
139                          QAccessible::Id childId = idForAccessible(childAcc);
140                          QWinRTUiaMetadataCache::instance()->load(childId);
141                          elementIds->append(childId);
142                      }
143                 }
144             }
145         }
146         return S_OK;
147     }))) {
148         return E_FAIL;
149     }
150 
151     return QWinRTUiaMainProvider::rawProviderArrayForAccessibleIdList(*elementIds, returnValueSize, returnValue);
152 }
153 
154 QT_END_NAMESPACE
155 
156 #endif // QT_CONFIG(accessibility)
157