Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / svdraw / sdrpagewindow.cxx
blob6e3cde95fff2d96669cea65b02e4a38b180c1e61
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/beans/XPropertySet.hpp>
23 #include <com/sun/star/awt/PosSize.hpp>
24 #include <com/sun/star/util/XModeChangeBroadcaster.hpp>
25 #include <comphelper/processfactory.hxx>
26 #include <vcl/svapp.hxx>
27 #include <toolkit/helper/vclunohelper.hxx>
28 #include <svx/svdouno.hxx>
29 #include <svx/svdpage.hxx>
30 #include <svx/svdview.hxx>
31 #include <svx/svdpagv.hxx>
32 #include <svx/sdrpaintwindow.hxx>
33 #include <sdr/contact/objectcontactofpageview.hxx>
34 #include <svx/sdr/contact/displayinfo.hxx>
35 #include <osl/mutex.hxx>
36 #include <svx/fmview.hxx>
37 #include <basegfx/matrix/b2dhommatrix.hxx>
41 using namespace ::rtl;
42 using namespace ::com::sun::star;
46 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlContainer > SdrPageWindow::GetControlContainer( bool _bCreateIfNecessary ) const
48 if ( !mxControlContainer.is() && _bCreateIfNecessary )
50 SdrView& rView = GetPageView().GetView();
52 const SdrPaintWindow& rPaintWindow( GetOriginalPaintWindow() ? *GetOriginalPaintWindow() : GetPaintWindow() );
53 if ( rPaintWindow.OutputToWindow() && !rView.IsPrintPreview() )
55 Window& rWindow = dynamic_cast< Window& >( rPaintWindow.GetOutputDevice() );
56 const_cast< SdrPageWindow* >( this )->mxControlContainer = VCLUnoHelper::CreateControlContainer( &rWindow );
58 // #100394# xC->setVisible triggers window->Show() and this has
59 // problems when the view is not completely constructed which may
60 // happen when loading. This leads to accessibility broadcasts which
61 // throw asserts due to the not finished view. All this chain can be avoided
62 // since xC->setVisible is here called only for the side effect in
63 // UnoControlContainer::setVisible(...) which calls createPeer(...).
64 // This will now be called directly from here.
66 uno::Reference< awt::XControl > xControl(mxControlContainer, uno::UNO_QUERY);
67 if(xControl.is())
69 uno::Reference< uno::XInterface > xContext = xControl->getContext();
70 if(!xContext.is())
72 xControl->createPeer( ::com::sun::star::uno::Reference< ::com::sun::star::awt::XToolkit > (),
73 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer > () );
77 else
79 // Printer and VirtualDevice, or rather: no OutDev
80 uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
81 const_cast< SdrPageWindow* >( this )->mxControlContainer = uno::Reference< awt::XControlContainer >(xFactory->createInstance("com.sun.star.awt.UnoControlContainer"), uno::UNO_QUERY);
82 uno::Reference< awt::XControlModel > xModel(xFactory->createInstance("com.sun.star.awt.UnoControlContainerModel"), uno::UNO_QUERY);
83 uno::Reference< awt::XControl > xControl(mxControlContainer, uno::UNO_QUERY);
84 if (xControl.is())
85 xControl->setModel(xModel);
87 OutputDevice& rOutDev = rPaintWindow.GetOutputDevice();
88 Point aPosPix = rOutDev.GetMapMode().GetOrigin();
89 Size aSizePix = rOutDev.GetOutputSizePixel();
91 uno::Reference< awt::XWindow > xContComp(mxControlContainer, uno::UNO_QUERY);
92 if( xContComp.is() )
93 xContComp->setPosSize(aPosPix.X(), aPosPix.Y(), aSizePix.Width(), aSizePix.Height(), awt::PosSize::POSSIZE);
96 FmFormView* pViewAsFormView = dynamic_cast< FmFormView* >( &rView );
97 if ( pViewAsFormView )
98 pViewAsFormView->InsertControlContainer(mxControlContainer);
100 return mxControlContainer;
103 SdrPageWindow::SdrPageWindow(SdrPageView& rPageView, SdrPaintWindow& rPaintWindow)
104 : mpObjectContact(0L),
105 mrPageView(rPageView),
106 mpPaintWindow(&rPaintWindow),
107 mpOriginalPaintWindow(NULL)
111 SdrPageWindow::~SdrPageWindow()
113 // #i26631#
114 ResetObjectContact();
116 if (mxControlContainer.is())
118 SdrView& rView = GetPageView().GetView();
120 // notify derived views
121 FmFormView* pViewAsFormView = dynamic_cast< FmFormView* >( &rView );
122 if ( pViewAsFormView )
123 pViewAsFormView->RemoveControlContainer(mxControlContainer);
125 // dispose the control container
126 uno::Reference< lang::XComponent > xComponent(mxControlContainer, uno::UNO_QUERY);
127 xComponent->dispose();
131 // ObjectContact section
132 sdr::contact::ObjectContact* SdrPageWindow::CreateViewSpecificObjectContact()
134 return new sdr::contact::ObjectContactOfPageView(*this);
137 // OVERLAY MANAGER
138 rtl::Reference< ::sdr::overlay::OverlayManager > SdrPageWindow::GetOverlayManager() const
140 return GetPaintWindow().GetOverlayManager();
143 void SdrPageWindow::patchPaintWindow(SdrPaintWindow& rPaintWindow)
145 mpOriginalPaintWindow = mpPaintWindow;
146 mpPaintWindow = &rPaintWindow;
149 void SdrPageWindow::unpatchPaintWindow()
151 DBG_ASSERT(mpOriginalPaintWindow, "SdrPageWindow::unpatchPaintWindow: paint window not patched!" );
152 if ( mpOriginalPaintWindow )
154 mpPaintWindow = mpOriginalPaintWindow;
155 mpOriginalPaintWindow = NULL;
159 void SdrPageWindow::PrePaint()
161 // give OC the chance to do ProcessDisplay preparations
162 if(HasObjectContact())
164 GetObjectContact().PrepareProcessDisplay();
168 void SdrPageWindow::PrepareRedraw(const Region& rReg)
170 // give OC the chance to do ProcessDisplay preparations
171 if(HasObjectContact())
173 GetObjectContact().PrepareProcessDisplay();
176 // if necessary, remember changed RedrawArea at PaintWindow for usage with
177 // overlay and PreRenderDevice stuff
178 GetPaintWindow().SetRedrawRegion(rReg);
182 // clip test
183 #ifdef CLIPPER_TEST
184 #include <svx/svdopath.hxx>
185 #include <basegfx/polygon/b2dpolygon.hxx>
186 #include <tools/helpers.hxx>
187 #include <basegfx/polygon/b2dpolygoncutandtouch.hxx>
188 #include <basegfx/polygon/b2dpolypolygontools.hxx>
189 #include <basegfx/polygon/b2dpolygontools.hxx>
190 #include <basegfx/polygon/b2dpolygonclipper.hxx>
192 // for ::std::sort
193 #include <algorithm>
195 namespace
197 void impPaintStrokePolygon(const basegfx::B2DPolygon& rCandidate, OutputDevice& rOutDev, Color aColor)
199 basegfx::B2DPolygon aCandidate(rCandidate);
201 if(aCandidate.areControlPointsUsed())
203 aCandidate = basegfx::tools::adaptiveSubdivideByAngle(rCandidate);
206 if(aCandidate.count())
208 const sal_uInt32 nLoopCount(aCandidate.isClosed() ? aCandidate.count() : aCandidate.count() - 1L);
209 rOutDev.SetFillColor();
210 rOutDev.SetLineColor(aColor);
212 for(sal_uInt32 a(0L); a < nLoopCount; a++)
214 const basegfx::B2DPoint aBStart(aCandidate.getB2DPoint(a));
215 const basegfx::B2DPoint aBEnd(aCandidate.getB2DPoint((a + 1) % aCandidate.count()));
216 const Point aStart(FRound(aBStart.getX()), FRound(aBStart.getY()));
217 const Point aEnd(FRound(aBEnd.getX()), FRound(aBEnd.getY()));
218 rOutDev.DrawLine(aStart, aEnd);
223 void impTryTest(const SdrPageView& rPageView, OutputDevice& rOutDev)
225 if(rPageView.GetPage() && rPageView.GetPage()->GetObjCount() >= 2L)
227 SdrPage* pPage = rPageView.GetPage();
228 SdrObject* pObjA = pPage->GetObj(0L);
230 if(pObjA && pObjA->ISA(SdrPathObj))
232 basegfx::B2DPolyPolygon aPolyA(((SdrPathObj*)pObjA)->GetPathPoly());
233 aPolyA = basegfx::tools::correctOrientations(aPolyA);
235 basegfx::B2DPolyPolygon aPolyB;
237 for(sal_uInt32 a(1L); a < rPageView.GetPage()->GetObjCount(); a++)
239 SdrObject* pObjB = pPage->GetObj(a);
241 if(pObjB && pObjB->ISA(SdrPathObj))
243 basegfx::B2DPolyPolygon aCandidate(((SdrPathObj*)pObjB)->GetPathPoly());
244 aCandidate = basegfx::tools::correctOrientations(aCandidate);
245 aPolyB.append(aCandidate);
249 if(aPolyA.count() && aPolyA.isClosed() && aPolyB.count())
251 // poly A is the clipregion, clip poly b against it. Algo depends on
252 // poly b being closed.
253 basegfx::B2DPolyPolygon aResult(basegfx::tools::clipPolyPolygonOnPolyPolygon(aPolyB, aPolyA));
255 for(sal_uInt32 a(0L); a < aResult.count(); a++)
257 Color aColor(rand()%255, rand()%255, rand()%255);
258 impPaintStrokePolygon(aResult.getB2DPolygon(a), rOutDev, aColor);
264 } // end of anonymous namespace
265 #endif // CLIPPER_TEST
269 void SdrPageWindow::RedrawAll(sdr::contact::ViewObjectContactRedirector* pRedirector) const
271 // set Redirector
272 GetObjectContact().SetViewObjectContactRedirector(pRedirector);
274 // set PaintingPageView
275 const SdrView& rView = mrPageView.GetView();
276 SdrModel& rModel = *((SdrModel*)rView.GetModel());
278 // get to be processed layers
279 const bool bPrinter(GetPaintWindow().OutputToPrinter());
280 SetOfByte aProcessLayers = bPrinter ? mrPageView.GetPrintableLayers() : mrPageView.GetVisibleLayers();
282 // create PaintInfoRec; use Rectangle only temporarily
283 const Region& rRegion = GetPaintWindow().GetRedrawRegion();
285 // create processing data
286 sdr::contact::DisplayInfo aDisplayInfo;
288 // Draw all layers. do NOT draw form layer from CompleteRedraw, this is done separately
289 // as a single layer paint
290 const SdrLayerAdmin& rLayerAdmin = rModel.GetLayerAdmin();
291 const SdrLayerID nControlLayerId = rLayerAdmin.GetLayerID(rLayerAdmin.GetControlLayerName(), false);
292 aProcessLayers.Clear(nControlLayerId);
294 // still something to paint?
295 if(!aProcessLayers.IsEmpty())
297 aDisplayInfo.SetProcessLayers(aProcessLayers);
299 // Set region as redraw area
300 aDisplayInfo.SetRedrawArea(rRegion);
302 // Draw/Impress
303 aDisplayInfo.SetPageProcessingActive(rView.IsPagePaintingAllowed()); // #i72889#
305 // paint page
306 GetObjectContact().ProcessDisplay(aDisplayInfo);
309 // reset redirector
310 GetObjectContact().SetViewObjectContactRedirector(0L);
312 // LineClip test
313 #ifdef CLIPPER_TEST
314 if(true)
316 impTryTest(GetPageView(), GetPaintWindow().GetOutputDevice());
318 #endif // CLIPPER_TEST
321 void SdrPageWindow::RedrawLayer(const SdrLayerID* pId, sdr::contact::ViewObjectContactRedirector* pRedirector) const
323 // set redirector
324 GetObjectContact().SetViewObjectContactRedirector(pRedirector);
326 // set PaintingPageView
327 const SdrView& rView = mrPageView.GetView();
328 SdrModel& rModel = *((SdrModel*)rView.GetModel());
330 // get the layers to process
331 const bool bPrinter(GetPaintWindow().OutputToPrinter());
332 SetOfByte aProcessLayers = bPrinter ? mrPageView.GetPrintableLayers() : mrPageView.GetVisibleLayers();
334 // is the given layer visible at all?
335 if(aProcessLayers.IsSet(*pId))
337 // find out if we are painting the ControlLayer
338 const SdrLayerAdmin& rLayerAdmin = rModel.GetLayerAdmin();
339 const SdrLayerID nControlLayerId = rLayerAdmin.GetLayerID(rLayerAdmin.GetControlLayerName(), false);
340 const bool bControlLayerProcessingActive(nControlLayerId == *pId);
342 // create PaintInfoRec, use Rectangle only temporarily
343 const Region& rRegion = GetPaintWindow().GetRedrawRegion();
345 // create processing data
346 sdr::contact::DisplayInfo aDisplayInfo;
348 // is it the control layer? If Yes, set flag
349 aDisplayInfo.SetControlLayerProcessingActive(bControlLayerProcessingActive);
351 // Draw just the one given layer
352 aProcessLayers.ClearAll();
353 aProcessLayers.Set(*pId);
355 aDisplayInfo.SetProcessLayers(aProcessLayers);
357 // Set region as redraw area
358 aDisplayInfo.SetRedrawArea(rRegion);
360 // Writer or calc, coming from original RedrawOneLayer.
361 // #i72889# no page painting for layer painting
362 aDisplayInfo.SetPageProcessingActive(false);
364 // paint page
365 GetObjectContact().ProcessDisplay(aDisplayInfo);
368 // reset redirector
369 GetObjectContact().SetViewObjectContactRedirector(0L);
372 // Invalidate call, used from ObjectContact(OfPageView) in InvalidatePartOfView(...)
373 void SdrPageWindow::InvalidatePageWindow(const basegfx::B2DRange& rRange)
375 if(GetPageView().IsVisible() && GetPaintWindow().OutputToWindow())
377 const SvtOptionsDrawinglayer aDrawinglayerOpt;
378 Window& rWindow(static_cast< Window& >(GetPaintWindow().GetOutputDevice()));
379 basegfx::B2DRange aDiscreteRange(rRange);
380 aDiscreteRange.transform(rWindow.GetViewTransformation());
382 if(aDrawinglayerOpt.IsAntiAliasing())
384 // invalidate one discrete unit more under the assumption that AA
385 // needs one pixel more
386 aDiscreteRange.grow(1.0);
389 const Rectangle aVCLDiscreteRectangle(
390 (sal_Int32)floor(aDiscreteRange.getMinX()), (sal_Int32)floor(aDiscreteRange.getMinY()),
391 (sal_Int32)ceil(aDiscreteRange.getMaxX()), (sal_Int32)ceil(aDiscreteRange.getMaxY()));
392 const bool bWasMapModeEnabled(rWindow.IsMapModeEnabled());
394 rWindow.EnableMapMode(false);
395 rWindow.Invalidate(aVCLDiscreteRectangle, INVALIDATE_NOERASE);
396 rWindow.EnableMapMode(bWasMapModeEnabled);
400 // ObjectContact section
401 sdr::contact::ObjectContact& SdrPageWindow::GetObjectContact() const
403 if(!mpObjectContact)
405 ((SdrPageWindow*)this)->mpObjectContact = ((SdrPageWindow*)this)->CreateViewSpecificObjectContact();
408 return *mpObjectContact;
411 bool SdrPageWindow::HasObjectContact() const
413 return ( mpObjectContact != NULL );
416 // #i26631#
417 void SdrPageWindow::ResetObjectContact()
419 if(mpObjectContact)
421 delete mpObjectContact;
422 mpObjectContact = 0L;
426 void SdrPageWindow::SetDesignMode( bool _bDesignMode ) const
428 const ::sdr::contact::ObjectContactOfPageView* pOC = dynamic_cast< const ::sdr::contact::ObjectContactOfPageView* >( &GetObjectContact() );
429 DBG_ASSERT( pOC, "SdrPageWindow::SetDesignMode: invalid object contact!" );
430 if ( pOC )
431 pOC->SetUNOControlsDesignMode( _bDesignMode );
434 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */