1 /*
2 * Date and time picker control
3 *
4 * Copyright 1998, 1999 Eric Kohl
5 * Copyright 1999, 2000 Alex Priem <alexp@sci.kun.nl>
6 * Copyright 2000 Chris Morgan <cmorgan@wpi.edu>
7 *
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.
12 *
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.
17 *
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
21 *
22 * NOTE
23 *
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.
26 *
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.
30 *
31 * TODO:
32 * -- DTS_APPCANPARSE
33 * -- DTS_SHORTDATECENTURYFORMAT
34 * -- DTN_FORMAT
35 * -- DTN_FORMATQUERY
36 * -- DTN_USERSTRING
37 * -- DTN_WMKEYDOWN
38 * -- FORMATCALLBACK
39 */
40
41 #include <math.h>
42 #include <string.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <limits.h>
46
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"
56
57 WINE_DEFAULT_DEBUG_CHANNEL(datetime);
58
59 typedef struct
60 {
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;
87
88 /* in monthcal.c */
89 extern int MONTHCAL_MonthLength(int month, int year);
90 extern int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace);
91
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.
96 *
97 */
98
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
125
126 #define DTHT_DATEFIELD 0xff /* for hit-testing */
127
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 */
133
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};
139
140
141 static DWORD
142 DATETIME_GetSystemTime (const DATETIME_INFO *infoPtr, SYSTEMTIME *systime)
143 {
144 if (!systime) return GDT_NONE;
145
146 if ((infoPtr->dwStyle & DTS_SHOWNONE) &&
147 (SendMessageW (infoPtr->hwndCheckbut, BM_GETCHECK, 0, 0) == BST_UNCHECKED))
148 return GDT_NONE;
149
150 *systime = infoPtr->date;
151
152 return GDT_VALID;
153 }
154
155
156 static BOOL
157 DATETIME_SetSystemTime (DATETIME_INFO *infoPtr, DWORD flag, const SYSTEMTIME *systime)
158 {
159 if (!systime) return 0;
160
161 TRACE("%04d/%02d/%02d %02d:%02d:%02d\n",
162 systime->wYear, systime->wMonth, systime->wDay,
163 systime->wHour, systime->wMinute, systime->wSecond);
164
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
173 )
174 return FALSE;
175
176 infoPtr->dateValid = TRUE;
177 infoPtr->date = *systime;
178 /* always store a valid day of week */
179 MONTHCAL_CalculateDayOfWeek(&infoPtr->date, TRUE);
180
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);
186 }
187 else
188 return FALSE;
189
190 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
191 return TRUE;
192 }
193
194
195 /***
196 * Split up a formattxt in actions.
197 * See ms documentation for the meaning of the letter codes/'specifiers'.
198 *
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'.
204 *
205 */
206 static void
207 DATETIME_UseFormat (DATETIME_INFO *infoPtr, LPCWSTR formattxt)
208 {
209 unsigned int i;
210 int j, k, len;
211 BOOL inside_literal = FALSE; /* inside '...' */
212 int *nrFields = &infoPtr->nrFields;
213
214 *nrFields = 0;
215 infoPtr->fieldspec[*nrFields] = 0;
216 len = strlenW(allowedformatchars);
217 k = 0;
218
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;
228 }
229 if (infoPtr->fieldspec[*nrFields] >> 4 != j) {
230 (*nrFields)++;
231 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
232 break;
233 }
234 if ((infoPtr->fieldspec[*nrFields] & 0x0f) == maxrepetition[j]) {
235 (*nrFields)++;
236 infoPtr->fieldspec[*nrFields] = (j<<4) + 1;
237 break;
238 }
239 infoPtr->fieldspec[*nrFields]++;
240 break;
241 } /* if allowedformatchar */
242 } /* for j */
243 }
244 else
245 j = len;
246
247 if (formattxt[i] == '\'')
248 {
249 inside_literal = !inside_literal;
250 continue;
251 }
252
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;
262 }
263 infoPtr->textbuf[k] = formattxt[i];
264 k++;
265 infoPtr->buflen[*nrFields]++;
266 } /* if j=len */
267
268 if (*nrFields == infoPtr->nrFieldsAllocated) {
269 FIXME ("out of memory; should reallocate. crash ahead.\n");
270 }
271 } /* for i */
272
273 TRACE("\n");
274
275 if (infoPtr->fieldspec[*nrFields] != 0) (*nrFields)++;
276 }
277
278
279 static BOOL
280 DATETIME_SetFormatW (DATETIME_INFO *infoPtr, LPCWSTR lpszFormat)
281 {
282 if (!lpszFormat) {
283 WCHAR format_buf[80];
284 DWORD format_item;
285
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;
294 }
295
296 DATETIME_UseFormat (infoPtr, lpszFormat);
297 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
298
299 return 1;
300 }
301
302
303 static BOOL
304 DATETIME_SetFormatA (DATETIME_INFO *infoPtr, LPCSTR lpszFormat)
305 {
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;
314 }
315 else
316 return DATETIME_SetFormatW (infoPtr, 0);
317
318 }
319
320
321 static void
322 DATETIME_ReturnTxt (const DATETIME_INFO *infoPtr, int count, LPWSTR result, int resultSize)
323 {
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];
330
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;
336 }
337
338 if (!infoPtr->fieldspec) return;
339
340 spec = infoPtr->fieldspec[count];
341 if (spec & DT_STRING) {
342 int txtlen = infoPtr->buflen[count];
343
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;
350 }
351
352
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;
375 }
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;
384 }
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;
445 }
446
447 TRACE ("arg%d=%x->[%s]\n", count, infoPtr->fieldspec[count], debugstr_w(result));
448 }
449
450 static int wrap(int val, int delta, int minVal, int maxVal)
451 {
452 val += delta;
453 if (delta == INT_MIN || val < minVal) return maxVal;
454 if (delta == INT_MAX || val > maxVal) return minVal;
455 return val;
456 }
457
458 static void
459 DATETIME_IncreaseField (DATETIME_INFO *infoPtr, int number, int delta)
460 {
461 SYSTEMTIME *date = &infoPtr->date;
462
463 TRACE ("%d\n", number);
464 if ((number > infoPtr->nrFields) || (number < 0)) return;
465
466 if ((infoPtr->fieldspec[number] & DTHT_DATEFIELD) == 0) return;
467
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;
511 }
512
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.
517 */
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;
525 }
526 }
527
528
529 static void
530 DATETIME_ReturnFieldWidth (const DATETIME_INFO *infoPtr, HDC hdc, int count, SHORT *width)
531 {
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;
543
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;
548 }
549
550 if (!infoPtr->fieldspec) return;
551
552 spec = infoPtr->fieldspec[count];
553 if (spec & DT_STRING) {
554 int txtlen = infoPtr->buflen[count];
555
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;
561 }
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:
589 {
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};
594
595 const WCHAR *fall;
596 LCTYPE lctype;
597 INT i, max_count;
598 LONG cx;
599
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;
622 }
623
624 cx = 0;
625 for (i = 0; i < max_count; i++)
626 {
627 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, lctype + i,
628 buffer, lstrlenW(buffer)))
629 {
630 GetTextExtentPoint32W(hdc, buffer, lstrlenW(buffer), &size);
631 if (size.cx > cx) cx = size.cx;
632 }
633 else /* locale independent fallback on failure */
634 {
635 GetTextExtentPoint32W(hdc, fall, lstrlenW(fall), &size);
636 cx = size.cx;
637 break;
638 }
639 }
640 *width = cx;
641 return;
642 }
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;
652 }
653 }
654 GetTextExtentPoint32W (hdc, bufptr, strlenW(bufptr), &size);
655 *width = size.cx;
656 }
657
658 static void
659 DATETIME_Refresh (DATETIME_INFO *infoPtr, HDC hdc)
660 {
661 TRACE("\n");
662
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];
673
674 DATETIME_ReturnTxt (infoPtr, 0, txt, sizeof(txt)/sizeof(txt[0]));
675 GetTextExtentPoint32W (hdc, txt, strlenW(txt), &size);
676 rcDraw->bottom = size.cy + 2;
677
678 prevright = infoPtr->checkbox.right = ((infoPtr->dwStyle & DTS_SHOWNONE) ? 18 : 2);
679
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;
690
691 if (infoPtr->dwStyle & WS_DISABLED)
692 oldTextColor = SetTextColor (hdc, comctl32_color.clrGrayText);
693 else if ((infoPtr->haveFocus) && (i == infoPtr->select)) {
694 RECT selection;
695
696 /* fill if focused */
697 HBRUSH hbr = CreateSolidBrush (comctl32_color.clrActiveCaption);
698
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);
706
707 FillRect(hdc, &selection, hbr);
708 DeleteObject (hbr);
709 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindow);
710 }
711 else
712 oldTextColor = SetTextColor (hdc, comctl32_color.clrWindowText);
713
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);
717 }
718 SetBkMode (hdc, oldBkMode);
719 SelectObject (hdc, oldFont);
720 }
721
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) );
726 }
727 }
728
729
730 static INT
731 DATETIME_HitTest (const DATETIME_INFO *infoPtr, POINT pt)
732 {
733 int i;
734
735 TRACE ("%d, %d\n", pt.x, pt.y);
736
737 if (PtInRect (&infoPtr->calbutton, pt)) return DTHT_MCPOPUP;
738 if (PtInRect (&infoPtr->checkbox, pt)) return DTHT_CHECKBOX;
739
740 for (i = 0; i < infoPtr->nrFields; i++) {
741 if (PtInRect (&infoPtr->fieldRect[i], pt)) return i;
742 }
743
744 return DTHT_NONE;
745 }
746
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)
750 {
751 for(--i; i >= 0; i--)
752 {
753 if (infoPtr->fieldspec[i] & DTHT_DATEFIELD) return i;
754 }
755 return -1;
756 }
757
758 static LRESULT
759 DATETIME_LButtonDown (DATETIME_INFO *infoPtr, INT x, INT y)
760 {
761 POINT pt;
762 int old, new;
763
764 pt.x = x;
765 pt.y = y;
766 old = infoPtr->select;
767 new = DATETIME_HitTest (infoPtr, pt);
768
769 SetFocus(infoPtr->hwndSelf);
770
771 if (!(new & DTHT_NODATEMASK) || (new == DTHT_NONE))
772 {
773 if (new == DTHT_NONE)
774 new = infoPtr->nrFields - 1;
775 else
776 {
777 /* hitting string part moves selection to next date field to left */
778 if (infoPtr->fieldspec[new] & DT_STRING)
779 {
780 new = DATETIME_GetPrevDateField(infoPtr, new);
781 if (new == -1) return 0;
782 }
783 /* never select full day of week */
784 if (infoPtr->fieldspec[new] == FULLDAY) return 0;
785 }
786 }
787 infoPtr->select = new;
788
789 if (infoPtr->select == DTHT_MCPOPUP) {
790 RECT rcMonthCal;
791 SendMessageW(infoPtr->hMonthCal, MCM_GETMINREQRECT, 0, (LPARAM)&rcMonthCal);
792
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;
796
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;
804
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);
810
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));
820
821 if (infoPtr->bDropdownEnabled) {
822 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
823 DATETIME_SendSimpleNotify (infoPtr, DTN_DROPDOWN);
824 }
825 infoPtr->bDropdownEnabled = TRUE;
826 }
827
828 TRACE ("dt:%p mc:%p mc parent:%p, desktop:%p\n",
829 infoPtr->hwndSelf, infoPtr->hMonthCal, infoPtr->hwndNotify, GetDesktopWindow ());
830 }
831
832 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
833
834 return 0;
835 }
836
837
838 static LRESULT
839 DATETIME_LButtonUp (DATETIME_INFO *infoPtr)
840 {
841 if(infoPtr->bCalDepressed) {
842 infoPtr->bCalDepressed = FALSE;
843 InvalidateRect(infoPtr->hwndSelf, &(infoPtr->calbutton), TRUE);
844 }
845
846 return 0;
847 }
848
849
850 static LRESULT
851 DATETIME_Paint (DATETIME_INFO *infoPtr, HDC hdc)
852 {
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);
860 }
861
862 /* Not a click on the dropdown box, enabled it */
863 infoPtr->bDropdownEnabled = TRUE;
864
865 return 0;
866 }
867
868
869 static LRESULT
870 DATETIME_Button_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
871 {
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);
876 }
877 return 0;
878 }
879
880
881
882 static LRESULT
883 DATETIME_Command (DATETIME_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
884 {
885 TRACE("hwndbutton = %p\n", infoPtr->hwndCheckbut);
886 if(infoPtr->hwndCheckbut == (HWND)lParam)
887 return DATETIME_Button_Command(infoPtr, wParam, lParam);
888 return 0;
889 }
890
891
892 static LRESULT
893 DATETIME_Enable (DATETIME_INFO *infoPtr, BOOL bEnable)
894 {
895 TRACE("%p %s\n", infoPtr, bEnable ? "TRUE" : "FALSE");
896 if (bEnable)
897 infoPtr->dwStyle &= ~WS_DISABLED;
898 else
899 infoPtr->dwStyle |= WS_DISABLED;
900
901 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
902
903 return 0;
904 }
905
906
907 static LRESULT
908 DATETIME_EraseBackground (const DATETIME_INFO *infoPtr, HDC hdc)
909 {
910 HBRUSH hBrush, hSolidBrush = NULL;
911 RECT rc;
912
913 if (infoPtr->dwStyle & WS_DISABLED)
914 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrBtnFace);
915 else
916 {
917 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLOREDIT,
918 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
919 if (!hBrush)
920 hBrush = hSolidBrush = CreateSolidBrush(comctl32_color.clrWindow);
921 }
922
923 GetClientRect (infoPtr->hwndSelf, &rc);
924
925 FillRect (hdc, &rc, hBrush);
926
927 if (hSolidBrush)
928 DeleteObject(hSolidBrush);
929
930 return -1;
931 }
932
933
934 static LRESULT
935 DATETIME_Notify (DATETIME_INFO *infoPtr, const NMHDR *lpnmh)
936 {
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);
939
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);
950 }
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;
955 }
956 return 0;
957 }
958
959
960 static LRESULT
961 DATETIME_KeyDown (DATETIME_INFO *infoPtr, DWORD vkCode)
962 {
963 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
964 int wrap = 0;
965
966 if (!(infoPtr->haveFocus)) return 0;
967 if ((fieldNum==0) && (infoPtr->select)) return 0;
968
969 if (infoPtr->select & FORMATCALLMASK) {
970 FIXME ("Callbacks not implemented yet\n");
971 }
972
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--;
999 }
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++;
1008 }
1009 } while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
1010 break;
1011 }
1012
1013 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1014
1015 return 0;
1016 }
1017
1018
1019 static LRESULT
1020 DATETIME_Char (DATETIME_INFO *infoPtr, WPARAM vkCode)
1021 {
1022 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1023
1024 if (vkCode >= '' && vkCode <= '9') {
1025 int num = vkCode-'';
1026 int newDays;
1027
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;
1098 }
1099 }
1100 return 0;
1101 }
1102
1103
1104 static LRESULT
1105 DATETIME_VScroll (DATETIME_INFO *infoPtr, WORD wScroll)
1106 {
1107 int fieldNum = infoPtr->select & DTHT_DATEFIELD;
1108
1109 if ((SHORT)LOWORD(wScroll) != SB_THUMBPOSITION) return 0;
1110 if (!(infoPtr->haveFocus)) return 0;
1111 if ((fieldNum==0) && (infoPtr->select)) return 0;
1112
1113 if (infoPtr->pendingUpdown >= 0) {
1114 DATETIME_IncreaseField (infoPtr, fieldNum, 1);
1115 DATETIME_SendDateTimeChangeNotify (infoPtr);
1116 }
1117 else {
1118 DATETIME_IncreaseField (infoPtr, fieldNum, -1);
1119 DATETIME_SendDateTimeChangeNotify (infoPtr);
1120 }
1121
1122 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1123
1124 return 0;
1125 }
1126
1127
1128 static LRESULT
1129 DATETIME_KillFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1130 {
1131 TRACE("lost focus to %p\n", lostFocus);
1132
1133 if (infoPtr->haveFocus) {
1134 DATETIME_SendSimpleNotify (infoPtr, NM_KILLFOCUS);
1135 infoPtr->haveFocus = 0;
1136 }
1137
1138 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
1139
1140 return 0;
1141 }
1142
1143
1144 static LRESULT
1145 DATETIME_NCCreate (HWND hwnd, const CREATESTRUCTW *lpcs)
1146 {
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);
1151
1152 return DefWindowProcW(hwnd, WM_NCCREATE, 0, (LPARAM)lpcs);
1153 }
1154
1155
1156 static LRESULT
1157 DATETIME_SetFocus (DATETIME_INFO *infoPtr, HWND lostFocus)
1158 {
1159 TRACE("got focus from %p\n", lostFocus);
1160
1161 /* if monthcal is open and it loses focus, close monthcal */
1162 if (infoPtr->hMonthCal && (lostFocus == infoPtr->hMonthCal) &&
1163 IsWindowVisible(infoPtr->hMonthCal))
1164 {
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
1169 */
1170 infoPtr->bDropdownEnabled = FALSE;
1171 return 0;
1172 }
1173
1174 if (infoPtr->haveFocus == 0) {
1175 DATETIME_SendSimpleNotify (infoPtr, NM_SETFOCUS);
1176 infoPtr->haveFocus = DTHT_GOTFOCUS;
1177 }
1178
1179 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1180
1181 return 0;
1182 }
1183
1184
1185 static BOOL
1186 DATETIME_SendDateTimeChangeNotify (const DATETIME_INFO *infoPtr)
1187 {
1188 NMDATETIMECHANGE dtdtc;
1189
1190 dtdtc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1191 dtdtc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1192 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1193
1194 dtdtc.dwFlags = infoPtr->dateValid ? GDT_VALID : GDT_NONE;
1195
1196 dtdtc.st = infoPtr->date;
1197 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1198 dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1199 }
1200
1201
1202 static BOOL
1203 DATETIME_SendSimpleNotify (const DATETIME_INFO *infoPtr, UINT code)
1204 {
1205 NMHDR nmhdr;
1206
1207 TRACE("%x\n", code);
1208 nmhdr.hwndFrom = infoPtr->hwndSelf;
1209 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1210 nmhdr.code = code;
1211
1212 return (BOOL) SendMessageW (infoPtr->hwndNotify, WM_NOTIFY,
1213 nmhdr.idFrom, (LPARAM)&nmhdr);
1214 }
1215
1216 static LRESULT
1217 DATETIME_Size (DATETIME_INFO *infoPtr, INT width, INT height)
1218 {
1219 /* set size */
1220 infoPtr->rcClient.bottom = height;
1221 infoPtr->rcClient.right = width;
1222
1223 TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1224
1225 infoPtr->rcDraw = infoPtr->rcClient;
1226
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);
1232 }
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;
1240 }
1241
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;
1248
1249 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1250
1251 return 0;
1252 }
1253
1254 static LRESULT
1255 DATETIME_StyleChanging(DATETIME_INFO *infoPtr, WPARAM wStyleType, STYLESTRUCT *lpss)
1256 {
1257 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1258 wStyleType, lpss->styleOld, lpss->styleNew);
1259
1260 /* block DTS_SHOWNONE change */
1261 if ((lpss->styleNew ^ lpss->styleOld) & DTS_SHOWNONE)
1262 {
1263 if (lpss->styleOld & DTS_SHOWNONE)
1264 lpss->styleNew |= DTS_SHOWNONE;
1265 else
1266 lpss->styleNew &= ~DTS_SHOWNONE;
1267 }
1268
1269 return 0;
1270 }
1271
1272 static LRESULT
1273 DATETIME_StyleChanged(DATETIME_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
1274 {
1275 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1276 wStyleType, lpss->styleOld, lpss->styleNew);
1277
1278 if (wStyleType != GWL_STYLE) return 0;
1279
1280 infoPtr->dwStyle = lpss->styleNew;
1281
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);
1287 }
1288 if ( (lpss->styleOld & DTS_SHOWNONE) && !(lpss->styleNew & DTS_SHOWNONE) ) {
1289 DestroyWindow(infoPtr->hwndCheckbut);
1290 infoPtr->hwndCheckbut = 0;
1291 }
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);
1295 }
1296 if ( (lpss->styleOld & DTS_UPDOWN) && !(lpss->styleNew & DTS_UPDOWN) ) {
1297 DestroyWindow(infoPtr->hUpdown);
1298 infoPtr->hUpdown = 0;
1299 }
1300
1301 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1302 return 0;
1303 }
1304
1305
1306 static LRESULT
1307 DATETIME_SetFont (DATETIME_INFO *infoPtr, HFONT font, BOOL repaint)
1308 {
1309 infoPtr->hFont = font;
1310 if (repaint) InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1311 return 0;
1312 }
1313
1314
1315 static LRESULT
1316 DATETIME_Create (HWND hwnd, const CREATESTRUCTW *lpcs)
1317 {
1318 DATETIME_INFO *infoPtr = Alloc (sizeof(DATETIME_INFO));
1319 STYLESTRUCT ss = { 0, lpcs->style };
1320
1321 if (!infoPtr) return -1;
1322
1323 infoPtr->hwndSelf = hwnd;
1324 infoPtr->dwStyle = lpcs->style;
1325
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;
1333
1334 DATETIME_StyleChanged(infoPtr, GWL_STYLE, &ss);
1335 DATETIME_SetFormatW (infoPtr, 0);
1336
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);
1340
1341 /* initialize info structure */
1342 GetLocalTime (&infoPtr->date);
1343 infoPtr->dateValid = TRUE;
1344 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1345
1346 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1347
1348 return 0;
1349 }
1350
1351
1352
1353 static LRESULT
1354 DATETIME_Destroy (DATETIME_INFO *infoPtr)
1355 {
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);
1364 return 0;
1365 }
1366
1367
1368 static LRESULT WINAPI
1369 DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1370 {
1371 DATETIME_INFO *infoPtr = ((DATETIME_INFO *)GetWindowLongPtrW (hwnd, 0));
1372 LRESULT ret;
1373
1374 TRACE ("%x, %lx, %lx\n", uMsg, wParam, lParam);
1375
1376 if (!infoPtr && (uMsg != WM_CREATE) && (uMsg != WM_NCCREATE))
1377 return DefWindowProcW( hwnd, uMsg, wParam, lParam );
1378
1379 switch (uMsg) {
1380
1381 case DTM_GETSYSTEMTIME:
1382 return DATETIME_GetSystemTime (infoPtr, (SYSTEMTIME *) lParam);
1383
1384 case DTM_SETSYSTEMTIME:
1385 return DATETIME_SetSystemTime (infoPtr, wParam, (SYSTEMTIME *) lParam);
1386
1387 case DTM_GETRANGE:
1388 ret = SendMessageW (infoPtr->hMonthCal, MCM_GETRANGE, wParam, lParam);
1389 return ret ? ret : 1; /* bug emulation */
1390
1391 case DTM_SETRANGE:
1392 return SendMessageW (infoPtr->hMonthCal, MCM_SETRANGE, wParam, lParam);
1393
1394 case DTM_SETFORMATA:
1395 return DATETIME_SetFormatA (infoPtr, (LPCSTR)lParam);
1396
1397 case DTM_SETFORMATW:
1398 return DATETIME_SetFormatW (infoPtr, (LPCWSTR)lParam);
1399
1400 case DTM_GETMONTHCAL:
1401 return (LRESULT)infoPtr->hMonthCal;
1402
1403 case DTM_SETMCCOLOR:
1404 return SendMessageW (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
1405
1406 case DTM_GETMCCOLOR:
1407 return SendMessageW (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
1408
1409 case DTM_SETMCFONT:
1410 return SendMessageW (infoPtr->hMonthCal, WM_SETFONT, wParam, lParam);
1411
1412 case DTM_GETMCFONT:
1413 return SendMessageW (infoPtr->hMonthCal, WM_GETFONT, wParam, lParam);
1414
1415 case WM_NOTIFY:
1416 return DATETIME_Notify (infoPtr, (LPNMHDR)lParam);
1417
1418 case WM_ENABLE:
1419 return DATETIME_Enable (infoPtr, (BOOL)wParam);
1420
1421 case WM_ERASEBKGND:
1422 return DATETIME_EraseBackground (infoPtr, (HDC)wParam);
1423
1424 case WM_GETDLGCODE:
1425 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1426
1427 case WM_PRINTCLIENT:
1428 case WM_PAINT:
1429 return DATETIME_Paint (infoPtr, (HDC)wParam);
1430
1431 case WM_KEYDOWN:
1432 return DATETIME_KeyDown (infoPtr, wParam);
1433
1434 case WM_CHAR:
1435 return DATETIME_Char (infoPtr, wParam);
1436
1437 case WM_KILLFOCUS:
1438 return DATETIME_KillFocus (infoPtr, (HWND)wParam);
1439
1440 case WM_NCCREATE:
1441 return DATETIME_NCCreate (hwnd, (LPCREATESTRUCTW)lParam);
1442
1443 case WM_SETFOCUS:
1444 return DATETIME_SetFocus (infoPtr, (HWND)wParam);
1445
1446 case WM_SIZE:
1447 return DATETIME_Size (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1448
1449 case WM_LBUTTONDOWN:
1450 return DATETIME_LButtonDown (infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1451
1452 case WM_LBUTTONUP:
1453 return DATETIME_LButtonUp (infoPtr);
1454
1455 case WM_VSCROLL:
1456 return DATETIME_VScroll (infoPtr, (WORD)wParam);
1457
1458 case WM_CREATE:
1459 return DATETIME_Create (hwnd, (LPCREATESTRUCTW)lParam);
1460
1461 case WM_DESTROY:
1462 return DATETIME_Destroy (infoPtr);
1463
1464 case WM_COMMAND:
1465 return DATETIME_Command (infoPtr, wParam, lParam);
1466
1467 case WM_STYLECHANGING:
1468 return DATETIME_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1469
1470 case WM_STYLECHANGED:
1471 return DATETIME_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
1472
1473 case WM_SETFONT:
1474 return DATETIME_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1475
1476 case WM_GETFONT:
1477 return (LRESULT) infoPtr->hFont;
1478
1479 case WM_SETTEXT:
1480 return CB_ERR;
1481
1482 default:
1483 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
1484 ERR("unknown msg %04x wp=%08lx lp=%08lx\n",
1485 uMsg, wParam, lParam);
1486 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
1487 }
1488 }
1489
1490
1491 void
1492 DATETIME_Register (void)
1493 {
1494 WNDCLASSW wndClass;
1495
1496 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1497 wndClass.style = CS_GLOBALCLASS;
1498 wndClass.lpfnWndProc = DATETIME_WindowProc;
1499 wndClass.cbClsExtra = 0;
1500 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1501 wndClass.hCursor = LoadCursorW (0, (LPCWSTR)IDC_ARROW);
1502 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1503 wndClass.lpszClassName = DATETIMEPICK_CLASSW;
1504
1505 RegisterClassW (&wndClass);
1506 }
1507
1508
1509 void
1510 DATETIME_Unregister (void)
1511 {
1512 UnregisterClassW (DATETIMEPICK_CLASSW, NULL);
1513 }
1514
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.