LCOV - code coverage report
Current view: top level - libreoffice/toolkit/source/controls - formattedcontrol.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 208 0.0 %
Date: 2012-12-17 Functions: 0 26 0.0 %
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           0 :         ::osl::Mutex& getDefaultFormatsMutex()
      48             :         {
      49           0 :             static ::osl::Mutex s_aDefaultFormatsMutex;
      50           0 :             return s_aDefaultFormatsMutex;
      51             :         }
      52             : 
      53             :         // ...............................................................
      54           0 :         Reference< XNumberFormatsSupplier >& lcl_getDefaultFormatsAccess_nothrow()
      55             :         {
      56           0 :             static Reference< XNumberFormatsSupplier > s_xDefaultFormats;
      57           0 :             return s_xDefaultFormats;
      58             :         }
      59             : 
      60             :         // ...............................................................
      61           0 :         bool& lcl_getTriedCreation()
      62             :         {
      63             :             static bool s_bTriedCreation = false;
      64           0 :             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           0 :         void    lcl_registerDefaultFormatsClient()
      90             :         {
      91           0 :             osl_atomic_increment( &s_refCount );
      92           0 :         }
      93             : 
      94             :         // ...............................................................
      95           0 :         void    lcl_revokeDefaultFormatsClient()
      96             :         {
      97           0 :             ::osl::ClearableMutexGuard aGuard( getDefaultFormatsMutex() );
      98           0 :             if ( 0 == osl_atomic_decrement( &s_refCount ) )
      99             :             {
     100           0 :                 Reference< XNumberFormatsSupplier >& rDefaultFormats( lcl_getDefaultFormatsAccess_nothrow() );
     101           0 :                 Reference< XNumberFormatsSupplier > xReleasePotentialLastReference( rDefaultFormats );
     102           0 :                 rDefaultFormats.clear();
     103           0 :                 lcl_getTriedCreation() = false;
     104             : 
     105           0 :                 aGuard.clear();
     106           0 :                 xReleasePotentialLastReference.clear();
     107           0 :             }
     108           0 :         }
     109             :     }
     110             : 
     111             :     // ===================================================================
     112             :     // = UnoControlFormattedFieldModel
     113             :     // ===================================================================
     114             :     // -------------------------------------------------------------------
     115           0 :     UnoControlFormattedFieldModel::UnoControlFormattedFieldModel( const Reference< XMultiServiceFactory >& i_factory )
     116             :         :UnoControlModel( i_factory )
     117             :         ,m_bRevokedAsClient( false )
     118           0 :         ,m_bSettingValueAndText( false )
     119             :     {
     120           0 :         ImplRegisterProperty( BASEPROPERTY_ALIGN );
     121           0 :         ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR );
     122           0 :         ImplRegisterProperty( BASEPROPERTY_BORDER );
     123           0 :         ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR );
     124           0 :         ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL );
     125           0 :         ImplRegisterProperty( BASEPROPERTY_EFFECTIVE_DEFAULT );
     126           0 :         ImplRegisterProperty( BASEPROPERTY_EFFECTIVE_VALUE );
     127           0 :         ImplRegisterProperty( BASEPROPERTY_EFFECTIVE_MAX );
     128           0 :         ImplRegisterProperty( BASEPROPERTY_EFFECTIVE_MIN );
     129           0 :         ImplRegisterProperty( BASEPROPERTY_ENABLED );
     130           0 :         ImplRegisterProperty( BASEPROPERTY_ENABLEVISIBLE );
     131           0 :         ImplRegisterProperty( BASEPROPERTY_FONTDESCRIPTOR );
     132           0 :         ImplRegisterProperty( BASEPROPERTY_FORMATKEY );
     133           0 :         ImplRegisterProperty( BASEPROPERTY_FORMATSSUPPLIER );
     134           0 :         ImplRegisterProperty( BASEPROPERTY_HELPTEXT );
     135           0 :         ImplRegisterProperty( BASEPROPERTY_HELPURL );
     136           0 :         ImplRegisterProperty( BASEPROPERTY_MAXTEXTLEN );
     137           0 :         ImplRegisterProperty( BASEPROPERTY_PRINTABLE );
     138           0 :         ImplRegisterProperty( BASEPROPERTY_REPEAT );
     139           0 :         ImplRegisterProperty( BASEPROPERTY_REPEAT_DELAY );
     140           0 :         ImplRegisterProperty( BASEPROPERTY_READONLY );
     141           0 :         ImplRegisterProperty( BASEPROPERTY_SPIN );
     142           0 :         ImplRegisterProperty( BASEPROPERTY_STRICTFORMAT );
     143           0 :         ImplRegisterProperty( BASEPROPERTY_TABSTOP );
     144           0 :         ImplRegisterProperty( BASEPROPERTY_TEXT );
     145           0 :         ImplRegisterProperty( BASEPROPERTY_TEXTCOLOR );
     146           0 :         ImplRegisterProperty( BASEPROPERTY_HIDEINACTIVESELECTION );
     147           0 :         ImplRegisterProperty( BASEPROPERTY_ENFORCE_FORMAT );
     148           0 :         ImplRegisterProperty( BASEPROPERTY_VERTICALALIGN );
     149           0 :         ImplRegisterProperty( BASEPROPERTY_WRITING_MODE );
     150           0 :         ImplRegisterProperty( BASEPROPERTY_CONTEXT_WRITING_MODE );
     151           0 :         ImplRegisterProperty( BASEPROPERTY_MOUSE_WHEEL_BEHAVIOUR );
     152             : 
     153           0 :         Any aTreatAsNumber;
     154           0 :         aTreatAsNumber <<= (sal_Bool) sal_True;
     155           0 :         ImplRegisterProperty( BASEPROPERTY_TREATASNUMBER, aTreatAsNumber );
     156             : 
     157           0 :         lcl_registerDefaultFormatsClient();
     158           0 :     }
     159             : 
     160             :     // -------------------------------------------------------------------
     161           0 :     UnoControlFormattedFieldModel::~UnoControlFormattedFieldModel()
     162             :     {
     163           0 :     }
     164             : 
     165             :     // -------------------------------------------------------------------
     166           0 :     ::rtl::OUString UnoControlFormattedFieldModel::getServiceName() throw(RuntimeException)
     167             :     {
     168           0 :         return ::rtl::OUString::createFromAscii( szServiceName_UnoControlFormattedFieldModel );
     169             :     }
     170             : 
     171             :     // -------------------------------------------------------------------
     172           0 :     void SAL_CALL UnoControlFormattedFieldModel::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw (Exception)
     173             :     {
     174           0 :         UnoControlModel::setFastPropertyValue_NoBroadcast( nHandle, rValue );
     175             : 
     176           0 :         switch ( nHandle )
     177             :         {
     178             :         case BASEPROPERTY_EFFECTIVE_VALUE:
     179           0 :             if ( !m_bSettingValueAndText )
     180           0 :                 impl_updateTextFromValue_nothrow();
     181           0 :             break;
     182             :         case BASEPROPERTY_FORMATSSUPPLIER:
     183           0 :             impl_updateCachedFormatter_nothrow();
     184           0 :             impl_updateTextFromValue_nothrow();
     185           0 :             break;
     186             :         case BASEPROPERTY_FORMATKEY:
     187           0 :             impl_updateCachedFormatKey_nothrow();
     188           0 :             impl_updateTextFromValue_nothrow();
     189           0 :             break;
     190             :         }
     191           0 :     }
     192             : 
     193             :     // -------------------------------------------------------------------
     194           0 :     void UnoControlFormattedFieldModel::impl_updateTextFromValue_nothrow()
     195             :     {
     196           0 :         if ( !m_xCachedFormatter.is() )
     197           0 :             impl_updateCachedFormatter_nothrow();
     198           0 :         if ( !m_xCachedFormatter.is() )
     199           0 :             return;
     200             : 
     201             :         try
     202             :         {
     203           0 :             Any aEffectiveValue;
     204           0 :             getFastPropertyValue( aEffectiveValue, BASEPROPERTY_EFFECTIVE_VALUE );
     205             : 
     206           0 :             ::rtl::OUString sStringValue;
     207           0 :             if ( !( aEffectiveValue >>= sStringValue ) )
     208             :             {
     209           0 :                 double nDoubleValue(0);
     210           0 :                 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           0 :             Reference< XPropertySet > xThis( *this, UNO_QUERY );
     220           0 :             xThis->setPropertyValue( GetPropertyName( BASEPROPERTY_TEXT ), makeAny( sStringValue ) );
     221             :         }
     222           0 :         catch( const Exception& )
     223             :         {
     224             :             DBG_UNHANDLED_EXCEPTION();
     225             :         }
     226             :     }
     227             : 
     228             :     // -------------------------------------------------------------------
     229           0 :     void UnoControlFormattedFieldModel::impl_updateCachedFormatter_nothrow()
     230             :     {
     231           0 :         Any aFormatsSupplier;
     232           0 :         getFastPropertyValue( aFormatsSupplier, BASEPROPERTY_FORMATSSUPPLIER );
     233             :         try
     234             :         {
     235           0 :             Reference< XNumberFormatsSupplier > xSupplier( aFormatsSupplier, UNO_QUERY );
     236           0 :             if ( !xSupplier.is() )
     237           0 :                 xSupplier = lcl_getDefaultFormats_throw();
     238             : 
     239           0 :             if ( !m_xCachedFormatter.is() )
     240             :             {
     241             :                 m_xCachedFormatter = Reference< XNumberFormatter >(
     242             :                     NumberFormatter::create(::comphelper::getProcessComponentContext()),
     243             :                     UNO_QUERY_THROW
     244           0 :                 );
     245             :             }
     246           0 :             m_xCachedFormatter->attachNumberFormatsSupplier( xSupplier );
     247             :         }
     248           0 :         catch( const Exception& )
     249             :         {
     250             :             DBG_UNHANDLED_EXCEPTION();
     251           0 :         }
     252           0 :     }
     253             : 
     254             :     // -------------------------------------------------------------------
     255           0 :     void UnoControlFormattedFieldModel::impl_updateCachedFormatKey_nothrow()
     256             :     {
     257           0 :         Any aFormatKey;
     258           0 :         getFastPropertyValue( aFormatKey, BASEPROPERTY_FORMATKEY );
     259           0 :         m_aCachedFormat = aFormatKey;
     260           0 :     }
     261             : 
     262             :     // -------------------------------------------------------------------
     263           0 :     void UnoControlFormattedFieldModel::dispose(  ) throw(RuntimeException)
     264             :     {
     265           0 :         UnoControlModel::dispose();
     266             : 
     267           0 :         ::osl::MutexGuard aGuard( GetMutex() );
     268           0 :         if ( !m_bRevokedAsClient )
     269             :         {
     270           0 :             lcl_revokeDefaultFormatsClient();
     271           0 :             m_bRevokedAsClient = true;
     272           0 :         }
     273           0 :     }
     274             : 
     275             :     // -------------------------------------------------------------------
     276           0 :     void UnoControlFormattedFieldModel::ImplNormalizePropertySequence( const sal_Int32 _nCount, sal_Int32* _pHandles,
     277             :         Any* _pValues, sal_Int32* _pValidHandles ) const SAL_THROW(())
     278             :     {
     279           0 :         ImplEnsureHandleOrder( _nCount, _pHandles, _pValues, BASEPROPERTY_EFFECTIVE_VALUE, BASEPROPERTY_TEXT );
     280             : 
     281           0 :         UnoControlModel::ImplNormalizePropertySequence( _nCount, _pHandles, _pValues, _pValidHandles );
     282           0 :     }
     283             : 
     284             :     // -------------------------------------------------------------------
     285             :     namespace
     286             :     {
     287             :         class ResetFlagOnExit
     288             :         {
     289             :         private:
     290             :             bool&   m_rFlag;
     291             : 
     292             :         public:
     293           0 :             ResetFlagOnExit( bool& _rFlag )
     294           0 :                 :m_rFlag( _rFlag )
     295             :             {
     296           0 :             }
     297           0 :             ~ResetFlagOnExit()
     298             :             {
     299           0 :                 m_rFlag = false;
     300           0 :             }
     301             :         };
     302             :     }
     303             : 
     304             :     // -------------------------------------------------------------------
     305           0 :     void SAL_CALL UnoControlFormattedFieldModel::setPropertyValues( const Sequence< ::rtl::OUString >& _rPropertyNames, const Sequence< Any >& _rValues ) throw(PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
     306             :     {
     307           0 :         bool bSettingValue = false;
     308           0 :         bool bSettingText = false;
     309           0 :         for (   const ::rtl::OUString* pPropertyNames = _rPropertyNames.getConstArray();
     310           0 :                 pPropertyNames != _rPropertyNames.getConstArray() + _rPropertyNames.getLength();
     311             :                 ++pPropertyNames
     312             :             )
     313             :         {
     314           0 :             if ( BASEPROPERTY_EFFECTIVE_VALUE == GetPropertyId( *pPropertyNames ) )
     315           0 :                 bSettingValue = true;
     316             : 
     317           0 :             if ( BASEPROPERTY_TEXT == GetPropertyId( *pPropertyNames ) )
     318           0 :                 bSettingText = true;
     319             :         }
     320             : 
     321           0 :         m_bSettingValueAndText = ( bSettingValue && bSettingText );
     322           0 :         ResetFlagOnExit aResetFlag( m_bSettingValueAndText );
     323           0 :         UnoControlModel::setPropertyValues( _rPropertyNames, _rValues );
     324           0 :     }
     325             : 
     326             :     // -------------------------------------------------------------------
     327           0 :     sal_Bool UnoControlFormattedFieldModel::convertFastPropertyValue(
     328             :                 Any& rConvertedValue, Any& rOldValue, sal_Int32 nPropId,
     329             :                 const Any& rValue ) throw (IllegalArgumentException)
     330             :     {
     331           0 :         if ( BASEPROPERTY_EFFECTIVE_DEFAULT == nPropId && rValue.hasValue() )
     332             :         {
     333           0 :             double dVal = 0;
     334           0 :             ::rtl::OUString sVal;
     335           0 :             sal_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             :                         ( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Unable to convert the given value for the property "))
     366           0 :                     +=  GetPropertyName((sal_uInt16)nPropId) )
     367           0 :                     +=  ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" (double, integer, or string expected).")),
     368             :                 static_cast< XPropertySet* >(this),
     369           0 :                 1);
     370             :         }
     371             : 
     372           0 :         return UnoControlModel::convertFastPropertyValue( rConvertedValue, rOldValue, nPropId, rValue );
     373             :     }
     374             : 
     375             :     // -------------------------------------------------------------------
     376           0 :     Any UnoControlFormattedFieldModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
     377             :     {
     378           0 :         Any aReturn;
     379           0 :         switch (nPropId)
     380             :         {
     381           0 :             case BASEPROPERTY_DEFAULTCONTROL: aReturn <<= ::rtl::OUString( ::rtl::OUString::createFromAscii( szServiceName_UnoControlFormattedField ) ); break;
     382             : 
     383           0 :             case BASEPROPERTY_TREATASNUMBER: aReturn <<= (sal_Bool)sal_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           0 :                 break;
     393             : 
     394           0 :             default : aReturn = UnoControlModel::ImplGetDefaultValue( nPropId ); break;
     395             :         }
     396             : 
     397           0 :         return aReturn;
     398             :     }
     399             : 
     400             :     // -------------------------------------------------------------------
     401           0 :     ::cppu::IPropertyArrayHelper& UnoControlFormattedFieldModel::getInfoHelper()
     402             :     {
     403             :         static UnoPropertyArrayHelper* pHelper = NULL;
     404           0 :         if ( !pHelper )
     405             :         {
     406           0 :             Sequence<sal_Int32> aIDs = ImplGetPropertyIds();
     407           0 :             pHelper = new UnoPropertyArrayHelper( aIDs );
     408             :         }
     409           0 :         return *pHelper;
     410             :     }
     411             : 
     412             :     // beans::XMultiPropertySet
     413             :     // -------------------------------------------------------------------
     414           0 :     Reference< XPropertySetInfo > UnoControlFormattedFieldModel::getPropertySetInfo(  ) throw(RuntimeException)
     415             :     {
     416           0 :         static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
     417           0 :         return xInfo;
     418             :     }
     419             : 
     420             :     // ===================================================================
     421             :     // = UnoFormattedFieldControl
     422             :     // ===================================================================
     423             :     // -------------------------------------------------------------------
     424           0 :     UnoFormattedFieldControl::UnoFormattedFieldControl( const Reference< XMultiServiceFactory >& i_factory )
     425           0 :         :UnoSpinFieldControl( i_factory )
     426             :     {
     427           0 :     }
     428             : 
     429             :     // -------------------------------------------------------------------
     430           0 :     ::rtl::OUString UnoFormattedFieldControl::GetComponentServiceName()
     431             :     {
     432           0 :         return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FormattedField"));
     433             :     }
     434             : 
     435             :     // -------------------------------------------------------------------
     436           0 :     void UnoFormattedFieldControl::textChanged(const TextEvent& e) throw(RuntimeException)
     437             :     {
     438           0 :         Reference< XVclWindowPeer >  xPeer(getPeer(), UNO_QUERY);
     439             :         OSL_ENSURE(xPeer.is(), "UnoFormattedFieldControl::textChanged : what kind of peer do I have ?");
     440             : 
     441           0 :         Sequence< ::rtl::OUString > aNames( 2 );
     442           0 :         aNames[0] = GetPropertyName( BASEPROPERTY_EFFECTIVE_VALUE );
     443           0 :         aNames[1] = GetPropertyName( BASEPROPERTY_TEXT );
     444             : 
     445           0 :         Sequence< Any > aValues( 2 );
     446           0 :         aValues[0] = xPeer->getProperty( aNames[0] );
     447           0 :         aValues[1] = xPeer->getProperty( aNames[1] );
     448             : 
     449           0 :         ImplSetPropertyValues( aNames, aValues, sal_False );
     450             : 
     451           0 :         if ( GetTextListeners().getLength() )
     452           0 :             GetTextListeners().textChanged( e );
     453           0 :     }
     454             : 
     455             : //........................................................................
     456             : }   // namespace toolkit
     457             : //........................................................................
     458             : 
     459             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10