usp10: If leading with a strong character use its bidi level for our intial LayoutRTL.
[wine/multimedia.git] / dlls / comctl32 / datetime.c
blob785769a4a37d32415ddf17c89bea2b1f924de81c
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 WCHAR charsEntered[4];
77 int nCharsEntered;
78 HFONT hFont;
79 int nrFieldsAllocated;
80 int nrFields;
81 int haveFocus;
82 int *fieldspec;
83 RECT *fieldRect;
84 int *buflen;
85 WCHAR textbuf[256];
86 POINT monthcal_pos;
87 int pendingUpdown;
88 } DATETIME_INFO, *LPDATETIME_INFO;
90 /* in monthcal.c */
91 extern int MONTHCAL_MonthLength(int month, int year);
92 extern int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace);
94 /* this list of defines is closely related to `allowedformatchars' defined
95 * in datetime.c; the high nibble indicates the `base type' of the format
96 * specifier.
97 * Do not change without first reading DATETIME_UseFormat.
101 #define DT_END_FORMAT 0
102 #define ONEDIGITDAY 0x01
103 #define TWODIGITDAY 0x02
104 #define THREECHARDAY 0x03
105 #define FULLDAY 0x04
106 #define ONEDIGIT12HOUR 0x11
107 #define TWODIGIT12HOUR 0x12
108 #define ONEDIGIT24HOUR 0x21
109 #define TWODIGIT24HOUR 0x22
110 #define ONEDIGITMINUTE 0x31
111 #define TWODIGITMINUTE 0x32
112 #define ONEDIGITMONTH 0x41
113 #define TWODIGITMONTH 0x42
114 #define THREECHARMONTH 0x43
115 #define FULLMONTH 0x44
116 #define ONEDIGITSECOND 0x51
117 #define TWODIGITSECOND 0x52
118 #define ONELETTERAMPM 0x61
119 #define TWOLETTERAMPM 0x62
120 #define ONEDIGITYEAR 0x71
121 #define TWODIGITYEAR 0x72
122 #define INVALIDFULLYEAR 0x73 /* FIXME - yyy is not valid - we'll treat it as yyyy */
123 #define FULLYEAR 0x74
124 #define FORMATCALLBACK 0x81 /* -> maximum of 0x80 callbacks possible */
125 #define FORMATCALLMASK 0x80
126 #define DT_STRING 0x0100
128 #define DTHT_DATEFIELD 0xff /* for hit-testing */
130 #define DTHT_NONE 0x1000
131 #define DTHT_CHECKBOX 0x2000 /* these should end at '00' , to make */
132 #define DTHT_MCPOPUP 0x3000 /* & DTHT_DATEFIELD 0 when DATETIME_KeyDown */
133 #define DTHT_GOTFOCUS 0x4000 /* tests for date-fields */
134 #define DTHT_NODATEMASK 0xf000 /* to mask check and drop down from others */
136 static BOOL DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code);
137 static BOOL DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr);
138 static const WCHAR allowedformatchars[] = {'d', 'h', 'H', 'm', 'M', 's', 't', 'y', 'X', 0};
139 static const int maxrepetition [] = {4,2,2,2,4,2,2,4,-1};
142 static DWORD
143 DATETIME_GetSystemTime (const DATETIME_INFO *infoPtr, SYSTEMTIME *systime)
145 if (!systime) return GDT_NONE;
147 if ((infoPtr->dwStyle & DTS_SHOWNONE) &&
148 (SendMessageW (infoPtr->hwndCheckbut, BM_GETCHECK, 0, 0) == BST_UNCHECKED))
149 return GDT_NONE;
151 *systime = infoPtr->date;
153 return GDT_VALID;
157 static BOOL
158 DATETIME_SetSystemTime (DATETIME_INFO *infoPtr, DWORD flag, const SYSTEMTIME *systime)
160 if (!systime) return 0;
162 TRACE("%04d/%02d/%02d %02d:%02d:%02d\n",
163 systime->wYear, systime->wMonth, systime->wDay,
164 systime->wHour, systime->wMinute, systime->wSecond);
166 if (flag == GDT_VALID) {
167 if (systime->wYear < 1601 || systime->wYear > 30827 ||
168 systime->wMonth < 1 || systime->wMonth > 12 ||
169 systime->wDay < 1 ||
170 systime->wDay > MONTHCAL_MonthLength(systime->wMonth, systime->wYear) ||
171 systime->wHour > 23 ||
172 systime->wMinute > 59 ||
173 systime->wSecond > 59 ||
174 systime->wMilliseconds > 999
176 return FALSE;
178 infoPtr->dateValid = TRUE;
179 infoPtr->date = *systime;
180 /* always store a valid day of week */
181 MONTHCAL_CalculateDayOfWeek(&infoPtr->date, TRUE);
183 SendMessageW (infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
184 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
185 } else if ((infoPtr->dwStyle & DTS_SHOWNONE) && (flag == GDT_NONE)) {
186 infoPtr->dateValid = FALSE;
187 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_UNCHECKED, 0);
189 else
190 return FALSE;
192 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
193 return TRUE;
197 /***
198 * Split up a formattxt in actions.
199 * See ms documentation for the meaning of the letter codes/'specifiers'.
201 * Notes:
202 * *'dddddd' is handled as 'dddd' plus 'dd'.
203 * *unrecognized formats are strings (here given the type DT_STRING;
204 * start of the string is encoded in lower bits of DT_STRING.
205 * Therefore, 'string' ends finally up as '<show seconds>tring'.
208 static void
209 DATETIME_UseFormat (DATETIME_INFO *infoPtr, LPCWSTR formattxt)
211 unsigned int i;
212 int j, k, len;
213 BOOL inside_literal = FALSE; /* inside '...' */
214 int *nrFields = &infoPtr->nrFields;
216 *nrFields = 0;
217 infoPtr->fieldspec[*nrFields] = 0;
218 len = strlenW(allowedformatchars);
219 k = 0;
221 for (i = 0; formattxt[i]; i++) {
222 TRACE ("\n%d %c:", i, formattxt[i]);
223 if (!inside_literal) {
224 for (j = 0; j < len; j++) {
225 if (allowedformatchars[j]==formattxt[i]) {
226 TRACE ("%c[%d,%x]", allowedformatchars[j], *nrFields, infoPtr->fieldspec[*nrFields]);
227 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
228 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
229 break;
231 if (infoPtr->fieldspec[*nrFields] >> 4 != j) {
232 (*nrFields)++;
233 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
234 break;
236 if ((infoPtr->fieldspec[*nrFields] & 0x0f) == maxrepetition[j]) {
237 (*nrFields)++;
238 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
239 break;
241 infoPtr->fieldspec[*nrFields]++;
242 break;
243 } /* if allowedformatchar */
244 } /* for j */
246 else
247 j = len;
249 if (formattxt[i] == '\'')
251 inside_literal = !inside_literal;
252 continue;
255 /* char is not a specifier: handle char like a string */
256 if (j == len) {
257 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
258 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
259 infoPtr->buflen[*nrFields] = 0;
260 } else if ((infoPtr->fieldspec[*nrFields] & DT_STRING) != DT_STRING) {
261 (*nrFields)++;
262 infoPtr->fieldspec[*nrFields] = DT_STRING + k;
263 infoPtr->buflen[*nrFields] = 0;
265 infoPtr->textbuf[k] = formattxt[i];
266 k++;
267 infoPtr->buflen[*nrFields]++;
268 } /* if j=len */
270 if (*nrFields == infoPtr->nrFieldsAllocated) {
271 FIXME ("out of memory; should reallocate. crash ahead.\n");
273 } /* for i */
275 TRACE("\n");
277 if (infoPtr->fieldspec[*nrFields] != 0) (*nrFields)++;
281 static BOOL
282 DATETIME_SetFormatW (DATETIME_INFO *infoPtr, LPCWSTR format)
284 WCHAR format_buf[80];
286 if (!format) {
287 DWORD format_item;
289 if (infoPtr->dwStyle & DTS_LONGDATEFORMAT)
290 format_item = LOCALE_SLONGDATE;
291 else if ((infoPtr->dwStyle & DTS_TIMEFORMAT) == DTS_TIMEFORMAT)
292 format_item = LOCALE_STIMEFORMAT;
293 else /* DTS_SHORTDATEFORMAT */
294 format_item = LOCALE_SSHORTDATE;
295 GetLocaleInfoW(LOCALE_USER_DEFAULT, format_item, format_buf, sizeof(format_buf)/sizeof(format_buf[0]));
296 format = format_buf;
299 DATETIME_UseFormat (infoPtr, format);
300 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
302 return TRUE;
306 static BOOL
307 DATETIME_SetFormatA (DATETIME_INFO *infoPtr, LPCSTR lpszFormat)
309 if (lpszFormat) {
310 BOOL retval;
311 INT len = MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, NULL, 0);
312 LPWSTR wstr = Alloc(len * sizeof(WCHAR));
313 if (wstr) MultiByteToWideChar(CP_ACP, 0, lpszFormat, -1, wstr, len);
314 retval = DATETIME_SetFormatW (infoPtr, wstr);
315 Free (wstr);
316 return retval;
318 else
319 return DATETIME_SetFormatW (infoPtr, 0);
324 static void
325 DATETIME_ReturnTxt (const DATETIME_INFO *infoPtr, int count, LPWSTR result, int resultSize)
327 static const WCHAR fmt_dW[] = { '%', 'd', 0 };
328 static const WCHAR fmt__2dW[] = { '%', '.', '2', 'd', 0 };
329 static const WCHAR fmt__3sW[] = { '%', '.', '3', 's', 0 };
330 SYSTEMTIME date = infoPtr->date;
331 int spec;
332 WCHAR buffer[80];
334 *result=0;
335 TRACE ("%d,%d\n", infoPtr->nrFields, count);
336 if (count>infoPtr->nrFields || count < 0) {
337 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
338 return;
341 if (!infoPtr->fieldspec) return;
343 spec = infoPtr->fieldspec[count];
344 if (spec & DT_STRING) {
345 int txtlen = infoPtr->buflen[count];
347 if (txtlen > resultSize)
348 txtlen = resultSize - 1;
349 memcpy (result, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
350 result[txtlen] = 0;
351 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
352 return;
356 switch (spec) {
357 case DT_END_FORMAT:
358 *result = 0;
359 break;
360 case ONEDIGITDAY:
361 wsprintfW (result, fmt_dW, date.wDay);
362 break;
363 case TWODIGITDAY:
364 wsprintfW (result, fmt__2dW, date.wDay);
365 break;
366 case THREECHARDAY:
367 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1+(date.wDayOfWeek+6)%7, result, 4);
368 /*wsprintfW (result,"%.3s",days[date.wDayOfWeek]);*/
369 break;
370 case FULLDAY:
371 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME1+(date.wDayOfWeek+6)%7, result, resultSize);
372 break;
373 case ONEDIGIT12HOUR:
374 if (date.wHour == 0) {
375 result[0] = '1';
376 result[1] = '2';
377 result[2] = 0;
379 else
380 wsprintfW (result, fmt_dW, date.wHour - (date.wHour > 12 ? 12 : 0));
381 break;
382 case TWODIGIT12HOUR:
383 if (date.wHour == 0) {
384 result[0] = '1';
385 result[1] = '2';
386 result[2] = 0;
388 else
389 wsprintfW (result, fmt__2dW, date.wHour - (date.wHour > 12 ? 12 : 0));
390 break;
391 case ONEDIGIT24HOUR:
392 wsprintfW (result, fmt_dW, date.wHour);
393 break;
394 case TWODIGIT24HOUR:
395 wsprintfW (result, fmt__2dW, date.wHour);
396 break;
397 case ONEDIGITSECOND:
398 wsprintfW (result, fmt_dW, date.wSecond);
399 break;
400 case TWODIGITSECOND:
401 wsprintfW (result, fmt__2dW, date.wSecond);
402 break;
403 case ONEDIGITMINUTE:
404 wsprintfW (result, fmt_dW, date.wMinute);
405 break;
406 case TWODIGITMINUTE:
407 wsprintfW (result, fmt__2dW, date.wMinute);
408 break;
409 case ONEDIGITMONTH:
410 wsprintfW (result, fmt_dW, date.wMonth);
411 break;
412 case TWODIGITMONTH:
413 wsprintfW (result, fmt__2dW, date.wMonth);
414 break;
415 case THREECHARMONTH:
416 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+date.wMonth -1,
417 buffer, sizeof(buffer)/sizeof(buffer[0]));
418 wsprintfW (result, fmt__3sW, buffer);
419 break;
420 case FULLMONTH:
421 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+date.wMonth -1,
422 result, resultSize);
423 break;
424 case ONELETTERAMPM:
425 result[0] = (date.wHour < 12 ? 'A' : 'P');
426 result[1] = 0;
427 break;
428 case TWOLETTERAMPM:
429 result[0] = (date.wHour < 12 ? 'A' : 'P');
430 result[1] = 'M';
431 result[2] = 0;
432 break;
433 case FORMATCALLBACK:
434 FIXME ("Not implemented\n");
435 result[0] = 'x';
436 result[1] = 0;
437 break;
438 case ONEDIGITYEAR:
439 wsprintfW (result, fmt_dW, date.wYear-10* (int) floor(date.wYear/10));
440 break;
441 case TWODIGITYEAR:
442 wsprintfW (result, fmt__2dW, date.wYear-100* (int) floor(date.wYear/100));
443 break;
444 case INVALIDFULLYEAR:
445 case FULLYEAR:
446 wsprintfW (result, fmt_dW, date.wYear);
447 break;
450 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
453 static int wrap(int val, int delta, int minVal, int maxVal)
455 val += delta;
456 if (delta == INT_MIN || val < minVal) return maxVal;
457 if (delta == INT_MAX || val > maxVal) return minVal;
458 return val;
461 static void
462 DATETIME_IncreaseField (DATETIME_INFO *infoPtr, int number, int delta)
464 SYSTEMTIME *date = &infoPtr->date;
466 TRACE ("%d\n", number);
467 if ((number > infoPtr->nrFields) || (number < 0)) return;
469 if ((infoPtr->fieldspec[number] & DTHT_DATEFIELD) == 0) return;
471 switch (infoPtr->fieldspec[number]) {
472 case ONEDIGITYEAR:
473 case TWODIGITYEAR:
474 case FULLYEAR:
475 date->wYear = wrap(date->wYear, delta, 1752, 9999);
476 if (date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
477 /* This can happen when moving away from a leap year. */
478 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear);
479 MONTHCAL_CalculateDayOfWeek(date, TRUE);
480 break;
481 case ONEDIGITMONTH:
482 case TWODIGITMONTH:
483 case THREECHARMONTH:
484 case FULLMONTH:
485 date->wMonth = wrap(date->wMonth, delta, 1, 12);
486 MONTHCAL_CalculateDayOfWeek(date, TRUE);
487 delta = 0;
488 /* fall through */
489 case ONEDIGITDAY:
490 case TWODIGITDAY:
491 case THREECHARDAY:
492 case FULLDAY:
493 date->wDay = wrap(date->wDay, delta, 1, MONTHCAL_MonthLength(date->wMonth, date->wYear));
494 MONTHCAL_CalculateDayOfWeek(date, TRUE);
495 break;
496 case ONELETTERAMPM:
497 case TWOLETTERAMPM:
498 delta *= 12;
499 /* fall through */
500 case ONEDIGIT12HOUR:
501 case TWODIGIT12HOUR:
502 case ONEDIGIT24HOUR:
503 case TWODIGIT24HOUR:
504 date->wHour = wrap(date->wHour, delta, 0, 23);
505 break;
506 case ONEDIGITMINUTE:
507 case TWODIGITMINUTE:
508 date->wMinute = wrap(date->wMinute, delta, 0, 59);
509 break;
510 case ONEDIGITSECOND:
511 case TWODIGITSECOND:
512 date->wSecond = wrap(date->wSecond, delta, 0, 59);
513 break;
514 case FORMATCALLBACK:
515 FIXME ("Not implemented\n");
516 break;
519 /* FYI: On 1752/9/14 the calendar changed and England and the
520 * American colonies changed to the Gregorian calendar. This change
521 * involved having September 14th follow September 2nd. So no date
522 * algorithm works before that date.
524 if (10000 * date->wYear + 100 * date->wMonth + date->wDay < 17520914) {
525 date->wYear = 1752;
526 date->wMonth = 9;
527 date->wDay = 14;
528 date->wSecond = 0;
529 date->wMinute = 0;
530 date->wHour = 0;
535 static void
536 DATETIME_ReturnFieldWidth (const DATETIME_INFO *infoPtr, HDC hdc, int count, SHORT *width)
538 /* fields are a fixed width, determined by the largest possible string */
539 /* presumably, these widths should be language dependent */
540 static const WCHAR fld_d1W[] = { '2', 0 };
541 static const WCHAR fld_d2W[] = { '2', '2', 0 };
542 static const WCHAR fld_d4W[] = { '2', '2', '2', '2', 0 };
543 static const WCHAR fld_am1[] = { 'A', 0 };
544 static const WCHAR fld_am2[] = { 'A', 'M', 0 };
545 int spec;
546 WCHAR buffer[80];
547 LPCWSTR bufptr;
548 SIZE size;
550 TRACE ("%d,%d\n", infoPtr->nrFields, count);
551 if (count>infoPtr->nrFields || count < 0) {
552 WARN ("buffer overrun, have %d want %d\n", infoPtr->nrFields, count);
553 return;
556 if (!infoPtr->fieldspec) return;
558 spec = infoPtr->fieldspec[count];
559 if (spec & DT_STRING) {
560 int txtlen = infoPtr->buflen[count];
562 if (txtlen > 79)
563 txtlen = 79;
564 memcpy (buffer, infoPtr->textbuf + (spec &~ DT_STRING), txtlen * sizeof(WCHAR));
565 buffer[txtlen] = 0;
566 bufptr = buffer;
568 else {
569 switch (spec) {
570 case ONEDIGITDAY:
571 case ONEDIGIT12HOUR:
572 case ONEDIGIT24HOUR:
573 case ONEDIGITSECOND:
574 case ONEDIGITMINUTE:
575 case ONEDIGITMONTH:
576 case ONEDIGITYEAR:
577 /* these seem to use a two byte field */
578 case TWODIGITDAY:
579 case TWODIGIT12HOUR:
580 case TWODIGIT24HOUR:
581 case TWODIGITSECOND:
582 case TWODIGITMINUTE:
583 case TWODIGITMONTH:
584 case TWODIGITYEAR:
585 bufptr = fld_d2W;
586 break;
587 case INVALIDFULLYEAR:
588 case FULLYEAR:
589 bufptr = fld_d4W;
590 break;
591 case THREECHARMONTH:
592 case FULLMONTH:
593 case THREECHARDAY:
594 case FULLDAY:
596 static const WCHAR fld_day[] = {'W','e','d','n','e','s','d','a','y',0};
597 static const WCHAR fld_abbrday[] = {'W','e','d',0};
598 static const WCHAR fld_mon[] = {'S','e','p','t','e','m','b','e','r',0};
599 static const WCHAR fld_abbrmon[] = {'D','e','c',0};
601 const WCHAR *fall;
602 LCTYPE lctype;
603 INT i, max_count;
604 LONG cx;
606 /* choose locale data type and fallback string */
607 switch (spec) {
608 case THREECHARDAY:
609 fall = fld_abbrday;
610 lctype = LOCALE_SABBREVDAYNAME1;
611 max_count = 7;
612 break;
613 case FULLDAY:
614 fall = fld_day;
615 lctype = LOCALE_SDAYNAME1;
616 max_count = 7;
617 break;
618 case THREECHARMONTH:
619 fall = fld_abbrmon;
620 lctype = LOCALE_SABBREVMONTHNAME1;
621 max_count = 12;
622 break;
623 default: /* FULLMONTH */
624 fall = fld_mon;
625 lctype = LOCALE_SMONTHNAME1;
626 max_count = 12;
627 break;
630 cx = 0;
631 for (i = 0; i < max_count; i++)
633 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, lctype + i,
634 buffer, lstrlenW(buffer)))
636 GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &size);
637 if (size.cx > cx) cx = size.cx;
639 else /* locale independent fallback on failure */
641 GetTextExtentPoint32W(hdc, fall, lstrlenW(fall), &size);
642 cx = size.cx;
643 break;
646 *width = cx;
647 return;
649 case ONELETTERAMPM:
650 bufptr = fld_am1;
651 break;
652 case TWOLETTERAMPM:
653 bufptr = fld_am2;
654 break;
655 default:
656 bufptr = fld_d1W;
657 break;
660 GetTextExtentPoint32W (hdc, bufptr, strlenW(bufptr), &size);
661 *width = size.cx;
664 static void
665 DATETIME_Refresh (DATETIME_INFO *infoPtr, HDC hdc)
667 TRACE("\n");
669 if (infoPtr->dateValid) {
670 int i, prevright;
671 RECT *field;
672 RECT *rcDraw = &infoPtr->rcDraw;
673 SIZE size;
674 COLORREF oldTextColor;
675 SHORT fieldWidth = 0;
676 HFONT oldFont = SelectObject (hdc, infoPtr->hFont);
677 INT oldBkMode = SetBkMode (hdc, TRANSPARENT);
678 WCHAR txt[80];
680 DATETIME_ReturnTxt (infoPtr, 0, txt, sizeof(txt)/sizeof(txt[0]));
681 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
682 rcDraw->bottom = size.cy + 2;
684 prevright = infoPtr->checkbox.right = ((infoPtr->dwStyle & DTS_SHOWNONE) ? 18 : 2);
686 for (i = 0; i < infoPtr->nrFields; i++) {
687 DATETIME_ReturnTxt (infoPtr, i, txt, sizeof(txt)/sizeof(txt[0]));
688 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
689 DATETIME_ReturnFieldWidth (infoPtr, hdc, i, &fieldWidth);
690 field = &infoPtr->fieldRect[i];
691 field->left = prevright;
692 field->right = prevright + fieldWidth;
693 field->top = rcDraw->top;
694 field->bottom = rcDraw->bottom;
695 prevright = field->right;
697 if (infoPtr->dwStyle & WS_DISABLED)
698 oldTextColor = SetTextColor (hdc, comctl32_color.clrGrayText);
699 else if ((infoPtr->haveFocus) && (i == infoPtr->select)) {
700 RECT selection;
702 /* fill if focused */
703 HBRUSH hbr = CreateSolidBrush (comctl32_color.clrActiveCaption);
705 if (infoPtr->nCharsEntered)
707 memcpy(txt, infoPtr->charsEntered, infoPtr->nCharsEntered * sizeof(WCHAR));
708 txt[infoPtr->nCharsEntered] = 0;
709 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
712 selection.left = 0;
713 selection.top = 0;
714 selection.right = size.cx;
715 selection.bottom = size.cy;
716 /* center rectangle */
717 OffsetRect(&selection, (field->right + field->left - size.cx)/2,
718 (field->bottom - size.cy)/2);
720 FillRect(hdc, &selection, hbr);
721 DeleteObject (hbr);
722 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindow);
724 else
725 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindowText);
727 /* draw the date text using the colour set above */
728 DrawTextW (hdc, txt, strlenW(txt), field, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
729 SetTextColor (hdc, oldTextColor);
731 SetBkMode (hdc, oldBkMode);
732 SelectObject (hdc, oldFont);
735 if (!(infoPtr->dwStyle & DTS_UPDOWN)) {
736 DrawFrameControl(hdc, &infoPtr->calbutton, DFC_SCROLL,
737 DFCS_SCROLLDOWN | (infoPtr->bCalDepressed ? DFCS_PUSHED : 0) |
738 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
743 static INT
744 DATETIME_HitTest (const DATETIME_INFO *infoPtr, POINT pt)
746 int i;
748 TRACE ("%d, %d\n", pt.x, pt.y);
750 if (PtInRect (&infoPtr->calbutton, pt)) return DTHT_MCPOPUP;
751 if (PtInRect (&infoPtr->checkbox, pt)) return DTHT_CHECKBOX;
753 for (i = 0; i < infoPtr->nrFields; i++) {
754 if (PtInRect (&infoPtr->fieldRect[i], pt)) return i;
757 return DTHT_NONE;
760 /* Returns index of a closest date field from given counting to left
761 or -1 if there's no such fields at left */
762 static int DATETIME_GetPrevDateField(const DATETIME_INFO *infoPtr, int i)
764 for(--i; i >= 0; i--)
766 if (infoPtr->fieldspec[i] & DTHT_DATEFIELD) return i;
768 return -1;
771 static void
772 DATETIME_ApplySelectedField (DATETIME_INFO *infoPtr)
774 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
775 int i, val=0, clamp_day=0;
776 SYSTEMTIME date = infoPtr->date;
778 if (infoPtr->select == -1 || infoPtr->nCharsEntered == 0)
779 return;
781 for (i=0; i<infoPtr->nCharsEntered; i++)
782 val = val * 10 + infoPtr->charsEntered[i] - '0';
784 infoPtr->nCharsEntered = 0;
786 switch (infoPtr->fieldspec[fieldNum]) {
787 case ONEDIGITYEAR:
788 case TWODIGITYEAR:
789 date.wYear = date.wYear - (date.wYear%100) + val;
790 clamp_day = 1;
791 break;
792 case INVALIDFULLYEAR:
793 case FULLYEAR:
794 date.wYear = val;
795 clamp_day = 1;
796 break;
797 case ONEDIGITMONTH:
798 case TWODIGITMONTH:
799 date.wMonth = val;
800 clamp_day = 1;
801 break;
802 case ONEDIGITDAY:
803 case TWODIGITDAY:
804 date.wDay = val;
805 break;
806 case ONEDIGIT12HOUR:
807 case TWODIGIT12HOUR:
808 case ONEDIGIT24HOUR:
809 case TWODIGIT24HOUR:
810 /* FIXME: Preserve AM/PM for 12HOUR? */
811 date.wHour = val;
812 break;
813 case ONEDIGITMINUTE:
814 case TWODIGITMINUTE:
815 date.wMinute = val;
816 break;
817 case ONEDIGITSECOND:
818 case TWODIGITSECOND:
819 date.wSecond = val;
820 break;
823 if (clamp_day && date.wDay > MONTHCAL_MonthLength(date.wMonth, date.wYear))
824 date.wDay = MONTHCAL_MonthLength(date.wMonth, date.wYear);
826 if (DATETIME_SetSystemTime(infoPtr, GDT_VALID, &date))
827 DATETIME_SendDateTimeChangeNotify (infoPtr);
830 static void
831 DATETIME_SetSelectedField (DATETIME_INFO *infoPtr, int select)
833 DATETIME_ApplySelectedField(infoPtr);
835 infoPtr->select = select;
836 infoPtr->nCharsEntered = 0;
839 static LRESULT
840 DATETIME_LButtonDown (DATETIME_INFO *infoPtr, INT x, INT y)
842 POINT pt;
843 int new;
845 pt.x = x;
846 pt.y = y;
847 new = DATETIME_HitTest (infoPtr, pt);
849 SetFocus(infoPtr->hwndSelf);
851 if (!(new & DTHT_NODATEMASK) || (new == DTHT_NONE))
853 if (new == DTHT_NONE)
854 new = infoPtr->nrFields - 1;
855 else
857 /* hitting string part moves selection to next date field to left */
858 if (infoPtr->fieldspec[new] & DT_STRING)
860 new = DATETIME_GetPrevDateField(infoPtr, new);
861 if (new == -1) return 0;
863 /* never select full day of week */
864 if (infoPtr->fieldspec[new] == FULLDAY) return 0;
868 DATETIME_SetSelectedField(infoPtr, new);
870 if (infoPtr->select == DTHT_MCPOPUP) {
871 RECT rcMonthCal;
872 POINT pos;
873 SendMessageW(infoPtr->hMonthCal, MCM_GETMINREQRECT, 0, (LPARAM)&rcMonthCal);
875 /* FIXME: button actually is only depressed during dropdown of the */
876 /* calendar control and when the mouse is over the button window */
877 infoPtr->bCalDepressed = TRUE;
879 /* recalculate the position of the monthcal popup */
880 if(infoPtr->dwStyle & DTS_RIGHTALIGN)
881 pos.x = infoPtr->calbutton.left - (rcMonthCal.right - rcMonthCal.left);
882 else
883 /* FIXME: this should be after the area reserved for the checkbox */
884 pos.x = infoPtr->rcDraw.left;
886 pos.y = infoPtr->rcClient.bottom;
887 OffsetRect( &rcMonthCal, pos.x, pos.y );
888 MapWindowPoints( infoPtr->hwndSelf, 0, (POINT *)&rcMonthCal, 2 );
889 SetWindowPos(infoPtr->hMonthCal, 0, rcMonthCal.left, rcMonthCal.top,
890 rcMonthCal.right - rcMonthCal.left, rcMonthCal.bottom - rcMonthCal.top, 0);
892 if(IsWindowVisible(infoPtr->hMonthCal)) {
893 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
894 infoPtr->bDropdownEnabled = FALSE;
895 DATETIME_SendSimpleNotify (infoPtr, DTN_CLOSEUP);
896 } else {
897 const SYSTEMTIME *lprgSysTimeArray = &infoPtr->date;
898 TRACE("update calendar %04d/%02d/%02d\n",
899 lprgSysTimeArray->wYear, lprgSysTimeArray->wMonth, lprgSysTimeArray->wDay);
900 SendMessageW(infoPtr->hMonthCal, MCM_SETCURSEL, 0, (LPARAM)(&infoPtr->date));
902 if (infoPtr->bDropdownEnabled) {
903 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
904 DATETIME_SendSimpleNotify (infoPtr, DTN_DROPDOWN);
906 infoPtr->bDropdownEnabled = TRUE;
909 TRACE ("dt:%p mc:%p mc parent:%p, desktop:%p\n",
910 infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hwndNotify, GetDesktopWindow ());
913 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
915 return 0;
919 static LRESULT
920 DATETIME_LButtonUp (DATETIME_INFO *infoPtr)
922 if(infoPtr->bCalDepressed) {
923 infoPtr->bCalDepressed = FALSE;
924 InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
927 return 0;
931 static LRESULT
932 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
934 if (!hdc) {
935 PAINTSTRUCT ps;
936 hdc = BeginPaint (infoPtr->hwndSelf, &ps);
937 DATETIME_Refresh (infoPtr, hdc);
938 EndPaint (infoPtr->hwndSelf, &ps);
939 } else {
940 DATETIME_Refresh (infoPtr, hdc);
943 /* Not a click on the dropdown box, enabled it */
944 infoPtr->bDropdownEnabled = TRUE;
946 return 0;
950 static LRESULT
951 DATETIME_Button_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
953 if( HIWORD(wParam) == BN_CLICKED) {
954 DWORD state = SendMessageW((HWND)lParam, BM_GETCHECK, 0, 0);
955 infoPtr->dateValid = (state == BST_CHECKED);
956 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
958 return 0;
963 static LRESULT
964 DATETIME_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
966 TRACE("hwndbutton = %p\n", infoPtr->hwndCheckbut);
967 if(infoPtr->hwndCheckbut == (HWND)lParam)
968 return DATETIME_Button_Command(infoPtr, wParam, lParam);
969 return 0;
973 static LRESULT
974 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
976 TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
977 if (bEnable)
978 infoPtr->dwStyle &= ~WS_DISABLED;
979 else
980 infoPtr->dwStyle |= WS_DISABLED;
982 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
984 return 0;
988 static LRESULT
989 DATETIME_EraseBackground (const DATETIME_INFO *infoPtr, HDC hdc)
991 HBRUSH hBrush, hSolidBrush = NULL;
992 RECT rc;
994 if (infoPtr->dwStyle & WS_DISABLED)
995 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
996 else
998 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
999 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
1000 if (!hBrush)
1001 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
1004 GetClientRect (infoPtr->hwndSelf, &rc);
1006 FillRect (hdc, &rc, hBrush);
1008 if (hSolidBrush)
1009 DeleteObject(hSolidBrush);
1011 return -1;
1015 static LRESULT
1016 DATETIME_Notify (DATETIME_INFO *infoPtr, const NMHDR *lpnmh)
1018 TRACE ("Got notification %x from %p\n", lpnmh->code, lpnmh->hwndFrom);
1019 TRACE ("info: %p %p %p\n", infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hUpdown);
1021 if (lpnmh->code == MCN_SELECT) {
1022 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1023 infoPtr->dateValid = TRUE;
1024 SendMessageW (infoPtr->hMonthCal, MCM_GETCURSEL, 0, (LPARAM)&infoPtr->date);
1025 TRACE("got from calendar %04d/%02d/%02d day of week %d\n",
1026 infoPtr->date.wYear, infoPtr->date.wMonth, infoPtr->date.wDay, infoPtr->date.wDayOfWeek);
1027 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, BST_CHECKED, 0);
1028 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1029 DATETIME_SendDateTimeChangeNotify (infoPtr);
1030 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1032 if ((lpnmh->hwndFrom == infoPtr->hUpdown) && (lpnmh->code == UDN_DELTAPOS)) {
1033 const NM_UPDOWN *lpnmud = (const NM_UPDOWN*)lpnmh;
1034 TRACE("Delta pos %d\n", lpnmud->iDelta);
1035 infoPtr->pendingUpdown = lpnmud->iDelta;
1037 return 0;
1041 static LRESULT
1042 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode)
1044 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1045 int wrap = 0;
1046 int new;
1048 if (!(infoPtr->haveFocus)) return 0;
1049 if ((fieldNum==0) && (infoPtr->select)) return 0;
1051 if (infoPtr->select & FORMATCALLMASK) {
1052 FIXME ("Callbacks not implemented yet\n");
1055 switch (vkCode) {
1056 case VK_ADD:
1057 case VK_UP:
1058 infoPtr->nCharsEntered = 0;
1059 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1060 DATETIME_SendDateTimeChangeNotify (infoPtr);
1061 break;
1062 case VK_SUBTRACT:
1063 case VK_DOWN:
1064 infoPtr->nCharsEntered = 0;
1065 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1066 DATETIME_SendDateTimeChangeNotify (infoPtr);
1067 break;
1068 case VK_HOME:
1069 infoPtr->nCharsEntered = 0;
1070 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MIN);
1071 DATETIME_SendDateTimeChangeNotify (infoPtr);
1072 break;
1073 case VK_END:
1074 infoPtr->nCharsEntered = 0;
1075 DATETIME_IncreaseField (infoPtr, fieldNum, INT_MAX);
1076 DATETIME_SendDateTimeChangeNotify (infoPtr);
1077 break;
1078 case VK_LEFT:
1079 new = infoPtr->select;
1080 do {
1081 if (new == 0) {
1082 new = new - 1;
1083 wrap++;
1084 } else {
1085 new--;
1087 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1088 if (new != infoPtr->select)
1089 DATETIME_SetSelectedField(infoPtr, new);
1090 break;
1091 case VK_RIGHT:
1092 new = infoPtr->select;
1093 do {
1094 new++;
1095 if (new==infoPtr->nrFields) {
1096 new = 0;
1097 wrap++;
1099 } while ((infoPtr->fieldspec[new] & DT_STRING) && (wrap<2));
1100 if (new != infoPtr->select)
1101 DATETIME_SetSelectedField(infoPtr, new);
1102 break;
1105 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1107 return 0;
1111 static LRESULT
1112 DATETIME_Char (DATETIME_INFO *infoPtr, WPARAM vkCode)
1114 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1116 if (vkCode >= '0' && vkCode <= '9') {
1117 int maxChars;
1118 int fieldSpec;
1120 infoPtr->charsEntered[infoPtr->nCharsEntered++] = vkCode;
1122 fieldSpec = infoPtr->fieldspec[fieldNum];
1124 if (fieldSpec == INVALIDFULLYEAR || fieldSpec == FULLYEAR)
1125 maxChars = 4;
1126 else
1127 maxChars = 2;
1129 if (maxChars == infoPtr->nCharsEntered)
1130 DATETIME_ApplySelectedField(infoPtr);
1132 return 0;
1136 static LRESULT
1137 DATETIME_VScroll (DATETIME_INFO *infoPtr, WORD wScroll)
1139 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1141 if ((SHORT)LOWORD(wScroll) != SB_THUMBPOSITION) return 0;
1142 if (!(infoPtr->haveFocus)) return 0;
1143 if ((fieldNum==0) && (infoPtr->select)) return 0;
1145 if (infoPtr->pendingUpdown >= 0) {
1146 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1147 DATETIME_SendDateTimeChangeNotify (infoPtr);
1149 else {
1150 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1151 DATETIME_SendDateTimeChangeNotify (infoPtr);
1154 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1156 return 0;
1160 static LRESULT
1161 DATETIME_KillFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1163 TRACE("lost focus to %p\n", lostFocus);
1165 if (infoPtr->haveFocus) {
1166 DATETIME_SendSimpleNotify (infoPtr, NM_KILLFOCUS);
1167 infoPtr->haveFocus = 0;
1168 DATETIME_SetSelectedField (infoPtr, -1);
1171 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
1173 return 0;
1177 static LRESULT
1178 DATETIME_NCCreate (HWND hwnd, const CREATESTRUCTW *lpcs)
1180 DWORD dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1181 /* force control to have client edge */
1182 dwExStyle |= WS_EX_CLIENTEDGE;
1183 SetWindowLongW(hwnd, GWL_EXSTYLE, dwExStyle);
1185 return DefWindowProcW(hwnd, WM_NCCREATE, 0, (LPARAM)lpcs);
1189 static LRESULT
1190 DATETIME_SetFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1192 TRACE("got focus from %p\n", lostFocus);
1194 /* if monthcal is open and it loses focus, close monthcal */
1195 if (infoPtr->hMonthCal && (lostFocus == infoPtr->hMonthCal) &&
1196 IsWindowVisible(infoPtr->hMonthCal))
1198 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
1199 DATETIME_SendSimpleNotify(infoPtr, DTN_CLOSEUP);
1200 /* note: this get triggered even if monthcal loses focus to a dropdown
1201 * box click, which occurs without an intermediate WM_PAINT call
1203 infoPtr->bDropdownEnabled = FALSE;
1204 return 0;
1207 if (infoPtr->haveFocus == 0) {
1208 DATETIME_SendSimpleNotify (infoPtr, NM_SETFOCUS);
1209 infoPtr->haveFocus = DTHT_GOTFOCUS;
1212 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1214 return 0;
1218 static BOOL
1219 DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr)
1221 NMDATETIMECHANGE dtdtc;
1223 dtdtc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1224 dtdtc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1225 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1227 dtdtc.dwFlags = infoPtr->dateValid ? GDT_VALID : GDT_NONE;
1229 dtdtc.st = infoPtr->date;
1230 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1231 dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1235 static BOOL
1236 DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code)
1238 NMHDR nmhdr;
1240 TRACE("%x\n", code);
1241 nmhdr.hwndFrom = infoPtr->hwndSelf;
1242 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1243 nmhdr.code = code;
1245 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1246 nmhdr.idFrom, (LPARAM)&nmhdr);
1249 static LRESULT
1250 DATETIME_Size (DATETIME_INFO *infoPtr, INT width, INT height)
1252 /* set size */
1253 infoPtr->rcClient.bottom = height;
1254 infoPtr->rcClient.right = width;
1256 TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1258 infoPtr->rcDraw = infoPtr->rcClient;
1260 if (infoPtr->dwStyle & DTS_UPDOWN) {
1261 SetWindowPos(infoPtr->hUpdown, NULL,
1262 infoPtr->rcClient.right-14, 0,
1263 15, infoPtr->rcClient.bottom - infoPtr->rcClient.top,
1264 SWP_NOACTIVATE | SWP_NOZORDER);
1266 else {
1267 /* set the size of the button that drops the calendar down */
1268 /* FIXME: account for style that allows button on left side */
1269 infoPtr->calbutton.top = infoPtr->rcDraw.top;
1270 infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1271 infoPtr->calbutton.left = infoPtr->rcDraw.right-15;
1272 infoPtr->calbutton.right = infoPtr->rcDraw.right;
1275 /* set enable/disable button size for show none style being enabled */
1276 /* FIXME: these dimensions are completely incorrect */
1277 infoPtr->checkbox.top = infoPtr->rcDraw.top;
1278 infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1279 infoPtr->checkbox.left = infoPtr->rcDraw.left;
1280 infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1282 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1284 return 0;
1287 static LRESULT
1288 DATETIME_StyleChanging(DATETIME_INFO *infoPtr, WPARAM wStyleType, STYLESTRUCT *lpss)
1290 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1291 wStyleType, lpss->styleOld, lpss->styleNew);
1293 /* block DTS_SHOWNONE change */
1294 if ((lpss->styleNew ^ lpss->styleOld) & DTS_SHOWNONE)
1296 if (lpss->styleOld & DTS_SHOWNONE)
1297 lpss->styleNew |= DTS_SHOWNONE;
1298 else
1299 lpss->styleNew &= ~DTS_SHOWNONE;
1302 return 0;
1305 static LRESULT
1306 DATETIME_StyleChanged(DATETIME_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
1308 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1309 wStyleType, lpss->styleOld, lpss->styleNew);
1311 if (wStyleType != GWL_STYLE) return 0;
1313 infoPtr->dwStyle = lpss->styleNew;
1315 if ( !(lpss->styleOld & DTS_SHOWNONE) && (lpss->styleNew & DTS_SHOWNONE) ) {
1316 infoPtr->hwndCheckbut = CreateWindowExW (0, WC_BUTTONW, 0, WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1317 2, 2, 13, 13, infoPtr->hwndSelf, 0,
1318 (HINSTANCE)GetWindowLongPtrW (infoPtr->hwndSelf, GWLP_HINSTANCE), 0);
1319 SendMessageW (infoPtr->hwndCheckbut, BM_SETCHECK, 1, 0);
1321 if ( (lpss->styleOld & DTS_SHOWNONE) && !(lpss->styleNew & DTS_SHOWNONE) ) {
1322 DestroyWindow(infoPtr->hwndCheckbut);
1323 infoPtr->hwndCheckbut = 0;
1325 if ( !(lpss->styleOld & DTS_UPDOWN) && (lpss->styleNew & DTS_UPDOWN) ) {
1326 infoPtr->hUpdown = CreateUpDownControl (WS_CHILD | WS_BORDER | WS_VISIBLE, 120, 1, 20, 20,
1327 infoPtr->hwndSelf, 1, 0, 0, UD_MAXVAL, UD_MINVAL, 0);
1329 if ( (lpss->styleOld & DTS_UPDOWN) && !(lpss->styleNew & DTS_UPDOWN) ) {
1330 DestroyWindow(infoPtr->hUpdown);
1331 infoPtr->hUpdown = 0;
1334 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1335 return 0;
1339 static LRESULT
1340 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1342 infoPtr->hFont = font;
1343 if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1344 return 0;
1348 static LRESULT
1349 DATETIME_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1351 DATETIME_INFO *infoPtr = Alloc (sizeof(DATETIME_INFO));
1352 STYLESTRUCT ss = { 0, lpcs->style };
1354 if (!infoPtr) return -1;
1356 infoPtr->hwndSelf = hwnd;
1357 infoPtr->dwStyle = lpcs->style;
1359 infoPtr->nrFieldsAllocated = 32;
1360 infoPtr->fieldspec = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1361 infoPtr->fieldRect = Alloc (infoPtr->nrFieldsAllocated * sizeof(RECT));
1362 infoPtr->buflen = Alloc (infoPtr->nrFieldsAllocated * sizeof(int));
1363 infoPtr->hwndNotify = lpcs->hwndParent;
1364 infoPtr->select = -1; /* initially, nothing is selected */
1365 infoPtr->bDropdownEnabled = TRUE;
1367 DATETIME_StyleChanged(infoPtr, GWL_STYLE, &ss);
1368 DATETIME_SetFormatW (infoPtr, 0);
1370 /* create the monthcal control */
1371 infoPtr->hMonthCal = CreateWindowExW (0, MONTHCAL_CLASSW, 0, WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS,
1372 0, 0, 0, 0, infoPtr->hwndSelf, 0, 0, 0);
1374 /* initialize info structure */
1375 GetLocalTime (&infoPtr->date);
1376 infoPtr->dateValid = TRUE;
1377 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1379 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1381 return 0;
1386 static LRESULT
1387 DATETIME_Destroy (DATETIME_INFO *infoPtr)
1389 if (infoPtr->hwndCheckbut)
1390 DestroyWindow(infoPtr->hwndCheckbut);
1391 if (infoPtr->hUpdown)
1392 DestroyWindow(infoPtr->hUpdown);
1393 if (infoPtr->hMonthCal)
1394 DestroyWindow(infoPtr->hMonthCal);
1395 SetWindowLongPtrW( infoPtr->hwndSelf, 0, 0 ); /* clear infoPtr */
1396 Free (infoPtr->buflen);
1397 Free (infoPtr->fieldRect);
1398 Free (infoPtr->fieldspec);
1399 Free (infoPtr);
1400 return 0;
1404 static INT
1405 DATETIME_GetText (const DATETIME_INFO *infoPtr, INT count, LPWSTR dst)
1407 WCHAR buf[80];
1408 int i;
1410 if (!dst || (count <= 0)) return 0;
1412 dst[0] = 0;
1413 for (i = 0; i < infoPtr->nrFields; i++)
1415 DATETIME_ReturnTxt(infoPtr, i, buf, sizeof(buf)/sizeof(buf[0]));
1416 if ((strlenW(dst) + strlenW(buf)) < count)
1417 strcatW(dst, buf);
1418 else break;
1420 return strlenW(dst);
1424 static LRESULT WINAPI
1425 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1427 DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1428 LRESULT ret;
1430 TRACE ("%x, %lx, %lx\n", uMsg, wParam, lParam);
1432 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
1433 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
1435 switch (uMsg) {
1437 case DTM_GETSYSTEMTIME:
1438 return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1440 case DTM_SETSYSTEMTIME:
1441 return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1443 case DTM_GETRANGE:
1444 ret = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1445 return ret ? ret : 1; /* bug emulation */
1447 case DTM_SETRANGE:
1448 return SendMessageW (infoPtr->hMonthCal, MCM_SETRANGE, wParam, lParam);
1450 case DTM_SETFORMATA:
1451 return DATETIME_SetFormatA (infoPtr, (LPCSTR)lParam);
1453 case DTM_SETFORMATW:
1454 return DATETIME_SetFormatW (infoPtr, (LPCWSTR)lParam);
1456 case DTM_GETMONTHCAL:
1457 return (LRESULT)infoPtr->hMonthCal;
1459 case DTM_SETMCCOLOR:
1460 return SendMessageW (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
1462 case DTM_GETMCCOLOR:
1463 return SendMessageW (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
1465 case DTM_SETMCFONT:
1466 return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1468 case DTM_GETMCFONT:
1469 return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1471 case WM_NOTIFY:
1472 return DATETIME_Notify (infoPtr, (LPNMHDR)lParam);
1474 case WM_ENABLE:
1475 return DATETIME_Enable (infoPtr, (BOOL)wParam);
1477 case WM_ERASEBKGND:
1478 return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1480 case WM_GETDLGCODE:
1481 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1483 case WM_PRINTCLIENT:
1484 case WM_PAINT:
1485 return DATETIME_Paint (infoPtr, (HDC)wParam);
1487 case WM_KEYDOWN:
1488 return DATETIME_KeyDown (infoPtr, wParam);
1490 case WM_CHAR:
1491 return DATETIME_Char (infoPtr, wParam);
1493 case WM_KILLFOCUS:
1494 return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1496 case WM_NCCREATE:
1497 return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1499 case WM_SETFOCUS:
1500 return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1502 case WM_SIZE:
1503 return DATETIME_Size (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1505 case WM_LBUTTONDOWN:
1506 return DATETIME_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1508 case WM_LBUTTONUP:
1509 return DATETIME_LButtonUp (infoPtr);
1511 case WM_VSCROLL:
1512 return DATETIME_VScroll (infoPtr, (WORD)wParam);
1514 case WM_CREATE:
1515 return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1517 case WM_DESTROY:
1518 return DATETIME_Destroy (infoPtr);
1520 case WM_COMMAND:
1521 return DATETIME_Command (infoPtr, wParam, lParam);
1523 case WM_STYLECHANGING:
1524 return DATETIME_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1526 case WM_STYLECHANGED:
1527 return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1529 case WM_SETFONT:
1530 return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1532 case WM_GETFONT:
1533 return (LRESULT) infoPtr->hFont;
1535 case WM_GETTEXT:
1536 return (LRESULT) DATETIME_GetText(infoPtr, wParam, (LPWSTR)lParam);
1538 case WM_SETTEXT:
1539 return CB_ERR;
1541 default:
1542 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
1543 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
1544 uMsg, wParam, lParam);
1545 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1550 void
1551 DATETIME_Register (void)
1553 WNDCLASSW wndClass;
1555 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1556 wndClass.style = CS_GLOBALCLASS;
1557 wndClass.lpfnWndProc = DATETIME_WindowProc;
1558 wndClass.cbClsExtra = 0;
1559 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1560 wndClass.hCursor = LoadCursorW (0, (LPCWSTR)IDC_ARROW);
1561 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1562 wndClass.lpszClassName = DATETIMEPICK_CLASSW;
1564 RegisterClassW (&wndClass);
1568 void
1569 DATETIME_Unregister (void)
1571 UnregisterClassW (DATETIMEPICK_CLASSW, NULL);