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