bump product version to 4.1.6.2
[LibreOffice.git] / forms / source / component / refvaluecomponent.cxx
blobf8825a556c2deaee9babfadd43c92830a360856a
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>
26 //........................................................................
27 namespace frm
29 //........................................................................
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;
36 //====================================================================
37 //=
38 //====================================================================
39 //--------------------------------------------------------------------
40 OReferenceValueComponent::OReferenceValueComponent( const Reference< XMultiServiceFactory>& _rxFactory, const OUString& _rUnoControlModelTypeName, const OUString& _rDefault, sal_Bool _bSupportNoCheckRefValue )
41 :OBoundControlModel( _rxFactory, _rUnoControlModelTypeName, _rDefault, sal_False, sal_True, sal_True )
42 ,m_eDefaultChecked( STATE_NOCHECK )
43 ,m_bSupportSecondRefValue( _bSupportNoCheckRefValue )
47 //--------------------------------------------------------------------
48 OReferenceValueComponent::OReferenceValueComponent( const OReferenceValueComponent* _pOriginal, const Reference< XMultiServiceFactory>& _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();
59 //--------------------------------------------------------------------
60 OReferenceValueComponent::~OReferenceValueComponent()
64 //--------------------------------------------------------------------
65 void OReferenceValueComponent::setReferenceValue( const OUString& _rRefValue )
67 m_sReferenceValue = _rRefValue;
68 calculateExternalValueType();
71 //--------------------------------------------------------------------
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 );
89 //--------------------------------------------------------------------
90 void SAL_CALL OReferenceValueComponent::setFastPropertyValue_NoBroadcast( sal_Int32 _nHandle, const Any& _rValue ) throw (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( (sal_Int16)STATE_NOCHECK );
107 OSL_VERIFY( _rValue >>= nDefaultChecked );
108 m_eDefaultChecked = (ToggleState)nDefaultChecked;
109 resetNoBroadcast();
111 break;
113 default:
114 OBoundControlModel::setFastPropertyValue_NoBroadcast( _nHandle, _rValue );
118 //--------------------------------------------------------------------
119 sal_Bool SAL_CALL OReferenceValueComponent::convertFastPropertyValue( Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue ) throw (IllegalArgumentException)
121 sal_Bool bModified = sal_False;
122 switch ( _nHandle )
124 case PROPERTY_ID_REFVALUE:
125 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_sReferenceValue );
126 break;
128 case PROPERTY_ID_UNCHECKED_REFVALUE:
129 OSL_ENSURE( m_bSupportSecondRefValue, "OReferenceValueComponent::convertFastPropertyValue: not supported!" );
130 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, m_sNoCheckReferenceValue );
131 break;
133 case PROPERTY_ID_DEFAULT_STATE:
134 bModified = tryPropertyValue( _rConvertedValue, _rOldValue, _rValue, (sal_Int16)m_eDefaultChecked );
135 break;
137 default:
138 bModified = OBoundControlModel::convertFastPropertyValue( _rConvertedValue, _rOldValue, _nHandle, _rValue );
139 break;
141 return bModified;
144 //------------------------------------------------------------------------------
145 Any OReferenceValueComponent::getDefaultForReset() const
147 return makeAny( (sal_Int16)m_eDefaultChecked );
150 //--------------------------------------------------------------------
151 void OReferenceValueComponent::describeFixedProperties( Sequence< Property >& _rProps ) const
153 BEGIN_DESCRIBE_PROPERTIES( m_bSupportSecondRefValue ? 3 : 2, OBoundControlModel )
154 DECL_PROP1( REFVALUE, OUString, BOUND );
155 DECL_PROP1( DEFAULT_STATE, sal_Int16, BOUND );
156 if ( m_bSupportSecondRefValue )
158 DECL_PROP1( UNCHECKED_REFVALUE, OUString, BOUND );
160 END_DESCRIBE_PROPERTIES();
163 //-----------------------------------------------------------------------------
164 Sequence< Type > OReferenceValueComponent::getSupportedBindingTypes()
166 ::std::list< Type > aTypes;
167 aTypes.push_back( ::getCppuType( static_cast< sal_Bool* >( NULL ) ) );
169 if ( !m_sReferenceValue.isEmpty() )
170 aTypes.push_front( ::getCppuType( static_cast< OUString* >( NULL ) ) );
171 // push_front, because this is the preferred type
173 Sequence< Type > aTypesRet( aTypes.size() );
174 ::std::copy( aTypes.begin(), aTypes.end(), aTypesRet.getArray() );
175 return aTypesRet;
178 //-----------------------------------------------------------------------------
179 Any OReferenceValueComponent::translateExternalValueToControlValue( const Any& _rExternalValue ) const
181 sal_Int16 nState = STATE_DONTKNOW;
183 sal_Bool bExternalState = sal_False;
184 OUString sExternalValue;
185 if ( _rExternalValue >>= bExternalState )
187 nState = ::sal::static_int_cast< sal_Int16 >( bExternalState ? STATE_CHECK : STATE_NOCHECK );
189 else if ( _rExternalValue >>= sExternalValue )
191 if ( sExternalValue == m_sReferenceValue )
192 nState = STATE_CHECK;
193 else
195 if ( !m_bSupportSecondRefValue || ( sExternalValue == m_sNoCheckReferenceValue ) )
196 nState = STATE_NOCHECK;
197 else
198 nState = STATE_DONTKNOW;
201 else if ( !_rExternalValue.hasValue() )
203 nState = STATE_DONTKNOW;
205 else
207 OSL_FAIL( "OReferenceValueComponent::translateExternalValueToControlValue: unexpected value type!" );
210 return makeAny( nState );
213 //-----------------------------------------------------------------------------
214 Any OReferenceValueComponent::translateControlValueToExternalValue( ) const
216 Any aExternalValue;
220 Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
221 sal_Int16 nControlValue = STATE_DONTKNOW;
222 aControlValue >>= nControlValue;
224 bool bBooleanExchange = getExternalValueType().getTypeClass() == TypeClass_BOOLEAN;
225 bool bStringExchange = getExternalValueType().getTypeClass() == TypeClass_STRING;
226 OSL_ENSURE( bBooleanExchange || bStringExchange,
227 "OReferenceValueComponent::translateControlValueToExternalValue: unexpected value exchange type!" );
229 switch( nControlValue )
231 case STATE_CHECK:
232 if ( bBooleanExchange )
234 aExternalValue <<= (sal_Bool)sal_True;
236 else if ( bStringExchange )
238 aExternalValue <<= m_sReferenceValue;
240 break;
242 case STATE_NOCHECK:
243 if ( bBooleanExchange )
245 aExternalValue <<= (sal_Bool)sal_False;
247 else if ( bStringExchange )
249 aExternalValue <<= (m_bSupportSecondRefValue ? m_sNoCheckReferenceValue : OUString());
251 break;
254 catch( const Exception& )
256 OSL_FAIL( "OReferenceValueComponent::translateControlValueToExternalValue: caught an exception!" );
259 return aExternalValue;
262 //-----------------------------------------------------------------------------
263 Any OReferenceValueComponent::translateControlValueToValidatableValue( ) const
265 if ( !m_xAggregateSet.is() )
266 return Any();
268 Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
269 sal_Int16 nControlValue = STATE_DONTKNOW;
270 aControlValue >>= nControlValue;
272 Any aValidatableValue;
273 switch ( nControlValue )
275 case STATE_CHECK:
276 aValidatableValue <<= (sal_Bool)sal_True;
277 break;
278 case STATE_NOCHECK:
279 aValidatableValue <<= (sal_Bool)sal_False;
280 break;
282 return aValidatableValue;
285 //........................................................................
286 } // namespace frm
287 //........................................................................
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */