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