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 "qwinrtuiagriditemprovider.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 
QWinRTUiaGridItemProvider(QAccessible::Id id)63 QWinRTUiaGridItemProvider::QWinRTUiaGridItemProvider(QAccessible::Id id) :
64     QWinRTUiaBaseProvider(id)
65 {
66     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
67 }
68 
~QWinRTUiaGridItemProvider()69 QWinRTUiaGridItemProvider::~QWinRTUiaGridItemProvider()
70 {
71     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
72 }
73 
74 // Returns the column index of the item.
get_Column(INT32 * value)75 HRESULT STDMETHODCALLTYPE QWinRTUiaGridItemProvider::get_Column(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->columnIndex();
84     return S_OK;
85 }
86 
87 // Returns the number of columns occupied by the item.
get_ColumnSpan(INT32 * value)88 HRESULT STDMETHODCALLTYPE QWinRTUiaGridItemProvider::get_ColumnSpan(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->columnCount();
97     return S_OK;
98 }
99 
100 // Returns the provider for the containing table/tree.
get_ContainingGrid(IIRawElementProviderSimple ** value)101 HRESULT STDMETHODCALLTYPE QWinRTUiaGridItemProvider::get_ContainingGrid(IIRawElementProviderSimple **value)
102 {
103     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
104 
105     if (!value)
106         return E_INVALIDARG;
107     *value = nullptr;
108 
109     auto accid = id();
110     auto elementId = std::make_shared<QAccessible::Id>(0);
111 
112     if (!SUCCEEDED(QEventDispatcherWinRT::runOnMainThread([accid, elementId]() {
113         if (QAccessibleInterface *accessible = accessibleForId(accid)) {
114             if (QAccessibleTableCellInterface *tableCellInterface = accessible->tableCellInterface()) {
115                 if (QAccessibleInterface *table = tableCellInterface->table()) {
116                     *elementId = idForAccessible(table);
117                     QWinRTUiaMetadataCache::instance()->load(*elementId);
118                 }
119             }
120         }
121         return S_OK;
122     }))) {
123         return E_FAIL;
124     }
125 
126     if (!*elementId)
127         return S_OK;
128 
129     return QWinRTUiaMainProvider::rawProviderForAccessibleId(*elementId, value);
130 }
131 
132 // Returns the row index of the item.
get_Row(INT32 * value)133 HRESULT STDMETHODCALLTYPE QWinRTUiaGridItemProvider::get_Row(INT32 *value)
134 {
135     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
136 
137     if (!value)
138         return E_INVALIDARG;
139 
140     QSharedPointer<QWinRTUiaControlMetadata> metadata = QWinRTUiaMetadataCache::instance()->metadataForId(id());
141     *value = metadata->rowIndex();
142     return S_OK;
143 }
144 
145 // Returns the number of rows occupied by the item.
get_RowSpan(INT32 * value)146 HRESULT STDMETHODCALLTYPE QWinRTUiaGridItemProvider::get_RowSpan(INT32 *value)
147 {
148     qCDebug(lcQpaUiAutomation) << __FUNCTION__;
149 
150     if (!value)
151         return E_INVALIDARG;
152 
153     QSharedPointer<QWinRTUiaControlMetadata> metadata = QWinRTUiaMetadataCache::instance()->metadataForId(id());
154     *value = metadata->rowCount();
155     return S_OK;
156 }
157 
158 QT_END_NAMESPACE
159 
160 #endif // QT_CONFIG(accessibility)
161