user32: Return empty string from LoadStringW() if resource is not found.
[wine.git] / dlls / comctl32 / monthcal.c
blobac8a382310e6b44be2e23b07fc1b3ee2d3afb217
1 /*
2 * Month calendar control
4 * Copyright 1998, 1999 Eric Kohl (ekohl@abo.rhein-zeitung.de)
5 * Copyright 1999 Alex Priem (alexp@sci.kun.nl)
6 * Copyright 1999 Chris Morgan <cmorgan@wpi.edu> and
7 * James Abbatiello <abbeyj@wpi.edu>
8 * Copyright 2000 Uwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de>
9 * Copyright 2009-2011 Nikolay Sivov
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * TODO:
26 * -- MCM_[GS]ETUNICODEFORMAT
27 * -- handle resources better (doesn't work now);
28 * -- take care of internationalization.
29 * -- keyboard handling.
30 * -- search for FIXME
33 #include <math.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "winnls.h"
44 #include "commctrl.h"
45 #include "comctl32.h"
46 #include "uxtheme.h"
47 #include "vssym32.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
52 #define MC_SEL_LBUTUP 1 /* Left button released */
53 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
54 #define MC_PREVPRESSED 4 /* Prev month button pressed */
55 #define MC_NEXTPRESSED 8 /* Next month button pressed */
56 #define MC_PREVNEXTMONTHDELAY 350 /* when continuously pressing `next/prev
57 month', wait 350 ms before going
58 to the next/prev month */
59 #define MC_TODAYUPDATEDELAY 120000 /* time between today check for update (2 min) */
61 #define MC_PREVNEXTMONTHTIMER 1 /* Timer IDs */
62 #define MC_TODAYUPDATETIMER 2
64 #define MC_CALENDAR_PADDING 6
66 /* convert from days to 100 nanoseconds unit - used as FILETIME unit */
67 #define DAYSTO100NSECS(days) (((ULONGLONG)(days))*24*60*60*10000000)
69 enum CachedPen
71 PenRed = 0,
72 PenText,
73 PenLast
76 enum CachedBrush
78 BrushTitle = 0,
79 BrushMonth,
80 BrushBackground,
81 BrushLast
84 /* single calendar data */
85 typedef struct _CALENDAR_INFO
87 RECT title; /* rect for the header above the calendar */
88 RECT titlemonth; /* the 'month name' text in the header */
89 RECT titleyear; /* the 'year number' text in the header */
90 RECT wdays; /* week days at top */
91 RECT days; /* calendar area */
92 RECT weeknums; /* week numbers at left side */
94 SYSTEMTIME month;/* contains calendar main month/year */
95 } CALENDAR_INFO;
97 typedef struct
99 HWND hwndSelf;
100 DWORD dwStyle; /* cached GWL_STYLE */
102 COLORREF colors[MCSC_TRAILINGTEXT+1];
103 HBRUSH brushes[BrushLast];
104 HPEN pens[PenLast];
106 HFONT hFont;
107 HFONT hBoldFont;
108 int textHeight;
109 int height_increment;
110 int width_increment;
111 INT delta; /* scroll rate; # of months that the */
112 /* control moves when user clicks a scroll button */
113 int firstDay; /* Start month calendar with firstDay's day,
114 stored in SYSTEMTIME format */
115 BOOL firstDaySet; /* first week day differs from locale defined */
117 BOOL isUnicode; /* value set with MCM_SETUNICODE format */
119 MONTHDAYSTATE *monthdayState;
120 SYSTEMTIME todaysDate;
121 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
122 int status; /* See MC_SEL flags */
123 SYSTEMTIME firstSel; /* first selected day */
124 INT maxSelCount;
125 SYSTEMTIME minSel; /* contains single selection when used without MCS_MULTISELECT */
126 SYSTEMTIME maxSel;
127 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
128 DWORD rangeValid;
129 SYSTEMTIME minDate;
130 SYSTEMTIME maxDate;
132 RECT titlebtnnext; /* the `next month' button in the header */
133 RECT titlebtnprev; /* the `prev month' button in the header */
134 RECT todayrect; /* `today: xx/xx/xx' text rect */
135 HWND hwndNotify; /* Window to receive the notifications */
136 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
137 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
138 WNDPROC EditWndProc; /* original Edit window procedure */
140 CALENDAR_INFO *calendars;
141 SIZE dim; /* [cx,cy] - dimensions of calendars matrix, row/column count */
142 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
144 static const WCHAR themeClass[] = L"Scrollbar";
146 /* empty SYSTEMTIME const */
147 static const SYSTEMTIME st_null;
148 /* valid date limits */
149 static const SYSTEMTIME max_allowed_date = { /* wYear */ 9999, /* wMonth */ 12, /* wDayOfWeek */ 0, /* wDay */ 31 };
150 static const SYSTEMTIME min_allowed_date = { /* wYear */ 1752, /* wMonth */ 9, /* wDayOfWeek */ 0, /* wDay */ 14 };
152 /* Prev/Next buttons */
153 enum nav_direction
155 DIRECTION_BACKWARD,
156 DIRECTION_FORWARD
159 /* helper functions */
160 static inline INT MONTHCAL_GetCalCount(const MONTHCAL_INFO *infoPtr)
162 return infoPtr->dim.cx * infoPtr->dim.cy;
165 /* send a single MCN_SELCHANGE notification */
166 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
168 NMSELCHANGE nmsc;
170 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
171 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
172 nmsc.nmhdr.code = MCN_SELCHANGE;
173 nmsc.stSelStart = infoPtr->minSel;
174 nmsc.stSelStart.wDayOfWeek = 0;
175 if(infoPtr->dwStyle & MCS_MULTISELECT){
176 nmsc.stSelEnd = infoPtr->maxSel;
177 nmsc.stSelEnd.wDayOfWeek = 0;
179 else
180 nmsc.stSelEnd = st_null;
182 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
185 /* send a single MCN_SELECT notification */
186 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
188 NMSELCHANGE nmsc;
190 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
191 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
192 nmsc.nmhdr.code = MCN_SELECT;
193 nmsc.stSelStart = infoPtr->minSel;
194 nmsc.stSelStart.wDayOfWeek = 0;
195 if(infoPtr->dwStyle & MCS_MULTISELECT){
196 nmsc.stSelEnd = infoPtr->maxSel;
197 nmsc.stSelEnd.wDayOfWeek = 0;
199 else
200 nmsc.stSelEnd = st_null;
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 static 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 )
249 return FALSE;
250 if (time->wDay == 0 || 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 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 {
358 if(infoPtr->rangeValid & GDTR_MAX) {
359 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
360 fix_st = &infoPtr->maxDate;
364 if(infoPtr->rangeValid & GDTR_MIN) {
365 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
366 fix_st = &infoPtr->minDate;
371 if (fix && fix_st) {
372 date->wYear = fix_st->wYear;
373 date->wMonth = fix_st->wMonth;
376 return !fix_st;
379 /* Checks passed range width with configured maximum selection count
381 * PARAMETERS
383 * [I] infoPtr : valid pointer to control data
384 * [I] range0 : pointer to valid date data (requested bound)
385 * [I] range1 : pointer to valid date data (primary bound)
386 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
388 * Adjust value computed basing on primary bound and current maximum selection
389 * count. For simple range check (without adjusted value required) (range0, range1)
390 * relation means nothing.
392 * RETURN VALUE
394 * TRUE - range is shorter or equal to maximum
395 * FALSE - range is larger than maximum
397 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
398 const SYSTEMTIME *range0,
399 const SYSTEMTIME *range1,
400 SYSTEMTIME *adjust)
402 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
403 FILETIME ft_range0, ft_range1;
404 LONG cmp;
406 SystemTimeToFileTime(range0, &ft_range0);
407 SystemTimeToFileTime(range1, &ft_range1);
409 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
410 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
411 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
412 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
414 cmp = CompareFileTime(&ft_range0, &ft_range1);
416 if(cmp == 1)
417 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
418 else
419 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
421 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
423 if(adjust) {
424 if(cmp == 1)
425 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
426 else
427 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
429 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
430 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
431 FileTimeToSystemTime(&ft_range0, adjust);
434 return FALSE;
436 else return TRUE;
439 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
440 Milliseconds are intentionally not validated. */
441 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
443 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
444 return FALSE;
445 else
446 return TRUE;
449 /* Note:Depending on DST, this may be offset by a day.
450 Need to find out if we're on a DST place & adjust the clock accordingly.
451 Above function assumes we have a valid data.
452 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
453 0 = Sunday.
456 /* Returns the day in the week
458 * PARAMETERS
459 * [i] date : input date
460 * [I] inplace : set calculated value back to date structure
462 * RETURN VALUE
463 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
465 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
467 SYSTEMTIME st = st_null;
468 FILETIME ft;
470 MONTHCAL_CopyDate(date, &st);
472 SystemTimeToFileTime(&st, &ft);
473 FileTimeToSystemTime(&ft, &st);
475 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
477 return st.wDayOfWeek;
480 /* add/subtract 'months' from date */
481 static inline void MONTHCAL_GetMonth(SYSTEMTIME *date, INT months)
483 INT length, m = date->wMonth + months;
485 date->wYear += m > 0 ? (m - 1) / 12 : m / 12 - 1;
486 date->wMonth = m > 0 ? (m - 1) % 12 + 1 : 12 + m % 12;
487 /* fix moving from last day in a month */
488 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
489 if(date->wDay > length) date->wDay = length;
490 MONTHCAL_CalculateDayOfWeek(date, TRUE);
493 /* properly updates date to point on next month */
494 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
496 MONTHCAL_GetMonth(date, 1);
499 /* properly updates date to point on prev month */
500 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
502 MONTHCAL_GetMonth(date, -1);
505 /* Returns full date for a first currently visible day */
506 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
508 /* zero indexed calendar has the earliest date */
509 SYSTEMTIME st_first = infoPtr->calendars[0].month;
510 INT firstDay;
512 st_first.wDay = 1;
513 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
515 *date = infoPtr->calendars[0].month;
516 MONTHCAL_GetPrevMonth(date);
518 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
519 (infoPtr->firstDay - firstDay) % 7 + 1;
521 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
522 date->wDay -= 7;
524 /* fix day of week */
525 MONTHCAL_CalculateDayOfWeek(date, TRUE);
528 /* Returns full date for a last currently visible day */
529 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
531 /* the latest date is in latest calendar */
532 SYSTEMTIME st, *lt_month = &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
533 INT first_day;
535 *date = *lt_month;
536 st = *lt_month;
538 /* day of week of first day of current month */
539 st.wDay = 1;
540 first_day = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
542 MONTHCAL_GetNextMonth(date);
543 MONTHCAL_GetPrevMonth(&st);
545 /* last calendar starts with some date from previous month that not displayed */
546 st.wDay = MONTHCAL_MonthLength(st.wMonth, st.wYear) +
547 (infoPtr->firstDay - first_day) % 7 + 1;
548 if (st.wDay > MONTHCAL_MonthLength(st.wMonth, st.wYear)) st.wDay -= 7;
550 /* Use month length to get max day. 42 means max day count in calendar area */
551 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
552 MONTHCAL_MonthLength(lt_month->wMonth, lt_month->wYear);
554 /* fix day of week */
555 MONTHCAL_CalculateDayOfWeek(date, TRUE);
558 /* From a given point calculate the row, column and day in the calendar,
559 'day == 0' means the last day of the last month. */
560 static int MONTHCAL_GetDayFromPos(const MONTHCAL_INFO *infoPtr, POINT pt, INT calIdx)
562 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
563 int firstDay, col, row;
564 RECT client;
566 GetClientRect(infoPtr->hwndSelf, &client);
568 /* if the point is outside the x bounds of the window put it at the boundary */
569 if (pt.x > client.right) pt.x = client.right;
571 col = (pt.x - infoPtr->calendars[calIdx].days.left ) / infoPtr->width_increment;
572 row = (pt.y - infoPtr->calendars[calIdx].days.top ) / infoPtr->height_increment;
574 st.wDay = 1;
575 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
576 return col + 7 * row - firstDay;
579 /* Get day position for given date and calendar
581 * PARAMETERS
583 * [I] infoPtr : pointer to control data
584 * [I] date : date value
585 * [O] col : day column (zero based)
586 * [O] row : week column (zero based)
587 * [I] calIdx : calendar index
589 static void MONTHCAL_GetDayPos(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
590 INT *col, INT *row, INT calIdx)
592 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
593 INT first;
595 st.wDay = 1;
596 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
598 if (calIdx == 0 || calIdx == MONTHCAL_GetCalCount(infoPtr)-1) {
599 const SYSTEMTIME *cal = &infoPtr->calendars[calIdx].month;
600 LONG cmp = MONTHCAL_CompareMonths(date, &st);
602 /* previous month */
603 if (cmp == -1) {
604 *col = (first - MONTHCAL_MonthLength(date->wMonth, cal->wYear) + date->wDay) % 7;
605 *row = 0;
606 return;
609 /* next month calculation is same as for current, just add current month length */
610 if (cmp == 1)
611 first += MONTHCAL_MonthLength(cal->wMonth, cal->wYear);
614 *col = (date->wDay + first) % 7;
615 *row = (date->wDay + first - *col) / 7;
618 /* returns bounding box for day in given position in given calendar */
619 static inline void MONTHCAL_GetDayRectI(const MONTHCAL_INFO *infoPtr, RECT *r,
620 INT col, INT row, INT calIdx)
622 r->left = infoPtr->calendars[calIdx].days.left + col * infoPtr->width_increment;
623 r->right = r->left + infoPtr->width_increment;
624 r->top = infoPtr->calendars[calIdx].days.top + row * infoPtr->height_increment;
625 r->bottom = r->top + infoPtr->textHeight;
628 /* Returns bounding box for given date
630 * NOTE: when calendar index is unknown pass -1
632 static BOOL MONTHCAL_GetDayRect(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date, RECT *r, INT calIdx)
634 INT col, row;
636 if (!MONTHCAL_ValidateDate(date))
638 SetRectEmpty(r);
639 return FALSE;
642 if (calIdx == -1)
644 INT cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[0].month);
646 if (cmp <= 0)
647 calIdx = 0;
648 else
650 cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month);
651 if (cmp >= 0)
652 calIdx = MONTHCAL_GetCalCount(infoPtr)-1;
653 else
655 for (calIdx = 1; calIdx < MONTHCAL_GetCalCount(infoPtr)-1; calIdx++)
656 if (MONTHCAL_CompareMonths(date, &infoPtr->calendars[calIdx].month) == 0)
657 break;
662 MONTHCAL_GetDayPos(infoPtr, date, &col, &row, calIdx);
663 MONTHCAL_GetDayRectI(infoPtr, r, col, row, calIdx);
665 return TRUE;
668 static LRESULT
669 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
671 INT range;
673 TRACE("flags %#lx, st %p\n", flag, st);
675 switch (flag) {
676 case GMR_VISIBLE:
678 if (st)
680 st[0] = infoPtr->calendars[0].month;
681 st[1] = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
683 if (st[0].wMonth == min_allowed_date.wMonth &&
684 st[0].wYear == min_allowed_date.wYear)
686 st[0].wDay = min_allowed_date.wDay;
688 else
689 st[0].wDay = 1;
690 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
692 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
693 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
696 range = MONTHCAL_GetCalCount(infoPtr);
697 break;
699 case GMR_DAYSTATE:
701 if (st)
703 MONTHCAL_GetMinDate(infoPtr, &st[0]);
704 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
706 /* include two partially visible months */
707 range = MONTHCAL_GetCalCount(infoPtr) + 2;
708 break;
710 default:
711 WARN("Unknown flag value, got %ld\n", flag);
712 range = 0;
715 return range;
718 /* Focused day helper:
720 - set focused date to given value;
721 - reset to zero value if NULL passed;
722 - invalidate previous and new day rectangle only if needed.
724 Returns TRUE if focused day changed, FALSE otherwise.
726 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
728 RECT r;
730 if(st)
732 /* there's nothing to do if it's the same date,
733 mouse move within same date rectangle case */
734 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
736 /* invalidate old focused day */
737 if (MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1))
738 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
740 infoPtr->focusedSel = *st;
743 /* On set invalidates new day, on reset clears previous focused day. */
744 if (MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1))
745 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
747 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
748 infoPtr->focusedSel = st_null;
750 return TRUE;
753 /* draw today boundary box for specified rectangle */
754 static void MONTHCAL_Circle(const MONTHCAL_INFO *infoPtr, HDC hdc, const RECT *r)
756 HPEN old_pen = SelectObject(hdc, infoPtr->pens[PenRed]);
757 HBRUSH old_brush;
759 old_brush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
760 Rectangle(hdc, r->left, r->top, r->right, r->bottom);
762 SelectObject(hdc, old_brush);
763 SelectObject(hdc, old_pen);
766 /* Draw today day mark rectangle
768 * [I] hdc : context to draw in
769 * [I] date : day to mark with rectangle
772 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
773 const SYSTEMTIME *date)
775 RECT r;
777 MONTHCAL_GetDayRect(infoPtr, date, &r, -1);
778 MONTHCAL_Circle(infoPtr, hdc, &r);
781 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
782 int bold, const PAINTSTRUCT *ps)
784 WCHAR buf[10];
785 RECT r, r_temp;
786 COLORREF oldCol = 0;
787 COLORREF oldBk = 0;
788 INT old_bkmode, selection;
790 /* no need to check styles: when selection is not valid, it is set to zero.
791 1 < day < 31, so everything is OK */
792 MONTHCAL_GetDayRect(infoPtr, st, &r, -1);
793 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
795 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
796 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0))
798 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
799 TRACE("%s\n", wine_dbgstr_rect(&r));
800 oldCol = SetTextColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
801 oldBk = SetBkColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
802 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
804 selection = 1;
806 else
807 selection = 0;
809 SelectObject(hdc, bold ? infoPtr->hBoldFont : infoPtr->hFont);
811 old_bkmode = SetBkMode(hdc, TRANSPARENT);
812 wsprintfW(buf, L"%d", st->wDay);
813 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
814 SetBkMode(hdc, old_bkmode);
816 if (selection)
818 SetTextColor(hdc, oldCol);
819 SetBkColor(hdc, oldBk);
823 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, enum nav_direction button)
825 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
826 RECT *r = button == DIRECTION_FORWARD ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
827 BOOL pressed = button == DIRECTION_FORWARD ? infoPtr->status & MC_NEXTPRESSED :
828 infoPtr->status & MC_PREVPRESSED;
829 if (theme)
831 static const int states[] = {
832 /* Prev button */
833 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
834 /* Next button */
835 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
837 int stateNum = button == DIRECTION_FORWARD ? 3 : 0;
838 if (pressed)
839 stateNum += 1;
840 else
842 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
844 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
846 else
848 int style = button == DIRECTION_FORWARD ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
849 if (pressed)
850 style |= DFCS_PUSHED;
851 else
853 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
856 DrawFrameControl(hdc, r, DFC_SCROLL, style);
860 /* paint a title with buttons and month/year string */
861 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
863 RECT *title = &infoPtr->calendars[calIdx].title;
864 const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
865 WCHAR monthW[80], strW[80], fmtW[80], yearW[6] /* valid year range is 1601-30827 */;
866 int yearoffset, monthoffset, shiftX;
867 SIZE sz;
869 /* fill header box */
870 FillRect(hdc, title, infoPtr->brushes[BrushTitle]);
872 /* month/year string */
873 SetBkColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
874 SetTextColor(hdc, infoPtr->colors[MCSC_TITLETEXT]);
875 SelectObject(hdc, infoPtr->hBoldFont);
877 /* draw formatted date string */
878 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_YEARMONTH, st, NULL, strW, ARRAY_SIZE(strW));
879 DrawTextW(hdc, strW, lstrlenW(strW), title, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
881 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SYEARMONTH, fmtW, ARRAY_SIZE(fmtW));
882 wsprintfW(yearW, L"%ld", st->wYear);
884 /* month is trickier as it's possible to have different format pictures, we'll
885 test for M, MM, MMM, and MMMM */
886 if (wcsstr(fmtW, L"MMMM"))
887 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+st->wMonth-1, monthW, ARRAY_SIZE(monthW));
888 else if (wcsstr(fmtW, L"MMM"))
889 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME1+st->wMonth-1, monthW, ARRAY_SIZE(monthW));
890 else if (wcsstr(fmtW, L"MM"))
891 wsprintfW(monthW, L"%02d", st->wMonth);
892 else
893 wsprintfW(monthW, L"%d", st->wMonth);
895 /* update hit boxes */
896 yearoffset = 0;
897 while (strW[yearoffset])
899 if (!wcsncmp(&strW[yearoffset], yearW, lstrlenW(yearW)))
900 break;
901 yearoffset++;
904 monthoffset = 0;
905 while (strW[monthoffset])
907 if (!wcsncmp(&strW[monthoffset], monthW, lstrlenW(monthW)))
908 break;
909 monthoffset++;
912 /* for left limits use offsets */
913 sz.cx = 0;
914 if (yearoffset)
915 GetTextExtentPoint32W(hdc, strW, yearoffset, &sz);
916 infoPtr->calendars[calIdx].titleyear.left = sz.cx;
918 sz.cx = 0;
919 if (monthoffset)
920 GetTextExtentPoint32W(hdc, strW, monthoffset, &sz);
921 infoPtr->calendars[calIdx].titlemonth.left = sz.cx;
923 /* for right limits use actual string parts lengths */
924 GetTextExtentPoint32W(hdc, &strW[yearoffset], lstrlenW(yearW), &sz);
925 infoPtr->calendars[calIdx].titleyear.right = infoPtr->calendars[calIdx].titleyear.left + sz.cx;
927 GetTextExtentPoint32W(hdc, monthW, lstrlenW(monthW), &sz);
928 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
930 /* Finally translate rectangles to match center aligned string,
931 hit rectangles are relative to title rectangle before translation. */
932 GetTextExtentPoint32W(hdc, strW, lstrlenW(strW), &sz);
933 shiftX = (title->right - title->left - sz.cx) / 2 + title->left;
934 OffsetRect(&infoPtr->calendars[calIdx].titleyear, shiftX, 0);
935 OffsetRect(&infoPtr->calendars[calIdx].titlemonth, shiftX, 0);
938 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
940 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
941 INT mindays, weeknum, weeknum1, startofprescal;
942 INT i, prev_month;
943 SYSTEMTIME st;
944 WCHAR buf[80];
945 HPEN old_pen;
946 RECT r;
948 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
950 MONTHCAL_GetMinDate(infoPtr, &st);
951 startofprescal = st.wDay;
952 st = *date;
954 prev_month = date->wMonth - 1;
955 if(prev_month == 0) prev_month = 12;
958 Rules what week to call the first week of a new year:
959 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
960 The week containing Jan 1 is the first week of year
961 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
962 First week of year must contain 4 days of the new year
963 LOCALE_IFIRSTWEEKOFYEAR == 1 (what countries?)
964 The first week of the year must contain only days of the new year
966 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, ARRAY_SIZE(buf));
967 weeknum = wcstol(buf, NULL, 10);
968 switch (weeknum)
970 case 1: mindays = 6;
971 break;
972 case 2: mindays = 3;
973 break;
974 case 0: mindays = 0;
975 break;
976 default:
977 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
978 mindays = 0;
981 if (date->wMonth == 1)
983 /* calculate all those exceptions for January */
984 st.wDay = st.wMonth = 1;
985 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
986 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
987 weeknum = 1;
988 else
990 weeknum = 0;
991 for(i = 0; i < 11; i++)
992 weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
994 weeknum += startofprescal + 7;
995 weeknum /= 7;
996 st.wYear -= 1;
997 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
998 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
1001 else
1003 weeknum = 0;
1004 for(i = 0; i < prev_month - 1; i++)
1005 weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
1007 weeknum += startofprescal + 7;
1008 weeknum /= 7;
1009 st.wDay = st.wMonth = 1;
1010 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
1011 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
1014 r = infoPtr->calendars[calIdx].weeknums;
1016 /* erase whole week numbers area */
1017 FillRect(hdc, &r, infoPtr->brushes[BrushMonth]);
1018 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1020 /* reduce rectangle to one week number */
1021 r.bottom = r.top + infoPtr->height_increment;
1023 for(i = 0; i < 6; i++) {
1024 if((i == 0) && (weeknum > 50))
1026 wsprintfW(buf, L"%d", weeknum);
1027 weeknum = 0;
1029 else if((i == 5) && (weeknum > 47))
1031 wsprintfW(buf, L"%d", 1);
1033 else
1034 wsprintfW(buf, L"%d", weeknum + i);
1036 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1037 OffsetRect(&r, 0, infoPtr->height_increment);
1040 /* line separator for week numbers column */
1041 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1042 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
1043 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
1044 SelectObject(hdc, old_pen);
1047 /* bottom today date */
1048 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1050 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
1051 RECT text_rect, box_rect;
1052 HFONT old_font;
1053 INT col;
1055 if(infoPtr->dwStyle & MCS_NOTODAY) return;
1057 LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, ARRAY_SIZE(buf_todayW));
1058 col = infoPtr->dwStyle & MCS_NOTODAYCIRCLE ? 0 : 1;
1059 if (infoPtr->dwStyle & MCS_WEEKNUMBERS) col--;
1060 /* label is located below first calendar last row */
1061 MONTHCAL_GetDayRectI(infoPtr, &text_rect, col, 6, infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx);
1062 box_rect = text_rect;
1064 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL, buf_dateW, ARRAY_SIZE(buf_dateW));
1065 old_font = SelectObject(hdc, infoPtr->hBoldFont);
1066 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1068 wsprintfW(buf, L"%s %s", buf_todayW, buf_dateW);
1069 DrawTextW(hdc, buf, -1, &text_rect, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1070 DrawTextW(hdc, buf, -1, &text_rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1072 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
1073 OffsetRect(&box_rect, -infoPtr->width_increment, 0);
1074 MONTHCAL_Circle(infoPtr, hdc, &box_rect);
1077 SelectObject(hdc, old_font);
1080 /* today mark + focus */
1081 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1083 /* circle today date if only it's in fully visible month */
1084 if (!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
1086 INT i;
1088 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1089 if (!MONTHCAL_CompareMonths(&infoPtr->todaysDate, &infoPtr->calendars[i].month))
1091 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
1092 break;
1096 if (!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
1098 RECT r;
1099 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
1100 DrawFocusRect(hdc, &r);
1104 /* months before first calendar month and after last calendar month */
1105 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1107 INT mask, index;
1108 UINT length;
1109 SYSTEMTIME st_max, st;
1111 if (infoPtr->dwStyle & MCS_NOTRAILINGDATES) return;
1113 SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
1115 /* draw prev month */
1116 MONTHCAL_GetMinDate(infoPtr, &st);
1117 mask = 1 << (st.wDay-1);
1118 /* December and January both 31 days long, so no worries if wrapped */
1119 length = MONTHCAL_MonthLength(infoPtr->calendars[0].month.wMonth - 1,
1120 infoPtr->calendars[0].month.wYear);
1121 index = 0;
1122 while(st.wDay <= length)
1124 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1125 mask <<= 1;
1126 st.wDay++;
1129 /* draw next month */
1130 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1131 st.wDay = 1;
1132 MONTHCAL_GetNextMonth(&st);
1133 MONTHCAL_GetMaxDate(infoPtr, &st_max);
1134 mask = 1;
1135 index = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)-1;
1136 while(st.wDay <= st_max.wDay)
1138 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1139 mask <<= 1;
1140 st.wDay++;
1144 static int get_localized_dayname(const MONTHCAL_INFO *infoPtr, unsigned int day, WCHAR *buff, unsigned int count)
1146 LCTYPE lctype;
1148 if (infoPtr->dwStyle & MCS_SHORTDAYSOFWEEK)
1149 lctype = LOCALE_SSHORTESTDAYNAME1 + day;
1150 else
1151 lctype = LOCALE_SABBREVDAYNAME1 + day;
1153 return GetLocaleInfoW(LOCALE_USER_DEFAULT, lctype, buff, count);
1156 /* paint a calendar area */
1157 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
1159 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
1160 INT i, j;
1161 UINT length;
1162 RECT r, fill_bk_rect;
1163 SYSTEMTIME st;
1164 WCHAR buf[80];
1165 HPEN old_pen;
1166 int mask;
1168 /* fill whole days area - from week days area to today note rectangle */
1169 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
1170 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
1171 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
1173 FillRect(hdc, &fill_bk_rect, infoPtr->brushes[BrushMonth]);
1175 /* draw line under day abbreviations */
1176 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1177 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
1178 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
1179 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
1180 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
1181 SelectObject(hdc, old_pen);
1183 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
1184 infoPtr->calendars[calIdx].weeknums.right;
1186 /* draw day abbreviations */
1187 SelectObject(hdc, infoPtr->hFont);
1188 SetBkColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
1189 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1190 /* rectangle to draw a single day abbreviation within */
1191 r = infoPtr->calendars[calIdx].wdays;
1192 r.right = r.left + infoPtr->width_increment;
1194 i = infoPtr->firstDay;
1195 for(j = 0; j < 7; j++) {
1196 get_localized_dayname(infoPtr, (i + j + 6) % 7, buf, ARRAY_SIZE(buf));
1197 DrawTextW(hdc, buf, lstrlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1198 OffsetRect(&r, infoPtr->width_increment, 0);
1201 /* draw current month */
1202 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1203 st = *date;
1204 st.wDay = 1;
1205 mask = 1;
1206 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1207 while(st.wDay <= length)
1209 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[calIdx+1] & mask, ps);
1210 mask <<= 1;
1211 st.wDay++;
1215 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1217 COLORREF old_text_clr, old_bk_clr;
1218 HFONT old_font;
1219 INT i;
1221 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1222 old_bk_clr = GetBkColor(hdc);
1223 old_font = GetCurrentObject(hdc, OBJ_FONT);
1225 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1227 RECT *title = &infoPtr->calendars[i].title;
1228 RECT r;
1230 /* draw title, redraw all its elements */
1231 if (IntersectRect(&r, &(ps->rcPaint), title))
1232 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1234 /* draw calendar area */
1235 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1236 if (IntersectRect(&r, &(ps->rcPaint), &r))
1237 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1239 /* week numbers */
1240 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1243 /* partially visible months */
1244 MONTHCAL_PaintLeadTrailMonths(infoPtr, hdc, ps);
1246 /* focus and today rectangle */
1247 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1249 /* today at the bottom left */
1250 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1252 /* navigation buttons */
1253 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_BACKWARD);
1254 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_FORWARD);
1256 /* restore context */
1257 SetBkColor(hdc, old_bk_clr);
1258 SelectObject(hdc, old_font);
1259 SetTextColor(hdc, old_text_clr);
1262 static LRESULT
1263 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, RECT *rect)
1265 TRACE("rect %p\n", rect);
1267 if(!rect) return FALSE;
1269 *rect = infoPtr->calendars[0].title;
1270 rect->bottom = infoPtr->calendars[0].days.bottom + infoPtr->todayrect.bottom -
1271 infoPtr->todayrect.top;
1273 AdjustWindowRect(rect, infoPtr->dwStyle, FALSE);
1275 /* minimal rectangle is zero based */
1276 OffsetRect(rect, -rect->left, -rect->top);
1278 TRACE("%s\n", wine_dbgstr_rect(rect));
1280 return TRUE;
1283 static COLORREF
1284 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, UINT index)
1286 TRACE("%p, %d\n", infoPtr, index);
1288 if (index > MCSC_TRAILINGTEXT) return -1;
1289 return infoPtr->colors[index];
1292 static LRESULT
1293 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, UINT index, COLORREF color)
1295 enum CachedBrush type;
1296 COLORREF prev;
1298 TRACE("%p, %d: color %#lx\n", infoPtr, index, color);
1300 if (index > MCSC_TRAILINGTEXT) return -1;
1302 prev = infoPtr->colors[index];
1303 infoPtr->colors[index] = color;
1305 /* update cached brush */
1306 switch (index)
1308 case MCSC_BACKGROUND:
1309 type = BrushBackground;
1310 break;
1311 case MCSC_TITLEBK:
1312 type = BrushTitle;
1313 break;
1314 case MCSC_MONTHBK:
1315 type = BrushMonth;
1316 break;
1317 default:
1318 type = BrushLast;
1321 if (type != BrushLast)
1323 DeleteObject(infoPtr->brushes[type]);
1324 infoPtr->brushes[type] = CreateSolidBrush(color);
1327 /* update cached pen */
1328 if (index == MCSC_TEXT)
1330 DeleteObject(infoPtr->pens[PenText]);
1331 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[index]);
1334 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND);
1335 return prev;
1338 static LRESULT
1339 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1341 TRACE("\n");
1343 if(infoPtr->delta)
1344 return infoPtr->delta;
1346 return MONTHCAL_GetMonthRange(infoPtr, GMR_VISIBLE, NULL);
1350 static LRESULT
1351 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1353 INT prev = infoPtr->delta;
1355 TRACE("delta %d\n", delta);
1357 infoPtr->delta = delta;
1358 return prev;
1362 static inline LRESULT
1363 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1365 int day;
1367 /* convert from SYSTEMTIME to locale format */
1368 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1370 return MAKELONG(day, infoPtr->firstDaySet);
1374 /* Sets the first day of the week that will appear in the control
1377 * PARAMETERS:
1378 * [I] infoPtr : valid pointer to control data
1379 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1382 * RETURN VALUE:
1383 * Low word contains previous first day,
1384 * high word indicates was first day forced with this message before or is
1385 * locale defined (TRUE - was forced, FALSE - wasn't).
1387 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1388 * FIXME: we need more error checking here
1390 static LRESULT
1391 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1393 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1394 int new_day;
1396 TRACE("%d\n", day);
1398 if(day == -1)
1400 WCHAR buf[80];
1402 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, ARRAY_SIZE(buf));
1403 TRACE("%s %d\n", debugstr_w(buf), lstrlenW(buf));
1405 new_day = wcstol(buf, NULL, 10);
1407 infoPtr->firstDaySet = FALSE;
1409 else if(day >= 7)
1411 new_day = 6; /* max first day allowed */
1412 infoPtr->firstDaySet = TRUE;
1414 else
1416 /* Native behaviour for that case is broken: invalid date number >31
1417 got displayed at (0,0) position, current month starts always from
1418 (1,0) position. Should be implemented here as well only if there's
1419 nothing else to do. */
1420 if (day < -1)
1421 FIXME("No bug compatibility for day=%d\n", day);
1423 new_day = day;
1424 infoPtr->firstDaySet = TRUE;
1427 /* convert from locale to SYSTEMTIME format */
1428 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1430 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1432 return prev;
1435 static LRESULT
1436 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1438 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1441 static LRESULT
1442 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1444 FILETIME ft_min, ft_max;
1446 TRACE("%x %p\n", limits, range);
1448 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1449 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1450 return FALSE;
1452 infoPtr->rangeValid = 0;
1453 infoPtr->minDate = infoPtr->maxDate = st_null;
1455 if (limits & GDTR_MIN)
1457 if (!MONTHCAL_ValidateTime(&range[0]))
1458 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1460 infoPtr->minDate = range[0];
1461 infoPtr->rangeValid |= GDTR_MIN;
1463 if (limits & GDTR_MAX)
1465 if (!MONTHCAL_ValidateTime(&range[1]))
1466 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1468 infoPtr->maxDate = range[1];
1469 infoPtr->rangeValid |= GDTR_MAX;
1472 /* Only one limit set - we are done */
1473 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1474 return TRUE;
1476 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1477 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1479 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1481 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1483 /* Native swaps limits only when both limits are being set. */
1484 SYSTEMTIME st_tmp = infoPtr->minDate;
1485 infoPtr->minDate = infoPtr->maxDate;
1486 infoPtr->maxDate = st_tmp;
1488 else
1490 /* reset the other limit */
1491 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1492 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1493 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1497 return TRUE;
1501 static LRESULT
1502 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1504 TRACE("%p\n", range);
1506 if (!range) return 0;
1508 range[1] = infoPtr->maxDate;
1509 range[0] = infoPtr->minDate;
1511 return infoPtr->rangeValid;
1515 static LRESULT
1516 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1518 TRACE("%p %d %p\n", infoPtr, months, states);
1520 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return 0;
1521 if (months != MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)) return 0;
1523 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1525 return 1;
1528 static LRESULT
1529 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1531 TRACE("%p\n", curSel);
1532 if(!curSel) return FALSE;
1533 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1535 *curSel = infoPtr->minSel;
1536 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1537 return TRUE;
1540 static LRESULT
1541 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1543 SYSTEMTIME prev = infoPtr->minSel, selection;
1544 INT diff;
1545 WORD day;
1547 TRACE("%p\n", curSel);
1548 if(!curSel) return FALSE;
1549 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1551 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1552 /* exit earlier if selection equals current */
1553 if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1555 selection = *curSel;
1556 selection.wHour = selection.wMinute = selection.wSecond = selection.wMilliseconds = 0;
1557 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1559 if(!MONTHCAL_IsDateInValidRange(infoPtr, &selection, FALSE)) return FALSE;
1561 /* scroll calendars only if we have to */
1562 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, curSel);
1563 if (diff <= 0)
1565 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, curSel);
1566 if (diff > 0) diff = 0;
1569 if (diff != 0)
1571 INT i;
1573 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1574 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1577 /* we need to store time part as it is */
1578 selection = *curSel;
1579 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1580 infoPtr->minSel = infoPtr->maxSel = selection;
1582 /* if selection is still in current month, reduce rectangle */
1583 day = prev.wDay;
1584 prev.wDay = curSel->wDay;
1585 if (MONTHCAL_IsDateEqual(&prev, curSel))
1587 RECT r_prev, r_new;
1589 prev.wDay = day;
1590 MONTHCAL_GetDayRect(infoPtr, &prev, &r_prev, -1);
1591 MONTHCAL_GetDayRect(infoPtr, curSel, &r_new, -1);
1593 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1594 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1596 else
1597 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1599 return TRUE;
1603 static LRESULT
1604 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1606 return infoPtr->maxSelCount;
1610 static LRESULT
1611 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1613 TRACE("%d\n", max);
1615 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1616 if(max <= 0) return FALSE;
1618 infoPtr->maxSelCount = max;
1620 return TRUE;
1624 static LRESULT
1625 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1627 TRACE("%p\n", range);
1629 if(!range) return FALSE;
1631 if(infoPtr->dwStyle & MCS_MULTISELECT)
1633 range[1] = infoPtr->maxSel;
1634 range[0] = infoPtr->minSel;
1635 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1636 return TRUE;
1639 return FALSE;
1643 static LRESULT
1644 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1646 SYSTEMTIME old_range[2];
1647 INT diff;
1649 TRACE("%p\n", range);
1651 if(!range || !(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1653 /* adjust timestamps */
1654 if(!MONTHCAL_ValidateTime(&range[0])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1655 if(!MONTHCAL_ValidateTime(&range[1])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1657 /* maximum range exceeded */
1658 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1660 old_range[0] = infoPtr->minSel;
1661 old_range[1] = infoPtr->maxSel;
1663 /* swap if min > max */
1664 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1666 infoPtr->minSel = range[0];
1667 infoPtr->maxSel = range[1];
1669 else
1671 infoPtr->minSel = range[1];
1672 infoPtr->maxSel = range[0];
1675 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, &infoPtr->maxSel);
1676 if (diff < 0)
1678 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, &infoPtr->maxSel);
1679 if (diff > 0) diff = 0;
1682 if (diff != 0)
1684 INT i;
1686 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1687 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1690 /* update day of week */
1691 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1692 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1694 /* redraw if bounds changed */
1695 /* FIXME: no actual need to redraw everything */
1696 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1697 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1699 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1702 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1703 return TRUE;
1707 static LRESULT
1708 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1710 TRACE("%p\n", today);
1712 if(!today) return FALSE;
1713 *today = infoPtr->todaysDate;
1714 return TRUE;
1717 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1719 * RETURN VALUE
1721 * TRUE - today date changed
1722 * FALSE - today date isn't changed
1724 static BOOL
1725 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1727 RECT rect;
1729 if (MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate))
1730 return FALSE;
1732 /* Invalidate old and new today day rectangle, and today label. */
1733 if (MONTHCAL_GetDayRect(infoPtr, &infoPtr->todaysDate, &rect, -1))
1734 InvalidateRect(infoPtr->hwndSelf, &rect, FALSE);
1736 if (MONTHCAL_GetDayRect(infoPtr, today, &rect, -1))
1737 InvalidateRect(infoPtr->hwndSelf, &rect, FALSE);
1739 infoPtr->todaysDate = *today;
1741 InvalidateRect(infoPtr->hwndSelf, &infoPtr->todayrect, FALSE);
1742 return TRUE;
1745 /* MCM_SETTODAT handler */
1746 static LRESULT
1747 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1749 TRACE("%p\n", today);
1751 if (today)
1753 /* remember if date was set successfully */
1754 if (MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1757 return 0;
1760 /* returns calendar index containing specified point, or -1 if it's background */
1761 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1763 RECT r;
1764 INT i;
1766 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1768 /* whole bounding rectangle allows some optimization to compute */
1769 r.left = infoPtr->calendars[i].title.left;
1770 r.top = infoPtr->calendars[i].title.top;
1771 r.bottom = infoPtr->calendars[i].days.bottom;
1772 r.right = infoPtr->calendars[i].days.right;
1774 if (PtInRect(&r, *pt)) return i;
1777 return -1;
1780 static inline UINT fill_hittest_info(const MCHITTESTINFO *src, MCHITTESTINFO *dest)
1782 dest->uHit = src->uHit;
1783 dest->st = src->st;
1785 if (dest->cbSize == sizeof(MCHITTESTINFO))
1786 memcpy(&dest->rc, &src->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1788 return src->uHit;
1791 static LRESULT
1792 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1794 MCHITTESTINFO htinfo;
1795 SYSTEMTIME *ht_month;
1796 INT day, calIdx;
1798 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1800 htinfo.st = st_null;
1801 htinfo.uHit = 0;
1803 /* we should preserve passed fields if hit area doesn't need them */
1804 if (lpht->cbSize == sizeof(MCHITTESTINFO))
1805 memcpy(&htinfo.rc, &lpht->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1807 /* guess in what calendar we are */
1808 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1809 if (calIdx == -1)
1811 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1813 htinfo.uHit = MCHT_TODAYLINK;
1814 htinfo.rc = infoPtr->todayrect;
1816 else
1817 /* outside of calendar area? What's left must be background :-) */
1818 htinfo.uHit = MCHT_CALENDARBK;
1820 return fill_hittest_info(&htinfo, lpht);
1823 /* are we in the header? */
1824 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1825 /* FIXME: buttons hittesting could be optimized cause maximum
1826 two calendars have buttons */
1827 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1829 htinfo.uHit = MCHT_TITLEBTNPREV;
1830 htinfo.rc = infoPtr->titlebtnprev;
1832 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1834 htinfo.uHit = MCHT_TITLEBTNNEXT;
1835 htinfo.rc = infoPtr->titlebtnnext;
1837 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1839 htinfo.uHit = MCHT_TITLEMONTH;
1840 htinfo.rc = infoPtr->calendars[calIdx].titlemonth;
1841 htinfo.iOffset = calIdx;
1843 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1845 htinfo.uHit = MCHT_TITLEYEAR;
1846 htinfo.rc = infoPtr->calendars[calIdx].titleyear;
1847 htinfo.iOffset = calIdx;
1849 else
1851 htinfo.uHit = MCHT_TITLE;
1852 htinfo.rc = infoPtr->calendars[calIdx].title;
1853 htinfo.iOffset = calIdx;
1856 return fill_hittest_info(&htinfo, lpht);
1859 ht_month = &infoPtr->calendars[calIdx].month;
1860 /* days area (including week days and week numbers) */
1861 day = MONTHCAL_GetDayFromPos(infoPtr, lpht->pt, calIdx);
1862 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1864 htinfo.uHit = MCHT_CALENDARDAY;
1865 htinfo.iOffset = calIdx;
1866 htinfo.st.wYear = ht_month->wYear;
1867 htinfo.st.wMonth = (day < 1) ? ht_month->wMonth -1 : ht_month->wMonth;
1868 htinfo.st.wDay = (day < 1) ?
1869 MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day : day;
1871 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1873 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1875 htinfo.uHit = MCHT_CALENDARWEEKNUM;
1876 htinfo.st.wYear = ht_month->wYear;
1877 htinfo.iOffset = calIdx;
1879 if (day < 1)
1881 htinfo.st.wMonth = ht_month->wMonth - 1;
1882 htinfo.st.wDay = MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day;
1884 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1886 htinfo.st.wMonth = ht_month->wMonth + 1;
1887 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1889 else
1891 htinfo.st.wMonth = ht_month->wMonth;
1892 htinfo.st.wDay = day;
1895 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1897 htinfo.iOffset = calIdx;
1898 htinfo.st.wDay = ht_month->wDay;
1899 htinfo.st.wYear = ht_month->wYear;
1900 htinfo.st.wMonth = ht_month->wMonth;
1901 /* previous month only valid for first calendar */
1902 if (day < 1 && calIdx == 0)
1904 htinfo.uHit = MCHT_CALENDARDATEPREV;
1905 MONTHCAL_GetPrevMonth(&htinfo.st);
1906 htinfo.st.wDay = MONTHCAL_MonthLength(htinfo.st.wMonth, htinfo.st.wYear) + day;
1908 /* next month only valid for last calendar */
1909 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear) &&
1910 calIdx == MONTHCAL_GetCalCount(infoPtr)-1)
1912 htinfo.uHit = MCHT_CALENDARDATENEXT;
1913 MONTHCAL_GetNextMonth(&htinfo.st);
1914 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1916 /* multiple calendars case - blank areas for previous/next month */
1917 else if (day < 1 || day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1919 htinfo.uHit = MCHT_CALENDARBK;
1921 else
1923 htinfo.uHit = MCHT_CALENDARDATE;
1924 htinfo.st.wDay = day;
1927 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1928 MONTHCAL_GetDayRectI(infoPtr, &htinfo.rc, htinfo.iCol, htinfo.iRow, calIdx);
1929 /* always update day of week */
1930 MONTHCAL_CalculateDayOfWeek(&htinfo.st, TRUE);
1933 return fill_hittest_info(&htinfo, lpht);
1936 /* MCN_GETDAYSTATE notification helper */
1937 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1939 MONTHDAYSTATE *state;
1940 NMDAYSTATE nmds;
1942 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return;
1944 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1945 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1946 nmds.nmhdr.code = MCN_GETDAYSTATE;
1947 nmds.cDayState = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0);
1948 nmds.prgDayState = state = Alloc(nmds.cDayState * sizeof(MONTHDAYSTATE));
1950 MONTHCAL_GetMinDate(infoPtr, &nmds.stStart);
1951 nmds.stStart.wDay = 1;
1953 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1954 memcpy(infoPtr->monthdayState, nmds.prgDayState,
1955 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
1957 Free(state);
1960 /* no valid range check performed */
1961 static void MONTHCAL_Scroll(MONTHCAL_INFO *infoPtr, INT delta, BOOL keep_selection)
1963 INT i, selIdx = -1;
1965 for(i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1967 /* save selection position to shift it later */
1968 if (selIdx == -1 && MONTHCAL_CompareMonths(&infoPtr->minSel, &infoPtr->calendars[i].month) == 0)
1969 selIdx = i;
1971 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, delta);
1974 if (keep_selection)
1975 return;
1977 /* selection is always shifted to first calendar */
1978 if (infoPtr->dwStyle & MCS_MULTISELECT)
1980 SYSTEMTIME range[2];
1982 MONTHCAL_GetSelRange(infoPtr, range);
1983 MONTHCAL_GetMonth(&range[0], delta - selIdx);
1984 MONTHCAL_GetMonth(&range[1], delta - selIdx);
1985 MONTHCAL_SetSelRange(infoPtr, range);
1987 else
1989 SYSTEMTIME st = infoPtr->minSel;
1991 MONTHCAL_GetMonth(&st, delta - selIdx);
1992 MONTHCAL_SetCurSel(infoPtr, &st);
1996 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
1998 INT delta = infoPtr->delta ? infoPtr->delta : MONTHCAL_GetCalCount(infoPtr);
1999 BOOL keep_selection;
2000 SYSTEMTIME st;
2002 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
2004 /* check if change allowed by range set */
2005 if(direction == DIRECTION_BACKWARD)
2007 st = infoPtr->calendars[0].month;
2008 MONTHCAL_GetMonth(&st, -delta);
2010 else
2012 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2013 MONTHCAL_GetMonth(&st, delta);
2016 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
2018 keep_selection = infoPtr->dwStyle & MCS_NOSELCHANGEONNAV;
2019 MONTHCAL_Scroll(infoPtr, direction == DIRECTION_BACKWARD ? -delta : delta, keep_selection);
2020 MONTHCAL_NotifyDayState(infoPtr);
2021 if (!keep_selection)
2022 MONTHCAL_NotifySelectionChange(infoPtr);
2025 static LRESULT
2026 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2028 HMENU hMenu;
2029 POINT menupoint;
2030 WCHAR buf[32];
2032 hMenu = CreatePopupMenu();
2033 LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, ARRAY_SIZE(buf));
2034 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
2035 menupoint.x = (short)LOWORD(lParam);
2036 menupoint.y = (short)HIWORD(lParam);
2037 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2038 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
2039 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
2041 if (infoPtr->dwStyle & MCS_MULTISELECT)
2043 SYSTEMTIME range[2];
2045 range[0] = range[1] = infoPtr->todaysDate;
2046 MONTHCAL_SetSelRange(infoPtr, range);
2048 else
2049 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2051 MONTHCAL_NotifySelectionChange(infoPtr);
2052 MONTHCAL_NotifySelect(infoPtr);
2055 return 0;
2058 /***
2059 * DESCRIPTION:
2060 * Subclassed edit control windproc function
2062 * PARAMETER(S):
2063 * [I] hwnd : the edit window handle
2064 * [I] uMsg : the message that is to be processed
2065 * [I] wParam : first message parameter
2066 * [I] lParam : second message parameter
2069 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2071 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
2073 TRACE("hwnd %p, uMsg %x, wParam %Ix, lParam %Ix\n", hwnd, uMsg, wParam, lParam);
2075 switch (uMsg)
2077 case WM_GETDLGCODE:
2078 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
2080 case WM_DESTROY:
2082 WNDPROC editProc = infoPtr->EditWndProc;
2083 infoPtr->EditWndProc = NULL;
2084 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
2085 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
2088 case WM_KILLFOCUS:
2089 break;
2091 case WM_KEYDOWN:
2092 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
2093 break;
2095 default:
2096 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
2099 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
2100 SendMessageW(hwnd, WM_CLOSE, 0, 0);
2101 return 0;
2104 /* creates updown control and edit box */
2105 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr, INT calIdx)
2107 RECT *rc = &infoPtr->calendars[calIdx].titleyear;
2108 RECT *title = &infoPtr->calendars[calIdx].title;
2110 infoPtr->hWndYearEdit =
2111 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
2112 rc->left + 3, (title->bottom + title->top - infoPtr->textHeight) / 2,
2113 rc->right - rc->left + 4,
2114 infoPtr->textHeight, infoPtr->hwndSelf,
2115 NULL, NULL, NULL);
2117 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
2119 infoPtr->hWndYearUpDown =
2120 CreateWindowExW(0, UPDOWN_CLASSW, 0,
2121 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
2122 rc->right + 7, (title->bottom + title->top - infoPtr->textHeight) / 2,
2123 18, infoPtr->textHeight, infoPtr->hwndSelf,
2124 NULL, NULL, NULL);
2126 /* attach edit box */
2127 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
2128 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
2129 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
2130 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->calendars[calIdx].month.wYear);
2132 /* subclass edit box */
2133 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
2134 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
2136 SetFocus(infoPtr->hWndYearEdit);
2139 static LRESULT
2140 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2142 MCHITTESTINFO ht;
2143 DWORD hit;
2145 /* Actually we don't need input focus for calendar, this is used to kill
2146 year updown and its buddy edit box */
2147 if (IsWindow(infoPtr->hWndYearUpDown))
2149 SetFocus(infoPtr->hwndSelf);
2150 return 0;
2153 SetCapture(infoPtr->hwndSelf);
2155 ht.cbSize = sizeof(MCHITTESTINFO);
2156 ht.pt.x = (short)LOWORD(lParam);
2157 ht.pt.y = (short)HIWORD(lParam);
2159 hit = MONTHCAL_HitTest(infoPtr, &ht);
2161 TRACE("%lx at %s\n", hit, wine_dbgstr_point(&ht.pt));
2163 switch(hit)
2165 case MCHT_TITLEBTNNEXT:
2166 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2167 infoPtr->status = MC_NEXTPRESSED;
2168 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2169 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2170 return 0;
2172 case MCHT_TITLEBTNPREV:
2173 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2174 infoPtr->status = MC_PREVPRESSED;
2175 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2176 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2177 return 0;
2179 case MCHT_TITLEMONTH:
2181 HMENU hMenu = CreatePopupMenu();
2182 WCHAR buf[32];
2183 POINT menupoint;
2184 INT i;
2186 for (i = 0; i < 12; i++)
2188 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, ARRAY_SIZE(buf));
2189 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
2191 menupoint.x = ht.pt.x;
2192 menupoint.y = ht.pt.y;
2193 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2194 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
2195 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
2197 if ((i > 0) && (i < 13) && infoPtr->calendars[ht.iOffset].month.wMonth != i)
2199 INT delta = i - infoPtr->calendars[ht.iOffset].month.wMonth;
2200 SYSTEMTIME st;
2202 /* check if change allowed by range set */
2203 st = delta < 0 ? infoPtr->calendars[0].month :
2204 infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2205 MONTHCAL_GetMonth(&st, delta);
2207 if (MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE))
2209 MONTHCAL_Scroll(infoPtr, delta, FALSE);
2210 MONTHCAL_NotifyDayState(infoPtr);
2211 MONTHCAL_NotifySelectionChange(infoPtr);
2212 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2215 return 0;
2217 case MCHT_TITLEYEAR:
2219 MONTHCAL_EditYear(infoPtr, ht.iOffset);
2220 return 0;
2222 case MCHT_TODAYLINK:
2224 if (infoPtr->dwStyle & MCS_MULTISELECT)
2226 SYSTEMTIME range[2];
2228 range[0] = range[1] = infoPtr->todaysDate;
2229 MONTHCAL_SetSelRange(infoPtr, range);
2231 else
2232 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2234 MONTHCAL_NotifySelectionChange(infoPtr);
2235 MONTHCAL_NotifySelect(infoPtr);
2236 return 0;
2238 case MCHT_CALENDARDATENEXT:
2239 case MCHT_CALENDARDATEPREV:
2240 case MCHT_CALENDARDATE:
2242 SYSTEMTIME st[2];
2244 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2246 st[0] = st[1] = ht.st;
2247 /* clear selection range */
2248 MONTHCAL_SetSelRange(infoPtr, st);
2250 infoPtr->status = MC_SEL_LBUTDOWN;
2251 if (MONTHCAL_SetDayFocus(infoPtr, &ht.st) && (infoPtr->dwStyle & MCS_MULTISELECT))
2252 MONTHCAL_NotifySelectionChange(infoPtr);
2253 return 0;
2257 return 1;
2261 static LRESULT
2262 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2264 NMHDR nmhdr;
2265 MCHITTESTINFO ht;
2266 DWORD hit;
2268 TRACE("\n");
2270 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2271 RECT *r;
2273 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2274 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2275 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2277 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2280 ReleaseCapture();
2282 /* always send NM_RELEASEDCAPTURE notification */
2283 nmhdr.hwndFrom = infoPtr->hwndSelf;
2284 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2285 nmhdr.code = NM_RELEASEDCAPTURE;
2286 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2288 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2290 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2292 ht.cbSize = sizeof(MCHITTESTINFO);
2293 ht.pt.x = (short)LOWORD(lParam);
2294 ht.pt.y = (short)HIWORD(lParam);
2295 hit = MONTHCAL_HitTest(infoPtr, &ht);
2297 infoPtr->status = MC_SEL_LBUTUP;
2298 MONTHCAL_SetDayFocus(infoPtr, NULL);
2300 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2302 SYSTEMTIME sel = infoPtr->minSel;
2304 /* will be invalidated here */
2305 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2307 /* send MCN_SELCHANGE only if new date selected */
2308 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2309 MONTHCAL_NotifySelectionChange(infoPtr);
2311 MONTHCAL_NotifySelect(infoPtr);
2314 return 0;
2318 static LRESULT
2319 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2321 TRACE("%Id\n", id);
2323 switch(id) {
2324 case MC_PREVNEXTMONTHTIMER:
2325 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2326 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2327 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2328 break;
2329 case MC_TODAYUPDATETIMER:
2331 SYSTEMTIME st;
2333 if(infoPtr->todaySet) return 0;
2335 GetLocalTime(&st);
2336 MONTHCAL_UpdateToday(infoPtr, &st);
2338 /* notification sent anyway */
2339 MONTHCAL_NotifySelectionChange(infoPtr);
2341 return 0;
2343 default:
2344 ERR("got unknown timer %Id\n", id);
2345 break;
2348 return 0;
2352 static LRESULT
2353 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2355 MCHITTESTINFO ht;
2356 SYSTEMTIME st_ht;
2357 INT hit;
2358 RECT r;
2360 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2362 ht.cbSize = sizeof(MCHITTESTINFO);
2363 ht.pt.x = (short)LOWORD(lParam);
2364 ht.pt.y = (short)HIWORD(lParam);
2365 ht.iOffset = -1;
2367 hit = MONTHCAL_HitTest(infoPtr, &ht);
2369 /* not on the calendar date numbers? bail out */
2370 TRACE("hit:%x\n",hit);
2371 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2373 MONTHCAL_SetDayFocus(infoPtr, NULL);
2374 return 0;
2377 st_ht = ht.st;
2379 /* if pointer is over focused day still there's nothing to do */
2380 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2382 MONTHCAL_GetDayRect(infoPtr, &ht.st, &r, ht.iOffset);
2384 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2385 SYSTEMTIME st[2];
2387 MONTHCAL_GetSelRange(infoPtr, st);
2389 /* If we're still at the first selected date and range is empty, return.
2390 If range isn't empty we should change range to a single firstSel */
2391 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2392 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2394 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2396 st[0] = infoPtr->firstSel;
2397 /* we should overwrite timestamp here */
2398 MONTHCAL_CopyDate(&st_ht, &st[1]);
2400 /* bounds will be swapped here if needed */
2401 MONTHCAL_SetSelRange(infoPtr, st);
2403 return 0;
2406 done:
2408 /* FIXME: this should specify a rectangle containing only the days that changed
2409 using InvalidateRect */
2410 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2412 return 0;
2416 static LRESULT
2417 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2419 HDC hdc;
2420 PAINTSTRUCT ps;
2422 if (hdc_paint)
2424 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2425 hdc = hdc_paint;
2427 else
2428 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2430 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2431 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2432 return 0;
2435 static LRESULT
2436 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2438 RECT rc;
2440 if (!GetClipBox(hdc, &rc)) return FALSE;
2442 FillRect(hdc, &rc, infoPtr->brushes[BrushBackground]);
2444 return TRUE;
2447 static LRESULT
2448 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2450 FIXME("Partial Stub: (hdc %p options %#lx)\n", hdc, options);
2452 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2453 return 0;
2455 if (options & PRF_ERASEBKGND)
2456 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2458 if (options & PRF_CLIENT)
2459 MONTHCAL_Paint(infoPtr, hdc);
2461 return 0;
2464 static LRESULT
2465 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2467 TRACE("\n");
2469 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2471 return 0;
2474 /* sets the size information */
2475 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2477 RECT *title=&infoPtr->calendars[0].title;
2478 RECT *prev=&infoPtr->titlebtnprev;
2479 RECT *next=&infoPtr->titlebtnnext;
2480 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2481 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2482 RECT *wdays=&infoPtr->calendars[0].wdays;
2483 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2484 RECT *days=&infoPtr->calendars[0].days;
2485 RECT *todayrect=&infoPtr->todayrect;
2487 INT xdiv, dx, dy, i, j, x, y, c_dx, c_dy;
2488 WCHAR buff[80];
2489 TEXTMETRICW tm;
2490 INT day_width;
2491 RECT client;
2492 HFONT font;
2493 SIZE size;
2494 HDC hdc;
2496 GetClientRect(infoPtr->hwndSelf, &client);
2498 hdc = GetDC(infoPtr->hwndSelf);
2499 font = SelectObject(hdc, infoPtr->hFont);
2501 /* get the height and width of each day's text */
2502 GetTextMetricsW(hdc, &tm);
2503 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2505 /* find widest day name for current locale and font */
2506 day_width = 0;
2507 for (i = 0; i < 7; i++)
2509 SIZE sz;
2511 if (get_localized_dayname(infoPtr, i, buff, ARRAY_SIZE(buff)))
2513 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2514 if (sz.cx > day_width) day_width = sz.cx;
2516 else /* locale independent fallback on failure */
2518 GetTextExtentPoint32W(hdc, L"Sun", 3, &sz);
2519 day_width = sz.cx;
2520 break;
2524 day_width += 2;
2526 /* recalculate the height and width increments and offsets */
2527 size.cx = 0;
2528 GetTextExtentPoint32W(hdc, L"00", 2, &size);
2530 /* restore the originally selected font */
2531 SelectObject(hdc, font);
2532 ReleaseDC(infoPtr->hwndSelf, hdc);
2534 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2536 infoPtr->width_increment = max(day_width, size.cx * 2 + 4);
2537 infoPtr->height_increment = infoPtr->textHeight;
2539 /* calculate title area */
2540 title->top = 0;
2541 title->bottom = 3 * infoPtr->height_increment / 2;
2542 title->left = 0;
2543 title->right = infoPtr->width_increment * xdiv;
2545 /* set the dimensions of the next and previous buttons and center */
2546 /* the month text vertically */
2547 prev->top = next->top = title->top + 4;
2548 prev->bottom = next->bottom = title->bottom - 4;
2549 prev->left = title->left + 4;
2550 prev->right = prev->left + (title->bottom - title->top);
2551 next->right = title->right - 4;
2552 next->left = next->right - (title->bottom - title->top);
2554 /* titlemonth->left and right change based upon the current month
2555 and are recalculated in refresh as the current month may change
2556 without the control being resized */
2557 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2558 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2560 /* week numbers */
2561 weeknumrect->left = 0;
2562 weeknumrect->right = infoPtr->dwStyle & MCS_WEEKNUMBERS ? prev->right : 0;
2564 /* days abbreviated names */
2565 wdays->left = days->left = weeknumrect->right;
2566 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2567 wdays->top = title->bottom;
2568 wdays->bottom = wdays->top + infoPtr->height_increment;
2570 days->top = weeknumrect->top = wdays->bottom;
2571 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2573 todayrect->left = 0;
2574 todayrect->right = title->right;
2575 todayrect->top = days->bottom;
2576 todayrect->bottom = days->bottom + infoPtr->height_increment;
2578 /* compute calendar count, update all calendars */
2579 x = (client.right + MC_CALENDAR_PADDING) / (title->right - title->left + MC_CALENDAR_PADDING);
2580 /* today label affects whole height */
2581 if (infoPtr->dwStyle & MCS_NOTODAY)
2582 y = (client.bottom + MC_CALENDAR_PADDING) / (days->bottom - title->top + MC_CALENDAR_PADDING);
2583 else
2584 y = (client.bottom - todayrect->bottom + todayrect->top + MC_CALENDAR_PADDING) /
2585 (days->bottom - title->top + MC_CALENDAR_PADDING);
2587 /* TODO: ensure that count is properly adjusted to fit 12 months constraint */
2588 if (x == 0) x = 1;
2589 if (y == 0) y = 1;
2591 if (x*y != MONTHCAL_GetCalCount(infoPtr))
2593 infoPtr->dim.cx = x;
2594 infoPtr->dim.cy = y;
2595 infoPtr->calendars = ReAlloc(infoPtr->calendars, MONTHCAL_GetCalCount(infoPtr)*sizeof(CALENDAR_INFO));
2597 infoPtr->monthdayState = ReAlloc(infoPtr->monthdayState,
2598 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
2599 MONTHCAL_NotifyDayState(infoPtr);
2601 /* update pointers that we'll need */
2602 title = &infoPtr->calendars[0].title;
2603 wdays = &infoPtr->calendars[0].wdays;
2604 days = &infoPtr->calendars[0].days;
2607 for (i = 1; i < MONTHCAL_GetCalCount(infoPtr); i++)
2609 /* set months */
2610 infoPtr->calendars[i] = infoPtr->calendars[0];
2611 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, i);
2614 /* offset all rectangles to center in client area */
2615 c_dx = (client.right - x * title->right - MC_CALENDAR_PADDING * (x-1)) / 2;
2616 c_dy = (client.bottom - y * todayrect->bottom - MC_CALENDAR_PADDING * (y-1)) / 2;
2618 /* if calendar doesn't fit client area show it at left/top bounds */
2619 if (title->left + c_dx < 0) c_dx = 0;
2620 if (title->top + c_dy < 0) c_dy = 0;
2622 for (i = 0; i < y; i++)
2624 for (j = 0; j < x; j++)
2626 dx = j*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx;
2627 dy = i*(days->bottom - title->top + MC_CALENDAR_PADDING) + c_dy;
2629 OffsetRect(&infoPtr->calendars[i*x+j].title, dx, dy);
2630 OffsetRect(&infoPtr->calendars[i*x+j].titlemonth, dx, dy);
2631 OffsetRect(&infoPtr->calendars[i*x+j].titleyear, dx, dy);
2632 OffsetRect(&infoPtr->calendars[i*x+j].wdays, dx, dy);
2633 OffsetRect(&infoPtr->calendars[i*x+j].weeknums, dx, dy);
2634 OffsetRect(&infoPtr->calendars[i*x+j].days, dx, dy);
2638 OffsetRect(prev, c_dx, c_dy);
2639 OffsetRect(next, (x-1)*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx, c_dy);
2641 i = infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx;
2642 todayrect->left = infoPtr->calendars[i].title.left;
2643 todayrect->right = infoPtr->calendars[i].title.right;
2644 todayrect->top = infoPtr->calendars[i].days.bottom;
2645 todayrect->bottom = infoPtr->calendars[i].days.bottom + infoPtr->height_increment;
2647 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2648 infoPtr->width_increment,infoPtr->height_increment,
2649 wine_dbgstr_rect(&client),
2650 wine_dbgstr_rect(title),
2651 wine_dbgstr_rect(wdays),
2652 wine_dbgstr_rect(days),
2653 wine_dbgstr_rect(todayrect));
2656 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2658 TRACE("(width=%d, height=%d)\n", Width, Height);
2660 MONTHCAL_UpdateSize(infoPtr);
2661 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2663 return 0;
2666 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2668 return (LRESULT)infoPtr->hFont;
2671 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2673 HFONT hOldFont;
2674 LOGFONTW lf;
2676 if (!hFont) return 0;
2678 hOldFont = infoPtr->hFont;
2679 infoPtr->hFont = hFont;
2681 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2682 lf.lfWeight = FW_BOLD;
2683 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2685 MONTHCAL_UpdateSize(infoPtr);
2687 if (redraw)
2688 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2690 return (LRESULT)hOldFont;
2693 /* update theme after a WM_THEMECHANGED message */
2694 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2696 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2697 CloseThemeData (theme);
2698 OpenThemeData (infoPtr->hwndSelf, themeClass);
2699 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
2700 return 0;
2703 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2704 const STYLESTRUCT *lpss)
2706 TRACE("styletype %Ix, styleOld %#lx, styleNew %#lx\n", wStyleType, lpss->styleOld, lpss->styleNew);
2708 if (wStyleType != GWL_STYLE) return 0;
2710 infoPtr->dwStyle = lpss->styleNew;
2712 /* make room for week numbers */
2713 if ((lpss->styleNew ^ lpss->styleOld) & (MCS_WEEKNUMBERS | MCS_SHORTDAYSOFWEEK))
2714 MONTHCAL_UpdateSize(infoPtr);
2716 return 0;
2719 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2720 STYLESTRUCT *lpss)
2722 TRACE("styletype %Ix, styleOld %#lx, styleNew %#lx\n", wStyleType, lpss->styleOld, lpss->styleNew);
2724 /* block MCS_MULTISELECT change */
2725 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2727 if (lpss->styleOld & MCS_MULTISELECT)
2728 lpss->styleNew |= MCS_MULTISELECT;
2729 else
2730 lpss->styleNew &= ~MCS_MULTISELECT;
2733 /* block MCS_DAYSTATE change */
2734 if ((lpss->styleNew ^ lpss->styleOld) & MCS_DAYSTATE)
2736 if (lpss->styleOld & MCS_DAYSTATE)
2737 lpss->styleNew |= MCS_DAYSTATE;
2738 else
2739 lpss->styleNew &= ~MCS_DAYSTATE;
2742 return 0;
2745 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2746 static LRESULT
2747 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2749 MONTHCAL_INFO *infoPtr;
2751 /* allocate memory for info structure */
2752 infoPtr = Alloc(sizeof(*infoPtr));
2753 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2755 if (infoPtr == NULL) {
2756 ERR("could not allocate info memory!\n");
2757 return 0;
2760 infoPtr->hwndSelf = hwnd;
2761 infoPtr->hwndNotify = lpcs->hwndParent;
2762 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2763 infoPtr->dim.cx = infoPtr->dim.cy = 1;
2764 infoPtr->calendars = Alloc(sizeof(*infoPtr->calendars));
2765 if (!infoPtr->calendars) goto fail;
2766 infoPtr->monthdayState = Alloc(3 * sizeof(*infoPtr->monthdayState));
2767 if (!infoPtr->monthdayState) goto fail;
2769 /* initialize info structure */
2770 /* FIXME: calculate systemtime ->> localtime(subtract timezoneinfo) */
2772 GetLocalTime(&infoPtr->todaysDate);
2773 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2775 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2777 infoPtr->colors[MCSC_BACKGROUND] = comctl32_color.clrWindow;
2778 infoPtr->colors[MCSC_TEXT] = comctl32_color.clrWindowText;
2779 infoPtr->colors[MCSC_TITLEBK] = comctl32_color.clrActiveCaption;
2780 infoPtr->colors[MCSC_TITLETEXT] = comctl32_color.clrWindow;
2781 infoPtr->colors[MCSC_MONTHBK] = comctl32_color.clrWindow;
2782 infoPtr->colors[MCSC_TRAILINGTEXT] = comctl32_color.clrGrayText;
2784 infoPtr->brushes[BrushBackground] = CreateSolidBrush(infoPtr->colors[MCSC_BACKGROUND]);
2785 infoPtr->brushes[BrushTitle] = CreateSolidBrush(infoPtr->colors[MCSC_TITLEBK]);
2786 infoPtr->brushes[BrushMonth] = CreateSolidBrush(infoPtr->colors[MCSC_MONTHBK]);
2788 infoPtr->pens[PenRed] = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
2789 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[MCSC_TEXT]);
2791 infoPtr->minSel = infoPtr->todaysDate;
2792 infoPtr->maxSel = infoPtr->todaysDate;
2793 infoPtr->calendars[0].month = infoPtr->todaysDate;
2794 infoPtr->isUnicode = TRUE;
2796 /* setup control layout and day state data */
2797 MONTHCAL_UpdateSize(infoPtr);
2799 /* today auto update timer, to be freed only on control destruction */
2800 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2802 OpenThemeData (infoPtr->hwndSelf, themeClass);
2804 return 0;
2806 fail:
2807 Free(infoPtr->monthdayState);
2808 Free(infoPtr->calendars);
2809 Free(infoPtr);
2810 return 0;
2813 static LRESULT
2814 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2816 INT i;
2818 Free(infoPtr->monthdayState);
2819 Free(infoPtr->calendars);
2820 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2822 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2824 for (i = 0; i < BrushLast; i++) DeleteObject(infoPtr->brushes[i]);
2825 for (i = 0; i < PenLast; i++) DeleteObject(infoPtr->pens[i]);
2827 Free(infoPtr);
2828 return 0;
2832 * Handler for WM_NOTIFY messages
2834 static LRESULT
2835 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2837 /* notification from year edit updown */
2838 if (hdr->code == UDN_DELTAPOS)
2840 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2842 if (hdr->hwndFrom == infoPtr->hWndYearUpDown && nmud->iDelta)
2844 /* year value limits are set up explicitly after updown creation */
2845 MONTHCAL_Scroll(infoPtr, 12 * nmud->iDelta, FALSE);
2846 MONTHCAL_NotifyDayState(infoPtr);
2847 MONTHCAL_NotifySelectionChange(infoPtr);
2850 return 0;
2853 static inline BOOL
2854 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2856 BOOL prev = infoPtr->isUnicode;
2857 infoPtr->isUnicode = isUnicode;
2858 return prev;
2861 static inline BOOL
2862 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2864 return infoPtr->isUnicode;
2867 static LRESULT WINAPI
2868 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2870 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0);
2872 TRACE("hwnd %p, msg %x, wparam %Ix, lparam %Ix\n", hwnd, uMsg, wParam, lParam);
2874 if (!infoPtr && (uMsg != WM_CREATE))
2875 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2876 switch(uMsg)
2878 case MCM_GETCURSEL:
2879 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2881 case MCM_SETCURSEL:
2882 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2884 case MCM_GETMAXSELCOUNT:
2885 return MONTHCAL_GetMaxSelCount(infoPtr);
2887 case MCM_SETMAXSELCOUNT:
2888 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2890 case MCM_GETSELRANGE:
2891 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2893 case MCM_SETSELRANGE:
2894 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2896 case MCM_GETMONTHRANGE:
2897 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2899 case MCM_SETDAYSTATE:
2900 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2902 case MCM_GETMINREQRECT:
2903 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2905 case MCM_GETCOLOR:
2906 return MONTHCAL_GetColor(infoPtr, wParam);
2908 case MCM_SETCOLOR:
2909 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2911 case MCM_GETTODAY:
2912 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2914 case MCM_SETTODAY:
2915 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2917 case MCM_HITTEST:
2918 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2920 case MCM_GETFIRSTDAYOFWEEK:
2921 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2923 case MCM_SETFIRSTDAYOFWEEK:
2924 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2926 case MCM_GETRANGE:
2927 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2929 case MCM_SETRANGE:
2930 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2932 case MCM_GETMONTHDELTA:
2933 return MONTHCAL_GetMonthDelta(infoPtr);
2935 case MCM_SETMONTHDELTA:
2936 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2938 case MCM_GETMAXTODAYWIDTH:
2939 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2941 case MCM_SETUNICODEFORMAT:
2942 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2944 case MCM_GETUNICODEFORMAT:
2945 return MONTHCAL_GetUnicodeFormat(infoPtr);
2947 case MCM_GETCALENDARCOUNT:
2948 return MONTHCAL_GetCalCount(infoPtr);
2950 case WM_GETDLGCODE:
2951 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2953 case WM_RBUTTONUP:
2954 return MONTHCAL_RButtonUp(infoPtr, lParam);
2956 case WM_LBUTTONDOWN:
2957 return MONTHCAL_LButtonDown(infoPtr, lParam);
2959 case WM_MOUSEMOVE:
2960 return MONTHCAL_MouseMove(infoPtr, lParam);
2962 case WM_LBUTTONUP:
2963 return MONTHCAL_LButtonUp(infoPtr, lParam);
2965 case WM_PAINT:
2966 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2968 case WM_PRINTCLIENT:
2969 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2971 case WM_ERASEBKGND:
2972 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2974 case WM_SETFOCUS:
2975 return MONTHCAL_SetFocus(infoPtr);
2977 case WM_SIZE:
2978 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2980 case WM_NOTIFY:
2981 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2983 case WM_CREATE:
2984 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2986 case WM_SETFONT:
2987 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2989 case WM_GETFONT:
2990 return MONTHCAL_GetFont(infoPtr);
2992 case WM_TIMER:
2993 return MONTHCAL_Timer(infoPtr, wParam);
2995 case WM_THEMECHANGED:
2996 return theme_changed (infoPtr);
2998 case WM_DESTROY:
2999 return MONTHCAL_Destroy(infoPtr);
3001 case WM_SYSCOLORCHANGE:
3002 COMCTL32_RefreshSysColors();
3003 return 0;
3005 case WM_STYLECHANGED:
3006 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
3008 case WM_STYLECHANGING:
3009 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
3011 default:
3012 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
3013 ERR( "unknown msg %04x, wp %Ix, lp %Ix\n", uMsg, wParam, lParam);
3014 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
3019 void
3020 MONTHCAL_Register(void)
3022 WNDCLASSW wndClass;
3024 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
3025 wndClass.style = CS_GLOBALCLASS;
3026 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
3027 wndClass.cbClsExtra = 0;
3028 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
3029 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3030 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
3031 wndClass.lpszClassName = MONTHCAL_CLASSW;
3033 RegisterClassW(&wndClass);
3037 void
3038 MONTHCAL_Unregister(void)
3040 UnregisterClassW(MONTHCAL_CLASSW, NULL);