dxgi/tests: Introduce an ARRAY_SIZE macro.
[wine.git] / dlls / comctl32 / datetime.c
blob43c64e73345623f507938a39accf955be8fa650e
1 /*
2 * Date and time picker control
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1999, 2000 Alex Priem <alexp@sci.kun.nl>
6 * Copyright 2000 Chris Morgan <cmorgan@wpi.edu>
7 * Copyright 2012 Owen Rudge for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * TODO:
24 * -- DTS_APPCANPARSE
25 * -- DTS_SHORTDATECENTURYFORMAT
26 * -- DTN_FORMAT
27 * -- DTN_FORMATQUERY
28 * -- DTN_USERSTRING
29 * -- DTN_WMKEYDOWN
30 * -- FORMATCALLBACK
33 #include <math.h>
34 #include <string.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <limits.h>
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "winnls.h"
44 #include "commctrl.h"
45 #include "comctl32.h"
46 #include "wine/debug.h"
47 #include "wine/unicode.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(datetime);
51 typedef struct
53 HWND hwndSelf;
54 HWND hMonthCal;
55 HWND hwndNotify;
56 HWND hUpdown;
57 DWORD dwStyle;
58 SYSTEMTIME date;
59 BOOL dateValid;
60 HWND hwndCheckbut;
61 RECT rcClient; /* rect around the edge of the window */
62 RECT rcDraw; /* rect inside of the border */
63 RECT checkbox; /* checkbox allowing the control to be enabled/disabled */
64 RECT calbutton; /* button that toggles the dropdown of the monthcal control */
65 BOOL bCalDepressed; /* TRUE = cal button is depressed */
66 BOOL bDropdownEnabled;
67 int select;
68 WCHAR charsEntered[4];
69 int nCharsEntered;
70 HFONT hFont;
71 int nrFieldsAllocated;
72 int nrFields;
73 int haveFocus;
74 int *fieldspec;
75 RECT *fieldRect;
76 int *buflen;
77 WCHAR textbuf[256];
78 POINT monthcal_pos;
79 int pendingUpdown;
80 } DATETIME_INFO, *LPDATETIME_INFO;
82 /* in monthcal.c */
83 extern int MONTHCAL_MonthLength(int month, int year);
84 extern int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace);
86 /* this list of defines is closely related to `allowedformatchars' defined
87 * in datetime.c; the high nibble indicates the `base type' of the format
88 * specifier.
89 * Do not change without first reading DATETIME_UseFormat.
93 #define DT_END_FORMAT 0
94 #define ONEDIGITDAY 0x01
95 #define TWODIGITDAY 0x02
96 #define THREECHARDAY 0x03
97 #define FULLDAY 0x04
98 #define ONEDIGIT12HOUR 0x11
99 #define TWODIGIT12HOUR 0x12
100 #define ONEDIGIT24HOUR 0x21
101 #define TWODIGIT24HOUR 0x22
102 #define ONEDIGITMINUTE 0x31
103 #define TWODIGITMINUTE 0x32
104 #define ONEDIGITMONTH 0x41
105 #define TWODIGITMONTH 0x42
106 #define THREECHARMONTH 0x43
107 #define FULLMONTH 0x44
108 #define ONEDIGITSECOND 0x51
109 #define TWODIGITSECOND 0x52
110 #define ONELETTERAMPM 0x61
111 #define TWOLETTERAMPM 0x62
112 #define ONEDIGITYEAR 0x71
113 #define TWODIGITYEAR 0x72
114 #define INVALIDFULLYEAR 0x73 /* FIXME - yyy is not valid - we'll treat it as yyyy */
115 #define FULLYEAR 0x74
116 #define FORMATCALLBACK 0x81 /* -> maximum of 0x80 callbacks possible */
117 #define FORMATCALLMASK 0x80
118 #define DT_STRING 0x0100
120 #define DTHT_DATEFIELD 0xff /* for hit-testing */
122 #define DTHT_NONE 0x1000
123 #define DTHT_CHECKBOX 0x2000 /* these should end at '00' , to make */
124 #define DTHT_MCPOPUP 0x3000 /* & DTHT_DATEFIELD 0 when DATETIME_KeyDown */
125 #define DTHT_GOTFOCUS 0x4000 /* tests for date-fields */
126 #define DTHT_NODATEMASK 0xf000 /* to mask check and drop down from others */
128 static BOOL DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code);
129 static BOOL DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr);
130 static const WCHAR allowedformatchars[] = {'d', 'h', 'H', 'm', 'M', 's', 't', 'y', 'X', 0};
131 static const int maxrepetition [] = {4,2,2,2,4,2,2,4,-1};
133 /* valid date limits */
134 static const SYSTEMTIME max_allowed_date = { /* wYear */ 9999, /* wMonth */ 12, /* wDayOfWeek */ 0, /* wDay */ 31 };
135 static const SYSTEMTIME min_allowed_date = { /* wYear */ 1752, /* wMonth */ 9, /* wDayOfWeek */ 0, /* wDay */ 14 };
137 static DWORD
138 DATETIME_GetSystemTime (const DATETIME_INFO *infoPtr, SYSTEMTIME *systime)
140 if (!systime) return GDT_NONE;
142 if ((infoPtr->dwStyle & DTS_SHOWNONE) &&
143 (SendMessageW (infoPtr->hwndCheckbut, BM_GETCHECK, 0, 0) == BST_UNCHECKED))
144 return GDT_NONE;
146 *systime = infoPtr->date;
148 return GDT_VALID;
151 /* Checks value is within configured date range
153 * PARAMETERS
155 * [I] infoPtr : valid pointer to control data
156 * [I] date : pointer to valid date data to check
158 * RETURN VALUE
160 * TRUE - date within configured range
161 * FALSE - date is outside configured range
163 static BOOL DATETIME_IsDateInValidRange(const DATETIME_INFO *infoPtr, const SYSTEMTIME *date)
165 SYSTEMTIME range[2];
166 DWORD limits;
168 if ((MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) ||
169 (MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1))
170 return FALSE;
172 limits = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, 0, (LPARAM)range);
174 if (limits & GDTR_MAX)
176 if (MONTHCAL_CompareSystemTime(date, &range[1]) == 1)
177 return FALSE;
180 if (limits & GDTR_MIN)
182 if (MONTHCAL_CompareSystemTime(date, &range[0]) == -1)
183 return FALSE;
186 return TRUE;
189 static BOOL
190 DATETIME_SetSystemTime (DATETIME_INFO *infoPtr, DWORD flag, const SYSTEMTIME *systime)
192 if (!systime) return FALSE;
194 TRACE("%04d/%02d/%02d %02d:%02d:%02d\n",
195 systime->wYear, systime->wMonth, systime->wDay,
196 systime->wHour, systime->wMinute, systime->wSecond);
198 if (flag == GDT_VALID) {
199 if (systime->wYear == 0 ||
200 systime->wMonth < 1 || systime->wMonth > 12 ||
201 systime->wDay < 1 ||
202 systime->wDay > MONTHCAL_MonthLength(systime->wMonth, systime->wYear) ||
203 systime->wHour > 23 ||
204 systime->wMinute > 59 ||
205 systime->wSecond > 59 ||
206 systime->wMilliseconds > 999
208 return FALSE;
210 /* Windows returns true if the date is valid but outside the limits set */
211 if (!DATETIME_IsDateInValidRange(infoPtr, systime))
212 return TRUE;
214 infoPtr->dateValid = TRUE;
215 infoPtr->date = *systime;
216 /* always store a valid day of week */
217 MONTHCAL_CalculateDayOfWeek(&infoPtr->date, TRUE);
219 SendMessageW (infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
220 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
221 } else if ((infoPtr->dwStyle & DTS_SHOWNONE) && (flag == GDT_NONE)) {
222 infoPtr->dateValid = FALSE;
223 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_UNCHECKED, 0);
225 else
226 return FALSE;
228 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
229 return TRUE;
233 /***
234 * Split up a formattxt in actions.
235 * See ms documentation for the meaning of the letter codes/'specifiers'.
237 * Notes:
238 * *'dddddd' is handled as 'dddd' plus 'dd'.
239 * *unrecognized formats are strings (here given the type DT_STRING;
240 * start of the string is encoded in lower bits of DT_STRING.
241 * Therefore, 'string' ends finally up as '<show seconds>tring'.
244 static void
245 DATETIME_UseFormat (DATETIME_INFO *infoPtr, LPCWSTR formattxt)
247 unsigned int i;
248 int j, k, len;
249 BOOL inside_literal = FALSE; /* inside '...' */
250 int *nrFields = &infoPtr->nrFields;
252 *nrFields = 0;
253 infoPtr->fieldspec[*nrFields] = 0;
254 len = strlenW(allowedformatchars);
255 k = 0;
257 for (i = 0; formattxt[i]; i++) {
258 TRACE ("\n%d %c:", i, formattxt[i]);
259 if (!inside_literal) {
260 for (j = 0; j < len; j++) {
261 if (allowedformatchars[j]==formattxt[i]) {
262 TRACE ("%c[%d,%x]", allowedformatchars[j], *nrFields, infoPtr->fieldspec[*nrFields]);
263 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
264 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
265 break;
267 if (infoPtr->fieldspec[*nrFields] >> 4 != j) {
268 (*nrFields)++;
269 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
270 break;
272 if ((infoPtr->fieldspec[*nrFields] & 0x0f) == maxrepetition[j]) {
273 (*nrFields)++;
274 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
275 break;
277 infoPtr->fieldspec[*nrFields]++;
278 break;
279 } /* if allowedformatchar */
280 } /* for j */
282 else
283 j = len;
285 if (formattxt[i] == '\'')
287 inside_literal = !inside_literal;
288 continue;
291 /* char is not a specifier: handle char like a string */
292 if (j == len) {
293 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
294 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
295 infoPtr->buflen[*nrFields] = 0;
296 } else if ((infoPtr->fieldspec[*nrFields] & DT_STRING) != DT_STRING) {
297 (*nrFields)++;
298 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
299 infoPtr->buflen[*nrFields] = 0;
301 infoPtr->textbuf[k] = formattxt[i];
302 k++;
303 infoPtr->buflen[*nrFields]++;
304 } /* if j=len */
306 if (*nrFields == infoPtr->nrFieldsAllocated) {
307 FIXME ("out of memory; should reallocate. crash ahead.\n");
309 } /* for i */
311 TRACE("\n");
313 if (infoPtr->fieldspec[*nrFields] != 0) (*nrFields)++;
317 static BOOL
318 DATETIME_SetFormatW (DATETIME_INFO *infoPtr, LPCWSTR format)
320 WCHAR format_buf[80];
322 if (!format) {
323 DWORD format_item;
325 if (infoPtr->dwStyle & DTS_LONGDATEFORMAT)
326 format_item = LOCALE_SLONGDATE;
327 else if ((infoPtr->dwStyle & DTS_TIMEFORMAT) == DTS_TIMEFORMAT)
328 format_item = LOCALE_STIMEFORMAT;
329 else /* DTS_SHORTDATEFORMAT */
330 format_item = LOCALE_SSHORTDATE;
331 GetLocaleInfoW(LOCALE_USER_DEFAULT, format_item, format_buf, sizeof(format_buf)/sizeof(format_buf[0]));
332 format = format_buf;
335 DATETIME_UseFormat (infoPtr, format);
336 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
338 return TRUE;
342 static BOOL
343 DATETIME_SetFormatA (DATETIME_INFO *infoPtr, LPCSTR lpszFormat)
345 if (lpszFormat) {
346 BOOL retval;
347 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, NULL, 0);
348 LPWSTR wstr = Alloc(len * sizeof(WCHAR));
349 if (wstr) MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, wstr, len);
350 retval = DATETIME_SetFormatW (infoPtr, wstr);
351 Free (wstr);
352 return retval;
354 else
355 return DATETIME_SetFormatW (infoPtr, 0);
360 static void
361 DATETIME_ReturnTxt (const DATETIME_INFO *infoPtr, int count, LPWSTR result, int resultSize)
363 static const WCHAR fmt_dW[] = { '%', 'd', 0 };
364 static const WCHAR fmt__2dW[] = { '%', '.', '2', 'd', 0 };
365 static const WCHAR fmt__3sW[] = { '%', '.', '3', 's', 0 };
366 SYSTEMTIME date = infoPtr->date;
367 int spec;
368 WCHAR buffer[80];
370 *result=0;
371 TRACE ("%d,%d\n", infoPtr->nrFields, count);
372 if (count>infoPtr->nrFields || count < 0) {
373 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
374 return;
377 if (!infoPtr->fieldspec) return;
379 spec = infoPtr->fieldspec[count];
380 if (spec & DT_STRING) {
381 int txtlen = infoPtr->buflen[count];
383 if (txtlen > resultSize)
384 txtlen = resultSize - 1;
385 memcpy (result, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
386 result[txtlen] = 0;
387 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
388 return;
392 switch (spec) {
393 case DT_END_FORMAT:
394 *result = 0;
395 break;
396 case ONEDIGITDAY:
397 wsprintfW (result, fmt_dW, date.wDay);
398 break;
399 case TWODIGITDAY:
400 wsprintfW (result, fmt__2dW, date.wDay);
401 break;
402 case THREECHARDAY:
403 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1+(date.wDayOfWeek+6)%7, result, 4);
404 /*wsprintfW (result,"%.3s",days[date.wDayOfWeek]);*/
405 break;
406 case FULLDAY:
407 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME1+(date.wDayOfWeek+6)%7, result, resultSize);
408 break;
409 case ONEDIGIT12HOUR:
410 if (date.wHour == 0) {
411 result[0] = '1';
412 result[1] = '2';
413 result[2] = 0;
415 else
416 wsprintfW (result, fmt_dW, date.wHour - (date.wHour > 12 ? 12 : 0));
417 break;
418 case TWODIGIT12HOUR:
419 if (date.wHour == 0) {
420 result[0] = '1';
421 result[1] = '2';
422 result[2] = 0;
424 else
425 wsprintfW (result, fmt__2dW, date.wHour - (date.wHour > 12 ? 12 : 0));
426 break;
427 case ONEDIGIT24HOUR:
428 wsprintfW (result, fmt_dW, date.wHour);
429 break;
430 case TWODIGIT24HOUR:
431 wsprintfW (result, fmt__2dW, date.wHour);
432 break;
433 case ONEDIGITSECOND:
434 wsprintfW (result, fmt_dW, date.wSecond);
435 break;
436 case TWODIGITSECOND:
437 wsprintfW (result, fmt__2dW, date.wSecond);
438 break;
439 case ONEDIGITMINUTE:
440 wsprintfW (result, fmt_dW, date.wMinute);
441 break;
442 case TWODIGITMINUTE:
443 wsprintfW (result, fmt__2dW, date.wMinute);
444 break;
445 case ONEDIGITMONTH:
446 wsprintfW (result, fmt_dW, date.wMonth);
447 break;
448 case TWODIGITMONTH:
449 wsprintfW (result, fmt__2dW, date.wMonth);
450 break;
451 case THREECHARMONTH:
452 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+date.wMonth -1,
453 buffer, sizeof(buffer)/sizeof(buffer[0]));
454 wsprintfW (result, fmt__3sW, buffer);
455 break;
456 case FULLMONTH:
457 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+date.wMonth -1,
458 result, resultSize);
459 break;
460 case ONELETTERAMPM:
461 result[0] = (date.wHour < 12 ? 'A' : 'P');
462 result[1] = 0;
463 break;
464 case TWOLETTERAMPM:
465 result[0] = (date.wHour < 12 ? 'A' : 'P');
466 result[1] = 'M';
467 result[2] = 0;
468 break;
469 case FORMATCALLBACK:
470 FIXME ("Not implemented\n");
471 result[0] = 'x';
472 result[1] = 0;
473 break;
474 case ONEDIGITYEAR:
475 wsprintfW (result, fmt_dW, date.wYear-10* (int) floor(date.wYear/10));
476 break;
477 case TWODIGITYEAR:
478 wsprintfW (result, fmt__2dW, date.wYear-100* (int) floor(date.wYear/100));
479 break;
480 case INVALIDFULLYEAR:
481 case FULLYEAR:
482 wsprintfW (result, fmt_dW, date.wYear);
483 break;
486 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
489 static int wrap(int val, int delta, int minVal, int maxVal)
491 val += delta;
492 if (delta == INT_MIN || val < minVal) return maxVal;
493 if (delta == INT_MAX || val > maxVal) return minVal;
494 return val;
497 static void
498 DATETIME_IncreaseField (DATETIME_INFO *infoPtr, int number, int delta)
500 SYSTEMTIME *date = &infoPtr->date;
501 SYSTEMTIME range[2];
502 DWORD limits;
503 BOOL min;
505 TRACE ("%d\n", number);
506 if ((number > infoPtr->nrFields) || (number < 0)) return;
508 if ((infoPtr->fieldspec[number] & DTHT_DATEFIELD) == 0) return;
510 switch (infoPtr->fieldspec[number]) {
511 case ONEDIGITYEAR:
512 case TWODIGITYEAR:
513 case FULLYEAR:
514 if (delta == INT_MIN)
515 date->wYear = 1752;
516 else if (delta == INT_MAX)
517 date->wYear = 9999;
518 else
519 date->wYear = max(min(date->wYear + delta, 9999), 1752);
521 if (date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
522 /* This can happen when moving away from a leap year. */
523 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear);
524 MONTHCAL_CalculateDayOfWeek(date, TRUE);
525 break;
526 case ONEDIGITMONTH:
527 case TWODIGITMONTH:
528 case THREECHARMONTH:
529 case FULLMONTH:
530 date->wMonth = wrap(date->wMonth, delta, 1, 12);
531 MONTHCAL_CalculateDayOfWeek(date, TRUE);
532 delta = 0;
533 /* fall through */
534 case ONEDIGITDAY:
535 case TWODIGITDAY:
536 case THREECHARDAY:
537 case FULLDAY:
538 date->wDay = wrap(date->wDay, delta, 1, MONTHCAL_MonthLength(date->wMonth, date->wYear));
539 MONTHCAL_CalculateDayOfWeek(date, TRUE);
540 break;
541 case ONELETTERAMPM:
542 case TWOLETTERAMPM:
543 delta *= 12;
544 /* fall through */
545 case ONEDIGIT12HOUR:
546 case TWODIGIT12HOUR:
547 case ONEDIGIT24HOUR:
548 case TWODIGIT24HOUR:
549 date->wHour = wrap(date->wHour, delta, 0, 23);
550 break;
551 case ONEDIGITMINUTE:
552 case TWODIGITMINUTE:
553 date->wMinute = wrap(date->wMinute, delta, 0, 59);
554 break;
555 case ONEDIGITSECOND:
556 case TWODIGITSECOND:
557 date->wSecond = wrap(date->wSecond, delta, 0, 59);
558 break;
559 case FORMATCALLBACK:
560 FIXME ("Not implemented\n");
561 break;
564 /* FYI: On 1752/9/14 the calendar changed and England and the
565 * American colonies changed to the Gregorian calendar. This change
566 * involved having September 14th follow September 2nd. So no date
567 * algorithm works before that date.
569 if (10000 * date->wYear + 100 * date->wMonth + date->wDay < 17520914) {
570 date->wYear = 1752;
571 date->wMonth = 9;
572 date->wDay = 14;
573 date->wSecond = 0;
574 date->wMinute = 0;
575 date->wHour = 0;
578 /* Ensure time is within bounds */
579 limits = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, 0, (LPARAM)range);
580 min = delta < 0;
582 if (limits & (min ? GDTR_MIN : GDTR_MAX))
584 int i = (min ? 0 : 1);
586 if (MONTHCAL_CompareSystemTime(date, &range[i]) == (min ? -1 : 1))
588 date->wYear = range[i].wYear;
589 date->wMonth = range[i].wMonth;
590 date->wDayOfWeek = range[i].wDayOfWeek;
591 date->wDay = range[i].wDay;
592 date->wHour = range[i].wHour;
593 date->wMinute = range[i].wMinute;
594 date->wSecond = range[i].wSecond;
595 date->wMilliseconds = range[i].wMilliseconds;
600 static void
601 DATETIME_ReturnFieldWidth (const DATETIME_INFO *infoPtr, HDC hdc, int count, SHORT *width)
603 /* fields are a fixed width, determined by the largest possible string */
604 /* presumably, these widths should be language dependent */
605 static const WCHAR fld_d1W[] = { '2', 0 };
606 static const WCHAR fld_d2W[] = { '2', '2', 0 };
607 static const WCHAR fld_d4W[] = { '2', '2', '2', '2', 0 };
608 static const WCHAR fld_am1[] = { 'A', 0 };
609 static const WCHAR fld_am2[] = { 'A', 'M', 0 };
610 int spec;
611 WCHAR buffer[80];
612 LPCWSTR bufptr;
613 SIZE size;
615 TRACE ("%d,%d\n", infoPtr->nrFields, count);
616 if (count>infoPtr->nrFields || count < 0) {
617 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
618 return;
621 if (!infoPtr->fieldspec) return;
623 spec = infoPtr->fieldspec[count];
624 if (spec & DT_STRING) {
625 int txtlen = infoPtr->buflen[count];
627 if (txtlen > 79)
628 txtlen = 79;
629 memcpy (buffer, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
630 buffer[txtlen] = 0;
631 bufptr = buffer;
633 else {
634 switch (spec) {
635 case ONEDIGITDAY:
636 case ONEDIGIT12HOUR:
637 case ONEDIGIT24HOUR:
638 case ONEDIGITSECOND:
639 case ONEDIGITMINUTE:
640 case ONEDIGITMONTH:
641 case ONEDIGITYEAR:
642 /* these seem to use a two byte field */
643 case TWODIGITDAY:
644 case TWODIGIT12HOUR:
645 case TWODIGIT24HOUR:
646 case TWODIGITSECOND:
647 case TWODIGITMINUTE:
648 case TWODIGITMONTH:
649 case TWODIGITYEAR:
650 bufptr = fld_d2W;
651 break;
652 case INVALIDFULLYEAR:
653 case FULLYEAR:
654 bufptr = fld_d4W;
655 break;
656 case THREECHARMONTH:
657 case FULLMONTH:
658 case THREECHARDAY:
659 case FULLDAY:
661 static const WCHAR fld_day[] = {'W','e','d','n','e','s','d','a','y',0};
662 static const WCHAR fld_abbrday[] = {'W','e','d',0};
663 static const WCHAR fld_mon[] = {'S','e','p','t','e','m','b','e','r',0};
664 static const WCHAR fld_abbrmon[] = {'D','e','c',0};
666 const WCHAR *fall;
667 LCTYPE lctype;
668 INT i, max_count;
669 LONG cx;
671 /* choose locale data type and fallback string */
672 switch (spec) {
673 case THREECHARDAY:
674 fall = fld_abbrday;
675 lctype = LOCALE_SABBREVDAYNAME1;
676 max_count = 7;
677 break;
678 case FULLDAY:
679 fall = fld_day;
680 lctype = LOCALE_SDAYNAME1;
681 max_count = 7;
682 break;
683 case THREECHARMONTH:
684 fall = fld_abbrmon;
685 lctype = LOCALE_SABBREVMONTHNAME1;
686 max_count = 12;
687 break;
688 default: /* FULLMONTH */
689 fall = fld_mon;
690 lctype = LOCALE_SMONTHNAME1;
691 max_count = 12;
692 break;
695 cx = 0;
696 for (i = 0; i < max_count; i++)
698 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, lctype + i,
699 buffer, lstrlenW(buffer)))
701 GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &size);
702 if (size.cx > cx) cx = size.cx;
704 else /* locale independent fallback on failure */
706 GetTextExtentPoint32W(hdc, fall, lstrlenW(fall), &size);
707 cx = size.cx;
708 break;
711 *width = cx;
712 return;
714 case ONELETTERAMPM:
715 bufptr = fld_am1;
716 break;
717 case TWOLETTERAMPM:
718 bufptr = fld_am2;
719 break;
720 default:
721 bufptr = fld_d1W;
722 break;
725 GetTextExtentPoint32W (hdc, bufptr, strlenW(bufptr), &size);
726 *width = size.cx;
729 static void
730 DATETIME_Refresh (DATETIME_INFO *infoPtr, HDC hdc)
732 TRACE("\n");
734 if (infoPtr->dateValid) {
735 int i, prevright;
736 RECT *field;
737 RECT *rcDraw = &infoPtr->rcDraw;
738 SIZE size;
739 COLORREF oldTextColor;
740 SHORT fieldWidth = 0;
741 HFONT oldFont = SelectObject (hdc, infoPtr->hFont);
742 INT oldBkMode = SetBkMode (hdc, TRANSPARENT);
743 WCHAR txt[80];
745 DATETIME_ReturnTxt (infoPtr, 0, txt, sizeof(txt)/sizeof(txt[0]));
746 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
747 rcDraw->bottom = size.cy + 2;
749 prevright = infoPtr->checkbox.right = ((infoPtr->dwStyle & DTS_SHOWNONE) ? 18 : 2);
751 for (i = 0; i < infoPtr->nrFields; i++) {
752 DATETIME_ReturnTxt (infoPtr, i, txt, sizeof(txt)/sizeof(txt[0]));
753 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
754 DATETIME_ReturnFieldWidth (infoPtr, hdc, i, &fieldWidth);
755 field = &infoPtr->fieldRect[i];
756 field->left = prevright;
757 field->right = prevright + fieldWidth;
758 field->top = rcDraw->top;
759 field->bottom = rcDraw->bottom;
760 prevright = field->right;
762 if (infoPtr->dwStyle & WS_DISABLED)
763 oldTextColor = SetTextColor (hdc, comctl32_color.clrGrayText);
764 else if ((infoPtr->haveFocus) && (i == infoPtr->select)) {
765 RECT selection;
767 /* fill if focused */
768 HBRUSH hbr = CreateSolidBrush (comctl32_color.clrActiveCaption);
770 if (infoPtr->nCharsEntered)
772 memcpy(txt, infoPtr->charsEntered, infoPtr->nCharsEntered * sizeof(WCHAR));
773 txt[infoPtr->nCharsEntered] = 0;
774 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
777 SetRect(&selection, 0, 0, size.cx, size.cy);
778 /* center rectangle */
779 OffsetRect(&selection, (field->right + field->left - size.cx)/2,
780 (field->bottom - size.cy)/2);
782 FillRect(hdc, &selection, hbr);
783 DeleteObject (hbr);
784 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindow);
786 else
787 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindowText);
789 /* draw the date text using the colour set above */
790 DrawTextW (hdc, txt, strlenW(txt), field, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
791 SetTextColor (hdc, oldTextColor);
793 SetBkMode (hdc, oldBkMode);
794 SelectObject (hdc, oldFont);
797 if (!(infoPtr->dwStyle & DTS_UPDOWN)) {
798 DrawFrameControl(hdc, &infoPtr->calbutton, DFC_SCROLL,
799 DFCS_SCROLLDOWN | (infoPtr->bCalDepressed ? DFCS_PUSHED : 0) |
800 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
805 static INT
806 DATETIME_HitTest (const DATETIME_INFO *infoPtr, POINT pt)
808 int i;
810 TRACE ("%s\n", wine_dbgstr_point(&pt));
812 if (PtInRect (&infoPtr->calbutton, pt)) return DTHT_MCPOPUP;
813 if (PtInRect (&infoPtr->checkbox, pt)) return DTHT_CHECKBOX;
815 for (i = 0; i < infoPtr->nrFields; i++) {
816 if (PtInRect (&infoPtr->fieldRect[i], pt)) return i;
819 return DTHT_NONE;
822 /* Returns index of the nearest preceding date field from given,
823 or -1 if none was found */
824 static int DATETIME_GetPrevDateField(const DATETIME_INFO *infoPtr, int i)
826 for(--i; i >= 0; i--)
828 if (infoPtr->fieldspec[i] & DTHT_DATEFIELD) return i;
830 return -1;
833 static void
834 DATETIME_ApplySelectedField (DATETIME_INFO *infoPtr)
836 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
837 int i, val = 0;
838 BOOL clamp_day = FALSE;
839 SYSTEMTIME date = infoPtr->date;
840 int oldyear;
842 if (infoPtr->select == -1 || infoPtr->nCharsEntered == 0)
843 return;
845 if ((infoPtr->fieldspec[fieldNum] == ONELETTERAMPM) ||
846 (infoPtr->fieldspec[fieldNum] == TWOLETTERAMPM))
847 val = infoPtr->charsEntered[0];
848 else {
849 for (i=0; i<infoPtr->nCharsEntered; i++)
850 val = val * 10 + infoPtr->charsEntered[i] - '0';
853 infoPtr->nCharsEntered = 0;
855 switch (infoPtr->fieldspec[fieldNum]) {
856 case ONEDIGITYEAR:
857 case TWODIGITYEAR:
858 oldyear = date.wYear;
859 date.wYear = date.wYear - (date.wYear%100) + val;
861 if (DATETIME_IsDateInValidRange(infoPtr, &date))
862 clamp_day = TRUE;
863 else
864 date.wYear = oldyear;
866 break;
867 case INVALIDFULLYEAR:
868 case FULLYEAR:
869 oldyear = date.wYear;
870 date.wYear = val;
872 if (DATETIME_IsDateInValidRange(infoPtr, &date))
873 clamp_day = TRUE;
874 else
875 date.wYear = oldyear;
877 break;
878 case ONEDIGITMONTH:
879 case TWODIGITMONTH:
880 date.wMonth = val;
881 clamp_day = TRUE;
882 break;
883 case ONEDIGITDAY:
884 case TWODIGITDAY:
885 date.wDay = val;
886 break;
887 case ONEDIGIT12HOUR:
888 case TWODIGIT12HOUR:
889 if (val >= 24)
890 val -= 20;
892 if (val >= 13)
893 date.wHour = val;
894 else if (val != 0) {
895 if (date.wHour >= 12) /* preserve current AM/PM state */
896 date.wHour = (val == 12 ? 12 : val + 12);
897 else
898 date.wHour = (val == 12 ? 0 : val);
900 break;
901 case ONEDIGIT24HOUR:
902 case TWODIGIT24HOUR:
903 date.wHour = val;
904 break;
905 case ONEDIGITMINUTE:
906 case TWODIGITMINUTE:
907 date.wMinute = val;
908 break;
909 case ONEDIGITSECOND:
910 case TWODIGITSECOND:
911 date.wSecond = val;
912 break;
913 case ONELETTERAMPM:
914 case TWOLETTERAMPM:
915 if (val == 'a' || val == 'A') {
916 if (date.wHour >= 12)
917 date.wHour -= 12;
918 } else if (val == 'p' || val == 'P') {
919 if (date.wHour < 12)
920 date.wHour += 12;
922 break;
925 if (clamp_day && date.wDay > MONTHCAL_MonthLength(date.wMonth, date.wYear))
926 date.wDay = MONTHCAL_MonthLength(date.wMonth, date.wYear);
928 if (DATETIME_SetSystemTime(infoPtr, GDT_VALID, &date))
929 DATETIME_SendDateTimeChangeNotify (infoPtr);
932 static void
933 DATETIME_SetSelectedField (DATETIME_INFO *infoPtr, int select)
935 DATETIME_ApplySelectedField(infoPtr);
937 infoPtr->select = select;
938 infoPtr->nCharsEntered = 0;
941 static LRESULT
942 DATETIME_LButtonDown (DATETIME_INFO *infoPtr, INT x, INT y)
944 POINT pt;
945 int new;
947 pt.x = x;
948 pt.y = y;
949 new = DATETIME_HitTest (infoPtr, pt);
951 SetFocus(infoPtr->hwndSelf);
953 if (!(new & DTHT_NODATEMASK) || (new == DTHT_NONE))
955 if (new == DTHT_NONE)
956 new = infoPtr->nrFields - 1;
957 else
959 /* hitting string part moves selection to next date field to left */
960 if (infoPtr->fieldspec[new] & DT_STRING)
962 new = DATETIME_GetPrevDateField(infoPtr, new);
963 if (new == -1) return 0;
965 /* never select full day of week */
966 if (infoPtr->fieldspec[new] == FULLDAY) return 0;
970 DATETIME_SetSelectedField(infoPtr, new);
972 if (infoPtr->select == DTHT_MCPOPUP) {
973 RECT rcMonthCal;
974 POINT pos;
975 SendMessageW(infoPtr->hMonthCal, MCM_GETMINREQRECT, 0, (LPARAM)&rcMonthCal);
977 /* FIXME: button actually is only depressed during dropdown of the */
978 /* calendar control and when the mouse is over the button window */
979 infoPtr->bCalDepressed = TRUE;
981 /* recalculate the position of the monthcal popup */
982 if(infoPtr->dwStyle & DTS_RIGHTALIGN)
983 pos.x = infoPtr->calbutton.left - (rcMonthCal.right - rcMonthCal.left);
984 else
985 /* FIXME: this should be after the area reserved for the checkbox */
986 pos.x = infoPtr->rcDraw.left;
988 pos.y = infoPtr->rcClient.bottom;
989 OffsetRect( &rcMonthCal, pos.x, pos.y );
990 MapWindowPoints( infoPtr->hwndSelf, 0, (POINT *)&rcMonthCal, 2 );
991 SetWindowPos(infoPtr->hMonthCal, 0, rcMonthCal.left, rcMonthCal.top,
992 rcMonthCal.right - rcMonthCal.left, rcMonthCal.bottom - rcMonthCal.top, 0);
994 if(IsWindowVisible(infoPtr->hMonthCal)) {
995 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
996 infoPtr->bDropdownEnabled = FALSE;
997 DATETIME_SendSimpleNotify (infoPtr, DTN_CLOSEUP);
998 } else {
999 const SYSTEMTIME *lprgSysTimeArray = &infoPtr->date;
1000 TRACE("update calendar %04d/%02d/%02d\n",
1001 lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay);
1002 SendMessageW(infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
1004 if (infoPtr->bDropdownEnabled) {
1005 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
1006 DATETIME_SendSimpleNotify (infoPtr, DTN_DROPDOWN);
1008 infoPtr->bDropdownEnabled = TRUE;
1011 TRACE ("dt:%p mc:%p mc parent:%p, desktop:%p\n",
1012 infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hwndNotify, GetDesktopWindow ());
1015 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1017 return 0;
1021 static LRESULT
1022 DATETIME_LButtonUp (DATETIME_INFO *infoPtr)
1024 if(infoPtr->bCalDepressed) {
1025 infoPtr->bCalDepressed = FALSE;
1026 InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
1029 return 0;
1033 static LRESULT
1034 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
1036 if (!hdc) {
1037 PAINTSTRUCT ps;
1038 hdc = BeginPaint (infoPtr->hwndSelf, &ps);
1039 DATETIME_Refresh (infoPtr, hdc);
1040 EndPaint (infoPtr->hwndSelf, &ps);
1041 } else {
1042 DATETIME_Refresh (infoPtr, hdc);
1045 /* Not a click on the dropdown box, enabled it */
1046 infoPtr->bDropdownEnabled = TRUE;
1048 return 0;
1052 static LRESULT
1053 DATETIME_Button_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1055 if( HIWORD(wParam) == BN_CLICKED) {
1056 DWORD state = SendMessageW((HWND)lParam, BM_GETCHECK, 0, 0);
1057 infoPtr->dateValid = (state == BST_CHECKED);
1058 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1060 return 0;
1065 static LRESULT
1066 DATETIME_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1068 TRACE("hwndbutton = %p\n", infoPtr->hwndCheckbut);
1069 if(infoPtr->hwndCheckbut == (HWND)lParam)
1070 return DATETIME_Button_Command(infoPtr, wParam, lParam);
1071 return 0;
1075 static LRESULT
1076 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
1078 TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
1079 if (bEnable)
1080 infoPtr->dwStyle &= ~WS_DISABLED;
1081 else
1082 infoPtr->dwStyle |= WS_DISABLED;
1084 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1086 return 0;
1090 static LRESULT
1091 DATETIME_EraseBackground (const DATETIME_INFO *infoPtr, HDC hdc)
1093 HBRUSH hBrush, hSolidBrush = NULL;
1094 RECT rc;
1096 if (infoPtr->dwStyle & WS_DISABLED)
1097 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
1098 else
1100 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
1101 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
1102 if (!hBrush)
1103 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
1106 GetClientRect (infoPtr->hwndSelf, &rc);
1108 FillRect (hdc, &rc, hBrush);
1110 if (hSolidBrush)
1111 DeleteObject(hSolidBrush);
1113 return -1;
1117 static LRESULT
1118 DATETIME_Notify (DATETIME_INFO *infoPtr, const NMHDR *lpnmh)
1120 TRACE ("Got notification %x from %p\n", lpnmh->code, lpnmh->hwndFrom);
1121 TRACE ("info: %p %p %p\n", infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hUpdown);
1123 if (lpnmh->code == MCN_SELECT) {
1124 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1125 infoPtr->dateValid = TRUE;
1126 SendMessageW (infoPtr->hMonthCal, MCM_GETCURSEL, 0, (LPARAM)&infoPtr->date);
1127 TRACE("got from calendar %04d/%02d/%02d day of week %d\n",
1128 infoPtr->date.wYear, infoPtr->date.wMonth, infoPtr->date.wDay, infoPtr->date.wDayOfWeek);
1129 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
1130 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1131 DATETIME_SendDateTimeChangeNotify (infoPtr);
1132 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1134 if ((lpnmh->hwndFrom == infoPtr->hUpdown) && (lpnmh->code == UDN_DELTAPOS)) {
1135 const NM_UPDOWN *lpnmud = (const NM_UPDOWN*)lpnmh;
1136 TRACE("Delta pos %d\n", lpnmud->iDelta);
1137 infoPtr->pendingUpdown = lpnmud->iDelta;
1139 return 0;
1143 static LRESULT
1144 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode)
1146 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1147 int wrap = 0;
1148 int new;
1150 if (!(infoPtr->haveFocus)) return 0;
1151 if ((fieldNum==0) && (infoPtr->select)) return 0;
1153 if (infoPtr->select & FORMATCALLMASK) {
1154 FIXME ("Callbacks not implemented yet\n");
1157 switch (vkCode) {
1158 case VK_ADD:
1159 case VK_UP:
1160 infoPtr->nCharsEntered = 0;
1161 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1162 DATETIME_SendDateTimeChangeNotify (infoPtr);
1163 break;
1164 case VK_SUBTRACT:
1165 case VK_DOWN:
1166 infoPtr->nCharsEntered = 0;
1167 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1168 DATETIME_SendDateTimeChangeNotify (infoPtr);
1169 break;
1170 case VK_HOME:
1171 infoPtr->nCharsEntered = 0;
1172 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MIN);
1173 DATETIME_SendDateTimeChangeNotify (infoPtr);
1174 break;
1175 case VK_END:
1176 infoPtr->nCharsEntered = 0;
1177 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MAX);
1178 DATETIME_SendDateTimeChangeNotify (infoPtr);
1179 break;
1180 case VK_LEFT:
1181 new = infoPtr->select;
1182 do {
1183 if (new == 0) {
1184 new = new - 1;
1185 wrap++;
1186 } else {
1187 new--;
1189 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1190 if (new != infoPtr->select)
1191 DATETIME_SetSelectedField(infoPtr, new);
1192 break;
1193 case VK_RIGHT:
1194 new = infoPtr->select;
1195 do {
1196 new++;
1197 if (new==infoPtr->nrFields) {
1198 new = 0;
1199 wrap++;
1201 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1202 if (new != infoPtr->select)
1203 DATETIME_SetSelectedField(infoPtr, new);
1204 break;
1207 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1209 return 0;
1213 static LRESULT
1214 DATETIME_Char (DATETIME_INFO *infoPtr, WPARAM vkCode)
1216 int fieldNum, fieldSpec;
1218 fieldNum = infoPtr->select & DTHT_DATEFIELD;
1219 fieldSpec = infoPtr->fieldspec[fieldNum];
1221 if (fieldSpec == ONELETTERAMPM || fieldSpec == TWOLETTERAMPM) {
1222 infoPtr->charsEntered[0] = vkCode;
1223 infoPtr->nCharsEntered = 1;
1225 DATETIME_ApplySelectedField(infoPtr);
1226 } else if (vkCode >= '0' && vkCode <= '9') {
1227 int maxChars;
1229 infoPtr->charsEntered[infoPtr->nCharsEntered++] = vkCode;
1231 if (fieldSpec == INVALIDFULLYEAR || fieldSpec == FULLYEAR)
1232 maxChars = 4;
1233 else
1234 maxChars = 2;
1236 if ((fieldSpec == ONEDIGIT12HOUR ||
1237 fieldSpec == TWODIGIT12HOUR ||
1238 fieldSpec == ONEDIGIT24HOUR ||
1239 fieldSpec == TWODIGIT24HOUR) &&
1240 (infoPtr->nCharsEntered == 1))
1242 if (vkCode >= '3')
1243 maxChars = 1;
1246 if (maxChars == infoPtr->nCharsEntered)
1247 DATETIME_ApplySelectedField(infoPtr);
1250 return 0;
1254 static LRESULT
1255 DATETIME_VScroll (DATETIME_INFO *infoPtr, WORD wScroll)
1257 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1259 if ((SHORT)LOWORD(wScroll) != SB_THUMBPOSITION) return 0;
1260 if (!(infoPtr->haveFocus)) return 0;
1261 if ((fieldNum==0) && (infoPtr->select)) return 0;
1263 if (infoPtr->pendingUpdown >= 0) {
1264 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1265 DATETIME_SendDateTimeChangeNotify (infoPtr);
1267 else {
1268 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1269 DATETIME_SendDateTimeChangeNotify (infoPtr);
1272 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1274 return 0;
1278 static LRESULT
1279 DATETIME_KillFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1281 TRACE("lost focus to %p\n", lostFocus);
1283 if (infoPtr->haveFocus) {
1284 DATETIME_SendSimpleNotify (infoPtr, NM_KILLFOCUS);
1285 infoPtr->haveFocus = 0;
1286 DATETIME_SetSelectedField (infoPtr, -1);
1289 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
1291 return 0;
1295 static LRESULT
1296 DATETIME_NCCreate (HWND hwnd, const CREATESTRUCTW *lpcs)
1298 DWORD dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1299 /* force control to have client edge */
1300 dwExStyle |= WS_EX_CLIENTEDGE;
1301 SetWindowLongW(hwnd, GWL_EXSTYLE, dwExStyle);
1303 return DefWindowProcW(hwnd, WM_NCCREATE, 0, (LPARAM)lpcs);
1307 static LRESULT
1308 DATETIME_SetFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1310 TRACE("got focus from %p\n", lostFocus);
1312 /* if monthcal is open and it loses focus, close monthcal */
1313 if (infoPtr->hMonthCal && (lostFocus == infoPtr->hMonthCal) &&
1314 IsWindowVisible(infoPtr->hMonthCal))
1316 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1317 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1318 /* note: this get triggered even if monthcal loses focus to a dropdown
1319 * box click, which occurs without an intermediate WM_PAINT call
1321 infoPtr->bDropdownEnabled = FALSE;
1322 return 0;
1325 if (infoPtr->haveFocus == 0) {
1326 DATETIME_SendSimpleNotify (infoPtr, NM_SETFOCUS);
1327 infoPtr->haveFocus = DTHT_GOTFOCUS;
1330 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1332 return 0;
1336 static BOOL
1337 DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr)
1339 NMDATETIMECHANGE dtdtc;
1341 dtdtc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1342 dtdtc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1343 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1345 dtdtc.dwFlags = infoPtr->dateValid ? GDT_VALID : GDT_NONE;
1347 dtdtc.st = infoPtr->date;
1348 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1349 dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1353 static BOOL
1354 DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code)
1356 NMHDR nmhdr;
1358 TRACE("%x\n", code);
1359 nmhdr.hwndFrom = infoPtr->hwndSelf;
1360 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1361 nmhdr.code = code;
1363 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1364 nmhdr.idFrom, (LPARAM)&nmhdr);
1367 static LRESULT
1368 DATETIME_Size (DATETIME_INFO *infoPtr, INT width, INT height)
1370 /* set size */
1371 infoPtr->rcClient.bottom = height;
1372 infoPtr->rcClient.right = width;
1374 TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1376 infoPtr->rcDraw = infoPtr->rcClient;
1378 if (infoPtr->dwStyle & DTS_UPDOWN) {
1379 SetWindowPos(infoPtr->hUpdown, NULL,
1380 infoPtr->rcClient.right-14, 0,
1381 15, infoPtr->rcClient.bottom - infoPtr->rcClient.top,
1382 SWP_NOACTIVATE | SWP_NOZORDER);
1384 else {
1385 /* set the size of the button that drops the calendar down */
1386 /* FIXME: account for style that allows button on left side */
1387 infoPtr->calbutton.top = infoPtr->rcDraw.top;
1388 infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1389 infoPtr->calbutton.left = infoPtr->rcDraw.right-15;
1390 infoPtr->calbutton.right = infoPtr->rcDraw.right;
1393 /* set enable/disable button size for show none style being enabled */
1394 /* FIXME: these dimensions are completely incorrect */
1395 infoPtr->checkbox.top = infoPtr->rcDraw.top;
1396 infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1397 infoPtr->checkbox.left = infoPtr->rcDraw.left;
1398 infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1400 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1402 return 0;
1405 static LRESULT
1406 DATETIME_StyleChanging(DATETIME_INFO *infoPtr, WPARAM wStyleType, STYLESTRUCT *lpss)
1408 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1409 wStyleType, lpss->styleOld, lpss->styleNew);
1411 /* block DTS_SHOWNONE change */
1412 if ((lpss->styleNew ^ lpss->styleOld) & DTS_SHOWNONE)
1414 if (lpss->styleOld & DTS_SHOWNONE)
1415 lpss->styleNew |= DTS_SHOWNONE;
1416 else
1417 lpss->styleNew &= ~DTS_SHOWNONE;
1420 return 0;
1423 static LRESULT
1424 DATETIME_StyleChanged(DATETIME_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
1426 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1427 wStyleType, lpss->styleOld, lpss->styleNew);
1429 if (wStyleType != GWL_STYLE) return 0;
1431 infoPtr->dwStyle = lpss->styleNew;
1433 if ( !(lpss->styleOld & DTS_SHOWNONE) && (lpss->styleNew & DTS_SHOWNONE) ) {
1434 infoPtr->hwndCheckbut = CreateWindowExW (0, WC_BUTTONW, 0, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1435 2, 2, 13, 13, infoPtr->hwndSelf, 0,
1436 (HINSTANCE)GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_HINSTANCE), 0);
1437 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, infoPtr->dateValid ? 1 : 0, 0);
1439 if ( (lpss->styleOld & DTS_SHOWNONE) && !(lpss->styleNew & DTS_SHOWNONE) ) {
1440 DestroyWindow(infoPtr->hwndCheckbut);
1441 infoPtr->hwndCheckbut = 0;
1443 if ( !(lpss->styleOld & DTS_UPDOWN) && (lpss->styleNew & DTS_UPDOWN) ) {
1444 infoPtr->hUpdown = CreateUpDownControl (WS_CHILD | WS_BORDER | WS_VISIBLE, 120, 1, 20, 20,
1445 infoPtr->hwndSelf, 1, 0, 0, UD_MAXVAL, UD_MINVAL, 0);
1447 if ( (lpss->styleOld & DTS_UPDOWN) && !(lpss->styleNew & DTS_UPDOWN) ) {
1448 DestroyWindow(infoPtr->hUpdown);
1449 infoPtr->hUpdown = 0;
1452 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1453 return 0;
1457 static LRESULT
1458 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1460 infoPtr->hFont = font;
1461 if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1462 return 0;
1466 static LRESULT
1467 DATETIME_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1469 DATETIME_INFO *infoPtr = Alloc (sizeof(DATETIME_INFO));
1470 STYLESTRUCT ss = { 0, lpcs->style };
1472 if (!infoPtr) return -1;
1474 infoPtr->hwndSelf = hwnd;
1475 infoPtr->dwStyle = lpcs->style;
1477 infoPtr->nrFieldsAllocated = 32;
1478 infoPtr->fieldspec = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1479 infoPtr->fieldRect = Alloc (infoPtr->nrFieldsAllocated * sizeof(RECT));
1480 infoPtr->buflen = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1481 infoPtr->hwndNotify = lpcs->hwndParent;
1482 infoPtr->select = -1; /* initially, nothing is selected */
1483 infoPtr->bDropdownEnabled = TRUE;
1485 DATETIME_StyleChanged(infoPtr, GWL_STYLE, &ss);
1486 DATETIME_SetFormatW (infoPtr, 0);
1488 /* create the monthcal control */
1489 infoPtr->hMonthCal = CreateWindowExW (0, MONTHCAL_CLASSW, 0, WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS,
1490 0, 0, 0, 0, infoPtr->hwndSelf, 0, 0, 0);
1492 /* initialize info structure */
1493 GetLocalTime (&infoPtr->date);
1494 infoPtr->dateValid = TRUE;
1495 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1497 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1499 return 0;
1504 static LRESULT
1505 DATETIME_Destroy (DATETIME_INFO *infoPtr)
1507 if (infoPtr->hwndCheckbut)
1508 DestroyWindow(infoPtr->hwndCheckbut);
1509 if (infoPtr->hUpdown)
1510 DestroyWindow(infoPtr->hUpdown);
1511 if (infoPtr->hMonthCal)
1512 DestroyWindow(infoPtr->hMonthCal);
1513 SetWindowLongPtrW( infoPtr->hwndSelf, 0, 0 ); /* clear infoPtr */
1514 Free (infoPtr->buflen);
1515 Free (infoPtr->fieldRect);
1516 Free (infoPtr->fieldspec);
1517 Free (infoPtr);
1518 return 0;
1522 static INT
1523 DATETIME_GetText (const DATETIME_INFO *infoPtr, INT count, LPWSTR dst)
1525 WCHAR buf[80];
1526 int i;
1528 if (!dst || (count <= 0)) return 0;
1530 dst[0] = 0;
1531 for (i = 0; i < infoPtr->nrFields; i++)
1533 DATETIME_ReturnTxt(infoPtr, i, buf, sizeof(buf)/sizeof(buf[0]));
1534 if ((strlenW(dst) + strlenW(buf)) < count)
1535 strcatW(dst, buf);
1536 else break;
1538 return strlenW(dst);
1542 static LRESULT WINAPI
1543 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1545 DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1547 TRACE ("%x, %lx, %lx\n", uMsg, wParam, lParam);
1549 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
1550 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
1552 switch (uMsg) {
1554 case DTM_GETSYSTEMTIME:
1555 return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1557 case DTM_SETSYSTEMTIME:
1558 return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1560 case DTM_GETRANGE:
1561 return SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1563 case DTM_SETRANGE:
1564 return SendMessageW (infoPtr->hMonthCal, MCM_SETRANGE, wParam, lParam);
1566 case DTM_SETFORMATA:
1567 return DATETIME_SetFormatA (infoPtr, (LPCSTR)lParam);
1569 case DTM_SETFORMATW:
1570 return DATETIME_SetFormatW (infoPtr, (LPCWSTR)lParam);
1572 case DTM_GETMONTHCAL:
1573 return (LRESULT)infoPtr->hMonthCal;
1575 case DTM_SETMCCOLOR:
1576 return SendMessageW (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
1578 case DTM_GETMCCOLOR:
1579 return SendMessageW (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
1581 case DTM_SETMCFONT:
1582 return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1584 case DTM_GETMCFONT:
1585 return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1587 case WM_NOTIFY:
1588 return DATETIME_Notify (infoPtr, (LPNMHDR)lParam);
1590 case WM_ENABLE:
1591 return DATETIME_Enable (infoPtr, (BOOL)wParam);
1593 case WM_ERASEBKGND:
1594 return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1596 case WM_GETDLGCODE:
1597 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1599 case WM_PRINTCLIENT:
1600 case WM_PAINT:
1601 return DATETIME_Paint (infoPtr, (HDC)wParam);
1603 case WM_KEYDOWN:
1604 return DATETIME_KeyDown (infoPtr, wParam);
1606 case WM_CHAR:
1607 return DATETIME_Char (infoPtr, wParam);
1609 case WM_KILLFOCUS:
1610 return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1612 case WM_NCCREATE:
1613 return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1615 case WM_SETFOCUS:
1616 return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1618 case WM_SIZE:
1619 return DATETIME_Size (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1621 case WM_LBUTTONDOWN:
1622 return DATETIME_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1624 case WM_LBUTTONUP:
1625 return DATETIME_LButtonUp (infoPtr);
1627 case WM_VSCROLL:
1628 return DATETIME_VScroll (infoPtr, (WORD)wParam);
1630 case WM_CREATE:
1631 return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1633 case WM_DESTROY:
1634 return DATETIME_Destroy (infoPtr);
1636 case WM_COMMAND:
1637 return DATETIME_Command (infoPtr, wParam, lParam);
1639 case WM_STYLECHANGING:
1640 return DATETIME_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1642 case WM_STYLECHANGED:
1643 return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1645 case WM_SETFONT:
1646 return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1648 case WM_GETFONT:
1649 return (LRESULT) infoPtr->hFont;
1651 case WM_GETTEXT:
1652 return (LRESULT) DATETIME_GetText(infoPtr, wParam, (LPWSTR)lParam);
1654 case WM_SETTEXT:
1655 return CB_ERR;
1657 default:
1658 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
1659 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
1660 uMsg, wParam, lParam);
1661 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1666 void
1667 DATETIME_Register (void)
1669 WNDCLASSW wndClass;
1671 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1672 wndClass.style = CS_GLOBALCLASS;
1673 wndClass.lpfnWndProc = DATETIME_WindowProc;
1674 wndClass.cbClsExtra = 0;
1675 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1676 wndClass.hCursor = LoadCursorW (0, (LPCWSTR)IDC_ARROW);
1677 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1678 wndClass.lpszClassName = DATETIMEPICK_CLASSW;
1680 RegisterClassW (&wndClass);
1684 void
1685 DATETIME_Unregister (void)
1687 UnregisterClassW (DATETIMEPICK_CLASSW, NULL);