ntoskrnl.exe: Implement ObReferenceObjectByName but only for loaded drivers.
[wine.git] / dlls / comctl32 / monthcal.c
blob0fef63824331316860d4d6a536e525b457461bc1
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 height_increment;
122 int width_increment;
123 INT delta; /* scroll rate; # of months that the */
124 /* control moves when user clicks a scroll button */
125 int firstDay; /* Start month calendar with firstDay's day,
126 stored in SYSTEMTIME format */
127 BOOL firstDaySet; /* first week day differs from locale defined */
129 BOOL isUnicode; /* value set with MCM_SETUNICODE format */
131 MONTHDAYSTATE *monthdayState;
132 SYSTEMTIME todaysDate;
133 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
134 int status; /* See MC_SEL flags */
135 SYSTEMTIME firstSel; /* first selected day */
136 INT maxSelCount;
137 SYSTEMTIME minSel; /* contains single selection when used without MCS_MULTISELECT */
138 SYSTEMTIME maxSel;
139 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
140 DWORD rangeValid;
141 SYSTEMTIME minDate;
142 SYSTEMTIME maxDate;
144 RECT titlebtnnext; /* the `next month' button in the header */
145 RECT titlebtnprev; /* the `prev month' button in the header */
146 RECT todayrect; /* `today: xx/xx/xx' text rect */
147 HWND hwndNotify; /* Window to receive the notifications */
148 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
149 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
150 WNDPROC EditWndProc; /* original Edit window procedure */
152 CALENDAR_INFO *calendars;
153 SIZE dim; /* [cx,cy] - dimensions of calendars matrix, row/column count */
154 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
156 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
158 /* empty SYSTEMTIME const */
159 static const SYSTEMTIME st_null;
160 /* valid date limits */
161 static const SYSTEMTIME max_allowed_date = { /* wYear */ 9999, /* wMonth */ 12, /* wDayOfWeek */ 0, /* wDay */ 31 };
162 static const SYSTEMTIME min_allowed_date = { /* wYear */ 1752, /* wMonth */ 9, /* wDayOfWeek */ 0, /* wDay */ 14 };
164 /* Prev/Next buttons */
165 enum nav_direction
167 DIRECTION_BACKWARD,
168 DIRECTION_FORWARD
171 /* helper functions */
172 static inline INT MONTHCAL_GetCalCount(const MONTHCAL_INFO *infoPtr)
174 return infoPtr->dim.cx * infoPtr->dim.cy;
177 /* send a single MCN_SELCHANGE notification */
178 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
180 NMSELCHANGE nmsc;
182 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
183 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
184 nmsc.nmhdr.code = MCN_SELCHANGE;
185 nmsc.stSelStart = infoPtr->minSel;
186 nmsc.stSelStart.wDayOfWeek = 0;
187 if(infoPtr->dwStyle & MCS_MULTISELECT){
188 nmsc.stSelEnd = infoPtr->maxSel;
189 nmsc.stSelEnd.wDayOfWeek = 0;
191 else
192 nmsc.stSelEnd = st_null;
194 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
197 /* send a single MCN_SELECT notification */
198 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
200 NMSELCHANGE nmsc;
202 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
203 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
204 nmsc.nmhdr.code = MCN_SELECT;
205 nmsc.stSelStart = infoPtr->minSel;
206 nmsc.stSelStart.wDayOfWeek = 0;
207 if(infoPtr->dwStyle & MCS_MULTISELECT){
208 nmsc.stSelEnd = infoPtr->maxSel;
209 nmsc.stSelEnd.wDayOfWeek = 0;
211 else
212 nmsc.stSelEnd = st_null;
214 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
217 static inline int MONTHCAL_MonthDiff(const SYSTEMTIME *left, const SYSTEMTIME *right)
219 return (right->wYear - left->wYear)*12 + right->wMonth - left->wMonth;
222 /* returns the number of days in any given month, checking for leap days */
223 /* January is 1, December is 12 */
224 int MONTHCAL_MonthLength(int month, int year)
226 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
227 /* Wrap around, this eases handling. Getting length only we shouldn't care
228 about year change here cause January and December have
229 the same day quantity */
230 if(month == 0)
231 month = 12;
232 else if(month == 13)
233 month = 1;
235 /* special case for calendar transition year */
236 if(month == min_allowed_date.wMonth && year == min_allowed_date.wYear) return 19;
238 /* if we have a leap year add 1 day to February */
239 /* a leap year is a year either divisible by 400 */
240 /* or divisible by 4 and not by 100 */
241 if(month == 2) { /* February */
242 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
243 (year%4 == 0)) ? 1 : 0);
245 else {
246 return mdays[month - 1];
250 /* compares timestamps using date part only */
251 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
253 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
254 (first->wDay == second->wDay);
257 /* make sure that date fields are valid */
258 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
260 if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
261 if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear)) return FALSE;
263 return TRUE;
266 /* Copies timestamp part only.
268 * PARAMETERS
270 * [I] from : source date
271 * [O] to : dest date
273 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
275 to->wHour = from->wHour;
276 to->wMinute = from->wMinute;
277 to->wSecond = from->wSecond;
280 /* Copies date part only.
282 * PARAMETERS
284 * [I] from : source date
285 * [O] to : dest date
287 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
289 to->wYear = from->wYear;
290 to->wMonth = from->wMonth;
291 to->wDay = from->wDay;
292 to->wDayOfWeek = from->wDayOfWeek;
295 /* Compares two dates in SYSTEMTIME format
297 * PARAMETERS
299 * [I] first : pointer to valid first date data to compare
300 * [I] second : pointer to valid second date data to compare
302 * RETURN VALUE
304 * -1 : first < second
305 * 0 : first == second
306 * 1 : first > second
308 * Note that no date validation performed, already validated values expected.
310 LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
312 FILETIME ft_first, ft_second;
314 SystemTimeToFileTime(first, &ft_first);
315 SystemTimeToFileTime(second, &ft_second);
317 return CompareFileTime(&ft_first, &ft_second);
320 static LONG MONTHCAL_CompareMonths(const SYSTEMTIME *first, const SYSTEMTIME *second)
322 SYSTEMTIME st_first, st_second;
324 st_first = st_second = st_null;
325 MONTHCAL_CopyDate(first, &st_first);
326 MONTHCAL_CopyDate(second, &st_second);
327 st_first.wDay = st_second.wDay = 1;
329 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
332 static LONG MONTHCAL_CompareDate(const SYSTEMTIME *first, const SYSTEMTIME *second)
334 SYSTEMTIME st_first, st_second;
336 st_first = st_second = st_null;
337 MONTHCAL_CopyDate(first, &st_first);
338 MONTHCAL_CopyDate(second, &st_second);
340 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
343 /* Checks largest possible date range and configured one
345 * PARAMETERS
347 * [I] infoPtr : valid pointer to control data
348 * [I] date : pointer to valid date data to check
349 * [I] fix : make date fit valid range
351 * RETURN VALUE
353 * TRUE - date within largest and configured range
354 * FALSE - date is outside largest or configured range
356 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr,
357 SYSTEMTIME *date, BOOL fix)
359 const SYSTEMTIME *fix_st = NULL;
361 if(MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) {
362 fix_st = &max_allowed_date;
364 else if(MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1) {
365 fix_st = &min_allowed_date;
367 else {
368 if(infoPtr->rangeValid & GDTR_MAX) {
369 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
370 fix_st = &infoPtr->maxDate;
374 if(infoPtr->rangeValid & GDTR_MIN) {
375 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
376 fix_st = &infoPtr->minDate;
381 if (fix && fix_st) {
382 date->wYear = fix_st->wYear;
383 date->wMonth = fix_st->wMonth;
386 return !fix_st;
389 /* Checks passed range width with configured maximum selection count
391 * PARAMETERS
393 * [I] infoPtr : valid pointer to control data
394 * [I] range0 : pointer to valid date data (requested bound)
395 * [I] range1 : pointer to valid date data (primary bound)
396 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
398 * Adjust value computed basing on primary bound and current maximum selection
399 * count. For simple range check (without adjusted value required) (range0, range1)
400 * relation means nothing.
402 * RETURN VALUE
404 * TRUE - range is shorter or equal to maximum
405 * FALSE - range is larger than maximum
407 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
408 const SYSTEMTIME *range0,
409 const SYSTEMTIME *range1,
410 SYSTEMTIME *adjust)
412 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
413 FILETIME ft_range0, ft_range1;
414 LONG cmp;
416 SystemTimeToFileTime(range0, &ft_range0);
417 SystemTimeToFileTime(range1, &ft_range1);
419 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
420 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
421 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
422 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
424 cmp = CompareFileTime(&ft_range0, &ft_range1);
426 if(cmp == 1)
427 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
428 else
429 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
431 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
433 if(adjust) {
434 if(cmp == 1)
435 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
436 else
437 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
439 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
440 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
441 FileTimeToSystemTime(&ft_range0, adjust);
444 return FALSE;
446 else return TRUE;
449 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
450 Milliseconds are intentionally not validated. */
451 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
453 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
454 return FALSE;
455 else
456 return TRUE;
459 /* Note:Depending on DST, this may be offset by a day.
460 Need to find out if we're on a DST place & adjust the clock accordingly.
461 Above function assumes we have a valid data.
462 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
463 0 = Sunday.
466 /* Returns the day in the week
468 * PARAMETERS
469 * [i] date : input date
470 * [I] inplace : set calculated value back to date structure
472 * RETURN VALUE
473 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
475 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
477 SYSTEMTIME st = st_null;
478 FILETIME ft;
480 MONTHCAL_CopyDate(date, &st);
482 SystemTimeToFileTime(&st, &ft);
483 FileTimeToSystemTime(&ft, &st);
485 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
487 return st.wDayOfWeek;
490 /* add/subtract 'months' from date */
491 static inline void MONTHCAL_GetMonth(SYSTEMTIME *date, INT months)
493 INT length, m = date->wMonth + months;
495 date->wYear += m > 0 ? (m - 1) / 12 : m / 12 - 1;
496 date->wMonth = m > 0 ? (m - 1) % 12 + 1 : 12 + m % 12;
497 /* fix moving from last day in a month */
498 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
499 if(date->wDay > length) date->wDay = length;
500 MONTHCAL_CalculateDayOfWeek(date, TRUE);
503 /* properly updates date to point on next month */
504 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
506 MONTHCAL_GetMonth(date, 1);
509 /* properly updates date to point on prev month */
510 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
512 MONTHCAL_GetMonth(date, -1);
515 /* Returns full date for a first currently visible day */
516 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
518 /* zero indexed calendar has the earliest date */
519 SYSTEMTIME st_first = infoPtr->calendars[0].month;
520 INT firstDay;
522 st_first.wDay = 1;
523 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
525 *date = infoPtr->calendars[0].month;
526 MONTHCAL_GetPrevMonth(date);
528 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
529 (infoPtr->firstDay - firstDay) % 7 + 1;
531 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
532 date->wDay -= 7;
534 /* fix day of week */
535 MONTHCAL_CalculateDayOfWeek(date, TRUE);
538 /* Returns full date for a last currently visible day */
539 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
541 /* the latest date is in latest calendar */
542 SYSTEMTIME st, *lt_month = &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
543 INT first_day;
545 *date = *lt_month;
546 st = *lt_month;
548 /* day of week of first day of current month */
549 st.wDay = 1;
550 first_day = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
552 MONTHCAL_GetNextMonth(date);
553 MONTHCAL_GetPrevMonth(&st);
555 /* last calendar starts with some date from previous month that not displayed */
556 st.wDay = MONTHCAL_MonthLength(st.wMonth, st.wYear) +
557 (infoPtr->firstDay - first_day) % 7 + 1;
558 if (st.wDay > MONTHCAL_MonthLength(st.wMonth, st.wYear)) st.wDay -= 7;
560 /* Use month length to get max day. 42 means max day count in calendar area */
561 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
562 MONTHCAL_MonthLength(lt_month->wMonth, lt_month->wYear);
564 /* fix day of week */
565 MONTHCAL_CalculateDayOfWeek(date, TRUE);
568 /* From a given point calculate the row, column and day in the calendar,
569 'day == 0' means the last day of the last month. */
570 static int MONTHCAL_GetDayFromPos(const MONTHCAL_INFO *infoPtr, POINT pt, INT calIdx)
572 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
573 int firstDay, col, row;
574 RECT client;
576 GetClientRect(infoPtr->hwndSelf, &client);
578 /* if the point is outside the x bounds of the window put it at the boundary */
579 if (pt.x > client.right) pt.x = client.right;
581 col = (pt.x - infoPtr->calendars[calIdx].days.left ) / infoPtr->width_increment;
582 row = (pt.y - infoPtr->calendars[calIdx].days.top ) / infoPtr->height_increment;
584 st.wDay = 1;
585 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
586 return col + 7 * row - firstDay;
589 /* Get day position for given date and calendar
591 * PARAMETERS
593 * [I] infoPtr : pointer to control data
594 * [I] date : date value
595 * [O] col : day column (zero based)
596 * [O] row : week column (zero based)
597 * [I] calIdx : calendar index
599 static void MONTHCAL_GetDayPos(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
600 INT *col, INT *row, INT calIdx)
602 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
603 INT first;
605 st.wDay = 1;
606 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
608 if (calIdx == 0 || calIdx == MONTHCAL_GetCalCount(infoPtr)-1) {
609 const SYSTEMTIME *cal = &infoPtr->calendars[calIdx].month;
610 LONG cmp = MONTHCAL_CompareMonths(date, &st);
612 /* previous month */
613 if (cmp == -1) {
614 *col = (first - MONTHCAL_MonthLength(date->wMonth, cal->wYear) + date->wDay) % 7;
615 *row = 0;
616 return;
619 /* next month calculation is same as for current, just add current month length */
620 if (cmp == 1)
621 first += MONTHCAL_MonthLength(cal->wMonth, cal->wYear);
624 *col = (date->wDay + first) % 7;
625 *row = (date->wDay + first - *col) / 7;
628 /* returns bounding box for day in given position in given calendar */
629 static inline void MONTHCAL_GetDayRectI(const MONTHCAL_INFO *infoPtr, RECT *r,
630 INT col, INT row, INT calIdx)
632 r->left = infoPtr->calendars[calIdx].days.left + col * infoPtr->width_increment;
633 r->right = r->left + infoPtr->width_increment;
634 r->top = infoPtr->calendars[calIdx].days.top + row * infoPtr->height_increment;
635 r->bottom = r->top + infoPtr->textHeight;
638 /* Returns bounding box for given date
640 * NOTE: when calendar index is unknown pass -1
642 static inline void MONTHCAL_GetDayRect(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
643 RECT *r, INT calIdx)
645 INT col, row;
647 if (calIdx == -1)
649 INT cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[0].month);
651 if (cmp <= 0)
652 calIdx = 0;
653 else
655 cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month);
656 if (cmp >= 0)
657 calIdx = MONTHCAL_GetCalCount(infoPtr)-1;
658 else
660 for (calIdx = 1; calIdx < MONTHCAL_GetCalCount(infoPtr)-1; calIdx++)
661 if (MONTHCAL_CompareMonths(date, &infoPtr->calendars[calIdx].month) == 0)
662 break;
667 MONTHCAL_GetDayPos(infoPtr, date, &col, &row, calIdx);
668 MONTHCAL_GetDayRectI(infoPtr, r, col, row, calIdx);
671 static LRESULT
672 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
674 INT range;
676 TRACE("flag=%d, st=%p\n", flag, st);
678 switch (flag) {
679 case GMR_VISIBLE:
681 if (st)
683 st[0] = infoPtr->calendars[0].month;
684 st[1] = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
686 if (st[0].wMonth == min_allowed_date.wMonth &&
687 st[0].wYear == min_allowed_date.wYear)
689 st[0].wDay = min_allowed_date.wDay;
691 else
692 st[0].wDay = 1;
693 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
695 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
696 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
699 range = MONTHCAL_GetCalCount(infoPtr);
700 break;
702 case GMR_DAYSTATE:
704 if (st)
706 MONTHCAL_GetMinDate(infoPtr, &st[0]);
707 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
709 /* include two partially visible months */
710 range = MONTHCAL_GetCalCount(infoPtr) + 2;
711 break;
713 default:
714 WARN("Unknown flag value, got %d\n", flag);
715 range = 0;
718 return range;
721 /* Focused day helper:
723 - set focused date to given value;
724 - reset to zero value if NULL passed;
725 - invalidate previous and new day rectangle only if needed.
727 Returns TRUE if focused day changed, FALSE otherwise.
729 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
731 RECT r;
733 if(st)
735 /* there's nothing to do if it's the same date,
736 mouse move within same date rectangle case */
737 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
739 /* invalidate old focused day */
740 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
741 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
743 infoPtr->focusedSel = *st;
746 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
748 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
749 infoPtr->focusedSel = st_null;
751 /* on set invalidates new day, on reset clears previous focused day */
752 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
754 return TRUE;
757 /* draw today boundary box for specified rectangle */
758 static void MONTHCAL_Circle(const MONTHCAL_INFO *infoPtr, HDC hdc, const RECT *r)
760 HPEN old_pen = SelectObject(hdc, infoPtr->pens[PenRed]);
761 HBRUSH old_brush;
763 old_brush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
764 Rectangle(hdc, r->left, r->top, r->right, r->bottom);
766 SelectObject(hdc, old_brush);
767 SelectObject(hdc, old_pen);
770 /* Draw today day mark rectangle
772 * [I] hdc : context to draw in
773 * [I] date : day to mark with rectangle
776 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
777 const SYSTEMTIME *date)
779 RECT r;
781 MONTHCAL_GetDayRect(infoPtr, date, &r, -1);
782 MONTHCAL_Circle(infoPtr, hdc, &r);
785 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
786 int bold, const PAINTSTRUCT *ps)
788 static const WCHAR fmtW[] = { '%','d',0 };
789 WCHAR buf[10];
790 RECT r, r_temp;
791 COLORREF oldCol = 0;
792 COLORREF oldBk = 0;
793 INT old_bkmode, selection;
795 /* no need to check styles: when selection is not valid, it is set to zero.
796 1 < day < 31, so everything is OK */
797 MONTHCAL_GetDayRect(infoPtr, st, &r, -1);
798 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
800 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
801 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0))
803 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
804 TRACE("%s\n", wine_dbgstr_rect(&r));
805 oldCol = SetTextColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
806 oldBk = SetBkColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
807 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
809 selection = 1;
811 else
812 selection = 0;
814 SelectObject(hdc, bold ? infoPtr->hBoldFont : infoPtr->hFont);
816 old_bkmode = SetBkMode(hdc, TRANSPARENT);
817 wsprintfW(buf, fmtW, st->wDay);
818 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
819 SetBkMode(hdc, old_bkmode);
821 if (selection)
823 SetTextColor(hdc, oldCol);
824 SetBkColor(hdc, oldBk);
828 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, enum nav_direction button)
830 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
831 RECT *r = button == DIRECTION_FORWARD ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
832 BOOL pressed = button == DIRECTION_FORWARD ? infoPtr->status & MC_NEXTPRESSED :
833 infoPtr->status & MC_PREVPRESSED;
834 if (theme)
836 static const int states[] = {
837 /* Prev button */
838 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
839 /* Next button */
840 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
842 int stateNum = button == DIRECTION_FORWARD ? 3 : 0;
843 if (pressed)
844 stateNum += 1;
845 else
847 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
849 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
851 else
853 int style = button == DIRECTION_FORWARD ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
854 if (pressed)
855 style |= DFCS_PUSHED;
856 else
858 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
861 DrawFrameControl(hdc, r, DFC_SCROLL, style);
865 /* paint a title with buttons and month/year string */
866 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
868 static const WCHAR mmmmW[] = {'M','M','M','M',0};
869 static const WCHAR mmmW[] = {'M','M','M',0};
870 static const WCHAR mmW[] = {'M','M',0};
871 static const WCHAR fmtyearW[] = {'%','l','d',0};
872 static const WCHAR fmtmmW[] = {'%','0','2','d',0};
873 static const WCHAR fmtmW[] = {'%','d',0};
874 RECT *title = &infoPtr->calendars[calIdx].title;
875 const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
876 WCHAR monthW[80], strW[80], fmtW[80], yearW[6] /* valid year range is 1601-30827 */;
877 int yearoffset, monthoffset, shiftX;
878 SIZE sz;
880 /* fill header box */
881 FillRect(hdc, title, infoPtr->brushes[BrushTitle]);
883 /* month/year string */
884 SetBkColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
885 SetTextColor(hdc, infoPtr->colors[MCSC_TITLETEXT]);
886 SelectObject(hdc, infoPtr->hBoldFont);
888 /* draw formatted date string */
889 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_YEARMONTH, st, NULL, strW, countof(strW));
890 DrawTextW(hdc, strW, strlenW(strW), title, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
892 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SYEARMONTH, fmtW, countof(fmtW));
893 wsprintfW(yearW, fmtyearW, st->wYear);
895 /* month is trickier as it's possible to have different format pictures, we'll
896 test for M, MM, MMM, and MMMM */
897 if (strstrW(fmtW, mmmmW))
898 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+st->wMonth-1, monthW, countof(monthW));
899 else if (strstrW(fmtW, mmmW))
900 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME1+st->wMonth-1, monthW, countof(monthW));
901 else if (strstrW(fmtW, mmW))
902 wsprintfW(monthW, fmtmmW, st->wMonth);
903 else
904 wsprintfW(monthW, fmtmW, st->wMonth);
906 /* update hit boxes */
907 yearoffset = 0;
908 while (strW[yearoffset])
910 if (!strncmpW(&strW[yearoffset], yearW, strlenW(yearW)))
911 break;
912 yearoffset++;
915 monthoffset = 0;
916 while (strW[monthoffset])
918 if (!strncmpW(&strW[monthoffset], monthW, strlenW(monthW)))
919 break;
920 monthoffset++;
923 /* for left limits use offsets */
924 sz.cx = 0;
925 if (yearoffset)
926 GetTextExtentPoint32W(hdc, strW, yearoffset, &sz);
927 infoPtr->calendars[calIdx].titleyear.left = sz.cx;
929 sz.cx = 0;
930 if (monthoffset)
931 GetTextExtentPoint32W(hdc, strW, monthoffset, &sz);
932 infoPtr->calendars[calIdx].titlemonth.left = sz.cx;
934 /* for right limits use actual string parts lengths */
935 GetTextExtentPoint32W(hdc, &strW[yearoffset], strlenW(yearW), &sz);
936 infoPtr->calendars[calIdx].titleyear.right = infoPtr->calendars[calIdx].titleyear.left + sz.cx;
938 GetTextExtentPoint32W(hdc, monthW, strlenW(monthW), &sz);
939 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
941 /* Finally translate rectangles to match center aligned string,
942 hit rectangles are relative to title rectangle before translation. */
943 GetTextExtentPoint32W(hdc, strW, strlenW(strW), &sz);
944 shiftX = (title->right - title->left - sz.cx) / 2 + title->left;
945 OffsetRect(&infoPtr->calendars[calIdx].titleyear, shiftX, 0);
946 OffsetRect(&infoPtr->calendars[calIdx].titlemonth, shiftX, 0);
949 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
951 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
952 static const WCHAR fmt_weekW[] = { '%','d',0 };
953 INT mindays, weeknum, weeknum1, startofprescal;
954 INT i, prev_month;
955 SYSTEMTIME st;
956 WCHAR buf[80];
957 HPEN old_pen;
958 RECT r;
960 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
962 MONTHCAL_GetMinDate(infoPtr, &st);
963 startofprescal = st.wDay;
964 st = *date;
966 prev_month = date->wMonth - 1;
967 if(prev_month == 0) prev_month = 12;
970 Rules what week to call the first week of a new year:
971 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
972 The week containing Jan 1 is the first week of year
973 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
974 First week of year must contain 4 days of the new year
975 LOCALE_IFIRSTWEEKOFYEAR == 1 (what countries?)
976 The first week of the year must contain only days of the new year
978 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
979 weeknum = atoiW(buf);
980 switch (weeknum)
982 case 1: mindays = 6;
983 break;
984 case 2: mindays = 3;
985 break;
986 case 0: mindays = 0;
987 break;
988 default:
989 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
990 mindays = 0;
993 if (date->wMonth == 1)
995 /* calculate all those exceptions for January */
996 st.wDay = st.wMonth = 1;
997 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
998 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
999 weeknum = 1;
1000 else
1002 weeknum = 0;
1003 for(i = 0; i < 11; i++)
1004 weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
1006 weeknum += startofprescal + 7;
1007 weeknum /= 7;
1008 st.wYear -= 1;
1009 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
1010 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
1013 else
1015 weeknum = 0;
1016 for(i = 0; i < prev_month - 1; i++)
1017 weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
1019 weeknum += startofprescal + 7;
1020 weeknum /= 7;
1021 st.wDay = st.wMonth = 1;
1022 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
1023 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
1026 r = infoPtr->calendars[calIdx].weeknums;
1028 /* erase whole week numbers area */
1029 FillRect(hdc, &r, infoPtr->brushes[BrushMonth]);
1030 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1032 /* reduce rectangle to one week number */
1033 r.bottom = r.top + infoPtr->height_increment;
1035 for(i = 0; i < 6; i++) {
1036 if((i == 0) && (weeknum > 50))
1038 wsprintfW(buf, fmt_weekW, weeknum);
1039 weeknum = 0;
1041 else if((i == 5) && (weeknum > 47))
1043 wsprintfW(buf, fmt_weekW, 1);
1045 else
1046 wsprintfW(buf, fmt_weekW, weeknum + i);
1048 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1049 OffsetRect(&r, 0, infoPtr->height_increment);
1052 /* line separator for week numbers column */
1053 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1054 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
1055 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
1056 SelectObject(hdc, old_pen);
1059 /* bottom today date */
1060 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1062 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
1063 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
1064 RECT text_rect, box_rect;
1065 HFONT old_font;
1066 INT col;
1068 if(infoPtr->dwStyle & MCS_NOTODAY) return;
1070 LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, countof(buf_todayW));
1071 col = infoPtr->dwStyle & MCS_NOTODAYCIRCLE ? 0 : 1;
1072 if (infoPtr->dwStyle & MCS_WEEKNUMBERS) col--;
1073 /* label is located below first calendar last row */
1074 MONTHCAL_GetDayRectI(infoPtr, &text_rect, col, 6, infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx);
1075 box_rect = text_rect;
1077 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
1078 buf_dateW, countof(buf_dateW));
1079 old_font = SelectObject(hdc, infoPtr->hBoldFont);
1080 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1082 wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
1083 DrawTextW(hdc, buf, -1, &text_rect, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1084 DrawTextW(hdc, buf, -1, &text_rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1086 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
1087 OffsetRect(&box_rect, -infoPtr->width_increment, 0);
1088 MONTHCAL_Circle(infoPtr, hdc, &box_rect);
1091 SelectObject(hdc, old_font);
1094 /* today mark + focus */
1095 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1097 /* circle today date if only it's in fully visible month */
1098 if (!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
1100 INT i;
1102 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1103 if (!MONTHCAL_CompareMonths(&infoPtr->todaysDate, &infoPtr->calendars[i].month))
1105 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
1106 break;
1110 if (!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
1112 RECT r;
1113 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
1114 DrawFocusRect(hdc, &r);
1118 /* months before first calendar month and after last calendar month */
1119 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1121 INT mask, index;
1122 UINT length;
1123 SYSTEMTIME st_max, st;
1125 if (infoPtr->dwStyle & MCS_NOTRAILINGDATES) return;
1127 SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
1129 /* draw prev month */
1130 MONTHCAL_GetMinDate(infoPtr, &st);
1131 mask = 1 << (st.wDay-1);
1132 /* December and January both 31 days long, so no worries if wrapped */
1133 length = MONTHCAL_MonthLength(infoPtr->calendars[0].month.wMonth - 1,
1134 infoPtr->calendars[0].month.wYear);
1135 index = 0;
1136 while(st.wDay <= length)
1138 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1139 mask <<= 1;
1140 st.wDay++;
1143 /* draw next month */
1144 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1145 st.wDay = 1;
1146 MONTHCAL_GetNextMonth(&st);
1147 MONTHCAL_GetMaxDate(infoPtr, &st_max);
1148 mask = 1;
1149 index = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)-1;
1150 while(st.wDay <= st_max.wDay)
1152 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1153 mask <<= 1;
1154 st.wDay++;
1158 static int get_localized_dayname(const MONTHCAL_INFO *infoPtr, unsigned int day, WCHAR *buff, unsigned int count)
1160 LCTYPE lctype;
1162 if (infoPtr->dwStyle & MCS_SHORTDAYSOFWEEK)
1163 lctype = LOCALE_SSHORTESTDAYNAME1 + day;
1164 else
1165 lctype = LOCALE_SABBREVDAYNAME1 + day;
1167 return GetLocaleInfoW(LOCALE_USER_DEFAULT, lctype, buff, count);
1170 /* paint a calendar area */
1171 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
1173 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
1174 INT i, j;
1175 UINT length;
1176 RECT r, fill_bk_rect;
1177 SYSTEMTIME st;
1178 WCHAR buf[80];
1179 HPEN old_pen;
1180 int mask;
1182 /* fill whole days area - from week days area to today note rectangle */
1183 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
1184 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
1185 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
1187 FillRect(hdc, &fill_bk_rect, infoPtr->brushes[BrushMonth]);
1189 /* draw line under day abbreviations */
1190 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1191 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
1192 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
1193 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
1194 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
1195 SelectObject(hdc, old_pen);
1197 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
1198 infoPtr->calendars[calIdx].weeknums.right;
1200 /* draw day abbreviations */
1201 SelectObject(hdc, infoPtr->hFont);
1202 SetBkColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
1203 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1204 /* rectangle to draw a single day abbreviation within */
1205 r = infoPtr->calendars[calIdx].wdays;
1206 r.right = r.left + infoPtr->width_increment;
1208 i = infoPtr->firstDay;
1209 for(j = 0; j < 7; j++) {
1210 get_localized_dayname(infoPtr, (i + j + 6) % 7, buf, countof(buf));
1211 DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1212 OffsetRect(&r, infoPtr->width_increment, 0);
1215 /* draw current month */
1216 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1217 st = *date;
1218 st.wDay = 1;
1219 mask = 1;
1220 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1221 while(st.wDay <= length)
1223 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[calIdx+1] & mask, ps);
1224 mask <<= 1;
1225 st.wDay++;
1229 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1231 COLORREF old_text_clr, old_bk_clr;
1232 HFONT old_font;
1233 INT i;
1235 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1236 old_bk_clr = GetBkColor(hdc);
1237 old_font = GetCurrentObject(hdc, OBJ_FONT);
1239 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1241 RECT *title = &infoPtr->calendars[i].title;
1242 RECT r;
1244 /* draw title, redraw all its elements */
1245 if (IntersectRect(&r, &(ps->rcPaint), title))
1246 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1248 /* draw calendar area */
1249 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1250 if (IntersectRect(&r, &(ps->rcPaint), &r))
1251 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1253 /* week numbers */
1254 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1257 /* partially visible months */
1258 MONTHCAL_PaintLeadTrailMonths(infoPtr, hdc, ps);
1260 /* focus and today rectangle */
1261 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1263 /* today at the bottom left */
1264 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1266 /* navigation buttons */
1267 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_BACKWARD);
1268 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_FORWARD);
1270 /* restore context */
1271 SetBkColor(hdc, old_bk_clr);
1272 SelectObject(hdc, old_font);
1273 SetTextColor(hdc, old_text_clr);
1276 static LRESULT
1277 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, RECT *rect)
1279 TRACE("rect %p\n", rect);
1281 if(!rect) return FALSE;
1283 *rect = infoPtr->calendars[0].title;
1284 rect->bottom = infoPtr->calendars[0].days.bottom + infoPtr->todayrect.bottom -
1285 infoPtr->todayrect.top;
1287 AdjustWindowRect(rect, infoPtr->dwStyle, FALSE);
1289 /* minimal rectangle is zero based */
1290 OffsetRect(rect, -rect->left, -rect->top);
1292 TRACE("%s\n", wine_dbgstr_rect(rect));
1294 return TRUE;
1297 static COLORREF
1298 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, UINT index)
1300 TRACE("%p, %d\n", infoPtr, index);
1302 if (index > MCSC_TRAILINGTEXT) return -1;
1303 return infoPtr->colors[index];
1306 static LRESULT
1307 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, UINT index, COLORREF color)
1309 enum CachedBrush type;
1310 COLORREF prev;
1312 TRACE("%p, %d: color %08x\n", infoPtr, index, color);
1314 if (index > MCSC_TRAILINGTEXT) return -1;
1316 prev = infoPtr->colors[index];
1317 infoPtr->colors[index] = color;
1319 /* update cached brush */
1320 switch (index)
1322 case MCSC_BACKGROUND:
1323 type = BrushBackground;
1324 break;
1325 case MCSC_TITLEBK:
1326 type = BrushTitle;
1327 break;
1328 case MCSC_MONTHBK:
1329 type = BrushMonth;
1330 break;
1331 default:
1332 type = BrushLast;
1335 if (type != BrushLast)
1337 DeleteObject(infoPtr->brushes[type]);
1338 infoPtr->brushes[type] = CreateSolidBrush(color);
1341 /* update cached pen */
1342 if (index == MCSC_TEXT)
1344 DeleteObject(infoPtr->pens[PenText]);
1345 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[index]);
1348 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND);
1349 return prev;
1352 static LRESULT
1353 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1355 TRACE("\n");
1357 if(infoPtr->delta)
1358 return infoPtr->delta;
1360 return MONTHCAL_GetMonthRange(infoPtr, GMR_VISIBLE, NULL);
1364 static LRESULT
1365 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1367 INT prev = infoPtr->delta;
1369 TRACE("delta %d\n", delta);
1371 infoPtr->delta = delta;
1372 return prev;
1376 static inline LRESULT
1377 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1379 int day;
1381 /* convert from SYSTEMTIME to locale format */
1382 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1384 return MAKELONG(day, infoPtr->firstDaySet);
1388 /* Sets the first day of the week that will appear in the control
1391 * PARAMETERS:
1392 * [I] infoPtr : valid pointer to control data
1393 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1396 * RETURN VALUE:
1397 * Low word contains previous first day,
1398 * high word indicates was first day forced with this message before or is
1399 * locale defined (TRUE - was forced, FALSE - wasn't).
1401 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1402 * FIXME: we need more error checking here
1404 static LRESULT
1405 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1407 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1408 int new_day;
1410 TRACE("%d\n", day);
1412 if(day == -1)
1414 WCHAR buf[80];
1416 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1417 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1419 new_day = atoiW(buf);
1421 infoPtr->firstDaySet = FALSE;
1423 else if(day >= 7)
1425 new_day = 6; /* max first day allowed */
1426 infoPtr->firstDaySet = TRUE;
1428 else
1430 /* Native behaviour for that case is broken: invalid date number >31
1431 got displayed at (0,0) position, current month starts always from
1432 (1,0) position. Should be implemented here as well only if there's
1433 nothing else to do. */
1434 if (day < -1)
1435 FIXME("No bug compatibility for day=%d\n", day);
1437 new_day = day;
1438 infoPtr->firstDaySet = TRUE;
1441 /* convert from locale to SYSTEMTIME format */
1442 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1444 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1446 return prev;
1449 static LRESULT
1450 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1452 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1455 static LRESULT
1456 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1458 FILETIME ft_min, ft_max;
1460 TRACE("%x %p\n", limits, range);
1462 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1463 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1464 return FALSE;
1466 infoPtr->rangeValid = 0;
1467 infoPtr->minDate = infoPtr->maxDate = st_null;
1469 if (limits & GDTR_MIN)
1471 if (!MONTHCAL_ValidateTime(&range[0]))
1472 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1474 infoPtr->minDate = range[0];
1475 infoPtr->rangeValid |= GDTR_MIN;
1477 if (limits & GDTR_MAX)
1479 if (!MONTHCAL_ValidateTime(&range[1]))
1480 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1482 infoPtr->maxDate = range[1];
1483 infoPtr->rangeValid |= GDTR_MAX;
1486 /* Only one limit set - we are done */
1487 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1488 return TRUE;
1490 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1491 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1493 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1495 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1497 /* Native swaps limits only when both limits are being set. */
1498 SYSTEMTIME st_tmp = infoPtr->minDate;
1499 infoPtr->minDate = infoPtr->maxDate;
1500 infoPtr->maxDate = st_tmp;
1502 else
1504 /* reset the other limit */
1505 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1506 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1507 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1511 return TRUE;
1515 static LRESULT
1516 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1518 TRACE("%p\n", range);
1520 if (!range) return 0;
1522 range[1] = infoPtr->maxDate;
1523 range[0] = infoPtr->minDate;
1525 return infoPtr->rangeValid;
1529 static LRESULT
1530 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1532 TRACE("%p %d %p\n", infoPtr, months, states);
1534 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return 0;
1535 if (months != MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)) return 0;
1537 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1539 return 1;
1542 static LRESULT
1543 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1545 TRACE("%p\n", curSel);
1546 if(!curSel) return FALSE;
1547 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1549 *curSel = infoPtr->minSel;
1550 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1551 return TRUE;
1554 static LRESULT
1555 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1557 SYSTEMTIME prev = infoPtr->minSel, selection;
1558 INT diff;
1559 WORD day;
1561 TRACE("%p\n", curSel);
1562 if(!curSel) return FALSE;
1563 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1565 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1566 /* exit earlier if selection equals current */
1567 if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1569 selection = *curSel;
1570 selection.wHour = selection.wMinute = selection.wSecond = selection.wMilliseconds = 0;
1571 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1573 if(!MONTHCAL_IsDateInValidRange(infoPtr, &selection, FALSE)) return FALSE;
1575 /* scroll calendars only if we have to */
1576 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, curSel);
1577 if (diff <= 0)
1579 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, curSel);
1580 if (diff > 0) diff = 0;
1583 if (diff != 0)
1585 INT i;
1587 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1588 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1591 /* we need to store time part as it is */
1592 selection = *curSel;
1593 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1594 infoPtr->minSel = infoPtr->maxSel = selection;
1596 /* if selection is still in current month, reduce rectangle */
1597 day = prev.wDay;
1598 prev.wDay = curSel->wDay;
1599 if (MONTHCAL_IsDateEqual(&prev, curSel))
1601 RECT r_prev, r_new;
1603 prev.wDay = day;
1604 MONTHCAL_GetDayRect(infoPtr, &prev, &r_prev, -1);
1605 MONTHCAL_GetDayRect(infoPtr, curSel, &r_new, -1);
1607 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1608 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1610 else
1611 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1613 return TRUE;
1617 static LRESULT
1618 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1620 return infoPtr->maxSelCount;
1624 static LRESULT
1625 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1627 TRACE("%d\n", max);
1629 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1630 if(max <= 0) return FALSE;
1632 infoPtr->maxSelCount = max;
1634 return TRUE;
1638 static LRESULT
1639 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1641 TRACE("%p\n", range);
1643 if(!range) return FALSE;
1645 if(infoPtr->dwStyle & MCS_MULTISELECT)
1647 range[1] = infoPtr->maxSel;
1648 range[0] = infoPtr->minSel;
1649 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1650 return TRUE;
1653 return FALSE;
1657 static LRESULT
1658 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1660 SYSTEMTIME old_range[2];
1661 INT diff;
1663 TRACE("%p\n", range);
1665 if(!range || !(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1667 /* adjust timestamps */
1668 if(!MONTHCAL_ValidateTime(&range[0])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1669 if(!MONTHCAL_ValidateTime(&range[1])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1671 /* maximum range exceeded */
1672 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1674 old_range[0] = infoPtr->minSel;
1675 old_range[1] = infoPtr->maxSel;
1677 /* swap if min > max */
1678 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1680 infoPtr->minSel = range[0];
1681 infoPtr->maxSel = range[1];
1683 else
1685 infoPtr->minSel = range[1];
1686 infoPtr->maxSel = range[0];
1689 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, &infoPtr->maxSel);
1690 if (diff < 0)
1692 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, &infoPtr->maxSel);
1693 if (diff > 0) diff = 0;
1696 if (diff != 0)
1698 INT i;
1700 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1701 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1704 /* update day of week */
1705 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1706 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1708 /* redraw if bounds changed */
1709 /* FIXME: no actual need to redraw everything */
1710 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1711 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1713 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1716 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1717 return TRUE;
1721 static LRESULT
1722 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1724 TRACE("%p\n", today);
1726 if(!today) return FALSE;
1727 *today = infoPtr->todaysDate;
1728 return TRUE;
1731 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1733 * RETURN VALUE
1735 * TRUE - today date changed
1736 * FALSE - today date isn't changed
1738 static BOOL
1739 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1741 RECT new_r, old_r;
1743 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1745 MONTHCAL_GetDayRect(infoPtr, &infoPtr->todaysDate, &old_r, -1);
1746 MONTHCAL_GetDayRect(infoPtr, today, &new_r, -1);
1748 infoPtr->todaysDate = *today;
1750 /* only two days need redrawing */
1751 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1752 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1753 /* and today label */
1754 InvalidateRect(infoPtr->hwndSelf, &infoPtr->todayrect, FALSE);
1755 return TRUE;
1758 /* MCM_SETTODAT handler */
1759 static LRESULT
1760 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1762 TRACE("%p\n", today);
1764 if (today)
1766 /* remember if date was set successfully */
1767 if (MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1770 return 0;
1773 /* returns calendar index containing specified point, or -1 if it's background */
1774 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1776 RECT r;
1777 INT i;
1779 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1781 /* whole bounding rectangle allows some optimization to compute */
1782 r.left = infoPtr->calendars[i].title.left;
1783 r.top = infoPtr->calendars[i].title.top;
1784 r.bottom = infoPtr->calendars[i].days.bottom;
1785 r.right = infoPtr->calendars[i].days.right;
1787 if (PtInRect(&r, *pt)) return i;
1790 return -1;
1793 static inline UINT fill_hittest_info(const MCHITTESTINFO *src, MCHITTESTINFO *dest)
1795 dest->uHit = src->uHit;
1796 dest->st = src->st;
1798 if (dest->cbSize == sizeof(MCHITTESTINFO))
1799 memcpy(&dest->rc, &src->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1801 return src->uHit;
1804 static LRESULT
1805 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1807 MCHITTESTINFO htinfo;
1808 SYSTEMTIME *ht_month;
1809 INT day, calIdx;
1811 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1813 htinfo.st = st_null;
1815 /* we should preserve passed fields if hit area doesn't need them */
1816 if (lpht->cbSize == sizeof(MCHITTESTINFO))
1817 memcpy(&htinfo.rc, &lpht->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1819 /* guess in what calendar we are */
1820 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1821 if (calIdx == -1)
1823 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1825 htinfo.uHit = MCHT_TODAYLINK;
1826 htinfo.rc = infoPtr->todayrect;
1828 else
1829 /* outside of calendar area? What's left must be background :-) */
1830 htinfo.uHit = MCHT_CALENDARBK;
1832 return fill_hittest_info(&htinfo, lpht);
1835 /* are we in the header? */
1836 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1837 /* FIXME: buttons hittesting could be optimized cause maximum
1838 two calendars have buttons */
1839 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1841 htinfo.uHit = MCHT_TITLEBTNPREV;
1842 htinfo.rc = infoPtr->titlebtnprev;
1844 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1846 htinfo.uHit = MCHT_TITLEBTNNEXT;
1847 htinfo.rc = infoPtr->titlebtnnext;
1849 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1851 htinfo.uHit = MCHT_TITLEMONTH;
1852 htinfo.rc = infoPtr->calendars[calIdx].titlemonth;
1853 htinfo.iOffset = calIdx;
1855 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1857 htinfo.uHit = MCHT_TITLEYEAR;
1858 htinfo.rc = infoPtr->calendars[calIdx].titleyear;
1859 htinfo.iOffset = calIdx;
1861 else
1863 htinfo.uHit = MCHT_TITLE;
1864 htinfo.rc = infoPtr->calendars[calIdx].title;
1865 htinfo.iOffset = calIdx;
1868 return fill_hittest_info(&htinfo, lpht);
1871 ht_month = &infoPtr->calendars[calIdx].month;
1872 /* days area (including week days and week numbers) */
1873 day = MONTHCAL_GetDayFromPos(infoPtr, lpht->pt, calIdx);
1874 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1876 htinfo.uHit = MCHT_CALENDARDAY;
1877 htinfo.iOffset = calIdx;
1878 htinfo.st.wYear = ht_month->wYear;
1879 htinfo.st.wMonth = (day < 1) ? ht_month->wMonth -1 : ht_month->wMonth;
1880 htinfo.st.wDay = (day < 1) ?
1881 MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day : day;
1883 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1885 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1887 htinfo.uHit = MCHT_CALENDARWEEKNUM;
1888 htinfo.st.wYear = ht_month->wYear;
1889 htinfo.iOffset = calIdx;
1891 if (day < 1)
1893 htinfo.st.wMonth = ht_month->wMonth - 1;
1894 htinfo.st.wDay = MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day;
1896 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1898 htinfo.st.wMonth = ht_month->wMonth + 1;
1899 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1901 else
1903 htinfo.st.wMonth = ht_month->wMonth;
1904 htinfo.st.wDay = day;
1907 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1909 htinfo.iOffset = calIdx;
1910 htinfo.st.wYear = ht_month->wYear;
1911 htinfo.st.wMonth = ht_month->wMonth;
1912 /* previous month only valid for first calendar */
1913 if (day < 1 && calIdx == 0)
1915 htinfo.uHit = MCHT_CALENDARDATEPREV;
1916 MONTHCAL_GetPrevMonth(&htinfo.st);
1917 htinfo.st.wDay = MONTHCAL_MonthLength(htinfo.st.wMonth, htinfo.st.wYear) + day;
1919 /* next month only valid for last calendar */
1920 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear) &&
1921 calIdx == MONTHCAL_GetCalCount(infoPtr)-1)
1923 htinfo.uHit = MCHT_CALENDARDATENEXT;
1924 MONTHCAL_GetNextMonth(&htinfo.st);
1925 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1927 /* multiple calendars case - blank areas for previous/next month */
1928 else if (day < 1 || day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1930 htinfo.uHit = MCHT_CALENDARBK;
1932 else
1934 htinfo.uHit = MCHT_CALENDARDATE;
1935 htinfo.st.wDay = day;
1938 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1939 MONTHCAL_GetDayRectI(infoPtr, &htinfo.rc, htinfo.iCol, htinfo.iRow, calIdx);
1940 /* always update day of week */
1941 MONTHCAL_CalculateDayOfWeek(&htinfo.st, TRUE);
1944 return fill_hittest_info(&htinfo, lpht);
1947 /* MCN_GETDAYSTATE notification helper */
1948 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1950 MONTHDAYSTATE *state;
1951 NMDAYSTATE nmds;
1953 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return;
1955 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1956 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1957 nmds.nmhdr.code = MCN_GETDAYSTATE;
1958 nmds.cDayState = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0);
1959 nmds.prgDayState = state = Alloc(nmds.cDayState * sizeof(MONTHDAYSTATE));
1961 MONTHCAL_GetMinDate(infoPtr, &nmds.stStart);
1962 nmds.stStart.wDay = 1;
1964 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1965 memcpy(infoPtr->monthdayState, nmds.prgDayState,
1966 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
1968 Free(state);
1971 /* no valid range check performed */
1972 static void MONTHCAL_Scroll(MONTHCAL_INFO *infoPtr, INT delta, BOOL keep_selection)
1974 INT i, selIdx = -1;
1976 for(i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1978 /* save selection position to shift it later */
1979 if (selIdx == -1 && MONTHCAL_CompareMonths(&infoPtr->minSel, &infoPtr->calendars[i].month) == 0)
1980 selIdx = i;
1982 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, delta);
1985 if (keep_selection)
1986 return;
1988 /* selection is always shifted to first calendar */
1989 if (infoPtr->dwStyle & MCS_MULTISELECT)
1991 SYSTEMTIME range[2];
1993 MONTHCAL_GetSelRange(infoPtr, range);
1994 MONTHCAL_GetMonth(&range[0], delta - selIdx);
1995 MONTHCAL_GetMonth(&range[1], delta - selIdx);
1996 MONTHCAL_SetSelRange(infoPtr, range);
1998 else
2000 SYSTEMTIME st = infoPtr->minSel;
2002 MONTHCAL_GetMonth(&st, delta - selIdx);
2003 MONTHCAL_SetCurSel(infoPtr, &st);
2007 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
2009 INT delta = infoPtr->delta ? infoPtr->delta : MONTHCAL_GetCalCount(infoPtr);
2010 BOOL keep_selection;
2011 SYSTEMTIME st;
2013 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
2015 /* check if change allowed by range set */
2016 if(direction == DIRECTION_BACKWARD)
2018 st = infoPtr->calendars[0].month;
2019 MONTHCAL_GetMonth(&st, -delta);
2021 else
2023 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2024 MONTHCAL_GetMonth(&st, delta);
2027 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
2029 keep_selection = infoPtr->dwStyle & MCS_NOSELCHANGEONNAV;
2030 MONTHCAL_Scroll(infoPtr, direction == DIRECTION_BACKWARD ? -delta : delta, keep_selection);
2031 MONTHCAL_NotifyDayState(infoPtr);
2032 if (!keep_selection)
2033 MONTHCAL_NotifySelectionChange(infoPtr);
2036 static LRESULT
2037 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2039 HMENU hMenu;
2040 POINT menupoint;
2041 WCHAR buf[32];
2043 hMenu = CreatePopupMenu();
2044 LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf));
2045 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
2046 menupoint.x = (short)LOWORD(lParam);
2047 menupoint.y = (short)HIWORD(lParam);
2048 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2049 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
2050 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
2052 if (infoPtr->dwStyle & MCS_MULTISELECT)
2054 SYSTEMTIME range[2];
2056 range[0] = range[1] = infoPtr->todaysDate;
2057 MONTHCAL_SetSelRange(infoPtr, range);
2059 else
2060 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2062 MONTHCAL_NotifySelectionChange(infoPtr);
2063 MONTHCAL_NotifySelect(infoPtr);
2066 return 0;
2069 /***
2070 * DESCRIPTION:
2071 * Subclassed edit control windproc function
2073 * PARAMETER(S):
2074 * [I] hwnd : the edit window handle
2075 * [I] uMsg : the message that is to be processed
2076 * [I] wParam : first message parameter
2077 * [I] lParam : second message parameter
2080 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2082 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
2084 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
2085 hwnd, uMsg, wParam, lParam);
2087 switch (uMsg)
2089 case WM_GETDLGCODE:
2090 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
2092 case WM_DESTROY:
2094 WNDPROC editProc = infoPtr->EditWndProc;
2095 infoPtr->EditWndProc = NULL;
2096 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
2097 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
2100 case WM_KILLFOCUS:
2101 break;
2103 case WM_KEYDOWN:
2104 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
2105 break;
2107 default:
2108 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
2111 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
2112 SendMessageW(hwnd, WM_CLOSE, 0, 0);
2113 return 0;
2116 /* creates updown control and edit box */
2117 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr, INT calIdx)
2119 RECT *rc = &infoPtr->calendars[calIdx].titleyear;
2120 RECT *title = &infoPtr->calendars[calIdx].title;
2122 infoPtr->hWndYearEdit =
2123 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
2124 rc->left + 3, (title->bottom + title->top - infoPtr->textHeight) / 2,
2125 rc->right - rc->left + 4,
2126 infoPtr->textHeight, infoPtr->hwndSelf,
2127 NULL, NULL, NULL);
2129 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
2131 infoPtr->hWndYearUpDown =
2132 CreateWindowExW(0, UPDOWN_CLASSW, 0,
2133 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
2134 rc->right + 7, (title->bottom + title->top - infoPtr->textHeight) / 2,
2135 18, infoPtr->textHeight, infoPtr->hwndSelf,
2136 NULL, NULL, NULL);
2138 /* attach edit box */
2139 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
2140 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
2141 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
2142 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->calendars[calIdx].month.wYear);
2144 /* subclass edit box */
2145 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
2146 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
2148 SetFocus(infoPtr->hWndYearEdit);
2151 static LRESULT
2152 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2154 MCHITTESTINFO ht;
2155 DWORD hit;
2157 /* Actually we don't need input focus for calendar, this is used to kill
2158 year updown and its buddy edit box */
2159 if (IsWindow(infoPtr->hWndYearUpDown))
2161 SetFocus(infoPtr->hwndSelf);
2162 return 0;
2165 SetCapture(infoPtr->hwndSelf);
2167 ht.cbSize = sizeof(MCHITTESTINFO);
2168 ht.pt.x = (short)LOWORD(lParam);
2169 ht.pt.y = (short)HIWORD(lParam);
2171 hit = MONTHCAL_HitTest(infoPtr, &ht);
2173 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
2175 switch(hit)
2177 case MCHT_TITLEBTNNEXT:
2178 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2179 infoPtr->status = MC_NEXTPRESSED;
2180 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2181 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2182 return 0;
2184 case MCHT_TITLEBTNPREV:
2185 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2186 infoPtr->status = MC_PREVPRESSED;
2187 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2188 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2189 return 0;
2191 case MCHT_TITLEMONTH:
2193 HMENU hMenu = CreatePopupMenu();
2194 WCHAR buf[32];
2195 POINT menupoint;
2196 INT i;
2198 for (i = 0; i < 12; i++)
2200 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
2201 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
2203 menupoint.x = ht.pt.x;
2204 menupoint.y = ht.pt.y;
2205 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2206 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
2207 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
2209 if ((i > 0) && (i < 13) && infoPtr->calendars[ht.iOffset].month.wMonth != i)
2211 INT delta = i - infoPtr->calendars[ht.iOffset].month.wMonth;
2212 SYSTEMTIME st;
2214 /* check if change allowed by range set */
2215 st = delta < 0 ? infoPtr->calendars[0].month :
2216 infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2217 MONTHCAL_GetMonth(&st, delta);
2219 if (MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE))
2221 MONTHCAL_Scroll(infoPtr, delta, FALSE);
2222 MONTHCAL_NotifyDayState(infoPtr);
2223 MONTHCAL_NotifySelectionChange(infoPtr);
2224 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2227 return 0;
2229 case MCHT_TITLEYEAR:
2231 MONTHCAL_EditYear(infoPtr, ht.iOffset);
2232 return 0;
2234 case MCHT_TODAYLINK:
2236 if (infoPtr->dwStyle & MCS_MULTISELECT)
2238 SYSTEMTIME range[2];
2240 range[0] = range[1] = infoPtr->todaysDate;
2241 MONTHCAL_SetSelRange(infoPtr, range);
2243 else
2244 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2246 MONTHCAL_NotifySelectionChange(infoPtr);
2247 MONTHCAL_NotifySelect(infoPtr);
2248 return 0;
2250 case MCHT_CALENDARDATENEXT:
2251 case MCHT_CALENDARDATEPREV:
2252 case MCHT_CALENDARDATE:
2254 SYSTEMTIME st[2];
2256 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2258 st[0] = st[1] = ht.st;
2259 /* clear selection range */
2260 MONTHCAL_SetSelRange(infoPtr, st);
2262 infoPtr->status = MC_SEL_LBUTDOWN;
2263 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
2264 return 0;
2268 return 1;
2272 static LRESULT
2273 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2275 NMHDR nmhdr;
2276 MCHITTESTINFO ht;
2277 DWORD hit;
2279 TRACE("\n");
2281 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2282 RECT *r;
2284 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2285 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2286 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2288 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2291 ReleaseCapture();
2293 /* always send NM_RELEASEDCAPTURE notification */
2294 nmhdr.hwndFrom = infoPtr->hwndSelf;
2295 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2296 nmhdr.code = NM_RELEASEDCAPTURE;
2297 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2299 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2301 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2303 ht.cbSize = sizeof(MCHITTESTINFO);
2304 ht.pt.x = (short)LOWORD(lParam);
2305 ht.pt.y = (short)HIWORD(lParam);
2306 hit = MONTHCAL_HitTest(infoPtr, &ht);
2308 infoPtr->status = MC_SEL_LBUTUP;
2309 MONTHCAL_SetDayFocus(infoPtr, NULL);
2311 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2313 SYSTEMTIME sel = infoPtr->minSel;
2315 /* will be invalidated here */
2316 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2318 /* send MCN_SELCHANGE only if new date selected */
2319 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2320 MONTHCAL_NotifySelectionChange(infoPtr);
2322 MONTHCAL_NotifySelect(infoPtr);
2325 return 0;
2329 static LRESULT
2330 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2332 TRACE("%ld\n", id);
2334 switch(id) {
2335 case MC_PREVNEXTMONTHTIMER:
2336 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2337 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2338 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2339 break;
2340 case MC_TODAYUPDATETIMER:
2342 SYSTEMTIME st;
2344 if(infoPtr->todaySet) return 0;
2346 GetLocalTime(&st);
2347 MONTHCAL_UpdateToday(infoPtr, &st);
2349 /* notification sent anyway */
2350 MONTHCAL_NotifySelectionChange(infoPtr);
2352 return 0;
2354 default:
2355 ERR("got unknown timer %ld\n", id);
2356 break;
2359 return 0;
2363 static LRESULT
2364 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2366 MCHITTESTINFO ht;
2367 SYSTEMTIME st_ht;
2368 INT hit;
2369 RECT r;
2371 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2373 ht.cbSize = sizeof(MCHITTESTINFO);
2374 ht.pt.x = (short)LOWORD(lParam);
2375 ht.pt.y = (short)HIWORD(lParam);
2376 ht.iOffset = -1;
2378 hit = MONTHCAL_HitTest(infoPtr, &ht);
2380 /* not on the calendar date numbers? bail out */
2381 TRACE("hit:%x\n",hit);
2382 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2384 MONTHCAL_SetDayFocus(infoPtr, NULL);
2385 return 0;
2388 st_ht = ht.st;
2390 /* if pointer is over focused day still there's nothing to do */
2391 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2393 MONTHCAL_GetDayRect(infoPtr, &ht.st, &r, ht.iOffset);
2395 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2396 SYSTEMTIME st[2];
2398 MONTHCAL_GetSelRange(infoPtr, st);
2400 /* If we're still at the first selected date and range is empty, return.
2401 If range isn't empty we should change range to a single firstSel */
2402 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2403 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2405 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2407 st[0] = infoPtr->firstSel;
2408 /* we should overwrite timestamp here */
2409 MONTHCAL_CopyDate(&st_ht, &st[1]);
2411 /* bounds will be swapped here if needed */
2412 MONTHCAL_SetSelRange(infoPtr, st);
2414 return 0;
2417 done:
2419 /* FIXME: this should specify a rectangle containing only the days that changed
2420 using InvalidateRect */
2421 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2423 return 0;
2427 static LRESULT
2428 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2430 HDC hdc;
2431 PAINTSTRUCT ps;
2433 if (hdc_paint)
2435 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2436 hdc = hdc_paint;
2438 else
2439 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2441 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2442 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2443 return 0;
2446 static LRESULT
2447 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2449 RECT rc;
2451 if (!GetClipBox(hdc, &rc)) return FALSE;
2453 FillRect(hdc, &rc, infoPtr->brushes[BrushBackground]);
2455 return TRUE;
2458 static LRESULT
2459 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2461 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2463 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2464 return 0;
2466 if (options & PRF_ERASEBKGND)
2467 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2469 if (options & PRF_CLIENT)
2470 MONTHCAL_Paint(infoPtr, hdc);
2472 return 0;
2475 static LRESULT
2476 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2478 TRACE("\n");
2480 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2482 return 0;
2485 /* sets the size information */
2486 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2488 static const WCHAR O0W[] = { '0','0',0 };
2489 RECT *title=&infoPtr->calendars[0].title;
2490 RECT *prev=&infoPtr->titlebtnprev;
2491 RECT *next=&infoPtr->titlebtnnext;
2492 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2493 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2494 RECT *wdays=&infoPtr->calendars[0].wdays;
2495 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2496 RECT *days=&infoPtr->calendars[0].days;
2497 RECT *todayrect=&infoPtr->todayrect;
2499 INT xdiv, dx, dy, i, j, x, y, c_dx, c_dy;
2500 WCHAR buff[80];
2501 TEXTMETRICW tm;
2502 INT day_width;
2503 RECT client;
2504 HFONT font;
2505 SIZE size;
2506 HDC hdc;
2508 GetClientRect(infoPtr->hwndSelf, &client);
2510 hdc = GetDC(infoPtr->hwndSelf);
2511 font = SelectObject(hdc, infoPtr->hFont);
2513 /* get the height and width of each day's text */
2514 GetTextMetricsW(hdc, &tm);
2515 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2517 /* find widest day name for current locale and font */
2518 day_width = 0;
2519 for (i = 0; i < 7; i++)
2521 SIZE sz;
2523 if (get_localized_dayname(infoPtr, i, buff, countof(buff)))
2525 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2526 if (sz.cx > day_width) day_width = sz.cx;
2528 else /* locale independent fallback on failure */
2530 static const WCHAR sunW[] = { 'S','u','n' };
2531 GetTextExtentPoint32W(hdc, sunW, countof(sunW), &sz);
2532 day_width = sz.cx;
2533 break;
2537 day_width += 2;
2539 /* recalculate the height and width increments and offsets */
2540 size.cx = 0;
2541 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2543 /* restore the originally selected font */
2544 SelectObject(hdc, font);
2545 ReleaseDC(infoPtr->hwndSelf, hdc);
2547 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2549 infoPtr->width_increment = max(day_width, size.cx * 2 + 4);
2550 infoPtr->height_increment = infoPtr->textHeight;
2552 /* calculate title area */
2553 title->top = 0;
2554 title->bottom = 3 * infoPtr->height_increment / 2;
2555 title->left = 0;
2556 title->right = infoPtr->width_increment * xdiv;
2558 /* set the dimensions of the next and previous buttons and center */
2559 /* the month text vertically */
2560 prev->top = next->top = title->top + 4;
2561 prev->bottom = next->bottom = title->bottom - 4;
2562 prev->left = title->left + 4;
2563 prev->right = prev->left + (title->bottom - title->top);
2564 next->right = title->right - 4;
2565 next->left = next->right - (title->bottom - title->top);
2567 /* titlemonth->left and right change based upon the current month
2568 and are recalculated in refresh as the current month may change
2569 without the control being resized */
2570 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2571 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2573 /* week numbers */
2574 weeknumrect->left = 0;
2575 weeknumrect->right = infoPtr->dwStyle & MCS_WEEKNUMBERS ? prev->right : 0;
2577 /* days abbreviated names */
2578 wdays->left = days->left = weeknumrect->right;
2579 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2580 wdays->top = title->bottom;
2581 wdays->bottom = wdays->top + infoPtr->height_increment;
2583 days->top = weeknumrect->top = wdays->bottom;
2584 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2586 todayrect->left = 0;
2587 todayrect->right = title->right;
2588 todayrect->top = days->bottom;
2589 todayrect->bottom = days->bottom + infoPtr->height_increment;
2591 /* compute calendar count, update all calendars */
2592 x = (client.right + MC_CALENDAR_PADDING) / (title->right - title->left + MC_CALENDAR_PADDING);
2593 /* today label affects whole height */
2594 if (infoPtr->dwStyle & MCS_NOTODAY)
2595 y = (client.bottom + MC_CALENDAR_PADDING) / (days->bottom - title->top + MC_CALENDAR_PADDING);
2596 else
2597 y = (client.bottom - todayrect->bottom + todayrect->top + MC_CALENDAR_PADDING) /
2598 (days->bottom - title->top + MC_CALENDAR_PADDING);
2600 /* TODO: ensure that count is properly adjusted to fit 12 months constraint */
2601 if (x == 0) x = 1;
2602 if (y == 0) y = 1;
2604 if (x*y != MONTHCAL_GetCalCount(infoPtr))
2606 infoPtr->dim.cx = x;
2607 infoPtr->dim.cy = y;
2608 infoPtr->calendars = ReAlloc(infoPtr->calendars, MONTHCAL_GetCalCount(infoPtr)*sizeof(CALENDAR_INFO));
2610 infoPtr->monthdayState = ReAlloc(infoPtr->monthdayState,
2611 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
2612 MONTHCAL_NotifyDayState(infoPtr);
2614 /* update pointers that we'll need */
2615 title = &infoPtr->calendars[0].title;
2616 wdays = &infoPtr->calendars[0].wdays;
2617 days = &infoPtr->calendars[0].days;
2620 for (i = 1; i < MONTHCAL_GetCalCount(infoPtr); i++)
2622 /* set months */
2623 infoPtr->calendars[i] = infoPtr->calendars[0];
2624 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, i);
2627 /* offset all rectangles to center in client area */
2628 c_dx = (client.right - x * title->right - MC_CALENDAR_PADDING * (x-1)) / 2;
2629 c_dy = (client.bottom - y * todayrect->bottom - MC_CALENDAR_PADDING * (y-1)) / 2;
2631 /* if calendar doesn't fit client area show it at left/top bounds */
2632 if (title->left + c_dx < 0) c_dx = 0;
2633 if (title->top + c_dy < 0) c_dy = 0;
2635 for (i = 0; i < y; i++)
2637 for (j = 0; j < x; j++)
2639 dx = j*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx;
2640 dy = i*(days->bottom - title->top + MC_CALENDAR_PADDING) + c_dy;
2642 OffsetRect(&infoPtr->calendars[i*x+j].title, dx, dy);
2643 OffsetRect(&infoPtr->calendars[i*x+j].titlemonth, dx, dy);
2644 OffsetRect(&infoPtr->calendars[i*x+j].titleyear, dx, dy);
2645 OffsetRect(&infoPtr->calendars[i*x+j].wdays, dx, dy);
2646 OffsetRect(&infoPtr->calendars[i*x+j].weeknums, dx, dy);
2647 OffsetRect(&infoPtr->calendars[i*x+j].days, dx, dy);
2651 OffsetRect(prev, c_dx, c_dy);
2652 OffsetRect(next, (x-1)*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx, c_dy);
2654 i = infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx;
2655 todayrect->left = infoPtr->calendars[i].title.left;
2656 todayrect->right = infoPtr->calendars[i].title.right;
2657 todayrect->top = infoPtr->calendars[i].days.bottom;
2658 todayrect->bottom = infoPtr->calendars[i].days.bottom + infoPtr->height_increment;
2660 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2661 infoPtr->width_increment,infoPtr->height_increment,
2662 wine_dbgstr_rect(&client),
2663 wine_dbgstr_rect(title),
2664 wine_dbgstr_rect(wdays),
2665 wine_dbgstr_rect(days),
2666 wine_dbgstr_rect(todayrect));
2669 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2671 TRACE("(width=%d, height=%d)\n", Width, Height);
2673 MONTHCAL_UpdateSize(infoPtr);
2674 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2676 return 0;
2679 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2681 return (LRESULT)infoPtr->hFont;
2684 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2686 HFONT hOldFont;
2687 LOGFONTW lf;
2689 if (!hFont) return 0;
2691 hOldFont = infoPtr->hFont;
2692 infoPtr->hFont = hFont;
2694 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2695 lf.lfWeight = FW_BOLD;
2696 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2698 MONTHCAL_UpdateSize(infoPtr);
2700 if (redraw)
2701 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2703 return (LRESULT)hOldFont;
2706 /* update theme after a WM_THEMECHANGED message */
2707 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2709 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2710 CloseThemeData (theme);
2711 OpenThemeData (infoPtr->hwndSelf, themeClass);
2712 return 0;
2715 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2716 const STYLESTRUCT *lpss)
2718 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2719 wStyleType, lpss->styleOld, lpss->styleNew);
2721 if (wStyleType != GWL_STYLE) return 0;
2723 infoPtr->dwStyle = lpss->styleNew;
2725 /* make room for week numbers */
2726 if ((lpss->styleNew ^ lpss->styleOld) & (MCS_WEEKNUMBERS | MCS_SHORTDAYSOFWEEK))
2727 MONTHCAL_UpdateSize(infoPtr);
2729 return 0;
2732 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2733 STYLESTRUCT *lpss)
2735 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2736 wStyleType, lpss->styleOld, lpss->styleNew);
2738 /* block MCS_MULTISELECT change */
2739 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2741 if (lpss->styleOld & MCS_MULTISELECT)
2742 lpss->styleNew |= MCS_MULTISELECT;
2743 else
2744 lpss->styleNew &= ~MCS_MULTISELECT;
2747 /* block MCS_DAYSTATE change */
2748 if ((lpss->styleNew ^ lpss->styleOld) & MCS_DAYSTATE)
2750 if (lpss->styleOld & MCS_DAYSTATE)
2751 lpss->styleNew |= MCS_DAYSTATE;
2752 else
2753 lpss->styleNew &= ~MCS_DAYSTATE;
2756 return 0;
2759 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2760 static LRESULT
2761 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2763 MONTHCAL_INFO *infoPtr;
2765 /* allocate memory for info structure */
2766 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2767 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2769 if (infoPtr == NULL) {
2770 ERR("could not allocate info memory!\n");
2771 return 0;
2774 infoPtr->hwndSelf = hwnd;
2775 infoPtr->hwndNotify = lpcs->hwndParent;
2776 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2777 infoPtr->dim.cx = infoPtr->dim.cy = 1;
2778 infoPtr->calendars = Alloc(sizeof(CALENDAR_INFO));
2779 if (!infoPtr->calendars) goto fail;
2780 infoPtr->monthdayState = Alloc(3*sizeof(MONTHDAYSTATE));
2781 if (!infoPtr->monthdayState) goto fail;
2783 /* initialize info structure */
2784 /* FIXME: calculate systemtime ->> localtime(subtract timezoneinfo) */
2786 GetLocalTime(&infoPtr->todaysDate);
2787 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2789 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2791 infoPtr->colors[MCSC_BACKGROUND] = comctl32_color.clrWindow;
2792 infoPtr->colors[MCSC_TEXT] = comctl32_color.clrWindowText;
2793 infoPtr->colors[MCSC_TITLEBK] = comctl32_color.clrActiveCaption;
2794 infoPtr->colors[MCSC_TITLETEXT] = comctl32_color.clrWindow;
2795 infoPtr->colors[MCSC_MONTHBK] = comctl32_color.clrWindow;
2796 infoPtr->colors[MCSC_TRAILINGTEXT] = comctl32_color.clrGrayText;
2798 infoPtr->brushes[BrushBackground] = CreateSolidBrush(infoPtr->colors[MCSC_BACKGROUND]);
2799 infoPtr->brushes[BrushTitle] = CreateSolidBrush(infoPtr->colors[MCSC_TITLEBK]);
2800 infoPtr->brushes[BrushMonth] = CreateSolidBrush(infoPtr->colors[MCSC_MONTHBK]);
2802 infoPtr->pens[PenRed] = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
2803 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[MCSC_TEXT]);
2805 infoPtr->minSel = infoPtr->todaysDate;
2806 infoPtr->maxSel = infoPtr->todaysDate;
2807 infoPtr->calendars[0].month = infoPtr->todaysDate;
2808 infoPtr->isUnicode = TRUE;
2810 /* setup control layout and day state data */
2811 MONTHCAL_UpdateSize(infoPtr);
2813 /* today auto update timer, to be freed only on control destruction */
2814 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2816 OpenThemeData (infoPtr->hwndSelf, themeClass);
2818 return 0;
2820 fail:
2821 Free(infoPtr->monthdayState);
2822 Free(infoPtr->calendars);
2823 Free(infoPtr);
2824 return 0;
2827 static LRESULT
2828 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2830 INT i;
2832 /* free month calendar info data */
2833 Free(infoPtr->monthdayState);
2834 Free(infoPtr->calendars);
2835 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2837 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2839 for (i = 0; i < BrushLast; i++) DeleteObject(infoPtr->brushes[i]);
2840 for (i = 0; i < PenLast; i++) DeleteObject(infoPtr->pens[i]);
2842 Free(infoPtr);
2843 return 0;
2847 * Handler for WM_NOTIFY messages
2849 static LRESULT
2850 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2852 /* notification from year edit updown */
2853 if (hdr->code == UDN_DELTAPOS)
2855 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2857 if (hdr->hwndFrom == infoPtr->hWndYearUpDown && nmud->iDelta)
2859 /* year value limits are set up explicitly after updown creation */
2860 MONTHCAL_Scroll(infoPtr, 12 * nmud->iDelta, FALSE);
2861 MONTHCAL_NotifyDayState(infoPtr);
2862 MONTHCAL_NotifySelectionChange(infoPtr);
2865 return 0;
2868 static inline BOOL
2869 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2871 BOOL prev = infoPtr->isUnicode;
2872 infoPtr->isUnicode = isUnicode;
2873 return prev;
2876 static inline BOOL
2877 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2879 return infoPtr->isUnicode;
2882 static LRESULT WINAPI
2883 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2885 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0);
2887 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2889 if (!infoPtr && (uMsg != WM_CREATE))
2890 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2891 switch(uMsg)
2893 case MCM_GETCURSEL:
2894 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2896 case MCM_SETCURSEL:
2897 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2899 case MCM_GETMAXSELCOUNT:
2900 return MONTHCAL_GetMaxSelCount(infoPtr);
2902 case MCM_SETMAXSELCOUNT:
2903 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2905 case MCM_GETSELRANGE:
2906 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2908 case MCM_SETSELRANGE:
2909 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2911 case MCM_GETMONTHRANGE:
2912 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2914 case MCM_SETDAYSTATE:
2915 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2917 case MCM_GETMINREQRECT:
2918 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2920 case MCM_GETCOLOR:
2921 return MONTHCAL_GetColor(infoPtr, wParam);
2923 case MCM_SETCOLOR:
2924 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2926 case MCM_GETTODAY:
2927 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2929 case MCM_SETTODAY:
2930 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2932 case MCM_HITTEST:
2933 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2935 case MCM_GETFIRSTDAYOFWEEK:
2936 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2938 case MCM_SETFIRSTDAYOFWEEK:
2939 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2941 case MCM_GETRANGE:
2942 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2944 case MCM_SETRANGE:
2945 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2947 case MCM_GETMONTHDELTA:
2948 return MONTHCAL_GetMonthDelta(infoPtr);
2950 case MCM_SETMONTHDELTA:
2951 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2953 case MCM_GETMAXTODAYWIDTH:
2954 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2956 case MCM_SETUNICODEFORMAT:
2957 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2959 case MCM_GETUNICODEFORMAT:
2960 return MONTHCAL_GetUnicodeFormat(infoPtr);
2962 case MCM_GETCALENDARCOUNT:
2963 return MONTHCAL_GetCalCount(infoPtr);
2965 case WM_GETDLGCODE:
2966 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2968 case WM_RBUTTONUP:
2969 return MONTHCAL_RButtonUp(infoPtr, lParam);
2971 case WM_LBUTTONDOWN:
2972 return MONTHCAL_LButtonDown(infoPtr, lParam);
2974 case WM_MOUSEMOVE:
2975 return MONTHCAL_MouseMove(infoPtr, lParam);
2977 case WM_LBUTTONUP:
2978 return MONTHCAL_LButtonUp(infoPtr, lParam);
2980 case WM_PAINT:
2981 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2983 case WM_PRINTCLIENT:
2984 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2986 case WM_ERASEBKGND:
2987 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2989 case WM_SETFOCUS:
2990 return MONTHCAL_SetFocus(infoPtr);
2992 case WM_SIZE:
2993 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2995 case WM_NOTIFY:
2996 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2998 case WM_CREATE:
2999 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
3001 case WM_SETFONT:
3002 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
3004 case WM_GETFONT:
3005 return MONTHCAL_GetFont(infoPtr);
3007 case WM_TIMER:
3008 return MONTHCAL_Timer(infoPtr, wParam);
3010 case WM_THEMECHANGED:
3011 return theme_changed (infoPtr);
3013 case WM_DESTROY:
3014 return MONTHCAL_Destroy(infoPtr);
3016 case WM_SYSCOLORCHANGE:
3017 COMCTL32_RefreshSysColors();
3018 return 0;
3020 case WM_STYLECHANGED:
3021 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
3023 case WM_STYLECHANGING:
3024 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
3026 default:
3027 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
3028 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
3029 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
3034 void
3035 MONTHCAL_Register(void)
3037 WNDCLASSW wndClass;
3039 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
3040 wndClass.style = CS_GLOBALCLASS;
3041 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
3042 wndClass.cbClsExtra = 0;
3043 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
3044 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3045 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
3046 wndClass.lpszClassName = MONTHCAL_CLASSW;
3048 RegisterClassW(&wndClass);
3052 void
3053 MONTHCAL_Unregister(void)
3055 UnregisterClassW(MONTHCAL_CLASSW, NULL);