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