Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / include / sfx2 / LokControlHandler.hxx
blobc422afc979ccdc217c32687a53d77102063d099c
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/.
8 */
10 #pragma once
12 #include <sfx2/lokhelper.hxx>
13 #include <svx/svditer.hxx>
14 #include <svx/svdouno.hxx>
15 #include <tools/UnitConversion.hxx>
16 #include <vcl/virdev.hxx>
17 #include <vcl/window.hxx>
18 #include <com/sun/star/awt/PosSize.hpp>
19 #include <com/sun/star/awt/XControl.hpp>
20 #include <com/sun/star/awt/XWindow.hpp>
21 #include <com/sun/star/awt/XWindowPeer.hpp>
22 #include <com/sun/star/awt/XGraphics.hpp>
23 #include <com/sun/star/awt/XView.hpp>
24 #include <toolkit/helper/vclunohelper.hxx>
26 class LokControlHandler
28 public:
29 static bool postMouseEvent(const SdrPage* pPage, const SdrView* pDrawView,
30 vcl::Window const& rMainWindow, int nType, Point aPointHmm,
31 int nCount, int nButtons, int nModifier)
33 SdrObjListIter aIterator(pPage, SdrIterMode::Flat);
34 while (aIterator.IsMore())
36 SdrObject* pObject = aIterator.Next();
37 SdrUnoObj* pUnoObect = dynamic_cast<SdrUnoObj*>(pObject);
38 if (pUnoObect)
40 tools::Rectangle aControlRectHMM = pUnoObect->GetLogicRect();
41 if (aControlRectHMM.Contains(aPointHmm))
43 css::uno::Reference<css::awt::XControl> xControl
44 = pUnoObect->GetUnoControl(*pDrawView, *rMainWindow.GetOutDev());
45 if (!xControl.is())
46 return false;
48 css::uno::Reference<css::awt::XWindow> xControlWindow(xControl,
49 css::uno::UNO_QUERY);
50 if (!xControlWindow.is())
51 return false;
53 css::uno::Reference<css::awt::XWindowPeer> xWindowPeer(xControl->getPeer());
55 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow(xWindowPeer);
56 if (pWindow)
58 Point aControlRelativePositionHMM = aPointHmm - aControlRectHMM.TopLeft();
59 Point aControlRelativePosition = o3tl::convert(
60 aControlRelativePositionHMM, o3tl::Length::mm100, o3tl::Length::px);
62 LokMouseEventData aMouseEventData(nType, aControlRelativePosition, nCount,
63 MouseEventModifiers::SIMPLECLICK,
64 nButtons, nModifier);
66 SfxLokHelper::postMouseEventAsync(pWindow, aMouseEventData);
67 return true;
72 return false;
75 static void drawUnoControl(const SdrView* pDrawView, const SdrUnoObj* pUnoObect,
76 vcl::Window const& rMainWindow, VirtualDevice& rDevice,
77 tools::Rectangle const& rTileRectHMM, double scaleX, double scaleY)
79 css::uno::Reference<css::awt::XControl> xControl
80 = pUnoObect->GetUnoControl(*pDrawView, *rMainWindow.GetOutDev());
81 if (!xControl.is())
82 return;
84 css::uno::Reference<css::awt::XWindow> xControlWindow(xControl, css::uno::UNO_QUERY);
85 if (!xControlWindow.is())
86 return;
88 css::uno::Reference<css::awt::XGraphics> xGraphics(rDevice.CreateUnoGraphics());
89 if (!xGraphics.is())
90 return;
92 css::uno::Reference<css::awt::XView> xControlView(xControl, css::uno::UNO_QUERY);
93 if (!xControlView.is())
94 return;
96 tools::Rectangle aObjectRectHMM = pUnoObect->GetLogicRect();
97 Point aOffsetFromTile(aObjectRectHMM.Left() - rTileRectHMM.Left(),
98 aObjectRectHMM.Top() - rTileRectHMM.Top());
99 tools::Rectangle aRectangleHMM(aOffsetFromTile, aObjectRectHMM.GetSize());
100 tools::Rectangle aRectanglePx
101 = o3tl::convert(aRectangleHMM, o3tl::Length::mm100, o3tl::Length::px);
103 xControlWindow->setPosSize(0, 0, aRectanglePx.GetWidth(), aRectanglePx.GetHeight(),
104 css::awt::PosSize::POSSIZE);
106 xControlView->setGraphics(xGraphics);
108 xControlView->draw(aRectanglePx.Left() * scaleX, aRectanglePx.Top() * scaleY);
111 static void paintControlTile(const SdrPage* pPage, const SdrView* pDrawView,
112 vcl::Window const& rMainWindow, VirtualDevice& rDevice,
113 Size aOutputSize, tools::Rectangle const& rTileRect)
115 tools::Rectangle aTileRectHMM
116 = o3tl::convert(rTileRect, o3tl::Length::twip, o3tl::Length::mm100);
118 // Resizes the virtual device so to contain the entries context
119 rDevice.SetOutputSizePixel(aOutputSize);
121 rDevice.Push(vcl::PushFlags::MAPMODE);
122 MapMode aDeviceMapMode(rDevice.GetMapMode());
124 const Fraction scale = conversionFract(o3tl::Length::px, o3tl::Length::mm100);
125 Fraction scaleX = Fraction(aOutputSize.Width(), aTileRectHMM.GetWidth()) * scale;
126 Fraction scaleY = Fraction(aOutputSize.Height(), aTileRectHMM.GetHeight()) * scale;
127 aDeviceMapMode.SetScaleX(scaleX);
128 aDeviceMapMode.SetScaleY(scaleY);
129 rDevice.SetMapMode(aDeviceMapMode);
131 SdrObjListIter aIterator(pPage, SdrIterMode::Flat);
133 while (aIterator.IsMore())
135 SdrObject* pObject = aIterator.Next();
136 SdrUnoObj* pUnoObect = dynamic_cast<SdrUnoObj*>(pObject);
137 if (pUnoObect)
139 tools::Rectangle aObjectRectHMM = pUnoObect->GetLogicRect();
141 // Check if we intersect with the tile rectangle and we
142 // need to draw the control.
143 if (aObjectRectHMM.Overlaps(aTileRectHMM))
145 drawUnoControl(pDrawView, pUnoObect, rMainWindow, rDevice, aTileRectHMM,
146 double(scaleX), double(scaleY));
151 rDevice.Pop();
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */