sync master with lastest vba changes
[ooovba.git] / binfilter / inc / bf_svtools / ondemand.hxx
blobb6d21e4dd3501e8a2014c3b6ef02978ef9fb3f12
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: ondemand.hxx,v $
10 * $Revision: 1.3 $
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 #ifndef INCLUDED_SVTOOLS_ONDEMAND_HXX
32 #define INCLUDED_SVTOOLS_ONDEMAND_HXX
34 #ifndef INCLUDED_SVTOOLS_SYSLOCALE_HXX
35 #include <bf_svtools/syslocale.hxx>
36 #endif
38 #ifndef INCLUDED_I18NPOOL_LANG_H
39 #include <i18npool/lang.h>
40 #endif
42 #ifndef _UNOTOOLS_LOCALEDATAWRAPPER_HXX
43 #include <unotools/localedatawrapper.hxx>
44 #endif
45 #ifndef _UNOTOOLS_CALENDARWRAPPER_HXX
46 #include <unotools/calendarwrapper.hxx>
47 #endif
48 #ifndef _UNOTOOLS_COLLATORWRAPPER_HXX
49 #include <unotools/collatorwrapper.hxx>
50 #endif
51 #ifndef _COM_SUN_STAR_I18N_COLLATOROPTIONS_HPP_
52 #include <com/sun/star/i18n/CollatorOptions.hpp>
53 #endif
54 #ifndef _UNOTOOLS_TRANSLITERATIONWRAPPER_HXX
55 #include <unotools/transliterationwrapper.hxx>
56 #endif
57 #ifndef _COM_SUN_STAR_I18N_TRANSLITERATIONMODULES_HPP_
58 #include <com/sun/star/i18n/TransliterationModules.hpp>
59 #endif
60 #ifndef _UNOTOOLS_NATIVENUMBERWRAPPER_HXX
61 #include <unotools/nativenumberwrapper.hxx>
62 #endif
63 #ifndef _COM_SUN_STAR_UNO_REFERENCE_HXX_
64 #include <com/sun/star/uno/Reference.hxx>
65 #endif
66 #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
67 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
68 #endif
70 namespace binfilter
74 On demand instanciation and initialization of several i18n wrappers,
75 helping the number formatter to not perform worse than it already does.
78 /** @short
79 Switch between LANGUAGE_SYSTEM and LANGUAGE_ENGLISH_US and any other
80 LocaleDataWrapper.
81 SvNumberformatter uses it upon switching locales.
83 @descr
84 Avoids reloading and analysing of locale data again and again.
86 @ATTENTION
87 If the default ctor is used the init() method MUST be called before
88 accessing any locale data. The passed parameters Locale and LanguageType
89 must match each other.
92 class OnDemandLocaleDataWrapper
94 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xSMgr;
95 SvtSysLocale aSysLocale;
96 LanguageType eCurrentLanguage;
97 LanguageType eLastAnyLanguage;
98 const LocaleDataWrapper* pSystem;
99 const LocaleDataWrapper* pEnglish;
100 LocaleDataWrapper* pAny;
101 const LocaleDataWrapper* pCurrent;
102 bool bInitialized;
104 public:
105 OnDemandLocaleDataWrapper()
106 : eLastAnyLanguage( LANGUAGE_DONTKNOW )
107 , pEnglish(0)
108 , pAny(0)
109 , bInitialized(false)
111 pCurrent = pSystem = aSysLocale.GetLocaleDataPtr();
112 eCurrentLanguage = LANGUAGE_SYSTEM;
114 OnDemandLocaleDataWrapper(
115 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
116 ::com::sun::star::lang::Locale& rLocale,
117 LanguageType eLang
119 : pEnglish(0)
120 , pAny(0)
121 , pCurrent(0)
122 , bInitialized(false)
124 pSystem = aSysLocale.GetLocaleDataPtr();
125 init( rxSMgr, rLocale, eLang );
127 ~OnDemandLocaleDataWrapper()
129 delete pEnglish;
130 delete pAny;
133 bool isInitialized() const { return bInitialized; }
135 bool is() const { return pCurrent != NULL; }
137 void init(
138 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
139 ::com::sun::star::lang::Locale& rLocale,
140 LanguageType eLang
143 xSMgr = rxSMgr;
144 changeLocale( rLocale, eLang );
145 bInitialized = true;
148 void changeLocale( ::com::sun::star::lang::Locale& rLocale, LanguageType eLang )
150 switch ( eLang )
152 case LANGUAGE_SYSTEM :
153 pCurrent = pSystem;
154 break;
155 case LANGUAGE_ENGLISH_US :
156 if ( !pEnglish )
157 pEnglish = new LocaleDataWrapper( xSMgr, rLocale );
158 pCurrent = pEnglish;
159 break;
160 default:
161 if ( !pAny )
163 pAny = new LocaleDataWrapper( xSMgr, rLocale );
164 eLastAnyLanguage = eLang;
166 else if ( eLastAnyLanguage != eLang )
168 pAny->setLocale( rLocale );
169 eLastAnyLanguage = eLang;
171 pCurrent = pAny;
173 eCurrentLanguage = eLang;
176 LanguageType getCurrentLanguage() const
177 { return eCurrentLanguage; }
179 LocaleDataWrapper* getAnyLocale()
181 if ( !pAny )
183 pAny = new LocaleDataWrapper( xSMgr, pCurrent->getLocale() );
184 eLastAnyLanguage = eCurrentLanguage;
186 else if ( pCurrent != pAny )
188 pAny->setLocale( pCurrent->getLocale() );
189 eLastAnyLanguage = eCurrentLanguage;
191 return pAny;
194 const LocaleDataWrapper* get() const { return pCurrent; }
195 const LocaleDataWrapper* operator->() const { return get(); }
196 const LocaleDataWrapper& operator*() const { return *get(); }
199 /** Load a calendar only if it's needed.
200 SvNumberformatter uses it upon switching locales.
201 @ATTENTION If the default ctor is used the init() method MUST be called
202 before accessing the calendar.
204 class OnDemandCalendarWrapper
206 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xSMgr;
207 ::com::sun::star::lang::Locale aLocale;
208 mutable CalendarWrapper* pPtr;
209 mutable bool bValid;
210 bool bInitialized;
212 public:
213 OnDemandCalendarWrapper()
214 : pPtr(0)
215 , bValid(false)
216 , bInitialized(false)
218 OnDemandCalendarWrapper(
219 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
220 ::com::sun::star::lang::Locale& rLocale
222 : bValid(false)
223 , bInitialized(false)
225 init( rxSMgr, rLocale );
227 ~OnDemandCalendarWrapper()
229 delete pPtr;
232 bool isInitialized() const { return bInitialized; }
234 bool is() const { return pPtr != NULL; }
236 void init(
237 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
238 ::com::sun::star::lang::Locale& rLocale
241 xSMgr = rxSMgr;
242 changeLocale( rLocale );
243 if ( pPtr )
245 delete pPtr;
246 pPtr = NULL;
248 bInitialized = true;
251 void changeLocale( ::com::sun::star::lang::Locale& rLocale )
253 bValid = false;
254 aLocale = rLocale;
257 CalendarWrapper* get() const
259 if ( !bValid )
261 if ( !pPtr )
262 pPtr = new CalendarWrapper( xSMgr );
263 pPtr->loadDefaultCalendar( aLocale );
264 bValid = true;
266 return pPtr;
269 CalendarWrapper* operator->() { return get(); }
270 CalendarWrapper& operator*() { return *get(); }
273 /** Load a collator only if it's needed.
274 SvNumberformatter uses it upon switching locales.
275 @ATTENTION If the default ctor is used the init() method MUST be called
276 before accessing the collator.
278 class OnDemandCollatorWrapper
280 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xSMgr;
281 ::com::sun::star::lang::Locale aLocale;
282 mutable CollatorWrapper* pPtr;
283 mutable bool bValid;
284 bool bInitialized;
286 public:
287 OnDemandCollatorWrapper()
288 : pPtr(0)
289 , bValid(false)
290 , bInitialized(false)
292 OnDemandCollatorWrapper(
293 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
294 ::com::sun::star::lang::Locale& rLocale
296 : bValid(false)
297 , bInitialized(false)
299 init( rxSMgr, rLocale );
301 ~OnDemandCollatorWrapper()
303 delete pPtr;
306 bool isInitialized() const { return bInitialized; }
308 bool is() const { return pPtr != NULL; }
310 void init(
311 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
312 ::com::sun::star::lang::Locale& rLocale
315 xSMgr = rxSMgr;
316 changeLocale( rLocale );
317 if ( pPtr )
319 delete pPtr;
320 pPtr = NULL;
322 bInitialized = true;
325 void changeLocale( ::com::sun::star::lang::Locale& rLocale )
327 bValid = false;
328 aLocale = rLocale;
331 const CollatorWrapper* get() const
333 if ( !bValid )
335 if ( !pPtr )
336 pPtr = new CollatorWrapper( xSMgr );
337 pPtr->loadDefaultCollator( aLocale, ::com::sun::star::i18n::CollatorOptions::CollatorOptions_IGNORE_CASE );
338 bValid = true;
340 return pPtr;
343 const CollatorWrapper* operator->() const { return get(); }
344 const CollatorWrapper& operator*() const { return *get(); }
347 /** Load a transliteration only if it's needed.
348 SvNumberformatter uses it upon switching locales.
349 @ATTENTION If the default ctor is used the init() method MUST be called
350 before accessing the transliteration.
352 class OnDemandTransliterationWrapper
354 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xSMgr;
355 LanguageType eLanguage;
356 ::com::sun::star::i18n::TransliterationModules nType;
357 mutable ::utl::TransliterationWrapper* pPtr;
358 mutable bool bValid;
359 bool bInitialized;
361 public:
362 OnDemandTransliterationWrapper()
363 : eLanguage( LANGUAGE_SYSTEM )
364 , pPtr(0)
365 , bValid(false)
366 , bInitialized(false)
368 OnDemandTransliterationWrapper(
369 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
370 LanguageType eLang,
371 ::com::sun::star::i18n::TransliterationModules nTypeP
373 : bValid(false)
374 , bInitialized(false)
376 init( rxSMgr, eLang, nTypeP );
378 ~OnDemandTransliterationWrapper()
380 delete pPtr;
383 bool isInitialized() const { return bInitialized; }
385 bool is() const { return pPtr != NULL; }
387 void init(
388 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr,
389 LanguageType eLang,
390 ::com::sun::star::i18n::TransliterationModules nTypeP
393 xSMgr = rxSMgr;
394 nType = nTypeP;
395 changeLocale( eLang );
396 if ( pPtr )
398 delete pPtr;
399 pPtr = NULL;
401 bInitialized = true;
404 void changeLocale( LanguageType eLang )
406 bValid = false;
407 eLanguage = eLang;
410 const ::utl::TransliterationWrapper* get() const
412 if ( !bValid )
414 if ( !pPtr )
415 pPtr = new ::utl::TransliterationWrapper( xSMgr, nType );
416 pPtr->loadModuleIfNeeded( eLanguage );
417 bValid = true;
419 return pPtr;
422 const ::utl::TransliterationWrapper* getForModule( const String& rModule, LanguageType eLang ) const
424 if ( !pPtr )
425 pPtr = new ::utl::TransliterationWrapper( xSMgr, nType );
426 pPtr->loadModuleByImplName( rModule, eLang );
427 bValid = false; // reforce settings change in get()
428 return pPtr;
431 const ::utl::TransliterationWrapper* operator->() const { return get(); }
432 const ::utl::TransliterationWrapper& operator*() const { return *get(); }
435 /** Load a native number service wrapper only if it's needed.
436 SvNumberformatter uses it.
438 @ATTENTION
439 If the default ctor is used the init() method MUST be called
440 before accessing the native number supplier.
442 class OnDemandNativeNumberWrapper
444 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xSMgr;
445 mutable NativeNumberWrapper* pPtr;
446 bool bInitialized;
448 public:
449 OnDemandNativeNumberWrapper()
450 : pPtr(0)
451 , bInitialized(false)
453 OnDemandNativeNumberWrapper(
454 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr
456 : pPtr(0)
457 , bInitialized(false)
459 init( rxSMgr );
461 ~OnDemandNativeNumberWrapper()
463 delete pPtr;
466 bool isInitialized() const { return bInitialized; }
468 void init(
469 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxSMgr
472 xSMgr = rxSMgr;
473 if ( pPtr )
475 delete pPtr;
476 pPtr = NULL;
478 bInitialized = true;
481 bool is() const { return pPtr != NULL; }
483 NativeNumberWrapper* get() const
485 if ( !pPtr )
486 pPtr = new NativeNumberWrapper( xSMgr );
487 return pPtr;
490 NativeNumberWrapper* operator->() { return get(); }
491 NativeNumberWrapper& operator*() { return *get(); }
496 #endif // INCLUDED_SVTOOLS_ONDEMAND_HXX