bump product version to 4.1.6.2
[LibreOffice.git] / svl / source / numbers / numfmuno.cxx
blob40f34e98f73817956fd4af023acacb04715031aa
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 .
21 #include <tools/color.hxx>
22 #include <i18nlangtag/mslangid.hxx>
23 #include <osl/mutex.hxx>
24 #include <rtl/ustring.hxx>
26 #include <com/sun/star/util/Date.hpp>
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
29 #include "numfmuno.hxx"
30 #include <svl/numuno.hxx>
31 #include <svl/zforlist.hxx>
32 #include <svl/zformat.hxx>
33 #include <svl/itemprop.hxx>
35 using namespace com::sun::star;
37 //------------------------------------------------------------------------
39 #define SERVICENAME_NUMBERFORMATTER "com.sun.star.util.NumberFormatter"
40 #define SERVICENAME_NUMBERSETTINGS "com.sun.star.util.NumberFormatSettings"
41 #define SERVICENAME_NUMBERFORMATS "com.sun.star.util.NumberFormats"
42 #define SERVICENAME_NUMBERFORMAT "com.sun.star.util.NumberFormatProperties"
44 //------------------------------------------------------------------------
46 #define PROPERTYNAME_FMTSTR "FormatString"
47 #define PROPERTYNAME_LOCALE "Locale"
48 #define PROPERTYNAME_TYPE "Type"
49 #define PROPERTYNAME_COMMENT "Comment"
50 #define PROPERTYNAME_CURREXT "CurrencyExtension"
51 #define PROPERTYNAME_CURRSYM "CurrencySymbol"
52 #define PROPERTYNAME_CURRABB "CurrencyAbbreviation"
53 #define PROPERTYNAME_DECIMALS "Decimals"
54 #define PROPERTYNAME_LEADING "LeadingZeros"
55 #define PROPERTYNAME_NEGRED "NegativeRed"
56 #define PROPERTYNAME_STDFORM "StandardFormat"
57 #define PROPERTYNAME_THOUS "ThousandsSeparator"
58 #define PROPERTYNAME_USERDEF "UserDefined"
60 #define PROPERTYNAME_NOZERO "NoZero"
61 #define PROPERTYNAME_NULLDATE "NullDate"
62 #define PROPERTYNAME_STDDEC "StandardDecimals"
63 #define PROPERTYNAME_TWODIGIT "TwoDigitDateStart"
65 //------------------------------------------------------------------------
67 // alles ohne Which-ID, Map nur fuer PropertySetInfo
69 static const SfxItemPropertyMapEntry* lcl_GetNumberFormatPropertyMap()
71 static SfxItemPropertyMapEntry aNumberFormatPropertyMap_Impl[] =
73 {MAP_CHAR_LEN(PROPERTYNAME_FMTSTR), 0, &getCppuType((OUString*)0), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
74 {MAP_CHAR_LEN(PROPERTYNAME_LOCALE), 0, &getCppuType((lang::Locale*)0),beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
75 {MAP_CHAR_LEN(PROPERTYNAME_TYPE), 0, &getCppuType((sal_Int16*)0), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
76 {MAP_CHAR_LEN(PROPERTYNAME_COMMENT), 0, &getCppuType((OUString*)0), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
77 {MAP_CHAR_LEN(PROPERTYNAME_CURREXT), 0, &getCppuType((OUString*)0), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
78 {MAP_CHAR_LEN(PROPERTYNAME_CURRSYM), 0, &getCppuType((OUString*)0), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
79 {MAP_CHAR_LEN(PROPERTYNAME_DECIMALS), 0, &getCppuType((sal_Int16*)0), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
80 {MAP_CHAR_LEN(PROPERTYNAME_LEADING), 0, &getCppuType((sal_Int16*)0), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
81 {MAP_CHAR_LEN(PROPERTYNAME_NEGRED), 0, &getBooleanCppuType(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
82 {MAP_CHAR_LEN(PROPERTYNAME_STDFORM), 0, &getBooleanCppuType(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
83 {MAP_CHAR_LEN(PROPERTYNAME_THOUS), 0, &getBooleanCppuType(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
84 {MAP_CHAR_LEN(PROPERTYNAME_USERDEF), 0, &getBooleanCppuType(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
85 {MAP_CHAR_LEN(PROPERTYNAME_CURRABB), 0, &getCppuType((OUString*)0), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
86 {0,0,0,0,0,0}
88 return aNumberFormatPropertyMap_Impl;
91 static const SfxItemPropertyMapEntry* lcl_GetNumberSettingsPropertyMap()
93 static SfxItemPropertyMapEntry aNumberSettingsPropertyMap_Impl[] =
95 {MAP_CHAR_LEN(PROPERTYNAME_NOZERO), 0, &getBooleanCppuType(), beans::PropertyAttribute::BOUND, 0},
96 {MAP_CHAR_LEN(PROPERTYNAME_NULLDATE), 0, &getCppuType((util::Date*)0), beans::PropertyAttribute::BOUND, 0},
97 {MAP_CHAR_LEN(PROPERTYNAME_STDDEC), 0, &getCppuType((sal_Int16*)0), beans::PropertyAttribute::BOUND, 0},
98 {MAP_CHAR_LEN(PROPERTYNAME_TWODIGIT), 0, &getCppuType((sal_Int16*)0), beans::PropertyAttribute::BOUND, 0},
99 {0,0,0,0,0,0}
101 return aNumberSettingsPropertyMap_Impl;
104 //----------------------------------------------------------------------------------------
106 static LanguageType lcl_GetLanguage( const lang::Locale& rLocale )
108 // empty language -> LANGUAGE_SYSTEM
109 if ( rLocale.Language.isEmpty() )
110 return LANGUAGE_SYSTEM;
112 LanguageType eRet = LanguageTag( rLocale ).getLanguageType( false );
113 if ( eRet == LANGUAGE_NONE )
114 eRet = LANGUAGE_SYSTEM; //! or throw an exception?
116 return eRet;
119 //----------------------------------------------------------------------------------------
121 SvNumberFormatterServiceObj::SvNumberFormatterServiceObj()
122 :m_aMutex()
126 SvNumberFormatterServiceObj::~SvNumberFormatterServiceObj()
130 com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL SvNumberFormatterServiceObj_CreateInstance( SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& )
132 return ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface >( ( ::cppu::OWeakObject* ) new SvNumberFormatterServiceObj );
135 // XNumberFormatter
137 void SAL_CALL SvNumberFormatterServiceObj::attachNumberFormatsSupplier( const uno::Reference<util::XNumberFormatsSupplier>& _xSupplier )
138 throw(uno::RuntimeException)
140 ::rtl::Reference< SvNumberFormatsSupplierObj > xAutoReleaseOld;
142 // SYNCHRONIZED ->
144 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
146 SvNumberFormatsSupplierObj* pNew = SvNumberFormatsSupplierObj::getImplementation( _xSupplier );
147 if (!pNew)
148 throw uno::RuntimeException(); // wrong object
150 xAutoReleaseOld = xSupplier;
152 xSupplier = pNew;
153 m_aMutex = xSupplier->getSharedMutex();
155 // <- SYNCHRONIZED
158 uno::Reference<util::XNumberFormatsSupplier> SAL_CALL SvNumberFormatterServiceObj::getNumberFormatsSupplier()
159 throw(uno::RuntimeException)
161 ::osl::MutexGuard aGuard( m_aMutex );
162 return xSupplier.get();
165 sal_Int32 SAL_CALL SvNumberFormatterServiceObj::detectNumberFormat( sal_Int32 nKey, const OUString& aString )
166 throw(util::NotNumericException, uno::RuntimeException)
168 ::osl::MutexGuard aGuard( m_aMutex );
170 sal_Int32 nRet = 0;
171 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
172 if (pFormatter)
174 sal_uInt32 nUKey = nKey;
175 double fValue = 0.0;
176 if ( pFormatter->IsNumberFormat(aString, nUKey, fValue) )
177 nRet = nUKey;
178 else
179 throw util::NotNumericException();
181 else
182 throw uno::RuntimeException();
184 return nRet;
187 double SAL_CALL SvNumberFormatterServiceObj::convertStringToNumber( sal_Int32 nKey, const OUString& aString )
188 throw(util::NotNumericException, uno::RuntimeException)
190 ::osl::MutexGuard aGuard( m_aMutex );
192 double fRet = 0.0;
193 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
194 if (pFormatter)
196 sal_uInt32 nUKey = nKey;
197 double fValue = 0.0;
198 if ( pFormatter->IsNumberFormat(aString, nUKey, fValue) )
199 fRet = fValue;
200 else
201 throw util::NotNumericException();
203 else
204 throw uno::RuntimeException();
206 return fRet;
209 OUString SAL_CALL SvNumberFormatterServiceObj::convertNumberToString( sal_Int32 nKey, double fValue )
210 throw(uno::RuntimeException)
212 ::osl::MutexGuard aGuard( m_aMutex );
214 OUString aRet;
215 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
216 if (pFormatter)
218 Color* pColor = NULL;
219 pFormatter->GetOutputString(fValue, nKey, aRet, &pColor);
221 else
222 throw uno::RuntimeException();
224 return aRet;
227 util::Color SAL_CALL SvNumberFormatterServiceObj::queryColorForNumber( sal_Int32 nKey,
228 double fValue,
229 util::Color aDefaultColor )
230 throw(uno::RuntimeException)
232 ::osl::MutexGuard aGuard( m_aMutex );
234 util::Color nRet = aDefaultColor; // color = sal_Int32
235 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
236 if (pFormatter)
238 OUString aStr;
239 Color* pColor = NULL;
240 pFormatter->GetOutputString(fValue, nKey, aStr, &pColor);
241 if (pColor)
242 nRet = pColor->GetColor();
243 // sonst Default behalten
245 else
246 throw uno::RuntimeException();
248 return nRet;
251 OUString SAL_CALL SvNumberFormatterServiceObj::formatString( sal_Int32 nKey,
252 const OUString& aString )
253 throw(uno::RuntimeException)
255 ::osl::MutexGuard aGuard( m_aMutex );
257 OUString aRet;
258 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
259 if (pFormatter)
261 OUString aTemp = aString;
262 Color* pColor = NULL;
263 pFormatter->GetOutputString(aTemp, nKey, aRet, &pColor);
265 else
267 throw uno::RuntimeException();
269 return aRet;
272 util::Color SAL_CALL SvNumberFormatterServiceObj::queryColorForString( sal_Int32 nKey,
273 const OUString& aString,
274 util::Color aDefaultColor )
275 throw(uno::RuntimeException)
277 ::osl::MutexGuard aGuard( m_aMutex );
279 util::Color nRet = aDefaultColor; // color = sal_Int32
280 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
281 if (pFormatter)
283 OUString aTemp = aString;
284 OUString aStr;
285 Color* pColor = NULL;
286 pFormatter->GetOutputString(aTemp, nKey, aStr, &pColor);
287 if (pColor)
289 nRet = pColor->GetColor();
291 // sonst Default behalten
293 else
295 throw uno::RuntimeException();
298 return nRet;
301 OUString SAL_CALL SvNumberFormatterServiceObj::getInputString( sal_Int32 nKey, double fValue )
302 throw(uno::RuntimeException)
304 ::osl::MutexGuard aGuard( m_aMutex );
306 OUString aRet;
307 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
308 if (pFormatter)
309 pFormatter->GetInputLineString(fValue, nKey, aRet);
310 else
311 throw uno::RuntimeException();
313 return aRet;
316 // XNumberFormatPreviewer
318 OUString SAL_CALL SvNumberFormatterServiceObj::convertNumberToPreviewString( const OUString& aFormat,
319 double fValue,
320 const lang::Locale& nLocale,
321 sal_Bool bAllowEnglish )
322 throw(util::MalformedNumberFormatException, uno::RuntimeException)
324 ::osl::MutexGuard aGuard( m_aMutex );
326 OUString aRet;
327 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
328 if (pFormatter)
330 LanguageType eLang = lcl_GetLanguage( nLocale );
331 Color* pColor = NULL;
333 bool bOk;
334 if ( bAllowEnglish )
335 bOk = pFormatter->GetPreviewStringGuess( aFormat, fValue, aRet, &pColor, eLang );
336 else
337 bOk = pFormatter->GetPreviewString( aFormat, fValue, aRet, &pColor, eLang );
339 if (!bOk)
340 throw util::MalformedNumberFormatException();
342 else
343 throw uno::RuntimeException();
345 return aRet;
348 util::Color SAL_CALL SvNumberFormatterServiceObj::queryPreviewColorForNumber( const OUString& aFormat,
349 double fValue,
350 const lang::Locale& nLocale,
351 sal_Bool bAllowEnglish,
352 util::Color aDefaultColor )
353 throw(util::MalformedNumberFormatException, uno::RuntimeException)
355 ::osl::MutexGuard aGuard( m_aMutex );
357 util::Color nRet = aDefaultColor; // color = sal_Int32
358 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
359 if (pFormatter)
361 OUString aOutString;
362 LanguageType eLang = lcl_GetLanguage( nLocale );
363 Color* pColor = NULL;
365 bool bOk;
366 if ( bAllowEnglish )
367 bOk = pFormatter->GetPreviewStringGuess( aFormat, fValue, aOutString, &pColor, eLang );
368 else
369 bOk = pFormatter->GetPreviewString( aFormat, fValue, aOutString, &pColor, eLang );
371 if (bOk)
373 if (pColor)
374 nRet = pColor->GetColor();
375 // sonst Default behalten
377 else
378 throw util::MalformedNumberFormatException();
380 else
381 throw uno::RuntimeException();
383 return nRet;
386 // XServiceInfo
388 OUString SAL_CALL SvNumberFormatterServiceObj::getImplementationName()
389 throw(uno::RuntimeException)
391 return OUString("com.sun.star.uno.util.numbers.SvNumberFormatterServiceObject");
394 sal_Bool SAL_CALL SvNumberFormatterServiceObj::supportsService( const OUString& ServiceName )
395 throw(uno::RuntimeException)
397 return ( ServiceName.compareToAscii(SERVICENAME_NUMBERFORMATTER) == 0 );
400 uno::Sequence<OUString> SAL_CALL SvNumberFormatterServiceObj::getSupportedServiceNames()
401 throw(uno::RuntimeException)
403 uno::Sequence<OUString> aRet(1);
404 OUString* pArray = aRet.getArray();
405 pArray[0] = SERVICENAME_NUMBERFORMATTER;
406 return aRet;
409 //------------------------------------------------------------------------
411 SvNumberFormatsObj::SvNumberFormatsObj( SvNumberFormatsSupplierObj& _rParent, ::comphelper::SharedMutex& _rMutex )
412 :rSupplier( _rParent )
413 ,m_aMutex( _rMutex )
415 rSupplier.acquire();
418 SvNumberFormatsObj::~SvNumberFormatsObj()
420 rSupplier.release();
423 // XNumberFormats
425 uno::Reference<beans::XPropertySet> SAL_CALL SvNumberFormatsObj::getByKey( sal_Int32 nKey )
426 throw(uno::RuntimeException)
428 ::osl::MutexGuard aGuard( m_aMutex );
430 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
431 const SvNumberformat* pFormat = pFormatter ? pFormatter->GetEntry(nKey) : NULL;
432 if (pFormat)
433 return new SvNumberFormatObj( rSupplier, nKey, m_aMutex );
434 else
435 throw uno::RuntimeException();
438 uno::Sequence<sal_Int32> SAL_CALL SvNumberFormatsObj::queryKeys( sal_Int16 nType,
439 const lang::Locale& nLocale,
440 sal_Bool bCreate )
441 throw(uno::RuntimeException)
443 ::osl::MutexGuard aGuard( m_aMutex );
445 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
446 if ( pFormatter )
448 sal_uInt32 nIndex = 0;
449 LanguageType eLang = lcl_GetLanguage( nLocale );
450 SvNumberFormatTable& rTable = bCreate ?
451 pFormatter->ChangeCL( nType, nIndex, eLang ) :
452 pFormatter->GetEntryTable( nType, nIndex, eLang );
453 sal_uInt32 nCount = rTable.size();
454 uno::Sequence<sal_Int32> aSeq(nCount);
455 sal_Int32* pAry = aSeq.getArray();
456 sal_uInt32 i=0;
457 for (SvNumberFormatTable::iterator it = rTable.begin(); it != rTable.end(); ++it, ++i)
458 pAry[i] = it->first;
460 return aSeq;
462 else
463 throw uno::RuntimeException();
466 sal_Int32 SAL_CALL SvNumberFormatsObj::queryKey( const OUString& aFormat,
467 const lang::Locale& nLocale,
468 sal_Bool bScan )
469 throw(uno::RuntimeException)
471 ::osl::MutexGuard aGuard( m_aMutex );
473 sal_Int32 nRet = 0;
474 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
475 if (pFormatter)
477 LanguageType eLang = lcl_GetLanguage( nLocale );
478 if (bScan)
480 //! irgendwas muss hier noch passieren...
482 nRet = pFormatter->GetEntryKey( aFormat, eLang );
484 else
485 throw uno::RuntimeException();
487 return nRet;
490 sal_Int32 SAL_CALL SvNumberFormatsObj::addNew( const OUString& aFormat,
491 const lang::Locale& nLocale )
492 throw(util::MalformedNumberFormatException, uno::RuntimeException)
494 ::osl::MutexGuard aGuard( m_aMutex );
496 sal_Int32 nRet = 0;
497 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
498 if (pFormatter)
500 OUString aFormStr = aFormat;
501 LanguageType eLang = lcl_GetLanguage( nLocale );
502 sal_uInt32 nKey = 0;
503 sal_Int32 nCheckPos = 0;
504 short nType = 0;
505 bool bOk = pFormatter->PutEntry( aFormStr, nCheckPos, nType, nKey, eLang );
506 if (bOk)
507 nRet = nKey;
508 else if (nCheckPos)
510 throw util::MalformedNumberFormatException(); // ungueltiges Format
512 else
513 throw uno::RuntimeException(); // anderer Fehler (z.B. schon vorhanden)
515 else
516 throw uno::RuntimeException();
518 return nRet;
521 sal_Int32 SAL_CALL SvNumberFormatsObj::addNewConverted( const OUString& aFormat,
522 const lang::Locale& nLocale,
523 const lang::Locale& nNewLocale )
524 throw(util::MalformedNumberFormatException, uno::RuntimeException)
526 ::osl::MutexGuard aGuard( m_aMutex );
528 sal_Int32 nRet = 0;
529 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
530 if (pFormatter)
532 OUString aFormStr = aFormat;
533 LanguageType eLang = lcl_GetLanguage( nLocale );
534 LanguageType eNewLang = lcl_GetLanguage( nNewLocale );
535 sal_uInt32 nKey = 0;
536 sal_Int32 nCheckPos = 0;
537 short nType = 0;
538 bool bOk = pFormatter->PutandConvertEntry( aFormStr, nCheckPos, nType, nKey, eLang, eNewLang );
539 if (bOk || nKey > 0)
540 nRet = nKey;
541 else if (nCheckPos)
543 throw util::MalformedNumberFormatException(); // ungueltiges Format
545 else
546 throw uno::RuntimeException(); // anderer Fehler (z.B. schon vorhanden)
548 else
549 throw uno::RuntimeException();
551 return nRet;
554 void SAL_CALL SvNumberFormatsObj::removeByKey( sal_Int32 nKey ) throw(uno::RuntimeException)
556 ::osl::MutexGuard aGuard( m_aMutex );
557 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
559 if (pFormatter)
561 pFormatter->DeleteEntry(nKey);
562 rSupplier.NumberFormatDeleted(nKey); // Benachrichtigung fuers Dokument
566 OUString SAL_CALL SvNumberFormatsObj::generateFormat( sal_Int32 nBaseKey,
567 const lang::Locale& nLocale,
568 sal_Bool bThousands,
569 sal_Bool bRed, sal_Int16 nDecimals,
570 sal_Int16 nLeading )
571 throw(uno::RuntimeException)
573 ::osl::MutexGuard aGuard( m_aMutex );
575 OUString aRet;
576 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
577 if (pFormatter)
579 LanguageType eLang = lcl_GetLanguage( nLocale );
580 aRet = pFormatter->GenerateFormat(nBaseKey, eLang, bThousands, bRed, nDecimals, nLeading);
582 else
583 throw uno::RuntimeException();
585 return aRet;
588 // XNumberFormatTypes
590 sal_Int32 SAL_CALL SvNumberFormatsObj::getStandardIndex( const lang::Locale& nLocale )
591 throw(uno::RuntimeException)
593 ::osl::MutexGuard aGuard( m_aMutex );
595 sal_Int32 nRet = 0;
596 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
597 if (pFormatter)
599 LanguageType eLang = lcl_GetLanguage( nLocale );
600 nRet = pFormatter->GetStandardIndex(eLang);
602 else
603 throw uno::RuntimeException();
605 return nRet;
608 sal_Int32 SAL_CALL SvNumberFormatsObj::getStandardFormat( sal_Int16 nType, const lang::Locale& nLocale )
609 throw(uno::RuntimeException)
611 ::osl::MutexGuard aGuard( m_aMutex );
613 sal_Int32 nRet = 0;
614 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
615 if (pFormatter)
617 LanguageType eLang = lcl_GetLanguage( nLocale );
618 // mask out "defined" bit, so type from an existing number format
619 // can directly be used for getStandardFormat
620 nType &= ~NUMBERFORMAT_DEFINED;
621 nRet = pFormatter->GetStandardFormat(nType, eLang);
623 else
624 throw uno::RuntimeException();
626 return nRet;
629 sal_Int32 SAL_CALL SvNumberFormatsObj::getFormatIndex( sal_Int16 nIndex, const lang::Locale& nLocale )
630 throw(uno::RuntimeException)
632 ::osl::MutexGuard aGuard( m_aMutex );
634 sal_Int32 nRet = 0;
635 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
636 if (pFormatter)
638 LanguageType eLang = lcl_GetLanguage( nLocale );
639 nRet = pFormatter->GetFormatIndex( (NfIndexTableOffset)nIndex, eLang );
641 else
642 throw uno::RuntimeException();
644 return nRet;
647 sal_Bool SAL_CALL SvNumberFormatsObj::isTypeCompatible( sal_Int16 nOldType, sal_Int16 nNewType )
648 throw(uno::RuntimeException)
650 ::osl::MutexGuard aGuard( m_aMutex );
652 sal_Bool bRet = sal_False;
653 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
654 if (pFormatter)
655 bRet = pFormatter->IsCompatible( nOldType, nNewType );
656 else
657 throw uno::RuntimeException();
659 return bRet;
662 sal_Int32 SAL_CALL SvNumberFormatsObj::getFormatForLocale( sal_Int32 nKey, const lang::Locale& nLocale )
663 throw(uno::RuntimeException)
665 ::osl::MutexGuard aGuard( m_aMutex );
667 sal_Int32 nRet = 0;
668 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
669 if (pFormatter)
671 LanguageType eLang = lcl_GetLanguage( nLocale );
672 nRet = pFormatter->GetFormatForLanguageIfBuiltIn(nKey, eLang);
674 else
675 throw uno::RuntimeException();
677 return nRet;
680 // XServiceInfo
682 OUString SAL_CALL SvNumberFormatsObj::getImplementationName()
683 throw(uno::RuntimeException)
685 return OUString("SvNumberFormatsObj");
688 sal_Bool SAL_CALL SvNumberFormatsObj::supportsService( const OUString& ServiceName )
689 throw(uno::RuntimeException)
691 return ( ServiceName.compareToAscii(SERVICENAME_NUMBERFORMATS) == 0 );
694 uno::Sequence<OUString> SAL_CALL SvNumberFormatsObj::getSupportedServiceNames()
695 throw(uno::RuntimeException)
697 uno::Sequence<OUString> aRet(1);
698 OUString* pArray = aRet.getArray();
699 pArray[0] = OUString(SERVICENAME_NUMBERFORMATS);
700 return aRet;
703 //------------------------------------------------------------------------
705 SvNumberFormatObj::SvNumberFormatObj( SvNumberFormatsSupplierObj& rParent, sal_uLong nK, const ::comphelper::SharedMutex& _rMutex )
706 :rSupplier( rParent )
707 ,nKey( nK )
708 ,m_aMutex( _rMutex )
710 rSupplier.acquire();
713 SvNumberFormatObj::~SvNumberFormatObj()
715 rSupplier.release();
718 // XPropertySet
720 uno::Reference<beans::XPropertySetInfo> SAL_CALL SvNumberFormatObj::getPropertySetInfo()
721 throw(uno::RuntimeException)
723 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
724 static uno::Reference<beans::XPropertySetInfo> aRef =
725 new SfxItemPropertySetInfo( lcl_GetNumberFormatPropertyMap() );
726 return aRef;
729 void SAL_CALL SvNumberFormatObj::setPropertyValue( const OUString&,
730 const uno::Any& )
731 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
732 lang::IllegalArgumentException, lang::WrappedTargetException,
733 uno::RuntimeException)
735 throw beans::UnknownPropertyException(); // everything is read-only
738 uno::Any SAL_CALL SvNumberFormatObj::getPropertyValue( const OUString& aPropertyName )
739 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
740 uno::RuntimeException)
742 ::osl::MutexGuard aGuard( m_aMutex );
744 uno::Any aRet;
745 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
746 const SvNumberformat* pFormat = pFormatter ? pFormatter->GetEntry(nKey) : NULL;
747 if (pFormat)
749 bool bThousand, bRed;
750 sal_uInt16 nDecimals, nLeading;
752 if (aPropertyName.equalsAscii( PROPERTYNAME_FMTSTR ))
754 aRet <<= OUString( pFormat->GetFormatstring() );
756 else if (aPropertyName.equalsAscii( PROPERTYNAME_LOCALE ))
758 lang::Locale aLocale( LanguageTag( pFormat->GetLanguage()).getLocale());
759 aRet <<= aLocale;
761 else if (aPropertyName.equalsAscii( PROPERTYNAME_TYPE ))
763 aRet <<= (sal_Int16)( pFormat->GetType() );
765 else if (aPropertyName.equalsAscii( PROPERTYNAME_COMMENT ))
767 aRet <<= OUString( pFormat->GetComment() );
769 else if (aPropertyName.equalsAscii( PROPERTYNAME_STDFORM ))
771 //! SvNumberformat Member bStandard rausreichen?
772 sal_Bool bStandard = ( ( nKey % SV_COUNTRY_LANGUAGE_OFFSET ) == 0 );
773 aRet.setValue( &bStandard, getBooleanCppuType() );
775 else if (aPropertyName.equalsAscii( PROPERTYNAME_USERDEF ))
777 sal_Bool bUserDef = ( ( pFormat->GetType() & NUMBERFORMAT_DEFINED ) != 0 );
778 aRet.setValue( &bUserDef, getBooleanCppuType() );
780 else if (aPropertyName.equalsAscii( PROPERTYNAME_DECIMALS ))
782 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
783 aRet <<= (sal_Int16)( nDecimals );
785 else if (aPropertyName.equalsAscii( PROPERTYNAME_LEADING ))
787 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
788 aRet <<= (sal_Int16)( nLeading );
790 else if (aPropertyName.equalsAscii( PROPERTYNAME_NEGRED ))
792 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
793 aRet.setValue( &bRed, getBooleanCppuType() );
795 else if (aPropertyName.equalsAscii( PROPERTYNAME_THOUS ))
797 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
798 aRet.setValue( &bThousand, getBooleanCppuType() );
800 else if (aPropertyName.equalsAscii( PROPERTYNAME_CURRSYM ))
802 OUString aSymbol, aExt;
803 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
804 aRet <<= aSymbol;
806 else if (aPropertyName.equalsAscii( PROPERTYNAME_CURREXT ))
808 OUString aSymbol, aExt;
809 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
810 aRet <<= aExt;
812 else if (aPropertyName.equalsAscii( PROPERTYNAME_CURRABB ))
814 OUString aSymbol, aExt;
815 bool bBank = false;
816 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
817 const NfCurrencyEntry* pCurr = pFormatter->GetCurrencyEntry( bBank,
818 aSymbol, aExt, pFormat->GetLanguage() );
819 if ( pCurr )
820 aRet <<= OUString( pCurr->GetBankSymbol() );
821 else
822 aRet <<= OUString();
824 else
825 throw beans::UnknownPropertyException();
827 else
828 throw uno::RuntimeException();
830 return aRet;
833 void SAL_CALL SvNumberFormatObj::addPropertyChangeListener( const OUString&,
834 const uno::Reference<beans::XPropertyChangeListener>&)
835 throw(beans::UnknownPropertyException,
836 lang::WrappedTargetException, uno::RuntimeException)
838 OSL_FAIL("not implemented");
841 void SAL_CALL SvNumberFormatObj::removePropertyChangeListener( const OUString&,
842 const uno::Reference<beans::XPropertyChangeListener>&)
843 throw(beans::UnknownPropertyException,
844 lang::WrappedTargetException, uno::RuntimeException)
846 OSL_FAIL("not implemented");
849 void SAL_CALL SvNumberFormatObj::addVetoableChangeListener( const OUString&,
850 const uno::Reference<beans::XVetoableChangeListener>&)
851 throw(beans::UnknownPropertyException,
852 lang::WrappedTargetException, uno::RuntimeException)
854 OSL_FAIL("not implemented");
857 void SAL_CALL SvNumberFormatObj::removeVetoableChangeListener( const OUString&,
858 const uno::Reference<beans::XVetoableChangeListener>&)
859 throw(beans::UnknownPropertyException,
860 lang::WrappedTargetException, uno::RuntimeException)
862 OSL_FAIL("not implemented");
865 // XPropertyAccess
867 uno::Sequence<beans::PropertyValue> SAL_CALL SvNumberFormatObj::getPropertyValues()
868 throw(uno::RuntimeException)
870 ::osl::MutexGuard aGuard( m_aMutex );
872 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
873 const SvNumberformat* pFormat = pFormatter ? pFormatter->GetEntry(nKey) : NULL;
874 if (pFormat)
876 OUString aSymbol, aExt;
877 OUString aAbb;
878 bool bBank = false;
879 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
880 const NfCurrencyEntry* pCurr = pFormatter->GetCurrencyEntry( bBank,
881 aSymbol, aExt, pFormat->GetLanguage() );
882 if ( pCurr )
883 aAbb = pCurr->GetBankSymbol();
885 OUString aFmtStr = pFormat->GetFormatstring();
886 OUString aComment = pFormat->GetComment();
887 sal_Bool bStandard = ( ( nKey % SV_COUNTRY_LANGUAGE_OFFSET ) == 0 );
888 //! SvNumberformat Member bStandard rausreichen?
889 sal_Bool bUserDef = ( ( pFormat->GetType() & NUMBERFORMAT_DEFINED ) != 0 );
890 bool bThousand, bRed;
891 sal_uInt16 nDecimals, nLeading;
892 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
893 lang::Locale aLocale( LanguageTag( pFormat->GetLanguage()).getLocale());
895 uno::Sequence<beans::PropertyValue> aSeq(13);
896 beans::PropertyValue* pArray = aSeq.getArray();
898 pArray[0].Name = PROPERTYNAME_FMTSTR;
899 pArray[0].Value <<= aFmtStr;
900 pArray[1].Name = PROPERTYNAME_LOCALE;
901 pArray[1].Value <<= aLocale;
902 pArray[2].Name = PROPERTYNAME_TYPE;
903 pArray[2].Value <<= (sal_Int16)( pFormat->GetType() );
904 pArray[3].Name = PROPERTYNAME_COMMENT;
905 pArray[3].Value <<= aComment;
906 pArray[4].Name = PROPERTYNAME_STDFORM;
907 pArray[4].Value.setValue( &bStandard, getBooleanCppuType() );
908 pArray[5].Name = PROPERTYNAME_USERDEF;
909 pArray[5].Value.setValue( &bUserDef, getBooleanCppuType() );
910 pArray[6].Name = PROPERTYNAME_DECIMALS;
911 pArray[6].Value <<= (sal_Int16)( nDecimals );
912 pArray[7].Name = PROPERTYNAME_LEADING;
913 pArray[7].Value <<= (sal_Int16)( nLeading );
914 pArray[8].Name = PROPERTYNAME_NEGRED;
915 pArray[8].Value.setValue( &bRed, getBooleanCppuType() );
916 pArray[9].Name = PROPERTYNAME_THOUS;
917 pArray[9].Value.setValue( &bThousand, getBooleanCppuType() );
918 pArray[10].Name = PROPERTYNAME_CURRSYM;
919 pArray[10].Value <<= aSymbol;
920 pArray[11].Name = PROPERTYNAME_CURREXT;
921 pArray[11].Value <<= aExt;
922 pArray[12].Name = PROPERTYNAME_CURRABB;
923 pArray[12].Value <<= aAbb;
925 return aSeq;
927 else
928 throw uno::RuntimeException();
931 void SAL_CALL SvNumberFormatObj::setPropertyValues( const uno::Sequence<beans::PropertyValue>& )
932 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
933 lang::IllegalArgumentException, lang::WrappedTargetException,
934 uno::RuntimeException)
936 throw beans::UnknownPropertyException(); // everything is read-only
939 // XServiceInfo
941 OUString SAL_CALL SvNumberFormatObj::getImplementationName()
942 throw(uno::RuntimeException)
944 return OUString("SvNumberFormatObj");
947 sal_Bool SAL_CALL SvNumberFormatObj::supportsService( const OUString& ServiceName )
948 throw(uno::RuntimeException)
950 return ( ServiceName.compareToAscii(SERVICENAME_NUMBERFORMAT) == 0 );
953 uno::Sequence<OUString> SAL_CALL SvNumberFormatObj::getSupportedServiceNames()
954 throw(uno::RuntimeException)
956 uno::Sequence<OUString> aRet(1);
957 OUString* pArray = aRet.getArray();
958 pArray[0] = SERVICENAME_NUMBERFORMAT;
959 return aRet;
962 //------------------------------------------------------------------------
964 SvNumberFormatSettingsObj::SvNumberFormatSettingsObj( SvNumberFormatsSupplierObj& rParent, const ::comphelper::SharedMutex& _rMutex )
965 :rSupplier( rParent )
966 ,m_aMutex( _rMutex )
968 rSupplier.acquire();
971 SvNumberFormatSettingsObj::~SvNumberFormatSettingsObj()
973 rSupplier.release();
976 // XPropertySet
978 uno::Reference<beans::XPropertySetInfo> SAL_CALL SvNumberFormatSettingsObj::getPropertySetInfo()
979 throw(uno::RuntimeException)
981 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
982 static uno::Reference<beans::XPropertySetInfo> aRef =
983 new SfxItemPropertySetInfo( lcl_GetNumberSettingsPropertyMap() );
984 return aRef;
987 void SAL_CALL SvNumberFormatSettingsObj::setPropertyValue( const OUString& aPropertyName,
988 const uno::Any& aValue )
989 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
990 lang::IllegalArgumentException, lang::WrappedTargetException,
991 uno::RuntimeException)
993 ::osl::MutexGuard aGuard( m_aMutex );
995 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
996 if (pFormatter)
998 if (aPropertyName.equalsAscii( PROPERTYNAME_NOZERO ))
1000 // operator >>= shouldn't be used for bool (?)
1001 if ( aValue.getValueTypeClass() == uno::TypeClass_BOOLEAN )
1002 pFormatter->SetNoZero( *(sal_Bool*)aValue.getValue() );
1004 else if (aPropertyName.equalsAscii( PROPERTYNAME_NULLDATE ))
1006 util::Date aDate;
1007 if ( aValue >>= aDate )
1008 pFormatter->ChangeNullDate( aDate.Day, aDate.Month, aDate.Year );
1010 else if (aPropertyName.equalsAscii( PROPERTYNAME_STDDEC ))
1012 sal_Int16 nInt16 = sal_Int16();
1013 if ( aValue >>= nInt16 )
1014 pFormatter->ChangeStandardPrec( nInt16 );
1016 else if (aPropertyName.equalsAscii( PROPERTYNAME_TWODIGIT ))
1018 sal_Int16 nInt16 = sal_Int16();
1019 if ( aValue >>= nInt16 )
1020 pFormatter->SetYear2000( nInt16 );
1022 else
1023 throw beans::UnknownPropertyException();
1025 rSupplier.SettingsChanged();
1027 else
1028 throw uno::RuntimeException();
1031 uno::Any SAL_CALL SvNumberFormatSettingsObj::getPropertyValue( const OUString& aPropertyName )
1032 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
1033 uno::RuntimeException)
1035 ::osl::MutexGuard aGuard( m_aMutex );
1037 uno::Any aRet;
1038 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
1039 if (pFormatter)
1041 if (aPropertyName.equalsAscii( PROPERTYNAME_NOZERO ))
1043 sal_Bool bNoZero = pFormatter->GetNoZero();
1044 aRet.setValue( &bNoZero, getBooleanCppuType() );
1046 else if (aPropertyName.equalsAscii( PROPERTYNAME_NULLDATE ))
1048 Date* pDate = pFormatter->GetNullDate();
1049 if (pDate)
1051 util::Date aUnoDate( pDate->GetDay(), pDate->GetMonth(), pDate->GetYear() );
1052 aRet <<= aUnoDate;
1055 else if (aPropertyName.equalsAscii( PROPERTYNAME_STDDEC ))
1056 aRet <<= (sal_Int16)( pFormatter->GetStandardPrec() );
1057 else if (aPropertyName.equalsAscii( PROPERTYNAME_TWODIGIT ))
1058 aRet <<= (sal_Int16)( pFormatter->GetYear2000() );
1059 else
1060 throw beans::UnknownPropertyException();
1062 else
1063 throw uno::RuntimeException();
1065 return aRet;
1068 void SAL_CALL SvNumberFormatSettingsObj::addPropertyChangeListener( const OUString&,
1069 const uno::Reference<beans::XPropertyChangeListener>&)
1070 throw(beans::UnknownPropertyException,
1071 lang::WrappedTargetException, uno::RuntimeException)
1073 OSL_FAIL("not implemented");
1076 void SAL_CALL SvNumberFormatSettingsObj::removePropertyChangeListener( const OUString&,
1077 const uno::Reference<beans::XPropertyChangeListener>&)
1078 throw(beans::UnknownPropertyException,
1079 lang::WrappedTargetException, uno::RuntimeException)
1081 OSL_FAIL("not implemented");
1084 void SAL_CALL SvNumberFormatSettingsObj::addVetoableChangeListener( const OUString&,
1085 const uno::Reference<beans::XVetoableChangeListener>&)
1086 throw(beans::UnknownPropertyException,
1087 lang::WrappedTargetException, uno::RuntimeException)
1089 OSL_FAIL("not implemented");
1092 void SAL_CALL SvNumberFormatSettingsObj::removeVetoableChangeListener( const OUString&,
1093 const uno::Reference<beans::XVetoableChangeListener>&)
1094 throw(beans::UnknownPropertyException,
1095 lang::WrappedTargetException, uno::RuntimeException)
1097 OSL_FAIL("not implemented");
1100 // XServiceInfo
1102 OUString SAL_CALL SvNumberFormatSettingsObj::getImplementationName()
1103 throw(uno::RuntimeException)
1105 return OUString("SvNumberFormatSettingsObj");
1108 sal_Bool SAL_CALL SvNumberFormatSettingsObj::supportsService( const OUString& ServiceName )
1109 throw(uno::RuntimeException)
1111 return ( ServiceName.compareToAscii(SERVICENAME_NUMBERSETTINGS) == 0 );
1114 uno::Sequence<OUString> SAL_CALL SvNumberFormatSettingsObj::getSupportedServiceNames()
1115 throw(uno::RuntimeException)
1117 uno::Sequence<OUString> aRet(1);
1118 OUString* pArray = aRet.getArray();
1119 pArray[0] = SERVICENAME_NUMBERSETTINGS;
1120 return aRet;
1124 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */