* Sync up to trunk HEAD (r62975).
[reactos.git] / dll / win32 / comctl32 / datetime.c
1 /*
2 * Date and time picker control
3 *
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1999, 2000 Alex Priem <alexp@sci.kun.nl>
6 * Copyright 2000 Chris Morgan <cmorgan@wpi.edu>
7 * Copyright 2012 Owen Rudge for CodeWeavers
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 *
23 * NOTE
24 *
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 20, 2004, by Dimitrie O. Paun.
27 *
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
31 *
32 * TODO:
33 * -- DTS_APPCANPARSE
34 * -- DTS_SHORTDATECENTURYFORMAT
35 * -- DTN_FORMAT
36 * -- DTN_FORMATQUERY
37 * -- DTN_USERSTRING
38 * -- DTN_WMKEYDOWN
39 * -- FORMATCALLBACK
40 */
41
42 #include "comctl32.h"
43
44 #include <math.h>
45
46 WINE_DEFAULT_DEBUG_CHANNEL(datetime);
47
48 typedef struct
49 {
50 HWND hwndSelf;
51 HWND hMonthCal;
52 HWND hwndNotify;
53 HWND hUpdown;
54 DWORD dwStyle;
55 SYSTEMTIME date;
56 BOOL dateValid;
57 HWND hwndCheckbut;
58 RECT rcClient; /* rect around the edge of the window */
59 RECT rcDraw; /* rect inside of the border */
60 RECT checkbox; /* checkbox allowing the control to be enabled/disabled */
61 RECT calbutton; /* button that toggles the dropdown of the monthcal control */
62 BOOL bCalDepressed; /* TRUE = cal button is depressed */
63 BOOL bDropdownEnabled;
64 int select;
65 WCHAR charsEntered[4];
66 int nCharsEntered;
67 HFONT hFont;
68 int nrFieldsAllocated;
69 int nrFields;
70 int haveFocus;
71 int *fieldspec;
72 RECT *fieldRect;
73 int *buflen;
74 WCHAR textbuf[256];
75 POINT monthcal_pos;
76 int pendingUpdown;
77 } DATETIME_INFO, *LPDATETIME_INFO;
78
79 /* in monthcal.c */
80 extern int MONTHCAL_MonthLength(int month, int year);
81 extern int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace);
82
83 /* this list of defines is closely related to `allowedformatchars' defined
84 * in datetime.c; the high nibble indicates the `base type' of the format
85 * specifier.
86 * Do not change without first reading DATETIME_UseFormat.
87 *
88 */
89
90 #define DT_END_FORMAT 0
91 #define ONEDIGITDAY 0x01
92 #define TWODIGITDAY 0x02
93 #define THREECHARDAY 0x03
94 #define FULLDAY 0x04
95 #define ONEDIGIT12HOUR 0x11
96 #define TWODIGIT12HOUR 0x12
97 #define ONEDIGIT24HOUR 0x21
98 #define TWODIGIT24HOUR 0x22
99 #define ONEDIGITMINUTE 0x31
100 #define TWODIGITMINUTE 0x32
101 #define ONEDIGITMONTH 0x41
102 #define TWODIGITMONTH 0x42
103 #define THREECHARMONTH 0x43
104 #define FULLMONTH 0x44
105 #define ONEDIGITSECOND 0x51
106 #define TWODIGITSECOND 0x52
107 #define ONELETTERAMPM 0x61
108 #define TWOLETTERAMPM 0x62
109 #define ONEDIGITYEAR 0x71
110 #define TWODIGITYEAR 0x72
111 #define INVALIDFULLYEAR 0x73 /* FIXME - yyy is not valid - we'll treat it as yyyy */
112 #define FULLYEAR 0x74
113 #define FORMATCALLBACK 0x81 /* -> maximum of 0x80 callbacks possible */
114 #define FORMATCALLMASK 0x80
115 #define DT_STRING 0x0100
116
117 #define DTHT_DATEFIELD 0xff /* for hit-testing */
118
119 #define DTHT_NONE 0x1000
120 #define DTHT_CHECKBOX 0x2000 /* these should end at '00' , to make */
121 #define DTHT_MCPOPUP 0x3000 /* & DTHT_DATEFIELD 0 when DATETIME_KeyDown */
122 #define DTHT_GOTFOCUS 0x4000 /* tests for date-fields */
123 #define DTHT_NODATEMASK 0xf000 /* to mask check and drop down from others */
124
125 static BOOL DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code);
126 static BOOL DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr);
127 static const WCHAR allowedformatchars[] = {'d', 'h', 'H', 'm', 'M', 's', 't', 'y', 'X', 0};
128 static const int maxrepetition [] = {4,2,2,2,4,2,2,4,-1};
129
130 /* valid date limits */
131 static const SYSTEMTIME max_allowed_date = { /* wYear */ 9999, /* wMonth */ 12, /* wDayOfWeek */ 0, /* wDay */ 31 };
132 static const SYSTEMTIME min_allowed_date = { /* wYear */ 1752, /* wMonth */ 9, /* wDayOfWeek */ 0, /* wDay */ 14 };
133
134 static DWORD
135 DATETIME_GetSystemTime (const DATETIME_INFO *infoPtr, SYSTEMTIME *systime)
136 {
137 if (!systime) return GDT_NONE;
138
139 if ((infoPtr->dwStyle & DTS_SHOWNONE) &&
140 (SendMessageW (infoPtr->hwndCheckbut, BM_GETCHECK, 0, 0) == BST_UNCHECKED))
141 return GDT_NONE;
142
143 *systime = infoPtr->date;
144
145 return GDT_VALID;
146 }
147
148 /* Checks value is within configured date range
149 *
150 * PARAMETERS
151 *
152 * [I] infoPtr : valid pointer to control data
153 * [I] date : pointer to valid date data to check
154 *
155 * RETURN VALUE
156 *
157 * TRUE - date within configured range
158 * FALSE - date is outside configured range
159 */
160 static BOOL DATETIME_IsDateInValidRange(const DATETIME_INFO *infoPtr, const SYSTEMTIME *date)
161 {
162 SYSTEMTIME range[2];
163 DWORD limits;
164
165 if ((MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) ||
166 (MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1))
167 return FALSE;
168
169 limits = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, 0, (LPARAM)range);
170
171 if (limits & GDTR_MAX)
172 {
173 if (MONTHCAL_CompareSystemTime(date, &range[1]) == 1)
174 return FALSE;
175 }
176
177 if (limits & GDTR_MIN)
178 {
179 if (MONTHCAL_CompareSystemTime(date, &range[0]) == -1)
180 return FALSE;
181 }
182
183 return TRUE;
184 }
185
186 static BOOL
187 DATETIME_SetSystemTime (DATETIME_INFO *infoPtr, DWORD flag, const SYSTEMTIME *systime)
188 {
189 if (!systime) return FALSE;
190
191 TRACE("%04d/%02d/%02d %02d:%02d:%02d\n",
192 systime->wYear, systime->wMonth, systime->wDay,
193 systime->wHour, systime->wMinute, systime->wSecond);
194
195 if (flag == GDT_VALID) {
196 if (systime->wYear == 0 ||
197 systime->wMonth < 1 || systime->wMonth > 12 ||
198 systime->wDay < 1 ||
199 systime->wDay > MONTHCAL_MonthLength(systime->wMonth, systime->wYear) ||
200 systime->wHour > 23 ||
201 systime->wMinute > 59 ||
202 systime->wSecond > 59 ||
203 systime->wMilliseconds > 999
204 )
205 return FALSE;
206
207 /* Windows returns true if the date is valid but outside the limits set */
208 if (!DATETIME_IsDateInValidRange(infoPtr, systime))
209 return TRUE;
210
211 infoPtr->dateValid = TRUE;
212 infoPtr->date = *systime;
213 /* always store a valid day of week */
214 MONTHCAL_CalculateDayOfWeek(&infoPtr->date, TRUE);
215
216 SendMessageW (infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
217 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
218 } else if ((infoPtr->dwStyle & DTS_SHOWNONE) && (flag == GDT_NONE)) {
219 infoPtr->dateValid = FALSE;
220 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_UNCHECKED, 0);
221 }
222 else
223 return FALSE;
224
225 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
226 return TRUE;
227 }
228
229
230 /***
231 * Split up a formattxt in actions.
232 * See ms documentation for the meaning of the letter codes/'specifiers'.
233 *
234 * Notes:
235 * *'dddddd' is handled as 'dddd' plus 'dd'.
236 * *unrecognized formats are strings (here given the type DT_STRING;
237 * start of the string is encoded in lower bits of DT_STRING.
238 * Therefore, 'string' ends finally up as '<show seconds>tring'.
239 *
240 */
241 static void
242 DATETIME_UseFormat (DATETIME_INFO *infoPtr, LPCWSTR formattxt)
243 {
244 unsigned int i;
245 int j, k, len;
246 BOOL inside_literal = FALSE; /* inside '...' */
247 int *nrFields = &infoPtr->nrFields;
248
249 *nrFields = 0;
250 infoPtr->fieldspec[*nrFields] = 0;
251 len = strlenW(allowedformatchars);
252 k = 0;
253
254 for (i = 0; formattxt[i]; i++) {
255 TRACE ("\n%d %c:", i, formattxt[i]);
256 if (!inside_literal) {
257 for (j = 0; j < len; j++) {
258 if (allowedformatchars[j]==formattxt[i]) {
259 TRACE ("%c[%d,%x]", allowedformatchars[j], *nrFields, infoPtr->fieldspec[*nrFields]);
260 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
261 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
262 break;
263 }
264 if (infoPtr->fieldspec[*nrFields] >> 4 != j) {
265 (*nrFields)++;
266 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
267 break;
268 }
269 if ((infoPtr->fieldspec[*nrFields] & 0x0f) == maxrepetition[j]) {
270 (*nrFields)++;
271 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
272 break;
273 }
274 infoPtr->fieldspec[*nrFields]++;
275 break;
276 } /* if allowedformatchar */
277 } /* for j */
278 }
279 else
280 j = len;
281
282 if (formattxt[i] == '\'')
283 {
284 inside_literal = !inside_literal;
285 continue;
286 }
287
288 /* char is not a specifier: handle char like a string */
289 if (j == len) {
290 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
291 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
292 infoPtr->buflen[*nrFields] = 0;
293 } else if ((infoPtr->fieldspec[*nrFields] & DT_STRING) != DT_STRING) {
294 (*nrFields)++;
295 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
296 infoPtr->buflen[*nrFields] = 0;
297 }
298 infoPtr->textbuf[k] = formattxt[i];
299 k++;
300 infoPtr->buflen[*nrFields]++;
301 } /* if j=len */
302
303 if (*nrFields == infoPtr->nrFieldsAllocated) {
304 FIXME ("out of memory; should reallocate. crash ahead.\n");
305 }
306 } /* for i */
307
308 TRACE("\n");
309
310 if (infoPtr->fieldspec[*nrFields] != 0) (*nrFields)++;
311 }
312
313
314 static BOOL
315 DATETIME_SetFormatW (DATETIME_INFO *infoPtr, LPCWSTR format)
316 {
317 WCHAR format_buf[80];
318
319 if (!format) {
320 DWORD format_item;
321
322 if (infoPtr->dwStyle & DTS_LONGDATEFORMAT)
323 format_item = LOCALE_SLONGDATE;
324 else if ((infoPtr->dwStyle & DTS_TIMEFORMAT) == DTS_TIMEFORMAT)
325 format_item = LOCALE_STIMEFORMAT;
326 else /* DTS_SHORTDATEFORMAT */
327 format_item = LOCALE_SSHORTDATE;
328 GetLocaleInfoW(LOCALE_USER_DEFAULT, format_item, format_buf, sizeof(format_buf)/sizeof(format_buf[0]));
329 format = format_buf;
330 }
331
332 DATETIME_UseFormat (infoPtr, format);
333 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
334
335 return TRUE;
336 }
337
338
339 static BOOL
340 DATETIME_SetFormatA (DATETIME_INFO *infoPtr, LPCSTR lpszFormat)
341 {
342 if (lpszFormat) {
343 BOOL retval;
344 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, NULL, 0);
345 LPWSTR wstr = Alloc(len * sizeof(WCHAR));
346 if (wstr) MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, wstr, len);
347 retval = DATETIME_SetFormatW (infoPtr, wstr);
348 Free (wstr);
349 return retval;
350 }
351 else
352 return DATETIME_SetFormatW (infoPtr, 0);
353
354 }
355
356
357 static void
358 DATETIME_ReturnTxt (const DATETIME_INFO *infoPtr, int count, LPWSTR result, int resultSize)
359 {
360 static const WCHAR fmt_dW[] = { '%', 'd', 0 };
361 static const WCHAR fmt__2dW[] = { '%', '.', '2', 'd', 0 };
362 static const WCHAR fmt__3sW[] = { '%', '.', '3', 's', 0 };
363 SYSTEMTIME date = infoPtr->date;
364 int spec;
365 WCHAR buffer[80];
366
367 *result=0;
368 TRACE ("%d,%d\n", infoPtr->nrFields, count);
369 if (count>infoPtr->nrFields || count < 0) {
370 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
371 return;
372 }
373
374 if (!infoPtr->fieldspec) return;
375
376 spec = infoPtr->fieldspec[count];
377 if (spec & DT_STRING) {
378 int txtlen = infoPtr->buflen[count];
379
380 if (txtlen > resultSize)
381 txtlen = resultSize - 1;
382 memcpy (result, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
383 result[txtlen] = 0;
384 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
385 return;
386 }
387
388
389 switch (spec) {
390 case DT_END_FORMAT:
391 *result = 0;
392 break;
393 case ONEDIGITDAY:
394 wsprintfW (result, fmt_dW, date.wDay);
395 break;
396 case TWODIGITDAY:
397 wsprintfW (result, fmt__2dW, date.wDay);
398 break;
399 case THREECHARDAY:
400 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1+(date.wDayOfWeek+6)%7, result, 4);
401 /*wsprintfW (result,"%.3s",days[date.wDayOfWeek]);*/
402 break;
403 case FULLDAY:
404 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME1+(date.wDayOfWeek+6)%7, result, resultSize);
405 break;
406 case ONEDIGIT12HOUR:
407 if (date.wHour == 0) {
408 result[0] = '1';
409 result[1] = '2';
410 result[2] = 0;
411 }
412 else
413 wsprintfW (result, fmt_dW, date.wHour - (date.wHour > 12 ? 12 : 0));
414 break;
415 case TWODIGIT12HOUR:
416 if (date.wHour == 0) {
417 result[0] = '1';
418 result[1] = '2';
419 result[2] = 0;
420 }
421 else
422 wsprintfW (result, fmt__2dW, date.wHour - (date.wHour > 12 ? 12 : 0));
423 break;
424 case ONEDIGIT24HOUR:
425 wsprintfW (result, fmt_dW, date.wHour);
426 break;
427 case TWODIGIT24HOUR:
428 wsprintfW (result, fmt__2dW, date.wHour);
429 break;
430 case ONEDIGITSECOND:
431 wsprintfW (result, fmt_dW, date.wSecond);
432 break;
433 case TWODIGITSECOND:
434 wsprintfW (result, fmt__2dW, date.wSecond);
435 break;
436 case ONEDIGITMINUTE:
437 wsprintfW (result, fmt_dW, date.wMinute);
438 break;
439 case TWODIGITMINUTE:
440 wsprintfW (result, fmt__2dW, date.wMinute);
441 break;
442 case ONEDIGITMONTH:
443 wsprintfW (result, fmt_dW, date.wMonth);
444 break;
445 case TWODIGITMONTH:
446 wsprintfW (result, fmt__2dW, date.wMonth);
447 break;
448 case THREECHARMONTH:
449 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+date.wMonth -1,
450 buffer, sizeof(buffer)/sizeof(buffer[0]));
451 wsprintfW (result, fmt__3sW, buffer);
452 break;
453 case FULLMONTH:
454 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+date.wMonth -1,
455 result, resultSize);
456 break;
457 case ONELETTERAMPM:
458 result[0] = (date.wHour < 12 ? 'A' : 'P');
459 result[1] = 0;
460 break;
461 case TWOLETTERAMPM:
462 result[0] = (date.wHour < 12 ? 'A' : 'P');
463 result[1] = 'M';
464 result[2] = 0;
465 break;
466 case FORMATCALLBACK:
467 FIXME ("Not implemented\n");
468 result[0] = 'x';
469 result[1] = 0;
470 break;
471 case ONEDIGITYEAR:
472 wsprintfW (result, fmt_dW, date.wYear-10* (int) floor(date.wYear/10));
473 break;
474 case TWODIGITYEAR:
475 wsprintfW (result, fmt__2dW, date.wYear-100* (int) floor(date.wYear/100));
476 break;
477 case INVALIDFULLYEAR:
478 case FULLYEAR:
479 wsprintfW (result, fmt_dW, date.wYear);
480 break;
481 }
482
483 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
484 }
485
486 static int wrap(int val, int delta, int minVal, int maxVal)
487 {
488 val += delta;
489 if (delta == INT_MIN || val < minVal) return maxVal;
490 if (delta == INT_MAX || val > maxVal) return minVal;
491 return val;
492 }
493
494 static void
495 DATETIME_IncreaseField (DATETIME_INFO *infoPtr, int number, int delta)
496 {
497 SYSTEMTIME *date = &infoPtr->date;
498 SYSTEMTIME range[2];
499 DWORD limits;
500 BOOL min;
501
502 TRACE ("%d\n", number);
503 if ((number > infoPtr->nrFields) || (number < 0)) return;
504
505 if ((infoPtr->fieldspec[number] & DTHT_DATEFIELD) == 0) return;
506
507 switch (infoPtr->fieldspec[number]) {
508 case ONEDIGITYEAR:
509 case TWODIGITYEAR:
510 case FULLYEAR:
511 if (delta == INT_MIN)
512 date->wYear = 1752;
513 else if (delta == INT_MAX)
514 date->wYear = 9999;
515 else
516 date->wYear = max(min(date->wYear + delta, 9999), 1752);
517
518 if (date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
519 /* This can happen when moving away from a leap year. */
520 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear);
521 MONTHCAL_CalculateDayOfWeek(date, TRUE);
522 break;
523 case ONEDIGITMONTH:
524 case TWODIGITMONTH:
525 case THREECHARMONTH:
526 case FULLMONTH:
527 date->wMonth = wrap(date->wMonth, delta, 1, 12);
528 MONTHCAL_CalculateDayOfWeek(date, TRUE);
529 delta = 0;
530 /* fall through */
531 case ONEDIGITDAY:
532 case TWODIGITDAY:
533 case THREECHARDAY:
534 case FULLDAY:
535 date->wDay = wrap(date->wDay, delta, 1, MONTHCAL_MonthLength(date->wMonth, date->wYear));
536 MONTHCAL_CalculateDayOfWeek(date, TRUE);
537 break;
538 case ONELETTERAMPM:
539 case TWOLETTERAMPM:
540 delta *= 12;
541 /* fall through */
542 case ONEDIGIT12HOUR:
543 case TWODIGIT12HOUR:
544 case ONEDIGIT24HOUR:
545 case TWODIGIT24HOUR:
546 date->wHour = wrap(date->wHour, delta, 0, 23);
547 break;
548 case ONEDIGITMINUTE:
549 case TWODIGITMINUTE:
550 date->wMinute = wrap(date->wMinute, delta, 0, 59);
551 break;
552 case ONEDIGITSECOND:
553 case TWODIGITSECOND:
554 date->wSecond = wrap(date->wSecond, delta, 0, 59);
555 break;
556 case FORMATCALLBACK:
557 FIXME ("Not implemented\n");
558 break;
559 }
560
561 /* FYI: On 1752/9/14 the calendar changed and England and the
562 * American colonies changed to the Gregorian calendar. This change
563 * involved having September 14th follow September 2nd. So no date
564 * algorithm works before that date.
565 */
566 if (10000 * date->wYear + 100 * date->wMonth + date->wDay < 17520914) {
567 date->wYear = 1752;
568 date->wMonth = 9;
569 date->wDay = 14;
570 date->wSecond = 0;
571 date->wMinute = 0;
572 date->wHour = 0;
573 }
574
575 /* Ensure time is within bounds */
576 limits = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, 0, (LPARAM)range);
577 min = delta < 0;
578
579 if (limits & (min ? GDTR_MIN : GDTR_MAX))
580 {
581 int i = (min ? 0 : 1);
582
583 if (MONTHCAL_CompareSystemTime(date, &range[i]) == (min ? -1 : 1))
584 {
585 date->wYear = range[i].wYear;
586 date->wMonth = range[i].wMonth;
587 date->wDayOfWeek = range[i].wDayOfWeek;
588 date->wDay = range[i].wDay;
589 date->wHour = range[i].wHour;
590 date->wMinute = range[i].wMinute;
591 date->wSecond = range[i].wSecond;
592 date->wMilliseconds = range[i].wMilliseconds;
593 }
594 }
595 }
596
597 static void
598 DATETIME_ReturnFieldWidth (const DATETIME_INFO *infoPtr, HDC hdc, int count, SHORT *width)
599 {
600 /* fields are a fixed width, determined by the largest possible string */
601 /* presumably, these widths should be language dependent */
602 static const WCHAR fld_d1W[] = { '2', 0 };
603 static const WCHAR fld_d2W[] = { '2', '2', 0 };
604 static const WCHAR fld_d4W[] = { '2', '2', '2', '2', 0 };
605 static const WCHAR fld_am1[] = { 'A', 0 };
606 static const WCHAR fld_am2[] = { 'A', 'M', 0 };
607 int spec;
608 WCHAR buffer[80];
609 LPCWSTR bufptr;
610 SIZE size;
611
612 TRACE ("%d,%d\n", infoPtr->nrFields, count);
613 if (count>infoPtr->nrFields || count < 0) {
614 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
615 return;
616 }
617
618 if (!infoPtr->fieldspec) return;
619
620 spec = infoPtr->fieldspec[count];
621 if (spec & DT_STRING) {
622 int txtlen = infoPtr->buflen[count];
623
624 if (txtlen > 79)
625 txtlen = 79;
626 memcpy (buffer, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
627 buffer[txtlen] = 0;
628 bufptr = buffer;
629 }
630 else {
631 switch (spec) {
632 case ONEDIGITDAY:
633 case ONEDIGIT12HOUR:
634 case ONEDIGIT24HOUR:
635 case ONEDIGITSECOND:
636 case ONEDIGITMINUTE:
637 case ONEDIGITMONTH:
638 case ONEDIGITYEAR:
639 /* these seem to use a two byte field */
640 case TWODIGITDAY:
641 case TWODIGIT12HOUR:
642 case TWODIGIT24HOUR:
643 case TWODIGITSECOND:
644 case TWODIGITMINUTE:
645 case TWODIGITMONTH:
646 case TWODIGITYEAR:
647 bufptr = fld_d2W;
648 break;
649 case INVALIDFULLYEAR:
650 case FULLYEAR:
651 bufptr = fld_d4W;
652 break;
653 case THREECHARMONTH:
654 case FULLMONTH:
655 case THREECHARDAY:
656 case FULLDAY:
657 {
658 static const WCHAR fld_day[] = {'W','e','d','n','e','s','d','a','y',0};
659 static const WCHAR fld_abbrday[] = {'W','e','d',0};
660 static const WCHAR fld_mon[] = {'S','e','p','t','e','m','b','e','r',0};
661 static const WCHAR fld_abbrmon[] = {'D','e','c',0};
662
663 const WCHAR *fall;
664 LCTYPE lctype;
665 INT i, max_count;
666 LONG cx;
667
668 /* choose locale data type and fallback string */
669 switch (spec) {
670 case THREECHARDAY:
671 fall = fld_abbrday;
672 lctype = LOCALE_SABBREVDAYNAME1;
673 max_count = 7;
674 break;
675 case FULLDAY:
676 fall = fld_day;
677 lctype = LOCALE_SDAYNAME1;
678 max_count = 7;
679 break;
680 case THREECHARMONTH:
681 fall = fld_abbrmon;
682 lctype = LOCALE_SABBREVMONTHNAME1;
683 max_count = 12;
684 break;
685 default: /* FULLMONTH */
686 fall = fld_mon;
687 lctype = LOCALE_SMONTHNAME1;
688 max_count = 12;
689 break;
690 }
691
692 cx = 0;
693 for (i = 0; i < max_count; i++)
694 {
695 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, lctype + i,
696 buffer, lstrlenW(buffer)))
697 {
698 GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &size);
699 if (size.cx > cx) cx = size.cx;
700 }
701 else /* locale independent fallback on failure */
702 {
703 GetTextExtentPoint32W(hdc, fall, lstrlenW(fall), &size);
704 cx = size.cx;
705 break;
706 }
707 }
708 *width = cx;
709 return;
710 }
711 case ONELETTERAMPM:
712 bufptr = fld_am1;
713 break;
714 case TWOLETTERAMPM:
715 bufptr = fld_am2;
716 break;
717 default:
718 bufptr = fld_d1W;
719 break;
720 }
721 }
722 GetTextExtentPoint32W (hdc, bufptr, strlenW(bufptr), &size);
723 *width = size.cx;
724 }
725
726 static void
727 DATETIME_Refresh (DATETIME_INFO *infoPtr, HDC hdc)
728 {
729 TRACE("\n");
730
731 if (infoPtr->dateValid) {
732 int i, prevright;
733 RECT *field;
734 RECT *rcDraw = &infoPtr->rcDraw;
735 SIZE size;
736 COLORREF oldTextColor;
737 SHORT fieldWidth = 0;
738 HFONT oldFont = SelectObject (hdc, infoPtr->hFont);
739 INT oldBkMode = SetBkMode (hdc, TRANSPARENT);
740 WCHAR txt[80];
741
742 DATETIME_ReturnTxt (infoPtr, 0, txt, sizeof(txt)/sizeof(txt[0]));
743 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
744 rcDraw->bottom = size.cy + 2;
745
746 prevright = infoPtr->checkbox.right = ((infoPtr->dwStyle & DTS_SHOWNONE) ? 18 : 2);
747
748 for (i = 0; i < infoPtr->nrFields; i++) {
749 DATETIME_ReturnTxt (infoPtr, i, txt, sizeof(txt)/sizeof(txt[0]));
750 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
751 DATETIME_ReturnFieldWidth (infoPtr, hdc, i, &fieldWidth);
752 field = &infoPtr->fieldRect[i];
753 field->left = prevright;
754 field->right = prevright + fieldWidth;
755 field->top = rcDraw->top;
756 field->bottom = rcDraw->bottom;
757 prevright = field->right;
758
759 if (infoPtr->dwStyle & WS_DISABLED)
760 oldTextColor = SetTextColor (hdc, comctl32_color.clrGrayText);
761 else if ((infoPtr->haveFocus) && (i == infoPtr->select)) {
762 RECT selection;
763
764 /* fill if focused */
765 HBRUSH hbr = CreateSolidBrush (comctl32_color.clrActiveCaption);
766
767 if (infoPtr->nCharsEntered)
768 {
769 memcpy(txt, infoPtr->charsEntered, infoPtr->nCharsEntered * sizeof(WCHAR));
770 txt[infoPtr->nCharsEntered] = 0;
771 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
772 }
773
774 selection.left = 0;
775 selection.top = 0;
776 selection.right = size.cx;
777 selection.bottom = size.cy;
778 /* center rectangle */
779 OffsetRect(&selection, (field->right + field->left - size.cx)/2,
780 (field->bottom - size.cy)/2);
781
782 FillRect(hdc, &selection, hbr);
783 DeleteObject (hbr);
784 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindow);
785 }
786 else
787 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindowText);
788
789 /* draw the date text using the colour set above */
790 DrawTextW (hdc, txt, strlenW(txt), field, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
791 SetTextColor (hdc, oldTextColor);
792 }
793 SetBkMode (hdc, oldBkMode);
794 SelectObject (hdc, oldFont);
795 }
796
797 if (!(infoPtr->dwStyle & DTS_UPDOWN)) {
798 DrawFrameControl(hdc, &infoPtr->calbutton, DFC_SCROLL,
799 DFCS_SCROLLDOWN | (infoPtr->bCalDepressed ? DFCS_PUSHED : 0) |
800 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
801 }
802 }
803
804
805 static INT
806 DATETIME_HitTest (const DATETIME_INFO *infoPtr, POINT pt)
807 {
808 int i;
809
810 TRACE ("%d, %d\n", pt.x, pt.y);
811
812 if (PtInRect (&infoPtr->calbutton, pt)) return DTHT_MCPOPUP;
813 if (PtInRect (&infoPtr->checkbox, pt)) return DTHT_CHECKBOX;
814
815 for (i = 0; i < infoPtr->nrFields; i++) {
816 if (PtInRect (&infoPtr->fieldRect[i], pt)) return i;
817 }
818
819 return DTHT_NONE;
820 }
821
822 /* Returns index of the nearest preceding date field from given,
823 or -1 if none was found */
824 static int DATETIME_GetPrevDateField(const DATETIME_INFO *infoPtr, int i)
825 {
826 for(--i; i >= 0; i--)
827 {
828 if (infoPtr->fieldspec[i] & DTHT_DATEFIELD) return i;
829 }
830 return -1;
831 }
832
833 static void
834 DATETIME_ApplySelectedField (DATETIME_INFO *infoPtr)
835 {
836 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
837 int i, val = 0;
838 BOOL clamp_day = FALSE;
839 SYSTEMTIME date = infoPtr->date;
840 int oldyear;
841
842 if (infoPtr->select == -1 || infoPtr->nCharsEntered == 0)
843 return;
844
845 if ((infoPtr->fieldspec[fieldNum] == ONELETTERAMPM) ||
846 (infoPtr->fieldspec[fieldNum] == TWOLETTERAMPM))
847 val = infoPtr->charsEntered[0];
848 else {
849 for (i=0; i<infoPtr->nCharsEntered; i++)
850 val = val * 10 + infoPtr->charsEntered[i] - '0';
851 }
852
853 infoPtr->nCharsEntered = 0;
854
855 switch (infoPtr->fieldspec[fieldNum]) {
856 case ONEDIGITYEAR:
857 case TWODIGITYEAR:
858 oldyear = date.wYear;
859 date.wYear = date.wYear - (date.wYear%100) + val;
860
861 if (DATETIME_IsDateInValidRange(infoPtr, &date))
862 clamp_day = TRUE;
863 else
864 date.wYear = oldyear;
865
866 break;
867 case INVALIDFULLYEAR:
868 case FULLYEAR:
869 oldyear = date.wYear;
870 date.wYear = val;
871
872 if (DATETIME_IsDateInValidRange(infoPtr, &date))
873 clamp_day = TRUE;
874 else
875 date.wYear = oldyear;
876
877 break;
878 case ONEDIGITMONTH:
879 case TWODIGITMONTH:
880 date.wMonth = val;
881 clamp_day = TRUE;
882 break;
883 case ONEDIGITDAY:
884 case TWODIGITDAY:
885 date.wDay = val;
886 break;
887 case ONEDIGIT12HOUR:
888 case TWODIGIT12HOUR:
889 if (val >= 24)
890 val -= 20;
891
892 if (val >= 13)
893 date.wHour = val;
894 else if (val != 0) {
895 if (date.wHour >= 12) /* preserve current AM/PM state */
896 date.wHour = (val == 12 ? 12 : val + 12);
897 else
898 date.wHour = (val == 12 ? 0 : val);
899 }
900 break;
901 case ONEDIGIT24HOUR:
902 case TWODIGIT24HOUR:
903 date.wHour = val;
904 break;
905 case ONEDIGITMINUTE:
906 case TWODIGITMINUTE:
907 date.wMinute = val;
908 break;
909 case ONEDIGITSECOND:
910 case TWODIGITSECOND:
911 date.wSecond = val;
912 break;
913 case ONELETTERAMPM:
914 case TWOLETTERAMPM:
915 if (val == 'a' || val == 'A') {
916 if (date.wHour >= 12)
917 date.wHour -= 12;
918 } else if (val == 'p' || val == 'P') {
919 if (date.wHour < 12)
920 date.wHour += 12;
921 }
922 break;
923 }
924
925 if (clamp_day && date.wDay > MONTHCAL_MonthLength(date.wMonth, date.wYear))
926 date.wDay = MONTHCAL_MonthLength(date.wMonth, date.wYear);
927
928 if (DATETIME_SetSystemTime(infoPtr, GDT_VALID, &date))
929 DATETIME_SendDateTimeChangeNotify (infoPtr);
930 }
931
932 static void
933 DATETIME_SetSelectedField (DATETIME_INFO *infoPtr, int select)
934 {
935 DATETIME_ApplySelectedField(infoPtr);
936
937 infoPtr->select = select;
938 infoPtr->nCharsEntered = 0;
939 }
940
941 static LRESULT
942 DATETIME_LButtonDown (DATETIME_INFO *infoPtr, INT x, INT y)
943 {
944 POINT pt;
945 int new;
946
947 pt.x = x;
948 pt.y = y;
949 new = DATETIME_HitTest (infoPtr, pt);
950
951 SetFocus(infoPtr->hwndSelf);
952
953 if (!(new & DTHT_NODATEMASK) || (new == DTHT_NONE))
954 {
955 if (new == DTHT_NONE)
956 new = infoPtr->nrFields - 1;
957 else
958 {
959 /* hitting string part moves selection to next date field to left */
960 if (infoPtr->fieldspec[new] & DT_STRING)
961 {
962 new = DATETIME_GetPrevDateField(infoPtr, new);
963 if (new == -1) return 0;
964 }
965 /* never select full day of week */
966 if (infoPtr->fieldspec[new] == FULLDAY) return 0;
967 }
968 }
969
970 DATETIME_SetSelectedField(infoPtr, new);
971
972 if (infoPtr->select == DTHT_MCPOPUP) {
973 RECT rcMonthCal;
974 POINT pos;
975 SendMessageW(infoPtr->hMonthCal, MCM_GETMINREQRECT, 0, (LPARAM)&rcMonthCal);
976
977 /* FIXME: button actually is only depressed during dropdown of the */
978 /* calendar control and when the mouse is over the button window */
979 infoPtr->bCalDepressed = TRUE;
980
981 /* recalculate the position of the monthcal popup */
982 if(infoPtr->dwStyle & DTS_RIGHTALIGN)
983 pos.x = infoPtr->calbutton.left - (rcMonthCal.right - rcMonthCal.left);
984 else
985 /* FIXME: this should be after the area reserved for the checkbox */
986 pos.x = infoPtr->rcDraw.left;
987
988 pos.y = infoPtr->rcClient.bottom;
989 OffsetRect( &rcMonthCal, pos.x, pos.y );
990 MapWindowPoints( infoPtr->hwndSelf, 0, (POINT *)&rcMonthCal, 2 );
991 SetWindowPos(infoPtr->hMonthCal, 0, rcMonthCal.left, rcMonthCal.top,
992 rcMonthCal.right - rcMonthCal.left, rcMonthCal.bottom - rcMonthCal.top, 0);
993
994 if(IsWindowVisible(infoPtr->hMonthCal)) {
995 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
996 infoPtr->bDropdownEnabled = FALSE;
997 DATETIME_SendSimpleNotify (infoPtr, DTN_CLOSEUP);
998 } else {
999 const SYSTEMTIME *lprgSysTimeArray = &infoPtr->date;
1000 TRACE("update calendar %04d/%02d/%02d\n",
1001 lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay);
1002 SendMessageW(infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
1003
1004 if (infoPtr->bDropdownEnabled) {
1005 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
1006 DATETIME_SendSimpleNotify (infoPtr, DTN_DROPDOWN);
1007 }
1008 infoPtr->bDropdownEnabled = TRUE;
1009 }
1010
1011 TRACE ("dt:%p mc:%p mc parent:%p, desktop:%p\n",
1012 infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hwndNotify, GetDesktopWindow ());
1013 }
1014
1015 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1016
1017 return 0;
1018 }
1019
1020
1021 static LRESULT
1022 DATETIME_LButtonUp (DATETIME_INFO *infoPtr)
1023 {
1024 if(infoPtr->bCalDepressed) {
1025 infoPtr->bCalDepressed = FALSE;
1026 InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
1027 }
1028
1029 return 0;
1030 }
1031
1032
1033 static LRESULT
1034 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
1035 {
1036 if (!hdc) {
1037 PAINTSTRUCT ps;
1038 hdc = BeginPaint (infoPtr->hwndSelf, &ps);
1039 DATETIME_Refresh (infoPtr, hdc);
1040 EndPaint (infoPtr->hwndSelf, &ps);
1041 } else {
1042 DATETIME_Refresh (infoPtr, hdc);
1043 }
1044
1045 /* Not a click on the dropdown box, enabled it */
1046 infoPtr->bDropdownEnabled = TRUE;
1047
1048 return 0;
1049 }
1050
1051
1052 static LRESULT
1053 DATETIME_Button_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1054 {
1055 if( HIWORD(wParam) == BN_CLICKED) {
1056 DWORD state = SendMessageW((HWND)lParam, BM_GETCHECK, 0, 0);
1057 infoPtr->dateValid = (state == BST_CHECKED);
1058 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1059 }
1060 return 0;
1061 }
1062
1063
1064
1065 static LRESULT
1066 DATETIME_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1067 {
1068 TRACE("hwndbutton = %p\n", infoPtr->hwndCheckbut);
1069 if(infoPtr->hwndCheckbut == (HWND)lParam)
1070 return DATETIME_Button_Command(infoPtr, wParam, lParam);
1071 return 0;
1072 }
1073
1074
1075 static LRESULT
1076 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
1077 {
1078 TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
1079 if (bEnable)
1080 infoPtr->dwStyle &= ~WS_DISABLED;
1081 else
1082 infoPtr->dwStyle |= WS_DISABLED;
1083
1084 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1085
1086 return 0;
1087 }
1088
1089
1090 static LRESULT
1091 DATETIME_EraseBackground (const DATETIME_INFO *infoPtr, HDC hdc)
1092 {
1093 HBRUSH hBrush, hSolidBrush = NULL;
1094 RECT rc;
1095
1096 if (infoPtr->dwStyle & WS_DISABLED)
1097 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
1098 else
1099 {
1100 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
1101 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
1102 if (!hBrush)
1103 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
1104 }
1105
1106 GetClientRect (infoPtr->hwndSelf, &rc);
1107
1108 FillRect (hdc, &rc, hBrush);
1109
1110 if (hSolidBrush)
1111 DeleteObject(hSolidBrush);
1112
1113 return -1;
1114 }
1115
1116
1117 static LRESULT
1118 DATETIME_Notify (DATETIME_INFO *infoPtr, const NMHDR *lpnmh)
1119 {
1120 TRACE ("Got notification %x from %p\n", lpnmh->code, lpnmh->hwndFrom);
1121 TRACE ("info: %p %p %p\n", infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hUpdown);
1122
1123 if (lpnmh->code == MCN_SELECT) {
1124 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1125 infoPtr->dateValid = TRUE;
1126 SendMessageW (infoPtr->hMonthCal, MCM_GETCURSEL, 0, (LPARAM)&infoPtr->date);
1127 TRACE("got from calendar %04d/%02d/%02d day of week %d\n",
1128 infoPtr->date.wYear, infoPtr->date.wMonth, infoPtr->date.wDay, infoPtr->date.wDayOfWeek);
1129 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
1130 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1131 DATETIME_SendDateTimeChangeNotify (infoPtr);
1132 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1133 }
1134 if ((lpnmh->hwndFrom == infoPtr->hUpdown) && (lpnmh->code == UDN_DELTAPOS)) {
1135 const NM_UPDOWN *lpnmud = (const NM_UPDOWN*)lpnmh;
1136 TRACE("Delta pos %d\n", lpnmud->iDelta);
1137 infoPtr->pendingUpdown = lpnmud->iDelta;
1138 }
1139 return 0;
1140 }
1141
1142
1143 static LRESULT
1144 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode)
1145 {
1146 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1147 int wrap = 0;
1148 int new;
1149
1150 if (!(infoPtr->haveFocus)) return 0;
1151 if ((fieldNum==0) && (infoPtr->select)) return 0;
1152
1153 if (infoPtr->select & FORMATCALLMASK) {
1154 FIXME ("Callbacks not implemented yet\n");
1155 }
1156
1157 switch (vkCode) {
1158 case VK_ADD:
1159 case VK_UP:
1160 infoPtr->nCharsEntered = 0;
1161 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1162 DATETIME_SendDateTimeChangeNotify (infoPtr);
1163 break;
1164 case VK_SUBTRACT:
1165 case VK_DOWN:
1166 infoPtr->nCharsEntered = 0;
1167 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1168 DATETIME_SendDateTimeChangeNotify (infoPtr);
1169 break;
1170 case VK_HOME:
1171 infoPtr->nCharsEntered = 0;
1172 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MIN);
1173 DATETIME_SendDateTimeChangeNotify (infoPtr);
1174 break;
1175 case VK_END:
1176 infoPtr->nCharsEntered = 0;
1177 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MAX);
1178 DATETIME_SendDateTimeChangeNotify (infoPtr);
1179 break;
1180 case VK_LEFT:
1181 new = infoPtr->select;
1182 do {
1183 if (new == 0) {
1184 new = new - 1;
1185 wrap++;
1186 } else {
1187 new--;
1188 }
1189 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1190 if (new != infoPtr->select)
1191 DATETIME_SetSelectedField(infoPtr, new);
1192 break;
1193 case VK_RIGHT:
1194 new = infoPtr->select;
1195 do {
1196 new++;
1197 if (new==infoPtr->nrFields) {
1198 new = 0;
1199 wrap++;
1200 }
1201 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1202 if (new != infoPtr->select)
1203 DATETIME_SetSelectedField(infoPtr, new);
1204 break;
1205 }
1206
1207 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1208
1209 return 0;
1210 }
1211
1212
1213 static LRESULT
1214 DATETIME_Char (DATETIME_INFO *infoPtr, WPARAM vkCode)
1215 {
1216 int fieldNum, fieldSpec;
1217
1218 fieldNum = infoPtr->select & DTHT_DATEFIELD;
1219 fieldSpec = infoPtr->fieldspec[fieldNum];
1220
1221 if (fieldSpec == ONELETTERAMPM || fieldSpec == TWOLETTERAMPM) {
1222 infoPtr->charsEntered[0] = vkCode;
1223 infoPtr->nCharsEntered = 1;
1224
1225 DATETIME_ApplySelectedField(infoPtr);
1226 } else if (vkCode >= '0' && vkCode <= '9') {
1227 int maxChars;
1228
1229 infoPtr->charsEntered[infoPtr->nCharsEntered++] = vkCode;
1230
1231 if (fieldSpec == INVALIDFULLYEAR || fieldSpec == FULLYEAR)
1232 maxChars = 4;
1233 else
1234 maxChars = 2;
1235
1236 if ((fieldSpec == ONEDIGIT12HOUR ||
1237 fieldSpec == TWODIGIT12HOUR ||
1238 fieldSpec == ONEDIGIT24HOUR ||
1239 fieldSpec == TWODIGIT24HOUR) &&
1240 (infoPtr->nCharsEntered == 1))
1241 {
1242 if (vkCode >= '3')
1243 maxChars = 1;
1244 }
1245
1246 if (maxChars == infoPtr->nCharsEntered)
1247 DATETIME_ApplySelectedField(infoPtr);
1248 }
1249
1250 return 0;
1251 }
1252
1253
1254 static LRESULT
1255 DATETIME_VScroll (DATETIME_INFO *infoPtr, WORD wScroll)
1256 {
1257 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1258
1259 if ((SHORT)LOWORD(wScroll) != SB_THUMBPOSITION) return 0;
1260 if (!(infoPtr->haveFocus)) return 0;
1261 if ((fieldNum==0) && (infoPtr->select)) return 0;
1262
1263 if (infoPtr->pendingUpdown >= 0) {
1264 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1265 DATETIME_SendDateTimeChangeNotify (infoPtr);
1266 }
1267 else {
1268 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1269 DATETIME_SendDateTimeChangeNotify (infoPtr);
1270 }
1271
1272 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1273
1274 return 0;
1275 }
1276
1277
1278 static LRESULT
1279 DATETIME_KillFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1280 {
1281 TRACE("lost focus to %p\n", lostFocus);
1282
1283 if (infoPtr->haveFocus) {
1284 DATETIME_SendSimpleNotify (infoPtr, NM_KILLFOCUS);
1285 infoPtr->haveFocus = 0;
1286 DATETIME_SetSelectedField (infoPtr, -1);
1287 }
1288
1289 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
1290
1291 return 0;
1292 }
1293
1294
1295 static LRESULT
1296 DATETIME_NCCreate (HWND hwnd, const CREATESTRUCTW *lpcs)
1297 {
1298 DWORD dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1299 /* force control to have client edge */
1300 dwExStyle |= WS_EX_CLIENTEDGE;
1301 SetWindowLongW(hwnd, GWL_EXSTYLE, dwExStyle);
1302
1303 return DefWindowProcW(hwnd, WM_NCCREATE, 0, (LPARAM)lpcs);
1304 }
1305
1306
1307 static LRESULT
1308 DATETIME_SetFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1309 {
1310 TRACE("got focus from %p\n", lostFocus);
1311
1312 /* if monthcal is open and it loses focus, close monthcal */
1313 if (infoPtr->hMonthCal && (lostFocus == infoPtr->hMonthCal) &&
1314 IsWindowVisible(infoPtr->hMonthCal))
1315 {
1316 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1317 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1318 /* note: this get triggered even if monthcal loses focus to a dropdown
1319 * box click, which occurs without an intermediate WM_PAINT call
1320 */
1321 infoPtr->bDropdownEnabled = FALSE;
1322 return 0;
1323 }
1324
1325 if (infoPtr->haveFocus == 0) {
1326 DATETIME_SendSimpleNotify (infoPtr, NM_SETFOCUS);
1327 infoPtr->haveFocus = DTHT_GOTFOCUS;
1328 }
1329
1330 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1331
1332 return 0;
1333 }
1334
1335
1336 static BOOL
1337 DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr)
1338 {
1339 NMDATETIMECHANGE dtdtc;
1340
1341 dtdtc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1342 dtdtc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1343 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1344
1345 dtdtc.dwFlags = infoPtr->dateValid ? GDT_VALID : GDT_NONE;
1346
1347 dtdtc.st = infoPtr->date;
1348 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1349 dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1350 }
1351
1352
1353 static BOOL
1354 DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code)
1355 {
1356 NMHDR nmhdr;
1357
1358 TRACE("%x\n", code);
1359 nmhdr.hwndFrom = infoPtr->hwndSelf;
1360 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1361 nmhdr.code = code;
1362
1363 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1364 nmhdr.idFrom, (LPARAM)&nmhdr);
1365 }
1366
1367 static LRESULT
1368 DATETIME_Size (DATETIME_INFO *infoPtr, INT width, INT height)
1369 {
1370 /* set size */
1371 infoPtr->rcClient.bottom = height;
1372 infoPtr->rcClient.right = width;
1373
1374 TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1375
1376 infoPtr->rcDraw = infoPtr->rcClient;
1377
1378 if (infoPtr->dwStyle & DTS_UPDOWN) {
1379 SetWindowPos(infoPtr->hUpdown, NULL,
1380 infoPtr->rcClient.right-14, 0,
1381 15, infoPtr->rcClient.bottom - infoPtr->rcClient.top,
1382 SWP_NOACTIVATE | SWP_NOZORDER);
1383 }
1384 else {
1385 /* set the size of the button that drops the calendar down */
1386 /* FIXME: account for style that allows button on left side */
1387 infoPtr->calbutton.top = infoPtr->rcDraw.top;
1388 infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1389 infoPtr->calbutton.left = infoPtr->rcDraw.right-15;
1390 infoPtr->calbutton.right = infoPtr->rcDraw.right;
1391 }
1392
1393 /* set enable/disable button size for show none style being enabled */
1394 /* FIXME: these dimensions are completely incorrect */
1395 infoPtr->checkbox.top = infoPtr->rcDraw.top;
1396 infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1397 infoPtr->checkbox.left = infoPtr->rcDraw.left;
1398 infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1399
1400 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1401
1402 return 0;
1403 }
1404
1405 static LRESULT
1406 DATETIME_StyleChanging(DATETIME_INFO *infoPtr, WPARAM wStyleType, STYLESTRUCT *lpss)
1407 {
1408 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1409 wStyleType, lpss->styleOld, lpss->styleNew);
1410
1411 /* block DTS_SHOWNONE change */
1412 if ((lpss->styleNew ^ lpss->styleOld) & DTS_SHOWNONE)
1413 {
1414 if (lpss->styleOld & DTS_SHOWNONE)
1415 lpss->styleNew |= DTS_SHOWNONE;
1416 else
1417 lpss->styleNew &= ~DTS_SHOWNONE;
1418 }
1419
1420 return 0;
1421 }
1422
1423 static LRESULT
1424 DATETIME_StyleChanged(DATETIME_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
1425 {
1426 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1427 wStyleType, lpss->styleOld, lpss->styleNew);
1428
1429 if (wStyleType != GWL_STYLE) return 0;
1430
1431 infoPtr->dwStyle = lpss->styleNew;
1432
1433 if ( !(lpss->styleOld & DTS_SHOWNONE) && (lpss->styleNew & DTS_SHOWNONE) ) {
1434 infoPtr->hwndCheckbut = CreateWindowExW (0, WC_BUTTONW, 0, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1435 2, 2, 13, 13, infoPtr->hwndSelf, 0,
1436 (HINSTANCE)GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_HINSTANCE), 0);
1437 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, infoPtr->dateValid ? 1 : 0, 0);
1438 }
1439 if ( (lpss->styleOld & DTS_SHOWNONE) && !(lpss->styleNew & DTS_SHOWNONE) ) {
1440 DestroyWindow(infoPtr->hwndCheckbut);
1441 infoPtr->hwndCheckbut = 0;
1442 }
1443 if ( !(lpss->styleOld & DTS_UPDOWN) && (lpss->styleNew & DTS_UPDOWN) ) {
1444 infoPtr->hUpdown = CreateUpDownControl (WS_CHILD | WS_BORDER | WS_VISIBLE, 120, 1, 20, 20,
1445 infoPtr->hwndSelf, 1, 0, 0, UD_MAXVAL, UD_MINVAL, 0);
1446 }
1447 if ( (lpss->styleOld & DTS_UPDOWN) && !(lpss->styleNew & DTS_UPDOWN) ) {
1448 DestroyWindow(infoPtr->hUpdown);
1449 infoPtr->hUpdown = 0;
1450 }
1451
1452 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1453 return 0;
1454 }
1455
1456
1457 static LRESULT
1458 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1459 {
1460 infoPtr->hFont = font;
1461 if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1462 return 0;
1463 }
1464
1465
1466 static LRESULT
1467 DATETIME_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1468 {
1469 DATETIME_INFO *infoPtr = Alloc (sizeof(DATETIME_INFO));
1470 STYLESTRUCT ss = { 0, lpcs->style };
1471
1472 if (!infoPtr) return -1;
1473
1474 infoPtr->hwndSelf = hwnd;
1475 infoPtr->dwStyle = lpcs->style;
1476
1477 infoPtr->nrFieldsAllocated = 32;
1478 infoPtr->fieldspec = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1479 infoPtr->fieldRect = Alloc (infoPtr->nrFieldsAllocated * sizeof(RECT));
1480 infoPtr->buflen = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1481 infoPtr->hwndNotify = lpcs->hwndParent;
1482 infoPtr->select = -1; /* initially, nothing is selected */
1483 infoPtr->bDropdownEnabled = TRUE;
1484
1485 DATETIME_StyleChanged(infoPtr, GWL_STYLE, &ss);
1486 DATETIME_SetFormatW (infoPtr, 0);
1487
1488 /* create the monthcal control */
1489 infoPtr->hMonthCal = CreateWindowExW (0, MONTHCAL_CLASSW, 0, WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS,
1490 0, 0, 0, 0, infoPtr->hwndSelf, 0, 0, 0);
1491
1492 /* initialize info structure */
1493 GetLocalTime (&infoPtr->date);
1494 infoPtr->dateValid = TRUE;
1495 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1496
1497 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1498
1499 return 0;
1500 }
1501
1502
1503
1504 static LRESULT
1505 DATETIME_Destroy (DATETIME_INFO *infoPtr)
1506 {
1507 if (infoPtr->hwndCheckbut)
1508 DestroyWindow(infoPtr->hwndCheckbut);
1509 if (infoPtr->hUpdown)
1510 DestroyWindow(infoPtr->hUpdown);
1511 if (infoPtr->hMonthCal)
1512 DestroyWindow(infoPtr->hMonthCal);
1513 SetWindowLongPtrW( infoPtr->hwndSelf, 0, 0 ); /* clear infoPtr */
1514 Free (infoPtr->buflen);
1515 Free (infoPtr->fieldRect);
1516 Free (infoPtr->fieldspec);
1517 Free (infoPtr);
1518 return 0;
1519 }
1520
1521
1522 static INT
1523 DATETIME_GetText (const DATETIME_INFO *infoPtr, INT count, LPWSTR dst)
1524 {
1525 WCHAR buf[80];
1526 int i;
1527
1528 if (!dst || (count <= 0)) return 0;
1529
1530 dst[0] = 0;
1531 for (i = 0; i < infoPtr->nrFields; i++)
1532 {
1533 DATETIME_ReturnTxt(infoPtr, i, buf, sizeof(buf)/sizeof(buf[0]));
1534 if ((strlenW(dst) + strlenW(buf)) < count)
1535 strcatW(dst, buf);
1536 else break;
1537 }
1538 return strlenW(dst);
1539 }
1540
1541
1542 static LRESULT WINAPI
1543 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1544 {
1545 DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1546 LRESULT ret;
1547
1548 TRACE ("%x, %lx, %lx\n", uMsg, wParam, lParam);
1549
1550 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
1551 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
1552
1553 switch (uMsg) {
1554
1555 case DTM_GETSYSTEMTIME:
1556 return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1557
1558 case DTM_SETSYSTEMTIME:
1559 return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1560
1561 case DTM_GETRANGE:
1562 ret = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1563 return ret ? ret : 1; /* bug emulation */
1564
1565 case DTM_SETRANGE:
1566 return SendMessageW (infoPtr->hMonthCal, MCM_SETRANGE, wParam, lParam);
1567
1568 case DTM_SETFORMATA:
1569 return DATETIME_SetFormatA (infoPtr, (LPCSTR)lParam);
1570
1571 case DTM_SETFORMATW:
1572 return DATETIME_SetFormatW (infoPtr, (LPCWSTR)lParam);
1573
1574 case DTM_GETMONTHCAL:
1575 return (LRESULT)infoPtr->hMonthCal;
1576
1577 case DTM_SETMCCOLOR:
1578 return SendMessageW (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
1579
1580 case DTM_GETMCCOLOR:
1581 return SendMessageW (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
1582
1583 case DTM_SETMCFONT:
1584 return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1585
1586 case DTM_GETMCFONT:
1587 return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1588
1589 case WM_NOTIFY:
1590 return DATETIME_Notify (infoPtr, (LPNMHDR)lParam);
1591
1592 case WM_ENABLE:
1593 return DATETIME_Enable (infoPtr, (BOOL)wParam);
1594
1595 case WM_ERASEBKGND:
1596 return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1597
1598 case WM_GETDLGCODE:
1599 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1600
1601 case WM_PRINTCLIENT:
1602 case WM_PAINT:
1603 return DATETIME_Paint (infoPtr, (HDC)wParam);
1604
1605 case WM_KEYDOWN:
1606 return DATETIME_KeyDown (infoPtr, wParam);
1607
1608 case WM_CHAR:
1609 return DATETIME_Char (infoPtr, wParam);
1610
1611 case WM_KILLFOCUS:
1612 return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1613
1614 case WM_NCCREATE:
1615 return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1616
1617 case WM_SETFOCUS:
1618 return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1619
1620 case WM_SIZE:
1621 return DATETIME_Size (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1622
1623 case WM_LBUTTONDOWN:
1624 return DATETIME_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1625
1626 case WM_LBUTTONUP:
1627 return DATETIME_LButtonUp (infoPtr);
1628
1629 case WM_VSCROLL:
1630 return DATETIME_VScroll (infoPtr, (WORD)wParam);
1631
1632 case WM_CREATE:
1633 return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1634
1635 case WM_DESTROY:
1636 return DATETIME_Destroy (infoPtr);
1637
1638 case WM_COMMAND:
1639 return DATETIME_Command (infoPtr, wParam, lParam);
1640
1641 case WM_STYLECHANGING:
1642 return DATETIME_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1643
1644 case WM_STYLECHANGED:
1645 return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1646
1647 case WM_SETFONT:
1648 return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1649
1650 case WM_GETFONT:
1651 return (LRESULT) infoPtr->hFont;
1652
1653 case WM_GETTEXT:
1654 return (LRESULT) DATETIME_GetText(infoPtr, wParam, (LPWSTR)lParam);
1655
1656 case WM_SETTEXT:
1657 return CB_ERR;
1658
1659 default:
1660 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
1661 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
1662 uMsg, wParam, lParam);
1663 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1664 }
1665 }
1666
1667
1668 void
1669 DATETIME_Register (void)
1670 {
1671 WNDCLASSW wndClass;
1672
1673 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1674 wndClass.style = CS_GLOBALCLASS;
1675 wndClass.lpfnWndProc = DATETIME_WindowProc;
1676 wndClass.cbClsExtra = 0;
1677 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1678 wndClass.hCursor = LoadCursorW (0, (LPCWSTR)IDC_ARROW);
1679 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1680 wndClass.lpszClassName = DATETIMEPICK_CLASSW;
1681
1682 RegisterClassW (&wndClass);
1683 }
1684
1685
1686 void
1687 DATETIME_Unregister (void)
1688 {
1689 UnregisterClassW (DATETIMEPICK_CLASSW, NULL);
1690 }