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::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
;
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);
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
;
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
);
136 _rxOutStream
->writeShort(0x0003);
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
);
152 sal_uInt16 nVersion
= _rxInStream
->readShort();
154 OUString sReferenceValue
;
155 sal_Int16
nDefaultChecked( 0 );
159 _rxInStream
>> sReferenceValue
;
160 nDefaultChecked
= _rxInStream
->readShort();
163 _rxInStream
>> sReferenceValue
;
164 _rxInStream
>> nDefaultChecked
;
165 readHelpTextCompatibly( _rxInStream
);
168 _rxInStream
>> sReferenceValue
;
169 _rxInStream
>> nDefaultChecked
;
170 readHelpTextCompatibly(_rxInStream
);
171 readCommonProperties(_rxInStream
);
174 OSL_FAIL("OCheckBoxModel::read : unknown version !");
175 defaultCommonProperties();
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
187 bool OCheckBoxModel::DbUseBool()
189 return getReferenceValue().isEmpty() && getNoCheckReferenceValue().isEmpty();
193 Any
OCheckBoxModel::translateDbColumnToControlValue()
198 // Set value in ControlModel
199 bool bValue
= bool(); // avoid warning
202 bValue
= m_xColumn
->getBoolean();
206 const OUString
sVal(m_xColumn
->getString());
207 if (sVal
== getReferenceValue())
209 else if (sVal
== getNoCheckReferenceValue())
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
);
234 bool OCheckBoxModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
236 OSL_PRECOND( m_xColumnUpdate
.is(), "OCheckBoxModel::commitControlValueToDbColumn: not bound!" );
237 if ( !m_xColumnUpdate
)
240 Any
aControlValue( m_xAggregateSet
->getPropertyValue( PROPERTY_STATE
) );
243 sal_Int16 nValue
= TRISTATE_INDET
;
244 aControlValue
>>= nValue
;
248 m_xColumnUpdate
->updateNull();
252 m_xColumnUpdate
->updateBoolean( true );
254 m_xColumnUpdate
->updateString( getReferenceValue() );
258 m_xColumnUpdate
->updateBoolean( false );
260 m_xColumnUpdate
->updateString( getNoCheckReferenceValue() );
263 OSL_FAIL("OCheckBoxModel::commitControlValueToDbColumn: invalid value !");
266 catch(const Exception
&)
268 OSL_FAIL("OCheckBoxModel::commitControlValueToDbColumn: could not commit !");
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: */