update dev300-m58
[ooovba.git] / forms / source / component / RadioButton.cxx
blob7b5780d9fc18c8b702933d6081a78e5e161c2b56
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: RadioButton.cxx,v $
10 * $Revision: 1.23 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_forms.hxx"
33 #include "RadioButton.hxx"
34 #include "GroupManager.hxx"
35 #include "property.hxx"
36 #ifndef _FRM_PROPERTY_HRC_
37 #include "property.hrc"
38 #endif
39 #include "services.hxx"
40 #include <tools/debug.hxx>
41 #include <comphelper/extract.hxx>
42 #include <comphelper/basicio.hxx>
43 #include <com/sun/star/container/XIndexAccess.hpp>
44 #include <com/sun/star/awt/XVclWindowPeer.hpp>
46 //.........................................................................
47 namespace frm
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::sdb;
51 using namespace ::com::sun::star::sdbc;
52 using namespace ::com::sun::star::sdbcx;
53 using namespace ::com::sun::star::beans;
54 using namespace ::com::sun::star::container;
55 using namespace ::com::sun::star::form;
56 using namespace ::com::sun::star::awt;
57 using namespace ::com::sun::star::io;
58 using namespace ::com::sun::star::lang;
59 using namespace ::com::sun::star::util;
60 using namespace ::com::sun::star::form::binding;
62 //==================================================================
63 //------------------------------------------------------------------------------
64 InterfaceRef SAL_CALL ORadioButtonControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory) throw (RuntimeException)
66 return *(new ORadioButtonControl(_rxFactory));
69 //------------------------------------------------------------------------------
70 StringSequence SAL_CALL ORadioButtonControl::getSupportedServiceNames() throw(RuntimeException)
72 StringSequence aSupported = OBoundControl::getSupportedServiceNames();
73 aSupported.realloc(aSupported.getLength() + 1);
75 ::rtl::OUString* pArray = aSupported.getArray();
76 pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_RADIOBUTTON;
77 return aSupported;
81 //------------------------------------------------------------------
82 ORadioButtonControl::ORadioButtonControl(const Reference<XMultiServiceFactory>& _rxFactory)
83 :OBoundControl(_rxFactory, VCL_CONTROL_RADIOBUTTON)
87 //------------------------------------------------------------------
88 void SAL_CALL ORadioButtonControl::createPeer(const Reference<starawt::XToolkit>& _rxToolkit, const Reference<starawt::XWindowPeer>& _rxParent) throw (RuntimeException)
90 OBoundControl::createPeer(_rxToolkit, _rxParent);
92 // switch off the auto-toggle, we do this ourself ....
93 // (formerly this switch-off was done in the toolkit - but the correct place is here ...)
94 // Reference< XVclWindowPeer > xVclWindowPeer( getPeer(), UNO_QUERY );
95 // if (xVclWindowPeer.is())
96 // xVclWindowPeer->setProperty(::rtl::OUString::createFromAscii("AutoToggle"), ::cppu::bool2any(sal_False));
97 // new order: do _not_ switch off the auto toggle because:
98 // * today, it is not necessary anymore to handle the toggling ourself (everything works fine without it)
99 // * without auto toggle, the AccessibleEvents as fired by the radio buttons are
100 // a. newly checked button: "unchecked"->"checked"
101 // b. previously checked button: "checked"->"unchecked"
102 // This is deadly for AT-tools, which then get the "unchecked" event _immediately_ after the "checked" event,
103 // and only read the latter. This makes radio buttons pretty unusable in form documents.
104 // So we switched AutoToggle _on_, again, because then VCL can handle the notifications, and will send
105 // them in the proper order.
108 //==================================================================
109 InterfaceRef SAL_CALL ORadioButtonModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory) throw (RuntimeException)
111 return *(new ORadioButtonModel(_rxFactory));
114 //------------------------------------------------------------------
115 DBG_NAME( ORadioButtonModel )
116 //------------------------------------------------------------------
117 ORadioButtonModel::ORadioButtonModel(const Reference<XMultiServiceFactory>& _rxFactory)
118 :OReferenceValueComponent( _rxFactory, VCL_CONTROLMODEL_RADIOBUTTON, FRM_SUN_CONTROL_RADIOBUTTON,sal_True )
119 // use the old control name for compytibility reasons
121 DBG_CTOR( ORadioButtonModel, NULL );
123 m_nClassId = FormComponentType::RADIOBUTTON;
124 m_aLabelServiceName = FRM_SUN_COMPONENT_GROUPBOX;
125 initValueProperty( PROPERTY_STATE, PROPERTY_ID_STATE );
126 startAggregatePropertyListening( PROPERTY_GROUP_NAME );
129 //------------------------------------------------------------------
130 ORadioButtonModel::ORadioButtonModel( const ORadioButtonModel* _pOriginal, const Reference<XMultiServiceFactory>& _rxFactory )
131 :OReferenceValueComponent( _pOriginal, _rxFactory )
133 DBG_CTOR( ORadioButtonModel, NULL );
136 //------------------------------------------------------------------------------
137 ORadioButtonModel::~ORadioButtonModel()
139 DBG_DTOR( ORadioButtonModel, NULL );
142 // XCloneable
143 //------------------------------------------------------------------------------
144 IMPLEMENT_DEFAULT_CLONING( ORadioButtonModel )
146 // XServiceInfo
147 //------------------------------------------------------------------------------
148 StringSequence SAL_CALL ORadioButtonModel::getSupportedServiceNames() throw(RuntimeException)
150 StringSequence aSupported = OReferenceValueComponent::getSupportedServiceNames();
152 sal_Int32 nOldLen = aSupported.getLength();
153 aSupported.realloc( nOldLen + 8 );
154 ::rtl::OUString* pStoreTo = aSupported.getArray() + nOldLen;
156 *pStoreTo++ = BINDABLE_CONTROL_MODEL;
157 *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
158 *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
160 *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
161 *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
163 *pStoreTo++ = FRM_SUN_COMPONENT_RADIOBUTTON;
164 *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_RADIOBUTTON;
165 *pStoreTo++ = BINDABLE_DATABASE_RADIO_BUTTON;
167 return aSupported;
170 //------------------------------------------------------------------------------
171 void ORadioButtonModel::SetSiblingPropsTo(const ::rtl::OUString& rPropName, const Any& rValue)
173 // mein Name
174 ::rtl::OUString sMyGroup;
175 if (hasProperty(PROPERTY_GROUP_NAME, this))
176 this->getPropertyValue(PROPERTY_GROUP_NAME) >>= sMyGroup;
177 if (sMyGroup.getLength() == 0)
178 sMyGroup = m_aName;
180 // meine Siblings durchiterieren
181 Reference<XIndexAccess> xIndexAccess(getParent(), UNO_QUERY);
182 if (xIndexAccess.is())
184 Reference<XPropertySet> xMyProps;
185 query_interface(static_cast<XWeak*>(this), xMyProps);
186 ::rtl::OUString sCurrentGroup;
187 sal_Int32 nNumSiblings = xIndexAccess->getCount();
188 for (sal_Int32 i=0; i<nNumSiblings; ++i)
190 Reference<XPropertySet> xSiblingProperties(*(InterfaceRef*)xIndexAccess->getByIndex(i).getValue(), UNO_QUERY);
191 if (!xSiblingProperties.is())
192 continue;
193 if (xMyProps == xSiblingProperties)
194 continue; // mich selber nicht umsetzen
196 // nur wenn es ein Radio-Button ist
197 if (!hasProperty(PROPERTY_CLASSID, xSiblingProperties))
198 continue;
199 sal_Int16 nType = 0;
200 xSiblingProperties->getPropertyValue(PROPERTY_CLASSID) >>= nType;
201 if (nType != FormComponentType::RADIOBUTTON)
202 continue;
204 // das 'zur selben Gruppe gehoeren' wird am Namen festgemacht
205 sCurrentGroup = OGroupManager::GetGroupName( xSiblingProperties );
206 if (sCurrentGroup == sMyGroup)
207 xSiblingProperties->setPropertyValue(rPropName, rValue);
212 //------------------------------------------------------------------------------
213 void ORadioButtonModel::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const Any& rValue) throw (Exception)
215 OReferenceValueComponent::setFastPropertyValue_NoBroadcast( nHandle, rValue );
217 // if the label control changed ...
218 if (nHandle == PROPERTY_ID_CONTROLLABEL)
219 { // ... forward this to our siblings
220 SetSiblingPropsTo(PROPERTY_CONTROLLABEL, rValue);
223 // wenn sich die ControlSource-Eigenschaft geaendert hat ...
224 if (nHandle == PROPERTY_ID_CONTROLSOURCE)
225 { // ... muss ich allen meinen Siblings, die in der selben RadioButton-Gruppe sind wie ich, auch die
226 // neue ControlSource mitgeben
227 SetSiblingPropsTo(PROPERTY_CONTROLSOURCE, rValue);
230 // die andere Richtung : wenn sich mein Name aendert ...
231 if (nHandle == PROPERTY_ID_NAME)
233 setControlSource();
236 if (nHandle == PROPERTY_ID_DEFAULT_STATE)
238 sal_Int16 nValue;
239 rValue >>= nValue;
240 if (1 == nValue)
241 { // bei allen Radios der selben Gruppe das 'default checked' ruecksetzen, denn wie schon der highlander wusste :
242 // es kann nur einen geben.
243 Any aZero;
244 nValue = 0;
245 aZero <<= nValue;
246 SetSiblingPropsTo(PROPERTY_DEFAULT_STATE, aZero);
251 void ORadioButtonModel::setControlSource()
253 Reference<XIndexAccess> xIndexAccess(getParent(), UNO_QUERY);
254 if (xIndexAccess.is())
256 ::rtl::OUString sName, sGroupName;
258 if (hasProperty(PROPERTY_GROUP_NAME, this))
259 this->getPropertyValue(PROPERTY_GROUP_NAME) >>= sGroupName;
260 this->getPropertyValue(PROPERTY_NAME) >>= sName;
262 Reference<XPropertySet> xMyProps;
263 query_interface(static_cast<XWeak*>(this), xMyProps);
264 for (sal_Int32 i=0; i<xIndexAccess->getCount(); ++i)
266 Reference<XPropertySet> xSiblingProperties(*(InterfaceRef*)xIndexAccess->getByIndex(i).getValue(), UNO_QUERY);
267 if (!xSiblingProperties.is())
268 continue;
270 if (xMyProps == xSiblingProperties)
271 // nur wenn ich nicht mich selber gefunden habe
272 continue;
274 sal_Int16 nType = 0;
275 xSiblingProperties->getPropertyValue(PROPERTY_CLASSID) >>= nType;
276 if (nType != FormComponentType::RADIOBUTTON)
277 // nur Radio-Buttons
278 continue;
280 ::rtl::OUString sSiblingName, sSiblingGroupName;
281 if (hasProperty(PROPERTY_GROUP_NAME, xSiblingProperties))
282 xSiblingProperties->getPropertyValue(PROPERTY_GROUP_NAME) >>= sSiblingGroupName;
283 xSiblingProperties->getPropertyValue(PROPERTY_NAME) >>= sSiblingName;
285 if ((sGroupName.getLength() == 0 && sSiblingGroupName.getLength() == 0 && // (no group name
286 sName == sSiblingName) || // names match) or
287 (sGroupName.getLength() != 0 && sSiblingGroupName.getLength() != 0 && // (have group name
288 sGroupName == sSiblingGroupName)) // they match)
290 setPropertyValue(PROPERTY_CONTROLSOURCE, xSiblingProperties->getPropertyValue(PROPERTY_CONTROLSOURCE));
291 break;
297 //------------------------------------------------------------------------------
298 void ORadioButtonModel::describeFixedProperties( Sequence< Property >& _rProps ) const
300 BEGIN_DESCRIBE_PROPERTIES( 1, OReferenceValueComponent )
301 DECL_PROP1(TABINDEX, sal_Int16, BOUND);
302 END_DESCRIBE_PROPERTIES();
305 //------------------------------------------------------------------------------
306 ::rtl::OUString SAL_CALL ORadioButtonModel::getServiceName() throw(RuntimeException)
308 return FRM_COMPONENT_RADIOBUTTON; // old (non-sun) name for compatibility !
311 //------------------------------------------------------------------------------
312 void SAL_CALL ORadioButtonModel::write(const Reference<XObjectOutputStream>& _rxOutStream)
313 throw(IOException, RuntimeException)
315 OReferenceValueComponent::write(_rxOutStream);
317 // Version
318 _rxOutStream->writeShort(0x0003);
320 // Properties
321 _rxOutStream << getReferenceValue();
322 _rxOutStream << (sal_Int16)getDefaultChecked();
323 writeHelpTextCompatibly(_rxOutStream);
325 // from version 0x0003 : common properties
326 writeCommonProperties(_rxOutStream);
329 //------------------------------------------------------------------------------
330 void SAL_CALL ORadioButtonModel::read(const Reference<XObjectInputStream>& _rxInStream) throw(IOException, RuntimeException)
332 OReferenceValueComponent::read(_rxInStream);
333 ::osl::MutexGuard aGuard(m_aMutex);
335 // Version
336 UINT16 nVersion = _rxInStream->readShort();
338 ::rtl::OUString sReferenceValue;
339 sal_Int16 nDefaultChecked( 0 );
340 switch (nVersion)
342 case 0x0001 :
343 _rxInStream >> sReferenceValue;
344 _rxInStream >> nDefaultChecked;
345 break;
346 case 0x0002 :
347 _rxInStream >> sReferenceValue;
348 _rxInStream >> nDefaultChecked;
349 readHelpTextCompatibly(_rxInStream);
350 break;
351 case 0x0003 :
352 _rxInStream >> sReferenceValue;
353 _rxInStream >> nDefaultChecked;
354 readHelpTextCompatibly(_rxInStream);
355 readCommonProperties(_rxInStream);
356 break;
357 default :
358 DBG_ERROR("ORadioButtonModel::read : unknown version !");
359 defaultCommonProperties();
360 break;
363 setReferenceValue( sReferenceValue );
364 setDefaultChecked( (ToggleState)nDefaultChecked );
366 // Nach dem Lesen die Defaultwerte anzeigen
367 if ( getControlSource().getLength() )
368 // (not if we don't have a control source - the "State" property acts like it is persistent, then
369 resetNoBroadcast();
372 //------------------------------------------------------------------------------
373 void ORadioButtonModel::_propertyChanged(const PropertyChangeEvent& _rEvent) throw(RuntimeException)
375 if ( _rEvent.PropertyName.equals( PROPERTY_STATE ) )
377 if ( _rEvent.NewValue == (sal_Int16)1 )
379 // wenn sich mein Status auf 'checked' geaendert hat, muss ich alle meine Siblings, die in der selben Gruppe
380 // sind wie ich, entsprechend zuruecksetzen
381 Any aZero;
382 aZero <<= (sal_Int16)0;
383 SetSiblingPropsTo( PROPERTY_STATE, aZero );
386 else if ( _rEvent.PropertyName.equals( PROPERTY_GROUP_NAME ) )
388 setControlSource();
389 // Can't call OReferenceValueComponent::_propertyChanged(), as it
390 // doesn't know what to do with the GroupName property.
391 return;
394 OReferenceValueComponent::_propertyChanged( _rEvent );
397 //------------------------------------------------------------------------------
398 Any ORadioButtonModel::translateDbColumnToControlValue()
400 return makeAny( (sal_Int16)
401 ( ( m_xColumn->getString() == getReferenceValue() ) ? STATE_CHECK : STATE_NOCHECK )
405 //------------------------------------------------------------------------------
406 Any ORadioButtonModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
408 Any aControlValue = OReferenceValueComponent::translateExternalValueToControlValue( _rExternalValue );
409 sal_Int16 nState = STATE_NOCHECK;
410 if ( ( aControlValue >>= nState ) && ( nState == STATE_DONTKNOW ) )
411 // radio buttons do not have the DONTKNOW state
412 aControlValue <<= (sal_Int16)STATE_NOCHECK;
413 return aControlValue;
416 //-----------------------------------------------------------------------------
417 sal_Bool ORadioButtonModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
419 Reference< XPropertySet > xField( getField() );
420 OSL_PRECOND( xField.is(), "ORadioButtonModel::commitControlValueToDbColumn: not bound!" );
421 if ( xField.is() )
425 sal_Int16 nValue = 0;
426 m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) >>= nValue;
427 if ( nValue == 1 )
428 xField->setPropertyValue( PROPERTY_VALUE, makeAny( getReferenceValue() ) );
430 catch(Exception&)
432 DBG_ERROR("ORadioButtonModel::commitControlValueToDbColumn: could not commit !");
435 return sal_True;
438 //.........................................................................
440 //.........................................................................