bump product version to 5.0.4.1
[LibreOffice.git] / forms / source / component / refvaluecomponent.cxx
blob0be4706f9d738b02871924214edd24a248320d03
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 "refvaluecomponent.hxx"
22 #include <tools/diagnose_ex.h>
24 #include <list>
27 namespace frm
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::lang;
33 using namespace ::com::sun::star::beans;
34 using namespace ::com::sun::star::form::binding;
37 //=
40 OReferenceValueComponent::OReferenceValueComponent( const Reference< XComponentContext >& _rxFactory, const OUString& _rUnoControlModelTypeName, const OUString& _rDefault, bool _bSupportNoCheckRefValue )
41 :OBoundControlModel( _rxFactory, _rUnoControlModelTypeName, _rDefault, false, true, true )
42 ,m_eDefaultChecked( TRISTATE_FALSE )
43 ,m_bSupportSecondRefValue( _bSupportNoCheckRefValue )
48 OReferenceValueComponent::OReferenceValueComponent( const OReferenceValueComponent* _pOriginal, const Reference< XComponentContext>& _rxFactory )
49 :OBoundControlModel( _pOriginal, _rxFactory )
51 m_sReferenceValue = _pOriginal->m_sReferenceValue;
52 m_sNoCheckReferenceValue = _pOriginal->m_sNoCheckReferenceValue;
53 m_eDefaultChecked = _pOriginal->m_eDefaultChecked;
54 m_bSupportSecondRefValue = _pOriginal->m_bSupportSecondRefValue;
56 calculateExternalValueType();
60 OReferenceValueComponent::~OReferenceValueComponent()
65 void OReferenceValueComponent::setReferenceValue( const OUString& _rRefValue )
67 m_sReferenceValue = _rRefValue;
68 calculateExternalValueType();
72 void SAL_CALL OReferenceValueComponent::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
74 switch ( _nHandle )
76 case PROPERTY_ID_REFVALUE: _rValue <<= m_sReferenceValue; break;
77 case PROPERTY_ID_DEFAULT_STATE: _rValue <<= (sal_Int16)m_eDefaultChecked; break;
79 case PROPERTY_ID_UNCHECKED_REFVALUE:
80 OSL_ENSURE( m_bSupportSecondRefValue, "OReferenceValueComponent::getFastPropertyValue: not supported!" );
81 _rValue <<= m_sNoCheckReferenceValue;
82 break;
84 default:
85 OBoundControlModel::getFastPropertyValue( _rValue, _nHandle );
90 void SAL_CALL OReferenceValueComponent::setFastPropertyValue_NoBroadcast( sal_Int32 _nHandle, const Any& _rValue ) throw (Exception, std::exception)
92 switch ( _nHandle )
94 case PROPERTY_ID_REFVALUE :
95 OSL_VERIFY( _rValue >>= m_sReferenceValue );
96 calculateExternalValueType();
97 break;
99 case PROPERTY_ID_UNCHECKED_REFVALUE:
100 OSL_ENSURE( m_bSupportSecondRefValue, "OReferenceValueComponent::setFastPropertyValue_NoBroadcast: not supported!" );
101 OSL_VERIFY( _rValue >>= m_sNoCheckReferenceValue );
102 break;
104 case PROPERTY_ID_DEFAULT_STATE:
106 sal_Int16 nDefaultChecked;
107 if (!(_rValue >>= nDefaultChecked) || nDefaultChecked < 0
108 || nDefaultChecked > 2)
110 throw css::lang::IllegalArgumentException(
111 ("DefaultState property value must be a SHORT in the range"
112 " 0--2"),
113 css::uno::Reference<css::uno::XInterface>(), -1);
115 m_eDefaultChecked = (ToggleState)nDefaultChecked;
116 resetNoBroadcast();
118 break;
120 default:
121 OBoundControlModel::setFastPropertyValue_NoBroadcast( _nHandle, _rValue );
126 sal_Bool SAL_CALL OReferenceValueComponent::convertFastPropertyValue( Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue ) throw (IllegalArgumentException)
128 bool bModified = false;
129 switch ( _nHandle )
131 case PROPERTY_ID_REFVALUE:
132 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_sReferenceValue );
133 break;
135 case PROPERTY_ID_UNCHECKED_REFVALUE:
136 OSL_ENSURE( m_bSupportSecondRefValue, "OReferenceValueComponent::convertFastPropertyValue: not supported!" );
137 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_sNoCheckReferenceValue );
138 break;
140 case PROPERTY_ID_DEFAULT_STATE:
141 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_eDefaultChecked );
142 break;
144 default:
145 bModified = OBoundControlModel::convertFastPropertyValue( _rConvertedValue, _rOldValue, _nHandle, _rValue );
146 break;
148 return bModified;
152 Any OReferenceValueComponent::getDefaultForReset() const
154 return makeAny( (sal_Int16)m_eDefaultChecked );
158 void OReferenceValueComponent::describeFixedProperties( Sequence< Property >& _rProps ) const
160 BEGIN_DESCRIBE_PROPERTIES( m_bSupportSecondRefValue ? 3 : 2, OBoundControlModel )
161 DECL_PROP1( REFVALUE, OUString, BOUND );
162 DECL_PROP1( DEFAULT_STATE, sal_Int16, BOUND );
163 if ( m_bSupportSecondRefValue )
165 DECL_PROP1( UNCHECKED_REFVALUE, OUString, BOUND );
167 END_DESCRIBE_PROPERTIES();
171 Sequence< Type > OReferenceValueComponent::getSupportedBindingTypes()
173 ::std::list< Type > aTypes;
174 aTypes.push_back( cppu::UnoType<sal_Bool>::get() );
176 if ( !m_sReferenceValue.isEmpty() )
177 aTypes.push_front( cppu::UnoType<OUString>::get() );
178 // push_front, because this is the preferred type
180 Sequence< Type > aTypesRet( aTypes.size() );
181 ::std::copy( aTypes.begin(), aTypes.end(), aTypesRet.getArray() );
182 return aTypesRet;
186 Any OReferenceValueComponent::translateExternalValueToControlValue( const Any& _rExternalValue ) const
188 sal_Int16 nState = TRISTATE_INDET;
190 bool bExternalState = false;
191 OUString sExternalValue;
192 if ( _rExternalValue >>= bExternalState )
194 nState = ::sal::static_int_cast< sal_Int16 >( bExternalState ? TRISTATE_TRUE : TRISTATE_FALSE );
196 else if ( _rExternalValue >>= sExternalValue )
198 if ( sExternalValue == m_sReferenceValue )
199 nState = TRISTATE_TRUE;
200 else
202 if ( !m_bSupportSecondRefValue || ( sExternalValue == m_sNoCheckReferenceValue ) )
203 nState = TRISTATE_FALSE;
204 else
205 nState = TRISTATE_INDET;
208 else if ( !_rExternalValue.hasValue() )
210 nState = TRISTATE_INDET;
212 else
214 OSL_FAIL( "OReferenceValueComponent::translateExternalValueToControlValue: unexpected value type!" );
217 return makeAny( nState );
221 Any OReferenceValueComponent::translateControlValueToExternalValue( ) const
223 Any aExternalValue;
227 Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
228 sal_Int16 nControlValue = TRISTATE_INDET;
229 aControlValue >>= nControlValue;
231 bool bBooleanExchange = getExternalValueType().getTypeClass() == TypeClass_BOOLEAN;
232 bool bStringExchange = getExternalValueType().getTypeClass() == TypeClass_STRING;
233 OSL_ENSURE( bBooleanExchange || bStringExchange,
234 "OReferenceValueComponent::translateControlValueToExternalValue: unexpected value exchange type!" );
236 switch( nControlValue )
238 case TRISTATE_TRUE:
239 if ( bBooleanExchange )
241 aExternalValue <<= true;
243 else if ( bStringExchange )
245 aExternalValue <<= m_sReferenceValue;
247 break;
249 case TRISTATE_FALSE:
250 if ( bBooleanExchange )
252 aExternalValue <<= false;
254 else if ( bStringExchange )
256 aExternalValue <<= (m_bSupportSecondRefValue ? m_sNoCheckReferenceValue : OUString());
258 break;
261 catch( const Exception& )
263 OSL_FAIL( "OReferenceValueComponent::translateControlValueToExternalValue: caught an exception!" );
266 return aExternalValue;
270 Any OReferenceValueComponent::translateControlValueToValidatableValue( ) const
272 if ( !m_xAggregateSet.is() )
273 return Any();
275 Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
276 sal_Int16 nControlValue = TRISTATE_INDET;
277 aControlValue >>= nControlValue;
279 Any aValidatableValue;
280 switch ( nControlValue )
282 case TRISTATE_TRUE:
283 aValidatableValue <<= true;
284 break;
285 case TRISTATE_FALSE:
286 aValidatableValue <<= false;
287 break;
289 return aValidatableValue;
293 } // namespace frm
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */