1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <QtFrame.hxx>
21 #include <QtFrame.moc>
24 #include <QtDragAndDrop.hxx>
25 #include <QtFontFace.hxx>
26 #include <QtGraphics.hxx>
27 #include <QtInstance.hxx>
28 #include <QtMainWindow.hxx>
30 #include <QtSvpGraphics.hxx>
31 #include <QtSystem.hxx>
32 #include <QtTools.hxx>
33 #include <QtTransferable.hxx>
34 #if CHECK_ANY_QT_USING_X11
35 #include <QtX11Support.hxx>
38 #include <QtCore/QMimeData>
39 #include <QtCore/QPoint>
40 #include <QtCore/QSize>
41 #include <QtCore/QThread>
42 #include <QtGui/QDragMoveEvent>
43 #include <QtGui/QDropEvent>
44 #include <QtGui/QIcon>
45 #include <QtGui/QWindow>
46 #include <QtGui/QScreen>
47 #include <QtWidgets/QStyle>
48 #include <QtWidgets/QToolTip>
49 #include <QtWidgets/QApplication>
50 #if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
51 #include <QtWidgets/QDesktopWidget>
53 #include <QtWidgets/QMenuBar>
54 #include <QtWidgets/QMainWindow>
55 #if CHECK_QT5_USING_X11
56 #include <QtX11Extras/QX11Info>
60 #include <vcl/syswin.hxx>
62 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
65 #include <headless/svpgdi.hxx>
67 #include <unx/fontmanager.hxx>
69 #if CHECK_QT5_USING_X11 && QT5_HAVE_XCB_ICCCM
70 static bool g_bNeedsWmHintsWindowGroup
= true;
73 static void SvpDamageHandler(void* handle
, sal_Int32 nExtentsX
, sal_Int32 nExtentsY
,
74 sal_Int32 nExtentsWidth
, sal_Int32 nExtentsHeight
)
76 QtFrame
* pThis
= static_cast<QtFrame
*>(handle
);
77 pThis
->Damage(nExtentsX
, nExtentsY
, nExtentsWidth
, nExtentsHeight
);
82 sal_Int32
screenNumber(const QScreen
* pScreen
)
84 const QList
<QScreen
*> screens
= QApplication::screens();
86 sal_Int32 nScreen
= 0;
88 for (const QScreen
* pCurScreen
: screens
)
90 if (pScreen
== pCurScreen
)
98 return bFound
? nScreen
: -1;
102 QtFrame::QtFrame(QtFrame
* pParent
, SalFrameStyleFlags nStyle
, bool bUseCairo
)
103 : m_pTopLevel(nullptr)
104 , m_bUseCairo(bUseCairo
)
105 , m_bNullRegion(true)
106 , m_bGraphicsInUse(false)
107 , m_ePointerStyle(PointerStyle::Arrow
)
108 , m_pDragSource(nullptr)
109 , m_pDropTarget(nullptr)
111 , m_bDefaultSize(true)
112 , m_bDefaultPos(true)
113 , m_bFullScreen(false)
114 , m_bFullScreenSpanAll(false)
115 #if CHECK_ANY_QT_USING_X11
116 , m_nKeyModifiers(ModKeyFlags::NONE
)
118 , m_nInputLanguage(LANGUAGE_DONTKNOW
)
120 QtInstance
* pInst
= GetQtInstance();
121 pInst
->insertFrame(this);
123 m_aDamageHandler
.handle
= this;
124 m_aDamageHandler
.damaged
= ::SvpDamageHandler
;
126 if (nStyle
& SalFrameStyleFlags::DEFAULT
) // ensure default style
128 nStyle
|= SalFrameStyleFlags::MOVEABLE
| SalFrameStyleFlags::SIZEABLE
129 | SalFrameStyleFlags::CLOSEABLE
;
130 nStyle
&= ~SalFrameStyleFlags::FLOAT
;
136 Qt::WindowFlags
aWinFlags(Qt::Widget
);
137 if (!(nStyle
& SalFrameStyleFlags::SYSTEMCHILD
))
139 if (nStyle
& SalFrameStyleFlags::INTRO
)
140 aWinFlags
= Qt::SplashScreen
;
141 // floating toolbars are frameless tool windows
142 // + they must be able to receive keyboard focus
143 else if ((nStyle
& SalFrameStyleFlags::FLOAT
)
144 && (nStyle
& SalFrameStyleFlags::OWNERDRAWDECORATION
))
145 aWinFlags
= Qt::Tool
| Qt::FramelessWindowHint
;
146 else if (nStyle
& SalFrameStyleFlags::TOOLTIP
)
147 aWinFlags
= Qt::ToolTip
;
148 // Can't use Qt::Popup, because it grabs the input focus and generates a focus-out event,
149 // instantly auto-closing the LO's editable ComboBox popup.
150 // On X11, the alternative Qt::Window | Qt::FramelessWindowHint | Qt::BypassWindowManagerHint
151 // seems to work well enough, but at least on Wayland and WASM, this results in problems.
152 // So while using Qt::ToolTip, the popups are wrongly advertised via accessibility, at least
153 // the GUI seems to work on all platforms... what a mess.
155 aWinFlags
= Qt::ToolTip
| Qt::FramelessWindowHint
;
156 else if (nStyle
& SalFrameStyleFlags::TOOLWINDOW
)
157 aWinFlags
= Qt::Tool
;
158 // top level windows can't be transient in Qt, so make them dialogs, if they have a parent. At least
159 // the plasma shell relies on this setting to skip dialogs in the window list. And Qt Xcb will just
160 // set transient for the types Dialog, Sheet, Tool, SplashScreen, ToolTip, Drawer and Popup.
161 else if (nStyle
& SalFrameStyleFlags::DIALOG
|| m_pParent
)
162 aWinFlags
= Qt::Dialog
;
164 aWinFlags
= Qt::Window
;
167 if (aWinFlags
== Qt::Window
)
169 m_pTopLevel
= new QtMainWindow(*this, aWinFlags
);
170 m_pQWidget
= new QtWidget(*this);
171 m_pTopLevel
->setCentralWidget(m_pQWidget
);
172 m_pTopLevel
->setFocusProxy(m_pQWidget
);
176 m_pQWidget
= new QtWidget(*this, aWinFlags
);
177 // from Qt's POV the popup window doesn't have the input focus, so we must force tooltips...
179 m_pQWidget
->setAttribute(Qt::WA_AlwaysShowToolTips
);
182 FillSystemEnvData(m_aSystemData
, reinterpret_cast<sal_IntPtr
>(this), m_pQWidget
);
184 QWindow
* pChildWindow
= windowHandle();
185 connect(pChildWindow
, &QWindow::screenChanged
, this, &QtFrame::screenChanged
);
187 if (pParent
&& !(pParent
->m_nStyle
& SalFrameStyleFlags::PLUG
))
189 QWindow
* pParentWindow
= pParent
->windowHandle();
190 if (pParentWindow
&& pChildWindow
&& (pParentWindow
!= pChildWindow
))
191 pChildWindow
->setTransientParent(pParentWindow
);
194 SetIcon(SV_ICON_ID_OFFICE
);
196 fixICCCMwindowGroup();
199 void QtFrame::screenChanged(QScreen
*) { m_pQWidget
->fakeResize(); }
201 void QtFrame::FillSystemEnvData(SystemEnvData
& rData
, sal_IntPtr pWindow
, QWidget
* pWidget
)
203 assert(rData
.platform
== SystemEnvData::Platform::Invalid
);
204 assert(rData
.toolkit
== SystemEnvData::Toolkit::Invalid
);
205 if (QGuiApplication::platformName() == "wayland")
206 rData
.platform
= SystemEnvData::Platform::Wayland
;
207 else if (QGuiApplication::platformName() == "xcb")
208 rData
.platform
= SystemEnvData::Platform::Xcb
;
209 else if (QGuiApplication::platformName() == "wasm")
210 rData
.platform
= SystemEnvData::Platform::WASM
;
213 // maybe add a SystemEnvData::Platform::Unsupported to avoid special cases and not abort?
215 "Unsupported qt VCL platform: " << toOUString(QGuiApplication::platformName()));
219 rData
.toolkit
= SystemEnvData::Toolkit::Qt
;
220 rData
.aShellWindow
= pWindow
;
221 rData
.pWidget
= pWidget
;
224 void QtFrame::fixICCCMwindowGroup()
226 #if CHECK_QT5_USING_X11 && QT5_HAVE_XCB_ICCCM
227 if (!g_bNeedsWmHintsWindowGroup
)
229 g_bNeedsWmHintsWindowGroup
= false;
231 assert(m_aSystemData
.platform
!= SystemEnvData::Platform::Invalid
);
232 if (m_aSystemData
.platform
!= SystemEnvData::Platform::Xcb
)
235 g_bNeedsWmHintsWindowGroup
= QtX11Support::fixICCCMwindowGroup(asChild()->winId());
237 (void)this; // avoid loplugin:staticmethods
243 QtInstance
* pInst
= GetQtInstance();
244 pInst
->eraseFrame(this);
246 m_aSystemData
.aShellWindow
= 0;
249 void QtFrame::Damage(sal_Int32 nExtentsX
, sal_Int32 nExtentsY
, sal_Int32 nExtentsWidth
,
250 sal_Int32 nExtentsHeight
) const
252 m_pQWidget
->update(scaledQRect(QRect(nExtentsX
, nExtentsY
, nExtentsWidth
, nExtentsHeight
),
253 1 / devicePixelRatioF()));
256 SalGraphics
* QtFrame::AcquireGraphics()
258 if (m_bGraphicsInUse
)
261 m_bGraphicsInUse
= true;
267 QSize aSize
= m_pQWidget
->size() * devicePixelRatioF();
268 m_pSvpGraphics
.reset(new QtSvpGraphics(this));
270 cairo_image_surface_create(CAIRO_FORMAT_ARGB32
, aSize
.width(), aSize
.height()));
271 m_pSvpGraphics
->setSurface(m_pSurface
.get(),
272 basegfx::B2IVector(aSize
.width(), aSize
.height()));
273 cairo_surface_set_user_data(m_pSurface
.get(), QtSvpGraphics::getDamageKey(),
274 &m_aDamageHandler
, nullptr);
276 return m_pSvpGraphics
.get();
282 m_pQtGraphics
.reset(new QtGraphics(this));
284 new QImage(m_pQWidget
->size() * devicePixelRatioF(), Qt_DefaultFormat32
));
285 m_pQImage
->fill(Qt::transparent
);
286 m_pQtGraphics
->ChangeQImage(m_pQImage
.get());
288 return m_pQtGraphics
.get();
292 void QtFrame::ReleaseGraphics(SalGraphics
* pSalGraph
)
296 assert(pSalGraph
== m_pSvpGraphics
.get());
298 assert(pSalGraph
== m_pQtGraphics
.get());
299 m_bGraphicsInUse
= false;
302 bool QtFrame::PostEvent(std::unique_ptr
<ImplSVEvent
> pData
)
304 QtInstance
* pInst
= GetQtInstance();
305 pInst
->PostEvent(this, pData
.release(), SalEvent::UserEvent
);
309 QWidget
* QtFrame::asChild() const
316 qreal
QtFrame::devicePixelRatioF() const { return asChild()->devicePixelRatioF(); }
318 bool QtFrame::isWindow() const { return asChild()->isWindow(); }
320 QWindow
* QtFrame::windowHandle() const
322 // set attribute 'Qt::WA_NativeWindow' first to make sure a window handle actually exists
323 QWidget
* pChild
= asChild();
324 assert(pChild
->window() == pChild
);
325 switch (m_aSystemData
.platform
)
327 case SystemEnvData::Platform::Wayland
:
328 case SystemEnvData::Platform::Xcb
:
329 pChild
->setAttribute(Qt::WA_NativeWindow
);
331 case SystemEnvData::Platform::WASM
:
332 // no idea, why Qt::WA_NativeWindow breaks the menubar for EMSCRIPTEN
334 case SystemEnvData::Platform::Invalid
:
338 return pChild
->windowHandle();
341 QScreen
* QtFrame::screen() const
343 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
344 return asChild()->screen();
346 // QWidget::screen only available from Qt 5.14 on, call windowHandle(),
347 // with the indirect result that mouse move events on Wayland will not be
348 // emitted reliably, s. QTBUG-75766
349 QWindow
* const pWindow
= windowHandle();
350 return pWindow
? pWindow
->screen() : nullptr;
354 bool QtFrame::isMinimized() const { return asChild()->isMinimized(); }
356 bool QtFrame::isMaximized() const { return asChild()->isMaximized(); }
358 void QtFrame::SetWindowStateImpl(Qt::WindowStates eState
)
360 return asChild()->setWindowState(eState
);
363 void QtFrame::SetTitle(const OUString
& rTitle
)
365 m_pQWidget
->window()->setWindowTitle(toQString(rTitle
));
368 void QtFrame::SetIcon(sal_uInt16 nIcon
)
371 & (SalFrameStyleFlags::PLUG
| SalFrameStyleFlags::SYSTEMCHILD
372 | SalFrameStyleFlags::FLOAT
| SalFrameStyleFlags::INTRO
373 | SalFrameStyleFlags::OWNERDRAWDECORATION
)
379 if (nIcon
== SV_ICON_ID_TEXT
)
380 appicon
= "libreoffice-writer";
381 else if (nIcon
== SV_ICON_ID_SPREADSHEET
)
382 appicon
= "libreoffice-calc";
383 else if (nIcon
== SV_ICON_ID_DRAWING
)
384 appicon
= "libreoffice-draw";
385 else if (nIcon
== SV_ICON_ID_PRESENTATION
)
386 appicon
= "libreoffice-impress";
387 else if (nIcon
== SV_ICON_ID_DATABASE
)
388 appicon
= "libreoffice-base";
389 else if (nIcon
== SV_ICON_ID_FORMULA
)
390 appicon
= "libreoffice-math";
392 appicon
= "libreoffice-startcenter";
394 QIcon aIcon
= QIcon::fromTheme(appicon
);
395 m_pQWidget
->window()->setWindowIcon(aIcon
);
398 void QtFrame::SetMenu(SalMenu
*) {}
400 void QtFrame::SetExtendedFrameStyle(SalExtStyle
/*nExtStyle*/) { /* not needed */}
402 void QtFrame::Show(bool bVisible
, bool bNoActivate
)
405 if (bVisible
== asChild()->isVisible())
408 auto* pSalInst(GetQtInstance());
411 if (!bVisible
) // hide
413 pSalInst
->RunInMainThread([this]() { asChild()->setVisible(false); });
420 pSalInst
->RunInMainThread([this, bNoActivate
]() {
421 QWidget
* const pChild
= asChild();
422 pChild
->setVisible(true);
426 pChild
->activateWindow();
432 void QtFrame::SetMinClientSize(tools::Long nWidth
, tools::Long nHeight
)
436 const qreal fRatio
= devicePixelRatioF();
437 asChild()->setMinimumSize(round(nWidth
/ fRatio
), round(nHeight
/ fRatio
));
441 void QtFrame::SetMaxClientSize(tools::Long nWidth
, tools::Long nHeight
)
445 const qreal fRatio
= devicePixelRatioF();
446 asChild()->setMaximumSize(round(nWidth
/ fRatio
), round(nHeight
/ fRatio
));
450 int QtFrame::menuBarOffset() const
452 QtMainWindow
* pTopLevel
= m_pParent
->GetTopLevelWindow();
453 if (pTopLevel
&& pTopLevel
->menuBar() && pTopLevel
->menuBar()->isVisible())
454 return round(pTopLevel
->menuBar()->geometry().height() * devicePixelRatioF());
458 void QtFrame::SetDefaultPos()
466 const qreal fRatio
= devicePixelRatioF();
467 QWidget
* const pParentWin
= m_pParent
->asChild()->window();
468 QWidget
* const pChildWin
= asChild()->window();
469 QPoint aPos
= (pParentWin
->rect().center() - pChildWin
->rect().center()) * fRatio
;
470 aPos
.ry() -= menuBarOffset();
471 SetPosSize(aPos
.x(), aPos
.y(), 0, 0, SAL_FRAME_POSSIZE_X
| SAL_FRAME_POSSIZE_Y
);
472 assert(!m_bDefaultPos
);
475 m_bDefaultPos
= false;
478 Size
QtFrame::CalcDefaultSize()
485 const QScreen
* pScreen
= screen();
487 pScreen
= QGuiApplication::screens().at(0);
488 aSize
= bestmaxFrameSizeForScreenSize(toSize(pScreen
->size()));
492 if (!m_bFullScreenSpanAll
)
494 aSize
= toSize(QGuiApplication::screens().at(maGeometry
.screen())->size());
498 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
499 QScreen
* pScreen
= QGuiApplication::screenAt(QPoint(0, 0));
501 // QGuiApplication::screenAt was added in Qt 5.10, use deprecated QDesktopWidget
502 int nLeftScreen
= QApplication::desktop()->screenNumber(QPoint(0, 0));
503 QScreen
* pScreen
= QGuiApplication::screens()[nLeftScreen
];
505 aSize
= toSize(pScreen
->availableVirtualGeometry().size());
512 void QtFrame::SetDefaultSize()
517 Size aDefSize
= CalcDefaultSize();
518 SetPosSize(0, 0, aDefSize
.Width(), aDefSize
.Height(),
519 SAL_FRAME_POSSIZE_WIDTH
| SAL_FRAME_POSSIZE_HEIGHT
);
520 assert(!m_bDefaultSize
);
523 void QtFrame::SetPosSize(tools::Long nX
, tools::Long nY
, tools::Long nWidth
, tools::Long nHeight
,
526 if (!isWindow() || isChild(true, false))
529 if (nFlags
& (SAL_FRAME_POSSIZE_WIDTH
| SAL_FRAME_POSSIZE_HEIGHT
))
531 if (isChild(false) || !m_pQWidget
->isMaximized())
533 if (!(nFlags
& SAL_FRAME_POSSIZE_WIDTH
))
534 nWidth
= maGeometry
.width();
535 else if (!(nFlags
& SAL_FRAME_POSSIZE_HEIGHT
))
536 nHeight
= maGeometry
.height();
538 if (nWidth
> 0 && nHeight
> 0)
540 m_bDefaultSize
= false;
541 const int nNewWidth
= round(nWidth
/ devicePixelRatioF());
542 const int nNewHeight
= round(nHeight
/ devicePixelRatioF());
543 if (m_nStyle
& SalFrameStyleFlags::SIZEABLE
)
544 asChild()->resize(nNewWidth
, nNewHeight
);
546 asChild()->setFixedSize(nNewWidth
, nNewHeight
);
549 // assume the resize happened
550 // needed for calculations and will eventually be corrected by events
552 maGeometry
.setWidth(nWidth
);
554 maGeometry
.setHeight(nHeight
);
558 if (!(nFlags
& (SAL_FRAME_POSSIZE_X
| SAL_FRAME_POSSIZE_Y
)))
560 if (nFlags
& (SAL_FRAME_POSSIZE_WIDTH
| SAL_FRAME_POSSIZE_HEIGHT
))
567 const SalFrameGeometry
& aParentGeometry
= m_pParent
->maGeometry
;
568 if (QGuiApplication::isRightToLeft())
569 nX
= aParentGeometry
.x() + aParentGeometry
.width() - nX
- maGeometry
.width() - 1;
571 nX
+= aParentGeometry
.x();
572 nY
+= aParentGeometry
.y() + menuBarOffset();
575 if (!(nFlags
& SAL_FRAME_POSSIZE_X
))
577 else if (!(nFlags
& SAL_FRAME_POSSIZE_Y
))
580 // assume the reposition happened
581 // needed for calculations and will eventually be corrected by events later
582 maGeometry
.setPos({ nX
, nY
});
584 m_bDefaultPos
= false;
585 asChild()->move(round(nX
/ devicePixelRatioF()), round(nY
/ devicePixelRatioF()));
588 void QtFrame::GetClientSize(tools::Long
& rWidth
, tools::Long
& rHeight
)
590 rWidth
= round(m_pQWidget
->width() * devicePixelRatioF());
591 rHeight
= round(m_pQWidget
->height() * devicePixelRatioF());
594 void QtFrame::GetWorkArea(tools::Rectangle
& rRect
)
598 QScreen
* pScreen
= screen();
602 QSize aSize
= pScreen
->availableVirtualSize() * devicePixelRatioF();
603 rRect
= tools::Rectangle(0, 0, aSize
.width(), aSize
.height());
606 SalFrame
* QtFrame::GetParent() const { return m_pParent
; }
608 void QtFrame::SetModal(bool bModal
)
610 if (!isWindow() || asChild()->isModal() == bModal
)
613 auto* pSalInst(GetQtInstance());
615 pSalInst
->RunInMainThread([this, bModal
]() {
617 QWidget
* const pChild
= asChild();
618 const bool bWasVisible
= pChild
->isVisible();
620 // modality change is only effective if the window is hidden
624 if (QGuiApplication::platformName() == "xcb")
626 SAL_WARN("vcl.qt", "SetModal called after Show - apply delay");
627 // tdf#152979 give QXcbConnection some time to avoid
628 // "qt.qpa.xcb: internal error: void QXcbWindow::setNetWmStateOnUnmappedWindow() called on mapped window"
629 QThread::msleep(100);
633 pChild
->setWindowModality(bModal
? Qt::WindowModal
: Qt::NonModal
);
640 bool QtFrame::GetModal() const { return isWindow() && windowHandle()->isModal(); }
642 void QtFrame::SetWindowState(const vcl::WindowData
* pState
)
644 if (!isWindow() || !pState
|| isChild(true, false))
647 const vcl::WindowDataMask nMaxGeometryMask
648 = vcl::WindowDataMask::PosSize
| vcl::WindowDataMask::MaximizedX
649 | vcl::WindowDataMask::MaximizedY
| vcl::WindowDataMask::MaximizedWidth
650 | vcl::WindowDataMask::MaximizedHeight
;
652 if ((pState
->mask() & vcl::WindowDataMask::State
)
653 && (pState
->state() & vcl::WindowState::Maximized
) && !isMaximized()
654 && (pState
->mask() & nMaxGeometryMask
) == nMaxGeometryMask
)
656 const qreal fRatio
= devicePixelRatioF();
657 QWidget
* const pChild
= asChild();
658 pChild
->resize(ceil(pState
->width() / fRatio
), ceil(pState
->height() / fRatio
));
659 pChild
->move(ceil(pState
->x() / fRatio
), ceil(pState
->y() / fRatio
));
660 SetWindowStateImpl(Qt::WindowMaximized
);
662 else if (pState
->mask() & vcl::WindowDataMask::PosSize
)
664 sal_uInt16 nPosSizeFlags
= 0;
665 if (pState
->mask() & vcl::WindowDataMask::X
)
666 nPosSizeFlags
|= SAL_FRAME_POSSIZE_X
;
667 if (pState
->mask() & vcl::WindowDataMask::Y
)
668 nPosSizeFlags
|= SAL_FRAME_POSSIZE_Y
;
669 if (pState
->mask() & vcl::WindowDataMask::Width
)
670 nPosSizeFlags
|= SAL_FRAME_POSSIZE_WIDTH
;
671 if (pState
->mask() & vcl::WindowDataMask::Height
)
672 nPosSizeFlags
|= SAL_FRAME_POSSIZE_HEIGHT
;
673 SetPosSize(pState
->x(), pState
->y(), pState
->width(), pState
->height(), nPosSizeFlags
);
675 else if (pState
->mask() & vcl::WindowDataMask::State
&& !isChild())
677 if (pState
->state() & vcl::WindowState::Maximized
)
678 SetWindowStateImpl(Qt::WindowMaximized
);
679 else if (pState
->state() & vcl::WindowState::Minimized
)
680 SetWindowStateImpl(Qt::WindowMinimized
);
682 SetWindowStateImpl(Qt::WindowNoState
);
686 bool QtFrame::GetWindowState(vcl::WindowData
* pState
)
688 pState
->setState(vcl::WindowState::Normal
);
689 pState
->setMask(vcl::WindowDataMask::State
);
691 pState
->rState() |= vcl::WindowState::Minimized
;
692 else if (isMaximized())
693 pState
->rState() |= vcl::WindowState::Maximized
;
696 // we want the frame position and the client area size
697 QRect rect
= scaledQRect({ asChild()->pos(), asChild()->size() }, devicePixelRatioF());
698 pState
->setPosSize(toRectangle(rect
));
699 pState
->rMask() |= vcl::WindowDataMask::PosSize
;
705 void QtFrame::ShowFullScreen(bool bFullScreen
, sal_Int32 nScreen
)
707 // only top-level windows can go fullscreen
710 if (m_bFullScreen
== bFullScreen
)
713 m_bFullScreen
= bFullScreen
;
714 m_bFullScreenSpanAll
= m_bFullScreen
&& (nScreen
< 0);
716 // show it if it isn't shown yet
722 m_aRestoreGeometry
= m_pTopLevel
->geometry();
723 m_nRestoreScreen
= maGeometry
.screen();
724 SetScreenNumber(m_bFullScreenSpanAll
? m_nRestoreScreen
: nScreen
);
725 if (!m_bFullScreenSpanAll
)
726 windowHandle()->showFullScreen();
728 windowHandle()->showNormal();
732 SetScreenNumber(m_nRestoreScreen
);
733 windowHandle()->showNormal();
734 m_pTopLevel
->setGeometry(m_aRestoreGeometry
);
738 void QtFrame::StartPresentation(bool bStart
)
740 #if CHECK_ANY_QT_USING_X11
741 // meh - so there's no Qt platform independent solution
742 // https://forum.qt.io/topic/38504/solved-qdialog-in-fullscreen-disable-os-screensaver
743 assert(m_aSystemData
.platform
!= SystemEnvData::Platform::Invalid
);
744 unsigned int nRootWindow(0);
745 std::optional
<Display
*> aDisplay
;
747 #if CHECK_QT5_USING_X11
748 if (QX11Info::isPlatformX11())
750 nRootWindow
= QX11Info::appRootWindow();
751 aDisplay
= QX11Info::display();
755 m_SessionManagerInhibitor
.inhibit(bStart
, u
"presentation", APPLICATION_INHIBIT_IDLE
,
756 nRootWindow
, aDisplay
);
762 void QtFrame::SetAlwaysOnTop(bool bOnTop
)
764 QWidget
* const pWidget
= asChild();
765 const Qt::WindowFlags flags
= pWidget
->windowFlags();
767 pWidget
->setWindowFlags(flags
| Qt::CustomizeWindowHint
| Qt::WindowStaysOnTopHint
);
769 pWidget
->setWindowFlags(flags
& ~(Qt::CustomizeWindowHint
| Qt::WindowStaysOnTopHint
));
772 void QtFrame::ToTop(SalFrameToTop nFlags
)
774 QWidget
* const pWidget
= asChild();
775 if (isWindow() && !(nFlags
& SalFrameToTop::GrabFocusOnly
))
777 if ((nFlags
& SalFrameToTop::RestoreWhenMin
) || (nFlags
& SalFrameToTop::ForegroundTask
))
779 if (nFlags
& SalFrameToTop::RestoreWhenMin
)
780 pWidget
->setWindowState(pWidget
->windowState() & ~Qt::WindowMinimized
);
781 pWidget
->activateWindow();
783 else if ((nFlags
& SalFrameToTop::GrabFocus
) || (nFlags
& SalFrameToTop::GrabFocusOnly
))
785 if (!(nFlags
& SalFrameToTop::GrabFocusOnly
))
786 pWidget
->activateWindow();
787 pWidget
->setFocus(Qt::OtherFocusReason
);
791 void QtFrame::SetPointer(PointerStyle ePointerStyle
)
793 if (ePointerStyle
== m_ePointerStyle
)
795 m_ePointerStyle
= ePointerStyle
;
797 m_pQWidget
->setCursor(GetQtData()->getCursor(ePointerStyle
));
800 void QtFrame::CaptureMouse(bool bMouse
)
802 static const char* pEnv
= getenv("SAL_NO_MOUSEGRABS");
807 m_pQWidget
->grabMouse();
809 m_pQWidget
->releaseMouse();
812 void QtFrame::SetPointerPos(tools::Long nX
, tools::Long nY
)
814 // some cursor already exists (and it has m_ePointerStyle shape)
815 // so here we just reposition it
816 QCursor::setPos(m_pQWidget
->mapToGlobal(QPoint(nX
, nY
) / devicePixelRatioF()));
819 void QtFrame::Flush()
821 // was: QGuiApplication::sync();
822 // but FIXME it causes too many issues, figure out sth better
824 // unclear if we need to also flush cairo surface - gtk3 backend
825 // does not do it. QPainter in QtWidget::paintEvent() is
826 // destroyed, so that state should be safely flushed.
829 bool QtFrame::ShowTooltip(const OUString
& rText
, const tools::Rectangle
& rHelpArea
)
831 QRect
aHelpArea(toQRect(rHelpArea
));
832 if (QGuiApplication::isRightToLeft())
833 aHelpArea
.moveLeft(maGeometry
.width() - aHelpArea
.width() - aHelpArea
.left() - 1);
834 m_aTooltipText
= rText
;
835 m_aTooltipArea
= aHelpArea
;
839 void QtFrame::SetInputContext(SalInputContext
* pContext
)
844 if (!(pContext
->mnOptions
& InputContextFlags::Text
))
847 m_pQWidget
->setAttribute(Qt::WA_InputMethodEnabled
);
850 void QtFrame::EndExtTextInput(EndExtTextInputFlags
/*nFlags*/)
853 m_pQWidget
->endExtTextInput();
856 OUString
QtFrame::GetKeyName(sal_uInt16 nKeyCode
)
858 vcl::KeyCode
vclKeyCode(nKeyCode
);
859 int nCode
= vclKeyCode
.GetCode();
862 if (nCode
>= KEY_0
&& nCode
<= KEY_9
)
863 nRetCode
= (nCode
- KEY_0
) + Qt::Key_0
;
864 else if (nCode
>= KEY_A
&& nCode
<= KEY_Z
)
865 nRetCode
= (nCode
- KEY_A
) + Qt::Key_A
;
866 else if (nCode
>= KEY_F1
&& nCode
<= KEY_F26
)
867 nRetCode
= (nCode
- KEY_F1
) + Qt::Key_F1
;
873 nRetCode
= Qt::Key_Down
;
876 nRetCode
= Qt::Key_Up
;
879 nRetCode
= Qt::Key_Left
;
882 nRetCode
= Qt::Key_Right
;
885 nRetCode
= Qt::Key_Home
;
888 nRetCode
= Qt::Key_End
;
891 nRetCode
= Qt::Key_PageUp
;
894 nRetCode
= Qt::Key_PageDown
;
897 nRetCode
= Qt::Key_Return
;
900 nRetCode
= Qt::Key_Escape
;
903 nRetCode
= Qt::Key_Tab
;
906 nRetCode
= Qt::Key_Backspace
;
909 nRetCode
= Qt::Key_Space
;
912 nRetCode
= Qt::Key_Insert
;
915 nRetCode
= Qt::Key_Delete
;
918 nRetCode
= Qt::Key_Plus
;
921 nRetCode
= Qt::Key_Minus
;
924 nRetCode
= Qt::Key_Asterisk
;
927 nRetCode
= Qt::Key_Slash
;
930 nRetCode
= Qt::Key_Period
;
933 nRetCode
= Qt::Key_Comma
;
936 nRetCode
= Qt::Key_Less
;
939 nRetCode
= Qt::Key_Greater
;
942 nRetCode
= Qt::Key_Equal
;
945 nRetCode
= Qt::Key_Find
;
947 case KEY_CONTEXTMENU
:
948 nRetCode
= Qt::Key_Menu
;
951 nRetCode
= Qt::Key_Help
;
954 nRetCode
= Qt::Key_Undo
;
957 nRetCode
= Qt::Key_Redo
;
960 nRetCode
= Qt::Key_AsciiTilde
;
963 nRetCode
= Qt::Key_QuoteLeft
;
965 case KEY_BRACKETLEFT
:
966 nRetCode
= Qt::Key_BracketLeft
;
968 case KEY_BRACKETRIGHT
:
969 nRetCode
= Qt::Key_BracketRight
;
972 nRetCode
= Qt::Key_Colon
;
975 nRetCode
= Qt::Key_Semicolon
;
980 nRetCode
= Qt::Key_Copy
;
983 nRetCode
= Qt::Key_Cut
;
986 nRetCode
= Qt::Key_Paste
;
989 nRetCode
= Qt::Key_Open
;
994 if (vclKeyCode
.IsShift())
995 nRetCode
+= Qt::SHIFT
;
996 if (vclKeyCode
.IsMod1())
997 nRetCode
+= Qt::CTRL
;
998 if (vclKeyCode
.IsMod2())
1001 QKeySequence
keySeq(nRetCode
);
1002 OUString sKeyName
= toOUString(keySeq
.toString());
1007 bool QtFrame::MapUnicodeToKeyCode(sal_Unicode
/*aUnicode*/, LanguageType
/*aLangType*/,
1008 vcl::KeyCode
& /*rKeyCode*/)
1010 // not supported yet
1014 LanguageType
QtFrame::GetInputLanguage() { return m_nInputLanguage
; }
1016 void QtFrame::setInputLanguage(LanguageType nInputLanguage
)
1018 if (nInputLanguage
== m_nInputLanguage
)
1020 m_nInputLanguage
= nInputLanguage
;
1021 CallCallback(SalEvent::InputLanguageChange
, nullptr);
1024 static Color
toColor(const QColor
& rColor
)
1026 return Color(rColor
.red(), rColor
.green(), rColor
.blue());
1029 static bool toVclFont(const QFont
& rQFont
, const css::lang::Locale
& rLocale
, vcl::Font
& rVclFont
)
1031 psp::FastPrintFontInfo aInfo
;
1032 QFontInfo
qFontInfo(rQFont
);
1034 OUString sFamilyName
= toOUString(rQFont
.family());
1035 aInfo
.m_aFamilyName
= sFamilyName
;
1036 aInfo
.m_eItalic
= QtFontFace::toFontItalic(qFontInfo
.style());
1037 aInfo
.m_eWeight
= QtFontFace::toFontWeight(qFontInfo
.weight());
1038 aInfo
.m_eWidth
= QtFontFace::toFontWidth(rQFont
.stretch());
1040 psp::PrintFontManager::get().matchFont(aInfo
, rLocale
);
1041 SAL_INFO("vcl.qt", "font match result for '"
1042 << sFamilyName
<< "': "
1043 << (aInfo
.m_nID
!= 0 ? OUString::Concat("'") + aInfo
.m_aFamilyName
+ "'"
1044 : OUString("failed")));
1046 if (aInfo
.m_nID
== 0)
1049 int nPointHeight
= qFontInfo
.pointSize();
1050 if (nPointHeight
<= 0)
1051 nPointHeight
= rQFont
.pointSize();
1053 vcl::Font
aFont(aInfo
.m_aFamilyName
, Size(0, nPointHeight
));
1054 if (aInfo
.m_eWeight
!= WEIGHT_DONTKNOW
)
1055 aFont
.SetWeight(aInfo
.m_eWeight
);
1056 if (aInfo
.m_eWidth
!= WIDTH_DONTKNOW
)
1057 aFont
.SetWidthType(aInfo
.m_eWidth
);
1058 if (aInfo
.m_eItalic
!= ITALIC_DONTKNOW
)
1059 aFont
.SetItalic(aInfo
.m_eItalic
);
1060 if (aInfo
.m_ePitch
!= PITCH_DONTKNOW
)
1061 aFont
.SetPitch(aInfo
.m_ePitch
);
1067 void QtFrame::UpdateSettings(AllSettings
& rSettings
)
1069 if (QtData::noNativeControls())
1072 StyleSettings
style(rSettings
.GetStyleSettings());
1073 const css::lang::Locale aLocale
= rSettings
.GetUILanguageTag().getLocale();
1076 QPalette pal
= QApplication::palette();
1078 style
.SetToolbarIconSize(ToolbarIconSize::Large
);
1080 Color aFore
= toColor(pal
.color(QPalette::Active
, QPalette::WindowText
));
1081 Color aBack
= toColor(pal
.color(QPalette::Active
, QPalette::Window
));
1082 Color aText
= toColor(pal
.color(QPalette::Active
, QPalette::Text
));
1083 Color aBase
= toColor(pal
.color(QPalette::Active
, QPalette::Base
));
1084 Color aButn
= toColor(pal
.color(QPalette::Active
, QPalette::ButtonText
));
1085 Color aMid
= toColor(pal
.color(QPalette::Active
, QPalette::Mid
));
1086 Color aHigh
= toColor(pal
.color(QPalette::Active
, QPalette::Highlight
));
1087 Color aHighText
= toColor(pal
.color(QPalette::Active
, QPalette::HighlightedText
));
1088 Color aLink
= toColor(pal
.color(QPalette::Active
, QPalette::Link
));
1089 Color aVisitedLink
= toColor(pal
.color(QPalette::Active
, QPalette::LinkVisited
));
1091 style
.SetSkipDisabledInMenus(true);
1094 style
.SetRadioCheckTextColor(aFore
);
1095 style
.SetLabelTextColor(aFore
);
1096 style
.SetDialogTextColor(aFore
);
1097 style
.SetGroupTextColor(aFore
);
1100 style
.SetFieldTextColor(aText
);
1101 style
.SetFieldRolloverTextColor(aText
);
1102 style
.SetListBoxWindowTextColor(aText
);
1103 style
.SetWindowTextColor(aText
);
1104 style
.SetToolTextColor(aText
);
1107 style
.SetFieldColor(aBase
);
1108 style
.SetWindowColor(aBase
);
1109 style
.SetActiveTabColor(aBase
);
1110 style
.SetListBoxWindowBackgroundColor(aBase
);
1111 style
.SetAlternatingRowColor(toColor(pal
.color(QPalette::Active
, QPalette::AlternateBase
)));
1114 style
.SetDefaultButtonTextColor(aButn
);
1115 style
.SetButtonTextColor(aButn
);
1116 style
.SetDefaultActionButtonTextColor(aButn
);
1117 style
.SetActionButtonTextColor(aButn
);
1118 style
.SetFlatButtonTextColor(aButn
);
1119 style
.SetDefaultButtonRolloverTextColor(aButn
);
1120 style
.SetButtonRolloverTextColor(aButn
);
1121 style
.SetDefaultActionButtonRolloverTextColor(aButn
);
1122 style
.SetActionButtonRolloverTextColor(aButn
);
1123 style
.SetFlatButtonRolloverTextColor(aButn
);
1124 style
.SetDefaultButtonPressedRolloverTextColor(aButn
);
1125 style
.SetButtonPressedRolloverTextColor(aButn
);
1126 style
.SetDefaultActionButtonPressedRolloverTextColor(aButn
);
1127 style
.SetActionButtonPressedRolloverTextColor(aButn
);
1128 style
.SetFlatButtonPressedRolloverTextColor(aButn
);
1131 style
.SetTabTextColor(aButn
);
1132 style
.SetTabRolloverTextColor(aButn
);
1133 style
.SetTabHighlightTextColor(aButn
);
1136 style
.SetDisableColor(toColor(pal
.color(QPalette::Disabled
, QPalette::WindowText
)));
1139 style
.BatchSetBackgrounds(aBack
);
1140 style
.SetInactiveTabColor(aBack
);
1143 style
.SetWorkspaceColor(aMid
);
1146 // https://invent.kde.org/plasma/plasma-workspace/-/merge_requests/305
1147 style
.SetAccentColor(aHigh
);
1148 style
.SetHighlightColor(aHigh
);
1149 style
.SetHighlightTextColor(aHighText
);
1150 style
.SetListBoxWindowHighlightColor(aHigh
);
1151 style
.SetListBoxWindowHighlightTextColor(aHighText
);
1152 style
.SetActiveColor(aHigh
);
1153 style
.SetActiveTextColor(aHighText
);
1156 style
.SetLinkColor(aLink
);
1157 style
.SetVisitedLinkColor(aVisitedLink
);
1160 style
.SetHelpColor(toColor(QToolTip::palette().color(QPalette::Active
, QPalette::ToolTipBase
)));
1161 style
.SetHelpTextColor(
1162 toColor(QToolTip::palette().color(QPalette::Active
, QPalette::ToolTipText
)));
1165 std::unique_ptr
<QMenuBar
> pMenuBar
= std::make_unique
<QMenuBar
>();
1166 QPalette qMenuCG
= pMenuBar
->palette();
1168 // Menu text and background color, theme specific
1169 Color aMenuFore
= toColor(qMenuCG
.color(QPalette::WindowText
));
1170 Color aMenuBack
= toColor(qMenuCG
.color(QPalette::Window
));
1172 style
.SetMenuTextColor(aMenuFore
);
1173 style
.SetMenuBarTextColor(style
.GetPersonaMenuBarTextColor().value_or(aMenuFore
));
1174 style
.SetMenuColor(aMenuBack
);
1175 style
.SetMenuBarColor(aMenuBack
);
1176 style
.SetMenuHighlightColor(toColor(qMenuCG
.color(QPalette::Highlight
)));
1177 style
.SetMenuHighlightTextColor(toColor(qMenuCG
.color(QPalette::HighlightedText
)));
1179 // set special menubar highlight text color
1180 if (QApplication::style()->inherits("HighContrastStyle"))
1181 ImplGetSVData()->maNWFData
.maMenuBarHighlightTextColor
1182 = toColor(qMenuCG
.color(QPalette::HighlightedText
));
1184 ImplGetSVData()->maNWFData
.maMenuBarHighlightTextColor
= aMenuFore
;
1186 // set menubar rollover color
1187 if (pMenuBar
->style()->styleHint(QStyle::SH_MenuBar_MouseTracking
))
1189 style
.SetMenuBarRolloverColor(toColor(qMenuCG
.color(QPalette::Highlight
)));
1190 style
.SetMenuBarRolloverTextColor(ImplGetSVData()->maNWFData
.maMenuBarHighlightTextColor
);
1194 style
.SetMenuBarRolloverColor(aMenuBack
);
1195 style
.SetMenuBarRolloverTextColor(aMenuFore
);
1197 style
.SetMenuBarHighlightTextColor(style
.GetMenuHighlightTextColor());
1201 if (toVclFont(QApplication::font(), aLocale
, aFont
))
1203 style
.BatchSetFonts(aFont
, aFont
);
1204 aFont
.SetWeight(WEIGHT_BOLD
);
1205 style
.SetTitleFont(aFont
);
1206 style
.SetFloatTitleFont(aFont
);
1210 if (toVclFont(QToolTip::font(), aLocale
, aFont
))
1211 style
.SetHelpFont(aFont
);
1214 if (toVclFont(pMenuBar
->font(), aLocale
, aFont
))
1215 style
.SetMenuFont(aFont
);
1218 style
.SetPreferredIconTheme(toOUString(QIcon::themeName()));
1221 style
.SetScrollBarSize(QApplication::style()->pixelMetric(QStyle::PM_ScrollBarExtent
));
1222 style
.SetMinThumbSize(QApplication::style()->pixelMetric(QStyle::PM_ScrollBarSliderMin
));
1224 // These colors are used for the ruler text and marks
1225 style
.SetShadowColor(toColor(pal
.color(QPalette::Disabled
, QPalette::WindowText
)));
1226 style
.SetDarkShadowColor(toColor(pal
.color(QPalette::Inactive
, QPalette::WindowText
)));
1228 // Cursor blink interval
1229 int nFlashTime
= QApplication::cursorFlashTime();
1230 style
.SetCursorBlinkTime(nFlashTime
!= 0 ? nFlashTime
/ 2 : STYLE_CURSOR_NOBLINKTIME
);
1232 rSettings
.SetStyleSettings(style
);
1235 void QtFrame::Beep() { QApplication::beep(); }
1237 SalFrame::SalPointerState
QtFrame::GetPointerState()
1239 SalPointerState aState
;
1240 aState
.maPos
= toPoint(QCursor::pos() * devicePixelRatioF());
1241 aState
.maPos
.Move(-maGeometry
.x(), -maGeometry
.y());
1242 aState
.mnState
= GetMouseModCode(QGuiApplication::mouseButtons())
1243 | GetKeyModCode(QGuiApplication::keyboardModifiers());
1247 KeyIndicatorState
QtFrame::GetIndicatorState() { return KeyIndicatorState(); }
1249 void QtFrame::SimulateKeyPress(sal_uInt16 nKeyCode
)
1251 SAL_WARN("vcl.qt", "missing simulate keypress " << nKeyCode
);
1254 // don't set QWidget parents; this breaks popups on Wayland, like the LO ComboBox or ColorPicker!
1255 void QtFrame::SetParent(SalFrame
* pNewParent
) { m_pParent
= static_cast<QtFrame
*>(pNewParent
); }
1257 void QtFrame::SetPluginParent(SystemParentData
* /*pNewParent*/)
1259 //FIXME: no SetPluginParent impl. for qt5
1262 void QtFrame::ResetClipRegion() { m_bNullRegion
= true; }
1264 void QtFrame::BeginSetClipRegion(sal_uInt32
)
1266 m_aRegion
= QRegion(QRect(QPoint(0, 0), m_pQWidget
->size()));
1269 void QtFrame::UnionClipRegion(tools::Long nX
, tools::Long nY
, tools::Long nWidth
,
1270 tools::Long nHeight
)
1273 = m_aRegion
.united(scaledQRect(QRect(nX
, nY
, nWidth
, nHeight
), 1 / devicePixelRatioF()));
1276 void QtFrame::EndSetClipRegion() { m_bNullRegion
= false; }
1278 void QtFrame::SetScreenNumber(unsigned int nScreen
)
1283 QWindow
* const pWindow
= windowHandle();
1287 QList
<QScreen
*> screens
= QApplication::screens();
1288 if (static_cast<int>(nScreen
) < screens
.size() || m_bFullScreenSpanAll
)
1292 if (!m_bFullScreenSpanAll
)
1294 screenGeo
= QGuiApplication::screens().at(nScreen
)->geometry();
1295 pWindow
->setScreen(QApplication::screens()[nScreen
]);
1297 else // special case: fullscreen over all available screens
1299 assert(m_bFullScreen
);
1301 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
1302 QScreen
* pScreen
= QGuiApplication::screenAt(QPoint(0, 0));
1304 // QGuiApplication::screenAt was added in Qt 5.10, use deprecated QDesktopWidget
1305 int nLeftScreen
= QApplication::desktop()->screenNumber(QPoint(0, 0));
1306 QScreen
* pScreen
= QGuiApplication::screens()[nLeftScreen
];
1308 // entire virtual desktop
1309 screenGeo
= pScreen
->availableVirtualGeometry();
1310 pWindow
->setScreen(pScreen
);
1311 pWindow
->setGeometry(screenGeo
);
1312 nScreen
= screenNumber(pScreen
);
1315 // setScreen by itself has no effect, explicitly move the widget to
1317 asChild()->move(screenGeo
.topLeft());
1321 // index outta bounds, use primary screen
1322 QScreen
* primaryScreen
= QApplication::primaryScreen();
1323 pWindow
->setScreen(primaryScreen
);
1324 nScreen
= static_cast<sal_uInt32
>(screenNumber(primaryScreen
));
1327 maGeometry
.setScreen(nScreen
);
1330 void QtFrame::SetApplicationID(const OUString
& rWMClass
)
1332 #if CHECK_QT5_USING_X11
1333 assert(m_aSystemData
.platform
!= SystemEnvData::Platform::Invalid
);
1334 if (m_aSystemData
.platform
!= SystemEnvData::Platform::Xcb
|| !m_pTopLevel
)
1337 QtX11Support::setApplicationID(m_pTopLevel
->winId(), rWMClass
);
1343 void QtFrame::ResolveWindowHandle(SystemEnvData
& rData
) const
1347 assert(rData
.platform
!= SystemEnvData::Platform::Invalid
);
1348 if (rData
.platform
!= SystemEnvData::Platform::Wayland
)
1349 rData
.SetWindowHandle(static_cast<QWidget
*>(rData
.pWidget
)->winId());
1354 void QtFrame::registerDragSource(QtDragSource
* pDragSource
)
1356 assert(!m_pDragSource
);
1357 m_pDragSource
= pDragSource
;
1360 void QtFrame::deregisterDragSource(QtDragSource
const* pDragSource
)
1362 assert(m_pDragSource
== pDragSource
);
1364 m_pDragSource
= nullptr;
1367 void QtFrame::registerDropTarget(QtDropTarget
* pDropTarget
)
1369 assert(!m_pDropTarget
);
1370 m_pDropTarget
= pDropTarget
;
1371 m_pQWidget
->setAcceptDrops(true);
1374 void QtFrame::deregisterDropTarget(QtDropTarget
const* pDropTarget
)
1376 assert(m_pDropTarget
== pDropTarget
);
1378 m_pDropTarget
= nullptr;
1381 static css::uno::Reference
<css::datatransfer::XTransferable
>
1382 lcl_getXTransferable(const QMimeData
* pMimeData
)
1384 css::uno::Reference
<css::datatransfer::XTransferable
> xTransferable
;
1385 const QtMimeData
* pQtMimeData
= dynamic_cast<const QtMimeData
*>(pMimeData
);
1387 xTransferable
= new QtDnDTransferable(pMimeData
);
1389 xTransferable
= pQtMimeData
->xTransferable();
1390 return xTransferable
;
1393 static sal_Int8
lcl_getUserDropAction(const QDropEvent
* pEvent
, const sal_Int8 nSourceActions
,
1394 const QMimeData
* pMimeData
)
1396 // we completely ignore all proposals by the Qt event, as they don't
1397 // match at all with the preferred LO DnD actions.
1398 // check the key modifiers to detect a user-overridden DnD action
1399 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
1400 const Qt::KeyboardModifiers eKeyMod
= pEvent
->modifiers();
1402 const Qt::KeyboardModifiers eKeyMod
= pEvent
->keyboardModifiers();
1404 sal_Int8 nUserDropAction
= 0;
1405 if ((eKeyMod
& Qt::ShiftModifier
) && !(eKeyMod
& Qt::ControlModifier
))
1406 nUserDropAction
= css::datatransfer::dnd::DNDConstants::ACTION_MOVE
;
1407 else if ((eKeyMod
& Qt::ControlModifier
) && !(eKeyMod
& Qt::ShiftModifier
))
1408 nUserDropAction
= css::datatransfer::dnd::DNDConstants::ACTION_COPY
;
1409 else if ((eKeyMod
& Qt::ShiftModifier
) && (eKeyMod
& Qt::ControlModifier
))
1410 nUserDropAction
= css::datatransfer::dnd::DNDConstants::ACTION_LINK
;
1411 nUserDropAction
&= nSourceActions
;
1413 // select the default DnD action, if there isn't a user preference
1414 if (0 == nUserDropAction
)
1416 // default LO internal action is move, but default external action is copy
1417 nUserDropAction
= dynamic_cast<const QtMimeData
*>(pMimeData
)
1418 ? css::datatransfer::dnd::DNDConstants::ACTION_MOVE
1419 : css::datatransfer::dnd::DNDConstants::ACTION_COPY
;
1420 nUserDropAction
&= nSourceActions
;
1422 // if the default doesn't match any allowed source action, fall back to the
1423 // preferred of all allowed source actions
1424 if (0 == nUserDropAction
)
1425 nUserDropAction
= toVclDropAction(getPreferredDropAction(nSourceActions
));
1427 // this is "our" preference, but actually we would even prefer any default,
1429 nUserDropAction
|= css::datatransfer::dnd::DNDConstants::ACTION_DEFAULT
;
1431 return nUserDropAction
;
1434 void QtFrame::handleDragMove(QDragMoveEvent
* pEvent
)
1436 assert(m_pDropTarget
);
1438 // prepare our suggested drop action for the drop target
1439 const sal_Int8 nSourceActions
= toVclDropActions(pEvent
->possibleActions());
1440 const QMimeData
* pMimeData
= pEvent
->mimeData();
1441 const sal_Int8 nUserDropAction
= lcl_getUserDropAction(pEvent
, nSourceActions
, pMimeData
);
1443 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
1444 const Point aPos
= toPoint(pEvent
->position().toPoint() * devicePixelRatioF());
1446 const Point aPos
= toPoint(pEvent
->pos() * devicePixelRatioF());
1449 css::datatransfer::dnd::DropTargetDragEnterEvent aEvent
;
1450 aEvent
.Source
= static_cast<css::datatransfer::dnd::XDropTarget
*>(m_pDropTarget
);
1451 aEvent
.Context
= static_cast<css::datatransfer::dnd::XDropTargetDragContext
*>(m_pDropTarget
);
1452 aEvent
.LocationX
= aPos
.X();
1453 aEvent
.LocationY
= aPos
.Y();
1454 aEvent
.DropAction
= nUserDropAction
;
1455 aEvent
.SourceActions
= nSourceActions
;
1457 // ask the drop target to accept our drop action
1460 aEvent
.SupportedDataFlavors
= lcl_getXTransferable(pMimeData
)->getTransferDataFlavors();
1461 m_pDropTarget
->fire_dragEnter(aEvent
);
1465 m_pDropTarget
->fire_dragOver(aEvent
);
1467 // the drop target accepted our drop action => inform Qt
1468 if (m_pDropTarget
->proposedDropAction() != 0)
1470 pEvent
->setDropAction(getPreferredDropAction(m_pDropTarget
->proposedDropAction()));
1473 else // or maybe someone else likes it?
1477 void QtFrame::handleDrop(QDropEvent
* pEvent
)
1479 assert(m_pDropTarget
);
1481 // prepare our suggested drop action for the drop target
1482 const sal_Int8 nSourceActions
= toVclDropActions(pEvent
->possibleActions());
1483 const sal_Int8 nUserDropAction
1484 = lcl_getUserDropAction(pEvent
, nSourceActions
, pEvent
->mimeData());
1486 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
1487 const Point aPos
= toPoint(pEvent
->position().toPoint() * devicePixelRatioF());
1489 const Point aPos
= toPoint(pEvent
->pos() * devicePixelRatioF());
1492 css::datatransfer::dnd::DropTargetDropEvent aEvent
;
1493 aEvent
.Source
= static_cast<css::datatransfer::dnd::XDropTarget
*>(m_pDropTarget
);
1494 aEvent
.Context
= static_cast<css::datatransfer::dnd::XDropTargetDropContext
*>(m_pDropTarget
);
1495 aEvent
.LocationX
= aPos
.X();
1496 aEvent
.LocationY
= aPos
.Y();
1497 aEvent
.SourceActions
= nSourceActions
;
1498 aEvent
.DropAction
= nUserDropAction
;
1499 aEvent
.Transferable
= lcl_getXTransferable(pEvent
->mimeData());
1501 // ask the drop target to accept our drop action
1502 m_pDropTarget
->fire_drop(aEvent
);
1505 const bool bDropSuccessful
= m_pDropTarget
->dropSuccessful();
1506 const sal_Int8 nDropAction
= m_pDropTarget
->proposedDropAction();
1508 // inform the drag source of the drag-origin frame of the drop result
1509 if (pEvent
->source())
1511 QtWidget
* pWidget
= dynamic_cast<QtWidget
*>(pEvent
->source());
1512 assert(pWidget
); // AFAIK there shouldn't be any non-Qt5Widget as source in LO itself
1514 pWidget
->frame().m_pDragSource
->fire_dragEnd(nDropAction
, bDropSuccessful
);
1517 // the drop target accepted our drop action => inform Qt
1518 if (bDropSuccessful
)
1520 pEvent
->setDropAction(getPreferredDropAction(nDropAction
));
1523 else // or maybe someone else likes it?
1527 void QtFrame::handleDragLeave()
1529 css::datatransfer::dnd::DropTargetEvent aEvent
;
1530 aEvent
.Source
= static_cast<css::datatransfer::dnd::XDropTarget
*>(m_pDropTarget
);
1531 m_pDropTarget
->fire_dragExit(aEvent
);
1535 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */