Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / toolkit / source / controls / tkspinbutton.cxx
blob46e72e4066480d7eb13e4f83660e614466a2f920
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 <cppuhelper/typeprovider.hxx>
28 #include <toolkit/controls/unocontrolmodel.hxx>
29 #include <toolkit/controls/unocontrolbase.hxx>
30 #include <toolkit/helper/macros.hxx>
31 #include <toolkit/helper/property.hxx>
33 #include <helper/unopropertyarrayhelper.hxx>
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::awt;
37 using namespace ::com::sun::star::lang;
38 using namespace ::com::sun::star::beans;
40 namespace {
42 class UnoSpinButtonModel : public UnoControlModel
44 protected:
45 css::uno::Any ImplGetDefaultValue( sal_uInt16 nPropId ) const override;
46 ::cppu::IPropertyArrayHelper& SAL_CALL getInfoHelper() override;
48 public:
49 explicit UnoSpinButtonModel( const css::uno::Reference< css::uno::XComponentContext >& i_factory );
51 rtl::Reference<UnoControlModel> Clone() const override { return new UnoSpinButtonModel( *this ); }
53 // XMultiPropertySet
54 css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo( ) override;
56 // XPersistObject
57 OUString SAL_CALL getServiceName() override;
59 // XServiceInfo
60 OUString SAL_CALL getImplementationName( ) override;
61 css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
65 //= UnoSpinButtonControl
68 typedef ::cppu::ImplHelper2 < css::awt::XAdjustmentListener
69 , css::awt::XSpinValue
70 > UnoSpinButtonControl_Base;
72 class UnoSpinButtonControl :public UnoControlBase
73 ,public UnoSpinButtonControl_Base
75 private:
76 AdjustmentListenerMultiplexer maAdjustmentListeners;
78 public:
79 UnoSpinButtonControl();
80 OUString GetComponentServiceName() override;
82 DECLARE_UNO3_AGG_DEFAULTS( UnoSpinButtonControl, UnoControlBase )
83 css::uno::Any SAL_CALL queryAggregation( const css::uno::Type & rType ) override;
85 void SAL_CALL createPeer( const css::uno::Reference< css::awt::XToolkit >& Toolkit, const css::uno::Reference< css::awt::XWindowPeer >& Parent ) override;
86 void SAL_CALL disposing( const css::lang::EventObject& Source ) override { UnoControlBase::disposing( Source ); }
87 void SAL_CALL dispose( ) override;
89 // XTypeProvider
90 DECLARE_XTYPEPROVIDER()
92 // XAdjustmentListener
93 void SAL_CALL adjustmentValueChanged( const css::awt::AdjustmentEvent& rEvent ) override;
95 // XSpinValue
96 virtual void SAL_CALL addAdjustmentListener( const css::uno::Reference< css::awt::XAdjustmentListener >& listener ) override;
97 virtual void SAL_CALL removeAdjustmentListener( const css::uno::Reference< css::awt::XAdjustmentListener >& listener ) override;
98 virtual void SAL_CALL setValue( sal_Int32 value ) override;
99 virtual void SAL_CALL setValues( sal_Int32 minValue, sal_Int32 maxValue, sal_Int32 currentValue ) override;
100 virtual sal_Int32 SAL_CALL getValue( ) override;
101 virtual void SAL_CALL setMinimum( sal_Int32 minValue ) override;
102 virtual void SAL_CALL setMaximum( sal_Int32 maxValue ) override;
103 virtual sal_Int32 SAL_CALL getMinimum( ) override;
104 virtual sal_Int32 SAL_CALL getMaximum( ) override;
105 virtual void SAL_CALL setSpinIncrement( sal_Int32 spinIncrement ) override;
106 virtual sal_Int32 SAL_CALL getSpinIncrement( ) override;
107 virtual void SAL_CALL setOrientation( sal_Int32 orientation ) override;
108 virtual sal_Int32 SAL_CALL getOrientation( ) override;
110 // XServiceInfo
111 OUString SAL_CALL getImplementationName( ) override;
112 css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
116 //= UnoSpinButtonModel
119 UnoSpinButtonModel::UnoSpinButtonModel( const css::uno::Reference< css::uno::XComponentContext >& i_factory )
120 :UnoControlModel( i_factory )
122 ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR );
123 ImplRegisterProperty( BASEPROPERTY_BORDER );
124 ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR );
125 ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL );
126 ImplRegisterProperty( BASEPROPERTY_ENABLED );
127 ImplRegisterProperty( BASEPROPERTY_ENABLEVISIBLE );
128 ImplRegisterProperty( BASEPROPERTY_HELPTEXT );
129 ImplRegisterProperty( BASEPROPERTY_HELPURL );
130 ImplRegisterProperty( BASEPROPERTY_ORIENTATION );
131 ImplRegisterProperty( BASEPROPERTY_PRINTABLE );
132 ImplRegisterProperty( BASEPROPERTY_REPEAT );
133 ImplRegisterProperty( BASEPROPERTY_REPEAT_DELAY );
134 ImplRegisterProperty( BASEPROPERTY_SYMBOL_COLOR );
135 ImplRegisterProperty( BASEPROPERTY_SPINVALUE );
136 ImplRegisterProperty( BASEPROPERTY_SPINVALUE_MIN );
137 ImplRegisterProperty( BASEPROPERTY_SPINVALUE_MAX );
138 ImplRegisterProperty( BASEPROPERTY_SPININCREMENT );
139 ImplRegisterProperty( BASEPROPERTY_TABSTOP );
140 ImplRegisterProperty( BASEPROPERTY_WRITING_MODE );
141 ImplRegisterProperty( BASEPROPERTY_CONTEXT_WRITING_MODE );
145 OUString UnoSpinButtonModel::getServiceName( )
147 return OUString("com.sun.star.awt.UnoControlSpinButtonModel");
151 Any UnoSpinButtonModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
153 switch ( nPropId )
155 case BASEPROPERTY_DEFAULTCONTROL:
156 return makeAny( OUString("com.sun.star.awt.UnoControlSpinButton") );
158 case BASEPROPERTY_BORDER:
159 return makeAny( sal_Int16(0) );
161 case BASEPROPERTY_REPEAT:
162 return makeAny( true );
164 default:
165 return UnoControlModel::ImplGetDefaultValue( nPropId );
170 ::cppu::IPropertyArrayHelper& UnoSpinButtonModel::getInfoHelper()
172 static UnoPropertyArrayHelper* pHelper = nullptr;
173 if ( !pHelper )
175 Sequence<sal_Int32> aIDs = ImplGetPropertyIds();
176 pHelper = new UnoPropertyArrayHelper( aIDs );
178 return *pHelper;
182 Reference< XPropertySetInfo > UnoSpinButtonModel::getPropertySetInfo( )
184 static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
185 return xInfo;
189 OUString SAL_CALL UnoSpinButtonModel::getImplementationName( )
191 return OUString( "stardiv.Toolkit.UnoSpinButtonModel" );
195 Sequence< OUString > SAL_CALL UnoSpinButtonModel::getSupportedServiceNames()
197 Sequence< OUString > aServices( UnoControlModel::getSupportedServiceNames() );
198 aServices.realloc( aServices.getLength() + 1 );
199 aServices[ aServices.getLength() - 1 ] = "com.sun.star.awt.UnoControlSpinButtonModel";
200 return aServices;
204 //= UnoSpinButtonControl
207 UnoSpinButtonControl::UnoSpinButtonControl()
208 :UnoControlBase()
209 ,maAdjustmentListeners( *this )
214 OUString UnoSpinButtonControl::GetComponentServiceName()
216 return OUString("SpinButton");
220 Any UnoSpinButtonControl::queryAggregation( const Type & rType )
222 Any aRet = UnoControlBase::queryAggregation( rType );
223 if ( !aRet.hasValue() )
224 aRet = UnoSpinButtonControl_Base::queryInterface( rType );
225 return aRet;
229 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoSpinButtonControl, UnoControlBase, UnoSpinButtonControl_Base )
232 void UnoSpinButtonControl::dispose()
234 ::osl::ClearableMutexGuard aGuard( GetMutex() );
235 if ( maAdjustmentListeners.getLength() )
237 Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
238 if ( xSpinnable.is() )
239 xSpinnable->removeAdjustmentListener( this );
241 EventObject aDisposeEvent;
242 aDisposeEvent.Source = *this;
244 aGuard.clear();
245 maAdjustmentListeners.disposeAndClear( aDisposeEvent );
248 UnoControl::dispose();
252 OUString SAL_CALL UnoSpinButtonControl::getImplementationName( )
254 return OUString( "stardiv.Toolkit.UnoSpinButtonControl" );
258 Sequence< OUString > SAL_CALL UnoSpinButtonControl::getSupportedServiceNames()
260 Sequence< OUString > aServices( UnoControlBase::getSupportedServiceNames() );
261 aServices.realloc( aServices.getLength() + 1 );
262 aServices[ aServices.getLength() - 1 ] = "com.sun.star.awt.UnoControlSpinButton";
263 return aServices;
267 void UnoSpinButtonControl::createPeer( const Reference< XToolkit > & rxToolkit, const Reference< XWindowPeer > & rParentPeer )
269 UnoControl::createPeer( rxToolkit, rParentPeer );
271 Reference < XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
272 if ( xSpinnable.is() )
273 xSpinnable->addAdjustmentListener( this );
277 void UnoSpinButtonControl::adjustmentValueChanged( const AdjustmentEvent& rEvent )
279 switch ( rEvent.Type )
281 case AdjustmentType_ADJUST_LINE:
282 case AdjustmentType_ADJUST_PAGE:
283 case AdjustmentType_ADJUST_ABS:
284 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE ), makeAny( rEvent.Value ), false );
285 break;
286 default:
287 OSL_FAIL( "UnoSpinButtonControl::adjustmentValueChanged - unknown Type" );
290 if ( maAdjustmentListeners.getLength() )
292 AdjustmentEvent aEvent( rEvent );
293 aEvent.Source = *this;
294 maAdjustmentListeners.adjustmentValueChanged( aEvent );
299 void UnoSpinButtonControl::addAdjustmentListener( const Reference< XAdjustmentListener > & listener )
301 ::osl::MutexGuard aGuard( GetMutex() );
302 maAdjustmentListeners.addInterface( listener );
306 void UnoSpinButtonControl::removeAdjustmentListener( const Reference< XAdjustmentListener > & listener )
308 ::osl::MutexGuard aGuard( GetMutex() );
309 maAdjustmentListeners.removeInterface( listener );
313 void SAL_CALL UnoSpinButtonControl::setValue( sal_Int32 value )
315 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE ), makeAny( value ), true );
319 void SAL_CALL UnoSpinButtonControl::setValues( sal_Int32 minValue, sal_Int32 maxValue, sal_Int32 currentValue )
321 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE_MIN ), makeAny( minValue ), true );
322 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE_MAX ), makeAny( maxValue ), true );
323 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE ), makeAny( currentValue ), true );
327 sal_Int32 SAL_CALL UnoSpinButtonControl::getValue( )
329 ::osl::MutexGuard aGuard( GetMutex() );
330 sal_Int32 nValue = 0;
332 Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
333 if ( xSpinnable.is() )
334 nValue = xSpinnable->getValue();
336 return nValue;
340 void SAL_CALL UnoSpinButtonControl::setMinimum( sal_Int32 minValue )
342 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE_MIN ), makeAny( minValue ), true );
346 void SAL_CALL UnoSpinButtonControl::setMaximum( sal_Int32 maxValue )
348 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE_MAX ), makeAny( maxValue ), true );
352 sal_Int32 SAL_CALL UnoSpinButtonControl::getMinimum( )
354 ::osl::MutexGuard aGuard( GetMutex() );
355 sal_Int32 nMin = 0;
357 Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
358 if ( xSpinnable.is() )
359 nMin = xSpinnable->getMinimum();
361 return nMin;
365 sal_Int32 SAL_CALL UnoSpinButtonControl::getMaximum( )
367 ::osl::MutexGuard aGuard( GetMutex() );
368 sal_Int32 nMax = 0;
370 Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
371 if ( xSpinnable.is() )
372 nMax = xSpinnable->getMaximum();
374 return nMax;
378 void SAL_CALL UnoSpinButtonControl::setSpinIncrement( sal_Int32 spinIncrement )
380 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPININCREMENT ), makeAny( spinIncrement ), true );
384 sal_Int32 SAL_CALL UnoSpinButtonControl::getSpinIncrement( )
386 ::osl::MutexGuard aGuard( GetMutex() );
387 sal_Int32 nIncrement = 0;
389 Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
390 if ( xSpinnable.is() )
391 nIncrement = xSpinnable->getSpinIncrement();
393 return nIncrement;
397 void SAL_CALL UnoSpinButtonControl::setOrientation( sal_Int32 orientation )
399 ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_ORIENTATION ), makeAny( orientation ), true );
403 sal_Int32 SAL_CALL UnoSpinButtonControl::getOrientation( )
405 ::osl::MutexGuard aGuard( GetMutex() );
406 sal_Int32 nOrientation = ScrollBarOrientation::HORIZONTAL;
408 Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
409 if ( xSpinnable.is() )
410 nOrientation = xSpinnable->getOrientation();
412 return nOrientation;
417 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
418 stardiv_Toolkit_UnoSpinButtonModel_get_implementation(
419 css::uno::XComponentContext *context,
420 css::uno::Sequence<css::uno::Any> const &)
422 return cppu::acquire(new UnoSpinButtonModel(context));
425 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
426 stardiv_Toolkit_UnoSpinButtonControl_get_implementation(
427 css::uno::XComponentContext *,
428 css::uno::Sequence<css::uno::Any> const &)
430 return cppu::acquire(new UnoSpinButtonControl());
433 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */