tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / forms / source / component / scrollbar.cxx
blob4eb5786f9fed70b2ec2a5b426c12bb6c8951c000
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "scrollbar.hxx"
21 #include <property.hxx>
22 #include <services.hxx>
23 #include <comphelper/property.hxx>
24 #include <comphelper/streamsection.hxx>
25 #include <comphelper/basicio.hxx>
26 #include <rtl/math.hxx>
27 #include <tools/debug.hxx>
28 #include <com/sun/star/beans/PropertyAttribute.hpp>
29 #include <com/sun/star/form/FormComponentType.hpp>
31 namespace frm
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::beans;
36 using namespace ::com::sun::star::form;
37 using namespace ::com::sun::star::util;
38 using namespace ::com::sun::star::io;
41 //= helper
43 Any translateExternalDoubleToControlIntValue(
44 const Any& _rExternalValue, const Reference< XPropertySet >& _rxProperties,
45 const OUString& _rMinValueName, const OUString& _rMaxValueName )
47 OSL_ENSURE( _rxProperties.is(), "translateExternalDoubleToControlIntValue: no aggregate!?" );
49 sal_Int32 nControlValue( 0 );
50 double nExternalValue = 0;
51 if ( _rExternalValue >>= nExternalValue )
53 if ( std::isinf( nExternalValue ) )
55 // set the minimum or maximum of the scroll values
56 OUString sLimitPropertyName = std::signbit( nExternalValue )
57 ? _rMinValueName : _rMaxValueName;
58 if ( _rxProperties.is() )
59 _rxProperties->getPropertyValue( sLimitPropertyName ) >>= nControlValue;
61 else
63 nControlValue = static_cast<sal_Int32>(::rtl::math::round( nExternalValue ));
66 else
68 if ( _rxProperties.is() )
69 _rxProperties->getPropertyValue( _rMinValueName ) >>= nControlValue;
72 return Any( nControlValue );
76 Any translateControlIntToExternalDoubleValue( const Any& _rControlIntValue )
78 Any aExternalDoubleValue;
79 sal_Int32 nScrollValue = 0;
80 if ( _rControlIntValue >>= nScrollValue )
81 aExternalDoubleValue <<= static_cast<double>(nScrollValue);
82 else
84 OSL_FAIL( "translateControlIntToExternalDoubleValue: no integer scroll value!" );
85 // aExternalDoubleValue is void here, which is okay for this purpose ...
88 return aExternalDoubleValue;
91 OScrollBarModel::OScrollBarModel( const Reference<XComponentContext>& _rxFactory )
92 :OBoundControlModel( _rxFactory, VCL_CONTROLMODEL_SCROLLBAR, VCL_CONTROL_SCROLLBAR, true, true, false )
93 ,m_nDefaultScrollValue( 0 )
96 m_nClassId = FormComponentType::SCROLLBAR;
97 initValueProperty( PROPERTY_SCROLL_VALUE, PROPERTY_ID_SCROLL_VALUE );
101 OScrollBarModel::OScrollBarModel( const OScrollBarModel* _pOriginal, const Reference< XComponentContext >& _rxFactory )
102 :OBoundControlModel( _pOriginal, _rxFactory )
104 m_nDefaultScrollValue = _pOriginal->m_nDefaultScrollValue;
108 OScrollBarModel::~OScrollBarModel( )
112 OUString SAL_CALL OScrollBarModel::getImplementationName()
114 return u"com.sun.star.comp.forms.OScrollBarModel"_ustr;
117 // note that we're passing OControlModel as "base class". This is because
118 // OBoundControlModel, our real base class, claims to support the DataAwareControlModel
119 // service, which isn't really true for us. We only derive from this class
120 // to benefit from the functionality for binding to spreadsheet cells
121 Sequence< OUString > SAL_CALL OScrollBarModel::getSupportedServiceNames()
123 static constexpr OUString aOwnNames[] { FRM_SUN_COMPONENT_SCROLLBAR, BINDABLE_INTEGER_VALUE_RANGE };
125 return ::comphelper::combineSequences(
126 getAggregateServiceNames(),
127 ::comphelper::concatSequences(
128 OControlModel::getSupportedServiceNames_Static(),
129 aOwnNames)
133 css::uno::Reference< css::util::XCloneable > SAL_CALL OScrollBarModel::createClone()
135 rtl::Reference<OScrollBarModel> pClone = new OScrollBarModel(this, getContext());
136 pClone->clonedFrom(this);
137 return pClone;
141 void OScrollBarModel::describeFixedProperties( Sequence< Property >& _rProps ) const
143 OControlModel::describeFixedProperties( _rProps );
144 sal_Int32 nOldCount = _rProps.getLength();
145 _rProps.realloc( nOldCount + 3);
146 css::beans::Property* pProperties = _rProps.getArray() + nOldCount;
147 *pProperties++ = css::beans::Property(PROPERTY_DEFAULT_SCROLL_VALUE, PROPERTY_ID_DEFAULT_SCROLL_VALUE, cppu::UnoType<sal_Int32>::get(), css::beans::PropertyAttribute::BOUND);
148 *pProperties++ = css::beans::Property(PROPERTY_TABINDEX, PROPERTY_ID_TABINDEX, cppu::UnoType<sal_Int16>::get(), css::beans::PropertyAttribute::BOUND);
149 *pProperties++ = css::beans::Property(PROPERTY_CONTROLSOURCEPROPERTY, PROPERTY_ID_CONTROLSOURCEPROPERTY, cppu::UnoType<OUString>::get(), css::beans::PropertyAttribute::READONLY | css::beans::PropertyAttribute::TRANSIENT);
150 DBG_ASSERT( pProperties == _rProps.getArray() + _rProps.getLength(), "<...>::describeFixedProperties/getInfoHelper: forgot to adjust the count ?");
154 void OScrollBarModel::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
156 switch ( _nHandle )
158 case PROPERTY_ID_DEFAULT_SCROLL_VALUE:
159 _rValue <<= m_nDefaultScrollValue;
160 break;
162 default:
163 OBoundControlModel::getFastPropertyValue( _rValue, _nHandle );
168 void OScrollBarModel::setFastPropertyValue_NoBroadcast( sal_Int32 _nHandle, const Any& _rValue )
170 switch ( _nHandle )
172 case PROPERTY_ID_DEFAULT_SCROLL_VALUE:
173 OSL_VERIFY( _rValue >>= m_nDefaultScrollValue );
174 resetNoBroadcast();
175 break;
177 default:
178 OBoundControlModel::setFastPropertyValue_NoBroadcast( _nHandle, _rValue );
183 sal_Bool OScrollBarModel::convertFastPropertyValue(
184 Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue )
186 bool bModified( false );
187 switch ( _nHandle )
189 case PROPERTY_ID_DEFAULT_SCROLL_VALUE:
190 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_nDefaultScrollValue );
191 break;
193 default:
194 bModified = OBoundControlModel::convertFastPropertyValue( _rConvertedValue, _rOldValue, _nHandle, _rValue );
195 break;
197 return bModified;
201 Any OScrollBarModel::getPropertyDefaultByHandle( sal_Int32 _nHandle ) const
203 Any aReturn;
205 switch ( _nHandle )
207 case PROPERTY_ID_DEFAULT_SCROLL_VALUE:
208 aReturn <<= sal_Int32(0);
209 break;
211 default:
212 aReturn = OBoundControlModel::getPropertyDefaultByHandle( _nHandle );
213 break;
216 return aReturn;
220 Any OScrollBarModel::translateDbColumnToControlValue( )
222 OSL_FAIL( "OScrollBarModel::commitControlValueToDbColumn: never to be called (we're not bound)!" );
223 return Any();
227 bool OScrollBarModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
229 OSL_FAIL( "OScrollBarModel::commitControlValueToDbColumn: never to be called (we're not bound)!" );
230 return true;
234 Any OScrollBarModel::getDefaultForReset() const
236 return Any( m_nDefaultScrollValue );
240 OUString SAL_CALL OScrollBarModel::getServiceName()
242 return FRM_SUN_COMPONENT_SCROLLBAR;
246 void SAL_CALL OScrollBarModel::write( const Reference< XObjectOutputStream >& _rxOutStream )
248 OBoundControlModel::write( _rxOutStream );
249 ::osl::MutexGuard aGuard( m_aMutex );
251 OStreamSection aSection( _rxOutStream );
253 // version
254 _rxOutStream->writeShort( 0x0001 );
256 // properties
257 _rxOutStream << m_nDefaultScrollValue;
258 writeHelpTextCompatibly( _rxOutStream );
262 void SAL_CALL OScrollBarModel::read( const Reference< XObjectInputStream>& _rxInStream )
264 OBoundControlModel::read( _rxInStream );
265 ::osl::MutexGuard aGuard( m_aMutex );
267 // version
269 OStreamSection aSection( _rxInStream );
271 sal_uInt16 nVersion = _rxInStream->readShort();
272 if ( nVersion == 0x0001 )
274 _rxInStream >> m_nDefaultScrollValue;
275 readHelpTextCompatibly( _rxInStream );
277 else
278 defaultCommonProperties();
280 // here, everything in the stream section which is left will be skipped
285 Any OScrollBarModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
287 return translateExternalDoubleToControlIntValue( _rExternalValue, m_xAggregateSet,
288 u"ScrollValueMin"_ustr,
289 u"ScrollValueMax"_ustr );
293 Any OScrollBarModel::translateControlValueToExternalValue( ) const
295 // by definition, the base class simply obtains the property value
296 return translateControlIntToExternalDoubleValue( OBoundControlModel::translateControlValueToExternalValue() );
300 Sequence< Type > OScrollBarModel::getSupportedBindingTypes()
302 return Sequence< Type >( & cppu::UnoType<double>::get(), 1 );
305 } // namespace frm
307 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
308 com_sun_star_comp_forms_OScrollBarModel_get_implementation(css::uno::XComponentContext* component,
309 css::uno::Sequence<css::uno::Any> const &)
311 return cppu::acquire(new frm::OScrollBarModel(component));
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */