Removed all non-standard common control headers from the include
[wine/hacks.git] / dlls / comctl32 / datetime.c
blobd0f58a2b5d86f95d59aef6f6a58f1a2eba57e106
1 /*
2 * Date and time picker control
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1999, 2000 Alex Priem <alexp@sci.kun.nl>
6 * Copyright 2000 Chris Morgan <cmorgan@wpi.edu>
9 * TODO:
10 * - All messages.
11 * - All notifications.
15 #include <math.h>
16 #include <string.h>
17 #include <stdio.h>
19 #include "winbase.h"
20 #include "wingdi.h"
21 #include "commctrl.h"
22 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(datetime);
26 typedef struct
28 HWND hMonthCal;
29 HWND hUpdown;
30 SYSTEMTIME date;
31 BOOL dateValid;
32 HWND hwndCheckbut;
33 RECT rcClient; /* rect around the edge of the window */
34 RECT rcDraw; /* rect inside of the border */
35 RECT checkbox; /* checkbox allowing the control to be enabled/disabled */
36 RECT calbutton; /* button that toggles the dropdown of the monthcal control */
37 BOOL bCalDepressed; /* TRUE = cal button is depressed */
38 int select;
39 HFONT hFont;
40 int nrFieldsAllocated;
41 int nrFields;
42 int haveFocus;
43 int *fieldspec;
44 RECT *fieldRect;
45 int *buflen;
46 char textbuf[256];
47 POINT monthcal_pos;
48 } DATETIME_INFO, *LPDATETIME_INFO;
50 /* in monthcal.c */
51 extern const int mdays[];
52 extern const char * const monthtxt[];
54 /* this list of defines is closely related to `allowedformatchars' defined
55 * in datetime.c; the high nibble indicates the `base type' of the format
56 * specifier.
57 * Do not change without first reading DATETIME_UseFormat.
61 #define DT_END_FORMAT 0
62 #define ONEDIGITDAY 0x01
63 #define TWODIGITDAY 0x02
64 #define THREECHARDAY 0x03
65 #define FULLDAY 0x04
66 #define ONEDIGIT12HOUR 0x11
67 #define TWODIGIT12HOUR 0x12
68 #define ONEDIGIT24HOUR 0x21
69 #define TWODIGIT24HOUR 0x22
70 #define ONEDIGITMINUTE 0x31
71 #define TWODIGITMINUTE 0x32
72 #define ONEDIGITMONTH 0x41
73 #define TWODIGITMONTH 0x42
74 #define THREECHARMONTH 0x43
75 #define FULLMONTH 0x44
76 #define ONEDIGITSECOND 0x51
77 #define TWODIGITSECOND 0x52
78 #define ONELETTERAMPM 0x61
79 #define TWOLETTERAMPM 0x62
80 #define ONEDIGITYEAR 0x71
81 #define TWODIGITYEAR 0x72
82 #define FULLYEAR 0x73
83 #define FORMATCALLBACK 0x81 /* -> maximum of 0x80 callbacks possible */
84 #define FORMATCALLMASK 0x80
85 #define DT_STRING 0x0100
87 #define DTHT_DATEFIELD 0xff /* for hit-testing */
89 #define DTHT_NONE 0
90 #define DTHT_CHECKBOX 0x200 /* these should end at '00' , to make */
91 #define DTHT_MCPOPUP 0x300 /* & DTHT_DATEFIELD 0 when DATETIME_KeyDown */
92 #define DTHT_GOTFOCUS 0x400 /* tests for date-fields */
94 #define DATETIME_GetInfoPtr(hwnd) ((DATETIME_INFO *)GetWindowLongA (hwnd, 0))
96 static BOOL DATETIME_SendSimpleNotify (HWND hwnd, UINT code);
97 static BOOL DATETIME_SendDateTimeChangeNotify (HWND hwnd);
98 extern void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to);
99 static const char * const days[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
100 "Thursday", "Friday", "Saturday", NULL};
101 static const char *allowedformatchars = {"dhHmMstyX'"};
102 static const int maxrepetition [] = {4,2,2,2,4,2,2,3,-1,-1};
105 static LRESULT
106 DATETIME_GetSystemTime (HWND hwnd, WPARAM wParam, LPARAM lParam )
108 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
109 DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
110 SYSTEMTIME *lprgSysTimeArray=(SYSTEMTIME *) lParam;
112 if (!lParam) return GDT_NONE;
114 if ((dwStyle & DTS_SHOWNONE) &&
115 (SendMessageA (infoPtr->hwndCheckbut, BM_GETCHECK, 0, 0)))
116 return GDT_NONE;
118 MONTHCAL_CopyTime (&infoPtr->date, lprgSysTimeArray);
120 return GDT_VALID;
124 static LRESULT
125 DATETIME_SetSystemTime (HWND hwnd, WPARAM wParam, LPARAM lParam )
127 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
128 SYSTEMTIME *lprgSysTimeArray=(SYSTEMTIME *) lParam;
130 if (!lParam) return 0;
132 if (lParam==GDT_VALID)
133 MONTHCAL_CopyTime (lprgSysTimeArray, &infoPtr->date);
134 if (lParam==GDT_NONE) {
135 infoPtr->dateValid=FALSE;
136 SendMessageA (infoPtr->hwndCheckbut, BM_SETCHECK, 0, 0);
138 return 1;
142 static LRESULT
143 DATETIME_GetMonthCalColor (HWND hwnd, WPARAM wParam)
145 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
147 return SendMessageA (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
151 static LRESULT
152 DATETIME_SetMonthCalColor (HWND hwnd, WPARAM wParam, LPARAM lParam)
154 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
156 return SendMessageA (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
160 /* FIXME: need to get way to force font into monthcal structure */
161 static LRESULT
162 DATETIME_GetMonthCal (HWND hwnd)
164 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
166 return infoPtr->hMonthCal;
171 /* FIXME: need to get way to force font into monthcal structure */
173 static LRESULT
174 DATETIME_GetMonthCalFont (HWND hwnd)
177 return 0;
181 static LRESULT
182 DATETIME_SetMonthCalFont (HWND hwnd, WPARAM wParam, LPARAM lParam)
185 return 0;
190 Split up a formattxt in actions.
191 See ms documentation for the meaning of the letter codes/'specifiers'.
193 Notes:
194 *'dddddd' is handled as 'dddd' plus 'dd'.
195 *unrecognized formats are strings (here given the type DT_STRING;
196 start of the string is encoded in lower bits of DT_STRING.
197 Therefore, 'string' ends finally up as '<show seconds>tring'.
202 static void
203 DATETIME_UseFormat (DATETIME_INFO *infoPtr, const char *formattxt)
205 int i,j,k,len;
206 int *nrFields=& infoPtr->nrFields;
208 TRACE ("%s\n",formattxt);
211 *nrFields=0;
212 infoPtr->fieldspec[*nrFields]=0;
213 len=strlen(allowedformatchars);
214 k=0;
216 for (i=0; i<strlen (formattxt); i++) {
217 TRACE ("\n%d %c:",i, formattxt[i]);
218 for (j=0; j<len; j++) {
219 if (allowedformatchars[j]==formattxt[i]) {
220 TRACE ("%c[%d,%x]",allowedformatchars[j], *nrFields,
221 infoPtr->fieldspec[*nrFields]);
222 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
223 infoPtr->fieldspec[*nrFields]=(j<<4) +1;
224 break;
226 if (infoPtr->fieldspec[*nrFields]>>4!=j) {
227 (*nrFields)++;
228 infoPtr->fieldspec[*nrFields]=(j<<4) +1;
229 break;
231 if ((infoPtr->fieldspec[*nrFields] & 0x0f)==maxrepetition[j]) {
232 (*nrFields)++;
233 infoPtr->fieldspec[*nrFields]=(j<<4) +1;
234 break;
236 infoPtr->fieldspec[*nrFields]++;
237 break;
238 } /* if allowedformatchar */
239 } /* for j */
242 /* char is not a specifier: handle char like a string */
243 if (j==len) {
244 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
245 infoPtr->fieldspec[*nrFields]=DT_STRING+k;
246 infoPtr->buflen[*nrFields]=0;
247 } else
248 if ((infoPtr->fieldspec[*nrFields] & DT_STRING)!=DT_STRING) {
249 (*nrFields)++;
250 infoPtr->fieldspec[*nrFields]=DT_STRING+k;
251 infoPtr->buflen[*nrFields]=0;
253 infoPtr->textbuf[k]=formattxt[i];
254 k++;
255 infoPtr->buflen[*nrFields]++;
256 } /* if j=len */
258 if (*nrFields==infoPtr->nrFieldsAllocated) {
259 FIXME ("out of memory; should reallocate. crash ahead.\n");
262 } /* for i */
264 TRACE("\n");
266 if (infoPtr->fieldspec[*nrFields]!=0) (*nrFields)++;
270 static LRESULT
271 DATETIME_SetFormat (HWND hwnd, WPARAM wParam, LPARAM lParam)
273 DATETIME_INFO *infoPtr= DATETIME_GetInfoPtr (hwnd);
275 if (!lParam) {
276 DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
278 if (dwStyle & DTS_LONGDATEFORMAT)
279 DATETIME_UseFormat (infoPtr, "dddd, MMMM dd, yyy");
280 else if (dwStyle & DTS_TIMEFORMAT)
281 DATETIME_UseFormat (infoPtr, "h:mm:ss tt");
282 else /* DTS_SHORTDATEFORMAT */
283 DATETIME_UseFormat (infoPtr, "M/d/yy");
285 else
286 DATETIME_UseFormat (infoPtr, (char *) lParam);
288 return infoPtr->nrFields;
292 static LRESULT
293 DATETIME_SetFormatW (HWND hwnd, WPARAM wParam, LPARAM lParam)
296 if (lParam) {
297 LPSTR buf;
298 int retval;
299 int len = lstrlenW ((LPWSTR) lParam)+1;
301 buf = (LPSTR) COMCTL32_Alloc (len);
302 lstrcpyWtoA (buf, (LPWSTR) lParam);
303 retval=DATETIME_SetFormat (hwnd, 0, (LPARAM) buf);
304 COMCTL32_Free (buf);
305 return retval;
307 else
308 return DATETIME_SetFormat (hwnd, 0, 0);
313 static void
314 DATETIME_ReturnTxt (DATETIME_INFO *infoPtr, int count, char *result)
316 SYSTEMTIME date = infoPtr->date;
317 int spec;
319 *result=0;
320 TRACE ("%d,%d\n", infoPtr->nrFields, count);
321 if ((count>infoPtr->nrFields) || (count<0)) {
322 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
323 return;
326 if (!infoPtr->fieldspec) return;
328 spec=infoPtr->fieldspec[count];
329 if (spec & DT_STRING) {
330 int txtlen=infoPtr->buflen[count];
332 strncpy (result, infoPtr->textbuf + (spec &~ DT_STRING), txtlen);
333 result[txtlen]=0;
334 TRACE ("arg%d=%x->[%s]\n",count,infoPtr->fieldspec[count],result);
335 return;
339 switch (spec) {
340 case DT_END_FORMAT:
341 *result=0;
342 break;
343 case ONEDIGITDAY:
344 sprintf (result,"%d",date.wDay);
345 break;
346 case TWODIGITDAY:
347 sprintf (result,"%.2d",date.wDay);
348 break;
349 case THREECHARDAY:
350 sprintf (result,"%.3s",days[date.wDayOfWeek]);
351 break;
352 case FULLDAY:
353 strcpy (result,days[date.wDayOfWeek]);
354 break;
355 case ONEDIGIT12HOUR:
356 if (date.wHour>12)
357 sprintf (result,"%d",date.wHour-12);
358 else
359 sprintf (result,"%d",date.wHour);
360 break;
361 case TWODIGIT12HOUR:
362 if (date.wHour>12)
363 sprintf (result,"%.2d",date.wHour-12);
364 else
365 sprintf (result,"%.2d",date.wHour);
366 break;
367 case ONEDIGIT24HOUR:
368 sprintf (result,"%d",date.wHour);
369 break;
370 case TWODIGIT24HOUR:
371 sprintf (result,"%.2d",date.wHour);
372 break;
373 case ONEDIGITSECOND:
374 sprintf (result,"%d",date.wSecond);
375 break;
376 case TWODIGITSECOND:
377 sprintf (result,"%.2d",date.wSecond);
378 break;
379 case ONEDIGITMINUTE:
380 sprintf (result,"%d",date.wMinute);
381 break;
382 case TWODIGITMINUTE:
383 sprintf (result,"%.2d",date.wMinute);
384 break;
385 case ONEDIGITMONTH:
386 sprintf (result,"%d",date.wMonth);
387 break;
388 case TWODIGITMONTH:
389 sprintf (result,"%.2d",date.wMonth);
390 break;
391 case THREECHARMONTH:
392 sprintf (result,"%.3s",monthtxt[date.wMonth-1]);
393 break;
394 case FULLMONTH:
395 strcpy (result,monthtxt[date.wMonth-1]);
396 break;
397 case ONELETTERAMPM:
398 if (date.wHour<12)
399 strcpy (result,"A");
400 else
401 strcpy (result,"P");
402 break;
403 case TWOLETTERAMPM:
404 if (date.wHour<12)
405 strcpy (result,"AM");
406 else
407 strcpy (result,"PM");
408 break;
409 case FORMATCALLBACK:
410 FIXME ("Not implemented\n");
411 strcpy (result,"xxx");
412 break;
413 case ONEDIGITYEAR:
414 sprintf (result,"%d",date.wYear-10* (int) floor(date.wYear/10));
415 break;
416 case TWODIGITYEAR:
417 sprintf (result,"%.2d",date.wYear-100* (int) floor(date.wYear/100));
418 break;
419 case FULLYEAR:
420 sprintf (result,"%d",date.wYear);
421 break;
424 TRACE ("arg%d=%x->[%s]\n",count,infoPtr->fieldspec[count],result);
428 static void
429 DATETIME_IncreaseField (DATETIME_INFO *infoPtr, int number)
431 SYSTEMTIME *date = &infoPtr->date;
432 int spec;
434 TRACE ("%d\n",number);
435 if ((number>infoPtr->nrFields) || (number<0)) return;
437 spec=infoPtr->fieldspec[number];
438 if ((spec & DTHT_DATEFIELD)==0) return;
440 switch (spec) {
441 case ONEDIGITDAY:
442 case TWODIGITDAY:
443 case THREECHARDAY:
444 case FULLDAY:
445 date->wDay++;
446 if (date->wDay>mdays[date->wMonth-1]) date->wDay=1;
447 break;
448 case ONEDIGIT12HOUR:
449 case TWODIGIT12HOUR:
450 case ONEDIGIT24HOUR:
451 case TWODIGIT24HOUR:
452 date->wHour++;
453 if (date->wHour>23) date->wHour=0;
454 break;
455 case ONEDIGITSECOND:
456 case TWODIGITSECOND:
457 date->wSecond++;
458 if (date->wSecond>59) date->wSecond=0;
459 break;
460 case ONEDIGITMINUTE:
461 case TWODIGITMINUTE:
462 date->wMinute++;
463 if (date->wMinute>59) date->wMinute=0;
464 break;
465 case ONEDIGITMONTH:
466 case TWODIGITMONTH:
467 case THREECHARMONTH:
468 case FULLMONTH:
469 date->wMonth++;
470 if (date->wMonth>12) date->wMonth=1;
471 if (date->wDay>mdays[date->wMonth-1])
472 date->wDay=mdays[date->wMonth-1];
473 break;
474 case ONELETTERAMPM:
475 case TWOLETTERAMPM:
476 date->wHour+=12;
477 if (date->wHour>23) date->wHour-=24;
478 break;
479 case FORMATCALLBACK:
480 FIXME ("Not implemented\n");
481 break;
482 case ONEDIGITYEAR:
483 case TWODIGITYEAR:
484 case FULLYEAR:
485 date->wYear++;
486 break;
492 static void
493 DATETIME_DecreaseField (DATETIME_INFO *infoPtr, int number)
495 SYSTEMTIME *date = & infoPtr->date;
496 int spec;
498 TRACE ("%d\n",number);
499 if ((number>infoPtr->nrFields) || (number<0)) return;
501 spec = infoPtr->fieldspec[number];
502 if ((spec & DTHT_DATEFIELD)==0) return;
504 TRACE ("%x\n",spec);
506 switch (spec) {
507 case ONEDIGITDAY:
508 case TWODIGITDAY:
509 case THREECHARDAY:
510 case FULLDAY:
511 date->wDay--;
512 if (date->wDay<1) date->wDay=mdays[date->wMonth-1];
513 break;
514 case ONEDIGIT12HOUR:
515 case TWODIGIT12HOUR:
516 case ONEDIGIT24HOUR:
517 case TWODIGIT24HOUR:
518 if (date->wHour)
519 date->wHour--;
520 else
521 date->wHour=23;
522 break;
523 case ONEDIGITSECOND:
524 case TWODIGITSECOND:
525 if (date->wHour)
526 date->wSecond--;
527 else
528 date->wHour=59;
529 break;
530 case ONEDIGITMINUTE:
531 case TWODIGITMINUTE:
532 if (date->wMinute)
533 date->wMinute--;
534 else
535 date->wMinute=59;
536 break;
537 case ONEDIGITMONTH:
538 case TWODIGITMONTH:
539 case THREECHARMONTH:
540 case FULLMONTH:
541 if (date->wMonth>1)
542 date->wMonth--;
543 else
544 date->wMonth=12;
545 if (date->wDay>mdays[date->wMonth-1])
546 date->wDay=mdays[date->wMonth-1];
547 break;
548 case ONELETTERAMPM:
549 case TWOLETTERAMPM:
550 if (date->wHour<12)
551 date->wHour+=12;
552 else
553 date->wHour-=12;
554 break;
555 case FORMATCALLBACK:
556 FIXME ("Not implemented\n");
557 break;
558 case ONEDIGITYEAR:
559 case TWODIGITYEAR:
560 case FULLYEAR:
561 date->wYear--;
562 break;
568 static void
569 DATETIME_ResetFieldDown (DATETIME_INFO *infoPtr, int number)
571 SYSTEMTIME *date = &infoPtr->date;
572 int spec;
574 TRACE ("%d\n",number);
575 if ((number>infoPtr->nrFields) || (number<0)) return;
577 spec = infoPtr->fieldspec[number];
578 if ((spec & DTHT_DATEFIELD)==0) return;
581 switch (spec) {
582 case ONEDIGITDAY:
583 case TWODIGITDAY:
584 case THREECHARDAY:
585 case FULLDAY:
586 date->wDay = 1;
587 break;
588 case ONEDIGIT12HOUR:
589 case TWODIGIT12HOUR:
590 case ONEDIGIT24HOUR:
591 case TWODIGIT24HOUR:
592 case ONELETTERAMPM:
593 case TWOLETTERAMPM:
594 date->wHour = 0;
595 break;
596 case ONEDIGITSECOND:
597 case TWODIGITSECOND:
598 date->wSecond = 0;
599 break;
600 case ONEDIGITMINUTE:
601 case TWODIGITMINUTE:
602 date->wMinute = 0;
603 break;
604 case ONEDIGITMONTH:
605 case TWODIGITMONTH:
606 case THREECHARMONTH:
607 case FULLMONTH:
608 date->wMonth = 1;
609 case FORMATCALLBACK:
610 FIXME ("Not implemented\n");
611 break;
612 case ONEDIGITYEAR:
613 case TWODIGITYEAR:
614 /* FYI: On 9/14/1752 the calender changed and England and the American */
615 /* colonies changed to the Gregorian calender. This change involved */
616 /* having September 14th following September 2nd. So no date algorithms */
617 /* work before that date. */
618 case FULLYEAR:
619 date->wSecond = 0;
620 date->wMinute = 0;
621 date->wHour = 0;
622 date->wDay = 14; /* overactive ms-programmers..*/
623 date->wMonth = 9;
624 date->wYear = 1752;
625 break;
631 static void
632 DATETIME_ResetFieldUp (DATETIME_INFO *infoPtr, int number)
634 SYSTEMTIME *date = & infoPtr->date;
635 int spec;
637 if ((number>infoPtr->nrFields) || (number<0)) return;
639 spec=infoPtr->fieldspec[number];
640 if ((spec & DTHT_DATEFIELD)==0) return;
642 switch (spec) {
643 case ONEDIGITDAY:
644 case TWODIGITDAY:
645 case THREECHARDAY:
646 case FULLDAY:
647 date->wDay=mdays[date->wMonth-1];
648 break;
649 case ONEDIGIT12HOUR:
650 case TWODIGIT12HOUR:
651 case ONEDIGIT24HOUR:
652 case TWODIGIT24HOUR:
653 case ONELETTERAMPM:
654 case TWOLETTERAMPM:
655 date->wHour=23;
656 break;
657 case ONEDIGITSECOND:
658 case TWODIGITSECOND:
659 date->wSecond=59;
660 break;
661 case ONEDIGITMINUTE:
662 case TWODIGITMINUTE:
663 date->wMinute=59;
664 break;
665 case ONEDIGITMONTH:
666 case TWODIGITMONTH:
667 case THREECHARMONTH:
668 case FULLMONTH:
669 date->wMonth=12;
670 case FORMATCALLBACK:
671 FIXME ("Not implemented\n");
672 break;
673 case ONEDIGITYEAR:
674 case TWODIGITYEAR:
675 case FULLYEAR:
676 date->wYear=9999; /* Y10K problem? naaah. */
677 break;
683 static void DATETIME_Refresh (HWND hwnd, HDC hdc)
686 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
687 int i,prevright;
688 RECT *field;
689 DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
690 RECT *rcDraw = &infoPtr->rcDraw;
691 RECT *rcClient = &infoPtr->rcClient;
692 RECT *calbutton = &infoPtr->calbutton;
693 RECT *checkbox = &infoPtr->checkbox;
694 HBRUSH hbr;
695 SIZE size;
696 COLORREF oldBk, oldTextColor;
698 /* draw control edge */
699 hbr = CreateSolidBrush(RGB(255, 255, 255));
700 FillRect(hdc, rcClient, hbr);
701 DrawEdge(hdc, rcClient, EDGE_SUNKEN, BF_RECT);
702 DeleteObject(hbr);
704 if (infoPtr->dateValid) {
705 char txt[80];
706 HFONT oldFont;
707 oldFont = SelectObject (hdc, infoPtr->hFont);
709 DATETIME_ReturnTxt (infoPtr, 0, txt);
710 GetTextExtentPoint32A (hdc, txt, strlen (txt), &size);
711 rcDraw->bottom = size.cy+2;
713 if (dwStyle & DTS_SHOWNONE) checkbox->right = 18;
715 prevright = checkbox->right;
717 for (i=0; i<infoPtr->nrFields; i++) {
718 DATETIME_ReturnTxt (infoPtr, i, txt);
719 GetTextExtentPoint32A (hdc, txt, strlen (txt), &size);
720 field = & infoPtr->fieldRect[i];
721 field->left = prevright;
722 field->right = prevright+size.cx;
723 field->top = rcDraw->top;
724 field->bottom = rcDraw->bottom;
725 prevright = field->right;
727 if ((infoPtr->haveFocus) && (i==infoPtr->select)) {
728 hbr = CreateSolidBrush (GetSysColor (COLOR_ACTIVECAPTION));
729 FillRect(hdc, field, hbr);
730 oldBk = SetBkColor (hdc, GetSysColor(COLOR_ACTIVECAPTION));
731 oldTextColor = SetTextColor (hdc, GetSysColor(COLOR_WINDOW));
732 DeleteObject (hbr);
733 DrawTextA ( hdc, txt, strlen(txt), field,
734 DT_RIGHT | DT_VCENTER | DT_SINGLELINE );
735 SetBkColor (hdc, oldBk);
736 SetTextColor (hdc, oldTextColor);
738 else
739 DrawTextA ( hdc, txt, strlen(txt), field,
740 DT_RIGHT | DT_VCENTER | DT_SINGLELINE );
743 SelectObject (hdc, oldFont);
746 if (!(dwStyle & DTS_UPDOWN)) {
747 DrawFrameControl(hdc, calbutton, DFC_SCROLL,
748 DFCS_SCROLLDOWN | (infoPtr->bCalDepressed ? DFCS_PUSHED : 0) |
749 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
754 static LRESULT
755 DATETIME_HitTest (HWND hwnd, DATETIME_INFO *infoPtr, POINT pt)
757 int i, retval;
759 TRACE ("%ld, %ld\n",pt.x,pt.y);
761 retval = DTHT_NONE;
762 if (PtInRect (&infoPtr->calbutton, pt))
763 {retval = DTHT_MCPOPUP; TRACE("Hit in calbutton(DTHT_MCPOPUP)\n"); goto done; }
764 if (PtInRect (&infoPtr->checkbox, pt))
765 {retval = DTHT_CHECKBOX; TRACE("Hit in checkbox(DTHT_CHECKBOX)\n"); goto done; }
767 for (i=0; i<infoPtr->nrFields; i++) {
768 if (PtInRect (&infoPtr->fieldRect[i], pt)) {
769 retval = i;
770 TRACE("Hit in date text in field %d\n", i);
771 break;
775 done:
776 return retval;
780 static LRESULT
781 DATETIME_LButtonDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
783 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
784 DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
785 int old, new;
786 POINT pt;
788 TRACE ("\n");
790 old = infoPtr->select;
791 pt.x = (INT)LOWORD(lParam);
792 pt.y = (INT)HIWORD(lParam);
794 new = DATETIME_HitTest (hwnd, infoPtr, pt);
796 /* FIXME: might be conditions where we don't want to update infoPtr->select */
797 infoPtr->select = new;
799 if (infoPtr->select != old) {
800 infoPtr->haveFocus = DTHT_GOTFOCUS;
803 if (infoPtr->select == DTHT_MCPOPUP) {
804 /* FIXME: button actually is only depressed during dropdown of the */
805 /* calender control and when the mouse is over the button window */
806 infoPtr->bCalDepressed = TRUE;
808 /* recalculate the position of the monthcal popup */
809 if(dwStyle & DTS_RIGHTALIGN)
810 infoPtr->monthcal_pos.x = infoPtr->rcClient.right - ((infoPtr->calbutton.right -
811 infoPtr->calbutton.left) + 145);
812 else
813 infoPtr->monthcal_pos.x = 8;
815 infoPtr->monthcal_pos.y = infoPtr->rcClient.bottom;
816 ClientToScreen (hwnd, &(infoPtr->monthcal_pos));
817 SetWindowPos(infoPtr->hMonthCal, 0, infoPtr->monthcal_pos.x,
818 infoPtr->monthcal_pos.y, 145, 150, 0);
820 if(IsWindowVisible(infoPtr->hMonthCal))
821 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
822 else
823 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
825 TRACE ("dt:%x mc:%x mc parent:%x, desktop:%x, mcpp:%x\n",
826 hwnd,infoPtr->hMonthCal,
827 GetParent (infoPtr->hMonthCal),
828 GetDesktopWindow (),
829 GetParent (GetParent (infoPtr->hMonthCal)));
830 DATETIME_SendSimpleNotify (hwnd, DTN_DROPDOWN);
833 InvalidateRect(hwnd, NULL, FALSE);
835 return 0;
839 static LRESULT
840 DATETIME_LButtonUp (HWND hwnd, WPARAM wParam, LPARAM lParam)
842 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
844 TRACE("\n");
846 if(infoPtr->bCalDepressed == TRUE) {
847 infoPtr->bCalDepressed = FALSE;
848 RedrawWindow(hwnd, &(infoPtr->calbutton), 0, RDW_ERASE|RDW_INVALIDATE);
851 return 0;
855 static LRESULT
856 DATETIME_Paint (HWND hwnd, WPARAM wParam)
858 HDC hdc;
859 PAINTSTRUCT ps;
861 hdc = wParam==0 ? BeginPaint (hwnd, &ps) : (HDC)wParam;
862 DATETIME_Refresh (hwnd, hdc);
863 if(!wParam)
864 EndPaint (hwnd, &ps);
865 return 0;
869 static LRESULT
870 DATETIME_ParentNotify (HWND hwnd, WPARAM wParam, LPARAM lParam)
872 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
873 LPNMHDR lpnmh = (LPNMHDR) lParam;
875 TRACE ("%x,%lx\n",wParam, lParam);
876 TRACE ("Got notification %x from %x\n", lpnmh->code, lpnmh->hwndFrom);
877 TRACE ("info: %x %x %x\n",hwnd,infoPtr->hMonthCal,infoPtr->hUpdown);
878 return 0;
882 static LRESULT
883 DATETIME_Notify (HWND hwnd, WPARAM wParam, LPARAM lParam)
886 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
887 LPNMHDR lpnmh = (LPNMHDR) lParam;
889 TRACE ("%x,%lx\n",wParam, lParam);
890 TRACE ("Got notification %x from %x\n", lpnmh->code, lpnmh->hwndFrom);
891 TRACE ("info: %x %x %x\n",hwnd,infoPtr->hMonthCal,infoPtr->hUpdown);
892 return 0;
896 static LRESULT
897 DATETIME_KeyDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
899 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
900 int FieldNum,wrap=0;
902 TRACE("%x %lx %x\n",wParam, lParam, infoPtr->select);
904 FieldNum = infoPtr->select & DTHT_DATEFIELD;
906 if (!(infoPtr->haveFocus)) return 0;
907 if ((FieldNum==0) && (infoPtr->select)) return 0;
909 if (infoPtr->select & FORMATCALLMASK) {
910 FIXME ("Callbacks not implemented yet\n");
913 switch (wParam) {
914 case VK_ADD:
915 case VK_UP:
916 DATETIME_IncreaseField (infoPtr,FieldNum);
917 DATETIME_SendDateTimeChangeNotify (hwnd);
918 break;
919 case VK_SUBTRACT:
920 case VK_DOWN:
921 DATETIME_DecreaseField (infoPtr,FieldNum);
922 DATETIME_SendDateTimeChangeNotify (hwnd);
923 break;
924 case VK_HOME:
925 DATETIME_ResetFieldDown (infoPtr,FieldNum);
926 DATETIME_SendDateTimeChangeNotify (hwnd);
927 break;
928 case VK_END:
929 DATETIME_ResetFieldUp(infoPtr,FieldNum);
930 DATETIME_SendDateTimeChangeNotify (hwnd);
931 break;
932 case VK_LEFT:
933 do {
934 if (infoPtr->select==0) {
935 infoPtr->select = infoPtr->nrFields - 1;
936 wrap++;
937 } else
938 infoPtr->select--;
940 while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
941 break;
942 case VK_RIGHT:
943 do {
944 infoPtr->select++;
945 if (infoPtr->select==infoPtr->nrFields) {
946 infoPtr->select = 0;
947 wrap++;
950 while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
951 break;
954 InvalidateRect(hwnd, NULL, FALSE);
956 return 0;
960 static LRESULT
961 DATETIME_KillFocus (HWND hwnd, WPARAM wParam, LPARAM lParam)
963 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
965 TRACE ("\n");
967 if (infoPtr->haveFocus) {
968 DATETIME_SendSimpleNotify (hwnd, NM_KILLFOCUS);
969 infoPtr->haveFocus = 0;
972 InvalidateRect (hwnd, NULL, TRUE);
974 return 0;
978 static LRESULT
979 DATETIME_SetFocus (HWND hwnd, WPARAM wParam, LPARAM lParam)
981 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
983 TRACE ("\n");
985 if (infoPtr->haveFocus==0) {
986 DATETIME_SendSimpleNotify (hwnd, NM_SETFOCUS);
987 infoPtr->haveFocus = DTHT_GOTFOCUS;
990 InvalidateRect(hwnd, NULL, FALSE);
992 return 0;
996 static BOOL
997 DATETIME_SendDateTimeChangeNotify (HWND hwnd)
1000 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
1001 NMDATETIMECHANGE dtdtc;
1003 TRACE ("\n");
1004 dtdtc.nmhdr.hwndFrom = hwnd;
1005 dtdtc.nmhdr.idFrom = GetWindowLongA( hwnd, GWL_ID);
1006 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1008 if ((GetWindowLongA (hwnd, GWL_STYLE) & DTS_SHOWNONE))
1009 dtdtc.dwFlags = GDT_NONE;
1010 else
1011 dtdtc.dwFlags = GDT_VALID;
1013 MONTHCAL_CopyTime (&infoPtr->date, &dtdtc.st);
1014 return (BOOL) SendMessageA (GetParent (hwnd), WM_NOTIFY,
1015 (WPARAM)dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1019 static BOOL
1020 DATETIME_SendSimpleNotify (HWND hwnd, UINT code)
1022 NMHDR nmhdr;
1024 TRACE("%x\n",code);
1025 nmhdr.hwndFrom = hwnd;
1026 nmhdr.idFrom = GetWindowLongA( hwnd, GWL_ID);
1027 nmhdr.code = code;
1029 return (BOOL) SendMessageA (GetParent (hwnd), WM_NOTIFY,
1030 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
1033 static LRESULT
1034 DATETIME_Size (HWND hwnd, WPARAM wParam, LPARAM lParam)
1036 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr(hwnd);
1037 DWORD dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
1039 /* set size */
1040 infoPtr->rcClient.bottom = HIWORD(lParam);
1041 infoPtr->rcClient.right = LOWORD(lParam);
1043 TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1045 /* use DrawEdge to adjust the size of rcEdge to get rcDraw */
1046 memcpy((&infoPtr->rcDraw), (&infoPtr->rcClient), sizeof(infoPtr->rcDraw));
1048 DrawEdge((HDC)NULL, &(infoPtr->rcDraw), EDGE_SUNKEN, BF_RECT | BF_ADJUST);
1050 /* set the size of the button that drops the calender down */
1051 /* FIXME: account for style that allows button on left side */
1052 infoPtr->calbutton.top = infoPtr->rcDraw.top;
1053 infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1054 infoPtr->calbutton.left = infoPtr->rcDraw.right-15;
1055 infoPtr->calbutton.right = infoPtr->rcDraw.right;
1057 /* set enable/disable button size for show none style being enabled */
1058 /* FIXME: these dimensions are completely incorrect */
1059 infoPtr->checkbox.top = infoPtr->rcDraw.top;
1060 infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1061 infoPtr->checkbox.left = infoPtr->rcDraw.left;
1062 infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1064 /* update the position of the monthcal control */
1065 if(dwStyle & DTS_RIGHTALIGN)
1066 infoPtr->monthcal_pos.x = infoPtr->rcClient.right - ((infoPtr->calbutton.right -
1067 infoPtr->calbutton.left) + 145);
1068 else
1069 infoPtr->monthcal_pos.x = 8;
1071 infoPtr->monthcal_pos.y = infoPtr->rcClient.bottom;
1072 ClientToScreen (hwnd, &(infoPtr->monthcal_pos));
1073 SetWindowPos(infoPtr->hMonthCal, 0, infoPtr->monthcal_pos.x,
1074 infoPtr->monthcal_pos.y,
1075 145, 150, 0);
1077 InvalidateRect(hwnd, NULL, FALSE);
1079 return 0;
1083 static LRESULT
1084 DATETIME_Create (HWND hwnd, WPARAM wParam, LPARAM lParam)
1086 DATETIME_INFO *infoPtr;
1087 DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
1089 /* allocate memory for info structure */
1090 infoPtr = (DATETIME_INFO *)COMCTL32_Alloc (sizeof(DATETIME_INFO));
1091 if (infoPtr == NULL) {
1092 ERR("could not allocate info memory!\n");
1093 return 0;
1096 SetWindowLongA (hwnd, 0, (DWORD)infoPtr);
1098 if (dwStyle & DTS_SHOWNONE) {
1099 infoPtr->hwndCheckbut=CreateWindowExA (0,"button", 0,
1100 WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1101 2,2,13,13,
1102 hwnd,
1103 0, GetWindowLongA (hwnd, GWL_HINSTANCE), 0);
1104 SendMessageA (infoPtr->hwndCheckbut, BM_SETCHECK, 1, 0);
1107 if (dwStyle & DTS_UPDOWN) {
1108 infoPtr->hUpdown=CreateUpDownControl (
1109 WS_CHILD | WS_BORDER | WS_VISIBLE,
1110 120,1,20,20,
1111 hwnd,1,0,0,
1112 UD_MAXVAL, UD_MINVAL, 0);
1115 infoPtr->fieldspec = (int *) COMCTL32_Alloc (32*sizeof(int));
1116 infoPtr->fieldRect = (RECT *) COMCTL32_Alloc (32*sizeof(RECT));
1117 infoPtr->buflen = (int *) COMCTL32_Alloc (32*sizeof(int));
1118 infoPtr->nrFieldsAllocated = 32;
1120 DATETIME_SetFormat (hwnd, 0, 0);
1122 /* create the monthcal control */
1123 infoPtr->hMonthCal = CreateWindowExA (0,"SysMonthCal32", 0,
1124 WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS,
1125 0, 0, 0, 0,
1126 GetParent(hwnd),
1127 0, 0, 0);
1129 /* initialize info structure */
1130 GetSystemTime (&infoPtr->date);
1131 infoPtr->dateValid = TRUE;
1132 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1133 return 0;
1137 static LRESULT
1138 DATETIME_Destroy (HWND hwnd, WPARAM wParam, LPARAM lParam)
1140 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
1142 COMCTL32_Free (infoPtr);
1143 return 0;
1147 static LRESULT WINAPI
1148 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1150 switch (uMsg)
1153 case DTM_GETSYSTEMTIME:
1154 DATETIME_GetSystemTime (hwnd, wParam, lParam);
1156 case DTM_SETSYSTEMTIME:
1157 DATETIME_SetSystemTime (hwnd, wParam, lParam);
1159 case DTM_GETRANGE:
1160 FIXME("Unimplemented msg DTM_GETRANGE\n");
1161 return 0;
1163 case DTM_SETRANGE:
1164 FIXME("Unimplemented msg DTM_SETRANGE\n");
1165 return 1;
1167 case DTM_SETFORMATA:
1168 return DATETIME_SetFormat (hwnd, wParam, lParam);
1170 case DTM_SETFORMATW:
1171 return DATETIME_SetFormatW (hwnd, wParam, lParam);
1173 case DTM_SETMCCOLOR:
1174 return DATETIME_SetMonthCalColor (hwnd, wParam, lParam);
1176 case DTM_GETMCCOLOR:
1177 return DATETIME_GetMonthCalColor (hwnd, wParam);
1179 case DTM_GETMONTHCAL:
1180 return DATETIME_GetMonthCal (hwnd);
1182 case DTM_SETMCFONT:
1183 return DATETIME_SetMonthCalFont (hwnd, wParam, lParam);
1185 case DTM_GETMCFONT:
1186 return DATETIME_GetMonthCalFont (hwnd);
1188 case WM_PARENTNOTIFY:
1189 return DATETIME_ParentNotify (hwnd, wParam, lParam);
1191 case WM_NOTIFY:
1192 return DATETIME_Notify (hwnd, wParam, lParam);
1194 case WM_GETDLGCODE:
1195 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1197 case WM_PAINT:
1198 return DATETIME_Paint (hwnd, wParam);
1200 case WM_KEYDOWN:
1201 return DATETIME_KeyDown (hwnd, wParam, lParam);
1203 case WM_KILLFOCUS:
1204 return DATETIME_KillFocus (hwnd, wParam, lParam);
1206 case WM_SETFOCUS:
1207 return DATETIME_SetFocus (hwnd, wParam, lParam);
1209 case WM_SIZE:
1210 return DATETIME_Size (hwnd, wParam, lParam);
1212 case WM_LBUTTONDOWN:
1213 return DATETIME_LButtonDown (hwnd, wParam, lParam);
1215 case WM_LBUTTONUP:
1216 return DATETIME_LButtonUp (hwnd, wParam, lParam);
1218 case WM_CREATE:
1219 return DATETIME_Create (hwnd, wParam, lParam);
1221 case WM_DESTROY:
1222 return DATETIME_Destroy (hwnd, wParam, lParam);
1224 default:
1225 if (uMsg >= WM_USER)
1226 ERR("unknown msg %04x wp=%08x lp=%08lx\n",
1227 uMsg, wParam, lParam);
1228 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
1230 return 0;
1234 VOID
1235 DATETIME_Register (void)
1237 WNDCLASSA wndClass;
1239 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1240 wndClass.style = CS_GLOBALCLASS;
1241 wndClass.lpfnWndProc = (WNDPROC)DATETIME_WindowProc;
1242 wndClass.cbClsExtra = 0;
1243 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1244 wndClass.hCursor = LoadCursorA (0, IDC_ARROWA);
1245 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1246 wndClass.lpszClassName = DATETIMEPICK_CLASSA;
1248 RegisterClassA (&wndClass);
1252 VOID
1253 DATETIME_Unregister (void)
1255 UnregisterClassA (DATETIMEPICK_CLASSA, (HINSTANCE)NULL);