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