Added MessageBoxes in case of registry funcs errors
[reactos.git] / reactos / lib / cpl / intl / locale.c
1 /*
2 * ReactOS
3 * Copyright (C) 2004, 2005 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id$
20 *
21 * PROJECT: ReactOS International Control Panel
22 * FILE: lib/cpl/intl/locale.c
23 * PURPOSE: Locale property page
24 * PROGRAMMER: Eric Kohl
25 * Klemens Friedl
26 * Aleksey Bragin
27 */
28
29 #define WINVER 0x0501
30
31 #include <windows.h>
32 #include <commctrl.h>
33 #include <cpl.h>
34
35 #include <stdio.h>
36
37 #include "intl.h"
38 #include "resource.h"
39
40 HWND hList;
41
42 BOOL CALLBACK LocalesEnumProc(
43 LPTSTR lpLocale // locale id
44 )
45 {
46 LCID lcid;
47 TCHAR lang[255];
48 int index;
49
50 //swscanf(lpLocale, L"%lx", &lcid); // maybe use wcstoul?
51 lcid = wcstoul(lpLocale, NULL, 16);
52
53 GetLocaleInfo(lcid, LOCALE_SLANGUAGE, lang, sizeof(lang));
54
55 index = SendMessageW(hList,
56 CB_ADDSTRING,
57 0,
58 (LPARAM)lang);
59
60 SendMessageW(hList,
61 CB_SETITEMDATA,
62 index,
63 (LPARAM)lcid);
64
65 return TRUE;
66 }
67
68
69 static VOID
70 CreateLanguagesList(HWND hwnd)
71 {
72 TCHAR langSel[255];
73
74 hList = hwnd;
75 EnumSystemLocalesW(LocalesEnumProc, LCID_SUPPORTED);
76
77 // Select current locale
78 GetLocaleInfo(GetUserDefaultLCID(), LOCALE_SLANGUAGE, langSel, sizeof(langSel)); // or should it be System and not user?
79
80 SendMessageW(hList,
81 CB_SELECTSTRING,
82 -1,
83 (LPARAM)langSel);
84 }
85
86 // Sets new locale
87 void SetNewLocale(LCID lcid)
88 {
89 // HKCU\\Control Panel\\International\\Locale = 0409 (type=0)
90 // HKLM,"SYSTEM\CurrentControlSet\Control\NLS\Language","Default",0x00000000,"0409" (type=0)
91 // HKLM,"SYSTEM\CurrentControlSet\Control\NLS\Language","InstallLanguage",0x00000000,"0409" (type=0)
92
93 // Set locale
94 HKEY localeKey;
95 HKEY langKey;
96 DWORD ret;
97 TCHAR value[9];
98 DWORD valuesize;
99
100 ret = RegOpenKeyW(HKEY_CURRENT_USER, L"Control Panel\\International", &localeKey);
101
102 if (ret != ERROR_SUCCESS)
103 {
104 // some serious error
105 MessageBoxW(NULL, L"Problem opening HKCU\\Control Panel\\International key", L"Big Problem", MB_OK);
106 return;
107 }
108
109 wsprintf(value, L"%04X", (DWORD)lcid);
110 valuesize = (wcslen(value) + 1) * sizeof(WCHAR);
111
112 RegSetValueExW(localeKey, L"Locale", 0, REG_SZ, (BYTE *)value, valuesize);
113 RegCloseKey(localeKey);
114
115 // Set language
116 ret = RegOpenKeyW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\NLS\\Language", &langKey);
117
118 if (ret != ERROR_SUCCESS)
119 {
120 MessageBoxW(NULL, L"Problem opening HKLM\\SYSTEM\\CurrentControlSet\\Control\\NLS\\Language key", L"Big Problem", MB_OK);
121 return;
122 }
123
124 RegSetValueExW(langKey, L"Default", 0, REG_SZ, (BYTE *)value, valuesize );
125 RegSetValueExW(langKey, L"InstallLanguage", 0, REG_SZ, (BYTE *)value, valuesize );
126 RegCloseKey(langKey);
127 }
128
129 /* Property page dialog callback */
130 INT_PTR CALLBACK
131 LocalePageProc(HWND hwndDlg,
132 UINT uMsg,
133 WPARAM wParam,
134 LPARAM lParam)
135 {
136 switch(uMsg)
137 {
138 case WM_INITDIALOG:
139 CreateLanguagesList(GetDlgItem(hwndDlg, IDC_LANGUAGELIST));
140 break;
141 case WM_COMMAND:
142 switch (LOWORD(wParam))
143 {
144 case IDC_LANGUAGELIST:
145 if (HIWORD(wParam) == CBN_SELCHANGE)
146 {
147 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
148 }
149 break;
150 }
151 break;
152
153 case WM_NOTIFY:
154 {
155 LPNMHDR lpnm = (LPNMHDR)lParam;
156 if (lpnm->code == PSN_APPLY)
157 {
158 // Apply changes
159 LCID NewLcid;
160 int iCurSel;
161
162 // Acquire new value
163 iCurSel = SendMessageW(hList,
164 CB_GETCURSEL,
165 0,
166 0);
167 if (iCurSel == CB_ERR)
168 break;
169
170 NewLcid = SendMessageW(hList,
171 CB_GETITEMDATA,
172 iCurSel,
173 0);
174
175 if (NewLcid == CB_ERR)
176 break;
177
178
179 // Actually set new locale
180 SetNewLocale(NewLcid);
181 }
182 }
183 break;
184 }
185 return FALSE;
186 }
187
188
189 /* EOF */