fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / slideshow / source / inc / tools.hxx
blobe1eefd7f1923152540790c4ab88c700e31c5f2b6
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_SOURCE_INC_TOOLS_HXX
21 #define INCLUDED_SLIDESHOW_SOURCE_INC_TOOLS_HXX
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/uno/Reference.hxx>
26 #include <com/sun/star/animations/XAnimationNode.hpp>
27 #include <com/sun/star/container/XEnumerationAccess.hpp>
28 #include <com/sun/star/container/XEnumeration.hpp>
29 #include <comphelper/random.hxx>
30 #include <vector>
32 #include <cppcanvas/color.hxx>
34 #include "shapeattributelayer.hxx"
35 #include "shape.hxx"
36 #include "rgbcolor.hxx"
37 #include "hslcolor.hxx"
39 #include <boost/shared_ptr.hpp>
40 #include <boost/current_function.hpp>
42 #include <functional>
43 #include <cstdlib>
44 #include <string.h>
45 #include <algorithm>
47 namespace com { namespace sun { namespace star { namespace beans {
48 struct NamedValue;
49 } } } }
50 namespace basegfx
52 class B2DRange;
53 class B2DVector;
54 class B2IVector;
55 class B2DHomMatrix;
57 namespace cppcanvas{ class Canvas; }
59 class GDIMetaFile;
61 /* Definition of some animation tools */
62 namespace slideshow
64 namespace internal
66 class UnoView;
67 class Shape;
68 class ShapeAttributeLayer;
70 typedef ::boost::shared_ptr< GDIMetaFile > GDIMetaFileSharedPtr;
72 template <typename T>
73 inline ::std::size_t hash_value( T const * p )
75 ::std::size_t d = static_cast< ::std::size_t >(
76 reinterpret_cast< ::std::ptrdiff_t >(p) );
77 return d + (d >> 3);
80 // xxx todo: remove with boost::hash when 1.33 is available
81 template <typename T>
82 struct hash : ::std::unary_function<T, ::std::size_t>
84 ::std::size_t operator()( T const& val ) const {
85 return hash_value(val);
91 namespace com { namespace sun { namespace star { namespace uno {
93 template <typename T>
94 inline ::std::size_t hash_value(
95 ::com::sun::star::uno::Reference<T> const& x )
97 // normalize to object root, because _only_ XInterface is defined
98 // to be stable during object lifetime:
99 ::com::sun::star::uno::Reference<
100 ::com::sun::star::uno::XInterface> const xRoot(
101 x, ::com::sun::star::uno::UNO_QUERY );
102 return slideshow::internal::hash<void *>()(xRoot.get());
105 } } } }
107 namespace slideshow
109 namespace internal
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( 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 basegfx::B2DRange calcRelativeShapeBounds( const basegfx::B2DVector& rPageSize,
183 const basegfx::B2DRange& rShapeBounds );
185 /** Get the shape transformation from the attribute set
187 @param rBounds
188 Original shape bound rect (to substitute default attribute
189 layer values)
191 @param pAttr
192 Attribute set. Might be NULL (then, rBounds is used to set
193 a simple scale and translate of the unit rect to rBounds).
195 basegfx::B2DHomMatrix getShapeTransformation(
196 const basegfx::B2DRange& rBounds,
197 const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
199 /** Get a shape's sprite transformation from the attribute set
201 @param rPixelSize
202 Pixel size of the sprite
204 @param rOrigSize
205 Original shape size (i.e. the size of the actual sprite
206 content, in the user coordinate system)
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).
212 @return the transformation to be applied to the sprite.
214 basegfx::B2DHomMatrix getSpriteTransformation(
215 const basegfx::B2DVector& rPixelSize,
216 const basegfx::B2DVector& rOrigSize,
217 const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
219 /** Calc update area for a shape.
221 This method calculates the 'covered' area for the shape,
222 i.e. the rectangle that is affected when rendering the
223 shape. Apart from applying the given transformation to the
224 shape rectangle, this method also takes attributes into
225 account, which further scale the output (e.g. character
226 sizes).
228 @param rUnitBounds
229 Shape bounds, in the unit rect coordinate space
231 @param rShapeTransform
232 Transformation matrix the shape should undergo.
234 @param pAttr
235 Current shape attributes
237 basegfx::B2DRange getShapeUpdateArea(
238 const basegfx::B2DRange& rUnitBounds,
239 const basegfx::B2DHomMatrix& rShapeTransform,
240 const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
242 /** Calc update area for a shape.
244 This method calculates the 'covered' area for the shape,
245 i.e. the rectangle that is affected when rendering the
246 shape. The difference from the other getShapeUpdateArea()
247 method is the fact that this one works without
248 ShapeAttributeLayer, and only scales up the given shape
249 user coordinate bound rect. The method is typically used
250 to retrieve user coordinate system bound rects for shapes
251 which are smaller than the default unit bound rect
252 (because e.g. of subsetting)
254 @param rUnitBounds
255 Shape bounds, in the unit rect coordinate space
257 @param rShapeBounds
258 Current shape bounding box in user coordinate space.
260 basegfx::B2DRange getShapeUpdateArea( const basegfx::B2DRange& rUnitBounds,
261 const basegfx::B2DRange& rShapeBounds );
263 /** Calc output position and size of shape, according to given
264 attribute layer.
266 Rotations, shears etc. and not taken into account,
267 i.e. the returned rectangle is NOT the bounding box. Use
268 it as if aBounds.getMinimum() is the output position and
269 aBounds.getRange() the scaling of the shape.
271 basegfx::B2DRange getShapePosSize(
272 const basegfx::B2DRange& rOrigBounds,
273 const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
275 /** Convert a plain UNO API 32 bit int to RGBColor
277 RGBColor unoColor2RGBColor( sal_Int32 );
278 /** Convert an IntSRGBA to plain UNO API 32 bit int
280 sal_Int32 RGBAColor2UnoColor( cppcanvas::Color::IntSRGBA );
282 /** Fill a plain rectangle on the given canvas with the given color
284 void fillRect( const boost::shared_ptr< cppcanvas::Canvas >& rCanvas,
285 const basegfx::B2DRange& rRect,
286 cppcanvas::Color::IntSRGBA aFillColor );
288 /** Init canvas with default background (white)
290 void initSlideBackground( const boost::shared_ptr< cppcanvas::Canvas >& rCanvas,
291 const basegfx::B2IVector& rSize );
293 /// Gets a random ordinal [0,n)
294 inline ::std::size_t getRandomOrdinal( const ::std::size_t n )
296 return comphelper::rng::uniform_size_distribution(0, n-1);
299 /// To work around ternary operator in initializer lists
300 /// (Solaris compiler problems)
301 template <typename T>
302 inline T const & ternary_op(
303 const bool cond, T const & arg1, T const & arg2 )
305 if (cond)
306 return arg1;
307 else
308 return arg2;
311 template <typename ValueType>
312 inline bool getPropertyValue(
313 ValueType & rValue,
314 com::sun::star::uno::Reference<
315 com::sun::star::beans::XPropertySet> const & xPropSet,
316 OUString const & propName )
318 try {
319 const com::sun::star::uno::Any& a(
320 xPropSet->getPropertyValue( propName ) );
321 bool const bRet = (a >>= rValue);
322 #if OSL_DEBUG_LEVEL > 0
323 if( !bRet )
324 OSL_TRACE( "%s: while retrieving property %s, cannot extract Any of type %s\n",
325 OUStringToOString( propName,
326 RTL_TEXTENCODING_ASCII_US ).getStr(),
327 BOOST_CURRENT_FUNCTION,
328 OUStringToOString( a.getValueTypeRef()->pTypeName,
329 RTL_TEXTENCODING_ASCII_US ).getStr() );
330 #endif
331 return bRet;
333 catch (com::sun::star::uno::RuntimeException &)
335 throw;
337 catch (com::sun::star::uno::Exception &)
339 return false;
343 template <typename ValueType>
344 inline bool getPropertyValue(
345 com::sun::star::uno::Reference< ValueType >& rIfc,
346 com::sun::star::uno::Reference<
347 com::sun::star::beans::XPropertySet> const & xPropSet,
348 OUString const & propName )
352 const com::sun::star::uno::Any& a(
353 xPropSet->getPropertyValue( propName ));
354 rIfc.set( a,
355 com::sun::star::uno::UNO_QUERY );
357 bool const bRet = rIfc.is();
358 #if OSL_DEBUG_LEVEL > 0
359 if( !bRet )
360 OSL_TRACE( "%s: while retrieving property %s, cannot extract Any of type %s to interface\n",
361 OUStringToOString( propName,
362 RTL_TEXTENCODING_ASCII_US ).getStr(),
363 BOOST_CURRENT_FUNCTION,
364 OUStringToOString( a.getValueTypeRef()->pTypeName,
365 RTL_TEXTENCODING_ASCII_US ).getStr() );
366 #endif
367 return bRet;
369 catch (com::sun::star::uno::RuntimeException &)
371 throw;
373 catch (com::sun::star::uno::Exception &)
375 return false;
379 /// Get the content of the BoundRect shape property
380 basegfx::B2DRange getAPIShapeBounds( const ::com::sun::star::uno::Reference<
381 ::com::sun::star::drawing::XShape >& xShape );
384 TODO(F1): When ZOrder someday becomes usable enable this
386 /// Get the content of the ZOrder shape property
387 double getAPIShapePrio( const ::com::sun::star::uno::Reference<
388 ::com::sun::star::drawing::XShape >& xShape );
391 basegfx::B2IVector getSlideSizePixel( const basegfx::B2DVector& rSize,
392 const boost::shared_ptr<UnoView>& pView );
395 // TODO(Q1): this could possibly be implemented with a somewhat
396 // more lightweight template, by having the actual worker receive
397 // only a function pointer, and a thin templated wrapper around
398 // that which converts member functions into that.
400 /** Apply given functor to every animation node child.
402 @param xNode
403 Parent node
405 @param rFunctor
406 Functor to apply. The functor must have an appropriate
407 operator()( const ::com::sun::star::uno::Reference<
408 ::com::sun::star::animations::XAnimationNode >& ) member.
410 @return true, if the functor was successfully applied to
411 all children, false otherwise.
413 template< typename Functor > inline bool for_each_childNode( const ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >& xNode,
414 Functor& rFunctor )
418 // get an XEnumerationAccess to the children
419 ::com::sun::star::uno::Reference< ::com::sun::star::container::XEnumerationAccess >
420 xEnumerationAccess( xNode,
421 ::com::sun::star::uno::UNO_QUERY_THROW );
422 ::com::sun::star::uno::Reference< ::com::sun::star::container::XEnumeration >
423 xEnumeration( xEnumerationAccess->createEnumeration(),
424 ::com::sun::star::uno::UNO_QUERY_THROW );
426 while( xEnumeration->hasMoreElements() )
428 ::com::sun::star::uno::Reference< ::com::sun::star::animations::XAnimationNode >
429 xChildNode( xEnumeration->nextElement(),
430 ::com::sun::star::uno::UNO_QUERY_THROW );
431 rFunctor( xChildNode );
433 return true;
435 catch( ::com::sun::star::uno::Exception& )
437 return false;
442 #endif // INCLUDED_SLIDESHOW_SOURCE_INC_TOOLS_HXX
444 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */