1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 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 "qwindowsuiatextprovider.h"
44 #include "qwindowsuiautils.h"
45 #include "qwindowscontext.h"
46
47 #include <QtGui/qaccessible.h>
48 #include <QtCore/qloggingcategory.h>
49 #include <QtCore/qstring.h>
50
51 QT_BEGIN_NAMESPACE
52
53 using namespace QWindowsUiAutomation;
54
55
QWindowsUiaTextProvider(QAccessible::Id id)56 QWindowsUiaTextProvider::QWindowsUiaTextProvider(QAccessible::Id id) :
57 QWindowsUiaBaseProvider(id)
58 {
59 }
60
~QWindowsUiaTextProvider()61 QWindowsUiaTextProvider::~QWindowsUiaTextProvider()
62 {
63 }
64
QueryInterface(REFIID iid,LPVOID * iface)65 HRESULT STDMETHODCALLTYPE QWindowsUiaTextProvider::QueryInterface(REFIID iid, LPVOID *iface)
66 {
67 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
68
69 if (!iface)
70 return E_INVALIDARG;
71 *iface = nullptr;
72
73 const bool result = qWindowsComQueryUnknownInterfaceMulti<ITextProvider>(this, iid, iface)
74 || qWindowsComQueryInterface<ITextProvider>(this, iid, iface)
75 || qWindowsComQueryInterface<ITextProvider2>(this, iid, iface);
76 return result ? S_OK : E_NOINTERFACE;
77 }
78
79 // Returns an array of providers for the selected text ranges.
GetSelection(SAFEARRAY ** pRetVal)80 HRESULT STDMETHODCALLTYPE QWindowsUiaTextProvider::GetSelection(SAFEARRAY **pRetVal)
81 {
82 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
83
84 if (!pRetVal)
85 return E_INVALIDARG;
86 *pRetVal = nullptr;
87
88 QAccessibleInterface *accessible = accessibleInterface();
89 if (!accessible)
90 return UIA_E_ELEMENTNOTAVAILABLE;
91
92 QAccessibleTextInterface *textInterface = accessible->textInterface();
93 if (!textInterface)
94 return UIA_E_ELEMENTNOTAVAILABLE;
95
96 int selCount = textInterface->selectionCount();
97 if (selCount > 0) {
98 // Build a safe array with the text range providers.
99 if ((*pRetVal = SafeArrayCreateVector(VT_UNKNOWN, 0, selCount))) {
100 for (LONG i = 0; i < selCount; ++i) {
101 int startOffset = 0, endOffset = 0;
102 textInterface->selection((int)i, &startOffset, &endOffset);
103 auto *textRangeProvider = new QWindowsUiaTextRangeProvider(id(), startOffset, endOffset);
104 SafeArrayPutElement(*pRetVal, &i, static_cast<IUnknown *>(textRangeProvider));
105 textRangeProvider->Release();
106 }
107 }
108 } else {
109 // If there is no selection, we return an array with a single degenerate (empty) text range at the cursor position.
110 if ((*pRetVal = SafeArrayCreateVector(VT_UNKNOWN, 0, 1))) {
111 LONG i = 0;
112 int cursorPosition = textInterface->cursorPosition();
113 auto *textRangeProvider = new QWindowsUiaTextRangeProvider(id(), cursorPosition, cursorPosition);
114 SafeArrayPutElement(*pRetVal, &i, static_cast<IUnknown *>(textRangeProvider));
115 textRangeProvider->Release();
116 }
117 }
118 return S_OK;
119 }
120
121 // Returns an array of providers for the visible text ranges.
GetVisibleRanges(SAFEARRAY ** pRetVal)122 HRESULT STDMETHODCALLTYPE QWindowsUiaTextProvider::GetVisibleRanges(SAFEARRAY **pRetVal)
123 {
124 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
125
126 if (!pRetVal)
127 return E_INVALIDARG;
128 *pRetVal = nullptr;
129
130 QAccessibleInterface *accessible = accessibleInterface();
131 if (!accessible)
132 return UIA_E_ELEMENTNOTAVAILABLE;
133
134 QAccessibleTextInterface *textInterface = accessible->textInterface();
135 if (!textInterface)
136 return UIA_E_ELEMENTNOTAVAILABLE;
137
138 // Considering the entire text as visible.
139 if ((*pRetVal = SafeArrayCreateVector(VT_UNKNOWN, 0, 1))) {
140 LONG i = 0;
141 auto *textRangeProvider = new QWindowsUiaTextRangeProvider(id(), 0, textInterface->characterCount());
142 SafeArrayPutElement(*pRetVal, &i, static_cast<IUnknown *>(textRangeProvider));
143 textRangeProvider->Release();
144 }
145 return S_OK;
146 }
147
RangeFromChild(IRawElementProviderSimple *,ITextRangeProvider ** pRetVal)148 HRESULT STDMETHODCALLTYPE QWindowsUiaTextProvider::RangeFromChild(IRawElementProviderSimple * /*childElement*/,
149 ITextRangeProvider **pRetVal)
150 {
151 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
152
153 if (!pRetVal)
154 return E_INVALIDARG;
155 *pRetVal = nullptr;
156 // No children supported.
157 return S_OK;
158 }
159
160 // Returns a degenerate text range at the specified point.
RangeFromPoint(UiaPoint point,ITextRangeProvider ** pRetVal)161 HRESULT STDMETHODCALLTYPE QWindowsUiaTextProvider::RangeFromPoint(UiaPoint point, ITextRangeProvider **pRetVal)
162 {
163 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
164
165 if (!pRetVal)
166 return E_INVALIDARG;
167 *pRetVal = nullptr;
168
169 QAccessibleInterface *accessible = accessibleInterface();
170 if (!accessible)
171 return UIA_E_ELEMENTNOTAVAILABLE;
172
173 QAccessibleTextInterface *textInterface = accessible->textInterface();
174 if (!textInterface)
175 return UIA_E_ELEMENTNOTAVAILABLE;
176
177 QWindow *window = windowForAccessible(accessible);
178 if (!window)
179 return UIA_E_ELEMENTNOTAVAILABLE;
180
181 QPoint pt;
182 nativeUiaPointToPoint(point, window, &pt);
183
184 int offset = textInterface->offsetAtPoint(pt);
185 if ((offset >= 0) && (offset < textInterface->characterCount())) {
186 *pRetVal = new QWindowsUiaTextRangeProvider(id(), offset, offset);
187 }
188 return S_OK;
189 }
190
191 // Returns a text range provider for the entire text.
get_DocumentRange(ITextRangeProvider ** pRetVal)192 HRESULT STDMETHODCALLTYPE QWindowsUiaTextProvider::get_DocumentRange(ITextRangeProvider **pRetVal)
193 {
194 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
195
196 if (!pRetVal)
197 return E_INVALIDARG;
198 *pRetVal = nullptr;
199
200 QAccessibleInterface *accessible = accessibleInterface();
201 if (!accessible)
202 return UIA_E_ELEMENTNOTAVAILABLE;
203
204 QAccessibleTextInterface *textInterface = accessible->textInterface();
205 if (!textInterface)
206 return UIA_E_ELEMENTNOTAVAILABLE;
207
208 *pRetVal = new QWindowsUiaTextRangeProvider(id(), 0, textInterface->characterCount());
209 return S_OK;
210 }
211
212 // Currently supporting single selection.
get_SupportedTextSelection(SupportedTextSelection * pRetVal)213 HRESULT STDMETHODCALLTYPE QWindowsUiaTextProvider::get_SupportedTextSelection(SupportedTextSelection *pRetVal)
214 {
215 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
216
217 if (!pRetVal)
218 return E_INVALIDARG;
219 *pRetVal = SupportedTextSelection_Single;
220 return S_OK;
221 }
222
223 // Not supporting annotations.
RangeFromAnnotation(IRawElementProviderSimple *,ITextRangeProvider ** pRetVal)224 HRESULT STDMETHODCALLTYPE QWindowsUiaTextProvider::RangeFromAnnotation(IRawElementProviderSimple * /*annotationElement*/, ITextRangeProvider **pRetVal)
225 {
226 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
227
228 if (!pRetVal)
229 return E_INVALIDARG;
230 *pRetVal = nullptr;
231 return S_OK;
232 }
233
GetCaretRange(BOOL * isActive,ITextRangeProvider ** pRetVal)234 HRESULT STDMETHODCALLTYPE QWindowsUiaTextProvider::GetCaretRange(BOOL *isActive, ITextRangeProvider **pRetVal)
235 {
236 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
237
238 if (!isActive || !pRetVal)
239 return E_INVALIDARG;
240 *isActive = FALSE;
241 *pRetVal = nullptr;
242
243 QAccessibleInterface *accessible = accessibleInterface();
244 if (!accessible)
245 return UIA_E_ELEMENTNOTAVAILABLE;
246
247 QAccessibleTextInterface *textInterface = accessible->textInterface();
248 if (!textInterface)
249 return UIA_E_ELEMENTNOTAVAILABLE;
250
251 *isActive = accessible->state().focused;
252
253 int cursorPosition = textInterface->cursorPosition();
254 *pRetVal = new QWindowsUiaTextRangeProvider(id(), cursorPosition, cursorPosition);
255 return S_OK;
256 }
257
258 QT_END_NAMESPACE
259
260 #endif // QT_CONFIG(accessibility)
261