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