Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
30 :
31 : //.........................................................................
32 : namespace frm
33 : {
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;
46 :
47 : //==================================================================
48 : //------------------------------------------------------------------------------
49 0 : InterfaceRef SAL_CALL ORadioButtonControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory) throw (RuntimeException)
50 : {
51 0 : return *(new ORadioButtonControl(_rxFactory));
52 : }
53 :
54 : //------------------------------------------------------------------------------
55 0 : StringSequence SAL_CALL ORadioButtonControl::getSupportedServiceNames() throw(RuntimeException)
56 : {
57 0 : StringSequence aSupported = OBoundControl::getSupportedServiceNames();
58 0 : aSupported.realloc(aSupported.getLength() + 1);
59 :
60 0 : ::rtl::OUString* pArray = aSupported.getArray();
61 0 : pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_RADIOBUTTON;
62 0 : return aSupported;
63 : }
64 :
65 :
66 : //------------------------------------------------------------------
67 0 : ORadioButtonControl::ORadioButtonControl(const Reference<XMultiServiceFactory>& _rxFactory)
68 0 : :OBoundControl(_rxFactory, VCL_CONTROL_RADIOBUTTON)
69 : {
70 0 : }
71 :
72 : //------------------------------------------------------------------
73 0 : void SAL_CALL ORadioButtonControl::createPeer(const Reference<starawt::XToolkit>& _rxToolkit, const Reference<starawt::XWindowPeer>& _rxParent) throw (RuntimeException)
74 : {
75 0 : OBoundControl::createPeer(_rxToolkit, _rxParent);
76 :
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(::rtl::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.
91 0 : }
92 :
93 : //==================================================================
94 4 : InterfaceRef SAL_CALL ORadioButtonModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory) throw (RuntimeException)
95 : {
96 4 : return *(new ORadioButtonModel(_rxFactory));
97 : }
98 :
99 : //------------------------------------------------------------------
100 : DBG_NAME( ORadioButtonModel )
101 : //------------------------------------------------------------------
102 4 : ORadioButtonModel::ORadioButtonModel(const Reference<XMultiServiceFactory>& _rxFactory)
103 4 : :OReferenceValueComponent( _rxFactory, VCL_CONTROLMODEL_RADIOBUTTON, FRM_SUN_CONTROL_RADIOBUTTON,sal_True )
104 : // use the old control name for compytibility reasons
105 : {
106 : DBG_CTOR( ORadioButtonModel, NULL );
107 :
108 4 : m_nClassId = FormComponentType::RADIOBUTTON;
109 4 : m_aLabelServiceName = FRM_SUN_COMPONENT_GROUPBOX;
110 4 : initValueProperty( PROPERTY_STATE, PROPERTY_ID_STATE );
111 4 : startAggregatePropertyListening( PROPERTY_GROUP_NAME );
112 4 : }
113 :
114 : //------------------------------------------------------------------
115 0 : ORadioButtonModel::ORadioButtonModel( const ORadioButtonModel* _pOriginal, const Reference<XMultiServiceFactory>& _rxFactory )
116 0 : :OReferenceValueComponent( _pOriginal, _rxFactory )
117 : {
118 : DBG_CTOR( ORadioButtonModel, NULL );
119 0 : }
120 :
121 : //------------------------------------------------------------------------------
122 0 : ORadioButtonModel::~ORadioButtonModel()
123 : {
124 : DBG_DTOR( ORadioButtonModel, NULL );
125 0 : }
126 :
127 : // XCloneable
128 : //------------------------------------------------------------------------------
129 0 : IMPLEMENT_DEFAULT_CLONING( ORadioButtonModel )
130 :
131 : // XServiceInfo
132 : //------------------------------------------------------------------------------
133 0 : StringSequence SAL_CALL ORadioButtonModel::getSupportedServiceNames() throw(RuntimeException)
134 : {
135 0 : StringSequence aSupported = OReferenceValueComponent::getSupportedServiceNames();
136 :
137 0 : sal_Int32 nOldLen = aSupported.getLength();
138 0 : aSupported.realloc( nOldLen + 8 );
139 0 : ::rtl::OUString* pStoreTo = aSupported.getArray() + nOldLen;
140 :
141 0 : *pStoreTo++ = BINDABLE_CONTROL_MODEL;
142 0 : *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
143 0 : *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
144 :
145 0 : *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
146 0 : *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
147 :
148 0 : *pStoreTo++ = FRM_SUN_COMPONENT_RADIOBUTTON;
149 0 : *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_RADIOBUTTON;
150 0 : *pStoreTo++ = BINDABLE_DATABASE_RADIO_BUTTON;
151 :
152 0 : return aSupported;
153 : }
154 :
155 : //------------------------------------------------------------------------------
156 2 : void ORadioButtonModel::SetSiblingPropsTo(const ::rtl::OUString& rPropName, const Any& rValue)
157 : {
158 : // my name
159 2 : ::rtl::OUString sMyGroup;
160 2 : if (hasProperty(PROPERTY_GROUP_NAME, this))
161 2 : this->getPropertyValue(PROPERTY_GROUP_NAME) >>= sMyGroup;
162 2 : if (sMyGroup.isEmpty())
163 2 : sMyGroup = m_aName;
164 :
165 : // Iterate over my siblings
166 2 : Reference<XIndexAccess> xIndexAccess(getParent(), UNO_QUERY);
167 2 : if (xIndexAccess.is())
168 : {
169 2 : Reference<XPropertySet> xMyProps;
170 2 : query_interface(static_cast<XWeak*>(this), xMyProps);
171 2 : ::rtl::OUString sCurrentGroup;
172 2 : sal_Int32 nNumSiblings = xIndexAccess->getCount();
173 6 : for (sal_Int32 i=0; i<nNumSiblings; ++i)
174 : {
175 4 : Reference<XPropertySet> xSiblingProperties(*(InterfaceRef*)xIndexAccess->getByIndex(i).getValue(), UNO_QUERY);
176 4 : if (!xSiblingProperties.is())
177 0 : continue;
178 4 : if (xMyProps == xSiblingProperties)
179 2 : continue; // do not set myself
180 :
181 : // Only if it's a RadioButton
182 2 : if (!hasProperty(PROPERTY_CLASSID, xSiblingProperties))
183 0 : continue;
184 2 : sal_Int16 nType = 0;
185 2 : xSiblingProperties->getPropertyValue(PROPERTY_CLASSID) >>= nType;
186 2 : if (nType != FormComponentType::RADIOBUTTON)
187 2 : continue;
188 :
189 : // The group association is attached to the name
190 0 : sCurrentGroup = OGroupManager::GetGroupName( xSiblingProperties );
191 0 : if (sCurrentGroup == sMyGroup)
192 0 : xSiblingProperties->setPropertyValue(rPropName, rValue);
193 6 : }
194 2 : }
195 2 : }
196 :
197 : //------------------------------------------------------------------------------
198 9 : void ORadioButtonModel::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const Any& rValue) throw (Exception)
199 : {
200 9 : OReferenceValueComponent::setFastPropertyValue_NoBroadcast( nHandle, rValue );
201 :
202 : // if the label control changed ...
203 9 : if (nHandle == PROPERTY_ID_CONTROLLABEL)
204 : { // ... forward this to our siblings
205 0 : SetSiblingPropsTo(PROPERTY_CONTROLLABEL, rValue);
206 : }
207 :
208 : // If the ControlSource property has changed ...
209 9 : 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 0 : SetSiblingPropsTo(PROPERTY_CONTROLSOURCE, rValue);
212 : }
213 :
214 : // The other way: if my name changes ...
215 9 : if (nHandle == PROPERTY_ID_NAME)
216 : {
217 4 : setControlSource();
218 : }
219 :
220 9 : if (nHandle == PROPERTY_ID_DEFAULT_STATE)
221 : {
222 : sal_Int16 nValue;
223 1 : rValue >>= nValue;
224 1 : 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 1 : Any aZero;
228 1 : nValue = 0;
229 1 : aZero <<= nValue;
230 1 : SetSiblingPropsTo(PROPERTY_DEFAULT_STATE, aZero);
231 : }
232 : }
233 9 : }
234 :
235 8 : void ORadioButtonModel::setControlSource()
236 : {
237 8 : Reference<XIndexAccess> xIndexAccess(getParent(), UNO_QUERY);
238 8 : if (xIndexAccess.is())
239 : {
240 8 : ::rtl::OUString sName, sGroupName;
241 :
242 8 : if (hasProperty(PROPERTY_GROUP_NAME, this))
243 8 : this->getPropertyValue(PROPERTY_GROUP_NAME) >>= sGroupName;
244 8 : this->getPropertyValue(PROPERTY_NAME) >>= sName;
245 :
246 8 : Reference<XPropertySet> xMyProps;
247 8 : query_interface(static_cast<XWeak*>(this), xMyProps);
248 30 : for (sal_Int32 i=0; i<xIndexAccess->getCount(); ++i)
249 : {
250 25 : Reference<XPropertySet> xSiblingProperties(*(InterfaceRef*)xIndexAccess->getByIndex(i).getValue(), UNO_QUERY);
251 25 : if (!xSiblingProperties.is())
252 0 : continue;
253 :
254 25 : if (xMyProps == xSiblingProperties)
255 : // Only if I didn't find myself
256 5 : continue;
257 :
258 20 : sal_Int16 nType = 0;
259 20 : xSiblingProperties->getPropertyValue(PROPERTY_CLASSID) >>= nType;
260 20 : if (nType != FormComponentType::RADIOBUTTON)
261 : // Only RadioButtons
262 8 : continue;
263 :
264 12 : ::rtl::OUString sSiblingName, sSiblingGroupName;
265 12 : if (hasProperty(PROPERTY_GROUP_NAME, xSiblingProperties))
266 12 : xSiblingProperties->getPropertyValue(PROPERTY_GROUP_NAME) >>= sSiblingGroupName;
267 12 : xSiblingProperties->getPropertyValue(PROPERTY_NAME) >>= sSiblingName;
268 :
269 39 : if ((sGroupName.isEmpty() && sSiblingGroupName.isEmpty() && // (no group name
270 6 : sName == sSiblingName) || // names match) or
271 18 : (!sGroupName.isEmpty() && !sSiblingGroupName.isEmpty() && // (have group name
272 3 : sGroupName == sSiblingGroupName)) // they match)
273 : {
274 3 : setPropertyValue(PROPERTY_CONTROLSOURCE, xSiblingProperties->getPropertyValue(PROPERTY_CONTROLSOURCE));
275 : break;
276 : }
277 20 : }
278 8 : }
279 8 : }
280 :
281 : //------------------------------------------------------------------------------
282 4 : void ORadioButtonModel::describeFixedProperties( Sequence< Property >& _rProps ) const
283 : {
284 4 : BEGIN_DESCRIBE_PROPERTIES( 1, OReferenceValueComponent )
285 4 : DECL_PROP1(TABINDEX, sal_Int16, BOUND);
286 : END_DESCRIBE_PROPERTIES();
287 4 : }
288 :
289 : //------------------------------------------------------------------------------
290 0 : ::rtl::OUString SAL_CALL ORadioButtonModel::getServiceName() throw(RuntimeException)
291 : {
292 0 : return FRM_COMPONENT_RADIOBUTTON; // old (non-sun) name for compatibility !
293 : }
294 :
295 : //------------------------------------------------------------------------------
296 0 : void SAL_CALL ORadioButtonModel::write(const Reference<XObjectOutputStream>& _rxOutStream)
297 : throw(IOException, RuntimeException)
298 : {
299 0 : OReferenceValueComponent::write(_rxOutStream);
300 :
301 : // Version
302 0 : _rxOutStream->writeShort(0x0003);
303 :
304 : // Properties
305 0 : _rxOutStream << getReferenceValue();
306 0 : _rxOutStream << (sal_Int16)getDefaultChecked();
307 0 : writeHelpTextCompatibly(_rxOutStream);
308 :
309 : // from version 0x0003 : common properties
310 0 : writeCommonProperties(_rxOutStream);
311 0 : }
312 :
313 : //------------------------------------------------------------------------------
314 0 : void SAL_CALL ORadioButtonModel::read(const Reference<XObjectInputStream>& _rxInStream) throw(IOException, RuntimeException)
315 : {
316 0 : OReferenceValueComponent::read(_rxInStream);
317 0 : ::osl::MutexGuard aGuard(m_aMutex);
318 :
319 : // Version
320 0 : sal_uInt16 nVersion = _rxInStream->readShort();
321 :
322 0 : ::rtl::OUString sReferenceValue;
323 0 : sal_Int16 nDefaultChecked( 0 );
324 0 : switch (nVersion)
325 : {
326 : case 0x0001 :
327 0 : _rxInStream >> sReferenceValue;
328 0 : _rxInStream >> nDefaultChecked;
329 0 : break;
330 : case 0x0002 :
331 0 : _rxInStream >> sReferenceValue;
332 0 : _rxInStream >> nDefaultChecked;
333 0 : readHelpTextCompatibly(_rxInStream);
334 0 : break;
335 : case 0x0003 :
336 0 : _rxInStream >> sReferenceValue;
337 0 : _rxInStream >> nDefaultChecked;
338 0 : readHelpTextCompatibly(_rxInStream);
339 0 : readCommonProperties(_rxInStream);
340 0 : break;
341 : default :
342 : OSL_FAIL("ORadioButtonModel::read : unknown version !");
343 0 : defaultCommonProperties();
344 0 : break;
345 : }
346 :
347 0 : setReferenceValue( sReferenceValue );
348 0 : setDefaultChecked( (ToggleState)nDefaultChecked );
349 :
350 : // Display default values after read
351 0 : if ( !getControlSource().isEmpty() )
352 : // (not if we don't have a control source - the "State" property acts like it is persistent, then
353 0 : resetNoBroadcast();
354 0 : }
355 :
356 : //------------------------------------------------------------------------------
357 5 : void ORadioButtonModel::_propertyChanged(const PropertyChangeEvent& _rEvent) throw(RuntimeException)
358 : {
359 5 : if ( _rEvent.PropertyName.equals( PROPERTY_STATE ) )
360 : {
361 1 : if ( _rEvent.NewValue == (sal_Int16)1 )
362 : {
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 1 : Any aZero;
365 1 : aZero <<= (sal_Int16)0;
366 1 : SetSiblingPropsTo( PROPERTY_STATE, aZero );
367 : }
368 : }
369 4 : else if ( _rEvent.PropertyName.equals( PROPERTY_GROUP_NAME ) )
370 : {
371 4 : setControlSource();
372 : // Can't call OReferenceValueComponent::_propertyChanged(), as it
373 : // doesn't know what to do with the GroupName property.
374 9 : return;
375 : }
376 :
377 1 : OReferenceValueComponent::_propertyChanged( _rEvent );
378 : }
379 :
380 : //------------------------------------------------------------------------------
381 0 : Any ORadioButtonModel::translateDbColumnToControlValue()
382 : {
383 : return makeAny( (sal_Int16)
384 0 : ( ( m_xColumn->getString() == getReferenceValue() ) ? STATE_CHECK : STATE_NOCHECK )
385 0 : );
386 : }
387 :
388 : //------------------------------------------------------------------------------
389 4 : Any ORadioButtonModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
390 : {
391 4 : Any aControlValue = OReferenceValueComponent::translateExternalValueToControlValue( _rExternalValue );
392 4 : sal_Int16 nState = STATE_NOCHECK;
393 4 : if ( ( aControlValue >>= nState ) && ( nState == STATE_DONTKNOW ) )
394 : // radio buttons do not have the DONTKNOW state
395 3 : aControlValue <<= (sal_Int16)STATE_NOCHECK;
396 4 : return aControlValue;
397 : }
398 :
399 : //-----------------------------------------------------------------------------
400 0 : sal_Bool ORadioButtonModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
401 : {
402 0 : Reference< XPropertySet > xField( getField() );
403 : OSL_PRECOND( xField.is(), "ORadioButtonModel::commitControlValueToDbColumn: not bound!" );
404 0 : if ( xField.is() )
405 : {
406 : try
407 : {
408 0 : sal_Int16 nValue = 0;
409 0 : m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) >>= nValue;
410 0 : if ( nValue == 1 )
411 0 : xField->setPropertyValue( PROPERTY_VALUE, makeAny( getReferenceValue() ) );
412 : }
413 0 : catch(const Exception&)
414 : {
415 : OSL_FAIL("ORadioButtonModel::commitControlValueToDbColumn: could not commit !");
416 : }
417 : }
418 0 : return sal_True;
419 : }
420 :
421 : //.........................................................................
422 : }
423 : //.........................................................................
424 :
425 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|