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 <com/sun/star/form/FormComponentType.hpp>
28 using namespace ::com::sun::star::uno
;
29 using namespace ::com::sun::star::sdb
;
30 using namespace ::com::sun::star::sdbc
;
31 using namespace ::com::sun::star::beans
;
32 using namespace ::com::sun::star::container
;
33 using namespace ::com::sun::star::form
;
34 using namespace ::com::sun::star::awt
;
35 using namespace ::com::sun::star::io
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::com::sun::star::util
;
38 using namespace ::com::sun::star::form::binding
;
40 OCheckBoxControl::OCheckBoxControl(const Reference
<XComponentContext
>& _rxFactory
)
41 :OBoundControl(_rxFactory
, VCL_CONTROL_CHECKBOX
)
46 css::uno::Sequence
<OUString
> SAL_CALL
OCheckBoxControl::getSupportedServiceNames()
48 css::uno::Sequence
<OUString
> aSupported
= OBoundControl::getSupportedServiceNames();
49 aSupported
.realloc(aSupported
.getLength() + 2);
51 OUString
* pArray
= aSupported
.getArray();
52 pArray
[aSupported
.getLength()-2] = FRM_SUN_CONTROL_CHECKBOX
;
53 pArray
[aSupported
.getLength()-1] = STARDIV_ONE_FORM_CONTROL_CHECKBOX
;
60 OCheckBoxModel::OCheckBoxModel(const Reference
<XComponentContext
>& _rxFactory
)
61 :OReferenceValueComponent( _rxFactory
, VCL_CONTROLMODEL_CHECKBOX
, FRM_SUN_CONTROL_CHECKBOX
)
62 // use the old control name for compytibility reasons
65 m_nClassId
= FormComponentType::CHECKBOX
;
66 initValueProperty( PROPERTY_STATE
, PROPERTY_ID_STATE
);
70 OCheckBoxModel::OCheckBoxModel( const OCheckBoxModel
* _pOriginal
, const Reference
<XComponentContext
>& _rxFactory
)
71 :OReferenceValueComponent( _pOriginal
, _rxFactory
)
76 OCheckBoxModel::~OCheckBoxModel()
81 IMPLEMENT_DEFAULT_CLONING( OCheckBoxModel
)
85 css::uno::Sequence
<OUString
> SAL_CALL
OCheckBoxModel::getSupportedServiceNames()
87 css::uno::Sequence
<OUString
> aSupported
= OReferenceValueComponent::getSupportedServiceNames();
89 sal_Int32 nOldLen
= aSupported
.getLength();
90 aSupported
.realloc( nOldLen
+ 9 );
91 OUString
* pStoreTo
= aSupported
.getArray() + nOldLen
;
93 *pStoreTo
++ = BINDABLE_CONTROL_MODEL
;
94 *pStoreTo
++ = DATA_AWARE_CONTROL_MODEL
;
95 *pStoreTo
++ = VALIDATABLE_CONTROL_MODEL
;
97 *pStoreTo
++ = BINDABLE_DATA_AWARE_CONTROL_MODEL
;
98 *pStoreTo
++ = VALIDATABLE_BINDABLE_CONTROL_MODEL
;
100 *pStoreTo
++ = FRM_SUN_COMPONENT_CHECKBOX
;
101 *pStoreTo
++ = FRM_SUN_COMPONENT_DATABASE_CHECKBOX
;
102 *pStoreTo
++ = BINDABLE_DATABASE_CHECK_BOX
;
104 *pStoreTo
++ = FRM_COMPONENT_CHECKBOX
;
110 void OCheckBoxModel::describeFixedProperties( Sequence
< Property
>& _rProps
) const
112 BEGIN_DESCRIBE_PROPERTIES( 1, OReferenceValueComponent
)
113 DECL_PROP1(TABINDEX
, sal_Int16
, BOUND
);
114 END_DESCRIBE_PROPERTIES();
118 OUString SAL_CALL
OCheckBoxModel::getServiceName()
120 return FRM_COMPONENT_CHECKBOX
; // old (non-sun) name for compatibility !
124 void SAL_CALL
OCheckBoxModel::write(const Reference
<css::io::XObjectOutputStream
>& _rxOutStream
)
126 OReferenceValueComponent::write(_rxOutStream
);
129 _rxOutStream
->writeShort(0x0003);
131 _rxOutStream
<< getReferenceValue();
132 _rxOutStream
<< static_cast<sal_Int16
>(getDefaultChecked());
133 writeHelpTextCompatibly(_rxOutStream
);
134 // from version 0x0003 : common properties
135 writeCommonProperties(_rxOutStream
);
139 void SAL_CALL
OCheckBoxModel::read(const Reference
<css::io::XObjectInputStream
>& _rxInStream
)
141 OReferenceValueComponent::read(_rxInStream
);
142 osl::MutexGuard
aGuard(m_aMutex
);
145 sal_uInt16 nVersion
= _rxInStream
->readShort();
147 OUString sReferenceValue
;
148 sal_Int16
nDefaultChecked( 0 );
152 _rxInStream
>> sReferenceValue
;
153 nDefaultChecked
= _rxInStream
->readShort();
156 _rxInStream
>> sReferenceValue
;
157 _rxInStream
>> nDefaultChecked
;
158 readHelpTextCompatibly( _rxInStream
);
161 _rxInStream
>> sReferenceValue
;
162 _rxInStream
>> nDefaultChecked
;
163 readHelpTextCompatibly(_rxInStream
);
164 readCommonProperties(_rxInStream
);
167 OSL_FAIL("OCheckBoxModel::read : unknown version !");
168 defaultCommonProperties();
171 setReferenceValue( sReferenceValue
);
172 setDefaultChecked( static_cast< ToggleState
>( nDefaultChecked
) );
174 // After reading in, display the default values
175 if ( !getControlSource().isEmpty() )
176 // (not if we don't have a control source - the "State" property acts like it is persistent, then
180 bool OCheckBoxModel::DbUseBool()
182 return getReferenceValue().isEmpty() && getNoCheckReferenceValue().isEmpty();
186 Any
OCheckBoxModel::translateDbColumnToControlValue()
191 // Set value in ControlModel
192 bool bValue
= bool(); // avoid warning
195 bValue
= m_xColumn
->getBoolean();
199 const OUString
sVal(m_xColumn
->getString());
200 if (sVal
== getReferenceValue())
202 else if (sVal
== getNoCheckReferenceValue())
205 aValue
<<= static_cast<sal_Int16
>(getDefaultChecked());
207 if ( m_xColumn
->wasNull() )
209 bool bTriState
= true;
210 if ( m_xAggregateSet
.is() )
211 m_xAggregateSet
->getPropertyValue( PROPERTY_TRISTATE
) >>= bTriState
;
212 aValue
<<= static_cast<sal_Int16
>( bTriState
? TRISTATE_INDET
: getDefaultChecked() );
214 else if ( !aValue
.hasValue() )
216 // Since above either bValue is initialised, either aValue.hasValue(),
217 // bValue cannot be used uninitialised here.
218 // But GCC does not see/understand that, which breaks -Werror builds,
219 // so we explicitly default-initialise it.
220 aValue
<<= static_cast<sal_Int16
>( bValue
? TRISTATE_TRUE
: TRISTATE_FALSE
);
227 bool OCheckBoxModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
229 OSL_PRECOND( m_xColumnUpdate
.is(), "OCheckBoxModel::commitControlValueToDbColumn: not bound!" );
230 if ( m_xColumnUpdate
.is() )
232 Any
aControlValue( m_xAggregateSet
->getPropertyValue( PROPERTY_STATE
) );
235 sal_Int16 nValue
= TRISTATE_INDET
;
236 aControlValue
>>= nValue
;
240 m_xColumnUpdate
->updateNull();
244 m_xColumnUpdate
->updateBoolean( true );
246 m_xColumnUpdate
->updateString( getReferenceValue() );
250 m_xColumnUpdate
->updateBoolean( false );
252 m_xColumnUpdate
->updateString( getNoCheckReferenceValue() );
255 OSL_FAIL("OCheckBoxModel::commitControlValueToDbColumn: invalid value !");
258 catch(const Exception
&)
260 OSL_FAIL("OCheckBoxModel::commitControlValueToDbColumn: could not commit !");
268 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
269 com_sun_star_form_OCheckBoxModel_get_implementation(css::uno::XComponentContext
* component
,
270 css::uno::Sequence
<css::uno::Any
> const &)
272 return cppu::acquire(new frm::OCheckBoxModel(component
));
275 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
276 com_sun_star_form_OCheckBoxControl_get_implementation(css::uno::XComponentContext
* component
,
277 css::uno::Sequence
<css::uno::Any
> const &)
279 return cppu::acquire(new frm::OCheckBoxControl(component
));
282 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */