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