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 "property.hrc"
24 #include "services.hxx"
25 #include <comphelper/basicio.hxx>
26 #include <comphelper/processfactory.hxx>
27 #include <com/sun/star/container/XIndexAccess.hpp>
28 #include <com/sun/star/awt/XVclWindowPeer.hpp>
32 using namespace ::com::sun::star::uno
;
33 using namespace ::com::sun::star::sdb
;
34 using namespace ::com::sun::star::sdbc
;
35 using namespace ::com::sun::star::sdbcx
;
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::awt
;
40 using namespace ::com::sun::star::io
;
41 using namespace ::com::sun::star::lang
;
42 using namespace ::com::sun::star::util
;
43 using namespace ::com::sun::star::form::binding
;
46 StringSequence SAL_CALL
ORadioButtonControl::getSupportedServiceNames() throw(RuntimeException
, std::exception
)
48 StringSequence aSupported
= OBoundControl::getSupportedServiceNames();
49 aSupported
.realloc(aSupported
.getLength() + 2);
51 OUString
* pArray
= aSupported
.getArray();
52 pArray
[aSupported
.getLength()-2] = FRM_SUN_CONTROL_RADIOBUTTON
;
53 pArray
[aSupported
.getLength()-1] = STARDIV_ONE_FORM_CONTROL_RADIOBUTTON
;
59 ORadioButtonControl::ORadioButtonControl(const Reference
<XComponentContext
>& _rxFactory
)
60 :OBoundControl(_rxFactory
, VCL_CONTROL_RADIOBUTTON
)
65 void SAL_CALL
ORadioButtonControl::createPeer(const Reference
<css::awt::XToolkit
>& _rxToolkit
, const Reference
<css::awt::XWindowPeer
>& _rxParent
) throw (RuntimeException
, std::exception
)
67 OBoundControl::createPeer(_rxToolkit
, _rxParent
);
69 // switch off the auto-toggle, we do this ourself ....
70 // (formerly this switch-off was done in the toolkit - but the correct place is here ...)
71 // Reference< XVclWindowPeer > xVclWindowPeer( getPeer(), UNO_QUERY );
72 // if (xVclWindowPeer.is())
73 // xVclWindowPeer->setProperty(OUString("AutoToggle"), ::cppu::bool2any(sal_False));
74 // new order: do _not_ switch off the auto toggle because:
75 // * today, it is not necessary anymore to handle the toggling ourself (everything works fine without it)
76 // * without auto toggle, the AccessibleEvents as fired by the radio buttons are
77 // a. newly checked button: "unchecked"->"checked"
78 // b. previously checked button: "checked"->"unchecked"
79 // This is deadly for AT-tools, which then get the "unchecked" event _immediately_ after the "checked" event,
80 // and only read the latter. This makes radio buttons pretty unusable in form documents.
81 // So we switched AutoToggle _on_, again, because then VCL can handle the notifications, and will send
82 // them in the proper order.
86 ORadioButtonModel::ORadioButtonModel(const Reference
<XComponentContext
>& _rxFactory
)
87 :OReferenceValueComponent( _rxFactory
, VCL_CONTROLMODEL_RADIOBUTTON
, FRM_SUN_CONTROL_RADIOBUTTON
,true )
88 // use the old control name for compytibility reasons
91 m_nClassId
= FormComponentType::RADIOBUTTON
;
92 m_aLabelServiceName
= FRM_SUN_COMPONENT_GROUPBOX
;
93 initValueProperty( PROPERTY_STATE
, PROPERTY_ID_STATE
);
94 startAggregatePropertyListening( PROPERTY_GROUP_NAME
);
98 ORadioButtonModel::ORadioButtonModel( const ORadioButtonModel
* _pOriginal
, const Reference
<XComponentContext
>& _rxFactory
)
99 :OReferenceValueComponent( _pOriginal
, _rxFactory
)
104 ORadioButtonModel::~ORadioButtonModel()
110 IMPLEMENT_DEFAULT_CLONING( ORadioButtonModel
)
114 StringSequence SAL_CALL
ORadioButtonModel::getSupportedServiceNames() throw(RuntimeException
, std::exception
)
116 StringSequence aSupported
= OReferenceValueComponent::getSupportedServiceNames();
118 sal_Int32 nOldLen
= aSupported
.getLength();
119 aSupported
.realloc( nOldLen
+ 9 );
120 OUString
* pStoreTo
= aSupported
.getArray() + nOldLen
;
122 *pStoreTo
++ = BINDABLE_CONTROL_MODEL
;
123 *pStoreTo
++ = DATA_AWARE_CONTROL_MODEL
;
124 *pStoreTo
++ = VALIDATABLE_CONTROL_MODEL
;
126 *pStoreTo
++ = BINDABLE_DATA_AWARE_CONTROL_MODEL
;
127 *pStoreTo
++ = VALIDATABLE_BINDABLE_CONTROL_MODEL
;
129 *pStoreTo
++ = FRM_SUN_COMPONENT_RADIOBUTTON
;
130 *pStoreTo
++ = FRM_SUN_COMPONENT_DATABASE_RADIOBUTTON
;
131 *pStoreTo
++ = BINDABLE_DATABASE_RADIO_BUTTON
;
133 *pStoreTo
++ = FRM_COMPONENT_RADIOBUTTON
;
139 void ORadioButtonModel::SetSiblingPropsTo(const OUString
& rPropName
, const Any
& rValue
)
143 if (hasProperty(PROPERTY_GROUP_NAME
, this))
144 this->getPropertyValue(PROPERTY_GROUP_NAME
) >>= sMyGroup
;
145 if (sMyGroup
.isEmpty())
148 // Iterate over my siblings
149 Reference
<XIndexAccess
> xIndexAccess(getParent(), UNO_QUERY
);
150 if (xIndexAccess
.is())
152 Reference
<XPropertySet
> xMyProps(
153 static_cast<XWeak
*>(this), css::uno::UNO_QUERY
);
154 OUString sCurrentGroup
;
155 sal_Int32 nNumSiblings
= xIndexAccess
->getCount();
156 for (sal_Int32 i
=0; i
<nNumSiblings
; ++i
)
158 Reference
<XPropertySet
> xSiblingProperties(*static_cast<InterfaceRef
const *>(xIndexAccess
->getByIndex(i
).getValue()), UNO_QUERY
);
159 if (!xSiblingProperties
.is())
161 if (xMyProps
== xSiblingProperties
)
162 continue; // do not set myself
164 // Only if it's a RadioButton
165 if (!hasProperty(PROPERTY_CLASSID
, xSiblingProperties
))
168 xSiblingProperties
->getPropertyValue(PROPERTY_CLASSID
) >>= nType
;
169 if (nType
!= FormComponentType::RADIOBUTTON
)
172 // The group association is attached to the name
173 sCurrentGroup
= OGroupManager::GetGroupName( xSiblingProperties
);
174 if (sCurrentGroup
== sMyGroup
)
175 xSiblingProperties
->setPropertyValue(rPropName
, rValue
);
181 void ORadioButtonModel::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle
, const Any
& rValue
) throw (Exception
, std::exception
)
183 OReferenceValueComponent::setFastPropertyValue_NoBroadcast( nHandle
, rValue
);
185 // if the label control changed ...
186 if (nHandle
== PROPERTY_ID_CONTROLLABEL
)
187 { // ... forward this to our siblings
188 SetSiblingPropsTo(PROPERTY_CONTROLLABEL
, rValue
);
191 // If the ControlSource property has changed ...
192 if (nHandle
== PROPERTY_ID_CONTROLSOURCE
)
193 { // ... I have to pass the new ControlSource to my siblings, which are in the same RadioButton group as I am
194 SetSiblingPropsTo(PROPERTY_CONTROLSOURCE
, rValue
);
197 // The other way: if my name changes ...
198 if (nHandle
== PROPERTY_ID_NAME
)
203 if (nHandle
== PROPERTY_ID_DEFAULT_STATE
)
208 { // Reset the 'default checked' for all Radios of the same group.
209 // Because (as the Highlander already knew): "There can be only one"
213 SetSiblingPropsTo(PROPERTY_DEFAULT_STATE
, aZero
);
218 void ORadioButtonModel::setControlSource()
220 Reference
<XIndexAccess
> xIndexAccess(getParent(), UNO_QUERY
);
221 if (xIndexAccess
.is())
223 OUString sName
, sGroupName
;
225 if (hasProperty(PROPERTY_GROUP_NAME
, this))
226 this->getPropertyValue(PROPERTY_GROUP_NAME
) >>= sGroupName
;
227 this->getPropertyValue(PROPERTY_NAME
) >>= sName
;
229 Reference
<XPropertySet
> xMyProps(
230 static_cast<XWeak
*>(this), css::uno::UNO_QUERY
);
231 for (sal_Int32 i
=0; i
<xIndexAccess
->getCount(); ++i
)
233 Reference
<XPropertySet
> xSiblingProperties(*static_cast<InterfaceRef
const *>(xIndexAccess
->getByIndex(i
).getValue()), UNO_QUERY
);
234 if (!xSiblingProperties
.is())
237 if (xMyProps
== xSiblingProperties
)
238 // Only if I didn't find myself
242 xSiblingProperties
->getPropertyValue(PROPERTY_CLASSID
) >>= nType
;
243 if (nType
!= FormComponentType::RADIOBUTTON
)
247 OUString sSiblingName
, sSiblingGroupName
;
248 if (hasProperty(PROPERTY_GROUP_NAME
, xSiblingProperties
))
249 xSiblingProperties
->getPropertyValue(PROPERTY_GROUP_NAME
) >>= sSiblingGroupName
;
250 xSiblingProperties
->getPropertyValue(PROPERTY_NAME
) >>= sSiblingName
;
252 if ((sGroupName
.isEmpty() && sSiblingGroupName
.isEmpty() && // (no group name
253 sName
== sSiblingName
) || // names match) or
254 (!sGroupName
.isEmpty() && !sSiblingGroupName
.isEmpty() && // (have group name
255 sGroupName
== sSiblingGroupName
)) // they match)
257 setPropertyValue(PROPERTY_CONTROLSOURCE
, xSiblingProperties
->getPropertyValue(PROPERTY_CONTROLSOURCE
));
265 void ORadioButtonModel::describeFixedProperties( Sequence
< Property
>& _rProps
) const
267 BEGIN_DESCRIBE_PROPERTIES( 1, OReferenceValueComponent
)
268 DECL_PROP1(TABINDEX
, sal_Int16
, BOUND
);
269 END_DESCRIBE_PROPERTIES();
273 OUString SAL_CALL
ORadioButtonModel::getServiceName() throw(RuntimeException
, std::exception
)
275 return OUString(FRM_COMPONENT_RADIOBUTTON
); // old (non-sun) name for compatibility !
279 void SAL_CALL
ORadioButtonModel::write(const Reference
<XObjectOutputStream
>& _rxOutStream
)
280 throw(IOException
, RuntimeException
, std::exception
)
282 OReferenceValueComponent::write(_rxOutStream
);
285 _rxOutStream
->writeShort(0x0003);
288 _rxOutStream
<< getReferenceValue();
289 _rxOutStream
<< (sal_Int16
)getDefaultChecked();
290 writeHelpTextCompatibly(_rxOutStream
);
292 // from version 0x0003 : common properties
293 writeCommonProperties(_rxOutStream
);
297 void SAL_CALL
ORadioButtonModel::read(const Reference
<XObjectInputStream
>& _rxInStream
) throw(IOException
, RuntimeException
, std::exception
)
299 OReferenceValueComponent::read(_rxInStream
);
300 ::osl::MutexGuard
aGuard(m_aMutex
);
303 sal_uInt16 nVersion
= _rxInStream
->readShort();
305 OUString sReferenceValue
;
306 sal_Int16
nDefaultChecked( 0 );
310 _rxInStream
>> sReferenceValue
;
311 _rxInStream
>> nDefaultChecked
;
314 _rxInStream
>> sReferenceValue
;
315 _rxInStream
>> nDefaultChecked
;
316 readHelpTextCompatibly(_rxInStream
);
319 _rxInStream
>> sReferenceValue
;
320 _rxInStream
>> nDefaultChecked
;
321 readHelpTextCompatibly(_rxInStream
);
322 readCommonProperties(_rxInStream
);
325 OSL_FAIL("ORadioButtonModel::read : unknown version !");
326 defaultCommonProperties();
330 setReferenceValue( sReferenceValue
);
331 setDefaultChecked( (ToggleState
)nDefaultChecked
);
333 // Display default values after read
334 if ( !getControlSource().isEmpty() )
335 // (not if we don't have a control source - the "State" property acts like it is persistent, then
340 void ORadioButtonModel::_propertyChanged(const PropertyChangeEvent
& _rEvent
) throw(RuntimeException
)
342 if ( _rEvent
.PropertyName
== PROPERTY_STATE
)
344 if ( _rEvent
.NewValue
== (sal_Int16
)1 )
346 // If my status has changed to 'checked', I have to reset all my siblings, which are in the same group as I am
348 aZero
<<= (sal_Int16
)0;
349 SetSiblingPropsTo( PROPERTY_STATE
, aZero
);
352 else if ( _rEvent
.PropertyName
== PROPERTY_GROUP_NAME
)
355 // Can't call OReferenceValueComponent::_propertyChanged(), as it
356 // doesn't know what to do with the GroupName property.
360 OReferenceValueComponent::_propertyChanged( _rEvent
);
364 Any
ORadioButtonModel::translateDbColumnToControlValue()
366 return makeAny( (sal_Int16
)
367 ( ( m_xColumn
->getString() == getReferenceValue() ) ? TRISTATE_TRUE
: TRISTATE_FALSE
)
372 Any
ORadioButtonModel::translateExternalValueToControlValue( const Any
& _rExternalValue
) const
374 Any aControlValue
= OReferenceValueComponent::translateExternalValueToControlValue( _rExternalValue
);
375 sal_Int16 nState
= TRISTATE_FALSE
;
376 if ( ( aControlValue
>>= nState
) && ( nState
== TRISTATE_INDET
) )
377 // radio buttons do not have the DONTKNOW state
378 aControlValue
<<= (sal_Int16
)TRISTATE_FALSE
;
379 return aControlValue
;
383 bool ORadioButtonModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
385 Reference
< XPropertySet
> xField( getField() );
386 OSL_PRECOND( xField
.is(), "ORadioButtonModel::commitControlValueToDbColumn: not bound!" );
391 sal_Int16 nValue
= 0;
392 m_xAggregateSet
->getPropertyValue( PROPERTY_STATE
) >>= nValue
;
394 xField
->setPropertyValue( PROPERTY_VALUE
, makeAny( getReferenceValue() ) );
396 catch(const Exception
&)
398 OSL_FAIL("ORadioButtonModel::commitControlValueToDbColumn: could not commit !");
406 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface
* SAL_CALL
407 com_sun_star_form_ORadioButtonModel_get_implementation(::com::sun::star::uno::XComponentContext
* component
,
408 ::com::sun::star::uno::Sequence
<css::uno::Any
> const &)
410 return cppu::acquire(new frm::ORadioButtonModel(component
));
413 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface
* SAL_CALL
414 com_sun_star_form_ORadioButtonControl_get_implementation(::com::sun::star::uno::XComponentContext
* component
,
415 ::com::sun::star::uno::Sequence
<css::uno::Any
> const &)
417 return cppu::acquire(new frm::ORadioButtonControl(component
));
420 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */