1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <toolkit/controls/formattedcontrol.hxx>
21 #include <toolkit/helper/property.hxx>
23 #include <com/sun/star/awt/XVclWindowPeer.hpp>
24 #include <com/sun/star/uno/XComponentContext.hpp>
25 #include <com/sun/star/util/NumberFormatter.hpp>
26 #include <com/sun/star/util/NumberFormatsSupplier.hpp>
28 #include <tools/diagnose_ex.h>
29 #include <comphelper/processfactory.hxx>
30 #include <osl/diagnose.h>
32 #include "helper/unopropertyarrayhelper.hxx"
38 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star::awt
;
40 using namespace ::com::sun::star::lang
;
41 using namespace ::com::sun::star::beans
;
42 using namespace ::com::sun::star::util
;
48 ::osl::Mutex
& getDefaultFormatsMutex()
50 static ::osl::Mutex s_aDefaultFormatsMutex
;
51 return s_aDefaultFormatsMutex
;
55 Reference
< XNumberFormatsSupplier
>& lcl_getDefaultFormatsAccess_nothrow()
57 static Reference
< XNumberFormatsSupplier
> s_xDefaultFormats
;
58 return s_xDefaultFormats
;
62 bool& lcl_getTriedCreation()
64 static bool s_bTriedCreation
= false;
65 return s_bTriedCreation
;
69 const Reference
< XNumberFormatsSupplier
>& lcl_getDefaultFormats_throw()
71 ::osl::MutexGuard
aGuard( getDefaultFormatsMutex() );
73 bool& rbTriedCreation
= lcl_getTriedCreation();
74 Reference
< XNumberFormatsSupplier
>& rDefaultFormats( lcl_getDefaultFormatsAccess_nothrow() );
75 if ( !rDefaultFormats
.is() && !rbTriedCreation
)
77 rbTriedCreation
= true;
78 rDefaultFormats
= NumberFormatsSupplier::createWithDefaultLocale( ::comphelper::getProcessComponentContext() );
80 if ( !rDefaultFormats
.is() )
81 throw RuntimeException();
83 return rDefaultFormats
;
87 static oslInterlockedCount
s_refCount(0);
90 void lcl_registerDefaultFormatsClient()
92 osl_atomic_increment( &s_refCount
);
96 void lcl_revokeDefaultFormatsClient()
98 ::osl::ClearableMutexGuard
aGuard( getDefaultFormatsMutex() );
99 if ( 0 == osl_atomic_decrement( &s_refCount
) )
101 Reference
< XNumberFormatsSupplier
>& rDefaultFormats( lcl_getDefaultFormatsAccess_nothrow() );
102 Reference
< XNumberFormatsSupplier
> xReleasePotentialLastReference( rDefaultFormats
);
103 rDefaultFormats
.clear();
104 lcl_getTriedCreation() = false;
107 xReleasePotentialLastReference
.clear();
113 // = UnoControlFormattedFieldModel
116 UnoControlFormattedFieldModel::UnoControlFormattedFieldModel( const Reference
< XComponentContext
>& rxContext
)
117 :UnoControlModel( rxContext
)
118 ,m_bRevokedAsClient( false )
119 ,m_bSettingValueAndText( false )
121 ImplRegisterProperty( BASEPROPERTY_ALIGN
);
122 ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR
);
123 ImplRegisterProperty( BASEPROPERTY_BORDER
);
124 ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR
);
125 ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL
);
126 ImplRegisterProperty( BASEPROPERTY_EFFECTIVE_DEFAULT
);
127 ImplRegisterProperty( BASEPROPERTY_EFFECTIVE_VALUE
);
128 ImplRegisterProperty( BASEPROPERTY_EFFECTIVE_MAX
);
129 ImplRegisterProperty( BASEPROPERTY_EFFECTIVE_MIN
);
130 ImplRegisterProperty( BASEPROPERTY_ENABLED
);
131 ImplRegisterProperty( BASEPROPERTY_ENABLEVISIBLE
);
132 ImplRegisterProperty( BASEPROPERTY_FONTDESCRIPTOR
);
133 ImplRegisterProperty( BASEPROPERTY_FORMATKEY
);
134 ImplRegisterProperty( BASEPROPERTY_FORMATSSUPPLIER
);
135 ImplRegisterProperty( BASEPROPERTY_HELPTEXT
);
136 ImplRegisterProperty( BASEPROPERTY_HELPURL
);
137 ImplRegisterProperty( BASEPROPERTY_MAXTEXTLEN
);
138 ImplRegisterProperty( BASEPROPERTY_PRINTABLE
);
139 ImplRegisterProperty( BASEPROPERTY_REPEAT
);
140 ImplRegisterProperty( BASEPROPERTY_REPEAT_DELAY
);
141 ImplRegisterProperty( BASEPROPERTY_READONLY
);
142 ImplRegisterProperty( BASEPROPERTY_SPIN
);
143 ImplRegisterProperty( BASEPROPERTY_STRICTFORMAT
);
144 ImplRegisterProperty( BASEPROPERTY_TABSTOP
);
145 ImplRegisterProperty( BASEPROPERTY_TEXT
);
146 ImplRegisterProperty( BASEPROPERTY_TEXTCOLOR
);
147 ImplRegisterProperty( BASEPROPERTY_HIDEINACTIVESELECTION
);
148 ImplRegisterProperty( BASEPROPERTY_ENFORCE_FORMAT
);
149 ImplRegisterProperty( BASEPROPERTY_VERTICALALIGN
);
150 ImplRegisterProperty( BASEPROPERTY_WRITING_MODE
);
151 ImplRegisterProperty( BASEPROPERTY_CONTEXT_WRITING_MODE
);
152 ImplRegisterProperty( BASEPROPERTY_MOUSE_WHEEL_BEHAVIOUR
);
155 aTreatAsNumber
<<= true;
156 ImplRegisterProperty( BASEPROPERTY_TREATASNUMBER
, aTreatAsNumber
);
158 lcl_registerDefaultFormatsClient();
162 UnoControlFormattedFieldModel::~UnoControlFormattedFieldModel()
167 OUString
UnoControlFormattedFieldModel::getServiceName()
169 return OUString::createFromAscii( szServiceName_UnoControlFormattedFieldModel
);
173 void SAL_CALL
UnoControlFormattedFieldModel::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle
, const Any
& rValue
)
175 UnoControlModel::setFastPropertyValue_NoBroadcast( nHandle
, rValue
);
179 case BASEPROPERTY_EFFECTIVE_VALUE
:
180 if ( !m_bSettingValueAndText
)
181 impl_updateTextFromValue_nothrow();
183 case BASEPROPERTY_FORMATSSUPPLIER
:
184 impl_updateCachedFormatter_nothrow();
185 impl_updateTextFromValue_nothrow();
187 case BASEPROPERTY_FORMATKEY
:
188 impl_updateCachedFormatKey_nothrow();
189 impl_updateTextFromValue_nothrow();
195 void UnoControlFormattedFieldModel::impl_updateTextFromValue_nothrow()
197 if ( !m_xCachedFormatter
.is() )
198 impl_updateCachedFormatter_nothrow();
199 if ( !m_xCachedFormatter
.is() )
205 getFastPropertyValue( aEffectiveValue
, BASEPROPERTY_EFFECTIVE_VALUE
);
207 OUString sStringValue
;
208 if ( !( aEffectiveValue
>>= sStringValue
) )
210 double nDoubleValue(0);
211 if ( aEffectiveValue
>>= nDoubleValue
)
213 sal_Int32
nFormatKey( 0 );
214 if ( m_aCachedFormat
.hasValue() )
215 m_aCachedFormat
>>= nFormatKey
;
216 sStringValue
= m_xCachedFormatter
->convertNumberToString( nFormatKey
, nDoubleValue
);
220 Reference
< XPropertySet
> xThis( *this, UNO_QUERY
);
221 xThis
->setPropertyValue( GetPropertyName( BASEPROPERTY_TEXT
), makeAny( sStringValue
) );
223 catch( const Exception
& )
225 DBG_UNHANDLED_EXCEPTION();
230 void UnoControlFormattedFieldModel::impl_updateCachedFormatter_nothrow()
232 Any aFormatsSupplier
;
233 getFastPropertyValue( aFormatsSupplier
, BASEPROPERTY_FORMATSSUPPLIER
);
236 Reference
< XNumberFormatsSupplier
> xSupplier( aFormatsSupplier
, UNO_QUERY
);
237 if ( !xSupplier
.is() )
238 xSupplier
= lcl_getDefaultFormats_throw();
240 if ( !m_xCachedFormatter
.is() )
242 m_xCachedFormatter
.set(
243 NumberFormatter::create(::comphelper::getProcessComponentContext()),
247 m_xCachedFormatter
->attachNumberFormatsSupplier( xSupplier
);
249 catch( const Exception
& )
251 DBG_UNHANDLED_EXCEPTION();
256 void UnoControlFormattedFieldModel::impl_updateCachedFormatKey_nothrow()
259 getFastPropertyValue( aFormatKey
, BASEPROPERTY_FORMATKEY
);
260 m_aCachedFormat
= aFormatKey
;
264 void UnoControlFormattedFieldModel::dispose( )
266 UnoControlModel::dispose();
268 ::osl::MutexGuard
aGuard( GetMutex() );
269 if ( !m_bRevokedAsClient
)
271 lcl_revokeDefaultFormatsClient();
272 m_bRevokedAsClient
= true;
277 void UnoControlFormattedFieldModel::ImplNormalizePropertySequence( const sal_Int32 _nCount
, sal_Int32
* _pHandles
,
278 Any
* _pValues
, sal_Int32
* _pValidHandles
) const
280 ImplEnsureHandleOrder( _nCount
, _pHandles
, _pValues
, BASEPROPERTY_EFFECTIVE_VALUE
, BASEPROPERTY_TEXT
);
282 UnoControlModel::ImplNormalizePropertySequence( _nCount
, _pHandles
, _pValues
, _pValidHandles
);
288 class ResetFlagOnExit
294 explicit ResetFlagOnExit( bool& _rFlag
)
306 void SAL_CALL
UnoControlFormattedFieldModel::setPropertyValues( const Sequence
< OUString
>& _rPropertyNames
, const Sequence
< Any
>& _rValues
)
308 bool bSettingValue
= false;
309 bool bSettingText
= false;
310 for ( const OUString
* pPropertyNames
= _rPropertyNames
.getConstArray();
311 pPropertyNames
!= _rPropertyNames
.getConstArray() + _rPropertyNames
.getLength();
315 if ( BASEPROPERTY_EFFECTIVE_VALUE
== GetPropertyId( *pPropertyNames
) )
316 bSettingValue
= true;
318 if ( BASEPROPERTY_TEXT
== GetPropertyId( *pPropertyNames
) )
322 m_bSettingValueAndText
= ( bSettingValue
&& bSettingText
);
323 ResetFlagOnExit
aResetFlag( m_bSettingValueAndText
);
324 UnoControlModel::setPropertyValues( _rPropertyNames
, _rValues
);
328 sal_Bool
UnoControlFormattedFieldModel::convertFastPropertyValue(
329 Any
& rConvertedValue
, Any
& rOldValue
, sal_Int32 nPropId
,
332 if ( BASEPROPERTY_EFFECTIVE_DEFAULT
== nPropId
&& rValue
.hasValue() )
336 bool bStreamed
= (rValue
>>= dVal
);
339 rConvertedValue
<<= dVal
;
344 bStreamed
= (rValue
>>= nVal
);
347 rConvertedValue
<<= static_cast<double>(nVal
);
351 bStreamed
= (rValue
>>= sVal
);
354 rConvertedValue
<<= sVal
;
361 getFastPropertyValue( rOldValue
, nPropId
);
362 return !CompareProperties( rConvertedValue
, rOldValue
);
365 throw IllegalArgumentException(
366 ("Unable to convert the given value for the property "
367 + GetPropertyName((sal_uInt16
)nPropId
)
368 + " (double, integer, or string expected)."),
369 static_cast< XPropertySet
* >(this),
373 return UnoControlModel::convertFastPropertyValue( rConvertedValue
, rOldValue
, nPropId
, rValue
);
377 Any
UnoControlFormattedFieldModel::ImplGetDefaultValue( sal_uInt16 nPropId
) const
382 case BASEPROPERTY_DEFAULTCONTROL
: aReturn
<<= OUString( OUString::createFromAscii( szServiceName_UnoControlFormattedField
) ); break;
384 case BASEPROPERTY_TREATASNUMBER
: aReturn
<<= true; break;
386 case BASEPROPERTY_EFFECTIVE_DEFAULT
:
387 case BASEPROPERTY_EFFECTIVE_VALUE
:
388 case BASEPROPERTY_EFFECTIVE_MAX
:
389 case BASEPROPERTY_EFFECTIVE_MIN
:
390 case BASEPROPERTY_FORMATKEY
:
391 case BASEPROPERTY_FORMATSSUPPLIER
:
395 default : aReturn
= UnoControlModel::ImplGetDefaultValue( nPropId
); break;
402 ::cppu::IPropertyArrayHelper
& UnoControlFormattedFieldModel::getInfoHelper()
404 static UnoPropertyArrayHelper
* pHelper
= nullptr;
407 Sequence
<sal_Int32
> aIDs
= ImplGetPropertyIds();
408 pHelper
= new UnoPropertyArrayHelper( aIDs
);
413 // beans::XMultiPropertySet
415 Reference
< XPropertySetInfo
> UnoControlFormattedFieldModel::getPropertySetInfo( )
417 static Reference
< XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
421 OUString
UnoControlFormattedFieldModel::getImplementationName()
423 return OUString("stardiv.Toolkit.UnoControlFormattedFieldModel");
426 css::uno::Sequence
<OUString
>
427 UnoControlFormattedFieldModel::getSupportedServiceNames()
429 auto s(UnoControlModel::getSupportedServiceNames());
430 s
.realloc(s
.getLength() + 2);
431 s
[s
.getLength() - 2] = "com.sun.star.awt.UnoControlFormattedFieldModel";
432 s
[s
.getLength() - 1] = "stardiv.vcl.controlmodel.FormattedField";
436 // = UnoFormattedFieldControl
439 UnoFormattedFieldControl::UnoFormattedFieldControl()
440 :UnoSpinFieldControl()
445 OUString
UnoFormattedFieldControl::GetComponentServiceName()
447 return OUString("FormattedField");
451 void UnoFormattedFieldControl::textChanged(const TextEvent
& e
)
453 Reference
< XVclWindowPeer
> xPeer(getPeer(), UNO_QUERY
);
454 OSL_ENSURE(xPeer
.is(), "UnoFormattedFieldControl::textChanged : what kind of peer do I have ?");
456 Sequence
< OUString
> aNames( 2 );
457 aNames
[0] = GetPropertyName( BASEPROPERTY_EFFECTIVE_VALUE
);
458 aNames
[1] = GetPropertyName( BASEPROPERTY_TEXT
);
460 Sequence
< Any
> aValues( 2 );
461 aValues
[0] = xPeer
->getProperty( aNames
[0] );
462 aValues
[1] = xPeer
->getProperty( aNames
[1] );
464 ImplSetPropertyValues( aNames
, aValues
, false );
466 if ( GetTextListeners().getLength() )
467 GetTextListeners().textChanged( e
);
470 OUString
UnoFormattedFieldControl::getImplementationName()
472 return OUString("stardiv.Toolkit.UnoFormattedFieldControl");
475 css::uno::Sequence
<OUString
>
476 UnoFormattedFieldControl::getSupportedServiceNames()
478 auto s(UnoEditControl::getSupportedServiceNames());
479 s
.realloc(s
.getLength() + 2);
480 s
[s
.getLength() - 2] = "com.sun.star.awt.UnoControlFormattedField";
481 s
[s
.getLength() - 1] = "stardiv.vcl.control.FormattedField";
484 } // namespace toolkit
487 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
488 stardiv_Toolkit_UnoControlFormattedFieldModel_get_implementation(
489 css::uno::XComponentContext
*context
,
490 css::uno::Sequence
<css::uno::Any
> const &)
492 return cppu::acquire(new toolkit::UnoControlFormattedFieldModel(context
));
495 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
496 stardiv_Toolkit_UnoFormattedFieldControl_get_implementation(
497 css::uno::XComponentContext
*,
498 css::uno::Sequence
<css::uno::Any
> const &)
500 return cppu::acquire(new toolkit::UnoFormattedFieldControl());
503 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */