1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_toolkit.hxx"
30 #include "toolkit/controls/tkspinbutton.hxx"
31 #include "toolkit/helper/property.hxx"
32 #include "toolkit/helper/unopropertyarrayhelper.hxx"
33 #include <com/sun/star/awt/ScrollBarOrientation.hpp>
34 
35 
36 #include <cppuhelper/typeprovider.hxx>
37 #include <tools/debug.hxx>
38 
39 //........................................................................
40 namespace toolkit
41 {
42 //........................................................................
43 
44     using namespace ::com::sun::star::uno;
45     using namespace ::com::sun::star::awt;
46     using namespace ::com::sun::star::lang;
47     using namespace ::com::sun::star::beans;
48 
49     //====================================================================
50 	//= UnoSpinButtonModel
51 	//====================================================================
52 	//--------------------------------------------------------------------
53     UnoSpinButtonModel::UnoSpinButtonModel( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& i_factory )
54         :UnoControlModel( i_factory )
55     {
56 	    ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR );
57         ImplRegisterProperty( BASEPROPERTY_BORDER );
58 		ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR );
59         ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL );
60 	    ImplRegisterProperty( BASEPROPERTY_ENABLED );
61 	    ImplRegisterProperty( BASEPROPERTY_ENABLEVISIBLE );
62         ImplRegisterProperty( BASEPROPERTY_HELPTEXT );
63 	    ImplRegisterProperty( BASEPROPERTY_HELPURL );
64         ImplRegisterProperty( BASEPROPERTY_ORIENTATION );
65         ImplRegisterProperty( BASEPROPERTY_PRINTABLE );
66         ImplRegisterProperty( BASEPROPERTY_REPEAT );
67         ImplRegisterProperty( BASEPROPERTY_REPEAT_DELAY );
68         ImplRegisterProperty( BASEPROPERTY_SYMBOL_COLOR );
69         ImplRegisterProperty( BASEPROPERTY_SPINVALUE );
70         ImplRegisterProperty( BASEPROPERTY_SPINVALUE_MIN );
71         ImplRegisterProperty( BASEPROPERTY_SPINVALUE_MAX );
72         ImplRegisterProperty( BASEPROPERTY_SPININCREMENT );
73         ImplRegisterProperty( BASEPROPERTY_TABSTOP );
74         ImplRegisterProperty( BASEPROPERTY_WRITING_MODE );
75         ImplRegisterProperty( BASEPROPERTY_CONTEXT_WRITING_MODE );
76     }
77 
78 	//--------------------------------------------------------------------
79     ::rtl::OUString UnoSpinButtonModel::getServiceName( ) throw (RuntimeException)
80     {
81 	    return ::rtl::OUString::createFromAscii( szServiceName_UnoSpinButtonModel );
82     }
83 
84 	//--------------------------------------------------------------------
85     Any UnoSpinButtonModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
86     {
87         switch ( nPropId )
88         {
89         case BASEPROPERTY_DEFAULTCONTROL:
90             return makeAny( ::rtl::OUString::createFromAscii( szServiceName_UnoSpinButtonControl ) );
91 
92         case BASEPROPERTY_BORDER:
93             return makeAny( (sal_Int16) 0 );
94 
95         case BASEPROPERTY_REPEAT:
96             return makeAny( (sal_Bool)sal_True );
97 
98         default:
99     	    return UnoControlModel::ImplGetDefaultValue( nPropId );
100         }
101     }
102 
103 	//--------------------------------------------------------------------
104     ::cppu::IPropertyArrayHelper& UnoSpinButtonModel::getInfoHelper()
105     {
106 	    static UnoPropertyArrayHelper* pHelper = NULL;
107 	    if ( !pHelper )
108 	    {
109 		    Sequence<sal_Int32>	aIDs = ImplGetPropertyIds();
110 		    pHelper = new UnoPropertyArrayHelper( aIDs );
111 	    }
112 	    return *pHelper;
113     }
114 
115 	//--------------------------------------------------------------------
116     Reference< XPropertySetInfo > UnoSpinButtonModel::getPropertySetInfo(  ) throw(RuntimeException)
117     {
118 	    static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
119 	    return xInfo;
120     }
121 
122 	//--------------------------------------------------------------------
123     ::rtl::OUString SAL_CALL UnoSpinButtonModel::getImplementationName(  ) throw(RuntimeException)
124     {
125         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.toolkit.UnoSpinButtonModel" ) );
126     }
127 
128 	//--------------------------------------------------------------------
129     Sequence< ::rtl::OUString > SAL_CALL UnoSpinButtonModel::getSupportedServiceNames() throw(RuntimeException)
130     {
131         Sequence< ::rtl::OUString > aServices( UnoControlModel::getSupportedServiceNames() );
132         aServices.realloc( aServices.getLength() + 1 );
133         aServices[ aServices.getLength() - 1 ] = ::rtl::OUString::createFromAscii( szServiceName_UnoSpinButtonModel );
134         return aServices;
135     }
136 
137     //====================================================================
138 	//= UnoSpinButtonControl
139 	//====================================================================
140 	//--------------------------------------------------------------------
141     UnoSpinButtonControl::UnoSpinButtonControl( const Reference< XMultiServiceFactory >& i_factory )
142         :UnoControlBase( i_factory )
143 	    ,maAdjustmentListeners( *this )
144     {
145     }
146 
147 	//--------------------------------------------------------------------
148     ::rtl::OUString UnoSpinButtonControl::GetComponentServiceName()
149     {
150 	    return ::rtl::OUString::createFromAscii( "SpinButton" );
151     }
152 
153 	//--------------------------------------------------------------------
154     Any UnoSpinButtonControl::queryAggregation( const Type & rType ) throw(RuntimeException)
155     {
156         Any aRet = UnoControlBase::queryAggregation( rType );
157         if ( !aRet.hasValue() )
158             aRet = UnoSpinButtonControl_Base::queryInterface( rType );
159         return aRet;
160     }
161 
162 	//--------------------------------------------------------------------
163     IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoSpinButtonControl, UnoControlBase, UnoSpinButtonControl_Base )
164 
165 	//--------------------------------------------------------------------
166     void UnoSpinButtonControl::dispose() throw(RuntimeException)
167     {
168         ::osl::ClearableMutexGuard aGuard( GetMutex() );
169         if ( maAdjustmentListeners.getLength() )
170         {
171             Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
172             if ( xSpinnable.is() )
173                 xSpinnable->removeAdjustmentListener( this );
174 
175             EventObject aDisposeEvent;
176 	        aDisposeEvent.Source = *this;
177 
178             aGuard.clear();
179 	        maAdjustmentListeners.disposeAndClear( aDisposeEvent );
180         }
181 
182         UnoControl::dispose();
183     }
184 
185 	//--------------------------------------------------------------------
186     ::rtl::OUString SAL_CALL UnoSpinButtonControl::getImplementationName(  ) throw(RuntimeException)
187     {
188         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.toolkit.UnoSpinButtonControl" ) );
189     }
190 
191 	//--------------------------------------------------------------------
192     Sequence< ::rtl::OUString > SAL_CALL UnoSpinButtonControl::getSupportedServiceNames() throw(RuntimeException)
193     {
194         Sequence< ::rtl::OUString > aServices( UnoControlBase::getSupportedServiceNames() );
195         aServices.realloc( aServices.getLength() + 1 );
196         aServices[ aServices.getLength() - 1 ] = ::rtl::OUString::createFromAscii( szServiceName_UnoSpinButtonControl );
197         return aServices;
198     }
199 
200     //--------------------------------------------------------------------
201     void UnoSpinButtonControl::createPeer( const Reference< XToolkit > & rxToolkit, const Reference< XWindowPeer >  & rParentPeer ) throw(RuntimeException)
202     {
203 	    UnoControl::createPeer( rxToolkit, rParentPeer );
204 
205         Reference < XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
206         if ( xSpinnable.is() )
207 	        xSpinnable->addAdjustmentListener( this );
208     }
209 
210 	//--------------------------------------------------------------------
211     void UnoSpinButtonControl::adjustmentValueChanged( const AdjustmentEvent& rEvent ) throw(RuntimeException)
212     {
213 	    switch ( rEvent.Type )
214 	    {
215 		    case AdjustmentType_ADJUST_LINE:
216 		    case AdjustmentType_ADJUST_PAGE:
217 		    case AdjustmentType_ADJUST_ABS:
218 				ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE ), makeAny( rEvent.Value ), sal_False );
219     		    break;
220 		    default:
221                 DBG_ERROR( "UnoSpinButtonControl::adjustmentValueChanged - unknown Type" );
222 	    }
223 
224 	    if ( maAdjustmentListeners.getLength() )
225         {
226             AdjustmentEvent aEvent( rEvent );
227             aEvent.Source = *this;
228 		    maAdjustmentListeners.adjustmentValueChanged( aEvent );
229         }
230     }
231 
232 	//--------------------------------------------------------------------
233     void UnoSpinButtonControl::addAdjustmentListener( const Reference< XAdjustmentListener > & listener ) throw(RuntimeException)
234     {
235         ::osl::MutexGuard aGuard( GetMutex() );
236 	    maAdjustmentListeners.addInterface( listener );
237     }
238 
239 	//--------------------------------------------------------------------
240     void UnoSpinButtonControl::removeAdjustmentListener( const Reference< XAdjustmentListener > & listener ) throw(RuntimeException)
241     {
242         ::osl::MutexGuard aGuard( GetMutex() );
243 	    maAdjustmentListeners.removeInterface( listener );
244     }
245 
246     //--------------------------------------------------------------------
247     void SAL_CALL UnoSpinButtonControl::setValue( sal_Int32 value ) throw (RuntimeException)
248     {
249 	    ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE ), makeAny( value ), sal_True );
250     }
251 
252     //--------------------------------------------------------------------
253     void SAL_CALL UnoSpinButtonControl::setValues( sal_Int32 minValue, sal_Int32 maxValue, sal_Int32 currentValue ) throw (RuntimeException)
254     {
255 	    ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE_MIN ), makeAny( minValue ), sal_True );
256 	    ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE_MAX ), makeAny( maxValue ), sal_True );
257 	    ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE ), makeAny( currentValue ), sal_True );
258     }
259 
260     //--------------------------------------------------------------------
261     sal_Int32 SAL_CALL UnoSpinButtonControl::getValue(  ) throw (RuntimeException)
262     {
263         ::osl::MutexGuard aGuard( GetMutex() );
264         sal_Int32 nValue = 0;
265 
266         Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
267 	    if ( xSpinnable.is() )
268 		    nValue = xSpinnable->getValue();
269 
270         return nValue;
271     }
272 
273     //--------------------------------------------------------------------
274     void SAL_CALL UnoSpinButtonControl::setMinimum( sal_Int32 minValue ) throw (RuntimeException)
275     {
276 	    ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE_MIN ), makeAny( minValue ), sal_True );
277     }
278 
279     //--------------------------------------------------------------------
280     void SAL_CALL UnoSpinButtonControl::setMaximum( sal_Int32 maxValue ) throw (RuntimeException)
281     {
282 	    ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPINVALUE_MAX ), makeAny( maxValue ), sal_True );
283     }
284 
285     //--------------------------------------------------------------------
286     sal_Int32 SAL_CALL UnoSpinButtonControl::getMinimum(  ) throw (RuntimeException)
287     {
288         ::osl::MutexGuard aGuard( GetMutex() );
289         sal_Int32 nMin = 0;
290 
291         Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
292 	    if ( xSpinnable.is() )
293 		    nMin = xSpinnable->getMinimum();
294 
295         return nMin;
296     }
297 
298     //--------------------------------------------------------------------
299     sal_Int32 SAL_CALL UnoSpinButtonControl::getMaximum(  ) throw (RuntimeException)
300     {
301         ::osl::MutexGuard aGuard( GetMutex() );
302         sal_Int32 nMax = 0;
303 
304         Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
305 	    if ( xSpinnable.is() )
306 		    nMax = xSpinnable->getMaximum();
307 
308         return nMax;
309     }
310 
311     //--------------------------------------------------------------------
312     void SAL_CALL UnoSpinButtonControl::setSpinIncrement( sal_Int32 spinIncrement ) throw (RuntimeException)
313     {
314 	    ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_SPININCREMENT ), makeAny( spinIncrement ), sal_True );
315     }
316 
317     //--------------------------------------------------------------------
318     sal_Int32 SAL_CALL UnoSpinButtonControl::getSpinIncrement(  ) throw (RuntimeException)
319     {
320         ::osl::MutexGuard aGuard( GetMutex() );
321         sal_Int32 nIncrement = 0;
322 
323         Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
324 	    if ( xSpinnable.is() )
325 		    nIncrement = xSpinnable->getSpinIncrement();
326 
327         return nIncrement;
328     }
329 
330     //--------------------------------------------------------------------
331     void SAL_CALL UnoSpinButtonControl::setOrientation( sal_Int32 orientation ) throw (NoSupportException, RuntimeException)
332     {
333 	    ImplSetPropertyValue( GetPropertyName( BASEPROPERTY_ORIENTATION ), makeAny( orientation ), sal_True );
334     }
335 
336     //--------------------------------------------------------------------
337     sal_Int32 SAL_CALL UnoSpinButtonControl::getOrientation(  ) throw (RuntimeException)
338     {
339         ::osl::MutexGuard aGuard( GetMutex() );
340         sal_Int32 nOrientation = ScrollBarOrientation::HORIZONTAL;
341 
342         Reference< XSpinValue > xSpinnable( getPeer(), UNO_QUERY );
343 	    if ( xSpinnable.is() )
344 		    nOrientation = xSpinnable->getOrientation();
345 
346         return nOrientation;
347     }
348 
349 //........................................................................
350 }  // namespace toolkit
351 //........................................................................
352 
353