wininet: Don't update *lpdwIndex if HttpQueryInfoW fails with ERROR_INSUFFICIENT_BUFFER.
[wine/wine-gecko.git] / dlls / comctl32 / datetime.c
blob5c73a90be5e76ff54f671c47f89f8aca5214ca32
1 /*
2 * Date and time picker control
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1999, 2000 Alex Priem <alexp@sci.kun.nl>
6 * Copyright 2000 Chris Morgan <cmorgan@wpi.edu>
7 * Copyright 2012 Owen Rudge for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * NOTE
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Oct. 20, 2004, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
32 * TODO:
33 * -- DTS_APPCANPARSE
34 * -- DTS_SHORTDATECENTURYFORMAT
35 * -- DTN_FORMAT
36 * -- DTN_FORMATQUERY
37 * -- DTN_USERSTRING
38 * -- DTN_WMKEYDOWN
39 * -- FORMATCALLBACK
42 #include <math.h>
43 #include <string.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <limits.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "commctrl.h"
54 #include "comctl32.h"
55 #include "wine/debug.h"
56 #include "wine/unicode.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(datetime);
60 typedef struct
62 HWND hwndSelf;
63 HWND hMonthCal;
64 HWND hwndNotify;
65 HWND hUpdown;
66 DWORD dwStyle;
67 SYSTEMTIME date;
68 BOOL dateValid;
69 HWND hwndCheckbut;
70 RECT rcClient; /* rect around the edge of the window */
71 RECT rcDraw; /* rect inside of the border */
72 RECT checkbox; /* checkbox allowing the control to be enabled/disabled */
73 RECT calbutton; /* button that toggles the dropdown of the monthcal control */
74 BOOL bCalDepressed; /* TRUE = cal button is depressed */
75 BOOL bDropdownEnabled;
76 int select;
77 WCHAR charsEntered[4];
78 int nCharsEntered;
79 HFONT hFont;
80 int nrFieldsAllocated;
81 int nrFields;
82 int haveFocus;
83 int *fieldspec;
84 RECT *fieldRect;
85 int *buflen;
86 WCHAR textbuf[256];
87 POINT monthcal_pos;
88 int pendingUpdown;
89 } DATETIME_INFO, *LPDATETIME_INFO;
91 /* in monthcal.c */
92 extern int MONTHCAL_MonthLength(int month, int year);
93 extern int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace);
95 /* this list of defines is closely related to `allowedformatchars' defined
96 * in datetime.c; the high nibble indicates the `base type' of the format
97 * specifier.
98 * Do not change without first reading DATETIME_UseFormat.
102 #define DT_END_FORMAT 0
103 #define ONEDIGITDAY 0x01
104 #define TWODIGITDAY 0x02
105 #define THREECHARDAY 0x03
106 #define FULLDAY 0x04
107 #define ONEDIGIT12HOUR 0x11
108 #define TWODIGIT12HOUR 0x12
109 #define ONEDIGIT24HOUR 0x21
110 #define TWODIGIT24HOUR 0x22
111 #define ONEDIGITMINUTE 0x31
112 #define TWODIGITMINUTE 0x32
113 #define ONEDIGITMONTH 0x41
114 #define TWODIGITMONTH 0x42
115 #define THREECHARMONTH 0x43
116 #define FULLMONTH 0x44
117 #define ONEDIGITSECOND 0x51
118 #define TWODIGITSECOND 0x52
119 #define ONELETTERAMPM 0x61
120 #define TWOLETTERAMPM 0x62
121 #define ONEDIGITYEAR 0x71
122 #define TWODIGITYEAR 0x72
123 #define INVALIDFULLYEAR 0x73 /* FIXME - yyy is not valid - we'll treat it as yyyy */
124 #define FULLYEAR 0x74
125 #define FORMATCALLBACK 0x81 /* -> maximum of 0x80 callbacks possible */
126 #define FORMATCALLMASK 0x80
127 #define DT_STRING 0x0100
129 #define DTHT_DATEFIELD 0xff /* for hit-testing */
131 #define DTHT_NONE 0x1000
132 #define DTHT_CHECKBOX 0x2000 /* these should end at '00' , to make */
133 #define DTHT_MCPOPUP 0x3000 /* & DTHT_DATEFIELD 0 when DATETIME_KeyDown */
134 #define DTHT_GOTFOCUS 0x4000 /* tests for date-fields */
135 #define DTHT_NODATEMASK 0xf000 /* to mask check and drop down from others */
137 static BOOL DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code);
138 static BOOL DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr);
139 static const WCHAR allowedformatchars[] = {'d', 'h', 'H', 'm', 'M', 's', 't', 'y', 'X', 0};
140 static const int maxrepetition [] = {4,2,2,2,4,2,2,4,-1};
142 /* valid date limits */
143 static const SYSTEMTIME max_allowed_date = { /* wYear */ 9999, /* wMonth */ 12, /* wDayOfWeek */ 0, /* wDay */ 31 };
144 static const SYSTEMTIME min_allowed_date = { /* wYear */ 1752, /* wMonth */ 9, /* wDayOfWeek */ 0, /* wDay */ 14 };
146 static DWORD
147 DATETIME_GetSystemTime (const DATETIME_INFO *infoPtr, SYSTEMTIME *systime)
149 if (!systime) return GDT_NONE;
151 if ((infoPtr->dwStyle & DTS_SHOWNONE) &&
152 (SendMessageW (infoPtr->hwndCheckbut, BM_GETCHECK, 0, 0) == BST_UNCHECKED))
153 return GDT_NONE;
155 *systime = infoPtr->date;
157 return GDT_VALID;
160 /* Checks value is within configured date range
162 * PARAMETERS
164 * [I] infoPtr : valid pointer to control data
165 * [I] date : pointer to valid date data to check
167 * RETURN VALUE
169 * TRUE - date within configured range
170 * FALSE - date is outside configured range
172 static BOOL DATETIME_IsDateInValidRange(const DATETIME_INFO *infoPtr, const SYSTEMTIME *date)
174 SYSTEMTIME range[2];
175 DWORD limits;
177 if ((MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) ||
178 (MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1))
179 return FALSE;
181 limits = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, 0, (LPARAM)range);
183 if (limits & GDTR_MAX)
185 if (MONTHCAL_CompareSystemTime(date, &range[1]) == 1)
186 return FALSE;
189 if (limits & GDTR_MIN)
191 if (MONTHCAL_CompareSystemTime(date, &range[0]) == -1)
192 return FALSE;
195 return TRUE;
198 static BOOL
199 DATETIME_SetSystemTime (DATETIME_INFO *infoPtr, DWORD flag, const SYSTEMTIME *systime)
201 if (!systime) return FALSE;
203 TRACE("%04d/%02d/%02d %02d:%02d:%02d\n",
204 systime->wYear, systime->wMonth, systime->wDay,
205 systime->wHour, systime->wMinute, systime->wSecond);
207 if (flag == GDT_VALID) {
208 if (systime->wYear == 0 ||
209 systime->wMonth < 1 || systime->wMonth > 12 ||
210 systime->wDay < 1 ||
211 systime->wDay > MONTHCAL_MonthLength(systime->wMonth, systime->wYear) ||
212 systime->wHour > 23 ||
213 systime->wMinute > 59 ||
214 systime->wSecond > 59 ||
215 systime->wMilliseconds > 999
217 return FALSE;
219 /* Windows returns true if the date is valid but outside the limits set */
220 if (!DATETIME_IsDateInValidRange(infoPtr, systime))
221 return TRUE;
223 infoPtr->dateValid = TRUE;
224 infoPtr->date = *systime;
225 /* always store a valid day of week */
226 MONTHCAL_CalculateDayOfWeek(&infoPtr->date, TRUE);
228 SendMessageW (infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
229 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
230 } else if ((infoPtr->dwStyle & DTS_SHOWNONE) && (flag == GDT_NONE)) {
231 infoPtr->dateValid = FALSE;
232 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_UNCHECKED, 0);
234 else
235 return FALSE;
237 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
238 return TRUE;
242 /***
243 * Split up a formattxt in actions.
244 * See ms documentation for the meaning of the letter codes/'specifiers'.
246 * Notes:
247 * *'dddddd' is handled as 'dddd' plus 'dd'.
248 * *unrecognized formats are strings (here given the type DT_STRING;
249 * start of the string is encoded in lower bits of DT_STRING.
250 * Therefore, 'string' ends finally up as '<show seconds>tring'.
253 static void
254 DATETIME_UseFormat (DATETIME_INFO *infoPtr, LPCWSTR formattxt)
256 unsigned int i;
257 int j, k, len;
258 BOOL inside_literal = FALSE; /* inside '...' */
259 int *nrFields = &infoPtr->nrFields;
261 *nrFields = 0;
262 infoPtr->fieldspec[*nrFields] = 0;
263 len = strlenW(allowedformatchars);
264 k = 0;
266 for (i = 0; formattxt[i]; i++) {
267 TRACE ("\n%d %c:", i, formattxt[i]);
268 if (!inside_literal) {
269 for (j = 0; j < len; j++) {
270 if (allowedformatchars[j]==formattxt[i]) {
271 TRACE ("%c[%d,%x]", allowedformatchars[j], *nrFields, infoPtr->fieldspec[*nrFields]);
272 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
273 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
274 break;
276 if (infoPtr->fieldspec[*nrFields] >> 4 != j) {
277 (*nrFields)++;
278 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
279 break;
281 if ((infoPtr->fieldspec[*nrFields] & 0x0f) == maxrepetition[j]) {
282 (*nrFields)++;
283 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
284 break;
286 infoPtr->fieldspec[*nrFields]++;
287 break;
288 } /* if allowedformatchar */
289 } /* for j */
291 else
292 j = len;
294 if (formattxt[i] == '\'')
296 inside_literal = !inside_literal;
297 continue;
300 /* char is not a specifier: handle char like a string */
301 if (j == len) {
302 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
303 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
304 infoPtr->buflen[*nrFields] = 0;
305 } else if ((infoPtr->fieldspec[*nrFields] & DT_STRING) != DT_STRING) {
306 (*nrFields)++;
307 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
308 infoPtr->buflen[*nrFields] = 0;
310 infoPtr->textbuf[k] = formattxt[i];
311 k++;
312 infoPtr->buflen[*nrFields]++;
313 } /* if j=len */
315 if (*nrFields == infoPtr->nrFieldsAllocated) {
316 FIXME ("out of memory; should reallocate. crash ahead.\n");
318 } /* for i */
320 TRACE("\n");
322 if (infoPtr->fieldspec[*nrFields] != 0) (*nrFields)++;
326 static BOOL
327 DATETIME_SetFormatW (DATETIME_INFO *infoPtr, LPCWSTR format)
329 WCHAR format_buf[80];
331 if (!format) {
332 DWORD format_item;
334 if (infoPtr->dwStyle & DTS_LONGDATEFORMAT)
335 format_item = LOCALE_SLONGDATE;
336 else if ((infoPtr->dwStyle & DTS_TIMEFORMAT) == DTS_TIMEFORMAT)
337 format_item = LOCALE_STIMEFORMAT;
338 else /* DTS_SHORTDATEFORMAT */
339 format_item = LOCALE_SSHORTDATE;
340 GetLocaleInfoW(LOCALE_USER_DEFAULT, format_item, format_buf, sizeof(format_buf)/sizeof(format_buf[0]));
341 format = format_buf;
344 DATETIME_UseFormat (infoPtr, format);
345 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
347 return TRUE;
351 static BOOL
352 DATETIME_SetFormatA (DATETIME_INFO *infoPtr, LPCSTR lpszFormat)
354 if (lpszFormat) {
355 BOOL retval;
356 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, NULL, 0);
357 LPWSTR wstr = Alloc(len * sizeof(WCHAR));
358 if (wstr) MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, wstr, len);
359 retval = DATETIME_SetFormatW (infoPtr, wstr);
360 Free (wstr);
361 return retval;
363 else
364 return DATETIME_SetFormatW (infoPtr, 0);
369 static void
370 DATETIME_ReturnTxt (const DATETIME_INFO *infoPtr, int count, LPWSTR result, int resultSize)
372 static const WCHAR fmt_dW[] = { '%', 'd', 0 };
373 static const WCHAR fmt__2dW[] = { '%', '.', '2', 'd', 0 };
374 static const WCHAR fmt__3sW[] = { '%', '.', '3', 's', 0 };
375 SYSTEMTIME date = infoPtr->date;
376 int spec;
377 WCHAR buffer[80];
379 *result=0;
380 TRACE ("%d,%d\n", infoPtr->nrFields, count);
381 if (count>infoPtr->nrFields || count < 0) {
382 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
383 return;
386 if (!infoPtr->fieldspec) return;
388 spec = infoPtr->fieldspec[count];
389 if (spec & DT_STRING) {
390 int txtlen = infoPtr->buflen[count];
392 if (txtlen > resultSize)
393 txtlen = resultSize - 1;
394 memcpy (result, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
395 result[txtlen] = 0;
396 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
397 return;
401 switch (spec) {
402 case DT_END_FORMAT:
403 *result = 0;
404 break;
405 case ONEDIGITDAY:
406 wsprintfW (result, fmt_dW, date.wDay);
407 break;
408 case TWODIGITDAY:
409 wsprintfW (result, fmt__2dW, date.wDay);
410 break;
411 case THREECHARDAY:
412 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1+(date.wDayOfWeek+6)%7, result, 4);
413 /*wsprintfW (result,"%.3s",days[date.wDayOfWeek]);*/
414 break;
415 case FULLDAY:
416 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME1+(date.wDayOfWeek+6)%7, result, resultSize);
417 break;
418 case ONEDIGIT12HOUR:
419 if (date.wHour == 0) {
420 result[0] = '1';
421 result[1] = '2';
422 result[2] = 0;
424 else
425 wsprintfW (result, fmt_dW, date.wHour - (date.wHour > 12 ? 12 : 0));
426 break;
427 case TWODIGIT12HOUR:
428 if (date.wHour == 0) {
429 result[0] = '1';
430 result[1] = '2';
431 result[2] = 0;
433 else
434 wsprintfW (result, fmt__2dW, date.wHour - (date.wHour > 12 ? 12 : 0));
435 break;
436 case ONEDIGIT24HOUR:
437 wsprintfW (result, fmt_dW, date.wHour);
438 break;
439 case TWODIGIT24HOUR:
440 wsprintfW (result, fmt__2dW, date.wHour);
441 break;
442 case ONEDIGITSECOND:
443 wsprintfW (result, fmt_dW, date.wSecond);
444 break;
445 case TWODIGITSECOND:
446 wsprintfW (result, fmt__2dW, date.wSecond);
447 break;
448 case ONEDIGITMINUTE:
449 wsprintfW (result, fmt_dW, date.wMinute);
450 break;
451 case TWODIGITMINUTE:
452 wsprintfW (result, fmt__2dW, date.wMinute);
453 break;
454 case ONEDIGITMONTH:
455 wsprintfW (result, fmt_dW, date.wMonth);
456 break;
457 case TWODIGITMONTH:
458 wsprintfW (result, fmt__2dW, date.wMonth);
459 break;
460 case THREECHARMONTH:
461 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+date.wMonth -1,
462 buffer, sizeof(buffer)/sizeof(buffer[0]));
463 wsprintfW (result, fmt__3sW, buffer);
464 break;
465 case FULLMONTH:
466 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+date.wMonth -1,
467 result, resultSize);
468 break;
469 case ONELETTERAMPM:
470 result[0] = (date.wHour < 12 ? 'A' : 'P');
471 result[1] = 0;
472 break;
473 case TWOLETTERAMPM:
474 result[0] = (date.wHour < 12 ? 'A' : 'P');
475 result[1] = 'M';
476 result[2] = 0;
477 break;
478 case FORMATCALLBACK:
479 FIXME ("Not implemented\n");
480 result[0] = 'x';
481 result[1] = 0;
482 break;
483 case ONEDIGITYEAR:
484 wsprintfW (result, fmt_dW, date.wYear-10* (int) floor(date.wYear/10));
485 break;
486 case TWODIGITYEAR:
487 wsprintfW (result, fmt__2dW, date.wYear-100* (int) floor(date.wYear/100));
488 break;
489 case INVALIDFULLYEAR:
490 case FULLYEAR:
491 wsprintfW (result, fmt_dW, date.wYear);
492 break;
495 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
498 static int wrap(int val, int delta, int minVal, int maxVal)
500 val += delta;
501 if (delta == INT_MIN || val < minVal) return maxVal;
502 if (delta == INT_MAX || val > maxVal) return minVal;
503 return val;
506 static void
507 DATETIME_IncreaseField (DATETIME_INFO *infoPtr, int number, int delta)
509 SYSTEMTIME *date = &infoPtr->date;
510 SYSTEMTIME range[2];
511 DWORD limits;
512 BOOL min;
514 TRACE ("%d\n", number);
515 if ((number > infoPtr->nrFields) || (number < 0)) return;
517 if ((infoPtr->fieldspec[number] & DTHT_DATEFIELD) == 0) return;
519 switch (infoPtr->fieldspec[number]) {
520 case ONEDIGITYEAR:
521 case TWODIGITYEAR:
522 case FULLYEAR:
523 if (delta == INT_MIN)
524 date->wYear = 1752;
525 else if (delta == INT_MAX)
526 date->wYear = 9999;
527 else
528 date->wYear = max(min(date->wYear + delta, 9999), 1752);
530 if (date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
531 /* This can happen when moving away from a leap year. */
532 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear);
533 MONTHCAL_CalculateDayOfWeek(date, TRUE);
534 break;
535 case ONEDIGITMONTH:
536 case TWODIGITMONTH:
537 case THREECHARMONTH:
538 case FULLMONTH:
539 date->wMonth = wrap(date->wMonth, delta, 1, 12);
540 MONTHCAL_CalculateDayOfWeek(date, TRUE);
541 delta = 0;
542 /* fall through */
543 case ONEDIGITDAY:
544 case TWODIGITDAY:
545 case THREECHARDAY:
546 case FULLDAY:
547 date->wDay = wrap(date->wDay, delta, 1, MONTHCAL_MonthLength(date->wMonth, date->wYear));
548 MONTHCAL_CalculateDayOfWeek(date, TRUE);
549 break;
550 case ONELETTERAMPM:
551 case TWOLETTERAMPM:
552 delta *= 12;
553 /* fall through */
554 case ONEDIGIT12HOUR:
555 case TWODIGIT12HOUR:
556 case ONEDIGIT24HOUR:
557 case TWODIGIT24HOUR:
558 date->wHour = wrap(date->wHour, delta, 0, 23);
559 break;
560 case ONEDIGITMINUTE:
561 case TWODIGITMINUTE:
562 date->wMinute = wrap(date->wMinute, delta, 0, 59);
563 break;
564 case ONEDIGITSECOND:
565 case TWODIGITSECOND:
566 date->wSecond = wrap(date->wSecond, delta, 0, 59);
567 break;
568 case FORMATCALLBACK:
569 FIXME ("Not implemented\n");
570 break;
573 /* FYI: On 1752/9/14 the calendar changed and England and the
574 * American colonies changed to the Gregorian calendar. This change
575 * involved having September 14th follow September 2nd. So no date
576 * algorithm works before that date.
578 if (10000 * date->wYear + 100 * date->wMonth + date->wDay < 17520914) {
579 date->wYear = 1752;
580 date->wMonth = 9;
581 date->wDay = 14;
582 date->wSecond = 0;
583 date->wMinute = 0;
584 date->wHour = 0;
587 /* Ensure time is within bounds */
588 limits = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, 0, (LPARAM)range);
589 min = delta < 0;
591 if (limits & (min ? GDTR_MIN : GDTR_MAX))
593 int i = (min ? 0 : 1);
595 if (MONTHCAL_CompareSystemTime(date, &range[i]) == (min ? -1 : 1))
597 date->wYear = range[i].wYear;
598 date->wMonth = range[i].wMonth;
599 date->wDayOfWeek = range[i].wDayOfWeek;
600 date->wDay = range[i].wDay;
601 date->wHour = range[i].wHour;
602 date->wMinute = range[i].wMinute;
603 date->wSecond = range[i].wSecond;
604 date->wMilliseconds = range[i].wMilliseconds;
609 static void
610 DATETIME_ReturnFieldWidth (const DATETIME_INFO *infoPtr, HDC hdc, int count, SHORT *width)
612 /* fields are a fixed width, determined by the largest possible string */
613 /* presumably, these widths should be language dependent */
614 static const WCHAR fld_d1W[] = { '2', 0 };
615 static const WCHAR fld_d2W[] = { '2', '2', 0 };
616 static const WCHAR fld_d4W[] = { '2', '2', '2', '2', 0 };
617 static const WCHAR fld_am1[] = { 'A', 0 };
618 static const WCHAR fld_am2[] = { 'A', 'M', 0 };
619 int spec;
620 WCHAR buffer[80];
621 LPCWSTR bufptr;
622 SIZE size;
624 TRACE ("%d,%d\n", infoPtr->nrFields, count);
625 if (count>infoPtr->nrFields || count < 0) {
626 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
627 return;
630 if (!infoPtr->fieldspec) return;
632 spec = infoPtr->fieldspec[count];
633 if (spec & DT_STRING) {
634 int txtlen = infoPtr->buflen[count];
636 if (txtlen > 79)
637 txtlen = 79;
638 memcpy (buffer, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
639 buffer[txtlen] = 0;
640 bufptr = buffer;
642 else {
643 switch (spec) {
644 case ONEDIGITDAY:
645 case ONEDIGIT12HOUR:
646 case ONEDIGIT24HOUR:
647 case ONEDIGITSECOND:
648 case ONEDIGITMINUTE:
649 case ONEDIGITMONTH:
650 case ONEDIGITYEAR:
651 /* these seem to use a two byte field */
652 case TWODIGITDAY:
653 case TWODIGIT12HOUR:
654 case TWODIGIT24HOUR:
655 case TWODIGITSECOND:
656 case TWODIGITMINUTE:
657 case TWODIGITMONTH:
658 case TWODIGITYEAR:
659 bufptr = fld_d2W;
660 break;
661 case INVALIDFULLYEAR:
662 case FULLYEAR:
663 bufptr = fld_d4W;
664 break;
665 case THREECHARMONTH:
666 case FULLMONTH:
667 case THREECHARDAY:
668 case FULLDAY:
670 static const WCHAR fld_day[] = {'W','e','d','n','e','s','d','a','y',0};
671 static const WCHAR fld_abbrday[] = {'W','e','d',0};
672 static const WCHAR fld_mon[] = {'S','e','p','t','e','m','b','e','r',0};
673 static const WCHAR fld_abbrmon[] = {'D','e','c',0};
675 const WCHAR *fall;
676 LCTYPE lctype;
677 INT i, max_count;
678 LONG cx;
680 /* choose locale data type and fallback string */
681 switch (spec) {
682 case THREECHARDAY:
683 fall = fld_abbrday;
684 lctype = LOCALE_SABBREVDAYNAME1;
685 max_count = 7;
686 break;
687 case FULLDAY:
688 fall = fld_day;
689 lctype = LOCALE_SDAYNAME1;
690 max_count = 7;
691 break;
692 case THREECHARMONTH:
693 fall = fld_abbrmon;
694 lctype = LOCALE_SABBREVMONTHNAME1;
695 max_count = 12;
696 break;
697 default: /* FULLMONTH */
698 fall = fld_mon;
699 lctype = LOCALE_SMONTHNAME1;
700 max_count = 12;
701 break;
704 cx = 0;
705 for (i = 0; i < max_count; i++)
707 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, lctype + i,
708 buffer, lstrlenW(buffer)))
710 GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &size);
711 if (size.cx > cx) cx = size.cx;
713 else /* locale independent fallback on failure */
715 GetTextExtentPoint32W(hdc, fall, lstrlenW(fall), &size);
716 cx = size.cx;
717 break;
720 *width = cx;
721 return;
723 case ONELETTERAMPM:
724 bufptr = fld_am1;
725 break;
726 case TWOLETTERAMPM:
727 bufptr = fld_am2;
728 break;
729 default:
730 bufptr = fld_d1W;
731 break;
734 GetTextExtentPoint32W (hdc, bufptr, strlenW(bufptr), &size);
735 *width = size.cx;
738 static void
739 DATETIME_Refresh (DATETIME_INFO *infoPtr, HDC hdc)
741 TRACE("\n");
743 if (infoPtr->dateValid) {
744 int i, prevright;
745 RECT *field;
746 RECT *rcDraw = &infoPtr->rcDraw;
747 SIZE size;
748 COLORREF oldTextColor;
749 SHORT fieldWidth = 0;
750 HFONT oldFont = SelectObject (hdc, infoPtr->hFont);
751 INT oldBkMode = SetBkMode (hdc, TRANSPARENT);
752 WCHAR txt[80];
754 DATETIME_ReturnTxt (infoPtr, 0, txt, sizeof(txt)/sizeof(txt[0]));
755 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
756 rcDraw->bottom = size.cy + 2;
758 prevright = infoPtr->checkbox.right = ((infoPtr->dwStyle & DTS_SHOWNONE) ? 18 : 2);
760 for (i = 0; i < infoPtr->nrFields; i++) {
761 DATETIME_ReturnTxt (infoPtr, i, txt, sizeof(txt)/sizeof(txt[0]));
762 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
763 DATETIME_ReturnFieldWidth (infoPtr, hdc, i, &fieldWidth);
764 field = &infoPtr->fieldRect[i];
765 field->left = prevright;
766 field->right = prevright + fieldWidth;
767 field->top = rcDraw->top;
768 field->bottom = rcDraw->bottom;
769 prevright = field->right;
771 if (infoPtr->dwStyle & WS_DISABLED)
772 oldTextColor = SetTextColor (hdc, comctl32_color.clrGrayText);
773 else if ((infoPtr->haveFocus) && (i == infoPtr->select)) {
774 RECT selection;
776 /* fill if focused */
777 HBRUSH hbr = CreateSolidBrush (comctl32_color.clrActiveCaption);
779 if (infoPtr->nCharsEntered)
781 memcpy(txt, infoPtr->charsEntered, infoPtr->nCharsEntered * sizeof(WCHAR));
782 txt[infoPtr->nCharsEntered] = 0;
783 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
786 selection.left = 0;
787 selection.top = 0;
788 selection.right = size.cx;
789 selection.bottom = size.cy;
790 /* center rectangle */
791 OffsetRect(&selection, (field->right + field->left - size.cx)/2,
792 (field->bottom - size.cy)/2);
794 FillRect(hdc, &selection, hbr);
795 DeleteObject (hbr);
796 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindow);
798 else
799 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindowText);
801 /* draw the date text using the colour set above */
802 DrawTextW (hdc, txt, strlenW(txt), field, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
803 SetTextColor (hdc, oldTextColor);
805 SetBkMode (hdc, oldBkMode);
806 SelectObject (hdc, oldFont);
809 if (!(infoPtr->dwStyle & DTS_UPDOWN)) {
810 DrawFrameControl(hdc, &infoPtr->calbutton, DFC_SCROLL,
811 DFCS_SCROLLDOWN | (infoPtr->bCalDepressed ? DFCS_PUSHED : 0) |
812 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
817 static INT
818 DATETIME_HitTest (const DATETIME_INFO *infoPtr, POINT pt)
820 int i;
822 TRACE ("%d, %d\n", pt.x, pt.y);
824 if (PtInRect (&infoPtr->calbutton, pt)) return DTHT_MCPOPUP;
825 if (PtInRect (&infoPtr->checkbox, pt)) return DTHT_CHECKBOX;
827 for (i = 0; i < infoPtr->nrFields; i++) {
828 if (PtInRect (&infoPtr->fieldRect[i], pt)) return i;
831 return DTHT_NONE;
834 /* Returns index of the nearest preceding date field from given,
835 or -1 if none was found */
836 static int DATETIME_GetPrevDateField(const DATETIME_INFO *infoPtr, int i)
838 for(--i; i >= 0; i--)
840 if (infoPtr->fieldspec[i] & DTHT_DATEFIELD) return i;
842 return -1;
845 static void
846 DATETIME_ApplySelectedField (DATETIME_INFO *infoPtr)
848 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
849 int i, val = 0;
850 BOOL clamp_day = FALSE;
851 SYSTEMTIME date = infoPtr->date;
852 int oldyear;
854 if (infoPtr->select == -1 || infoPtr->nCharsEntered == 0)
855 return;
857 if ((infoPtr->fieldspec[fieldNum] == ONELETTERAMPM) ||
858 (infoPtr->fieldspec[fieldNum] == TWOLETTERAMPM))
859 val = infoPtr->charsEntered[0];
860 else {
861 for (i=0; i<infoPtr->nCharsEntered; i++)
862 val = val * 10 + infoPtr->charsEntered[i] - '0';
865 infoPtr->nCharsEntered = 0;
867 switch (infoPtr->fieldspec[fieldNum]) {
868 case ONEDIGITYEAR:
869 case TWODIGITYEAR:
870 oldyear = date.wYear;
871 date.wYear = date.wYear - (date.wYear%100) + val;
873 if (DATETIME_IsDateInValidRange(infoPtr, &date))
874 clamp_day = TRUE;
875 else
876 date.wYear = oldyear;
878 break;
879 case INVALIDFULLYEAR:
880 case FULLYEAR:
881 oldyear = date.wYear;
882 date.wYear = val;
884 if (DATETIME_IsDateInValidRange(infoPtr, &date))
885 clamp_day = TRUE;
886 else
887 date.wYear = oldyear;
889 break;
890 case ONEDIGITMONTH:
891 case TWODIGITMONTH:
892 date.wMonth = val;
893 clamp_day = TRUE;
894 break;
895 case ONEDIGITDAY:
896 case TWODIGITDAY:
897 date.wDay = val;
898 break;
899 case ONEDIGIT12HOUR:
900 case TWODIGIT12HOUR:
901 if (val >= 24)
902 val -= 20;
904 if (val >= 13)
905 date.wHour = val;
906 else if (val != 0) {
907 if (date.wHour >= 12) /* preserve current AM/PM state */
908 date.wHour = (val == 12 ? 12 : val + 12);
909 else
910 date.wHour = (val == 12 ? 0 : val);
912 break;
913 case ONEDIGIT24HOUR:
914 case TWODIGIT24HOUR:
915 date.wHour = val;
916 break;
917 case ONEDIGITMINUTE:
918 case TWODIGITMINUTE:
919 date.wMinute = val;
920 break;
921 case ONEDIGITSECOND:
922 case TWODIGITSECOND:
923 date.wSecond = val;
924 break;
925 case ONELETTERAMPM:
926 case TWOLETTERAMPM:
927 if (val == 'a' || val == 'A') {
928 if (date.wHour >= 12)
929 date.wHour -= 12;
930 } else if (val == 'p' || val == 'P') {
931 if (date.wHour < 12)
932 date.wHour += 12;
934 break;
937 if (clamp_day && date.wDay > MONTHCAL_MonthLength(date.wMonth, date.wYear))
938 date.wDay = MONTHCAL_MonthLength(date.wMonth, date.wYear);
940 if (DATETIME_SetSystemTime(infoPtr, GDT_VALID, &date))
941 DATETIME_SendDateTimeChangeNotify (infoPtr);
944 static void
945 DATETIME_SetSelectedField (DATETIME_INFO *infoPtr, int select)
947 DATETIME_ApplySelectedField(infoPtr);
949 infoPtr->select = select;
950 infoPtr->nCharsEntered = 0;
953 static LRESULT
954 DATETIME_LButtonDown (DATETIME_INFO *infoPtr, INT x, INT y)
956 POINT pt;
957 int new;
959 pt.x = x;
960 pt.y = y;
961 new = DATETIME_HitTest (infoPtr, pt);
963 SetFocus(infoPtr->hwndSelf);
965 if (!(new & DTHT_NODATEMASK) || (new == DTHT_NONE))
967 if (new == DTHT_NONE)
968 new = infoPtr->nrFields - 1;
969 else
971 /* hitting string part moves selection to next date field to left */
972 if (infoPtr->fieldspec[new] & DT_STRING)
974 new = DATETIME_GetPrevDateField(infoPtr, new);
975 if (new == -1) return 0;
977 /* never select full day of week */
978 if (infoPtr->fieldspec[new] == FULLDAY) return 0;
982 DATETIME_SetSelectedField(infoPtr, new);
984 if (infoPtr->select == DTHT_MCPOPUP) {
985 RECT rcMonthCal;
986 POINT pos;
987 SendMessageW(infoPtr->hMonthCal, MCM_GETMINREQRECT, 0, (LPARAM)&rcMonthCal);
989 /* FIXME: button actually is only depressed during dropdown of the */
990 /* calendar control and when the mouse is over the button window */
991 infoPtr->bCalDepressed = TRUE;
993 /* recalculate the position of the monthcal popup */
994 if(infoPtr->dwStyle & DTS_RIGHTALIGN)
995 pos.x = infoPtr->calbutton.left - (rcMonthCal.right - rcMonthCal.left);
996 else
997 /* FIXME: this should be after the area reserved for the checkbox */
998 pos.x = infoPtr->rcDraw.left;
1000 pos.y = infoPtr->rcClient.bottom;
1001 OffsetRect( &rcMonthCal, pos.x, pos.y );
1002 MapWindowPoints( infoPtr->hwndSelf, 0, (POINT *)&rcMonthCal, 2 );
1003 SetWindowPos(infoPtr->hMonthCal, 0, rcMonthCal.left, rcMonthCal.top,
1004 rcMonthCal.right - rcMonthCal.left, rcMonthCal.bottom - rcMonthCal.top, 0);
1006 if(IsWindowVisible(infoPtr->hMonthCal)) {
1007 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1008 infoPtr->bDropdownEnabled = FALSE;
1009 DATETIME_SendSimpleNotify (infoPtr, DTN_CLOSEUP);
1010 } else {
1011 const SYSTEMTIME *lprgSysTimeArray = &infoPtr->date;
1012 TRACE("update calendar %04d/%02d/%02d\n",
1013 lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay);
1014 SendMessageW(infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
1016 if (infoPtr->bDropdownEnabled) {
1017 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
1018 DATETIME_SendSimpleNotify (infoPtr, DTN_DROPDOWN);
1020 infoPtr->bDropdownEnabled = TRUE;
1023 TRACE ("dt:%p mc:%p mc parent:%p, desktop:%p\n",
1024 infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hwndNotify, GetDesktopWindow ());
1027 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1029 return 0;
1033 static LRESULT
1034 DATETIME_LButtonUp (DATETIME_INFO *infoPtr)
1036 if(infoPtr->bCalDepressed) {
1037 infoPtr->bCalDepressed = FALSE;
1038 InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
1041 return 0;
1045 static LRESULT
1046 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
1048 if (!hdc) {
1049 PAINTSTRUCT ps;
1050 hdc = BeginPaint (infoPtr->hwndSelf, &ps);
1051 DATETIME_Refresh (infoPtr, hdc);
1052 EndPaint (infoPtr->hwndSelf, &ps);
1053 } else {
1054 DATETIME_Refresh (infoPtr, hdc);
1057 /* Not a click on the dropdown box, enabled it */
1058 infoPtr->bDropdownEnabled = TRUE;
1060 return 0;
1064 static LRESULT
1065 DATETIME_Button_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1067 if( HIWORD(wParam) == BN_CLICKED) {
1068 DWORD state = SendMessageW((HWND)lParam, BM_GETCHECK, 0, 0);
1069 infoPtr->dateValid = (state == BST_CHECKED);
1070 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1072 return 0;
1077 static LRESULT
1078 DATETIME_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1080 TRACE("hwndbutton = %p\n", infoPtr->hwndCheckbut);
1081 if(infoPtr->hwndCheckbut == (HWND)lParam)
1082 return DATETIME_Button_Command(infoPtr, wParam, lParam);
1083 return 0;
1087 static LRESULT
1088 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
1090 TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
1091 if (bEnable)
1092 infoPtr->dwStyle &= ~WS_DISABLED;
1093 else
1094 infoPtr->dwStyle |= WS_DISABLED;
1096 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1098 return 0;
1102 static LRESULT
1103 DATETIME_EraseBackground (const DATETIME_INFO *infoPtr, HDC hdc)
1105 HBRUSH hBrush, hSolidBrush = NULL;
1106 RECT rc;
1108 if (infoPtr->dwStyle & WS_DISABLED)
1109 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
1110 else
1112 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
1113 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
1114 if (!hBrush)
1115 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
1118 GetClientRect (infoPtr->hwndSelf, &rc);
1120 FillRect (hdc, &rc, hBrush);
1122 if (hSolidBrush)
1123 DeleteObject(hSolidBrush);
1125 return -1;
1129 static LRESULT
1130 DATETIME_Notify (DATETIME_INFO *infoPtr, const NMHDR *lpnmh)
1132 TRACE ("Got notification %x from %p\n", lpnmh->code, lpnmh->hwndFrom);
1133 TRACE ("info: %p %p %p\n", infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hUpdown);
1135 if (lpnmh->code == MCN_SELECT) {
1136 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1137 infoPtr->dateValid = TRUE;
1138 SendMessageW (infoPtr->hMonthCal, MCM_GETCURSEL, 0, (LPARAM)&infoPtr->date);
1139 TRACE("got from calendar %04d/%02d/%02d day of week %d\n",
1140 infoPtr->date.wYear, infoPtr->date.wMonth, infoPtr->date.wDay, infoPtr->date.wDayOfWeek);
1141 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
1142 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1143 DATETIME_SendDateTimeChangeNotify (infoPtr);
1144 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1146 if ((lpnmh->hwndFrom == infoPtr->hUpdown) && (lpnmh->code == UDN_DELTAPOS)) {
1147 const NM_UPDOWN *lpnmud = (const NM_UPDOWN*)lpnmh;
1148 TRACE("Delta pos %d\n", lpnmud->iDelta);
1149 infoPtr->pendingUpdown = lpnmud->iDelta;
1151 return 0;
1155 static LRESULT
1156 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode)
1158 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1159 int wrap = 0;
1160 int new;
1162 if (!(infoPtr->haveFocus)) return 0;
1163 if ((fieldNum==0) && (infoPtr->select)) return 0;
1165 if (infoPtr->select & FORMATCALLMASK) {
1166 FIXME ("Callbacks not implemented yet\n");
1169 switch (vkCode) {
1170 case VK_ADD:
1171 case VK_UP:
1172 infoPtr->nCharsEntered = 0;
1173 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1174 DATETIME_SendDateTimeChangeNotify (infoPtr);
1175 break;
1176 case VK_SUBTRACT:
1177 case VK_DOWN:
1178 infoPtr->nCharsEntered = 0;
1179 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1180 DATETIME_SendDateTimeChangeNotify (infoPtr);
1181 break;
1182 case VK_HOME:
1183 infoPtr->nCharsEntered = 0;
1184 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MIN);
1185 DATETIME_SendDateTimeChangeNotify (infoPtr);
1186 break;
1187 case VK_END:
1188 infoPtr->nCharsEntered = 0;
1189 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MAX);
1190 DATETIME_SendDateTimeChangeNotify (infoPtr);
1191 break;
1192 case VK_LEFT:
1193 new = infoPtr->select;
1194 do {
1195 if (new == 0) {
1196 new = new - 1;
1197 wrap++;
1198 } else {
1199 new--;
1201 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1202 if (new != infoPtr->select)
1203 DATETIME_SetSelectedField(infoPtr, new);
1204 break;
1205 case VK_RIGHT:
1206 new = infoPtr->select;
1207 do {
1208 new++;
1209 if (new==infoPtr->nrFields) {
1210 new = 0;
1211 wrap++;
1213 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1214 if (new != infoPtr->select)
1215 DATETIME_SetSelectedField(infoPtr, new);
1216 break;
1219 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1221 return 0;
1225 static LRESULT
1226 DATETIME_Char (DATETIME_INFO *infoPtr, WPARAM vkCode)
1228 int fieldNum, fieldSpec;
1230 fieldNum = infoPtr->select & DTHT_DATEFIELD;
1231 fieldSpec = infoPtr->fieldspec[fieldNum];
1233 if (fieldSpec == ONELETTERAMPM || fieldSpec == TWOLETTERAMPM) {
1234 infoPtr->charsEntered[0] = vkCode;
1235 infoPtr->nCharsEntered = 1;
1237 DATETIME_ApplySelectedField(infoPtr);
1238 } else if (vkCode >= '0' && vkCode <= '9') {
1239 int maxChars;
1241 infoPtr->charsEntered[infoPtr->nCharsEntered++] = vkCode;
1243 if (fieldSpec == INVALIDFULLYEAR || fieldSpec == FULLYEAR)
1244 maxChars = 4;
1245 else
1246 maxChars = 2;
1248 if ((fieldSpec == ONEDIGIT12HOUR ||
1249 fieldSpec == TWODIGIT12HOUR ||
1250 fieldSpec == ONEDIGIT24HOUR ||
1251 fieldSpec == TWODIGIT24HOUR) &&
1252 (infoPtr->nCharsEntered == 1))
1254 if (vkCode >= '3')
1255 maxChars = 1;
1258 if (maxChars == infoPtr->nCharsEntered)
1259 DATETIME_ApplySelectedField(infoPtr);
1262 return 0;
1266 static LRESULT
1267 DATETIME_VScroll (DATETIME_INFO *infoPtr, WORD wScroll)
1269 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1271 if ((SHORT)LOWORD(wScroll) != SB_THUMBPOSITION) return 0;
1272 if (!(infoPtr->haveFocus)) return 0;
1273 if ((fieldNum==0) && (infoPtr->select)) return 0;
1275 if (infoPtr->pendingUpdown >= 0) {
1276 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1277 DATETIME_SendDateTimeChangeNotify (infoPtr);
1279 else {
1280 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1281 DATETIME_SendDateTimeChangeNotify (infoPtr);
1284 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1286 return 0;
1290 static LRESULT
1291 DATETIME_KillFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1293 TRACE("lost focus to %p\n", lostFocus);
1295 if (infoPtr->haveFocus) {
1296 DATETIME_SendSimpleNotify (infoPtr, NM_KILLFOCUS);
1297 infoPtr->haveFocus = 0;
1298 DATETIME_SetSelectedField (infoPtr, -1);
1301 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
1303 return 0;
1307 static LRESULT
1308 DATETIME_NCCreate (HWND hwnd, const CREATESTRUCTW *lpcs)
1310 DWORD dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1311 /* force control to have client edge */
1312 dwExStyle |= WS_EX_CLIENTEDGE;
1313 SetWindowLongW(hwnd, GWL_EXSTYLE, dwExStyle);
1315 return DefWindowProcW(hwnd, WM_NCCREATE, 0, (LPARAM)lpcs);
1319 static LRESULT
1320 DATETIME_SetFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1322 TRACE("got focus from %p\n", lostFocus);
1324 /* if monthcal is open and it loses focus, close monthcal */
1325 if (infoPtr->hMonthCal && (lostFocus == infoPtr->hMonthCal) &&
1326 IsWindowVisible(infoPtr->hMonthCal))
1328 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1329 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1330 /* note: this get triggered even if monthcal loses focus to a dropdown
1331 * box click, which occurs without an intermediate WM_PAINT call
1333 infoPtr->bDropdownEnabled = FALSE;
1334 return 0;
1337 if (infoPtr->haveFocus == 0) {
1338 DATETIME_SendSimpleNotify (infoPtr, NM_SETFOCUS);
1339 infoPtr->haveFocus = DTHT_GOTFOCUS;
1342 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1344 return 0;
1348 static BOOL
1349 DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr)
1351 NMDATETIMECHANGE dtdtc;
1353 dtdtc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1354 dtdtc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1355 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1357 dtdtc.dwFlags = infoPtr->dateValid ? GDT_VALID : GDT_NONE;
1359 dtdtc.st = infoPtr->date;
1360 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1361 dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1365 static BOOL
1366 DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code)
1368 NMHDR nmhdr;
1370 TRACE("%x\n", code);
1371 nmhdr.hwndFrom = infoPtr->hwndSelf;
1372 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1373 nmhdr.code = code;
1375 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1376 nmhdr.idFrom, (LPARAM)&nmhdr);
1379 static LRESULT
1380 DATETIME_Size (DATETIME_INFO *infoPtr, INT width, INT height)
1382 /* set size */
1383 infoPtr->rcClient.bottom = height;
1384 infoPtr->rcClient.right = width;
1386 TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1388 infoPtr->rcDraw = infoPtr->rcClient;
1390 if (infoPtr->dwStyle & DTS_UPDOWN) {
1391 SetWindowPos(infoPtr->hUpdown, NULL,
1392 infoPtr->rcClient.right-14, 0,
1393 15, infoPtr->rcClient.bottom - infoPtr->rcClient.top,
1394 SWP_NOACTIVATE | SWP_NOZORDER);
1396 else {
1397 /* set the size of the button that drops the calendar down */
1398 /* FIXME: account for style that allows button on left side */
1399 infoPtr->calbutton.top = infoPtr->rcDraw.top;
1400 infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1401 infoPtr->calbutton.left = infoPtr->rcDraw.right-15;
1402 infoPtr->calbutton.right = infoPtr->rcDraw.right;
1405 /* set enable/disable button size for show none style being enabled */
1406 /* FIXME: these dimensions are completely incorrect */
1407 infoPtr->checkbox.top = infoPtr->rcDraw.top;
1408 infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1409 infoPtr->checkbox.left = infoPtr->rcDraw.left;
1410 infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1412 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1414 return 0;
1417 static LRESULT
1418 DATETIME_StyleChanging(DATETIME_INFO *infoPtr, WPARAM wStyleType, STYLESTRUCT *lpss)
1420 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1421 wStyleType, lpss->styleOld, lpss->styleNew);
1423 /* block DTS_SHOWNONE change */
1424 if ((lpss->styleNew ^ lpss->styleOld) & DTS_SHOWNONE)
1426 if (lpss->styleOld & DTS_SHOWNONE)
1427 lpss->styleNew |= DTS_SHOWNONE;
1428 else
1429 lpss->styleNew &= ~DTS_SHOWNONE;
1432 return 0;
1435 static LRESULT
1436 DATETIME_StyleChanged(DATETIME_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
1438 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1439 wStyleType, lpss->styleOld, lpss->styleNew);
1441 if (wStyleType != GWL_STYLE) return 0;
1443 infoPtr->dwStyle = lpss->styleNew;
1445 if ( !(lpss->styleOld & DTS_SHOWNONE) && (lpss->styleNew & DTS_SHOWNONE) ) {
1446 infoPtr->hwndCheckbut = CreateWindowExW (0, WC_BUTTONW, 0, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1447 2, 2, 13, 13, infoPtr->hwndSelf, 0,
1448 (HINSTANCE)GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_HINSTANCE), 0);
1449 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, infoPtr->dateValid ? 1 : 0, 0);
1451 if ( (lpss->styleOld & DTS_SHOWNONE) && !(lpss->styleNew & DTS_SHOWNONE) ) {
1452 DestroyWindow(infoPtr->hwndCheckbut);
1453 infoPtr->hwndCheckbut = 0;
1455 if ( !(lpss->styleOld & DTS_UPDOWN) && (lpss->styleNew & DTS_UPDOWN) ) {
1456 infoPtr->hUpdown = CreateUpDownControl (WS_CHILD | WS_BORDER | WS_VISIBLE, 120, 1, 20, 20,
1457 infoPtr->hwndSelf, 1, 0, 0, UD_MAXVAL, UD_MINVAL, 0);
1459 if ( (lpss->styleOld & DTS_UPDOWN) && !(lpss->styleNew & DTS_UPDOWN) ) {
1460 DestroyWindow(infoPtr->hUpdown);
1461 infoPtr->hUpdown = 0;
1464 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1465 return 0;
1469 static LRESULT
1470 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1472 infoPtr->hFont = font;
1473 if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1474 return 0;
1478 static LRESULT
1479 DATETIME_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1481 DATETIME_INFO *infoPtr = Alloc (sizeof(DATETIME_INFO));
1482 STYLESTRUCT ss = { 0, lpcs->style };
1484 if (!infoPtr) return -1;
1486 infoPtr->hwndSelf = hwnd;
1487 infoPtr->dwStyle = lpcs->style;
1489 infoPtr->nrFieldsAllocated = 32;
1490 infoPtr->fieldspec = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1491 infoPtr->fieldRect = Alloc (infoPtr->nrFieldsAllocated * sizeof(RECT));
1492 infoPtr->buflen = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1493 infoPtr->hwndNotify = lpcs->hwndParent;
1494 infoPtr->select = -1; /* initially, nothing is selected */
1495 infoPtr->bDropdownEnabled = TRUE;
1497 DATETIME_StyleChanged(infoPtr, GWL_STYLE, &ss);
1498 DATETIME_SetFormatW (infoPtr, 0);
1500 /* create the monthcal control */
1501 infoPtr->hMonthCal = CreateWindowExW (0, MONTHCAL_CLASSW, 0, WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS,
1502 0, 0, 0, 0, infoPtr->hwndSelf, 0, 0, 0);
1504 /* initialize info structure */
1505 GetLocalTime (&infoPtr->date);
1506 infoPtr->dateValid = TRUE;
1507 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1509 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1511 return 0;
1516 static LRESULT
1517 DATETIME_Destroy (DATETIME_INFO *infoPtr)
1519 if (infoPtr->hwndCheckbut)
1520 DestroyWindow(infoPtr->hwndCheckbut);
1521 if (infoPtr->hUpdown)
1522 DestroyWindow(infoPtr->hUpdown);
1523 if (infoPtr->hMonthCal)
1524 DestroyWindow(infoPtr->hMonthCal);
1525 SetWindowLongPtrW( infoPtr->hwndSelf, 0, 0 ); /* clear infoPtr */
1526 Free (infoPtr->buflen);
1527 Free (infoPtr->fieldRect);
1528 Free (infoPtr->fieldspec);
1529 Free (infoPtr);
1530 return 0;
1534 static INT
1535 DATETIME_GetText (const DATETIME_INFO *infoPtr, INT count, LPWSTR dst)
1537 WCHAR buf[80];
1538 int i;
1540 if (!dst || (count <= 0)) return 0;
1542 dst[0] = 0;
1543 for (i = 0; i < infoPtr->nrFields; i++)
1545 DATETIME_ReturnTxt(infoPtr, i, buf, sizeof(buf)/sizeof(buf[0]));
1546 if ((strlenW(dst) + strlenW(buf)) < count)
1547 strcatW(dst, buf);
1548 else break;
1550 return strlenW(dst);
1554 static LRESULT WINAPI
1555 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1557 DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1558 LRESULT ret;
1560 TRACE ("%x, %lx, %lx\n", uMsg, wParam, lParam);
1562 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
1563 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
1565 switch (uMsg) {
1567 case DTM_GETSYSTEMTIME:
1568 return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1570 case DTM_SETSYSTEMTIME:
1571 return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1573 case DTM_GETRANGE:
1574 ret = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1575 return ret ? ret : 1; /* bug emulation */
1577 case DTM_SETRANGE:
1578 return SendMessageW (infoPtr->hMonthCal, MCM_SETRANGE, wParam, lParam);
1580 case DTM_SETFORMATA:
1581 return DATETIME_SetFormatA (infoPtr, (LPCSTR)lParam);
1583 case DTM_SETFORMATW:
1584 return DATETIME_SetFormatW (infoPtr, (LPCWSTR)lParam);
1586 case DTM_GETMONTHCAL:
1587 return (LRESULT)infoPtr->hMonthCal;
1589 case DTM_SETMCCOLOR:
1590 return SendMessageW (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
1592 case DTM_GETMCCOLOR:
1593 return SendMessageW (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
1595 case DTM_SETMCFONT:
1596 return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1598 case DTM_GETMCFONT:
1599 return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1601 case WM_NOTIFY:
1602 return DATETIME_Notify (infoPtr, (LPNMHDR)lParam);
1604 case WM_ENABLE:
1605 return DATETIME_Enable (infoPtr, (BOOL)wParam);
1607 case WM_ERASEBKGND:
1608 return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1610 case WM_GETDLGCODE:
1611 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1613 case WM_PRINTCLIENT:
1614 case WM_PAINT:
1615 return DATETIME_Paint (infoPtr, (HDC)wParam);
1617 case WM_KEYDOWN:
1618 return DATETIME_KeyDown (infoPtr, wParam);
1620 case WM_CHAR:
1621 return DATETIME_Char (infoPtr, wParam);
1623 case WM_KILLFOCUS:
1624 return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1626 case WM_NCCREATE:
1627 return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1629 case WM_SETFOCUS:
1630 return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1632 case WM_SIZE:
1633 return DATETIME_Size (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1635 case WM_LBUTTONDOWN:
1636 return DATETIME_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1638 case WM_LBUTTONUP:
1639 return DATETIME_LButtonUp (infoPtr);
1641 case WM_VSCROLL:
1642 return DATETIME_VScroll (infoPtr, (WORD)wParam);
1644 case WM_CREATE:
1645 return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1647 case WM_DESTROY:
1648 return DATETIME_Destroy (infoPtr);
1650 case WM_COMMAND:
1651 return DATETIME_Command (infoPtr, wParam, lParam);
1653 case WM_STYLECHANGING:
1654 return DATETIME_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1656 case WM_STYLECHANGED:
1657 return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1659 case WM_SETFONT:
1660 return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1662 case WM_GETFONT:
1663 return (LRESULT) infoPtr->hFont;
1665 case WM_GETTEXT:
1666 return (LRESULT) DATETIME_GetText(infoPtr, wParam, (LPWSTR)lParam);
1668 case WM_SETTEXT:
1669 return CB_ERR;
1671 default:
1672 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
1673 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
1674 uMsg, wParam, lParam);
1675 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1680 void
1681 DATETIME_Register (void)
1683 WNDCLASSW wndClass;
1685 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1686 wndClass.style = CS_GLOBALCLASS;
1687 wndClass.lpfnWndProc = DATETIME_WindowProc;
1688 wndClass.cbClsExtra = 0;
1689 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1690 wndClass.hCursor = LoadCursorW (0, (LPCWSTR)IDC_ARROW);
1691 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1692 wndClass.lpszClassName = DATETIMEPICK_CLASSW;
1694 RegisterClassW (&wndClass);
1698 void
1699 DATETIME_Unregister (void)
1701 UnregisterClassW (DATETIMEPICK_CLASSW, NULL);