dwrite: Added initial stub.
[wine/multimedia.git] / dlls / comctl32 / datetime.c
blobbfd5d5ef4c08be94f2f414f4ca70ff6ca25f09e6
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 int 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 0;
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) == FALSE)
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) ? TRUE : FALSE);
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 selection.left = 0;
787 selection.top = 0;
788 selection.right = size.cx;
789 selection.bottom = size.cy;
790 /* center rectangle */
791 OffsetRect(&selection, (field->right + field->left - size.cx)/2,
792 (field->bottom - size.cy)/2);
794 FillRect(hdc, &selection, hbr);
795 DeleteObject (hbr);
796 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindow);
798 else
799 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindowText);
801 /* draw the date text using the colour set above */
802 DrawTextW (hdc, txt, strlenW(txt), field, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
803 SetTextColor (hdc, oldTextColor);
805 SetBkMode (hdc, oldBkMode);
806 SelectObject (hdc, oldFont);
809 if (!(infoPtr->dwStyle & DTS_UPDOWN)) {
810 DrawFrameControl(hdc, &infoPtr->calbutton, DFC_SCROLL,
811 DFCS_SCROLLDOWN | (infoPtr->bCalDepressed ? DFCS_PUSHED : 0) |
812 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
817 static INT
818 DATETIME_HitTest (const DATETIME_INFO *infoPtr, POINT pt)
820 int i;
822 TRACE ("%d, %d\n", pt.x, pt.y);
824 if (PtInRect (&infoPtr->calbutton, pt)) return DTHT_MCPOPUP;
825 if (PtInRect (&infoPtr->checkbox, pt)) return DTHT_CHECKBOX;
827 for (i = 0; i < infoPtr->nrFields; i++) {
828 if (PtInRect (&infoPtr->fieldRect[i], pt)) return i;
831 return DTHT_NONE;
834 /* Returns index of a closest date field from given counting to left
835 or -1 if there's no such fields at left */
836 static int DATETIME_GetPrevDateField(const DATETIME_INFO *infoPtr, int i)
838 for(--i; i >= 0; i--)
840 if (infoPtr->fieldspec[i] & DTHT_DATEFIELD) return i;
842 return -1;
845 static void
846 DATETIME_ApplySelectedField (DATETIME_INFO *infoPtr)
848 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
849 int i, val=0, clamp_day=0;
850 SYSTEMTIME date = infoPtr->date;
851 int oldyear;
853 if (infoPtr->select == -1 || infoPtr->nCharsEntered == 0)
854 return;
856 if ((infoPtr->fieldspec[fieldNum] == ONELETTERAMPM) ||
857 (infoPtr->fieldspec[fieldNum] == TWOLETTERAMPM))
858 val = infoPtr->charsEntered[0];
859 else {
860 for (i=0; i<infoPtr->nCharsEntered; i++)
861 val = val * 10 + infoPtr->charsEntered[i] - '0';
864 infoPtr->nCharsEntered = 0;
866 switch (infoPtr->fieldspec[fieldNum]) {
867 case ONEDIGITYEAR:
868 case TWODIGITYEAR:
869 oldyear = date.wYear;
870 date.wYear = date.wYear - (date.wYear%100) + val;
872 if (DATETIME_IsDateInValidRange(infoPtr, &date))
873 clamp_day = 1;
874 else
875 date.wYear = oldyear;
877 break;
878 case INVALIDFULLYEAR:
879 case FULLYEAR:
880 oldyear = date.wYear;
881 date.wYear = val;
883 if (DATETIME_IsDateInValidRange(infoPtr, &date))
884 clamp_day = 1;
885 else
886 date.wYear = oldyear;
888 break;
889 case ONEDIGITMONTH:
890 case TWODIGITMONTH:
891 date.wMonth = val;
892 clamp_day = 1;
893 break;
894 case ONEDIGITDAY:
895 case TWODIGITDAY:
896 date.wDay = val;
897 break;
898 case ONEDIGIT12HOUR:
899 case TWODIGIT12HOUR:
900 if (val >= 24)
901 val -= 20;
903 if (val >= 13)
904 date.wHour = val;
905 else if (val != 0) {
906 if (date.wHour >= 12) /* preserve current AM/PM state */
907 date.wHour = (val == 12 ? 12 : val + 12);
908 else
909 date.wHour = (val == 12 ? 0 : val);
911 break;
912 case ONEDIGIT24HOUR:
913 case TWODIGIT24HOUR:
914 date.wHour = val;
915 break;
916 case ONEDIGITMINUTE:
917 case TWODIGITMINUTE:
918 date.wMinute = val;
919 break;
920 case ONEDIGITSECOND:
921 case TWODIGITSECOND:
922 date.wSecond = val;
923 break;
924 case ONELETTERAMPM:
925 case TWOLETTERAMPM:
926 if (val == 'a' || val == 'A') {
927 if (date.wHour >= 12)
928 date.wHour -= 12;
929 } else if (val == 'p' || val == 'P') {
930 if (date.wHour < 12)
931 date.wHour += 12;
933 break;
936 if (clamp_day && date.wDay > MONTHCAL_MonthLength(date.wMonth, date.wYear))
937 date.wDay = MONTHCAL_MonthLength(date.wMonth, date.wYear);
939 if (DATETIME_SetSystemTime(infoPtr, GDT_VALID, &date))
940 DATETIME_SendDateTimeChangeNotify (infoPtr);
943 static void
944 DATETIME_SetSelectedField (DATETIME_INFO *infoPtr, int select)
946 DATETIME_ApplySelectedField(infoPtr);
948 infoPtr->select = select;
949 infoPtr->nCharsEntered = 0;
952 static LRESULT
953 DATETIME_LButtonDown (DATETIME_INFO *infoPtr, INT x, INT y)
955 POINT pt;
956 int new;
958 pt.x = x;
959 pt.y = y;
960 new = DATETIME_HitTest (infoPtr, pt);
962 SetFocus(infoPtr->hwndSelf);
964 if (!(new & DTHT_NODATEMASK) || (new == DTHT_NONE))
966 if (new == DTHT_NONE)
967 new = infoPtr->nrFields - 1;
968 else
970 /* hitting string part moves selection to next date field to left */
971 if (infoPtr->fieldspec[new] & DT_STRING)
973 new = DATETIME_GetPrevDateField(infoPtr, new);
974 if (new == -1) return 0;
976 /* never select full day of week */
977 if (infoPtr->fieldspec[new] == FULLDAY) return 0;
981 DATETIME_SetSelectedField(infoPtr, new);
983 if (infoPtr->select == DTHT_MCPOPUP) {
984 RECT rcMonthCal;
985 POINT pos;
986 SendMessageW(infoPtr->hMonthCal, MCM_GETMINREQRECT, 0, (LPARAM)&rcMonthCal);
988 /* FIXME: button actually is only depressed during dropdown of the */
989 /* calendar control and when the mouse is over the button window */
990 infoPtr->bCalDepressed = TRUE;
992 /* recalculate the position of the monthcal popup */
993 if(infoPtr->dwStyle & DTS_RIGHTALIGN)
994 pos.x = infoPtr->calbutton.left - (rcMonthCal.right - rcMonthCal.left);
995 else
996 /* FIXME: this should be after the area reserved for the checkbox */
997 pos.x = infoPtr->rcDraw.left;
999 pos.y = infoPtr->rcClient.bottom;
1000 OffsetRect( &rcMonthCal, pos.x, pos.y );
1001 MapWindowPoints( infoPtr->hwndSelf, 0, (POINT *)&rcMonthCal, 2 );
1002 SetWindowPos(infoPtr->hMonthCal, 0, rcMonthCal.left, rcMonthCal.top,
1003 rcMonthCal.right - rcMonthCal.left, rcMonthCal.bottom - rcMonthCal.top, 0);
1005 if(IsWindowVisible(infoPtr->hMonthCal)) {
1006 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1007 infoPtr->bDropdownEnabled = FALSE;
1008 DATETIME_SendSimpleNotify (infoPtr, DTN_CLOSEUP);
1009 } else {
1010 const SYSTEMTIME *lprgSysTimeArray = &infoPtr->date;
1011 TRACE("update calendar %04d/%02d/%02d\n",
1012 lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay);
1013 SendMessageW(infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
1015 if (infoPtr->bDropdownEnabled) {
1016 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
1017 DATETIME_SendSimpleNotify (infoPtr, DTN_DROPDOWN);
1019 infoPtr->bDropdownEnabled = TRUE;
1022 TRACE ("dt:%p mc:%p mc parent:%p, desktop:%p\n",
1023 infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hwndNotify, GetDesktopWindow ());
1026 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1028 return 0;
1032 static LRESULT
1033 DATETIME_LButtonUp (DATETIME_INFO *infoPtr)
1035 if(infoPtr->bCalDepressed) {
1036 infoPtr->bCalDepressed = FALSE;
1037 InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
1040 return 0;
1044 static LRESULT
1045 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
1047 if (!hdc) {
1048 PAINTSTRUCT ps;
1049 hdc = BeginPaint (infoPtr->hwndSelf, &ps);
1050 DATETIME_Refresh (infoPtr, hdc);
1051 EndPaint (infoPtr->hwndSelf, &ps);
1052 } else {
1053 DATETIME_Refresh (infoPtr, hdc);
1056 /* Not a click on the dropdown box, enabled it */
1057 infoPtr->bDropdownEnabled = TRUE;
1059 return 0;
1063 static LRESULT
1064 DATETIME_Button_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1066 if( HIWORD(wParam) == BN_CLICKED) {
1067 DWORD state = SendMessageW((HWND)lParam, BM_GETCHECK, 0, 0);
1068 infoPtr->dateValid = (state == BST_CHECKED);
1069 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1071 return 0;
1076 static LRESULT
1077 DATETIME_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1079 TRACE("hwndbutton = %p\n", infoPtr->hwndCheckbut);
1080 if(infoPtr->hwndCheckbut == (HWND)lParam)
1081 return DATETIME_Button_Command(infoPtr, wParam, lParam);
1082 return 0;
1086 static LRESULT
1087 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
1089 TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
1090 if (bEnable)
1091 infoPtr->dwStyle &= ~WS_DISABLED;
1092 else
1093 infoPtr->dwStyle |= WS_DISABLED;
1095 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1097 return 0;
1101 static LRESULT
1102 DATETIME_EraseBackground (const DATETIME_INFO *infoPtr, HDC hdc)
1104 HBRUSH hBrush, hSolidBrush = NULL;
1105 RECT rc;
1107 if (infoPtr->dwStyle & WS_DISABLED)
1108 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
1109 else
1111 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
1112 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
1113 if (!hBrush)
1114 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
1117 GetClientRect (infoPtr->hwndSelf, &rc);
1119 FillRect (hdc, &rc, hBrush);
1121 if (hSolidBrush)
1122 DeleteObject(hSolidBrush);
1124 return -1;
1128 static LRESULT
1129 DATETIME_Notify (DATETIME_INFO *infoPtr, const NMHDR *lpnmh)
1131 TRACE ("Got notification %x from %p\n", lpnmh->code, lpnmh->hwndFrom);
1132 TRACE ("info: %p %p %p\n", infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hUpdown);
1134 if (lpnmh->code == MCN_SELECT) {
1135 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1136 infoPtr->dateValid = TRUE;
1137 SendMessageW (infoPtr->hMonthCal, MCM_GETCURSEL, 0, (LPARAM)&infoPtr->date);
1138 TRACE("got from calendar %04d/%02d/%02d day of week %d\n",
1139 infoPtr->date.wYear, infoPtr->date.wMonth, infoPtr->date.wDay, infoPtr->date.wDayOfWeek);
1140 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
1141 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1142 DATETIME_SendDateTimeChangeNotify (infoPtr);
1143 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1145 if ((lpnmh->hwndFrom == infoPtr->hUpdown) && (lpnmh->code == UDN_DELTAPOS)) {
1146 const NM_UPDOWN *lpnmud = (const NM_UPDOWN*)lpnmh;
1147 TRACE("Delta pos %d\n", lpnmud->iDelta);
1148 infoPtr->pendingUpdown = lpnmud->iDelta;
1150 return 0;
1154 static LRESULT
1155 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode)
1157 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1158 int wrap = 0;
1159 int new;
1161 if (!(infoPtr->haveFocus)) return 0;
1162 if ((fieldNum==0) && (infoPtr->select)) return 0;
1164 if (infoPtr->select & FORMATCALLMASK) {
1165 FIXME ("Callbacks not implemented yet\n");
1168 switch (vkCode) {
1169 case VK_ADD:
1170 case VK_UP:
1171 infoPtr->nCharsEntered = 0;
1172 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1173 DATETIME_SendDateTimeChangeNotify (infoPtr);
1174 break;
1175 case VK_SUBTRACT:
1176 case VK_DOWN:
1177 infoPtr->nCharsEntered = 0;
1178 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1179 DATETIME_SendDateTimeChangeNotify (infoPtr);
1180 break;
1181 case VK_HOME:
1182 infoPtr->nCharsEntered = 0;
1183 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MIN);
1184 DATETIME_SendDateTimeChangeNotify (infoPtr);
1185 break;
1186 case VK_END:
1187 infoPtr->nCharsEntered = 0;
1188 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MAX);
1189 DATETIME_SendDateTimeChangeNotify (infoPtr);
1190 break;
1191 case VK_LEFT:
1192 new = infoPtr->select;
1193 do {
1194 if (new == 0) {
1195 new = new - 1;
1196 wrap++;
1197 } else {
1198 new--;
1200 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1201 if (new != infoPtr->select)
1202 DATETIME_SetSelectedField(infoPtr, new);
1203 break;
1204 case VK_RIGHT:
1205 new = infoPtr->select;
1206 do {
1207 new++;
1208 if (new==infoPtr->nrFields) {
1209 new = 0;
1210 wrap++;
1212 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1213 if (new != infoPtr->select)
1214 DATETIME_SetSelectedField(infoPtr, new);
1215 break;
1218 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1220 return 0;
1224 static LRESULT
1225 DATETIME_Char (DATETIME_INFO *infoPtr, WPARAM vkCode)
1227 int fieldNum, fieldSpec;
1229 fieldNum = infoPtr->select & DTHT_DATEFIELD;
1230 fieldSpec = infoPtr->fieldspec[fieldNum];
1232 if (fieldSpec == ONELETTERAMPM || fieldSpec == TWOLETTERAMPM) {
1233 infoPtr->charsEntered[0] = vkCode;
1234 infoPtr->nCharsEntered = 1;
1236 DATETIME_ApplySelectedField(infoPtr);
1237 } else if (vkCode >= '0' && vkCode <= '9') {
1238 int maxChars;
1240 infoPtr->charsEntered[infoPtr->nCharsEntered++] = vkCode;
1242 if (fieldSpec == INVALIDFULLYEAR || fieldSpec == FULLYEAR)
1243 maxChars = 4;
1244 else
1245 maxChars = 2;
1247 if ((fieldSpec == ONEDIGIT12HOUR ||
1248 fieldSpec == TWODIGIT12HOUR ||
1249 fieldSpec == ONEDIGIT24HOUR ||
1250 fieldSpec == TWODIGIT24HOUR) &&
1251 (infoPtr->nCharsEntered == 1))
1253 if (vkCode >= '3')
1254 maxChars = 1;
1257 if (maxChars == infoPtr->nCharsEntered)
1258 DATETIME_ApplySelectedField(infoPtr);
1261 return 0;
1265 static LRESULT
1266 DATETIME_VScroll (DATETIME_INFO *infoPtr, WORD wScroll)
1268 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1270 if ((SHORT)LOWORD(wScroll) != SB_THUMBPOSITION) return 0;
1271 if (!(infoPtr->haveFocus)) return 0;
1272 if ((fieldNum==0) && (infoPtr->select)) return 0;
1274 if (infoPtr->pendingUpdown >= 0) {
1275 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1276 DATETIME_SendDateTimeChangeNotify (infoPtr);
1278 else {
1279 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1280 DATETIME_SendDateTimeChangeNotify (infoPtr);
1283 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1285 return 0;
1289 static LRESULT
1290 DATETIME_KillFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1292 TRACE("lost focus to %p\n", lostFocus);
1294 if (infoPtr->haveFocus) {
1295 DATETIME_SendSimpleNotify (infoPtr, NM_KILLFOCUS);
1296 infoPtr->haveFocus = 0;
1297 DATETIME_SetSelectedField (infoPtr, -1);
1300 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
1302 return 0;
1306 static LRESULT
1307 DATETIME_NCCreate (HWND hwnd, const CREATESTRUCTW *lpcs)
1309 DWORD dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1310 /* force control to have client edge */
1311 dwExStyle |= WS_EX_CLIENTEDGE;
1312 SetWindowLongW(hwnd, GWL_EXSTYLE, dwExStyle);
1314 return DefWindowProcW(hwnd, WM_NCCREATE, 0, (LPARAM)lpcs);
1318 static LRESULT
1319 DATETIME_SetFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1321 TRACE("got focus from %p\n", lostFocus);
1323 /* if monthcal is open and it loses focus, close monthcal */
1324 if (infoPtr->hMonthCal && (lostFocus == infoPtr->hMonthCal) &&
1325 IsWindowVisible(infoPtr->hMonthCal))
1327 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1328 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1329 /* note: this get triggered even if monthcal loses focus to a dropdown
1330 * box click, which occurs without an intermediate WM_PAINT call
1332 infoPtr->bDropdownEnabled = FALSE;
1333 return 0;
1336 if (infoPtr->haveFocus == 0) {
1337 DATETIME_SendSimpleNotify (infoPtr, NM_SETFOCUS);
1338 infoPtr->haveFocus = DTHT_GOTFOCUS;
1341 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1343 return 0;
1347 static BOOL
1348 DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr)
1350 NMDATETIMECHANGE dtdtc;
1352 dtdtc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1353 dtdtc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1354 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1356 dtdtc.dwFlags = infoPtr->dateValid ? GDT_VALID : GDT_NONE;
1358 dtdtc.st = infoPtr->date;
1359 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1360 dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1364 static BOOL
1365 DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code)
1367 NMHDR nmhdr;
1369 TRACE("%x\n", code);
1370 nmhdr.hwndFrom = infoPtr->hwndSelf;
1371 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1372 nmhdr.code = code;
1374 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1375 nmhdr.idFrom, (LPARAM)&nmhdr);
1378 static LRESULT
1379 DATETIME_Size (DATETIME_INFO *infoPtr, INT width, INT height)
1381 /* set size */
1382 infoPtr->rcClient.bottom = height;
1383 infoPtr->rcClient.right = width;
1385 TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1387 infoPtr->rcDraw = infoPtr->rcClient;
1389 if (infoPtr->dwStyle & DTS_UPDOWN) {
1390 SetWindowPos(infoPtr->hUpdown, NULL,
1391 infoPtr->rcClient.right-14, 0,
1392 15, infoPtr->rcClient.bottom - infoPtr->rcClient.top,
1393 SWP_NOACTIVATE | SWP_NOZORDER);
1395 else {
1396 /* set the size of the button that drops the calendar down */
1397 /* FIXME: account for style that allows button on left side */
1398 infoPtr->calbutton.top = infoPtr->rcDraw.top;
1399 infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1400 infoPtr->calbutton.left = infoPtr->rcDraw.right-15;
1401 infoPtr->calbutton.right = infoPtr->rcDraw.right;
1404 /* set enable/disable button size for show none style being enabled */
1405 /* FIXME: these dimensions are completely incorrect */
1406 infoPtr->checkbox.top = infoPtr->rcDraw.top;
1407 infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1408 infoPtr->checkbox.left = infoPtr->rcDraw.left;
1409 infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1411 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1413 return 0;
1416 static LRESULT
1417 DATETIME_StyleChanging(DATETIME_INFO *infoPtr, WPARAM wStyleType, STYLESTRUCT *lpss)
1419 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1420 wStyleType, lpss->styleOld, lpss->styleNew);
1422 /* block DTS_SHOWNONE change */
1423 if ((lpss->styleNew ^ lpss->styleOld) & DTS_SHOWNONE)
1425 if (lpss->styleOld & DTS_SHOWNONE)
1426 lpss->styleNew |= DTS_SHOWNONE;
1427 else
1428 lpss->styleNew &= ~DTS_SHOWNONE;
1431 return 0;
1434 static LRESULT
1435 DATETIME_StyleChanged(DATETIME_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
1437 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1438 wStyleType, lpss->styleOld, lpss->styleNew);
1440 if (wStyleType != GWL_STYLE) return 0;
1442 infoPtr->dwStyle = lpss->styleNew;
1444 if ( !(lpss->styleOld & DTS_SHOWNONE) && (lpss->styleNew & DTS_SHOWNONE) ) {
1445 infoPtr->hwndCheckbut = CreateWindowExW (0, WC_BUTTONW, 0, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1446 2, 2, 13, 13, infoPtr->hwndSelf, 0,
1447 (HINSTANCE)GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_HINSTANCE), 0);
1448 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, infoPtr->dateValid ? 1 : 0, 0);
1450 if ( (lpss->styleOld & DTS_SHOWNONE) && !(lpss->styleNew & DTS_SHOWNONE) ) {
1451 DestroyWindow(infoPtr->hwndCheckbut);
1452 infoPtr->hwndCheckbut = 0;
1454 if ( !(lpss->styleOld & DTS_UPDOWN) && (lpss->styleNew & DTS_UPDOWN) ) {
1455 infoPtr->hUpdown = CreateUpDownControl (WS_CHILD | WS_BORDER | WS_VISIBLE, 120, 1, 20, 20,
1456 infoPtr->hwndSelf, 1, 0, 0, UD_MAXVAL, UD_MINVAL, 0);
1458 if ( (lpss->styleOld & DTS_UPDOWN) && !(lpss->styleNew & DTS_UPDOWN) ) {
1459 DestroyWindow(infoPtr->hUpdown);
1460 infoPtr->hUpdown = 0;
1463 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1464 return 0;
1468 static LRESULT
1469 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1471 infoPtr->hFont = font;
1472 if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1473 return 0;
1477 static LRESULT
1478 DATETIME_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1480 DATETIME_INFO *infoPtr = Alloc (sizeof(DATETIME_INFO));
1481 STYLESTRUCT ss = { 0, lpcs->style };
1483 if (!infoPtr) return -1;
1485 infoPtr->hwndSelf = hwnd;
1486 infoPtr->dwStyle = lpcs->style;
1488 infoPtr->nrFieldsAllocated = 32;
1489 infoPtr->fieldspec = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1490 infoPtr->fieldRect = Alloc (infoPtr->nrFieldsAllocated * sizeof(RECT));
1491 infoPtr->buflen = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1492 infoPtr->hwndNotify = lpcs->hwndParent;
1493 infoPtr->select = -1; /* initially, nothing is selected */
1494 infoPtr->bDropdownEnabled = TRUE;
1496 DATETIME_StyleChanged(infoPtr, GWL_STYLE, &ss);
1497 DATETIME_SetFormatW (infoPtr, 0);
1499 /* create the monthcal control */
1500 infoPtr->hMonthCal = CreateWindowExW (0, MONTHCAL_CLASSW, 0, WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS,
1501 0, 0, 0, 0, infoPtr->hwndSelf, 0, 0, 0);
1503 /* initialize info structure */
1504 GetLocalTime (&infoPtr->date);
1505 infoPtr->dateValid = TRUE;
1506 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1508 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1510 return 0;
1515 static LRESULT
1516 DATETIME_Destroy (DATETIME_INFO *infoPtr)
1518 if (infoPtr->hwndCheckbut)
1519 DestroyWindow(infoPtr->hwndCheckbut);
1520 if (infoPtr->hUpdown)
1521 DestroyWindow(infoPtr->hUpdown);
1522 if (infoPtr->hMonthCal)
1523 DestroyWindow(infoPtr->hMonthCal);
1524 SetWindowLongPtrW( infoPtr->hwndSelf, 0, 0 ); /* clear infoPtr */
1525 Free (infoPtr->buflen);
1526 Free (infoPtr->fieldRect);
1527 Free (infoPtr->fieldspec);
1528 Free (infoPtr);
1529 return 0;
1533 static INT
1534 DATETIME_GetText (const DATETIME_INFO *infoPtr, INT count, LPWSTR dst)
1536 WCHAR buf[80];
1537 int i;
1539 if (!dst || (count <= 0)) return 0;
1541 dst[0] = 0;
1542 for (i = 0; i < infoPtr->nrFields; i++)
1544 DATETIME_ReturnTxt(infoPtr, i, buf, sizeof(buf)/sizeof(buf[0]));
1545 if ((strlenW(dst) + strlenW(buf)) < count)
1546 strcatW(dst, buf);
1547 else break;
1549 return strlenW(dst);
1553 static LRESULT WINAPI
1554 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1556 DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1557 LRESULT ret;
1559 TRACE ("%x, %lx, %lx\n", uMsg, wParam, lParam);
1561 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
1562 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
1564 switch (uMsg) {
1566 case DTM_GETSYSTEMTIME:
1567 return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1569 case DTM_SETSYSTEMTIME:
1570 return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1572 case DTM_GETRANGE:
1573 ret = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1574 return ret ? ret : 1; /* bug emulation */
1576 case DTM_SETRANGE:
1577 return SendMessageW (infoPtr->hMonthCal, MCM_SETRANGE, wParam, lParam);
1579 case DTM_SETFORMATA:
1580 return DATETIME_SetFormatA (infoPtr, (LPCSTR)lParam);
1582 case DTM_SETFORMATW:
1583 return DATETIME_SetFormatW (infoPtr, (LPCWSTR)lParam);
1585 case DTM_GETMONTHCAL:
1586 return (LRESULT)infoPtr->hMonthCal;
1588 case DTM_SETMCCOLOR:
1589 return SendMessageW (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
1591 case DTM_GETMCCOLOR:
1592 return SendMessageW (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
1594 case DTM_SETMCFONT:
1595 return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1597 case DTM_GETMCFONT:
1598 return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1600 case WM_NOTIFY:
1601 return DATETIME_Notify (infoPtr, (LPNMHDR)lParam);
1603 case WM_ENABLE:
1604 return DATETIME_Enable (infoPtr, (BOOL)wParam);
1606 case WM_ERASEBKGND:
1607 return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1609 case WM_GETDLGCODE:
1610 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1612 case WM_PRINTCLIENT:
1613 case WM_PAINT:
1614 return DATETIME_Paint (infoPtr, (HDC)wParam);
1616 case WM_KEYDOWN:
1617 return DATETIME_KeyDown (infoPtr, wParam);
1619 case WM_CHAR:
1620 return DATETIME_Char (infoPtr, wParam);
1622 case WM_KILLFOCUS:
1623 return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1625 case WM_NCCREATE:
1626 return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1628 case WM_SETFOCUS:
1629 return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1631 case WM_SIZE:
1632 return DATETIME_Size (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1634 case WM_LBUTTONDOWN:
1635 return DATETIME_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1637 case WM_LBUTTONUP:
1638 return DATETIME_LButtonUp (infoPtr);
1640 case WM_VSCROLL:
1641 return DATETIME_VScroll (infoPtr, (WORD)wParam);
1643 case WM_CREATE:
1644 return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1646 case WM_DESTROY:
1647 return DATETIME_Destroy (infoPtr);
1649 case WM_COMMAND:
1650 return DATETIME_Command (infoPtr, wParam, lParam);
1652 case WM_STYLECHANGING:
1653 return DATETIME_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1655 case WM_STYLECHANGED:
1656 return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1658 case WM_SETFONT:
1659 return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1661 case WM_GETFONT:
1662 return (LRESULT) infoPtr->hFont;
1664 case WM_GETTEXT:
1665 return (LRESULT) DATETIME_GetText(infoPtr, wParam, (LPWSTR)lParam);
1667 case WM_SETTEXT:
1668 return CB_ERR;
1670 default:
1671 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
1672 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
1673 uMsg, wParam, lParam);
1674 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1679 void
1680 DATETIME_Register (void)
1682 WNDCLASSW wndClass;
1684 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1685 wndClass.style = CS_GLOBALCLASS;
1686 wndClass.lpfnWndProc = DATETIME_WindowProc;
1687 wndClass.cbClsExtra = 0;
1688 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1689 wndClass.hCursor = LoadCursorW (0, (LPCWSTR)IDC_ARROW);
1690 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1691 wndClass.lpszClassName = DATETIMEPICK_CLASSW;
1693 RegisterClassW (&wndClass);
1697 void
1698 DATETIME_Unregister (void)
1700 UnregisterClassW (DATETIMEPICK_CLASSW, NULL);