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 #include <canvas/verbosetrace.hxx>
34*cdf0e10cSrcweir #include <canvas/canvastools.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <com/sun/star/drawing/XShape.hpp>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include "mediashape.hxx"
39*cdf0e10cSrcweir #include "viewmediashape.hxx"
40*cdf0e10cSrcweir #include "externalshapebase.hxx"
41*cdf0e10cSrcweir #include "slideshowcontext.hxx"
42*cdf0e10cSrcweir #include "shape.hxx"
43*cdf0e10cSrcweir #include "tools.hxx"
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include <boost/bind.hpp>
46*cdf0e10cSrcweir #include <algorithm>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir using namespace ::com::sun::star;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir namespace slideshow
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir     namespace internal
55*cdf0e10cSrcweir     {
56*cdf0e10cSrcweir         /** Represents a media shape.
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir             This implementation offers support for media shapes.
59*cdf0e10cSrcweir 			Such shapes need special treatment.
60*cdf0e10cSrcweir          */
61*cdf0e10cSrcweir         class MediaShape : public ExternalShapeBase
62*cdf0e10cSrcweir         {
63*cdf0e10cSrcweir         public:
64*cdf0e10cSrcweir             /** Create a shape for the given XShape for a media object
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir             	@param xShape
67*cdf0e10cSrcweir                 The XShape to represent.
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir                 @param nPrio
70*cdf0e10cSrcweir                 Externally-determined shape priority (used e.g. for
71*cdf0e10cSrcweir                 paint ordering). This number _must be_ unique!
72*cdf0e10cSrcweir              */
73*cdf0e10cSrcweir             MediaShape( const ::com::sun::star::uno::Reference<
74*cdf0e10cSrcweir                        		::com::sun::star::drawing::XShape >&	xShape,
75*cdf0e10cSrcweir                         double										nPrio,
76*cdf0e10cSrcweir                         const SlideShowContext&                     rContext ); // throw ShapeLoadFailedException;
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir         private:
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir             // View layer methods
81*cdf0e10cSrcweir             //------------------------------------------------------------------
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir             virtual void addViewLayer( const ViewLayerSharedPtr& 	rNewLayer,
84*cdf0e10cSrcweir                                        bool							bRedrawLayer );
85*cdf0e10cSrcweir             virtual bool removeViewLayer( const ViewLayerSharedPtr& rNewLayer );
86*cdf0e10cSrcweir             virtual bool clearAllViewLayers();
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir             // ExternalShapeBase methods
90*cdf0e10cSrcweir             //------------------------------------------------------------------
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir             virtual bool implRender( const ::basegfx::B2DRange& rCurrBounds ) const;
93*cdf0e10cSrcweir             virtual void implViewChanged( const UnoViewSharedPtr& rView );
94*cdf0e10cSrcweir             virtual void implViewsChanged();
95*cdf0e10cSrcweir             virtual bool implStartIntrinsicAnimation();
96*cdf0e10cSrcweir             virtual bool implEndIntrinsicAnimation();
97*cdf0e10cSrcweir             virtual bool implPauseIntrinsicAnimation();
98*cdf0e10cSrcweir             virtual bool implIsIntrinsicAnimationPlaying() const;
99*cdf0e10cSrcweir             virtual void implSetIntrinsicAnimationTime(double);
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir             /// the list of active view shapes (one for each registered view layer)
102*cdf0e10cSrcweir             typedef ::std::vector< ViewMediaShapeSharedPtr > ViewMediaShapeVector;
103*cdf0e10cSrcweir             ViewMediaShapeVector                             maViewMediaShapes;
104*cdf0e10cSrcweir             bool                                             mbIsPlaying;
105*cdf0e10cSrcweir         };
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir         MediaShape::MediaShape( const uno::Reference< drawing::XShape >& xShape,
109*cdf0e10cSrcweir                               	double									 nPrio,
110*cdf0e10cSrcweir                                 const SlideShowContext&                  rContext ) :
111*cdf0e10cSrcweir             ExternalShapeBase( xShape, nPrio, rContext ),
112*cdf0e10cSrcweir             maViewMediaShapes(),
113*cdf0e10cSrcweir             mbIsPlaying(false)
114*cdf0e10cSrcweir         {
115*cdf0e10cSrcweir         }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 		// ---------------------------------------------------------------------
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir         void MediaShape::implViewChanged( const UnoViewSharedPtr& rView )
120*cdf0e10cSrcweir         {
121*cdf0e10cSrcweir             // determine ViewMediaShape that needs update
122*cdf0e10cSrcweir             ViewMediaShapeVector::const_iterator       aIter(maViewMediaShapes.begin());
123*cdf0e10cSrcweir             ViewMediaShapeVector::const_iterator const aEnd (maViewMediaShapes.end());
124*cdf0e10cSrcweir             while( aIter != aEnd )
125*cdf0e10cSrcweir             {
126*cdf0e10cSrcweir                 if( (*aIter)->getViewLayer()->isOnView(rView) )
127*cdf0e10cSrcweir                     (*aIter)->resize(getBounds());
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir                 ++aIter;
130*cdf0e10cSrcweir             }
131*cdf0e10cSrcweir         }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 		// ---------------------------------------------------------------------
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir         void MediaShape::implViewsChanged()
136*cdf0e10cSrcweir         {
137*cdf0e10cSrcweir             // resize all ViewShapes
138*cdf0e10cSrcweir             ::std::for_each( maViewMediaShapes.begin(),
139*cdf0e10cSrcweir                              maViewMediaShapes.end(),
140*cdf0e10cSrcweir                              ::boost::bind(
141*cdf0e10cSrcweir                                  &ViewMediaShape::resize,
142*cdf0e10cSrcweir                                  _1,
143*cdf0e10cSrcweir                                  ::boost::cref( getBounds())) );
144*cdf0e10cSrcweir         }
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir 		// ---------------------------------------------------------------------
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir         void MediaShape::addViewLayer( const ViewLayerSharedPtr& rNewLayer,
149*cdf0e10cSrcweir                                        bool						 bRedrawLayer )
150*cdf0e10cSrcweir         {
151*cdf0e10cSrcweir             maViewMediaShapes.push_back(
152*cdf0e10cSrcweir                 ViewMediaShapeSharedPtr( new ViewMediaShape( rNewLayer,
153*cdf0e10cSrcweir                                                              getXShape(),
154*cdf0e10cSrcweir                                                              mxComponentContext )));
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir             // push new size to view shape
157*cdf0e10cSrcweir             maViewMediaShapes.back()->resize( getBounds() );
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir             // render the Shape on the newly added ViewLayer
160*cdf0e10cSrcweir             if( bRedrawLayer )
161*cdf0e10cSrcweir                 maViewMediaShapes.back()->render( getBounds() );
162*cdf0e10cSrcweir         }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 		// ---------------------------------------------------------------------
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir         bool MediaShape::removeViewLayer( const ViewLayerSharedPtr& rLayer )
167*cdf0e10cSrcweir         {
168*cdf0e10cSrcweir             const ViewMediaShapeVector::iterator aEnd( maViewMediaShapes.end() );
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir             OSL_ENSURE( ::std::count_if(maViewMediaShapes.begin(),
171*cdf0e10cSrcweir                                         aEnd,
172*cdf0e10cSrcweir                                         ::boost::bind<bool>(
173*cdf0e10cSrcweir                                             ::std::equal_to< ViewLayerSharedPtr >(),
174*cdf0e10cSrcweir                                             ::boost::bind( &ViewMediaShape::getViewLayer, _1 ),
175*cdf0e10cSrcweir                                             ::boost::cref( rLayer ) ) ) < 2,
176*cdf0e10cSrcweir                         "MediaShape::removeViewLayer(): Duplicate ViewLayer entries!" );
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir             ViewMediaShapeVector::iterator aIter;
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir             if( (aIter=::std::remove_if( maViewMediaShapes.begin(),
181*cdf0e10cSrcweir                                          aEnd,
182*cdf0e10cSrcweir                                          ::boost::bind<bool>(
183*cdf0e10cSrcweir                                              ::std::equal_to< ViewLayerSharedPtr >(),
184*cdf0e10cSrcweir                                              ::boost::bind( &ViewMediaShape::getViewLayer,
185*cdf0e10cSrcweir                                                             _1 ),
186*cdf0e10cSrcweir                                              ::boost::cref( rLayer ) ) )) == aEnd )
187*cdf0e10cSrcweir             {
188*cdf0e10cSrcweir                 // view layer seemingly was not added, failed
189*cdf0e10cSrcweir                 return false;
190*cdf0e10cSrcweir             }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir             // actually erase from container
193*cdf0e10cSrcweir             maViewMediaShapes.erase( aIter, aEnd );
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir             return true;
196*cdf0e10cSrcweir         }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir 		// ---------------------------------------------------------------------
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir         bool MediaShape::clearAllViewLayers()
201*cdf0e10cSrcweir         {
202*cdf0e10cSrcweir             maViewMediaShapes.clear();
203*cdf0e10cSrcweir             return true;
204*cdf0e10cSrcweir         }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir 		// ---------------------------------------------------------------------
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir         bool MediaShape::implRender( const ::basegfx::B2DRange& rCurrBounds ) const
209*cdf0e10cSrcweir         {
210*cdf0e10cSrcweir             // redraw all view shapes, by calling their update() method
211*cdf0e10cSrcweir             if( ::std::count_if( maViewMediaShapes.begin(),
212*cdf0e10cSrcweir                                  maViewMediaShapes.end(),
213*cdf0e10cSrcweir                                  ::boost::bind<bool>(
214*cdf0e10cSrcweir                                      ::boost::mem_fn( &ViewMediaShape::render ),
215*cdf0e10cSrcweir                                      _1,
216*cdf0e10cSrcweir                                      ::boost::cref( rCurrBounds ) ) )
217*cdf0e10cSrcweir                 != static_cast<ViewMediaShapeVector::difference_type>(maViewMediaShapes.size()) )
218*cdf0e10cSrcweir             {
219*cdf0e10cSrcweir                 // at least one of the ViewShape::update() calls did return
220*cdf0e10cSrcweir                 // false - update failed on at least one ViewLayer
221*cdf0e10cSrcweir                 return false;
222*cdf0e10cSrcweir             }
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir             return true;
225*cdf0e10cSrcweir         }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir 		// ---------------------------------------------------------------------
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir         bool MediaShape::implStartIntrinsicAnimation()
230*cdf0e10cSrcweir         {
231*cdf0e10cSrcweir             ::std::for_each( maViewMediaShapes.begin(),
232*cdf0e10cSrcweir                              maViewMediaShapes.end(),
233*cdf0e10cSrcweir                              ::boost::mem_fn( &ViewMediaShape::startMedia ) );
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir             mbIsPlaying = true;
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir             return true;
238*cdf0e10cSrcweir         }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir 		// ---------------------------------------------------------------------
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir         bool MediaShape::implEndIntrinsicAnimation()
243*cdf0e10cSrcweir         {
244*cdf0e10cSrcweir             ::std::for_each( maViewMediaShapes.begin(),
245*cdf0e10cSrcweir                              maViewMediaShapes.end(),
246*cdf0e10cSrcweir                              ::boost::mem_fn( &ViewMediaShape::endMedia ) );
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir             mbIsPlaying = false;
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir             return true;
251*cdf0e10cSrcweir         }
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir 		// ---------------------------------------------------------------------
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir         bool MediaShape::implPauseIntrinsicAnimation()
256*cdf0e10cSrcweir         {
257*cdf0e10cSrcweir             ::std::for_each( maViewMediaShapes.begin(),
258*cdf0e10cSrcweir                              maViewMediaShapes.end(),
259*cdf0e10cSrcweir                              ::boost::mem_fn( &ViewMediaShape::pauseMedia ) );
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir             mbIsPlaying = false;
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir             return true;
264*cdf0e10cSrcweir         }
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir 		// ---------------------------------------------------------------------
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir         bool MediaShape::implIsIntrinsicAnimationPlaying() const
269*cdf0e10cSrcweir         {
270*cdf0e10cSrcweir             return mbIsPlaying;
271*cdf0e10cSrcweir         }
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir 		// ---------------------------------------------------------------------
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir         void MediaShape::implSetIntrinsicAnimationTime(double fTime)
276*cdf0e10cSrcweir         {
277*cdf0e10cSrcweir             ::std::for_each( maViewMediaShapes.begin(),
278*cdf0e10cSrcweir                              maViewMediaShapes.end(),
279*cdf0e10cSrcweir                              ::boost::bind( &ViewMediaShape::setMediaTime,
280*cdf0e10cSrcweir                                             _1, boost::cref(fTime)) );
281*cdf0e10cSrcweir         }
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir 		// ---------------------------------------------------------------------
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir         ShapeSharedPtr createMediaShape(
286*cdf0e10cSrcweir             const uno::Reference< drawing::XShape >& xShape,
287*cdf0e10cSrcweir             double                                   nPrio,
288*cdf0e10cSrcweir             const SlideShowContext&                  rContext)
289*cdf0e10cSrcweir         {
290*cdf0e10cSrcweir             boost::shared_ptr< MediaShape > pMediaShape(
291*cdf0e10cSrcweir                 new MediaShape(xShape, nPrio, rContext));
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir             return pMediaShape;
294*cdf0e10cSrcweir         }
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir     }
297*cdf0e10cSrcweir }
298