wined3d: Move the fill mode to wined3d_rasterizer_state.
[wine.git] / dlls / comctl32 / monthcal.c
blob1de13dab36139b3f36ad166f6758351780231acb
1 /*
2 * 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>
9 * Copyright 2009-2011 Nikolay Sivov
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * TODO:
26 * -- MCM_[GS]ETUNICODEFORMAT
27 * -- handle resources better (doesn't work now);
28 * -- take care of internationalization.
29 * -- keyboard handling.
30 * -- search for FIXME
33 #include <math.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "winnls.h"
44 #include "commctrl.h"
45 #include "comctl32.h"
46 #include "uxtheme.h"
47 #include "vssym32.h"
48 #include "wine/debug.h"
49 #include "wine/heap.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
53 #define MC_SEL_LBUTUP 1 /* Left button released */
54 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
55 #define MC_PREVPRESSED 4 /* Prev month button pressed */
56 #define MC_NEXTPRESSED 8 /* Next month button pressed */
57 #define MC_PREVNEXTMONTHDELAY 350 /* when continuously pressing `next/prev
58 month', wait 350 ms before going
59 to the next/prev month */
60 #define MC_TODAYUPDATEDELAY 120000 /* time between today check for update (2 min) */
62 #define MC_PREVNEXTMONTHTIMER 1 /* Timer IDs */
63 #define MC_TODAYUPDATETIMER 2
65 #define MC_CALENDAR_PADDING 6
67 /* convert from days to 100 nanoseconds unit - used as FILETIME unit */
68 #define DAYSTO100NSECS(days) (((ULONGLONG)(days))*24*60*60*10000000)
70 enum CachedPen
72 PenRed = 0,
73 PenText,
74 PenLast
77 enum CachedBrush
79 BrushTitle = 0,
80 BrushMonth,
81 BrushBackground,
82 BrushLast
85 /* single calendar data */
86 typedef struct _CALENDAR_INFO
88 RECT title; /* rect for the header above the calendar */
89 RECT titlemonth; /* the 'month name' text in the header */
90 RECT titleyear; /* the 'year number' text in the header */
91 RECT wdays; /* week days at top */
92 RECT days; /* calendar area */
93 RECT weeknums; /* week numbers at left side */
95 SYSTEMTIME month;/* contains calendar main month/year */
96 } CALENDAR_INFO;
98 typedef struct
100 HWND hwndSelf;
101 DWORD dwStyle; /* cached GWL_STYLE */
103 COLORREF colors[MCSC_TRAILINGTEXT+1];
104 HBRUSH brushes[BrushLast];
105 HPEN pens[PenLast];
107 HFONT hFont;
108 HFONT hBoldFont;
109 int textHeight;
110 int height_increment;
111 int width_increment;
112 INT delta; /* scroll rate; # of months that the */
113 /* control moves when user clicks a scroll button */
114 int firstDay; /* Start month calendar with firstDay's day,
115 stored in SYSTEMTIME format */
116 BOOL firstDaySet; /* first week day differs from locale defined */
118 BOOL isUnicode; /* value set with MCM_SETUNICODE format */
120 MONTHDAYSTATE *monthdayState;
121 SYSTEMTIME todaysDate;
122 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
123 int status; /* See MC_SEL flags */
124 SYSTEMTIME firstSel; /* first selected day */
125 INT maxSelCount;
126 SYSTEMTIME minSel; /* contains single selection when used without MCS_MULTISELECT */
127 SYSTEMTIME maxSel;
128 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
129 DWORD rangeValid;
130 SYSTEMTIME minDate;
131 SYSTEMTIME maxDate;
133 RECT titlebtnnext; /* the `next month' button in the header */
134 RECT titlebtnprev; /* the `prev month' button in the header */
135 RECT todayrect; /* `today: xx/xx/xx' text rect */
136 HWND hwndNotify; /* Window to receive the notifications */
137 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
138 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
139 WNDPROC EditWndProc; /* original Edit window procedure */
141 CALENDAR_INFO *calendars;
142 SIZE dim; /* [cx,cy] - dimensions of calendars matrix, row/column count */
143 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
145 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
147 /* empty SYSTEMTIME const */
148 static const SYSTEMTIME st_null;
149 /* valid date limits */
150 static const SYSTEMTIME max_allowed_date = { /* wYear */ 9999, /* wMonth */ 12, /* wDayOfWeek */ 0, /* wDay */ 31 };
151 static const SYSTEMTIME min_allowed_date = { /* wYear */ 1752, /* wMonth */ 9, /* wDayOfWeek */ 0, /* wDay */ 14 };
153 /* Prev/Next buttons */
154 enum nav_direction
156 DIRECTION_BACKWARD,
157 DIRECTION_FORWARD
160 /* helper functions */
161 static inline INT MONTHCAL_GetCalCount(const MONTHCAL_INFO *infoPtr)
163 return infoPtr->dim.cx * infoPtr->dim.cy;
166 /* send a single MCN_SELCHANGE notification */
167 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
169 NMSELCHANGE nmsc;
171 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
172 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
173 nmsc.nmhdr.code = MCN_SELCHANGE;
174 nmsc.stSelStart = infoPtr->minSel;
175 nmsc.stSelStart.wDayOfWeek = 0;
176 if(infoPtr->dwStyle & MCS_MULTISELECT){
177 nmsc.stSelEnd = infoPtr->maxSel;
178 nmsc.stSelEnd.wDayOfWeek = 0;
180 else
181 nmsc.stSelEnd = st_null;
183 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
186 /* send a single MCN_SELECT notification */
187 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
189 NMSELCHANGE nmsc;
191 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
192 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
193 nmsc.nmhdr.code = MCN_SELECT;
194 nmsc.stSelStart = infoPtr->minSel;
195 nmsc.stSelStart.wDayOfWeek = 0;
196 if(infoPtr->dwStyle & MCS_MULTISELECT){
197 nmsc.stSelEnd = infoPtr->maxSel;
198 nmsc.stSelEnd.wDayOfWeek = 0;
200 else
201 nmsc.stSelEnd = st_null;
203 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
206 static inline int MONTHCAL_MonthDiff(const SYSTEMTIME *left, const SYSTEMTIME *right)
208 return (right->wYear - left->wYear)*12 + right->wMonth - left->wMonth;
211 /* returns the number of days in any given month, checking for leap days */
212 /* January is 1, December is 12 */
213 int MONTHCAL_MonthLength(int month, int year)
215 static const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
216 /* Wrap around, this eases handling. Getting length only we shouldn't care
217 about year change here cause January and December have
218 the same day quantity */
219 if(month == 0)
220 month = 12;
221 else if(month == 13)
222 month = 1;
224 /* special case for calendar transition year */
225 if(month == min_allowed_date.wMonth && year == min_allowed_date.wYear) return 19;
227 /* if we have a leap year add 1 day to February */
228 /* a leap year is a year either divisible by 400 */
229 /* or divisible by 4 and not by 100 */
230 if(month == 2) { /* February */
231 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
232 (year%4 == 0)) ? 1 : 0);
234 else {
235 return mdays[month - 1];
239 /* compares timestamps using date part only */
240 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
242 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
243 (first->wDay == second->wDay);
246 /* make sure that date fields are valid */
247 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
249 if (time->wMonth < 1 || time->wMonth > 12 )
250 return FALSE;
251 if (time->wDay == 0 || time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear))
252 return FALSE;
254 return TRUE;
257 /* Copies timestamp part only.
259 * PARAMETERS
261 * [I] from : source date
262 * [O] to : dest date
264 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
266 to->wHour = from->wHour;
267 to->wMinute = from->wMinute;
268 to->wSecond = from->wSecond;
271 /* Copies date part only.
273 * PARAMETERS
275 * [I] from : source date
276 * [O] to : dest date
278 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
280 to->wYear = from->wYear;
281 to->wMonth = from->wMonth;
282 to->wDay = from->wDay;
283 to->wDayOfWeek = from->wDayOfWeek;
286 /* Compares two dates in SYSTEMTIME format
288 * PARAMETERS
290 * [I] first : pointer to valid first date data to compare
291 * [I] second : pointer to valid second date data to compare
293 * RETURN VALUE
295 * -1 : first < second
296 * 0 : first == second
297 * 1 : first > second
299 * Note that no date validation performed, already validated values expected.
301 LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
303 FILETIME ft_first, ft_second;
305 SystemTimeToFileTime(first, &ft_first);
306 SystemTimeToFileTime(second, &ft_second);
308 return CompareFileTime(&ft_first, &ft_second);
311 static LONG MONTHCAL_CompareMonths(const SYSTEMTIME *first, const SYSTEMTIME *second)
313 SYSTEMTIME st_first, st_second;
315 st_first = st_second = st_null;
316 MONTHCAL_CopyDate(first, &st_first);
317 MONTHCAL_CopyDate(second, &st_second);
318 st_first.wDay = st_second.wDay = 1;
320 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
323 static LONG MONTHCAL_CompareDate(const SYSTEMTIME *first, const SYSTEMTIME *second)
325 SYSTEMTIME st_first, st_second;
327 st_first = st_second = st_null;
328 MONTHCAL_CopyDate(first, &st_first);
329 MONTHCAL_CopyDate(second, &st_second);
331 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
334 /* Checks largest possible date range and configured one
336 * PARAMETERS
338 * [I] infoPtr : valid pointer to control data
339 * [I] date : pointer to valid date data to check
340 * [I] fix : make date fit valid range
342 * RETURN VALUE
344 * TRUE - date within largest and configured range
345 * FALSE - date is outside largest or configured range
347 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr,
348 SYSTEMTIME *date, BOOL fix)
350 const SYSTEMTIME *fix_st = NULL;
352 if(MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) {
353 fix_st = &max_allowed_date;
355 else if(MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1) {
356 fix_st = &min_allowed_date;
358 else {
359 if(infoPtr->rangeValid & GDTR_MAX) {
360 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
361 fix_st = &infoPtr->maxDate;
365 if(infoPtr->rangeValid & GDTR_MIN) {
366 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
367 fix_st = &infoPtr->minDate;
372 if (fix && fix_st) {
373 date->wYear = fix_st->wYear;
374 date->wMonth = fix_st->wMonth;
377 return !fix_st;
380 /* Checks passed range width with configured maximum selection count
382 * PARAMETERS
384 * [I] infoPtr : valid pointer to control data
385 * [I] range0 : pointer to valid date data (requested bound)
386 * [I] range1 : pointer to valid date data (primary bound)
387 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
389 * Adjust value computed basing on primary bound and current maximum selection
390 * count. For simple range check (without adjusted value required) (range0, range1)
391 * relation means nothing.
393 * RETURN VALUE
395 * TRUE - range is shorter or equal to maximum
396 * FALSE - range is larger than maximum
398 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
399 const SYSTEMTIME *range0,
400 const SYSTEMTIME *range1,
401 SYSTEMTIME *adjust)
403 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
404 FILETIME ft_range0, ft_range1;
405 LONG cmp;
407 SystemTimeToFileTime(range0, &ft_range0);
408 SystemTimeToFileTime(range1, &ft_range1);
410 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
411 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
412 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
413 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
415 cmp = CompareFileTime(&ft_range0, &ft_range1);
417 if(cmp == 1)
418 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
419 else
420 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
422 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
424 if(adjust) {
425 if(cmp == 1)
426 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
427 else
428 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
430 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
431 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
432 FileTimeToSystemTime(&ft_range0, adjust);
435 return FALSE;
437 else return TRUE;
440 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
441 Milliseconds are intentionally not validated. */
442 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
444 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
445 return FALSE;
446 else
447 return TRUE;
450 /* Note:Depending on DST, this may be offset by a day.
451 Need to find out if we're on a DST place & adjust the clock accordingly.
452 Above function assumes we have a valid data.
453 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
454 0 = Sunday.
457 /* Returns the day in the week
459 * PARAMETERS
460 * [i] date : input date
461 * [I] inplace : set calculated value back to date structure
463 * RETURN VALUE
464 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
466 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
468 SYSTEMTIME st = st_null;
469 FILETIME ft;
471 MONTHCAL_CopyDate(date, &st);
473 SystemTimeToFileTime(&st, &ft);
474 FileTimeToSystemTime(&ft, &st);
476 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
478 return st.wDayOfWeek;
481 /* add/subtract 'months' from date */
482 static inline void MONTHCAL_GetMonth(SYSTEMTIME *date, INT months)
484 INT length, m = date->wMonth + months;
486 date->wYear += m > 0 ? (m - 1) / 12 : m / 12 - 1;
487 date->wMonth = m > 0 ? (m - 1) % 12 + 1 : 12 + m % 12;
488 /* fix moving from last day in a month */
489 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
490 if(date->wDay > length) date->wDay = length;
491 MONTHCAL_CalculateDayOfWeek(date, TRUE);
494 /* properly updates date to point on next month */
495 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
497 MONTHCAL_GetMonth(date, 1);
500 /* properly updates date to point on prev month */
501 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
503 MONTHCAL_GetMonth(date, -1);
506 /* Returns full date for a first currently visible day */
507 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
509 /* zero indexed calendar has the earliest date */
510 SYSTEMTIME st_first = infoPtr->calendars[0].month;
511 INT firstDay;
513 st_first.wDay = 1;
514 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
516 *date = infoPtr->calendars[0].month;
517 MONTHCAL_GetPrevMonth(date);
519 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
520 (infoPtr->firstDay - firstDay) % 7 + 1;
522 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
523 date->wDay -= 7;
525 /* fix day of week */
526 MONTHCAL_CalculateDayOfWeek(date, TRUE);
529 /* Returns full date for a last currently visible day */
530 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
532 /* the latest date is in latest calendar */
533 SYSTEMTIME st, *lt_month = &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
534 INT first_day;
536 *date = *lt_month;
537 st = *lt_month;
539 /* day of week of first day of current month */
540 st.wDay = 1;
541 first_day = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
543 MONTHCAL_GetNextMonth(date);
544 MONTHCAL_GetPrevMonth(&st);
546 /* last calendar starts with some date from previous month that not displayed */
547 st.wDay = MONTHCAL_MonthLength(st.wMonth, st.wYear) +
548 (infoPtr->firstDay - first_day) % 7 + 1;
549 if (st.wDay > MONTHCAL_MonthLength(st.wMonth, st.wYear)) st.wDay -= 7;
551 /* Use month length to get max day. 42 means max day count in calendar area */
552 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
553 MONTHCAL_MonthLength(lt_month->wMonth, lt_month->wYear);
555 /* fix day of week */
556 MONTHCAL_CalculateDayOfWeek(date, TRUE);
559 /* From a given point calculate the row, column and day in the calendar,
560 'day == 0' means the last day of the last month. */
561 static int MONTHCAL_GetDayFromPos(const MONTHCAL_INFO *infoPtr, POINT pt, INT calIdx)
563 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
564 int firstDay, col, row;
565 RECT client;
567 GetClientRect(infoPtr->hwndSelf, &client);
569 /* if the point is outside the x bounds of the window put it at the boundary */
570 if (pt.x > client.right) pt.x = client.right;
572 col = (pt.x - infoPtr->calendars[calIdx].days.left ) / infoPtr->width_increment;
573 row = (pt.y - infoPtr->calendars[calIdx].days.top ) / infoPtr->height_increment;
575 st.wDay = 1;
576 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
577 return col + 7 * row - firstDay;
580 /* Get day position for given date and calendar
582 * PARAMETERS
584 * [I] infoPtr : pointer to control data
585 * [I] date : date value
586 * [O] col : day column (zero based)
587 * [O] row : week column (zero based)
588 * [I] calIdx : calendar index
590 static void MONTHCAL_GetDayPos(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
591 INT *col, INT *row, INT calIdx)
593 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
594 INT first;
596 st.wDay = 1;
597 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
599 if (calIdx == 0 || calIdx == MONTHCAL_GetCalCount(infoPtr)-1) {
600 const SYSTEMTIME *cal = &infoPtr->calendars[calIdx].month;
601 LONG cmp = MONTHCAL_CompareMonths(date, &st);
603 /* previous month */
604 if (cmp == -1) {
605 *col = (first - MONTHCAL_MonthLength(date->wMonth, cal->wYear) + date->wDay) % 7;
606 *row = 0;
607 return;
610 /* next month calculation is same as for current, just add current month length */
611 if (cmp == 1)
612 first += MONTHCAL_MonthLength(cal->wMonth, cal->wYear);
615 *col = (date->wDay + first) % 7;
616 *row = (date->wDay + first - *col) / 7;
619 /* returns bounding box for day in given position in given calendar */
620 static inline void MONTHCAL_GetDayRectI(const MONTHCAL_INFO *infoPtr, RECT *r,
621 INT col, INT row, INT calIdx)
623 r->left = infoPtr->calendars[calIdx].days.left + col * infoPtr->width_increment;
624 r->right = r->left + infoPtr->width_increment;
625 r->top = infoPtr->calendars[calIdx].days.top + row * infoPtr->height_increment;
626 r->bottom = r->top + infoPtr->textHeight;
629 /* Returns bounding box for given date
631 * NOTE: when calendar index is unknown pass -1
633 static BOOL MONTHCAL_GetDayRect(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date, RECT *r, INT calIdx)
635 INT col, row;
637 if (!MONTHCAL_ValidateDate(date))
639 SetRectEmpty(r);
640 return FALSE;
643 if (calIdx == -1)
645 INT cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[0].month);
647 if (cmp <= 0)
648 calIdx = 0;
649 else
651 cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month);
652 if (cmp >= 0)
653 calIdx = MONTHCAL_GetCalCount(infoPtr)-1;
654 else
656 for (calIdx = 1; calIdx < MONTHCAL_GetCalCount(infoPtr)-1; calIdx++)
657 if (MONTHCAL_CompareMonths(date, &infoPtr->calendars[calIdx].month) == 0)
658 break;
663 MONTHCAL_GetDayPos(infoPtr, date, &col, &row, calIdx);
664 MONTHCAL_GetDayRectI(infoPtr, r, col, row, calIdx);
666 return TRUE;
669 static LRESULT
670 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
672 INT range;
674 TRACE("flag=%d, st=%p\n", flag, st);
676 switch (flag) {
677 case GMR_VISIBLE:
679 if (st)
681 st[0] = infoPtr->calendars[0].month;
682 st[1] = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
684 if (st[0].wMonth == min_allowed_date.wMonth &&
685 st[0].wYear == min_allowed_date.wYear)
687 st[0].wDay = min_allowed_date.wDay;
689 else
690 st[0].wDay = 1;
691 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
693 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
694 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
697 range = MONTHCAL_GetCalCount(infoPtr);
698 break;
700 case GMR_DAYSTATE:
702 if (st)
704 MONTHCAL_GetMinDate(infoPtr, &st[0]);
705 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
707 /* include two partially visible months */
708 range = MONTHCAL_GetCalCount(infoPtr) + 2;
709 break;
711 default:
712 WARN("Unknown flag value, got %d\n", flag);
713 range = 0;
716 return range;
719 /* Focused day helper:
721 - set focused date to given value;
722 - reset to zero value if NULL passed;
723 - invalidate previous and new day rectangle only if needed.
725 Returns TRUE if focused day changed, FALSE otherwise.
727 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
729 RECT r;
731 if(st)
733 /* there's nothing to do if it's the same date,
734 mouse move within same date rectangle case */
735 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
737 /* invalidate old focused day */
738 if (MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1))
739 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
741 infoPtr->focusedSel = *st;
744 /* On set invalidates new day, on reset clears previous focused day. */
745 if (MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1))
746 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
748 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
749 infoPtr->focusedSel = st_null;
751 return TRUE;
754 /* draw today boundary box for specified rectangle */
755 static void MONTHCAL_Circle(const MONTHCAL_INFO *infoPtr, HDC hdc, const RECT *r)
757 HPEN old_pen = SelectObject(hdc, infoPtr->pens[PenRed]);
758 HBRUSH old_brush;
760 old_brush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
761 Rectangle(hdc, r->left, r->top, r->right, r->bottom);
763 SelectObject(hdc, old_brush);
764 SelectObject(hdc, old_pen);
767 /* Draw today day mark rectangle
769 * [I] hdc : context to draw in
770 * [I] date : day to mark with rectangle
773 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
774 const SYSTEMTIME *date)
776 RECT r;
778 MONTHCAL_GetDayRect(infoPtr, date, &r, -1);
779 MONTHCAL_Circle(infoPtr, hdc, &r);
782 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
783 int bold, const PAINTSTRUCT *ps)
785 static const WCHAR fmtW[] = { '%','d',0 };
786 WCHAR buf[10];
787 RECT r, r_temp;
788 COLORREF oldCol = 0;
789 COLORREF oldBk = 0;
790 INT old_bkmode, selection;
792 /* no need to check styles: when selection is not valid, it is set to zero.
793 1 < day < 31, so everything is OK */
794 MONTHCAL_GetDayRect(infoPtr, st, &r, -1);
795 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
797 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
798 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0))
800 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
801 TRACE("%s\n", wine_dbgstr_rect(&r));
802 oldCol = SetTextColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
803 oldBk = SetBkColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
804 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
806 selection = 1;
808 else
809 selection = 0;
811 SelectObject(hdc, bold ? infoPtr->hBoldFont : infoPtr->hFont);
813 old_bkmode = SetBkMode(hdc, TRANSPARENT);
814 wsprintfW(buf, fmtW, st->wDay);
815 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
816 SetBkMode(hdc, old_bkmode);
818 if (selection)
820 SetTextColor(hdc, oldCol);
821 SetBkColor(hdc, oldBk);
825 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, enum nav_direction button)
827 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
828 RECT *r = button == DIRECTION_FORWARD ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
829 BOOL pressed = button == DIRECTION_FORWARD ? infoPtr->status & MC_NEXTPRESSED :
830 infoPtr->status & MC_PREVPRESSED;
831 if (theme)
833 static const int states[] = {
834 /* Prev button */
835 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
836 /* Next button */
837 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
839 int stateNum = button == DIRECTION_FORWARD ? 3 : 0;
840 if (pressed)
841 stateNum += 1;
842 else
844 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
846 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
848 else
850 int style = button == DIRECTION_FORWARD ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
851 if (pressed)
852 style |= DFCS_PUSHED;
853 else
855 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
858 DrawFrameControl(hdc, r, DFC_SCROLL, style);
862 /* paint a title with buttons and month/year string */
863 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
865 static const WCHAR mmmmW[] = {'M','M','M','M',0};
866 static const WCHAR mmmW[] = {'M','M','M',0};
867 static const WCHAR mmW[] = {'M','M',0};
868 static const WCHAR fmtyearW[] = {'%','l','d',0};
869 static const WCHAR fmtmmW[] = {'%','0','2','d',0};
870 static const WCHAR fmtmW[] = {'%','d',0};
871 RECT *title = &infoPtr->calendars[calIdx].title;
872 const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
873 WCHAR monthW[80], strW[80], fmtW[80], yearW[6] /* valid year range is 1601-30827 */;
874 int yearoffset, monthoffset, shiftX;
875 SIZE sz;
877 /* fill header box */
878 FillRect(hdc, title, infoPtr->brushes[BrushTitle]);
880 /* month/year string */
881 SetBkColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
882 SetTextColor(hdc, infoPtr->colors[MCSC_TITLETEXT]);
883 SelectObject(hdc, infoPtr->hBoldFont);
885 /* draw formatted date string */
886 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_YEARMONTH, st, NULL, strW, ARRAY_SIZE(strW));
887 DrawTextW(hdc, strW, lstrlenW(strW), title, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
889 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SYEARMONTH, fmtW, ARRAY_SIZE(fmtW));
890 wsprintfW(yearW, fmtyearW, st->wYear);
892 /* month is trickier as it's possible to have different format pictures, we'll
893 test for M, MM, MMM, and MMMM */
894 if (wcsstr(fmtW, mmmmW))
895 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+st->wMonth-1, monthW, ARRAY_SIZE(monthW));
896 else if (wcsstr(fmtW, mmmW))
897 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME1+st->wMonth-1, monthW, ARRAY_SIZE(monthW));
898 else if (wcsstr(fmtW, mmW))
899 wsprintfW(monthW, fmtmmW, st->wMonth);
900 else
901 wsprintfW(monthW, fmtmW, st->wMonth);
903 /* update hit boxes */
904 yearoffset = 0;
905 while (strW[yearoffset])
907 if (!wcsncmp(&strW[yearoffset], yearW, lstrlenW(yearW)))
908 break;
909 yearoffset++;
912 monthoffset = 0;
913 while (strW[monthoffset])
915 if (!wcsncmp(&strW[monthoffset], monthW, lstrlenW(monthW)))
916 break;
917 monthoffset++;
920 /* for left limits use offsets */
921 sz.cx = 0;
922 if (yearoffset)
923 GetTextExtentPoint32W(hdc, strW, yearoffset, &sz);
924 infoPtr->calendars[calIdx].titleyear.left = sz.cx;
926 sz.cx = 0;
927 if (monthoffset)
928 GetTextExtentPoint32W(hdc, strW, monthoffset, &sz);
929 infoPtr->calendars[calIdx].titlemonth.left = sz.cx;
931 /* for right limits use actual string parts lengths */
932 GetTextExtentPoint32W(hdc, &strW[yearoffset], lstrlenW(yearW), &sz);
933 infoPtr->calendars[calIdx].titleyear.right = infoPtr->calendars[calIdx].titleyear.left + sz.cx;
935 GetTextExtentPoint32W(hdc, monthW, lstrlenW(monthW), &sz);
936 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
938 /* Finally translate rectangles to match center aligned string,
939 hit rectangles are relative to title rectangle before translation. */
940 GetTextExtentPoint32W(hdc, strW, lstrlenW(strW), &sz);
941 shiftX = (title->right - title->left - sz.cx) / 2 + title->left;
942 OffsetRect(&infoPtr->calendars[calIdx].titleyear, shiftX, 0);
943 OffsetRect(&infoPtr->calendars[calIdx].titlemonth, shiftX, 0);
946 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
948 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
949 static const WCHAR fmt_weekW[] = { '%','d',0 };
950 INT mindays, weeknum, weeknum1, startofprescal;
951 INT i, prev_month;
952 SYSTEMTIME st;
953 WCHAR buf[80];
954 HPEN old_pen;
955 RECT r;
957 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
959 MONTHCAL_GetMinDate(infoPtr, &st);
960 startofprescal = st.wDay;
961 st = *date;
963 prev_month = date->wMonth - 1;
964 if(prev_month == 0) prev_month = 12;
967 Rules what week to call the first week of a new year:
968 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
969 The week containing Jan 1 is the first week of year
970 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
971 First week of year must contain 4 days of the new year
972 LOCALE_IFIRSTWEEKOFYEAR == 1 (what countries?)
973 The first week of the year must contain only days of the new year
975 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, ARRAY_SIZE(buf));
976 weeknum = wcstol(buf, NULL, 10);
977 switch (weeknum)
979 case 1: mindays = 6;
980 break;
981 case 2: mindays = 3;
982 break;
983 case 0: mindays = 0;
984 break;
985 default:
986 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
987 mindays = 0;
990 if (date->wMonth == 1)
992 /* calculate all those exceptions for January */
993 st.wDay = st.wMonth = 1;
994 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
995 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
996 weeknum = 1;
997 else
999 weeknum = 0;
1000 for(i = 0; i < 11; i++)
1001 weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
1003 weeknum += startofprescal + 7;
1004 weeknum /= 7;
1005 st.wYear -= 1;
1006 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
1007 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
1010 else
1012 weeknum = 0;
1013 for(i = 0; i < prev_month - 1; i++)
1014 weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
1016 weeknum += startofprescal + 7;
1017 weeknum /= 7;
1018 st.wDay = st.wMonth = 1;
1019 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
1020 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
1023 r = infoPtr->calendars[calIdx].weeknums;
1025 /* erase whole week numbers area */
1026 FillRect(hdc, &r, infoPtr->brushes[BrushMonth]);
1027 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1029 /* reduce rectangle to one week number */
1030 r.bottom = r.top + infoPtr->height_increment;
1032 for(i = 0; i < 6; i++) {
1033 if((i == 0) && (weeknum > 50))
1035 wsprintfW(buf, fmt_weekW, weeknum);
1036 weeknum = 0;
1038 else if((i == 5) && (weeknum > 47))
1040 wsprintfW(buf, fmt_weekW, 1);
1042 else
1043 wsprintfW(buf, fmt_weekW, weeknum + i);
1045 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1046 OffsetRect(&r, 0, infoPtr->height_increment);
1049 /* line separator for week numbers column */
1050 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1051 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
1052 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
1053 SelectObject(hdc, old_pen);
1056 /* bottom today date */
1057 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1059 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
1060 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
1061 RECT text_rect, box_rect;
1062 HFONT old_font;
1063 INT col;
1065 if(infoPtr->dwStyle & MCS_NOTODAY) return;
1067 LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, ARRAY_SIZE(buf_todayW));
1068 col = infoPtr->dwStyle & MCS_NOTODAYCIRCLE ? 0 : 1;
1069 if (infoPtr->dwStyle & MCS_WEEKNUMBERS) col--;
1070 /* label is located below first calendar last row */
1071 MONTHCAL_GetDayRectI(infoPtr, &text_rect, col, 6, infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx);
1072 box_rect = text_rect;
1074 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL, buf_dateW, ARRAY_SIZE(buf_dateW));
1075 old_font = SelectObject(hdc, infoPtr->hBoldFont);
1076 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1078 wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
1079 DrawTextW(hdc, buf, -1, &text_rect, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1080 DrawTextW(hdc, buf, -1, &text_rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1082 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
1083 OffsetRect(&box_rect, -infoPtr->width_increment, 0);
1084 MONTHCAL_Circle(infoPtr, hdc, &box_rect);
1087 SelectObject(hdc, old_font);
1090 /* today mark + focus */
1091 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1093 /* circle today date if only it's in fully visible month */
1094 if (!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
1096 INT i;
1098 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1099 if (!MONTHCAL_CompareMonths(&infoPtr->todaysDate, &infoPtr->calendars[i].month))
1101 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
1102 break;
1106 if (!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
1108 RECT r;
1109 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
1110 DrawFocusRect(hdc, &r);
1114 /* months before first calendar month and after last calendar month */
1115 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1117 INT mask, index;
1118 UINT length;
1119 SYSTEMTIME st_max, st;
1121 if (infoPtr->dwStyle & MCS_NOTRAILINGDATES) return;
1123 SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
1125 /* draw prev month */
1126 MONTHCAL_GetMinDate(infoPtr, &st);
1127 mask = 1 << (st.wDay-1);
1128 /* December and January both 31 days long, so no worries if wrapped */
1129 length = MONTHCAL_MonthLength(infoPtr->calendars[0].month.wMonth - 1,
1130 infoPtr->calendars[0].month.wYear);
1131 index = 0;
1132 while(st.wDay <= length)
1134 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1135 mask <<= 1;
1136 st.wDay++;
1139 /* draw next month */
1140 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1141 st.wDay = 1;
1142 MONTHCAL_GetNextMonth(&st);
1143 MONTHCAL_GetMaxDate(infoPtr, &st_max);
1144 mask = 1;
1145 index = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)-1;
1146 while(st.wDay <= st_max.wDay)
1148 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1149 mask <<= 1;
1150 st.wDay++;
1154 static int get_localized_dayname(const MONTHCAL_INFO *infoPtr, unsigned int day, WCHAR *buff, unsigned int count)
1156 LCTYPE lctype;
1158 if (infoPtr->dwStyle & MCS_SHORTDAYSOFWEEK)
1159 lctype = LOCALE_SSHORTESTDAYNAME1 + day;
1160 else
1161 lctype = LOCALE_SABBREVDAYNAME1 + day;
1163 return GetLocaleInfoW(LOCALE_USER_DEFAULT, lctype, buff, count);
1166 /* paint a calendar area */
1167 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
1169 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
1170 INT i, j;
1171 UINT length;
1172 RECT r, fill_bk_rect;
1173 SYSTEMTIME st;
1174 WCHAR buf[80];
1175 HPEN old_pen;
1176 int mask;
1178 /* fill whole days area - from week days area to today note rectangle */
1179 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
1180 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
1181 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
1183 FillRect(hdc, &fill_bk_rect, infoPtr->brushes[BrushMonth]);
1185 /* draw line under day abbreviations */
1186 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1187 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
1188 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
1189 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
1190 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
1191 SelectObject(hdc, old_pen);
1193 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
1194 infoPtr->calendars[calIdx].weeknums.right;
1196 /* draw day abbreviations */
1197 SelectObject(hdc, infoPtr->hFont);
1198 SetBkColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
1199 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1200 /* rectangle to draw a single day abbreviation within */
1201 r = infoPtr->calendars[calIdx].wdays;
1202 r.right = r.left + infoPtr->width_increment;
1204 i = infoPtr->firstDay;
1205 for(j = 0; j < 7; j++) {
1206 get_localized_dayname(infoPtr, (i + j + 6) % 7, buf, ARRAY_SIZE(buf));
1207 DrawTextW(hdc, buf, lstrlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1208 OffsetRect(&r, infoPtr->width_increment, 0);
1211 /* draw current month */
1212 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1213 st = *date;
1214 st.wDay = 1;
1215 mask = 1;
1216 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1217 while(st.wDay <= length)
1219 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[calIdx+1] & mask, ps);
1220 mask <<= 1;
1221 st.wDay++;
1225 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1227 COLORREF old_text_clr, old_bk_clr;
1228 HFONT old_font;
1229 INT i;
1231 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1232 old_bk_clr = GetBkColor(hdc);
1233 old_font = GetCurrentObject(hdc, OBJ_FONT);
1235 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1237 RECT *title = &infoPtr->calendars[i].title;
1238 RECT r;
1240 /* draw title, redraw all its elements */
1241 if (IntersectRect(&r, &(ps->rcPaint), title))
1242 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1244 /* draw calendar area */
1245 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1246 if (IntersectRect(&r, &(ps->rcPaint), &r))
1247 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1249 /* week numbers */
1250 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1253 /* partially visible months */
1254 MONTHCAL_PaintLeadTrailMonths(infoPtr, hdc, ps);
1256 /* focus and today rectangle */
1257 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1259 /* today at the bottom left */
1260 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1262 /* navigation buttons */
1263 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_BACKWARD);
1264 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_FORWARD);
1266 /* restore context */
1267 SetBkColor(hdc, old_bk_clr);
1268 SelectObject(hdc, old_font);
1269 SetTextColor(hdc, old_text_clr);
1272 static LRESULT
1273 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, RECT *rect)
1275 TRACE("rect %p\n", rect);
1277 if(!rect) return FALSE;
1279 *rect = infoPtr->calendars[0].title;
1280 rect->bottom = infoPtr->calendars[0].days.bottom + infoPtr->todayrect.bottom -
1281 infoPtr->todayrect.top;
1283 AdjustWindowRect(rect, infoPtr->dwStyle, FALSE);
1285 /* minimal rectangle is zero based */
1286 OffsetRect(rect, -rect->left, -rect->top);
1288 TRACE("%s\n", wine_dbgstr_rect(rect));
1290 return TRUE;
1293 static COLORREF
1294 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, UINT index)
1296 TRACE("%p, %d\n", infoPtr, index);
1298 if (index > MCSC_TRAILINGTEXT) return -1;
1299 return infoPtr->colors[index];
1302 static LRESULT
1303 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, UINT index, COLORREF color)
1305 enum CachedBrush type;
1306 COLORREF prev;
1308 TRACE("%p, %d: color %08x\n", infoPtr, index, color);
1310 if (index > MCSC_TRAILINGTEXT) return -1;
1312 prev = infoPtr->colors[index];
1313 infoPtr->colors[index] = color;
1315 /* update cached brush */
1316 switch (index)
1318 case MCSC_BACKGROUND:
1319 type = BrushBackground;
1320 break;
1321 case MCSC_TITLEBK:
1322 type = BrushTitle;
1323 break;
1324 case MCSC_MONTHBK:
1325 type = BrushMonth;
1326 break;
1327 default:
1328 type = BrushLast;
1331 if (type != BrushLast)
1333 DeleteObject(infoPtr->brushes[type]);
1334 infoPtr->brushes[type] = CreateSolidBrush(color);
1337 /* update cached pen */
1338 if (index == MCSC_TEXT)
1340 DeleteObject(infoPtr->pens[PenText]);
1341 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[index]);
1344 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND);
1345 return prev;
1348 static LRESULT
1349 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1351 TRACE("\n");
1353 if(infoPtr->delta)
1354 return infoPtr->delta;
1356 return MONTHCAL_GetMonthRange(infoPtr, GMR_VISIBLE, NULL);
1360 static LRESULT
1361 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1363 INT prev = infoPtr->delta;
1365 TRACE("delta %d\n", delta);
1367 infoPtr->delta = delta;
1368 return prev;
1372 static inline LRESULT
1373 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1375 int day;
1377 /* convert from SYSTEMTIME to locale format */
1378 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1380 return MAKELONG(day, infoPtr->firstDaySet);
1384 /* Sets the first day of the week that will appear in the control
1387 * PARAMETERS:
1388 * [I] infoPtr : valid pointer to control data
1389 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1392 * RETURN VALUE:
1393 * Low word contains previous first day,
1394 * high word indicates was first day forced with this message before or is
1395 * locale defined (TRUE - was forced, FALSE - wasn't).
1397 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1398 * FIXME: we need more error checking here
1400 static LRESULT
1401 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1403 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1404 int new_day;
1406 TRACE("%d\n", day);
1408 if(day == -1)
1410 WCHAR buf[80];
1412 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, ARRAY_SIZE(buf));
1413 TRACE("%s %d\n", debugstr_w(buf), lstrlenW(buf));
1415 new_day = wcstol(buf, NULL, 10);
1417 infoPtr->firstDaySet = FALSE;
1419 else if(day >= 7)
1421 new_day = 6; /* max first day allowed */
1422 infoPtr->firstDaySet = TRUE;
1424 else
1426 /* Native behaviour for that case is broken: invalid date number >31
1427 got displayed at (0,0) position, current month starts always from
1428 (1,0) position. Should be implemented here as well only if there's
1429 nothing else to do. */
1430 if (day < -1)
1431 FIXME("No bug compatibility for day=%d\n", day);
1433 new_day = day;
1434 infoPtr->firstDaySet = TRUE;
1437 /* convert from locale to SYSTEMTIME format */
1438 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1440 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1442 return prev;
1445 static LRESULT
1446 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1448 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1451 static LRESULT
1452 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1454 FILETIME ft_min, ft_max;
1456 TRACE("%x %p\n", limits, range);
1458 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1459 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1460 return FALSE;
1462 infoPtr->rangeValid = 0;
1463 infoPtr->minDate = infoPtr->maxDate = st_null;
1465 if (limits & GDTR_MIN)
1467 if (!MONTHCAL_ValidateTime(&range[0]))
1468 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1470 infoPtr->minDate = range[0];
1471 infoPtr->rangeValid |= GDTR_MIN;
1473 if (limits & GDTR_MAX)
1475 if (!MONTHCAL_ValidateTime(&range[1]))
1476 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1478 infoPtr->maxDate = range[1];
1479 infoPtr->rangeValid |= GDTR_MAX;
1482 /* Only one limit set - we are done */
1483 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1484 return TRUE;
1486 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1487 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1489 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1491 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1493 /* Native swaps limits only when both limits are being set. */
1494 SYSTEMTIME st_tmp = infoPtr->minDate;
1495 infoPtr->minDate = infoPtr->maxDate;
1496 infoPtr->maxDate = st_tmp;
1498 else
1500 /* reset the other limit */
1501 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1502 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1503 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1507 return TRUE;
1511 static LRESULT
1512 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1514 TRACE("%p\n", range);
1516 if (!range) return 0;
1518 range[1] = infoPtr->maxDate;
1519 range[0] = infoPtr->minDate;
1521 return infoPtr->rangeValid;
1525 static LRESULT
1526 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1528 TRACE("%p %d %p\n", infoPtr, months, states);
1530 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return 0;
1531 if (months != MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)) return 0;
1533 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1535 return 1;
1538 static LRESULT
1539 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1541 TRACE("%p\n", curSel);
1542 if(!curSel) return FALSE;
1543 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1545 *curSel = infoPtr->minSel;
1546 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1547 return TRUE;
1550 static LRESULT
1551 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1553 SYSTEMTIME prev = infoPtr->minSel, selection;
1554 INT diff;
1555 WORD day;
1557 TRACE("%p\n", curSel);
1558 if(!curSel) return FALSE;
1559 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1561 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1562 /* exit earlier if selection equals current */
1563 if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1565 selection = *curSel;
1566 selection.wHour = selection.wMinute = selection.wSecond = selection.wMilliseconds = 0;
1567 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1569 if(!MONTHCAL_IsDateInValidRange(infoPtr, &selection, FALSE)) return FALSE;
1571 /* scroll calendars only if we have to */
1572 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, curSel);
1573 if (diff <= 0)
1575 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, curSel);
1576 if (diff > 0) diff = 0;
1579 if (diff != 0)
1581 INT i;
1583 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1584 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1587 /* we need to store time part as it is */
1588 selection = *curSel;
1589 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1590 infoPtr->minSel = infoPtr->maxSel = selection;
1592 /* if selection is still in current month, reduce rectangle */
1593 day = prev.wDay;
1594 prev.wDay = curSel->wDay;
1595 if (MONTHCAL_IsDateEqual(&prev, curSel))
1597 RECT r_prev, r_new;
1599 prev.wDay = day;
1600 MONTHCAL_GetDayRect(infoPtr, &prev, &r_prev, -1);
1601 MONTHCAL_GetDayRect(infoPtr, curSel, &r_new, -1);
1603 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1604 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1606 else
1607 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1609 return TRUE;
1613 static LRESULT
1614 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1616 return infoPtr->maxSelCount;
1620 static LRESULT
1621 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1623 TRACE("%d\n", max);
1625 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1626 if(max <= 0) return FALSE;
1628 infoPtr->maxSelCount = max;
1630 return TRUE;
1634 static LRESULT
1635 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1637 TRACE("%p\n", range);
1639 if(!range) return FALSE;
1641 if(infoPtr->dwStyle & MCS_MULTISELECT)
1643 range[1] = infoPtr->maxSel;
1644 range[0] = infoPtr->minSel;
1645 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1646 return TRUE;
1649 return FALSE;
1653 static LRESULT
1654 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1656 SYSTEMTIME old_range[2];
1657 INT diff;
1659 TRACE("%p\n", range);
1661 if(!range || !(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1663 /* adjust timestamps */
1664 if(!MONTHCAL_ValidateTime(&range[0])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1665 if(!MONTHCAL_ValidateTime(&range[1])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1667 /* maximum range exceeded */
1668 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1670 old_range[0] = infoPtr->minSel;
1671 old_range[1] = infoPtr->maxSel;
1673 /* swap if min > max */
1674 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1676 infoPtr->minSel = range[0];
1677 infoPtr->maxSel = range[1];
1679 else
1681 infoPtr->minSel = range[1];
1682 infoPtr->maxSel = range[0];
1685 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, &infoPtr->maxSel);
1686 if (diff < 0)
1688 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, &infoPtr->maxSel);
1689 if (diff > 0) diff = 0;
1692 if (diff != 0)
1694 INT i;
1696 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1697 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1700 /* update day of week */
1701 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1702 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1704 /* redraw if bounds changed */
1705 /* FIXME: no actual need to redraw everything */
1706 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1707 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1709 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1712 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1713 return TRUE;
1717 static LRESULT
1718 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1720 TRACE("%p\n", today);
1722 if(!today) return FALSE;
1723 *today = infoPtr->todaysDate;
1724 return TRUE;
1727 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1729 * RETURN VALUE
1731 * TRUE - today date changed
1732 * FALSE - today date isn't changed
1734 static BOOL
1735 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1737 RECT rect;
1739 if (MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate))
1740 return FALSE;
1742 /* Invalidate old and new today day rectangle, and today label. */
1743 if (MONTHCAL_GetDayRect(infoPtr, &infoPtr->todaysDate, &rect, -1))
1744 InvalidateRect(infoPtr->hwndSelf, &rect, FALSE);
1746 if (MONTHCAL_GetDayRect(infoPtr, today, &rect, -1))
1747 InvalidateRect(infoPtr->hwndSelf, &rect, FALSE);
1749 infoPtr->todaysDate = *today;
1751 InvalidateRect(infoPtr->hwndSelf, &infoPtr->todayrect, FALSE);
1752 return TRUE;
1755 /* MCM_SETTODAT handler */
1756 static LRESULT
1757 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1759 TRACE("%p\n", today);
1761 if (today)
1763 /* remember if date was set successfully */
1764 if (MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1767 return 0;
1770 /* returns calendar index containing specified point, or -1 if it's background */
1771 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1773 RECT r;
1774 INT i;
1776 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1778 /* whole bounding rectangle allows some optimization to compute */
1779 r.left = infoPtr->calendars[i].title.left;
1780 r.top = infoPtr->calendars[i].title.top;
1781 r.bottom = infoPtr->calendars[i].days.bottom;
1782 r.right = infoPtr->calendars[i].days.right;
1784 if (PtInRect(&r, *pt)) return i;
1787 return -1;
1790 static inline UINT fill_hittest_info(const MCHITTESTINFO *src, MCHITTESTINFO *dest)
1792 dest->uHit = src->uHit;
1793 dest->st = src->st;
1795 if (dest->cbSize == sizeof(MCHITTESTINFO))
1796 memcpy(&dest->rc, &src->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1798 return src->uHit;
1801 static LRESULT
1802 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1804 MCHITTESTINFO htinfo;
1805 SYSTEMTIME *ht_month;
1806 INT day, calIdx;
1808 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1810 htinfo.st = st_null;
1812 /* we should preserve passed fields if hit area doesn't need them */
1813 if (lpht->cbSize == sizeof(MCHITTESTINFO))
1814 memcpy(&htinfo.rc, &lpht->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1816 /* guess in what calendar we are */
1817 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1818 if (calIdx == -1)
1820 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1822 htinfo.uHit = MCHT_TODAYLINK;
1823 htinfo.rc = infoPtr->todayrect;
1825 else
1826 /* outside of calendar area? What's left must be background :-) */
1827 htinfo.uHit = MCHT_CALENDARBK;
1829 return fill_hittest_info(&htinfo, lpht);
1832 /* are we in the header? */
1833 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1834 /* FIXME: buttons hittesting could be optimized cause maximum
1835 two calendars have buttons */
1836 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1838 htinfo.uHit = MCHT_TITLEBTNPREV;
1839 htinfo.rc = infoPtr->titlebtnprev;
1841 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1843 htinfo.uHit = MCHT_TITLEBTNNEXT;
1844 htinfo.rc = infoPtr->titlebtnnext;
1846 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1848 htinfo.uHit = MCHT_TITLEMONTH;
1849 htinfo.rc = infoPtr->calendars[calIdx].titlemonth;
1850 htinfo.iOffset = calIdx;
1852 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1854 htinfo.uHit = MCHT_TITLEYEAR;
1855 htinfo.rc = infoPtr->calendars[calIdx].titleyear;
1856 htinfo.iOffset = calIdx;
1858 else
1860 htinfo.uHit = MCHT_TITLE;
1861 htinfo.rc = infoPtr->calendars[calIdx].title;
1862 htinfo.iOffset = calIdx;
1865 return fill_hittest_info(&htinfo, lpht);
1868 ht_month = &infoPtr->calendars[calIdx].month;
1869 /* days area (including week days and week numbers) */
1870 day = MONTHCAL_GetDayFromPos(infoPtr, lpht->pt, calIdx);
1871 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1873 htinfo.uHit = MCHT_CALENDARDAY;
1874 htinfo.iOffset = calIdx;
1875 htinfo.st.wYear = ht_month->wYear;
1876 htinfo.st.wMonth = (day < 1) ? ht_month->wMonth -1 : ht_month->wMonth;
1877 htinfo.st.wDay = (day < 1) ?
1878 MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day : day;
1880 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1882 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1884 htinfo.uHit = MCHT_CALENDARWEEKNUM;
1885 htinfo.st.wYear = ht_month->wYear;
1886 htinfo.iOffset = calIdx;
1888 if (day < 1)
1890 htinfo.st.wMonth = ht_month->wMonth - 1;
1891 htinfo.st.wDay = MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day;
1893 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1895 htinfo.st.wMonth = ht_month->wMonth + 1;
1896 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1898 else
1900 htinfo.st.wMonth = ht_month->wMonth;
1901 htinfo.st.wDay = day;
1904 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1906 htinfo.iOffset = calIdx;
1907 htinfo.st.wDay = ht_month->wDay;
1908 htinfo.st.wYear = ht_month->wYear;
1909 htinfo.st.wMonth = ht_month->wMonth;
1910 /* previous month only valid for first calendar */
1911 if (day < 1 && calIdx == 0)
1913 htinfo.uHit = MCHT_CALENDARDATEPREV;
1914 MONTHCAL_GetPrevMonth(&htinfo.st);
1915 htinfo.st.wDay = MONTHCAL_MonthLength(htinfo.st.wMonth, htinfo.st.wYear) + day;
1917 /* next month only valid for last calendar */
1918 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear) &&
1919 calIdx == MONTHCAL_GetCalCount(infoPtr)-1)
1921 htinfo.uHit = MCHT_CALENDARDATENEXT;
1922 MONTHCAL_GetNextMonth(&htinfo.st);
1923 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1925 /* multiple calendars case - blank areas for previous/next month */
1926 else if (day < 1 || day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1928 htinfo.uHit = MCHT_CALENDARBK;
1930 else
1932 htinfo.uHit = MCHT_CALENDARDATE;
1933 htinfo.st.wDay = day;
1936 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1937 MONTHCAL_GetDayRectI(infoPtr, &htinfo.rc, htinfo.iCol, htinfo.iRow, calIdx);
1938 /* always update day of week */
1939 MONTHCAL_CalculateDayOfWeek(&htinfo.st, TRUE);
1942 return fill_hittest_info(&htinfo, lpht);
1945 /* MCN_GETDAYSTATE notification helper */
1946 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1948 MONTHDAYSTATE *state;
1949 NMDAYSTATE nmds;
1951 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return;
1953 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1954 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1955 nmds.nmhdr.code = MCN_GETDAYSTATE;
1956 nmds.cDayState = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0);
1957 nmds.prgDayState = state = heap_alloc_zero(nmds.cDayState * sizeof(MONTHDAYSTATE));
1959 MONTHCAL_GetMinDate(infoPtr, &nmds.stStart);
1960 nmds.stStart.wDay = 1;
1962 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1963 memcpy(infoPtr->monthdayState, nmds.prgDayState,
1964 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
1966 heap_free(state);
1969 /* no valid range check performed */
1970 static void MONTHCAL_Scroll(MONTHCAL_INFO *infoPtr, INT delta, BOOL keep_selection)
1972 INT i, selIdx = -1;
1974 for(i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1976 /* save selection position to shift it later */
1977 if (selIdx == -1 && MONTHCAL_CompareMonths(&infoPtr->minSel, &infoPtr->calendars[i].month) == 0)
1978 selIdx = i;
1980 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, delta);
1983 if (keep_selection)
1984 return;
1986 /* selection is always shifted to first calendar */
1987 if (infoPtr->dwStyle & MCS_MULTISELECT)
1989 SYSTEMTIME range[2];
1991 MONTHCAL_GetSelRange(infoPtr, range);
1992 MONTHCAL_GetMonth(&range[0], delta - selIdx);
1993 MONTHCAL_GetMonth(&range[1], delta - selIdx);
1994 MONTHCAL_SetSelRange(infoPtr, range);
1996 else
1998 SYSTEMTIME st = infoPtr->minSel;
2000 MONTHCAL_GetMonth(&st, delta - selIdx);
2001 MONTHCAL_SetCurSel(infoPtr, &st);
2005 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
2007 INT delta = infoPtr->delta ? infoPtr->delta : MONTHCAL_GetCalCount(infoPtr);
2008 BOOL keep_selection;
2009 SYSTEMTIME st;
2011 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
2013 /* check if change allowed by range set */
2014 if(direction == DIRECTION_BACKWARD)
2016 st = infoPtr->calendars[0].month;
2017 MONTHCAL_GetMonth(&st, -delta);
2019 else
2021 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2022 MONTHCAL_GetMonth(&st, delta);
2025 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
2027 keep_selection = infoPtr->dwStyle & MCS_NOSELCHANGEONNAV;
2028 MONTHCAL_Scroll(infoPtr, direction == DIRECTION_BACKWARD ? -delta : delta, keep_selection);
2029 MONTHCAL_NotifyDayState(infoPtr);
2030 if (!keep_selection)
2031 MONTHCAL_NotifySelectionChange(infoPtr);
2034 static LRESULT
2035 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2037 HMENU hMenu;
2038 POINT menupoint;
2039 WCHAR buf[32];
2041 hMenu = CreatePopupMenu();
2042 LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, ARRAY_SIZE(buf));
2043 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
2044 menupoint.x = (short)LOWORD(lParam);
2045 menupoint.y = (short)HIWORD(lParam);
2046 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2047 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
2048 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
2050 if (infoPtr->dwStyle & MCS_MULTISELECT)
2052 SYSTEMTIME range[2];
2054 range[0] = range[1] = infoPtr->todaysDate;
2055 MONTHCAL_SetSelRange(infoPtr, range);
2057 else
2058 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2060 MONTHCAL_NotifySelectionChange(infoPtr);
2061 MONTHCAL_NotifySelect(infoPtr);
2064 return 0;
2067 /***
2068 * DESCRIPTION:
2069 * Subclassed edit control windproc function
2071 * PARAMETER(S):
2072 * [I] hwnd : the edit window handle
2073 * [I] uMsg : the message that is to be processed
2074 * [I] wParam : first message parameter
2075 * [I] lParam : second message parameter
2078 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2080 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
2082 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
2083 hwnd, uMsg, wParam, lParam);
2085 switch (uMsg)
2087 case WM_GETDLGCODE:
2088 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
2090 case WM_DESTROY:
2092 WNDPROC editProc = infoPtr->EditWndProc;
2093 infoPtr->EditWndProc = NULL;
2094 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
2095 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
2098 case WM_KILLFOCUS:
2099 break;
2101 case WM_KEYDOWN:
2102 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
2103 break;
2105 default:
2106 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
2109 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
2110 SendMessageW(hwnd, WM_CLOSE, 0, 0);
2111 return 0;
2114 /* creates updown control and edit box */
2115 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr, INT calIdx)
2117 RECT *rc = &infoPtr->calendars[calIdx].titleyear;
2118 RECT *title = &infoPtr->calendars[calIdx].title;
2120 infoPtr->hWndYearEdit =
2121 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
2122 rc->left + 3, (title->bottom + title->top - infoPtr->textHeight) / 2,
2123 rc->right - rc->left + 4,
2124 infoPtr->textHeight, infoPtr->hwndSelf,
2125 NULL, NULL, NULL);
2127 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
2129 infoPtr->hWndYearUpDown =
2130 CreateWindowExW(0, UPDOWN_CLASSW, 0,
2131 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
2132 rc->right + 7, (title->bottom + title->top - infoPtr->textHeight) / 2,
2133 18, infoPtr->textHeight, infoPtr->hwndSelf,
2134 NULL, NULL, NULL);
2136 /* attach edit box */
2137 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
2138 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
2139 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
2140 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->calendars[calIdx].month.wYear);
2142 /* subclass edit box */
2143 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
2144 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
2146 SetFocus(infoPtr->hWndYearEdit);
2149 static LRESULT
2150 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2152 MCHITTESTINFO ht;
2153 DWORD hit;
2155 /* Actually we don't need input focus for calendar, this is used to kill
2156 year updown and its buddy edit box */
2157 if (IsWindow(infoPtr->hWndYearUpDown))
2159 SetFocus(infoPtr->hwndSelf);
2160 return 0;
2163 SetCapture(infoPtr->hwndSelf);
2165 ht.cbSize = sizeof(MCHITTESTINFO);
2166 ht.pt.x = (short)LOWORD(lParam);
2167 ht.pt.y = (short)HIWORD(lParam);
2169 hit = MONTHCAL_HitTest(infoPtr, &ht);
2171 TRACE("%x at %s\n", hit, wine_dbgstr_point(&ht.pt));
2173 switch(hit)
2175 case MCHT_TITLEBTNNEXT:
2176 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2177 infoPtr->status = MC_NEXTPRESSED;
2178 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2179 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2180 return 0;
2182 case MCHT_TITLEBTNPREV:
2183 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2184 infoPtr->status = MC_PREVPRESSED;
2185 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2186 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2187 return 0;
2189 case MCHT_TITLEMONTH:
2191 HMENU hMenu = CreatePopupMenu();
2192 WCHAR buf[32];
2193 POINT menupoint;
2194 INT i;
2196 for (i = 0; i < 12; i++)
2198 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, ARRAY_SIZE(buf));
2199 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
2201 menupoint.x = ht.pt.x;
2202 menupoint.y = ht.pt.y;
2203 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2204 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
2205 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
2207 if ((i > 0) && (i < 13) && infoPtr->calendars[ht.iOffset].month.wMonth != i)
2209 INT delta = i - infoPtr->calendars[ht.iOffset].month.wMonth;
2210 SYSTEMTIME st;
2212 /* check if change allowed by range set */
2213 st = delta < 0 ? infoPtr->calendars[0].month :
2214 infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2215 MONTHCAL_GetMonth(&st, delta);
2217 if (MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE))
2219 MONTHCAL_Scroll(infoPtr, delta, FALSE);
2220 MONTHCAL_NotifyDayState(infoPtr);
2221 MONTHCAL_NotifySelectionChange(infoPtr);
2222 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2225 return 0;
2227 case MCHT_TITLEYEAR:
2229 MONTHCAL_EditYear(infoPtr, ht.iOffset);
2230 return 0;
2232 case MCHT_TODAYLINK:
2234 if (infoPtr->dwStyle & MCS_MULTISELECT)
2236 SYSTEMTIME range[2];
2238 range[0] = range[1] = infoPtr->todaysDate;
2239 MONTHCAL_SetSelRange(infoPtr, range);
2241 else
2242 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2244 MONTHCAL_NotifySelectionChange(infoPtr);
2245 MONTHCAL_NotifySelect(infoPtr);
2246 return 0;
2248 case MCHT_CALENDARDATENEXT:
2249 case MCHT_CALENDARDATEPREV:
2250 case MCHT_CALENDARDATE:
2252 SYSTEMTIME st[2];
2254 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2256 st[0] = st[1] = ht.st;
2257 /* clear selection range */
2258 MONTHCAL_SetSelRange(infoPtr, st);
2260 infoPtr->status = MC_SEL_LBUTDOWN;
2261 if (MONTHCAL_SetDayFocus(infoPtr, &ht.st) && (infoPtr->dwStyle & MCS_MULTISELECT))
2262 MONTHCAL_NotifySelectionChange(infoPtr);
2263 return 0;
2267 return 1;
2271 static LRESULT
2272 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2274 NMHDR nmhdr;
2275 MCHITTESTINFO ht;
2276 DWORD hit;
2278 TRACE("\n");
2280 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2281 RECT *r;
2283 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2284 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2285 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2287 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2290 ReleaseCapture();
2292 /* always send NM_RELEASEDCAPTURE notification */
2293 nmhdr.hwndFrom = infoPtr->hwndSelf;
2294 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2295 nmhdr.code = NM_RELEASEDCAPTURE;
2296 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2298 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2300 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2302 ht.cbSize = sizeof(MCHITTESTINFO);
2303 ht.pt.x = (short)LOWORD(lParam);
2304 ht.pt.y = (short)HIWORD(lParam);
2305 hit = MONTHCAL_HitTest(infoPtr, &ht);
2307 infoPtr->status = MC_SEL_LBUTUP;
2308 MONTHCAL_SetDayFocus(infoPtr, NULL);
2310 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2312 SYSTEMTIME sel = infoPtr->minSel;
2314 /* will be invalidated here */
2315 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2317 /* send MCN_SELCHANGE only if new date selected */
2318 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2319 MONTHCAL_NotifySelectionChange(infoPtr);
2321 MONTHCAL_NotifySelect(infoPtr);
2324 return 0;
2328 static LRESULT
2329 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2331 TRACE("%ld\n", id);
2333 switch(id) {
2334 case MC_PREVNEXTMONTHTIMER:
2335 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2336 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2337 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2338 break;
2339 case MC_TODAYUPDATETIMER:
2341 SYSTEMTIME st;
2343 if(infoPtr->todaySet) return 0;
2345 GetLocalTime(&st);
2346 MONTHCAL_UpdateToday(infoPtr, &st);
2348 /* notification sent anyway */
2349 MONTHCAL_NotifySelectionChange(infoPtr);
2351 return 0;
2353 default:
2354 ERR("got unknown timer %ld\n", id);
2355 break;
2358 return 0;
2362 static LRESULT
2363 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2365 MCHITTESTINFO ht;
2366 SYSTEMTIME st_ht;
2367 INT hit;
2368 RECT r;
2370 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2372 ht.cbSize = sizeof(MCHITTESTINFO);
2373 ht.pt.x = (short)LOWORD(lParam);
2374 ht.pt.y = (short)HIWORD(lParam);
2375 ht.iOffset = -1;
2377 hit = MONTHCAL_HitTest(infoPtr, &ht);
2379 /* not on the calendar date numbers? bail out */
2380 TRACE("hit:%x\n",hit);
2381 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2383 MONTHCAL_SetDayFocus(infoPtr, NULL);
2384 return 0;
2387 st_ht = ht.st;
2389 /* if pointer is over focused day still there's nothing to do */
2390 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2392 MONTHCAL_GetDayRect(infoPtr, &ht.st, &r, ht.iOffset);
2394 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2395 SYSTEMTIME st[2];
2397 MONTHCAL_GetSelRange(infoPtr, st);
2399 /* If we're still at the first selected date and range is empty, return.
2400 If range isn't empty we should change range to a single firstSel */
2401 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2402 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2404 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2406 st[0] = infoPtr->firstSel;
2407 /* we should overwrite timestamp here */
2408 MONTHCAL_CopyDate(&st_ht, &st[1]);
2410 /* bounds will be swapped here if needed */
2411 MONTHCAL_SetSelRange(infoPtr, st);
2413 return 0;
2416 done:
2418 /* FIXME: this should specify a rectangle containing only the days that changed
2419 using InvalidateRect */
2420 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2422 return 0;
2426 static LRESULT
2427 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2429 HDC hdc;
2430 PAINTSTRUCT ps;
2432 if (hdc_paint)
2434 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2435 hdc = hdc_paint;
2437 else
2438 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2440 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2441 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2442 return 0;
2445 static LRESULT
2446 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2448 RECT rc;
2450 if (!GetClipBox(hdc, &rc)) return FALSE;
2452 FillRect(hdc, &rc, infoPtr->brushes[BrushBackground]);
2454 return TRUE;
2457 static LRESULT
2458 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2460 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2462 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2463 return 0;
2465 if (options & PRF_ERASEBKGND)
2466 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2468 if (options & PRF_CLIENT)
2469 MONTHCAL_Paint(infoPtr, hdc);
2471 return 0;
2474 static LRESULT
2475 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2477 TRACE("\n");
2479 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2481 return 0;
2484 /* sets the size information */
2485 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2487 static const WCHAR O0W[] = { '0','0',0 };
2488 RECT *title=&infoPtr->calendars[0].title;
2489 RECT *prev=&infoPtr->titlebtnprev;
2490 RECT *next=&infoPtr->titlebtnnext;
2491 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2492 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2493 RECT *wdays=&infoPtr->calendars[0].wdays;
2494 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2495 RECT *days=&infoPtr->calendars[0].days;
2496 RECT *todayrect=&infoPtr->todayrect;
2498 INT xdiv, dx, dy, i, j, x, y, c_dx, c_dy;
2499 WCHAR buff[80];
2500 TEXTMETRICW tm;
2501 INT day_width;
2502 RECT client;
2503 HFONT font;
2504 SIZE size;
2505 HDC hdc;
2507 GetClientRect(infoPtr->hwndSelf, &client);
2509 hdc = GetDC(infoPtr->hwndSelf);
2510 font = SelectObject(hdc, infoPtr->hFont);
2512 /* get the height and width of each day's text */
2513 GetTextMetricsW(hdc, &tm);
2514 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2516 /* find widest day name for current locale and font */
2517 day_width = 0;
2518 for (i = 0; i < 7; i++)
2520 SIZE sz;
2522 if (get_localized_dayname(infoPtr, i, buff, ARRAY_SIZE(buff)))
2524 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2525 if (sz.cx > day_width) day_width = sz.cx;
2527 else /* locale independent fallback on failure */
2529 static const WCHAR sunW[] = { 'S','u','n' };
2530 GetTextExtentPoint32W(hdc, sunW, ARRAY_SIZE(sunW), &sz);
2531 day_width = sz.cx;
2532 break;
2536 day_width += 2;
2538 /* recalculate the height and width increments and offsets */
2539 size.cx = 0;
2540 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2542 /* restore the originally selected font */
2543 SelectObject(hdc, font);
2544 ReleaseDC(infoPtr->hwndSelf, hdc);
2546 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2548 infoPtr->width_increment = max(day_width, size.cx * 2 + 4);
2549 infoPtr->height_increment = infoPtr->textHeight;
2551 /* calculate title area */
2552 title->top = 0;
2553 title->bottom = 3 * infoPtr->height_increment / 2;
2554 title->left = 0;
2555 title->right = infoPtr->width_increment * xdiv;
2557 /* set the dimensions of the next and previous buttons and center */
2558 /* the month text vertically */
2559 prev->top = next->top = title->top + 4;
2560 prev->bottom = next->bottom = title->bottom - 4;
2561 prev->left = title->left + 4;
2562 prev->right = prev->left + (title->bottom - title->top);
2563 next->right = title->right - 4;
2564 next->left = next->right - (title->bottom - title->top);
2566 /* titlemonth->left and right change based upon the current month
2567 and are recalculated in refresh as the current month may change
2568 without the control being resized */
2569 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2570 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2572 /* week numbers */
2573 weeknumrect->left = 0;
2574 weeknumrect->right = infoPtr->dwStyle & MCS_WEEKNUMBERS ? prev->right : 0;
2576 /* days abbreviated names */
2577 wdays->left = days->left = weeknumrect->right;
2578 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2579 wdays->top = title->bottom;
2580 wdays->bottom = wdays->top + infoPtr->height_increment;
2582 days->top = weeknumrect->top = wdays->bottom;
2583 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2585 todayrect->left = 0;
2586 todayrect->right = title->right;
2587 todayrect->top = days->bottom;
2588 todayrect->bottom = days->bottom + infoPtr->height_increment;
2590 /* compute calendar count, update all calendars */
2591 x = (client.right + MC_CALENDAR_PADDING) / (title->right - title->left + MC_CALENDAR_PADDING);
2592 /* today label affects whole height */
2593 if (infoPtr->dwStyle & MCS_NOTODAY)
2594 y = (client.bottom + MC_CALENDAR_PADDING) / (days->bottom - title->top + MC_CALENDAR_PADDING);
2595 else
2596 y = (client.bottom - todayrect->bottom + todayrect->top + MC_CALENDAR_PADDING) /
2597 (days->bottom - title->top + MC_CALENDAR_PADDING);
2599 /* TODO: ensure that count is properly adjusted to fit 12 months constraint */
2600 if (x == 0) x = 1;
2601 if (y == 0) y = 1;
2603 if (x*y != MONTHCAL_GetCalCount(infoPtr))
2605 infoPtr->dim.cx = x;
2606 infoPtr->dim.cy = y;
2607 infoPtr->calendars = heap_realloc(infoPtr->calendars, MONTHCAL_GetCalCount(infoPtr)*sizeof(CALENDAR_INFO));
2609 infoPtr->monthdayState = heap_realloc(infoPtr->monthdayState,
2610 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
2611 MONTHCAL_NotifyDayState(infoPtr);
2613 /* update pointers that we'll need */
2614 title = &infoPtr->calendars[0].title;
2615 wdays = &infoPtr->calendars[0].wdays;
2616 days = &infoPtr->calendars[0].days;
2619 for (i = 1; i < MONTHCAL_GetCalCount(infoPtr); i++)
2621 /* set months */
2622 infoPtr->calendars[i] = infoPtr->calendars[0];
2623 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, i);
2626 /* offset all rectangles to center in client area */
2627 c_dx = (client.right - x * title->right - MC_CALENDAR_PADDING * (x-1)) / 2;
2628 c_dy = (client.bottom - y * todayrect->bottom - MC_CALENDAR_PADDING * (y-1)) / 2;
2630 /* if calendar doesn't fit client area show it at left/top bounds */
2631 if (title->left + c_dx < 0) c_dx = 0;
2632 if (title->top + c_dy < 0) c_dy = 0;
2634 for (i = 0; i < y; i++)
2636 for (j = 0; j < x; j++)
2638 dx = j*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx;
2639 dy = i*(days->bottom - title->top + MC_CALENDAR_PADDING) + c_dy;
2641 OffsetRect(&infoPtr->calendars[i*x+j].title, dx, dy);
2642 OffsetRect(&infoPtr->calendars[i*x+j].titlemonth, dx, dy);
2643 OffsetRect(&infoPtr->calendars[i*x+j].titleyear, dx, dy);
2644 OffsetRect(&infoPtr->calendars[i*x+j].wdays, dx, dy);
2645 OffsetRect(&infoPtr->calendars[i*x+j].weeknums, dx, dy);
2646 OffsetRect(&infoPtr->calendars[i*x+j].days, dx, dy);
2650 OffsetRect(prev, c_dx, c_dy);
2651 OffsetRect(next, (x-1)*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx, c_dy);
2653 i = infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx;
2654 todayrect->left = infoPtr->calendars[i].title.left;
2655 todayrect->right = infoPtr->calendars[i].title.right;
2656 todayrect->top = infoPtr->calendars[i].days.bottom;
2657 todayrect->bottom = infoPtr->calendars[i].days.bottom + infoPtr->height_increment;
2659 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2660 infoPtr->width_increment,infoPtr->height_increment,
2661 wine_dbgstr_rect(&client),
2662 wine_dbgstr_rect(title),
2663 wine_dbgstr_rect(wdays),
2664 wine_dbgstr_rect(days),
2665 wine_dbgstr_rect(todayrect));
2668 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2670 TRACE("(width=%d, height=%d)\n", Width, Height);
2672 MONTHCAL_UpdateSize(infoPtr);
2673 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2675 return 0;
2678 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2680 return (LRESULT)infoPtr->hFont;
2683 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2685 HFONT hOldFont;
2686 LOGFONTW lf;
2688 if (!hFont) return 0;
2690 hOldFont = infoPtr->hFont;
2691 infoPtr->hFont = hFont;
2693 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2694 lf.lfWeight = FW_BOLD;
2695 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2697 MONTHCAL_UpdateSize(infoPtr);
2699 if (redraw)
2700 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2702 return (LRESULT)hOldFont;
2705 /* update theme after a WM_THEMECHANGED message */
2706 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2708 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2709 CloseThemeData (theme);
2710 OpenThemeData (infoPtr->hwndSelf, themeClass);
2711 return 0;
2714 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2715 const STYLESTRUCT *lpss)
2717 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2718 wStyleType, lpss->styleOld, lpss->styleNew);
2720 if (wStyleType != GWL_STYLE) return 0;
2722 infoPtr->dwStyle = lpss->styleNew;
2724 /* make room for week numbers */
2725 if ((lpss->styleNew ^ lpss->styleOld) & (MCS_WEEKNUMBERS | MCS_SHORTDAYSOFWEEK))
2726 MONTHCAL_UpdateSize(infoPtr);
2728 return 0;
2731 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2732 STYLESTRUCT *lpss)
2734 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2735 wStyleType, lpss->styleOld, lpss->styleNew);
2737 /* block MCS_MULTISELECT change */
2738 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2740 if (lpss->styleOld & MCS_MULTISELECT)
2741 lpss->styleNew |= MCS_MULTISELECT;
2742 else
2743 lpss->styleNew &= ~MCS_MULTISELECT;
2746 /* block MCS_DAYSTATE change */
2747 if ((lpss->styleNew ^ lpss->styleOld) & MCS_DAYSTATE)
2749 if (lpss->styleOld & MCS_DAYSTATE)
2750 lpss->styleNew |= MCS_DAYSTATE;
2751 else
2752 lpss->styleNew &= ~MCS_DAYSTATE;
2755 return 0;
2758 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2759 static LRESULT
2760 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2762 MONTHCAL_INFO *infoPtr;
2764 /* allocate memory for info structure */
2765 infoPtr = heap_alloc_zero(sizeof(*infoPtr));
2766 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2768 if (infoPtr == NULL) {
2769 ERR("could not allocate info memory!\n");
2770 return 0;
2773 infoPtr->hwndSelf = hwnd;
2774 infoPtr->hwndNotify = lpcs->hwndParent;
2775 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2776 infoPtr->dim.cx = infoPtr->dim.cy = 1;
2777 infoPtr->calendars = heap_alloc_zero(sizeof(CALENDAR_INFO));
2778 if (!infoPtr->calendars) goto fail;
2779 infoPtr->monthdayState = heap_alloc_zero(3 * sizeof(MONTHDAYSTATE));
2780 if (!infoPtr->monthdayState) goto fail;
2782 /* initialize info structure */
2783 /* FIXME: calculate systemtime ->> localtime(subtract timezoneinfo) */
2785 GetLocalTime(&infoPtr->todaysDate);
2786 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2788 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2790 infoPtr->colors[MCSC_BACKGROUND] = comctl32_color.clrWindow;
2791 infoPtr->colors[MCSC_TEXT] = comctl32_color.clrWindowText;
2792 infoPtr->colors[MCSC_TITLEBK] = comctl32_color.clrActiveCaption;
2793 infoPtr->colors[MCSC_TITLETEXT] = comctl32_color.clrWindow;
2794 infoPtr->colors[MCSC_MONTHBK] = comctl32_color.clrWindow;
2795 infoPtr->colors[MCSC_TRAILINGTEXT] = comctl32_color.clrGrayText;
2797 infoPtr->brushes[BrushBackground] = CreateSolidBrush(infoPtr->colors[MCSC_BACKGROUND]);
2798 infoPtr->brushes[BrushTitle] = CreateSolidBrush(infoPtr->colors[MCSC_TITLEBK]);
2799 infoPtr->brushes[BrushMonth] = CreateSolidBrush(infoPtr->colors[MCSC_MONTHBK]);
2801 infoPtr->pens[PenRed] = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
2802 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[MCSC_TEXT]);
2804 infoPtr->minSel = infoPtr->todaysDate;
2805 infoPtr->maxSel = infoPtr->todaysDate;
2806 infoPtr->calendars[0].month = infoPtr->todaysDate;
2807 infoPtr->isUnicode = TRUE;
2809 /* setup control layout and day state data */
2810 MONTHCAL_UpdateSize(infoPtr);
2812 /* today auto update timer, to be freed only on control destruction */
2813 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2815 OpenThemeData (infoPtr->hwndSelf, themeClass);
2817 return 0;
2819 fail:
2820 heap_free(infoPtr->monthdayState);
2821 heap_free(infoPtr->calendars);
2822 heap_free(infoPtr);
2823 return 0;
2826 static LRESULT
2827 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2829 INT i;
2831 /* free month calendar info data */
2832 heap_free(infoPtr->monthdayState);
2833 heap_free(infoPtr->calendars);
2834 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2836 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2838 for (i = 0; i < BrushLast; i++) DeleteObject(infoPtr->brushes[i]);
2839 for (i = 0; i < PenLast; i++) DeleteObject(infoPtr->pens[i]);
2841 heap_free(infoPtr);
2842 return 0;
2846 * Handler for WM_NOTIFY messages
2848 static LRESULT
2849 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2851 /* notification from year edit updown */
2852 if (hdr->code == UDN_DELTAPOS)
2854 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2856 if (hdr->hwndFrom == infoPtr->hWndYearUpDown && nmud->iDelta)
2858 /* year value limits are set up explicitly after updown creation */
2859 MONTHCAL_Scroll(infoPtr, 12 * nmud->iDelta, FALSE);
2860 MONTHCAL_NotifyDayState(infoPtr);
2861 MONTHCAL_NotifySelectionChange(infoPtr);
2864 return 0;
2867 static inline BOOL
2868 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2870 BOOL prev = infoPtr->isUnicode;
2871 infoPtr->isUnicode = isUnicode;
2872 return prev;
2875 static inline BOOL
2876 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2878 return infoPtr->isUnicode;
2881 static LRESULT WINAPI
2882 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2884 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0);
2886 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2888 if (!infoPtr && (uMsg != WM_CREATE))
2889 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2890 switch(uMsg)
2892 case MCM_GETCURSEL:
2893 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2895 case MCM_SETCURSEL:
2896 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2898 case MCM_GETMAXSELCOUNT:
2899 return MONTHCAL_GetMaxSelCount(infoPtr);
2901 case MCM_SETMAXSELCOUNT:
2902 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2904 case MCM_GETSELRANGE:
2905 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2907 case MCM_SETSELRANGE:
2908 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2910 case MCM_GETMONTHRANGE:
2911 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2913 case MCM_SETDAYSTATE:
2914 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2916 case MCM_GETMINREQRECT:
2917 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2919 case MCM_GETCOLOR:
2920 return MONTHCAL_GetColor(infoPtr, wParam);
2922 case MCM_SETCOLOR:
2923 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2925 case MCM_GETTODAY:
2926 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2928 case MCM_SETTODAY:
2929 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2931 case MCM_HITTEST:
2932 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2934 case MCM_GETFIRSTDAYOFWEEK:
2935 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2937 case MCM_SETFIRSTDAYOFWEEK:
2938 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2940 case MCM_GETRANGE:
2941 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2943 case MCM_SETRANGE:
2944 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2946 case MCM_GETMONTHDELTA:
2947 return MONTHCAL_GetMonthDelta(infoPtr);
2949 case MCM_SETMONTHDELTA:
2950 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2952 case MCM_GETMAXTODAYWIDTH:
2953 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2955 case MCM_SETUNICODEFORMAT:
2956 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2958 case MCM_GETUNICODEFORMAT:
2959 return MONTHCAL_GetUnicodeFormat(infoPtr);
2961 case MCM_GETCALENDARCOUNT:
2962 return MONTHCAL_GetCalCount(infoPtr);
2964 case WM_GETDLGCODE:
2965 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2967 case WM_RBUTTONUP:
2968 return MONTHCAL_RButtonUp(infoPtr, lParam);
2970 case WM_LBUTTONDOWN:
2971 return MONTHCAL_LButtonDown(infoPtr, lParam);
2973 case WM_MOUSEMOVE:
2974 return MONTHCAL_MouseMove(infoPtr, lParam);
2976 case WM_LBUTTONUP:
2977 return MONTHCAL_LButtonUp(infoPtr, lParam);
2979 case WM_PAINT:
2980 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2982 case WM_PRINTCLIENT:
2983 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2985 case WM_ERASEBKGND:
2986 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2988 case WM_SETFOCUS:
2989 return MONTHCAL_SetFocus(infoPtr);
2991 case WM_SIZE:
2992 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2994 case WM_NOTIFY:
2995 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2997 case WM_CREATE:
2998 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
3000 case WM_SETFONT:
3001 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
3003 case WM_GETFONT:
3004 return MONTHCAL_GetFont(infoPtr);
3006 case WM_TIMER:
3007 return MONTHCAL_Timer(infoPtr, wParam);
3009 case WM_THEMECHANGED:
3010 return theme_changed (infoPtr);
3012 case WM_DESTROY:
3013 return MONTHCAL_Destroy(infoPtr);
3015 case WM_SYSCOLORCHANGE:
3016 COMCTL32_RefreshSysColors();
3017 return 0;
3019 case WM_STYLECHANGED:
3020 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
3022 case WM_STYLECHANGING:
3023 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
3025 default:
3026 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
3027 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
3028 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
3033 void
3034 MONTHCAL_Register(void)
3036 WNDCLASSW wndClass;
3038 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
3039 wndClass.style = CS_GLOBALCLASS;
3040 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
3041 wndClass.cbClsExtra = 0;
3042 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
3043 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3044 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
3045 wndClass.lpszClassName = MONTHCAL_CLASSW;
3047 RegisterClassW(&wndClass);
3051 void
3052 MONTHCAL_Unregister(void)
3054 UnregisterClassW(MONTHCAL_CLASSW, NULL);