Impress Remote 1.0.5, tag sdremote-1.0.5
[LibreOffice.git] / slideshow / source / inc / tools.hxx
blob15d723b09fa227193c879719482949c20e6e6143
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>
42 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
45 namespace com { namespace sun { namespace star { namespace beans {
46 struct NamedValue;
47 } } } }
48 namespace basegfx
50 class B2DRange;
51 class B2DVector;
52 class B2IVector;
53 class B2DHomMatrix;
55 namespace cppcanvas{ class Canvas; }
57 class GDIMetaFile;
59 /* Definition of some animation tools */
60 namespace slideshow
62 namespace internal
64 class UnoView;
65 class Shape;
66 class ShapeAttributeLayer;
68 typedef ::boost::shared_ptr< GDIMetaFile > GDIMetaFileSharedPtr;
70 template <typename T>
71 inline ::std::size_t hash_value( T * const& p )
73 ::std::size_t d = static_cast< ::std::size_t >(
74 reinterpret_cast< ::std::ptrdiff_t >(p) );
75 return d + (d >> 3);
78 // xxx todo: remove with boost::hash when 1.33 is available
79 template <typename T>
80 struct hash : ::std::unary_function<T, ::std::size_t>
82 ::std::size_t operator()( T const& val ) const {
83 return hash_value(val);
89 namespace com { namespace sun { namespace star { namespace uno {
91 template <typename T>
92 inline ::std::size_t hash_value(
93 ::com::sun::star::uno::Reference<T> const& x )
95 // normalize to object root, because _only_ XInterface is defined
96 // to be stable during object lifetime:
97 ::com::sun::star::uno::Reference<
98 ::com::sun::star::uno::XInterface> const xRoot(
99 x, ::com::sun::star::uno::UNO_QUERY );
100 return slideshow::internal::hash<void *>()(xRoot.get());
103 } } } }
105 namespace slideshow
107 namespace internal
109 /** Cycle mode of intrinsic animations
111 enum CycleMode
113 /// loop the animation back to back
114 CYCLE_LOOP,
115 /// loop, but play backwards from end to start
116 CYCLE_PINGPONGLOOP
120 // Value extraction from Any
121 // =========================
123 /// extract unary double value from Any
124 bool extractValue( double& o_rValue,
125 const ::com::sun::star::uno::Any& rSourceAny,
126 const boost::shared_ptr<Shape>& rShape,
127 const basegfx::B2DVector& rSlideBounds );
129 /// extract int from Any
130 bool extractValue( sal_Int32& o_rValue,
131 const ::com::sun::star::uno::Any& rSourceAny,
132 const boost::shared_ptr<Shape>& rShape,
133 const basegfx::B2DVector& rSlideBounds );
135 /// extract enum/constant group value from Any
136 bool extractValue( sal_Int16& o_rValue,
137 const ::com::sun::star::uno::Any& rSourceAny,
138 const boost::shared_ptr<Shape>& rShape,
139 const basegfx::B2DVector& rSlideBounds );
141 /// extract color value from Any
142 bool extractValue( RGBColor& o_rValue,
143 const ::com::sun::star::uno::Any& rSourceAny,
144 const boost::shared_ptr<Shape>& rShape,
145 const basegfx::B2DVector& rSlideBounds );
147 /// extract color value from Any
148 bool extractValue( HSLColor& o_rValue,
149 const ::com::sun::star::uno::Any& rSourceAny,
150 const boost::shared_ptr<Shape>& rShape,
151 const basegfx::B2DVector& rSlideBounds );
153 /// extract plain string from Any
154 bool extractValue( ::rtl::OUString& o_rValue,
155 const ::com::sun::star::uno::Any& rSourceAny,
156 const boost::shared_ptr<Shape>& rShape,
157 const basegfx::B2DVector& rSlideBounds );
159 /// extract bool value from Any
160 bool extractValue( bool& o_rValue,
161 const ::com::sun::star::uno::Any& rSourceAny,
162 const boost::shared_ptr<Shape>& rShape,
163 const basegfx::B2DVector& rSlideBounds );
165 /// extract double 2-tuple from Any
166 bool extractValue( basegfx::B2DTuple& o_rPair,
167 const ::com::sun::star::uno::Any& rSourceAny,
168 const boost::shared_ptr<Shape>& rShape,
169 const basegfx::B2DVector& rSlideBounds );
171 /** Search a sequence of NamedValues for a given element.
173 @return true, if the sequence contains the specified
174 element.
176 bool findNamedValue( ::com::sun::star::uno::Sequence<
177 ::com::sun::star::beans::NamedValue > const& rSequence,
178 const ::com::sun::star::beans::NamedValue& rSearchKey );
180 basegfx::B2DRange calcRelativeShapeBounds( const basegfx::B2DVector& rPageSize,
181 const basegfx::B2DRange& rShapeBounds );
183 /** Get the shape transformation from the attribute set
185 @param rBounds
186 Original shape bound rect (to substitute default attribute
187 layer values)
189 @param pAttr
190 Attribute set. Might be NULL (then, rBounds is used to set
191 a simple scale and translate of the unit rect to rBounds).
193 basegfx::B2DHomMatrix getShapeTransformation(
194 const basegfx::B2DRange& rBounds,
195 const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
197 /** Get a shape's sprite transformation from the attribute set
199 @param rPixelSize
200 Pixel size of the sprite
202 @param rOrigSize
203 Original shape size (i.e. the size of the actual sprite
204 content, in the user coordinate system)
206 @param pAttr
207 Attribute set. Might be NULL (then, rBounds is used to set
208 a simple scale and translate of the unit rect to rBounds).
210 @return the transformation to be applied to the sprite.
212 basegfx::B2DHomMatrix getSpriteTransformation(
213 const basegfx::B2DVector& rPixelSize,
214 const basegfx::B2DVector& rOrigSize,
215 const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
217 /** Calc update area for a shape.
219 This method calculates the 'covered' area for the shape,
220 i.e. the rectangle that is affected when rendering the
221 shape. Apart from applying the given transformation to the
222 shape rectangle, this method also takes attributes into
223 account, which further scale the output (e.g. character
224 sizes).
226 @param rUnitBounds
227 Shape bounds, in the unit rect coordinate space
229 @param rShapeTransform
230 Transformation matrix the shape should undergo.
232 @param pAttr
233 Current shape attributes
235 basegfx::B2DRange getShapeUpdateArea(
236 const basegfx::B2DRange& rUnitBounds,
237 const basegfx::B2DHomMatrix& rShapeTransform,
238 const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
240 /** Calc update area for a shape.
242 This method calculates the 'covered' area for the shape,
243 i.e. the rectangle that is affected when rendering the
244 shape. The difference from the other getShapeUpdateArea()
245 method is the fact that this one works without
246 ShapeAttributeLayer, and only scales up the given shape
247 user coordinate bound rect. The method is typically used
248 to retrieve user coordinate system bound rects for shapes
249 which are smaller than the default unit bound rect
250 (because e.g. of subsetting)
252 @param rUnitBounds
253 Shape bounds, in the unit rect coordinate space
255 @param rShapeBounds
256 Current shape bounding box in user coordinate space.
258 basegfx::B2DRange getShapeUpdateArea( const basegfx::B2DRange& rUnitBounds,
259 const basegfx::B2DRange& rShapeBounds );
261 /** Calc output position and size of shape, according to given
262 attribute layer.
264 Rotations, shears etc. and not taken into account,
265 i.e. the returned rectangle is NOT the bounding box. Use
266 it as if aBounds.getMinimum() is the output position and
267 aBounds.getRange() the scaling of the shape.
269 basegfx::B2DRange getShapePosSize(
270 const basegfx::B2DRange& rOrigBounds,
271 const boost::shared_ptr<ShapeAttributeLayer>& pAttr );
273 /** Convert a plain UNO API 32 bit int to RGBColor
275 RGBColor unoColor2RGBColor( sal_Int32 );
276 /** Convert an IntSRGBA to plain UNO API 32 bit int
278 sal_Int32 RGBAColor2UnoColor( cppcanvas::Color::IntSRGBA );
280 /** Fill a plain rectangle on the given canvas with the given color
282 void fillRect( const boost::shared_ptr< cppcanvas::Canvas >& rCanvas,
283 const basegfx::B2DRange& rRect,
284 cppcanvas::Color::IntSRGBA aFillColor );
286 /** Init canvas with default background (white)
288 void initSlideBackground( const boost::shared_ptr< cppcanvas::Canvas >& rCanvas,
289 const basegfx::B2IVector& rSize );
291 /// Gets a random ordinal [0,n)
292 inline ::std::size_t getRandomOrdinal( const ::std::size_t n )
294 return static_cast< ::std::size_t >(
295 double(n) * rand() / (RAND_MAX + 1.0) );
298 /// To work around ternary operator in initializer lists
299 /// (Solaris compiler problems)
300 template <typename T>
301 inline T const & ternary_op(
302 const bool cond, T const & arg1, T const & arg2 )
304 if (cond)
305 return arg1;
306 else
307 return arg2;
310 template <typename ValueType>
311 inline bool getPropertyValue(
312 ValueType & rValue,
313 com::sun::star::uno::Reference<
314 com::sun::star::beans::XPropertySet> const & xPropSet,
315 rtl::OUString const & propName )
317 try {
318 const com::sun::star::uno::Any& a(
319 xPropSet->getPropertyValue( propName ) );
320 bool const bRet = (a >>= rValue);
321 #if OSL_DEBUG_LEVEL > 0
322 if( !bRet )
323 OSL_TRACE( "%s: while retrieving property %s, cannot extract Any of type %s\n",
324 ::rtl::OUStringToOString( propName,
325 RTL_TEXTENCODING_ASCII_US ).getStr(),
326 BOOST_CURRENT_FUNCTION,
327 ::rtl::OUStringToOString( a.getValueTypeRef()->pTypeName,
328 RTL_TEXTENCODING_ASCII_US ).getStr() );
329 #endif
330 return bRet;
332 catch (com::sun::star::uno::RuntimeException &)
334 throw;
336 catch (com::sun::star::uno::Exception &)
338 return false;
342 template <typename ValueType>
343 inline bool getPropertyValue(
344 com::sun::star::uno::Reference< ValueType >& rIfc,
345 com::sun::star::uno::Reference<
346 com::sun::star::beans::XPropertySet> const & xPropSet,
347 rtl::OUString const & propName )
351 const com::sun::star::uno::Any& a(
352 xPropSet->getPropertyValue( propName ));
353 rIfc.set( a,
354 com::sun::star::uno::UNO_QUERY );
356 bool const bRet = rIfc.is();
357 #if OSL_DEBUG_LEVEL > 0
358 if( !bRet )
359 OSL_TRACE( "%s: while retrieving property %s, cannot extract Any of type %s to interface\n",
360 ::rtl::OUStringToOString( propName,
361 RTL_TEXTENCODING_ASCII_US ).getStr(),
362 BOOST_CURRENT_FUNCTION,
363 ::rtl::OUStringToOString( a.getValueTypeRef()->pTypeName,
364 RTL_TEXTENCODING_ASCII_US ).getStr() );
365 #endif
366 return bRet;
368 catch (com::sun::star::uno::RuntimeException &)
370 throw;
372 catch (com::sun::star::uno::Exception &)
374 return false;
378 /// Get the content of the BoundRect shape property
379 basegfx::B2DRange getAPIShapeBounds( const ::com::sun::star::uno::Reference<
380 ::com::sun::star::drawing::XShape >& xShape );
383 TODO(F1): When ZOrder someday becomes usable enable this
385 /// Get the content of the ZOrder shape property
386 double getAPIShapePrio( const ::com::sun::star::uno::Reference<
387 ::com::sun::star::drawing::XShape >& xShape );
390 basegfx::B2IVector getSlideSizePixel( const basegfx::B2DVector& rSize,
391 const boost::shared_ptr<UnoView>& pView );
395 #endif /* INCLUDED_SLIDESHOW_TOOLS_HXX */
397 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */