Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / forms / source / component / CheckBox.cxx
blobfcfdabc53ae1685cc0c08a079a47791ad2773b35
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 <services.hxx>
23 #include <comphelper/basicio.hxx>
24 #include <tools/debug.hxx>
25 #include <com/sun/star/beans/PropertyAttribute.hpp>
26 #include <com/sun/star/form/FormComponentType.hpp>
28 namespace frm
30 using namespace ::com::sun::star::uno;
31 using namespace ::com::sun::star::sdb;
32 using namespace ::com::sun::star::sdbc;
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 css::uno::Sequence<OUString> SAL_CALL OCheckBoxControl::getSupportedServiceNames()
50 css::uno::Sequence<OUString> 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 )
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 css::uno::Reference< css::util::XCloneable > SAL_CALL OCheckBoxModel::createClone()
85 rtl::Reference<OCheckBoxModel> pClone = new OCheckBoxModel(this, getContext());
86 pClone->clonedFrom(this);
87 return pClone;
91 // XServiceInfo
93 css::uno::Sequence<OUString> SAL_CALL OCheckBoxModel::getSupportedServiceNames()
95 css::uno::Sequence<OUString> aSupported = OReferenceValueComponent::getSupportedServiceNames();
97 sal_Int32 nOldLen = aSupported.getLength();
98 aSupported.realloc( nOldLen + 9 );
99 OUString* pStoreTo = aSupported.getArray() + nOldLen;
101 *pStoreTo++ = BINDABLE_CONTROL_MODEL;
102 *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
103 *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
105 *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
106 *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
108 *pStoreTo++ = FRM_SUN_COMPONENT_CHECKBOX;
109 *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_CHECKBOX;
110 *pStoreTo++ = BINDABLE_DATABASE_CHECK_BOX;
112 *pStoreTo++ = FRM_COMPONENT_CHECKBOX;
114 return aSupported;
118 void OCheckBoxModel::describeFixedProperties( Sequence< Property >& _rProps ) const
120 OReferenceValueComponent::describeFixedProperties( _rProps );
121 sal_Int32 nOldCount = _rProps.getLength();
122 _rProps.realloc( nOldCount + 1);
123 css::beans::Property* pProperties = _rProps.getArray() + nOldCount;
124 *pProperties++ = css::beans::Property(PROPERTY_TABINDEX, PROPERTY_ID_TABINDEX, cppu::UnoType<sal_Int16>::get(), css::beans::PropertyAttribute::BOUND);
125 DBG_ASSERT( pProperties == _rProps.getArray() + _rProps.getLength(), "<...>::describeFixedProperties/getInfoHelper: forgot to adjust the count ?");
129 OUString SAL_CALL OCheckBoxModel::getServiceName()
131 return FRM_COMPONENT_CHECKBOX; // old (non-sun) name for compatibility !
135 void SAL_CALL OCheckBoxModel::write(const Reference<css::io::XObjectOutputStream>& _rxOutStream)
137 OReferenceValueComponent::write(_rxOutStream);
139 // Version
140 _rxOutStream->writeShort(0x0003);
141 // Properties
142 _rxOutStream << getReferenceValue();
143 _rxOutStream << static_cast<sal_Int16>(getDefaultChecked());
144 writeHelpTextCompatibly(_rxOutStream);
145 // from version 0x0003 : common properties
146 writeCommonProperties(_rxOutStream);
150 void SAL_CALL OCheckBoxModel::read(const Reference<css::io::XObjectInputStream>& _rxInStream)
152 OReferenceValueComponent::read(_rxInStream);
153 osl::MutexGuard aGuard(m_aMutex);
155 // Version
156 sal_uInt16 nVersion = _rxInStream->readShort();
158 OUString sReferenceValue;
159 sal_Int16 nDefaultChecked( 0 );
160 switch ( nVersion )
162 case 0x0001:
163 _rxInStream >> sReferenceValue;
164 nDefaultChecked = _rxInStream->readShort();
165 break;
166 case 0x0002:
167 _rxInStream >> sReferenceValue;
168 _rxInStream >> nDefaultChecked;
169 readHelpTextCompatibly( _rxInStream );
170 break;
171 case 0x0003:
172 _rxInStream >> sReferenceValue;
173 _rxInStream >> nDefaultChecked;
174 readHelpTextCompatibly(_rxInStream);
175 readCommonProperties(_rxInStream);
176 break;
177 default:
178 OSL_FAIL("OCheckBoxModel::read : unknown version !");
179 defaultCommonProperties();
180 break;
182 setReferenceValue( sReferenceValue );
183 setDefaultChecked( static_cast< ToggleState >( nDefaultChecked ) );
185 // After reading in, display the default values
186 if ( !getControlSource().isEmpty() )
187 // (not if we don't have a control source - the "State" property acts like it is persistent, then
188 resetNoBroadcast();
191 bool OCheckBoxModel::DbUseBool()
193 return getReferenceValue().isEmpty() && getNoCheckReferenceValue().isEmpty();
197 Any OCheckBoxModel::translateDbColumnToControlValue()
199 Any aValue;
202 // Set value in ControlModel
203 bool bValue = bool(); // avoid warning
204 if(DbUseBool())
206 bValue = m_xColumn->getBoolean();
208 else
210 const OUString sVal(m_xColumn->getString());
211 if (sVal == getReferenceValue())
212 bValue = true;
213 else if (sVal == getNoCheckReferenceValue())
214 bValue = false;
215 else
216 aValue <<= static_cast<sal_Int16>(getDefaultChecked());
218 if ( m_xColumn->wasNull() )
220 bool bTriState = true;
221 if ( m_xAggregateSet.is() )
222 m_xAggregateSet->getPropertyValue( PROPERTY_TRISTATE ) >>= bTriState;
223 aValue <<= static_cast<sal_Int16>( bTriState ? TRISTATE_INDET : getDefaultChecked() );
225 else if ( !aValue.hasValue() )
227 // Since above either bValue is initialised, either aValue.hasValue(),
228 // bValue cannot be used uninitialised here.
229 // But GCC does not see/understand that, which breaks -Werror builds,
230 // so we explicitly default-initialise it.
231 aValue <<= static_cast<sal_Int16>( bValue ? TRISTATE_TRUE : TRISTATE_FALSE );
234 return aValue;
238 bool OCheckBoxModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
240 OSL_PRECOND( m_xColumnUpdate.is(), "OCheckBoxModel::commitControlValueToDbColumn: not bound!" );
241 if ( !m_xColumnUpdate )
242 return true;
244 Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
247 sal_Int16 nValue = TRISTATE_INDET;
248 aControlValue >>= nValue;
249 switch (nValue)
251 case TRISTATE_INDET:
252 m_xColumnUpdate->updateNull();
253 break;
254 case TRISTATE_TRUE:
255 if (DbUseBool())
256 m_xColumnUpdate->updateBoolean( true );
257 else
258 m_xColumnUpdate->updateString( getReferenceValue() );
259 break;
260 case TRISTATE_FALSE:
261 if (DbUseBool())
262 m_xColumnUpdate->updateBoolean( false );
263 else
264 m_xColumnUpdate->updateString( getNoCheckReferenceValue() );
265 break;
266 default:
267 OSL_FAIL("OCheckBoxModel::commitControlValueToDbColumn: invalid value !");
270 catch(const Exception&)
272 OSL_FAIL("OCheckBoxModel::commitControlValueToDbColumn: could not commit !");
274 return true;
279 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
280 com_sun_star_form_OCheckBoxModel_get_implementation(css::uno::XComponentContext* component,
281 css::uno::Sequence<css::uno::Any> const &)
283 return cppu::acquire(new frm::OCheckBoxModel(component));
286 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
287 com_sun_star_form_OCheckBoxControl_get_implementation(css::uno::XComponentContext* component,
288 css::uno::Sequence<css::uno::Any> const &)
290 return cppu::acquire(new frm::OCheckBoxControl(component));
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */