comctl32/monthcal: Fix some day state problems.
[wine/multimedia.git] / dlls / comctl32 / monthcal.c
blob9e4284cd6d98ce12ae84cee9044f6c64171b854a
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 * NOTE
27 * This code was audited for completeness against the documented features
28 * of Comctl32.dll version 6.0 on Oct. 20, 2004, by Dimitrie O. Paun.
30 * Unless otherwise noted, we believe this code to be complete, as per
31 * the specification mentioned above.
32 * If you discover missing features, or bugs, please note them below.
34 * TODO:
35 * -- MCM_[GS]ETUNICODEFORMAT
36 * -- handle resources better (doesn't work now);
37 * -- take care of internationalization.
38 * -- keyboard handling.
39 * -- search for FIXME
42 #include <math.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "commctrl.h"
54 #include "comctl32.h"
55 #include "uxtheme.h"
56 #include "vssym32.h"
57 #include "wine/unicode.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
62 #define MC_SEL_LBUTUP 1 /* Left button released */
63 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
64 #define MC_PREVPRESSED 4 /* Prev month button pressed */
65 #define MC_NEXTPRESSED 8 /* Next month button pressed */
66 #define MC_PREVNEXTMONTHDELAY 350 /* when continuously pressing `next/prev
67 month', wait 350 ms before going
68 to the next/prev month */
69 #define MC_TODAYUPDATEDELAY 120000 /* time between today check for update (2 min) */
71 #define MC_PREVNEXTMONTHTIMER 1 /* Timer IDs */
72 #define MC_TODAYUPDATETIMER 2
74 #define MC_CALENDAR_PADDING 6
76 #define countof(arr) (sizeof(arr)/sizeof(arr[0]))
78 /* convert from days to 100 nanoseconds unit - used as FILETIME unit */
79 #define DAYSTO100NSECS(days) (((ULONGLONG)(days))*24*60*60*10000000)
81 enum CachedPen
83 PenRed = 0,
84 PenText,
85 PenLast
88 enum CachedBrush
90 BrushTitle = 0,
91 BrushMonth,
92 BrushBackground,
93 BrushLast
96 /* single calendar data */
97 typedef struct _CALENDAR_INFO
99 RECT title; /* rect for the header above the calendar */
100 RECT titlemonth; /* the 'month name' text in the header */
101 RECT titleyear; /* the 'year number' text in the header */
102 RECT wdays; /* week days at top */
103 RECT days; /* calendar area */
104 RECT weeknums; /* week numbers at left side */
106 SYSTEMTIME month;/* contains calendar main month/year */
107 } CALENDAR_INFO;
109 typedef struct
111 HWND hwndSelf;
112 DWORD dwStyle; /* cached GWL_STYLE */
114 COLORREF colors[MCSC_TRAILINGTEXT+1];
115 HBRUSH brushes[BrushLast];
116 HPEN pens[PenLast];
118 HFONT hFont;
119 HFONT hBoldFont;
120 int textHeight;
121 int textWidth;
122 int height_increment;
123 int width_increment;
124 INT delta; /* scroll rate; # of months that the */
125 /* control moves when user clicks a scroll button */
126 int visible; /* # of months visible */
127 int firstDay; /* Start month calendar with firstDay's day,
128 stored in SYSTEMTIME format */
129 BOOL firstDaySet; /* first week day differs from locale defined */
131 BOOL isUnicode; /* value set with MCM_SETUNICODE format */
133 MONTHDAYSTATE *monthdayState;
134 SYSTEMTIME todaysDate;
135 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
136 int status; /* See MC_SEL flags */
137 SYSTEMTIME firstSel; /* first selected day */
138 INT maxSelCount;
139 SYSTEMTIME minSel; /* contains single selection when used without MCS_MULTISELECT */
140 SYSTEMTIME maxSel;
141 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
142 DWORD rangeValid;
143 SYSTEMTIME minDate;
144 SYSTEMTIME maxDate;
146 RECT titlebtnnext; /* the `next month' button in the header */
147 RECT titlebtnprev; /* the `prev month' button in the header */
148 RECT todayrect; /* `today: xx/xx/xx' text rect */
149 HWND hwndNotify; /* Window to receive the notifications */
150 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
151 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
152 WNDPROC EditWndProc; /* original Edit window procedure */
154 CALENDAR_INFO *calendars;
155 SIZE dim; /* [cx,cy] - dimensions of calendars matrix, row/column count */
156 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
158 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
160 /* empty SYSTEMTIME const */
161 static const SYSTEMTIME st_null;
162 /* valid date limits */
163 static const SYSTEMTIME max_allowed_date = { .wYear = 9999, .wMonth = 12, .wDay = 31 };
164 static const SYSTEMTIME min_allowed_date = { .wYear = 1752, .wMonth = 9, .wDay = 14 };
166 /* Prev/Next buttons */
167 enum nav_direction
169 DIRECTION_BACKWARD,
170 DIRECTION_FORWARD
173 /* helper functions */
174 static inline INT MONTHCAL_GetCalCount(const MONTHCAL_INFO *infoPtr)
176 return infoPtr->dim.cx * infoPtr->dim.cy;
179 /* send a single MCN_SELCHANGE notification */
180 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
182 NMSELCHANGE nmsc;
184 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
185 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
186 nmsc.nmhdr.code = MCN_SELCHANGE;
187 nmsc.stSelStart = infoPtr->minSel;
188 nmsc.stSelEnd = infoPtr->maxSel;
189 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
192 /* send a single MCN_SELECT notification */
193 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
195 NMSELCHANGE nmsc;
197 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
198 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
199 nmsc.nmhdr.code = MCN_SELECT;
200 nmsc.stSelStart = infoPtr->minSel;
201 nmsc.stSelEnd = infoPtr->maxSel;
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 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 ) return FALSE;
250 if(time->wDayOfWeek > 6) return FALSE;
251 if(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 static 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 if(infoPtr->rangeValid & GDTR_MAX) {
359 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
360 fix_st = &infoPtr->maxDate;
363 else if(infoPtr->rangeValid & GDTR_MIN) {
364 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
365 fix_st = &infoPtr->minDate;
369 if (fix && fix_st) {
370 date->wYear = fix_st->wYear;
371 date->wMonth = fix_st->wMonth;
374 return fix_st ? FALSE : TRUE;
377 /* Checks passed range width with configured maximum selection count
379 * PARAMETERS
381 * [I] infoPtr : valid pointer to control data
382 * [I] range0 : pointer to valid date data (requested bound)
383 * [I] range1 : pointer to valid date data (primary bound)
384 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
386 * Adjust value computed basing on primary bound and current maximum selection
387 * count. For simple range check (without adjusted value required) (range0, range1)
388 * relation means nothing.
390 * RETURN VALUE
392 * TRUE - range is shorter or equal to maximum
393 * FALSE - range is larger than maximum
395 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
396 const SYSTEMTIME *range0,
397 const SYSTEMTIME *range1,
398 SYSTEMTIME *adjust)
400 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
401 FILETIME ft_range0, ft_range1;
402 LONG cmp;
404 SystemTimeToFileTime(range0, &ft_range0);
405 SystemTimeToFileTime(range1, &ft_range1);
407 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
408 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
409 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
410 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
412 cmp = CompareFileTime(&ft_range0, &ft_range1);
414 if(cmp == 1)
415 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
416 else
417 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
419 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
421 if(adjust) {
422 if(cmp == 1)
423 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
424 else
425 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
427 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
428 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
429 FileTimeToSystemTime(&ft_range0, adjust);
432 return FALSE;
434 else return TRUE;
437 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
438 Milliseconds are intentionally not validated. */
439 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
441 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
442 return FALSE;
443 else
444 return TRUE;
447 /* Note:Depending on DST, this may be offset by a day.
448 Need to find out if we're on a DST place & adjust the clock accordingly.
449 Above function assumes we have a valid data.
450 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
451 0 = Sunday.
454 /* Returns the day in the week
456 * PARAMETERS
457 * [i] date : input date
458 * [I] inplace : set calculated value back to date structure
460 * RETURN VALUE
461 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
463 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
465 SYSTEMTIME st = st_null;
466 FILETIME ft;
468 MONTHCAL_CopyDate(date, &st);
470 SystemTimeToFileTime(&st, &ft);
471 FileTimeToSystemTime(&ft, &st);
473 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
475 return st.wDayOfWeek;
478 /* add/subtract 'months' from date */
479 static inline void MONTHCAL_GetMonth(SYSTEMTIME *date, INT months)
481 INT length, m = date->wMonth + months;
483 date->wYear += m > 0 ? (m - 1) / 12 : m / 12 - 1;
484 date->wMonth = m > 0 ? (m - 1) % 12 + 1 : 12 + m % 12;
485 /* fix moving from last day in a month */
486 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
487 if(date->wDay > length) date->wDay = length;
488 MONTHCAL_CalculateDayOfWeek(date, TRUE);
491 /* properly updates date to point on next month */
492 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
494 MONTHCAL_GetMonth(date, 1);
497 /* properly updates date to point on prev month */
498 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
500 MONTHCAL_GetMonth(date, -1);
503 /* Returns full date for a first currently visible day */
504 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
506 /* zero indexed calendar has the earliest date */
507 SYSTEMTIME st_first = infoPtr->calendars[0].month;
508 INT firstDay;
510 st_first.wDay = 1;
511 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
513 *date = infoPtr->calendars[0].month;
514 MONTHCAL_GetPrevMonth(date);
516 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
517 (infoPtr->firstDay - firstDay) % 7 + 1;
519 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
520 date->wDay -= 7;
522 /* fix day of week */
523 MONTHCAL_CalculateDayOfWeek(date, TRUE);
526 /* Returns full date for a last currently visible day */
527 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
529 /* the latest date is in latest calendar */
530 SYSTEMTIME st, *lt_month = &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
531 INT first_day;
533 *date = *lt_month;
534 st = *lt_month;
536 /* day of week of first day of current month */
537 st.wDay = 1;
538 first_day = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
540 MONTHCAL_GetNextMonth(date);
541 MONTHCAL_GetPrevMonth(&st);
543 /* last calendar starts with some date from previous month that not displayed */
544 st.wDay = MONTHCAL_MonthLength(st.wMonth, st.wYear) +
545 (infoPtr->firstDay - first_day) % 7 + 1;
546 if (st.wDay > MONTHCAL_MonthLength(st.wMonth, st.wYear)) st.wDay -= 7;
548 /* Use month length to get max day. 42 means max day count in calendar area */
549 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
550 MONTHCAL_MonthLength(lt_month->wMonth, lt_month->wYear);
552 /* fix day of week */
553 MONTHCAL_CalculateDayOfWeek(date, TRUE);
556 /* From a given point calculate the row, column and day in the calendar,
557 'day == 0' means the last day of the last month. */
558 static int MONTHCAL_GetDayFromPos(const MONTHCAL_INFO *infoPtr, POINT pt, INT calIdx)
560 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
561 int firstDay, col, row;
562 RECT client;
564 GetClientRect(infoPtr->hwndSelf, &client);
566 /* if the point is outside the x bounds of the window put it at the boundary */
567 if (pt.x > client.right) pt.x = client.right;
569 col = (pt.x - infoPtr->calendars[calIdx].days.left ) / infoPtr->width_increment;
570 row = (pt.y - infoPtr->calendars[calIdx].days.top ) / infoPtr->height_increment;
572 st.wDay = 1;
573 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
574 return col + 7 * row - firstDay;
577 /* Get day position for given date and calendar
579 * PARAMETERS
581 * [I] infoPtr : pointer to control data
582 * [I] date : date value
583 * [O] col : day column (zero based)
584 * [O] row : week column (zero based)
585 * [I] calIdx : calendar index
587 static void MONTHCAL_GetDayPos(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
588 INT *col, INT *row, INT calIdx)
590 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
591 INT first;
593 st.wDay = 1;
594 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
596 if (calIdx == 0 || calIdx == MONTHCAL_GetCalCount(infoPtr)-1) {
597 const SYSTEMTIME *cal = &infoPtr->calendars[calIdx].month;
598 LONG cmp = MONTHCAL_CompareMonths(date, &st);
600 /* previous month */
601 if (cmp == -1) {
602 *col = (first - MONTHCAL_MonthLength(date->wMonth, cal->wYear) + date->wDay) % 7;
603 *row = 0;
604 return;
607 /* next month calculation is same as for current, just add current month length */
608 if (cmp == 1)
609 first += MONTHCAL_MonthLength(cal->wMonth, cal->wYear);
612 *col = (date->wDay + first) % 7;
613 *row = (date->wDay + first - *col) / 7;
616 /* returns bounding box for day in given position in given calendar */
617 static inline void MONTHCAL_GetDayRectI(const MONTHCAL_INFO *infoPtr, RECT *r,
618 INT col, INT row, INT calIdx)
620 r->left = infoPtr->calendars[calIdx].days.left + col * infoPtr->width_increment;
621 r->right = r->left + infoPtr->width_increment;
622 r->top = infoPtr->calendars[calIdx].days.top + row * infoPtr->height_increment;
623 r->bottom = r->top + infoPtr->textHeight;
626 /* Returns bounding box for given date
628 * NOTE: when calendar index is unknown pass -1
630 static inline void MONTHCAL_GetDayRect(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
631 RECT *r, INT calIdx)
633 INT col, row;
635 if (calIdx == -1)
637 INT cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[0].month);
639 if (cmp <= 0)
640 calIdx = 0;
641 else
643 cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month);
644 if (cmp >= 0)
645 calIdx = MONTHCAL_GetCalCount(infoPtr)-1;
646 else
648 for (calIdx = 1; calIdx < MONTHCAL_GetCalCount(infoPtr)-1; calIdx++)
649 if (MONTHCAL_CompareMonths(date, &infoPtr->calendars[calIdx].month) == 0)
650 break;
655 MONTHCAL_GetDayPos(infoPtr, date, &col, &row, calIdx);
656 MONTHCAL_GetDayRectI(infoPtr, r, col, row, calIdx);
659 static LRESULT
660 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
662 INT range;
664 TRACE("flag=%d, st=%p\n", flag, st);
666 switch (flag) {
667 case GMR_VISIBLE:
669 if (st)
671 st[0] = infoPtr->calendars[0].month;
672 st[1] = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
674 if (st[0].wMonth == min_allowed_date.wMonth &&
675 st[0].wYear == min_allowed_date.wYear)
677 st[0].wDay = min_allowed_date.wDay;
679 else
680 st[0].wDay = 1;
681 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
683 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
684 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
687 range = MONTHCAL_GetCalCount(infoPtr);
688 break;
690 case GMR_DAYSTATE:
692 if (st)
694 MONTHCAL_GetMinDate(infoPtr, &st[0]);
695 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
697 /* include two partially visible months */
698 range = MONTHCAL_GetCalCount(infoPtr) + 2;
699 break;
701 default:
702 WARN("Unknown flag value, got %d\n", flag);
703 range = 0;
706 return range;
709 /* Focused day helper:
711 - set focused date to given value;
712 - reset to zero value if NULL passed;
713 - invalidate previous and new day rectangle only if needed.
715 Returns TRUE if focused day changed, FALSE otherwise.
717 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
719 RECT r;
721 if(st)
723 /* there's nothing to do if it's the same date,
724 mouse move within same date rectangle case */
725 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
727 /* invalidate old focused day */
728 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
729 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
731 infoPtr->focusedSel = *st;
734 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
736 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
737 infoPtr->focusedSel = st_null;
739 /* on set invalidates new day, on reset clears previous focused day */
740 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
742 return TRUE;
745 /* draw today boundary box for specified rectangle */
746 static void MONTHCAL_Circle(const MONTHCAL_INFO *infoPtr, HDC hdc, const RECT *r)
748 HPEN old_pen = SelectObject(hdc, infoPtr->pens[PenRed]);
749 HBRUSH old_brush;
751 old_brush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
752 Rectangle(hdc, r->left, r->top, r->right, r->bottom);
754 SelectObject(hdc, old_brush);
755 SelectObject(hdc, old_pen);
758 /* Draw today day mark rectangle
760 * [I] hdc : context to draw in
761 * [I] date : day to mark with rectangle
764 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
765 const SYSTEMTIME *date)
767 RECT r;
769 MONTHCAL_GetDayRect(infoPtr, date, &r, -1);
770 MONTHCAL_Circle(infoPtr, hdc, &r);
773 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
774 int bold, const PAINTSTRUCT *ps)
776 static const WCHAR fmtW[] = { '%','d',0 };
777 WCHAR buf[10];
778 RECT r, r_temp;
779 COLORREF oldCol = 0;
780 COLORREF oldBk = 0;
781 INT old_bkmode, selection;
783 /* no need to check styles: when selection is not valid, it is set to zero.
784 1 < day < 31, so everything is OK */
785 MONTHCAL_GetDayRect(infoPtr, st, &r, -1);
786 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
788 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
789 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0))
791 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
792 TRACE("%s\n", wine_dbgstr_rect(&r));
793 oldCol = SetTextColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
794 oldBk = SetBkColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
795 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
797 selection = 1;
799 else
800 selection = 0;
802 SelectObject(hdc, bold ? infoPtr->hBoldFont : infoPtr->hFont);
804 old_bkmode = SetBkMode(hdc, TRANSPARENT);
805 wsprintfW(buf, fmtW, st->wDay);
806 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
807 SetBkMode(hdc, old_bkmode);
809 if (selection)
811 SetTextColor(hdc, oldCol);
812 SetBkColor(hdc, oldBk);
816 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, enum nav_direction button)
818 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
819 RECT *r = button == DIRECTION_FORWARD ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
820 BOOL pressed = button == DIRECTION_FORWARD ? infoPtr->status & MC_NEXTPRESSED :
821 infoPtr->status & MC_PREVPRESSED;
822 if (theme)
824 static const int states[] = {
825 /* Prev button */
826 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
827 /* Next button */
828 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
830 int stateNum = button == DIRECTION_FORWARD ? 3 : 0;
831 if (pressed)
832 stateNum += 1;
833 else
835 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
837 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
839 else
841 int style = button == DIRECTION_FORWARD ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
842 if (pressed)
843 style |= DFCS_PUSHED;
844 else
846 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
849 DrawFrameControl(hdc, r, DFC_SCROLL, style);
853 /* paint a title with buttons and month/year string */
854 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
856 static const WCHAR fmt_monthW[] = { '%','s',' ','%','l','d',0 };
857 RECT *title = &infoPtr->calendars[calIdx].title;
858 const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
859 WCHAR buf_month[80], buf_fmt[80];
860 SIZE sz;
862 /* fill header box */
863 FillRect(hdc, title, infoPtr->brushes[BrushTitle]);
865 /* month/year string */
866 SetBkColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
867 SetTextColor(hdc, infoPtr->colors[MCSC_TITLETEXT]);
868 SelectObject(hdc, infoPtr->hBoldFont);
870 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1 + st->wMonth - 1,
871 buf_month, countof(buf_month));
873 wsprintfW(buf_fmt, fmt_monthW, buf_month, st->wYear);
874 DrawTextW(hdc, buf_fmt, strlenW(buf_fmt), title,
875 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
877 /* update title rectangles with current month - used while testing hits */
878 GetTextExtentPoint32W(hdc, buf_fmt, strlenW(buf_fmt), &sz);
879 infoPtr->calendars[calIdx].titlemonth.left = title->right / 2 + title->left / 2 - sz.cx / 2;
880 infoPtr->calendars[calIdx].titleyear.right = title->right / 2 + title->left / 2 + sz.cx / 2;
882 GetTextExtentPoint32W(hdc, buf_month, strlenW(buf_month), &sz);
883 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
884 infoPtr->calendars[calIdx].titleyear.left = infoPtr->calendars[calIdx].titlemonth.right;
887 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
889 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
890 static const WCHAR fmt_weekW[] = { '%','d',0 };
891 INT mindays, weeknum, weeknum1, startofprescal;
892 INT i, prev_month;
893 SYSTEMTIME st;
894 WCHAR buf[80];
895 HPEN old_pen;
896 RECT r;
898 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
900 MONTHCAL_GetMinDate(infoPtr, &st);
901 startofprescal = st.wDay;
902 st = *date;
904 prev_month = date->wMonth - 1;
905 if(prev_month == 0) prev_month = 12;
908 Rules what week to call the first week of a new year:
909 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
910 The week containing Jan 1 is the first week of year
911 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
912 First week of year must contain 4 days of the new year
913 LOCALE_IFIRSTWEEKOFYEAR == 1 (what countries?)
914 The first week of the year must contain only days of the new year
916 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
917 weeknum = atoiW(buf);
918 switch (weeknum)
920 case 1: mindays = 6;
921 break;
922 case 2: mindays = 3;
923 break;
924 case 0: mindays = 0;
925 break;
926 default:
927 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
928 mindays = 0;
931 if (date->wMonth == 1)
933 /* calculate all those exceptions for January */
934 st.wDay = st.wMonth = 1;
935 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
936 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
937 weeknum = 1;
938 else
940 weeknum = 0;
941 for(i = 0; i < 11; i++)
942 weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
944 weeknum += startofprescal + 7;
945 weeknum /= 7;
946 st.wYear -= 1;
947 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
948 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
951 else
953 weeknum = 0;
954 for(i = 0; i < prev_month - 1; i++)
955 weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
957 weeknum += startofprescal + 7;
958 weeknum /= 7;
959 st.wDay = st.wMonth = 1;
960 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
961 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
964 r = infoPtr->calendars[calIdx].weeknums;
966 /* erase whole week numbers area */
967 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
968 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
970 /* reduce rectangle to one week number */
971 r.bottom = r.top + infoPtr->height_increment;
973 for(i = 0; i < 6; i++) {
974 if((i == 0) && (weeknum > 50))
976 wsprintfW(buf, fmt_weekW, weeknum);
977 weeknum = 0;
979 else if((i == 5) && (weeknum > 47))
981 wsprintfW(buf, fmt_weekW, 1);
983 else
984 wsprintfW(buf, fmt_weekW, weeknum + i);
986 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
987 OffsetRect(&r, 0, infoPtr->height_increment);
990 /* line separator for week numbers column */
991 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
992 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
993 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
994 SelectObject(hdc, old_pen);
997 /* bottom today date */
998 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1000 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
1001 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
1002 RECT text_rect, box_rect;
1003 HFONT old_font;
1004 INT col;
1006 if(infoPtr->dwStyle & MCS_NOTODAY) return;
1008 if (!LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, countof(buf_todayW)))
1010 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
1011 WARN("Can't load resource\n");
1012 strcpyW(buf_todayW, todayW);
1015 col = infoPtr->dwStyle & MCS_NOTODAYCIRCLE ? 0 : 1;
1016 if (infoPtr->dwStyle & MCS_WEEKNUMBERS) col--;
1017 /* label is located below first calendar last row */
1018 MONTHCAL_GetDayRectI(infoPtr, &text_rect, col, 6, infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx);
1019 box_rect = text_rect;
1021 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
1022 buf_dateW, countof(buf_dateW));
1023 old_font = SelectObject(hdc, infoPtr->hBoldFont);
1024 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1026 wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
1027 DrawTextW(hdc, buf, -1, &text_rect, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1028 DrawTextW(hdc, buf, -1, &text_rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1030 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
1031 OffsetRect(&box_rect, -infoPtr->width_increment, 0);
1032 MONTHCAL_Circle(infoPtr, hdc, &box_rect);
1035 SelectObject(hdc, old_font);
1038 /* today mark + focus */
1039 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1041 /* circle today date if only it's in fully visible month */
1042 if (!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
1044 INT i;
1046 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1047 if (!MONTHCAL_CompareMonths(&infoPtr->todaysDate, &infoPtr->calendars[i].month))
1049 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
1050 break;
1054 if (!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
1056 RECT r;
1057 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
1058 DrawFocusRect(hdc, &r);
1062 /* months before first calendar month and after last calendar month */
1063 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1065 INT mask, length, index;
1066 SYSTEMTIME st_max, st;
1068 if (infoPtr->dwStyle & MCS_NOTRAILINGDATES) return;
1070 SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
1072 /* draw prev month */
1073 MONTHCAL_GetMinDate(infoPtr, &st);
1074 mask = 1 << (st.wDay-1);
1075 /* December and January both 31 days long, so no worries if wrapped */
1076 length = MONTHCAL_MonthLength(infoPtr->calendars[0].month.wMonth - 1,
1077 infoPtr->calendars[0].month.wYear);
1078 index = 0;
1079 while(st.wDay <= length)
1081 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1082 mask <<= 1;
1083 st.wDay++;
1086 /* draw next month */
1087 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1088 st.wDay = 1;
1089 MONTHCAL_GetNextMonth(&st);
1090 MONTHCAL_GetMaxDate(infoPtr, &st_max);
1091 mask = 1;
1092 index = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)-1;
1093 while(st.wDay <= st_max.wDay)
1095 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1096 mask <<= 1;
1097 st.wDay++;
1101 /* paint a calendar area */
1102 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
1104 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
1105 INT i, j, length;
1106 RECT r, fill_bk_rect;
1107 SYSTEMTIME st;
1108 WCHAR buf[80];
1109 HPEN old_pen;
1110 int mask;
1112 /* fill whole days area - from week days area to today note rectangle */
1113 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
1114 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
1115 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
1117 FillRect(hdc, &fill_bk_rect, infoPtr->brushes[BrushMonth]);
1119 /* draw line under day abbreviations */
1120 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1121 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
1122 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
1123 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
1124 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
1125 SelectObject(hdc, old_pen);
1127 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
1128 infoPtr->calendars[calIdx].weeknums.right;
1130 /* draw day abbreviations */
1131 SelectObject(hdc, infoPtr->hFont);
1132 SetBkColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
1133 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1134 /* rectangle to draw a single day abbreviation within */
1135 r = infoPtr->calendars[calIdx].wdays;
1136 r.right = r.left + infoPtr->width_increment;
1138 i = infoPtr->firstDay;
1139 for(j = 0; j < 7; j++) {
1140 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
1141 DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1142 OffsetRect(&r, infoPtr->width_increment, 0);
1145 /* draw current month */
1146 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1147 st = *date;
1148 st.wDay = 1;
1149 mask = 1;
1150 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1151 while(st.wDay <= length)
1153 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[calIdx+1] & mask, ps);
1154 mask <<= 1;
1155 st.wDay++;
1159 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1161 COLORREF old_text_clr, old_bk_clr;
1162 HFONT old_font;
1163 INT i;
1165 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1166 old_bk_clr = GetBkColor(hdc);
1167 old_font = GetCurrentObject(hdc, OBJ_FONT);
1169 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1171 RECT *title = &infoPtr->calendars[i].title;
1172 RECT r;
1174 /* draw title, redraw all its elements */
1175 if (IntersectRect(&r, &(ps->rcPaint), title))
1176 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1178 /* draw calendar area */
1179 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1180 if (IntersectRect(&r, &(ps->rcPaint), &r))
1181 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1183 /* week numbers */
1184 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1187 /* partially visible months */
1188 MONTHCAL_PaintLeadTrailMonths(infoPtr, hdc, ps);
1190 /* focus and today rectangle */
1191 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1193 /* today at the bottom left */
1194 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1196 /* navigation buttons */
1197 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_BACKWARD);
1198 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_FORWARD);
1200 /* restore context */
1201 SetBkColor(hdc, old_bk_clr);
1202 SelectObject(hdc, old_font);
1203 SetTextColor(hdc, old_text_clr);
1206 static LRESULT
1207 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, RECT *rect)
1209 TRACE("rect %p\n", rect);
1211 if(!rect) return FALSE;
1213 *rect = infoPtr->calendars[0].title;
1214 rect->bottom = infoPtr->calendars[0].days.bottom + infoPtr->todayrect.bottom -
1215 infoPtr->todayrect.top;
1217 AdjustWindowRect(rect, infoPtr->dwStyle, FALSE);
1219 /* minimal rectangle is zero based */
1220 OffsetRect(rect, -rect->left, -rect->top);
1222 TRACE("%s\n", wine_dbgstr_rect(rect));
1224 return TRUE;
1227 static COLORREF
1228 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, UINT index)
1230 TRACE("%p, %d\n", infoPtr, index);
1232 if (index > MCSC_TRAILINGTEXT) return -1;
1233 return infoPtr->colors[index];
1236 static LRESULT
1237 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, UINT index, COLORREF color)
1239 enum CachedBrush type;
1240 COLORREF prev;
1242 TRACE("%p, %d: color %08x\n", infoPtr, index, color);
1244 if (index > MCSC_TRAILINGTEXT) return -1;
1246 prev = infoPtr->colors[index];
1247 infoPtr->colors[index] = color;
1249 /* update cached brush */
1250 switch (index)
1252 case MCSC_BACKGROUND:
1253 type = BrushBackground;
1254 break;
1255 case MCSC_TITLEBK:
1256 type = BrushTitle;
1257 break;
1258 case MCSC_MONTHBK:
1259 type = BrushMonth;
1260 break;
1261 default:
1262 type = BrushLast;
1265 if (type != BrushLast)
1267 DeleteObject(infoPtr->brushes[type]);
1268 infoPtr->brushes[type] = CreateSolidBrush(color);
1271 /* update cached pen */
1272 if (index == MCSC_TEXT)
1274 DeleteObject(infoPtr->pens[PenText]);
1275 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[index]);
1278 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND ? TRUE : FALSE);
1279 return prev;
1282 static LRESULT
1283 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1285 TRACE("\n");
1287 if(infoPtr->delta)
1288 return infoPtr->delta;
1289 else
1290 return infoPtr->visible;
1294 static LRESULT
1295 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1297 INT prev = infoPtr->delta;
1299 TRACE("delta %d\n", delta);
1301 infoPtr->delta = delta;
1302 return prev;
1306 static inline LRESULT
1307 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1309 int day;
1311 /* convert from SYSTEMTIME to locale format */
1312 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1314 return MAKELONG(day, infoPtr->firstDaySet);
1318 /* Sets the first day of the week that will appear in the control
1321 * PARAMETERS:
1322 * [I] infoPtr : valid pointer to control data
1323 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1326 * RETURN VALUE:
1327 * Low word contains previous first day,
1328 * high word indicates was first day forced with this message before or is
1329 * locale defined (TRUE - was forced, FALSE - wasn't).
1331 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1332 * FIXME: we need more error checking here
1334 static LRESULT
1335 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1337 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1338 int new_day;
1340 TRACE("%d\n", day);
1342 if(day == -1)
1344 WCHAR buf[80];
1346 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1347 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1349 new_day = atoiW(buf);
1351 infoPtr->firstDaySet = FALSE;
1353 else if(day >= 7)
1355 new_day = 6; /* max first day allowed */
1356 infoPtr->firstDaySet = TRUE;
1358 else
1360 /* Native behaviour for that case is broken: invalid date number >31
1361 got displayed at (0,0) position, current month starts always from
1362 (1,0) position. Should be implemented here as well only if there's
1363 nothing else to do. */
1364 if (day < -1)
1365 FIXME("No bug compatibility for day=%d\n", day);
1367 new_day = day;
1368 infoPtr->firstDaySet = TRUE;
1371 /* convert from locale to SYSTEMTIME format */
1372 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1374 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1376 return prev;
1379 static LRESULT
1380 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1382 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1385 static LRESULT
1386 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1388 FILETIME ft_min, ft_max;
1390 TRACE("%x %p\n", limits, range);
1392 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1393 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1394 return FALSE;
1396 if (limits & GDTR_MIN)
1398 if (!MONTHCAL_ValidateTime(&range[0]))
1399 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1401 infoPtr->minDate = range[0];
1402 infoPtr->rangeValid |= GDTR_MIN;
1404 if (limits & GDTR_MAX)
1406 if (!MONTHCAL_ValidateTime(&range[1]))
1407 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1409 infoPtr->maxDate = range[1];
1410 infoPtr->rangeValid |= GDTR_MAX;
1413 /* Only one limit set - we are done */
1414 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1415 return TRUE;
1417 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1418 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1420 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1422 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1424 /* Native swaps limits only when both limits are being set. */
1425 SYSTEMTIME st_tmp = infoPtr->minDate;
1426 infoPtr->minDate = infoPtr->maxDate;
1427 infoPtr->maxDate = st_tmp;
1429 else
1431 /* reset the other limit */
1432 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1433 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1434 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1438 return TRUE;
1442 static LRESULT
1443 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1445 TRACE("%p\n", range);
1447 if(!range) return FALSE;
1449 range[1] = infoPtr->maxDate;
1450 range[0] = infoPtr->minDate;
1452 return infoPtr->rangeValid;
1456 static LRESULT
1457 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1459 TRACE("%p %d %p\n", infoPtr, months, states);
1461 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return 0;
1462 if (months != MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)) return 0;
1464 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1466 return 1;
1469 static LRESULT
1470 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1472 TRACE("%p\n", curSel);
1473 if(!curSel) return FALSE;
1474 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1476 *curSel = infoPtr->minSel;
1477 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1478 return TRUE;
1481 static LRESULT
1482 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1484 SYSTEMTIME prev = infoPtr->minSel;
1485 INT diff;
1486 WORD day;
1488 TRACE("%p\n", curSel);
1489 if(!curSel) return FALSE;
1490 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1492 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1493 /* exit earlier if selection equals current */
1494 if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1496 if(!MONTHCAL_IsDateInValidRange(infoPtr, curSel, FALSE)) return FALSE;
1498 /* scroll calendars only if we have to */
1499 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, curSel);
1500 if (diff <= 0)
1502 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, curSel);
1503 if (diff > 0) diff = 0;
1506 if (diff != 0)
1508 INT i;
1510 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1511 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1514 infoPtr->minSel = *curSel;
1515 infoPtr->maxSel = *curSel;
1517 /* if selection is still in current month, reduce rectangle */
1518 day = prev.wDay;
1519 prev.wDay = curSel->wDay;
1520 if (MONTHCAL_IsDateEqual(&prev, curSel))
1522 RECT r_prev, r_new;
1524 prev.wDay = day;
1525 MONTHCAL_GetDayRect(infoPtr, &prev, &r_prev, -1);
1526 MONTHCAL_GetDayRect(infoPtr, curSel, &r_new, -1);
1528 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1529 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1531 else
1532 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1534 return TRUE;
1538 static LRESULT
1539 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1541 return infoPtr->maxSelCount;
1545 static LRESULT
1546 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1548 TRACE("%d\n", max);
1550 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1551 if(max <= 0) return FALSE;
1553 infoPtr->maxSelCount = max;
1555 return TRUE;
1559 static LRESULT
1560 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1562 TRACE("%p\n", range);
1564 if(!range) return FALSE;
1566 if(infoPtr->dwStyle & MCS_MULTISELECT)
1568 range[1] = infoPtr->maxSel;
1569 range[0] = infoPtr->minSel;
1570 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1571 return TRUE;
1574 return FALSE;
1578 static LRESULT
1579 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1581 SYSTEMTIME old_range[2];
1582 INT diff;
1584 TRACE("%p\n", range);
1586 if(!range || !(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1588 /* adjust timestamps */
1589 if(!MONTHCAL_ValidateTime(&range[0])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1590 if(!MONTHCAL_ValidateTime(&range[1])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1592 /* maximum range exceeded */
1593 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1595 old_range[0] = infoPtr->minSel;
1596 old_range[1] = infoPtr->maxSel;
1598 /* swap if min > max */
1599 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1601 infoPtr->minSel = range[0];
1602 infoPtr->maxSel = range[1];
1604 else
1606 infoPtr->minSel = range[1];
1607 infoPtr->maxSel = range[0];
1610 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, &infoPtr->maxSel);
1611 if (diff < 0)
1613 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, &infoPtr->maxSel);
1614 if (diff > 0) diff = 0;
1617 if (diff != 0)
1619 INT i;
1621 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1622 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1625 /* update day of week */
1626 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1627 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1629 /* redraw if bounds changed */
1630 /* FIXME: no actual need to redraw everything */
1631 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1632 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1634 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1637 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1638 return TRUE;
1642 static LRESULT
1643 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1645 TRACE("%p\n", today);
1647 if(!today) return FALSE;
1648 *today = infoPtr->todaysDate;
1649 return TRUE;
1652 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1654 * RETURN VALUE
1656 * TRUE - today date changed
1657 * FALSE - today date isn't changed
1659 static BOOL
1660 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1662 RECT new_r, old_r;
1664 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1666 MONTHCAL_GetDayRect(infoPtr, &infoPtr->todaysDate, &old_r, -1);
1667 MONTHCAL_GetDayRect(infoPtr, today, &new_r, -1);
1669 infoPtr->todaysDate = *today;
1671 /* only two days need redrawing */
1672 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1673 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1674 return TRUE;
1677 /* MCM_SETTODAT handler */
1678 static LRESULT
1679 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1681 TRACE("%p\n", today);
1683 if(!today) return FALSE;
1685 /* remember if date was set successfully */
1686 if(MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1688 return TRUE;
1691 /* returns calendar index containing specified point, or -1 if it's background */
1692 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1694 RECT r;
1695 INT i;
1697 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1699 /* whole bounding rectangle allows some optimization to compute */
1700 r.left = infoPtr->calendars[i].title.left;
1701 r.top = infoPtr->calendars[i].title.top;
1702 r.bottom = infoPtr->calendars[i].days.bottom;
1703 r.right = infoPtr->calendars[i].days.right;
1705 if (PtInRect(&r, *pt)) return i;
1708 return -1;
1711 static inline UINT fill_hittest_info(const MCHITTESTINFO *src, MCHITTESTINFO *dest)
1713 dest->uHit = src->uHit;
1714 dest->st = src->st;
1716 if (dest->cbSize == sizeof(MCHITTESTINFO))
1717 memcpy(&dest->rc, &src->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1719 return src->uHit;
1722 static LRESULT
1723 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1725 MCHITTESTINFO htinfo;
1726 SYSTEMTIME *ht_month;
1727 INT day, calIdx;
1729 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1731 htinfo.st = st_null;
1733 /* we should preserve passed fields if hit area doesn't need them */
1734 if (lpht->cbSize == sizeof(MCHITTESTINFO))
1735 memcpy(&htinfo.rc, &lpht->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1737 /* Comment in for debugging...
1738 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,
1739 infoPtr->wdays.left, infoPtr->wdays.right,
1740 infoPtr->wdays.top, infoPtr->wdays.bottom,
1741 infoPtr->days.left, infoPtr->days.right,
1742 infoPtr->days.top, infoPtr->days.bottom,
1743 infoPtr->todayrect.left, infoPtr->todayrect.right,
1744 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1745 infoPtr->weeknums.left, infoPtr->weeknums.right,
1746 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1749 /* guess in what calendar we are */
1750 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1751 if (calIdx == -1)
1753 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1755 htinfo.uHit = MCHT_TODAYLINK;
1756 htinfo.rc = infoPtr->todayrect;
1758 else
1759 /* outside of calendar area? What's left must be background :-) */
1760 htinfo.uHit = MCHT_CALENDARBK;
1762 return fill_hittest_info(&htinfo, lpht);
1765 /* are we in the header? */
1766 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1767 /* FIXME: buttons hittesting could be optimized cause maximum
1768 two calendars have buttons */
1769 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1771 htinfo.uHit = MCHT_TITLEBTNPREV;
1772 htinfo.rc = infoPtr->titlebtnprev;
1774 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1776 htinfo.uHit = MCHT_TITLEBTNNEXT;
1777 htinfo.rc = infoPtr->titlebtnnext;
1779 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1781 htinfo.uHit = MCHT_TITLEMONTH;
1782 htinfo.rc = infoPtr->calendars[calIdx].titlemonth;
1783 htinfo.iOffset = calIdx;
1785 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1787 htinfo.uHit = MCHT_TITLEYEAR;
1788 htinfo.rc = infoPtr->calendars[calIdx].titleyear;
1789 htinfo.iOffset = calIdx;
1791 else
1793 htinfo.uHit = MCHT_TITLE;
1794 htinfo.rc = infoPtr->calendars[calIdx].title;
1795 htinfo.iOffset = calIdx;
1798 return fill_hittest_info(&htinfo, lpht);
1801 ht_month = &infoPtr->calendars[calIdx].month;
1802 /* days area (including week days and week numbers) */
1803 day = MONTHCAL_GetDayFromPos(infoPtr, lpht->pt, calIdx);
1804 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1806 htinfo.uHit = MCHT_CALENDARDAY;
1807 htinfo.iOffset = calIdx;
1808 htinfo.st.wYear = ht_month->wYear;
1809 htinfo.st.wMonth = (day < 1) ? ht_month->wMonth -1 : ht_month->wMonth;
1810 htinfo.st.wDay = (day < 1) ?
1811 MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day : day;
1813 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1815 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1817 htinfo.uHit = MCHT_CALENDARWEEKNUM;
1818 htinfo.st.wYear = ht_month->wYear;
1819 htinfo.iOffset = calIdx;
1821 if (day < 1)
1823 htinfo.st.wMonth = ht_month->wMonth - 1;
1824 htinfo.st.wDay = MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day;
1826 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1828 htinfo.st.wMonth = ht_month->wMonth + 1;
1829 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1831 else
1833 htinfo.st.wMonth = ht_month->wMonth;
1834 htinfo.st.wDay = day;
1837 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1839 htinfo.iOffset = calIdx;
1840 htinfo.st.wYear = ht_month->wYear;
1841 htinfo.st.wMonth = ht_month->wMonth;
1842 /* previous month only valid for first calendar */
1843 if (day < 1 && calIdx == 0)
1845 htinfo.uHit = MCHT_CALENDARDATEPREV;
1846 MONTHCAL_GetPrevMonth(&htinfo.st);
1847 htinfo.st.wDay = MONTHCAL_MonthLength(htinfo.st.wMonth, htinfo.st.wYear) + day;
1849 /* next month only valid for last calendar */
1850 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear) &&
1851 calIdx == MONTHCAL_GetCalCount(infoPtr)-1)
1853 htinfo.uHit = MCHT_CALENDARDATENEXT;
1854 MONTHCAL_GetNextMonth(&htinfo.st);
1855 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1857 /* multiple calendars case - blank areas for previous/next month */
1858 else if (day < 1 || day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1860 htinfo.uHit = MCHT_CALENDARBK;
1862 else
1864 htinfo.uHit = MCHT_CALENDARDATE;
1865 htinfo.st.wDay = day;
1868 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1869 MONTHCAL_GetDayRectI(infoPtr, &htinfo.rc, htinfo.iCol, htinfo.iRow, calIdx);
1870 /* always update day of week */
1871 MONTHCAL_CalculateDayOfWeek(&htinfo.st, TRUE);
1874 return fill_hittest_info(&htinfo, lpht);
1877 /* MCN_GETDAYSTATE notification helper */
1878 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1880 if(infoPtr->dwStyle & MCS_DAYSTATE) {
1881 NMDAYSTATE nmds;
1883 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1884 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1885 nmds.nmhdr.code = MCN_GETDAYSTATE;
1886 nmds.cDayState = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0);
1887 nmds.prgDayState = Alloc(nmds.cDayState * sizeof(MONTHDAYSTATE));
1889 MONTHCAL_GetMinDate(infoPtr, &nmds.stStart);
1890 nmds.stStart.wDay = 1;
1892 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1893 memcpy(infoPtr->monthdayState, nmds.prgDayState,
1894 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
1896 Free(nmds.prgDayState);
1900 /* no valid range check performed */
1901 static void MONTHCAL_Scroll(MONTHCAL_INFO *infoPtr, INT delta)
1903 INT i, selIdx = -1;
1905 for(i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1907 /* save selection position to shift it later */
1908 if (selIdx == -1 && MONTHCAL_CompareMonths(&infoPtr->minSel, &infoPtr->calendars[i].month) == 0)
1909 selIdx = i;
1911 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, delta);
1914 /* selection is always shifted to first calendar */
1915 if(infoPtr->dwStyle & MCS_MULTISELECT)
1917 SYSTEMTIME range[2];
1919 MONTHCAL_GetSelRange(infoPtr, range);
1920 MONTHCAL_GetMonth(&range[0], delta - selIdx);
1921 MONTHCAL_GetMonth(&range[1], delta - selIdx);
1922 MONTHCAL_SetSelRange(infoPtr, range);
1924 else
1926 SYSTEMTIME st = infoPtr->minSel;
1928 MONTHCAL_GetMonth(&st, delta - selIdx);
1929 MONTHCAL_SetCurSel(infoPtr, &st);
1933 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
1935 INT delta = infoPtr->delta ? infoPtr->delta : MONTHCAL_GetCalCount(infoPtr);
1936 SYSTEMTIME st;
1938 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
1940 /* check if change allowed by range set */
1941 if(direction == DIRECTION_BACKWARD)
1943 st = infoPtr->calendars[0].month;
1944 MONTHCAL_GetMonth(&st, -delta);
1946 else
1948 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1949 MONTHCAL_GetMonth(&st, delta);
1952 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
1954 MONTHCAL_Scroll(infoPtr, direction == DIRECTION_BACKWARD ? -delta : delta);
1955 MONTHCAL_NotifyDayState(infoPtr);
1956 MONTHCAL_NotifySelectionChange(infoPtr);
1959 static LRESULT
1960 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1962 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1963 HMENU hMenu;
1964 POINT menupoint;
1965 WCHAR buf[32];
1967 hMenu = CreatePopupMenu();
1968 if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1970 WARN("Can't load resource\n");
1971 strcpyW(buf, todayW);
1973 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1974 menupoint.x = (short)LOWORD(lParam);
1975 menupoint.y = (short)HIWORD(lParam);
1976 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1977 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
1978 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1980 infoPtr->calendars[0].month = infoPtr->todaysDate;
1981 infoPtr->minSel = infoPtr->todaysDate;
1982 infoPtr->maxSel = infoPtr->todaysDate;
1983 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1986 return 0;
1989 /***
1990 * DESCRIPTION:
1991 * Subclassed edit control windproc function
1993 * PARAMETER(S):
1994 * [I] hwnd : the edit window handle
1995 * [I] uMsg : the message that is to be processed
1996 * [I] wParam : first message parameter
1997 * [I] lParam : second message parameter
2000 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2002 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
2004 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
2005 hwnd, uMsg, wParam, lParam);
2007 switch (uMsg)
2009 case WM_GETDLGCODE:
2010 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
2012 case WM_DESTROY:
2014 WNDPROC editProc = infoPtr->EditWndProc;
2015 infoPtr->EditWndProc = NULL;
2016 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
2017 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
2020 case WM_KILLFOCUS:
2021 break;
2023 case WM_KEYDOWN:
2024 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
2025 break;
2027 default:
2028 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
2031 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
2032 SendMessageW(hwnd, WM_CLOSE, 0, 0);
2033 return 0;
2036 /* creates updown control and edit box */
2037 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr, INT calIdx)
2039 RECT *rc = &infoPtr->calendars[calIdx].titleyear;
2040 RECT *title = &infoPtr->calendars[calIdx].title;
2042 infoPtr->hWndYearEdit =
2043 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
2044 rc->left + 3, (title->bottom + title->top - infoPtr->textHeight) / 2,
2045 rc->right - rc->left + 4,
2046 infoPtr->textHeight, infoPtr->hwndSelf,
2047 NULL, NULL, NULL);
2049 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
2051 infoPtr->hWndYearUpDown =
2052 CreateWindowExW(0, UPDOWN_CLASSW, 0,
2053 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
2054 rc->right + 7, (title->bottom + title->top - infoPtr->textHeight) / 2,
2055 18, infoPtr->textHeight, infoPtr->hwndSelf,
2056 NULL, NULL, NULL);
2058 /* attach edit box */
2059 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
2060 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
2061 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
2062 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->calendars[calIdx].month.wYear);
2064 /* subclass edit box */
2065 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
2066 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
2068 SetFocus(infoPtr->hWndYearEdit);
2071 static LRESULT
2072 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2074 MCHITTESTINFO ht;
2075 DWORD hit;
2077 /* Actually we don't need input focus for calendar, this is used to kill
2078 year updown and its buddy edit box */
2079 if (IsWindow(infoPtr->hWndYearUpDown))
2081 SetFocus(infoPtr->hwndSelf);
2082 return 0;
2085 SetCapture(infoPtr->hwndSelf);
2087 ht.cbSize = sizeof(MCHITTESTINFO);
2088 ht.pt.x = (short)LOWORD(lParam);
2089 ht.pt.y = (short)HIWORD(lParam);
2091 hit = MONTHCAL_HitTest(infoPtr, &ht);
2093 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
2095 switch(hit)
2097 case MCHT_TITLEBTNNEXT:
2098 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2099 infoPtr->status = MC_NEXTPRESSED;
2100 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2101 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2102 return 0;
2104 case MCHT_TITLEBTNPREV:
2105 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2106 infoPtr->status = MC_PREVPRESSED;
2107 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2108 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2109 return 0;
2111 case MCHT_TITLEMONTH:
2113 HMENU hMenu = CreatePopupMenu();
2114 WCHAR buf[32];
2115 POINT menupoint;
2116 INT i;
2118 for (i = 0; i < 12; i++)
2120 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
2121 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
2123 menupoint.x = ht.pt.x;
2124 menupoint.y = ht.pt.y;
2125 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2126 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
2127 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
2129 if ((i > 0) && (i < 13) && infoPtr->calendars[ht.iOffset].month.wMonth != i)
2131 INT delta = i - infoPtr->calendars[ht.iOffset].month.wMonth;
2132 SYSTEMTIME st;
2134 /* check if change allowed by range set */
2135 st = delta < 0 ? infoPtr->calendars[0].month :
2136 infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2137 MONTHCAL_GetMonth(&st, delta);
2139 if (MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE))
2141 MONTHCAL_Scroll(infoPtr, delta);
2142 MONTHCAL_NotifyDayState(infoPtr);
2143 MONTHCAL_NotifySelectionChange(infoPtr);
2144 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2147 return 0;
2149 case MCHT_TITLEYEAR:
2151 MONTHCAL_EditYear(infoPtr, ht.iOffset);
2152 return 0;
2154 case MCHT_TODAYLINK:
2156 infoPtr->calendars[0].month = infoPtr->todaysDate;
2157 infoPtr->minSel = infoPtr->todaysDate;
2158 infoPtr->maxSel = infoPtr->todaysDate;
2159 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2161 MONTHCAL_NotifySelectionChange(infoPtr);
2162 MONTHCAL_NotifySelect(infoPtr);
2163 return 0;
2165 case MCHT_CALENDARDATENEXT:
2166 case MCHT_CALENDARDATEPREV:
2167 case MCHT_CALENDARDATE:
2169 SYSTEMTIME st[2];
2171 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2173 st[0] = st[1] = ht.st;
2174 /* clear selection range */
2175 MONTHCAL_SetSelRange(infoPtr, st);
2177 infoPtr->status = MC_SEL_LBUTDOWN;
2178 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
2179 return 0;
2183 return 1;
2187 static LRESULT
2188 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2190 NMHDR nmhdr;
2191 MCHITTESTINFO ht;
2192 DWORD hit;
2194 TRACE("\n");
2196 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2197 RECT *r;
2199 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2200 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2201 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2203 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2206 ReleaseCapture();
2208 /* always send NM_RELEASEDCAPTURE notification */
2209 nmhdr.hwndFrom = infoPtr->hwndSelf;
2210 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2211 nmhdr.code = NM_RELEASEDCAPTURE;
2212 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2214 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2216 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2218 ht.cbSize = sizeof(MCHITTESTINFO);
2219 ht.pt.x = (short)LOWORD(lParam);
2220 ht.pt.y = (short)HIWORD(lParam);
2221 hit = MONTHCAL_HitTest(infoPtr, &ht);
2223 infoPtr->status = MC_SEL_LBUTUP;
2224 MONTHCAL_SetDayFocus(infoPtr, NULL);
2226 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2228 SYSTEMTIME sel = infoPtr->minSel;
2230 /* will be invalidated here */
2231 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2233 /* send MCN_SELCHANGE only if new date selected */
2234 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2235 MONTHCAL_NotifySelectionChange(infoPtr);
2237 MONTHCAL_NotifySelect(infoPtr);
2240 return 0;
2244 static LRESULT
2245 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2247 TRACE("%ld\n", id);
2249 switch(id) {
2250 case MC_PREVNEXTMONTHTIMER:
2251 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2252 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2253 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2254 break;
2255 case MC_TODAYUPDATETIMER:
2257 SYSTEMTIME st;
2259 if(infoPtr->todaySet) return 0;
2261 GetLocalTime(&st);
2262 MONTHCAL_UpdateToday(infoPtr, &st);
2264 /* notification sent anyway */
2265 MONTHCAL_NotifySelectionChange(infoPtr);
2267 return 0;
2269 default:
2270 ERR("got unknown timer %ld\n", id);
2271 break;
2274 return 0;
2278 static LRESULT
2279 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2281 MCHITTESTINFO ht;
2282 SYSTEMTIME st_ht;
2283 INT hit;
2284 RECT r;
2286 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2288 ht.cbSize = sizeof(MCHITTESTINFO);
2289 ht.pt.x = (short)LOWORD(lParam);
2290 ht.pt.y = (short)HIWORD(lParam);
2291 ht.iOffset = -1;
2293 hit = MONTHCAL_HitTest(infoPtr, &ht);
2295 /* not on the calendar date numbers? bail out */
2296 TRACE("hit:%x\n",hit);
2297 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2299 MONTHCAL_SetDayFocus(infoPtr, NULL);
2300 return 0;
2303 st_ht = ht.st;
2305 /* if pointer is over focused day still there's nothing to do */
2306 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2308 MONTHCAL_GetDayRect(infoPtr, &ht.st, &r, ht.iOffset);
2310 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2311 SYSTEMTIME st[2];
2313 MONTHCAL_GetSelRange(infoPtr, st);
2315 /* If we're still at the first selected date and range is empty, return.
2316 If range isn't empty we should change range to a single firstSel */
2317 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2318 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2320 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2322 st[0] = infoPtr->firstSel;
2323 /* we should overwrite timestamp here */
2324 MONTHCAL_CopyDate(&st_ht, &st[1]);
2326 /* bounds will be swapped here if needed */
2327 MONTHCAL_SetSelRange(infoPtr, st);
2329 return 0;
2332 done:
2334 /* FIXME: this should specify a rectangle containing only the days that changed
2335 using InvalidateRect */
2336 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2338 return 0;
2342 static LRESULT
2343 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2345 HDC hdc;
2346 PAINTSTRUCT ps;
2348 if (hdc_paint)
2350 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2351 hdc = hdc_paint;
2353 else
2354 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2356 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2357 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2358 return 0;
2361 static LRESULT
2362 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2364 RECT rc;
2366 if (!GetClipBox(hdc, &rc)) return FALSE;
2368 FillRect(hdc, &rc, infoPtr->brushes[BrushBackground]);
2370 return TRUE;
2373 static LRESULT
2374 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2376 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2378 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2379 return 0;
2381 if (options & PRF_ERASEBKGND)
2382 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2384 if (options & PRF_CLIENT)
2385 MONTHCAL_Paint(infoPtr, hdc);
2387 return 0;
2390 static LRESULT
2391 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2393 TRACE("\n");
2395 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2397 return 0;
2400 /* sets the size information */
2401 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2403 static const WCHAR O0W[] = { '0','0',0 };
2404 RECT *title=&infoPtr->calendars[0].title;
2405 RECT *prev=&infoPtr->titlebtnprev;
2406 RECT *next=&infoPtr->titlebtnnext;
2407 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2408 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2409 RECT *wdays=&infoPtr->calendars[0].wdays;
2410 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2411 RECT *days=&infoPtr->calendars[0].days;
2412 RECT *todayrect=&infoPtr->todayrect;
2414 INT xdiv, dx, dy, i, j, x, y, c_dx, c_dy;
2415 WCHAR buff[80];
2416 TEXTMETRICW tm;
2417 SIZE size, sz;
2418 RECT client;
2419 HFONT font;
2420 HDC hdc;
2422 GetClientRect(infoPtr->hwndSelf, &client);
2424 hdc = GetDC(infoPtr->hwndSelf);
2425 font = SelectObject(hdc, infoPtr->hFont);
2427 /* get the height and width of each day's text */
2428 GetTextMetricsW(hdc, &tm);
2429 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2431 /* find largest abbreviated day name for current locale */
2432 size.cx = sz.cx = 0;
2433 for (i = 0; i < 7; i++)
2435 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + i,
2436 buff, countof(buff)))
2438 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2439 if (sz.cx > size.cx) size.cx = sz.cx;
2441 else /* locale independent fallback on failure */
2443 static const WCHAR SunW[] = { 'S','u','n',0 };
2445 GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2446 break;
2450 infoPtr->textWidth = size.cx + 2;
2452 /* recalculate the height and width increments and offsets */
2453 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2455 /* restore the originally selected font */
2456 SelectObject(hdc, font);
2457 ReleaseDC(infoPtr->hwndSelf, hdc);
2459 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2461 infoPtr->width_increment = size.cx * 2 + 4;
2462 infoPtr->height_increment = infoPtr->textHeight;
2464 /* calculate title area */
2465 title->top = 0;
2466 title->bottom = 3 * infoPtr->height_increment / 2;
2467 title->left = 0;
2468 title->right = infoPtr->width_increment * xdiv;
2470 /* set the dimensions of the next and previous buttons and center */
2471 /* the month text vertically */
2472 prev->top = next->top = title->top + 4;
2473 prev->bottom = next->bottom = title->bottom - 4;
2474 prev->left = title->left + 4;
2475 prev->right = prev->left + (title->bottom - title->top);
2476 next->right = title->right - 4;
2477 next->left = next->right - (title->bottom - title->top);
2479 /* titlemonth->left and right change based upon the current month
2480 and are recalculated in refresh as the current month may change
2481 without the control being resized */
2482 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2483 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2485 /* week numbers */
2486 weeknumrect->left = 0;
2487 weeknumrect->right = infoPtr->dwStyle & MCS_WEEKNUMBERS ? prev->right : 0;
2489 /* days abbreviated names */
2490 wdays->left = days->left = weeknumrect->right;
2491 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2492 wdays->top = title->bottom;
2493 wdays->bottom = wdays->top + infoPtr->height_increment;
2495 days->top = weeknumrect->top = wdays->bottom;
2496 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2498 todayrect->left = 0;
2499 todayrect->right = title->right;
2500 todayrect->top = days->bottom;
2501 todayrect->bottom = days->bottom + infoPtr->height_increment;
2503 /* compute calendar count, update all calendars */
2504 x = (client.right + MC_CALENDAR_PADDING) / (title->right - title->left + MC_CALENDAR_PADDING);
2505 /* today label affects whole height */
2506 if (infoPtr->dwStyle & MCS_NOTODAY)
2507 y = (client.bottom + MC_CALENDAR_PADDING) / (days->bottom - title->top + MC_CALENDAR_PADDING);
2508 else
2509 y = (client.bottom - todayrect->bottom + todayrect->top + MC_CALENDAR_PADDING) /
2510 (days->bottom - title->top + MC_CALENDAR_PADDING);
2512 /* TODO: ensure that count is properly adjusted to fit 12 months constraint */
2513 if (x == 0) x = 1;
2514 if (y == 0) y = 1;
2516 if (x*y != MONTHCAL_GetCalCount(infoPtr))
2518 infoPtr->dim.cx = x;
2519 infoPtr->dim.cy = y;
2520 infoPtr->calendars = ReAlloc(infoPtr->calendars, MONTHCAL_GetCalCount(infoPtr)*sizeof(CALENDAR_INFO));
2522 infoPtr->monthdayState = ReAlloc(infoPtr->monthdayState,
2523 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
2524 MONTHCAL_NotifyDayState(infoPtr);
2527 for (i = 1; i < MONTHCAL_GetCalCount(infoPtr); i++)
2529 /* set months */
2530 infoPtr->calendars[i] = infoPtr->calendars[0];
2531 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, i);
2534 /* offset all rectangles to center in client area */
2535 c_dx = (client.right - x * title->right - MC_CALENDAR_PADDING * (x-1)) / 2;
2536 c_dy = (client.bottom - y * todayrect->bottom - MC_CALENDAR_PADDING * (y-1)) / 2;
2538 /* if calendar doesn't fit client area show it at left/top bounds */
2539 if (title->left + c_dx < 0) c_dx = 0;
2540 if (title->top + c_dy < 0) c_dy = 0;
2542 for (i = 0; i < y; i++)
2544 for (j = 0; j < x; j++)
2546 dx = j*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx;
2547 dy = i*(days->bottom - title->top + MC_CALENDAR_PADDING) + c_dy;
2549 OffsetRect(&infoPtr->calendars[i*x+j].title, dx, dy);
2550 OffsetRect(&infoPtr->calendars[i*x+j].titlemonth, dx, dy);
2551 OffsetRect(&infoPtr->calendars[i*x+j].titleyear, dx, dy);
2552 OffsetRect(&infoPtr->calendars[i*x+j].wdays, dx, dy);
2553 OffsetRect(&infoPtr->calendars[i*x+j].weeknums, dx, dy);
2554 OffsetRect(&infoPtr->calendars[i*x+j].days, dx, dy);
2558 OffsetRect(prev, c_dx, c_dy);
2559 OffsetRect(next, (x-1)*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx, c_dy);
2561 i = infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx;
2562 todayrect->left = infoPtr->calendars[i].title.left;
2563 todayrect->right = infoPtr->calendars[i].title.right;
2564 todayrect->top = infoPtr->calendars[i].days.bottom;
2565 todayrect->bottom = infoPtr->calendars[i].days.bottom + infoPtr->height_increment;
2567 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2568 infoPtr->width_increment,infoPtr->height_increment,
2569 wine_dbgstr_rect(&client),
2570 wine_dbgstr_rect(title),
2571 wine_dbgstr_rect(wdays),
2572 wine_dbgstr_rect(days),
2573 wine_dbgstr_rect(todayrect));
2576 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2578 TRACE("(width=%d, height=%d)\n", Width, Height);
2580 MONTHCAL_UpdateSize(infoPtr);
2581 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2583 return 0;
2586 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2588 return (LRESULT)infoPtr->hFont;
2591 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2593 HFONT hOldFont;
2594 LOGFONTW lf;
2596 if (!hFont) return 0;
2598 hOldFont = infoPtr->hFont;
2599 infoPtr->hFont = hFont;
2601 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2602 lf.lfWeight = FW_BOLD;
2603 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2605 MONTHCAL_UpdateSize(infoPtr);
2607 if (redraw)
2608 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2610 return (LRESULT)hOldFont;
2613 /* update theme after a WM_THEMECHANGED message */
2614 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2616 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2617 CloseThemeData (theme);
2618 OpenThemeData (infoPtr->hwndSelf, themeClass);
2619 return 0;
2622 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2623 const STYLESTRUCT *lpss)
2625 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2626 wStyleType, lpss->styleOld, lpss->styleNew);
2628 if (wStyleType != GWL_STYLE) return 0;
2630 infoPtr->dwStyle = lpss->styleNew;
2632 /* make room for week numbers */
2633 if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2634 MONTHCAL_UpdateSize(infoPtr);
2636 return 0;
2639 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2640 STYLESTRUCT *lpss)
2642 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2643 wStyleType, lpss->styleOld, lpss->styleNew);
2645 /* block MCS_MULTISELECT change */
2646 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2648 if (lpss->styleOld & MCS_MULTISELECT)
2649 lpss->styleNew |= MCS_MULTISELECT;
2650 else
2651 lpss->styleNew &= ~MCS_MULTISELECT;
2654 /* block MCS_DAYSTATE change */
2655 if ((lpss->styleNew ^ lpss->styleOld) & MCS_DAYSTATE)
2657 if (lpss->styleOld & MCS_DAYSTATE)
2658 lpss->styleNew |= MCS_DAYSTATE;
2659 else
2660 lpss->styleNew &= ~MCS_DAYSTATE;
2663 return 0;
2666 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2667 static LRESULT
2668 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2670 MONTHCAL_INFO *infoPtr;
2672 /* allocate memory for info structure */
2673 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2674 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2676 if (infoPtr == NULL) {
2677 ERR("could not allocate info memory!\n");
2678 return 0;
2681 infoPtr->hwndSelf = hwnd;
2682 infoPtr->hwndNotify = lpcs->hwndParent;
2683 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2684 infoPtr->dim.cx = infoPtr->dim.cy = 1;
2685 infoPtr->calendars = Alloc(sizeof(CALENDAR_INFO));
2686 if (!infoPtr->calendars) goto fail;
2687 infoPtr->monthdayState = Alloc(3*sizeof(MONTHDAYSTATE));
2688 if (!infoPtr->monthdayState) goto fail;
2690 /* initialize info structure */
2691 /* FIXME: calculate systemtime ->> localtime(subtract timezoneinfo) */
2693 GetLocalTime(&infoPtr->todaysDate);
2694 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2696 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2698 infoPtr->colors[MCSC_BACKGROUND] = comctl32_color.clrWindow;
2699 infoPtr->colors[MCSC_TEXT] = comctl32_color.clrWindowText;
2700 infoPtr->colors[MCSC_TITLEBK] = comctl32_color.clrActiveCaption;
2701 infoPtr->colors[MCSC_TITLETEXT] = comctl32_color.clrWindow;
2702 infoPtr->colors[MCSC_MONTHBK] = comctl32_color.clrWindow;
2703 infoPtr->colors[MCSC_TRAILINGTEXT] = comctl32_color.clrGrayText;
2705 infoPtr->brushes[BrushBackground] = CreateSolidBrush(infoPtr->colors[MCSC_BACKGROUND]);
2706 infoPtr->brushes[BrushTitle] = CreateSolidBrush(infoPtr->colors[MCSC_TITLEBK]);
2707 infoPtr->brushes[BrushMonth] = CreateSolidBrush(infoPtr->colors[MCSC_MONTHBK]);
2709 infoPtr->pens[PenRed] = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
2710 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[MCSC_TEXT]);
2712 infoPtr->minSel = infoPtr->todaysDate;
2713 infoPtr->maxSel = infoPtr->todaysDate;
2714 infoPtr->calendars[0].month = infoPtr->todaysDate;
2715 infoPtr->isUnicode = TRUE;
2717 /* setup control layout and day state data */
2718 MONTHCAL_UpdateSize(infoPtr);
2720 /* today auto update timer, to be freed only on control destruction */
2721 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2723 OpenThemeData (infoPtr->hwndSelf, themeClass);
2725 return 0;
2727 fail:
2728 Free(infoPtr->monthdayState);
2729 Free(infoPtr->calendars);
2730 Free(infoPtr);
2731 return 0;
2734 static LRESULT
2735 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2737 INT i;
2739 /* free month calendar info data */
2740 Free(infoPtr->monthdayState);
2741 Free(infoPtr->calendars);
2742 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2744 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2746 for (i = 0; i < BrushLast; i++) DeleteObject(infoPtr->brushes[i]);
2747 for (i = 0; i < PenLast; i++) DeleteObject(infoPtr->pens[i]);
2749 Free(infoPtr);
2750 return 0;
2754 * Handler for WM_NOTIFY messages
2756 static LRESULT
2757 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2759 /* notification from year edit updown */
2760 if (hdr->code == UDN_DELTAPOS)
2762 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2764 if (hdr->hwndFrom == infoPtr->hWndYearUpDown && nmud->iDelta)
2766 /* year value limits are set up explicitly after updown creation */
2767 MONTHCAL_Scroll(infoPtr, 12 * nmud->iDelta);
2768 MONTHCAL_NotifyDayState(infoPtr);
2769 MONTHCAL_NotifySelectionChange(infoPtr);
2772 return 0;
2775 static inline BOOL
2776 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2778 BOOL prev = infoPtr->isUnicode;
2779 infoPtr->isUnicode = isUnicode;
2780 return prev;
2783 static inline BOOL
2784 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2786 return infoPtr->isUnicode;
2789 static LRESULT WINAPI
2790 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2792 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0);
2794 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2796 if (!infoPtr && (uMsg != WM_CREATE))
2797 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2798 switch(uMsg)
2800 case MCM_GETCURSEL:
2801 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2803 case MCM_SETCURSEL:
2804 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2806 case MCM_GETMAXSELCOUNT:
2807 return MONTHCAL_GetMaxSelCount(infoPtr);
2809 case MCM_SETMAXSELCOUNT:
2810 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2812 case MCM_GETSELRANGE:
2813 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2815 case MCM_SETSELRANGE:
2816 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2818 case MCM_GETMONTHRANGE:
2819 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2821 case MCM_SETDAYSTATE:
2822 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2824 case MCM_GETMINREQRECT:
2825 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2827 case MCM_GETCOLOR:
2828 return MONTHCAL_GetColor(infoPtr, wParam);
2830 case MCM_SETCOLOR:
2831 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2833 case MCM_GETTODAY:
2834 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2836 case MCM_SETTODAY:
2837 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2839 case MCM_HITTEST:
2840 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2842 case MCM_GETFIRSTDAYOFWEEK:
2843 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2845 case MCM_SETFIRSTDAYOFWEEK:
2846 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2848 case MCM_GETRANGE:
2849 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2851 case MCM_SETRANGE:
2852 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2854 case MCM_GETMONTHDELTA:
2855 return MONTHCAL_GetMonthDelta(infoPtr);
2857 case MCM_SETMONTHDELTA:
2858 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2860 case MCM_GETMAXTODAYWIDTH:
2861 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2863 case MCM_SETUNICODEFORMAT:
2864 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2866 case MCM_GETUNICODEFORMAT:
2867 return MONTHCAL_GetUnicodeFormat(infoPtr);
2869 case MCM_GETCALENDARCOUNT:
2870 return MONTHCAL_GetCalCount(infoPtr);
2872 case WM_GETDLGCODE:
2873 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2875 case WM_RBUTTONUP:
2876 return MONTHCAL_RButtonUp(infoPtr, lParam);
2878 case WM_LBUTTONDOWN:
2879 return MONTHCAL_LButtonDown(infoPtr, lParam);
2881 case WM_MOUSEMOVE:
2882 return MONTHCAL_MouseMove(infoPtr, lParam);
2884 case WM_LBUTTONUP:
2885 return MONTHCAL_LButtonUp(infoPtr, lParam);
2887 case WM_PAINT:
2888 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2890 case WM_PRINTCLIENT:
2891 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2893 case WM_ERASEBKGND:
2894 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2896 case WM_SETFOCUS:
2897 return MONTHCAL_SetFocus(infoPtr);
2899 case WM_SIZE:
2900 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2902 case WM_NOTIFY:
2903 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2905 case WM_CREATE:
2906 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2908 case WM_SETFONT:
2909 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2911 case WM_GETFONT:
2912 return MONTHCAL_GetFont(infoPtr);
2914 case WM_TIMER:
2915 return MONTHCAL_Timer(infoPtr, wParam);
2917 case WM_THEMECHANGED:
2918 return theme_changed (infoPtr);
2920 case WM_DESTROY:
2921 return MONTHCAL_Destroy(infoPtr);
2923 case WM_SYSCOLORCHANGE:
2924 COMCTL32_RefreshSysColors();
2925 return 0;
2927 case WM_STYLECHANGED:
2928 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2930 case WM_STYLECHANGING:
2931 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2933 default:
2934 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2935 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2936 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2941 void
2942 MONTHCAL_Register(void)
2944 WNDCLASSW wndClass;
2946 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2947 wndClass.style = CS_GLOBALCLASS;
2948 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2949 wndClass.cbClsExtra = 0;
2950 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2951 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2952 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2953 wndClass.lpszClassName = MONTHCAL_CLASSW;
2955 RegisterClassW(&wndClass);
2959 void
2960 MONTHCAL_Unregister(void)
2962 UnregisterClassW(MONTHCAL_CLASSW, NULL);