Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / comctl32 / datetime.c
blobc240d4fde40532ace7a1124527dbe2f6bc9ad212
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_FORMAT
35 * -- DTN_FORMATQUERY
36 * -- DTN_USERSTRING
37 * -- DTN_WMKEYDOWN
38 * -- FORMATCALLBACK
41 #include <math.h>
42 #include <string.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <limits.h>
47 #include "windef.h"
48 #include "winbase.h"
49 #include "wingdi.h"
50 #include "winuser.h"
51 #include "winnls.h"
52 #include "commctrl.h"
53 #include "comctl32.h"
54 #include "wine/debug.h"
55 #include "wine/unicode.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(datetime);
59 typedef struct
61 HWND hwndSelf;
62 HWND hMonthCal;
63 HWND hwndNotify;
64 HWND hUpdown;
65 DWORD dwStyle;
66 SYSTEMTIME date;
67 BOOL dateValid;
68 HWND hwndCheckbut;
69 RECT rcClient; /* rect around the edge of the window */
70 RECT rcDraw; /* rect inside of the border */
71 RECT checkbox; /* checkbox allowing the control to be enabled/disabled */
72 RECT calbutton; /* button that toggles the dropdown of the monthcal control */
73 BOOL bCalDepressed; /* TRUE = cal button is depressed */
74 int bDropdownEnabled;
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);
90 extern int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace);
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 0x1000
129 #define DTHT_CHECKBOX 0x2000 /* these should end at '00' , to make */
130 #define DTHT_MCPOPUP 0x3000 /* & DTHT_DATEFIELD 0 when DATETIME_KeyDown */
131 #define DTHT_GOTFOCUS 0x4000 /* tests for date-fields */
132 #define DTHT_NODATEMASK 0xf000 /* to mask check and drop down from others */
134 static BOOL DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code);
135 static BOOL DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr);
136 extern void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to);
137 static const WCHAR allowedformatchars[] = {'d', 'h', 'H', 'm', 'M', 's', 't', 'y', 'X', 0};
138 static const int maxrepetition [] = {4,2,2,2,4,2,2,4,-1};
141 static DWORD
142 DATETIME_GetSystemTime (const DATETIME_INFO *infoPtr, SYSTEMTIME *systime)
144 if (!systime) return GDT_NONE;
146 if ((infoPtr->dwStyle & DTS_SHOWNONE) &&
147 (SendMessageW (infoPtr->hwndCheckbut, BM_GETCHECK, 0, 0) == BST_UNCHECKED))
148 return GDT_NONE;
150 *systime = infoPtr->date;
152 return GDT_VALID;
156 static BOOL
157 DATETIME_SetSystemTime (DATETIME_INFO *infoPtr, DWORD flag, const SYSTEMTIME *systime)
159 if (!systime) return 0;
161 TRACE("%04d/%02d/%02d %02d:%02d:%02d\n",
162 systime->wYear, systime->wMonth, systime->wDay,
163 systime->wHour, systime->wMinute, systime->wSecond);
165 if (flag == GDT_VALID) {
166 if (systime->wYear < 1601 || systime->wYear > 30827 ||
167 systime->wMonth < 1 || systime->wMonth > 12 ||
168 systime->wDay < 1 || systime->wDay > 31 ||
169 systime->wHour > 23 ||
170 systime->wMinute > 59 ||
171 systime->wSecond > 59 ||
172 systime->wMilliseconds > 999
174 return FALSE;
176 infoPtr->dateValid = TRUE;
177 infoPtr->date = *systime;
178 /* always store a valid day of week */
179 MONTHCAL_CalculateDayOfWeek(&infoPtr->date, TRUE);
181 SendMessageW (infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
182 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
183 } else if ((infoPtr->dwStyle & DTS_SHOWNONE) && (flag == GDT_NONE)) {
184 infoPtr->dateValid = FALSE;
185 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_UNCHECKED, 0);
187 else
188 return FALSE;
190 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
191 return TRUE;
195 /***
196 * Split up a formattxt in actions.
197 * See ms documentation for the meaning of the letter codes/'specifiers'.
199 * Notes:
200 * *'dddddd' is handled as 'dddd' plus 'dd'.
201 * *unrecognized formats are strings (here given the type DT_STRING;
202 * start of the string is encoded in lower bits of DT_STRING.
203 * Therefore, 'string' ends finally up as '<show seconds>tring'.
206 static void
207 DATETIME_UseFormat (DATETIME_INFO *infoPtr, LPCWSTR formattxt)
209 unsigned int i;
210 int j, k, len;
211 BOOL inside_literal = FALSE; /* inside '...' */
212 int *nrFields = &infoPtr->nrFields;
214 *nrFields = 0;
215 infoPtr->fieldspec[*nrFields] = 0;
216 len = strlenW(allowedformatchars);
217 k = 0;
219 for (i = 0; formattxt[i]; i++) {
220 TRACE ("\n%d %c:", i, formattxt[i]);
221 if (!inside_literal) {
222 for (j = 0; j < len; j++) {
223 if (allowedformatchars[j]==formattxt[i]) {
224 TRACE ("%c[%d,%x]", allowedformatchars[j], *nrFields, infoPtr->fieldspec[*nrFields]);
225 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
226 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
227 break;
229 if (infoPtr->fieldspec[*nrFields] >> 4 != j) {
230 (*nrFields)++;
231 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
232 break;
234 if ((infoPtr->fieldspec[*nrFields] & 0x0f) == maxrepetition[j]) {
235 (*nrFields)++;
236 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
237 break;
239 infoPtr->fieldspec[*nrFields]++;
240 break;
241 } /* if allowedformatchar */
242 } /* for j */
244 else
245 j = len;
247 if (formattxt[i] == '\'')
249 inside_literal = !inside_literal;
250 continue;
253 /* char is not a specifier: handle char like a string */
254 if (j == len) {
255 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
256 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
257 infoPtr->buflen[*nrFields] = 0;
258 } else if ((infoPtr->fieldspec[*nrFields] & DT_STRING) != DT_STRING) {
259 (*nrFields)++;
260 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
261 infoPtr->buflen[*nrFields] = 0;
263 infoPtr->textbuf[k] = formattxt[i];
264 k++;
265 infoPtr->buflen[*nrFields]++;
266 } /* if j=len */
268 if (*nrFields == infoPtr->nrFieldsAllocated) {
269 FIXME ("out of memory; should reallocate. crash ahead.\n");
271 } /* for i */
273 TRACE("\n");
275 if (infoPtr->fieldspec[*nrFields] != 0) (*nrFields)++;
279 static BOOL
280 DATETIME_SetFormatW (DATETIME_INFO *infoPtr, LPCWSTR lpszFormat)
282 if (!lpszFormat) {
283 WCHAR format_buf[80];
284 DWORD format_item;
286 if (infoPtr->dwStyle & DTS_LONGDATEFORMAT)
287 format_item = LOCALE_SLONGDATE;
288 else if ((infoPtr->dwStyle & DTS_TIMEFORMAT) == DTS_TIMEFORMAT)
289 format_item = LOCALE_STIMEFORMAT;
290 else /* DTS_SHORTDATEFORMAT */
291 format_item = LOCALE_SSHORTDATE;
292 GetLocaleInfoW( GetSystemDefaultLCID(), format_item, format_buf, sizeof(format_buf)/sizeof(format_buf[0]));
293 lpszFormat = format_buf;
296 DATETIME_UseFormat (infoPtr, lpszFormat);
297 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
299 return 1;
303 static BOOL
304 DATETIME_SetFormatA (DATETIME_INFO *infoPtr, LPCSTR lpszFormat)
306 if (lpszFormat) {
307 BOOL retval;
308 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, NULL, 0);
309 LPWSTR wstr = Alloc(len * sizeof(WCHAR));
310 if (wstr) MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, wstr, len);
311 retval = DATETIME_SetFormatW (infoPtr, wstr);
312 Free (wstr);
313 return retval;
315 else
316 return DATETIME_SetFormatW (infoPtr, 0);
321 static void
322 DATETIME_ReturnTxt (const DATETIME_INFO *infoPtr, int count, LPWSTR result, int resultSize)
324 static const WCHAR fmt_dW[] = { '%', 'd', 0 };
325 static const WCHAR fmt__2dW[] = { '%', '.', '2', 'd', 0 };
326 static const WCHAR fmt__3sW[] = { '%', '.', '3', 's', 0 };
327 SYSTEMTIME date = infoPtr->date;
328 int spec;
329 WCHAR buffer[80];
331 *result=0;
332 TRACE ("%d,%d\n", infoPtr->nrFields, count);
333 if (count>infoPtr->nrFields || count < 0) {
334 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
335 return;
338 if (!infoPtr->fieldspec) return;
340 spec = infoPtr->fieldspec[count];
341 if (spec & DT_STRING) {
342 int txtlen = infoPtr->buflen[count];
344 if (txtlen > resultSize)
345 txtlen = resultSize - 1;
346 memcpy (result, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
347 result[txtlen] = 0;
348 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
349 return;
353 switch (spec) {
354 case DT_END_FORMAT:
355 *result = 0;
356 break;
357 case ONEDIGITDAY:
358 wsprintfW (result, fmt_dW, date.wDay);
359 break;
360 case TWODIGITDAY:
361 wsprintfW (result, fmt__2dW, date.wDay);
362 break;
363 case THREECHARDAY:
364 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1+(date.wDayOfWeek+6)%7, result, 4);
365 /*wsprintfW (result,"%.3s",days[date.wDayOfWeek]);*/
366 break;
367 case FULLDAY:
368 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME1+(date.wDayOfWeek+6)%7, result, resultSize);
369 break;
370 case ONEDIGIT12HOUR:
371 if (date.wHour == 0) {
372 result[0] = '1';
373 result[1] = '2';
374 result[2] = 0;
376 else
377 wsprintfW (result, fmt_dW, date.wHour - (date.wHour > 12 ? 12 : 0));
378 break;
379 case TWODIGIT12HOUR:
380 if (date.wHour == 0) {
381 result[0] = '1';
382 result[1] = '2';
383 result[2] = 0;
385 else
386 wsprintfW (result, fmt__2dW, date.wHour - (date.wHour > 12 ? 12 : 0));
387 break;
388 case ONEDIGIT24HOUR:
389 wsprintfW (result, fmt_dW, date.wHour);
390 break;
391 case TWODIGIT24HOUR:
392 wsprintfW (result, fmt__2dW, date.wHour);
393 break;
394 case ONEDIGITSECOND:
395 wsprintfW (result, fmt_dW, date.wSecond);
396 break;
397 case TWODIGITSECOND:
398 wsprintfW (result, fmt__2dW, date.wSecond);
399 break;
400 case ONEDIGITMINUTE:
401 wsprintfW (result, fmt_dW, date.wMinute);
402 break;
403 case TWODIGITMINUTE:
404 wsprintfW (result, fmt__2dW, date.wMinute);
405 break;
406 case ONEDIGITMONTH:
407 wsprintfW (result, fmt_dW, date.wMonth);
408 break;
409 case TWODIGITMONTH:
410 wsprintfW (result, fmt__2dW, date.wMonth);
411 break;
412 case THREECHARMONTH:
413 GetLocaleInfoW(GetSystemDefaultLCID(), LOCALE_SMONTHNAME1+date.wMonth -1,
414 buffer, sizeof(buffer)/sizeof(buffer[0]));
415 wsprintfW (result, fmt__3sW, buffer);
416 break;
417 case FULLMONTH:
418 GetLocaleInfoW(GetSystemDefaultLCID(),LOCALE_SMONTHNAME1+date.wMonth -1,
419 result, resultSize);
420 break;
421 case ONELETTERAMPM:
422 result[0] = (date.wHour < 12 ? 'A' : 'P');
423 result[1] = 0;
424 break;
425 case TWOLETTERAMPM:
426 result[0] = (date.wHour < 12 ? 'A' : 'P');
427 result[1] = 'M';
428 result[2] = 0;
429 break;
430 case FORMATCALLBACK:
431 FIXME ("Not implemented\n");
432 result[0] = 'x';
433 result[1] = 0;
434 break;
435 case ONEDIGITYEAR:
436 wsprintfW (result, fmt_dW, date.wYear-10* (int) floor(date.wYear/10));
437 break;
438 case TWODIGITYEAR:
439 wsprintfW (result, fmt__2dW, date.wYear-100* (int) floor(date.wYear/100));
440 break;
441 case INVALIDFULLYEAR:
442 case FULLYEAR:
443 wsprintfW (result, fmt_dW, date.wYear);
444 break;
447 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
450 static int wrap(int val, int delta, int minVal, int maxVal)
452 val += delta;
453 if (delta == INT_MIN || val < minVal) return maxVal;
454 if (delta == INT_MAX || val > maxVal) return minVal;
455 return val;
458 static void
459 DATETIME_IncreaseField (DATETIME_INFO *infoPtr, int number, int delta)
461 SYSTEMTIME *date = &infoPtr->date;
463 TRACE ("%d\n", number);
464 if ((number > infoPtr->nrFields) || (number < 0)) return;
466 if ((infoPtr->fieldspec[number] & DTHT_DATEFIELD) == 0) return;
468 switch (infoPtr->fieldspec[number]) {
469 case ONEDIGITYEAR:
470 case TWODIGITYEAR:
471 case FULLYEAR:
472 date->wYear = wrap(date->wYear, delta, 1752, 9999);
473 MONTHCAL_CalculateDayOfWeek(date, TRUE);
474 break;
475 case ONEDIGITMONTH:
476 case TWODIGITMONTH:
477 case THREECHARMONTH:
478 case FULLMONTH:
479 date->wMonth = wrap(date->wMonth, delta, 1, 12);
480 MONTHCAL_CalculateDayOfWeek(date, TRUE);
481 delta = 0;
482 /* fall through */
483 case ONEDIGITDAY:
484 case TWODIGITDAY:
485 case THREECHARDAY:
486 case FULLDAY:
487 date->wDay = wrap(date->wDay, delta, 1, MONTHCAL_MonthLength(date->wMonth, date->wYear));
488 MONTHCAL_CalculateDayOfWeek(date, TRUE);
489 break;
490 case ONELETTERAMPM:
491 case TWOLETTERAMPM:
492 delta *= 12;
493 /* fall through */
494 case ONEDIGIT12HOUR:
495 case TWODIGIT12HOUR:
496 case ONEDIGIT24HOUR:
497 case TWODIGIT24HOUR:
498 date->wHour = wrap(date->wHour, delta, 0, 23);
499 break;
500 case ONEDIGITMINUTE:
501 case TWODIGITMINUTE:
502 date->wMinute = wrap(date->wMinute, delta, 0, 59);
503 break;
504 case ONEDIGITSECOND:
505 case TWODIGITSECOND:
506 date->wSecond = wrap(date->wSecond, delta, 0, 59);
507 break;
508 case FORMATCALLBACK:
509 FIXME ("Not implemented\n");
510 break;
513 /* FYI: On 1752/9/14 the calendar changed and England and the
514 * American colonies changed to the Gregorian calendar. This change
515 * involved having September 14th follow September 2nd. So no date
516 * algorithm works before that date.
518 if (10000 * date->wYear + 100 * date->wMonth + date->wDay < 17520914) {
519 date->wYear = 1752;
520 date->wMonth = 9;
521 date->wDay = 14;
522 date->wSecond = 0;
523 date->wMinute = 0;
524 date->wHour = 0;
529 static void
530 DATETIME_ReturnFieldWidth (const DATETIME_INFO *infoPtr, HDC hdc, int count, SHORT *width)
532 /* fields are a fixed width, determined by the largest possible string */
533 /* presumably, these widths should be language dependent */
534 static const WCHAR fld_d1W[] = { '2', 0 };
535 static const WCHAR fld_d2W[] = { '2', '2', 0 };
536 static const WCHAR fld_d4W[] = { '2', '2', '2', '2', 0 };
537 static const WCHAR fld_am1[] = { 'A', 0 };
538 static const WCHAR fld_am2[] = { 'A', 'M', 0 };
539 int spec;
540 WCHAR buffer[80];
541 LPCWSTR bufptr;
542 SIZE size;
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);
547 return;
550 if (!infoPtr->fieldspec) return;
552 spec = infoPtr->fieldspec[count];
553 if (spec & DT_STRING) {
554 int txtlen = infoPtr->buflen[count];
556 if (txtlen > 79)
557 txtlen = 79;
558 memcpy (buffer, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
559 buffer[txtlen] = 0;
560 bufptr = buffer;
562 else {
563 switch (spec) {
564 case ONEDIGITDAY:
565 case ONEDIGIT12HOUR:
566 case ONEDIGIT24HOUR:
567 case ONEDIGITSECOND:
568 case ONEDIGITMINUTE:
569 case ONEDIGITMONTH:
570 case ONEDIGITYEAR:
571 /* these seem to use a two byte field */
572 case TWODIGITDAY:
573 case TWODIGIT12HOUR:
574 case TWODIGIT24HOUR:
575 case TWODIGITSECOND:
576 case TWODIGITMINUTE:
577 case TWODIGITMONTH:
578 case TWODIGITYEAR:
579 bufptr = fld_d2W;
580 break;
581 case INVALIDFULLYEAR:
582 case FULLYEAR:
583 bufptr = fld_d4W;
584 break;
585 case THREECHARMONTH:
586 case FULLMONTH:
587 case THREECHARDAY:
588 case FULLDAY:
590 static const WCHAR fld_day[] = {'W','e','d','n','e','s','d','a','y',0};
591 static const WCHAR fld_abbrday[] = {'W','e','d',0};
592 static const WCHAR fld_mon[] = {'S','e','p','t','e','m','b','e','r',0};
593 static const WCHAR fld_abbrmon[] = {'D','e','c',0};
595 const WCHAR *fall;
596 LCTYPE lctype;
597 INT i, max_count;
598 LONG cx;
600 /* choose locale data type and fallback string */
601 switch (spec) {
602 case THREECHARDAY:
603 fall = fld_abbrday;
604 lctype = LOCALE_SABBREVDAYNAME1;
605 max_count = 7;
606 break;
607 case FULLDAY:
608 fall = fld_day;
609 lctype = LOCALE_SDAYNAME1;
610 max_count = 7;
611 break;
612 case THREECHARMONTH:
613 fall = fld_abbrmon;
614 lctype = LOCALE_SABBREVMONTHNAME1;
615 max_count = 12;
616 break;
617 case FULLMONTH:
618 fall = fld_mon;
619 lctype = LOCALE_SMONTHNAME1;
620 max_count = 12;
621 break;
624 cx = 0;
625 for (i = 0; i < max_count; i++)
627 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, lctype + i,
628 buffer, lstrlenW(buffer)))
630 GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &size);
631 if (size.cx > cx) cx = size.cx;
633 else /* locale independent fallback on failure */
635 GetTextExtentPoint32W(hdc, fall, lstrlenW(fall), &size);
636 cx = size.cx;
637 break;
640 *width = cx;
641 return;
643 case ONELETTERAMPM:
644 bufptr = fld_am1;
645 break;
646 case TWOLETTERAMPM:
647 bufptr = fld_am2;
648 break;
649 default:
650 bufptr = fld_d1W;
651 break;
654 GetTextExtentPoint32W (hdc, bufptr, strlenW(bufptr), &size);
655 *width = size.cx;
658 static void
659 DATETIME_Refresh (DATETIME_INFO *infoPtr, HDC hdc)
661 TRACE("\n");
663 if (infoPtr->dateValid) {
664 int i, prevright;
665 RECT *field;
666 RECT *rcDraw = &infoPtr->rcDraw;
667 SIZE size;
668 COLORREF oldTextColor;
669 SHORT fieldWidth = 0;
670 HFONT oldFont = SelectObject (hdc, infoPtr->hFont);
671 INT oldBkMode = SetBkMode (hdc, TRANSPARENT);
672 WCHAR txt[80];
674 DATETIME_ReturnTxt (infoPtr, 0, txt, sizeof(txt)/sizeof(txt[0]));
675 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
676 rcDraw->bottom = size.cy + 2;
678 prevright = infoPtr->checkbox.right = ((infoPtr->dwStyle & DTS_SHOWNONE) ? 18 : 2);
680 for (i = 0; i < infoPtr->nrFields; i++) {
681 DATETIME_ReturnTxt (infoPtr, i, txt, sizeof(txt)/sizeof(txt[0]));
682 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
683 DATETIME_ReturnFieldWidth (infoPtr, hdc, i, &fieldWidth);
684 field = &infoPtr->fieldRect[i];
685 field->left = prevright;
686 field->right = prevright + fieldWidth;
687 field->top = rcDraw->top;
688 field->bottom = rcDraw->bottom;
689 prevright = field->right;
691 if (infoPtr->dwStyle & WS_DISABLED)
692 oldTextColor = SetTextColor (hdc, comctl32_color.clrGrayText);
693 else if ((infoPtr->haveFocus) && (i == infoPtr->select)) {
694 RECT selection;
696 /* fill if focused */
697 HBRUSH hbr = CreateSolidBrush (comctl32_color.clrActiveCaption);
699 selection.left = 0;
700 selection.top = 0;
701 selection.right = size.cx;
702 selection.bottom = size.cy;
703 /* center rectangle */
704 OffsetRect(&selection, (field->right + field->left - size.cx)/2,
705 (field->bottom - size.cy)/2);
707 FillRect(hdc, &selection, hbr);
708 DeleteObject (hbr);
709 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindow);
711 else
712 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindowText);
714 /* draw the date text using the colour set above */
715 DrawTextW (hdc, txt, strlenW(txt), field, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
716 SetTextColor (hdc, oldTextColor);
718 SetBkMode (hdc, oldBkMode);
719 SelectObject (hdc, oldFont);
722 if (!(infoPtr->dwStyle & DTS_UPDOWN)) {
723 DrawFrameControl(hdc, &infoPtr->calbutton, DFC_SCROLL,
724 DFCS_SCROLLDOWN | (infoPtr->bCalDepressed ? DFCS_PUSHED : 0) |
725 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
730 static INT
731 DATETIME_HitTest (const DATETIME_INFO *infoPtr, POINT pt)
733 int i;
735 TRACE ("%d, %d\n", pt.x, pt.y);
737 if (PtInRect (&infoPtr->calbutton, pt)) return DTHT_MCPOPUP;
738 if (PtInRect (&infoPtr->checkbox, pt)) return DTHT_CHECKBOX;
740 for (i = 0; i < infoPtr->nrFields; i++) {
741 if (PtInRect (&infoPtr->fieldRect[i], pt)) return i;
744 return DTHT_NONE;
747 /* Returns index of a closest date field from given counting to left
748 or -1 if there's no such fields at left */
749 static int DATETIME_GetPrevDateField(const DATETIME_INFO *infoPtr, int i)
751 for(--i; i >= 0; i--)
753 if (infoPtr->fieldspec[i] & DTHT_DATEFIELD) return i;
755 return -1;
758 static LRESULT
759 DATETIME_LButtonDown (DATETIME_INFO *infoPtr, INT x, INT y)
761 POINT pt;
762 int old, new;
764 pt.x = x;
765 pt.y = y;
766 old = infoPtr->select;
767 new = DATETIME_HitTest (infoPtr, pt);
769 SetFocus(infoPtr->hwndSelf);
771 if (!(new & DTHT_NODATEMASK) || (new == DTHT_NONE))
773 if (new == DTHT_NONE)
774 new = infoPtr->nrFields - 1;
775 else
777 /* hitting string part moves selection to next date field to left */
778 if (infoPtr->fieldspec[new] & DT_STRING)
780 new = DATETIME_GetPrevDateField(infoPtr, new);
781 if (new == -1) return 0;
783 /* never select full day of week */
784 if (infoPtr->fieldspec[new] == FULLDAY) return 0;
787 infoPtr->select = new;
789 if (infoPtr->select == DTHT_MCPOPUP) {
790 RECT rcMonthCal;
791 SendMessageW(infoPtr->hMonthCal, MCM_GETMINREQRECT, 0, (LPARAM)&rcMonthCal);
793 /* FIXME: button actually is only depressed during dropdown of the */
794 /* calendar control and when the mouse is over the button window */
795 infoPtr->bCalDepressed = TRUE;
797 /* recalculate the position of the monthcal popup */
798 if(infoPtr->dwStyle & DTS_RIGHTALIGN)
799 infoPtr->monthcal_pos.x = infoPtr->calbutton.left -
800 (rcMonthCal.right - rcMonthCal.left);
801 else
802 /* FIXME: this should be after the area reserved for the checkbox */
803 infoPtr->monthcal_pos.x = infoPtr->rcDraw.left;
805 infoPtr->monthcal_pos.y = infoPtr->rcClient.bottom;
806 ClientToScreen (infoPtr->hwndSelf, &(infoPtr->monthcal_pos));
807 SetWindowPos(infoPtr->hMonthCal, 0, infoPtr->monthcal_pos.x,
808 infoPtr->monthcal_pos.y, rcMonthCal.right - rcMonthCal.left,
809 rcMonthCal.bottom - rcMonthCal.top, 0);
811 if(IsWindowVisible(infoPtr->hMonthCal)) {
812 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
813 infoPtr->bDropdownEnabled = FALSE;
814 DATETIME_SendSimpleNotify (infoPtr, DTN_CLOSEUP);
815 } else {
816 const SYSTEMTIME *lprgSysTimeArray = &infoPtr->date;
817 TRACE("update calendar %04d/%02d/%02d\n",
818 lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay);
819 SendMessageW(infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
821 if (infoPtr->bDropdownEnabled) {
822 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
823 DATETIME_SendSimpleNotify (infoPtr, DTN_DROPDOWN);
825 infoPtr->bDropdownEnabled = TRUE;
828 TRACE ("dt:%p mc:%p mc parent:%p, desktop:%p\n",
829 infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hwndNotify, GetDesktopWindow ());
832 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
834 return 0;
838 static LRESULT
839 DATETIME_LButtonUp (DATETIME_INFO *infoPtr)
841 if(infoPtr->bCalDepressed) {
842 infoPtr->bCalDepressed = FALSE;
843 InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
846 return 0;
850 static LRESULT
851 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
853 if (!hdc) {
854 PAINTSTRUCT ps;
855 hdc = BeginPaint (infoPtr->hwndSelf, &ps);
856 DATETIME_Refresh (infoPtr, hdc);
857 EndPaint (infoPtr->hwndSelf, &ps);
858 } else {
859 DATETIME_Refresh (infoPtr, hdc);
862 /* Not a click on the dropdown box, enabled it */
863 infoPtr->bDropdownEnabled = TRUE;
865 return 0;
869 static LRESULT
870 DATETIME_Button_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
872 if( HIWORD(wParam) == BN_CLICKED) {
873 DWORD state = SendMessageW((HWND)lParam, BM_GETCHECK, 0, 0);
874 infoPtr->dateValid = (state == BST_CHECKED);
875 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
877 return 0;
882 static LRESULT
883 DATETIME_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
885 TRACE("hwndbutton = %p\n", infoPtr->hwndCheckbut);
886 if(infoPtr->hwndCheckbut == (HWND)lParam)
887 return DATETIME_Button_Command(infoPtr, wParam, lParam);
888 return 0;
892 static LRESULT
893 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
895 TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
896 if (bEnable)
897 infoPtr->dwStyle &= ~WS_DISABLED;
898 else
899 infoPtr->dwStyle |= WS_DISABLED;
901 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
903 return 0;
907 static LRESULT
908 DATETIME_EraseBackground (const DATETIME_INFO *infoPtr, HDC hdc)
910 HBRUSH hBrush, hSolidBrush = NULL;
911 RECT rc;
913 if (infoPtr->dwStyle & WS_DISABLED)
914 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
915 else
917 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
918 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
919 if (!hBrush)
920 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
923 GetClientRect (infoPtr->hwndSelf, &rc);
925 FillRect (hdc, &rc, hBrush);
927 if (hSolidBrush)
928 DeleteObject(hSolidBrush);
930 return -1;
934 static LRESULT
935 DATETIME_Notify (DATETIME_INFO *infoPtr, const NMHDR *lpnmh)
937 TRACE ("Got notification %x from %p\n", lpnmh->code, lpnmh->hwndFrom);
938 TRACE ("info: %p %p %p\n", infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hUpdown);
940 if (lpnmh->code == MCN_SELECT) {
941 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
942 infoPtr->dateValid = TRUE;
943 SendMessageW (infoPtr->hMonthCal, MCM_GETCURSEL, 0, (LPARAM)&infoPtr->date);
944 TRACE("got from calendar %04d/%02d/%02d day of week %d\n",
945 infoPtr->date.wYear, infoPtr->date.wMonth, infoPtr->date.wDay, infoPtr->date.wDayOfWeek);
946 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
947 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
948 DATETIME_SendDateTimeChangeNotify (infoPtr);
949 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
951 if ((lpnmh->hwndFrom == infoPtr->hUpdown) && (lpnmh->code == UDN_DELTAPOS)) {
952 LPNMUPDOWN lpnmud = (LPNMUPDOWN)lpnmh;
953 TRACE("Delta pos %d\n", lpnmud->iDelta);
954 infoPtr->pendingUpdown = lpnmud->iDelta;
956 return 0;
960 static LRESULT
961 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode)
963 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
964 int wrap = 0;
966 if (!(infoPtr->haveFocus)) return 0;
967 if ((fieldNum==0) && (infoPtr->select)) return 0;
969 if (infoPtr->select & FORMATCALLMASK) {
970 FIXME ("Callbacks not implemented yet\n");
973 switch (vkCode) {
974 case VK_ADD:
975 case VK_UP:
976 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
977 DATETIME_SendDateTimeChangeNotify (infoPtr);
978 break;
979 case VK_SUBTRACT:
980 case VK_DOWN:
981 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
982 DATETIME_SendDateTimeChangeNotify (infoPtr);
983 break;
984 case VK_HOME:
985 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MIN);
986 DATETIME_SendDateTimeChangeNotify (infoPtr);
987 break;
988 case VK_END:
989 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MAX);
990 DATETIME_SendDateTimeChangeNotify (infoPtr);
991 break;
992 case VK_LEFT:
993 do {
994 if (infoPtr->select == 0) {
995 infoPtr->select = infoPtr->nrFields - 1;
996 wrap++;
997 } else {
998 infoPtr->select--;
1000 } while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
1001 break;
1002 case VK_RIGHT:
1003 do {
1004 infoPtr->select++;
1005 if (infoPtr->select==infoPtr->nrFields) {
1006 infoPtr->select = 0;
1007 wrap++;
1009 } while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
1010 break;
1013 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1015 return 0;
1019 static LRESULT
1020 DATETIME_Char (DATETIME_INFO *infoPtr, WPARAM vkCode)
1022 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1024 if (vkCode >= '0' && vkCode <= '9') {
1025 int num = vkCode-'0';
1026 int newDays;
1028 /* this is a somewhat simplified version of what Windows does */
1029 SYSTEMTIME *date = &infoPtr->date;
1030 switch (infoPtr->fieldspec[fieldNum]) {
1031 case ONEDIGITYEAR:
1032 case TWODIGITYEAR:
1033 date->wYear = date->wYear - (date->wYear%100) +
1034 (date->wYear%10)*10 + num;
1035 MONTHCAL_CalculateDayOfWeek(date, TRUE);
1036 DATETIME_SendDateTimeChangeNotify (infoPtr);
1037 break;
1038 case INVALIDFULLYEAR:
1039 case FULLYEAR:
1040 /* reset current year initialy */
1041 date->wYear = ((date->wYear/1000) ? 0 : 1)*(date->wYear%1000)*10 + num;
1042 MONTHCAL_CalculateDayOfWeek(date, TRUE);
1043 DATETIME_SendDateTimeChangeNotify (infoPtr);
1044 break;
1045 case ONEDIGITMONTH:
1046 case TWODIGITMONTH:
1047 if ((date->wMonth%10) > 1 || num > 2)
1048 date->wMonth = num;
1049 else
1050 date->wMonth = (date->wMonth%10)*10+num;
1051 MONTHCAL_CalculateDayOfWeek(date, TRUE);
1052 DATETIME_SendDateTimeChangeNotify (infoPtr);
1053 break;
1054 case ONEDIGITDAY:
1055 case TWODIGITDAY:
1056 newDays = (date->wDay%10)*10+num;
1057 if (newDays > MONTHCAL_MonthLength(date->wMonth, date->wYear))
1058 date->wDay = num;
1059 else
1060 date->wDay = newDays;
1061 MONTHCAL_CalculateDayOfWeek(date, TRUE);
1062 DATETIME_SendDateTimeChangeNotify (infoPtr);
1063 break;
1064 case ONEDIGIT12HOUR:
1065 case TWODIGIT12HOUR:
1066 if ((date->wHour%10) > 1 || num > 2)
1067 date->wHour = num;
1068 else
1069 date->wHour = (date->wHour%10)*10+num;
1070 DATETIME_SendDateTimeChangeNotify (infoPtr);
1071 break;
1072 case ONEDIGIT24HOUR:
1073 case TWODIGIT24HOUR:
1074 if ((date->wHour%10) > 2)
1075 date->wHour = num;
1076 else if ((date->wHour%10) == 2 && num > 3)
1077 date->wHour = num;
1078 else
1079 date->wHour = (date->wHour%10)*10+num;
1080 DATETIME_SendDateTimeChangeNotify (infoPtr);
1081 break;
1082 case ONEDIGITMINUTE:
1083 case TWODIGITMINUTE:
1084 if ((date->wMinute%10) > 5)
1085 date->wMinute = num;
1086 else
1087 date->wMinute = (date->wMinute%10)*10+num;
1088 DATETIME_SendDateTimeChangeNotify (infoPtr);
1089 break;
1090 case ONEDIGITSECOND:
1091 case TWODIGITSECOND:
1092 if ((date->wSecond%10) > 5)
1093 date->wSecond = num;
1094 else
1095 date->wSecond = (date->wSecond%10)*10+num;
1096 DATETIME_SendDateTimeChangeNotify (infoPtr);
1097 break;
1100 return 0;
1104 static LRESULT
1105 DATETIME_VScroll (DATETIME_INFO *infoPtr, WORD wScroll)
1107 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1109 if ((SHORT)LOWORD(wScroll) != SB_THUMBPOSITION) return 0;
1110 if (!(infoPtr->haveFocus)) return 0;
1111 if ((fieldNum==0) && (infoPtr->select)) return 0;
1113 if (infoPtr->pendingUpdown >= 0) {
1114 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1115 DATETIME_SendDateTimeChangeNotify (infoPtr);
1117 else {
1118 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1119 DATETIME_SendDateTimeChangeNotify (infoPtr);
1122 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1124 return 0;
1128 static LRESULT
1129 DATETIME_KillFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1131 TRACE("lost focus to %p\n", lostFocus);
1133 if (infoPtr->haveFocus) {
1134 DATETIME_SendSimpleNotify (infoPtr, NM_KILLFOCUS);
1135 infoPtr->haveFocus = 0;
1138 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
1140 return 0;
1144 static LRESULT
1145 DATETIME_NCCreate (HWND hwnd, const CREATESTRUCTW *lpcs)
1147 DWORD dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1148 /* force control to have client edge */
1149 dwExStyle |= WS_EX_CLIENTEDGE;
1150 SetWindowLongW(hwnd, GWL_EXSTYLE, dwExStyle);
1152 return DefWindowProcW(hwnd, WM_NCCREATE, 0, (LPARAM)lpcs);
1156 static LRESULT
1157 DATETIME_SetFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1159 TRACE("got focus from %p\n", lostFocus);
1161 /* if monthcal is open and it loses focus, close monthcal */
1162 if (infoPtr->hMonthCal && (lostFocus == infoPtr->hMonthCal) &&
1163 IsWindowVisible(infoPtr->hMonthCal))
1165 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1166 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1167 /* note: this get triggered even if monthcal loses focus to a dropdown
1168 * box click, which occurs without an intermediate WM_PAINT call
1170 infoPtr->bDropdownEnabled = FALSE;
1171 return 0;
1174 if (infoPtr->haveFocus == 0) {
1175 DATETIME_SendSimpleNotify (infoPtr, NM_SETFOCUS);
1176 infoPtr->haveFocus = DTHT_GOTFOCUS;
1179 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1181 return 0;
1185 static BOOL
1186 DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr)
1188 NMDATETIMECHANGE dtdtc;
1190 dtdtc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1191 dtdtc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1192 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1194 dtdtc.dwFlags = infoPtr->dateValid ? GDT_VALID : GDT_NONE;
1196 dtdtc.st = infoPtr->date;
1197 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1198 dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1202 static BOOL
1203 DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code)
1205 NMHDR nmhdr;
1207 TRACE("%x\n", code);
1208 nmhdr.hwndFrom = infoPtr->hwndSelf;
1209 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1210 nmhdr.code = code;
1212 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1213 nmhdr.idFrom, (LPARAM)&nmhdr);
1216 static LRESULT
1217 DATETIME_Size (DATETIME_INFO *infoPtr, INT width, INT height)
1219 /* set size */
1220 infoPtr->rcClient.bottom = height;
1221 infoPtr->rcClient.right = width;
1223 TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1225 infoPtr->rcDraw = infoPtr->rcClient;
1227 if (infoPtr->dwStyle & DTS_UPDOWN) {
1228 SetWindowPos(infoPtr->hUpdown, NULL,
1229 infoPtr->rcClient.right-14, 0,
1230 15, infoPtr->rcClient.bottom - infoPtr->rcClient.top,
1231 SWP_NOACTIVATE | SWP_NOZORDER);
1233 else {
1234 /* set the size of the button that drops the calendar down */
1235 /* FIXME: account for style that allows button on left side */
1236 infoPtr->calbutton.top = infoPtr->rcDraw.top;
1237 infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1238 infoPtr->calbutton.left = infoPtr->rcDraw.right-15;
1239 infoPtr->calbutton.right = infoPtr->rcDraw.right;
1242 /* set enable/disable button size for show none style being enabled */
1243 /* FIXME: these dimensions are completely incorrect */
1244 infoPtr->checkbox.top = infoPtr->rcDraw.top;
1245 infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1246 infoPtr->checkbox.left = infoPtr->rcDraw.left;
1247 infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1249 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1251 return 0;
1254 static LRESULT
1255 DATETIME_StyleChanging(DATETIME_INFO *infoPtr, WPARAM wStyleType, STYLESTRUCT *lpss)
1257 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1258 wStyleType, lpss->styleOld, lpss->styleNew);
1260 /* block DTS_SHOWNONE change */
1261 if ((lpss->styleNew ^ lpss->styleOld) & DTS_SHOWNONE)
1263 if (lpss->styleOld & DTS_SHOWNONE)
1264 lpss->styleNew |= DTS_SHOWNONE;
1265 else
1266 lpss->styleNew &= ~DTS_SHOWNONE;
1269 return 0;
1272 static LRESULT
1273 DATETIME_StyleChanged(DATETIME_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
1275 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1276 wStyleType, lpss->styleOld, lpss->styleNew);
1278 if (wStyleType != GWL_STYLE) return 0;
1280 infoPtr->dwStyle = lpss->styleNew;
1282 if ( !(lpss->styleOld & DTS_SHOWNONE) && (lpss->styleNew & DTS_SHOWNONE) ) {
1283 infoPtr->hwndCheckbut = CreateWindowExW (0, WC_BUTTONW, 0, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1284 2, 2, 13, 13, infoPtr->hwndSelf, 0,
1285 (HINSTANCE)GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_HINSTANCE), 0);
1286 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, 1, 0);
1288 if ( (lpss->styleOld & DTS_SHOWNONE) && !(lpss->styleNew & DTS_SHOWNONE) ) {
1289 DestroyWindow(infoPtr->hwndCheckbut);
1290 infoPtr->hwndCheckbut = 0;
1292 if ( !(lpss->styleOld & DTS_UPDOWN) && (lpss->styleNew & DTS_UPDOWN) ) {
1293 infoPtr->hUpdown = CreateUpDownControl (WS_CHILD | WS_BORDER | WS_VISIBLE, 120, 1, 20, 20,
1294 infoPtr->hwndSelf, 1, 0, 0, UD_MAXVAL, UD_MINVAL, 0);
1296 if ( (lpss->styleOld & DTS_UPDOWN) && !(lpss->styleNew & DTS_UPDOWN) ) {
1297 DestroyWindow(infoPtr->hUpdown);
1298 infoPtr->hUpdown = 0;
1301 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1302 return 0;
1306 static LRESULT
1307 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1309 infoPtr->hFont = font;
1310 if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1311 return 0;
1315 static LRESULT
1316 DATETIME_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1318 DATETIME_INFO *infoPtr = Alloc (sizeof(DATETIME_INFO));
1319 STYLESTRUCT ss = { 0, lpcs->style };
1321 if (!infoPtr) return -1;
1323 infoPtr->hwndSelf = hwnd;
1324 infoPtr->dwStyle = lpcs->style;
1326 infoPtr->nrFieldsAllocated = 32;
1327 infoPtr->fieldspec = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1328 infoPtr->fieldRect = Alloc (infoPtr->nrFieldsAllocated * sizeof(RECT));
1329 infoPtr->buflen = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1330 infoPtr->hwndNotify = lpcs->hwndParent;
1331 infoPtr->select = -1; /* initially, nothing is selected */
1332 infoPtr->bDropdownEnabled = TRUE;
1334 DATETIME_StyleChanged(infoPtr, GWL_STYLE, &ss);
1335 DATETIME_SetFormatW (infoPtr, 0);
1337 /* create the monthcal control */
1338 infoPtr->hMonthCal = CreateWindowExW (0, MONTHCAL_CLASSW, 0, WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS,
1339 0, 0, 0, 0, infoPtr->hwndSelf, 0, 0, 0);
1341 /* initialize info structure */
1342 GetLocalTime (&infoPtr->date);
1343 infoPtr->dateValid = TRUE;
1344 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1346 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1348 return 0;
1353 static LRESULT
1354 DATETIME_Destroy (DATETIME_INFO *infoPtr)
1356 if (infoPtr->hwndCheckbut)
1357 DestroyWindow(infoPtr->hwndCheckbut);
1358 if (infoPtr->hUpdown)
1359 DestroyWindow(infoPtr->hUpdown);
1360 if (infoPtr->hMonthCal)
1361 DestroyWindow(infoPtr->hMonthCal);
1362 SetWindowLongPtrW( infoPtr->hwndSelf, 0, 0 ); /* clear infoPtr */
1363 Free (infoPtr->buflen);
1364 Free (infoPtr->fieldRect);
1365 Free (infoPtr->fieldspec);
1366 Free (infoPtr);
1367 return 0;
1371 static INT
1372 DATETIME_GetText (DATETIME_INFO *infoPtr, INT count, LPWSTR dst)
1374 WCHAR buf[80];
1375 int i;
1377 if (!dst || (count <= 0)) return 0;
1379 dst[0] = 0;
1380 for (i = 0; i < infoPtr->nrFields; i++)
1382 DATETIME_ReturnTxt(infoPtr, i, buf, sizeof(buf)/sizeof(buf[0]));
1383 if ((strlenW(dst) + strlenW(buf)) < count)
1384 strcatW(dst, buf);
1385 else break;
1387 return strlenW(dst);
1391 static LRESULT WINAPI
1392 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1394 DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1395 LRESULT ret;
1397 TRACE ("%x, %lx, %lx\n", uMsg, wParam, lParam);
1399 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
1400 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
1402 switch (uMsg) {
1404 case DTM_GETSYSTEMTIME:
1405 return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1407 case DTM_SETSYSTEMTIME:
1408 return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1410 case DTM_GETRANGE:
1411 ret = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1412 return ret ? ret : 1; /* bug emulation */
1414 case DTM_SETRANGE:
1415 return SendMessageW (infoPtr->hMonthCal, MCM_SETRANGE, wParam, lParam);
1417 case DTM_SETFORMATA:
1418 return DATETIME_SetFormatA (infoPtr, (LPCSTR)lParam);
1420 case DTM_SETFORMATW:
1421 return DATETIME_SetFormatW (infoPtr, (LPCWSTR)lParam);
1423 case DTM_GETMONTHCAL:
1424 return (LRESULT)infoPtr->hMonthCal;
1426 case DTM_SETMCCOLOR:
1427 return SendMessageW (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
1429 case DTM_GETMCCOLOR:
1430 return SendMessageW (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
1432 case DTM_SETMCFONT:
1433 return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1435 case DTM_GETMCFONT:
1436 return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1438 case WM_NOTIFY:
1439 return DATETIME_Notify (infoPtr, (LPNMHDR)lParam);
1441 case WM_ENABLE:
1442 return DATETIME_Enable (infoPtr, (BOOL)wParam);
1444 case WM_ERASEBKGND:
1445 return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1447 case WM_GETDLGCODE:
1448 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1450 case WM_PRINTCLIENT:
1451 case WM_PAINT:
1452 return DATETIME_Paint (infoPtr, (HDC)wParam);
1454 case WM_KEYDOWN:
1455 return DATETIME_KeyDown (infoPtr, wParam);
1457 case WM_CHAR:
1458 return DATETIME_Char (infoPtr, wParam);
1460 case WM_KILLFOCUS:
1461 return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1463 case WM_NCCREATE:
1464 return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1466 case WM_SETFOCUS:
1467 return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1469 case WM_SIZE:
1470 return DATETIME_Size (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1472 case WM_LBUTTONDOWN:
1473 return DATETIME_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1475 case WM_LBUTTONUP:
1476 return DATETIME_LButtonUp (infoPtr);
1478 case WM_VSCROLL:
1479 return DATETIME_VScroll (infoPtr, (WORD)wParam);
1481 case WM_CREATE:
1482 return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1484 case WM_DESTROY:
1485 return DATETIME_Destroy (infoPtr);
1487 case WM_COMMAND:
1488 return DATETIME_Command (infoPtr, wParam, lParam);
1490 case WM_STYLECHANGING:
1491 return DATETIME_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1493 case WM_STYLECHANGED:
1494 return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1496 case WM_SETFONT:
1497 return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1499 case WM_GETFONT:
1500 return (LRESULT) infoPtr->hFont;
1502 case WM_GETTEXT:
1503 return (LRESULT) DATETIME_GetText(infoPtr, wParam, (LPWSTR)lParam);
1505 case WM_SETTEXT:
1506 return CB_ERR;
1508 default:
1509 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
1510 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
1511 uMsg, wParam, lParam);
1512 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1517 void
1518 DATETIME_Register (void)
1520 WNDCLASSW wndClass;
1522 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1523 wndClass.style = CS_GLOBALCLASS;
1524 wndClass.lpfnWndProc = DATETIME_WindowProc;
1525 wndClass.cbClsExtra = 0;
1526 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1527 wndClass.hCursor = LoadCursorW (0, (LPCWSTR)IDC_ARROW);
1528 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1529 wndClass.lpszClassName = DATETIMEPICK_CLASSW;
1531 RegisterClassW (&wndClass);
1535 void
1536 DATETIME_Unregister (void)
1538 UnregisterClassW (DATETIMEPICK_CLASSW, NULL);