Bump version to 4.1-6
[LibreOffice.git] / slideshow / source / inc / tools.hxx
blobd0ee0b751e2f84d690b385dea7cac282be4d10d3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_SLIDESHOW_TOOLS_HXX
21 #define INCLUDED_SLIDESHOW_TOOLS_HXX
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <cppcanvas/color.hxx>
28 #include "shapeattributelayer.hxx"
29 #include "shape.hxx"
30 #include "rgbcolor.hxx"
31 #include "hslcolor.hxx"
33 #include <boost/shared_ptr.hpp>
34 #include <boost/current_function.hpp>
36 #include <functional>
37 #include <cstdlib>
38 #include <string.h> // for strcmp
39 #include <algorithm>
43 namespace com { namespace sun { namespace star { namespace beans {
44 struct NamedValue;
45 } } } }
46 namespace basegfx
48 class B2DRange;
49 class B2DVector;
50 class B2IVector;
51 class B2DHomMatrix;
53 namespace cppcanvas{ class Canvas; }
55 class GDIMetaFile;
57 /* Definition of some animation tools */
58 namespace slideshow
60 namespace internal
62 class UnoView;
63 class Shape;
64 class ShapeAttributeLayer;
66 typedef ::boost::shared_ptr< GDIMetaFile > GDIMetaFileSharedPtr;
68 template <typename T>
69 inline ::std::size_t hash_value( T * const& p )
71 ::std::size_t d = static_cast< ::std::size_t >(
72 reinterpret_cast< ::std::ptrdiff_t >(p) );
73 return d + (d >> 3);
76 // xxx todo: remove with boost::hash when 1.33 is available
77 template <typename T>
78 struct hash : ::std::unary_function<T, ::std::size_t>
80 ::std::size_t operator()( T const& val ) const {
81 return hash_value(val);
87 namespace com { namespace sun { namespace star { namespace uno {
89 template <typename T>
90 inline ::std::size_t hash_value(
91 ::com::sun::star::uno::Reference<T> const& x )
93 // normalize to object root, because _only_ XInterface is defined
94 // to be stable during object lifetime:
95 ::com::sun::star::uno::Reference<
96 ::com::sun::star::uno::XInterface> const xRoot(
97 x, ::com::sun::star::uno::UNO_QUERY );
98 return slideshow::internal::hash<void *>()(xRoot.get());
101 } } } }
103 namespace slideshow
105 namespace internal
107 /** Cycle mode of intrinsic animations
109 enum CycleMode
111 /// loop the animation back to back
112 CYCLE_LOOP,
113 /// loop, but play backwards from end to start
114 CYCLE_PINGPONGLOOP
118 // Value extraction from Any
119 // =========================
121 /// extract unary double value from Any
122 bool extractValue( double& o_rValue,
123 const ::com::sun::star::uno::Any& rSourceAny,
124 const boost::shared_ptr<Shape>& rShape,
125 const basegfx::B2DVector& rSlideBounds );
127 /// extract int from Any
128 bool extractValue( sal_Int32& o_rValue,
129 const ::com::sun::star::uno::Any& rSourceAny,
130 const boost::shared_ptr<Shape>& rShape,
131 const basegfx::B2DVector& rSlideBounds );
133 /// extract enum/constant group value from Any
134 bool extractValue( sal_Int16& o_rValue,
135 const ::com::sun::star::uno::Any& rSourceAny,
136 const boost::shared_ptr<Shape>& rShape,
137 const basegfx::B2DVector& rSlideBounds );
139 /// extract color value from Any
140 bool extractValue( RGBColor& o_rValue,
141 const ::com::sun::star::uno::Any& rSourceAny,
142 const boost::shared_ptr<Shape>& rShape,
143 const basegfx::B2DVector& rSlideBounds );
145 /// extract color value from Any
146 bool extractValue( HSLColor& o_rValue,
147 const ::com::sun::star::uno::Any& rSourceAny,
148 const boost::shared_ptr<Shape>& rShape,
149 const basegfx::B2DVector& rSlideBounds );
151 /// extract plain string from Any
152 bool extractValue( OUString& o_rValue,
153 const ::com::sun::star::uno::Any& rSourceAny,
154 const boost::shared_ptr<Shape>& rShape,
155 const basegfx::B2DVector& rSlideBounds );
157 /// extract bool value from Any
158 bool extractValue( bool& o_rValue,
159 const ::com::sun::star::uno::Any& rSourceAny,
160 const boost::shared_ptr<Shape>& rShape,
161 const basegfx::B2DVector& rSlideBounds );
163 /// extract double 2-tuple from Any
164 bool extractValue( basegfx::B2DTuple& o_rPair,
165 const ::com::sun::star::uno::Any& rSourceAny,
166 const boost::shared_ptr<Shape>& rShape,
167 const basegfx::B2DVector& rSlideBounds );
169 /** Search a sequence of NamedValues for a given element.
171 @return true, if the sequence contains the specified
172 element.
174 bool findNamedValue( ::com::sun::star::uno::Sequence<
175 ::com::sun::star::beans::NamedValue > const& rSequence,
176 const ::com::sun::star::beans::NamedValue& rSearchKey );
178 basegfx::B2DRange calcRelativeShapeBounds( const basegfx::B2DVector& rPageSize,
179 const basegfx::B2DRange& rShapeBounds );
181 /** Get the shape transformation from the attribute set
183 @param rBounds
184 Original shape bound rect (to substitute default attribute
185 layer values)
187 @param pAttr
188 Attribute set. Might be NULL (then, rBounds is used to set
189 a simple scale and translate of the unit rect to rBounds).
191 basegfx::B2DHomMatrix getShapeTransformation(
192 const basegfx::B2DRange& rBounds,
193 const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
195 /** Get a shape's sprite transformation from the attribute set
197 @param rPixelSize
198 Pixel size of the sprite
200 @param rOrigSize
201 Original shape size (i.e. the size of the actual sprite
202 content, in the user coordinate system)
204 @param pAttr
205 Attribute set. Might be NULL (then, rBounds is used to set
206 a simple scale and translate of the unit rect to rBounds).
208 @return the transformation to be applied to the sprite.
210 basegfx::B2DHomMatrix getSpriteTransformation(
211 const basegfx::B2DVector& rPixelSize,
212 const basegfx::B2DVector& rOrigSize,
213 const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
215 /** Calc update area for a shape.
217 This method calculates the 'covered' area for the shape,
218 i.e. the rectangle that is affected when rendering the
219 shape. Apart from applying the given transformation to the
220 shape rectangle, this method also takes attributes into
221 account, which further scale the output (e.g. character
222 sizes).
224 @param rUnitBounds
225 Shape bounds, in the unit rect coordinate space
227 @param rShapeTransform
228 Transformation matrix the shape should undergo.
230 @param pAttr
231 Current shape attributes
233 basegfx::B2DRange getShapeUpdateArea(
234 const basegfx::B2DRange& rUnitBounds,
235 const basegfx::B2DHomMatrix& rShapeTransform,
236 const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
238 /** Calc update area for a shape.
240 This method calculates the 'covered' area for the shape,
241 i.e. the rectangle that is affected when rendering the
242 shape. The difference from the other getShapeUpdateArea()
243 method is the fact that this one works without
244 ShapeAttributeLayer, and only scales up the given shape
245 user coordinate bound rect. The method is typically used
246 to retrieve user coordinate system bound rects for shapes
247 which are smaller than the default unit bound rect
248 (because e.g. of subsetting)
250 @param rUnitBounds
251 Shape bounds, in the unit rect coordinate space
253 @param rShapeBounds
254 Current shape bounding box in user coordinate space.
256 basegfx::B2DRange getShapeUpdateArea( const basegfx::B2DRange& rUnitBounds,
257 const basegfx::B2DRange& rShapeBounds );
259 /** Calc output position and size of shape, according to given
260 attribute layer.
262 Rotations, shears etc. and not taken into account,
263 i.e. the returned rectangle is NOT the bounding box. Use
264 it as if aBounds.getMinimum() is the output position and
265 aBounds.getRange() the scaling of the shape.
267 basegfx::B2DRange getShapePosSize(
268 const basegfx::B2DRange& rOrigBounds,
269 const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
271 /** Convert a plain UNO API 32 bit int to RGBColor
273 RGBColor unoColor2RGBColor( sal_Int32 );
274 /** Convert an IntSRGBA to plain UNO API 32 bit int
276 sal_Int32 RGBAColor2UnoColor( cppcanvas::Color::IntSRGBA );
278 /** Fill a plain rectangle on the given canvas with the given color
280 void fillRect( const boost::shared_ptr< cppcanvas::Canvas >& rCanvas,
281 const basegfx::B2DRange& rRect,
282 cppcanvas::Color::IntSRGBA aFillColor );
284 /** Init canvas with default background (white)
286 void initSlideBackground( const boost::shared_ptr< cppcanvas::Canvas >& rCanvas,
287 const basegfx::B2IVector& rSize );
289 /// Gets a random ordinal [0,n)
290 inline ::std::size_t getRandomOrdinal( const ::std::size_t n )
292 return static_cast< ::std::size_t >(
293 double(n) * rand() / (RAND_MAX + 1.0) );
296 /// To work around ternary operator in initializer lists
297 /// (Solaris compiler problems)
298 template <typename T>
299 inline T const & ternary_op(
300 const bool cond, T const & arg1, T const & arg2 )
302 if (cond)
303 return arg1;
304 else
305 return arg2;
308 template <typename ValueType>
309 inline bool getPropertyValue(
310 ValueType & rValue,
311 com::sun::star::uno::Reference<
312 com::sun::star::beans::XPropertySet> const & xPropSet,
313 OUString const & propName )
315 try {
316 const com::sun::star::uno::Any& a(
317 xPropSet->getPropertyValue( propName ) );
318 bool const bRet = (a >>= rValue);
319 #if OSL_DEBUG_LEVEL > 0
320 if( !bRet )
321 OSL_TRACE( "%s: while retrieving property %s, cannot extract Any of type %s\n",
322 OUStringToOString( propName,
323 RTL_TEXTENCODING_ASCII_US ).getStr(),
324 BOOST_CURRENT_FUNCTION,
325 OUStringToOString( a.getValueTypeRef()->pTypeName,
326 RTL_TEXTENCODING_ASCII_US ).getStr() );
327 #endif
328 return bRet;
330 catch (com::sun::star::uno::RuntimeException &)
332 throw;
334 catch (com::sun::star::uno::Exception &)
336 return false;
340 template <typename ValueType>
341 inline bool getPropertyValue(
342 com::sun::star::uno::Reference< ValueType >& rIfc,
343 com::sun::star::uno::Reference<
344 com::sun::star::beans::XPropertySet> const & xPropSet,
345 OUString const & propName )
349 const com::sun::star::uno::Any& a(
350 xPropSet->getPropertyValue( propName ));
351 rIfc.set( a,
352 com::sun::star::uno::UNO_QUERY );
354 bool const bRet = rIfc.is();
355 #if OSL_DEBUG_LEVEL > 0
356 if( !bRet )
357 OSL_TRACE( "%s: while retrieving property %s, cannot extract Any of type %s to interface\n",
358 OUStringToOString( propName,
359 RTL_TEXTENCODING_ASCII_US ).getStr(),
360 BOOST_CURRENT_FUNCTION,
361 OUStringToOString( a.getValueTypeRef()->pTypeName,
362 RTL_TEXTENCODING_ASCII_US ).getStr() );
363 #endif
364 return bRet;
366 catch (com::sun::star::uno::RuntimeException &)
368 throw;
370 catch (com::sun::star::uno::Exception &)
372 return false;
376 /// Get the content of the BoundRect shape property
377 basegfx::B2DRange getAPIShapeBounds( const ::com::sun::star::uno::Reference<
378 ::com::sun::star::drawing::XShape >& xShape );
381 TODO(F1): When ZOrder someday becomes usable enable this
383 /// Get the content of the ZOrder shape property
384 double getAPIShapePrio( const ::com::sun::star::uno::Reference<
385 ::com::sun::star::drawing::XShape >& xShape );
388 basegfx::B2IVector getSlideSizePixel( const basegfx::B2DVector& rSize,
389 const boost::shared_ptr<UnoView>& pView );
393 #endif /* INCLUDED_SLIDESHOW_TOOLS_HXX */
395 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */