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