xref: /aoo41x/main/slideshow/source/inc/tools.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef INCLUDED_SLIDESHOW_TOOLS_HXX
29 #define INCLUDED_SLIDESHOW_TOOLS_HXX
30 
31 #include <com/sun/star/uno/Sequence.hxx>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 
34 #include <cppcanvas/color.hxx>
35 
36 #include "shapeattributelayer.hxx"
37 #include "shape.hxx"
38 #include "rgbcolor.hxx"
39 #include "hslcolor.hxx"
40 
41 #include <boost/shared_ptr.hpp>
42 #include <boost/current_function.hpp>
43 
44 #include <functional>
45 #include <cstdlib>
46 #include <string.h> // for strcmp
47 #include <algorithm>
48 
49 
50 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
51 
52 
53 namespace com { namespace sun { namespace star { namespace beans {
54     struct NamedValue;
55 } } } }
56 namespace basegfx
57 {
58     class B2DRange;
59     class B2DVector;
60     class B2IVector;
61     class B2DHomMatrix;
62 }
63 namespace cppcanvas{ class Canvas; }
64 
65 class GDIMetaFile;
66 
67 /* Definition of some animation tools */
68 namespace slideshow
69 {
70     namespace internal
71     {
72         class UnoView;
73         class Shape;
74         class ShapeAttributeLayer;
75 
76         typedef ::boost::shared_ptr< GDIMetaFile > GDIMetaFileSharedPtr;
77 
78         // xxx todo: remove with boost::hash when 1.33 is available
79         template <typename T>
80         struct hash : ::std::unary_function<T, ::std::size_t>
81         {
82             ::std::size_t operator()( T const& val ) const {
83                 return hash_value(val);
84             }
85         };
86         template <typename T>
87         inline ::std::size_t hash_value( T * const& p )
88         {
89             ::std::size_t d = static_cast< ::std::size_t >(
90                 reinterpret_cast< ::std::ptrdiff_t >(p) );
91             return d + (d >> 3);
92         }
93 
94         // xxx todo: shift to namespace com::sun::star::uno when
95         //           1.33 is available
96         template <typename T>
97         inline ::std::size_t hash_value(
98             ::com::sun::star::uno::Reference<T> const& x )
99         {
100             // normalize to object root, because _only_ XInterface is defined
101             // to be stable during object lifetime:
102             ::com::sun::star::uno::Reference<
103                   ::com::sun::star::uno::XInterface> const xRoot(
104                       x, ::com::sun::star::uno::UNO_QUERY );
105             return hash<void *>()(xRoot.get());
106         }
107 
108         /** Cycle mode of intrinsic animations
109          */
110         enum CycleMode
111         {
112             /// loop the animation back to back
113             CYCLE_LOOP,
114             /// loop, but play backwards from end to start
115             CYCLE_PINGPONGLOOP
116         };
117 
118 
119         // Value extraction from Any
120         // =========================
121 
122         /// extract unary double value from Any
123 		bool extractValue( double&								o_rValue,
124                            const ::com::sun::star::uno::Any& 	rSourceAny,
125                            const boost::shared_ptr<Shape>&		rShape,
126                            const basegfx::B2DVector&			rSlideBounds );
127 
128         /// extract int from Any
129         bool extractValue( sal_Int32&							o_rValue,
130                            const ::com::sun::star::uno::Any& 	rSourceAny,
131                            const boost::shared_ptr<Shape>&		rShape,
132                            const basegfx::B2DVector&			rSlideBounds );
133 
134         /// extract enum/constant group value from Any
135         bool extractValue( sal_Int16&							o_rValue,
136                            const ::com::sun::star::uno::Any& 	rSourceAny,
137                            const boost::shared_ptr<Shape>&		rShape,
138                            const basegfx::B2DVector&			rSlideBounds );
139 
140         /// extract color value from Any
141         bool extractValue( RGBColor&							o_rValue,
142                            const ::com::sun::star::uno::Any& 	rSourceAny,
143                            const boost::shared_ptr<Shape>&		rShape,
144                            const basegfx::B2DVector&			rSlideBounds );
145 
146         /// extract color value from Any
147         bool extractValue( HSLColor&							o_rValue,
148                            const ::com::sun::star::uno::Any& 	rSourceAny,
149                            const boost::shared_ptr<Shape>&		rShape,
150                            const basegfx::B2DVector&			rSlideBounds );
151 
152         /// extract plain string from Any
153         bool extractValue( ::rtl::OUString&						o_rValue,
154                            const ::com::sun::star::uno::Any& 	rSourceAny,
155                            const boost::shared_ptr<Shape>&		rShape,
156                            const basegfx::B2DVector&			rSlideBounds );
157 
158         /// extract bool value from Any
159         bool extractValue( bool&								o_rValue,
160                            const ::com::sun::star::uno::Any& 	rSourceAny,
161                            const boost::shared_ptr<Shape>&		rShape,
162                            const basegfx::B2DVector&			rSlideBounds );
163 
164         /// extract double 2-tuple from Any
165         bool extractValue( basegfx::B2DTuple&					o_rPair,
166                            const ::com::sun::star::uno::Any& 	rSourceAny,
167                            const boost::shared_ptr<Shape>&		rShape,
168                            const basegfx::B2DVector&			rSlideBounds );
169 
170         /** Search a sequence of NamedValues for a given element.
171 
172         	@return true, if the sequence contains the specified
173         	element.
174          */
175         bool findNamedValue( ::com::sun::star::uno::Sequence<
176                              	::com::sun::star::beans::NamedValue > const& rSequence,
177                              const ::com::sun::star::beans::NamedValue&	rSearchKey );
178 
179         /** Search a sequence of NamedValues for an element with a given name.
180 
181         	@param o_pRet
182             If non-NULL, receives the full NamedValue found (if it was
183             found, that is).
184 
185         	@return true, if the sequence contains the specified
186         	element.
187          */
188         bool findNamedValue( ::com::sun::star::beans::NamedValue* 		o_pRet,
189                              const ::com::sun::star::uno::Sequence<
190                              	::com::sun::star::beans::NamedValue >& 	rSequence,
191                              const ::rtl::OUString&						rSearchString );
192 
193         basegfx::B2DRange calcRelativeShapeBounds( const basegfx::B2DVector& rPageSize,
194                                                    const basegfx::B2DRange&  rShapeBounds );
195 
196         /** Get the shape transformation from the attribute set
197 
198 	        @param rBounds
199             Original shape bound rect (to substitute default attribute
200             layer values)
201 
202             @param pAttr
203             Attribute set. Might be NULL (then, rBounds is used to set
204             a simple scale and translate of the unit rect to rBounds).
205         */
206         basegfx::B2DHomMatrix getShapeTransformation(
207             const basegfx::B2DRange&                      rBounds,
208             const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
209 
210         /** Get a shape's sprite transformation from the attribute set
211 
212 	        @param rPixelSize
213             Pixel size of the sprite
214 
215             @param rOrigSize
216             Original shape size (i.e. the size of the actual sprite
217             content, in the user coordinate system)
218 
219             @param pAttr
220             Attribute set. Might be NULL (then, rBounds is used to set
221             a simple scale and translate of the unit rect to rBounds).
222 
223             @return the transformation to be applied to the sprite.
224         */
225         basegfx::B2DHomMatrix getSpriteTransformation(
226             const basegfx::B2DVector&                     rPixelSize,
227             const basegfx::B2DVector&                     rOrigSize,
228             const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
229 
230         /** Calc update area for a shape.
231 
232         	This method calculates the 'covered' area for the shape,
233         	i.e. the rectangle that is affected when rendering the
234         	shape. Apart from applying the given transformation to the
235         	shape rectangle, this method also takes attributes into
236         	account, which further scale the output (e.g. character
237         	sizes).
238 
239             @param rUnitBounds
240             Shape bounds, in the unit rect coordinate space
241 
242             @param rShapeTransform
243             Transformation matrix the shape should undergo.
244 
245             @param pAttr
246             Current shape attributes
247          */
248         basegfx::B2DRange getShapeUpdateArea(
249             const basegfx::B2DRange&                      rUnitBounds,
250             const basegfx::B2DHomMatrix&                  rShapeTransform,
251             const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
252 
253         /** Calc update area for a shape.
254 
255         	This method calculates the 'covered' area for the shape,
256         	i.e. the rectangle that is affected when rendering the
257         	shape. The difference from the other getShapeUpdateArea()
258         	method is the fact that this one works without
259         	ShapeAttributeLayer, and only scales up the given shape
260         	user coordinate bound rect. The method is typically used
261         	to retrieve user coordinate system bound rects for shapes
262         	which are smaller than the default unit bound rect
263         	(because e.g. of subsetting)
264 
265             @param rUnitBounds
266             Shape bounds, in the unit rect coordinate space
267 
268             @param rShapeBounds
269             Current shape bounding box in user coordinate space.
270          */
271         basegfx::B2DRange getShapeUpdateArea( const basegfx::B2DRange& rUnitBounds,
272                                               const basegfx::B2DRange& rShapeBounds );
273 
274         /** Calc output position and size of shape, according to given
275             attribute layer.
276 
277             Rotations, shears etc. and not taken into account,
278             i.e. the returned rectangle is NOT the bounding box. Use
279             it as if aBounds.getMinimum() is the output position and
280             aBounds.getRange() the scaling of the shape.
281          */
282         basegfx::B2DRange getShapePosSize(
283             const basegfx::B2DRange&                      rOrigBounds,
284             const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
285 
286         /** Convert a plain UNO API 32 bit int to RGBColor
287          */
288         RGBColor unoColor2RGBColor( sal_Int32 );
289         /** Convert an IntSRGBA to plain UNO API 32 bit int
290          */
291         sal_Int32 RGBAColor2UnoColor( cppcanvas::Color::IntSRGBA );
292 
293         /** Fill a plain rectangle on the given canvas with the given color
294          */
295         void fillRect( const boost::shared_ptr< cppcanvas::Canvas >& rCanvas,
296                        const basegfx::B2DRange&                      rRect,
297                        cppcanvas::Color::IntSRGBA                    aFillColor );
298 
299         /** Init canvas with default background (white)
300          */
301         void initSlideBackground( const boost::shared_ptr< cppcanvas::Canvas >& rCanvas,
302                                   const basegfx::B2IVector&                     rSize );
303 
304         /// Gets a random ordinal [0,n)
305         inline ::std::size_t getRandomOrdinal( const ::std::size_t n )
306         {
307             return static_cast< ::std::size_t >(
308                 double(n) * rand() / (RAND_MAX + 1.0) );
309         }
310 
311         /// To work around ternary operator in initializer lists
312         /// (Solaris compiler problems)
313         template <typename T>
314         inline T const & ternary_op(
315             const bool cond, T const & arg1, T const & arg2 )
316         {
317             if (cond)
318                 return arg1;
319             else
320                 return arg2;
321         }
322 
323         template <typename ValueType>
324         inline bool getPropertyValue(
325             ValueType & rValue,
326             com::sun::star::uno::Reference<
327             com::sun::star::beans::XPropertySet> const & xPropSet,
328             rtl::OUString const & propName )
329         {
330             try {
331                 const com::sun::star::uno::Any& a(
332                     xPropSet->getPropertyValue( propName ) );
333                 bool const bRet = (a >>= rValue);
334 #if OSL_DEBUG_LEVEL > 0
335                 if( !bRet )
336                     OSL_TRACE( "%s: while retrieving property %s, cannot extract Any of type %s\n",
337                                ::rtl::OUStringToOString( propName,
338                                                          RTL_TEXTENCODING_ASCII_US ).getStr(),
339                                BOOST_CURRENT_FUNCTION,
340                                ::rtl::OUStringToOString( a.getValueTypeRef()->pTypeName,
341                                                          RTL_TEXTENCODING_ASCII_US ).getStr() );
342 #endif
343                 return bRet;
344             }
345             catch (com::sun::star::uno::RuntimeException &)
346             {
347                 throw;
348             }
349             catch (com::sun::star::uno::Exception &)
350             {
351                 return false;
352             }
353         }
354 
355         template <typename ValueType>
356         inline bool getPropertyValue(
357             com::sun::star::uno::Reference< ValueType >& rIfc,
358             com::sun::star::uno::Reference<
359             com::sun::star::beans::XPropertySet> const & xPropSet,
360             rtl::OUString const & propName )
361         {
362             try
363             {
364                 const com::sun::star::uno::Any& a(
365                     xPropSet->getPropertyValue( propName ));
366                 rIfc.set( a,
367                           com::sun::star::uno::UNO_QUERY );
368 
369                 bool const bRet = rIfc.is();
370 #if OSL_DEBUG_LEVEL > 0
371                 if( !bRet )
372                     OSL_TRACE( "%s: while retrieving property %s, cannot extract Any of type %s to interface\n",
373                                ::rtl::OUStringToOString( propName,
374                                                          RTL_TEXTENCODING_ASCII_US ).getStr(),
375                                BOOST_CURRENT_FUNCTION,
376                                ::rtl::OUStringToOString( a.getValueTypeRef()->pTypeName,
377                                                          RTL_TEXTENCODING_ASCII_US ).getStr() );
378 #endif
379                 return bRet;
380             }
381             catch (com::sun::star::uno::RuntimeException &)
382             {
383                 throw;
384             }
385             catch (com::sun::star::uno::Exception &)
386             {
387                 return false;
388             }
389         }
390 
391         /// Get the content of the BoundRect shape property
392         basegfx::B2DRange getAPIShapeBounds( const ::com::sun::star::uno::Reference<
393                                                 ::com::sun::star::drawing::XShape >& xShape );
394 
395         /// Get the content of the ZOrder shape property
396         double getAPIShapePrio( const ::com::sun::star::uno::Reference<
397                                       ::com::sun::star::drawing::XShape >& xShape );
398 
399         basegfx::B2IVector getSlideSizePixel( const basegfx::B2DVector&         rSize,
400                                               const boost::shared_ptr<UnoView>& pView );
401     }
402 }
403 
404 #endif /* INCLUDED_SLIDESHOW_TOOLS_HXX */
405