wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / comctl32 / datetime.c
blobf44b52c3b65f29f2464035e6a14ce5ff8401f5e0
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 /* in monthcal.c */
85 extern int MONTHCAL_MonthLength(int month, int year);
86 extern int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace);
88 /* this list of defines is closely related to `allowedformatchars' defined
89 * in datetime.c; the high nibble indicates the `base type' of the format
90 * specifier.
91 * Do not change without first reading DATETIME_UseFormat.
95 #define DT_END_FORMAT 0
96 #define ONEDIGITDAY 0x01
97 #define TWODIGITDAY 0x02
98 #define THREECHARDAY 0x03
99 #define FULLDAY 0x04
100 #define ONEDIGIT12HOUR 0x11
101 #define TWODIGIT12HOUR 0x12
102 #define ONEDIGIT24HOUR 0x21
103 #define TWODIGIT24HOUR 0x22
104 #define ONEDIGITMINUTE 0x31
105 #define TWODIGITMINUTE 0x32
106 #define ONEDIGITMONTH 0x41
107 #define TWODIGITMONTH 0x42
108 #define THREECHARMONTH 0x43
109 #define FULLMONTH 0x44
110 #define ONEDIGITSECOND 0x51
111 #define TWODIGITSECOND 0x52
112 #define ONELETTERAMPM 0x61
113 #define TWOLETTERAMPM 0x62
114 #define ONEDIGITYEAR 0x71
115 #define TWODIGITYEAR 0x72
116 #define INVALIDFULLYEAR 0x73 /* FIXME - yyy is not valid - we'll treat it as yyyy */
117 #define FULLYEAR 0x74
118 #define FORMATCALLBACK 0x81 /* -> maximum of 0x80 callbacks possible */
119 #define FORMATCALLMASK 0x80
120 #define DT_STRING 0x0100
122 #define DTHT_DATEFIELD 0xff /* for hit-testing */
124 #define DTHT_NONE 0x1000
125 #define DTHT_CHECKBOX 0x2000 /* these should end at '00' , to make */
126 #define DTHT_MCPOPUP 0x3000 /* & DTHT_DATEFIELD 0 when DATETIME_KeyDown */
127 #define DTHT_GOTFOCUS 0x4000 /* tests for date-fields */
128 #define DTHT_NODATEMASK 0xf000 /* to mask check and drop down from others */
130 static BOOL DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code);
131 static BOOL DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr);
132 static const WCHAR allowedformatchars[] = L"dhHmMstyX";
133 static const int maxrepetition [] = {4,2,2,2,4,2,2,4,-1};
134 static const WCHAR *themeClass = WC_SCROLLBARW;
136 /* valid date limits */
137 static const SYSTEMTIME max_allowed_date = { .wYear = 9999, .wMonth = 12, .wDayOfWeek = 0, .wDay = 31 };
138 static const SYSTEMTIME min_allowed_date = { .wYear = 1752, .wMonth = 9, .wDayOfWeek = 0, .wDay = 14 };
140 static DWORD
141 DATETIME_GetSystemTime (const DATETIME_INFO *infoPtr, SYSTEMTIME *systime)
143 if (!systime) return GDT_NONE;
145 if ((infoPtr->dwStyle & DTS_SHOWNONE) &&
146 (SendMessageW (infoPtr->hwndCheckbut, BM_GETCHECK, 0, 0) == BST_UNCHECKED))
147 return GDT_NONE;
149 *systime = infoPtr->date;
151 return GDT_VALID;
154 /* Checks value is within configured date range
156 * PARAMETERS
158 * [I] infoPtr : valid pointer to control data
159 * [I] date : pointer to valid date data to check
161 * RETURN VALUE
163 * TRUE - date within configured range
164 * FALSE - date is outside configured range
166 static BOOL DATETIME_IsDateInValidRange(const DATETIME_INFO *infoPtr, const SYSTEMTIME *date)
168 SYSTEMTIME range[2];
169 DWORD limits;
171 if ((MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) ||
172 (MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1))
173 return FALSE;
175 limits = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, 0, (LPARAM)range);
177 if (limits & GDTR_MAX)
179 if (MONTHCAL_CompareSystemTime(date, &range[1]) == 1)
180 return FALSE;
183 if (limits & GDTR_MIN)
185 if (MONTHCAL_CompareSystemTime(date, &range[0]) == -1)
186 return FALSE;
189 return TRUE;
192 static BOOL
193 DATETIME_SetSystemTime (DATETIME_INFO *infoPtr, DWORD flag, const SYSTEMTIME *systime)
195 if (!systime) return FALSE;
197 TRACE("%04d/%02d/%02d %02d:%02d:%02d\n",
198 systime->wYear, systime->wMonth, systime->wDay,
199 systime->wHour, systime->wMinute, systime->wSecond);
201 if (flag == GDT_VALID) {
202 if (systime->wYear == 0 ||
203 systime->wMonth < 1 || systime->wMonth > 12 ||
204 systime->wDay < 1 ||
205 systime->wDay > MONTHCAL_MonthLength(systime->wMonth, systime->wYear) ||
206 systime->wHour > 23 ||
207 systime->wMinute > 59 ||
208 systime->wSecond > 59 ||
209 systime->wMilliseconds > 999
211 return FALSE;
213 /* Windows returns true if the date is valid but outside the limits set */
214 if (!DATETIME_IsDateInValidRange(infoPtr, systime))
215 return TRUE;
217 infoPtr->dateValid = TRUE;
218 infoPtr->date = *systime;
219 /* always store a valid day of week */
220 MONTHCAL_CalculateDayOfWeek(&infoPtr->date, TRUE);
222 SendMessageW (infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
223 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
224 } else if ((infoPtr->dwStyle & DTS_SHOWNONE) && (flag == GDT_NONE)) {
225 infoPtr->dateValid = FALSE;
226 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_UNCHECKED, 0);
228 else
229 return FALSE;
231 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
232 return TRUE;
236 /***
237 * Split up a formattxt in actions.
238 * See ms documentation for the meaning of the letter codes/'specifiers'.
240 * Notes:
241 * *'dddddd' is handled as 'dddd' plus 'dd'.
242 * *unrecognized formats are strings (here given the type DT_STRING;
243 * start of the string is encoded in lower bits of DT_STRING.
244 * Therefore, 'string' ends up as '<show seconds>tring'.
247 static void
248 DATETIME_UseFormat (DATETIME_INFO *infoPtr, LPCWSTR formattxt)
250 unsigned int i;
251 int j, k, len;
252 BOOL inside_literal = FALSE; /* inside '...' */
253 int *nrFields = &infoPtr->nrFields;
255 *nrFields = 0;
256 infoPtr->fieldspec[*nrFields] = 0;
257 len = lstrlenW(allowedformatchars);
258 k = 0;
260 for (i = 0; formattxt[i]; i++) {
261 TRACE ("\n%d %c:", i, formattxt[i]);
262 if (!inside_literal) {
263 for (j = 0; j < len; j++) {
264 if (allowedformatchars[j]==formattxt[i]) {
265 TRACE ("%c[%d,%x]", allowedformatchars[j], *nrFields, infoPtr->fieldspec[*nrFields]);
266 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
267 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
268 break;
270 if (infoPtr->fieldspec[*nrFields] >> 4 != j) {
271 (*nrFields)++;
272 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
273 break;
275 if ((infoPtr->fieldspec[*nrFields] & 0x0f) == maxrepetition[j]) {
276 (*nrFields)++;
277 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
278 break;
280 infoPtr->fieldspec[*nrFields]++;
281 break;
282 } /* if allowedformatchar */
283 } /* for j */
285 else
286 j = len;
288 if (formattxt[i] == '\'')
290 inside_literal = !inside_literal;
291 continue;
294 /* char is not a specifier: handle char like a string */
295 if (j == len) {
296 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
297 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
298 infoPtr->buflen[*nrFields] = 0;
299 } else if ((infoPtr->fieldspec[*nrFields] & DT_STRING) != DT_STRING) {
300 (*nrFields)++;
301 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
302 infoPtr->buflen[*nrFields] = 0;
304 infoPtr->textbuf[k] = formattxt[i];
305 k++;
306 infoPtr->buflen[*nrFields]++;
307 } /* if j=len */
309 if (*nrFields == infoPtr->nrFieldsAllocated) {
310 FIXME ("out of memory; should reallocate. crash ahead.\n");
312 } /* for i */
314 TRACE("\n");
316 if (infoPtr->fieldspec[*nrFields] != 0) (*nrFields)++;
320 static BOOL
321 DATETIME_SetFormatW (DATETIME_INFO *infoPtr, LPCWSTR format)
323 WCHAR format_buf[80];
325 if (!format) {
326 DWORD format_item;
328 if ((infoPtr->dwStyle & DTS_SHORTDATECENTURYFORMAT) == DTS_SHORTDATECENTURYFORMAT)
329 format_item = LOCALE_SSHORTDATE;
330 else if ((infoPtr->dwStyle & DTS_LONGDATEFORMAT) == DTS_LONGDATEFORMAT)
331 format_item = LOCALE_SLONGDATE;
332 else if ((infoPtr->dwStyle & DTS_TIMEFORMAT) == DTS_TIMEFORMAT)
333 format_item = LOCALE_STIMEFORMAT;
334 else /* DTS_SHORTDATEFORMAT */
335 format_item = LOCALE_SSHORTDATE;
336 GetLocaleInfoW(LOCALE_USER_DEFAULT, format_item, format_buf, ARRAY_SIZE(format_buf));
337 format = format_buf;
340 DATETIME_UseFormat (infoPtr, format);
341 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
343 return TRUE;
347 static BOOL
348 DATETIME_SetFormatA (DATETIME_INFO *infoPtr, LPCSTR lpszFormat)
350 if (lpszFormat) {
351 BOOL retval;
352 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, NULL, 0);
353 LPWSTR wstr = Alloc(len * sizeof(WCHAR));
354 if (wstr) MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, wstr, len);
355 retval = DATETIME_SetFormatW (infoPtr, wstr);
356 Free (wstr);
357 return retval;
359 else
360 return DATETIME_SetFormatW (infoPtr, 0);
365 static void
366 DATETIME_ReturnTxt (const DATETIME_INFO *infoPtr, int count, LPWSTR result, int resultSize)
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, L"%d", date.wDay);
400 break;
401 case TWODIGITDAY:
402 wsprintfW (result, L"%.2d", 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, L"%d", 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, L"%.2d", date.wHour - (date.wHour > 12 ? 12 : 0));
428 break;
429 case ONEDIGIT24HOUR:
430 wsprintfW (result, L"%d", date.wHour);
431 break;
432 case TWODIGIT24HOUR:
433 wsprintfW (result, L"%.2d", date.wHour);
434 break;
435 case ONEDIGITSECOND:
436 wsprintfW (result, L"%d", date.wSecond);
437 break;
438 case TWODIGITSECOND:
439 wsprintfW (result, L"%.2d", date.wSecond);
440 break;
441 case ONEDIGITMINUTE:
442 wsprintfW (result, L"%d", date.wMinute);
443 break;
444 case TWODIGITMINUTE:
445 wsprintfW (result, L"%.2d", date.wMinute);
446 break;
447 case ONEDIGITMONTH:
448 wsprintfW (result, L"%d", date.wMonth);
449 break;
450 case TWODIGITMONTH:
451 wsprintfW (result, L"%.2d", date.wMonth);
452 break;
453 case THREECHARMONTH:
454 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+date.wMonth -1, buffer, ARRAY_SIZE(buffer));
455 wsprintfW (result, L"%s.3s", 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, L"%d", date.wYear % 10);
477 break;
478 case TWODIGITYEAR:
479 wsprintfW (result, L"%.2d", date.wYear % 100);
480 break;
481 case INVALIDFULLYEAR:
482 case FULLYEAR:
483 wsprintfW (result, L"%d", 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 int spec;
606 WCHAR buffer[80];
607 LPCWSTR bufptr;
608 SIZE size;
610 if (!infoPtr->fieldspec) return 0;
612 spec = infoPtr->fieldspec[count];
613 if (spec & DT_STRING) {
614 int txtlen = infoPtr->buflen[count];
616 if (txtlen > 79)
617 txtlen = 79;
618 memcpy (buffer, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
619 buffer[txtlen] = 0;
620 bufptr = buffer;
622 else {
623 switch (spec) {
624 case ONEDIGITDAY:
625 case ONEDIGIT12HOUR:
626 case ONEDIGIT24HOUR:
627 case ONEDIGITSECOND:
628 case ONEDIGITMINUTE:
629 case ONEDIGITMONTH:
630 case ONEDIGITYEAR:
631 /* these seem to use a two byte field */
632 case TWODIGITDAY:
633 case TWODIGIT12HOUR:
634 case TWODIGIT24HOUR:
635 case TWODIGITSECOND:
636 case TWODIGITMINUTE:
637 case TWODIGITMONTH:
638 case TWODIGITYEAR:
639 bufptr = L"22";
640 break;
641 case INVALIDFULLYEAR:
642 case FULLYEAR:
643 bufptr = L"2222";
644 break;
645 case THREECHARMONTH:
646 case FULLMONTH:
647 case THREECHARDAY:
648 case FULLDAY:
650 const WCHAR *fall;
651 LCTYPE lctype;
652 INT i, max_count;
653 LONG cx;
655 /* choose locale data type and fallback string */
656 switch (spec) {
657 case THREECHARDAY:
658 fall = L"Wed";
659 lctype = LOCALE_SABBREVDAYNAME1;
660 max_count = 7;
661 break;
662 case FULLDAY:
663 fall = L"Wednesday";
664 lctype = LOCALE_SDAYNAME1;
665 max_count = 7;
666 break;
667 case THREECHARMONTH:
668 fall = L"Dec";
669 lctype = LOCALE_SABBREVMONTHNAME1;
670 max_count = 12;
671 break;
672 default: /* FULLMONTH */
673 fall = L"September";
674 lctype = LOCALE_SMONTHNAME1;
675 max_count = 12;
676 break;
679 cx = 0;
680 for (i = 0; i < max_count; i++)
682 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, lctype + i,
683 buffer, ARRAY_SIZE(buffer)))
685 GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &size);
686 if (size.cx > cx) cx = size.cx;
688 else /* locale independent fallback on failure */
690 GetTextExtentPoint32W(hdc, fall, lstrlenW(fall), &size);
691 cx = size.cx;
692 break;
695 return cx;
697 case ONELETTERAMPM:
698 bufptr = L"A";
699 break;
700 case TWOLETTERAMPM:
701 bufptr = L"AM";
702 break;
703 default:
704 bufptr = L"2";
705 break;
708 GetTextExtentPoint32W (hdc, bufptr, lstrlenW(bufptr), &size);
709 return size.cx;
712 static void
713 DATETIME_Refresh (DATETIME_INFO *infoPtr, HDC hdc)
715 HTHEME theme;
716 int state;
718 TRACE("\n");
720 if (infoPtr->dateValid) {
721 int i, prevright;
722 RECT *field;
723 RECT *rcDraw = &infoPtr->rcDraw;
724 SIZE size;
725 COLORREF oldTextColor;
726 HFONT oldFont = SelectObject (hdc, infoPtr->hFont);
727 INT oldBkMode = SetBkMode (hdc, TRANSPARENT);
728 WCHAR txt[80];
730 DATETIME_ReturnTxt (infoPtr, 0, txt, ARRAY_SIZE(txt));
731 GetTextExtentPoint32W (hdc, txt, lstrlenW(txt), &size);
732 rcDraw->bottom = size.cy + 2;
734 prevright = infoPtr->checkbox.right = ((infoPtr->dwStyle & DTS_SHOWNONE) ? 18 : 2);
736 for (i = 0; i < infoPtr->nrFields; i++) {
737 DATETIME_ReturnTxt (infoPtr, i, txt, ARRAY_SIZE(txt));
738 GetTextExtentPoint32W (hdc, txt, lstrlenW(txt), &size);
739 field = &infoPtr->fieldRect[i];
740 field->left = prevright;
741 field->right = prevright + DATETIME_GetFieldWidth (infoPtr, hdc, i);
742 field->top = rcDraw->top;
743 field->bottom = rcDraw->bottom;
744 prevright = field->right;
746 if (infoPtr->dwStyle & WS_DISABLED)
747 oldTextColor = SetTextColor (hdc, comctl32_color.clrGrayText);
748 else if ((infoPtr->haveFocus) && (i == infoPtr->select)) {
749 RECT selection;
751 /* fill if focused */
752 HBRUSH hbr = CreateSolidBrush (comctl32_color.clrActiveCaption);
754 if (infoPtr->nCharsEntered)
756 memcpy(txt, infoPtr->charsEntered, infoPtr->nCharsEntered * sizeof(WCHAR));
757 txt[infoPtr->nCharsEntered] = 0;
758 GetTextExtentPoint32W (hdc, txt, lstrlenW(txt), &size);
761 SetRect(&selection, 0, 0, size.cx, size.cy);
762 /* center rectangle */
763 OffsetRect(&selection, (field->right + field->left - size.cx)/2,
764 (field->bottom - size.cy)/2);
766 FillRect(hdc, &selection, hbr);
767 DeleteObject (hbr);
768 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindow);
770 else
771 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindowText);
773 /* draw the date text using the colour set above */
774 DrawTextW (hdc, txt, lstrlenW(txt), field, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
775 SetTextColor (hdc, oldTextColor);
777 SetBkMode (hdc, oldBkMode);
778 SelectObject (hdc, oldFont);
781 if (infoPtr->dwStyle & DTS_UPDOWN)
782 return;
784 theme = GetWindowTheme(infoPtr->hwndSelf);
785 if (theme)
787 if (infoPtr->dwStyle & WS_DISABLED)
788 state = ABS_DOWNDISABLED;
789 else if (infoPtr->bCalDepressed)
790 state = ABS_DOWNPRESSED;
791 else if (infoPtr->bCalHot)
792 state = ABS_DOWNHOT;
793 else
794 state = ABS_DOWNNORMAL;
796 if (IsThemeBackgroundPartiallyTransparent(theme, SBP_ARROWBTN, state))
797 DrawThemeParentBackground(infoPtr->hwndSelf, hdc, &infoPtr->calbutton);
798 DrawThemeBackground(theme, hdc, SBP_ARROWBTN, state, &infoPtr->calbutton, NULL);
800 else
802 DrawFrameControl(hdc, &infoPtr->calbutton, DFC_SCROLL,
803 DFCS_SCROLLDOWN | (infoPtr->bCalDepressed ? DFCS_PUSHED : 0) |
804 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
809 static INT
810 DATETIME_HitTest (const DATETIME_INFO *infoPtr, POINT pt)
812 int i;
814 TRACE ("%s\n", wine_dbgstr_point(&pt));
816 if (PtInRect (&infoPtr->calbutton, pt)) return DTHT_MCPOPUP;
817 if (PtInRect (&infoPtr->checkbox, pt)) return DTHT_CHECKBOX;
819 for (i = 0; i < infoPtr->nrFields; i++) {
820 if (PtInRect (&infoPtr->fieldRect[i], pt)) return i;
823 return DTHT_NONE;
826 /* Returns index of the nearest preceding date field from given,
827 or -1 if none was found */
828 static int DATETIME_GetPrevDateField(const DATETIME_INFO *infoPtr, int i)
830 for(--i; i >= 0; i--)
832 if (infoPtr->fieldspec[i] & DTHT_DATEFIELD) return i;
834 return -1;
837 static void
838 DATETIME_ApplySelectedField (DATETIME_INFO *infoPtr)
840 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
841 int i, val = 0;
842 BOOL clamp_day = FALSE;
843 SYSTEMTIME date = infoPtr->date;
844 int oldyear;
846 if (infoPtr->select == -1 || infoPtr->nCharsEntered == 0)
847 return;
849 if ((infoPtr->fieldspec[fieldNum] == ONELETTERAMPM) ||
850 (infoPtr->fieldspec[fieldNum] == TWOLETTERAMPM))
851 val = infoPtr->charsEntered[0];
852 else {
853 for (i=0; i<infoPtr->nCharsEntered; i++)
854 val = val * 10 + infoPtr->charsEntered[i] - '0';
857 infoPtr->nCharsEntered = 0;
859 switch (infoPtr->fieldspec[fieldNum]) {
860 case ONEDIGITYEAR:
861 case TWODIGITYEAR:
862 oldyear = date.wYear;
863 date.wYear = date.wYear - (date.wYear%100) + val;
865 if (DATETIME_IsDateInValidRange(infoPtr, &date))
866 clamp_day = TRUE;
867 else
868 date.wYear = oldyear;
870 break;
871 case INVALIDFULLYEAR:
872 case FULLYEAR:
873 oldyear = date.wYear;
874 date.wYear = val;
876 if (DATETIME_IsDateInValidRange(infoPtr, &date))
877 clamp_day = TRUE;
878 else
879 date.wYear = oldyear;
881 break;
882 case ONEDIGITMONTH:
883 case TWODIGITMONTH:
884 date.wMonth = val;
885 clamp_day = TRUE;
886 break;
887 case ONEDIGITDAY:
888 case TWODIGITDAY:
889 date.wDay = val;
890 break;
891 case ONEDIGIT12HOUR:
892 case TWODIGIT12HOUR:
893 if (val >= 24)
894 val -= 20;
896 if (val >= 13)
897 date.wHour = val;
898 else if (val != 0) {
899 if (date.wHour >= 12) /* preserve current AM/PM state */
900 date.wHour = (val == 12 ? 12 : val + 12);
901 else
902 date.wHour = (val == 12 ? 0 : val);
904 break;
905 case ONEDIGIT24HOUR:
906 case TWODIGIT24HOUR:
907 date.wHour = val;
908 break;
909 case ONEDIGITMINUTE:
910 case TWODIGITMINUTE:
911 date.wMinute = val;
912 break;
913 case ONEDIGITSECOND:
914 case TWODIGITSECOND:
915 date.wSecond = val;
916 break;
917 case ONELETTERAMPM:
918 case TWOLETTERAMPM:
919 if (val == 'a' || val == 'A') {
920 if (date.wHour >= 12)
921 date.wHour -= 12;
922 } else if (val == 'p' || val == 'P') {
923 if (date.wHour < 12)
924 date.wHour += 12;
926 break;
929 if (clamp_day && date.wDay > MONTHCAL_MonthLength(date.wMonth, date.wYear))
930 date.wDay = MONTHCAL_MonthLength(date.wMonth, date.wYear);
932 if (DATETIME_SetSystemTime(infoPtr, GDT_VALID, &date))
933 DATETIME_SendDateTimeChangeNotify (infoPtr);
936 static void
937 DATETIME_SetSelectedField (DATETIME_INFO *infoPtr, int select)
939 DATETIME_ApplySelectedField(infoPtr);
941 infoPtr->select = select;
942 infoPtr->nCharsEntered = 0;
945 static LRESULT
946 DATETIME_LButtonDown (DATETIME_INFO *infoPtr, INT x, INT y)
948 POINT pt;
949 int new;
951 pt.x = x;
952 pt.y = y;
953 new = DATETIME_HitTest (infoPtr, pt);
955 SetFocus(infoPtr->hwndSelf);
957 if (!(new & DTHT_NODATEMASK) || (new == DTHT_NONE))
959 if (new == DTHT_NONE)
960 new = infoPtr->nrFields - 1;
961 else
963 /* hitting string part moves selection to next date field to left */
964 if (infoPtr->fieldspec[new] & DT_STRING)
966 new = DATETIME_GetPrevDateField(infoPtr, new);
967 if (new == -1) return 0;
969 /* never select full day of week */
970 if (infoPtr->fieldspec[new] == FULLDAY) return 0;
974 DATETIME_SetSelectedField(infoPtr, new);
976 if (infoPtr->select == DTHT_MCPOPUP) {
977 RECT rcMonthCal;
978 POINT pos;
979 SendMessageW(infoPtr->hMonthCal, MCM_GETMINREQRECT, 0, (LPARAM)&rcMonthCal);
981 /* FIXME: button actually is only depressed during dropdown of the */
982 /* calendar control and when the mouse is over the button window */
983 infoPtr->bCalDepressed = TRUE;
985 /* recalculate the position of the monthcal popup */
986 if(infoPtr->dwStyle & DTS_RIGHTALIGN)
987 pos.x = infoPtr->calbutton.left - (rcMonthCal.right - rcMonthCal.left);
988 else
989 /* FIXME: this should be after the area reserved for the checkbox */
990 pos.x = infoPtr->rcDraw.left;
992 pos.y = infoPtr->rcClient.bottom;
993 OffsetRect( &rcMonthCal, pos.x, pos.y );
994 MapWindowPoints( infoPtr->hwndSelf, 0, (POINT *)&rcMonthCal, 2 );
995 SetWindowPos(infoPtr->hMonthCal, 0, rcMonthCal.left, rcMonthCal.top,
996 rcMonthCal.right - rcMonthCal.left, rcMonthCal.bottom - rcMonthCal.top, 0);
998 if(IsWindowVisible(infoPtr->hMonthCal)) {
999 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1000 infoPtr->bDropdownEnabled = FALSE;
1001 DATETIME_SendSimpleNotify (infoPtr, DTN_CLOSEUP);
1002 } else {
1003 const SYSTEMTIME *lprgSysTimeArray = &infoPtr->date;
1004 TRACE("update calendar %04d/%02d/%02d\n",
1005 lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay);
1006 SendMessageW(infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
1008 if (infoPtr->bDropdownEnabled) {
1009 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
1010 DATETIME_SendSimpleNotify (infoPtr, DTN_DROPDOWN);
1012 infoPtr->bDropdownEnabled = TRUE;
1015 TRACE ("dt:%p mc:%p mc parent:%p, desktop:%p\n",
1016 infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hwndNotify, GetDesktopWindow ());
1019 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1021 return 0;
1025 static LRESULT
1026 DATETIME_LButtonUp (DATETIME_INFO *infoPtr)
1028 if(infoPtr->bCalDepressed) {
1029 infoPtr->bCalDepressed = FALSE;
1030 InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
1033 return 0;
1037 static LRESULT
1038 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
1040 if (!hdc) {
1041 PAINTSTRUCT ps;
1042 hdc = BeginPaint (infoPtr->hwndSelf, &ps);
1043 DATETIME_Refresh (infoPtr, hdc);
1044 EndPaint (infoPtr->hwndSelf, &ps);
1045 } else {
1046 DATETIME_Refresh (infoPtr, hdc);
1049 /* Not a click on the dropdown box, enabled it */
1050 infoPtr->bDropdownEnabled = TRUE;
1052 return 0;
1056 static LRESULT
1057 DATETIME_Button_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1059 if( HIWORD(wParam) == BN_CLICKED) {
1060 DWORD state = SendMessageW((HWND)lParam, BM_GETCHECK, 0, 0);
1061 infoPtr->dateValid = (state == BST_CHECKED);
1062 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1064 return 0;
1069 static LRESULT
1070 DATETIME_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1072 TRACE("hwndbutton = %p\n", infoPtr->hwndCheckbut);
1073 if(infoPtr->hwndCheckbut == (HWND)lParam)
1074 return DATETIME_Button_Command(infoPtr, wParam, lParam);
1075 return 0;
1079 static LRESULT
1080 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
1082 TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
1083 if (bEnable)
1084 infoPtr->dwStyle &= ~WS_DISABLED;
1085 else
1086 infoPtr->dwStyle |= WS_DISABLED;
1088 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1090 return 0;
1094 static LRESULT
1095 DATETIME_EraseBackground (const DATETIME_INFO *infoPtr, HDC hdc)
1097 HBRUSH hBrush, hSolidBrush = NULL;
1098 RECT rc;
1100 if (infoPtr->dwStyle & WS_DISABLED)
1101 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
1102 else
1104 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
1105 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
1106 if (!hBrush)
1107 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
1110 GetClientRect (infoPtr->hwndSelf, &rc);
1112 FillRect (hdc, &rc, hBrush);
1114 if (hSolidBrush)
1115 DeleteObject(hSolidBrush);
1117 return -1;
1121 static LRESULT
1122 DATETIME_Notify (DATETIME_INFO *infoPtr, const NMHDR *lpnmh)
1124 TRACE ("Got notification %x from %p\n", lpnmh->code, lpnmh->hwndFrom);
1125 TRACE ("info: %p %p %p\n", infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hUpdown);
1127 if (lpnmh->code == MCN_SELECT) {
1128 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1129 infoPtr->dateValid = TRUE;
1130 SendMessageW (infoPtr->hMonthCal, MCM_GETCURSEL, 0, (LPARAM)&infoPtr->date);
1131 TRACE("got from calendar %04d/%02d/%02d day of week %d\n",
1132 infoPtr->date.wYear, infoPtr->date.wMonth, infoPtr->date.wDay, infoPtr->date.wDayOfWeek);
1133 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
1134 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1135 DATETIME_SendDateTimeChangeNotify (infoPtr);
1136 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1138 if ((lpnmh->hwndFrom == infoPtr->hUpdown) && (lpnmh->code == UDN_DELTAPOS)) {
1139 const NM_UPDOWN *lpnmud = (const NM_UPDOWN*)lpnmh;
1140 TRACE("Delta pos %d\n", lpnmud->iDelta);
1141 infoPtr->pendingUpdown = lpnmud->iDelta;
1143 return 0;
1147 static LRESULT
1148 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode)
1150 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1151 int wrap = 0;
1152 int new;
1154 if (!(infoPtr->haveFocus)) return 0;
1155 if ((fieldNum==0) && (infoPtr->select)) return 0;
1157 if (infoPtr->select & FORMATCALLMASK) {
1158 FIXME ("Callbacks not implemented yet\n");
1161 switch (vkCode) {
1162 case VK_ADD:
1163 case VK_UP:
1164 infoPtr->nCharsEntered = 0;
1165 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1166 DATETIME_SendDateTimeChangeNotify (infoPtr);
1167 break;
1168 case VK_SUBTRACT:
1169 case VK_DOWN:
1170 infoPtr->nCharsEntered = 0;
1171 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1172 DATETIME_SendDateTimeChangeNotify (infoPtr);
1173 break;
1174 case VK_HOME:
1175 infoPtr->nCharsEntered = 0;
1176 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MIN);
1177 DATETIME_SendDateTimeChangeNotify (infoPtr);
1178 break;
1179 case VK_END:
1180 infoPtr->nCharsEntered = 0;
1181 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MAX);
1182 DATETIME_SendDateTimeChangeNotify (infoPtr);
1183 break;
1184 case VK_LEFT:
1185 new = infoPtr->select;
1186 do {
1187 if (new == 0) {
1188 new = new - 1;
1189 wrap++;
1190 } else {
1191 new--;
1193 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1194 if (new != infoPtr->select)
1195 DATETIME_SetSelectedField(infoPtr, new);
1196 break;
1197 case VK_RIGHT:
1198 new = infoPtr->select;
1199 do {
1200 new++;
1201 if (new==infoPtr->nrFields) {
1202 new = 0;
1203 wrap++;
1205 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1206 if (new != infoPtr->select)
1207 DATETIME_SetSelectedField(infoPtr, new);
1208 break;
1211 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1213 return 0;
1217 static LRESULT
1218 DATETIME_Char (DATETIME_INFO *infoPtr, WPARAM vkCode)
1220 int fieldNum, fieldSpec;
1222 fieldNum = infoPtr->select & DTHT_DATEFIELD;
1223 fieldSpec = infoPtr->fieldspec[fieldNum];
1225 if (fieldSpec == ONELETTERAMPM || fieldSpec == TWOLETTERAMPM) {
1226 infoPtr->charsEntered[0] = vkCode;
1227 infoPtr->nCharsEntered = 1;
1229 DATETIME_ApplySelectedField(infoPtr);
1230 } else if (vkCode >= '0' && vkCode <= '9') {
1231 int maxChars;
1233 infoPtr->charsEntered[infoPtr->nCharsEntered++] = vkCode;
1235 if (fieldSpec == INVALIDFULLYEAR || fieldSpec == FULLYEAR)
1236 maxChars = 4;
1237 else
1238 maxChars = 2;
1240 if ((fieldSpec == ONEDIGIT12HOUR ||
1241 fieldSpec == TWODIGIT12HOUR ||
1242 fieldSpec == ONEDIGIT24HOUR ||
1243 fieldSpec == TWODIGIT24HOUR) &&
1244 (infoPtr->nCharsEntered == 1))
1246 if (vkCode >= '3')
1247 maxChars = 1;
1250 if (maxChars == infoPtr->nCharsEntered)
1251 DATETIME_ApplySelectedField(infoPtr);
1254 return 0;
1258 static LRESULT
1259 DATETIME_VScroll (DATETIME_INFO *infoPtr, WORD wScroll)
1261 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1263 if ((SHORT)LOWORD(wScroll) != SB_THUMBPOSITION) return 0;
1264 if (!(infoPtr->haveFocus)) return 0;
1265 if ((fieldNum==0) && (infoPtr->select)) return 0;
1267 if (infoPtr->pendingUpdown >= 0) {
1268 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1269 DATETIME_SendDateTimeChangeNotify (infoPtr);
1271 else {
1272 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1273 DATETIME_SendDateTimeChangeNotify (infoPtr);
1276 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1278 return 0;
1282 static LRESULT
1283 DATETIME_KillFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1285 TRACE("lost focus to %p\n", lostFocus);
1287 if (infoPtr->haveFocus) {
1288 DATETIME_SendSimpleNotify (infoPtr, NM_KILLFOCUS);
1289 infoPtr->haveFocus = 0;
1290 DATETIME_SetSelectedField (infoPtr, -1);
1293 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
1295 return 0;
1299 static LRESULT
1300 DATETIME_NCCreate (HWND hwnd, const CREATESTRUCTW *lpcs)
1302 DWORD dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1303 /* force control to have client edge */
1304 dwExStyle |= WS_EX_CLIENTEDGE;
1305 SetWindowLongW(hwnd, GWL_EXSTYLE, dwExStyle);
1307 return 1;
1310 static LRESULT DATETIME_NCPaint (HWND hwnd, HRGN region)
1312 INT cxEdge, cyEdge;
1313 HRGN clipRgn;
1314 HTHEME theme;
1315 LONG exStyle;
1316 RECT r;
1317 HDC dc;
1319 theme = OpenThemeData(NULL, WC_EDITW);
1320 if (!theme)
1321 return DefWindowProcW(hwnd, WM_NCPAINT, (WPARAM)region, 0);
1323 exStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1324 if (!(exStyle & WS_EX_CLIENTEDGE))
1326 CloseThemeData(theme);
1327 return DefWindowProcW(hwnd, WM_NCPAINT, (WPARAM)region, 0);
1330 cxEdge = GetSystemMetrics(SM_CXEDGE);
1331 cyEdge = GetSystemMetrics(SM_CYEDGE);
1332 GetWindowRect(hwnd, &r);
1334 /* New clipping region passed to default proc to exclude border */
1335 clipRgn = CreateRectRgn(r.left + cxEdge, r.top + cyEdge, r.right - cxEdge, r.bottom - cyEdge);
1336 if (region != (HRGN)1)
1337 CombineRgn(clipRgn, clipRgn, region, RGN_AND);
1338 OffsetRect(&r, -r.left, -r.top);
1340 dc = GetDCEx(hwnd, region, DCX_WINDOW | DCX_INTERSECTRGN);
1341 if (IsThemeBackgroundPartiallyTransparent(theme, 0, 0))
1342 DrawThemeParentBackground(hwnd, dc, &r);
1343 DrawThemeBackground(theme, dc, 0, 0, &r, 0);
1344 ReleaseDC(hwnd, dc);
1345 CloseThemeData(theme);
1347 /* Call default proc to get the scrollbars etc. also painted */
1348 DefWindowProcW(hwnd, WM_NCPAINT, (WPARAM)clipRgn, 0);
1349 DeleteObject(clipRgn);
1350 return 0;
1353 static LRESULT DATETIME_MouseMove (DATETIME_INFO *infoPtr, LONG x, LONG y)
1355 TRACKMOUSEEVENT event;
1356 POINT point;
1357 BOOL hot;
1359 point.x = x;
1360 point.y = y;
1361 hot = PtInRect(&infoPtr->calbutton, point);
1362 if (hot != infoPtr->bCalHot)
1364 infoPtr->bCalHot = hot;
1365 RedrawWindow(infoPtr->hwndSelf, &infoPtr->calbutton, 0, RDW_INVALIDATE | RDW_UPDATENOW);
1368 if (!hot)
1369 return 0;
1371 event.cbSize = sizeof(TRACKMOUSEEVENT);
1372 event.dwFlags = TME_QUERY;
1373 if (!TrackMouseEvent(&event) || event.hwndTrack != infoPtr->hwndSelf || !(event.dwFlags & TME_LEAVE))
1375 event.hwndTrack = infoPtr->hwndSelf;
1376 event.dwFlags = TME_LEAVE;
1377 TrackMouseEvent(&event);
1380 return 0;
1383 static LRESULT DATETIME_MouseLeave (DATETIME_INFO *infoPtr)
1385 infoPtr->bCalHot = FALSE;
1386 RedrawWindow(infoPtr->hwndSelf, &infoPtr->calbutton, 0, RDW_INVALIDATE | RDW_UPDATENOW);
1387 return 0;
1390 static LRESULT
1391 DATETIME_SetFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1393 TRACE("got focus from %p\n", lostFocus);
1395 /* if monthcal is open and it loses focus, close monthcal */
1396 if (infoPtr->hMonthCal && (lostFocus == infoPtr->hMonthCal) &&
1397 IsWindowVisible(infoPtr->hMonthCal))
1399 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1400 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1401 /* note: this get triggered even if monthcal loses focus to a dropdown
1402 * box click, which occurs without an intermediate WM_PAINT call
1404 infoPtr->bDropdownEnabled = FALSE;
1405 return 0;
1408 if (infoPtr->haveFocus == 0) {
1409 DATETIME_SendSimpleNotify (infoPtr, NM_SETFOCUS);
1410 infoPtr->haveFocus = DTHT_GOTFOCUS;
1413 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1415 return 0;
1419 static BOOL
1420 DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr)
1422 NMDATETIMECHANGE dtdtc;
1424 dtdtc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1425 dtdtc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1426 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1428 dtdtc.dwFlags = infoPtr->dateValid ? GDT_VALID : GDT_NONE;
1430 dtdtc.st = infoPtr->date;
1431 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1432 dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1436 static BOOL
1437 DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code)
1439 NMHDR nmhdr;
1441 TRACE("%x\n", code);
1442 nmhdr.hwndFrom = infoPtr->hwndSelf;
1443 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1444 nmhdr.code = code;
1446 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1447 nmhdr.idFrom, (LPARAM)&nmhdr);
1450 static LRESULT
1451 DATETIME_Size (DATETIME_INFO *infoPtr, INT width, INT height)
1453 /* set size */
1454 infoPtr->rcClient.bottom = height;
1455 infoPtr->rcClient.right = width;
1457 TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1459 infoPtr->rcDraw = infoPtr->rcClient;
1461 if (infoPtr->dwStyle & DTS_UPDOWN) {
1462 SetWindowPos(infoPtr->hUpdown, NULL,
1463 infoPtr->rcClient.right-14, 0,
1464 15, infoPtr->rcClient.bottom - infoPtr->rcClient.top,
1465 SWP_NOACTIVATE | SWP_NOZORDER);
1467 else {
1468 /* set the size of the button that drops the calendar down */
1469 /* FIXME: account for style that allows button on left side */
1470 infoPtr->calbutton.top = infoPtr->rcDraw.top;
1471 infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1472 infoPtr->calbutton.left = infoPtr->rcDraw.right-15;
1473 infoPtr->calbutton.right = infoPtr->rcDraw.right;
1476 /* set enable/disable button size for show none style being enabled */
1477 /* FIXME: these dimensions are completely incorrect */
1478 infoPtr->checkbox.top = infoPtr->rcDraw.top;
1479 infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1480 infoPtr->checkbox.left = infoPtr->rcDraw.left;
1481 infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1483 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1485 return 0;
1488 static LRESULT
1489 DATETIME_StyleChanging(DATETIME_INFO *infoPtr, WPARAM wStyleType, STYLESTRUCT *lpss)
1491 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1492 wStyleType, lpss->styleOld, lpss->styleNew);
1494 /* block DTS_SHOWNONE change */
1495 if ((lpss->styleNew ^ lpss->styleOld) & DTS_SHOWNONE)
1497 if (lpss->styleOld & DTS_SHOWNONE)
1498 lpss->styleNew |= DTS_SHOWNONE;
1499 else
1500 lpss->styleNew &= ~DTS_SHOWNONE;
1503 return 0;
1506 static LRESULT
1507 DATETIME_StyleChanged(DATETIME_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
1509 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1510 wStyleType, lpss->styleOld, lpss->styleNew);
1512 if (wStyleType != GWL_STYLE) return 0;
1514 infoPtr->dwStyle = lpss->styleNew;
1516 if ( !(lpss->styleOld & DTS_SHOWNONE) && (lpss->styleNew & DTS_SHOWNONE) ) {
1517 infoPtr->hwndCheckbut = CreateWindowExW (0, WC_BUTTONW, 0, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1518 2, 2, 13, 13, infoPtr->hwndSelf, 0,
1519 (HINSTANCE)GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_HINSTANCE), 0);
1520 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, infoPtr->dateValid ? 1 : 0, 0);
1522 if ( (lpss->styleOld & DTS_SHOWNONE) && !(lpss->styleNew & DTS_SHOWNONE) ) {
1523 DestroyWindow(infoPtr->hwndCheckbut);
1524 infoPtr->hwndCheckbut = 0;
1526 if ( !(lpss->styleOld & DTS_UPDOWN) && (lpss->styleNew & DTS_UPDOWN) ) {
1527 infoPtr->hUpdown = CreateUpDownControl (WS_CHILD | WS_BORDER | WS_VISIBLE, 120, 1, 20, 20,
1528 infoPtr->hwndSelf, 1, 0, 0, UD_MAXVAL, UD_MINVAL, 0);
1530 if ( (lpss->styleOld & DTS_UPDOWN) && !(lpss->styleNew & DTS_UPDOWN) ) {
1531 DestroyWindow(infoPtr->hUpdown);
1532 infoPtr->hUpdown = 0;
1535 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1536 return 0;
1539 static LRESULT DATETIME_ThemeChanged (DATETIME_INFO *infoPtr)
1541 HTHEME theme;
1543 theme = GetWindowTheme(infoPtr->hwndSelf);
1544 CloseThemeData(theme);
1545 OpenThemeData(infoPtr->hwndSelf, themeClass);
1546 return 0;
1549 static BOOL DATETIME_GetIdealSize(DATETIME_INFO *infoPtr, SIZE *size)
1551 SIZE field_size;
1552 RECT rect;
1553 WCHAR txt[80];
1554 HDC hdc;
1555 HFONT oldFont;
1556 int i;
1558 size->cx = size->cy = 0;
1560 hdc = GetDC(infoPtr->hwndSelf);
1561 oldFont = SelectObject(hdc, infoPtr->hFont);
1563 /* Get text font height */
1564 DATETIME_ReturnTxt(infoPtr, 0, txt, ARRAY_SIZE(txt));
1565 GetTextExtentPoint32W(hdc, txt, lstrlenW(txt), &field_size);
1566 size->cy = field_size.cy;
1568 /* Get text font width */
1569 for (i = 0; i < infoPtr->nrFields; i++)
1571 size->cx += DATETIME_GetFieldWidth(infoPtr, hdc, i);
1574 SelectObject(hdc, oldFont);
1575 ReleaseDC(infoPtr->hwndSelf, hdc);
1577 if (infoPtr->dwStyle & DTS_UPDOWN)
1579 GetWindowRect(infoPtr->hUpdown, &rect);
1580 size->cx += rect.right - rect.left;
1582 else
1584 size->cx += infoPtr->calbutton.right - infoPtr->calbutton.left;
1587 if (infoPtr->dwStyle & DTS_SHOWNONE)
1589 size->cx += infoPtr->checkbox.right - infoPtr->checkbox.left;
1592 /* Add space between controls for them not to get too close */
1593 size->cx += 12;
1594 size->cy += 4;
1596 TRACE("cx=%d cy=%d\n", size->cx, size->cy);
1597 return TRUE;
1600 static LRESULT
1601 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1603 infoPtr->hFont = font;
1604 if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1605 return 0;
1609 static LRESULT
1610 DATETIME_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1612 DATETIME_INFO *infoPtr = Alloc (sizeof(DATETIME_INFO));
1613 STYLESTRUCT ss = { 0, lpcs->style };
1615 if (!infoPtr) return -1;
1617 infoPtr->hwndSelf = hwnd;
1618 infoPtr->dwStyle = lpcs->style;
1620 infoPtr->nrFieldsAllocated = 32;
1621 infoPtr->fieldspec = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1622 infoPtr->fieldRect = Alloc (infoPtr->nrFieldsAllocated * sizeof(RECT));
1623 infoPtr->buflen = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1624 infoPtr->hwndNotify = lpcs->hwndParent;
1625 infoPtr->select = -1; /* initially, nothing is selected */
1626 infoPtr->bDropdownEnabled = TRUE;
1627 infoPtr->bCalHot = FALSE;
1629 DATETIME_StyleChanged(infoPtr, GWL_STYLE, &ss);
1630 DATETIME_SetFormatW (infoPtr, 0);
1632 /* create the monthcal control */
1633 infoPtr->hMonthCal = CreateWindowExW (0, MONTHCAL_CLASSW, 0, WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS,
1634 0, 0, 0, 0, infoPtr->hwndSelf, 0, 0, 0);
1636 /* initialize info structure */
1637 GetLocalTime (&infoPtr->date);
1638 infoPtr->dateValid = TRUE;
1639 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1641 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1642 OpenThemeData(hwnd, themeClass);
1644 return 0;
1649 static LRESULT
1650 DATETIME_Destroy (DATETIME_INFO *infoPtr)
1652 HTHEME theme;
1654 theme = GetWindowTheme(infoPtr->hwndSelf);
1655 CloseThemeData(theme);
1657 if (infoPtr->hwndCheckbut)
1658 DestroyWindow(infoPtr->hwndCheckbut);
1659 if (infoPtr->hUpdown)
1660 DestroyWindow(infoPtr->hUpdown);
1661 if (infoPtr->hMonthCal)
1662 DestroyWindow(infoPtr->hMonthCal);
1663 SetWindowLongPtrW( infoPtr->hwndSelf, 0, 0 ); /* clear infoPtr */
1664 Free (infoPtr->buflen);
1665 Free (infoPtr->fieldRect);
1666 Free (infoPtr->fieldspec);
1667 Free (infoPtr);
1668 return 0;
1672 static INT
1673 DATETIME_GetText (const DATETIME_INFO *infoPtr, INT count, LPWSTR dst)
1675 WCHAR buf[80];
1676 int i;
1678 if (!dst || (count <= 0)) return 0;
1680 dst[0] = 0;
1681 for (i = 0; i < infoPtr->nrFields; i++)
1683 DATETIME_ReturnTxt(infoPtr, i, buf, ARRAY_SIZE(buf));
1684 if ((lstrlenW(dst) + lstrlenW(buf)) < count)
1685 lstrcatW(dst, buf);
1686 else break;
1688 return lstrlenW(dst);
1691 static int DATETIME_GetTextLength(const DATETIME_INFO *info)
1693 int i, length = 0;
1694 WCHAR buffer[80];
1696 TRACE("%p.\n", info);
1698 for (i = 0; i < info->nrFields; i++)
1700 DATETIME_ReturnTxt(info, i, buffer, ARRAY_SIZE(buffer));
1701 length += lstrlenW(buffer);
1703 return length;
1706 static LRESULT WINAPI
1707 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1709 DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1711 TRACE ("%x, %lx, %lx\n", uMsg, wParam, lParam);
1713 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
1714 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
1716 switch (uMsg) {
1718 case DTM_GETSYSTEMTIME:
1719 return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1721 case DTM_SETSYSTEMTIME:
1722 return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1724 case DTM_GETRANGE:
1725 return SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1727 case DTM_SETRANGE:
1728 return SendMessageW (infoPtr->hMonthCal, MCM_SETRANGE, wParam, lParam);
1730 case DTM_SETFORMATA:
1731 return DATETIME_SetFormatA (infoPtr, (LPCSTR)lParam);
1733 case DTM_SETFORMATW:
1734 return DATETIME_SetFormatW (infoPtr, (LPCWSTR)lParam);
1736 case DTM_GETMONTHCAL:
1737 return (LRESULT)infoPtr->hMonthCal;
1739 case DTM_SETMCCOLOR:
1740 return SendMessageW (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
1742 case DTM_GETMCCOLOR:
1743 return SendMessageW (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
1745 case DTM_SETMCFONT:
1746 return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1748 case DTM_GETMCFONT:
1749 return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1751 case DTM_GETIDEALSIZE:
1752 return DATETIME_GetIdealSize(infoPtr, (SIZE *)lParam);
1754 case WM_NOTIFY:
1755 return DATETIME_Notify (infoPtr, (LPNMHDR)lParam);
1757 case WM_ENABLE:
1758 return DATETIME_Enable (infoPtr, (BOOL)wParam);
1760 case WM_ERASEBKGND:
1761 return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1763 case WM_GETDLGCODE:
1764 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1766 case WM_PRINTCLIENT:
1767 case WM_PAINT:
1768 return DATETIME_Paint (infoPtr, (HDC)wParam);
1770 case WM_KEYDOWN:
1771 return DATETIME_KeyDown (infoPtr, wParam);
1773 case WM_CHAR:
1774 return DATETIME_Char (infoPtr, wParam);
1776 case WM_KILLFOCUS:
1777 return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1779 case WM_NCCREATE:
1780 return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1782 case WM_NCPAINT:
1783 return DATETIME_NCPaint(hwnd, (HRGN)wParam);
1785 case WM_MOUSEMOVE:
1786 return DATETIME_MouseMove(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1788 case WM_MOUSELEAVE:
1789 return DATETIME_MouseLeave(infoPtr);
1791 case WM_SETFOCUS:
1792 return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1794 case WM_SIZE:
1795 return DATETIME_Size (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1797 case WM_LBUTTONDOWN:
1798 return DATETIME_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1800 case WM_LBUTTONUP:
1801 return DATETIME_LButtonUp (infoPtr);
1803 case WM_VSCROLL:
1804 return DATETIME_VScroll (infoPtr, (WORD)wParam);
1806 case WM_CREATE:
1807 return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1809 case WM_DESTROY:
1810 return DATETIME_Destroy (infoPtr);
1812 case WM_COMMAND:
1813 return DATETIME_Command (infoPtr, wParam, lParam);
1815 case WM_STYLECHANGING:
1816 return DATETIME_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1818 case WM_STYLECHANGED:
1819 return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1821 case WM_THEMECHANGED:
1822 return DATETIME_ThemeChanged(infoPtr);
1824 case WM_SETFONT:
1825 return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1827 case WM_GETFONT:
1828 return (LRESULT) infoPtr->hFont;
1830 case WM_GETTEXT:
1831 return (LRESULT) DATETIME_GetText(infoPtr, wParam, (LPWSTR)lParam);
1833 case WM_GETTEXTLENGTH:
1834 return (LRESULT)DATETIME_GetTextLength(infoPtr);
1836 case WM_SETTEXT:
1837 return CB_ERR;
1839 default:
1840 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
1841 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
1842 uMsg, wParam, lParam);
1843 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1848 void
1849 DATETIME_Register (void)
1851 WNDCLASSW wndClass;
1853 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1854 wndClass.style = CS_GLOBALCLASS;
1855 wndClass.lpfnWndProc = DATETIME_WindowProc;
1856 wndClass.cbClsExtra = 0;
1857 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1858 wndClass.hCursor = LoadCursorW (0, (LPCWSTR)IDC_ARROW);
1859 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1860 wndClass.lpszClassName = DATETIMEPICK_CLASSW;
1862 RegisterClassW (&wndClass);
1866 void
1867 DATETIME_Unregister (void)
1869 UnregisterClassW (DATETIMEPICK_CLASSW, NULL);