bbbca257f05c4fc2f8a754966b16dd4f11f0407f
[reactos.git] / reactos / dll / cpl / intl_new / locale.c
1 /*
2 * PROJECT: ReactOS International Control Panel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: lib/cpl/intl/locale.h
5 * PURPOSE: Regional parameters page
6 * PROGRAMMERS: Eric Kohl
7 * Alexey Zavyalov (gen_x@mail.ru)
8 */
9
10 /* INCLUDES *****************************************************************/
11
12 #include <windows.h>
13 #include <commctrl.h>
14 #include <cpl.h>
15
16 #include "intl.h"
17 #include "resource.h"
18
19 /* GLOBALS ******************************************************************/
20
21 #define SAMPLE_NUMBER L"123456789"
22 #define NO_FLAG 0
23
24 HWND hLocaleList, hGeoList;
25
26 /* FUNCTIONS ****************************************************************/
27
28 /* Location enumerate procedure */
29 BOOL
30 CALLBACK
31 LocationsEnumProc(GEOID gId)
32 {
33 TCHAR loc[MAX_STR_SIZE];
34 int index;
35
36 GetGeoInfo(gId, GEO_FRIENDLYNAME, loc, MAX_FMT_SIZE, LANG_SYSTEM_DEFAULT);
37 index = (int) SendMessageW(hGeoList,
38 CB_ADDSTRING,
39 0,
40 (LPARAM)loc);
41
42 SendMessageW(hGeoList,
43 CB_SETITEMDATA,
44 index,
45 (LPARAM)gId);
46
47 return TRUE;
48 }
49
50 /* Enumerate all system locations identifiers */
51 static
52 VOID
53 CreateLocationsList(HWND hWnd)
54 {
55 GEOID userGeoID;
56 TCHAR loc[MAX_STR_SIZE];
57
58 hGeoList = hWnd;
59
60 EnumSystemGeoID(GEOCLASS_NATION, 0, LocationsEnumProc);
61
62 // Select current location
63 userGeoID = GetUserGeoID(GEOCLASS_NATION);
64 GetGeoInfo(userGeoID,
65 GEO_FRIENDLYNAME,
66 loc,
67 MAX_FMT_SIZE,
68 LANG_SYSTEM_DEFAULT);
69
70 SendMessageW(hGeoList,
71 CB_SELECTSTRING,
72 (WPARAM) -1,
73 (LPARAM)loc);
74 }
75
76 /* Locale enumerate procedure */
77 BOOL
78 CALLBACK
79 LocalesEnumProc(LPTSTR lpLocale)
80 {
81 LCID lcid;
82 TCHAR lang[MAX_STR_SIZE];
83 int index;
84
85 lcid = wcstoul(lpLocale, NULL, 16);
86
87 GetLocaleInfo(lcid, LOCALE_SLANGUAGE, lang, sizeof(lang));
88 index = (int) SendMessageW(hLocaleList,
89 CB_ADDSTRING,
90 0,
91 (LPARAM)lang);
92
93 SendMessageW(hLocaleList,
94 CB_SETITEMDATA,
95 (WPARAM) index,
96 (LPARAM)lcid);
97
98 return TRUE;
99 }
100
101 /* Enumerate all installed locale identifiers */
102 static
103 VOID
104 CreateLanguagesList(HWND hWnd)
105 {
106 TCHAR langSel[MAX_STR_SIZE];
107
108 hLocaleList = hWnd;
109
110 EnumSystemLocalesW(LocalesEnumProc, LCID_INSTALLED);
111
112 // Select current locale
113 GetLocaleInfo(GetUserDefaultLCID(),
114 LOCALE_SLANGUAGE,
115 langSel,
116 sizeof(langSel));
117
118 SendMessageW(hLocaleList,
119 CB_SELECTSTRING,
120 (WPARAM) -1,
121 (LPARAM)langSel);
122 }
123
124
125 /* Update all locale samples */
126 static
127 VOID
128 UpdateLocaleSample(HWND hwndDlg, LCID lcidLocale)
129 {
130 WCHAR OutBuffer[MAX_FMT_SIZE];
131
132 /* Get number format sample */
133 GetNumberFormatW(lcidLocale, NO_FLAG, SAMPLE_NUMBER, NULL, OutBuffer,
134 MAX_FMT_SIZE);
135 SendMessageW(GetDlgItem(hwndDlg, IDC_NUMSAMPLE_EDIT),
136 WM_SETTEXT, 0, (LPARAM)OutBuffer);
137
138 /* Get monetary format sample */
139 GetCurrencyFormatW(lcidLocale, LOCALE_USE_CP_ACP, SAMPLE_NUMBER, NULL,
140 OutBuffer, MAX_FMT_SIZE);
141 SendMessageW(GetDlgItem(hwndDlg, IDC_MONEYSAMPLE_EDIT),
142 WM_SETTEXT, 0, (LPARAM)OutBuffer);
143
144 /* Get time format sample */
145 GetTimeFormatW(lcidLocale, NO_FLAG, NULL, NULL, OutBuffer, MAX_FMT_SIZE);
146 SendMessageW(GetDlgItem(hwndDlg, IDC_TIMESAMPLE_EDIT),
147 WM_SETTEXT,
148 0,
149 (LPARAM)OutBuffer);
150
151 /* Get short date format sample */
152 GetDateFormatW(lcidLocale, DATE_SHORTDATE, NULL, NULL, OutBuffer,
153 MAX_FMT_SIZE);
154 SendMessageW(GetDlgItem(hwndDlg, IDC_SHORTTIMESAMPLE_EDIT), WM_SETTEXT,
155 0, (LPARAM)OutBuffer);
156
157 /* Get long date sample */
158 GetDateFormatW(lcidLocale, DATE_LONGDATE, NULL, NULL, OutBuffer,
159 MAX_FMT_SIZE);
160 SendMessageW(GetDlgItem(hwndDlg, IDC_FULLTIMESAMPLE_EDIT),
161 WM_SETTEXT, 0, (LPARAM)OutBuffer);
162 }
163
164 /* Setting up new locale */
165 static
166 VOID
167 SetNewLocale(LCID LcidLocale)
168 {
169 int RetCode;
170 TCHAR OutBuff[MAX_STR_SIZE];
171 int LocaleCounter;
172 HKEY LocaleKey;
173 TCHAR Value[9];
174 DWORD ValueSize;
175
176 //SetUserDefaultLCID(LcidLocale); // Not implemented in MS :(
177
178 if(!IsValidLocale(LcidLocale, LCID_INSTALLED))
179 {
180 MessageBoxW(NULL, L"Invalid locale.", L"Critical error",
181 MB_OK | MB_ICONERROR);
182 return;
183 }
184
185 //HACKHACK: I'm sure there is a better way to do this
186 for(LocaleCounter = LOCALE_ILANGUAGE;
187 LocaleCounter <= LOCALE_SISO3166CTRYNAME; LocaleCounter++)
188 {
189 RetCode = GetLocaleInfoW(LcidLocale,
190 (LCTYPE)LocaleCounter,
191 OutBuff,
192 sizeof(OutBuff));
193 if(RetCode == ERROR_INVALID_PARAMETER)
194 continue;
195 SetLocaleInfoW(LOCALE_USER_DEFAULT, (LCTYPE)LocaleCounter, OutBuff);
196 }
197
198 RetCode = RegOpenKeyW(HKEY_CURRENT_USER,
199 L"Control Panel\\International",
200 &LocaleKey);
201
202 if(RetCode != ERROR_SUCCESS)
203 {
204 MessageBoxW(NULL,
205 L"Problem opening HKCU\\Control Panel\\International key",
206 L"Critical error", MB_OK | MB_ICONERROR);
207 return;
208 }
209
210 wsprintf(Value, L"%04X", (DWORD)LcidLocale);
211 ValueSize = (DWORD) (wcslen(Value) + 1) * sizeof(WCHAR);
212
213 RegSetValueExW(LocaleKey, L"Locale", 0, REG_SZ, (BYTE *)Value, ValueSize);
214 RegCloseKey(LocaleKey);
215 }
216
217 /* Regional Parameters page dialog callback */
218 INT_PTR
219 CALLBACK
220 RegOptsProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
221 {
222 int CurrSel;
223 LCID NewLCID;
224 GEOID NewLocation;
225
226 switch(uMsg)
227 {
228 case WM_INITDIALOG:
229 CreateLocationsList(GetDlgItem(hwndDlg, IDC_LOCATION_COMBO));
230 CreateLanguagesList(GetDlgItem(hwndDlg, IDC_SETREG_COMBO));
231 /* Update locale samples */
232 UpdateLocaleSample(hwndDlg, LOCALE_USER_DEFAULT);
233 break;
234
235 case WM_COMMAND:
236 switch (LOWORD(wParam))
237 {
238 case IDC_SETUP_BUTTON:
239 // setup button code...
240 MessageBox(NULL, L"Not implemented yet", L"Info",
241 MB_OK | MB_ICONINFORMATION);
242 break;
243
244 /* If selected other locale */
245 case IDC_SETREG_COMBO:
246 if (HIWORD(wParam) == CBN_SELCHANGE ||
247 HIWORD(wParam) == CBN_EDITCHANGE)
248 {
249 CurrSel = (int) SendMessageW((HWND)lParam, CB_GETCURSEL,
250 0, 0);
251 NewLCID = (LCID) SendMessageW((HWND)lParam, CB_GETITEMDATA,
252 CurrSel, 0);
253 UpdateLocaleSample(hwndDlg,
254 MAKELCID(NewLCID, SORT_DEFAULT));
255 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
256 }
257 break;
258
259 case IDC_LOCATION_COMBO:
260 if (HIWORD(wParam) == CBN_SELCHANGE ||
261 HIWORD(wParam) == CBN_EDITCHANGE)
262 {
263 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
264 }
265 break;
266 }
267 break;
268
269 case WM_NOTIFY:
270 {
271 LPNMHDR lpnm = (LPNMHDR)lParam;
272 /* If push apply button */
273 if (lpnm->code == (UINT)PSN_APPLY)
274 {
275 /* Set locale */
276 CurrSel = (int) SendMessageW(GetDlgItem(hwndDlg, IDC_SETREG_COMBO),
277 CB_GETCURSEL, 0, 0);
278 NewLCID = (LCID) SendMessageW(GetDlgItem(hwndDlg, IDC_SETREG_COMBO),
279 CB_GETITEMDATA, CurrSel, 0);
280 if (NewLCID == (LCID)CB_ERR) break;
281
282 SetNewLocale(MAKELCID(NewLCID, SORT_DEFAULT));
283
284 /* Set geo location */
285 CurrSel = (int) SendMessageW(GetDlgItem(hwndDlg, IDC_LOCATION_COMBO),
286 CB_GETCURSEL,
287 0,
288 0);
289 NewLocation = (GEOID) SendMessageW(GetDlgItem(hwndDlg, IDC_LOCATION_COMBO),
290 CB_GETITEMDATA,
291 CurrSel,
292 0);
293 SetUserGeoID(NewLocation);
294 }
295 }
296 break;
297 }
298 return FALSE;
299 }
300
301 /* EOF */