wined3d: Add a test for fixed function bump mapping.
[wine/multimedia.git] / dlls / comctl32 / datetime.c
blob100cd746e03727e7582166fcd7fd0f23248bdafc
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 bDropdownEnabled;
76 int select;
77 HFONT hFont;
78 int nrFieldsAllocated;
79 int nrFields;
80 int haveFocus;
81 int *fieldspec;
82 RECT *fieldRect;
83 int *buflen;
84 WCHAR textbuf[256];
85 POINT monthcal_pos;
86 int pendingUpdown;
87 } DATETIME_INFO, *LPDATETIME_INFO;
89 /* in monthcal.c */
90 extern int MONTHCAL_MonthLength(int month, int year);
92 /* this list of defines is closely related to `allowedformatchars' defined
93 * in datetime.c; the high nibble indicates the `base type' of the format
94 * specifier.
95 * Do not change without first reading DATETIME_UseFormat.
99 #define DT_END_FORMAT 0
100 #define ONEDIGITDAY 0x01
101 #define TWODIGITDAY 0x02
102 #define THREECHARDAY 0x03
103 #define FULLDAY 0x04
104 #define ONEDIGIT12HOUR 0x11
105 #define TWODIGIT12HOUR 0x12
106 #define ONEDIGIT24HOUR 0x21
107 #define TWODIGIT24HOUR 0x22
108 #define ONEDIGITMINUTE 0x31
109 #define TWODIGITMINUTE 0x32
110 #define ONEDIGITMONTH 0x41
111 #define TWODIGITMONTH 0x42
112 #define THREECHARMONTH 0x43
113 #define FULLMONTH 0x44
114 #define ONEDIGITSECOND 0x51
115 #define TWODIGITSECOND 0x52
116 #define ONELETTERAMPM 0x61
117 #define TWOLETTERAMPM 0x62
118 #define ONEDIGITYEAR 0x71
119 #define TWODIGITYEAR 0x72
120 #define INVALIDFULLYEAR 0x73 /* FIXME - yyy is not valid - we'll treat it as yyyy */
121 #define FULLYEAR 0x74
122 #define FORMATCALLBACK 0x81 /* -> maximum of 0x80 callbacks possible */
123 #define FORMATCALLMASK 0x80
124 #define DT_STRING 0x0100
126 #define DTHT_DATEFIELD 0xff /* for hit-testing */
128 #define DTHT_NONE 0
129 #define DTHT_CHECKBOX 0x200 /* these should end at '00' , to make */
130 #define DTHT_MCPOPUP 0x300 /* & DTHT_DATEFIELD 0 when DATETIME_KeyDown */
131 #define DTHT_GOTFOCUS 0x400 /* tests for date-fields */
133 static BOOL DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code);
134 static BOOL DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr);
135 extern void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to);
136 static const WCHAR allowedformatchars[] = {'d', 'h', 'H', 'm', 'M', 's', 't', 'y', 'X', '\'', 0};
137 static const int maxrepetition [] = {4,2,2,2,4,2,2,4,-1,-1};
140 static DWORD
141 DATETIME_GetSystemTime (const DATETIME_INFO *infoPtr, SYSTEMTIME *lprgSysTimeArray)
143 if (!lprgSysTimeArray) return GDT_NONE;
145 if ((infoPtr->dwStyle & DTS_SHOWNONE) &&
146 (SendMessageW (infoPtr->hwndCheckbut, BM_GETCHECK, 0, 0) == BST_UNCHECKED))
147 return GDT_NONE;
149 MONTHCAL_CopyTime (&infoPtr->date, lprgSysTimeArray);
151 return GDT_VALID;
155 static BOOL
156 DATETIME_SetSystemTime (DATETIME_INFO *infoPtr, DWORD flag, const SYSTEMTIME *lprgSysTimeArray)
158 if (!lprgSysTimeArray) return 0;
160 TRACE("%04d/%02d/%02d %02d:%02d:%02d\n",
161 lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay,
162 lprgSysTimeArray->wHour, lprgSysTimeArray->wMinute, lprgSysTimeArray->wSecond);
164 if (flag == GDT_VALID) {
165 if (lprgSysTimeArray->wYear < 1601 || lprgSysTimeArray->wYear > 30827 ||
166 lprgSysTimeArray->wMonth < 1 || lprgSysTimeArray->wMonth > 12 ||
167 lprgSysTimeArray->wDayOfWeek > 6 ||
168 lprgSysTimeArray->wDay < 1 || lprgSysTimeArray->wDay > 31 ||
169 lprgSysTimeArray->wHour > 23 ||
170 lprgSysTimeArray->wMinute > 59 ||
171 lprgSysTimeArray->wSecond > 59 ||
172 lprgSysTimeArray->wMilliseconds > 999
174 return 0;
176 infoPtr->dateValid = TRUE;
177 MONTHCAL_CopyTime (lprgSysTimeArray, &infoPtr->date);
178 SendMessageW (infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
179 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
180 } else if ((infoPtr->dwStyle & DTS_SHOWNONE) && (flag == GDT_NONE)) {
181 infoPtr->dateValid = FALSE;
182 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_UNCHECKED, 0);
184 else
185 return 0;
187 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
188 return TRUE;
192 /***
193 * Split up a formattxt in actions.
194 * See ms documentation for the meaning of the letter codes/'specifiers'.
196 * Notes:
197 * *'dddddd' is handled as 'dddd' plus 'dd'.
198 * *unrecognized formats are strings (here given the type DT_STRING;
199 * start of the string is encoded in lower bits of DT_STRING.
200 * Therefore, 'string' ends finally up as '<show seconds>tring'.
203 static void
204 DATETIME_UseFormat (DATETIME_INFO *infoPtr, LPCWSTR formattxt)
206 unsigned int i;
207 int j, k, len;
208 int *nrFields = &infoPtr->nrFields;
210 *nrFields = 0;
211 infoPtr->fieldspec[*nrFields] = 0;
212 len = strlenW(allowedformatchars);
213 k = 0;
215 for (i = 0; formattxt[i]; i++) {
216 TRACE ("\n%d %c:", i, formattxt[i]);
217 for (j = 0; j < len; j++) {
218 if (allowedformatchars[j]==formattxt[i]) {
219 TRACE ("%c[%d,%x]", allowedformatchars[j], *nrFields, infoPtr->fieldspec[*nrFields]);
220 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
221 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
222 break;
224 if (infoPtr->fieldspec[*nrFields] >> 4 != j) {
225 (*nrFields)++;
226 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
227 break;
229 if ((infoPtr->fieldspec[*nrFields] & 0x0f) == maxrepetition[j]) {
230 (*nrFields)++;
231 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
232 break;
234 infoPtr->fieldspec[*nrFields]++;
235 break;
236 } /* if allowedformatchar */
237 } /* for j */
239 /* char is not a specifier: handle char like a string */
240 if (j == len) {
241 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
242 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
243 infoPtr->buflen[*nrFields] = 0;
244 } else if ((infoPtr->fieldspec[*nrFields] & DT_STRING) != DT_STRING) {
245 (*nrFields)++;
246 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
247 infoPtr->buflen[*nrFields] = 0;
249 infoPtr->textbuf[k] = formattxt[i];
250 k++;
251 infoPtr->buflen[*nrFields]++;
252 } /* if j=len */
254 if (*nrFields == infoPtr->nrFieldsAllocated) {
255 FIXME ("out of memory; should reallocate. crash ahead.\n");
257 } /* for i */
259 TRACE("\n");
261 if (infoPtr->fieldspec[*nrFields] != 0) (*nrFields)++;
265 static BOOL
266 DATETIME_SetFormatW (DATETIME_INFO *infoPtr, LPCWSTR lpszFormat)
268 if (!lpszFormat) {
269 WCHAR format_buf[80];
270 DWORD format_item;
272 if (infoPtr->dwStyle & DTS_LONGDATEFORMAT)
273 format_item = LOCALE_SLONGDATE;
274 else if ((infoPtr->dwStyle & DTS_TIMEFORMAT) == DTS_TIMEFORMAT)
275 format_item = LOCALE_STIMEFORMAT;
276 else /* DTS_SHORTDATEFORMAT */
277 format_item = LOCALE_SSHORTDATE;
278 GetLocaleInfoW( GetSystemDefaultLCID(), format_item, format_buf, sizeof(format_buf)/sizeof(format_buf[0]));
279 lpszFormat = format_buf;
282 DATETIME_UseFormat (infoPtr, lpszFormat);
283 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
285 return 1;
289 static BOOL
290 DATETIME_SetFormatA (DATETIME_INFO *infoPtr, LPCSTR lpszFormat)
292 if (lpszFormat) {
293 BOOL retval;
294 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, NULL, 0);
295 LPWSTR wstr = Alloc(len * sizeof(WCHAR));
296 if (wstr) MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, wstr, len);
297 retval = DATETIME_SetFormatW (infoPtr, wstr);
298 Free (wstr);
299 return retval;
301 else
302 return DATETIME_SetFormatW (infoPtr, 0);
307 static void
308 DATETIME_ReturnTxt (const DATETIME_INFO *infoPtr, int count, LPWSTR result, int resultSize)
310 static const WCHAR fmt_dW[] = { '%', 'd', 0 };
311 static const WCHAR fmt__2dW[] = { '%', '.', '2', 'd', 0 };
312 static const WCHAR fmt__3sW[] = { '%', '.', '3', 's', 0 };
313 SYSTEMTIME date = infoPtr->date;
314 int spec;
315 WCHAR buffer[80];
317 *result=0;
318 TRACE ("%d,%d\n", infoPtr->nrFields, count);
319 if (count>infoPtr->nrFields || count < 0) {
320 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
321 return;
324 if (!infoPtr->fieldspec) return;
326 spec = infoPtr->fieldspec[count];
327 if (spec & DT_STRING) {
328 int txtlen = infoPtr->buflen[count];
330 if (txtlen > resultSize)
331 txtlen = resultSize - 1;
332 memcpy (result, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
333 result[txtlen] = 0;
334 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
335 return;
339 switch (spec) {
340 case DT_END_FORMAT:
341 *result = 0;
342 break;
343 case ONEDIGITDAY:
344 wsprintfW (result, fmt_dW, date.wDay);
345 break;
346 case TWODIGITDAY:
347 wsprintfW (result, fmt__2dW, date.wDay);
348 break;
349 case THREECHARDAY:
350 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1+(date.wDayOfWeek+6)%7, result, 4);
351 /*wsprintfW (result,"%.3s",days[date.wDayOfWeek]);*/
352 break;
353 case FULLDAY:
354 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME1+(date.wDayOfWeek+6)%7, result, resultSize);
355 break;
356 case ONEDIGIT12HOUR:
357 if (date.wHour == 0) {
358 result[0] = '1';
359 result[1] = '2';
360 result[2] = 0;
362 else
363 wsprintfW (result, fmt_dW, date.wHour - (date.wHour > 12 ? 12 : 0));
364 break;
365 case TWODIGIT12HOUR:
366 if (date.wHour == 0) {
367 result[0] = '1';
368 result[1] = '2';
369 result[2] = 0;
371 else
372 wsprintfW (result, fmt__2dW, date.wHour - (date.wHour > 12 ? 12 : 0));
373 break;
374 case ONEDIGIT24HOUR:
375 wsprintfW (result, fmt_dW, date.wHour);
376 break;
377 case TWODIGIT24HOUR:
378 wsprintfW (result, fmt__2dW, date.wHour);
379 break;
380 case ONEDIGITSECOND:
381 wsprintfW (result, fmt_dW, date.wSecond);
382 break;
383 case TWODIGITSECOND:
384 wsprintfW (result, fmt__2dW, date.wSecond);
385 break;
386 case ONEDIGITMINUTE:
387 wsprintfW (result, fmt_dW, date.wMinute);
388 break;
389 case TWODIGITMINUTE:
390 wsprintfW (result, fmt__2dW, date.wMinute);
391 break;
392 case ONEDIGITMONTH:
393 wsprintfW (result, fmt_dW, date.wMonth);
394 break;
395 case TWODIGITMONTH:
396 wsprintfW (result, fmt__2dW, date.wMonth);
397 break;
398 case THREECHARMONTH:
399 GetLocaleInfoW(GetSystemDefaultLCID(), LOCALE_SMONTHNAME1+date.wMonth -1,
400 buffer, sizeof(buffer)/sizeof(buffer[0]));
401 wsprintfW (result, fmt__3sW, buffer);
402 break;
403 case FULLMONTH:
404 GetLocaleInfoW(GetSystemDefaultLCID(),LOCALE_SMONTHNAME1+date.wMonth -1,
405 result, resultSize);
406 break;
407 case ONELETTERAMPM:
408 result[0] = (date.wHour < 12 ? 'A' : 'P');
409 result[1] = 0;
410 break;
411 case TWOLETTERAMPM:
412 result[0] = (date.wHour < 12 ? 'A' : 'P');
413 result[1] = 'M';
414 result[2] = 0;
415 break;
416 case FORMATCALLBACK:
417 FIXME ("Not implemented\n");
418 result[0] = 'x';
419 result[1] = 0;
420 break;
421 case ONEDIGITYEAR:
422 wsprintfW (result, fmt_dW, date.wYear-10* (int) floor(date.wYear/10));
423 break;
424 case TWODIGITYEAR:
425 wsprintfW (result, fmt__2dW, date.wYear-100* (int) floor(date.wYear/100));
426 break;
427 case INVALIDFULLYEAR:
428 case FULLYEAR:
429 wsprintfW (result, fmt_dW, date.wYear);
430 break;
433 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
436 /* Offsets of days in the week to the weekday of january 1 in a leap year. */
437 static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
439 /* returns the day in the week(0 == sunday, 6 == saturday) */
440 /* day(1 == 1st, 2 == 2nd... etc), year is the year value */
441 static int DATETIME_CalculateDayOfWeek(DWORD day, DWORD month, DWORD year)
443 year-=(month < 3);
445 return((year + year/4 - year/100 + year/400 +
446 DayOfWeekTable[month-1] + day ) % 7);
449 static int wrap(int val, int delta, int minVal, int maxVal)
451 val += delta;
452 if (delta == INT_MIN || val < minVal) return maxVal;
453 if (delta == INT_MAX || val > maxVal) return minVal;
454 return val;
457 static void
458 DATETIME_IncreaseField (DATETIME_INFO *infoPtr, int number, int delta)
460 SYSTEMTIME *date = &infoPtr->date;
462 TRACE ("%d\n", number);
463 if ((number > infoPtr->nrFields) || (number < 0)) return;
465 if ((infoPtr->fieldspec[number] & DTHT_DATEFIELD) == 0) return;
467 switch (infoPtr->fieldspec[number]) {
468 case ONEDIGITYEAR:
469 case TWODIGITYEAR:
470 case FULLYEAR:
471 date->wYear = wrap(date->wYear, delta, 1752, 9999);
472 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(date->wDay,date->wMonth,date->wYear);
473 break;
474 case ONEDIGITMONTH:
475 case TWODIGITMONTH:
476 case THREECHARMONTH:
477 case FULLMONTH:
478 date->wMonth = wrap(date->wMonth, delta, 1, 12);
479 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(date->wDay,date->wMonth,date->wYear);
480 delta = 0;
481 /* fall through */
482 case ONEDIGITDAY:
483 case TWODIGITDAY:
484 case THREECHARDAY:
485 case FULLDAY:
486 date->wDay = wrap(date->wDay, delta, 1, MONTHCAL_MonthLength(date->wMonth, date->wYear));
487 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(date->wDay,date->wMonth,date->wYear);
488 break;
489 case ONELETTERAMPM:
490 case TWOLETTERAMPM:
491 delta *= 12;
492 /* fall through */
493 case ONEDIGIT12HOUR:
494 case TWODIGIT12HOUR:
495 case ONEDIGIT24HOUR:
496 case TWODIGIT24HOUR:
497 date->wHour = wrap(date->wHour, delta, 0, 23);
498 break;
499 case ONEDIGITMINUTE:
500 case TWODIGITMINUTE:
501 date->wMinute = wrap(date->wMinute, delta, 0, 59);
502 break;
503 case ONEDIGITSECOND:
504 case TWODIGITSECOND:
505 date->wSecond = wrap(date->wSecond, delta, 0, 59);
506 break;
507 case FORMATCALLBACK:
508 FIXME ("Not implemented\n");
509 break;
512 /* FYI: On 1752/9/14 the calendar changed and England and the
513 * American colonies changed to the Gregorian calendar. This change
514 * involved having September 14th follow September 2nd. So no date
515 * algorithm works before that date.
517 if (10000 * date->wYear + 100 * date->wMonth + date->wDay < 17520914) {
518 date->wYear = 1752;
519 date->wMonth = 9;
520 date->wDay = 14;
521 date->wSecond = 0;
522 date->wMinute = 0;
523 date->wHour = 0;
528 static void
529 DATETIME_ReturnFieldWidth (const DATETIME_INFO *infoPtr, HDC hdc, int count, SHORT *fieldWidthPtr)
531 /* fields are a fixed width, determined by the largest possible string */
532 /* presumably, these widths should be language dependent */
533 static const WCHAR fld_d1W[] = { '2', 0 };
534 static const WCHAR fld_d2W[] = { '2', '2', 0 };
535 static const WCHAR fld_d4W[] = { '2', '2', '2', '2', 0 };
536 static const WCHAR fld_am1[] = { 'A', 0 };
537 static const WCHAR fld_am2[] = { 'A', 'M', 0 };
538 static const WCHAR fld_day[] = { 'W', 'e', 'd', 'n', 'e', 's', 'd', 'a', 'y', 0 };
539 static const WCHAR fld_day3[] = { 'W', 'e', 'd', 0 };
540 static const WCHAR fld_mon[] = { 'S', 'e', 'p', 't', 'e', 'm', 'b', 'e', 'r', 0 };
541 static const WCHAR fld_mon3[] = { 'D', 'e', 'c', 0 };
542 int spec;
543 WCHAR buffer[80];
544 LPCWSTR bufptr;
545 SIZE size;
547 TRACE ("%d,%d\n", infoPtr->nrFields, count);
548 if (count>infoPtr->nrFields || count < 0) {
549 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
550 return;
553 if (!infoPtr->fieldspec) return;
555 spec = infoPtr->fieldspec[count];
556 if (spec & DT_STRING) {
557 int txtlen = infoPtr->buflen[count];
559 if (txtlen > 79)
560 txtlen = 79;
561 memcpy (buffer, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
562 buffer[txtlen] = 0;
563 bufptr = buffer;
565 else {
566 switch (spec) {
567 case ONEDIGITDAY:
568 case ONEDIGIT12HOUR:
569 case ONEDIGIT24HOUR:
570 case ONEDIGITSECOND:
571 case ONEDIGITMINUTE:
572 case ONEDIGITMONTH:
573 case ONEDIGITYEAR:
574 /* these seem to use a two byte field */
575 case TWODIGITDAY:
576 case TWODIGIT12HOUR:
577 case TWODIGIT24HOUR:
578 case TWODIGITSECOND:
579 case TWODIGITMINUTE:
580 case TWODIGITMONTH:
581 case TWODIGITYEAR:
582 bufptr = fld_d2W;
583 break;
584 case INVALIDFULLYEAR:
585 case FULLYEAR:
586 bufptr = fld_d4W;
587 break;
588 case THREECHARDAY:
589 bufptr = fld_day3;
590 break;
591 case FULLDAY:
592 bufptr = fld_day;
593 break;
594 case THREECHARMONTH:
595 bufptr = fld_mon3;
596 break;
597 case FULLMONTH:
598 bufptr = fld_mon;
599 break;
600 case ONELETTERAMPM:
601 bufptr = fld_am1;
602 break;
603 case TWOLETTERAMPM:
604 bufptr = fld_am2;
605 break;
606 default:
607 bufptr = fld_d1W;
608 break;
611 GetTextExtentPoint32W (hdc, bufptr, strlenW(bufptr), &size);
612 *fieldWidthPtr = size.cx;
615 static void
616 DATETIME_Refresh (DATETIME_INFO *infoPtr, HDC hdc)
618 int i,prevright;
619 RECT *field;
620 RECT *rcDraw = &infoPtr->rcDraw;
621 RECT *calbutton = &infoPtr->calbutton;
622 RECT *checkbox = &infoPtr->checkbox;
623 SIZE size;
624 COLORREF oldTextColor;
625 SHORT fieldWidth = 0;
627 /* draw control edge */
628 TRACE("\n");
630 if (infoPtr->dateValid) {
631 HFONT oldFont = SelectObject (hdc, infoPtr->hFont);
632 INT oldBkMode = SetBkMode (hdc, TRANSPARENT);
633 WCHAR txt[80];
635 DATETIME_ReturnTxt (infoPtr, 0, txt, sizeof(txt)/sizeof(txt[0]));
636 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
637 rcDraw->bottom = size.cy + 2;
639 prevright = checkbox->right = ((infoPtr->dwStyle & DTS_SHOWNONE) ? 18 : 2);
641 for (i = 0; i < infoPtr->nrFields; i++) {
642 DATETIME_ReturnTxt (infoPtr, i, txt, sizeof(txt)/sizeof(txt[0]));
643 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
644 DATETIME_ReturnFieldWidth (infoPtr, hdc, i, &fieldWidth);
645 field = &infoPtr->fieldRect[i];
646 field->left = prevright;
647 field->right = prevright + fieldWidth;
648 field->top = rcDraw->top;
649 field->bottom = rcDraw->bottom;
650 prevright = field->right;
652 if (infoPtr->dwStyle & WS_DISABLED)
653 oldTextColor = SetTextColor (hdc, comctl32_color.clrGrayText);
654 else if ((infoPtr->haveFocus) && (i == infoPtr->select)) {
655 /* fill if focussed */
656 HBRUSH hbr = CreateSolidBrush (comctl32_color.clrActiveCaption);
657 FillRect(hdc, field, hbr);
658 DeleteObject (hbr);
659 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindow);
661 else
662 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindowText);
664 /* draw the date text using the colour set above */
665 DrawTextW (hdc, txt, strlenW(txt), field, DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
666 SetTextColor (hdc, oldTextColor);
668 SetBkMode (hdc, oldBkMode);
669 SelectObject (hdc, oldFont);
672 if (!(infoPtr->dwStyle & DTS_UPDOWN)) {
673 DrawFrameControl(hdc, calbutton, DFC_SCROLL,
674 DFCS_SCROLLDOWN | (infoPtr->bCalDepressed ? DFCS_PUSHED : 0) |
675 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
680 static INT
681 DATETIME_HitTest (const DATETIME_INFO *infoPtr, POINT pt)
683 int i;
685 TRACE ("%d, %d\n", pt.x, pt.y);
687 if (PtInRect (&infoPtr->calbutton, pt)) return DTHT_MCPOPUP;
688 if (PtInRect (&infoPtr->checkbox, pt)) return DTHT_CHECKBOX;
690 for (i=0; i < infoPtr->nrFields; i++) {
691 if (PtInRect (&infoPtr->fieldRect[i], pt)) return i;
694 return DTHT_NONE;
698 static LRESULT
699 DATETIME_LButtonDown (DATETIME_INFO *infoPtr, WORD wKey, INT x, INT y)
701 POINT pt;
702 int old, new;
704 pt.x = x;
705 pt.y = y;
706 old = infoPtr->select;
707 new = DATETIME_HitTest (infoPtr, pt);
709 /* FIXME: might be conditions where we don't want to update infoPtr->select */
710 infoPtr->select = new;
712 SetFocus(infoPtr->hwndSelf);
714 if (infoPtr->select == DTHT_MCPOPUP) {
715 RECT rcMonthCal;
716 SendMessageW(infoPtr->hMonthCal, MCM_GETMINREQRECT, 0, (LPARAM)&rcMonthCal);
718 /* FIXME: button actually is only depressed during dropdown of the */
719 /* calendar control and when the mouse is over the button window */
720 infoPtr->bCalDepressed = TRUE;
722 /* recalculate the position of the monthcal popup */
723 if(infoPtr->dwStyle & DTS_RIGHTALIGN)
724 infoPtr->monthcal_pos.x = infoPtr->calbutton.left -
725 (rcMonthCal.right - rcMonthCal.left);
726 else
727 /* FIXME: this should be after the area reserved for the checkbox */
728 infoPtr->monthcal_pos.x = infoPtr->rcDraw.left;
730 infoPtr->monthcal_pos.y = infoPtr->rcClient.bottom;
731 ClientToScreen (infoPtr->hwndSelf, &(infoPtr->monthcal_pos));
732 SetWindowPos(infoPtr->hMonthCal, 0, infoPtr->monthcal_pos.x,
733 infoPtr->monthcal_pos.y, rcMonthCal.right - rcMonthCal.left,
734 rcMonthCal.bottom - rcMonthCal.top, 0);
736 if(IsWindowVisible(infoPtr->hMonthCal)) {
737 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
738 } else {
739 const SYSTEMTIME *lprgSysTimeArray = &infoPtr->date;
740 TRACE("update calendar %04d/%02d/%02d\n",
741 lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay);
742 SendMessageW(infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
744 if (infoPtr->bDropdownEnabled)
745 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
746 infoPtr->bDropdownEnabled = TRUE;
749 TRACE ("dt:%p mc:%p mc parent:%p, desktop:%p\n",
750 infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hwndNotify, GetDesktopWindow ());
751 DATETIME_SendSimpleNotify (infoPtr, DTN_DROPDOWN);
754 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
756 return 0;
760 static LRESULT
761 DATETIME_LButtonUp (DATETIME_INFO *infoPtr, WORD wKey)
763 if(infoPtr->bCalDepressed) {
764 infoPtr->bCalDepressed = FALSE;
765 InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
768 return 0;
772 static LRESULT
773 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
775 if (!hdc) {
776 PAINTSTRUCT ps;
777 hdc = BeginPaint (infoPtr->hwndSelf, &ps);
778 DATETIME_Refresh (infoPtr, hdc);
779 EndPaint (infoPtr->hwndSelf, &ps);
780 } else {
781 DATETIME_Refresh (infoPtr, hdc);
784 /* Not a click on the dropdown box, enabled it */
785 infoPtr->bDropdownEnabled = TRUE;
787 return 0;
791 static LRESULT
792 DATETIME_Button_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
794 if( HIWORD(wParam) == BN_CLICKED) {
795 DWORD state = SendMessageW((HWND)lParam, BM_GETCHECK, 0, 0);
796 infoPtr->dateValid = (state == BST_CHECKED);
797 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
799 return 0;
804 static LRESULT
805 DATETIME_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
807 TRACE("hwndbutton = %p\n", infoPtr->hwndCheckbut);
808 if(infoPtr->hwndCheckbut == (HWND)lParam)
809 return DATETIME_Button_Command(infoPtr, wParam, lParam);
810 return 0;
814 static LRESULT
815 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
817 TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
818 if (bEnable)
819 infoPtr->dwStyle &= ~WS_DISABLED;
820 else
821 infoPtr->dwStyle |= WS_DISABLED;
823 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
825 return 0;
829 static LRESULT
830 DATETIME_EraseBackground (const DATETIME_INFO *infoPtr, HDC hdc)
832 HBRUSH hBrush, hSolidBrush = NULL;
833 RECT rc;
835 if (infoPtr->dwStyle & WS_DISABLED)
836 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
837 else
839 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
840 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
841 if (!hBrush)
842 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
845 GetClientRect (infoPtr->hwndSelf, &rc);
847 FillRect (hdc, &rc, hBrush);
849 if (hSolidBrush)
850 DeleteObject(hSolidBrush);
852 return -1;
856 static LRESULT
857 DATETIME_Notify (DATETIME_INFO *infoPtr, int idCtrl, LPNMHDR lpnmh)
859 TRACE ("Got notification %x from %p\n", lpnmh->code, lpnmh->hwndFrom);
860 TRACE ("info: %p %p %p\n", infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hUpdown);
862 if (lpnmh->code == MCN_SELECT) {
863 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
864 infoPtr->dateValid = TRUE;
865 SendMessageW (infoPtr->hMonthCal, MCM_GETCURSEL, 0, (LPARAM)&infoPtr->date);
866 TRACE("got from calendar %04d/%02d/%02d day of week %d\n",
867 infoPtr->date.wYear, infoPtr->date.wMonth, infoPtr->date.wDay, infoPtr->date.wDayOfWeek);
868 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
869 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
870 DATETIME_SendDateTimeChangeNotify (infoPtr);
872 if ((lpnmh->hwndFrom == infoPtr->hUpdown) && (lpnmh->code == UDN_DELTAPOS)) {
873 LPNMUPDOWN lpnmud = (LPNMUPDOWN)lpnmh;
874 TRACE("Delta pos %d\n", lpnmud->iDelta);
875 infoPtr->pendingUpdown = lpnmud->iDelta;
877 return 0;
881 static LRESULT
882 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode, LPARAM flags)
884 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
885 int wrap = 0;
887 if (!(infoPtr->haveFocus)) return 0;
888 if ((fieldNum==0) && (infoPtr->select)) return 0;
890 if (infoPtr->select & FORMATCALLMASK) {
891 FIXME ("Callbacks not implemented yet\n");
894 if (vkCode >= '0' && vkCode <= '9') {
895 /* this is a somewhat simplified version of what Windows does */
896 SYSTEMTIME *date = &infoPtr->date;
897 switch (infoPtr->fieldspec[fieldNum]) {
898 case ONEDIGITYEAR:
899 case TWODIGITYEAR:
900 date->wYear = date->wYear - (date->wYear%100) +
901 (date->wYear%10)*10 + (vkCode-'0');
902 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
903 date->wDay,date->wMonth,date->wYear);
904 DATETIME_SendDateTimeChangeNotify (infoPtr);
905 break;
906 case INVALIDFULLYEAR:
907 case FULLYEAR:
908 date->wYear = (date->wYear%1000)*10 + (vkCode-'0');
909 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
910 date->wDay,date->wMonth,date->wYear);
911 DATETIME_SendDateTimeChangeNotify (infoPtr);
912 break;
913 case ONEDIGITMONTH:
914 case TWODIGITMONTH:
915 if ((date->wMonth%10) > 1 || (vkCode-'0') > 2)
916 date->wMonth = vkCode-'0';
917 else
918 date->wMonth = (date->wMonth%10)*10+vkCode-'0';
919 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
920 date->wDay,date->wMonth,date->wYear);
921 DATETIME_SendDateTimeChangeNotify (infoPtr);
922 break;
923 case ONEDIGITDAY:
924 case TWODIGITDAY:
925 /* probably better checking here would help */
926 if ((date->wDay%10) >= 3 && (vkCode-'0') > 1)
927 date->wDay = vkCode-'0';
928 else
929 date->wDay = (date->wDay%10)*10+vkCode-'0';
930 date->wDayOfWeek = DATETIME_CalculateDayOfWeek(
931 date->wDay,date->wMonth,date->wYear);
932 DATETIME_SendDateTimeChangeNotify (infoPtr);
933 break;
934 case ONEDIGIT12HOUR:
935 case TWODIGIT12HOUR:
936 if ((date->wHour%10) > 1 || (vkCode-'0') > 2)
937 date->wHour = vkCode-'0';
938 else
939 date->wHour = (date->wHour%10)*10+vkCode-'0';
940 DATETIME_SendDateTimeChangeNotify (infoPtr);
941 break;
942 case ONEDIGIT24HOUR:
943 case TWODIGIT24HOUR:
944 if ((date->wHour%10) > 2)
945 date->wHour = vkCode-'0';
946 else if ((date->wHour%10) == 2 && (vkCode-'0') > 3)
947 date->wHour = vkCode-'0';
948 else
949 date->wHour = (date->wHour%10)*10+vkCode-'0';
950 DATETIME_SendDateTimeChangeNotify (infoPtr);
951 break;
952 case ONEDIGITMINUTE:
953 case TWODIGITMINUTE:
954 if ((date->wMinute%10) > 5)
955 date->wMinute = vkCode-'0';
956 else
957 date->wMinute = (date->wMinute%10)*10+vkCode-'0';
958 DATETIME_SendDateTimeChangeNotify (infoPtr);
959 break;
960 case ONEDIGITSECOND:
961 case TWODIGITSECOND:
962 if ((date->wSecond%10) > 5)
963 date->wSecond = vkCode-'0';
964 else
965 date->wSecond = (date->wSecond%10)*10+vkCode-'0';
966 DATETIME_SendDateTimeChangeNotify (infoPtr);
967 break;
971 switch (vkCode) {
972 case VK_ADD:
973 case VK_UP:
974 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
975 DATETIME_SendDateTimeChangeNotify (infoPtr);
976 break;
977 case VK_SUBTRACT:
978 case VK_DOWN:
979 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
980 DATETIME_SendDateTimeChangeNotify (infoPtr);
981 break;
982 case VK_HOME:
983 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MIN);
984 DATETIME_SendDateTimeChangeNotify (infoPtr);
985 break;
986 case VK_END:
987 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MAX);
988 DATETIME_SendDateTimeChangeNotify (infoPtr);
989 break;
990 case VK_LEFT:
991 do {
992 if (infoPtr->select == 0) {
993 infoPtr->select = infoPtr->nrFields - 1;
994 wrap++;
995 } else {
996 infoPtr->select--;
998 } while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
999 break;
1000 case VK_RIGHT:
1001 do {
1002 infoPtr->select++;
1003 if (infoPtr->select==infoPtr->nrFields) {
1004 infoPtr->select = 0;
1005 wrap++;
1007 } while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
1008 break;
1011 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1013 return 0;
1017 static LRESULT
1018 DATETIME_VScroll (DATETIME_INFO *infoPtr, WORD wScroll)
1020 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1022 if ((SHORT)LOWORD(wScroll) != SB_THUMBPOSITION) return 0;
1023 if (!(infoPtr->haveFocus)) return 0;
1024 if ((fieldNum==0) && (infoPtr->select)) return 0;
1026 if (infoPtr->pendingUpdown >= 0) {
1027 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1028 DATETIME_SendDateTimeChangeNotify (infoPtr);
1030 else {
1031 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1032 DATETIME_SendDateTimeChangeNotify (infoPtr);
1035 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1037 return 0;
1041 static LRESULT
1042 DATETIME_KillFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1044 TRACE("lost focus to %p\n", lostFocus);
1046 if (infoPtr->haveFocus) {
1047 DATETIME_SendSimpleNotify (infoPtr, NM_KILLFOCUS);
1048 infoPtr->haveFocus = 0;
1051 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
1053 return 0;
1057 static LRESULT
1058 DATETIME_NCCreate (HWND hwnd, const CREATESTRUCTW *lpcs)
1060 DWORD dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1061 /* force control to have client edge */
1062 dwExStyle |= WS_EX_CLIENTEDGE;
1063 SetWindowLongW(hwnd, GWL_EXSTYLE, dwExStyle);
1065 return DefWindowProcW(hwnd, WM_NCCREATE, 0, (LPARAM)lpcs);
1069 static LRESULT
1070 DATETIME_SetFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1072 TRACE("got focus from %p\n", lostFocus);
1074 /* if monthcal is open and it loses focus, close monthcal */
1075 if (infoPtr->hMonthCal && (lostFocus == infoPtr->hMonthCal) &&
1076 IsWindowVisible(infoPtr->hMonthCal))
1078 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1079 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1080 /* note: this get triggered even if monthcal loses focus to a dropdown
1081 * box click, which occurs without an intermediate WM_PAINT call
1083 infoPtr->bDropdownEnabled = FALSE;
1084 return 0;
1087 if (infoPtr->haveFocus == 0) {
1088 DATETIME_SendSimpleNotify (infoPtr, NM_SETFOCUS);
1089 infoPtr->haveFocus = DTHT_GOTFOCUS;
1092 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1094 return 0;
1098 static BOOL
1099 DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr)
1101 NMDATETIMECHANGE dtdtc;
1103 dtdtc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1104 dtdtc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1105 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1107 dtdtc.dwFlags = (infoPtr->dwStyle & DTS_SHOWNONE) ? GDT_NONE : GDT_VALID;
1109 MONTHCAL_CopyTime (&infoPtr->date, &dtdtc.st);
1110 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1111 dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1115 static BOOL
1116 DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code)
1118 NMHDR nmhdr;
1120 TRACE("%x\n", code);
1121 nmhdr.hwndFrom = infoPtr->hwndSelf;
1122 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1123 nmhdr.code = code;
1125 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1126 nmhdr.idFrom, (LPARAM)&nmhdr);
1129 static LRESULT
1130 DATETIME_Size (DATETIME_INFO *infoPtr, WORD flags, INT width, INT height)
1132 /* set size */
1133 infoPtr->rcClient.bottom = height;
1134 infoPtr->rcClient.right = width;
1136 TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1138 infoPtr->rcDraw = infoPtr->rcClient;
1140 if (infoPtr->dwStyle & DTS_UPDOWN) {
1141 SetWindowPos(infoPtr->hUpdown, NULL,
1142 infoPtr->rcClient.right-14, 0,
1143 15, infoPtr->rcClient.bottom - infoPtr->rcClient.top,
1144 SWP_NOACTIVATE | SWP_NOZORDER);
1146 else {
1147 /* set the size of the button that drops the calendar down */
1148 /* FIXME: account for style that allows button on left side */
1149 infoPtr->calbutton.top = infoPtr->rcDraw.top;
1150 infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1151 infoPtr->calbutton.left = infoPtr->rcDraw.right-15;
1152 infoPtr->calbutton.right = infoPtr->rcDraw.right;
1155 /* set enable/disable button size for show none style being enabled */
1156 /* FIXME: these dimensions are completely incorrect */
1157 infoPtr->checkbox.top = infoPtr->rcDraw.top;
1158 infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1159 infoPtr->checkbox.left = infoPtr->rcDraw.left;
1160 infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1162 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1164 return 0;
1168 static LRESULT
1169 DATETIME_StyleChanged(DATETIME_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
1171 static const WCHAR buttonW[] = { 'b', 'u', 't', 't', 'o', 'n', 0 };
1173 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1174 wStyleType, lpss->styleOld, lpss->styleNew);
1176 if (wStyleType != GWL_STYLE) return 0;
1178 infoPtr->dwStyle = lpss->styleNew;
1180 if ( !(lpss->styleOld & DTS_SHOWNONE) && (lpss->styleNew & DTS_SHOWNONE) ) {
1181 infoPtr->hwndCheckbut = CreateWindowExW (0, buttonW, 0, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1182 2, 2, 13, 13, infoPtr->hwndSelf, 0,
1183 (HINSTANCE)GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_HINSTANCE), 0);
1184 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, 1, 0);
1186 if ( (lpss->styleOld & DTS_SHOWNONE) && !(lpss->styleNew & DTS_SHOWNONE) ) {
1187 DestroyWindow(infoPtr->hwndCheckbut);
1188 infoPtr->hwndCheckbut = 0;
1190 if ( !(lpss->styleOld & DTS_UPDOWN) && (lpss->styleNew & DTS_UPDOWN) ) {
1191 infoPtr->hUpdown = CreateUpDownControl (WS_CHILD | WS_BORDER | WS_VISIBLE, 120, 1, 20, 20,
1192 infoPtr->hwndSelf, 1, 0, 0, UD_MAXVAL, UD_MINVAL, 0);
1194 if ( (lpss->styleOld & DTS_UPDOWN) && !(lpss->styleNew & DTS_UPDOWN) ) {
1195 DestroyWindow(infoPtr->hUpdown);
1196 infoPtr->hUpdown = 0;
1199 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1200 return 0;
1204 static LRESULT
1205 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1207 infoPtr->hFont = font;
1208 if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1209 return 0;
1213 static LRESULT
1214 DATETIME_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1216 static const WCHAR SysMonthCal32W[] = { 'S', 'y', 's', 'M', 'o', 'n', 't', 'h', 'C', 'a', 'l', '3', '2', 0 };
1217 DATETIME_INFO *infoPtr = (DATETIME_INFO *)Alloc (sizeof(DATETIME_INFO));
1218 STYLESTRUCT ss = { 0, lpcs->style };
1220 if (!infoPtr) return -1;
1222 infoPtr->hwndSelf = hwnd;
1223 infoPtr->dwStyle = lpcs->style;
1225 infoPtr->nrFieldsAllocated = 32;
1226 infoPtr->fieldspec = (int *) Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1227 infoPtr->fieldRect = (RECT *) Alloc (infoPtr->nrFieldsAllocated * sizeof(RECT));
1228 infoPtr->buflen = (int *) Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1229 infoPtr->hwndNotify = lpcs->hwndParent;
1230 infoPtr->select = -1; /* initially, nothing is selected */
1231 infoPtr->bDropdownEnabled = TRUE;
1233 DATETIME_StyleChanged(infoPtr, GWL_STYLE, &ss);
1234 DATETIME_SetFormatW (infoPtr, 0);
1236 /* create the monthcal control */
1237 infoPtr->hMonthCal = CreateWindowExW (0, SysMonthCal32W, 0, WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS,
1238 0, 0, 0, 0, infoPtr->hwndSelf, 0, 0, 0);
1240 /* initialize info structure */
1241 GetLocalTime (&infoPtr->date);
1242 infoPtr->dateValid = TRUE;
1243 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1245 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1247 return 0;
1252 static LRESULT
1253 DATETIME_Destroy (DATETIME_INFO *infoPtr)
1255 if (infoPtr->hwndCheckbut)
1256 DestroyWindow(infoPtr->hwndCheckbut);
1257 if (infoPtr->hUpdown)
1258 DestroyWindow(infoPtr->hUpdown);
1259 if (infoPtr->hMonthCal)
1260 DestroyWindow(infoPtr->hMonthCal);
1261 SetWindowLongPtrW( infoPtr->hwndSelf, 0, 0 ); /* clear infoPtr */
1262 Free (infoPtr);
1263 return 0;
1267 static LRESULT WINAPI
1268 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1270 DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1271 LRESULT ret;
1273 TRACE ("%x, %lx, %lx\n", uMsg, wParam, lParam);
1275 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
1276 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
1278 switch (uMsg) {
1280 case DTM_GETSYSTEMTIME:
1281 return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1283 case DTM_SETSYSTEMTIME:
1284 return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1286 case DTM_GETRANGE:
1287 ret = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1288 return ret ? ret : 1; /* bug emulation */
1290 case DTM_SETRANGE:
1291 return SendMessageW (infoPtr->hMonthCal, MCM_SETRANGE, wParam, lParam);
1293 case DTM_SETFORMATA:
1294 return DATETIME_SetFormatA (infoPtr, (LPCSTR)lParam);
1296 case DTM_SETFORMATW:
1297 return DATETIME_SetFormatW (infoPtr, (LPCWSTR)lParam);
1299 case DTM_GETMONTHCAL:
1300 return (LRESULT)infoPtr->hMonthCal;
1302 case DTM_SETMCCOLOR:
1303 return SendMessageW (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
1305 case DTM_GETMCCOLOR:
1306 return SendMessageW (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
1308 case DTM_SETMCFONT:
1309 return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1311 case DTM_GETMCFONT:
1312 return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1314 case WM_NOTIFY:
1315 return DATETIME_Notify (infoPtr, (int)wParam, (LPNMHDR)lParam);
1317 case WM_ENABLE:
1318 return DATETIME_Enable (infoPtr, (BOOL)wParam);
1320 case WM_ERASEBKGND:
1321 return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1323 case WM_GETDLGCODE:
1324 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1326 case WM_PRINTCLIENT:
1327 case WM_PAINT:
1328 return DATETIME_Paint (infoPtr, (HDC)wParam);
1330 case WM_KEYDOWN:
1331 return DATETIME_KeyDown (infoPtr, wParam, lParam);
1333 case WM_KILLFOCUS:
1334 return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1336 case WM_NCCREATE:
1337 return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1339 case WM_SETFOCUS:
1340 return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1342 case WM_SIZE:
1343 return DATETIME_Size (infoPtr, wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1345 case WM_LBUTTONDOWN:
1346 return DATETIME_LButtonDown (infoPtr, (WORD)wParam, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1348 case WM_LBUTTONUP:
1349 return DATETIME_LButtonUp (infoPtr, (WORD)wParam);
1351 case WM_VSCROLL:
1352 return DATETIME_VScroll (infoPtr, (WORD)wParam);
1354 case WM_CREATE:
1355 return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1357 case WM_DESTROY:
1358 return DATETIME_Destroy (infoPtr);
1360 case WM_COMMAND:
1361 return DATETIME_Command (infoPtr, wParam, lParam);
1363 case WM_STYLECHANGED:
1364 return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1366 case WM_SETFONT:
1367 return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1369 case WM_GETFONT:
1370 return (LRESULT) infoPtr->hFont;
1372 default:
1373 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
1374 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
1375 uMsg, wParam, lParam);
1376 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1381 void
1382 DATETIME_Register (void)
1384 WNDCLASSW wndClass;
1386 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1387 wndClass.style = CS_GLOBALCLASS;
1388 wndClass.lpfnWndProc = DATETIME_WindowProc;
1389 wndClass.cbClsExtra = 0;
1390 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1391 wndClass.hCursor = LoadCursorW (0, (LPCWSTR)IDC_ARROW);
1392 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1393 wndClass.lpszClassName = DATETIMEPICK_CLASSW;
1395 RegisterClassW (&wndClass);
1399 void
1400 DATETIME_Unregister (void)
1402 UnregisterClassW (DATETIMEPICK_CLASSW, NULL);