kernel32/tests: Fix crashes on win8 in console.
[wine/multimedia.git] / dlls / comctl32 / monthcal.c
blobf27eee3de3e48f3cf6b35a4512291565f1647ab9
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, /* wDayOfWeek */ 0, /* wDay */ 31 };
164 static const SYSTEMTIME min_allowed_date = { /* wYear */ 1752, /* wMonth */ 9, /* wDayOfWeek */ 0, /* 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.stSelStart.wDayOfWeek = 0;
189 if(infoPtr->dwStyle & MCS_MULTISELECT){
190 nmsc.stSelEnd = infoPtr->maxSel;
191 nmsc.stSelEnd.wDayOfWeek = 0;
193 else
194 nmsc.stSelEnd = st_null;
196 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
199 /* send a single MCN_SELECT notification */
200 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
202 NMSELCHANGE nmsc;
204 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
205 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
206 nmsc.nmhdr.code = MCN_SELECT;
207 nmsc.stSelStart = infoPtr->minSel;
208 nmsc.stSelStart.wDayOfWeek = 0;
209 if(infoPtr->dwStyle & MCS_MULTISELECT){
210 nmsc.stSelEnd = infoPtr->maxSel;
211 nmsc.stSelEnd.wDayOfWeek = 0;
213 else
214 nmsc.stSelEnd = st_null;
216 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
219 static inline int MONTHCAL_MonthDiff(const SYSTEMTIME *left, const SYSTEMTIME *right)
221 return (right->wYear - left->wYear)*12 + right->wMonth - left->wMonth;
224 /* returns the number of days in any given month, checking for leap days */
225 /* January is 1, December is 12 */
226 int MONTHCAL_MonthLength(int month, int year)
228 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
229 /* Wrap around, this eases handling. Getting length only we shouldn't care
230 about year change here cause January and December have
231 the same day quantity */
232 if(month == 0)
233 month = 12;
234 else if(month == 13)
235 month = 1;
237 /* special case for calendar transition year */
238 if(month == min_allowed_date.wMonth && year == min_allowed_date.wYear) return 19;
240 /* if we have a leap year add 1 day to February */
241 /* a leap year is a year either divisible by 400 */
242 /* or divisible by 4 and not by 100 */
243 if(month == 2) { /* February */
244 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
245 (year%4 == 0)) ? 1 : 0);
247 else {
248 return mdays[month - 1];
252 /* compares timestamps using date part only */
253 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
255 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
256 (first->wDay == second->wDay);
259 /* make sure that date fields are valid */
260 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
262 if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
263 if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear)) return FALSE;
265 return TRUE;
268 /* Copies timestamp part only.
270 * PARAMETERS
272 * [I] from : source date
273 * [O] to : dest date
275 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
277 to->wHour = from->wHour;
278 to->wMinute = from->wMinute;
279 to->wSecond = from->wSecond;
282 /* Copies date part only.
284 * PARAMETERS
286 * [I] from : source date
287 * [O] to : dest date
289 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
291 to->wYear = from->wYear;
292 to->wMonth = from->wMonth;
293 to->wDay = from->wDay;
294 to->wDayOfWeek = from->wDayOfWeek;
297 /* Compares two dates in SYSTEMTIME format
299 * PARAMETERS
301 * [I] first : pointer to valid first date data to compare
302 * [I] second : pointer to valid second date data to compare
304 * RETURN VALUE
306 * -1 : first < second
307 * 0 : first == second
308 * 1 : first > second
310 * Note that no date validation performed, already validated values expected.
312 LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
314 FILETIME ft_first, ft_second;
316 SystemTimeToFileTime(first, &ft_first);
317 SystemTimeToFileTime(second, &ft_second);
319 return CompareFileTime(&ft_first, &ft_second);
322 static LONG MONTHCAL_CompareMonths(const SYSTEMTIME *first, const SYSTEMTIME *second)
324 SYSTEMTIME st_first, st_second;
326 st_first = st_second = st_null;
327 MONTHCAL_CopyDate(first, &st_first);
328 MONTHCAL_CopyDate(second, &st_second);
329 st_first.wDay = st_second.wDay = 1;
331 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
334 static LONG MONTHCAL_CompareDate(const SYSTEMTIME *first, const SYSTEMTIME *second)
336 SYSTEMTIME st_first, st_second;
338 st_first = st_second = st_null;
339 MONTHCAL_CopyDate(first, &st_first);
340 MONTHCAL_CopyDate(second, &st_second);
342 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
345 /* Checks largest possible date range and configured one
347 * PARAMETERS
349 * [I] infoPtr : valid pointer to control data
350 * [I] date : pointer to valid date data to check
351 * [I] fix : make date fit valid range
353 * RETURN VALUE
355 * TRUE - date within largest and configured range
356 * FALSE - date is outside largest or configured range
358 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr,
359 SYSTEMTIME *date, BOOL fix)
361 const SYSTEMTIME *fix_st = NULL;
363 if(MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) {
364 fix_st = &max_allowed_date;
366 else if(MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1) {
367 fix_st = &min_allowed_date;
369 else {
370 if(infoPtr->rangeValid & GDTR_MAX) {
371 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
372 fix_st = &infoPtr->maxDate;
376 if(infoPtr->rangeValid & GDTR_MIN) {
377 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
378 fix_st = &infoPtr->minDate;
383 if (fix && fix_st) {
384 date->wYear = fix_st->wYear;
385 date->wMonth = fix_st->wMonth;
388 return !fix_st;
391 /* Checks passed range width with configured maximum selection count
393 * PARAMETERS
395 * [I] infoPtr : valid pointer to control data
396 * [I] range0 : pointer to valid date data (requested bound)
397 * [I] range1 : pointer to valid date data (primary bound)
398 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
400 * Adjust value computed basing on primary bound and current maximum selection
401 * count. For simple range check (without adjusted value required) (range0, range1)
402 * relation means nothing.
404 * RETURN VALUE
406 * TRUE - range is shorter or equal to maximum
407 * FALSE - range is larger than maximum
409 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
410 const SYSTEMTIME *range0,
411 const SYSTEMTIME *range1,
412 SYSTEMTIME *adjust)
414 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
415 FILETIME ft_range0, ft_range1;
416 LONG cmp;
418 SystemTimeToFileTime(range0, &ft_range0);
419 SystemTimeToFileTime(range1, &ft_range1);
421 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
422 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
423 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
424 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
426 cmp = CompareFileTime(&ft_range0, &ft_range1);
428 if(cmp == 1)
429 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
430 else
431 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
433 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
435 if(adjust) {
436 if(cmp == 1)
437 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
438 else
439 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
441 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
442 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
443 FileTimeToSystemTime(&ft_range0, adjust);
446 return FALSE;
448 else return TRUE;
451 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
452 Milliseconds are intentionally not validated. */
453 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
455 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
456 return FALSE;
457 else
458 return TRUE;
461 /* Note:Depending on DST, this may be offset by a day.
462 Need to find out if we're on a DST place & adjust the clock accordingly.
463 Above function assumes we have a valid data.
464 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
465 0 = Sunday.
468 /* Returns the day in the week
470 * PARAMETERS
471 * [i] date : input date
472 * [I] inplace : set calculated value back to date structure
474 * RETURN VALUE
475 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
477 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
479 SYSTEMTIME st = st_null;
480 FILETIME ft;
482 MONTHCAL_CopyDate(date, &st);
484 SystemTimeToFileTime(&st, &ft);
485 FileTimeToSystemTime(&ft, &st);
487 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
489 return st.wDayOfWeek;
492 /* add/subtract 'months' from date */
493 static inline void MONTHCAL_GetMonth(SYSTEMTIME *date, INT months)
495 INT length, m = date->wMonth + months;
497 date->wYear += m > 0 ? (m - 1) / 12 : m / 12 - 1;
498 date->wMonth = m > 0 ? (m - 1) % 12 + 1 : 12 + m % 12;
499 /* fix moving from last day in a month */
500 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
501 if(date->wDay > length) date->wDay = length;
502 MONTHCAL_CalculateDayOfWeek(date, TRUE);
505 /* properly updates date to point on next month */
506 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
508 MONTHCAL_GetMonth(date, 1);
511 /* properly updates date to point on prev month */
512 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
514 MONTHCAL_GetMonth(date, -1);
517 /* Returns full date for a first currently visible day */
518 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
520 /* zero indexed calendar has the earliest date */
521 SYSTEMTIME st_first = infoPtr->calendars[0].month;
522 INT firstDay;
524 st_first.wDay = 1;
525 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
527 *date = infoPtr->calendars[0].month;
528 MONTHCAL_GetPrevMonth(date);
530 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
531 (infoPtr->firstDay - firstDay) % 7 + 1;
533 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
534 date->wDay -= 7;
536 /* fix day of week */
537 MONTHCAL_CalculateDayOfWeek(date, TRUE);
540 /* Returns full date for a last currently visible day */
541 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
543 /* the latest date is in latest calendar */
544 SYSTEMTIME st, *lt_month = &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
545 INT first_day;
547 *date = *lt_month;
548 st = *lt_month;
550 /* day of week of first day of current month */
551 st.wDay = 1;
552 first_day = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
554 MONTHCAL_GetNextMonth(date);
555 MONTHCAL_GetPrevMonth(&st);
557 /* last calendar starts with some date from previous month that not displayed */
558 st.wDay = MONTHCAL_MonthLength(st.wMonth, st.wYear) +
559 (infoPtr->firstDay - first_day) % 7 + 1;
560 if (st.wDay > MONTHCAL_MonthLength(st.wMonth, st.wYear)) st.wDay -= 7;
562 /* Use month length to get max day. 42 means max day count in calendar area */
563 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
564 MONTHCAL_MonthLength(lt_month->wMonth, lt_month->wYear);
566 /* fix day of week */
567 MONTHCAL_CalculateDayOfWeek(date, TRUE);
570 /* From a given point calculate the row, column and day in the calendar,
571 'day == 0' means the last day of the last month. */
572 static int MONTHCAL_GetDayFromPos(const MONTHCAL_INFO *infoPtr, POINT pt, INT calIdx)
574 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
575 int firstDay, col, row;
576 RECT client;
578 GetClientRect(infoPtr->hwndSelf, &client);
580 /* if the point is outside the x bounds of the window put it at the boundary */
581 if (pt.x > client.right) pt.x = client.right;
583 col = (pt.x - infoPtr->calendars[calIdx].days.left ) / infoPtr->width_increment;
584 row = (pt.y - infoPtr->calendars[calIdx].days.top ) / infoPtr->height_increment;
586 st.wDay = 1;
587 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
588 return col + 7 * row - firstDay;
591 /* Get day position for given date and calendar
593 * PARAMETERS
595 * [I] infoPtr : pointer to control data
596 * [I] date : date value
597 * [O] col : day column (zero based)
598 * [O] row : week column (zero based)
599 * [I] calIdx : calendar index
601 static void MONTHCAL_GetDayPos(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
602 INT *col, INT *row, INT calIdx)
604 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
605 INT first;
607 st.wDay = 1;
608 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
610 if (calIdx == 0 || calIdx == MONTHCAL_GetCalCount(infoPtr)-1) {
611 const SYSTEMTIME *cal = &infoPtr->calendars[calIdx].month;
612 LONG cmp = MONTHCAL_CompareMonths(date, &st);
614 /* previous month */
615 if (cmp == -1) {
616 *col = (first - MONTHCAL_MonthLength(date->wMonth, cal->wYear) + date->wDay) % 7;
617 *row = 0;
618 return;
621 /* next month calculation is same as for current, just add current month length */
622 if (cmp == 1)
623 first += MONTHCAL_MonthLength(cal->wMonth, cal->wYear);
626 *col = (date->wDay + first) % 7;
627 *row = (date->wDay + first - *col) / 7;
630 /* returns bounding box for day in given position in given calendar */
631 static inline void MONTHCAL_GetDayRectI(const MONTHCAL_INFO *infoPtr, RECT *r,
632 INT col, INT row, INT calIdx)
634 r->left = infoPtr->calendars[calIdx].days.left + col * infoPtr->width_increment;
635 r->right = r->left + infoPtr->width_increment;
636 r->top = infoPtr->calendars[calIdx].days.top + row * infoPtr->height_increment;
637 r->bottom = r->top + infoPtr->textHeight;
640 /* Returns bounding box for given date
642 * NOTE: when calendar index is unknown pass -1
644 static inline void MONTHCAL_GetDayRect(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
645 RECT *r, INT calIdx)
647 INT col, row;
649 if (calIdx == -1)
651 INT cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[0].month);
653 if (cmp <= 0)
654 calIdx = 0;
655 else
657 cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month);
658 if (cmp >= 0)
659 calIdx = MONTHCAL_GetCalCount(infoPtr)-1;
660 else
662 for (calIdx = 1; calIdx < MONTHCAL_GetCalCount(infoPtr)-1; calIdx++)
663 if (MONTHCAL_CompareMonths(date, &infoPtr->calendars[calIdx].month) == 0)
664 break;
669 MONTHCAL_GetDayPos(infoPtr, date, &col, &row, calIdx);
670 MONTHCAL_GetDayRectI(infoPtr, r, col, row, calIdx);
673 static LRESULT
674 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
676 INT range;
678 TRACE("flag=%d, st=%p\n", flag, st);
680 switch (flag) {
681 case GMR_VISIBLE:
683 if (st)
685 st[0] = infoPtr->calendars[0].month;
686 st[1] = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
688 if (st[0].wMonth == min_allowed_date.wMonth &&
689 st[0].wYear == min_allowed_date.wYear)
691 st[0].wDay = min_allowed_date.wDay;
693 else
694 st[0].wDay = 1;
695 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
697 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
698 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
701 range = MONTHCAL_GetCalCount(infoPtr);
702 break;
704 case GMR_DAYSTATE:
706 if (st)
708 MONTHCAL_GetMinDate(infoPtr, &st[0]);
709 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
711 /* include two partially visible months */
712 range = MONTHCAL_GetCalCount(infoPtr) + 2;
713 break;
715 default:
716 WARN("Unknown flag value, got %d\n", flag);
717 range = 0;
720 return range;
723 /* Focused day helper:
725 - set focused date to given value;
726 - reset to zero value if NULL passed;
727 - invalidate previous and new day rectangle only if needed.
729 Returns TRUE if focused day changed, FALSE otherwise.
731 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
733 RECT r;
735 if(st)
737 /* there's nothing to do if it's the same date,
738 mouse move within same date rectangle case */
739 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
741 /* invalidate old focused day */
742 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
743 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
745 infoPtr->focusedSel = *st;
748 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
750 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
751 infoPtr->focusedSel = st_null;
753 /* on set invalidates new day, on reset clears previous focused day */
754 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
756 return TRUE;
759 /* draw today boundary box for specified rectangle */
760 static void MONTHCAL_Circle(const MONTHCAL_INFO *infoPtr, HDC hdc, const RECT *r)
762 HPEN old_pen = SelectObject(hdc, infoPtr->pens[PenRed]);
763 HBRUSH old_brush;
765 old_brush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
766 Rectangle(hdc, r->left, r->top, r->right, r->bottom);
768 SelectObject(hdc, old_brush);
769 SelectObject(hdc, old_pen);
772 /* Draw today day mark rectangle
774 * [I] hdc : context to draw in
775 * [I] date : day to mark with rectangle
778 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
779 const SYSTEMTIME *date)
781 RECT r;
783 MONTHCAL_GetDayRect(infoPtr, date, &r, -1);
784 MONTHCAL_Circle(infoPtr, hdc, &r);
787 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
788 int bold, const PAINTSTRUCT *ps)
790 static const WCHAR fmtW[] = { '%','d',0 };
791 WCHAR buf[10];
792 RECT r, r_temp;
793 COLORREF oldCol = 0;
794 COLORREF oldBk = 0;
795 INT old_bkmode, selection;
797 /* no need to check styles: when selection is not valid, it is set to zero.
798 1 < day < 31, so everything is OK */
799 MONTHCAL_GetDayRect(infoPtr, st, &r, -1);
800 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
802 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
803 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0))
805 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
806 TRACE("%s\n", wine_dbgstr_rect(&r));
807 oldCol = SetTextColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
808 oldBk = SetBkColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
809 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
811 selection = 1;
813 else
814 selection = 0;
816 SelectObject(hdc, bold ? infoPtr->hBoldFont : infoPtr->hFont);
818 old_bkmode = SetBkMode(hdc, TRANSPARENT);
819 wsprintfW(buf, fmtW, st->wDay);
820 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
821 SetBkMode(hdc, old_bkmode);
823 if (selection)
825 SetTextColor(hdc, oldCol);
826 SetBkColor(hdc, oldBk);
830 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, enum nav_direction button)
832 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
833 RECT *r = button == DIRECTION_FORWARD ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
834 BOOL pressed = button == DIRECTION_FORWARD ? infoPtr->status & MC_NEXTPRESSED :
835 infoPtr->status & MC_PREVPRESSED;
836 if (theme)
838 static const int states[] = {
839 /* Prev button */
840 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
841 /* Next button */
842 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
844 int stateNum = button == DIRECTION_FORWARD ? 3 : 0;
845 if (pressed)
846 stateNum += 1;
847 else
849 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
851 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
853 else
855 int style = button == DIRECTION_FORWARD ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
856 if (pressed)
857 style |= DFCS_PUSHED;
858 else
860 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
863 DrawFrameControl(hdc, r, DFC_SCROLL, style);
867 /* paint a title with buttons and month/year string */
868 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
870 static const WCHAR fmt_monthW[] = { '%','s',' ','%','l','d',0 };
871 RECT *title = &infoPtr->calendars[calIdx].title;
872 const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
873 WCHAR buf_month[80], buf_fmt[80];
874 SIZE sz;
876 /* fill header box */
877 FillRect(hdc, title, infoPtr->brushes[BrushTitle]);
879 /* month/year string */
880 SetBkColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
881 SetTextColor(hdc, infoPtr->colors[MCSC_TITLETEXT]);
882 SelectObject(hdc, infoPtr->hBoldFont);
884 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1 + st->wMonth - 1,
885 buf_month, countof(buf_month));
887 wsprintfW(buf_fmt, fmt_monthW, buf_month, st->wYear);
888 DrawTextW(hdc, buf_fmt, strlenW(buf_fmt), title,
889 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
891 /* update title rectangles with current month - used while testing hits */
892 GetTextExtentPoint32W(hdc, buf_fmt, strlenW(buf_fmt), &sz);
893 infoPtr->calendars[calIdx].titlemonth.left = title->right / 2 + title->left / 2 - sz.cx / 2;
894 infoPtr->calendars[calIdx].titleyear.right = title->right / 2 + title->left / 2 + sz.cx / 2;
896 GetTextExtentPoint32W(hdc, buf_month, strlenW(buf_month), &sz);
897 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
898 infoPtr->calendars[calIdx].titleyear.left = infoPtr->calendars[calIdx].titlemonth.right;
901 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
903 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
904 static const WCHAR fmt_weekW[] = { '%','d',0 };
905 INT mindays, weeknum, weeknum1, startofprescal;
906 INT i, prev_month;
907 SYSTEMTIME st;
908 WCHAR buf[80];
909 HPEN old_pen;
910 RECT r;
912 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
914 MONTHCAL_GetMinDate(infoPtr, &st);
915 startofprescal = st.wDay;
916 st = *date;
918 prev_month = date->wMonth - 1;
919 if(prev_month == 0) prev_month = 12;
922 Rules what week to call the first week of a new year:
923 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
924 The week containing Jan 1 is the first week of year
925 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
926 First week of year must contain 4 days of the new year
927 LOCALE_IFIRSTWEEKOFYEAR == 1 (what countries?)
928 The first week of the year must contain only days of the new year
930 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
931 weeknum = atoiW(buf);
932 switch (weeknum)
934 case 1: mindays = 6;
935 break;
936 case 2: mindays = 3;
937 break;
938 case 0: mindays = 0;
939 break;
940 default:
941 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
942 mindays = 0;
945 if (date->wMonth == 1)
947 /* calculate all those exceptions for January */
948 st.wDay = st.wMonth = 1;
949 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
950 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
951 weeknum = 1;
952 else
954 weeknum = 0;
955 for(i = 0; i < 11; i++)
956 weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
958 weeknum += startofprescal + 7;
959 weeknum /= 7;
960 st.wYear -= 1;
961 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
962 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
965 else
967 weeknum = 0;
968 for(i = 0; i < prev_month - 1; i++)
969 weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
971 weeknum += startofprescal + 7;
972 weeknum /= 7;
973 st.wDay = st.wMonth = 1;
974 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
975 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
978 r = infoPtr->calendars[calIdx].weeknums;
980 /* erase whole week numbers area */
981 FillRect(hdc, &r, infoPtr->brushes[BrushMonth]);
982 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
984 /* reduce rectangle to one week number */
985 r.bottom = r.top + infoPtr->height_increment;
987 for(i = 0; i < 6; i++) {
988 if((i == 0) && (weeknum > 50))
990 wsprintfW(buf, fmt_weekW, weeknum);
991 weeknum = 0;
993 else if((i == 5) && (weeknum > 47))
995 wsprintfW(buf, fmt_weekW, 1);
997 else
998 wsprintfW(buf, fmt_weekW, weeknum + i);
1000 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1001 OffsetRect(&r, 0, infoPtr->height_increment);
1004 /* line separator for week numbers column */
1005 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1006 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
1007 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
1008 SelectObject(hdc, old_pen);
1011 /* bottom today date */
1012 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1014 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
1015 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
1016 RECT text_rect, box_rect;
1017 HFONT old_font;
1018 INT col;
1020 if(infoPtr->dwStyle & MCS_NOTODAY) return;
1022 if (!LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, countof(buf_todayW)))
1024 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
1025 WARN("Can't load resource\n");
1026 strcpyW(buf_todayW, todayW);
1029 col = infoPtr->dwStyle & MCS_NOTODAYCIRCLE ? 0 : 1;
1030 if (infoPtr->dwStyle & MCS_WEEKNUMBERS) col--;
1031 /* label is located below first calendar last row */
1032 MONTHCAL_GetDayRectI(infoPtr, &text_rect, col, 6, infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx);
1033 box_rect = text_rect;
1035 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
1036 buf_dateW, countof(buf_dateW));
1037 old_font = SelectObject(hdc, infoPtr->hBoldFont);
1038 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1040 wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
1041 DrawTextW(hdc, buf, -1, &text_rect, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1042 DrawTextW(hdc, buf, -1, &text_rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1044 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
1045 OffsetRect(&box_rect, -infoPtr->width_increment, 0);
1046 MONTHCAL_Circle(infoPtr, hdc, &box_rect);
1049 SelectObject(hdc, old_font);
1052 /* today mark + focus */
1053 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1055 /* circle today date if only it's in fully visible month */
1056 if (!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
1058 INT i;
1060 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1061 if (!MONTHCAL_CompareMonths(&infoPtr->todaysDate, &infoPtr->calendars[i].month))
1063 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
1064 break;
1068 if (!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
1070 RECT r;
1071 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
1072 DrawFocusRect(hdc, &r);
1076 /* months before first calendar month and after last calendar month */
1077 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1079 INT mask, length, index;
1080 SYSTEMTIME st_max, st;
1082 if (infoPtr->dwStyle & MCS_NOTRAILINGDATES) return;
1084 SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
1086 /* draw prev month */
1087 MONTHCAL_GetMinDate(infoPtr, &st);
1088 mask = 1 << (st.wDay-1);
1089 /* December and January both 31 days long, so no worries if wrapped */
1090 length = MONTHCAL_MonthLength(infoPtr->calendars[0].month.wMonth - 1,
1091 infoPtr->calendars[0].month.wYear);
1092 index = 0;
1093 while(st.wDay <= length)
1095 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1096 mask <<= 1;
1097 st.wDay++;
1100 /* draw next month */
1101 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1102 st.wDay = 1;
1103 MONTHCAL_GetNextMonth(&st);
1104 MONTHCAL_GetMaxDate(infoPtr, &st_max);
1105 mask = 1;
1106 index = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)-1;
1107 while(st.wDay <= st_max.wDay)
1109 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1110 mask <<= 1;
1111 st.wDay++;
1115 /* paint a calendar area */
1116 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
1118 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
1119 INT i, j, length;
1120 RECT r, fill_bk_rect;
1121 SYSTEMTIME st;
1122 WCHAR buf[80];
1123 HPEN old_pen;
1124 int mask;
1126 /* fill whole days area - from week days area to today note rectangle */
1127 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
1128 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
1129 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
1131 FillRect(hdc, &fill_bk_rect, infoPtr->brushes[BrushMonth]);
1133 /* draw line under day abbreviations */
1134 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1135 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
1136 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
1137 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
1138 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
1139 SelectObject(hdc, old_pen);
1141 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
1142 infoPtr->calendars[calIdx].weeknums.right;
1144 /* draw day abbreviations */
1145 SelectObject(hdc, infoPtr->hFont);
1146 SetBkColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
1147 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1148 /* rectangle to draw a single day abbreviation within */
1149 r = infoPtr->calendars[calIdx].wdays;
1150 r.right = r.left + infoPtr->width_increment;
1152 i = infoPtr->firstDay;
1153 for(j = 0; j < 7; j++) {
1154 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
1155 DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1156 OffsetRect(&r, infoPtr->width_increment, 0);
1159 /* draw current month */
1160 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1161 st = *date;
1162 st.wDay = 1;
1163 mask = 1;
1164 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1165 while(st.wDay <= length)
1167 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[calIdx+1] & mask, ps);
1168 mask <<= 1;
1169 st.wDay++;
1173 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1175 COLORREF old_text_clr, old_bk_clr;
1176 HFONT old_font;
1177 INT i;
1179 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1180 old_bk_clr = GetBkColor(hdc);
1181 old_font = GetCurrentObject(hdc, OBJ_FONT);
1183 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1185 RECT *title = &infoPtr->calendars[i].title;
1186 RECT r;
1188 /* draw title, redraw all its elements */
1189 if (IntersectRect(&r, &(ps->rcPaint), title))
1190 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1192 /* draw calendar area */
1193 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1194 if (IntersectRect(&r, &(ps->rcPaint), &r))
1195 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1197 /* week numbers */
1198 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1201 /* partially visible months */
1202 MONTHCAL_PaintLeadTrailMonths(infoPtr, hdc, ps);
1204 /* focus and today rectangle */
1205 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1207 /* today at the bottom left */
1208 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1210 /* navigation buttons */
1211 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_BACKWARD);
1212 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_FORWARD);
1214 /* restore context */
1215 SetBkColor(hdc, old_bk_clr);
1216 SelectObject(hdc, old_font);
1217 SetTextColor(hdc, old_text_clr);
1220 static LRESULT
1221 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, RECT *rect)
1223 TRACE("rect %p\n", rect);
1225 if(!rect) return FALSE;
1227 *rect = infoPtr->calendars[0].title;
1228 rect->bottom = infoPtr->calendars[0].days.bottom + infoPtr->todayrect.bottom -
1229 infoPtr->todayrect.top;
1231 AdjustWindowRect(rect, infoPtr->dwStyle, FALSE);
1233 /* minimal rectangle is zero based */
1234 OffsetRect(rect, -rect->left, -rect->top);
1236 TRACE("%s\n", wine_dbgstr_rect(rect));
1238 return TRUE;
1241 static COLORREF
1242 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, UINT index)
1244 TRACE("%p, %d\n", infoPtr, index);
1246 if (index > MCSC_TRAILINGTEXT) return -1;
1247 return infoPtr->colors[index];
1250 static LRESULT
1251 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, UINT index, COLORREF color)
1253 enum CachedBrush type;
1254 COLORREF prev;
1256 TRACE("%p, %d: color %08x\n", infoPtr, index, color);
1258 if (index > MCSC_TRAILINGTEXT) return -1;
1260 prev = infoPtr->colors[index];
1261 infoPtr->colors[index] = color;
1263 /* update cached brush */
1264 switch (index)
1266 case MCSC_BACKGROUND:
1267 type = BrushBackground;
1268 break;
1269 case MCSC_TITLEBK:
1270 type = BrushTitle;
1271 break;
1272 case MCSC_MONTHBK:
1273 type = BrushMonth;
1274 break;
1275 default:
1276 type = BrushLast;
1279 if (type != BrushLast)
1281 DeleteObject(infoPtr->brushes[type]);
1282 infoPtr->brushes[type] = CreateSolidBrush(color);
1285 /* update cached pen */
1286 if (index == MCSC_TEXT)
1288 DeleteObject(infoPtr->pens[PenText]);
1289 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[index]);
1292 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND);
1293 return prev;
1296 static LRESULT
1297 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1299 TRACE("\n");
1301 if(infoPtr->delta)
1302 return infoPtr->delta;
1303 else
1304 return infoPtr->visible;
1308 static LRESULT
1309 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1311 INT prev = infoPtr->delta;
1313 TRACE("delta %d\n", delta);
1315 infoPtr->delta = delta;
1316 return prev;
1320 static inline LRESULT
1321 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1323 int day;
1325 /* convert from SYSTEMTIME to locale format */
1326 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1328 return MAKELONG(day, infoPtr->firstDaySet);
1332 /* Sets the first day of the week that will appear in the control
1335 * PARAMETERS:
1336 * [I] infoPtr : valid pointer to control data
1337 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1340 * RETURN VALUE:
1341 * Low word contains previous first day,
1342 * high word indicates was first day forced with this message before or is
1343 * locale defined (TRUE - was forced, FALSE - wasn't).
1345 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1346 * FIXME: we need more error checking here
1348 static LRESULT
1349 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1351 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1352 int new_day;
1354 TRACE("%d\n", day);
1356 if(day == -1)
1358 WCHAR buf[80];
1360 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1361 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1363 new_day = atoiW(buf);
1365 infoPtr->firstDaySet = FALSE;
1367 else if(day >= 7)
1369 new_day = 6; /* max first day allowed */
1370 infoPtr->firstDaySet = TRUE;
1372 else
1374 /* Native behaviour for that case is broken: invalid date number >31
1375 got displayed at (0,0) position, current month starts always from
1376 (1,0) position. Should be implemented here as well only if there's
1377 nothing else to do. */
1378 if (day < -1)
1379 FIXME("No bug compatibility for day=%d\n", day);
1381 new_day = day;
1382 infoPtr->firstDaySet = TRUE;
1385 /* convert from locale to SYSTEMTIME format */
1386 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1388 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1390 return prev;
1393 static LRESULT
1394 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1396 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1399 static LRESULT
1400 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1402 FILETIME ft_min, ft_max;
1404 TRACE("%x %p\n", limits, range);
1406 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1407 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1408 return FALSE;
1410 if (limits & GDTR_MIN)
1412 if (!MONTHCAL_ValidateTime(&range[0]))
1413 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1415 infoPtr->minDate = range[0];
1416 infoPtr->rangeValid |= GDTR_MIN;
1418 if (limits & GDTR_MAX)
1420 if (!MONTHCAL_ValidateTime(&range[1]))
1421 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1423 infoPtr->maxDate = range[1];
1424 infoPtr->rangeValid |= GDTR_MAX;
1427 /* Only one limit set - we are done */
1428 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1429 return TRUE;
1431 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1432 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1434 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1436 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1438 /* Native swaps limits only when both limits are being set. */
1439 SYSTEMTIME st_tmp = infoPtr->minDate;
1440 infoPtr->minDate = infoPtr->maxDate;
1441 infoPtr->maxDate = st_tmp;
1443 else
1445 /* reset the other limit */
1446 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1447 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1448 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1452 return TRUE;
1456 static LRESULT
1457 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1459 TRACE("%p\n", range);
1461 if(!range) return FALSE;
1463 range[1] = infoPtr->maxDate;
1464 range[0] = infoPtr->minDate;
1466 return infoPtr->rangeValid;
1470 static LRESULT
1471 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1473 TRACE("%p %d %p\n", infoPtr, months, states);
1475 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return 0;
1476 if (months != MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)) return 0;
1478 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1480 return 1;
1483 static LRESULT
1484 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1486 TRACE("%p\n", curSel);
1487 if(!curSel) return FALSE;
1488 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1490 *curSel = infoPtr->minSel;
1491 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1492 return TRUE;
1495 static LRESULT
1496 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1498 SYSTEMTIME prev = infoPtr->minSel, selection;
1499 INT diff;
1500 WORD day;
1502 TRACE("%p\n", curSel);
1503 if(!curSel) return FALSE;
1504 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1506 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1507 /* exit earlier if selection equals current */
1508 if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1510 selection = *curSel;
1511 selection.wHour = selection.wMinute = selection.wSecond = selection.wMilliseconds = 0;
1512 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1514 if(!MONTHCAL_IsDateInValidRange(infoPtr, &selection, FALSE)) return FALSE;
1516 /* scroll calendars only if we have to */
1517 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, curSel);
1518 if (diff <= 0)
1520 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, curSel);
1521 if (diff > 0) diff = 0;
1524 if (diff != 0)
1526 INT i;
1528 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1529 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1532 /* we need to store time part as it is */
1533 selection = *curSel;
1534 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1535 infoPtr->minSel = infoPtr->maxSel = selection;
1537 /* if selection is still in current month, reduce rectangle */
1538 day = prev.wDay;
1539 prev.wDay = curSel->wDay;
1540 if (MONTHCAL_IsDateEqual(&prev, curSel))
1542 RECT r_prev, r_new;
1544 prev.wDay = day;
1545 MONTHCAL_GetDayRect(infoPtr, &prev, &r_prev, -1);
1546 MONTHCAL_GetDayRect(infoPtr, curSel, &r_new, -1);
1548 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1549 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1551 else
1552 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1554 return TRUE;
1558 static LRESULT
1559 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1561 return infoPtr->maxSelCount;
1565 static LRESULT
1566 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1568 TRACE("%d\n", max);
1570 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1571 if(max <= 0) return FALSE;
1573 infoPtr->maxSelCount = max;
1575 return TRUE;
1579 static LRESULT
1580 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1582 TRACE("%p\n", range);
1584 if(!range) return FALSE;
1586 if(infoPtr->dwStyle & MCS_MULTISELECT)
1588 range[1] = infoPtr->maxSel;
1589 range[0] = infoPtr->minSel;
1590 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1591 return TRUE;
1594 return FALSE;
1598 static LRESULT
1599 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1601 SYSTEMTIME old_range[2];
1602 INT diff;
1604 TRACE("%p\n", range);
1606 if(!range || !(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1608 /* adjust timestamps */
1609 if(!MONTHCAL_ValidateTime(&range[0])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1610 if(!MONTHCAL_ValidateTime(&range[1])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1612 /* maximum range exceeded */
1613 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1615 old_range[0] = infoPtr->minSel;
1616 old_range[1] = infoPtr->maxSel;
1618 /* swap if min > max */
1619 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1621 infoPtr->minSel = range[0];
1622 infoPtr->maxSel = range[1];
1624 else
1626 infoPtr->minSel = range[1];
1627 infoPtr->maxSel = range[0];
1630 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, &infoPtr->maxSel);
1631 if (diff < 0)
1633 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, &infoPtr->maxSel);
1634 if (diff > 0) diff = 0;
1637 if (diff != 0)
1639 INT i;
1641 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1642 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1645 /* update day of week */
1646 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1647 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1649 /* redraw if bounds changed */
1650 /* FIXME: no actual need to redraw everything */
1651 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1652 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1654 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1657 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1658 return TRUE;
1662 static LRESULT
1663 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1665 TRACE("%p\n", today);
1667 if(!today) return FALSE;
1668 *today = infoPtr->todaysDate;
1669 return TRUE;
1672 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1674 * RETURN VALUE
1676 * TRUE - today date changed
1677 * FALSE - today date isn't changed
1679 static BOOL
1680 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1682 RECT new_r, old_r;
1684 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1686 MONTHCAL_GetDayRect(infoPtr, &infoPtr->todaysDate, &old_r, -1);
1687 MONTHCAL_GetDayRect(infoPtr, today, &new_r, -1);
1689 infoPtr->todaysDate = *today;
1691 /* only two days need redrawing */
1692 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1693 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1694 /* and today label */
1695 InvalidateRect(infoPtr->hwndSelf, &infoPtr->todayrect, FALSE);
1696 return TRUE;
1699 /* MCM_SETTODAT handler */
1700 static LRESULT
1701 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1703 TRACE("%p\n", today);
1705 if (today)
1707 /* remember if date was set successfully */
1708 if (MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1711 return 0;
1714 /* returns calendar index containing specified point, or -1 if it's background */
1715 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1717 RECT r;
1718 INT i;
1720 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1722 /* whole bounding rectangle allows some optimization to compute */
1723 r.left = infoPtr->calendars[i].title.left;
1724 r.top = infoPtr->calendars[i].title.top;
1725 r.bottom = infoPtr->calendars[i].days.bottom;
1726 r.right = infoPtr->calendars[i].days.right;
1728 if (PtInRect(&r, *pt)) return i;
1731 return -1;
1734 static inline UINT fill_hittest_info(const MCHITTESTINFO *src, MCHITTESTINFO *dest)
1736 dest->uHit = src->uHit;
1737 dest->st = src->st;
1739 if (dest->cbSize == sizeof(MCHITTESTINFO))
1740 memcpy(&dest->rc, &src->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1742 return src->uHit;
1745 static LRESULT
1746 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1748 MCHITTESTINFO htinfo;
1749 SYSTEMTIME *ht_month;
1750 INT day, calIdx;
1752 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1754 htinfo.st = st_null;
1756 /* we should preserve passed fields if hit area doesn't need them */
1757 if (lpht->cbSize == sizeof(MCHITTESTINFO))
1758 memcpy(&htinfo.rc, &lpht->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1760 /* Comment in for debugging...
1761 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,
1762 infoPtr->wdays.left, infoPtr->wdays.right,
1763 infoPtr->wdays.top, infoPtr->wdays.bottom,
1764 infoPtr->days.left, infoPtr->days.right,
1765 infoPtr->days.top, infoPtr->days.bottom,
1766 infoPtr->todayrect.left, infoPtr->todayrect.right,
1767 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1768 infoPtr->weeknums.left, infoPtr->weeknums.right,
1769 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1772 /* guess in what calendar we are */
1773 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1774 if (calIdx == -1)
1776 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1778 htinfo.uHit = MCHT_TODAYLINK;
1779 htinfo.rc = infoPtr->todayrect;
1781 else
1782 /* outside of calendar area? What's left must be background :-) */
1783 htinfo.uHit = MCHT_CALENDARBK;
1785 return fill_hittest_info(&htinfo, lpht);
1788 /* are we in the header? */
1789 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1790 /* FIXME: buttons hittesting could be optimized cause maximum
1791 two calendars have buttons */
1792 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1794 htinfo.uHit = MCHT_TITLEBTNPREV;
1795 htinfo.rc = infoPtr->titlebtnprev;
1797 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1799 htinfo.uHit = MCHT_TITLEBTNNEXT;
1800 htinfo.rc = infoPtr->titlebtnnext;
1802 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1804 htinfo.uHit = MCHT_TITLEMONTH;
1805 htinfo.rc = infoPtr->calendars[calIdx].titlemonth;
1806 htinfo.iOffset = calIdx;
1808 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1810 htinfo.uHit = MCHT_TITLEYEAR;
1811 htinfo.rc = infoPtr->calendars[calIdx].titleyear;
1812 htinfo.iOffset = calIdx;
1814 else
1816 htinfo.uHit = MCHT_TITLE;
1817 htinfo.rc = infoPtr->calendars[calIdx].title;
1818 htinfo.iOffset = calIdx;
1821 return fill_hittest_info(&htinfo, lpht);
1824 ht_month = &infoPtr->calendars[calIdx].month;
1825 /* days area (including week days and week numbers) */
1826 day = MONTHCAL_GetDayFromPos(infoPtr, lpht->pt, calIdx);
1827 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1829 htinfo.uHit = MCHT_CALENDARDAY;
1830 htinfo.iOffset = calIdx;
1831 htinfo.st.wYear = ht_month->wYear;
1832 htinfo.st.wMonth = (day < 1) ? ht_month->wMonth -1 : ht_month->wMonth;
1833 htinfo.st.wDay = (day < 1) ?
1834 MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day : day;
1836 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1838 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1840 htinfo.uHit = MCHT_CALENDARWEEKNUM;
1841 htinfo.st.wYear = ht_month->wYear;
1842 htinfo.iOffset = calIdx;
1844 if (day < 1)
1846 htinfo.st.wMonth = ht_month->wMonth - 1;
1847 htinfo.st.wDay = MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day;
1849 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1851 htinfo.st.wMonth = ht_month->wMonth + 1;
1852 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1854 else
1856 htinfo.st.wMonth = ht_month->wMonth;
1857 htinfo.st.wDay = day;
1860 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1862 htinfo.iOffset = calIdx;
1863 htinfo.st.wYear = ht_month->wYear;
1864 htinfo.st.wMonth = ht_month->wMonth;
1865 /* previous month only valid for first calendar */
1866 if (day < 1 && calIdx == 0)
1868 htinfo.uHit = MCHT_CALENDARDATEPREV;
1869 MONTHCAL_GetPrevMonth(&htinfo.st);
1870 htinfo.st.wDay = MONTHCAL_MonthLength(htinfo.st.wMonth, htinfo.st.wYear) + day;
1872 /* next month only valid for last calendar */
1873 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear) &&
1874 calIdx == MONTHCAL_GetCalCount(infoPtr)-1)
1876 htinfo.uHit = MCHT_CALENDARDATENEXT;
1877 MONTHCAL_GetNextMonth(&htinfo.st);
1878 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1880 /* multiple calendars case - blank areas for previous/next month */
1881 else if (day < 1 || day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1883 htinfo.uHit = MCHT_CALENDARBK;
1885 else
1887 htinfo.uHit = MCHT_CALENDARDATE;
1888 htinfo.st.wDay = day;
1891 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1892 MONTHCAL_GetDayRectI(infoPtr, &htinfo.rc, htinfo.iCol, htinfo.iRow, calIdx);
1893 /* always update day of week */
1894 MONTHCAL_CalculateDayOfWeek(&htinfo.st, TRUE);
1897 return fill_hittest_info(&htinfo, lpht);
1900 /* MCN_GETDAYSTATE notification helper */
1901 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1903 MONTHDAYSTATE *state;
1904 NMDAYSTATE nmds;
1906 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return;
1908 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1909 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1910 nmds.nmhdr.code = MCN_GETDAYSTATE;
1911 nmds.cDayState = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0);
1912 nmds.prgDayState = state = Alloc(nmds.cDayState * sizeof(MONTHDAYSTATE));
1914 MONTHCAL_GetMinDate(infoPtr, &nmds.stStart);
1915 nmds.stStart.wDay = 1;
1917 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1918 memcpy(infoPtr->monthdayState, nmds.prgDayState,
1919 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
1921 Free(state);
1924 /* no valid range check performed */
1925 static void MONTHCAL_Scroll(MONTHCAL_INFO *infoPtr, INT delta)
1927 INT i, selIdx = -1;
1929 for(i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1931 /* save selection position to shift it later */
1932 if (selIdx == -1 && MONTHCAL_CompareMonths(&infoPtr->minSel, &infoPtr->calendars[i].month) == 0)
1933 selIdx = i;
1935 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, delta);
1938 /* selection is always shifted to first calendar */
1939 if(infoPtr->dwStyle & MCS_MULTISELECT)
1941 SYSTEMTIME range[2];
1943 MONTHCAL_GetSelRange(infoPtr, range);
1944 MONTHCAL_GetMonth(&range[0], delta - selIdx);
1945 MONTHCAL_GetMonth(&range[1], delta - selIdx);
1946 MONTHCAL_SetSelRange(infoPtr, range);
1948 else
1950 SYSTEMTIME st = infoPtr->minSel;
1952 MONTHCAL_GetMonth(&st, delta - selIdx);
1953 MONTHCAL_SetCurSel(infoPtr, &st);
1957 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
1959 INT delta = infoPtr->delta ? infoPtr->delta : MONTHCAL_GetCalCount(infoPtr);
1960 SYSTEMTIME st;
1962 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
1964 /* check if change allowed by range set */
1965 if(direction == DIRECTION_BACKWARD)
1967 st = infoPtr->calendars[0].month;
1968 MONTHCAL_GetMonth(&st, -delta);
1970 else
1972 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1973 MONTHCAL_GetMonth(&st, delta);
1976 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
1978 MONTHCAL_Scroll(infoPtr, direction == DIRECTION_BACKWARD ? -delta : delta);
1979 MONTHCAL_NotifyDayState(infoPtr);
1980 MONTHCAL_NotifySelectionChange(infoPtr);
1983 static LRESULT
1984 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1986 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1987 HMENU hMenu;
1988 POINT menupoint;
1989 WCHAR buf[32];
1991 hMenu = CreatePopupMenu();
1992 if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1994 WARN("Can't load resource\n");
1995 strcpyW(buf, todayW);
1997 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1998 menupoint.x = (short)LOWORD(lParam);
1999 menupoint.y = (short)HIWORD(lParam);
2000 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2001 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
2002 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
2004 if (infoPtr->dwStyle & MCS_MULTISELECT)
2006 SYSTEMTIME range[2];
2008 range[0] = range[1] = infoPtr->todaysDate;
2009 MONTHCAL_SetSelRange(infoPtr, range);
2011 else
2012 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2014 MONTHCAL_NotifySelectionChange(infoPtr);
2015 MONTHCAL_NotifySelect(infoPtr);
2018 return 0;
2021 /***
2022 * DESCRIPTION:
2023 * Subclassed edit control windproc function
2025 * PARAMETER(S):
2026 * [I] hwnd : the edit window handle
2027 * [I] uMsg : the message that is to be processed
2028 * [I] wParam : first message parameter
2029 * [I] lParam : second message parameter
2032 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2034 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
2036 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
2037 hwnd, uMsg, wParam, lParam);
2039 switch (uMsg)
2041 case WM_GETDLGCODE:
2042 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
2044 case WM_DESTROY:
2046 WNDPROC editProc = infoPtr->EditWndProc;
2047 infoPtr->EditWndProc = NULL;
2048 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
2049 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
2052 case WM_KILLFOCUS:
2053 break;
2055 case WM_KEYDOWN:
2056 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
2057 break;
2059 default:
2060 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
2063 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
2064 SendMessageW(hwnd, WM_CLOSE, 0, 0);
2065 return 0;
2068 /* creates updown control and edit box */
2069 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr, INT calIdx)
2071 RECT *rc = &infoPtr->calendars[calIdx].titleyear;
2072 RECT *title = &infoPtr->calendars[calIdx].title;
2074 infoPtr->hWndYearEdit =
2075 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
2076 rc->left + 3, (title->bottom + title->top - infoPtr->textHeight) / 2,
2077 rc->right - rc->left + 4,
2078 infoPtr->textHeight, infoPtr->hwndSelf,
2079 NULL, NULL, NULL);
2081 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
2083 infoPtr->hWndYearUpDown =
2084 CreateWindowExW(0, UPDOWN_CLASSW, 0,
2085 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
2086 rc->right + 7, (title->bottom + title->top - infoPtr->textHeight) / 2,
2087 18, infoPtr->textHeight, infoPtr->hwndSelf,
2088 NULL, NULL, NULL);
2090 /* attach edit box */
2091 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
2092 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
2093 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
2094 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->calendars[calIdx].month.wYear);
2096 /* subclass edit box */
2097 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
2098 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
2100 SetFocus(infoPtr->hWndYearEdit);
2103 static LRESULT
2104 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2106 MCHITTESTINFO ht;
2107 DWORD hit;
2109 /* Actually we don't need input focus for calendar, this is used to kill
2110 year updown and its buddy edit box */
2111 if (IsWindow(infoPtr->hWndYearUpDown))
2113 SetFocus(infoPtr->hwndSelf);
2114 return 0;
2117 SetCapture(infoPtr->hwndSelf);
2119 ht.cbSize = sizeof(MCHITTESTINFO);
2120 ht.pt.x = (short)LOWORD(lParam);
2121 ht.pt.y = (short)HIWORD(lParam);
2123 hit = MONTHCAL_HitTest(infoPtr, &ht);
2125 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
2127 switch(hit)
2129 case MCHT_TITLEBTNNEXT:
2130 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2131 infoPtr->status = MC_NEXTPRESSED;
2132 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2133 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2134 return 0;
2136 case MCHT_TITLEBTNPREV:
2137 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2138 infoPtr->status = MC_PREVPRESSED;
2139 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2140 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2141 return 0;
2143 case MCHT_TITLEMONTH:
2145 HMENU hMenu = CreatePopupMenu();
2146 WCHAR buf[32];
2147 POINT menupoint;
2148 INT i;
2150 for (i = 0; i < 12; i++)
2152 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
2153 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
2155 menupoint.x = ht.pt.x;
2156 menupoint.y = ht.pt.y;
2157 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2158 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
2159 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
2161 if ((i > 0) && (i < 13) && infoPtr->calendars[ht.iOffset].month.wMonth != i)
2163 INT delta = i - infoPtr->calendars[ht.iOffset].month.wMonth;
2164 SYSTEMTIME st;
2166 /* check if change allowed by range set */
2167 st = delta < 0 ? infoPtr->calendars[0].month :
2168 infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2169 MONTHCAL_GetMonth(&st, delta);
2171 if (MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE))
2173 MONTHCAL_Scroll(infoPtr, delta);
2174 MONTHCAL_NotifyDayState(infoPtr);
2175 MONTHCAL_NotifySelectionChange(infoPtr);
2176 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2179 return 0;
2181 case MCHT_TITLEYEAR:
2183 MONTHCAL_EditYear(infoPtr, ht.iOffset);
2184 return 0;
2186 case MCHT_TODAYLINK:
2188 if (infoPtr->dwStyle & MCS_MULTISELECT)
2190 SYSTEMTIME range[2];
2192 range[0] = range[1] = infoPtr->todaysDate;
2193 MONTHCAL_SetSelRange(infoPtr, range);
2195 else
2196 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2198 MONTHCAL_NotifySelectionChange(infoPtr);
2199 MONTHCAL_NotifySelect(infoPtr);
2200 return 0;
2202 case MCHT_CALENDARDATENEXT:
2203 case MCHT_CALENDARDATEPREV:
2204 case MCHT_CALENDARDATE:
2206 SYSTEMTIME st[2];
2208 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2210 st[0] = st[1] = ht.st;
2211 /* clear selection range */
2212 MONTHCAL_SetSelRange(infoPtr, st);
2214 infoPtr->status = MC_SEL_LBUTDOWN;
2215 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
2216 return 0;
2220 return 1;
2224 static LRESULT
2225 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2227 NMHDR nmhdr;
2228 MCHITTESTINFO ht;
2229 DWORD hit;
2231 TRACE("\n");
2233 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2234 RECT *r;
2236 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2237 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2238 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2240 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2243 ReleaseCapture();
2245 /* always send NM_RELEASEDCAPTURE notification */
2246 nmhdr.hwndFrom = infoPtr->hwndSelf;
2247 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2248 nmhdr.code = NM_RELEASEDCAPTURE;
2249 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2251 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2253 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2255 ht.cbSize = sizeof(MCHITTESTINFO);
2256 ht.pt.x = (short)LOWORD(lParam);
2257 ht.pt.y = (short)HIWORD(lParam);
2258 hit = MONTHCAL_HitTest(infoPtr, &ht);
2260 infoPtr->status = MC_SEL_LBUTUP;
2261 MONTHCAL_SetDayFocus(infoPtr, NULL);
2263 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2265 SYSTEMTIME sel = infoPtr->minSel;
2267 /* will be invalidated here */
2268 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2270 /* send MCN_SELCHANGE only if new date selected */
2271 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2272 MONTHCAL_NotifySelectionChange(infoPtr);
2274 MONTHCAL_NotifySelect(infoPtr);
2277 return 0;
2281 static LRESULT
2282 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2284 TRACE("%ld\n", id);
2286 switch(id) {
2287 case MC_PREVNEXTMONTHTIMER:
2288 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2289 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2290 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2291 break;
2292 case MC_TODAYUPDATETIMER:
2294 SYSTEMTIME st;
2296 if(infoPtr->todaySet) return 0;
2298 GetLocalTime(&st);
2299 MONTHCAL_UpdateToday(infoPtr, &st);
2301 /* notification sent anyway */
2302 MONTHCAL_NotifySelectionChange(infoPtr);
2304 return 0;
2306 default:
2307 ERR("got unknown timer %ld\n", id);
2308 break;
2311 return 0;
2315 static LRESULT
2316 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2318 MCHITTESTINFO ht;
2319 SYSTEMTIME st_ht;
2320 INT hit;
2321 RECT r;
2323 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2325 ht.cbSize = sizeof(MCHITTESTINFO);
2326 ht.pt.x = (short)LOWORD(lParam);
2327 ht.pt.y = (short)HIWORD(lParam);
2328 ht.iOffset = -1;
2330 hit = MONTHCAL_HitTest(infoPtr, &ht);
2332 /* not on the calendar date numbers? bail out */
2333 TRACE("hit:%x\n",hit);
2334 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2336 MONTHCAL_SetDayFocus(infoPtr, NULL);
2337 return 0;
2340 st_ht = ht.st;
2342 /* if pointer is over focused day still there's nothing to do */
2343 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2345 MONTHCAL_GetDayRect(infoPtr, &ht.st, &r, ht.iOffset);
2347 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2348 SYSTEMTIME st[2];
2350 MONTHCAL_GetSelRange(infoPtr, st);
2352 /* If we're still at the first selected date and range is empty, return.
2353 If range isn't empty we should change range to a single firstSel */
2354 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2355 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2357 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2359 st[0] = infoPtr->firstSel;
2360 /* we should overwrite timestamp here */
2361 MONTHCAL_CopyDate(&st_ht, &st[1]);
2363 /* bounds will be swapped here if needed */
2364 MONTHCAL_SetSelRange(infoPtr, st);
2366 return 0;
2369 done:
2371 /* FIXME: this should specify a rectangle containing only the days that changed
2372 using InvalidateRect */
2373 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2375 return 0;
2379 static LRESULT
2380 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2382 HDC hdc;
2383 PAINTSTRUCT ps;
2385 if (hdc_paint)
2387 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2388 hdc = hdc_paint;
2390 else
2391 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2393 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2394 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2395 return 0;
2398 static LRESULT
2399 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2401 RECT rc;
2403 if (!GetClipBox(hdc, &rc)) return FALSE;
2405 FillRect(hdc, &rc, infoPtr->brushes[BrushBackground]);
2407 return TRUE;
2410 static LRESULT
2411 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2413 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2415 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2416 return 0;
2418 if (options & PRF_ERASEBKGND)
2419 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2421 if (options & PRF_CLIENT)
2422 MONTHCAL_Paint(infoPtr, hdc);
2424 return 0;
2427 static LRESULT
2428 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2430 TRACE("\n");
2432 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2434 return 0;
2437 /* sets the size information */
2438 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2440 static const WCHAR O0W[] = { '0','0',0 };
2441 RECT *title=&infoPtr->calendars[0].title;
2442 RECT *prev=&infoPtr->titlebtnprev;
2443 RECT *next=&infoPtr->titlebtnnext;
2444 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2445 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2446 RECT *wdays=&infoPtr->calendars[0].wdays;
2447 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2448 RECT *days=&infoPtr->calendars[0].days;
2449 RECT *todayrect=&infoPtr->todayrect;
2451 INT xdiv, dx, dy, i, j, x, y, c_dx, c_dy;
2452 WCHAR buff[80];
2453 TEXTMETRICW tm;
2454 SIZE size, sz;
2455 RECT client;
2456 HFONT font;
2457 HDC hdc;
2459 GetClientRect(infoPtr->hwndSelf, &client);
2461 hdc = GetDC(infoPtr->hwndSelf);
2462 font = SelectObject(hdc, infoPtr->hFont);
2464 /* get the height and width of each day's text */
2465 GetTextMetricsW(hdc, &tm);
2466 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2468 /* find largest abbreviated day name for current locale */
2469 size.cx = sz.cx = 0;
2470 for (i = 0; i < 7; i++)
2472 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + i,
2473 buff, countof(buff)))
2475 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2476 if (sz.cx > size.cx) size.cx = sz.cx;
2478 else /* locale independent fallback on failure */
2480 static const WCHAR SunW[] = { 'S','u','n',0 };
2482 GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2483 break;
2487 infoPtr->textWidth = size.cx + 2;
2489 /* recalculate the height and width increments and offsets */
2490 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2492 /* restore the originally selected font */
2493 SelectObject(hdc, font);
2494 ReleaseDC(infoPtr->hwndSelf, hdc);
2496 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2498 infoPtr->width_increment = size.cx * 2 + 4;
2499 infoPtr->height_increment = infoPtr->textHeight;
2501 /* calculate title area */
2502 title->top = 0;
2503 title->bottom = 3 * infoPtr->height_increment / 2;
2504 title->left = 0;
2505 title->right = infoPtr->width_increment * xdiv;
2507 /* set the dimensions of the next and previous buttons and center */
2508 /* the month text vertically */
2509 prev->top = next->top = title->top + 4;
2510 prev->bottom = next->bottom = title->bottom - 4;
2511 prev->left = title->left + 4;
2512 prev->right = prev->left + (title->bottom - title->top);
2513 next->right = title->right - 4;
2514 next->left = next->right - (title->bottom - title->top);
2516 /* titlemonth->left and right change based upon the current month
2517 and are recalculated in refresh as the current month may change
2518 without the control being resized */
2519 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2520 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2522 /* week numbers */
2523 weeknumrect->left = 0;
2524 weeknumrect->right = infoPtr->dwStyle & MCS_WEEKNUMBERS ? prev->right : 0;
2526 /* days abbreviated names */
2527 wdays->left = days->left = weeknumrect->right;
2528 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2529 wdays->top = title->bottom;
2530 wdays->bottom = wdays->top + infoPtr->height_increment;
2532 days->top = weeknumrect->top = wdays->bottom;
2533 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2535 todayrect->left = 0;
2536 todayrect->right = title->right;
2537 todayrect->top = days->bottom;
2538 todayrect->bottom = days->bottom + infoPtr->height_increment;
2540 /* compute calendar count, update all calendars */
2541 x = (client.right + MC_CALENDAR_PADDING) / (title->right - title->left + MC_CALENDAR_PADDING);
2542 /* today label affects whole height */
2543 if (infoPtr->dwStyle & MCS_NOTODAY)
2544 y = (client.bottom + MC_CALENDAR_PADDING) / (days->bottom - title->top + MC_CALENDAR_PADDING);
2545 else
2546 y = (client.bottom - todayrect->bottom + todayrect->top + MC_CALENDAR_PADDING) /
2547 (days->bottom - title->top + MC_CALENDAR_PADDING);
2549 /* TODO: ensure that count is properly adjusted to fit 12 months constraint */
2550 if (x == 0) x = 1;
2551 if (y == 0) y = 1;
2553 if (x*y != MONTHCAL_GetCalCount(infoPtr))
2555 infoPtr->dim.cx = x;
2556 infoPtr->dim.cy = y;
2557 infoPtr->calendars = ReAlloc(infoPtr->calendars, MONTHCAL_GetCalCount(infoPtr)*sizeof(CALENDAR_INFO));
2559 infoPtr->monthdayState = ReAlloc(infoPtr->monthdayState,
2560 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
2561 MONTHCAL_NotifyDayState(infoPtr);
2563 /* update pointers that we'll need */
2564 title = &infoPtr->calendars[0].title;
2565 wdays = &infoPtr->calendars[0].wdays;
2566 days = &infoPtr->calendars[0].days;
2569 for (i = 1; i < MONTHCAL_GetCalCount(infoPtr); i++)
2571 /* set months */
2572 infoPtr->calendars[i] = infoPtr->calendars[0];
2573 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, i);
2576 /* offset all rectangles to center in client area */
2577 c_dx = (client.right - x * title->right - MC_CALENDAR_PADDING * (x-1)) / 2;
2578 c_dy = (client.bottom - y * todayrect->bottom - MC_CALENDAR_PADDING * (y-1)) / 2;
2580 /* if calendar doesn't fit client area show it at left/top bounds */
2581 if (title->left + c_dx < 0) c_dx = 0;
2582 if (title->top + c_dy < 0) c_dy = 0;
2584 for (i = 0; i < y; i++)
2586 for (j = 0; j < x; j++)
2588 dx = j*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx;
2589 dy = i*(days->bottom - title->top + MC_CALENDAR_PADDING) + c_dy;
2591 OffsetRect(&infoPtr->calendars[i*x+j].title, dx, dy);
2592 OffsetRect(&infoPtr->calendars[i*x+j].titlemonth, dx, dy);
2593 OffsetRect(&infoPtr->calendars[i*x+j].titleyear, dx, dy);
2594 OffsetRect(&infoPtr->calendars[i*x+j].wdays, dx, dy);
2595 OffsetRect(&infoPtr->calendars[i*x+j].weeknums, dx, dy);
2596 OffsetRect(&infoPtr->calendars[i*x+j].days, dx, dy);
2600 OffsetRect(prev, c_dx, c_dy);
2601 OffsetRect(next, (x-1)*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx, c_dy);
2603 i = infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx;
2604 todayrect->left = infoPtr->calendars[i].title.left;
2605 todayrect->right = infoPtr->calendars[i].title.right;
2606 todayrect->top = infoPtr->calendars[i].days.bottom;
2607 todayrect->bottom = infoPtr->calendars[i].days.bottom + infoPtr->height_increment;
2609 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2610 infoPtr->width_increment,infoPtr->height_increment,
2611 wine_dbgstr_rect(&client),
2612 wine_dbgstr_rect(title),
2613 wine_dbgstr_rect(wdays),
2614 wine_dbgstr_rect(days),
2615 wine_dbgstr_rect(todayrect));
2618 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2620 TRACE("(width=%d, height=%d)\n", Width, Height);
2622 MONTHCAL_UpdateSize(infoPtr);
2623 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2625 return 0;
2628 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2630 return (LRESULT)infoPtr->hFont;
2633 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2635 HFONT hOldFont;
2636 LOGFONTW lf;
2638 if (!hFont) return 0;
2640 hOldFont = infoPtr->hFont;
2641 infoPtr->hFont = hFont;
2643 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2644 lf.lfWeight = FW_BOLD;
2645 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2647 MONTHCAL_UpdateSize(infoPtr);
2649 if (redraw)
2650 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2652 return (LRESULT)hOldFont;
2655 /* update theme after a WM_THEMECHANGED message */
2656 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2658 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2659 CloseThemeData (theme);
2660 OpenThemeData (infoPtr->hwndSelf, themeClass);
2661 return 0;
2664 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2665 const STYLESTRUCT *lpss)
2667 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2668 wStyleType, lpss->styleOld, lpss->styleNew);
2670 if (wStyleType != GWL_STYLE) return 0;
2672 infoPtr->dwStyle = lpss->styleNew;
2674 /* make room for week numbers */
2675 if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2676 MONTHCAL_UpdateSize(infoPtr);
2678 return 0;
2681 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2682 STYLESTRUCT *lpss)
2684 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2685 wStyleType, lpss->styleOld, lpss->styleNew);
2687 /* block MCS_MULTISELECT change */
2688 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2690 if (lpss->styleOld & MCS_MULTISELECT)
2691 lpss->styleNew |= MCS_MULTISELECT;
2692 else
2693 lpss->styleNew &= ~MCS_MULTISELECT;
2696 /* block MCS_DAYSTATE change */
2697 if ((lpss->styleNew ^ lpss->styleOld) & MCS_DAYSTATE)
2699 if (lpss->styleOld & MCS_DAYSTATE)
2700 lpss->styleNew |= MCS_DAYSTATE;
2701 else
2702 lpss->styleNew &= ~MCS_DAYSTATE;
2705 return 0;
2708 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2709 static LRESULT
2710 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2712 MONTHCAL_INFO *infoPtr;
2714 /* allocate memory for info structure */
2715 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2716 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2718 if (infoPtr == NULL) {
2719 ERR("could not allocate info memory!\n");
2720 return 0;
2723 infoPtr->hwndSelf = hwnd;
2724 infoPtr->hwndNotify = lpcs->hwndParent;
2725 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2726 infoPtr->dim.cx = infoPtr->dim.cy = 1;
2727 infoPtr->calendars = Alloc(sizeof(CALENDAR_INFO));
2728 if (!infoPtr->calendars) goto fail;
2729 infoPtr->monthdayState = Alloc(3*sizeof(MONTHDAYSTATE));
2730 if (!infoPtr->monthdayState) goto fail;
2732 /* initialize info structure */
2733 /* FIXME: calculate systemtime ->> localtime(subtract timezoneinfo) */
2735 GetLocalTime(&infoPtr->todaysDate);
2736 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2738 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2740 infoPtr->colors[MCSC_BACKGROUND] = comctl32_color.clrWindow;
2741 infoPtr->colors[MCSC_TEXT] = comctl32_color.clrWindowText;
2742 infoPtr->colors[MCSC_TITLEBK] = comctl32_color.clrActiveCaption;
2743 infoPtr->colors[MCSC_TITLETEXT] = comctl32_color.clrWindow;
2744 infoPtr->colors[MCSC_MONTHBK] = comctl32_color.clrWindow;
2745 infoPtr->colors[MCSC_TRAILINGTEXT] = comctl32_color.clrGrayText;
2747 infoPtr->brushes[BrushBackground] = CreateSolidBrush(infoPtr->colors[MCSC_BACKGROUND]);
2748 infoPtr->brushes[BrushTitle] = CreateSolidBrush(infoPtr->colors[MCSC_TITLEBK]);
2749 infoPtr->brushes[BrushMonth] = CreateSolidBrush(infoPtr->colors[MCSC_MONTHBK]);
2751 infoPtr->pens[PenRed] = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
2752 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[MCSC_TEXT]);
2754 infoPtr->minSel = infoPtr->todaysDate;
2755 infoPtr->maxSel = infoPtr->todaysDate;
2756 infoPtr->calendars[0].month = infoPtr->todaysDate;
2757 infoPtr->isUnicode = TRUE;
2759 /* setup control layout and day state data */
2760 MONTHCAL_UpdateSize(infoPtr);
2762 /* today auto update timer, to be freed only on control destruction */
2763 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2765 OpenThemeData (infoPtr->hwndSelf, themeClass);
2767 return 0;
2769 fail:
2770 Free(infoPtr->monthdayState);
2771 Free(infoPtr->calendars);
2772 Free(infoPtr);
2773 return 0;
2776 static LRESULT
2777 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2779 INT i;
2781 /* free month calendar info data */
2782 Free(infoPtr->monthdayState);
2783 Free(infoPtr->calendars);
2784 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2786 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2788 for (i = 0; i < BrushLast; i++) DeleteObject(infoPtr->brushes[i]);
2789 for (i = 0; i < PenLast; i++) DeleteObject(infoPtr->pens[i]);
2791 Free(infoPtr);
2792 return 0;
2796 * Handler for WM_NOTIFY messages
2798 static LRESULT
2799 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2801 /* notification from year edit updown */
2802 if (hdr->code == UDN_DELTAPOS)
2804 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2806 if (hdr->hwndFrom == infoPtr->hWndYearUpDown && nmud->iDelta)
2808 /* year value limits are set up explicitly after updown creation */
2809 MONTHCAL_Scroll(infoPtr, 12 * nmud->iDelta);
2810 MONTHCAL_NotifyDayState(infoPtr);
2811 MONTHCAL_NotifySelectionChange(infoPtr);
2814 return 0;
2817 static inline BOOL
2818 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2820 BOOL prev = infoPtr->isUnicode;
2821 infoPtr->isUnicode = isUnicode;
2822 return prev;
2825 static inline BOOL
2826 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2828 return infoPtr->isUnicode;
2831 static LRESULT WINAPI
2832 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2834 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0);
2836 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2838 if (!infoPtr && (uMsg != WM_CREATE))
2839 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2840 switch(uMsg)
2842 case MCM_GETCURSEL:
2843 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2845 case MCM_SETCURSEL:
2846 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2848 case MCM_GETMAXSELCOUNT:
2849 return MONTHCAL_GetMaxSelCount(infoPtr);
2851 case MCM_SETMAXSELCOUNT:
2852 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2854 case MCM_GETSELRANGE:
2855 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2857 case MCM_SETSELRANGE:
2858 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2860 case MCM_GETMONTHRANGE:
2861 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2863 case MCM_SETDAYSTATE:
2864 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2866 case MCM_GETMINREQRECT:
2867 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2869 case MCM_GETCOLOR:
2870 return MONTHCAL_GetColor(infoPtr, wParam);
2872 case MCM_SETCOLOR:
2873 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2875 case MCM_GETTODAY:
2876 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2878 case MCM_SETTODAY:
2879 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2881 case MCM_HITTEST:
2882 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2884 case MCM_GETFIRSTDAYOFWEEK:
2885 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2887 case MCM_SETFIRSTDAYOFWEEK:
2888 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2890 case MCM_GETRANGE:
2891 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2893 case MCM_SETRANGE:
2894 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2896 case MCM_GETMONTHDELTA:
2897 return MONTHCAL_GetMonthDelta(infoPtr);
2899 case MCM_SETMONTHDELTA:
2900 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2902 case MCM_GETMAXTODAYWIDTH:
2903 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2905 case MCM_SETUNICODEFORMAT:
2906 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2908 case MCM_GETUNICODEFORMAT:
2909 return MONTHCAL_GetUnicodeFormat(infoPtr);
2911 case MCM_GETCALENDARCOUNT:
2912 return MONTHCAL_GetCalCount(infoPtr);
2914 case WM_GETDLGCODE:
2915 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2917 case WM_RBUTTONUP:
2918 return MONTHCAL_RButtonUp(infoPtr, lParam);
2920 case WM_LBUTTONDOWN:
2921 return MONTHCAL_LButtonDown(infoPtr, lParam);
2923 case WM_MOUSEMOVE:
2924 return MONTHCAL_MouseMove(infoPtr, lParam);
2926 case WM_LBUTTONUP:
2927 return MONTHCAL_LButtonUp(infoPtr, lParam);
2929 case WM_PAINT:
2930 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2932 case WM_PRINTCLIENT:
2933 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2935 case WM_ERASEBKGND:
2936 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2938 case WM_SETFOCUS:
2939 return MONTHCAL_SetFocus(infoPtr);
2941 case WM_SIZE:
2942 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2944 case WM_NOTIFY:
2945 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2947 case WM_CREATE:
2948 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2950 case WM_SETFONT:
2951 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2953 case WM_GETFONT:
2954 return MONTHCAL_GetFont(infoPtr);
2956 case WM_TIMER:
2957 return MONTHCAL_Timer(infoPtr, wParam);
2959 case WM_THEMECHANGED:
2960 return theme_changed (infoPtr);
2962 case WM_DESTROY:
2963 return MONTHCAL_Destroy(infoPtr);
2965 case WM_SYSCOLORCHANGE:
2966 COMCTL32_RefreshSysColors();
2967 return 0;
2969 case WM_STYLECHANGED:
2970 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2972 case WM_STYLECHANGING:
2973 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2975 default:
2976 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2977 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2978 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2983 void
2984 MONTHCAL_Register(void)
2986 WNDCLASSW wndClass;
2988 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2989 wndClass.style = CS_GLOBALCLASS;
2990 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2991 wndClass.cbClsExtra = 0;
2992 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2993 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2994 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2995 wndClass.lpszClassName = MONTHCAL_CLASSW;
2997 RegisterClassW(&wndClass);
3001 void
3002 MONTHCAL_Unregister(void)
3004 UnregisterClassW(MONTHCAL_CLASSW, NULL);