winewayland.drv: Implement vkGetPhysicalDeviceSurfaceSupportKHR.
[wine.git] / dlls / comctl32 / datetime.c
blob393406e778bd9372dddcf4e0150c69c2bd6d7227
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 "uxtheme.h"
47 #include "vsstyle.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(datetime);
52 typedef struct
54 HWND hwndSelf;
55 HWND hMonthCal;
56 HWND hwndNotify;
57 HWND hUpdown;
58 DWORD dwStyle;
59 SYSTEMTIME date;
60 BOOL dateValid;
61 HWND hwndCheckbut;
62 RECT rcClient; /* rect around the edge of the window */
63 RECT rcDraw; /* rect inside of the border */
64 RECT checkbox; /* checkbox allowing the control to be enabled/disabled */
65 RECT calbutton; /* button that toggles the dropdown of the monthcal control */
66 BOOL bCalDepressed; /* TRUE = cal button is depressed */
67 BOOL bCalHot; /* TRUE if calendar button is hovered */
68 BOOL bDropdownEnabled;
69 int select;
70 WCHAR charsEntered[4];
71 int nCharsEntered;
72 HFONT hFont;
73 int nrFieldsAllocated;
74 int nrFields;
75 int haveFocus;
76 int *fieldspec;
77 RECT *fieldRect;
78 int *buflen;
79 WCHAR textbuf[256];
80 POINT monthcal_pos;
81 int pendingUpdown;
82 } DATETIME_INFO, *LPDATETIME_INFO;
84 /* this list of defines is closely related to `allowedformatchars' defined
85 * in datetime.c; the high nibble indicates the `base type' of the format
86 * specifier.
87 * Do not change without first reading DATETIME_UseFormat.
91 #define DT_END_FORMAT 0
92 #define ONEDIGITDAY 0x01
93 #define TWODIGITDAY 0x02
94 #define THREECHARDAY 0x03
95 #define FULLDAY 0x04
96 #define ONEDIGIT12HOUR 0x11
97 #define TWODIGIT12HOUR 0x12
98 #define ONEDIGIT24HOUR 0x21
99 #define TWODIGIT24HOUR 0x22
100 #define ONEDIGITMINUTE 0x31
101 #define TWODIGITMINUTE 0x32
102 #define ONEDIGITMONTH 0x41
103 #define TWODIGITMONTH 0x42
104 #define THREECHARMONTH 0x43
105 #define FULLMONTH 0x44
106 #define ONEDIGITSECOND 0x51
107 #define TWODIGITSECOND 0x52
108 #define ONELETTERAMPM 0x61
109 #define TWOLETTERAMPM 0x62
110 #define ONEDIGITYEAR 0x71
111 #define TWODIGITYEAR 0x72
112 #define INVALIDFULLYEAR 0x73 /* FIXME - yyy is not valid - we'll treat it as yyyy */
113 #define FULLYEAR 0x74
114 #define FORMATCALLBACK 0x81 /* -> maximum of 0x80 callbacks possible */
115 #define FORMATCALLMASK 0x80
116 #define DT_STRING 0x0100
118 #define DTHT_DATEFIELD 0xff /* for hit-testing */
120 #define DTHT_NONE 0x1000
121 #define DTHT_CHECKBOX 0x2000 /* these should end at '00' , to make */
122 #define DTHT_MCPOPUP 0x3000 /* & DTHT_DATEFIELD 0 when DATETIME_KeyDown */
123 #define DTHT_GOTFOCUS 0x4000 /* tests for date-fields */
124 #define DTHT_NODATEMASK 0xf000 /* to mask check and drop down from others */
126 static BOOL DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code);
127 static BOOL DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr);
128 static const WCHAR allowedformatchars[] = L"dhHmMstyX";
129 static const int maxrepetition [] = {4,2,2,2,4,2,2,4,-1};
130 static const WCHAR *themeClass = WC_SCROLLBARW;
132 /* valid date limits */
133 static const SYSTEMTIME max_allowed_date = { .wYear = 9999, .wMonth = 12, .wDayOfWeek = 0, .wDay = 31 };
134 static const SYSTEMTIME min_allowed_date = { .wYear = 1752, .wMonth = 9, .wDayOfWeek = 0, .wDay = 14 };
136 static DWORD
137 DATETIME_GetSystemTime (const DATETIME_INFO *infoPtr, SYSTEMTIME *systime)
139 if (!systime) return GDT_NONE;
141 if ((infoPtr->dwStyle & DTS_SHOWNONE) &&
142 (SendMessageW (infoPtr->hwndCheckbut, BM_GETCHECK, 0, 0) == BST_UNCHECKED))
143 return GDT_NONE;
145 *systime = infoPtr->date;
147 return GDT_VALID;
150 /* Checks value is within configured date range
152 * PARAMETERS
154 * [I] infoPtr : valid pointer to control data
155 * [I] date : pointer to valid date data to check
157 * RETURN VALUE
159 * TRUE - date within configured range
160 * FALSE - date is outside configured range
162 static BOOL DATETIME_IsDateInValidRange(const DATETIME_INFO *infoPtr, const SYSTEMTIME *date)
164 SYSTEMTIME range[2];
165 DWORD limits;
167 if ((MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) ||
168 (MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1))
169 return FALSE;
171 limits = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, 0, (LPARAM)range);
173 if (limits & GDTR_MAX)
175 if (MONTHCAL_CompareSystemTime(date, &range[1]) == 1)
176 return FALSE;
179 if (limits & GDTR_MIN)
181 if (MONTHCAL_CompareSystemTime(date, &range[0]) == -1)
182 return FALSE;
185 return TRUE;
188 static BOOL
189 DATETIME_SetSystemTime (DATETIME_INFO *infoPtr, DWORD flag, const SYSTEMTIME *systime)
191 if (!systime) return FALSE;
193 TRACE("%04d/%02d/%02d %02d:%02d:%02d\n",
194 systime->wYear, systime->wMonth, systime->wDay,
195 systime->wHour, systime->wMinute, systime->wSecond);
197 if (flag == GDT_VALID) {
198 if (systime->wYear == 0 ||
199 systime->wMonth < 1 || systime->wMonth > 12 ||
200 systime->wDay < 1 ||
201 systime->wDay > MONTHCAL_MonthLength(systime->wMonth, systime->wYear) ||
202 systime->wHour > 23 ||
203 systime->wMinute > 59 ||
204 systime->wSecond > 59 ||
205 systime->wMilliseconds > 999
207 return FALSE;
209 /* Windows returns true if the date is valid but outside the limits set */
210 if (!DATETIME_IsDateInValidRange(infoPtr, systime))
211 return TRUE;
213 infoPtr->dateValid = TRUE;
214 infoPtr->date = *systime;
215 /* always store a valid day of week */
216 MONTHCAL_CalculateDayOfWeek(&infoPtr->date, TRUE);
218 SendMessageW (infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
219 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
220 } else if ((infoPtr->dwStyle & DTS_SHOWNONE) && (flag == GDT_NONE)) {
221 infoPtr->dateValid = FALSE;
222 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_UNCHECKED, 0);
224 else
225 return FALSE;
227 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
228 return TRUE;
232 /***
233 * Split up a formattxt in actions.
234 * See ms documentation for the meaning of the letter codes/'specifiers'.
236 * Notes:
237 * *'dddddd' is handled as 'dddd' plus 'dd'.
238 * *unrecognized formats are strings (here given the type DT_STRING;
239 * start of the string is encoded in lower bits of DT_STRING.
240 * Therefore, 'string' ends up as '<show seconds>tring'.
243 static void
244 DATETIME_UseFormat (DATETIME_INFO *infoPtr, LPCWSTR formattxt)
246 unsigned int i;
247 int j, k, len;
248 BOOL inside_literal = FALSE; /* inside '...' */
249 int *nrFields = &infoPtr->nrFields;
251 *nrFields = 0;
252 infoPtr->fieldspec[*nrFields] = 0;
253 len = lstrlenW(allowedformatchars);
254 k = 0;
256 for (i = 0; formattxt[i]; i++) {
257 TRACE ("\n%d %c:", i, formattxt[i]);
258 if (!inside_literal) {
259 for (j = 0; j < len; j++) {
260 if (allowedformatchars[j]==formattxt[i]) {
261 TRACE ("%c[%d,%x]", allowedformatchars[j], *nrFields, infoPtr->fieldspec[*nrFields]);
262 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
263 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
264 break;
266 if (infoPtr->fieldspec[*nrFields] >> 4 != j) {
267 (*nrFields)++;
268 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
269 break;
271 if ((infoPtr->fieldspec[*nrFields] & 0x0f) == maxrepetition[j]) {
272 (*nrFields)++;
273 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
274 break;
276 infoPtr->fieldspec[*nrFields]++;
277 break;
278 } /* if allowedformatchar */
279 } /* for j */
281 else
282 j = len;
284 if (formattxt[i] == '\'')
286 inside_literal = !inside_literal;
287 continue;
290 /* char is not a specifier: handle char like a string */
291 if (j == len) {
292 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
293 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
294 infoPtr->buflen[*nrFields] = 0;
295 } else if ((infoPtr->fieldspec[*nrFields] & DT_STRING) != DT_STRING) {
296 (*nrFields)++;
297 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
298 infoPtr->buflen[*nrFields] = 0;
300 infoPtr->textbuf[k] = formattxt[i];
301 k++;
302 infoPtr->buflen[*nrFields]++;
303 } /* if j=len */
305 if (*nrFields == infoPtr->nrFieldsAllocated) {
306 FIXME ("out of memory; should reallocate. crash ahead.\n");
308 } /* for i */
310 TRACE("\n");
312 if (infoPtr->fieldspec[*nrFields] != 0) (*nrFields)++;
316 static BOOL
317 DATETIME_SetFormatW (DATETIME_INFO *infoPtr, LPCWSTR format)
319 WCHAR format_buf[80];
321 if (!format) {
322 DWORD format_item;
324 if ((infoPtr->dwStyle & DTS_SHORTDATECENTURYFORMAT) == DTS_SHORTDATECENTURYFORMAT)
325 format_item = LOCALE_SSHORTDATE;
326 else if ((infoPtr->dwStyle & DTS_LONGDATEFORMAT) == DTS_LONGDATEFORMAT)
327 format_item = LOCALE_SLONGDATE;
328 else if ((infoPtr->dwStyle & DTS_TIMEFORMAT) == DTS_TIMEFORMAT)
329 format_item = LOCALE_STIMEFORMAT;
330 else /* DTS_SHORTDATEFORMAT */
331 format_item = LOCALE_SSHORTDATE;
332 GetLocaleInfoW(LOCALE_USER_DEFAULT, format_item, format_buf, ARRAY_SIZE(format_buf));
333 format = format_buf;
336 DATETIME_UseFormat (infoPtr, format);
337 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
339 return TRUE;
343 static BOOL
344 DATETIME_SetFormatA (DATETIME_INFO *infoPtr, LPCSTR lpszFormat)
346 if (lpszFormat) {
347 BOOL retval;
348 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, NULL, 0);
349 LPWSTR wstr = Alloc(len * sizeof(WCHAR));
350 if (wstr) MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, wstr, len);
351 retval = DATETIME_SetFormatW (infoPtr, wstr);
352 Free (wstr);
353 return retval;
355 else
356 return DATETIME_SetFormatW (infoPtr, 0);
361 static void
362 DATETIME_ReturnTxt (const DATETIME_INFO *infoPtr, int count, LPWSTR result, int resultSize)
364 SYSTEMTIME date = infoPtr->date;
365 int spec;
366 WCHAR buffer[80];
368 *result=0;
369 TRACE ("%d,%d\n", infoPtr->nrFields, count);
370 if (count>infoPtr->nrFields || count < 0) {
371 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
372 return;
375 if (!infoPtr->fieldspec) return;
377 spec = infoPtr->fieldspec[count];
378 if (spec & DT_STRING) {
379 int txtlen = infoPtr->buflen[count];
381 if (txtlen > resultSize)
382 txtlen = resultSize - 1;
383 memcpy (result, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
384 result[txtlen] = 0;
385 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
386 return;
390 switch (spec) {
391 case DT_END_FORMAT:
392 *result = 0;
393 break;
394 case ONEDIGITDAY:
395 wsprintfW (result, L"%d", date.wDay);
396 break;
397 case TWODIGITDAY:
398 wsprintfW (result, L"%.2d", date.wDay);
399 break;
400 case THREECHARDAY:
401 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1+(date.wDayOfWeek+6)%7, result, 4);
402 /*wsprintfW (result,"%.3s",days[date.wDayOfWeek]);*/
403 break;
404 case FULLDAY:
405 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME1+(date.wDayOfWeek+6)%7, result, resultSize);
406 break;
407 case ONEDIGIT12HOUR:
408 if (date.wHour == 0) {
409 result[0] = '1';
410 result[1] = '2';
411 result[2] = 0;
413 else
414 wsprintfW (result, L"%d", date.wHour - (date.wHour > 12 ? 12 : 0));
415 break;
416 case TWODIGIT12HOUR:
417 if (date.wHour == 0) {
418 result[0] = '1';
419 result[1] = '2';
420 result[2] = 0;
422 else
423 wsprintfW (result, L"%.2d", date.wHour - (date.wHour > 12 ? 12 : 0));
424 break;
425 case ONEDIGIT24HOUR:
426 wsprintfW (result, L"%d", date.wHour);
427 break;
428 case TWODIGIT24HOUR:
429 wsprintfW (result, L"%.2d", date.wHour);
430 break;
431 case ONEDIGITSECOND:
432 wsprintfW (result, L"%d", date.wSecond);
433 break;
434 case TWODIGITSECOND:
435 wsprintfW (result, L"%.2d", date.wSecond);
436 break;
437 case ONEDIGITMINUTE:
438 wsprintfW (result, L"%d", date.wMinute);
439 break;
440 case TWODIGITMINUTE:
441 wsprintfW (result, L"%.2d", date.wMinute);
442 break;
443 case ONEDIGITMONTH:
444 wsprintfW (result, L"%d", date.wMonth);
445 break;
446 case TWODIGITMONTH:
447 wsprintfW (result, L"%.2d", date.wMonth);
448 break;
449 case THREECHARMONTH:
450 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+date.wMonth -1, buffer, ARRAY_SIZE(buffer));
451 wsprintfW (result, L"%s.3s", buffer);
452 break;
453 case FULLMONTH:
454 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+date.wMonth -1,
455 result, resultSize);
456 break;
457 case ONELETTERAMPM:
458 result[0] = (date.wHour < 12 ? 'A' : 'P');
459 result[1] = 0;
460 break;
461 case TWOLETTERAMPM:
462 result[0] = (date.wHour < 12 ? 'A' : 'P');
463 result[1] = 'M';
464 result[2] = 0;
465 break;
466 case FORMATCALLBACK:
467 FIXME ("Not implemented\n");
468 result[0] = 'x';
469 result[1] = 0;
470 break;
471 case ONEDIGITYEAR:
472 wsprintfW (result, L"%d", date.wYear % 10);
473 break;
474 case TWODIGITYEAR:
475 wsprintfW (result, L"%.2d", date.wYear % 100);
476 break;
477 case INVALIDFULLYEAR:
478 case FULLYEAR:
479 wsprintfW (result, L"%d", date.wYear);
480 break;
483 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
486 static int wrap(int val, int delta, int minVal, int maxVal)
488 val += delta;
489 if (delta == INT_MIN || val < minVal) return maxVal;
490 if (delta == INT_MAX || val > maxVal) return minVal;
491 return val;
494 static void
495 DATETIME_IncreaseField (DATETIME_INFO *infoPtr, int number, int delta)
497 SYSTEMTIME *date = &infoPtr->date;
498 SYSTEMTIME range[2];
499 DWORD limits;
500 BOOL min;
502 TRACE ("%d\n", number);
503 if ((number > infoPtr->nrFields) || (number < 0)) return;
505 if ((infoPtr->fieldspec[number] & DTHT_DATEFIELD) == 0) return;
507 switch (infoPtr->fieldspec[number]) {
508 case ONEDIGITYEAR:
509 case TWODIGITYEAR:
510 case FULLYEAR:
511 if (delta == INT_MIN)
512 date->wYear = 1752;
513 else if (delta == INT_MAX)
514 date->wYear = 9999;
515 else
516 date->wYear = max(min(date->wYear + delta, 9999), 1752);
518 if (date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
519 /* This can happen when moving away from a leap year. */
520 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear);
521 MONTHCAL_CalculateDayOfWeek(date, TRUE);
522 break;
523 case ONEDIGITMONTH:
524 case TWODIGITMONTH:
525 case THREECHARMONTH:
526 case FULLMONTH:
527 date->wMonth = wrap(date->wMonth, delta, 1, 12);
528 MONTHCAL_CalculateDayOfWeek(date, TRUE);
529 delta = 0;
530 /* fall through */
531 case ONEDIGITDAY:
532 case TWODIGITDAY:
533 case THREECHARDAY:
534 case FULLDAY:
535 date->wDay = wrap(date->wDay, delta, 1, MONTHCAL_MonthLength(date->wMonth, date->wYear));
536 MONTHCAL_CalculateDayOfWeek(date, TRUE);
537 break;
538 case ONELETTERAMPM:
539 case TWOLETTERAMPM:
540 delta *= 12;
541 /* fall through */
542 case ONEDIGIT12HOUR:
543 case TWODIGIT12HOUR:
544 case ONEDIGIT24HOUR:
545 case TWODIGIT24HOUR:
546 date->wHour = wrap(date->wHour, delta, 0, 23);
547 break;
548 case ONEDIGITMINUTE:
549 case TWODIGITMINUTE:
550 date->wMinute = wrap(date->wMinute, delta, 0, 59);
551 break;
552 case ONEDIGITSECOND:
553 case TWODIGITSECOND:
554 date->wSecond = wrap(date->wSecond, delta, 0, 59);
555 break;
556 case FORMATCALLBACK:
557 FIXME ("Not implemented\n");
558 break;
561 /* FYI: On 1752/9/14 the calendar changed and England and the
562 * American colonies changed to the Gregorian calendar. This change
563 * involved having September 14th follow September 2nd. So no date
564 * algorithm works before that date.
566 if (10000 * date->wYear + 100 * date->wMonth + date->wDay < 17520914) {
567 date->wYear = 1752;
568 date->wMonth = 9;
569 date->wDay = 14;
570 date->wSecond = 0;
571 date->wMinute = 0;
572 date->wHour = 0;
575 /* Ensure time is within bounds */
576 limits = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, 0, (LPARAM)range);
577 min = delta < 0;
579 if (limits & (min ? GDTR_MIN : GDTR_MAX))
581 int i = (min ? 0 : 1);
583 if (MONTHCAL_CompareSystemTime(date, &range[i]) == (min ? -1 : 1))
585 date->wYear = range[i].wYear;
586 date->wMonth = range[i].wMonth;
587 date->wDayOfWeek = range[i].wDayOfWeek;
588 date->wDay = range[i].wDay;
589 date->wHour = range[i].wHour;
590 date->wMinute = range[i].wMinute;
591 date->wSecond = range[i].wSecond;
592 date->wMilliseconds = range[i].wMilliseconds;
597 static int DATETIME_GetFieldWidth (const DATETIME_INFO *infoPtr, HDC hdc, int count)
599 /* fields are a fixed width, determined by the largest possible string */
600 /* presumably, these widths should be language dependent */
601 int spec;
602 WCHAR buffer[80];
603 LPCWSTR bufptr;
604 SIZE size;
606 if (!infoPtr->fieldspec) return 0;
608 spec = infoPtr->fieldspec[count];
609 if (spec & DT_STRING) {
610 int txtlen = infoPtr->buflen[count];
612 if (txtlen > 79)
613 txtlen = 79;
614 memcpy (buffer, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
615 buffer[txtlen] = 0;
616 bufptr = buffer;
618 else {
619 switch (spec) {
620 case ONEDIGITDAY:
621 case ONEDIGIT12HOUR:
622 case ONEDIGIT24HOUR:
623 case ONEDIGITSECOND:
624 case ONEDIGITMINUTE:
625 case ONEDIGITMONTH:
626 case ONEDIGITYEAR:
627 /* these seem to use a two byte field */
628 case TWODIGITDAY:
629 case TWODIGIT12HOUR:
630 case TWODIGIT24HOUR:
631 case TWODIGITSECOND:
632 case TWODIGITMINUTE:
633 case TWODIGITMONTH:
634 case TWODIGITYEAR:
635 bufptr = L"22";
636 break;
637 case INVALIDFULLYEAR:
638 case FULLYEAR:
639 bufptr = L"2222";
640 break;
641 case THREECHARMONTH:
642 case FULLMONTH:
643 case THREECHARDAY:
644 case FULLDAY:
646 const WCHAR *fall;
647 LCTYPE lctype;
648 INT i, max_count;
649 LONG cx;
651 /* choose locale data type and fallback string */
652 switch (spec) {
653 case THREECHARDAY:
654 fall = L"Wed";
655 lctype = LOCALE_SABBREVDAYNAME1;
656 max_count = 7;
657 break;
658 case FULLDAY:
659 fall = L"Wednesday";
660 lctype = LOCALE_SDAYNAME1;
661 max_count = 7;
662 break;
663 case THREECHARMONTH:
664 fall = L"Dec";
665 lctype = LOCALE_SABBREVMONTHNAME1;
666 max_count = 12;
667 break;
668 default: /* FULLMONTH */
669 fall = L"September";
670 lctype = LOCALE_SMONTHNAME1;
671 max_count = 12;
672 break;
675 cx = 0;
676 for (i = 0; i < max_count; i++)
678 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, lctype + i,
679 buffer, ARRAY_SIZE(buffer)))
681 GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &size);
682 if (size.cx > cx) cx = size.cx;
684 else /* locale independent fallback on failure */
686 GetTextExtentPoint32W(hdc, fall, lstrlenW(fall), &size);
687 cx = size.cx;
688 break;
691 return cx;
693 case ONELETTERAMPM:
694 bufptr = L"A";
695 break;
696 case TWOLETTERAMPM:
697 bufptr = L"AM";
698 break;
699 default:
700 bufptr = L"2";
701 break;
704 GetTextExtentPoint32W (hdc, bufptr, lstrlenW(bufptr), &size);
705 return size.cx;
708 static void
709 DATETIME_Refresh (DATETIME_INFO *infoPtr, HDC hdc)
711 HTHEME theme;
712 int state;
714 TRACE("\n");
716 if (infoPtr->dateValid) {
717 int i, prevright;
718 RECT *field;
719 RECT *rcDraw = &infoPtr->rcDraw;
720 SIZE size;
721 COLORREF oldTextColor;
722 HFONT oldFont = SelectObject (hdc, infoPtr->hFont);
723 INT oldBkMode = SetBkMode (hdc, TRANSPARENT);
724 WCHAR txt[80];
726 DATETIME_ReturnTxt (infoPtr, 0, txt, ARRAY_SIZE(txt));
727 GetTextExtentPoint32W (hdc, txt, lstrlenW(txt), &size);
728 rcDraw->bottom = size.cy + 2;
730 prevright = infoPtr->checkbox.right = ((infoPtr->dwStyle & DTS_SHOWNONE) ? 18 : 2);
732 for (i = 0; i < infoPtr->nrFields; i++) {
733 DATETIME_ReturnTxt (infoPtr, i, txt, ARRAY_SIZE(txt));
734 GetTextExtentPoint32W (hdc, txt, lstrlenW(txt), &size);
735 field = &infoPtr->fieldRect[i];
736 field->left = prevright;
737 field->right = prevright + DATETIME_GetFieldWidth (infoPtr, hdc, i);
738 field->top = rcDraw->top;
739 field->bottom = rcDraw->bottom;
740 prevright = field->right;
742 if (infoPtr->dwStyle & WS_DISABLED)
743 oldTextColor = SetTextColor (hdc, comctl32_color.clrGrayText);
744 else if ((infoPtr->haveFocus) && (i == infoPtr->select)) {
745 RECT selection;
747 /* fill if focused */
748 HBRUSH hbr = CreateSolidBrush (comctl32_color.clrActiveCaption);
750 if (infoPtr->nCharsEntered)
752 memcpy(txt, infoPtr->charsEntered, infoPtr->nCharsEntered * sizeof(WCHAR));
753 txt[infoPtr->nCharsEntered] = 0;
754 GetTextExtentPoint32W (hdc, txt, lstrlenW(txt), &size);
757 SetRect(&selection, 0, 0, size.cx, size.cy);
758 /* center rectangle */
759 OffsetRect(&selection, (field->right + field->left - size.cx)/2,
760 (field->bottom - size.cy)/2);
762 FillRect(hdc, &selection, hbr);
763 DeleteObject (hbr);
764 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindow);
766 else
767 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindowText);
769 /* draw the date text using the colour set above */
770 DrawTextW (hdc, txt, lstrlenW(txt), field, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
771 SetTextColor (hdc, oldTextColor);
773 SetBkMode (hdc, oldBkMode);
774 SelectObject (hdc, oldFont);
777 if (infoPtr->dwStyle & DTS_UPDOWN)
778 return;
780 theme = GetWindowTheme(infoPtr->hwndSelf);
781 if (theme)
783 if (infoPtr->dwStyle & WS_DISABLED)
784 state = ABS_DOWNDISABLED;
785 else if (infoPtr->bCalDepressed)
786 state = ABS_DOWNPRESSED;
787 else if (infoPtr->bCalHot)
788 state = ABS_DOWNHOT;
789 else
790 state = ABS_DOWNNORMAL;
792 DrawThemeBackground(theme, hdc, SBP_ARROWBTN, state, &infoPtr->calbutton, NULL);
794 else
796 DrawFrameControl(hdc, &infoPtr->calbutton, DFC_SCROLL,
797 DFCS_SCROLLDOWN | (infoPtr->bCalDepressed ? DFCS_PUSHED : 0) |
798 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
803 static INT
804 DATETIME_HitTest (const DATETIME_INFO *infoPtr, POINT pt)
806 int i;
808 TRACE ("%s\n", wine_dbgstr_point(&pt));
810 if (PtInRect (&infoPtr->calbutton, pt)) return DTHT_MCPOPUP;
811 if (PtInRect (&infoPtr->checkbox, pt)) return DTHT_CHECKBOX;
813 for (i = 0; i < infoPtr->nrFields; i++) {
814 if (PtInRect (&infoPtr->fieldRect[i], pt)) return i;
817 return DTHT_NONE;
820 /* Returns index of the nearest preceding date field from given,
821 or -1 if none was found */
822 static int DATETIME_GetPrevDateField(const DATETIME_INFO *infoPtr, int i)
824 for(--i; i >= 0; i--)
826 if (infoPtr->fieldspec[i] & DTHT_DATEFIELD) return i;
828 return -1;
831 static void
832 DATETIME_ApplySelectedField (DATETIME_INFO *infoPtr)
834 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
835 int i, val = 0;
836 BOOL clamp_day = FALSE;
837 SYSTEMTIME date = infoPtr->date;
838 int oldyear;
840 if (infoPtr->select == -1 || infoPtr->nCharsEntered == 0)
841 return;
843 if ((infoPtr->fieldspec[fieldNum] == ONELETTERAMPM) ||
844 (infoPtr->fieldspec[fieldNum] == TWOLETTERAMPM))
845 val = infoPtr->charsEntered[0];
846 else {
847 for (i=0; i<infoPtr->nCharsEntered; i++)
848 val = val * 10 + infoPtr->charsEntered[i] - '0';
851 infoPtr->nCharsEntered = 0;
853 switch (infoPtr->fieldspec[fieldNum]) {
854 case ONEDIGITYEAR:
855 case TWODIGITYEAR:
856 oldyear = date.wYear;
857 date.wYear = date.wYear - (date.wYear%100) + val;
859 if (DATETIME_IsDateInValidRange(infoPtr, &date))
860 clamp_day = TRUE;
861 else
862 date.wYear = oldyear;
864 break;
865 case INVALIDFULLYEAR:
866 case FULLYEAR:
867 oldyear = date.wYear;
868 date.wYear = val;
870 if (DATETIME_IsDateInValidRange(infoPtr, &date))
871 clamp_day = TRUE;
872 else
873 date.wYear = oldyear;
875 break;
876 case ONEDIGITMONTH:
877 case TWODIGITMONTH:
878 date.wMonth = val;
879 clamp_day = TRUE;
880 break;
881 case ONEDIGITDAY:
882 case TWODIGITDAY:
883 date.wDay = val;
884 break;
885 case ONEDIGIT12HOUR:
886 case TWODIGIT12HOUR:
887 if (val >= 24)
888 val -= 20;
890 if (val >= 13)
891 date.wHour = val;
892 else if (val != 0) {
893 if (date.wHour >= 12) /* preserve current AM/PM state */
894 date.wHour = (val == 12 ? 12 : val + 12);
895 else
896 date.wHour = (val == 12 ? 0 : val);
898 break;
899 case ONEDIGIT24HOUR:
900 case TWODIGIT24HOUR:
901 date.wHour = val;
902 break;
903 case ONEDIGITMINUTE:
904 case TWODIGITMINUTE:
905 date.wMinute = val;
906 break;
907 case ONEDIGITSECOND:
908 case TWODIGITSECOND:
909 date.wSecond = val;
910 break;
911 case ONELETTERAMPM:
912 case TWOLETTERAMPM:
913 if (val == 'a' || val == 'A') {
914 if (date.wHour >= 12)
915 date.wHour -= 12;
916 } else if (val == 'p' || val == 'P') {
917 if (date.wHour < 12)
918 date.wHour += 12;
920 break;
923 if (clamp_day && date.wDay > MONTHCAL_MonthLength(date.wMonth, date.wYear))
924 date.wDay = MONTHCAL_MonthLength(date.wMonth, date.wYear);
926 if (DATETIME_SetSystemTime(infoPtr, GDT_VALID, &date))
927 DATETIME_SendDateTimeChangeNotify (infoPtr);
930 static void
931 DATETIME_SetSelectedField (DATETIME_INFO *infoPtr, int select)
933 DATETIME_ApplySelectedField(infoPtr);
935 infoPtr->select = select;
936 infoPtr->nCharsEntered = 0;
939 static LRESULT
940 DATETIME_LButtonDown (DATETIME_INFO *infoPtr, INT x, INT y)
942 POINT pt;
943 int new;
945 pt.x = x;
946 pt.y = y;
947 new = DATETIME_HitTest (infoPtr, pt);
949 SetFocus(infoPtr->hwndSelf);
951 if (!(new & DTHT_NODATEMASK) || (new == DTHT_NONE))
953 if (new == DTHT_NONE)
954 new = infoPtr->nrFields - 1;
955 else
957 /* hitting string part moves selection to next date field to left */
958 if (infoPtr->fieldspec[new] & DT_STRING)
960 new = DATETIME_GetPrevDateField(infoPtr, new);
961 if (new == -1) return 0;
963 /* never select full day of week */
964 if (infoPtr->fieldspec[new] == FULLDAY) return 0;
968 DATETIME_SetSelectedField(infoPtr, new);
970 if (infoPtr->select == DTHT_MCPOPUP) {
971 RECT rcMonthCal;
972 POINT pos;
973 SendMessageW(infoPtr->hMonthCal, MCM_GETMINREQRECT, 0, (LPARAM)&rcMonthCal);
975 /* FIXME: button actually is only depressed during dropdown of the */
976 /* calendar control and when the mouse is over the button window */
977 infoPtr->bCalDepressed = TRUE;
979 /* recalculate the position of the monthcal popup */
980 if(infoPtr->dwStyle & DTS_RIGHTALIGN)
981 pos.x = infoPtr->calbutton.left - (rcMonthCal.right - rcMonthCal.left);
982 else
983 /* FIXME: this should be after the area reserved for the checkbox */
984 pos.x = infoPtr->rcDraw.left;
986 pos.y = infoPtr->rcClient.bottom;
987 OffsetRect( &rcMonthCal, pos.x, pos.y );
988 MapWindowPoints( infoPtr->hwndSelf, 0, (POINT *)&rcMonthCal, 2 );
989 SetWindowPos(infoPtr->hMonthCal, 0, rcMonthCal.left, rcMonthCal.top,
990 rcMonthCal.right - rcMonthCal.left, rcMonthCal.bottom - rcMonthCal.top, 0);
992 if(IsWindowVisible(infoPtr->hMonthCal)) {
993 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
994 infoPtr->bDropdownEnabled = FALSE;
995 DATETIME_SendSimpleNotify (infoPtr, DTN_CLOSEUP);
996 } else {
997 const SYSTEMTIME *lprgSysTimeArray = &infoPtr->date;
998 TRACE("update calendar %04d/%02d/%02d\n",
999 lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay);
1000 SendMessageW(infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
1002 if (infoPtr->bDropdownEnabled) {
1003 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
1004 DATETIME_SendSimpleNotify (infoPtr, DTN_DROPDOWN);
1006 infoPtr->bDropdownEnabled = TRUE;
1009 TRACE ("dt:%p mc:%p mc parent:%p, desktop:%p\n",
1010 infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hwndNotify, GetDesktopWindow ());
1013 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1015 return 0;
1019 static LRESULT
1020 DATETIME_LButtonUp (DATETIME_INFO *infoPtr)
1022 if(infoPtr->bCalDepressed) {
1023 infoPtr->bCalDepressed = FALSE;
1024 InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
1027 return 0;
1031 static LRESULT
1032 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
1034 if (!hdc) {
1035 PAINTSTRUCT ps;
1036 hdc = BeginPaint (infoPtr->hwndSelf, &ps);
1037 DATETIME_Refresh (infoPtr, hdc);
1038 EndPaint (infoPtr->hwndSelf, &ps);
1039 } else {
1040 DATETIME_Refresh (infoPtr, hdc);
1043 /* Not a click on the dropdown box, enabled it */
1044 infoPtr->bDropdownEnabled = TRUE;
1046 return 0;
1050 static LRESULT
1051 DATETIME_Button_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1053 if( HIWORD(wParam) == BN_CLICKED) {
1054 DWORD state = SendMessageW((HWND)lParam, BM_GETCHECK, 0, 0);
1055 infoPtr->dateValid = (state == BST_CHECKED);
1056 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1057 DATETIME_SendDateTimeChangeNotify(infoPtr);
1059 return 0;
1064 static LRESULT
1065 DATETIME_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1067 TRACE("hwndbutton = %p\n", infoPtr->hwndCheckbut);
1068 if(infoPtr->hwndCheckbut == (HWND)lParam)
1069 return DATETIME_Button_Command(infoPtr, wParam, lParam);
1070 return 0;
1074 static LRESULT
1075 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
1077 TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
1078 if (bEnable)
1079 infoPtr->dwStyle &= ~WS_DISABLED;
1080 else
1081 infoPtr->dwStyle |= WS_DISABLED;
1083 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1085 return 0;
1089 static LRESULT
1090 DATETIME_EraseBackground (const DATETIME_INFO *infoPtr, HDC hdc)
1092 HBRUSH hBrush, hSolidBrush = NULL;
1093 RECT rc;
1095 if (infoPtr->dwStyle & WS_DISABLED)
1096 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
1097 else
1099 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
1100 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
1101 if (!hBrush)
1102 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
1105 GetClientRect (infoPtr->hwndSelf, &rc);
1107 FillRect (hdc, &rc, hBrush);
1109 if (hSolidBrush)
1110 DeleteObject(hSolidBrush);
1112 return -1;
1116 static LRESULT
1117 DATETIME_Notify (DATETIME_INFO *infoPtr, const NMHDR *lpnmh)
1119 TRACE ("Got notification %x from %p\n", lpnmh->code, lpnmh->hwndFrom);
1120 TRACE ("info: %p %p %p\n", infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hUpdown);
1122 if (lpnmh->code == MCN_SELECT) {
1123 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1124 infoPtr->dateValid = TRUE;
1125 SendMessageW (infoPtr->hMonthCal, MCM_GETCURSEL, 0, (LPARAM)&infoPtr->date);
1126 TRACE("got from calendar %04d/%02d/%02d day of week %d\n",
1127 infoPtr->date.wYear, infoPtr->date.wMonth, infoPtr->date.wDay, infoPtr->date.wDayOfWeek);
1128 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
1129 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1130 DATETIME_SendDateTimeChangeNotify (infoPtr);
1131 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1133 if ((lpnmh->hwndFrom == infoPtr->hUpdown) && (lpnmh->code == UDN_DELTAPOS)) {
1134 const NM_UPDOWN *lpnmud = (const NM_UPDOWN*)lpnmh;
1135 TRACE("Delta pos %d\n", lpnmud->iDelta);
1136 infoPtr->pendingUpdown = lpnmud->iDelta;
1138 return 0;
1142 static LRESULT
1143 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode)
1145 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1146 int wrap = 0;
1147 int new;
1149 if (!(infoPtr->haveFocus)) return 0;
1150 if ((fieldNum==0) && (infoPtr->select)) return 0;
1152 if (infoPtr->select & FORMATCALLMASK) {
1153 FIXME ("Callbacks not implemented yet\n");
1156 switch (vkCode) {
1157 case VK_ADD:
1158 case VK_UP:
1159 infoPtr->nCharsEntered = 0;
1160 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1161 DATETIME_SendDateTimeChangeNotify (infoPtr);
1162 break;
1163 case VK_SUBTRACT:
1164 case VK_DOWN:
1165 infoPtr->nCharsEntered = 0;
1166 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1167 DATETIME_SendDateTimeChangeNotify (infoPtr);
1168 break;
1169 case VK_HOME:
1170 infoPtr->nCharsEntered = 0;
1171 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MIN);
1172 DATETIME_SendDateTimeChangeNotify (infoPtr);
1173 break;
1174 case VK_END:
1175 infoPtr->nCharsEntered = 0;
1176 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MAX);
1177 DATETIME_SendDateTimeChangeNotify (infoPtr);
1178 break;
1179 case VK_LEFT:
1180 new = infoPtr->select;
1181 do {
1182 if (new == 0) {
1183 new = new - 1;
1184 wrap++;
1185 } else {
1186 new--;
1188 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1189 if (new != infoPtr->select)
1190 DATETIME_SetSelectedField(infoPtr, new);
1191 break;
1192 case VK_RIGHT:
1193 new = infoPtr->select;
1194 do {
1195 new++;
1196 if (new==infoPtr->nrFields) {
1197 new = 0;
1198 wrap++;
1200 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1201 if (new != infoPtr->select)
1202 DATETIME_SetSelectedField(infoPtr, new);
1203 break;
1206 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1208 return 0;
1212 static LRESULT
1213 DATETIME_Char (DATETIME_INFO *infoPtr, WPARAM vkCode)
1215 int fieldNum, fieldSpec;
1217 fieldNum = infoPtr->select & DTHT_DATEFIELD;
1218 fieldSpec = infoPtr->fieldspec[fieldNum];
1220 if (fieldSpec == ONELETTERAMPM || fieldSpec == TWOLETTERAMPM) {
1221 infoPtr->charsEntered[0] = vkCode;
1222 infoPtr->nCharsEntered = 1;
1224 DATETIME_ApplySelectedField(infoPtr);
1225 } else if (vkCode >= '0' && vkCode <= '9') {
1226 int maxChars;
1228 infoPtr->charsEntered[infoPtr->nCharsEntered++] = vkCode;
1230 if (fieldSpec == INVALIDFULLYEAR || fieldSpec == FULLYEAR)
1231 maxChars = 4;
1232 else
1233 maxChars = 2;
1235 if ((fieldSpec == ONEDIGIT12HOUR ||
1236 fieldSpec == TWODIGIT12HOUR ||
1237 fieldSpec == ONEDIGIT24HOUR ||
1238 fieldSpec == TWODIGIT24HOUR) &&
1239 (infoPtr->nCharsEntered == 1))
1241 if (vkCode >= '3')
1242 maxChars = 1;
1245 if (maxChars == infoPtr->nCharsEntered)
1246 DATETIME_ApplySelectedField(infoPtr);
1249 return 0;
1253 static LRESULT
1254 DATETIME_VScroll (DATETIME_INFO *infoPtr, WORD wScroll)
1256 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1258 if ((SHORT)LOWORD(wScroll) != SB_THUMBPOSITION) return 0;
1259 if (!(infoPtr->haveFocus)) return 0;
1260 if ((fieldNum==0) && (infoPtr->select)) return 0;
1262 if (infoPtr->pendingUpdown >= 0) {
1263 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1264 DATETIME_SendDateTimeChangeNotify (infoPtr);
1266 else {
1267 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1268 DATETIME_SendDateTimeChangeNotify (infoPtr);
1271 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1273 return 0;
1277 static LRESULT
1278 DATETIME_KillFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1280 TRACE("lost focus to %p\n", lostFocus);
1282 if (infoPtr->haveFocus) {
1283 DATETIME_SendSimpleNotify (infoPtr, NM_KILLFOCUS);
1284 infoPtr->haveFocus = 0;
1285 DATETIME_SetSelectedField (infoPtr, -1);
1288 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
1290 return 0;
1294 static LRESULT
1295 DATETIME_NCCreate (HWND hwnd, const CREATESTRUCTW *lpcs)
1297 DWORD dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1298 /* force control to have client edge */
1299 dwExStyle |= WS_EX_CLIENTEDGE;
1300 SetWindowLongW(hwnd, GWL_EXSTYLE, dwExStyle);
1302 return 1;
1305 static LRESULT DATETIME_NCPaint (HWND hwnd, HRGN region)
1307 INT cxEdge, cyEdge;
1308 HRGN clipRgn;
1309 HTHEME theme;
1310 LONG exStyle;
1311 RECT r;
1312 HDC dc;
1314 theme = OpenThemeDataForDpi(NULL, WC_EDITW, GetDpiForWindow(hwnd));
1315 if (!theme)
1316 return DefWindowProcW(hwnd, WM_NCPAINT, (WPARAM)region, 0);
1318 exStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1319 if (!(exStyle & WS_EX_CLIENTEDGE))
1321 CloseThemeData(theme);
1322 return DefWindowProcW(hwnd, WM_NCPAINT, (WPARAM)region, 0);
1325 cxEdge = GetSystemMetrics(SM_CXEDGE);
1326 cyEdge = GetSystemMetrics(SM_CYEDGE);
1327 GetWindowRect(hwnd, &r);
1329 /* New clipping region passed to default proc to exclude border */
1330 clipRgn = CreateRectRgn(r.left + cxEdge, r.top + cyEdge, r.right - cxEdge, r.bottom - cyEdge);
1331 if (region != (HRGN)1)
1332 CombineRgn(clipRgn, clipRgn, region, RGN_AND);
1333 OffsetRect(&r, -r.left, -r.top);
1335 dc = GetDCEx(hwnd, region, DCX_WINDOW | DCX_INTERSECTRGN);
1336 if (IsThemeBackgroundPartiallyTransparent(theme, 0, 0))
1337 DrawThemeParentBackground(hwnd, dc, &r);
1338 DrawThemeBackground(theme, dc, 0, 0, &r, 0);
1339 ReleaseDC(hwnd, dc);
1340 CloseThemeData(theme);
1342 /* Call default proc to get the scrollbars etc. also painted */
1343 DefWindowProcW(hwnd, WM_NCPAINT, (WPARAM)clipRgn, 0);
1344 DeleteObject(clipRgn);
1345 return 0;
1348 static LRESULT DATETIME_MouseMove (DATETIME_INFO *infoPtr, LONG x, LONG y)
1350 TRACKMOUSEEVENT event;
1351 POINT point;
1352 BOOL hot;
1354 point.x = x;
1355 point.y = y;
1356 hot = PtInRect(&infoPtr->calbutton, point);
1357 if (hot != infoPtr->bCalHot)
1359 infoPtr->bCalHot = hot;
1360 RedrawWindow(infoPtr->hwndSelf, &infoPtr->calbutton, 0, RDW_INVALIDATE | RDW_UPDATENOW);
1363 if (!hot)
1364 return 0;
1366 event.cbSize = sizeof(TRACKMOUSEEVENT);
1367 event.dwFlags = TME_QUERY;
1368 if (!TrackMouseEvent(&event) || event.hwndTrack != infoPtr->hwndSelf || !(event.dwFlags & TME_LEAVE))
1370 event.hwndTrack = infoPtr->hwndSelf;
1371 event.dwFlags = TME_LEAVE;
1372 TrackMouseEvent(&event);
1375 return 0;
1378 static LRESULT DATETIME_MouseLeave (DATETIME_INFO *infoPtr)
1380 infoPtr->bCalHot = FALSE;
1381 RedrawWindow(infoPtr->hwndSelf, &infoPtr->calbutton, 0, RDW_INVALIDATE | RDW_UPDATENOW);
1382 return 0;
1385 static LRESULT
1386 DATETIME_SetFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1388 TRACE("got focus from %p\n", lostFocus);
1390 /* if monthcal is open and it loses focus, close monthcal */
1391 if (infoPtr->hMonthCal && (lostFocus == infoPtr->hMonthCal) &&
1392 IsWindowVisible(infoPtr->hMonthCal))
1394 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1395 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1396 /* note: this get triggered even if monthcal loses focus to a dropdown
1397 * box click, which occurs without an intermediate WM_PAINT call
1399 infoPtr->bDropdownEnabled = FALSE;
1400 return 0;
1403 if (infoPtr->haveFocus == 0) {
1404 DATETIME_SendSimpleNotify (infoPtr, NM_SETFOCUS);
1405 infoPtr->haveFocus = DTHT_GOTFOCUS;
1408 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1410 return 0;
1414 static BOOL
1415 DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr)
1417 NMDATETIMECHANGE dtdtc;
1419 dtdtc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1420 dtdtc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1421 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1423 dtdtc.dwFlags = infoPtr->dateValid ? GDT_VALID : GDT_NONE;
1425 dtdtc.st = infoPtr->date;
1426 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1427 dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1431 static BOOL
1432 DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code)
1434 NMHDR nmhdr;
1436 TRACE("%x\n", code);
1437 nmhdr.hwndFrom = infoPtr->hwndSelf;
1438 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1439 nmhdr.code = code;
1441 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1442 nmhdr.idFrom, (LPARAM)&nmhdr);
1445 static LRESULT
1446 DATETIME_Size (DATETIME_INFO *infoPtr, INT width, INT height)
1448 /* set size */
1449 infoPtr->rcClient.bottom = height;
1450 infoPtr->rcClient.right = width;
1452 TRACE("Height %ld, Width %ld\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1454 infoPtr->rcDraw = infoPtr->rcClient;
1456 if (infoPtr->dwStyle & DTS_UPDOWN) {
1457 SetWindowPos(infoPtr->hUpdown, NULL,
1458 infoPtr->rcClient.right-14, 0,
1459 15, infoPtr->rcClient.bottom - infoPtr->rcClient.top,
1460 SWP_NOACTIVATE | SWP_NOZORDER);
1462 else {
1463 /* set the size of the button that drops the calendar down */
1464 /* FIXME: account for style that allows button on left side */
1465 infoPtr->calbutton.top = infoPtr->rcDraw.top;
1466 infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1467 infoPtr->calbutton.left = infoPtr->rcDraw.right-15;
1468 infoPtr->calbutton.right = infoPtr->rcDraw.right;
1471 /* set enable/disable button size for show none style being enabled */
1472 /* FIXME: these dimensions are completely incorrect */
1473 infoPtr->checkbox.top = infoPtr->rcDraw.top;
1474 infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1475 infoPtr->checkbox.left = infoPtr->rcDraw.left;
1476 infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1478 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1480 return 0;
1483 static LRESULT
1484 DATETIME_StyleChanging(DATETIME_INFO *infoPtr, WPARAM wStyleType, STYLESTRUCT *lpss)
1486 TRACE("styletype %Ix, styleOld %#lx, styleNew %#lx\n", wStyleType, lpss->styleOld, lpss->styleNew);
1488 /* block DTS_SHOWNONE change */
1489 if ((lpss->styleNew ^ lpss->styleOld) & DTS_SHOWNONE)
1491 if (lpss->styleOld & DTS_SHOWNONE)
1492 lpss->styleNew |= DTS_SHOWNONE;
1493 else
1494 lpss->styleNew &= ~DTS_SHOWNONE;
1497 return 0;
1500 static LRESULT
1501 DATETIME_StyleChanged(DATETIME_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
1503 TRACE("styletype %Ix, styleOld %#lx, styleNew %#lx\n", wStyleType, lpss->styleOld, lpss->styleNew);
1505 if (wStyleType != GWL_STYLE) return 0;
1507 infoPtr->dwStyle = lpss->styleNew;
1509 if ( !(lpss->styleOld & DTS_SHOWNONE) && (lpss->styleNew & DTS_SHOWNONE) ) {
1510 infoPtr->hwndCheckbut = CreateWindowExW (0, WC_BUTTONW, 0, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1511 2, 2, 13, 13, infoPtr->hwndSelf, 0,
1512 (HINSTANCE)GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_HINSTANCE), 0);
1513 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, infoPtr->dateValid ? 1 : 0, 0);
1515 if ( (lpss->styleOld & DTS_SHOWNONE) && !(lpss->styleNew & DTS_SHOWNONE) ) {
1516 DestroyWindow(infoPtr->hwndCheckbut);
1517 infoPtr->hwndCheckbut = 0;
1519 if ( !(lpss->styleOld & DTS_UPDOWN) && (lpss->styleNew & DTS_UPDOWN) ) {
1520 infoPtr->hUpdown = CreateUpDownControl (WS_CHILD | WS_BORDER | WS_VISIBLE, 120, 1, 20, 20,
1521 infoPtr->hwndSelf, 1, 0, 0, UD_MAXVAL, UD_MINVAL, 0);
1523 if ( (lpss->styleOld & DTS_UPDOWN) && !(lpss->styleNew & DTS_UPDOWN) ) {
1524 DestroyWindow(infoPtr->hUpdown);
1525 infoPtr->hUpdown = 0;
1527 return 0;
1530 static LRESULT DATETIME_ThemeChanged (DATETIME_INFO *infoPtr)
1532 HTHEME theme;
1534 theme = GetWindowTheme(infoPtr->hwndSelf);
1535 CloseThemeData(theme);
1536 OpenThemeData(infoPtr->hwndSelf, themeClass);
1537 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1538 return 0;
1541 static BOOL DATETIME_GetIdealSize(DATETIME_INFO *infoPtr, SIZE *size)
1543 SIZE field_size;
1544 RECT rect;
1545 WCHAR txt[80];
1546 HDC hdc;
1547 HFONT oldFont;
1548 int i;
1550 size->cx = size->cy = 0;
1552 hdc = GetDC(infoPtr->hwndSelf);
1553 oldFont = SelectObject(hdc, infoPtr->hFont);
1555 /* Get text font height */
1556 DATETIME_ReturnTxt(infoPtr, 0, txt, ARRAY_SIZE(txt));
1557 GetTextExtentPoint32W(hdc, txt, lstrlenW(txt), &field_size);
1558 size->cy = field_size.cy;
1560 /* Get text font width */
1561 for (i = 0; i < infoPtr->nrFields; i++)
1563 size->cx += DATETIME_GetFieldWidth(infoPtr, hdc, i);
1566 SelectObject(hdc, oldFont);
1567 ReleaseDC(infoPtr->hwndSelf, hdc);
1569 if (infoPtr->dwStyle & DTS_UPDOWN)
1571 GetWindowRect(infoPtr->hUpdown, &rect);
1572 size->cx += rect.right - rect.left;
1574 else
1576 size->cx += infoPtr->calbutton.right - infoPtr->calbutton.left;
1579 if (infoPtr->dwStyle & DTS_SHOWNONE)
1581 size->cx += infoPtr->checkbox.right - infoPtr->checkbox.left;
1584 /* Add space between controls for them not to get too close */
1585 size->cx += 12;
1586 size->cy += 4;
1588 TRACE("cx %ld, cy %ld\n", size->cx, size->cy);
1589 return TRUE;
1592 static LRESULT
1593 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1595 infoPtr->hFont = font;
1596 if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1597 return 0;
1601 static LRESULT
1602 DATETIME_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1604 DATETIME_INFO *infoPtr = Alloc (sizeof(DATETIME_INFO));
1605 STYLESTRUCT ss = { 0, lpcs->style };
1607 if (!infoPtr) return -1;
1609 infoPtr->hwndSelf = hwnd;
1610 infoPtr->dwStyle = lpcs->style;
1612 infoPtr->nrFieldsAllocated = 32;
1613 infoPtr->fieldspec = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1614 infoPtr->fieldRect = Alloc (infoPtr->nrFieldsAllocated * sizeof(RECT));
1615 infoPtr->buflen = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1616 infoPtr->hwndNotify = lpcs->hwndParent;
1617 infoPtr->select = -1; /* initially, nothing is selected */
1618 infoPtr->bDropdownEnabled = TRUE;
1619 infoPtr->bCalHot = FALSE;
1621 DATETIME_StyleChanged(infoPtr, GWL_STYLE, &ss);
1622 DATETIME_SetFormatW (infoPtr, 0);
1624 /* create the monthcal control */
1625 infoPtr->hMonthCal = CreateWindowExW (0, MONTHCAL_CLASSW, 0, WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS,
1626 0, 0, 0, 0, infoPtr->hwndSelf, 0, 0, 0);
1628 /* initialize info structure */
1629 GetLocalTime (&infoPtr->date);
1630 infoPtr->dateValid = TRUE;
1631 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1633 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1634 OpenThemeData(hwnd, themeClass);
1636 return 0;
1641 static LRESULT
1642 DATETIME_Destroy (DATETIME_INFO *infoPtr)
1644 HTHEME theme;
1646 theme = GetWindowTheme(infoPtr->hwndSelf);
1647 CloseThemeData(theme);
1649 if (infoPtr->hwndCheckbut)
1650 DestroyWindow(infoPtr->hwndCheckbut);
1651 if (infoPtr->hUpdown)
1652 DestroyWindow(infoPtr->hUpdown);
1653 if (infoPtr->hMonthCal)
1654 DestroyWindow(infoPtr->hMonthCal);
1655 SetWindowLongPtrW( infoPtr->hwndSelf, 0, 0 ); /* clear infoPtr */
1656 Free (infoPtr->buflen);
1657 Free (infoPtr->fieldRect);
1658 Free (infoPtr->fieldspec);
1659 Free (infoPtr);
1660 return 0;
1664 static INT
1665 DATETIME_GetText (const DATETIME_INFO *infoPtr, INT count, LPWSTR dst)
1667 WCHAR buf[80];
1668 int i;
1670 if (!dst || (count <= 0)) return 0;
1672 dst[0] = 0;
1673 for (i = 0; i < infoPtr->nrFields; i++)
1675 DATETIME_ReturnTxt(infoPtr, i, buf, ARRAY_SIZE(buf));
1676 if ((lstrlenW(dst) + lstrlenW(buf)) < count)
1677 lstrcatW(dst, buf);
1678 else break;
1680 return lstrlenW(dst);
1683 static int DATETIME_GetTextLength(const DATETIME_INFO *info)
1685 int i, length = 0;
1686 WCHAR buffer[80];
1688 TRACE("%p.\n", info);
1690 for (i = 0; i < info->nrFields; i++)
1692 DATETIME_ReturnTxt(info, i, buffer, ARRAY_SIZE(buffer));
1693 length += lstrlenW(buffer);
1695 return length;
1698 static LRESULT WINAPI
1699 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1701 DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1703 TRACE("%x, %Ix, %Ix\n", uMsg, wParam, lParam);
1705 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
1706 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
1708 switch (uMsg) {
1710 case DTM_GETSYSTEMTIME:
1711 return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1713 case DTM_SETSYSTEMTIME:
1714 return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1716 case DTM_GETRANGE:
1717 return SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1719 case DTM_SETRANGE:
1720 return SendMessageW (infoPtr->hMonthCal, MCM_SETRANGE, wParam, lParam);
1722 case DTM_SETFORMATA:
1723 return DATETIME_SetFormatA (infoPtr, (LPCSTR)lParam);
1725 case DTM_SETFORMATW:
1726 return DATETIME_SetFormatW (infoPtr, (LPCWSTR)lParam);
1728 case DTM_GETMONTHCAL:
1729 return (LRESULT)infoPtr->hMonthCal;
1731 case DTM_SETMCCOLOR:
1732 return SendMessageW (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
1734 case DTM_GETMCCOLOR:
1735 return SendMessageW (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
1737 case DTM_SETMCFONT:
1738 return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1740 case DTM_GETMCFONT:
1741 return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1743 case DTM_GETIDEALSIZE:
1744 return DATETIME_GetIdealSize(infoPtr, (SIZE *)lParam);
1746 case WM_NOTIFY:
1747 return DATETIME_Notify (infoPtr, (LPNMHDR)lParam);
1749 case WM_ENABLE:
1750 return DATETIME_Enable (infoPtr, (BOOL)wParam);
1752 case WM_ERASEBKGND:
1753 return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1755 case WM_GETDLGCODE:
1756 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1758 case WM_PRINTCLIENT:
1759 case WM_PAINT:
1760 return DATETIME_Paint (infoPtr, (HDC)wParam);
1762 case WM_KEYDOWN:
1763 return DATETIME_KeyDown (infoPtr, wParam);
1765 case WM_CHAR:
1766 return DATETIME_Char (infoPtr, wParam);
1768 case WM_KILLFOCUS:
1769 return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1771 case WM_NCCREATE:
1772 return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1774 case WM_NCPAINT:
1775 return DATETIME_NCPaint(hwnd, (HRGN)wParam);
1777 case WM_MOUSEMOVE:
1778 return DATETIME_MouseMove(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1780 case WM_MOUSELEAVE:
1781 return DATETIME_MouseLeave(infoPtr);
1783 case WM_SETFOCUS:
1784 return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1786 case WM_SIZE:
1787 return DATETIME_Size (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1789 case WM_LBUTTONDOWN:
1790 return DATETIME_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1792 case WM_LBUTTONUP:
1793 return DATETIME_LButtonUp (infoPtr);
1795 case WM_VSCROLL:
1796 return DATETIME_VScroll (infoPtr, (WORD)wParam);
1798 case WM_CREATE:
1799 return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1801 case WM_DESTROY:
1802 return DATETIME_Destroy (infoPtr);
1804 case WM_COMMAND:
1805 return DATETIME_Command (infoPtr, wParam, lParam);
1807 case WM_STYLECHANGING:
1808 return DATETIME_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1810 case WM_STYLECHANGED:
1811 return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1813 case WM_THEMECHANGED:
1814 return DATETIME_ThemeChanged(infoPtr);
1816 case WM_SETFONT:
1817 return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1819 case WM_GETFONT:
1820 return (LRESULT) infoPtr->hFont;
1822 case WM_GETTEXT:
1823 return (LRESULT) DATETIME_GetText(infoPtr, wParam, (LPWSTR)lParam);
1825 case WM_GETTEXTLENGTH:
1826 return (LRESULT)DATETIME_GetTextLength(infoPtr);
1828 case WM_SETTEXT:
1829 return CB_ERR;
1831 default:
1832 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
1833 ERR("unknown msg %04x, wp %Ix, lp %Ix\n", uMsg, wParam, lParam);
1834 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1839 void
1840 DATETIME_Register (void)
1842 WNDCLASSW wndClass;
1844 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1845 wndClass.style = CS_GLOBALCLASS;
1846 wndClass.lpfnWndProc = DATETIME_WindowProc;
1847 wndClass.cbClsExtra = 0;
1848 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1849 wndClass.hCursor = LoadCursorW (0, (LPCWSTR)IDC_ARROW);
1850 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1851 wndClass.lpszClassName = DATETIMEPICK_CLASSW;
1853 RegisterClassW (&wndClass);
1857 void
1858 DATETIME_Unregister (void)
1860 UnregisterClassW (DATETIMEPICK_CLASSW, NULL);