- Remove ALL the unneeded "author date id revision" svn properties.
[reactos.git] / reactos / dll / cpl / intl / generalp.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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /* $Id: generalp.c 56849 2012-07-07 23:02:14Z fireball $
20 *
21 * PROJECT: ReactOS International Control Panel
22 * FILE: dll/cpl/intl/generalp.c
23 * PURPOSE: General property page
24 * PROGRAMMER: Eric Kohl
25 * Klemens Friedl
26 * Aleksey Bragin
27 */
28
29 #include "intl.h"
30
31 #define SAMPLE_NUMBER _T("123456789")
32 #define NO_FLAG 0
33
34 HWND hList;
35 HWND hLocaleList, hGeoList;
36 BOOL bSpain = FALSE;
37
38 static BOOL CALLBACK
39 LocalesEnumProc(LPTSTR lpLocale)
40 {
41 LCID lcid;
42 TCHAR lang[255];
43 INT index;
44 BOOL bNoShow = FALSE;
45
46 lcid = _tcstoul(lpLocale, NULL, 16);
47
48 /* Display only languages with installed support */
49 if (!IsValidLocale(lcid, LCID_INSTALLED))
50 return TRUE;
51
52 if (lcid == MAKELCID(MAKELANGID(LANG_SPANISH, SUBLANG_SPANISH), SORT_DEFAULT) ||
53 lcid == MAKELCID(MAKELANGID(LANG_SPANISH, SUBLANG_SPANISH_MODERN), SORT_DEFAULT))
54 {
55 if (bSpain == FALSE)
56 {
57 LoadString(hApplet, IDS_SPAIN, lang, 255);
58 bSpain = TRUE;
59 }
60 else
61 {
62 bNoShow = TRUE;
63 }
64 }
65 else
66 {
67 GetLocaleInfo(lcid, LOCALE_SLANGUAGE, lang, sizeof(lang)/sizeof(TCHAR));
68 }
69
70 if (bNoShow == FALSE)
71 {
72 index = SendMessage(hList,
73 CB_ADDSTRING,
74 0,
75 (LPARAM)lang);
76
77 SendMessage(hList,
78 CB_SETITEMDATA,
79 index,
80 (LPARAM)lcid);
81 }
82
83 return TRUE;
84 }
85
86 /* Update all locale samples */
87 static VOID
88 UpdateLocaleSample(HWND hwndDlg, LCID lcidLocale)
89 {
90 TCHAR OutBuffer[MAX_SAMPLES_STR_SIZE];
91
92 /* Get number format sample */
93 GetNumberFormat(lcidLocale, NO_FLAG, SAMPLE_NUMBER, NULL, OutBuffer,
94 MAX_SAMPLES_STR_SIZE);
95 SendMessage(GetDlgItem(hwndDlg, IDC_NUMSAMPLE_EDIT),
96 WM_SETTEXT, 0, (LPARAM)OutBuffer);
97
98 /* Get monetary format sample */
99 GetCurrencyFormat(lcidLocale, LOCALE_USE_CP_ACP, SAMPLE_NUMBER, NULL,
100 OutBuffer, MAX_SAMPLES_STR_SIZE);
101 SendMessage(GetDlgItem(hwndDlg, IDC_MONEYSAMPLE_EDIT),
102 WM_SETTEXT, 0, (LPARAM)OutBuffer);
103
104 /* Get time format sample */
105 GetTimeFormat(lcidLocale, NO_FLAG, NULL, NULL, OutBuffer, MAX_SAMPLES_STR_SIZE);
106 SendMessage(GetDlgItem(hwndDlg, IDC_TIMESAMPLE_EDIT),
107 WM_SETTEXT,
108 0,
109 (LPARAM)OutBuffer);
110
111 /* Get short date format sample */
112 GetDateFormat(lcidLocale, DATE_SHORTDATE, NULL, NULL, OutBuffer,
113 MAX_SAMPLES_STR_SIZE);
114 SendMessage(GetDlgItem(hwndDlg, IDC_SHORTTIMESAMPLE_EDIT), WM_SETTEXT,
115 0, (LPARAM)OutBuffer);
116
117 /* Get long date sample */
118 GetDateFormat(lcidLocale, DATE_LONGDATE, NULL, NULL, OutBuffer,
119 MAX_SAMPLES_STR_SIZE);
120 SendMessage(GetDlgItem(hwndDlg, IDC_FULLTIMESAMPLE_EDIT),
121 WM_SETTEXT, 0, (LPARAM)OutBuffer);
122 }
123
124 static VOID
125 CreateLanguagesList(HWND hwnd)
126 {
127 TCHAR langSel[255];
128
129 hList = hwnd;
130 bSpain = FALSE;
131 EnumSystemLocales(LocalesEnumProc, LCID_SUPPORTED);
132
133 /* Select current locale */
134 /* or should it be System and not user? */
135 GetLocaleInfo(GetUserDefaultLCID(), LOCALE_SLANGUAGE, langSel, sizeof(langSel)/sizeof(TCHAR));
136
137 SendMessage(hList,
138 CB_SELECTSTRING,
139 -1,
140 (LPARAM)langSel);
141 }
142
143 /* Sets new locale */
144 VOID
145 SetNewLocale(LCID lcid)
146 {
147 // HKCU\\Control Panel\\International\\Locale = 0409 (type=0)
148 // HKLM,"SYSTEM\CurrentControlSet\Control\NLS\Language","Default",0x00000000,"0409" (type=0)
149 // HKLM,"SYSTEM\CurrentControlSet\Control\NLS\Language","InstallLanguage",0x00000000,"0409" (type=0)
150
151 // Set locale
152 HKEY localeKey;
153 HKEY langKey;
154 DWORD ret;
155 TCHAR value[9];
156 DWORD valuesize;
157 TCHAR ACPPage[9];
158 TCHAR OEMPage[9];
159
160 ret = GetLocaleInfo(MAKELCID(lcid, SORT_DEFAULT), LOCALE_IDEFAULTCODEPAGE, OEMPage, sizeof(OEMPage)/sizeof(TCHAR));
161 if (ret == 0)
162 {
163 MessageBox(NULL, _T("Problem reading OEM code page"), _T("Big Problem"), MB_OK);
164 return;
165 }
166
167 ret = GetLocaleInfo(MAKELCID(lcid, SORT_DEFAULT), LOCALE_IDEFAULTANSICODEPAGE, ACPPage, sizeof(ACPPage)/sizeof(TCHAR));
168 if (ret == 0)
169 {
170 MessageBox(NULL, _T("Problem reading ANSI code page"), _T("Big Problem"), MB_OK);
171 return;
172 }
173
174 ret = RegOpenKey(HKEY_CURRENT_USER, _T("Control Panel\\International"), &localeKey);
175 if (ret != ERROR_SUCCESS)
176 {
177 // Some serious error
178 MessageBox(NULL, _T("Problem opening HKCU\\Control Panel\\International key"),
179 _T("Big Problem"), MB_OK);
180 return;
181 }
182
183 wsprintf(value, _T("%04X"), (DWORD)lcid);
184 valuesize = (_tcslen(value) + 1) * sizeof(TCHAR);
185
186 RegSetValueEx(localeKey, _T("Locale"), 0, REG_SZ, (LPBYTE)value, valuesize);
187 RegCloseKey(localeKey);
188
189 ret = RegOpenKey(HKEY_USERS, _T(".DEFAULT\\Control Panel\\International"), &localeKey);
190 if (ret != ERROR_SUCCESS)
191 {
192 // Some serious error
193 MessageBox(NULL, _T("Problem opening HKU\\.DEFAULT\\Control Panel\\International key"),
194 _T("Big Problem"), MB_OK);
195 return;
196 }
197
198 wsprintf(value, _T("%04X"), (DWORD)lcid);
199 valuesize = (_tcslen(value) + 1) * sizeof(TCHAR);
200
201 RegSetValueEx(localeKey, _T("Locale"), 0, REG_SZ, (BYTE *)value, valuesize);
202 RegCloseKey(localeKey);
203
204 // Set language
205 ret = RegOpenKey(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\Control\\NLS\\Language"), &langKey);
206 if (ret != ERROR_SUCCESS)
207 {
208 MessageBoxW(NULL, _T("Problem opening HKLM\\SYSTEM\\CurrentControlSet\\Control\\NLS\\Language key"),
209 _T("Big Problem"), MB_OK);
210 return;
211 }
212
213 RegSetValueEx(langKey, _T("Default"), 0, REG_SZ, (BYTE *)value, valuesize );
214 RegSetValueEx(langKey, _T("InstallLanguage"), 0, REG_SZ, (BYTE *)value, valuesize );
215
216 RegCloseKey(langKey);
217
218
219 /* Set language */
220 ret = RegOpenKey(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\Control\\NLS\\CodePage"), &langKey);
221 if (ret != ERROR_SUCCESS)
222 {
223 MessageBox(NULL, _T("Problem opening HKLM\\SYSTEM\\CurrentControlSet\\Control\\NLS\\CodePage key"),
224 _T("Big Problem"), MB_OK);
225 return;
226 }
227
228 RegSetValueExW(langKey, _T("OEMCP"), 0, REG_SZ, (BYTE *)OEMPage, (_tcslen(OEMPage) +1 ) * sizeof(TCHAR));
229 RegSetValueExW(langKey, _T("ACP"), 0, REG_SZ, (BYTE *)ACPPage, (_tcslen(ACPPage) +1 ) * sizeof(TCHAR));
230
231 RegCloseKey(langKey);
232 }
233
234 /* Location enumerate procedure */
235 BOOL
236 CALLBACK
237 LocationsEnumProc(GEOID gId)
238 {
239 TCHAR loc[MAX_STR_SIZE];
240 INT index;
241
242 GetGeoInfo(gId, GEO_FRIENDLYNAME, loc, MAX_STR_SIZE, LANG_SYSTEM_DEFAULT);
243 index = (INT)SendMessage(hGeoList,
244 CB_ADDSTRING,
245 0,
246 (LPARAM)loc);
247
248 SendMessage(hGeoList,
249 CB_SETITEMDATA,
250 index,
251 (LPARAM)gId);
252
253 return TRUE;
254 }
255
256 /* Enumerate all system locations identifiers */
257 static
258 VOID
259 CreateLocationsList(HWND hWnd)
260 {
261 GEOID userGeoID;
262 TCHAR loc[MAX_STR_SIZE];
263
264 hGeoList = hWnd;
265
266 EnumSystemGeoID(GEOCLASS_NATION, 0, LocationsEnumProc);
267
268 /* Select current location */
269 userGeoID = GetUserGeoID(GEOCLASS_NATION);
270 GetGeoInfo(userGeoID,
271 GEO_FRIENDLYNAME,
272 loc,
273 MAX_STR_SIZE,
274 LANG_SYSTEM_DEFAULT);
275
276 SendMessage(hGeoList,
277 CB_SELECTSTRING,
278 (WPARAM) -1,
279 (LPARAM)loc);
280 }
281
282 DWORD
283 VerifyUnattendLCID(HWND hwndDlg)
284 {
285 LRESULT lCount, lIndex, lResult;
286
287 lCount = SendMessage(hList, CB_GETCOUNT, (WPARAM)0, (LPARAM)0);
288 if (lCount == CB_ERR)
289 {
290 return 0;
291 }
292
293 for (lIndex = 0; lIndex < lCount; lIndex++)
294 {
295 lResult = SendMessage(hList, CB_GETITEMDATA, (WPARAM)lIndex, (LPARAM)0);
296 if (lResult == CB_ERR)
297 {
298 continue;
299 }
300
301 if (lResult == (LRESULT)UnattendLCID)
302 {
303 SendMessage(hList, CB_SETCURSEL, (WPARAM)lIndex, (LPARAM)0);
304 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
305 return 1;
306 }
307 }
308
309 return 0;
310 }
311
312
313 /* Property page dialog callback */
314 INT_PTR CALLBACK
315 GeneralPageProc(HWND hwndDlg,
316 UINT uMsg,
317 WPARAM wParam,
318 LPARAM lParam)
319 {
320 switch(uMsg)
321 {
322 case WM_INITDIALOG:
323 CreateLanguagesList(GetDlgItem(hwndDlg, IDC_LANGUAGELIST));
324 UpdateLocaleSample(hwndDlg, LOCALE_USER_DEFAULT);
325 CreateLocationsList(GetDlgItem(hwndDlg, IDC_LOCATION_COMBO));
326 if (IsUnattendedSetupEnabled)
327 {
328 if (VerifyUnattendLCID(hwndDlg))
329 {
330 SetNewLocale(UnattendLCID);
331 PostQuitMessage(0);
332 } else
333 DPRINT1("VerifyUnattendLCID failed\n");
334 return TRUE;
335 }
336 break;
337
338 case WM_COMMAND:
339 switch (LOWORD(wParam))
340 {
341 case IDC_LANGUAGELIST:
342 if (HIWORD(wParam) == CBN_SELCHANGE)
343 {
344 LCID NewLcid;
345 INT iCurSel;
346
347 iCurSel = SendMessage(hList,
348 CB_GETCURSEL,
349 0,
350 0);
351 if (iCurSel == CB_ERR)
352 break;
353
354 NewLcid = SendMessage(hList,
355 CB_GETITEMDATA,
356 iCurSel,
357 0);
358 if (NewLcid == (LCID)CB_ERR)
359 break;
360
361 UpdateLocaleSample(hwndDlg, NewLcid);
362
363 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
364 }
365 break;
366
367 case IDC_LOCATION_COMBO:
368 if (HIWORD(wParam) == CBN_SELCHANGE)
369 {
370 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
371 }
372 break;
373 case IDC_SETUP_BUTTON:
374 {
375 LCID NewLcid;
376 INT iCurSel;
377
378 iCurSel = SendMessage(hList,
379 CB_GETCURSEL,
380 0,
381 0);
382 if (iCurSel == CB_ERR)
383 break;
384
385 NewLcid = SendMessage(hList,
386 CB_GETITEMDATA,
387 iCurSel,
388 0);
389 if (NewLcid == (LCID)CB_ERR)
390 break;
391
392 SetupApplet(GetParent(hwndDlg), NewLcid);
393 }
394 break;
395 }
396 break;
397
398 case WM_NOTIFY:
399 {
400 LPNMHDR lpnm = (LPNMHDR)lParam;
401
402 if (lpnm->code == (UINT)PSN_APPLY)
403 {
404 /* Apply changes */
405 LCID NewLcid;
406 GEOID NewGeoID;
407 INT iCurSel;
408
409 PropSheet_UnChanged(GetParent(hwndDlg), hwndDlg);
410
411 /* Acquire new value */
412 iCurSel = SendMessage(hList,
413 CB_GETCURSEL,
414 0,
415 0);
416 if (iCurSel == CB_ERR)
417 break;
418
419 NewLcid = SendMessage(hList,
420 CB_GETITEMDATA,
421 iCurSel,
422 0);
423 if (NewLcid == (LCID)CB_ERR)
424 break;
425
426 iCurSel = SendMessage(GetDlgItem(hwndDlg, IDC_LOCATION_COMBO),
427 CB_GETCURSEL,
428 0,
429 0);
430 if (iCurSel == CB_ERR)
431 break;
432
433 NewGeoID = SendMessage(GetDlgItem(hwndDlg, IDC_LOCATION_COMBO),
434 CB_GETITEMDATA,
435 iCurSel,
436 0);
437 if (NewGeoID == (GEOID)CB_ERR)
438 break;
439
440 /* Set new locale */
441 SetNewLocale(NewLcid);
442 AddNewKbLayoutsByLcid(NewLcid);
443 SetUserGeoID(NewGeoID);
444 SetNonUnicodeLang(hwndDlg, NewLcid);
445 }
446 }
447 break;
448 }
449
450 return FALSE;
451 }
452
453 /* EOF */