comctl32/monthcal: Fix today date box painting.
[wine.git] / dlls / comctl32 / monthcal.c
blob762a7926f2856ed3554cdeb8ab196f507223b1d0
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 countof(arr) (sizeof(arr)/sizeof(arr[0]))
76 /* convert from days to 100 nanoseconds unit - used as FILETIME unit */
77 #define DAYSTO100NSECS(days) (((ULONGLONG)(days))*24*60*60*10000000)
79 enum CachedPen
81 PenRed = 0,
82 PenText,
83 PenLast
86 enum CachedBrush
88 BrushTitle = 0,
89 BrushMonth,
90 BrushBackground,
91 BrushLast
94 /* single calendar data */
95 typedef struct _CALENDAR_INFO
97 RECT title; /* rect for the header above the calendar */
98 RECT titlemonth; /* the 'month name' text in the header */
99 RECT titleyear; /* the 'year number' text in the header */
100 RECT wdays; /* week days at top */
101 RECT days; /* calendar area */
102 RECT weeknums; /* week numbers at left side */
104 SYSTEMTIME month;/* contains calendar main month/year */
105 } CALENDAR_INFO;
107 typedef struct
109 HWND hwndSelf;
110 DWORD dwStyle; /* cached GWL_STYLE */
112 COLORREF colors[MCSC_TRAILINGTEXT+1];
113 HBRUSH brushes[BrushLast];
114 HPEN pens[PenLast];
116 HFONT hFont;
117 HFONT hBoldFont;
118 int textHeight;
119 int textWidth;
120 int height_increment;
121 int width_increment;
122 INT delta; /* scroll rate; # of months that the */
123 /* control moves when user clicks a scroll button */
124 int visible; /* # of months visible */
125 int firstDay; /* Start month calendar with firstDay's day,
126 stored in SYSTEMTIME format */
127 BOOL firstDaySet; /* first week day differs from locale defined */
129 BOOL isUnicode; /* value set with MCM_SETUNICODE format */
131 int monthRange;
132 MONTHDAYSTATE *monthdayState;
133 SYSTEMTIME todaysDate;
134 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
135 int status; /* See MC_SEL flags */
136 SYSTEMTIME firstSel; /* first selected day */
137 INT maxSelCount;
138 SYSTEMTIME minSel; /* contains single selection when used without MCS_MULTISELECT */
139 SYSTEMTIME maxSel;
140 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
141 DWORD rangeValid;
142 SYSTEMTIME minDate;
143 SYSTEMTIME maxDate;
145 RECT titlebtnnext; /* the `next month' button in the header */
146 RECT titlebtnprev; /* the `prev month' button in the header */
147 RECT todayrect; /* `today: xx/xx/xx' text rect */
148 HWND hwndNotify; /* Window to receive the notifications */
149 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
150 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
151 WNDPROC EditWndProc; /* original Edit window procedure */
153 CALENDAR_INFO *calendars;
154 SIZE dim; /* [cx,cy] - dimensions of calendars matrix, row/column count */
155 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
157 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
159 /* empty SYSTEMTIME const */
160 static const SYSTEMTIME st_null;
161 /* valid date limits */
162 static const SYSTEMTIME max_allowed_date = { .wYear = 9999, .wMonth = 12, .wDay = 31 };
163 static const SYSTEMTIME min_allowed_date = { .wYear = 1752, .wMonth = 9, .wDay = 14 };
165 /* Prev/Next buttons */
166 enum nav_direction
168 DIRECTION_BACKWARD,
169 DIRECTION_FORWARD
172 /* helper functions */
173 static inline INT MONTHCAL_GetCalCount(const MONTHCAL_INFO *infoPtr)
175 return infoPtr->dim.cx * infoPtr->dim.cy;
178 /* send a single MCN_SELCHANGE notification */
179 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
181 NMSELCHANGE nmsc;
183 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
184 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
185 nmsc.nmhdr.code = MCN_SELCHANGE;
186 nmsc.stSelStart = infoPtr->minSel;
187 nmsc.stSelEnd = infoPtr->maxSel;
188 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
191 /* send a single MCN_SELECT notification */
192 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
194 NMSELCHANGE nmsc;
196 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
197 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
198 nmsc.nmhdr.code = MCN_SELECT;
199 nmsc.stSelStart = infoPtr->minSel;
200 nmsc.stSelEnd = infoPtr->maxSel;
202 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
205 static inline int MONTHCAL_MonthDiff(const SYSTEMTIME *left, const SYSTEMTIME *right)
207 return (right->wYear - left->wYear)*12 + right->wMonth - left->wMonth;
210 /* returns the number of days in any given month, checking for leap days */
211 /* January is 1, December is 12 */
212 int MONTHCAL_MonthLength(int month, int year)
214 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
215 /* Wrap around, this eases handling. Getting length only we shouldn't care
216 about year change here cause January and December have
217 the same day quantity */
218 if(month == 0)
219 month = 12;
220 else if(month == 13)
221 month = 1;
223 /* special case for calendar transition year */
224 if(month == min_allowed_date.wMonth && year == min_allowed_date.wYear) return 19;
226 /* if we have a leap year add 1 day to February */
227 /* a leap year is a year either divisible by 400 */
228 /* or divisible by 4 and not by 100 */
229 if(month == 2) { /* February */
230 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
231 (year%4 == 0)) ? 1 : 0);
233 else {
234 return mdays[month - 1];
238 /* compares timestamps using date part only */
239 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
241 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
242 (first->wDay == second->wDay);
245 /* make sure that date fields are valid */
246 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
248 if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
249 if(time->wDayOfWeek > 6) return FALSE;
250 if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear))
251 return FALSE;
253 return TRUE;
256 /* Copies timestamp part only.
258 * PARAMETERS
260 * [I] from : source date
261 * [O] to : dest date
263 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
265 to->wHour = from->wHour;
266 to->wMinute = from->wMinute;
267 to->wSecond = from->wSecond;
270 /* Copies date part only.
272 * PARAMETERS
274 * [I] from : source date
275 * [O] to : dest date
277 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
279 to->wYear = from->wYear;
280 to->wMonth = from->wMonth;
281 to->wDay = from->wDay;
282 to->wDayOfWeek = from->wDayOfWeek;
285 /* Compares two dates in SYSTEMTIME format
287 * PARAMETERS
289 * [I] first : pointer to valid first date data to compare
290 * [I] second : pointer to valid second date data to compare
292 * RETURN VALUE
294 * -1 : first < second
295 * 0 : first == second
296 * 1 : first > second
298 * Note that no date validation performed, already validated values expected.
300 static LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
302 FILETIME ft_first, ft_second;
304 SystemTimeToFileTime(first, &ft_first);
305 SystemTimeToFileTime(second, &ft_second);
307 return CompareFileTime(&ft_first, &ft_second);
310 static LONG MONTHCAL_CompareMonths(const SYSTEMTIME *first, const SYSTEMTIME *second)
312 SYSTEMTIME st_first, st_second;
314 st_first = st_second = st_null;
315 MONTHCAL_CopyDate(first, &st_first);
316 MONTHCAL_CopyDate(second, &st_second);
317 st_first.wDay = st_second.wDay = 1;
319 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
322 static LONG MONTHCAL_CompareDate(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);
330 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
333 /* Checks largest possible date range and configured one
335 * PARAMETERS
337 * [I] infoPtr : valid pointer to control data
338 * [I] date : pointer to valid date data to check
339 * [I] fix : make date fit valid range
341 * RETURN VALUE
343 * TRUE - date within largest and configured range
344 * FALSE - date is outside largest or configured range
346 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr,
347 SYSTEMTIME *date, BOOL fix)
349 const SYSTEMTIME *fix_st = NULL;
351 if(MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) {
352 fix_st = &max_allowed_date;
354 else if(MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1) {
355 fix_st = &min_allowed_date;
357 else if(infoPtr->rangeValid & GDTR_MAX) {
358 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
359 fix_st = &infoPtr->maxDate;
362 else if(infoPtr->rangeValid & GDTR_MIN) {
363 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
364 fix_st = &infoPtr->minDate;
368 if (fix && fix_st) {
369 date->wYear = fix_st->wYear;
370 date->wMonth = fix_st->wMonth;
373 return fix_st ? FALSE : TRUE;
376 /* Checks passed range width with configured maximum selection count
378 * PARAMETERS
380 * [I] infoPtr : valid pointer to control data
381 * [I] range0 : pointer to valid date data (requested bound)
382 * [I] range1 : pointer to valid date data (primary bound)
383 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
385 * Adjust value computed basing on primary bound and current maximum selection
386 * count. For simple range check (without adjusted value required) (range0, range1)
387 * relation means nothing.
389 * RETURN VALUE
391 * TRUE - range is shorter or equal to maximum
392 * FALSE - range is larger than maximum
394 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
395 const SYSTEMTIME *range0,
396 const SYSTEMTIME *range1,
397 SYSTEMTIME *adjust)
399 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
400 FILETIME ft_range0, ft_range1;
401 LONG cmp;
403 SystemTimeToFileTime(range0, &ft_range0);
404 SystemTimeToFileTime(range1, &ft_range1);
406 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
407 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
408 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
409 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
411 cmp = CompareFileTime(&ft_range0, &ft_range1);
413 if(cmp == 1)
414 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
415 else
416 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
418 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
420 if(adjust) {
421 if(cmp == 1)
422 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
423 else
424 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
426 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
427 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
428 FileTimeToSystemTime(&ft_range0, adjust);
431 return FALSE;
433 else return TRUE;
436 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
437 Milliseconds are intentionally not validated. */
438 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
440 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
441 return FALSE;
442 else
443 return TRUE;
446 /* Note:Depending on DST, this may be offset by a day.
447 Need to find out if we're on a DST place & adjust the clock accordingly.
448 Above function assumes we have a valid data.
449 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
450 0 = Sunday.
453 /* Returns the day in the week
455 * PARAMETERS
456 * [i] date : input date
457 * [I] inplace : set calculated value back to date structure
459 * RETURN VALUE
460 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
462 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
464 SYSTEMTIME st = st_null;
465 FILETIME ft;
467 MONTHCAL_CopyDate(date, &st);
469 SystemTimeToFileTime(&st, &ft);
470 FileTimeToSystemTime(&ft, &st);
472 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
474 return st.wDayOfWeek;
477 /* add/subtract 'months' from date */
478 static inline void MONTHCAL_GetMonth(SYSTEMTIME *date, INT months)
480 INT length, m = date->wMonth + months;
482 date->wYear += m > 0 ? (m - 1) / 12 : m / 12 - 1;
483 date->wMonth = m > 0 ? (m - 1) % 12 + 1 : 12 + m % 12;
484 /* fix moving from last day in a month */
485 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
486 if(date->wDay > length) date->wDay = length;
487 MONTHCAL_CalculateDayOfWeek(date, TRUE);
490 /* properly updates date to point on next month */
491 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
493 MONTHCAL_GetMonth(date, 1);
496 /* properly updates date to point on prev month */
497 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
499 MONTHCAL_GetMonth(date, -1);
502 /* Returns full date for a first currently visible day */
503 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
505 /* zero indexed calendar has the earliest date */
506 SYSTEMTIME st_first = infoPtr->calendars[0].month;
507 INT firstDay;
509 st_first.wDay = 1;
510 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
512 *date = infoPtr->calendars[0].month;
513 MONTHCAL_GetPrevMonth(date);
515 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
516 (infoPtr->firstDay - firstDay) % 7 + 1;
518 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
519 date->wDay -= 7;
521 /* fix day of week */
522 MONTHCAL_CalculateDayOfWeek(date, TRUE);
525 /* Returns full date for a last currently visible day */
526 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
528 /* the latest date is in latest calendar */
529 SYSTEMTIME st, *lt_month = &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
530 INT first_day;
532 *date = *lt_month;
533 st = *lt_month;
535 /* day of week of first day of current month */
536 st.wDay = 1;
537 first_day = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
539 MONTHCAL_GetNextMonth(date);
540 MONTHCAL_GetPrevMonth(&st);
542 /* last calendar starts with some date from previous month that not displayed */
543 st.wDay = MONTHCAL_MonthLength(st.wMonth, st.wYear) +
544 (infoPtr->firstDay - first_day) % 7 + 1;
545 if (st.wDay > MONTHCAL_MonthLength(st.wMonth, st.wYear)) st.wDay -= 7;
547 /* Use month length to get max day. 42 means max day count in calendar area */
548 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
549 MONTHCAL_MonthLength(lt_month->wMonth, lt_month->wYear);
551 /* fix day of week */
552 MONTHCAL_CalculateDayOfWeek(date, TRUE);
555 /* From a given point calculate the row, column and day in the calendar,
556 'day == 0' means the last day of the last month. */
557 static int MONTHCAL_GetDayFromPos(const MONTHCAL_INFO *infoPtr, POINT pt, INT calIdx)
559 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
560 int firstDay, col, row;
561 RECT client;
563 GetClientRect(infoPtr->hwndSelf, &client);
565 /* if the point is outside the x bounds of the window put it at the boundary */
566 if (pt.x > client.right) pt.x = client.right;
568 col = (pt.x - infoPtr->calendars[calIdx].days.left ) / infoPtr->width_increment;
569 row = (pt.y - infoPtr->calendars[calIdx].days.top ) / infoPtr->height_increment;
571 st.wDay = 1;
572 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
573 return col + 7 * row - firstDay;
576 /* Get day position for given date and calendar
578 * PARAMETERS
580 * [I] infoPtr : pointer to control data
581 * [I] date : date value
582 * [O] col : day column (zero based)
583 * [O] row : week column (zero based)
584 * [I] calIdx : calendar index
586 static void MONTHCAL_GetDayPos(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
587 INT *col, INT *row, INT calIdx)
589 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
590 INT first;
592 st.wDay = 1;
593 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
595 if (calIdx == 0 || calIdx == MONTHCAL_GetCalCount(infoPtr)-1) {
596 const SYSTEMTIME *cal = &infoPtr->calendars[calIdx].month;
597 LONG cmp = MONTHCAL_CompareMonths(date, &st);
599 /* previous month */
600 if (cmp == -1) {
601 *col = (first - MONTHCAL_MonthLength(date->wMonth, cal->wYear) + date->wDay) % 7;
602 *row = 0;
603 return;
606 /* next month calculation is same as for current, just add current month length */
607 if (cmp == 1)
608 first += MONTHCAL_MonthLength(cal->wMonth, cal->wYear);
611 *col = (date->wDay + first) % 7;
612 *row = (date->wDay + first - *col) / 7;
615 /* returns bounding box for day in given position in given calendar */
616 static inline void MONTHCAL_GetDayRectI(const MONTHCAL_INFO *infoPtr, RECT *r,
617 INT col, INT row, INT calIdx)
619 r->left = infoPtr->calendars[calIdx].days.left + col * infoPtr->width_increment;
620 r->right = r->left + infoPtr->width_increment;
621 r->top = infoPtr->calendars[calIdx].days.top + row * infoPtr->height_increment;
622 r->bottom = r->top + infoPtr->textHeight;
625 /* Returns bounding box for given date
627 * NOTE: when calendar index is unknown pass -1
629 static inline void MONTHCAL_GetDayRect(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
630 RECT *r, INT calIdx)
632 INT col, row;
634 if (calIdx == -1)
636 INT cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[0].month);
638 if (cmp <= 0)
639 calIdx = 0;
640 else
642 cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month);
643 if (cmp >= 0)
644 calIdx = MONTHCAL_GetCalCount(infoPtr)-1;
645 else
647 for (calIdx = 1; calIdx < MONTHCAL_GetCalCount(infoPtr)-1; calIdx++)
648 if (MONTHCAL_CompareMonths(date, &infoPtr->calendars[calIdx].month) == 0)
649 break;
654 MONTHCAL_GetDayPos(infoPtr, date, &col, &row, calIdx);
655 MONTHCAL_GetDayRectI(infoPtr, r, col, row, calIdx);
658 /* Focused day helper:
660 - set focused date to given value;
661 - reset to zero value if NULL passed;
662 - invalidate previous and new day rectangle only if needed.
664 Returns TRUE if focused day changed, FALSE otherwise.
666 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
668 RECT r;
670 if(st)
672 /* there's nothing to do if it's the same date,
673 mouse move within same date rectangle case */
674 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
676 /* invalidate old focused day */
677 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
678 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
680 infoPtr->focusedSel = *st;
683 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
685 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
686 infoPtr->focusedSel = st_null;
688 /* on set invalidates new day, on reset clears previous focused day */
689 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
691 return TRUE;
694 /* draw today boundary box for specified rectangle */
695 static void MONTHCAL_Circle(const MONTHCAL_INFO *infoPtr, HDC hdc, const RECT *r)
697 HPEN old_pen = SelectObject(hdc, infoPtr->pens[PenRed]);
698 HBRUSH old_brush;
700 old_brush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
701 Rectangle(hdc, r->left, r->top, r->right, r->bottom);
703 SelectObject(hdc, old_brush);
704 SelectObject(hdc, old_pen);
707 /* Draw today day mark rectangle
709 * [I] hdc : context to draw in
710 * [I] date : day to mark with rectangle
713 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
714 const SYSTEMTIME *date)
716 RECT r;
718 MONTHCAL_GetDayRect(infoPtr, date, &r, -1);
719 MONTHCAL_Circle(infoPtr, hdc, &r);
722 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
723 int bold, const PAINTSTRUCT *ps)
725 static const WCHAR fmtW[] = { '%','d',0 };
726 WCHAR buf[10];
727 RECT r, r_temp;
728 COLORREF oldCol = 0;
729 COLORREF oldBk = 0;
730 INT old_bkmode, selection;
732 /* no need to check styles: when selection is not valid, it is set to zero.
733 1 < day < 31, so everything is OK */
734 MONTHCAL_GetDayRect(infoPtr, st, &r, -1);
735 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
737 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
738 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0))
740 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
741 TRACE("%s\n", wine_dbgstr_rect(&r));
742 oldCol = SetTextColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
743 oldBk = SetBkColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
744 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
746 selection = 1;
748 else
749 selection = 0;
751 SelectObject(hdc, bold ? infoPtr->hBoldFont : infoPtr->hFont);
753 old_bkmode = SetBkMode(hdc, TRANSPARENT);
754 wsprintfW(buf, fmtW, st->wDay);
755 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
756 SetBkMode(hdc, old_bkmode);
758 if (selection)
760 SetTextColor(hdc, oldCol);
761 SetBkColor(hdc, oldBk);
765 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, enum nav_direction button)
767 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
768 RECT *r = button == DIRECTION_FORWARD ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
769 BOOL pressed = button == DIRECTION_FORWARD ? infoPtr->status & MC_NEXTPRESSED :
770 infoPtr->status & MC_PREVPRESSED;
771 if (theme)
773 static const int states[] = {
774 /* Prev button */
775 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
776 /* Next button */
777 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
779 int stateNum = button == DIRECTION_FORWARD ? 3 : 0;
780 if (pressed)
781 stateNum += 1;
782 else
784 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
786 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
788 else
790 int style = button == DIRECTION_FORWARD ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
791 if (pressed)
792 style |= DFCS_PUSHED;
793 else
795 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
798 DrawFrameControl(hdc, r, DFC_SCROLL, style);
802 /* paint a title with buttons and month/year string */
803 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
805 static const WCHAR fmt_monthW[] = { '%','s',' ','%','l','d',0 };
806 RECT *title = &infoPtr->calendars[calIdx].title;
807 const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
808 WCHAR buf_month[80], buf_fmt[80];
809 SIZE sz;
811 /* fill header box */
812 FillRect(hdc, title, infoPtr->brushes[BrushTitle]);
814 /* month/year string */
815 SetBkColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
816 SetTextColor(hdc, infoPtr->colors[MCSC_TITLETEXT]);
817 SelectObject(hdc, infoPtr->hBoldFont);
819 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1 + st->wMonth - 1,
820 buf_month, countof(buf_month));
822 wsprintfW(buf_fmt, fmt_monthW, buf_month, st->wYear);
823 DrawTextW(hdc, buf_fmt, strlenW(buf_fmt), title,
824 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
826 /* update title rectangles with current month - used while testing hits */
827 GetTextExtentPoint32W(hdc, buf_fmt, strlenW(buf_fmt), &sz);
828 infoPtr->calendars[calIdx].titlemonth.left = title->right / 2 + title->left / 2 - sz.cx / 2;
829 infoPtr->calendars[calIdx].titleyear.right = title->right / 2 + title->left / 2 + sz.cx / 2;
831 GetTextExtentPoint32W(hdc, buf_month, strlenW(buf_month), &sz);
832 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
833 infoPtr->calendars[calIdx].titleyear.left = infoPtr->calendars[calIdx].titlemonth.right;
836 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
838 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
839 static const WCHAR fmt_weekW[] = { '%','d',0 };
840 INT mindays, weeknum, weeknum1, startofprescal;
841 INT i, prev_month;
842 SYSTEMTIME st;
843 WCHAR buf[80];
844 HPEN old_pen;
845 RECT r;
847 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
849 MONTHCAL_GetMinDate(infoPtr, &st);
850 startofprescal = st.wDay;
851 st = *date;
853 prev_month = date->wMonth - 1;
854 if(prev_month == 0) prev_month = 12;
857 Rules what week to call the first week of a new year:
858 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
859 The week containing Jan 1 is the first week of year
860 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
861 First week of year must contain 4 days of the new year
862 LOCALE_IFIRSTWEEKOFYEAR == 1 (what countries?)
863 The first week of the year must contain only days of the new year
865 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
866 weeknum = atoiW(buf);
867 switch (weeknum)
869 case 1: mindays = 6;
870 break;
871 case 2: mindays = 3;
872 break;
873 case 0: mindays = 0;
874 break;
875 default:
876 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
877 mindays = 0;
880 if (date->wMonth == 1)
882 /* calculate all those exceptions for January */
883 st.wDay = st.wMonth = 1;
884 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
885 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
886 weeknum = 1;
887 else
889 weeknum = 0;
890 for(i = 0; i < 11; i++)
891 weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
893 weeknum += startofprescal + 7;
894 weeknum /= 7;
895 st.wYear -= 1;
896 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
897 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
900 else
902 weeknum = 0;
903 for(i = 0; i < prev_month - 1; i++)
904 weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
906 weeknum += startofprescal + 7;
907 weeknum /= 7;
908 st.wDay = st.wMonth = 1;
909 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
910 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
913 r = infoPtr->calendars[calIdx].weeknums;
915 /* erase whole week numbers area */
916 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
917 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
919 /* reduce rectangle to one week number */
920 r.bottom = r.top + infoPtr->height_increment;
922 for(i = 0; i < 6; i++) {
923 if((i == 0) && (weeknum > 50))
925 wsprintfW(buf, fmt_weekW, weeknum);
926 weeknum = 0;
928 else if((i == 5) && (weeknum > 47))
930 wsprintfW(buf, fmt_weekW, 1);
932 else
933 wsprintfW(buf, fmt_weekW, weeknum + i);
935 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
936 OffsetRect(&r, 0, infoPtr->height_increment);
939 /* line separator for week numbers column */
940 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
941 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
942 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
943 SelectObject(hdc, old_pen);
946 /* bottom today date */
947 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
949 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
950 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
951 RECT text_rect, box_rect;
952 HFONT old_font;
953 INT col;
955 if(infoPtr->dwStyle & MCS_NOTODAY) return;
957 if (!LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, countof(buf_todayW)))
959 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
960 WARN("Can't load resource\n");
961 strcpyW(buf_todayW, todayW);
964 col = infoPtr->dwStyle & MCS_NOTODAYCIRCLE ? 0 : 1;
965 if (infoPtr->dwStyle & MCS_WEEKNUMBERS) col--;
966 /* label is located below first calendar last row */
967 MONTHCAL_GetDayRectI(infoPtr, &text_rect, col, 6, infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx);
968 box_rect = text_rect;
970 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
971 buf_dateW, countof(buf_dateW));
972 old_font = SelectObject(hdc, infoPtr->hBoldFont);
973 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
975 wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
976 DrawTextW(hdc, buf, -1, &text_rect, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
977 DrawTextW(hdc, buf, -1, &text_rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
979 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
980 OffsetRect(&box_rect, -infoPtr->width_increment, 0);
981 MONTHCAL_Circle(infoPtr, hdc, &box_rect);
984 SelectObject(hdc, old_font);
987 /* today mark + focus */
988 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
990 /* circle today date if only it's in fully visible month */
991 if (!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
993 INT i;
995 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
996 if (!MONTHCAL_CompareMonths(&infoPtr->todaysDate, &infoPtr->calendars[i].month))
998 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
999 break;
1003 if (!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
1005 RECT r;
1006 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
1007 DrawFocusRect(hdc, &r);
1011 /* months before first calendar month and after last calendar month */
1012 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1014 INT mask, length;
1015 SYSTEMTIME st_max, st;
1017 if (infoPtr->dwStyle & MCS_NOTRAILINGDATES) return;
1019 SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
1021 /* draw prev month */
1022 MONTHCAL_GetMinDate(infoPtr, &st);
1023 mask = 1 << (st.wDay-1);
1024 /* December and January both 31 days long, so no worries if wrapped */
1025 length = MONTHCAL_MonthLength(infoPtr->calendars[0].month.wMonth - 1,
1026 infoPtr->calendars[0].month.wYear);
1027 while(st.wDay <= length)
1029 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[0] & mask, ps);
1030 mask <<= 1;
1031 st.wDay++;
1034 /* draw next month */
1035 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1036 st.wDay = 1;
1037 MONTHCAL_GetNextMonth(&st);
1038 MONTHCAL_GetMaxDate(infoPtr, &st_max);
1039 mask = 1;
1041 while(st.wDay <= st_max.wDay)
1043 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[2] & mask, ps);
1044 mask <<= 1;
1045 st.wDay++;
1049 /* paint a calendar area */
1050 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
1052 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
1053 INT i, j, length;
1054 RECT r, fill_bk_rect;
1055 SYSTEMTIME st;
1056 WCHAR buf[80];
1057 HPEN old_pen;
1058 int mask;
1060 /* fill whole days area - from week days area to today note rectangle */
1061 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
1062 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
1063 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
1065 FillRect(hdc, &fill_bk_rect, infoPtr->brushes[BrushMonth]);
1067 /* draw line under day abbreviations */
1068 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1069 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
1070 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
1071 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
1072 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
1073 SelectObject(hdc, old_pen);
1075 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
1076 infoPtr->calendars[calIdx].weeknums.right;
1078 /* draw day abbreviations */
1079 SelectObject(hdc, infoPtr->hFont);
1080 SetBkColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
1081 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1082 /* rectangle to draw a single day abbreviation within */
1083 r = infoPtr->calendars[calIdx].wdays;
1084 r.right = r.left + infoPtr->width_increment;
1086 i = infoPtr->firstDay;
1087 for(j = 0; j < 7; j++) {
1088 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
1089 DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1090 OffsetRect(&r, infoPtr->width_increment, 0);
1093 /* draw current month */
1094 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1095 st = *date;
1096 st.wDay = 1;
1097 mask = 1;
1098 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1099 while(st.wDay <= length)
1101 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[1] & mask, ps);
1102 mask <<= 1;
1103 st.wDay++;
1107 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1109 COLORREF old_text_clr, old_bk_clr;
1110 HFONT old_font;
1111 INT i;
1113 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1114 old_bk_clr = GetBkColor(hdc);
1115 old_font = GetCurrentObject(hdc, OBJ_FONT);
1117 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1119 RECT *title = &infoPtr->calendars[i].title;
1120 RECT r;
1122 /* draw title, redraw all its elements */
1123 if (IntersectRect(&r, &(ps->rcPaint), title))
1124 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1126 /* draw calendar area */
1127 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1128 if (IntersectRect(&r, &(ps->rcPaint), &r))
1129 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1131 /* week numbers */
1132 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1135 /* partially visible months */
1136 MONTHCAL_PaintLeadTrailMonths(infoPtr, hdc, ps);
1138 /* focus and today rectangle */
1139 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1141 /* today at the bottom left */
1142 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1144 /* navigation buttons */
1145 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_BACKWARD);
1146 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_FORWARD);
1148 /* restore context */
1149 SetBkColor(hdc, old_bk_clr);
1150 SelectObject(hdc, old_font);
1151 SetTextColor(hdc, old_text_clr);
1154 static LRESULT
1155 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, RECT *rect)
1157 TRACE("rect %p\n", rect);
1159 if(!rect) return FALSE;
1161 *rect = infoPtr->calendars[0].title;
1162 rect->bottom = infoPtr->calendars[0].days.bottom + infoPtr->todayrect.bottom -
1163 infoPtr->todayrect.top;
1165 AdjustWindowRect(rect, infoPtr->dwStyle, FALSE);
1167 /* minimal rectangle is zero based */
1168 OffsetRect(rect, -rect->left, -rect->top);
1170 TRACE("%s\n", wine_dbgstr_rect(rect));
1172 return TRUE;
1175 static COLORREF
1176 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, UINT index)
1178 TRACE("%p, %d\n", infoPtr, index);
1180 if (index > MCSC_TRAILINGTEXT) return -1;
1181 return infoPtr->colors[index];
1184 static LRESULT
1185 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, UINT index, COLORREF color)
1187 enum CachedBrush type;
1188 COLORREF prev;
1190 TRACE("%p, %d: color %08x\n", infoPtr, index, color);
1192 if (index > MCSC_TRAILINGTEXT) return -1;
1194 prev = infoPtr->colors[index];
1195 infoPtr->colors[index] = color;
1197 /* update cached brush */
1198 switch (index)
1200 case MCSC_BACKGROUND:
1201 type = BrushBackground;
1202 break;
1203 case MCSC_TITLEBK:
1204 type = BrushTitle;
1205 break;
1206 case MCSC_MONTHBK:
1207 type = BrushMonth;
1208 break;
1209 default:
1210 type = BrushLast;
1213 if (type != BrushLast)
1215 DeleteObject(infoPtr->brushes[type]);
1216 infoPtr->brushes[type] = CreateSolidBrush(color);
1219 /* update cached pen */
1220 if (index == MCSC_TEXT)
1222 DeleteObject(infoPtr->pens[PenText]);
1223 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[index]);
1226 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND ? TRUE : FALSE);
1227 return prev;
1230 static LRESULT
1231 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1233 TRACE("\n");
1235 if(infoPtr->delta)
1236 return infoPtr->delta;
1237 else
1238 return infoPtr->visible;
1242 static LRESULT
1243 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1245 INT prev = infoPtr->delta;
1247 TRACE("delta %d\n", delta);
1249 infoPtr->delta = delta;
1250 return prev;
1254 static inline LRESULT
1255 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1257 int day;
1259 /* convert from SYSTEMTIME to locale format */
1260 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1262 return MAKELONG(day, infoPtr->firstDaySet);
1266 /* Sets the first day of the week that will appear in the control
1269 * PARAMETERS:
1270 * [I] infoPtr : valid pointer to control data
1271 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1274 * RETURN VALUE:
1275 * Low word contains previous first day,
1276 * high word indicates was first day forced with this message before or is
1277 * locale defined (TRUE - was forced, FALSE - wasn't).
1279 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1280 * FIXME: we need more error checking here
1282 static LRESULT
1283 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1285 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1286 int new_day;
1288 TRACE("%d\n", day);
1290 if(day == -1)
1292 WCHAR buf[80];
1294 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1295 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1297 new_day = atoiW(buf);
1299 infoPtr->firstDaySet = FALSE;
1301 else if(day >= 7)
1303 new_day = 6; /* max first day allowed */
1304 infoPtr->firstDaySet = TRUE;
1306 else
1308 /* Native behaviour for that case is broken: invalid date number >31
1309 got displayed at (0,0) position, current month starts always from
1310 (1,0) position. Should be implemented here as well only if there's
1311 nothing else to do. */
1312 if (day < -1)
1313 FIXME("No bug compatibility for day=%d\n", day);
1315 new_day = day;
1316 infoPtr->firstDaySet = TRUE;
1319 /* convert from locale to SYSTEMTIME format */
1320 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1322 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1324 return prev;
1328 static LRESULT
1329 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
1331 TRACE("flag=%d, st=%p\n", flag, st);
1333 if(st)
1335 switch (flag) {
1336 case GMR_VISIBLE:
1338 st[0] = infoPtr->calendars[0].month;
1339 st[1] = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1341 if (st[0].wMonth == min_allowed_date.wMonth &&
1342 st[0].wYear == min_allowed_date.wYear)
1344 st[0].wDay = min_allowed_date.wDay;
1346 else
1347 st[0].wDay = 1;
1348 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
1350 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
1351 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
1353 return MONTHCAL_GetCalCount(infoPtr);
1355 case GMR_DAYSTATE:
1357 MONTHCAL_GetMinDate(infoPtr, &st[0]);
1358 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
1359 break;
1361 default:
1362 WARN("Unknown flag value, got %d\n", flag);
1366 return infoPtr->monthRange;
1370 static LRESULT
1371 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1373 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1377 static LRESULT
1378 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1380 FILETIME ft_min, ft_max;
1382 TRACE("%x %p\n", limits, range);
1384 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1385 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1386 return FALSE;
1388 if (limits & GDTR_MIN)
1390 if (!MONTHCAL_ValidateTime(&range[0]))
1391 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1393 infoPtr->minDate = range[0];
1394 infoPtr->rangeValid |= GDTR_MIN;
1396 if (limits & GDTR_MAX)
1398 if (!MONTHCAL_ValidateTime(&range[1]))
1399 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1401 infoPtr->maxDate = range[1];
1402 infoPtr->rangeValid |= GDTR_MAX;
1405 /* Only one limit set - we are done */
1406 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1407 return TRUE;
1409 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1410 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1412 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1414 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1416 /* Native swaps limits only when both limits are being set. */
1417 SYSTEMTIME st_tmp = infoPtr->minDate;
1418 infoPtr->minDate = infoPtr->maxDate;
1419 infoPtr->maxDate = st_tmp;
1421 else
1423 /* reset the other limit */
1424 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1425 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1426 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1430 return TRUE;
1434 static LRESULT
1435 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1437 TRACE("%p\n", range);
1439 if(!range) return FALSE;
1441 range[1] = infoPtr->maxDate;
1442 range[0] = infoPtr->minDate;
1444 return infoPtr->rangeValid;
1448 static LRESULT
1449 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1451 TRACE("%p %d %p\n", infoPtr, months, states);
1452 if(months != infoPtr->monthRange) return 0;
1454 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1456 return 1;
1459 static LRESULT
1460 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1462 TRACE("%p\n", curSel);
1463 if(!curSel) return FALSE;
1464 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1466 *curSel = infoPtr->minSel;
1467 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1468 return TRUE;
1471 static LRESULT
1472 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1474 SYSTEMTIME prev = infoPtr->minSel;
1475 INT diff;
1476 WORD day;
1478 TRACE("%p\n", curSel);
1479 if(!curSel) return FALSE;
1480 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1482 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1483 /* exit earlier if selection equals current */
1484 if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1486 if(!MONTHCAL_IsDateInValidRange(infoPtr, curSel, FALSE)) return FALSE;
1488 /* scroll calendars only if we have to */
1489 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, curSel);
1490 if (diff <= 0)
1492 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, curSel);
1493 if (diff > 0) diff = 0;
1496 if (diff != 0)
1498 INT i;
1500 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1501 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1504 infoPtr->minSel = *curSel;
1505 infoPtr->maxSel = *curSel;
1507 /* if selection is still in current month, reduce rectangle */
1508 day = prev.wDay;
1509 prev.wDay = curSel->wDay;
1510 if (MONTHCAL_IsDateEqual(&prev, curSel))
1512 RECT r_prev, r_new;
1514 prev.wDay = day;
1515 MONTHCAL_GetDayRect(infoPtr, &prev, &r_prev, -1);
1516 MONTHCAL_GetDayRect(infoPtr, curSel, &r_new, -1);
1518 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1519 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1521 else
1522 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1524 return TRUE;
1528 static LRESULT
1529 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1531 return infoPtr->maxSelCount;
1535 static LRESULT
1536 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1538 TRACE("%d\n", max);
1540 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1541 if(max <= 0) return FALSE;
1543 infoPtr->maxSelCount = max;
1545 return TRUE;
1549 static LRESULT
1550 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1552 TRACE("%p\n", range);
1554 if(!range) return FALSE;
1556 if(infoPtr->dwStyle & MCS_MULTISELECT)
1558 range[1] = infoPtr->maxSel;
1559 range[0] = infoPtr->minSel;
1560 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1561 return TRUE;
1564 return FALSE;
1568 static LRESULT
1569 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1571 SYSTEMTIME old_range[2];
1572 INT diff;
1574 TRACE("%p\n", range);
1576 if(!range || !(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1578 /* adjust timestamps */
1579 if(!MONTHCAL_ValidateTime(&range[0])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1580 if(!MONTHCAL_ValidateTime(&range[1])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1582 /* maximum range exceeded */
1583 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1585 old_range[0] = infoPtr->minSel;
1586 old_range[1] = infoPtr->maxSel;
1588 /* swap if min > max */
1589 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1591 infoPtr->minSel = range[0];
1592 infoPtr->maxSel = range[1];
1594 else
1596 infoPtr->minSel = range[1];
1597 infoPtr->maxSel = range[0];
1600 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, &infoPtr->maxSel);
1601 if (diff < 0)
1603 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, &infoPtr->maxSel);
1604 if (diff > 0) diff = 0;
1607 if (diff != 0)
1609 INT i;
1611 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1612 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1615 /* update day of week */
1616 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1617 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1619 /* redraw if bounds changed */
1620 /* FIXME: no actual need to redraw everything */
1621 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1622 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1624 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1627 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1628 return TRUE;
1632 static LRESULT
1633 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1635 TRACE("%p\n", today);
1637 if(!today) return FALSE;
1638 *today = infoPtr->todaysDate;
1639 return TRUE;
1642 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1644 * RETURN VALUE
1646 * TRUE - today date changed
1647 * FALSE - today date isn't changed
1649 static BOOL
1650 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1652 RECT new_r, old_r;
1654 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1656 MONTHCAL_GetDayRect(infoPtr, &infoPtr->todaysDate, &old_r, -1);
1657 MONTHCAL_GetDayRect(infoPtr, today, &new_r, -1);
1659 infoPtr->todaysDate = *today;
1661 /* only two days need redrawing */
1662 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1663 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1664 return TRUE;
1667 /* MCM_SETTODAT handler */
1668 static LRESULT
1669 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1671 TRACE("%p\n", today);
1673 if(!today) return FALSE;
1675 /* remember if date was set successfully */
1676 if(MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1678 return TRUE;
1681 /* returns calendar index containing specified point, or -1 if it's background */
1682 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1684 RECT r;
1685 INT i;
1687 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1689 /* whole bounding rectangle allows some optimization to compute */
1690 r.left = infoPtr->calendars[i].title.left;
1691 r.top = infoPtr->calendars[i].title.top;
1692 r.bottom = infoPtr->calendars[i].days.bottom;
1693 r.right = infoPtr->calendars[i].days.right;
1695 if (PtInRect(&r, *pt)) return i;
1698 return -1;
1701 static inline UINT fill_hittest_info(const MCHITTESTINFO *src, MCHITTESTINFO *dest)
1703 dest->uHit = src->uHit;
1704 dest->st = src->st;
1706 if (dest->cbSize == sizeof(MCHITTESTINFO))
1707 memcpy(&dest->rc, &src->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1709 return src->uHit;
1712 static LRESULT
1713 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1715 MCHITTESTINFO htinfo;
1716 SYSTEMTIME *ht_month;
1717 INT day, calIdx;
1719 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1721 htinfo.st = st_null;
1723 /* we should preserve passed fields if hit area doesn't need them */
1724 if (lpht->cbSize == sizeof(MCHITTESTINFO))
1725 memcpy(&htinfo.rc, &lpht->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1727 /* Comment in for debugging...
1728 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,
1729 infoPtr->wdays.left, infoPtr->wdays.right,
1730 infoPtr->wdays.top, infoPtr->wdays.bottom,
1731 infoPtr->days.left, infoPtr->days.right,
1732 infoPtr->days.top, infoPtr->days.bottom,
1733 infoPtr->todayrect.left, infoPtr->todayrect.right,
1734 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1735 infoPtr->weeknums.left, infoPtr->weeknums.right,
1736 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1739 /* guess in what calendar we are */
1740 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1741 if (calIdx == -1)
1743 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1745 htinfo.uHit = MCHT_TODAYLINK;
1746 htinfo.rc = infoPtr->todayrect;
1748 else
1749 /* outside of calendar area? What's left must be background :-) */
1750 htinfo.uHit = MCHT_CALENDARBK;
1752 return fill_hittest_info(&htinfo, lpht);
1755 /* are we in the header? */
1756 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1757 /* FIXME: buttons hittesting could be optimized cause maximum
1758 two calendars have buttons */
1759 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1761 htinfo.uHit = MCHT_TITLEBTNPREV;
1762 htinfo.rc = infoPtr->titlebtnprev;
1764 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1766 htinfo.uHit = MCHT_TITLEBTNNEXT;
1767 htinfo.rc = infoPtr->titlebtnnext;
1769 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1771 htinfo.uHit = MCHT_TITLEMONTH;
1772 htinfo.rc = infoPtr->calendars[calIdx].titlemonth;
1773 htinfo.iOffset = calIdx;
1775 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1777 htinfo.uHit = MCHT_TITLEYEAR;
1778 htinfo.rc = infoPtr->calendars[calIdx].titleyear;
1779 htinfo.iOffset = calIdx;
1781 else
1783 htinfo.uHit = MCHT_TITLE;
1784 htinfo.rc = infoPtr->calendars[calIdx].title;
1785 htinfo.iOffset = calIdx;
1788 return fill_hittest_info(&htinfo, lpht);
1791 ht_month = &infoPtr->calendars[calIdx].month;
1792 /* days area (including week days and week numbers) */
1793 day = MONTHCAL_GetDayFromPos(infoPtr, lpht->pt, calIdx);
1794 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1796 htinfo.uHit = MCHT_CALENDARDAY;
1797 htinfo.iOffset = calIdx;
1798 htinfo.st.wYear = ht_month->wYear;
1799 htinfo.st.wMonth = (day < 1) ? ht_month->wMonth -1 : ht_month->wMonth;
1800 htinfo.st.wDay = (day < 1) ?
1801 MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day : day;
1803 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1805 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1807 htinfo.uHit = MCHT_CALENDARWEEKNUM;
1808 htinfo.st.wYear = ht_month->wYear;
1809 htinfo.iOffset = calIdx;
1811 if (day < 1)
1813 htinfo.st.wMonth = ht_month->wMonth - 1;
1814 htinfo.st.wDay = MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day;
1816 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1818 htinfo.st.wMonth = ht_month->wMonth + 1;
1819 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1821 else
1823 htinfo.st.wMonth = ht_month->wMonth;
1824 htinfo.st.wDay = day;
1827 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1829 htinfo.iOffset = calIdx;
1830 htinfo.st.wYear = ht_month->wYear;
1831 htinfo.st.wMonth = ht_month->wMonth;
1832 /* previous month only valid for first calendar */
1833 if (day < 1 && calIdx == 0)
1835 htinfo.uHit = MCHT_CALENDARDATEPREV;
1836 MONTHCAL_GetPrevMonth(&htinfo.st);
1837 htinfo.st.wDay = MONTHCAL_MonthLength(htinfo.st.wMonth, htinfo.st.wYear) + day;
1839 /* next month only valid for last calendar */
1840 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear) &&
1841 calIdx == MONTHCAL_GetCalCount(infoPtr)-1)
1843 htinfo.uHit = MCHT_CALENDARDATENEXT;
1844 MONTHCAL_GetNextMonth(&htinfo.st);
1845 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1847 /* multiple calendars case - blank areas for previous/next month */
1848 else if (day < 1 || day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1850 htinfo.uHit = MCHT_CALENDARBK;
1852 else
1854 htinfo.uHit = MCHT_CALENDARDATE;
1855 htinfo.st.wDay = day;
1858 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1859 MONTHCAL_GetDayRectI(infoPtr, &htinfo.rc, htinfo.iCol, htinfo.iRow, calIdx);
1860 /* always update day of week */
1861 MONTHCAL_CalculateDayOfWeek(&htinfo.st, TRUE);
1864 return fill_hittest_info(&htinfo, lpht);
1867 /* MCN_GETDAYSTATE notification helper */
1868 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1870 if(infoPtr->dwStyle & MCS_DAYSTATE) {
1871 NMDAYSTATE nmds;
1873 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1874 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1875 nmds.nmhdr.code = MCN_GETDAYSTATE;
1876 nmds.cDayState = infoPtr->monthRange;
1877 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1879 nmds.stStart = infoPtr->todaysDate;
1880 nmds.stStart.wYear = infoPtr->minSel.wYear;
1881 nmds.stStart.wMonth = infoPtr->minSel.wMonth;
1882 nmds.stStart.wDay = 1;
1884 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1885 memcpy(infoPtr->monthdayState, nmds.prgDayState, infoPtr->monthRange*sizeof(MONTHDAYSTATE));
1887 Free(nmds.prgDayState);
1891 /* no valid range check performed */
1892 static void MONTHCAL_Scroll(MONTHCAL_INFO *infoPtr, INT delta)
1894 INT i, selIdx = -1;
1896 for(i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1898 /* save selection position to shift it later */
1899 if (selIdx == -1 && MONTHCAL_CompareMonths(&infoPtr->minSel, &infoPtr->calendars[i].month) == 0)
1900 selIdx = i;
1902 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, delta);
1905 /* selection is always shifted to first calendar */
1906 if(infoPtr->dwStyle & MCS_MULTISELECT)
1908 SYSTEMTIME range[2];
1910 MONTHCAL_GetSelRange(infoPtr, range);
1911 MONTHCAL_GetMonth(&range[0], delta - selIdx);
1912 MONTHCAL_GetMonth(&range[1], delta - selIdx);
1913 MONTHCAL_SetSelRange(infoPtr, range);
1915 else
1917 SYSTEMTIME st = infoPtr->minSel;
1919 MONTHCAL_GetMonth(&st, delta - selIdx);
1920 MONTHCAL_SetCurSel(infoPtr, &st);
1924 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
1926 INT delta = infoPtr->delta ? infoPtr->delta : MONTHCAL_GetCalCount(infoPtr);
1927 SYSTEMTIME st;
1929 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
1931 /* check if change allowed by range set */
1932 if(direction == DIRECTION_BACKWARD)
1934 st = infoPtr->calendars[0].month;
1935 MONTHCAL_GetMonth(&st, -delta);
1937 else
1939 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1940 MONTHCAL_GetMonth(&st, delta);
1943 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
1945 MONTHCAL_Scroll(infoPtr, direction == DIRECTION_BACKWARD ? -delta : delta);
1946 MONTHCAL_NotifyDayState(infoPtr);
1947 MONTHCAL_NotifySelectionChange(infoPtr);
1950 static LRESULT
1951 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1953 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1954 HMENU hMenu;
1955 POINT menupoint;
1956 WCHAR buf[32];
1958 hMenu = CreatePopupMenu();
1959 if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1961 WARN("Can't load resource\n");
1962 strcpyW(buf, todayW);
1964 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1965 menupoint.x = (short)LOWORD(lParam);
1966 menupoint.y = (short)HIWORD(lParam);
1967 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1968 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
1969 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1971 infoPtr->calendars[0].month = infoPtr->todaysDate;
1972 infoPtr->minSel = infoPtr->todaysDate;
1973 infoPtr->maxSel = infoPtr->todaysDate;
1974 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1977 return 0;
1980 /***
1981 * DESCRIPTION:
1982 * Subclassed edit control windproc function
1984 * PARAMETER(S):
1985 * [I] hwnd : the edit window handle
1986 * [I] uMsg : the message that is to be processed
1987 * [I] wParam : first message parameter
1988 * [I] lParam : second message parameter
1991 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1993 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
1995 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
1996 hwnd, uMsg, wParam, lParam);
1998 switch (uMsg)
2000 case WM_GETDLGCODE:
2001 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
2003 case WM_DESTROY:
2005 WNDPROC editProc = infoPtr->EditWndProc;
2006 infoPtr->EditWndProc = NULL;
2007 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
2008 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
2011 case WM_KILLFOCUS:
2012 break;
2014 case WM_KEYDOWN:
2015 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
2016 break;
2018 default:
2019 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
2022 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
2023 SendMessageW(hwnd, WM_CLOSE, 0, 0);
2024 return 0;
2027 /* creates updown control and edit box */
2028 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr, INT calIdx)
2030 RECT *rc = &infoPtr->calendars[calIdx].titleyear;
2031 RECT *title = &infoPtr->calendars[calIdx].title;
2033 infoPtr->hWndYearEdit =
2034 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
2035 rc->left + 3, (title->bottom + title->top - infoPtr->textHeight) / 2,
2036 rc->right - rc->left + 4,
2037 infoPtr->textHeight, infoPtr->hwndSelf,
2038 NULL, NULL, NULL);
2040 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
2042 infoPtr->hWndYearUpDown =
2043 CreateWindowExW(0, UPDOWN_CLASSW, 0,
2044 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
2045 rc->right + 7, (title->bottom + title->top - infoPtr->textHeight) / 2,
2046 18, infoPtr->textHeight, infoPtr->hwndSelf,
2047 NULL, NULL, NULL);
2049 /* attach edit box */
2050 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
2051 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
2052 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
2053 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->calendars[calIdx].month.wYear);
2055 /* subclass edit box */
2056 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
2057 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
2059 SetFocus(infoPtr->hWndYearEdit);
2062 static LRESULT
2063 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2065 MCHITTESTINFO ht;
2066 DWORD hit;
2068 /* Actually we don't need input focus for calendar, this is used to kill
2069 year updown and its buddy edit box */
2070 if (IsWindow(infoPtr->hWndYearUpDown))
2072 SetFocus(infoPtr->hwndSelf);
2073 return 0;
2076 SetCapture(infoPtr->hwndSelf);
2078 ht.cbSize = sizeof(MCHITTESTINFO);
2079 ht.pt.x = (short)LOWORD(lParam);
2080 ht.pt.y = (short)HIWORD(lParam);
2082 hit = MONTHCAL_HitTest(infoPtr, &ht);
2084 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
2086 switch(hit)
2088 case MCHT_TITLEBTNNEXT:
2089 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2090 infoPtr->status = MC_NEXTPRESSED;
2091 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2092 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2093 return 0;
2095 case MCHT_TITLEBTNPREV:
2096 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2097 infoPtr->status = MC_PREVPRESSED;
2098 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2099 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2100 return 0;
2102 case MCHT_TITLEMONTH:
2104 HMENU hMenu = CreatePopupMenu();
2105 WCHAR buf[32];
2106 POINT menupoint;
2107 INT i;
2109 for (i = 0; i < 12; i++)
2111 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
2112 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
2114 menupoint.x = ht.pt.x;
2115 menupoint.y = ht.pt.y;
2116 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2117 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
2118 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
2120 if ((i > 0) && (i < 13) && infoPtr->calendars[ht.iOffset].month.wMonth != i)
2122 INT delta = i - infoPtr->calendars[ht.iOffset].month.wMonth;
2123 SYSTEMTIME st;
2125 /* check if change allowed by range set */
2126 st = delta < 0 ? infoPtr->calendars[0].month :
2127 infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2128 MONTHCAL_GetMonth(&st, delta);
2130 if (MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE))
2132 MONTHCAL_Scroll(infoPtr, delta);
2133 MONTHCAL_NotifyDayState(infoPtr);
2134 MONTHCAL_NotifySelectionChange(infoPtr);
2135 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2138 return 0;
2140 case MCHT_TITLEYEAR:
2142 MONTHCAL_EditYear(infoPtr, ht.iOffset);
2143 return 0;
2145 case MCHT_TODAYLINK:
2147 infoPtr->calendars[0].month = infoPtr->todaysDate;
2148 infoPtr->minSel = infoPtr->todaysDate;
2149 infoPtr->maxSel = infoPtr->todaysDate;
2150 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2152 MONTHCAL_NotifySelectionChange(infoPtr);
2153 MONTHCAL_NotifySelect(infoPtr);
2154 return 0;
2156 case MCHT_CALENDARDATENEXT:
2157 case MCHT_CALENDARDATEPREV:
2158 case MCHT_CALENDARDATE:
2160 SYSTEMTIME st[2];
2162 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2164 st[0] = st[1] = ht.st;
2165 /* clear selection range */
2166 MONTHCAL_SetSelRange(infoPtr, st);
2168 infoPtr->status = MC_SEL_LBUTDOWN;
2169 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
2170 return 0;
2174 return 1;
2178 static LRESULT
2179 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2181 NMHDR nmhdr;
2182 MCHITTESTINFO ht;
2183 DWORD hit;
2185 TRACE("\n");
2187 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2188 RECT *r;
2190 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2191 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2192 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2194 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2197 ReleaseCapture();
2199 /* always send NM_RELEASEDCAPTURE notification */
2200 nmhdr.hwndFrom = infoPtr->hwndSelf;
2201 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2202 nmhdr.code = NM_RELEASEDCAPTURE;
2203 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2205 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2207 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2209 ht.cbSize = sizeof(MCHITTESTINFO);
2210 ht.pt.x = (short)LOWORD(lParam);
2211 ht.pt.y = (short)HIWORD(lParam);
2212 hit = MONTHCAL_HitTest(infoPtr, &ht);
2214 infoPtr->status = MC_SEL_LBUTUP;
2215 MONTHCAL_SetDayFocus(infoPtr, NULL);
2217 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2219 SYSTEMTIME sel = infoPtr->minSel;
2221 /* will be invalidated here */
2222 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2224 /* send MCN_SELCHANGE only if new date selected */
2225 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2226 MONTHCAL_NotifySelectionChange(infoPtr);
2228 MONTHCAL_NotifySelect(infoPtr);
2231 return 0;
2235 static LRESULT
2236 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2238 TRACE("%ld\n", id);
2240 switch(id) {
2241 case MC_PREVNEXTMONTHTIMER:
2242 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2243 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2244 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2245 break;
2246 case MC_TODAYUPDATETIMER:
2248 SYSTEMTIME st;
2250 if(infoPtr->todaySet) return 0;
2252 GetLocalTime(&st);
2253 MONTHCAL_UpdateToday(infoPtr, &st);
2255 /* notification sent anyway */
2256 MONTHCAL_NotifySelectionChange(infoPtr);
2258 return 0;
2260 default:
2261 ERR("got unknown timer %ld\n", id);
2262 break;
2265 return 0;
2269 static LRESULT
2270 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2272 MCHITTESTINFO ht;
2273 SYSTEMTIME st_ht;
2274 INT hit;
2275 RECT r;
2277 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2279 ht.cbSize = sizeof(MCHITTESTINFO);
2280 ht.pt.x = (short)LOWORD(lParam);
2281 ht.pt.y = (short)HIWORD(lParam);
2282 ht.iOffset = -1;
2284 hit = MONTHCAL_HitTest(infoPtr, &ht);
2286 /* not on the calendar date numbers? bail out */
2287 TRACE("hit:%x\n",hit);
2288 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2290 MONTHCAL_SetDayFocus(infoPtr, NULL);
2291 return 0;
2294 st_ht = ht.st;
2296 /* if pointer is over focused day still there's nothing to do */
2297 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2299 MONTHCAL_GetDayRect(infoPtr, &ht.st, &r, ht.iOffset);
2301 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2302 SYSTEMTIME st[2];
2304 MONTHCAL_GetSelRange(infoPtr, st);
2306 /* If we're still at the first selected date and range is empty, return.
2307 If range isn't empty we should change range to a single firstSel */
2308 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2309 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2311 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2313 st[0] = infoPtr->firstSel;
2314 /* we should overwrite timestamp here */
2315 MONTHCAL_CopyDate(&st_ht, &st[1]);
2317 /* bounds will be swapped here if needed */
2318 MONTHCAL_SetSelRange(infoPtr, st);
2320 return 0;
2323 done:
2325 /* FIXME: this should specify a rectangle containing only the days that changed
2326 using InvalidateRect */
2327 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2329 return 0;
2333 static LRESULT
2334 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2336 HDC hdc;
2337 PAINTSTRUCT ps;
2339 if (hdc_paint)
2341 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2342 hdc = hdc_paint;
2344 else
2345 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2347 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2348 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2349 return 0;
2352 static LRESULT
2353 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2355 RECT rc;
2357 if (!GetClipBox(hdc, &rc)) return FALSE;
2359 FillRect(hdc, &rc, infoPtr->brushes[BrushBackground]);
2361 return TRUE;
2364 static LRESULT
2365 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2367 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2369 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2370 return 0;
2372 if (options & PRF_ERASEBKGND)
2373 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2375 if (options & PRF_CLIENT)
2376 MONTHCAL_Paint(infoPtr, hdc);
2378 return 0;
2381 static LRESULT
2382 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2384 TRACE("\n");
2386 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2388 return 0;
2391 /* sets the size information */
2392 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2394 static const WCHAR O0W[] = { '0','0',0 };
2395 HDC hdc = GetDC(infoPtr->hwndSelf);
2396 RECT *title=&infoPtr->calendars[0].title;
2397 RECT *prev=&infoPtr->titlebtnprev;
2398 RECT *next=&infoPtr->titlebtnnext;
2399 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2400 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2401 RECT *wdays=&infoPtr->calendars[0].wdays;
2402 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2403 RECT *days=&infoPtr->calendars[0].days;
2404 RECT *todayrect=&infoPtr->todayrect;
2405 SIZE size, sz;
2406 TEXTMETRICW tm;
2407 HFONT currentFont;
2408 INT xdiv, dx, dy, i;
2409 RECT client;
2410 WCHAR buff[80];
2412 GetClientRect(infoPtr->hwndSelf, &client);
2414 currentFont = SelectObject(hdc, infoPtr->hFont);
2416 /* get the height and width of each day's text */
2417 GetTextMetricsW(hdc, &tm);
2418 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2420 /* find largest abbreviated day name for current locale */
2421 size.cx = sz.cx = 0;
2422 for (i = 0; i < 7; i++)
2424 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + i,
2425 buff, countof(buff)))
2427 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2428 if (sz.cx > size.cx) size.cx = sz.cx;
2430 else /* locale independent fallback on failure */
2432 static const WCHAR SunW[] = { 'S','u','n',0 };
2434 GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2435 break;
2439 infoPtr->textWidth = size.cx + 2;
2441 /* recalculate the height and width increments and offsets */
2442 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2444 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2446 infoPtr->width_increment = size.cx * 2 + 4;
2447 infoPtr->height_increment = infoPtr->textHeight;
2449 /* calculate title area */
2450 title->top = 0;
2451 title->bottom = 3 * infoPtr->height_increment / 2;
2452 title->left = 0;
2453 title->right = infoPtr->width_increment * xdiv;
2455 /* set the dimensions of the next and previous buttons and center */
2456 /* the month text vertically */
2457 prev->top = next->top = title->top + 4;
2458 prev->bottom = next->bottom = title->bottom - 4;
2459 prev->left = title->left + 4;
2460 prev->right = prev->left + (title->bottom - title->top);
2461 next->right = title->right - 4;
2462 next->left = next->right - (title->bottom - title->top);
2464 /* titlemonth->left and right change based upon the current month */
2465 /* and are recalculated in refresh as the current month may change */
2466 /* without the control being resized */
2467 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2468 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2470 /* setup the dimensions of the rectangle we draw the names of the */
2471 /* days of the week in */
2472 weeknumrect->left = 0;
2474 if(infoPtr->dwStyle & MCS_WEEKNUMBERS)
2475 weeknumrect->right = prev->right;
2476 else
2477 weeknumrect->right = weeknumrect->left;
2479 wdays->left = days->left = weeknumrect->right;
2480 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2481 wdays->top = title->bottom;
2482 wdays->bottom = wdays->top + infoPtr->height_increment;
2484 days->top = weeknumrect->top = wdays->bottom;
2485 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2487 todayrect->left = 0;
2488 todayrect->right = title->right;
2489 todayrect->top = days->bottom;
2490 todayrect->bottom = days->bottom + infoPtr->height_increment;
2492 /* offset all rectangles to center in client area */
2493 dx = (client.right - title->right) / 2;
2494 dy = (client.bottom - todayrect->bottom) / 2;
2496 /* if calendar doesn't fit client area show it at left/top bounds */
2497 if (title->left + dx < 0) dx = 0;
2498 if (title->top + dy < 0) dy = 0;
2500 if (dx != 0 || dy != 0)
2502 OffsetRect(title, dx, dy);
2503 OffsetRect(prev, dx, dy);
2504 OffsetRect(next, dx, dy);
2505 OffsetRect(titlemonth, dx, dy);
2506 OffsetRect(titleyear, dx, dy);
2507 OffsetRect(wdays, dx, dy);
2508 OffsetRect(weeknumrect, dx, dy);
2509 OffsetRect(days, dx, dy);
2510 OffsetRect(todayrect, dx, dy);
2513 /* TODO: update calendars count */
2514 infoPtr->dim.cx = infoPtr->dim.cy = 1;
2516 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2517 infoPtr->width_increment,infoPtr->height_increment,
2518 wine_dbgstr_rect(&client),
2519 wine_dbgstr_rect(title),
2520 wine_dbgstr_rect(wdays),
2521 wine_dbgstr_rect(days),
2522 wine_dbgstr_rect(todayrect));
2524 /* restore the originally selected font */
2525 SelectObject(hdc, currentFont);
2527 ReleaseDC(infoPtr->hwndSelf, hdc);
2530 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2532 TRACE("(width=%d, height=%d)\n", Width, Height);
2534 MONTHCAL_UpdateSize(infoPtr);
2535 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2537 return 0;
2540 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2542 return (LRESULT)infoPtr->hFont;
2545 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2547 HFONT hOldFont;
2548 LOGFONTW lf;
2550 if (!hFont) return 0;
2552 hOldFont = infoPtr->hFont;
2553 infoPtr->hFont = hFont;
2555 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2556 lf.lfWeight = FW_BOLD;
2557 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2559 MONTHCAL_UpdateSize(infoPtr);
2561 if (redraw)
2562 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2564 return (LRESULT)hOldFont;
2567 /* update theme after a WM_THEMECHANGED message */
2568 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2570 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2571 CloseThemeData (theme);
2572 OpenThemeData (infoPtr->hwndSelf, themeClass);
2573 return 0;
2576 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2577 const STYLESTRUCT *lpss)
2579 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2580 wStyleType, lpss->styleOld, lpss->styleNew);
2582 if (wStyleType != GWL_STYLE) return 0;
2584 infoPtr->dwStyle = lpss->styleNew;
2586 /* make room for week numbers */
2587 if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2588 MONTHCAL_UpdateSize(infoPtr);
2590 return 0;
2593 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2594 STYLESTRUCT *lpss)
2596 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2597 wStyleType, lpss->styleOld, lpss->styleNew);
2599 /* block MCS_MULTISELECT change */
2600 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2602 if (lpss->styleOld & MCS_MULTISELECT)
2603 lpss->styleNew |= MCS_MULTISELECT;
2604 else
2605 lpss->styleNew &= ~MCS_MULTISELECT;
2608 return 0;
2611 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2612 static LRESULT
2613 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2615 MONTHCAL_INFO *infoPtr;
2617 /* allocate memory for info structure */
2618 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2619 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2621 if (infoPtr == NULL) {
2622 ERR("could not allocate info memory!\n");
2623 return 0;
2626 infoPtr->hwndSelf = hwnd;
2627 infoPtr->hwndNotify = lpcs->hwndParent;
2628 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2629 infoPtr->calendars = Alloc(sizeof(CALENDAR_INFO));
2630 if (!infoPtr->calendars) goto fail;
2632 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
2634 /* initialize info structure */
2635 /* FIXME: calculate systemtime ->> localtime(subtract timezoneinfo) */
2637 GetLocalTime(&infoPtr->todaysDate);
2638 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2640 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2641 infoPtr->monthRange = 3;
2643 infoPtr->monthdayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
2644 if (!infoPtr->monthdayState) goto fail;
2646 infoPtr->colors[MCSC_BACKGROUND] = comctl32_color.clrWindow;
2647 infoPtr->colors[MCSC_TEXT] = comctl32_color.clrWindowText;
2648 infoPtr->colors[MCSC_TITLEBK] = comctl32_color.clrActiveCaption;
2649 infoPtr->colors[MCSC_TITLETEXT] = comctl32_color.clrWindow;
2650 infoPtr->colors[MCSC_MONTHBK] = comctl32_color.clrWindow;
2651 infoPtr->colors[MCSC_TRAILINGTEXT] = comctl32_color.clrGrayText;
2653 infoPtr->brushes[BrushBackground] = CreateSolidBrush(infoPtr->colors[MCSC_BACKGROUND]);
2654 infoPtr->brushes[BrushTitle] = CreateSolidBrush(infoPtr->colors[MCSC_TITLEBK]);
2655 infoPtr->brushes[BrushMonth] = CreateSolidBrush(infoPtr->colors[MCSC_MONTHBK]);
2657 infoPtr->pens[PenRed] = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
2658 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[MCSC_TEXT]);
2660 infoPtr->minSel = infoPtr->todaysDate;
2661 infoPtr->maxSel = infoPtr->todaysDate;
2662 infoPtr->calendars[0].month = infoPtr->todaysDate;
2663 infoPtr->isUnicode = TRUE;
2665 /* set all control dimensions */
2666 MONTHCAL_UpdateSize(infoPtr);
2668 /* today auto update timer, to be freed only on control destruction */
2669 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2671 OpenThemeData (infoPtr->hwndSelf, themeClass);
2673 return 0;
2675 fail:
2676 Free(infoPtr->monthdayState);
2677 Free(infoPtr->calendars);
2678 Free(infoPtr);
2679 return 0;
2682 static LRESULT
2683 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2685 INT i;
2687 /* free month calendar info data */
2688 Free(infoPtr->monthdayState);
2689 Free(infoPtr->calendars);
2690 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2692 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2694 for (i = 0; i < BrushLast; i++) DeleteObject(infoPtr->brushes[i]);
2695 for (i = 0; i < PenLast; i++) DeleteObject(infoPtr->pens[i]);
2697 Free(infoPtr);
2698 return 0;
2702 * Handler for WM_NOTIFY messages
2704 static LRESULT
2705 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2707 /* notification from year edit updown */
2708 if (hdr->code == UDN_DELTAPOS)
2710 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2712 if (hdr->hwndFrom == infoPtr->hWndYearUpDown && nmud->iDelta)
2714 /* year value limits are set up explicitly after updown creation */
2715 MONTHCAL_Scroll(infoPtr, 12 * nmud->iDelta);
2716 MONTHCAL_NotifyDayState(infoPtr);
2717 MONTHCAL_NotifySelectionChange(infoPtr);
2720 return 0;
2723 static inline BOOL
2724 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2726 BOOL prev = infoPtr->isUnicode;
2727 infoPtr->isUnicode = isUnicode;
2728 return prev;
2731 static inline BOOL
2732 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2734 return infoPtr->isUnicode;
2737 static LRESULT WINAPI
2738 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2740 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0);
2742 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2744 if (!infoPtr && (uMsg != WM_CREATE))
2745 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2746 switch(uMsg)
2748 case MCM_GETCURSEL:
2749 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2751 case MCM_SETCURSEL:
2752 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2754 case MCM_GETMAXSELCOUNT:
2755 return MONTHCAL_GetMaxSelCount(infoPtr);
2757 case MCM_SETMAXSELCOUNT:
2758 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2760 case MCM_GETSELRANGE:
2761 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2763 case MCM_SETSELRANGE:
2764 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2766 case MCM_GETMONTHRANGE:
2767 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2769 case MCM_SETDAYSTATE:
2770 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2772 case MCM_GETMINREQRECT:
2773 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2775 case MCM_GETCOLOR:
2776 return MONTHCAL_GetColor(infoPtr, wParam);
2778 case MCM_SETCOLOR:
2779 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2781 case MCM_GETTODAY:
2782 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2784 case MCM_SETTODAY:
2785 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2787 case MCM_HITTEST:
2788 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2790 case MCM_GETFIRSTDAYOFWEEK:
2791 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2793 case MCM_SETFIRSTDAYOFWEEK:
2794 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2796 case MCM_GETRANGE:
2797 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2799 case MCM_SETRANGE:
2800 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2802 case MCM_GETMONTHDELTA:
2803 return MONTHCAL_GetMonthDelta(infoPtr);
2805 case MCM_SETMONTHDELTA:
2806 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2808 case MCM_GETMAXTODAYWIDTH:
2809 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2811 case MCM_SETUNICODEFORMAT:
2812 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2814 case MCM_GETUNICODEFORMAT:
2815 return MONTHCAL_GetUnicodeFormat(infoPtr);
2817 case MCM_GETCALENDARCOUNT:
2818 return MONTHCAL_GetCalCount(infoPtr);
2820 case WM_GETDLGCODE:
2821 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2823 case WM_RBUTTONUP:
2824 return MONTHCAL_RButtonUp(infoPtr, lParam);
2826 case WM_LBUTTONDOWN:
2827 return MONTHCAL_LButtonDown(infoPtr, lParam);
2829 case WM_MOUSEMOVE:
2830 return MONTHCAL_MouseMove(infoPtr, lParam);
2832 case WM_LBUTTONUP:
2833 return MONTHCAL_LButtonUp(infoPtr, lParam);
2835 case WM_PAINT:
2836 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2838 case WM_PRINTCLIENT:
2839 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2841 case WM_ERASEBKGND:
2842 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2844 case WM_SETFOCUS:
2845 return MONTHCAL_SetFocus(infoPtr);
2847 case WM_SIZE:
2848 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2850 case WM_NOTIFY:
2851 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2853 case WM_CREATE:
2854 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2856 case WM_SETFONT:
2857 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2859 case WM_GETFONT:
2860 return MONTHCAL_GetFont(infoPtr);
2862 case WM_TIMER:
2863 return MONTHCAL_Timer(infoPtr, wParam);
2865 case WM_THEMECHANGED:
2866 return theme_changed (infoPtr);
2868 case WM_DESTROY:
2869 return MONTHCAL_Destroy(infoPtr);
2871 case WM_SYSCOLORCHANGE:
2872 COMCTL32_RefreshSysColors();
2873 return 0;
2875 case WM_STYLECHANGED:
2876 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2878 case WM_STYLECHANGING:
2879 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2881 default:
2882 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2883 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2884 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2889 void
2890 MONTHCAL_Register(void)
2892 WNDCLASSW wndClass;
2894 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2895 wndClass.style = CS_GLOBALCLASS;
2896 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2897 wndClass.cbClsExtra = 0;
2898 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2899 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2900 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2901 wndClass.lpszClassName = MONTHCAL_CLASSW;
2903 RegisterClassW(&wndClass);
2907 void
2908 MONTHCAL_Unregister(void)
2910 UnregisterClassW(MONTHCAL_CLASSW, NULL);