511400d616e96a86557fcb8654d6ce6eed61d6d5
[reactos.git] / reactos / dll / cpl / intl / time.c
1 /*
2 * ReactOS
3 * Copyright (C) 2004 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 /*
20 * PROJECT: ReactOS International Control Panel
21 * FILE: dll/cpl/intl/time.c
22 * PURPOSE: Time property page
23 * PROGRAMMER: Eric Kohl
24 */
25
26 #include "intl.h"
27
28 static HWND hwndEnum = NULL;
29
30 static BOOL CALLBACK
31 TimeFormatEnumProc(PWSTR lpTimeFormatString)
32 {
33 SendMessageW(hwndEnum,
34 CB_ADDSTRING,
35 0,
36 (LPARAM)lpTimeFormatString);
37
38 return TRUE;
39 }
40
41 static VOID
42 UpdateTimeSample(HWND hWnd, PGLOBALDATA pGlobalData)
43 {
44 WCHAR szBuffer[MAX_SAMPLES_STR_SIZE];
45
46 GetTimeFormatW(pGlobalData->UserLCID, 0, NULL,
47 pGlobalData->szTimeFormat, szBuffer,
48 MAX_SAMPLES_STR_SIZE);
49 SendDlgItemMessageW(hWnd, IDC_TIMESAMPLE, WM_SETTEXT, 0, (LPARAM)szBuffer);
50 }
51
52
53 static VOID
54 GetSelectedComboEntry(HWND hwndDlg, DWORD dwIdc, WCHAR *Buffer, UINT uSize)
55 {
56 HWND hChildWnd;
57 PWSTR tmp;
58 INT nIndex;
59 UINT uReqSize;
60
61 /* Get handle to time format control */
62 hChildWnd = GetDlgItem(hwndDlg, dwIdc);
63
64 /* Get index to selected time format */
65 nIndex = SendMessageW(hChildWnd, CB_GETCURSEL, 0, 0);
66 if (nIndex == CB_ERR)
67 {
68 /* No selection? Get content of the edit control */
69 SendMessageW(hChildWnd, WM_GETTEXT, uSize, (LPARAM)Buffer);
70 }
71 else
72 {
73 /* Get requested size, including the null terminator;
74 * it shouldn't be required because the previous CB_LIMITTEXT,
75 * but it would be better to check it anyways */
76 uReqSize = SendMessageW(hChildWnd, CB_GETLBTEXTLEN, (WPARAM)nIndex, 0) + 1;
77
78 /* Allocate enough space to be more safe */
79 tmp = (PWSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, uReqSize * sizeof(WCHAR));
80 if (tmp != NULL)
81 {
82 /* Get selected time format text */
83 SendMessageW(hChildWnd, CB_GETLBTEXT, (WPARAM)nIndex, (LPARAM)tmp);
84
85 /* Finally, copy the result into the output */
86 wcsncpy(Buffer, tmp, uSize);
87
88 HeapFree(GetProcessHeap(), 0, tmp);
89 }
90 }
91 }
92
93
94 static
95 VOID
96 InitTimeFormatCB(
97 HWND hwndDlg,
98 PGLOBALDATA pGlobalData)
99 {
100 /* Get the time format */
101 SendDlgItemMessageW(hwndDlg, IDC_TIMEFORMAT,
102 CB_LIMITTEXT, MAX_TIMEFORMAT, 0);
103
104 /* Add available time formats to the list */
105 hwndEnum = GetDlgItem(hwndDlg, IDC_TIMEFORMAT);
106 EnumTimeFormatsW(TimeFormatEnumProc, pGlobalData->UserLCID, 0);
107
108 SendDlgItemMessageW(hwndDlg, IDC_TIMEFORMAT,
109 CB_SELECTSTRING,
110 -1,
111 (LPARAM)pGlobalData->szTimeFormat);
112 }
113
114 static
115 VOID
116 InitTimeSeparatorCB(
117 HWND hwndDlg,
118 PGLOBALDATA pGlobalData)
119 {
120 SendDlgItemMessageW(hwndDlg, IDC_TIMESEPARATOR,
121 CB_LIMITTEXT, MAX_TIMESEPARATOR, 0);
122
123 SendDlgItemMessageW(hwndDlg, IDC_TIMESEPARATOR,
124 CB_ADDSTRING,
125 0,
126 (LPARAM)pGlobalData->szTimeSep);
127
128 SendDlgItemMessageW(hwndDlg, IDC_TIMESEPARATOR,
129 CB_SETCURSEL,
130 0, /* Index */
131 0);
132 }
133
134
135 static
136 VOID
137 InitAmSymbol(
138 HWND hwndDlg,
139 PGLOBALDATA pGlobalData)
140 {
141 int nLen;
142
143 SendDlgItemMessageW(hwndDlg, IDC_TIMEAMSYMBOL,
144 CB_LIMITTEXT, MAX_TIMEAMSYMBOL, 0);
145
146 nLen = wcslen(pGlobalData->szTimeAM);
147
148 SendDlgItemMessageW(hwndDlg, IDC_TIMEAMSYMBOL,
149 CB_ADDSTRING,
150 0,
151 (LPARAM)pGlobalData->szTimeAM);
152 if (nLen != 0)
153 {
154 SendDlgItemMessageW(hwndDlg, IDC_TIMEAMSYMBOL,
155 CB_ADDSTRING,
156 0,
157 (LPARAM)L"");
158 }
159
160 SendDlgItemMessageW(hwndDlg, IDC_TIMEAMSYMBOL,
161 CB_SETCURSEL,
162 0, /* Index */
163 0);
164 }
165
166
167 static
168 VOID
169 InitPmSymbol(
170 HWND hwndDlg,
171 PGLOBALDATA pGlobalData)
172 {
173 int nLen;
174
175 SendDlgItemMessageW(hwndDlg, IDC_TIMEPMSYMBOL,
176 CB_LIMITTEXT, MAX_TIMEPMSYMBOL, 0);
177
178 nLen = wcslen(pGlobalData->szTimeAM);
179
180 SendDlgItemMessageW(hwndDlg, IDC_TIMEPMSYMBOL,
181 CB_ADDSTRING,
182 0,
183 (LPARAM)pGlobalData->szTimePM);
184 if (nLen != 0)
185 {
186 SendDlgItemMessageW(hwndDlg, IDC_TIMEPMSYMBOL,
187 CB_ADDSTRING,
188 0,
189 (LPARAM)L"");
190 }
191 SendDlgItemMessageW(hwndDlg, IDC_TIMEPMSYMBOL,
192 CB_SETCURSEL,
193 0, /* Index */
194 0);
195 }
196
197
198 /* Property page dialog callback */
199 INT_PTR CALLBACK
200 TimePageProc(HWND hwndDlg,
201 UINT uMsg,
202 WPARAM wParam,
203 LPARAM lParam)
204 {
205 PGLOBALDATA pGlobalData;
206
207 pGlobalData = (PGLOBALDATA)GetWindowLongPtr(hwndDlg, DWLP_USER);
208
209 switch (uMsg)
210 {
211 case WM_INITDIALOG:
212 pGlobalData = (PGLOBALDATA)((LPPROPSHEETPAGE)lParam)->lParam;
213 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pGlobalData);
214
215 /* Get the time format */
216 InitTimeFormatCB(hwndDlg, pGlobalData);
217
218 /* Get the time separator */
219 InitTimeSeparatorCB(hwndDlg, pGlobalData);
220
221 /* Get the AM symbol */
222 InitAmSymbol(hwndDlg, pGlobalData);
223
224 /* Get the PM symbol */
225 InitPmSymbol(hwndDlg, pGlobalData);
226
227 /* Update the time format sample */
228 UpdateTimeSample(hwndDlg, pGlobalData);
229 break;
230
231 case WM_COMMAND:
232 switch (LOWORD(wParam))
233 {
234 case IDC_TIMEFORMAT:
235 case IDC_TIMESEPARATOR:
236 case IDC_TIMEAMSYMBOL:
237 case IDC_TIMEPMSYMBOL:
238 if (HIWORD(wParam) == CBN_SELCHANGE ||
239 HIWORD(wParam) == CBN_EDITCHANGE)
240 {
241 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
242 }
243 break;
244 }
245 break;
246
247 case WM_NOTIFY:
248 if (((LPNMHDR)lParam)->code == (UINT)PSN_APPLY)
249 {
250 /* Get selected/typed time format text */
251 GetSelectedComboEntry(hwndDlg, IDC_TIMEFORMAT,
252 pGlobalData->szTimeFormat,
253 MAX_TIMEFORMAT);
254
255 /* Get selected/typed time separator text */
256 GetSelectedComboEntry(hwndDlg, IDC_TIMESEPARATOR,
257 pGlobalData->szTimeSep,
258 MAX_TIMESEPARATOR);
259
260 /* Get selected/typed AM symbol text */
261 GetSelectedComboEntry(hwndDlg, IDC_TIMEAMSYMBOL,
262 pGlobalData->szTimeAM,
263 MAX_TIMEAMSYMBOL);
264
265 /* Get selected/typed PM symbol text */
266 GetSelectedComboEntry(hwndDlg, IDC_TIMEPMSYMBOL,
267 pGlobalData->szTimePM,
268 MAX_TIMEPMSYMBOL);
269
270 pGlobalData->bUserLocaleChanged = TRUE;
271
272 /* Update the time format sample */
273 UpdateTimeSample(hwndDlg, pGlobalData);
274 }
275 break;
276 }
277
278 return FALSE;
279 }
280
281 /* EOF */