api-ms-win-core-comm-l1-1-0: Add dll.
[wine.git] / dlls / comctl32 / datetime.c
blobfb8a5c0380ce4340095418424ce94984de248363
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 * TODO:
24 * -- DTS_APPCANPARSE
25 * -- DTS_SHORTDATECENTURYFORMAT
26 * -- DTN_FORMAT
27 * -- DTN_FORMATQUERY
28 * -- DTN_USERSTRING
29 * -- DTN_WMKEYDOWN
30 * -- FORMATCALLBACK
33 #include <math.h>
34 #include <string.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <limits.h>
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "winnls.h"
44 #include "commctrl.h"
45 #include "comctl32.h"
46 #include "wine/debug.h"
47 #include "wine/unicode.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(datetime);
51 typedef struct
53 HWND hwndSelf;
54 HWND hMonthCal;
55 HWND hwndNotify;
56 HWND hUpdown;
57 DWORD dwStyle;
58 SYSTEMTIME date;
59 BOOL dateValid;
60 HWND hwndCheckbut;
61 RECT rcClient; /* rect around the edge of the window */
62 RECT rcDraw; /* rect inside of the border */
63 RECT checkbox; /* checkbox allowing the control to be enabled/disabled */
64 RECT calbutton; /* button that toggles the dropdown of the monthcal control */
65 BOOL bCalDepressed; /* TRUE = cal button is depressed */
66 BOOL bDropdownEnabled;
67 int select;
68 WCHAR charsEntered[4];
69 int nCharsEntered;
70 HFONT hFont;
71 int nrFieldsAllocated;
72 int nrFields;
73 int haveFocus;
74 int *fieldspec;
75 RECT *fieldRect;
76 int *buflen;
77 WCHAR textbuf[256];
78 POINT monthcal_pos;
79 int pendingUpdown;
80 } DATETIME_INFO, *LPDATETIME_INFO;
82 /* in monthcal.c */
83 extern int MONTHCAL_MonthLength(int month, int year);
84 extern int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace);
86 /* this list of defines is closely related to `allowedformatchars' defined
87 * in datetime.c; the high nibble indicates the `base type' of the format
88 * specifier.
89 * Do not change without first reading DATETIME_UseFormat.
93 #define DT_END_FORMAT 0
94 #define ONEDIGITDAY 0x01
95 #define TWODIGITDAY 0x02
96 #define THREECHARDAY 0x03
97 #define FULLDAY 0x04
98 #define ONEDIGIT12HOUR 0x11
99 #define TWODIGIT12HOUR 0x12
100 #define ONEDIGIT24HOUR 0x21
101 #define TWODIGIT24HOUR 0x22
102 #define ONEDIGITMINUTE 0x31
103 #define TWODIGITMINUTE 0x32
104 #define ONEDIGITMONTH 0x41
105 #define TWODIGITMONTH 0x42
106 #define THREECHARMONTH 0x43
107 #define FULLMONTH 0x44
108 #define ONEDIGITSECOND 0x51
109 #define TWODIGITSECOND 0x52
110 #define ONELETTERAMPM 0x61
111 #define TWOLETTERAMPM 0x62
112 #define ONEDIGITYEAR 0x71
113 #define TWODIGITYEAR 0x72
114 #define INVALIDFULLYEAR 0x73 /* FIXME - yyy is not valid - we'll treat it as yyyy */
115 #define FULLYEAR 0x74
116 #define FORMATCALLBACK 0x81 /* -> maximum of 0x80 callbacks possible */
117 #define FORMATCALLMASK 0x80
118 #define DT_STRING 0x0100
120 #define DTHT_DATEFIELD 0xff /* for hit-testing */
122 #define DTHT_NONE 0x1000
123 #define DTHT_CHECKBOX 0x2000 /* these should end at '00' , to make */
124 #define DTHT_MCPOPUP 0x3000 /* & DTHT_DATEFIELD 0 when DATETIME_KeyDown */
125 #define DTHT_GOTFOCUS 0x4000 /* tests for date-fields */
126 #define DTHT_NODATEMASK 0xf000 /* to mask check and drop down from others */
128 static BOOL DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code);
129 static BOOL DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr);
130 static const WCHAR allowedformatchars[] = {'d', 'h', 'H', 'm', 'M', 's', 't', 'y', 'X', 0};
131 static const int maxrepetition [] = {4,2,2,2,4,2,2,4,-1};
133 /* valid date limits */
134 static const SYSTEMTIME max_allowed_date = { /* wYear */ 9999, /* wMonth */ 12, /* wDayOfWeek */ 0, /* wDay */ 31 };
135 static const SYSTEMTIME min_allowed_date = { /* wYear */ 1752, /* wMonth */ 9, /* wDayOfWeek */ 0, /* wDay */ 14 };
137 static DWORD
138 DATETIME_GetSystemTime (const DATETIME_INFO *infoPtr, SYSTEMTIME *systime)
140 if (!systime) return GDT_NONE;
142 if ((infoPtr->dwStyle & DTS_SHOWNONE) &&
143 (SendMessageW (infoPtr->hwndCheckbut, BM_GETCHECK, 0, 0) == BST_UNCHECKED))
144 return GDT_NONE;
146 *systime = infoPtr->date;
148 return GDT_VALID;
151 /* Checks value is within configured date range
153 * PARAMETERS
155 * [I] infoPtr : valid pointer to control data
156 * [I] date : pointer to valid date data to check
158 * RETURN VALUE
160 * TRUE - date within configured range
161 * FALSE - date is outside configured range
163 static BOOL DATETIME_IsDateInValidRange(const DATETIME_INFO *infoPtr, const SYSTEMTIME *date)
165 SYSTEMTIME range[2];
166 DWORD limits;
168 if ((MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) ||
169 (MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1))
170 return FALSE;
172 limits = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, 0, (LPARAM)range);
174 if (limits & GDTR_MAX)
176 if (MONTHCAL_CompareSystemTime(date, &range[1]) == 1)
177 return FALSE;
180 if (limits & GDTR_MIN)
182 if (MONTHCAL_CompareSystemTime(date, &range[0]) == -1)
183 return FALSE;
186 return TRUE;
189 static BOOL
190 DATETIME_SetSystemTime (DATETIME_INFO *infoPtr, DWORD flag, const SYSTEMTIME *systime)
192 if (!systime) return FALSE;
194 TRACE("%04d/%02d/%02d %02d:%02d:%02d\n",
195 systime->wYear, systime->wMonth, systime->wDay,
196 systime->wHour, systime->wMinute, systime->wSecond);
198 if (flag == GDT_VALID) {
199 if (systime->wYear == 0 ||
200 systime->wMonth < 1 || systime->wMonth > 12 ||
201 systime->wDay < 1 ||
202 systime->wDay > MONTHCAL_MonthLength(systime->wMonth, systime->wYear) ||
203 systime->wHour > 23 ||
204 systime->wMinute > 59 ||
205 systime->wSecond > 59 ||
206 systime->wMilliseconds > 999
208 return FALSE;
210 /* Windows returns true if the date is valid but outside the limits set */
211 if (!DATETIME_IsDateInValidRange(infoPtr, systime))
212 return TRUE;
214 infoPtr->dateValid = TRUE;
215 infoPtr->date = *systime;
216 /* always store a valid day of week */
217 MONTHCAL_CalculateDayOfWeek(&infoPtr->date, TRUE);
219 SendMessageW (infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
220 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
221 } else if ((infoPtr->dwStyle & DTS_SHOWNONE) && (flag == GDT_NONE)) {
222 infoPtr->dateValid = FALSE;
223 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_UNCHECKED, 0);
225 else
226 return FALSE;
228 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
229 return TRUE;
233 /***
234 * Split up a formattxt in actions.
235 * See ms documentation for the meaning of the letter codes/'specifiers'.
237 * Notes:
238 * *'dddddd' is handled as 'dddd' plus 'dd'.
239 * *unrecognized formats are strings (here given the type DT_STRING;
240 * start of the string is encoded in lower bits of DT_STRING.
241 * Therefore, 'string' ends finally up as '<show seconds>tring'.
244 static void
245 DATETIME_UseFormat (DATETIME_INFO *infoPtr, LPCWSTR formattxt)
247 unsigned int i;
248 int j, k, len;
249 BOOL inside_literal = FALSE; /* inside '...' */
250 int *nrFields = &infoPtr->nrFields;
252 *nrFields = 0;
253 infoPtr->fieldspec[*nrFields] = 0;
254 len = strlenW(allowedformatchars);
255 k = 0;
257 for (i = 0; formattxt[i]; i++) {
258 TRACE ("\n%d %c:", i, formattxt[i]);
259 if (!inside_literal) {
260 for (j = 0; j < len; j++) {
261 if (allowedformatchars[j]==formattxt[i]) {
262 TRACE ("%c[%d,%x]", allowedformatchars[j], *nrFields, infoPtr->fieldspec[*nrFields]);
263 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
264 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
265 break;
267 if (infoPtr->fieldspec[*nrFields] >> 4 != j) {
268 (*nrFields)++;
269 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
270 break;
272 if ((infoPtr->fieldspec[*nrFields] & 0x0f) == maxrepetition[j]) {
273 (*nrFields)++;
274 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
275 break;
277 infoPtr->fieldspec[*nrFields]++;
278 break;
279 } /* if allowedformatchar */
280 } /* for j */
282 else
283 j = len;
285 if (formattxt[i] == '\'')
287 inside_literal = !inside_literal;
288 continue;
291 /* char is not a specifier: handle char like a string */
292 if (j == len) {
293 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
294 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
295 infoPtr->buflen[*nrFields] = 0;
296 } else if ((infoPtr->fieldspec[*nrFields] & DT_STRING) != DT_STRING) {
297 (*nrFields)++;
298 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
299 infoPtr->buflen[*nrFields] = 0;
301 infoPtr->textbuf[k] = formattxt[i];
302 k++;
303 infoPtr->buflen[*nrFields]++;
304 } /* if j=len */
306 if (*nrFields == infoPtr->nrFieldsAllocated) {
307 FIXME ("out of memory; should reallocate. crash ahead.\n");
309 } /* for i */
311 TRACE("\n");
313 if (infoPtr->fieldspec[*nrFields] != 0) (*nrFields)++;
317 static BOOL
318 DATETIME_SetFormatW (DATETIME_INFO *infoPtr, LPCWSTR format)
320 WCHAR format_buf[80];
322 if (!format) {
323 DWORD format_item;
325 if ((infoPtr->dwStyle & DTS_SHORTDATECENTURYFORMAT) == DTS_SHORTDATECENTURYFORMAT)
326 format_item = LOCALE_SSHORTDATE;
327 else if ((infoPtr->dwStyle & DTS_LONGDATEFORMAT) == DTS_LONGDATEFORMAT)
328 format_item = LOCALE_SLONGDATE;
329 else if ((infoPtr->dwStyle & DTS_TIMEFORMAT) == DTS_TIMEFORMAT)
330 format_item = LOCALE_STIMEFORMAT;
331 else /* DTS_SHORTDATEFORMAT */
332 format_item = LOCALE_SSHORTDATE;
333 GetLocaleInfoW(LOCALE_USER_DEFAULT, format_item, format_buf, ARRAY_SIZE(format_buf));
334 format = format_buf;
337 DATETIME_UseFormat (infoPtr, format);
338 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
340 return TRUE;
344 static BOOL
345 DATETIME_SetFormatA (DATETIME_INFO *infoPtr, LPCSTR lpszFormat)
347 if (lpszFormat) {
348 BOOL retval;
349 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, NULL, 0);
350 LPWSTR wstr = Alloc(len * sizeof(WCHAR));
351 if (wstr) MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, wstr, len);
352 retval = DATETIME_SetFormatW (infoPtr, wstr);
353 Free (wstr);
354 return retval;
356 else
357 return DATETIME_SetFormatW (infoPtr, 0);
362 static void
363 DATETIME_ReturnTxt (const DATETIME_INFO *infoPtr, int count, LPWSTR result, int resultSize)
365 static const WCHAR fmt_dW[] = { '%', 'd', 0 };
366 static const WCHAR fmt__2dW[] = { '%', '.', '2', 'd', 0 };
367 static const WCHAR fmt__3sW[] = { '%', '.', '3', 's', 0 };
368 SYSTEMTIME date = infoPtr->date;
369 int spec;
370 WCHAR buffer[80];
372 *result=0;
373 TRACE ("%d,%d\n", infoPtr->nrFields, count);
374 if (count>infoPtr->nrFields || count < 0) {
375 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
376 return;
379 if (!infoPtr->fieldspec) return;
381 spec = infoPtr->fieldspec[count];
382 if (spec & DT_STRING) {
383 int txtlen = infoPtr->buflen[count];
385 if (txtlen > resultSize)
386 txtlen = resultSize - 1;
387 memcpy (result, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
388 result[txtlen] = 0;
389 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
390 return;
394 switch (spec) {
395 case DT_END_FORMAT:
396 *result = 0;
397 break;
398 case ONEDIGITDAY:
399 wsprintfW (result, fmt_dW, date.wDay);
400 break;
401 case TWODIGITDAY:
402 wsprintfW (result, fmt__2dW, date.wDay);
403 break;
404 case THREECHARDAY:
405 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1+(date.wDayOfWeek+6)%7, result, 4);
406 /*wsprintfW (result,"%.3s",days[date.wDayOfWeek]);*/
407 break;
408 case FULLDAY:
409 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME1+(date.wDayOfWeek+6)%7, result, resultSize);
410 break;
411 case ONEDIGIT12HOUR:
412 if (date.wHour == 0) {
413 result[0] = '1';
414 result[1] = '2';
415 result[2] = 0;
417 else
418 wsprintfW (result, fmt_dW, date.wHour - (date.wHour > 12 ? 12 : 0));
419 break;
420 case TWODIGIT12HOUR:
421 if (date.wHour == 0) {
422 result[0] = '1';
423 result[1] = '2';
424 result[2] = 0;
426 else
427 wsprintfW (result, fmt__2dW, date.wHour - (date.wHour > 12 ? 12 : 0));
428 break;
429 case ONEDIGIT24HOUR:
430 wsprintfW (result, fmt_dW, date.wHour);
431 break;
432 case TWODIGIT24HOUR:
433 wsprintfW (result, fmt__2dW, date.wHour);
434 break;
435 case ONEDIGITSECOND:
436 wsprintfW (result, fmt_dW, date.wSecond);
437 break;
438 case TWODIGITSECOND:
439 wsprintfW (result, fmt__2dW, date.wSecond);
440 break;
441 case ONEDIGITMINUTE:
442 wsprintfW (result, fmt_dW, date.wMinute);
443 break;
444 case TWODIGITMINUTE:
445 wsprintfW (result, fmt__2dW, date.wMinute);
446 break;
447 case ONEDIGITMONTH:
448 wsprintfW (result, fmt_dW, date.wMonth);
449 break;
450 case TWODIGITMONTH:
451 wsprintfW (result, fmt__2dW, date.wMonth);
452 break;
453 case THREECHARMONTH:
454 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+date.wMonth -1, buffer, ARRAY_SIZE(buffer));
455 wsprintfW (result, fmt__3sW, buffer);
456 break;
457 case FULLMONTH:
458 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+date.wMonth -1,
459 result, resultSize);
460 break;
461 case ONELETTERAMPM:
462 result[0] = (date.wHour < 12 ? 'A' : 'P');
463 result[1] = 0;
464 break;
465 case TWOLETTERAMPM:
466 result[0] = (date.wHour < 12 ? 'A' : 'P');
467 result[1] = 'M';
468 result[2] = 0;
469 break;
470 case FORMATCALLBACK:
471 FIXME ("Not implemented\n");
472 result[0] = 'x';
473 result[1] = 0;
474 break;
475 case ONEDIGITYEAR:
476 wsprintfW (result, fmt_dW, date.wYear-10* (int) floor(date.wYear/10));
477 break;
478 case TWODIGITYEAR:
479 wsprintfW (result, fmt__2dW, date.wYear-100* (int) floor(date.wYear/100));
480 break;
481 case INVALIDFULLYEAR:
482 case FULLYEAR:
483 wsprintfW (result, fmt_dW, date.wYear);
484 break;
487 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
490 static int wrap(int val, int delta, int minVal, int maxVal)
492 val += delta;
493 if (delta == INT_MIN || val < minVal) return maxVal;
494 if (delta == INT_MAX || val > maxVal) return minVal;
495 return val;
498 static void
499 DATETIME_IncreaseField (DATETIME_INFO *infoPtr, int number, int delta)
501 SYSTEMTIME *date = &infoPtr->date;
502 SYSTEMTIME range[2];
503 DWORD limits;
504 BOOL min;
506 TRACE ("%d\n", number);
507 if ((number > infoPtr->nrFields) || (number < 0)) return;
509 if ((infoPtr->fieldspec[number] & DTHT_DATEFIELD) == 0) return;
511 switch (infoPtr->fieldspec[number]) {
512 case ONEDIGITYEAR:
513 case TWODIGITYEAR:
514 case FULLYEAR:
515 if (delta == INT_MIN)
516 date->wYear = 1752;
517 else if (delta == INT_MAX)
518 date->wYear = 9999;
519 else
520 date->wYear = max(min(date->wYear + delta, 9999), 1752);
522 if (date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
523 /* This can happen when moving away from a leap year. */
524 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear);
525 MONTHCAL_CalculateDayOfWeek(date, TRUE);
526 break;
527 case ONEDIGITMONTH:
528 case TWODIGITMONTH:
529 case THREECHARMONTH:
530 case FULLMONTH:
531 date->wMonth = wrap(date->wMonth, delta, 1, 12);
532 MONTHCAL_CalculateDayOfWeek(date, TRUE);
533 delta = 0;
534 /* fall through */
535 case ONEDIGITDAY:
536 case TWODIGITDAY:
537 case THREECHARDAY:
538 case FULLDAY:
539 date->wDay = wrap(date->wDay, delta, 1, MONTHCAL_MonthLength(date->wMonth, date->wYear));
540 MONTHCAL_CalculateDayOfWeek(date, TRUE);
541 break;
542 case ONELETTERAMPM:
543 case TWOLETTERAMPM:
544 delta *= 12;
545 /* fall through */
546 case ONEDIGIT12HOUR:
547 case TWODIGIT12HOUR:
548 case ONEDIGIT24HOUR:
549 case TWODIGIT24HOUR:
550 date->wHour = wrap(date->wHour, delta, 0, 23);
551 break;
552 case ONEDIGITMINUTE:
553 case TWODIGITMINUTE:
554 date->wMinute = wrap(date->wMinute, delta, 0, 59);
555 break;
556 case ONEDIGITSECOND:
557 case TWODIGITSECOND:
558 date->wSecond = wrap(date->wSecond, delta, 0, 59);
559 break;
560 case FORMATCALLBACK:
561 FIXME ("Not implemented\n");
562 break;
565 /* FYI: On 1752/9/14 the calendar changed and England and the
566 * American colonies changed to the Gregorian calendar. This change
567 * involved having September 14th follow September 2nd. So no date
568 * algorithm works before that date.
570 if (10000 * date->wYear + 100 * date->wMonth + date->wDay < 17520914) {
571 date->wYear = 1752;
572 date->wMonth = 9;
573 date->wDay = 14;
574 date->wSecond = 0;
575 date->wMinute = 0;
576 date->wHour = 0;
579 /* Ensure time is within bounds */
580 limits = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, 0, (LPARAM)range);
581 min = delta < 0;
583 if (limits & (min ? GDTR_MIN : GDTR_MAX))
585 int i = (min ? 0 : 1);
587 if (MONTHCAL_CompareSystemTime(date, &range[i]) == (min ? -1 : 1))
589 date->wYear = range[i].wYear;
590 date->wMonth = range[i].wMonth;
591 date->wDayOfWeek = range[i].wDayOfWeek;
592 date->wDay = range[i].wDay;
593 date->wHour = range[i].wHour;
594 date->wMinute = range[i].wMinute;
595 date->wSecond = range[i].wSecond;
596 date->wMilliseconds = range[i].wMilliseconds;
601 static int DATETIME_GetFieldWidth (const DATETIME_INFO *infoPtr, HDC hdc, int count)
603 /* fields are a fixed width, determined by the largest possible string */
604 /* presumably, these widths should be language dependent */
605 static const WCHAR fld_d1W[] = { '2', 0 };
606 static const WCHAR fld_d2W[] = { '2', '2', 0 };
607 static const WCHAR fld_d4W[] = { '2', '2', '2', '2', 0 };
608 static const WCHAR fld_am1[] = { 'A', 0 };
609 static const WCHAR fld_am2[] = { 'A', 'M', 0 };
610 int spec;
611 WCHAR buffer[80];
612 LPCWSTR bufptr;
613 SIZE size;
615 if (!infoPtr->fieldspec) return 0;
617 spec = infoPtr->fieldspec[count];
618 if (spec & DT_STRING) {
619 int txtlen = infoPtr->buflen[count];
621 if (txtlen > 79)
622 txtlen = 79;
623 memcpy (buffer, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
624 buffer[txtlen] = 0;
625 bufptr = buffer;
627 else {
628 switch (spec) {
629 case ONEDIGITDAY:
630 case ONEDIGIT12HOUR:
631 case ONEDIGIT24HOUR:
632 case ONEDIGITSECOND:
633 case ONEDIGITMINUTE:
634 case ONEDIGITMONTH:
635 case ONEDIGITYEAR:
636 /* these seem to use a two byte field */
637 case TWODIGITDAY:
638 case TWODIGIT12HOUR:
639 case TWODIGIT24HOUR:
640 case TWODIGITSECOND:
641 case TWODIGITMINUTE:
642 case TWODIGITMONTH:
643 case TWODIGITYEAR:
644 bufptr = fld_d2W;
645 break;
646 case INVALIDFULLYEAR:
647 case FULLYEAR:
648 bufptr = fld_d4W;
649 break;
650 case THREECHARMONTH:
651 case FULLMONTH:
652 case THREECHARDAY:
653 case FULLDAY:
655 static const WCHAR fld_day[] = {'W','e','d','n','e','s','d','a','y',0};
656 static const WCHAR fld_abbrday[] = {'W','e','d',0};
657 static const WCHAR fld_mon[] = {'S','e','p','t','e','m','b','e','r',0};
658 static const WCHAR fld_abbrmon[] = {'D','e','c',0};
660 const WCHAR *fall;
661 LCTYPE lctype;
662 INT i, max_count;
663 LONG cx;
665 /* choose locale data type and fallback string */
666 switch (spec) {
667 case THREECHARDAY:
668 fall = fld_abbrday;
669 lctype = LOCALE_SABBREVDAYNAME1;
670 max_count = 7;
671 break;
672 case FULLDAY:
673 fall = fld_day;
674 lctype = LOCALE_SDAYNAME1;
675 max_count = 7;
676 break;
677 case THREECHARMONTH:
678 fall = fld_abbrmon;
679 lctype = LOCALE_SABBREVMONTHNAME1;
680 max_count = 12;
681 break;
682 default: /* FULLMONTH */
683 fall = fld_mon;
684 lctype = LOCALE_SMONTHNAME1;
685 max_count = 12;
686 break;
689 cx = 0;
690 for (i = 0; i < max_count; i++)
692 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, lctype + i,
693 buffer, ARRAY_SIZE(buffer)))
695 GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &size);
696 if (size.cx > cx) cx = size.cx;
698 else /* locale independent fallback on failure */
700 GetTextExtentPoint32W(hdc, fall, lstrlenW(fall), &size);
701 cx = size.cx;
702 break;
705 return cx;
707 case ONELETTERAMPM:
708 bufptr = fld_am1;
709 break;
710 case TWOLETTERAMPM:
711 bufptr = fld_am2;
712 break;
713 default:
714 bufptr = fld_d1W;
715 break;
718 GetTextExtentPoint32W (hdc, bufptr, strlenW(bufptr), &size);
719 return size.cx;
722 static void
723 DATETIME_Refresh (DATETIME_INFO *infoPtr, HDC hdc)
725 TRACE("\n");
727 if (infoPtr->dateValid) {
728 int i, prevright;
729 RECT *field;
730 RECT *rcDraw = &infoPtr->rcDraw;
731 SIZE size;
732 COLORREF oldTextColor;
733 HFONT oldFont = SelectObject (hdc, infoPtr->hFont);
734 INT oldBkMode = SetBkMode (hdc, TRANSPARENT);
735 WCHAR txt[80];
737 DATETIME_ReturnTxt (infoPtr, 0, txt, ARRAY_SIZE(txt));
738 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
739 rcDraw->bottom = size.cy + 2;
741 prevright = infoPtr->checkbox.right = ((infoPtr->dwStyle & DTS_SHOWNONE) ? 18 : 2);
743 for (i = 0; i < infoPtr->nrFields; i++) {
744 DATETIME_ReturnTxt (infoPtr, i, txt, ARRAY_SIZE(txt));
745 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
746 field = &infoPtr->fieldRect[i];
747 field->left = prevright;
748 field->right = prevright + DATETIME_GetFieldWidth (infoPtr, hdc, i);
749 field->top = rcDraw->top;
750 field->bottom = rcDraw->bottom;
751 prevright = field->right;
753 if (infoPtr->dwStyle & WS_DISABLED)
754 oldTextColor = SetTextColor (hdc, comctl32_color.clrGrayText);
755 else if ((infoPtr->haveFocus) && (i == infoPtr->select)) {
756 RECT selection;
758 /* fill if focused */
759 HBRUSH hbr = CreateSolidBrush (comctl32_color.clrActiveCaption);
761 if (infoPtr->nCharsEntered)
763 memcpy(txt, infoPtr->charsEntered, infoPtr->nCharsEntered * sizeof(WCHAR));
764 txt[infoPtr->nCharsEntered] = 0;
765 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
768 SetRect(&selection, 0, 0, size.cx, size.cy);
769 /* center rectangle */
770 OffsetRect(&selection, (field->right + field->left - size.cx)/2,
771 (field->bottom - size.cy)/2);
773 FillRect(hdc, &selection, hbr);
774 DeleteObject (hbr);
775 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindow);
777 else
778 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindowText);
780 /* draw the date text using the colour set above */
781 DrawTextW (hdc, txt, strlenW(txt), field, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
782 SetTextColor (hdc, oldTextColor);
784 SetBkMode (hdc, oldBkMode);
785 SelectObject (hdc, oldFont);
788 if (!(infoPtr->dwStyle & DTS_UPDOWN)) {
789 DrawFrameControl(hdc, &infoPtr->calbutton, DFC_SCROLL,
790 DFCS_SCROLLDOWN | (infoPtr->bCalDepressed ? DFCS_PUSHED : 0) |
791 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
796 static INT
797 DATETIME_HitTest (const DATETIME_INFO *infoPtr, POINT pt)
799 int i;
801 TRACE ("%s\n", wine_dbgstr_point(&pt));
803 if (PtInRect (&infoPtr->calbutton, pt)) return DTHT_MCPOPUP;
804 if (PtInRect (&infoPtr->checkbox, pt)) return DTHT_CHECKBOX;
806 for (i = 0; i < infoPtr->nrFields; i++) {
807 if (PtInRect (&infoPtr->fieldRect[i], pt)) return i;
810 return DTHT_NONE;
813 /* Returns index of the nearest preceding date field from given,
814 or -1 if none was found */
815 static int DATETIME_GetPrevDateField(const DATETIME_INFO *infoPtr, int i)
817 for(--i; i >= 0; i--)
819 if (infoPtr->fieldspec[i] & DTHT_DATEFIELD) return i;
821 return -1;
824 static void
825 DATETIME_ApplySelectedField (DATETIME_INFO *infoPtr)
827 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
828 int i, val = 0;
829 BOOL clamp_day = FALSE;
830 SYSTEMTIME date = infoPtr->date;
831 int oldyear;
833 if (infoPtr->select == -1 || infoPtr->nCharsEntered == 0)
834 return;
836 if ((infoPtr->fieldspec[fieldNum] == ONELETTERAMPM) ||
837 (infoPtr->fieldspec[fieldNum] == TWOLETTERAMPM))
838 val = infoPtr->charsEntered[0];
839 else {
840 for (i=0; i<infoPtr->nCharsEntered; i++)
841 val = val * 10 + infoPtr->charsEntered[i] - '0';
844 infoPtr->nCharsEntered = 0;
846 switch (infoPtr->fieldspec[fieldNum]) {
847 case ONEDIGITYEAR:
848 case TWODIGITYEAR:
849 oldyear = date.wYear;
850 date.wYear = date.wYear - (date.wYear%100) + val;
852 if (DATETIME_IsDateInValidRange(infoPtr, &date))
853 clamp_day = TRUE;
854 else
855 date.wYear = oldyear;
857 break;
858 case INVALIDFULLYEAR:
859 case FULLYEAR:
860 oldyear = date.wYear;
861 date.wYear = val;
863 if (DATETIME_IsDateInValidRange(infoPtr, &date))
864 clamp_day = TRUE;
865 else
866 date.wYear = oldyear;
868 break;
869 case ONEDIGITMONTH:
870 case TWODIGITMONTH:
871 date.wMonth = val;
872 clamp_day = TRUE;
873 break;
874 case ONEDIGITDAY:
875 case TWODIGITDAY:
876 date.wDay = val;
877 break;
878 case ONEDIGIT12HOUR:
879 case TWODIGIT12HOUR:
880 if (val >= 24)
881 val -= 20;
883 if (val >= 13)
884 date.wHour = val;
885 else if (val != 0) {
886 if (date.wHour >= 12) /* preserve current AM/PM state */
887 date.wHour = (val == 12 ? 12 : val + 12);
888 else
889 date.wHour = (val == 12 ? 0 : val);
891 break;
892 case ONEDIGIT24HOUR:
893 case TWODIGIT24HOUR:
894 date.wHour = val;
895 break;
896 case ONEDIGITMINUTE:
897 case TWODIGITMINUTE:
898 date.wMinute = val;
899 break;
900 case ONEDIGITSECOND:
901 case TWODIGITSECOND:
902 date.wSecond = val;
903 break;
904 case ONELETTERAMPM:
905 case TWOLETTERAMPM:
906 if (val == 'a' || val == 'A') {
907 if (date.wHour >= 12)
908 date.wHour -= 12;
909 } else if (val == 'p' || val == 'P') {
910 if (date.wHour < 12)
911 date.wHour += 12;
913 break;
916 if (clamp_day && date.wDay > MONTHCAL_MonthLength(date.wMonth, date.wYear))
917 date.wDay = MONTHCAL_MonthLength(date.wMonth, date.wYear);
919 if (DATETIME_SetSystemTime(infoPtr, GDT_VALID, &date))
920 DATETIME_SendDateTimeChangeNotify (infoPtr);
923 static void
924 DATETIME_SetSelectedField (DATETIME_INFO *infoPtr, int select)
926 DATETIME_ApplySelectedField(infoPtr);
928 infoPtr->select = select;
929 infoPtr->nCharsEntered = 0;
932 static LRESULT
933 DATETIME_LButtonDown (DATETIME_INFO *infoPtr, INT x, INT y)
935 POINT pt;
936 int new;
938 pt.x = x;
939 pt.y = y;
940 new = DATETIME_HitTest (infoPtr, pt);
942 SetFocus(infoPtr->hwndSelf);
944 if (!(new & DTHT_NODATEMASK) || (new == DTHT_NONE))
946 if (new == DTHT_NONE)
947 new = infoPtr->nrFields - 1;
948 else
950 /* hitting string part moves selection to next date field to left */
951 if (infoPtr->fieldspec[new] & DT_STRING)
953 new = DATETIME_GetPrevDateField(infoPtr, new);
954 if (new == -1) return 0;
956 /* never select full day of week */
957 if (infoPtr->fieldspec[new] == FULLDAY) return 0;
961 DATETIME_SetSelectedField(infoPtr, new);
963 if (infoPtr->select == DTHT_MCPOPUP) {
964 RECT rcMonthCal;
965 POINT pos;
966 SendMessageW(infoPtr->hMonthCal, MCM_GETMINREQRECT, 0, (LPARAM)&rcMonthCal);
968 /* FIXME: button actually is only depressed during dropdown of the */
969 /* calendar control and when the mouse is over the button window */
970 infoPtr->bCalDepressed = TRUE;
972 /* recalculate the position of the monthcal popup */
973 if(infoPtr->dwStyle & DTS_RIGHTALIGN)
974 pos.x = infoPtr->calbutton.left - (rcMonthCal.right - rcMonthCal.left);
975 else
976 /* FIXME: this should be after the area reserved for the checkbox */
977 pos.x = infoPtr->rcDraw.left;
979 pos.y = infoPtr->rcClient.bottom;
980 OffsetRect( &rcMonthCal, pos.x, pos.y );
981 MapWindowPoints( infoPtr->hwndSelf, 0, (POINT *)&rcMonthCal, 2 );
982 SetWindowPos(infoPtr->hMonthCal, 0, rcMonthCal.left, rcMonthCal.top,
983 rcMonthCal.right - rcMonthCal.left, rcMonthCal.bottom - rcMonthCal.top, 0);
985 if(IsWindowVisible(infoPtr->hMonthCal)) {
986 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
987 infoPtr->bDropdownEnabled = FALSE;
988 DATETIME_SendSimpleNotify (infoPtr, DTN_CLOSEUP);
989 } else {
990 const SYSTEMTIME *lprgSysTimeArray = &infoPtr->date;
991 TRACE("update calendar %04d/%02d/%02d\n",
992 lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay);
993 SendMessageW(infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
995 if (infoPtr->bDropdownEnabled) {
996 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
997 DATETIME_SendSimpleNotify (infoPtr, DTN_DROPDOWN);
999 infoPtr->bDropdownEnabled = TRUE;
1002 TRACE ("dt:%p mc:%p mc parent:%p, desktop:%p\n",
1003 infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hwndNotify, GetDesktopWindow ());
1006 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1008 return 0;
1012 static LRESULT
1013 DATETIME_LButtonUp (DATETIME_INFO *infoPtr)
1015 if(infoPtr->bCalDepressed) {
1016 infoPtr->bCalDepressed = FALSE;
1017 InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
1020 return 0;
1024 static LRESULT
1025 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
1027 if (!hdc) {
1028 PAINTSTRUCT ps;
1029 hdc = BeginPaint (infoPtr->hwndSelf, &ps);
1030 DATETIME_Refresh (infoPtr, hdc);
1031 EndPaint (infoPtr->hwndSelf, &ps);
1032 } else {
1033 DATETIME_Refresh (infoPtr, hdc);
1036 /* Not a click on the dropdown box, enabled it */
1037 infoPtr->bDropdownEnabled = TRUE;
1039 return 0;
1043 static LRESULT
1044 DATETIME_Button_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1046 if( HIWORD(wParam) == BN_CLICKED) {
1047 DWORD state = SendMessageW((HWND)lParam, BM_GETCHECK, 0, 0);
1048 infoPtr->dateValid = (state == BST_CHECKED);
1049 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1051 return 0;
1056 static LRESULT
1057 DATETIME_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1059 TRACE("hwndbutton = %p\n", infoPtr->hwndCheckbut);
1060 if(infoPtr->hwndCheckbut == (HWND)lParam)
1061 return DATETIME_Button_Command(infoPtr, wParam, lParam);
1062 return 0;
1066 static LRESULT
1067 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
1069 TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
1070 if (bEnable)
1071 infoPtr->dwStyle &= ~WS_DISABLED;
1072 else
1073 infoPtr->dwStyle |= WS_DISABLED;
1075 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1077 return 0;
1081 static LRESULT
1082 DATETIME_EraseBackground (const DATETIME_INFO *infoPtr, HDC hdc)
1084 HBRUSH hBrush, hSolidBrush = NULL;
1085 RECT rc;
1087 if (infoPtr->dwStyle & WS_DISABLED)
1088 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
1089 else
1091 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
1092 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
1093 if (!hBrush)
1094 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
1097 GetClientRect (infoPtr->hwndSelf, &rc);
1099 FillRect (hdc, &rc, hBrush);
1101 if (hSolidBrush)
1102 DeleteObject(hSolidBrush);
1104 return -1;
1108 static LRESULT
1109 DATETIME_Notify (DATETIME_INFO *infoPtr, const NMHDR *lpnmh)
1111 TRACE ("Got notification %x from %p\n", lpnmh->code, lpnmh->hwndFrom);
1112 TRACE ("info: %p %p %p\n", infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hUpdown);
1114 if (lpnmh->code == MCN_SELECT) {
1115 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1116 infoPtr->dateValid = TRUE;
1117 SendMessageW (infoPtr->hMonthCal, MCM_GETCURSEL, 0, (LPARAM)&infoPtr->date);
1118 TRACE("got from calendar %04d/%02d/%02d day of week %d\n",
1119 infoPtr->date.wYear, infoPtr->date.wMonth, infoPtr->date.wDay, infoPtr->date.wDayOfWeek);
1120 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
1121 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1122 DATETIME_SendDateTimeChangeNotify (infoPtr);
1123 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1125 if ((lpnmh->hwndFrom == infoPtr->hUpdown) && (lpnmh->code == UDN_DELTAPOS)) {
1126 const NM_UPDOWN *lpnmud = (const NM_UPDOWN*)lpnmh;
1127 TRACE("Delta pos %d\n", lpnmud->iDelta);
1128 infoPtr->pendingUpdown = lpnmud->iDelta;
1130 return 0;
1134 static LRESULT
1135 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode)
1137 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1138 int wrap = 0;
1139 int new;
1141 if (!(infoPtr->haveFocus)) return 0;
1142 if ((fieldNum==0) && (infoPtr->select)) return 0;
1144 if (infoPtr->select & FORMATCALLMASK) {
1145 FIXME ("Callbacks not implemented yet\n");
1148 switch (vkCode) {
1149 case VK_ADD:
1150 case VK_UP:
1151 infoPtr->nCharsEntered = 0;
1152 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1153 DATETIME_SendDateTimeChangeNotify (infoPtr);
1154 break;
1155 case VK_SUBTRACT:
1156 case VK_DOWN:
1157 infoPtr->nCharsEntered = 0;
1158 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1159 DATETIME_SendDateTimeChangeNotify (infoPtr);
1160 break;
1161 case VK_HOME:
1162 infoPtr->nCharsEntered = 0;
1163 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MIN);
1164 DATETIME_SendDateTimeChangeNotify (infoPtr);
1165 break;
1166 case VK_END:
1167 infoPtr->nCharsEntered = 0;
1168 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MAX);
1169 DATETIME_SendDateTimeChangeNotify (infoPtr);
1170 break;
1171 case VK_LEFT:
1172 new = infoPtr->select;
1173 do {
1174 if (new == 0) {
1175 new = new - 1;
1176 wrap++;
1177 } else {
1178 new--;
1180 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1181 if (new != infoPtr->select)
1182 DATETIME_SetSelectedField(infoPtr, new);
1183 break;
1184 case VK_RIGHT:
1185 new = infoPtr->select;
1186 do {
1187 new++;
1188 if (new==infoPtr->nrFields) {
1189 new = 0;
1190 wrap++;
1192 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1193 if (new != infoPtr->select)
1194 DATETIME_SetSelectedField(infoPtr, new);
1195 break;
1198 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1200 return 0;
1204 static LRESULT
1205 DATETIME_Char (DATETIME_INFO *infoPtr, WPARAM vkCode)
1207 int fieldNum, fieldSpec;
1209 fieldNum = infoPtr->select & DTHT_DATEFIELD;
1210 fieldSpec = infoPtr->fieldspec[fieldNum];
1212 if (fieldSpec == ONELETTERAMPM || fieldSpec == TWOLETTERAMPM) {
1213 infoPtr->charsEntered[0] = vkCode;
1214 infoPtr->nCharsEntered = 1;
1216 DATETIME_ApplySelectedField(infoPtr);
1217 } else if (vkCode >= '0' && vkCode <= '9') {
1218 int maxChars;
1220 infoPtr->charsEntered[infoPtr->nCharsEntered++] = vkCode;
1222 if (fieldSpec == INVALIDFULLYEAR || fieldSpec == FULLYEAR)
1223 maxChars = 4;
1224 else
1225 maxChars = 2;
1227 if ((fieldSpec == ONEDIGIT12HOUR ||
1228 fieldSpec == TWODIGIT12HOUR ||
1229 fieldSpec == ONEDIGIT24HOUR ||
1230 fieldSpec == TWODIGIT24HOUR) &&
1231 (infoPtr->nCharsEntered == 1))
1233 if (vkCode >= '3')
1234 maxChars = 1;
1237 if (maxChars == infoPtr->nCharsEntered)
1238 DATETIME_ApplySelectedField(infoPtr);
1241 return 0;
1245 static LRESULT
1246 DATETIME_VScroll (DATETIME_INFO *infoPtr, WORD wScroll)
1248 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1250 if ((SHORT)LOWORD(wScroll) != SB_THUMBPOSITION) return 0;
1251 if (!(infoPtr->haveFocus)) return 0;
1252 if ((fieldNum==0) && (infoPtr->select)) return 0;
1254 if (infoPtr->pendingUpdown >= 0) {
1255 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1256 DATETIME_SendDateTimeChangeNotify (infoPtr);
1258 else {
1259 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1260 DATETIME_SendDateTimeChangeNotify (infoPtr);
1263 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1265 return 0;
1269 static LRESULT
1270 DATETIME_KillFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1272 TRACE("lost focus to %p\n", lostFocus);
1274 if (infoPtr->haveFocus) {
1275 DATETIME_SendSimpleNotify (infoPtr, NM_KILLFOCUS);
1276 infoPtr->haveFocus = 0;
1277 DATETIME_SetSelectedField (infoPtr, -1);
1280 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
1282 return 0;
1286 static LRESULT
1287 DATETIME_NCCreate (HWND hwnd, const CREATESTRUCTW *lpcs)
1289 DWORD dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1290 /* force control to have client edge */
1291 dwExStyle |= WS_EX_CLIENTEDGE;
1292 SetWindowLongW(hwnd, GWL_EXSTYLE, dwExStyle);
1294 return DefWindowProcW(hwnd, WM_NCCREATE, 0, (LPARAM)lpcs);
1298 static LRESULT
1299 DATETIME_SetFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1301 TRACE("got focus from %p\n", lostFocus);
1303 /* if monthcal is open and it loses focus, close monthcal */
1304 if (infoPtr->hMonthCal && (lostFocus == infoPtr->hMonthCal) &&
1305 IsWindowVisible(infoPtr->hMonthCal))
1307 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1308 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1309 /* note: this get triggered even if monthcal loses focus to a dropdown
1310 * box click, which occurs without an intermediate WM_PAINT call
1312 infoPtr->bDropdownEnabled = FALSE;
1313 return 0;
1316 if (infoPtr->haveFocus == 0) {
1317 DATETIME_SendSimpleNotify (infoPtr, NM_SETFOCUS);
1318 infoPtr->haveFocus = DTHT_GOTFOCUS;
1321 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1323 return 0;
1327 static BOOL
1328 DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr)
1330 NMDATETIMECHANGE dtdtc;
1332 dtdtc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1333 dtdtc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1334 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1336 dtdtc.dwFlags = infoPtr->dateValid ? GDT_VALID : GDT_NONE;
1338 dtdtc.st = infoPtr->date;
1339 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1340 dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1344 static BOOL
1345 DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code)
1347 NMHDR nmhdr;
1349 TRACE("%x\n", code);
1350 nmhdr.hwndFrom = infoPtr->hwndSelf;
1351 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1352 nmhdr.code = code;
1354 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1355 nmhdr.idFrom, (LPARAM)&nmhdr);
1358 static LRESULT
1359 DATETIME_Size (DATETIME_INFO *infoPtr, INT width, INT height)
1361 /* set size */
1362 infoPtr->rcClient.bottom = height;
1363 infoPtr->rcClient.right = width;
1365 TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1367 infoPtr->rcDraw = infoPtr->rcClient;
1369 if (infoPtr->dwStyle & DTS_UPDOWN) {
1370 SetWindowPos(infoPtr->hUpdown, NULL,
1371 infoPtr->rcClient.right-14, 0,
1372 15, infoPtr->rcClient.bottom - infoPtr->rcClient.top,
1373 SWP_NOACTIVATE | SWP_NOZORDER);
1375 else {
1376 /* set the size of the button that drops the calendar down */
1377 /* FIXME: account for style that allows button on left side */
1378 infoPtr->calbutton.top = infoPtr->rcDraw.top;
1379 infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1380 infoPtr->calbutton.left = infoPtr->rcDraw.right-15;
1381 infoPtr->calbutton.right = infoPtr->rcDraw.right;
1384 /* set enable/disable button size for show none style being enabled */
1385 /* FIXME: these dimensions are completely incorrect */
1386 infoPtr->checkbox.top = infoPtr->rcDraw.top;
1387 infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1388 infoPtr->checkbox.left = infoPtr->rcDraw.left;
1389 infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1391 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1393 return 0;
1396 static LRESULT
1397 DATETIME_StyleChanging(DATETIME_INFO *infoPtr, WPARAM wStyleType, STYLESTRUCT *lpss)
1399 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1400 wStyleType, lpss->styleOld, lpss->styleNew);
1402 /* block DTS_SHOWNONE change */
1403 if ((lpss->styleNew ^ lpss->styleOld) & DTS_SHOWNONE)
1405 if (lpss->styleOld & DTS_SHOWNONE)
1406 lpss->styleNew |= DTS_SHOWNONE;
1407 else
1408 lpss->styleNew &= ~DTS_SHOWNONE;
1411 return 0;
1414 static LRESULT
1415 DATETIME_StyleChanged(DATETIME_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
1417 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1418 wStyleType, lpss->styleOld, lpss->styleNew);
1420 if (wStyleType != GWL_STYLE) return 0;
1422 infoPtr->dwStyle = lpss->styleNew;
1424 if ( !(lpss->styleOld & DTS_SHOWNONE) && (lpss->styleNew & DTS_SHOWNONE) ) {
1425 infoPtr->hwndCheckbut = CreateWindowExW (0, WC_BUTTONW, 0, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1426 2, 2, 13, 13, infoPtr->hwndSelf, 0,
1427 (HINSTANCE)GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_HINSTANCE), 0);
1428 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, infoPtr->dateValid ? 1 : 0, 0);
1430 if ( (lpss->styleOld & DTS_SHOWNONE) && !(lpss->styleNew & DTS_SHOWNONE) ) {
1431 DestroyWindow(infoPtr->hwndCheckbut);
1432 infoPtr->hwndCheckbut = 0;
1434 if ( !(lpss->styleOld & DTS_UPDOWN) && (lpss->styleNew & DTS_UPDOWN) ) {
1435 infoPtr->hUpdown = CreateUpDownControl (WS_CHILD | WS_BORDER | WS_VISIBLE, 120, 1, 20, 20,
1436 infoPtr->hwndSelf, 1, 0, 0, UD_MAXVAL, UD_MINVAL, 0);
1438 if ( (lpss->styleOld & DTS_UPDOWN) && !(lpss->styleNew & DTS_UPDOWN) ) {
1439 DestroyWindow(infoPtr->hUpdown);
1440 infoPtr->hUpdown = 0;
1443 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1444 return 0;
1447 static BOOL DATETIME_GetIdealSize(DATETIME_INFO *infoPtr, SIZE *size)
1449 SIZE field_size;
1450 RECT rect;
1451 WCHAR txt[80];
1452 HDC hdc;
1453 HFONT oldFont;
1454 int i;
1456 size->cx = size->cy = 0;
1458 hdc = GetDC(infoPtr->hwndSelf);
1459 oldFont = SelectObject(hdc, infoPtr->hFont);
1461 /* Get text font height */
1462 DATETIME_ReturnTxt(infoPtr, 0, txt, ARRAY_SIZE(txt));
1463 GetTextExtentPoint32W(hdc, txt, strlenW(txt), &field_size);
1464 size->cy = field_size.cy;
1466 /* Get text font width */
1467 for (i = 0; i < infoPtr->nrFields; i++)
1469 size->cx += DATETIME_GetFieldWidth(infoPtr, hdc, i);
1472 SelectObject(hdc, oldFont);
1473 ReleaseDC(infoPtr->hwndSelf, hdc);
1475 if (infoPtr->dwStyle & DTS_UPDOWN)
1477 GetWindowRect(infoPtr->hUpdown, &rect);
1478 size->cx += rect.right - rect.left;
1480 else
1482 size->cx += infoPtr->calbutton.right - infoPtr->calbutton.left;
1485 if (infoPtr->dwStyle & DTS_SHOWNONE)
1487 size->cx += infoPtr->checkbox.right - infoPtr->checkbox.left;
1490 /* Add space between controls for them not to get too close */
1491 size->cx += 12;
1492 size->cy += 4;
1494 TRACE("cx=%d cy=%d\n", size->cx, size->cy);
1495 return TRUE;
1498 static LRESULT
1499 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1501 infoPtr->hFont = font;
1502 if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1503 return 0;
1507 static LRESULT
1508 DATETIME_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1510 DATETIME_INFO *infoPtr = Alloc (sizeof(DATETIME_INFO));
1511 STYLESTRUCT ss = { 0, lpcs->style };
1513 if (!infoPtr) return -1;
1515 infoPtr->hwndSelf = hwnd;
1516 infoPtr->dwStyle = lpcs->style;
1518 infoPtr->nrFieldsAllocated = 32;
1519 infoPtr->fieldspec = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1520 infoPtr->fieldRect = Alloc (infoPtr->nrFieldsAllocated * sizeof(RECT));
1521 infoPtr->buflen = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1522 infoPtr->hwndNotify = lpcs->hwndParent;
1523 infoPtr->select = -1; /* initially, nothing is selected */
1524 infoPtr->bDropdownEnabled = TRUE;
1526 DATETIME_StyleChanged(infoPtr, GWL_STYLE, &ss);
1527 DATETIME_SetFormatW (infoPtr, 0);
1529 /* create the monthcal control */
1530 infoPtr->hMonthCal = CreateWindowExW (0, MONTHCAL_CLASSW, 0, WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS,
1531 0, 0, 0, 0, infoPtr->hwndSelf, 0, 0, 0);
1533 /* initialize info structure */
1534 GetLocalTime (&infoPtr->date);
1535 infoPtr->dateValid = TRUE;
1536 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1538 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1540 return 0;
1545 static LRESULT
1546 DATETIME_Destroy (DATETIME_INFO *infoPtr)
1548 if (infoPtr->hwndCheckbut)
1549 DestroyWindow(infoPtr->hwndCheckbut);
1550 if (infoPtr->hUpdown)
1551 DestroyWindow(infoPtr->hUpdown);
1552 if (infoPtr->hMonthCal)
1553 DestroyWindow(infoPtr->hMonthCal);
1554 SetWindowLongPtrW( infoPtr->hwndSelf, 0, 0 ); /* clear infoPtr */
1555 Free (infoPtr->buflen);
1556 Free (infoPtr->fieldRect);
1557 Free (infoPtr->fieldspec);
1558 Free (infoPtr);
1559 return 0;
1563 static INT
1564 DATETIME_GetText (const DATETIME_INFO *infoPtr, INT count, LPWSTR dst)
1566 WCHAR buf[80];
1567 int i;
1569 if (!dst || (count <= 0)) return 0;
1571 dst[0] = 0;
1572 for (i = 0; i < infoPtr->nrFields; i++)
1574 DATETIME_ReturnTxt(infoPtr, i, buf, ARRAY_SIZE(buf));
1575 if ((strlenW(dst) + strlenW(buf)) < count)
1576 strcatW(dst, buf);
1577 else break;
1579 return strlenW(dst);
1583 static LRESULT WINAPI
1584 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1586 DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1588 TRACE ("%x, %lx, %lx\n", uMsg, wParam, lParam);
1590 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
1591 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
1593 switch (uMsg) {
1595 case DTM_GETSYSTEMTIME:
1596 return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1598 case DTM_SETSYSTEMTIME:
1599 return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1601 case DTM_GETRANGE:
1602 return SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1604 case DTM_SETRANGE:
1605 return SendMessageW (infoPtr->hMonthCal, MCM_SETRANGE, wParam, lParam);
1607 case DTM_SETFORMATA:
1608 return DATETIME_SetFormatA (infoPtr, (LPCSTR)lParam);
1610 case DTM_SETFORMATW:
1611 return DATETIME_SetFormatW (infoPtr, (LPCWSTR)lParam);
1613 case DTM_GETMONTHCAL:
1614 return (LRESULT)infoPtr->hMonthCal;
1616 case DTM_SETMCCOLOR:
1617 return SendMessageW (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
1619 case DTM_GETMCCOLOR:
1620 return SendMessageW (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
1622 case DTM_SETMCFONT:
1623 return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1625 case DTM_GETMCFONT:
1626 return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1628 case DTM_GETIDEALSIZE:
1629 return DATETIME_GetIdealSize(infoPtr, (SIZE *)lParam);
1631 case WM_NOTIFY:
1632 return DATETIME_Notify (infoPtr, (LPNMHDR)lParam);
1634 case WM_ENABLE:
1635 return DATETIME_Enable (infoPtr, (BOOL)wParam);
1637 case WM_ERASEBKGND:
1638 return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1640 case WM_GETDLGCODE:
1641 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1643 case WM_PRINTCLIENT:
1644 case WM_PAINT:
1645 return DATETIME_Paint (infoPtr, (HDC)wParam);
1647 case WM_KEYDOWN:
1648 return DATETIME_KeyDown (infoPtr, wParam);
1650 case WM_CHAR:
1651 return DATETIME_Char (infoPtr, wParam);
1653 case WM_KILLFOCUS:
1654 return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1656 case WM_NCCREATE:
1657 return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1659 case WM_SETFOCUS:
1660 return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1662 case WM_SIZE:
1663 return DATETIME_Size (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1665 case WM_LBUTTONDOWN:
1666 return DATETIME_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1668 case WM_LBUTTONUP:
1669 return DATETIME_LButtonUp (infoPtr);
1671 case WM_VSCROLL:
1672 return DATETIME_VScroll (infoPtr, (WORD)wParam);
1674 case WM_CREATE:
1675 return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1677 case WM_DESTROY:
1678 return DATETIME_Destroy (infoPtr);
1680 case WM_COMMAND:
1681 return DATETIME_Command (infoPtr, wParam, lParam);
1683 case WM_STYLECHANGING:
1684 return DATETIME_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1686 case WM_STYLECHANGED:
1687 return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1689 case WM_SETFONT:
1690 return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1692 case WM_GETFONT:
1693 return (LRESULT) infoPtr->hFont;
1695 case WM_GETTEXT:
1696 return (LRESULT) DATETIME_GetText(infoPtr, wParam, (LPWSTR)lParam);
1698 case WM_SETTEXT:
1699 return CB_ERR;
1701 default:
1702 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
1703 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
1704 uMsg, wParam, lParam);
1705 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1710 void
1711 DATETIME_Register (void)
1713 WNDCLASSW wndClass;
1715 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1716 wndClass.style = CS_GLOBALCLASS;
1717 wndClass.lpfnWndProc = DATETIME_WindowProc;
1718 wndClass.cbClsExtra = 0;
1719 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1720 wndClass.hCursor = LoadCursorW (0, (LPCWSTR)IDC_ARROW);
1721 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1722 wndClass.lpszClassName = DATETIMEPICK_CLASSW;
1724 RegisterClassW (&wndClass);
1728 void
1729 DATETIME_Unregister (void)
1731 UnregisterClassW (DATETIMEPICK_CLASSW, NULL);