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
qwindowsuiautils.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
4#include <QtGui/qtguiglobal.h>
5#if QT_CONFIG(accessibility)
6
7#include "qwindowsuiautils.h"
8#include "qwindowscontext.h"
9#include "qwindowswindow.h"
10
11#include <QtGui/qwindow.h>
12#include <QtGui/private/qhighdpiscaling_p.h>
13#include <qpa/qplatforminputcontextfactory_p.h>
14
15#include <cmath>
16
17QT_BEGIN_NAMESPACE
18
19namespace QWindowsUiAutomation {
20
21// Returns the window containing the element (usually the top window),
22QWindow *windowForAccessible(const QAccessibleInterface *accessible)
23{
24 QWindow *window = accessible->window();
25 if (!window) {
26 const QAccessibleInterface *acc = accessible;
27 const QAccessibleInterface *par = accessible->parent();
28 while (par && par->isValid() && !window) {
29 window = par->window();
30 acc = par;
31 par = par->parent();
32 }
33 if (!window) {
34 // Workaround for WebEngineView not knowing its parent.
35 const auto appWindows = QGuiApplication::topLevelWindows();
36 for (QWindow *w : appWindows) {
37 if (QAccessibleInterface *root = w->accessibleRoot()) {
38 int count = root->childCount();
39 for (int i = 0; i < count; ++i) {
40 if (root->child(i) == acc)
41 return w;
42 }
43 }
44 }
45 }
46 }
47 return window;
48}
49
50// Returns the native window handle associated with the element, if any.
51// Usually it will be NULL, as Qt5 by default uses alien widgets with no native windows.
52HWND hwndForAccessible(const QAccessibleInterface *accessible)
53{
54 if (QWindow *window = accessible->window()) {
55 if (!accessible->parent() || (accessible->parent()->window() != window)) {
56 return QWindowsBaseWindow::handleOf(window);
57 }
58 }
59 return nullptr;
60}
61
62void clearVariant(VARIANT *variant)
63{
64 variant->vt = VT_EMPTY;
65 variant->punkVal = nullptr;
66}
67
68// Scales a rect to native coordinates, according to high dpi settings.
69void rectToNativeUiaRect(const QRect &rect, const QWindow *w, UiaRect *uiaRect)
70{
71 if (w && uiaRect) {
72 QRectF r = QHighDpi::toNativePixels(QRectF(rect), w);
73 uiaRect->left =r.x();
74 uiaRect->top = r.y();
75 uiaRect->width = r.width();
76 uiaRect->height = r.height();
77 }
78}
79
80// Scales a point from native coordinates, according to high dpi settings.
81void nativeUiaPointToPoint(const UiaPoint &uiaPoint, const QWindow *w, QPoint *point)
82{
83 if (w && point)
84 *point = QHighDpi::fromNativePixels(QPoint(uiaPoint.x, uiaPoint.y), w);
85}
86
87// Maps an accessibility role ID to an UI Automation control type ID.
88long roleToControlTypeId(QAccessible::Role role)
89{
90 static const QHash<QAccessible::Role, long> mapping {
91 {QAccessible::TitleBar, UIA_TitleBarControlTypeId},
92 {QAccessible::MenuBar, UIA_MenuBarControlTypeId},
93 {QAccessible::ScrollBar, UIA_ScrollBarControlTypeId},
94 {QAccessible::Grip, UIA_ThumbControlTypeId},
95 {QAccessible::Sound, UIA_CustomControlTypeId},
96 {QAccessible::Cursor, UIA_CustomControlTypeId},
97 {QAccessible::Caret, UIA_CustomControlTypeId},
98 {QAccessible::AlertMessage, UIA_WindowControlTypeId},
99 {QAccessible::Window, UIA_WindowControlTypeId},
100 {QAccessible::Client, UIA_GroupControlTypeId},
101 {QAccessible::PopupMenu, UIA_MenuControlTypeId},
102 {QAccessible::MenuItem, UIA_MenuItemControlTypeId},
103 {QAccessible::ToolTip, UIA_ToolTipControlTypeId},
104 {QAccessible::Application, UIA_CustomControlTypeId},
105 {QAccessible::Document, UIA_DocumentControlTypeId},
106 {QAccessible::Pane, UIA_PaneControlTypeId},
107 {QAccessible::Chart, UIA_CustomControlTypeId},
108 {QAccessible::Dialog, UIA_WindowControlTypeId},
109 {QAccessible::Border, UIA_CustomControlTypeId},
110 {QAccessible::Grouping, UIA_GroupControlTypeId},
111 {QAccessible::Separator, UIA_SeparatorControlTypeId},
112 {QAccessible::ToolBar, UIA_ToolBarControlTypeId},
113 {QAccessible::StatusBar, UIA_StatusBarControlTypeId},
114 {QAccessible::Table, UIA_TableControlTypeId},
115 {QAccessible::ColumnHeader, UIA_HeaderControlTypeId},
116 {QAccessible::RowHeader, UIA_HeaderControlTypeId},
117 {QAccessible::Column, UIA_HeaderItemControlTypeId},
118 {QAccessible::Row, UIA_HeaderItemControlTypeId},
119 {QAccessible::Cell, UIA_DataItemControlTypeId},
120 {QAccessible::Link, UIA_HyperlinkControlTypeId},
121 {QAccessible::HelpBalloon, UIA_ToolTipControlTypeId},
122 {QAccessible::Assistant, UIA_CustomControlTypeId},
123 {QAccessible::List, UIA_ListControlTypeId},
124 {QAccessible::ListItem, UIA_ListItemControlTypeId},
125 {QAccessible::Tree, UIA_TreeControlTypeId},
126 {QAccessible::TreeItem, UIA_TreeItemControlTypeId},
127 {QAccessible::PageTab, UIA_TabItemControlTypeId},
128 {QAccessible::PropertyPage, UIA_CustomControlTypeId},
129 {QAccessible::Indicator, UIA_CustomControlTypeId},
130 {QAccessible::Graphic, UIA_ImageControlTypeId},
131 {QAccessible::StaticText, UIA_TextControlTypeId},
132 {QAccessible::EditableText, UIA_EditControlTypeId},
133 {QAccessible::Button, UIA_ButtonControlTypeId},
134 {QAccessible::CheckBox, UIA_CheckBoxControlTypeId},
135 {QAccessible::RadioButton, UIA_RadioButtonControlTypeId},
136 {QAccessible::ComboBox, UIA_ComboBoxControlTypeId},
137 {QAccessible::ProgressBar, UIA_ProgressBarControlTypeId},
138 {QAccessible::Dial, UIA_CustomControlTypeId},
139 {QAccessible::HotkeyField, UIA_CustomControlTypeId},
140 {QAccessible::Slider, UIA_SliderControlTypeId},
141 {QAccessible::SpinBox, UIA_SpinnerControlTypeId},
142 {QAccessible::Canvas, UIA_CustomControlTypeId},
143 {QAccessible::Animation, UIA_CustomControlTypeId},
144 {QAccessible::Equation, UIA_CustomControlTypeId},
145 {QAccessible::ButtonDropDown, UIA_ButtonControlTypeId},
146 {QAccessible::ButtonMenu, UIA_ButtonControlTypeId},
147 {QAccessible::ButtonDropGrid, UIA_ButtonControlTypeId},
148 {QAccessible::Whitespace, UIA_CustomControlTypeId},
149 {QAccessible::PageTabList, UIA_TabControlTypeId},
150 {QAccessible::Clock, UIA_CustomControlTypeId},
151 {QAccessible::Splitter, UIA_CustomControlTypeId},
152 {QAccessible::Paragraph, UIA_TextControlTypeId},
153 {QAccessible::WebDocument, UIA_DocumentControlTypeId},
154 {QAccessible::Heading, UIA_TextControlTypeId},
155 {QAccessible::BlockQuote, UIA_GroupControlTypeId},
156 };
157
158 long controlType = mapping.value(role, UIA_CustomControlTypeId);
159
160 // The native OSK should be disabled if the Qt OSK is in use,
161 // or if disabled via application attribute.
162 static bool imModuleEmpty = QPlatformInputContextFactory::requested().isEmpty();
163 bool nativeVKDisabled = QCoreApplication::testAttribute(Qt::AA_DisableNativeVirtualKeyboard);
164
165 // If we want to disable the native OSK auto-showing
166 // we have to report text fields as non-editable.
167 if (controlType == UIA_EditControlTypeId && (!imModuleEmpty || nativeVKDisabled))
168 controlType = UIA_TextControlTypeId;
169
170 return controlType;
171}
172
173// True if a character can be a separator for a text unit.
174bool isTextUnitSeparator(TextUnit unit, const QChar &ch)
175{
176 return (((unit == TextUnit_Word) || (unit == TextUnit_Format)) && ch.isSpace())
177 || ((unit == TextUnit_Line) && (ch.toLatin1() == '\n'));
178}
179
180} // namespace QWindowsUiAutomation
181
182
183QT_END_NAMESPACE
184
185#endif // QT_CONFIG(accessibility)