LibreOffice Module forms (master) 1
CheckBox.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "CheckBox.hxx"
21#include <property.hxx>
22#include <services.hxx>
24#include <tools/debug.hxx>
25#include <com/sun/star/beans/PropertyAttribute.hpp>
26#include <com/sun/star/form/FormComponentType.hpp>
27
28namespace frm
29{
30using namespace ::com::sun::star::uno;
31using namespace ::com::sun::star::sdb;
32using namespace ::com::sun::star::sdbc;
33using namespace ::com::sun::star::beans;
34using namespace ::com::sun::star::container;
35using namespace ::com::sun::star::form;
36using namespace ::com::sun::star::awt;
37using namespace ::com::sun::star::io;
38using namespace ::com::sun::star::lang;
39using namespace ::com::sun::star::util;
40using namespace ::com::sun::star::form::binding;
41
42OCheckBoxControl::OCheckBoxControl(const Reference<XComponentContext>& _rxFactory)
44{
45}
46
47
48css::uno::Sequence<OUString> SAL_CALL OCheckBoxControl::getSupportedServiceNames()
49{
50 css::uno::Sequence<OUString> aSupported = OBoundControl::getSupportedServiceNames();
51 aSupported.realloc(aSupported.getLength() + 2);
52
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;
57}
58
59
60//= OCheckBoxModel
61
62OCheckBoxModel::OCheckBoxModel(const Reference<XComponentContext>& _rxFactory)
64 // use the old control name for compytibility reasons
65{
66
67 m_nClassId = FormComponentType::CHECKBOX;
68 initValueProperty( PROPERTY_STATE, PROPERTY_ID_STATE );
69}
70
71
72OCheckBoxModel::OCheckBoxModel( const OCheckBoxModel* _pOriginal, const Reference<XComponentContext>& _rxFactory )
73 :OReferenceValueComponent( _pOriginal, _rxFactory )
74{
75}
76
77
79{
80}
81
82
83css::uno::Reference< css::util::XCloneable > SAL_CALL OCheckBoxModel::createClone()
84{
86 pClone->clonedFrom(this);
87 return pClone;
88}
89
90
91// XServiceInfo
92
93css::uno::Sequence<OUString> SAL_CALL OCheckBoxModel::getSupportedServiceNames()
94{
95 css::uno::Sequence<OUString> aSupported = OReferenceValueComponent::getSupportedServiceNames();
96
97 sal_Int32 nOldLen = aSupported.getLength();
98 aSupported.realloc( nOldLen + 9 );
99 OUString* pStoreTo = aSupported.getArray() + nOldLen;
100
101 *pStoreTo++ = BINDABLE_CONTROL_MODEL;
102 *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
103 *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
104
107
108 *pStoreTo++ = FRM_SUN_COMPONENT_CHECKBOX;
110 *pStoreTo++ = BINDABLE_DATABASE_CHECK_BOX;
111
112 *pStoreTo++ = FRM_COMPONENT_CHECKBOX;
113
114 return aSupported;
115}
116
117
118void OCheckBoxModel::describeFixedProperties( Sequence< Property >& _rProps ) const
119{
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 ?");
126}
127
128
130{
131 return FRM_COMPONENT_CHECKBOX; // old (non-sun) name for compatibility !
132}
133
134
135void SAL_CALL OCheckBoxModel::write(const Reference<css::io::XObjectOutputStream>& _rxOutStream)
136{
138
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);
147}
148
149
150void SAL_CALL OCheckBoxModel::read(const Reference<css::io::XObjectInputStream>& _rxInStream)
151{
153 osl::MutexGuard aGuard(m_aMutex);
154
155 // Version
156 sal_uInt16 nVersion = _rxInStream->readShort();
157
158 OUString sReferenceValue;
159 sal_Int16 nDefaultChecked( 0 );
160 switch ( nVersion )
161 {
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 !");
180 break;
181 }
182 setReferenceValue( sReferenceValue );
183 setDefaultChecked( static_cast< ToggleState >( nDefaultChecked ) );
184
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
189}
190
192{
193 return getReferenceValue().isEmpty() && getNoCheckReferenceValue().isEmpty();
194}
195
196
198{
199 Any aValue;
200
201
202 // Set value in ControlModel
203 bool bValue = bool(); // avoid warning
204 if(DbUseBool())
205 {
206 bValue = m_xColumn->getBoolean();
207 }
208 else
209 {
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());
217 }
218 if ( m_xColumn->wasNull() )
219 {
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() );
224 }
225 else if ( !aValue.hasValue() )
226 {
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 );
232 }
233
234 return aValue;
235}
236
237
239{
240 OSL_PRECOND( m_xColumnUpdate.is(), "OCheckBoxModel::commitControlValueToDbColumn: not bound!" );
241 if ( !m_xColumnUpdate )
242 return true;
243
244 Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
245 try
246 {
247 sal_Int16 nValue = TRISTATE_INDET;
248 aControlValue >>= nValue;
249 switch (nValue)
250 {
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 !");
268 }
269 }
270 catch(const Exception&)
271 {
272 OSL_FAIL("OCheckBoxModel::commitControlValueToDbColumn: could not commit !");
273 }
274 return true;
275}
276
277}
278
279extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
280com_sun_star_form_OCheckBoxModel_get_implementation(css::uno::XComponentContext* component,
281 css::uno::Sequence<css::uno::Any> const &)
282{
283 return cppu::acquire(new frm::OCheckBoxModel(component));
284}
285
286extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
287com_sun_star_form_OCheckBoxControl_get_implementation(css::uno::XComponentContext* component,
288 css::uno::Sequence<css::uno::Any> const &)
289{
290 return cppu::acquire(new frm::OCheckBoxControl(component));
291}
292
293/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_form_OCheckBoxControl_get_implementation(css::uno::XComponentContext *component, css::uno::Sequence< css::uno::Any > const &)
Definition: CheckBox.cxx:287
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_form_OCheckBoxModel_get_implementation(css::uno::XComponentContext *component, css::uno::Sequence< css::uno::Any > const &)
Definition: CheckBox.cxx:280
void writeCommonProperties(const css::uno::Reference< css::io::XObjectOutputStream > &_rxOutStream)
We can't write (new) common properties in this base class, as the file format doesn't allow this (unf...
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
css::uno::Reference< css::sdb::XColumn > m_xColumn
virtual void resetNoBroadcast()
called to reset the control to some kind of default.
const OUString & getControlSource() const
virtual void SAL_CALL write(const css::uno::Reference< css::io::XObjectOutputStream > &OutStream) override
virtual void SAL_CALL read(const css::uno::Reference< css::io::XObjectInputStream > &InStream) override
void readCommonProperties(const css::uno::Reference< css::io::XObjectInputStream > &_rxInStream)
css::uno::Reference< css::sdb::XColumnUpdate > m_xColumnUpdate
OCheckBoxControl(const css::uno::Reference< css::uno::XComponentContext > &_rxContext)
Definition: CheckBox.cxx:42
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: CheckBox.cxx:48
virtual ~OCheckBoxModel() override
Definition: CheckBox.cxx:78
virtual css::uno::Any translateDbColumnToControlValue() override
translates a db column value into a control value.
Definition: CheckBox.cxx:197
virtual bool commitControlValueToDbColumn(bool _bPostReset) override
commits the current control value to the database column we're bound to @precond we're properly bound...
Definition: CheckBox.cxx:238
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: CheckBox.cxx:93
virtual void SAL_CALL write(const css::uno::Reference< css::io::XObjectOutputStream > &_rxOutStream) override
Definition: CheckBox.cxx:135
virtual void describeFixedProperties(css::uno::Sequence< css::beans::Property > &_rProps) const override
describes the properties provided by this class, or its respective derived class
Definition: CheckBox.cxx:118
virtual void SAL_CALL read(const css::uno::Reference< css::io::XObjectInputStream > &_rxInStream) override
Definition: CheckBox.cxx:150
virtual css::uno::Reference< css::util::XCloneable > SAL_CALL createClone() override
Definition: CheckBox.cxx:83
OCheckBoxModel(const css::uno::Reference< css::uno::XComponentContext > &_rxFactory)
virtual OUString SAL_CALL getServiceName() override
Definition: CheckBox.cxx:129
::osl::Mutex m_aMutex
const css::uno::Reference< css::uno::XComponentContext > & getContext() const
void writeHelpTextCompatibly(const css::uno::Reference< css::io::XObjectOutputStream > &_rxOutStream)
void readHelpTextCompatibly(const css::uno::Reference< css::io::XObjectInputStream > &_rxInStream)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
an OBoundControlModel which features the exchange of a reference value
void setDefaultChecked(ToggleState _eChecked)
const OUString & getReferenceValue() const
ToggleState getDefaultChecked() const
virtual void describeFixedProperties(css::uno::Sequence< css::beans::Property > &_rProps) const override
describes the properties provided by this class, or its respective derived class
const OUString & getNoCheckReferenceValue() const
void setReferenceValue(const OUString &_rRefValue)
#define DBG_ASSERT(sCon, aError)
sal_Int16 nVersion
sal_Int16 nValue
constexpr OUStringLiteral PROPERTY_TABINDEX
Definition: frm_strings.hxx:26
constexpr OUStringLiteral PROPERTY_STATE
Definition: frm_strings.hxx:58
constexpr OUStringLiteral PROPERTY_TRISTATE
Definition: frm_strings.hxx:59
@ Exception
ListBox is a bit confusing / different from other form components, so here are a few notes:
Definition: BaseListBox.hxx:25
ToggleState
Definition: togglestate.hxx:27
@ TRISTATE_INDET
Definition: togglestate.hxx:27
@ TRISTATE_FALSE
Definition: togglestate.hxx:27
@ TRISTATE_TRUE
Definition: togglestate.hxx:27
#define PROPERTY_ID_STATE
Definition: property.hxx:87
#define PROPERTY_ID_TABINDEX
Definition: property.hxx:41
constexpr OUStringLiteral BINDABLE_DATABASE_CHECK_BOX
Definition: services.hxx:169
constexpr OUStringLiteral VCL_CONTROLMODEL_CHECKBOX
Definition: services.hxx:46
constexpr OUStringLiteral FRM_COMPONENT_CHECKBOX
Definition: services.hxx:73
constexpr OUStringLiteral VALIDATABLE_BINDABLE_CONTROL_MODEL
Definition: services.hxx:184
constexpr OUStringLiteral VCL_CONTROL_CHECKBOX
Definition: services.hxx:29
constexpr OUStringLiteral BINDABLE_DATA_AWARE_CONTROL_MODEL
Definition: services.hxx:181
constexpr OUStringLiteral FRM_SUN_COMPONENT_CHECKBOX
Definition: services.hxx:120
constexpr OUStringLiteral DATA_AWARE_CONTROL_MODEL
Definition: services.hxx:182
constexpr OUStringLiteral VALIDATABLE_CONTROL_MODEL
Definition: services.hxx:183
constexpr OUStringLiteral FRM_SUN_COMPONENT_DATABASE_CHECKBOX
Definition: services.hxx:139
constexpr OUStringLiteral BINDABLE_CONTROL_MODEL
Definition: services.hxx:179
constexpr OUStringLiteral STARDIV_ONE_FORM_CONTROL_CHECKBOX
Definition: services.hxx:91
constexpr OUStringLiteral FRM_SUN_CONTROL_CHECKBOX
Definition: services.hxx:156