xref: /aoo4110/main/i18npool/source/isolang/inwnt.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // no include "precompiled_i18npool.hxx" because this file is included in insys.cxx
25 
26 #include <sal/config.h>
27 
28 #ifdef _MSC_VER
29 #pragma warning(push,1) // disable warnings within system headers
30 #endif
31 #include <windef.h>     // needed by winnls.h
32 #include <winbase.h>    // needed by winnls.h
33 #include <winnls.h>
34 #ifdef _MSC_VER
35 #pragma warning(pop)
36 #endif
37 #include <rtl/instance.hxx>
38 #include "i18npool/mslangid.hxx"
39 
40 static LanguageType nImplSystemLanguage = LANGUAGE_DONTKNOW;
41 static LanguageType nImplSystemUILanguage = LANGUAGE_DONTKNOW;
42 
43 // =======================================================================
44 
GetSVLang(LANGID nWinLangId)45 static LanguageType GetSVLang( LANGID nWinLangId )
46 {
47     // No Translation, we work with the original MS code without the SORT_ID.
48     // So we can get never LANG-ID's from MS, which are currently not defined
49     // by us.
50     return LanguageType( static_cast<sal_uInt16>(nWinLangId & 0xffff));
51 }
52 
53 // -----------------------------------------------------------------------
54 
55 typedef LANGID (WINAPI *getLangFromEnv)();
56 
getPlatformSystemLanguageImpl(LanguageType & rSystemLanguage,getLangFromEnv pGetUserDefault,getLangFromEnv pGetSystemDefault)57 static void getPlatformSystemLanguageImpl( LanguageType& rSystemLanguage,
58         getLangFromEnv pGetUserDefault, getLangFromEnv pGetSystemDefault )
59 {
60     LanguageType nLang = rSystemLanguage;
61     if ( nLang == LANGUAGE_DONTKNOW )
62     {
63         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex());
64         nLang = rSystemLanguage;
65         if ( nLang == LANGUAGE_DONTKNOW )
66         {
67             LANGID nLangId;
68 
69             nLangId = (pGetUserDefault)();
70             nLang = GetSVLang( nLangId );
71 
72             if ( nLang == LANGUAGE_DONTKNOW )
73             {
74                 nLangId = (pGetSystemDefault)();
75                 nLang = GetSVLang( nLangId );
76             }
77             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
78             rSystemLanguage = nLang;
79         }
80         else
81             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
82     }
83 }
84 
85 // -----------------------------------------------------------------------
86 
getPlatformSystemLanguage()87 LanguageType MsLangId::getPlatformSystemLanguage()
88 {
89     getPlatformSystemLanguageImpl( nImplSystemLanguage,
90             &GetUserDefaultLangID, &GetSystemDefaultLangID);
91     return nImplSystemLanguage;
92 }
93 
94 // -----------------------------------------------------------------------
95 
getPlatformSystemUILanguage()96 LanguageType MsLangId::getPlatformSystemUILanguage()
97 {
98     // TODO: this could be distinguished, #if(WINVER >= 0x0500)
99     // needs _run_ time differentiation though, not at compile time.
100     getPlatformSystemLanguageImpl( nImplSystemUILanguage,
101             &GetUserDefaultUILanguage, &GetSystemDefaultUILanguage);
102     return nImplSystemUILanguage;
103 }
104