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