Avoid potential negative array index access to cached text.
[LibreOffice.git] / toolkit / source / controls / animatedimages.cxx
blobdec4a8c5330c7e116f412e42fd205a2c48d38766
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 <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;
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() const 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()
88 OUString AnimatedImagesControl::GetComponentServiceName() const
90 return "AnimatedImages";
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();
115 return false;
119 OUString SAL_CALL AnimatedImagesControl::getImplementationName( )
121 return "org.openoffice.comp.toolkit.AnimatedImagesControl";
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";
130 return aServices;
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() )
138 EventObject aEvent;
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 ) )
150 return false;
152 if ( xOldContainer.is() )
153 xOldContainer->removeContainerListener( this );
155 if ( xNewContainer.is() )
156 xNewContainer->addContainerListener( this );
158 lcl_updatePeer( getPeer(), getModel() );
160 return true;
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 );
203 namespace toolkit {
205 namespace
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) )
219 return;
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 return new AnimatedImagesControlModel( *this );
265 Reference< css::beans::XPropertySetInfo > SAL_CALL AnimatedImagesControlModel::getPropertySetInfo( )
267 static Reference< css::beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
268 return xInfo;
272 OUString SAL_CALL AnimatedImagesControlModel::getServiceName()
274 return "com.sun.star.awt.AnimatedImagesControlModel";
278 OUString SAL_CALL AnimatedImagesControlModel::getImplementationName( )
280 return "org.openoffice.comp.toolkit.AnimatedImagesControlModel";
284 Sequence< OUString > SAL_CALL AnimatedImagesControlModel::getSupportedServiceNames()
286 return { "com.sun.star.awt.AnimatedImagesControlModel", "com.sun.star.awt.UnoControlModel" };
290 void AnimatedImagesControlModel::setFastPropertyValue_NoBroadcast( std::unique_lock<std::mutex>& rGuard, sal_Int32 i_handle, const Any& i_value )
292 switch ( i_handle )
294 case BASEPROPERTY_IMAGE_SCALE_MODE:
296 sal_Int16 nImageScaleMode( ImageScaleMode::ANISOTROPIC );
297 OSL_VERIFY( i_value >>= nImageScaleMode ); // convertFastPropertyValue ensures that this has the proper type
298 if ( ( nImageScaleMode != ImageScaleMode::NONE )
299 && ( nImageScaleMode != ImageScaleMode::ISOTROPIC )
300 && ( nImageScaleMode != ImageScaleMode::ANISOTROPIC )
302 throw IllegalArgumentException( OUString(), *this, 1 );
304 break;
307 AnimatedImagesControlModel_Base::setFastPropertyValue_NoBroadcast( rGuard, i_handle, i_value );
311 Any AnimatedImagesControlModel::ImplGetDefaultValue( sal_uInt16 i_propertyId ) const
313 switch ( i_propertyId )
315 case BASEPROPERTY_DEFAULTCONTROL:
316 return Any( OUString("com.sun.star.awt.AnimatedImagesControl") );
318 case BASEPROPERTY_BORDER:
319 return Any( css::awt::VisualEffect::NONE );
321 case BASEPROPERTY_STEP_TIME:
322 return Any( sal_Int32(100) );
324 case BASEPROPERTY_AUTO_REPEAT:
325 return Any( true );
327 case BASEPROPERTY_IMAGE_SCALE_MODE:
328 return Any( ImageScaleMode::NONE );
330 default:
331 return UnoControlModel::ImplGetDefaultValue( i_propertyId );
336 ::cppu::IPropertyArrayHelper& AnimatedImagesControlModel::getInfoHelper()
338 static UnoPropertyArrayHelper aHelper( ImplGetPropertyIds() );
339 return aHelper;
343 ::sal_Int32 SAL_CALL AnimatedImagesControlModel::getStepTime()
345 sal_Int32 nStepTime( 100 );
346 OSL_VERIFY( getPropertyValue( GetPropertyName( BASEPROPERTY_STEP_TIME ) ) >>= nStepTime );
347 return nStepTime;
351 void SAL_CALL AnimatedImagesControlModel::setStepTime( ::sal_Int32 i_stepTime )
353 setPropertyValue( GetPropertyName( BASEPROPERTY_STEP_TIME ), Any( i_stepTime ) );
357 sal_Bool SAL_CALL AnimatedImagesControlModel::getAutoRepeat()
359 bool bAutoRepeat( true );
360 OSL_VERIFY( getPropertyValue( GetPropertyName( BASEPROPERTY_AUTO_REPEAT ) ) >>= bAutoRepeat );
361 return bAutoRepeat;
365 void SAL_CALL AnimatedImagesControlModel::setAutoRepeat( sal_Bool i_autoRepeat )
367 setPropertyValue( GetPropertyName( BASEPROPERTY_AUTO_REPEAT ), Any( i_autoRepeat ) );
371 ::sal_Int16 SAL_CALL AnimatedImagesControlModel::getScaleMode()
373 sal_Int16 nImageScaleMode( ImageScaleMode::ANISOTROPIC );
374 OSL_VERIFY( getPropertyValue( GetPropertyName( BASEPROPERTY_IMAGE_SCALE_MODE ) ) >>= nImageScaleMode );
375 return nImageScaleMode;
379 void SAL_CALL AnimatedImagesControlModel::setScaleMode( ::sal_Int16 i_scaleMode )
381 setPropertyValue( GetPropertyName( BASEPROPERTY_IMAGE_SCALE_MODE ), Any( i_scaleMode ) );
385 ::sal_Int32 SAL_CALL AnimatedImagesControlModel::getImageSetCount( )
387 std::unique_lock aGuard( m_aMutex );
388 if ( m_bDisposed )
389 throw DisposedException();
391 return maImageSets.size();
395 Sequence< OUString > SAL_CALL AnimatedImagesControlModel::getImageSet( ::sal_Int32 i_index )
397 std::unique_lock aGuard( m_aMutex );
398 if ( m_bDisposed )
399 throw DisposedException();
401 lcl_checkIndex( maImageSets, i_index, *this );
403 return maImageSets[ i_index ];
407 void SAL_CALL AnimatedImagesControlModel::insertImageSet( ::sal_Int32 i_index, const Sequence< OUString >& i_imageURLs )
409 std::unique_lock aGuard( m_aMutex );
410 // sanity checks
411 if ( m_bDisposed )
412 throw DisposedException();
414 lcl_checkIndex( maImageSets, i_index, *this, true );
416 // actual insertion
417 maImageSets.insert( maImageSets.begin() + i_index, i_imageURLs );
419 // listener notification
420 lcl_notify( aGuard, maContainerListeners, &XContainerListener::elementInserted, i_index, i_imageURLs, *this );
424 void SAL_CALL AnimatedImagesControlModel::replaceImageSet( ::sal_Int32 i_index, const Sequence< OUString >& i_imageURLs )
426 std::unique_lock aGuard( m_aMutex );
427 // sanity checks
428 if ( m_bDisposed )
429 throw DisposedException();
431 lcl_checkIndex( maImageSets, i_index, *this );
433 // actual insertion
434 maImageSets[ i_index ] = i_imageURLs;
436 // listener notification
437 lcl_notify( aGuard, maContainerListeners, &XContainerListener::elementReplaced, i_index, i_imageURLs, *this );
441 void SAL_CALL AnimatedImagesControlModel::removeImageSet( ::sal_Int32 i_index )
443 std::unique_lock aGuard( m_aMutex );
444 // sanity checks
445 if ( m_bDisposed )
446 throw DisposedException();
448 lcl_checkIndex( maImageSets, i_index, *this );
450 // actual removal
451 ::std::vector< Sequence< OUString > >::iterator removalPos = maImageSets.begin() + i_index;
452 Sequence< OUString > aRemovedElement( *removalPos );
453 maImageSets.erase( removalPos );
455 // listener notification
456 lcl_notify( aGuard, maContainerListeners, &XContainerListener::elementRemoved, i_index, aRemovedElement, *this );
460 void SAL_CALL AnimatedImagesControlModel::addContainerListener( const Reference< XContainerListener >& i_listener )
462 std::unique_lock aGuard( m_aMutex );
463 maContainerListeners.addInterface( aGuard, i_listener );
467 void SAL_CALL AnimatedImagesControlModel::removeContainerListener( const Reference< XContainerListener >& i_listener )
469 std::unique_lock aGuard( m_aMutex );
470 maContainerListeners.removeInterface( aGuard, i_listener );
475 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
476 org_openoffice_comp_toolkit_AnimatedImagesControl_get_implementation(
477 css::uno::XComponentContext *,
478 css::uno::Sequence<css::uno::Any> const &)
480 return cppu::acquire(new AnimatedImagesControl());
483 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
484 org_openoffice_comp_toolkit_AnimatedImagesControlModel_get_implementation(
485 css::uno::XComponentContext *context,
486 css::uno::Sequence<css::uno::Any> const &)
488 return cppu::acquire(new toolkit::AnimatedImagesControlModel(context));
491 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */