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

Generated by: LCOV version 1.10