Files
libreoffice/toolkit/source/controls/formattedcontrol.cxx

466 lines
18 KiB
C++
Raw Normal View History

/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2008 by Sun Microsystems, Inc.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* $RCSfile: formattedcontrol.cxx,v $
* $Revision: 1.14 $
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_toolkit.hxx"
#include <toolkit/controls/formattedcontrol.hxx>
#include <toolkit/helper/unopropertyarrayhelper.hxx>
#include <toolkit/helper/property.hxx>
#include <com/sun/star/awt/XVclWindowPeer.hpp>
#include <com/sun/star/util/XNumberFormatsSupplier.hpp>
#include <tools/diagnose_ex.h>
#include <comphelper/processfactory.hxx>
#include <osl/diagnose.h>
//........................................................................
namespace toolkit
{
//........................................................................
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::awt;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::util;
// -------------------------------------------------------------------
namespace
{
// ...............................................................
::osl::Mutex& getDefaultFormatsMutex()
{
static ::osl::Mutex s_aDefaultFormatsMutex;
return s_aDefaultFormatsMutex;
}
// ...............................................................
Reference< XNumberFormatsSupplier >& lcl_getDefaultFormatsAccess_nothrow()
{
static Reference< XNumberFormatsSupplier > s_xDefaultFormats;
return s_xDefaultFormats;
}
// ...............................................................
bool& lcl_getTriedCreation()
{
static bool s_bTriedCreation = false;
return s_bTriedCreation;
}
// ...............................................................
const Reference< XNumberFormatsSupplier >& lcl_getDefaultFormats_throw()
{
::osl::MutexGuard aGuard( getDefaultFormatsMutex() );
bool& rbTriedCreation = lcl_getTriedCreation();
Reference< XNumberFormatsSupplier >& rDefaultFormats( lcl_getDefaultFormatsAccess_nothrow() );
if ( !rDefaultFormats.is() && !rbTriedCreation )
{
rbTriedCreation = true;
rDefaultFormats = Reference< XNumberFormatsSupplier >(
::comphelper::createProcessComponent(
::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.NumberFormatsSupplier" ) ) ),
UNO_QUERY_THROW
);
}
if ( !rDefaultFormats.is() )
throw RuntimeException();
return rDefaultFormats;
}
// ...............................................................
static oslInterlockedCount s_refCount(0);
// ...............................................................
void lcl_registerDefaultFormatsClient()
{
osl_incrementInterlockedCount( &s_refCount );
}
// ...............................................................
void lcl_revokeDefaultFormatsClient()
{
::osl::ClearableMutexGuard aGuard( getDefaultFormatsMutex() );
if ( 0 == osl_decrementInterlockedCount( &s_refCount ) )
{
Reference< XNumberFormatsSupplier >& rDefaultFormats( lcl_getDefaultFormatsAccess_nothrow() );
Reference< XNumberFormatsSupplier > xReleasePotentialLastReference( rDefaultFormats );
rDefaultFormats.clear();
lcl_getTriedCreation() = false;
aGuard.clear();
xReleasePotentialLastReference.clear();
}
}
}
// ===================================================================
// = UnoControlFormattedFieldModel
// ===================================================================
// -------------------------------------------------------------------
UnoControlFormattedFieldModel::UnoControlFormattedFieldModel()
:m_bRevokedAsClient( false )
,m_bSettingValueAndText( false )
{
ImplRegisterProperty( BASEPROPERTY_ALIGN );
ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR );
ImplRegisterProperty( BASEPROPERTY_BORDER );
ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR );
ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL );
ImplRegisterProperty( BASEPROPERTY_EFFECTIVE_DEFAULT );
ImplRegisterProperty( BASEPROPERTY_EFFECTIVE_VALUE );
ImplRegisterProperty( BASEPROPERTY_EFFECTIVE_MAX );
ImplRegisterProperty( BASEPROPERTY_EFFECTIVE_MIN );
ImplRegisterProperty( BASEPROPERTY_ENABLED );
ImplRegisterProperty( BASEPROPERTY_FONTDESCRIPTOR );
ImplRegisterProperty( BASEPROPERTY_FORMATKEY );
ImplRegisterProperty( BASEPROPERTY_FORMATSSUPPLIER );
ImplRegisterProperty( BASEPROPERTY_HELPTEXT );
ImplRegisterProperty( BASEPROPERTY_HELPURL );
ImplRegisterProperty( BASEPROPERTY_MAXTEXTLEN );
ImplRegisterProperty( BASEPROPERTY_PRINTABLE );
ImplRegisterProperty( BASEPROPERTY_REPEAT );
ImplRegisterProperty( BASEPROPERTY_REPEAT_DELAY );
ImplRegisterProperty( BASEPROPERTY_READONLY );
ImplRegisterProperty( BASEPROPERTY_SPIN );
ImplRegisterProperty( BASEPROPERTY_STRICTFORMAT );
ImplRegisterProperty( BASEPROPERTY_TABSTOP );
ImplRegisterProperty( BASEPROPERTY_TEXT );
ImplRegisterProperty( BASEPROPERTY_TEXTCOLOR );
ImplRegisterProperty( BASEPROPERTY_HIDEINACTIVESELECTION );
ImplRegisterProperty( BASEPROPERTY_ENFORCE_FORMAT );
CWS-TOOLING: integrate CWS rtlcontrols 2008-12-11 21:08:49 +0100 fs r265367 : CONTEXT_WRITING_MODE is transient 2008-12-11 21:08:00 +0100 fs r265365 : REGISTER_PROP_3 2008-12-11 20:53:44 +0100 fs r265362 : ContextWritingMode is not MAYBEVOID 2008-12-11 15:29:08 +0100 fs r265315 : prevent a deadlock during complex.dbaccess.DatabaseDocument test 2008-12-11 15:01:13 +0100 fs r265304 : manual RESYNC to m37 2008-12-10 20:04:38 +0100 pl r265230 : #i30631# fix a snafu in mirroring 2008-12-10 19:14:45 +0100 pl r265229 : #i30631# rework PaintToDevice for RTL controls 2008-12-05 10:19:13 +0100 fs r264893 : #i10000# ImplInitSettings => ImplInitWindow (ImplInitSettings clashed with base classes ImplInitSettings on unxsols4) 2008-12-03 12:55:24 +0100 fs r264768 : #i100000# 2008-12-03 07:11:48 +0100 fs r264741 : #i10000# 2008-12-02 10:37:51 +0100 fs r264670 : CWS-TOOLING: rebase CWS rtlcontrols to trunk@264325 (milestone: DEV300:m36) 2008-12-02 09:27:50 +0100 fs r264660 : merge from trunk 2008-11-25 10:28:36 +0100 ama r264277 : Fix #i94572# 2008-11-24 11:46:48 +0100 fs r264218 : #i30631# proper context writing mode 2008-11-24 09:38:04 +0100 fs r264204 : #i30631# (approved by PL) 2008-11-24 09:35:47 +0100 fs r264203 : #i30631# Context/WritingMode 2008-11-24 09:33:36 +0100 fs r264202 : #i30631# Context/WritingMode 2008-11-24 09:31:53 +0100 fs r264200 : #i30631# RTL 2008-11-19 08:51:48 +0100 fs r263963 : #i10000# 2008-11-18 20:58:11 +0100 fs r263878 : #i10000# 2008-11-18 15:30:44 +0100 fs r263778 : migrate the CWS from CVS to SVN the CVS changes contained in this change set are the ones between the following two CVS tags: CWS_DEV300_RTLCONTROLS_ANCHOR CWS_DEV300_RTLCONTROLS_PRE_MIGRATION 2008-11-18 12:29:04 +0100 ama r263762 : Fix #i94572#: Context direction for drawing objects 2008-11-18 12:25:50 +0100 ama r263761 : Fix #i94572#: Context direction for drawing objects 2008-11-18 12:02:30 +0100 ama r263759 : Fix #i94572#: Context direction for drawing objects
2008-12-16 13:30:53 +00:00
ImplRegisterProperty( BASEPROPERTY_WRITING_MODE );
ImplRegisterProperty( BASEPROPERTY_CONTEXT_WRITING_MODE );
Any aTreatAsNumber;
aTreatAsNumber <<= (sal_Bool) sal_True;
ImplRegisterProperty( BASEPROPERTY_TREATASNUMBER, aTreatAsNumber );
lcl_registerDefaultFormatsClient();
}
// -------------------------------------------------------------------
UnoControlFormattedFieldModel::~UnoControlFormattedFieldModel()
{
}
// -------------------------------------------------------------------
::rtl::OUString UnoControlFormattedFieldModel::getServiceName() throw(RuntimeException)
{
return ::rtl::OUString::createFromAscii( szServiceName_UnoControlFormattedFieldModel );
}
// -------------------------------------------------------------------
void SAL_CALL UnoControlFormattedFieldModel::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw (Exception)
{
UnoControlModel::setFastPropertyValue_NoBroadcast( nHandle, rValue );
switch ( nHandle )
{
case BASEPROPERTY_EFFECTIVE_VALUE:
if ( !m_bSettingValueAndText )
impl_updateTextFromValue_nothrow();
break;
case BASEPROPERTY_FORMATSSUPPLIER:
impl_updateCachedFormatter_nothrow();
impl_updateTextFromValue_nothrow();
break;
case BASEPROPERTY_FORMATKEY:
impl_updateCachedFormatKey_nothrow();
impl_updateTextFromValue_nothrow();
break;
}
}
// -------------------------------------------------------------------
void UnoControlFormattedFieldModel::impl_updateTextFromValue_nothrow()
{
if ( !m_xCachedFormatter.is() )
impl_updateCachedFormatter_nothrow();
if ( !m_xCachedFormatter.is() )
return;
try
{
Any aEffectiveValue;
getFastPropertyValue( aEffectiveValue, BASEPROPERTY_EFFECTIVE_VALUE );
::rtl::OUString sStringValue;
if ( !( aEffectiveValue >>= sStringValue ) )
{
double nDoubleValue(0);
if ( aEffectiveValue >>= nDoubleValue )
{
sal_Int32 nFormatKey( 0 );
if ( m_aCachedFormat.hasValue() )
m_aCachedFormat >>= nFormatKey;
sStringValue = m_xCachedFormatter->convertNumberToString( nFormatKey, nDoubleValue );
}
}
Reference< XPropertySet > xThis( *this, UNO_QUERY );
xThis->setPropertyValue( GetPropertyName( BASEPROPERTY_TEXT ), makeAny( sStringValue ) );
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION();
}
}
// -------------------------------------------------------------------
void UnoControlFormattedFieldModel::impl_updateCachedFormatter_nothrow()
{
Any aFormatsSupplier;
getFastPropertyValue( aFormatsSupplier, BASEPROPERTY_FORMATSSUPPLIER );
try
{
Reference< XNumberFormatsSupplier > xSupplier( aFormatsSupplier, UNO_QUERY );
if ( !xSupplier.is() )
xSupplier = lcl_getDefaultFormats_throw();
if ( !m_xCachedFormatter.is() )
{
m_xCachedFormatter = Reference< XNumberFormatter >(
::comphelper::createProcessComponent( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.NumberFormatter" ) ) ),
UNO_QUERY_THROW
);
}
m_xCachedFormatter->attachNumberFormatsSupplier( xSupplier );
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION();
}
}
// -------------------------------------------------------------------
void UnoControlFormattedFieldModel::impl_updateCachedFormatKey_nothrow()
{
Any aFormatKey;
getFastPropertyValue( aFormatKey, BASEPROPERTY_FORMATKEY );
m_aCachedFormat = aFormatKey;
}
// -------------------------------------------------------------------
void UnoControlFormattedFieldModel::dispose( ) throw(RuntimeException)
{
UnoControlModel::dispose();
::osl::MutexGuard aGuard( GetMutex() );
if ( !m_bRevokedAsClient )
{
lcl_revokeDefaultFormatsClient();
m_bRevokedAsClient = true;
}
}
// -------------------------------------------------------------------
void UnoControlFormattedFieldModel::ImplNormalizePropertySequence( const sal_Int32 _nCount, sal_Int32* _pHandles,
Any* _pValues, sal_Int32* _pValidHandles ) const SAL_THROW(())
{
ImplEnsureHandleOrder( _nCount, _pHandles, _pValues, BASEPROPERTY_EFFECTIVE_VALUE, BASEPROPERTY_TEXT );
UnoControlModel::ImplNormalizePropertySequence( _nCount, _pHandles, _pValues, _pValidHandles );
}
// -------------------------------------------------------------------
namespace
{
class ResetFlagOnExit
{
private:
bool& m_rFlag;
public:
ResetFlagOnExit( bool& _rFlag )
:m_rFlag( _rFlag )
{
}
~ResetFlagOnExit()
{
m_rFlag = false;
}
};
}
// -------------------------------------------------------------------
void SAL_CALL UnoControlFormattedFieldModel::setPropertyValues( const Sequence< ::rtl::OUString >& _rPropertyNames, const Sequence< Any >& _rValues ) throw(PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
{
bool bSettingValue = false;
bool bSettingText = false;
for ( const ::rtl::OUString* pPropertyNames = _rPropertyNames.getConstArray();
pPropertyNames != _rPropertyNames.getConstArray() + _rPropertyNames.getLength();
++pPropertyNames
)
{
bSettingValue = ( BASEPROPERTY_EFFECTIVE_VALUE == GetPropertyId( *pPropertyNames ) );
bSettingText = ( BASEPROPERTY_TEXT == GetPropertyId( *pPropertyNames ) );
}
m_bSettingValueAndText = ( bSettingValue && bSettingText );
ResetFlagOnExit aResetFlag( m_bSettingValueAndText );
UnoControlModel::setPropertyValues( _rPropertyNames, _rValues );
}
// -------------------------------------------------------------------
sal_Bool UnoControlFormattedFieldModel::convertFastPropertyValue(
Any& rConvertedValue, Any& rOldValue, sal_Int32 nPropId,
const Any& rValue ) throw (IllegalArgumentException)
{
if ( BASEPROPERTY_EFFECTIVE_DEFAULT == nPropId && rValue.hasValue() )
{
double dVal = 0;
sal_Int32 nVal = 0;
::rtl::OUString sVal;
sal_Bool bStreamed = (rValue >>= dVal);
if ( bStreamed )
{
rConvertedValue <<= dVal;
}
else
{
bStreamed = (rValue >>= nVal);
if ( bStreamed )
{
rConvertedValue <<= static_cast<double>(nVal);
}
else
{
bStreamed = (rValue >>= sVal);
if ( bStreamed )
{
rConvertedValue <<= sVal;
}
}
}
if ( bStreamed )
{
getFastPropertyValue( rOldValue, nPropId );
return !CompareProperties( rConvertedValue, rOldValue );
}
throw IllegalArgumentException(
( ::rtl::OUString::createFromAscii("Unable to convert the given value for the property ")
+= GetPropertyName((sal_uInt16)nPropId) )
+= ::rtl::OUString::createFromAscii(" (double, integer, or string expected)."),
static_cast< XPropertySet* >(this),
1);
}
return UnoControlModel::convertFastPropertyValue( rConvertedValue, rOldValue, nPropId, rValue );
}
// -------------------------------------------------------------------
Any UnoControlFormattedFieldModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
{
Any aReturn;
switch (nPropId)
{
case BASEPROPERTY_DEFAULTCONTROL: aReturn <<= ::rtl::OUString( ::rtl::OUString::createFromAscii( szServiceName_UnoControlFormattedField ) ); break;
case BASEPROPERTY_TREATASNUMBER: aReturn <<= (sal_Bool)sal_True; break;
case BASEPROPERTY_EFFECTIVE_DEFAULT:
case BASEPROPERTY_EFFECTIVE_VALUE:
case BASEPROPERTY_EFFECTIVE_MAX:
case BASEPROPERTY_EFFECTIVE_MIN:
case BASEPROPERTY_FORMATKEY:
case BASEPROPERTY_FORMATSSUPPLIER:
// (void)
break;
default : aReturn = UnoControlModel::ImplGetDefaultValue( nPropId ); break;
}
return aReturn;
}
// -------------------------------------------------------------------
::cppu::IPropertyArrayHelper& UnoControlFormattedFieldModel::getInfoHelper()
{
static UnoPropertyArrayHelper* pHelper = NULL;
if ( !pHelper )
{
Sequence<sal_Int32> aIDs = ImplGetPropertyIds();
pHelper = new UnoPropertyArrayHelper( aIDs );
}
return *pHelper;
}
// beans::XMultiPropertySet
// -------------------------------------------------------------------
Reference< XPropertySetInfo > UnoControlFormattedFieldModel::getPropertySetInfo( ) throw(RuntimeException)
{
static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
return xInfo;
}
// ===================================================================
// = UnoFormattedFieldControl
// ===================================================================
// -------------------------------------------------------------------
UnoFormattedFieldControl::UnoFormattedFieldControl()
{
}
// -------------------------------------------------------------------
::rtl::OUString UnoFormattedFieldControl::GetComponentServiceName()
{
return ::rtl::OUString::createFromAscii( "FormattedField" );
}
// -------------------------------------------------------------------
void UnoFormattedFieldControl::textChanged(const TextEvent& e) throw(RuntimeException)
{
Reference< XVclWindowPeer > xPeer(getPeer(), UNO_QUERY);
OSL_ENSURE(xPeer.is(), "UnoFormattedFieldControl::textChanged : what kind of peer do I have ?");
Sequence< ::rtl::OUString > aNames( 2 );
aNames[0] = GetPropertyName( BASEPROPERTY_EFFECTIVE_VALUE );
aNames[1] = GetPropertyName( BASEPROPERTY_TEXT );
Sequence< Any > aValues( 2 );
aValues[0] = xPeer->getProperty( aNames[0] );
aValues[1] = xPeer->getProperty( aNames[1] );
ImplSetPropertyValues( aNames, aValues, FALSE );
if ( GetTextListeners().getLength() )
GetTextListeners().textChanged( e );
}
//........................................................................
} // namespace toolkit
//........................................................................