merge the formfield patch from ooo-build
[ooovba.git] / vcl / source / app / i18nhelp.cxx
blob8e9f3aca42834059ac6f73e27b13a5be377b21c1
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: i18nhelp.cxx,v $
10 * $Revision: 1.9 $
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_vcl.hxx"
34 #include "vcl/i18nhelp.hxx"
36 #include "com/sun/star/lang/XMultiServiceFactory.hpp"
37 #include "com/sun/star/i18n/TransliterationModules.hpp"
38 #include "unotools/localedatawrapper.hxx"
39 #include "unotools/transliterationwrapper.hxx"
40 #include "i18npool/mslangid.hxx"
42 #include "rtl/ustrbuf.hxx"
44 using namespace ::com::sun::star;
46 vcl::I18nHelper::I18nHelper( ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxMSF, const ::com::sun::star::lang::Locale& rLocale )
48 mxMSF = rxMSF;
49 maLocale = rLocale;
50 mpLocaleDataWrapper = NULL;
51 mpTransliterationWrapper= NULL;
52 mbTransliterateIgnoreCase = sal_False;
55 vcl::I18nHelper::~I18nHelper()
57 ImplDestroyWrappers();
60 void vcl::I18nHelper::ImplDestroyWrappers()
62 delete mpLocaleDataWrapper;
63 mpLocaleDataWrapper = NULL;
65 delete mpTransliterationWrapper;
66 mpTransliterationWrapper= NULL;
69 utl::TransliterationWrapper& vcl::I18nHelper::ImplGetTransliterationWrapper() const
71 if ( !mpTransliterationWrapper )
73 sal_Int32 nModules = i18n::TransliterationModules_IGNORE_WIDTH;
74 if ( mbTransliterateIgnoreCase )
75 nModules |= i18n::TransliterationModules_IGNORE_CASE;
77 ((vcl::I18nHelper*)this)->mpTransliterationWrapper = new utl::TransliterationWrapper( mxMSF, (i18n::TransliterationModules)nModules );
78 ((vcl::I18nHelper*)this)->mpTransliterationWrapper->loadModuleIfNeeded( MsLangId::convertLocaleToLanguage( maLocale ) );
80 return *mpTransliterationWrapper;
83 LocaleDataWrapper& vcl::I18nHelper::ImplGetLocaleDataWrapper() const
85 if ( !mpLocaleDataWrapper )
87 ((vcl::I18nHelper*)this)->mpLocaleDataWrapper = new LocaleDataWrapper( mxMSF, maLocale );
89 return *mpLocaleDataWrapper;
92 const ::com::sun::star::lang::Locale& vcl::I18nHelper::getLocale() const
94 return maLocale;
97 inline bool is_formatting_mark( sal_Unicode c )
99 if( (c >= 0x200B) && (c <= 0x200F) ) // BiDi and zero-width-markers
100 return true;
101 if( (c >= 0x2028) && (c <= 0x202E) ) // BiDi and paragraph-markers
102 return true;
103 return false;
106 /* #i100057# filter formatting marks out of strings before passing them to
107 the transliteration. The real solution would have been an additional TransliterationModule
108 to ignore these marks during transliteration; however changin the code in i18npool that actually
109 implements this could produce unwanted side effects.
111 Of course this copying around is not really good, but looking at i18npool, one more time
112 will not hurt.
114 String vcl::I18nHelper::filterFormattingChars( const String& rStr )
116 sal_Int32 nUnicodes = rStr.Len();
117 rtl::OUStringBuffer aBuf( nUnicodes );
118 const sal_Unicode* pStr = rStr.GetBuffer();
119 while( nUnicodes-- )
121 if( ! is_formatting_mark( *pStr ) )
122 aBuf.append( *pStr );
123 pStr++;
125 return aBuf.makeStringAndClear();
128 sal_Int32 vcl::I18nHelper::CompareString( const String& rStr1, const String& rStr2 ) const
130 ::osl::Guard< ::osl::Mutex > aGuard( ((vcl::I18nHelper*)this)->maMutex );
132 if ( mbTransliterateIgnoreCase )
134 // Change mbTransliterateIgnoreCase and destroy the warpper, next call to
135 // ImplGetTransliterationWrapper() will create a wrapper with the correct bIgnoreCase
136 ((vcl::I18nHelper*)this)->mbTransliterateIgnoreCase = FALSE;
137 delete ((vcl::I18nHelper*)this)->mpTransliterationWrapper;
138 ((vcl::I18nHelper*)this)->mpTransliterationWrapper = NULL;
142 String aStr1( filterFormattingChars(rStr1) );
143 String aStr2( filterFormattingChars(rStr2) );
144 return ImplGetTransliterationWrapper().compareString( aStr1, aStr2 );
147 sal_Bool vcl::I18nHelper::MatchString( const String& rStr1, const String& rStr2 ) const
149 ::osl::Guard< ::osl::Mutex > aGuard( ((vcl::I18nHelper*)this)->maMutex );
151 if ( !mbTransliterateIgnoreCase )
153 // Change mbTransliterateIgnoreCase and destroy the warpper, next call to
154 // ImplGetTransliterationWrapper() will create a wrapper with the correct bIgnoreCase
155 ((vcl::I18nHelper*)this)->mbTransliterateIgnoreCase = TRUE;
156 delete ((vcl::I18nHelper*)this)->mpTransliterationWrapper;
157 ((vcl::I18nHelper*)this)->mpTransliterationWrapper = NULL;
160 String aStr1( filterFormattingChars(rStr1) );
161 String aStr2( filterFormattingChars(rStr2) );
162 return ImplGetTransliterationWrapper().isMatch( aStr1, aStr2 );
165 sal_Bool vcl::I18nHelper::MatchMnemonic( const String& rString, sal_Unicode cMnemonicChar ) const
167 ::osl::Guard< ::osl::Mutex > aGuard( ((vcl::I18nHelper*)this)->maMutex );
169 BOOL bEqual = FALSE;
170 USHORT n = rString.Search( '~' );
171 if ( n != STRING_NOTFOUND )
173 String aMatchStr( rString, n+1, STRING_LEN ); // not only one char, because of transliteration...
174 bEqual = MatchString( cMnemonicChar, aMatchStr );
176 return bEqual;
180 String vcl::I18nHelper::GetDate( const Date& rDate ) const
182 ::osl::Guard< ::osl::Mutex > aGuard( ((vcl::I18nHelper*)this)->maMutex );
184 return ImplGetLocaleDataWrapper().getDate( rDate );
187 String vcl::I18nHelper::GetNum( long nNumber, USHORT nDecimals, BOOL bUseThousandSep, BOOL bTrailingZeros ) const
189 return ImplGetLocaleDataWrapper().getNum( nNumber, nDecimals, bUseThousandSep, bTrailingZeros );