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 <tools/debug.hxx>
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
28 #include <com/sun/star/container/XIndexAccess.hpp>
29 #include <com/sun/star/form/FormComponentType.hpp>
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::sdb
;
35 using namespace ::com::sun::star::sdbc
;
36 using namespace ::com::sun::star::beans
;
37 using namespace ::com::sun::star::container
;
38 using namespace ::com::sun::star::form
;
39 using namespace ::com::sun::star::io
;
40 using namespace ::com::sun::star::util
;
43 css::uno::Sequence
<OUString
> SAL_CALL
ORadioButtonControl::getSupportedServiceNames()
45 css::uno::Sequence
<OUString
> aSupported
= OBoundControl::getSupportedServiceNames();
46 aSupported
.realloc(aSupported
.getLength() + 2);
48 OUString
* pArray
= aSupported
.getArray();
49 pArray
[aSupported
.getLength()-2] = FRM_SUN_CONTROL_RADIOBUTTON
;
50 pArray
[aSupported
.getLength()-1] = STARDIV_ONE_FORM_CONTROL_RADIOBUTTON
;
55 ORadioButtonControl::ORadioButtonControl(const Reference
<XComponentContext
>& _rxFactory
)
56 :OBoundControl(_rxFactory
, VCL_CONTROL_RADIOBUTTON
)
61 ORadioButtonModel::ORadioButtonModel(const Reference
<XComponentContext
>& _rxFactory
)
62 :OReferenceValueComponent( _rxFactory
, VCL_CONTROLMODEL_RADIOBUTTON
, FRM_SUN_CONTROL_RADIOBUTTON
)
63 // use the old control name for compatibility reasons
66 m_nClassId
= FormComponentType::RADIOBUTTON
;
67 m_aLabelServiceName
= FRM_SUN_COMPONENT_GROUPBOX
;
68 initValueProperty( PROPERTY_STATE
, PROPERTY_ID_STATE
);
69 startAggregatePropertyListening( PROPERTY_GROUP_NAME
);
73 ORadioButtonModel::ORadioButtonModel( const ORadioButtonModel
* _pOriginal
, const Reference
<XComponentContext
>& _rxFactory
)
74 :OReferenceValueComponent( _pOriginal
, _rxFactory
)
79 ORadioButtonModel::~ORadioButtonModel()
85 css::uno::Reference
< css::util::XCloneable
> SAL_CALL
ORadioButtonModel::createClone()
87 rtl::Reference
<ORadioButtonModel
> pClone
= new ORadioButtonModel(this, getContext());
88 pClone
->clonedFrom(this);
94 css::uno::Sequence
<OUString
> SAL_CALL
ORadioButtonModel::getSupportedServiceNames()
96 css::uno::Sequence
<OUString
> aSupported
= OReferenceValueComponent::getSupportedServiceNames();
98 sal_Int32 nOldLen
= aSupported
.getLength();
99 aSupported
.realloc( nOldLen
+ 9 );
100 OUString
* pStoreTo
= aSupported
.getArray() + nOldLen
;
102 *pStoreTo
++ = BINDABLE_CONTROL_MODEL
;
103 *pStoreTo
++ = DATA_AWARE_CONTROL_MODEL
;
104 *pStoreTo
++ = VALIDATABLE_CONTROL_MODEL
;
106 *pStoreTo
++ = BINDABLE_DATA_AWARE_CONTROL_MODEL
;
107 *pStoreTo
++ = VALIDATABLE_BINDABLE_CONTROL_MODEL
;
109 *pStoreTo
++ = FRM_SUN_COMPONENT_RADIOBUTTON
;
110 *pStoreTo
++ = FRM_SUN_COMPONENT_DATABASE_RADIOBUTTON
;
111 *pStoreTo
++ = BINDABLE_DATABASE_RADIO_BUTTON
;
113 *pStoreTo
++ = FRM_COMPONENT_RADIOBUTTON
;
119 void ORadioButtonModel::SetSiblingPropsTo(const OUString
& rPropName
, const Any
& rValue
)
123 if (hasProperty(PROPERTY_GROUP_NAME
, this))
124 getPropertyValue(PROPERTY_GROUP_NAME
) >>= sMyGroup
;
125 if (sMyGroup
.isEmpty())
128 // Iterate over my siblings
129 Reference
<XIndexAccess
> xIndexAccess(getParent(), UNO_QUERY
);
130 if (!xIndexAccess
.is())
133 Reference
<XPropertySet
> xMyProps
= this;
134 OUString sCurrentGroup
;
135 sal_Int32 nNumSiblings
= xIndexAccess
->getCount();
136 for (sal_Int32 i
=0; i
<nNumSiblings
; ++i
)
138 Reference
<XPropertySet
> xSiblingProperties(xIndexAccess
->getByIndex(i
), UNO_QUERY
);
139 if (!xSiblingProperties
.is())
141 if (xMyProps
== xSiblingProperties
)
142 continue; // do not set myself
144 // Only if it's a RadioButton
145 if (!hasProperty(PROPERTY_CLASSID
, xSiblingProperties
))
148 xSiblingProperties
->getPropertyValue(PROPERTY_CLASSID
) >>= nType
;
149 if (nType
!= FormComponentType::RADIOBUTTON
)
152 // The group association is attached to the name
153 sCurrentGroup
= OGroupManager::GetGroupName( xSiblingProperties
);
154 if (sCurrentGroup
== sMyGroup
)
155 xSiblingProperties
->setPropertyValue(rPropName
, rValue
);
160 void ORadioButtonModel::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle
, const Any
& rValue
)
162 OReferenceValueComponent::setFastPropertyValue_NoBroadcast( nHandle
, rValue
);
164 // if the label control changed ...
165 if (nHandle
== PROPERTY_ID_CONTROLLABEL
)
166 { // ... forward this to our siblings
167 SetSiblingPropsTo(PROPERTY_CONTROLLABEL
, rValue
);
170 // If the ControlSource property has changed ...
171 if (nHandle
== PROPERTY_ID_CONTROLSOURCE
)
172 { // ... I have to pass the new ControlSource to my siblings, which are in the same RadioButton group as I am
173 SetSiblingPropsTo(PROPERTY_CONTROLSOURCE
, rValue
);
176 // The other way: if my name changes ...
177 if (nHandle
== PROPERTY_ID_NAME
)
182 if (nHandle
!= PROPERTY_ID_DEFAULT_STATE
)
188 { // Reset the 'default checked' for all Radios of the same group.
189 // Because (as the Highlander already knew): "There can be only one"
193 SetSiblingPropsTo(PROPERTY_DEFAULT_STATE
, aZero
);
197 void ORadioButtonModel::setControlSource()
199 Reference
<XIndexAccess
> xIndexAccess(getParent(), UNO_QUERY
);
200 if (!xIndexAccess
.is())
203 OUString sName
, sGroupName
;
205 if (hasProperty(PROPERTY_GROUP_NAME
, this))
206 getPropertyValue(PROPERTY_GROUP_NAME
) >>= sGroupName
;
207 getPropertyValue(PROPERTY_NAME
) >>= sName
;
209 Reference
<XPropertySet
> xMyProps
= this;
210 for (sal_Int32 i
=0; i
<xIndexAccess
->getCount(); ++i
)
212 Reference
<XPropertySet
> xSiblingProperties(xIndexAccess
->getByIndex(i
), UNO_QUERY
);
213 if (!xSiblingProperties
.is())
216 if (xMyProps
== xSiblingProperties
)
217 // Only if I didn't find myself
221 xSiblingProperties
->getPropertyValue(PROPERTY_CLASSID
) >>= nType
;
222 if (nType
!= FormComponentType::RADIOBUTTON
)
226 OUString sSiblingName
, sSiblingGroupName
;
227 if (hasProperty(PROPERTY_GROUP_NAME
, xSiblingProperties
))
228 xSiblingProperties
->getPropertyValue(PROPERTY_GROUP_NAME
) >>= sSiblingGroupName
;
229 xSiblingProperties
->getPropertyValue(PROPERTY_NAME
) >>= sSiblingName
;
231 if ((sGroupName
.isEmpty() && sSiblingGroupName
.isEmpty() && // (no group name
232 sName
== sSiblingName
) || // names match) or
233 (!sGroupName
.isEmpty() && !sSiblingGroupName
.isEmpty() && // (have group name
234 sGroupName
== sSiblingGroupName
)) // they match)
236 setPropertyValue(PROPERTY_CONTROLSOURCE
, xSiblingProperties
->getPropertyValue(PROPERTY_CONTROLSOURCE
));
243 void ORadioButtonModel::describeFixedProperties( Sequence
< Property
>& _rProps
) const
245 OReferenceValueComponent::describeFixedProperties( _rProps
);
246 sal_Int32 nOldCount
= _rProps
.getLength();
247 _rProps
.realloc( nOldCount
+ 1);
248 css::beans::Property
* pProperties
= _rProps
.getArray() + nOldCount
;
249 *pProperties
++ = css::beans::Property(PROPERTY_TABINDEX
, PROPERTY_ID_TABINDEX
, cppu::UnoType
<sal_Int16
>::get(), css::beans::PropertyAttribute::BOUND
);
250 DBG_ASSERT( pProperties
== _rProps
.getArray() + _rProps
.getLength(), "<...>::describeFixedProperties/getInfoHelper: forgot to adjust the count ?");
254 OUString SAL_CALL
ORadioButtonModel::getServiceName()
256 return FRM_COMPONENT_RADIOBUTTON
; // old (non-sun) name for compatibility !
260 void SAL_CALL
ORadioButtonModel::write(const Reference
<XObjectOutputStream
>& _rxOutStream
)
262 OReferenceValueComponent::write(_rxOutStream
);
265 _rxOutStream
->writeShort(0x0003);
268 _rxOutStream
<< getReferenceValue();
269 _rxOutStream
<< static_cast<sal_Int16
>(getDefaultChecked());
270 writeHelpTextCompatibly(_rxOutStream
);
272 // from version 0x0003 : common properties
273 writeCommonProperties(_rxOutStream
);
277 void SAL_CALL
ORadioButtonModel::read(const Reference
<XObjectInputStream
>& _rxInStream
)
279 OReferenceValueComponent::read(_rxInStream
);
280 ::osl::MutexGuard
aGuard(m_aMutex
);
283 sal_uInt16 nVersion
= _rxInStream
->readShort();
285 OUString sReferenceValue
;
286 sal_Int16
nDefaultChecked( 0 );
290 _rxInStream
>> sReferenceValue
;
291 _rxInStream
>> nDefaultChecked
;
294 _rxInStream
>> sReferenceValue
;
295 _rxInStream
>> nDefaultChecked
;
296 readHelpTextCompatibly(_rxInStream
);
299 _rxInStream
>> sReferenceValue
;
300 _rxInStream
>> nDefaultChecked
;
301 readHelpTextCompatibly(_rxInStream
);
302 readCommonProperties(_rxInStream
);
305 OSL_FAIL("ORadioButtonModel::read : unknown version !");
306 defaultCommonProperties();
310 setReferenceValue( sReferenceValue
);
311 setDefaultChecked( static_cast<ToggleState
>(nDefaultChecked
) );
313 // Display default values after read
314 if ( !getControlSource().isEmpty() )
315 // (not if we don't have a control source - the "State" property acts like it is persistent, then
320 void ORadioButtonModel::_propertyChanged(const PropertyChangeEvent
& _rEvent
)
322 if ( _rEvent
.PropertyName
== PROPERTY_STATE
)
324 if ( _rEvent
.NewValue
== sal_Int16(1) )
326 // If my status has changed to 'checked', I have to reset all my siblings, which are in the same group as I am
328 aZero
<<= sal_Int16(0);
329 SetSiblingPropsTo( PROPERTY_STATE
, aZero
);
332 else if ( _rEvent
.PropertyName
== PROPERTY_GROUP_NAME
)
335 // Can't call OReferenceValueComponent::_propertyChanged(), as it
336 // doesn't know what to do with the GroupName property.
340 OReferenceValueComponent::_propertyChanged( _rEvent
);
344 Any
ORadioButtonModel::translateDbColumnToControlValue()
346 return Any( static_cast<sal_Int16
>( ( m_xColumn
->getString() == getReferenceValue() ) ? TRISTATE_TRUE
: TRISTATE_FALSE
)
351 Any
ORadioButtonModel::translateExternalValueToControlValue( const Any
& _rExternalValue
) const
353 Any aControlValue
= OReferenceValueComponent::translateExternalValueToControlValue( _rExternalValue
);
354 sal_Int16 nState
= TRISTATE_FALSE
;
355 if ( ( aControlValue
>>= nState
) && ( nState
== TRISTATE_INDET
) )
356 // radio buttons do not have the DONTKNOW state
357 aControlValue
<<= sal_Int16(TRISTATE_FALSE
);
358 return aControlValue
;
362 bool ORadioButtonModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
364 Reference
< XPropertySet
> xField( getField() );
365 OSL_PRECOND( xField
.is(), "ORadioButtonModel::commitControlValueToDbColumn: not bound!" );
370 sal_Int16 nValue
= 0;
371 m_xAggregateSet
->getPropertyValue( PROPERTY_STATE
) >>= nValue
;
373 xField
->setPropertyValue( PROPERTY_VALUE
, Any( getReferenceValue() ) );
375 catch(const Exception
&)
377 OSL_FAIL("ORadioButtonModel::commitControlValueToDbColumn: could not commit !");
385 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
386 com_sun_star_form_ORadioButtonModel_get_implementation(css::uno::XComponentContext
* component
,
387 css::uno::Sequence
<css::uno::Any
> const &)
389 return cppu::acquire(new frm::ORadioButtonModel(component
));
392 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
393 com_sun_star_form_ORadioButtonControl_get_implementation(css::uno::XComponentContext
* component
,
394 css::uno::Sequence
<css::uno::Any
> const &)
396 return cppu::acquire(new frm::ORadioButtonControl(component
));
399 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */