Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / toolkit / source / controls / animatedimages.cxx
bloba9cee3921d5a7566d0a74dd52c216ad764c3c7a6
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 .
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;
47 namespace {
49 typedef ::cppu::AggImplInheritanceHelper2 < UnoControlBase
50 , css::awt::XAnimation
51 , css::container::XContainerListener
52 > AnimatedImagesControl_Base;
54 class AnimatedImagesControl : public AnimatedImagesControl_Base
56 public:
57 AnimatedImagesControl();
58 OUString GetComponentServiceName() override;
60 // XAnimation
61 virtual void SAL_CALL startAnimation( ) override;
62 virtual void SAL_CALL stopAnimation( ) override;
63 virtual sal_Bool SAL_CALL isAnimationRunning( ) override;
65 // XServiceInfo
66 OUString SAL_CALL getImplementationName( ) override;
67 css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
69 // XControl
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;
74 // XContainerListener
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;
79 // XEventListener
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();
116 return false;
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";
131 return aServices;
134 void lcl_updatePeer( Reference< XWindowPeer > const& i_peer, Reference< XControlModel > const& i_model )
136 const Reference< css::util::XModifyListener > xPeerModify( i_peer, UNO_QUERY );
137 if ( xPeerModify.is() )
139 EventObject aEvent;
140 aEvent.Source = i_model;
141 xPeerModify->modified( aEvent );
145 sal_Bool SAL_CALL AnimatedImagesControl::setModel( const Reference< XControlModel >& i_rModel )
147 const Reference< XAnimatedImages > xOldContainer( getModel(), UNO_QUERY );
148 const Reference< XAnimatedImages > xNewContainer( i_rModel, UNO_QUERY );
150 if ( !AnimatedImagesControl_Base::setModel( i_rModel ) )
151 return false;
153 if ( xOldContainer.is() )
154 xOldContainer->removeContainerListener( this );
156 if ( xNewContainer.is() )
157 xNewContainer->addContainerListener( this );
159 lcl_updatePeer( getPeer(), getModel() );
161 return true;
165 void SAL_CALL AnimatedImagesControl::createPeer( const Reference< XToolkit >& i_toolkit, const Reference< XWindowPeer >& i_parentPeer )
167 AnimatedImagesControl_Base::createPeer( i_toolkit, i_parentPeer );
169 lcl_updatePeer( getPeer(), getModel() );
173 void SAL_CALL AnimatedImagesControl::elementInserted( const ContainerEvent& i_event )
175 const Reference< XContainerListener > xPeerListener( getPeer(), UNO_QUERY );
176 if ( xPeerListener.is() )
177 xPeerListener->elementInserted( i_event );
181 void SAL_CALL AnimatedImagesControl::elementRemoved( const ContainerEvent& i_event )
183 const Reference< XContainerListener > xPeerListener( getPeer(), UNO_QUERY );
184 if ( xPeerListener.is() )
185 xPeerListener->elementRemoved( i_event );
189 void SAL_CALL AnimatedImagesControl::elementReplaced( const ContainerEvent& i_event )
191 const Reference< XContainerListener > xPeerListener( getPeer(), UNO_QUERY );
192 if ( xPeerListener.is() )
193 xPeerListener->elementReplaced( i_event );
197 void SAL_CALL AnimatedImagesControl::disposing( const EventObject& i_event )
199 UnoControlBase::disposing( i_event );
204 namespace toolkit {
206 struct AnimatedImagesControlModel_Data
208 ::std::vector< Sequence< OUString > > aImageSets;
211 namespace
213 void lcl_checkIndex( const AnimatedImagesControlModel_Data& i_data, const sal_Int32 i_index, const Reference< XInterface >& i_context,
214 const bool i_forInsert = false )
216 if ( ( i_index < 0 ) || ( size_t( i_index ) > i_data.aImageSets.size() + ( i_forInsert ? 1 : 0 ) ) )
217 throw IndexOutOfBoundsException( OUString(), i_context );
220 void lcl_notify( ::osl::ClearableMutexGuard& i_guard, ::cppu::OBroadcastHelper const & i_broadcaseHelper,
221 void ( SAL_CALL XContainerListener::*i_notificationMethod )( const ContainerEvent& ),
222 const sal_Int32 i_accessor, const Sequence< OUString >& i_imageURLs, const Reference< XInterface >& i_context )
224 ::cppu::OInterfaceContainerHelper* pContainerListeners = i_broadcaseHelper.getContainer( cppu::UnoType<XContainerListener>::get() );
225 if ( pContainerListeners == nullptr )
226 return;
228 ContainerEvent aEvent;
229 aEvent.Source = i_context;
230 aEvent.Accessor <<= i_accessor;
231 aEvent.Element <<= i_imageURLs;
233 i_guard.clear();
234 pContainerListeners->notifyEach( i_notificationMethod, aEvent );
239 AnimatedImagesControlModel::AnimatedImagesControlModel( Reference< css::uno::XComponentContext > const & i_factory )
240 :AnimatedImagesControlModel_Base( i_factory )
241 ,m_xData( new AnimatedImagesControlModel_Data )
243 ImplRegisterProperty( BASEPROPERTY_AUTO_REPEAT );
244 ImplRegisterProperty( BASEPROPERTY_BORDER );
245 ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR );
246 ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR );
247 ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL );
248 ImplRegisterProperty( BASEPROPERTY_ENABLEVISIBLE );
249 ImplRegisterProperty( BASEPROPERTY_HELPTEXT );
250 ImplRegisterProperty( BASEPROPERTY_HELPURL );
251 ImplRegisterProperty( BASEPROPERTY_IMAGE_SCALE_MODE );
252 ImplRegisterProperty( BASEPROPERTY_STEP_TIME );
256 AnimatedImagesControlModel::AnimatedImagesControlModel( const AnimatedImagesControlModel& i_copySource )
257 :AnimatedImagesControlModel_Base( i_copySource )
258 ,m_xData( new AnimatedImagesControlModel_Data( *i_copySource.m_xData ) )
263 AnimatedImagesControlModel::~AnimatedImagesControlModel()
268 rtl::Reference<UnoControlModel> AnimatedImagesControlModel::Clone() const
270 return new AnimatedImagesControlModel( *this );
274 Reference< css::beans::XPropertySetInfo > SAL_CALL AnimatedImagesControlModel::getPropertySetInfo( )
276 static Reference< css::beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
277 return xInfo;
281 OUString SAL_CALL AnimatedImagesControlModel::getServiceName()
283 return OUString("com.sun.star.awt.AnimatedImagesControlModel");
287 OUString SAL_CALL AnimatedImagesControlModel::getImplementationName( )
289 return OUString("org.openoffice.comp.toolkit.AnimatedImagesControlModel");
293 Sequence< OUString > SAL_CALL AnimatedImagesControlModel::getSupportedServiceNames()
295 Sequence< OUString > aServiceNames(2);
296 aServiceNames[0] = "com.sun.star.awt.AnimatedImagesControlModel";
297 aServiceNames[1] = "com.sun.star.awt.UnoControlModel";
298 return aServiceNames;
302 void SAL_CALL AnimatedImagesControlModel::setFastPropertyValue_NoBroadcast( sal_Int32 i_handle, const Any& i_value )
304 switch ( i_handle )
306 case BASEPROPERTY_IMAGE_SCALE_MODE:
308 sal_Int16 nImageScaleMode( ImageScaleMode::ANISOTROPIC );
309 OSL_VERIFY( i_value >>= nImageScaleMode ); // convertFastPropertyValue ensures that this has the proper type
310 if ( ( nImageScaleMode != ImageScaleMode::NONE )
311 && ( nImageScaleMode != ImageScaleMode::ISOTROPIC )
312 && ( nImageScaleMode != ImageScaleMode::ANISOTROPIC )
314 throw IllegalArgumentException( OUString(), *this, 1 );
316 break;
319 AnimatedImagesControlModel_Base::setFastPropertyValue_NoBroadcast( i_handle, i_value );
323 Any AnimatedImagesControlModel::ImplGetDefaultValue( sal_uInt16 i_propertyId ) const
325 switch ( i_propertyId )
327 case BASEPROPERTY_DEFAULTCONTROL:
328 return makeAny( OUString("com.sun.star.awt.AnimatedImagesControl") );
330 case BASEPROPERTY_BORDER:
331 return makeAny( css::awt::VisualEffect::NONE );
333 case BASEPROPERTY_STEP_TIME:
334 return makeAny( sal_Int32(100) );
336 case BASEPROPERTY_AUTO_REPEAT:
337 return makeAny( true );
339 case BASEPROPERTY_IMAGE_SCALE_MODE:
340 return makeAny( ImageScaleMode::NONE );
342 default:
343 return UnoControlModel::ImplGetDefaultValue( i_propertyId );
348 ::cppu::IPropertyArrayHelper& SAL_CALL AnimatedImagesControlModel::getInfoHelper()
350 static UnoPropertyArrayHelper* pHelper = nullptr;
351 if ( !pHelper )
353 Sequence< sal_Int32 > aIDs = ImplGetPropertyIds();
354 pHelper = new UnoPropertyArrayHelper( aIDs );
356 return *pHelper;
360 ::sal_Int32 SAL_CALL AnimatedImagesControlModel::getStepTime()
362 sal_Int32 nStepTime( 100 );
363 OSL_VERIFY( getPropertyValue( GetPropertyName( BASEPROPERTY_STEP_TIME ) ) >>= nStepTime );
364 return nStepTime;
368 void SAL_CALL AnimatedImagesControlModel::setStepTime( ::sal_Int32 i_stepTime )
370 setPropertyValue( GetPropertyName( BASEPROPERTY_STEP_TIME ), makeAny( i_stepTime ) );
374 sal_Bool SAL_CALL AnimatedImagesControlModel::getAutoRepeat()
376 bool bAutoRepeat( true );
377 OSL_VERIFY( getPropertyValue( GetPropertyName( BASEPROPERTY_AUTO_REPEAT ) ) >>= bAutoRepeat );
378 return bAutoRepeat;
382 void SAL_CALL AnimatedImagesControlModel::setAutoRepeat( sal_Bool i_autoRepeat )
384 setPropertyValue( GetPropertyName( BASEPROPERTY_AUTO_REPEAT ), makeAny( i_autoRepeat ) );
388 ::sal_Int16 SAL_CALL AnimatedImagesControlModel::getScaleMode()
390 sal_Int16 nImageScaleMode( ImageScaleMode::ANISOTROPIC );
391 OSL_VERIFY( getPropertyValue( GetPropertyName( BASEPROPERTY_IMAGE_SCALE_MODE ) ) >>= nImageScaleMode );
392 return nImageScaleMode;
396 void SAL_CALL AnimatedImagesControlModel::setScaleMode( ::sal_Int16 i_scaleMode )
398 setPropertyValue( GetPropertyName( BASEPROPERTY_IMAGE_SCALE_MODE ), makeAny( i_scaleMode ) );
402 ::sal_Int32 SAL_CALL AnimatedImagesControlModel::getImageSetCount( )
404 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
405 if ( GetBroadcastHelper().bDisposed || GetBroadcastHelper().bInDispose )
406 throw DisposedException();
408 return m_xData->aImageSets.size();
412 Sequence< OUString > SAL_CALL AnimatedImagesControlModel::getImageSet( ::sal_Int32 i_index )
414 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
415 if ( GetBroadcastHelper().bDisposed || GetBroadcastHelper().bInDispose )
416 throw DisposedException();
418 lcl_checkIndex( *m_xData, i_index, *this );
420 return m_xData->aImageSets[ i_index ];
424 void SAL_CALL AnimatedImagesControlModel::insertImageSet( ::sal_Int32 i_index, const Sequence< OUString >& i_imageURLs )
426 ::osl::ClearableMutexGuard aGuard( GetMutex() );
427 // sanity checks
428 if ( GetBroadcastHelper().bDisposed || GetBroadcastHelper().bInDispose )
429 throw DisposedException();
431 lcl_checkIndex( *m_xData, i_index, *this, true );
433 // actual insertion
434 m_xData->aImageSets.insert( m_xData->aImageSets.begin() + i_index, i_imageURLs );
436 // listener notification
437 lcl_notify( aGuard, BrdcstHelper, &XContainerListener::elementInserted, i_index, i_imageURLs, *this );
441 void SAL_CALL AnimatedImagesControlModel::replaceImageSet( ::sal_Int32 i_index, const Sequence< OUString >& i_imageURLs )
443 ::osl::ClearableMutexGuard aGuard( GetMutex() );
444 // sanity checks
445 if ( GetBroadcastHelper().bDisposed || GetBroadcastHelper().bInDispose )
446 throw DisposedException();
448 lcl_checkIndex( *m_xData, i_index, *this );
450 // actual insertion
451 m_xData->aImageSets[ i_index ] = i_imageURLs;
453 // listener notification
454 lcl_notify( aGuard, BrdcstHelper, &XContainerListener::elementReplaced, i_index, i_imageURLs, *this );
458 void SAL_CALL AnimatedImagesControlModel::removeImageSet( ::sal_Int32 i_index )
460 ::osl::ClearableMutexGuard aGuard( GetMutex() );
461 // sanity checks
462 if ( GetBroadcastHelper().bDisposed || GetBroadcastHelper().bInDispose )
463 throw DisposedException();
465 lcl_checkIndex( *m_xData, i_index, *this );
467 // actual removal
468 ::std::vector< Sequence< OUString > >::iterator removalPos = m_xData->aImageSets.begin() + i_index;
469 Sequence< OUString > aRemovedElement( *removalPos );
470 m_xData->aImageSets.erase( removalPos );
472 // listener notification
473 lcl_notify( aGuard, BrdcstHelper, &XContainerListener::elementRemoved, i_index, aRemovedElement, *this );
477 void SAL_CALL AnimatedImagesControlModel::addContainerListener( const Reference< XContainerListener >& i_listener )
479 BrdcstHelper.addListener( cppu::UnoType<XContainerListener>::get(), i_listener );
483 void SAL_CALL AnimatedImagesControlModel::removeContainerListener( const Reference< XContainerListener >& i_listener )
485 BrdcstHelper.removeListener( cppu::UnoType<XContainerListener>::get(), i_listener );
490 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
491 org_openoffice_comp_toolkit_AnimatedImagesControl_get_implementation(
492 css::uno::XComponentContext *,
493 css::uno::Sequence<css::uno::Any> const &)
495 return cppu::acquire(new AnimatedImagesControl());
498 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
499 org_openoffice_comp_toolkit_AnimatedImagesControlModel_get_implementation(
500 css::uno::XComponentContext *context,
501 css::uno::Sequence<css::uno::Any> const &)
503 return cppu::acquire(new toolkit::AnimatedImagesControlModel(context));
506 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */