Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / drawinglayer / source / primitive2d / controlprimitive2d.cxx
blob3de3043086bcb51c3e640a09414f48be59bbb445
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 <drawinglayer/primitive2d/controlprimitive2d.hxx>
21 #include <com/sun/star/beans/XPropertySet.hpp>
22 #include <comphelper/processfactory.hxx>
23 #include <com/sun/star/awt/XControl.hpp>
24 #include <com/sun/star/awt/XWindow2.hpp>
25 #include <com/sun/star/uno/XComponentContext.hpp>
26 #include <drawinglayer/geometry/viewinformation2d.hxx>
27 #include <vcl/virdev.hxx>
28 #include <vcl/svapp.hxx>
29 #include <com/sun/star/awt/PosSize.hpp>
30 #include <vcl/bitmapex.hxx>
31 #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
32 #include <tools/diagnose_ex.h>
33 #include <basegfx/polygon/b2dpolygontools.hxx>
34 #include <basegfx/polygon/b2dpolygon.hxx>
35 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
36 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
37 #include <svtools/optionsdrawinglayer.hxx>
38 #include <toolkit/awt/vclxwindow.hxx>
39 #include <vcl/window.hxx>
40 #include <basegfx/matrix/b2dhommatrixtools.hxx>
42 using namespace com::sun::star;
44 namespace drawinglayer
46 namespace primitive2d
48 void ControlPrimitive2D::createXControl()
50 if(!mxXControl.is() && getControlModel().is())
52 uno::Reference< beans::XPropertySet > xSet(getControlModel(), uno::UNO_QUERY);
54 if(xSet.is())
56 uno::Any aValue(xSet->getPropertyValue("DefaultControl"));
57 OUString aUnoControlTypeName;
59 if(aValue >>= aUnoControlTypeName)
61 if(!aUnoControlTypeName.isEmpty())
63 uno::Reference< uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
64 uno::Reference< awt::XControl > xXControl(
65 xContext->getServiceManager()->createInstanceWithContext(aUnoControlTypeName, xContext), uno::UNO_QUERY);
67 if(xXControl.is())
69 xXControl->setModel(getControlModel());
71 // remember XControl
72 mxXControl = xXControl;
80 Primitive2DReference ControlPrimitive2D::createBitmapDecomposition(const geometry::ViewInformation2D& rViewInformation) const
82 Primitive2DReference xRetval;
83 const uno::Reference< awt::XControl >& rXControl(getXControl());
85 if(rXControl.is())
87 uno::Reference< awt::XWindow > xControlWindow(rXControl, uno::UNO_QUERY);
89 if(xControlWindow.is())
91 // get decomposition to get size
92 basegfx::B2DVector aScale, aTranslate;
93 double fRotate, fShearX;
94 getTransform().decompose(aScale, aTranslate, fRotate, fShearX);
96 // get absolute discrete size (no mirror or rotate here)
97 aScale = basegfx::absolute(aScale);
98 basegfx::B2DVector aDiscreteSize(rViewInformation.getObjectToViewTransformation() * aScale);
100 // limit to a maximum square size, e.g. 300x150 pixels (45000)
101 const SvtOptionsDrawinglayer aDrawinglayerOpt;
102 const double fDiscreteMax(aDrawinglayerOpt.GetQuadraticFormControlRenderLimit());
103 const double fDiscreteQuadratic(aDiscreteSize.getX() * aDiscreteSize.getY());
104 const bool bScaleUsed(fDiscreteQuadratic > fDiscreteMax);
105 double fFactor(1.0);
107 if(bScaleUsed)
109 // get factor and adapt to scaled size
110 fFactor = sqrt(fDiscreteMax / fDiscreteQuadratic);
111 aDiscreteSize *= fFactor;
114 // go to integer
115 const sal_Int32 nSizeX(basegfx::fround(aDiscreteSize.getX()));
116 const sal_Int32 nSizeY(basegfx::fround(aDiscreteSize.getY()));
118 if(nSizeX > 0 && nSizeY > 0)
120 // prepare VirtualDevice
121 ScopedVclPtrInstance< VirtualDevice > aVirtualDevice(*Application::GetDefaultDevice());
122 const Size aSizePixel(nSizeX, nSizeY);
123 aVirtualDevice->SetOutputSizePixel(aSizePixel);
125 // set size at control
126 xControlWindow->setPosSize(0, 0, nSizeX, nSizeY, awt::PosSize::POSSIZE);
128 // get graphics and view
129 uno::Reference< awt::XGraphics > xGraphics(aVirtualDevice->CreateUnoGraphics());
130 uno::Reference< awt::XView > xControlView(rXControl, uno::UNO_QUERY);
132 if(xGraphics.is() && xControlView.is())
134 // link graphics and view
135 xControlView->setGraphics(xGraphics);
137 { // #i93162# For painting the control setting a Zoom (using setZoom() at the xControlView)
138 // is needed to define the font size. Normally this is done in
139 // ViewObjectContactOfUnoControl::createPrimitive2DSequence by using positionControlForPaint().
140 // For some reason the difference between MapUnit::MapTwipS and MapUnit::Map100thMM still plays
141 // a role there so that for Draw/Impress/Calc (the MapUnit::Map100thMM users) i need to set a zoom
142 // here, too. The factor includes the needed scale, but is calculated by pure comparisons. It
143 // is somehow related to the twips/100thmm relationship.
144 bool bUserIs100thmm(false);
145 const uno::Reference< awt::XControl > xControl(xControlView, uno::UNO_QUERY);
147 if(xControl.is())
149 uno::Reference< awt::XWindowPeer > xWindowPeer(xControl->getPeer());
151 if(xWindowPeer.is())
153 VCLXWindow* pVCLXWindow = comphelper::getUnoTunnelImplementation<VCLXWindow>(xWindowPeer);
155 if(pVCLXWindow)
157 VclPtr<vcl::Window> pWindow = pVCLXWindow->GetWindow();
159 if(pWindow)
161 pWindow = pWindow->GetParent();
163 if(pWindow && MapUnit::Map100thMM == pWindow->GetMapMode().GetMapUnit())
165 bUserIs100thmm = true;
172 if(bUserIs100thmm)
174 // calc screen zoom for text display. fFactor is already added indirectly in aDiscreteSize
175 basegfx::B2DVector aScreenZoom(
176 basegfx::fTools::equalZero(aScale.getX()) ? 1.0 : aDiscreteSize.getX() / aScale.getX(),
177 basegfx::fTools::equalZero(aScale.getY()) ? 1.0 : aDiscreteSize.getY() / aScale.getY());
178 static const double fZoomScale(28.0); // do not ask for this constant factor, but it gets the zoom right
179 aScreenZoom *= fZoomScale;
181 // set zoom at control view for text scaling
182 xControlView->setZoom(static_cast<float>(aScreenZoom.getX()), static_cast<float>(aScreenZoom.getY()));
188 // try to paint it to VirtualDevice
189 xControlView->draw(0, 0);
191 // get bitmap
192 const BitmapEx aContent(aVirtualDevice->GetBitmapEx(Point(), aSizePixel));
194 // to avoid scaling, use the Bitmap pixel size as primitive size
195 const Size aBitmapSize(aContent.GetSizePixel());
196 basegfx::B2DVector aBitmapSizeLogic(
197 rViewInformation.getInverseObjectToViewTransformation() *
198 basegfx::B2DVector(aBitmapSize.getWidth() - 1, aBitmapSize.getHeight() - 1));
200 if(bScaleUsed)
202 // if scaled adapt to scaled size
203 aBitmapSizeLogic /= fFactor;
206 // short form for scale and translate transformation
207 const basegfx::B2DHomMatrix aBitmapTransform(basegfx::utils::createScaleTranslateB2DHomMatrix(
208 aBitmapSizeLogic.getX(), aBitmapSizeLogic.getY(), aTranslate.getX(), aTranslate.getY()));
210 // create primitive
211 xRetval = new BitmapPrimitive2D(aContent, aBitmapTransform);
213 catch( const uno::Exception& )
215 DBG_UNHANDLED_EXCEPTION("drawinglayer");
222 return xRetval;
225 Primitive2DReference ControlPrimitive2D::createPlaceholderDecomposition() const
227 // create a gray placeholder hairline polygon in object size
228 basegfx::B2DRange aObjectRange(0.0, 0.0, 1.0, 1.0);
229 aObjectRange.transform(getTransform());
230 const basegfx::B2DPolygon aOutline(basegfx::utils::createPolygonFromRect(aObjectRange));
231 const basegfx::BColor aGrayTone(0xc0 / 255.0, 0xc0 / 255.0, 0xc0 / 255.0);
233 // The replacement object may also get a text like 'empty group' here later
234 Primitive2DReference xRetval(new PolygonHairlinePrimitive2D(aOutline, aGrayTone));
236 return xRetval;
239 void ControlPrimitive2D::create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& rViewInformation) const
241 // try to create a bitmap decomposition. If that fails for some reason,
242 // at least create a replacement decomposition.
243 Primitive2DReference xReference(createBitmapDecomposition(rViewInformation));
245 if(!xReference.is())
247 xReference = createPlaceholderDecomposition();
250 rContainer.push_back(xReference);
253 ControlPrimitive2D::ControlPrimitive2D(
254 const basegfx::B2DHomMatrix& rTransform,
255 const uno::Reference< awt::XControlModel >& rxControlModel)
256 : BufferedDecompositionPrimitive2D(),
257 maTransform(rTransform),
258 mxControlModel(rxControlModel),
259 mxXControl(),
260 maLastViewScaling()
264 ControlPrimitive2D::ControlPrimitive2D(
265 const basegfx::B2DHomMatrix& rTransform,
266 const uno::Reference< awt::XControlModel >& rxControlModel,
267 const uno::Reference< awt::XControl >& rxXControl)
268 : BufferedDecompositionPrimitive2D(),
269 maTransform(rTransform),
270 mxControlModel(rxControlModel),
271 mxXControl(rxXControl),
272 maLastViewScaling()
276 const uno::Reference< awt::XControl >& ControlPrimitive2D::getXControl() const
278 if(!mxXControl.is())
280 const_cast< ControlPrimitive2D* >(this)->createXControl();
283 return mxXControl;
286 bool ControlPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
288 // use base class compare operator
289 if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
291 const ControlPrimitive2D& rCompare = static_cast<const ControlPrimitive2D&>(rPrimitive);
293 if(getTransform() == rCompare.getTransform())
295 // check if ControlModel references both are/are not
296 bool bRetval(getControlModel().is() == rCompare.getControlModel().is());
298 if(bRetval && getControlModel().is())
300 // both exist, check for equality
301 bRetval = (getControlModel() == rCompare.getControlModel());
304 if(bRetval)
306 // check if XControl references both are/are not
307 bRetval = (getXControl().is() == rCompare.getXControl().is());
310 if(bRetval && getXControl().is())
312 // both exist, check for equality
313 bRetval = (getXControl() == rCompare.getXControl());
316 return bRetval;
320 return false;
323 basegfx::B2DRange ControlPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
325 // simply derivate from unit range
326 basegfx::B2DRange aRetval(0.0, 0.0, 1.0, 1.0);
327 aRetval.transform(getTransform());
328 return aRetval;
331 void ControlPrimitive2D::get2DDecomposition(Primitive2DDecompositionVisitor& rVisitor, const geometry::ViewInformation2D& rViewInformation) const
333 // this primitive is view-dependent related to the scaling. If scaling has changed,
334 // destroy existing decomposition. To detect change, use size of unit size in view coordinates
335 ::osl::MutexGuard aGuard( m_aMutex );
336 const basegfx::B2DVector aNewScaling(rViewInformation.getObjectToViewTransformation() * basegfx::B2DVector(1.0, 1.0));
338 if(!getBuffered2DDecomposition().empty())
340 if(!maLastViewScaling.equal(aNewScaling))
342 // conditions of last local decomposition have changed, delete
343 const_cast< ControlPrimitive2D* >(this)->setBuffered2DDecomposition(Primitive2DContainer());
347 if(getBuffered2DDecomposition().empty())
349 // remember ViewTransformation
350 const_cast< ControlPrimitive2D* >(this)->maLastViewScaling = aNewScaling;
353 // use parent implementation
354 BufferedDecompositionPrimitive2D::get2DDecomposition(rVisitor, rViewInformation);
357 // provide unique ID
358 ImplPrimitive2DIDBlock(ControlPrimitive2D, PRIMITIVE2D_ID_CONTROLPRIMITIVE2D)
360 } // end of namespace primitive2d
361 } // end of namespace drawinglayer
363 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */