kernel32/profile: Move GetPrivateProfileSectionA to its own test.
[wine] / dlls / comctl32 / datetime.c
1 /*
2  * Date and time picker control
3  *
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  *
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.
12  *
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.
17  *
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
21  *
22  * NOTE
23  * 
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.
26  * 
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.
30  * 
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
40  */
41
42 #include <math.h>
43 #include <string.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <limits.h>
47
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"
57
58 WINE_DEFAULT_DEBUG_CHANNEL(datetime);
59
60 typedef struct
61 {
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  select;
76     HFONT hFont;
77     int nrFieldsAllocated;
78     int nrFields;
79     int haveFocus;
80     int *fieldspec;
81     RECT *fieldRect;
82     int  *buflen;
83     WCHAR textbuf[256];
84     POINT monthcal_pos;
85     int pendingUpdown;
86 } DATETIME_INFO, *LPDATETIME_INFO;
87
88 /* in monthcal.c */
89 extern int MONTHCAL_MonthLength(int month, int year);
90
91 /* this list of defines is closely related to `allowedformatchars' defined
92  * in datetime.c; the high nibble indicates the `base type' of the format
93  * specifier.
94  * Do not change without first reading DATETIME_UseFormat.
95  *
96  */
97
98 #define DT_END_FORMAT      0
99 #define ONEDIGITDAY     0x01
100 #define TWODIGITDAY     0x02
101 #define THREECHARDAY    0x03
102 #define FULLDAY         0x04
103 #define ONEDIGIT12HOUR  0x11
104 #define TWODIGIT12HOUR  0x12
105 #define ONEDIGIT24HOUR  0x21
106 #define TWODIGIT24HOUR  0x22
107 #define ONEDIGITMINUTE  0x31
108 #define TWODIGITMINUTE  0x32
109 #define ONEDIGITMONTH   0x41
110 #define TWODIGITMONTH   0x42
111 #define THREECHARMONTH  0x43
112 #define FULLMONTH       0x44
113 #define ONEDIGITSECOND  0x51
114 #define TWODIGITSECOND  0x52
115 #define ONELETTERAMPM   0x61
116 #define TWOLETTERAMPM   0x62
117 #define ONEDIGITYEAR    0x71
118 #define TWODIGITYEAR    0x72
119 #define INVALIDFULLYEAR 0x73      /* FIXME - yyy is not valid - we'll treat it as yyyy */
120 #define FULLYEAR        0x74
121 #define FORMATCALLBACK  0x81      /* -> maximum of 0x80 callbacks possible */
122 #define FORMATCALLMASK  0x80
123 #define DT_STRING       0x0100
124
125 #define DTHT_DATEFIELD  0xff      /* for hit-testing */
126
127 #define DTHT_NONE     0
128 #define DTHT_CHECKBOX 0x200     /* these should end at '00' , to make */
129 #define DTHT_MCPOPUP  0x300     /* & DTHT_DATEFIELD 0 when DATETIME_KeyDown */
130 #define DTHT_GOTFOCUS 0x400     /* tests for date-fields */
131
132 static BOOL DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code);
133 static BOOL DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr);
134 extern void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to);
135 static const WCHAR allowedformatchars[] = {'d', 'h', 'H', 'm', 'M', 's', 't', 'y', 'X', '\'', 0};
136 static const int maxrepetition [] = {4,2,2,2,4,2,2,4,-1,-1};
137
138
139 static DWORD
140 DATETIME_GetSystemTime (DATETIME_INFO *infoPtr, SYSTEMTIME *lprgSysTimeArray)
141 {
142     if (!lprgSysTimeArray) return GDT_NONE;
143
144     if ((infoPtr->dwStyle & DTS_SHOWNONE) &&
145         (SendMessageW (infoPtr->hwndCheckbut, BM_GETCHECK, 0, 0) == BST_UNCHECKED))
146         return GDT_NONE;
147
148     MONTHCAL_CopyTime (&infoPtr->date, lprgSysTimeArray);
149
150     return GDT_VALID;
151 }
152
153
154 static BOOL
155 DATETIME_SetSystemTime (DATETIME_INFO *infoPtr, DWORD flag, const SYSTEMTIME *lprgSysTimeArray)
156 {
157     if (!lprgSysTimeArray) return 0;
158
159     TRACE("%04d/%02d/%02d %02d:%02d:%02d\n",
160           lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay,
161           lprgSysTimeArray->wHour, lprgSysTimeArray->wMinute, lprgSysTimeArray->wSecond);
162
163     if (flag == GDT_VALID) {
164       if (lprgSysTimeArray->wYear < 1601 || lprgSysTimeArray->wYear > 30827 ||
165           lprgSysTimeArray->wMonth < 1 || lprgSysTimeArray->wMonth > 12 ||
166           lprgSysTimeArray->wDayOfWeek > 6 ||
167           lprgSysTimeArray->wDay < 1 || lprgSysTimeArray->wDay > 31 ||
168           lprgSysTimeArray->wHour > 23 ||
169           lprgSysTimeArray->wMinute > 59 ||
170           lprgSysTimeArray->wSecond > 59 ||
171           lprgSysTimeArray->wMilliseconds > 999
172           )
173         return 0;
174
175         infoPtr->dateValid = TRUE;
176         MONTHCAL_CopyTime (lprgSysTimeArray, &infoPtr->date);
177         SendMessageW (infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
178         SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
179     } else if ((infoPtr->dwStyle & DTS_SHOWNONE) && (flag == GDT_NONE)) {
180         infoPtr->dateValid = FALSE;
181         SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_UNCHECKED, 0);
182     }
183     else
184         return 0;
185
186     InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
187     return TRUE;
188 }
189
190
191 /***
192  * Split up a formattxt in actions.
193  * See ms documentation for the meaning of the letter codes/'specifiers'.
194  *
195  * Notes:
196  * *'dddddd' is handled as 'dddd' plus 'dd'.
197  * *unrecognized formats are strings (here given the type DT_STRING;
198  * start of the string is encoded in lower bits of DT_STRING.
199  * Therefore, 'string' ends finally up as '<show seconds>tring'.
200  *
201  */
202 static void
203 DATETIME_UseFormat (DATETIME_INFO *infoPtr, LPCWSTR formattxt)
204 {
205     unsigned int i;
206     int j, k, len;
207     int *nrFields = &infoPtr->nrFields;
208
209     *nrFields = 0;
210     infoPtr->fieldspec[*nrFields] = 0;
211     len = strlenW(allowedformatchars);
212     k = 0;
213
214     for (i = 0; formattxt[i]; i++)  {
215         TRACE ("\n%d %c:", i, formattxt[i]);
216         for (j = 0; j < len; j++) {
217             if (allowedformatchars[j]==formattxt[i]) {
218                 TRACE ("%c[%d,%x]", allowedformatchars[j], *nrFields, infoPtr->fieldspec[*nrFields]);
219                 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
220                     infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
221                     break;
222                 }
223                 if (infoPtr->fieldspec[*nrFields] >> 4 != j) {
224                     (*nrFields)++;
225                     infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
226                     break;
227                 }
228                 if ((infoPtr->fieldspec[*nrFields] & 0x0f) == maxrepetition[j]) {
229                     (*nrFields)++;
230                     infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
231                     break;
232                 }
233                 infoPtr->fieldspec[*nrFields]++;
234                 break;
235             }   /* if allowedformatchar */
236         } /* for j */
237
238         /* char is not a specifier: handle char like a string */
239         if (j == len) {
240             if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
241                 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
242                 infoPtr->buflen[*nrFields] = 0;
243             } else if ((infoPtr->fieldspec[*nrFields] & DT_STRING) != DT_STRING)  {
244                 (*nrFields)++;
245                 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
246                 infoPtr->buflen[*nrFields] = 0;
247             }
248             infoPtr->textbuf[k] = formattxt[i];
249             k++;
250             infoPtr->buflen[*nrFields]++;
251         }   /* if j=len */
252
253         if (*nrFields == infoPtr->nrFieldsAllocated) {
254             FIXME ("out of memory; should reallocate. crash ahead.\n");
255         }
256     } /* for i */
257
258     TRACE("\n");
259
260     if (infoPtr->fieldspec[*nrFields] != 0) (*nrFields)++;
261 }
262
263
264 static BOOL
265 DATETIME_SetFormatW (DATETIME_INFO *infoPtr, LPCWSTR lpszFormat)
266 {
267     if (!lpszFormat) {
268         WCHAR format_buf[80];
269         DWORD format_item;
270
271         if (infoPtr->dwStyle & DTS_LONGDATEFORMAT)
272             format_item = LOCALE_SLONGDATE;
273         else if ((infoPtr->dwStyle & DTS_TIMEFORMAT) == DTS_TIMEFORMAT)
274             format_item = LOCALE_STIMEFORMAT;
275         else /* DTS_SHORTDATEFORMAT */
276             format_item = LOCALE_SSHORTDATE;
277         GetLocaleInfoW( GetSystemDefaultLCID(), format_item, format_buf, sizeof(format_buf)/sizeof(format_buf[0]));
278         lpszFormat = format_buf;
279     }
280
281     DATETIME_UseFormat (infoPtr, lpszFormat);
282     InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
283
284     return 1;
285 }
286
287
288 static BOOL
289 DATETIME_SetFormatA (DATETIME_INFO *infoPtr, LPCSTR lpszFormat)
290 {
291     if (lpszFormat) {
292         BOOL retval;
293         INT len = MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, NULL, 0);
294         LPWSTR wstr = Alloc(len * sizeof(WCHAR));
295         if (wstr) MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, wstr, len);
296         retval = DATETIME_SetFormatW (infoPtr, wstr);
297         Free (wstr);
298         return retval;
299     }
300     else
301         return DATETIME_SetFormatW (infoPtr, 0);
302
303 }
304
305
306 static void
307 DATETIME_ReturnTxt (const DATETIME_INFO *infoPtr, int count, LPWSTR result, int resultSize)
308 {
309     static const WCHAR fmt_dW[] = { '%', 'd', 0 };
310     static const WCHAR fmt__2dW[] = { '%', '.', '2', 'd', 0 };
311     static const WCHAR fmt__3sW[] = { '%', '.', '3', 's', 0 };
312     SYSTEMTIME date = infoPtr->date;
313     int spec;
314     WCHAR buffer[80];
315
316     *result=0;
317     TRACE ("%d,%d\n", infoPtr->nrFields, count);
318     if (count>infoPtr->nrFields || count < 0) {
319         WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
320         return;
321     }
322
323     if (!infoPtr->fieldspec) return;
324
325     spec = infoPtr->fieldspec[count];
326     if (spec & DT_STRING) {
327         int txtlen = infoPtr->buflen[count];
328
329         if (txtlen > resultSize)
330             txtlen = resultSize - 1;
331         memcpy (result, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
332         result[txtlen] = 0;
333         TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
334         return;
335     }
336
337
338     switch (spec) {
339         case DT_END_FORMAT:
340             *result = 0;
341             break;
342         case ONEDIGITDAY:
343             wsprintfW (result, fmt_dW, date.wDay);
344             break;
345         case TWODIGITDAY:
346             wsprintfW (result, fmt__2dW, date.wDay);
347             break;
348         case THREECHARDAY:
349             GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1+(date.wDayOfWeek+6)%7, result, 4);
350             /*wsprintfW (result,"%.3s",days[date.wDayOfWeek]);*/
351             break;
352         case FULLDAY:
353             GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME1+(date.wDayOfWeek+6)%7, result, resultSize);
354             break;
355         case ONEDIGIT12HOUR:
356             if (date.wHour == 0) {
357                 result[0] = '1';
358                 result[1] = '2';
359                 result[2] = 0;
360             }
361             else
362                 wsprintfW (result, fmt_dW, date.wHour - (date.wHour > 12 ? 12 : 0));
363             break;
364         case TWODIGIT12HOUR:
365             if (date.wHour == 0) {
366                 result[0] = '1';
367                 result[1] = '2';
368                 result[2] = 0;
369             }
370             else
371                 wsprintfW (result, fmt__2dW, date.wHour - (date.wHour > 12 ? 12 : 0));
372             break;
373         case ONEDIGIT24HOUR:
374             wsprintfW (result, fmt_dW, date.wHour);
375             break;
376         case TWODIGIT24HOUR:
377             wsprintfW (result, fmt__2dW, date.wHour);
378             break;
379         case ONEDIGITSECOND:
380             wsprintfW (result, fmt_dW, date.wSecond);
381             break;
382         case TWODIGITSECOND:
383             wsprintfW (result, fmt__2dW, date.wSecond);
384             break;
385         case ONEDIGITMINUTE:
386             wsprintfW (result, fmt_dW, date.wMinute);
387             break;
388         case TWODIGITMINUTE:
389             wsprintfW (result, fmt__2dW, date.wMinute);
390             break;
391         case ONEDIGITMONTH:
392             wsprintfW (result, fmt_dW, date.wMonth);
393             break;
394         case TWODIGITMONTH:
395             wsprintfW (result, fmt__2dW, date.wMonth);
396             break;
397         case THREECHARMONTH:
398             GetLocaleInfoW(GetSystemDefaultLCID(), LOCALE_SMONTHNAME1+date.wMonth -1, 
399                            buffer, sizeof(buffer)/sizeof(buffer[0]));
400             wsprintfW (result, fmt__3sW, buffer);
401             break;
402         case FULLMONTH:
403             GetLocaleInfoW(GetSystemDefaultLCID(),LOCALE_SMONTHNAME1+date.wMonth -1,
404                            result, resultSize);
405             break;
406         case ONELETTERAMPM:
407             result[0] = (date.wHour < 12 ? 'A' : 'P');
408             result[1] = 0;
409             break;
410         case TWOLETTERAMPM:
411             result[0] = (date.wHour < 12 ? 'A' : 'P');
412             result[1] = 'M';
413             result[2] = 0;
414             break;
415         case FORMATCALLBACK:
416             FIXME ("Not implemented\n");
417             result[0] = 'x';
418             result[1] = 0;
419             break;
420         case ONEDIGITYEAR:
421             wsprintfW (result, fmt_dW, date.wYear-10* (int) floor(date.wYear/10));
422             break;
423         case TWODIGITYEAR:
424             wsprintfW (result, fmt__2dW, date.wYear-100* (int) floor(date.wYear/100));
425             break;
426         case INVALIDFULLYEAR:
427         case FULLYEAR:
428             wsprintfW (result, fmt_dW, date.wYear);
429             break;
430     }
431
432     TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
433 }
434
435 /* Offsets of days in the week to the weekday of january 1 in a leap year. */
436 static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
437
438 /* returns the day in the week(0 == sunday, 6 == saturday) */
439 /* day(1 == 1st, 2 == 2nd... etc), year is the  year value */
440 static int DATETIME_CalculateDayOfWeek(DWORD day, DWORD month, DWORD year)
441 {
442     year-=(month < 3);
443     
444     return((year + year/4 - year/100 + year/400 +
445          DayOfWeekTable[month-1] + day ) % 7);
446 }
447
448 static int wrap(int val, int delta, int minVal, int maxVal)
449 {
450     val += delta;
451     if (delta == INT_MIN || val < minVal) return maxVal;
452     if (delta == INT_MAX || val > maxVal) return minVal;
453     return val;
454 }
455
456 static void
457 DATETIME_IncreaseField (DATETIME_INFO *infoPtr, int number, int delta)
458 {
459     SYSTEMTIME *date = &infoPtr->date;
460
461     TRACE ("%d\n", number);
462     if ((number > infoPtr->nrFields) || (number < 0)) return;
463
464     if ((infoPtr->fieldspec[number] & DTHT_DATEFIELD) == 0) return;
465
466     switch (infoPtr->fieldspec[number]) {
467         case ONEDIGITYEAR:
468         case TWODIGITYEAR:
469         case FULLYEAR:
470             date->wYear = wrap(date->wYear, delta, 1752, 9999);
471             date->wDayOfWeek = DATETIME_CalculateDayOfWeek(date->wDay,date->wMonth,date->wYear);
472             break;
473         case ONEDIGITMONTH:
474         case TWODIGITMONTH:
475         case THREECHARMONTH:
476         case FULLMONTH:
477             date->wMonth = wrap(date->wMonth, delta, 1, 12);
478             date->wDayOfWeek = DATETIME_CalculateDayOfWeek(date->wDay,date->wMonth,date->wYear);
479             delta = 0;
480             /* fall through */
481         case ONEDIGITDAY:
482         case TWODIGITDAY:
483         case THREECHARDAY:
484         case FULLDAY:
485             date->wDay = wrap(date->wDay, delta, 1, MONTHCAL_MonthLength(date->wMonth, date->wYear));
486             date->wDayOfWeek = DATETIME_CalculateDayOfWeek(date->wDay,date->wMonth,date->wYear);
487             break;
488         case ONELETTERAMPM:
489         case TWOLETTERAMPM:
490             delta *= 12;
491             /* fall through */
492         case ONEDIGIT12HOUR:
493         case TWODIGIT12HOUR:
494         case ONEDIGIT24HOUR:
495         case TWODIGIT24HOUR:
496             date->wHour = wrap(date->wHour, delta, 0, 23);
497             break;
498         case ONEDIGITMINUTE:
499         case TWODIGITMINUTE:
500             date->wMinute = wrap(date->wMinute, delta, 0, 59);
501             break;
502         case ONEDIGITSECOND:
503         case TWODIGITSECOND:
504             date->wSecond = wrap(date->wSecond, delta, 0, 59);
505             break;
506         case FORMATCALLBACK:
507             FIXME ("Not implemented\n");
508             break;
509     }
510
511     /* FYI: On 1752/9/14 the calendar changed and England and the
512      * American colonies changed to the Gregorian calendar. This change
513      * involved having September 14th follow September 2nd. So no date
514      * algorithm works before that date.
515      */
516     if (10000 * date->wYear + 100 * date->wMonth + date->wDay < 17520914) {
517         date->wYear = 1752;
518         date->wMonth = 9;
519         date->wDay = 14;
520         date->wSecond = 0;
521         date->wMinute = 0;
522         date->wHour = 0;
523     }
524 }
525
526
527 static void
528 DATETIME_ReturnFieldWidth (const DATETIME_INFO *infoPtr, HDC hdc, int count, SHORT *fieldWidthPtr)
529 {
530     /* fields are a fixed width, determined by the largest possible string */
531     /* presumably, these widths should be language dependent */
532     static const WCHAR fld_d1W[] = { '2', 0 };
533     static const WCHAR fld_d2W[] = { '2', '2', 0 };
534     static const WCHAR fld_d4W[] = { '2', '2', '2', '2', 0 };
535     static const WCHAR fld_am1[] = { 'A', 0 };
536     static const WCHAR fld_am2[] = { 'A', 'M', 0 };
537     static const WCHAR fld_day[] = { 'W', 'e', 'd', 'n', 'e', 's', 'd', 'a', 'y', 0 };
538     static const WCHAR fld_day3[] = { 'W', 'e', 'd', 0 };
539     static const WCHAR fld_mon[] = { 'S', 'e', 'p', 't', 'e', 'm', 'b', 'e', 'r', 0 };
540     static const WCHAR fld_mon3[] = { 'D', 'e', 'c', 0 };
541     int spec;
542     WCHAR buffer[80];
543     LPCWSTR bufptr;
544     SIZE size;
545
546     TRACE ("%d,%d\n", infoPtr->nrFields, count);
547     if (count>infoPtr->nrFields || count < 0) {
548         WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
549         return;
550     }
551
552     if (!infoPtr->fieldspec) return;
553
554     spec = infoPtr->fieldspec[count];
555     if (spec & DT_STRING) {
556         int txtlen = infoPtr->buflen[count];
557
558         if (txtlen > 79)
559             txtlen = 79;
560         memcpy (buffer, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
561         buffer[txtlen] = 0;
562         bufptr = buffer;
563     }
564     else {
565         switch (spec) {
566             case ONEDIGITDAY:
567             case ONEDIGIT12HOUR:
568             case ONEDIGIT24HOUR:
569             case ONEDIGITSECOND:
570             case ONEDIGITMINUTE:
571             case ONEDIGITMONTH:
572             case ONEDIGITYEAR:
573                 /* these seem to use a two byte field */
574             case TWODIGITDAY:
575             case TWODIGIT12HOUR:
576             case TWODIGIT24HOUR:
577             case TWODIGITSECOND:
578             case TWODIGITMINUTE:
579             case TWODIGITMONTH:
580             case TWODIGITYEAR:
581                 bufptr = fld_d2W;
582                 break;
583             case INVALIDFULLYEAR:
584             case FULLYEAR:
585                 bufptr = fld_d4W;
586                 break;
587             case THREECHARDAY:
588                 bufptr = fld_day3;
589                 break;
590             case FULLDAY:
591                 bufptr = fld_day;
592                 break;
593             case THREECHARMONTH:
594                 bufptr = fld_mon3;
595                 break;
596             case FULLMONTH:
597                 bufptr = fld_mon;
598                 break;
599             case ONELETTERAMPM:
600                 bufptr = fld_am1;
601                 break;
602             case TWOLETTERAMPM:
603                 bufptr = fld_am2;
604                 break;
605             default:
606                 bufptr = fld_d1W;
607                 break;
608         }
609     }
610     GetTextExtentPoint32W (hdc, bufptr, strlenW(bufptr), &size);
611     *fieldWidthPtr = size.cx;
612 }
613
614 static void 
615 DATETIME_Refresh (DATETIME_INFO *infoPtr, HDC hdc)
616 {
617     int i,prevright;
618     RECT *field;
619     RECT *rcDraw = &infoPtr->rcDraw;
620     RECT *calbutton = &infoPtr->calbutton;
621     RECT *checkbox = &infoPtr->checkbox;
622     SIZE size;
623     COLORREF oldTextColor;
624     SHORT fieldWidth = 0;
625
626     /* draw control edge */
627     TRACE("\n");
628
629     if (infoPtr->dateValid) {
630         HFONT oldFont = SelectObject (hdc, infoPtr->hFont);
631         INT oldBkMode = SetBkMode (hdc, TRANSPARENT);
632         WCHAR txt[80];
633
634         DATETIME_ReturnTxt (infoPtr, 0, txt, sizeof(txt)/sizeof(txt[0]));
635         GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
636         rcDraw->bottom = size.cy + 2;
637
638         prevright = checkbox->right = ((infoPtr->dwStyle & DTS_SHOWNONE) ? 18 : 2);
639
640         for (i = 0; i < infoPtr->nrFields; i++) {
641             DATETIME_ReturnTxt (infoPtr, i, txt, sizeof(txt)/sizeof(txt[0]));
642             GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
643             DATETIME_ReturnFieldWidth (infoPtr, hdc, i, &fieldWidth);
644             field = &infoPtr->fieldRect[i];
645             field->left  = prevright;
646             field->right = prevright + fieldWidth;
647             field->top   = rcDraw->top;
648             field->bottom = rcDraw->bottom;
649             prevright = field->right;
650
651             if (infoPtr->dwStyle & WS_DISABLED)
652                 oldTextColor = SetTextColor (hdc, comctl32_color.clrGrayText);
653             else if ((infoPtr->haveFocus) && (i == infoPtr->select)) {
654                 /* fill if focussed */
655                 HBRUSH hbr = CreateSolidBrush (comctl32_color.clrActiveCaption);
656                 FillRect(hdc, field, hbr);
657                 DeleteObject (hbr);
658                 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindow);
659             }
660             else
661                 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindowText);
662
663             /* draw the date text using the colour set above */
664             DrawTextW (hdc, txt, strlenW(txt), field, DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
665             SetTextColor (hdc, oldTextColor);
666         }
667         SetBkMode (hdc, oldBkMode);
668         SelectObject (hdc, oldFont);
669     }
670
671     if (!(infoPtr->dwStyle & DTS_UPDOWN)) {
672         DrawFrameControl(hdc, calbutton, DFC_SCROLL,
673                          DFCS_SCROLLDOWN | (infoPtr->bCalDepressed ? DFCS_PUSHED : 0) |
674                          (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
675     }
676 }
677
678
679 static INT
680 DATETIME_HitTest (const DATETIME_INFO *infoPtr, POINT pt)
681 {
682     int i;
683
684     TRACE ("%d, %d\n", pt.x, pt.y);
685
686     if (PtInRect (&infoPtr->calbutton, pt)) return DTHT_MCPOPUP;
687     if (PtInRect (&infoPtr->checkbox, pt)) return DTHT_CHECKBOX;
688
689     for (i=0; i < infoPtr->nrFields; i++) {
690         if (PtInRect (&infoPtr->fieldRect[i], pt)) return i;
691     }
692
693     return DTHT_NONE;
694 }
695
696
697 static LRESULT
698 DATETIME_LButtonDown (DATETIME_INFO *infoPtr, WORD wKey, INT x, INT y)
699 {
700     POINT pt;
701     int old, new;
702
703     pt.x = x;
704     pt.y = y;
705     old = infoPtr->select;
706     new = DATETIME_HitTest (infoPtr, pt);
707
708     /* FIXME: might be conditions where we don't want to update infoPtr->select */
709     infoPtr->select = new;
710
711     SetFocus(infoPtr->hwndSelf);
712
713     if (infoPtr->select == DTHT_MCPOPUP) {
714         RECT rcMonthCal;
715         SendMessageW(infoPtr->hMonthCal, MCM_GETMINREQRECT, 0, (LPARAM)&rcMonthCal);
716
717         /* FIXME: button actually is only depressed during dropdown of the */
718         /* calendar control and when the mouse is over the button window */
719         infoPtr->bCalDepressed = TRUE;
720
721         /* recalculate the position of the monthcal popup */
722         if(infoPtr->dwStyle & DTS_RIGHTALIGN)
723             infoPtr->monthcal_pos.x = infoPtr->calbutton.left - 
724                 (rcMonthCal.right - rcMonthCal.left);
725         else
726             /* FIXME: this should be after the area reserved for the checkbox */
727             infoPtr->monthcal_pos.x = infoPtr->rcDraw.left;
728
729         infoPtr->monthcal_pos.y = infoPtr->rcClient.bottom;
730         ClientToScreen (infoPtr->hwndSelf, &(infoPtr->monthcal_pos));
731         SetWindowPos(infoPtr->hMonthCal, 0, infoPtr->monthcal_pos.x,
732             infoPtr->monthcal_pos.y, rcMonthCal.right - rcMonthCal.left,
733             rcMonthCal.bottom - rcMonthCal.top, 0);
734
735         if(IsWindowVisible(infoPtr->hMonthCal)) {
736             ShowWindow(infoPtr->hMonthCal, SW_HIDE);
737         } else {
738             const SYSTEMTIME *lprgSysTimeArray = &infoPtr->date;
739             TRACE("update calendar %04d/%02d/%02d\n", 
740             lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay);
741             SendMessageW(infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
742             ShowWindow(infoPtr->hMonthCal, SW_SHOW);
743         }
744
745         TRACE ("dt:%p mc:%p mc parent:%p, desktop:%p\n",
746                infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hwndNotify, GetDesktopWindow ());
747         DATETIME_SendSimpleNotify (infoPtr, DTN_DROPDOWN);
748     }
749
750     InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
751
752     return 0;
753 }
754
755
756 static LRESULT
757 DATETIME_LButtonUp (DATETIME_INFO *infoPtr, WORD wKey)
758 {
759     if(infoPtr->bCalDepressed) {
760         infoPtr->bCalDepressed = FALSE;
761         InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
762     }
763
764     return 0;
765 }
766
767
768 static LRESULT
769 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
770 {
771     if (!hdc) {
772         PAINTSTRUCT ps;
773         hdc = BeginPaint (infoPtr->hwndSelf, &ps);
774         DATETIME_Refresh (infoPtr, hdc);
775         EndPaint (infoPtr->hwndSelf, &ps);
776     } else {
777         DATETIME_Refresh (infoPtr, hdc);
778     }
779     return 0;
780 }
781
782
783 static LRESULT
784 DATETIME_Button_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
785 {
786     if( HIWORD(wParam) == BN_CLICKED) {
787         DWORD state = SendMessageW((HWND)lParam, BM_GETCHECK, 0, 0);
788         infoPtr->dateValid = (state == BST_CHECKED);
789         InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
790     }
791     return 0;
792 }
793           
794         
795         
796 static LRESULT
797 DATETIME_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
798 {
799     TRACE("hwndbutton = %p\n", infoPtr->hwndCheckbut);
800     if(infoPtr->hwndCheckbut == (HWND)lParam)
801         return DATETIME_Button_Command(infoPtr, wParam, lParam);
802     return 0;
803 }
804
805
806 static LRESULT
807 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
808 {
809     TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
810     if (bEnable)
811         infoPtr->dwStyle &= ~WS_DISABLED;
812     else
813         infoPtr->dwStyle |= WS_DISABLED;
814     return 0;
815 }
816
817
818 static LRESULT
819 DATETIME_EraseBackground (const DATETIME_INFO *infoPtr, HDC hdc)
820 {
821     HBRUSH hBrush, hSolidBrush = NULL;
822     RECT   rc;
823
824     if (infoPtr->dwStyle & WS_DISABLED)
825         hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
826     else
827     {
828         hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
829                                       (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
830         if (!hBrush)
831             hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
832     }
833
834     GetClientRect (infoPtr->hwndSelf, &rc);
835
836     FillRect (hdc, &rc, hBrush);
837
838     if (hSolidBrush)
839         DeleteObject(hSolidBrush);
840
841     return -1;
842 }
843
844
845 static LRESULT
846 DATETIME_Notify (DATETIME_INFO *infoPtr, int idCtrl, LPNMHDR lpnmh)
847 {
848     TRACE ("Got notification %x from %p\n", lpnmh->code, lpnmh->hwndFrom);
849     TRACE ("info: %p %p %p\n", infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hUpdown);
850
851     if (lpnmh->code == MCN_SELECT) {
852         ShowWindow(infoPtr->hMonthCal, SW_HIDE);
853         infoPtr->dateValid = TRUE;
854         SendMessageW (infoPtr->hMonthCal, MCM_GETCURSEL, 0, (LPARAM)&infoPtr->date);
855         TRACE("got from calendar %04d/%02d/%02d day of week %d\n", 
856         infoPtr->date.wYear, infoPtr->date.wMonth, infoPtr->date.wDay, infoPtr->date.wDayOfWeek);
857         SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
858         InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
859         DATETIME_SendDateTimeChangeNotify (infoPtr);
860     }
861     if ((lpnmh->hwndFrom == infoPtr->hUpdown) && (lpnmh->code == UDN_DELTAPOS)) {
862         LPNMUPDOWN lpnmud = (LPNMUPDOWN)lpnmh;
863         TRACE("Delta pos %d\n", lpnmud->iDelta);
864         infoPtr->pendingUpdown = lpnmud->iDelta;
865     }
866     return 0;
867 }
868
869
870 static LRESULT
871 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode, LPARAM flags)
872 {
873     int fieldNum = infoPtr->select & DTHT_DATEFIELD;
874     int wrap = 0;
875
876     if (!(infoPtr->haveFocus)) return 0;
877     if ((fieldNum==0) && (infoPtr->select)) return 0;
878
879     if (infoPtr->select & FORMATCALLMASK) {
880         FIXME ("Callbacks not implemented yet\n");
881     }
882
883     if (vkCode >= '0' && vkCode <= '9') {
884         /* this is a somewhat simplified version of what Windows does */
885         SYSTEMTIME *date = &infoPtr->date;
886         switch (infoPtr->fieldspec[fieldNum]) {
887             case ONEDIGITYEAR:
888             case TWODIGITYEAR:
889                 date->wYear = date->wYear - (date->wYear%100) +
890                         (date->wYear%10)*10 + (vkCode-'0');
891                 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
892                         date->wDay,date->wMonth,date->wYear);
893                 DATETIME_SendDateTimeChangeNotify (infoPtr);
894                 break;
895             case INVALIDFULLYEAR:
896             case FULLYEAR:
897                 date->wYear = (date->wYear%1000)*10 + (vkCode-'0');
898                 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
899                         date->wDay,date->wMonth,date->wYear);
900                 DATETIME_SendDateTimeChangeNotify (infoPtr);
901                 break;
902             case ONEDIGITMONTH:
903             case TWODIGITMONTH:
904                 if ((date->wMonth%10) > 1 || (vkCode-'0') > 2)
905                     date->wMonth = vkCode-'0';
906                 else
907                     date->wMonth = (date->wMonth%10)*10+vkCode-'0';
908                 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
909                         date->wDay,date->wMonth,date->wYear);
910                 DATETIME_SendDateTimeChangeNotify (infoPtr);
911                 break;
912             case ONEDIGITDAY:
913             case TWODIGITDAY:
914                 /* probably better checking here would help */
915                 if ((date->wDay%10) >= 3 && (vkCode-'0') > 1)
916                     date->wDay = vkCode-'0';
917                 else
918                     date->wDay = (date->wDay%10)*10+vkCode-'0';
919                 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
920                         date->wDay,date->wMonth,date->wYear);
921                 DATETIME_SendDateTimeChangeNotify (infoPtr);
922                 break;
923             case ONEDIGIT12HOUR:
924             case TWODIGIT12HOUR:
925                 if ((date->wHour%10) > 1 || (vkCode-'0') > 2)
926                     date->wHour = vkCode-'0';
927                 else
928                     date->wHour = (date->wHour%10)*10+vkCode-'0';
929                 DATETIME_SendDateTimeChangeNotify (infoPtr);
930                 break;
931             case ONEDIGIT24HOUR:
932             case TWODIGIT24HOUR:
933                 if ((date->wHour%10) > 2)
934                     date->wHour = vkCode-'0';
935                 else if ((date->wHour%10) == 2 && (vkCode-'0') > 3)
936                     date->wHour = vkCode-'0';
937                 else
938                     date->wHour = (date->wHour%10)*10+vkCode-'0';
939                 DATETIME_SendDateTimeChangeNotify (infoPtr);
940                 break;
941             case ONEDIGITMINUTE:
942             case TWODIGITMINUTE:
943                 if ((date->wMinute%10) > 5)
944                     date->wMinute = vkCode-'0';
945                 else
946                     date->wMinute = (date->wMinute%10)*10+vkCode-'0';
947                 DATETIME_SendDateTimeChangeNotify (infoPtr);
948                 break;
949             case ONEDIGITSECOND:
950             case TWODIGITSECOND:
951                 if ((date->wSecond%10) > 5)
952                     date->wSecond = vkCode-'0';
953                 else
954                     date->wSecond = (date->wSecond%10)*10+vkCode-'0';
955                 DATETIME_SendDateTimeChangeNotify (infoPtr);
956                 break;
957         }
958     }
959     
960     switch (vkCode) {
961         case VK_ADD:
962         case VK_UP:
963             DATETIME_IncreaseField (infoPtr, fieldNum, 1);
964             DATETIME_SendDateTimeChangeNotify (infoPtr);
965             break;
966         case VK_SUBTRACT:
967         case VK_DOWN:
968             DATETIME_IncreaseField (infoPtr, fieldNum, -1);
969             DATETIME_SendDateTimeChangeNotify (infoPtr);
970             break;
971         case VK_HOME:
972             DATETIME_IncreaseField (infoPtr, fieldNum, INT_MIN);
973             DATETIME_SendDateTimeChangeNotify (infoPtr);
974             break;
975         case VK_END:
976             DATETIME_IncreaseField (infoPtr, fieldNum, INT_MAX);
977             DATETIME_SendDateTimeChangeNotify (infoPtr);
978             break;
979         case VK_LEFT:
980             do {
981                 if (infoPtr->select == 0) {
982                     infoPtr->select = infoPtr->nrFields - 1;
983                     wrap++;
984                 } else {
985                     infoPtr->select--;
986                 }
987             } while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
988             break;
989         case VK_RIGHT:
990             do {
991                 infoPtr->select++;
992                 if (infoPtr->select==infoPtr->nrFields) {
993                     infoPtr->select = 0;
994                     wrap++;
995                 }
996             } while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
997             break;
998     }
999
1000     InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1001
1002     return 0;
1003 }
1004
1005
1006 static LRESULT
1007 DATETIME_VScroll (DATETIME_INFO *infoPtr, WORD wScroll)
1008 {
1009     int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1010
1011     if ((SHORT)LOWORD(wScroll) != SB_THUMBPOSITION) return 0;
1012     if (!(infoPtr->haveFocus)) return 0;
1013     if ((fieldNum==0) && (infoPtr->select)) return 0;
1014
1015     if (infoPtr->pendingUpdown >= 0) {
1016         DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1017         DATETIME_SendDateTimeChangeNotify (infoPtr);
1018     }
1019     else {
1020         DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1021         DATETIME_SendDateTimeChangeNotify (infoPtr);
1022     }
1023
1024     InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1025
1026     return 0;
1027 }
1028
1029
1030 static LRESULT
1031 DATETIME_KillFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1032 {
1033     TRACE("lost focus to %p\n", lostFocus);
1034
1035     if (infoPtr->haveFocus) {
1036         DATETIME_SendSimpleNotify (infoPtr, NM_KILLFOCUS);
1037         infoPtr->haveFocus = 0;
1038     }
1039
1040     InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
1041
1042     return 0;
1043 }
1044
1045
1046 static LRESULT
1047 DATETIME_NCCreate (HWND hwnd, const CREATESTRUCTW *lpcs)
1048 {
1049     DWORD dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1050     /* force control to have client edge */
1051     dwExStyle |= WS_EX_CLIENTEDGE;
1052     SetWindowLongW(hwnd, GWL_EXSTYLE, dwExStyle);
1053
1054     return DefWindowProcW(hwnd, WM_NCCREATE, 0, (LPARAM)lpcs);
1055 }
1056
1057
1058 static LRESULT
1059 DATETIME_SetFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1060 {
1061     TRACE("got focus from %p\n", lostFocus);
1062
1063     if (infoPtr->haveFocus == 0) {
1064         DATETIME_SendSimpleNotify (infoPtr, NM_SETFOCUS);
1065         infoPtr->haveFocus = DTHT_GOTFOCUS;
1066     }
1067
1068     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1069
1070     return 0;
1071 }
1072
1073
1074 static BOOL
1075 DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr)
1076 {
1077     NMDATETIMECHANGE dtdtc;
1078
1079     dtdtc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1080     dtdtc.nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1081     dtdtc.nmhdr.code     = DTN_DATETIMECHANGE;
1082
1083     dtdtc.dwFlags = (infoPtr->dwStyle & DTS_SHOWNONE) ? GDT_NONE : GDT_VALID;
1084
1085     MONTHCAL_CopyTime (&infoPtr->date, &dtdtc.st);
1086     return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1087                                 (WPARAM)dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1088 }
1089
1090
1091 static BOOL
1092 DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code)
1093 {
1094     NMHDR nmhdr;
1095
1096     TRACE("%x\n", code);
1097     nmhdr.hwndFrom = infoPtr->hwndSelf;
1098     nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1099     nmhdr.code     = code;
1100
1101     return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1102                                 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
1103 }
1104
1105 static LRESULT
1106 DATETIME_Size (DATETIME_INFO *infoPtr, WORD flags, INT width, INT height)
1107 {
1108     /* set size */
1109     infoPtr->rcClient.bottom = height;
1110     infoPtr->rcClient.right = width;
1111
1112     TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1113
1114     infoPtr->rcDraw = infoPtr->rcClient;
1115     
1116     if (infoPtr->dwStyle & DTS_UPDOWN) {
1117         SetWindowPos(infoPtr->hUpdown, NULL,
1118             infoPtr->rcClient.right-14, 0,
1119             15, infoPtr->rcClient.bottom - infoPtr->rcClient.top,
1120             SWP_NOACTIVATE | SWP_NOZORDER);
1121     }
1122     else {
1123         /* set the size of the button that drops the calendar down */
1124         /* FIXME: account for style that allows button on left side */
1125         infoPtr->calbutton.top   = infoPtr->rcDraw.top;
1126         infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1127         infoPtr->calbutton.left  = infoPtr->rcDraw.right-15;
1128         infoPtr->calbutton.right = infoPtr->rcDraw.right;
1129     }
1130
1131     /* set enable/disable button size for show none style being enabled */
1132     /* FIXME: these dimensions are completely incorrect */
1133     infoPtr->checkbox.top = infoPtr->rcDraw.top;
1134     infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1135     infoPtr->checkbox.left = infoPtr->rcDraw.left;
1136     infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1137
1138     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1139
1140     return 0;
1141 }
1142
1143
1144 static LRESULT 
1145 DATETIME_StyleChanged(DATETIME_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
1146 {
1147     static const WCHAR buttonW[] = { 'b', 'u', 't', 't', 'o', 'n', 0 };
1148
1149     TRACE("(styletype=%x, styleOld=0x%08x, styleNew=0x%08x)\n",
1150           wStyleType, lpss->styleOld, lpss->styleNew);
1151
1152     if (wStyleType != GWL_STYLE) return 0;
1153   
1154     infoPtr->dwStyle = lpss->styleNew;
1155
1156     if ( !(lpss->styleOld & DTS_SHOWNONE) && (lpss->styleNew & DTS_SHOWNONE) ) {
1157         infoPtr->hwndCheckbut = CreateWindowExW (0, buttonW, 0, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1158                                                  2, 2, 13, 13, infoPtr->hwndSelf, 0, 
1159                                                 (HINSTANCE)GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_HINSTANCE), 0);
1160         SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, 1, 0);
1161     }
1162     if ( (lpss->styleOld & DTS_SHOWNONE) && !(lpss->styleNew & DTS_SHOWNONE) ) {
1163         DestroyWindow(infoPtr->hwndCheckbut);
1164         infoPtr->hwndCheckbut = 0;
1165     }
1166     if ( !(lpss->styleOld & DTS_UPDOWN) && (lpss->styleNew & DTS_UPDOWN) ) {
1167         infoPtr->hUpdown = CreateUpDownControl (WS_CHILD | WS_BORDER | WS_VISIBLE, 120, 1, 20, 20, 
1168                                                 infoPtr->hwndSelf, 1, 0, 0, UD_MAXVAL, UD_MINVAL, 0);
1169     }
1170     if ( (lpss->styleOld & DTS_UPDOWN) && !(lpss->styleNew & DTS_UPDOWN) ) {
1171         DestroyWindow(infoPtr->hUpdown);
1172         infoPtr->hUpdown = 0;
1173     }
1174
1175     InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1176     return 0;
1177 }
1178
1179
1180 static LRESULT
1181 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1182 {
1183     infoPtr->hFont = font;
1184     if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1185     return 0;
1186 }
1187
1188
1189 static LRESULT
1190 DATETIME_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1191 {
1192     static const WCHAR SysMonthCal32W[] = { 'S', 'y', 's', 'M', 'o', 'n', 't', 'h', 'C', 'a', 'l', '3', '2', 0 };
1193     DATETIME_INFO *infoPtr = (DATETIME_INFO *)Alloc (sizeof(DATETIME_INFO));
1194     STYLESTRUCT ss = { 0, lpcs->style };
1195
1196     if (!infoPtr) return -1;
1197
1198     infoPtr->hwndSelf = hwnd;
1199     infoPtr->dwStyle = lpcs->style;
1200
1201     infoPtr->nrFieldsAllocated = 32;
1202     infoPtr->fieldspec = (int *) Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1203     infoPtr->fieldRect = (RECT *) Alloc (infoPtr->nrFieldsAllocated * sizeof(RECT));
1204     infoPtr->buflen = (int *) Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1205     infoPtr->hwndNotify = lpcs->hwndParent;
1206     infoPtr->select = -1; /* initially, nothing is selected */
1207
1208     DATETIME_StyleChanged(infoPtr, GWL_STYLE, &ss);
1209     DATETIME_SetFormatW (infoPtr, 0);
1210
1211     /* create the monthcal control */
1212     infoPtr->hMonthCal = CreateWindowExW (0, SysMonthCal32W, 0, WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS, 
1213                                           0, 0, 0, 0, infoPtr->hwndSelf, 0, 0, 0);
1214
1215     /* initialize info structure */
1216     GetLocalTime (&infoPtr->date);
1217     infoPtr->dateValid = TRUE;
1218     infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1219
1220     SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1221
1222     return 0;
1223 }
1224
1225
1226
1227 static LRESULT
1228 DATETIME_Destroy (DATETIME_INFO *infoPtr)
1229 {
1230     if (infoPtr->hwndCheckbut)
1231         DestroyWindow(infoPtr->hwndCheckbut);
1232     if (infoPtr->hUpdown)
1233         DestroyWindow(infoPtr->hUpdown);
1234     if (infoPtr->hMonthCal) 
1235         DestroyWindow(infoPtr->hMonthCal);
1236     SetWindowLongPtrW( infoPtr->hwndSelf, 0, 0 ); /* clear infoPtr */
1237     Free (infoPtr);
1238     return 0;
1239 }
1240
1241
1242 static LRESULT WINAPI
1243 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1244 {
1245     DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1246     LRESULT ret;
1247
1248     TRACE ("%x, %x, %lx\n", uMsg, wParam, lParam);
1249
1250     if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
1251         return DefWindowProcW( hwnd, uMsg, wParam, lParam );
1252
1253     switch (uMsg) {
1254
1255     case DTM_GETSYSTEMTIME:
1256         return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1257
1258     case DTM_SETSYSTEMTIME:
1259         return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1260
1261     case DTM_GETRANGE:
1262         ret = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1263         return ret ? ret : 1; /* bug emulation */
1264
1265     case DTM_SETRANGE:
1266         return SendMessageW (infoPtr->hMonthCal, MCM_SETRANGE, wParam, lParam);
1267
1268     case DTM_SETFORMATA:
1269         return DATETIME_SetFormatA (infoPtr, (LPCSTR)lParam);
1270
1271     case DTM_SETFORMATW:
1272         return DATETIME_SetFormatW (infoPtr, (LPCWSTR)lParam);
1273
1274     case DTM_GETMONTHCAL:
1275         return (LRESULT)infoPtr->hMonthCal;
1276
1277     case DTM_SETMCCOLOR:
1278         return SendMessageW (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
1279
1280     case DTM_GETMCCOLOR:
1281         return SendMessageW (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
1282
1283     case DTM_SETMCFONT:
1284         return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1285
1286     case DTM_GETMCFONT:
1287         return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1288
1289     case WM_NOTIFY:
1290         return DATETIME_Notify (infoPtr, (int)wParam, (LPNMHDR)lParam);
1291
1292     case WM_ENABLE:
1293         return DATETIME_Enable (infoPtr, (BOOL)wParam);
1294
1295     case WM_ERASEBKGND:
1296         return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1297
1298     case WM_GETDLGCODE:
1299         return DLGC_WANTARROWS | DLGC_WANTCHARS;
1300
1301     case WM_PRINTCLIENT:
1302     case WM_PAINT:
1303         return DATETIME_Paint (infoPtr, (HDC)wParam);
1304
1305     case WM_KEYDOWN:
1306         return DATETIME_KeyDown (infoPtr, wParam, lParam);
1307
1308     case WM_KILLFOCUS:
1309         return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1310
1311     case WM_NCCREATE:
1312         return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1313
1314     case WM_SETFOCUS:
1315         return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1316
1317     case WM_SIZE:
1318         return DATETIME_Size (infoPtr, wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1319
1320     case WM_LBUTTONDOWN:
1321         return DATETIME_LButtonDown (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1322
1323     case WM_LBUTTONUP:
1324         return DATETIME_LButtonUp (infoPtr, (WORD)wParam);
1325
1326     case WM_VSCROLL:
1327         return DATETIME_VScroll (infoPtr, (WORD)wParam);
1328
1329     case WM_CREATE:
1330         return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1331
1332     case WM_DESTROY:
1333         return DATETIME_Destroy (infoPtr);
1334
1335     case WM_COMMAND:
1336         return DATETIME_Command (infoPtr, wParam, lParam);
1337
1338     case WM_STYLECHANGED:
1339         return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1340
1341     case WM_SETFONT:
1342         return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1343
1344     case WM_GETFONT:
1345         return (LRESULT) infoPtr->hFont;
1346
1347     default:
1348         if ((uMsg >= WM_USER) && (uMsg < WM_APP))
1349                 ERR("unknown msg %04x wp=%08x lp=%08lx\n",
1350                      uMsg, wParam, lParam);
1351         return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1352     }
1353 }
1354
1355
1356 void
1357 DATETIME_Register (void)
1358 {
1359     WNDCLASSW wndClass;
1360
1361     ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1362     wndClass.style         = CS_GLOBALCLASS;
1363     wndClass.lpfnWndProc   = DATETIME_WindowProc;
1364     wndClass.cbClsExtra    = 0;
1365     wndClass.cbWndExtra    = sizeof(DATETIME_INFO *);
1366     wndClass.hCursor       = LoadCursorW (0, (LPCWSTR)IDC_ARROW);
1367     wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1368     wndClass.lpszClassName = DATETIMEPICK_CLASSW;
1369
1370     RegisterClassW (&wndClass);
1371 }
1372
1373
1374 void
1375 DATETIME_Unregister (void)
1376 {
1377     UnregisterClassW (DATETIMEPICK_CLASSW, NULL);
1378 }