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 <controls/animatedimages.hxx>
22 #include <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>
34 #include <o3tl/safeint.hxx>
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() const 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()
88 OUString
AnimatedImagesControl::GetComponentServiceName() const
90 return u
"AnimatedImages"_ustr
;
94 void SAL_CALL
AnimatedImagesControl::startAnimation( )
96 Reference
< XAnimation
> xAnimation( getPeer(), UNO_QUERY
);
97 if ( xAnimation
.is() )
98 xAnimation
->startAnimation();
102 void SAL_CALL
AnimatedImagesControl::stopAnimation( )
104 Reference
< XAnimation
> xAnimation( getPeer(), UNO_QUERY
);
105 if ( xAnimation
.is() )
106 xAnimation
->stopAnimation();
110 sal_Bool SAL_CALL
AnimatedImagesControl::isAnimationRunning( )
112 Reference
< XAnimation
> xAnimation( getPeer(), UNO_QUERY
);
113 if ( xAnimation
.is() )
114 return xAnimation
->isAnimationRunning();
119 OUString SAL_CALL
AnimatedImagesControl::getImplementationName( )
121 return u
"org.openoffice.comp.toolkit.AnimatedImagesControl"_ustr
;
125 Sequence
< OUString
> SAL_CALL
AnimatedImagesControl::getSupportedServiceNames()
127 Sequence
< OUString
> aServices( AnimatedImagesControl_Base::getSupportedServiceNames() );
128 aServices
.realloc( aServices
.getLength() + 1 );
129 aServices
.getArray()[ aServices
.getLength() - 1 ] = "com.sun.star.awt.AnimatedImagesControl";
133 void lcl_updatePeer( Reference
< XWindowPeer
> const& i_peer
, Reference
< XControlModel
> const& i_model
)
135 const Reference
< css::util::XModifyListener
> xPeerModify( i_peer
, UNO_QUERY
);
136 if ( xPeerModify
.is() )
139 aEvent
.Source
= i_model
;
140 xPeerModify
->modified( aEvent
);
144 sal_Bool SAL_CALL
AnimatedImagesControl::setModel( const Reference
< XControlModel
>& i_rModel
)
146 const Reference
< XAnimatedImages
> xOldContainer( getModel(), UNO_QUERY
);
147 const Reference
< XAnimatedImages
> xNewContainer( i_rModel
, UNO_QUERY
);
149 if ( !AnimatedImagesControl_Base::setModel( i_rModel
) )
152 if ( xOldContainer
.is() )
153 xOldContainer
->removeContainerListener( this );
155 if ( xNewContainer
.is() )
156 xNewContainer
->addContainerListener( this );
158 lcl_updatePeer( getPeer(), getModel() );
164 void SAL_CALL
AnimatedImagesControl::createPeer( const Reference
< XToolkit
>& i_toolkit
, const Reference
< XWindowPeer
>& i_parentPeer
)
166 AnimatedImagesControl_Base::createPeer( i_toolkit
, i_parentPeer
);
168 lcl_updatePeer( getPeer(), getModel() );
172 void SAL_CALL
AnimatedImagesControl::elementInserted( const ContainerEvent
& i_event
)
174 const Reference
< XContainerListener
> xPeerListener( getPeer(), UNO_QUERY
);
175 if ( xPeerListener
.is() )
176 xPeerListener
->elementInserted( i_event
);
180 void SAL_CALL
AnimatedImagesControl::elementRemoved( const ContainerEvent
& i_event
)
182 const Reference
< XContainerListener
> xPeerListener( getPeer(), UNO_QUERY
);
183 if ( xPeerListener
.is() )
184 xPeerListener
->elementRemoved( i_event
);
188 void SAL_CALL
AnimatedImagesControl::elementReplaced( const ContainerEvent
& i_event
)
190 const Reference
< XContainerListener
> xPeerListener( getPeer(), UNO_QUERY
);
191 if ( xPeerListener
.is() )
192 xPeerListener
->elementReplaced( i_event
);
196 void SAL_CALL
AnimatedImagesControl::disposing( const EventObject
& i_event
)
198 UnoControlBase::disposing( i_event
);
207 void lcl_checkIndex( const std::vector
< css::uno::Sequence
< OUString
> > & rImageSets
, const sal_Int32 i_index
, const Reference
< XInterface
>& i_context
,
208 const bool i_forInsert
= false )
210 if ( ( i_index
< 0 ) || ( o3tl::make_unsigned( i_index
) > rImageSets
.size() + ( i_forInsert
? 1 : 0 ) ) )
211 throw IndexOutOfBoundsException( OUString(), i_context
);
214 void lcl_notify( std::unique_lock
<std::mutex
>& i_guard
, comphelper::OInterfaceContainerHelper4
<XContainerListener
>& rContainer
,
215 void ( SAL_CALL
XContainerListener::*i_notificationMethod
)( const ContainerEvent
& ),
216 const sal_Int32 i_accessor
, const Sequence
< OUString
>& i_imageURLs
, const Reference
< XInterface
>& i_context
)
218 if ( !rContainer
.getLength(i_guard
) )
221 ContainerEvent aEvent
;
222 aEvent
.Source
= i_context
;
223 aEvent
.Accessor
<<= i_accessor
;
224 aEvent
.Element
<<= i_imageURLs
;
226 rContainer
.notifyEach( i_guard
, i_notificationMethod
, aEvent
);
231 AnimatedImagesControlModel::AnimatedImagesControlModel( Reference
< css::uno::XComponentContext
> const & i_factory
)
232 :AnimatedImagesControlModel_Base( i_factory
)
234 ImplRegisterProperty( BASEPROPERTY_AUTO_REPEAT
);
235 ImplRegisterProperty( BASEPROPERTY_BORDER
);
236 ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR
);
237 ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR
);
238 ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL
);
239 ImplRegisterProperty( BASEPROPERTY_ENABLEVISIBLE
);
240 ImplRegisterProperty( BASEPROPERTY_HELPTEXT
);
241 ImplRegisterProperty( BASEPROPERTY_HELPURL
);
242 ImplRegisterProperty( BASEPROPERTY_IMAGE_SCALE_MODE
);
243 ImplRegisterProperty( BASEPROPERTY_STEP_TIME
);
247 AnimatedImagesControlModel::AnimatedImagesControlModel( const AnimatedImagesControlModel
& i_copySource
)
248 :AnimatedImagesControlModel_Base( i_copySource
)
249 ,maImageSets( i_copySource
.maImageSets
)
254 AnimatedImagesControlModel::~AnimatedImagesControlModel()
259 rtl::Reference
<UnoControlModel
> AnimatedImagesControlModel::Clone() const
261 std::unique_lock
aGuard( m_aMutex
);
262 return new AnimatedImagesControlModel( *this );
266 Reference
< css::beans::XPropertySetInfo
> SAL_CALL
AnimatedImagesControlModel::getPropertySetInfo( )
268 static Reference
< css::beans::XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
273 OUString SAL_CALL
AnimatedImagesControlModel::getServiceName()
275 return u
"com.sun.star.awt.AnimatedImagesControlModel"_ustr
;
279 OUString SAL_CALL
AnimatedImagesControlModel::getImplementationName( )
281 return u
"org.openoffice.comp.toolkit.AnimatedImagesControlModel"_ustr
;
285 Sequence
< OUString
> SAL_CALL
AnimatedImagesControlModel::getSupportedServiceNames()
287 return { u
"com.sun.star.awt.AnimatedImagesControlModel"_ustr
, u
"com.sun.star.awt.UnoControlModel"_ustr
};
291 void AnimatedImagesControlModel::setFastPropertyValue_NoBroadcast( std::unique_lock
<std::mutex
>& rGuard
, sal_Int32 i_handle
, const Any
& i_value
)
295 case BASEPROPERTY_IMAGE_SCALE_MODE
:
297 sal_Int16
nImageScaleMode( ImageScaleMode::ANISOTROPIC
);
298 OSL_VERIFY( i_value
>>= nImageScaleMode
); // convertFastPropertyValue ensures that this has the proper type
299 if ( ( nImageScaleMode
!= ImageScaleMode::NONE
)
300 && ( nImageScaleMode
!= ImageScaleMode::ISOTROPIC
)
301 && ( nImageScaleMode
!= ImageScaleMode::ANISOTROPIC
)
303 throw IllegalArgumentException( OUString(), *this, 1 );
308 AnimatedImagesControlModel_Base::setFastPropertyValue_NoBroadcast( rGuard
, i_handle
, i_value
);
312 Any
AnimatedImagesControlModel::ImplGetDefaultValue( sal_uInt16 i_propertyId
) const
314 switch ( i_propertyId
)
316 case BASEPROPERTY_DEFAULTCONTROL
:
317 return Any( u
"com.sun.star.awt.AnimatedImagesControl"_ustr
);
319 case BASEPROPERTY_BORDER
:
320 return Any( css::awt::VisualEffect::NONE
);
322 case BASEPROPERTY_STEP_TIME
:
323 return Any( sal_Int32(100) );
325 case BASEPROPERTY_AUTO_REPEAT
:
328 case BASEPROPERTY_IMAGE_SCALE_MODE
:
329 return Any( ImageScaleMode::NONE
);
332 return UnoControlModel::ImplGetDefaultValue( i_propertyId
);
337 ::cppu::IPropertyArrayHelper
& AnimatedImagesControlModel::getInfoHelper()
339 static UnoPropertyArrayHelper
aHelper( ImplGetPropertyIds() );
344 ::sal_Int32 SAL_CALL
AnimatedImagesControlModel::getStepTime()
346 sal_Int32
nStepTime( 100 );
347 OSL_VERIFY( getPropertyValue( GetPropertyName( BASEPROPERTY_STEP_TIME
) ) >>= nStepTime
);
352 void SAL_CALL
AnimatedImagesControlModel::setStepTime( ::sal_Int32 i_stepTime
)
354 setPropertyValue( GetPropertyName( BASEPROPERTY_STEP_TIME
), Any( i_stepTime
) );
358 sal_Bool SAL_CALL
AnimatedImagesControlModel::getAutoRepeat()
360 bool bAutoRepeat( true );
361 OSL_VERIFY( getPropertyValue( GetPropertyName( BASEPROPERTY_AUTO_REPEAT
) ) >>= bAutoRepeat
);
366 void SAL_CALL
AnimatedImagesControlModel::setAutoRepeat( sal_Bool i_autoRepeat
)
368 setPropertyValue( GetPropertyName( BASEPROPERTY_AUTO_REPEAT
), Any( i_autoRepeat
) );
372 ::sal_Int16 SAL_CALL
AnimatedImagesControlModel::getScaleMode()
374 sal_Int16
nImageScaleMode( ImageScaleMode::ANISOTROPIC
);
375 OSL_VERIFY( getPropertyValue( GetPropertyName( BASEPROPERTY_IMAGE_SCALE_MODE
) ) >>= nImageScaleMode
);
376 return nImageScaleMode
;
380 void SAL_CALL
AnimatedImagesControlModel::setScaleMode( ::sal_Int16 i_scaleMode
)
382 setPropertyValue( GetPropertyName( BASEPROPERTY_IMAGE_SCALE_MODE
), Any( i_scaleMode
) );
386 ::sal_Int32 SAL_CALL
AnimatedImagesControlModel::getImageSetCount( )
388 std::unique_lock
aGuard( m_aMutex
);
390 throw DisposedException();
392 return maImageSets
.size();
396 Sequence
< OUString
> SAL_CALL
AnimatedImagesControlModel::getImageSet( ::sal_Int32 i_index
)
398 std::unique_lock
aGuard( m_aMutex
);
400 throw DisposedException();
402 lcl_checkIndex( maImageSets
, i_index
, *this );
404 return maImageSets
[ i_index
];
408 void SAL_CALL
AnimatedImagesControlModel::insertImageSet( ::sal_Int32 i_index
, const Sequence
< OUString
>& i_imageURLs
)
410 std::unique_lock
aGuard( m_aMutex
);
413 throw DisposedException();
415 lcl_checkIndex( maImageSets
, i_index
, *this, true );
418 maImageSets
.insert( maImageSets
.begin() + i_index
, i_imageURLs
);
420 // listener notification
421 lcl_notify( aGuard
, maContainerListeners
, &XContainerListener::elementInserted
, i_index
, i_imageURLs
, *this );
425 void SAL_CALL
AnimatedImagesControlModel::replaceImageSet( ::sal_Int32 i_index
, const Sequence
< OUString
>& i_imageURLs
)
427 std::unique_lock
aGuard( m_aMutex
);
430 throw DisposedException();
432 lcl_checkIndex( maImageSets
, i_index
, *this );
435 maImageSets
[ i_index
] = i_imageURLs
;
437 // listener notification
438 lcl_notify( aGuard
, maContainerListeners
, &XContainerListener::elementReplaced
, i_index
, i_imageURLs
, *this );
442 void SAL_CALL
AnimatedImagesControlModel::removeImageSet( ::sal_Int32 i_index
)
444 std::unique_lock
aGuard( m_aMutex
);
447 throw DisposedException();
449 lcl_checkIndex( maImageSets
, i_index
, *this );
452 ::std::vector
< Sequence
< OUString
> >::iterator removalPos
= maImageSets
.begin() + i_index
;
453 Sequence
< OUString
> aRemovedElement( *removalPos
);
454 maImageSets
.erase( removalPos
);
456 // listener notification
457 lcl_notify( aGuard
, maContainerListeners
, &XContainerListener::elementRemoved
, i_index
, aRemovedElement
, *this );
461 void SAL_CALL
AnimatedImagesControlModel::addContainerListener( const Reference
< XContainerListener
>& i_listener
)
463 std::unique_lock
aGuard( m_aMutex
);
464 maContainerListeners
.addInterface( aGuard
, i_listener
);
468 void SAL_CALL
AnimatedImagesControlModel::removeContainerListener( const Reference
< XContainerListener
>& i_listener
)
470 std::unique_lock
aGuard( m_aMutex
);
471 maContainerListeners
.removeInterface( aGuard
, i_listener
);
476 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
477 org_openoffice_comp_toolkit_AnimatedImagesControl_get_implementation(
478 css::uno::XComponentContext
*,
479 css::uno::Sequence
<css::uno::Any
> const &)
481 return cppu::acquire(new AnimatedImagesControl());
484 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
485 org_openoffice_comp_toolkit_AnimatedImagesControlModel_get_implementation(
486 css::uno::XComponentContext
*context
,
487 css::uno::Sequence
<css::uno::Any
> const &)
489 return cppu::acquire(new toolkit::AnimatedImagesControlModel(context
));
492 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */