Avoid potential negative array index access to cached text.
[LibreOffice.git] / toolkit / source / controls / tkspinbutton.cxx
blob59dad491c59b063a4dcfaa95f7e153c4a6873235
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 .
20 #include <com/sun/star/awt/ScrollBarOrientation.hpp>
21 #include <com/sun/star/awt/XSpinValue.hpp>
22 #include <com/sun/star/awt/XAdjustmentListener.hpp>
23 #include <com/sun/star/uno/XComponentContext.hpp>
25 #include <comphelper/uno3.hxx>
26 #include <cppuhelper/implbase2.hxx>
27 #include <toolkit/controls/unocontrolmodel.hxx>
28 #include <toolkit/controls/unocontrolbase.hxx>
29 #include <helper/property.hxx>
31 #include <helper/unopropertyarrayhelper.hxx>
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::awt;
35 using namespace ::com::sun::star::lang;
36 using namespace ::com::sun::star::beans;
38 namespace {
40 class UnoSpinButtonModel : public UnoControlModel
42 protected:
43 css::uno::Any ImplGetDefaultValue( sal_uInt16 nPropId ) const override;
44 ::cppu::IPropertyArrayHelper& getInfoHelper() override;
46 public:
47 explicit UnoSpinButtonModel( const css::uno::Reference< css::uno::XComponentContext >& i_factory );
48 UnoSpinButtonModel(const UnoSpinButtonModel & rOther) : UnoControlModel(rOther) {}
50 rtl::Reference<UnoControlModel> Clone() const override { return new UnoSpinButtonModel( *this ); }
52 // XMultiPropertySet
53 css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo( ) override;
55 // XPersistObject
56 OUString SAL_CALL getServiceName() override;
58 // XServiceInfo
59 OUString SAL_CALL getImplementationName( ) override;
60 css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
64 //= UnoSpinButtonControl
67 typedef ::cppu::ImplHelper2 < css::awt::XAdjustmentListener
68 , css::awt::XSpinValue
69 > UnoSpinButtonControl_Base;
71 class UnoSpinButtonControl :public UnoControlBase
72 ,public UnoSpinButtonControl_Base
74 private:
75 AdjustmentListenerMultiplexer maAdjustmentListeners;
77 public:
78 UnoSpinButtonControl();
79 OUString GetComponentServiceName() const override;
81 DECLARE_UNO3_AGG_DEFAULTS( UnoSpinButtonControl, UnoControlBase )
82 css::uno::Any SAL_CALL queryAggregation( const css::uno::Type & rType ) override;
84 void SAL_CALL createPeer( const css::uno::Reference< css::awt::XToolkit >& Toolkit, const css::uno::Reference< css::awt::XWindowPeer >& Parent ) override;
85 void SAL_CALL disposing( const css::lang::EventObject& Source ) override { UnoControlBase::disposing( Source ); }
86 void SAL_CALL dispose( ) override;
88 // XTypeProvider
89 DECLARE_XTYPEPROVIDER()
91 // XAdjustmentListener
92 void SAL_CALL adjustmentValueChanged( const css::awt::AdjustmentEvent& rEvent ) override;
94 // XSpinValue
95 virtual void SAL_CALL addAdjustmentListener( const css::uno::Reference< css::awt::XAdjustmentListener >& listener ) override;
96 virtual void SAL_CALL removeAdjustmentListener( const css::uno::Reference< css::awt::XAdjustmentListener >& listener ) override;
97 virtual void SAL_CALL setValue( sal_Int32 value ) override;
98 virtual void SAL_CALL setValues( sal_Int32 minValue, sal_Int32 maxValue, sal_Int32 currentValue ) override;
99 virtual sal_Int32 SAL_CALL getValue( ) override;
100 virtual void SAL_CALL setMinimum( sal_Int32 minValue ) override;
101 virtual void SAL_CALL setMaximum( sal_Int32 maxValue ) override;
102 virtual sal_Int32 SAL_CALL getMinimum( ) override;
103 virtual sal_Int32 SAL_CALL getMaximum( ) override;
104 virtual void SAL_CALL setSpinIncrement( sal_Int32 spinIncrement ) override;
105 virtual sal_Int32 SAL_CALL getSpinIncrement( ) override;
106 virtual void SAL_CALL setOrientation( sal_Int32 orientation ) override;
107 virtual sal_Int32 SAL_CALL getOrientation( ) override;
109 // XServiceInfo
110 OUString SAL_CALL getImplementationName( ) override;
111 css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
115 //= UnoSpinButtonModel
118 UnoSpinButtonModel::UnoSpinButtonModel( const css::uno::Reference< css::uno::XComponentContext >& i_factory )
119 :UnoControlModel( i_factory )
121 ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR );
122 ImplRegisterProperty( BASEPROPERTY_BORDER );
123 ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR );
124 ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL );
125 ImplRegisterProperty( BASEPROPERTY_ENABLED );
126 ImplRegisterProperty( BASEPROPERTY_ENABLEVISIBLE );
127 ImplRegisterProperty( BASEPROPERTY_HELPTEXT );
128 ImplRegisterProperty( BASEPROPERTY_HELPURL );
129 ImplRegisterProperty( BASEPROPERTY_ORIENTATION );
130 ImplRegisterProperty( BASEPROPERTY_PRINTABLE );
131 ImplRegisterProperty( BASEPROPERTY_REPEAT );
132 ImplRegisterProperty( BASEPROPERTY_REPEAT_DELAY );
133 ImplRegisterProperty( BASEPROPERTY_SYMBOL_COLOR );
134 ImplRegisterProperty( BASEPROPERTY_SPINVALUE );
135 ImplRegisterProperty( BASEPROPERTY_SPINVALUE_MIN );
136 ImplRegisterProperty( BASEPROPERTY_SPINVALUE_MAX );
137 ImplRegisterProperty( BASEPROPERTY_SPININCREMENT );
138 ImplRegisterProperty( BASEPROPERTY_TABSTOP );
139 ImplRegisterProperty( BASEPROPERTY_WRITING_MODE );
140 ImplRegisterProperty( BASEPROPERTY_CONTEXT_WRITING_MODE );
144 OUString UnoSpinButtonModel::getServiceName( )
146 return "com.sun.star.awt.UnoControlSpinButtonModel";
150 Any UnoSpinButtonModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
152 switch ( nPropId )
154 case BASEPROPERTY_DEFAULTCONTROL:
155 return Any( OUString("com.sun.star.awt.UnoControlSpinButton") );
157 case BASEPROPERTY_BORDER:
158 return Any( sal_Int16(0) );
160 case BASEPROPERTY_REPEAT:
161 return Any( true );
163 default:
164 return UnoControlModel::ImplGetDefaultValue( nPropId );
169 ::cppu::IPropertyArrayHelper& UnoSpinButtonModel::getInfoHelper()
171 static UnoPropertyArrayHelper aHelper( ImplGetPropertyIds() );
172 return aHelper;
176 Reference< XPropertySetInfo > UnoSpinButtonModel::getPropertySetInfo( )
178 static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
179 return xInfo;
183 OUString SAL_CALL UnoSpinButtonModel::getImplementationName( )
185 return "stardiv.Toolkit.UnoSpinButtonModel";
189 Sequence< OUString > SAL_CALL UnoSpinButtonModel::getSupportedServiceNames()
191 const css::uno::Sequence<OUString> vals { "com.sun.star.awt.UnoControlSpinButtonModel" };
192 return comphelper::concatSequences( UnoControlModel::getSupportedServiceNames(), vals );
196 //= UnoSpinButtonControl
199 UnoSpinButtonControl::UnoSpinButtonControl()
200 :maAdjustmentListeners( *this )
205 OUString UnoSpinButtonControl::GetComponentServiceName() const
207 return "SpinButton";
211 Any UnoSpinButtonControl::queryAggregation( const Type & rType )
213 Any aRet = UnoControlBase::queryAggregation( rType );
214 if ( !aRet.hasValue() )
215 aRet = UnoSpinButtonControl_Base::queryInterface( rType );
216 return aRet;
220 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoSpinButtonControl, UnoControlBase, UnoSpinButtonControl_Base )
223 void UnoSpinButtonControl::dispose()
225 ::osl::ClearableMutexGuard aGuard( GetMutex() );
226 if ( maAdjustmentListeners.getLength() )
228 Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
229 if ( xSpinnable.is() )
230 xSpinnable->removeAdjustmentListener( this );
232 EventObject aDisposeEvent;
233 aDisposeEvent.Source = *this;
235 aGuard.clear();
236 maAdjustmentListeners.disposeAndClear( aDisposeEvent );
239 UnoControl::dispose();
243 OUString SAL_CALL UnoSpinButtonControl::getImplementationName( )
245 return "stardiv.Toolkit.UnoSpinButtonControl";
249 Sequence< OUString > SAL_CALL UnoSpinButtonControl::getSupportedServiceNames()
251 const css::uno::Sequence<OUString> vals { "com.sun.star.awt.UnoControlSpinButton" };
252 return comphelper::concatSequences( UnoControlBase::getSupportedServiceNames(), vals );
256 void UnoSpinButtonControl::createPeer( const Reference< XToolkit > & rxToolkit, const Reference< XWindowPeer > & rParentPeer )
258 UnoControl::createPeer( rxToolkit, rParentPeer );
260 Reference < XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
261 if ( xSpinnable.is() )
262 xSpinnable->addAdjustmentListener( this );
266 void UnoSpinButtonControl::adjustmentValueChanged( const AdjustmentEvent& rEvent )
268 switch ( rEvent.Type )
270 case AdjustmentType_ADJUST_LINE:
271 case AdjustmentType_ADJUST_PAGE:
272 case AdjustmentType_ADJUST_ABS:
273 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE ), Any( rEvent.Value ), false );
274 break;
275 default:
276 OSL_FAIL( "UnoSpinButtonControl::adjustmentValueChanged - unknown Type" );
279 if ( maAdjustmentListeners.getLength() )
281 AdjustmentEvent aEvent( rEvent );
282 aEvent.Source = *this;
283 maAdjustmentListeners.adjustmentValueChanged( aEvent );
288 void UnoSpinButtonControl::addAdjustmentListener( const Reference< XAdjustmentListener > & listener )
290 ::osl::MutexGuard aGuard( GetMutex() );
291 maAdjustmentListeners.addInterface( listener );
295 void UnoSpinButtonControl::removeAdjustmentListener( const Reference< XAdjustmentListener > & listener )
297 ::osl::MutexGuard aGuard( GetMutex() );
298 maAdjustmentListeners.removeInterface( listener );
302 void SAL_CALL UnoSpinButtonControl::setValue( sal_Int32 value )
304 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE ), Any( value ), true );
308 void SAL_CALL UnoSpinButtonControl::setValues( sal_Int32 minValue, sal_Int32 maxValue, sal_Int32 currentValue )
310 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE_MIN ), Any( minValue ), true );
311 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE_MAX ), Any( maxValue ), true );
312 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE ), Any( currentValue ), true );
316 sal_Int32 SAL_CALL UnoSpinButtonControl::getValue( )
318 ::osl::MutexGuard aGuard( GetMutex() );
319 sal_Int32 nValue = 0;
321 Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
322 if ( xSpinnable.is() )
323 nValue = xSpinnable->getValue();
325 return nValue;
329 void SAL_CALL UnoSpinButtonControl::setMinimum( sal_Int32 minValue )
331 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE_MIN ), Any( minValue ), true );
335 void SAL_CALL UnoSpinButtonControl::setMaximum( sal_Int32 maxValue )
337 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE_MAX ), Any( maxValue ), true );
341 sal_Int32 SAL_CALL UnoSpinButtonControl::getMinimum( )
343 ::osl::MutexGuard aGuard( GetMutex() );
344 sal_Int32 nMin = 0;
346 Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
347 if ( xSpinnable.is() )
348 nMin = xSpinnable->getMinimum();
350 return nMin;
354 sal_Int32 SAL_CALL UnoSpinButtonControl::getMaximum( )
356 ::osl::MutexGuard aGuard( GetMutex() );
357 sal_Int32 nMax = 0;
359 Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
360 if ( xSpinnable.is() )
361 nMax = xSpinnable->getMaximum();
363 return nMax;
367 void SAL_CALL UnoSpinButtonControl::setSpinIncrement( sal_Int32 spinIncrement )
369 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPININCREMENT ), Any( spinIncrement ), true );
373 sal_Int32 SAL_CALL UnoSpinButtonControl::getSpinIncrement( )
375 ::osl::MutexGuard aGuard( GetMutex() );
376 sal_Int32 nIncrement = 0;
378 Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
379 if ( xSpinnable.is() )
380 nIncrement = xSpinnable->getSpinIncrement();
382 return nIncrement;
386 void SAL_CALL UnoSpinButtonControl::setOrientation( sal_Int32 orientation )
388 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_ORIENTATION ), Any( orientation ), true );
392 sal_Int32 SAL_CALL UnoSpinButtonControl::getOrientation( )
394 ::osl::MutexGuard aGuard( GetMutex() );
395 sal_Int32 nOrientation = ScrollBarOrientation::HORIZONTAL;
397 Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
398 if ( xSpinnable.is() )
399 nOrientation = xSpinnable->getOrientation();
401 return nOrientation;
406 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
407 stardiv_Toolkit_UnoSpinButtonModel_get_implementation(
408 css::uno::XComponentContext *context,
409 css::uno::Sequence<css::uno::Any> const &)
411 return cppu::acquire(new UnoSpinButtonModel(context));
414 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
415 stardiv_Toolkit_UnoSpinButtonControl_get_implementation(
416 css::uno::XComponentContext *,
417 css::uno::Sequence<css::uno::Any> const &)
419 return cppu::acquire(new UnoSpinButtonControl());
422 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */