merge the formfield patch from ooo-build
[ooovba.git] / slideshow / source / inc / tools.hxx
bloba438216014809556a804a0b5a2c2d7c07a6cbeee
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tools.hxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef INCLUDED_SLIDESHOW_TOOLS_HXX
32 #define INCLUDED_SLIDESHOW_TOOLS_HXX
34 #include <com/sun/star/uno/Sequence.hxx>
35 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <cppcanvas/color.hxx>
39 #include "shapeattributelayer.hxx"
40 #include "shape.hxx"
41 #include "rgbcolor.hxx"
42 #include "hslcolor.hxx"
44 #include <boost/shared_ptr.hpp>
45 #include <boost/current_function.hpp>
47 #include <functional>
48 #include <cstdlib>
49 #include <string.h> // for strcmp
50 #include <algorithm>
53 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
56 namespace com { namespace sun { namespace star { namespace beans {
57 struct NamedValue;
58 } } } }
59 namespace basegfx
61 class B2DRange;
62 class B2DVector;
63 class B2IVector;
64 class B2DHomMatrix;
66 namespace cppcanvas{ class Canvas; }
68 class GDIMetaFile;
70 /* Definition of some animation tools */
71 namespace slideshow
73 namespace internal
75 class UnoView;
76 class Shape;
77 class ShapeAttributeLayer;
79 typedef ::boost::shared_ptr< GDIMetaFile > GDIMetaFileSharedPtr;
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>
85 ::std::size_t operator()( T const& val ) const {
86 return hash_value(val);
89 template <typename T>
90 inline ::std::size_t hash_value( T * const& p )
92 ::std::size_t d = static_cast< ::std::size_t >(
93 reinterpret_cast< ::std::ptrdiff_t >(p) );
94 return d + (d >> 3);
97 // xxx todo: shift to namespace com::sun::star::uno when
98 // 1.33 is available
99 template <typename T>
100 inline ::std::size_t hash_value(
101 ::com::sun::star::uno::Reference<T> const& x )
103 // normalize to object root, because _only_ XInterface is defined
104 // to be stable during object lifetime:
105 ::com::sun::star::uno::Reference<
106 ::com::sun::star::uno::XInterface> const xRoot(
107 x, ::com::sun::star::uno::UNO_QUERY );
108 return hash<void *>()(xRoot.get());
111 /** Cycle mode of intrinsic animations
113 enum CycleMode
115 /// loop the animation back to back
116 CYCLE_LOOP,
117 /// loop, but play backwards from end to start
118 CYCLE_PINGPONGLOOP
122 // Value extraction from Any
123 // =========================
125 /// extract unary double value from Any
126 bool extractValue( double& o_rValue,
127 const ::com::sun::star::uno::Any& rSourceAny,
128 const boost::shared_ptr<Shape>& rShape,
129 const basegfx::B2DVector& rSlideBounds );
131 /// extract int from Any
132 bool extractValue( sal_Int32& o_rValue,
133 const ::com::sun::star::uno::Any& rSourceAny,
134 const boost::shared_ptr<Shape>& rShape,
135 const basegfx::B2DVector& rSlideBounds );
137 /// extract enum/constant group value from Any
138 bool extractValue( sal_Int16& o_rValue,
139 const ::com::sun::star::uno::Any& rSourceAny,
140 const boost::shared_ptr<Shape>& rShape,
141 const basegfx::B2DVector& rSlideBounds );
143 /// extract color value from Any
144 bool extractValue( RGBColor& o_rValue,
145 const ::com::sun::star::uno::Any& rSourceAny,
146 const boost::shared_ptr<Shape>& rShape,
147 const basegfx::B2DVector& rSlideBounds );
149 /// extract color value from Any
150 bool extractValue( HSLColor& o_rValue,
151 const ::com::sun::star::uno::Any& rSourceAny,
152 const boost::shared_ptr<Shape>& rShape,
153 const basegfx::B2DVector& rSlideBounds );
155 /// extract plain string from Any
156 bool extractValue( ::rtl::OUString& o_rValue,
157 const ::com::sun::star::uno::Any& rSourceAny,
158 const boost::shared_ptr<Shape>& rShape,
159 const basegfx::B2DVector& rSlideBounds );
161 /// extract bool value from Any
162 bool extractValue( bool& o_rValue,
163 const ::com::sun::star::uno::Any& rSourceAny,
164 const boost::shared_ptr<Shape>& rShape,
165 const basegfx::B2DVector& rSlideBounds );
167 /// extract double 2-tuple from Any
168 bool extractValue( basegfx::B2DTuple& o_rPair,
169 const ::com::sun::star::uno::Any& rSourceAny,
170 const boost::shared_ptr<Shape>& rShape,
171 const basegfx::B2DVector& rSlideBounds );
173 /** Search a sequence of NamedValues for a given element.
175 @return true, if the sequence contains the specified
176 element.
178 bool findNamedValue( ::com::sun::star::uno::Sequence<
179 ::com::sun::star::beans::NamedValue > const& rSequence,
180 const ::com::sun::star::beans::NamedValue& rSearchKey );
182 /** Search a sequence of NamedValues for an element with a given name.
184 @param o_pRet
185 If non-NULL, receives the full NamedValue found (if it was
186 found, that is).
188 @return true, if the sequence contains the specified
189 element.
191 bool findNamedValue( ::com::sun::star::beans::NamedValue* o_pRet,
192 const ::com::sun::star::uno::Sequence<
193 ::com::sun::star::beans::NamedValue >& rSequence,
194 const ::rtl::OUString& rSearchString );
196 basegfx::B2DRange calcRelativeShapeBounds( const basegfx::B2DVector& rPageSize,
197 const basegfx::B2DRange& rShapeBounds );
199 /** Get the shape transformation from the attribute set
201 @param rBounds
202 Original shape bound rect (to substitute default attribute
203 layer values)
205 @param pAttr
206 Attribute set. Might be NULL (then, rBounds is used to set
207 a simple scale and translate of the unit rect to rBounds).
209 basegfx::B2DHomMatrix getShapeTransformation(
210 const basegfx::B2DRange& rBounds,
211 const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
213 /** Get a shape's sprite transformation from the attribute set
215 @param rPixelSize
216 Pixel size of the sprite
218 @param rOrigSize
219 Original shape size (i.e. the size of the actual sprite
220 content, in the user coordinate system)
222 @param pAttr
223 Attribute set. Might be NULL (then, rBounds is used to set
224 a simple scale and translate of the unit rect to rBounds).
226 @return the transformation to be applied to the sprite.
228 basegfx::B2DHomMatrix getSpriteTransformation(
229 const basegfx::B2DVector& rPixelSize,
230 const basegfx::B2DVector& rOrigSize,
231 const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
233 /** Calc update area for a shape.
235 This method calculates the 'covered' area for the shape,
236 i.e. the rectangle that is affected when rendering the
237 shape. Apart from applying the given transformation to the
238 shape rectangle, this method also takes attributes into
239 account, which further scale the output (e.g. character
240 sizes).
242 @param rUnitBounds
243 Shape bounds, in the unit rect coordinate space
245 @param rShapeTransform
246 Transformation matrix the shape should undergo.
248 @param pAttr
249 Current shape attributes
251 basegfx::B2DRange getShapeUpdateArea(
252 const basegfx::B2DRange& rUnitBounds,
253 const basegfx::B2DHomMatrix& rShapeTransform,
254 const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
256 /** Calc update area for a shape.
258 This method calculates the 'covered' area for the shape,
259 i.e. the rectangle that is affected when rendering the
260 shape. The difference from the other getShapeUpdateArea()
261 method is the fact that this one works without
262 ShapeAttributeLayer, and only scales up the given shape
263 user coordinate bound rect. The method is typically used
264 to retrieve user coordinate system bound rects for shapes
265 which are smaller than the default unit bound rect
266 (because e.g. of subsetting)
268 @param rUnitBounds
269 Shape bounds, in the unit rect coordinate space
271 @param rShapeBounds
272 Current shape bounding box in user coordinate space.
274 basegfx::B2DRange getShapeUpdateArea( const basegfx::B2DRange& rUnitBounds,
275 const basegfx::B2DRange& rShapeBounds );
277 /** Calc output position and size of shape, according to given
278 attribute layer.
280 Rotations, shears etc. and not taken into account,
281 i.e. the returned rectangle is NOT the bounding box. Use
282 it as if aBounds.getMinimum() is the output position and
283 aBounds.getRange() the scaling of the shape.
285 basegfx::B2DRange getShapePosSize(
286 const basegfx::B2DRange& rOrigBounds,
287 const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
289 /** Convert a plain UNO API 32 bit int to RGBColor
291 RGBColor unoColor2RGBColor( sal_Int32 );
293 /** Fill a plain rectangle on the given canvas with the given color
295 void fillRect( const boost::shared_ptr< cppcanvas::Canvas >& rCanvas,
296 const basegfx::B2DRange& rRect,
297 cppcanvas::Color::IntSRGBA aFillColor );
299 /** Init canvas with default background (white)
301 void initSlideBackground( const boost::shared_ptr< cppcanvas::Canvas >& rCanvas,
302 const basegfx::B2IVector& rSize );
304 /// Gets a random ordinal [0,n)
305 inline ::std::size_t getRandomOrdinal( const ::std::size_t n )
307 return static_cast< ::std::size_t >(
308 double(n) * rand() / (RAND_MAX + 1.0) );
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 )
317 if (cond)
318 return arg1;
319 else
320 return arg2;
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 )
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;
345 catch (com::sun::star::uno::RuntimeException &)
347 throw;
349 catch (com::sun::star::uno::Exception &)
351 return false;
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 )
362 try
364 const com::sun::star::uno::Any& a(
365 xPropSet->getPropertyValue( propName ));
366 rIfc.set( a,
367 com::sun::star::uno::UNO_QUERY );
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;
381 catch (com::sun::star::uno::RuntimeException &)
383 throw;
385 catch (com::sun::star::uno::Exception &)
387 return false;
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 );
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 );
399 basegfx::B2IVector getSlideSizePixel( const basegfx::B2DVector& rSize,
400 const boost::shared_ptr<UnoView>& pView );
404 #endif /* INCLUDED_SLIDESHOW_TOOLS_HXX */