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>
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.
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.
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
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.
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.
33 * -- DTS_SHORTDATECENTURYFORMAT
55 #include "wine/debug.h"
56 #include "wine/unicode.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(datetime);
70 RECT rcClient; /* rect around the edge of the window */
71 RECT rcDraw; /* rect inside of the border */
72 RECT checkbox; /* checkbox allowing the control to be enabled/disabled */
73 RECT calbutton; /* button that toggles the dropdown of the monthcal control */
74 BOOL bCalDepressed; /* TRUE = cal button is depressed */
77 int nrFieldsAllocated;
86 } DATETIME_INFO, *LPDATETIME_INFO;
89 extern int MONTHCAL_MonthLength(int month, int year);
91 /* this list of defines is closely related to `allowedformatchars' defined
92 * in datetime.c; the high nibble indicates the `base type' of the format
94 * Do not change without first reading DATETIME_UseFormat.
98 #define DT_END_FORMAT 0
99 #define ONEDIGITDAY 0x01
100 #define TWODIGITDAY 0x02
101 #define THREECHARDAY 0x03
103 #define ONEDIGIT12HOUR 0x11
104 #define TWODIGIT12HOUR 0x12
105 #define ONEDIGIT24HOUR 0x21
106 #define TWODIGIT24HOUR 0x22
107 #define ONEDIGITMINUTE 0x31
108 #define TWODIGITMINUTE 0x32
109 #define ONEDIGITMONTH 0x41
110 #define TWODIGITMONTH 0x42
111 #define THREECHARMONTH 0x43
112 #define FULLMONTH 0x44
113 #define ONEDIGITSECOND 0x51
114 #define TWODIGITSECOND 0x52
115 #define ONELETTERAMPM 0x61
116 #define TWOLETTERAMPM 0x62
117 #define ONEDIGITYEAR 0x71
118 #define TWODIGITYEAR 0x72
119 #define INVALIDFULLYEAR 0x73 /* FIXME - yyy is not valid - we'll treat it as yyyy */
120 #define FULLYEAR 0x74
121 #define FORMATCALLBACK 0x81 /* -> maximum of 0x80 callbacks possible */
122 #define FORMATCALLMASK 0x80
123 #define DT_STRING 0x0100
125 #define DTHT_DATEFIELD 0xff /* for hit-testing */
128 #define DTHT_CHECKBOX 0x200 /* these should end at '00' , to make */
129 #define DTHT_MCPOPUP 0x300 /* & DTHT_DATEFIELD 0 when DATETIME_KeyDown */
130 #define DTHT_GOTFOCUS 0x400 /* tests for date-fields */
132 static BOOL DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code);
133 static BOOL DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr);
134 extern void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to);
135 static const WCHAR allowedformatchars[] = {'d', 'h', 'H', 'm', 'M', 's', 't', 'y', 'X', '\'', 0};
136 static const int maxrepetition [] = {4,2,2,2,4,2,2,4,-1,-1};
140 DATETIME_GetSystemTime (const DATETIME_INFO *infoPtr, SYSTEMTIME *lprgSysTimeArray)
142 if (!lprgSysTimeArray) return GDT_NONE;
144 if ((infoPtr->dwStyle & DTS_SHOWNONE) &&
145 (SendMessageW (infoPtr->hwndCheckbut, BM_GETCHECK, 0, 0) == BST_UNCHECKED))
148 MONTHCAL_CopyTime (&infoPtr->date, lprgSysTimeArray);
155 DATETIME_SetSystemTime (DATETIME_INFO *infoPtr, DWORD flag, const SYSTEMTIME *lprgSysTimeArray)
157 if (!lprgSysTimeArray) return 0;
159 TRACE("%04d/%02d/%02d %02d:%02d:%02d\n",
160 lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay,
161 lprgSysTimeArray->wHour, lprgSysTimeArray->wMinute, lprgSysTimeArray->wSecond);
163 if (flag == GDT_VALID) {
164 if (lprgSysTimeArray->wYear < 1601 || lprgSysTimeArray->wYear > 30827 ||
165 lprgSysTimeArray->wMonth < 1 || lprgSysTimeArray->wMonth > 12 ||
166 lprgSysTimeArray->wDayOfWeek > 6 ||
167 lprgSysTimeArray->wDay < 1 || lprgSysTimeArray->wDay > 31 ||
168 lprgSysTimeArray->wHour > 23 ||
169 lprgSysTimeArray->wMinute > 59 ||
170 lprgSysTimeArray->wSecond > 59 ||
171 lprgSysTimeArray->wMilliseconds > 999
175 infoPtr->dateValid = TRUE;
176 MONTHCAL_CopyTime (lprgSysTimeArray, &infoPtr->date);
177 SendMessageW (infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
178 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
179 } else if ((infoPtr->dwStyle & DTS_SHOWNONE) && (flag == GDT_NONE)) {
180 infoPtr->dateValid = FALSE;
181 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_UNCHECKED, 0);
186 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
192 * Split up a formattxt in actions.
193 * See ms documentation for the meaning of the letter codes/'specifiers'.
196 * *'dddddd' is handled as 'dddd' plus 'dd'.
197 * *unrecognized formats are strings (here given the type DT_STRING;
198 * start of the string is encoded in lower bits of DT_STRING.
199 * Therefore, 'string' ends finally up as '<show seconds>tring'.
203 DATETIME_UseFormat (DATETIME_INFO *infoPtr, LPCWSTR formattxt)
207 int *nrFields = &infoPtr->nrFields;
210 infoPtr->fieldspec[*nrFields] = 0;
211 len = strlenW(allowedformatchars);
214 for (i = 0; formattxt[i]; i++) {
215 TRACE ("\n%d %c:", i, formattxt[i]);
216 for (j = 0; j < len; j++) {
217 if (allowedformatchars[j]==formattxt[i]) {
218 TRACE ("%c[%d,%x]", allowedformatchars[j], *nrFields, infoPtr->fieldspec[*nrFields]);
219 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
220 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
223 if (infoPtr->fieldspec[*nrFields] >> 4 != j) {
225 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
228 if ((infoPtr->fieldspec[*nrFields] & 0x0f) == maxrepetition[j]) {
230 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
233 infoPtr->fieldspec[*nrFields]++;
235 } /* if allowedformatchar */
238 /* char is not a specifier: handle char like a string */
240 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
241 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
242 infoPtr->buflen[*nrFields] = 0;
243 } else if ((infoPtr->fieldspec[*nrFields] & DT_STRING) != DT_STRING) {
245 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
246 infoPtr->buflen[*nrFields] = 0;
248 infoPtr->textbuf[k] = formattxt[i];
250 infoPtr->buflen[*nrFields]++;
253 if (*nrFields == infoPtr->nrFieldsAllocated) {
254 FIXME ("out of memory; should reallocate. crash ahead.\n");
260 if (infoPtr->fieldspec[*nrFields] != 0) (*nrFields)++;
265 DATETIME_SetFormatW (DATETIME_INFO *infoPtr, LPCWSTR lpszFormat)
268 WCHAR format_buf[80];
271 if (infoPtr->dwStyle & DTS_LONGDATEFORMAT)
272 format_item = LOCALE_SLONGDATE;
273 else if ((infoPtr->dwStyle & DTS_TIMEFORMAT) == DTS_TIMEFORMAT)
274 format_item = LOCALE_STIMEFORMAT;
275 else /* DTS_SHORTDATEFORMAT */
276 format_item = LOCALE_SSHORTDATE;
277 GetLocaleInfoW( GetSystemDefaultLCID(), format_item, format_buf, sizeof(format_buf)/sizeof(format_buf[0]));
278 lpszFormat = format_buf;
281 DATETIME_UseFormat (infoPtr, lpszFormat);
282 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
289 DATETIME_SetFormatA (DATETIME_INFO *infoPtr, LPCSTR lpszFormat)
293 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, NULL, 0);
294 LPWSTR wstr = Alloc(len * sizeof(WCHAR));
295 if (wstr) MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, wstr, len);
296 retval = DATETIME_SetFormatW (infoPtr, wstr);
301 return DATETIME_SetFormatW (infoPtr, 0);
307 DATETIME_ReturnTxt (const DATETIME_INFO *infoPtr, int count, LPWSTR result, int resultSize)
309 static const WCHAR fmt_dW[] = { '%', 'd', 0 };
310 static const WCHAR fmt__2dW[] = { '%', '.', '2', 'd', 0 };
311 static const WCHAR fmt__3sW[] = { '%', '.', '3', 's', 0 };
312 SYSTEMTIME date = infoPtr->date;
317 TRACE ("%d,%d\n", infoPtr->nrFields, count);
318 if (count>infoPtr->nrFields || count < 0) {
319 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
323 if (!infoPtr->fieldspec) return;
325 spec = infoPtr->fieldspec[count];
326 if (spec & DT_STRING) {
327 int txtlen = infoPtr->buflen[count];
329 if (txtlen > resultSize)
330 txtlen = resultSize - 1;
331 memcpy (result, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
333 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
343 wsprintfW (result, fmt_dW, date.wDay);
346 wsprintfW (result, fmt__2dW, date.wDay);
349 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1+(date.wDayOfWeek+6)%7, result, 4);
350 /*wsprintfW (result,"%.3s",days[date.wDayOfWeek]);*/
353 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME1+(date.wDayOfWeek+6)%7, result, resultSize);
356 if (date.wHour == 0) {
362 wsprintfW (result, fmt_dW, date.wHour - (date.wHour > 12 ? 12 : 0));
365 if (date.wHour == 0) {
371 wsprintfW (result, fmt__2dW, date.wHour - (date.wHour > 12 ? 12 : 0));
374 wsprintfW (result, fmt_dW, date.wHour);
377 wsprintfW (result, fmt__2dW, date.wHour);
380 wsprintfW (result, fmt_dW, date.wSecond);
383 wsprintfW (result, fmt__2dW, date.wSecond);
386 wsprintfW (result, fmt_dW, date.wMinute);
389 wsprintfW (result, fmt__2dW, date.wMinute);
392 wsprintfW (result, fmt_dW, date.wMonth);
395 wsprintfW (result, fmt__2dW, date.wMonth);
398 GetLocaleInfoW(GetSystemDefaultLCID(), LOCALE_SMONTHNAME1+date.wMonth -1,
399 buffer, sizeof(buffer)/sizeof(buffer[0]));
400 wsprintfW (result, fmt__3sW, buffer);
403 GetLocaleInfoW(GetSystemDefaultLCID(),LOCALE_SMONTHNAME1+date.wMonth -1,
407 result[0] = (date.wHour < 12 ? 'A' : 'P');
411 result[0] = (date.wHour < 12 ? 'A' : 'P');
416 FIXME ("Not implemented\n");
421 wsprintfW (result, fmt_dW, date.wYear-10* (int) floor(date.wYear/10));
424 wsprintfW (result, fmt__2dW, date.wYear-100* (int) floor(date.wYear/100));
426 case INVALIDFULLYEAR:
428 wsprintfW (result, fmt_dW, date.wYear);
432 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
435 /* Offsets of days in the week to the weekday of january 1 in a leap year. */
436 static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
438 /* returns the day in the week(0 == sunday, 6 == saturday) */
439 /* day(1 == 1st, 2 == 2nd... etc), year is the year value */
440 static int DATETIME_CalculateDayOfWeek(DWORD day, DWORD month, DWORD year)
444 return((year + year/4 - year/100 + year/400 +
445 DayOfWeekTable[month-1] + day ) % 7);
448 static int wrap(int val, int delta, int minVal, int maxVal)
451 if (delta == INT_MIN || val < minVal) return maxVal;
452 if (delta == INT_MAX || val > maxVal) return minVal;
457 DATETIME_IncreaseField (DATETIME_INFO *infoPtr, int number, int delta)
459 SYSTEMTIME *date = &infoPtr->date;
461 TRACE ("%d\n", number);
462 if ((number > infoPtr->nrFields) || (number < 0)) return;
464 if ((infoPtr->fieldspec[number] & DTHT_DATEFIELD) == 0) return;
466 switch (infoPtr->fieldspec[number]) {
470 date->wYear = wrap(date->wYear, delta, 1752, 9999);
471 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(date->wDay,date->wMonth,date->wYear);
477 date->wMonth = wrap(date->wMonth, delta, 1, 12);
478 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(date->wDay,date->wMonth,date->wYear);
485 date->wDay = wrap(date->wDay, delta, 1, MONTHCAL_MonthLength(date->wMonth, date->wYear));
486 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(date->wDay,date->wMonth,date->wYear);
496 date->wHour = wrap(date->wHour, delta, 0, 23);
500 date->wMinute = wrap(date->wMinute, delta, 0, 59);
504 date->wSecond = wrap(date->wSecond, delta, 0, 59);
507 FIXME ("Not implemented\n");
511 /* FYI: On 1752/9/14 the calendar changed and England and the
512 * American colonies changed to the Gregorian calendar. This change
513 * involved having September 14th follow September 2nd. So no date
514 * algorithm works before that date.
516 if (10000 * date->wYear + 100 * date->wMonth + date->wDay < 17520914) {
528 DATETIME_ReturnFieldWidth (const DATETIME_INFO *infoPtr, HDC hdc, int count, SHORT *fieldWidthPtr)
530 /* fields are a fixed width, determined by the largest possible string */
531 /* presumably, these widths should be language dependent */
532 static const WCHAR fld_d1W[] = { '2', 0 };
533 static const WCHAR fld_d2W[] = { '2', '2', 0 };
534 static const WCHAR fld_d4W[] = { '2', '2', '2', '2', 0 };
535 static const WCHAR fld_am1[] = { 'A', 0 };
536 static const WCHAR fld_am2[] = { 'A', 'M', 0 };
537 static const WCHAR fld_day[] = { 'W', 'e', 'd', 'n', 'e', 's', 'd', 'a', 'y', 0 };
538 static const WCHAR fld_day3[] = { 'W', 'e', 'd', 0 };
539 static const WCHAR fld_mon[] = { 'S', 'e', 'p', 't', 'e', 'm', 'b', 'e', 'r', 0 };
540 static const WCHAR fld_mon3[] = { 'D', 'e', 'c', 0 };
546 TRACE ("%d,%d\n", infoPtr->nrFields, count);
547 if (count>infoPtr->nrFields || count < 0) {
548 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
552 if (!infoPtr->fieldspec) return;
554 spec = infoPtr->fieldspec[count];
555 if (spec & DT_STRING) {
556 int txtlen = infoPtr->buflen[count];
560 memcpy (buffer, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
573 /* these seem to use a two byte field */
583 case INVALIDFULLYEAR:
610 GetTextExtentPoint32W (hdc, bufptr, strlenW(bufptr), &size);
611 *fieldWidthPtr = size.cx;
615 DATETIME_Refresh (DATETIME_INFO *infoPtr, HDC hdc)
619 RECT *rcDraw = &infoPtr->rcDraw;
620 RECT *calbutton = &infoPtr->calbutton;
621 RECT *checkbox = &infoPtr->checkbox;
623 COLORREF oldTextColor;
624 SHORT fieldWidth = 0;
626 /* draw control edge */
629 if (infoPtr->dateValid) {
630 HFONT oldFont = SelectObject (hdc, infoPtr->hFont);
631 INT oldBkMode = SetBkMode (hdc, TRANSPARENT);
634 DATETIME_ReturnTxt (infoPtr, 0, txt, sizeof(txt)/sizeof(txt[0]));
635 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
636 rcDraw->bottom = size.cy + 2;
638 prevright = checkbox->right = ((infoPtr->dwStyle & DTS_SHOWNONE) ? 18 : 2);
640 for (i = 0; i < infoPtr->nrFields; i++) {
641 DATETIME_ReturnTxt (infoPtr, i, txt, sizeof(txt)/sizeof(txt[0]));
642 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
643 DATETIME_ReturnFieldWidth (infoPtr, hdc, i, &fieldWidth);
644 field = &infoPtr->fieldRect[i];
645 field->left = prevright;
646 field->right = prevright + fieldWidth;
647 field->top = rcDraw->top;
648 field->bottom = rcDraw->bottom;
649 prevright = field->right;
651 if (infoPtr->dwStyle & WS_DISABLED)
652 oldTextColor = SetTextColor (hdc, comctl32_color.clrGrayText);
653 else if ((infoPtr->haveFocus) && (i == infoPtr->select)) {
654 /* fill if focussed */
655 HBRUSH hbr = CreateSolidBrush (comctl32_color.clrActiveCaption);
656 FillRect(hdc, field, hbr);
658 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindow);
661 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindowText);
663 /* draw the date text using the colour set above */
664 DrawTextW (hdc, txt, strlenW(txt), field, DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
665 SetTextColor (hdc, oldTextColor);
667 SetBkMode (hdc, oldBkMode);
668 SelectObject (hdc, oldFont);
671 if (!(infoPtr->dwStyle & DTS_UPDOWN)) {
672 DrawFrameControl(hdc, calbutton, DFC_SCROLL,
673 DFCS_SCROLLDOWN | (infoPtr->bCalDepressed ? DFCS_PUSHED : 0) |
674 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
680 DATETIME_HitTest (const DATETIME_INFO *infoPtr, POINT pt)
684 TRACE ("%d, %d\n", pt.x, pt.y);
686 if (PtInRect (&infoPtr->calbutton, pt)) return DTHT_MCPOPUP;
687 if (PtInRect (&infoPtr->checkbox, pt)) return DTHT_CHECKBOX;
689 for (i=0; i < infoPtr->nrFields; i++) {
690 if (PtInRect (&infoPtr->fieldRect[i], pt)) return i;
698 DATETIME_LButtonDown (DATETIME_INFO *infoPtr, WORD wKey, INT x, INT y)
705 old = infoPtr->select;
706 new = DATETIME_HitTest (infoPtr, pt);
708 /* FIXME: might be conditions where we don't want to update infoPtr->select */
709 infoPtr->select = new;
711 SetFocus(infoPtr->hwndSelf);
713 if (infoPtr->select == DTHT_MCPOPUP) {
715 SendMessageW(infoPtr->hMonthCal, MCM_GETMINREQRECT, 0, (LPARAM)&rcMonthCal);
717 /* FIXME: button actually is only depressed during dropdown of the */
718 /* calendar control and when the mouse is over the button window */
719 infoPtr->bCalDepressed = TRUE;
721 /* recalculate the position of the monthcal popup */
722 if(infoPtr->dwStyle & DTS_RIGHTALIGN)
723 infoPtr->monthcal_pos.x = infoPtr->calbutton.left -
724 (rcMonthCal.right - rcMonthCal.left);
726 /* FIXME: this should be after the area reserved for the checkbox */
727 infoPtr->monthcal_pos.x = infoPtr->rcDraw.left;
729 infoPtr->monthcal_pos.y = infoPtr->rcClient.bottom;
730 ClientToScreen (infoPtr->hwndSelf, &(infoPtr->monthcal_pos));
731 SetWindowPos(infoPtr->hMonthCal, 0, infoPtr->monthcal_pos.x,
732 infoPtr->monthcal_pos.y, rcMonthCal.right - rcMonthCal.left,
733 rcMonthCal.bottom - rcMonthCal.top, 0);
735 if(IsWindowVisible(infoPtr->hMonthCal)) {
736 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
738 const SYSTEMTIME *lprgSysTimeArray = &infoPtr->date;
739 TRACE("update calendar %04d/%02d/%02d\n",
740 lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay);
741 SendMessageW(infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
742 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
745 TRACE ("dt:%p mc:%p mc parent:%p, desktop:%p\n",
746 infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hwndNotify, GetDesktopWindow ());
747 DATETIME_SendSimpleNotify (infoPtr, DTN_DROPDOWN);
750 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
757 DATETIME_LButtonUp (DATETIME_INFO *infoPtr, WORD wKey)
759 if(infoPtr->bCalDepressed) {
760 infoPtr->bCalDepressed = FALSE;
761 InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
769 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
773 hdc = BeginPaint (infoPtr->hwndSelf, &ps);
774 DATETIME_Refresh (infoPtr, hdc);
775 EndPaint (infoPtr->hwndSelf, &ps);
777 DATETIME_Refresh (infoPtr, hdc);
784 DATETIME_Button_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
786 if( HIWORD(wParam) == BN_CLICKED) {
787 DWORD state = SendMessageW((HWND)lParam, BM_GETCHECK, 0, 0);
788 infoPtr->dateValid = (state == BST_CHECKED);
789 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
797 DATETIME_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
799 TRACE("hwndbutton = %p\n", infoPtr->hwndCheckbut);
800 if(infoPtr->hwndCheckbut == (HWND)lParam)
801 return DATETIME_Button_Command(infoPtr, wParam, lParam);
807 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
809 TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
811 infoPtr->dwStyle &= ~WS_DISABLED;
813 infoPtr->dwStyle |= WS_DISABLED;
815 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
822 DATETIME_EraseBackground (const DATETIME_INFO *infoPtr, HDC hdc)
824 HBRUSH hBrush, hSolidBrush = NULL;
827 if (infoPtr->dwStyle & WS_DISABLED)
828 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
831 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
832 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
834 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
837 GetClientRect (infoPtr->hwndSelf, &rc);
839 FillRect (hdc, &rc, hBrush);
842 DeleteObject(hSolidBrush);
849 DATETIME_Notify (DATETIME_INFO *infoPtr, int idCtrl, LPNMHDR lpnmh)
851 TRACE ("Got notification %x from %p\n", lpnmh->code, lpnmh->hwndFrom);
852 TRACE ("info: %p %p %p\n", infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hUpdown);
854 if (lpnmh->code == MCN_SELECT) {
855 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
856 infoPtr->dateValid = TRUE;
857 SendMessageW (infoPtr->hMonthCal, MCM_GETCURSEL, 0, (LPARAM)&infoPtr->date);
858 TRACE("got from calendar %04d/%02d/%02d day of week %d\n",
859 infoPtr->date.wYear, infoPtr->date.wMonth, infoPtr->date.wDay, infoPtr->date.wDayOfWeek);
860 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
861 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
862 DATETIME_SendDateTimeChangeNotify (infoPtr);
864 if ((lpnmh->hwndFrom == infoPtr->hUpdown) && (lpnmh->code == UDN_DELTAPOS)) {
865 LPNMUPDOWN lpnmud = (LPNMUPDOWN)lpnmh;
866 TRACE("Delta pos %d\n", lpnmud->iDelta);
867 infoPtr->pendingUpdown = lpnmud->iDelta;
874 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode, LPARAM flags)
876 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
879 if (!(infoPtr->haveFocus)) return 0;
880 if ((fieldNum==0) && (infoPtr->select)) return 0;
882 if (infoPtr->select & FORMATCALLMASK) {
883 FIXME ("Callbacks not implemented yet\n");
886 if (vkCode >= '0' && vkCode <= '9') {
887 /* this is a somewhat simplified version of what Windows does */
888 SYSTEMTIME *date = &infoPtr->date;
889 switch (infoPtr->fieldspec[fieldNum]) {
892 date->wYear = date->wYear - (date->wYear%100) +
893 (date->wYear%10)*10 + (vkCode-'0');
894 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
895 date->wDay,date->wMonth,date->wYear);
896 DATETIME_SendDateTimeChangeNotify (infoPtr);
898 case INVALIDFULLYEAR:
900 date->wYear = (date->wYear%1000)*10 + (vkCode-'0');
901 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
902 date->wDay,date->wMonth,date->wYear);
903 DATETIME_SendDateTimeChangeNotify (infoPtr);
907 if ((date->wMonth%10) > 1 || (vkCode-'0') > 2)
908 date->wMonth = vkCode-'0';
910 date->wMonth = (date->wMonth%10)*10+vkCode-'0';
911 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
912 date->wDay,date->wMonth,date->wYear);
913 DATETIME_SendDateTimeChangeNotify (infoPtr);
917 /* probably better checking here would help */
918 if ((date->wDay%10) >= 3 && (vkCode-'0') > 1)
919 date->wDay = vkCode-'0';
921 date->wDay = (date->wDay%10)*10+vkCode-'0';
922 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
923 date->wDay,date->wMonth,date->wYear);
924 DATETIME_SendDateTimeChangeNotify (infoPtr);
928 if ((date->wHour%10) > 1 || (vkCode-'0') > 2)
929 date->wHour = vkCode-'0';
931 date->wHour = (date->wHour%10)*10+vkCode-'0';
932 DATETIME_SendDateTimeChangeNotify (infoPtr);
936 if ((date->wHour%10) > 2)
937 date->wHour = vkCode-'0';
938 else if ((date->wHour%10) == 2 && (vkCode-'0') > 3)
939 date->wHour = vkCode-'0';
941 date->wHour = (date->wHour%10)*10+vkCode-'0';
942 DATETIME_SendDateTimeChangeNotify (infoPtr);
946 if ((date->wMinute%10) > 5)
947 date->wMinute = vkCode-'0';
949 date->wMinute = (date->wMinute%10)*10+vkCode-'0';
950 DATETIME_SendDateTimeChangeNotify (infoPtr);
954 if ((date->wSecond%10) > 5)
955 date->wSecond = vkCode-'0';
957 date->wSecond = (date->wSecond%10)*10+vkCode-'0';
958 DATETIME_SendDateTimeChangeNotify (infoPtr);
966 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
967 DATETIME_SendDateTimeChangeNotify (infoPtr);
971 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
972 DATETIME_SendDateTimeChangeNotify (infoPtr);
975 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MIN);
976 DATETIME_SendDateTimeChangeNotify (infoPtr);
979 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MAX);
980 DATETIME_SendDateTimeChangeNotify (infoPtr);
984 if (infoPtr->select == 0) {
985 infoPtr->select = infoPtr->nrFields - 1;
990 } while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
995 if (infoPtr->select==infoPtr->nrFields) {
999 } while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
1003 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1010 DATETIME_VScroll (DATETIME_INFO *infoPtr, WORD wScroll)
1012 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1014 if ((SHORT)LOWORD(wScroll) != SB_THUMBPOSITION) return 0;
1015 if (!(infoPtr->haveFocus)) return 0;
1016 if ((fieldNum==0) && (infoPtr->select)) return 0;
1018 if (infoPtr->pendingUpdown >= 0) {
1019 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1020 DATETIME_SendDateTimeChangeNotify (infoPtr);
1023 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1024 DATETIME_SendDateTimeChangeNotify (infoPtr);
1027 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1034 DATETIME_KillFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1036 TRACE("lost focus to %p\n", lostFocus);
1038 if (infoPtr->haveFocus) {
1039 DATETIME_SendSimpleNotify (infoPtr, NM_KILLFOCUS);
1040 infoPtr->haveFocus = 0;
1043 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
1050 DATETIME_NCCreate (HWND hwnd, const CREATESTRUCTW *lpcs)
1052 DWORD dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1053 /* force control to have client edge */
1054 dwExStyle |= WS_EX_CLIENTEDGE;
1055 SetWindowLongW(hwnd, GWL_EXSTYLE, dwExStyle);
1057 return DefWindowProcW(hwnd, WM_NCCREATE, 0, (LPARAM)lpcs);
1062 DATETIME_SetFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1064 TRACE("got focus from %p\n", lostFocus);
1066 if (infoPtr->haveFocus == 0) {
1067 DATETIME_SendSimpleNotify (infoPtr, NM_SETFOCUS);
1068 infoPtr->haveFocus = DTHT_GOTFOCUS;
1071 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1078 DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr)
1080 NMDATETIMECHANGE dtdtc;
1082 dtdtc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1083 dtdtc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1084 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1086 dtdtc.dwFlags = (infoPtr->dwStyle & DTS_SHOWNONE) ? GDT_NONE : GDT_VALID;
1088 MONTHCAL_CopyTime (&infoPtr->date, &dtdtc.st);
1089 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1090 (WPARAM)dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1095 DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code)
1099 TRACE("%x\n", code);
1100 nmhdr.hwndFrom = infoPtr->hwndSelf;
1101 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1104 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1105 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
1109 DATETIME_Size (DATETIME_INFO *infoPtr, WORD flags, INT width, INT height)
1112 infoPtr->rcClient.bottom = height;
1113 infoPtr->rcClient.right = width;
1115 TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1117 infoPtr->rcDraw = infoPtr->rcClient;
1119 if (infoPtr->dwStyle & DTS_UPDOWN) {
1120 SetWindowPos(infoPtr->hUpdown, NULL,
1121 infoPtr->rcClient.right-14, 0,
1122 15, infoPtr->rcClient.bottom - infoPtr->rcClient.top,
1123 SWP_NOACTIVATE | SWP_NOZORDER);
1126 /* set the size of the button that drops the calendar down */
1127 /* FIXME: account for style that allows button on left side */
1128 infoPtr->calbutton.top = infoPtr->rcDraw.top;
1129 infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1130 infoPtr->calbutton.left = infoPtr->rcDraw.right-15;
1131 infoPtr->calbutton.right = infoPtr->rcDraw.right;
1134 /* set enable/disable button size for show none style being enabled */
1135 /* FIXME: these dimensions are completely incorrect */
1136 infoPtr->checkbox.top = infoPtr->rcDraw.top;
1137 infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1138 infoPtr->checkbox.left = infoPtr->rcDraw.left;
1139 infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1141 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1148 DATETIME_StyleChanged(DATETIME_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
1150 static const WCHAR buttonW[] = { 'b', 'u', 't', 't', 'o', 'n', 0 };
1152 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1153 wStyleType, lpss->styleOld, lpss->styleNew);
1155 if (wStyleType != GWL_STYLE) return 0;
1157 infoPtr->dwStyle = lpss->styleNew;
1159 if ( !(lpss->styleOld & DTS_SHOWNONE) && (lpss->styleNew & DTS_SHOWNONE) ) {
1160 infoPtr->hwndCheckbut = CreateWindowExW (0, buttonW, 0, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1161 2, 2, 13, 13, infoPtr->hwndSelf, 0,
1162 (HINSTANCE)GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_HINSTANCE), 0);
1163 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, 1, 0);
1165 if ( (lpss->styleOld & DTS_SHOWNONE) && !(lpss->styleNew & DTS_SHOWNONE) ) {
1166 DestroyWindow(infoPtr->hwndCheckbut);
1167 infoPtr->hwndCheckbut = 0;
1169 if ( !(lpss->styleOld & DTS_UPDOWN) && (lpss->styleNew & DTS_UPDOWN) ) {
1170 infoPtr->hUpdown = CreateUpDownControl (WS_CHILD | WS_BORDER | WS_VISIBLE, 120, 1, 20, 20,
1171 infoPtr->hwndSelf, 1, 0, 0, UD_MAXVAL, UD_MINVAL, 0);
1173 if ( (lpss->styleOld & DTS_UPDOWN) && !(lpss->styleNew & DTS_UPDOWN) ) {
1174 DestroyWindow(infoPtr->hUpdown);
1175 infoPtr->hUpdown = 0;
1178 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1184 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1186 infoPtr->hFont = font;
1187 if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1193 DATETIME_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1195 static const WCHAR SysMonthCal32W[] = { 'S', 'y', 's', 'M', 'o', 'n', 't', 'h', 'C', 'a', 'l', '3', '2', 0 };
1196 DATETIME_INFO *infoPtr = (DATETIME_INFO *)Alloc (sizeof(DATETIME_INFO));
1197 STYLESTRUCT ss = { 0, lpcs->style };
1199 if (!infoPtr) return -1;
1201 infoPtr->hwndSelf = hwnd;
1202 infoPtr->dwStyle = lpcs->style;
1204 infoPtr->nrFieldsAllocated = 32;
1205 infoPtr->fieldspec = (int *) Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1206 infoPtr->fieldRect = (RECT *) Alloc (infoPtr->nrFieldsAllocated * sizeof(RECT));
1207 infoPtr->buflen = (int *) Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1208 infoPtr->hwndNotify = lpcs->hwndParent;
1209 infoPtr->select = -1; /* initially, nothing is selected */
1211 DATETIME_StyleChanged(infoPtr, GWL_STYLE, &ss);
1212 DATETIME_SetFormatW (infoPtr, 0);
1214 /* create the monthcal control */
1215 infoPtr->hMonthCal = CreateWindowExW (0, SysMonthCal32W, 0, WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS,
1216 0, 0, 0, 0, infoPtr->hwndSelf, 0, 0, 0);
1218 /* initialize info structure */
1219 GetLocalTime (&infoPtr->date);
1220 infoPtr->dateValid = TRUE;
1221 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1223 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1231 DATETIME_Destroy (DATETIME_INFO *infoPtr)
1233 if (infoPtr->hwndCheckbut)
1234 DestroyWindow(infoPtr->hwndCheckbut);
1235 if (infoPtr->hUpdown)
1236 DestroyWindow(infoPtr->hUpdown);
1237 if (infoPtr->hMonthCal)
1238 DestroyWindow(infoPtr->hMonthCal);
1239 SetWindowLongPtrW( infoPtr->hwndSelf, 0, 0 ); /* clear infoPtr */
1245 static LRESULT WINAPI
1246 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1248 DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1251 TRACE ("%x, %lx, %lx\n", uMsg, wParam, lParam);
1253 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
1254 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
1258 case DTM_GETSYSTEMTIME:
1259 return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1261 case DTM_SETSYSTEMTIME:
1262 return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1265 ret = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1266 return ret ? ret : 1; /* bug emulation */
1269 return SendMessageW (infoPtr->hMonthCal, MCM_SETRANGE, wParam, lParam);
1271 case DTM_SETFORMATA:
1272 return DATETIME_SetFormatA (infoPtr, (LPCSTR)lParam);
1274 case DTM_SETFORMATW:
1275 return DATETIME_SetFormatW (infoPtr, (LPCWSTR)lParam);
1277 case DTM_GETMONTHCAL:
1278 return (LRESULT)infoPtr->hMonthCal;
1280 case DTM_SETMCCOLOR:
1281 return SendMessageW (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
1283 case DTM_GETMCCOLOR:
1284 return SendMessageW (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
1287 return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1290 return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1293 return DATETIME_Notify (infoPtr, (int)wParam, (LPNMHDR)lParam);
1296 return DATETIME_Enable (infoPtr, (BOOL)wParam);
1299 return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1302 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1304 case WM_PRINTCLIENT:
1306 return DATETIME_Paint (infoPtr, (HDC)wParam);
1309 return DATETIME_KeyDown (infoPtr, wParam, lParam);
1312 return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1315 return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1318 return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1321 return DATETIME_Size (infoPtr, wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1323 case WM_LBUTTONDOWN:
1324 return DATETIME_LButtonDown (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1327 return DATETIME_LButtonUp (infoPtr, (WORD)wParam);
1330 return DATETIME_VScroll (infoPtr, (WORD)wParam);
1333 return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1336 return DATETIME_Destroy (infoPtr);
1339 return DATETIME_Command (infoPtr, wParam, lParam);
1341 case WM_STYLECHANGED:
1342 return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1345 return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1348 return (LRESULT) infoPtr->hFont;
1351 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
1352 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
1353 uMsg, wParam, lParam);
1354 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1360 DATETIME_Register (void)
1364 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1365 wndClass.style = CS_GLOBALCLASS;
1366 wndClass.lpfnWndProc = DATETIME_WindowProc;
1367 wndClass.cbClsExtra = 0;
1368 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1369 wndClass.hCursor = LoadCursorW (0, (LPCWSTR)IDC_ARROW);
1370 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1371 wndClass.lpszClassName = DATETIMEPICK_CLASSW;
1373 RegisterClassW (&wndClass);
1378 DATETIME_Unregister (void)
1380 UnregisterClassW (DATETIMEPICK_CLASSW, NULL);