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