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 "RadioButton.hxx"
21 #include "GroupManager.hxx"
22 #include <property.hxx>
23 #include <services.hxx>
24 #include <comphelper/basicio.hxx>
25 #include <comphelper/property.hxx>
26 #include <com/sun/star/container/XIndexAccess.hpp>
27 #include <com/sun/star/form/FormComponentType.hpp>
31 using namespace ::com::sun::star::uno
;
32 using namespace ::com::sun::star::sdb
;
33 using namespace ::com::sun::star::sdbc
;
34 using namespace ::com::sun::star::beans
;
35 using namespace ::com::sun::star::container
;
36 using namespace ::com::sun::star::form
;
37 using namespace ::com::sun::star::awt
;
38 using namespace ::com::sun::star::io
;
39 using namespace ::com::sun::star::lang
;
40 using namespace ::com::sun::star::util
;
41 using namespace ::com::sun::star::form::binding
;
44 css::uno::Sequence
<OUString
> SAL_CALL
ORadioButtonControl::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_RADIOBUTTON
;
51 pArray
[aSupported
.getLength()-1] = STARDIV_ONE_FORM_CONTROL_RADIOBUTTON
;
56 ORadioButtonControl::ORadioButtonControl(const Reference
<XComponentContext
>& _rxFactory
)
57 :OBoundControl(_rxFactory
, VCL_CONTROL_RADIOBUTTON
)
62 ORadioButtonModel::ORadioButtonModel(const Reference
<XComponentContext
>& _rxFactory
)
63 :OReferenceValueComponent( _rxFactory
, VCL_CONTROLMODEL_RADIOBUTTON
, FRM_SUN_CONTROL_RADIOBUTTON
)
64 // use the old control name for compytibility reasons
67 m_nClassId
= FormComponentType::RADIOBUTTON
;
68 m_aLabelServiceName
= FRM_SUN_COMPONENT_GROUPBOX
;
69 initValueProperty( PROPERTY_STATE
, PROPERTY_ID_STATE
);
70 startAggregatePropertyListening( PROPERTY_GROUP_NAME
);
74 ORadioButtonModel::ORadioButtonModel( const ORadioButtonModel
* _pOriginal
, const Reference
<XComponentContext
>& _rxFactory
)
75 :OReferenceValueComponent( _pOriginal
, _rxFactory
)
80 ORadioButtonModel::~ORadioButtonModel()
86 IMPLEMENT_DEFAULT_CLONING( ORadioButtonModel
)
90 css::uno::Sequence
<OUString
> SAL_CALL
ORadioButtonModel::getSupportedServiceNames()
92 css::uno::Sequence
<OUString
> aSupported
= OReferenceValueComponent::getSupportedServiceNames();
94 sal_Int32 nOldLen
= aSupported
.getLength();
95 aSupported
.realloc( nOldLen
+ 9 );
96 OUString
* pStoreTo
= aSupported
.getArray() + nOldLen
;
98 *pStoreTo
++ = BINDABLE_CONTROL_MODEL
;
99 *pStoreTo
++ = DATA_AWARE_CONTROL_MODEL
;
100 *pStoreTo
++ = VALIDATABLE_CONTROL_MODEL
;
102 *pStoreTo
++ = BINDABLE_DATA_AWARE_CONTROL_MODEL
;
103 *pStoreTo
++ = VALIDATABLE_BINDABLE_CONTROL_MODEL
;
105 *pStoreTo
++ = FRM_SUN_COMPONENT_RADIOBUTTON
;
106 *pStoreTo
++ = FRM_SUN_COMPONENT_DATABASE_RADIOBUTTON
;
107 *pStoreTo
++ = BINDABLE_DATABASE_RADIO_BUTTON
;
109 *pStoreTo
++ = FRM_COMPONENT_RADIOBUTTON
;
115 void ORadioButtonModel::SetSiblingPropsTo(const OUString
& rPropName
, const Any
& rValue
)
119 if (hasProperty(PROPERTY_GROUP_NAME
, this))
120 getPropertyValue(PROPERTY_GROUP_NAME
) >>= sMyGroup
;
121 if (sMyGroup
.isEmpty())
124 // Iterate over my siblings
125 Reference
<XIndexAccess
> xIndexAccess(getParent(), UNO_QUERY
);
126 if (!xIndexAccess
.is())
129 Reference
<XPropertySet
> xMyProps
= this;
130 OUString sCurrentGroup
;
131 sal_Int32 nNumSiblings
= xIndexAccess
->getCount();
132 for (sal_Int32 i
=0; i
<nNumSiblings
; ++i
)
134 Reference
<XPropertySet
> xSiblingProperties(xIndexAccess
->getByIndex(i
), UNO_QUERY
);
135 if (!xSiblingProperties
.is())
137 if (xMyProps
== xSiblingProperties
)
138 continue; // do not set myself
140 // Only if it's a RadioButton
141 if (!hasProperty(PROPERTY_CLASSID
, xSiblingProperties
))
144 xSiblingProperties
->getPropertyValue(PROPERTY_CLASSID
) >>= nType
;
145 if (nType
!= FormComponentType::RADIOBUTTON
)
148 // The group association is attached to the name
149 sCurrentGroup
= OGroupManager::GetGroupName( xSiblingProperties
);
150 if (sCurrentGroup
== sMyGroup
)
151 xSiblingProperties
->setPropertyValue(rPropName
, rValue
);
156 void ORadioButtonModel::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle
, const Any
& rValue
)
158 OReferenceValueComponent::setFastPropertyValue_NoBroadcast( nHandle
, rValue
);
160 // if the label control changed ...
161 if (nHandle
== PROPERTY_ID_CONTROLLABEL
)
162 { // ... forward this to our siblings
163 SetSiblingPropsTo(PROPERTY_CONTROLLABEL
, rValue
);
166 // If the ControlSource property has changed ...
167 if (nHandle
== PROPERTY_ID_CONTROLSOURCE
)
168 { // ... I have to pass the new ControlSource to my siblings, which are in the same RadioButton group as I am
169 SetSiblingPropsTo(PROPERTY_CONTROLSOURCE
, rValue
);
172 // The other way: if my name changes ...
173 if (nHandle
== PROPERTY_ID_NAME
)
178 if (nHandle
!= PROPERTY_ID_DEFAULT_STATE
)
184 { // Reset the 'default checked' for all Radios of the same group.
185 // Because (as the Highlander already knew): "There can be only one"
189 SetSiblingPropsTo(PROPERTY_DEFAULT_STATE
, aZero
);
193 void ORadioButtonModel::setControlSource()
195 Reference
<XIndexAccess
> xIndexAccess(getParent(), UNO_QUERY
);
196 if (!xIndexAccess
.is())
199 OUString sName
, sGroupName
;
201 if (hasProperty(PROPERTY_GROUP_NAME
, this))
202 getPropertyValue(PROPERTY_GROUP_NAME
) >>= sGroupName
;
203 getPropertyValue(PROPERTY_NAME
) >>= sName
;
205 Reference
<XPropertySet
> xMyProps
= this;
206 for (sal_Int32 i
=0; i
<xIndexAccess
->getCount(); ++i
)
208 Reference
<XPropertySet
> xSiblingProperties(xIndexAccess
->getByIndex(i
), UNO_QUERY
);
209 if (!xSiblingProperties
.is())
212 if (xMyProps
== xSiblingProperties
)
213 // Only if I didn't find myself
217 xSiblingProperties
->getPropertyValue(PROPERTY_CLASSID
) >>= nType
;
218 if (nType
!= FormComponentType::RADIOBUTTON
)
222 OUString sSiblingName
, sSiblingGroupName
;
223 if (hasProperty(PROPERTY_GROUP_NAME
, xSiblingProperties
))
224 xSiblingProperties
->getPropertyValue(PROPERTY_GROUP_NAME
) >>= sSiblingGroupName
;
225 xSiblingProperties
->getPropertyValue(PROPERTY_NAME
) >>= sSiblingName
;
227 if ((sGroupName
.isEmpty() && sSiblingGroupName
.isEmpty() && // (no group name
228 sName
== sSiblingName
) || // names match) or
229 (!sGroupName
.isEmpty() && !sSiblingGroupName
.isEmpty() && // (have group name
230 sGroupName
== sSiblingGroupName
)) // they match)
232 setPropertyValue(PROPERTY_CONTROLSOURCE
, xSiblingProperties
->getPropertyValue(PROPERTY_CONTROLSOURCE
));
239 void ORadioButtonModel::describeFixedProperties( Sequence
< Property
>& _rProps
) const
241 BEGIN_DESCRIBE_PROPERTIES( 1, OReferenceValueComponent
)
242 DECL_PROP1(TABINDEX
, sal_Int16
, BOUND
);
243 END_DESCRIBE_PROPERTIES();
247 OUString SAL_CALL
ORadioButtonModel::getServiceName()
249 return FRM_COMPONENT_RADIOBUTTON
; // old (non-sun) name for compatibility !
253 void SAL_CALL
ORadioButtonModel::write(const Reference
<XObjectOutputStream
>& _rxOutStream
)
255 OReferenceValueComponent::write(_rxOutStream
);
258 _rxOutStream
->writeShort(0x0003);
261 _rxOutStream
<< getReferenceValue();
262 _rxOutStream
<< static_cast<sal_Int16
>(getDefaultChecked());
263 writeHelpTextCompatibly(_rxOutStream
);
265 // from version 0x0003 : common properties
266 writeCommonProperties(_rxOutStream
);
270 void SAL_CALL
ORadioButtonModel::read(const Reference
<XObjectInputStream
>& _rxInStream
)
272 OReferenceValueComponent::read(_rxInStream
);
273 ::osl::MutexGuard
aGuard(m_aMutex
);
276 sal_uInt16 nVersion
= _rxInStream
->readShort();
278 OUString sReferenceValue
;
279 sal_Int16
nDefaultChecked( 0 );
283 _rxInStream
>> sReferenceValue
;
284 _rxInStream
>> nDefaultChecked
;
287 _rxInStream
>> sReferenceValue
;
288 _rxInStream
>> nDefaultChecked
;
289 readHelpTextCompatibly(_rxInStream
);
292 _rxInStream
>> sReferenceValue
;
293 _rxInStream
>> nDefaultChecked
;
294 readHelpTextCompatibly(_rxInStream
);
295 readCommonProperties(_rxInStream
);
298 OSL_FAIL("ORadioButtonModel::read : unknown version !");
299 defaultCommonProperties();
303 setReferenceValue( sReferenceValue
);
304 setDefaultChecked( static_cast<ToggleState
>(nDefaultChecked
) );
306 // Display default values after read
307 if ( !getControlSource().isEmpty() )
308 // (not if we don't have a control source - the "State" property acts like it is persistent, then
313 void ORadioButtonModel::_propertyChanged(const PropertyChangeEvent
& _rEvent
)
315 if ( _rEvent
.PropertyName
== PROPERTY_STATE
)
317 if ( _rEvent
.NewValue
== sal_Int16(1) )
319 // If my status has changed to 'checked', I have to reset all my siblings, which are in the same group as I am
321 aZero
<<= sal_Int16(0);
322 SetSiblingPropsTo( PROPERTY_STATE
, aZero
);
325 else if ( _rEvent
.PropertyName
== PROPERTY_GROUP_NAME
)
328 // Can't call OReferenceValueComponent::_propertyChanged(), as it
329 // doesn't know what to do with the GroupName property.
333 OReferenceValueComponent::_propertyChanged( _rEvent
);
337 Any
ORadioButtonModel::translateDbColumnToControlValue()
339 return makeAny( static_cast<sal_Int16
>( ( m_xColumn
->getString() == getReferenceValue() ) ? TRISTATE_TRUE
: TRISTATE_FALSE
)
344 Any
ORadioButtonModel::translateExternalValueToControlValue( const Any
& _rExternalValue
) const
346 Any aControlValue
= OReferenceValueComponent::translateExternalValueToControlValue( _rExternalValue
);
347 sal_Int16 nState
= TRISTATE_FALSE
;
348 if ( ( aControlValue
>>= nState
) && ( nState
== TRISTATE_INDET
) )
349 // radio buttons do not have the DONTKNOW state
350 aControlValue
<<= sal_Int16(TRISTATE_FALSE
);
351 return aControlValue
;
355 bool ORadioButtonModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
357 Reference
< XPropertySet
> xField( getField() );
358 OSL_PRECOND( xField
.is(), "ORadioButtonModel::commitControlValueToDbColumn: not bound!" );
363 sal_Int16 nValue
= 0;
364 m_xAggregateSet
->getPropertyValue( PROPERTY_STATE
) >>= nValue
;
366 xField
->setPropertyValue( PROPERTY_VALUE
, makeAny( getReferenceValue() ) );
368 catch(const Exception
&)
370 OSL_FAIL("ORadioButtonModel::commitControlValueToDbColumn: could not commit !");
378 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
379 com_sun_star_form_ORadioButtonModel_get_implementation(css::uno::XComponentContext
* component
,
380 css::uno::Sequence
<css::uno::Any
> const &)
382 return cppu::acquire(new frm::ORadioButtonModel(component
));
385 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
386 com_sun_star_form_ORadioButtonControl_get_implementation(css::uno::XComponentContext
* component
,
387 css::uno::Sequence
<css::uno::Any
> const &)
389 return cppu::acquire(new frm::ORadioButtonControl(component
));
392 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */