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 css::uno::Reference
< css::util::XCloneable
> SAL_CALL
ORadioButtonModel::createClone()
88 rtl::Reference
<ORadioButtonModel
> pClone
= new ORadioButtonModel(this, getContext());
89 pClone
->clonedFrom(this);
95 css::uno::Sequence
<OUString
> SAL_CALL
ORadioButtonModel::getSupportedServiceNames()
97 css::uno::Sequence
<OUString
> aSupported
= OReferenceValueComponent::getSupportedServiceNames();
99 sal_Int32 nOldLen
= aSupported
.getLength();
100 aSupported
.realloc( nOldLen
+ 9 );
101 OUString
* pStoreTo
= aSupported
.getArray() + nOldLen
;
103 *pStoreTo
++ = BINDABLE_CONTROL_MODEL
;
104 *pStoreTo
++ = DATA_AWARE_CONTROL_MODEL
;
105 *pStoreTo
++ = VALIDATABLE_CONTROL_MODEL
;
107 *pStoreTo
++ = BINDABLE_DATA_AWARE_CONTROL_MODEL
;
108 *pStoreTo
++ = VALIDATABLE_BINDABLE_CONTROL_MODEL
;
110 *pStoreTo
++ = FRM_SUN_COMPONENT_RADIOBUTTON
;
111 *pStoreTo
++ = FRM_SUN_COMPONENT_DATABASE_RADIOBUTTON
;
112 *pStoreTo
++ = BINDABLE_DATABASE_RADIO_BUTTON
;
114 *pStoreTo
++ = FRM_COMPONENT_RADIOBUTTON
;
120 void ORadioButtonModel::SetSiblingPropsTo(const OUString
& rPropName
, const Any
& rValue
)
124 if (hasProperty(PROPERTY_GROUP_NAME
, this))
125 getPropertyValue(PROPERTY_GROUP_NAME
) >>= sMyGroup
;
126 if (sMyGroup
.isEmpty())
129 // Iterate over my siblings
130 Reference
<XIndexAccess
> xIndexAccess(getParent(), UNO_QUERY
);
131 if (!xIndexAccess
.is())
134 Reference
<XPropertySet
> xMyProps
= this;
135 OUString sCurrentGroup
;
136 sal_Int32 nNumSiblings
= xIndexAccess
->getCount();
137 for (sal_Int32 i
=0; i
<nNumSiblings
; ++i
)
139 Reference
<XPropertySet
> xSiblingProperties(xIndexAccess
->getByIndex(i
), UNO_QUERY
);
140 if (!xSiblingProperties
.is())
142 if (xMyProps
== xSiblingProperties
)
143 continue; // do not set myself
145 // Only if it's a RadioButton
146 if (!hasProperty(PROPERTY_CLASSID
, xSiblingProperties
))
149 xSiblingProperties
->getPropertyValue(PROPERTY_CLASSID
) >>= nType
;
150 if (nType
!= FormComponentType::RADIOBUTTON
)
153 // The group association is attached to the name
154 sCurrentGroup
= OGroupManager::GetGroupName( xSiblingProperties
);
155 if (sCurrentGroup
== sMyGroup
)
156 xSiblingProperties
->setPropertyValue(rPropName
, rValue
);
161 void ORadioButtonModel::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle
, const Any
& rValue
)
163 OReferenceValueComponent::setFastPropertyValue_NoBroadcast( nHandle
, rValue
);
165 // if the label control changed ...
166 if (nHandle
== PROPERTY_ID_CONTROLLABEL
)
167 { // ... forward this to our siblings
168 SetSiblingPropsTo(PROPERTY_CONTROLLABEL
, rValue
);
171 // If the ControlSource property has changed ...
172 if (nHandle
== PROPERTY_ID_CONTROLSOURCE
)
173 { // ... I have to pass the new ControlSource to my siblings, which are in the same RadioButton group as I am
174 SetSiblingPropsTo(PROPERTY_CONTROLSOURCE
, rValue
);
177 // The other way: if my name changes ...
178 if (nHandle
== PROPERTY_ID_NAME
)
183 if (nHandle
!= PROPERTY_ID_DEFAULT_STATE
)
189 { // Reset the 'default checked' for all Radios of the same group.
190 // Because (as the Highlander already knew): "There can be only one"
194 SetSiblingPropsTo(PROPERTY_DEFAULT_STATE
, aZero
);
198 void ORadioButtonModel::setControlSource()
200 Reference
<XIndexAccess
> xIndexAccess(getParent(), UNO_QUERY
);
201 if (!xIndexAccess
.is())
204 OUString sName
, sGroupName
;
206 if (hasProperty(PROPERTY_GROUP_NAME
, this))
207 getPropertyValue(PROPERTY_GROUP_NAME
) >>= sGroupName
;
208 getPropertyValue(PROPERTY_NAME
) >>= sName
;
210 Reference
<XPropertySet
> xMyProps
= this;
211 for (sal_Int32 i
=0; i
<xIndexAccess
->getCount(); ++i
)
213 Reference
<XPropertySet
> xSiblingProperties(xIndexAccess
->getByIndex(i
), UNO_QUERY
);
214 if (!xSiblingProperties
.is())
217 if (xMyProps
== xSiblingProperties
)
218 // Only if I didn't find myself
222 xSiblingProperties
->getPropertyValue(PROPERTY_CLASSID
) >>= nType
;
223 if (nType
!= FormComponentType::RADIOBUTTON
)
227 OUString sSiblingName
, sSiblingGroupName
;
228 if (hasProperty(PROPERTY_GROUP_NAME
, xSiblingProperties
))
229 xSiblingProperties
->getPropertyValue(PROPERTY_GROUP_NAME
) >>= sSiblingGroupName
;
230 xSiblingProperties
->getPropertyValue(PROPERTY_NAME
) >>= sSiblingName
;
232 if ((sGroupName
.isEmpty() && sSiblingGroupName
.isEmpty() && // (no group name
233 sName
== sSiblingName
) || // names match) or
234 (!sGroupName
.isEmpty() && !sSiblingGroupName
.isEmpty() && // (have group name
235 sGroupName
== sSiblingGroupName
)) // they match)
237 setPropertyValue(PROPERTY_CONTROLSOURCE
, xSiblingProperties
->getPropertyValue(PROPERTY_CONTROLSOURCE
));
244 void ORadioButtonModel::describeFixedProperties( Sequence
< Property
>& _rProps
) const
246 OReferenceValueComponent::describeFixedProperties( _rProps
);
247 sal_Int32 nOldCount
= _rProps
.getLength();
248 _rProps
.realloc( nOldCount
+ 1);
249 css::beans::Property
* pProperties
= _rProps
.getArray() + nOldCount
;
250 *pProperties
++ = css::beans::Property(PROPERTY_TABINDEX
, PROPERTY_ID_TABINDEX
, cppu::UnoType
<sal_Int16
>::get(), css::beans::PropertyAttribute::BOUND
);
251 DBG_ASSERT( pProperties
== _rProps
.getArray() + _rProps
.getLength(), "<...>::describeFixedProperties/getInfoHelper: forgot to adjust the count ?");
255 OUString SAL_CALL
ORadioButtonModel::getServiceName()
257 return FRM_COMPONENT_RADIOBUTTON
; // old (non-sun) name for compatibility !
261 void SAL_CALL
ORadioButtonModel::write(const Reference
<XObjectOutputStream
>& _rxOutStream
)
263 OReferenceValueComponent::write(_rxOutStream
);
266 _rxOutStream
->writeShort(0x0003);
269 _rxOutStream
<< getReferenceValue();
270 _rxOutStream
<< static_cast<sal_Int16
>(getDefaultChecked());
271 writeHelpTextCompatibly(_rxOutStream
);
273 // from version 0x0003 : common properties
274 writeCommonProperties(_rxOutStream
);
278 void SAL_CALL
ORadioButtonModel::read(const Reference
<XObjectInputStream
>& _rxInStream
)
280 OReferenceValueComponent::read(_rxInStream
);
281 ::osl::MutexGuard
aGuard(m_aMutex
);
284 sal_uInt16 nVersion
= _rxInStream
->readShort();
286 OUString sReferenceValue
;
287 sal_Int16
nDefaultChecked( 0 );
291 _rxInStream
>> sReferenceValue
;
292 _rxInStream
>> nDefaultChecked
;
295 _rxInStream
>> sReferenceValue
;
296 _rxInStream
>> nDefaultChecked
;
297 readHelpTextCompatibly(_rxInStream
);
300 _rxInStream
>> sReferenceValue
;
301 _rxInStream
>> nDefaultChecked
;
302 readHelpTextCompatibly(_rxInStream
);
303 readCommonProperties(_rxInStream
);
306 OSL_FAIL("ORadioButtonModel::read : unknown version !");
307 defaultCommonProperties();
311 setReferenceValue( sReferenceValue
);
312 setDefaultChecked( static_cast<ToggleState
>(nDefaultChecked
) );
314 // Display default values after read
315 if ( !getControlSource().isEmpty() )
316 // (not if we don't have a control source - the "State" property acts like it is persistent, then
321 void ORadioButtonModel::_propertyChanged(const PropertyChangeEvent
& _rEvent
)
323 if ( _rEvent
.PropertyName
== PROPERTY_STATE
)
325 if ( _rEvent
.NewValue
== sal_Int16(1) )
327 // If my status has changed to 'checked', I have to reset all my siblings, which are in the same group as I am
329 aZero
<<= sal_Int16(0);
330 SetSiblingPropsTo( PROPERTY_STATE
, aZero
);
333 else if ( _rEvent
.PropertyName
== PROPERTY_GROUP_NAME
)
336 // Can't call OReferenceValueComponent::_propertyChanged(), as it
337 // doesn't know what to do with the GroupName property.
341 OReferenceValueComponent::_propertyChanged( _rEvent
);
345 Any
ORadioButtonModel::translateDbColumnToControlValue()
347 return makeAny( static_cast<sal_Int16
>( ( m_xColumn
->getString() == getReferenceValue() ) ? TRISTATE_TRUE
: TRISTATE_FALSE
)
352 Any
ORadioButtonModel::translateExternalValueToControlValue( const Any
& _rExternalValue
) const
354 Any aControlValue
= OReferenceValueComponent::translateExternalValueToControlValue( _rExternalValue
);
355 sal_Int16 nState
= TRISTATE_FALSE
;
356 if ( ( aControlValue
>>= nState
) && ( nState
== TRISTATE_INDET
) )
357 // radio buttons do not have the DONTKNOW state
358 aControlValue
<<= sal_Int16(TRISTATE_FALSE
);
359 return aControlValue
;
363 bool ORadioButtonModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
365 Reference
< XPropertySet
> xField( getField() );
366 OSL_PRECOND( xField
.is(), "ORadioButtonModel::commitControlValueToDbColumn: not bound!" );
371 sal_Int16 nValue
= 0;
372 m_xAggregateSet
->getPropertyValue( PROPERTY_STATE
) >>= nValue
;
374 xField
->setPropertyValue( PROPERTY_VALUE
, makeAny( getReferenceValue() ) );
376 catch(const Exception
&)
378 OSL_FAIL("ORadioButtonModel::commitControlValueToDbColumn: could not commit !");
386 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
387 com_sun_star_form_ORadioButtonModel_get_implementation(css::uno::XComponentContext
* component
,
388 css::uno::Sequence
<css::uno::Any
> const &)
390 return cppu::acquire(new frm::ORadioButtonModel(component
));
393 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
394 com_sun_star_form_ORadioButtonControl_get_implementation(css::uno::XComponentContext
* component
,
395 css::uno::Sequence
<css::uno::Any
> const &)
397 return cppu::acquire(new frm::ORadioButtonControl(component
));
400 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */