d3d8: Get rid of the format switching code in d3d8_device_CopyRects().
[wine.git] / dlls / comctl32 / monthcal.c
blob84a01dacd84e2c421a5ed1d393be78ab3b316be0
1 /*
2 * Month calendar control
4 * Copyright 1998, 1999 Eric Kohl (ekohl@abo.rhein-zeitung.de)
5 * Copyright 1999 Alex Priem (alexp@sci.kun.nl)
6 * Copyright 1999 Chris Morgan <cmorgan@wpi.edu> and
7 * James Abbatiello <abbeyj@wpi.edu>
8 * Copyright 2000 Uwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de>
9 * Copyright 2009-2011 Nikolay Sivov
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * NOTE
27 * This code was audited for completeness against the documented features
28 * of Comctl32.dll version 6.0 on Oct. 20, 2004, by Dimitrie O. Paun.
30 * Unless otherwise noted, we believe this code to be complete, as per
31 * the specification mentioned above.
32 * If you discover missing features, or bugs, please note them below.
34 * TODO:
35 * -- MCM_[GS]ETUNICODEFORMAT
36 * -- handle resources better (doesn't work now);
37 * -- take care of internationalization.
38 * -- keyboard handling.
39 * -- search for FIXME
42 #include <math.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "commctrl.h"
54 #include "comctl32.h"
55 #include "uxtheme.h"
56 #include "vssym32.h"
57 #include "wine/unicode.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
62 #define MC_SEL_LBUTUP 1 /* Left button released */
63 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
64 #define MC_PREVPRESSED 4 /* Prev month button pressed */
65 #define MC_NEXTPRESSED 8 /* Next month button pressed */
66 #define MC_PREVNEXTMONTHDELAY 350 /* when continuously pressing `next/prev
67 month', wait 350 ms before going
68 to the next/prev month */
69 #define MC_TODAYUPDATEDELAY 120000 /* time between today check for update (2 min) */
71 #define MC_PREVNEXTMONTHTIMER 1 /* Timer IDs */
72 #define MC_TODAYUPDATETIMER 2
74 #define MC_CALENDAR_PADDING 6
76 #define countof(arr) (sizeof(arr)/sizeof(arr[0]))
78 /* convert from days to 100 nanoseconds unit - used as FILETIME unit */
79 #define DAYSTO100NSECS(days) (((ULONGLONG)(days))*24*60*60*10000000)
81 enum CachedPen
83 PenRed = 0,
84 PenText,
85 PenLast
88 enum CachedBrush
90 BrushTitle = 0,
91 BrushMonth,
92 BrushBackground,
93 BrushLast
96 /* single calendar data */
97 typedef struct _CALENDAR_INFO
99 RECT title; /* rect for the header above the calendar */
100 RECT titlemonth; /* the 'month name' text in the header */
101 RECT titleyear; /* the 'year number' text in the header */
102 RECT wdays; /* week days at top */
103 RECT days; /* calendar area */
104 RECT weeknums; /* week numbers at left side */
106 SYSTEMTIME month;/* contains calendar main month/year */
107 } CALENDAR_INFO;
109 typedef struct
111 HWND hwndSelf;
112 DWORD dwStyle; /* cached GWL_STYLE */
114 COLORREF colors[MCSC_TRAILINGTEXT+1];
115 HBRUSH brushes[BrushLast];
116 HPEN pens[PenLast];
118 HFONT hFont;
119 HFONT hBoldFont;
120 int textHeight;
121 int textWidth;
122 int height_increment;
123 int width_increment;
124 INT delta; /* scroll rate; # of months that the */
125 /* control moves when user clicks a scroll button */
126 int firstDay; /* Start month calendar with firstDay's day,
127 stored in SYSTEMTIME format */
128 BOOL firstDaySet; /* first week day differs from locale defined */
130 BOOL isUnicode; /* value set with MCM_SETUNICODE format */
132 MONTHDAYSTATE *monthdayState;
133 SYSTEMTIME todaysDate;
134 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
135 int status; /* See MC_SEL flags */
136 SYSTEMTIME firstSel; /* first selected day */
137 INT maxSelCount;
138 SYSTEMTIME minSel; /* contains single selection when used without MCS_MULTISELECT */
139 SYSTEMTIME maxSel;
140 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
141 DWORD rangeValid;
142 SYSTEMTIME minDate;
143 SYSTEMTIME maxDate;
145 RECT titlebtnnext; /* the `next month' button in the header */
146 RECT titlebtnprev; /* the `prev month' button in the header */
147 RECT todayrect; /* `today: xx/xx/xx' text rect */
148 HWND hwndNotify; /* Window to receive the notifications */
149 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
150 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
151 WNDPROC EditWndProc; /* original Edit window procedure */
153 CALENDAR_INFO *calendars;
154 SIZE dim; /* [cx,cy] - dimensions of calendars matrix, row/column count */
155 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
157 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
159 /* empty SYSTEMTIME const */
160 static const SYSTEMTIME st_null;
161 /* valid date limits */
162 static const SYSTEMTIME max_allowed_date = { /* wYear */ 9999, /* wMonth */ 12, /* wDayOfWeek */ 0, /* wDay */ 31 };
163 static const SYSTEMTIME min_allowed_date = { /* wYear */ 1752, /* wMonth */ 9, /* wDayOfWeek */ 0, /* wDay */ 14 };
165 /* Prev/Next buttons */
166 enum nav_direction
168 DIRECTION_BACKWARD,
169 DIRECTION_FORWARD
172 /* helper functions */
173 static inline INT MONTHCAL_GetCalCount(const MONTHCAL_INFO *infoPtr)
175 return infoPtr->dim.cx * infoPtr->dim.cy;
178 /* send a single MCN_SELCHANGE notification */
179 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
181 NMSELCHANGE nmsc;
183 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
184 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
185 nmsc.nmhdr.code = MCN_SELCHANGE;
186 nmsc.stSelStart = infoPtr->minSel;
187 nmsc.stSelStart.wDayOfWeek = 0;
188 if(infoPtr->dwStyle & MCS_MULTISELECT){
189 nmsc.stSelEnd = infoPtr->maxSel;
190 nmsc.stSelEnd.wDayOfWeek = 0;
192 else
193 nmsc.stSelEnd = st_null;
195 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
198 /* send a single MCN_SELECT notification */
199 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
201 NMSELCHANGE nmsc;
203 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
204 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
205 nmsc.nmhdr.code = MCN_SELECT;
206 nmsc.stSelStart = infoPtr->minSel;
207 nmsc.stSelStart.wDayOfWeek = 0;
208 if(infoPtr->dwStyle & MCS_MULTISELECT){
209 nmsc.stSelEnd = infoPtr->maxSel;
210 nmsc.stSelEnd.wDayOfWeek = 0;
212 else
213 nmsc.stSelEnd = st_null;
215 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
218 static inline int MONTHCAL_MonthDiff(const SYSTEMTIME *left, const SYSTEMTIME *right)
220 return (right->wYear - left->wYear)*12 + right->wMonth - left->wMonth;
223 /* returns the number of days in any given month, checking for leap days */
224 /* January is 1, December is 12 */
225 int MONTHCAL_MonthLength(int month, int year)
227 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
228 /* Wrap around, this eases handling. Getting length only we shouldn't care
229 about year change here cause January and December have
230 the same day quantity */
231 if(month == 0)
232 month = 12;
233 else if(month == 13)
234 month = 1;
236 /* special case for calendar transition year */
237 if(month == min_allowed_date.wMonth && year == min_allowed_date.wYear) return 19;
239 /* if we have a leap year add 1 day to February */
240 /* a leap year is a year either divisible by 400 */
241 /* or divisible by 4 and not by 100 */
242 if(month == 2) { /* February */
243 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
244 (year%4 == 0)) ? 1 : 0);
246 else {
247 return mdays[month - 1];
251 /* compares timestamps using date part only */
252 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
254 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
255 (first->wDay == second->wDay);
258 /* make sure that date fields are valid */
259 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
261 if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
262 if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear)) return FALSE;
264 return TRUE;
267 /* Copies timestamp part only.
269 * PARAMETERS
271 * [I] from : source date
272 * [O] to : dest date
274 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
276 to->wHour = from->wHour;
277 to->wMinute = from->wMinute;
278 to->wSecond = from->wSecond;
281 /* Copies date part only.
283 * PARAMETERS
285 * [I] from : source date
286 * [O] to : dest date
288 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
290 to->wYear = from->wYear;
291 to->wMonth = from->wMonth;
292 to->wDay = from->wDay;
293 to->wDayOfWeek = from->wDayOfWeek;
296 /* Compares two dates in SYSTEMTIME format
298 * PARAMETERS
300 * [I] first : pointer to valid first date data to compare
301 * [I] second : pointer to valid second date data to compare
303 * RETURN VALUE
305 * -1 : first < second
306 * 0 : first == second
307 * 1 : first > second
309 * Note that no date validation performed, already validated values expected.
311 LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
313 FILETIME ft_first, ft_second;
315 SystemTimeToFileTime(first, &ft_first);
316 SystemTimeToFileTime(second, &ft_second);
318 return CompareFileTime(&ft_first, &ft_second);
321 static LONG MONTHCAL_CompareMonths(const SYSTEMTIME *first, const SYSTEMTIME *second)
323 SYSTEMTIME st_first, st_second;
325 st_first = st_second = st_null;
326 MONTHCAL_CopyDate(first, &st_first);
327 MONTHCAL_CopyDate(second, &st_second);
328 st_first.wDay = st_second.wDay = 1;
330 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
333 static LONG MONTHCAL_CompareDate(const SYSTEMTIME *first, const SYSTEMTIME *second)
335 SYSTEMTIME st_first, st_second;
337 st_first = st_second = st_null;
338 MONTHCAL_CopyDate(first, &st_first);
339 MONTHCAL_CopyDate(second, &st_second);
341 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
344 /* Checks largest possible date range and configured one
346 * PARAMETERS
348 * [I] infoPtr : valid pointer to control data
349 * [I] date : pointer to valid date data to check
350 * [I] fix : make date fit valid range
352 * RETURN VALUE
354 * TRUE - date within largest and configured range
355 * FALSE - date is outside largest or configured range
357 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr,
358 SYSTEMTIME *date, BOOL fix)
360 const SYSTEMTIME *fix_st = NULL;
362 if(MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) {
363 fix_st = &max_allowed_date;
365 else if(MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1) {
366 fix_st = &min_allowed_date;
368 else {
369 if(infoPtr->rangeValid & GDTR_MAX) {
370 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
371 fix_st = &infoPtr->maxDate;
375 if(infoPtr->rangeValid & GDTR_MIN) {
376 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
377 fix_st = &infoPtr->minDate;
382 if (fix && fix_st) {
383 date->wYear = fix_st->wYear;
384 date->wMonth = fix_st->wMonth;
387 return !fix_st;
390 /* Checks passed range width with configured maximum selection count
392 * PARAMETERS
394 * [I] infoPtr : valid pointer to control data
395 * [I] range0 : pointer to valid date data (requested bound)
396 * [I] range1 : pointer to valid date data (primary bound)
397 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
399 * Adjust value computed basing on primary bound and current maximum selection
400 * count. For simple range check (without adjusted value required) (range0, range1)
401 * relation means nothing.
403 * RETURN VALUE
405 * TRUE - range is shorter or equal to maximum
406 * FALSE - range is larger than maximum
408 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
409 const SYSTEMTIME *range0,
410 const SYSTEMTIME *range1,
411 SYSTEMTIME *adjust)
413 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
414 FILETIME ft_range0, ft_range1;
415 LONG cmp;
417 SystemTimeToFileTime(range0, &ft_range0);
418 SystemTimeToFileTime(range1, &ft_range1);
420 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
421 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
422 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
423 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
425 cmp = CompareFileTime(&ft_range0, &ft_range1);
427 if(cmp == 1)
428 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
429 else
430 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
432 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
434 if(adjust) {
435 if(cmp == 1)
436 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
437 else
438 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
440 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
441 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
442 FileTimeToSystemTime(&ft_range0, adjust);
445 return FALSE;
447 else return TRUE;
450 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
451 Milliseconds are intentionally not validated. */
452 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
454 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
455 return FALSE;
456 else
457 return TRUE;
460 /* Note:Depending on DST, this may be offset by a day.
461 Need to find out if we're on a DST place & adjust the clock accordingly.
462 Above function assumes we have a valid data.
463 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
464 0 = Sunday.
467 /* Returns the day in the week
469 * PARAMETERS
470 * [i] date : input date
471 * [I] inplace : set calculated value back to date structure
473 * RETURN VALUE
474 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
476 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
478 SYSTEMTIME st = st_null;
479 FILETIME ft;
481 MONTHCAL_CopyDate(date, &st);
483 SystemTimeToFileTime(&st, &ft);
484 FileTimeToSystemTime(&ft, &st);
486 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
488 return st.wDayOfWeek;
491 /* add/subtract 'months' from date */
492 static inline void MONTHCAL_GetMonth(SYSTEMTIME *date, INT months)
494 INT length, m = date->wMonth + months;
496 date->wYear += m > 0 ? (m - 1) / 12 : m / 12 - 1;
497 date->wMonth = m > 0 ? (m - 1) % 12 + 1 : 12 + m % 12;
498 /* fix moving from last day in a month */
499 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
500 if(date->wDay > length) date->wDay = length;
501 MONTHCAL_CalculateDayOfWeek(date, TRUE);
504 /* properly updates date to point on next month */
505 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
507 MONTHCAL_GetMonth(date, 1);
510 /* properly updates date to point on prev month */
511 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
513 MONTHCAL_GetMonth(date, -1);
516 /* Returns full date for a first currently visible day */
517 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
519 /* zero indexed calendar has the earliest date */
520 SYSTEMTIME st_first = infoPtr->calendars[0].month;
521 INT firstDay;
523 st_first.wDay = 1;
524 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
526 *date = infoPtr->calendars[0].month;
527 MONTHCAL_GetPrevMonth(date);
529 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
530 (infoPtr->firstDay - firstDay) % 7 + 1;
532 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
533 date->wDay -= 7;
535 /* fix day of week */
536 MONTHCAL_CalculateDayOfWeek(date, TRUE);
539 /* Returns full date for a last currently visible day */
540 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
542 /* the latest date is in latest calendar */
543 SYSTEMTIME st, *lt_month = &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
544 INT first_day;
546 *date = *lt_month;
547 st = *lt_month;
549 /* day of week of first day of current month */
550 st.wDay = 1;
551 first_day = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
553 MONTHCAL_GetNextMonth(date);
554 MONTHCAL_GetPrevMonth(&st);
556 /* last calendar starts with some date from previous month that not displayed */
557 st.wDay = MONTHCAL_MonthLength(st.wMonth, st.wYear) +
558 (infoPtr->firstDay - first_day) % 7 + 1;
559 if (st.wDay > MONTHCAL_MonthLength(st.wMonth, st.wYear)) st.wDay -= 7;
561 /* Use month length to get max day. 42 means max day count in calendar area */
562 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
563 MONTHCAL_MonthLength(lt_month->wMonth, lt_month->wYear);
565 /* fix day of week */
566 MONTHCAL_CalculateDayOfWeek(date, TRUE);
569 /* From a given point calculate the row, column and day in the calendar,
570 'day == 0' means the last day of the last month. */
571 static int MONTHCAL_GetDayFromPos(const MONTHCAL_INFO *infoPtr, POINT pt, INT calIdx)
573 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
574 int firstDay, col, row;
575 RECT client;
577 GetClientRect(infoPtr->hwndSelf, &client);
579 /* if the point is outside the x bounds of the window put it at the boundary */
580 if (pt.x > client.right) pt.x = client.right;
582 col = (pt.x - infoPtr->calendars[calIdx].days.left ) / infoPtr->width_increment;
583 row = (pt.y - infoPtr->calendars[calIdx].days.top ) / infoPtr->height_increment;
585 st.wDay = 1;
586 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
587 return col + 7 * row - firstDay;
590 /* Get day position for given date and calendar
592 * PARAMETERS
594 * [I] infoPtr : pointer to control data
595 * [I] date : date value
596 * [O] col : day column (zero based)
597 * [O] row : week column (zero based)
598 * [I] calIdx : calendar index
600 static void MONTHCAL_GetDayPos(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
601 INT *col, INT *row, INT calIdx)
603 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
604 INT first;
606 st.wDay = 1;
607 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
609 if (calIdx == 0 || calIdx == MONTHCAL_GetCalCount(infoPtr)-1) {
610 const SYSTEMTIME *cal = &infoPtr->calendars[calIdx].month;
611 LONG cmp = MONTHCAL_CompareMonths(date, &st);
613 /* previous month */
614 if (cmp == -1) {
615 *col = (first - MONTHCAL_MonthLength(date->wMonth, cal->wYear) + date->wDay) % 7;
616 *row = 0;
617 return;
620 /* next month calculation is same as for current, just add current month length */
621 if (cmp == 1)
622 first += MONTHCAL_MonthLength(cal->wMonth, cal->wYear);
625 *col = (date->wDay + first) % 7;
626 *row = (date->wDay + first - *col) / 7;
629 /* returns bounding box for day in given position in given calendar */
630 static inline void MONTHCAL_GetDayRectI(const MONTHCAL_INFO *infoPtr, RECT *r,
631 INT col, INT row, INT calIdx)
633 r->left = infoPtr->calendars[calIdx].days.left + col * infoPtr->width_increment;
634 r->right = r->left + infoPtr->width_increment;
635 r->top = infoPtr->calendars[calIdx].days.top + row * infoPtr->height_increment;
636 r->bottom = r->top + infoPtr->textHeight;
639 /* Returns bounding box for given date
641 * NOTE: when calendar index is unknown pass -1
643 static inline void MONTHCAL_GetDayRect(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
644 RECT *r, INT calIdx)
646 INT col, row;
648 if (calIdx == -1)
650 INT cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[0].month);
652 if (cmp <= 0)
653 calIdx = 0;
654 else
656 cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month);
657 if (cmp >= 0)
658 calIdx = MONTHCAL_GetCalCount(infoPtr)-1;
659 else
661 for (calIdx = 1; calIdx < MONTHCAL_GetCalCount(infoPtr)-1; calIdx++)
662 if (MONTHCAL_CompareMonths(date, &infoPtr->calendars[calIdx].month) == 0)
663 break;
668 MONTHCAL_GetDayPos(infoPtr, date, &col, &row, calIdx);
669 MONTHCAL_GetDayRectI(infoPtr, r, col, row, calIdx);
672 static LRESULT
673 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
675 INT range;
677 TRACE("flag=%d, st=%p\n", flag, st);
679 switch (flag) {
680 case GMR_VISIBLE:
682 if (st)
684 st[0] = infoPtr->calendars[0].month;
685 st[1] = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
687 if (st[0].wMonth == min_allowed_date.wMonth &&
688 st[0].wYear == min_allowed_date.wYear)
690 st[0].wDay = min_allowed_date.wDay;
692 else
693 st[0].wDay = 1;
694 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
696 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
697 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
700 range = MONTHCAL_GetCalCount(infoPtr);
701 break;
703 case GMR_DAYSTATE:
705 if (st)
707 MONTHCAL_GetMinDate(infoPtr, &st[0]);
708 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
710 /* include two partially visible months */
711 range = MONTHCAL_GetCalCount(infoPtr) + 2;
712 break;
714 default:
715 WARN("Unknown flag value, got %d\n", flag);
716 range = 0;
719 return range;
722 /* Focused day helper:
724 - set focused date to given value;
725 - reset to zero value if NULL passed;
726 - invalidate previous and new day rectangle only if needed.
728 Returns TRUE if focused day changed, FALSE otherwise.
730 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
732 RECT r;
734 if(st)
736 /* there's nothing to do if it's the same date,
737 mouse move within same date rectangle case */
738 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
740 /* invalidate old focused day */
741 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
742 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
744 infoPtr->focusedSel = *st;
747 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
749 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
750 infoPtr->focusedSel = st_null;
752 /* on set invalidates new day, on reset clears previous focused day */
753 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
755 return TRUE;
758 /* draw today boundary box for specified rectangle */
759 static void MONTHCAL_Circle(const MONTHCAL_INFO *infoPtr, HDC hdc, const RECT *r)
761 HPEN old_pen = SelectObject(hdc, infoPtr->pens[PenRed]);
762 HBRUSH old_brush;
764 old_brush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
765 Rectangle(hdc, r->left, r->top, r->right, r->bottom);
767 SelectObject(hdc, old_brush);
768 SelectObject(hdc, old_pen);
771 /* Draw today day mark rectangle
773 * [I] hdc : context to draw in
774 * [I] date : day to mark with rectangle
777 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
778 const SYSTEMTIME *date)
780 RECT r;
782 MONTHCAL_GetDayRect(infoPtr, date, &r, -1);
783 MONTHCAL_Circle(infoPtr, hdc, &r);
786 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
787 int bold, const PAINTSTRUCT *ps)
789 static const WCHAR fmtW[] = { '%','d',0 };
790 WCHAR buf[10];
791 RECT r, r_temp;
792 COLORREF oldCol = 0;
793 COLORREF oldBk = 0;
794 INT old_bkmode, selection;
796 /* no need to check styles: when selection is not valid, it is set to zero.
797 1 < day < 31, so everything is OK */
798 MONTHCAL_GetDayRect(infoPtr, st, &r, -1);
799 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
801 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
802 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0))
804 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
805 TRACE("%s\n", wine_dbgstr_rect(&r));
806 oldCol = SetTextColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
807 oldBk = SetBkColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
808 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
810 selection = 1;
812 else
813 selection = 0;
815 SelectObject(hdc, bold ? infoPtr->hBoldFont : infoPtr->hFont);
817 old_bkmode = SetBkMode(hdc, TRANSPARENT);
818 wsprintfW(buf, fmtW, st->wDay);
819 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
820 SetBkMode(hdc, old_bkmode);
822 if (selection)
824 SetTextColor(hdc, oldCol);
825 SetBkColor(hdc, oldBk);
829 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, enum nav_direction button)
831 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
832 RECT *r = button == DIRECTION_FORWARD ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
833 BOOL pressed = button == DIRECTION_FORWARD ? infoPtr->status & MC_NEXTPRESSED :
834 infoPtr->status & MC_PREVPRESSED;
835 if (theme)
837 static const int states[] = {
838 /* Prev button */
839 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
840 /* Next button */
841 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
843 int stateNum = button == DIRECTION_FORWARD ? 3 : 0;
844 if (pressed)
845 stateNum += 1;
846 else
848 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
850 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
852 else
854 int style = button == DIRECTION_FORWARD ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
855 if (pressed)
856 style |= DFCS_PUSHED;
857 else
859 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
862 DrawFrameControl(hdc, r, DFC_SCROLL, style);
866 /* paint a title with buttons and month/year string */
867 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
869 static const WCHAR fmt_monthW[] = { '%','s',' ','%','l','d',0 };
870 RECT *title = &infoPtr->calendars[calIdx].title;
871 const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
872 WCHAR buf_month[80], buf_fmt[80];
873 SIZE sz;
875 /* fill header box */
876 FillRect(hdc, title, infoPtr->brushes[BrushTitle]);
878 /* month/year string */
879 SetBkColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
880 SetTextColor(hdc, infoPtr->colors[MCSC_TITLETEXT]);
881 SelectObject(hdc, infoPtr->hBoldFont);
883 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1 + st->wMonth - 1,
884 buf_month, countof(buf_month));
886 wsprintfW(buf_fmt, fmt_monthW, buf_month, st->wYear);
887 DrawTextW(hdc, buf_fmt, strlenW(buf_fmt), title,
888 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
890 /* update title rectangles with current month - used while testing hits */
891 GetTextExtentPoint32W(hdc, buf_fmt, strlenW(buf_fmt), &sz);
892 infoPtr->calendars[calIdx].titlemonth.left = title->right / 2 + title->left / 2 - sz.cx / 2;
893 infoPtr->calendars[calIdx].titleyear.right = title->right / 2 + title->left / 2 + sz.cx / 2;
895 GetTextExtentPoint32W(hdc, buf_month, strlenW(buf_month), &sz);
896 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
897 infoPtr->calendars[calIdx].titleyear.left = infoPtr->calendars[calIdx].titlemonth.right;
900 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
902 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
903 static const WCHAR fmt_weekW[] = { '%','d',0 };
904 INT mindays, weeknum, weeknum1, startofprescal;
905 INT i, prev_month;
906 SYSTEMTIME st;
907 WCHAR buf[80];
908 HPEN old_pen;
909 RECT r;
911 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
913 MONTHCAL_GetMinDate(infoPtr, &st);
914 startofprescal = st.wDay;
915 st = *date;
917 prev_month = date->wMonth - 1;
918 if(prev_month == 0) prev_month = 12;
921 Rules what week to call the first week of a new year:
922 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
923 The week containing Jan 1 is the first week of year
924 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
925 First week of year must contain 4 days of the new year
926 LOCALE_IFIRSTWEEKOFYEAR == 1 (what countries?)
927 The first week of the year must contain only days of the new year
929 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
930 weeknum = atoiW(buf);
931 switch (weeknum)
933 case 1: mindays = 6;
934 break;
935 case 2: mindays = 3;
936 break;
937 case 0: mindays = 0;
938 break;
939 default:
940 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
941 mindays = 0;
944 if (date->wMonth == 1)
946 /* calculate all those exceptions for January */
947 st.wDay = st.wMonth = 1;
948 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
949 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
950 weeknum = 1;
951 else
953 weeknum = 0;
954 for(i = 0; i < 11; i++)
955 weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
957 weeknum += startofprescal + 7;
958 weeknum /= 7;
959 st.wYear -= 1;
960 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
961 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
964 else
966 weeknum = 0;
967 for(i = 0; i < prev_month - 1; i++)
968 weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
970 weeknum += startofprescal + 7;
971 weeknum /= 7;
972 st.wDay = st.wMonth = 1;
973 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
974 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
977 r = infoPtr->calendars[calIdx].weeknums;
979 /* erase whole week numbers area */
980 FillRect(hdc, &r, infoPtr->brushes[BrushMonth]);
981 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
983 /* reduce rectangle to one week number */
984 r.bottom = r.top + infoPtr->height_increment;
986 for(i = 0; i < 6; i++) {
987 if((i == 0) && (weeknum > 50))
989 wsprintfW(buf, fmt_weekW, weeknum);
990 weeknum = 0;
992 else if((i == 5) && (weeknum > 47))
994 wsprintfW(buf, fmt_weekW, 1);
996 else
997 wsprintfW(buf, fmt_weekW, weeknum + i);
999 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1000 OffsetRect(&r, 0, infoPtr->height_increment);
1003 /* line separator for week numbers column */
1004 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1005 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
1006 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
1007 SelectObject(hdc, old_pen);
1010 /* bottom today date */
1011 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1013 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
1014 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
1015 RECT text_rect, box_rect;
1016 HFONT old_font;
1017 INT col;
1019 if(infoPtr->dwStyle & MCS_NOTODAY) return;
1021 if (!LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, countof(buf_todayW)))
1023 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
1024 WARN("Can't load resource\n");
1025 strcpyW(buf_todayW, todayW);
1028 col = infoPtr->dwStyle & MCS_NOTODAYCIRCLE ? 0 : 1;
1029 if (infoPtr->dwStyle & MCS_WEEKNUMBERS) col--;
1030 /* label is located below first calendar last row */
1031 MONTHCAL_GetDayRectI(infoPtr, &text_rect, col, 6, infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx);
1032 box_rect = text_rect;
1034 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
1035 buf_dateW, countof(buf_dateW));
1036 old_font = SelectObject(hdc, infoPtr->hBoldFont);
1037 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1039 wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
1040 DrawTextW(hdc, buf, -1, &text_rect, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1041 DrawTextW(hdc, buf, -1, &text_rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1043 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
1044 OffsetRect(&box_rect, -infoPtr->width_increment, 0);
1045 MONTHCAL_Circle(infoPtr, hdc, &box_rect);
1048 SelectObject(hdc, old_font);
1051 /* today mark + focus */
1052 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1054 /* circle today date if only it's in fully visible month */
1055 if (!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
1057 INT i;
1059 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1060 if (!MONTHCAL_CompareMonths(&infoPtr->todaysDate, &infoPtr->calendars[i].month))
1062 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
1063 break;
1067 if (!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
1069 RECT r;
1070 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
1071 DrawFocusRect(hdc, &r);
1075 /* months before first calendar month and after last calendar month */
1076 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1078 INT mask, index;
1079 UINT length;
1080 SYSTEMTIME st_max, st;
1082 if (infoPtr->dwStyle & MCS_NOTRAILINGDATES) return;
1084 SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
1086 /* draw prev month */
1087 MONTHCAL_GetMinDate(infoPtr, &st);
1088 mask = 1 << (st.wDay-1);
1089 /* December and January both 31 days long, so no worries if wrapped */
1090 length = MONTHCAL_MonthLength(infoPtr->calendars[0].month.wMonth - 1,
1091 infoPtr->calendars[0].month.wYear);
1092 index = 0;
1093 while(st.wDay <= length)
1095 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1096 mask <<= 1;
1097 st.wDay++;
1100 /* draw next month */
1101 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1102 st.wDay = 1;
1103 MONTHCAL_GetNextMonth(&st);
1104 MONTHCAL_GetMaxDate(infoPtr, &st_max);
1105 mask = 1;
1106 index = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)-1;
1107 while(st.wDay <= st_max.wDay)
1109 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1110 mask <<= 1;
1111 st.wDay++;
1115 /* paint a calendar area */
1116 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
1118 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
1119 INT i, j;
1120 UINT length;
1121 RECT r, fill_bk_rect;
1122 SYSTEMTIME st;
1123 WCHAR buf[80];
1124 HPEN old_pen;
1125 int mask;
1127 /* fill whole days area - from week days area to today note rectangle */
1128 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
1129 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
1130 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
1132 FillRect(hdc, &fill_bk_rect, infoPtr->brushes[BrushMonth]);
1134 /* draw line under day abbreviations */
1135 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1136 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
1137 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
1138 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
1139 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
1140 SelectObject(hdc, old_pen);
1142 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
1143 infoPtr->calendars[calIdx].weeknums.right;
1145 /* draw day abbreviations */
1146 SelectObject(hdc, infoPtr->hFont);
1147 SetBkColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
1148 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1149 /* rectangle to draw a single day abbreviation within */
1150 r = infoPtr->calendars[calIdx].wdays;
1151 r.right = r.left + infoPtr->width_increment;
1153 i = infoPtr->firstDay;
1154 for(j = 0; j < 7; j++) {
1155 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
1156 DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1157 OffsetRect(&r, infoPtr->width_increment, 0);
1160 /* draw current month */
1161 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1162 st = *date;
1163 st.wDay = 1;
1164 mask = 1;
1165 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1166 while(st.wDay <= length)
1168 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[calIdx+1] & mask, ps);
1169 mask <<= 1;
1170 st.wDay++;
1174 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1176 COLORREF old_text_clr, old_bk_clr;
1177 HFONT old_font;
1178 INT i;
1180 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1181 old_bk_clr = GetBkColor(hdc);
1182 old_font = GetCurrentObject(hdc, OBJ_FONT);
1184 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1186 RECT *title = &infoPtr->calendars[i].title;
1187 RECT r;
1189 /* draw title, redraw all its elements */
1190 if (IntersectRect(&r, &(ps->rcPaint), title))
1191 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1193 /* draw calendar area */
1194 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1195 if (IntersectRect(&r, &(ps->rcPaint), &r))
1196 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1198 /* week numbers */
1199 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1202 /* partially visible months */
1203 MONTHCAL_PaintLeadTrailMonths(infoPtr, hdc, ps);
1205 /* focus and today rectangle */
1206 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1208 /* today at the bottom left */
1209 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1211 /* navigation buttons */
1212 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_BACKWARD);
1213 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_FORWARD);
1215 /* restore context */
1216 SetBkColor(hdc, old_bk_clr);
1217 SelectObject(hdc, old_font);
1218 SetTextColor(hdc, old_text_clr);
1221 static LRESULT
1222 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, RECT *rect)
1224 TRACE("rect %p\n", rect);
1226 if(!rect) return FALSE;
1228 *rect = infoPtr->calendars[0].title;
1229 rect->bottom = infoPtr->calendars[0].days.bottom + infoPtr->todayrect.bottom -
1230 infoPtr->todayrect.top;
1232 AdjustWindowRect(rect, infoPtr->dwStyle, FALSE);
1234 /* minimal rectangle is zero based */
1235 OffsetRect(rect, -rect->left, -rect->top);
1237 TRACE("%s\n", wine_dbgstr_rect(rect));
1239 return TRUE;
1242 static COLORREF
1243 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, UINT index)
1245 TRACE("%p, %d\n", infoPtr, index);
1247 if (index > MCSC_TRAILINGTEXT) return -1;
1248 return infoPtr->colors[index];
1251 static LRESULT
1252 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, UINT index, COLORREF color)
1254 enum CachedBrush type;
1255 COLORREF prev;
1257 TRACE("%p, %d: color %08x\n", infoPtr, index, color);
1259 if (index > MCSC_TRAILINGTEXT) return -1;
1261 prev = infoPtr->colors[index];
1262 infoPtr->colors[index] = color;
1264 /* update cached brush */
1265 switch (index)
1267 case MCSC_BACKGROUND:
1268 type = BrushBackground;
1269 break;
1270 case MCSC_TITLEBK:
1271 type = BrushTitle;
1272 break;
1273 case MCSC_MONTHBK:
1274 type = BrushMonth;
1275 break;
1276 default:
1277 type = BrushLast;
1280 if (type != BrushLast)
1282 DeleteObject(infoPtr->brushes[type]);
1283 infoPtr->brushes[type] = CreateSolidBrush(color);
1286 /* update cached pen */
1287 if (index == MCSC_TEXT)
1289 DeleteObject(infoPtr->pens[PenText]);
1290 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[index]);
1293 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND);
1294 return prev;
1297 static LRESULT
1298 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1300 TRACE("\n");
1302 if(infoPtr->delta)
1303 return infoPtr->delta;
1305 return MONTHCAL_GetMonthRange(infoPtr, GMR_VISIBLE, NULL);
1309 static LRESULT
1310 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1312 INT prev = infoPtr->delta;
1314 TRACE("delta %d\n", delta);
1316 infoPtr->delta = delta;
1317 return prev;
1321 static inline LRESULT
1322 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1324 int day;
1326 /* convert from SYSTEMTIME to locale format */
1327 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1329 return MAKELONG(day, infoPtr->firstDaySet);
1333 /* Sets the first day of the week that will appear in the control
1336 * PARAMETERS:
1337 * [I] infoPtr : valid pointer to control data
1338 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1341 * RETURN VALUE:
1342 * Low word contains previous first day,
1343 * high word indicates was first day forced with this message before or is
1344 * locale defined (TRUE - was forced, FALSE - wasn't).
1346 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1347 * FIXME: we need more error checking here
1349 static LRESULT
1350 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1352 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1353 int new_day;
1355 TRACE("%d\n", day);
1357 if(day == -1)
1359 WCHAR buf[80];
1361 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1362 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1364 new_day = atoiW(buf);
1366 infoPtr->firstDaySet = FALSE;
1368 else if(day >= 7)
1370 new_day = 6; /* max first day allowed */
1371 infoPtr->firstDaySet = TRUE;
1373 else
1375 /* Native behaviour for that case is broken: invalid date number >31
1376 got displayed at (0,0) position, current month starts always from
1377 (1,0) position. Should be implemented here as well only if there's
1378 nothing else to do. */
1379 if (day < -1)
1380 FIXME("No bug compatibility for day=%d\n", day);
1382 new_day = day;
1383 infoPtr->firstDaySet = TRUE;
1386 /* convert from locale to SYSTEMTIME format */
1387 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1389 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1391 return prev;
1394 static LRESULT
1395 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1397 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1400 static LRESULT
1401 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1403 FILETIME ft_min, ft_max;
1405 TRACE("%x %p\n", limits, range);
1407 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1408 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1409 return FALSE;
1411 if (limits & GDTR_MIN)
1413 if (!MONTHCAL_ValidateTime(&range[0]))
1414 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1416 infoPtr->minDate = range[0];
1417 infoPtr->rangeValid |= GDTR_MIN;
1419 if (limits & GDTR_MAX)
1421 if (!MONTHCAL_ValidateTime(&range[1]))
1422 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1424 infoPtr->maxDate = range[1];
1425 infoPtr->rangeValid |= GDTR_MAX;
1428 /* Only one limit set - we are done */
1429 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1430 return TRUE;
1432 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1433 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1435 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1437 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1439 /* Native swaps limits only when both limits are being set. */
1440 SYSTEMTIME st_tmp = infoPtr->minDate;
1441 infoPtr->minDate = infoPtr->maxDate;
1442 infoPtr->maxDate = st_tmp;
1444 else
1446 /* reset the other limit */
1447 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1448 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1449 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1453 return TRUE;
1457 static LRESULT
1458 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1460 TRACE("%p\n", range);
1462 if(!range) return FALSE;
1464 range[1] = infoPtr->maxDate;
1465 range[0] = infoPtr->minDate;
1467 return infoPtr->rangeValid;
1471 static LRESULT
1472 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1474 TRACE("%p %d %p\n", infoPtr, months, states);
1476 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return 0;
1477 if (months != MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)) return 0;
1479 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1481 return 1;
1484 static LRESULT
1485 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1487 TRACE("%p\n", curSel);
1488 if(!curSel) return FALSE;
1489 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1491 *curSel = infoPtr->minSel;
1492 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1493 return TRUE;
1496 static LRESULT
1497 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1499 SYSTEMTIME prev = infoPtr->minSel, selection;
1500 INT diff;
1501 WORD day;
1503 TRACE("%p\n", curSel);
1504 if(!curSel) return FALSE;
1505 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1507 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1508 /* exit earlier if selection equals current */
1509 if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1511 selection = *curSel;
1512 selection.wHour = selection.wMinute = selection.wSecond = selection.wMilliseconds = 0;
1513 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1515 if(!MONTHCAL_IsDateInValidRange(infoPtr, &selection, FALSE)) return FALSE;
1517 /* scroll calendars only if we have to */
1518 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, curSel);
1519 if (diff <= 0)
1521 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, curSel);
1522 if (diff > 0) diff = 0;
1525 if (diff != 0)
1527 INT i;
1529 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1530 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1533 /* we need to store time part as it is */
1534 selection = *curSel;
1535 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1536 infoPtr->minSel = infoPtr->maxSel = selection;
1538 /* if selection is still in current month, reduce rectangle */
1539 day = prev.wDay;
1540 prev.wDay = curSel->wDay;
1541 if (MONTHCAL_IsDateEqual(&prev, curSel))
1543 RECT r_prev, r_new;
1545 prev.wDay = day;
1546 MONTHCAL_GetDayRect(infoPtr, &prev, &r_prev, -1);
1547 MONTHCAL_GetDayRect(infoPtr, curSel, &r_new, -1);
1549 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1550 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1552 else
1553 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1555 return TRUE;
1559 static LRESULT
1560 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1562 return infoPtr->maxSelCount;
1566 static LRESULT
1567 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1569 TRACE("%d\n", max);
1571 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1572 if(max <= 0) return FALSE;
1574 infoPtr->maxSelCount = max;
1576 return TRUE;
1580 static LRESULT
1581 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1583 TRACE("%p\n", range);
1585 if(!range) return FALSE;
1587 if(infoPtr->dwStyle & MCS_MULTISELECT)
1589 range[1] = infoPtr->maxSel;
1590 range[0] = infoPtr->minSel;
1591 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1592 return TRUE;
1595 return FALSE;
1599 static LRESULT
1600 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1602 SYSTEMTIME old_range[2];
1603 INT diff;
1605 TRACE("%p\n", range);
1607 if(!range || !(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1609 /* adjust timestamps */
1610 if(!MONTHCAL_ValidateTime(&range[0])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1611 if(!MONTHCAL_ValidateTime(&range[1])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1613 /* maximum range exceeded */
1614 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1616 old_range[0] = infoPtr->minSel;
1617 old_range[1] = infoPtr->maxSel;
1619 /* swap if min > max */
1620 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1622 infoPtr->minSel = range[0];
1623 infoPtr->maxSel = range[1];
1625 else
1627 infoPtr->minSel = range[1];
1628 infoPtr->maxSel = range[0];
1631 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, &infoPtr->maxSel);
1632 if (diff < 0)
1634 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, &infoPtr->maxSel);
1635 if (diff > 0) diff = 0;
1638 if (diff != 0)
1640 INT i;
1642 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1643 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1646 /* update day of week */
1647 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1648 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1650 /* redraw if bounds changed */
1651 /* FIXME: no actual need to redraw everything */
1652 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1653 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1655 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1658 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1659 return TRUE;
1663 static LRESULT
1664 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1666 TRACE("%p\n", today);
1668 if(!today) return FALSE;
1669 *today = infoPtr->todaysDate;
1670 return TRUE;
1673 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1675 * RETURN VALUE
1677 * TRUE - today date changed
1678 * FALSE - today date isn't changed
1680 static BOOL
1681 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1683 RECT new_r, old_r;
1685 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1687 MONTHCAL_GetDayRect(infoPtr, &infoPtr->todaysDate, &old_r, -1);
1688 MONTHCAL_GetDayRect(infoPtr, today, &new_r, -1);
1690 infoPtr->todaysDate = *today;
1692 /* only two days need redrawing */
1693 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1694 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1695 /* and today label */
1696 InvalidateRect(infoPtr->hwndSelf, &infoPtr->todayrect, FALSE);
1697 return TRUE;
1700 /* MCM_SETTODAT handler */
1701 static LRESULT
1702 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1704 TRACE("%p\n", today);
1706 if (today)
1708 /* remember if date was set successfully */
1709 if (MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1712 return 0;
1715 /* returns calendar index containing specified point, or -1 if it's background */
1716 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1718 RECT r;
1719 INT i;
1721 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1723 /* whole bounding rectangle allows some optimization to compute */
1724 r.left = infoPtr->calendars[i].title.left;
1725 r.top = infoPtr->calendars[i].title.top;
1726 r.bottom = infoPtr->calendars[i].days.bottom;
1727 r.right = infoPtr->calendars[i].days.right;
1729 if (PtInRect(&r, *pt)) return i;
1732 return -1;
1735 static inline UINT fill_hittest_info(const MCHITTESTINFO *src, MCHITTESTINFO *dest)
1737 dest->uHit = src->uHit;
1738 dest->st = src->st;
1740 if (dest->cbSize == sizeof(MCHITTESTINFO))
1741 memcpy(&dest->rc, &src->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1743 return src->uHit;
1746 static LRESULT
1747 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1749 MCHITTESTINFO htinfo;
1750 SYSTEMTIME *ht_month;
1751 INT day, calIdx;
1753 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1755 htinfo.st = st_null;
1757 /* we should preserve passed fields if hit area doesn't need them */
1758 if (lpht->cbSize == sizeof(MCHITTESTINFO))
1759 memcpy(&htinfo.rc, &lpht->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1761 /* Comment in for debugging...
1762 TRACE("%d %d wd[%d %d %d %d] d[%d %d %d %d] t[%d %d %d %d] wn[%d %d %d %d]\n", x, y,
1763 infoPtr->wdays.left, infoPtr->wdays.right,
1764 infoPtr->wdays.top, infoPtr->wdays.bottom,
1765 infoPtr->days.left, infoPtr->days.right,
1766 infoPtr->days.top, infoPtr->days.bottom,
1767 infoPtr->todayrect.left, infoPtr->todayrect.right,
1768 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1769 infoPtr->weeknums.left, infoPtr->weeknums.right,
1770 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1773 /* guess in what calendar we are */
1774 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1775 if (calIdx == -1)
1777 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1779 htinfo.uHit = MCHT_TODAYLINK;
1780 htinfo.rc = infoPtr->todayrect;
1782 else
1783 /* outside of calendar area? What's left must be background :-) */
1784 htinfo.uHit = MCHT_CALENDARBK;
1786 return fill_hittest_info(&htinfo, lpht);
1789 /* are we in the header? */
1790 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1791 /* FIXME: buttons hittesting could be optimized cause maximum
1792 two calendars have buttons */
1793 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1795 htinfo.uHit = MCHT_TITLEBTNPREV;
1796 htinfo.rc = infoPtr->titlebtnprev;
1798 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1800 htinfo.uHit = MCHT_TITLEBTNNEXT;
1801 htinfo.rc = infoPtr->titlebtnnext;
1803 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1805 htinfo.uHit = MCHT_TITLEMONTH;
1806 htinfo.rc = infoPtr->calendars[calIdx].titlemonth;
1807 htinfo.iOffset = calIdx;
1809 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1811 htinfo.uHit = MCHT_TITLEYEAR;
1812 htinfo.rc = infoPtr->calendars[calIdx].titleyear;
1813 htinfo.iOffset = calIdx;
1815 else
1817 htinfo.uHit = MCHT_TITLE;
1818 htinfo.rc = infoPtr->calendars[calIdx].title;
1819 htinfo.iOffset = calIdx;
1822 return fill_hittest_info(&htinfo, lpht);
1825 ht_month = &infoPtr->calendars[calIdx].month;
1826 /* days area (including week days and week numbers) */
1827 day = MONTHCAL_GetDayFromPos(infoPtr, lpht->pt, calIdx);
1828 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1830 htinfo.uHit = MCHT_CALENDARDAY;
1831 htinfo.iOffset = calIdx;
1832 htinfo.st.wYear = ht_month->wYear;
1833 htinfo.st.wMonth = (day < 1) ? ht_month->wMonth -1 : ht_month->wMonth;
1834 htinfo.st.wDay = (day < 1) ?
1835 MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day : day;
1837 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1839 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1841 htinfo.uHit = MCHT_CALENDARWEEKNUM;
1842 htinfo.st.wYear = ht_month->wYear;
1843 htinfo.iOffset = calIdx;
1845 if (day < 1)
1847 htinfo.st.wMonth = ht_month->wMonth - 1;
1848 htinfo.st.wDay = MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day;
1850 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1852 htinfo.st.wMonth = ht_month->wMonth + 1;
1853 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1855 else
1857 htinfo.st.wMonth = ht_month->wMonth;
1858 htinfo.st.wDay = day;
1861 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1863 htinfo.iOffset = calIdx;
1864 htinfo.st.wYear = ht_month->wYear;
1865 htinfo.st.wMonth = ht_month->wMonth;
1866 /* previous month only valid for first calendar */
1867 if (day < 1 && calIdx == 0)
1869 htinfo.uHit = MCHT_CALENDARDATEPREV;
1870 MONTHCAL_GetPrevMonth(&htinfo.st);
1871 htinfo.st.wDay = MONTHCAL_MonthLength(htinfo.st.wMonth, htinfo.st.wYear) + day;
1873 /* next month only valid for last calendar */
1874 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear) &&
1875 calIdx == MONTHCAL_GetCalCount(infoPtr)-1)
1877 htinfo.uHit = MCHT_CALENDARDATENEXT;
1878 MONTHCAL_GetNextMonth(&htinfo.st);
1879 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1881 /* multiple calendars case - blank areas for previous/next month */
1882 else if (day < 1 || day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1884 htinfo.uHit = MCHT_CALENDARBK;
1886 else
1888 htinfo.uHit = MCHT_CALENDARDATE;
1889 htinfo.st.wDay = day;
1892 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1893 MONTHCAL_GetDayRectI(infoPtr, &htinfo.rc, htinfo.iCol, htinfo.iRow, calIdx);
1894 /* always update day of week */
1895 MONTHCAL_CalculateDayOfWeek(&htinfo.st, TRUE);
1898 return fill_hittest_info(&htinfo, lpht);
1901 /* MCN_GETDAYSTATE notification helper */
1902 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1904 MONTHDAYSTATE *state;
1905 NMDAYSTATE nmds;
1907 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return;
1909 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1910 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1911 nmds.nmhdr.code = MCN_GETDAYSTATE;
1912 nmds.cDayState = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0);
1913 nmds.prgDayState = state = Alloc(nmds.cDayState * sizeof(MONTHDAYSTATE));
1915 MONTHCAL_GetMinDate(infoPtr, &nmds.stStart);
1916 nmds.stStart.wDay = 1;
1918 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1919 memcpy(infoPtr->monthdayState, nmds.prgDayState,
1920 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
1922 Free(state);
1925 /* no valid range check performed */
1926 static void MONTHCAL_Scroll(MONTHCAL_INFO *infoPtr, INT delta)
1928 INT i, selIdx = -1;
1930 for(i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1932 /* save selection position to shift it later */
1933 if (selIdx == -1 && MONTHCAL_CompareMonths(&infoPtr->minSel, &infoPtr->calendars[i].month) == 0)
1934 selIdx = i;
1936 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, delta);
1939 /* selection is always shifted to first calendar */
1940 if(infoPtr->dwStyle & MCS_MULTISELECT)
1942 SYSTEMTIME range[2];
1944 MONTHCAL_GetSelRange(infoPtr, range);
1945 MONTHCAL_GetMonth(&range[0], delta - selIdx);
1946 MONTHCAL_GetMonth(&range[1], delta - selIdx);
1947 MONTHCAL_SetSelRange(infoPtr, range);
1949 else
1951 SYSTEMTIME st = infoPtr->minSel;
1953 MONTHCAL_GetMonth(&st, delta - selIdx);
1954 MONTHCAL_SetCurSel(infoPtr, &st);
1958 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
1960 INT delta = infoPtr->delta ? infoPtr->delta : MONTHCAL_GetCalCount(infoPtr);
1961 SYSTEMTIME st;
1963 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
1965 /* check if change allowed by range set */
1966 if(direction == DIRECTION_BACKWARD)
1968 st = infoPtr->calendars[0].month;
1969 MONTHCAL_GetMonth(&st, -delta);
1971 else
1973 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1974 MONTHCAL_GetMonth(&st, delta);
1977 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
1979 MONTHCAL_Scroll(infoPtr, direction == DIRECTION_BACKWARD ? -delta : delta);
1980 MONTHCAL_NotifyDayState(infoPtr);
1981 MONTHCAL_NotifySelectionChange(infoPtr);
1984 static LRESULT
1985 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1987 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1988 HMENU hMenu;
1989 POINT menupoint;
1990 WCHAR buf[32];
1992 hMenu = CreatePopupMenu();
1993 if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1995 WARN("Can't load resource\n");
1996 strcpyW(buf, todayW);
1998 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1999 menupoint.x = (short)LOWORD(lParam);
2000 menupoint.y = (short)HIWORD(lParam);
2001 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2002 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
2003 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
2005 if (infoPtr->dwStyle & MCS_MULTISELECT)
2007 SYSTEMTIME range[2];
2009 range[0] = range[1] = infoPtr->todaysDate;
2010 MONTHCAL_SetSelRange(infoPtr, range);
2012 else
2013 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2015 MONTHCAL_NotifySelectionChange(infoPtr);
2016 MONTHCAL_NotifySelect(infoPtr);
2019 return 0;
2022 /***
2023 * DESCRIPTION:
2024 * Subclassed edit control windproc function
2026 * PARAMETER(S):
2027 * [I] hwnd : the edit window handle
2028 * [I] uMsg : the message that is to be processed
2029 * [I] wParam : first message parameter
2030 * [I] lParam : second message parameter
2033 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2035 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
2037 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
2038 hwnd, uMsg, wParam, lParam);
2040 switch (uMsg)
2042 case WM_GETDLGCODE:
2043 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
2045 case WM_DESTROY:
2047 WNDPROC editProc = infoPtr->EditWndProc;
2048 infoPtr->EditWndProc = NULL;
2049 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
2050 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
2053 case WM_KILLFOCUS:
2054 break;
2056 case WM_KEYDOWN:
2057 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
2058 break;
2060 default:
2061 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
2064 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
2065 SendMessageW(hwnd, WM_CLOSE, 0, 0);
2066 return 0;
2069 /* creates updown control and edit box */
2070 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr, INT calIdx)
2072 RECT *rc = &infoPtr->calendars[calIdx].titleyear;
2073 RECT *title = &infoPtr->calendars[calIdx].title;
2075 infoPtr->hWndYearEdit =
2076 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
2077 rc->left + 3, (title->bottom + title->top - infoPtr->textHeight) / 2,
2078 rc->right - rc->left + 4,
2079 infoPtr->textHeight, infoPtr->hwndSelf,
2080 NULL, NULL, NULL);
2082 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
2084 infoPtr->hWndYearUpDown =
2085 CreateWindowExW(0, UPDOWN_CLASSW, 0,
2086 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
2087 rc->right + 7, (title->bottom + title->top - infoPtr->textHeight) / 2,
2088 18, infoPtr->textHeight, infoPtr->hwndSelf,
2089 NULL, NULL, NULL);
2091 /* attach edit box */
2092 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
2093 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
2094 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
2095 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->calendars[calIdx].month.wYear);
2097 /* subclass edit box */
2098 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
2099 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
2101 SetFocus(infoPtr->hWndYearEdit);
2104 static LRESULT
2105 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2107 MCHITTESTINFO ht;
2108 DWORD hit;
2110 /* Actually we don't need input focus for calendar, this is used to kill
2111 year updown and its buddy edit box */
2112 if (IsWindow(infoPtr->hWndYearUpDown))
2114 SetFocus(infoPtr->hwndSelf);
2115 return 0;
2118 SetCapture(infoPtr->hwndSelf);
2120 ht.cbSize = sizeof(MCHITTESTINFO);
2121 ht.pt.x = (short)LOWORD(lParam);
2122 ht.pt.y = (short)HIWORD(lParam);
2124 hit = MONTHCAL_HitTest(infoPtr, &ht);
2126 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
2128 switch(hit)
2130 case MCHT_TITLEBTNNEXT:
2131 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2132 infoPtr->status = MC_NEXTPRESSED;
2133 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2134 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2135 return 0;
2137 case MCHT_TITLEBTNPREV:
2138 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2139 infoPtr->status = MC_PREVPRESSED;
2140 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2141 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2142 return 0;
2144 case MCHT_TITLEMONTH:
2146 HMENU hMenu = CreatePopupMenu();
2147 WCHAR buf[32];
2148 POINT menupoint;
2149 INT i;
2151 for (i = 0; i < 12; i++)
2153 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
2154 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
2156 menupoint.x = ht.pt.x;
2157 menupoint.y = ht.pt.y;
2158 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2159 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
2160 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
2162 if ((i > 0) && (i < 13) && infoPtr->calendars[ht.iOffset].month.wMonth != i)
2164 INT delta = i - infoPtr->calendars[ht.iOffset].month.wMonth;
2165 SYSTEMTIME st;
2167 /* check if change allowed by range set */
2168 st = delta < 0 ? infoPtr->calendars[0].month :
2169 infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2170 MONTHCAL_GetMonth(&st, delta);
2172 if (MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE))
2174 MONTHCAL_Scroll(infoPtr, delta);
2175 MONTHCAL_NotifyDayState(infoPtr);
2176 MONTHCAL_NotifySelectionChange(infoPtr);
2177 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2180 return 0;
2182 case MCHT_TITLEYEAR:
2184 MONTHCAL_EditYear(infoPtr, ht.iOffset);
2185 return 0;
2187 case MCHT_TODAYLINK:
2189 if (infoPtr->dwStyle & MCS_MULTISELECT)
2191 SYSTEMTIME range[2];
2193 range[0] = range[1] = infoPtr->todaysDate;
2194 MONTHCAL_SetSelRange(infoPtr, range);
2196 else
2197 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2199 MONTHCAL_NotifySelectionChange(infoPtr);
2200 MONTHCAL_NotifySelect(infoPtr);
2201 return 0;
2203 case MCHT_CALENDARDATENEXT:
2204 case MCHT_CALENDARDATEPREV:
2205 case MCHT_CALENDARDATE:
2207 SYSTEMTIME st[2];
2209 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2211 st[0] = st[1] = ht.st;
2212 /* clear selection range */
2213 MONTHCAL_SetSelRange(infoPtr, st);
2215 infoPtr->status = MC_SEL_LBUTDOWN;
2216 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
2217 return 0;
2221 return 1;
2225 static LRESULT
2226 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2228 NMHDR nmhdr;
2229 MCHITTESTINFO ht;
2230 DWORD hit;
2232 TRACE("\n");
2234 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2235 RECT *r;
2237 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2238 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2239 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2241 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2244 ReleaseCapture();
2246 /* always send NM_RELEASEDCAPTURE notification */
2247 nmhdr.hwndFrom = infoPtr->hwndSelf;
2248 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2249 nmhdr.code = NM_RELEASEDCAPTURE;
2250 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2252 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2254 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2256 ht.cbSize = sizeof(MCHITTESTINFO);
2257 ht.pt.x = (short)LOWORD(lParam);
2258 ht.pt.y = (short)HIWORD(lParam);
2259 hit = MONTHCAL_HitTest(infoPtr, &ht);
2261 infoPtr->status = MC_SEL_LBUTUP;
2262 MONTHCAL_SetDayFocus(infoPtr, NULL);
2264 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2266 SYSTEMTIME sel = infoPtr->minSel;
2268 /* will be invalidated here */
2269 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2271 /* send MCN_SELCHANGE only if new date selected */
2272 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2273 MONTHCAL_NotifySelectionChange(infoPtr);
2275 MONTHCAL_NotifySelect(infoPtr);
2278 return 0;
2282 static LRESULT
2283 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2285 TRACE("%ld\n", id);
2287 switch(id) {
2288 case MC_PREVNEXTMONTHTIMER:
2289 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2290 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2291 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2292 break;
2293 case MC_TODAYUPDATETIMER:
2295 SYSTEMTIME st;
2297 if(infoPtr->todaySet) return 0;
2299 GetLocalTime(&st);
2300 MONTHCAL_UpdateToday(infoPtr, &st);
2302 /* notification sent anyway */
2303 MONTHCAL_NotifySelectionChange(infoPtr);
2305 return 0;
2307 default:
2308 ERR("got unknown timer %ld\n", id);
2309 break;
2312 return 0;
2316 static LRESULT
2317 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2319 MCHITTESTINFO ht;
2320 SYSTEMTIME st_ht;
2321 INT hit;
2322 RECT r;
2324 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2326 ht.cbSize = sizeof(MCHITTESTINFO);
2327 ht.pt.x = (short)LOWORD(lParam);
2328 ht.pt.y = (short)HIWORD(lParam);
2329 ht.iOffset = -1;
2331 hit = MONTHCAL_HitTest(infoPtr, &ht);
2333 /* not on the calendar date numbers? bail out */
2334 TRACE("hit:%x\n",hit);
2335 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2337 MONTHCAL_SetDayFocus(infoPtr, NULL);
2338 return 0;
2341 st_ht = ht.st;
2343 /* if pointer is over focused day still there's nothing to do */
2344 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2346 MONTHCAL_GetDayRect(infoPtr, &ht.st, &r, ht.iOffset);
2348 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2349 SYSTEMTIME st[2];
2351 MONTHCAL_GetSelRange(infoPtr, st);
2353 /* If we're still at the first selected date and range is empty, return.
2354 If range isn't empty we should change range to a single firstSel */
2355 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2356 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2358 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2360 st[0] = infoPtr->firstSel;
2361 /* we should overwrite timestamp here */
2362 MONTHCAL_CopyDate(&st_ht, &st[1]);
2364 /* bounds will be swapped here if needed */
2365 MONTHCAL_SetSelRange(infoPtr, st);
2367 return 0;
2370 done:
2372 /* FIXME: this should specify a rectangle containing only the days that changed
2373 using InvalidateRect */
2374 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2376 return 0;
2380 static LRESULT
2381 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2383 HDC hdc;
2384 PAINTSTRUCT ps;
2386 if (hdc_paint)
2388 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2389 hdc = hdc_paint;
2391 else
2392 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2394 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2395 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2396 return 0;
2399 static LRESULT
2400 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2402 RECT rc;
2404 if (!GetClipBox(hdc, &rc)) return FALSE;
2406 FillRect(hdc, &rc, infoPtr->brushes[BrushBackground]);
2408 return TRUE;
2411 static LRESULT
2412 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2414 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2416 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2417 return 0;
2419 if (options & PRF_ERASEBKGND)
2420 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2422 if (options & PRF_CLIENT)
2423 MONTHCAL_Paint(infoPtr, hdc);
2425 return 0;
2428 static LRESULT
2429 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2431 TRACE("\n");
2433 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2435 return 0;
2438 /* sets the size information */
2439 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2441 static const WCHAR O0W[] = { '0','0',0 };
2442 RECT *title=&infoPtr->calendars[0].title;
2443 RECT *prev=&infoPtr->titlebtnprev;
2444 RECT *next=&infoPtr->titlebtnnext;
2445 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2446 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2447 RECT *wdays=&infoPtr->calendars[0].wdays;
2448 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2449 RECT *days=&infoPtr->calendars[0].days;
2450 RECT *todayrect=&infoPtr->todayrect;
2452 INT xdiv, dx, dy, i, j, x, y, c_dx, c_dy;
2453 WCHAR buff[80];
2454 TEXTMETRICW tm;
2455 SIZE size, sz;
2456 RECT client;
2457 HFONT font;
2458 HDC hdc;
2460 GetClientRect(infoPtr->hwndSelf, &client);
2462 hdc = GetDC(infoPtr->hwndSelf);
2463 font = SelectObject(hdc, infoPtr->hFont);
2465 /* get the height and width of each day's text */
2466 GetTextMetricsW(hdc, &tm);
2467 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2469 /* find largest abbreviated day name for current locale */
2470 size.cx = sz.cx = 0;
2471 for (i = 0; i < 7; i++)
2473 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + i,
2474 buff, countof(buff)))
2476 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2477 if (sz.cx > size.cx) size.cx = sz.cx;
2479 else /* locale independent fallback on failure */
2481 static const WCHAR SunW[] = { 'S','u','n',0 };
2483 GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2484 break;
2488 infoPtr->textWidth = size.cx + 2;
2490 /* recalculate the height and width increments and offsets */
2491 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2493 /* restore the originally selected font */
2494 SelectObject(hdc, font);
2495 ReleaseDC(infoPtr->hwndSelf, hdc);
2497 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2499 infoPtr->width_increment = size.cx * 2 + 4;
2500 infoPtr->height_increment = infoPtr->textHeight;
2502 /* calculate title area */
2503 title->top = 0;
2504 title->bottom = 3 * infoPtr->height_increment / 2;
2505 title->left = 0;
2506 title->right = infoPtr->width_increment * xdiv;
2508 /* set the dimensions of the next and previous buttons and center */
2509 /* the month text vertically */
2510 prev->top = next->top = title->top + 4;
2511 prev->bottom = next->bottom = title->bottom - 4;
2512 prev->left = title->left + 4;
2513 prev->right = prev->left + (title->bottom - title->top);
2514 next->right = title->right - 4;
2515 next->left = next->right - (title->bottom - title->top);
2517 /* titlemonth->left and right change based upon the current month
2518 and are recalculated in refresh as the current month may change
2519 without the control being resized */
2520 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2521 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2523 /* week numbers */
2524 weeknumrect->left = 0;
2525 weeknumrect->right = infoPtr->dwStyle & MCS_WEEKNUMBERS ? prev->right : 0;
2527 /* days abbreviated names */
2528 wdays->left = days->left = weeknumrect->right;
2529 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2530 wdays->top = title->bottom;
2531 wdays->bottom = wdays->top + infoPtr->height_increment;
2533 days->top = weeknumrect->top = wdays->bottom;
2534 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2536 todayrect->left = 0;
2537 todayrect->right = title->right;
2538 todayrect->top = days->bottom;
2539 todayrect->bottom = days->bottom + infoPtr->height_increment;
2541 /* compute calendar count, update all calendars */
2542 x = (client.right + MC_CALENDAR_PADDING) / (title->right - title->left + MC_CALENDAR_PADDING);
2543 /* today label affects whole height */
2544 if (infoPtr->dwStyle & MCS_NOTODAY)
2545 y = (client.bottom + MC_CALENDAR_PADDING) / (days->bottom - title->top + MC_CALENDAR_PADDING);
2546 else
2547 y = (client.bottom - todayrect->bottom + todayrect->top + MC_CALENDAR_PADDING) /
2548 (days->bottom - title->top + MC_CALENDAR_PADDING);
2550 /* TODO: ensure that count is properly adjusted to fit 12 months constraint */
2551 if (x == 0) x = 1;
2552 if (y == 0) y = 1;
2554 if (x*y != MONTHCAL_GetCalCount(infoPtr))
2556 infoPtr->dim.cx = x;
2557 infoPtr->dim.cy = y;
2558 infoPtr->calendars = ReAlloc(infoPtr->calendars, MONTHCAL_GetCalCount(infoPtr)*sizeof(CALENDAR_INFO));
2560 infoPtr->monthdayState = ReAlloc(infoPtr->monthdayState,
2561 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
2562 MONTHCAL_NotifyDayState(infoPtr);
2564 /* update pointers that we'll need */
2565 title = &infoPtr->calendars[0].title;
2566 wdays = &infoPtr->calendars[0].wdays;
2567 days = &infoPtr->calendars[0].days;
2570 for (i = 1; i < MONTHCAL_GetCalCount(infoPtr); i++)
2572 /* set months */
2573 infoPtr->calendars[i] = infoPtr->calendars[0];
2574 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, i);
2577 /* offset all rectangles to center in client area */
2578 c_dx = (client.right - x * title->right - MC_CALENDAR_PADDING * (x-1)) / 2;
2579 c_dy = (client.bottom - y * todayrect->bottom - MC_CALENDAR_PADDING * (y-1)) / 2;
2581 /* if calendar doesn't fit client area show it at left/top bounds */
2582 if (title->left + c_dx < 0) c_dx = 0;
2583 if (title->top + c_dy < 0) c_dy = 0;
2585 for (i = 0; i < y; i++)
2587 for (j = 0; j < x; j++)
2589 dx = j*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx;
2590 dy = i*(days->bottom - title->top + MC_CALENDAR_PADDING) + c_dy;
2592 OffsetRect(&infoPtr->calendars[i*x+j].title, dx, dy);
2593 OffsetRect(&infoPtr->calendars[i*x+j].titlemonth, dx, dy);
2594 OffsetRect(&infoPtr->calendars[i*x+j].titleyear, dx, dy);
2595 OffsetRect(&infoPtr->calendars[i*x+j].wdays, dx, dy);
2596 OffsetRect(&infoPtr->calendars[i*x+j].weeknums, dx, dy);
2597 OffsetRect(&infoPtr->calendars[i*x+j].days, dx, dy);
2601 OffsetRect(prev, c_dx, c_dy);
2602 OffsetRect(next, (x-1)*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx, c_dy);
2604 i = infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx;
2605 todayrect->left = infoPtr->calendars[i].title.left;
2606 todayrect->right = infoPtr->calendars[i].title.right;
2607 todayrect->top = infoPtr->calendars[i].days.bottom;
2608 todayrect->bottom = infoPtr->calendars[i].days.bottom + infoPtr->height_increment;
2610 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2611 infoPtr->width_increment,infoPtr->height_increment,
2612 wine_dbgstr_rect(&client),
2613 wine_dbgstr_rect(title),
2614 wine_dbgstr_rect(wdays),
2615 wine_dbgstr_rect(days),
2616 wine_dbgstr_rect(todayrect));
2619 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2621 TRACE("(width=%d, height=%d)\n", Width, Height);
2623 MONTHCAL_UpdateSize(infoPtr);
2624 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2626 return 0;
2629 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2631 return (LRESULT)infoPtr->hFont;
2634 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2636 HFONT hOldFont;
2637 LOGFONTW lf;
2639 if (!hFont) return 0;
2641 hOldFont = infoPtr->hFont;
2642 infoPtr->hFont = hFont;
2644 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2645 lf.lfWeight = FW_BOLD;
2646 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2648 MONTHCAL_UpdateSize(infoPtr);
2650 if (redraw)
2651 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2653 return (LRESULT)hOldFont;
2656 /* update theme after a WM_THEMECHANGED message */
2657 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2659 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2660 CloseThemeData (theme);
2661 OpenThemeData (infoPtr->hwndSelf, themeClass);
2662 return 0;
2665 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2666 const STYLESTRUCT *lpss)
2668 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2669 wStyleType, lpss->styleOld, lpss->styleNew);
2671 if (wStyleType != GWL_STYLE) return 0;
2673 infoPtr->dwStyle = lpss->styleNew;
2675 /* make room for week numbers */
2676 if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2677 MONTHCAL_UpdateSize(infoPtr);
2679 return 0;
2682 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2683 STYLESTRUCT *lpss)
2685 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2686 wStyleType, lpss->styleOld, lpss->styleNew);
2688 /* block MCS_MULTISELECT change */
2689 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2691 if (lpss->styleOld & MCS_MULTISELECT)
2692 lpss->styleNew |= MCS_MULTISELECT;
2693 else
2694 lpss->styleNew &= ~MCS_MULTISELECT;
2697 /* block MCS_DAYSTATE change */
2698 if ((lpss->styleNew ^ lpss->styleOld) & MCS_DAYSTATE)
2700 if (lpss->styleOld & MCS_DAYSTATE)
2701 lpss->styleNew |= MCS_DAYSTATE;
2702 else
2703 lpss->styleNew &= ~MCS_DAYSTATE;
2706 return 0;
2709 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2710 static LRESULT
2711 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2713 MONTHCAL_INFO *infoPtr;
2715 /* allocate memory for info structure */
2716 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2717 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2719 if (infoPtr == NULL) {
2720 ERR("could not allocate info memory!\n");
2721 return 0;
2724 infoPtr->hwndSelf = hwnd;
2725 infoPtr->hwndNotify = lpcs->hwndParent;
2726 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2727 infoPtr->dim.cx = infoPtr->dim.cy = 1;
2728 infoPtr->calendars = Alloc(sizeof(CALENDAR_INFO));
2729 if (!infoPtr->calendars) goto fail;
2730 infoPtr->monthdayState = Alloc(3*sizeof(MONTHDAYSTATE));
2731 if (!infoPtr->monthdayState) goto fail;
2733 /* initialize info structure */
2734 /* FIXME: calculate systemtime ->> localtime(subtract timezoneinfo) */
2736 GetLocalTime(&infoPtr->todaysDate);
2737 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2739 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2741 infoPtr->colors[MCSC_BACKGROUND] = comctl32_color.clrWindow;
2742 infoPtr->colors[MCSC_TEXT] = comctl32_color.clrWindowText;
2743 infoPtr->colors[MCSC_TITLEBK] = comctl32_color.clrActiveCaption;
2744 infoPtr->colors[MCSC_TITLETEXT] = comctl32_color.clrWindow;
2745 infoPtr->colors[MCSC_MONTHBK] = comctl32_color.clrWindow;
2746 infoPtr->colors[MCSC_TRAILINGTEXT] = comctl32_color.clrGrayText;
2748 infoPtr->brushes[BrushBackground] = CreateSolidBrush(infoPtr->colors[MCSC_BACKGROUND]);
2749 infoPtr->brushes[BrushTitle] = CreateSolidBrush(infoPtr->colors[MCSC_TITLEBK]);
2750 infoPtr->brushes[BrushMonth] = CreateSolidBrush(infoPtr->colors[MCSC_MONTHBK]);
2752 infoPtr->pens[PenRed] = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
2753 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[MCSC_TEXT]);
2755 infoPtr->minSel = infoPtr->todaysDate;
2756 infoPtr->maxSel = infoPtr->todaysDate;
2757 infoPtr->calendars[0].month = infoPtr->todaysDate;
2758 infoPtr->isUnicode = TRUE;
2760 /* setup control layout and day state data */
2761 MONTHCAL_UpdateSize(infoPtr);
2763 /* today auto update timer, to be freed only on control destruction */
2764 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2766 OpenThemeData (infoPtr->hwndSelf, themeClass);
2768 return 0;
2770 fail:
2771 Free(infoPtr->monthdayState);
2772 Free(infoPtr->calendars);
2773 Free(infoPtr);
2774 return 0;
2777 static LRESULT
2778 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2780 INT i;
2782 /* free month calendar info data */
2783 Free(infoPtr->monthdayState);
2784 Free(infoPtr->calendars);
2785 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2787 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2789 for (i = 0; i < BrushLast; i++) DeleteObject(infoPtr->brushes[i]);
2790 for (i = 0; i < PenLast; i++) DeleteObject(infoPtr->pens[i]);
2792 Free(infoPtr);
2793 return 0;
2797 * Handler for WM_NOTIFY messages
2799 static LRESULT
2800 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2802 /* notification from year edit updown */
2803 if (hdr->code == UDN_DELTAPOS)
2805 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2807 if (hdr->hwndFrom == infoPtr->hWndYearUpDown && nmud->iDelta)
2809 /* year value limits are set up explicitly after updown creation */
2810 MONTHCAL_Scroll(infoPtr, 12 * nmud->iDelta);
2811 MONTHCAL_NotifyDayState(infoPtr);
2812 MONTHCAL_NotifySelectionChange(infoPtr);
2815 return 0;
2818 static inline BOOL
2819 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2821 BOOL prev = infoPtr->isUnicode;
2822 infoPtr->isUnicode = isUnicode;
2823 return prev;
2826 static inline BOOL
2827 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2829 return infoPtr->isUnicode;
2832 static LRESULT WINAPI
2833 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2835 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0);
2837 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2839 if (!infoPtr && (uMsg != WM_CREATE))
2840 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2841 switch(uMsg)
2843 case MCM_GETCURSEL:
2844 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2846 case MCM_SETCURSEL:
2847 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2849 case MCM_GETMAXSELCOUNT:
2850 return MONTHCAL_GetMaxSelCount(infoPtr);
2852 case MCM_SETMAXSELCOUNT:
2853 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2855 case MCM_GETSELRANGE:
2856 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2858 case MCM_SETSELRANGE:
2859 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2861 case MCM_GETMONTHRANGE:
2862 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2864 case MCM_SETDAYSTATE:
2865 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2867 case MCM_GETMINREQRECT:
2868 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2870 case MCM_GETCOLOR:
2871 return MONTHCAL_GetColor(infoPtr, wParam);
2873 case MCM_SETCOLOR:
2874 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2876 case MCM_GETTODAY:
2877 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2879 case MCM_SETTODAY:
2880 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2882 case MCM_HITTEST:
2883 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2885 case MCM_GETFIRSTDAYOFWEEK:
2886 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2888 case MCM_SETFIRSTDAYOFWEEK:
2889 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2891 case MCM_GETRANGE:
2892 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2894 case MCM_SETRANGE:
2895 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2897 case MCM_GETMONTHDELTA:
2898 return MONTHCAL_GetMonthDelta(infoPtr);
2900 case MCM_SETMONTHDELTA:
2901 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2903 case MCM_GETMAXTODAYWIDTH:
2904 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2906 case MCM_SETUNICODEFORMAT:
2907 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2909 case MCM_GETUNICODEFORMAT:
2910 return MONTHCAL_GetUnicodeFormat(infoPtr);
2912 case MCM_GETCALENDARCOUNT:
2913 return MONTHCAL_GetCalCount(infoPtr);
2915 case WM_GETDLGCODE:
2916 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2918 case WM_RBUTTONUP:
2919 return MONTHCAL_RButtonUp(infoPtr, lParam);
2921 case WM_LBUTTONDOWN:
2922 return MONTHCAL_LButtonDown(infoPtr, lParam);
2924 case WM_MOUSEMOVE:
2925 return MONTHCAL_MouseMove(infoPtr, lParam);
2927 case WM_LBUTTONUP:
2928 return MONTHCAL_LButtonUp(infoPtr, lParam);
2930 case WM_PAINT:
2931 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2933 case WM_PRINTCLIENT:
2934 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2936 case WM_ERASEBKGND:
2937 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2939 case WM_SETFOCUS:
2940 return MONTHCAL_SetFocus(infoPtr);
2942 case WM_SIZE:
2943 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2945 case WM_NOTIFY:
2946 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2948 case WM_CREATE:
2949 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2951 case WM_SETFONT:
2952 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2954 case WM_GETFONT:
2955 return MONTHCAL_GetFont(infoPtr);
2957 case WM_TIMER:
2958 return MONTHCAL_Timer(infoPtr, wParam);
2960 case WM_THEMECHANGED:
2961 return theme_changed (infoPtr);
2963 case WM_DESTROY:
2964 return MONTHCAL_Destroy(infoPtr);
2966 case WM_SYSCOLORCHANGE:
2967 COMCTL32_RefreshSysColors();
2968 return 0;
2970 case WM_STYLECHANGED:
2971 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2973 case WM_STYLECHANGING:
2974 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2976 default:
2977 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2978 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2979 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2984 void
2985 MONTHCAL_Register(void)
2987 WNDCLASSW wndClass;
2989 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2990 wndClass.style = CS_GLOBALCLASS;
2991 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2992 wndClass.cbClsExtra = 0;
2993 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2994 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2995 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2996 wndClass.lpszClassName = MONTHCAL_CLASSW;
2998 RegisterClassW(&wndClass);
3002 void
3003 MONTHCAL_Unregister(void)
3005 UnregisterClassW(MONTHCAL_CLASSW, NULL);