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 (DATETIME_INFO *infoPtr, UINT code);
133 static BOOL DATETIME_SendDateTimeChangeNotify (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 (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, 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 (lprgSysTimeArray->wYear < 1601 || lprgSysTimeArray->wYear > 30827 ||
164 lprgSysTimeArray->wMonth < 1 || lprgSysTimeArray->wMonth > 12 ||
165 lprgSysTimeArray->wDayOfWeek > 6 ||
166 lprgSysTimeArray->wDay < 1 || lprgSysTimeArray->wDay > 31 ||
167 lprgSysTimeArray->wHour > 23 ||
168 lprgSysTimeArray->wMinute > 59 ||
169 lprgSysTimeArray->wSecond > 59 ||
170 lprgSysTimeArray->wMilliseconds > 999
174 if (flag == GDT_VALID) {
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 (flag == GDT_NONE) {
180 infoPtr->dateValid = FALSE;
181 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_UNCHECKED, 0);
184 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
190 * Split up a formattxt in actions.
191 * See ms documentation for the meaning of the letter codes/'specifiers'.
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'.
201 DATETIME_UseFormat (DATETIME_INFO *infoPtr, LPCWSTR formattxt)
205 int *nrFields = &infoPtr->nrFields;
208 infoPtr->fieldspec[*nrFields] = 0;
209 len = strlenW(allowedformatchars);
212 for (i = 0; formattxt[i]; i++) {
213 TRACE ("\n%d %c:", i, formattxt[i]);
214 for (j = 0; j < len; j++) {
215 if (allowedformatchars[j]==formattxt[i]) {
216 TRACE ("%c[%d,%x]", allowedformatchars[j], *nrFields, infoPtr->fieldspec[*nrFields]);
217 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
218 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
221 if (infoPtr->fieldspec[*nrFields] >> 4 != j) {
223 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
226 if ((infoPtr->fieldspec[*nrFields] & 0x0f) == maxrepetition[j]) {
228 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
231 infoPtr->fieldspec[*nrFields]++;
233 } /* if allowedformatchar */
236 /* char is not a specifier: handle char like a string */
238 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
239 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
240 infoPtr->buflen[*nrFields] = 0;
241 } else if ((infoPtr->fieldspec[*nrFields] & DT_STRING) != DT_STRING) {
243 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
244 infoPtr->buflen[*nrFields] = 0;
246 infoPtr->textbuf[k] = formattxt[i];
248 infoPtr->buflen[*nrFields]++;
251 if (*nrFields == infoPtr->nrFieldsAllocated) {
252 FIXME ("out of memory; should reallocate. crash ahead.\n");
258 if (infoPtr->fieldspec[*nrFields] != 0) (*nrFields)++;
263 DATETIME_SetFormatW (DATETIME_INFO *infoPtr, LPCWSTR lpszFormat)
266 WCHAR format_buf[80];
269 if (infoPtr->dwStyle & DTS_LONGDATEFORMAT)
270 format_item = LOCALE_SLONGDATE;
271 else if ((infoPtr->dwStyle & DTS_TIMEFORMAT) == DTS_TIMEFORMAT)
272 format_item = LOCALE_STIMEFORMAT;
273 else /* DTS_SHORTDATEFORMAT */
274 format_item = LOCALE_SSHORTDATE;
275 GetLocaleInfoW( GetSystemDefaultLCID(), format_item, format_buf, sizeof(format_buf)/sizeof(format_buf[0]));
276 lpszFormat = format_buf;
279 DATETIME_UseFormat (infoPtr, lpszFormat);
280 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
287 DATETIME_SetFormatA (DATETIME_INFO *infoPtr, LPCSTR lpszFormat)
291 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, NULL, 0);
292 LPWSTR wstr = Alloc(len * sizeof(WCHAR));
293 if (wstr) MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, wstr, len);
294 retval = DATETIME_SetFormatW (infoPtr, wstr);
299 return DATETIME_SetFormatW (infoPtr, 0);
305 DATETIME_ReturnTxt (DATETIME_INFO *infoPtr, int count, LPWSTR result, int resultSize)
307 static const WCHAR fmt_dW[] = { '%', 'd', 0 };
308 static const WCHAR fmt__2dW[] = { '%', '.', '2', 'd', 0 };
309 static const WCHAR fmt__3sW[] = { '%', '.', '3', 's', 0 };
310 SYSTEMTIME date = infoPtr->date;
315 TRACE ("%d,%d\n", infoPtr->nrFields, count);
316 if (count>infoPtr->nrFields || count < 0) {
317 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
321 if (!infoPtr->fieldspec) return;
323 spec = infoPtr->fieldspec[count];
324 if (spec & DT_STRING) {
325 int txtlen = infoPtr->buflen[count];
327 if (txtlen > resultSize)
328 txtlen = resultSize - 1;
329 memcpy (result, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
331 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
341 wsprintfW (result, fmt_dW, date.wDay);
344 wsprintfW (result, fmt__2dW, date.wDay);
347 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1+(date.wDayOfWeek+6)%7, result, 4);
348 /*wsprintfW (result,"%.3s",days[date.wDayOfWeek]);*/
351 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME1+(date.wDayOfWeek+6)%7, result, resultSize);
354 if (date.wHour == 0) {
360 wsprintfW (result, fmt_dW, date.wHour - (date.wHour > 12 ? 12 : 0));
363 if (date.wHour == 0) {
369 wsprintfW (result, fmt__2dW, date.wHour - (date.wHour > 12 ? 12 : 0));
372 wsprintfW (result, fmt_dW, date.wHour);
375 wsprintfW (result, fmt__2dW, date.wHour);
378 wsprintfW (result, fmt_dW, date.wSecond);
381 wsprintfW (result, fmt__2dW, date.wSecond);
384 wsprintfW (result, fmt_dW, date.wMinute);
387 wsprintfW (result, fmt__2dW, date.wMinute);
390 wsprintfW (result, fmt_dW, date.wMonth);
393 wsprintfW (result, fmt__2dW, date.wMonth);
396 GetLocaleInfoW(GetSystemDefaultLCID(), LOCALE_SMONTHNAME1+date.wMonth -1,
397 buffer, sizeof(buffer)/sizeof(buffer[0]));
398 wsprintfW (result, fmt__3sW, buffer);
401 GetLocaleInfoW(GetSystemDefaultLCID(),LOCALE_SMONTHNAME1+date.wMonth -1,
405 result[0] = (date.wHour < 12 ? 'A' : 'P');
409 result[0] = (date.wHour < 12 ? 'A' : 'P');
414 FIXME ("Not implemented\n");
419 wsprintfW (result, fmt_dW, date.wYear-10* (int) floor(date.wYear/10));
422 wsprintfW (result, fmt__2dW, date.wYear-100* (int) floor(date.wYear/100));
424 case INVALIDFULLYEAR:
426 wsprintfW (result, fmt_dW, date.wYear);
430 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
433 /* Offsets of days in the week to the weekday of january 1 in a leap year. */
434 static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
436 /* returns the day in the week(0 == sunday, 6 == saturday) */
437 /* day(1 == 1st, 2 == 2nd... etc), year is the year value */
438 static int DATETIME_CalculateDayOfWeek(DWORD day, DWORD month, DWORD year)
442 return((year + year/4 - year/100 + year/400 +
443 DayOfWeekTable[month-1] + day ) % 7);
446 static int wrap(int val, int delta, int minVal, int maxVal)
449 if (delta == INT_MIN || val < minVal) return maxVal;
450 if (delta == INT_MAX || val > maxVal) return minVal;
455 DATETIME_IncreaseField (DATETIME_INFO *infoPtr, int number, int delta)
457 SYSTEMTIME *date = &infoPtr->date;
459 TRACE ("%d\n", number);
460 if ((number > infoPtr->nrFields) || (number < 0)) return;
462 if ((infoPtr->fieldspec[number] & DTHT_DATEFIELD) == 0) return;
464 switch (infoPtr->fieldspec[number]) {
468 date->wYear = wrap(date->wYear, delta, 1752, 9999);
469 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(date->wDay,date->wMonth,date->wYear);
475 date->wMonth = wrap(date->wMonth, delta, 1, 12);
476 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(date->wDay,date->wMonth,date->wYear);
483 date->wDay = wrap(date->wDay, delta, 1, MONTHCAL_MonthLength(date->wMonth, date->wYear));
484 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(date->wDay,date->wMonth,date->wYear);
494 date->wHour = wrap(date->wHour, delta, 0, 23);
498 date->wMinute = wrap(date->wMinute, delta, 0, 59);
502 date->wSecond = wrap(date->wSecond, delta, 0, 59);
505 FIXME ("Not implemented\n");
509 /* FYI: On 1752/9/14 the calendar changed and England and the
510 * American colonies changed to the Gregorian calendar. This change
511 * involved having September 14th follow September 2nd. So no date
512 * algorithm works before that date.
514 if (10000 * date->wYear + 100 * date->wMonth + date->wDay < 17520914) {
526 DATETIME_ReturnFieldWidth (DATETIME_INFO *infoPtr, HDC hdc, int count, SHORT *fieldWidthPtr)
528 /* fields are a fixed width, determined by the largest possible string */
529 /* presumably, these widths should be language dependent */
530 static const WCHAR fld_d1W[] = { '2', 0 };
531 static const WCHAR fld_d2W[] = { '2', '2', 0 };
532 static const WCHAR fld_d4W[] = { '2', '2', '2', '2', 0 };
533 static const WCHAR fld_am1[] = { 'A', 0 };
534 static const WCHAR fld_am2[] = { 'A', 'M', 0 };
535 static const WCHAR fld_day[] = { 'W', 'e', 'd', 'n', 'e', 's', 'd', 'a', 'y', 0 };
536 static const WCHAR fld_day3[] = { 'W', 'e', 'd', 0 };
537 static const WCHAR fld_mon[] = { 'S', 'e', 'p', 't', 'e', 'm', 'b', 'e', 'r', 0 };
538 static const WCHAR fld_mon3[] = { 'D', 'e', 'c', 0 };
544 TRACE ("%d,%d\n", infoPtr->nrFields, count);
545 if (count>infoPtr->nrFields || count < 0) {
546 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
550 if (!infoPtr->fieldspec) return;
552 spec = infoPtr->fieldspec[count];
553 if (spec & DT_STRING) {
554 int txtlen = infoPtr->buflen[count];
558 memcpy (buffer, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
571 /* these seem to use a two byte field */
581 case INVALIDFULLYEAR:
608 GetTextExtentPoint32W (hdc, bufptr, strlenW(bufptr), &size);
609 *fieldWidthPtr = size.cx;
613 DATETIME_Refresh (DATETIME_INFO *infoPtr, HDC hdc)
617 RECT *rcDraw = &infoPtr->rcDraw;
618 RECT *calbutton = &infoPtr->calbutton;
619 RECT *checkbox = &infoPtr->checkbox;
621 COLORREF oldTextColor;
622 SHORT fieldWidth = 0;
624 /* draw control edge */
627 if (infoPtr->dateValid) {
628 HFONT oldFont = SelectObject (hdc, infoPtr->hFont);
629 INT oldBkMode = SetBkMode (hdc, TRANSPARENT);
632 DATETIME_ReturnTxt (infoPtr, 0, txt, sizeof(txt)/sizeof(txt[0]));
633 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
634 rcDraw->bottom = size.cy + 2;
636 prevright = checkbox->right = ((infoPtr->dwStyle & DTS_SHOWNONE) ? 18 : 2);
638 for (i = 0; i < infoPtr->nrFields; i++) {
639 DATETIME_ReturnTxt (infoPtr, i, txt, sizeof(txt)/sizeof(txt[0]));
640 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
641 DATETIME_ReturnFieldWidth (infoPtr, hdc, i, &fieldWidth);
642 field = &infoPtr->fieldRect[i];
643 field->left = prevright;
644 field->right = prevright + fieldWidth;
645 field->top = rcDraw->top;
646 field->bottom = rcDraw->bottom;
647 prevright = field->right;
649 if (infoPtr->dwStyle & WS_DISABLED)
650 oldTextColor = SetTextColor (hdc, comctl32_color.clrGrayText);
651 else if ((infoPtr->haveFocus) && (i == infoPtr->select)) {
652 /* fill if focussed */
653 HBRUSH hbr = CreateSolidBrush (comctl32_color.clrActiveCaption);
654 FillRect(hdc, field, hbr);
656 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindow);
659 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindowText);
661 /* draw the date text using the colour set above */
662 DrawTextW (hdc, txt, strlenW(txt), field, DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
663 SetTextColor (hdc, oldTextColor);
665 SetBkMode (hdc, oldBkMode);
666 SelectObject (hdc, oldFont);
669 if (!(infoPtr->dwStyle & DTS_UPDOWN)) {
670 DrawFrameControl(hdc, calbutton, DFC_SCROLL,
671 DFCS_SCROLLDOWN | (infoPtr->bCalDepressed ? DFCS_PUSHED : 0) |
672 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
678 DATETIME_HitTest (DATETIME_INFO *infoPtr, POINT pt)
682 TRACE ("%d, %d\n", pt.x, pt.y);
684 if (PtInRect (&infoPtr->calbutton, pt)) return DTHT_MCPOPUP;
685 if (PtInRect (&infoPtr->checkbox, pt)) return DTHT_CHECKBOX;
687 for (i=0; i < infoPtr->nrFields; i++) {
688 if (PtInRect (&infoPtr->fieldRect[i], pt)) return i;
696 DATETIME_LButtonDown (DATETIME_INFO *infoPtr, WORD wKey, INT x, INT y)
703 old = infoPtr->select;
704 new = DATETIME_HitTest (infoPtr, pt);
706 /* FIXME: might be conditions where we don't want to update infoPtr->select */
707 infoPtr->select = new;
709 SetFocus(infoPtr->hwndSelf);
711 if (infoPtr->select == DTHT_MCPOPUP) {
713 SendMessageW(infoPtr->hMonthCal, MCM_GETMINREQRECT, 0, (LPARAM)&rcMonthCal);
715 /* FIXME: button actually is only depressed during dropdown of the */
716 /* calendar control and when the mouse is over the button window */
717 infoPtr->bCalDepressed = TRUE;
719 /* recalculate the position of the monthcal popup */
720 if(infoPtr->dwStyle & DTS_RIGHTALIGN)
721 infoPtr->monthcal_pos.x = infoPtr->calbutton.left -
722 (rcMonthCal.right - rcMonthCal.left);
724 /* FIXME: this should be after the area reserved for the checkbox */
725 infoPtr->monthcal_pos.x = infoPtr->rcDraw.left;
727 infoPtr->monthcal_pos.y = infoPtr->rcClient.bottom;
728 ClientToScreen (infoPtr->hwndSelf, &(infoPtr->monthcal_pos));
729 SetWindowPos(infoPtr->hMonthCal, 0, infoPtr->monthcal_pos.x,
730 infoPtr->monthcal_pos.y, rcMonthCal.right - rcMonthCal.left,
731 rcMonthCal.bottom - rcMonthCal.top, 0);
733 if(IsWindowVisible(infoPtr->hMonthCal)) {
734 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
736 SYSTEMTIME *lprgSysTimeArray = &infoPtr->date;
737 TRACE("update calendar %04d/%02d/%02d\n",
738 lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay);
739 SendMessageW(infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
740 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
743 TRACE ("dt:%p mc:%p mc parent:%p, desktop:%p\n",
744 infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hwndNotify, GetDesktopWindow ());
745 DATETIME_SendSimpleNotify (infoPtr, DTN_DROPDOWN);
748 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
755 DATETIME_LButtonUp (DATETIME_INFO *infoPtr, WORD wKey)
757 if(infoPtr->bCalDepressed) {
758 infoPtr->bCalDepressed = FALSE;
759 InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
767 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
771 hdc = BeginPaint (infoPtr->hwndSelf, &ps);
772 DATETIME_Refresh (infoPtr, hdc);
773 EndPaint (infoPtr->hwndSelf, &ps);
775 DATETIME_Refresh (infoPtr, hdc);
782 DATETIME_Button_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
784 if( HIWORD(wParam) == BN_CLICKED) {
785 DWORD state = SendMessageW((HWND)lParam, BM_GETCHECK, 0, 0);
786 infoPtr->dateValid = (state == BST_CHECKED);
787 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
795 DATETIME_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
797 TRACE("hwndbutton = %p\n", infoPtr->hwndCheckbut);
798 if(infoPtr->hwndCheckbut == (HWND)lParam)
799 return DATETIME_Button_Command(infoPtr, wParam, lParam);
805 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
807 TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
809 infoPtr->dwStyle &= ~WS_DISABLED;
811 infoPtr->dwStyle |= WS_DISABLED;
817 DATETIME_EraseBackground (DATETIME_INFO *infoPtr, HDC hdc)
819 HBRUSH hBrush, hSolidBrush = NULL;
822 if (infoPtr->dwStyle & WS_DISABLED)
823 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
826 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
827 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
829 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
832 GetClientRect (infoPtr->hwndSelf, &rc);
834 FillRect (hdc, &rc, hBrush);
837 DeleteObject(hSolidBrush);
844 DATETIME_Notify (DATETIME_INFO *infoPtr, int idCtrl, LPNMHDR lpnmh)
846 TRACE ("Got notification %x from %p\n", lpnmh->code, lpnmh->hwndFrom);
847 TRACE ("info: %p %p %p\n", infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hUpdown);
849 if (lpnmh->code == MCN_SELECT) {
850 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
851 infoPtr->dateValid = TRUE;
852 SendMessageW (infoPtr->hMonthCal, MCM_GETCURSEL, 0, (LPARAM)&infoPtr->date);
853 TRACE("got from calendar %04d/%02d/%02d day of week %d\n",
854 infoPtr->date.wYear, infoPtr->date.wMonth, infoPtr->date.wDay, infoPtr->date.wDayOfWeek);
855 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
856 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
857 DATETIME_SendDateTimeChangeNotify (infoPtr);
859 if ((lpnmh->hwndFrom == infoPtr->hUpdown) && (lpnmh->code == UDN_DELTAPOS)) {
860 LPNMUPDOWN lpnmud = (LPNMUPDOWN)lpnmh;
861 TRACE("Delta pos %d\n", lpnmud->iDelta);
862 infoPtr->pendingUpdown = lpnmud->iDelta;
869 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode, LPARAM flags)
871 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
874 if (!(infoPtr->haveFocus)) return 0;
875 if ((fieldNum==0) && (infoPtr->select)) return 0;
877 if (infoPtr->select & FORMATCALLMASK) {
878 FIXME ("Callbacks not implemented yet\n");
881 if (vkCode >= '0' && vkCode <= '9') {
882 /* this is a somewhat simplified version of what Windows does */
883 SYSTEMTIME *date = &infoPtr->date;
884 switch (infoPtr->fieldspec[fieldNum]) {
887 date->wYear = date->wYear - (date->wYear%100) +
888 (date->wYear%10)*10 + (vkCode-'0');
889 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
890 date->wDay,date->wMonth,date->wYear);
891 DATETIME_SendDateTimeChangeNotify (infoPtr);
893 case INVALIDFULLYEAR:
895 date->wYear = (date->wYear%1000)*10 + (vkCode-'0');
896 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
897 date->wDay,date->wMonth,date->wYear);
898 DATETIME_SendDateTimeChangeNotify (infoPtr);
902 if ((date->wMonth%10) > 1 || (vkCode-'0') > 2)
903 date->wMonth = vkCode-'0';
905 date->wMonth = (date->wMonth%10)*10+vkCode-'0';
906 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
907 date->wDay,date->wMonth,date->wYear);
908 DATETIME_SendDateTimeChangeNotify (infoPtr);
912 /* probably better checking here would help */
913 if ((date->wDay%10) >= 3 && (vkCode-'0') > 1)
914 date->wDay = vkCode-'0';
916 date->wDay = (date->wDay%10)*10+vkCode-'0';
917 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
918 date->wDay,date->wMonth,date->wYear);
919 DATETIME_SendDateTimeChangeNotify (infoPtr);
923 if ((date->wHour%10) > 1 || (vkCode-'0') > 2)
924 date->wHour = vkCode-'0';
926 date->wHour = (date->wHour%10)*10+vkCode-'0';
927 DATETIME_SendDateTimeChangeNotify (infoPtr);
931 if ((date->wHour%10) > 2)
932 date->wHour = vkCode-'0';
933 else if ((date->wHour%10) == 2 && (vkCode-'0') > 3)
934 date->wHour = vkCode-'0';
936 date->wHour = (date->wHour%10)*10+vkCode-'0';
937 DATETIME_SendDateTimeChangeNotify (infoPtr);
941 if ((date->wMinute%10) > 5)
942 date->wMinute = vkCode-'0';
944 date->wMinute = (date->wMinute%10)*10+vkCode-'0';
945 DATETIME_SendDateTimeChangeNotify (infoPtr);
949 if ((date->wSecond%10) > 5)
950 date->wSecond = vkCode-'0';
952 date->wSecond = (date->wSecond%10)*10+vkCode-'0';
953 DATETIME_SendDateTimeChangeNotify (infoPtr);
961 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
962 DATETIME_SendDateTimeChangeNotify (infoPtr);
966 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
967 DATETIME_SendDateTimeChangeNotify (infoPtr);
970 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MIN);
971 DATETIME_SendDateTimeChangeNotify (infoPtr);
974 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MAX);
975 DATETIME_SendDateTimeChangeNotify (infoPtr);
979 if (infoPtr->select == 0) {
980 infoPtr->select = infoPtr->nrFields - 1;
985 } while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
990 if (infoPtr->select==infoPtr->nrFields) {
994 } while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
998 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1005 DATETIME_VScroll (DATETIME_INFO *infoPtr, WORD wScroll)
1007 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1009 if ((SHORT)LOWORD(wScroll) != SB_THUMBPOSITION) return 0;
1010 if (!(infoPtr->haveFocus)) return 0;
1011 if ((fieldNum==0) && (infoPtr->select)) return 0;
1013 if (infoPtr->pendingUpdown >= 0) {
1014 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1015 DATETIME_SendDateTimeChangeNotify (infoPtr);
1018 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1019 DATETIME_SendDateTimeChangeNotify (infoPtr);
1022 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1029 DATETIME_KillFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1031 TRACE("lost focus to %p\n", lostFocus);
1033 if (infoPtr->haveFocus) {
1034 DATETIME_SendSimpleNotify (infoPtr, NM_KILLFOCUS);
1035 infoPtr->haveFocus = 0;
1038 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
1045 DATETIME_NCCreate (HWND hwnd, LPCREATESTRUCTW lpcs)
1047 DWORD dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1048 /* force control to have client edge */
1049 dwExStyle |= WS_EX_CLIENTEDGE;
1050 SetWindowLongW(hwnd, GWL_EXSTYLE, dwExStyle);
1052 return DefWindowProcW(hwnd, WM_NCCREATE, 0, (LPARAM)lpcs);
1057 DATETIME_SetFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1059 TRACE("got focus from %p\n", lostFocus);
1061 if (infoPtr->haveFocus == 0) {
1062 DATETIME_SendSimpleNotify (infoPtr, NM_SETFOCUS);
1063 infoPtr->haveFocus = DTHT_GOTFOCUS;
1066 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1073 DATETIME_SendDateTimeChangeNotify (DATETIME_INFO *infoPtr)
1075 NMDATETIMECHANGE dtdtc;
1077 dtdtc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1078 dtdtc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1079 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1081 dtdtc.dwFlags = (infoPtr->dwStyle & DTS_SHOWNONE) ? GDT_NONE : GDT_VALID;
1083 MONTHCAL_CopyTime (&infoPtr->date, &dtdtc.st);
1084 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1085 (WPARAM)dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1090 DATETIME_SendSimpleNotify (DATETIME_INFO *infoPtr, UINT code)
1094 TRACE("%x\n", code);
1095 nmhdr.hwndFrom = infoPtr->hwndSelf;
1096 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1099 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1100 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
1104 DATETIME_Size (DATETIME_INFO *infoPtr, WORD flags, INT width, INT height)
1107 infoPtr->rcClient.bottom = height;
1108 infoPtr->rcClient.right = width;
1110 TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1112 infoPtr->rcDraw = infoPtr->rcClient;
1114 if (infoPtr->dwStyle & DTS_UPDOWN) {
1115 SetWindowPos(infoPtr->hUpdown, NULL,
1116 infoPtr->rcClient.right-14, 0,
1117 15, infoPtr->rcClient.bottom - infoPtr->rcClient.top,
1118 SWP_NOACTIVATE | SWP_NOZORDER);
1121 /* set the size of the button that drops the calendar down */
1122 /* FIXME: account for style that allows button on left side */
1123 infoPtr->calbutton.top = infoPtr->rcDraw.top;
1124 infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1125 infoPtr->calbutton.left = infoPtr->rcDraw.right-15;
1126 infoPtr->calbutton.right = infoPtr->rcDraw.right;
1129 /* set enable/disable button size for show none style being enabled */
1130 /* FIXME: these dimensions are completely incorrect */
1131 infoPtr->checkbox.top = infoPtr->rcDraw.top;
1132 infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1133 infoPtr->checkbox.left = infoPtr->rcDraw.left;
1134 infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1136 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1143 DATETIME_StyleChanged(DATETIME_INFO *infoPtr, WPARAM wStyleType, LPSTYLESTRUCT lpss)
1145 static const WCHAR buttonW[] = { 'b', 'u', 't', 't', 'o', 'n', 0 };
1147 TRACE("(styletype=%x, styleOld=0x%08x, styleNew=0x%08x)\n",
1148 wStyleType, lpss->styleOld, lpss->styleNew);
1150 if (wStyleType != GWL_STYLE) return 0;
1152 infoPtr->dwStyle = lpss->styleNew;
1154 if ( !(lpss->styleOld & DTS_SHOWNONE) && (lpss->styleNew & DTS_SHOWNONE) ) {
1155 infoPtr->hwndCheckbut = CreateWindowExW (0, buttonW, 0, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1156 2, 2, 13, 13, infoPtr->hwndSelf, 0,
1157 (HINSTANCE)GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_HINSTANCE), 0);
1158 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, 1, 0);
1160 if ( (lpss->styleOld & DTS_SHOWNONE) && !(lpss->styleNew & DTS_SHOWNONE) ) {
1161 DestroyWindow(infoPtr->hwndCheckbut);
1162 infoPtr->hwndCheckbut = 0;
1164 if ( !(lpss->styleOld & DTS_UPDOWN) && (lpss->styleNew & DTS_UPDOWN) ) {
1165 infoPtr->hUpdown = CreateUpDownControl (WS_CHILD | WS_BORDER | WS_VISIBLE, 120, 1, 20, 20,
1166 infoPtr->hwndSelf, 1, 0, 0, UD_MAXVAL, UD_MINVAL, 0);
1168 if ( (lpss->styleOld & DTS_UPDOWN) && !(lpss->styleNew & DTS_UPDOWN) ) {
1169 DestroyWindow(infoPtr->hUpdown);
1170 infoPtr->hUpdown = 0;
1173 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1179 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1181 infoPtr->hFont = font;
1182 if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1188 DATETIME_Create (HWND hwnd, LPCREATESTRUCTW lpcs)
1190 static const WCHAR SysMonthCal32W[] = { 'S', 'y', 's', 'M', 'o', 'n', 't', 'h', 'C', 'a', 'l', '3', '2', 0 };
1191 DATETIME_INFO *infoPtr = (DATETIME_INFO *)Alloc (sizeof(DATETIME_INFO));
1192 STYLESTRUCT ss = { 0, lpcs->style };
1194 if (!infoPtr) return -1;
1196 infoPtr->hwndSelf = hwnd;
1197 infoPtr->dwStyle = lpcs->style;
1199 infoPtr->nrFieldsAllocated = 32;
1200 infoPtr->fieldspec = (int *) Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1201 infoPtr->fieldRect = (RECT *) Alloc (infoPtr->nrFieldsAllocated * sizeof(RECT));
1202 infoPtr->buflen = (int *) Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1203 infoPtr->hwndNotify = lpcs->hwndParent;
1204 infoPtr->select = -1; /* initially, nothing is selected */
1206 DATETIME_StyleChanged(infoPtr, GWL_STYLE, &ss);
1207 DATETIME_SetFormatW (infoPtr, 0);
1209 /* create the monthcal control */
1210 infoPtr->hMonthCal = CreateWindowExW (0, SysMonthCal32W, 0, WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS,
1211 0, 0, 0, 0, infoPtr->hwndSelf, 0, 0, 0);
1213 /* initialize info structure */
1214 GetLocalTime (&infoPtr->date);
1215 infoPtr->dateValid = TRUE;
1216 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1218 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1226 DATETIME_Destroy (DATETIME_INFO *infoPtr)
1228 if (infoPtr->hwndCheckbut)
1229 DestroyWindow(infoPtr->hwndCheckbut);
1230 if (infoPtr->hUpdown)
1231 DestroyWindow(infoPtr->hUpdown);
1232 if (infoPtr->hMonthCal)
1233 DestroyWindow(infoPtr->hMonthCal);
1234 SetWindowLongPtrW( infoPtr->hwndSelf, 0, 0 ); /* clear infoPtr */
1240 static LRESULT WINAPI
1241 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1243 DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1246 TRACE ("%x, %x, %lx\n", uMsg, wParam, lParam);
1248 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
1249 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
1253 case DTM_GETSYSTEMTIME:
1254 return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1256 case DTM_SETSYSTEMTIME:
1257 return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1260 ret = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1261 return ret ? ret : 1; /* bug emulation */
1264 return SendMessageW (infoPtr->hMonthCal, MCM_SETRANGE, wParam, lParam);
1266 case DTM_SETFORMATA:
1267 return DATETIME_SetFormatA (infoPtr, (LPCSTR)lParam);
1269 case DTM_SETFORMATW:
1270 return DATETIME_SetFormatW (infoPtr, (LPCWSTR)lParam);
1272 case DTM_GETMONTHCAL:
1273 return (LRESULT)infoPtr->hMonthCal;
1275 case DTM_SETMCCOLOR:
1276 return SendMessageW (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
1278 case DTM_GETMCCOLOR:
1279 return SendMessageW (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
1282 return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1285 return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1288 return DATETIME_Notify (infoPtr, (int)wParam, (LPNMHDR)lParam);
1291 return DATETIME_Enable (infoPtr, (BOOL)wParam);
1294 return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1297 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1299 case WM_PRINTCLIENT:
1301 return DATETIME_Paint (infoPtr, (HDC)wParam);
1304 return DATETIME_KeyDown (infoPtr, wParam, lParam);
1307 return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1310 return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1313 return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1316 return DATETIME_Size (infoPtr, wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1318 case WM_LBUTTONDOWN:
1319 return DATETIME_LButtonDown (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1322 return DATETIME_LButtonUp (infoPtr, (WORD)wParam);
1325 return DATETIME_VScroll (infoPtr, (WORD)wParam);
1328 return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1331 return DATETIME_Destroy (infoPtr);
1334 return DATETIME_Command (infoPtr, wParam, lParam);
1336 case WM_STYLECHANGED:
1337 return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1340 return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1343 return (LRESULT) infoPtr->hFont;
1346 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
1347 ERR("unknown msg %04x wp=%08x lp=%08lx\n",
1348 uMsg, wParam, lParam);
1349 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1355 DATETIME_Register (void)
1359 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1360 wndClass.style = CS_GLOBALCLASS;
1361 wndClass.lpfnWndProc = DATETIME_WindowProc;
1362 wndClass.cbClsExtra = 0;
1363 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1364 wndClass.hCursor = LoadCursorW (0, (LPCWSTR)IDC_ARROW);
1365 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1366 wndClass.lpszClassName = DATETIMEPICK_CLASSW;
1368 RegisterClassW (&wndClass);
1373 DATETIME_Unregister (void)
1375 UnregisterClassW (DATETIMEPICK_CLASSW, NULL);