1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/XWindow2.hpp>
24 #include <drawinglayer/geometry/viewinformation2d.hxx>
25 #include <vcl/virdev.hxx>
26 #include <vcl/svapp.hxx>
27 #include <com/sun/star/awt/PosSize.hpp>
28 #include <vcl/bitmapex.hxx>
29 #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
30 #include <tools/diagnose_ex.h>
31 #include <basegfx/polygon/b2dpolygontools.hxx>
32 #include <basegfx/polygon/b2dpolygon.hxx>
33 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
34 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
35 #include <svtools/optionsdrawinglayer.hxx>
36 #include <toolkit/awt/vclxwindow.hxx>
37 #include <vcl/window.hxx>
38 #include <basegfx/matrix/b2dhommatrixtools.hxx>
40 //////////////////////////////////////////////////////////////////////////////
42 using namespace com::sun::star
;
44 //////////////////////////////////////////////////////////////////////////////
46 namespace drawinglayer
50 void ControlPrimitive2D::createXControl()
52 if(!mxXControl
.is() && getControlModel().is())
54 uno::Reference
< beans::XPropertySet
> xSet(getControlModel(), uno::UNO_QUERY
);
58 uno::Any
aValue(xSet
->getPropertyValue("DefaultControl"));
59 OUString aUnoControlTypeName
;
61 if(aValue
>>= aUnoControlTypeName
)
63 if(!aUnoControlTypeName
.isEmpty())
65 uno::Reference
< uno::XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
66 uno::Reference
< awt::XControl
> xXControl(
67 xContext
->getServiceManager()->createInstanceWithContext(aUnoControlTypeName
, xContext
), uno::UNO_QUERY
);
71 xXControl
->setModel(getControlModel());
74 mxXControl
= xXControl
;
82 Primitive2DReference
ControlPrimitive2D::createBitmapDecomposition(const geometry::ViewInformation2D
& rViewInformation
) const
84 Primitive2DReference xRetval
;
85 const uno::Reference
< awt::XControl
>& rXControl(getXControl());
89 uno::Reference
< awt::XWindow
> xControlWindow(rXControl
, uno::UNO_QUERY
);
91 if(xControlWindow
.is())
93 // get decomposition to get size
94 basegfx::B2DVector aScale
, aTranslate
;
95 double fRotate
, fShearX
;
96 getTransform().decompose(aScale
, aTranslate
, fRotate
, fShearX
);
98 // get absolute discrete size (no mirror or rotate here)
99 aScale
= basegfx::absolute(aScale
);
100 basegfx::B2DVector
aDiscreteSize(rViewInformation
.getObjectToViewTransformation() * aScale
);
102 // limit to a maximum square size, e.g. 300x150 pixels (45000)
103 const SvtOptionsDrawinglayer aDrawinglayerOpt
;
104 const double fDiscreteMax(aDrawinglayerOpt
.GetQuadraticFormControlRenderLimit());
105 const double fDiscreteQuadratic(aDiscreteSize
.getX() * aDiscreteSize
.getY());
106 const bool bScaleUsed(fDiscreteQuadratic
> fDiscreteMax
);
111 // get factor and adapt to scaled size
112 fFactor
= sqrt(fDiscreteMax
/ fDiscreteQuadratic
);
113 aDiscreteSize
*= fFactor
;
117 const sal_Int32
nSizeX(basegfx::fround(aDiscreteSize
.getX()));
118 const sal_Int32
nSizeY(basegfx::fround(aDiscreteSize
.getY()));
120 if(nSizeX
> 0 && nSizeY
> 0)
122 // prepare VirtualDevice
123 VirtualDevice
aVirtualDevice(*Application::GetDefaultDevice());
124 const Size
aSizePixel(nSizeX
, nSizeY
);
125 aVirtualDevice
.SetOutputSizePixel(aSizePixel
);
127 // set size at control
128 xControlWindow
->setPosSize(0, 0, nSizeX
, nSizeY
, awt::PosSize::POSSIZE
);
130 // get graphics and view
131 uno::Reference
< awt::XGraphics
> xGraphics(aVirtualDevice
.CreateUnoGraphics());
132 uno::Reference
< awt::XView
> xControlView(rXControl
, uno::UNO_QUERY
);
134 if(xGraphics
.is() && xControlView
.is())
136 // link graphics and view
137 xControlView
->setGraphics(xGraphics
);
139 { // #i93162# For painting the control setting a Zoom (using setZoom() at the xControlView)
140 // is needed to define the font size. Normally this is done in
141 // ViewObjectContactOfUnoControl::createPrimitive2DSequence by using positionControlForPaint().
142 // For some reason the difference between MAP_TWIPS and MAP_100TH_MM still plays
143 // a role there so that for Draw/Impress/Calc (the MAP_100TH_MM users) i need to set a zoom
144 // here, too. The factor includes the needed scale, but is calculated by pure comparisons. It
145 // is somehow related to the twips/100thmm relationship.
146 bool bUserIs100thmm(false);
147 const uno::Reference
< awt::XControl
> xControl(xControlView
, uno::UNO_QUERY
);
151 uno::Reference
< awt::XWindowPeer
> xWindowPeer(xControl
->getPeer());
155 VCLXWindow
* pVCLXWindow
= VCLXWindow::GetImplementation(xWindowPeer
);
159 Window
* pWindow
= pVCLXWindow
->GetWindow();
163 pWindow
= pWindow
->GetParent();
167 if(MAP_100TH_MM
== pWindow
->GetMapMode().GetMapUnit())
169 bUserIs100thmm
= true;
179 // calc screen zoom for text display. fFactor is already added indirectly in aDiscreteSize
180 basegfx::B2DVector
aScreenZoom(
181 basegfx::fTools::equalZero(aScale
.getX()) ? 1.0 : aDiscreteSize
.getX() / aScale
.getX(),
182 basegfx::fTools::equalZero(aScale
.getY()) ? 1.0 : aDiscreteSize
.getY() / aScale
.getY());
183 static double fZoomScale(28.0); // do not ask for this constant factor, but it gets the zoom right
184 aScreenZoom
*= fZoomScale
;
186 // set zoom at control view for text scaling
187 xControlView
->setZoom((float)aScreenZoom
.getX(), (float)aScreenZoom
.getY());
193 // try to paint it to VirtualDevice
194 xControlView
->draw(0, 0);
197 const Bitmap
aContent(aVirtualDevice
.GetBitmap(Point(), aSizePixel
));
199 // to avoid scaling, use the Bitmap pixel size as primitive size
200 const Size
aBitmapSize(aContent
.GetSizePixel());
201 basegfx::B2DVector
aBitmapSizeLogic(
202 rViewInformation
.getInverseObjectToViewTransformation() *
203 basegfx::B2DVector(aBitmapSize
.getWidth() - 1, aBitmapSize
.getHeight() - 1));
207 // if scaled adapt to scaled size
208 aBitmapSizeLogic
/= fFactor
;
211 // short form for scale and translate transformation
212 const basegfx::B2DHomMatrix
aBitmapTransform(basegfx::tools::createScaleTranslateB2DHomMatrix(
213 aBitmapSizeLogic
.getX(), aBitmapSizeLogic
.getY(), aTranslate
.getX(), aTranslate
.getY()));
216 xRetval
= new BitmapPrimitive2D(BitmapEx(aContent
), aBitmapTransform
);
218 catch( const uno::Exception
& )
220 DBG_UNHANDLED_EXCEPTION();
230 Primitive2DReference
ControlPrimitive2D::createPlaceholderDecomposition(const geometry::ViewInformation2D
& /*rViewInformation*/) const
232 // create a gray placeholder hairline polygon in object size
233 basegfx::B2DRange
aObjectRange(0.0, 0.0, 1.0, 1.0);
234 aObjectRange
.transform(getTransform());
235 const basegfx::B2DPolygon
aOutline(basegfx::tools::createPolygonFromRect(aObjectRange
));
236 const basegfx::BColor
aGrayTone(0xc0 / 255.0, 0xc0 / 255.0, 0xc0 / 255.0);
238 // The replacement object may also get a text like 'empty group' here later
239 Primitive2DReference
xRetval(new PolygonHairlinePrimitive2D(aOutline
, aGrayTone
));
244 Primitive2DSequence
ControlPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D
& rViewInformation
) const
246 // try to create a bitmap decomposition. If that fails for some reason,
247 // at least create a replacement decomposition.
248 Primitive2DReference
xReference(createBitmapDecomposition(rViewInformation
));
252 xReference
= createPlaceholderDecomposition(rViewInformation
);
255 return Primitive2DSequence(&xReference
, 1L);
258 ControlPrimitive2D::ControlPrimitive2D(
259 const basegfx::B2DHomMatrix
& rTransform
,
260 const uno::Reference
< awt::XControlModel
>& rxControlModel
)
261 : BufferedDecompositionPrimitive2D(),
262 maTransform(rTransform
),
263 mxControlModel(rxControlModel
),
269 ControlPrimitive2D::ControlPrimitive2D(
270 const basegfx::B2DHomMatrix
& rTransform
,
271 const uno::Reference
< awt::XControlModel
>& rxControlModel
,
272 const uno::Reference
< awt::XControl
>& rxXControl
)
273 : BufferedDecompositionPrimitive2D(),
274 maTransform(rTransform
),
275 mxControlModel(rxControlModel
),
276 mxXControl(rxXControl
),
281 const uno::Reference
< awt::XControl
>& ControlPrimitive2D::getXControl() const
285 const_cast< ControlPrimitive2D
* >(this)->createXControl();
291 bool ControlPrimitive2D::operator==(const BasePrimitive2D
& rPrimitive
) const
293 // use base class compare operator
294 if(BufferedDecompositionPrimitive2D::operator==(rPrimitive
))
296 const ControlPrimitive2D
& rCompare
= (ControlPrimitive2D
&)rPrimitive
;
298 if(getTransform() == rCompare
.getTransform())
300 // check if ControlModel references both are/are not
301 bool bRetval(getControlModel().is() == rCompare
.getControlModel().is());
303 if(bRetval
&& getControlModel().is())
305 // both exist, check for equality
306 bRetval
= (getControlModel() == rCompare
.getControlModel());
311 // check if XControl references both are/are not
312 bRetval
= (getXControl().is() == rCompare
.getXControl().is());
315 if(bRetval
&& getXControl().is())
317 // both exist, check for equality
318 bRetval
= (getXControl() == rCompare
.getXControl());
328 basegfx::B2DRange
ControlPrimitive2D::getB2DRange(const geometry::ViewInformation2D
& /*rViewInformation*/) const
330 // simply derivate from unit range
331 basegfx::B2DRange
aRetval(0.0, 0.0, 1.0, 1.0);
332 aRetval
.transform(getTransform());
336 Primitive2DSequence
ControlPrimitive2D::get2DDecomposition(const geometry::ViewInformation2D
& rViewInformation
) const
338 // this primitive is view-dependent related to the scaling. If scaling has changed,
339 // destroy existing decomposition. To detect change, use size of unit size in view coordinates
340 ::osl::MutexGuard
aGuard( m_aMutex
);
341 const basegfx::B2DVector
aNewScaling(rViewInformation
.getObjectToViewTransformation() * basegfx::B2DVector(1.0, 1.0));
343 if(getBuffered2DDecomposition().hasElements())
345 if(!maLastViewScaling
.equal(aNewScaling
))
347 // conditions of last local decomposition have changed, delete
348 const_cast< ControlPrimitive2D
* >(this)->setBuffered2DDecomposition(Primitive2DSequence());
352 if(!getBuffered2DDecomposition().hasElements())
354 // remember ViewTransformation
355 const_cast< ControlPrimitive2D
* >(this)->maLastViewScaling
= aNewScaling
;
358 // use parent implementation
359 return BufferedDecompositionPrimitive2D::get2DDecomposition(rViewInformation
);
363 ImplPrimitive2DIDBlock(ControlPrimitive2D
, PRIMITIVE2D_ID_CONTROLPRIMITIVE2D
)
365 } // end of namespace primitive2d
366 } // end of namespace drawinglayer
368 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */