1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
;
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);
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
;
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
);
140 _rxOutStream
->writeShort(0x0003);
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
);
156 sal_uInt16 nVersion
= _rxInStream
->readShort();
158 OUString sReferenceValue
;
159 sal_Int16
nDefaultChecked( 0 );
163 _rxInStream
>> sReferenceValue
;
164 nDefaultChecked
= _rxInStream
->readShort();
167 _rxInStream
>> sReferenceValue
;
168 _rxInStream
>> nDefaultChecked
;
169 readHelpTextCompatibly( _rxInStream
);
172 _rxInStream
>> sReferenceValue
;
173 _rxInStream
>> nDefaultChecked
;
174 readHelpTextCompatibly(_rxInStream
);
175 readCommonProperties(_rxInStream
);
178 OSL_FAIL("OCheckBoxModel::read : unknown version !");
179 defaultCommonProperties();
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
191 bool OCheckBoxModel::DbUseBool()
193 return getReferenceValue().isEmpty() && getNoCheckReferenceValue().isEmpty();
197 Any
OCheckBoxModel::translateDbColumnToControlValue()
202 // Set value in ControlModel
203 bool bValue
= bool(); // avoid warning
206 bValue
= m_xColumn
->getBoolean();
210 const OUString
sVal(m_xColumn
->getString());
211 if (sVal
== getReferenceValue())
213 else if (sVal
== getNoCheckReferenceValue())
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
);
238 bool OCheckBoxModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
240 OSL_PRECOND( m_xColumnUpdate
.is(), "OCheckBoxModel::commitControlValueToDbColumn: not bound!" );
241 if ( !m_xColumnUpdate
)
244 Any
aControlValue( m_xAggregateSet
->getPropertyValue( PROPERTY_STATE
) );
247 sal_Int16 nValue
= TRISTATE_INDET
;
248 aControlValue
>>= nValue
;
252 m_xColumnUpdate
->updateNull();
256 m_xColumnUpdate
->updateBoolean( true );
258 m_xColumnUpdate
->updateString( getReferenceValue() );
262 m_xColumnUpdate
->updateBoolean( false );
264 m_xColumnUpdate
->updateString( getNoCheckReferenceValue() );
267 OSL_FAIL("OCheckBoxModel::commitControlValueToDbColumn: invalid value !");
270 catch(const Exception
&)
272 OSL_FAIL("OCheckBoxModel::commitControlValueToDbColumn: could not commit !");
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: */