nss: upgrade to release 3.73
[LibreOffice.git] / drawinglayer / source / primitive2d / controlprimitive2d.cxx
blob3f20baa1351fa353c6d2e3a20c3e99900dd0edcf
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/uno/XComponentContext.hpp>
25 #include <drawinglayer/geometry/viewinformation2d.hxx>
26 #include <vcl/virdev.hxx>
27 #include <vcl/svapp.hxx>
28 #include <com/sun/star/awt/PosSize.hpp>
29 #include <vcl/bitmapex.hxx>
30 #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
31 #include <tools/diagnose_ex.h>
32 #include <basegfx/polygon/b2dpolygontools.hxx>
33 #include <basegfx/polygon/b2dpolygon.hxx>
34 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
35 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
36 #include <svtools/optionsdrawinglayer.hxx>
37 #include <toolkit/awt/vclxwindow.hxx>
38 #include <vcl/window.hxx>
39 #include <basegfx/matrix/b2dhommatrixtools.hxx>
40 #include <toolkit/helper/vclunohelper.hxx>
42 using namespace com::sun::star;
44 namespace drawinglayer::primitive2d
46 void ControlPrimitive2D::createXControl()
48 if(mxXControl.is() || !getControlModel().is())
49 return;
51 uno::Reference< beans::XPropertySet > xSet(getControlModel(), uno::UNO_QUERY);
53 if(!xSet.is())
54 return;
56 uno::Any aValue(xSet->getPropertyValue("DefaultControl"));
57 OUString aUnoControlTypeName;
59 if(!(aValue >>= aUnoControlTypeName))
60 return;
62 if(aUnoControlTypeName.isEmpty())
63 return;
65 uno::Reference< uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
66 uno::Reference< awt::XControl > xXControl(
67 xContext->getServiceManager()->createInstanceWithContext(aUnoControlTypeName, xContext), uno::UNO_QUERY);
69 if(xXControl.is())
71 xXControl->setModel(getControlModel());
73 // remember XControl
74 mxXControl = xXControl;
78 Primitive2DReference ControlPrimitive2D::createBitmapDecomposition(const geometry::ViewInformation2D& rViewInformation) const
80 Primitive2DReference xRetval;
81 const uno::Reference< awt::XControl >& rXControl(getXControl());
83 if(rXControl.is())
85 uno::Reference< awt::XWindow > xControlWindow(rXControl, uno::UNO_QUERY);
87 if(xControlWindow.is())
89 // get decomposition to get size
90 basegfx::B2DVector aScale, aTranslate;
91 double fRotate, fShearX;
92 getTransform().decompose(aScale, aTranslate, fRotate, fShearX);
94 // get absolute discrete size (no mirror or rotate here)
95 aScale = basegfx::absolute(aScale);
96 basegfx::B2DVector aDiscreteSize(rViewInformation.getObjectToViewTransformation() * aScale);
98 // limit to a maximum square size, e.g. 300x150 pixels (45000)
99 const SvtOptionsDrawinglayer aDrawinglayerOpt;
100 const double fDiscreteMax(aDrawinglayerOpt.GetQuadraticFormControlRenderLimit());
101 const double fDiscreteQuadratic(aDiscreteSize.getX() * aDiscreteSize.getY());
102 const bool bScaleUsed(fDiscreteQuadratic > fDiscreteMax);
103 double fFactor(1.0);
105 if(bScaleUsed)
107 // get factor and adapt to scaled size
108 fFactor = sqrt(fDiscreteMax / fDiscreteQuadratic);
109 aDiscreteSize *= fFactor;
112 // go to integer
113 const sal_Int32 nSizeX(basegfx::fround(aDiscreteSize.getX()));
114 const sal_Int32 nSizeY(basegfx::fround(aDiscreteSize.getY()));
116 if(nSizeX > 0 && nSizeY > 0)
118 // prepare VirtualDevice
119 ScopedVclPtrInstance< VirtualDevice > aVirtualDevice(*Application::GetDefaultDevice());
120 const Size aSizePixel(nSizeX, nSizeY);
121 aVirtualDevice->SetOutputSizePixel(aSizePixel);
123 // set size at control
124 xControlWindow->setPosSize(0, 0, nSizeX, nSizeY, awt::PosSize::POSSIZE);
126 // get graphics and view
127 uno::Reference< awt::XGraphics > xGraphics(aVirtualDevice->CreateUnoGraphics());
128 uno::Reference< awt::XView > xControlView(rXControl, uno::UNO_QUERY);
130 if(xGraphics.is() && xControlView.is())
132 // link graphics and view
133 xControlView->setGraphics(xGraphics);
135 { // #i93162# For painting the control setting a Zoom (using setZoom() at the xControlView)
136 // is needed to define the font size. Normally this is done in
137 // ViewObjectContactOfUnoControl::createPrimitive2DSequence by using positionControlForPaint().
138 // For some reason the difference between MapUnit::MapTwipS and MapUnit::Map100thMM still plays
139 // a role there so that for Draw/Impress/Calc (the MapUnit::Map100thMM users) i need to set a zoom
140 // here, too. The factor includes the needed scale, but is calculated by pure comparisons. It
141 // is somehow related to the twips/100thmm relationship.
142 bool bUserIs100thmm(false);
143 const uno::Reference< awt::XControl > xControl(xControlView, uno::UNO_QUERY);
145 if(xControl.is())
147 uno::Reference< awt::XWindowPeer > xWindowPeer(xControl->getPeer());
149 if(xWindowPeer.is())
151 VCLXWindow* pVCLXWindow = comphelper::getUnoTunnelImplementation<VCLXWindow>(xWindowPeer);
153 if(pVCLXWindow)
155 VclPtr<vcl::Window> pWindow = pVCLXWindow->GetWindow();
157 if(pWindow)
159 pWindow = pWindow->GetParent();
161 if(pWindow && MapUnit::Map100thMM == pWindow->GetMapMode().GetMapUnit())
163 bUserIs100thmm = true;
170 if(bUserIs100thmm)
172 // calc screen zoom for text display. fFactor is already added indirectly in aDiscreteSize
173 basegfx::B2DVector aScreenZoom(
174 basegfx::fTools::equalZero(aScale.getX()) ? 1.0 : aDiscreteSize.getX() / aScale.getX(),
175 basegfx::fTools::equalZero(aScale.getY()) ? 1.0 : aDiscreteSize.getY() / aScale.getY());
176 static const double fZoomScale(28.0); // do not ask for this constant factor, but it gets the zoom right
177 aScreenZoom *= fZoomScale;
179 // set zoom at control view for text scaling
180 xControlView->setZoom(static_cast<float>(aScreenZoom.getX()), static_cast<float>(aScreenZoom.getY()));
186 // try to paint it to VirtualDevice
187 xControlView->draw(0, 0);
189 // get bitmap
190 const BitmapEx aContent(aVirtualDevice->GetBitmapEx(Point(), aSizePixel));
192 // to avoid scaling, use the Bitmap pixel size as primitive size
193 const Size aBitmapSize(aContent.GetSizePixel());
194 basegfx::B2DVector aBitmapSizeLogic(
195 rViewInformation.getInverseObjectToViewTransformation() *
196 basegfx::B2DVector(aBitmapSize.getWidth() - 1, aBitmapSize.getHeight() - 1));
198 if(bScaleUsed)
200 // if scaled adapt to scaled size
201 aBitmapSizeLogic /= fFactor;
204 // short form for scale and translate transformation
205 const basegfx::B2DHomMatrix aBitmapTransform(basegfx::utils::createScaleTranslateB2DHomMatrix(
206 aBitmapSizeLogic.getX(), aBitmapSizeLogic.getY(), aTranslate.getX(), aTranslate.getY()));
208 // create primitive
209 xRetval = new BitmapPrimitive2D(
210 VCLUnoHelper::CreateVCLXBitmap(aContent),
211 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
362 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */