Update ooo320-m1
[ooovba.git] / svtools / source / numbers / numfmuno.cxx
blobcf2b0125d784f493f87474fb94296940c8c46967
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: numfmuno.cxx,v $
10 * $Revision: 1.15 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
33 #ifndef GCC
34 #endif
36 #include <vcl/svapp.hxx>
37 #include <tools/color.hxx>
38 #include <tools/debug.hxx>
39 #include <i18npool/mslangid.hxx>
40 #include <vos/mutex.hxx>
41 #include <rtl/ustring.hxx>
43 #include <com/sun/star/util/Date.hpp>
44 #include <com/sun/star/beans/PropertyAttribute.hpp>
46 #include "numfmuno.hxx"
47 #include "numuno.hxx"
48 #include <svtools/zforlist.hxx>
49 #include <svtools/zformat.hxx>
50 #include <svtools/itemprop.hxx>
52 using namespace com::sun::star;
54 //------------------------------------------------------------------------
56 #define SERVICENAME_NUMBERFORMATTER "com.sun.star.util.NumberFormatter"
57 #define SERVICENAME_NUMBERSETTINGS "com.sun.star.util.NumberFormatSettings"
58 #define SERVICENAME_NUMBERFORMATS "com.sun.star.util.NumberFormats"
59 #define SERVICENAME_NUMBERFORMAT "com.sun.star.util.NumberFormatProperties"
61 //------------------------------------------------------------------------
63 #define PROPERTYNAME_FMTSTR "FormatString"
64 #define PROPERTYNAME_LOCALE "Locale"
65 #define PROPERTYNAME_TYPE "Type"
66 #define PROPERTYNAME_COMMENT "Comment"
67 #define PROPERTYNAME_CURREXT "CurrencyExtension"
68 #define PROPERTYNAME_CURRSYM "CurrencySymbol"
69 #define PROPERTYNAME_CURRABB "CurrencyAbbreviation"
70 #define PROPERTYNAME_DECIMALS "Decimals"
71 #define PROPERTYNAME_LEADING "LeadingZeros"
72 #define PROPERTYNAME_NEGRED "NegativeRed"
73 #define PROPERTYNAME_STDFORM "StandardFormat"
74 #define PROPERTYNAME_THOUS "ThousandsSeparator"
75 #define PROPERTYNAME_USERDEF "UserDefined"
77 #define PROPERTYNAME_NOZERO "NoZero"
78 #define PROPERTYNAME_NULLDATE "NullDate"
79 #define PROPERTYNAME_STDDEC "StandardDecimals"
80 #define PROPERTYNAME_TWODIGIT "TwoDigitDateStart"
82 //------------------------------------------------------------------------
84 // alles ohne Which-ID, Map nur fuer PropertySetInfo
86 const SfxItemPropertyMapEntry* lcl_GetNumberFormatPropertyMap()
88 static SfxItemPropertyMapEntry aNumberFormatPropertyMap_Impl[] =
90 {MAP_CHAR_LEN(PROPERTYNAME_FMTSTR), 0, &getCppuType((rtl::OUString*)0),beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
91 {MAP_CHAR_LEN(PROPERTYNAME_LOCALE), 0, &getCppuType((lang::Locale*)0),beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
92 {MAP_CHAR_LEN(PROPERTYNAME_TYPE), 0, &getCppuType((sal_Int16*)0), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
93 {MAP_CHAR_LEN(PROPERTYNAME_COMMENT), 0, &getCppuType((rtl::OUString*)0),beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
94 {MAP_CHAR_LEN(PROPERTYNAME_CURREXT), 0, &getCppuType((rtl::OUString*)0),beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
95 {MAP_CHAR_LEN(PROPERTYNAME_CURRSYM), 0, &getCppuType((rtl::OUString*)0),beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
96 {MAP_CHAR_LEN(PROPERTYNAME_DECIMALS), 0, &getCppuType((sal_Int16*)0), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
97 {MAP_CHAR_LEN(PROPERTYNAME_LEADING), 0, &getCppuType((sal_Int16*)0), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
98 {MAP_CHAR_LEN(PROPERTYNAME_NEGRED), 0, &getBooleanCppuType(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
99 {MAP_CHAR_LEN(PROPERTYNAME_STDFORM), 0, &getBooleanCppuType(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
100 {MAP_CHAR_LEN(PROPERTYNAME_THOUS), 0, &getBooleanCppuType(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
101 {MAP_CHAR_LEN(PROPERTYNAME_USERDEF), 0, &getBooleanCppuType(), beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
102 {MAP_CHAR_LEN(PROPERTYNAME_CURRABB), 0, &getCppuType((rtl::OUString*)0),beans::PropertyAttribute::BOUND | beans::PropertyAttribute::READONLY, 0},
103 {0,0,0,0,0,0}
105 return aNumberFormatPropertyMap_Impl;
108 const SfxItemPropertyMapEntry* lcl_GetNumberSettingsPropertyMap()
110 static SfxItemPropertyMapEntry aNumberSettingsPropertyMap_Impl[] =
112 {MAP_CHAR_LEN(PROPERTYNAME_NOZERO), 0, &getBooleanCppuType(), beans::PropertyAttribute::BOUND, 0},
113 {MAP_CHAR_LEN(PROPERTYNAME_NULLDATE), 0, &getCppuType((util::Date*)0), beans::PropertyAttribute::BOUND, 0},
114 {MAP_CHAR_LEN(PROPERTYNAME_STDDEC), 0, &getCppuType((sal_Int16*)0), beans::PropertyAttribute::BOUND, 0},
115 {MAP_CHAR_LEN(PROPERTYNAME_TWODIGIT), 0, &getCppuType((sal_Int16*)0), beans::PropertyAttribute::BOUND, 0},
116 {0,0,0,0,0,0}
118 return aNumberSettingsPropertyMap_Impl;
121 //----------------------------------------------------------------------------------------
123 LanguageType lcl_GetLanguage( const lang::Locale& rLocale )
125 // empty language -> LANGUAGE_SYSTEM
126 if ( rLocale.Language.getLength() == 0 )
127 return LANGUAGE_SYSTEM;
129 LanguageType eRet = MsLangId::convertLocaleToLanguage( rLocale );
130 if ( eRet == LANGUAGE_NONE )
131 eRet = LANGUAGE_SYSTEM; //! or throw an exception?
133 return eRet;
136 //----------------------------------------------------------------------------------------
138 SvNumberFormatterServiceObj::SvNumberFormatterServiceObj()
139 :m_aMutex()
143 SvNumberFormatterServiceObj::~SvNumberFormatterServiceObj()
147 com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL SvNumberFormatterServiceObj_CreateInstance( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& )
149 return ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface >( ( ::cppu::OWeakObject* ) new SvNumberFormatterServiceObj );
152 // XNumberFormatter
154 void SAL_CALL SvNumberFormatterServiceObj::attachNumberFormatsSupplier(
155 const uno::Reference<util::XNumberFormatsSupplier>& _xSupplier )
156 throw(uno::RuntimeException)
158 ::rtl::Reference< SvNumberFormatsSupplierObj > xAutoReleaseOld;
160 // SYNCHRONIZED ->
162 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
164 SvNumberFormatsSupplierObj* pNew = SvNumberFormatsSupplierObj::getImplementation( _xSupplier );
165 if (!pNew)
166 throw uno::RuntimeException(); // wrong object
168 xAutoReleaseOld = xSupplier;
170 xSupplier = pNew;
171 m_aMutex = xSupplier->getSharedMutex();
173 // <- SYNCHRONIZED
176 uno::Reference<util::XNumberFormatsSupplier> SAL_CALL
177 SvNumberFormatterServiceObj::getNumberFormatsSupplier()
178 throw(uno::RuntimeException)
180 ::osl::MutexGuard aGuard( m_aMutex );
181 return xSupplier.get();
184 sal_Int32 SAL_CALL SvNumberFormatterServiceObj::detectNumberFormat(
185 sal_Int32 nKey, const rtl::OUString& aString )
186 throw(util::NotNumericException, uno::RuntimeException)
188 ::osl::MutexGuard aGuard( m_aMutex );
190 INT32 nRet = 0;
191 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
192 if (pFormatter)
194 String aTemp = aString;
195 sal_uInt32 nUKey = nKey;
196 double fValue = 0.0;
197 if ( pFormatter->IsNumberFormat(aTemp, nUKey, fValue) )
198 nRet = nUKey;
199 else
200 throw util::NotNumericException();
202 else
203 throw uno::RuntimeException();
205 return nRet;
208 double SAL_CALL SvNumberFormatterServiceObj::convertStringToNumber(
209 sal_Int32 nKey, const rtl::OUString& aString )
210 throw(util::NotNumericException, uno::RuntimeException)
212 ::osl::MutexGuard aGuard( m_aMutex );
214 double fRet = 0.0;
215 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
216 if (pFormatter)
218 String aTemp = aString;
219 sal_uInt32 nUKey = nKey;
220 double fValue = 0.0;
221 if ( pFormatter->IsNumberFormat(aTemp, nUKey, fValue) )
222 fRet = fValue;
223 else
224 throw util::NotNumericException();
226 else
227 throw uno::RuntimeException();
229 return fRet;
232 rtl::OUString SAL_CALL SvNumberFormatterServiceObj::convertNumberToString(
233 sal_Int32 nKey, double fValue ) throw(uno::RuntimeException)
235 ::osl::MutexGuard aGuard( m_aMutex );
237 String aRet;
238 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
239 if (pFormatter)
241 Color* pColor = NULL;
242 pFormatter->GetOutputString(fValue, nKey, aRet, &pColor);
244 else
245 throw uno::RuntimeException();
247 return aRet;
250 util::Color SAL_CALL SvNumberFormatterServiceObj::queryColorForNumber( sal_Int32 nKey,
251 double fValue, util::Color aDefaultColor )
252 throw(uno::RuntimeException)
254 ::osl::MutexGuard aGuard( m_aMutex );
256 util::Color nRet = aDefaultColor; // color = INT32
257 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
258 if (pFormatter)
260 String aStr;
261 Color* pColor = NULL;
262 pFormatter->GetOutputString(fValue, nKey, aStr, &pColor);
263 if (pColor)
264 nRet = pColor->GetColor();
265 // sonst Default behalten
267 else
268 throw uno::RuntimeException();
270 return nRet;
273 rtl::OUString SAL_CALL SvNumberFormatterServiceObj::formatString( sal_Int32 nKey,
274 const rtl::OUString& aString ) throw(uno::RuntimeException)
276 ::osl::MutexGuard aGuard( m_aMutex );
278 String aRet;
279 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
280 if (pFormatter)
282 String aTemp = aString;
283 Color* pColor = NULL;
284 pFormatter->GetOutputString(aTemp, nKey, aRet, &pColor);
286 else
287 throw uno::RuntimeException();
289 return aRet;
292 util::Color SAL_CALL SvNumberFormatterServiceObj::queryColorForString( sal_Int32 nKey,
293 const rtl::OUString& aString,util::Color aDefaultColor )
294 throw(uno::RuntimeException)
296 ::osl::MutexGuard aGuard( m_aMutex );
298 util::Color nRet = aDefaultColor; // color = INT32
299 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
300 if (pFormatter)
302 String aTemp = aString;
303 String aStr;
304 Color* pColor = NULL;
305 pFormatter->GetOutputString(aTemp, nKey, aStr, &pColor);
306 if (pColor)
307 nRet = pColor->GetColor();
308 // sonst Default behalten
310 else
311 throw uno::RuntimeException();
313 return nRet;
316 rtl::OUString SAL_CALL SvNumberFormatterServiceObj::getInputString( sal_Int32 nKey, double fValue )
317 throw(uno::RuntimeException)
319 ::osl::MutexGuard aGuard( m_aMutex );
321 String aRet;
322 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
323 if (pFormatter)
324 pFormatter->GetInputLineString(fValue, nKey, aRet);
325 else
326 throw uno::RuntimeException();
328 return aRet;
331 // XNumberFormatPreviewer
333 rtl::OUString SAL_CALL SvNumberFormatterServiceObj::convertNumberToPreviewString(
334 const rtl::OUString& aFormat, double fValue,
335 const lang::Locale& nLocale, sal_Bool bAllowEnglish )
336 throw(util::MalformedNumberFormatException, uno::RuntimeException)
338 ::osl::MutexGuard aGuard( m_aMutex );
340 String aRet;
341 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
342 if (pFormatter)
344 String aOutString;
345 String aFormString = aFormat;
346 LanguageType eLang = lcl_GetLanguage( nLocale );
347 Color* pColor = NULL;
349 BOOL bOk;
350 if ( bAllowEnglish )
351 bOk = pFormatter->GetPreviewStringGuess(
352 aFormString, fValue, aOutString, &pColor, eLang );
353 else
354 bOk = pFormatter->GetPreviewString(
355 aFormString, fValue, aOutString, &pColor, eLang );
357 if (bOk)
358 aRet = aOutString;
359 else
360 throw util::MalformedNumberFormatException();
362 else
363 throw uno::RuntimeException();
365 return aRet;
368 util::Color SAL_CALL SvNumberFormatterServiceObj::queryPreviewColorForNumber(
369 const rtl::OUString& aFormat, double fValue,
370 const lang::Locale& nLocale, sal_Bool bAllowEnglish,
371 util::Color aDefaultColor )
372 throw(util::MalformedNumberFormatException, uno::RuntimeException)
374 ::osl::MutexGuard aGuard( m_aMutex );
376 util::Color nRet = aDefaultColor; // color = INT32
377 SvNumberFormatter* pFormatter = xSupplier.is() ? xSupplier->GetNumberFormatter() : NULL;
378 if (pFormatter)
380 String aOutString;
381 String aFormString = aFormat;
382 LanguageType eLang = lcl_GetLanguage( nLocale );
383 Color* pColor = NULL;
385 BOOL bOk;
386 if ( bAllowEnglish )
387 bOk = pFormatter->GetPreviewStringGuess(
388 aFormString, fValue, aOutString, &pColor, eLang );
389 else
390 bOk = pFormatter->GetPreviewString(
391 aFormString, fValue, aOutString, &pColor, eLang );
393 if (bOk)
395 if (pColor)
396 nRet = pColor->GetColor();
397 // sonst Default behalten
399 else
400 throw util::MalformedNumberFormatException();
402 else
403 throw uno::RuntimeException();
405 return nRet;
408 // XServiceInfo
410 rtl::OUString SAL_CALL SvNumberFormatterServiceObj::getImplementationName()
411 throw(uno::RuntimeException)
413 return rtl::OUString::createFromAscii("com.sun.star.uno.util.numbers.SvNumberFormatterServiceObject");
416 sal_Bool SAL_CALL SvNumberFormatterServiceObj::supportsService( const rtl::OUString& ServiceName )
417 throw(uno::RuntimeException)
419 return ( ServiceName.compareToAscii(SERVICENAME_NUMBERFORMATTER) == 0 );
422 uno::Sequence<rtl::OUString> SAL_CALL SvNumberFormatterServiceObj::getSupportedServiceNames()
423 throw(uno::RuntimeException)
425 uno::Sequence<rtl::OUString> aRet(1);
426 rtl::OUString* pArray = aRet.getArray();
427 pArray[0] = rtl::OUString::createFromAscii(SERVICENAME_NUMBERFORMATTER);
428 return aRet;
431 //------------------------------------------------------------------------
433 SvNumberFormatsObj::SvNumberFormatsObj( SvNumberFormatsSupplierObj& _rParent, ::comphelper::SharedMutex& _rMutex )
434 :rSupplier( _rParent )
435 ,m_aMutex( _rMutex )
437 rSupplier.acquire();
440 SvNumberFormatsObj::~SvNumberFormatsObj()
442 rSupplier.release();
445 // XNumberFormats
447 uno::Reference<beans::XPropertySet> SAL_CALL SvNumberFormatsObj::getByKey( sal_Int32 nKey )
448 throw(uno::RuntimeException)
450 ::osl::MutexGuard aGuard( m_aMutex );
452 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
453 const SvNumberformat* pFormat = pFormatter ? pFormatter->GetEntry(nKey) : NULL;
454 if (pFormat)
455 return new SvNumberFormatObj( rSupplier, nKey, m_aMutex );
456 else
457 throw uno::RuntimeException();
460 uno::Sequence<sal_Int32> SAL_CALL SvNumberFormatsObj::queryKeys( sal_Int16 nType,
461 const lang::Locale& nLocale, sal_Bool bCreate )
462 throw(uno::RuntimeException)
464 ::osl::MutexGuard aGuard( m_aMutex );
466 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
467 if ( pFormatter )
469 sal_uInt32 nIndex = 0;
470 LanguageType eLang = lcl_GetLanguage( nLocale );
471 SvNumberFormatTable& rTable = bCreate ?
472 pFormatter->ChangeCL( nType, nIndex, eLang ) :
473 pFormatter->GetEntryTable( nType, nIndex, eLang );
474 sal_uInt32 nCount = rTable.Count();
475 uno::Sequence<sal_Int32> aSeq(nCount);
476 sal_Int32* pAry = aSeq.getArray();
477 for (sal_uInt32 i=0; i<nCount; i++)
478 pAry[i] = rTable.GetObjectKey( i );
480 return aSeq;
482 else
483 throw uno::RuntimeException();
486 sal_Int32 SAL_CALL SvNumberFormatsObj::queryKey( const rtl::OUString& aFormat,
487 const lang::Locale& nLocale, sal_Bool bScan )
488 throw(uno::RuntimeException)
490 ::osl::MutexGuard aGuard( m_aMutex );
492 INT32 nRet = 0;
493 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
494 if (pFormatter)
496 String aFormStr = aFormat;
497 LanguageType eLang = lcl_GetLanguage( nLocale );
498 if (bScan)
500 //! irgendwas muss hier noch passieren...
502 nRet = pFormatter->GetEntryKey( aFormat, eLang );
504 else
505 throw uno::RuntimeException();
507 return nRet;
510 sal_Int32 SAL_CALL SvNumberFormatsObj::addNew( const rtl::OUString& aFormat,
511 const lang::Locale& nLocale )
512 throw(util::MalformedNumberFormatException, uno::RuntimeException)
514 ::osl::MutexGuard aGuard( m_aMutex );
516 INT32 nRet = 0;
517 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
518 if (pFormatter)
520 String aFormStr = aFormat;
521 LanguageType eLang = lcl_GetLanguage( nLocale );
522 sal_uInt32 nKey = 0;
523 xub_StrLen nCheckPos = 0;
524 short nType = 0;
525 BOOL bOk = pFormatter->PutEntry( aFormStr, nCheckPos, nType, nKey, eLang );
526 if (bOk)
527 nRet = nKey;
528 else if (nCheckPos)
530 throw util::MalformedNumberFormatException(); // ungueltiges Format
532 else
533 throw uno::RuntimeException(); // anderer Fehler (z.B. schon vorhanden)
535 else
536 throw uno::RuntimeException();
538 return nRet;
541 sal_Int32 SAL_CALL SvNumberFormatsObj::addNewConverted( const rtl::OUString& aFormat,
542 const lang::Locale& nLocale, const lang::Locale& nNewLocale )
543 throw(util::MalformedNumberFormatException, uno::RuntimeException)
545 ::osl::MutexGuard aGuard( m_aMutex );
547 INT32 nRet = 0;
548 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
549 if (pFormatter)
551 String aFormStr = aFormat;
552 LanguageType eLang = lcl_GetLanguage( nLocale );
553 LanguageType eNewLang = lcl_GetLanguage( nNewLocale );
554 sal_uInt32 nKey = 0;
555 xub_StrLen nCheckPos = 0;
556 short nType = 0;
557 BOOL bOk = pFormatter->PutandConvertEntry( aFormStr, nCheckPos, nType, nKey, eLang, eNewLang );
558 if (bOk || nKey > 0)
559 nRet = nKey;
560 else if (nCheckPos)
562 throw util::MalformedNumberFormatException(); // ungueltiges Format
564 else
565 throw uno::RuntimeException(); // anderer Fehler (z.B. schon vorhanden)
567 else
568 throw uno::RuntimeException();
570 return nRet;
573 void SAL_CALL SvNumberFormatsObj::removeByKey( sal_Int32 nKey ) throw(uno::RuntimeException)
575 ::osl::MutexGuard aGuard( m_aMutex );
576 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
578 if (pFormatter)
580 pFormatter->DeleteEntry(nKey);
581 rSupplier.NumberFormatDeleted(nKey); // Benachrichtigung fuers Dokument
585 rtl::OUString SAL_CALL SvNumberFormatsObj::generateFormat( sal_Int32 nBaseKey,
586 const lang::Locale& nLocale, sal_Bool bThousands,
587 sal_Bool bRed, sal_Int16 nDecimals, sal_Int16 nLeading )
588 throw(uno::RuntimeException)
590 ::osl::MutexGuard aGuard( m_aMutex );
592 String aRet;
593 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
594 if (pFormatter)
596 LanguageType eLang = lcl_GetLanguage( nLocale );
597 pFormatter->GenerateFormat( aRet, nBaseKey, eLang, bThousands, bRed, nDecimals, nLeading );
599 else
600 throw uno::RuntimeException();
602 return aRet;
605 // XNumberFormatTypes
607 sal_Int32 SAL_CALL SvNumberFormatsObj::getStandardIndex( const lang::Locale& nLocale )
608 throw(uno::RuntimeException)
610 ::osl::MutexGuard aGuard( m_aMutex );
612 INT32 nRet = 0;
613 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
614 if (pFormatter)
616 LanguageType eLang = lcl_GetLanguage( nLocale );
617 nRet = pFormatter->GetStandardIndex(eLang);
619 else
620 throw uno::RuntimeException();
622 return nRet;
625 sal_Int32 SAL_CALL SvNumberFormatsObj::getStandardFormat( sal_Int16 nType, const lang::Locale& nLocale )
626 throw(uno::RuntimeException)
628 ::osl::MutexGuard aGuard( m_aMutex );
630 INT32 nRet = 0;
631 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
632 if (pFormatter)
634 LanguageType eLang = lcl_GetLanguage( nLocale );
635 // mask out "defined" bit, so type from an existing number format
636 // can directly be used for getStandardFormat
637 nType &= ~NUMBERFORMAT_DEFINED;
638 nRet = pFormatter->GetStandardFormat(nType, eLang);
640 else
641 throw uno::RuntimeException();
643 return nRet;
646 sal_Int32 SAL_CALL SvNumberFormatsObj::getFormatIndex( sal_Int16 nIndex, const lang::Locale& nLocale )
647 throw(uno::RuntimeException)
649 ::osl::MutexGuard aGuard( m_aMutex );
651 INT32 nRet = 0;
652 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
653 if (pFormatter)
655 LanguageType eLang = lcl_GetLanguage( nLocale );
656 nRet = pFormatter->GetFormatIndex( (NfIndexTableOffset)nIndex, eLang );
658 else
659 throw uno::RuntimeException();
661 return nRet;
664 sal_Bool SAL_CALL SvNumberFormatsObj::isTypeCompatible( sal_Int16 nOldType, sal_Int16 nNewType )
665 throw(uno::RuntimeException)
667 ::osl::MutexGuard aGuard( m_aMutex );
669 BOOL bRet = FALSE;
670 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
671 if (pFormatter)
672 bRet = pFormatter->IsCompatible( nOldType, nNewType );
673 else
674 throw uno::RuntimeException();
676 return bRet;
679 sal_Int32 SAL_CALL SvNumberFormatsObj::getFormatForLocale( sal_Int32 nKey, const lang::Locale& nLocale )
680 throw(uno::RuntimeException)
682 ::osl::MutexGuard aGuard( m_aMutex );
684 INT32 nRet = 0;
685 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
686 if (pFormatter)
688 LanguageType eLang = lcl_GetLanguage( nLocale );
689 nRet = pFormatter->GetFormatForLanguageIfBuiltIn(nKey, eLang);
691 else
692 throw uno::RuntimeException();
694 return nRet;
697 // XServiceInfo
699 rtl::OUString SAL_CALL SvNumberFormatsObj::getImplementationName()
700 throw(uno::RuntimeException)
702 return rtl::OUString::createFromAscii("SvNumberFormatsObj");
705 sal_Bool SAL_CALL SvNumberFormatsObj::supportsService( const rtl::OUString& ServiceName )
706 throw(uno::RuntimeException)
708 return ( ServiceName.compareToAscii(SERVICENAME_NUMBERFORMATS) == 0 );
711 uno::Sequence<rtl::OUString> SAL_CALL SvNumberFormatsObj::getSupportedServiceNames()
712 throw(uno::RuntimeException)
714 uno::Sequence<rtl::OUString> aRet(1);
715 rtl::OUString* pArray = aRet.getArray();
716 pArray[0] = rtl::OUString::createFromAscii(SERVICENAME_NUMBERFORMATS);
717 return aRet;
720 //------------------------------------------------------------------------
722 SvNumberFormatObj::SvNumberFormatObj( SvNumberFormatsSupplierObj& rParent, ULONG nK, const ::comphelper::SharedMutex& _rMutex )
723 :rSupplier( rParent )
724 ,nKey( nK )
725 ,m_aMutex( _rMutex )
727 rSupplier.acquire();
730 SvNumberFormatObj::~SvNumberFormatObj()
732 rSupplier.release();
735 // XPropertySet
737 uno::Reference<beans::XPropertySetInfo> SAL_CALL SvNumberFormatObj::getPropertySetInfo()
738 throw(uno::RuntimeException)
740 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
741 static uno::Reference<beans::XPropertySetInfo> aRef =
742 new SfxItemPropertySetInfo( lcl_GetNumberFormatPropertyMap() );
743 return aRef;
746 void SAL_CALL SvNumberFormatObj::setPropertyValue( const rtl::OUString&,
747 const uno::Any& )
748 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
749 lang::IllegalArgumentException, lang::WrappedTargetException,
750 uno::RuntimeException)
752 throw beans::UnknownPropertyException(); // everything is read-only
755 uno::Any SAL_CALL SvNumberFormatObj::getPropertyValue( const rtl::OUString& aPropertyName )
756 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
757 uno::RuntimeException)
759 ::osl::MutexGuard aGuard( m_aMutex );
761 uno::Any aRet;
762 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
763 const SvNumberformat* pFormat = pFormatter ? pFormatter->GetEntry(nKey) : NULL;
764 if (pFormat)
766 BOOL bThousand, bRed;
767 USHORT nDecimals, nLeading;
769 String aString = aPropertyName;
770 if (aString.EqualsAscii( PROPERTYNAME_FMTSTR ))
772 aRet <<= rtl::OUString( pFormat->GetFormatstring() );
774 else if (aString.EqualsAscii( PROPERTYNAME_LOCALE ))
776 lang::Locale aLocale( MsLangId::convertLanguageToLocale(
777 pFormat->GetLanguage()));
778 aRet <<= aLocale;
780 else if (aString.EqualsAscii( PROPERTYNAME_TYPE ))
782 aRet <<= (sal_Int16)( pFormat->GetType() );
784 else if (aString.EqualsAscii( PROPERTYNAME_COMMENT ))
786 aRet <<= rtl::OUString( pFormat->GetComment() );
788 else if (aString.EqualsAscii( PROPERTYNAME_STDFORM ))
790 //! SvNumberformat Member bStandard rausreichen?
791 BOOL bStandard = ( ( nKey % SV_COUNTRY_LANGUAGE_OFFSET ) == 0 );
792 aRet.setValue( &bStandard, getBooleanCppuType() );
794 else if (aString.EqualsAscii( PROPERTYNAME_USERDEF ))
796 BOOL bUserDef = ( ( pFormat->GetType() & NUMBERFORMAT_DEFINED ) != 0 );
797 aRet.setValue( &bUserDef, getBooleanCppuType() );
799 else if (aString.EqualsAscii( PROPERTYNAME_DECIMALS ))
801 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
802 aRet <<= (sal_Int16)( nDecimals );
804 else if (aString.EqualsAscii( PROPERTYNAME_LEADING ))
806 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
807 aRet <<= (sal_Int16)( nLeading );
809 else if (aString.EqualsAscii( PROPERTYNAME_NEGRED ))
811 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
812 aRet.setValue( &bRed, getBooleanCppuType() );
814 else if (aString.EqualsAscii( PROPERTYNAME_THOUS ))
816 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
817 aRet.setValue( &bThousand, getBooleanCppuType() );
819 else if (aString.EqualsAscii( PROPERTYNAME_CURRSYM ))
821 String aSymbol, aExt;
822 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
823 aRet <<= rtl::OUString( aSymbol );
825 else if (aString.EqualsAscii( PROPERTYNAME_CURREXT ))
827 String aSymbol, aExt;
828 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
829 aRet <<= rtl::OUString( aExt );
831 else if (aString.EqualsAscii( PROPERTYNAME_CURRABB ))
833 String aSymbol, aExt;
834 BOOL bBank = FALSE;
835 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
836 const NfCurrencyEntry* pCurr = pFormatter->GetCurrencyEntry( bBank,
837 aSymbol, aExt, pFormat->GetLanguage() );
838 if ( pCurr )
839 aRet <<= rtl::OUString( pCurr->GetBankSymbol() );
840 else
841 aRet <<= rtl::OUString();
843 else
844 throw beans::UnknownPropertyException();
846 else
847 throw uno::RuntimeException();
849 return aRet;
852 void SAL_CALL SvNumberFormatObj::addPropertyChangeListener( const rtl::OUString&,
853 const uno::Reference<beans::XPropertyChangeListener>&)
854 throw(beans::UnknownPropertyException,
855 lang::WrappedTargetException, uno::RuntimeException)
857 DBG_ERROR("not implemented");
860 void SAL_CALL SvNumberFormatObj::removePropertyChangeListener( const rtl::OUString&,
861 const uno::Reference<beans::XPropertyChangeListener>&)
862 throw(beans::UnknownPropertyException,
863 lang::WrappedTargetException, uno::RuntimeException)
865 DBG_ERROR("not implemented");
868 void SAL_CALL SvNumberFormatObj::addVetoableChangeListener( const rtl::OUString&,
869 const uno::Reference<beans::XVetoableChangeListener>&)
870 throw(beans::UnknownPropertyException,
871 lang::WrappedTargetException, uno::RuntimeException)
873 DBG_ERROR("not implemented");
876 void SAL_CALL SvNumberFormatObj::removeVetoableChangeListener( const rtl::OUString&,
877 const uno::Reference<beans::XVetoableChangeListener>&)
878 throw(beans::UnknownPropertyException,
879 lang::WrappedTargetException, uno::RuntimeException)
881 DBG_ERROR("not implemented");
884 // XPropertyAccess
886 uno::Sequence<beans::PropertyValue> SAL_CALL SvNumberFormatObj::getPropertyValues()
887 throw(uno::RuntimeException)
889 ::osl::MutexGuard aGuard( m_aMutex );
891 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
892 const SvNumberformat* pFormat = pFormatter ? pFormatter->GetEntry(nKey) : NULL;
893 if (pFormat)
895 String aSymbol, aExt, aAbb;
896 BOOL bBank = FALSE;
897 pFormat->GetNewCurrencySymbol( aSymbol, aExt );
898 const NfCurrencyEntry* pCurr = pFormatter->GetCurrencyEntry( bBank,
899 aSymbol, aExt, pFormat->GetLanguage() );
900 if ( pCurr )
901 aAbb = pCurr->GetBankSymbol();
903 String aFmtStr = pFormat->GetFormatstring();
904 String aComment = pFormat->GetComment();
905 BOOL bStandard = ( ( nKey % SV_COUNTRY_LANGUAGE_OFFSET ) == 0 );
906 //! SvNumberformat Member bStandard rausreichen?
907 BOOL bUserDef = ( ( pFormat->GetType() & NUMBERFORMAT_DEFINED ) != 0 );
908 BOOL bThousand, bRed;
909 USHORT nDecimals, nLeading;
910 pFormat->GetFormatSpecialInfo( bThousand, bRed, nDecimals, nLeading );
911 lang::Locale aLocale( MsLangId::convertLanguageToLocale(
912 pFormat->GetLanguage()));
914 uno::Sequence<beans::PropertyValue> aSeq(13);
915 beans::PropertyValue* pArray = aSeq.getArray();
917 pArray[0].Name = rtl::OUString::createFromAscii( PROPERTYNAME_FMTSTR );
918 pArray[0].Value <<= rtl::OUString( aFmtStr );
919 pArray[1].Name = rtl::OUString::createFromAscii( PROPERTYNAME_LOCALE );
920 pArray[1].Value <<= aLocale;
921 pArray[2].Name = rtl::OUString::createFromAscii( PROPERTYNAME_TYPE );
922 pArray[2].Value <<= (sal_Int16)( pFormat->GetType() );
923 pArray[3].Name = rtl::OUString::createFromAscii( PROPERTYNAME_COMMENT );
924 pArray[3].Value <<= rtl::OUString( aComment );
925 pArray[4].Name = rtl::OUString::createFromAscii( PROPERTYNAME_STDFORM );
926 pArray[4].Value.setValue( &bStandard, getBooleanCppuType() );
927 pArray[5].Name = rtl::OUString::createFromAscii( PROPERTYNAME_USERDEF );
928 pArray[5].Value.setValue( &bUserDef, getBooleanCppuType() );
929 pArray[6].Name = rtl::OUString::createFromAscii( PROPERTYNAME_DECIMALS );
930 pArray[6].Value <<= (sal_Int16)( nDecimals );
931 pArray[7].Name = rtl::OUString::createFromAscii( PROPERTYNAME_LEADING );
932 pArray[7].Value <<= (sal_Int16)( nLeading );
933 pArray[8].Name = rtl::OUString::createFromAscii( PROPERTYNAME_NEGRED );
934 pArray[8].Value.setValue( &bRed, getBooleanCppuType() );
935 pArray[9].Name = rtl::OUString::createFromAscii( PROPERTYNAME_THOUS );
936 pArray[9].Value.setValue( &bThousand, getBooleanCppuType() );
937 pArray[10].Name = rtl::OUString::createFromAscii( PROPERTYNAME_CURRSYM );
938 pArray[10].Value <<= rtl::OUString( aSymbol );
939 pArray[11].Name = rtl::OUString::createFromAscii( PROPERTYNAME_CURREXT );
940 pArray[11].Value <<= rtl::OUString( aExt );
941 pArray[12].Name = rtl::OUString::createFromAscii( PROPERTYNAME_CURRABB );
942 pArray[12].Value <<= rtl::OUString( aAbb );
944 return aSeq;
946 else
947 throw uno::RuntimeException();
950 void SAL_CALL SvNumberFormatObj::setPropertyValues( const uno::Sequence<beans::PropertyValue>& )
951 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
952 lang::IllegalArgumentException, lang::WrappedTargetException,
953 uno::RuntimeException)
955 throw beans::UnknownPropertyException(); // everything is read-only
958 // XServiceInfo
960 rtl::OUString SAL_CALL SvNumberFormatObj::getImplementationName()
961 throw(uno::RuntimeException)
963 return rtl::OUString::createFromAscii("SvNumberFormatObj");
966 sal_Bool SAL_CALL SvNumberFormatObj::supportsService( const rtl::OUString& ServiceName )
967 throw(uno::RuntimeException)
969 return ( ServiceName.compareToAscii(SERVICENAME_NUMBERFORMAT) == 0 );
972 uno::Sequence<rtl::OUString> SAL_CALL SvNumberFormatObj::getSupportedServiceNames()
973 throw(uno::RuntimeException)
975 uno::Sequence<rtl::OUString> aRet(1);
976 rtl::OUString* pArray = aRet.getArray();
977 pArray[0] = rtl::OUString::createFromAscii(SERVICENAME_NUMBERFORMAT);
978 return aRet;
981 //------------------------------------------------------------------------
983 SvNumberFormatSettingsObj::SvNumberFormatSettingsObj( SvNumberFormatsSupplierObj& rParent, const ::comphelper::SharedMutex& _rMutex )
984 :rSupplier( rParent )
985 ,m_aMutex( _rMutex )
987 rSupplier.acquire();
990 SvNumberFormatSettingsObj::~SvNumberFormatSettingsObj()
992 rSupplier.release();
995 // XPropertySet
997 uno::Reference<beans::XPropertySetInfo> SAL_CALL SvNumberFormatSettingsObj::getPropertySetInfo()
998 throw(uno::RuntimeException)
1000 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
1001 static uno::Reference<beans::XPropertySetInfo> aRef =
1002 new SfxItemPropertySetInfo( lcl_GetNumberSettingsPropertyMap() );
1003 return aRef;
1006 void SAL_CALL SvNumberFormatSettingsObj::setPropertyValue( const rtl::OUString& aPropertyName,
1007 const uno::Any& aValue )
1008 throw(beans::UnknownPropertyException, beans::PropertyVetoException,
1009 lang::IllegalArgumentException, lang::WrappedTargetException,
1010 uno::RuntimeException)
1012 ::osl::MutexGuard aGuard( m_aMutex );
1014 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
1015 if (pFormatter)
1017 String aString = aPropertyName;
1018 if (aString.EqualsAscii( PROPERTYNAME_NOZERO ))
1020 // operator >>= shouldn't be used for bool (?)
1021 if ( aValue.getValueTypeClass() == uno::TypeClass_BOOLEAN )
1022 pFormatter->SetNoZero( *(sal_Bool*)aValue.getValue() );
1024 else if (aString.EqualsAscii( PROPERTYNAME_NULLDATE ))
1026 util::Date aDate;
1027 if ( aValue >>= aDate )
1028 pFormatter->ChangeNullDate( aDate.Day, aDate.Month, aDate.Year );
1030 else if (aString.EqualsAscii( PROPERTYNAME_STDDEC ))
1032 sal_Int16 nInt16 = sal_Int16();
1033 if ( aValue >>= nInt16 )
1034 pFormatter->ChangeStandardPrec( nInt16 );
1036 else if (aString.EqualsAscii( PROPERTYNAME_TWODIGIT ))
1038 sal_Int16 nInt16 = sal_Int16();
1039 if ( aValue >>= nInt16 )
1040 pFormatter->SetYear2000( nInt16 );
1042 else
1043 throw beans::UnknownPropertyException();
1045 rSupplier.SettingsChanged();
1047 else
1048 throw uno::RuntimeException();
1051 uno::Any SAL_CALL SvNumberFormatSettingsObj::getPropertyValue( const rtl::OUString& aPropertyName )
1052 throw(beans::UnknownPropertyException, lang::WrappedTargetException,
1053 uno::RuntimeException)
1055 ::osl::MutexGuard aGuard( m_aMutex );
1057 uno::Any aRet;
1058 SvNumberFormatter* pFormatter = rSupplier.GetNumberFormatter();
1059 if (pFormatter)
1061 String aString = aPropertyName;
1062 if (aString.EqualsAscii( PROPERTYNAME_NOZERO ))
1064 BOOL bNoZero = pFormatter->GetNoZero();
1065 aRet.setValue( &bNoZero, getBooleanCppuType() );
1067 else if (aString.EqualsAscii( PROPERTYNAME_NULLDATE ))
1069 Date* pDate = pFormatter->GetNullDate();
1070 if (pDate)
1072 util::Date aUnoDate( pDate->GetDay(), pDate->GetMonth(), pDate->GetYear() );
1073 aRet <<= aUnoDate;
1076 else if (aString.EqualsAscii( PROPERTYNAME_STDDEC ))
1077 aRet <<= (sal_Int16)( pFormatter->GetStandardPrec() );
1078 else if (aString.EqualsAscii( PROPERTYNAME_TWODIGIT ))
1079 aRet <<= (sal_Int16)( pFormatter->GetYear2000() );
1080 else
1081 throw beans::UnknownPropertyException();
1083 else
1084 throw uno::RuntimeException();
1086 return aRet;
1089 void SAL_CALL SvNumberFormatSettingsObj::addPropertyChangeListener( const rtl::OUString&,
1090 const uno::Reference<beans::XPropertyChangeListener>&)
1091 throw(beans::UnknownPropertyException,
1092 lang::WrappedTargetException, uno::RuntimeException)
1094 DBG_ERROR("not implemented");
1097 void SAL_CALL SvNumberFormatSettingsObj::removePropertyChangeListener( const rtl::OUString&,
1098 const uno::Reference<beans::XPropertyChangeListener>&)
1099 throw(beans::UnknownPropertyException,
1100 lang::WrappedTargetException, uno::RuntimeException)
1102 DBG_ERROR("not implemented");
1105 void SAL_CALL SvNumberFormatSettingsObj::addVetoableChangeListener( const rtl::OUString&,
1106 const uno::Reference<beans::XVetoableChangeListener>&)
1107 throw(beans::UnknownPropertyException,
1108 lang::WrappedTargetException, uno::RuntimeException)
1110 DBG_ERROR("not implemented");
1113 void SAL_CALL SvNumberFormatSettingsObj::removeVetoableChangeListener( const rtl::OUString&,
1114 const uno::Reference<beans::XVetoableChangeListener>&)
1115 throw(beans::UnknownPropertyException,
1116 lang::WrappedTargetException, uno::RuntimeException)
1118 DBG_ERROR("not implemented");
1121 // XServiceInfo
1123 rtl::OUString SAL_CALL SvNumberFormatSettingsObj::getImplementationName()
1124 throw(uno::RuntimeException)
1126 return rtl::OUString::createFromAscii("SvNumberFormatSettingsObj");
1129 sal_Bool SAL_CALL SvNumberFormatSettingsObj::supportsService( const rtl::OUString& ServiceName )
1130 throw(uno::RuntimeException)
1132 return ( ServiceName.compareToAscii(SERVICENAME_NUMBERSETTINGS) == 0 );
1135 uno::Sequence<rtl::OUString> SAL_CALL SvNumberFormatSettingsObj::getSupportedServiceNames()
1136 throw(uno::RuntimeException)
1138 uno::Sequence<rtl::OUString> aRet(1);
1139 rtl::OUString* pArray = aRet.getArray();
1140 pArray[0] = rtl::OUString::createFromAscii(SERVICENAME_NUMBERSETTINGS);
1141 return aRet;