Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / osl / w32 / nlsupport.c
blob175a8c6edee87f974b0b6c9a5663a47c7825044d
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 #define UNICODE
21 #ifdef _MSC_VER
22 #pragma warning(push,1) /* disable warnings within system headers */
23 #endif
24 #include <windows.h>
25 #ifdef _MSC_VER
26 #pragma warning(pop)
27 #endif
28 #include <wchar.h>
30 #include <osl/mutex.h>
31 #include <osl/nlsupport.h>
32 #include <osl/diagnose.h>
33 #include <osl/process.h>
34 #include <rtl/tencinfo.h>
37 /* XXX NOTE:
38 * http://msdn.microsoft.com/en-us/library/windows/desktop/dd373848.aspx
39 * (retrieved 2013-02-13) has some weird description for the LOCALE_SISO*
40 * constants: "The maximum number of characters allowed for this string is
41 * nine, including a terminating null character." NINE?!? In ISO 639 and ISO
42 * 3166?
44 #define ELP_LANGUAGE_FIELD_LENGTH 4
45 #define ELP_COUNTRY_FIELD_LENGTH 3
47 /** Struct used in EnumLocalesProcA() called via EnumSystemLocalesA() to obtain
48 available locales.
50 struct EnumLocalesParams
52 WCHAR Language[ELP_LANGUAGE_FIELD_LENGTH];
53 WCHAR Country[ELP_COUNTRY_FIELD_LENGTH];
54 LCID Locale;
57 static DWORD g_dwTLSLocaleEncId = (DWORD) -1;
59 /*****************************************************************************
60 * callback function test
62 * osl_getTextEncodingFromLocale calls EnumSystemLocalesA, so that we don't
63 * need to provide a unicode wrapper for this function under Win9x
64 * that means the callback function has an ansi prototype and receives
65 * the locale strings as ansi strings
66 *****************************************************************************/
68 BOOL CALLBACK EnumLocalesProcA( LPSTR lpLocaleStringA )
70 struct EnumLocalesParams * params;
72 LCID localeId;
73 LPSTR pszEndA;
75 WCHAR langCode[ELP_LANGUAGE_FIELD_LENGTH];
77 /* convert hex-string to LCID */
78 localeId = strtol( lpLocaleStringA, &pszEndA, 16 );
80 /* check params received via TLS */
81 params = (struct EnumLocalesParams *) TlsGetValue( g_dwTLSLocaleEncId );
82 if( NULL == params || '\0' == params->Language[0] )
83 return FALSE;
86 get the ISO language code for this locale
88 remember: we call the GetLocaleInfoW function
89 because the ansi version of this function returns
90 an error under WinNT/2000 when called with an
91 unicode only lcid
93 if( GetLocaleInfo( localeId, LOCALE_SISO639LANGNAME , langCode, ELP_LANGUAGE_FIELD_LENGTH ) )
95 WCHAR ctryCode[ELP_COUNTRY_FIELD_LENGTH];
97 /* continue if language code does not match */
98 if( 0 != wcscmp( langCode, params->Language ) )
99 return TRUE;
101 /* check if country code is set and equals the current locale */
102 if( '\0' != params->Country[0] && GetLocaleInfo( localeId,
103 LOCALE_SISO3166CTRYNAME , ctryCode, ELP_COUNTRY_FIELD_LENGTH ) )
105 /* save return value in TLS and break if found desired locale */
106 if( 0 == wcscmp( ctryCode, params->Country ) )
108 params->Locale = localeId;
109 return FALSE;
112 else
114 /* fill with default values for that language */
115 LANGID langId = LANGIDFROMLCID( localeId );
117 /* exchange sublanguage with SUBLANG_NEUTRAL */
118 langId = MAKELANGID( PRIMARYLANGID( langId ), SUBLANG_NEUTRAL );
120 /* and use default sorting order */
121 params->Locale = MAKELCID( langId, SORT_DEFAULT );
123 return FALSE;
127 /* retry by going on */
128 return TRUE;
132 /*****************************************************************************
133 * GetTextEncodingFromLCID
134 *****************************************************************************/
136 rtl_TextEncoding GetTextEncodingFromLCID( LCID localeId )
138 rtl_TextEncoding Encoding = RTL_TEXTENCODING_DONTKNOW;
139 WCHAR ansiCP[6];
141 /* query ansi codepage for given locale */
142 if( localeId && GetLocaleInfo( localeId, LOCALE_IDEFAULTANSICODEPAGE, ansiCP, 6 ) )
144 /* if GetLocaleInfo returns "0", it is a UNICODE only locale */
145 if( 0 != wcscmp( ansiCP, L"0" ) )
147 WCHAR *pwcEnd;
148 UINT codepage;
150 /* values returned from GetLocaleInfo are decimal based */
151 codepage = wcstol( ansiCP, &pwcEnd, 10 );
153 /* find matching rtl encoding */
154 Encoding = rtl_getTextEncodingFromWindowsCodePage( codepage );
156 else
157 Encoding = RTL_TEXTENCODING_UNICODE;
160 return Encoding;
164 /*****************************************************************************
165 * osl_getTextEncodingFromLocale
166 *****************************************************************************/
168 rtl_TextEncoding SAL_CALL osl_getTextEncodingFromLocale( rtl_Locale * pLocale )
170 struct EnumLocalesParams params = { L"", L"", 0 };
172 /* initialise global TLS id */
173 if( (DWORD) -1 == g_dwTLSLocaleEncId )
175 oslMutex globalMutex = * osl_getGlobalMutex();
177 /* initializing must be thread save */
178 osl_acquireMutex( globalMutex );
180 if( (DWORD) -1 == g_dwTLSLocaleEncId )
181 g_dwTLSLocaleEncId = TlsAlloc();
183 osl_releaseMutex( globalMutex );
186 /* if pLocale is NULL, use process locale as default */
187 if( NULL == pLocale )
188 osl_getProcessLocale( &pLocale );
190 /* copy in parameters to structure */
191 if( pLocale && pLocale->Language && pLocale->Language->length < ELP_LANGUAGE_FIELD_LENGTH )
193 wcscpy( params.Language, pLocale->Language->buffer );
195 if( pLocale->Country && pLocale->Country->length < ELP_COUNTRY_FIELD_LENGTH )
196 wcscpy( params.Country, pLocale->Country->buffer );
198 /* save pointer to local structure in TLS */
199 TlsSetValue( g_dwTLSLocaleEncId, &params );
201 /* enum all locales known to Windows */
202 EnumSystemLocalesA( EnumLocalesProcA, LCID_SUPPORTED );
204 /* use the LCID found in iteration */
205 return GetTextEncodingFromLCID( params.Locale );
208 return RTL_TEXTENCODING_DONTKNOW;
211 /*****************************************************************************
212 * imp_getProcessLocale
213 *****************************************************************************/
215 void _imp_getProcessLocale( rtl_Locale ** ppLocale )
217 WCHAR langCode[ELP_LANGUAGE_FIELD_LENGTH];
218 WCHAR ctryCode[ELP_COUNTRY_FIELD_LENGTH];
219 LCID localeId;
221 OSL_ASSERT( ppLocale );
223 /* get the LCID to retrieve information from */
224 localeId = GetUserDefaultLCID();
226 /* call GetLocaleInfo to retrieve the iso codes */
227 if( GetLocaleInfo( localeId, LOCALE_SISO639LANGNAME , langCode, ELP_LANGUAGE_FIELD_LENGTH ) &&
228 GetLocaleInfo( localeId, LOCALE_SISO3166CTRYNAME , ctryCode, ELP_COUNTRY_FIELD_LENGTH ) )
230 *ppLocale = rtl_locale_register( langCode, ctryCode, L"" );
232 else
234 *ppLocale = rtl_locale_register( L"C", L"", L"" );
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */