1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <osl/diagnose.h>
33 #include <cppcanvas/color.hxx>
35 #include "shapeattributelayer.hxx"
37 #include "rgbcolor.hxx"
38 #include "hslcolor.hxx"
46 namespace com
{ namespace sun
{ namespace star
{ namespace beans
{
56 namespace cppcanvas
{ class Canvas
; }
60 /* Definition of some animation tools */
67 class ShapeAttributeLayer
;
69 typedef ::std::shared_ptr
< GDIMetaFile
> GDIMetaFileSharedPtr
;
72 inline ::std::size_t hash_value( T
const * p
)
74 ::std::size_t d
= static_cast< ::std::size_t >(
75 reinterpret_cast< ::std::ptrdiff_t >(p
) );
79 // xxx todo: remove with boost::hash when 1.33 is available
81 struct hash
: ::std::unary_function
<T
, ::std::size_t>
83 ::std::size_t operator()( T
const& val
) const {
84 return hash_value(val
);
90 namespace com
{ namespace sun
{ namespace star
{ namespace uno
{
93 inline ::std::size_t hash_value(
94 css::uno::Reference
<T
> const& x
)
96 // normalize to object root, because _only_ XInterface is defined
97 // to be stable during object lifetime:
98 css::uno::Reference
< css::uno::XInterface
> const xRoot( x
, css::uno::UNO_QUERY
);
99 return slideshow::internal::hash
<void *>()(xRoot
.get());
108 /** Cycle mode of intrinsic animations
112 /// loop the animation back to back
114 /// loop, but play backwards from end to start
119 // Value extraction from Any
120 // =========================
122 /// extract unary double value from Any
123 bool extractValue( double& o_rValue
,
124 const css::uno::Any
& rSourceAny
,
125 const std::shared_ptr
<Shape
>& rShape
,
126 const basegfx::B2DVector
& rSlideBounds
);
128 /// extract int from Any
129 bool extractValue( sal_Int32
& o_rValue
,
130 const css::uno::Any
& rSourceAny
,
131 const std::shared_ptr
<Shape
>& rShape
,
132 const basegfx::B2DVector
& rSlideBounds
);
134 /// extract enum/constant group value from Any
135 bool extractValue( sal_Int16
& o_rValue
,
136 const css::uno::Any
& rSourceAny
,
137 const std::shared_ptr
<Shape
>& rShape
,
138 const basegfx::B2DVector
& rSlideBounds
);
140 /// extract color value from Any
141 bool extractValue( RGBColor
& o_rValue
,
142 const css::uno::Any
& rSourceAny
,
143 const std::shared_ptr
<Shape
>& rShape
,
144 const basegfx::B2DVector
& rSlideBounds
);
146 /// extract color value from Any
147 bool extractValue( HSLColor
& o_rValue
,
148 const css::uno::Any
& rSourceAny
,
149 const std::shared_ptr
<Shape
>& rShape
,
150 const basegfx::B2DVector
& rSlideBounds
);
152 /// extract plain string from Any
153 bool extractValue( OUString
& o_rValue
,
154 const css::uno::Any
& rSourceAny
,
155 const std::shared_ptr
<Shape
>& rShape
,
156 const basegfx::B2DVector
& rSlideBounds
);
158 /// extract bool value from Any
159 bool extractValue( bool& o_rValue
,
160 const css::uno::Any
& rSourceAny
,
161 const std::shared_ptr
<Shape
>& rShape
,
162 const basegfx::B2DVector
& rSlideBounds
);
164 /// extract double 2-tuple from Any
165 bool extractValue( basegfx::B2DTuple
& o_rPair
,
166 const css::uno::Any
& rSourceAny
,
167 const std::shared_ptr
<Shape
>& rShape
,
168 const basegfx::B2DVector
& rSlideBounds
);
170 /** Search a sequence of NamedValues for a given element.
172 @return true, if the sequence contains the specified
175 bool findNamedValue( css::uno::Sequence
< css::beans::NamedValue
> const& rSequence
,
176 const css::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
184 Original shape bound rect (to substitute default attribute
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 std::shared_ptr
<ShapeAttributeLayer
>& pAttr
);
195 /** Get a shape's sprite transformation from the attribute set
198 Pixel size of the sprite
201 Original shape size (i.e. the size of the actual sprite
202 content, in the user coordinate system)
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 std::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
225 Shape bounds, in the unit rect coordinate space
227 @param rShapeTransform
228 Transformation matrix the shape should undergo.
231 Current shape attributes
233 basegfx::B2DRange
getShapeUpdateArea(
234 const basegfx::B2DRange
& rUnitBounds
,
235 const basegfx::B2DHomMatrix
& rShapeTransform
,
236 const std::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)
251 Shape bounds, in the unit rect coordinate space
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
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 std::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 std::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 std::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 comphelper::rng::uniform_size_distribution(0, n
-1);
295 /// To work around ternary operator in initializer lists
296 /// (Solaris compiler problems)
298 template <typename T
>
299 inline T
const & ternary_op(
300 const bool cond
, T
const & arg1
, T
const & arg2
)
309 template <typename ValueType
>
310 inline bool getPropertyValue(
313 css::beans::XPropertySet
> const & xPropSet
,
314 OUString
const & propName
)
317 const css::uno::Any
& a(
318 xPropSet
->getPropertyValue( propName
) );
319 bool const bRet
= css::uno::fromAny(a
, &rValue
);
320 #if OSL_DEBUG_LEVEL > 0
322 OSL_TRACE( "%s: while retrieving property %s, cannot extract Any of type %s\n",
323 OUStringToOString( propName
,
324 RTL_TEXTENCODING_ASCII_US
).getStr(),
326 OUStringToOString( a
.getValueTypeRef()->pTypeName
,
327 RTL_TEXTENCODING_ASCII_US
).getStr() );
331 catch (css::uno::RuntimeException
&)
335 catch (css::uno::Exception
&)
341 template <typename ValueType
>
342 inline bool getPropertyValue(
343 css::uno::Reference
< ValueType
>& rIfc
,
344 css::uno::Reference
< css::beans::XPropertySet
> const & xPropSet
,
345 OUString
const & propName
)
349 const css::uno::Any
& a(xPropSet
->getPropertyValue( propName
));
350 rIfc
.set( a
, css::uno::UNO_QUERY
);
352 bool const bRet
= rIfc
.is();
353 #if OSL_DEBUG_LEVEL > 0
355 OSL_TRACE( "%s: while retrieving property %s, cannot extract Any of type %s to interface\n",
356 OUStringToOString( propName
,
357 RTL_TEXTENCODING_ASCII_US
).getStr(),
359 OUStringToOString( a
.getValueTypeRef()->pTypeName
,
360 RTL_TEXTENCODING_ASCII_US
).getStr() );
364 catch (css::uno::RuntimeException
&)
368 catch (css::uno::Exception
&)
374 /// Get the content of the BoundRect shape property
375 basegfx::B2DRange
getAPIShapeBounds( const css::uno::Reference
< css::drawing::XShape
>& xShape
);
378 TODO(F1): When ZOrder someday becomes usable enable this
380 /// Get the content of the ZOrder shape property
381 double getAPIShapePrio( const css::uno::Reference< css::drawing::XShape >& xShape );
384 basegfx::B2IVector
getSlideSizePixel( const basegfx::B2DVector
& rSize
,
385 const std::shared_ptr
<UnoView
>& pView
);
388 // TODO(Q1): this could possibly be implemented with a somewhat
389 // more lightweight template, by having the actual worker receive
390 // only a function pointer, and a thin templated wrapper around
391 // that which converts member functions into that.
393 /** Apply given functor to every animation node child.
399 Functor to apply. The functor must have an appropriate
400 operator()( const css::uno::Reference< css::animations::XAnimationNode >& ) member.
402 @return true, if the functor was successfully applied to
403 all children, false otherwise.
405 template< typename Functor
> inline bool for_each_childNode( const css::uno::Reference
< css::animations::XAnimationNode
>& xNode
,
410 // get an XEnumerationAccess to the children
411 css::uno::Reference
< css::container::XEnumerationAccess
>
412 xEnumerationAccess( xNode
,
413 css::uno::UNO_QUERY_THROW
);
414 css::uno::Reference
< css::container::XEnumeration
>
415 xEnumeration( xEnumerationAccess
->createEnumeration(),
416 css::uno::UNO_QUERY_THROW
);
418 while( xEnumeration
->hasMoreElements() )
420 css::uno::Reference
< css::animations::XAnimationNode
>
421 xChildNode( xEnumeration
->nextElement(),
422 css::uno::UNO_QUERY_THROW
);
423 rFunctor( xChildNode
);
427 catch( css::uno::Exception
& )
434 #endif // INCLUDED_SLIDESHOW_SOURCE_INC_TOOLS_HXX
436 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */