source: trunk/src/comctl32/datetime.c@ 4611

Last change on this file since 4611 was 4611, checked in by sandervl, 25 years ago

updated animate, monthcal, flatsb, datetime + comboex controls with latest wine (2000-12-11)

File size: 31.0 KB
Line 
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 *
9 * TODO:
10 * - All messages.
11 * - All notifications.
12 *
13 */
14
15#include <math.h>
16#include <string.h>
17#include <stdio.h>
18
19#include "winbase.h"
20#include "wingdi.h"
21#include "wine/winestring.h"
22#include "commctrl.h"
23#include "debugtools.h"
24
25DEFAULT_DEBUG_CHANNEL(datetime);
26
27typedef struct
28{
29 HWND hMonthCal;
30 HWND hUpdown;
31 SYSTEMTIME date;
32 BOOL dateValid;
33 HWND hwndCheckbut;
34 RECT rcClient; /* rect around the edge of the window */
35 RECT rcDraw; /* rect inside of the border */
36 RECT checkbox; /* checkbox allowing the control to be enabled/disabled */
37 RECT calbutton; /* button that toggles the dropdown of the monthcal control */
38 BOOL bCalDepressed; /* TRUE = cal button is depressed */
39 int select;
40 HFONT hFont;
41 int nrFieldsAllocated;
42 int nrFields;
43 int haveFocus;
44 int *fieldspec;
45 RECT *fieldRect;
46 int *buflen;
47 char textbuf[256];
48 POINT monthcal_pos;
49} DATETIME_INFO, *LPDATETIME_INFO;
50
51/* in monthcal.c */
52extern int MONTHCAL_MonthLength(int month, int year);
53
54/* this list of defines is closely related to `allowedformatchars' defined
55 * in datetime.c; the high nibble indicates the `base type' of the format
56 * specifier.
57 * Do not change without first reading DATETIME_UseFormat.
58 *
59 */
60
61#define DT_END_FORMAT 0
62#define ONEDIGITDAY 0x01
63#define TWODIGITDAY 0x02
64#define THREECHARDAY 0x03
65#define FULLDAY 0x04
66#define ONEDIGIT12HOUR 0x11
67#define TWODIGIT12HOUR 0x12
68#define ONEDIGIT24HOUR 0x21
69#define TWODIGIT24HOUR 0x22
70#define ONEDIGITMINUTE 0x31
71#define TWODIGITMINUTE 0x32
72#define ONEDIGITMONTH 0x41
73#define TWODIGITMONTH 0x42
74#define THREECHARMONTH 0x43
75#define FULLMONTH 0x44
76#define ONEDIGITSECOND 0x51
77#define TWODIGITSECOND 0x52
78#define ONELETTERAMPM 0x61
79#define TWOLETTERAMPM 0x62
80#define ONEDIGITYEAR 0x71
81#define TWODIGITYEAR 0x72
82#define FULLYEAR 0x73
83#define FORMATCALLBACK 0x81 /* -> maximum of 0x80 callbacks possible */
84#define FORMATCALLMASK 0x80
85#define DT_STRING 0x0100
86
87#define DTHT_DATEFIELD 0xff /* for hit-testing */
88
89#define DTHT_NONE 0
90#define DTHT_CHECKBOX 0x200 /* these should end at '00' , to make */
91#define DTHT_MCPOPUP 0x300 /* & DTHT_DATEFIELD 0 when DATETIME_KeyDown */
92#define DTHT_GOTFOCUS 0x400 /* tests for date-fields */
93
94#define DATETIME_GetInfoPtr(hwnd) ((DATETIME_INFO *)GetWindowLongA (hwnd, 0))
95
96static BOOL DATETIME_SendSimpleNotify (HWND hwnd, UINT code);
97static BOOL DATETIME_SendDateTimeChangeNotify (HWND hwnd);
98extern void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to);
99static const char *allowedformatchars = {"dhHmMstyX'"};
100static const int maxrepetition [] = {4,2,2,2,4,2,2,3,-1,-1};
101
102
103static LRESULT
104DATETIME_GetSystemTime (HWND hwnd, WPARAM wParam, LPARAM lParam )
105{
106 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
107 DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
108 SYSTEMTIME *lprgSysTimeArray=(SYSTEMTIME *) lParam;
109
110 TRACE("%04x %08lx\n",wParam,lParam);
111 if (!lParam) return GDT_NONE;
112
113 if ((dwStyle & DTS_SHOWNONE) &&
114 (SendMessageA (infoPtr->hwndCheckbut, BM_GETCHECK, 0, 0)))
115 return GDT_NONE;
116
117 MONTHCAL_CopyTime (&infoPtr->date, lprgSysTimeArray);
118
119 return GDT_VALID;
120}
121
122
123static LRESULT
124DATETIME_SetSystemTime (HWND hwnd, WPARAM wParam, LPARAM lParam )
125{
126 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
127 SYSTEMTIME *lprgSysTimeArray=(SYSTEMTIME *) lParam;
128
129 TRACE("%04x %08lx\n",wParam,lParam);
130 if (!lParam) return 0;
131
132 if (lParam==GDT_VALID)
133 MONTHCAL_CopyTime (lprgSysTimeArray, &infoPtr->date);
134 if (lParam==GDT_NONE) {
135 infoPtr->dateValid=FALSE;
136 SendMessageA (infoPtr->hwndCheckbut, BM_SETCHECK, 0, 0);
137 }
138 return 1;
139}
140
141
142static LRESULT
143DATETIME_GetMonthCalColor (HWND hwnd, WPARAM wParam)
144{
145 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
146
147 TRACE("%04x\n",wParam);
148 return SendMessageA (infoPtr->hMonthCal, MCM_GETCOLOR, wParam, 0);
149}
150
151
152static LRESULT
153DATETIME_SetMonthCalColor (HWND hwnd, WPARAM wParam, LPARAM lParam)
154{
155 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
156
157 TRACE("%04x %08lx\n",wParam,lParam);
158 return SendMessageA (infoPtr->hMonthCal, MCM_SETCOLOR, wParam, lParam);
159}
160
161
162/* FIXME: need to get way to force font into monthcal structure */
163static LRESULT
164DATETIME_GetMonthCal (HWND hwnd)
165{
166 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
167
168 TRACE("\n");
169 return infoPtr->hMonthCal;
170}
171
172
173
174/* FIXME: need to get way to force font into monthcal structure */
175
176static LRESULT
177DATETIME_GetMonthCalFont (HWND hwnd)
178{
179
180 TRACE("\n");
181 return 0;
182}
183
184
185static LRESULT
186DATETIME_SetMonthCalFont (HWND hwnd, WPARAM wParam, LPARAM lParam)
187{
188
189 TRACE("%04x %08lx\n",wParam,lParam);
190 return 0;
191}
192
193
194/*
195 Split up a formattxt in actions.
196 See ms documentation for the meaning of the letter codes/'specifiers'.
197
198 Notes:
199 *'dddddd' is handled as 'dddd' plus 'dd'.
200 *unrecognized formats are strings (here given the type DT_STRING;
201 start of the string is encoded in lower bits of DT_STRING.
202 Therefore, 'string' ends finally up as '<show seconds>tring'.
203
204 */
205
206
207static void
208DATETIME_UseFormat (DATETIME_INFO *infoPtr, const char *formattxt)
209{
210 int i,j,k,len;
211 int *nrFields=& infoPtr->nrFields;
212
213 TRACE ("%s\n",formattxt);
214
215
216 *nrFields=0;
217 infoPtr->fieldspec[*nrFields]=0;
218 len=strlen(allowedformatchars);
219 k=0;
220
221 for (i=0; i<strlen (formattxt); i++) {
222 TRACE ("\n%d %c:",i, formattxt[i]);
223 for (j=0; j<len; j++) {
224 if (allowedformatchars[j]==formattxt[i]) {
225 TRACE ("%c[%d,%x]",allowedformatchars[j], *nrFields,
226 infoPtr->fieldspec[*nrFields]);
227 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
228 infoPtr->fieldspec[*nrFields]=(j<<4) +1;
229 break;
230 }
231 if (infoPtr->fieldspec[*nrFields]>>4!=j) {
232 (*nrFields)++;
233 infoPtr->fieldspec[*nrFields]=(j<<4) +1;
234 break;
235 }
236 if ((infoPtr->fieldspec[*nrFields] & 0x0f)==maxrepetition[j]) {
237 (*nrFields)++;
238 infoPtr->fieldspec[*nrFields]=(j<<4) +1;
239 break;
240 }
241 infoPtr->fieldspec[*nrFields]++;
242 break;
243 } /* if allowedformatchar */
244 } /* for j */
245
246
247 /* char is not a specifier: handle char like a string */
248 if (j==len) {
249 if ((*nrFields==0) && (infoPtr->fieldspec[*nrFields]==0)) {
250 infoPtr->fieldspec[*nrFields]=DT_STRING+k;
251 infoPtr->buflen[*nrFields]=0;
252 } else
253 if ((infoPtr->fieldspec[*nrFields] & DT_STRING)!=DT_STRING) {
254 (*nrFields)++;
255 infoPtr->fieldspec[*nrFields]=DT_STRING+k;
256 infoPtr->buflen[*nrFields]=0;
257 }
258 infoPtr->textbuf[k]=formattxt[i];
259 k++;
260 infoPtr->buflen[*nrFields]++;
261 } /* if j=len */
262
263 if (*nrFields==infoPtr->nrFieldsAllocated) {
264 FIXME ("out of memory; should reallocate. crash ahead.\n");
265 }
266
267 } /* for i */
268
269 TRACE("\n");
270
271 if (infoPtr->fieldspec[*nrFields]!=0) (*nrFields)++;
272}
273
274
275static LRESULT
276DATETIME_SetFormat (HWND hwnd, WPARAM wParam, LPARAM lParam)
277{
278 DATETIME_INFO *infoPtr= DATETIME_GetInfoPtr (hwnd);
279 char format_buf[80];
280 DWORD format_item;
281
282 TRACE("%04x %08lx\n",wParam,lParam);
283 if (!lParam) {
284 DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
285
286 if (dwStyle & DTS_LONGDATEFORMAT)
287 format_item=LOCALE_SLONGDATE;
288 else if (dwStyle & DTS_TIMEFORMAT)
289 format_item=LOCALE_STIMEFORMAT;
290 else /* DTS_SHORTDATEFORMAT */
291 format_item=LOCALE_SSHORTDATE;
292 GetLocaleInfoA( GetSystemDefaultLCID(), format_item,format_buf,sizeof(format_buf));
293 DATETIME_UseFormat (infoPtr, format_buf);
294 }
295 else
296 DATETIME_UseFormat (infoPtr, (char *) lParam);
297
298 return infoPtr->nrFields;
299}
300
301
302static LRESULT
303DATETIME_SetFormatW (HWND hwnd, WPARAM wParam, LPARAM lParam)
304
305{
306 TRACE("%04x %08lx\n",wParam,lParam);
307 if (lParam) {
308 LPSTR buf;
309 int retval;
310 int len = lstrlenW ((LPWSTR) lParam)+1;
311
312 buf = (LPSTR) COMCTL32_Alloc (len);
313 lstrcpyWtoA (buf, (LPWSTR) lParam);
314 retval=DATETIME_SetFormat (hwnd, 0, (LPARAM) buf);
315 COMCTL32_Free (buf);
316 return retval;
317 }
318 else
319 return DATETIME_SetFormat (hwnd, 0, 0);
320
321}
322
323
324static void
325DATETIME_ReturnTxt (DATETIME_INFO *infoPtr, int count, char *result)
326{
327 SYSTEMTIME date = infoPtr->date;
328 int spec;
329 char 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 strncpy (result, infoPtr->textbuf + (spec &~ DT_STRING), txtlen);
345 result[txtlen]=0;
346 TRACE ("arg%d=%x->[%s]\n",count,infoPtr->fieldspec[count],result);
347 return;
348 }
349
350
351 switch (spec) {
352 case DT_END_FORMAT:
353 *result=0;
354 break;
355 case ONEDIGITDAY:
356 sprintf (result,"%d",date.wDay);
357 break;
358 case TWODIGITDAY:
359 sprintf (result,"%.2d",date.wDay);
360 break;
361 case THREECHARDAY:
362 GetLocaleInfoA( LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1+(date.wDayOfWeek+6)%7,
363 result,4);
364 /*sprintf (result,"%.3s",days[date.wDayOfWeek]);*/
365 break;
366 case FULLDAY:
367 GetLocaleInfoA( LOCALE_USER_DEFAULT,LOCALE_SDAYNAME1+ (date.wDayOfWeek+6)%7,
368 buffer,sizeof(buffer));
369 strcpy (result,buffer);
370 break;
371 case ONEDIGIT12HOUR:
372 if (date.wHour>12)
373 sprintf (result,"%d",date.wHour-12);
374 else
375 sprintf (result,"%d",date.wHour);
376 break;
377 case TWODIGIT12HOUR:
378 if (date.wHour>12)
379 sprintf (result,"%.2d",date.wHour-12);
380 else
381 sprintf (result,"%.2d",date.wHour);
382 break;
383 case ONEDIGIT24HOUR:
384 sprintf (result,"%d",date.wHour);
385 break;
386 case TWODIGIT24HOUR:
387 sprintf (result,"%.2d",date.wHour);
388 break;
389 case ONEDIGITSECOND:
390 sprintf (result,"%d",date.wSecond);
391 break;
392 case TWODIGITSECOND:
393 sprintf (result,"%.2d",date.wSecond);
394 break;
395 case ONEDIGITMINUTE:
396 sprintf (result,"%d",date.wMinute);
397 break;
398 case TWODIGITMINUTE:
399 sprintf (result,"%.2d",date.wMinute);
400 break;
401 case ONEDIGITMONTH:
402 sprintf (result,"%d",date.wMonth);
403 break;
404 case TWODIGITMONTH:
405 sprintf (result,"%.2d",date.wMonth);
406 break;
407 case THREECHARMONTH:
408 GetLocaleInfoA( GetSystemDefaultLCID(),LOCALE_SMONTHNAME1+date.wMonth -1,
409 buffer,sizeof(buffer));
410 sprintf (result,"%.3s",buffer);
411 break;
412 case FULLMONTH:
413 GetLocaleInfoA( GetSystemDefaultLCID(),LOCALE_SMONTHNAME1+date.wMonth -1,
414 result,sizeof(result));
415 break;
416 case ONELETTERAMPM:
417 if (date.wHour<12)
418 strcpy (result,"A");
419 else
420 strcpy (result,"P");
421 break;
422 case TWOLETTERAMPM:
423 if (date.wHour<12)
424 strcpy (result,"AM");
425 else
426 strcpy (result,"PM");
427 break;
428 case FORMATCALLBACK:
429 FIXME ("Not implemented\n");
430 strcpy (result,"xxx");
431 break;
432 case ONEDIGITYEAR:
433 sprintf (result,"%d",date.wYear-10* (int) floor(date.wYear/10));
434 break;
435 case TWODIGITYEAR:
436 sprintf (result,"%.2d",date.wYear-100* (int) floor(date.wYear/100));
437 break;
438 case FULLYEAR:
439 sprintf (result,"%d",date.wYear);
440 break;
441 }
442
443 TRACE ("arg%d=%x->[%s]\n",count,infoPtr->fieldspec[count],result);
444}
445
446
447static void
448DATETIME_IncreaseField (DATETIME_INFO *infoPtr, int number)
449{
450 SYSTEMTIME *date = &infoPtr->date;
451 int spec;
452
453 TRACE ("%d\n",number);
454 if ((number>infoPtr->nrFields) || (number<0)) return;
455
456 spec=infoPtr->fieldspec[number];
457 if ((spec & DTHT_DATEFIELD)==0) return;
458
459 switch (spec) {
460 case ONEDIGITDAY:
461 case TWODIGITDAY:
462 case THREECHARDAY:
463 case FULLDAY:
464 date->wDay++;
465 if (date->wDay>MONTHCAL_MonthLength(date->wMonth,date->wYear))
466 date->wDay=1;
467 break;
468 case ONEDIGIT12HOUR:
469 case TWODIGIT12HOUR:
470 case ONEDIGIT24HOUR:
471 case TWODIGIT24HOUR:
472 date->wHour++;
473 if (date->wHour>23) date->wHour=0;
474 break;
475 case ONEDIGITSECOND:
476 case TWODIGITSECOND:
477 date->wSecond++;
478 if (date->wSecond>59) date->wSecond=0;
479 break;
480 case ONEDIGITMINUTE:
481 case TWODIGITMINUTE:
482 date->wMinute++;
483 if (date->wMinute>59) date->wMinute=0;
484 break;
485 case ONEDIGITMONTH:
486 case TWODIGITMONTH:
487 case THREECHARMONTH:
488 case FULLMONTH:
489 date->wMonth++;
490 if (date->wMonth>12) date->wMonth=1;
491 if (date->wDay>MONTHCAL_MonthLength(date->wMonth,date->wYear))
492 date->wDay=MONTHCAL_MonthLength(date->wMonth,date->wYear);
493 break;
494 case ONELETTERAMPM:
495 case TWOLETTERAMPM:
496 date->wHour+=12;
497 if (date->wHour>23) date->wHour-=24;
498 break;
499 case FORMATCALLBACK:
500 FIXME ("Not implemented\n");
501 break;
502 case ONEDIGITYEAR:
503 case TWODIGITYEAR:
504 case FULLYEAR:
505 date->wYear++;
506 break;
507 }
508
509}
510
511
512static void
513DATETIME_DecreaseField (DATETIME_INFO *infoPtr, int number)
514{
515 SYSTEMTIME *date = & infoPtr->date;
516 int spec;
517
518 TRACE ("%d\n",number);
519 if ((number>infoPtr->nrFields) || (number<0)) return;
520
521 spec = infoPtr->fieldspec[number];
522 if ((spec & DTHT_DATEFIELD)==0) return;
523
524 TRACE ("%x\n",spec);
525
526 switch (spec) {
527 case ONEDIGITDAY:
528 case TWODIGITDAY:
529 case THREECHARDAY:
530 case FULLDAY:
531 date->wDay--;
532 if (date->wDay<1)
533 date->wDay=MONTHCAL_MonthLength(date->wMonth,date->wYear);
534 break;
535 case ONEDIGIT12HOUR:
536 case TWODIGIT12HOUR:
537 case ONEDIGIT24HOUR:
538 case TWODIGIT24HOUR:
539 if (date->wHour)
540 date->wHour--;
541 else
542 date->wHour=23;
543 break;
544 case ONEDIGITSECOND:
545 case TWODIGITSECOND:
546 if (date->wHour)
547 date->wSecond--;
548 else
549 date->wHour=59;
550 break;
551 case ONEDIGITMINUTE:
552 case TWODIGITMINUTE:
553 if (date->wMinute)
554 date->wMinute--;
555 else
556 date->wMinute=59;
557 break;
558 case ONEDIGITMONTH:
559 case TWODIGITMONTH:
560 case THREECHARMONTH:
561 case FULLMONTH:
562 if (date->wMonth>1)
563 date->wMonth--;
564 else
565 date->wMonth=12;
566 if (date->wDay>MONTHCAL_MonthLength(date->wMonth,date->wYear))
567 date->wDay=MONTHCAL_MonthLength(date->wMonth,date->wYear);
568 break;
569 case ONELETTERAMPM:
570 case TWOLETTERAMPM:
571 if (date->wHour<12)
572 date->wHour+=12;
573 else
574 date->wHour-=12;
575 break;
576 case FORMATCALLBACK:
577 FIXME ("Not implemented\n");
578 break;
579 case ONEDIGITYEAR:
580 case TWODIGITYEAR:
581 case FULLYEAR:
582 date->wYear--;
583 break;
584 }
585
586}
587
588
589static void
590DATETIME_ResetFieldDown (DATETIME_INFO *infoPtr, int number)
591{
592 SYSTEMTIME *date = &infoPtr->date;
593 int spec;
594
595 TRACE ("%d\n",number);
596 if ((number>infoPtr->nrFields) || (number<0)) return;
597
598 spec = infoPtr->fieldspec[number];
599 if ((spec & DTHT_DATEFIELD)==0) return;
600
601
602 switch (spec) {
603 case ONEDIGITDAY:
604 case TWODIGITDAY:
605 case THREECHARDAY:
606 case FULLDAY:
607 date->wDay = 1;
608 break;
609 case ONEDIGIT12HOUR:
610 case TWODIGIT12HOUR:
611 case ONEDIGIT24HOUR:
612 case TWODIGIT24HOUR:
613 case ONELETTERAMPM:
614 case TWOLETTERAMPM:
615 date->wHour = 0;
616 break;
617 case ONEDIGITSECOND:
618 case TWODIGITSECOND:
619 date->wSecond = 0;
620 break;
621 case ONEDIGITMINUTE:
622 case TWODIGITMINUTE:
623 date->wMinute = 0;
624 break;
625 case ONEDIGITMONTH:
626 case TWODIGITMONTH:
627 case THREECHARMONTH:
628 case FULLMONTH:
629 date->wMonth = 1;
630 case FORMATCALLBACK:
631 FIXME ("Not implemented\n");
632 break;
633 case ONEDIGITYEAR:
634 case TWODIGITYEAR:
635 /* FYI: On 9/14/1752 the calender changed and England and the American */
636 /* colonies changed to the Gregorian calender. This change involved */
637 /* having September 14th following September 2nd. So no date algorithms */
638 /* work before that date. */
639 case FULLYEAR:
640 date->wSecond = 0;
641 date->wMinute = 0;
642 date->wHour = 0;
643 date->wDay = 14; /* overactive ms-programmers..*/
644 date->wMonth = 9;
645 date->wYear = 1752;
646 break;
647 }
648
649}
650
651
652static void
653DATETIME_ResetFieldUp (DATETIME_INFO *infoPtr, int number)
654{
655 SYSTEMTIME *date = & infoPtr->date;
656 int spec;
657
658 TRACE("%d \n",number);
659 if ((number>infoPtr->nrFields) || (number<0)) return;
660
661 spec=infoPtr->fieldspec[number];
662 if ((spec & DTHT_DATEFIELD)==0) return;
663
664 switch (spec) {
665 case ONEDIGITDAY:
666 case TWODIGITDAY:
667 case THREECHARDAY:
668 case FULLDAY:
669 date->wDay=MONTHCAL_MonthLength(date->wMonth,date->wYear);
670 break;
671 case ONEDIGIT12HOUR:
672 case TWODIGIT12HOUR:
673 case ONEDIGIT24HOUR:
674 case TWODIGIT24HOUR:
675 case ONELETTERAMPM:
676 case TWOLETTERAMPM:
677 date->wHour=23;
678 break;
679 case ONEDIGITSECOND:
680 case TWODIGITSECOND:
681 date->wSecond=59;
682 break;
683 case ONEDIGITMINUTE:
684 case TWODIGITMINUTE:
685 date->wMinute=59;
686 break;
687 case ONEDIGITMONTH:
688 case TWODIGITMONTH:
689 case THREECHARMONTH:
690 case FULLMONTH:
691 date->wMonth=12;
692 case FORMATCALLBACK:
693 FIXME ("Not implemented\n");
694 break;
695 case ONEDIGITYEAR:
696 case TWODIGITYEAR:
697 case FULLYEAR:
698 date->wYear=9999; /* Y10K problem? naaah. */
699 break;
700 }
701
702}
703
704
705static void DATETIME_Refresh (HWND hwnd, HDC hdc)
706
707{
708 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
709 int i,prevright;
710 RECT *field;
711 DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
712 RECT *rcDraw = &infoPtr->rcDraw;
713 RECT *rcClient = &infoPtr->rcClient;
714 RECT *calbutton = &infoPtr->calbutton;
715 RECT *checkbox = &infoPtr->checkbox;
716 HBRUSH hbr;
717 SIZE size;
718 COLORREF oldBk, oldTextColor;
719
720 /* draw control edge */
721 TRACE("\n");
722 hbr = CreateSolidBrush(RGB(255, 255, 255));
723 FillRect(hdc, rcClient, hbr);
724 DrawEdge(hdc, rcClient, EDGE_SUNKEN, BF_RECT);
725 DeleteObject(hbr);
726
727 if (infoPtr->dateValid) {
728 char txt[80];
729 HFONT oldFont;
730 oldFont = SelectObject (hdc, infoPtr->hFont);
731
732 DATETIME_ReturnTxt (infoPtr, 0, txt);
733 GetTextExtentPoint32A (hdc, txt, strlen (txt), &size);
734 rcDraw->bottom = size.cy+2;
735
736 if (dwStyle & DTS_SHOWNONE) checkbox->right = 18;
737
738 prevright = checkbox->right;
739
740 for (i=0; i<infoPtr->nrFields; i++) {
741 DATETIME_ReturnTxt (infoPtr, i, txt);
742 GetTextExtentPoint32A (hdc, txt, strlen (txt), &size);
743 field = & infoPtr->fieldRect[i];
744 field->left = prevright;
745 field->right = prevright+size.cx;
746 field->top = rcDraw->top;
747 field->bottom = rcDraw->bottom;
748 prevright = field->right;
749
750 if ((infoPtr->haveFocus) && (i==infoPtr->select)) {
751 hbr = CreateSolidBrush (GetSysColor (COLOR_ACTIVECAPTION));
752 FillRect(hdc, field, hbr);
753 oldBk = SetBkColor (hdc, GetSysColor(COLOR_ACTIVECAPTION));
754 oldTextColor = SetTextColor (hdc, GetSysColor(COLOR_WINDOW));
755 DeleteObject (hbr);
756 DrawTextA ( hdc, txt, strlen(txt), field,
757 DT_RIGHT | DT_VCENTER | DT_SINGLELINE );
758 SetBkColor (hdc, oldBk);
759 SetTextColor (hdc, oldTextColor);
760 }
761 else
762 DrawTextA ( hdc, txt, strlen(txt), field,
763 DT_RIGHT | DT_VCENTER | DT_SINGLELINE );
764 }
765
766 SelectObject (hdc, oldFont);
767 }
768
769 if (!(dwStyle & DTS_UPDOWN)) {
770 DrawFrameControl(hdc, calbutton, DFC_SCROLL,
771 DFCS_SCROLLDOWN | (infoPtr->bCalDepressed ? DFCS_PUSHED : 0) |
772 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
773 }
774}
775
776
777static LRESULT
778DATETIME_HitTest (HWND hwnd, DATETIME_INFO *infoPtr, POINT pt)
779{
780 int i, retval;
781
782 TRACE ("%ld, %ld\n",pt.x,pt.y);
783
784 retval = DTHT_NONE;
785 if (PtInRect (&infoPtr->calbutton, pt))
786 {retval = DTHT_MCPOPUP; TRACE("Hit in calbutton(DTHT_MCPOPUP)\n"); goto done; }
787 if (PtInRect (&infoPtr->checkbox, pt))
788 {retval = DTHT_CHECKBOX; TRACE("Hit in checkbox(DTHT_CHECKBOX)\n"); goto done; }
789
790 for (i=0; i<infoPtr->nrFields; i++) {
791 if (PtInRect (&infoPtr->fieldRect[i], pt)) {
792 retval = i;
793 TRACE("Hit in date text in field %d\n", i);
794 break;
795 }
796 }
797
798done:
799 return retval;
800}
801
802
803static LRESULT
804DATETIME_LButtonDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
805{
806 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
807 DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
808 int old, new;
809 POINT pt;
810
811 TRACE ("\n");
812
813 old = infoPtr->select;
814 pt.x = (INT)LOWORD(lParam);
815 pt.y = (INT)HIWORD(lParam);
816
817 new = DATETIME_HitTest (hwnd, infoPtr, pt);
818
819 /* FIXME: might be conditions where we don't want to update infoPtr->select */
820 infoPtr->select = new;
821
822 if (infoPtr->select != old) {
823 infoPtr->haveFocus = DTHT_GOTFOCUS;
824 }
825
826 if (infoPtr->select == DTHT_MCPOPUP) {
827 /* FIXME: button actually is only depressed during dropdown of the */
828 /* calender control and when the mouse is over the button window */
829 infoPtr->bCalDepressed = TRUE;
830
831 /* recalculate the position of the monthcal popup */
832 if(dwStyle & DTS_RIGHTALIGN)
833 infoPtr->monthcal_pos.x = infoPtr->rcClient.right - ((infoPtr->calbutton.right -
834 infoPtr->calbutton.left) + 145);
835 else
836 infoPtr->monthcal_pos.x = 8;
837
838 infoPtr->monthcal_pos.y = infoPtr->rcClient.bottom;
839 ClientToScreen (hwnd, &(infoPtr->monthcal_pos));
840 SetWindowPos(infoPtr->hMonthCal, 0, infoPtr->monthcal_pos.x,
841 infoPtr->monthcal_pos.y, 145, 150, 0);
842
843 if(IsWindowVisible(infoPtr->hMonthCal))
844 ShowWindow(infoPtr->hMonthCal, SW_HIDE);
845 else
846 ShowWindow(infoPtr->hMonthCal, SW_SHOW);
847
848 TRACE ("dt:%x mc:%x mc parent:%x, desktop:%x, mcpp:%x\n",
849 hwnd,infoPtr->hMonthCal,
850 GetParent (infoPtr->hMonthCal),
851 GetDesktopWindow (),
852 GetParent (GetParent (infoPtr->hMonthCal)));
853 DATETIME_SendSimpleNotify (hwnd, DTN_DROPDOWN);
854 }
855
856 InvalidateRect(hwnd, NULL, FALSE);
857
858 return 0;
859}
860
861
862static LRESULT
863DATETIME_LButtonUp (HWND hwnd, WPARAM wParam, LPARAM lParam)
864{
865 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
866
867 TRACE("\n");
868
869 if(infoPtr->bCalDepressed == TRUE) {
870 infoPtr->bCalDepressed = FALSE;
871 InvalidateRect(hwnd, &(infoPtr->calbutton), TRUE);
872 }
873
874 return 0;
875}
876
877
878static LRESULT
879DATETIME_Paint (HWND hwnd, WPARAM wParam)
880{
881 HDC hdc;
882 PAINTSTRUCT ps;
883
884 hdc = wParam==0 ? BeginPaint (hwnd, &ps) : (HDC)wParam;
885 DATETIME_Refresh (hwnd, hdc);
886 if(!wParam)
887 EndPaint (hwnd, &ps);
888 return 0;
889}
890
891
892static LRESULT
893DATETIME_ParentNotify (HWND hwnd, WPARAM wParam, LPARAM lParam)
894{
895 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
896 LPNMHDR lpnmh = (LPNMHDR) lParam;
897
898 TRACE ("%x,%lx\n",wParam, lParam);
899 TRACE ("Got notification %x from %x\n", lpnmh->code, lpnmh->hwndFrom);
900 TRACE ("info: %x %x %x\n",hwnd,infoPtr->hMonthCal,infoPtr->hUpdown);
901 return 0;
902}
903
904
905static LRESULT
906DATETIME_Notify (HWND hwnd, WPARAM wParam, LPARAM lParam)
907
908{
909 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
910 LPNMHDR lpnmh = (LPNMHDR) lParam;
911
912 TRACE ("%x,%lx\n",wParam, lParam);
913 TRACE ("Got notification %x from %x\n", lpnmh->code, lpnmh->hwndFrom);
914 TRACE ("info: %x %x %x\n",hwnd,infoPtr->hMonthCal,infoPtr->hUpdown);
915 return 0;
916}
917
918
919static LRESULT
920DATETIME_KeyDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
921{
922 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
923 int FieldNum,wrap=0;
924
925 TRACE("%x %lx %x\n",wParam, lParam, infoPtr->select);
926
927 FieldNum = infoPtr->select & DTHT_DATEFIELD;
928
929 if (!(infoPtr->haveFocus)) return 0;
930 if ((FieldNum==0) && (infoPtr->select)) return 0;
931
932 if (infoPtr->select & FORMATCALLMASK) {
933 FIXME ("Callbacks not implemented yet\n");
934 }
935
936 switch (wParam) {
937 case VK_ADD:
938 case VK_UP:
939 DATETIME_IncreaseField (infoPtr,FieldNum);
940 DATETIME_SendDateTimeChangeNotify (hwnd);
941 break;
942 case VK_SUBTRACT:
943 case VK_DOWN:
944 DATETIME_DecreaseField (infoPtr,FieldNum);
945 DATETIME_SendDateTimeChangeNotify (hwnd);
946 break;
947 case VK_HOME:
948 DATETIME_ResetFieldDown (infoPtr,FieldNum);
949 DATETIME_SendDateTimeChangeNotify (hwnd);
950 break;
951 case VK_END:
952 DATETIME_ResetFieldUp(infoPtr,FieldNum);
953 DATETIME_SendDateTimeChangeNotify (hwnd);
954 break;
955 case VK_LEFT:
956 do {
957 if (infoPtr->select==0) {
958 infoPtr->select = infoPtr->nrFields - 1;
959 wrap++;
960 } else
961 infoPtr->select--;
962 }
963 while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
964 break;
965 case VK_RIGHT:
966 do {
967 infoPtr->select++;
968 if (infoPtr->select==infoPtr->nrFields) {
969 infoPtr->select = 0;
970 wrap++;
971 }
972 }
973 while ((infoPtr->fieldspec[infoPtr->select] & DT_STRING) && (wrap<2));
974 break;
975 }
976
977 InvalidateRect(hwnd, NULL, FALSE);
978
979 return 0;
980}
981
982
983static LRESULT
984DATETIME_KillFocus (HWND hwnd, WPARAM wParam, LPARAM lParam)
985{
986 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
987
988 TRACE ("\n");
989
990 if (infoPtr->haveFocus) {
991 DATETIME_SendSimpleNotify (hwnd, NM_KILLFOCUS);
992 infoPtr->haveFocus = 0;
993 }
994
995 InvalidateRect (hwnd, NULL, TRUE);
996
997 return 0;
998}
999
1000
1001static LRESULT
1002DATETIME_SetFocus (HWND hwnd, WPARAM wParam, LPARAM lParam)
1003{
1004 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
1005
1006 TRACE ("\n");
1007
1008 if (infoPtr->haveFocus==0) {
1009 DATETIME_SendSimpleNotify (hwnd, NM_SETFOCUS);
1010 infoPtr->haveFocus = DTHT_GOTFOCUS;
1011 }
1012
1013 InvalidateRect(hwnd, NULL, FALSE);
1014
1015 return 0;
1016}
1017
1018
1019static BOOL
1020DATETIME_SendDateTimeChangeNotify (HWND hwnd)
1021
1022{
1023 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
1024 NMDATETIMECHANGE dtdtc;
1025
1026 TRACE ("\n");
1027 dtdtc.nmhdr.hwndFrom = hwnd;
1028 dtdtc.nmhdr.idFrom = GetWindowLongA( hwnd, GWL_ID);
1029 dtdtc.nmhdr.code = DTN_DATETIMECHANGE;
1030
1031 if ((GetWindowLongA (hwnd, GWL_STYLE) & DTS_SHOWNONE))
1032 dtdtc.dwFlags = GDT_NONE;
1033 else
1034 dtdtc.dwFlags = GDT_VALID;
1035
1036 MONTHCAL_CopyTime (&infoPtr->date, &dtdtc.st);
1037 return (BOOL) SendMessageA (GetParent (hwnd), WM_NOTIFY,
1038 (WPARAM)dtdtc.nmhdr.idFrom, (LPARAM)&dtdtc);
1039}
1040
1041
1042static BOOL
1043DATETIME_SendSimpleNotify (HWND hwnd, UINT code)
1044{
1045 NMHDR nmhdr;
1046
1047 TRACE("%x\n",code);
1048 nmhdr.hwndFrom = hwnd;
1049 nmhdr.idFrom = GetWindowLongA( hwnd, GWL_ID);
1050 nmhdr.code = code;
1051
1052 return (BOOL) SendMessageA (GetParent (hwnd), WM_NOTIFY,
1053 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
1054}
1055
1056static LRESULT
1057DATETIME_Size (HWND hwnd, WPARAM wParam, LPARAM lParam)
1058{
1059 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr(hwnd);
1060 DWORD dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
1061
1062 /* set size */
1063 infoPtr->rcClient.bottom = HIWORD(lParam);
1064 infoPtr->rcClient.right = LOWORD(lParam);
1065
1066 TRACE("Height=%d, Width=%d\n", infoPtr->rcClient.bottom, infoPtr->rcClient.right);
1067
1068 /* use DrawEdge to adjust the size of rcEdge to get rcDraw */
1069 memcpy((&infoPtr->rcDraw), (&infoPtr->rcClient), sizeof(infoPtr->rcDraw));
1070
1071 DrawEdge((HDC)NULL, &(infoPtr->rcDraw), EDGE_SUNKEN, BF_RECT | BF_ADJUST);
1072
1073 /* set the size of the button that drops the calender down */
1074 /* FIXME: account for style that allows button on left side */
1075 infoPtr->calbutton.top = infoPtr->rcDraw.top;
1076 infoPtr->calbutton.bottom= infoPtr->rcDraw.bottom;
1077 infoPtr->calbutton.left = infoPtr->rcDraw.right-15;
1078 infoPtr->calbutton.right = infoPtr->rcDraw.right;
1079
1080 /* set enable/disable button size for show none style being enabled */
1081 /* FIXME: these dimensions are completely incorrect */
1082 infoPtr->checkbox.top = infoPtr->rcDraw.top;
1083 infoPtr->checkbox.bottom = infoPtr->rcDraw.bottom;
1084 infoPtr->checkbox.left = infoPtr->rcDraw.left;
1085 infoPtr->checkbox.right = infoPtr->rcDraw.left + 10;
1086
1087 /* update the position of the monthcal control */
1088 if(dwStyle & DTS_RIGHTALIGN)
1089 infoPtr->monthcal_pos.x = infoPtr->rcClient.right - ((infoPtr->calbutton.right -
1090 infoPtr->calbutton.left) + 145);
1091 else
1092 infoPtr->monthcal_pos.x = 8;
1093
1094 infoPtr->monthcal_pos.y = infoPtr->rcClient.bottom;
1095 ClientToScreen (hwnd, &(infoPtr->monthcal_pos));
1096 SetWindowPos(infoPtr->hMonthCal, 0, infoPtr->monthcal_pos.x,
1097 infoPtr->monthcal_pos.y,
1098 145, 150, 0);
1099
1100 InvalidateRect(hwnd, NULL, FALSE);
1101
1102 return 0;
1103}
1104
1105
1106static LRESULT
1107DATETIME_Create (HWND hwnd, WPARAM wParam, LPARAM lParam)
1108{
1109 DATETIME_INFO *infoPtr;
1110 DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
1111
1112 /* allocate memory for info structure */
1113 TRACE("%04x %08lx\n",wParam,lParam);
1114 infoPtr = (DATETIME_INFO *)COMCTL32_Alloc (sizeof(DATETIME_INFO));
1115 if (infoPtr == NULL) {
1116 ERR("could not allocate info memory!\n");
1117 return 0;
1118 }
1119
1120 SetWindowLongA (hwnd, 0, (DWORD)infoPtr);
1121
1122 if (dwStyle & DTS_SHOWNONE) {
1123 infoPtr->hwndCheckbut=CreateWindowExA (0,"button", 0,
1124 WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
1125 2,2,13,13,
1126 hwnd,
1127 0, GetWindowLongA (hwnd, GWL_HINSTANCE), 0);
1128 SendMessageA (infoPtr->hwndCheckbut, BM_SETCHECK, 1, 0);
1129 }
1130
1131 if (dwStyle & DTS_UPDOWN) {
1132 infoPtr->hUpdown=CreateUpDownControl (
1133 WS_CHILD | WS_BORDER | WS_VISIBLE,
1134 120,1,20,20,
1135 hwnd,1,0,0,
1136 UD_MAXVAL, UD_MINVAL, 0);
1137 }
1138
1139 infoPtr->fieldspec = (int *) COMCTL32_Alloc (32*sizeof(int));
1140 infoPtr->fieldRect = (RECT *) COMCTL32_Alloc (32*sizeof(RECT));
1141 infoPtr->buflen = (int *) COMCTL32_Alloc (32*sizeof(int));
1142 infoPtr->nrFieldsAllocated = 32;
1143
1144 DATETIME_SetFormat (hwnd, 0, 0);
1145
1146 /* create the monthcal control */
1147 infoPtr->hMonthCal = CreateWindowExA (0,"SysMonthCal32", 0,
1148 WS_BORDER | WS_POPUP | WS_CLIPSIBLINGS,
1149 0, 0, 0, 0,
1150 GetParent(hwnd),
1151 0, 0, 0);
1152
1153 /* initialize info structure */
1154 GetSystemTime (&infoPtr->date);
1155 infoPtr->dateValid = TRUE;
1156 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1157 return 0;
1158}
1159
1160
1161static LRESULT
1162DATETIME_Destroy (HWND hwnd, WPARAM wParam, LPARAM lParam)
1163{
1164 DATETIME_INFO *infoPtr = DATETIME_GetInfoPtr (hwnd);
1165
1166 TRACE("\n");
1167 COMCTL32_Free (infoPtr);
1168 return 0;
1169}
1170
1171
1172static LRESULT WINAPI
1173DATETIME_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1174{
1175 switch (uMsg)
1176 {
1177
1178 case DTM_GETSYSTEMTIME:
1179 DATETIME_GetSystemTime (hwnd, wParam, lParam);
1180
1181 case DTM_SETSYSTEMTIME:
1182 DATETIME_SetSystemTime (hwnd, wParam, lParam);
1183
1184 case DTM_GETRANGE:
1185 FIXME("Unimplemented msg DTM_GETRANGE\n");
1186 return 0;
1187
1188 case DTM_SETRANGE:
1189 FIXME("Unimplemented msg DTM_SETRANGE\n");
1190 return 1;
1191
1192 case DTM_SETFORMATA:
1193 return DATETIME_SetFormat (hwnd, wParam, lParam);
1194
1195 case DTM_SETFORMATW:
1196 return DATETIME_SetFormatW (hwnd, wParam, lParam);
1197
1198 case DTM_SETMCCOLOR:
1199 return DATETIME_SetMonthCalColor (hwnd, wParam, lParam);
1200
1201 case DTM_GETMCCOLOR:
1202 return DATETIME_GetMonthCalColor (hwnd, wParam);
1203
1204 case DTM_GETMONTHCAL:
1205 return DATETIME_GetMonthCal (hwnd);
1206
1207 case DTM_SETMCFONT:
1208 return DATETIME_SetMonthCalFont (hwnd, wParam, lParam);
1209
1210 case DTM_GETMCFONT:
1211 return DATETIME_GetMonthCalFont (hwnd);
1212
1213 case WM_PARENTNOTIFY:
1214 return DATETIME_ParentNotify (hwnd, wParam, lParam);
1215
1216 case WM_NOTIFY:
1217 return DATETIME_Notify (hwnd, wParam, lParam);
1218
1219 case WM_GETDLGCODE:
1220 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1221
1222 case WM_PAINT:
1223 return DATETIME_Paint (hwnd, wParam);
1224
1225 case WM_KEYDOWN:
1226 return DATETIME_KeyDown (hwnd, wParam, lParam);
1227
1228 case WM_KILLFOCUS:
1229 return DATETIME_KillFocus (hwnd, wParam, lParam);
1230
1231 case WM_SETFOCUS:
1232 return DATETIME_SetFocus (hwnd, wParam, lParam);
1233
1234 case WM_SIZE:
1235 return DATETIME_Size (hwnd, wParam, lParam);
1236
1237 case WM_LBUTTONDOWN:
1238 return DATETIME_LButtonDown (hwnd, wParam, lParam);
1239
1240 case WM_LBUTTONUP:
1241 return DATETIME_LButtonUp (hwnd, wParam, lParam);
1242
1243 case WM_CREATE:
1244 return DATETIME_Create (hwnd, wParam, lParam);
1245
1246 case WM_DESTROY:
1247 return DATETIME_Destroy (hwnd, wParam, lParam);
1248
1249 default:
1250 if (uMsg >= WM_USER)
1251 ERR("unknown msg %04x wp=%08x lp=%08lx\n",
1252 uMsg, wParam, lParam);
1253 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
1254 }
1255 return 0;
1256}
1257
1258
1259VOID
1260DATETIME_Register (void)
1261{
1262 WNDCLASSA wndClass;
1263
1264 TRACE("\n");
1265 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1266 wndClass.style = CS_GLOBALCLASS;
1267 wndClass.lpfnWndProc = (WNDPROC)DATETIME_WindowProc;
1268 wndClass.cbClsExtra = 0;
1269 wndClass.cbWndExtra = sizeof(DATETIME_INFO *);
1270 wndClass.hCursor = LoadCursorA (0, IDC_ARROWA);
1271 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1272 wndClass.lpszClassName = DATETIMEPICK_CLASSA;
1273
1274 RegisterClassA (&wndClass);
1275}
1276
1277
1278VOID
1279DATETIME_Unregister (void)
1280{
1281 TRACE("\n");
1282 UnregisterClassA (DATETIMEPICK_CLASSA, (HINSTANCE)NULL);
1283}
Note: See TracBrowser for help on using the repository browser.