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