comctl32/monthcal: Fix today label position.
[wine/multimedia.git] / dlls / comctl32 / monthcal.c
blobdfa3d6e96dc33e86c7404be0251bcb8848364d3e
1 /*
2 * Month calendar control
4 * Copyright 1998, 1999 Eric Kohl (ekohl@abo.rhein-zeitung.de)
5 * Copyright 1999 Alex Priem (alexp@sci.kun.nl)
6 * Copyright 1999 Chris Morgan <cmorgan@wpi.edu> and
7 * James Abbatiello <abbeyj@wpi.edu>
8 * Copyright 2000 Uwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de>
9 * Copyright 2009-2011 Nikolay Sivov
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * NOTE
27 * This code was audited for completeness against the documented features
28 * of Comctl32.dll version 6.0 on Oct. 20, 2004, by Dimitrie O. Paun.
30 * Unless otherwise noted, we believe this code to be complete, as per
31 * the specification mentioned above.
32 * If you discover missing features, or bugs, please note them below.
34 * TODO:
35 * -- MCM_[GS]ETUNICODEFORMAT
36 * -- handle resources better (doesn't work now);
37 * -- take care of internationalization.
38 * -- keyboard handling.
39 * -- search for FIXME
42 #include <math.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "commctrl.h"
54 #include "comctl32.h"
55 #include "uxtheme.h"
56 #include "vssym32.h"
57 #include "wine/unicode.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
62 #define MC_SEL_LBUTUP 1 /* Left button released */
63 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
64 #define MC_PREVPRESSED 4 /* Prev month button pressed */
65 #define MC_NEXTPRESSED 8 /* Next month button pressed */
66 #define MC_PREVNEXTMONTHDELAY 350 /* when continuously pressing `next/prev
67 month', wait 350 ms before going
68 to the next/prev month */
69 #define MC_TODAYUPDATEDELAY 120000 /* time between today check for update (2 min) */
71 #define MC_PREVNEXTMONTHTIMER 1 /* Timer IDs */
72 #define MC_TODAYUPDATETIMER 2
74 #define countof(arr) (sizeof(arr)/sizeof(arr[0]))
76 /* convert from days to 100 nanoseconds unit - used as FILETIME unit */
77 #define DAYSTO100NSECS(days) (((ULONGLONG)(days))*24*60*60*10000000)
79 enum CachedPen
81 PenRed = 0,
82 PenText,
83 PenLast
86 enum CachedBrush
88 BrushTitle = 0,
89 BrushMonth,
90 BrushBackground,
91 BrushLast
94 /* single calendar data */
95 typedef struct _CALENDAR_INFO
97 RECT title; /* rect for the header above the calendar */
98 RECT titlemonth; /* the 'month name' text in the header */
99 RECT titleyear; /* the 'year number' text in the header */
100 RECT wdays; /* week days at top */
101 RECT days; /* calendar area */
102 RECT weeknums; /* week numbers at left side */
104 SYSTEMTIME month;/* contains calendar main month/year */
105 } CALENDAR_INFO;
107 typedef struct
109 HWND hwndSelf;
110 DWORD dwStyle; /* cached GWL_STYLE */
112 COLORREF colors[MCSC_TRAILINGTEXT+1];
113 HBRUSH brushes[BrushLast];
114 HPEN pens[PenLast];
116 HFONT hFont;
117 HFONT hBoldFont;
118 int textHeight;
119 int textWidth;
120 int height_increment;
121 int width_increment;
122 INT delta; /* scroll rate; # of months that the */
123 /* control moves when user clicks a scroll button */
124 int visible; /* # of months visible */
125 int firstDay; /* Start month calendar with firstDay's day,
126 stored in SYSTEMTIME format */
127 BOOL firstDaySet; /* first week day differs from locale defined */
129 BOOL isUnicode; /* value set with MCM_SETUNICODE format */
131 int monthRange;
132 MONTHDAYSTATE *monthdayState;
133 SYSTEMTIME todaysDate;
134 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
135 int status; /* See MC_SEL flags */
136 SYSTEMTIME firstSel; /* first selected day */
137 INT maxSelCount;
138 SYSTEMTIME minSel; /* contains single selection when used without MCS_MULTISELECT */
139 SYSTEMTIME maxSel;
140 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
141 DWORD rangeValid;
142 SYSTEMTIME minDate;
143 SYSTEMTIME maxDate;
145 RECT titlebtnnext; /* the `next month' button in the header */
146 RECT titlebtnprev; /* the `prev month' button in the header */
147 RECT todayrect; /* `today: xx/xx/xx' text rect */
148 HWND hwndNotify; /* Window to receive the notifications */
149 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
150 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
151 WNDPROC EditWndProc; /* original Edit window procedure */
153 CALENDAR_INFO *calendars;
154 SIZE dim; /* [cx,cy] - dimensions of calendars matrix, row/column count */
155 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
157 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
159 /* empty SYSTEMTIME const */
160 static const SYSTEMTIME st_null;
161 /* valid date limits */
162 static const SYSTEMTIME max_allowed_date = { .wYear = 9999, .wMonth = 12, .wDay = 31 };
163 static const SYSTEMTIME min_allowed_date = { .wYear = 1752, .wMonth = 9, .wDay = 14 };
165 /* Prev/Next buttons */
166 enum nav_direction
168 DIRECTION_BACKWARD,
169 DIRECTION_FORWARD
172 /* helper functions */
173 static inline INT MONTHCAL_GetCalCount(const MONTHCAL_INFO *infoPtr)
175 return infoPtr->dim.cx * infoPtr->dim.cy;
178 /* send a single MCN_SELCHANGE notification */
179 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
181 NMSELCHANGE nmsc;
183 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
184 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
185 nmsc.nmhdr.code = MCN_SELCHANGE;
186 nmsc.stSelStart = infoPtr->minSel;
187 nmsc.stSelEnd = infoPtr->maxSel;
188 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
191 /* send a single MCN_SELECT notification */
192 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
194 NMSELCHANGE nmsc;
196 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
197 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
198 nmsc.nmhdr.code = MCN_SELECT;
199 nmsc.stSelStart = infoPtr->minSel;
200 nmsc.stSelEnd = infoPtr->maxSel;
202 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
205 static inline int MONTHCAL_MonthDiff(const SYSTEMTIME *left, const SYSTEMTIME *right)
207 return (right->wYear - left->wYear)*12 + right->wMonth - left->wMonth;
210 /* returns the number of days in any given month, checking for leap days */
211 /* January is 1, December is 12 */
212 int MONTHCAL_MonthLength(int month, int year)
214 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
215 /* Wrap around, this eases handling. Getting length only we shouldn't care
216 about year change here cause January and December have
217 the same day quantity */
218 if(month == 0)
219 month = 12;
220 else if(month == 13)
221 month = 1;
223 /* special case for calendar transition year */
224 if(month == min_allowed_date.wMonth && year == min_allowed_date.wYear) return 19;
226 /* if we have a leap year add 1 day to February */
227 /* a leap year is a year either divisible by 400 */
228 /* or divisible by 4 and not by 100 */
229 if(month == 2) { /* February */
230 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
231 (year%4 == 0)) ? 1 : 0);
233 else {
234 return mdays[month - 1];
238 /* compares timestamps using date part only */
239 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
241 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
242 (first->wDay == second->wDay);
245 /* make sure that date fields are valid */
246 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
248 if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
249 if(time->wDayOfWeek > 6) return FALSE;
250 if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear))
251 return FALSE;
253 return TRUE;
256 /* Copies timestamp part only.
258 * PARAMETERS
260 * [I] from : source date
261 * [O] to : dest date
263 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
265 to->wHour = from->wHour;
266 to->wMinute = from->wMinute;
267 to->wSecond = from->wSecond;
270 /* Copies date part only.
272 * PARAMETERS
274 * [I] from : source date
275 * [O] to : dest date
277 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
279 to->wYear = from->wYear;
280 to->wMonth = from->wMonth;
281 to->wDay = from->wDay;
282 to->wDayOfWeek = from->wDayOfWeek;
285 /* Compares two dates in SYSTEMTIME format
287 * PARAMETERS
289 * [I] first : pointer to valid first date data to compare
290 * [I] second : pointer to valid second date data to compare
292 * RETURN VALUE
294 * -1 : first < second
295 * 0 : first == second
296 * 1 : first > second
298 * Note that no date validation performed, already validated values expected.
300 static LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
302 FILETIME ft_first, ft_second;
304 SystemTimeToFileTime(first, &ft_first);
305 SystemTimeToFileTime(second, &ft_second);
307 return CompareFileTime(&ft_first, &ft_second);
310 static LONG MONTHCAL_CompareMonths(const SYSTEMTIME *first, const SYSTEMTIME *second)
312 SYSTEMTIME st_first, st_second;
314 st_first = st_second = st_null;
315 MONTHCAL_CopyDate(first, &st_first);
316 MONTHCAL_CopyDate(second, &st_second);
317 st_first.wDay = st_second.wDay = 1;
319 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
322 static LONG MONTHCAL_CompareDate(const SYSTEMTIME *first, const SYSTEMTIME *second)
324 SYSTEMTIME st_first, st_second;
326 st_first = st_second = st_null;
327 MONTHCAL_CopyDate(first, &st_first);
328 MONTHCAL_CopyDate(second, &st_second);
330 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
333 /* Checks largest possible date range and configured one
335 * PARAMETERS
337 * [I] infoPtr : valid pointer to control data
338 * [I] date : pointer to valid date data to check
339 * [I] fix : make date fit valid range
341 * RETURN VALUE
343 * TRUE - date within largest and configured range
344 * FALSE - date is outside largest or configured range
346 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr,
347 SYSTEMTIME *date, BOOL fix)
349 const SYSTEMTIME *fix_st = NULL;
351 if(MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) {
352 fix_st = &max_allowed_date;
354 else if(MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1) {
355 fix_st = &min_allowed_date;
357 else if(infoPtr->rangeValid & GDTR_MAX) {
358 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
359 fix_st = &infoPtr->maxDate;
362 else if(infoPtr->rangeValid & GDTR_MIN) {
363 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
364 fix_st = &infoPtr->minDate;
368 if (fix && fix_st) {
369 date->wYear = fix_st->wYear;
370 date->wMonth = fix_st->wMonth;
373 return fix_st ? FALSE : TRUE;
376 /* Checks passed range width with configured maximum selection count
378 * PARAMETERS
380 * [I] infoPtr : valid pointer to control data
381 * [I] range0 : pointer to valid date data (requested bound)
382 * [I] range1 : pointer to valid date data (primary bound)
383 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
385 * Adjust value computed basing on primary bound and current maximum selection
386 * count. For simple range check (without adjusted value required) (range0, range1)
387 * relation means nothing.
389 * RETURN VALUE
391 * TRUE - range is shorter or equal to maximum
392 * FALSE - range is larger than maximum
394 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
395 const SYSTEMTIME *range0,
396 const SYSTEMTIME *range1,
397 SYSTEMTIME *adjust)
399 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
400 FILETIME ft_range0, ft_range1;
401 LONG cmp;
403 SystemTimeToFileTime(range0, &ft_range0);
404 SystemTimeToFileTime(range1, &ft_range1);
406 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
407 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
408 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
409 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
411 cmp = CompareFileTime(&ft_range0, &ft_range1);
413 if(cmp == 1)
414 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
415 else
416 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
418 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
420 if(adjust) {
421 if(cmp == 1)
422 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
423 else
424 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
426 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
427 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
428 FileTimeToSystemTime(&ft_range0, adjust);
431 return FALSE;
433 else return TRUE;
436 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
437 Milliseconds are intentionally not validated. */
438 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
440 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
441 return FALSE;
442 else
443 return TRUE;
446 /* Note:Depending on DST, this may be offset by a day.
447 Need to find out if we're on a DST place & adjust the clock accordingly.
448 Above function assumes we have a valid data.
449 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
450 0 = Sunday.
453 /* Returns the day in the week
455 * PARAMETERS
456 * [i] date : input date
457 * [I] inplace : set calculated value back to date structure
459 * RETURN VALUE
460 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
462 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
464 SYSTEMTIME st = st_null;
465 FILETIME ft;
467 MONTHCAL_CopyDate(date, &st);
469 SystemTimeToFileTime(&st, &ft);
470 FileTimeToSystemTime(&ft, &st);
472 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
474 return st.wDayOfWeek;
477 /* add/subtract 'months' from date */
478 static inline void MONTHCAL_GetMonth(SYSTEMTIME *date, INT months)
480 INT length, m = date->wMonth + months;
482 date->wYear += m > 0 ? (m - 1) / 12 : m / 12 - 1;
483 date->wMonth = m > 0 ? (m - 1) % 12 + 1 : 12 + m % 12;
484 /* fix moving from last day in a month */
485 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
486 if(date->wDay > length) date->wDay = length;
487 MONTHCAL_CalculateDayOfWeek(date, TRUE);
490 /* properly updates date to point on next month */
491 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
493 MONTHCAL_GetMonth(date, 1);
496 /* properly updates date to point on prev month */
497 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
499 MONTHCAL_GetMonth(date, -1);
502 /* Returns full date for a first currently visible day */
503 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
505 /* zero indexed calendar has the earliest date */
506 SYSTEMTIME st_first = infoPtr->calendars[0].month;
507 INT firstDay;
509 st_first.wDay = 1;
510 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
512 *date = infoPtr->calendars[0].month;
513 MONTHCAL_GetPrevMonth(date);
515 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
516 (infoPtr->firstDay - firstDay) % 7 + 1;
518 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
519 date->wDay -= 7;
521 /* fix day of week */
522 MONTHCAL_CalculateDayOfWeek(date, TRUE);
525 /* Returns full date for a last currently visible day */
526 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
528 /* the latest date is in latest calendar */
529 SYSTEMTIME st, *lt_month = &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
530 INT first_day;
532 *date = *lt_month;
533 st = *lt_month;
535 /* day of week of first day of current month */
536 st.wDay = 1;
537 first_day = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
539 MONTHCAL_GetNextMonth(date);
540 MONTHCAL_GetPrevMonth(&st);
542 /* last calendar starts with some date from previous month that not displayed */
543 st.wDay = MONTHCAL_MonthLength(st.wMonth, st.wYear) +
544 (infoPtr->firstDay - first_day) % 7 + 1;
545 if (st.wDay > MONTHCAL_MonthLength(st.wMonth, st.wYear)) st.wDay -= 7;
547 /* Use month length to get max day. 42 means max day count in calendar area */
548 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
549 MONTHCAL_MonthLength(lt_month->wMonth, lt_month->wYear);
551 /* fix day of week */
552 MONTHCAL_CalculateDayOfWeek(date, TRUE);
555 /* From a given point calculate the row, column and day in the calendar,
556 'day == 0' means the last day of the last month. */
557 static int MONTHCAL_GetDayFromPos(const MONTHCAL_INFO *infoPtr, POINT pt, INT calIdx)
559 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
560 int firstDay, col, row;
561 RECT client;
563 GetClientRect(infoPtr->hwndSelf, &client);
565 /* if the point is outside the x bounds of the window put it at the boundary */
566 if (pt.x > client.right) pt.x = client.right;
568 col = (pt.x - infoPtr->calendars[calIdx].days.left ) / infoPtr->width_increment;
569 row = (pt.y - infoPtr->calendars[calIdx].days.top ) / infoPtr->height_increment;
571 st.wDay = 1;
572 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
573 return col + 7 * row - firstDay;
576 /* Get day position for given date and calendar
578 * PARAMETERS
580 * [I] infoPtr : pointer to control data
581 * [I] date : date value
582 * [O] col : day column (zero based)
583 * [O] row : week column (zero based)
584 * [I] calIdx : calendar index
586 static void MONTHCAL_GetDayPos(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
587 INT *col, INT *row, INT calIdx)
589 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
590 INT first;
592 st.wDay = 1;
593 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
595 if (calIdx == 0 || calIdx == MONTHCAL_GetCalCount(infoPtr)-1) {
596 const SYSTEMTIME *cal = &infoPtr->calendars[calIdx].month;
597 LONG cmp = MONTHCAL_CompareMonths(date, &st);
599 /* previous month */
600 if (cmp == -1) {
601 *col = (first - MONTHCAL_MonthLength(date->wMonth, cal->wYear) + date->wDay) % 7;
602 *row = 0;
603 return;
606 /* next month calculation is same as for current, just add current month length */
607 if (cmp == 1)
608 first += MONTHCAL_MonthLength(cal->wMonth, cal->wYear);
611 *col = (date->wDay + first) % 7;
612 *row = (date->wDay + first - *col) / 7;
615 /* returns bounding box for day in given position in given calendar */
616 static inline void MONTHCAL_GetDayRectI(const MONTHCAL_INFO *infoPtr, RECT *r,
617 INT col, INT row, INT calIdx)
619 r->left = infoPtr->calendars[calIdx].days.left + col * infoPtr->width_increment;
620 r->right = r->left + infoPtr->width_increment;
621 r->top = infoPtr->calendars[calIdx].days.top + row * infoPtr->height_increment;
622 r->bottom = r->top + infoPtr->textHeight;
625 /* Returns bounding box for given date
627 * NOTE: when calendar index is unknown pass -1
629 static inline void MONTHCAL_GetDayRect(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
630 RECT *r, INT calIdx)
632 INT col, row;
634 if (calIdx == -1)
636 INT cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[0].month);
638 if (cmp <= 0)
639 calIdx = 0;
640 else
642 cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month);
643 if (cmp >= 0)
644 calIdx = MONTHCAL_GetCalCount(infoPtr)-1;
645 else
647 for (calIdx = 1; calIdx < MONTHCAL_GetCalCount(infoPtr)-1; calIdx++)
648 if (MONTHCAL_CompareMonths(date, &infoPtr->calendars[calIdx].month) == 0)
649 break;
654 MONTHCAL_GetDayPos(infoPtr, date, &col, &row, calIdx);
655 MONTHCAL_GetDayRectI(infoPtr, r, col, row, calIdx);
658 /* Focused day helper:
660 - set focused date to given value;
661 - reset to zero value if NULL passed;
662 - invalidate previous and new day rectangle only if needed.
664 Returns TRUE if focused day changed, FALSE otherwise.
666 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
668 RECT r;
670 if(st)
672 /* there's nothing to do if it's the same date,
673 mouse move within same date rectangle case */
674 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
676 /* invalidate old focused day */
677 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
678 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
680 infoPtr->focusedSel = *st;
683 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
685 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
686 infoPtr->focusedSel = st_null;
688 /* on set invalidates new day, on reset clears previous focused day */
689 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
691 return TRUE;
694 /* draw today boundary box for specified rectangle */
695 static void MONTHCAL_Circle(const MONTHCAL_INFO *infoPtr, HDC hdc, const RECT *r)
697 HPEN old_pen = SelectObject(hdc, infoPtr->pens[PenRed]);
698 HBRUSH old_brush;
700 old_brush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
701 Rectangle(hdc, r->left, r->top, r->right, r->bottom);
703 SelectObject(hdc, old_brush);
704 SelectObject(hdc, old_pen);
707 /* Draw today day mark rectangle
709 * [I] hdc : context to draw in
710 * [I] date : day to mark with rectangle
713 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
714 const SYSTEMTIME *date)
716 RECT r;
718 MONTHCAL_GetDayRect(infoPtr, date, &r, -1);
719 MONTHCAL_Circle(infoPtr, hdc, &r);
722 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
723 int bold, const PAINTSTRUCT *ps)
725 static const WCHAR fmtW[] = { '%','d',0 };
726 WCHAR buf[10];
727 RECT r, r_temp;
728 COLORREF oldCol = 0;
729 COLORREF oldBk = 0;
730 INT old_bkmode, selection;
732 /* no need to check styles: when selection is not valid, it is set to zero.
733 1 < day < 31, so everything is OK */
734 MONTHCAL_GetDayRect(infoPtr, st, &r, -1);
735 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
737 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
738 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0))
740 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
741 TRACE("%s\n", wine_dbgstr_rect(&r));
742 oldCol = SetTextColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
743 oldBk = SetBkColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
744 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
746 selection = 1;
748 else
749 selection = 0;
751 SelectObject(hdc, bold ? infoPtr->hBoldFont : infoPtr->hFont);
753 old_bkmode = SetBkMode(hdc, TRANSPARENT);
754 wsprintfW(buf, fmtW, st->wDay);
755 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
756 SetBkMode(hdc, old_bkmode);
758 if (selection)
760 SetTextColor(hdc, oldCol);
761 SetBkColor(hdc, oldBk);
765 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, enum nav_direction button)
767 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
768 RECT *r = button == DIRECTION_FORWARD ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
769 BOOL pressed = button == DIRECTION_FORWARD ? infoPtr->status & MC_NEXTPRESSED :
770 infoPtr->status & MC_PREVPRESSED;
771 if (theme)
773 static const int states[] = {
774 /* Prev button */
775 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
776 /* Next button */
777 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
779 int stateNum = button == DIRECTION_FORWARD ? 3 : 0;
780 if (pressed)
781 stateNum += 1;
782 else
784 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
786 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
788 else
790 int style = button == DIRECTION_FORWARD ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
791 if (pressed)
792 style |= DFCS_PUSHED;
793 else
795 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
798 DrawFrameControl(hdc, r, DFC_SCROLL, style);
802 /* paint a title with buttons and month/year string */
803 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
805 static const WCHAR fmt_monthW[] = { '%','s',' ','%','l','d',0 };
806 RECT *title = &infoPtr->calendars[calIdx].title;
807 const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
808 WCHAR buf_month[80], buf_fmt[80];
809 SIZE sz;
811 /* fill header box */
812 FillRect(hdc, title, infoPtr->brushes[BrushTitle]);
814 /* month/year string */
815 SetBkColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
816 SetTextColor(hdc, infoPtr->colors[MCSC_TITLETEXT]);
817 SelectObject(hdc, infoPtr->hBoldFont);
819 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1 + st->wMonth - 1,
820 buf_month, countof(buf_month));
822 wsprintfW(buf_fmt, fmt_monthW, buf_month, st->wYear);
823 DrawTextW(hdc, buf_fmt, strlenW(buf_fmt), title,
824 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
826 /* update title rectangles with current month - used while testing hits */
827 GetTextExtentPoint32W(hdc, buf_fmt, strlenW(buf_fmt), &sz);
828 infoPtr->calendars[calIdx].titlemonth.left = title->right / 2 + title->left / 2 - sz.cx / 2;
829 infoPtr->calendars[calIdx].titleyear.right = title->right / 2 + title->left / 2 + sz.cx / 2;
831 GetTextExtentPoint32W(hdc, buf_month, strlenW(buf_month), &sz);
832 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
833 infoPtr->calendars[calIdx].titleyear.left = infoPtr->calendars[calIdx].titlemonth.right;
836 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
838 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
839 static const WCHAR fmt_weekW[] = { '%','d',0 };
840 INT mindays, weeknum, weeknum1, startofprescal;
841 INT i, prev_month;
842 SYSTEMTIME st;
843 WCHAR buf[80];
844 HPEN old_pen;
845 RECT r;
847 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
849 MONTHCAL_GetMinDate(infoPtr, &st);
850 startofprescal = st.wDay;
851 st = *date;
853 prev_month = date->wMonth - 1;
854 if(prev_month == 0) prev_month = 12;
857 Rules what week to call the first week of a new year:
858 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
859 The week containing Jan 1 is the first week of year
860 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
861 First week of year must contain 4 days of the new year
862 LOCALE_IFIRSTWEEKOFYEAR == 1 (what countries?)
863 The first week of the year must contain only days of the new year
865 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
866 weeknum = atoiW(buf);
867 switch (weeknum)
869 case 1: mindays = 6;
870 break;
871 case 2: mindays = 3;
872 break;
873 case 0: mindays = 0;
874 break;
875 default:
876 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
877 mindays = 0;
880 if (date->wMonth == 1)
882 /* calculate all those exceptions for January */
883 st.wDay = st.wMonth = 1;
884 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
885 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
886 weeknum = 1;
887 else
889 weeknum = 0;
890 for(i = 0; i < 11; i++)
891 weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
893 weeknum += startofprescal + 7;
894 weeknum /= 7;
895 st.wYear -= 1;
896 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
897 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
900 else
902 weeknum = 0;
903 for(i = 0; i < prev_month - 1; i++)
904 weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
906 weeknum += startofprescal + 7;
907 weeknum /= 7;
908 st.wDay = st.wMonth = 1;
909 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
910 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
913 r = infoPtr->calendars[calIdx].weeknums;
915 /* erase whole week numbers area */
916 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
917 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
919 /* reduce rectangle to one week number */
920 r.bottom = r.top + infoPtr->height_increment;
922 for(i = 0; i < 6; i++) {
923 if((i == 0) && (weeknum > 50))
925 wsprintfW(buf, fmt_weekW, weeknum);
926 weeknum = 0;
928 else if((i == 5) && (weeknum > 47))
930 wsprintfW(buf, fmt_weekW, 1);
932 else
933 wsprintfW(buf, fmt_weekW, weeknum + i);
935 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
936 OffsetRect(&r, 0, infoPtr->height_increment);
939 /* line separator for week numbers column */
940 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
941 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
942 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
943 SelectObject(hdc, old_pen);
946 /* bottom today date */
947 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
949 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
950 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
951 RECT text_rect, box_rect;
952 HFONT old_font;
953 INT col;
955 if(infoPtr->dwStyle & MCS_NOTODAY) return;
957 if (!LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, countof(buf_todayW)))
959 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
960 WARN("Can't load resource\n");
961 strcpyW(buf_todayW, todayW);
964 col = infoPtr->dwStyle & MCS_NOTODAYCIRCLE ? 0 : 1;
965 if (infoPtr->dwStyle & MCS_WEEKNUMBERS) col--;
966 /* label is located below first calendar last row */
967 MONTHCAL_GetDayRectI(infoPtr, &text_rect, col, 6, infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx);
968 box_rect = text_rect;
970 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
971 buf_dateW, countof(buf_dateW));
972 old_font = SelectObject(hdc, infoPtr->hBoldFont);
973 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
975 wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
976 DrawTextW(hdc, buf, -1, &text_rect, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
977 DrawTextW(hdc, buf, -1, &text_rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
979 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
980 OffsetRect(&box_rect, -infoPtr->width_increment, 0);
981 MONTHCAL_Circle(infoPtr, hdc, &box_rect);
984 SelectObject(hdc, old_font);
987 /* today mark + focus */
988 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
990 if((infoPtr->minSel.wMonth == infoPtr->todaysDate.wMonth) &&
991 (infoPtr->minSel.wYear == infoPtr->todaysDate.wYear) &&
992 !(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
994 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
997 if(!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
999 RECT r;
1000 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
1001 DrawFocusRect(hdc, &r);
1005 /* months before first calendar month and after last calendar month */
1006 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1008 INT mask, length;
1009 SYSTEMTIME st_max, st;
1011 if (infoPtr->dwStyle & MCS_NOTRAILINGDATES) return;
1013 SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
1015 /* draw prev month */
1016 MONTHCAL_GetMinDate(infoPtr, &st);
1017 mask = 1 << (st.wDay-1);
1018 /* December and January both 31 days long, so no worries if wrapped */
1019 length = MONTHCAL_MonthLength(infoPtr->calendars[0].month.wMonth - 1,
1020 infoPtr->calendars[0].month.wYear);
1021 while(st.wDay <= length)
1023 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[0] & mask, ps);
1024 mask <<= 1;
1025 st.wDay++;
1028 /* draw next month */
1029 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1030 st.wDay = 1;
1031 MONTHCAL_GetNextMonth(&st);
1032 MONTHCAL_GetMaxDate(infoPtr, &st_max);
1033 mask = 1;
1035 while(st.wDay <= st_max.wDay)
1037 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[2] & mask, ps);
1038 mask <<= 1;
1039 st.wDay++;
1043 /* paint a calendar area */
1044 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
1046 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
1047 INT i, j, length;
1048 RECT r, fill_bk_rect;
1049 SYSTEMTIME st;
1050 WCHAR buf[80];
1051 HPEN old_pen;
1052 int mask;
1054 /* fill whole days area - from week days area to today note rectangle */
1055 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
1056 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
1057 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
1059 FillRect(hdc, &fill_bk_rect, infoPtr->brushes[BrushMonth]);
1061 /* draw line under day abbreviations */
1062 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1063 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
1064 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
1065 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
1066 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
1067 SelectObject(hdc, old_pen);
1069 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
1070 infoPtr->calendars[calIdx].weeknums.right;
1072 /* draw day abbreviations */
1073 SelectObject(hdc, infoPtr->hFont);
1074 SetBkColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
1075 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1076 /* rectangle to draw a single day abbreviation within */
1077 r = infoPtr->calendars[calIdx].wdays;
1078 r.right = r.left + infoPtr->width_increment;
1080 i = infoPtr->firstDay;
1081 for(j = 0; j < 7; j++) {
1082 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
1083 DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1084 OffsetRect(&r, infoPtr->width_increment, 0);
1087 /* draw current month */
1088 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1089 st = *date;
1090 st.wDay = 1;
1091 mask = 1;
1092 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1093 while(st.wDay <= length)
1095 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[1] & mask, ps);
1096 mask <<= 1;
1097 st.wDay++;
1101 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1103 COLORREF old_text_clr, old_bk_clr;
1104 HFONT old_font;
1105 INT i;
1107 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1108 old_bk_clr = GetBkColor(hdc);
1109 old_font = GetCurrentObject(hdc, OBJ_FONT);
1111 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1113 RECT *title = &infoPtr->calendars[i].title;
1114 RECT r;
1116 /* draw title, redraw all its elements */
1117 if (IntersectRect(&r, &(ps->rcPaint), title))
1118 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1120 /* draw calendar area */
1121 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1122 if (IntersectRect(&r, &(ps->rcPaint), &r))
1123 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1125 /* week numbers */
1126 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1129 /* partially visible months */
1130 MONTHCAL_PaintLeadTrailMonths(infoPtr, hdc, ps);
1132 /* focus and today rectangle */
1133 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1135 /* today at the bottom left */
1136 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1138 /* navigation buttons */
1139 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_BACKWARD);
1140 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_FORWARD);
1142 /* restore context */
1143 SetBkColor(hdc, old_bk_clr);
1144 SelectObject(hdc, old_font);
1145 SetTextColor(hdc, old_text_clr);
1148 static LRESULT
1149 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, RECT *rect)
1151 TRACE("rect %p\n", rect);
1153 if(!rect) return FALSE;
1155 *rect = infoPtr->calendars[0].title;
1156 rect->bottom = infoPtr->calendars[0].days.bottom + infoPtr->todayrect.bottom -
1157 infoPtr->todayrect.top;
1159 AdjustWindowRect(rect, infoPtr->dwStyle, FALSE);
1161 /* minimal rectangle is zero based */
1162 OffsetRect(rect, -rect->left, -rect->top);
1164 TRACE("%s\n", wine_dbgstr_rect(rect));
1166 return TRUE;
1169 static COLORREF
1170 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, UINT index)
1172 TRACE("%p, %d\n", infoPtr, index);
1174 if (index > MCSC_TRAILINGTEXT) return -1;
1175 return infoPtr->colors[index];
1178 static LRESULT
1179 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, UINT index, COLORREF color)
1181 enum CachedBrush type;
1182 COLORREF prev;
1184 TRACE("%p, %d: color %08x\n", infoPtr, index, color);
1186 if (index > MCSC_TRAILINGTEXT) return -1;
1188 prev = infoPtr->colors[index];
1189 infoPtr->colors[index] = color;
1191 /* update cached brush */
1192 switch (index)
1194 case MCSC_BACKGROUND:
1195 type = BrushBackground;
1196 break;
1197 case MCSC_TITLEBK:
1198 type = BrushTitle;
1199 break;
1200 case MCSC_MONTHBK:
1201 type = BrushMonth;
1202 break;
1203 default:
1204 type = BrushLast;
1207 if (type != BrushLast)
1209 DeleteObject(infoPtr->brushes[type]);
1210 infoPtr->brushes[type] = CreateSolidBrush(color);
1213 /* update cached pen */
1214 if (index == MCSC_TEXT)
1216 DeleteObject(infoPtr->pens[PenText]);
1217 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[index]);
1220 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND ? TRUE : FALSE);
1221 return prev;
1224 static LRESULT
1225 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1227 TRACE("\n");
1229 if(infoPtr->delta)
1230 return infoPtr->delta;
1231 else
1232 return infoPtr->visible;
1236 static LRESULT
1237 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1239 INT prev = infoPtr->delta;
1241 TRACE("delta %d\n", delta);
1243 infoPtr->delta = delta;
1244 return prev;
1248 static inline LRESULT
1249 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1251 int day;
1253 /* convert from SYSTEMTIME to locale format */
1254 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1256 return MAKELONG(day, infoPtr->firstDaySet);
1260 /* Sets the first day of the week that will appear in the control
1263 * PARAMETERS:
1264 * [I] infoPtr : valid pointer to control data
1265 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1268 * RETURN VALUE:
1269 * Low word contains previous first day,
1270 * high word indicates was first day forced with this message before or is
1271 * locale defined (TRUE - was forced, FALSE - wasn't).
1273 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1274 * FIXME: we need more error checking here
1276 static LRESULT
1277 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1279 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1280 int new_day;
1282 TRACE("%d\n", day);
1284 if(day == -1)
1286 WCHAR buf[80];
1288 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1289 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1291 new_day = atoiW(buf);
1293 infoPtr->firstDaySet = FALSE;
1295 else if(day >= 7)
1297 new_day = 6; /* max first day allowed */
1298 infoPtr->firstDaySet = TRUE;
1300 else
1302 /* Native behaviour for that case is broken: invalid date number >31
1303 got displayed at (0,0) position, current month starts always from
1304 (1,0) position. Should be implemented here as well only if there's
1305 nothing else to do. */
1306 if (day < -1)
1307 FIXME("No bug compatibility for day=%d\n", day);
1309 new_day = day;
1310 infoPtr->firstDaySet = TRUE;
1313 /* convert from locale to SYSTEMTIME format */
1314 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1316 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1318 return prev;
1322 static LRESULT
1323 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
1325 TRACE("flag=%d, st=%p\n", flag, st);
1327 if(st)
1329 switch (flag) {
1330 case GMR_VISIBLE:
1332 st[0] = infoPtr->calendars[0].month;
1333 st[1] = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1335 if (st[0].wMonth == min_allowed_date.wMonth &&
1336 st[0].wYear == min_allowed_date.wYear)
1338 st[0].wDay = min_allowed_date.wDay;
1340 else
1341 st[0].wDay = 1;
1342 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
1344 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
1345 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
1347 return MONTHCAL_GetCalCount(infoPtr);
1349 case GMR_DAYSTATE:
1351 MONTHCAL_GetMinDate(infoPtr, &st[0]);
1352 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
1353 break;
1355 default:
1356 WARN("Unknown flag value, got %d\n", flag);
1360 return infoPtr->monthRange;
1364 static LRESULT
1365 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1367 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1371 static LRESULT
1372 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1374 FILETIME ft_min, ft_max;
1376 TRACE("%x %p\n", limits, range);
1378 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1379 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1380 return FALSE;
1382 if (limits & GDTR_MIN)
1384 if (!MONTHCAL_ValidateTime(&range[0]))
1385 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1387 infoPtr->minDate = range[0];
1388 infoPtr->rangeValid |= GDTR_MIN;
1390 if (limits & GDTR_MAX)
1392 if (!MONTHCAL_ValidateTime(&range[1]))
1393 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1395 infoPtr->maxDate = range[1];
1396 infoPtr->rangeValid |= GDTR_MAX;
1399 /* Only one limit set - we are done */
1400 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1401 return TRUE;
1403 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1404 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1406 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1408 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1410 /* Native swaps limits only when both limits are being set. */
1411 SYSTEMTIME st_tmp = infoPtr->minDate;
1412 infoPtr->minDate = infoPtr->maxDate;
1413 infoPtr->maxDate = st_tmp;
1415 else
1417 /* reset the other limit */
1418 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1419 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1420 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1424 return TRUE;
1428 static LRESULT
1429 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1431 TRACE("%p\n", range);
1433 if(!range) return FALSE;
1435 range[1] = infoPtr->maxDate;
1436 range[0] = infoPtr->minDate;
1438 return infoPtr->rangeValid;
1442 static LRESULT
1443 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1445 TRACE("%p %d %p\n", infoPtr, months, states);
1446 if(months != infoPtr->monthRange) return 0;
1448 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1450 return 1;
1453 static LRESULT
1454 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1456 TRACE("%p\n", curSel);
1457 if(!curSel) return FALSE;
1458 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1460 *curSel = infoPtr->minSel;
1461 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1462 return TRUE;
1465 static LRESULT
1466 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1468 SYSTEMTIME prev = infoPtr->minSel;
1469 INT diff;
1470 WORD day;
1472 TRACE("%p\n", curSel);
1473 if(!curSel) return FALSE;
1474 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1476 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1477 /* exit earlier if selection equals current */
1478 if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1480 if(!MONTHCAL_IsDateInValidRange(infoPtr, curSel, FALSE)) return FALSE;
1482 /* scroll calendars only if we have to */
1483 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, curSel);
1484 if (diff <= 0)
1486 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, curSel);
1487 if (diff > 0) diff = 0;
1490 if (diff != 0)
1492 INT i;
1494 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1495 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1498 infoPtr->minSel = *curSel;
1499 infoPtr->maxSel = *curSel;
1501 /* if selection is still in current month, reduce rectangle */
1502 day = prev.wDay;
1503 prev.wDay = curSel->wDay;
1504 if (MONTHCAL_IsDateEqual(&prev, curSel))
1506 RECT r_prev, r_new;
1508 prev.wDay = day;
1509 MONTHCAL_GetDayRect(infoPtr, &prev, &r_prev, -1);
1510 MONTHCAL_GetDayRect(infoPtr, curSel, &r_new, -1);
1512 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1513 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1515 else
1516 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1518 return TRUE;
1522 static LRESULT
1523 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1525 return infoPtr->maxSelCount;
1529 static LRESULT
1530 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1532 TRACE("%d\n", max);
1534 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1535 if(max <= 0) return FALSE;
1537 infoPtr->maxSelCount = max;
1539 return TRUE;
1543 static LRESULT
1544 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1546 TRACE("%p\n", range);
1548 if(!range) return FALSE;
1550 if(infoPtr->dwStyle & MCS_MULTISELECT)
1552 range[1] = infoPtr->maxSel;
1553 range[0] = infoPtr->minSel;
1554 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1555 return TRUE;
1558 return FALSE;
1562 static LRESULT
1563 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1565 SYSTEMTIME old_range[2];
1566 INT diff;
1568 TRACE("%p\n", range);
1570 if(!range || !(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1572 /* adjust timestamps */
1573 if(!MONTHCAL_ValidateTime(&range[0])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1574 if(!MONTHCAL_ValidateTime(&range[1])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1576 /* maximum range exceeded */
1577 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1579 old_range[0] = infoPtr->minSel;
1580 old_range[1] = infoPtr->maxSel;
1582 /* swap if min > max */
1583 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1585 infoPtr->minSel = range[0];
1586 infoPtr->maxSel = range[1];
1588 else
1590 infoPtr->minSel = range[1];
1591 infoPtr->maxSel = range[0];
1594 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, &infoPtr->maxSel);
1595 if (diff < 0)
1597 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, &infoPtr->maxSel);
1598 if (diff > 0) diff = 0;
1601 if (diff != 0)
1603 INT i;
1605 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1606 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1609 /* update day of week */
1610 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1611 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1613 /* redraw if bounds changed */
1614 /* FIXME: no actual need to redraw everything */
1615 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1616 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1618 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1621 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1622 return TRUE;
1626 static LRESULT
1627 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1629 TRACE("%p\n", today);
1631 if(!today) return FALSE;
1632 *today = infoPtr->todaysDate;
1633 return TRUE;
1636 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1638 * RETURN VALUE
1640 * TRUE - today date changed
1641 * FALSE - today date isn't changed
1643 static BOOL
1644 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1646 RECT new_r, old_r;
1648 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1650 MONTHCAL_GetDayRect(infoPtr, &infoPtr->todaysDate, &old_r, -1);
1651 MONTHCAL_GetDayRect(infoPtr, today, &new_r, -1);
1653 infoPtr->todaysDate = *today;
1655 /* only two days need redrawing */
1656 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1657 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1658 return TRUE;
1661 /* MCM_SETTODAT handler */
1662 static LRESULT
1663 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1665 TRACE("%p\n", today);
1667 if(!today) return FALSE;
1669 /* remember if date was set successfully */
1670 if(MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1672 return TRUE;
1675 /* returns calendar index containing specified point, or -1 if it's background */
1676 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1678 RECT r;
1679 INT i;
1681 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1683 /* whole bounding rectangle allows some optimization to compute */
1684 r.left = infoPtr->calendars[i].title.left;
1685 r.top = infoPtr->calendars[i].title.top;
1686 r.bottom = infoPtr->calendars[i].days.bottom;
1687 r.right = infoPtr->calendars[i].days.right;
1689 if (PtInRect(&r, *pt)) return i;
1692 return -1;
1695 static inline UINT fill_hittest_info(const MCHITTESTINFO *src, MCHITTESTINFO *dest)
1697 dest->uHit = src->uHit;
1698 dest->st = src->st;
1700 if (dest->cbSize == sizeof(MCHITTESTINFO))
1701 memcpy(&dest->rc, &src->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1703 return src->uHit;
1706 static LRESULT
1707 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1709 MCHITTESTINFO htinfo;
1710 SYSTEMTIME *ht_month;
1711 INT day, calIdx;
1713 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1715 htinfo.st = st_null;
1717 /* we should preserve passed fields if hit area doesn't need them */
1718 if (lpht->cbSize == sizeof(MCHITTESTINFO))
1719 memcpy(&htinfo.rc, &lpht->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1721 /* Comment in for debugging...
1722 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,
1723 infoPtr->wdays.left, infoPtr->wdays.right,
1724 infoPtr->wdays.top, infoPtr->wdays.bottom,
1725 infoPtr->days.left, infoPtr->days.right,
1726 infoPtr->days.top, infoPtr->days.bottom,
1727 infoPtr->todayrect.left, infoPtr->todayrect.right,
1728 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1729 infoPtr->weeknums.left, infoPtr->weeknums.right,
1730 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1733 /* guess in what calendar we are */
1734 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1735 if (calIdx == -1)
1737 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1739 htinfo.uHit = MCHT_TODAYLINK;
1740 htinfo.rc = infoPtr->todayrect;
1742 else
1743 /* outside of calendar area? What's left must be background :-) */
1744 htinfo.uHit = MCHT_CALENDARBK;
1746 return fill_hittest_info(&htinfo, lpht);
1749 /* are we in the header? */
1750 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1751 /* FIXME: buttons hittesting could be optimized cause maximum
1752 two calendars have buttons */
1753 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1755 htinfo.uHit = MCHT_TITLEBTNPREV;
1756 htinfo.rc = infoPtr->titlebtnprev;
1758 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1760 htinfo.uHit = MCHT_TITLEBTNNEXT;
1761 htinfo.rc = infoPtr->titlebtnnext;
1763 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1765 htinfo.uHit = MCHT_TITLEMONTH;
1766 htinfo.rc = infoPtr->calendars[calIdx].titlemonth;
1767 htinfo.iOffset = calIdx;
1769 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1771 htinfo.uHit = MCHT_TITLEYEAR;
1772 htinfo.rc = infoPtr->calendars[calIdx].titleyear;
1773 htinfo.iOffset = calIdx;
1775 else
1777 htinfo.uHit = MCHT_TITLE;
1778 htinfo.rc = infoPtr->calendars[calIdx].title;
1779 htinfo.iOffset = calIdx;
1782 return fill_hittest_info(&htinfo, lpht);
1785 ht_month = &infoPtr->calendars[calIdx].month;
1786 /* days area (including week days and week numbers) */
1787 day = MONTHCAL_GetDayFromPos(infoPtr, lpht->pt, calIdx);
1788 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1790 htinfo.uHit = MCHT_CALENDARDAY;
1791 htinfo.iOffset = calIdx;
1792 htinfo.st.wYear = ht_month->wYear;
1793 htinfo.st.wMonth = (day < 1) ? ht_month->wMonth -1 : ht_month->wMonth;
1794 htinfo.st.wDay = (day < 1) ?
1795 MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day : day;
1797 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1799 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1801 htinfo.uHit = MCHT_CALENDARWEEKNUM;
1802 htinfo.st.wYear = ht_month->wYear;
1803 htinfo.iOffset = calIdx;
1805 if (day < 1)
1807 htinfo.st.wMonth = ht_month->wMonth - 1;
1808 htinfo.st.wDay = MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day;
1810 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1812 htinfo.st.wMonth = ht_month->wMonth + 1;
1813 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1815 else
1817 htinfo.st.wMonth = ht_month->wMonth;
1818 htinfo.st.wDay = day;
1821 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1823 htinfo.iOffset = calIdx;
1824 htinfo.st.wYear = ht_month->wYear;
1825 htinfo.st.wMonth = ht_month->wMonth;
1826 /* previous month only valid for first calendar */
1827 if (day < 1 && calIdx == 0)
1829 htinfo.uHit = MCHT_CALENDARDATEPREV;
1830 MONTHCAL_GetPrevMonth(&htinfo.st);
1831 htinfo.st.wDay = MONTHCAL_MonthLength(htinfo.st.wMonth, htinfo.st.wYear) + day;
1833 /* next month only valid for last calendar */
1834 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear) &&
1835 calIdx == MONTHCAL_GetCalCount(infoPtr)-1)
1837 htinfo.uHit = MCHT_CALENDARDATENEXT;
1838 MONTHCAL_GetNextMonth(&htinfo.st);
1839 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1841 /* multiple calendars case - blank areas for previous/next month */
1842 else if (day < 1 || day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1844 htinfo.uHit = MCHT_CALENDARBK;
1846 else
1848 htinfo.uHit = MCHT_CALENDARDATE;
1849 htinfo.st.wDay = day;
1852 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1853 MONTHCAL_GetDayRectI(infoPtr, &htinfo.rc, htinfo.iCol, htinfo.iRow, calIdx);
1854 /* always update day of week */
1855 MONTHCAL_CalculateDayOfWeek(&htinfo.st, TRUE);
1858 return fill_hittest_info(&htinfo, lpht);
1861 /* MCN_GETDAYSTATE notification helper */
1862 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1864 if(infoPtr->dwStyle & MCS_DAYSTATE) {
1865 NMDAYSTATE nmds;
1867 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1868 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1869 nmds.nmhdr.code = MCN_GETDAYSTATE;
1870 nmds.cDayState = infoPtr->monthRange;
1871 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1873 nmds.stStart = infoPtr->todaysDate;
1874 nmds.stStart.wYear = infoPtr->minSel.wYear;
1875 nmds.stStart.wMonth = infoPtr->minSel.wMonth;
1876 nmds.stStart.wDay = 1;
1878 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1879 memcpy(infoPtr->monthdayState, nmds.prgDayState, infoPtr->monthRange*sizeof(MONTHDAYSTATE));
1881 Free(nmds.prgDayState);
1885 /* no valid range check performed */
1886 static void MONTHCAL_Scroll(MONTHCAL_INFO *infoPtr, INT delta)
1888 INT i, selIdx = -1;
1890 for(i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1892 /* save selection position to shift it later */
1893 if (selIdx == -1 && MONTHCAL_CompareMonths(&infoPtr->minSel, &infoPtr->calendars[i].month) == 0)
1894 selIdx = i;
1896 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, delta);
1899 /* selection is always shifted to first calendar */
1900 if(infoPtr->dwStyle & MCS_MULTISELECT)
1902 SYSTEMTIME range[2];
1904 MONTHCAL_GetSelRange(infoPtr, range);
1905 MONTHCAL_GetMonth(&range[0], delta - selIdx);
1906 MONTHCAL_GetMonth(&range[1], delta - selIdx);
1907 MONTHCAL_SetSelRange(infoPtr, range);
1909 else
1911 SYSTEMTIME st = infoPtr->minSel;
1913 MONTHCAL_GetMonth(&st, delta - selIdx);
1914 MONTHCAL_SetCurSel(infoPtr, &st);
1918 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
1920 INT delta = infoPtr->delta ? infoPtr->delta : MONTHCAL_GetCalCount(infoPtr);
1921 SYSTEMTIME st;
1923 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
1925 /* check if change allowed by range set */
1926 if(direction == DIRECTION_BACKWARD)
1928 st = infoPtr->calendars[0].month;
1929 MONTHCAL_GetMonth(&st, -delta);
1931 else
1933 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1934 MONTHCAL_GetMonth(&st, delta);
1937 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
1939 MONTHCAL_Scroll(infoPtr, direction == DIRECTION_BACKWARD ? -delta : delta);
1940 MONTHCAL_NotifyDayState(infoPtr);
1941 MONTHCAL_NotifySelectionChange(infoPtr);
1944 static LRESULT
1945 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1947 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1948 HMENU hMenu;
1949 POINT menupoint;
1950 WCHAR buf[32];
1952 hMenu = CreatePopupMenu();
1953 if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1955 WARN("Can't load resource\n");
1956 strcpyW(buf, todayW);
1958 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1959 menupoint.x = (short)LOWORD(lParam);
1960 menupoint.y = (short)HIWORD(lParam);
1961 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1962 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
1963 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1965 infoPtr->calendars[0].month = infoPtr->todaysDate;
1966 infoPtr->minSel = infoPtr->todaysDate;
1967 infoPtr->maxSel = infoPtr->todaysDate;
1968 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1971 return 0;
1974 /***
1975 * DESCRIPTION:
1976 * Subclassed edit control windproc function
1978 * PARAMETER(S):
1979 * [I] hwnd : the edit window handle
1980 * [I] uMsg : the message that is to be processed
1981 * [I] wParam : first message parameter
1982 * [I] lParam : second message parameter
1985 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1987 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
1989 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
1990 hwnd, uMsg, wParam, lParam);
1992 switch (uMsg)
1994 case WM_GETDLGCODE:
1995 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
1997 case WM_DESTROY:
1999 WNDPROC editProc = infoPtr->EditWndProc;
2000 infoPtr->EditWndProc = NULL;
2001 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
2002 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
2005 case WM_KILLFOCUS:
2006 break;
2008 case WM_KEYDOWN:
2009 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
2010 break;
2012 default:
2013 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
2016 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
2017 SendMessageW(hwnd, WM_CLOSE, 0, 0);
2018 return 0;
2021 /* creates updown control and edit box */
2022 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr, INT calIdx)
2024 RECT *rc = &infoPtr->calendars[calIdx].titleyear;
2025 RECT *title = &infoPtr->calendars[calIdx].title;
2027 infoPtr->hWndYearEdit =
2028 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
2029 rc->left + 3, (title->bottom + title->top - infoPtr->textHeight) / 2,
2030 rc->right - rc->left + 4,
2031 infoPtr->textHeight, infoPtr->hwndSelf,
2032 NULL, NULL, NULL);
2034 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
2036 infoPtr->hWndYearUpDown =
2037 CreateWindowExW(0, UPDOWN_CLASSW, 0,
2038 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
2039 rc->right + 7, (title->bottom + title->top - infoPtr->textHeight) / 2,
2040 18, infoPtr->textHeight, infoPtr->hwndSelf,
2041 NULL, NULL, NULL);
2043 /* attach edit box */
2044 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
2045 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
2046 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
2047 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->calendars[calIdx].month.wYear);
2049 /* subclass edit box */
2050 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
2051 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
2053 SetFocus(infoPtr->hWndYearEdit);
2056 static LRESULT
2057 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2059 MCHITTESTINFO ht;
2060 DWORD hit;
2062 /* Actually we don't need input focus for calendar, this is used to kill
2063 year updown and its buddy edit box */
2064 if (IsWindow(infoPtr->hWndYearUpDown))
2066 SetFocus(infoPtr->hwndSelf);
2067 return 0;
2070 SetCapture(infoPtr->hwndSelf);
2072 ht.cbSize = sizeof(MCHITTESTINFO);
2073 ht.pt.x = (short)LOWORD(lParam);
2074 ht.pt.y = (short)HIWORD(lParam);
2076 hit = MONTHCAL_HitTest(infoPtr, &ht);
2078 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
2080 switch(hit)
2082 case MCHT_TITLEBTNNEXT:
2083 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2084 infoPtr->status = MC_NEXTPRESSED;
2085 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2086 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2087 return 0;
2089 case MCHT_TITLEBTNPREV:
2090 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2091 infoPtr->status = MC_PREVPRESSED;
2092 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2093 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2094 return 0;
2096 case MCHT_TITLEMONTH:
2098 HMENU hMenu = CreatePopupMenu();
2099 WCHAR buf[32];
2100 POINT menupoint;
2101 INT i;
2103 for (i = 0; i < 12; i++)
2105 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
2106 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
2108 menupoint.x = ht.pt.x;
2109 menupoint.y = ht.pt.y;
2110 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2111 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
2112 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
2114 if ((i > 0) && (i < 13) && infoPtr->calendars[ht.iOffset].month.wMonth != i)
2116 INT delta = i - infoPtr->calendars[ht.iOffset].month.wMonth;
2117 SYSTEMTIME st;
2119 /* check if change allowed by range set */
2120 st = delta < 0 ? infoPtr->calendars[0].month :
2121 infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2122 MONTHCAL_GetMonth(&st, delta);
2124 if (MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE))
2126 MONTHCAL_Scroll(infoPtr, delta);
2127 MONTHCAL_NotifyDayState(infoPtr);
2128 MONTHCAL_NotifySelectionChange(infoPtr);
2129 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2132 return 0;
2134 case MCHT_TITLEYEAR:
2136 MONTHCAL_EditYear(infoPtr, ht.iOffset);
2137 return 0;
2139 case MCHT_TODAYLINK:
2141 infoPtr->calendars[0].month = infoPtr->todaysDate;
2142 infoPtr->minSel = infoPtr->todaysDate;
2143 infoPtr->maxSel = infoPtr->todaysDate;
2144 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2146 MONTHCAL_NotifySelectionChange(infoPtr);
2147 MONTHCAL_NotifySelect(infoPtr);
2148 return 0;
2150 case MCHT_CALENDARDATENEXT:
2151 case MCHT_CALENDARDATEPREV:
2152 case MCHT_CALENDARDATE:
2154 SYSTEMTIME st[2];
2156 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2158 st[0] = st[1] = ht.st;
2159 /* clear selection range */
2160 MONTHCAL_SetSelRange(infoPtr, st);
2162 infoPtr->status = MC_SEL_LBUTDOWN;
2163 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
2164 return 0;
2168 return 1;
2172 static LRESULT
2173 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2175 NMHDR nmhdr;
2176 MCHITTESTINFO ht;
2177 DWORD hit;
2179 TRACE("\n");
2181 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2182 RECT *r;
2184 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2185 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2186 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2188 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2191 ReleaseCapture();
2193 /* always send NM_RELEASEDCAPTURE notification */
2194 nmhdr.hwndFrom = infoPtr->hwndSelf;
2195 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2196 nmhdr.code = NM_RELEASEDCAPTURE;
2197 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2199 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2201 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2203 ht.cbSize = sizeof(MCHITTESTINFO);
2204 ht.pt.x = (short)LOWORD(lParam);
2205 ht.pt.y = (short)HIWORD(lParam);
2206 hit = MONTHCAL_HitTest(infoPtr, &ht);
2208 infoPtr->status = MC_SEL_LBUTUP;
2209 MONTHCAL_SetDayFocus(infoPtr, NULL);
2211 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2213 SYSTEMTIME sel = infoPtr->minSel;
2215 /* will be invalidated here */
2216 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2218 /* send MCN_SELCHANGE only if new date selected */
2219 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2220 MONTHCAL_NotifySelectionChange(infoPtr);
2222 MONTHCAL_NotifySelect(infoPtr);
2225 return 0;
2229 static LRESULT
2230 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2232 TRACE("%ld\n", id);
2234 switch(id) {
2235 case MC_PREVNEXTMONTHTIMER:
2236 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2237 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2238 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2239 break;
2240 case MC_TODAYUPDATETIMER:
2242 SYSTEMTIME st;
2244 if(infoPtr->todaySet) return 0;
2246 GetLocalTime(&st);
2247 MONTHCAL_UpdateToday(infoPtr, &st);
2249 /* notification sent anyway */
2250 MONTHCAL_NotifySelectionChange(infoPtr);
2252 return 0;
2254 default:
2255 ERR("got unknown timer %ld\n", id);
2256 break;
2259 return 0;
2263 static LRESULT
2264 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2266 MCHITTESTINFO ht;
2267 SYSTEMTIME st_ht;
2268 INT hit;
2269 RECT r;
2271 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2273 ht.cbSize = sizeof(MCHITTESTINFO);
2274 ht.pt.x = (short)LOWORD(lParam);
2275 ht.pt.y = (short)HIWORD(lParam);
2276 ht.iOffset = -1;
2278 hit = MONTHCAL_HitTest(infoPtr, &ht);
2280 /* not on the calendar date numbers? bail out */
2281 TRACE("hit:%x\n",hit);
2282 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2284 MONTHCAL_SetDayFocus(infoPtr, NULL);
2285 return 0;
2288 st_ht = ht.st;
2290 /* if pointer is over focused day still there's nothing to do */
2291 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2293 MONTHCAL_GetDayRect(infoPtr, &ht.st, &r, ht.iOffset);
2295 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2296 SYSTEMTIME st[2];
2298 MONTHCAL_GetSelRange(infoPtr, st);
2300 /* If we're still at the first selected date and range is empty, return.
2301 If range isn't empty we should change range to a single firstSel */
2302 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2303 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2305 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2307 st[0] = infoPtr->firstSel;
2308 /* we should overwrite timestamp here */
2309 MONTHCAL_CopyDate(&st_ht, &st[1]);
2311 /* bounds will be swapped here if needed */
2312 MONTHCAL_SetSelRange(infoPtr, st);
2314 return 0;
2317 done:
2319 /* FIXME: this should specify a rectangle containing only the days that changed
2320 using InvalidateRect */
2321 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2323 return 0;
2327 static LRESULT
2328 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2330 HDC hdc;
2331 PAINTSTRUCT ps;
2333 if (hdc_paint)
2335 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2336 hdc = hdc_paint;
2338 else
2339 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2341 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2342 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2343 return 0;
2346 static LRESULT
2347 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2349 RECT rc;
2351 if (!GetClipBox(hdc, &rc)) return FALSE;
2353 FillRect(hdc, &rc, infoPtr->brushes[BrushBackground]);
2355 return TRUE;
2358 static LRESULT
2359 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2361 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2363 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2364 return 0;
2366 if (options & PRF_ERASEBKGND)
2367 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2369 if (options & PRF_CLIENT)
2370 MONTHCAL_Paint(infoPtr, hdc);
2372 return 0;
2375 static LRESULT
2376 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2378 TRACE("\n");
2380 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2382 return 0;
2385 /* sets the size information */
2386 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2388 static const WCHAR O0W[] = { '0','0',0 };
2389 HDC hdc = GetDC(infoPtr->hwndSelf);
2390 RECT *title=&infoPtr->calendars[0].title;
2391 RECT *prev=&infoPtr->titlebtnprev;
2392 RECT *next=&infoPtr->titlebtnnext;
2393 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2394 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2395 RECT *wdays=&infoPtr->calendars[0].wdays;
2396 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2397 RECT *days=&infoPtr->calendars[0].days;
2398 RECT *todayrect=&infoPtr->todayrect;
2399 SIZE size, sz;
2400 TEXTMETRICW tm;
2401 HFONT currentFont;
2402 INT xdiv, dx, dy, i;
2403 RECT client;
2404 WCHAR buff[80];
2406 GetClientRect(infoPtr->hwndSelf, &client);
2408 currentFont = SelectObject(hdc, infoPtr->hFont);
2410 /* get the height and width of each day's text */
2411 GetTextMetricsW(hdc, &tm);
2412 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2414 /* find largest abbreviated day name for current locale */
2415 size.cx = sz.cx = 0;
2416 for (i = 0; i < 7; i++)
2418 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + i,
2419 buff, countof(buff)))
2421 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2422 if (sz.cx > size.cx) size.cx = sz.cx;
2424 else /* locale independent fallback on failure */
2426 static const WCHAR SunW[] = { 'S','u','n',0 };
2428 GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2429 break;
2433 infoPtr->textWidth = size.cx + 2;
2435 /* recalculate the height and width increments and offsets */
2436 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2438 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2440 infoPtr->width_increment = size.cx * 2 + 4;
2441 infoPtr->height_increment = infoPtr->textHeight;
2443 /* calculate title area */
2444 title->top = 0;
2445 title->bottom = 3 * infoPtr->height_increment / 2;
2446 title->left = 0;
2447 title->right = infoPtr->width_increment * xdiv;
2449 /* set the dimensions of the next and previous buttons and center */
2450 /* the month text vertically */
2451 prev->top = next->top = title->top + 4;
2452 prev->bottom = next->bottom = title->bottom - 4;
2453 prev->left = title->left + 4;
2454 prev->right = prev->left + (title->bottom - title->top);
2455 next->right = title->right - 4;
2456 next->left = next->right - (title->bottom - title->top);
2458 /* titlemonth->left and right change based upon the current month */
2459 /* and are recalculated in refresh as the current month may change */
2460 /* without the control being resized */
2461 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2462 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2464 /* setup the dimensions of the rectangle we draw the names of the */
2465 /* days of the week in */
2466 weeknumrect->left = 0;
2468 if(infoPtr->dwStyle & MCS_WEEKNUMBERS)
2469 weeknumrect->right = prev->right;
2470 else
2471 weeknumrect->right = weeknumrect->left;
2473 wdays->left = days->left = weeknumrect->right;
2474 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2475 wdays->top = title->bottom;
2476 wdays->bottom = wdays->top + infoPtr->height_increment;
2478 days->top = weeknumrect->top = wdays->bottom;
2479 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2481 todayrect->left = 0;
2482 todayrect->right = title->right;
2483 todayrect->top = days->bottom;
2484 todayrect->bottom = days->bottom + infoPtr->height_increment;
2486 /* offset all rectangles to center in client area */
2487 dx = (client.right - title->right) / 2;
2488 dy = (client.bottom - todayrect->bottom) / 2;
2490 /* if calendar doesn't fit client area show it at left/top bounds */
2491 if (title->left + dx < 0) dx = 0;
2492 if (title->top + dy < 0) dy = 0;
2494 if (dx != 0 || dy != 0)
2496 OffsetRect(title, dx, dy);
2497 OffsetRect(prev, dx, dy);
2498 OffsetRect(next, dx, dy);
2499 OffsetRect(titlemonth, dx, dy);
2500 OffsetRect(titleyear, dx, dy);
2501 OffsetRect(wdays, dx, dy);
2502 OffsetRect(weeknumrect, dx, dy);
2503 OffsetRect(days, dx, dy);
2504 OffsetRect(todayrect, dx, dy);
2507 /* TODO: update calendars count */
2508 infoPtr->dim.cx = infoPtr->dim.cy = 1;
2510 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2511 infoPtr->width_increment,infoPtr->height_increment,
2512 wine_dbgstr_rect(&client),
2513 wine_dbgstr_rect(title),
2514 wine_dbgstr_rect(wdays),
2515 wine_dbgstr_rect(days),
2516 wine_dbgstr_rect(todayrect));
2518 /* restore the originally selected font */
2519 SelectObject(hdc, currentFont);
2521 ReleaseDC(infoPtr->hwndSelf, hdc);
2524 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2526 TRACE("(width=%d, height=%d)\n", Width, Height);
2528 MONTHCAL_UpdateSize(infoPtr);
2529 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2531 return 0;
2534 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2536 return (LRESULT)infoPtr->hFont;
2539 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2541 HFONT hOldFont;
2542 LOGFONTW lf;
2544 if (!hFont) return 0;
2546 hOldFont = infoPtr->hFont;
2547 infoPtr->hFont = hFont;
2549 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2550 lf.lfWeight = FW_BOLD;
2551 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2553 MONTHCAL_UpdateSize(infoPtr);
2555 if (redraw)
2556 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2558 return (LRESULT)hOldFont;
2561 /* update theme after a WM_THEMECHANGED message */
2562 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2564 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2565 CloseThemeData (theme);
2566 OpenThemeData (infoPtr->hwndSelf, themeClass);
2567 return 0;
2570 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2571 const STYLESTRUCT *lpss)
2573 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2574 wStyleType, lpss->styleOld, lpss->styleNew);
2576 if (wStyleType != GWL_STYLE) return 0;
2578 infoPtr->dwStyle = lpss->styleNew;
2580 /* make room for week numbers */
2581 if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2582 MONTHCAL_UpdateSize(infoPtr);
2584 return 0;
2587 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2588 STYLESTRUCT *lpss)
2590 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2591 wStyleType, lpss->styleOld, lpss->styleNew);
2593 /* block MCS_MULTISELECT change */
2594 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2596 if (lpss->styleOld & MCS_MULTISELECT)
2597 lpss->styleNew |= MCS_MULTISELECT;
2598 else
2599 lpss->styleNew &= ~MCS_MULTISELECT;
2602 return 0;
2605 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2606 static LRESULT
2607 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2609 MONTHCAL_INFO *infoPtr;
2611 /* allocate memory for info structure */
2612 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2613 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2615 if (infoPtr == NULL) {
2616 ERR("could not allocate info memory!\n");
2617 return 0;
2620 infoPtr->hwndSelf = hwnd;
2621 infoPtr->hwndNotify = lpcs->hwndParent;
2622 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2623 infoPtr->calendars = Alloc(sizeof(CALENDAR_INFO));
2624 if (!infoPtr->calendars) goto fail;
2626 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
2628 /* initialize info structure */
2629 /* FIXME: calculate systemtime ->> localtime(subtract timezoneinfo) */
2631 GetLocalTime(&infoPtr->todaysDate);
2632 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2634 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2635 infoPtr->monthRange = 3;
2637 infoPtr->monthdayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
2638 if (!infoPtr->monthdayState) goto fail;
2640 infoPtr->colors[MCSC_BACKGROUND] = comctl32_color.clrWindow;
2641 infoPtr->colors[MCSC_TEXT] = comctl32_color.clrWindowText;
2642 infoPtr->colors[MCSC_TITLEBK] = comctl32_color.clrActiveCaption;
2643 infoPtr->colors[MCSC_TITLETEXT] = comctl32_color.clrWindow;
2644 infoPtr->colors[MCSC_MONTHBK] = comctl32_color.clrWindow;
2645 infoPtr->colors[MCSC_TRAILINGTEXT] = comctl32_color.clrGrayText;
2647 infoPtr->brushes[BrushBackground] = CreateSolidBrush(infoPtr->colors[MCSC_BACKGROUND]);
2648 infoPtr->brushes[BrushTitle] = CreateSolidBrush(infoPtr->colors[MCSC_TITLEBK]);
2649 infoPtr->brushes[BrushMonth] = CreateSolidBrush(infoPtr->colors[MCSC_MONTHBK]);
2651 infoPtr->pens[PenRed] = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
2652 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[MCSC_TEXT]);
2654 infoPtr->minSel = infoPtr->todaysDate;
2655 infoPtr->maxSel = infoPtr->todaysDate;
2656 infoPtr->calendars[0].month = infoPtr->todaysDate;
2657 infoPtr->isUnicode = TRUE;
2659 /* set all control dimensions */
2660 MONTHCAL_UpdateSize(infoPtr);
2662 /* today auto update timer, to be freed only on control destruction */
2663 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2665 OpenThemeData (infoPtr->hwndSelf, themeClass);
2667 return 0;
2669 fail:
2670 Free(infoPtr->monthdayState);
2671 Free(infoPtr->calendars);
2672 Free(infoPtr);
2673 return 0;
2676 static LRESULT
2677 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2679 INT i;
2681 /* free month calendar info data */
2682 Free(infoPtr->monthdayState);
2683 Free(infoPtr->calendars);
2684 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2686 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2688 for (i = 0; i < BrushLast; i++) DeleteObject(infoPtr->brushes[i]);
2689 for (i = 0; i < PenLast; i++) DeleteObject(infoPtr->pens[i]);
2691 Free(infoPtr);
2692 return 0;
2696 * Handler for WM_NOTIFY messages
2698 static LRESULT
2699 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2701 /* notification from year edit updown */
2702 if (hdr->code == UDN_DELTAPOS)
2704 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2706 if (hdr->hwndFrom == infoPtr->hWndYearUpDown && nmud->iDelta)
2708 /* year value limits are set up explicitly after updown creation */
2709 MONTHCAL_Scroll(infoPtr, 12 * nmud->iDelta);
2710 MONTHCAL_NotifyDayState(infoPtr);
2711 MONTHCAL_NotifySelectionChange(infoPtr);
2714 return 0;
2717 static inline BOOL
2718 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2720 BOOL prev = infoPtr->isUnicode;
2721 infoPtr->isUnicode = isUnicode;
2722 return prev;
2725 static inline BOOL
2726 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2728 return infoPtr->isUnicode;
2731 static LRESULT WINAPI
2732 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2734 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0);
2736 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2738 if (!infoPtr && (uMsg != WM_CREATE))
2739 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2740 switch(uMsg)
2742 case MCM_GETCURSEL:
2743 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2745 case MCM_SETCURSEL:
2746 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2748 case MCM_GETMAXSELCOUNT:
2749 return MONTHCAL_GetMaxSelCount(infoPtr);
2751 case MCM_SETMAXSELCOUNT:
2752 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2754 case MCM_GETSELRANGE:
2755 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2757 case MCM_SETSELRANGE:
2758 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2760 case MCM_GETMONTHRANGE:
2761 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2763 case MCM_SETDAYSTATE:
2764 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2766 case MCM_GETMINREQRECT:
2767 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2769 case MCM_GETCOLOR:
2770 return MONTHCAL_GetColor(infoPtr, wParam);
2772 case MCM_SETCOLOR:
2773 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2775 case MCM_GETTODAY:
2776 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2778 case MCM_SETTODAY:
2779 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2781 case MCM_HITTEST:
2782 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2784 case MCM_GETFIRSTDAYOFWEEK:
2785 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2787 case MCM_SETFIRSTDAYOFWEEK:
2788 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2790 case MCM_GETRANGE:
2791 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2793 case MCM_SETRANGE:
2794 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2796 case MCM_GETMONTHDELTA:
2797 return MONTHCAL_GetMonthDelta(infoPtr);
2799 case MCM_SETMONTHDELTA:
2800 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2802 case MCM_GETMAXTODAYWIDTH:
2803 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2805 case MCM_SETUNICODEFORMAT:
2806 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2808 case MCM_GETUNICODEFORMAT:
2809 return MONTHCAL_GetUnicodeFormat(infoPtr);
2811 case MCM_GETCALENDARCOUNT:
2812 return MONTHCAL_GetCalCount(infoPtr);
2814 case WM_GETDLGCODE:
2815 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2817 case WM_RBUTTONUP:
2818 return MONTHCAL_RButtonUp(infoPtr, lParam);
2820 case WM_LBUTTONDOWN:
2821 return MONTHCAL_LButtonDown(infoPtr, lParam);
2823 case WM_MOUSEMOVE:
2824 return MONTHCAL_MouseMove(infoPtr, lParam);
2826 case WM_LBUTTONUP:
2827 return MONTHCAL_LButtonUp(infoPtr, lParam);
2829 case WM_PAINT:
2830 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2832 case WM_PRINTCLIENT:
2833 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2835 case WM_ERASEBKGND:
2836 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2838 case WM_SETFOCUS:
2839 return MONTHCAL_SetFocus(infoPtr);
2841 case WM_SIZE:
2842 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2844 case WM_NOTIFY:
2845 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2847 case WM_CREATE:
2848 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2850 case WM_SETFONT:
2851 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2853 case WM_GETFONT:
2854 return MONTHCAL_GetFont(infoPtr);
2856 case WM_TIMER:
2857 return MONTHCAL_Timer(infoPtr, wParam);
2859 case WM_THEMECHANGED:
2860 return theme_changed (infoPtr);
2862 case WM_DESTROY:
2863 return MONTHCAL_Destroy(infoPtr);
2865 case WM_SYSCOLORCHANGE:
2866 COMCTL32_RefreshSysColors();
2867 return 0;
2869 case WM_STYLECHANGED:
2870 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2872 case WM_STYLECHANGING:
2873 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2875 default:
2876 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2877 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2878 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2883 void
2884 MONTHCAL_Register(void)
2886 WNDCLASSW wndClass;
2888 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2889 wndClass.style = CS_GLOBALCLASS;
2890 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2891 wndClass.cbClsExtra = 0;
2892 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2893 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2894 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2895 wndClass.lpszClassName = MONTHCAL_CLASSW;
2897 RegisterClassW(&wndClass);
2901 void
2902 MONTHCAL_Unregister(void)
2904 UnregisterClassW(MONTHCAL_CLASSW, NULL);