Fix MsiRecordSetString for NULL strings and update test case.
[wine.git] / dlls / comctl32 / monthcal.c
blob55220d02b197de6c8b860fe407642246934dc05d
1 /* Month calendar control
4 * Copyright 1998, 1999 Eric Kohl (ekohl@abo.rhein-zeitung.de)
5 * Copyright 1999 Alex Priem (alexp@sci.kun.nl)
6 * Copyright 1999 Chris Morgan <cmorgan@wpi.edu> and
7 * James Abbatiello <abbeyj@wpi.edu>
8 * Copyright 2000 Uwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * NOTE
26 * This code was audited for completeness against the documented features
27 * of Comctl32.dll version 6.0 on Oct. 20, 2004, by Dimitrie O. Paun.
29 * Unless otherwise noted, we believe this code to be complete, as per
30 * the specification mentioned above.
31 * If you discover missing features, or bugs, please note them below.
33 * TODO:
34 * -- MCM_[GS]ETUNICODEFORMAT
35 * -- MONTHCAL_GetMonthRange
36 * -- Unicodification
37 * -- handle resources better (doesn't work now);
38 * -- take care of internationalization.
39 * -- keyboard handling.
40 * -- GetRange: At the moment, we copy ranges anyway, regardless of
41 * infoPtr->rangeValid; a invalid range is simply filled
42 * with zeros in SetRange. Is this the right behavior?
43 * -- search for FIXME
46 #include <math.h>
47 #include <stdarg.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
52 #include "windef.h"
53 #include "winbase.h"
54 #include "wingdi.h"
55 #include "winuser.h"
56 #include "winnls.h"
57 #include "commctrl.h"
58 #include "comctl32.h"
59 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
63 #define MC_SEL_LBUTUP 1 /* Left button released */
64 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
65 #define MC_PREVPRESSED 4 /* Prev month button pressed */
66 #define MC_NEXTPRESSED 8 /* Next month button pressed */
67 #define MC_NEXTMONTHDELAY 350 /* when continuously pressing `next */
68 /* month', wait 500 ms before going */
69 /* to the next month */
70 #define MC_NEXTMONTHTIMER 1 /* Timer ID's */
71 #define MC_PREVMONTHTIMER 2
73 typedef struct
75 HWND hwndSelf;
76 COLORREF bk;
77 COLORREF txt;
78 COLORREF titlebk;
79 COLORREF titletxt;
80 COLORREF monthbk;
81 COLORREF trailingtxt;
82 HFONT hFont;
83 HFONT hBoldFont;
84 int textHeight;
85 int textWidth;
86 int height_increment;
87 int width_increment;
88 int firstDayplace; /* place of the first day of the current month */
89 int delta; /* scroll rate; # of months that the */
90 /* control moves when user clicks a scroll button */
91 int visible; /* # of months visible */
92 int firstDay; /* Start month calendar with firstDay's day */
93 int monthRange;
94 MONTHDAYSTATE *monthdayState;
95 SYSTEMTIME todaysDate;
96 DWORD currentMonth;
97 DWORD currentYear;
98 int status; /* See MC_SEL flags */
99 int curSelDay; /* current selected day */
100 int firstSelDay; /* first selected day */
101 int maxSelCount;
102 SYSTEMTIME minSel;
103 SYSTEMTIME maxSel;
104 DWORD rangeValid;
105 SYSTEMTIME minDate;
106 SYSTEMTIME maxDate;
108 RECT title; /* rect for the header above the calendar */
109 RECT titlebtnnext; /* the `next month' button in the header */
110 RECT titlebtnprev; /* the `prev month' button in the header */
111 RECT titlemonth; /* the `month name' txt in the header */
112 RECT titleyear; /* the `year number' txt in the header */
113 RECT wdays; /* week days at top */
114 RECT days; /* calendar area */
115 RECT weeknums; /* week numbers at left side */
116 RECT todayrect; /* `today: xx/xx/xx' text rect */
117 HWND hwndNotify; /* Window to receive the notifications */
118 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
119 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
120 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
123 /* Offsets of days in the week to the weekday of january 1. */
124 static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
127 #define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0))
129 /* helper functions */
131 /* returns the number of days in any given month, checking for leap days */
132 /* january is 1, december is 12 */
133 int MONTHCAL_MonthLength(int month, int year)
135 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0};
136 /*Wrap around, this eases handling*/
137 if(month == 0)
138 month = 12;
139 if(month == 13)
140 month = 1;
142 /* if we have a leap year add 1 day to February */
143 /* a leap year is a year either divisible by 400 */
144 /* or divisible by 4 and not by 100 */
145 if(month == 2) { /* February */
146 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
147 (year%4 == 0)) ? 1 : 0);
149 else {
150 return mdays[month - 1];
155 /* make sure that time is valid */
156 static int MONTHCAL_ValidateTime(SYSTEMTIME time)
158 if(time.wMonth > 12) return FALSE;
159 if(time.wDayOfWeek > 6) return FALSE;
160 if(time.wDay > MONTHCAL_MonthLength(time.wMonth, time.wYear))
161 return FALSE;
162 if(time.wHour > 23) return FALSE;
163 if(time.wMinute > 59) return FALSE;
164 if(time.wSecond > 59) return FALSE;
165 if(time.wMilliseconds > 999) return FALSE;
167 return TRUE;
171 void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
173 to->wYear = from->wYear;
174 to->wMonth = from->wMonth;
175 to->wDayOfWeek = from->wDayOfWeek;
176 to->wDay = from->wDay;
177 to->wHour = from->wHour;
178 to->wMinute = from->wMinute;
179 to->wSecond = from->wSecond;
180 to->wMilliseconds = from->wMilliseconds;
184 /* Note:Depending on DST, this may be offset by a day.
185 Need to find out if we're on a DST place & adjust the clock accordingly.
186 Above function assumes we have a valid data.
187 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
188 0 = Monday.
191 /* returns the day in the week(0 == monday, 6 == sunday) */
192 /* day(1 == 1st, 2 == 2nd... etc), year is the year value */
193 static int MONTHCAL_CalculateDayOfWeek(DWORD day, DWORD month, DWORD year)
195 year-=(month < 3);
197 return((year + year/4 - year/100 + year/400 +
198 DayOfWeekTable[month-1] + day - 1 ) % 7);
201 /* From a given point, calculate the row (weekpos), column(daypos)
202 and day in the calendar. day== 0 mean the last day of tha last month
204 static int MONTHCAL_CalcDayFromPos(MONTHCAL_INFO *infoPtr, int x, int y,
205 int *daypos,int *weekpos)
207 int retval, firstDay;
208 RECT rcClient;
210 GetClientRect(infoPtr->hwndSelf, &rcClient);
212 /* if the point is outside the x bounds of the window put
213 it at the boundry */
214 if (x > rcClient.right)
215 x = rcClient.right;
218 *daypos = (x - infoPtr->days.left ) / infoPtr->width_increment;
219 *weekpos = (y - infoPtr->days.top ) / infoPtr->height_increment;
221 firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear)+6 - infoPtr->firstDay)%7;
222 retval = *daypos + (7 * *weekpos) - firstDay;
223 return retval;
226 /* day is the day of the month, 1 == 1st day of the month */
227 /* sets x and y to be the position of the day */
228 /* x == day, y == week where(0,0) == firstDay, 1st week */
229 static void MONTHCAL_CalcDayXY(MONTHCAL_INFO *infoPtr, int day, int month,
230 int *x, int *y)
232 int firstDay, prevMonth;
234 firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear) +6 - infoPtr->firstDay)%7;
236 if(month==infoPtr->currentMonth) {
237 *x = (day + firstDay) % 7;
238 *y = (day + firstDay - *x) / 7;
239 return;
241 if(month < infoPtr->currentMonth) {
242 prevMonth = month - 1;
243 if(prevMonth==0)
244 prevMonth = 12;
246 *x = (MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear) - firstDay) % 7;
247 *y = 0;
248 return;
251 *y = MONTHCAL_MonthLength(month, infoPtr->currentYear - 1) / 7;
252 *x = (day + firstDay + MONTHCAL_MonthLength(month,
253 infoPtr->currentYear)) % 7;
257 /* x: column(day), y: row(week) */
258 static void MONTHCAL_CalcDayRect(MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
260 r->left = infoPtr->days.left + x * infoPtr->width_increment;
261 r->right = r->left + infoPtr->width_increment;
262 r->top = infoPtr->days.top + y * infoPtr->height_increment;
263 r->bottom = r->top + infoPtr->textHeight;
267 /* sets the RECT struct r to the rectangle around the day and month */
268 /* day is the day value of the month(1 == 1st), month is the month */
269 /* value(january == 1, december == 12) */
270 static inline void MONTHCAL_CalcPosFromDay(MONTHCAL_INFO *infoPtr,
271 int day, int month, RECT *r)
273 int x, y;
275 MONTHCAL_CalcDayXY(infoPtr, day, month, &x, &y);
276 MONTHCAL_CalcDayRect(infoPtr, r, x, y);
280 /* day is the day in the month(1 == 1st of the month) */
281 /* month is the month value(1 == january, 12 == december) */
282 static void MONTHCAL_CircleDay(MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month)
284 HPEN hRedPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
285 HPEN hOldPen2 = SelectObject(hdc, hRedPen);
286 POINT points[13];
287 int x, y;
288 RECT day_rect;
291 MONTHCAL_CalcPosFromDay(infoPtr, day, month, &day_rect);
293 x = day_rect.left;
294 y = day_rect.top;
296 points[0].x = x;
297 points[0].y = y - 1;
298 points[1].x = x + 0.8 * infoPtr->width_increment;
299 points[1].y = y - 1;
300 points[2].x = x + 0.9 * infoPtr->width_increment;
301 points[2].y = y;
302 points[3].x = x + infoPtr->width_increment;
303 points[3].y = y + 0.5 * infoPtr->height_increment;
305 points[4].x = x + infoPtr->width_increment;
306 points[4].y = y + 0.9 * infoPtr->height_increment;
307 points[5].x = x + 0.6 * infoPtr->width_increment;
308 points[5].y = y + 0.9 * infoPtr->height_increment;
309 points[6].x = x + 0.5 * infoPtr->width_increment;
310 points[6].y = y + 0.9 * infoPtr->height_increment; /* bring the bottom up just
311 a hair to fit inside the day rectangle */
313 points[7].x = x + 0.2 * infoPtr->width_increment;
314 points[7].y = y + 0.8 * infoPtr->height_increment;
315 points[8].x = x + 0.1 * infoPtr->width_increment;
316 points[8].y = y + 0.8 * infoPtr->height_increment;
317 points[9].x = x;
318 points[9].y = y + 0.5 * infoPtr->height_increment;
320 points[10].x = x + 0.1 * infoPtr->width_increment;
321 points[10].y = y + 0.2 * infoPtr->height_increment;
322 points[11].x = x + 0.2 * infoPtr->width_increment;
323 points[11].y = y + 0.3 * infoPtr->height_increment;
324 points[12].x = x + 0.4 * infoPtr->width_increment;
325 points[12].y = y + 0.2 * infoPtr->height_increment;
327 PolyBezier(hdc, points, 13);
328 DeleteObject(hRedPen);
329 SelectObject(hdc, hOldPen2);
333 static void MONTHCAL_DrawDay(MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month,
334 int x, int y, int bold)
336 char buf[10];
337 RECT r;
338 static int haveBoldFont, haveSelectedDay = FALSE;
339 HBRUSH hbr;
340 COLORREF oldCol = 0;
341 COLORREF oldBk = 0;
343 sprintf(buf, "%d", day);
345 /* No need to check styles: when selection is not valid, it is set to zero.
346 * 1<day<31, so evertyhing's OK.
349 MONTHCAL_CalcDayRect(infoPtr, &r, x, y);
351 if((day>=infoPtr->minSel.wDay) && (day<=infoPtr->maxSel.wDay)
352 && (month==infoPtr->currentMonth)) {
353 HRGN hrgn;
354 RECT r2;
356 TRACE("%d %d %d\n",day, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
357 TRACE("%ld %ld %ld %ld\n", r.left, r.top, r.right, r.bottom);
358 oldCol = SetTextColor(hdc, infoPtr->monthbk);
359 oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
360 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
361 hrgn = CreateEllipticRgn(r.left, r.top, r.right, r.bottom);
362 FillRgn(hdc, hrgn, hbr);
364 /* FIXME: this may need to be changed now b/c of the other
365 drawing changes 11/3/99 CMM */
366 r2.left = r.left - 0.25 * infoPtr->textWidth;
367 r2.top = r.top;
368 r2.right = r.left + 0.5 * infoPtr->textWidth;
369 r2.bottom = r.bottom;
370 if(haveSelectedDay) FillRect(hdc, &r2, hbr);
371 haveSelectedDay = TRUE;
372 } else {
373 haveSelectedDay = FALSE;
376 /* need to add some code for multiple selections */
378 if((bold) &&(!haveBoldFont)) {
379 SelectObject(hdc, infoPtr->hBoldFont);
380 haveBoldFont = TRUE;
382 if((!bold) &&(haveBoldFont)) {
383 SelectObject(hdc, infoPtr->hFont);
384 haveBoldFont = FALSE;
387 if(haveSelectedDay) {
388 SetTextColor(hdc, oldCol);
389 SetBkColor(hdc, oldBk);
392 SetBkMode(hdc,TRANSPARENT);
393 DrawTextA(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
395 /* draw a rectangle around the currently selected days text */
396 if((day==infoPtr->curSelDay) && (month==infoPtr->currentMonth))
397 DrawFocusRect(hdc, &r);
401 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, PAINTSTRUCT* ps)
403 RECT *title=&infoPtr->title;
404 RECT *prev=&infoPtr->titlebtnprev;
405 RECT *next=&infoPtr->titlebtnnext;
406 RECT *titlemonth=&infoPtr->titlemonth;
407 RECT *titleyear=&infoPtr->titleyear;
408 RECT dayrect;
409 RECT *days=&dayrect;
410 RECT rtoday;
411 int i, j, m, mask, day, firstDay, weeknum, weeknum1,prevMonth;
412 int textHeight = infoPtr->textHeight, textWidth = infoPtr->textWidth;
413 SIZE size;
414 HBRUSH hbr;
415 HFONT currentFont;
416 char buf[20];
417 char buf1[20];
418 char buf2[32];
419 COLORREF oldTextColor, oldBkColor;
420 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
421 RECT rcTemp;
422 RECT rcDay; /* used in MONTHCAL_CalcDayRect() */
423 SYSTEMTIME localtime;
424 int startofprescal;
426 oldTextColor = SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
428 /* fill background */
429 hbr = CreateSolidBrush (infoPtr->bk);
430 FillRect(hdc, &ps->rcPaint, hbr);
431 DeleteObject(hbr);
433 /* draw header */
434 if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
436 hbr = CreateSolidBrush(infoPtr->titlebk);
437 FillRect(hdc, title, hbr);
438 DeleteObject(hbr);
441 /* if the previous button is pressed draw it depressed */
442 if(IntersectRect(&rcTemp, &(ps->rcPaint), prev))
444 if((infoPtr->status & MC_PREVPRESSED))
445 DrawFrameControl(hdc, prev, DFC_SCROLL,
446 DFCS_SCROLLLEFT | DFCS_PUSHED |
447 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
448 else /* if the previous button is pressed draw it depressed */
449 DrawFrameControl(hdc, prev, DFC_SCROLL,
450 DFCS_SCROLLLEFT |(dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
453 /* if next button is depressed draw it depressed */
454 if(IntersectRect(&rcTemp, &(ps->rcPaint), next))
456 if((infoPtr->status & MC_NEXTPRESSED))
457 DrawFrameControl(hdc, next, DFC_SCROLL,
458 DFCS_SCROLLRIGHT | DFCS_PUSHED |
459 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
460 else /* if the next button is pressed draw it depressed */
461 DrawFrameControl(hdc, next, DFC_SCROLL,
462 DFCS_SCROLLRIGHT |(dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
465 oldBkColor = SetBkColor(hdc, infoPtr->titlebk);
466 SetTextColor(hdc, infoPtr->titletxt);
467 currentFont = SelectObject(hdc, infoPtr->hBoldFont);
469 /* titlemonth->left and right are set in MONTHCAL_UpdateSize */
470 titlemonth->left = title->left;
471 titlemonth->right = title->right;
473 GetLocaleInfoA( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+infoPtr->currentMonth -1,
474 buf1,sizeof(buf1));
475 sprintf(buf, "%s %ld", buf1, infoPtr->currentYear);
477 if(IntersectRect(&rcTemp, &(ps->rcPaint), titlemonth))
479 DrawTextA(hdc, buf, strlen(buf), titlemonth,
480 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
483 SelectObject(hdc, infoPtr->hFont);
485 /* titlemonth left/right contained rect for whole titletxt('June 1999')
486 * MCM_HitTestInfo wants month & year rects, so prepare these now.
487 *(no, we can't draw them separately; the whole text is centered)
489 GetTextExtentPoint32A(hdc, buf, strlen(buf), &size);
490 titlemonth->left = title->right / 2 - size.cx / 2;
491 titleyear->right = title->right / 2 + size.cx / 2;
492 GetTextExtentPoint32A(hdc, buf1, strlen(buf1), &size);
493 titlemonth->right = titlemonth->left + size.cx;
494 titleyear->left = titlemonth->right;
496 /* draw month area */
497 rcTemp.top=infoPtr->wdays.top;
498 rcTemp.left=infoPtr->wdays.left;
499 rcTemp.bottom=infoPtr->todayrect.bottom;
500 rcTemp.right =infoPtr->todayrect.right;
501 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcTemp))
503 hbr = CreateSolidBrush(infoPtr->monthbk);
504 FillRect(hdc, &rcTemp, hbr);
505 DeleteObject(hbr);
508 /* draw line under day abbreviatons */
510 MoveToEx(hdc, infoPtr->days.left + 3, title->bottom + textHeight + 1, NULL);
511 LineTo(hdc, infoPtr->days.right - 3, title->bottom + textHeight + 1);
513 prevMonth = infoPtr->currentMonth - 1;
514 if(prevMonth == 0) /* if currentMonth is january(1) prevMonth is */
515 prevMonth = 12; /* december(12) of the previous year */
517 infoPtr->wdays.left = infoPtr->days.left = infoPtr->weeknums.right;
518 /* draw day abbreviations */
520 SetBkColor(hdc, infoPtr->monthbk);
521 SetTextColor(hdc, infoPtr->trailingtxt);
523 /* copy this rect so we can change the values without changing */
524 /* the original version */
525 days->left = infoPtr->wdays.left;
526 days->right = days->left + infoPtr->width_increment;
527 days->top = infoPtr->wdays.top;
528 days->bottom = infoPtr->wdays.bottom;
530 i = infoPtr->firstDay;
532 for(j=0; j<7; j++) {
533 GetLocaleInfoA( LOCALE_USER_DEFAULT,LOCALE_SABBREVDAYNAME1 + (i +j)%7,
534 buf,sizeof(buf));
535 DrawTextA(hdc, buf, strlen(buf), days,
536 DT_CENTER | DT_VCENTER | DT_SINGLELINE );
537 days->left+=infoPtr->width_increment;
538 days->right+=infoPtr->width_increment;
541 /* draw day numbers; first, the previous month */
543 firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear);
545 day = MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear) +
546 (infoPtr->firstDay + 7 - firstDay)%7 + 1;
547 if (day > MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear))
548 day -=7;
549 startofprescal = day;
550 mask = 1<<(day-1);
552 i = 0;
553 m = 0;
554 while(day <= MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear)) {
555 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
556 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
558 MONTHCAL_DrawDay(infoPtr, hdc, day, prevMonth, i, 0,
559 infoPtr->monthdayState[m] & mask);
562 mask<<=1;
563 day++;
564 i++;
567 /* draw `current' month */
569 day = 1; /* start at the beginning of the current month */
571 infoPtr->firstDayplace = i;
572 SetTextColor(hdc, infoPtr->txt);
573 m++;
574 mask = 1;
576 /* draw the first week of the current month */
577 while(i<7) {
578 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
579 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
582 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth, i, 0,
583 infoPtr->monthdayState[m] & mask);
585 if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
586 (day==infoPtr->todaysDate.wDay) &&
587 (infoPtr->currentYear == infoPtr->todaysDate.wYear)) {
588 if(!(dwStyle & MCS_NOTODAYCIRCLE))
589 MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->currentMonth);
593 mask<<=1;
594 day++;
595 i++;
598 j = 1; /* move to the 2nd week of the current month */
599 i = 0; /* move back to sunday */
600 while(day <= MONTHCAL_MonthLength(infoPtr->currentMonth, infoPtr->currentYear)) {
601 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
602 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
604 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth, i, j,
605 infoPtr->monthdayState[m] & mask);
607 if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
608 (day==infoPtr->todaysDate.wDay) &&
609 (infoPtr->currentYear == infoPtr->todaysDate.wYear))
610 if(!(dwStyle & MCS_NOTODAYCIRCLE))
611 MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->currentMonth);
613 mask<<=1;
614 day++;
615 i++;
616 if(i>6) { /* past saturday, goto the next weeks sunday */
617 i = 0;
618 j++;
622 /* draw `next' month */
624 day = 1; /* start at the first day of the next month */
625 m++;
626 mask = 1;
628 SetTextColor(hdc, infoPtr->trailingtxt);
629 while((i<7) &&(j<6)) {
630 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
631 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
633 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth + 1, i, j,
634 infoPtr->monthdayState[m] & mask);
637 mask<<=1;
638 day++;
639 i++;
640 if(i==7) { /* past saturday, go to next week's sunday */
641 i = 0;
642 j++;
645 SetTextColor(hdc, infoPtr->txt);
648 /* draw `today' date if style allows it, and draw a circle before today's
649 * date if necessary */
651 if(!(dwStyle & MCS_NOTODAY)) {
652 int offset = 0;
653 if(!(dwStyle & MCS_NOTODAYCIRCLE)) {
654 /*day is the number of days from nextmonth we put on the calendar */
655 MONTHCAL_CircleDay(infoPtr, hdc,
656 day+MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear),
657 infoPtr->currentMonth);
658 offset+=textWidth;
660 if (!LoadStringA(COMCTL32_hModule,IDM_TODAY,buf1,sizeof(buf1)))
662 WARN("Can't load resource\n");
663 strcpy(buf1,"Today:");
665 MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
666 MONTHCAL_CopyTime(&infoPtr->todaysDate,&localtime);
667 GetDateFormatA(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&localtime,NULL,buf2,sizeof(buf2));
668 sprintf(buf, "%s %s", buf1,buf2);
669 SelectObject(hdc, infoPtr->hBoldFont);
671 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rtoday))
673 DrawTextA(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
674 DrawTextA(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
676 SelectObject(hdc, infoPtr->hFont);
679 /*eventually draw week numbers*/
680 if(dwStyle & MCS_WEEKNUMBERS) {
681 /* display weeknumbers*/
682 int mindays;
684 /* Rules what week to call the first week of a new year:
685 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
686 The week containing Jan 1 is the first week of year
687 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
688 First week of year must contain 4 days of the new year
689 LOCALE_IFIRSTWEEKOFYEAR == 1 (what contries?)
690 The first week of the year must contain only days of the new year
692 GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR,
693 buf, sizeof(buf));
694 sscanf(buf, "%d", &weeknum);
695 switch (weeknum)
697 case 1: mindays = 6;
698 break;
699 case 2: mindays = 3;
700 break;
701 case 0:
702 default:
703 mindays = 0;
705 if (infoPtr->currentMonth < 2)
707 /* calculate all those exceptions for january */
708 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear);
709 if ((infoPtr->firstDay +7 - weeknum1)%7 > mindays)
710 weeknum =1;
711 else
713 weeknum = 0;
714 for(i=0; i<11; i++)
715 weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->currentYear-1);
716 weeknum +=startofprescal+ 7;
717 weeknum /=7;
718 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear-1);
719 if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
720 weeknum++;
723 else
725 weeknum = 0;
726 for(i=0; i<prevMonth-1; i++)
727 weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->currentYear);
728 weeknum +=startofprescal+ 7;
729 weeknum /=7;
730 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear);
731 if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
732 weeknum++;
734 days->left = infoPtr->weeknums.left;
735 days->right = infoPtr->weeknums.right;
736 days->top = infoPtr->weeknums.top;
737 days->bottom = days->top +infoPtr->height_increment;
738 for(i=0; i<6; i++) {
739 if((i==0)&&(weeknum>50))
741 sprintf(buf, "%d", weeknum);
742 weeknum=0;
744 else if((i==5)&&(weeknum>47))
746 sprintf(buf, "%d", 1);
748 else
749 sprintf(buf, "%d", weeknum + i);
750 DrawTextA(hdc, buf, -1, days, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
751 days->top+=infoPtr->height_increment;
752 days->bottom+=infoPtr->height_increment;
755 MoveToEx(hdc, infoPtr->weeknums.right, infoPtr->weeknums.top + 3 , NULL);
756 LineTo(hdc, infoPtr->weeknums.right, infoPtr->weeknums.bottom );
759 /* currentFont was font at entering Refresh */
761 SetBkColor(hdc, oldBkColor);
762 SelectObject(hdc, currentFont);
763 SetTextColor(hdc, oldTextColor);
767 static LRESULT
768 MONTHCAL_GetMinReqRect(MONTHCAL_INFO *infoPtr, LPARAM lParam)
770 LPRECT lpRect = (LPRECT) lParam;
772 TRACE("rect %p\n", lpRect);
774 /* validate parameters */
776 if((infoPtr==NULL) ||(lpRect == NULL) ) return FALSE;
778 lpRect->left = infoPtr->title.left;
779 lpRect->top = infoPtr->title.top;
780 lpRect->right = infoPtr->title.right;
781 lpRect->bottom = infoPtr->todayrect.bottom;
782 AdjustWindowRect(lpRect, GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE), FALSE);
784 TRACE("%s\n", wine_dbgstr_rect(lpRect));
786 return TRUE;
790 static LRESULT
791 MONTHCAL_GetColor(MONTHCAL_INFO *infoPtr, WPARAM wParam)
793 TRACE("\n");
795 switch((int)wParam) {
796 case MCSC_BACKGROUND:
797 return infoPtr->bk;
798 case MCSC_TEXT:
799 return infoPtr->txt;
800 case MCSC_TITLEBK:
801 return infoPtr->titlebk;
802 case MCSC_TITLETEXT:
803 return infoPtr->titletxt;
804 case MCSC_MONTHBK:
805 return infoPtr->monthbk;
806 case MCSC_TRAILINGTEXT:
807 return infoPtr->trailingtxt;
810 return -1;
814 static LRESULT
815 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
817 int prev = -1;
819 TRACE("%d: color %08lx\n", wParam, lParam);
821 switch((int)wParam) {
822 case MCSC_BACKGROUND:
823 prev = infoPtr->bk;
824 infoPtr->bk = (COLORREF)lParam;
825 break;
826 case MCSC_TEXT:
827 prev = infoPtr->txt;
828 infoPtr->txt = (COLORREF)lParam;
829 break;
830 case MCSC_TITLEBK:
831 prev = infoPtr->titlebk;
832 infoPtr->titlebk = (COLORREF)lParam;
833 break;
834 case MCSC_TITLETEXT:
835 prev=infoPtr->titletxt;
836 infoPtr->titletxt = (COLORREF)lParam;
837 break;
838 case MCSC_MONTHBK:
839 prev = infoPtr->monthbk;
840 infoPtr->monthbk = (COLORREF)lParam;
841 break;
842 case MCSC_TRAILINGTEXT:
843 prev = infoPtr->trailingtxt;
844 infoPtr->trailingtxt = (COLORREF)lParam;
845 break;
848 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
849 return prev;
853 static LRESULT
854 MONTHCAL_GetMonthDelta(MONTHCAL_INFO *infoPtr)
856 TRACE("\n");
858 if(infoPtr->delta)
859 return infoPtr->delta;
860 else
861 return infoPtr->visible;
865 static LRESULT
866 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, WPARAM wParam)
868 int prev = infoPtr->delta;
870 TRACE("delta %d\n", wParam);
872 infoPtr->delta = (int)wParam;
873 return prev;
877 static LRESULT
878 MONTHCAL_GetFirstDayOfWeek(MONTHCAL_INFO *infoPtr)
880 return infoPtr->firstDay;
884 /* sets the first day of the week that will appear in the control */
885 /* 0 == Monday, 6 == Sunday */
886 /* FIXME: this needs to be implemented properly in MONTHCAL_Refresh() */
887 /* FIXME: we need more error checking here */
888 static LRESULT
889 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, LPARAM lParam)
891 int prev = infoPtr->firstDay;
892 char buf[40];
893 int day;
895 TRACE("day %ld\n", lParam);
897 if((lParam >= 0) && (lParam < 7)) {
898 infoPtr->firstDay = (int)lParam;
900 else
902 GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK,
903 buf, sizeof(buf));
904 TRACE("%s %d\n", buf, strlen(buf));
905 if(sscanf(buf, "%d", &day) == 1)
906 infoPtr->firstDay = day;
907 else
908 infoPtr->firstDay = 0;
910 return prev;
914 static LRESULT
915 MONTHCAL_GetMonthRange(MONTHCAL_INFO *infoPtr)
917 TRACE("\n");
919 return infoPtr->monthRange;
923 static LRESULT
924 MONTHCAL_GetMaxTodayWidth(MONTHCAL_INFO *infoPtr)
926 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
930 /* FIXME: are validated times taken from current date/time or simply
931 * copied?
932 * FIXME: check whether MCM_GETMONTHRANGE shows correct result after
933 * adjusting range with MCM_SETRANGE
936 static LRESULT
937 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
939 SYSTEMTIME *lprgSysTimeArray=(SYSTEMTIME *)lParam;
940 int prev;
942 TRACE("%x %lx\n", wParam, lParam);
944 if(wParam & GDTR_MAX) {
945 if(MONTHCAL_ValidateTime(lprgSysTimeArray[1])){
946 MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxDate);
947 infoPtr->rangeValid|=GDTR_MAX;
948 } else {
949 GetSystemTime(&infoPtr->todaysDate);
950 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
953 if(wParam & GDTR_MIN) {
954 if(MONTHCAL_ValidateTime(lprgSysTimeArray[0])) {
955 MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minDate);
956 infoPtr->rangeValid|=GDTR_MIN;
957 } else {
958 GetSystemTime(&infoPtr->todaysDate);
959 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->minDate);
963 prev = infoPtr->monthRange;
964 infoPtr->monthRange = infoPtr->maxDate.wMonth - infoPtr->minDate.wMonth;
966 if(infoPtr->monthRange!=prev) {
967 infoPtr->monthdayState = ReAlloc(infoPtr->monthdayState,
968 infoPtr->monthRange * sizeof(MONTHDAYSTATE));
971 return 1;
975 static LRESULT
976 MONTHCAL_GetRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
978 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
979 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *)lParam;
981 /* validate parameters */
983 if((infoPtr==NULL) || (lprgSysTimeArray==NULL)) return FALSE;
985 MONTHCAL_CopyTime(&infoPtr->maxDate, &lprgSysTimeArray[1]);
986 MONTHCAL_CopyTime(&infoPtr->minDate, &lprgSysTimeArray[0]);
988 return infoPtr->rangeValid;
992 static LRESULT
993 MONTHCAL_SetDayState(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
996 int i, iMonths = (int)wParam;
997 MONTHDAYSTATE *dayStates = (LPMONTHDAYSTATE)lParam;
999 TRACE("%x %lx\n", wParam, lParam);
1000 if(iMonths!=infoPtr->monthRange) return 0;
1002 for(i=0; i<iMonths; i++)
1003 infoPtr->monthdayState[i] = dayStates[i];
1004 return 1;
1007 static LRESULT
1008 MONTHCAL_GetCurSel(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1010 SYSTEMTIME *lpSel = (SYSTEMTIME *) lParam;
1012 TRACE("%lx\n", lParam);
1013 if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
1014 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
1016 MONTHCAL_CopyTime(&infoPtr->minSel, lpSel);
1017 TRACE("%d/%d/%d\n", lpSel->wYear, lpSel->wMonth, lpSel->wDay);
1018 return TRUE;
1021 /* FIXME: if the specified date is not visible, make it visible */
1022 /* FIXME: redraw? */
1023 static LRESULT
1024 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1026 SYSTEMTIME *lpSel = (SYSTEMTIME *)lParam;
1028 TRACE("%lx\n", lParam);
1029 if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
1030 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
1032 infoPtr->currentMonth=lpSel->wMonth;
1033 infoPtr->currentYear=lpSel->wYear;
1035 MONTHCAL_CopyTime(lpSel, &infoPtr->minSel);
1036 MONTHCAL_CopyTime(lpSel, &infoPtr->maxSel);
1038 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1040 return TRUE;
1044 static LRESULT
1045 MONTHCAL_GetMaxSelCount(MONTHCAL_INFO *infoPtr)
1047 return infoPtr->maxSelCount;
1051 static LRESULT
1052 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1054 TRACE("%x\n", wParam);
1056 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) {
1057 infoPtr->maxSelCount = wParam;
1060 return TRUE;
1064 static LRESULT
1065 MONTHCAL_GetSelRange(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1067 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1069 TRACE("%lx\n", lParam);
1071 /* validate parameters */
1073 if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1075 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)
1077 MONTHCAL_CopyTime(&infoPtr->maxSel, &lprgSysTimeArray[1]);
1078 MONTHCAL_CopyTime(&infoPtr->minSel, &lprgSysTimeArray[0]);
1079 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1080 return TRUE;
1083 return FALSE;
1087 static LRESULT
1088 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1090 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1092 TRACE("%lx\n", lParam);
1094 /* validate parameters */
1096 if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1098 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)
1100 MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxSel);
1101 MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minSel);
1102 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1103 return TRUE;
1106 return FALSE;
1110 static LRESULT
1111 MONTHCAL_GetToday(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1113 SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1115 TRACE("%lx\n", lParam);
1117 /* validate parameters */
1119 if((infoPtr==NULL) || (lpToday==NULL)) return FALSE;
1120 MONTHCAL_CopyTime(&infoPtr->todaysDate, lpToday);
1121 return TRUE;
1125 static LRESULT
1126 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1128 SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1130 TRACE("%lx\n", lParam);
1132 /* validate parameters */
1134 if((infoPtr==NULL) ||(lpToday==NULL)) return FALSE;
1135 MONTHCAL_CopyTime(lpToday, &infoPtr->todaysDate);
1136 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1137 return TRUE;
1141 static LRESULT
1142 MONTHCAL_HitTest(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1144 PMCHITTESTINFO lpht = (PMCHITTESTINFO)lParam;
1145 UINT x,y;
1146 DWORD retval;
1147 int day,wday,wnum;
1150 x = lpht->pt.x;
1151 y = lpht->pt.y;
1152 retval = MCHT_NOWHERE;
1155 /* Comment in for debugging...
1156 TRACE("%d %d wd[%d %d %d %d] d[%d %d %d %d] t[%d %d %d %d] wn[%d %d %d %d]\n", x, y,
1157 infoPtr->wdays.left, infoPtr->wdays.right,
1158 infoPtr->wdays.top, infoPtr->wdays.bottom,
1159 infoPtr->days.left, infoPtr->days.right,
1160 infoPtr->days.top, infoPtr->days.bottom,
1161 infoPtr->todayrect.left, infoPtr->todayrect.right,
1162 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1163 infoPtr->weeknums.left, infoPtr->weeknums.right,
1164 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1167 /* are we in the header? */
1169 if(PtInRect(&infoPtr->title, lpht->pt)) {
1170 if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1171 retval = MCHT_TITLEBTNPREV;
1172 goto done;
1174 if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1175 retval = MCHT_TITLEBTNNEXT;
1176 goto done;
1178 if(PtInRect(&infoPtr->titlemonth, lpht->pt)) {
1179 retval = MCHT_TITLEMONTH;
1180 goto done;
1182 if(PtInRect(&infoPtr->titleyear, lpht->pt)) {
1183 retval = MCHT_TITLEYEAR;
1184 goto done;
1187 retval = MCHT_TITLE;
1188 goto done;
1191 day = MONTHCAL_CalcDayFromPos(infoPtr,x,y,&wday,&wnum);
1192 if(PtInRect(&infoPtr->wdays, lpht->pt)) {
1193 retval = MCHT_CALENDARDAY;
1194 lpht->st.wYear = infoPtr->currentYear;
1195 lpht->st.wMonth = (day < 1)? infoPtr->currentMonth -1 : infoPtr->currentMonth;
1196 lpht->st.wDay = (day < 1)?
1197 MONTHCAL_MonthLength(infoPtr->currentMonth-1,infoPtr->currentYear) -day : day;
1198 goto done;
1200 if(PtInRect(&infoPtr->weeknums, lpht->pt)) {
1201 retval = MCHT_CALENDARWEEKNUM;
1202 lpht->st.wYear = infoPtr->currentYear;
1203 lpht->st.wMonth = (day < 1) ? infoPtr->currentMonth -1 :
1204 (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear)) ?
1205 infoPtr->currentMonth +1 :infoPtr->currentMonth;
1206 lpht->st.wDay = (day < 1 ) ?
1207 MONTHCAL_MonthLength(infoPtr->currentMonth-1,infoPtr->currentYear) -day :
1208 (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear)) ?
1209 day - MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear) : day;
1210 goto done;
1212 if(PtInRect(&infoPtr->days, lpht->pt))
1214 lpht->st.wYear = infoPtr->currentYear;
1215 if ( day < 1)
1217 retval = MCHT_CALENDARDATEPREV;
1218 lpht->st.wMonth = infoPtr->currentMonth - 1;
1219 if (lpht->st.wMonth <1)
1221 lpht->st.wMonth = 12;
1222 lpht->st.wYear--;
1224 lpht->st.wDay = MONTHCAL_MonthLength(lpht->st.wMonth,lpht->st.wYear) -day;
1226 else if (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear))
1228 retval = MCHT_CALENDARDATENEXT;
1229 lpht->st.wMonth = infoPtr->currentMonth + 1;
1230 if (lpht->st.wMonth <12)
1232 lpht->st.wMonth = 1;
1233 lpht->st.wYear++;
1235 lpht->st.wDay = day - MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear) ;
1237 else {
1238 retval = MCHT_CALENDARDATE;
1239 lpht->st.wMonth = infoPtr->currentMonth;
1240 lpht->st.wDay = day;
1242 goto done;
1244 if(PtInRect(&infoPtr->todayrect, lpht->pt)) {
1245 retval = MCHT_TODAYLINK;
1246 goto done;
1250 /* Hit nothing special? What's left must be background :-) */
1252 retval = MCHT_CALENDARBK;
1253 done:
1254 lpht->uHit = retval;
1255 return retval;
1259 static void MONTHCAL_GoToNextMonth(MONTHCAL_INFO *infoPtr)
1261 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1263 TRACE("MONTHCAL_GoToNextMonth\n");
1265 infoPtr->currentMonth++;
1266 if(infoPtr->currentMonth > 12) {
1267 infoPtr->currentYear++;
1268 infoPtr->currentMonth = 1;
1271 if(dwStyle & MCS_DAYSTATE) {
1272 NMDAYSTATE nmds;
1273 int i;
1275 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1276 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1277 nmds.nmhdr.code = MCN_GETDAYSTATE;
1278 nmds.cDayState = infoPtr->monthRange;
1279 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1281 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1282 (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
1283 for(i=0; i<infoPtr->monthRange; i++)
1284 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1289 static void MONTHCAL_GoToPrevMonth(MONTHCAL_INFO *infoPtr)
1291 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1293 TRACE("\n");
1295 infoPtr->currentMonth--;
1296 if(infoPtr->currentMonth < 1) {
1297 infoPtr->currentYear--;
1298 infoPtr->currentMonth = 12;
1301 if(dwStyle & MCS_DAYSTATE) {
1302 NMDAYSTATE nmds;
1303 int i;
1305 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1306 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1307 nmds.nmhdr.code = MCN_GETDAYSTATE;
1308 nmds.cDayState = infoPtr->monthRange;
1309 nmds.prgDayState = Alloc
1310 (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1312 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1313 (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
1314 for(i=0; i<infoPtr->monthRange; i++)
1315 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1319 static LRESULT
1320 MONTHCAL_RButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1322 HMENU hMenu;
1323 POINT menupoint;
1324 char buf[32];
1326 hMenu = CreatePopupMenu();
1327 if (!LoadStringA(COMCTL32_hModule,IDM_GOTODAY,buf,sizeof(buf)))
1329 WARN("Can't load resource\n");
1330 strcpy(buf,"Go to Today:");
1332 AppendMenuA(hMenu, MF_STRING|MF_ENABLED,1, buf);
1333 menupoint.x=(INT)LOWORD(lParam);
1334 menupoint.y=(INT)HIWORD(lParam);
1335 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1336 if( TrackPopupMenu(hMenu,TPM_RIGHTBUTTON| TPM_NONOTIFY|TPM_RETURNCMD,
1337 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1339 infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
1340 infoPtr->currentYear=infoPtr->todaysDate.wYear;
1341 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1343 return 0;
1346 static LRESULT
1347 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1349 MCHITTESTINFO ht;
1350 DWORD hit;
1351 HMENU hMenu;
1352 RECT rcDay; /* used in determining area to invalidate */
1353 char buf[32];
1354 int i;
1355 POINT menupoint;
1357 TRACE("%lx\n", lParam);
1359 if (infoPtr->hWndYearUpDown)
1361 infoPtr->currentYear=SendMessageA( infoPtr->hWndYearUpDown, UDM_SETPOS, (WPARAM) 0,(LPARAM)0);
1362 if(!DestroyWindow(infoPtr->hWndYearUpDown))
1364 FIXME("Can't destroy Updown Control\n");
1366 else
1367 infoPtr->hWndYearUpDown=0;
1368 if(!DestroyWindow(infoPtr->hWndYearEdit))
1370 FIXME("Can't destroy Updown Control\n");
1372 else
1373 infoPtr->hWndYearEdit=0;
1374 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1377 ht.pt.x = (INT)LOWORD(lParam);
1378 ht.pt.y = (INT)HIWORD(lParam);
1379 hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1381 /* FIXME: these flags should be checked by */
1382 /*((hit & MCHT_XXX) == MCHT_XXX) b/c some of the flags are */
1383 /* multi-bit */
1384 if(hit ==MCHT_TITLEBTNNEXT) {
1385 MONTHCAL_GoToNextMonth(infoPtr);
1386 infoPtr->status = MC_NEXTPRESSED;
1387 SetTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1388 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1389 return TRUE;
1391 if(hit == MCHT_TITLEBTNPREV){
1392 MONTHCAL_GoToPrevMonth(infoPtr);
1393 infoPtr->status = MC_PREVPRESSED;
1394 SetTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1395 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1396 return TRUE;
1399 if(hit == MCHT_TITLEMONTH) {
1400 hMenu = CreatePopupMenu();
1402 for (i=0; i<12;i++)
1404 GetLocaleInfoA( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+i,
1405 buf,sizeof(buf));
1406 AppendMenuA(hMenu, MF_STRING|MF_ENABLED,i+1, buf);
1408 menupoint.x=infoPtr->titlemonth.right;
1409 menupoint.y=infoPtr->titlemonth.bottom;
1410 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1411 i= TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
1412 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
1413 if ((i>0) && (i<13))
1415 infoPtr->currentMonth=i;
1416 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1419 if(hit == MCHT_TITLEYEAR) {
1420 infoPtr->hWndYearEdit=CreateWindowExA(0,
1421 "EDIT",
1423 WS_VISIBLE | WS_CHILD |UDS_SETBUDDYINT,
1424 infoPtr->titleyear.left+3,infoPtr->titlebtnnext.top,
1425 infoPtr->titleyear.right-infoPtr->titleyear.left,
1426 infoPtr->textHeight,
1427 infoPtr->hwndSelf,
1428 NULL,
1429 NULL,
1430 NULL);
1431 infoPtr->hWndYearUpDown=CreateWindowExA(0,
1432 UPDOWN_CLASSA,
1434 WS_VISIBLE | WS_CHILD |UDS_SETBUDDYINT|UDS_NOTHOUSANDS|UDS_ARROWKEYS,
1435 infoPtr->titleyear.right+6,infoPtr->titlebtnnext.top,
1437 infoPtr->textHeight,
1438 infoPtr->hwndSelf,
1439 NULL,
1440 NULL,
1441 NULL);
1442 SendMessageA( infoPtr->hWndYearUpDown, UDM_SETRANGE, (WPARAM) 0, MAKELONG (9999, 1753));
1443 SendMessageA( infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM) infoPtr->hWndYearEdit, (LPARAM)0 );
1444 SendMessageA( infoPtr->hWndYearUpDown, UDM_SETPOS, (WPARAM) 0,(LPARAM)infoPtr->currentYear );
1445 return TRUE;
1448 if(hit == MCHT_TODAYLINK) {
1449 infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
1450 infoPtr->currentYear=infoPtr->todaysDate.wYear;
1451 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1452 return TRUE;
1454 if(hit == MCHT_CALENDARDATE) {
1455 SYSTEMTIME selArray[2];
1456 NMSELCHANGE nmsc;
1458 MONTHCAL_CopyTime(&ht.st, &selArray[0]);
1459 MONTHCAL_CopyTime(&ht.st, &selArray[1]);
1460 MONTHCAL_SetSelRange(infoPtr, (LPARAM)&selArray);
1461 MONTHCAL_SetCurSel(infoPtr, (LPARAM)&selArray);
1462 TRACE("MCHT_CALENDARDATE\n");
1463 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1464 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1465 nmsc.nmhdr.code = MCN_SELCHANGE;
1466 MONTHCAL_CopyTime(&infoPtr->minSel,&nmsc.stSelStart);
1467 MONTHCAL_CopyTime(&infoPtr->maxSel,&nmsc.stSelEnd);
1469 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1470 (WPARAM)nmsc.nmhdr.idFrom,(LPARAM)&nmsc);
1473 /* redraw both old and new days if the selected day changed */
1474 if(infoPtr->curSelDay != ht.st.wDay) {
1475 MONTHCAL_CalcPosFromDay(infoPtr, ht.st.wDay, ht.st.wMonth, &rcDay);
1476 InvalidateRect(infoPtr->hwndSelf, &rcDay, TRUE);
1478 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->curSelDay, infoPtr->currentMonth, &rcDay);
1479 InvalidateRect(infoPtr->hwndSelf, &rcDay, TRUE);
1482 infoPtr->firstSelDay = ht.st.wDay;
1483 infoPtr->curSelDay = ht.st.wDay;
1484 infoPtr->status = MC_SEL_LBUTDOWN;
1485 return TRUE;
1488 return 0;
1492 static LRESULT
1493 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1495 NMSELCHANGE nmsc;
1496 NMHDR nmhdr;
1497 BOOL redraw = FALSE;
1498 MCHITTESTINFO ht;
1499 DWORD hit;
1501 TRACE("\n");
1503 if(infoPtr->status & MC_NEXTPRESSED) {
1504 KillTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER);
1505 infoPtr->status &= ~MC_NEXTPRESSED;
1506 redraw = TRUE;
1508 if(infoPtr->status & MC_PREVPRESSED) {
1509 KillTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER);
1510 infoPtr->status &= ~MC_PREVPRESSED;
1511 redraw = TRUE;
1514 ht.pt.x = (INT)LOWORD(lParam);
1515 ht.pt.y = (INT)HIWORD(lParam);
1516 hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1518 infoPtr->status = MC_SEL_LBUTUP;
1520 if(hit ==MCHT_CALENDARDATENEXT) {
1521 MONTHCAL_GoToNextMonth(infoPtr);
1522 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1523 return TRUE;
1525 if(hit == MCHT_CALENDARDATEPREV){
1526 MONTHCAL_GoToPrevMonth(infoPtr);
1527 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1528 return TRUE;
1530 nmhdr.hwndFrom = infoPtr->hwndSelf;
1531 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1532 nmhdr.code = NM_RELEASEDCAPTURE;
1533 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
1535 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1536 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
1537 /* redraw if necessary */
1538 if(redraw)
1539 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1540 /* only send MCN_SELECT if currently displayed month's day was selected */
1541 if(hit == MCHT_CALENDARDATE) {
1542 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1543 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1544 nmsc.nmhdr.code = MCN_SELECT;
1545 MONTHCAL_CopyTime(&infoPtr->minSel, &nmsc.stSelStart);
1546 MONTHCAL_CopyTime(&infoPtr->maxSel, &nmsc.stSelEnd);
1548 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1549 (WPARAM)nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1552 return 0;
1556 static LRESULT
1557 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1559 BOOL redraw = FALSE;
1561 TRACE("%d\n", wParam);
1563 switch(wParam) {
1564 case MC_NEXTMONTHTIMER:
1565 redraw = TRUE;
1566 MONTHCAL_GoToNextMonth(infoPtr);
1567 break;
1568 case MC_PREVMONTHTIMER:
1569 redraw = TRUE;
1570 MONTHCAL_GoToPrevMonth(infoPtr);
1571 break;
1572 default:
1573 ERR("got unknown timer\n");
1574 break;
1577 /* redraw only if necessary */
1578 if(redraw)
1579 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1581 return 0;
1585 static LRESULT
1586 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1588 MCHITTESTINFO ht;
1589 int oldselday, selday, hit;
1590 RECT r;
1592 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
1594 ht.pt.x = LOWORD(lParam);
1595 ht.pt.y = HIWORD(lParam);
1597 hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1599 /* not on the calendar date numbers? bail out */
1600 TRACE("hit:%x\n",hit);
1601 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE) return 0;
1603 selday = ht.st.wDay;
1604 oldselday = infoPtr->curSelDay;
1605 infoPtr->curSelDay = selday;
1606 MONTHCAL_CalcPosFromDay(infoPtr, selday, ht.st. wMonth, &r);
1608 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) {
1609 SYSTEMTIME selArray[2];
1610 int i;
1612 MONTHCAL_GetSelRange(infoPtr, (LPARAM)&selArray);
1613 i = 0;
1614 if(infoPtr->firstSelDay==selArray[0].wDay) i=1;
1615 TRACE("oldRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1616 if(infoPtr->firstSelDay==selArray[1].wDay) {
1617 /* 1st time we get here: selArray[0]=selArray[1]) */
1618 /* if we're still at the first selected date, return */
1619 if(infoPtr->firstSelDay==selday) goto done;
1620 if(selday<infoPtr->firstSelDay) i = 0;
1623 if(abs(infoPtr->firstSelDay - selday) >= infoPtr->maxSelCount) {
1624 if(selday>infoPtr->firstSelDay)
1625 selday = infoPtr->firstSelDay + infoPtr->maxSelCount;
1626 else
1627 selday = infoPtr->firstSelDay - infoPtr->maxSelCount;
1630 if(selArray[i].wDay!=selday) {
1631 TRACE("newRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1633 selArray[i].wDay = selday;
1635 if(selArray[0].wDay>selArray[1].wDay) {
1636 DWORD tempday;
1637 tempday = selArray[1].wDay;
1638 selArray[1].wDay = selArray[0].wDay;
1639 selArray[0].wDay = tempday;
1642 MONTHCAL_SetSelRange(infoPtr, (LPARAM)&selArray);
1646 done:
1648 /* only redraw if the currently selected day changed */
1649 /* FIXME: this should specify a rectangle containing only the days that changed */
1650 /* using InvalidateRect */
1651 if(oldselday != infoPtr->curSelDay)
1652 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1654 return 0;
1658 static LRESULT
1659 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1661 HDC hdc;
1662 PAINTSTRUCT ps;
1664 if (wParam)
1666 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
1667 hdc = (HDC)wParam;
1669 else
1670 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
1672 MONTHCAL_Refresh(infoPtr, hdc, &ps);
1673 if (!wParam) EndPaint(infoPtr->hwndSelf, &ps);
1674 return 0;
1678 static LRESULT
1679 MONTHCAL_KillFocus(MONTHCAL_INFO *infoPtr)
1681 TRACE("\n");
1683 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1685 return 0;
1689 static LRESULT
1690 MONTHCAL_SetFocus(MONTHCAL_INFO *infoPtr)
1692 TRACE("\n");
1694 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1696 return 0;
1699 /* sets the size information */
1700 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
1702 HDC hdc = GetDC(infoPtr->hwndSelf);
1703 RECT *title=&infoPtr->title;
1704 RECT *prev=&infoPtr->titlebtnprev;
1705 RECT *next=&infoPtr->titlebtnnext;
1706 RECT *titlemonth=&infoPtr->titlemonth;
1707 RECT *titleyear=&infoPtr->titleyear;
1708 RECT *wdays=&infoPtr->wdays;
1709 RECT *weeknumrect=&infoPtr->weeknums;
1710 RECT *days=&infoPtr->days;
1711 RECT *todayrect=&infoPtr->todayrect;
1712 SIZE size;
1713 TEXTMETRICA tm;
1714 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1715 HFONT currentFont;
1716 int xdiv, left_offset;
1717 RECT rcClient;
1719 GetClientRect(infoPtr->hwndSelf, &rcClient);
1721 currentFont = SelectObject(hdc, infoPtr->hFont);
1723 /* get the height and width of each day's text */
1724 GetTextMetricsA(hdc, &tm);
1725 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
1726 GetTextExtentPoint32A(hdc, "Sun", 3, &size);
1727 infoPtr->textWidth = size.cx + 2;
1729 /* recalculate the height and width increments and offsets */
1730 GetTextExtentPoint32A(hdc, "00", 2, &size);
1732 xdiv = (dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
1734 infoPtr->width_increment = size.cx * 2;
1735 infoPtr->height_increment = infoPtr->textHeight;
1736 left_offset = (rcClient.right - rcClient.left) - (infoPtr->width_increment * xdiv);
1738 /* calculate title area */
1739 title->top = rcClient.top;
1740 title->bottom = title->top + 3 * infoPtr->height_increment / 2;
1741 title->left = left_offset;
1742 title->right = rcClient.right;
1744 /* set the dimensions of the next and previous buttons and center */
1745 /* the month text vertically */
1746 prev->top = next->top = title->top + 4;
1747 prev->bottom = next->bottom = title->bottom - 4;
1748 prev->left = title->left + 4;
1749 prev->right = prev->left + (title->bottom - title->top) ;
1750 next->right = title->right - 4;
1751 next->left = next->right - (title->bottom - title->top);
1753 /* titlemonth->left and right change based upon the current month */
1754 /* and are recalculated in refresh as the current month may change */
1755 /* without the control being resized */
1756 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
1757 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
1759 /* setup the dimensions of the rectangle we draw the names of the */
1760 /* days of the week in */
1761 weeknumrect->left = left_offset;
1762 if(dwStyle & MCS_WEEKNUMBERS)
1763 weeknumrect->right=prev->right;
1764 else
1765 weeknumrect->right=weeknumrect->left;
1766 wdays->left = days->left = weeknumrect->right;
1767 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
1768 wdays->top = title->bottom ;
1769 wdays->bottom = wdays->top + infoPtr->height_increment;
1771 days->top = weeknumrect->top = wdays->bottom ;
1772 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
1774 todayrect->left = rcClient.left;
1775 todayrect->right = rcClient.right;
1776 todayrect->top = days->bottom;
1777 todayrect->bottom = days->bottom + infoPtr->height_increment;
1779 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
1780 infoPtr->width_increment,infoPtr->height_increment,
1781 wine_dbgstr_rect(&rcClient),
1782 wine_dbgstr_rect(title),
1783 wine_dbgstr_rect(wdays),
1784 wine_dbgstr_rect(days),
1785 wine_dbgstr_rect(todayrect));
1787 /* restore the originally selected font */
1788 SelectObject(hdc, currentFont);
1790 ReleaseDC(infoPtr->hwndSelf, hdc);
1793 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
1795 TRACE("(width=%d, height=%d)\n", Width, Height);
1797 MONTHCAL_UpdateSize(infoPtr);
1799 /* invalidate client area and erase background */
1800 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1802 return 0;
1805 static LRESULT MONTHCAL_GetFont(MONTHCAL_INFO *infoPtr)
1807 return (LRESULT)infoPtr->hFont;
1810 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
1812 HFONT hOldFont;
1813 LOGFONTW lf;
1815 if (!hFont) return 0;
1817 hOldFont = infoPtr->hFont;
1818 infoPtr->hFont = hFont;
1820 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
1821 lf.lfWeight = FW_BOLD;
1822 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
1824 if (redraw)
1825 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1827 return (LRESULT)hOldFont;
1830 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
1831 static LRESULT
1832 MONTHCAL_Create(HWND hwnd, WPARAM wParam, LPARAM lParam)
1834 MONTHCAL_INFO *infoPtr;
1836 /* allocate memory for info structure */
1837 infoPtr =(MONTHCAL_INFO*)Alloc(sizeof(MONTHCAL_INFO));
1838 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
1840 if(infoPtr == NULL) {
1841 ERR( "could not allocate info memory!\n");
1842 return 0;
1845 infoPtr->hwndSelf = hwnd;
1846 infoPtr->hwndNotify = ((LPCREATESTRUCTW)lParam)->hwndParent;
1848 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
1850 /* initialize info structure */
1851 /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
1853 GetSystemTime(&infoPtr->todaysDate);
1854 MONTHCAL_SetFirstDayOfWeek(infoPtr, (LPARAM)-1);
1855 infoPtr->currentMonth = infoPtr->todaysDate.wMonth;
1856 infoPtr->currentYear = infoPtr->todaysDate.wYear;
1857 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->minDate);
1858 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
1859 infoPtr->maxDate.wYear=2050;
1860 infoPtr->minDate.wYear=1950;
1861 infoPtr->maxSelCount = 7;
1862 infoPtr->monthRange = 3;
1863 infoPtr->monthdayState = Alloc
1864 (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1865 infoPtr->titlebk = GetSysColor(COLOR_ACTIVECAPTION);
1866 infoPtr->titletxt = GetSysColor(COLOR_WINDOW);
1867 infoPtr->monthbk = GetSysColor(COLOR_WINDOW);
1868 infoPtr->trailingtxt = GetSysColor(COLOR_GRAYTEXT);
1869 infoPtr->bk = GetSysColor(COLOR_WINDOW);
1870 infoPtr->txt = GetSysColor(COLOR_WINDOWTEXT);
1872 /* call MONTHCAL_UpdateSize to set all of the dimensions */
1873 /* of the control */
1874 MONTHCAL_UpdateSize(infoPtr);
1876 return 0;
1880 static LRESULT
1881 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
1883 /* free month calendar info data */
1884 if(infoPtr->monthdayState)
1885 Free(infoPtr->monthdayState);
1886 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
1887 Free(infoPtr);
1888 return 0;
1892 static LRESULT WINAPI
1893 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1895 MONTHCAL_INFO *infoPtr;
1897 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hwnd, uMsg, wParam, lParam);
1899 infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1900 if (!infoPtr && (uMsg != WM_CREATE))
1901 return DefWindowProcA(hwnd, uMsg, wParam, lParam);
1902 switch(uMsg)
1904 case MCM_GETCURSEL:
1905 return MONTHCAL_GetCurSel(infoPtr, lParam);
1907 case MCM_SETCURSEL:
1908 return MONTHCAL_SetCurSel(infoPtr, lParam);
1910 case MCM_GETMAXSELCOUNT:
1911 return MONTHCAL_GetMaxSelCount(infoPtr);
1913 case MCM_SETMAXSELCOUNT:
1914 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
1916 case MCM_GETSELRANGE:
1917 return MONTHCAL_GetSelRange(infoPtr, lParam);
1919 case MCM_SETSELRANGE:
1920 return MONTHCAL_SetSelRange(infoPtr, lParam);
1922 case MCM_GETMONTHRANGE:
1923 return MONTHCAL_GetMonthRange(infoPtr);
1925 case MCM_SETDAYSTATE:
1926 return MONTHCAL_SetDayState(infoPtr, wParam, lParam);
1928 case MCM_GETMINREQRECT:
1929 return MONTHCAL_GetMinReqRect(infoPtr, lParam);
1931 case MCM_GETCOLOR:
1932 return MONTHCAL_GetColor(infoPtr, wParam);
1934 case MCM_SETCOLOR:
1935 return MONTHCAL_SetColor(infoPtr, wParam, lParam);
1937 case MCM_GETTODAY:
1938 return MONTHCAL_GetToday(infoPtr, lParam);
1940 case MCM_SETTODAY:
1941 return MONTHCAL_SetToday(infoPtr, lParam);
1943 case MCM_HITTEST:
1944 return MONTHCAL_HitTest(infoPtr, lParam);
1946 case MCM_GETFIRSTDAYOFWEEK:
1947 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
1949 case MCM_SETFIRSTDAYOFWEEK:
1950 return MONTHCAL_SetFirstDayOfWeek(infoPtr, lParam);
1952 case MCM_GETRANGE:
1953 return MONTHCAL_GetRange(hwnd, wParam, lParam);
1955 case MCM_SETRANGE:
1956 return MONTHCAL_SetRange(infoPtr, wParam, lParam);
1958 case MCM_GETMONTHDELTA:
1959 return MONTHCAL_GetMonthDelta(infoPtr);
1961 case MCM_SETMONTHDELTA:
1962 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
1964 case MCM_GETMAXTODAYWIDTH:
1965 return MONTHCAL_GetMaxTodayWidth(infoPtr);
1967 case WM_GETDLGCODE:
1968 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1970 case WM_KILLFOCUS:
1971 return MONTHCAL_KillFocus(infoPtr);
1973 case WM_RBUTTONDOWN:
1974 return MONTHCAL_RButtonDown(infoPtr, lParam);
1976 case WM_LBUTTONDOWN:
1977 return MONTHCAL_LButtonDown(infoPtr, lParam);
1979 case WM_MOUSEMOVE:
1980 return MONTHCAL_MouseMove(infoPtr, wParam, lParam);
1982 case WM_LBUTTONUP:
1983 return MONTHCAL_LButtonUp(infoPtr, lParam);
1985 case WM_PAINT:
1986 return MONTHCAL_Paint(infoPtr, wParam);
1988 case WM_SETFOCUS:
1989 return MONTHCAL_SetFocus(infoPtr);
1991 case WM_SIZE:
1992 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1994 case WM_CREATE:
1995 return MONTHCAL_Create(hwnd, wParam, lParam);
1997 case WM_SETFONT:
1998 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2000 case WM_GETFONT:
2001 return MONTHCAL_GetFont(infoPtr);
2003 case WM_TIMER:
2004 return MONTHCAL_Timer(infoPtr, wParam);
2006 case WM_DESTROY:
2007 return MONTHCAL_Destroy(infoPtr);
2009 default:
2010 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
2011 ERR( "unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
2012 return DefWindowProcA(hwnd, uMsg, wParam, lParam);
2017 void
2018 MONTHCAL_Register(void)
2020 WNDCLASSA wndClass;
2022 ZeroMemory(&wndClass, sizeof(WNDCLASSA));
2023 wndClass.style = CS_GLOBALCLASS;
2024 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2025 wndClass.cbClsExtra = 0;
2026 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2027 wndClass.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
2028 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2029 wndClass.lpszClassName = MONTHCAL_CLASSA;
2031 RegisterClassA(&wndClass);
2035 void
2036 MONTHCAL_Unregister(void)
2038 UnregisterClassA(MONTHCAL_CLASSA, NULL);