1// Copyright (C) 2016 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#ifndef QGUIAPPLICATION_P_H
5#define QGUIAPPLICATION_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtGui/private/qtguiglobal_p.h>
19#include <QtGui/qguiapplication.h>
20#include <QtGui/qicon.h>
21
22#include <QtCore/QHash>
23#include <QtCore/QPointF>
24#include <QtCore/private/qcoreapplication_p.h>
25
26#include <QtCore/qnativeinterface.h>
27#include <QtCore/private/qnativeinterface_p.h>
28#include <QtCore/private/qnumeric_p.h>
29#include <QtCore/private/qthread_p.h>
30
31#include <qpa/qwindowsysteminterface.h>
32#include <qpa/qwindowsysteminterface_p.h>
33#if QT_CONFIG(shortcut)
34# include "private/qshortcutmap_p.h"
35#endif
36
37#include <QtCore/qpointer.h>
38
39#include <memory>
40
41QT_BEGIN_NAMESPACE
42
43Q_DECLARE_LOGGING_CATEGORY(lcPopup)
44Q_DECLARE_LOGGING_CATEGORY(lcVirtualKeyboard)
45
46class QColorTrcLut;
47class QPlatformIntegration;
48class QPlatformTheme;
49class QPlatformDragQtResponse;
50#if QT_CONFIG(draganddrop)
51class QDrag;
52#endif // QT_CONFIG(draganddrop)
53class QInputDeviceManager;
54#ifndef QT_NO_ACTION
55class QActionPrivate;
56#endif
57#if QT_CONFIG(shortcut)
58class QShortcutPrivate;
59#endif
60
61class Q_GUI_EXPORT QGuiApplicationPrivate : public QCoreApplicationPrivate
62{
63 Q_DECLARE_PUBLIC(QGuiApplication)
64public:
65 QGuiApplicationPrivate(int &argc, char **argv);
66 ~QGuiApplicationPrivate();
67
68 void init();
69
70 void createPlatformIntegration();
71 void createEventDispatcher() override;
72 void eventDispatcherReady() override;
73
74 virtual void notifyLayoutDirectionChange();
75 virtual void notifyActiveWindowChange(QWindow *previous);
76
77#if QT_CONFIG(commandlineparser)
78 void addQtOptions(QList<QCommandLineOption> *options) override;
79#endif
80 bool canQuitAutomatically() override;
81 void quit() override;
82
83 void maybeLastWindowClosed();
84 bool lastWindowClosed() const;
85 static bool quitOnLastWindowClosed;
86
87 static void captureGlobalModifierState(QEvent *e);
88 static Qt::KeyboardModifiers modifier_buttons;
89 static Qt::MouseButtons mouse_buttons;
90
91 static QPlatformIntegration *platform_integration;
92
93 static QPlatformIntegration *platformIntegration()
94 { return platform_integration; }
95
96 static QPlatformTheme *platform_theme;
97
98 static QPlatformTheme *platformTheme()
99 { return platform_theme; }
100
101 static QAbstractEventDispatcher *qt_qpa_core_dispatcher()
102 {
103 if (QCoreApplication::instance())
104 return QCoreApplication::instance()->d_func()->threadData.loadRelaxed()->eventDispatcher.loadRelaxed();
105 else
106 return nullptr;
107 }
108
109 static void processMouseEvent(QWindowSystemInterfacePrivate::MouseEvent *e);
110 static void processKeyEvent(QWindowSystemInterfacePrivate::KeyEvent *e);
111 static void processWheelEvent(QWindowSystemInterfacePrivate::WheelEvent *e);
112 static void processTouchEvent(QWindowSystemInterfacePrivate::TouchEvent *e);
113
114 static void processCloseEvent(QWindowSystemInterfacePrivate::CloseEvent *e);
115
116 static void processGeometryChangeEvent(QWindowSystemInterfacePrivate::GeometryChangeEvent *e);
117
118 static void processEnterEvent(QWindowSystemInterfacePrivate::EnterEvent *e);
119 static void processLeaveEvent(QWindowSystemInterfacePrivate::LeaveEvent *e);
120
121 static void processFocusWindowEvent(QWindowSystemInterfacePrivate::FocusWindowEvent *e);
122
123 static void processWindowStateChangedEvent(QWindowSystemInterfacePrivate::WindowStateChangedEvent *e);
124 static void processWindowScreenChangedEvent(QWindowSystemInterfacePrivate::WindowScreenChangedEvent *e);
125 static void processWindowDevicePixelRatioChangedEvent(QWindowSystemInterfacePrivate::WindowDevicePixelRatioChangedEvent *e);
126
127 static void processSafeAreaMarginsChangedEvent(QWindowSystemInterfacePrivate::SafeAreaMarginsChangedEvent *e);
128
129 static void processWindowSystemEvent(QWindowSystemInterfacePrivate::WindowSystemEvent *e);
130
131 static void processApplicationTermination(QWindowSystemInterfacePrivate::WindowSystemEvent *e);
132
133 static void updateFilteredScreenOrientation(QScreen *screen);
134 static void processScreenOrientationChange(QWindowSystemInterfacePrivate::ScreenOrientationEvent *e);
135 static void processScreenGeometryChange(QWindowSystemInterfacePrivate::ScreenGeometryEvent *e);
136 static void processScreenLogicalDotsPerInchChange(QWindowSystemInterfacePrivate::ScreenLogicalDotsPerInchEvent *e);
137 static void processScreenRefreshRateChange(QWindowSystemInterfacePrivate::ScreenRefreshRateEvent *e);
138 static void processThemeChanged(QWindowSystemInterfacePrivate::ThemeChangeEvent *tce);
139
140 static void processExposeEvent(QWindowSystemInterfacePrivate::ExposeEvent *e);
141 static void processPaintEvent(QWindowSystemInterfacePrivate::PaintEvent *e);
142
143 static void processFileOpenEvent(QWindowSystemInterfacePrivate::FileOpenEvent *e);
144
145 static void processTabletEvent(QWindowSystemInterfacePrivate::TabletEvent *e);
146 static void processTabletEnterProximityEvent(QWindowSystemInterfacePrivate::TabletEnterProximityEvent *e);
147 static void processTabletLeaveProximityEvent(QWindowSystemInterfacePrivate::TabletLeaveProximityEvent *e);
148
149#ifndef QT_NO_GESTURES
150 static void processGestureEvent(QWindowSystemInterfacePrivate::GestureEvent *e);
151#endif
152
153 static void processPlatformPanelEvent(QWindowSystemInterfacePrivate::PlatformPanelEvent *e);
154#ifndef QT_NO_CONTEXTMENU
155 static void processContextMenuEvent(QWindowSystemInterfacePrivate::ContextMenuEvent *e);
156#endif
157
158#if QT_CONFIG(draganddrop)
159 static QPlatformDragQtResponse processDrag(QWindow *w, const QMimeData *dropData,
160 const QPoint &p, Qt::DropActions supportedActions,
161 Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers);
162 static QPlatformDropQtResponse processDrop(QWindow *w, const QMimeData *dropData,
163 const QPoint &p, Qt::DropActions supportedActions,
164 Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers);
165#endif
166
167 static bool processNativeEvent(QWindow *window, const QByteArray &eventType, void *message, qintptr *result);
168
169 static bool sendQWindowEventToQPlatformWindow(QWindow *window, QEvent *event);
170
171 static bool maybeForwardEventToVirtualKeyboard(QEvent *e);
172 static bool isUsingVirtualKeyboard();
173
174 static inline Qt::Alignment visualAlignment(Qt::LayoutDirection direction, Qt::Alignment alignment)
175 {
176 if (!(alignment & Qt::AlignHorizontal_Mask))
177 alignment |= Qt::AlignLeft;
178 if (!(alignment & Qt::AlignAbsolute) && (alignment & (Qt::AlignLeft | Qt::AlignRight))) {
179 if (direction == Qt::RightToLeft)
180 alignment ^= (Qt::AlignLeft | Qt::AlignRight);
181 alignment |= Qt::AlignAbsolute;
182 }
183 return alignment;
184 }
185
186 QPixmap getPixmapCursor(Qt::CursorShape cshape);
187
188 void _q_updateFocusObject(QObject *object);
189
190 static QGuiApplicationPrivate *instance() { return self; }
191
192 static QIcon *app_icon;
193 static QString *platform_name;
194 static QString *displayName;
195 static QString *desktopFileName;
196
197 QWindowList modalWindowList;
198 static void showModalWindow(QWindow *window);
199 static void hideModalWindow(QWindow *window);
200 static void updateBlockedStatus(QWindow *window);
201
202 virtual Qt::WindowModality defaultModality() const;
203 virtual bool windowNeverBlocked(QWindow *window) const;
204 bool isWindowBlocked(QWindow *window, QWindow **blockingWindow = nullptr) const;
205 static qsizetype popupCount() { return QGuiApplicationPrivate::popup_list.size(); }
206 static QWindow *activePopupWindow();
207 static void activatePopup(QWindow *popup);
208 static bool closePopup(QWindow *popup);
209 static bool closeAllPopups();
210
211 static Qt::MouseButton mousePressButton;
212 static struct QLastCursorPosition {
213 constexpr inline QLastCursorPosition() noexcept : thePoint(qt_inf(), qt_inf()) {}
214 constexpr inline Q_IMPLICIT QLastCursorPosition(QPointF p) noexcept : thePoint(p) {}
215 constexpr inline Q_IMPLICIT operator QPointF() const noexcept { return thePoint; }
216 constexpr inline qreal x() const noexcept{ return thePoint.x(); }
217 constexpr inline qreal y() const noexcept{ return thePoint.y(); }
218 Q_GUI_EXPORT QPoint toPoint() const noexcept;
219
220 constexpr void reset() noexcept { *this = QLastCursorPosition{}; }
221
222 // QGuiApplicationPrivate::lastCursorPosition is used for mouse-move detection
223 // but even QPointF's qFuzzCompare on doubles is too precise, and causes move-noise
224 // e.g. on macOS (see QTBUG-111170). So we specialize the equality operators here
225 // to use single-point precision.
226 friend constexpr bool operator==(const QLastCursorPosition &p1, const QPointF &p2) noexcept
227 {
228 return qFuzzyCompare(p1: float(p1.x()), p2: float(p2.x()))
229 && qFuzzyCompare(p1: float(p1.y()), p2: float(p2.y()));
230 }
231 friend constexpr bool operator!=(const QLastCursorPosition &p1, const QPointF &p2) noexcept
232 {
233 return !(p1 == p2);
234 }
235 friend constexpr bool operator==(const QPointF &p1, const QLastCursorPosition &p2) noexcept
236 {
237 return p2 == p1;
238 }
239 friend constexpr bool operator!=(const QPointF &p1, const QLastCursorPosition &p2) noexcept
240 {
241 return !(p2 == p1);
242 }
243
244 private:
245 QPointF thePoint;
246 } lastCursorPosition;
247 static QWindow *currentMouseWindow;
248 static QWindow *currentMousePressWindow;
249 static Qt::ApplicationState applicationState;
250 static Qt::HighDpiScaleFactorRoundingPolicy highDpiScaleFactorRoundingPolicy;
251 static QPointer<QWindow> currentDragWindow;
252
253 // TODO remove this: QPointingDevice can store what we need directly
254 struct TabletPointData {
255 TabletPointData(qint64 devId = 0) : deviceId(devId), state(Qt::NoButton), target(nullptr) {}
256 qint64 deviceId;
257 Qt::MouseButtons state;
258 QWindow *target;
259 };
260 static QList<TabletPointData> tabletDevicePoints;
261 static TabletPointData &tabletDevicePoint(qint64 deviceId);
262
263#ifndef QT_NO_CLIPBOARD
264 static QClipboard *qt_clipboard;
265#endif
266
267 static QPalette *app_pal;
268
269 static QWindowList window_list;
270 static QWindowList popup_list;
271 static const QWindow *active_popup_on_press;
272 static QWindow *focus_window;
273
274#ifndef QT_NO_CURSOR
275 QList<QCursor> cursor_list;
276#endif
277 static QList<QScreen *> screen_list;
278
279 static QFont *app_font;
280
281 static QString styleOverride;
282 static QStyleHints *styleHints;
283 static bool obey_desktop_settings;
284 static bool popup_closed_on_press;
285 QInputMethod *inputMethod;
286
287 QString firstWindowTitle;
288 QIcon forcedWindowIcon;
289
290 static QList<QObject *> generic_plugin_list;
291#if QT_CONFIG(shortcut)
292 QShortcutMap shortcutMap;
293#endif
294
295#ifndef QT_NO_SESSIONMANAGER
296 QSessionManager *session_manager;
297 bool is_session_restored;
298 bool is_saving_session;
299 void commitData();
300 void saveState();
301#endif
302
303 QEvent::Type lastTouchType;
304 struct SynthesizedMouseData {
305 SynthesizedMouseData(const QPointF &p, const QPointF &sp, QWindow *w)
306 : pos(p), screenPos(sp), window(w) { }
307 QPointF pos;
308 QPointF screenPos;
309 QPointer<QWindow> window;
310 };
311 QHash<QWindow *, SynthesizedMouseData> synthesizedMousePoints;
312
313 static QInputDeviceManager *inputDeviceManager();
314
315 const QColorTrcLut *colorProfileForA8Text();
316 const QColorTrcLut *colorProfileForA32Text();
317
318 // hook reimplemented in QApplication to apply the QStyle function on the QIcon
319 virtual QPixmap applyQIconStyleHelper(QIcon::Mode, const QPixmap &basePixmap) const { return basePixmap; }
320
321 virtual void notifyWindowIconChanged();
322
323 static void applyWindowGeometrySpecificationTo(QWindow *window);
324
325 static void setApplicationState(Qt::ApplicationState state, bool forcePropagate = false);
326
327 static void resetCachedDevicePixelRatio();
328
329#ifndef QT_NO_ACTION
330 virtual QActionPrivate *createActionPrivate() const;
331#endif
332#ifndef QT_NO_SHORTCUT
333 virtual QShortcutPrivate *createShortcutPrivate() const;
334#endif
335
336 static void updatePalette();
337
338 static QEvent::Type contextMenuEventType();
339
340protected:
341 virtual void handleThemeChanged();
342
343 static bool setPalette(const QPalette &palette);
344 virtual QPalette basePalette() const;
345 virtual void handlePaletteChanged(const char *className = nullptr);
346
347#if QT_CONFIG(draganddrop)
348 virtual void notifyDragStarted(const QDrag *);
349#endif // QT_CONFIG(draganddrop)
350
351private:
352 static void clearPalette();
353
354 friend class QDragManager;
355 friend class QWindowPrivate;
356
357 static QGuiApplicationPrivate *self;
358 static int m_fakeMouseSourcePointId;
359#ifdef Q_OS_WIN
360 std::shared_ptr<QColorTrcLut> m_a8ColorProfile;
361#endif
362 std::shared_ptr<QColorTrcLut> m_a32ColorProfile;
363
364 bool ownGlobalShareContext;
365
366 static QInputDeviceManager *m_inputDeviceManager;
367
368 // Cache the maximum device pixel ratio, to iterate through the screen list
369 // only the first time it's required, or when devices are added or removed.
370 static qreal m_maxDevicePixelRatio;
371};
372
373// ----------------- QNativeInterface -----------------
374
375class QWindowsMimeConverter;
376
377namespace QNativeInterface::Private {
378
379#if defined(Q_OS_WIN) || defined(Q_QDOC)
380
381
382struct Q_GUI_EXPORT QWindowsApplication
383{
384 QT_DECLARE_NATIVE_INTERFACE(QWindowsApplication, 1, QGuiApplication)
385
386 enum WindowActivationBehavior {
387 DefaultActivateWindow,
388 AlwaysActivateWindow
389 };
390
391 enum TouchWindowTouchType {
392 NormalTouch = 0x00000000,
393 FineTouch = 0x00000001,
394 WantPalmTouch = 0x00000002
395 };
396
397 Q_DECLARE_FLAGS(TouchWindowTouchTypes, TouchWindowTouchType)
398
399 enum DarkModeHandlingFlag {
400 DarkModeWindowFrames = 0x1,
401 DarkModeStyle = 0x2
402 };
403
404 Q_DECLARE_FLAGS(DarkModeHandling, DarkModeHandlingFlag)
405
406 virtual void setTouchWindowTouchType(TouchWindowTouchTypes type) = 0;
407 virtual TouchWindowTouchTypes touchWindowTouchType() const = 0;
408
409 virtual WindowActivationBehavior windowActivationBehavior() const = 0;
410 virtual void setWindowActivationBehavior(WindowActivationBehavior behavior) = 0;
411
412 virtual void setHasBorderInFullScreenDefault(bool border) = 0;
413
414 virtual bool isTabletMode() const = 0;
415
416 virtual bool isWinTabEnabled() const = 0;
417 virtual bool setWinTabEnabled(bool enabled) = 0;
418
419 virtual DarkModeHandling darkModeHandling() const = 0;
420 virtual void setDarkModeHandling(DarkModeHandling handling) = 0;
421
422 virtual void registerMime(QWindowsMimeConverter *mime) = 0;
423 virtual void unregisterMime(QWindowsMimeConverter *mime) = 0;
424
425 virtual int registerMimeType(const QString &mime) = 0;
426
427 virtual HWND createMessageWindow(const QString &classNameTemplate,
428 const QString &windowName,
429 QFunctionPointer eventProc = nullptr) const = 0;
430
431 virtual bool asyncExpose() const = 0; // internal, used by Active Qt
432 virtual void setAsyncExpose(bool value) = 0;
433
434 virtual QVariant gpu() const = 0; // internal, used by qtdiag
435 virtual QVariant gpuList() const = 0;
436
437 virtual void populateLightSystemPalette(QPalette &pal) const = 0;
438};
439#endif // Q_OS_WIN
440
441} // QNativeInterface::Private
442
443#if defined(Q_OS_WIN)
444Q_DECLARE_OPERATORS_FOR_FLAGS(QNativeInterface::Private::QWindowsApplication::TouchWindowTouchTypes)
445Q_DECLARE_OPERATORS_FOR_FLAGS(QNativeInterface::Private::QWindowsApplication::DarkModeHandling)
446#endif
447
448QT_END_NAMESPACE
449
450#endif // QGUIAPPLICATION_P_H
451

source code of qtbase/src/gui/kernel/qguiapplication_p.h