lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / svl / source / numbers / numfmuno.cxx
blobf290d8cb37ec0bb93200a6be01d0f6deaab7ee0e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <tools/color.hxx>
21 #include <i18nlangtag/mslangid.hxx>
22 #include <o3tl/any.hxx>
23 #include <osl/mutex.hxx>
24 #include <osl/diagnose.h>
25 #include <rtl/ustring.hxx>
27 #include <com/sun/star/util/Date.hpp>
28 #include <com/sun/star/util/MalformedNumberFormatException.hpp>
29 #include <com/sun/star/util/NotNumericException.hpp>
30 #include <com/sun/star/beans/PropertyAttribute.hpp>
31 #include <comphelper/propertysequence.hxx>
32 #include <cppuhelper/supportsservice.hxx>
34 #include "numfmuno.hxx"
35 #include <svl/numuno.hxx>
36 #include <svl/zforlist.hxx>
37 #include <svl/zformat.hxx>
38 #include <svl/itemprop.hxx>
40 using namespace com::sun::star;
42 #define PROPERTYNAME_FMTSTR "FormatString"
43 #define PROPERTYNAME_LOCALE "Locale"
44 #define PROPERTYNAME_TYPE "Type"
45 #define PROPERTYNAME_COMMENT "Comment"
46 #define PROPERTYNAME_CURREXT "CurrencyExtension"
47 #define PROPERTYNAME_CURRSYM "CurrencySymbol"
48 #define PROPERTYNAME_CURRABB "CurrencyAbbreviation"
49 #define PROPERTYNAME_DECIMALS "Decimals"
50 #define PROPERTYNAME_LEADING "LeadingZeros"
51 #define PROPERTYNAME_NEGRED "NegativeRed"
52 #define PROPERTYNAME_STDFORM "StandardFormat"
53 #define PROPERTYNAME_THOUS "ThousandsSeparator"
54 #define PROPERTYNAME_USERDEF "UserDefined"
56 #define PROPERTYNAME_NOZERO "NoZero"
57 #define PROPERTYNAME_NULLDATE "NullDate"
58 #define PROPERTYNAME_STDDEC "StandardDecimals"
59 #define PROPERTYNAME_TWODIGIT "TwoDigitDateStart"
61 // All without a Which-ID, Map only for PropertySetInfo
63 static const SfxItemPropertyMapEntry* lcl_GetNumberFormatPropertyMap()
65 static const SfxItemPropertyMapEntry aNumberFormatPropertyMap_Impl[] =
67 {OUString(PROPERTYNAME_FMTSTR), 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
68 {OUString(PROPERTYNAME_LOCALE), 0, cppu::UnoType<lang::Locale>::get(),beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
69 {OUString(PROPERTYNAME_TYPE), 0, cppu::UnoType<sal_Int16>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
70 {OUString(PROPERTYNAME_COMMENT), 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
71 {OUString(PROPERTYNAME_CURREXT), 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
72 {OUString(PROPERTYNAME_CURRSYM), 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
73 {OUString(PROPERTYNAME_DECIMALS), 0, cppu::UnoType<sal_Int16>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
74 {OUString(PROPERTYNAME_LEADING), 0, cppu::UnoType<sal_Int16>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
75 {OUString(PROPERTYNAME_NEGRED), 0, cppu::UnoType<bool>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
76 {OUString(PROPERTYNAME_STDFORM), 0, cppu::UnoType<bool>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
77 {OUString(PROPERTYNAME_THOUS), 0, cppu::UnoType<bool>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
78 {OUString(PROPERTYNAME_USERDEF), 0, cppu::UnoType<bool>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
79 {OUString(PROPERTYNAME_CURRABB), 0, cppu::UnoType<OUString>::get(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
80 { OUString(), 0, css::uno::Type(), 0, 0 }
82 return aNumberFormatPropertyMap_Impl;
85 static const SfxItemPropertyMapEntry* lcl_GetNumberSettingsPropertyMap()
87 static const SfxItemPropertyMapEntry aNumberSettingsPropertyMap_Impl[] =
89 {OUString(PROPERTYNAME_NOZERO), 0, cppu::UnoType<bool>::get(), beans::PropertyAttribute::BOUND, 0},
90 {OUString(PROPERTYNAME_NULLDATE), 0, cppu::UnoType<util::Date>::get(), beans::PropertyAttribute::BOUND, 0},
91 {OUString(PROPERTYNAME_STDDEC), 0, cppu::UnoType<sal_Int16>::get(), beans::PropertyAttribute::BOUND, 0},
92 {OUString(PROPERTYNAME_TWODIGIT), 0, cppu::UnoType<sal_Int16>::get(), beans::PropertyAttribute::BOUND, 0},
93 { OUString(), 0, css::uno::Type(), 0, 0 }
95 return aNumberSettingsPropertyMap_Impl;
98 static LanguageType lcl_GetLanguage( const lang::Locale& rLocale )
100 LanguageType eRet = LanguageTag::convertToLanguageType( rLocale, false);
101 if ( eRet == LANGUAGE_NONE )
102 eRet = LANGUAGE_SYSTEM; //! or throw an exception?
104 return eRet;
107 SvNumberFormatterServiceObj::SvNumberFormatterServiceObj()
108 :m_aMutex()
112 SvNumberFormatterServiceObj::~SvNumberFormatterServiceObj()
116 // XNumberFormatter
118 void SAL_CALL SvNumberFormatterServiceObj::attachNumberFormatsSupplier( const uno::Reference<util::XNumberFormatsSupplier>& _xSupplier )
120 ::rtl::Reference< SvNumberFormatsSupplierObj > xAutoReleaseOld;
122 // SYNCHRONIZED ->
124 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
126 SvNumberFormatsSupplierObj* pNew = SvNumberFormatsSupplierObj::getImplementation( _xSupplier );
127 if (!pNew)
128 throw uno::RuntimeException(); // wrong object
130 xAutoReleaseOld = xSupplier;
132 xSupplier = pNew;
133 m_aMutex = xSupplier->getSharedMutex();
135 // <- SYNCHRONIZED
138 uno::Reference<util::XNumberFormatsSupplier> SAL_CALL SvNumberFormatterServiceObj::getNumberFormatsSupplier()
140 ::osl::MutexGuard aGuard( m_aMutex );
141 return xSupplier.get();
144 sal_Int32 SAL_CALL SvNumberFormatterServiceObj::detectNumberFormat( sal_Int32 nKey, const OUString& aString )
146 ::osl::MutexGuard aGuard( m_aMutex );
148 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
149 if (!pFormatter)
150 throw uno::RuntimeException();
152 sal_uInt32 nUKey = nKey;
153 double fValue = 0.0;
154 if ( !pFormatter->IsNumberFormat(aString, nUKey, fValue) )
155 throw util::NotNumericException();
157 return nUKey;
160 double SAL_CALL SvNumberFormatterServiceObj::convertStringToNumber( sal_Int32 nKey, const OUString& aString )
162 ::osl::MutexGuard aGuard( m_aMutex );
164 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
165 if (!pFormatter)
166 throw uno::RuntimeException();
168 sal_uInt32 nUKey = nKey;
169 double fValue = 0.0;
170 if ( !pFormatter->IsNumberFormat(aString, nUKey, fValue) )
171 throw util::NotNumericException();
173 return fValue;
176 OUString SAL_CALL SvNumberFormatterServiceObj::convertNumberToString( sal_Int32 nKey, double fValue )
178 ::osl::MutexGuard aGuard( m_aMutex );
180 OUString aRet;
181 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
182 if (!pFormatter)
183 throw uno::RuntimeException();
185 Color* pColor = nullptr;
186 pFormatter->GetOutputString(fValue, nKey, aRet, &pColor);
188 return aRet;
191 util::Color SAL_CALL SvNumberFormatterServiceObj::queryColorForNumber( sal_Int32 nKey,
192 double fValue,
193 util::Color aDefaultColor )
195 ::osl::MutexGuard aGuard( m_aMutex );
197 util::Color nRet = aDefaultColor; // color = sal_Int32
198 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
199 if (!pFormatter)
200 throw uno::RuntimeException();
202 OUString aStr;
203 Color* pColor = nullptr;
204 pFormatter->GetOutputString(fValue, nKey, aStr, &pColor);
205 if (pColor)
206 nRet = sal_uInt32(*pColor);
207 // Else keep Default
209 return nRet;
212 OUString SAL_CALL SvNumberFormatterServiceObj::formatString( sal_Int32 nKey,
213 const OUString& aString )
215 ::osl::MutexGuard aGuard( m_aMutex );
217 OUString aRet;
218 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
219 if (!pFormatter)
221 throw uno::RuntimeException();
224 Color* pColor = nullptr;
225 pFormatter->GetOutputString(aString, nKey, aRet, &pColor);
227 return aRet;
230 util::Color SAL_CALL SvNumberFormatterServiceObj::queryColorForString( sal_Int32 nKey,
231 const OUString& aString,
232 util::Color aDefaultColor )
234 ::osl::MutexGuard aGuard( m_aMutex );
236 util::Color nRet = aDefaultColor; // color = sal_Int32
237 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
238 if (!pFormatter)
240 throw uno::RuntimeException();
243 OUString aStr;
244 Color* pColor = nullptr;
245 pFormatter->GetOutputString(aString, nKey, aStr, &pColor);
246 if (pColor)
248 nRet = sal_uInt32(*pColor);
250 // Else keep Default
252 return nRet;
255 OUString SAL_CALL SvNumberFormatterServiceObj::getInputString( sal_Int32 nKey, double fValue )
257 ::osl::MutexGuard aGuard( m_aMutex );
259 OUString aRet;
260 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
261 if (!pFormatter)
262 throw uno::RuntimeException();
264 pFormatter->GetInputLineString(fValue, nKey, aRet);
266 return aRet;
269 // XNumberFormatPreviewer
271 OUString SAL_CALL SvNumberFormatterServiceObj::convertNumberToPreviewString( const OUString& aFormat,
272 double fValue,
273 const lang::Locale& nLocale,
274 sal_Bool bAllowEnglish )
276 ::osl::MutexGuard aGuard( m_aMutex );
278 OUString aRet;
279 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
280 if (!pFormatter)
281 throw uno::RuntimeException();
283 LanguageType eLang = lcl_GetLanguage( nLocale );
284 Color* pColor = nullptr;
286 bool bOk;
287 if ( bAllowEnglish )
288 bOk = pFormatter->GetPreviewStringGuess( aFormat, fValue, aRet, &pColor, eLang );
289 else
290 bOk = pFormatter->GetPreviewString( aFormat, fValue, aRet, &pColor, eLang );
292 if (!bOk)
293 throw util::MalformedNumberFormatException();
295 return aRet;
298 util::Color SAL_CALL SvNumberFormatterServiceObj::queryPreviewColorForNumber( const OUString& aFormat,
299 double fValue,
300 const lang::Locale& nLocale,
301 sal_Bool bAllowEnglish,
302 util::Color aDefaultColor )
304 ::osl::MutexGuard aGuard( m_aMutex );
306 util::Color nRet = aDefaultColor; // color = sal_Int32
307 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : nullptr;
308 if (!pFormatter)
309 throw uno::RuntimeException();
311 OUString aOutString;
312 LanguageType eLang = lcl_GetLanguage( nLocale );
313 Color* pColor = nullptr;
315 bool bOk;
316 if ( bAllowEnglish )
317 bOk = pFormatter->GetPreviewStringGuess( aFormat, fValue, aOutString, &pColor, eLang );
318 else
319 bOk = pFormatter->GetPreviewString( aFormat, fValue, aOutString, &pColor, eLang );
321 if (!bOk)
322 throw util::MalformedNumberFormatException();
324 if (pColor)
325 nRet = sal_uInt32(*pColor);
326 // Else keep Default
328 return nRet;
331 // XServiceInfo
333 OUString SAL_CALL SvNumberFormatterServiceObj::getImplementationName()
335 return OUString("com.sun.star.uno.util.numbers.SvNumberFormatterServiceObject");
338 sal_Bool SAL_CALL SvNumberFormatterServiceObj::supportsService( const OUString& ServiceName )
340 return cppu::supportsService( this, ServiceName );
343 uno::Sequence<OUString> SAL_CALL SvNumberFormatterServiceObj::getSupportedServiceNames()
345 return { "com.sun.star.util.NumberFormatter" };
348 SvNumberFormatsObj::SvNumberFormatsObj( SvNumberFormatsSupplierObj& _rParent, ::comphelper::SharedMutex const & _rMutex )
349 :m_xSupplier( &_rParent )
350 ,m_aMutex( _rMutex )
354 SvNumberFormatsObj::~SvNumberFormatsObj()
358 // XNumberFormats
360 uno::Reference<beans::XPropertySet> SAL_CALL SvNumberFormatsObj::getByKey( sal_Int32 nKey )
362 ::osl::MutexGuard aGuard( m_aMutex );
364 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
365 const SvNumberformat* pFormat = pFormatter ? pFormatter->GetEntry(nKey) : nullptr;
366 if (!pFormat)
367 throw uno::RuntimeException();
369 return new SvNumberFormatObj( *m_xSupplier, nKey, m_aMutex );
372 uno::Sequence<sal_Int32> SAL_CALL SvNumberFormatsObj::queryKeys( sal_Int16 nType,
373 const lang::Locale& nLocale,
374 sal_Bool bCreate )
376 ::osl::MutexGuard aGuard( m_aMutex );
378 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
379 if ( !pFormatter )
380 throw uno::RuntimeException();
382 sal_uInt32 nIndex = 0;
383 LanguageType eLang = lcl_GetLanguage( nLocale );
384 SvNumberFormatTable& rTable = bCreate ?
385 pFormatter->ChangeCL( static_cast<SvNumFormatType>(nType), nIndex, eLang ) :
386 pFormatter->GetEntryTable( static_cast<SvNumFormatType>(nType), nIndex, eLang );
387 sal_uInt32 nCount = rTable.size();
388 uno::Sequence<sal_Int32> aSeq(nCount);
389 sal_Int32* pAry = aSeq.getArray();
390 sal_uInt32 i=0;
391 for (SvNumberFormatTable::const_iterator it = rTable.begin(); it != rTable.end(); ++it, ++i)
392 pAry[i] = it->first;
394 return aSeq;
397 sal_Int32 SAL_CALL SvNumberFormatsObj::queryKey( const OUString& aFormat,
398 const lang::Locale& nLocale,
399 sal_Bool bScan )
401 ::osl::MutexGuard aGuard( m_aMutex );
403 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
404 if (!pFormatter)
405 throw uno::RuntimeException();
407 LanguageType eLang = lcl_GetLanguage( nLocale );
408 if (bScan)
410 //! FIXME: Something still needs to happen here ...
412 sal_Int32 nRet = pFormatter->GetEntryKey( aFormat, eLang );
413 return nRet;
416 sal_Int32 SAL_CALL SvNumberFormatsObj::addNew( const OUString& aFormat,
417 const lang::Locale& nLocale )
419 ::osl::MutexGuard aGuard( m_aMutex );
421 sal_Int32 nRet = 0;
422 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
423 if (!pFormatter)
424 throw uno::RuntimeException();
426 OUString aFormStr = aFormat;
427 LanguageType eLang = lcl_GetLanguage( nLocale );
428 sal_uInt32 nKey = 0;
429 sal_Int32 nCheckPos = 0;
430 SvNumFormatType nType = SvNumFormatType::ALL;
431 bool bOk = pFormatter->PutEntry( aFormStr, nCheckPos, nType, nKey, eLang );
432 if (bOk)
433 nRet = nKey;
434 else if (nCheckPos)
436 throw util::MalformedNumberFormatException(); // Invalid Format
438 else
439 throw uno::RuntimeException(); // Other error (e.g. already added)
441 return nRet;
444 sal_Int32 SAL_CALL SvNumberFormatsObj::addNewConverted( const OUString& aFormat,
445 const lang::Locale& nLocale,
446 const lang::Locale& nNewLocale )
448 ::osl::MutexGuard aGuard( m_aMutex );
450 sal_Int32 nRet = 0;
451 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
452 if (!pFormatter)
453 throw uno::RuntimeException();
455 OUString aFormStr = aFormat;
456 LanguageType eLang = lcl_GetLanguage( nLocale );
457 LanguageType eNewLang = lcl_GetLanguage( nNewLocale );
458 sal_uInt32 nKey = 0;
459 sal_Int32 nCheckPos = 0;
460 SvNumFormatType nType = SvNumFormatType::ALL;
461 // This is used also when reading OOXML documents, there's no indicator
462 // whether to convert date particle order as well, so don't. See tdf#119013
463 bool bOk = pFormatter->PutandConvertEntry( aFormStr, nCheckPos, nType, nKey, eLang, eNewLang, false);
464 if (bOk || nKey > 0)
465 nRet = nKey;
466 else if (nCheckPos)
468 throw util::MalformedNumberFormatException(); // Invalid format
470 else
471 throw uno::RuntimeException(); // Other error (e.g. already added)
473 return nRet;
476 void SAL_CALL SvNumberFormatsObj::removeByKey( sal_Int32 nKey )
478 ::osl::MutexGuard aGuard( m_aMutex );
479 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
481 if (pFormatter)
483 pFormatter->DeleteEntry(nKey);
487 OUString SAL_CALL SvNumberFormatsObj::generateFormat( sal_Int32 nBaseKey,
488 const lang::Locale& nLocale,
489 sal_Bool bThousands,
490 sal_Bool bRed, sal_Int16 nDecimals,
491 sal_Int16 nLeading )
493 ::osl::MutexGuard aGuard( m_aMutex );
495 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
496 if (!pFormatter)
497 throw uno::RuntimeException();
499 LanguageType eLang = lcl_GetLanguage( nLocale );
500 OUString aRet = pFormatter->GenerateFormat(nBaseKey, eLang, bThousands, bRed, nDecimals, nLeading);
501 return aRet;
504 // XNumberFormatTypes
506 sal_Int32 SAL_CALL SvNumberFormatsObj::getStandardIndex( const lang::Locale& nLocale )
508 ::osl::MutexGuard aGuard( m_aMutex );
510 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
511 if (!pFormatter)
512 throw uno::RuntimeException();
514 LanguageType eLang = lcl_GetLanguage( nLocale );
515 sal_Int32 nRet = pFormatter->GetStandardIndex(eLang);
516 return nRet;
519 sal_Int32 SAL_CALL SvNumberFormatsObj::getStandardFormat( sal_Int16 nType, const lang::Locale& nLocale )
521 ::osl::MutexGuard aGuard( m_aMutex );
523 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
524 if (!pFormatter)
525 throw uno::RuntimeException();
527 LanguageType eLang = lcl_GetLanguage( nLocale );
528 // Mask out "defined" bit, so type from an existing number format
529 // can directly be used for getStandardFormat
530 SvNumFormatType nType2 = static_cast<SvNumFormatType>(nType);
531 nType2 &= ~SvNumFormatType::DEFINED;
532 sal_Int32 nRet = pFormatter->GetStandardFormat(nType2, eLang);
533 return nRet;
536 sal_Int32 SAL_CALL SvNumberFormatsObj::getFormatIndex( sal_Int16 nIndex, const lang::Locale& nLocale )
538 ::osl::MutexGuard aGuard( m_aMutex );
540 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
541 if (!pFormatter)
542 throw uno::RuntimeException();
544 LanguageType eLang = lcl_GetLanguage( nLocale );
545 sal_Int32 nRet = pFormatter->GetFormatIndex( static_cast<NfIndexTableOffset>(nIndex), eLang );
546 return nRet;
549 sal_Bool SAL_CALL SvNumberFormatsObj::isTypeCompatible( sal_Int16 nOldType, sal_Int16 nNewType )
551 ::osl::MutexGuard aGuard( m_aMutex );
553 return SvNumberFormatter::IsCompatible( static_cast<SvNumFormatType>(nOldType), static_cast<SvNumFormatType>(nNewType) );
556 sal_Int32 SAL_CALL SvNumberFormatsObj::getFormatForLocale( sal_Int32 nKey, const lang::Locale& nLocale )
558 ::osl::MutexGuard aGuard( m_aMutex );
560 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
561 if (!pFormatter)
562 throw uno::RuntimeException();
564 LanguageType eLang = lcl_GetLanguage( nLocale );
565 sal_Int32 nRet = pFormatter->GetFormatForLanguageIfBuiltIn(nKey, eLang);
566 return nRet;
569 // XServiceInfo
571 OUString SAL_CALL SvNumberFormatsObj::getImplementationName()
573 return OUString("SvNumberFormatsObj");
576 sal_Bool SAL_CALL SvNumberFormatsObj::supportsService( const OUString& ServiceName )
578 return cppu::supportsService( this, ServiceName );
581 uno::Sequence<OUString> SAL_CALL SvNumberFormatsObj::getSupportedServiceNames()
583 return { "com.sun.star.util.NumberFormats" };
586 SvNumberFormatObj::SvNumberFormatObj( SvNumberFormatsSupplierObj& rParent, sal_uLong nK, const ::comphelper::SharedMutex& _rMutex )
587 :m_xSupplier( &rParent )
588 ,nKey( nK )
589 ,m_aMutex( _rMutex )
593 SvNumberFormatObj::~SvNumberFormatObj()
597 // XPropertySet
599 uno::Reference<beans::XPropertySetInfo> SAL_CALL SvNumberFormatObj::getPropertySetInfo()
601 static uno::Reference<beans::XPropertySetInfo> aRef =
602 new SfxItemPropertySetInfo( lcl_GetNumberFormatPropertyMap() );
603 return aRef;
606 void SAL_CALL SvNumberFormatObj::setPropertyValue( const OUString&,
607 const uno::Any& )
609 throw beans::UnknownPropertyException(); // Everything is read-only
612 uno::Any SAL_CALL SvNumberFormatObj::getPropertyValue( const OUString& aPropertyName )
614 ::osl::MutexGuard aGuard( m_aMutex );
616 uno::Any aRet;
617 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
618 const SvNumberformat* pFormat = pFormatter ? pFormatter->GetEntry(nKey) : nullptr;
619 if (!pFormat)
620 throw uno::RuntimeException();
622 bool bThousand, bRed;
623 sal_uInt16 nDecimals, nLeading;
625 if (aPropertyName == PROPERTYNAME_FMTSTR)
627 aRet <<= pFormat->GetFormatstring();
629 else if (aPropertyName == PROPERTYNAME_LOCALE)
631 lang::Locale aLocale( LanguageTag::convertToLocale( pFormat->GetLanguage(), false));
632 aRet <<= aLocale;
634 else if (aPropertyName == PROPERTYNAME_TYPE)
636 aRet <<= static_cast<sal_Int16>( pFormat->GetType() );
638 else if (aPropertyName == PROPERTYNAME_COMMENT)
640 aRet <<= pFormat->GetComment();
642 else if (aPropertyName == PROPERTYNAME_STDFORM)
644 //! Pass through SvNumberformat Member bStandard?
645 aRet <<= ( ( nKey % SV_COUNTRY_LANGUAGE_OFFSET ) == 0 );
647 else if (aPropertyName == PROPERTYNAME_USERDEF)
649 aRet <<= bool( pFormat->GetType() & SvNumFormatType::DEFINED );
651 else if (aPropertyName == PROPERTYNAME_DECIMALS)
653 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
654 aRet <<= static_cast<sal_Int16>(nDecimals);
656 else if (aPropertyName == PROPERTYNAME_LEADING)
658 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
659 aRet <<= static_cast<sal_Int16>(nLeading);
661 else if (aPropertyName == PROPERTYNAME_NEGRED)
663 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
664 aRet <<= bRed;
666 else if (aPropertyName == PROPERTYNAME_THOUS)
668 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
669 aRet <<= bThousand;
671 else if (aPropertyName == PROPERTYNAME_CURRSYM)
673 OUString aSymbol, aExt;
674 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
675 aRet <<= aSymbol;
677 else if (aPropertyName == PROPERTYNAME_CURREXT)
679 OUString aSymbol, aExt;
680 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
681 aRet <<= aExt;
683 else if (aPropertyName == PROPERTYNAME_CURRABB)
685 OUString aSymbol, aExt;
686 bool bBank = false;
687 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
688 const NfCurrencyEntry* pCurr = SvNumberFormatter::GetCurrencyEntry( bBank,
689 aSymbol, aExt, pFormat->GetLanguage() );
690 if ( pCurr )
691 aRet <<= pCurr->GetBankSymbol();
692 else
693 aRet <<= OUString();
695 else
696 throw beans::UnknownPropertyException();
698 return aRet;
701 void SAL_CALL SvNumberFormatObj::addPropertyChangeListener( const OUString&,
702 const uno::Reference<beans::XPropertyChangeListener>&)
704 OSL_FAIL("not implemented");
707 void SAL_CALL SvNumberFormatObj::removePropertyChangeListener( const OUString&,
708 const uno::Reference<beans::XPropertyChangeListener>&)
710 OSL_FAIL("not implemented");
713 void SAL_CALL SvNumberFormatObj::addVetoableChangeListener( const OUString&,
714 const uno::Reference<beans::XVetoableChangeListener>&)
716 OSL_FAIL("not implemented");
719 void SAL_CALL SvNumberFormatObj::removeVetoableChangeListener( const OUString&,
720 const uno::Reference<beans::XVetoableChangeListener>&)
722 OSL_FAIL("not implemented");
725 // XPropertyAccess
727 uno::Sequence<beans::PropertyValue> SAL_CALL SvNumberFormatObj::getPropertyValues()
729 ::osl::MutexGuard aGuard( m_aMutex );
731 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
732 const SvNumberformat* pFormat = pFormatter ? pFormatter->GetEntry(nKey) : nullptr;
733 if (!pFormat)
734 throw uno::RuntimeException();
736 OUString aSymbol, aExt;
737 OUString aAbb;
738 bool bBank = false;
739 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
740 const NfCurrencyEntry* pCurr = SvNumberFormatter::GetCurrencyEntry( bBank,
741 aSymbol, aExt, pFormat->GetLanguage() );
742 if ( pCurr )
743 aAbb = pCurr->GetBankSymbol();
745 OUString aFmtStr = pFormat->GetFormatstring();
746 OUString aComment = pFormat->GetComment();
747 bool bStandard = ( ( nKey % SV_COUNTRY_LANGUAGE_OFFSET ) == 0 );
748 //! Pass through SvNumberformat Member bStandard?
749 bool bUserDef( pFormat->GetType() & SvNumFormatType::DEFINED );
750 bool bThousand, bRed;
751 sal_uInt16 nDecimals, nLeading;
752 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
753 lang::Locale aLocale( LanguageTag( pFormat->GetLanguage()).getLocale());
755 uno::Sequence<beans::PropertyValue> aSeq( comphelper::InitPropertySequence({
756 { PROPERTYNAME_FMTSTR, uno::Any(aFmtStr) },
757 { PROPERTYNAME_LOCALE, uno::Any(aLocale) },
758 { PROPERTYNAME_TYPE, uno::Any(sal_Int16( pFormat->GetType() )) },
759 { PROPERTYNAME_COMMENT, uno::Any(aComment) },
760 { PROPERTYNAME_STDFORM, uno::Any(bStandard) },
761 { PROPERTYNAME_USERDEF, uno::Any(bUserDef) },
762 { PROPERTYNAME_DECIMALS, uno::Any(sal_Int16( nDecimals )) },
763 { PROPERTYNAME_LEADING, uno::Any(sal_Int16( nLeading )) },
764 { PROPERTYNAME_NEGRED, uno::Any(bRed) },
765 { PROPERTYNAME_THOUS, uno::Any(bThousand) },
766 { PROPERTYNAME_CURRSYM, uno::Any(aSymbol) },
767 { PROPERTYNAME_CURREXT, uno::Any(aExt) },
768 { PROPERTYNAME_CURRABB, uno::Any(aAbb) }
769 }));
771 return aSeq;
774 void SAL_CALL SvNumberFormatObj::setPropertyValues( const uno::Sequence<beans::PropertyValue>& )
776 throw beans::UnknownPropertyException(); // Everything is read-only
779 // XServiceInfo
781 OUString SAL_CALL SvNumberFormatObj::getImplementationName()
783 return OUString("SvNumberFormatObj");
786 sal_Bool SAL_CALL SvNumberFormatObj::supportsService( const OUString& ServiceName )
788 return cppu::supportsService( this, ServiceName );
791 uno::Sequence<OUString> SAL_CALL SvNumberFormatObj::getSupportedServiceNames()
793 return { "com.sun.star.util.NumberFormatProperties" };
796 SvNumberFormatSettingsObj::SvNumberFormatSettingsObj( SvNumberFormatsSupplierObj& rParent, const ::comphelper::SharedMutex& _rMutex )
797 :m_xSupplier( &rParent )
798 ,m_aMutex( _rMutex )
802 SvNumberFormatSettingsObj::~SvNumberFormatSettingsObj()
806 // XPropertySet
808 uno::Reference<beans::XPropertySetInfo> SAL_CALL SvNumberFormatSettingsObj::getPropertySetInfo()
810 static uno::Reference<beans::XPropertySetInfo> aRef =
811 new SfxItemPropertySetInfo( lcl_GetNumberSettingsPropertyMap() );
812 return aRef;
815 void SAL_CALL SvNumberFormatSettingsObj::setPropertyValue( const OUString& aPropertyName,
816 const uno::Any& aValue )
818 ::osl::MutexGuard aGuard( m_aMutex );
820 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
821 if (!pFormatter)
822 throw uno::RuntimeException();
824 if (aPropertyName == PROPERTYNAME_NOZERO)
826 // operator >>= shouldn't be used for bool (?)
827 if ( auto b = o3tl::tryAccess<bool>(aValue) )
828 pFormatter->SetNoZero( *b );
830 else if (aPropertyName == PROPERTYNAME_NULLDATE)
832 util::Date aDate;
833 if ( aValue >>= aDate )
834 pFormatter->ChangeNullDate( aDate.Day, aDate.Month, aDate.Year );
836 else if (aPropertyName == PROPERTYNAME_STDDEC)
838 sal_Int16 nInt16 = sal_Int16();
839 if ( aValue >>= nInt16 )
840 pFormatter->ChangeStandardPrec( nInt16 );
842 else if (aPropertyName == PROPERTYNAME_TWODIGIT)
844 sal_Int16 nInt16 = sal_Int16();
845 if ( aValue >>= nInt16 )
846 pFormatter->SetYear2000( nInt16 );
848 else
849 throw beans::UnknownPropertyException();
853 uno::Any SAL_CALL SvNumberFormatSettingsObj::getPropertyValue( const OUString& aPropertyName )
855 ::osl::MutexGuard aGuard( m_aMutex );
857 uno::Any aRet;
858 SvNumberFormatter* pFormatter = m_xSupplier->GetNumberFormatter();
859 if (!pFormatter)
860 throw uno::RuntimeException();
862 if (aPropertyName == PROPERTYNAME_NOZERO)
864 aRet <<= pFormatter->GetNoZero();
866 else if (aPropertyName == PROPERTYNAME_NULLDATE)
868 const Date& rDate = pFormatter->GetNullDate();
869 aRet <<= rDate.GetUNODate();
871 else if (aPropertyName == PROPERTYNAME_STDDEC)
872 aRet <<= static_cast<sal_Int16>( pFormatter->GetStandardPrec() );
873 else if (aPropertyName == PROPERTYNAME_TWODIGIT)
874 aRet <<= static_cast<sal_Int16>( pFormatter->GetYear2000() );
875 else
876 throw beans::UnknownPropertyException();
878 return aRet;
881 void SAL_CALL SvNumberFormatSettingsObj::addPropertyChangeListener( const OUString&,
882 const uno::Reference<beans::XPropertyChangeListener>&)
884 OSL_FAIL("not implemented");
887 void SAL_CALL SvNumberFormatSettingsObj::removePropertyChangeListener( const OUString&,
888 const uno::Reference<beans::XPropertyChangeListener>&)
890 OSL_FAIL("not implemented");
893 void SAL_CALL SvNumberFormatSettingsObj::addVetoableChangeListener( const OUString&,
894 const uno::Reference<beans::XVetoableChangeListener>&)
896 OSL_FAIL("not implemented");
899 void SAL_CALL SvNumberFormatSettingsObj::removeVetoableChangeListener( const OUString&,
900 const uno::Reference<beans::XVetoableChangeListener>&)
902 OSL_FAIL("not implemented");
905 // XServiceInfo
907 OUString SAL_CALL SvNumberFormatSettingsObj::getImplementationName()
909 return OUString("SvNumberFormatSettingsObj");
912 sal_Bool SAL_CALL SvNumberFormatSettingsObj::supportsService( const OUString& ServiceName )
914 return cppu::supportsService( this, ServiceName );
917 uno::Sequence<OUString> SAL_CALL SvNumberFormatSettingsObj::getSupportedServiceNames()
919 return { "com.sun.star.util.NumberFormatSettings" };
923 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
924 com_sun_star_uno_util_numbers_SvNumberFormatterServiceObject_get_implementation(css::uno::XComponentContext*,
925 css::uno::Sequence<css::uno::Any> const &)
927 return cppu::acquire(new SvNumberFormatterServiceObj());
931 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */