Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / svdraw / sdrpagewindow.cxx
blob123d1d2d03a60310542c73180e7384dad42026df
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 <svx/sdrpagewindow.hxx>
21 #include <com/sun/star/awt/XWindow.hpp>
22 #include <com/sun/star/awt/PosSize.hpp>
23 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
24 #include <comphelper/lok.hxx>
25 #include <comphelper/processfactory.hxx>
26 #include <toolkit/helper/vclunohelper.hxx>
27 #include <svx/svdview.hxx>
28 #include <svx/svdpagv.hxx>
29 #include <svx/sdrpaintwindow.hxx>
30 #include <svx/sdr/contact/objectcontactofpageview.hxx>
31 #include <svx/sdr/contact/displayinfo.hxx>
32 #include <svx/fmview.hxx>
33 #include <sfx2/lokhelper.hxx>
34 #include <svtools/optionsdrawinglayer.hxx>
35 #include <tools/debug.hxx>
36 #include <vcl/window.hxx>
38 using namespace ::com::sun::star;
40 struct SdrPageWindow::Impl
42 // #110094# ObjectContact section
43 mutable sdr::contact::ObjectContact* mpObjectContact;
45 // the SdrPageView this window belongs to
46 SdrPageView& mrPageView;
48 // the PaintWindow to paint on. Here is access to OutDev etc.
49 // #i72752# change to pointer to allow patcing it in DrawLayer() if necessary
50 SdrPaintWindow* mpPaintWindow;
51 SdrPaintWindow* mpOriginalPaintWindow;
53 // UNO stuff for xControls
54 uno::Reference<awt::XControlContainer> mxControlContainer;
56 Impl( SdrPageView& rPageView, SdrPaintWindow& rPaintWindow ) :
57 mpObjectContact(nullptr),
58 mrPageView(rPageView),
59 mpPaintWindow(&rPaintWindow),
60 mpOriginalPaintWindow(nullptr)
66 uno::Reference<awt::XControlContainer> const & SdrPageWindow::GetControlContainer( bool _bCreateIfNecessary ) const
68 if (!mpImpl->mxControlContainer.is() && _bCreateIfNecessary)
70 SdrView& rView = GetPageView().GetView();
72 const SdrPaintWindow& rPaintWindow( GetOriginalPaintWindow() ? *GetOriginalPaintWindow() : GetPaintWindow() );
73 if ( rPaintWindow.OutputToWindow() && !rView.IsPrintPreview() )
75 vcl::Window* pWindow = rPaintWindow.GetOutputDevice().GetOwnerWindow();
76 const_cast< SdrPageWindow* >( this )->mpImpl->mxControlContainer = VCLUnoHelper::CreateControlContainer( pWindow );
78 // #100394# xC->setVisible triggers window->Show() and this has
79 // problems when the view is not completely constructed which may
80 // happen when loading. This leads to accessibility broadcasts which
81 // throw asserts due to the not finished view. All this chain can be avoided
82 // since xC->setVisible is here called only for the side effect in
83 // UnoControlContainer::setVisible(...) which calls createPeer(...).
84 // This will now be called directly from here.
86 uno::Reference< awt::XControl > xControl(mpImpl->mxControlContainer, uno::UNO_QUERY);
87 if(xControl.is())
89 uno::Reference< uno::XInterface > xContext = xControl->getContext();
90 if(!xContext.is())
92 xControl->createPeer( uno::Reference<awt::XToolkit>(), uno::Reference<awt::XWindowPeer>() );
96 else
98 // Printer and VirtualDevice, or rather: no OutDev
99 uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
100 const_cast< SdrPageWindow* >( this )->mpImpl->mxControlContainer.set(xFactory->createInstance("com.sun.star.awt.UnoControlContainer"), uno::UNO_QUERY);
101 uno::Reference< awt::XControlModel > xModel(xFactory->createInstance("com.sun.star.awt.UnoControlContainerModel"), uno::UNO_QUERY);
102 uno::Reference< awt::XControl > xControl(mpImpl->mxControlContainer, uno::UNO_QUERY);
103 if (xControl.is())
104 xControl->setModel(xModel);
106 OutputDevice& rOutDev = rPaintWindow.GetOutputDevice();
107 Point aPosPix = rOutDev.GetMapMode().GetOrigin();
108 Size aSizePix = rOutDev.GetOutputSizePixel();
110 uno::Reference< awt::XWindow > xContComp(mpImpl->mxControlContainer, uno::UNO_QUERY);
111 if( xContComp.is() )
112 xContComp->setPosSize(aPosPix.X(), aPosPix.Y(), aSizePix.Width(), aSizePix.Height(), awt::PosSize::POSSIZE);
115 FmFormView* pViewAsFormView = dynamic_cast< FmFormView* >( &rView );
116 if ( pViewAsFormView )
117 pViewAsFormView->InsertControlContainer(mpImpl->mxControlContainer);
119 return mpImpl->mxControlContainer;
122 SdrPageWindow::SdrPageWindow(SdrPageView& rPageView, SdrPaintWindow& rPaintWindow) :
123 mpImpl(new Impl(rPageView, rPaintWindow))
127 SdrPageWindow::~SdrPageWindow()
129 // #i26631#
130 ResetObjectContact();
132 if (!mpImpl->mxControlContainer.is())
133 return;
135 auto & rView = static_cast<SdrPaintView &>(GetPageView().GetView());
137 // notify derived views
138 FmFormView* pViewAsFormView = dynamic_cast< FmFormView* >( &rView );
139 if ( pViewAsFormView )
140 pViewAsFormView->RemoveControlContainer(mpImpl->mxControlContainer);
142 // dispose the control container
143 uno::Reference< lang::XComponent > xComponent(mpImpl->mxControlContainer, uno::UNO_QUERY);
144 xComponent->dispose();
147 SdrPageView& SdrPageWindow::GetPageView() const
149 return mpImpl->mrPageView;
152 SdrPaintWindow& SdrPageWindow::GetPaintWindow() const
154 return *mpImpl->mpPaintWindow;
157 const SdrPaintWindow* SdrPageWindow::GetOriginalPaintWindow() const
159 return mpImpl->mpOriginalPaintWindow;
162 // OVERLAY MANAGER
163 rtl::Reference< sdr::overlay::OverlayManager > const & SdrPageWindow::GetOverlayManager() const
165 return GetPaintWindow().GetOverlayManager();
168 SdrPaintWindow* SdrPageWindow::patchPaintWindow(SdrPaintWindow& rPaintWindow)
170 if (!mpImpl)
171 return nullptr;
173 if (!mpImpl->mpOriginalPaintWindow)
175 // first patch
176 mpImpl->mpOriginalPaintWindow = mpImpl->mpPaintWindow;
177 mpImpl->mpPaintWindow = &rPaintWindow;
178 mpImpl->mpOriginalPaintWindow->setPatched(&rPaintWindow);
179 return mpImpl->mpOriginalPaintWindow;
181 else
183 // second or more patch
184 auto pPreviousPaintWindow = mpImpl->mpPaintWindow;
185 mpImpl->mpPaintWindow = &rPaintWindow;
186 mpImpl->mpOriginalPaintWindow->setPatched(&rPaintWindow);
187 return pPreviousPaintWindow;
191 void SdrPageWindow::unpatchPaintWindow(SdrPaintWindow* pPreviousPaintWindow)
193 if (pPreviousPaintWindow == mpImpl->mpOriginalPaintWindow)
195 // first patch
196 mpImpl->mpPaintWindow = mpImpl->mpOriginalPaintWindow;
197 mpImpl->mpOriginalPaintWindow->setPatched(nullptr);
198 mpImpl->mpOriginalPaintWindow = nullptr;
200 else
202 // second or more patch
203 mpImpl->mpPaintWindow = pPreviousPaintWindow;
204 mpImpl->mpOriginalPaintWindow->setPatched(pPreviousPaintWindow);
208 void SdrPageWindow::PrePaint()
210 // give OC the chance to do ProcessDisplay preparations
211 if(HasObjectContact())
213 GetObjectContact().PrepareProcessDisplay();
217 void SdrPageWindow::PrepareRedraw(const vcl::Region& rReg)
219 // give OC the chance to do ProcessDisplay preparations
220 if(HasObjectContact())
222 GetObjectContact().PrepareProcessDisplay();
225 // if necessary, remember changed RedrawArea at PaintWindow for usage with
226 // overlay and PreRenderDevice stuff
227 GetPaintWindow().SetRedrawRegion(rReg);
231 // clip test
232 #ifdef CLIPPER_TEST
233 #include <svx/svdopath.hxx>
234 #include <basegfx/polygon/b2dpolygon.hxx>
235 #include <tools/helpers.hxx>
236 #include <basegfx/polygon/b2dpolygoncutandtouch.hxx>
237 #include <basegfx/polygon/b2dpolypolygontools.hxx>
238 #include <basegfx/polygon/b2dpolygontools.hxx>
239 #include <basegfx/polygon/b2dpolygonclipper.hxx>
241 // for ::std::sort
242 #include <algorithm>
244 namespace
246 void impPaintStrokePolygon(const basegfx::B2DPolygon& rCandidate, OutputDevice& rOutDev, Color aColor)
248 basegfx::B2DPolygon aCandidate(rCandidate);
250 if(aCandidate.areControlPointsUsed())
252 aCandidate = basegfx::utils::adaptiveSubdivideByAngle(rCandidate);
255 if(aCandidate.count())
257 const sal_uInt32 nLoopCount(aCandidate.isClosed() ? aCandidate.count() : aCandidate.count() - 1);
258 rOutDev.SetFillColor();
259 rOutDev.SetLineColor(aColor);
261 for(sal_uInt32 a(0); a < nLoopCount; a++)
263 const basegfx::B2DPoint aBStart(aCandidate.getB2DPoint(a));
264 const basegfx::B2DPoint aBEnd(aCandidate.getB2DPoint((a + 1) % aCandidate.count()));
265 const Point aStart(FRound(aBStart.getX()), FRound(aBStart.getY()));
266 const Point aEnd(FRound(aBEnd.getX()), FRound(aBEnd.getY()));
267 rOutDev.DrawLine(aStart, aEnd);
272 void impTryTest(const SdrPageView& rPageView, OutputDevice& rOutDev)
274 if(rPageView.GetPage() && rPageView.GetPage()->GetObjCount() >= 2)
276 SdrPage* pPage = rPageView.GetPage();
277 SdrObject* pObjA = pPage->GetObj(0);
279 if(dynamic_cast<const SdrPathObj*>( pObjA))
281 basegfx::B2DPolyPolygon aPolyA(pObjA->GetPathPoly());
282 aPolyA = basegfx::utils::correctOrientations(aPolyA);
284 basegfx::B2DPolyPolygon aPolyB;
286 for(sal_uInt32 a(1); a < rPageView.GetPage()->GetObjCount(); a++)
288 SdrObject* pObjB = pPage->GetObj(a);
290 if(dynamic_cast<const SdrPathObj*>( pObjB))
292 basegfx::B2DPolyPolygon aCandidate(pObjB->GetPathPoly());
293 aCandidate = basegfx::utils::correctOrientations(aCandidate);
294 aPolyB.append(aCandidate);
298 if(aPolyA.count() && aPolyA.isClosed() && aPolyB.count())
300 // poly A is the clipregion, clip poly b against it. Algo depends on
301 // poly b being closed.
302 basegfx::B2DPolyPolygon aResult(basegfx::utils::clipPolyPolygonOnPolyPolygon(aPolyB, aPolyA));
304 for(auto const& rPolygon : aResult)
306 int nR = comphelper::rng::uniform_int_distribution(0, 254);
307 int nG = comphelper::rng::uniform_int_distribution(0, 254);
308 int nB = comphelper::rng::uniform_int_distribution(0, 254);
309 Color aColor(nR, nG, nB);
310 impPaintStrokePolygon(rPolygon, rOutDev, aColor);
316 } // end of anonymous namespace
317 #endif // CLIPPER_TEST
320 void SdrPageWindow::RedrawAll( sdr::contact::ViewObjectContactRedirector* pRedirector )
322 // set Redirector
323 GetObjectContact().SetViewObjectContactRedirector(pRedirector);
325 // set PaintingPageView
326 const SdrView& rView = mpImpl->mrPageView.GetView();
327 SdrModel& rModel = rView.GetModel();
329 // get to be processed layers
330 const bool bPrinter(GetPaintWindow().OutputToPrinter());
331 SdrLayerIDSet aProcessLayers = bPrinter ? mpImpl->mrPageView.GetPrintableLayers() : mpImpl->mrPageView.GetVisibleLayers();
333 // create PaintInfoRec; use Rectangle only temporarily
334 const vcl::Region& rRegion = GetPaintWindow().GetRedrawRegion();
336 // create processing data
337 sdr::contact::DisplayInfo aDisplayInfo;
339 // Draw all layers. do NOT draw form layer from CompleteRedraw, this is done separately
340 // as a single layer paint
341 const SdrLayerAdmin& rLayerAdmin = rModel.GetLayerAdmin();
342 const SdrLayerID nControlLayerId = rLayerAdmin.GetLayerID(rLayerAdmin.GetControlLayerName());
343 aProcessLayers.Clear(nControlLayerId);
345 // still something to paint?
346 if(!aProcessLayers.IsEmpty())
348 aDisplayInfo.SetProcessLayers(aProcessLayers);
350 // Set region as redraw area
351 aDisplayInfo.SetRedrawArea(rRegion);
353 // Draw/Impress
354 aDisplayInfo.SetPageProcessingActive(rView.IsPagePaintingAllowed()); // #i72889#
356 // paint page
357 GetObjectContact().ProcessDisplay(aDisplayInfo);
360 // reset redirector
361 GetObjectContact().SetViewObjectContactRedirector(nullptr);
363 // LineClip test
364 #ifdef CLIPPER_TEST
365 if(true)
367 impTryTest(GetPageView(), GetPaintWindow().GetOutputDevice());
369 #endif // CLIPPER_TEST
372 void SdrPageWindow::RedrawLayer(const SdrLayerID* pId,
373 sdr::contact::ViewObjectContactRedirector* pRedirector,
374 basegfx::B2IRectangle const*const pPageFrame)
376 // set redirector
377 GetObjectContact().SetViewObjectContactRedirector(pRedirector);
379 // set PaintingPageView
380 const SdrView& rView = mpImpl->mrPageView.GetView();
381 SdrModel& rModel = rView.GetModel();
383 // get the layers to process
384 const bool bPrinter(GetPaintWindow().OutputToPrinter());
385 SdrLayerIDSet aProcessLayers = bPrinter ? mpImpl->mrPageView.GetPrintableLayers() : mpImpl->mrPageView.GetVisibleLayers();
387 // is the given layer visible at all?
388 if(aProcessLayers.IsSet(*pId))
390 // find out if we are painting the ControlLayer
391 const SdrLayerAdmin& rLayerAdmin = rModel.GetLayerAdmin();
392 const SdrLayerID nControlLayerId = rLayerAdmin.GetLayerID(rLayerAdmin.GetControlLayerName());
393 const bool bControlLayerProcessingActive(nControlLayerId == *pId);
395 // create PaintInfoRec, use Rectangle only temporarily
396 const vcl::Region& rRegion = GetPaintWindow().GetRedrawRegion();
398 // create processing data
399 sdr::contact::DisplayInfo aDisplayInfo;
401 // is it the control layer? If Yes, set flag
402 aDisplayInfo.SetControlLayerProcessingActive(bControlLayerProcessingActive);
404 // Draw just the one given layer
405 aProcessLayers.ClearAll();
406 aProcessLayers.Set(*pId);
408 aDisplayInfo.SetProcessLayers(aProcessLayers);
410 // Set region as redraw area
411 aDisplayInfo.SetRedrawArea(rRegion);
413 // Writer or calc, coming from original RedrawOneLayer.
414 // #i72889# no page painting for layer painting
415 aDisplayInfo.SetPageProcessingActive(false);
417 if (pPageFrame) // Writer page frame for anchor based clipping
419 aDisplayInfo.SetWriterPageFrame(*pPageFrame);
422 // paint page
423 GetObjectContact().ProcessDisplay(aDisplayInfo);
426 // reset redirector
427 GetObjectContact().SetViewObjectContactRedirector(nullptr);
430 // Invalidate call, used from ObjectContact(OfPageView) in InvalidatePartOfView(...)
431 void SdrPageWindow::InvalidatePageWindow(const basegfx::B2DRange& rRange)
433 if (GetPageView().IsVisible() && GetPaintWindow().OutputToWindow())
435 OutputDevice& rWindow(GetPaintWindow().GetOutputDevice());
436 basegfx::B2DRange aDiscreteRange(rRange);
437 aDiscreteRange.transform(rWindow.GetViewTransformation());
439 if (SvtOptionsDrawinglayer::IsAntiAliasing())
441 // invalidate one discrete unit more under the assumption that AA
442 // needs one pixel more
443 aDiscreteRange.grow(1.0);
446 // If the shapes use negative X coordinates, make them positive before sending
447 // the invalidation rectangle.
448 bool bNegativeX = mpImpl->mrPageView.GetView().IsNegativeX();
450 const tools::Rectangle aVCLDiscreteRectangle(
451 static_cast<tools::Long>(bNegativeX ? std::max(0.0, ceil(-aDiscreteRange.getMaxX())) : floor(aDiscreteRange.getMinX())),
452 static_cast<tools::Long>(floor(aDiscreteRange.getMinY())),
453 static_cast<tools::Long>(bNegativeX ? std::max(0.0, floor(-aDiscreteRange.getMinX())) : ceil(aDiscreteRange.getMaxX())),
454 static_cast<tools::Long>(ceil(aDiscreteRange.getMaxY())));
456 const bool bWasMapModeEnabled(rWindow.IsMapModeEnabled());
457 rWindow.EnableMapMode(false);
458 GetPageView().GetView().InvalidateOneWin(rWindow, aVCLDiscreteRectangle);
459 rWindow.EnableMapMode(bWasMapModeEnabled);
461 else if (comphelper::LibreOfficeKit::isActive())
463 // we don't really have to have a paint window with LOK; OTOH we know
464 // that the drawinglayer units are 100ths of mm, so they are easy to
465 // convert to twips
467 // If the shapes use negative X coordinates, make them positive before sending
468 // the invalidation rectangle.
469 bool bNegativeX = mpImpl->mrPageView.GetView().IsNegativeX();
470 const tools::Rectangle aRect100thMM(
471 static_cast<tools::Long>(bNegativeX ? std::max(0.0, ceil(-rRange.getMaxX())) : floor(rRange.getMinX())),
472 static_cast<tools::Long>(floor(rRange.getMinY())),
473 static_cast<tools::Long>(bNegativeX ? std::max(0.0, floor(-rRange.getMinX())) : ceil(rRange.getMaxX())),
474 static_cast<tools::Long>(ceil(rRange.getMaxY())));
476 const tools::Rectangle aRectTwips = o3tl::convert(aRect100thMM, o3tl::Length::mm100, o3tl::Length::twip);
478 if (SfxViewShell* pViewShell = SfxViewShell::Current())
479 SfxLokHelper::notifyInvalidation(pViewShell, &aRectTwips);
483 // ObjectContact section
484 const sdr::contact::ObjectContact& SdrPageWindow::GetObjectContact() const
486 if (!mpImpl->mpObjectContact)
488 mpImpl->mpObjectContact = GetPageView().GetView().createViewSpecificObjectContact(
489 const_cast<SdrPageWindow&>(*this),
490 "svx::svdraw::SdrPageWindow mpObjectContact");
493 return *mpImpl->mpObjectContact;
496 sdr::contact::ObjectContact& SdrPageWindow::GetObjectContact()
498 if (!mpImpl->mpObjectContact)
500 mpImpl->mpObjectContact = GetPageView().GetView().createViewSpecificObjectContact(
501 *this,
502 "svx::svdraw::SdrPageWindow mpObjectContact" );
505 return *mpImpl->mpObjectContact;
508 bool SdrPageWindow::HasObjectContact() const
510 return mpImpl->mpObjectContact != nullptr;
513 // #i26631#
514 void SdrPageWindow::ResetObjectContact()
516 if (mpImpl->mpObjectContact)
518 delete mpImpl->mpObjectContact;
519 mpImpl->mpObjectContact = nullptr;
523 void SdrPageWindow::SetDesignMode( bool _bDesignMode ) const
525 const sdr::contact::ObjectContactOfPageView* pOC = dynamic_cast< const sdr::contact::ObjectContactOfPageView* >( &GetObjectContact() );
526 DBG_ASSERT( pOC, "SdrPageWindow::SetDesignMode: invalid object contact!" );
527 if ( pOC )
528 pOC->SetUNOControlsDesignMode( _bDesignMode );
531 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */