LCOV - code coverage report
Current view: top level - forms/source/component - RadioButton.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 152 189 80.4 %
Date: 2014-04-11 Functions: 19 22 86.4 %
Legend: Lines: hit not hit

          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          16 : InterfaceRef SAL_CALL ORadioButtonControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory) throw (RuntimeException)
      50             : {
      51          16 :     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          16 : ORadioButtonControl::ORadioButtonControl(const Reference<XComponentContext>& _rxFactory)
      68          16 :                       :OBoundControl(_rxFactory, VCL_CONTROL_RADIOBUTTON)
      69             : {
      70          16 : }
      71             : 
      72             : 
      73          19 : void SAL_CALL ORadioButtonControl::createPeer(const Reference<starawt::XToolkit>& _rxToolkit, const Reference<starawt::XWindowPeer>& _rxParent) throw (RuntimeException, std::exception)
      74             : {
      75          19 :     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          19 : }
      92             : 
      93             : 
      94          21 : InterfaceRef SAL_CALL ORadioButtonModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory) throw (RuntimeException)
      95             : {
      96          21 :     return *(new ORadioButtonModel( comphelper::getComponentContext(_rxFactory) ));
      97             : }
      98             : 
      99             : 
     100             : 
     101          21 : ORadioButtonModel::ORadioButtonModel(const Reference<XComponentContext>& _rxFactory)
     102          21 :     :OReferenceValueComponent( _rxFactory, VCL_CONTROLMODEL_RADIOBUTTON, FRM_SUN_CONTROL_RADIOBUTTON,sal_True )
     103             :                     // use the old control name for compytibility reasons
     104             : {
     105             : 
     106          21 :     m_nClassId = FormComponentType::RADIOBUTTON;
     107          21 :     m_aLabelServiceName = FRM_SUN_COMPONENT_GROUPBOX;
     108          21 :     initValueProperty( PROPERTY_STATE, PROPERTY_ID_STATE );
     109          21 :     startAggregatePropertyListening( PROPERTY_GROUP_NAME );
     110          21 : }
     111             : 
     112             : 
     113           1 : ORadioButtonModel::ORadioButtonModel( const ORadioButtonModel* _pOriginal, const Reference<XComponentContext>& _rxFactory )
     114           1 :     :OReferenceValueComponent( _pOriginal, _rxFactory )
     115             : {
     116           1 : }
     117             : 
     118             : 
     119          38 : ORadioButtonModel::~ORadioButtonModel()
     120             : {
     121          38 : }
     122             : 
     123             : // XCloneable
     124             : 
     125           1 : IMPLEMENT_DEFAULT_CLONING( ORadioButtonModel )
     126             : 
     127             : // XServiceInfo
     128             : 
     129          28 : StringSequence SAL_CALL ORadioButtonModel::getSupportedServiceNames() throw(RuntimeException, std::exception)
     130             : {
     131          28 :     StringSequence aSupported = OReferenceValueComponent::getSupportedServiceNames();
     132             : 
     133          28 :     sal_Int32 nOldLen = aSupported.getLength();
     134          28 :     aSupported.realloc( nOldLen + 8 );
     135          28 :     OUString* pStoreTo = aSupported.getArray() + nOldLen;
     136             : 
     137          28 :     *pStoreTo++ = BINDABLE_CONTROL_MODEL;
     138          28 :     *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
     139          28 :     *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
     140             : 
     141          28 :     *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
     142          28 :     *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
     143             : 
     144          28 :     *pStoreTo++ = FRM_SUN_COMPONENT_RADIOBUTTON;
     145          28 :     *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_RADIOBUTTON;
     146          28 :     *pStoreTo++ = BINDABLE_DATABASE_RADIO_BUTTON;
     147             : 
     148          28 :     return aSupported;
     149             : }
     150             : 
     151             : 
     152          30 : void ORadioButtonModel::SetSiblingPropsTo(const OUString& rPropName, const Any& rValue)
     153             : {
     154             :     // my name
     155          30 :     OUString sMyGroup;
     156          30 :     if (hasProperty(PROPERTY_GROUP_NAME, this))
     157          30 :         this->getPropertyValue(PROPERTY_GROUP_NAME) >>= sMyGroup;
     158          30 :     if (sMyGroup.isEmpty())
     159          17 :         sMyGroup = m_aName;
     160             : 
     161             :     // Iterate over my siblings
     162          60 :     Reference<XIndexAccess> xIndexAccess(getParent(), UNO_QUERY);
     163          30 :     if (xIndexAccess.is())
     164             :     {
     165          28 :         Reference<XPropertySet> xMyProps;
     166          28 :         query_interface(static_cast<XWeak*>(this), xMyProps);
     167          56 :         OUString sCurrentGroup;
     168          28 :         sal_Int32 nNumSiblings = xIndexAccess->getCount();
     169          86 :         for (sal_Int32 i=0; i<nNumSiblings; ++i)
     170             :         {
     171          58 :             Reference<XPropertySet> xSiblingProperties(*(InterfaceRef*)xIndexAccess->getByIndex(i).getValue(), UNO_QUERY);
     172          58 :             if (!xSiblingProperties.is())
     173           0 :                 continue;
     174          58 :             if (xMyProps == xSiblingProperties)
     175          28 :                 continue;   // do not set myself
     176             : 
     177             :             // Only if it's a RadioButton
     178          30 :             if (!hasProperty(PROPERTY_CLASSID, xSiblingProperties))
     179           0 :                 continue;
     180          30 :             sal_Int16 nType = 0;
     181          30 :             xSiblingProperties->getPropertyValue(PROPERTY_CLASSID) >>= nType;
     182          30 :             if (nType != FormComponentType::RADIOBUTTON)
     183          30 :                 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          28 :         }
     190          30 :     }
     191          30 : }
     192             : 
     193             : 
     194         158 : void ORadioButtonModel::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const Any& rValue) throw (Exception, std::exception)
     195             : {
     196         158 :     OReferenceValueComponent::setFastPropertyValue_NoBroadcast( nHandle, rValue );
     197             : 
     198             :     // if the label control changed ...
     199         158 :     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         158 :     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          16 :         SetSiblingPropsTo(PROPERTY_CONTROLSOURCE, rValue);
     208             :     }
     209             : 
     210             :     // The other way: if my name changes ...
     211         158 :     if (nHandle == PROPERTY_ID_NAME)
     212             :     {
     213          34 :         setControlSource();
     214             :     }
     215             : 
     216         158 :     if (nHandle == PROPERTY_ID_DEFAULT_STATE)
     217             :     {
     218             :         sal_Int16 nValue;
     219          12 :         rValue >>= nValue;
     220          12 :         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           4 :             Any aZero;
     224           4 :             nValue = 0;
     225           4 :             aZero <<= nValue;
     226           4 :             SetSiblingPropsTo(PROPERTY_DEFAULT_STATE, aZero);
     227             :         }
     228             :     }
     229         158 : }
     230             : 
     231          44 : void ORadioButtonModel::setControlSource()
     232             : {
     233          44 :     Reference<XIndexAccess> xIndexAccess(getParent(), UNO_QUERY);
     234          44 :     if (xIndexAccess.is())
     235             :     {
     236          56 :         OUString sName, sGroupName;
     237             : 
     238          28 :         if (hasProperty(PROPERTY_GROUP_NAME, this))
     239          28 :             this->getPropertyValue(PROPERTY_GROUP_NAME) >>= sGroupName;
     240          28 :         this->getPropertyValue(PROPERTY_NAME) >>= sName;
     241             : 
     242          56 :         Reference<XPropertySet> xMyProps;
     243          28 :         query_interface(static_cast<XWeak*>(this), xMyProps);
     244          90 :         for (sal_Int32 i=0; i<xIndexAccess->getCount(); ++i)
     245             :         {
     246          62 :             Reference<XPropertySet> xSiblingProperties(*(InterfaceRef*)xIndexAccess->getByIndex(i).getValue(), UNO_QUERY);
     247          62 :             if (!xSiblingProperties.is())
     248           0 :                 continue;
     249             : 
     250          62 :             if (xMyProps == xSiblingProperties)
     251             :                 // Only if I didn't find myself
     252          28 :                 continue;
     253             : 
     254          34 :             sal_Int16 nType = 0;
     255          34 :             xSiblingProperties->getPropertyValue(PROPERTY_CLASSID) >>= nType;
     256          34 :             if (nType != FormComponentType::RADIOBUTTON)
     257             :                 // Only RadioButtons
     258          28 :                 continue;
     259             : 
     260          12 :             OUString sSiblingName, sSiblingGroupName;
     261           6 :             if (hasProperty(PROPERTY_GROUP_NAME, xSiblingProperties))
     262           6 :                 xSiblingProperties->getPropertyValue(PROPERTY_GROUP_NAME) >>= sSiblingGroupName;
     263           6 :             xSiblingProperties->getPropertyValue(PROPERTY_NAME) >>= sSiblingName;
     264             : 
     265          24 :             if ((sGroupName.isEmpty() && sSiblingGroupName.isEmpty() &&                 // (no group name
     266          18 :                  sName == sSiblingName) ||                                              //  names match) or
     267           6 :                 (!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          34 :         }
     274          44 :     }
     275          44 : }
     276             : 
     277             : 
     278          23 : void ORadioButtonModel::describeFixedProperties( Sequence< Property >& _rProps ) const
     279             : {
     280          23 :     BEGIN_DESCRIBE_PROPERTIES( 1, OReferenceValueComponent )
     281          23 :         DECL_PROP1(TABINDEX,            sal_Int16,                  BOUND);
     282             :     END_DESCRIBE_PROPERTIES();
     283          23 : }
     284             : 
     285             : 
     286           2 : OUString SAL_CALL ORadioButtonModel::getServiceName() throw(RuntimeException, std::exception)
     287             : {
     288           2 :     return OUString(FRM_COMPONENT_RADIOBUTTON);   // old (non-sun) name for compatibility !
     289             : }
     290             : 
     291             : 
     292           1 : void SAL_CALL ORadioButtonModel::write(const Reference<XObjectOutputStream>& _rxOutStream)
     293             :     throw(IOException, RuntimeException, std::exception)
     294             : {
     295           1 :     OReferenceValueComponent::write(_rxOutStream);
     296             : 
     297             :     // Version
     298           1 :     _rxOutStream->writeShort(0x0003);
     299             : 
     300             :     // Properties
     301           1 :     _rxOutStream << getReferenceValue();
     302           1 :     _rxOutStream << (sal_Int16)getDefaultChecked();
     303           1 :     writeHelpTextCompatibly(_rxOutStream);
     304             : 
     305             :     // from version 0x0003 : common properties
     306           1 :     writeCommonProperties(_rxOutStream);
     307           1 : }
     308             : 
     309             : 
     310           1 : void SAL_CALL ORadioButtonModel::read(const Reference<XObjectInputStream>& _rxInStream) throw(IOException, RuntimeException, std::exception)
     311             : {
     312           1 :     OReferenceValueComponent::read(_rxInStream);
     313           1 :     ::osl::MutexGuard aGuard(m_aMutex);
     314             : 
     315             :     // Version
     316           1 :     sal_uInt16 nVersion = _rxInStream->readShort();
     317             : 
     318           2 :     OUString sReferenceValue;
     319           1 :     sal_Int16 nDefaultChecked( 0 );
     320           1 :     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           1 :             _rxInStream >> sReferenceValue;
     333           1 :             _rxInStream >> nDefaultChecked;
     334           1 :             readHelpTextCompatibly(_rxInStream);
     335           1 :             readCommonProperties(_rxInStream);
     336           1 :             break;
     337             :         default :
     338             :             OSL_FAIL("ORadioButtonModel::read : unknown version !");
     339           0 :             defaultCommonProperties();
     340           0 :             break;
     341             :     }
     342             : 
     343           1 :     setReferenceValue( sReferenceValue );
     344           1 :     setDefaultChecked( (ToggleState)nDefaultChecked );
     345             : 
     346             :     // Display default values after read
     347           1 :     if ( !getControlSource().isEmpty() )
     348             :         // (not if we don't have a control source - the "State" property acts like it is persistent, then
     349           2 :         resetNoBroadcast();
     350           1 : }
     351             : 
     352             : 
     353          33 : void ORadioButtonModel::_propertyChanged(const PropertyChangeEvent& _rEvent) throw(RuntimeException)
     354             : {
     355          33 :     if ( _rEvent.PropertyName.equals( PROPERTY_STATE ) )
     356             :     {
     357          23 :         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          10 :             Any aZero;
     361          10 :             aZero <<= (sal_Int16)0;
     362          10 :             SetSiblingPropsTo( PROPERTY_STATE, aZero );
     363             :         }
     364             :     }
     365          10 :     else if ( _rEvent.PropertyName.equals( PROPERTY_GROUP_NAME ) )
     366             :     {
     367          10 :         setControlSource();
     368             :         // Can't call OReferenceValueComponent::_propertyChanged(), as it
     369             :         // doesn't know what to do with the GroupName property.
     370          43 :         return;
     371             :     }
     372             : 
     373          23 :     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           3 : Any ORadioButtonModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
     386             : {
     387           3 :     Any aControlValue = OReferenceValueComponent::translateExternalValueToControlValue( _rExternalValue );
     388           3 :     sal_Int16 nState = TRISTATE_FALSE;
     389           3 :     if ( ( aControlValue >>= nState ) && ( nState == TRISTATE_INDET ) )
     390             :         // radio buttons do not have the DONTKNOW state
     391           2 :         aControlValue <<= (sal_Int16)TRISTATE_FALSE;
     392           3 :     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: */

Generated by: LCOV version 1.10