Impress Remote 1.0.5, tag sdremote-1.0.5
[LibreOffice.git] / toolkit / source / controls / animatedimages.cxx
blobcbab23368ca36d7c217b75054714f8587cd7038a
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/servicenames.hxx"
23 #include "toolkit/helper/property.hxx"
24 #include "toolkit/helper/unopropertyarrayhelper.hxx"
26 #include <com/sun/star/lang/DisposedException.hpp>
27 #include <com/sun/star/awt/VisualEffect.hpp>
28 #include <com/sun/star/awt/ImageScaleMode.hpp>
29 #include <com/sun/star/util/XModifyListener.hpp>
31 //......................................................................................................................
32 namespace toolkit
34 //......................................................................................................................
36 /** === begin UNO using === **/
37 using ::com::sun::star::uno::Reference;
38 using ::com::sun::star::uno::XInterface;
39 using ::com::sun::star::uno::UNO_QUERY;
40 using ::com::sun::star::uno::UNO_QUERY_THROW;
41 using ::com::sun::star::uno::UNO_SET_THROW;
42 using ::com::sun::star::uno::Exception;
43 using ::com::sun::star::uno::RuntimeException;
44 using ::com::sun::star::uno::Any;
45 using ::com::sun::star::uno::makeAny;
46 using ::com::sun::star::uno::Sequence;
47 using ::com::sun::star::uno::Type;
48 using ::com::sun::star::container::ContainerEvent;
49 using ::com::sun::star::container::XContainerListener;
50 using ::com::sun::star::beans::XPropertySetInfo;
51 using ::com::sun::star::lang::DisposedException;
52 using ::com::sun::star::lang::IndexOutOfBoundsException;
53 using ::com::sun::star::lang::EventObject;
54 using ::com::sun::star::awt::XControlModel;
55 using ::com::sun::star::awt::XAnimatedImages;
56 using ::com::sun::star::lang::IllegalArgumentException;
57 using ::com::sun::star::awt::XWindowPeer;
58 using ::com::sun::star::util::XModifyListener;
59 using ::com::sun::star::awt::XToolkit;
60 using ::com::sun::star::lang::XMultiServiceFactory;
61 /** === end UNO using === **/
62 namespace VisualEffect = ::com::sun::star::awt::VisualEffect;
63 namespace ImageScaleMode = ::com::sun::star::awt::ImageScaleMode;
65 //==================================================================================================================
66 //= AnimatedImagesControl
67 //==================================================================================================================
68 //------------------------------------------------------------------------------------------------------------------
69 AnimatedImagesControl::AnimatedImagesControl( Reference< XMultiServiceFactory > const & i_factory )
70 :AnimatedImagesControl_Base( i_factory )
74 //------------------------------------------------------------------------------------------------------------------
75 ::rtl::OUString AnimatedImagesControl::GetComponentServiceName()
77 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "AnimatedImages" ) );
80 //------------------------------------------------------------------------------------------------------------------
81 void SAL_CALL AnimatedImagesControl::startAnimation( ) throw (RuntimeException)
83 Reference< XAnimation > xAnimation( getPeer(), UNO_QUERY );
84 if ( xAnimation.is() )
85 xAnimation->startAnimation();
88 //------------------------------------------------------------------------------------------------------------------
89 void SAL_CALL AnimatedImagesControl::stopAnimation( ) throw (RuntimeException)
91 Reference< XAnimation > xAnimation( getPeer(), UNO_QUERY );
92 if ( xAnimation.is() )
93 xAnimation->stopAnimation();
96 //------------------------------------------------------------------------------------------------------------------
97 ::sal_Bool SAL_CALL AnimatedImagesControl::isAnimationRunning( ) throw (RuntimeException)
99 Reference< XAnimation > xAnimation( getPeer(), UNO_QUERY );
100 if ( xAnimation.is() )
101 return xAnimation->isAnimationRunning();
102 return sal_False;
105 //------------------------------------------------------------------------------------------------------------------
106 ::rtl::OUString SAL_CALL AnimatedImagesControl::getImplementationName( ) throw(RuntimeException)
108 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.comp.toolkit.AnimatedImagesControl" ) );
111 //------------------------------------------------------------------------------------------------------------------
112 Sequence< ::rtl::OUString > SAL_CALL AnimatedImagesControl::getSupportedServiceNames() throw(RuntimeException)
114 Sequence< ::rtl::OUString > aServices( AnimatedImagesControl_Base::getSupportedServiceNames() );
115 aServices.realloc( aServices.getLength() + 1 );
116 aServices[ aServices.getLength() - 1 ] = ::rtl::OUString::createFromAscii( szServiceName_AnimatedImagesControl );
117 return aServices;
120 //------------------------------------------------------------------------------------------------------------------
121 namespace
123 void lcl_updatePeer( Reference< XWindowPeer > const& i_peer, Reference< XControlModel > const& i_model )
125 const Reference< XModifyListener > xPeerModify( i_peer, UNO_QUERY );
126 if ( xPeerModify.is() )
128 EventObject aEvent;
129 aEvent.Source = i_model;
130 xPeerModify->modified( aEvent );
135 //------------------------------------------------------------------------------------------------------------------
136 sal_Bool SAL_CALL AnimatedImagesControl::setModel( const Reference< XControlModel >& i_rModel ) throw ( RuntimeException )
138 const Reference< XAnimatedImages > xOldContainer( getModel(), UNO_QUERY );
139 const Reference< XAnimatedImages > xNewContainer( i_rModel, UNO_QUERY );
141 if ( !AnimatedImagesControl_Base::setModel( i_rModel ) )
142 return sal_False;
144 if ( xOldContainer.is() )
145 xOldContainer->removeContainerListener( this );
147 if ( xNewContainer.is() )
148 xNewContainer->addContainerListener( this );
150 lcl_updatePeer( getPeer(), getModel() );
152 return sal_True;
155 //------------------------------------------------------------------------------------------------------------------
156 void SAL_CALL AnimatedImagesControl::createPeer( const Reference< XToolkit >& i_toolkit, const Reference< XWindowPeer >& i_parentPeer ) throw(RuntimeException)
158 AnimatedImagesControl_Base::createPeer( i_toolkit, i_parentPeer );
160 lcl_updatePeer( getPeer(), getModel() );
163 //------------------------------------------------------------------------------------------------------------------
164 void SAL_CALL AnimatedImagesControl::elementInserted( const ContainerEvent& i_event ) throw (RuntimeException)
166 const Reference< XContainerListener > xPeerListener( getPeer(), UNO_QUERY );
167 if ( xPeerListener.is() )
168 xPeerListener->elementInserted( i_event );
171 //------------------------------------------------------------------------------------------------------------------
172 void SAL_CALL AnimatedImagesControl::elementRemoved( const ContainerEvent& i_event ) throw (RuntimeException)
174 const Reference< XContainerListener > xPeerListener( getPeer(), UNO_QUERY );
175 if ( xPeerListener.is() )
176 xPeerListener->elementRemoved( i_event );
179 //------------------------------------------------------------------------------------------------------------------
180 void SAL_CALL AnimatedImagesControl::elementReplaced( const ContainerEvent& i_event ) throw (RuntimeException)
182 const Reference< XContainerListener > xPeerListener( getPeer(), UNO_QUERY );
183 if ( xPeerListener.is() )
184 xPeerListener->elementReplaced( i_event );
187 //------------------------------------------------------------------------------------------------------------------
188 void SAL_CALL AnimatedImagesControl::disposing( const EventObject& i_event ) throw (RuntimeException)
190 UnoControlBase::disposing( i_event );
193 //==================================================================================================================
194 //= AnimatedImagesControlModel_Data
195 //==================================================================================================================
196 struct AnimatedImagesControlModel_Data
198 ::std::vector< Sequence< ::rtl::OUString > > aImageSets;
201 namespace
203 void lcl_checkIndex( const AnimatedImagesControlModel_Data& i_data, const sal_Int32 i_index, const Reference< XInterface >& i_context,
204 const bool i_forInsert = false )
206 if ( ( i_index < 0 ) || ( size_t( i_index ) > i_data.aImageSets.size() + ( i_forInsert ? 1 : 0 ) ) )
207 throw IndexOutOfBoundsException( ::rtl::OUString(), i_context );
210 void lcl_notify( ::osl::ClearableMutexGuard& i_guard, ::cppu::OBroadcastHelper& i_broadcaseHelper,
211 void ( SAL_CALL XContainerListener::*i_notificationMethod )( const ContainerEvent& ),
212 const sal_Int32 i_accessor, const Sequence< ::rtl::OUString >& i_imageURLs, const Reference< XInterface >& i_context )
214 ::cppu::OInterfaceContainerHelper* pContainerListeners = i_broadcaseHelper.getContainer( XContainerListener::static_type() );
215 if ( pContainerListeners == NULL )
216 return;
218 ContainerEvent aEvent;
219 aEvent.Source = i_context;
220 aEvent.Accessor <<= i_accessor;
221 aEvent.Element <<= i_imageURLs;
223 i_guard.clear();
224 pContainerListeners->notifyEach( i_notificationMethod, aEvent );
228 //==================================================================================================================
229 //= AnimatedImagesControlModel
230 //==================================================================================================================
231 //------------------------------------------------------------------------------------------------------------------
232 AnimatedImagesControlModel::AnimatedImagesControlModel( Reference< XMultiServiceFactory > const & i_factory )
233 :AnimatedImagesControlModel_Base( i_factory )
234 ,m_pData( new AnimatedImagesControlModel_Data )
236 ImplRegisterProperty( BASEPROPERTY_AUTO_REPEAT );
237 ImplRegisterProperty( BASEPROPERTY_BORDER );
238 ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR );
239 ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR );
240 ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL );
241 ImplRegisterProperty( BASEPROPERTY_ENABLEVISIBLE );
242 ImplRegisterProperty( BASEPROPERTY_HELPTEXT );
243 ImplRegisterProperty( BASEPROPERTY_HELPURL );
244 ImplRegisterProperty( BASEPROPERTY_IMAGE_SCALE_MODE );
245 ImplRegisterProperty( BASEPROPERTY_STEP_TIME );
248 //------------------------------------------------------------------------------------------------------------------
249 AnimatedImagesControlModel::AnimatedImagesControlModel( const AnimatedImagesControlModel& i_copySource )
250 :AnimatedImagesControlModel_Base( i_copySource )
251 ,m_pData( new AnimatedImagesControlModel_Data( *i_copySource.m_pData ) )
255 //------------------------------------------------------------------------------------------------------------------
256 AnimatedImagesControlModel::~AnimatedImagesControlModel()
260 //------------------------------------------------------------------------------------------------------------------
261 UnoControlModel* AnimatedImagesControlModel::Clone() const
263 return new AnimatedImagesControlModel( *this );
266 //------------------------------------------------------------------------------------------------------------------
267 Reference< XPropertySetInfo > SAL_CALL AnimatedImagesControlModel::getPropertySetInfo( ) throw(RuntimeException)
269 static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
270 return xInfo;
273 //------------------------------------------------------------------------------------------------------------------
274 ::rtl::OUString SAL_CALL AnimatedImagesControlModel::getServiceName() throw(RuntimeException)
276 return ::rtl::OUString::createFromAscii( szServiceName_AnimatedImagesControlModel );
279 //------------------------------------------------------------------------------------------------------------------
280 ::rtl::OUString SAL_CALL AnimatedImagesControlModel::getImplementationName( ) throw(RuntimeException)
282 return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("org.openoffice.comp.toolkit.AnimatedImagesControlModel"));
285 //------------------------------------------------------------------------------------------------------------------
286 Sequence< ::rtl::OUString > SAL_CALL AnimatedImagesControlModel::getSupportedServiceNames() throw(RuntimeException)
288 Sequence< ::rtl::OUString > aServiceNames(2);
289 aServiceNames[0] = ::rtl::OUString::createFromAscii( szServiceName_AnimatedImagesControlModel );
290 aServiceNames[1] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.awt.UnoControlModel"));
291 return aServiceNames;
294 //------------------------------------------------------------------------------------------------------------------
295 void SAL_CALL AnimatedImagesControlModel::setFastPropertyValue_NoBroadcast( sal_Int32 i_handle, const Any& i_value ) throw (Exception)
297 switch ( i_handle )
299 case BASEPROPERTY_IMAGE_SCALE_MODE:
301 sal_Int16 nImageScaleMode( ImageScaleMode::Anisotropic );
302 OSL_VERIFY( i_value >>= nImageScaleMode ); // convertFastPropertyValue ensures that this has the proper type
303 if ( ( nImageScaleMode != ImageScaleMode::None )
304 && ( nImageScaleMode != ImageScaleMode::Isotropic )
305 && ( nImageScaleMode != ImageScaleMode::Anisotropic )
307 throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
309 break;
312 AnimatedImagesControlModel_Base::setFastPropertyValue_NoBroadcast( i_handle, i_value );
315 //------------------------------------------------------------------------------------------------------------------
316 Any AnimatedImagesControlModel::ImplGetDefaultValue( sal_uInt16 i_propertyId ) const
318 switch ( i_propertyId )
320 case BASEPROPERTY_DEFAULTCONTROL:
321 return makeAny( ::rtl::OUString::createFromAscii( szServiceName_AnimatedImagesControl ) );
323 case BASEPROPERTY_BORDER:
324 return makeAny( VisualEffect::NONE );
326 case BASEPROPERTY_STEP_TIME:
327 return makeAny( (sal_Int32) 100 );
329 case BASEPROPERTY_AUTO_REPEAT:
330 return makeAny( (sal_Bool)sal_True );
332 case BASEPROPERTY_IMAGE_SCALE_MODE:
333 return makeAny( ImageScaleMode::None );
335 default:
336 return UnoControlModel::ImplGetDefaultValue( i_propertyId );
340 //------------------------------------------------------------------------------------------------------------------
341 ::cppu::IPropertyArrayHelper& SAL_CALL AnimatedImagesControlModel::getInfoHelper()
343 static UnoPropertyArrayHelper* pHelper = NULL;
344 if ( !pHelper )
346 Sequence< sal_Int32 > aIDs = ImplGetPropertyIds();
347 pHelper = new UnoPropertyArrayHelper( aIDs );
349 return *pHelper;
352 //------------------------------------------------------------------------------------------------------------------
353 ::sal_Int32 SAL_CALL AnimatedImagesControlModel::getStepTime() throw (RuntimeException)
355 sal_Int32 nStepTime( 100 );
356 OSL_VERIFY( getPropertyValue( GetPropertyName( BASEPROPERTY_STEP_TIME ) ) >>= nStepTime );
357 return nStepTime;
360 //------------------------------------------------------------------------------------------------------------------
361 void SAL_CALL AnimatedImagesControlModel::setStepTime( ::sal_Int32 i_stepTime ) throw (RuntimeException)
363 setPropertyValue( GetPropertyName( BASEPROPERTY_STEP_TIME ), makeAny( i_stepTime ) );
366 //------------------------------------------------------------------------------------------------------------------
367 ::sal_Bool SAL_CALL AnimatedImagesControlModel::getAutoRepeat() throw (RuntimeException)
369 sal_Bool bAutoRepeat( sal_True );
370 OSL_VERIFY( getPropertyValue( GetPropertyName( BASEPROPERTY_AUTO_REPEAT ) ) >>= bAutoRepeat );
371 return bAutoRepeat;
374 //------------------------------------------------------------------------------------------------------------------
375 void SAL_CALL AnimatedImagesControlModel::setAutoRepeat( ::sal_Bool i_autoRepeat ) throw (RuntimeException)
377 setPropertyValue( GetPropertyName( BASEPROPERTY_AUTO_REPEAT ), makeAny( i_autoRepeat ) );
380 //------------------------------------------------------------------------------------------------------------------
381 ::sal_Int16 SAL_CALL AnimatedImagesControlModel::getScaleMode() throw (RuntimeException)
383 sal_Int16 nImageScaleMode( ImageScaleMode::Anisotropic );
384 OSL_VERIFY( getPropertyValue( GetPropertyName( BASEPROPERTY_IMAGE_SCALE_MODE ) ) >>= nImageScaleMode );
385 return nImageScaleMode;
388 //------------------------------------------------------------------------------------------------------------------
389 void SAL_CALL AnimatedImagesControlModel::setScaleMode( ::sal_Int16 i_scaleMode ) throw (IllegalArgumentException, RuntimeException)
391 setPropertyValue( GetPropertyName( BASEPROPERTY_IMAGE_SCALE_MODE ), makeAny( i_scaleMode ) );
394 //------------------------------------------------------------------------------------------------------------------
395 ::sal_Int32 SAL_CALL AnimatedImagesControlModel::getImageSetCount( ) throw (RuntimeException)
397 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
398 if ( GetBroadcastHelper().bDisposed || GetBroadcastHelper().bInDispose )
399 throw DisposedException();
401 return m_pData->aImageSets.size();
404 //------------------------------------------------------------------------------------------------------------------
405 Sequence< ::rtl::OUString > SAL_CALL AnimatedImagesControlModel::getImageSet( ::sal_Int32 i_index ) throw (IndexOutOfBoundsException, RuntimeException)
407 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
408 if ( GetBroadcastHelper().bDisposed || GetBroadcastHelper().bInDispose )
409 throw DisposedException();
411 lcl_checkIndex( *m_pData, i_index, *this );
413 return m_pData->aImageSets[ i_index ];
416 //------------------------------------------------------------------------------------------------------------------
417 void SAL_CALL AnimatedImagesControlModel::insertImageSet( ::sal_Int32 i_index, const Sequence< ::rtl::OUString >& i_imageURLs ) throw (IndexOutOfBoundsException, RuntimeException)
419 ::osl::ClearableMutexGuard aGuard( GetMutex() );
420 // sanity checks
421 if ( GetBroadcastHelper().bDisposed || GetBroadcastHelper().bInDispose )
422 throw DisposedException();
424 lcl_checkIndex( *m_pData, i_index, *this, true );
426 // actaul insertion
427 m_pData->aImageSets.insert( m_pData->aImageSets.begin() + i_index, i_imageURLs );
429 // listener notification
430 lcl_notify( aGuard, BrdcstHelper, &XContainerListener::elementInserted, i_index, i_imageURLs, *this );
433 //------------------------------------------------------------------------------------------------------------------
434 void SAL_CALL AnimatedImagesControlModel::replaceImageSet( ::sal_Int32 i_index, const Sequence< ::rtl::OUString >& i_imageURLs ) throw (IndexOutOfBoundsException, RuntimeException)
436 ::osl::ClearableMutexGuard aGuard( GetMutex() );
437 // sanity checks
438 if ( GetBroadcastHelper().bDisposed || GetBroadcastHelper().bInDispose )
439 throw DisposedException();
441 lcl_checkIndex( *m_pData, i_index, *this );
443 // actaul insertion
444 m_pData->aImageSets[ i_index ] = i_imageURLs;
446 // listener notification
447 lcl_notify( aGuard, BrdcstHelper, &XContainerListener::elementReplaced, i_index, i_imageURLs, *this );
450 //------------------------------------------------------------------------------------------------------------------
451 void SAL_CALL AnimatedImagesControlModel::removeImageSet( ::sal_Int32 i_index ) throw (IndexOutOfBoundsException, RuntimeException)
453 ::osl::ClearableMutexGuard aGuard( GetMutex() );
454 // sanity checks
455 if ( GetBroadcastHelper().bDisposed || GetBroadcastHelper().bInDispose )
456 throw DisposedException();
458 lcl_checkIndex( *m_pData, i_index, *this );
460 // actual removal
461 ::std::vector< Sequence< ::rtl::OUString > >::iterator removalPos = m_pData->aImageSets.begin() + i_index;
462 Sequence< ::rtl::OUString > aRemovedElement( *removalPos );
463 m_pData->aImageSets.erase( removalPos );
465 // listener notification
466 lcl_notify( aGuard, BrdcstHelper, &XContainerListener::elementRemoved, i_index, aRemovedElement, *this );
469 //------------------------------------------------------------------------------------------------------------------
470 void SAL_CALL AnimatedImagesControlModel::addContainerListener( const Reference< XContainerListener >& i_listener ) throw (RuntimeException)
472 BrdcstHelper.addListener( XContainerListener::static_type(), i_listener );
475 //------------------------------------------------------------------------------------------------------------------
476 void SAL_CALL AnimatedImagesControlModel::removeContainerListener( const Reference< XContainerListener >& i_listener ) throw (RuntimeException)
478 BrdcstHelper.removeListener( XContainerListener::static_type(), i_listener );
481 //......................................................................................................................
482 } // namespace toolkit
483 //......................................................................................................................
485 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */