Bump version to 21.06.18.1
[LibreOffice.git] / include / sfx2 / LokControlHandler.hxx
blob34547950643c9e1a1b3308857400e0dad70a5c83
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/dllapi.h>
13 #include <svx/svdouno.hxx>
14 #include <vcl/window.hxx>
15 #include <tools/UnitConversion.hxx>
16 #include <com/sun/star/awt/PosSize.hpp>
17 #include <com/sun/star/awt/XControl.hpp>
18 #include <com/sun/star/awt/XWindow.hpp>
19 #include <com/sun/star/awt/XWindowPeer.hpp>
20 #include <com/sun/star/awt/XGraphics.hpp>
21 #include <com/sun/star/awt/XView.hpp>
22 #include <toolkit/helper/vclunohelper.hxx>
24 class LokControlHandler
26 public:
27 static constexpr double convertMm100ToPixel(double fNumber) { return fNumber * 96.0 / 2540.0; }
29 static Point convertMm100ToPixel(const Point& rPoint)
31 return Point(convertMm100ToPixel(rPoint.getX()), convertMm100ToPixel(rPoint.getY()));
34 static tools::Rectangle convertMm100ToPixel(const tools::Rectangle& rRectangle)
36 return tools::Rectangle(convertMm100ToPixel(rRectangle.TopLeft()),
37 Size(convertMm100ToPixel(rRectangle.GetWidth()),
38 convertMm100ToPixel(rRectangle.GetHeight())));
41 static Point convertTwipToMm100(const Point& rPoint)
43 return Point(::convertTwipToMm100(sal_Int64(rPoint.getX())),
44 ::convertTwipToMm100(sal_Int64(rPoint.getY())));
47 static tools::Rectangle convertTwipToMm100(const tools::Rectangle& rRectangle)
49 return tools::Rectangle(convertTwipToMm100(rRectangle.TopLeft()),
50 Size(::convertTwipToMm100(rRectangle.GetWidth()),
51 ::convertTwipToMm100(rRectangle.GetHeight())));
54 static bool postMouseEvent(SdrPage* pPage, SdrView* pDrawView, vcl::Window const& rMainWindow,
55 int nType, Point aPointHmm, int nCount, int nButtons, int nModifier)
57 SdrObjListIter aIterator(pPage, SdrIterMode::Flat);
58 while (aIterator.IsMore())
60 SdrObject* pObject = aIterator.Next();
61 SdrUnoObj* pUnoObect = dynamic_cast<SdrUnoObj*>(pObject);
62 if (pUnoObect)
64 tools::Rectangle aControlRectHMM = pUnoObect->GetLogicRect();
65 if (aControlRectHMM.IsInside(aPointHmm))
67 css::uno::Reference<css::awt::XControl> xControl
68 = pUnoObect->GetUnoControl(*pDrawView, rMainWindow);
69 if (!xControl.is())
70 return false;
72 css::uno::Reference<css::awt::XWindow> xControlWindow(xControl,
73 css::uno::UNO_QUERY);
74 if (!xControlWindow.is())
75 return false;
77 css::uno::Reference<css::awt::XWindowPeer> xWindowPeer(xControl->getPeer());
79 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow(xWindowPeer);
80 if (pWindow)
82 Point aControlRelativePositionHMM = aPointHmm - aControlRectHMM.TopLeft();
83 Point aControlRelativePosition
84 = convertMm100ToPixel(aControlRelativePositionHMM);
86 LokMouseEventData aMouseEventData(nType, aControlRelativePosition, nCount,
87 MouseEventModifiers::SIMPLECLICK,
88 nButtons, nModifier);
90 SfxLokHelper::postMouseEventAsync(pWindow, aMouseEventData);
91 return true;
96 return false;
99 static void drawUnoControl(SdrView* pDrawView, SdrUnoObj* pUnoObect,
100 vcl::Window const& rMainWindow, VirtualDevice& rDevice,
101 tools::Rectangle const& rTileRectHMM, double scaleX, double scaleY)
103 css::uno::Reference<css::awt::XControl> xControl
104 = pUnoObect->GetUnoControl(*pDrawView, rMainWindow);
105 if (!xControl.is())
106 return;
108 css::uno::Reference<css::awt::XWindow> xControlWindow(xControl, css::uno::UNO_QUERY);
109 if (!xControlWindow.is())
110 return;
112 css::uno::Reference<css::awt::XGraphics> xGraphics(rDevice.CreateUnoGraphics());
113 if (!xGraphics.is())
114 return;
116 css::uno::Reference<css::awt::XView> xControlView(xControl, css::uno::UNO_QUERY);
117 if (!xControlView.is())
118 return;
120 tools::Rectangle aObjectRectHMM = pUnoObect->GetLogicRect();
121 Point aOffsetFromTile = Point(aObjectRectHMM.Left() - rTileRectHMM.Left(),
122 aObjectRectHMM.Top() - rTileRectHMM.Top());
123 tools::Rectangle aRectangleHMM(aOffsetFromTile, aObjectRectHMM.GetSize());
124 tools::Rectangle aRectanglePx = convertMm100ToPixel(aRectangleHMM);
126 xControlWindow->setPosSize(0, 0, aRectanglePx.GetWidth(), aRectanglePx.GetHeight(),
127 css::awt::PosSize::POSSIZE);
129 xControlView->setGraphics(xGraphics);
131 xControlView->draw(aRectanglePx.Left() * scaleX, aRectanglePx.Top() * scaleY);
134 static void paintControlTile(SdrPage* pPage, SdrView* pDrawView, vcl::Window const& rMainWindow,
135 VirtualDevice& rDevice, Size aOutputSize,
136 tools::Rectangle const& rTileRect)
138 tools::Rectangle aTileRectHMM = convertTwipToMm100(rTileRect);
140 // Resizes the virtual device so to contain the entries context
141 rDevice.SetOutputSizePixel(aOutputSize);
143 rDevice.Push(PushFlags::MAPMODE);
144 MapMode aDeviceMapMode(rDevice.GetMapMode());
146 Fraction scaleX
147 = Fraction(aOutputSize.Width(), 96) * Fraction(1440) / Fraction(rTileRect.GetWidth());
148 Fraction scaleY
149 = Fraction(aOutputSize.Height(), 96) * Fraction(1440) / Fraction(rTileRect.GetHeight());
151 aDeviceMapMode.SetScaleX(scaleX);
152 aDeviceMapMode.SetScaleY(scaleY);
153 rDevice.SetMapMode(aDeviceMapMode);
155 SdrObjListIter aIterator(pPage, SdrIterMode::Flat);
157 while (aIterator.IsMore())
159 SdrObject* pObject = aIterator.Next();
160 SdrUnoObj* pUnoObect = dynamic_cast<SdrUnoObj*>(pObject);
161 if (pUnoObect)
163 tools::Rectangle aObjectRectHMM = pUnoObect->GetLogicRect();
165 // Check if we intersect with the tile rectangle and we
166 // need to draw the control.
167 if (aObjectRectHMM.IsOver(aTileRectHMM))
169 drawUnoControl(pDrawView, pUnoObect, rMainWindow, rDevice, aTileRectHMM,
170 double(scaleX), double(scaleY));
175 rDevice.Pop();
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */