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 QWIDGET_P_H
41#define QWIDGET_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 and qfiledialog.cpp. This header
49// file may change from version to version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtWidgets/private/qtwidgetsglobal_p.h>
55#include "QtWidgets/qwidget.h"
56#include "private/qobject_p.h"
57#include "QtCore/qrect.h"
58#include "QtCore/qlocale.h"
59#include "QtCore/qset.h"
60#include "QtGui/qregion.h"
61#include "QtGui/qinputmethod.h"
62#include "QtGui/qopengl.h"
63#include "QtGui/qsurfaceformat.h"
64#include "QtWidgets/qsizepolicy.h"
65#include "QtWidgets/qstyle.h"
66#include "QtWidgets/qapplication.h"
67#if QT_CONFIG(graphicseffect)
68#include <private/qgraphicseffect_p.h>
69#endif
70#if QT_CONFIG(graphicsview)
71#include "QtWidgets/qgraphicsproxywidget.h"
72#include "QtWidgets/qgraphicsscene.h"
73#include "QtWidgets/qgraphicsview.h"
74#endif
75#include <private/qgesture_p.h>
76#include <qpa/qplatformbackingstore.h>
77
78QT_BEGIN_NAMESPACE
79
80// Extra QWidget data
81// - to minimize memory usage for members that are seldom used.
82// - top-level widgets have extra extra data to reduce cost further
83class QWidgetWindow;
84class QPaintEngine;
85class QPixmap;
86class QWidgetBackingStore;
87class QGraphicsProxyWidget;
88class QWidgetItemV2;
89class QOpenGLContext;
90
91class QStyle;
92
93class QUnifiedToolbarSurface;
94
95// implemented in qshortcut.cpp
96bool qWidgetShortcutContextMatcher(QObject *object, Qt::ShortcutContext context);
97
98class QUpdateLaterEvent : public QEvent
99{
100public:
101 explicit QUpdateLaterEvent(const QRegion& paintRegion)
102 : QEvent(UpdateLater), m_region(paintRegion)
103 {
104 }
105
106 ~QUpdateLaterEvent()
107 {
108 }
109
110 inline const QRegion &region() const { return m_region; }
111
112protected:
113 QRegion m_region;
114};
115
116
117
118class Q_AUTOTEST_EXPORT QWidgetBackingStoreTracker
119{
120
121public:
122 QWidgetBackingStoreTracker();
123 ~QWidgetBackingStoreTracker();
124
125 void create(QWidget *tlw);
126 void destroy();
127
128 void registerWidget(QWidget *w);
129 void unregisterWidget(QWidget *w);
130 void unregisterWidgetSubtree(QWidget *w);
131
132 inline QWidgetBackingStore* data()
133 {
134 return m_ptr;
135 }
136
137 inline QWidgetBackingStore* operator->()
138 {
139 return m_ptr;
140 }
141
142 inline QWidgetBackingStore& operator*()
143 {
144 return *m_ptr;
145 }
146
147 inline operator bool() const
148 {
149 return (nullptr != m_ptr);
150 }
151
152private:
153 Q_DISABLE_COPY_MOVE(QWidgetBackingStoreTracker)
154
155private:
156 QWidgetBackingStore* m_ptr;
157 QSet<QWidget *> m_widgets;
158};
159
160struct QTLWExtra {
161 // *************************** Cross-platform variables *****************************
162
163 // Regular pointers (keep them together to avoid gaps on 64 bits architectures).
164 QIcon *icon; // widget icon
165 QWidgetBackingStoreTracker backingStoreTracker;
166 QBackingStore *backingStore;
167 QPainter *sharedPainter;
168 QWidgetWindow *window;
169 QOpenGLContext *shareContext;
170
171 // Implicit pointers (shared_null).
172 QString caption; // widget caption
173 QString iconText; // widget icon text
174 QString role; // widget role
175 QString filePath; // widget file path
176
177 // Other variables.
178 short incw, inch; // size increments
179 short basew, baseh; // base sizes
180 // frame strut, don't use these directly, use QWidgetPrivate::frameStrut() instead.
181 QRect frameStrut;
182 QRect normalGeometry; // used by showMin/maximized/FullScreen
183 Qt::WindowFlags savedFlags; // Save widget flags while showing fullscreen
184 int initialScreenIndex; // Screen number when passing a QDesktop[Screen]Widget as parent.
185
186 QVector<QPlatformTextureList *> widgetTextures;
187
188 // *************************** Cross-platform bit fields ****************************
189 uint opacity : 8;
190 uint posIncludesFrame : 1;
191 uint sizeAdjusted : 1;
192 uint inTopLevelResize : 1;
193 uint embedded : 1;
194
195 // *************************** Platform specific values (bit fields first) **********
196#if 0 /* Used to be included in Qt4 for Q_WS_X11 */ // <----------------------------------------------------------- X11
197 uint spont_unmapped: 1; // window was spontaneously unmapped
198 uint dnd : 1; // DND properties installed
199 uint validWMState : 1; // is WM_STATE valid?
200 uint waitingForMapNotify : 1; // show() has been called, haven't got the MapNotify yet
201 WId parentWinId; // parent window Id (valid after reparenting)
202 WId userTimeWindow; // window id that contains user-time timestamp when WM supports a _NET_WM_USER_TIME_WINDOW atom
203 QPoint fullScreenOffset;
204#ifndef QT_NO_XSYNC
205 WId syncUpdateCounter;
206 ulong syncRequestTimestamp;
207 qint32 newCounterValueHi;
208 quint32 newCounterValueLo;
209#endif
210#elif 0 /* Used to be included in Qt4 for Q_WS_WIN */ // <--------------------------------------------------------- WIN
211 uint hotkeyRegistered: 1; // Hot key from the STARTUPINFO has been registered.
212 HICON winIconBig; // internal big Windows icon
213 HICON winIconSmall; // internal small Windows icon
214#elif 0 /* Used to be included in Qt4 for Q_WS_MAC */ // <--------------------------------------------------------- MAC
215 uint resizer : 4;
216 uint isSetGeometry : 1;
217 uint isMove : 1;
218 quint32 wattr;
219 quint32 wclass;
220 WindowGroupRef group;
221 IconRef windowIcon; // the current window icon, if set with setWindowIcon_sys.
222 quint32 savedWindowAttributesFromMaximized; // Saved attributes from when the calling updateMaximizeButton_sys()
223 // This value is just to make sure we maximize and restore to the right location, yet we allow apps to be maximized and
224 // manually resized.
225 // The name is misleading, since this is set when maximizing the window. It is a hint to saveGeometry(..) to record the
226 // starting position as 0,0 instead of the normal starting position.
227 bool wasMaximized;
228#endif
229};
230
231struct QWExtra {
232 // *************************** Cross-platform variables *****************************
233
234 // Regular pointers (keep them together to avoid gaps on 64 bits architectures).
235 void *glContext; // if the widget is hijacked by QGLWindowSurface
236 QTLWExtra *topextra; // only useful for TLWs
237#if QT_CONFIG(graphicsview)
238 QGraphicsProxyWidget *proxyWidget; // if the widget is embedded
239#endif
240#ifndef QT_NO_CURSOR
241 QCursor *curs;
242#endif
243 QPointer<QStyle> style;
244 QPointer<QWidget> focus_proxy;
245
246 // Implicit pointers (shared_empty/shared_null).
247 QRegion mask; // widget mask
248 QString styleSheet;
249
250 // Other variables.
251 qint32 minw;
252 qint32 minh; // minimum size
253 qint32 maxw;
254 qint32 maxh; // maximum size
255 quint16 customDpiX;
256 quint16 customDpiY;
257 QSize staticContentsSize;
258
259 // *************************** Cross-platform bit fields ****************************
260 uint explicitMinSize : 2;
261 uint explicitMaxSize : 2;
262 uint autoFillBackground : 1;
263 uint nativeChildrenForced : 1;
264 uint inRenderWithPainter : 1;
265 uint hasMask : 1;
266 uint hasWindowContainer : 1;
267
268 // *************************** Platform specific values (bit fields first) **********
269#if 0 /* Used to be included in Qt4 for Q_WS_WIN */ // <----------------------------------------------------------- WIN
270#if QT_CONFIG(draganddrop)
271 QOleDropTarget *dropTarget; // drop target
272 QList<QPointer<QWidget> > oleDropWidgets;
273#endif
274#elif 0 /* Used to be included in Qt4 for Q_WS_X11 */ // <--------------------------------------------------------- X11
275 uint compress_events : 1;
276 WId xDndProxy; // XDND forwarding to embedded windows
277#elif 0 /* Used to be included in Qt4 for Q_WS_MAC */ // <------------------------------------------------------ MAC
278 // Cocoa Mask stuff
279 QImage maskBits;
280 CGImageRef imageMask;
281#endif
282};
283
284/*!
285 \internal
286
287 Returns \c true if \a p or any of its parents enable the
288 Qt::BypassGraphicsProxyWidget window flag. Used in QWidget::show() and
289 QWidget::setParent() to determine whether it's necessary to embed the
290 widget into a QGraphicsProxyWidget or not.
291*/
292static inline bool bypassGraphicsProxyWidget(const QWidget *p)
293{
294 while (p) {
295 if (p->windowFlags() & Qt::BypassGraphicsProxyWidget)
296 return true;
297 p = p->parentWidget();
298 }
299 return false;
300}
301
302class Q_WIDGETS_EXPORT QWidgetPrivate : public QObjectPrivate
303{
304 Q_DECLARE_PUBLIC(QWidget)
305
306public:
307 // *************************** Cross-platform ***************************************
308 enum DrawWidgetFlags {
309 DrawAsRoot = 0x01,
310 DrawPaintOnScreen = 0x02,
311 DrawRecursive = 0x04,
312 DrawInvisible = 0x08,
313 DontSubtractOpaqueChildren = 0x10,
314 DontDrawOpaqueChildren = 0x20,
315 DontDrawNativeChildren = 0x40,
316 DontSetCompositionMode = 0x80
317 };
318
319 enum CloseMode {
320 CloseNoEvent,
321 CloseWithEvent,
322 CloseWithSpontaneousEvent
323 };
324
325 enum Direction {
326 DirectionNorth = 0x01,
327 DirectionEast = 0x10,
328 DirectionSouth = 0x02,
329 DirectionWest = 0x20
330 };
331
332 // Functions.
333 explicit QWidgetPrivate(int version = QObjectPrivateVersion);
334 ~QWidgetPrivate();
335
336 static QWidgetPrivate *get(QWidget *w) { return w->d_func(); }
337 static const QWidgetPrivate *get(const QWidget *w) { return w->d_func(); }
338
339 QWExtra *extraData() const;
340 QTLWExtra *topData() const;
341 QTLWExtra *maybeTopData() const;
342 QPainter *sharedPainter() const;
343 void setSharedPainter(QPainter *painter);
344 QWidgetBackingStore *maybeBackingStore() const;
345 QWidgetWindow *windowHandle() const;
346
347 template <typename T>
348 void repaint(T t);
349
350 template <typename T>
351 void update(T t);
352
353 void init(QWidget *desktopWidget, Qt::WindowFlags f);
354 void create_sys(WId window, bool initializeWindow, bool destroyOldWindow);
355 void createRecursively();
356 void createWinId();
357
358 void setScreenForPoint(const QPoint &pos);
359
360 void createTLExtra();
361 void createExtra();
362 void deleteExtra();
363 void createSysExtra();
364 void deleteSysExtra();
365 void createTLSysExtra();
366 void deleteTLSysExtra();
367 void updateSystemBackground();
368 void propagatePaletteChange();
369
370 void setPalette_helper(const QPalette &);
371 void resolvePalette();
372 QPalette naturalWidgetPalette(uint inheritedMask) const;
373
374 void setMask_sys(const QRegion &);
375
376 void raise_sys();
377 void lower_sys();
378 void stackUnder_sys(QWidget *);
379
380 QWidget *deepestFocusProxy() const;
381 void setFocus_sys();
382 void updateFocusChild();
383
384 void updateFont(const QFont &);
385 inline void setFont_helper(const QFont &font) {
386 if (directFontResolveMask == font.resolve() && data.fnt == font)
387 return;
388 updateFont(font);
389 }
390 QFont localFont() const;
391 void resolveFont();
392 QFont naturalWidgetFont(uint inheritedMask) const;
393
394 void setLayoutDirection_helper(Qt::LayoutDirection);
395 void resolveLayoutDirection();
396
397 void setLocale_helper(const QLocale &l, bool forceUpdate = false);
398 void resolveLocale();
399
400 void setStyle_helper(QStyle *newStyle, bool propagate);
401 void inheritStyle();
402
403 void setUpdatesEnabled_helper(bool );
404
405 bool updateBrushOrigin(QPainter *, const QBrush &brush) const;
406 void paintBackground(QPainter *, const QRegion &, int flags = DrawAsRoot) const;
407 bool isAboutToShow() const;
408 QRegion prepareToRender(const QRegion &region, QWidget::RenderFlags renderFlags);
409 void render_helper(QPainter *painter, const QPoint &targetOffset, const QRegion &sourceRegion,
410 QWidget::RenderFlags renderFlags);
411 void render(QPaintDevice *target, const QPoint &targetOffset, const QRegion &sourceRegion,
412 QWidget::RenderFlags renderFlags);
413 void drawWidget(QPaintDevice *pdev, const QRegion &rgn, const QPoint &offset, int flags,
414 QPainter *sharedPainter = nullptr, QWidgetBackingStore *backingStore = nullptr);
415 void sendPaintEvent(const QRegion &toBePainted);
416
417
418 void paintSiblingsRecursive(QPaintDevice *pdev, const QObjectList& children, int index,
419 const QRegion &rgn, const QPoint &offset, int flags,
420 QPainter *sharedPainter, QWidgetBackingStore *backingStore);
421
422#if QT_CONFIG(graphicsview)
423 static QGraphicsProxyWidget * nearestGraphicsProxyWidget(const QWidget *origin);
424#endif
425 void repaint_sys(const QRegion &rgn);
426
427 QRect clipRect() const;
428 QRegion clipRegion() const;
429 void setSystemClip(QPaintEngine *paintEngine, qreal devicePixelRatio, const QRegion &region);
430 void subtractOpaqueChildren(QRegion &rgn, const QRect &clipRect) const;
431 void subtractOpaqueSiblings(QRegion &source, bool *hasDirtySiblingsAbove = nullptr,
432 bool alsoNonOpaque = false) const;
433 void clipToEffectiveMask(QRegion &region) const;
434 void updateIsOpaque();
435 void setOpaque(bool opaque);
436 void updateIsTranslucent();
437 bool paintOnScreen() const;
438#if QT_CONFIG(graphicseffect)
439 void invalidateGraphicsEffectsRecursively();
440#endif // QT_CONFIG(graphicseffect)
441
442 const QRegion &getOpaqueChildren() const;
443 void setDirtyOpaqueRegion();
444
445 bool close_helper(CloseMode mode);
446
447 void setWindowIcon_helper();
448 void setWindowIcon_sys();
449 void setWindowOpacity_sys(qreal opacity);
450 void adjustQuitOnCloseAttribute();
451
452 void scrollChildren(int dx, int dy);
453 void moveRect(const QRect &, int dx, int dy);
454 void scrollRect(const QRect &, int dx, int dy);
455 void invalidateBackingStore_resizeHelper(const QPoint &oldPos, const QSize &oldSize);
456
457 template <class T>
458 void invalidateBackingStore(const T &);
459
460 QRegion overlappedRegion(const QRect &rect, bool breakAfterFirst = false) const;
461 void syncBackingStore();
462 void syncBackingStore(const QRegion &region);
463
464 // tells the input method about the widgets transform
465 void updateWidgetTransform(QEvent *event);
466
467 void reparentFocusWidgets(QWidget *oldtlw);
468
469 static int pointToRect(const QPoint &p, const QRect &r);
470
471 void setWinId(WId);
472 void showChildren(bool spontaneous);
473 void hideChildren(bool spontaneous);
474 void setParent_sys(QWidget *parent, Qt::WindowFlags);
475 void scroll_sys(int dx, int dy);
476 void scroll_sys(int dx, int dy, const QRect &r);
477 void deactivateWidgetCleanup();
478 void setGeometry_sys(int, int, int, int, bool);
479 void fixPosIncludesFrame();
480 void sendPendingMoveAndResizeEvents(bool recursive = false, bool disableUpdates = false);
481 void activateChildLayoutsRecursively();
482 void show_recursive();
483 void show_helper();
484 void show_sys();
485 void hide_sys();
486 void hide_helper();
487 void _q_showIfNotHidden();
488 void setVisible(bool);
489
490 void setEnabled_helper(bool);
491 static void adjustFlags(Qt::WindowFlags &flags, QWidget *w = 0);
492
493 void updateFrameStrut();
494 QRect frameStrut() const;
495
496#ifdef QT_KEYPAD_NAVIGATION
497 static bool navigateToDirection(Direction direction);
498 static QWidget *widgetInNavigationDirection(Direction direction);
499 static bool canKeypadNavigate(Qt::Orientation orientation);
500 static bool inTabWidget(QWidget *widget);
501#endif
502
503 void setWindowIconText_sys(const QString &cap);
504 void setWindowIconText_helper(const QString &cap);
505 void setWindowTitle_sys(const QString &cap);
506 void setWindowFilePath_sys(const QString &filePath);
507
508#ifndef QT_NO_CURSOR
509 void setCursor_sys(const QCursor &cursor);
510 void unsetCursor_sys();
511#endif
512
513 void setWindowTitle_helper(const QString &cap);
514 void setWindowFilePath_helper(const QString &filePath);
515 void setWindowModified_helper();
516 virtual void setWindowFlags(Qt::WindowFlags windowFlags);
517
518 bool setMinimumSize_helper(int &minw, int &minh);
519 bool setMaximumSize_helper(int &maxw, int &maxh);
520 void setConstraints_sys();
521 bool pointInsideRectAndMask(const QPoint &) const;
522 QWidget *childAt_helper(const QPoint &, bool) const;
523 QWidget *childAtRecursiveHelper(const QPoint &p, bool) const;
524 void updateGeometry_helper(bool forceUpdate);
525
526 void getLayoutItemMargins(int *left, int *top, int *right, int *bottom) const;
527 void setLayoutItemMargins(int left, int top, int right, int bottom);
528 void setLayoutItemMargins(QStyle::SubElement element, const QStyleOption *opt = nullptr);
529
530 void updateContentsRect();
531 QMargins safeAreaMargins() const;
532
533 // aboutToDestroy() is called just before the contents of
534 // QWidget::destroy() is executed. It's used to signal QWidget
535 // sub-classes that their internals are about to be released.
536 virtual void aboutToDestroy() {}
537
538 inline QWidget *effectiveFocusWidget() {
539 QWidget *w = q_func();
540 while (w->focusProxy())
541 w = w->focusProxy();
542 return w;
543 }
544
545 void setModal_sys();
546
547 // This is an helper function that return the available geometry for
548 // a widget and takes care is this one is in QGraphicsView.
549 // If the widget is not embed in a scene then the geometry available is
550 // null, we let QDesktopWidget decide for us.
551 static QRect screenGeometry(const QWidget *widget)
552 {
553 QRect screen;
554#if QT_CONFIG(graphicsview)
555 QGraphicsProxyWidget *ancestorProxy = widget->d_func()->nearestGraphicsProxyWidget(widget);
556 //It's embedded if it has an ancestor
557 if (ancestorProxy) {
558 if (!bypassGraphicsProxyWidget(widget) && ancestorProxy->scene() != nullptr) {
559 // One view, let be smart and return the viewport rect then the popup is aligned
560 if (ancestorProxy->scene()->views().size() == 1) {
561 QGraphicsView *view = ancestorProxy->scene()->views().at(0);
562 screen = view->mapToScene(view->viewport()->rect()).boundingRect().toRect();
563 } else {
564 screen = ancestorProxy->scene()->sceneRect().toRect();
565 }
566 }
567 }
568#else
569 Q_UNUSED(widget);
570#endif
571 return screen;
572 }
573
574 inline void setRedirected(QPaintDevice *replacement, const QPoint &offset)
575 {
576 Q_ASSERT(q_func()->testAttribute(Qt::WA_WState_InPaintEvent));
577 redirectDev = replacement;
578 redirectOffset = offset;
579 }
580
581 inline QPaintDevice *redirected(QPoint *offset) const
582 {
583 if (offset)
584 *offset = redirectDev ? redirectOffset : QPoint();
585 return redirectDev;
586 }
587
588 inline void restoreRedirected()
589 { redirectDev = nullptr; }
590
591 inline void enforceNativeChildren()
592 {
593 if (!extra)
594 createExtra();
595
596 if (extra->nativeChildrenForced)
597 return;
598 extra->nativeChildrenForced = 1;
599
600 for (int i = 0; i < children.size(); ++i) {
601 if (QWidget *child = qobject_cast<QWidget *>(children.at(i)))
602 child->setAttribute(Qt::WA_NativeWindow);
603 }
604 }
605
606 inline bool nativeChildrenForced() const
607 {
608 return extra ? extra->nativeChildrenForced : false;
609 }
610
611 inline QRect effectiveRectFor(const QRegion &region) const
612 {
613 return effectiveRectFor(region.boundingRect());
614 }
615
616 inline QRect effectiveRectFor(const QRect &rect) const
617 {
618#if QT_CONFIG(graphicseffect)
619 if (graphicsEffect && graphicsEffect->isEnabled())
620 return graphicsEffect->boundingRectFor(rect).toAlignedRect();
621#endif // QT_CONFIG(graphicseffect)
622 return rect;
623 }
624
625 QSize adjustedSize() const;
626
627 inline void handleSoftwareInputPanel(Qt::MouseButton button, bool clickCausedFocus)
628 {
629 Q_Q(QWidget);
630 if (button == Qt::LeftButton && qApp->autoSipEnabled()) {
631 QStyle::RequestSoftwareInputPanel behavior = QStyle::RequestSoftwareInputPanel(
632 q->style()->styleHint(QStyle::SH_RequestSoftwareInputPanel));
633 if (!clickCausedFocus || behavior == QStyle::RSIP_OnMouseClick) {
634 QGuiApplication::inputMethod()->show();
635 }
636 }
637 }
638
639 void setWSGeometry();
640
641 inline QPoint mapToWS(const QPoint &p) const
642 { return p - data.wrect.topLeft(); }
643
644 inline QPoint mapFromWS(const QPoint &p) const
645 { return p + data.wrect.topLeft(); }
646
647 inline QRect mapToWS(const QRect &r) const
648 { return r.translated(-data.wrect.topLeft()); }
649
650 inline QRect mapFromWS(const QRect &r) const
651 { return r.translated(data.wrect.topLeft()); }
652
653 QOpenGLContext *shareContext() const;
654
655 virtual QObject *focusObject() { return nullptr; }
656
657#ifndef QT_NO_OPENGL
658 virtual GLuint textureId() const { return 0; }
659 virtual QPlatformTextureList::Flags textureListFlags() {
660 Q_Q(QWidget);
661 return q->testAttribute(Qt::WA_AlwaysStackOnTop)
662 ? QPlatformTextureList::StacksOnTop
663 : QPlatformTextureList::Flags(nullptr);
664 }
665 virtual QImage grabFramebuffer() { return QImage(); }
666 virtual void beginBackingStorePainting() { }
667 virtual void endBackingStorePainting() { }
668 virtual void beginCompose() { }
669 virtual void endCompose() { }
670 void setRenderToTexture() { renderToTexture = true; setTextureChildSeen(); }
671 void setTextureChildSeen()
672 {
673 Q_Q(QWidget);
674 if (textureChildSeen)
675 return;
676 textureChildSeen = 1;
677
678 if (!q->isWindow()) {
679 QWidget *parent = q->parentWidget();
680 if (parent)
681 get(parent)->setTextureChildSeen();
682 }
683 }
684 static void sendComposeStatus(QWidget *w, bool end);
685 // Called on setViewport().
686 virtual void initializeViewportFramebuffer() { }
687 // When using a QOpenGLWidget as viewport with QAbstractScrollArea, resize events are
688 // filtered away from the widget. This is fine for QGLWidget but bad for QOpenGLWidget
689 // since the fbo must be resized. We need an alternative way to notify.
690 virtual void resizeViewportFramebuffer() { }
691 // Called after each paint event.
692 virtual void resolveSamples() { }
693#endif
694
695 static void setWidgetParentHelper(QObject *widgetAsObject, QObject *newParent);
696
697 // Variables.
698 // Regular pointers (keep them together to avoid gaps on 64 bit architectures).
699 QWExtra *extra;
700 QWidget *focus_next;
701 QWidget *focus_prev;
702 QWidget *focus_child;
703 QLayout *layout;
704 QRegion *needsFlush;
705 QPaintDevice *redirectDev;
706 QWidgetItemV2 *widgetItem;
707 QPaintEngine *extraPaintEngine;
708 mutable const QMetaObject *polished;
709 QGraphicsEffect *graphicsEffect;
710 // All widgets are added into the allWidgets set. Once
711 // they receive a window id they are also added to the mapper.
712 // This should just ensure that all widgets are deleted by QApplication
713 static QWidgetMapper *mapper;
714 static QWidgetSet *allWidgets;
715#if !defined(QT_NO_IM)
716 Qt::InputMethodHints imHints;
717#endif
718#ifdef QT_KEYPAD_NAVIGATION
719 static QPointer<QWidget> editingWidget;
720#endif
721
722 // Implicit pointers (shared_null/shared_empty).
723 QRegion opaqueChildren;
724 QRegion dirty;
725#ifndef QT_NO_TOOLTIP
726 QString toolTip;
727 int toolTipDuration;
728#endif
729#if QT_CONFIG(statustip)
730 QString statusTip;
731#endif
732#if QT_CONFIG(whatsthis)
733 QString whatsThis;
734#endif
735#ifndef QT_NO_ACCESSIBILITY
736 QString accessibleName;
737 QString accessibleDescription;
738#endif
739
740 // Other variables.
741 uint directFontResolveMask;
742 uint inheritedFontResolveMask;
743 uint inheritedPaletteResolveMask;
744 short leftmargin;
745 short topmargin;
746 short rightmargin;
747 short bottommargin;
748 signed char leftLayoutItemMargin;
749 signed char topLayoutItemMargin;
750 signed char rightLayoutItemMargin;
751 signed char bottomLayoutItemMargin;
752 static int instanceCounter; // Current number of widget instances
753 static int maxInstances; // Maximum number of widget instances
754 Qt::HANDLE hd;
755 QWidgetData data;
756 QSizePolicy size_policy;
757 QLocale locale;
758 QPoint redirectOffset;
759#ifndef QT_NO_ACTION
760 QList<QAction*> actions;
761#endif
762#ifndef QT_NO_GESTURES
763 QMap<Qt::GestureType, Qt::GestureFlags> gestureContext;
764#endif
765
766 // Bit fields.
767 uint high_attributes[4]; // the low ones are in QWidget::widget_attributes
768 QPalette::ColorRole fg_role : 8;
769 QPalette::ColorRole bg_role : 8;
770 uint dirtyOpaqueChildren : 1;
771 uint isOpaque : 1;
772 uint retainSizeWhenHiddenChanged : 1;
773 uint inDirtyList : 1;
774 uint isScrolled : 1;
775 uint isMoved : 1;
776 uint usesDoubleBufferedGLContext : 1;
777 uint mustHaveWindowHandle : 1;
778 uint renderToTexture : 1;
779 uint textureChildSeen : 1;
780#ifndef QT_NO_IM
781 uint inheritsInputMethodHints : 1;
782#endif
783#ifndef QT_NO_OPENGL
784 uint renderToTextureReallyDirty : 1;
785 uint renderToTextureComposeActive : 1;
786#endif
787 uint childrenHiddenByWState : 1;
788 uint childrenShownByExpose : 1;
789
790 // *************************** Platform specific ************************************
791#if defined(Q_OS_WIN)
792 uint noPaintOnScreen : 1; // see qwidget.cpp ::paintEngine()
793#endif
794#if 0 /* Used to be included in Qt4 for Q_WS_X11 */ // <----------------------------------------------------------- X11
795 Qt::HANDLE picture;
796 static QWidget *mouseGrabber;
797 static QWidget *keyboardGrabber;
798
799 void setWindowRole();
800 void sendStartupMessage(const char *message) const;
801 void x11UpdateIsOpaque();
802 bool isBackgroundInherited() const;
803 void updateX11AcceptFocus();
804 QPoint mapToGlobal(const QPoint &pos) const;
805 QPoint mapFromGlobal(const QPoint &pos) const;
806#elif 0 /* Used to be included in Qt4 for Q_WS_WIN */ // <--------------------------------------------------------- WIN
807#ifndef QT_NO_GESTURES
808 uint nativeGesturePanEnabled : 1;
809#endif
810 bool shouldShowMaximizeButton();
811 void winUpdateIsOpaque();
812 void reparentChildren();
813#if QT_CONFIG(draganddrop)
814 QOleDropTarget *registerOleDnd(QWidget *widget);
815 void unregisterOleDnd(QWidget *widget, QOleDropTarget *target);
816#endif
817 void grabMouseWhileInWindow();
818 void registerTouchWindow();
819 void winSetupGestures();
820#elif defined(Q_OS_MAC) // <--------------------------------------------------------- MAC
821 void macUpdateSizeAttribute();
822#elif 0 /* Used to be included in Qt4 for Q_WS_MAC */ // <--------------------------------------------------------- MAC (old stuff)
823 // This is new stuff
824 uint needWindowChange : 1;
825
826 // Each wiget keeps a list of all its child and grandchild OpenGL widgets.
827 // This list is used to update the gl context whenever a parent and a granparent
828 // moves, and also to check for intersections with gl widgets within the window
829 // when a widget moves.
830 struct GlWidgetInfo
831 {
832 GlWidgetInfo(QWidget *widget) : widget(widget), lastUpdateWidget(0) { }
833 bool operator==(const GlWidgetInfo &other) const { return (widget == other.widget); }
834 QWidget * widget;
835 QWidget * lastUpdateWidget;
836 };
837
838 // dirtyOnWidget contains the areas in the widget that needs to be repained,
839 // in the same way as dirtyOnScreen does for the window. Areas are added in
840 // dirtyWidget_sys and cleared in the paint event. In scroll_sys we then use
841 // this information repaint invalid areas when widgets are scrolled.
842 QRegion dirtyOnWidget;
843 EventHandlerRef window_event;
844 QList<GlWidgetInfo> glWidgets;
845
846 //these are here just for code compat (HIViews)
847 Qt::HANDLE qd_hd;
848
849 void macUpdateHideOnSuspend();
850 void macUpdateOpaqueSizeGrip();
851 void macUpdateIgnoreMouseEvents();
852 void macUpdateMetalAttribute();
853 void macUpdateIsOpaque();
854 void macSetNeedsDisplay(QRegion region);
855 void setEnabled_helper_sys(bool enable);
856 bool isRealWindow() const;
857 void adjustWithinMaxAndMinSize(int &w, int &h);
858 void applyMaxAndMinSizeOnWindow();
859 void update_sys(const QRect &rect);
860 void update_sys(const QRegion &rgn);
861 void setGeometry_sys_helper(int, int, int, int, bool);
862 void updateMaximizeButton_sys();
863 void createWindow_sys();
864 void recreateMacWindow();
865 void setSubWindowStacking(bool set);
866 void setWindowLevel();
867 void finishCreateWindow_sys_Cocoa(void * /*NSWindow * */ windowRef);
868 void syncCocoaMask();
869 void finishCocoaMaskSetup();
870 // Did we add the drawRectOriginal method?
871 bool drawRectOriginalAdded;
872 // Is the original drawRect method available?
873 bool originalDrawMethod;
874 // Do we need to change the methods?
875 bool changeMethods;
876
877 // Unified toolbar variables
878 bool isInUnifiedToolbar;
879 QUnifiedToolbarSurface *unifiedSurface;
880 QPoint toolbar_offset;
881 QWidget *toolbar_ancestor;
882 bool flushRequested;
883 bool touchEventsEnabled;
884 void determineWindowClass();
885 void transferChildren();
886 bool qt_mac_dnd_event(uint, DragRef);
887 void toggleDrawers(bool);
888 //mac event functions
889 static bool qt_create_root_win();
890 static void qt_clean_root_win();
891 static bool qt_mac_update_sizer(QWidget *, int up = 0);
892 static OSStatus qt_window_event(EventHandlerCallRef er, EventRef event, void *);
893 static OSStatus qt_widget_event(EventHandlerCallRef er, EventRef event, void *);
894 static bool qt_widget_rgn(QWidget *, short, RgnHandle, bool);
895 void registerTouchWindow(bool enable = true);
896#endif
897 void setNetWmWindowTypes(bool skipIfMissing = false);
898
899 bool stealKeyboardGrab(bool grab);
900 bool stealMouseGrab(bool grab);
901};
902
903struct QWidgetPaintContext
904{
905 inline QWidgetPaintContext(QPaintDevice *d, const QRegion &r, const QPoint &o, int f,
906 QPainter *p, QWidgetBackingStore *b)
907 : pdev(d), rgn(r), offset(o), flags(f), sharedPainter(p), backingStore(b), painter(nullptr) {}
908
909 QPaintDevice *pdev;
910 QRegion rgn;
911 QPoint offset;
912 int flags;
913 QPainter *sharedPainter;
914 QWidgetBackingStore *backingStore;
915 QPainter *painter;
916};
917
918#if QT_CONFIG(graphicseffect)
919class QWidgetEffectSourcePrivate : public QGraphicsEffectSourcePrivate
920{
921public:
922 QWidgetEffectSourcePrivate(QWidget *widget)
923 : QGraphicsEffectSourcePrivate(), m_widget(widget), context(nullptr), updateDueToGraphicsEffect(false)
924 {}
925
926 void detach() override
927 { m_widget->d_func()->graphicsEffect = nullptr; }
928
929 const QGraphicsItem *graphicsItem() const override
930 { return nullptr; }
931
932 const QWidget *widget() const override
933 { return m_widget; }
934
935 void update() override
936 {
937 updateDueToGraphicsEffect = true;
938 m_widget->update();
939 updateDueToGraphicsEffect = false;
940 }
941
942 bool isPixmap() const override
943 { return false; }
944
945 void effectBoundingRectChanged() override
946 {
947 // ### This function should take a rect parameter; then we can avoid
948 // updating too much on the parent widget.
949 if (QWidget *parent = m_widget->parentWidget())
950 parent->update();
951 else
952 update();
953 }
954
955 const QStyleOption *styleOption() const override
956 { return nullptr; }
957
958 QRect deviceRect() const override
959 { return m_widget->window()->rect(); }
960
961 QRectF boundingRect(Qt::CoordinateSystem system) const override;
962 void draw(QPainter *p) override;
963 QPixmap pixmap(Qt::CoordinateSystem system, QPoint *offset,
964 QGraphicsEffect::PixmapPadMode mode) const override;
965
966 QWidget *m_widget;
967 QWidgetPaintContext *context;
968 QTransform lastEffectTransform;
969 bool updateDueToGraphicsEffect;
970};
971#endif // QT_CONFIG(graphicseffect)
972
973inline QWExtra *QWidgetPrivate::extraData() const
974{
975 return extra;
976}
977
978inline QTLWExtra *QWidgetPrivate::topData() const
979{
980 const_cast<QWidgetPrivate *>(this)->createTLExtra();
981 return extra->topextra;
982}
983
984inline QTLWExtra *QWidgetPrivate::maybeTopData() const
985{
986 return extra ? extra->topextra : nullptr;
987}
988
989inline QPainter *QWidgetPrivate::sharedPainter() const
990{
991 Q_Q(const QWidget);
992 QTLWExtra *x = q->window()->d_func()->maybeTopData();
993 return x ? x->sharedPainter : nullptr;
994}
995
996inline void QWidgetPrivate::setSharedPainter(QPainter *painter)
997{
998 Q_Q(QWidget);
999 QTLWExtra *x = q->window()->d_func()->topData();
1000 x->sharedPainter = painter;
1001}
1002
1003inline bool QWidgetPrivate::pointInsideRectAndMask(const QPoint &p) const
1004{
1005 Q_Q(const QWidget);
1006 return q->rect().contains(p) && (!extra || !extra->hasMask || q->testAttribute(Qt::WA_MouseNoMask)
1007 || extra->mask.contains(p));
1008}
1009
1010inline QWidgetBackingStore *QWidgetPrivate::maybeBackingStore() const
1011{
1012 Q_Q(const QWidget);
1013 QTLWExtra *x = q->window()->d_func()->maybeTopData();
1014 return x ? x->backingStoreTracker.data() : nullptr;
1015}
1016
1017inline QWidgetWindow *QWidgetPrivate::windowHandle() const
1018{
1019 if (QTLWExtra *x = maybeTopData())
1020 return x->window;
1021 return nullptr;
1022}
1023
1024QT_END_NAMESPACE
1025
1026#endif // QWIDGET_P_H
1027