regedit/tests: Use verify_key() for consistency with the verify_reg*() functions.
[wine.git] / dlls / comctl32 / datetime.c
blob80b800ee89a5f1c09acbce768e08939bfac8cf21
1 /*
2 * Date and time picker control
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
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.
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.
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
23 * NOTE
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.
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.
32 * TODO:
33 * -- DTS_APPCANPARSE
34 * -- DTS_SHORTDATECENTURYFORMAT
35 * -- DTN_FORMAT
36 * -- DTN_FORMATQUERY
37 * -- DTN_USERSTRING
38 * -- DTN_WMKEYDOWN
39 * -- FORMATCALLBACK
42 #include <math.h>
43 #include <string.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <limits.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "commctrl.h"
54 #include "comctl32.h"
55 #include "wine/debug.h"
56 #include "wine/unicode.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(datetime);
60 typedef struct
62 HWND hwndSelf;
63 HWND hMonthCal;
64 HWND hwndNotify;
65 HWND hUpdown;
66 DWORD dwStyle;
67 SYSTEMTIME date;
68 BOOL dateValid;
69 HWND hwndCheckbut;
70 RECT rcClient; /* rect around the edge of the window */
71 RECT rcDraw; /* rect inside of the border */
72 RECT checkbox; /* checkbox allowing the control to be enabled/disabled */
73 RECT calbutton; /* button that toggles the dropdown of the monthcal control */
74 BOOL bCalDepressed; /* TRUE = cal button is depressed */
75 BOOL bDropdownEnabled;
76 int select;
77 WCHAR charsEntered[4];
78 int nCharsEntered;
79 HFONT hFont;
80 int nrFieldsAllocated;
81 int nrFields;
82 int haveFocus;
83 int *fieldspec;
84 RECT *fieldRect;
85 int *buflen;
86 WCHAR textbuf[256];
87 POINT monthcal_pos;
88 int pendingUpdown;
89 } DATETIME_INFO, *LPDATETIME_INFO;
91 /* in monthcal.c */
92 extern int MONTHCAL_MonthLength(int month, int year);
93 extern int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace);
95 /* this list of defines is closely related to `allowedformatchars' defined
96 * in datetime.c; the high nibble indicates the `base type' of the format
97 * specifier.
98 * Do not change without first reading DATETIME_UseFormat.
102 #define DT_END_FORMAT 0
103 #define ONEDIGITDAY 0x01
104 #define TWODIGITDAY 0x02
105 #define THREECHARDAY 0x03
106 #define FULLDAY 0x04
107 #define ONEDIGIT12HOUR 0x11
108 #define TWODIGIT12HOUR 0x12
109 #define ONEDIGIT24HOUR 0x21
110 #define TWODIGIT24HOUR 0x22
111 #define ONEDIGITMINUTE 0x31
112 #define TWODIGITMINUTE 0x32
113 #define ONEDIGITMONTH 0x41
114 #define TWODIGITMONTH 0x42
115 #define THREECHARMONTH 0x43
116 #define FULLMONTH 0x44
117 #define ONEDIGITSECOND 0x51
118 #define TWODIGITSECOND 0x52
119 #define ONELETTERAMPM 0x61
120 #define TWOLETTERAMPM 0x62
121 #define ONEDIGITYEAR 0x71
122 #define TWODIGITYEAR 0x72
123 #define INVALIDFULLYEAR 0x73 /* FIXME - yyy is not valid - we'll treat it as yyyy */
124 #define FULLYEAR 0x74
125 #define FORMATCALLBACK 0x81 /* -> maximum of 0x80 callbacks possible */
126 #define FORMATCALLMASK 0x80
127 #define DT_STRING 0x0100
129 #define DTHT_DATEFIELD 0xff /* for hit-testing */
131 #define DTHT_NONE 0x1000
132 #define DTHT_CHECKBOX 0x2000 /* these should end at '00' , to make */
133 #define DTHT_MCPOPUP 0x3000 /* & DTHT_DATEFIELD 0 when DATETIME_KeyDown */
134 #define DTHT_GOTFOCUS 0x4000 /* tests for date-fields */
135 #define DTHT_NODATEMASK 0xf000 /* to mask check and drop down from others */
137 static BOOL DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code);
138 static BOOL DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr);
139 static const WCHAR allowedformatchars[] = {'d', 'h', 'H', 'm', 'M', 's', 't', 'y', 'X', 0};
140 static const int maxrepetition [] = {4,2,2,2,4,2,2,4,-1};
142 /* valid date limits */
143 static const SYSTEMTIME max_allowed_date = { /* wYear */ 9999, /* wMonth */ 12, /* wDayOfWeek */ 0, /* wDay */ 31 };
144 static const SYSTEMTIME min_allowed_date = { /* wYear */ 1752, /* wMonth */ 9, /* wDayOfWeek */ 0, /* wDay */ 14 };
146 static DWORD
147 DATETIME_GetSystemTime (const DATETIME_INFO *infoPtr, SYSTEMTIME *systime)
149 if (!systime) return GDT_NONE;
151 if ((infoPtr->dwStyle & DTS_SHOWNONE) &&
152 (SendMessageW (infoPtr->hwndCheckbut, BM_GETCHECK, 0, 0) == BST_UNCHECKED))
153 return GDT_NONE;
155 *systime = infoPtr->date;
157 return GDT_VALID;
160 /* Checks value is within configured date range
162 * PARAMETERS
164 * [I] infoPtr : valid pointer to control data
165 * [I] date : pointer to valid date data to check
167 * RETURN VALUE
169 * TRUE - date within configured range
170 * FALSE - date is outside configured range
172 static BOOL DATETIME_IsDateInValidRange(const DATETIME_INFO *infoPtr, const SYSTEMTIME *date)
174 SYSTEMTIME range[2];
175 DWORD limits;
177 if ((MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) ||
178 (MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1))
179 return FALSE;
181 limits = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, 0, (LPARAM)range);
183 if (limits & GDTR_MAX)
185 if (MONTHCAL_CompareSystemTime(date, &range[1]) == 1)
186 return FALSE;
189 if (limits & GDTR_MIN)
191 if (MONTHCAL_CompareSystemTime(date, &range[0]) == -1)
192 return FALSE;
195 return TRUE;
198 static BOOL
199 DATETIME_SetSystemTime (DATETIME_INFO *infoPtr, DWORD flag, const SYSTEMTIME *systime)
201 if (!systime) return FALSE;
203 TRACE("%04d/%02d/%02d %02d:%02d:%02d\n",
204 systime->wYear, systime->wMonth, systime->wDay,
205 systime->wHour, systime->wMinute, systime->wSecond);
207 if (flag == GDT_VALID) {
208 if (systime->wYear == 0 ||
209 systime->wMonth < 1 || systime->wMonth > 12 ||
210 systime->wDay < 1 ||
211 systime->wDay > MONTHCAL_MonthLength(systime->wMonth, systime->wYear) ||
212 systime->wHour > 23 ||
213 systime->wMinute > 59 ||
214 systime->wSecond > 59 ||
215 systime->wMilliseconds > 999
217 return FALSE;
219 /* Windows returns true if the date is valid but outside the limits set */
220 if (!DATETIME_IsDateInValidRange(infoPtr, systime))
221 return TRUE;
223 infoPtr->dateValid = TRUE;
224 infoPtr->date = *systime;
225 /* always store a valid day of week */
226 MONTHCAL_CalculateDayOfWeek(&infoPtr->date, TRUE);
228 SendMessageW (infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
229 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
230 } else if ((infoPtr->dwStyle & DTS_SHOWNONE) && (flag == GDT_NONE)) {
231 infoPtr->dateValid = FALSE;
232 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_UNCHECKED, 0);
234 else
235 return FALSE;
237 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
238 return TRUE;
242 /***
243 * Split up a formattxt in actions.
244 * See ms documentation for the meaning of the letter codes/'specifiers'.
246 * Notes:
247 * *'dddddd' is handled as 'dddd' plus 'dd'.
248 * *unrecognized formats are strings (here given the type DT_STRING;
249 * start of the string is encoded in lower bits of DT_STRING.
250 * Therefore, 'string' ends finally up as '<show seconds>tring'.
253 static void
254 DATETIME_UseFormat (DATETIME_INFO *infoPtr, LPCWSTR formattxt)
256 unsigned int i;
257 int j, k, len;
258 BOOL inside_literal = FALSE; /* inside '...' */
259 int *nrFields = &infoPtr->nrFields;
261 *nrFields = 0;
262 infoPtr->fieldspec[*nrFields] = 0;
263 len = strlenW(allowedformatchars);
264 k = 0;
266 for (i = 0; formattxt[i]; i++) {
267 TRACE ("\n%d %c:", i, formattxt[i]);
268 if (!inside_literal) {
269 for (j = 0; j < len; j++) {
270 if (allowedformatchars[j]==formattxt[i]) {
271 TRACE ("%c[%d,%x]", allowedformatchars[j], *nrFields, infoPtr->fieldspec[*nrFields]);
272 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
273 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
274 break;
276 if (infoPtr->fieldspec[*nrFields] >> 4 != j) {
277 (*nrFields)++;
278 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
279 break;
281 if ((infoPtr->fieldspec[*nrFields] & 0x0f) == maxrepetition[j]) {
282 (*nrFields)++;
283 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
284 break;
286 infoPtr->fieldspec[*nrFields]++;
287 break;
288 } /* if allowedformatchar */
289 } /* for j */
291 else
292 j = len;
294 if (formattxt[i] == '\'')
296 inside_literal = !inside_literal;
297 continue;
300 /* char is not a specifier: handle char like a string */
301 if (j == len) {
302 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
303 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
304 infoPtr->buflen[*nrFields] = 0;
305 } else if ((infoPtr->fieldspec[*nrFields] & DT_STRING) != DT_STRING) {
306 (*nrFields)++;
307 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
308 infoPtr->buflen[*nrFields] = 0;
310 infoPtr->textbuf[k] = formattxt[i];
311 k++;
312 infoPtr->buflen[*nrFields]++;
313 } /* if j=len */
315 if (*nrFields == infoPtr->nrFieldsAllocated) {
316 FIXME ("out of memory; should reallocate. crash ahead.\n");
318 } /* for i */
320 TRACE("\n");
322 if (infoPtr->fieldspec[*nrFields] != 0) (*nrFields)++;
326 static BOOL
327 DATETIME_SetFormatW (DATETIME_INFO *infoPtr, LPCWSTR format)
329 WCHAR format_buf[80];
331 if (!format) {
332 DWORD format_item;
334 if (infoPtr->dwStyle & DTS_LONGDATEFORMAT)
335 format_item = LOCALE_SLONGDATE;
336 else if ((infoPtr->dwStyle & DTS_TIMEFORMAT) == DTS_TIMEFORMAT)
337 format_item = LOCALE_STIMEFORMAT;
338 else /* DTS_SHORTDATEFORMAT */
339 format_item = LOCALE_SSHORTDATE;
340 GetLocaleInfoW(LOCALE_USER_DEFAULT, format_item, format_buf, sizeof(format_buf)/sizeof(format_buf[0]));
341 format = format_buf;
344 DATETIME_UseFormat (infoPtr, format);
345 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
347 return TRUE;
351 static BOOL
352 DATETIME_SetFormatA (DATETIME_INFO *infoPtr, LPCSTR lpszFormat)
354 if (lpszFormat) {
355 BOOL retval;
356 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, NULL, 0);
357 LPWSTR wstr = Alloc(len * sizeof(WCHAR));
358 if (wstr) MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, wstr, len);
359 retval = DATETIME_SetFormatW (infoPtr, wstr);
360 Free (wstr);
361 return retval;
363 else
364 return DATETIME_SetFormatW (infoPtr, 0);
369 static void
370 DATETIME_ReturnTxt (const DATETIME_INFO *infoPtr, int count, LPWSTR result, int resultSize)
372 static const WCHAR fmt_dW[] = { '%', 'd', 0 };
373 static const WCHAR fmt__2dW[] = { '%', '.', '2', 'd', 0 };
374 static const WCHAR fmt__3sW[] = { '%', '.', '3', 's', 0 };
375 SYSTEMTIME date = infoPtr->date;
376 int spec;
377 WCHAR buffer[80];
379 *result=0;
380 TRACE ("%d,%d\n", infoPtr->nrFields, count);
381 if (count>infoPtr->nrFields || count < 0) {
382 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
383 return;
386 if (!infoPtr->fieldspec) return;
388 spec = infoPtr->fieldspec[count];
389 if (spec & DT_STRING) {
390 int txtlen = infoPtr->buflen[count];
392 if (txtlen > resultSize)
393 txtlen = resultSize - 1;
394 memcpy (result, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
395 result[txtlen] = 0;
396 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
397 return;
401 switch (spec) {
402 case DT_END_FORMAT:
403 *result = 0;
404 break;
405 case ONEDIGITDAY:
406 wsprintfW (result, fmt_dW, date.wDay);
407 break;
408 case TWODIGITDAY:
409 wsprintfW (result, fmt__2dW, date.wDay);
410 break;
411 case THREECHARDAY:
412 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1+(date.wDayOfWeek+6)%7, result, 4);
413 /*wsprintfW (result,"%.3s",days[date.wDayOfWeek]);*/
414 break;
415 case FULLDAY:
416 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME1+(date.wDayOfWeek+6)%7, result, resultSize);
417 break;
418 case ONEDIGIT12HOUR:
419 if (date.wHour == 0) {
420 result[0] = '1';
421 result[1] = '2';
422 result[2] = 0;
424 else
425 wsprintfW (result, fmt_dW, date.wHour - (date.wHour > 12 ? 12 : 0));
426 break;
427 case TWODIGIT12HOUR:
428 if (date.wHour == 0) {
429 result[0] = '1';
430 result[1] = '2';
431 result[2] = 0;
433 else
434 wsprintfW (result, fmt__2dW, date.wHour - (date.wHour > 12 ? 12 : 0));
435 break;
436 case ONEDIGIT24HOUR:
437 wsprintfW (result, fmt_dW, date.wHour);
438 break;
439 case TWODIGIT24HOUR:
440 wsprintfW (result, fmt__2dW, date.wHour);
441 break;
442 case ONEDIGITSECOND:
443 wsprintfW (result, fmt_dW, date.wSecond);
444 break;
445 case TWODIGITSECOND:
446 wsprintfW (result, fmt__2dW, date.wSecond);
447 break;
448 case ONEDIGITMINUTE:
449 wsprintfW (result, fmt_dW, date.wMinute);
450 break;
451 case TWODIGITMINUTE:
452 wsprintfW (result, fmt__2dW, date.wMinute);
453 break;
454 case ONEDIGITMONTH:
455 wsprintfW (result, fmt_dW, date.wMonth);
456 break;
457 case TWODIGITMONTH:
458 wsprintfW (result, fmt__2dW, date.wMonth);
459 break;
460 case THREECHARMONTH:
461 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+date.wMonth -1,
462 buffer, sizeof(buffer)/sizeof(buffer[0]));
463 wsprintfW (result, fmt__3sW, buffer);
464 break;
465 case FULLMONTH:
466 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+date.wMonth -1,
467 result, resultSize);
468 break;
469 case ONELETTERAMPM:
470 result[0] = (date.wHour < 12 ? 'A' : 'P');
471 result[1] = 0;
472 break;
473 case TWOLETTERAMPM:
474 result[0] = (date.wHour < 12 ? 'A' : 'P');
475 result[1] = 'M';
476 result[2] = 0;
477 break;
478 case FORMATCALLBACK:
479 FIXME ("Not implemented\n");
480 result[0] = 'x';
481 result[1] = 0;
482 break;
483 case ONEDIGITYEAR:
484 wsprintfW (result, fmt_dW, date.wYear-10* (int) floor(date.wYear/10));
485 break;
486 case TWODIGITYEAR:
487 wsprintfW (result, fmt__2dW, date.wYear-100* (int) floor(date.wYear/100));
488 break;
489 case INVALIDFULLYEAR:
490 case FULLYEAR:
491 wsprintfW (result, fmt_dW, date.wYear);
492 break;
495 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
498 static int wrap(int val, int delta, int minVal, int maxVal)
500 val += delta;
501 if (delta == INT_MIN || val < minVal) return maxVal;
502 if (delta == INT_MAX || val > maxVal) return minVal;
503 return val;
506 static void
507 DATETIME_IncreaseField (DATETIME_INFO *infoPtr, int number, int delta)
509 SYSTEMTIME *date = &infoPtr->date;
510 SYSTEMTIME range[2];
511 DWORD limits;
512 BOOL min;
514 TRACE ("%d\n", number);
515 if ((number > infoPtr->nrFields) || (number < 0)) return;
517 if ((infoPtr->fieldspec[number] & DTHT_DATEFIELD) == 0) return;
519 switch (infoPtr->fieldspec[number]) {
520 case ONEDIGITYEAR:
521 case TWODIGITYEAR:
522 case FULLYEAR:
523 if (delta == INT_MIN)
524 date->wYear = 1752;
525 else if (delta == INT_MAX)
526 date->wYear = 9999;
527 else
528 date->wYear = max(min(date->wYear + delta, 9999), 1752);
530 if (date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
531 /* This can happen when moving away from a leap year. */
532 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear);
533 MONTHCAL_CalculateDayOfWeek(date, TRUE);
534 break;
535 case ONEDIGITMONTH:
536 case TWODIGITMONTH:
537 case THREECHARMONTH:
538 case FULLMONTH:
539 date->wMonth = wrap(date->wMonth, delta, 1, 12);
540 MONTHCAL_CalculateDayOfWeek(date, TRUE);
541 delta = 0;
542 /* fall through */
543 case ONEDIGITDAY:
544 case TWODIGITDAY:
545 case THREECHARDAY:
546 case FULLDAY:
547 date->wDay = wrap(date->wDay, delta, 1, MONTHCAL_MonthLength(date->wMonth, date->wYear));
548 MONTHCAL_CalculateDayOfWeek(date, TRUE);
549 break;
550 case ONELETTERAMPM:
551 case TWOLETTERAMPM:
552 delta *= 12;
553 /* fall through */
554 case ONEDIGIT12HOUR:
555 case TWODIGIT12HOUR:
556 case ONEDIGIT24HOUR:
557 case TWODIGIT24HOUR:
558 date->wHour = wrap(date->wHour, delta, 0, 23);
559 break;
560 case ONEDIGITMINUTE:
561 case TWODIGITMINUTE:
562 date->wMinute = wrap(date->wMinute, delta, 0, 59);
563 break;
564 case ONEDIGITSECOND:
565 case TWODIGITSECOND:
566 date->wSecond = wrap(date->wSecond, delta, 0, 59);
567 break;
568 case FORMATCALLBACK:
569 FIXME ("Not implemented\n");
570 break;
573 /* FYI: On 1752/9/14 the calendar changed and England and the
574 * American colonies changed to the Gregorian calendar. This change
575 * involved having September 14th follow September 2nd. So no date
576 * algorithm works before that date.
578 if (10000 * date->wYear + 100 * date->wMonth + date->wDay < 17520914) {
579 date->wYear = 1752;
580 date->wMonth = 9;
581 date->wDay = 14;
582 date->wSecond = 0;
583 date->wMinute = 0;
584 date->wHour = 0;
587 /* Ensure time is within bounds */
588 limits = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, 0, (LPARAM)range);
589 min = delta < 0;
591 if (limits & (min ? GDTR_MIN : GDTR_MAX))
593 int i = (min ? 0 : 1);
595 if (MONTHCAL_CompareSystemTime(date, &range[i]) == (min ? -1 : 1))
597 date->wYear = range[i].wYear;
598 date->wMonth = range[i].wMonth;
599 date->wDayOfWeek = range[i].wDayOfWeek;
600 date->wDay = range[i].wDay;
601 date->wHour = range[i].wHour;
602 date->wMinute = range[i].wMinute;
603 date->wSecond = range[i].wSecond;
604 date->wMilliseconds = range[i].wMilliseconds;
609 static void
610 DATETIME_ReturnFieldWidth (const DATETIME_INFO *infoPtr, HDC hdc, int count, SHORT *width)
612 /* fields are a fixed width, determined by the largest possible string */
613 /* presumably, these widths should be language dependent */
614 static const WCHAR fld_d1W[] = { '2', 0 };
615 static const WCHAR fld_d2W[] = { '2', '2', 0 };
616 static const WCHAR fld_d4W[] = { '2', '2', '2', '2', 0 };
617 static const WCHAR fld_am1[] = { 'A', 0 };
618 static const WCHAR fld_am2[] = { 'A', 'M', 0 };
619 int spec;
620 WCHAR buffer[80];
621 LPCWSTR bufptr;
622 SIZE size;
624 TRACE ("%d,%d\n", infoPtr->nrFields, count);
625 if (count>infoPtr->nrFields || count < 0) {
626 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
627 return;
630 if (!infoPtr->fieldspec) return;
632 spec = infoPtr->fieldspec[count];
633 if (spec & DT_STRING) {
634 int txtlen = infoPtr->buflen[count];
636 if (txtlen > 79)
637 txtlen = 79;
638 memcpy (buffer, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
639 buffer[txtlen] = 0;
640 bufptr = buffer;
642 else {
643 switch (spec) {
644 case ONEDIGITDAY:
645 case ONEDIGIT12HOUR:
646 case ONEDIGIT24HOUR:
647 case ONEDIGITSECOND:
648 case ONEDIGITMINUTE:
649 case ONEDIGITMONTH:
650 case ONEDIGITYEAR:
651 /* these seem to use a two byte field */
652 case TWODIGITDAY:
653 case TWODIGIT12HOUR:
654 case TWODIGIT24HOUR:
655 case TWODIGITSECOND:
656 case TWODIGITMINUTE:
657 case TWODIGITMONTH:
658 case TWODIGITYEAR:
659 bufptr = fld_d2W;
660 break;
661 case INVALIDFULLYEAR:
662 case FULLYEAR:
663 bufptr = fld_d4W;
664 break;
665 case THREECHARMONTH:
666 case FULLMONTH:
667 case THREECHARDAY:
668 case FULLDAY:
670 static const WCHAR fld_day[] = {'W','e','d','n','e','s','d','a','y',0};
671 static const WCHAR fld_abbrday[] = {'W','e','d',0};
672 static const WCHAR fld_mon[] = {'S','e','p','t','e','m','b','e','r',0};
673 static const WCHAR fld_abbrmon[] = {'D','e','c',0};
675 const WCHAR *fall;
676 LCTYPE lctype;
677 INT i, max_count;
678 LONG cx;
680 /* choose locale data type and fallback string */
681 switch (spec) {
682 case THREECHARDAY:
683 fall = fld_abbrday;
684 lctype = LOCALE_SABBREVDAYNAME1;
685 max_count = 7;
686 break;
687 case FULLDAY:
688 fall = fld_day;
689 lctype = LOCALE_SDAYNAME1;
690 max_count = 7;
691 break;
692 case THREECHARMONTH:
693 fall = fld_abbrmon;
694 lctype = LOCALE_SABBREVMONTHNAME1;
695 max_count = 12;
696 break;
697 default: /* FULLMONTH */
698 fall = fld_mon;
699 lctype = LOCALE_SMONTHNAME1;
700 max_count = 12;
701 break;
704 cx = 0;
705 for (i = 0; i < max_count; i++)
707 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, lctype + i,
708 buffer, lstrlenW(buffer)))
710 GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &size);
711 if (size.cx > cx) cx = size.cx;
713 else /* locale independent fallback on failure */
715 GetTextExtentPoint32W(hdc, fall, lstrlenW(fall), &size);
716 cx = size.cx;
717 break;
720 *width = cx;
721 return;
723 case ONELETTERAMPM:
724 bufptr = fld_am1;
725 break;
726 case TWOLETTERAMPM:
727 bufptr = fld_am2;
728 break;
729 default:
730 bufptr = fld_d1W;
731 break;
734 GetTextExtentPoint32W (hdc, bufptr, strlenW(bufptr), &size);
735 *width = size.cx;
738 static void
739 DATETIME_Refresh (DATETIME_INFO *infoPtr, HDC hdc)
741 TRACE("\n");
743 if (infoPtr->dateValid) {
744 int i, prevright;
745 RECT *field;
746 RECT *rcDraw = &infoPtr->rcDraw;
747 SIZE size;
748 COLORREF oldTextColor;
749 SHORT fieldWidth = 0;
750 HFONT oldFont = SelectObject (hdc, infoPtr->hFont);
751 INT oldBkMode = SetBkMode (hdc, TRANSPARENT);
752 WCHAR txt[80];
754 DATETIME_ReturnTxt (infoPtr, 0, txt, sizeof(txt)/sizeof(txt[0]));
755 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
756 rcDraw->bottom = size.cy + 2;
758 prevright = infoPtr->checkbox.right = ((infoPtr->dwStyle & DTS_SHOWNONE) ? 18 : 2);
760 for (i = 0; i < infoPtr->nrFields; i++) {
761 DATETIME_ReturnTxt (infoPtr, i, txt, sizeof(txt)/sizeof(txt[0]));
762 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
763 DATETIME_ReturnFieldWidth (infoPtr, hdc, i, &fieldWidth);
764 field = &infoPtr->fieldRect[i];
765 field->left = prevright;
766 field->right = prevright + fieldWidth;
767 field->top = rcDraw->top;
768 field->bottom = rcDraw->bottom;
769 prevright = field->right;
771 if (infoPtr->dwStyle & WS_DISABLED)
772 oldTextColor = SetTextColor (hdc, comctl32_color.clrGrayText);
773 else if ((infoPtr->haveFocus) && (i == infoPtr->select)) {
774 RECT selection;
776 /* fill if focused */
777 HBRUSH hbr = CreateSolidBrush (comctl32_color.clrActiveCaption);
779 if (infoPtr->nCharsEntered)
781 memcpy(txt, infoPtr->charsEntered, infoPtr->nCharsEntered * sizeof(WCHAR));
782 txt[infoPtr->nCharsEntered] = 0;
783 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
786 SetRect(&selection, 0, 0, size.cx, size.cy);
787 /* center rectangle */
788 OffsetRect(&selection, (field->right + field->left - size.cx)/2,
789 (field->bottom - size.cy)/2);
791 FillRect(hdc, &selection, hbr);
792 DeleteObject (hbr);
793 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindow);
795 else
796 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindowText);
798 /* draw the date text using the colour set above */
799 DrawTextW (hdc, txt, strlenW(txt), field, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
800 SetTextColor (hdc, oldTextColor);
802 SetBkMode (hdc, oldBkMode);
803 SelectObject (hdc, oldFont);
806 if (!(infoPtr->dwStyle & DTS_UPDOWN)) {
807 DrawFrameControl(hdc, &infoPtr->calbutton, DFC_SCROLL,
808 DFCS_SCROLLDOWN | (infoPtr->bCalDepressed ? DFCS_PUSHED : 0) |
809 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
814 static INT
815 DATETIME_HitTest (const DATETIME_INFO *infoPtr, POINT pt)
817 int i;
819 TRACE ("%s\n", wine_dbgstr_point(&pt));
821 if (PtInRect (&infoPtr->calbutton, pt)) return DTHT_MCPOPUP;
822 if (PtInRect (&infoPtr->checkbox, pt)) return DTHT_CHECKBOX;
824 for (i = 0; i < infoPtr->nrFields; i++) {
825 if (PtInRect (&infoPtr->fieldRect[i], pt)) return i;
828 return DTHT_NONE;
831 /* Returns index of the nearest preceding date field from given,
832 or -1 if none was found */
833 static int DATETIME_GetPrevDateField(const DATETIME_INFO *infoPtr, int i)
835 for(--i; i >= 0; i--)
837 if (infoPtr->fieldspec[i] & DTHT_DATEFIELD) return i;
839 return -1;
842 static void
843 DATETIME_ApplySelectedField (DATETIME_INFO *infoPtr)
845 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
846 int i, val = 0;
847 BOOL clamp_day = FALSE;
848 SYSTEMTIME date = infoPtr->date;
849 int oldyear;
851 if (infoPtr->select == -1 || infoPtr->nCharsEntered == 0)
852 return;
854 if ((infoPtr->fieldspec[fieldNum] == ONELETTERAMPM) ||
855 (infoPtr->fieldspec[fieldNum] == TWOLETTERAMPM))
856 val = infoPtr->charsEntered[0];
857 else {
858 for (i=0; i<infoPtr->nCharsEntered; i++)
859 val = val * 10 + infoPtr->charsEntered[i] - '0';
862 infoPtr->nCharsEntered = 0;
864 switch (infoPtr->fieldspec[fieldNum]) {
865 case ONEDIGITYEAR:
866 case TWODIGITYEAR:
867 oldyear = date.wYear;
868 date.wYear = date.wYear - (date.wYear%100) + val;
870 if (DATETIME_IsDateInValidRange(infoPtr, &date))
871 clamp_day = TRUE;
872 else
873 date.wYear = oldyear;
875 break;
876 case INVALIDFULLYEAR:
877 case FULLYEAR:
878 oldyear = date.wYear;
879 date.wYear = val;
881 if (DATETIME_IsDateInValidRange(infoPtr, &date))
882 clamp_day = TRUE;
883 else
884 date.wYear = oldyear;
886 break;
887 case ONEDIGITMONTH:
888 case TWODIGITMONTH:
889 date.wMonth = val;
890 clamp_day = TRUE;
891 break;
892 case ONEDIGITDAY:
893 case TWODIGITDAY:
894 date.wDay = val;
895 break;
896 case ONEDIGIT12HOUR:
897 case TWODIGIT12HOUR:
898 if (val >= 24)
899 val -= 20;
901 if (val >= 13)
902 date.wHour = val;
903 else if (val != 0) {
904 if (date.wHour >= 12) /* preserve current AM/PM state */
905 date.wHour = (val == 12 ? 12 : val + 12);
906 else
907 date.wHour = (val == 12 ? 0 : val);
909 break;
910 case ONEDIGIT24HOUR:
911 case TWODIGIT24HOUR:
912 date.wHour = val;
913 break;
914 case ONEDIGITMINUTE:
915 case TWODIGITMINUTE:
916 date.wMinute = val;
917 break;
918 case ONEDIGITSECOND:
919 case TWODIGITSECOND:
920 date.wSecond = val;
921 break;
922 case ONELETTERAMPM:
923 case TWOLETTERAMPM:
924 if (val == 'a' || val == 'A') {
925 if (date.wHour >= 12)
926 date.wHour -= 12;
927 } else if (val == 'p' || val == 'P') {
928 if (date.wHour < 12)
929 date.wHour += 12;
931 break;
934 if (clamp_day && date.wDay > MONTHCAL_MonthLength(date.wMonth, date.wYear))
935 date.wDay = MONTHCAL_MonthLength(date.wMonth, date.wYear);
937 if (DATETIME_SetSystemTime(infoPtr, GDT_VALID, &date))
938 DATETIME_SendDateTimeChangeNotify (infoPtr);
941 static void
942 DATETIME_SetSelectedField (DATETIME_INFO *infoPtr, int select)
944 DATETIME_ApplySelectedField(infoPtr);
946 infoPtr->select = select;
947 infoPtr->nCharsEntered = 0;
950 static LRESULT
951 DATETIME_LButtonDown (DATETIME_INFO *infoPtr, INT x, INT y)
953 POINT pt;
954 int new;
956 pt.x = x;
957 pt.y = y;
958 new = DATETIME_HitTest (infoPtr, pt);
960 SetFocus(infoPtr->hwndSelf);
962 if (!(new & DTHT_NODATEMASK) || (new == DTHT_NONE))
964 if (new == DTHT_NONE)
965 new = infoPtr->nrFields - 1;
966 else
968 /* hitting string part moves selection to next date field to left */
969 if (infoPtr->fieldspec[new] & DT_STRING)
971 new = DATETIME_GetPrevDateField(infoPtr, new);
972 if (new == -1) return 0;
974 /* never select full day of week */
975 if (infoPtr->fieldspec[new] == FULLDAY) return 0;
979 DATETIME_SetSelectedField(infoPtr, new);
981 if (infoPtr->select == DTHT_MCPOPUP) {
982 RECT rcMonthCal;
983 POINT pos;
984 SendMessageW(infoPtr->hMonthCal, MCM_GETMINREQRECT, 0, (LPARAM)&rcMonthCal);
986 /* FIXME: button actually is only depressed during dropdown of the */
987 /* calendar control and when the mouse is over the button window */
988 infoPtr->bCalDepressed = TRUE;
990 /* recalculate the position of the monthcal popup */
991 if(infoPtr->dwStyle & DTS_RIGHTALIGN)
992 pos.x = infoPtr->calbutton.left - (rcMonthCal.right - rcMonthCal.left);
993 else
994 /* FIXME: this should be after the area reserved for the checkbox */
995 pos.x = infoPtr->rcDraw.left;
997 pos.y = infoPtr->rcClient.bottom;
998 OffsetRect( &rcMonthCal, pos.x, pos.y );
999 MapWindowPoints( infoPtr->hwndSelf, 0, (POINT *)&rcMonthCal, 2 );
1000 SetWindowPos(infoPtr->hMonthCal, 0, rcMonthCal.left, rcMonthCal.top,
1001 rcMonthCal.right - rcMonthCal.left, rcMonthCal.bottom - rcMonthCal.top, 0);
1003 if(IsWindowVisible(infoPtr->hMonthCal)) {
1004 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1005 infoPtr->bDropdownEnabled = FALSE;
1006 DATETIME_SendSimpleNotify (infoPtr, DTN_CLOSEUP);
1007 } else {
1008 const SYSTEMTIME *lprgSysTimeArray = &infoPtr->date;
1009 TRACE("update calendar %04d/%02d/%02d\n",
1010 lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay);
1011 SendMessageW(infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
1013 if (infoPtr->bDropdownEnabled) {
1014 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
1015 DATETIME_SendSimpleNotify (infoPtr, DTN_DROPDOWN);
1017 infoPtr->bDropdownEnabled = TRUE;
1020 TRACE ("dt:%p mc:%p mc parent:%p, desktop:%p\n",
1021 infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hwndNotify, GetDesktopWindow ());
1024 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1026 return 0;
1030 static LRESULT
1031 DATETIME_LButtonUp (DATETIME_INFO *infoPtr)
1033 if(infoPtr->bCalDepressed) {
1034 infoPtr->bCalDepressed = FALSE;
1035 InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
1038 return 0;
1042 static LRESULT
1043 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
1045 if (!hdc) {
1046 PAINTSTRUCT ps;
1047 hdc = BeginPaint (infoPtr->hwndSelf, &ps);
1048 DATETIME_Refresh (infoPtr, hdc);
1049 EndPaint (infoPtr->hwndSelf, &ps);
1050 } else {
1051 DATETIME_Refresh (infoPtr, hdc);
1054 /* Not a click on the dropdown box, enabled it */
1055 infoPtr->bDropdownEnabled = TRUE;
1057 return 0;
1061 static LRESULT
1062 DATETIME_Button_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1064 if( HIWORD(wParam) == BN_CLICKED) {
1065 DWORD state = SendMessageW((HWND)lParam, BM_GETCHECK, 0, 0);
1066 infoPtr->dateValid = (state == BST_CHECKED);
1067 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1069 return 0;
1074 static LRESULT
1075 DATETIME_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1077 TRACE("hwndbutton = %p\n", infoPtr->hwndCheckbut);
1078 if(infoPtr->hwndCheckbut == (HWND)lParam)
1079 return DATETIME_Button_Command(infoPtr, wParam, lParam);
1080 return 0;
1084 static LRESULT
1085 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
1087 TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
1088 if (bEnable)
1089 infoPtr->dwStyle &= ~WS_DISABLED;
1090 else
1091 infoPtr->dwStyle |= WS_DISABLED;
1093 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1095 return 0;
1099 static LRESULT
1100 DATETIME_EraseBackground (const DATETIME_INFO *infoPtr, HDC hdc)
1102 HBRUSH hBrush, hSolidBrush = NULL;
1103 RECT rc;
1105 if (infoPtr->dwStyle & WS_DISABLED)
1106 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
1107 else
1109 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
1110 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
1111 if (!hBrush)
1112 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
1115 GetClientRect (infoPtr->hwndSelf, &rc);
1117 FillRect (hdc, &rc, hBrush);
1119 if (hSolidBrush)
1120 DeleteObject(hSolidBrush);
1122 return -1;
1126 static LRESULT
1127 DATETIME_Notify (DATETIME_INFO *infoPtr, const NMHDR *lpnmh)
1129 TRACE ("Got notification %x from %p\n", lpnmh->code, lpnmh->hwndFrom);
1130 TRACE ("info: %p %p %p\n", infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hUpdown);
1132 if (lpnmh->code == MCN_SELECT) {
1133 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1134 infoPtr->dateValid = TRUE;
1135 SendMessageW (infoPtr->hMonthCal, MCM_GETCURSEL, 0, (LPARAM)&infoPtr->date);
1136 TRACE("got from calendar %04d/%02d/%02d day of week %d\n",
1137 infoPtr->date.wYear, infoPtr->date.wMonth, infoPtr->date.wDay, infoPtr->date.wDayOfWeek);
1138 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
1139 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1140 DATETIME_SendDateTimeChangeNotify (infoPtr);
1141 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1143 if ((lpnmh->hwndFrom == infoPtr->hUpdown) && (lpnmh->code == UDN_DELTAPOS)) {
1144 const NM_UPDOWN *lpnmud = (const NM_UPDOWN*)lpnmh;
1145 TRACE("Delta pos %d\n", lpnmud->iDelta);
1146 infoPtr->pendingUpdown = lpnmud->iDelta;
1148 return 0;
1152 static LRESULT
1153 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode)
1155 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1156 int wrap = 0;
1157 int new;
1159 if (!(infoPtr->haveFocus)) return 0;
1160 if ((fieldNum==0) && (infoPtr->select)) return 0;
1162 if (infoPtr->select & FORMATCALLMASK) {
1163 FIXME ("Callbacks not implemented yet\n");
1166 switch (vkCode) {
1167 case VK_ADD:
1168 case VK_UP:
1169 infoPtr->nCharsEntered = 0;
1170 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1171 DATETIME_SendDateTimeChangeNotify (infoPtr);
1172 break;
1173 case VK_SUBTRACT:
1174 case VK_DOWN:
1175 infoPtr->nCharsEntered = 0;
1176 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1177 DATETIME_SendDateTimeChangeNotify (infoPtr);
1178 break;
1179 case VK_HOME:
1180 infoPtr->nCharsEntered = 0;
1181 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MIN);
1182 DATETIME_SendDateTimeChangeNotify (infoPtr);
1183 break;
1184 case VK_END:
1185 infoPtr->nCharsEntered = 0;
1186 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MAX);
1187 DATETIME_SendDateTimeChangeNotify (infoPtr);
1188 break;
1189 case VK_LEFT:
1190 new = infoPtr->select;
1191 do {
1192 if (new == 0) {
1193 new = new - 1;
1194 wrap++;
1195 } else {
1196 new--;
1198 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1199 if (new != infoPtr->select)
1200 DATETIME_SetSelectedField(infoPtr, new);
1201 break;
1202 case VK_RIGHT:
1203 new = infoPtr->select;
1204 do {
1205 new++;
1206 if (new==infoPtr->nrFields) {
1207 new = 0;
1208 wrap++;
1210 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1211 if (new != infoPtr->select)
1212 DATETIME_SetSelectedField(infoPtr, new);
1213 break;
1216 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1218 return 0;
1222 static LRESULT
1223 DATETIME_Char (DATETIME_INFO *infoPtr, WPARAM vkCode)
1225 int fieldNum, fieldSpec;
1227 fieldNum = infoPtr->select & DTHT_DATEFIELD;
1228 fieldSpec = infoPtr->fieldspec[fieldNum];
1230 if (fieldSpec == ONELETTERAMPM || fieldSpec == TWOLETTERAMPM) {
1231 infoPtr->charsEntered[0] = vkCode;
1232 infoPtr->nCharsEntered = 1;
1234 DATETIME_ApplySelectedField(infoPtr);
1235 } else if (vkCode >= '0' && vkCode <= '9') {
1236 int maxChars;
1238 infoPtr->charsEntered[infoPtr->nCharsEntered++] = vkCode;
1240 if (fieldSpec == INVALIDFULLYEAR || fieldSpec == FULLYEAR)
1241 maxChars = 4;
1242 else
1243 maxChars = 2;
1245 if ((fieldSpec == ONEDIGIT12HOUR ||
1246 fieldSpec == TWODIGIT12HOUR ||
1247 fieldSpec == ONEDIGIT24HOUR ||
1248 fieldSpec == TWODIGIT24HOUR) &&
1249 (infoPtr->nCharsEntered == 1))
1251 if (vkCode >= '3')
1252 maxChars = 1;
1255 if (maxChars == infoPtr->nCharsEntered)
1256 DATETIME_ApplySelectedField(infoPtr);
1259 return 0;
1263 static LRESULT
1264 DATETIME_VScroll (DATETIME_INFO *infoPtr, WORD wScroll)
1266 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1268 if ((SHORT)LOWORD(wScroll) != SB_THUMBPOSITION) return 0;
1269 if (!(infoPtr->haveFocus)) return 0;
1270 if ((fieldNum==0) && (infoPtr->select)) return 0;
1272 if (infoPtr->pendingUpdown >= 0) {
1273 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1274 DATETIME_SendDateTimeChangeNotify (infoPtr);
1276 else {
1277 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1278 DATETIME_SendDateTimeChangeNotify (infoPtr);
1281 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1283 return 0;
1287 static LRESULT
1288 DATETIME_KillFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1290 TRACE("lost focus to %p\n", lostFocus);
1292 if (infoPtr->haveFocus) {
1293 DATETIME_SendSimpleNotify (infoPtr, NM_KILLFOCUS);
1294 infoPtr->haveFocus = 0;
1295 DATETIME_SetSelectedField (infoPtr, -1);
1298 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
1300 return 0;
1304 static LRESULT
1305 DATETIME_NCCreate (HWND hwnd, const CREATESTRUCTW *lpcs)
1307 DWORD dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1308 /* force control to have client edge */
1309 dwExStyle |= WS_EX_CLIENTEDGE;
1310 SetWindowLongW(hwnd, GWL_EXSTYLE, dwExStyle);
1312 return DefWindowProcW(hwnd, WM_NCCREATE, 0, (LPARAM)lpcs);
1316 static LRESULT
1317 DATETIME_SetFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1319 TRACE("got focus from %p\n", lostFocus);
1321 /* if monthcal is open and it loses focus, close monthcal */
1322 if (infoPtr->hMonthCal && (lostFocus == infoPtr->hMonthCal) &&
1323 IsWindowVisible(infoPtr->hMonthCal))
1325 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1326 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1327 /* note: this get triggered even if monthcal loses focus to a dropdown
1328 * box click, which occurs without an intermediate WM_PAINT call
1330 infoPtr->bDropdownEnabled = FALSE;
1331 return 0;
1334 if (infoPtr->haveFocus == 0) {
1335 DATETIME_SendSimpleNotify (infoPtr, NM_SETFOCUS);
1336 infoPtr->haveFocus = DTHT_GOTFOCUS;
1339 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1341 return 0;
1345 static BOOL
1346 DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr)
1348 NMDATETIMECHANGE dtdtc;
1350 dtdtc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1351 dtdtc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1352 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1354 dtdtc.dwFlags = infoPtr->dateValid ? GDT_VALID : GDT_NONE;
1356 dtdtc.st = infoPtr->date;
1357 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1358 dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1362 static BOOL
1363 DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code)
1365 NMHDR nmhdr;
1367 TRACE("%x\n", code);
1368 nmhdr.hwndFrom = infoPtr->hwndSelf;
1369 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1370 nmhdr.code = code;
1372 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1373 nmhdr.idFrom, (LPARAM)&nmhdr);
1376 static LRESULT
1377 DATETIME_Size (DATETIME_INFO *infoPtr, INT width, INT height)
1379 /* set size */
1380 infoPtr->rcClient.bottom = height;
1381 infoPtr->rcClient.right = width;
1383 TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1385 infoPtr->rcDraw = infoPtr->rcClient;
1387 if (infoPtr->dwStyle & DTS_UPDOWN) {
1388 SetWindowPos(infoPtr->hUpdown, NULL,
1389 infoPtr->rcClient.right-14, 0,
1390 15, infoPtr->rcClient.bottom - infoPtr->rcClient.top,
1391 SWP_NOACTIVATE | SWP_NOZORDER);
1393 else {
1394 /* set the size of the button that drops the calendar down */
1395 /* FIXME: account for style that allows button on left side */
1396 infoPtr->calbutton.top = infoPtr->rcDraw.top;
1397 infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1398 infoPtr->calbutton.left = infoPtr->rcDraw.right-15;
1399 infoPtr->calbutton.right = infoPtr->rcDraw.right;
1402 /* set enable/disable button size for show none style being enabled */
1403 /* FIXME: these dimensions are completely incorrect */
1404 infoPtr->checkbox.top = infoPtr->rcDraw.top;
1405 infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1406 infoPtr->checkbox.left = infoPtr->rcDraw.left;
1407 infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1409 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1411 return 0;
1414 static LRESULT
1415 DATETIME_StyleChanging(DATETIME_INFO *infoPtr, WPARAM wStyleType, STYLESTRUCT *lpss)
1417 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1418 wStyleType, lpss->styleOld, lpss->styleNew);
1420 /* block DTS_SHOWNONE change */
1421 if ((lpss->styleNew ^ lpss->styleOld) & DTS_SHOWNONE)
1423 if (lpss->styleOld & DTS_SHOWNONE)
1424 lpss->styleNew |= DTS_SHOWNONE;
1425 else
1426 lpss->styleNew &= ~DTS_SHOWNONE;
1429 return 0;
1432 static LRESULT
1433 DATETIME_StyleChanged(DATETIME_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
1435 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1436 wStyleType, lpss->styleOld, lpss->styleNew);
1438 if (wStyleType != GWL_STYLE) return 0;
1440 infoPtr->dwStyle = lpss->styleNew;
1442 if ( !(lpss->styleOld & DTS_SHOWNONE) && (lpss->styleNew & DTS_SHOWNONE) ) {
1443 infoPtr->hwndCheckbut = CreateWindowExW (0, WC_BUTTONW, 0, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1444 2, 2, 13, 13, infoPtr->hwndSelf, 0,
1445 (HINSTANCE)GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_HINSTANCE), 0);
1446 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, infoPtr->dateValid ? 1 : 0, 0);
1448 if ( (lpss->styleOld & DTS_SHOWNONE) && !(lpss->styleNew & DTS_SHOWNONE) ) {
1449 DestroyWindow(infoPtr->hwndCheckbut);
1450 infoPtr->hwndCheckbut = 0;
1452 if ( !(lpss->styleOld & DTS_UPDOWN) && (lpss->styleNew & DTS_UPDOWN) ) {
1453 infoPtr->hUpdown = CreateUpDownControl (WS_CHILD | WS_BORDER | WS_VISIBLE, 120, 1, 20, 20,
1454 infoPtr->hwndSelf, 1, 0, 0, UD_MAXVAL, UD_MINVAL, 0);
1456 if ( (lpss->styleOld & DTS_UPDOWN) && !(lpss->styleNew & DTS_UPDOWN) ) {
1457 DestroyWindow(infoPtr->hUpdown);
1458 infoPtr->hUpdown = 0;
1461 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1462 return 0;
1466 static LRESULT
1467 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1469 infoPtr->hFont = font;
1470 if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1471 return 0;
1475 static LRESULT
1476 DATETIME_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1478 DATETIME_INFO *infoPtr = Alloc (sizeof(DATETIME_INFO));
1479 STYLESTRUCT ss = { 0, lpcs->style };
1481 if (!infoPtr) return -1;
1483 infoPtr->hwndSelf = hwnd;
1484 infoPtr->dwStyle = lpcs->style;
1486 infoPtr->nrFieldsAllocated = 32;
1487 infoPtr->fieldspec = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1488 infoPtr->fieldRect = Alloc (infoPtr->nrFieldsAllocated * sizeof(RECT));
1489 infoPtr->buflen = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1490 infoPtr->hwndNotify = lpcs->hwndParent;
1491 infoPtr->select = -1; /* initially, nothing is selected */
1492 infoPtr->bDropdownEnabled = TRUE;
1494 DATETIME_StyleChanged(infoPtr, GWL_STYLE, &ss);
1495 DATETIME_SetFormatW (infoPtr, 0);
1497 /* create the monthcal control */
1498 infoPtr->hMonthCal = CreateWindowExW (0, MONTHCAL_CLASSW, 0, WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS,
1499 0, 0, 0, 0, infoPtr->hwndSelf, 0, 0, 0);
1501 /* initialize info structure */
1502 GetLocalTime (&infoPtr->date);
1503 infoPtr->dateValid = TRUE;
1504 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1506 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1508 return 0;
1513 static LRESULT
1514 DATETIME_Destroy (DATETIME_INFO *infoPtr)
1516 if (infoPtr->hwndCheckbut)
1517 DestroyWindow(infoPtr->hwndCheckbut);
1518 if (infoPtr->hUpdown)
1519 DestroyWindow(infoPtr->hUpdown);
1520 if (infoPtr->hMonthCal)
1521 DestroyWindow(infoPtr->hMonthCal);
1522 SetWindowLongPtrW( infoPtr->hwndSelf, 0, 0 ); /* clear infoPtr */
1523 Free (infoPtr->buflen);
1524 Free (infoPtr->fieldRect);
1525 Free (infoPtr->fieldspec);
1526 Free (infoPtr);
1527 return 0;
1531 static INT
1532 DATETIME_GetText (const DATETIME_INFO *infoPtr, INT count, LPWSTR dst)
1534 WCHAR buf[80];
1535 int i;
1537 if (!dst || (count <= 0)) return 0;
1539 dst[0] = 0;
1540 for (i = 0; i < infoPtr->nrFields; i++)
1542 DATETIME_ReturnTxt(infoPtr, i, buf, sizeof(buf)/sizeof(buf[0]));
1543 if ((strlenW(dst) + strlenW(buf)) < count)
1544 strcatW(dst, buf);
1545 else break;
1547 return strlenW(dst);
1551 static LRESULT WINAPI
1552 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1554 DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1556 TRACE ("%x, %lx, %lx\n", uMsg, wParam, lParam);
1558 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
1559 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
1561 switch (uMsg) {
1563 case DTM_GETSYSTEMTIME:
1564 return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1566 case DTM_SETSYSTEMTIME:
1567 return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1569 case DTM_GETRANGE:
1570 return SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1572 case DTM_SETRANGE:
1573 return SendMessageW (infoPtr->hMonthCal, MCM_SETRANGE, wParam, lParam);
1575 case DTM_SETFORMATA:
1576 return DATETIME_SetFormatA (infoPtr, (LPCSTR)lParam);
1578 case DTM_SETFORMATW:
1579 return DATETIME_SetFormatW (infoPtr, (LPCWSTR)lParam);
1581 case DTM_GETMONTHCAL:
1582 return (LRESULT)infoPtr->hMonthCal;
1584 case DTM_SETMCCOLOR:
1585 return SendMessageW (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
1587 case DTM_GETMCCOLOR:
1588 return SendMessageW (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
1590 case DTM_SETMCFONT:
1591 return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1593 case DTM_GETMCFONT:
1594 return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1596 case WM_NOTIFY:
1597 return DATETIME_Notify (infoPtr, (LPNMHDR)lParam);
1599 case WM_ENABLE:
1600 return DATETIME_Enable (infoPtr, (BOOL)wParam);
1602 case WM_ERASEBKGND:
1603 return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1605 case WM_GETDLGCODE:
1606 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1608 case WM_PRINTCLIENT:
1609 case WM_PAINT:
1610 return DATETIME_Paint (infoPtr, (HDC)wParam);
1612 case WM_KEYDOWN:
1613 return DATETIME_KeyDown (infoPtr, wParam);
1615 case WM_CHAR:
1616 return DATETIME_Char (infoPtr, wParam);
1618 case WM_KILLFOCUS:
1619 return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1621 case WM_NCCREATE:
1622 return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1624 case WM_SETFOCUS:
1625 return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1627 case WM_SIZE:
1628 return DATETIME_Size (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1630 case WM_LBUTTONDOWN:
1631 return DATETIME_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1633 case WM_LBUTTONUP:
1634 return DATETIME_LButtonUp (infoPtr);
1636 case WM_VSCROLL:
1637 return DATETIME_VScroll (infoPtr, (WORD)wParam);
1639 case WM_CREATE:
1640 return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1642 case WM_DESTROY:
1643 return DATETIME_Destroy (infoPtr);
1645 case WM_COMMAND:
1646 return DATETIME_Command (infoPtr, wParam, lParam);
1648 case WM_STYLECHANGING:
1649 return DATETIME_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1651 case WM_STYLECHANGED:
1652 return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1654 case WM_SETFONT:
1655 return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1657 case WM_GETFONT:
1658 return (LRESULT) infoPtr->hFont;
1660 case WM_GETTEXT:
1661 return (LRESULT) DATETIME_GetText(infoPtr, wParam, (LPWSTR)lParam);
1663 case WM_SETTEXT:
1664 return CB_ERR;
1666 default:
1667 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
1668 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
1669 uMsg, wParam, lParam);
1670 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1675 void
1676 DATETIME_Register (void)
1678 WNDCLASSW wndClass;
1680 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1681 wndClass.style = CS_GLOBALCLASS;
1682 wndClass.lpfnWndProc = DATETIME_WindowProc;
1683 wndClass.cbClsExtra = 0;
1684 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1685 wndClass.hCursor = LoadCursorW (0, (LPCWSTR)IDC_ARROW);
1686 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1687 wndClass.lpszClassName = DATETIMEPICK_CLASSW;
1689 RegisterClassW (&wndClass);
1693 void
1694 DATETIME_Unregister (void)
1696 UnregisterClassW (DATETIMEPICK_CLASSW, NULL);