Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwindowsuiamainprovider.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <QtGui/qtguiglobal.h>
6#if QT_CONFIG(accessibility)
7
8#include "qwindowsuiamainprovider.h"
9#include "qwindowsuiavalueprovider.h"
10#include "qwindowsuiarangevalueprovider.h"
11#include "qwindowsuiatextprovider.h"
12#include "qwindowsuiatoggleprovider.h"
13#include "qwindowsuiainvokeprovider.h"
14#include "qwindowsuiaselectionprovider.h"
15#include "qwindowsuiaselectionitemprovider.h"
16#include "qwindowsuiatableprovider.h"
17#include "qwindowsuiatableitemprovider.h"
18#include "qwindowsuiagridprovider.h"
19#include "qwindowsuiagriditemprovider.h"
20#include "qwindowsuiawindowprovider.h"
21#include "qwindowsuiaexpandcollapseprovider.h"
22#include "qwindowscontext.h"
23#include "qwindowsuiautils.h"
24#include "qwindowsuiaprovidercache.h"
25
26#include <QtCore/qloggingcategory.h>
27#include <QtGui/private/qaccessiblebridgeutils_p.h>
28#include <QtGui/qaccessible.h>
29#include <QtGui/qguiapplication.h>
30#include <QtGui/qwindow.h>
31#include <QtCore/private/qcomvariant_p.h>
32
33#if !defined(Q_CC_BOR) && !defined (Q_CC_GNU)
34#include <comdef.h>
35#endif
36
37#include <QtCore/qt_windows.h>
38
39QT_BEGIN_NAMESPACE
40
41using namespace QWindowsUiAutomation;
42
43// Returns a cached instance of the provider for a specific accessible interface.
44ComPtr<QWindowsUiaMainProvider> QWindowsUiaMainProvider::providerForAccessible(QAccessibleInterface *accessible)
45{
46 if (!accessible)
47 return nullptr;
48
49 QAccessible::Id id = QAccessible::uniqueId(accessible);
50 QWindowsUiaProviderCache *providerCache = QWindowsUiaProviderCache::instance();
51 ComPtr<QWindowsUiaMainProvider> provider = providerCache->providerForId(id);
52
53 if (!provider) {
54 provider = makeComObject<QWindowsUiaMainProvider>(accessible);
55 providerCache->insert(id, provider.Get()); // Cache holds weak references
56 }
57 return provider;
58}
59
60QWindowsUiaMainProvider::QWindowsUiaMainProvider(QAccessibleInterface *a)
61 : QWindowsUiaBaseProvider(QAccessible::uniqueId(a))
62{
63}
64
65QWindowsUiaMainProvider::~QWindowsUiaMainProvider()
66{
67}
68
69void QWindowsUiaMainProvider::notifyFocusChange(QAccessibleEvent *event)
70{
71 if (QAccessibleInterface *accessible = event->accessibleInterface()) {
72 // If this is a complex element, raise event for the focused child instead.
73 if (accessible->childCount()) {
74 if (QAccessibleInterface *child = accessible->focusChild())
75 accessible = child;
76 }
77 if (auto provider = providerForAccessible(accessible))
78 UiaRaiseAutomationEvent(provider.Get(), UIA_AutomationFocusChangedEventId);
79 }
80}
81
82void QWindowsUiaMainProvider::notifyStateChange(QAccessibleStateChangeEvent *event)
83{
84 if (QAccessibleInterface *accessible = event->accessibleInterface()) {
85 if (event->changedStates().checked || event->changedStates().checkStateMixed) {
86 // Notifies states changes in checkboxes, switches, and checkable item view items.
87 if (accessible->role() == QAccessible::CheckBox
88 || accessible->role() == QAccessible::Switch
89 || accessible->role() == QAccessible::Cell
90 || accessible->role() == QAccessible::ListItem
91 || accessible->role() == QAccessible::TreeItem) {
92 if (auto provider = providerForAccessible(accessible)) {
93 long toggleState = ToggleState_Off;
94 if (accessible->state().checked)
95 toggleState = accessible->state().checkStateMixed ? ToggleState_Indeterminate : ToggleState_On;
96
97 QComVariant oldVal;
98 QComVariant newVal{toggleState};
99 UiaRaiseAutomationPropertyChangedEvent(
100 provider.Get(), UIA_ToggleToggleStatePropertyId, oldVal.get(), newVal.get());
101 }
102 }
103 }
104 if (event->changedStates().expanded) {
105 if (accessible->state().expandable) {
106 if (auto provider = providerForAccessible(accessible)) {
107 long expandedState = accessible->state().expanded ? ExpandCollapseState_Expanded : ExpandCollapseState_Collapsed;
108
109 QComVariant oldVal;
110 QComVariant newVal{expandedState};
111 UiaRaiseAutomationPropertyChangedEvent(
112 provider.Get(), UIA_ExpandCollapseExpandCollapseStatePropertyId, oldVal.get(), newVal.get());
113 }
114 }
115 }
116 if (event->changedStates().active) {
117 if (accessible->role() == QAccessible::Window) {
118 // Notifies window opened/closed.
119 if (auto provider = providerForAccessible(accessible)) {
120 if (accessible->state().active) {
121 UiaRaiseAutomationEvent(provider.Get(), UIA_Window_WindowOpenedEventId);
122 if (QAccessibleInterface *focused = accessible->focusChild()) {
123 if (auto focusedProvider = providerForAccessible(focused)) {
124 UiaRaiseAutomationEvent(focusedProvider.Get(),
125 UIA_AutomationFocusChangedEventId);
126 }
127 }
128 } else {
129 UiaRaiseAutomationEvent(provider.Get(), UIA_Window_WindowClosedEventId);
130 }
131 }
132 }
133 }
134 }
135}
136
137void QWindowsUiaMainProvider::notifyValueChange(QAccessibleValueChangeEvent *event)
138{
139 if (QAccessibleInterface *accessible = event->accessibleInterface()) {
140 if (accessible->role() == QAccessible::ComboBox && accessible->childCount() > 0) {
141 QAccessibleInterface *listacc = accessible->child(0);
142 if (listacc && listacc->role() == QAccessible::List) {
143 int count = listacc->childCount();
144 for (int i = 0; i < count; ++i) {
145 QAccessibleInterface *item = listacc->child(i);
146 if (item && item->isValid() && item->text(QAccessible::Name) == event->value()) {
147 if (!item->state().selected) {
148 if (QAccessibleActionInterface *actionInterface = item->actionInterface())
149 actionInterface->doAction(QAccessibleActionInterface::toggleAction());
150 }
151 break;
152 }
153 }
154 }
155 }
156 if (event->value().typeId() == QMetaType::QString) {
157 if (auto provider = providerForAccessible(accessible)) {
158 // Notifies changes in string values.
159 const QComVariant oldVal;
160 const QComVariant newVal{ event->value().toString() };
161 UiaRaiseAutomationPropertyChangedEvent(provider.Get(), UIA_ValueValuePropertyId,
162 oldVal.get(), newVal.get());
163 }
164 } else if (QAccessibleValueInterface *valueInterface = accessible->valueInterface()) {
165 if (auto provider = providerForAccessible(accessible)) {
166 // Notifies changes in values of controls supporting the value interface.
167 const QComVariant oldVal;
168 const QComVariant newVal{ valueInterface->currentValue().toDouble() };
169 UiaRaiseAutomationPropertyChangedEvent(
170 provider.Get(), UIA_RangeValueValuePropertyId, oldVal.get(), newVal.get());
171 }
172 }
173 }
174}
175
176void QWindowsUiaMainProvider::notifyNameChange(QAccessibleEvent *event)
177{
178 if (QAccessibleInterface *accessible = event->accessibleInterface()) {
179 // Restrict notification to combo boxes and the currently focused element, which
180 // need it for accessibility, in order to avoid slowdowns with unnecessary notifications.
181 if (accessible->role() == QAccessible::ComboBox || accessible->state().focused) {
182 if (auto provider = providerForAccessible(accessible)) {
183 QComVariant oldVal;
184 QComVariant newVal{ accessible->text(QAccessible::Name) };
185 UiaRaiseAutomationPropertyChangedEvent(provider.Get(), UIA_NamePropertyId,
186 oldVal.get(), newVal.get());
187 }
188 }
189 }
190}
191
192void QWindowsUiaMainProvider::notifyRoleChange(QAccessibleEvent *event)
193{
194 if (QAccessibleInterface *accessible = event->accessibleInterface()) {
195 if (auto provider = providerForAccessible(accessible)) {
196 QComVariant oldVal;
197 QComVariant newVal{ roleToControlTypeId(accessible->role()) };
198 UiaRaiseAutomationPropertyChangedEvent(provider.Get(), UIA_ControlTypePropertyId,
199 oldVal.get(), newVal.get());
200 }
201 }
202}
203
204void QWindowsUiaMainProvider::notifySelectionChange(QAccessibleEvent *event)
205{
206 if (QAccessibleInterface *accessible = event->accessibleInterface()) {
207 if (auto provider = providerForAccessible(accessible))
208 UiaRaiseAutomationEvent(provider.Get(), UIA_SelectionItem_ElementSelectedEventId);
209 }
210}
211
212// Notifies changes in text content and selection state of text controls.
213void QWindowsUiaMainProvider::notifyTextChange(QAccessibleEvent *event)
214{
215 if (QAccessibleInterface *accessible = event->accessibleInterface()) {
216 if (accessible->textInterface()) {
217 if (auto provider = providerForAccessible(accessible)) {
218 if (event->type() == QAccessible::TextSelectionChanged) {
219 UiaRaiseAutomationEvent(provider.Get(), UIA_Text_TextSelectionChangedEventId);
220 } else if (event->type() == QAccessible::TextCaretMoved) {
221 if (!accessible->state().readOnly) {
222 UiaRaiseAutomationEvent(provider.Get(),
223 UIA_Text_TextSelectionChangedEventId);
224 }
225 } else {
226 UiaRaiseAutomationEvent(provider.Get(), UIA_Text_TextChangedEventId);
227 }
228 }
229 }
230 }
231}
232
233void QWindowsUiaMainProvider::raiseNotification(QAccessibleAnnouncementEvent *event)
234{
235 if (QAccessibleInterface *accessible = event->accessibleInterface()) {
236 if (auto provider = providerForAccessible(accessible)) {
237 QBStr message{ event->message() };
238 QAccessible::AnnouncementPoliteness prio = event->politeness();
239 NotificationProcessing processing = (prio == QAccessible::AnnouncementPoliteness::Assertive)
240 ? NotificationProcessing_ImportantAll
241 : NotificationProcessing_All;
242 QBStr activityId{ QString::fromLatin1("") };
243#if !defined(Q_CC_MSVC) || !defined(QT_WIN_SERVER_2016_COMPAT)
244 UiaRaiseNotificationEvent(provider.Get(), NotificationKind_Other, processing, message.bstr(),
245 activityId.bstr());
246#else
247 HMODULE uiautomationcore = GetModuleHandleW(L"UIAutomationCore.dll");
248 if (uiautomationcore != NULL) {
249 typedef HRESULT (WINAPI *EVENTFUNC)(IRawElementProviderSimple *, NotificationKind,
250 NotificationProcessing, BSTR, BSTR);
251
252 EVENTFUNC uiaRaiseNotificationEvent =
253 (EVENTFUNC)GetProcAddress(uiautomationcore, "UiaRaiseNotificationEvent");
254 if (uiaRaiseNotificationEvent != NULL) {
255 uiaRaiseNotificationEvent(provider.Get(), NotificationKind_Other, processing,
256 message.bstr(), activityId.bstr());
257 }
258 }
259#endif
260 }
261 }
262}
263
264HRESULT STDMETHODCALLTYPE QWindowsUiaMainProvider::QueryInterface(REFIID iid, LPVOID *iface)
265{
266 HRESULT result = QComObject::QueryInterface(iid, iface);
267
268 if (SUCCEEDED(result) && iid == __uuidof(IRawElementProviderFragmentRoot)) {
269 QAccessibleInterface *accessible = accessibleInterface();
270 if (accessible && hwndForAccessible(accessible)) {
271 result = S_OK;
272 } else {
273 Release();
274 result = E_NOINTERFACE;
275 *iface = nullptr;
276 }
277 }
278
279 return result;
280}
281
282HRESULT QWindowsUiaMainProvider::get_ProviderOptions(ProviderOptions *pRetVal)
283{
284 if (!pRetVal)
285 return E_INVALIDARG;
286 // We are STA, (OleInitialize()).
287 *pRetVal = static_cast<ProviderOptions>(ProviderOptions_ServerSideProvider | ProviderOptions_UseComThreading);
288 return S_OK;
289}
290
291// Return providers for specific control patterns
292HRESULT QWindowsUiaMainProvider::GetPatternProvider(PATTERNID idPattern, IUnknown **pRetVal)
293{
294 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << idPattern;
295
296 if (!pRetVal)
297 return E_INVALIDARG;
298 *pRetVal = nullptr;
299
300 QAccessibleInterface *accessible = accessibleInterface();
301 if (!accessible)
302 return UIA_E_ELEMENTNOTAVAILABLE;
303
304 switch (idPattern) {
305 case UIA_WindowPatternId:
306 if (accessible->parent() && (accessible->parent()->role() == QAccessible::Application)) {
307 *pRetVal = makeComObject<QWindowsUiaWindowProvider>(id()).Detach();
308 }
309 break;
310 case UIA_TextPatternId:
311 case UIA_TextPattern2Id:
312 // All text controls.
313 if (accessible->textInterface()) {
314 *pRetVal = makeComObject<QWindowsUiaTextProvider>(id()).Detach();
315 }
316 break;
317 case UIA_ValuePatternId:
318 // All non-static controls support the Value pattern.
319 if (accessible->role() != QAccessible::StaticText)
320 *pRetVal = makeComObject<QWindowsUiaValueProvider>(id()).Detach();
321 break;
322 case UIA_RangeValuePatternId:
323 // Controls providing a numeric value within a range (e.g., sliders, scroll bars, dials).
324 if (accessible->valueInterface()) {
325 *pRetVal = makeComObject<QWindowsUiaRangeValueProvider>(id()).Detach();
326 }
327 break;
328 case UIA_TogglePatternId:
329 // Checkboxes and other checkable controls.
330 if (accessible->state().checkable)
331 *pRetVal = makeComObject<QWindowsUiaToggleProvider>(id()).Detach();
332 break;
333 case UIA_SelectionPatternId:
334 case UIA_SelectionPattern2Id:
335 // Selections via QAccessibleSelectionInterface or lists of items.
336 if (accessible->selectionInterface()
337 || accessible->role() == QAccessible::List
338 || accessible->role() == QAccessible::PageTabList) {
339 *pRetVal = makeComObject<QWindowsUiaSelectionProvider>(id()).Detach();
340 }
341 break;
342 case UIA_SelectionItemPatternId:
343 // Parent supports selection interface or items within a list and radio buttons.
344 if ((accessible->parent() && accessible->parent()->selectionInterface())
345 || (accessible->role() == QAccessible::RadioButton)
346 || (accessible->role() == QAccessible::ListItem)
347 || (accessible->role() == QAccessible::PageTab)) {
348 *pRetVal = makeComObject<QWindowsUiaSelectionItemProvider>(id()).Detach();
349 }
350 break;
351 case UIA_TablePatternId:
352 // Table/tree.
353 if (accessible->tableInterface()
354 && ((accessible->role() == QAccessible::Table) || (accessible->role() == QAccessible::Tree))) {
355 *pRetVal = makeComObject<QWindowsUiaTableProvider>(id()).Detach();
356 }
357 break;
358 case UIA_TableItemPatternId:
359 // Item within a table/tree.
360 if (accessible->tableCellInterface()
361 && ((accessible->role() == QAccessible::Cell) || (accessible->role() == QAccessible::TreeItem))) {
362 *pRetVal = makeComObject<QWindowsUiaTableItemProvider>(id()).Detach();
363 }
364 break;
365 case UIA_GridPatternId:
366 // Table/tree.
367 if (accessible->tableInterface()
368 && ((accessible->role() == QAccessible::Table) || (accessible->role() == QAccessible::Tree))) {
369 *pRetVal = makeComObject<QWindowsUiaGridProvider>(id()).Detach();
370 }
371 break;
372 case UIA_GridItemPatternId:
373 // Item within a table/tree.
374 if (accessible->tableCellInterface()
375 && ((accessible->role() == QAccessible::Cell) || (accessible->role() == QAccessible::TreeItem))) {
376 *pRetVal = makeComObject<QWindowsUiaGridItemProvider>(id()).Detach();
377 }
378 break;
379 case UIA_InvokePatternId:
380 // Things that have an invokable action (e.g., simple buttons).
381 if (accessible->actionInterface()) {
382 *pRetVal = makeComObject<QWindowsUiaInvokeProvider>(id()).Detach();
383 }
384 break;
385 case UIA_ExpandCollapsePatternId:
386 // Menu items with submenus.
387 if ((accessible->role() == QAccessible::MenuItem
388 && accessible->childCount() > 0
389 && accessible->child(0)->role() == QAccessible::PopupMenu)
390 || accessible->role() == QAccessible::ComboBox
391 || accessible->state().expandable) {
392 *pRetVal = makeComObject<QWindowsUiaExpandCollapseProvider>(id()).Detach();
393 }
394 break;
395 default:
396 break;
397 }
398
399 return S_OK;
400}
401
402void QWindowsUiaMainProvider::setLabelledBy(QAccessibleInterface *accessible, VARIANT *pRetVal)
403{
404 Q_ASSERT(accessible);
405
406 typedef std::pair<QAccessibleInterface*, QAccessible::Relation> RelationPair;
407 const QList<RelationPair> relationInterfaces = accessible->relations(QAccessible::Label);
408 if (relationInterfaces.empty())
409 return;
410
411 // UIA_LabeledByPropertyId only supports one relation
412 ComPtr<IRawElementProviderSimple> provider = providerForAccessible(relationInterfaces.first().first);
413 if (!provider)
414 return;
415
416 pRetVal->vt = VT_UNKNOWN;
417 pRetVal->punkVal = provider.Detach();
418}
419
420void QWindowsUiaMainProvider::fillVariantArrayForRelation(QAccessibleInterface* accessible,
421 QAccessible::Relation relation, VARIANT *pRetVal)
422{
423 Q_ASSERT(accessible);
424
425 typedef std::pair<QAccessibleInterface*, QAccessible::Relation> RelationPair;
426 const QList<RelationPair> relationInterfaces = accessible->relations(relation);
427 if (relationInterfaces.empty())
428 return;
429
430 SAFEARRAY *elements = SafeArrayCreateVector(VT_UNKNOWN, 0, relationInterfaces.size());
431 for (LONG i = 0; i < relationInterfaces.size(); ++i) {
432 if (ComPtr<IRawElementProviderSimple> provider =
433 providerForAccessible(relationInterfaces.at(i).first)) {
434 SafeArrayPutElement(elements, &i, provider.Get());
435 }
436 }
437
438 pRetVal->vt = VT_UNKNOWN | VT_ARRAY;
439 pRetVal->parray = elements;
440}
441
442void QWindowsUiaMainProvider::setAriaProperties(QAccessibleInterface *accessible, VARIANT *pRetVal)
443{
444 Q_ASSERT(accessible);
445
446 QAccessibleAttributesInterface *attributesIface = accessible->attributesInterface();
447 if (!attributesIface)
448 return;
449
450 QString ariaString;
451 const QList<QAccessible::Attribute> attrKeys = attributesIface->attributeKeys();
452 for (qsizetype i = 0; i < attrKeys.size(); ++i) {
453 if (i != 0)
454 ariaString += QStringLiteral(";");
455 const QAccessible::Attribute key = attrKeys.at(i);
456 const QVariant value = attributesIface->attributeValue(key);
457 // see "Core Accessibility API Mappings" spec: https://www.w3.org/TR/core-aam-1.2/
458 switch (key) {
459 case QAccessible::Attribute::Custom:
460 {
461 // forward custom attributes as-is
462 Q_ASSERT((value.canConvert<QHash<QString, QString>>()));
463 const QHash<QString, QString> attrMap = value.value<QHash<QString, QString>>();
464 for (auto [name, val] : attrMap.asKeyValueRange()) {
465 if (name != *attrMap.keyBegin())
466 ariaString += QStringLiteral(";");
467 ariaString += name + QStringLiteral("=") + val;
468 }
469 break;
470 }
471 case QAccessible::Attribute::Level:
472 Q_ASSERT(value.canConvert<int>());
473 ariaString += QStringLiteral("level=") + QString::number(value.toInt());
474 break;
475 default:
476 break;
477 }
478 }
479
480 *pRetVal = QComVariant{ ariaString }.release();
481}
482
483void QWindowsUiaMainProvider::setStyle(QAccessibleInterface *accessible, VARIANT *pRetVal)
484{
485 Q_ASSERT(accessible);
486
487 QAccessibleAttributesInterface *attributesIface = accessible->attributesInterface();
488 if (!attributesIface)
489 return;
490
491 // currently, only heading styles are implemented here
492 if (accessible->role() != QAccessible::Role::Heading)
493 return;
494
495 const QVariant levelVariant = attributesIface->attributeValue(QAccessible::Attribute::Level);
496 if (!levelVariant.isValid())
497 return;
498
499 Q_ASSERT(levelVariant.canConvert<int>());
500 // UIA only has styles for heading levels 1-9
501 const int level = levelVariant.toInt();
502 if (level < 1 || level > 9)
503 return;
504
505 const long styleId = styleIdForHeadingLevel(level);
506 *pRetVal = QComVariant{ styleId }.release();
507}
508
509int QWindowsUiaMainProvider::styleIdForHeadingLevel(int headingLevel)
510{
511 // only heading levels 1-9 have a corresponding UIA style ID
512 Q_ASSERT(headingLevel > 0 && headingLevel <= 9);
513
514 static constexpr int styles[] = {
515 StyleId_Heading1,
516 StyleId_Heading2,
517 StyleId_Heading3,
518 StyleId_Heading4,
519 StyleId_Heading5,
520 StyleId_Heading6,
521 StyleId_Heading7,
522 StyleId_Heading8,
523 StyleId_Heading9,
524 };
525
526 return styles[headingLevel - 1];
527}
528
529HRESULT QWindowsUiaMainProvider::GetPropertyValue(PROPERTYID idProp, VARIANT *pRetVal)
530{
531 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << idProp;
532
533 if (!pRetVal)
534 return E_INVALIDARG;
535 clearVariant(pRetVal);
536
537 QAccessibleInterface *accessible = accessibleInterface();
538 if (!accessible)
539 return UIA_E_ELEMENTNOTAVAILABLE;
540
541 bool topLevelWindow = accessible->parent() && (accessible->parent()->role() == QAccessible::Application);
542
543 switch (idProp) {
544 case UIA_ProcessIdPropertyId:
545 // PID
546 *pRetVal = QComVariant{ static_cast<long>(GetCurrentProcessId()) }.release();
547 break;
548 case UIA_AccessKeyPropertyId:
549 // Accelerator key.
550 *pRetVal = QComVariant{ accessible->text(QAccessible::Accelerator) }.release();
551 break;
552 case UIA_AriaRolePropertyId:
553 if (accessible->role() == QAccessible::Heading)
554 *pRetVal = QComVariant{ QStringLiteral("heading") }.release();
555 break;
556 case UIA_AriaPropertiesPropertyId:
557 setAriaProperties(accessible, pRetVal);
558 break;
559 case UIA_AutomationIdPropertyId:
560 // Automation ID, which can be used by tools to select a specific control in the UI.
561 *pRetVal = QComVariant{ QAccessibleBridgeUtils::accessibleId(accessible) }.release();
562 break;
563 case UIA_ClassNamePropertyId:
564 // Class name.
565 if (QObject *o = accessible->object()) {
566 QString className = QLatin1StringView(o->metaObject()->className());
567 *pRetVal = QComVariant{ className }.release();
568 }
569 break;
570 case UIA_CulturePropertyId:
571 {
572 QLocale locale;
573 if (QAccessibleAttributesInterface *attributesIface = accessible->attributesInterface()) {
574 const QVariant localeVariant = attributesIface->attributeValue(QAccessible::Attribute::Locale);
575 if (localeVariant.isValid()) {
576 Q_ASSERT(localeVariant.canConvert<QLocale>());
577 locale = localeVariant.toLocale();
578 }
579 }
580 LCID lcid = LocaleNameToLCID(qUtf16Printable(locale.bcp47Name()), 0);
581 *pRetVal = QComVariant{ long(lcid) }.release();
582 break;
583 }
584 case UIA_DescribedByPropertyId:
585 fillVariantArrayForRelation(accessible, QAccessible::DescriptionFor, pRetVal);
586 break;
587 case UIA_FlowsFromPropertyId:
588 fillVariantArrayForRelation(accessible, QAccessible::FlowsTo, pRetVal);
589 break;
590 case UIA_FlowsToPropertyId:
591 fillVariantArrayForRelation(accessible, QAccessible::FlowsFrom, pRetVal);
592 break;
593 case UIA_LabeledByPropertyId:
594 setLabelledBy(accessible, pRetVal);
595 break;
596 case UIA_FrameworkIdPropertyId:
597 *pRetVal = QComVariant{ QStringLiteral("Qt") }.release();
598 break;
599 case UIA_ControlTypePropertyId:
600 if (topLevelWindow) {
601 // Reports a top-level widget as a window, instead of "custom".
602 *pRetVal = QComVariant{ UIA_WindowControlTypeId }.release();
603 } else {
604 // Control type converted from role.
605 *pRetVal = QComVariant{ roleToControlTypeId(accessible->role()) }.release();
606 }
607 break;
608 case UIA_HelpTextPropertyId:
609 *pRetVal = QComVariant{ accessible->text(QAccessible::Help) }.release();
610 break;
611 case UIA_HasKeyboardFocusPropertyId:
612 // If the top-level window has no focused child, report the top-level
613 // widget (window). If it already has a focused widget, it will be
614 // reported automatically.
615 if (topLevelWindow) {
616 QAccessibleInterface *focusacc = accessible->focusChild();
617 if (!focusacc) {
618 *pRetVal = QComVariant{ accessible->state().active ? true : false }.release();
619 break;
620 }
621 }
622 *pRetVal = QComVariant{ accessible->state().focused ? true : false }.release();
623 break;
624 case UIA_IsKeyboardFocusablePropertyId:
625 if (topLevelWindow) {
626 // Windows should always be focusable
627 *pRetVal = QComVariant{ true }.release();
628 } else {
629 *pRetVal = QComVariant{ accessible->state().focusable ? true : false }.release();
630 }
631 break;
632 case UIA_IsOffscreenPropertyId:
633 *pRetVal = QComVariant{ accessible->state().offscreen ? true : false }.release();
634 break;
635 case UIA_IsContentElementPropertyId:
636 *pRetVal = QComVariant{ true }.release();
637 break;
638 case UIA_IsControlElementPropertyId:
639 *pRetVal = QComVariant{ true }.release();
640 break;
641 case UIA_IsEnabledPropertyId:
642 *pRetVal = QComVariant{ !accessible->state().disabled }.release();
643 break;
644 case UIA_IsPasswordPropertyId:
645 *pRetVal = QComVariant{ accessible->role() == QAccessible::EditableText
646 && accessible->state().passwordEdit }
647 .release();
648 break;
649 case UIA_IsPeripheralPropertyId:
650 // True for peripheral UIs.
651 if (QWindow *window = windowForAccessible(accessible)) {
652 const Qt::WindowType wt = window->type();
653 *pRetVal = QComVariant{ wt == Qt::Popup || wt == Qt::ToolTip || wt == Qt::SplashScreen }
654 .release();
655 }
656 break;
657 case UIA_IsDialogPropertyId:
658 *pRetVal = QComVariant{ accessible->role() == QAccessible::Dialog
659 || accessible->role() == QAccessible::AlertMessage }
660 .release();
661 break;
662 case UIA_FullDescriptionPropertyId:
663 *pRetVal = QComVariant{ accessible->text(QAccessible::Description) }.release();
664 break;
665 case UIA_LocalizedControlTypePropertyId:
666 // see Core Accessibility API Mappings spec:
667 // https://www.w3.org/TR/core-aam-1.2/#role-map-blockquote
668 if (accessible->role() == QAccessible::BlockQuote)
669 *pRetVal = QComVariant{ tr("blockquote") }.release();
670 break;
671 case UIA_NamePropertyId: {
672 QString name = accessible->text(QAccessible::Name);
673 if (name.isEmpty() && topLevelWindow)
674 name = QCoreApplication::applicationName();
675 *pRetVal = QComVariant{ name }.release();
676 break;
677 }
678 case UIA_OrientationPropertyId: {
679 OrientationType orientationType = OrientationType_None;
680 if (QAccessibleAttributesInterface *attributesIface = accessible->attributesInterface()) {
681 const QVariant orientationVariant =
682 attributesIface->attributeValue(QAccessible::Attribute::Orientation);
683 if (orientationVariant.isValid()) {
684 Q_ASSERT(orientationVariant.canConvert<Qt::Orientation>());
685 const Qt::Orientation orientation = orientationVariant.value<Qt::Orientation>();
686 orientationType = orientation == Qt::Horizontal ? OrientationType_Horizontal
687 : OrientationType_Vertical;
688 }
689 }
690 *pRetVal = QComVariant{ long(orientationType) }.release();
691 break;
692 }
693 case UIA_StyleIdAttributeId:
694 setStyle(accessible, pRetVal);
695 break;
696 default:
697 break;
698 }
699 return S_OK;
700}
701
702HRESULT QWindowsUiaMainProvider::get_HostRawElementProvider(IRawElementProviderSimple **pRetVal)
703{
704 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
705
706 if (!pRetVal)
707 return E_INVALIDARG;
708 *pRetVal = nullptr;
709
710 // Returns a host provider only for controls associated with a native window handle. Others should return NULL.
711 if (QAccessibleInterface *accessible = accessibleInterface()) {
712 if (HWND hwnd = hwndForAccessible(accessible)) {
713 return UiaHostProviderFromHwnd(hwnd, pRetVal);
714 }
715 }
716 return S_OK;
717}
718
719// Navigates within the tree of accessible controls.
720HRESULT QWindowsUiaMainProvider::Navigate(NavigateDirection direction, IRawElementProviderFragment **pRetVal)
721{
722 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << direction << " this: " << this;
723
724 if (!pRetVal)
725 return E_INVALIDARG;
726 *pRetVal = nullptr;
727
728 QAccessibleInterface *accessible = accessibleInterface();
729 if (!accessible)
730 return UIA_E_ELEMENTNOTAVAILABLE;
731
732 QAccessibleInterface *targetacc = nullptr;
733
734 if (direction == NavigateDirection_Parent) {
735 if (QAccessibleInterface *parent = accessible->parent()) {
736 // The Application's children are considered top level objects.
737 if (parent->isValid() && parent->role() != QAccessible::Application) {
738 targetacc = parent;
739 }
740 }
741 } else {
742 QAccessibleInterface *parent = nullptr;
743 int index = 0;
744 int incr = 1;
745 switch (direction) {
746 case NavigateDirection_FirstChild:
747 parent = accessible;
748 index = 0;
749 incr = 1;
750 break;
751 case NavigateDirection_LastChild:
752 parent = accessible;
753 index = accessible->childCount() - 1;
754 incr = -1;
755 break;
756 case NavigateDirection_NextSibling:
757 if ((parent = accessible->parent()))
758 index = parent->indexOfChild(accessible) + 1;
759 incr = 1;
760 break;
761 case NavigateDirection_PreviousSibling:
762 if ((parent = accessible->parent()))
763 index = parent->indexOfChild(accessible) - 1;
764 incr = -1;
765 break;
766 default:
767 Q_UNREACHABLE();
768 break;
769 }
770
771 if (parent && parent->isValid()) {
772 for (int count = parent->childCount(); index >= 0 && index < count; index += incr) {
773 if (QAccessibleInterface *child = parent->child(index)) {
774 if (child->isValid() && !child->state().invisible) {
775 targetacc = child;
776 break;
777 }
778 }
779 }
780 }
781 }
782
783 if (targetacc)
784 *pRetVal = providerForAccessible(targetacc).Detach();
785 return S_OK;
786}
787
788// Returns a unique id assigned to the UI element, used as key by the UI Automation framework.
789HRESULT QWindowsUiaMainProvider::GetRuntimeId(SAFEARRAY **pRetVal)
790{
791 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
792
793 if (!pRetVal)
794 return E_INVALIDARG;
795 *pRetVal = nullptr;
796
797 QAccessibleInterface *accessible = accessibleInterface();
798 if (!accessible)
799 return UIA_E_ELEMENTNOTAVAILABLE;
800
801 // The UiaAppendRuntimeId constant is used to make then ID unique
802 // among multiple instances running on the system.
803 int rtId[] = { UiaAppendRuntimeId, int(id()) };
804
805 if ((*pRetVal = SafeArrayCreateVector(VT_I4, 0, 2))) {
806 for (LONG i = 0; i < 2; ++i)
807 SafeArrayPutElement(*pRetVal, &i, &rtId[i]);
808 }
809 return S_OK;
810}
811
812// Returns the bounding rectangle for the accessible control.
813HRESULT QWindowsUiaMainProvider::get_BoundingRectangle(UiaRect *pRetVal)
814{
815 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
816
817 if (!pRetVal)
818 return E_INVALIDARG;
819
820 QAccessibleInterface *accessible = accessibleInterface();
821 if (!accessible)
822 return UIA_E_ELEMENTNOTAVAILABLE;
823
824 QWindow *window = windowForAccessible(accessible);
825 if (!window)
826 return UIA_E_ELEMENTNOTAVAILABLE;
827
828 rectToNativeUiaRect(accessible->rect(), window, pRetVal);
829 return S_OK;
830}
831
832HRESULT QWindowsUiaMainProvider::GetEmbeddedFragmentRoots(SAFEARRAY **pRetVal)
833{
834 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
835
836 if (!pRetVal)
837 return E_INVALIDARG;
838 *pRetVal = nullptr;
839 // No embedded roots.
840 return S_OK;
841}
842
843// Sets focus to the control.
844HRESULT QWindowsUiaMainProvider::SetFocus()
845{
846 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
847
848 QAccessibleInterface *accessible = accessibleInterface();
849 if (!accessible)
850 return UIA_E_ELEMENTNOTAVAILABLE;
851
852 QAccessibleActionInterface *actionInterface = accessible->actionInterface();
853 if (!actionInterface)
854 return UIA_E_ELEMENTNOTAVAILABLE;
855
856 actionInterface->doAction(QAccessibleActionInterface::setFocusAction());
857 return S_OK;
858}
859
860HRESULT QWindowsUiaMainProvider::get_FragmentRoot(IRawElementProviderFragmentRoot **pRetVal)
861{
862 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
863
864 if (!pRetVal)
865 return E_INVALIDARG;
866 *pRetVal = nullptr;
867
868 // Our UI Automation implementation considers the window as the root for
869 // non-native controls/fragments.
870 if (QAccessibleInterface *accessible = accessibleInterface()) {
871 if (QWindow *window = windowForAccessible(accessible)) {
872 if (QAccessibleInterface *rootacc = window->accessibleRoot())
873 *pRetVal = providerForAccessible(rootacc).Detach();
874 }
875 }
876 return S_OK;
877}
878
879// Returns a provider for the UI element present at the specified screen coordinates.
880HRESULT QWindowsUiaMainProvider::ElementProviderFromPoint(double x, double y, IRawElementProviderFragment **pRetVal)
881{
882 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << x << y;
883
884 if (!pRetVal) {
885 return E_INVALIDARG;
886 }
887 *pRetVal = nullptr;
888
889 QAccessibleInterface *accessible = accessibleInterface();
890 if (!accessible)
891 return UIA_E_ELEMENTNOTAVAILABLE;
892
893 QWindow *window = windowForAccessible(accessible);
894 if (!window)
895 return UIA_E_ELEMENTNOTAVAILABLE;
896
897 // Scales coordinates from High DPI screens.
898 UiaPoint uiaPoint = {x, y};
899 QPoint point;
900 nativeUiaPointToPoint(uiaPoint, window, &point);
901
902 QAccessibleInterface *targetacc = accessible->childAt(point.x(), point.y());
903
904 if (targetacc) {
905 QAccessibleInterface *acc = targetacc;
906 // Controls can be embedded within grouping elements. By default returns the innermost control.
907 while (acc) {
908 targetacc = acc;
909 // For accessibility tools it may be better to return the text element instead of its subcomponents.
910 if (targetacc->textInterface()) break;
911 acc = acc->childAt(point.x(), point.y());
912 }
913 *pRetVal = providerForAccessible(targetacc).Detach();
914 }
915 return S_OK;
916}
917
918// Returns the fragment with focus.
919HRESULT QWindowsUiaMainProvider::GetFocus(IRawElementProviderFragment **pRetVal)
920{
921 qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
922
923 if (!pRetVal)
924 return E_INVALIDARG;
925 *pRetVal = nullptr;
926
927 if (QAccessibleInterface *accessible = accessibleInterface()) {
928 if (QAccessibleInterface *focusacc = accessible->focusChild()) {
929 *pRetVal = providerForAccessible(focusacc).Detach();
930 }
931 }
932 return S_OK;
933}
934
935QT_END_NAMESPACE
936
937#endif // QT_CONFIG(accessibility)