1*aaef562fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*aaef562fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*aaef562fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*aaef562fSAndrew Rist  * distributed with this work for additional information
6*aaef562fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*aaef562fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*aaef562fSAndrew Rist  * "License"); you may not use this file except in compliance
9*aaef562fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*aaef562fSAndrew Rist  *
11*aaef562fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*aaef562fSAndrew Rist  *
13*aaef562fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*aaef562fSAndrew Rist  * software distributed under the License is distributed on an
15*aaef562fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*aaef562fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*aaef562fSAndrew Rist  * specific language governing permissions and limitations
18*aaef562fSAndrew Rist  * under the License.
19*aaef562fSAndrew Rist  *
20*aaef562fSAndrew Rist  *************************************************************/
21*aaef562fSAndrew Rist 
22*aaef562fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef INCLUDED_SLIDESHOW_VIEWSHAPE_HXX
25cdf0e10cSrcweir #define INCLUDED_SLIDESHOW_VIEWSHAPE_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <cppcanvas/renderer.hxx>
28cdf0e10cSrcweir #include <cppcanvas/bitmap.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <basegfx/range/b2drectangle.hxx>
31cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
34cdf0e10cSrcweir #include <boost/utility.hpp>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include "tools.hxx"
37cdf0e10cSrcweir #include "shapeattributelayer.hxx"
38cdf0e10cSrcweir #include "animatedsprite.hxx"
39cdf0e10cSrcweir #include "viewlayer.hxx"
40cdf0e10cSrcweir #include "doctreenode.hxx"
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include <vector>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 
45cdf0e10cSrcweir namespace slideshow
46cdf0e10cSrcweir {
47cdf0e10cSrcweir     namespace internal
48cdf0e10cSrcweir     {
49cdf0e10cSrcweir         /** This class is the viewable representation of a draw
50cdf0e10cSrcweir             document's XShape, associated to a specific View
51cdf0e10cSrcweir 
52cdf0e10cSrcweir             The class is able to render the associated XShape on
53cdf0e10cSrcweir             View implementations.
54cdf0e10cSrcweir          */
55cdf0e10cSrcweir         class ViewShape : private boost::noncopyable
56cdf0e10cSrcweir         {
57cdf0e10cSrcweir         public:
58cdf0e10cSrcweir             /** Create a ViewShape for the given View
59cdf0e10cSrcweir 
60cdf0e10cSrcweir             	@param rView
61cdf0e10cSrcweir                 The associated View object.
62cdf0e10cSrcweir              */
63cdf0e10cSrcweir             explicit ViewShape( const ViewLayerSharedPtr& rViewLayer );
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 			/** Query the associated view layer of this shape
66cdf0e10cSrcweir              */
67cdf0e10cSrcweir             ViewLayerSharedPtr getViewLayer() const;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir             /** Query dimension of a safety border around the shape for AA
70cdf0e10cSrcweir 
71cdf0e10cSrcweir             	If the view performs antialiasing, this method
72cdf0e10cSrcweir             	calculates a safety border around the shape, in the
73cdf0e10cSrcweir             	shape coordinate system, which is guaranteed to
74cdf0e10cSrcweir             	include every pixel touched when rendering the shape.
75cdf0e10cSrcweir              */
76cdf0e10cSrcweir             ::basegfx::B2DSize getAntialiasingBorder() const;
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 
79cdf0e10cSrcweir             // animation methods
80cdf0e10cSrcweir             //------------------------------------------------------------------
81cdf0e10cSrcweir 
82cdf0e10cSrcweir             /** Notify the ViewShape that an animation starts now
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 				This method enters animation mode on the associate
85cdf0e10cSrcweir 				target view. The shape can be animated in parallel on
86cdf0e10cSrcweir 				different views.
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 	            @return whether the mode change finished successfully.
89cdf0e10cSrcweir              */
90cdf0e10cSrcweir             bool enterAnimationMode();
91cdf0e10cSrcweir 
92cdf0e10cSrcweir             /** Notify the ViewShape that it is no longer animated
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 				This methods ends animation mode on the associate
95cdf0e10cSrcweir 				target view
96cdf0e10cSrcweir              */
97cdf0e10cSrcweir             void leaveAnimationMode();
98cdf0e10cSrcweir 
99cdf0e10cSrcweir             /** Query whether the ViewShape is currently animated
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 				This method checks whether the ViewShape is currently in
102cdf0e10cSrcweir 				animation mode.
103cdf0e10cSrcweir              */
isBackgroundDetached() const104cdf0e10cSrcweir             bool isBackgroundDetached() const { return mbAnimationMode; }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 
107cdf0e10cSrcweir             // render methods
108cdf0e10cSrcweir             //------------------------------------------------------------------
109cdf0e10cSrcweir 
110cdf0e10cSrcweir             enum UpdateFlags
111cdf0e10cSrcweir             {
112cdf0e10cSrcweir                 NONE=			0,
113cdf0e10cSrcweir                 TRANSFORMATION=	1,
114cdf0e10cSrcweir                 CLIP=			2,
115cdf0e10cSrcweir                 ALPHA=			4,
116cdf0e10cSrcweir                 POSITION=		8,
117cdf0e10cSrcweir                 CONTENT=	    16,
118cdf0e10cSrcweir                 FORCE=			32
119cdf0e10cSrcweir             };
120cdf0e10cSrcweir 
121cdf0e10cSrcweir             struct RenderArgs
122cdf0e10cSrcweir             {
123cdf0e10cSrcweir                 /** Create render argument struct
124cdf0e10cSrcweir 
125cdf0e10cSrcweir                     @param rOrigBounds
126cdf0e10cSrcweir                     The initial shape bounds
127cdf0e10cSrcweir 
128cdf0e10cSrcweir                     @param rUpdateBounds
129cdf0e10cSrcweir                     The area covered by the shape
130cdf0e10cSrcweir 
131cdf0e10cSrcweir                     @param rBounds
132cdf0e10cSrcweir                     The current shape bounds
133cdf0e10cSrcweir 
134cdf0e10cSrcweir                     @param rAttr
135cdf0e10cSrcweir                     The current shape attribute set. Can be NULL, for
136cdf0e10cSrcweir                     default attributes. Attention: stored as a reference,
137cdf0e10cSrcweir                     thus, parameter object must stay valid!
138cdf0e10cSrcweir 
139cdf0e10cSrcweir                     @param rSubsets
140cdf0e10cSrcweir                     Vector of subset rendering ranges. Attention:
141cdf0e10cSrcweir                     stored as a reference, thus, parameter object must
142cdf0e10cSrcweir                     stay valid!
143cdf0e10cSrcweir 
144cdf0e10cSrcweir                     @param nPrio
145cdf0e10cSrcweir                     Shape priority
146cdf0e10cSrcweir                 */
RenderArgsslideshow::internal::ViewShape::RenderArgs147cdf0e10cSrcweir                 RenderArgs( const ::basegfx::B2DRectangle&		rOrigBounds,
148cdf0e10cSrcweir                             const ::basegfx::B2DRectangle&		rUpdateBounds,
149cdf0e10cSrcweir                             const ::basegfx::B2DRectangle&		rBounds,
150cdf0e10cSrcweir                             const ::basegfx::B2DRectangle&		rUnitBounds,
151cdf0e10cSrcweir                             const ShapeAttributeLayerSharedPtr&	rAttr,
152cdf0e10cSrcweir                             const VectorOfDocTreeNodes&			rSubsets,
153cdf0e10cSrcweir                             double                              nPrio ) :
154cdf0e10cSrcweir                     maOrigBounds( rOrigBounds ),
155cdf0e10cSrcweir                     maUpdateBounds( rUpdateBounds ),
156cdf0e10cSrcweir                     maBounds( rBounds ),
157cdf0e10cSrcweir                     maUnitBounds( rUnitBounds ),
158cdf0e10cSrcweir                     mrAttr( rAttr ),
159cdf0e10cSrcweir                     mrSubsets( rSubsets ),
160cdf0e10cSrcweir                     mnShapePriority( nPrio )
161cdf0e10cSrcweir                 {
162cdf0e10cSrcweir                 }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir                 const ::basegfx::B2DRectangle		maOrigBounds;
165cdf0e10cSrcweir                 const ::basegfx::B2DRectangle		maUpdateBounds;
166cdf0e10cSrcweir                 const ::basegfx::B2DRectangle		maBounds;
167cdf0e10cSrcweir                 const ::basegfx::B2DRectangle		maUnitBounds;
168cdf0e10cSrcweir                 const ShapeAttributeLayerSharedPtr&	mrAttr;
169cdf0e10cSrcweir                 const VectorOfDocTreeNodes&			mrSubsets;
170cdf0e10cSrcweir                 const double                        mnShapePriority;
171cdf0e10cSrcweir             };
172cdf0e10cSrcweir 
173cdf0e10cSrcweir             /** Update the ViewShape
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 				This method updates the ViewShape on the associated
176cdf0e10cSrcweir 				view. If the shape is currently animated, the render
177cdf0e10cSrcweir 				target is the sprite, otherwise the view's
178cdf0e10cSrcweir 				canvas. This method does not render anything, if the
179cdf0e10cSrcweir 				update flags are 0.
180cdf0e10cSrcweir 
181cdf0e10cSrcweir                 @param rMtf
182cdf0e10cSrcweir                 The metafile representation of the shape
183cdf0e10cSrcweir 
184cdf0e10cSrcweir                 @param rArgs
185cdf0e10cSrcweir                 Parameter structure, containing all necessary arguments
186cdf0e10cSrcweir 
187cdf0e10cSrcweir                 @param nUpdateFlags
188cdf0e10cSrcweir                 Bitmask of things to update. Use FORCE to force a repaint.
189cdf0e10cSrcweir 
190cdf0e10cSrcweir                 @param bIsVisible
191cdf0e10cSrcweir                 When false, the shape is fully invisible (and possibly
192cdf0e10cSrcweir                 don't need to be painted)
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 	            @return whether the rendering finished successfully.
195cdf0e10cSrcweir             */
196cdf0e10cSrcweir             bool update( const GDIMetaFileSharedPtr&	rMtf,
197cdf0e10cSrcweir                          const RenderArgs&				rArgs,
198cdf0e10cSrcweir                          int							nUpdateFlags,
199cdf0e10cSrcweir                          bool							bIsVisible ) const;
200cdf0e10cSrcweir 
201cdf0e10cSrcweir             /** Retrieve renderer for given canvas and metafile.
202cdf0e10cSrcweir 
203cdf0e10cSrcweir             	If necessary, the renderer is created or updated for
204cdf0e10cSrcweir             	the metafile and attribute layer.
205cdf0e10cSrcweir 
206cdf0e10cSrcweir                 @return a renderer that renders to the given
207cdf0e10cSrcweir                 destination canvas
208cdf0e10cSrcweir              */
209cdf0e10cSrcweir             ::cppcanvas::RendererSharedPtr getRenderer( const ::cppcanvas::CanvasSharedPtr&	rDestinationCanvas,
210cdf0e10cSrcweir                                                         const GDIMetaFileSharedPtr&			rMtf,
211cdf0e10cSrcweir                                                         const ShapeAttributeLayerSharedPtr&	rAttr ) const;
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 
214cdf0e10cSrcweir         private:
215cdf0e10cSrcweir             struct RendererCacheEntry
216cdf0e10cSrcweir             {
RendererCacheEntryslideshow::internal::ViewShape::RendererCacheEntry217cdf0e10cSrcweir                 RendererCacheEntry() :
218cdf0e10cSrcweir                     mpDestinationCanvas(),
219cdf0e10cSrcweir                     mpRenderer(),
220cdf0e10cSrcweir                     mpMtf(),
221cdf0e10cSrcweir                     mpLastBitmap(),
222cdf0e10cSrcweir                     mpLastBitmapCanvas()
223cdf0e10cSrcweir                 {
224cdf0e10cSrcweir                 }
225cdf0e10cSrcweir 
getDestinationCanvasslideshow::internal::ViewShape::RendererCacheEntry226cdf0e10cSrcweir                 ::cppcanvas::CanvasSharedPtr getDestinationCanvas()
227cdf0e10cSrcweir                 {
228cdf0e10cSrcweir                     return mpDestinationCanvas;
229cdf0e10cSrcweir                 }
230cdf0e10cSrcweir 
231cdf0e10cSrcweir                 ::cppcanvas::CanvasSharedPtr		mpDestinationCanvas;
232cdf0e10cSrcweir                 ::cppcanvas::RendererSharedPtr		mpRenderer;
233cdf0e10cSrcweir                 GDIMetaFileSharedPtr				mpMtf;
234cdf0e10cSrcweir                 ::cppcanvas::BitmapSharedPtr		mpLastBitmap;
235cdf0e10cSrcweir                 ::cppcanvas::BitmapCanvasSharedPtr	mpLastBitmapCanvas;
236cdf0e10cSrcweir             };
237cdf0e10cSrcweir 
238cdf0e10cSrcweir             typedef ::std::vector< RendererCacheEntry >	RendererCacheVector;
239cdf0e10cSrcweir 
240cdf0e10cSrcweir 
241cdf0e10cSrcweir             /** Prefetch Renderer for given canvas
242cdf0e10cSrcweir              */
243cdf0e10cSrcweir             bool prefetch( RendererCacheEntry&					io_rCacheEntry,
244cdf0e10cSrcweir                            const ::cppcanvas::CanvasSharedPtr&	rDestinationCanvas,
245cdf0e10cSrcweir                            const GDIMetaFileSharedPtr&			rMtf,
246cdf0e10cSrcweir                            const ShapeAttributeLayerSharedPtr&	rAttr ) const;
247cdf0e10cSrcweir 
248cdf0e10cSrcweir             /** Draw with prefetched Renderer to stored canvas
249cdf0e10cSrcweir 
250cdf0e10cSrcweir             	This method draws prefetched Renderer to its
251cdf0e10cSrcweir             	associated canvas (which happens to be mpLastCanvas).
252cdf0e10cSrcweir              */
253cdf0e10cSrcweir             bool draw( const ::cppcanvas::CanvasSharedPtr&	rDestinationCanvas,
254cdf0e10cSrcweir                        const GDIMetaFileSharedPtr&			rMtf,
255cdf0e10cSrcweir                        const ShapeAttributeLayerSharedPtr&	rAttr,
256cdf0e10cSrcweir                        const ::basegfx::B2DHomMatrix&		rTransform,
257cdf0e10cSrcweir                        const ::basegfx::B2DPolyPolygon*		pClip,
258cdf0e10cSrcweir                        const VectorOfDocTreeNodes&			rSubsets ) const;
259cdf0e10cSrcweir 
260cdf0e10cSrcweir             /** Render shape to an active sprite
261cdf0e10cSrcweir              */
262cdf0e10cSrcweir             bool renderSprite( const ViewLayerSharedPtr&            rViewLayer,
263cdf0e10cSrcweir                                const GDIMetaFileSharedPtr&			rMtf,
264cdf0e10cSrcweir                                const ::basegfx::B2DRectangle&		rOrigBounds,
265cdf0e10cSrcweir                                const ::basegfx::B2DRectangle&		rBounds,
266cdf0e10cSrcweir                                const ::basegfx::B2DRectangle&		rUnitBounds,
267cdf0e10cSrcweir                                int									nUpdateFlags,
268cdf0e10cSrcweir                                const ShapeAttributeLayerSharedPtr&	pAttr,
269cdf0e10cSrcweir                                const VectorOfDocTreeNodes&			rSubsets,
270cdf0e10cSrcweir                                double                               nPrio,
271cdf0e10cSrcweir                                bool 								bIsVisible ) const;
272cdf0e10cSrcweir 
273cdf0e10cSrcweir             /** Render shape to given canvas
274cdf0e10cSrcweir              */
275cdf0e10cSrcweir             bool render( const ::cppcanvas::CanvasSharedPtr&	rDestinationCanvas,
276cdf0e10cSrcweir                          const GDIMetaFileSharedPtr&			rMtf,
277cdf0e10cSrcweir                          const ::basegfx::B2DRectangle&			rBounds,
278cdf0e10cSrcweir                          const ::basegfx::B2DRectangle&			rUpdateBounds,
279cdf0e10cSrcweir                          int									nUpdateFlags,
280cdf0e10cSrcweir                          const ShapeAttributeLayerSharedPtr&	pAttr,
281cdf0e10cSrcweir                          const VectorOfDocTreeNodes&			rSubsets,
282cdf0e10cSrcweir                          bool 									bIsVisible ) const;
283cdf0e10cSrcweir 
284cdf0e10cSrcweir             /** Calc sprite size in pixel
285cdf0e10cSrcweir 
286cdf0e10cSrcweir             	Converts user coordinate system to device pixel, and
287cdf0e10cSrcweir             	adds antialiasing border.
288cdf0e10cSrcweir 
289cdf0e10cSrcweir             	@param rUserSize
290cdf0e10cSrcweir                 Size of the sprite in user coordinate system (doc coordinates)
291cdf0e10cSrcweir              */
292cdf0e10cSrcweir             ::basegfx::B2DSize calcSpriteSizePixel( const ::basegfx::B2DSize& rUserSize ) const;
293cdf0e10cSrcweir 
294cdf0e10cSrcweir             enum{ MAX_RENDER_CACHE_ENTRIES=2 };
295cdf0e10cSrcweir 
296cdf0e10cSrcweir             /** Retrieve a valid iterator to renderer cache entry
297cdf0e10cSrcweir 
298cdf0e10cSrcweir             	This method ensures that an internal limit of
299cdf0e10cSrcweir             	MAX_RENDER_CACHE_ENTRIES is not exceeded.
300cdf0e10cSrcweir 
301cdf0e10cSrcweir             	@param rDestinationCanvas
302cdf0e10cSrcweir                 Destination canvas to retrieve cache entry for
303cdf0e10cSrcweir 
304cdf0e10cSrcweir             	@return a valid iterator to a renderer cache entry for
305cdf0e10cSrcweir             	the given canvas. The entry might be
306cdf0e10cSrcweir             	default-constructed (if newly added)
307cdf0e10cSrcweir              */
308cdf0e10cSrcweir             RendererCacheVector::iterator getCacheEntry( const ::cppcanvas::CanvasSharedPtr& rDestinationCanvas ) const;
309cdf0e10cSrcweir 
310cdf0e10cSrcweir             void invalidateRenderer() const;
311cdf0e10cSrcweir 
312cdf0e10cSrcweir             /** The view layer this object is part of.
313cdf0e10cSrcweir 
314cdf0e10cSrcweir                 Needed for sprite creation
315cdf0e10cSrcweir             */
316cdf0e10cSrcweir             ViewLayerSharedPtr							mpViewLayer;
317cdf0e10cSrcweir 
318cdf0e10cSrcweir             /// A set of cached mtf/canvas combinations
319cdf0e10cSrcweir             mutable RendererCacheVector					maRenderers;
320cdf0e10cSrcweir 
321cdf0e10cSrcweir             /// The sprite object
322cdf0e10cSrcweir             mutable AnimatedSpriteSharedPtr				mpSprite;
323cdf0e10cSrcweir 
324cdf0e10cSrcweir             /// If true, render() calls go to the sprite
325cdf0e10cSrcweir             mutable bool								mbAnimationMode;
326cdf0e10cSrcweir 
327cdf0e10cSrcweir             /// If true, shape needs full repaint (and the sprite a setup, if any)
328cdf0e10cSrcweir             mutable bool								mbForceUpdate;
329cdf0e10cSrcweir         };
330cdf0e10cSrcweir 
331cdf0e10cSrcweir         typedef ::boost::shared_ptr< ViewShape > ViewShapeSharedPtr;
332cdf0e10cSrcweir 
333cdf0e10cSrcweir     }
334cdf0e10cSrcweir }
335cdf0e10cSrcweir 
336cdf0e10cSrcweir #endif /* INCLUDED_SLIDESHOW_VIEWSHAPE_HXX */
337