tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / drawinglayer / source / primitive2d / controlprimitive2d.cxx
blob28d5b5d1d1c2da34b75138845c67854c6bc1457e
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/awt/XWindow.hpp>
22 #include <com/sun/star/awt/XVclWindowPeer.hpp>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <comphelper/processfactory.hxx>
25 #include <com/sun/star/awt/XControl.hpp>
26 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <drawinglayer/geometry/viewinformation2d.hxx>
28 #include <utility>
29 #include <rtl/ustrbuf.hxx>
30 #include <vcl/virdev.hxx>
31 #include <vcl/svapp.hxx>
32 #include <com/sun/star/awt/PosSize.hpp>
33 #include <vcl/bitmapex.hxx>
34 #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
35 #include <comphelper/diagnose_ex.hxx>
36 #include <basegfx/polygon/b2dpolygontools.hxx>
37 #include <basegfx/polygon/b2dpolygon.hxx>
38 #include <drawinglayer/primitive2d/PolygonHairlinePrimitive2D.hxx>
39 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
40 #include <vcl/window.hxx>
41 #include <basegfx/matrix/b2dhommatrixtools.hxx>
42 #include <toolkit/helper/vclunohelper.hxx>
43 #include <officecfg/Office/Common.hxx>
45 using namespace com::sun::star;
47 namespace drawinglayer::primitive2d
49 void ControlPrimitive2D::createXControl()
51 if(mxXControl.is() || !getControlModel().is())
52 return;
54 uno::Reference< beans::XPropertySet > xSet(getControlModel(), uno::UNO_QUERY);
56 if(!xSet.is())
57 return;
59 uno::Any aValue(xSet->getPropertyValue(u"DefaultControl"_ustr));
60 OUString aUnoControlTypeName;
62 if(!(aValue >>= aUnoControlTypeName))
63 return;
65 if(aUnoControlTypeName.isEmpty())
66 return;
68 const uno::Reference< uno::XComponentContext >& xContext( ::comphelper::getProcessComponentContext() );
69 uno::Reference< awt::XControl > xXControl(
70 xContext->getServiceManager()->createInstanceWithContext(aUnoControlTypeName, xContext), uno::UNO_QUERY);
72 if(xXControl.is())
74 xXControl->setModel(getControlModel());
76 // remember XControl
77 mxXControl = std::move(xXControl);
81 Primitive2DReference ControlPrimitive2D::createBitmapDecomposition(const geometry::ViewInformation2D& rViewInformation) const
83 Primitive2DReference xRetval;
84 const uno::Reference< awt::XControl >& rXControl(getXControl());
86 if(rXControl.is())
88 uno::Reference< awt::XWindow > xControlWindow(rXControl, uno::UNO_QUERY);
90 if(xControlWindow.is())
92 // get decomposition to get size
93 basegfx::B2DVector aScale, aTranslate;
94 double fRotate, fShearX;
95 getTransform().decompose(aScale, aTranslate, fRotate, fShearX);
97 // get absolute discrete size (no mirror or rotate here)
98 aScale = basegfx::absolute(aScale);
99 basegfx::B2DVector aDiscreteSize(rViewInformation.getObjectToViewTransformation() * aScale);
101 // limit to a maximum square size, e.g. 300x150 pixels (45000)
102 const double fDiscreteMax(officecfg::Office::Common::Drawinglayer::QuadraticFormControlRenderLimit::get());
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());
150 if (xWindowPeer)
152 uno::Reference<awt::XVclWindowPeer> xPeerProps(xWindowPeer, uno::UNO_QUERY_THROW);
153 uno::Any aAny = xPeerProps->getProperty(u"ParentIs100thmm"_ustr); // see VCLXWindow::getProperty
154 aAny >>= bUserIs100thmm;
158 if(bUserIs100thmm)
160 // calc screen zoom for text display. fFactor is already added indirectly in aDiscreteSize
161 basegfx::B2DVector aScreenZoom(
162 basegfx::fTools::equalZero(aScale.getX()) ? 1.0 : aDiscreteSize.getX() / aScale.getX(),
163 basegfx::fTools::equalZero(aScale.getY()) ? 1.0 : aDiscreteSize.getY() / aScale.getY());
164 static const double fZoomScale(28.0); // do not ask for this constant factor, but it gets the zoom right
165 aScreenZoom *= fZoomScale;
167 // set zoom at control view for text scaling
168 xControlView->setZoom(static_cast<float>(aScreenZoom.getX()), static_cast<float>(aScreenZoom.getY()));
174 // try to paint it to VirtualDevice
175 xControlView->draw(0, 0);
177 // get bitmap
178 const BitmapEx aContent(aVirtualDevice->GetBitmapEx(Point(), aSizePixel));
180 // to avoid scaling, use the Bitmap pixel size as primitive size
181 const Size aBitmapSize(aContent.GetSizePixel());
182 basegfx::B2DVector aBitmapSizeLogic(
183 rViewInformation.getInverseObjectToViewTransformation() *
184 basegfx::B2DVector(aBitmapSize.getWidth() - 1, aBitmapSize.getHeight() - 1));
186 if(bScaleUsed)
188 // if scaled adapt to scaled size
189 aBitmapSizeLogic /= fFactor;
192 // short form for scale and translate transformation
193 const basegfx::B2DHomMatrix aBitmapTransform(basegfx::utils::createScaleTranslateB2DHomMatrix(
194 aBitmapSizeLogic.getX(), aBitmapSizeLogic.getY(), aTranslate.getX(), aTranslate.getY()));
196 // create primitive
197 xRetval = new BitmapPrimitive2D(
198 aContent,
199 aBitmapTransform);
201 catch( const uno::Exception& )
203 DBG_UNHANDLED_EXCEPTION("drawinglayer");
210 return xRetval;
213 Primitive2DReference ControlPrimitive2D::createPlaceholderDecomposition() const
215 // create a gray placeholder hairline polygon in object size
216 basegfx::B2DRange aObjectRange(0.0, 0.0, 1.0, 1.0);
217 aObjectRange.transform(getTransform());
218 basegfx::B2DPolygon aOutline(basegfx::utils::createPolygonFromRect(aObjectRange));
219 const basegfx::BColor aGrayTone(0xc0 / 255.0, 0xc0 / 255.0, 0xc0 / 255.0);
221 // The replacement object may also get a text like 'empty group' here later
222 Primitive2DReference xRetval(new PolygonHairlinePrimitive2D(std::move(aOutline), aGrayTone));
224 return xRetval;
227 Primitive2DReference ControlPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
229 // try to create a bitmap decomposition. If that fails for some reason,
230 // at least create a replacement decomposition.
231 Primitive2DReference xReference(createBitmapDecomposition(rViewInformation));
233 if(!xReference.is())
235 xReference = createPlaceholderDecomposition();
238 return xReference;
241 ControlPrimitive2D::ControlPrimitive2D(
242 basegfx::B2DHomMatrix aTransform,
243 uno::Reference< awt::XControlModel > xControlModel,
244 uno::Reference<awt::XControl> xXControl,
245 ::std::u16string_view const rTitle,
246 ::std::u16string_view const rDescription,
247 void const*const pAnchorKey)
248 : maTransform(std::move(aTransform)),
249 mxControlModel(std::move(xControlModel)),
250 mxXControl(std::move(xXControl))
251 , m_pAnchorStructureElementKey(pAnchorKey)
253 ::rtl::OUStringBuffer buf(rTitle);
254 if (!rTitle.empty() && !rDescription.empty())
256 buf.append(" - ");
258 buf.append(rDescription);
259 m_AltText = buf.makeStringAndClear();
262 const uno::Reference< awt::XControl >& ControlPrimitive2D::getXControl() const
264 if(!mxXControl.is())
266 const_cast< ControlPrimitive2D* >(this)->createXControl();
269 return mxXControl;
272 bool ControlPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
274 // use base class compare operator
275 if(!BufferedDecompositionPrimitive2D::operator==(rPrimitive))
276 return false;
278 const ControlPrimitive2D& rCompare = static_cast<const ControlPrimitive2D&>(rPrimitive);
280 if(getTransform() != rCompare.getTransform())
281 return false;
283 // check if ControlModel references both are/are not
284 if (getControlModel().is() != rCompare.getControlModel().is())
285 return false;
287 if(getControlModel().is())
289 // both exist, check for equality
290 if (getControlModel() != rCompare.getControlModel())
291 return false;
294 // check if XControl references both are/are not
295 if (getXControl().is() != rCompare.getXControl().is())
296 return false;
298 if(getXControl().is())
300 // both exist, check for equality
301 if (getXControl() != rCompare.getXControl())
302 return false;
305 return true;
308 basegfx::B2DRange ControlPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
310 // simply derivate from unit range
311 basegfx::B2DRange aRetval(0.0, 0.0, 1.0, 1.0);
312 aRetval.transform(getTransform());
313 return aRetval;
316 void ControlPrimitive2D::get2DDecomposition(Primitive2DDecompositionVisitor& rVisitor, const geometry::ViewInformation2D& rViewInformation) const
318 // this primitive is view-dependent related to the scaling. If scaling has changed,
319 // destroy existing decomposition. To detect change, use size of unit size in view coordinates
320 const basegfx::B2DVector aNewScaling(rViewInformation.getObjectToViewTransformation() * basegfx::B2DVector(1.0, 1.0));
322 if(getBuffered2DDecomposition())
324 if(!maLastViewScaling.equal(aNewScaling))
326 // conditions of last local decomposition have changed, delete
327 const_cast< ControlPrimitive2D* >(this)->setBuffered2DDecomposition(nullptr);
331 if(!getBuffered2DDecomposition())
333 // remember ViewTransformation
334 const_cast< ControlPrimitive2D* >(this)->maLastViewScaling = aNewScaling;
337 // use parent implementation
338 BufferedDecompositionPrimitive2D::get2DDecomposition(rVisitor, rViewInformation);
341 // provide unique ID
342 sal_uInt32 ControlPrimitive2D::getPrimitive2DID() const
344 return PRIMITIVE2D_ID_CONTROLPRIMITIVE2D;
347 } // end of namespace
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */