* Sync up to trunk head (r64959).
[reactos.git] / 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(LPTSTR lpTimeFormatString)
32 {
33 SendMessage(hwndEnum,
34 CB_ADDSTRING,
35 0,
36 (LPARAM)lpTimeFormatString);
37
38 return TRUE;
39 }
40
41 static VOID
42 UpdateTimeSample(HWND hWnd, LCID lcid)
43 {
44 TCHAR szBuffer[80];
45
46 GetTimeFormat(lcid, 0, NULL, NULL, szBuffer, 80);
47 SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)szBuffer);
48 }
49
50
51 static VOID
52 GetSelectedComboEntry(HWND hwndDlg, DWORD dwIdc, TCHAR *Buffer, UINT uSize)
53 {
54 int nIndex;
55 HWND hChildWnd;
56
57 /* Get handle to time format control */
58 hChildWnd = GetDlgItem(hwndDlg, dwIdc);
59 /* Get index to selected time format */
60 nIndex = SendMessage(hChildWnd, CB_GETCURSEL, 0, 0);
61 if (nIndex == CB_ERR)
62 /* No selection? Get content of the edit control */
63 SendMessage(hChildWnd, WM_GETTEXT, uSize, (LPARAM)Buffer);
64 else {
65 LPTSTR tmp;
66 UINT uReqSize;
67
68 /* Get requested size, including the null terminator;
69 * it shouldn't be required because the previous CB_LIMITTEXT,
70 * but it would be better to check it anyways */
71 uReqSize = SendMessage(hChildWnd, CB_GETLBTEXTLEN, (WPARAM)nIndex, 0) + 1;
72 /* Allocate enough space to be more safe */
73 tmp = (LPTSTR)_alloca(uReqSize*sizeof(TCHAR));
74 /* Get selected time format text */
75 SendMessage(hChildWnd, CB_GETLBTEXT, (WPARAM)nIndex, (LPARAM)tmp);
76 /* Finally, copy the result into the output */
77 _tcsncpy(Buffer, tmp, uSize);
78 }
79 }
80
81 /* Property page dialog callback */
82 INT_PTR CALLBACK
83 TimePageProc(HWND hwndDlg,
84 UINT uMsg,
85 WPARAM wParam,
86 LPARAM lParam)
87 {
88 PGLOBALDATA pGlobalData;
89
90 pGlobalData = (PGLOBALDATA)GetWindowLongPtr(hwndDlg, DWLP_USER);
91
92 switch (uMsg)
93 {
94 case WM_INITDIALOG:
95 {
96 TCHAR Buffer[80];
97 int nLen;
98
99 pGlobalData = (PGLOBALDATA)((LPPROPSHEETPAGE)lParam)->lParam;
100 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pGlobalData);
101
102 /* Update the time format sample */
103 UpdateTimeSample(GetDlgItem(hwndDlg, IDC_TIMESAMPLE), pGlobalData->lcid);
104
105 /* Get the time format */
106 SendMessage(GetDlgItem(hwndDlg, IDC_TIMEFORMAT),
107 CB_LIMITTEXT, MAX_TIMEFORMAT, 0);
108
109 /* Add available time formats to the list */
110 hwndEnum = GetDlgItem(hwndDlg, IDC_TIMEFORMAT);
111 EnumTimeFormats(TimeFormatEnumProc, pGlobalData->lcid, 0);
112
113 GetLocaleInfo(pGlobalData->lcid, LOCALE_STIMEFORMAT, Buffer, sizeof(Buffer)/sizeof(TCHAR));
114 SendMessage(GetDlgItem(hwndDlg, IDC_TIMEFORMAT),
115 CB_SELECTSTRING,
116 -1,
117 (LPARAM)Buffer);
118
119 /* Get the time separator */
120 SendMessage(GetDlgItem(hwndDlg, IDC_TIMESEPARATOR),
121 CB_LIMITTEXT, MAX_TIMESEPARATOR, 0);
122 GetLocaleInfo(pGlobalData->lcid, LOCALE_STIME, Buffer, sizeof(Buffer)/sizeof(TCHAR));
123 SendMessage(GetDlgItem(hwndDlg, IDC_TIMESEPARATOR),
124 CB_ADDSTRING,
125 0,
126 (LPARAM)Buffer);
127 SendMessage(GetDlgItem(hwndDlg, IDC_TIMESEPARATOR),
128 CB_SETCURSEL,
129 0, /* Index */
130 0);
131
132 /* Get the AM symbol */
133 SendMessage(GetDlgItem(hwndDlg, IDC_TIMEAMSYMBOL),
134 CB_LIMITTEXT, MAX_TIMEAMSYMBOL, 0);
135 nLen = GetLocaleInfo(pGlobalData->lcid, LOCALE_S1159, Buffer, sizeof(Buffer)/sizeof(TCHAR));
136 SendMessage(GetDlgItem(hwndDlg, IDC_TIMEAMSYMBOL),
137 CB_ADDSTRING,
138 0,
139 (LPARAM)Buffer);
140 if (nLen != 0)
141 {
142 SendMessage(GetDlgItem(hwndDlg, IDC_TIMEAMSYMBOL),
143 CB_ADDSTRING,
144 0,
145 (LPARAM)_T(""));
146 }
147 SendMessage(GetDlgItem(hwndDlg, IDC_TIMEAMSYMBOL),
148 CB_SETCURSEL,
149 0, /* Index */
150 0);
151
152 /* Get the PM symbol */
153 SendMessage(GetDlgItem(hwndDlg, IDC_TIMEPMSYMBOL),
154 CB_LIMITTEXT, MAX_TIMEPMSYMBOL, 0);
155 nLen = GetLocaleInfo(pGlobalData->lcid, LOCALE_S2359, Buffer, sizeof(Buffer)/sizeof(TCHAR));
156 SendMessage(GetDlgItem(hwndDlg, IDC_TIMEPMSYMBOL),
157 CB_ADDSTRING,
158 0,
159 (LPARAM)Buffer);
160 if (nLen != 0)
161 {
162 SendMessage(GetDlgItem(hwndDlg, IDC_TIMEPMSYMBOL),
163 CB_ADDSTRING,
164 0,
165 (LPARAM)_T(""));
166 }
167 SendMessage(GetDlgItem(hwndDlg, IDC_TIMEPMSYMBOL),
168 CB_SETCURSEL,
169 0, /* Index */
170 0);
171 }
172 break;
173
174 case WM_COMMAND:
175 switch (LOWORD(wParam))
176 {
177 case IDC_TIMEFORMAT:
178 case IDC_TIMESEPARATOR:
179 case IDC_TIMEAMSYMBOL:
180 case IDC_TIMEPMSYMBOL:
181 if (HIWORD(wParam) == CBN_SELCHANGE ||
182 HIWORD(wParam) == CBN_EDITCHANGE)
183 {
184 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
185 }
186 break;
187 }
188 break;
189
190 case WM_NOTIFY:
191 {
192 LPNMHDR lpnm = (LPNMHDR)lParam;
193
194 if (lpnm->code == (UINT)PSN_APPLY)
195 {
196 TCHAR Buffer[80];
197
198 /* Get selected/typed time format text */
199 GetSelectedComboEntry(hwndDlg, IDC_TIMEFORMAT, Buffer, sizeof(Buffer)/sizeof(TCHAR));
200
201 /* Set time format */
202 SetLocaleInfo(pGlobalData->lcid, LOCALE_STIMEFORMAT, Buffer);
203
204 /* Get selected/typed time separator text */
205 GetSelectedComboEntry(hwndDlg, IDC_TIMESEPARATOR, Buffer, sizeof(Buffer)/sizeof(TCHAR));
206
207 /* Set time separator */
208 SetLocaleInfo(pGlobalData->lcid, LOCALE_STIME, Buffer);
209
210 /* Get selected/typed AM symbol text */
211 GetSelectedComboEntry(hwndDlg, IDC_TIMEAMSYMBOL, Buffer, sizeof(Buffer)/sizeof(TCHAR));
212
213 /* Set the AM symbol */
214 SetLocaleInfo(pGlobalData->lcid, LOCALE_S1159, Buffer);
215
216 /* Get selected/typed PM symbol text */
217 GetSelectedComboEntry(hwndDlg, IDC_TIMEPMSYMBOL, Buffer, sizeof(Buffer)/sizeof(TCHAR));
218
219 /* Set the PM symbol */
220 SetLocaleInfo(pGlobalData->lcid, LOCALE_S2359, Buffer);
221
222 /* Update the time format sample */
223 UpdateTimeSample(GetDlgItem(hwndDlg, IDC_TIMESAMPLE), pGlobalData->lcid);
224 }
225 }
226 break;
227 }
228
229 return FALSE;
230 }
231
232 /* EOF */