tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / forms / source / component / CheckBox.cxx
blobff3f2058ffcce02dfc526b2bcbd1396674cdcd60
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::form;
35 using namespace ::com::sun::star::io;
36 using namespace ::com::sun::star::util;
38 OCheckBoxControl::OCheckBoxControl(const Reference<XComponentContext>& _rxFactory)
39 :OBoundControl(_rxFactory, VCL_CONTROL_CHECKBOX)
44 css::uno::Sequence<OUString> SAL_CALL OCheckBoxControl::getSupportedServiceNames()
46 css::uno::Sequence<OUString> aSupported = OBoundControl::getSupportedServiceNames();
47 aSupported.realloc(aSupported.getLength() + 2);
49 OUString* pArray = aSupported.getArray();
50 pArray[aSupported.getLength()-2] = FRM_SUN_CONTROL_CHECKBOX;
51 pArray[aSupported.getLength()-1] = STARDIV_ONE_FORM_CONTROL_CHECKBOX;
52 return aSupported;
56 //= OCheckBoxModel
58 OCheckBoxModel::OCheckBoxModel(const Reference<XComponentContext>& _rxFactory)
59 :OReferenceValueComponent( _rxFactory, VCL_CONTROLMODEL_CHECKBOX, FRM_SUN_CONTROL_CHECKBOX )
60 // use the old control name for compatibility reasons
63 m_nClassId = FormComponentType::CHECKBOX;
64 initValueProperty( PROPERTY_STATE, PROPERTY_ID_STATE );
68 OCheckBoxModel::OCheckBoxModel( const OCheckBoxModel* _pOriginal, const Reference<XComponentContext>& _rxFactory )
69 :OReferenceValueComponent( _pOriginal, _rxFactory )
74 OCheckBoxModel::~OCheckBoxModel()
79 css::uno::Reference< css::util::XCloneable > SAL_CALL OCheckBoxModel::createClone()
81 rtl::Reference<OCheckBoxModel> pClone = new OCheckBoxModel(this, getContext());
82 pClone->clonedFrom(this);
83 return pClone;
87 // XServiceInfo
89 css::uno::Sequence<OUString> SAL_CALL OCheckBoxModel::getSupportedServiceNames()
91 css::uno::Sequence<OUString> aSupported = OReferenceValueComponent::getSupportedServiceNames();
93 sal_Int32 nOldLen = aSupported.getLength();
94 aSupported.realloc( nOldLen + 9 );
95 OUString* pStoreTo = aSupported.getArray() + nOldLen;
97 *pStoreTo++ = BINDABLE_CONTROL_MODEL;
98 *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
99 *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
101 *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
102 *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
104 *pStoreTo++ = FRM_SUN_COMPONENT_CHECKBOX;
105 *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_CHECKBOX;
106 *pStoreTo++ = BINDABLE_DATABASE_CHECK_BOX;
108 *pStoreTo++ = FRM_COMPONENT_CHECKBOX;
110 return aSupported;
114 void OCheckBoxModel::describeFixedProperties( Sequence< Property >& _rProps ) const
116 OReferenceValueComponent::describeFixedProperties( _rProps );
117 sal_Int32 nOldCount = _rProps.getLength();
118 _rProps.realloc( nOldCount + 1);
119 css::beans::Property* pProperties = _rProps.getArray() + nOldCount;
120 *pProperties++ = css::beans::Property(PROPERTY_TABINDEX, PROPERTY_ID_TABINDEX, cppu::UnoType<sal_Int16>::get(), css::beans::PropertyAttribute::BOUND);
121 DBG_ASSERT( pProperties == _rProps.getArray() + _rProps.getLength(), "<...>::describeFixedProperties/getInfoHelper: forgot to adjust the count ?");
125 OUString SAL_CALL OCheckBoxModel::getServiceName()
127 return FRM_COMPONENT_CHECKBOX; // old (non-sun) name for compatibility !
131 void SAL_CALL OCheckBoxModel::write(const Reference<css::io::XObjectOutputStream>& _rxOutStream)
133 OReferenceValueComponent::write(_rxOutStream);
135 // Version
136 _rxOutStream->writeShort(0x0003);
137 // Properties
138 _rxOutStream << getReferenceValue();
139 _rxOutStream << static_cast<sal_Int16>(getDefaultChecked());
140 writeHelpTextCompatibly(_rxOutStream);
141 // from version 0x0003 : common properties
142 writeCommonProperties(_rxOutStream);
146 void SAL_CALL OCheckBoxModel::read(const Reference<css::io::XObjectInputStream>& _rxInStream)
148 OReferenceValueComponent::read(_rxInStream);
149 osl::MutexGuard aGuard(m_aMutex);
151 // Version
152 sal_uInt16 nVersion = _rxInStream->readShort();
154 OUString sReferenceValue;
155 sal_Int16 nDefaultChecked( 0 );
156 switch ( nVersion )
158 case 0x0001:
159 _rxInStream >> sReferenceValue;
160 nDefaultChecked = _rxInStream->readShort();
161 break;
162 case 0x0002:
163 _rxInStream >> sReferenceValue;
164 _rxInStream >> nDefaultChecked;
165 readHelpTextCompatibly( _rxInStream );
166 break;
167 case 0x0003:
168 _rxInStream >> sReferenceValue;
169 _rxInStream >> nDefaultChecked;
170 readHelpTextCompatibly(_rxInStream);
171 readCommonProperties(_rxInStream);
172 break;
173 default:
174 OSL_FAIL("OCheckBoxModel::read : unknown version !");
175 defaultCommonProperties();
176 break;
178 setReferenceValue( sReferenceValue );
179 setDefaultChecked( static_cast< ToggleState >( nDefaultChecked ) );
181 // After reading in, display the default values
182 if ( !getControlSource().isEmpty() )
183 // (not if we don't have a control source - the "State" property acts like it is persistent, then
184 resetNoBroadcast();
187 bool OCheckBoxModel::DbUseBool()
189 return getReferenceValue().isEmpty() && getNoCheckReferenceValue().isEmpty();
193 Any OCheckBoxModel::translateDbColumnToControlValue()
195 Any aValue;
198 // Set value in ControlModel
199 bool bValue = bool(); // avoid warning
200 if(DbUseBool())
202 bValue = m_xColumn->getBoolean();
204 else
206 const OUString sVal(m_xColumn->getString());
207 if (sVal == getReferenceValue())
208 bValue = true;
209 else if (sVal == getNoCheckReferenceValue())
210 bValue = false;
211 else
212 aValue <<= static_cast<sal_Int16>(getDefaultChecked());
214 if ( m_xColumn->wasNull() )
216 bool bTriState = true;
217 if ( m_xAggregateSet.is() )
218 m_xAggregateSet->getPropertyValue( PROPERTY_TRISTATE ) >>= bTriState;
219 aValue <<= static_cast<sal_Int16>( bTriState ? TRISTATE_INDET : getDefaultChecked() );
221 else if ( !aValue.hasValue() )
223 // Since above either bValue is initialised, either aValue.hasValue(),
224 // bValue cannot be used uninitialised here.
225 // But GCC does not see/understand that, which breaks -Werror builds,
226 // so we explicitly default-initialise it.
227 aValue <<= static_cast<sal_Int16>( bValue ? TRISTATE_TRUE : TRISTATE_FALSE );
230 return aValue;
234 bool OCheckBoxModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
236 OSL_PRECOND( m_xColumnUpdate.is(), "OCheckBoxModel::commitControlValueToDbColumn: not bound!" );
237 if ( !m_xColumnUpdate )
238 return true;
240 Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
243 sal_Int16 nValue = TRISTATE_INDET;
244 aControlValue >>= nValue;
245 switch (nValue)
247 case TRISTATE_INDET:
248 m_xColumnUpdate->updateNull();
249 break;
250 case TRISTATE_TRUE:
251 if (DbUseBool())
252 m_xColumnUpdate->updateBoolean( true );
253 else
254 m_xColumnUpdate->updateString( getReferenceValue() );
255 break;
256 case TRISTATE_FALSE:
257 if (DbUseBool())
258 m_xColumnUpdate->updateBoolean( false );
259 else
260 m_xColumnUpdate->updateString( getNoCheckReferenceValue() );
261 break;
262 default:
263 OSL_FAIL("OCheckBoxModel::commitControlValueToDbColumn: invalid value !");
266 catch(const Exception&)
268 OSL_FAIL("OCheckBoxModel::commitControlValueToDbColumn: could not commit !");
270 return true;
275 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
276 com_sun_star_form_OCheckBoxModel_get_implementation(css::uno::XComponentContext* component,
277 css::uno::Sequence<css::uno::Any> const &)
279 return cppu::acquire(new frm::OCheckBoxModel(component));
282 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
283 com_sun_star_form_OCheckBoxControl_get_implementation(css::uno::XComponentContext* component,
284 css::uno::Sequence<css::uno::Any> const &)
286 return cppu::acquire(new frm::OCheckBoxControl(component));
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */