xref: /trunk/main/forms/source/component/scrollbar.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 )
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     {
192         sal_Bool bModified( sal_False );
193         switch ( _nHandle )
194         {
195             case PROPERTY_ID_DEFAULT_SCROLL_VALUE:
196                 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_nDefaultScrollValue );
197                 break;
198 
199             default:
200                 bModified = OBoundControlModel::convertFastPropertyValue( _rConvertedValue, _rOldValue, _nHandle, _rValue );
201                 break;
202         }
203         return bModified;
204     }
205 
206     //--------------------------------------------------------------------
getPropertyDefaultByHandle(sal_Int32 _nHandle) const207     Any OScrollBarModel::getPropertyDefaultByHandle( sal_Int32 _nHandle ) const
208     {
209         Any aReturn;
210 
211         switch ( _nHandle )
212         {
213         case PROPERTY_ID_DEFAULT_SCROLL_VALUE:
214             aReturn <<= (sal_Int32)0;
215             break;
216 
217         default:
218             aReturn = OBoundControlModel::getPropertyDefaultByHandle( _nHandle );
219             break;
220         }
221 
222         return aReturn;
223     }
224 
225     //------------------------------------------------------------------------------
translateDbColumnToControlValue()226     Any OScrollBarModel::translateDbColumnToControlValue( )
227     {
228         OSL_ENSURE( sal_False, "OScrollBarModel::commitControlValueToDbColumn: never to be called (we're not bound)!" );
229         return Any();
230     }
231 
232     //------------------------------------------------------------------------------
commitControlValueToDbColumn(bool)233     sal_Bool OScrollBarModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
234     {
235         OSL_ENSURE( sal_False, "OScrollBarModel::commitControlValueToDbColumn: never to be called (we're not bound)!" );
236         return sal_True;
237     }
238 
239     //------------------------------------------------------------------------------
getDefaultForReset() const240     Any OScrollBarModel::getDefaultForReset() const
241     {
242         return makeAny( (sal_Int32)m_nDefaultScrollValue );
243     }
244 
245     //--------------------------------------------------------------------
getServiceName()246     ::rtl::OUString SAL_CALL OScrollBarModel::getServiceName()
247     {
248         return FRM_SUN_COMPONENT_SCROLLBAR;
249     }
250 
251     //--------------------------------------------------------------------
write(const Reference<XObjectOutputStream> & _rxOutStream)252     void SAL_CALL OScrollBarModel::write( const Reference< XObjectOutputStream >& _rxOutStream )
253     {
254         OBoundControlModel::write( _rxOutStream );
255         ::osl::MutexGuard aGuard( m_aMutex );
256 
257         OStreamSection aSection( Reference< XDataOutputStream >( _rxOutStream, UNO_QUERY ) );
258 
259         // version
260         _rxOutStream->writeShort( 0x0001 );
261 
262         // properties
263         _rxOutStream << m_nDefaultScrollValue;
264         writeHelpTextCompatibly( _rxOutStream );
265     }
266 
267     //--------------------------------------------------------------------
read(const Reference<XObjectInputStream> & _rxInStream)268     void SAL_CALL OScrollBarModel::read( const Reference< XObjectInputStream>& _rxInStream )
269     {
270         OBoundControlModel::read( _rxInStream );
271         ::osl::MutexGuard aGuard( m_aMutex );
272 
273         // version
274         {
275             OStreamSection aSection( Reference< XDataInputStream >( _rxInStream, UNO_QUERY ) );
276 
277             sal_uInt16 nVersion = _rxInStream->readShort();
278             if ( nVersion == 0x0001 )
279             {
280                 _rxInStream >> m_nDefaultScrollValue;
281                 readHelpTextCompatibly( _rxInStream );
282             }
283             else
284                 defaultCommonProperties();
285 
286             // here, everything in the stream section which is left will be skipped
287         }
288     }
289 
290     //--------------------------------------------------------------------
translateExternalValueToControlValue(const Any & _rExternalValue) const291     Any OScrollBarModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
292     {
293         return translateExternalDoubleToControlIntValue( _rExternalValue, m_xAggregateSet,
294             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ScrollValueMin" ) ),
295             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ScrollValueMax" ) ) );
296     }
297 
298     //--------------------------------------------------------------------
translateControlValueToExternalValue() const299     Any OScrollBarModel::translateControlValueToExternalValue( ) const
300     {
301         // by definition, the base class simply obtains the property value
302         return translateControlIntToExternalDoubleValue( OBoundControlModel::translateControlValueToExternalValue() );
303     }
304 
305     //--------------------------------------------------------------------
getSupportedBindingTypes()306     Sequence< Type > OScrollBarModel::getSupportedBindingTypes()
307     {
308         return Sequence< Type >( &::getCppuType( static_cast< double* >( NULL ) ), 1 );
309     }
310 
311 //........................................................................
312 }   // namespace frm
313 //........................................................................
314