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 "qwinrtuiagridprovider.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 
QWinRTUiaGridProvider(QAccessible::Id id)63 QWinRTUiaGridProvider::QWinRTUiaGridProvider(QAccessible::Id id) :
64     QWinRTUiaBaseProvider(id)
65 {
66     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
67 }
68 
~QWinRTUiaGridProvider()69 QWinRTUiaGridProvider::~QWinRTUiaGridProvider()
70 {
71     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
72 }
73 
74 // Returns the number of columns.
get_ColumnCount(INT32 * value)75 HRESULT STDMETHODCALLTYPE QWinRTUiaGridProvider::get_ColumnCount(INT32 *value)
76 {
77     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
78 
79     if (!value)
80         return E_INVALIDARG;
81 
82     QSharedPointer<QWinRTUiaControlMetadata> metadata = QWinRTUiaMetadataCache::instance()->metadataForId(id());
83     *value = metadata->columnCount();
84     return S_OK;
85 }
86 
87 // Returns the number of rows.
get_RowCount(INT32 * value)88 HRESULT STDMETHODCALLTYPE QWinRTUiaGridProvider::get_RowCount(INT32 *value)
89 {
90     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
91 
92     if (!value)
93         return E_INVALIDARG;
94 
95     QSharedPointer<QWinRTUiaControlMetadata> metadata = QWinRTUiaMetadataCache::instance()->metadataForId(id());
96     *value = metadata->rowCount();
97     return S_OK;
98 }
99 
GetItem(INT32 row,INT32 column,IIRawElementProviderSimple ** returnValue)100 HRESULT STDMETHODCALLTYPE QWinRTUiaGridProvider::GetItem(INT32 row, INT32 column, IIRawElementProviderSimple **returnValue)
101 {
102     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
103 
104     if (!returnValue)
105         return E_INVALIDARG;
106     *returnValue = nullptr;
107 
108     auto accid = id();
109     auto elementId = std::make_shared<QAccessible::Id>(0);
110 
111     if (!SUCCEEDED(QEventDispatcherWinRT::runOnMainThread([accid, row, column, elementId]() {
112         if (QAccessibleInterface *accessible = accessibleForId(accid)) {
113             if (QAccessibleTableInterface *tableInterface = accessible->tableInterface()) {
114                 if ((row >= 0) && (row < tableInterface->rowCount()) && (column >= 0) && (column < tableInterface->columnCount())) {
115                     if (QAccessibleInterface *cell = tableInterface->cellAt(row, column)) {
116                         *elementId = idForAccessible(cell);
117                         QWinRTUiaMetadataCache::instance()->load(*elementId);
118                     }
119                 }
120             }
121         }
122         return S_OK;
123     }))) {
124         return E_FAIL;
125     }
126 
127     if (!*elementId)
128         return E_FAIL;
129 
130     return QWinRTUiaMainProvider::rawProviderForAccessibleId(*elementId, returnValue);
131 }
132 
133 QT_END_NAMESPACE
134 
135 #endif // QT_CONFIG(accessibility)
136