1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_slideshow.hxx"
26 
27 // must be first
28 #include <canvas/debug.hxx>
29 #include <canvas/verbosetrace.hxx>
30 #include <canvas/canvastools.hxx>
31 
32 #include <com/sun/star/drawing/XShape.hpp>
33 
34 #include "mediashape.hxx"
35 #include "viewmediashape.hxx"
36 #include "externalshapebase.hxx"
37 #include "slideshowcontext.hxx"
38 #include "shape.hxx"
39 #include "tools.hxx"
40 
41 #include <boost/bind.hpp>
42 #include <algorithm>
43 
44 
45 using namespace ::com::sun::star;
46 
47 
48 namespace slideshow
49 {
50     namespace internal
51     {
52         /** Represents a media shape.
53 
54             This implementation offers support for media shapes.
55 			Such shapes need special treatment.
56          */
57         class MediaShape : public ExternalShapeBase
58         {
59         public:
60             /** Create a shape for the given XShape for a media object
61 
62             	@param xShape
63                 The XShape to represent.
64 
65                 @param nPrio
66                 Externally-determined shape priority (used e.g. for
67                 paint ordering). This number _must be_ unique!
68              */
69             MediaShape( const ::com::sun::star::uno::Reference<
70                        		::com::sun::star::drawing::XShape >&	xShape,
71                         double										nPrio,
72                         const SlideShowContext&                     rContext ); // throw ShapeLoadFailedException;
73 
74         private:
75 
76             // View layer methods
77             //------------------------------------------------------------------
78 
79             virtual void addViewLayer( const ViewLayerSharedPtr& 	rNewLayer,
80                                        bool							bRedrawLayer );
81             virtual bool removeViewLayer( const ViewLayerSharedPtr& rNewLayer );
82             virtual bool clearAllViewLayers();
83 
84 
85             // ExternalShapeBase methods
86             //------------------------------------------------------------------
87 
88             virtual bool implRender( const ::basegfx::B2DRange& rCurrBounds ) const;
89             virtual void implViewChanged( const UnoViewSharedPtr& rView );
90             virtual void implViewsChanged();
91             virtual bool implStartIntrinsicAnimation();
92             virtual bool implEndIntrinsicAnimation();
93             virtual bool implPauseIntrinsicAnimation();
94             virtual bool implIsIntrinsicAnimationPlaying() const;
95             virtual void implSetIntrinsicAnimationTime(double);
96 
97             /// the list of active view shapes (one for each registered view layer)
98             typedef ::std::vector< ViewMediaShapeSharedPtr > ViewMediaShapeVector;
99             ViewMediaShapeVector                             maViewMediaShapes;
100             bool                                             mbIsPlaying;
101         };
102 
103 
MediaShape(const uno::Reference<drawing::XShape> & xShape,double nPrio,const SlideShowContext & rContext)104         MediaShape::MediaShape( const uno::Reference< drawing::XShape >& xShape,
105                               	double									 nPrio,
106                                 const SlideShowContext&                  rContext ) :
107             ExternalShapeBase( xShape, nPrio, rContext ),
108             maViewMediaShapes(),
109             mbIsPlaying(false)
110         {
111         }
112 
113 		// ---------------------------------------------------------------------
114 
implViewChanged(const UnoViewSharedPtr & rView)115         void MediaShape::implViewChanged( const UnoViewSharedPtr& rView )
116         {
117             // determine ViewMediaShape that needs update
118             ViewMediaShapeVector::const_iterator       aIter(maViewMediaShapes.begin());
119             ViewMediaShapeVector::const_iterator const aEnd (maViewMediaShapes.end());
120             while( aIter != aEnd )
121             {
122                 if( (*aIter)->getViewLayer()->isOnView(rView) )
123                     (*aIter)->resize(getBounds());
124 
125                 ++aIter;
126             }
127         }
128 
129 		// ---------------------------------------------------------------------
130 
implViewsChanged()131         void MediaShape::implViewsChanged()
132         {
133             // resize all ViewShapes
134             ::basegfx::B2DRange aBounds = getBounds();
135             ::std::for_each( maViewMediaShapes.begin(),
136                              maViewMediaShapes.end(),
137                              ::boost::bind(
138                                  &ViewMediaShape::resize,
139                                  _1,
140                                  ::boost::cref( aBounds ) ) );
141         }
142 
143 		// ---------------------------------------------------------------------
144 
addViewLayer(const ViewLayerSharedPtr & rNewLayer,bool bRedrawLayer)145         void MediaShape::addViewLayer( const ViewLayerSharedPtr& rNewLayer,
146                                        bool						 bRedrawLayer )
147         {
148             maViewMediaShapes.push_back(
149                 ViewMediaShapeSharedPtr( new ViewMediaShape( rNewLayer,
150                                                              getXShape(),
151                                                              mxComponentContext )));
152 
153             // push new size to view shape
154             maViewMediaShapes.back()->resize( getBounds() );
155 
156             // render the Shape on the newly added ViewLayer
157             if( bRedrawLayer )
158                 maViewMediaShapes.back()->render( getBounds() );
159         }
160 
161 		// ---------------------------------------------------------------------
162 
removeViewLayer(const ViewLayerSharedPtr & rLayer)163         bool MediaShape::removeViewLayer( const ViewLayerSharedPtr& rLayer )
164         {
165             const ViewMediaShapeVector::iterator aEnd( maViewMediaShapes.end() );
166 
167             OSL_ENSURE( ::std::count_if(maViewMediaShapes.begin(),
168                                         aEnd,
169                                         ::boost::bind<bool>(
170                                             ::std::equal_to< ViewLayerSharedPtr >(),
171                                             ::boost::bind( &ViewMediaShape::getViewLayer, _1 ),
172                                             ::boost::cref( rLayer ) ) ) < 2,
173                         "MediaShape::removeViewLayer(): Duplicate ViewLayer entries!" );
174 
175             ViewMediaShapeVector::iterator aIter;
176 
177             if( (aIter=::std::remove_if( maViewMediaShapes.begin(),
178                                          aEnd,
179                                          ::boost::bind<bool>(
180                                              ::std::equal_to< ViewLayerSharedPtr >(),
181                                              ::boost::bind( &ViewMediaShape::getViewLayer,
182                                                             _1 ),
183                                              ::boost::cref( rLayer ) ) )) == aEnd )
184             {
185                 // view layer seemingly was not added, failed
186                 return false;
187             }
188 
189             // actually erase from container
190             maViewMediaShapes.erase( aIter, aEnd );
191 
192             return true;
193         }
194 
195 		// ---------------------------------------------------------------------
196 
clearAllViewLayers()197         bool MediaShape::clearAllViewLayers()
198         {
199             maViewMediaShapes.clear();
200             return true;
201         }
202 
203 		// ---------------------------------------------------------------------
204 
implRender(const::basegfx::B2DRange & rCurrBounds) const205         bool MediaShape::implRender( const ::basegfx::B2DRange& rCurrBounds ) const
206         {
207             // redraw all view shapes, by calling their update() method
208             if( ::std::count_if( maViewMediaShapes.begin(),
209                                  maViewMediaShapes.end(),
210                                  ::boost::bind<bool>(
211                                      ::boost::mem_fn( &ViewMediaShape::render ),
212                                      _1,
213                                      ::boost::cref( rCurrBounds ) ) )
214                 != static_cast<ViewMediaShapeVector::difference_type>(maViewMediaShapes.size()) )
215             {
216                 // at least one of the ViewShape::update() calls did return
217                 // false - update failed on at least one ViewLayer
218                 return false;
219             }
220 
221             return true;
222         }
223 
224 		// ---------------------------------------------------------------------
225 
implStartIntrinsicAnimation()226         bool MediaShape::implStartIntrinsicAnimation()
227         {
228             ::std::for_each( maViewMediaShapes.begin(),
229                              maViewMediaShapes.end(),
230                              ::boost::mem_fn( &ViewMediaShape::startMedia ) );
231 
232             mbIsPlaying = true;
233 
234             return true;
235         }
236 
237 		// ---------------------------------------------------------------------
238 
implEndIntrinsicAnimation()239         bool MediaShape::implEndIntrinsicAnimation()
240         {
241             ::std::for_each( maViewMediaShapes.begin(),
242                              maViewMediaShapes.end(),
243                              ::boost::mem_fn( &ViewMediaShape::endMedia ) );
244 
245             mbIsPlaying = false;
246 
247             return true;
248         }
249 
250 		// ---------------------------------------------------------------------
251 
implPauseIntrinsicAnimation()252         bool MediaShape::implPauseIntrinsicAnimation()
253         {
254             ::std::for_each( maViewMediaShapes.begin(),
255                              maViewMediaShapes.end(),
256                              ::boost::mem_fn( &ViewMediaShape::pauseMedia ) );
257 
258             mbIsPlaying = false;
259 
260             return true;
261         }
262 
263 		// ---------------------------------------------------------------------
264 
implIsIntrinsicAnimationPlaying() const265         bool MediaShape::implIsIntrinsicAnimationPlaying() const
266         {
267             return mbIsPlaying;
268         }
269 
270 		// ---------------------------------------------------------------------
271 
implSetIntrinsicAnimationTime(double fTime)272         void MediaShape::implSetIntrinsicAnimationTime(double fTime)
273         {
274             ::std::for_each( maViewMediaShapes.begin(),
275                              maViewMediaShapes.end(),
276                              ::boost::bind( &ViewMediaShape::setMediaTime,
277                                             _1, boost::cref(fTime)) );
278         }
279 
280 		// ---------------------------------------------------------------------
281 
createMediaShape(const uno::Reference<drawing::XShape> & xShape,double nPrio,const SlideShowContext & rContext)282         ShapeSharedPtr createMediaShape(
283             const uno::Reference< drawing::XShape >& xShape,
284             double                                   nPrio,
285             const SlideShowContext&                  rContext)
286         {
287             boost::shared_ptr< MediaShape > pMediaShape(
288                 new MediaShape(xShape, nPrio, rContext));
289 
290             return pMediaShape;
291         }
292 
293     }
294 }
295