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_forms.hxx"
30 #include "spinbutton.hxx"
31 #include <comphelper/streamsection.hxx>
32 #include <comphelper/basicio.hxx>
33 
34 //--------------------------------------------------------------------------
35 extern "C" void SAL_CALL createRegistryInfo_OSpinButtonModel()
36 {
37 	static ::frm::OMultiInstanceAutoRegistration< ::frm::OSpinButtonModel >   aRegisterModel;
38 }
39 
40 //........................................................................
41 namespace frm
42 {
43 //........................................................................
44 
45     using namespace ::com::sun::star::uno;
46     using namespace ::com::sun::star::beans;
47     using namespace ::com::sun::star::form;
48     using namespace ::com::sun::star::awt;
49     using namespace ::com::sun::star::lang;
50     using namespace ::com::sun::star::util;
51     using namespace ::com::sun::star::io;
52     using namespace ::com::sun::star::form::binding;
53 
54     //====================================================================
55 	//= OSpinButtonModel
56 	//====================================================================
57     // implemented elsewhere
58     Any translateExternalDoubleToControlIntValue(
59         const Any& _rExternalValue, const Reference< XPropertySet >& _rxProperties,
60         const ::rtl::OUString& _rMinValueName, const ::rtl::OUString& _rMaxValueName );
61     Any translateControlIntToExternalDoubleValue( const Any& _rControlIntValue );
62 
63     //====================================================================
64 	//= OSpinButtonModel
65 	//====================================================================
66 	//--------------------------------------------------------------------
67     DBG_NAME( OSpinButtonModel )
68     //--------------------------------------------------------------------
69     OSpinButtonModel::OSpinButtonModel( const Reference<XMultiServiceFactory>& _rxFactory )
70         :OBoundControlModel( _rxFactory, VCL_CONTROLMODEL_SPINBUTTON, VCL_CONTROL_SPINBUTTON, sal_True, sal_True, sal_False )
71         ,m_nDefaultSpinValue( 0 )
72     {
73 	    DBG_CTOR( OSpinButtonModel, NULL );
74 
75 	    m_nClassId = FormComponentType::SPINBUTTON;
76         initValueProperty( PROPERTY_SPIN_VALUE, PROPERTY_ID_SPIN_VALUE );
77     }
78 
79     //--------------------------------------------------------------------
80     OSpinButtonModel::OSpinButtonModel( const OSpinButtonModel* _pOriginal, const Reference< XMultiServiceFactory >& _rxFactory )
81 	    :OBoundControlModel( _pOriginal, _rxFactory )
82     {
83 	    DBG_CTOR( OSpinButtonModel, NULL );
84         m_nDefaultSpinValue = _pOriginal->m_nDefaultSpinValue;
85     }
86 
87     //--------------------------------------------------------------------
88     OSpinButtonModel::~OSpinButtonModel( )
89     {
90         DBG_DTOR( OSpinButtonModel, NULL );
91     }
92 
93     //--------------------------------------------------------------------
94     IMPLEMENT_SERVICE_REGISTRATION_2( OSpinButtonModel, OControlModel, FRM_SUN_COMPONENT_SPINBUTTON, BINDABLE_INTEGER_VALUE_RANGE )
95         // note that we're passing OControlModel as "base class". This is because
96         // OBoundControlModel, our real base class, claims to support the DataAwareControlModel
97         // service, which isn't really true for us. We only derive from this class
98         // to benefit from the functionality for binding to spreadsheet cells
99 
100     //------------------------------------------------------------------------------
101     IMPLEMENT_DEFAULT_CLONING( OSpinButtonModel )
102 
103     //------------------------------------------------------------------------------
104     void SAL_CALL OSpinButtonModel::disposing()
105     {
106 	    OBoundControlModel::disposing();
107     }
108 
109     //--------------------------------------------------------------------
110     void OSpinButtonModel::describeFixedProperties( Sequence< Property >& _rProps ) const
111     {
112 	    BEGIN_DESCRIBE_PROPERTIES( 3, OControlModel )
113             DECL_PROP1( DEFAULT_SPIN_VALUE,   sal_Int32,       BOUND );
114             DECL_PROP1( TABINDEX,             sal_Int16,       BOUND );
115             DECL_PROP2( CONTROLSOURCEPROPERTY,::rtl::OUString, READONLY, TRANSIENT );
116 	    END_DESCRIBE_PROPERTIES();
117     }
118 
119     //------------------------------------------------------------------------------
120     void OSpinButtonModel::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
121     {
122 	    switch ( _nHandle )
123 	    {
124 		    case PROPERTY_ID_DEFAULT_SPIN_VALUE:
125                 _rValue <<= m_nDefaultSpinValue;
126                 break;
127 
128             default:
129 			    OBoundControlModel::getFastPropertyValue( _rValue, _nHandle );
130 	    }
131     }
132 
133     //------------------------------------------------------------------------------
134     void OSpinButtonModel::setFastPropertyValue_NoBroadcast( sal_Int32 _nHandle, const Any& _rValue ) throw ( Exception )
135     {
136 	    switch ( _nHandle )
137 	    {
138 		    case PROPERTY_ID_DEFAULT_SPIN_VALUE:
139                 OSL_VERIFY( _rValue >>= m_nDefaultSpinValue );
140 			    resetNoBroadcast();
141 			    break;
142 
143 		    default:
144 			    OBoundControlModel::setFastPropertyValue_NoBroadcast( _nHandle, _rValue );
145 	    }
146     }
147 
148     //------------------------------------------------------------------------------
149     sal_Bool OSpinButtonModel::convertFastPropertyValue(
150 			    Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue )
151 			    throw ( IllegalArgumentException )
152     {
153 	    sal_Bool bModified( sal_False );
154 	    switch ( _nHandle )
155 	    {
156 		    case PROPERTY_ID_DEFAULT_SPIN_VALUE:
157 			    bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_nDefaultSpinValue );
158 			    break;
159 
160             default:
161 			    bModified = OBoundControlModel::convertFastPropertyValue( _rConvertedValue, _rOldValue, _nHandle, _rValue );
162 			    break;
163 	    }
164 	    return bModified;
165     }
166 
167     //--------------------------------------------------------------------
168     Any OSpinButtonModel::getPropertyDefaultByHandle( sal_Int32 _nHandle ) const
169     {
170         Any aReturn;
171 
172         switch ( _nHandle )
173         {
174         case PROPERTY_ID_DEFAULT_SPIN_VALUE:
175             aReturn <<= (sal_Int32)0;
176             break;
177 
178         default:
179             aReturn = OBoundControlModel::getPropertyDefaultByHandle( _nHandle );
180             break;
181         }
182 
183         return aReturn;
184     }
185 
186     //------------------------------------------------------------------------------
187     Any OSpinButtonModel::translateDbColumnToControlValue( )
188     {
189         OSL_ENSURE( sal_False, "OSpinButtonModel::commitControlValueToDbColumn: never to be called (we're not bound)!" );
190         return Any();
191     }
192 
193     //------------------------------------------------------------------------------
194     sal_Bool OSpinButtonModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
195     {
196         OSL_ENSURE( sal_False, "OSpinButtonModel::commitControlValueToDbColumn: never to be called (we're not bound)!" );
197         return sal_True;
198     }
199 
200     //------------------------------------------------------------------------------
201     Any OSpinButtonModel::getDefaultForReset() const
202     {
203         return makeAny( (sal_Int32)m_nDefaultSpinValue );
204     }
205 
206     //--------------------------------------------------------------------
207     ::rtl::OUString SAL_CALL OSpinButtonModel::getServiceName() throw( RuntimeException )
208     {
209 	    return FRM_SUN_COMPONENT_SPINBUTTON;
210     }
211 
212     //--------------------------------------------------------------------
213     void SAL_CALL OSpinButtonModel::write( const Reference< XObjectOutputStream >& _rxOutStream )
214 	    throw( IOException, RuntimeException )
215     {
216 	    OBoundControlModel::write( _rxOutStream );
217         ::osl::MutexGuard aGuard( m_aMutex );
218 
219         OStreamSection aSection( Reference< XDataOutputStream >( _rxOutStream, UNO_QUERY ) );
220 
221         // version
222 	    _rxOutStream->writeShort( 0x0001 );
223 
224         // properties
225 	    _rxOutStream << m_nDefaultSpinValue;
226 	    writeHelpTextCompatibly( _rxOutStream );
227     }
228 
229     //--------------------------------------------------------------------
230     void SAL_CALL OSpinButtonModel::read( const Reference< XObjectInputStream>& _rxInStream ) throw( IOException, RuntimeException )
231     {
232 	    OBoundControlModel::read( _rxInStream );
233 	    ::osl::MutexGuard aGuard( m_aMutex );
234 
235 	    // version
236         {
237             OStreamSection aSection( Reference< XDataInputStream >( _rxInStream, UNO_QUERY ) );
238 
239             sal_uInt16 nVersion = _rxInStream->readShort();
240             if ( nVersion == 0x0001 )
241             {
242 	            _rxInStream >> m_nDefaultSpinValue;
243 	            readHelpTextCompatibly( _rxInStream );
244             }
245             else
246 			    defaultCommonProperties();
247 
248             // here, everything in the stream section which is left will be skipped
249         }
250     }
251 
252     //--------------------------------------------------------------------
253     Any OSpinButtonModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
254     {
255         return translateExternalDoubleToControlIntValue( _rExternalValue, m_xAggregateSet,
256             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SpinValueMin" ) ),
257             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SpinValueMax" ) ) );
258     }
259 
260     //--------------------------------------------------------------------
261     Any OSpinButtonModel::translateControlValueToExternalValue( ) const
262     {
263         // by definition, the base class simply obtains the property value
264         return translateControlIntToExternalDoubleValue( OBoundControlModel::translateControlValueToExternalValue() );
265     }
266 
267     //--------------------------------------------------------------------
268     Sequence< Type > OSpinButtonModel::getSupportedBindingTypes()
269     {
270         return Sequence< Type >( &::getCppuType( static_cast< double* >( NULL ) ), 1 );
271     }
272 
273 //........................................................................
274 }   // namespace frm
275 //........................................................................
276