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