1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtWidgets module 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 #ifndef QAPPLICATION_P_H
41 #define QAPPLICATION_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists for the convenience
48 // of qapplication_*.cpp, qwidget*.cpp, qcolor_x11.cpp, qfiledialog.cpp
49 // and many other.  This header file may change from version to version
50 // without notice, or even be removed.
51 //
52 // We mean it.
53 //
54 
55 #include <QtWidgets/private/qtwidgetsglobal_p.h>
56 #include "QtWidgets/qapplication.h"
57 #include "QtGui/qevent.h"
58 #include "QtGui/qfont.h"
59 #include "QtGui/qcursor.h"
60 #include "QtGui/qregion.h"
61 #include "QtGui/qwindow.h"
62 #include "qwidget.h"
63 #include <qpa/qplatformnativeinterface.h>
64 #include "QtCore/qmutex.h"
65 #include "QtCore/qtranslator.h"
66 #include "QtCore/qbasictimer.h"
67 #include "QtCore/qhash.h"
68 #include "QtCore/qpointer.h"
69 #include "private/qcoreapplication_p.h"
70 #include "QtCore/qpoint.h"
71 #include <QTime>
72 #include <qpa/qwindowsysteminterface.h>
73 #include <qpa/qwindowsysteminterface_p.h>
74 #include <qpa/qplatformintegration.h>
75 #include "private/qguiapplication_p.h"
76 
77 QT_BEGIN_NAMESPACE
78 
79 class QClipboard;
80 class QGraphicsScene;
81 class QObject;
82 class QWidget;
83 class QSocketNotifier;
84 class QTouchDevice;
85 #ifndef QT_NO_GESTURES
86 class QGestureManager;
87 #endif
88 
89 extern Q_GUI_EXPORT bool qt_is_gui_used;
90 #ifndef QT_NO_CLIPBOARD
91 extern QClipboard *qt_clipboard;
92 #endif
93 
94 typedef QHash<QByteArray, QFont> FontHash;
95 Q_WIDGETS_EXPORT FontHash *qt_app_fonts_hash();
96 
97 #define QApplicationPrivateBase QGuiApplicationPrivate
98 
99 class Q_WIDGETS_EXPORT QApplicationPrivate : public QApplicationPrivateBase
100 {
101     Q_DECLARE_PUBLIC(QApplication)
102 public:
103     QApplicationPrivate(int &argc, char **argv, int flags);
104     ~QApplicationPrivate();
105 
106     virtual void notifyLayoutDirectionChange() override;
107     virtual void notifyActiveWindowChange(QWindow *) override;
108 
109     virtual bool shouldQuit() override;
110     bool tryCloseAllWindows() override;
111 
112     static bool autoSipEnabled;
113     static QString desktopStyleKey();
114 
115     void createEventDispatcher() override;
116     static void dispatchEnterLeave(QWidget *enter, QWidget *leave, const QPointF &globalPosF);
117 
118     void notifyWindowIconChanged() override;
119 
120     //modality
121     bool isWindowBlocked(QWindow *window, QWindow **blockingWindow = nullptr) const override;
122     static bool isBlockedByModal(QWidget *widget);
123     static bool modalState();
124     static bool tryModalHelper(QWidget *widget, QWidget **rettop = nullptr);
125 
126 #ifdef QT_KEYPAD_NAVIGATION
keypadNavigationEnabled()127     static bool keypadNavigationEnabled()
128     {
129         return navigationMode == Qt::NavigationModeKeypadTabOrder ||
130                 navigationMode == Qt::NavigationModeKeypadDirectional;
131     }
132 #endif
133 
134     bool notify_helper(QObject *receiver, QEvent * e);
135 
136     void init();
137     void initialize();
138     void process_cmdline();
139 
140     static bool inPopupMode();
popupActive()141     bool popupActive() override { return inPopupMode(); }
142     void closePopup(QWidget *popup);
143     void openPopup(QWidget *popup);
144     static void setFocusWidget(QWidget *focus, Qt::FocusReason reason);
145     static QWidget *focusNextPrevChild_helper(QWidget *toplevel, bool next,
146                                               bool *wrappingOccurred = nullptr);
147 
148 #if QT_CONFIG(graphicsview)
149     // Maintain a list of all scenes to ensure font and palette propagation to
150     // all scenes.
151     QList<QGraphicsScene *> scene_list;
152 #endif
153 
154     QBasicTimer toolTipWakeUp, toolTipFallAsleep;
155     QPoint toolTipPos, toolTipGlobalPos, hoverGlobalPos;
156     QPointer<QWidget> toolTipWidget;
157 
158     static QSize app_strut;
159     static QWidgetList *popupWidgets;
160     static QStyle *app_style;
161 
162 protected:
163     void notifyThemeChanged() override;
164 
165     QPalette basePalette() const override;
166     void handlePaletteChanged(const char *className = nullptr) override;
167 
168 #if QT_CONFIG(draganddrop)
169     void notifyDragStarted(const QDrag *) override;
170 #endif // QT_CONFIG(draganddrop)
171 
172 public:
173     static QFont *sys_font;
174     static QFont *set_font;
175     static QWidget *main_widget;
176     static QWidget *focus_widget;
177     static QWidget *hidden_focus_widget;
178     static QWidget *active_window;
179 #if QT_CONFIG(wheelevent)
180     static int  wheel_scroll_lines;
181     static QPointer<QWidget> wheel_widget;
182 #endif
183 
184     static int enabledAnimations; // Combination of QPlatformTheme::UiEffect
185     static bool widgetCount; // Coupled with -widgetcount switch
186 
187     static void initializeWidgetPalettesFromTheme();
188     static void initializeWidgetFontHash();
189     static void setSystemFont(const QFont &font);
190 
191     using PaletteHash = QHash<QByteArray, QPalette>;
192     static PaletteHash widgetPalettes;
193 
instance()194     static QApplicationPrivate *instance() { return self; }
195 
196 #ifdef QT_KEYPAD_NAVIGATION
197     static QWidget *oldEditFocus;
198     static Qt::NavigationMode navigationMode;
199 #endif
200 
201 #ifndef QT_NO_STYLE_STYLESHEET
202     static QString styleSheet;
203 #endif
204     static QPointer<QWidget> leaveAfterRelease;
205     static QWidget *pickMouseReceiver(QWidget *candidate, const QPoint &windowPos, QPoint *pos,
206                                       QEvent::Type type, Qt::MouseButtons buttons,
207                                       QWidget *buttonDown, QWidget *alienWidget);
208     static bool sendMouseEvent(QWidget *receiver, QMouseEvent *event, QWidget *alienWidget,
209                                QWidget *native, QWidget **buttonDown, QPointer<QWidget> &lastMouseReceiver,
210                                bool spontaneous = true, bool onlyDispatchEnterLeave = false);
211     void sendSyntheticEnterLeave(QWidget *widget);
212 
windowForWidget(const QWidget * widget)213     static QWindow *windowForWidget(const QWidget *widget)
214     {
215         if (QWindow *window = widget->windowHandle())
216             return window;
217         if (const QWidget *nativeParent = widget->nativeParentWidget())
218             return nativeParent->windowHandle();
219         return nullptr;
220     }
221 
222 #ifdef Q_OS_WIN
getHWNDForWidget(const QWidget * widget)223     static HWND getHWNDForWidget(const QWidget *widget)
224     {
225         if (QWindow *window = windowForWidget(widget))
226             if (window->handle() && QGuiApplication::platformNativeInterface())
227                 return static_cast<HWND> (QGuiApplication::platformNativeInterface()->
228                                           nativeResourceForWindow(QByteArrayLiteral("handle"), window));
229         return 0;
230     }
231 #endif
232 
233 #ifndef QT_NO_GESTURES
234     QGestureManager *gestureManager;
235     QWidget *gestureWidget;
236 #endif
237 
238     static bool updateTouchPointsForWidget(QWidget *widget, QTouchEvent *touchEvent);
239     void initializeMultitouch();
240     void initializeMultitouch_sys();
241     void cleanupMultitouch();
242     void cleanupMultitouch_sys();
243     QWidget *findClosestTouchPointTarget(QTouchDevice *device, const QTouchEvent::TouchPoint &touchPoint);
244     void appendTouchPoint(const QTouchEvent::TouchPoint &touchPoint);
245     void removeTouchPoint(int touchPointId);
246     void activateImplicitTouchGrab(QWidget *widget, QTouchEvent *touchBeginEvent);
247     static bool translateRawTouchEvent(QWidget *widget,
248                                        QTouchDevice *device,
249                                        const QList<QTouchEvent::TouchPoint> &touchPoints,
250                                        ulong timestamp);
251     static void translateTouchCancel(QTouchDevice *device, ulong timestamp);
252 
253     QPixmap applyQIconStyleHelper(QIcon::Mode mode, const QPixmap& base) const override;
254 private:
255     static QApplicationPrivate *self;
256     static bool tryCloseAllWidgetWindows(QWindowList *processedWindows);
257 
258     static void giveFocusAccordingToFocusPolicy(QWidget *w, QEvent *event, QPoint localPos);
259     static bool shouldSetFocus(QWidget *w, Qt::FocusPolicy policy);
260 
261 
262     static bool isAlien(QWidget *);
263 };
264 
265 extern void qt_qpa_set_cursor(QWidget * w, bool force);
266 
267 QT_END_NAMESPACE
268 
269 #endif // QAPPLICATION_P_H
270