ia2comproxy: Introduce new proxy stub DLL for IAccessible2.
[wine.git] / dlls / comctl32 / monthcal.c
blob4fc984f6f4b4e0ed9c22e1368554848d9f5b5c7a
1 /*
2 * Month calendar control
4 * Copyright 1998, 1999 Eric Kohl (ekohl@abo.rhein-zeitung.de)
5 * Copyright 1999 Alex Priem (alexp@sci.kun.nl)
6 * Copyright 1999 Chris Morgan <cmorgan@wpi.edu> and
7 * James Abbatiello <abbeyj@wpi.edu>
8 * Copyright 2000 Uwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de>
9 * Copyright 2009-2011 Nikolay Sivov
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * TODO:
26 * -- MCM_[GS]ETUNICODEFORMAT
27 * -- handle resources better (doesn't work now);
28 * -- take care of internationalization.
29 * -- keyboard handling.
30 * -- search for FIXME
33 #include <math.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "winnls.h"
44 #include "commctrl.h"
45 #include "comctl32.h"
46 #include "uxtheme.h"
47 #include "vssym32.h"
48 #include "wine/debug.h"
49 #include "wine/heap.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
53 #define MC_SEL_LBUTUP 1 /* Left button released */
54 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
55 #define MC_PREVPRESSED 4 /* Prev month button pressed */
56 #define MC_NEXTPRESSED 8 /* Next month button pressed */
57 #define MC_PREVNEXTMONTHDELAY 350 /* when continuously pressing `next/prev
58 month', wait 350 ms before going
59 to the next/prev month */
60 #define MC_TODAYUPDATEDELAY 120000 /* time between today check for update (2 min) */
62 #define MC_PREVNEXTMONTHTIMER 1 /* Timer IDs */
63 #define MC_TODAYUPDATETIMER 2
65 #define MC_CALENDAR_PADDING 6
67 /* convert from days to 100 nanoseconds unit - used as FILETIME unit */
68 #define DAYSTO100NSECS(days) (((ULONGLONG)(days))*24*60*60*10000000)
70 enum CachedPen
72 PenRed = 0,
73 PenText,
74 PenLast
77 enum CachedBrush
79 BrushTitle = 0,
80 BrushMonth,
81 BrushBackground,
82 BrushLast
85 /* single calendar data */
86 typedef struct _CALENDAR_INFO
88 RECT title; /* rect for the header above the calendar */
89 RECT titlemonth; /* the 'month name' text in the header */
90 RECT titleyear; /* the 'year number' text in the header */
91 RECT wdays; /* week days at top */
92 RECT days; /* calendar area */
93 RECT weeknums; /* week numbers at left side */
95 SYSTEMTIME month;/* contains calendar main month/year */
96 } CALENDAR_INFO;
98 typedef struct
100 HWND hwndSelf;
101 DWORD dwStyle; /* cached GWL_STYLE */
103 COLORREF colors[MCSC_TRAILINGTEXT+1];
104 HBRUSH brushes[BrushLast];
105 HPEN pens[PenLast];
107 HFONT hFont;
108 HFONT hBoldFont;
109 int textHeight;
110 int height_increment;
111 int width_increment;
112 INT delta; /* scroll rate; # of months that the */
113 /* control moves when user clicks a scroll button */
114 int firstDay; /* Start month calendar with firstDay's day,
115 stored in SYSTEMTIME format */
116 BOOL firstDaySet; /* first week day differs from locale defined */
118 BOOL isUnicode; /* value set with MCM_SETUNICODE format */
120 MONTHDAYSTATE *monthdayState;
121 SYSTEMTIME todaysDate;
122 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
123 int status; /* See MC_SEL flags */
124 SYSTEMTIME firstSel; /* first selected day */
125 INT maxSelCount;
126 SYSTEMTIME minSel; /* contains single selection when used without MCS_MULTISELECT */
127 SYSTEMTIME maxSel;
128 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
129 DWORD rangeValid;
130 SYSTEMTIME minDate;
131 SYSTEMTIME maxDate;
133 RECT titlebtnnext; /* the `next month' button in the header */
134 RECT titlebtnprev; /* the `prev month' button in the header */
135 RECT todayrect; /* `today: xx/xx/xx' text rect */
136 HWND hwndNotify; /* Window to receive the notifications */
137 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
138 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
139 WNDPROC EditWndProc; /* original Edit window procedure */
141 CALENDAR_INFO *calendars;
142 SIZE dim; /* [cx,cy] - dimensions of calendars matrix, row/column count */
143 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
145 static const WCHAR themeClass[] = L"Scrollbar";
147 /* empty SYSTEMTIME const */
148 static const SYSTEMTIME st_null;
149 /* valid date limits */
150 static const SYSTEMTIME max_allowed_date = { /* wYear */ 9999, /* wMonth */ 12, /* wDayOfWeek */ 0, /* wDay */ 31 };
151 static const SYSTEMTIME min_allowed_date = { /* wYear */ 1752, /* wMonth */ 9, /* wDayOfWeek */ 0, /* wDay */ 14 };
153 /* Prev/Next buttons */
154 enum nav_direction
156 DIRECTION_BACKWARD,
157 DIRECTION_FORWARD
160 /* helper functions */
161 static inline INT MONTHCAL_GetCalCount(const MONTHCAL_INFO *infoPtr)
163 return infoPtr->dim.cx * infoPtr->dim.cy;
166 /* send a single MCN_SELCHANGE notification */
167 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
169 NMSELCHANGE nmsc;
171 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
172 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
173 nmsc.nmhdr.code = MCN_SELCHANGE;
174 nmsc.stSelStart = infoPtr->minSel;
175 nmsc.stSelStart.wDayOfWeek = 0;
176 if(infoPtr->dwStyle & MCS_MULTISELECT){
177 nmsc.stSelEnd = infoPtr->maxSel;
178 nmsc.stSelEnd.wDayOfWeek = 0;
180 else
181 nmsc.stSelEnd = st_null;
183 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
186 /* send a single MCN_SELECT notification */
187 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
189 NMSELCHANGE nmsc;
191 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
192 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
193 nmsc.nmhdr.code = MCN_SELECT;
194 nmsc.stSelStart = infoPtr->minSel;
195 nmsc.stSelStart.wDayOfWeek = 0;
196 if(infoPtr->dwStyle & MCS_MULTISELECT){
197 nmsc.stSelEnd = infoPtr->maxSel;
198 nmsc.stSelEnd.wDayOfWeek = 0;
200 else
201 nmsc.stSelEnd = st_null;
203 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
206 static inline int MONTHCAL_MonthDiff(const SYSTEMTIME *left, const SYSTEMTIME *right)
208 return (right->wYear - left->wYear)*12 + right->wMonth - left->wMonth;
211 /* returns the number of days in any given month, checking for leap days */
212 /* January is 1, December is 12 */
213 int MONTHCAL_MonthLength(int month, int year)
215 static const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
216 /* Wrap around, this eases handling. Getting length only we shouldn't care
217 about year change here cause January and December have
218 the same day quantity */
219 if(month == 0)
220 month = 12;
221 else if(month == 13)
222 month = 1;
224 /* special case for calendar transition year */
225 if(month == min_allowed_date.wMonth && year == min_allowed_date.wYear) return 19;
227 /* if we have a leap year add 1 day to February */
228 /* a leap year is a year either divisible by 400 */
229 /* or divisible by 4 and not by 100 */
230 if(month == 2) { /* February */
231 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
232 (year%4 == 0)) ? 1 : 0);
234 else {
235 return mdays[month - 1];
239 /* compares timestamps using date part only */
240 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
242 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
243 (first->wDay == second->wDay);
246 /* make sure that date fields are valid */
247 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
249 if (time->wMonth < 1 || time->wMonth > 12 )
250 return FALSE;
251 if (time->wDay == 0 || time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear))
252 return FALSE;
254 return TRUE;
257 /* Copies timestamp part only.
259 * PARAMETERS
261 * [I] from : source date
262 * [O] to : dest date
264 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
266 to->wHour = from->wHour;
267 to->wMinute = from->wMinute;
268 to->wSecond = from->wSecond;
271 /* Copies date part only.
273 * PARAMETERS
275 * [I] from : source date
276 * [O] to : dest date
278 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
280 to->wYear = from->wYear;
281 to->wMonth = from->wMonth;
282 to->wDay = from->wDay;
283 to->wDayOfWeek = from->wDayOfWeek;
286 /* Compares two dates in SYSTEMTIME format
288 * PARAMETERS
290 * [I] first : pointer to valid first date data to compare
291 * [I] second : pointer to valid second date data to compare
293 * RETURN VALUE
295 * -1 : first < second
296 * 0 : first == second
297 * 1 : first > second
299 * Note that no date validation performed, already validated values expected.
301 LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
303 FILETIME ft_first, ft_second;
305 SystemTimeToFileTime(first, &ft_first);
306 SystemTimeToFileTime(second, &ft_second);
308 return CompareFileTime(&ft_first, &ft_second);
311 static LONG MONTHCAL_CompareMonths(const SYSTEMTIME *first, const SYSTEMTIME *second)
313 SYSTEMTIME st_first, st_second;
315 st_first = st_second = st_null;
316 MONTHCAL_CopyDate(first, &st_first);
317 MONTHCAL_CopyDate(second, &st_second);
318 st_first.wDay = st_second.wDay = 1;
320 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
323 static LONG MONTHCAL_CompareDate(const SYSTEMTIME *first, const SYSTEMTIME *second)
325 SYSTEMTIME st_first, st_second;
327 st_first = st_second = st_null;
328 MONTHCAL_CopyDate(first, &st_first);
329 MONTHCAL_CopyDate(second, &st_second);
331 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
334 /* Checks largest possible date range and configured one
336 * PARAMETERS
338 * [I] infoPtr : valid pointer to control data
339 * [I] date : pointer to valid date data to check
340 * [I] fix : make date fit valid range
342 * RETURN VALUE
344 * TRUE - date within largest and configured range
345 * FALSE - date is outside largest or configured range
347 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr,
348 SYSTEMTIME *date, BOOL fix)
350 const SYSTEMTIME *fix_st = NULL;
352 if(MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) {
353 fix_st = &max_allowed_date;
355 else if(MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1) {
356 fix_st = &min_allowed_date;
358 else {
359 if(infoPtr->rangeValid & GDTR_MAX) {
360 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
361 fix_st = &infoPtr->maxDate;
365 if(infoPtr->rangeValid & GDTR_MIN) {
366 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
367 fix_st = &infoPtr->minDate;
372 if (fix && fix_st) {
373 date->wYear = fix_st->wYear;
374 date->wMonth = fix_st->wMonth;
377 return !fix_st;
380 /* Checks passed range width with configured maximum selection count
382 * PARAMETERS
384 * [I] infoPtr : valid pointer to control data
385 * [I] range0 : pointer to valid date data (requested bound)
386 * [I] range1 : pointer to valid date data (primary bound)
387 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
389 * Adjust value computed basing on primary bound and current maximum selection
390 * count. For simple range check (without adjusted value required) (range0, range1)
391 * relation means nothing.
393 * RETURN VALUE
395 * TRUE - range is shorter or equal to maximum
396 * FALSE - range is larger than maximum
398 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
399 const SYSTEMTIME *range0,
400 const SYSTEMTIME *range1,
401 SYSTEMTIME *adjust)
403 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
404 FILETIME ft_range0, ft_range1;
405 LONG cmp;
407 SystemTimeToFileTime(range0, &ft_range0);
408 SystemTimeToFileTime(range1, &ft_range1);
410 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
411 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
412 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
413 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
415 cmp = CompareFileTime(&ft_range0, &ft_range1);
417 if(cmp == 1)
418 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
419 else
420 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
422 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
424 if(adjust) {
425 if(cmp == 1)
426 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
427 else
428 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
430 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
431 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
432 FileTimeToSystemTime(&ft_range0, adjust);
435 return FALSE;
437 else return TRUE;
440 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
441 Milliseconds are intentionally not validated. */
442 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
444 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
445 return FALSE;
446 else
447 return TRUE;
450 /* Note:Depending on DST, this may be offset by a day.
451 Need to find out if we're on a DST place & adjust the clock accordingly.
452 Above function assumes we have a valid data.
453 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
454 0 = Sunday.
457 /* Returns the day in the week
459 * PARAMETERS
460 * [i] date : input date
461 * [I] inplace : set calculated value back to date structure
463 * RETURN VALUE
464 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
466 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
468 SYSTEMTIME st = st_null;
469 FILETIME ft;
471 MONTHCAL_CopyDate(date, &st);
473 SystemTimeToFileTime(&st, &ft);
474 FileTimeToSystemTime(&ft, &st);
476 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
478 return st.wDayOfWeek;
481 /* add/subtract 'months' from date */
482 static inline void MONTHCAL_GetMonth(SYSTEMTIME *date, INT months)
484 INT length, m = date->wMonth + months;
486 date->wYear += m > 0 ? (m - 1) / 12 : m / 12 - 1;
487 date->wMonth = m > 0 ? (m - 1) % 12 + 1 : 12 + m % 12;
488 /* fix moving from last day in a month */
489 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
490 if(date->wDay > length) date->wDay = length;
491 MONTHCAL_CalculateDayOfWeek(date, TRUE);
494 /* properly updates date to point on next month */
495 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
497 MONTHCAL_GetMonth(date, 1);
500 /* properly updates date to point on prev month */
501 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
503 MONTHCAL_GetMonth(date, -1);
506 /* Returns full date for a first currently visible day */
507 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
509 /* zero indexed calendar has the earliest date */
510 SYSTEMTIME st_first = infoPtr->calendars[0].month;
511 INT firstDay;
513 st_first.wDay = 1;
514 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
516 *date = infoPtr->calendars[0].month;
517 MONTHCAL_GetPrevMonth(date);
519 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
520 (infoPtr->firstDay - firstDay) % 7 + 1;
522 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
523 date->wDay -= 7;
525 /* fix day of week */
526 MONTHCAL_CalculateDayOfWeek(date, TRUE);
529 /* Returns full date for a last currently visible day */
530 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
532 /* the latest date is in latest calendar */
533 SYSTEMTIME st, *lt_month = &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
534 INT first_day;
536 *date = *lt_month;
537 st = *lt_month;
539 /* day of week of first day of current month */
540 st.wDay = 1;
541 first_day = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
543 MONTHCAL_GetNextMonth(date);
544 MONTHCAL_GetPrevMonth(&st);
546 /* last calendar starts with some date from previous month that not displayed */
547 st.wDay = MONTHCAL_MonthLength(st.wMonth, st.wYear) +
548 (infoPtr->firstDay - first_day) % 7 + 1;
549 if (st.wDay > MONTHCAL_MonthLength(st.wMonth, st.wYear)) st.wDay -= 7;
551 /* Use month length to get max day. 42 means max day count in calendar area */
552 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
553 MONTHCAL_MonthLength(lt_month->wMonth, lt_month->wYear);
555 /* fix day of week */
556 MONTHCAL_CalculateDayOfWeek(date, TRUE);
559 /* From a given point calculate the row, column and day in the calendar,
560 'day == 0' means the last day of the last month. */
561 static int MONTHCAL_GetDayFromPos(const MONTHCAL_INFO *infoPtr, POINT pt, INT calIdx)
563 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
564 int firstDay, col, row;
565 RECT client;
567 GetClientRect(infoPtr->hwndSelf, &client);
569 /* if the point is outside the x bounds of the window put it at the boundary */
570 if (pt.x > client.right) pt.x = client.right;
572 col = (pt.x - infoPtr->calendars[calIdx].days.left ) / infoPtr->width_increment;
573 row = (pt.y - infoPtr->calendars[calIdx].days.top ) / infoPtr->height_increment;
575 st.wDay = 1;
576 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
577 return col + 7 * row - firstDay;
580 /* Get day position for given date and calendar
582 * PARAMETERS
584 * [I] infoPtr : pointer to control data
585 * [I] date : date value
586 * [O] col : day column (zero based)
587 * [O] row : week column (zero based)
588 * [I] calIdx : calendar index
590 static void MONTHCAL_GetDayPos(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
591 INT *col, INT *row, INT calIdx)
593 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
594 INT first;
596 st.wDay = 1;
597 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
599 if (calIdx == 0 || calIdx == MONTHCAL_GetCalCount(infoPtr)-1) {
600 const SYSTEMTIME *cal = &infoPtr->calendars[calIdx].month;
601 LONG cmp = MONTHCAL_CompareMonths(date, &st);
603 /* previous month */
604 if (cmp == -1) {
605 *col = (first - MONTHCAL_MonthLength(date->wMonth, cal->wYear) + date->wDay) % 7;
606 *row = 0;
607 return;
610 /* next month calculation is same as for current, just add current month length */
611 if (cmp == 1)
612 first += MONTHCAL_MonthLength(cal->wMonth, cal->wYear);
615 *col = (date->wDay + first) % 7;
616 *row = (date->wDay + first - *col) / 7;
619 /* returns bounding box for day in given position in given calendar */
620 static inline void MONTHCAL_GetDayRectI(const MONTHCAL_INFO *infoPtr, RECT *r,
621 INT col, INT row, INT calIdx)
623 r->left = infoPtr->calendars[calIdx].days.left + col * infoPtr->width_increment;
624 r->right = r->left + infoPtr->width_increment;
625 r->top = infoPtr->calendars[calIdx].days.top + row * infoPtr->height_increment;
626 r->bottom = r->top + infoPtr->textHeight;
629 /* Returns bounding box for given date
631 * NOTE: when calendar index is unknown pass -1
633 static BOOL MONTHCAL_GetDayRect(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date, RECT *r, INT calIdx)
635 INT col, row;
637 if (!MONTHCAL_ValidateDate(date))
639 SetRectEmpty(r);
640 return FALSE;
643 if (calIdx == -1)
645 INT cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[0].month);
647 if (cmp <= 0)
648 calIdx = 0;
649 else
651 cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month);
652 if (cmp >= 0)
653 calIdx = MONTHCAL_GetCalCount(infoPtr)-1;
654 else
656 for (calIdx = 1; calIdx < MONTHCAL_GetCalCount(infoPtr)-1; calIdx++)
657 if (MONTHCAL_CompareMonths(date, &infoPtr->calendars[calIdx].month) == 0)
658 break;
663 MONTHCAL_GetDayPos(infoPtr, date, &col, &row, calIdx);
664 MONTHCAL_GetDayRectI(infoPtr, r, col, row, calIdx);
666 return TRUE;
669 static LRESULT
670 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
672 INT range;
674 TRACE("flags %#lx, st %p\n", flag, st);
676 switch (flag) {
677 case GMR_VISIBLE:
679 if (st)
681 st[0] = infoPtr->calendars[0].month;
682 st[1] = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
684 if (st[0].wMonth == min_allowed_date.wMonth &&
685 st[0].wYear == min_allowed_date.wYear)
687 st[0].wDay = min_allowed_date.wDay;
689 else
690 st[0].wDay = 1;
691 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
693 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
694 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
697 range = MONTHCAL_GetCalCount(infoPtr);
698 break;
700 case GMR_DAYSTATE:
702 if (st)
704 MONTHCAL_GetMinDate(infoPtr, &st[0]);
705 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
707 /* include two partially visible months */
708 range = MONTHCAL_GetCalCount(infoPtr) + 2;
709 break;
711 default:
712 WARN("Unknown flag value, got %ld\n", flag);
713 range = 0;
716 return range;
719 /* Focused day helper:
721 - set focused date to given value;
722 - reset to zero value if NULL passed;
723 - invalidate previous and new day rectangle only if needed.
725 Returns TRUE if focused day changed, FALSE otherwise.
727 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
729 RECT r;
731 if(st)
733 /* there's nothing to do if it's the same date,
734 mouse move within same date rectangle case */
735 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
737 /* invalidate old focused day */
738 if (MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1))
739 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
741 infoPtr->focusedSel = *st;
744 /* On set invalidates new day, on reset clears previous focused day. */
745 if (MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1))
746 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
748 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
749 infoPtr->focusedSel = st_null;
751 return TRUE;
754 /* draw today boundary box for specified rectangle */
755 static void MONTHCAL_Circle(const MONTHCAL_INFO *infoPtr, HDC hdc, const RECT *r)
757 HPEN old_pen = SelectObject(hdc, infoPtr->pens[PenRed]);
758 HBRUSH old_brush;
760 old_brush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
761 Rectangle(hdc, r->left, r->top, r->right, r->bottom);
763 SelectObject(hdc, old_brush);
764 SelectObject(hdc, old_pen);
767 /* Draw today day mark rectangle
769 * [I] hdc : context to draw in
770 * [I] date : day to mark with rectangle
773 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
774 const SYSTEMTIME *date)
776 RECT r;
778 MONTHCAL_GetDayRect(infoPtr, date, &r, -1);
779 MONTHCAL_Circle(infoPtr, hdc, &r);
782 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
783 int bold, const PAINTSTRUCT *ps)
785 WCHAR buf[10];
786 RECT r, r_temp;
787 COLORREF oldCol = 0;
788 COLORREF oldBk = 0;
789 INT old_bkmode, selection;
791 /* no need to check styles: when selection is not valid, it is set to zero.
792 1 < day < 31, so everything is OK */
793 MONTHCAL_GetDayRect(infoPtr, st, &r, -1);
794 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
796 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
797 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0))
799 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
800 TRACE("%s\n", wine_dbgstr_rect(&r));
801 oldCol = SetTextColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
802 oldBk = SetBkColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
803 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
805 selection = 1;
807 else
808 selection = 0;
810 SelectObject(hdc, bold ? infoPtr->hBoldFont : infoPtr->hFont);
812 old_bkmode = SetBkMode(hdc, TRANSPARENT);
813 wsprintfW(buf, L"%d", st->wDay);
814 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
815 SetBkMode(hdc, old_bkmode);
817 if (selection)
819 SetTextColor(hdc, oldCol);
820 SetBkColor(hdc, oldBk);
824 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, enum nav_direction button)
826 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
827 RECT *r = button == DIRECTION_FORWARD ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
828 BOOL pressed = button == DIRECTION_FORWARD ? infoPtr->status & MC_NEXTPRESSED :
829 infoPtr->status & MC_PREVPRESSED;
830 if (theme)
832 static const int states[] = {
833 /* Prev button */
834 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
835 /* Next button */
836 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
838 int stateNum = button == DIRECTION_FORWARD ? 3 : 0;
839 if (pressed)
840 stateNum += 1;
841 else
843 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
845 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
847 else
849 int style = button == DIRECTION_FORWARD ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
850 if (pressed)
851 style |= DFCS_PUSHED;
852 else
854 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
857 DrawFrameControl(hdc, r, DFC_SCROLL, style);
861 /* paint a title with buttons and month/year string */
862 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
864 RECT *title = &infoPtr->calendars[calIdx].title;
865 const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
866 WCHAR monthW[80], strW[80], fmtW[80], yearW[6] /* valid year range is 1601-30827 */;
867 int yearoffset, monthoffset, shiftX;
868 SIZE sz;
870 /* fill header box */
871 FillRect(hdc, title, infoPtr->brushes[BrushTitle]);
873 /* month/year string */
874 SetBkColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
875 SetTextColor(hdc, infoPtr->colors[MCSC_TITLETEXT]);
876 SelectObject(hdc, infoPtr->hBoldFont);
878 /* draw formatted date string */
879 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_YEARMONTH, st, NULL, strW, ARRAY_SIZE(strW));
880 DrawTextW(hdc, strW, lstrlenW(strW), title, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
882 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SYEARMONTH, fmtW, ARRAY_SIZE(fmtW));
883 wsprintfW(yearW, L"%ld", st->wYear);
885 /* month is trickier as it's possible to have different format pictures, we'll
886 test for M, MM, MMM, and MMMM */
887 if (wcsstr(fmtW, L"MMMM"))
888 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+st->wMonth-1, monthW, ARRAY_SIZE(monthW));
889 else if (wcsstr(fmtW, L"MMM"))
890 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME1+st->wMonth-1, monthW, ARRAY_SIZE(monthW));
891 else if (wcsstr(fmtW, L"MM"))
892 wsprintfW(monthW, L"%02d", st->wMonth);
893 else
894 wsprintfW(monthW, L"%d", st->wMonth);
896 /* update hit boxes */
897 yearoffset = 0;
898 while (strW[yearoffset])
900 if (!wcsncmp(&strW[yearoffset], yearW, lstrlenW(yearW)))
901 break;
902 yearoffset++;
905 monthoffset = 0;
906 while (strW[monthoffset])
908 if (!wcsncmp(&strW[monthoffset], monthW, lstrlenW(monthW)))
909 break;
910 monthoffset++;
913 /* for left limits use offsets */
914 sz.cx = 0;
915 if (yearoffset)
916 GetTextExtentPoint32W(hdc, strW, yearoffset, &sz);
917 infoPtr->calendars[calIdx].titleyear.left = sz.cx;
919 sz.cx = 0;
920 if (monthoffset)
921 GetTextExtentPoint32W(hdc, strW, monthoffset, &sz);
922 infoPtr->calendars[calIdx].titlemonth.left = sz.cx;
924 /* for right limits use actual string parts lengths */
925 GetTextExtentPoint32W(hdc, &strW[yearoffset], lstrlenW(yearW), &sz);
926 infoPtr->calendars[calIdx].titleyear.right = infoPtr->calendars[calIdx].titleyear.left + sz.cx;
928 GetTextExtentPoint32W(hdc, monthW, lstrlenW(monthW), &sz);
929 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
931 /* Finally translate rectangles to match center aligned string,
932 hit rectangles are relative to title rectangle before translation. */
933 GetTextExtentPoint32W(hdc, strW, lstrlenW(strW), &sz);
934 shiftX = (title->right - title->left - sz.cx) / 2 + title->left;
935 OffsetRect(&infoPtr->calendars[calIdx].titleyear, shiftX, 0);
936 OffsetRect(&infoPtr->calendars[calIdx].titlemonth, shiftX, 0);
939 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
941 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
942 INT mindays, weeknum, weeknum1, startofprescal;
943 INT i, prev_month;
944 SYSTEMTIME st;
945 WCHAR buf[80];
946 HPEN old_pen;
947 RECT r;
949 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
951 MONTHCAL_GetMinDate(infoPtr, &st);
952 startofprescal = st.wDay;
953 st = *date;
955 prev_month = date->wMonth - 1;
956 if(prev_month == 0) prev_month = 12;
959 Rules what week to call the first week of a new year:
960 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
961 The week containing Jan 1 is the first week of year
962 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
963 First week of year must contain 4 days of the new year
964 LOCALE_IFIRSTWEEKOFYEAR == 1 (what countries?)
965 The first week of the year must contain only days of the new year
967 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, ARRAY_SIZE(buf));
968 weeknum = wcstol(buf, NULL, 10);
969 switch (weeknum)
971 case 1: mindays = 6;
972 break;
973 case 2: mindays = 3;
974 break;
975 case 0: mindays = 0;
976 break;
977 default:
978 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
979 mindays = 0;
982 if (date->wMonth == 1)
984 /* calculate all those exceptions for January */
985 st.wDay = st.wMonth = 1;
986 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
987 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
988 weeknum = 1;
989 else
991 weeknum = 0;
992 for(i = 0; i < 11; i++)
993 weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
995 weeknum += startofprescal + 7;
996 weeknum /= 7;
997 st.wYear -= 1;
998 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
999 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
1002 else
1004 weeknum = 0;
1005 for(i = 0; i < prev_month - 1; i++)
1006 weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
1008 weeknum += startofprescal + 7;
1009 weeknum /= 7;
1010 st.wDay = st.wMonth = 1;
1011 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
1012 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
1015 r = infoPtr->calendars[calIdx].weeknums;
1017 /* erase whole week numbers area */
1018 FillRect(hdc, &r, infoPtr->brushes[BrushMonth]);
1019 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1021 /* reduce rectangle to one week number */
1022 r.bottom = r.top + infoPtr->height_increment;
1024 for(i = 0; i < 6; i++) {
1025 if((i == 0) && (weeknum > 50))
1027 wsprintfW(buf, L"%d", weeknum);
1028 weeknum = 0;
1030 else if((i == 5) && (weeknum > 47))
1032 wsprintfW(buf, L"%d", 1);
1034 else
1035 wsprintfW(buf, L"%d", weeknum + i);
1037 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1038 OffsetRect(&r, 0, infoPtr->height_increment);
1041 /* line separator for week numbers column */
1042 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1043 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
1044 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
1045 SelectObject(hdc, old_pen);
1048 /* bottom today date */
1049 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1051 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
1052 RECT text_rect, box_rect;
1053 HFONT old_font;
1054 INT col;
1056 if(infoPtr->dwStyle & MCS_NOTODAY) return;
1058 LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, ARRAY_SIZE(buf_todayW));
1059 col = infoPtr->dwStyle & MCS_NOTODAYCIRCLE ? 0 : 1;
1060 if (infoPtr->dwStyle & MCS_WEEKNUMBERS) col--;
1061 /* label is located below first calendar last row */
1062 MONTHCAL_GetDayRectI(infoPtr, &text_rect, col, 6, infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx);
1063 box_rect = text_rect;
1065 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL, buf_dateW, ARRAY_SIZE(buf_dateW));
1066 old_font = SelectObject(hdc, infoPtr->hBoldFont);
1067 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1069 wsprintfW(buf, L"%s %s", buf_todayW, buf_dateW);
1070 DrawTextW(hdc, buf, -1, &text_rect, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1071 DrawTextW(hdc, buf, -1, &text_rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1073 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
1074 OffsetRect(&box_rect, -infoPtr->width_increment, 0);
1075 MONTHCAL_Circle(infoPtr, hdc, &box_rect);
1078 SelectObject(hdc, old_font);
1081 /* today mark + focus */
1082 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1084 /* circle today date if only it's in fully visible month */
1085 if (!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
1087 INT i;
1089 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1090 if (!MONTHCAL_CompareMonths(&infoPtr->todaysDate, &infoPtr->calendars[i].month))
1092 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
1093 break;
1097 if (!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
1099 RECT r;
1100 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
1101 DrawFocusRect(hdc, &r);
1105 /* months before first calendar month and after last calendar month */
1106 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1108 INT mask, index;
1109 UINT length;
1110 SYSTEMTIME st_max, st;
1112 if (infoPtr->dwStyle & MCS_NOTRAILINGDATES) return;
1114 SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
1116 /* draw prev month */
1117 MONTHCAL_GetMinDate(infoPtr, &st);
1118 mask = 1 << (st.wDay-1);
1119 /* December and January both 31 days long, so no worries if wrapped */
1120 length = MONTHCAL_MonthLength(infoPtr->calendars[0].month.wMonth - 1,
1121 infoPtr->calendars[0].month.wYear);
1122 index = 0;
1123 while(st.wDay <= length)
1125 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1126 mask <<= 1;
1127 st.wDay++;
1130 /* draw next month */
1131 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1132 st.wDay = 1;
1133 MONTHCAL_GetNextMonth(&st);
1134 MONTHCAL_GetMaxDate(infoPtr, &st_max);
1135 mask = 1;
1136 index = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)-1;
1137 while(st.wDay <= st_max.wDay)
1139 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1140 mask <<= 1;
1141 st.wDay++;
1145 static int get_localized_dayname(const MONTHCAL_INFO *infoPtr, unsigned int day, WCHAR *buff, unsigned int count)
1147 LCTYPE lctype;
1149 if (infoPtr->dwStyle & MCS_SHORTDAYSOFWEEK)
1150 lctype = LOCALE_SSHORTESTDAYNAME1 + day;
1151 else
1152 lctype = LOCALE_SABBREVDAYNAME1 + day;
1154 return GetLocaleInfoW(LOCALE_USER_DEFAULT, lctype, buff, count);
1157 /* paint a calendar area */
1158 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
1160 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
1161 INT i, j;
1162 UINT length;
1163 RECT r, fill_bk_rect;
1164 SYSTEMTIME st;
1165 WCHAR buf[80];
1166 HPEN old_pen;
1167 int mask;
1169 /* fill whole days area - from week days area to today note rectangle */
1170 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
1171 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
1172 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
1174 FillRect(hdc, &fill_bk_rect, infoPtr->brushes[BrushMonth]);
1176 /* draw line under day abbreviations */
1177 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1178 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
1179 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
1180 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
1181 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
1182 SelectObject(hdc, old_pen);
1184 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
1185 infoPtr->calendars[calIdx].weeknums.right;
1187 /* draw day abbreviations */
1188 SelectObject(hdc, infoPtr->hFont);
1189 SetBkColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
1190 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1191 /* rectangle to draw a single day abbreviation within */
1192 r = infoPtr->calendars[calIdx].wdays;
1193 r.right = r.left + infoPtr->width_increment;
1195 i = infoPtr->firstDay;
1196 for(j = 0; j < 7; j++) {
1197 get_localized_dayname(infoPtr, (i + j + 6) % 7, buf, ARRAY_SIZE(buf));
1198 DrawTextW(hdc, buf, lstrlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1199 OffsetRect(&r, infoPtr->width_increment, 0);
1202 /* draw current month */
1203 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1204 st = *date;
1205 st.wDay = 1;
1206 mask = 1;
1207 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1208 while(st.wDay <= length)
1210 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[calIdx+1] & mask, ps);
1211 mask <<= 1;
1212 st.wDay++;
1216 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1218 COLORREF old_text_clr, old_bk_clr;
1219 HFONT old_font;
1220 INT i;
1222 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1223 old_bk_clr = GetBkColor(hdc);
1224 old_font = GetCurrentObject(hdc, OBJ_FONT);
1226 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1228 RECT *title = &infoPtr->calendars[i].title;
1229 RECT r;
1231 /* draw title, redraw all its elements */
1232 if (IntersectRect(&r, &(ps->rcPaint), title))
1233 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1235 /* draw calendar area */
1236 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1237 if (IntersectRect(&r, &(ps->rcPaint), &r))
1238 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1240 /* week numbers */
1241 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1244 /* partially visible months */
1245 MONTHCAL_PaintLeadTrailMonths(infoPtr, hdc, ps);
1247 /* focus and today rectangle */
1248 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1250 /* today at the bottom left */
1251 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1253 /* navigation buttons */
1254 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_BACKWARD);
1255 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_FORWARD);
1257 /* restore context */
1258 SetBkColor(hdc, old_bk_clr);
1259 SelectObject(hdc, old_font);
1260 SetTextColor(hdc, old_text_clr);
1263 static LRESULT
1264 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, RECT *rect)
1266 TRACE("rect %p\n", rect);
1268 if(!rect) return FALSE;
1270 *rect = infoPtr->calendars[0].title;
1271 rect->bottom = infoPtr->calendars[0].days.bottom + infoPtr->todayrect.bottom -
1272 infoPtr->todayrect.top;
1274 AdjustWindowRect(rect, infoPtr->dwStyle, FALSE);
1276 /* minimal rectangle is zero based */
1277 OffsetRect(rect, -rect->left, -rect->top);
1279 TRACE("%s\n", wine_dbgstr_rect(rect));
1281 return TRUE;
1284 static COLORREF
1285 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, UINT index)
1287 TRACE("%p, %d\n", infoPtr, index);
1289 if (index > MCSC_TRAILINGTEXT) return -1;
1290 return infoPtr->colors[index];
1293 static LRESULT
1294 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, UINT index, COLORREF color)
1296 enum CachedBrush type;
1297 COLORREF prev;
1299 TRACE("%p, %d: color %#lx\n", infoPtr, index, color);
1301 if (index > MCSC_TRAILINGTEXT) return -1;
1303 prev = infoPtr->colors[index];
1304 infoPtr->colors[index] = color;
1306 /* update cached brush */
1307 switch (index)
1309 case MCSC_BACKGROUND:
1310 type = BrushBackground;
1311 break;
1312 case MCSC_TITLEBK:
1313 type = BrushTitle;
1314 break;
1315 case MCSC_MONTHBK:
1316 type = BrushMonth;
1317 break;
1318 default:
1319 type = BrushLast;
1322 if (type != BrushLast)
1324 DeleteObject(infoPtr->brushes[type]);
1325 infoPtr->brushes[type] = CreateSolidBrush(color);
1328 /* update cached pen */
1329 if (index == MCSC_TEXT)
1331 DeleteObject(infoPtr->pens[PenText]);
1332 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[index]);
1335 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND);
1336 return prev;
1339 static LRESULT
1340 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1342 TRACE("\n");
1344 if(infoPtr->delta)
1345 return infoPtr->delta;
1347 return MONTHCAL_GetMonthRange(infoPtr, GMR_VISIBLE, NULL);
1351 static LRESULT
1352 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1354 INT prev = infoPtr->delta;
1356 TRACE("delta %d\n", delta);
1358 infoPtr->delta = delta;
1359 return prev;
1363 static inline LRESULT
1364 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1366 int day;
1368 /* convert from SYSTEMTIME to locale format */
1369 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1371 return MAKELONG(day, infoPtr->firstDaySet);
1375 /* Sets the first day of the week that will appear in the control
1378 * PARAMETERS:
1379 * [I] infoPtr : valid pointer to control data
1380 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1383 * RETURN VALUE:
1384 * Low word contains previous first day,
1385 * high word indicates was first day forced with this message before or is
1386 * locale defined (TRUE - was forced, FALSE - wasn't).
1388 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1389 * FIXME: we need more error checking here
1391 static LRESULT
1392 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1394 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1395 int new_day;
1397 TRACE("%d\n", day);
1399 if(day == -1)
1401 WCHAR buf[80];
1403 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, ARRAY_SIZE(buf));
1404 TRACE("%s %d\n", debugstr_w(buf), lstrlenW(buf));
1406 new_day = wcstol(buf, NULL, 10);
1408 infoPtr->firstDaySet = FALSE;
1410 else if(day >= 7)
1412 new_day = 6; /* max first day allowed */
1413 infoPtr->firstDaySet = TRUE;
1415 else
1417 /* Native behaviour for that case is broken: invalid date number >31
1418 got displayed at (0,0) position, current month starts always from
1419 (1,0) position. Should be implemented here as well only if there's
1420 nothing else to do. */
1421 if (day < -1)
1422 FIXME("No bug compatibility for day=%d\n", day);
1424 new_day = day;
1425 infoPtr->firstDaySet = TRUE;
1428 /* convert from locale to SYSTEMTIME format */
1429 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1431 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1433 return prev;
1436 static LRESULT
1437 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1439 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1442 static LRESULT
1443 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1445 FILETIME ft_min, ft_max;
1447 TRACE("%x %p\n", limits, range);
1449 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1450 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1451 return FALSE;
1453 infoPtr->rangeValid = 0;
1454 infoPtr->minDate = infoPtr->maxDate = st_null;
1456 if (limits & GDTR_MIN)
1458 if (!MONTHCAL_ValidateTime(&range[0]))
1459 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1461 infoPtr->minDate = range[0];
1462 infoPtr->rangeValid |= GDTR_MIN;
1464 if (limits & GDTR_MAX)
1466 if (!MONTHCAL_ValidateTime(&range[1]))
1467 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1469 infoPtr->maxDate = range[1];
1470 infoPtr->rangeValid |= GDTR_MAX;
1473 /* Only one limit set - we are done */
1474 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1475 return TRUE;
1477 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1478 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1480 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1482 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1484 /* Native swaps limits only when both limits are being set. */
1485 SYSTEMTIME st_tmp = infoPtr->minDate;
1486 infoPtr->minDate = infoPtr->maxDate;
1487 infoPtr->maxDate = st_tmp;
1489 else
1491 /* reset the other limit */
1492 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1493 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1494 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1498 return TRUE;
1502 static LRESULT
1503 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1505 TRACE("%p\n", range);
1507 if (!range) return 0;
1509 range[1] = infoPtr->maxDate;
1510 range[0] = infoPtr->minDate;
1512 return infoPtr->rangeValid;
1516 static LRESULT
1517 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1519 TRACE("%p %d %p\n", infoPtr, months, states);
1521 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return 0;
1522 if (months != MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)) return 0;
1524 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1526 return 1;
1529 static LRESULT
1530 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1532 TRACE("%p\n", curSel);
1533 if(!curSel) return FALSE;
1534 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1536 *curSel = infoPtr->minSel;
1537 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1538 return TRUE;
1541 static LRESULT
1542 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1544 SYSTEMTIME prev = infoPtr->minSel, selection;
1545 INT diff;
1546 WORD day;
1548 TRACE("%p\n", curSel);
1549 if(!curSel) return FALSE;
1550 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1552 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1553 /* exit earlier if selection equals current */
1554 if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1556 selection = *curSel;
1557 selection.wHour = selection.wMinute = selection.wSecond = selection.wMilliseconds = 0;
1558 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1560 if(!MONTHCAL_IsDateInValidRange(infoPtr, &selection, FALSE)) return FALSE;
1562 /* scroll calendars only if we have to */
1563 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, curSel);
1564 if (diff <= 0)
1566 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, curSel);
1567 if (diff > 0) diff = 0;
1570 if (diff != 0)
1572 INT i;
1574 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1575 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1578 /* we need to store time part as it is */
1579 selection = *curSel;
1580 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1581 infoPtr->minSel = infoPtr->maxSel = selection;
1583 /* if selection is still in current month, reduce rectangle */
1584 day = prev.wDay;
1585 prev.wDay = curSel->wDay;
1586 if (MONTHCAL_IsDateEqual(&prev, curSel))
1588 RECT r_prev, r_new;
1590 prev.wDay = day;
1591 MONTHCAL_GetDayRect(infoPtr, &prev, &r_prev, -1);
1592 MONTHCAL_GetDayRect(infoPtr, curSel, &r_new, -1);
1594 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1595 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1597 else
1598 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1600 return TRUE;
1604 static LRESULT
1605 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1607 return infoPtr->maxSelCount;
1611 static LRESULT
1612 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1614 TRACE("%d\n", max);
1616 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1617 if(max <= 0) return FALSE;
1619 infoPtr->maxSelCount = max;
1621 return TRUE;
1625 static LRESULT
1626 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1628 TRACE("%p\n", range);
1630 if(!range) return FALSE;
1632 if(infoPtr->dwStyle & MCS_MULTISELECT)
1634 range[1] = infoPtr->maxSel;
1635 range[0] = infoPtr->minSel;
1636 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1637 return TRUE;
1640 return FALSE;
1644 static LRESULT
1645 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1647 SYSTEMTIME old_range[2];
1648 INT diff;
1650 TRACE("%p\n", range);
1652 if(!range || !(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1654 /* adjust timestamps */
1655 if(!MONTHCAL_ValidateTime(&range[0])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1656 if(!MONTHCAL_ValidateTime(&range[1])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1658 /* maximum range exceeded */
1659 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1661 old_range[0] = infoPtr->minSel;
1662 old_range[1] = infoPtr->maxSel;
1664 /* swap if min > max */
1665 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1667 infoPtr->minSel = range[0];
1668 infoPtr->maxSel = range[1];
1670 else
1672 infoPtr->minSel = range[1];
1673 infoPtr->maxSel = range[0];
1676 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, &infoPtr->maxSel);
1677 if (diff < 0)
1679 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, &infoPtr->maxSel);
1680 if (diff > 0) diff = 0;
1683 if (diff != 0)
1685 INT i;
1687 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1688 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1691 /* update day of week */
1692 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1693 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1695 /* redraw if bounds changed */
1696 /* FIXME: no actual need to redraw everything */
1697 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1698 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1700 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1703 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1704 return TRUE;
1708 static LRESULT
1709 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1711 TRACE("%p\n", today);
1713 if(!today) return FALSE;
1714 *today = infoPtr->todaysDate;
1715 return TRUE;
1718 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1720 * RETURN VALUE
1722 * TRUE - today date changed
1723 * FALSE - today date isn't changed
1725 static BOOL
1726 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1728 RECT rect;
1730 if (MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate))
1731 return FALSE;
1733 /* Invalidate old and new today day rectangle, and today label. */
1734 if (MONTHCAL_GetDayRect(infoPtr, &infoPtr->todaysDate, &rect, -1))
1735 InvalidateRect(infoPtr->hwndSelf, &rect, FALSE);
1737 if (MONTHCAL_GetDayRect(infoPtr, today, &rect, -1))
1738 InvalidateRect(infoPtr->hwndSelf, &rect, FALSE);
1740 infoPtr->todaysDate = *today;
1742 InvalidateRect(infoPtr->hwndSelf, &infoPtr->todayrect, FALSE);
1743 return TRUE;
1746 /* MCM_SETTODAT handler */
1747 static LRESULT
1748 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1750 TRACE("%p\n", today);
1752 if (today)
1754 /* remember if date was set successfully */
1755 if (MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1758 return 0;
1761 /* returns calendar index containing specified point, or -1 if it's background */
1762 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1764 RECT r;
1765 INT i;
1767 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1769 /* whole bounding rectangle allows some optimization to compute */
1770 r.left = infoPtr->calendars[i].title.left;
1771 r.top = infoPtr->calendars[i].title.top;
1772 r.bottom = infoPtr->calendars[i].days.bottom;
1773 r.right = infoPtr->calendars[i].days.right;
1775 if (PtInRect(&r, *pt)) return i;
1778 return -1;
1781 static inline UINT fill_hittest_info(const MCHITTESTINFO *src, MCHITTESTINFO *dest)
1783 dest->uHit = src->uHit;
1784 dest->st = src->st;
1786 if (dest->cbSize == sizeof(MCHITTESTINFO))
1787 memcpy(&dest->rc, &src->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1789 return src->uHit;
1792 static LRESULT
1793 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1795 MCHITTESTINFO htinfo;
1796 SYSTEMTIME *ht_month;
1797 INT day, calIdx;
1799 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1801 htinfo.st = st_null;
1802 htinfo.uHit = 0;
1804 /* we should preserve passed fields if hit area doesn't need them */
1805 if (lpht->cbSize == sizeof(MCHITTESTINFO))
1806 memcpy(&htinfo.rc, &lpht->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1808 /* guess in what calendar we are */
1809 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1810 if (calIdx == -1)
1812 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1814 htinfo.uHit = MCHT_TODAYLINK;
1815 htinfo.rc = infoPtr->todayrect;
1817 else
1818 /* outside of calendar area? What's left must be background :-) */
1819 htinfo.uHit = MCHT_CALENDARBK;
1821 return fill_hittest_info(&htinfo, lpht);
1824 /* are we in the header? */
1825 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1826 /* FIXME: buttons hittesting could be optimized cause maximum
1827 two calendars have buttons */
1828 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1830 htinfo.uHit = MCHT_TITLEBTNPREV;
1831 htinfo.rc = infoPtr->titlebtnprev;
1833 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1835 htinfo.uHit = MCHT_TITLEBTNNEXT;
1836 htinfo.rc = infoPtr->titlebtnnext;
1838 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1840 htinfo.uHit = MCHT_TITLEMONTH;
1841 htinfo.rc = infoPtr->calendars[calIdx].titlemonth;
1842 htinfo.iOffset = calIdx;
1844 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1846 htinfo.uHit = MCHT_TITLEYEAR;
1847 htinfo.rc = infoPtr->calendars[calIdx].titleyear;
1848 htinfo.iOffset = calIdx;
1850 else
1852 htinfo.uHit = MCHT_TITLE;
1853 htinfo.rc = infoPtr->calendars[calIdx].title;
1854 htinfo.iOffset = calIdx;
1857 return fill_hittest_info(&htinfo, lpht);
1860 ht_month = &infoPtr->calendars[calIdx].month;
1861 /* days area (including week days and week numbers) */
1862 day = MONTHCAL_GetDayFromPos(infoPtr, lpht->pt, calIdx);
1863 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1865 htinfo.uHit = MCHT_CALENDARDAY;
1866 htinfo.iOffset = calIdx;
1867 htinfo.st.wYear = ht_month->wYear;
1868 htinfo.st.wMonth = (day < 1) ? ht_month->wMonth -1 : ht_month->wMonth;
1869 htinfo.st.wDay = (day < 1) ?
1870 MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day : day;
1872 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1874 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1876 htinfo.uHit = MCHT_CALENDARWEEKNUM;
1877 htinfo.st.wYear = ht_month->wYear;
1878 htinfo.iOffset = calIdx;
1880 if (day < 1)
1882 htinfo.st.wMonth = ht_month->wMonth - 1;
1883 htinfo.st.wDay = MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day;
1885 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1887 htinfo.st.wMonth = ht_month->wMonth + 1;
1888 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1890 else
1892 htinfo.st.wMonth = ht_month->wMonth;
1893 htinfo.st.wDay = day;
1896 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1898 htinfo.iOffset = calIdx;
1899 htinfo.st.wDay = ht_month->wDay;
1900 htinfo.st.wYear = ht_month->wYear;
1901 htinfo.st.wMonth = ht_month->wMonth;
1902 /* previous month only valid for first calendar */
1903 if (day < 1 && calIdx == 0)
1905 htinfo.uHit = MCHT_CALENDARDATEPREV;
1906 MONTHCAL_GetPrevMonth(&htinfo.st);
1907 htinfo.st.wDay = MONTHCAL_MonthLength(htinfo.st.wMonth, htinfo.st.wYear) + day;
1909 /* next month only valid for last calendar */
1910 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear) &&
1911 calIdx == MONTHCAL_GetCalCount(infoPtr)-1)
1913 htinfo.uHit = MCHT_CALENDARDATENEXT;
1914 MONTHCAL_GetNextMonth(&htinfo.st);
1915 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1917 /* multiple calendars case - blank areas for previous/next month */
1918 else if (day < 1 || day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1920 htinfo.uHit = MCHT_CALENDARBK;
1922 else
1924 htinfo.uHit = MCHT_CALENDARDATE;
1925 htinfo.st.wDay = day;
1928 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1929 MONTHCAL_GetDayRectI(infoPtr, &htinfo.rc, htinfo.iCol, htinfo.iRow, calIdx);
1930 /* always update day of week */
1931 MONTHCAL_CalculateDayOfWeek(&htinfo.st, TRUE);
1934 return fill_hittest_info(&htinfo, lpht);
1937 /* MCN_GETDAYSTATE notification helper */
1938 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1940 MONTHDAYSTATE *state;
1941 NMDAYSTATE nmds;
1943 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return;
1945 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1946 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1947 nmds.nmhdr.code = MCN_GETDAYSTATE;
1948 nmds.cDayState = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0);
1949 nmds.prgDayState = state = heap_alloc_zero(nmds.cDayState * sizeof(MONTHDAYSTATE));
1951 MONTHCAL_GetMinDate(infoPtr, &nmds.stStart);
1952 nmds.stStart.wDay = 1;
1954 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1955 memcpy(infoPtr->monthdayState, nmds.prgDayState,
1956 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
1958 heap_free(state);
1961 /* no valid range check performed */
1962 static void MONTHCAL_Scroll(MONTHCAL_INFO *infoPtr, INT delta, BOOL keep_selection)
1964 INT i, selIdx = -1;
1966 for(i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1968 /* save selection position to shift it later */
1969 if (selIdx == -1 && MONTHCAL_CompareMonths(&infoPtr->minSel, &infoPtr->calendars[i].month) == 0)
1970 selIdx = i;
1972 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, delta);
1975 if (keep_selection)
1976 return;
1978 /* selection is always shifted to first calendar */
1979 if (infoPtr->dwStyle & MCS_MULTISELECT)
1981 SYSTEMTIME range[2];
1983 MONTHCAL_GetSelRange(infoPtr, range);
1984 MONTHCAL_GetMonth(&range[0], delta - selIdx);
1985 MONTHCAL_GetMonth(&range[1], delta - selIdx);
1986 MONTHCAL_SetSelRange(infoPtr, range);
1988 else
1990 SYSTEMTIME st = infoPtr->minSel;
1992 MONTHCAL_GetMonth(&st, delta - selIdx);
1993 MONTHCAL_SetCurSel(infoPtr, &st);
1997 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
1999 INT delta = infoPtr->delta ? infoPtr->delta : MONTHCAL_GetCalCount(infoPtr);
2000 BOOL keep_selection;
2001 SYSTEMTIME st;
2003 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
2005 /* check if change allowed by range set */
2006 if(direction == DIRECTION_BACKWARD)
2008 st = infoPtr->calendars[0].month;
2009 MONTHCAL_GetMonth(&st, -delta);
2011 else
2013 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2014 MONTHCAL_GetMonth(&st, delta);
2017 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
2019 keep_selection = infoPtr->dwStyle & MCS_NOSELCHANGEONNAV;
2020 MONTHCAL_Scroll(infoPtr, direction == DIRECTION_BACKWARD ? -delta : delta, keep_selection);
2021 MONTHCAL_NotifyDayState(infoPtr);
2022 if (!keep_selection)
2023 MONTHCAL_NotifySelectionChange(infoPtr);
2026 static LRESULT
2027 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2029 HMENU hMenu;
2030 POINT menupoint;
2031 WCHAR buf[32];
2033 hMenu = CreatePopupMenu();
2034 LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, ARRAY_SIZE(buf));
2035 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
2036 menupoint.x = (short)LOWORD(lParam);
2037 menupoint.y = (short)HIWORD(lParam);
2038 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2039 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
2040 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
2042 if (infoPtr->dwStyle & MCS_MULTISELECT)
2044 SYSTEMTIME range[2];
2046 range[0] = range[1] = infoPtr->todaysDate;
2047 MONTHCAL_SetSelRange(infoPtr, range);
2049 else
2050 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2052 MONTHCAL_NotifySelectionChange(infoPtr);
2053 MONTHCAL_NotifySelect(infoPtr);
2056 return 0;
2059 /***
2060 * DESCRIPTION:
2061 * Subclassed edit control windproc function
2063 * PARAMETER(S):
2064 * [I] hwnd : the edit window handle
2065 * [I] uMsg : the message that is to be processed
2066 * [I] wParam : first message parameter
2067 * [I] lParam : second message parameter
2070 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2072 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
2074 TRACE("hwnd %p, uMsg %x, wParam %Ix, lParam %Ix\n", hwnd, uMsg, wParam, lParam);
2076 switch (uMsg)
2078 case WM_GETDLGCODE:
2079 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
2081 case WM_DESTROY:
2083 WNDPROC editProc = infoPtr->EditWndProc;
2084 infoPtr->EditWndProc = NULL;
2085 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
2086 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
2089 case WM_KILLFOCUS:
2090 break;
2092 case WM_KEYDOWN:
2093 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
2094 break;
2096 default:
2097 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
2100 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
2101 SendMessageW(hwnd, WM_CLOSE, 0, 0);
2102 return 0;
2105 /* creates updown control and edit box */
2106 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr, INT calIdx)
2108 RECT *rc = &infoPtr->calendars[calIdx].titleyear;
2109 RECT *title = &infoPtr->calendars[calIdx].title;
2111 infoPtr->hWndYearEdit =
2112 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
2113 rc->left + 3, (title->bottom + title->top - infoPtr->textHeight) / 2,
2114 rc->right - rc->left + 4,
2115 infoPtr->textHeight, infoPtr->hwndSelf,
2116 NULL, NULL, NULL);
2118 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
2120 infoPtr->hWndYearUpDown =
2121 CreateWindowExW(0, UPDOWN_CLASSW, 0,
2122 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
2123 rc->right + 7, (title->bottom + title->top - infoPtr->textHeight) / 2,
2124 18, infoPtr->textHeight, infoPtr->hwndSelf,
2125 NULL, NULL, NULL);
2127 /* attach edit box */
2128 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
2129 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
2130 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
2131 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->calendars[calIdx].month.wYear);
2133 /* subclass edit box */
2134 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
2135 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
2137 SetFocus(infoPtr->hWndYearEdit);
2140 static LRESULT
2141 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2143 MCHITTESTINFO ht;
2144 DWORD hit;
2146 /* Actually we don't need input focus for calendar, this is used to kill
2147 year updown and its buddy edit box */
2148 if (IsWindow(infoPtr->hWndYearUpDown))
2150 SetFocus(infoPtr->hwndSelf);
2151 return 0;
2154 SetCapture(infoPtr->hwndSelf);
2156 ht.cbSize = sizeof(MCHITTESTINFO);
2157 ht.pt.x = (short)LOWORD(lParam);
2158 ht.pt.y = (short)HIWORD(lParam);
2160 hit = MONTHCAL_HitTest(infoPtr, &ht);
2162 TRACE("%lx at %s\n", hit, wine_dbgstr_point(&ht.pt));
2164 switch(hit)
2166 case MCHT_TITLEBTNNEXT:
2167 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2168 infoPtr->status = MC_NEXTPRESSED;
2169 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2170 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2171 return 0;
2173 case MCHT_TITLEBTNPREV:
2174 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2175 infoPtr->status = MC_PREVPRESSED;
2176 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2177 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2178 return 0;
2180 case MCHT_TITLEMONTH:
2182 HMENU hMenu = CreatePopupMenu();
2183 WCHAR buf[32];
2184 POINT menupoint;
2185 INT i;
2187 for (i = 0; i < 12; i++)
2189 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, ARRAY_SIZE(buf));
2190 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
2192 menupoint.x = ht.pt.x;
2193 menupoint.y = ht.pt.y;
2194 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2195 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
2196 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
2198 if ((i > 0) && (i < 13) && infoPtr->calendars[ht.iOffset].month.wMonth != i)
2200 INT delta = i - infoPtr->calendars[ht.iOffset].month.wMonth;
2201 SYSTEMTIME st;
2203 /* check if change allowed by range set */
2204 st = delta < 0 ? infoPtr->calendars[0].month :
2205 infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2206 MONTHCAL_GetMonth(&st, delta);
2208 if (MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE))
2210 MONTHCAL_Scroll(infoPtr, delta, FALSE);
2211 MONTHCAL_NotifyDayState(infoPtr);
2212 MONTHCAL_NotifySelectionChange(infoPtr);
2213 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2216 return 0;
2218 case MCHT_TITLEYEAR:
2220 MONTHCAL_EditYear(infoPtr, ht.iOffset);
2221 return 0;
2223 case MCHT_TODAYLINK:
2225 if (infoPtr->dwStyle & MCS_MULTISELECT)
2227 SYSTEMTIME range[2];
2229 range[0] = range[1] = infoPtr->todaysDate;
2230 MONTHCAL_SetSelRange(infoPtr, range);
2232 else
2233 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2235 MONTHCAL_NotifySelectionChange(infoPtr);
2236 MONTHCAL_NotifySelect(infoPtr);
2237 return 0;
2239 case MCHT_CALENDARDATENEXT:
2240 case MCHT_CALENDARDATEPREV:
2241 case MCHT_CALENDARDATE:
2243 SYSTEMTIME st[2];
2245 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2247 st[0] = st[1] = ht.st;
2248 /* clear selection range */
2249 MONTHCAL_SetSelRange(infoPtr, st);
2251 infoPtr->status = MC_SEL_LBUTDOWN;
2252 if (MONTHCAL_SetDayFocus(infoPtr, &ht.st) && (infoPtr->dwStyle & MCS_MULTISELECT))
2253 MONTHCAL_NotifySelectionChange(infoPtr);
2254 return 0;
2258 return 1;
2262 static LRESULT
2263 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2265 NMHDR nmhdr;
2266 MCHITTESTINFO ht;
2267 DWORD hit;
2269 TRACE("\n");
2271 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2272 RECT *r;
2274 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2275 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2276 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2278 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2281 ReleaseCapture();
2283 /* always send NM_RELEASEDCAPTURE notification */
2284 nmhdr.hwndFrom = infoPtr->hwndSelf;
2285 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2286 nmhdr.code = NM_RELEASEDCAPTURE;
2287 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2289 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2291 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2293 ht.cbSize = sizeof(MCHITTESTINFO);
2294 ht.pt.x = (short)LOWORD(lParam);
2295 ht.pt.y = (short)HIWORD(lParam);
2296 hit = MONTHCAL_HitTest(infoPtr, &ht);
2298 infoPtr->status = MC_SEL_LBUTUP;
2299 MONTHCAL_SetDayFocus(infoPtr, NULL);
2301 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2303 SYSTEMTIME sel = infoPtr->minSel;
2305 /* will be invalidated here */
2306 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2308 /* send MCN_SELCHANGE only if new date selected */
2309 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2310 MONTHCAL_NotifySelectionChange(infoPtr);
2312 MONTHCAL_NotifySelect(infoPtr);
2315 return 0;
2319 static LRESULT
2320 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2322 TRACE("%Id\n", id);
2324 switch(id) {
2325 case MC_PREVNEXTMONTHTIMER:
2326 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2327 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2328 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2329 break;
2330 case MC_TODAYUPDATETIMER:
2332 SYSTEMTIME st;
2334 if(infoPtr->todaySet) return 0;
2336 GetLocalTime(&st);
2337 MONTHCAL_UpdateToday(infoPtr, &st);
2339 /* notification sent anyway */
2340 MONTHCAL_NotifySelectionChange(infoPtr);
2342 return 0;
2344 default:
2345 ERR("got unknown timer %Id\n", id);
2346 break;
2349 return 0;
2353 static LRESULT
2354 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2356 MCHITTESTINFO ht;
2357 SYSTEMTIME st_ht;
2358 INT hit;
2359 RECT r;
2361 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2363 ht.cbSize = sizeof(MCHITTESTINFO);
2364 ht.pt.x = (short)LOWORD(lParam);
2365 ht.pt.y = (short)HIWORD(lParam);
2366 ht.iOffset = -1;
2368 hit = MONTHCAL_HitTest(infoPtr, &ht);
2370 /* not on the calendar date numbers? bail out */
2371 TRACE("hit:%x\n",hit);
2372 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2374 MONTHCAL_SetDayFocus(infoPtr, NULL);
2375 return 0;
2378 st_ht = ht.st;
2380 /* if pointer is over focused day still there's nothing to do */
2381 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2383 MONTHCAL_GetDayRect(infoPtr, &ht.st, &r, ht.iOffset);
2385 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2386 SYSTEMTIME st[2];
2388 MONTHCAL_GetSelRange(infoPtr, st);
2390 /* If we're still at the first selected date and range is empty, return.
2391 If range isn't empty we should change range to a single firstSel */
2392 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2393 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2395 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2397 st[0] = infoPtr->firstSel;
2398 /* we should overwrite timestamp here */
2399 MONTHCAL_CopyDate(&st_ht, &st[1]);
2401 /* bounds will be swapped here if needed */
2402 MONTHCAL_SetSelRange(infoPtr, st);
2404 return 0;
2407 done:
2409 /* FIXME: this should specify a rectangle containing only the days that changed
2410 using InvalidateRect */
2411 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2413 return 0;
2417 static LRESULT
2418 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2420 HDC hdc;
2421 PAINTSTRUCT ps;
2423 if (hdc_paint)
2425 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2426 hdc = hdc_paint;
2428 else
2429 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2431 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2432 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2433 return 0;
2436 static LRESULT
2437 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2439 RECT rc;
2441 if (!GetClipBox(hdc, &rc)) return FALSE;
2443 FillRect(hdc, &rc, infoPtr->brushes[BrushBackground]);
2445 return TRUE;
2448 static LRESULT
2449 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2451 FIXME("Partial Stub: (hdc %p options %#lx)\n", hdc, options);
2453 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2454 return 0;
2456 if (options & PRF_ERASEBKGND)
2457 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2459 if (options & PRF_CLIENT)
2460 MONTHCAL_Paint(infoPtr, hdc);
2462 return 0;
2465 static LRESULT
2466 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2468 TRACE("\n");
2470 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2472 return 0;
2475 /* sets the size information */
2476 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2478 RECT *title=&infoPtr->calendars[0].title;
2479 RECT *prev=&infoPtr->titlebtnprev;
2480 RECT *next=&infoPtr->titlebtnnext;
2481 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2482 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2483 RECT *wdays=&infoPtr->calendars[0].wdays;
2484 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2485 RECT *days=&infoPtr->calendars[0].days;
2486 RECT *todayrect=&infoPtr->todayrect;
2488 INT xdiv, dx, dy, i, j, x, y, c_dx, c_dy;
2489 WCHAR buff[80];
2490 TEXTMETRICW tm;
2491 INT day_width;
2492 RECT client;
2493 HFONT font;
2494 SIZE size;
2495 HDC hdc;
2497 GetClientRect(infoPtr->hwndSelf, &client);
2499 hdc = GetDC(infoPtr->hwndSelf);
2500 font = SelectObject(hdc, infoPtr->hFont);
2502 /* get the height and width of each day's text */
2503 GetTextMetricsW(hdc, &tm);
2504 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2506 /* find widest day name for current locale and font */
2507 day_width = 0;
2508 for (i = 0; i < 7; i++)
2510 SIZE sz;
2512 if (get_localized_dayname(infoPtr, i, buff, ARRAY_SIZE(buff)))
2514 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2515 if (sz.cx > day_width) day_width = sz.cx;
2517 else /* locale independent fallback on failure */
2519 GetTextExtentPoint32W(hdc, L"Sun", 3, &sz);
2520 day_width = sz.cx;
2521 break;
2525 day_width += 2;
2527 /* recalculate the height and width increments and offsets */
2528 size.cx = 0;
2529 GetTextExtentPoint32W(hdc, L"00", 2, &size);
2531 /* restore the originally selected font */
2532 SelectObject(hdc, font);
2533 ReleaseDC(infoPtr->hwndSelf, hdc);
2535 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2537 infoPtr->width_increment = max(day_width, size.cx * 2 + 4);
2538 infoPtr->height_increment = infoPtr->textHeight;
2540 /* calculate title area */
2541 title->top = 0;
2542 title->bottom = 3 * infoPtr->height_increment / 2;
2543 title->left = 0;
2544 title->right = infoPtr->width_increment * xdiv;
2546 /* set the dimensions of the next and previous buttons and center */
2547 /* the month text vertically */
2548 prev->top = next->top = title->top + 4;
2549 prev->bottom = next->bottom = title->bottom - 4;
2550 prev->left = title->left + 4;
2551 prev->right = prev->left + (title->bottom - title->top);
2552 next->right = title->right - 4;
2553 next->left = next->right - (title->bottom - title->top);
2555 /* titlemonth->left and right change based upon the current month
2556 and are recalculated in refresh as the current month may change
2557 without the control being resized */
2558 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2559 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2561 /* week numbers */
2562 weeknumrect->left = 0;
2563 weeknumrect->right = infoPtr->dwStyle & MCS_WEEKNUMBERS ? prev->right : 0;
2565 /* days abbreviated names */
2566 wdays->left = days->left = weeknumrect->right;
2567 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2568 wdays->top = title->bottom;
2569 wdays->bottom = wdays->top + infoPtr->height_increment;
2571 days->top = weeknumrect->top = wdays->bottom;
2572 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2574 todayrect->left = 0;
2575 todayrect->right = title->right;
2576 todayrect->top = days->bottom;
2577 todayrect->bottom = days->bottom + infoPtr->height_increment;
2579 /* compute calendar count, update all calendars */
2580 x = (client.right + MC_CALENDAR_PADDING) / (title->right - title->left + MC_CALENDAR_PADDING);
2581 /* today label affects whole height */
2582 if (infoPtr->dwStyle & MCS_NOTODAY)
2583 y = (client.bottom + MC_CALENDAR_PADDING) / (days->bottom - title->top + MC_CALENDAR_PADDING);
2584 else
2585 y = (client.bottom - todayrect->bottom + todayrect->top + MC_CALENDAR_PADDING) /
2586 (days->bottom - title->top + MC_CALENDAR_PADDING);
2588 /* TODO: ensure that count is properly adjusted to fit 12 months constraint */
2589 if (x == 0) x = 1;
2590 if (y == 0) y = 1;
2592 if (x*y != MONTHCAL_GetCalCount(infoPtr))
2594 infoPtr->dim.cx = x;
2595 infoPtr->dim.cy = y;
2596 infoPtr->calendars = heap_realloc(infoPtr->calendars, MONTHCAL_GetCalCount(infoPtr)*sizeof(CALENDAR_INFO));
2598 infoPtr->monthdayState = heap_realloc(infoPtr->monthdayState,
2599 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
2600 MONTHCAL_NotifyDayState(infoPtr);
2602 /* update pointers that we'll need */
2603 title = &infoPtr->calendars[0].title;
2604 wdays = &infoPtr->calendars[0].wdays;
2605 days = &infoPtr->calendars[0].days;
2608 for (i = 1; i < MONTHCAL_GetCalCount(infoPtr); i++)
2610 /* set months */
2611 infoPtr->calendars[i] = infoPtr->calendars[0];
2612 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, i);
2615 /* offset all rectangles to center in client area */
2616 c_dx = (client.right - x * title->right - MC_CALENDAR_PADDING * (x-1)) / 2;
2617 c_dy = (client.bottom - y * todayrect->bottom - MC_CALENDAR_PADDING * (y-1)) / 2;
2619 /* if calendar doesn't fit client area show it at left/top bounds */
2620 if (title->left + c_dx < 0) c_dx = 0;
2621 if (title->top + c_dy < 0) c_dy = 0;
2623 for (i = 0; i < y; i++)
2625 for (j = 0; j < x; j++)
2627 dx = j*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx;
2628 dy = i*(days->bottom - title->top + MC_CALENDAR_PADDING) + c_dy;
2630 OffsetRect(&infoPtr->calendars[i*x+j].title, dx, dy);
2631 OffsetRect(&infoPtr->calendars[i*x+j].titlemonth, dx, dy);
2632 OffsetRect(&infoPtr->calendars[i*x+j].titleyear, dx, dy);
2633 OffsetRect(&infoPtr->calendars[i*x+j].wdays, dx, dy);
2634 OffsetRect(&infoPtr->calendars[i*x+j].weeknums, dx, dy);
2635 OffsetRect(&infoPtr->calendars[i*x+j].days, dx, dy);
2639 OffsetRect(prev, c_dx, c_dy);
2640 OffsetRect(next, (x-1)*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx, c_dy);
2642 i = infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx;
2643 todayrect->left = infoPtr->calendars[i].title.left;
2644 todayrect->right = infoPtr->calendars[i].title.right;
2645 todayrect->top = infoPtr->calendars[i].days.bottom;
2646 todayrect->bottom = infoPtr->calendars[i].days.bottom + infoPtr->height_increment;
2648 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2649 infoPtr->width_increment,infoPtr->height_increment,
2650 wine_dbgstr_rect(&client),
2651 wine_dbgstr_rect(title),
2652 wine_dbgstr_rect(wdays),
2653 wine_dbgstr_rect(days),
2654 wine_dbgstr_rect(todayrect));
2657 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2659 TRACE("(width=%d, height=%d)\n", Width, Height);
2661 MONTHCAL_UpdateSize(infoPtr);
2662 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2664 return 0;
2667 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2669 return (LRESULT)infoPtr->hFont;
2672 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2674 HFONT hOldFont;
2675 LOGFONTW lf;
2677 if (!hFont) return 0;
2679 hOldFont = infoPtr->hFont;
2680 infoPtr->hFont = hFont;
2682 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2683 lf.lfWeight = FW_BOLD;
2684 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2686 MONTHCAL_UpdateSize(infoPtr);
2688 if (redraw)
2689 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2691 return (LRESULT)hOldFont;
2694 /* update theme after a WM_THEMECHANGED message */
2695 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2697 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2698 CloseThemeData (theme);
2699 OpenThemeData (infoPtr->hwndSelf, themeClass);
2700 InvalidateRect (infoPtr->hwndSelf, NULL, TRUE);
2701 return 0;
2704 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2705 const STYLESTRUCT *lpss)
2707 TRACE("styletype %Ix, styleOld %#lx, styleNew %#lx\n", wStyleType, lpss->styleOld, lpss->styleNew);
2709 if (wStyleType != GWL_STYLE) return 0;
2711 infoPtr->dwStyle = lpss->styleNew;
2713 /* make room for week numbers */
2714 if ((lpss->styleNew ^ lpss->styleOld) & (MCS_WEEKNUMBERS | MCS_SHORTDAYSOFWEEK))
2715 MONTHCAL_UpdateSize(infoPtr);
2717 return 0;
2720 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2721 STYLESTRUCT *lpss)
2723 TRACE("styletype %Ix, styleOld %#lx, styleNew %#lx\n", wStyleType, lpss->styleOld, lpss->styleNew);
2725 /* block MCS_MULTISELECT change */
2726 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2728 if (lpss->styleOld & MCS_MULTISELECT)
2729 lpss->styleNew |= MCS_MULTISELECT;
2730 else
2731 lpss->styleNew &= ~MCS_MULTISELECT;
2734 /* block MCS_DAYSTATE change */
2735 if ((lpss->styleNew ^ lpss->styleOld) & MCS_DAYSTATE)
2737 if (lpss->styleOld & MCS_DAYSTATE)
2738 lpss->styleNew |= MCS_DAYSTATE;
2739 else
2740 lpss->styleNew &= ~MCS_DAYSTATE;
2743 return 0;
2746 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2747 static LRESULT
2748 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2750 MONTHCAL_INFO *infoPtr;
2752 /* allocate memory for info structure */
2753 infoPtr = heap_alloc_zero(sizeof(*infoPtr));
2754 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2756 if (infoPtr == NULL) {
2757 ERR("could not allocate info memory!\n");
2758 return 0;
2761 infoPtr->hwndSelf = hwnd;
2762 infoPtr->hwndNotify = lpcs->hwndParent;
2763 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2764 infoPtr->dim.cx = infoPtr->dim.cy = 1;
2765 infoPtr->calendars = heap_alloc_zero(sizeof(CALENDAR_INFO));
2766 if (!infoPtr->calendars) goto fail;
2767 infoPtr->monthdayState = heap_alloc_zero(3 * sizeof(MONTHDAYSTATE));
2768 if (!infoPtr->monthdayState) goto fail;
2770 /* initialize info structure */
2771 /* FIXME: calculate systemtime ->> localtime(subtract timezoneinfo) */
2773 GetLocalTime(&infoPtr->todaysDate);
2774 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2776 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2778 infoPtr->colors[MCSC_BACKGROUND] = comctl32_color.clrWindow;
2779 infoPtr->colors[MCSC_TEXT] = comctl32_color.clrWindowText;
2780 infoPtr->colors[MCSC_TITLEBK] = comctl32_color.clrActiveCaption;
2781 infoPtr->colors[MCSC_TITLETEXT] = comctl32_color.clrWindow;
2782 infoPtr->colors[MCSC_MONTHBK] = comctl32_color.clrWindow;
2783 infoPtr->colors[MCSC_TRAILINGTEXT] = comctl32_color.clrGrayText;
2785 infoPtr->brushes[BrushBackground] = CreateSolidBrush(infoPtr->colors[MCSC_BACKGROUND]);
2786 infoPtr->brushes[BrushTitle] = CreateSolidBrush(infoPtr->colors[MCSC_TITLEBK]);
2787 infoPtr->brushes[BrushMonth] = CreateSolidBrush(infoPtr->colors[MCSC_MONTHBK]);
2789 infoPtr->pens[PenRed] = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
2790 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[MCSC_TEXT]);
2792 infoPtr->minSel = infoPtr->todaysDate;
2793 infoPtr->maxSel = infoPtr->todaysDate;
2794 infoPtr->calendars[0].month = infoPtr->todaysDate;
2795 infoPtr->isUnicode = TRUE;
2797 /* setup control layout and day state data */
2798 MONTHCAL_UpdateSize(infoPtr);
2800 /* today auto update timer, to be freed only on control destruction */
2801 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2803 OpenThemeData (infoPtr->hwndSelf, themeClass);
2805 return 0;
2807 fail:
2808 heap_free(infoPtr->monthdayState);
2809 heap_free(infoPtr->calendars);
2810 heap_free(infoPtr);
2811 return 0;
2814 static LRESULT
2815 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2817 INT i;
2819 /* free month calendar info data */
2820 heap_free(infoPtr->monthdayState);
2821 heap_free(infoPtr->calendars);
2822 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2824 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2826 for (i = 0; i < BrushLast; i++) DeleteObject(infoPtr->brushes[i]);
2827 for (i = 0; i < PenLast; i++) DeleteObject(infoPtr->pens[i]);
2829 heap_free(infoPtr);
2830 return 0;
2834 * Handler for WM_NOTIFY messages
2836 static LRESULT
2837 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2839 /* notification from year edit updown */
2840 if (hdr->code == UDN_DELTAPOS)
2842 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2844 if (hdr->hwndFrom == infoPtr->hWndYearUpDown && nmud->iDelta)
2846 /* year value limits are set up explicitly after updown creation */
2847 MONTHCAL_Scroll(infoPtr, 12 * nmud->iDelta, FALSE);
2848 MONTHCAL_NotifyDayState(infoPtr);
2849 MONTHCAL_NotifySelectionChange(infoPtr);
2852 return 0;
2855 static inline BOOL
2856 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2858 BOOL prev = infoPtr->isUnicode;
2859 infoPtr->isUnicode = isUnicode;
2860 return prev;
2863 static inline BOOL
2864 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2866 return infoPtr->isUnicode;
2869 static LRESULT WINAPI
2870 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2872 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0);
2874 TRACE("hwnd %p, msg %x, wparam %Ix, lparam %Ix\n", hwnd, uMsg, wParam, lParam);
2876 if (!infoPtr && (uMsg != WM_CREATE))
2877 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2878 switch(uMsg)
2880 case MCM_GETCURSEL:
2881 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2883 case MCM_SETCURSEL:
2884 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2886 case MCM_GETMAXSELCOUNT:
2887 return MONTHCAL_GetMaxSelCount(infoPtr);
2889 case MCM_SETMAXSELCOUNT:
2890 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2892 case MCM_GETSELRANGE:
2893 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2895 case MCM_SETSELRANGE:
2896 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2898 case MCM_GETMONTHRANGE:
2899 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2901 case MCM_SETDAYSTATE:
2902 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2904 case MCM_GETMINREQRECT:
2905 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2907 case MCM_GETCOLOR:
2908 return MONTHCAL_GetColor(infoPtr, wParam);
2910 case MCM_SETCOLOR:
2911 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2913 case MCM_GETTODAY:
2914 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2916 case MCM_SETTODAY:
2917 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2919 case MCM_HITTEST:
2920 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2922 case MCM_GETFIRSTDAYOFWEEK:
2923 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2925 case MCM_SETFIRSTDAYOFWEEK:
2926 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2928 case MCM_GETRANGE:
2929 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2931 case MCM_SETRANGE:
2932 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2934 case MCM_GETMONTHDELTA:
2935 return MONTHCAL_GetMonthDelta(infoPtr);
2937 case MCM_SETMONTHDELTA:
2938 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2940 case MCM_GETMAXTODAYWIDTH:
2941 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2943 case MCM_SETUNICODEFORMAT:
2944 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2946 case MCM_GETUNICODEFORMAT:
2947 return MONTHCAL_GetUnicodeFormat(infoPtr);
2949 case MCM_GETCALENDARCOUNT:
2950 return MONTHCAL_GetCalCount(infoPtr);
2952 case WM_GETDLGCODE:
2953 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2955 case WM_RBUTTONUP:
2956 return MONTHCAL_RButtonUp(infoPtr, lParam);
2958 case WM_LBUTTONDOWN:
2959 return MONTHCAL_LButtonDown(infoPtr, lParam);
2961 case WM_MOUSEMOVE:
2962 return MONTHCAL_MouseMove(infoPtr, lParam);
2964 case WM_LBUTTONUP:
2965 return MONTHCAL_LButtonUp(infoPtr, lParam);
2967 case WM_PAINT:
2968 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2970 case WM_PRINTCLIENT:
2971 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2973 case WM_ERASEBKGND:
2974 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2976 case WM_SETFOCUS:
2977 return MONTHCAL_SetFocus(infoPtr);
2979 case WM_SIZE:
2980 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2982 case WM_NOTIFY:
2983 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2985 case WM_CREATE:
2986 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2988 case WM_SETFONT:
2989 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2991 case WM_GETFONT:
2992 return MONTHCAL_GetFont(infoPtr);
2994 case WM_TIMER:
2995 return MONTHCAL_Timer(infoPtr, wParam);
2997 case WM_THEMECHANGED:
2998 return theme_changed (infoPtr);
3000 case WM_DESTROY:
3001 return MONTHCAL_Destroy(infoPtr);
3003 case WM_SYSCOLORCHANGE:
3004 COMCTL32_RefreshSysColors();
3005 return 0;
3007 case WM_STYLECHANGED:
3008 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
3010 case WM_STYLECHANGING:
3011 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
3013 default:
3014 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
3015 ERR( "unknown msg %04x, wp %Ix, lp %Ix\n", uMsg, wParam, lParam);
3016 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
3021 void
3022 MONTHCAL_Register(void)
3024 WNDCLASSW wndClass;
3026 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
3027 wndClass.style = CS_GLOBALCLASS;
3028 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
3029 wndClass.cbClsExtra = 0;
3030 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
3031 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3032 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
3033 wndClass.lpszClassName = MONTHCAL_CLASSW;
3035 RegisterClassW(&wndClass);
3039 void
3040 MONTHCAL_Unregister(void)
3042 UnregisterClassW(MONTHCAL_CLASSW, NULL);