1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_slideshow.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir // must be first
32*cdf0e10cSrcweir #include <canvas/debug.hxx>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <basegfx/range/b2drange.hxx>
35*cdf0e10cSrcweir #include <basegfx/range/b1drange.hxx>
36*cdf0e10cSrcweir #include <basegfx/range/b2dpolyrange.hxx>
37*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
38*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygon.hxx>
39*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygontools.hxx>
40*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include "layer.hxx"
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include <boost/bind.hpp>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir using namespace ::com::sun::star;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir namespace slideshow
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir     namespace internal
52*cdf0e10cSrcweir     {
53*cdf0e10cSrcweir         Layer::Layer( const basegfx::B2DRange& rMaxLayerBounds,
54*cdf0e10cSrcweir                       Dummy                     ) :
55*cdf0e10cSrcweir             maViewEntries(),
56*cdf0e10cSrcweir             maBounds(),
57*cdf0e10cSrcweir             maNewBounds(),
58*cdf0e10cSrcweir             maMaxBounds( rMaxLayerBounds ),
59*cdf0e10cSrcweir             mbBoundsDirty(false),
60*cdf0e10cSrcweir             mbBackgroundLayer(true),
61*cdf0e10cSrcweir             mbClipSet(false)
62*cdf0e10cSrcweir         {
63*cdf0e10cSrcweir         }
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir         Layer::Layer( const basegfx::B2DRange& rMaxLayerBounds ) :
66*cdf0e10cSrcweir             maViewEntries(),
67*cdf0e10cSrcweir             maBounds(),
68*cdf0e10cSrcweir             maNewBounds(),
69*cdf0e10cSrcweir             maMaxBounds( rMaxLayerBounds ),
70*cdf0e10cSrcweir             mbBoundsDirty(false),
71*cdf0e10cSrcweir             mbBackgroundLayer(false),
72*cdf0e10cSrcweir             mbClipSet(false)
73*cdf0e10cSrcweir         {
74*cdf0e10cSrcweir         }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir         ViewLayerSharedPtr Layer::addView( const ViewSharedPtr& rNewView )
77*cdf0e10cSrcweir         {
78*cdf0e10cSrcweir             OSL_ASSERT( rNewView );
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir             ViewEntryVector::iterator aIter;
81*cdf0e10cSrcweir             const ViewEntryVector::iterator aEnd( maViewEntries.end() );
82*cdf0e10cSrcweir             if( (aIter=std::find_if( maViewEntries.begin(),
83*cdf0e10cSrcweir                                      aEnd,
84*cdf0e10cSrcweir                                      boost::bind<bool>(
85*cdf0e10cSrcweir                                          std::equal_to< ViewSharedPtr >(),
86*cdf0e10cSrcweir                                          boost::bind( &ViewEntry::getView, _1 ),
87*cdf0e10cSrcweir                                          boost::cref( rNewView )))) != aEnd )
88*cdf0e10cSrcweir             {
89*cdf0e10cSrcweir                 // already added - just return existing layer
90*cdf0e10cSrcweir                 return aIter->mpViewLayer;
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir             }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir             // not yet added - create new view layer
95*cdf0e10cSrcweir             ViewLayerSharedPtr pNewLayer;
96*cdf0e10cSrcweir             if( mbBackgroundLayer )
97*cdf0e10cSrcweir                 pNewLayer = rNewView;
98*cdf0e10cSrcweir             else
99*cdf0e10cSrcweir                 pNewLayer = rNewView->createViewLayer(maBounds);
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir             // add to local list
102*cdf0e10cSrcweir             maViewEntries.push_back(
103*cdf0e10cSrcweir                 ViewEntry( rNewView,
104*cdf0e10cSrcweir                            pNewLayer ));
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir             return maViewEntries.back().mpViewLayer;
107*cdf0e10cSrcweir         }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir         ViewLayerSharedPtr Layer::removeView( const ViewSharedPtr& rView )
110*cdf0e10cSrcweir         {
111*cdf0e10cSrcweir             OSL_ASSERT( rView );
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir             ViewEntryVector::iterator       aIter;
114*cdf0e10cSrcweir             const ViewEntryVector::iterator aEnd( maViewEntries.end() );
115*cdf0e10cSrcweir             if( (aIter=std::find_if( maViewEntries.begin(),
116*cdf0e10cSrcweir                                        aEnd,
117*cdf0e10cSrcweir                                        boost::bind<bool>(
118*cdf0e10cSrcweir                                            std::equal_to< ViewSharedPtr >(),
119*cdf0e10cSrcweir                                            boost::bind( &ViewEntry::getView, _1 ),
120*cdf0e10cSrcweir                                            boost::cref( rView )))) == aEnd )
121*cdf0e10cSrcweir             {
122*cdf0e10cSrcweir                 // View was not added/is already removed
123*cdf0e10cSrcweir                 return ViewLayerSharedPtr();
124*cdf0e10cSrcweir             }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir             OSL_ENSURE( std::count_if( maViewEntries.begin(),
127*cdf0e10cSrcweir                                        aEnd,
128*cdf0e10cSrcweir                                        boost::bind<bool>(
129*cdf0e10cSrcweir                                            std::equal_to< ViewSharedPtr >(),
130*cdf0e10cSrcweir                                            boost::bind( &ViewEntry::getView, _1 ),
131*cdf0e10cSrcweir                                            boost::cref( rView ))) == 1,
132*cdf0e10cSrcweir                         "Layer::removeView(): view added multiple times" );
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir             ViewLayerSharedPtr pRet( aIter->mpViewLayer );
135*cdf0e10cSrcweir             maViewEntries.erase(aIter);
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir             return pRet;
138*cdf0e10cSrcweir         }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir         void Layer::viewChanged( const ViewSharedPtr& rChangedView )
141*cdf0e10cSrcweir         {
142*cdf0e10cSrcweir             ViewEntryVector::iterator aIter;
143*cdf0e10cSrcweir             const ViewEntryVector::iterator aEnd( maViewEntries.end() );
144*cdf0e10cSrcweir             if( (aIter=std::find_if( maViewEntries.begin(),
145*cdf0e10cSrcweir                                      aEnd,
146*cdf0e10cSrcweir                                      boost::bind<bool>(
147*cdf0e10cSrcweir                                          std::equal_to< ViewSharedPtr >(),
148*cdf0e10cSrcweir                                          boost::bind( &ViewEntry::getView, _1 ),
149*cdf0e10cSrcweir                                          boost::cref( rChangedView )))) !=
150*cdf0e10cSrcweir                 aEnd )
151*cdf0e10cSrcweir             {
152*cdf0e10cSrcweir                 // adapt size of given ViewLayer - background layer
153*cdf0e10cSrcweir                 // resizes with view.
154*cdf0e10cSrcweir                 if( !mbBackgroundLayer )
155*cdf0e10cSrcweir                     aIter->mpViewLayer->resize(maBounds);
156*cdf0e10cSrcweir             }
157*cdf0e10cSrcweir         }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir         void Layer::viewsChanged()
160*cdf0e10cSrcweir         {
161*cdf0e10cSrcweir             // adapt size of given ViewLayer - background layer
162*cdf0e10cSrcweir             // resizes with view.
163*cdf0e10cSrcweir             if( !mbBackgroundLayer )
164*cdf0e10cSrcweir             {
165*cdf0e10cSrcweir                 std::for_each( maViewEntries.begin(),
166*cdf0e10cSrcweir                                maViewEntries.end(),
167*cdf0e10cSrcweir                                boost::bind( &ViewLayer::resize,
168*cdf0e10cSrcweir                                             boost::bind( &ViewEntry::getViewLayer,
169*cdf0e10cSrcweir                                                          _1 ),
170*cdf0e10cSrcweir                                             boost::cref(maBounds)));
171*cdf0e10cSrcweir             }
172*cdf0e10cSrcweir         }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         void Layer::setShapeViews( ShapeSharedPtr const& rShape ) const
175*cdf0e10cSrcweir         {
176*cdf0e10cSrcweir             rShape->clearAllViewLayers();
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir             std::for_each( maViewEntries.begin(),
179*cdf0e10cSrcweir                            maViewEntries.end(),
180*cdf0e10cSrcweir                            boost::bind(&Shape::addViewLayer,
181*cdf0e10cSrcweir                                        boost::cref(rShape),
182*cdf0e10cSrcweir                                        boost::bind(&ViewEntry::getViewLayer,
183*cdf0e10cSrcweir                                                    _1),
184*cdf0e10cSrcweir                                        false ));
185*cdf0e10cSrcweir         }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir         void Layer::setPriority( const ::basegfx::B1DRange& rPrioRange )
188*cdf0e10cSrcweir         {
189*cdf0e10cSrcweir             if( !mbBackgroundLayer )
190*cdf0e10cSrcweir             {
191*cdf0e10cSrcweir                 std::for_each( maViewEntries.begin(),
192*cdf0e10cSrcweir                                maViewEntries.end(),
193*cdf0e10cSrcweir                                boost::bind( &ViewLayer::setPriority,
194*cdf0e10cSrcweir                                             boost::bind( &ViewEntry::getViewLayer,
195*cdf0e10cSrcweir                                                          _1 ),
196*cdf0e10cSrcweir                                             boost::cref(rPrioRange)));
197*cdf0e10cSrcweir             }
198*cdf0e10cSrcweir         }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir         void Layer::addUpdateRange( ::basegfx::B2DRange const& rUpdateRange )
201*cdf0e10cSrcweir         {
202*cdf0e10cSrcweir             // TODO(Q1): move this to B2DMultiRange
203*cdf0e10cSrcweir             if( !rUpdateRange.isEmpty() )
204*cdf0e10cSrcweir                 maUpdateAreas.appendElement( rUpdateRange,
205*cdf0e10cSrcweir                                              basegfx::ORIENTATION_POSITIVE );
206*cdf0e10cSrcweir         }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir         void Layer::updateBounds( ShapeSharedPtr const& rShape )
209*cdf0e10cSrcweir         {
210*cdf0e10cSrcweir             if( !mbBackgroundLayer )
211*cdf0e10cSrcweir             {
212*cdf0e10cSrcweir                 if( !mbBoundsDirty )
213*cdf0e10cSrcweir                     maNewBounds.reset();
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir                 maNewBounds.expand( rShape->getUpdateArea() );
216*cdf0e10cSrcweir             }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir             mbBoundsDirty = true;
219*cdf0e10cSrcweir         }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir         bool Layer::commitBounds()
222*cdf0e10cSrcweir         {
223*cdf0e10cSrcweir             mbBoundsDirty = false;
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir             if( mbBackgroundLayer )
226*cdf0e10cSrcweir                 return false;
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir             if( maNewBounds == maBounds )
229*cdf0e10cSrcweir                 return false;
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir             maBounds = maNewBounds;
232*cdf0e10cSrcweir             if( std::count_if( maViewEntries.begin(),
233*cdf0e10cSrcweir                                maViewEntries.end(),
234*cdf0e10cSrcweir                                boost::bind( &ViewLayer::resize,
235*cdf0e10cSrcweir                                             boost::bind( &ViewEntry::getViewLayer,
236*cdf0e10cSrcweir                                                          _1 ),
237*cdf0e10cSrcweir                                             boost::cref(maBounds)) ) == 0 )
238*cdf0e10cSrcweir             {
239*cdf0e10cSrcweir                 return false;
240*cdf0e10cSrcweir             }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir             // layer content invalid, update areas have wrong
243*cdf0e10cSrcweir             // coordinates/not sensible anymore.
244*cdf0e10cSrcweir             clearUpdateRanges();
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir             return true;
247*cdf0e10cSrcweir         }
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir         void Layer::clearUpdateRanges()
250*cdf0e10cSrcweir         {
251*cdf0e10cSrcweir             maUpdateAreas.clear();
252*cdf0e10cSrcweir         }
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir         void Layer::clearContent()
255*cdf0e10cSrcweir         {
256*cdf0e10cSrcweir             // clear content on all view layers
257*cdf0e10cSrcweir             std::for_each( maViewEntries.begin(),
258*cdf0e10cSrcweir                            maViewEntries.end(),
259*cdf0e10cSrcweir                            boost::bind(
260*cdf0e10cSrcweir                                &ViewLayer::clear,
261*cdf0e10cSrcweir                                boost::bind(
262*cdf0e10cSrcweir                                    &ViewEntry::getViewLayer,
263*cdf0e10cSrcweir                                    _1)));
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir             // layer content cleared, update areas are not sensible
266*cdf0e10cSrcweir             // anymore.
267*cdf0e10cSrcweir             clearUpdateRanges();
268*cdf0e10cSrcweir         }
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir         class LayerEndUpdate : private boost::noncopyable
271*cdf0e10cSrcweir         {
272*cdf0e10cSrcweir         public:
273*cdf0e10cSrcweir             LayerEndUpdate( LayerSharedPtr const& rLayer ) :
274*cdf0e10cSrcweir                 mpLayer( rLayer )
275*cdf0e10cSrcweir             {}
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir             ~LayerEndUpdate() { if(mpLayer) mpLayer->endUpdate(); }
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir             void dismiss() { mpLayer.reset(); }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir         private:
282*cdf0e10cSrcweir             LayerSharedPtr mpLayer;
283*cdf0e10cSrcweir         };
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir         Layer::EndUpdater Layer::beginUpdate()
286*cdf0e10cSrcweir         {
287*cdf0e10cSrcweir             if( maUpdateAreas.count() )
288*cdf0e10cSrcweir             {
289*cdf0e10cSrcweir                 // perform proper layer update. That means, setup proper
290*cdf0e10cSrcweir                 // clipping, and render each shape that intersects with
291*cdf0e10cSrcweir                 // the calculated update area
292*cdf0e10cSrcweir                 ::basegfx::B2DPolyPolygon aClip( maUpdateAreas.solveCrossovers() );
293*cdf0e10cSrcweir                 aClip = ::basegfx::tools::stripNeutralPolygons(aClip);
294*cdf0e10cSrcweir                 aClip = ::basegfx::tools::stripDispensablePolygons(aClip, false);
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir                 // actually, if there happen to be shapes with zero
297*cdf0e10cSrcweir                 // update area in the maUpdateAreas vector, the
298*cdf0e10cSrcweir                 // resulting clip polygon will be empty.
299*cdf0e10cSrcweir                 if( aClip.count() )
300*cdf0e10cSrcweir                 {
301*cdf0e10cSrcweir                     // set clip to all view layers
302*cdf0e10cSrcweir                     std::for_each( maViewEntries.begin(),
303*cdf0e10cSrcweir                                    maViewEntries.end(),
304*cdf0e10cSrcweir                                    boost::bind(
305*cdf0e10cSrcweir                                        &ViewLayer::setClip,
306*cdf0e10cSrcweir                                        boost::bind(
307*cdf0e10cSrcweir                                            &ViewEntry::getViewLayer,
308*cdf0e10cSrcweir                                            _1),
309*cdf0e10cSrcweir                                        boost::cref(aClip)));
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir                     // clear update area on all view layers
312*cdf0e10cSrcweir                     std::for_each( maViewEntries.begin(),
313*cdf0e10cSrcweir                                    maViewEntries.end(),
314*cdf0e10cSrcweir                                    boost::bind(
315*cdf0e10cSrcweir                                        &ViewLayer::clear,
316*cdf0e10cSrcweir                                        boost::bind(
317*cdf0e10cSrcweir                                            &ViewEntry::getViewLayer,
318*cdf0e10cSrcweir                                            _1)));
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir                     mbClipSet = true;
321*cdf0e10cSrcweir                 }
322*cdf0e10cSrcweir             }
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir             return EndUpdater(new LayerEndUpdate(shared_from_this()));
325*cdf0e10cSrcweir         }
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir         void Layer::endUpdate()
328*cdf0e10cSrcweir         {
329*cdf0e10cSrcweir             if( mbClipSet )
330*cdf0e10cSrcweir             {
331*cdf0e10cSrcweir                 mbClipSet = false;
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir                 basegfx::B2DPolyPolygon aEmptyClip;
334*cdf0e10cSrcweir                 std::for_each( maViewEntries.begin(),
335*cdf0e10cSrcweir                                maViewEntries.end(),
336*cdf0e10cSrcweir                                boost::bind(
337*cdf0e10cSrcweir                                    &ViewLayer::setClip,
338*cdf0e10cSrcweir                                    boost::bind(
339*cdf0e10cSrcweir                                        &ViewEntry::getViewLayer,
340*cdf0e10cSrcweir                                        _1),
341*cdf0e10cSrcweir                                    boost::cref(aEmptyClip)));
342*cdf0e10cSrcweir             }
343*cdf0e10cSrcweir 
344*cdf0e10cSrcweir             clearUpdateRanges();
345*cdf0e10cSrcweir         }
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir         bool Layer::isInsideUpdateArea( ShapeSharedPtr const& rShape ) const
348*cdf0e10cSrcweir         {
349*cdf0e10cSrcweir             return maUpdateAreas.overlaps( rShape->getUpdateArea() );
350*cdf0e10cSrcweir         }
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir         LayerSharedPtr Layer::createBackgroundLayer( const basegfx::B2DRange& rMaxLayerBounds )
353*cdf0e10cSrcweir         {
354*cdf0e10cSrcweir             return LayerSharedPtr(new Layer( rMaxLayerBounds,
355*cdf0e10cSrcweir                                              BackgroundLayer ));
356*cdf0e10cSrcweir         }
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir         LayerSharedPtr Layer::createLayer( const basegfx::B2DRange& rMaxLayerBounds )
359*cdf0e10cSrcweir         {
360*cdf0e10cSrcweir             return LayerSharedPtr( new Layer( rMaxLayerBounds ) );
361*cdf0e10cSrcweir         }
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir     }
364*cdf0e10cSrcweir }
365