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