bump product version to 5.0.4.1
[LibreOffice.git] / forms / source / component / CheckBox.cxx
blobdc4d86fe3e09db439f59898a0a4404ab0f6b1a7a
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 "CheckBox.hxx"
21 #include "property.hxx"
22 #include "property.hrc"
23 #include "services.hxx"
24 #include <comphelper/basicio.hxx>
25 #include <comphelper/processfactory.hxx>
27 namespace frm
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::sdb;
31 using namespace ::com::sun::star::sdbc;
32 using namespace ::com::sun::star::sdbcx;
33 using namespace ::com::sun::star::beans;
34 using namespace ::com::sun::star::container;
35 using namespace ::com::sun::star::form;
36 using namespace ::com::sun::star::awt;
37 using namespace ::com::sun::star::io;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::util;
40 using namespace ::com::sun::star::form::binding;
42 OCheckBoxControl::OCheckBoxControl(const Reference<XComponentContext>& _rxFactory)
43 :OBoundControl(_rxFactory, VCL_CONTROL_CHECKBOX)
48 StringSequence SAL_CALL OCheckBoxControl::getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException, std::exception)
50 StringSequence aSupported = OBoundControl::getSupportedServiceNames();
51 aSupported.realloc(aSupported.getLength() + 2);
53 OUString* pArray = aSupported.getArray();
54 pArray[aSupported.getLength()-2] = FRM_SUN_CONTROL_CHECKBOX;
55 pArray[aSupported.getLength()-1] = STARDIV_ONE_FORM_CONTROL_CHECKBOX;
56 return aSupported;
60 //= OCheckBoxModel
62 OCheckBoxModel::OCheckBoxModel(const Reference<XComponentContext>& _rxFactory)
63 :OReferenceValueComponent( _rxFactory, VCL_CONTROLMODEL_CHECKBOX, FRM_SUN_CONTROL_CHECKBOX, true )
64 // use the old control name for compytibility reasons
67 m_nClassId = FormComponentType::CHECKBOX;
68 initValueProperty( PROPERTY_STATE, PROPERTY_ID_STATE );
72 OCheckBoxModel::OCheckBoxModel( const OCheckBoxModel* _pOriginal, const Reference<XComponentContext>& _rxFactory )
73 :OReferenceValueComponent( _pOriginal, _rxFactory )
78 OCheckBoxModel::~OCheckBoxModel()
83 IMPLEMENT_DEFAULT_CLONING( OCheckBoxModel )
85 // XServiceInfo
87 StringSequence SAL_CALL OCheckBoxModel::getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException, std::exception)
89 StringSequence aSupported = OReferenceValueComponent::getSupportedServiceNames();
91 sal_Int32 nOldLen = aSupported.getLength();
92 aSupported.realloc( nOldLen + 9 );
93 OUString* pStoreTo = aSupported.getArray() + nOldLen;
95 *pStoreTo++ = BINDABLE_CONTROL_MODEL;
96 *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
97 *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
99 *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
100 *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
102 *pStoreTo++ = FRM_SUN_COMPONENT_CHECKBOX;
103 *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_CHECKBOX;
104 *pStoreTo++ = BINDABLE_DATABASE_CHECK_BOX;
106 *pStoreTo++ = FRM_COMPONENT_CHECKBOX;
108 return aSupported;
112 void OCheckBoxModel::describeFixedProperties( Sequence< Property >& _rProps ) const
114 BEGIN_DESCRIBE_PROPERTIES( 1, OReferenceValueComponent )
115 DECL_PROP1(TABINDEX, sal_Int16, BOUND);
116 END_DESCRIBE_PROPERTIES();
120 OUString SAL_CALL OCheckBoxModel::getServiceName() throw(RuntimeException, std::exception)
122 return OUString(FRM_COMPONENT_CHECKBOX); // old (non-sun) name for compatibility !
126 void SAL_CALL OCheckBoxModel::write(const Reference<css::io::XObjectOutputStream>& _rxOutStream)
127 throw(css::io::IOException, RuntimeException, std::exception)
129 OReferenceValueComponent::write(_rxOutStream);
131 // Version
132 _rxOutStream->writeShort(0x0003);
133 // Properties
134 _rxOutStream << getReferenceValue();
135 _rxOutStream << (sal_Int16)getDefaultChecked();
136 writeHelpTextCompatibly(_rxOutStream);
137 // from version 0x0003 : common properties
138 writeCommonProperties(_rxOutStream);
142 void SAL_CALL OCheckBoxModel::read(const Reference<css::io::XObjectInputStream>& _rxInStream) throw(css::io::IOException, RuntimeException, std::exception)
144 OReferenceValueComponent::read(_rxInStream);
145 osl::MutexGuard aGuard(m_aMutex);
147 // Version
148 sal_uInt16 nVersion = _rxInStream->readShort();
150 OUString sReferenceValue;
151 sal_Int16 nDefaultChecked( 0 );
152 switch ( nVersion )
154 case 0x0001:
155 _rxInStream >> sReferenceValue;
156 nDefaultChecked = _rxInStream->readShort();
157 break;
158 case 0x0002:
159 _rxInStream >> sReferenceValue;
160 _rxInStream >> nDefaultChecked;
161 readHelpTextCompatibly( _rxInStream );
162 break;
163 case 0x0003:
164 _rxInStream >> sReferenceValue;
165 _rxInStream >> nDefaultChecked;
166 readHelpTextCompatibly(_rxInStream);
167 readCommonProperties(_rxInStream);
168 break;
169 default:
170 OSL_FAIL("OCheckBoxModel::read : unknown version !");
171 defaultCommonProperties();
172 break;
174 setReferenceValue( sReferenceValue );
175 setDefaultChecked( static_cast< ToggleState >( nDefaultChecked ) );
177 // After reading in, display the default values
178 if ( !getControlSource().isEmpty() )
179 // (not if we don't have a control source - the "State" property acts like it is persistent, then
180 resetNoBroadcast();
183 bool OCheckBoxModel::DbUseBool()
185 if ( ! (getReferenceValue().isEmpty() && getNoCheckReferenceValue().isEmpty()) )
186 return false;
187 return true;
191 Any OCheckBoxModel::translateDbColumnToControlValue()
193 Any aValue;
196 // Set value in ControlModel
197 bool bValue = bool(); // avoid warning
198 if(DbUseBool())
200 bValue = m_xColumn->getBoolean();
202 else
204 const OUString sVal(m_xColumn->getString());
205 if (sVal == getReferenceValue())
206 bValue = true;
207 else if (sVal == getNoCheckReferenceValue())
208 bValue = false;
209 else
210 aValue <<= static_cast<sal_Int16>(getDefaultChecked());
212 if ( m_xColumn->wasNull() )
214 bool bTriState = true;
215 if ( m_xAggregateSet.is() )
216 m_xAggregateSet->getPropertyValue( PROPERTY_TRISTATE ) >>= bTriState;
217 aValue <<= (sal_Int16)( bTriState ? TRISTATE_INDET : getDefaultChecked() );
219 else if ( !aValue.hasValue() )
221 // Since above either bValue is initialised, either aValue.hasValue(),
222 // bValue cannot be used uninitialised here.
223 // But GCC does not see/understand that, which breaks -Werror builds,
224 // so we explicitly default-initialise it.
225 aValue <<= (sal_Int16)( bValue ? TRISTATE_TRUE : TRISTATE_FALSE );
228 return aValue;
232 bool OCheckBoxModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
234 OSL_PRECOND( m_xColumnUpdate.is(), "OCheckBoxModel::commitControlValueToDbColumn: not bound!" );
235 if ( m_xColumnUpdate.is() )
237 Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
240 sal_Int16 nValue = TRISTATE_INDET;
241 aControlValue >>= nValue;
242 switch (nValue)
244 case TRISTATE_INDET:
245 m_xColumnUpdate->updateNull();
246 break;
247 case TRISTATE_TRUE:
248 if (DbUseBool())
249 m_xColumnUpdate->updateBoolean( sal_True );
250 else
251 m_xColumnUpdate->updateString( getReferenceValue() );
252 break;
253 case TRISTATE_FALSE:
254 if (DbUseBool())
255 m_xColumnUpdate->updateBoolean( sal_False );
256 else
257 m_xColumnUpdate->updateString( getNoCheckReferenceValue() );
258 break;
259 default:
260 OSL_FAIL("OCheckBoxModel::commitControlValueToDbColumn: invalid value !");
263 catch(const Exception&)
265 OSL_FAIL("OCheckBoxModel::commitControlValueToDbColumn: could not commit !");
268 return true;
273 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
274 com_sun_star_form_OCheckBoxModel_get_implementation(::com::sun::star::uno::XComponentContext* component,
275 ::com::sun::star::uno::Sequence<css::uno::Any> const &)
277 return cppu::acquire(new frm::OCheckBoxModel(component));
280 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
281 com_sun_star_form_OCheckBoxControl_get_implementation(::com::sun::star::uno::XComponentContext* component,
282 ::com::sun::star::uno::Sequence<css::uno::Any> const &)
284 return cppu::acquire(new frm::OCheckBoxControl(component));
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */