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
qmdisubwindow.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
5/*!
6 \class QMdiSubWindow
7 \brief The QMdiSubWindow class provides a subwindow class for
8 QMdiArea.
9 \since 4.3
10 \ingroup mainwindow-classes
11 \inmodule QtWidgets
12
13 QMdiSubWindow represents a top-level window in a QMdiArea, and consists
14 of a title bar with window decorations, an internal widget, and
15 (depending on the current style) a window frame and a size
16 grip. QMdiSubWindow has its own layout, which consists of the
17 title bar and a center area for the internal widget.
18
19 \image qmdisubwindowlayout.png
20 {MDI window with a title bar and an internal widget}
21
22 The most common way to construct a QMdiSubWindow is to call
23 QMdiArea::addSubWindow() with the internal widget as the argument.
24 You can also create a subwindow yourself, and set an internal
25 widget by calling setWidget().
26
27 You use the same API when programming with subwindows as with
28 regular top-level windows (e.g., you can call functions such as
29 show(), hide(), showMaximized(), and setWindowTitle()).
30
31 \section1 Subwindow Handling
32
33 QMdiSubWindow also supports behavior specific to subwindows in
34 an MDI area.
35
36 By default, each QMdiSubWindow is visible inside the MDI area
37 viewport when moved around, but it is also possible to specify
38 transparent window movement and resizing behavior, where only
39 the outline of a subwindow is updated during these operations.
40 The setOption() function is used to enable this behavior.
41
42 The isShaded() function detects whether the subwindow is
43 currently shaded (i.e., the window is collapsed so that only the
44 title bar is visible). To enter shaded mode, call showShaded().
45 QMdiSubWindow emits the windowStateChanged() signal whenever the
46 window state has changed (e.g., when the window becomes minimized,
47 or is restored). It also emits aboutToActivate() before it is
48 activated.
49
50 In keyboard-interactive mode, the windows are moved and resized
51 with the keyboard. You can enter this mode through the system menu
52 of the window. The keyboardSingleStep and keyboardPageStep
53 properties control the distance the widget is moved or resized for
54 each keypress event. When shift is pressed down page step is used;
55 otherwise single step is used.
56
57 You can also change the active window with the keyboard. By
58 pressing the control and tab keys at the same time, the next
59 (using the current \l{QMdiArea::}{WindowOrder}) subwindow will be
60 activated. By pressing control, shift, and tab, you will activate
61 the previous window. This is equivalent to calling
62 \l{QMdiArea::}{activateNextSubWindow()} and
63 \l{QMdiArea::}{activatePreviousSubWindow()}. Note that these
64 shortcuts overrides global shortcuts, but not the \l{QMdiArea}s
65 shortcuts.
66
67 \sa QMdiArea
68*/
69
70/*!
71 \enum QMdiSubWindow::SubWindowOption
72
73 This enum describes options that customize the behavior
74 of QMdiSubWindow.
75
76 \omitvalue AllowOutsideAreaHorizontally
77 \omitvalue AllowOutsideAreaVertically
78
79 \value RubberBandResize If you enable this option, a rubber band
80 control is used to represent the subwindow's outline, and the user
81 resizes this instead of the subwindow itself.
82 As a result, the subwindow maintains its original position and size
83 until the resize operation has been completed, at which time it will
84 receive a single QResizeEvent.
85 By default, this option is disabled.
86
87 \value RubberBandMove If you enable this option, a rubber band
88 control is used to represent the subwindow's outline, and the user
89 moves this instead of the subwindow itself.
90 As a result, the subwindow remains in its original position until
91 the move operation has completed, at which time a QMoveEvent is
92 sent to the window. By default, this option is disabled.
93*/
94
95/*!
96 \fn QMdiSubWindow::windowStateChanged(Qt::WindowStates oldState, Qt::WindowStates newState)
97
98 QMdiSubWindow emits this signal after the window state changes. \a
99 oldState is the window state before it changed, and \a newState is the
100 new, current state.
101*/
102
103/*!
104 \fn QMdiSubWindow::aboutToActivate()
105
106 QMdiSubWindow emits this signal immediately before it is
107 activated. After the subwindow has been activated, the QMdiArea that
108 manages the subwindow will also emit the
109 \l{QMdiArea::}{subWindowActivated()} signal.
110
111 \sa QMdiArea::subWindowActivated()
112*/
113
114#include "qmdisubwindow_p.h"
115
116#include <QApplication>
117#include <QStylePainter>
118#include <QVBoxLayout>
119#include <QMouseEvent>
120#if QT_CONFIG(whatsthis)
121#include <QWhatsThis>
122#endif
123#if QT_CONFIG(tooltip)
124#include <QToolTip>
125#endif
126#if QT_CONFIG(mainwindow)
127#include <QMainWindow>
128#endif
129#include <QScrollBar>
130#include <QDebug>
131#include <QMdiArea>
132#include <QScopedValueRollback>
133#if QT_CONFIG(action)
134# include <qaction.h>
135#endif
136#if QT_CONFIG(menu)
137#include <QMenu>
138#endif
139#include <QProxyStyle>
140
142
143using namespace Qt::StringLiterals;
144using namespace std::chrono_literals;
145
146using namespace QMdi;
147
149{
150 QStyle::SC_TitleBarLabel, // 1
151 QStyle::SC_TitleBarSysMenu, // 2
152 QStyle::SC_TitleBarMinButton, // 3
153 QStyle::SC_TitleBarMaxButton, // 4
154 QStyle::SC_TitleBarShadeButton, // 5
155 QStyle::SC_TitleBarCloseButton, // 6
156 QStyle::SC_TitleBarNormalButton, // 7
157 QStyle::SC_TitleBarUnshadeButton, // 8
158 QStyle::SC_TitleBarContextHelpButton // 9
159};
160static const int NumSubControls = sizeof(SubControls) / sizeof(SubControls[0]);
161
163 Qt::FramelessWindowHint
164 | Qt::CustomizeWindowHint
165 | Qt::WindowTitleHint
166 | Qt::WindowSystemMenuHint
167 | Qt::WindowMinimizeButtonHint
168 | Qt::WindowMaximizeButtonHint
169 | Qt::WindowMinMaxButtonsHint;
170
171
172static const int BoundaryMargin = 5;
173
174static inline bool isMacStyle(QStyle *style)
175{
176 auto proxyStyle = qobject_cast<QProxyStyle *>(style);
177 auto styleToCheck = proxyStyle ? proxyStyle->baseStyle() : style;
178 return styleToCheck->inherits("QMacStyle");
179}
180
181static inline int getMoveDeltaComponent(uint cflags, uint moveFlag, uint resizeFlag,
182 int delta, int maxDelta, int minDelta)
183{
184 if (cflags & moveFlag) {
185 if (delta > 0)
186 return (cflags & resizeFlag) ? qMin(delta, maxDelta) : delta;
187 return (cflags & resizeFlag) ? qMax(delta, minDelta) : delta;
188 }
189 return 0;
190}
191
192static inline int getResizeDeltaComponent(uint cflags, uint resizeFlag,
193 uint resizeReverseFlag, int delta)
194{
195 if (cflags & resizeFlag) {
196 if (cflags & resizeReverseFlag)
197 return -delta;
198 return delta;
199 }
200 return 0;
201}
202
203static inline bool isChildOfQMdiSubWindow(const QWidget *child)
204{
205 Q_ASSERT(child);
206 QWidget *parent = child->parentWidget();
207 while (parent) {
208 if (qobject_cast<QMdiSubWindow *>(parent))
209 return true;
210 parent = parent->parentWidget();
211 }
212 return false;
213}
214
215static inline bool isChildOfTabbedQMdiArea(const QMdiSubWindow *child)
216{
217 Q_ASSERT(child);
218 if (QMdiArea *mdiArea = child->mdiArea()) {
219 if (mdiArea->viewMode() == QMdiArea::TabbedView)
220 return true;
221 }
222 return false;
223}
224
225template<typename T>
226static inline ControlElement<T> *ptr(QWidget *widget)
227{
228 if (widget && widget->qt_metacast("ControlElement")
229 && strcmp(widget->metaObject()->className(), T::staticMetaObject.className()) == 0) {
230 return static_cast<ControlElement<T> *>(widget);
231 }
232 return nullptr;
233}
234
235QString QMdiSubWindowPrivate::originalWindowTitleHelper() const
236{
237 Q_Q(const QMdiSubWindow);
238 // QTBUG-92240: When DontMaximizeSubWindowOnActivation is set and
239 // there is another subwindow maximized, use its original title.
240 if (auto *mdiArea = q->mdiArea()) {
241 const auto &subWindows = mdiArea->subWindowList();
242 for (auto *subWindow : subWindows) {
243 if (subWindow != q && subWindow->isMaximized()) {
244 auto *subWindowD = static_cast<QMdiSubWindowPrivate *>(qt_widget_private(subWindow));
245 if (!subWindowD->originalTitle.isNull())
246 return subWindowD->originalTitle;
247 }
248 }
249 }
250 return q->window()->windowTitle();
251}
252
253QString QMdiSubWindowPrivate::originalWindowTitle()
254{
255 if (originalTitle.isNull()) {
256 originalTitle = originalWindowTitleHelper();
257 if (originalTitle.isNull())
258 originalTitle = ""_L1;
259 }
260 return originalTitle;
261}
262
263void QMdiSubWindowPrivate::setNewWindowTitle()
264{
265 Q_Q(QMdiSubWindow);
266 QString childTitle = q->windowTitle();
267 if (childTitle.isEmpty())
268 return;
269 QString original = originalWindowTitle();
270 if (!original.isEmpty()) {
271 if (!original.contains(QMdiSubWindow::tr("- [%1]").arg(childTitle))) {
272 auto title = QMdiSubWindow::tr("%1 - [%2]").arg(original, childTitle);
273 ignoreWindowTitleChange = true;
274 q->window()->setWindowTitle(title);
275 ignoreWindowTitleChange = false;
276 }
277
278 } else {
279 ignoreWindowTitleChange = true;
280 q->window()->setWindowTitle(childTitle);
281 ignoreWindowTitleChange = false;
282 }
283}
284
285static inline bool isHoverControl(QStyle::SubControl control)
286{
287 return control != QStyle::SC_None && control != QStyle::SC_TitleBarLabel;
288}
289
290#if QT_CONFIG(tooltip)
291static void showToolTip(QHelpEvent *helpEvent, QWidget *widget, const QStyleOptionComplex &opt,
292 QStyle::ComplexControl complexControl, QStyle::SubControl subControl)
293{
294 Q_ASSERT(helpEvent);
295 Q_ASSERT(helpEvent->type() == QEvent::ToolTip);
296 Q_ASSERT(widget);
297
298 if (widget->style()->styleHint(QStyle::SH_TitleBar_ShowToolTipsOnButtons, &opt, widget))
299 return;
300
301 // Convert CC_MdiControls to CC_TitleBar. Sub controls of different complex
302 // controls cannot be in the same switch as they might have the same value.
303 if (complexControl == QStyle::CC_MdiControls) {
304 if (subControl == QStyle::SC_MdiMinButton)
305 subControl = QStyle::SC_TitleBarMinButton;
306 else if (subControl == QStyle::SC_MdiCloseButton)
307 subControl = QStyle::SC_TitleBarCloseButton;
308 else if (subControl == QStyle::SC_MdiNormalButton)
309 subControl = QStyle::SC_TitleBarNormalButton;
310 else
311 subControl = QStyle::SC_None;
312 }
313
314 // Don't change the tooltip for the base widget itself.
315 if (subControl == QStyle::SC_None)
316 return;
317
318 QString toolTip;
319
320 switch (subControl) {
321 case QStyle::SC_TitleBarMinButton:
322 toolTip = QMdiSubWindow::tr("Minimize");
323 break;
324 case QStyle::SC_TitleBarMaxButton:
325 toolTip = QMdiSubWindow::tr("Maximize");
326 break;
327 case QStyle::SC_TitleBarUnshadeButton:
328 toolTip = QMdiSubWindow::tr("Unshade");
329 break;
330 case QStyle::SC_TitleBarShadeButton:
331 toolTip = QMdiSubWindow::tr("Shade");
332 break;
333 case QStyle::SC_TitleBarNormalButton:
334 if (widget->isMaximized() || !qobject_cast<QMdiSubWindow *>(widget))
335 toolTip = QMdiSubWindow::tr("Restore Down");
336 else
337 toolTip = QMdiSubWindow::tr("Restore");
338 break;
339 case QStyle::SC_TitleBarCloseButton:
340 toolTip = QMdiSubWindow::tr("Close");
341 break;
342 case QStyle::SC_TitleBarContextHelpButton:
343 toolTip = QMdiSubWindow::tr("Help");
344 break;
345 case QStyle::SC_TitleBarSysMenu:
346 toolTip = QMdiSubWindow::tr("Menu");
347 break;
348 default:
349 break;
350 }
351
352 const QRect rect = widget->style()->subControlRect(complexControl, &opt, subControl, widget);
353 QToolTip::showText(helpEvent->globalPos(), toolTip, widget, rect);
354}
355#endif // QT_CONFIG(tooltip)
356
357namespace QMdi {
358/*
359 \class ControlLabel
360 \internal
361*/
362class ControlLabel : public QWidget
363{
365public:
367
368 QSize sizeHint() const override;
369
370signals:
373
374protected:
375 bool event(QEvent *event) override;
376 void paintEvent(QPaintEvent *paintEvent) override;
377 void mousePressEvent(QMouseEvent *mouseEvent) override;
378 void mouseDoubleClickEvent(QMouseEvent *mouseEvent) override;
379 void mouseReleaseEvent(QMouseEvent *mouseEvent) override;
380
381private:
382 QPixmap label;
383 bool isPressed;
384 void updateWindowIcon();
385};
386} // namespace QMdi
387
388ControlLabel::ControlLabel(QWidget *parent)
389 : QWidget(parent), isPressed(false)
390{
391 setFocusPolicy(Qt::NoFocus);
392 updateWindowIcon();
393 setFixedSize(label.deviceIndependentSize().toSize());
394}
395
396/*
397 \internal
398*/
400{
401 return label.deviceIndependentSize().toSize();
402}
403
404/*
405 \internal
406*/
407bool ControlLabel::event(QEvent *event)
408{
409 if (event->type() == QEvent::WindowIconChange)
410 updateWindowIcon();
411 else if (event->type() == QEvent::StyleChange) {
412 updateWindowIcon();
413 setFixedSize(label.size());
414 }
415#if QT_CONFIG(tooltip)
416 else if (event->type() == QEvent::ToolTip) {
417 QStyleOptionTitleBar options;
418 options.initFrom(this);
419 showToolTip(static_cast<QHelpEvent *>(event), this, options,
420 QStyle::CC_TitleBar, QStyle::SC_TitleBarSysMenu);
421 }
422#endif
423 return QWidget::event(event);
424}
425
426/*
427 \internal
428*/
429void ControlLabel::paintEvent(QPaintEvent * /*paintEvent*/)
430{
431 QPainter painter(this);
432 painter.drawPixmap(0, 0, label);
433}
434
435/*
436 \internal
437*/
438void ControlLabel::mousePressEvent(QMouseEvent *mouseEvent)
439{
440 if (mouseEvent->button() != Qt::LeftButton) {
441 mouseEvent->ignore();
442 return;
443 }
444 isPressed = true;
445}
446
447/*
448 \internal
449*/
450void ControlLabel::mouseDoubleClickEvent(QMouseEvent *mouseEvent)
451{
452 if (mouseEvent->button() != Qt::LeftButton) {
453 mouseEvent->ignore();
454 return;
455 }
456 isPressed = false;
457 emit _q_doubleClicked();
458}
459
460/*
461 \internal
462*/
463void ControlLabel::mouseReleaseEvent(QMouseEvent *mouseEvent)
464{
465 if (mouseEvent->button() != Qt::LeftButton) {
466 mouseEvent->ignore();
467 return;
468 }
469 if (isPressed) {
470 isPressed = false;
471 emit _q_clicked();
472 }
473}
474
475/*
476 \internal
477*/
478void ControlLabel::updateWindowIcon()
479{
480 QIcon menuIcon = windowIcon();
481 if (menuIcon.isNull())
482 menuIcon = style()->standardIcon(QStyle::SP_TitleBarMenuButton, nullptr, parentWidget());
483 const int iconSize = style()->pixelMetric(QStyle::PM_TitleBarButtonIconSize, nullptr, parentWidget());
484 label = menuIcon.pixmap(QSize(iconSize, iconSize), devicePixelRatio());
485 update();
486}
487
488namespace QMdi {
489/*
490 \class ControllerWidget
491 \internal
492*/
494{
496public:
498 QSize sizeHint() const override;
499 void setControlVisible(QMdiSubWindowPrivate::WindowStateAction action, bool visible);
500 inline bool hasVisibleControls() const
501 {
502 return (visibleControls & QStyle::SC_MdiMinButton)
503 || (visibleControls & QStyle::SC_MdiNormalButton)
504 || (visibleControls & QStyle::SC_MdiCloseButton);
505 }
506
507signals:
510 void _q_close();
511
512protected:
513 void paintEvent(QPaintEvent *event) override;
514 void mousePressEvent(QMouseEvent *event) override;
515 void mouseReleaseEvent(QMouseEvent *event) override;
516 void mouseMoveEvent(QMouseEvent *event) override;
517 void leaveEvent(QEvent *event) override;
518 bool event(QEvent *event) override;
519
520private:
521 QStyle::SubControl activeControl;
522 QStyle::SubControl hoverControl;
523 QStyle::SubControls visibleControls;
524 void initStyleOption(QStyleOptionComplex *option) const;
525 inline QStyle::SubControl getSubControl(const QPoint &pos) const
526 {
527 QStyleOptionComplex opt;
528 initStyleOption(&opt);
529 return style()->hitTestComplexControl(QStyle::CC_MdiControls, &opt, pos, this);
530 }
531};
532} // namespace QMdi
533
534/*
535 \internal
536*/
537ControllerWidget::ControllerWidget(QWidget *parent)
538 : QWidget(parent),
539 activeControl(QStyle::SC_None),
540 hoverControl(QStyle::SC_None),
541 visibleControls(QStyle::SC_None)
542{
543 setFocusPolicy(Qt::NoFocus);
544 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
545 setMouseTracking(true);
546}
547
548/*
549 \internal
550*/
552{
553 ensurePolished();
554 QStyleOptionComplex opt;
555 initStyleOption(&opt);
556 const int buttonSize = style()->pixelMetric(QStyle::PM_TitleBarButtonSize, &opt, this);
557 QSize size(3 * buttonSize, buttonSize);
558 return style()->sizeFromContents(QStyle::CT_MdiControls, &opt, size, this);
559}
560
561void ControllerWidget::setControlVisible(QMdiSubWindowPrivate::WindowStateAction action, bool visible)
562{
563 QStyle::SubControl subControl = QStyle::SC_None;
564
565 // Map action from QMdiSubWindowPrivate::WindowStateAction to QStyle::SubControl.
566 if (action == QMdiSubWindowPrivate::MaximizeAction)
567 subControl = QStyle::SC_MdiNormalButton;
568 else if (action == QMdiSubWindowPrivate::CloseAction)
569 subControl = QStyle::SC_MdiCloseButton;
570 else if (action == QMdiSubWindowPrivate::MinimizeAction)
571 subControl = QStyle::SC_MdiMinButton;
572
573 if (subControl == QStyle::SC_None)
574 return;
575
576 visibleControls.setFlag(subControl, visible && !(visibleControls & subControl));
577}
578
579/*
580 \internal
581*/
582void ControllerWidget::paintEvent(QPaintEvent * /*paintEvent*/)
583{
584 QStyleOptionComplex opt;
585 initStyleOption(&opt);
586 if (activeControl == hoverControl) {
587 opt.activeSubControls = activeControl;
588 opt.state |= QStyle::State_Sunken;
589 } else if (hoverControl != QStyle::SC_None && (activeControl == QStyle::SC_None)) {
590 opt.activeSubControls = hoverControl;
591 opt.state |= QStyle::State_MouseOver;
592 }
593 QPainter painter(this);
594 style()->drawComplexControl(QStyle::CC_MdiControls, &opt, &painter, this);
595}
596
597/*
598 \internal
599*/
600void ControllerWidget::mousePressEvent(QMouseEvent *event)
601{
602 if (event->button() != Qt::LeftButton) {
603 event->ignore();
604 return;
605 }
606 activeControl = getSubControl(event->position().toPoint());
607 update();
608}
609
610/*
611 \internal
612*/
613void ControllerWidget::mouseReleaseEvent(QMouseEvent *event)
614{
615 if (event->button() != Qt::LeftButton) {
616 event->ignore();
617 return;
618 }
619
620 QStyle::SubControl under_mouse = getSubControl(event->position().toPoint());
621 if (under_mouse == activeControl) {
622 switch (activeControl) {
623 case QStyle::SC_MdiCloseButton:
624 emit _q_close();
625 break;
626 case QStyle::SC_MdiNormalButton:
627 emit _q_restore();
628 break;
629 case QStyle::SC_MdiMinButton:
630 emit _q_minimize();
631 break;
632 default:
633 break;
634 }
635 }
636
637 activeControl = QStyle::SC_None;
638 update();
639}
640
641/*
642 \internal
643*/
644void ControllerWidget::mouseMoveEvent(QMouseEvent *event)
645{
646 QStyle::SubControl under_mouse = getSubControl(event->position().toPoint());
647 //test if hover state changes
648 if (hoverControl != under_mouse) {
649 hoverControl = under_mouse;
650 update();
651 }
652}
653
654/*
655 \internal
656*/
657void ControllerWidget::leaveEvent(QEvent * /*event*/)
658{
659 hoverControl = QStyle::SC_None;
660 update();
661}
662
663/*
664 \internal
665*/
666bool ControllerWidget::event(QEvent *event)
667{
668#if QT_CONFIG(tooltip)
669 if (event->type() == QEvent::ToolTip) {
670 QStyleOptionComplex opt;
671 initStyleOption(&opt);
672 QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
673 showToolTip(helpEvent, this, opt, QStyle::CC_MdiControls, getSubControl(helpEvent->pos()));
674 }
675#endif // QT_CONFIG(tooltip)
676 return QWidget::event(event);
677}
678
679/*
680 \internal
681*/
682void ControllerWidget::initStyleOption(QStyleOptionComplex *option) const
683{
684 option->initFrom(this);
685 option->subControls = visibleControls;
686 option->activeSubControls = QStyle::SC_None;
687}
688
689/*
690 \internal
691*/
692ControlContainer::ControlContainer(QMdiSubWindow *mdiChild)
693 : QObject(mdiChild),
694 previousLeft(nullptr),
695 previousRight(nullptr),
696#if QT_CONFIG(menubar)
697 m_menuBar(nullptr),
698#endif
700{
701 Q_ASSERT(mdiChild);
702
703 m_controllerWidget = new ControlElement<ControllerWidget>(mdiChild);
704 connect(m_controllerWidget, SIGNAL(_q_close()), mdiChild, SLOT(close()));
705 connect(m_controllerWidget, SIGNAL(_q_restore()), mdiChild, SLOT(showNormal()));
706 connect(m_controllerWidget, SIGNAL(_q_minimize()), mdiChild, SLOT(showMinimized()));
707
708 m_menuLabel = new ControlElement<ControlLabel>(mdiChild);
709 m_menuLabel->setWindowIcon(mdiChild->windowIcon());
710#if QT_CONFIG(menu)
711 connect(m_menuLabel, SIGNAL(_q_clicked()), mdiChild, SLOT(showSystemMenu()));
712#endif
713 connect(m_menuLabel, SIGNAL(_q_doubleClicked()), mdiChild, SLOT(close()));
714}
715
717{
718#if QT_CONFIG(menubar)
719 removeButtonsFromMenuBar();
720#endif
721 delete m_menuLabel;
722 m_menuLabel = nullptr;
723 delete m_controllerWidget;
724 m_controllerWidget = nullptr;
725}
726
727#if QT_CONFIG(menubar)
728/*
729 \internal
730*/
731QMenuBar *QMdiSubWindowPrivate::menuBar() const
732{
733#if !QT_CONFIG(mainwindow)
734 return nullptr;
735#else
736 Q_Q(const QMdiSubWindow);
737 if (!q->isMaximized() || drawTitleBarWhenMaximized() || isChildOfTabbedQMdiArea(q))
738 return nullptr;
739
740 if (QMainWindow *mainWindow = qobject_cast<QMainWindow *>(q->window()))
741 return mainWindow->menuBar();
742
743 return nullptr;
744#endif
745}
746
747/*
748 \internal
749*/
750void ControlContainer::showButtonsInMenuBar(QMenuBar *menuBar)
751{
752 if (!menuBar || !mdiChild || mdiChild->windowFlags() & Qt::FramelessWindowHint)
753 return;
754 m_menuBar = menuBar;
755
756 if (m_menuLabel && mdiChild->windowFlags() & Qt::WindowSystemMenuHint) {
757 QWidget *currentLeft = menuBar->cornerWidget(Qt::TopLeftCorner);
758 if (currentLeft)
759 currentLeft->hide();
760 if (currentLeft != m_menuLabel) {
761 menuBar->setCornerWidget(m_menuLabel, Qt::TopLeftCorner);
762 previousLeft = currentLeft;
763 }
764 m_menuLabel->show();
765 }
766 ControllerWidget *controllerWidget = qobject_cast<ControllerWidget *>(m_controllerWidget);
767 if (controllerWidget && controllerWidget->hasVisibleControls()) {
768 QWidget *currentRight = menuBar->cornerWidget(Qt::TopRightCorner);
769 if (currentRight)
770 currentRight->hide();
771 if (currentRight != m_controllerWidget) {
772 menuBar->setCornerWidget(m_controllerWidget, Qt::TopRightCorner);
773 previousRight = currentRight;
774 }
775 m_controllerWidget->show();
776 }
777 mdiChild->d_func()->setNewWindowTitle();
778}
779
780/*
781 \internal
782*/
783void ControlContainer::removeButtonsFromMenuBar(QMenuBar *menuBar)
784{
785 if (menuBar && menuBar != m_menuBar) {
786 // m_menubar was deleted while sub-window was maximized
787 previousRight = nullptr;
788 previousLeft = nullptr;
789 m_menuBar = menuBar;
790 }
791
792 if (!m_menuBar || !mdiChild || qt_widget_private(mdiChild->window())->data.in_destructor)
793 return;
794
795 QMdiSubWindow *child = nullptr;
796 if (m_controllerWidget) {
797 QWidget *currentRight = m_menuBar->cornerWidget(Qt::TopRightCorner);
798 if (currentRight == m_controllerWidget) {
799 if (ControlElement<ControllerWidget> *ce = ptr<ControllerWidget>(previousRight)) {
800 if (!ce->mdiChild || !ce->mdiChild->isMaximized())
801 previousRight = nullptr;
802 else
803 child = ce->mdiChild;
804 }
805 m_menuBar->setCornerWidget(previousRight, Qt::TopRightCorner);
806 if (previousRight) {
807 previousRight->show();
808 previousRight = nullptr;
809 }
810 }
811 m_controllerWidget->hide();
812 m_controllerWidget->setParent(nullptr);
813 }
814 if (m_menuLabel) {
815 QWidget *currentLeft = m_menuBar->cornerWidget(Qt::TopLeftCorner);
816 if (currentLeft == m_menuLabel) {
817 if (ControlElement<ControlLabel> *ce = ptr<ControlLabel>(previousLeft)) {
818 if (!ce->mdiChild || !ce->mdiChild->isMaximized())
819 previousLeft = nullptr;
820 else if (!child)
821 child = mdiChild;
822 }
823 m_menuBar->setCornerWidget(previousLeft, Qt::TopLeftCorner);
824 if (previousLeft) {
825 previousLeft->show();
826 previousLeft = nullptr;
827 }
828 }
829 m_menuLabel->hide();
830 m_menuLabel->setParent(nullptr);
831 }
832 m_menuBar->update();
833 if (child)
834 child->d_func()->setNewWindowTitle();
835 else if (mdiChild)
836 mdiChild->window()->setWindowTitle(mdiChild->d_func()->originalWindowTitle());
837}
838
839#endif // QT_CONFIG(menubar)
840
841void ControlContainer::updateWindowIcon(const QIcon &windowIcon)
842{
843 if (m_menuLabel)
844 m_menuLabel->setWindowIcon(windowIcon);
845}
846
847/*!
848 \internal
849*/
850QMdiSubWindowPrivate::QMdiSubWindowPrivate()
851 : baseWidget(nullptr),
852 restoreFocusWidget(nullptr),
853 controlContainer(nullptr),
854#if QT_CONFIG(sizegrip)
855 sizeGrip(nullptr),
856#endif
857#if QT_CONFIG(rubberband)
858 rubberBand(nullptr),
859#endif
860 userMinimumSize(0,0),
861 resizeEnabled(true),
862 moveEnabled(true),
863 isInInteractiveMode(false),
864#if QT_CONFIG(rubberband)
865 isInRubberBandMode(false),
866#endif
867 isShadeMode(false),
868 ignoreWindowTitleChange(false),
869 ignoreNextActivationEvent(false),
870 activationEnabled(true),
871 isShadeRequestFromMinimizeMode(false),
872 isMaximizeMode(false),
873 isWidgetHiddenByUs(false),
874 isActive(false),
875 isExplicitlyDeactivated(false),
876 keyboardSingleStep(5),
877 keyboardPageStep(20),
878 hoveredSubControl(QStyle::SC_None),
879 activeSubControl(QStyle::SC_None),
880 focusInReason(Qt::ActiveWindowFocusReason)
881{
882 initOperationMap();
883}
884
885/*!
886 \internal
887*/
888void QMdiSubWindowPrivate::_q_updateStaysOnTopHint()
889{
890#if QT_CONFIG(action)
891 Q_Q(QMdiSubWindow);
892 if (QAction *senderAction = qobject_cast<QAction *>(q->sender())) {
893 if (senderAction->isChecked()) {
894 q->setWindowFlags(q->windowFlags() | Qt::WindowStaysOnTopHint);
895 q->raise();
896 } else {
897 q->setWindowFlags(q->windowFlags() & ~Qt::WindowStaysOnTopHint);
898 q->lower();
899 }
900 }
901#endif // QT_CONFIG(action)
902}
903
904/*!
905 \internal
906*/
907void QMdiSubWindowPrivate::_q_enterInteractiveMode()
908{
909#if QT_CONFIG(action)
910 Q_Q(QMdiSubWindow);
911 QAction *action = qobject_cast<QAction *>(q->sender());
912 if (!action)
913 return;
914
915 QPoint pressPos;
916 if (actions[MoveAction] && actions[MoveAction] == action) {
917 currentOperation = Move;
918 pressPos = QPoint(q->width() / 2, titleBarHeight() - 1);
919 } else if (actions[ResizeAction] && actions[ResizeAction] == action) {
920 currentOperation = q->isLeftToRight() ? BottomRightResize : BottomLeftResize;
921 int offset = q->style()->pixelMetric(QStyle::PM_MdiSubWindowFrameWidth, nullptr, q) / 2;
922 int x = q->isLeftToRight() ? q->width() - offset : offset;
923 pressPos = QPoint(x, q->height() - offset);
924 } else {
925 return;
926 }
927
928 updateCursor();
929#ifndef QT_NO_CURSOR
930 q->cursor().setPos(q->mapToGlobal(pressPos));
931#endif
932 mousePressPosition = q->mapToParent(pressPos);
933 oldGeometry = q->geometry();
934 isInInteractiveMode = true;
935 q->setFocus();
936#if QT_CONFIG(rubberband)
937 if ((q->testOption(QMdiSubWindow::RubberBandResize)
938 && (currentOperation == BottomRightResize || currentOperation == BottomLeftResize))
939 || (q->testOption(QMdiSubWindow::RubberBandMove) && currentOperation == Move)) {
940 enterRubberBandMode();
941 } else
942#endif // QT_CONFIG(rubberband)
943 {
944 q->grabMouse();
945 }
946#endif // QT_NO_ACTION
947}
948
949/*!
950 \internal
951*/
952void QMdiSubWindowPrivate::_q_processFocusChanged(QWidget *old, QWidget *now)
953{
954 Q_UNUSED(old);
955 Q_Q(QMdiSubWindow);
956 if (now && (now == q || q->isAncestorOf(now))) {
957 if (now == q && !isInInteractiveMode)
958 setFocusWidget();
959 setActive(true);
960 }
961}
962
963/*!
964 \internal
965*/
966void QMdiSubWindowPrivate::leaveInteractiveMode()
967{
968 Q_Q(QMdiSubWindow);
969#if QT_CONFIG(rubberband)
970 if (isInRubberBandMode)
971 leaveRubberBandMode();
972 else
973#endif
974 q->releaseMouse();
975 isInInteractiveMode = false;
976 currentOperation = std::nullopt;
977 updateDirtyRegions();
978 updateCursor();
979 if (baseWidget && baseWidget->focusWidget())
980 baseWidget->focusWidget()->setFocus();
981}
982
983/*!
984 \internal
985*/
986void QMdiSubWindowPrivate::removeBaseWidget()
987{
988 if (!baseWidget)
989 return;
990
991 Q_Q(QMdiSubWindow);
992 baseWidget->removeEventFilter(q);
993 if (layout)
994 layout->removeWidget(baseWidget);
995 if (baseWidget->windowTitle() == q->windowTitle()) {
996 ignoreWindowTitleChange = true;
997 q->setWindowTitle(QString());
998 ignoreWindowTitleChange = false;
999 q->setWindowModified(false);
1000 }
1001 lastChildWindowTitle.clear();
1002 // QTBUG-47993: parent widget can be reset before this call
1003 if (baseWidget->parentWidget() == q)
1004 baseWidget->setParent(nullptr);
1005 baseWidget = nullptr;
1006 isWidgetHiddenByUs = false;
1007}
1008
1009/*!
1010 \internal
1011
1012 This is only a function to avoid having to scope all the identifiers
1013 manually.
1014*/
1015constexpr auto QMdiSubWindowPrivate::initOperationMap() -> OperationInfoMap
1016{
1017 return {{{
1018 { Move, {HMove | VMove, Qt::ArrowCursor} },
1019 { TopResize, {VMove | VResize | VResizeReverse, Qt::SizeVerCursor} },
1020 { BottomResize, {VResize, Qt::SizeVerCursor} },
1021 { LeftResize, {HMove | HResize | HResizeReverse, Qt::SizeHorCursor} },
1022 { RightResize, {HResize, Qt::SizeHorCursor} },
1023 { TopLeftResize, {HMove | VMove | HResize | VResize | VResizeReverse | HResizeReverse,
1024 Qt::SizeFDiagCursor} },
1025 { TopRightResize, {VMove | HResize | VResize | VResizeReverse,
1026 Qt::SizeBDiagCursor} },
1027 { BottomLeftResize, {HMove | HResize | VResize | HResizeReverse,
1028 Qt::SizeBDiagCursor} },
1029 { BottomRightResize, {HResize | VResize, Qt::SizeFDiagCursor} },
1030 }}};
1031}
1032
1033constexpr auto operationMap = QMdiSubWindowPrivate::initOperationMap();
1034
1035#if QT_CONFIG(menu)
1036
1037/*!
1038 \internal
1039*/
1040void QMdiSubWindowPrivate::createSystemMenu()
1041{
1042 Q_Q(QMdiSubWindow);
1043 Q_ASSERT_X(q, "QMdiSubWindowPrivate::createSystemMenu",
1044 "You can NOT call this function before QMdiSubWindow's ctor");
1045 systemMenu = new QMenu(q);
1046 systemMenu->installEventFilter(q);
1047 const QStyle *style = q->style();
1048 addToSystemMenu(RestoreAction, QMdiSubWindow::tr("&Restore"), SLOT(showNormal()));
1049 actions[RestoreAction]->setIcon(style->standardIcon(QStyle::SP_TitleBarNormalButton, nullptr, q));
1050 actions[RestoreAction]->setEnabled(false);
1051 addToSystemMenu(MoveAction, QMdiSubWindow::tr("&Move"), SLOT(_q_enterInteractiveMode()));
1052 addToSystemMenu(ResizeAction, QMdiSubWindow::tr("&Size"), SLOT(_q_enterInteractiveMode()));
1053 addToSystemMenu(MinimizeAction, QMdiSubWindow::tr("Mi&nimize"), SLOT(showMinimized()));
1054 actions[MinimizeAction]->setIcon(style->standardIcon(QStyle::SP_TitleBarMinButton, nullptr, q));
1055 addToSystemMenu(MaximizeAction, QMdiSubWindow::tr("Ma&ximize"), SLOT(showMaximized()));
1056 actions[MaximizeAction]->setIcon(style->standardIcon(QStyle::SP_TitleBarMaxButton, nullptr, q));
1057 addToSystemMenu(StayOnTopAction, QMdiSubWindow::tr("Stay on &Top"), SLOT(_q_updateStaysOnTopHint()));
1058 actions[StayOnTopAction]->setCheckable(true);
1059 systemMenu->addSeparator();
1060 addToSystemMenu(CloseAction, QMdiSubWindow::tr("&Close"), SLOT(close()));
1061 actions[CloseAction]->setIcon(style->standardIcon(QStyle::SP_TitleBarCloseButton, nullptr, q));
1062#if !defined(QT_NO_SHORTCUT)
1063 actions[CloseAction]->setShortcuts(QKeySequence::Close);
1064#endif
1065 updateActions();
1066}
1067#endif
1068
1069/*!
1070 \internal
1071*/
1072void QMdiSubWindowPrivate::updateCursor()
1073{
1074#ifndef QT_NO_CURSOR
1075 Q_Q(QMdiSubWindow);
1076 if (isMacStyle(q->style()))
1077 return;
1078
1079 if (currentOperation)
1080 q->setCursor(operationMap[*currentOperation].cursorShape());
1081 else
1082 q->unsetCursor();
1083#endif
1084}
1085
1086/*!
1087 \internal
1088*/
1089void QMdiSubWindowPrivate::updateDirtyRegions()
1090{
1091 // No update necessary
1092 if (!parent)
1093 return;
1094
1095 size_t i = 0;
1096 for (QRegion &r : regions)
1097 r = getRegion(Operation(i++));
1098}
1099
1100/*!
1101 \internal
1102*/
1103void QMdiSubWindowPrivate::updateGeometryConstraints()
1104{
1105 Q_Q(QMdiSubWindow);
1106 if (!parent)
1107 return;
1108
1109 internalMinimumSize = (!q->isMinimized() && !q->minimumSize().isNull())
1110 ? q->minimumSize() : q->minimumSizeHint();
1111 int margin, minWidth;
1112 sizeParameters(&margin, &minWidth);
1113 q->setContentsMargins(margin, titleBarHeight(), margin, margin);
1114 if (q->isMaximized() || (q->isMinimized() && !q->isShaded())) {
1115 moveEnabled = false;
1116 resizeEnabled = false;
1117 } else {
1118 moveEnabled = true;
1119 if ((q->windowFlags() & Qt::MSWindowsFixedSizeDialogHint) || q->isShaded())
1120 resizeEnabled = false;
1121 else
1122 resizeEnabled = true;
1123 }
1124 updateDirtyRegions();
1125}
1126
1127/*!
1128 \internal
1129*/
1130void QMdiSubWindowPrivate::updateMask()
1131{
1132 Q_Q(QMdiSubWindow);
1133 if (!q->mask().isEmpty())
1134 q->clearMask();
1135
1136 if (!parent)
1137 return;
1138
1139 if ((q->isMaximized() && !drawTitleBarWhenMaximized())
1140 || q->windowFlags() & Qt::FramelessWindowHint)
1141 return;
1142
1143 if (!resizeTimer.isActive())
1144 cachedStyleOptions = titleBarOptions();
1145 cachedStyleOptions.rect = q->rect();
1146 QStyleHintReturnMask frameMask;
1147 q->style()->styleHint(QStyle::SH_WindowFrame_Mask, &cachedStyleOptions, q, &frameMask);
1148 if (!frameMask.region.isEmpty())
1149 q->setMask(frameMask.region);
1150}
1151
1152/*!
1153 \internal
1154*/
1155void QMdiSubWindowPrivate::setNewGeometry(const QPoint &pos)
1156{
1157 Q_Q(QMdiSubWindow);
1158 Q_ASSERT(currentOperation);
1159 Q_ASSERT(parent);
1160
1161 const uint cflags = operationMap[*currentOperation].changeFlags;
1162 int posX = pos.x();
1163 int posY = pos.y();
1164
1165 const bool restrictHorizontal = !q->testOption(QMdiSubWindow::AllowOutsideAreaHorizontally);
1166 const bool restrictVertical = !q->testOption(QMdiSubWindow::AllowOutsideAreaVertically);
1167
1168 if (restrictHorizontal || restrictVertical) {
1169 QRect parentRect = q->parentWidget()->rect();
1170 if (restrictVertical && (cflags & VResizeReverse || currentOperation == Move)) {
1171 posY = qMin(qMax(mousePressPosition.y() - oldGeometry.y(), posY),
1172 parentRect.height() - BoundaryMargin);
1173 }
1174 if (currentOperation == Move) {
1175 if (restrictHorizontal)
1176 posX = qMin(qMax(BoundaryMargin, posX), parentRect.width() - BoundaryMargin);
1177 if (restrictVertical)
1178 posY = qMin(posY, parentRect.height() - BoundaryMargin);
1179 } else {
1180 if (restrictHorizontal) {
1181 if (cflags & HResizeReverse)
1182 posX = qMax(mousePressPosition.x() - oldGeometry.x(), posX);
1183 else
1184 posX = qMin(parentRect.width() - (oldGeometry.x() + oldGeometry.width()
1185 - mousePressPosition.x()), posX);
1186 }
1187 if (restrictVertical && !(cflags & VResizeReverse)) {
1188 posY = qMin(parentRect.height() - (oldGeometry.y() + oldGeometry.height()
1189 - mousePressPosition.y()), posY);
1190 }
1191 }
1192 }
1193
1194 QRect geometry;
1195 if (cflags & (HMove | VMove)) {
1196 int dx = getMoveDeltaComponent(cflags, HMove, HResize, posX - mousePressPosition.x(),
1197 oldGeometry.width() - internalMinimumSize.width(),
1198 oldGeometry.width() - q->maximumWidth());
1199 int dy = getMoveDeltaComponent(cflags, VMove, VResize, posY - mousePressPosition.y(),
1200 oldGeometry.height() - internalMinimumSize.height(),
1201 oldGeometry.height() - q->maximumHeight());
1202 geometry.setTopLeft(oldGeometry.topLeft() + QPoint(dx, dy));
1203 } else {
1204 geometry.setTopLeft(q->geometry().topLeft());
1205 }
1206
1207 if (cflags & (HResize | VResize)) {
1208 int dx = getResizeDeltaComponent(cflags, HResize, HResizeReverse,
1209 posX - mousePressPosition.x());
1210 int dy = getResizeDeltaComponent(cflags, VResize, VResizeReverse,
1211 posY - mousePressPosition.y());
1212 geometry.setSize(oldGeometry.size() + QSize(dx, dy));
1213 } else {
1214 geometry.setSize(q->geometry().size());
1215 }
1216
1217 setNewGeometry(&geometry);
1218}
1219
1220/*!
1221 \internal
1222*/
1223void QMdiSubWindowPrivate::setMinimizeMode()
1224{
1225 Q_Q(QMdiSubWindow);
1226 Q_ASSERT(parent);
1227
1228 ensureWindowState(Qt::WindowMinimized);
1229 isShadeRequestFromMinimizeMode = true;
1230 q->showShaded();
1231 isShadeRequestFromMinimizeMode = false;
1232
1233 moveEnabled = false;
1234#if QT_CONFIG(action)
1235 setEnabled(MoveAction, moveEnabled);
1236#endif
1237
1238 Q_ASSERT(q->windowState() & Qt::WindowMinimized);
1239 Q_ASSERT(!(q->windowState() & Qt::WindowMaximized));
1240 // This should be a valid assert, but people can actually re-implement
1241 // setVisible and do crazy stuff, so we're not guaranteed that
1242 // the widget is hidden after calling hide().
1243 // Q_ASSERT(baseWidget ? baseWidget->isHidden() : true);
1244
1245 setActive(true);
1246}
1247
1248/*!
1249 \internal
1250*/
1251void QMdiSubWindowPrivate::setNormalMode()
1252{
1253 Q_Q(QMdiSubWindow);
1254 Q_ASSERT(parent);
1255
1256 isShadeMode = false;
1257 isMaximizeMode = false;
1258
1259 ensureWindowState(Qt::WindowNoState);
1260#if QT_CONFIG(menubar)
1261 removeButtonsFromMenuBar();
1262#endif
1263
1264 // Hide the window before we change the geometry to avoid multiple resize
1265 // events and wrong window state.
1266 const bool wasVisible = q->isVisible();
1267 if (wasVisible)
1268 q->setVisible(false);
1269
1270 // Restore minimum size if set by user.
1271 if (!userMinimumSize.isNull()) {
1272 q->setMinimumSize(userMinimumSize);
1273 userMinimumSize = QSize(0, 0);
1274 }
1275
1276 // Show the internal widget if it was hidden by us,
1277 if (baseWidget && isWidgetHiddenByUs) {
1278 baseWidget->show();
1279 isWidgetHiddenByUs = false;
1280 }
1281
1282 updateGeometryConstraints();
1283 QRect newGeometry = oldGeometry;
1284 newGeometry.setSize(restoreSize.expandedTo(internalMinimumSize));
1285 q->setGeometry(newGeometry);
1286
1287 if (wasVisible)
1288 q->setVisible(true);
1289
1290 // Invalidate the restore size.
1291 restoreSize.setWidth(-1);
1292 restoreSize.setHeight(-1);
1293
1294#if QT_CONFIG(sizegrip)
1295 setSizeGripVisible(true);
1296#endif
1297
1298#if QT_CONFIG(action)
1299 setEnabled(MoveAction, true);
1300 setEnabled(MaximizeAction, true);
1301 setEnabled(MinimizeAction, true);
1302 setEnabled(RestoreAction, false);
1303 setEnabled(ResizeAction, resizeEnabled);
1304#endif // QT_NO_ACTION
1305
1306 Q_ASSERT(!(q_func()->windowState() & Qt::WindowMinimized));
1307 // This sub-window can be maximized when shown above if not the
1308 // QMdiArea::DontMaximizeSubWindowOnActionvation is set. Make sure
1309 // the Qt::WindowMaximized flag is set accordingly.
1310 Q_ASSERT((isMaximizeMode && q_func()->windowState() & Qt::WindowMaximized)
1311 || (!isMaximizeMode && !(q_func()->windowState() & Qt::WindowMaximized)));
1312 Q_ASSERT(!isShadeMode);
1313
1314 setActive(true);
1315 restoreFocus();
1316 updateMask();
1317}
1318
1319inline void QMdiSubWindowPrivate::storeFocusWidget()
1320{
1321 if (QWidget *focus = QApplication::focusWidget()) {
1322 if (!restoreFocusWidget && q_func()->isAncestorOf(focus))
1323 restoreFocusWidget = focus;
1324 }
1325}
1326
1327/*!
1328 \internal
1329*/
1330void QMdiSubWindowPrivate::setMaximizeMode()
1331{
1332 Q_Q(QMdiSubWindow);
1333 Q_ASSERT(parent);
1334
1335 ensureWindowState(Qt::WindowMaximized);
1336 isShadeMode = false;
1337 isMaximizeMode = true;
1338
1339 storeFocusWidget();
1340
1341#if QT_CONFIG(sizegrip)
1342 setSizeGripVisible(false);
1343#endif
1344
1345 // Store old geometry and set restore size if not already set.
1346 if (!restoreSize.isValid()) {
1347 oldGeometry = q->geometry();
1348 restoreSize.setWidth(oldGeometry.width());
1349 restoreSize.setHeight(oldGeometry.height());
1350 }
1351
1352 // Hide the window before we change the geometry to avoid multiple resize
1353 // events and wrong window state.
1354 const bool wasVisible = q->isVisible();
1355 if (wasVisible)
1356 q->setVisible(false);
1357
1358 // Show the internal widget if it was hidden by us.
1359 if (baseWidget && isWidgetHiddenByUs) {
1360 baseWidget->show();
1361 isWidgetHiddenByUs = false;
1362 }
1363
1364 updateGeometryConstraints();
1365
1366 if (wasVisible) {
1367#if QT_CONFIG(menubar)
1368 if (QMenuBar *mBar = menuBar())
1369 showButtonsInMenuBar(mBar);
1370 else
1371#endif
1372 if (!controlContainer)
1373 controlContainer = new ControlContainer(q);
1374 }
1375
1376 QWidget *parent = q->parentWidget();
1377 QRect availableRect = parent->contentsRect();
1378
1379 // Adjust geometry if the sub-window is inside a scroll area.
1380 QAbstractScrollArea *scrollArea = qobject_cast<QAbstractScrollArea *>(parent->parentWidget());
1381 if (scrollArea && scrollArea->viewport() == parent) {
1382 QScrollBar *hbar = scrollArea->horizontalScrollBar();
1383 QScrollBar *vbar = scrollArea->verticalScrollBar();
1384 const int xOffset = hbar ? hbar->value() : 0;
1385 const int yOffset = vbar ? vbar->value() : 0;
1386 availableRect.adjust(-xOffset, -yOffset, -xOffset, -yOffset);
1387 oldGeometry.adjust(xOffset, yOffset, xOffset, yOffset);
1388 }
1389
1390 setNewGeometry(&availableRect);
1391 // QWidget::setGeometry will reset Qt::WindowMaximized so we have to update it here.
1392 ensureWindowState(Qt::WindowMaximized);
1393
1394 if (wasVisible)
1395 q->setVisible(true);
1396
1397 resizeEnabled = false;
1398 moveEnabled = false;
1399
1400#if QT_CONFIG(action)
1401 setEnabled(MoveAction, moveEnabled);
1402 setEnabled(MaximizeAction, false);
1403 setEnabled(MinimizeAction, true);
1404 setEnabled(RestoreAction, true);
1405 setEnabled(ResizeAction, resizeEnabled);
1406#endif // QT_NO_ACTION
1407
1408 Q_ASSERT(q->windowState() & Qt::WindowMaximized);
1409 Q_ASSERT(!(q->windowState() & Qt::WindowMinimized));
1410
1411 restoreFocus();
1412 updateMask();
1413}
1414
1415/*!
1416 \internal
1417*/
1418void QMdiSubWindowPrivate::setActive(bool activate, bool changeFocus)
1419{
1420 Q_Q(QMdiSubWindow);
1421 if (!parent || !activationEnabled)
1422 return;
1423
1424 if (activate && !isActive && q->isEnabled()) {
1425 isActive = true;
1426 isExplicitlyDeactivated = false;
1427 Qt::WindowStates oldWindowState = q->windowState();
1428 ensureWindowState(Qt::WindowActive);
1429 emit q->aboutToActivate();
1430#if QT_CONFIG(menubar)
1431 if (QMenuBar *mBar = menuBar())
1432 showButtonsInMenuBar(mBar);
1433#endif
1434 Q_ASSERT(isActive);
1435 emit q->windowStateChanged(oldWindowState, q->windowState());
1436 } else if (!activate && isActive) {
1437 isActive = false;
1438 Qt::WindowStates oldWindowState = q->windowState();
1439 q->overrideWindowState(q->windowState() & ~Qt::WindowActive);
1440 if (changeFocus) {
1441 storeFocusWidget();
1442 QWidget *focusWidget = QApplication::focusWidget();
1443 if (focusWidget && (focusWidget == q || q->isAncestorOf(focusWidget)))
1444 focusWidget->clearFocus();
1445 }
1446 if (baseWidget)
1447 baseWidget->overrideWindowState(baseWidget->windowState() & ~Qt::WindowActive);
1448 Q_ASSERT(!isActive);
1449 emit q->windowStateChanged(oldWindowState, q->windowState());
1450 }
1451
1452 if (activate && isActive && q->isEnabled() && !q->hasFocus()
1453 && !q->isAncestorOf(QApplication::focusWidget())) {
1454 if (changeFocus)
1455 setFocusWidget();
1456 ensureWindowState(Qt::WindowActive);
1457 }
1458
1459 int frameWidth = q->style()->pixelMetric(QStyle::PM_MdiSubWindowFrameWidth, nullptr, q);
1460 int titleBarHeight = this->titleBarHeight();
1461 QRegion windowDecoration = QRegion(0, 0, q->width(), q->height());
1462 windowDecoration -= QRegion(frameWidth, titleBarHeight, q->width() - 2 * frameWidth,
1463 q->height() - titleBarHeight - frameWidth);
1464
1465 // Make sure we don't use cached style options if we get
1466 // resize events right before activation/deactivation.
1467 if (resizeTimer.isActive()) {
1468 resizeTimer.stop();
1469 updateDirtyRegions();
1470 }
1471
1472 q->update(windowDecoration);
1473}
1474
1475/*!
1476 \internal
1477*/
1478void QMdiSubWindowPrivate::processClickedSubControl()
1479{
1480 Q_Q(QMdiSubWindow);
1481 switch (activeSubControl) {
1482 case QStyle::SC_TitleBarContextHelpButton:
1483#if QT_CONFIG(whatsthis)
1484 QWhatsThis::enterWhatsThisMode();
1485#endif
1486 break;
1487 case QStyle::SC_TitleBarShadeButton:
1488 q->showShaded();
1489 hoveredSubControl = QStyle::SC_TitleBarUnshadeButton;
1490 break;
1491 case QStyle::SC_TitleBarUnshadeButton:
1492 if (q->isShaded())
1493 hoveredSubControl = QStyle::SC_TitleBarShadeButton;
1494 q->showNormal();
1495 break;
1496 case QStyle::SC_TitleBarMinButton:
1497 if (isMacStyle(q->style())) {
1498 if (q->isMinimized())
1499 q->showNormal();
1500 else
1501 q->showMinimized();
1502 break;
1503 }
1504
1505 q->showMinimized();
1506 break;
1507 case QStyle::SC_TitleBarNormalButton:
1508 if (q->isShaded())
1509 hoveredSubControl = QStyle::SC_TitleBarMinButton;
1510 q->showNormal();
1511 break;
1512 case QStyle::SC_TitleBarMaxButton:
1513 if (isMacStyle(q->style())) {
1514 if (q->isMaximized())
1515 q->showNormal();
1516 else
1517 q->showMaximized();
1518 break;
1519 }
1520
1521 q->showMaximized();
1522 break;
1523 case QStyle::SC_TitleBarCloseButton:
1524 q->close();
1525 break;
1526 default:
1527 break;
1528 }
1529}
1530
1531/*!
1532 \internal
1533*/
1534QRegion QMdiSubWindowPrivate::getRegion(Operation operation) const
1535{
1536 Q_Q(const QMdiSubWindow);
1537 int width = q->width();
1538 int height = q->height();
1539 int titleBarHeight = this->titleBarHeight();
1540 int frameWidth = q->style()->pixelMetric(QStyle::PM_MdiSubWindowFrameWidth, nullptr, q);
1541 int cornerConst = titleBarHeight - frameWidth;
1542 int titleBarConst = 2 * titleBarHeight;
1543
1544 if (operation == Move) {
1545 QStyleOptionTitleBar titleBarOptions = this->titleBarOptions();
1546 QRegion move(frameWidth, frameWidth, width - 2 * frameWidth, cornerConst);
1547 // Depending on which window flags are set, activated sub controllers will
1548 // be subtracted from the 'move' region.
1549 for (int i = 0; i < NumSubControls; ++i) {
1550 if (SubControls[i] == QStyle::SC_TitleBarLabel)
1551 continue;
1552 move -= QRegion(q->style()->subControlRect(QStyle::CC_TitleBar, &titleBarOptions,
1553 SubControls[i]));
1554 }
1555 return move;
1556 }
1557
1558 QRegion region;
1559 if (isMacStyle(q->style()))
1560 return region;
1561
1562 switch (operation) {
1563 case TopResize:
1564 region = QRegion(titleBarHeight, 0, width - titleBarConst, frameWidth);
1565 break;
1566 case BottomResize:
1567 region = QRegion(titleBarHeight, height - frameWidth, width - titleBarConst, frameWidth);
1568 break;
1569 case LeftResize:
1570 region = QRegion(0, titleBarHeight, frameWidth, height - titleBarConst);
1571 break;
1572 case RightResize:
1573 region = QRegion(width - frameWidth, titleBarHeight, frameWidth, height - titleBarConst);
1574 break;
1575 case TopLeftResize:
1576 region = QRegion(0, 0, titleBarHeight, titleBarHeight)
1577 - QRegion(frameWidth, frameWidth, cornerConst, cornerConst);
1578 break;
1579 case TopRightResize:
1580 region = QRegion(width - titleBarHeight, 0, titleBarHeight, titleBarHeight)
1581 - QRegion(width - titleBarHeight, frameWidth, cornerConst, cornerConst);
1582 break;
1583 case BottomLeftResize:
1584 region = QRegion(0, height - titleBarHeight, titleBarHeight, titleBarHeight)
1585 - QRegion(frameWidth, height - titleBarHeight, cornerConst, cornerConst);
1586 break;
1587 case BottomRightResize:
1588 region = QRegion(width - titleBarHeight, height - titleBarHeight, titleBarHeight, titleBarHeight)
1589 - QRegion(width - titleBarHeight, height - titleBarHeight, cornerConst, cornerConst);
1590 break;
1591 case Operation::Move: // handled above
1592 Q_UNREACHABLE();
1593 }
1594
1595 return region;
1596}
1597
1598/*!
1599 \internal
1600*/
1601auto QMdiSubWindowPrivate::getOperation(QPoint pos) const -> std::optional<Operation>
1602{
1603 size_t i = 0;
1604 for (const QRegion &r : regions) {
1605 if (r.contains(pos))
1606 return Operation(i);
1607 ++i;
1608 }
1609 return std::nullopt;
1610}
1611
1612extern QString qt_setWindowTitle_helperHelper(const QString&, const QWidget*);
1613
1614/*!
1615 \internal
1616*/
1617QStyleOptionTitleBar QMdiSubWindowPrivate::titleBarOptions() const
1618{
1619 Q_Q(const QMdiSubWindow);
1620 QStyleOptionTitleBar titleBarOptions;
1621 titleBarOptions.initFrom(q);
1622 if (activeSubControl != QStyle::SC_None) {
1623 if (hoveredSubControl == activeSubControl) {
1624 titleBarOptions.state |= QStyle::State_Sunken;
1625 titleBarOptions.activeSubControls = activeSubControl;
1626 }
1627 } else if (autoRaise() && hoveredSubControl != QStyle::SC_None
1628 && hoveredSubControl != QStyle::SC_TitleBarLabel) {
1629 titleBarOptions.state |= QStyle::State_MouseOver;
1630 titleBarOptions.activeSubControls = hoveredSubControl;
1631 } else {
1632 titleBarOptions.state &= ~QStyle::State_MouseOver;
1633 titleBarOptions.activeSubControls = QStyle::SC_None;
1634 }
1635
1636 titleBarOptions.subControls = QStyle::SC_All;
1637 titleBarOptions.titleBarFlags = q->windowFlags();
1638 titleBarOptions.titleBarState = q->windowState();
1639 titleBarOptions.palette = titleBarPalette;
1640 titleBarOptions.icon = menuIcon;
1641
1642 if (isActive) {
1643 titleBarOptions.state |= QStyle::State_Active;
1644 titleBarOptions.titleBarState |= QStyle::State_Active;
1645 titleBarOptions.palette.setCurrentColorGroup(QPalette::Active);
1646 } else {
1647 titleBarOptions.state &= ~QStyle::State_Active;
1648 titleBarOptions.palette.setCurrentColorGroup(QPalette::Inactive);
1649 }
1650
1651 int border = hasBorder(titleBarOptions) ? 4 : 0;
1652 int paintHeight = titleBarHeight(titleBarOptions);
1653 paintHeight -= q->isMinimized() ? 2 * border : border;
1654 titleBarOptions.rect = QRect(border, border, q->width() - 2 * border, paintHeight);
1655
1656 if (!windowTitle.isEmpty()) {
1657 // Set the text here before asking for the width of the title bar label
1658 // in case people uses the actual text to calculate the width.
1659 titleBarOptions.text = windowTitle;
1660 titleBarOptions.fontMetrics = QFontMetrics(font, q);
1661 int width = q->style()->subControlRect(QStyle::CC_TitleBar, &titleBarOptions,
1662 QStyle::SC_TitleBarLabel, q).width();
1663 // Set elided text if we don't have enough space for the entire title.
1664 titleBarOptions.text = titleBarOptions.fontMetrics.elidedText(windowTitle, Qt::ElideRight, width);
1665 }
1666 return titleBarOptions;
1667}
1668
1669/*!
1670 \internal
1671*/
1672void QMdiSubWindowPrivate::ensureWindowState(Qt::WindowState state)
1673{
1674 Q_Q(QMdiSubWindow);
1675 Qt::WindowStates windowStates = q->windowState() | state;
1676 switch (state) {
1677 case Qt::WindowMinimized:
1678 windowStates &= ~Qt::WindowMaximized;
1679 windowStates &= ~Qt::WindowFullScreen;
1680 windowStates &= ~Qt::WindowNoState;
1681 break;
1682 case Qt::WindowMaximized:
1683 windowStates &= ~Qt::WindowMinimized;
1684 windowStates &= ~Qt::WindowFullScreen;
1685 windowStates &= ~Qt::WindowNoState;
1686 break;
1687 case Qt::WindowNoState:
1688 windowStates &= ~Qt::WindowMinimized;
1689 windowStates &= ~Qt::WindowMaximized;
1690 windowStates &= ~Qt::WindowFullScreen;
1691 break;
1692 default:
1693 break;
1694 }
1695 if (baseWidget) {
1696 if (!(baseWidget->windowState() & Qt::WindowActive) && windowStates & Qt::WindowActive)
1697 baseWidget->overrideWindowState(windowStates & ~Qt::WindowActive);
1698 else
1699 baseWidget->overrideWindowState(windowStates);
1700 }
1701 q->overrideWindowState(windowStates);
1702}
1703
1704/*!
1705 \internal
1706*/
1707int QMdiSubWindowPrivate::titleBarHeight(const QStyleOptionTitleBar &options) const
1708{
1709 Q_Q(const QMdiSubWindow);
1710 if (!parent || q->windowFlags() & Qt::FramelessWindowHint
1711 || (q->isMaximized() && !drawTitleBarWhenMaximized())) {
1712 return 0;
1713 }
1714
1715 int height = q->style()->pixelMetric(QStyle::PM_TitleBarHeight, &options, q);
1716 if (hasBorder(options))
1717 height += q->isMinimized() ? 8 : 4;
1718 return height;
1719}
1720
1721/*!
1722 \internal
1723*/
1724void QMdiSubWindowPrivate::sizeParameters(int *margin, int *minWidth) const
1725{
1726 Q_Q(const QMdiSubWindow);
1727 Qt::WindowFlags flags = q->windowFlags();
1728 if (!parent || flags & Qt::FramelessWindowHint) {
1729 *margin = 0;
1730 *minWidth = 0;
1731 return;
1732 }
1733
1734 if (q->isMaximized() && !drawTitleBarWhenMaximized())
1735 *margin = 0;
1736 else
1737 *margin = q->style()->pixelMetric(QStyle::PM_MdiSubWindowFrameWidth, nullptr, q);
1738
1739 QStyleOptionTitleBar opt = this->titleBarOptions();
1740 int tempWidth = 0;
1741 for (int i = 0; i < NumSubControls; ++i) {
1742 if (SubControls[i] == QStyle::SC_TitleBarLabel) {
1743 tempWidth += 30;
1744 continue;
1745 }
1746 QRect rect = q->style()->subControlRect(QStyle::CC_TitleBar, &opt, SubControls[i], q);
1747 if (!rect.isValid())
1748 continue;
1749 tempWidth += rect.width();
1750 }
1751 *minWidth = tempWidth;
1752}
1753
1754/*!
1755 \internal
1756*/
1757bool QMdiSubWindowPrivate::drawTitleBarWhenMaximized() const
1758{
1759 Q_Q(const QMdiSubWindow);
1760 if (q->window()->testAttribute(Qt::WA_CanHostQMdiSubWindowTitleBar))
1761 return false;
1762
1763 if (isChildOfTabbedQMdiArea(q))
1764 return false;
1765
1766 if (q->style()->styleHint(QStyle::SH_Workspace_FillSpaceOnMaximize, nullptr, q))
1767 return true;
1768#if !QT_CONFIG(menubar) || !QT_CONFIG(mainwindow)
1769 Q_UNUSED(isChildOfQMdiSubWindow);
1770 return true;
1771#else
1772 QMainWindow *mainWindow = qobject_cast<QMainWindow *>(q->window());
1773 if (!mainWindow || !qobject_cast<QMenuBar *>(mainWindow->menuWidget())
1774 || mainWindow->menuWidget()->isHidden())
1775 return true;
1776
1777 return isChildOfQMdiSubWindow(q);
1778#endif
1779}
1780
1781#if QT_CONFIG(menubar)
1782
1783/*!
1784 \internal
1785*/
1786void QMdiSubWindowPrivate::showButtonsInMenuBar(QMenuBar *menuBar)
1787{
1788 Q_Q(QMdiSubWindow);
1789 Q_ASSERT(q->isMaximized() && !drawTitleBarWhenMaximized());
1790
1791 if (isChildOfTabbedQMdiArea(q))
1792 return;
1793
1794 removeButtonsFromMenuBar();
1795 if (!controlContainer)
1796 controlContainer = new ControlContainer(q);
1797
1798 ignoreWindowTitleChange = true;
1799 controlContainer->showButtonsInMenuBar(menuBar);
1800 ignoreWindowTitleChange = false;
1801
1802 QWidget *topLevelWindow = q->window();
1803 topLevelWindow->setWindowModified(q->isWindowModified());
1804 topLevelWindow->installEventFilter(q);
1805
1806 int buttonHeight = 0;
1807 if (controlContainer->controllerWidget())
1808 buttonHeight = controlContainer->controllerWidget()->height();
1809 else if (controlContainer->systemMenuLabel())
1810 buttonHeight = controlContainer->systemMenuLabel()->height();
1811
1812 // This will rarely happen.
1813 if (menuBar && menuBar->height() < buttonHeight
1814 && topLevelWindow->layout()) {
1815 // Make sure topLevelWindow->contentsRect returns correct geometry.
1816 // topLevelWidget->updateGeoemtry will not do the trick here since it will post the event.
1817 QEvent event(QEvent::LayoutRequest);
1818 QCoreApplication::sendEvent(topLevelWindow, &event);
1819 }
1820}
1821
1822/*!
1823 \internal
1824*/
1825void QMdiSubWindowPrivate::removeButtonsFromMenuBar()
1826{
1827 Q_Q(QMdiSubWindow);
1828
1829 if (!controlContainer || isChildOfTabbedQMdiArea(q))
1830 return;
1831
1832 QMenuBar *currentMenuBar = nullptr;
1833#if QT_CONFIG(mainwindow)
1834 if (QMainWindow *mainWindow = qobject_cast<QMainWindow *>(q->window())) {
1835 // NB! We can't use menuBar() here because that one will actually create
1836 // a menubar for us if not set. That's not what we want :-)
1837 currentMenuBar = qobject_cast<QMenuBar *>(mainWindow->menuWidget());
1838 }
1839#endif
1840
1841 ignoreWindowTitleChange = true;
1842 controlContainer->removeButtonsFromMenuBar(currentMenuBar);
1843 ignoreWindowTitleChange = false;
1844
1845 QWidget *topLevelWindow = q->window();
1846 topLevelWindow->removeEventFilter(q);
1847 if (baseWidget && !drawTitleBarWhenMaximized())
1848 topLevelWindow->setWindowModified(false);
1849 originalTitle.clear();
1850}
1851
1852#endif // QT_CONFIG(menubar)
1853
1854void QMdiSubWindowPrivate::updateWindowTitle(bool isRequestFromChild)
1855{
1856 Q_Q(QMdiSubWindow);
1857 if (isRequestFromChild && !q->windowTitle().isEmpty() && !lastChildWindowTitle.isEmpty()
1858 && lastChildWindowTitle != q->windowTitle()) {
1859 return;
1860 }
1861
1862 QWidget *titleWidget = nullptr;
1863 if (isRequestFromChild)
1864 titleWidget = baseWidget;
1865 else
1866 titleWidget = q;
1867 if (!titleWidget || titleWidget->windowTitle().isEmpty())
1868 return;
1869
1870 ignoreWindowTitleChange = true;
1871 q->setWindowTitle(titleWidget->windowTitle());
1872 if (q->maximizedButtonsWidget())
1873 setNewWindowTitle();
1874 ignoreWindowTitleChange = false;
1875}
1876
1877#if QT_CONFIG(rubberband)
1878void QMdiSubWindowPrivate::enterRubberBandMode()
1879{
1880 Q_Q(QMdiSubWindow);
1881 if (q->isMaximized())
1882 return;
1883 Q_ASSERT(oldGeometry.isValid());
1884 Q_ASSERT(parent);
1885 if (!rubberBand) {
1886 rubberBand = new QRubberBand(QRubberBand::Rectangle, q->parentWidget());
1887 // For accessibility to identify this special widget.
1888 rubberBand->setObjectName("qt_rubberband"_L1);
1889 }
1890 QPoint rubberBandPos = q->mapToParent(QPoint(0, 0));
1891 rubberBand->setGeometry(rubberBandPos.x(), rubberBandPos.y(),
1892 oldGeometry.width(), oldGeometry.height());
1893 rubberBand->show();
1894 isInRubberBandMode = true;
1895 q->grabMouse();
1896}
1897
1898void QMdiSubWindowPrivate::leaveRubberBandMode()
1899{
1900 Q_Q(QMdiSubWindow);
1901 Q_ASSERT(rubberBand);
1902 Q_ASSERT(isInRubberBandMode);
1903 q->releaseMouse();
1904 isInRubberBandMode = false;
1905 q->setGeometry(rubberBand->geometry());
1906 rubberBand->hide();
1907 currentOperation = std::nullopt;
1908}
1909#endif // QT_CONFIG(rubberband)
1910
1911// Taken from the old QWorkspace (::readColors())
1912QPalette QMdiSubWindowPrivate::desktopPalette() const
1913{
1914 Q_Q(const QMdiSubWindow);
1915 QPalette newPalette = q->palette();
1916
1917 newPalette.setColor(QPalette::Active, QPalette::Highlight,
1918 newPalette.color(QPalette::Active, QPalette::Highlight));
1919 newPalette.setColor(QPalette::Active, QPalette::Base,
1920 newPalette.color(QPalette::Active, QPalette::Highlight));
1921 newPalette.setColor(QPalette::Inactive, QPalette::Highlight,
1922 newPalette.color(QPalette::Inactive, QPalette::Dark));
1923 newPalette.setColor(QPalette::Inactive, QPalette::Base,
1924 newPalette.color(QPalette::Inactive, QPalette::Dark));
1925 newPalette.setColor(QPalette::Inactive, QPalette::HighlightedText,
1926 newPalette.color(QPalette::Inactive, QPalette::Window));
1927
1928 return newPalette;
1929}
1930
1931void QMdiSubWindowPrivate::updateActions()
1932{
1933 Qt::WindowFlags windowFlags = q_func()->windowFlags();
1934 // Hide all
1935 for (int i = 0; i < NumWindowStateActions; ++i)
1936 setVisible(WindowStateAction(i), false);
1937
1938#if defined(Q_OS_MACOS) && QT_CONFIG(action)
1939 if (q_func()->style()->inherits("QMacStyle"))
1940 for (QAction *action : actions)
1941 if (action)
1942 action->setIconVisibleInMenu(false);
1943#endif
1944
1945 if (windowFlags & Qt::FramelessWindowHint)
1946 return;
1947
1948 setVisible(StayOnTopAction, true);
1949 setVisible(MoveAction, moveEnabled);
1950 setVisible(ResizeAction, resizeEnabled);
1951
1952 // CloseAction
1953 if (windowFlags & Qt::WindowSystemMenuHint)
1954 setVisible(CloseAction, true);
1955
1956 // RestoreAction
1957 if (windowFlags & (Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint))
1958 setVisible(RestoreAction, true);
1959
1960 // MinimizeAction
1961 if (windowFlags & Qt::WindowMinimizeButtonHint)
1962 setVisible(MinimizeAction, true);
1963
1964 // MaximizeAction
1965 if (windowFlags & Qt::WindowMaximizeButtonHint)
1966 setVisible(MaximizeAction, true);
1967}
1968
1969void QMdiSubWindowPrivate::setFocusWidget()
1970{
1971 Q_Q(QMdiSubWindow);
1972 if (!baseWidget) {
1973 q->setFocus();
1974 return;
1975 }
1976
1977 // This will give focus to the next child if possible, otherwise
1978 // do nothing, hence it's not possible to tab between windows with
1979 // just hitting tab (unless Qt::TabFocus is removed from the focus policy).
1980 if (focusInReason == Qt::TabFocusReason) {
1981 q->focusNextChild();
1982 return;
1983 }
1984
1985 // Same as above, but gives focus to the previous child.
1986 if (focusInReason == Qt::BacktabFocusReason) {
1987 q->focusPreviousChild();
1988 return;
1989 }
1990
1991 if (!(q->windowState() & Qt::WindowMinimized) && restoreFocus())
1992 return;
1993
1994 if (QWidget *focusWidget = baseWidget->focusWidget()) {
1995 if (!focusWidget->hasFocus() && q->isAncestorOf(focusWidget)
1996 && focusWidget->isVisible() && !q->isMinimized()
1997 && focusWidget->focusPolicy() != Qt::NoFocus) {
1998 focusWidget->setFocus();
1999 } else {
2000 q->setFocus();
2001 }
2002 return;
2003 }
2004
2005 QWidget *focusWidget = q->nextInFocusChain();
2006 while (focusWidget && focusWidget != q && focusWidget->focusPolicy() == Qt::NoFocus)
2007 focusWidget = focusWidget->nextInFocusChain();
2008 if (focusWidget && q->isAncestorOf(focusWidget))
2009 focusWidget->setFocus();
2010 else if (baseWidget->focusPolicy() != Qt::NoFocus)
2011 baseWidget->setFocus();
2012 else if (!q->hasFocus())
2013 q->setFocus();
2014}
2015
2016bool QMdiSubWindowPrivate::restoreFocus()
2017{
2018 if (restoreFocusWidget.isNull())
2019 return false;
2020 QWidget *candidate = restoreFocusWidget;
2021 restoreFocusWidget.clear();
2022 if (!candidate->hasFocus() && q_func()->isAncestorOf(candidate)
2023 && candidate->isVisible()
2024 && candidate->focusPolicy() != Qt::NoFocus) {
2025 candidate->setFocus();
2026 return true;
2027 }
2028 return candidate->hasFocus();
2029}
2030
2031/*!
2032 \internal
2033*/
2034void QMdiSubWindowPrivate::setWindowFlags(Qt::WindowFlags windowFlags)
2035{
2036 Q_Q(QMdiSubWindow);
2037
2038 if (!parent) {
2039 QWidgetPrivate::setWindowFlags(windowFlags);
2040 return;
2041 }
2042
2043 Qt::WindowFlags windowType = windowFlags & Qt::WindowType_Mask;
2044 if (windowType == Qt::Dialog || windowFlags & Qt::MSWindowsFixedSizeDialogHint)
2045 windowFlags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint;
2046
2047 // Set standard flags if none of the customize flags are set
2048 if (!(windowFlags & CustomizeWindowFlags))
2049 windowFlags |= Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint;
2050 else if (windowFlags & Qt::FramelessWindowHint && windowFlags & Qt::WindowStaysOnTopHint)
2051 windowFlags = Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint;
2052 else if (windowFlags & Qt::FramelessWindowHint)
2053 windowFlags = Qt::FramelessWindowHint;
2054
2055 windowFlags &= ~windowType;
2056 windowFlags &= ~Qt::WindowFullscreenButtonHint;
2057 windowFlags |= Qt::SubWindow;
2058
2059#if QT_CONFIG(action)
2060 if (QAction *stayOnTopAction = actions[QMdiSubWindowPrivate::StayOnTopAction]) {
2061 if (windowFlags & Qt::WindowStaysOnTopHint)
2062 stayOnTopAction->setChecked(true);
2063 else
2064 stayOnTopAction->setChecked(false);
2065 }
2066#endif
2067
2068#if QT_CONFIG(sizegrip)
2069 if ((windowFlags & Qt::FramelessWindowHint) && sizeGrip)
2070 delete sizeGrip;
2071#endif
2072
2073 QWidgetPrivate::setWindowFlags(windowFlags);
2074 updateGeometryConstraints();
2075 updateActions();
2076 QSize currentSize = q->size();
2077 if (q->isVisible() && (currentSize.width() < internalMinimumSize.width()
2078 || currentSize.height() < internalMinimumSize.height())) {
2079 q->resize(currentSize.expandedTo(internalMinimumSize));
2080 }
2081}
2082
2083void QMdiSubWindowPrivate::setVisible(WindowStateAction action, bool visible)
2084{
2085#if QT_CONFIG(action)
2086 if (actions[action])
2087 actions[action]->setVisible(visible);
2088#endif
2089
2090 Q_Q(QMdiSubWindow);
2091 if (!controlContainer)
2092 controlContainer = new ControlContainer(q);
2093
2094 if (ControllerWidget *ctrlWidget = qobject_cast<ControllerWidget *>
2095 (controlContainer->controllerWidget())) {
2096 ctrlWidget->setControlVisible(action, visible);
2097 }
2098 q->update();
2099}
2100
2101#if QT_CONFIG(action)
2102void QMdiSubWindowPrivate::setEnabled(WindowStateAction action, bool enable)
2103{
2104 if (actions[action])
2105 actions[action]->setEnabled(enable);
2106}
2107
2108#if QT_CONFIG(menu)
2109void QMdiSubWindowPrivate::addToSystemMenu(WindowStateAction action, const QString &text,
2110 const char *slot)
2111{
2112 if (!systemMenu)
2113 return;
2114 actions[action] = systemMenu->addAction(text, q_func(), slot);
2115}
2116#endif
2117#endif // QT_NO_ACTION
2118
2119/*!
2120 \internal
2121*/
2122QSize QMdiSubWindowPrivate::iconSize() const
2123{
2124 Q_Q(const QMdiSubWindow);
2125 if (!parent || q->windowFlags() & Qt::FramelessWindowHint)
2126 return QSize(-1, -1);
2127 return QSize(q->style()->pixelMetric(QStyle::PM_MdiSubWindowMinimizedWidth, nullptr, q), titleBarHeight());
2128}
2129
2130#if QT_CONFIG(sizegrip)
2131
2132/*!
2133 \internal
2134*/
2135void QMdiSubWindowPrivate::setSizeGrip(QSizeGrip *newSizeGrip)
2136{
2137 Q_Q(QMdiSubWindow);
2138 if (!newSizeGrip || sizeGrip || q->windowFlags() & Qt::FramelessWindowHint)
2139 return;
2140
2141 if (layout && layout->indexOf(newSizeGrip) != -1)
2142 return;
2143 newSizeGrip->setFixedSize(newSizeGrip->sizeHint());
2144 bool putSizeGripInLayout = layout ? true : false;
2145 if (isMacStyle(q->style()))
2146 putSizeGripInLayout = false;
2147 if (putSizeGripInLayout) {
2148 layout->addWidget(newSizeGrip);
2149 layout->setAlignment(newSizeGrip, Qt::AlignBottom | Qt::AlignRight);
2150 } else {
2151 newSizeGrip->setParent(q);
2152 newSizeGrip->move(q->isLeftToRight() ? q->width() - newSizeGrip->width() : 0,
2153 q->height() - newSizeGrip->height());
2154 sizeGrip = newSizeGrip;
2155 }
2156 newSizeGrip->raise();
2157 updateGeometryConstraints();
2158 newSizeGrip->installEventFilter(q);
2159}
2160
2161/*!
2162 \internal
2163*/
2164void QMdiSubWindowPrivate::setSizeGripVisible(bool visible) const
2165{
2166 // See if we can find any size grips
2167 const QList<QSizeGrip *> sizeGrips = q_func()->findChildren<QSizeGrip *>();
2168 for (QSizeGrip *grip : sizeGrips)
2169 grip->setVisible(visible);
2170}
2171
2172#endif // QT_CONFIG(sizegrip)
2173
2174/*!
2175 \internal
2176*/
2177void QMdiSubWindowPrivate::updateInternalWindowTitle()
2178{
2179 Q_Q(QMdiSubWindow);
2180 if (q->isWindowModified()) {
2181 windowTitle = q->windowTitle();
2182 windowTitle.replace("[*]"_L1, "*"_L1);
2183 } else {
2184 windowTitle = qt_setWindowTitle_helperHelper(q->windowTitle(), q);
2185 }
2186 q->update(0, 0, q->width(), titleBarHeight());
2187}
2188
2189/*!
2190 Constructs a new QMdiSubWindow widget. The \a parent and \a
2191 flags arguments are passed to QWidget's constructor.
2192
2193 Instead of using addSubWindow(), it is also simply possible to
2194 use setParent() when you add the subwindow to a QMdiArea.
2195
2196 Note that only \l{QMdiSubWindow}s can be set as children of
2197 QMdiArea; you cannot, for instance, write:
2198
2199 \code
2200 //bad code
2201 QMdiArea mdiArea;
2202 QTextEdit editor(&mdiArea); // invalid child widget
2203 \endcode
2204
2205 \sa QMdiArea::addSubWindow()
2206*/
2207QMdiSubWindow::QMdiSubWindow(QWidget *parent, Qt::WindowFlags flags)
2208 : QWidget(*new QMdiSubWindowPrivate, parent, { })
2209{
2210 Q_D(QMdiSubWindow);
2211#if QT_CONFIG(menu)
2212 d->createSystemMenu();
2213 addActions(d->systemMenu->actions());
2214#endif
2215 d->setWindowFlags(flags);
2216 setBackgroundRole(QPalette::Window);
2217 setAutoFillBackground(true);
2218 setMouseTracking(true);
2219 setLayout(new QVBoxLayout);
2220 setFocusPolicy(Qt::StrongFocus);
2221 layout()->setContentsMargins(QMargins());
2222 d->updateGeometryConstraints();
2223 setAttribute(Qt::WA_Resized, false);
2224 d->titleBarPalette = d->desktopPalette();
2225 d->font = QApplication::font("QMdiSubWindowTitleBar");
2226 // We don't want the menu icon by default on mac.
2227#ifndef Q_OS_DARWIN
2228 if (windowIcon().isNull())
2229 d->menuIcon = style()->standardIcon(QStyle::SP_TitleBarMenuButton, nullptr, this);
2230 else
2231 d->menuIcon = windowIcon();
2232#endif
2233 connect(qApp, SIGNAL(focusChanged(QWidget*,QWidget*)),
2234 this, SLOT(_q_processFocusChanged(QWidget*,QWidget*)));
2235}
2236
2237/*!
2238 Destroys the subwindow.
2239
2240 \sa QMdiArea::removeSubWindow()
2241*/
2242QMdiSubWindow::~QMdiSubWindow()
2243{
2244 Q_D(QMdiSubWindow);
2245#if QT_CONFIG(menubar)
2246 d->removeButtonsFromMenuBar();
2247#endif
2248 d->setActive(false);
2249 delete d->controlContainer;
2250}
2251
2252/*!
2253 Sets \a widget as the internal widget of this subwindow. The
2254 internal widget is displayed in the center of the subwindow
2255 beneath the title bar.
2256
2257 QMdiSubWindow takes temporary ownership of \a widget; you do
2258 not have to delete it. Any existing internal widget will be
2259 removed and reparented to the root window.
2260
2261 \sa widget()
2262*/
2263void QMdiSubWindow::setWidget(QWidget *widget)
2264{
2265 Q_D(QMdiSubWindow);
2266 if (!widget) {
2267 d->removeBaseWidget();
2268 return;
2269 }
2270
2271 if (Q_UNLIKELY(widget == d->baseWidget)) {
2272 qWarning("QMdiSubWindow::setWidget: widget is already set");
2273 return;
2274 }
2275
2276 bool wasResized = testAttribute(Qt::WA_Resized);
2277 d->removeBaseWidget();
2278
2279 if (QLayout *layout = this->layout())
2280 layout->addWidget(widget);
2281 else
2282 widget->setParent(this);
2283
2284#if QT_CONFIG(sizegrip)
2285 QSizeGrip *sizeGrip = widget->findChild<QSizeGrip *>();
2286 if (sizeGrip)
2287 sizeGrip->installEventFilter(this);
2288 if (d->sizeGrip)
2289 d->sizeGrip->raise();
2290#endif
2291
2292 d->baseWidget = widget;
2293 d->baseWidget->installEventFilter(this);
2294
2295 d->ignoreWindowTitleChange = true;
2296 bool isWindowModified = this->isWindowModified();
2297 if (windowTitle().isEmpty()) {
2298 d->updateWindowTitle(true);
2299 isWindowModified = d->baseWidget->isWindowModified();
2300 }
2301 if (!this->isWindowModified() && isWindowModified && windowTitle().contains("[*]"_L1))
2302 setWindowModified(isWindowModified);
2303 d->lastChildWindowTitle = d->baseWidget->windowTitle();
2304 d->ignoreWindowTitleChange = false;
2305
2306 if (windowIcon().isNull() && !d->baseWidget->windowIcon().isNull())
2307 setWindowIcon(d->baseWidget->windowIcon());
2308
2309 d->updateGeometryConstraints();
2310 if (!wasResized && testAttribute(Qt::WA_Resized))
2311 setAttribute(Qt::WA_Resized, false);
2312}
2313
2314/*!
2315 Returns the current internal widget.
2316
2317 \sa setWidget()
2318*/
2319QWidget *QMdiSubWindow::widget() const
2320{
2321 return d_func()->baseWidget;
2322}
2323
2324
2325/*!
2326 \internal
2327*/
2328QWidget *QMdiSubWindow::maximizedButtonsWidget() const
2329{
2330 Q_D(const QMdiSubWindow);
2331 if (isVisible() && d->controlContainer && isMaximized() && !d->drawTitleBarWhenMaximized()
2332 && !isChildOfTabbedQMdiArea(this)) {
2333 return d->controlContainer->controllerWidget();
2334 }
2335 return nullptr;
2336}
2337
2338/*!
2339 \internal
2340*/
2341QWidget *QMdiSubWindow::maximizedSystemMenuIconWidget() const
2342{
2343 Q_D(const QMdiSubWindow);
2344 if (isVisible() && d->controlContainer && isMaximized() && !d->drawTitleBarWhenMaximized()
2345 && !isChildOfTabbedQMdiArea(this)) {
2346 return d->controlContainer->systemMenuLabel();
2347 }
2348 return nullptr;
2349}
2350
2351/*!
2352 Returns \c true if this window is shaded; otherwise returns \c false.
2353
2354 A window is shaded if it is collapsed so that only the title bar is
2355 visible.
2356*/
2357bool QMdiSubWindow::isShaded() const
2358{
2359 return d_func()->isShadeMode;
2360}
2361
2362/*!
2363 If \a on is true, \a option is enabled on the subwindow; otherwise it is
2364 disabled. See SubWindowOption for the effect of each option.
2365
2366 \sa SubWindowOption, testOption()
2367*/
2368void QMdiSubWindow::setOption(SubWindowOption option, bool on)
2369{
2370 Q_D(QMdiSubWindow);
2371 d->options.setFlag(option, on);
2372
2373#if QT_CONFIG(rubberband)
2374 if ((option & (RubberBandResize | RubberBandMove)) && !on && d->isInRubberBandMode)
2375 d->leaveRubberBandMode();
2376#endif
2377}
2378
2379/*!
2380 Returns \c true if \a option is enabled; otherwise returns \c false.
2381
2382 \sa SubWindowOption, setOption()
2383*/
2384bool QMdiSubWindow::testOption(SubWindowOption option) const
2385{
2386 return d_func()->options & option;
2387}
2388
2389/*!
2390 \property QMdiSubWindow::keyboardSingleStep
2391 \brief sets how far a widget should move or resize when using the
2392 keyboard arrow keys.
2393
2394 When in keyboard-interactive mode, you can use the arrow and page keys to
2395 either move or resize the window. This property controls the arrow keys.
2396 The common way to enter keyboard interactive mode is to enter the
2397 subwindow menu, and select either "resize" or "move".
2398
2399 The default keyboard single step value is 5 pixels.
2400
2401 \sa keyboardPageStep
2402*/
2403int QMdiSubWindow::keyboardSingleStep() const
2404{
2405 return d_func()->keyboardSingleStep;
2406}
2407
2408void QMdiSubWindow::setKeyboardSingleStep(int step)
2409{
2410 // Haven't done any boundary check here since negative step only
2411 // means inverted behavior, which is OK if the user want it.
2412 // A step equal to zero means "do nothing".
2413 d_func()->keyboardSingleStep = step;
2414}
2415
2416/*!
2417 \property QMdiSubWindow::keyboardPageStep
2418 \brief sets how far a widget should move or resize when using the
2419 keyboard page keys.
2420
2421 When in keyboard-interactive mode, you can use the arrow and page keys to
2422 either move or resize the window. This property controls the page
2423 keys. The common way to enter keyboard interactive mode is to enter the
2424 subwindow menu, and select either "resize" or "move".
2425
2426 The default keyboard page step value is 20 pixels.
2427
2428 \sa keyboardSingleStep
2429*/
2430int QMdiSubWindow::keyboardPageStep() const
2431{
2432 return d_func()->keyboardPageStep;
2433}
2434
2435void QMdiSubWindow::setKeyboardPageStep(int step)
2436{
2437 // Haven't done any boundary check here since negative step only
2438 // means inverted behavior, which is OK if the user want it.
2439 // A step equal to zero means "do nothing".
2440 d_func()->keyboardPageStep = step;
2441}
2442
2443#if QT_CONFIG(menu)
2444/*!
2445 Sets \a systemMenu as the current system menu for this subwindow.
2446
2447 By default, each QMdiSubWindow has a standard system menu.
2448
2449 QActions for the system menu created by QMdiSubWindow will
2450 automatically be updated depending on the current window state;
2451 e.g., the minimize action will be disabled after the window is
2452 minimized.
2453
2454 QActions added by the user are not updated by QMdiSubWindow.
2455
2456 QMdiSubWindow takes ownership of \a systemMenu; you do not have to
2457 delete it. Any existing menus will be deleted.
2458
2459 \sa systemMenu(), showSystemMenu()
2460*/
2461void QMdiSubWindow::setSystemMenu(QMenu *systemMenu)
2462{
2463 Q_D(QMdiSubWindow);
2464 if (Q_UNLIKELY(systemMenu && systemMenu == d->systemMenu)) {
2465 qWarning("QMdiSubWindow::setSystemMenu: system menu is already set");
2466 return;
2467 }
2468
2469 if (d->systemMenu) {
2470 delete d->systemMenu;
2471 d->systemMenu = nullptr;
2472 }
2473
2474 if (!systemMenu)
2475 return;
2476
2477 if (systemMenu->parent() != this)
2478 systemMenu->setParent(this);
2479 d->systemMenu = systemMenu;
2480}
2481
2482/*!
2483 Returns a pointer to the current system menu, or zero if no system
2484 menu is set. QMdiSubWindow provides a default system menu, but you can
2485 also set the menu with setSystemMenu().
2486
2487 \sa setSystemMenu(), showSystemMenu()
2488*/
2489QMenu *QMdiSubWindow::systemMenu() const
2490{
2491 return d_func()->systemMenu;
2492}
2493
2494/*!
2495 Shows the system menu below the system menu icon in the title bar.
2496
2497 \sa setSystemMenu(), systemMenu()
2498*/
2499void QMdiSubWindow::showSystemMenu()
2500{
2501 Q_D(QMdiSubWindow);
2502 if (!d->systemMenu)
2503 return;
2504
2505 QPoint globalPopupPos;
2506 if (QWidget *icon = maximizedSystemMenuIconWidget()) {
2507 if (isLeftToRight())
2508 globalPopupPos = icon->mapToGlobal(QPoint(0, icon->y() + icon->height()));
2509 else
2510 globalPopupPos = icon->mapToGlobal(QPoint(icon->width(), icon->y() + icon->height()));
2511 } else {
2512 if (isLeftToRight())
2513 globalPopupPos = mapToGlobal(contentsRect().topLeft());
2514 else // + QPoint(1, 0) because topRight() == QPoint(left() + width() -1, top())
2515 globalPopupPos = mapToGlobal(contentsRect().topRight()) + QPoint(1, 0);
2516 }
2517
2518 // Adjust x() with -menuwidth in reverse mode.
2519 if (isRightToLeft())
2520 globalPopupPos -= QPoint(d->systemMenu->sizeHint().width(), 0);
2521 d->systemMenu->popup(globalPopupPos);
2522}
2523#endif // QT_CONFIG(menu)
2524
2525/*!
2526 \since 4.4
2527
2528 Returns the area containing this sub-window, or \nullptr if there
2529 is none.
2530
2531 \sa QMdiArea::addSubWindow()
2532*/
2533QMdiArea *QMdiSubWindow::mdiArea() const
2534{
2535 QWidget *parent = parentWidget();
2536 while (parent) {
2537 if (QMdiArea *area = qobject_cast<QMdiArea *>(parent)) {
2538 if (area->viewport() == parentWidget())
2539 return area;
2540 }
2541 parent = parent->parentWidget();
2542 }
2543 return nullptr;
2544}
2545
2546/*!
2547 Calling this function makes the subwindow enter the shaded mode.
2548 When the subwindow is shaded, only the title bar is visible.
2549
2550 Although shading is not supported by all styles, this function will
2551 still show the subwindow as shaded, regardless of whether support
2552 for shading is available. However, when used with styles without
2553 shading support, the user will be unable to return from shaded mode
2554 through the user interface (e.g., through a shade button in the title
2555 bar).
2556
2557 \sa isShaded()
2558*/
2559void QMdiSubWindow::showShaded()
2560{
2561 if (!parent())
2562 return;
2563
2564 Q_D(QMdiSubWindow);
2565 // setMinimizeMode uses this function.
2566 if (!d->isShadeRequestFromMinimizeMode && isShaded())
2567 return;
2568
2569 d->isMaximizeMode = false;
2570
2571 d->storeFocusWidget();
2572
2573 if (!d->isShadeRequestFromMinimizeMode) {
2574 d->isShadeMode = true;
2575 d->ensureWindowState(Qt::WindowMinimized);
2576 }
2577
2578#if QT_CONFIG(menubar)
2579 d->removeButtonsFromMenuBar();
2580#endif
2581
2582 // showMinimized() will reset Qt::WindowActive, which makes sense
2583 // for top level widgets, but in MDI it makes sense to have an
2584 // active window which is minimized.
2585 if (hasFocus() || isAncestorOf(QApplication::focusWidget()))
2586 d->ensureWindowState(Qt::WindowActive);
2587
2588#if QT_CONFIG(sizegrip)
2589 d->setSizeGripVisible(false);
2590#endif
2591
2592 if (!d->restoreSize.isValid() || d->isShadeMode) {
2593 d->oldGeometry = geometry();
2594 d->restoreSize.setWidth(d->oldGeometry.width());
2595 d->restoreSize.setHeight(d->oldGeometry.height());
2596 }
2597
2598 // Hide the window before we change the geometry to avoid multiple resize
2599 // events and wrong window state.
2600 const bool wasVisible = isVisible();
2601 if (wasVisible)
2602 setVisible(false);
2603
2604 d->updateGeometryConstraints();
2605 // Update minimum size to internalMinimumSize if set by user.
2606 if (!minimumSize().isNull()) {
2607 d->userMinimumSize = minimumSize();
2608 setMinimumSize(d->internalMinimumSize);
2609 }
2610 resize(d->internalMinimumSize);
2611
2612 // Hide the internal widget if not already hidden by the user.
2613 if (d->baseWidget && !d->baseWidget->isHidden() && !(windowFlags() & Qt::FramelessWindowHint)) {
2614 d->baseWidget->hide();
2615 d->isWidgetHiddenByUs = true;
2616 }
2617
2618 if (wasVisible)
2619 setVisible(true);
2620
2621 d->setFocusWidget();
2622 d->resizeEnabled = false;
2623 d->moveEnabled = true;
2624 d->updateDirtyRegions();
2625 d->updateMask();
2626
2627#if QT_CONFIG(action)
2628 d->setEnabled(QMdiSubWindowPrivate::MinimizeAction, false);
2629 d->setEnabled(QMdiSubWindowPrivate::ResizeAction, d->resizeEnabled);
2630 d->setEnabled(QMdiSubWindowPrivate::MaximizeAction, true);
2631 d->setEnabled(QMdiSubWindowPrivate::RestoreAction, true);
2632 d->setEnabled(QMdiSubWindowPrivate::MoveAction, d->moveEnabled);
2633#endif
2634}
2635
2636/*!
2637 \reimp
2638*/
2639bool QMdiSubWindow::eventFilter(QObject *object, QEvent *event)
2640{
2641 Q_D(QMdiSubWindow);
2642 if (!object)
2643 return QWidget::eventFilter(object, event);
2644
2645#if QT_CONFIG(menu)
2646 // System menu events.
2647 if (d->systemMenu && d->systemMenu == object) {
2648 if (event->type() == QEvent::MouseButtonDblClick) {
2649 const QMouseEvent *mouseEvent = static_cast<const QMouseEvent *>(event);
2650 const QAction *action = d->systemMenu->actionAt(mouseEvent->position().toPoint());
2651 if (!action || action->isEnabled())
2652 close();
2653 } else if (event->type() == QEvent::MouseMove) {
2654 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
2655 d->hoveredSubControl = d->getSubControl(mapFromGlobal(mouseEvent->globalPosition()).toPoint());
2656 } else if (event->type() == QEvent::Hide) {
2657 d->activeSubControl = QStyle::SC_None;
2658 update(QRegion(0, 0, width(), d->titleBarHeight()));
2659 }
2660 return QWidget::eventFilter(object, event);
2661 }
2662#endif
2663
2664#if QT_CONFIG(sizegrip)
2665 if (object != d->baseWidget && parent() && qobject_cast<QSizeGrip *>(object)) {
2666 if (event->type() != QEvent::MouseButtonPress || !testOption(QMdiSubWindow::RubberBandResize))
2667 return QWidget::eventFilter(object, event);
2668 const QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
2669 d->mousePressPosition = parentWidget()->mapFromGlobal(mouseEvent->globalPosition()).toPoint();
2670 d->oldGeometry = geometry();
2671 d->currentOperation = isLeftToRight() ? QMdiSubWindowPrivate::BottomRightResize
2672 : QMdiSubWindowPrivate::BottomLeftResize;
2673#if QT_CONFIG(rubberband)
2674 d->enterRubberBandMode();
2675#endif
2676 return true;
2677 }
2678#endif
2679
2680 if (object != d->baseWidget && event->type() != QEvent::WindowTitleChange)
2681 return QWidget::eventFilter(object, event);
2682
2683 switch (event->type()) {
2684 case QEvent::Show:
2685 d->setActive(true);
2686 break;
2687 case QEvent::ShowToParent:
2688 if (!d->isWidgetHiddenByUs)
2689 show();
2690 break;
2691 case QEvent::WindowStateChange: {
2692 QWindowStateChangeEvent *changeEvent = static_cast<QWindowStateChangeEvent*>(event);
2693 if (changeEvent->isOverride())
2694 break;
2695 Qt::WindowStates oldState = changeEvent->oldState();
2696 Qt::WindowStates newState = d->baseWidget->windowState();
2697 if (!(oldState & Qt::WindowMinimized) && (newState & Qt::WindowMinimized))
2698 showMinimized();
2699 else if (!(oldState & Qt::WindowMaximized) && (newState & Qt::WindowMaximized))
2700 showMaximized();
2701 else if (!(newState & (Qt::WindowMaximized | Qt::WindowMinimized | Qt::WindowFullScreen)))
2702 showNormal();
2703 break;
2704 }
2705 case QEvent::Enter:
2706 d->currentOperation = std::nullopt;
2707 d->updateCursor();
2708 break;
2709 case QEvent::LayoutRequest:
2710 d->updateGeometryConstraints();
2711 break;
2712 case QEvent::WindowTitleChange:
2713 if (d->ignoreWindowTitleChange)
2714 break;
2715 if (object == d->baseWidget) {
2716 d->updateWindowTitle(true);
2717 d->lastChildWindowTitle = d->baseWidget->windowTitle();
2718#if QT_CONFIG(menubar)
2719 } else if (maximizedButtonsWidget() && d->controlContainer->menuBar() && d->controlContainer->menuBar()
2720 ->cornerWidget(Qt::TopRightCorner) == maximizedButtonsWidget()) {
2721 d->originalTitle.clear();
2722 if (d->baseWidget && d->baseWidget->windowTitle() == windowTitle())
2723 d->updateWindowTitle(true);
2724 else
2725 d->updateWindowTitle(false);
2726#endif
2727 }
2728 break;
2729 case QEvent::ModifiedChange: {
2730 if (object != d->baseWidget)
2731 break;
2732 bool windowModified = d->baseWidget->isWindowModified();
2733 if (!windowModified && d->baseWidget->windowTitle() != windowTitle())
2734 break;
2735 if (windowTitle().contains("[*]"_L1))
2736 setWindowModified(windowModified);
2737 break;
2738 }
2739 default:
2740 break;
2741 }
2742 return QWidget::eventFilter(object, event);
2743}
2744
2745/*!
2746 \reimp
2747*/
2748bool QMdiSubWindow::event(QEvent *event)
2749{
2750 Q_D(QMdiSubWindow);
2751 switch (event->type()) {
2752 case QEvent::StyleChange: {
2753 bool wasShaded = isShaded();
2754 bool wasMinimized = isMinimized();
2755 bool wasMaximized = isMaximized();
2756 // Don't emit subWindowActivated, the app doesn't have to know about our hacks
2757 const QScopedValueRollback<bool> activationEnabledSaver(d->activationEnabled);
2758 d->activationEnabled = false;
2759
2760 ensurePolished();
2761 setContentsMargins(0, 0, 0, 0);
2762 if (wasMinimized || wasMaximized || wasShaded)
2763 showNormal();
2764 d->updateGeometryConstraints();
2765 resize(d->internalMinimumSize.expandedTo(size()));
2766 d->updateMask();
2767 d->updateDirtyRegions();
2768 if (wasShaded)
2769 showShaded();
2770 else if (wasMinimized)
2771 showMinimized();
2772 else if (wasMaximized)
2773 showMaximized();
2774 break;
2775 }
2776 case QEvent::ParentAboutToChange:
2777 d->setActive(false);
2778 break;
2779 case QEvent::ParentChange: {
2780 bool wasResized = testAttribute(Qt::WA_Resized);
2781#if QT_CONFIG(menubar)
2782 d->removeButtonsFromMenuBar();
2783#endif
2784 d->currentOperation = std::nullopt;
2785 d->activeSubControl = QStyle::SC_None;
2786 d->hoveredSubControl = QStyle::SC_None;
2787#if QT_CONFIG(rubberband)
2788 if (d->isInRubberBandMode)
2789 d->leaveRubberBandMode();
2790#endif
2791 d->isShadeMode = false;
2792 d->isMaximizeMode = false;
2793 d->isWidgetHiddenByUs = false;
2794 if (!parent()) {
2795#if QT_CONFIG(sizegrip)
2796 if (isMacStyle(style()))
2797 delete d->sizeGrip;
2798#endif
2799 setOption(RubberBandResize, false);
2800 setOption(RubberBandMove, false);
2801 } else {
2802 d->setWindowFlags(windowFlags());
2803 }
2804 setContentsMargins(0, 0, 0, 0);
2805 d->updateGeometryConstraints();
2806 d->updateCursor();
2807 d->updateMask();
2808 d->updateDirtyRegions();
2809 d->updateActions();
2810 if (!wasResized && testAttribute(Qt::WA_Resized))
2811 setAttribute(Qt::WA_Resized, false);
2812 break;
2813 }
2814 case QEvent::WindowActivate:
2815 if (d->ignoreNextActivationEvent) {
2816 d->ignoreNextActivationEvent = false;
2817 break;
2818 }
2819 d->isExplicitlyDeactivated = false;
2820 d->setActive(true);
2821 break;
2822 case QEvent::WindowDeactivate:
2823 if (d->ignoreNextActivationEvent) {
2824 d->ignoreNextActivationEvent = false;
2825 break;
2826 }
2827 d->isExplicitlyDeactivated = true;
2828 d->setActive(false);
2829 break;
2830 case QEvent::WindowTitleChange:
2831 if (!d->ignoreWindowTitleChange)
2832 d->updateWindowTitle(false);
2833 d->updateInternalWindowTitle();
2834 break;
2835 case QEvent::ModifiedChange:
2836 if (!windowTitle().contains("[*]"_L1))
2837 break;
2838#if QT_CONFIG(menubar)
2839 if (maximizedButtonsWidget() && d->controlContainer->menuBar() && d->controlContainer->menuBar()
2840 ->cornerWidget(Qt::TopRightCorner) == maximizedButtonsWidget()) {
2841 window()->setWindowModified(isWindowModified());
2842 }
2843#endif // QT_CONFIG(menubar)
2844 d->updateInternalWindowTitle();
2845 break;
2846 case QEvent::LayoutDirectionChange:
2847 d->updateDirtyRegions();
2848 break;
2849 case QEvent::LayoutRequest:
2850 d->updateGeometryConstraints();
2851 break;
2852 case QEvent::WindowIconChange:
2853 d->menuIcon = windowIcon();
2854 // We don't want the default menu icon on mac.
2855#ifndef Q_OS_DARWIN
2856 if (d->menuIcon.isNull())
2857 d->menuIcon = style()->standardIcon(QStyle::SP_TitleBarMenuButton, nullptr, this);
2858#endif
2859 if (d->controlContainer)
2860 d->controlContainer->updateWindowIcon(d->menuIcon);
2861 if (!maximizedSystemMenuIconWidget())
2862 update(0, 0, width(), d->titleBarHeight());
2863 break;
2864 case QEvent::PaletteChange:
2865 d->titleBarPalette = d->desktopPalette();
2866 break;
2867 case QEvent::FontChange:
2868 d->font = font();
2869 break;
2870#if QT_CONFIG(tooltip)
2871 case QEvent::ToolTip:
2872 showToolTip(static_cast<QHelpEvent *>(event), this, d->titleBarOptions(),
2873 QStyle::CC_TitleBar, d->hoveredSubControl);
2874 break;
2875#endif
2876#if QT_CONFIG(action)
2877 case QEvent::ActionAdded:
2878 case QEvent::ActionChanged:
2879 case QEvent::ActionRemoved:
2880 update();
2881 break;
2882#endif
2883 default:
2884 break;
2885 }
2886 return QWidget::event(event);
2887}
2888
2889/*!
2890 \reimp
2891*/
2892void QMdiSubWindow::showEvent(QShowEvent *showEvent)
2893{
2894 Q_D(QMdiSubWindow);
2895 if (!parent()) {
2896 QWidget::showEvent(showEvent);
2897 return;
2898 }
2899
2900#if QT_CONFIG(sizegrip)
2901 if (isMacStyle(style()) && !d->sizeGrip
2902 && !(windowFlags() & Qt::FramelessWindowHint)) {
2903 d->setSizeGrip(new QSizeGrip(this));
2904 Q_ASSERT(d->sizeGrip);
2905 if (isMinimized())
2906 d->setSizeGripVisible(false);
2907 else
2908 d->setSizeGripVisible(true);
2909 resize(size().expandedTo(d->internalMinimumSize));
2910 }
2911#endif
2912
2913 d->updateDirtyRegions();
2914 // Show buttons in the menu bar if they're already not there.
2915 // We want to do this when QMdiSubWindow becomes visible after being hidden.
2916#if QT_CONFIG(menubar)
2917 if (d->controlContainer) {
2918 if (QMenuBar *menuBar = d->menuBar()) {
2919 if (menuBar->cornerWidget(Qt::TopRightCorner) != maximizedButtonsWidget())
2920 d->showButtonsInMenuBar(menuBar);
2921 }
2922 }
2923#endif
2924 d->setActive(true);
2925}
2926
2927/*!
2928 \reimp
2929*/
2930void QMdiSubWindow::hideEvent(QHideEvent * /*hideEvent*/)
2931{
2932#if QT_CONFIG(menubar)
2933 d_func()->removeButtonsFromMenuBar();
2934#endif
2935}
2936
2937/*!
2938 \reimp
2939*/
2940void QMdiSubWindow::changeEvent(QEvent *changeEvent)
2941{
2942 if (!parent()) {
2943 QWidget::changeEvent(changeEvent);
2944 return;
2945 }
2946
2947 if (changeEvent->type() != QEvent::WindowStateChange) {
2948 QWidget::changeEvent(changeEvent);
2949 return;
2950 }
2951
2952 QWindowStateChangeEvent *event = static_cast<QWindowStateChangeEvent *>(changeEvent);
2953 if (event->isOverride()) {
2954 event->ignore();
2955 return;
2956 }
2957
2958 Qt::WindowStates oldState = event->oldState();
2959 Qt::WindowStates newState = windowState();
2960 if (oldState == newState) {
2961 changeEvent->ignore();
2962 return;
2963 }
2964
2965 // QWidget ensures that the widget is visible _after_ setWindowState(),
2966 // but we need to ensure that the widget is visible _before_
2967 // setWindowState() returns.
2968 Q_D(QMdiSubWindow);
2969 if (!isVisible()) {
2970 d->ensureWindowState(Qt::WindowNoState);
2971 setVisible(true);
2972 }
2973
2974 if (!d->oldGeometry.isValid())
2975 d->oldGeometry = geometry();
2976
2977 if ((oldState & Qt::WindowActive) && (newState & Qt::WindowActive))
2978 d->currentOperation = std::nullopt;
2979
2980 if (!(oldState & Qt::WindowMinimized) && (newState & Qt::WindowMinimized))
2981 d->setMinimizeMode();
2982 else if (!(oldState & Qt::WindowMaximized) && (newState & Qt::WindowMaximized))
2983 d->setMaximizeMode();
2984 else if (!(newState & (Qt::WindowMaximized | Qt::WindowMinimized | Qt::WindowFullScreen)))
2985 d->setNormalMode();
2986
2987 if (d->isActive)
2988 d->ensureWindowState(Qt::WindowActive);
2989 if (d->activationEnabled)
2990 emit windowStateChanged(oldState, windowState());
2991}
2992
2993/*!
2994 \reimp
2995*/
2996void QMdiSubWindow::closeEvent(QCloseEvent *closeEvent)
2997{
2998 Q_D(QMdiSubWindow);
2999 bool acceptClose = true;
3000 if (d->baseWidget)
3001 acceptClose = d->baseWidget->close();
3002 if (!acceptClose) {
3003 closeEvent->ignore();
3004 return;
3005 }
3006#if QT_CONFIG(menubar)
3007 d->removeButtonsFromMenuBar();
3008#endif
3009 d->setActive(false);
3010 if (parentWidget() && testAttribute(Qt::WA_DeleteOnClose)) {
3011 QChildEvent childRemoved(QEvent::ChildRemoved, this);
3012 QCoreApplication::sendEvent(parentWidget(), &childRemoved);
3013 }
3014 closeEvent->accept();
3015}
3016
3017/*!
3018 \reimp
3019*/
3020void QMdiSubWindow::leaveEvent(QEvent * /*leaveEvent*/)
3021{
3022 Q_D(QMdiSubWindow);
3023 if (d->hoveredSubControl != QStyle::SC_None) {
3024 d->hoveredSubControl = QStyle::SC_None;
3025 update(QRegion(0, 0, width(), d->titleBarHeight()));
3026 }
3027}
3028
3029/*!
3030 \reimp
3031
3032 \warning When maximizing or restoring a subwindow, the resulting call to this function
3033 may have an invalid QResizeEvent::oldSize().
3034*/
3035void QMdiSubWindow::resizeEvent(QResizeEvent *resizeEvent)
3036{
3037 Q_D(QMdiSubWindow);
3038#if QT_CONFIG(sizegrip)
3039 if (d->sizeGrip) {
3040 d->sizeGrip->move(isLeftToRight() ? width() - d->sizeGrip->width() : 0,
3041 height() - d->sizeGrip->height());
3042 }
3043#endif
3044
3045 if (!parent()) {
3046 QWidget::resizeEvent(resizeEvent);
3047 return;
3048 }
3049
3050 if (d->isMaximizeMode)
3051 d->ensureWindowState(Qt::WindowMaximized);
3052
3053 d->updateMask();
3054 if (!isVisible())
3055 return;
3056
3057 if (!d->resizeTimer.isActive())
3058 d->cachedStyleOptions = d->titleBarOptions();
3059 d->resizeTimer.start(200ms, this);
3060}
3061
3062/*!
3063 \reimp
3064*/
3065void QMdiSubWindow::timerEvent(QTimerEvent *timerEvent)
3066{
3067 Q_D(QMdiSubWindow);
3068 if (timerEvent->id() == d->resizeTimer.id()) {
3069 d->resizeTimer.stop();
3070 d->updateDirtyRegions();
3071 }
3072}
3073
3074/*!
3075 \reimp
3076*/
3077void QMdiSubWindow::moveEvent(QMoveEvent *moveEvent)
3078{
3079 if (!parent()) {
3080 QWidget::moveEvent(moveEvent);
3081 return;
3082 }
3083
3084 Q_D(QMdiSubWindow);
3085 if (d->isMaximizeMode)
3086 d->ensureWindowState(Qt::WindowMaximized);
3087}
3088
3089/*!
3090 \reimp
3091*/
3092void QMdiSubWindow::paintEvent(QPaintEvent *paintEvent)
3093{
3094 if (!parent() || (windowFlags() & Qt::FramelessWindowHint)) {
3095 QWidget::paintEvent(paintEvent);
3096 return;
3097 }
3098
3099 Q_D(QMdiSubWindow);
3100
3101 if (d->resizeTimer.isActive()) {
3102 // Only update the style option rect and the window title.
3103 int border = d->hasBorder(d->cachedStyleOptions) ? 4 : 0;
3104 int titleBarHeight = d->titleBarHeight(d->cachedStyleOptions);
3105 titleBarHeight -= isMinimized() ? 2 * border : border;
3106 d->cachedStyleOptions.rect = QRect(border, border, width() - 2 * border, titleBarHeight);
3107 if (!d->windowTitle.isEmpty()) {
3108 int width = style()->subControlRect(QStyle::CC_TitleBar, &d->cachedStyleOptions,
3109 QStyle::SC_TitleBarLabel, this).width();
3110 d->cachedStyleOptions.text = d->cachedStyleOptions.fontMetrics
3111 .elidedText(d->windowTitle, Qt::ElideRight, width);
3112 }
3113 } else {
3114 // Force full update.
3115 d->cachedStyleOptions = d->titleBarOptions();
3116 }
3117
3118 QStylePainter painter(this);
3119 QStyleOptionFrame frameOptions;
3120 frameOptions.initFrom(this);
3121 frameOptions.state.setFlag(QStyle::State_Active, d->isActive);
3122 if (isMaximized() && !d->drawTitleBarWhenMaximized()) {
3123 if (!autoFillBackground() && (!widget() || !qt_widget_private(widget())->isOpaque)) {
3124 // make sure we paint all pixels of a maximized QMdiSubWindow if no-one else does
3125 painter.drawPrimitive(QStyle::PE_FrameWindow, frameOptions);
3126 }
3127 return;
3128 }
3129
3130 if (!d->windowTitle.isEmpty())
3131 painter.setFont(d->font);
3132 painter.drawComplexControl(QStyle::CC_TitleBar, d->cachedStyleOptions);
3133
3134 if (isMinimized() && !d->hasBorder(d->cachedStyleOptions))
3135 return;
3136
3137 frameOptions.lineWidth = style()->pixelMetric(QStyle::PM_MdiSubWindowFrameWidth, nullptr, this);
3138
3139 // ### Ensure that we do not require setting the cliprect for 4.4
3140 if (!isMinimized() && !d->hasBorder(d->cachedStyleOptions))
3141 painter.setClipRect(rect().adjusted(0, d->titleBarHeight(d->cachedStyleOptions), 0, 0));
3142 if (!isMinimized() || d->hasBorder(d->cachedStyleOptions))
3143 painter.drawPrimitive(QStyle::PE_FrameWindow, frameOptions);
3144}
3145
3146/*!
3147 \reimp
3148*/
3149void QMdiSubWindow::mousePressEvent(QMouseEvent *mouseEvent)
3150{
3151 if (!parent()) {
3152 QWidget::mousePressEvent(mouseEvent);
3153 return;
3154 }
3155
3156 Q_D(QMdiSubWindow);
3157 if (d->isInInteractiveMode)
3158 d->leaveInteractiveMode();
3159#if QT_CONFIG(rubberband)
3160 if (d->isInRubberBandMode)
3161 d->leaveRubberBandMode();
3162#endif
3163
3164 if (mouseEvent->button() != Qt::LeftButton) {
3165 mouseEvent->ignore();
3166 return;
3167 }
3168
3169 if (d->currentOperation) {
3170 d->updateCursor();
3171 d->mousePressPosition = mapToParent(mouseEvent->position().toPoint());
3172 if (d->resizeEnabled || d->moveEnabled)
3173 d->oldGeometry = geometry();
3174#if QT_CONFIG(rubberband)
3175 if ((testOption(QMdiSubWindow::RubberBandResize) && d->isResizeOperation())
3176 || (testOption(QMdiSubWindow::RubberBandMove) && d->isMoveOperation())) {
3177 d->enterRubberBandMode();
3178 }
3179#endif
3180 return;
3181 }
3182
3183 d->activeSubControl = d->hoveredSubControl;
3184#if QT_CONFIG(menu)
3185 if (d->activeSubControl == QStyle::SC_TitleBarSysMenu)
3186 showSystemMenu();
3187 else
3188#endif
3189 update(QRegion(0, 0, width(), d->titleBarHeight()));
3190}
3191
3192/*!
3193 \reimp
3194*/
3195void QMdiSubWindow::mouseDoubleClickEvent(QMouseEvent *mouseEvent)
3196{
3197 if (!parent()) {
3198 QWidget::mouseDoubleClickEvent(mouseEvent);
3199 return;
3200 }
3201
3202 if (mouseEvent->button() != Qt::LeftButton) {
3203 mouseEvent->ignore();
3204 return;
3205 }
3206
3207 Q_D(QMdiSubWindow);
3208 if (!d->isMoveOperation()) {
3209#if QT_CONFIG(menu)
3210 if (d->hoveredSubControl == QStyle::SC_TitleBarSysMenu)
3211 close();
3212#endif
3213 return;
3214 }
3215
3216 Qt::WindowFlags flags = windowFlags();
3217 if (isMinimized()) {
3218 if ((isShaded() && (flags & Qt::WindowShadeButtonHint))
3219 || (flags & Qt::WindowMinimizeButtonHint)) {
3220 showNormal();
3221 }
3222 return;
3223 }
3224
3225 if (isMaximized()) {
3226 if (flags & Qt::WindowMaximizeButtonHint)
3227 showNormal();
3228 return;
3229 }
3230
3231 if (flags & Qt::WindowShadeButtonHint)
3232 showShaded();
3233 else if (flags & Qt::WindowMaximizeButtonHint)
3234 showMaximized();
3235}
3236
3237/*!
3238 \reimp
3239*/
3240void QMdiSubWindow::mouseReleaseEvent(QMouseEvent *mouseEvent)
3241{
3242 if (!parent()) {
3243 QWidget::mouseReleaseEvent(mouseEvent);
3244 return;
3245 }
3246
3247 if (mouseEvent->button() != Qt::LeftButton) {
3248 mouseEvent->ignore();
3249 return;
3250 }
3251
3252 Q_D(QMdiSubWindow);
3253 if (d->currentOperation) {
3254#if QT_CONFIG(rubberband)
3255 if (d->isInRubberBandMode && !d->isInInteractiveMode)
3256 d->leaveRubberBandMode();
3257#endif
3258 if (d->resizeEnabled || d->moveEnabled)
3259 d->oldGeometry = geometry();
3260 }
3261
3262 d->currentOperation = d->getOperation(mouseEvent->position().toPoint());
3263 d->updateCursor();
3264
3265 d->hoveredSubControl = d->getSubControl(mouseEvent->position().toPoint());
3266 if (d->activeSubControl != QStyle::SC_None
3267 && d->activeSubControl == d->hoveredSubControl) {
3268 d->processClickedSubControl();
3269 }
3270 d->activeSubControl = QStyle::SC_None;
3271 update(QRegion(0, 0, width(), d->titleBarHeight()));
3272}
3273
3274/*!
3275 \reimp
3276*/
3277void QMdiSubWindow::mouseMoveEvent(QMouseEvent *mouseEvent)
3278{
3279 if (!parent()) {
3280 QWidget::mouseMoveEvent(mouseEvent);
3281 return;
3282 }
3283
3284 Q_D(QMdiSubWindow);
3285 // No update needed if we're in a move/resize operation.
3286 if (!d->isMoveOperation() && !d->isResizeOperation()) {
3287 // Find previous and current hover region.
3288 const QStyleOptionTitleBar options = d->titleBarOptions();
3289 QStyle::SubControl oldHover = d->hoveredSubControl;
3290 d->hoveredSubControl = d->getSubControl(mouseEvent->position().toPoint());
3291 QRegion hoverRegion;
3292 if (isHoverControl(oldHover) && oldHover != d->hoveredSubControl)
3293 hoverRegion += style()->subControlRect(QStyle::CC_TitleBar, &options, oldHover, this);
3294 if (isHoverControl(d->hoveredSubControl) && d->hoveredSubControl != oldHover) {
3295 hoverRegion += style()->subControlRect(QStyle::CC_TitleBar, &options,
3296 d->hoveredSubControl, this);
3297 }
3298
3299 if (isMacStyle(style()) && !hoverRegion.isEmpty())
3300 hoverRegion += QRegion(0, 0, width(), d->titleBarHeight(options));
3301
3302 if (!hoverRegion.isEmpty())
3303 update(hoverRegion);
3304 }
3305
3306 if ((mouseEvent->buttons() & Qt::LeftButton) || d->isInInteractiveMode) {
3307 if ((d->isResizeOperation() && d->resizeEnabled) || (d->isMoveOperation() && d->moveEnabled)) {
3308 // As setNewGeometry moves the window, it invalidates the pos() value of any mouse move events that are
3309 // currently queued in the event loop. Map to parent using globalPos() instead.
3310 d->setNewGeometry(parentWidget()->mapFromGlobal(mouseEvent->globalPosition()).toPoint());
3311 }
3312 return;
3313 }
3314
3315 // Do not resize/move if not allowed.
3316 d->currentOperation = d->getOperation(mouseEvent->position().toPoint());
3317 if ((d->isResizeOperation() && !d->resizeEnabled) || (d->isMoveOperation() && !d->moveEnabled))
3318 d->currentOperation = std::nullopt;
3319 d->updateCursor();
3320}
3321
3322/*!
3323 \reimp
3324*/
3325void QMdiSubWindow::keyPressEvent(QKeyEvent *keyEvent)
3326{
3327 Q_D(QMdiSubWindow);
3328 if (!d->isInInteractiveMode || !parent()) {
3329 keyEvent->ignore();
3330 return;
3331 }
3332
3333 QPoint delta;
3334 switch (keyEvent->key()) {
3335 case Qt::Key_Right:
3336 if (keyEvent->modifiers() & Qt::ShiftModifier)
3337 delta = QPoint(d->keyboardPageStep, 0);
3338 else
3339 delta = QPoint(d->keyboardSingleStep, 0);
3340 break;
3341 case Qt::Key_Up:
3342 if (keyEvent->modifiers() & Qt::ShiftModifier)
3343 delta = QPoint(0, -d->keyboardPageStep);
3344 else
3345 delta = QPoint(0, -d->keyboardSingleStep);
3346 break;
3347 case Qt::Key_Left:
3348 if (keyEvent->modifiers() & Qt::ShiftModifier)
3349 delta = QPoint(-d->keyboardPageStep, 0);
3350 else
3351 delta = QPoint(-d->keyboardSingleStep, 0);
3352 break;
3353 case Qt::Key_Down:
3354 if (keyEvent->modifiers() & Qt::ShiftModifier)
3355 delta = QPoint(0, d->keyboardPageStep);
3356 else
3357 delta = QPoint(0, d->keyboardSingleStep);
3358 break;
3359 case Qt::Key_Escape:
3360 case Qt::Key_Return:
3361 case Qt::Key_Enter:
3362 d->leaveInteractiveMode();
3363 return;
3364 default:
3365 keyEvent->ignore();
3366 return;
3367 }
3368
3369#ifndef QT_NO_CURSOR
3370 QPoint newPosition = parentWidget()->mapFromGlobal(cursor().pos() + delta);
3371 QRect oldGeometry =
3372#if QT_CONFIG(rubberband)
3373 d->isInRubberBandMode ? d->rubberBand->geometry() :
3374#endif
3375 geometry();
3376 d->setNewGeometry(newPosition);
3377 QRect currentGeometry =
3378#if QT_CONFIG(rubberband)
3379 d->isInRubberBandMode ? d->rubberBand->geometry() :
3380#endif
3381 geometry();
3382 if (currentGeometry == oldGeometry)
3383 return;
3384
3385 // Update cursor position
3386
3387 QPoint actualDelta;
3388 if (d->isMoveOperation()) {
3389 actualDelta = QPoint(currentGeometry.x() - oldGeometry.x(),
3390 currentGeometry.y() - oldGeometry.y());
3391 } else {
3392 int dx = isLeftToRight() ? currentGeometry.width() - oldGeometry.width()
3393 : currentGeometry.x() - oldGeometry.x();
3394 actualDelta = QPoint(dx, currentGeometry.height() - oldGeometry.height());
3395 }
3396
3397 // Adjust in case we weren't able to move as long as wanted.
3398 if (actualDelta != delta)
3399 newPosition += (actualDelta - delta);
3400 cursor().setPos(parentWidget()->mapToGlobal(newPosition));
3401#endif
3402}
3403
3404#ifndef QT_NO_CONTEXTMENU
3405/*!
3406 \reimp
3407*/
3408void QMdiSubWindow::contextMenuEvent(QContextMenuEvent *contextMenuEvent)
3409{
3410 Q_D(QMdiSubWindow);
3411 if (!d->systemMenu) {
3412 contextMenuEvent->ignore();
3413 return;
3414 }
3415
3416 if (d->hoveredSubControl == QStyle::SC_TitleBarSysMenu
3417 || d->getRegion(QMdiSubWindowPrivate::Move).contains(contextMenuEvent->pos())) {
3418 d->systemMenu->exec(contextMenuEvent->globalPos());
3419 } else {
3420 contextMenuEvent->ignore();
3421 }
3422}
3423#endif // QT_NO_CONTEXTMENU
3424
3425/*!
3426 \reimp
3427*/
3428void QMdiSubWindow::focusInEvent(QFocusEvent *focusInEvent)
3429{
3430 d_func()->focusInReason = focusInEvent->reason();
3431}
3432
3433/*!
3434 \reimp
3435*/
3436void QMdiSubWindow::focusOutEvent(QFocusEvent * /*focusOutEvent*/)
3437{
3438 // To avoid update() in QWidget::focusOutEvent.
3439}
3440
3441/*!
3442 \reimp
3443*/
3444void QMdiSubWindow::childEvent(QChildEvent *childEvent)
3445{
3446 if (childEvent->type() != QEvent::ChildPolished)
3447 return;
3448#if QT_CONFIG(sizegrip)
3449 if (QSizeGrip *sizeGrip = qobject_cast<QSizeGrip *>(childEvent->child()))
3450 d_func()->setSizeGrip(sizeGrip);
3451#endif
3452}
3453
3454/*!
3455 \reimp
3456*/
3457QSize QMdiSubWindow::sizeHint() const
3458{
3459 Q_D(const QMdiSubWindow);
3460 int margin, minWidth;
3461 d->sizeParameters(&margin, &minWidth);
3462 QSize size(2 * margin, d->titleBarHeight() + margin);
3463 if (d->baseWidget && d->baseWidget->sizeHint().isValid())
3464 size += d->baseWidget->sizeHint();
3465 return size.expandedTo(minimumSizeHint());
3466}
3467
3468/*!
3469 \reimp
3470*/
3471QSize QMdiSubWindow::minimumSizeHint() const
3472{
3473 Q_D(const QMdiSubWindow);
3474 if (isVisible())
3475 ensurePolished();
3476
3477 // Minimized window.
3478 if (parent() && isMinimized() && !isShaded())
3479 return d->iconSize();
3480
3481 // Calculate window decoration.
3482 int margin, minWidth;
3483 d->sizeParameters(&margin, &minWidth);
3484 int decorationHeight = margin + d->titleBarHeight();
3485 int minHeight = decorationHeight;
3486
3487 // Shaded window.
3488 if (parent() && isShaded())
3489 return QSize(qMax(minWidth, width()), d->titleBarHeight());
3490
3491 // Content
3492 if (layout()) {
3493 QSize minLayoutSize = layout()->minimumSize();
3494 if (minLayoutSize.isValid()) {
3495 minWidth = qMax(minWidth, minLayoutSize.width() + 2 * margin);
3496 minHeight += minLayoutSize.height();
3497 }
3498 } else if (d->baseWidget && d->baseWidget->isVisible()) {
3499 QSize minBaseWidgetSize = d->baseWidget->minimumSizeHint();
3500 if (minBaseWidgetSize.isValid()) {
3501 minWidth = qMax(minWidth, minBaseWidgetSize.width() + 2 * margin);
3502 minHeight += minBaseWidgetSize.height();
3503 }
3504 }
3505
3506#if QT_CONFIG(sizegrip)
3507 // SizeGrip
3508 int sizeGripHeight = 0;
3509 if (d->sizeGrip && d->sizeGrip->isVisibleTo(const_cast<QMdiSubWindow *>(this)))
3510 sizeGripHeight = d->sizeGrip->height();
3511 else if (parent() && isMacStyle(style()) && !d->sizeGrip)
3512 sizeGripHeight = style()->pixelMetric(QStyle::PM_SizeGripSize, nullptr, this);
3513 minHeight = qMax(minHeight, decorationHeight + sizeGripHeight);
3514#endif
3515
3516 return QSize(minWidth, minHeight);
3517}
3518
3519QT_END_NAMESPACE
3520
3521#include "moc_qmdisubwindow.cpp"
3522#include "qmdisubwindow.moc"
void updateWindowIcon(const QIcon &windowIcon)
ControlContainer(QMdiSubWindow *mdiChild)
QSize sizeHint() const override
bool event(QEvent *event) override
This virtual function receives events to an object and should return true if the event e was recogniz...
void mousePressEvent(QMouseEvent *mouseEvent) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse press events...
void mouseReleaseEvent(QMouseEvent *mouseEvent) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse release even...
void mouseDoubleClickEvent(QMouseEvent *mouseEvent) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse double click...
void paintEvent(QPaintEvent *paintEvent) override
This event handler can be reimplemented in a subclass to receive paint events passed in event.
bool event(QEvent *event) override
This virtual function receives events to an object and should return true if the event e was recogniz...
void mousePressEvent(QMouseEvent *event) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse press events...
void mouseReleaseEvent(QMouseEvent *event) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse release even...
void setControlVisible(QMdiSubWindowPrivate::WindowStateAction action, bool visible)
void mouseMoveEvent(QMouseEvent *event) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse move events ...
void leaveEvent(QEvent *event) override
This event handler can be reimplemented in a subclass to receive widget leave events which are passed...
QSize sizeHint() const override
bool hasVisibleControls() const
void paintEvent(QPaintEvent *event) override
This event handler can be reimplemented in a subclass to receive paint events passed in event.
Combined button and popup list for selecting options.
static const int BoundaryMargin
static bool isChildOfQMdiSubWindow(const QWidget *child)
static const int NumSubControls
static const QStyle::SubControl SubControls[]
constexpr auto operationMap
static int getMoveDeltaComponent(uint cflags, uint moveFlag, uint resizeFlag, int delta, int maxDelta, int minDelta)
static bool isHoverControl(QStyle::SubControl control)
static const Qt::WindowFlags CustomizeWindowFlags
static int getResizeDeltaComponent(uint cflags, uint resizeFlag, uint resizeReverseFlag, int delta)
static bool isMacStyle(QStyle *style)
QString qt_setWindowTitle_helperHelper(const QString &, const QWidget *)
Returns a modified window title with the [*] place holder replaced according to the rules described i...
Definition qwidget.cpp:6016
static ControlElement< T > * ptr(QWidget *widget)
static bool isChildOfTabbedQMdiArea(const QMdiSubWindow *child)