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