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 "qwinrtuiatableprovider.h"
44 #include "qwinrtuiamainprovider.h"
45 #include "qwinrtuiametadatacache.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 
QWinRTUiaTableProvider(QAccessible::Id id)63 QWinRTUiaTableProvider::QWinRTUiaTableProvider(QAccessible::Id id) :
64     QWinRTUiaBaseProvider(id)
65 {
66     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
67 }
68 
~QWinRTUiaTableProvider()69 QWinRTUiaTableProvider::~QWinRTUiaTableProvider()
70 {
71     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
72 }
73 
74 // Returns the primary direction of traversal for the table.
get_RowOrColumnMajor(RowOrColumnMajor * value)75 HRESULT STDMETHODCALLTYPE QWinRTUiaTableProvider::get_RowOrColumnMajor(RowOrColumnMajor *value)
76 {
77     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
78 
79     if (!value)
80         return E_INVALIDARG;
81     *value = RowOrColumnMajor_Indeterminate;
82     return S_OK;
83 }
84 
85 // Gets the providers for all the column headers in the table.
GetColumnHeaders(UINT32 * returnValueSize,IIRawElementProviderSimple *** returnValue)86 HRESULT STDMETHODCALLTYPE QWinRTUiaTableProvider::GetColumnHeaders(UINT32 *returnValueSize, IIRawElementProviderSimple ***returnValue)
87 {
88     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
89 
90     if (!returnValueSize || !returnValue)
91         return E_INVALIDARG;
92     *returnValueSize = 0;
93     *returnValue = nullptr;
94 
95     auto accid = id();
96     auto elementIds = std::make_shared<QVarLengthArray<QAccessible::Id>>();
97 
98     if (!SUCCEEDED(QEventDispatcherWinRT::runOnMainThread([accid, elementIds]() {
99         if (QAccessibleInterface *accessible = accessibleForId(accid)) {
100             if (QAccessibleTableInterface *tableInterface = accessible->tableInterface()) {
101                 for (int i = 0; i < tableInterface->columnCount(); ++i) {
102                     if (QAccessibleInterface *cell = tableInterface->cellAt(0, i)) {
103                         QWinRTUiaMetadataCache::instance()->load(idForAccessible(cell));
104                         if (QAccessibleTableCellInterface *tableCellInterface = cell->tableCellInterface()) {
105                             QList<QAccessibleInterface *> headers = tableCellInterface->columnHeaderCells();
106                             for (auto header : qAsConst(headers)) {
107                                 QAccessible::Id headerId = idForAccessible(header);
108                                 QWinRTUiaMetadataCache::instance()->load(headerId);
109                                 elementIds->append(headerId);
110                             }
111                         }
112                     }
113                 }
114             }
115         }
116         return S_OK;
117     }))) {
118         return E_FAIL;
119     }
120 
121     return QWinRTUiaMainProvider::rawProviderArrayForAccessibleIdList(*elementIds, returnValueSize, returnValue);
122 }
123 
124 // Gets the providers for all the row headers in the table.
GetRowHeaders(UINT32 * returnValueSize,IIRawElementProviderSimple *** returnValue)125 HRESULT STDMETHODCALLTYPE QWinRTUiaTableProvider::GetRowHeaders(UINT32 *returnValueSize, IIRawElementProviderSimple ***returnValue)
126 {
127     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
128 
129     if (!returnValueSize || !returnValue)
130         return E_INVALIDARG;
131     *returnValueSize = 0;
132     *returnValue = nullptr;
133 
134     auto accid = id();
135     auto elementIds = std::make_shared<QVarLengthArray<QAccessible::Id>>();
136 
137     if (!SUCCEEDED(QEventDispatcherWinRT::runOnMainThread([accid, elementIds]() {
138         if (QAccessibleInterface *accessible = accessibleForId(accid)) {
139             if (QAccessibleTableInterface *tableInterface = accessible->tableInterface()) {
140                 for (int i = 0; i < tableInterface->rowCount(); ++i) {
141                     if (QAccessibleInterface *cell = tableInterface->cellAt(i, 0)) {
142                         QWinRTUiaMetadataCache::instance()->load(idForAccessible(cell));
143                         if (QAccessibleTableCellInterface *tableCellInterface = cell->tableCellInterface()) {
144                             QList<QAccessibleInterface *> headers = tableCellInterface->rowHeaderCells();
145                             for (auto header : qAsConst(headers)) {
146                                 QAccessible::Id headerId = idForAccessible(header);
147                                 QWinRTUiaMetadataCache::instance()->load(headerId);
148                                 elementIds->append(headerId);
149                             }
150                         }
151                     }
152                 }
153             }
154         }
155         return S_OK;
156     }))) {
157         return E_FAIL;
158     }
159 
160     return QWinRTUiaMainProvider::rawProviderArrayForAccessibleIdList(*elementIds, returnValueSize, returnValue);
161 }
162 
163 QT_END_NAMESPACE
164 
165 #endif // QT_CONFIG(accessibility)
166