LCOV - code coverage report
Current view: top level - forms/source/component - RadioButton.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 158 189 83.6 %
Date: 2015-06-13 12:38:46 Functions: 20 22 90.9 %
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 <comphelper/basicio.hxx>
      26             : #include <comphelper/processfactory.hxx>
      27             : #include <com/sun/star/container/XIndexAccess.hpp>
      28             : #include <com/sun/star/awt/XVclWindowPeer.hpp>
      29             : 
      30             : namespace frm
      31             : {
      32             : using namespace ::com::sun::star::uno;
      33             : using namespace ::com::sun::star::sdb;
      34             : using namespace ::com::sun::star::sdbc;
      35             : using namespace ::com::sun::star::sdbcx;
      36             : using namespace ::com::sun::star::beans;
      37             : using namespace ::com::sun::star::container;
      38             : using namespace ::com::sun::star::form;
      39             : using namespace ::com::sun::star::awt;
      40             : using namespace ::com::sun::star::io;
      41             : using namespace ::com::sun::star::lang;
      42             : using namespace ::com::sun::star::util;
      43             : using namespace ::com::sun::star::form::binding;
      44             : 
      45             : 
      46           1 : StringSequence SAL_CALL ORadioButtonControl::getSupportedServiceNames() throw(RuntimeException, std::exception)
      47             : {
      48           1 :     StringSequence aSupported = OBoundControl::getSupportedServiceNames();
      49           1 :     aSupported.realloc(aSupported.getLength() + 2);
      50             : 
      51           1 :     OUString* pArray = aSupported.getArray();
      52           1 :     pArray[aSupported.getLength()-2] = FRM_SUN_CONTROL_RADIOBUTTON;
      53           1 :     pArray[aSupported.getLength()-1] = STARDIV_ONE_FORM_CONTROL_RADIOBUTTON;
      54           1 :     return aSupported;
      55             : }
      56             : 
      57             : 
      58             : 
      59          19 : ORadioButtonControl::ORadioButtonControl(const Reference<XComponentContext>& _rxFactory)
      60          19 :                       :OBoundControl(_rxFactory, VCL_CONTROL_RADIOBUTTON)
      61             : {
      62          19 : }
      63             : 
      64             : 
      65          21 : void SAL_CALL ORadioButtonControl::createPeer(const Reference<css::awt::XToolkit>& _rxToolkit, const Reference<css::awt::XWindowPeer>& _rxParent) throw (RuntimeException, std::exception)
      66             : {
      67          21 :     OBoundControl::createPeer(_rxToolkit, _rxParent);
      68             : 
      69             :     // switch off the auto-toggle, we do this ourself ....
      70             :     // (formerly this switch-off was done in the toolkit - but the correct place is here ...)
      71             : //  Reference< XVclWindowPeer >  xVclWindowPeer( getPeer(), UNO_QUERY );
      72             : //  if (xVclWindowPeer.is())
      73             : //      xVclWindowPeer->setProperty(OUString("AutoToggle"), ::cppu::bool2any(sal_False));
      74             :     // new order: do _not_ switch off the auto toggle because:
      75             :     // * today, it is not necessary anymore to handle the toggling ourself (everything works fine without it)
      76             :     // * without auto toggle, the AccessibleEvents as fired by the radio buttons are
      77             :     //     a. newly checked button: "unchecked"->"checked"
      78             :     //     b. previously checked button: "checked"->"unchecked"
      79             :     //   This is deadly for AT-tools, which then get the "unchecked" event _immediately_ after the "checked" event,
      80             :     //   and only read the latter. This makes radio buttons pretty unusable in form documents.
      81             :     //   So we switched AutoToggle _on_, again, because then VCL can handle the notifications, and will send
      82             :     //   them in the proper order.
      83          21 : }
      84             : 
      85             : 
      86          24 : ORadioButtonModel::ORadioButtonModel(const Reference<XComponentContext>& _rxFactory)
      87          24 :     :OReferenceValueComponent( _rxFactory, VCL_CONTROLMODEL_RADIOBUTTON, FRM_SUN_CONTROL_RADIOBUTTON,true )
      88             :                     // use the old control name for compytibility reasons
      89             : {
      90             : 
      91          24 :     m_nClassId = FormComponentType::RADIOBUTTON;
      92          24 :     m_aLabelServiceName = FRM_SUN_COMPONENT_GROUPBOX;
      93          24 :     initValueProperty( PROPERTY_STATE, PROPERTY_ID_STATE );
      94          24 :     startAggregatePropertyListening( PROPERTY_GROUP_NAME );
      95          24 : }
      96             : 
      97             : 
      98           1 : ORadioButtonModel::ORadioButtonModel( const ORadioButtonModel* _pOriginal, const Reference<XComponentContext>& _rxFactory )
      99           1 :     :OReferenceValueComponent( _pOriginal, _rxFactory )
     100             : {
     101           1 : }
     102             : 
     103             : 
     104          44 : ORadioButtonModel::~ORadioButtonModel()
     105             : {
     106          44 : }
     107             : 
     108             : // XCloneable
     109             : 
     110           1 : IMPLEMENT_DEFAULT_CLONING( ORadioButtonModel )
     111             : 
     112             : // XServiceInfo
     113             : 
     114          29 : StringSequence SAL_CALL ORadioButtonModel::getSupportedServiceNames() throw(RuntimeException, std::exception)
     115             : {
     116          29 :     StringSequence aSupported = OReferenceValueComponent::getSupportedServiceNames();
     117             : 
     118          29 :     sal_Int32 nOldLen = aSupported.getLength();
     119          29 :     aSupported.realloc( nOldLen + 9 );
     120          29 :     OUString* pStoreTo = aSupported.getArray() + nOldLen;
     121             : 
     122          29 :     *pStoreTo++ = BINDABLE_CONTROL_MODEL;
     123          29 :     *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
     124          29 :     *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
     125             : 
     126          29 :     *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
     127          29 :     *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
     128             : 
     129          29 :     *pStoreTo++ = FRM_SUN_COMPONENT_RADIOBUTTON;
     130          29 :     *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_RADIOBUTTON;
     131          29 :     *pStoreTo++ = BINDABLE_DATABASE_RADIO_BUTTON;
     132             : 
     133          29 :     *pStoreTo++ = FRM_COMPONENT_RADIOBUTTON;
     134             : 
     135          29 :     return aSupported;
     136             : }
     137             : 
     138             : 
     139          36 : void ORadioButtonModel::SetSiblingPropsTo(const OUString& rPropName, const Any& rValue)
     140             : {
     141             :     // my name
     142          36 :     OUString sMyGroup;
     143          36 :     if (hasProperty(PROPERTY_GROUP_NAME, this))
     144          36 :         this->getPropertyValue(PROPERTY_GROUP_NAME) >>= sMyGroup;
     145          36 :     if (sMyGroup.isEmpty())
     146          19 :         sMyGroup = m_aName;
     147             : 
     148             :     // Iterate over my siblings
     149          72 :     Reference<XIndexAccess> xIndexAccess(getParent(), UNO_QUERY);
     150          36 :     if (xIndexAccess.is())
     151             :     {
     152             :         Reference<XPropertySet> xMyProps(
     153          32 :             static_cast<XWeak*>(this), css::uno::UNO_QUERY);
     154          64 :         OUString sCurrentGroup;
     155          32 :         sal_Int32 nNumSiblings = xIndexAccess->getCount();
     156          98 :         for (sal_Int32 i=0; i<nNumSiblings; ++i)
     157             :         {
     158          66 :             Reference<XPropertySet> xSiblingProperties(*static_cast<InterfaceRef const *>(xIndexAccess->getByIndex(i).getValue()), UNO_QUERY);
     159          66 :             if (!xSiblingProperties.is())
     160           0 :                 continue;
     161          66 :             if (xMyProps == xSiblingProperties)
     162          32 :                 continue;   // do not set myself
     163             : 
     164             :             // Only if it's a RadioButton
     165          34 :             if (!hasProperty(PROPERTY_CLASSID, xSiblingProperties))
     166           0 :                 continue;
     167          34 :             sal_Int16 nType = 0;
     168          34 :             xSiblingProperties->getPropertyValue(PROPERTY_CLASSID) >>= nType;
     169          34 :             if (nType != FormComponentType::RADIOBUTTON)
     170          34 :                 continue;
     171             : 
     172             :             // The group association is attached to the name
     173           0 :             sCurrentGroup = OGroupManager::GetGroupName( xSiblingProperties );
     174           0 :             if (sCurrentGroup == sMyGroup)
     175           0 :                 xSiblingProperties->setPropertyValue(rPropName, rValue);
     176          32 :         }
     177          36 :     }
     178          36 : }
     179             : 
     180             : 
     181         164 : void ORadioButtonModel::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const Any& rValue) throw (Exception, std::exception)
     182             : {
     183         164 :     OReferenceValueComponent::setFastPropertyValue_NoBroadcast( nHandle, rValue );
     184             : 
     185             :     // if the label control changed ...
     186         163 :     if (nHandle == PROPERTY_ID_CONTROLLABEL)
     187             :     {   // ... forward this to our siblings
     188           0 :         SetSiblingPropsTo(PROPERTY_CONTROLLABEL, rValue);
     189             :     }
     190             : 
     191             :     // If the ControlSource property has changed ...
     192         163 :     if (nHandle == PROPERTY_ID_CONTROLSOURCE)
     193             :     {   // ... I have to pass the new ControlSource to my siblings, which are in the same RadioButton group as I am
     194          16 :         SetSiblingPropsTo(PROPERTY_CONTROLSOURCE, rValue);
     195             :     }
     196             : 
     197             :     // The other way: if my name changes ...
     198         163 :     if (nHandle == PROPERTY_ID_NAME)
     199             :     {
     200          38 :         setControlSource();
     201             :     }
     202             : 
     203         163 :     if (nHandle == PROPERTY_ID_DEFAULT_STATE)
     204             :     {
     205             :         sal_Int16 nValue;
     206          12 :         rValue >>= nValue;
     207          12 :         if (1 == nValue)
     208             :         {   // Reset the 'default checked' for all Radios of the same group.
     209             :             // Because (as the Highlander already knew): "There can be only one"
     210           7 :             Any aZero;
     211           7 :             nValue = 0;
     212           7 :             aZero <<= nValue;
     213           7 :             SetSiblingPropsTo(PROPERTY_DEFAULT_STATE, aZero);
     214             :         }
     215             :     }
     216         163 : }
     217             : 
     218          48 : void ORadioButtonModel::setControlSource()
     219             : {
     220          48 :     Reference<XIndexAccess> xIndexAccess(getParent(), UNO_QUERY);
     221          48 :     if (xIndexAccess.is())
     222             :     {
     223          56 :         OUString sName, sGroupName;
     224             : 
     225          28 :         if (hasProperty(PROPERTY_GROUP_NAME, this))
     226          28 :             this->getPropertyValue(PROPERTY_GROUP_NAME) >>= sGroupName;
     227          28 :         this->getPropertyValue(PROPERTY_NAME) >>= sName;
     228             : 
     229             :         Reference<XPropertySet> xMyProps(
     230          56 :             static_cast<XWeak*>(this), css::uno::UNO_QUERY);
     231          90 :         for (sal_Int32 i=0; i<xIndexAccess->getCount(); ++i)
     232             :         {
     233          62 :             Reference<XPropertySet> xSiblingProperties(*static_cast<InterfaceRef const *>(xIndexAccess->getByIndex(i).getValue()), UNO_QUERY);
     234          62 :             if (!xSiblingProperties.is())
     235           0 :                 continue;
     236             : 
     237          62 :             if (xMyProps == xSiblingProperties)
     238             :                 // Only if I didn't find myself
     239          28 :                 continue;
     240             : 
     241          34 :             sal_Int16 nType = 0;
     242          34 :             xSiblingProperties->getPropertyValue(PROPERTY_CLASSID) >>= nType;
     243          34 :             if (nType != FormComponentType::RADIOBUTTON)
     244             :                 // Only RadioButtons
     245          28 :                 continue;
     246             : 
     247          12 :             OUString sSiblingName, sSiblingGroupName;
     248           6 :             if (hasProperty(PROPERTY_GROUP_NAME, xSiblingProperties))
     249           6 :                 xSiblingProperties->getPropertyValue(PROPERTY_GROUP_NAME) >>= sSiblingGroupName;
     250           6 :             xSiblingProperties->getPropertyValue(PROPERTY_NAME) >>= sSiblingName;
     251             : 
     252          24 :             if ((sGroupName.isEmpty() && sSiblingGroupName.isEmpty() &&                 // (no group name
     253          18 :                  sName == sSiblingName) ||                                              //  names match) or
     254           6 :                 (!sGroupName.isEmpty() && !sSiblingGroupName.isEmpty() &&               // (have group name
     255           0 :                  sGroupName == sSiblingGroupName))                                      //  they match)
     256             :             {
     257           0 :                 setPropertyValue(PROPERTY_CONTROLSOURCE, xSiblingProperties->getPropertyValue(PROPERTY_CONTROLSOURCE));
     258           0 :                 break;
     259             :             }
     260          34 :         }
     261          48 :     }
     262          48 : }
     263             : 
     264             : 
     265          26 : void ORadioButtonModel::describeFixedProperties( Sequence< Property >& _rProps ) const
     266             : {
     267          26 :     BEGIN_DESCRIBE_PROPERTIES( 1, OReferenceValueComponent )
     268          26 :         DECL_PROP1(TABINDEX,            sal_Int16,                  BOUND);
     269             :     END_DESCRIBE_PROPERTIES();
     270          26 : }
     271             : 
     272             : 
     273           2 : OUString SAL_CALL ORadioButtonModel::getServiceName() throw(RuntimeException, std::exception)
     274             : {
     275           2 :     return OUString(FRM_COMPONENT_RADIOBUTTON);   // old (non-sun) name for compatibility !
     276             : }
     277             : 
     278             : 
     279           1 : void SAL_CALL ORadioButtonModel::write(const Reference<XObjectOutputStream>& _rxOutStream)
     280             :     throw(IOException, RuntimeException, std::exception)
     281             : {
     282           1 :     OReferenceValueComponent::write(_rxOutStream);
     283             : 
     284             :     // Version
     285           1 :     _rxOutStream->writeShort(0x0003);
     286             : 
     287             :     // Properties
     288           1 :     _rxOutStream << getReferenceValue();
     289           1 :     _rxOutStream << (sal_Int16)getDefaultChecked();
     290           1 :     writeHelpTextCompatibly(_rxOutStream);
     291             : 
     292             :     // from version 0x0003 : common properties
     293           1 :     writeCommonProperties(_rxOutStream);
     294           1 : }
     295             : 
     296             : 
     297           1 : void SAL_CALL ORadioButtonModel::read(const Reference<XObjectInputStream>& _rxInStream) throw(IOException, RuntimeException, std::exception)
     298             : {
     299           1 :     OReferenceValueComponent::read(_rxInStream);
     300           1 :     ::osl::MutexGuard aGuard(m_aMutex);
     301             : 
     302             :     // Version
     303           1 :     sal_uInt16 nVersion = _rxInStream->readShort();
     304             : 
     305           2 :     OUString sReferenceValue;
     306           1 :     sal_Int16 nDefaultChecked( 0 );
     307           1 :     switch (nVersion)
     308             :     {
     309             :         case 0x0001 :
     310           0 :             _rxInStream >> sReferenceValue;
     311           0 :             _rxInStream >> nDefaultChecked;
     312           0 :             break;
     313             :         case 0x0002 :
     314           0 :             _rxInStream >> sReferenceValue;
     315           0 :             _rxInStream >> nDefaultChecked;
     316           0 :             readHelpTextCompatibly(_rxInStream);
     317           0 :             break;
     318             :         case 0x0003 :
     319           1 :             _rxInStream >> sReferenceValue;
     320           1 :             _rxInStream >> nDefaultChecked;
     321           1 :             readHelpTextCompatibly(_rxInStream);
     322           1 :             readCommonProperties(_rxInStream);
     323           1 :             break;
     324             :         default :
     325             :             OSL_FAIL("ORadioButtonModel::read : unknown version !");
     326           0 :             defaultCommonProperties();
     327           0 :             break;
     328             :     }
     329             : 
     330           1 :     setReferenceValue( sReferenceValue );
     331           1 :     setDefaultChecked( (ToggleState)nDefaultChecked );
     332             : 
     333             :     // Display default values after read
     334           1 :     if ( !getControlSource().isEmpty() )
     335             :         // (not if we don't have a control source - the "State" property acts like it is persistent, then
     336           2 :         resetNoBroadcast();
     337           1 : }
     338             : 
     339             : 
     340          35 : void ORadioButtonModel::_propertyChanged(const PropertyChangeEvent& _rEvent) throw(RuntimeException)
     341             : {
     342          35 :     if ( _rEvent.PropertyName == PROPERTY_STATE )
     343             :     {
     344          25 :         if ( _rEvent.NewValue == (sal_Int16)1 )
     345             :         {
     346             :             // If my status has changed to 'checked', I have to reset all my siblings, which are in the same group as I am
     347          13 :             Any aZero;
     348          13 :             aZero <<= (sal_Int16)0;
     349          13 :             SetSiblingPropsTo( PROPERTY_STATE, aZero );
     350             :         }
     351             :     }
     352          10 :     else if ( _rEvent.PropertyName == PROPERTY_GROUP_NAME )
     353             :     {
     354          10 :         setControlSource();
     355             :         // Can't call OReferenceValueComponent::_propertyChanged(), as it
     356             :         // doesn't know what to do with the GroupName property.
     357          45 :         return;
     358             :     }
     359             : 
     360          25 :     OReferenceValueComponent::_propertyChanged( _rEvent );
     361             : }
     362             : 
     363             : 
     364           0 : Any ORadioButtonModel::translateDbColumnToControlValue()
     365             : {
     366             :     return makeAny( (sal_Int16)
     367           0 :         ( ( m_xColumn->getString() == getReferenceValue() ) ? TRISTATE_TRUE : TRISTATE_FALSE )
     368           0 :     );
     369             : }
     370             : 
     371             : 
     372           3 : Any ORadioButtonModel::translateExternalValueToControlValue( const Any& _rExternalValue ) const
     373             : {
     374           3 :     Any aControlValue = OReferenceValueComponent::translateExternalValueToControlValue( _rExternalValue );
     375           3 :     sal_Int16 nState = TRISTATE_FALSE;
     376           3 :     if ( ( aControlValue >>= nState ) && ( nState == TRISTATE_INDET ) )
     377             :         // radio buttons do not have the DONTKNOW state
     378           2 :         aControlValue <<= (sal_Int16)TRISTATE_FALSE;
     379           3 :     return aControlValue;
     380             : }
     381             : 
     382             : 
     383           0 : bool ORadioButtonModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
     384             : {
     385           0 :     Reference< XPropertySet > xField( getField() );
     386             :     OSL_PRECOND( xField.is(), "ORadioButtonModel::commitControlValueToDbColumn: not bound!" );
     387           0 :     if ( xField.is() )
     388             :     {
     389             :         try
     390             :         {
     391           0 :             sal_Int16 nValue = 0;
     392           0 :             m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) >>= nValue;
     393           0 :             if ( nValue == 1 )
     394           0 :                 xField->setPropertyValue( PROPERTY_VALUE, makeAny( getReferenceValue() ) );
     395             :         }
     396           0 :         catch(const Exception&)
     397             :         {
     398             :             OSL_FAIL("ORadioButtonModel::commitControlValueToDbColumn: could not commit !");
     399             :         }
     400             :     }
     401           0 :     return true;
     402             : }
     403             : 
     404             : }
     405             : 
     406             : extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
     407          24 : com_sun_star_form_ORadioButtonModel_get_implementation(::com::sun::star::uno::XComponentContext* component,
     408             :         ::com::sun::star::uno::Sequence<css::uno::Any> const &)
     409             : {
     410          24 :     return cppu::acquire(new frm::ORadioButtonModel(component));
     411             : }
     412             : 
     413             : extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
     414          19 : com_sun_star_form_ORadioButtonControl_get_implementation(::com::sun::star::uno::XComponentContext* component,
     415             :         ::com::sun::star::uno::Sequence<css::uno::Any> const &)
     416             : {
     417          19 :     return cppu::acquire(new frm::ORadioButtonControl(component));
     418             : }
     419             : 
     420             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11