1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tools.hxx,v $
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"
41 #include "rgbcolor.hxx"
42 #include "hslcolor.hxx"
44 #include <boost/shared_ptr.hpp>
45 #include <boost/current_function.hpp>
49 #include <string.h> // for strcmp
53 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
56 namespace com
{ namespace sun
{ namespace star
{ namespace beans
{
66 namespace cppcanvas
{ class Canvas
; }
70 /* Definition of some animation tools */
77 class ShapeAttributeLayer
;
79 typedef ::boost::shared_ptr
< GDIMetaFile
> GDIMetaFileSharedPtr
;
81 // xxx todo: remove with boost::hash when 1.33 is available
83 struct hash
: ::std::unary_function
<T
, ::std::size_t>
85 ::std::size_t operator()( T
const& val
) const {
86 return hash_value(val
);
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
) );
97 // xxx todo: shift to namespace com::sun::star::uno when
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
115 /// loop the animation back to back
117 /// loop, but play backwards from end to start
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
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.
185 If non-NULL, receives the full NamedValue found (if it was
188 @return true, if the sequence contains the specified
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
202 Original shape bound rect (to substitute default attribute
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
216 Pixel size of the sprite
219 Original shape size (i.e. the size of the actual sprite
220 content, in the user coordinate system)
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
243 Shape bounds, in the unit rect coordinate space
245 @param rShapeTransform
246 Transformation matrix the shape should undergo.
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)
269 Shape bounds, in the unit rect coordinate space
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
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
)
323 template <typename ValueType
>
324 inline bool getPropertyValue(
326 com::sun::star::uno::Reference
<
327 com::sun::star::beans::XPropertySet
> const & xPropSet
,
328 rtl::OUString
const & propName
)
331 const com::sun::star::uno::Any
& a(
332 xPropSet
->getPropertyValue( propName
) );
333 bool const bRet
= (a
>>= rValue
);
334 #if OSL_DEBUG_LEVEL > 0
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() );
345 catch (com::sun::star::uno::RuntimeException
&)
349 catch (com::sun::star::uno::Exception
&)
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
)
364 const com::sun::star::uno::Any
& a(
365 xPropSet
->getPropertyValue( propName
));
367 com::sun::star::uno::UNO_QUERY
);
369 bool const bRet
= rIfc
.is();
370 #if OSL_DEBUG_LEVEL > 0
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() );
381 catch (com::sun::star::uno::RuntimeException
&)
385 catch (com::sun::star::uno::Exception
&)
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 */