xref: /aoo41x/main/i18npool/source/isolang/inwnt.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // no include "precompiled_i18npool.hxx" because this file is included in insys.cxx
29 
30 #include <sal/config.h>
31 
32 #ifdef _MSC_VER
33 #pragma warning(push,1) // disable warnings within system headers
34 #endif
35 #include <windef.h>     // needed by winnls.h
36 #include <winbase.h>    // needed by winnls.h
37 #include <winnls.h>
38 #ifdef _MSC_VER
39 #pragma warning(pop)
40 #endif
41 #include <rtl/instance.hxx>
42 #include "i18npool/mslangid.hxx"
43 
44 static LanguageType nImplSystemLanguage = LANGUAGE_DONTKNOW;
45 static LanguageType nImplSystemUILanguage = LANGUAGE_DONTKNOW;
46 
47 // =======================================================================
48 
49 static LanguageType GetSVLang( LANGID nWinLangId )
50 {
51     // No Translation, we work with the original MS code without the SORT_ID.
52     // So we can get never LANG-ID's from MS, which are currently not defined
53     // by us.
54     return LanguageType( static_cast<sal_uInt16>(nWinLangId & 0xffff));
55 }
56 
57 // -----------------------------------------------------------------------
58 
59 typedef LANGID (WINAPI *getLangFromEnv)();
60 
61 static void getPlatformSystemLanguageImpl( LanguageType& rSystemLanguage,
62         getLangFromEnv pGetUserDefault, getLangFromEnv pGetSystemDefault )
63 {
64     LanguageType nLang = rSystemLanguage;
65     if ( nLang == LANGUAGE_DONTKNOW )
66     {
67         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex());
68         nLang = rSystemLanguage;
69         if ( nLang == LANGUAGE_DONTKNOW )
70         {
71             LANGID nLangId;
72 
73             nLangId = (pGetUserDefault)();
74             nLang = GetSVLang( nLangId );
75 
76             if ( nLang == LANGUAGE_DONTKNOW )
77             {
78                 nLangId = (pGetSystemDefault)();
79                 nLang = GetSVLang( nLangId );
80             }
81             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
82             rSystemLanguage = nLang;
83         }
84         else
85             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
86     }
87 }
88 
89 // -----------------------------------------------------------------------
90 
91 LanguageType MsLangId::getPlatformSystemLanguage()
92 {
93     getPlatformSystemLanguageImpl( nImplSystemLanguage,
94             &GetUserDefaultLangID, &GetSystemDefaultLangID);
95     return nImplSystemLanguage;
96 }
97 
98 // -----------------------------------------------------------------------
99 
100 LanguageType MsLangId::getPlatformSystemUILanguage()
101 {
102     // TODO: this could be distinguished, #if(WINVER >= 0x0500)
103     // needs _run_ time differentiation though, not at compile time.
104     getPlatformSystemLanguageImpl( nImplSystemUILanguage,
105             &GetUserDefaultUILanguage, &GetSystemDefaultUILanguage);
106     return nImplSystemUILanguage;
107 }
108