comctl32/datetime: Use locale data to compute field widths for day and month.
[wine/hacks.git] / dlls / comctl32 / datetime.c
blob9b4926c189250afd39e239ee03e5efed4e3da902
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>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * NOTE
24 * This code was audited for completeness against the documented features
25 * of Comctl32.dll version 6.0 on Oct. 20, 2004, by Dimitrie O. Paun.
27 * Unless otherwise noted, we believe this code to be complete, as per
28 * the specification mentioned above.
29 * If you discover missing features, or bugs, please note them below.
31 * TODO:
32 * -- DTS_APPCANPARSE
33 * -- DTS_SHORTDATECENTURYFORMAT
34 * -- DTN_CLOSEUP
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 int bDropdownEnabled;
76 int select;
77 HFONT hFont;
78 int nrFieldsAllocated;
79 int nrFields;
80 int haveFocus;
81 int *fieldspec;
82 RECT *fieldRect;
83 int *buflen;
84 WCHAR textbuf[256];
85 POINT monthcal_pos;
86 int pendingUpdown;
87 } DATETIME_INFO, *LPDATETIME_INFO;
89 /* in monthcal.c */
90 extern int MONTHCAL_MonthLength(int month, int year);
91 extern int MONTHCAL_CalculateDayOfWeek(WORD day, WORD month, WORD year);
93 /* this list of defines is closely related to `allowedformatchars' defined
94 * in datetime.c; the high nibble indicates the `base type' of the format
95 * specifier.
96 * Do not change without first reading DATETIME_UseFormat.
100 #define DT_END_FORMAT 0
101 #define ONEDIGITDAY 0x01
102 #define TWODIGITDAY 0x02
103 #define THREECHARDAY 0x03
104 #define FULLDAY 0x04
105 #define ONEDIGIT12HOUR 0x11
106 #define TWODIGIT12HOUR 0x12
107 #define ONEDIGIT24HOUR 0x21
108 #define TWODIGIT24HOUR 0x22
109 #define ONEDIGITMINUTE 0x31
110 #define TWODIGITMINUTE 0x32
111 #define ONEDIGITMONTH 0x41
112 #define TWODIGITMONTH 0x42
113 #define THREECHARMONTH 0x43
114 #define FULLMONTH 0x44
115 #define ONEDIGITSECOND 0x51
116 #define TWODIGITSECOND 0x52
117 #define ONELETTERAMPM 0x61
118 #define TWOLETTERAMPM 0x62
119 #define ONEDIGITYEAR 0x71
120 #define TWODIGITYEAR 0x72
121 #define INVALIDFULLYEAR 0x73 /* FIXME - yyy is not valid - we'll treat it as yyyy */
122 #define FULLYEAR 0x74
123 #define FORMATCALLBACK 0x81 /* -> maximum of 0x80 callbacks possible */
124 #define FORMATCALLMASK 0x80
125 #define DT_STRING 0x0100
127 #define DTHT_DATEFIELD 0xff /* for hit-testing */
129 #define DTHT_NONE 0x1000
130 #define DTHT_CHECKBOX 0x2000 /* these should end at '00' , to make */
131 #define DTHT_MCPOPUP 0x3000 /* & DTHT_DATEFIELD 0 when DATETIME_KeyDown */
132 #define DTHT_GOTFOCUS 0x4000 /* tests for date-fields */
133 #define DTHT_NODATEMASK 0xf000 /* to mask check and drop down from others */
135 static BOOL DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code);
136 static BOOL DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr);
137 extern void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to);
138 static const WCHAR allowedformatchars[] = {'d', 'h', 'H', 'm', 'M', 's', 't', 'y', 'X', 0};
139 static const int maxrepetition [] = {4,2,2,2,4,2,2,4,-1};
142 static DWORD
143 DATETIME_GetSystemTime (const DATETIME_INFO *infoPtr, SYSTEMTIME *systime)
145 if (!systime) return GDT_NONE;
147 if ((infoPtr->dwStyle & DTS_SHOWNONE) &&
148 (SendMessageW (infoPtr->hwndCheckbut, BM_GETCHECK, 0, 0) == BST_UNCHECKED))
149 return GDT_NONE;
151 *systime = infoPtr->date;
153 return GDT_VALID;
157 static BOOL
158 DATETIME_SetSystemTime (DATETIME_INFO *infoPtr, DWORD flag, const SYSTEMTIME *systime)
160 if (!systime) return 0;
162 TRACE("%04d/%02d/%02d %02d:%02d:%02d\n",
163 systime->wYear, systime->wMonth, systime->wDay,
164 systime->wHour, systime->wMinute, systime->wSecond);
166 if (flag == GDT_VALID) {
167 if (systime->wYear < 1601 || systime->wYear > 30827 ||
168 systime->wMonth < 1 || systime->wMonth > 12 ||
169 systime->wDay < 1 || systime->wDay > 31 ||
170 systime->wHour > 23 ||
171 systime->wMinute > 59 ||
172 systime->wSecond > 59 ||
173 systime->wMilliseconds > 999
175 return FALSE;
177 infoPtr->dateValid = TRUE;
178 infoPtr->date = *systime;
179 /* always store a valid day of week */
180 infoPtr->date.wDayOfWeek =
181 MONTHCAL_CalculateDayOfWeek(infoPtr->date.wDay, infoPtr->date.wMonth,
182 infoPtr->date.wYear);
184 SendMessageW (infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
185 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
186 } else if ((infoPtr->dwStyle & DTS_SHOWNONE) && (flag == GDT_NONE)) {
187 infoPtr->dateValid = FALSE;
188 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_UNCHECKED, 0);
190 else
191 return FALSE;
193 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
194 return TRUE;
198 /***
199 * Split up a formattxt in actions.
200 * See ms documentation for the meaning of the letter codes/'specifiers'.
202 * Notes:
203 * *'dddddd' is handled as 'dddd' plus 'dd'.
204 * *unrecognized formats are strings (here given the type DT_STRING;
205 * start of the string is encoded in lower bits of DT_STRING.
206 * Therefore, 'string' ends finally up as '<show seconds>tring'.
209 static void
210 DATETIME_UseFormat (DATETIME_INFO *infoPtr, LPCWSTR formattxt)
212 unsigned int i;
213 int j, k, len;
214 BOOL inside_literal = FALSE; /* inside '...' */
215 int *nrFields = &infoPtr->nrFields;
217 *nrFields = 0;
218 infoPtr->fieldspec[*nrFields] = 0;
219 len = strlenW(allowedformatchars);
220 k = 0;
222 for (i = 0; formattxt[i]; i++) {
223 TRACE ("\n%d %c:", i, formattxt[i]);
224 if (!inside_literal) {
225 for (j = 0; j < len; j++) {
226 if (allowedformatchars[j]==formattxt[i]) {
227 TRACE ("%c[%d,%x]", allowedformatchars[j], *nrFields, infoPtr->fieldspec[*nrFields]);
228 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
229 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
230 break;
232 if (infoPtr->fieldspec[*nrFields] >> 4 != j) {
233 (*nrFields)++;
234 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
235 break;
237 if ((infoPtr->fieldspec[*nrFields] & 0x0f) == maxrepetition[j]) {
238 (*nrFields)++;
239 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
240 break;
242 infoPtr->fieldspec[*nrFields]++;
243 break;
244 } /* if allowedformatchar */
245 } /* for j */
247 else
248 j = len;
250 if (formattxt[i] == '\'')
252 inside_literal = !inside_literal;
253 continue;
256 /* char is not a specifier: handle char like a string */
257 if (j == len) {
258 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
259 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
260 infoPtr->buflen[*nrFields] = 0;
261 } else if ((infoPtr->fieldspec[*nrFields] & DT_STRING) != DT_STRING) {
262 (*nrFields)++;
263 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
264 infoPtr->buflen[*nrFields] = 0;
266 infoPtr->textbuf[k] = formattxt[i];
267 k++;
268 infoPtr->buflen[*nrFields]++;
269 } /* if j=len */
271 if (*nrFields == infoPtr->nrFieldsAllocated) {
272 FIXME ("out of memory; should reallocate. crash ahead.\n");
274 } /* for i */
276 TRACE("\n");
278 if (infoPtr->fieldspec[*nrFields] != 0) (*nrFields)++;
282 static BOOL
283 DATETIME_SetFormatW (DATETIME_INFO *infoPtr, LPCWSTR lpszFormat)
285 if (!lpszFormat) {
286 WCHAR format_buf[80];
287 DWORD format_item;
289 if (infoPtr->dwStyle & DTS_LONGDATEFORMAT)
290 format_item = LOCALE_SLONGDATE;
291 else if ((infoPtr->dwStyle & DTS_TIMEFORMAT) == DTS_TIMEFORMAT)
292 format_item = LOCALE_STIMEFORMAT;
293 else /* DTS_SHORTDATEFORMAT */
294 format_item = LOCALE_SSHORTDATE;
295 GetLocaleInfoW( GetSystemDefaultLCID(), format_item, format_buf, sizeof(format_buf)/sizeof(format_buf[0]));
296 lpszFormat = format_buf;
299 DATETIME_UseFormat (infoPtr, lpszFormat);
300 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
302 return 1;
306 static BOOL
307 DATETIME_SetFormatA (DATETIME_INFO *infoPtr, LPCSTR lpszFormat)
309 if (lpszFormat) {
310 BOOL retval;
311 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, NULL, 0);
312 LPWSTR wstr = Alloc(len * sizeof(WCHAR));
313 if (wstr) MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, wstr, len);
314 retval = DATETIME_SetFormatW (infoPtr, wstr);
315 Free (wstr);
316 return retval;
318 else
319 return DATETIME_SetFormatW (infoPtr, 0);
324 static void
325 DATETIME_ReturnTxt (const DATETIME_INFO *infoPtr, int count, LPWSTR result, int resultSize)
327 static const WCHAR fmt_dW[] = { '%', 'd', 0 };
328 static const WCHAR fmt__2dW[] = { '%', '.', '2', 'd', 0 };
329 static const WCHAR fmt__3sW[] = { '%', '.', '3', 's', 0 };
330 SYSTEMTIME date = infoPtr->date;
331 int spec;
332 WCHAR buffer[80];
334 *result=0;
335 TRACE ("%d,%d\n", infoPtr->nrFields, count);
336 if (count>infoPtr->nrFields || count < 0) {
337 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
338 return;
341 if (!infoPtr->fieldspec) return;
343 spec = infoPtr->fieldspec[count];
344 if (spec & DT_STRING) {
345 int txtlen = infoPtr->buflen[count];
347 if (txtlen > resultSize)
348 txtlen = resultSize - 1;
349 memcpy (result, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
350 result[txtlen] = 0;
351 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
352 return;
356 switch (spec) {
357 case DT_END_FORMAT:
358 *result = 0;
359 break;
360 case ONEDIGITDAY:
361 wsprintfW (result, fmt_dW, date.wDay);
362 break;
363 case TWODIGITDAY:
364 wsprintfW (result, fmt__2dW, date.wDay);
365 break;
366 case THREECHARDAY:
367 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1+(date.wDayOfWeek+6)%7, result, 4);
368 /*wsprintfW (result,"%.3s",days[date.wDayOfWeek]);*/
369 break;
370 case FULLDAY:
371 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME1+(date.wDayOfWeek+6)%7, result, resultSize);
372 break;
373 case ONEDIGIT12HOUR:
374 if (date.wHour == 0) {
375 result[0] = '1';
376 result[1] = '2';
377 result[2] = 0;
379 else
380 wsprintfW (result, fmt_dW, date.wHour - (date.wHour > 12 ? 12 : 0));
381 break;
382 case TWODIGIT12HOUR:
383 if (date.wHour == 0) {
384 result[0] = '1';
385 result[1] = '2';
386 result[2] = 0;
388 else
389 wsprintfW (result, fmt__2dW, date.wHour - (date.wHour > 12 ? 12 : 0));
390 break;
391 case ONEDIGIT24HOUR:
392 wsprintfW (result, fmt_dW, date.wHour);
393 break;
394 case TWODIGIT24HOUR:
395 wsprintfW (result, fmt__2dW, date.wHour);
396 break;
397 case ONEDIGITSECOND:
398 wsprintfW (result, fmt_dW, date.wSecond);
399 break;
400 case TWODIGITSECOND:
401 wsprintfW (result, fmt__2dW, date.wSecond);
402 break;
403 case ONEDIGITMINUTE:
404 wsprintfW (result, fmt_dW, date.wMinute);
405 break;
406 case TWODIGITMINUTE:
407 wsprintfW (result, fmt__2dW, date.wMinute);
408 break;
409 case ONEDIGITMONTH:
410 wsprintfW (result, fmt_dW, date.wMonth);
411 break;
412 case TWODIGITMONTH:
413 wsprintfW (result, fmt__2dW, date.wMonth);
414 break;
415 case THREECHARMONTH:
416 GetLocaleInfoW(GetSystemDefaultLCID(), LOCALE_SMONTHNAME1+date.wMonth -1,
417 buffer, sizeof(buffer)/sizeof(buffer[0]));
418 wsprintfW (result, fmt__3sW, buffer);
419 break;
420 case FULLMONTH:
421 GetLocaleInfoW(GetSystemDefaultLCID(),LOCALE_SMONTHNAME1+date.wMonth -1,
422 result, resultSize);
423 break;
424 case ONELETTERAMPM:
425 result[0] = (date.wHour < 12 ? 'A' : 'P');
426 result[1] = 0;
427 break;
428 case TWOLETTERAMPM:
429 result[0] = (date.wHour < 12 ? 'A' : 'P');
430 result[1] = 'M';
431 result[2] = 0;
432 break;
433 case FORMATCALLBACK:
434 FIXME ("Not implemented\n");
435 result[0] = 'x';
436 result[1] = 0;
437 break;
438 case ONEDIGITYEAR:
439 wsprintfW (result, fmt_dW, date.wYear-10* (int) floor(date.wYear/10));
440 break;
441 case TWODIGITYEAR:
442 wsprintfW (result, fmt__2dW, date.wYear-100* (int) floor(date.wYear/100));
443 break;
444 case INVALIDFULLYEAR:
445 case FULLYEAR:
446 wsprintfW (result, fmt_dW, date.wYear);
447 break;
450 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
453 /* Offsets of days in the week to the weekday of january 1 in a leap year. */
454 static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
456 /* returns the day in the week(0 == sunday, 6 == saturday) */
457 /* day(1 == 1st, 2 == 2nd... etc), year is the year value */
458 static int DATETIME_CalculateDayOfWeek(DWORD day, DWORD month, DWORD year)
460 year-=(month < 3);
462 return((year + year/4 - year/100 + year/400 +
463 DayOfWeekTable[month-1] + day ) % 7);
466 static int wrap(int val, int delta, int minVal, int maxVal)
468 val += delta;
469 if (delta == INT_MIN || val < minVal) return maxVal;
470 if (delta == INT_MAX || val > maxVal) return minVal;
471 return val;
474 static void
475 DATETIME_IncreaseField (DATETIME_INFO *infoPtr, int number, int delta)
477 SYSTEMTIME *date = &infoPtr->date;
479 TRACE ("%d\n", number);
480 if ((number > infoPtr->nrFields) || (number < 0)) return;
482 if ((infoPtr->fieldspec[number] & DTHT_DATEFIELD) == 0) return;
484 switch (infoPtr->fieldspec[number]) {
485 case ONEDIGITYEAR:
486 case TWODIGITYEAR:
487 case FULLYEAR:
488 date->wYear = wrap(date->wYear, delta, 1752, 9999);
489 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(date->wDay,date->wMonth,date->wYear);
490 break;
491 case ONEDIGITMONTH:
492 case TWODIGITMONTH:
493 case THREECHARMONTH:
494 case FULLMONTH:
495 date->wMonth = wrap(date->wMonth, delta, 1, 12);
496 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(date->wDay,date->wMonth,date->wYear);
497 delta = 0;
498 /* fall through */
499 case ONEDIGITDAY:
500 case TWODIGITDAY:
501 case THREECHARDAY:
502 case FULLDAY:
503 date->wDay = wrap(date->wDay, delta, 1, MONTHCAL_MonthLength(date->wMonth, date->wYear));
504 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(date->wDay,date->wMonth,date->wYear);
505 break;
506 case ONELETTERAMPM:
507 case TWOLETTERAMPM:
508 delta *= 12;
509 /* fall through */
510 case ONEDIGIT12HOUR:
511 case TWODIGIT12HOUR:
512 case ONEDIGIT24HOUR:
513 case TWODIGIT24HOUR:
514 date->wHour = wrap(date->wHour, delta, 0, 23);
515 break;
516 case ONEDIGITMINUTE:
517 case TWODIGITMINUTE:
518 date->wMinute = wrap(date->wMinute, delta, 0, 59);
519 break;
520 case ONEDIGITSECOND:
521 case TWODIGITSECOND:
522 date->wSecond = wrap(date->wSecond, delta, 0, 59);
523 break;
524 case FORMATCALLBACK:
525 FIXME ("Not implemented\n");
526 break;
529 /* FYI: On 1752/9/14 the calendar changed and England and the
530 * American colonies changed to the Gregorian calendar. This change
531 * involved having September 14th follow September 2nd. So no date
532 * algorithm works before that date.
534 if (10000 * date->wYear + 100 * date->wMonth + date->wDay < 17520914) {
535 date->wYear = 1752;
536 date->wMonth = 9;
537 date->wDay = 14;
538 date->wSecond = 0;
539 date->wMinute = 0;
540 date->wHour = 0;
545 static void
546 DATETIME_ReturnFieldWidth (const DATETIME_INFO *infoPtr, HDC hdc, int count, SHORT *width)
548 /* fields are a fixed width, determined by the largest possible string */
549 /* presumably, these widths should be language dependent */
550 static const WCHAR fld_d1W[] = { '2', 0 };
551 static const WCHAR fld_d2W[] = { '2', '2', 0 };
552 static const WCHAR fld_d4W[] = { '2', '2', '2', '2', 0 };
553 static const WCHAR fld_am1[] = { 'A', 0 };
554 static const WCHAR fld_am2[] = { 'A', 'M', 0 };
555 int spec;
556 WCHAR buffer[80];
557 LPCWSTR bufptr;
558 SIZE size;
560 TRACE ("%d,%d\n", infoPtr->nrFields, count);
561 if (count>infoPtr->nrFields || count < 0) {
562 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
563 return;
566 if (!infoPtr->fieldspec) return;
568 spec = infoPtr->fieldspec[count];
569 if (spec & DT_STRING) {
570 int txtlen = infoPtr->buflen[count];
572 if (txtlen > 79)
573 txtlen = 79;
574 memcpy (buffer, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
575 buffer[txtlen] = 0;
576 bufptr = buffer;
578 else {
579 switch (spec) {
580 case ONEDIGITDAY:
581 case ONEDIGIT12HOUR:
582 case ONEDIGIT24HOUR:
583 case ONEDIGITSECOND:
584 case ONEDIGITMINUTE:
585 case ONEDIGITMONTH:
586 case ONEDIGITYEAR:
587 /* these seem to use a two byte field */
588 case TWODIGITDAY:
589 case TWODIGIT12HOUR:
590 case TWODIGIT24HOUR:
591 case TWODIGITSECOND:
592 case TWODIGITMINUTE:
593 case TWODIGITMONTH:
594 case TWODIGITYEAR:
595 bufptr = fld_d2W;
596 break;
597 case INVALIDFULLYEAR:
598 case FULLYEAR:
599 bufptr = fld_d4W;
600 break;
601 case THREECHARMONTH:
602 case FULLMONTH:
603 case THREECHARDAY:
604 case FULLDAY:
606 static const WCHAR fld_day[] = {'W','e','d','n','e','s','d','a','y',0};
607 static const WCHAR fld_abbrday[] = {'W','e','d',0};
608 static const WCHAR fld_mon[] = {'S','e','p','t','e','m','b','e','r',0};
609 static const WCHAR fld_abbrmon[] = {'D','e','c',0};
611 const WCHAR *fall;
612 LCTYPE lctype;
613 INT i, max_count;
614 LONG cx;
616 /* choose locale data type and fallback string */
617 switch (spec) {
618 case THREECHARDAY:
619 fall = fld_abbrday;
620 lctype = LOCALE_SABBREVDAYNAME1;
621 max_count = 7;
622 break;
623 case FULLDAY:
624 fall = fld_day;
625 lctype = LOCALE_SDAYNAME1;
626 max_count = 7;
627 break;
628 case THREECHARMONTH:
629 fall = fld_abbrmon;
630 lctype = LOCALE_SABBREVMONTHNAME1;
631 max_count = 12;
632 break;
633 case FULLMONTH:
634 fall = fld_mon;
635 lctype = LOCALE_SMONTHNAME1;
636 max_count = 12;
637 break;
640 cx = 0;
641 for (i = 0; i < max_count; i++)
643 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, lctype + i,
644 buffer, lstrlenW(buffer)))
646 GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &size);
647 if (size.cx > cx) cx = size.cx;
649 else /* locale independent fallback on failure */
651 GetTextExtentPoint32W(hdc, fall, lstrlenW(fall), &size);
652 cx = size.cx;
653 break;
656 *width = cx;
657 return;
659 case ONELETTERAMPM:
660 bufptr = fld_am1;
661 break;
662 case TWOLETTERAMPM:
663 bufptr = fld_am2;
664 break;
665 default:
666 bufptr = fld_d1W;
667 break;
670 GetTextExtentPoint32W (hdc, bufptr, strlenW(bufptr), &size);
671 *width = size.cx;
674 static void
675 DATETIME_Refresh (DATETIME_INFO *infoPtr, HDC hdc)
677 TRACE("\n");
679 if (infoPtr->dateValid) {
680 int i, prevright;
681 RECT *field;
682 RECT *rcDraw = &infoPtr->rcDraw;
683 SIZE size;
684 COLORREF oldTextColor;
685 SHORT fieldWidth = 0;
686 HFONT oldFont = SelectObject (hdc, infoPtr->hFont);
687 INT oldBkMode = SetBkMode (hdc, TRANSPARENT);
688 WCHAR txt[80];
690 DATETIME_ReturnTxt (infoPtr, 0, txt, sizeof(txt)/sizeof(txt[0]));
691 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
692 rcDraw->bottom = size.cy + 2;
694 prevright = infoPtr->checkbox.right = ((infoPtr->dwStyle & DTS_SHOWNONE) ? 18 : 2);
696 for (i = 0; i < infoPtr->nrFields; i++) {
697 DATETIME_ReturnTxt (infoPtr, i, txt, sizeof(txt)/sizeof(txt[0]));
698 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
699 DATETIME_ReturnFieldWidth (infoPtr, hdc, i, &fieldWidth);
700 field = &infoPtr->fieldRect[i];
701 field->left = prevright;
702 field->right = prevright + fieldWidth;
703 field->top = rcDraw->top;
704 field->bottom = rcDraw->bottom;
705 prevright = field->right;
707 if (infoPtr->dwStyle & WS_DISABLED)
708 oldTextColor = SetTextColor (hdc, comctl32_color.clrGrayText);
709 else if ((infoPtr->haveFocus) && (i == infoPtr->select)) {
710 RECT selection;
712 /* fill if focused */
713 HBRUSH hbr = CreateSolidBrush (comctl32_color.clrActiveCaption);
715 selection.right = field->right;
716 selection.left = selection.right - size.cx;
717 selection.top = 0;
718 selection.bottom = size.cy;
719 /* center rectangle */
720 OffsetRect(&selection, 0, (field->bottom - size.cy)/2);
722 FillRect(hdc, &selection, hbr);
723 DeleteObject (hbr);
724 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindow);
726 else
727 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindowText);
729 /* draw the date text using the colour set above */
730 DrawTextW (hdc, txt, strlenW(txt), field, DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
731 SetTextColor (hdc, oldTextColor);
733 SetBkMode (hdc, oldBkMode);
734 SelectObject (hdc, oldFont);
737 if (!(infoPtr->dwStyle & DTS_UPDOWN)) {
738 DrawFrameControl(hdc, &infoPtr->calbutton, DFC_SCROLL,
739 DFCS_SCROLLDOWN | (infoPtr->bCalDepressed ? DFCS_PUSHED : 0) |
740 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
745 static INT
746 DATETIME_HitTest (const DATETIME_INFO *infoPtr, POINT pt)
748 int i;
750 TRACE ("%d, %d\n", pt.x, pt.y);
752 if (PtInRect (&infoPtr->calbutton, pt)) return DTHT_MCPOPUP;
753 if (PtInRect (&infoPtr->checkbox, pt)) return DTHT_CHECKBOX;
755 for (i = 0; i < infoPtr->nrFields; i++) {
756 if (PtInRect (&infoPtr->fieldRect[i], pt)) return i;
759 return DTHT_NONE;
762 /* Returns index of a closest date field from given counting to left
763 or -1 if there's no such fields at left */
764 static int DATETIME_GetPrevDateField(const DATETIME_INFO *infoPtr, int i)
766 for(--i; i >= 0; i--)
768 if (infoPtr->fieldspec[i] & DTHT_DATEFIELD) return i;
770 return -1;
773 static LRESULT
774 DATETIME_LButtonDown (DATETIME_INFO *infoPtr, INT x, INT y)
776 POINT pt;
777 int old, new;
779 pt.x = x;
780 pt.y = y;
781 old = infoPtr->select;
782 new = DATETIME_HitTest (infoPtr, pt);
784 SetFocus(infoPtr->hwndSelf);
786 if (!(new & DTHT_NODATEMASK) || (new == DTHT_NONE))
788 if (new == DTHT_NONE)
789 new = infoPtr->nrFields - 1;
790 else
792 /* hitting string part moves selection to next date field to left */
793 if (infoPtr->fieldspec[new] & DT_STRING)
795 new = DATETIME_GetPrevDateField(infoPtr, new);
796 if (new == -1) return 0;
798 /* never select full day of week */
799 if (infoPtr->fieldspec[new] == FULLDAY) return 0;
802 infoPtr->select = new;
804 if (infoPtr->select == DTHT_MCPOPUP) {
805 RECT rcMonthCal;
806 SendMessageW(infoPtr->hMonthCal, MCM_GETMINREQRECT, 0, (LPARAM)&rcMonthCal);
808 /* FIXME: button actually is only depressed during dropdown of the */
809 /* calendar control and when the mouse is over the button window */
810 infoPtr->bCalDepressed = TRUE;
812 /* recalculate the position of the monthcal popup */
813 if(infoPtr->dwStyle & DTS_RIGHTALIGN)
814 infoPtr->monthcal_pos.x = infoPtr->calbutton.left -
815 (rcMonthCal.right - rcMonthCal.left);
816 else
817 /* FIXME: this should be after the area reserved for the checkbox */
818 infoPtr->monthcal_pos.x = infoPtr->rcDraw.left;
820 infoPtr->monthcal_pos.y = infoPtr->rcClient.bottom;
821 ClientToScreen (infoPtr->hwndSelf, &(infoPtr->monthcal_pos));
822 SetWindowPos(infoPtr->hMonthCal, 0, infoPtr->monthcal_pos.x,
823 infoPtr->monthcal_pos.y, rcMonthCal.right - rcMonthCal.left,
824 rcMonthCal.bottom - rcMonthCal.top, 0);
826 if(IsWindowVisible(infoPtr->hMonthCal)) {
827 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
828 } else {
829 const SYSTEMTIME *lprgSysTimeArray = &infoPtr->date;
830 TRACE("update calendar %04d/%02d/%02d\n",
831 lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay);
832 SendMessageW(infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
834 if (infoPtr->bDropdownEnabled)
835 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
836 infoPtr->bDropdownEnabled = TRUE;
839 TRACE ("dt:%p mc:%p mc parent:%p, desktop:%p\n",
840 infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hwndNotify, GetDesktopWindow ());
841 DATETIME_SendSimpleNotify (infoPtr, DTN_DROPDOWN);
844 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
846 return 0;
850 static LRESULT
851 DATETIME_LButtonUp (DATETIME_INFO *infoPtr)
853 if(infoPtr->bCalDepressed) {
854 infoPtr->bCalDepressed = FALSE;
855 InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
858 return 0;
862 static LRESULT
863 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
865 if (!hdc) {
866 PAINTSTRUCT ps;
867 hdc = BeginPaint (infoPtr->hwndSelf, &ps);
868 DATETIME_Refresh (infoPtr, hdc);
869 EndPaint (infoPtr->hwndSelf, &ps);
870 } else {
871 DATETIME_Refresh (infoPtr, hdc);
874 /* Not a click on the dropdown box, enabled it */
875 infoPtr->bDropdownEnabled = TRUE;
877 return 0;
881 static LRESULT
882 DATETIME_Button_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
884 if( HIWORD(wParam) == BN_CLICKED) {
885 DWORD state = SendMessageW((HWND)lParam, BM_GETCHECK, 0, 0);
886 infoPtr->dateValid = (state == BST_CHECKED);
887 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
889 return 0;
894 static LRESULT
895 DATETIME_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
897 TRACE("hwndbutton = %p\n", infoPtr->hwndCheckbut);
898 if(infoPtr->hwndCheckbut == (HWND)lParam)
899 return DATETIME_Button_Command(infoPtr, wParam, lParam);
900 return 0;
904 static LRESULT
905 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
907 TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
908 if (bEnable)
909 infoPtr->dwStyle &= ~WS_DISABLED;
910 else
911 infoPtr->dwStyle |= WS_DISABLED;
913 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
915 return 0;
919 static LRESULT
920 DATETIME_EraseBackground (const DATETIME_INFO *infoPtr, HDC hdc)
922 HBRUSH hBrush, hSolidBrush = NULL;
923 RECT rc;
925 if (infoPtr->dwStyle & WS_DISABLED)
926 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
927 else
929 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
930 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
931 if (!hBrush)
932 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
935 GetClientRect (infoPtr->hwndSelf, &rc);
937 FillRect (hdc, &rc, hBrush);
939 if (hSolidBrush)
940 DeleteObject(hSolidBrush);
942 return -1;
946 static LRESULT
947 DATETIME_Notify (DATETIME_INFO *infoPtr, LPNMHDR lpnmh)
949 TRACE ("Got notification %x from %p\n", lpnmh->code, lpnmh->hwndFrom);
950 TRACE ("info: %p %p %p\n", infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hUpdown);
952 if (lpnmh->code == MCN_SELECT) {
953 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
954 infoPtr->dateValid = TRUE;
955 SendMessageW (infoPtr->hMonthCal, MCM_GETCURSEL, 0, (LPARAM)&infoPtr->date);
956 TRACE("got from calendar %04d/%02d/%02d day of week %d\n",
957 infoPtr->date.wYear, infoPtr->date.wMonth, infoPtr->date.wDay, infoPtr->date.wDayOfWeek);
958 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
959 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
960 DATETIME_SendDateTimeChangeNotify (infoPtr);
962 if ((lpnmh->hwndFrom == infoPtr->hUpdown) && (lpnmh->code == UDN_DELTAPOS)) {
963 LPNMUPDOWN lpnmud = (LPNMUPDOWN)lpnmh;
964 TRACE("Delta pos %d\n", lpnmud->iDelta);
965 infoPtr->pendingUpdown = lpnmud->iDelta;
967 return 0;
971 static LRESULT
972 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode)
974 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
975 int wrap = 0;
977 if (!(infoPtr->haveFocus)) return 0;
978 if ((fieldNum==0) && (infoPtr->select)) return 0;
980 if (infoPtr->select & FORMATCALLMASK) {
981 FIXME ("Callbacks not implemented yet\n");
984 switch (vkCode) {
985 case VK_ADD:
986 case VK_UP:
987 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
988 DATETIME_SendDateTimeChangeNotify (infoPtr);
989 break;
990 case VK_SUBTRACT:
991 case VK_DOWN:
992 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
993 DATETIME_SendDateTimeChangeNotify (infoPtr);
994 break;
995 case VK_HOME:
996 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MIN);
997 DATETIME_SendDateTimeChangeNotify (infoPtr);
998 break;
999 case VK_END:
1000 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MAX);
1001 DATETIME_SendDateTimeChangeNotify (infoPtr);
1002 break;
1003 case VK_LEFT:
1004 do {
1005 if (infoPtr->select == 0) {
1006 infoPtr->select = infoPtr->nrFields - 1;
1007 wrap++;
1008 } else {
1009 infoPtr->select--;
1011 } while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
1012 break;
1013 case VK_RIGHT:
1014 do {
1015 infoPtr->select++;
1016 if (infoPtr->select==infoPtr->nrFields) {
1017 infoPtr->select = 0;
1018 wrap++;
1020 } while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
1021 break;
1024 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1026 return 0;
1030 static LRESULT
1031 DATETIME_Char (DATETIME_INFO *infoPtr, WPARAM vkCode)
1033 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1035 if (vkCode >= '0' && vkCode <= '9') {
1036 int num = vkCode-'0';
1037 int newDays;
1039 /* this is a somewhat simplified version of what Windows does */
1040 SYSTEMTIME *date = &infoPtr->date;
1041 switch (infoPtr->fieldspec[fieldNum]) {
1042 case ONEDIGITYEAR:
1043 case TWODIGITYEAR:
1044 date->wYear = date->wYear - (date->wYear%100) +
1045 (date->wYear%10)*10 + num;
1046 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
1047 date->wDay,date->wMonth,date->wYear);
1048 DATETIME_SendDateTimeChangeNotify (infoPtr);
1049 break;
1050 case INVALIDFULLYEAR:
1051 case FULLYEAR:
1052 /* reset current year initialy */
1053 date->wYear = ((date->wYear/1000) ? 0 : 1)*(date->wYear%1000)*10 + num;
1054 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
1055 date->wDay,date->wMonth,date->wYear);
1056 DATETIME_SendDateTimeChangeNotify (infoPtr);
1057 break;
1058 case ONEDIGITMONTH:
1059 case TWODIGITMONTH:
1060 if ((date->wMonth%10) > 1 || num > 2)
1061 date->wMonth = num;
1062 else
1063 date->wMonth = (date->wMonth%10)*10+num;
1064 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
1065 date->wDay,date->wMonth,date->wYear);
1066 DATETIME_SendDateTimeChangeNotify (infoPtr);
1067 break;
1068 case ONEDIGITDAY:
1069 case TWODIGITDAY:
1070 newDays = (date->wDay%10)*10+num;
1071 if (newDays > MONTHCAL_MonthLength(date->wMonth, date->wYear))
1072 date->wDay = num;
1073 else
1074 date->wDay = newDays;
1075 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
1076 date->wDay,date->wMonth,date->wYear);
1077 DATETIME_SendDateTimeChangeNotify (infoPtr);
1078 break;
1079 case ONEDIGIT12HOUR:
1080 case TWODIGIT12HOUR:
1081 if ((date->wHour%10) > 1 || num > 2)
1082 date->wHour = num;
1083 else
1084 date->wHour = (date->wHour%10)*10+num;
1085 DATETIME_SendDateTimeChangeNotify (infoPtr);
1086 break;
1087 case ONEDIGIT24HOUR:
1088 case TWODIGIT24HOUR:
1089 if ((date->wHour%10) > 2)
1090 date->wHour = num;
1091 else if ((date->wHour%10) == 2 && num > 3)
1092 date->wHour = num;
1093 else
1094 date->wHour = (date->wHour%10)*10+num;
1095 DATETIME_SendDateTimeChangeNotify (infoPtr);
1096 break;
1097 case ONEDIGITMINUTE:
1098 case TWODIGITMINUTE:
1099 if ((date->wMinute%10) > 5)
1100 date->wMinute = num;
1101 else
1102 date->wMinute = (date->wMinute%10)*10+num;
1103 DATETIME_SendDateTimeChangeNotify (infoPtr);
1104 break;
1105 case ONEDIGITSECOND:
1106 case TWODIGITSECOND:
1107 if ((date->wSecond%10) > 5)
1108 date->wSecond = num;
1109 else
1110 date->wSecond = (date->wSecond%10)*10+num;
1111 DATETIME_SendDateTimeChangeNotify (infoPtr);
1112 break;
1115 return 0;
1119 static LRESULT
1120 DATETIME_VScroll (DATETIME_INFO *infoPtr, WORD wScroll)
1122 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1124 if ((SHORT)LOWORD(wScroll) != SB_THUMBPOSITION) return 0;
1125 if (!(infoPtr->haveFocus)) return 0;
1126 if ((fieldNum==0) && (infoPtr->select)) return 0;
1128 if (infoPtr->pendingUpdown >= 0) {
1129 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1130 DATETIME_SendDateTimeChangeNotify (infoPtr);
1132 else {
1133 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1134 DATETIME_SendDateTimeChangeNotify (infoPtr);
1137 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1139 return 0;
1143 static LRESULT
1144 DATETIME_KillFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1146 TRACE("lost focus to %p\n", lostFocus);
1148 if (infoPtr->haveFocus) {
1149 DATETIME_SendSimpleNotify (infoPtr, NM_KILLFOCUS);
1150 infoPtr->haveFocus = 0;
1153 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
1155 return 0;
1159 static LRESULT
1160 DATETIME_NCCreate (HWND hwnd, const CREATESTRUCTW *lpcs)
1162 DWORD dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1163 /* force control to have client edge */
1164 dwExStyle |= WS_EX_CLIENTEDGE;
1165 SetWindowLongW(hwnd, GWL_EXSTYLE, dwExStyle);
1167 return DefWindowProcW(hwnd, WM_NCCREATE, 0, (LPARAM)lpcs);
1171 static LRESULT
1172 DATETIME_SetFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1174 TRACE("got focus from %p\n", lostFocus);
1176 /* if monthcal is open and it loses focus, close monthcal */
1177 if (infoPtr->hMonthCal && (lostFocus == infoPtr->hMonthCal) &&
1178 IsWindowVisible(infoPtr->hMonthCal))
1180 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1181 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1182 /* note: this get triggered even if monthcal loses focus to a dropdown
1183 * box click, which occurs without an intermediate WM_PAINT call
1185 infoPtr->bDropdownEnabled = FALSE;
1186 return 0;
1189 if (infoPtr->haveFocus == 0) {
1190 DATETIME_SendSimpleNotify (infoPtr, NM_SETFOCUS);
1191 infoPtr->haveFocus = DTHT_GOTFOCUS;
1194 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1196 return 0;
1200 static BOOL
1201 DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr)
1203 NMDATETIMECHANGE dtdtc;
1205 dtdtc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1206 dtdtc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1207 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1209 dtdtc.dwFlags = (infoPtr->dwStyle & DTS_SHOWNONE) ? GDT_NONE : GDT_VALID;
1211 dtdtc.st = infoPtr->date;
1212 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1213 dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1217 static BOOL
1218 DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code)
1220 NMHDR nmhdr;
1222 TRACE("%x\n", code);
1223 nmhdr.hwndFrom = infoPtr->hwndSelf;
1224 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1225 nmhdr.code = code;
1227 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1228 nmhdr.idFrom, (LPARAM)&nmhdr);
1231 static LRESULT
1232 DATETIME_Size (DATETIME_INFO *infoPtr, INT width, INT height)
1234 /* set size */
1235 infoPtr->rcClient.bottom = height;
1236 infoPtr->rcClient.right = width;
1238 TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1240 infoPtr->rcDraw = infoPtr->rcClient;
1242 if (infoPtr->dwStyle & DTS_UPDOWN) {
1243 SetWindowPos(infoPtr->hUpdown, NULL,
1244 infoPtr->rcClient.right-14, 0,
1245 15, infoPtr->rcClient.bottom - infoPtr->rcClient.top,
1246 SWP_NOACTIVATE | SWP_NOZORDER);
1248 else {
1249 /* set the size of the button that drops the calendar down */
1250 /* FIXME: account for style that allows button on left side */
1251 infoPtr->calbutton.top = infoPtr->rcDraw.top;
1252 infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1253 infoPtr->calbutton.left = infoPtr->rcDraw.right-15;
1254 infoPtr->calbutton.right = infoPtr->rcDraw.right;
1257 /* set enable/disable button size for show none style being enabled */
1258 /* FIXME: these dimensions are completely incorrect */
1259 infoPtr->checkbox.top = infoPtr->rcDraw.top;
1260 infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1261 infoPtr->checkbox.left = infoPtr->rcDraw.left;
1262 infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1264 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1266 return 0;
1270 static LRESULT
1271 DATETIME_StyleChanged(DATETIME_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
1273 static const WCHAR buttonW[] = { 'b', 'u', 't', 't', 'o', 'n', 0 };
1275 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1276 wStyleType, lpss->styleOld, lpss->styleNew);
1278 if (wStyleType != GWL_STYLE) return 0;
1280 infoPtr->dwStyle = lpss->styleNew;
1282 if ( !(lpss->styleOld & DTS_SHOWNONE) && (lpss->styleNew & DTS_SHOWNONE) ) {
1283 infoPtr->hwndCheckbut = CreateWindowExW (0, buttonW, 0, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1284 2, 2, 13, 13, infoPtr->hwndSelf, 0,
1285 (HINSTANCE)GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_HINSTANCE), 0);
1286 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, 1, 0);
1288 if ( (lpss->styleOld & DTS_SHOWNONE) && !(lpss->styleNew & DTS_SHOWNONE) ) {
1289 DestroyWindow(infoPtr->hwndCheckbut);
1290 infoPtr->hwndCheckbut = 0;
1292 if ( !(lpss->styleOld & DTS_UPDOWN) && (lpss->styleNew & DTS_UPDOWN) ) {
1293 infoPtr->hUpdown = CreateUpDownControl (WS_CHILD | WS_BORDER | WS_VISIBLE, 120, 1, 20, 20,
1294 infoPtr->hwndSelf, 1, 0, 0, UD_MAXVAL, UD_MINVAL, 0);
1296 if ( (lpss->styleOld & DTS_UPDOWN) && !(lpss->styleNew & DTS_UPDOWN) ) {
1297 DestroyWindow(infoPtr->hUpdown);
1298 infoPtr->hUpdown = 0;
1301 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1302 return 0;
1306 static LRESULT
1307 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1309 infoPtr->hFont = font;
1310 if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1311 return 0;
1315 static LRESULT
1316 DATETIME_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1318 DATETIME_INFO *infoPtr = Alloc (sizeof(DATETIME_INFO));
1319 STYLESTRUCT ss = { 0, lpcs->style };
1321 if (!infoPtr) return -1;
1323 infoPtr->hwndSelf = hwnd;
1324 infoPtr->dwStyle = lpcs->style;
1326 infoPtr->nrFieldsAllocated = 32;
1327 infoPtr->fieldspec = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1328 infoPtr->fieldRect = Alloc (infoPtr->nrFieldsAllocated * sizeof(RECT));
1329 infoPtr->buflen = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1330 infoPtr->hwndNotify = lpcs->hwndParent;
1331 infoPtr->select = -1; /* initially, nothing is selected */
1332 infoPtr->bDropdownEnabled = TRUE;
1334 DATETIME_StyleChanged(infoPtr, GWL_STYLE, &ss);
1335 DATETIME_SetFormatW (infoPtr, 0);
1337 /* create the monthcal control */
1338 infoPtr->hMonthCal = CreateWindowExW (0, MONTHCAL_CLASSW, 0, WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS,
1339 0, 0, 0, 0, infoPtr->hwndSelf, 0, 0, 0);
1341 /* initialize info structure */
1342 GetLocalTime (&infoPtr->date);
1343 infoPtr->dateValid = TRUE;
1344 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1346 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1348 return 0;
1353 static LRESULT
1354 DATETIME_Destroy (DATETIME_INFO *infoPtr)
1356 if (infoPtr->hwndCheckbut)
1357 DestroyWindow(infoPtr->hwndCheckbut);
1358 if (infoPtr->hUpdown)
1359 DestroyWindow(infoPtr->hUpdown);
1360 if (infoPtr->hMonthCal)
1361 DestroyWindow(infoPtr->hMonthCal);
1362 SetWindowLongPtrW( infoPtr->hwndSelf, 0, 0 ); /* clear infoPtr */
1363 Free (infoPtr);
1364 return 0;
1368 static LRESULT WINAPI
1369 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1371 DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1372 LRESULT ret;
1374 TRACE ("%x, %lx, %lx\n", uMsg, wParam, lParam);
1376 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
1377 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
1379 switch (uMsg) {
1381 case DTM_GETSYSTEMTIME:
1382 return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1384 case DTM_SETSYSTEMTIME:
1385 return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1387 case DTM_GETRANGE:
1388 ret = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1389 return ret ? ret : 1; /* bug emulation */
1391 case DTM_SETRANGE:
1392 return SendMessageW (infoPtr->hMonthCal, MCM_SETRANGE, wParam, lParam);
1394 case DTM_SETFORMATA:
1395 return DATETIME_SetFormatA (infoPtr, (LPCSTR)lParam);
1397 case DTM_SETFORMATW:
1398 return DATETIME_SetFormatW (infoPtr, (LPCWSTR)lParam);
1400 case DTM_GETMONTHCAL:
1401 return (LRESULT)infoPtr->hMonthCal;
1403 case DTM_SETMCCOLOR:
1404 return SendMessageW (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
1406 case DTM_GETMCCOLOR:
1407 return SendMessageW (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
1409 case DTM_SETMCFONT:
1410 return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1412 case DTM_GETMCFONT:
1413 return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1415 case WM_NOTIFY:
1416 return DATETIME_Notify (infoPtr, (LPNMHDR)lParam);
1418 case WM_ENABLE:
1419 return DATETIME_Enable (infoPtr, (BOOL)wParam);
1421 case WM_ERASEBKGND:
1422 return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1424 case WM_GETDLGCODE:
1425 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1427 case WM_PRINTCLIENT:
1428 case WM_PAINT:
1429 return DATETIME_Paint (infoPtr, (HDC)wParam);
1431 case WM_KEYDOWN:
1432 return DATETIME_KeyDown (infoPtr, wParam);
1434 case WM_CHAR:
1435 return DATETIME_Char (infoPtr, wParam);
1437 case WM_KILLFOCUS:
1438 return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1440 case WM_NCCREATE:
1441 return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1443 case WM_SETFOCUS:
1444 return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1446 case WM_SIZE:
1447 return DATETIME_Size (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1449 case WM_LBUTTONDOWN:
1450 return DATETIME_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1452 case WM_LBUTTONUP:
1453 return DATETIME_LButtonUp (infoPtr);
1455 case WM_VSCROLL:
1456 return DATETIME_VScroll (infoPtr, (WORD)wParam);
1458 case WM_CREATE:
1459 return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1461 case WM_DESTROY:
1462 return DATETIME_Destroy (infoPtr);
1464 case WM_COMMAND:
1465 return DATETIME_Command (infoPtr, wParam, lParam);
1467 case WM_STYLECHANGED:
1468 return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1470 case WM_SETFONT:
1471 return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1473 case WM_GETFONT:
1474 return (LRESULT) infoPtr->hFont;
1476 case WM_SETTEXT:
1477 return CB_ERR;
1479 default:
1480 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
1481 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
1482 uMsg, wParam, lParam);
1483 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1488 void
1489 DATETIME_Register (void)
1491 WNDCLASSW wndClass;
1493 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1494 wndClass.style = CS_GLOBALCLASS;
1495 wndClass.lpfnWndProc = DATETIME_WindowProc;
1496 wndClass.cbClsExtra = 0;
1497 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1498 wndClass.hCursor = LoadCursorW (0, (LPCWSTR)IDC_ARROW);
1499 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1500 wndClass.lpszClassName = DATETIMEPICK_CLASSW;
1502 RegisterClassW (&wndClass);
1506 void
1507 DATETIME_Unregister (void)
1509 UnregisterClassW (DATETIMEPICK_CLASSW, NULL);