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/awt/XWindow.hpp>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <comphelper/processfactory.hxx>
24 #include <com/sun/star/awt/XControl.hpp>
25 #include <com/sun/star/uno/XComponentContext.hpp>
26 #include <drawinglayer/geometry/viewinformation2d.hxx>
28 #include <rtl/ustrbuf.hxx>
29 #include <vcl/virdev.hxx>
30 #include <vcl/svapp.hxx>
31 #include <com/sun/star/awt/PosSize.hpp>
32 #include <vcl/bitmapex.hxx>
33 #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
34 #include <comphelper/diagnose_ex.hxx>
35 #include <basegfx/polygon/b2dpolygontools.hxx>
36 #include <basegfx/polygon/b2dpolygon.hxx>
37 #include <drawinglayer/primitive2d/PolygonHairlinePrimitive2D.hxx>
38 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
39 #include <svtools/optionsdrawinglayer.hxx>
40 #include <vcl/window.hxx>
41 #include <basegfx/matrix/b2dhommatrixtools.hxx>
42 #include <toolkit/helper/vclunohelper.hxx>
44 using namespace com::sun::star
;
46 namespace drawinglayer::primitive2d
48 void ControlPrimitive2D::createXControl()
50 if(mxXControl
.is() || !getControlModel().is())
53 uno::Reference
< beans::XPropertySet
> xSet(getControlModel(), uno::UNO_QUERY
);
58 uno::Any
aValue(xSet
->getPropertyValue("DefaultControl"));
59 OUString aUnoControlTypeName
;
61 if(!(aValue
>>= aUnoControlTypeName
))
64 if(aUnoControlTypeName
.isEmpty())
67 uno::Reference
< uno::XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
68 uno::Reference
< awt::XControl
> xXControl(
69 xContext
->getServiceManager()->createInstanceWithContext(aUnoControlTypeName
, xContext
), uno::UNO_QUERY
);
73 xXControl
->setModel(getControlModel());
76 mxXControl
= xXControl
;
80 Primitive2DReference
ControlPrimitive2D::createBitmapDecomposition(const geometry::ViewInformation2D
& rViewInformation
) const
82 Primitive2DReference xRetval
;
83 const uno::Reference
< awt::XControl
>& rXControl(getXControl());
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 double fDiscreteMax(SvtOptionsDrawinglayer::GetQuadraticFormControlRenderLimit());
102 const double fDiscreteQuadratic(aDiscreteSize
.getX() * aDiscreteSize
.getY());
103 const bool bScaleUsed(fDiscreteQuadratic
> fDiscreteMax
);
108 // get factor and adapt to scaled size
109 fFactor
= sqrt(fDiscreteMax
/ fDiscreteQuadratic
);
110 aDiscreteSize
*= fFactor
;
114 const sal_Int32
nSizeX(basegfx::fround(aDiscreteSize
.getX()));
115 const sal_Int32
nSizeY(basegfx::fround(aDiscreteSize
.getY()));
117 if(nSizeX
> 0 && nSizeY
> 0)
119 // prepare VirtualDevice
120 ScopedVclPtrInstance
< VirtualDevice
> aVirtualDevice(*Application::GetDefaultDevice());
121 const Size
aSizePixel(nSizeX
, nSizeY
);
122 aVirtualDevice
->SetOutputSizePixel(aSizePixel
);
124 // set size at control
125 xControlWindow
->setPosSize(0, 0, nSizeX
, nSizeY
, awt::PosSize::POSSIZE
);
127 // get graphics and view
128 uno::Reference
< awt::XGraphics
> xGraphics(aVirtualDevice
->CreateUnoGraphics());
129 uno::Reference
< awt::XView
> xControlView(rXControl
, uno::UNO_QUERY
);
131 if(xGraphics
.is() && xControlView
.is())
133 // link graphics and view
134 xControlView
->setGraphics(xGraphics
);
136 { // #i93162# For painting the control setting a Zoom (using setZoom() at the xControlView)
137 // is needed to define the font size. Normally this is done in
138 // ViewObjectContactOfUnoControl::createPrimitive2DSequence by using positionControlForPaint().
139 // For some reason the difference between MapUnit::MapTwipS and MapUnit::Map100thMM still plays
140 // a role there so that for Draw/Impress/Calc (the MapUnit::Map100thMM users) i need to set a zoom
141 // here, too. The factor includes the needed scale, but is calculated by pure comparisons. It
142 // is somehow related to the twips/100thmm relationship.
143 bool bUserIs100thmm(false);
144 const uno::Reference
< awt::XControl
> xControl(xControlView
, uno::UNO_QUERY
);
148 uno::Reference
<awt::XWindowPeer
> xWindowPeer(xControl
->getPeer());
150 VclPtr
<vcl::Window
> pWindow
= VCLUnoHelper::GetWindow(xWindowPeer
);
153 pWindow
= pWindow
->GetParent();
155 if(pWindow
&& MapUnit::Map100thMM
== pWindow
->GetMapMode().GetMapUnit())
157 bUserIs100thmm
= true;
164 // calc screen zoom for text display. fFactor is already added indirectly in aDiscreteSize
165 basegfx::B2DVector
aScreenZoom(
166 basegfx::fTools::equalZero(aScale
.getX()) ? 1.0 : aDiscreteSize
.getX() / aScale
.getX(),
167 basegfx::fTools::equalZero(aScale
.getY()) ? 1.0 : aDiscreteSize
.getY() / aScale
.getY());
168 static const double fZoomScale(28.0); // do not ask for this constant factor, but it gets the zoom right
169 aScreenZoom
*= fZoomScale
;
171 // set zoom at control view for text scaling
172 xControlView
->setZoom(static_cast<float>(aScreenZoom
.getX()), static_cast<float>(aScreenZoom
.getY()));
178 // try to paint it to VirtualDevice
179 xControlView
->draw(0, 0);
182 const BitmapEx
aContent(aVirtualDevice
->GetBitmapEx(Point(), aSizePixel
));
184 // to avoid scaling, use the Bitmap pixel size as primitive size
185 const Size
aBitmapSize(aContent
.GetSizePixel());
186 basegfx::B2DVector
aBitmapSizeLogic(
187 rViewInformation
.getInverseObjectToViewTransformation() *
188 basegfx::B2DVector(aBitmapSize
.getWidth() - 1, aBitmapSize
.getHeight() - 1));
192 // if scaled adapt to scaled size
193 aBitmapSizeLogic
/= fFactor
;
196 // short form for scale and translate transformation
197 const basegfx::B2DHomMatrix
aBitmapTransform(basegfx::utils::createScaleTranslateB2DHomMatrix(
198 aBitmapSizeLogic
.getX(), aBitmapSizeLogic
.getY(), aTranslate
.getX(), aTranslate
.getY()));
201 xRetval
= new BitmapPrimitive2D(
205 catch( const uno::Exception
& )
207 DBG_UNHANDLED_EXCEPTION("drawinglayer");
217 Primitive2DReference
ControlPrimitive2D::createPlaceholderDecomposition() const
219 // create a gray placeholder hairline polygon in object size
220 basegfx::B2DRange
aObjectRange(0.0, 0.0, 1.0, 1.0);
221 aObjectRange
.transform(getTransform());
222 basegfx::B2DPolygon
aOutline(basegfx::utils::createPolygonFromRect(aObjectRange
));
223 const basegfx::BColor
aGrayTone(0xc0 / 255.0, 0xc0 / 255.0, 0xc0 / 255.0);
225 // The replacement object may also get a text like 'empty group' here later
226 Primitive2DReference
xRetval(new PolygonHairlinePrimitive2D(std::move(aOutline
), aGrayTone
));
231 void ControlPrimitive2D::create2DDecomposition(Primitive2DContainer
& rContainer
, const geometry::ViewInformation2D
& rViewInformation
) const
233 // try to create a bitmap decomposition. If that fails for some reason,
234 // at least create a replacement decomposition.
235 Primitive2DReference
xReference(createBitmapDecomposition(rViewInformation
));
239 xReference
= createPlaceholderDecomposition();
242 rContainer
.push_back(xReference
);
245 ControlPrimitive2D::ControlPrimitive2D(
246 basegfx::B2DHomMatrix aTransform
,
247 uno::Reference
< awt::XControlModel
> xControlModel
,
248 uno::Reference
<awt::XControl
> xXControl
,
249 ::std::u16string_view
const rTitle
,
250 ::std::u16string_view
const rDescription
)
251 : maTransform(std::move(aTransform
)),
252 mxControlModel(std::move(xControlModel
)),
253 mxXControl(std::move(xXControl
))
255 ::rtl::OUStringBuffer
buf(rTitle
);
256 if (!rTitle
.empty() && !rDescription
.empty())
260 buf
.append(rDescription
);
261 m_AltText
= buf
.makeStringAndClear();
264 const uno::Reference
< awt::XControl
>& ControlPrimitive2D::getXControl() const
268 const_cast< ControlPrimitive2D
* >(this)->createXControl();
274 bool ControlPrimitive2D::operator==(const BasePrimitive2D
& rPrimitive
) const
276 // use base class compare operator
277 if(!BufferedDecompositionPrimitive2D::operator==(rPrimitive
))
280 const ControlPrimitive2D
& rCompare
= static_cast<const ControlPrimitive2D
&>(rPrimitive
);
282 if(getTransform() != rCompare
.getTransform())
285 // check if ControlModel references both are/are not
286 if (getControlModel().is() != rCompare
.getControlModel().is())
289 if(getControlModel().is())
291 // both exist, check for equality
292 if (getControlModel() != rCompare
.getControlModel())
296 // check if XControl references both are/are not
297 if (getXControl().is() != rCompare
.getXControl().is())
300 if(getXControl().is())
302 // both exist, check for equality
303 if (getXControl() != rCompare
.getXControl())
310 basegfx::B2DRange
ControlPrimitive2D::getB2DRange(const geometry::ViewInformation2D
& /*rViewInformation*/) const
312 // simply derivate from unit range
313 basegfx::B2DRange
aRetval(0.0, 0.0, 1.0, 1.0);
314 aRetval
.transform(getTransform());
318 void ControlPrimitive2D::get2DDecomposition(Primitive2DDecompositionVisitor
& rVisitor
, const geometry::ViewInformation2D
& rViewInformation
) const
320 // this primitive is view-dependent related to the scaling. If scaling has changed,
321 // destroy existing decomposition. To detect change, use size of unit size in view coordinates
322 const basegfx::B2DVector
aNewScaling(rViewInformation
.getObjectToViewTransformation() * basegfx::B2DVector(1.0, 1.0));
324 if(!getBuffered2DDecomposition().empty())
326 if(!maLastViewScaling
.equal(aNewScaling
))
328 // conditions of last local decomposition have changed, delete
329 const_cast< ControlPrimitive2D
* >(this)->setBuffered2DDecomposition(Primitive2DContainer());
333 if(getBuffered2DDecomposition().empty())
335 // remember ViewTransformation
336 const_cast< ControlPrimitive2D
* >(this)->maLastViewScaling
= aNewScaling
;
339 // use parent implementation
340 BufferedDecompositionPrimitive2D::get2DDecomposition(rVisitor
, rViewInformation
);
344 sal_uInt32
ControlPrimitive2D::getPrimitive2DID() const
346 return PRIMITIVE2D_ID_CONTROLPRIMITIVE2D
;
349 } // end of namespace
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */