bump product version to 6.4.0.3
[LibreOffice.git] / vcl / qt5 / Qt5Frame.cxx
blob919336553d07e8531b164c40add981234087432c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <Qt5Frame.hxx>
21 #include <Qt5Frame.moc>
23 #include <Qt5Data.hxx>
24 #include <Qt5DragAndDrop.hxx>
25 #include <Qt5Graphics.hxx>
26 #include <Qt5Instance.hxx>
27 #include <Qt5MainWindow.hxx>
28 #include <Qt5Menu.hxx>
29 #include <Qt5SvpGraphics.hxx>
30 #include <Qt5System.hxx>
31 #include <Qt5Tools.hxx>
32 #include <Qt5Transferable.hxx>
33 #include <Qt5Widget.hxx>
35 #include <QtCore/QMimeData>
36 #include <QtCore/QPoint>
37 #include <QtCore/QSize>
38 #include <QtCore/QThread>
39 #include <QtCore/QVersionNumber>
40 #include <QtGui/QDragMoveEvent>
41 #include <QtGui/QDropEvent>
42 #include <QtGui/QIcon>
43 #include <QtGui/QWindow>
44 #include <QtGui/QScreen>
45 #include <QtWidgets/QStyle>
46 #include <QtWidgets/QToolTip>
47 #include <QtWidgets/QApplication>
48 #include <QtWidgets/QDesktopWidget>
49 #include <QtWidgets/QMenuBar>
50 #include <QtWidgets/QMainWindow>
52 #if QT5_USING_X11
53 #include <QtX11Extras/QX11Info>
54 #include <xcb/xproto.h>
55 #if QT5_HAVE_XCB_ICCCM
56 #include <xcb/xcb_icccm.h>
57 #endif
58 #endif
60 #include <saldatabasic.hxx>
61 #include <window.h>
62 #include <vcl/layout.hxx>
63 #include <vcl/syswin.hxx>
65 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
67 #include <cairo.h>
68 #include <headless/svpgdi.hxx>
70 #if QT5_USING_X11 && QT5_HAVE_XCB_ICCCM
71 static bool g_bNeedsWmHintsWindowGroup = true;
72 static xcb_atom_t g_aXcbClientLeaderAtom = 0;
73 #endif
75 static void SvpDamageHandler(void* handle, sal_Int32 nExtentsX, sal_Int32 nExtentsY,
76 sal_Int32 nExtentsWidth, sal_Int32 nExtentsHeight)
78 Qt5Frame* pThis = static_cast<Qt5Frame*>(handle);
79 pThis->Damage(nExtentsX, nExtentsY, nExtentsWidth, nExtentsHeight);
82 namespace
84 sal_Int32 screenNumber(const QScreen* pScreen)
86 const QList<QScreen*> screens = QApplication::screens();
88 sal_Int32 nScreen = 0;
89 bool bFound = false;
90 for (const QScreen* pCurScreen : screens)
92 if (pScreen == pCurScreen)
94 bFound = true;
95 break;
97 nScreen++;
100 return bFound ? nScreen : -1;
104 Qt5Frame::Qt5Frame(Qt5Frame* pParent, SalFrameStyleFlags nStyle, bool bUseCairo)
105 : m_pTopLevel(nullptr)
106 , m_bUseCairo(bUseCairo)
107 , m_pSvpGraphics(nullptr)
108 , m_bNullRegion(true)
109 , m_bGraphicsInUse(false)
110 , m_bGraphicsInvalid(false)
111 , m_ePointerStyle(PointerStyle::Arrow)
112 , m_pDragSource(nullptr)
113 , m_pDropTarget(nullptr)
114 , m_bInDrag(false)
115 , m_bDefaultSize(true)
116 , m_bDefaultPos(true)
117 , m_bFullScreen(false)
118 , m_bFullScreenSpanAll(false)
120 Qt5Instance* pInst = static_cast<Qt5Instance*>(GetSalData()->m_pInstance);
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;
133 m_nStyle = nStyle;
134 m_pParent = pParent;
136 Qt::WindowFlags aWinFlags;
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::FLOAT | SalFrameStyleFlags::TOOLTIP))
147 aWinFlags |= Qt::ToolTip;
148 else if ((nStyle & SalFrameStyleFlags::FLOAT)
149 && !(nStyle & SalFrameStyleFlags::OWNERDRAWDECORATION))
150 aWinFlags |= Qt::Popup;
151 else if (nStyle & SalFrameStyleFlags::TOOLWINDOW)
152 aWinFlags |= Qt::Tool;
153 // top level windows can't be transient in Qt, so make them dialogs, if they have a parent. At least
154 // the plasma shell relies on this setting to skip dialogs in the window list. And Qt Xcb will just
155 // set transient for the types Dialog, Sheet, Tool, SplashScreen, ToolTip, Drawer and Popup.
156 else if (nStyle & SalFrameStyleFlags::DIALOG || m_pParent)
157 aWinFlags |= Qt::Dialog;
158 else
159 aWinFlags |= Qt::Window;
162 if (aWinFlags == Qt::Window)
164 m_pTopLevel = new Qt5MainWindow(*this, aWinFlags);
165 m_pQWidget = new Qt5Widget(*this, aWinFlags);
166 m_pTopLevel->setCentralWidget(m_pQWidget);
167 m_pTopLevel->setFocusProxy(m_pQWidget);
169 else
170 m_pQWidget = new Qt5Widget(*this, aWinFlags);
172 if (pParent && !(pParent->m_nStyle & SalFrameStyleFlags::PLUG))
174 QWindow* pParentWindow = pParent->GetQWidget()->window()->windowHandle();
175 QWindow* pChildWindow = asChild()->window()->windowHandle();
176 if (pParentWindow && pChildWindow && (pParentWindow != pChildWindow))
177 pChildWindow->setTransientParent(pParentWindow);
180 // Calling 'QWidget::winId()' implicitly enables native windows to be used
181 // rather than "alien widgets" that are unknown to the windowing system,
182 // s. https://doc.qt.io/qt-5/qwidget.html#native-widgets-vs-alien-widgets
183 // Avoid this on Wayland due to problems with missing 'mouseMoveEvent's,
184 // s. tdf#122293/QTBUG-75766
185 const bool bWayland = QGuiApplication::platformName() == "wayland";
186 if (!bWayland)
187 m_aSystemData.aWindow = m_pQWidget->winId();
188 else
190 // TODO implement as needed for Wayland,
191 // s.a. commit c0d4f3ad3307c which did this for gtk3
192 // QPlatformNativeInterface* native = QGuiApplication::platformNativeInterface();
193 // m_aSystemData.pDisplay = native->nativeResourceForWindow("display", nullptr);
194 // m_aSystemData.aWindow = reinterpret_cast<unsigned long>(
195 // native->nativeResourceForWindow("surface", m_pQWidget->windowHandle()));
198 m_aSystemData.aShellWindow = reinterpret_cast<sal_IntPtr>(this);
199 //m_aSystemData.pSalFrame = this;
200 m_aSystemData.pWidget = m_pQWidget;
201 //m_aSystemData.nScreen = m_nXScreen.getXScreen();
202 m_aSystemData.toolkit = SystemEnvData::Toolkit::Qt5;
203 if (!bWayland)
204 m_aSystemData.platform = SystemEnvData::Platform::Xcb;
205 else
206 m_aSystemData.platform = SystemEnvData::Platform::Wayland;
208 SetIcon(SV_ICON_ID_OFFICE);
210 fixICCCMwindowGroup();
213 void Qt5Frame::fixICCCMwindowGroup()
215 #if QT5_USING_X11 && QT5_HAVE_XCB_ICCCM
216 // older Qt5 just sets WM_CLIENT_LEADER, but not the XCB_ICCCM_WM_HINT_WINDOW_GROUP
217 // see Qt commit 0de4b326d8 ("xcb: fix issue with dialogs hidden by other windows")
218 // or QTBUG-46626. So LO has to set this itself to help some WMs.
219 if (!g_bNeedsWmHintsWindowGroup)
220 return;
221 g_bNeedsWmHintsWindowGroup = false;
223 if (QGuiApplication::platformName() != "xcb")
224 return;
225 if (QVersionNumber::fromString(qVersion()) >= QVersionNumber(5, 12))
226 return;
228 xcb_connection_t* conn = QX11Info::connection();
229 xcb_window_t win = asChild()->winId();
231 xcb_icccm_wm_hints_t hints;
233 xcb_get_property_cookie_t prop_cookie = xcb_icccm_get_wm_hints_unchecked(conn, win);
234 if (!xcb_icccm_get_wm_hints_reply(conn, prop_cookie, &hints, nullptr))
235 return;
237 if (hints.flags & XCB_ICCCM_WM_HINT_WINDOW_GROUP)
238 return;
240 if (g_aXcbClientLeaderAtom == 0)
242 const char* const leader_name = "WM_CLIENT_LEADER\0";
243 xcb_intern_atom_cookie_t atom_cookie
244 = xcb_intern_atom(conn, 1, strlen(leader_name), leader_name);
245 xcb_intern_atom_reply_t* atom_reply = xcb_intern_atom_reply(conn, atom_cookie, nullptr);
246 if (!atom_reply)
247 return;
248 g_aXcbClientLeaderAtom = atom_reply->atom;
249 free(atom_reply);
252 g_bNeedsWmHintsWindowGroup = true;
254 prop_cookie = xcb_get_property(conn, 0, win, g_aXcbClientLeaderAtom, XCB_ATOM_WINDOW, 0, 1);
255 xcb_get_property_reply_t* prop_reply = xcb_get_property_reply(conn, prop_cookie, nullptr);
256 if (!prop_reply)
257 return;
259 if (xcb_get_property_value_length(prop_reply) != 4)
261 free(prop_reply);
262 return;
265 xcb_window_t leader = *static_cast<xcb_window_t*>(xcb_get_property_value(prop_reply));
266 free(prop_reply);
268 hints.flags |= XCB_ICCCM_WM_HINT_WINDOW_GROUP;
269 hints.window_group = leader;
270 xcb_icccm_set_wm_hints(conn, win, &hints);
271 #endif
274 Qt5Frame::~Qt5Frame()
276 Qt5Instance* pInst = static_cast<Qt5Instance*>(GetSalData()->m_pInstance);
277 pInst->eraseFrame(this);
278 delete asChild();
279 m_aSystemData.aShellWindow = 0;
282 void Qt5Frame::Damage(sal_Int32 nExtentsX, sal_Int32 nExtentsY, sal_Int32 nExtentsWidth,
283 sal_Int32 nExtentsHeight) const
285 m_pQWidget->update(nExtentsX, nExtentsY, nExtentsWidth, nExtentsHeight);
288 void Qt5Frame::TriggerPaintEvent()
290 QSize aSize(m_pQWidget->size());
291 SalPaintEvent aPaintEvt(0, 0, aSize.width(), aSize.height(), true);
292 CallCallback(SalEvent::Paint, &aPaintEvt);
295 void Qt5Frame::TriggerPaintEvent(QRect aRect)
297 SalPaintEvent aPaintEvt(aRect.x(), aRect.y(), aRect.width(), aRect.height(), true);
298 CallCallback(SalEvent::Paint, &aPaintEvt);
301 void Qt5Frame::InitQt5SvpGraphics(Qt5SvpGraphics* pQt5SvpGraphics)
303 int width = 640;
304 int height = 480;
305 m_pSvpGraphics = pQt5SvpGraphics;
306 m_pSurface.reset(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height));
307 m_pSvpGraphics->setSurface(m_pSurface.get(), basegfx::B2IVector(width, height));
308 cairo_surface_set_user_data(m_pSurface.get(), Qt5SvpGraphics::getDamageKey(), &m_aDamageHandler,
309 nullptr);
312 SalGraphics* Qt5Frame::AcquireGraphics()
314 if (m_bGraphicsInUse)
315 return nullptr;
317 m_bGraphicsInUse = true;
319 if (m_bUseCairo)
321 if (!m_pOurSvpGraphics.get() || m_bGraphicsInvalid)
323 m_pOurSvpGraphics.reset(new Qt5SvpGraphics(this));
324 InitQt5SvpGraphics(m_pOurSvpGraphics.get());
325 m_bGraphicsInvalid = false;
327 return m_pOurSvpGraphics.get();
329 else
331 if (!m_pQt5Graphics.get() || m_bGraphicsInvalid)
333 m_pQt5Graphics.reset(new Qt5Graphics(this));
334 m_pQImage.reset(new QImage(m_pQWidget->size(), Qt5_DefaultFormat32));
335 m_pQImage->fill(Qt::transparent);
336 m_pQt5Graphics->ChangeQImage(m_pQImage.get());
337 m_bGraphicsInvalid = false;
339 return m_pQt5Graphics.get();
343 void Qt5Frame::ReleaseGraphics(SalGraphics* pSalGraph)
345 (void)pSalGraph;
346 if (m_bUseCairo)
347 assert(pSalGraph == m_pOurSvpGraphics.get());
348 else
349 assert(pSalGraph == m_pQt5Graphics.get());
350 m_bGraphicsInUse = false;
353 bool Qt5Frame::PostEvent(std::unique_ptr<ImplSVEvent> pData)
355 Qt5Instance* pInst = static_cast<Qt5Instance*>(GetSalData()->m_pInstance);
356 pInst->PostEvent(this, pData.release(), SalEvent::UserEvent);
357 return true;
360 QWidget* Qt5Frame::asChild() const { return m_pTopLevel ? m_pTopLevel : m_pQWidget; }
362 bool Qt5Frame::isWindow() const { return asChild()->isWindow(); }
364 QWindow* Qt5Frame::windowHandle() const
366 // set attribute 'Qt::WA_NativeWindow' first to make sure a window handle actually exists
367 QWidget* pChild = asChild();
368 pChild->setAttribute(Qt::WA_NativeWindow);
369 return pChild->windowHandle();
372 QScreen* Qt5Frame::screen() const
374 QWindow* const pWindow = windowHandle();
375 return pWindow ? pWindow->screen() : nullptr;
378 bool Qt5Frame::isMinimized() const { return asChild()->isMinimized(); }
380 bool Qt5Frame::isMaximized() const { return asChild()->isMaximized(); }
382 void Qt5Frame::SetWindowStateImpl(Qt::WindowStates eState)
384 return asChild()->setWindowState(eState);
387 void Qt5Frame::SetTitle(const OUString& rTitle)
389 m_pQWidget->window()->setWindowTitle(toQString(rTitle));
392 void Qt5Frame::SetIcon(sal_uInt16 nIcon)
394 if (m_nStyle
395 & (SalFrameStyleFlags::PLUG | SalFrameStyleFlags::SYSTEMCHILD
396 | SalFrameStyleFlags::FLOAT | SalFrameStyleFlags::INTRO
397 | SalFrameStyleFlags::OWNERDRAWDECORATION)
398 || !isWindow())
399 return;
401 QString appicon;
403 if (nIcon == SV_ICON_ID_TEXT)
404 appicon = "libreoffice-writer";
405 else if (nIcon == SV_ICON_ID_SPREADSHEET)
406 appicon = "libreoffice-calc";
407 else if (nIcon == SV_ICON_ID_DRAWING)
408 appicon = "libreoffice-draw";
409 else if (nIcon == SV_ICON_ID_PRESENTATION)
410 appicon = "libreoffice-impress";
411 else if (nIcon == SV_ICON_ID_DATABASE)
412 appicon = "libreoffice-base";
413 else if (nIcon == SV_ICON_ID_FORMULA)
414 appicon = "libreoffice-math";
415 else
416 appicon = "libreoffice-startcenter";
418 QIcon aIcon = QIcon::fromTheme(appicon);
419 m_pQWidget->window()->setWindowIcon(aIcon);
422 void Qt5Frame::SetMenu(SalMenu* pMenu) { m_pSalMenu = static_cast<Qt5Menu*>(pMenu); }
424 void Qt5Frame::DrawMenuBar() { /* not needed */}
426 void Qt5Frame::SetExtendedFrameStyle(SalExtStyle /*nExtStyle*/) { /* not needed */}
428 void Qt5Frame::Show(bool bVisible, bool /*bNoActivate*/)
430 assert(m_pQWidget);
432 SetDefaultSize();
433 SetDefaultPos();
435 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
436 assert(pSalInst);
437 pSalInst->RunInMainThread([this, bVisible]() { asChild()->setVisible(bVisible); });
440 void Qt5Frame::SetMinClientSize(long nWidth, long nHeight)
442 if (!isChild())
443 asChild()->setMinimumSize(nWidth, nHeight);
446 void Qt5Frame::SetMaxClientSize(long nWidth, long nHeight)
448 if (!isChild())
449 asChild()->setMaximumSize(nWidth, nHeight);
452 void Qt5Frame::SetDefaultPos()
454 if (!m_bDefaultPos)
455 return;
457 // center on parent
458 if (m_pParent)
460 QWidget* const pWindow = m_pParent->GetQWidget()->window();
461 QWidget* const pWidget = asChild();
462 QPoint aPos = pWindow->rect().center() - pWidget->rect().center();
463 SetPosSize(aPos.x(), aPos.y(), 0, 0, SAL_FRAME_POSSIZE_X | SAL_FRAME_POSSIZE_Y);
464 assert(!m_bDefaultPos);
466 else
467 m_bDefaultPos = false;
470 Size Qt5Frame::CalcDefaultSize()
472 assert(isWindow());
474 Size aSize;
475 if (!m_bFullScreen)
477 const QScreen* pScreen = screen();
478 aSize = bestmaxFrameSizeForScreenSize(
479 toSize(pScreen ? pScreen->size() : QApplication::desktop()->screenGeometry(0).size()));
481 else
483 if (!m_bFullScreenSpanAll)
484 aSize = toSize(
485 QApplication::desktop()->screenGeometry(maGeometry.nDisplayScreenNumber).size());
486 else
488 int nLeftScreen = QApplication::desktop()->screenNumber(QPoint(0, 0));
489 aSize = toSize(QApplication::screens()[nLeftScreen]->availableVirtualGeometry().size());
493 return aSize;
496 void Qt5Frame::SetDefaultSize()
498 if (!m_bDefaultSize)
499 return;
501 Size aDefSize = CalcDefaultSize();
502 SetPosSize(0, 0, aDefSize.Width(), aDefSize.Height(),
503 SAL_FRAME_POSSIZE_WIDTH | SAL_FRAME_POSSIZE_HEIGHT);
504 assert(!m_bDefaultSize);
507 void Qt5Frame::SetPosSize(long nX, long nY, long nWidth, long nHeight, sal_uInt16 nFlags)
509 if (!isWindow() || isChild(true, false))
510 return;
512 if (nFlags & (SAL_FRAME_POSSIZE_WIDTH | SAL_FRAME_POSSIZE_HEIGHT))
514 if (isChild(false) || !m_pQWidget->isMaximized())
516 if (!(nFlags & SAL_FRAME_POSSIZE_WIDTH))
517 nWidth = maGeometry.nWidth;
518 else if (!(nFlags & SAL_FRAME_POSSIZE_HEIGHT))
519 nHeight = maGeometry.nHeight;
521 if (nWidth > 0 && nHeight > 0)
523 m_bDefaultSize = false;
524 if (m_nStyle & SalFrameStyleFlags::SIZEABLE)
525 asChild()->resize(nWidth, nHeight);
526 else
527 asChild()->setFixedSize(nWidth, nHeight);
530 // assume the resize happened
531 // needed for calculations and will eventually be corrected by events
532 if (nWidth > 0)
533 maGeometry.nWidth = nWidth;
534 if (nHeight > 0)
535 maGeometry.nHeight = nHeight;
539 if (nFlags & (SAL_FRAME_POSSIZE_X | SAL_FRAME_POSSIZE_Y))
541 if (m_pParent)
543 const SalFrameGeometry& aParentGeometry = m_pParent->maGeometry;
544 if (QGuiApplication::isRightToLeft())
545 nX = aParentGeometry.nX + aParentGeometry.nWidth - nX - maGeometry.nWidth - 1;
546 else
547 nX += aParentGeometry.nX;
548 nY += aParentGeometry.nY;
550 Qt5MainWindow* pTopLevel = m_pParent->GetTopLevelWindow();
551 if (pTopLevel && pTopLevel->menuBar() && pTopLevel->menuBar()->isVisible())
552 nY += pTopLevel->menuBar()->geometry().height();
555 if (!(nFlags & SAL_FRAME_POSSIZE_X))
556 nX = maGeometry.nX;
557 else if (!(nFlags & SAL_FRAME_POSSIZE_Y))
558 nY = maGeometry.nY;
560 // assume the reposition happened
561 // needed for calculations and will eventually be corrected by events later
562 maGeometry.nX = nX;
563 maGeometry.nY = nY;
565 m_bDefaultPos = false;
566 asChild()->move(nX, nY);
570 void Qt5Frame::GetClientSize(long& rWidth, long& rHeight)
572 rWidth = m_pQWidget->width();
573 rHeight = m_pQWidget->height();
576 void Qt5Frame::GetWorkArea(tools::Rectangle& rRect)
578 if (!isWindow())
579 return;
580 QScreen* pScreen = screen();
581 if (!pScreen)
582 return;
584 QSize aSize = pScreen->availableVirtualSize();
585 rRect = tools::Rectangle(0, 0, aSize.width(), aSize.height());
588 SalFrame* Qt5Frame::GetParent() const { return m_pParent; }
590 void Qt5Frame::SetModal(bool bModal)
592 if (isWindow())
594 auto* pSalInst(static_cast<Qt5Instance*>(GetSalData()->m_pInstance));
595 assert(pSalInst);
596 pSalInst->RunInMainThread([this, bModal]() {
598 QWidget* const pChild = asChild();
599 const bool bWasVisible = pChild->isVisible();
601 // modality change is only effective if the window is hidden
602 if (bWasVisible)
603 pChild->hide();
605 pChild->setWindowModality(bModal ? Qt::WindowModal : Qt::NonModal);
607 if (bWasVisible)
608 pChild->show();
613 bool Qt5Frame::GetModal() const { return isWindow() && windowHandle()->isModal(); }
615 void Qt5Frame::SetWindowState(const SalFrameState* pState)
617 if (!isWindow() || !pState || isChild(true, false))
618 return;
620 const WindowStateMask nMaxGeometryMask
621 = WindowStateMask::X | WindowStateMask::Y | WindowStateMask::Width | WindowStateMask::Height
622 | WindowStateMask::MaximizedX | WindowStateMask::MaximizedY
623 | WindowStateMask::MaximizedWidth | WindowStateMask::MaximizedHeight;
625 if ((pState->mnMask & WindowStateMask::State) && (pState->mnState & WindowStateState::Maximized)
626 && !isMaximized() && (pState->mnMask & nMaxGeometryMask) == nMaxGeometryMask)
628 QWidget* const pChild = asChild();
629 pChild->resize(pState->mnWidth, pState->mnHeight);
630 pChild->move(pState->mnX, pState->mnY);
631 SetWindowStateImpl(Qt::WindowMaximized);
633 else if (pState->mnMask
634 & (WindowStateMask::X | WindowStateMask::Y | WindowStateMask::Width
635 | WindowStateMask::Height))
637 sal_uInt16 nPosSizeFlags = 0;
638 if (pState->mnMask & WindowStateMask::X)
639 nPosSizeFlags |= SAL_FRAME_POSSIZE_X;
640 if (pState->mnMask & WindowStateMask::Y)
641 nPosSizeFlags |= SAL_FRAME_POSSIZE_Y;
642 if (pState->mnMask & WindowStateMask::Width)
643 nPosSizeFlags |= SAL_FRAME_POSSIZE_WIDTH;
644 if (pState->mnMask & WindowStateMask::Height)
645 nPosSizeFlags |= SAL_FRAME_POSSIZE_HEIGHT;
646 SetPosSize(pState->mnX, pState->mnY, pState->mnWidth, pState->mnHeight, nPosSizeFlags);
648 else if (pState->mnMask & WindowStateMask::State && !isChild())
650 if (pState->mnState & WindowStateState::Maximized)
651 SetWindowStateImpl(Qt::WindowMaximized);
652 else if (pState->mnState & WindowStateState::Minimized)
653 SetWindowStateImpl(Qt::WindowMinimized);
654 else
655 SetWindowStateImpl(Qt::WindowNoState);
659 bool Qt5Frame::GetWindowState(SalFrameState* pState)
661 pState->mnState = WindowStateState::Normal;
662 pState->mnMask = WindowStateMask::State;
663 if (isMinimized() /*|| !windowHandle()*/)
664 pState->mnState |= WindowStateState::Minimized;
665 else if (isMaximized())
667 pState->mnState |= WindowStateState::Maximized;
669 else
671 // geometry() is the drawable area, which is wanted here
672 QRect rect = asChild()->geometry();
673 pState->mnX = rect.x();
674 pState->mnY = rect.y();
675 pState->mnWidth = rect.width();
676 pState->mnHeight = rect.height();
677 // the menubar is drawn natively, adjust for that
678 if (maGeometry.nTopDecoration)
680 pState->mnY += maGeometry.nTopDecoration;
681 pState->mnHeight -= maGeometry.nTopDecoration;
683 pState->mnMask |= WindowStateMask::X | WindowStateMask::Y | WindowStateMask::Width
684 | WindowStateMask::Height;
687 return true;
690 void Qt5Frame::ShowFullScreen(bool bFullScreen, sal_Int32 nScreen)
692 // only top-level windows can go fullscreen
693 assert(m_pTopLevel);
695 if (m_bFullScreen == bFullScreen)
696 return;
698 m_bFullScreen = bFullScreen;
699 m_bFullScreenSpanAll = m_bFullScreen && (nScreen < 0);
701 // show it if it isn't shown yet
702 if (!isWindow())
703 m_pTopLevel->show();
705 if (m_bFullScreen)
707 m_aRestoreGeometry = m_pTopLevel->geometry();
708 m_nRestoreScreen = maGeometry.nDisplayScreenNumber;
709 SetScreenNumber(m_bFullScreenSpanAll ? m_nRestoreScreen : nScreen);
710 if (!m_bFullScreenSpanAll)
711 windowHandle()->showFullScreen();
712 else
713 windowHandle()->showNormal();
715 else
717 SetScreenNumber(m_nRestoreScreen);
718 windowHandle()->showNormal();
719 m_pTopLevel->setGeometry(m_aRestoreGeometry);
723 void Qt5Frame::StartPresentation(bool bStart)
725 // meh - so there's no Qt platform independent solution
726 // https://forum.qt.io/topic/38504/solved-qdialog-in-fullscreen-disable-os-screensaver
727 #if QT5_USING_X11
728 boost::optional<unsigned int> aRootWindow;
729 boost::optional<Display*> aDisplay;
731 if (QX11Info::isPlatformX11())
733 aRootWindow = QX11Info::appRootWindow();
734 aDisplay = QX11Info::display();
737 m_ScreenSaverInhibitor.inhibit(bStart, "presentation", QX11Info::isPlatformX11(), aRootWindow,
738 aDisplay);
739 #else
740 (void)bStart;
741 #endif
744 void Qt5Frame::SetAlwaysOnTop(bool bOnTop)
746 QWidget* const pWidget = asChild();
747 const Qt::WindowFlags flags = pWidget->windowFlags();
748 if (bOnTop)
749 pWidget->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
750 else
751 pWidget->setWindowFlags(flags & ~(Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint));
754 void Qt5Frame::ToTop(SalFrameToTop nFlags)
756 QWidget* const pWidget = asChild();
757 if (isWindow() && !(nFlags & SalFrameToTop::GrabFocusOnly))
758 pWidget->raise();
759 if ((nFlags & SalFrameToTop::RestoreWhenMin) || (nFlags & SalFrameToTop::ForegroundTask))
760 pWidget->activateWindow();
761 else if ((nFlags & SalFrameToTop::GrabFocus) || (nFlags & SalFrameToTop::GrabFocusOnly))
763 pWidget->activateWindow();
764 pWidget->setFocus();
768 void Qt5Frame::SetPointer(PointerStyle ePointerStyle)
770 QWindow* pWindow = m_pQWidget->window()->windowHandle();
771 if (!pWindow)
772 return;
773 if (ePointerStyle == m_ePointerStyle)
774 return;
775 m_ePointerStyle = ePointerStyle;
777 pWindow->setCursor(static_cast<Qt5Data*>(GetSalData())->getCursor(ePointerStyle));
780 void Qt5Frame::CaptureMouse(bool bMouse)
782 static const char* pEnv = getenv("SAL_NO_MOUSEGRABS");
783 if (pEnv && *pEnv)
784 return;
786 if (bMouse)
787 m_pQWidget->grabMouse();
788 else
789 m_pQWidget->releaseMouse();
792 void Qt5Frame::SetPointerPos(long nX, long nY)
794 // some cursor already exists (and it has m_ePointerStyle shape)
795 // so here we just reposition it
796 QCursor::setPos(m_pQWidget->mapToGlobal(QPoint(nX, nY)));
799 void Qt5Frame::Flush()
801 // was: QGuiApplication::sync();
802 // but FIXME it causes too many issues, figure out sth better
804 // unclear if we need to also flush cairo surface - gtk3 backend
805 // does not do it. QPainter in Qt5Widget::paintEvent() is
806 // destroyed, so that state should be safely flushed.
809 bool Qt5Frame::ShowTooltip(const OUString& rText, const tools::Rectangle& rHelpArea)
811 QRect aHelpArea(toQRect(rHelpArea));
812 if (QGuiApplication::isRightToLeft())
813 aHelpArea.moveLeft(maGeometry.nWidth - aHelpArea.width() - aHelpArea.left() - 1);
814 QToolTip::showText(QCursor::pos(), toQString(rText), m_pQWidget, aHelpArea);
815 return true;
818 void Qt5Frame::SetInputContext(SalInputContext* pContext)
820 if (!pContext)
821 return;
823 if (!(pContext->mnOptions & InputContextFlags::Text))
824 return;
826 m_pQWidget->setAttribute(Qt::WA_InputMethodEnabled);
829 void Qt5Frame::EndExtTextInput(EndExtTextInputFlags /*nFlags*/)
831 Qt5Widget* pQt5Widget = static_cast<Qt5Widget*>(m_pQWidget);
832 if (pQt5Widget)
833 pQt5Widget->endExtTextInput();
836 OUString Qt5Frame::GetKeyName(sal_uInt16 nKeyCode)
838 vcl::KeyCode vclKeyCode(nKeyCode);
839 int nCode = vclKeyCode.GetCode();
840 int nRetCode = 0;
842 if (nCode >= KEY_0 && nCode <= KEY_9)
843 nRetCode = (nCode - KEY_0) + Qt::Key_0;
844 else if (nCode >= KEY_A && nCode <= KEY_Z)
845 nRetCode = (nCode - KEY_A) + Qt::Key_A;
846 else if (nCode >= KEY_F1 && nCode <= KEY_F26)
847 nRetCode = (nCode - KEY_F1) + Qt::Key_F1;
848 else
850 switch (nCode)
852 case KEY_DOWN:
853 nRetCode = Qt::Key_Down;
854 break;
855 case KEY_UP:
856 nRetCode = Qt::Key_Up;
857 break;
858 case KEY_LEFT:
859 nRetCode = Qt::Key_Left;
860 break;
861 case KEY_RIGHT:
862 nRetCode = Qt::Key_Right;
863 break;
864 case KEY_HOME:
865 nRetCode = Qt::Key_Home;
866 break;
867 case KEY_END:
868 nRetCode = Qt::Key_End;
869 break;
870 case KEY_PAGEUP:
871 nRetCode = Qt::Key_PageUp;
872 break;
873 case KEY_PAGEDOWN:
874 nRetCode = Qt::Key_PageDown;
875 break;
876 case KEY_RETURN:
877 nRetCode = Qt::Key_Return;
878 break;
879 case KEY_ESCAPE:
880 nRetCode = Qt::Key_Escape;
881 break;
882 case KEY_TAB:
883 nRetCode = Qt::Key_Tab;
884 break;
885 case KEY_BACKSPACE:
886 nRetCode = Qt::Key_Backspace;
887 break;
888 case KEY_SPACE:
889 nRetCode = Qt::Key_Space;
890 break;
891 case KEY_INSERT:
892 nRetCode = Qt::Key_Insert;
893 break;
894 case KEY_DELETE:
895 nRetCode = Qt::Key_Delete;
896 break;
897 case KEY_ADD:
898 nRetCode = Qt::Key_Plus;
899 break;
900 case KEY_SUBTRACT:
901 nRetCode = Qt::Key_Minus;
902 break;
903 case KEY_MULTIPLY:
904 nRetCode = Qt::Key_Asterisk;
905 break;
906 case KEY_DIVIDE:
907 nRetCode = Qt::Key_Slash;
908 break;
909 case KEY_POINT:
910 nRetCode = Qt::Key_Period;
911 break;
912 case KEY_COMMA:
913 nRetCode = Qt::Key_Comma;
914 break;
915 case KEY_LESS:
916 nRetCode = Qt::Key_Less;
917 break;
918 case KEY_GREATER:
919 nRetCode = Qt::Key_Greater;
920 break;
921 case KEY_EQUAL:
922 nRetCode = Qt::Key_Equal;
923 break;
924 case KEY_FIND:
925 nRetCode = Qt::Key_Find;
926 break;
927 case KEY_CONTEXTMENU:
928 nRetCode = Qt::Key_Menu;
929 break;
930 case KEY_HELP:
931 nRetCode = Qt::Key_Help;
932 break;
933 case KEY_UNDO:
934 nRetCode = Qt::Key_Undo;
935 break;
936 case KEY_REPEAT:
937 nRetCode = Qt::Key_Redo;
938 break;
939 case KEY_TILDE:
940 nRetCode = Qt::Key_AsciiTilde;
941 break;
942 case KEY_QUOTELEFT:
943 nRetCode = Qt::Key_QuoteLeft;
944 break;
945 case KEY_BRACKETLEFT:
946 nRetCode = Qt::Key_BracketLeft;
947 break;
948 case KEY_BRACKETRIGHT:
949 nRetCode = Qt::Key_BracketRight;
950 break;
951 case KEY_SEMICOLON:
952 nRetCode = Qt::Key_Semicolon;
953 break;
955 // Special cases
956 case KEY_COPY:
957 nRetCode = Qt::Key_Copy;
958 break;
959 case KEY_CUT:
960 nRetCode = Qt::Key_Cut;
961 break;
962 case KEY_PASTE:
963 nRetCode = Qt::Key_Paste;
964 break;
965 case KEY_OPEN:
966 nRetCode = Qt::Key_Open;
967 break;
971 if (vclKeyCode.IsShift())
972 nRetCode += Qt::SHIFT;
973 if (vclKeyCode.IsMod1())
974 nRetCode += Qt::CTRL;
975 if (vclKeyCode.IsMod2())
976 nRetCode += Qt::ALT;
978 QKeySequence keySeq(nRetCode);
979 OUString sKeyName = toOUString(keySeq.toString());
981 return sKeyName;
984 bool Qt5Frame::MapUnicodeToKeyCode(sal_Unicode /*aUnicode*/, LanguageType /*aLangType*/,
985 vcl::KeyCode& /*rKeyCode*/)
987 // not supported yet
988 return false;
991 LanguageType Qt5Frame::GetInputLanguage()
993 // fallback
994 return LANGUAGE_DONTKNOW;
997 static Color toColor(const QColor& rColor)
999 return Color(rColor.red(), rColor.green(), rColor.blue());
1002 void Qt5Frame::UpdateSettings(AllSettings& rSettings)
1004 if (Qt5Data::noNativeControls())
1005 return;
1007 StyleSettings style(rSettings.GetStyleSettings());
1009 // General settings
1010 QPalette pal = QApplication::palette();
1012 style.SetToolbarIconSize(ToolbarIconSize::Large);
1014 Color aFore = toColor(pal.color(QPalette::Active, QPalette::WindowText));
1015 Color aBack = toColor(pal.color(QPalette::Active, QPalette::Window));
1016 Color aText = toColor(pal.color(QPalette::Active, QPalette::Text));
1017 Color aBase = toColor(pal.color(QPalette::Active, QPalette::Base));
1018 Color aButn = toColor(pal.color(QPalette::Active, QPalette::ButtonText));
1019 Color aMid = toColor(pal.color(QPalette::Active, QPalette::Mid));
1020 Color aHigh = toColor(pal.color(QPalette::Active, QPalette::Highlight));
1021 Color aHighText = toColor(pal.color(QPalette::Active, QPalette::HighlightedText));
1022 Color aLink = toColor(pal.color(QPalette::Active, QPalette::Link));
1023 Color aVisitedLink = toColor(pal.color(QPalette::Active, QPalette::LinkVisited));
1025 style.SetSkipDisabledInMenus(true);
1027 // Foreground
1028 style.SetRadioCheckTextColor(aFore);
1029 style.SetLabelTextColor(aFore);
1030 style.SetDialogTextColor(aFore);
1031 style.SetGroupTextColor(aFore);
1033 // Text
1034 style.SetFieldTextColor(aText);
1035 style.SetFieldRolloverTextColor(aText);
1036 style.SetWindowTextColor(aText);
1037 style.SetToolTextColor(aText);
1039 // Base
1040 style.SetFieldColor(aBase);
1041 style.SetWindowColor(aBase);
1042 style.SetActiveTabColor(aBase);
1044 // Buttons
1045 style.SetButtonTextColor(aButn);
1046 style.SetDefaultActionButtonTextColor(aButn);
1047 style.SetActionButtonTextColor(aButn);
1048 style.SetActionButtonRolloverTextColor(aButn);
1049 style.SetButtonRolloverTextColor(aButn);
1050 style.SetButtonPressedRolloverTextColor(aButn);
1052 // Tabs
1053 style.SetTabTextColor(aButn);
1054 style.SetTabRolloverTextColor(aButn);
1055 style.SetTabHighlightTextColor(aButn);
1057 // Disable color
1058 style.SetDisableColor(toColor(pal.color(QPalette::Disabled, QPalette::WindowText)));
1060 // Background
1061 style.BatchSetBackgrounds(aBack);
1062 style.SetInactiveTabColor(aBack);
1064 // Workspace
1065 style.SetWorkspaceColor(aMid);
1067 // Selection
1068 style.SetHighlightColor(aHigh);
1069 style.SetHighlightTextColor(aHighText);
1071 // Links
1072 style.SetLinkColor(aLink);
1073 style.SetVisitedLinkColor(aVisitedLink);
1075 // Tooltip
1076 style.SetHelpColor(toColor(QToolTip::palette().color(QPalette::Active, QPalette::ToolTipBase)));
1077 style.SetHelpTextColor(
1078 toColor(QToolTip::palette().color(QPalette::Active, QPalette::ToolTipText)));
1080 const int flash_time = QApplication::cursorFlashTime();
1081 style.SetCursorBlinkTime(flash_time != 0 ? flash_time / 2 : STYLE_CURSOR_NOBLINKTIME);
1083 // Menu
1084 std::unique_ptr<QMenuBar> pMenuBar = std::make_unique<QMenuBar>();
1085 QPalette qMenuCG = pMenuBar->palette();
1087 // Menu text and background color, theme specific
1088 Color aMenuFore = toColor(qMenuCG.color(QPalette::WindowText));
1089 Color aMenuBack = toColor(qMenuCG.color(QPalette::Window));
1091 style.SetMenuTextColor(aMenuFore);
1092 style.SetMenuBarTextColor(style.GetPersonaMenuBarTextColor().get_value_or(aMenuFore));
1093 style.SetMenuColor(aMenuBack);
1094 style.SetMenuBarColor(aMenuBack);
1095 style.SetMenuHighlightColor(toColor(qMenuCG.color(QPalette::Highlight)));
1096 style.SetMenuHighlightTextColor(toColor(qMenuCG.color(QPalette::HighlightedText)));
1098 // set special menubar highlight text color
1099 if (QApplication::style()->inherits("HighContrastStyle"))
1100 ImplGetSVData()->maNWFData.maMenuBarHighlightTextColor
1101 = toColor(qMenuCG.color(QPalette::HighlightedText));
1102 else
1103 ImplGetSVData()->maNWFData.maMenuBarHighlightTextColor = aMenuFore;
1105 // set menubar rollover color
1106 if (pMenuBar->style()->styleHint(QStyle::SH_MenuBar_MouseTracking))
1108 style.SetMenuBarRolloverColor(toColor(qMenuCG.color(QPalette::Highlight)));
1109 style.SetMenuBarRolloverTextColor(ImplGetSVData()->maNWFData.maMenuBarHighlightTextColor);
1111 else
1113 style.SetMenuBarRolloverColor(aMenuBack);
1114 style.SetMenuBarRolloverTextColor(aMenuFore);
1116 style.SetMenuBarHighlightTextColor(style.GetMenuHighlightTextColor());
1118 // Scroll bar size
1119 style.SetScrollBarSize(QApplication::style()->pixelMetric(QStyle::PM_ScrollBarExtent));
1120 style.SetMinThumbSize(QApplication::style()->pixelMetric(QStyle::PM_ScrollBarSliderMin));
1122 // These colors are used for the ruler text and marks
1123 style.SetShadowColor(toColor(pal.color(QPalette::Disabled, QPalette::WindowText)));
1124 style.SetDarkShadowColor(toColor(pal.color(QPalette::Inactive, QPalette::WindowText)));
1126 m_bGraphicsInvalid = true;
1127 rSettings.SetStyleSettings(style);
1130 void Qt5Frame::Beep() { QApplication::beep(); }
1132 SalFrame::SalPointerState Qt5Frame::GetPointerState()
1134 SalPointerState aState;
1135 aState.maPos = toPoint(QCursor::pos());
1136 aState.mnState = GetMouseModCode(QGuiApplication::mouseButtons())
1137 | GetKeyModCode(QGuiApplication::keyboardModifiers());
1138 return aState;
1141 KeyIndicatorState Qt5Frame::GetIndicatorState() { return KeyIndicatorState(); }
1143 void Qt5Frame::SimulateKeyPress(sal_uInt16 nKeyCode)
1145 SAL_WARN("vcl.qt5", "missing simulate keypress " << nKeyCode);
1148 void Qt5Frame::SetParent(SalFrame* pNewParent) { m_pParent = static_cast<Qt5Frame*>(pNewParent); }
1150 bool Qt5Frame::SetPluginParent(SystemParentData* /*pNewParent*/)
1152 //FIXME: no SetPluginParent impl. for qt5
1153 return false;
1156 void Qt5Frame::ResetClipRegion() { m_bNullRegion = true; }
1158 void Qt5Frame::BeginSetClipRegion(sal_uInt32)
1160 m_aRegion = QRegion(QRect(QPoint(0, 0), m_pQWidget->size()));
1163 void Qt5Frame::UnionClipRegion(long nX, long nY, long nWidth, long nHeight)
1165 m_aRegion = m_aRegion.united(QRegion(nX, nY, nWidth, nHeight));
1168 void Qt5Frame::EndSetClipRegion() { m_bNullRegion = false; }
1170 void Qt5Frame::SetScreenNumber(unsigned int nScreen)
1172 if (isWindow())
1174 QWindow* const pWindow = windowHandle();
1175 if (pWindow)
1177 QList<QScreen*> screens = QApplication::screens();
1178 if (static_cast<int>(nScreen) < screens.size() || m_bFullScreenSpanAll)
1180 QRect screenGeo;
1182 if (!m_bFullScreenSpanAll)
1184 screenGeo = QApplication::desktop()->screenGeometry(nScreen);
1185 pWindow->setScreen(QApplication::screens()[nScreen]);
1187 else // special case: fullscreen over all available screens
1189 assert(m_bFullScreen);
1190 // left-most screen
1191 int nLeftScreen = QApplication::desktop()->screenNumber(QPoint(0, 0));
1192 // entire virtual desktop
1193 screenGeo = QApplication::screens()[nLeftScreen]->availableVirtualGeometry();
1194 pWindow->setScreen(QApplication::screens()[nLeftScreen]);
1195 pWindow->setGeometry(screenGeo);
1196 nScreen = nLeftScreen;
1199 // setScreen by itself has no effect, explicitly move the widget to
1200 // the new screen
1201 asChild()->move(screenGeo.topLeft());
1203 else
1205 // index outta bounds, use primary screen
1206 QScreen* primaryScreen = QApplication::primaryScreen();
1207 pWindow->setScreen(primaryScreen);
1208 nScreen = static_cast<sal_uInt32>(screenNumber(primaryScreen));
1211 maGeometry.nDisplayScreenNumber = nScreen;
1216 void Qt5Frame::SetApplicationID(const OUString& rWMClass)
1218 #if QT5_USING_X11
1219 if (QGuiApplication::platformName() != "xcb" || !m_pTopLevel)
1220 return;
1222 OString aResClass = OUStringToOString(rWMClass, RTL_TEXTENCODING_ASCII_US);
1223 const char* pResClass
1224 = !aResClass.isEmpty() ? aResClass.getStr() : SalGenericSystem::getFrameClassName();
1225 OString aResName = SalGenericSystem::getFrameResName();
1227 // the WM_CLASS data consists of two concatenated cstrings, including the terminating '\0' chars
1228 const uint32_t data_len = aResName.getLength() + 1 + strlen(pResClass) + 1;
1229 char* data = new char[data_len];
1230 memcpy(data, aResName.getStr(), aResName.getLength() + 1);
1231 memcpy(data + aResName.getLength() + 1, pResClass, strlen(pResClass) + 1);
1233 xcb_change_property(QX11Info::connection(), XCB_PROP_MODE_REPLACE, m_pTopLevel->winId(),
1234 XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, 8, data_len, data);
1235 delete[] data;
1236 #else
1237 (void)rWMClass;
1238 #endif
1241 // Drag'n'drop foo
1243 void Qt5Frame::registerDragSource(Qt5DragSource* pDragSource)
1245 assert(!m_pDragSource);
1246 m_pDragSource = pDragSource;
1249 void Qt5Frame::deregisterDragSource(Qt5DragSource const* pDragSource)
1251 assert(m_pDragSource == pDragSource);
1252 (void)pDragSource;
1253 m_pDragSource = nullptr;
1256 void Qt5Frame::registerDropTarget(Qt5DropTarget* pDropTarget)
1258 assert(!m_pDropTarget);
1259 m_pDropTarget = pDropTarget;
1260 m_pQWidget->setAcceptDrops(true);
1263 void Qt5Frame::deregisterDropTarget(Qt5DropTarget const* pDropTarget)
1265 assert(m_pDropTarget == pDropTarget);
1266 (void)pDropTarget;
1267 m_pDropTarget = nullptr;
1270 static css::uno::Reference<css::datatransfer::XTransferable>
1271 lcl_getXTransferable(const QMimeData* pMimeData)
1273 css::uno::Reference<css::datatransfer::XTransferable> xTransferable;
1274 const Qt5MimeData* pQt5MimeData = dynamic_cast<const Qt5MimeData*>(pMimeData);
1275 if (!pQt5MimeData)
1276 xTransferable = new Qt5DnDTransferable(pMimeData);
1277 else
1278 xTransferable = pQt5MimeData->xTransferable();
1279 return xTransferable;
1282 static sal_Int8 lcl_getUserDropAction(const QDropEvent* pEvent, const sal_Int8 nSourceActions,
1283 const QMimeData* pMimeData)
1285 // we completely ignore all proposals by the Qt event, as they don't
1286 // match at all with the preferred LO DnD actions.
1288 // check the key modifiers to detect a user-overridden DnD action
1289 const Qt::KeyboardModifiers eKeyMod = pEvent->keyboardModifiers();
1290 sal_Int8 nUserDropAction = 0;
1291 if ((eKeyMod & Qt::ShiftModifier) && !(eKeyMod & Qt::ControlModifier))
1292 nUserDropAction = css::datatransfer::dnd::DNDConstants::ACTION_MOVE;
1293 else if ((eKeyMod & Qt::ControlModifier) && !(eKeyMod & Qt::ShiftModifier))
1294 nUserDropAction = css::datatransfer::dnd::DNDConstants::ACTION_COPY;
1295 else if ((eKeyMod & Qt::ShiftModifier) && (eKeyMod & Qt::ControlModifier))
1296 nUserDropAction = css::datatransfer::dnd::DNDConstants::ACTION_LINK;
1297 nUserDropAction &= nSourceActions;
1299 // select the default DnD action, if there isn't a user preference
1300 if (0 == nUserDropAction)
1302 // default LO internal action is move, but default external action is copy
1303 nUserDropAction = dynamic_cast<const Qt5MimeData*>(pMimeData)
1304 ? css::datatransfer::dnd::DNDConstants::ACTION_MOVE
1305 : css::datatransfer::dnd::DNDConstants::ACTION_COPY;
1306 nUserDropAction &= nSourceActions;
1308 // if the default doesn't match any allowed source action, fall back to the
1309 // preferred of all allowed source actions
1310 if (0 == nUserDropAction)
1311 nUserDropAction = toVclDropAction(getPreferredDropAction(nSourceActions));
1313 // this is "our" preference, but actually we would even prefer any default,
1314 // if there is any
1315 nUserDropAction |= css::datatransfer::dnd::DNDConstants::ACTION_DEFAULT;
1317 return nUserDropAction;
1320 void Qt5Frame::handleDragMove(QDragMoveEvent* pEvent)
1322 assert(m_pDropTarget);
1324 // prepare our suggested drop action for the drop target
1325 const sal_Int8 nSourceActions = toVclDropActions(pEvent->possibleActions());
1326 const QMimeData* pMimeData = pEvent->mimeData();
1327 const sal_Int8 nUserDropAction = lcl_getUserDropAction(pEvent, nSourceActions, pMimeData);
1329 css::datatransfer::dnd::DropTargetDragEnterEvent aEvent;
1330 aEvent.Source = static_cast<css::datatransfer::dnd::XDropTarget*>(m_pDropTarget);
1331 aEvent.Context = static_cast<css::datatransfer::dnd::XDropTargetDragContext*>(m_pDropTarget);
1332 aEvent.LocationX = pEvent->pos().x();
1333 aEvent.LocationY = pEvent->pos().y();
1334 aEvent.DropAction = nUserDropAction;
1335 aEvent.SourceActions = nSourceActions;
1337 // ask the drop target to accept our drop action
1338 if (!m_bInDrag)
1340 aEvent.SupportedDataFlavors = lcl_getXTransferable(pMimeData)->getTransferDataFlavors();
1341 m_pDropTarget->fire_dragEnter(aEvent);
1342 m_bInDrag = true;
1344 else
1345 m_pDropTarget->fire_dragOver(aEvent);
1347 // the drop target accepted our drop action => inform Qt
1348 if (m_pDropTarget->proposedDropAction() != 0)
1350 pEvent->setDropAction(getPreferredDropAction(m_pDropTarget->proposedDropAction()));
1351 pEvent->accept();
1353 else // or maybe someone else likes it?
1354 pEvent->ignore();
1357 void Qt5Frame::handleDrop(QDropEvent* pEvent)
1359 assert(m_pDropTarget);
1361 // prepare our suggested drop action for the drop target
1362 const sal_Int8 nSourceActions = toVclDropActions(pEvent->possibleActions());
1363 const sal_Int8 nUserDropAction
1364 = lcl_getUserDropAction(pEvent, nSourceActions, pEvent->mimeData());
1366 css::datatransfer::dnd::DropTargetDropEvent aEvent;
1367 aEvent.Source = static_cast<css::datatransfer::dnd::XDropTarget*>(m_pDropTarget);
1368 aEvent.Context = static_cast<css::datatransfer::dnd::XDropTargetDropContext*>(m_pDropTarget);
1369 aEvent.LocationX = pEvent->pos().x();
1370 aEvent.LocationY = pEvent->pos().y();
1371 aEvent.SourceActions = nSourceActions;
1372 aEvent.DropAction = nUserDropAction;
1373 aEvent.Transferable = lcl_getXTransferable(pEvent->mimeData());
1375 // ask the drop target to accept our drop action
1376 m_pDropTarget->fire_drop(aEvent);
1377 m_bInDrag = false;
1379 const bool bDropSuccessful = m_pDropTarget->dropSuccessful();
1380 const sal_Int8 nDropAction = m_pDropTarget->proposedDropAction();
1382 // inform the drag source of the drag-origin frame of the drop result
1383 if (pEvent->source())
1385 Qt5Widget* pWidget = dynamic_cast<Qt5Widget*>(pEvent->source());
1386 assert(pWidget); // AFAIK there shouldn't be any non-Qt5Widget as source in LO itself
1387 if (pWidget)
1388 pWidget->frame().m_pDragSource->fire_dragEnd(nDropAction, bDropSuccessful);
1391 // the drop target accepted our drop action => inform Qt
1392 if (bDropSuccessful)
1394 pEvent->setDropAction(getPreferredDropAction(nDropAction));
1395 pEvent->accept();
1397 else // or maybe someone else likes it?
1398 pEvent->ignore();
1401 void Qt5Frame::handleDragLeave()
1403 css::datatransfer::dnd::DropTargetEvent aEvent;
1404 aEvent.Source = static_cast<css::datatransfer::dnd::XDropTarget*>(m_pDropTarget);
1405 m_pDropTarget->fire_dragExit(aEvent);
1406 m_bInDrag = false;
1409 cairo_t* Qt5Frame::getCairoContext() const
1411 cairo_t* cr = nullptr;
1412 if (m_bUseCairo)
1414 cr = cairo_create(m_pSurface.get());
1415 assert(cr);
1417 return cr;
1420 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */