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 .
21 #include <toolkit/controls/animatedimages.hxx>
22 #include <toolkit/helper/property.hxx>
24 #include <com/sun/star/lang/DisposedException.hpp>
25 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
26 #include <com/sun/star/awt/VisualEffect.hpp>
27 #include <com/sun/star/awt/ImageScaleMode.hpp>
28 #include <com/sun/star/awt/XAnimation.hpp>
29 #include <com/sun/star/awt/XAnimatedImages.hpp>
30 #include <com/sun/star/beans/XPropertySetInfo.hpp>
31 #include <com/sun/star/container/XContainerListener.hpp>
32 #include <com/sun/star/uno/XComponentContext.hpp>
33 #include <com/sun/star/util/XModifyListener.hpp>
35 #include <toolkit/controls/unocontrolbase.hxx>
36 #include <toolkit/controls/unocontrolmodel.hxx>
38 #include <cppuhelper/implbase2.hxx>
40 #include "helper/unopropertyarrayhelper.hxx"
42 using namespace css::awt
;
43 using namespace css::container
;
44 using namespace css::lang
;
45 using namespace css::uno
;
49 typedef ::cppu::AggImplInheritanceHelper2
< UnoControlBase
50 , css::awt::XAnimation
51 , css::container::XContainerListener
52 > AnimatedImagesControl_Base
;
54 class AnimatedImagesControl
: public AnimatedImagesControl_Base
57 AnimatedImagesControl();
58 OUString
GetComponentServiceName() override
;
61 virtual void SAL_CALL
startAnimation( ) override
;
62 virtual void SAL_CALL
stopAnimation( ) override
;
63 virtual sal_Bool SAL_CALL
isAnimationRunning( ) override
;
66 OUString SAL_CALL
getImplementationName( ) override
;
67 css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
70 sal_Bool SAL_CALL
setModel( const css::uno::Reference
< css::awt::XControlModel
>& i_rModel
) override
;
71 void SAL_CALL
createPeer( const css::uno::Reference
< css::awt::XToolkit
>& i_toolkit
, const css::uno::Reference
< css::awt::XWindowPeer
>& i_parentPeer
) override
;
75 virtual void SAL_CALL
elementInserted( const css::container::ContainerEvent
& Event
) override
;
76 virtual void SAL_CALL
elementRemoved( const css::container::ContainerEvent
& Event
) override
;
77 virtual void SAL_CALL
elementReplaced( const css::container::ContainerEvent
& Event
) override
;
80 virtual void SAL_CALL
disposing( const css::lang::EventObject
& i_event
) override
;
83 AnimatedImagesControl::AnimatedImagesControl()
84 :AnimatedImagesControl_Base()
89 OUString
AnimatedImagesControl::GetComponentServiceName()
91 return OUString( "AnimatedImages" );
95 void SAL_CALL
AnimatedImagesControl::startAnimation( )
97 Reference
< XAnimation
> xAnimation( getPeer(), UNO_QUERY
);
98 if ( xAnimation
.is() )
99 xAnimation
->startAnimation();
103 void SAL_CALL
AnimatedImagesControl::stopAnimation( )
105 Reference
< XAnimation
> xAnimation( getPeer(), UNO_QUERY
);
106 if ( xAnimation
.is() )
107 xAnimation
->stopAnimation();
111 sal_Bool SAL_CALL
AnimatedImagesControl::isAnimationRunning( )
113 Reference
< XAnimation
> xAnimation( getPeer(), UNO_QUERY
);
114 if ( xAnimation
.is() )
115 return xAnimation
->isAnimationRunning();
120 OUString SAL_CALL
AnimatedImagesControl::getImplementationName( )
122 return OUString( "org.openoffice.comp.toolkit.AnimatedImagesControl" );
126 Sequence
< OUString
> SAL_CALL
AnimatedImagesControl::getSupportedServiceNames()
128 Sequence
< OUString
> aServices( AnimatedImagesControl_Base::getSupportedServiceNames() );
129 aServices
.realloc( aServices
.getLength() + 1 );
130 aServices
[ aServices
.getLength() - 1 ] = "com.sun.star.awt.AnimatedImagesControl";
137 void lcl_updatePeer( Reference
< XWindowPeer
> const& i_peer
, Reference
< XControlModel
> const& i_model
)
139 const Reference
< css::util::XModifyListener
> xPeerModify( i_peer
, UNO_QUERY
);
140 if ( xPeerModify
.is() )
143 aEvent
.Source
= i_model
;
144 xPeerModify
->modified( aEvent
);
150 sal_Bool SAL_CALL
AnimatedImagesControl::setModel( const Reference
< XControlModel
>& i_rModel
)
152 const Reference
< XAnimatedImages
> xOldContainer( getModel(), UNO_QUERY
);
153 const Reference
< XAnimatedImages
> xNewContainer( i_rModel
, UNO_QUERY
);
155 if ( !AnimatedImagesControl_Base::setModel( i_rModel
) )
158 if ( xOldContainer
.is() )
159 xOldContainer
->removeContainerListener( this );
161 if ( xNewContainer
.is() )
162 xNewContainer
->addContainerListener( this );
164 lcl_updatePeer( getPeer(), getModel() );
170 void SAL_CALL
AnimatedImagesControl::createPeer( const Reference
< XToolkit
>& i_toolkit
, const Reference
< XWindowPeer
>& i_parentPeer
)
172 AnimatedImagesControl_Base::createPeer( i_toolkit
, i_parentPeer
);
174 lcl_updatePeer( getPeer(), getModel() );
178 void SAL_CALL
AnimatedImagesControl::elementInserted( const ContainerEvent
& i_event
)
180 const Reference
< XContainerListener
> xPeerListener( getPeer(), UNO_QUERY
);
181 if ( xPeerListener
.is() )
182 xPeerListener
->elementInserted( i_event
);
186 void SAL_CALL
AnimatedImagesControl::elementRemoved( const ContainerEvent
& i_event
)
188 const Reference
< XContainerListener
> xPeerListener( getPeer(), UNO_QUERY
);
189 if ( xPeerListener
.is() )
190 xPeerListener
->elementRemoved( i_event
);
194 void SAL_CALL
AnimatedImagesControl::elementReplaced( const ContainerEvent
& i_event
)
196 const Reference
< XContainerListener
> xPeerListener( getPeer(), UNO_QUERY
);
197 if ( xPeerListener
.is() )
198 xPeerListener
->elementReplaced( i_event
);
202 void SAL_CALL
AnimatedImagesControl::disposing( const EventObject
& i_event
)
204 UnoControlBase::disposing( i_event
);
211 struct AnimatedImagesControlModel_Data
213 ::std::vector
< Sequence
< OUString
> > aImageSets
;
218 void lcl_checkIndex( const AnimatedImagesControlModel_Data
& i_data
, const sal_Int32 i_index
, const Reference
< XInterface
>& i_context
,
219 const bool i_forInsert
= false )
221 if ( ( i_index
< 0 ) || ( size_t( i_index
) > i_data
.aImageSets
.size() + ( i_forInsert
? 1 : 0 ) ) )
222 throw IndexOutOfBoundsException( OUString(), i_context
);
225 void lcl_notify( ::osl::ClearableMutexGuard
& i_guard
, ::cppu::OBroadcastHelper
& i_broadcaseHelper
,
226 void ( SAL_CALL
XContainerListener::*i_notificationMethod
)( const ContainerEvent
& ),
227 const sal_Int32 i_accessor
, const Sequence
< OUString
>& i_imageURLs
, const Reference
< XInterface
>& i_context
)
229 ::cppu::OInterfaceContainerHelper
* pContainerListeners
= i_broadcaseHelper
.getContainer( cppu::UnoType
<XContainerListener
>::get() );
230 if ( pContainerListeners
== nullptr )
233 ContainerEvent aEvent
;
234 aEvent
.Source
= i_context
;
235 aEvent
.Accessor
<<= i_accessor
;
236 aEvent
.Element
<<= i_imageURLs
;
239 pContainerListeners
->notifyEach( i_notificationMethod
, aEvent
);
244 AnimatedImagesControlModel::AnimatedImagesControlModel( Reference
< css::uno::XComponentContext
> const & i_factory
)
245 :AnimatedImagesControlModel_Base( i_factory
)
246 ,m_xData( new AnimatedImagesControlModel_Data
)
248 ImplRegisterProperty( BASEPROPERTY_AUTO_REPEAT
);
249 ImplRegisterProperty( BASEPROPERTY_BORDER
);
250 ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR
);
251 ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR
);
252 ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL
);
253 ImplRegisterProperty( BASEPROPERTY_ENABLEVISIBLE
);
254 ImplRegisterProperty( BASEPROPERTY_HELPTEXT
);
255 ImplRegisterProperty( BASEPROPERTY_HELPURL
);
256 ImplRegisterProperty( BASEPROPERTY_IMAGE_SCALE_MODE
);
257 ImplRegisterProperty( BASEPROPERTY_STEP_TIME
);
261 AnimatedImagesControlModel::AnimatedImagesControlModel( const AnimatedImagesControlModel
& i_copySource
)
262 :AnimatedImagesControlModel_Base( i_copySource
)
263 ,m_xData( new AnimatedImagesControlModel_Data( *i_copySource
.m_xData
) )
268 AnimatedImagesControlModel::~AnimatedImagesControlModel()
273 UnoControlModel
* AnimatedImagesControlModel::Clone() const
275 return new AnimatedImagesControlModel( *this );
279 Reference
< css::beans::XPropertySetInfo
> SAL_CALL
AnimatedImagesControlModel::getPropertySetInfo( )
281 static Reference
< css::beans::XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
286 OUString SAL_CALL
AnimatedImagesControlModel::getServiceName()
288 return OUString("com.sun.star.awt.AnimatedImagesControlModel");
292 OUString SAL_CALL
AnimatedImagesControlModel::getImplementationName( )
294 return OUString("org.openoffice.comp.toolkit.AnimatedImagesControlModel");
298 Sequence
< OUString
> SAL_CALL
AnimatedImagesControlModel::getSupportedServiceNames()
300 Sequence
< OUString
> aServiceNames(2);
301 aServiceNames
[0] = "com.sun.star.awt.AnimatedImagesControlModel";
302 aServiceNames
[1] = "com.sun.star.awt.UnoControlModel";
303 return aServiceNames
;
307 void SAL_CALL
AnimatedImagesControlModel::setFastPropertyValue_NoBroadcast( sal_Int32 i_handle
, const Any
& i_value
)
311 case BASEPROPERTY_IMAGE_SCALE_MODE
:
313 sal_Int16
nImageScaleMode( ImageScaleMode::ANISOTROPIC
);
314 OSL_VERIFY( i_value
>>= nImageScaleMode
); // convertFastPropertyValue ensures that this has the proper type
315 if ( ( nImageScaleMode
!= ImageScaleMode::NONE
)
316 && ( nImageScaleMode
!= ImageScaleMode::ISOTROPIC
)
317 && ( nImageScaleMode
!= ImageScaleMode::ANISOTROPIC
)
319 throw IllegalArgumentException( OUString(), *this, 1 );
324 AnimatedImagesControlModel_Base::setFastPropertyValue_NoBroadcast( i_handle
, i_value
);
328 Any
AnimatedImagesControlModel::ImplGetDefaultValue( sal_uInt16 i_propertyId
) const
330 switch ( i_propertyId
)
332 case BASEPROPERTY_DEFAULTCONTROL
:
333 return makeAny( OUString("com.sun.star.awt.AnimatedImagesControl") );
335 case BASEPROPERTY_BORDER
:
336 return makeAny( css::awt::VisualEffect::NONE
);
338 case BASEPROPERTY_STEP_TIME
:
339 return makeAny( (sal_Int32
) 100 );
341 case BASEPROPERTY_AUTO_REPEAT
:
342 return makeAny( true );
344 case BASEPROPERTY_IMAGE_SCALE_MODE
:
345 return makeAny( ImageScaleMode::NONE
);
348 return UnoControlModel::ImplGetDefaultValue( i_propertyId
);
353 ::cppu::IPropertyArrayHelper
& SAL_CALL
AnimatedImagesControlModel::getInfoHelper()
355 static UnoPropertyArrayHelper
* pHelper
= nullptr;
358 Sequence
< sal_Int32
> aIDs
= ImplGetPropertyIds();
359 pHelper
= new UnoPropertyArrayHelper( aIDs
);
365 ::sal_Int32 SAL_CALL
AnimatedImagesControlModel::getStepTime()
367 sal_Int32
nStepTime( 100 );
368 OSL_VERIFY( getPropertyValue( GetPropertyName( BASEPROPERTY_STEP_TIME
) ) >>= nStepTime
);
373 void SAL_CALL
AnimatedImagesControlModel::setStepTime( ::sal_Int32 i_stepTime
)
375 setPropertyValue( GetPropertyName( BASEPROPERTY_STEP_TIME
), makeAny( i_stepTime
) );
379 sal_Bool SAL_CALL
AnimatedImagesControlModel::getAutoRepeat()
381 bool bAutoRepeat( true );
382 OSL_VERIFY( getPropertyValue( GetPropertyName( BASEPROPERTY_AUTO_REPEAT
) ) >>= bAutoRepeat
);
387 void SAL_CALL
AnimatedImagesControlModel::setAutoRepeat( sal_Bool i_autoRepeat
)
389 setPropertyValue( GetPropertyName( BASEPROPERTY_AUTO_REPEAT
), makeAny( i_autoRepeat
) );
393 ::sal_Int16 SAL_CALL
AnimatedImagesControlModel::getScaleMode()
395 sal_Int16
nImageScaleMode( ImageScaleMode::ANISOTROPIC
);
396 OSL_VERIFY( getPropertyValue( GetPropertyName( BASEPROPERTY_IMAGE_SCALE_MODE
) ) >>= nImageScaleMode
);
397 return nImageScaleMode
;
401 void SAL_CALL
AnimatedImagesControlModel::setScaleMode( ::sal_Int16 i_scaleMode
)
403 setPropertyValue( GetPropertyName( BASEPROPERTY_IMAGE_SCALE_MODE
), makeAny( i_scaleMode
) );
407 ::sal_Int32 SAL_CALL
AnimatedImagesControlModel::getImageSetCount( )
409 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
410 if ( GetBroadcastHelper().bDisposed
|| GetBroadcastHelper().bInDispose
)
411 throw DisposedException();
413 return m_xData
->aImageSets
.size();
417 Sequence
< OUString
> SAL_CALL
AnimatedImagesControlModel::getImageSet( ::sal_Int32 i_index
)
419 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
420 if ( GetBroadcastHelper().bDisposed
|| GetBroadcastHelper().bInDispose
)
421 throw DisposedException();
423 lcl_checkIndex( *m_xData
, i_index
, *this );
425 return m_xData
->aImageSets
[ i_index
];
429 void SAL_CALL
AnimatedImagesControlModel::insertImageSet( ::sal_Int32 i_index
, const Sequence
< OUString
>& i_imageURLs
)
431 ::osl::ClearableMutexGuard
aGuard( GetMutex() );
433 if ( GetBroadcastHelper().bDisposed
|| GetBroadcastHelper().bInDispose
)
434 throw DisposedException();
436 lcl_checkIndex( *m_xData
, i_index
, *this, true );
439 m_xData
->aImageSets
.insert( m_xData
->aImageSets
.begin() + i_index
, i_imageURLs
);
441 // listener notification
442 lcl_notify( aGuard
, BrdcstHelper
, &XContainerListener::elementInserted
, i_index
, i_imageURLs
, *this );
446 void SAL_CALL
AnimatedImagesControlModel::replaceImageSet( ::sal_Int32 i_index
, const Sequence
< OUString
>& i_imageURLs
)
448 ::osl::ClearableMutexGuard
aGuard( GetMutex() );
450 if ( GetBroadcastHelper().bDisposed
|| GetBroadcastHelper().bInDispose
)
451 throw DisposedException();
453 lcl_checkIndex( *m_xData
, i_index
, *this );
456 m_xData
->aImageSets
[ i_index
] = i_imageURLs
;
458 // listener notification
459 lcl_notify( aGuard
, BrdcstHelper
, &XContainerListener::elementReplaced
, i_index
, i_imageURLs
, *this );
463 void SAL_CALL
AnimatedImagesControlModel::removeImageSet( ::sal_Int32 i_index
)
465 ::osl::ClearableMutexGuard
aGuard( GetMutex() );
467 if ( GetBroadcastHelper().bDisposed
|| GetBroadcastHelper().bInDispose
)
468 throw DisposedException();
470 lcl_checkIndex( *m_xData
, i_index
, *this );
473 ::std::vector
< Sequence
< OUString
> >::iterator removalPos
= m_xData
->aImageSets
.begin() + i_index
;
474 Sequence
< OUString
> aRemovedElement( *removalPos
);
475 m_xData
->aImageSets
.erase( removalPos
);
477 // listener notification
478 lcl_notify( aGuard
, BrdcstHelper
, &XContainerListener::elementRemoved
, i_index
, aRemovedElement
, *this );
482 void SAL_CALL
AnimatedImagesControlModel::addContainerListener( const Reference
< XContainerListener
>& i_listener
)
484 BrdcstHelper
.addListener( cppu::UnoType
<XContainerListener
>::get(), i_listener
);
488 void SAL_CALL
AnimatedImagesControlModel::removeContainerListener( const Reference
< XContainerListener
>& i_listener
)
490 BrdcstHelper
.removeListener( cppu::UnoType
<XContainerListener
>::get(), i_listener
);
495 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
496 org_openoffice_comp_toolkit_AnimatedImagesControl_get_implementation(
497 css::uno::XComponentContext
*,
498 css::uno::Sequence
<css::uno::Any
> const &)
500 return cppu::acquire(new AnimatedImagesControl());
503 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
504 org_openoffice_comp_toolkit_AnimatedImagesControlModel_get_implementation(
505 css::uno::XComponentContext
*context
,
506 css::uno::Sequence
<css::uno::Any
> const &)
508 return cppu::acquire(new toolkit::AnimatedImagesControlModel(context
));
511 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */