LCOV - code coverage report
Current view: top level - toolkit/source/controls - formattedcontrol.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 176 213 82.6 %
Date: 2014-04-11 Functions: 27 28 96.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 <toolkit/controls/formattedcontrol.hxx>
      21             : #include <toolkit/helper/unopropertyarrayhelper.hxx>
      22             : #include <toolkit/helper/property.hxx>
      23             : 
      24             : #include <com/sun/star/awt/XVclWindowPeer.hpp>
      25             : #include <com/sun/star/util/NumberFormatter.hpp>
      26             : #include <com/sun/star/util/NumberFormatsSupplier.hpp>
      27             : 
      28             : #include <tools/diagnose_ex.h>
      29             : #include <comphelper/processfactory.hxx>
      30             : #include <osl/diagnose.h>
      31             : 
      32             : 
      33             : namespace toolkit
      34             : {
      35             : 
      36             : 
      37             :     using namespace ::com::sun::star::uno;
      38             :     using namespace ::com::sun::star::awt;
      39             :     using namespace ::com::sun::star::lang;
      40             :     using namespace ::com::sun::star::beans;
      41             :     using namespace ::com::sun::star::util;
      42             : 
      43             : 
      44             :     namespace
      45             :     {
      46             : 
      47          16 :         ::osl::Mutex& getDefaultFormatsMutex()
      48             :         {
      49          16 :             static ::osl::Mutex s_aDefaultFormatsMutex;
      50          16 :             return s_aDefaultFormatsMutex;
      51             :         }
      52             : 
      53             : 
      54           2 :         Reference< XNumberFormatsSupplier >& lcl_getDefaultFormatsAccess_nothrow()
      55             :         {
      56           2 :             static Reference< XNumberFormatsSupplier > s_xDefaultFormats;
      57           2 :             return s_xDefaultFormats;
      58             :         }
      59             : 
      60             : 
      61           2 :         bool& lcl_getTriedCreation()
      62             :         {
      63             :             static bool s_bTriedCreation = false;
      64           2 :             return s_bTriedCreation;
      65             :         }
      66             : 
      67             : 
      68           0 :         const Reference< XNumberFormatsSupplier >& lcl_getDefaultFormats_throw()
      69             :         {
      70           0 :             ::osl::MutexGuard aGuard( getDefaultFormatsMutex() );
      71             : 
      72           0 :             bool& rbTriedCreation = lcl_getTriedCreation();
      73           0 :             Reference< XNumberFormatsSupplier >& rDefaultFormats( lcl_getDefaultFormatsAccess_nothrow() );
      74           0 :             if ( !rDefaultFormats.is() && !rbTriedCreation )
      75             :             {
      76           0 :                 rbTriedCreation = true;
      77           0 :                 rDefaultFormats = NumberFormatsSupplier::createWithDefaultLocale( ::comphelper::getProcessComponentContext() );
      78             :             }
      79           0 :             if ( !rDefaultFormats.is() )
      80           0 :                 throw RuntimeException();
      81             : 
      82           0 :             return rDefaultFormats;
      83             :         }
      84             : 
      85             : 
      86             :         static oslInterlockedCount  s_refCount(0);
      87             : 
      88             : 
      89          19 :         void    lcl_registerDefaultFormatsClient()
      90             :         {
      91          19 :             osl_atomic_increment( &s_refCount );
      92          19 :         }
      93             : 
      94             : 
      95          16 :         void    lcl_revokeDefaultFormatsClient()
      96             :         {
      97          16 :             ::osl::ClearableMutexGuard aGuard( getDefaultFormatsMutex() );
      98          16 :             if ( 0 == osl_atomic_decrement( &s_refCount ) )
      99             :             {
     100           2 :                 Reference< XNumberFormatsSupplier >& rDefaultFormats( lcl_getDefaultFormatsAccess_nothrow() );
     101           2 :                 Reference< XNumberFormatsSupplier > xReleasePotentialLastReference( rDefaultFormats );
     102           2 :                 rDefaultFormats.clear();
     103           2 :                 lcl_getTriedCreation() = false;
     104             : 
     105           2 :                 aGuard.clear();
     106           2 :                 xReleasePotentialLastReference.clear();
     107          16 :             }
     108          16 :         }
     109             :     }
     110             : 
     111             : 
     112             :     // = UnoControlFormattedFieldModel
     113             : 
     114             : 
     115          19 :     UnoControlFormattedFieldModel::UnoControlFormattedFieldModel( const Reference< XComponentContext >& rxContext )
     116             :         :UnoControlModel( rxContext )
     117             :         ,m_bRevokedAsClient( false )
     118          19 :         ,m_bSettingValueAndText( false )
     119             :     {
     120          19 :         ImplRegisterProperty( BASEPROPERTY_ALIGN );
     121          19 :         ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR );
     122          19 :         ImplRegisterProperty( BASEPROPERTY_BORDER );
     123          19 :         ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR );
     124          19 :         ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL );
     125          19 :         ImplRegisterProperty( BASEPROPERTY_EFFECTIVE_DEFAULT );
     126          19 :         ImplRegisterProperty( BASEPROPERTY_EFFECTIVE_VALUE );
     127          19 :         ImplRegisterProperty( BASEPROPERTY_EFFECTIVE_MAX );
     128          19 :         ImplRegisterProperty( BASEPROPERTY_EFFECTIVE_MIN );
     129          19 :         ImplRegisterProperty( BASEPROPERTY_ENABLED );
     130          19 :         ImplRegisterProperty( BASEPROPERTY_ENABLEVISIBLE );
     131          19 :         ImplRegisterProperty( BASEPROPERTY_FONTDESCRIPTOR );
     132          19 :         ImplRegisterProperty( BASEPROPERTY_FORMATKEY );
     133          19 :         ImplRegisterProperty( BASEPROPERTY_FORMATSSUPPLIER );
     134          19 :         ImplRegisterProperty( BASEPROPERTY_HELPTEXT );
     135          19 :         ImplRegisterProperty( BASEPROPERTY_HELPURL );
     136          19 :         ImplRegisterProperty( BASEPROPERTY_MAXTEXTLEN );
     137          19 :         ImplRegisterProperty( BASEPROPERTY_PRINTABLE );
     138          19 :         ImplRegisterProperty( BASEPROPERTY_REPEAT );
     139          19 :         ImplRegisterProperty( BASEPROPERTY_REPEAT_DELAY );
     140          19 :         ImplRegisterProperty( BASEPROPERTY_READONLY );
     141          19 :         ImplRegisterProperty( BASEPROPERTY_SPIN );
     142          19 :         ImplRegisterProperty( BASEPROPERTY_STRICTFORMAT );
     143          19 :         ImplRegisterProperty( BASEPROPERTY_TABSTOP );
     144          19 :         ImplRegisterProperty( BASEPROPERTY_TEXT );
     145          19 :         ImplRegisterProperty( BASEPROPERTY_TEXTCOLOR );
     146          19 :         ImplRegisterProperty( BASEPROPERTY_HIDEINACTIVESELECTION );
     147          19 :         ImplRegisterProperty( BASEPROPERTY_ENFORCE_FORMAT );
     148          19 :         ImplRegisterProperty( BASEPROPERTY_VERTICALALIGN );
     149          19 :         ImplRegisterProperty( BASEPROPERTY_WRITING_MODE );
     150          19 :         ImplRegisterProperty( BASEPROPERTY_CONTEXT_WRITING_MODE );
     151          19 :         ImplRegisterProperty( BASEPROPERTY_MOUSE_WHEEL_BEHAVIOUR );
     152             : 
     153          19 :         Any aTreatAsNumber;
     154          19 :         aTreatAsNumber <<= true;
     155          19 :         ImplRegisterProperty( BASEPROPERTY_TREATASNUMBER, aTreatAsNumber );
     156             : 
     157          19 :         lcl_registerDefaultFormatsClient();
     158          19 :     }
     159             : 
     160             : 
     161          40 :     UnoControlFormattedFieldModel::~UnoControlFormattedFieldModel()
     162             :     {
     163          40 :     }
     164             : 
     165             : 
     166           1 :     OUString UnoControlFormattedFieldModel::getServiceName() throw(RuntimeException, std::exception)
     167             :     {
     168           1 :         return OUString::createFromAscii( szServiceName_UnoControlFormattedFieldModel );
     169             :     }
     170             : 
     171             : 
     172         327 :     void SAL_CALL UnoControlFormattedFieldModel::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw (Exception, std::exception)
     173             :     {
     174         327 :         UnoControlModel::setFastPropertyValue_NoBroadcast( nHandle, rValue );
     175             : 
     176         327 :         switch ( nHandle )
     177             :         {
     178             :         case BASEPROPERTY_EFFECTIVE_VALUE:
     179           8 :             if ( !m_bSettingValueAndText )
     180           5 :                 impl_updateTextFromValue_nothrow();
     181           8 :             break;
     182             :         case BASEPROPERTY_FORMATSSUPPLIER:
     183          29 :             impl_updateCachedFormatter_nothrow();
     184          29 :             impl_updateTextFromValue_nothrow();
     185          29 :             break;
     186             :         case BASEPROPERTY_FORMATKEY:
     187          14 :             impl_updateCachedFormatKey_nothrow();
     188          14 :             impl_updateTextFromValue_nothrow();
     189          14 :             break;
     190             :         }
     191         327 :     }
     192             : 
     193             : 
     194          48 :     void UnoControlFormattedFieldModel::impl_updateTextFromValue_nothrow()
     195             :     {
     196          48 :         if ( !m_xCachedFormatter.is() )
     197           0 :             impl_updateCachedFormatter_nothrow();
     198          48 :         if ( !m_xCachedFormatter.is() )
     199          48 :             return;
     200             : 
     201             :         try
     202             :         {
     203          48 :             Any aEffectiveValue;
     204          48 :             getFastPropertyValue( aEffectiveValue, BASEPROPERTY_EFFECTIVE_VALUE );
     205             : 
     206          96 :             OUString sStringValue;
     207          48 :             if ( !( aEffectiveValue >>= sStringValue ) )
     208             :             {
     209          43 :                 double nDoubleValue(0);
     210          43 :                 if ( aEffectiveValue >>= nDoubleValue )
     211             :                 {
     212           0 :                     sal_Int32 nFormatKey( 0 );
     213           0 :                     if ( m_aCachedFormat.hasValue() )
     214           0 :                         m_aCachedFormat >>= nFormatKey;
     215           0 :                     sStringValue = m_xCachedFormatter->convertNumberToString( nFormatKey, nDoubleValue );
     216             :                 }
     217             :             }
     218             : 
     219          96 :             Reference< XPropertySet > xThis( *this, UNO_QUERY );
     220          96 :             xThis->setPropertyValue( GetPropertyName( BASEPROPERTY_TEXT ), makeAny( sStringValue ) );
     221             :         }
     222           0 :         catch( const Exception& )
     223             :         {
     224             :             DBG_UNHANDLED_EXCEPTION();
     225             :         }
     226             :     }
     227             : 
     228             : 
     229          29 :     void UnoControlFormattedFieldModel::impl_updateCachedFormatter_nothrow()
     230             :     {
     231          29 :         Any aFormatsSupplier;
     232          29 :         getFastPropertyValue( aFormatsSupplier, BASEPROPERTY_FORMATSSUPPLIER );
     233             :         try
     234             :         {
     235          29 :             Reference< XNumberFormatsSupplier > xSupplier( aFormatsSupplier, UNO_QUERY );
     236          29 :             if ( !xSupplier.is() )
     237           0 :                 xSupplier = lcl_getDefaultFormats_throw();
     238             : 
     239          29 :             if ( !m_xCachedFormatter.is() )
     240             :             {
     241          30 :                 m_xCachedFormatter = Reference< XNumberFormatter >(
     242             :                     NumberFormatter::create(::comphelper::getProcessComponentContext()),
     243             :                     UNO_QUERY_THROW
     244          15 :                 );
     245             :             }
     246          29 :             m_xCachedFormatter->attachNumberFormatsSupplier( xSupplier );
     247             :         }
     248           0 :         catch( const Exception& )
     249             :         {
     250             :             DBG_UNHANDLED_EXCEPTION();
     251          29 :         }
     252          29 :     }
     253             : 
     254             : 
     255          14 :     void UnoControlFormattedFieldModel::impl_updateCachedFormatKey_nothrow()
     256             :     {
     257          14 :         Any aFormatKey;
     258          14 :         getFastPropertyValue( aFormatKey, BASEPROPERTY_FORMATKEY );
     259          14 :         m_aCachedFormat = aFormatKey;
     260          14 :     }
     261             : 
     262             : 
     263          16 :     void UnoControlFormattedFieldModel::dispose(  ) throw(RuntimeException, std::exception)
     264             :     {
     265          16 :         UnoControlModel::dispose();
     266             : 
     267          16 :         ::osl::MutexGuard aGuard( GetMutex() );
     268          16 :         if ( !m_bRevokedAsClient )
     269             :         {
     270          16 :             lcl_revokeDefaultFormatsClient();
     271          16 :             m_bRevokedAsClient = true;
     272          16 :         }
     273          16 :     }
     274             : 
     275             : 
     276          45 :     void UnoControlFormattedFieldModel::ImplNormalizePropertySequence( const sal_Int32 _nCount, sal_Int32* _pHandles,
     277             :         Any* _pValues, sal_Int32* _pValidHandles ) const SAL_THROW(())
     278             :     {
     279          45 :         ImplEnsureHandleOrder( _nCount, _pHandles, _pValues, BASEPROPERTY_EFFECTIVE_VALUE, BASEPROPERTY_TEXT );
     280             : 
     281          45 :         UnoControlModel::ImplNormalizePropertySequence( _nCount, _pHandles, _pValues, _pValidHandles );
     282          45 :     }
     283             : 
     284             : 
     285             :     namespace
     286             :     {
     287             :         class ResetFlagOnExit
     288             :         {
     289             :         private:
     290             :             bool&   m_rFlag;
     291             : 
     292             :         public:
     293          46 :             ResetFlagOnExit( bool& _rFlag )
     294          46 :                 :m_rFlag( _rFlag )
     295             :             {
     296          46 :             }
     297          46 :             ~ResetFlagOnExit()
     298             :             {
     299          46 :                 m_rFlag = false;
     300          46 :             }
     301             :         };
     302             :     }
     303             : 
     304             : 
     305          46 :     void SAL_CALL UnoControlFormattedFieldModel::setPropertyValues( const Sequence< OUString >& _rPropertyNames, const Sequence< Any >& _rValues ) throw(PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException, std::exception)
     306             :     {
     307          46 :         bool bSettingValue = false;
     308          46 :         bool bSettingText = false;
     309        3080 :         for (   const OUString* pPropertyNames = _rPropertyNames.getConstArray();
     310        1540 :                 pPropertyNames != _rPropertyNames.getConstArray() + _rPropertyNames.getLength();
     311             :                 ++pPropertyNames
     312             :             )
     313             :         {
     314        1494 :             if ( BASEPROPERTY_EFFECTIVE_VALUE == GetPropertyId( *pPropertyNames ) )
     315           6 :                 bSettingValue = true;
     316             : 
     317        1494 :             if ( BASEPROPERTY_TEXT == GetPropertyId( *pPropertyNames ) )
     318          45 :                 bSettingText = true;
     319             :         }
     320             : 
     321          46 :         m_bSettingValueAndText = ( bSettingValue && bSettingText );
     322          46 :         ResetFlagOnExit aResetFlag( m_bSettingValueAndText );
     323          46 :         UnoControlModel::setPropertyValues( _rPropertyNames, _rValues );
     324          46 :     }
     325             : 
     326             : 
     327        1183 :     sal_Bool UnoControlFormattedFieldModel::convertFastPropertyValue(
     328             :                 Any& rConvertedValue, Any& rOldValue, sal_Int32 nPropId,
     329             :                 const Any& rValue ) throw (IllegalArgumentException)
     330             :     {
     331        1183 :         if ( BASEPROPERTY_EFFECTIVE_DEFAULT == nPropId && rValue.hasValue() )
     332             :         {
     333           0 :             double dVal = 0;
     334           0 :             OUString sVal;
     335           0 :             bool bStreamed = (rValue >>= dVal);
     336           0 :             if ( bStreamed )
     337             :             {
     338           0 :                 rConvertedValue <<= dVal;
     339             :             }
     340             :             else
     341             :             {
     342           0 :                 sal_Int32  nVal = 0;
     343           0 :                 bStreamed = (rValue >>= nVal);
     344           0 :                 if ( bStreamed )
     345             :                 {
     346           0 :                     rConvertedValue <<= static_cast<double>(nVal);
     347             :                 }
     348             :                 else
     349             :                 {
     350           0 :                     bStreamed = (rValue >>= sVal);
     351           0 :                     if ( bStreamed )
     352             :                     {
     353           0 :                         rConvertedValue <<= sVal;
     354             :                     }
     355             :                 }
     356             :             }
     357             : 
     358           0 :             if ( bStreamed )
     359             :             {
     360           0 :                 getFastPropertyValue( rOldValue, nPropId );
     361           0 :                 return !CompareProperties( rConvertedValue, rOldValue );
     362             :             }
     363             : 
     364             :             throw IllegalArgumentException(
     365             :                         ( OUString("Unable to convert the given value for the property ")
     366           0 :                     +=  GetPropertyName((sal_uInt16)nPropId) )
     367           0 :                     += " (double, integer, or string expected).",
     368             :                 static_cast< XPropertySet* >(this),
     369           0 :                 1);
     370             :         }
     371             : 
     372        1183 :         return UnoControlModel::convertFastPropertyValue( rConvertedValue, rOldValue, nPropId, rValue );
     373             :     }
     374             : 
     375             : 
     376         716 :     Any UnoControlFormattedFieldModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
     377             :     {
     378         716 :         Any aReturn;
     379         716 :         switch (nPropId)
     380             :         {
     381          20 :             case BASEPROPERTY_DEFAULTCONTROL: aReturn <<= OUString( OUString::createFromAscii( szServiceName_UnoControlFormattedField ) ); break;
     382             : 
     383           0 :             case BASEPROPERTY_TREATASNUMBER: aReturn <<= true; break;
     384             : 
     385             :             case BASEPROPERTY_EFFECTIVE_DEFAULT:
     386             :             case BASEPROPERTY_EFFECTIVE_VALUE:
     387             :             case BASEPROPERTY_EFFECTIVE_MAX:
     388             :             case BASEPROPERTY_EFFECTIVE_MIN:
     389             :             case BASEPROPERTY_FORMATKEY:
     390             :             case BASEPROPERTY_FORMATSSUPPLIER:
     391             :                 // (void)
     392         118 :                 break;
     393             : 
     394         578 :             default : aReturn = UnoControlModel::ImplGetDefaultValue( nPropId ); break;
     395             :         }
     396             : 
     397         716 :         return aReturn;
     398             :     }
     399             : 
     400             : 
     401        2213 :     ::cppu::IPropertyArrayHelper& UnoControlFormattedFieldModel::getInfoHelper()
     402             :     {
     403             :         static UnoPropertyArrayHelper* pHelper = NULL;
     404        2213 :         if ( !pHelper )
     405             :         {
     406           3 :             Sequence<sal_Int32> aIDs = ImplGetPropertyIds();
     407           3 :             pHelper = new UnoPropertyArrayHelper( aIDs );
     408             :         }
     409        2213 :         return *pHelper;
     410             :     }
     411             : 
     412             :     // beans::XMultiPropertySet
     413             : 
     414          91 :     Reference< XPropertySetInfo > UnoControlFormattedFieldModel::getPropertySetInfo(  ) throw(RuntimeException, std::exception)
     415             :     {
     416          91 :         static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
     417          91 :         return xInfo;
     418             :     }
     419             : 
     420             : 
     421             :     // = UnoFormattedFieldControl
     422             : 
     423             : 
     424           1 :     UnoFormattedFieldControl::UnoFormattedFieldControl()
     425           1 :         :UnoSpinFieldControl()
     426             :     {
     427           1 :     }
     428             : 
     429             : 
     430           1 :     OUString UnoFormattedFieldControl::GetComponentServiceName()
     431             :     {
     432           1 :         return OUString("FormattedField");
     433             :     }
     434             : 
     435             : 
     436           6 :     void UnoFormattedFieldControl::textChanged(const TextEvent& e) throw(RuntimeException, std::exception)
     437             :     {
     438           6 :         Reference< XVclWindowPeer >  xPeer(getPeer(), UNO_QUERY);
     439             :         OSL_ENSURE(xPeer.is(), "UnoFormattedFieldControl::textChanged : what kind of peer do I have ?");
     440             : 
     441          12 :         Sequence< OUString > aNames( 2 );
     442           6 :         aNames[0] = GetPropertyName( BASEPROPERTY_EFFECTIVE_VALUE );
     443           6 :         aNames[1] = GetPropertyName( BASEPROPERTY_TEXT );
     444             : 
     445          12 :         Sequence< Any > aValues( 2 );
     446           6 :         aValues[0] = xPeer->getProperty( aNames[0] );
     447           6 :         aValues[1] = xPeer->getProperty( aNames[1] );
     448             : 
     449           6 :         ImplSetPropertyValues( aNames, aValues, false );
     450             : 
     451           6 :         if ( GetTextListeners().getLength() )
     452          12 :             GetTextListeners().textChanged( e );
     453           6 :     }
     454             : 
     455             : 
     456             : }   // namespace toolkit
     457             : 
     458             : 
     459             : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
     460          18 : stardiv_Toolkit_UnoControlFormattedFieldModel_get_implementation(
     461             :     css::uno::XComponentContext *context,
     462             :     css::uno::Sequence<css::uno::Any> const &)
     463             : {
     464          18 :     return cppu::acquire(new toolkit::UnoControlFormattedFieldModel(context));
     465             : }
     466             : 
     467             : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
     468           1 : stardiv_Toolkit_UnoFormattedFieldControl_get_implementation(
     469             :     css::uno::XComponentContext *,
     470             :     css::uno::Sequence<css::uno::Any> const &)
     471             : {
     472           1 :     return cppu::acquire(new toolkit::UnoFormattedFieldControl());
     473             : }
     474             : 
     475             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10