user32: Fix checking of show count in ShowCursor.
[wine/multimedia.git] / dlls / comctl32 / datetime.c
blob24189507d64b0d10ca1e2f8ad2d492b55f9991c6
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(LOCALE_USER_DEFAULT, 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(LOCALE_USER_DEFAULT, 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(LOCALE_USER_DEFAULT, 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 default: /* 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 new;
764 pt.x = x;
765 pt.y = y;
766 new = DATETIME_HitTest (infoPtr, pt);
768 SetFocus(infoPtr->hwndSelf);
770 if (!(new & DTHT_NODATEMASK) || (new == DTHT_NONE))
772 if (new == DTHT_NONE)
773 new = infoPtr->nrFields - 1;
774 else
776 /* hitting string part moves selection to next date field to left */
777 if (infoPtr->fieldspec[new] & DT_STRING)
779 new = DATETIME_GetPrevDateField(infoPtr, new);
780 if (new == -1) return 0;
782 /* never select full day of week */
783 if (infoPtr->fieldspec[new] == FULLDAY) return 0;
786 infoPtr->select = new;
788 if (infoPtr->select == DTHT_MCPOPUP) {
789 RECT rcMonthCal;
790 SendMessageW(infoPtr->hMonthCal, MCM_GETMINREQRECT, 0, (LPARAM)&rcMonthCal);
792 /* FIXME: button actually is only depressed during dropdown of the */
793 /* calendar control and when the mouse is over the button window */
794 infoPtr->bCalDepressed = TRUE;
796 /* recalculate the position of the monthcal popup */
797 if(infoPtr->dwStyle & DTS_RIGHTALIGN)
798 infoPtr->monthcal_pos.x = infoPtr->calbutton.left -
799 (rcMonthCal.right - rcMonthCal.left);
800 else
801 /* FIXME: this should be after the area reserved for the checkbox */
802 infoPtr->monthcal_pos.x = infoPtr->rcDraw.left;
804 infoPtr->monthcal_pos.y = infoPtr->rcClient.bottom;
805 ClientToScreen (infoPtr->hwndSelf, &(infoPtr->monthcal_pos));
806 SetWindowPos(infoPtr->hMonthCal, 0, infoPtr->monthcal_pos.x,
807 infoPtr->monthcal_pos.y, rcMonthCal.right - rcMonthCal.left,
808 rcMonthCal.bottom - rcMonthCal.top, 0);
810 if(IsWindowVisible(infoPtr->hMonthCal)) {
811 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
812 infoPtr->bDropdownEnabled = FALSE;
813 DATETIME_SendSimpleNotify (infoPtr, DTN_CLOSEUP);
814 } else {
815 const SYSTEMTIME *lprgSysTimeArray = &infoPtr->date;
816 TRACE("update calendar %04d/%02d/%02d\n",
817 lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay);
818 SendMessageW(infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
820 if (infoPtr->bDropdownEnabled) {
821 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
822 DATETIME_SendSimpleNotify (infoPtr, DTN_DROPDOWN);
824 infoPtr->bDropdownEnabled = TRUE;
827 TRACE ("dt:%p mc:%p mc parent:%p, desktop:%p\n",
828 infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hwndNotify, GetDesktopWindow ());
831 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
833 return 0;
837 static LRESULT
838 DATETIME_LButtonUp (DATETIME_INFO *infoPtr)
840 if(infoPtr->bCalDepressed) {
841 infoPtr->bCalDepressed = FALSE;
842 InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
845 return 0;
849 static LRESULT
850 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
852 if (!hdc) {
853 PAINTSTRUCT ps;
854 hdc = BeginPaint (infoPtr->hwndSelf, &ps);
855 DATETIME_Refresh (infoPtr, hdc);
856 EndPaint (infoPtr->hwndSelf, &ps);
857 } else {
858 DATETIME_Refresh (infoPtr, hdc);
861 /* Not a click on the dropdown box, enabled it */
862 infoPtr->bDropdownEnabled = TRUE;
864 return 0;
868 static LRESULT
869 DATETIME_Button_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
871 if( HIWORD(wParam) == BN_CLICKED) {
872 DWORD state = SendMessageW((HWND)lParam, BM_GETCHECK, 0, 0);
873 infoPtr->dateValid = (state == BST_CHECKED);
874 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
876 return 0;
881 static LRESULT
882 DATETIME_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
884 TRACE("hwndbutton = %p\n", infoPtr->hwndCheckbut);
885 if(infoPtr->hwndCheckbut == (HWND)lParam)
886 return DATETIME_Button_Command(infoPtr, wParam, lParam);
887 return 0;
891 static LRESULT
892 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
894 TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
895 if (bEnable)
896 infoPtr->dwStyle &= ~WS_DISABLED;
897 else
898 infoPtr->dwStyle |= WS_DISABLED;
900 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
902 return 0;
906 static LRESULT
907 DATETIME_EraseBackground (const DATETIME_INFO *infoPtr, HDC hdc)
909 HBRUSH hBrush, hSolidBrush = NULL;
910 RECT rc;
912 if (infoPtr->dwStyle & WS_DISABLED)
913 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
914 else
916 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
917 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
918 if (!hBrush)
919 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
922 GetClientRect (infoPtr->hwndSelf, &rc);
924 FillRect (hdc, &rc, hBrush);
926 if (hSolidBrush)
927 DeleteObject(hSolidBrush);
929 return -1;
933 static LRESULT
934 DATETIME_Notify (DATETIME_INFO *infoPtr, const NMHDR *lpnmh)
936 TRACE ("Got notification %x from %p\n", lpnmh->code, lpnmh->hwndFrom);
937 TRACE ("info: %p %p %p\n", infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hUpdown);
939 if (lpnmh->code == MCN_SELECT) {
940 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
941 infoPtr->dateValid = TRUE;
942 SendMessageW (infoPtr->hMonthCal, MCM_GETCURSEL, 0, (LPARAM)&infoPtr->date);
943 TRACE("got from calendar %04d/%02d/%02d day of week %d\n",
944 infoPtr->date.wYear, infoPtr->date.wMonth, infoPtr->date.wDay, infoPtr->date.wDayOfWeek);
945 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
946 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
947 DATETIME_SendDateTimeChangeNotify (infoPtr);
948 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
950 if ((lpnmh->hwndFrom == infoPtr->hUpdown) && (lpnmh->code == UDN_DELTAPOS)) {
951 const NM_UPDOWN *lpnmud = (const NM_UPDOWN*)lpnmh;
952 TRACE("Delta pos %d\n", lpnmud->iDelta);
953 infoPtr->pendingUpdown = lpnmud->iDelta;
955 return 0;
959 static LRESULT
960 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode)
962 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
963 int wrap = 0;
965 if (!(infoPtr->haveFocus)) return 0;
966 if ((fieldNum==0) && (infoPtr->select)) return 0;
968 if (infoPtr->select & FORMATCALLMASK) {
969 FIXME ("Callbacks not implemented yet\n");
972 switch (vkCode) {
973 case VK_ADD:
974 case VK_UP:
975 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
976 DATETIME_SendDateTimeChangeNotify (infoPtr);
977 break;
978 case VK_SUBTRACT:
979 case VK_DOWN:
980 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
981 DATETIME_SendDateTimeChangeNotify (infoPtr);
982 break;
983 case VK_HOME:
984 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MIN);
985 DATETIME_SendDateTimeChangeNotify (infoPtr);
986 break;
987 case VK_END:
988 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MAX);
989 DATETIME_SendDateTimeChangeNotify (infoPtr);
990 break;
991 case VK_LEFT:
992 do {
993 if (infoPtr->select == 0) {
994 infoPtr->select = infoPtr->nrFields - 1;
995 wrap++;
996 } else {
997 infoPtr->select--;
999 } while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
1000 break;
1001 case VK_RIGHT:
1002 do {
1003 infoPtr->select++;
1004 if (infoPtr->select==infoPtr->nrFields) {
1005 infoPtr->select = 0;
1006 wrap++;
1008 } while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
1009 break;
1012 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1014 return 0;
1018 static LRESULT
1019 DATETIME_Char (DATETIME_INFO *infoPtr, WPARAM vkCode)
1021 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1023 if (vkCode >= '0' && vkCode <= '9') {
1024 int num = vkCode-'0';
1025 int newDays;
1027 /* this is a somewhat simplified version of what Windows does */
1028 SYSTEMTIME *date = &infoPtr->date;
1029 switch (infoPtr->fieldspec[fieldNum]) {
1030 case ONEDIGITYEAR:
1031 case TWODIGITYEAR:
1032 date->wYear = date->wYear - (date->wYear%100) +
1033 (date->wYear%10)*10 + num;
1034 MONTHCAL_CalculateDayOfWeek(date, TRUE);
1035 DATETIME_SendDateTimeChangeNotify (infoPtr);
1036 break;
1037 case INVALIDFULLYEAR:
1038 case FULLYEAR:
1039 /* reset current year initialy */
1040 date->wYear = ((date->wYear/1000) ? 0 : 1)*(date->wYear%1000)*10 + num;
1041 MONTHCAL_CalculateDayOfWeek(date, TRUE);
1042 DATETIME_SendDateTimeChangeNotify (infoPtr);
1043 break;
1044 case ONEDIGITMONTH:
1045 case TWODIGITMONTH:
1046 if ((date->wMonth%10) > 1 || num > 2)
1047 date->wMonth = num;
1048 else
1049 date->wMonth = (date->wMonth%10)*10+num;
1050 MONTHCAL_CalculateDayOfWeek(date, TRUE);
1051 DATETIME_SendDateTimeChangeNotify (infoPtr);
1052 break;
1053 case ONEDIGITDAY:
1054 case TWODIGITDAY:
1055 newDays = (date->wDay%10)*10+num;
1056 if (newDays > MONTHCAL_MonthLength(date->wMonth, date->wYear))
1057 date->wDay = num;
1058 else
1059 date->wDay = newDays;
1060 MONTHCAL_CalculateDayOfWeek(date, TRUE);
1061 DATETIME_SendDateTimeChangeNotify (infoPtr);
1062 break;
1063 case ONEDIGIT12HOUR:
1064 case TWODIGIT12HOUR:
1065 if ((date->wHour%10) > 1 || num > 2)
1066 date->wHour = num;
1067 else
1068 date->wHour = (date->wHour%10)*10+num;
1069 DATETIME_SendDateTimeChangeNotify (infoPtr);
1070 break;
1071 case ONEDIGIT24HOUR:
1072 case TWODIGIT24HOUR:
1073 if ((date->wHour%10) > 2)
1074 date->wHour = num;
1075 else if ((date->wHour%10) == 2 && num > 3)
1076 date->wHour = num;
1077 else
1078 date->wHour = (date->wHour%10)*10+num;
1079 DATETIME_SendDateTimeChangeNotify (infoPtr);
1080 break;
1081 case ONEDIGITMINUTE:
1082 case TWODIGITMINUTE:
1083 if ((date->wMinute%10) > 5)
1084 date->wMinute = num;
1085 else
1086 date->wMinute = (date->wMinute%10)*10+num;
1087 DATETIME_SendDateTimeChangeNotify (infoPtr);
1088 break;
1089 case ONEDIGITSECOND:
1090 case TWODIGITSECOND:
1091 if ((date->wSecond%10) > 5)
1092 date->wSecond = num;
1093 else
1094 date->wSecond = (date->wSecond%10)*10+num;
1095 DATETIME_SendDateTimeChangeNotify (infoPtr);
1096 break;
1099 return 0;
1103 static LRESULT
1104 DATETIME_VScroll (DATETIME_INFO *infoPtr, WORD wScroll)
1106 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1108 if ((SHORT)LOWORD(wScroll) != SB_THUMBPOSITION) return 0;
1109 if (!(infoPtr->haveFocus)) return 0;
1110 if ((fieldNum==0) && (infoPtr->select)) return 0;
1112 if (infoPtr->pendingUpdown >= 0) {
1113 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1114 DATETIME_SendDateTimeChangeNotify (infoPtr);
1116 else {
1117 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1118 DATETIME_SendDateTimeChangeNotify (infoPtr);
1121 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1123 return 0;
1127 static LRESULT
1128 DATETIME_KillFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1130 TRACE("lost focus to %p\n", lostFocus);
1132 if (infoPtr->haveFocus) {
1133 DATETIME_SendSimpleNotify (infoPtr, NM_KILLFOCUS);
1134 infoPtr->haveFocus = 0;
1137 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
1139 return 0;
1143 static LRESULT
1144 DATETIME_NCCreate (HWND hwnd, const CREATESTRUCTW *lpcs)
1146 DWORD dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1147 /* force control to have client edge */
1148 dwExStyle |= WS_EX_CLIENTEDGE;
1149 SetWindowLongW(hwnd, GWL_EXSTYLE, dwExStyle);
1151 return DefWindowProcW(hwnd, WM_NCCREATE, 0, (LPARAM)lpcs);
1155 static LRESULT
1156 DATETIME_SetFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1158 TRACE("got focus from %p\n", lostFocus);
1160 /* if monthcal is open and it loses focus, close monthcal */
1161 if (infoPtr->hMonthCal && (lostFocus == infoPtr->hMonthCal) &&
1162 IsWindowVisible(infoPtr->hMonthCal))
1164 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1165 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1166 /* note: this get triggered even if monthcal loses focus to a dropdown
1167 * box click, which occurs without an intermediate WM_PAINT call
1169 infoPtr->bDropdownEnabled = FALSE;
1170 return 0;
1173 if (infoPtr->haveFocus == 0) {
1174 DATETIME_SendSimpleNotify (infoPtr, NM_SETFOCUS);
1175 infoPtr->haveFocus = DTHT_GOTFOCUS;
1178 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1180 return 0;
1184 static BOOL
1185 DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr)
1187 NMDATETIMECHANGE dtdtc;
1189 dtdtc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1190 dtdtc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1191 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1193 dtdtc.dwFlags = infoPtr->dateValid ? GDT_VALID : GDT_NONE;
1195 dtdtc.st = infoPtr->date;
1196 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1197 dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1201 static BOOL
1202 DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code)
1204 NMHDR nmhdr;
1206 TRACE("%x\n", code);
1207 nmhdr.hwndFrom = infoPtr->hwndSelf;
1208 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1209 nmhdr.code = code;
1211 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1212 nmhdr.idFrom, (LPARAM)&nmhdr);
1215 static LRESULT
1216 DATETIME_Size (DATETIME_INFO *infoPtr, INT width, INT height)
1218 /* set size */
1219 infoPtr->rcClient.bottom = height;
1220 infoPtr->rcClient.right = width;
1222 TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1224 infoPtr->rcDraw = infoPtr->rcClient;
1226 if (infoPtr->dwStyle & DTS_UPDOWN) {
1227 SetWindowPos(infoPtr->hUpdown, NULL,
1228 infoPtr->rcClient.right-14, 0,
1229 15, infoPtr->rcClient.bottom - infoPtr->rcClient.top,
1230 SWP_NOACTIVATE | SWP_NOZORDER);
1232 else {
1233 /* set the size of the button that drops the calendar down */
1234 /* FIXME: account for style that allows button on left side */
1235 infoPtr->calbutton.top = infoPtr->rcDraw.top;
1236 infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1237 infoPtr->calbutton.left = infoPtr->rcDraw.right-15;
1238 infoPtr->calbutton.right = infoPtr->rcDraw.right;
1241 /* set enable/disable button size for show none style being enabled */
1242 /* FIXME: these dimensions are completely incorrect */
1243 infoPtr->checkbox.top = infoPtr->rcDraw.top;
1244 infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1245 infoPtr->checkbox.left = infoPtr->rcDraw.left;
1246 infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1248 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1250 return 0;
1253 static LRESULT
1254 DATETIME_StyleChanging(DATETIME_INFO *infoPtr, WPARAM wStyleType, STYLESTRUCT *lpss)
1256 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1257 wStyleType, lpss->styleOld, lpss->styleNew);
1259 /* block DTS_SHOWNONE change */
1260 if ((lpss->styleNew ^ lpss->styleOld) & DTS_SHOWNONE)
1262 if (lpss->styleOld & DTS_SHOWNONE)
1263 lpss->styleNew |= DTS_SHOWNONE;
1264 else
1265 lpss->styleNew &= ~DTS_SHOWNONE;
1268 return 0;
1271 static LRESULT
1272 DATETIME_StyleChanged(DATETIME_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
1274 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1275 wStyleType, lpss->styleOld, lpss->styleNew);
1277 if (wStyleType != GWL_STYLE) return 0;
1279 infoPtr->dwStyle = lpss->styleNew;
1281 if ( !(lpss->styleOld & DTS_SHOWNONE) && (lpss->styleNew & DTS_SHOWNONE) ) {
1282 infoPtr->hwndCheckbut = CreateWindowExW (0, WC_BUTTONW, 0, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1283 2, 2, 13, 13, infoPtr->hwndSelf, 0,
1284 (HINSTANCE)GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_HINSTANCE), 0);
1285 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, 1, 0);
1287 if ( (lpss->styleOld & DTS_SHOWNONE) && !(lpss->styleNew & DTS_SHOWNONE) ) {
1288 DestroyWindow(infoPtr->hwndCheckbut);
1289 infoPtr->hwndCheckbut = 0;
1291 if ( !(lpss->styleOld & DTS_UPDOWN) && (lpss->styleNew & DTS_UPDOWN) ) {
1292 infoPtr->hUpdown = CreateUpDownControl (WS_CHILD | WS_BORDER | WS_VISIBLE, 120, 1, 20, 20,
1293 infoPtr->hwndSelf, 1, 0, 0, UD_MAXVAL, UD_MINVAL, 0);
1295 if ( (lpss->styleOld & DTS_UPDOWN) && !(lpss->styleNew & DTS_UPDOWN) ) {
1296 DestroyWindow(infoPtr->hUpdown);
1297 infoPtr->hUpdown = 0;
1300 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1301 return 0;
1305 static LRESULT
1306 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1308 infoPtr->hFont = font;
1309 if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1310 return 0;
1314 static LRESULT
1315 DATETIME_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1317 DATETIME_INFO *infoPtr = Alloc (sizeof(DATETIME_INFO));
1318 STYLESTRUCT ss = { 0, lpcs->style };
1320 if (!infoPtr) return -1;
1322 infoPtr->hwndSelf = hwnd;
1323 infoPtr->dwStyle = lpcs->style;
1325 infoPtr->nrFieldsAllocated = 32;
1326 infoPtr->fieldspec = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1327 infoPtr->fieldRect = Alloc (infoPtr->nrFieldsAllocated * sizeof(RECT));
1328 infoPtr->buflen = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1329 infoPtr->hwndNotify = lpcs->hwndParent;
1330 infoPtr->select = -1; /* initially, nothing is selected */
1331 infoPtr->bDropdownEnabled = TRUE;
1333 DATETIME_StyleChanged(infoPtr, GWL_STYLE, &ss);
1334 DATETIME_SetFormatW (infoPtr, 0);
1336 /* create the monthcal control */
1337 infoPtr->hMonthCal = CreateWindowExW (0, MONTHCAL_CLASSW, 0, WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS,
1338 0, 0, 0, 0, infoPtr->hwndSelf, 0, 0, 0);
1340 /* initialize info structure */
1341 GetLocalTime (&infoPtr->date);
1342 infoPtr->dateValid = TRUE;
1343 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1345 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1347 return 0;
1352 static LRESULT
1353 DATETIME_Destroy (DATETIME_INFO *infoPtr)
1355 if (infoPtr->hwndCheckbut)
1356 DestroyWindow(infoPtr->hwndCheckbut);
1357 if (infoPtr->hUpdown)
1358 DestroyWindow(infoPtr->hUpdown);
1359 if (infoPtr->hMonthCal)
1360 DestroyWindow(infoPtr->hMonthCal);
1361 SetWindowLongPtrW( infoPtr->hwndSelf, 0, 0 ); /* clear infoPtr */
1362 Free (infoPtr->buflen);
1363 Free (infoPtr->fieldRect);
1364 Free (infoPtr->fieldspec);
1365 Free (infoPtr);
1366 return 0;
1370 static INT
1371 DATETIME_GetText (DATETIME_INFO *infoPtr, INT count, LPWSTR dst)
1373 WCHAR buf[80];
1374 int i;
1376 if (!dst || (count <= 0)) return 0;
1378 dst[0] = 0;
1379 for (i = 0; i < infoPtr->nrFields; i++)
1381 DATETIME_ReturnTxt(infoPtr, i, buf, sizeof(buf)/sizeof(buf[0]));
1382 if ((strlenW(dst) + strlenW(buf)) < count)
1383 strcatW(dst, buf);
1384 else break;
1386 return strlenW(dst);
1390 static LRESULT WINAPI
1391 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1393 DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1394 LRESULT ret;
1396 TRACE ("%x, %lx, %lx\n", uMsg, wParam, lParam);
1398 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
1399 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
1401 switch (uMsg) {
1403 case DTM_GETSYSTEMTIME:
1404 return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1406 case DTM_SETSYSTEMTIME:
1407 return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1409 case DTM_GETRANGE:
1410 ret = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1411 return ret ? ret : 1; /* bug emulation */
1413 case DTM_SETRANGE:
1414 return SendMessageW (infoPtr->hMonthCal, MCM_SETRANGE, wParam, lParam);
1416 case DTM_SETFORMATA:
1417 return DATETIME_SetFormatA (infoPtr, (LPCSTR)lParam);
1419 case DTM_SETFORMATW:
1420 return DATETIME_SetFormatW (infoPtr, (LPCWSTR)lParam);
1422 case DTM_GETMONTHCAL:
1423 return (LRESULT)infoPtr->hMonthCal;
1425 case DTM_SETMCCOLOR:
1426 return SendMessageW (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
1428 case DTM_GETMCCOLOR:
1429 return SendMessageW (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
1431 case DTM_SETMCFONT:
1432 return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1434 case DTM_GETMCFONT:
1435 return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1437 case WM_NOTIFY:
1438 return DATETIME_Notify (infoPtr, (LPNMHDR)lParam);
1440 case WM_ENABLE:
1441 return DATETIME_Enable (infoPtr, (BOOL)wParam);
1443 case WM_ERASEBKGND:
1444 return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1446 case WM_GETDLGCODE:
1447 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1449 case WM_PRINTCLIENT:
1450 case WM_PAINT:
1451 return DATETIME_Paint (infoPtr, (HDC)wParam);
1453 case WM_KEYDOWN:
1454 return DATETIME_KeyDown (infoPtr, wParam);
1456 case WM_CHAR:
1457 return DATETIME_Char (infoPtr, wParam);
1459 case WM_KILLFOCUS:
1460 return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1462 case WM_NCCREATE:
1463 return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1465 case WM_SETFOCUS:
1466 return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1468 case WM_SIZE:
1469 return DATETIME_Size (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1471 case WM_LBUTTONDOWN:
1472 return DATETIME_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1474 case WM_LBUTTONUP:
1475 return DATETIME_LButtonUp (infoPtr);
1477 case WM_VSCROLL:
1478 return DATETIME_VScroll (infoPtr, (WORD)wParam);
1480 case WM_CREATE:
1481 return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1483 case WM_DESTROY:
1484 return DATETIME_Destroy (infoPtr);
1486 case WM_COMMAND:
1487 return DATETIME_Command (infoPtr, wParam, lParam);
1489 case WM_STYLECHANGING:
1490 return DATETIME_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1492 case WM_STYLECHANGED:
1493 return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1495 case WM_SETFONT:
1496 return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1498 case WM_GETFONT:
1499 return (LRESULT) infoPtr->hFont;
1501 case WM_GETTEXT:
1502 return (LRESULT) DATETIME_GetText(infoPtr, wParam, (LPWSTR)lParam);
1504 case WM_SETTEXT:
1505 return CB_ERR;
1507 default:
1508 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
1509 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
1510 uMsg, wParam, lParam);
1511 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1516 void
1517 DATETIME_Register (void)
1519 WNDCLASSW wndClass;
1521 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1522 wndClass.style = CS_GLOBALCLASS;
1523 wndClass.lpfnWndProc = DATETIME_WindowProc;
1524 wndClass.cbClsExtra = 0;
1525 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1526 wndClass.hCursor = LoadCursorW (0, (LPCWSTR)IDC_ARROW);
1527 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1528 wndClass.lpszClassName = DATETIMEPICK_CLASSW;
1530 RegisterClassW (&wndClass);
1534 void
1535 DATETIME_Unregister (void)
1537 UnregisterClassW (DATETIMEPICK_CLASSW, NULL);