wined3d: Add crs and pow instruction support.
[wine/wine-kai.git] / dlls / comctl32 / datetime.c
blob40c2ca504509cc64340104567b9e864226134393
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>
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
22 * NOTE
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.
31 * TODO:
32 * -- DTS_APPCANPARSE
33 * -- DTS_SHORTDATECENTURYFORMAT
34 * -- DTN_CLOSEUP
35 * -- DTN_FORMAT
36 * -- DTN_FORMATQUERY
37 * -- DTN_USERSTRING
38 * -- DTN_WMKEYDOWN
39 * -- FORMATCALLBACK
42 #include <math.h>
43 #include <string.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <limits.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "commctrl.h"
54 #include "comctl32.h"
55 #include "wine/debug.h"
56 #include "wine/unicode.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(datetime);
60 typedef struct
62 HWND hwndSelf;
63 HWND hMonthCal;
64 HWND hwndNotify;
65 HWND hUpdown;
66 DWORD dwStyle;
67 SYSTEMTIME date;
68 BOOL dateValid;
69 HWND hwndCheckbut;
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 */
75 int select;
76 HFONT hFont;
77 int nrFieldsAllocated;
78 int nrFields;
79 int haveFocus;
80 int *fieldspec;
81 RECT *fieldRect;
82 int *buflen;
83 WCHAR textbuf[256];
84 POINT monthcal_pos;
85 int pendingUpdown;
86 } DATETIME_INFO, *LPDATETIME_INFO;
88 /* in monthcal.c */
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
93 * specifier.
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
102 #define FULLDAY 0x04
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 */
127 #define DTHT_NONE 0
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};
139 static DWORD
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))
146 return GDT_NONE;
148 MONTHCAL_CopyTime (&infoPtr->date, lprgSysTimeArray);
150 return GDT_VALID;
154 static BOOL
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
173 return 0;
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);
183 else
184 return 0;
186 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
187 return TRUE;
191 /***
192 * Split up a formattxt in actions.
193 * See ms documentation for the meaning of the letter codes/'specifiers'.
195 * Notes:
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'.
202 static void
203 DATETIME_UseFormat (DATETIME_INFO *infoPtr, LPCWSTR formattxt)
205 unsigned int i;
206 int j, k, len;
207 int *nrFields = &infoPtr->nrFields;
209 *nrFields = 0;
210 infoPtr->fieldspec[*nrFields] = 0;
211 len = strlenW(allowedformatchars);
212 k = 0;
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;
221 break;
223 if (infoPtr->fieldspec[*nrFields] >> 4 != j) {
224 (*nrFields)++;
225 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
226 break;
228 if ((infoPtr->fieldspec[*nrFields] & 0x0f) == maxrepetition[j]) {
229 (*nrFields)++;
230 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
231 break;
233 infoPtr->fieldspec[*nrFields]++;
234 break;
235 } /* if allowedformatchar */
236 } /* for j */
238 /* char is not a specifier: handle char like a string */
239 if (j == len) {
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) {
244 (*nrFields)++;
245 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
246 infoPtr->buflen[*nrFields] = 0;
248 infoPtr->textbuf[k] = formattxt[i];
249 k++;
250 infoPtr->buflen[*nrFields]++;
251 } /* if j=len */
253 if (*nrFields == infoPtr->nrFieldsAllocated) {
254 FIXME ("out of memory; should reallocate. crash ahead.\n");
256 } /* for i */
258 TRACE("\n");
260 if (infoPtr->fieldspec[*nrFields] != 0) (*nrFields)++;
264 static BOOL
265 DATETIME_SetFormatW (DATETIME_INFO *infoPtr, LPCWSTR lpszFormat)
267 if (!lpszFormat) {
268 WCHAR format_buf[80];
269 DWORD format_item;
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);
284 return 1;
288 static BOOL
289 DATETIME_SetFormatA (DATETIME_INFO *infoPtr, LPCSTR lpszFormat)
291 if (lpszFormat) {
292 BOOL retval;
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);
297 Free (wstr);
298 return retval;
300 else
301 return DATETIME_SetFormatW (infoPtr, 0);
306 static void
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;
313 int spec;
314 WCHAR buffer[80];
316 *result=0;
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);
320 return;
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));
332 result[txtlen] = 0;
333 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
334 return;
338 switch (spec) {
339 case DT_END_FORMAT:
340 *result = 0;
341 break;
342 case ONEDIGITDAY:
343 wsprintfW (result, fmt_dW, date.wDay);
344 break;
345 case TWODIGITDAY:
346 wsprintfW (result, fmt__2dW, date.wDay);
347 break;
348 case THREECHARDAY:
349 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1+(date.wDayOfWeek+6)%7, result, 4);
350 /*wsprintfW (result,"%.3s",days[date.wDayOfWeek]);*/
351 break;
352 case FULLDAY:
353 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME1+(date.wDayOfWeek+6)%7, result, resultSize);
354 break;
355 case ONEDIGIT12HOUR:
356 if (date.wHour == 0) {
357 result[0] = '1';
358 result[1] = '2';
359 result[2] = 0;
361 else
362 wsprintfW (result, fmt_dW, date.wHour - (date.wHour > 12 ? 12 : 0));
363 break;
364 case TWODIGIT12HOUR:
365 if (date.wHour == 0) {
366 result[0] = '1';
367 result[1] = '2';
368 result[2] = 0;
370 else
371 wsprintfW (result, fmt__2dW, date.wHour - (date.wHour > 12 ? 12 : 0));
372 break;
373 case ONEDIGIT24HOUR:
374 wsprintfW (result, fmt_dW, date.wHour);
375 break;
376 case TWODIGIT24HOUR:
377 wsprintfW (result, fmt__2dW, date.wHour);
378 break;
379 case ONEDIGITSECOND:
380 wsprintfW (result, fmt_dW, date.wSecond);
381 break;
382 case TWODIGITSECOND:
383 wsprintfW (result, fmt__2dW, date.wSecond);
384 break;
385 case ONEDIGITMINUTE:
386 wsprintfW (result, fmt_dW, date.wMinute);
387 break;
388 case TWODIGITMINUTE:
389 wsprintfW (result, fmt__2dW, date.wMinute);
390 break;
391 case ONEDIGITMONTH:
392 wsprintfW (result, fmt_dW, date.wMonth);
393 break;
394 case TWODIGITMONTH:
395 wsprintfW (result, fmt__2dW, date.wMonth);
396 break;
397 case THREECHARMONTH:
398 GetLocaleInfoW(GetSystemDefaultLCID(), LOCALE_SMONTHNAME1+date.wMonth -1,
399 buffer, sizeof(buffer)/sizeof(buffer[0]));
400 wsprintfW (result, fmt__3sW, buffer);
401 break;
402 case FULLMONTH:
403 GetLocaleInfoW(GetSystemDefaultLCID(),LOCALE_SMONTHNAME1+date.wMonth -1,
404 result, resultSize);
405 break;
406 case ONELETTERAMPM:
407 result[0] = (date.wHour < 12 ? 'A' : 'P');
408 result[1] = 0;
409 break;
410 case TWOLETTERAMPM:
411 result[0] = (date.wHour < 12 ? 'A' : 'P');
412 result[1] = 'M';
413 result[2] = 0;
414 break;
415 case FORMATCALLBACK:
416 FIXME ("Not implemented\n");
417 result[0] = 'x';
418 result[1] = 0;
419 break;
420 case ONEDIGITYEAR:
421 wsprintfW (result, fmt_dW, date.wYear-10* (int) floor(date.wYear/10));
422 break;
423 case TWODIGITYEAR:
424 wsprintfW (result, fmt__2dW, date.wYear-100* (int) floor(date.wYear/100));
425 break;
426 case INVALIDFULLYEAR:
427 case FULLYEAR:
428 wsprintfW (result, fmt_dW, date.wYear);
429 break;
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)
442 year-=(month < 3);
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)
450 val += delta;
451 if (delta == INT_MIN || val < minVal) return maxVal;
452 if (delta == INT_MAX || val > maxVal) return minVal;
453 return val;
456 static void
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]) {
467 case ONEDIGITYEAR:
468 case TWODIGITYEAR:
469 case FULLYEAR:
470 date->wYear = wrap(date->wYear, delta, 1752, 9999);
471 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(date->wDay,date->wMonth,date->wYear);
472 break;
473 case ONEDIGITMONTH:
474 case TWODIGITMONTH:
475 case THREECHARMONTH:
476 case FULLMONTH:
477 date->wMonth = wrap(date->wMonth, delta, 1, 12);
478 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(date->wDay,date->wMonth,date->wYear);
479 delta = 0;
480 /* fall through */
481 case ONEDIGITDAY:
482 case TWODIGITDAY:
483 case THREECHARDAY:
484 case FULLDAY:
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);
487 break;
488 case ONELETTERAMPM:
489 case TWOLETTERAMPM:
490 delta *= 12;
491 /* fall through */
492 case ONEDIGIT12HOUR:
493 case TWODIGIT12HOUR:
494 case ONEDIGIT24HOUR:
495 case TWODIGIT24HOUR:
496 date->wHour = wrap(date->wHour, delta, 0, 23);
497 break;
498 case ONEDIGITMINUTE:
499 case TWODIGITMINUTE:
500 date->wMinute = wrap(date->wMinute, delta, 0, 59);
501 break;
502 case ONEDIGITSECOND:
503 case TWODIGITSECOND:
504 date->wSecond = wrap(date->wSecond, delta, 0, 59);
505 break;
506 case FORMATCALLBACK:
507 FIXME ("Not implemented\n");
508 break;
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) {
517 date->wYear = 1752;
518 date->wMonth = 9;
519 date->wDay = 14;
520 date->wSecond = 0;
521 date->wMinute = 0;
522 date->wHour = 0;
527 static void
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 };
541 int spec;
542 WCHAR buffer[80];
543 LPCWSTR bufptr;
544 SIZE size;
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);
549 return;
552 if (!infoPtr->fieldspec) return;
554 spec = infoPtr->fieldspec[count];
555 if (spec & DT_STRING) {
556 int txtlen = infoPtr->buflen[count];
558 if (txtlen > 79)
559 txtlen = 79;
560 memcpy (buffer, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
561 buffer[txtlen] = 0;
562 bufptr = buffer;
564 else {
565 switch (spec) {
566 case ONEDIGITDAY:
567 case ONEDIGIT12HOUR:
568 case ONEDIGIT24HOUR:
569 case ONEDIGITSECOND:
570 case ONEDIGITMINUTE:
571 case ONEDIGITMONTH:
572 case ONEDIGITYEAR:
573 /* these seem to use a two byte field */
574 case TWODIGITDAY:
575 case TWODIGIT12HOUR:
576 case TWODIGIT24HOUR:
577 case TWODIGITSECOND:
578 case TWODIGITMINUTE:
579 case TWODIGITMONTH:
580 case TWODIGITYEAR:
581 bufptr = fld_d2W;
582 break;
583 case INVALIDFULLYEAR:
584 case FULLYEAR:
585 bufptr = fld_d4W;
586 break;
587 case THREECHARDAY:
588 bufptr = fld_day3;
589 break;
590 case FULLDAY:
591 bufptr = fld_day;
592 break;
593 case THREECHARMONTH:
594 bufptr = fld_mon3;
595 break;
596 case FULLMONTH:
597 bufptr = fld_mon;
598 break;
599 case ONELETTERAMPM:
600 bufptr = fld_am1;
601 break;
602 case TWOLETTERAMPM:
603 bufptr = fld_am2;
604 break;
605 default:
606 bufptr = fld_d1W;
607 break;
610 GetTextExtentPoint32W (hdc, bufptr, strlenW(bufptr), &size);
611 *fieldWidthPtr = size.cx;
614 static void
615 DATETIME_Refresh (DATETIME_INFO *infoPtr, HDC hdc)
617 int i,prevright;
618 RECT *field;
619 RECT *rcDraw = &infoPtr->rcDraw;
620 RECT *calbutton = &infoPtr->calbutton;
621 RECT *checkbox = &infoPtr->checkbox;
622 SIZE size;
623 COLORREF oldTextColor;
624 SHORT fieldWidth = 0;
626 /* draw control edge */
627 TRACE("\n");
629 if (infoPtr->dateValid) {
630 HFONT oldFont = SelectObject (hdc, infoPtr->hFont);
631 INT oldBkMode = SetBkMode (hdc, TRANSPARENT);
632 WCHAR txt[80];
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);
657 DeleteObject (hbr);
658 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindow);
660 else
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) );
679 static INT
680 DATETIME_HitTest (const DATETIME_INFO *infoPtr, POINT pt)
682 int i;
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;
693 return DTHT_NONE;
697 static LRESULT
698 DATETIME_LButtonDown (DATETIME_INFO *infoPtr, WORD wKey, INT x, INT y)
700 POINT pt;
701 int old, new;
703 pt.x = x;
704 pt.y = 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) {
714 RECT rcMonthCal;
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);
725 else
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);
737 } else {
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);
752 return 0;
756 static LRESULT
757 DATETIME_LButtonUp (DATETIME_INFO *infoPtr, WORD wKey)
759 if(infoPtr->bCalDepressed) {
760 infoPtr->bCalDepressed = FALSE;
761 InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
764 return 0;
768 static LRESULT
769 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
771 if (!hdc) {
772 PAINTSTRUCT ps;
773 hdc = BeginPaint (infoPtr->hwndSelf, &ps);
774 DATETIME_Refresh (infoPtr, hdc);
775 EndPaint (infoPtr->hwndSelf, &ps);
776 } else {
777 DATETIME_Refresh (infoPtr, hdc);
779 return 0;
783 static LRESULT
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);
791 return 0;
796 static LRESULT
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);
802 return 0;
806 static LRESULT
807 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
809 TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
810 if (bEnable)
811 infoPtr->dwStyle &= ~WS_DISABLED;
812 else
813 infoPtr->dwStyle |= WS_DISABLED;
815 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
817 return 0;
821 static LRESULT
822 DATETIME_EraseBackground (const DATETIME_INFO *infoPtr, HDC hdc)
824 HBRUSH hBrush, hSolidBrush = NULL;
825 RECT rc;
827 if (infoPtr->dwStyle & WS_DISABLED)
828 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
829 else
831 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
832 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
833 if (!hBrush)
834 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
837 GetClientRect (infoPtr->hwndSelf, &rc);
839 FillRect (hdc, &rc, hBrush);
841 if (hSolidBrush)
842 DeleteObject(hSolidBrush);
844 return -1;
848 static LRESULT
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;
869 return 0;
873 static LRESULT
874 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode, LPARAM flags)
876 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
877 int wrap = 0;
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]) {
890 case ONEDIGITYEAR:
891 case TWODIGITYEAR:
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);
897 break;
898 case INVALIDFULLYEAR:
899 case FULLYEAR:
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);
904 break;
905 case ONEDIGITMONTH:
906 case TWODIGITMONTH:
907 if ((date->wMonth%10) > 1 || (vkCode-'0') > 2)
908 date->wMonth = vkCode-'0';
909 else
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);
914 break;
915 case ONEDIGITDAY:
916 case TWODIGITDAY:
917 /* probably better checking here would help */
918 if ((date->wDay%10) >= 3 && (vkCode-'0') > 1)
919 date->wDay = vkCode-'0';
920 else
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);
925 break;
926 case ONEDIGIT12HOUR:
927 case TWODIGIT12HOUR:
928 if ((date->wHour%10) > 1 || (vkCode-'0') > 2)
929 date->wHour = vkCode-'0';
930 else
931 date->wHour = (date->wHour%10)*10+vkCode-'0';
932 DATETIME_SendDateTimeChangeNotify (infoPtr);
933 break;
934 case ONEDIGIT24HOUR:
935 case TWODIGIT24HOUR:
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';
940 else
941 date->wHour = (date->wHour%10)*10+vkCode-'0';
942 DATETIME_SendDateTimeChangeNotify (infoPtr);
943 break;
944 case ONEDIGITMINUTE:
945 case TWODIGITMINUTE:
946 if ((date->wMinute%10) > 5)
947 date->wMinute = vkCode-'0';
948 else
949 date->wMinute = (date->wMinute%10)*10+vkCode-'0';
950 DATETIME_SendDateTimeChangeNotify (infoPtr);
951 break;
952 case ONEDIGITSECOND:
953 case TWODIGITSECOND:
954 if ((date->wSecond%10) > 5)
955 date->wSecond = vkCode-'0';
956 else
957 date->wSecond = (date->wSecond%10)*10+vkCode-'0';
958 DATETIME_SendDateTimeChangeNotify (infoPtr);
959 break;
963 switch (vkCode) {
964 case VK_ADD:
965 case VK_UP:
966 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
967 DATETIME_SendDateTimeChangeNotify (infoPtr);
968 break;
969 case VK_SUBTRACT:
970 case VK_DOWN:
971 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
972 DATETIME_SendDateTimeChangeNotify (infoPtr);
973 break;
974 case VK_HOME:
975 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MIN);
976 DATETIME_SendDateTimeChangeNotify (infoPtr);
977 break;
978 case VK_END:
979 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MAX);
980 DATETIME_SendDateTimeChangeNotify (infoPtr);
981 break;
982 case VK_LEFT:
983 do {
984 if (infoPtr->select == 0) {
985 infoPtr->select = infoPtr->nrFields - 1;
986 wrap++;
987 } else {
988 infoPtr->select--;
990 } while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
991 break;
992 case VK_RIGHT:
993 do {
994 infoPtr->select++;
995 if (infoPtr->select==infoPtr->nrFields) {
996 infoPtr->select = 0;
997 wrap++;
999 } while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
1000 break;
1003 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1005 return 0;
1009 static LRESULT
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);
1022 else {
1023 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1024 DATETIME_SendDateTimeChangeNotify (infoPtr);
1027 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1029 return 0;
1033 static LRESULT
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);
1045 return 0;
1049 static LRESULT
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);
1061 static LRESULT
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);
1073 return 0;
1077 static BOOL
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);
1094 static BOOL
1095 DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code)
1097 NMHDR nmhdr;
1099 TRACE("%x\n", code);
1100 nmhdr.hwndFrom = infoPtr->hwndSelf;
1101 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1102 nmhdr.code = code;
1104 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1105 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
1108 static LRESULT
1109 DATETIME_Size (DATETIME_INFO *infoPtr, WORD flags, INT width, INT height)
1111 /* set size */
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);
1125 else {
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);
1143 return 0;
1147 static LRESULT
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);
1179 return 0;
1183 static LRESULT
1184 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1186 infoPtr->hFont = font;
1187 if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1188 return 0;
1192 static LRESULT
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);
1225 return 0;
1230 static LRESULT
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 */
1240 Free (infoPtr);
1241 return 0;
1245 static LRESULT WINAPI
1246 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1248 DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1249 LRESULT ret;
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 );
1256 switch (uMsg) {
1258 case DTM_GETSYSTEMTIME:
1259 return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1261 case DTM_SETSYSTEMTIME:
1262 return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1264 case DTM_GETRANGE:
1265 ret = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1266 return ret ? ret : 1; /* bug emulation */
1268 case DTM_SETRANGE:
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);
1286 case DTM_SETMCFONT:
1287 return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1289 case DTM_GETMCFONT:
1290 return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1292 case WM_NOTIFY:
1293 return DATETIME_Notify (infoPtr, (int)wParam, (LPNMHDR)lParam);
1295 case WM_ENABLE:
1296 return DATETIME_Enable (infoPtr, (BOOL)wParam);
1298 case WM_ERASEBKGND:
1299 return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1301 case WM_GETDLGCODE:
1302 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1304 case WM_PRINTCLIENT:
1305 case WM_PAINT:
1306 return DATETIME_Paint (infoPtr, (HDC)wParam);
1308 case WM_KEYDOWN:
1309 return DATETIME_KeyDown (infoPtr, wParam, lParam);
1311 case WM_KILLFOCUS:
1312 return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1314 case WM_NCCREATE:
1315 return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1317 case WM_SETFOCUS:
1318 return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1320 case WM_SIZE:
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));
1326 case WM_LBUTTONUP:
1327 return DATETIME_LButtonUp (infoPtr, (WORD)wParam);
1329 case WM_VSCROLL:
1330 return DATETIME_VScroll (infoPtr, (WORD)wParam);
1332 case WM_CREATE:
1333 return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1335 case WM_DESTROY:
1336 return DATETIME_Destroy (infoPtr);
1338 case WM_COMMAND:
1339 return DATETIME_Command (infoPtr, wParam, lParam);
1341 case WM_STYLECHANGED:
1342 return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1344 case WM_SETFONT:
1345 return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1347 case WM_GETFONT:
1348 return (LRESULT) infoPtr->hFont;
1350 default:
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);
1359 void
1360 DATETIME_Register (void)
1362 WNDCLASSW wndClass;
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);
1377 void
1378 DATETIME_Unregister (void)
1380 UnregisterClassW (DATETIMEPICK_CLASSW, NULL);