bump product version to 4.1.6.2
[LibreOffice.git] / forms / source / component / RadioButton.cxx
blobb0bcdfb2a8801ac5505cde927df3fe43aa2c73e2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <tools/debug.hxx>
26 #include <comphelper/extract.hxx>
27 #include <comphelper/basicio.hxx>
28 #include <com/sun/star/container/XIndexAccess.hpp>
29 #include <com/sun/star/awt/XVclWindowPeer.hpp>
31 //.........................................................................
32 namespace frm
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::sdb;
36 using namespace ::com::sun::star::sdbc;
37 using namespace ::com::sun::star::sdbcx;
38 using namespace ::com::sun::star::beans;
39 using namespace ::com::sun::star::container;
40 using namespace ::com::sun::star::form;
41 using namespace ::com::sun::star::awt;
42 using namespace ::com::sun::star::io;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::util;
45 using namespace ::com::sun::star::form::binding;
47 //==================================================================
48 //------------------------------------------------------------------------------
49 InterfaceRef SAL_CALL ORadioButtonControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory) throw (RuntimeException)
51 return *(new ORadioButtonControl(_rxFactory));
54 //------------------------------------------------------------------------------
55 StringSequence SAL_CALL ORadioButtonControl::getSupportedServiceNames() throw(RuntimeException)
57 StringSequence aSupported = OBoundControl::getSupportedServiceNames();
58 aSupported.realloc(aSupported.getLength() + 1);
60 OUString* pArray = aSupported.getArray();
61 pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_RADIOBUTTON;
62 return aSupported;
66 //------------------------------------------------------------------
67 ORadioButtonControl::ORadioButtonControl(const Reference<XMultiServiceFactory>& _rxFactory)
68 :OBoundControl(_rxFactory, VCL_CONTROL_RADIOBUTTON)
72 //------------------------------------------------------------------
73 void SAL_CALL ORadioButtonControl::createPeer(const Reference<starawt::XToolkit>& _rxToolkit, const Reference<starawt::XWindowPeer>& _rxParent) throw (RuntimeException)
75 OBoundControl::createPeer(_rxToolkit, _rxParent);
77 // switch off the auto-toggle, we do this ourself ....
78 // (formerly this switch-off was done in the toolkit - but the correct place is here ...)
79 // Reference< XVclWindowPeer > xVclWindowPeer( getPeer(), UNO_QUERY );
80 // if (xVclWindowPeer.is())
81 // xVclWindowPeer->setProperty(OUString("AutoToggle"), ::cppu::bool2any(sal_False));
82 // new order: do _not_ switch off the auto toggle because:
83 // * today, it is not necessary anymore to handle the toggling ourself (everything works fine without it)
84 // * without auto toggle, the AccessibleEvents as fired by the radio buttons are
85 // a. newly checked button: "unchecked"->"checked"
86 // b. previously checked button: "checked"->"unchecked"
87 // This is deadly for AT-tools, which then get the "unchecked" event _immediately_ after the "checked" event,
88 // and only read the latter. This makes radio buttons pretty unusable in form documents.
89 // So we switched AutoToggle _on_, again, because then VCL can handle the notifications, and will send
90 // them in the proper order.
93 //==================================================================
94 InterfaceRef SAL_CALL ORadioButtonModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory) throw (RuntimeException)
96 return *(new ORadioButtonModel(_rxFactory));
99 //------------------------------------------------------------------
100 DBG_NAME( ORadioButtonModel )
101 //------------------------------------------------------------------
102 ORadioButtonModel::ORadioButtonModel(const Reference<XMultiServiceFactory>& _rxFactory)
103 :OReferenceValueComponent( _rxFactory, VCL_CONTROLMODEL_RADIOBUTTON, FRM_SUN_CONTROL_RADIOBUTTON,sal_True )
104 // use the old control name for compytibility reasons
106 DBG_CTOR( ORadioButtonModel, NULL );
108 m_nClassId = FormComponentType::RADIOBUTTON;
109 m_aLabelServiceName = FRM_SUN_COMPONENT_GROUPBOX;
110 initValueProperty( PROPERTY_STATE, PROPERTY_ID_STATE );
111 startAggregatePropertyListening( PROPERTY_GROUP_NAME );
114 //------------------------------------------------------------------
115 ORadioButtonModel::ORadioButtonModel( const ORadioButtonModel* _pOriginal, const Reference<XMultiServiceFactory>& _rxFactory )
116 :OReferenceValueComponent( _pOriginal, _rxFactory )
118 DBG_CTOR( ORadioButtonModel, NULL );
121 //------------------------------------------------------------------------------
122 ORadioButtonModel::~ORadioButtonModel()
124 DBG_DTOR( ORadioButtonModel, NULL );
127 // XCloneable
128 //------------------------------------------------------------------------------
129 IMPLEMENT_DEFAULT_CLONING( ORadioButtonModel )
131 // XServiceInfo
132 //------------------------------------------------------------------------------
133 StringSequence SAL_CALL ORadioButtonModel::getSupportedServiceNames() throw(RuntimeException)
135 StringSequence aSupported = OReferenceValueComponent::getSupportedServiceNames();
137 sal_Int32 nOldLen = aSupported.getLength();
138 aSupported.realloc( nOldLen + 8 );
139 OUString* pStoreTo = aSupported.getArray() + nOldLen;
141 *pStoreTo++ = BINDABLE_CONTROL_MODEL;
142 *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
143 *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
145 *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
146 *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
148 *pStoreTo++ = FRM_SUN_COMPONENT_RADIOBUTTON;
149 *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_RADIOBUTTON;
150 *pStoreTo++ = BINDABLE_DATABASE_RADIO_BUTTON;
152 return aSupported;
155 //------------------------------------------------------------------------------
156 void ORadioButtonModel::SetSiblingPropsTo(const OUString& rPropName, const Any& rValue)
158 // my name
159 OUString sMyGroup;
160 if (hasProperty(PROPERTY_GROUP_NAME, this))
161 this->getPropertyValue(PROPERTY_GROUP_NAME) >>= sMyGroup;
162 if (sMyGroup.isEmpty())
163 sMyGroup = m_aName;
165 // Iterate over my siblings
166 Reference<XIndexAccess> xIndexAccess(getParent(), UNO_QUERY);
167 if (xIndexAccess.is())
169 Reference<XPropertySet> xMyProps;
170 query_interface(static_cast<XWeak*>(this), xMyProps);
171 OUString sCurrentGroup;
172 sal_Int32 nNumSiblings = xIndexAccess->getCount();
173 for (sal_Int32 i=0; i<nNumSiblings; ++i)
175 Reference<XPropertySet> xSiblingProperties(*(InterfaceRef*)xIndexAccess->getByIndex(i).getValue(), UNO_QUERY);
176 if (!xSiblingProperties.is())
177 continue;
178 if (xMyProps == xSiblingProperties)
179 continue; // do not set myself
181 // Only if it's a RadioButton
182 if (!hasProperty(PROPERTY_CLASSID, xSiblingProperties))
183 continue;
184 sal_Int16 nType = 0;
185 xSiblingProperties->getPropertyValue(PROPERTY_CLASSID) >>= nType;
186 if (nType != FormComponentType::RADIOBUTTON)
187 continue;
189 // The group association is attached to the name
190 sCurrentGroup = OGroupManager::GetGroupName( xSiblingProperties );
191 if (sCurrentGroup == sMyGroup)
192 xSiblingProperties->setPropertyValue(rPropName, rValue);
197 //------------------------------------------------------------------------------
198 void ORadioButtonModel::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const Any& rValue) throw (Exception)
200 OReferenceValueComponent::setFastPropertyValue_NoBroadcast( nHandle, rValue );
202 // if the label control changed ...
203 if (nHandle == PROPERTY_ID_CONTROLLABEL)
204 { // ... forward this to our siblings
205 SetSiblingPropsTo(PROPERTY_CONTROLLABEL, rValue);
208 // If the ControlSource property has changed ...
209 if (nHandle == PROPERTY_ID_CONTROLSOURCE)
210 { // ... I have to pass the new ControlSource to my siblings, which are in the same RadioButton group as I am
211 SetSiblingPropsTo(PROPERTY_CONTROLSOURCE, rValue);
214 // The other way: if my name changes ...
215 if (nHandle == PROPERTY_ID_NAME)
217 setControlSource();
220 if (nHandle == PROPERTY_ID_DEFAULT_STATE)
222 sal_Int16 nValue;
223 rValue >>= nValue;
224 if (1 == nValue)
225 { // Reset the 'default checked' for all Radios of the same group.
226 // Because (as the Highlander already knew): "There can be only one"
227 Any aZero;
228 nValue = 0;
229 aZero <<= nValue;
230 SetSiblingPropsTo(PROPERTY_DEFAULT_STATE, aZero);
235 void ORadioButtonModel::setControlSource()
237 Reference<XIndexAccess> xIndexAccess(getParent(), UNO_QUERY);
238 if (xIndexAccess.is())
240 OUString sName, sGroupName;
242 if (hasProperty(PROPERTY_GROUP_NAME, this))
243 this->getPropertyValue(PROPERTY_GROUP_NAME) >>= sGroupName;
244 this->getPropertyValue(PROPERTY_NAME) >>= sName;
246 Reference<XPropertySet> xMyProps;
247 query_interface(static_cast<XWeak*>(this), xMyProps);
248 for (sal_Int32 i=0; i<xIndexAccess->getCount(); ++i)
250 Reference<XPropertySet> xSiblingProperties(*(InterfaceRef*)xIndexAccess->getByIndex(i).getValue(), UNO_QUERY);
251 if (!xSiblingProperties.is())
252 continue;
254 if (xMyProps == xSiblingProperties)
255 // Only if I didn't find myself
256 continue;
258 sal_Int16 nType = 0;
259 xSiblingProperties->getPropertyValue(PROPERTY_CLASSID) >>= nType;
260 if (nType != FormComponentType::RADIOBUTTON)
261 // Only RadioButtons
262 continue;
264 OUString sSiblingName, sSiblingGroupName;
265 if (hasProperty(PROPERTY_GROUP_NAME, xSiblingProperties))
266 xSiblingProperties->getPropertyValue(PROPERTY_GROUP_NAME) >>= sSiblingGroupName;
267 xSiblingProperties->getPropertyValue(PROPERTY_NAME) >>= sSiblingName;
269 if ((sGroupName.isEmpty() && sSiblingGroupName.isEmpty() && // (no group name
270 sName == sSiblingName) || // names match) or
271 (!sGroupName.isEmpty() && !sSiblingGroupName.isEmpty() && // (have group name
272 sGroupName == sSiblingGroupName)) // they match)
274 setPropertyValue(PROPERTY_CONTROLSOURCE, xSiblingProperties->getPropertyValue(PROPERTY_CONTROLSOURCE));
275 break;
281 //------------------------------------------------------------------------------
282 void ORadioButtonModel::describeFixedProperties( Sequence< Property >& _rProps ) const
284 BEGIN_DESCRIBE_PROPERTIES( 1, OReferenceValueComponent )
285 DECL_PROP1(TABINDEX, sal_Int16, BOUND);
286 END_DESCRIBE_PROPERTIES();
289 //------------------------------------------------------------------------------
290 OUString SAL_CALL ORadioButtonModel::getServiceName() throw(RuntimeException)
292 return OUString(FRM_COMPONENT_RADIOBUTTON); // old (non-sun) name for compatibility !
295 //------------------------------------------------------------------------------
296 void SAL_CALL ORadioButtonModel::write(const Reference<XObjectOutputStream>& _rxOutStream)
297 throw(IOException, RuntimeException)
299 OReferenceValueComponent::write(_rxOutStream);
301 // Version
302 _rxOutStream->writeShort(0x0003);
304 // Properties
305 _rxOutStream << getReferenceValue();
306 _rxOutStream << (sal_Int16)getDefaultChecked();
307 writeHelpTextCompatibly(_rxOutStream);
309 // from version 0x0003 : common properties
310 writeCommonProperties(_rxOutStream);
313 //------------------------------------------------------------------------------
314 void SAL_CALL ORadioButtonModel::read(const Reference<XObjectInputStream>& _rxInStream) throw(IOException, RuntimeException)
316 OReferenceValueComponent::read(_rxInStream);
317 ::osl::MutexGuard aGuard(m_aMutex);
319 // Version
320 sal_uInt16 nVersion = _rxInStream->readShort();
322 OUString sReferenceValue;
323 sal_Int16 nDefaultChecked( 0 );
324 switch (nVersion)
326 case 0x0001 :
327 _rxInStream >> sReferenceValue;
328 _rxInStream >> nDefaultChecked;
329 break;
330 case 0x0002 :
331 _rxInStream >> sReferenceValue;
332 _rxInStream >> nDefaultChecked;
333 readHelpTextCompatibly(_rxInStream);
334 break;
335 case 0x0003 :
336 _rxInStream >> sReferenceValue;
337 _rxInStream >> nDefaultChecked;
338 readHelpTextCompatibly(_rxInStream);
339 readCommonProperties(_rxInStream);
340 break;
341 default :
342 OSL_FAIL("ORadioButtonModel::read : unknown version !");
343 defaultCommonProperties();
344 break;
347 setReferenceValue( sReferenceValue );
348 setDefaultChecked( (ToggleState)nDefaultChecked );
350 // Display default values after read
351 if ( !getControlSource().isEmpty() )
352 // (not if we don't have a control source - the "State" property acts like it is persistent, then
353 resetNoBroadcast();
356 //------------------------------------------------------------------------------
357 void ORadioButtonModel::_propertyChanged(const PropertyChangeEvent& _rEvent) throw(RuntimeException)
359 if ( _rEvent.PropertyName.equals( PROPERTY_STATE ) )
361 if ( _rEvent.NewValue == (sal_Int16)1 )
363 // If my status has changed to 'checked', I have to reset all my siblings, which are in the same group as I am
364 Any aZero;
365 aZero <<= (sal_Int16)0;
366 SetSiblingPropsTo( PROPERTY_STATE, aZero );
369 else if ( _rEvent.PropertyName.equals( PROPERTY_GROUP_NAME ) )
371 setControlSource();
372 // Can't call OReferenceValueComponent::_propertyChanged(), as it
373 // doesn't know what to do with the GroupName property.
374 return;
377 OReferenceValueComponent::_propertyChanged( _rEvent );
380 //------------------------------------------------------------------------------
381 Any ORadioButtonModel::translateDbColumnToControlValue()
383 return makeAny( (sal_Int16)
384 ( ( m_xColumn->getString() == getReferenceValue() ) ? STATE_CHECK : STATE_NOCHECK )
388 //------------------------------------------------------------------------------
389 Any ORadioButtonModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
391 Any aControlValue = OReferenceValueComponent::translateExternalValueToControlValue( _rExternalValue );
392 sal_Int16 nState = STATE_NOCHECK;
393 if ( ( aControlValue >>= nState ) && ( nState == STATE_DONTKNOW ) )
394 // radio buttons do not have the DONTKNOW state
395 aControlValue <<= (sal_Int16)STATE_NOCHECK;
396 return aControlValue;
399 //-----------------------------------------------------------------------------
400 sal_Bool ORadioButtonModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
402 Reference< XPropertySet > xField( getField() );
403 OSL_PRECOND( xField.is(), "ORadioButtonModel::commitControlValueToDbColumn: not bound!" );
404 if ( xField.is() )
408 sal_Int16 nValue = 0;
409 m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) >>= nValue;
410 if ( nValue == 1 )
411 xField->setPropertyValue( PROPERTY_VALUE, makeAny( getReferenceValue() ) );
413 catch(const Exception&)
415 OSL_FAIL("ORadioButtonModel::commitControlValueToDbColumn: could not commit !");
418 return sal_True;
421 //.........................................................................
423 //.........................................................................
425 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */