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