ntdll/tests: FILE_OVERWRITE and FILE_OVERWRITE_IF aren't legal for NtCreateNamedPipeFile.
[wine/multimedia.git] / dlls / comctl32 / monthcal.c
blob0c51075938fff388c1336f957a89180e00a8a8e6
1 /*
2 * Month calendar control
4 * Copyright 1998, 1999 Eric Kohl (ekohl@abo.rhein-zeitung.de)
5 * Copyright 1999 Alex Priem (alexp@sci.kun.nl)
6 * Copyright 1999 Chris Morgan <cmorgan@wpi.edu> and
7 * James Abbatiello <abbeyj@wpi.edu>
8 * Copyright 2000 Uwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de>
9 * Copyright 2009-2011 Nikolay Sivov
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * NOTE
27 * This code was audited for completeness against the documented features
28 * of Comctl32.dll version 6.0 on Oct. 20, 2004, by Dimitrie O. Paun.
30 * Unless otherwise noted, we believe this code to be complete, as per
31 * the specification mentioned above.
32 * If you discover missing features, or bugs, please note them below.
34 * TODO:
35 * -- MCM_[GS]ETUNICODEFORMAT
36 * -- handle resources better (doesn't work now);
37 * -- take care of internationalization.
38 * -- keyboard handling.
39 * -- search for FIXME
42 #include <math.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "commctrl.h"
54 #include "comctl32.h"
55 #include "uxtheme.h"
56 #include "vssym32.h"
57 #include "wine/unicode.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
62 #define MC_SEL_LBUTUP 1 /* Left button released */
63 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
64 #define MC_PREVPRESSED 4 /* Prev month button pressed */
65 #define MC_NEXTPRESSED 8 /* Next month button pressed */
66 #define MC_PREVNEXTMONTHDELAY 350 /* when continuously pressing `next/prev
67 month', wait 350 ms before going
68 to the next/prev month */
69 #define MC_TODAYUPDATEDELAY 120000 /* time between today check for update (2 min) */
71 #define MC_PREVNEXTMONTHTIMER 1 /* Timer IDs */
72 #define MC_TODAYUPDATETIMER 2
74 #define MC_CALENDAR_PADDING 6
76 #define countof(arr) (sizeof(arr)/sizeof(arr[0]))
78 /* convert from days to 100 nanoseconds unit - used as FILETIME unit */
79 #define DAYSTO100NSECS(days) (((ULONGLONG)(days))*24*60*60*10000000)
81 enum CachedPen
83 PenRed = 0,
84 PenText,
85 PenLast
88 enum CachedBrush
90 BrushTitle = 0,
91 BrushMonth,
92 BrushBackground,
93 BrushLast
96 /* single calendar data */
97 typedef struct _CALENDAR_INFO
99 RECT title; /* rect for the header above the calendar */
100 RECT titlemonth; /* the 'month name' text in the header */
101 RECT titleyear; /* the 'year number' text in the header */
102 RECT wdays; /* week days at top */
103 RECT days; /* calendar area */
104 RECT weeknums; /* week numbers at left side */
106 SYSTEMTIME month;/* contains calendar main month/year */
107 } CALENDAR_INFO;
109 typedef struct
111 HWND hwndSelf;
112 DWORD dwStyle; /* cached GWL_STYLE */
114 COLORREF colors[MCSC_TRAILINGTEXT+1];
115 HBRUSH brushes[BrushLast];
116 HPEN pens[PenLast];
118 HFONT hFont;
119 HFONT hBoldFont;
120 int textHeight;
121 int textWidth;
122 int height_increment;
123 int width_increment;
124 INT delta; /* scroll rate; # of months that the */
125 /* control moves when user clicks a scroll button */
126 int visible; /* # of months visible */
127 int firstDay; /* Start month calendar with firstDay's day,
128 stored in SYSTEMTIME format */
129 BOOL firstDaySet; /* first week day differs from locale defined */
131 BOOL isUnicode; /* value set with MCM_SETUNICODE format */
133 MONTHDAYSTATE *monthdayState;
134 SYSTEMTIME todaysDate;
135 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
136 int status; /* See MC_SEL flags */
137 SYSTEMTIME firstSel; /* first selected day */
138 INT maxSelCount;
139 SYSTEMTIME minSel; /* contains single selection when used without MCS_MULTISELECT */
140 SYSTEMTIME maxSel;
141 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
142 DWORD rangeValid;
143 SYSTEMTIME minDate;
144 SYSTEMTIME maxDate;
146 RECT titlebtnnext; /* the `next month' button in the header */
147 RECT titlebtnprev; /* the `prev month' button in the header */
148 RECT todayrect; /* `today: xx/xx/xx' text rect */
149 HWND hwndNotify; /* Window to receive the notifications */
150 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
151 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
152 WNDPROC EditWndProc; /* original Edit window procedure */
154 CALENDAR_INFO *calendars;
155 SIZE dim; /* [cx,cy] - dimensions of calendars matrix, row/column count */
156 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
158 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
160 /* empty SYSTEMTIME const */
161 static const SYSTEMTIME st_null;
162 /* valid date limits */
163 static const SYSTEMTIME max_allowed_date = { .wYear = 9999, .wMonth = 12, .wDay = 31 };
164 static const SYSTEMTIME min_allowed_date = { .wYear = 1752, .wMonth = 9, .wDay = 14 };
166 /* Prev/Next buttons */
167 enum nav_direction
169 DIRECTION_BACKWARD,
170 DIRECTION_FORWARD
173 /* helper functions */
174 static inline INT MONTHCAL_GetCalCount(const MONTHCAL_INFO *infoPtr)
176 return infoPtr->dim.cx * infoPtr->dim.cy;
179 /* send a single MCN_SELCHANGE notification */
180 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
182 NMSELCHANGE nmsc;
184 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
185 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
186 nmsc.nmhdr.code = MCN_SELCHANGE;
187 nmsc.stSelStart = infoPtr->minSel;
188 nmsc.stSelEnd = infoPtr->maxSel;
189 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
192 /* send a single MCN_SELECT notification */
193 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
195 NMSELCHANGE nmsc;
197 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
198 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
199 nmsc.nmhdr.code = MCN_SELECT;
200 nmsc.stSelStart = infoPtr->minSel;
201 nmsc.stSelEnd = infoPtr->maxSel;
203 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
206 static inline int MONTHCAL_MonthDiff(const SYSTEMTIME *left, const SYSTEMTIME *right)
208 return (right->wYear - left->wYear)*12 + right->wMonth - left->wMonth;
211 /* returns the number of days in any given month, checking for leap days */
212 /* January is 1, December is 12 */
213 int MONTHCAL_MonthLength(int month, int year)
215 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
216 /* Wrap around, this eases handling. Getting length only we shouldn't care
217 about year change here cause January and December have
218 the same day quantity */
219 if(month == 0)
220 month = 12;
221 else if(month == 13)
222 month = 1;
224 /* special case for calendar transition year */
225 if(month == min_allowed_date.wMonth && year == min_allowed_date.wYear) return 19;
227 /* if we have a leap year add 1 day to February */
228 /* a leap year is a year either divisible by 400 */
229 /* or divisible by 4 and not by 100 */
230 if(month == 2) { /* February */
231 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
232 (year%4 == 0)) ? 1 : 0);
234 else {
235 return mdays[month - 1];
239 /* compares timestamps using date part only */
240 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
242 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
243 (first->wDay == second->wDay);
246 /* make sure that date fields are valid */
247 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
249 if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
250 if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear)) return FALSE;
252 return TRUE;
255 /* Copies timestamp part only.
257 * PARAMETERS
259 * [I] from : source date
260 * [O] to : dest date
262 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
264 to->wHour = from->wHour;
265 to->wMinute = from->wMinute;
266 to->wSecond = from->wSecond;
269 /* Copies date part only.
271 * PARAMETERS
273 * [I] from : source date
274 * [O] to : dest date
276 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
278 to->wYear = from->wYear;
279 to->wMonth = from->wMonth;
280 to->wDay = from->wDay;
281 to->wDayOfWeek = from->wDayOfWeek;
284 /* Compares two dates in SYSTEMTIME format
286 * PARAMETERS
288 * [I] first : pointer to valid first date data to compare
289 * [I] second : pointer to valid second date data to compare
291 * RETURN VALUE
293 * -1 : first < second
294 * 0 : first == second
295 * 1 : first > second
297 * Note that no date validation performed, already validated values expected.
299 static LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
301 FILETIME ft_first, ft_second;
303 SystemTimeToFileTime(first, &ft_first);
304 SystemTimeToFileTime(second, &ft_second);
306 return CompareFileTime(&ft_first, &ft_second);
309 static LONG MONTHCAL_CompareMonths(const SYSTEMTIME *first, const SYSTEMTIME *second)
311 SYSTEMTIME st_first, st_second;
313 st_first = st_second = st_null;
314 MONTHCAL_CopyDate(first, &st_first);
315 MONTHCAL_CopyDate(second, &st_second);
316 st_first.wDay = st_second.wDay = 1;
318 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
321 static LONG MONTHCAL_CompareDate(const SYSTEMTIME *first, const SYSTEMTIME *second)
323 SYSTEMTIME st_first, st_second;
325 st_first = st_second = st_null;
326 MONTHCAL_CopyDate(first, &st_first);
327 MONTHCAL_CopyDate(second, &st_second);
329 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
332 /* Checks largest possible date range and configured one
334 * PARAMETERS
336 * [I] infoPtr : valid pointer to control data
337 * [I] date : pointer to valid date data to check
338 * [I] fix : make date fit valid range
340 * RETURN VALUE
342 * TRUE - date within largest and configured range
343 * FALSE - date is outside largest or configured range
345 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr,
346 SYSTEMTIME *date, BOOL fix)
348 const SYSTEMTIME *fix_st = NULL;
350 if(MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) {
351 fix_st = &max_allowed_date;
353 else if(MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1) {
354 fix_st = &min_allowed_date;
356 else if(infoPtr->rangeValid & GDTR_MAX) {
357 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
358 fix_st = &infoPtr->maxDate;
361 else if(infoPtr->rangeValid & GDTR_MIN) {
362 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
363 fix_st = &infoPtr->minDate;
367 if (fix && fix_st) {
368 date->wYear = fix_st->wYear;
369 date->wMonth = fix_st->wMonth;
372 return fix_st ? FALSE : TRUE;
375 /* Checks passed range width with configured maximum selection count
377 * PARAMETERS
379 * [I] infoPtr : valid pointer to control data
380 * [I] range0 : pointer to valid date data (requested bound)
381 * [I] range1 : pointer to valid date data (primary bound)
382 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
384 * Adjust value computed basing on primary bound and current maximum selection
385 * count. For simple range check (without adjusted value required) (range0, range1)
386 * relation means nothing.
388 * RETURN VALUE
390 * TRUE - range is shorter or equal to maximum
391 * FALSE - range is larger than maximum
393 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
394 const SYSTEMTIME *range0,
395 const SYSTEMTIME *range1,
396 SYSTEMTIME *adjust)
398 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
399 FILETIME ft_range0, ft_range1;
400 LONG cmp;
402 SystemTimeToFileTime(range0, &ft_range0);
403 SystemTimeToFileTime(range1, &ft_range1);
405 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
406 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
407 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
408 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
410 cmp = CompareFileTime(&ft_range0, &ft_range1);
412 if(cmp == 1)
413 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
414 else
415 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
417 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
419 if(adjust) {
420 if(cmp == 1)
421 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
422 else
423 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
425 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
426 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
427 FileTimeToSystemTime(&ft_range0, adjust);
430 return FALSE;
432 else return TRUE;
435 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
436 Milliseconds are intentionally not validated. */
437 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
439 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
440 return FALSE;
441 else
442 return TRUE;
445 /* Note:Depending on DST, this may be offset by a day.
446 Need to find out if we're on a DST place & adjust the clock accordingly.
447 Above function assumes we have a valid data.
448 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
449 0 = Sunday.
452 /* Returns the day in the week
454 * PARAMETERS
455 * [i] date : input date
456 * [I] inplace : set calculated value back to date structure
458 * RETURN VALUE
459 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
461 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
463 SYSTEMTIME st = st_null;
464 FILETIME ft;
466 MONTHCAL_CopyDate(date, &st);
468 SystemTimeToFileTime(&st, &ft);
469 FileTimeToSystemTime(&ft, &st);
471 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
473 return st.wDayOfWeek;
476 /* add/subtract 'months' from date */
477 static inline void MONTHCAL_GetMonth(SYSTEMTIME *date, INT months)
479 INT length, m = date->wMonth + months;
481 date->wYear += m > 0 ? (m - 1) / 12 : m / 12 - 1;
482 date->wMonth = m > 0 ? (m - 1) % 12 + 1 : 12 + m % 12;
483 /* fix moving from last day in a month */
484 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
485 if(date->wDay > length) date->wDay = length;
486 MONTHCAL_CalculateDayOfWeek(date, TRUE);
489 /* properly updates date to point on next month */
490 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
492 MONTHCAL_GetMonth(date, 1);
495 /* properly updates date to point on prev month */
496 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
498 MONTHCAL_GetMonth(date, -1);
501 /* Returns full date for a first currently visible day */
502 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
504 /* zero indexed calendar has the earliest date */
505 SYSTEMTIME st_first = infoPtr->calendars[0].month;
506 INT firstDay;
508 st_first.wDay = 1;
509 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
511 *date = infoPtr->calendars[0].month;
512 MONTHCAL_GetPrevMonth(date);
514 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
515 (infoPtr->firstDay - firstDay) % 7 + 1;
517 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
518 date->wDay -= 7;
520 /* fix day of week */
521 MONTHCAL_CalculateDayOfWeek(date, TRUE);
524 /* Returns full date for a last currently visible day */
525 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
527 /* the latest date is in latest calendar */
528 SYSTEMTIME st, *lt_month = &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
529 INT first_day;
531 *date = *lt_month;
532 st = *lt_month;
534 /* day of week of first day of current month */
535 st.wDay = 1;
536 first_day = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
538 MONTHCAL_GetNextMonth(date);
539 MONTHCAL_GetPrevMonth(&st);
541 /* last calendar starts with some date from previous month that not displayed */
542 st.wDay = MONTHCAL_MonthLength(st.wMonth, st.wYear) +
543 (infoPtr->firstDay - first_day) % 7 + 1;
544 if (st.wDay > MONTHCAL_MonthLength(st.wMonth, st.wYear)) st.wDay -= 7;
546 /* Use month length to get max day. 42 means max day count in calendar area */
547 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
548 MONTHCAL_MonthLength(lt_month->wMonth, lt_month->wYear);
550 /* fix day of week */
551 MONTHCAL_CalculateDayOfWeek(date, TRUE);
554 /* From a given point calculate the row, column and day in the calendar,
555 'day == 0' means the last day of the last month. */
556 static int MONTHCAL_GetDayFromPos(const MONTHCAL_INFO *infoPtr, POINT pt, INT calIdx)
558 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
559 int firstDay, col, row;
560 RECT client;
562 GetClientRect(infoPtr->hwndSelf, &client);
564 /* if the point is outside the x bounds of the window put it at the boundary */
565 if (pt.x > client.right) pt.x = client.right;
567 col = (pt.x - infoPtr->calendars[calIdx].days.left ) / infoPtr->width_increment;
568 row = (pt.y - infoPtr->calendars[calIdx].days.top ) / infoPtr->height_increment;
570 st.wDay = 1;
571 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
572 return col + 7 * row - firstDay;
575 /* Get day position for given date and calendar
577 * PARAMETERS
579 * [I] infoPtr : pointer to control data
580 * [I] date : date value
581 * [O] col : day column (zero based)
582 * [O] row : week column (zero based)
583 * [I] calIdx : calendar index
585 static void MONTHCAL_GetDayPos(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
586 INT *col, INT *row, INT calIdx)
588 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
589 INT first;
591 st.wDay = 1;
592 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
594 if (calIdx == 0 || calIdx == MONTHCAL_GetCalCount(infoPtr)-1) {
595 const SYSTEMTIME *cal = &infoPtr->calendars[calIdx].month;
596 LONG cmp = MONTHCAL_CompareMonths(date, &st);
598 /* previous month */
599 if (cmp == -1) {
600 *col = (first - MONTHCAL_MonthLength(date->wMonth, cal->wYear) + date->wDay) % 7;
601 *row = 0;
602 return;
605 /* next month calculation is same as for current, just add current month length */
606 if (cmp == 1)
607 first += MONTHCAL_MonthLength(cal->wMonth, cal->wYear);
610 *col = (date->wDay + first) % 7;
611 *row = (date->wDay + first - *col) / 7;
614 /* returns bounding box for day in given position in given calendar */
615 static inline void MONTHCAL_GetDayRectI(const MONTHCAL_INFO *infoPtr, RECT *r,
616 INT col, INT row, INT calIdx)
618 r->left = infoPtr->calendars[calIdx].days.left + col * infoPtr->width_increment;
619 r->right = r->left + infoPtr->width_increment;
620 r->top = infoPtr->calendars[calIdx].days.top + row * infoPtr->height_increment;
621 r->bottom = r->top + infoPtr->textHeight;
624 /* Returns bounding box for given date
626 * NOTE: when calendar index is unknown pass -1
628 static inline void MONTHCAL_GetDayRect(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
629 RECT *r, INT calIdx)
631 INT col, row;
633 if (calIdx == -1)
635 INT cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[0].month);
637 if (cmp <= 0)
638 calIdx = 0;
639 else
641 cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month);
642 if (cmp >= 0)
643 calIdx = MONTHCAL_GetCalCount(infoPtr)-1;
644 else
646 for (calIdx = 1; calIdx < MONTHCAL_GetCalCount(infoPtr)-1; calIdx++)
647 if (MONTHCAL_CompareMonths(date, &infoPtr->calendars[calIdx].month) == 0)
648 break;
653 MONTHCAL_GetDayPos(infoPtr, date, &col, &row, calIdx);
654 MONTHCAL_GetDayRectI(infoPtr, r, col, row, calIdx);
657 static LRESULT
658 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
660 INT range;
662 TRACE("flag=%d, st=%p\n", flag, st);
664 switch (flag) {
665 case GMR_VISIBLE:
667 if (st)
669 st[0] = infoPtr->calendars[0].month;
670 st[1] = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
672 if (st[0].wMonth == min_allowed_date.wMonth &&
673 st[0].wYear == min_allowed_date.wYear)
675 st[0].wDay = min_allowed_date.wDay;
677 else
678 st[0].wDay = 1;
679 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
681 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
682 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
685 range = MONTHCAL_GetCalCount(infoPtr);
686 break;
688 case GMR_DAYSTATE:
690 if (st)
692 MONTHCAL_GetMinDate(infoPtr, &st[0]);
693 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
695 /* include two partially visible months */
696 range = MONTHCAL_GetCalCount(infoPtr) + 2;
697 break;
699 default:
700 WARN("Unknown flag value, got %d\n", flag);
701 range = 0;
704 return range;
707 /* Focused day helper:
709 - set focused date to given value;
710 - reset to zero value if NULL passed;
711 - invalidate previous and new day rectangle only if needed.
713 Returns TRUE if focused day changed, FALSE otherwise.
715 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
717 RECT r;
719 if(st)
721 /* there's nothing to do if it's the same date,
722 mouse move within same date rectangle case */
723 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
725 /* invalidate old focused day */
726 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
727 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
729 infoPtr->focusedSel = *st;
732 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
734 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
735 infoPtr->focusedSel = st_null;
737 /* on set invalidates new day, on reset clears previous focused day */
738 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
740 return TRUE;
743 /* draw today boundary box for specified rectangle */
744 static void MONTHCAL_Circle(const MONTHCAL_INFO *infoPtr, HDC hdc, const RECT *r)
746 HPEN old_pen = SelectObject(hdc, infoPtr->pens[PenRed]);
747 HBRUSH old_brush;
749 old_brush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
750 Rectangle(hdc, r->left, r->top, r->right, r->bottom);
752 SelectObject(hdc, old_brush);
753 SelectObject(hdc, old_pen);
756 /* Draw today day mark rectangle
758 * [I] hdc : context to draw in
759 * [I] date : day to mark with rectangle
762 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
763 const SYSTEMTIME *date)
765 RECT r;
767 MONTHCAL_GetDayRect(infoPtr, date, &r, -1);
768 MONTHCAL_Circle(infoPtr, hdc, &r);
771 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
772 int bold, const PAINTSTRUCT *ps)
774 static const WCHAR fmtW[] = { '%','d',0 };
775 WCHAR buf[10];
776 RECT r, r_temp;
777 COLORREF oldCol = 0;
778 COLORREF oldBk = 0;
779 INT old_bkmode, selection;
781 /* no need to check styles: when selection is not valid, it is set to zero.
782 1 < day < 31, so everything is OK */
783 MONTHCAL_GetDayRect(infoPtr, st, &r, -1);
784 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
786 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
787 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0))
789 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
790 TRACE("%s\n", wine_dbgstr_rect(&r));
791 oldCol = SetTextColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
792 oldBk = SetBkColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
793 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
795 selection = 1;
797 else
798 selection = 0;
800 SelectObject(hdc, bold ? infoPtr->hBoldFont : infoPtr->hFont);
802 old_bkmode = SetBkMode(hdc, TRANSPARENT);
803 wsprintfW(buf, fmtW, st->wDay);
804 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
805 SetBkMode(hdc, old_bkmode);
807 if (selection)
809 SetTextColor(hdc, oldCol);
810 SetBkColor(hdc, oldBk);
814 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, enum nav_direction button)
816 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
817 RECT *r = button == DIRECTION_FORWARD ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
818 BOOL pressed = button == DIRECTION_FORWARD ? infoPtr->status & MC_NEXTPRESSED :
819 infoPtr->status & MC_PREVPRESSED;
820 if (theme)
822 static const int states[] = {
823 /* Prev button */
824 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
825 /* Next button */
826 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
828 int stateNum = button == DIRECTION_FORWARD ? 3 : 0;
829 if (pressed)
830 stateNum += 1;
831 else
833 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
835 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
837 else
839 int style = button == DIRECTION_FORWARD ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
840 if (pressed)
841 style |= DFCS_PUSHED;
842 else
844 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
847 DrawFrameControl(hdc, r, DFC_SCROLL, style);
851 /* paint a title with buttons and month/year string */
852 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
854 static const WCHAR fmt_monthW[] = { '%','s',' ','%','l','d',0 };
855 RECT *title = &infoPtr->calendars[calIdx].title;
856 const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
857 WCHAR buf_month[80], buf_fmt[80];
858 SIZE sz;
860 /* fill header box */
861 FillRect(hdc, title, infoPtr->brushes[BrushTitle]);
863 /* month/year string */
864 SetBkColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
865 SetTextColor(hdc, infoPtr->colors[MCSC_TITLETEXT]);
866 SelectObject(hdc, infoPtr->hBoldFont);
868 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1 + st->wMonth - 1,
869 buf_month, countof(buf_month));
871 wsprintfW(buf_fmt, fmt_monthW, buf_month, st->wYear);
872 DrawTextW(hdc, buf_fmt, strlenW(buf_fmt), title,
873 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
875 /* update title rectangles with current month - used while testing hits */
876 GetTextExtentPoint32W(hdc, buf_fmt, strlenW(buf_fmt), &sz);
877 infoPtr->calendars[calIdx].titlemonth.left = title->right / 2 + title->left / 2 - sz.cx / 2;
878 infoPtr->calendars[calIdx].titleyear.right = title->right / 2 + title->left / 2 + sz.cx / 2;
880 GetTextExtentPoint32W(hdc, buf_month, strlenW(buf_month), &sz);
881 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
882 infoPtr->calendars[calIdx].titleyear.left = infoPtr->calendars[calIdx].titlemonth.right;
885 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
887 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
888 static const WCHAR fmt_weekW[] = { '%','d',0 };
889 INT mindays, weeknum, weeknum1, startofprescal;
890 INT i, prev_month;
891 SYSTEMTIME st;
892 WCHAR buf[80];
893 HPEN old_pen;
894 RECT r;
896 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
898 MONTHCAL_GetMinDate(infoPtr, &st);
899 startofprescal = st.wDay;
900 st = *date;
902 prev_month = date->wMonth - 1;
903 if(prev_month == 0) prev_month = 12;
906 Rules what week to call the first week of a new year:
907 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
908 The week containing Jan 1 is the first week of year
909 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
910 First week of year must contain 4 days of the new year
911 LOCALE_IFIRSTWEEKOFYEAR == 1 (what countries?)
912 The first week of the year must contain only days of the new year
914 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
915 weeknum = atoiW(buf);
916 switch (weeknum)
918 case 1: mindays = 6;
919 break;
920 case 2: mindays = 3;
921 break;
922 case 0: mindays = 0;
923 break;
924 default:
925 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
926 mindays = 0;
929 if (date->wMonth == 1)
931 /* calculate all those exceptions for January */
932 st.wDay = st.wMonth = 1;
933 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
934 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
935 weeknum = 1;
936 else
938 weeknum = 0;
939 for(i = 0; i < 11; i++)
940 weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
942 weeknum += startofprescal + 7;
943 weeknum /= 7;
944 st.wYear -= 1;
945 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
946 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
949 else
951 weeknum = 0;
952 for(i = 0; i < prev_month - 1; i++)
953 weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
955 weeknum += startofprescal + 7;
956 weeknum /= 7;
957 st.wDay = st.wMonth = 1;
958 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
959 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
962 r = infoPtr->calendars[calIdx].weeknums;
964 /* erase whole week numbers area */
965 FillRect(hdc, &r, infoPtr->brushes[BrushMonth]);
966 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
968 /* reduce rectangle to one week number */
969 r.bottom = r.top + infoPtr->height_increment;
971 for(i = 0; i < 6; i++) {
972 if((i == 0) && (weeknum > 50))
974 wsprintfW(buf, fmt_weekW, weeknum);
975 weeknum = 0;
977 else if((i == 5) && (weeknum > 47))
979 wsprintfW(buf, fmt_weekW, 1);
981 else
982 wsprintfW(buf, fmt_weekW, weeknum + i);
984 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
985 OffsetRect(&r, 0, infoPtr->height_increment);
988 /* line separator for week numbers column */
989 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
990 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
991 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
992 SelectObject(hdc, old_pen);
995 /* bottom today date */
996 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
998 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
999 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
1000 RECT text_rect, box_rect;
1001 HFONT old_font;
1002 INT col;
1004 if(infoPtr->dwStyle & MCS_NOTODAY) return;
1006 if (!LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, countof(buf_todayW)))
1008 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
1009 WARN("Can't load resource\n");
1010 strcpyW(buf_todayW, todayW);
1013 col = infoPtr->dwStyle & MCS_NOTODAYCIRCLE ? 0 : 1;
1014 if (infoPtr->dwStyle & MCS_WEEKNUMBERS) col--;
1015 /* label is located below first calendar last row */
1016 MONTHCAL_GetDayRectI(infoPtr, &text_rect, col, 6, infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx);
1017 box_rect = text_rect;
1019 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
1020 buf_dateW, countof(buf_dateW));
1021 old_font = SelectObject(hdc, infoPtr->hBoldFont);
1022 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1024 wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
1025 DrawTextW(hdc, buf, -1, &text_rect, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1026 DrawTextW(hdc, buf, -1, &text_rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1028 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
1029 OffsetRect(&box_rect, -infoPtr->width_increment, 0);
1030 MONTHCAL_Circle(infoPtr, hdc, &box_rect);
1033 SelectObject(hdc, old_font);
1036 /* today mark + focus */
1037 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1039 /* circle today date if only it's in fully visible month */
1040 if (!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
1042 INT i;
1044 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1045 if (!MONTHCAL_CompareMonths(&infoPtr->todaysDate, &infoPtr->calendars[i].month))
1047 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
1048 break;
1052 if (!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
1054 RECT r;
1055 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
1056 DrawFocusRect(hdc, &r);
1060 /* months before first calendar month and after last calendar month */
1061 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1063 INT mask, length, index;
1064 SYSTEMTIME st_max, st;
1066 if (infoPtr->dwStyle & MCS_NOTRAILINGDATES) return;
1068 SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
1070 /* draw prev month */
1071 MONTHCAL_GetMinDate(infoPtr, &st);
1072 mask = 1 << (st.wDay-1);
1073 /* December and January both 31 days long, so no worries if wrapped */
1074 length = MONTHCAL_MonthLength(infoPtr->calendars[0].month.wMonth - 1,
1075 infoPtr->calendars[0].month.wYear);
1076 index = 0;
1077 while(st.wDay <= length)
1079 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1080 mask <<= 1;
1081 st.wDay++;
1084 /* draw next month */
1085 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1086 st.wDay = 1;
1087 MONTHCAL_GetNextMonth(&st);
1088 MONTHCAL_GetMaxDate(infoPtr, &st_max);
1089 mask = 1;
1090 index = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)-1;
1091 while(st.wDay <= st_max.wDay)
1093 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1094 mask <<= 1;
1095 st.wDay++;
1099 /* paint a calendar area */
1100 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
1102 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
1103 INT i, j, length;
1104 RECT r, fill_bk_rect;
1105 SYSTEMTIME st;
1106 WCHAR buf[80];
1107 HPEN old_pen;
1108 int mask;
1110 /* fill whole days area - from week days area to today note rectangle */
1111 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
1112 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
1113 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
1115 FillRect(hdc, &fill_bk_rect, infoPtr->brushes[BrushMonth]);
1117 /* draw line under day abbreviations */
1118 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1119 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
1120 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
1121 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
1122 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
1123 SelectObject(hdc, old_pen);
1125 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
1126 infoPtr->calendars[calIdx].weeknums.right;
1128 /* draw day abbreviations */
1129 SelectObject(hdc, infoPtr->hFont);
1130 SetBkColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
1131 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1132 /* rectangle to draw a single day abbreviation within */
1133 r = infoPtr->calendars[calIdx].wdays;
1134 r.right = r.left + infoPtr->width_increment;
1136 i = infoPtr->firstDay;
1137 for(j = 0; j < 7; j++) {
1138 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
1139 DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1140 OffsetRect(&r, infoPtr->width_increment, 0);
1143 /* draw current month */
1144 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1145 st = *date;
1146 st.wDay = 1;
1147 mask = 1;
1148 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1149 while(st.wDay <= length)
1151 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[calIdx+1] & mask, ps);
1152 mask <<= 1;
1153 st.wDay++;
1157 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1159 COLORREF old_text_clr, old_bk_clr;
1160 HFONT old_font;
1161 INT i;
1163 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1164 old_bk_clr = GetBkColor(hdc);
1165 old_font = GetCurrentObject(hdc, OBJ_FONT);
1167 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1169 RECT *title = &infoPtr->calendars[i].title;
1170 RECT r;
1172 /* draw title, redraw all its elements */
1173 if (IntersectRect(&r, &(ps->rcPaint), title))
1174 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1176 /* draw calendar area */
1177 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1178 if (IntersectRect(&r, &(ps->rcPaint), &r))
1179 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1181 /* week numbers */
1182 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1185 /* partially visible months */
1186 MONTHCAL_PaintLeadTrailMonths(infoPtr, hdc, ps);
1188 /* focus and today rectangle */
1189 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1191 /* today at the bottom left */
1192 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1194 /* navigation buttons */
1195 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_BACKWARD);
1196 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_FORWARD);
1198 /* restore context */
1199 SetBkColor(hdc, old_bk_clr);
1200 SelectObject(hdc, old_font);
1201 SetTextColor(hdc, old_text_clr);
1204 static LRESULT
1205 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, RECT *rect)
1207 TRACE("rect %p\n", rect);
1209 if(!rect) return FALSE;
1211 *rect = infoPtr->calendars[0].title;
1212 rect->bottom = infoPtr->calendars[0].days.bottom + infoPtr->todayrect.bottom -
1213 infoPtr->todayrect.top;
1215 AdjustWindowRect(rect, infoPtr->dwStyle, FALSE);
1217 /* minimal rectangle is zero based */
1218 OffsetRect(rect, -rect->left, -rect->top);
1220 TRACE("%s\n", wine_dbgstr_rect(rect));
1222 return TRUE;
1225 static COLORREF
1226 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, UINT index)
1228 TRACE("%p, %d\n", infoPtr, index);
1230 if (index > MCSC_TRAILINGTEXT) return -1;
1231 return infoPtr->colors[index];
1234 static LRESULT
1235 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, UINT index, COLORREF color)
1237 enum CachedBrush type;
1238 COLORREF prev;
1240 TRACE("%p, %d: color %08x\n", infoPtr, index, color);
1242 if (index > MCSC_TRAILINGTEXT) return -1;
1244 prev = infoPtr->colors[index];
1245 infoPtr->colors[index] = color;
1247 /* update cached brush */
1248 switch (index)
1250 case MCSC_BACKGROUND:
1251 type = BrushBackground;
1252 break;
1253 case MCSC_TITLEBK:
1254 type = BrushTitle;
1255 break;
1256 case MCSC_MONTHBK:
1257 type = BrushMonth;
1258 break;
1259 default:
1260 type = BrushLast;
1263 if (type != BrushLast)
1265 DeleteObject(infoPtr->brushes[type]);
1266 infoPtr->brushes[type] = CreateSolidBrush(color);
1269 /* update cached pen */
1270 if (index == MCSC_TEXT)
1272 DeleteObject(infoPtr->pens[PenText]);
1273 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[index]);
1276 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND ? TRUE : FALSE);
1277 return prev;
1280 static LRESULT
1281 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1283 TRACE("\n");
1285 if(infoPtr->delta)
1286 return infoPtr->delta;
1287 else
1288 return infoPtr->visible;
1292 static LRESULT
1293 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1295 INT prev = infoPtr->delta;
1297 TRACE("delta %d\n", delta);
1299 infoPtr->delta = delta;
1300 return prev;
1304 static inline LRESULT
1305 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1307 int day;
1309 /* convert from SYSTEMTIME to locale format */
1310 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1312 return MAKELONG(day, infoPtr->firstDaySet);
1316 /* Sets the first day of the week that will appear in the control
1319 * PARAMETERS:
1320 * [I] infoPtr : valid pointer to control data
1321 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1324 * RETURN VALUE:
1325 * Low word contains previous first day,
1326 * high word indicates was first day forced with this message before or is
1327 * locale defined (TRUE - was forced, FALSE - wasn't).
1329 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1330 * FIXME: we need more error checking here
1332 static LRESULT
1333 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1335 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1336 int new_day;
1338 TRACE("%d\n", day);
1340 if(day == -1)
1342 WCHAR buf[80];
1344 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1345 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1347 new_day = atoiW(buf);
1349 infoPtr->firstDaySet = FALSE;
1351 else if(day >= 7)
1353 new_day = 6; /* max first day allowed */
1354 infoPtr->firstDaySet = TRUE;
1356 else
1358 /* Native behaviour for that case is broken: invalid date number >31
1359 got displayed at (0,0) position, current month starts always from
1360 (1,0) position. Should be implemented here as well only if there's
1361 nothing else to do. */
1362 if (day < -1)
1363 FIXME("No bug compatibility for day=%d\n", day);
1365 new_day = day;
1366 infoPtr->firstDaySet = TRUE;
1369 /* convert from locale to SYSTEMTIME format */
1370 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1372 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1374 return prev;
1377 static LRESULT
1378 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1380 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1383 static LRESULT
1384 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1386 FILETIME ft_min, ft_max;
1388 TRACE("%x %p\n", limits, range);
1390 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1391 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1392 return FALSE;
1394 if (limits & GDTR_MIN)
1396 if (!MONTHCAL_ValidateTime(&range[0]))
1397 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1399 infoPtr->minDate = range[0];
1400 infoPtr->rangeValid |= GDTR_MIN;
1402 if (limits & GDTR_MAX)
1404 if (!MONTHCAL_ValidateTime(&range[1]))
1405 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1407 infoPtr->maxDate = range[1];
1408 infoPtr->rangeValid |= GDTR_MAX;
1411 /* Only one limit set - we are done */
1412 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1413 return TRUE;
1415 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1416 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1418 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1420 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1422 /* Native swaps limits only when both limits are being set. */
1423 SYSTEMTIME st_tmp = infoPtr->minDate;
1424 infoPtr->minDate = infoPtr->maxDate;
1425 infoPtr->maxDate = st_tmp;
1427 else
1429 /* reset the other limit */
1430 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1431 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1432 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1436 return TRUE;
1440 static LRESULT
1441 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1443 TRACE("%p\n", range);
1445 if(!range) return FALSE;
1447 range[1] = infoPtr->maxDate;
1448 range[0] = infoPtr->minDate;
1450 return infoPtr->rangeValid;
1454 static LRESULT
1455 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1457 TRACE("%p %d %p\n", infoPtr, months, states);
1459 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return 0;
1460 if (months != MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)) return 0;
1462 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1464 return 1;
1467 static LRESULT
1468 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1470 TRACE("%p\n", curSel);
1471 if(!curSel) return FALSE;
1472 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1474 *curSel = infoPtr->minSel;
1475 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1476 return TRUE;
1479 static LRESULT
1480 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1482 SYSTEMTIME prev = infoPtr->minSel, selection;
1483 INT diff;
1484 WORD day;
1486 TRACE("%p\n", curSel);
1487 if(!curSel) return FALSE;
1488 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1490 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1491 /* exit earlier if selection equals current */
1492 if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1494 selection = *curSel;
1495 selection.wHour = selection.wMinute = selection.wSecond = selection.wMilliseconds = 0;
1496 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1498 if(!MONTHCAL_IsDateInValidRange(infoPtr, &selection, FALSE)) return FALSE;
1500 /* scroll calendars only if we have to */
1501 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, curSel);
1502 if (diff <= 0)
1504 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, curSel);
1505 if (diff > 0) diff = 0;
1508 if (diff != 0)
1510 INT i;
1512 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1513 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1516 /* we need to store time part as it is */
1517 selection = *curSel;
1518 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1519 infoPtr->minSel = infoPtr->maxSel = selection;
1521 /* if selection is still in current month, reduce rectangle */
1522 day = prev.wDay;
1523 prev.wDay = curSel->wDay;
1524 if (MONTHCAL_IsDateEqual(&prev, curSel))
1526 RECT r_prev, r_new;
1528 prev.wDay = day;
1529 MONTHCAL_GetDayRect(infoPtr, &prev, &r_prev, -1);
1530 MONTHCAL_GetDayRect(infoPtr, curSel, &r_new, -1);
1532 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1533 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1535 else
1536 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1538 return TRUE;
1542 static LRESULT
1543 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1545 return infoPtr->maxSelCount;
1549 static LRESULT
1550 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1552 TRACE("%d\n", max);
1554 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1555 if(max <= 0) return FALSE;
1557 infoPtr->maxSelCount = max;
1559 return TRUE;
1563 static LRESULT
1564 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1566 TRACE("%p\n", range);
1568 if(!range) return FALSE;
1570 if(infoPtr->dwStyle & MCS_MULTISELECT)
1572 range[1] = infoPtr->maxSel;
1573 range[0] = infoPtr->minSel;
1574 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1575 return TRUE;
1578 return FALSE;
1582 static LRESULT
1583 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1585 SYSTEMTIME old_range[2];
1586 INT diff;
1588 TRACE("%p\n", range);
1590 if(!range || !(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1592 /* adjust timestamps */
1593 if(!MONTHCAL_ValidateTime(&range[0])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1594 if(!MONTHCAL_ValidateTime(&range[1])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1596 /* maximum range exceeded */
1597 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1599 old_range[0] = infoPtr->minSel;
1600 old_range[1] = infoPtr->maxSel;
1602 /* swap if min > max */
1603 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1605 infoPtr->minSel = range[0];
1606 infoPtr->maxSel = range[1];
1608 else
1610 infoPtr->minSel = range[1];
1611 infoPtr->maxSel = range[0];
1614 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, &infoPtr->maxSel);
1615 if (diff < 0)
1617 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, &infoPtr->maxSel);
1618 if (diff > 0) diff = 0;
1621 if (diff != 0)
1623 INT i;
1625 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1626 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1629 /* update day of week */
1630 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1631 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1633 /* redraw if bounds changed */
1634 /* FIXME: no actual need to redraw everything */
1635 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1636 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1638 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1641 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1642 return TRUE;
1646 static LRESULT
1647 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1649 TRACE("%p\n", today);
1651 if(!today) return FALSE;
1652 *today = infoPtr->todaysDate;
1653 return TRUE;
1656 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1658 * RETURN VALUE
1660 * TRUE - today date changed
1661 * FALSE - today date isn't changed
1663 static BOOL
1664 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1666 RECT new_r, old_r;
1668 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1670 MONTHCAL_GetDayRect(infoPtr, &infoPtr->todaysDate, &old_r, -1);
1671 MONTHCAL_GetDayRect(infoPtr, today, &new_r, -1);
1673 infoPtr->todaysDate = *today;
1675 /* only two days need redrawing */
1676 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1677 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1678 /* and today label */
1679 InvalidateRect(infoPtr->hwndSelf, &infoPtr->todayrect, FALSE);
1680 return TRUE;
1683 /* MCM_SETTODAT handler */
1684 static LRESULT
1685 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1687 TRACE("%p\n", today);
1689 if (today)
1691 /* remember if date was set successfully */
1692 if (MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1695 return 0;
1698 /* returns calendar index containing specified point, or -1 if it's background */
1699 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1701 RECT r;
1702 INT i;
1704 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1706 /* whole bounding rectangle allows some optimization to compute */
1707 r.left = infoPtr->calendars[i].title.left;
1708 r.top = infoPtr->calendars[i].title.top;
1709 r.bottom = infoPtr->calendars[i].days.bottom;
1710 r.right = infoPtr->calendars[i].days.right;
1712 if (PtInRect(&r, *pt)) return i;
1715 return -1;
1718 static inline UINT fill_hittest_info(const MCHITTESTINFO *src, MCHITTESTINFO *dest)
1720 dest->uHit = src->uHit;
1721 dest->st = src->st;
1723 if (dest->cbSize == sizeof(MCHITTESTINFO))
1724 memcpy(&dest->rc, &src->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1726 return src->uHit;
1729 static LRESULT
1730 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1732 MCHITTESTINFO htinfo;
1733 SYSTEMTIME *ht_month;
1734 INT day, calIdx;
1736 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1738 htinfo.st = st_null;
1740 /* we should preserve passed fields if hit area doesn't need them */
1741 if (lpht->cbSize == sizeof(MCHITTESTINFO))
1742 memcpy(&htinfo.rc, &lpht->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1744 /* Comment in for debugging...
1745 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,
1746 infoPtr->wdays.left, infoPtr->wdays.right,
1747 infoPtr->wdays.top, infoPtr->wdays.bottom,
1748 infoPtr->days.left, infoPtr->days.right,
1749 infoPtr->days.top, infoPtr->days.bottom,
1750 infoPtr->todayrect.left, infoPtr->todayrect.right,
1751 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1752 infoPtr->weeknums.left, infoPtr->weeknums.right,
1753 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1756 /* guess in what calendar we are */
1757 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1758 if (calIdx == -1)
1760 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1762 htinfo.uHit = MCHT_TODAYLINK;
1763 htinfo.rc = infoPtr->todayrect;
1765 else
1766 /* outside of calendar area? What's left must be background :-) */
1767 htinfo.uHit = MCHT_CALENDARBK;
1769 return fill_hittest_info(&htinfo, lpht);
1772 /* are we in the header? */
1773 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1774 /* FIXME: buttons hittesting could be optimized cause maximum
1775 two calendars have buttons */
1776 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1778 htinfo.uHit = MCHT_TITLEBTNPREV;
1779 htinfo.rc = infoPtr->titlebtnprev;
1781 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1783 htinfo.uHit = MCHT_TITLEBTNNEXT;
1784 htinfo.rc = infoPtr->titlebtnnext;
1786 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1788 htinfo.uHit = MCHT_TITLEMONTH;
1789 htinfo.rc = infoPtr->calendars[calIdx].titlemonth;
1790 htinfo.iOffset = calIdx;
1792 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1794 htinfo.uHit = MCHT_TITLEYEAR;
1795 htinfo.rc = infoPtr->calendars[calIdx].titleyear;
1796 htinfo.iOffset = calIdx;
1798 else
1800 htinfo.uHit = MCHT_TITLE;
1801 htinfo.rc = infoPtr->calendars[calIdx].title;
1802 htinfo.iOffset = calIdx;
1805 return fill_hittest_info(&htinfo, lpht);
1808 ht_month = &infoPtr->calendars[calIdx].month;
1809 /* days area (including week days and week numbers) */
1810 day = MONTHCAL_GetDayFromPos(infoPtr, lpht->pt, calIdx);
1811 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1813 htinfo.uHit = MCHT_CALENDARDAY;
1814 htinfo.iOffset = calIdx;
1815 htinfo.st.wYear = ht_month->wYear;
1816 htinfo.st.wMonth = (day < 1) ? ht_month->wMonth -1 : ht_month->wMonth;
1817 htinfo.st.wDay = (day < 1) ?
1818 MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day : day;
1820 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1822 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1824 htinfo.uHit = MCHT_CALENDARWEEKNUM;
1825 htinfo.st.wYear = ht_month->wYear;
1826 htinfo.iOffset = calIdx;
1828 if (day < 1)
1830 htinfo.st.wMonth = ht_month->wMonth - 1;
1831 htinfo.st.wDay = MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day;
1833 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1835 htinfo.st.wMonth = ht_month->wMonth + 1;
1836 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1838 else
1840 htinfo.st.wMonth = ht_month->wMonth;
1841 htinfo.st.wDay = day;
1844 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1846 htinfo.iOffset = calIdx;
1847 htinfo.st.wYear = ht_month->wYear;
1848 htinfo.st.wMonth = ht_month->wMonth;
1849 /* previous month only valid for first calendar */
1850 if (day < 1 && calIdx == 0)
1852 htinfo.uHit = MCHT_CALENDARDATEPREV;
1853 MONTHCAL_GetPrevMonth(&htinfo.st);
1854 htinfo.st.wDay = MONTHCAL_MonthLength(htinfo.st.wMonth, htinfo.st.wYear) + day;
1856 /* next month only valid for last calendar */
1857 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear) &&
1858 calIdx == MONTHCAL_GetCalCount(infoPtr)-1)
1860 htinfo.uHit = MCHT_CALENDARDATENEXT;
1861 MONTHCAL_GetNextMonth(&htinfo.st);
1862 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1864 /* multiple calendars case - blank areas for previous/next month */
1865 else if (day < 1 || day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1867 htinfo.uHit = MCHT_CALENDARBK;
1869 else
1871 htinfo.uHit = MCHT_CALENDARDATE;
1872 htinfo.st.wDay = day;
1875 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1876 MONTHCAL_GetDayRectI(infoPtr, &htinfo.rc, htinfo.iCol, htinfo.iRow, calIdx);
1877 /* always update day of week */
1878 MONTHCAL_CalculateDayOfWeek(&htinfo.st, TRUE);
1881 return fill_hittest_info(&htinfo, lpht);
1884 /* MCN_GETDAYSTATE notification helper */
1885 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1887 MONTHDAYSTATE *state;
1888 NMDAYSTATE nmds;
1890 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return;
1892 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1893 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1894 nmds.nmhdr.code = MCN_GETDAYSTATE;
1895 nmds.cDayState = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0);
1896 nmds.prgDayState = state = Alloc(nmds.cDayState * sizeof(MONTHDAYSTATE));
1898 MONTHCAL_GetMinDate(infoPtr, &nmds.stStart);
1899 nmds.stStart.wDay = 1;
1901 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1902 memcpy(infoPtr->monthdayState, nmds.prgDayState,
1903 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
1905 Free(state);
1908 /* no valid range check performed */
1909 static void MONTHCAL_Scroll(MONTHCAL_INFO *infoPtr, INT delta)
1911 INT i, selIdx = -1;
1913 for(i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1915 /* save selection position to shift it later */
1916 if (selIdx == -1 && MONTHCAL_CompareMonths(&infoPtr->minSel, &infoPtr->calendars[i].month) == 0)
1917 selIdx = i;
1919 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, delta);
1922 /* selection is always shifted to first calendar */
1923 if(infoPtr->dwStyle & MCS_MULTISELECT)
1925 SYSTEMTIME range[2];
1927 MONTHCAL_GetSelRange(infoPtr, range);
1928 MONTHCAL_GetMonth(&range[0], delta - selIdx);
1929 MONTHCAL_GetMonth(&range[1], delta - selIdx);
1930 MONTHCAL_SetSelRange(infoPtr, range);
1932 else
1934 SYSTEMTIME st = infoPtr->minSel;
1936 MONTHCAL_GetMonth(&st, delta - selIdx);
1937 MONTHCAL_SetCurSel(infoPtr, &st);
1941 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
1943 INT delta = infoPtr->delta ? infoPtr->delta : MONTHCAL_GetCalCount(infoPtr);
1944 SYSTEMTIME st;
1946 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
1948 /* check if change allowed by range set */
1949 if(direction == DIRECTION_BACKWARD)
1951 st = infoPtr->calendars[0].month;
1952 MONTHCAL_GetMonth(&st, -delta);
1954 else
1956 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1957 MONTHCAL_GetMonth(&st, delta);
1960 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
1962 MONTHCAL_Scroll(infoPtr, direction == DIRECTION_BACKWARD ? -delta : delta);
1963 MONTHCAL_NotifyDayState(infoPtr);
1964 MONTHCAL_NotifySelectionChange(infoPtr);
1967 static LRESULT
1968 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1970 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1971 HMENU hMenu;
1972 POINT menupoint;
1973 WCHAR buf[32];
1975 hMenu = CreatePopupMenu();
1976 if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1978 WARN("Can't load resource\n");
1979 strcpyW(buf, todayW);
1981 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1982 menupoint.x = (short)LOWORD(lParam);
1983 menupoint.y = (short)HIWORD(lParam);
1984 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1985 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
1986 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1988 if (infoPtr->dwStyle & MCS_MULTISELECT)
1990 SYSTEMTIME range[2];
1992 range[0] = range[1] = infoPtr->todaysDate;
1993 MONTHCAL_SetSelRange(infoPtr, range);
1995 else
1996 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
1998 MONTHCAL_NotifySelectionChange(infoPtr);
1999 MONTHCAL_NotifySelect(infoPtr);
2002 return 0;
2005 /***
2006 * DESCRIPTION:
2007 * Subclassed edit control windproc function
2009 * PARAMETER(S):
2010 * [I] hwnd : the edit window handle
2011 * [I] uMsg : the message that is to be processed
2012 * [I] wParam : first message parameter
2013 * [I] lParam : second message parameter
2016 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2018 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
2020 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
2021 hwnd, uMsg, wParam, lParam);
2023 switch (uMsg)
2025 case WM_GETDLGCODE:
2026 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
2028 case WM_DESTROY:
2030 WNDPROC editProc = infoPtr->EditWndProc;
2031 infoPtr->EditWndProc = NULL;
2032 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
2033 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
2036 case WM_KILLFOCUS:
2037 break;
2039 case WM_KEYDOWN:
2040 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
2041 break;
2043 default:
2044 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
2047 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
2048 SendMessageW(hwnd, WM_CLOSE, 0, 0);
2049 return 0;
2052 /* creates updown control and edit box */
2053 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr, INT calIdx)
2055 RECT *rc = &infoPtr->calendars[calIdx].titleyear;
2056 RECT *title = &infoPtr->calendars[calIdx].title;
2058 infoPtr->hWndYearEdit =
2059 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
2060 rc->left + 3, (title->bottom + title->top - infoPtr->textHeight) / 2,
2061 rc->right - rc->left + 4,
2062 infoPtr->textHeight, infoPtr->hwndSelf,
2063 NULL, NULL, NULL);
2065 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
2067 infoPtr->hWndYearUpDown =
2068 CreateWindowExW(0, UPDOWN_CLASSW, 0,
2069 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
2070 rc->right + 7, (title->bottom + title->top - infoPtr->textHeight) / 2,
2071 18, infoPtr->textHeight, infoPtr->hwndSelf,
2072 NULL, NULL, NULL);
2074 /* attach edit box */
2075 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
2076 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
2077 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
2078 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->calendars[calIdx].month.wYear);
2080 /* subclass edit box */
2081 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
2082 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
2084 SetFocus(infoPtr->hWndYearEdit);
2087 static LRESULT
2088 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2090 MCHITTESTINFO ht;
2091 DWORD hit;
2093 /* Actually we don't need input focus for calendar, this is used to kill
2094 year updown and its buddy edit box */
2095 if (IsWindow(infoPtr->hWndYearUpDown))
2097 SetFocus(infoPtr->hwndSelf);
2098 return 0;
2101 SetCapture(infoPtr->hwndSelf);
2103 ht.cbSize = sizeof(MCHITTESTINFO);
2104 ht.pt.x = (short)LOWORD(lParam);
2105 ht.pt.y = (short)HIWORD(lParam);
2107 hit = MONTHCAL_HitTest(infoPtr, &ht);
2109 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
2111 switch(hit)
2113 case MCHT_TITLEBTNNEXT:
2114 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2115 infoPtr->status = MC_NEXTPRESSED;
2116 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2117 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2118 return 0;
2120 case MCHT_TITLEBTNPREV:
2121 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2122 infoPtr->status = MC_PREVPRESSED;
2123 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2124 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2125 return 0;
2127 case MCHT_TITLEMONTH:
2129 HMENU hMenu = CreatePopupMenu();
2130 WCHAR buf[32];
2131 POINT menupoint;
2132 INT i;
2134 for (i = 0; i < 12; i++)
2136 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
2137 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
2139 menupoint.x = ht.pt.x;
2140 menupoint.y = ht.pt.y;
2141 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2142 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
2143 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
2145 if ((i > 0) && (i < 13) && infoPtr->calendars[ht.iOffset].month.wMonth != i)
2147 INT delta = i - infoPtr->calendars[ht.iOffset].month.wMonth;
2148 SYSTEMTIME st;
2150 /* check if change allowed by range set */
2151 st = delta < 0 ? infoPtr->calendars[0].month :
2152 infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2153 MONTHCAL_GetMonth(&st, delta);
2155 if (MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE))
2157 MONTHCAL_Scroll(infoPtr, delta);
2158 MONTHCAL_NotifyDayState(infoPtr);
2159 MONTHCAL_NotifySelectionChange(infoPtr);
2160 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2163 return 0;
2165 case MCHT_TITLEYEAR:
2167 MONTHCAL_EditYear(infoPtr, ht.iOffset);
2168 return 0;
2170 case MCHT_TODAYLINK:
2172 if (infoPtr->dwStyle & MCS_MULTISELECT)
2174 SYSTEMTIME range[2];
2176 range[0] = range[1] = infoPtr->todaysDate;
2177 MONTHCAL_SetSelRange(infoPtr, range);
2179 else
2180 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2182 MONTHCAL_NotifySelectionChange(infoPtr);
2183 MONTHCAL_NotifySelect(infoPtr);
2184 return 0;
2186 case MCHT_CALENDARDATENEXT:
2187 case MCHT_CALENDARDATEPREV:
2188 case MCHT_CALENDARDATE:
2190 SYSTEMTIME st[2];
2192 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2194 st[0] = st[1] = ht.st;
2195 /* clear selection range */
2196 MONTHCAL_SetSelRange(infoPtr, st);
2198 infoPtr->status = MC_SEL_LBUTDOWN;
2199 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
2200 return 0;
2204 return 1;
2208 static LRESULT
2209 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2211 NMHDR nmhdr;
2212 MCHITTESTINFO ht;
2213 DWORD hit;
2215 TRACE("\n");
2217 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2218 RECT *r;
2220 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2221 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2222 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2224 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2227 ReleaseCapture();
2229 /* always send NM_RELEASEDCAPTURE notification */
2230 nmhdr.hwndFrom = infoPtr->hwndSelf;
2231 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2232 nmhdr.code = NM_RELEASEDCAPTURE;
2233 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2235 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2237 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2239 ht.cbSize = sizeof(MCHITTESTINFO);
2240 ht.pt.x = (short)LOWORD(lParam);
2241 ht.pt.y = (short)HIWORD(lParam);
2242 hit = MONTHCAL_HitTest(infoPtr, &ht);
2244 infoPtr->status = MC_SEL_LBUTUP;
2245 MONTHCAL_SetDayFocus(infoPtr, NULL);
2247 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2249 SYSTEMTIME sel = infoPtr->minSel;
2251 /* will be invalidated here */
2252 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2254 /* send MCN_SELCHANGE only if new date selected */
2255 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2256 MONTHCAL_NotifySelectionChange(infoPtr);
2258 MONTHCAL_NotifySelect(infoPtr);
2261 return 0;
2265 static LRESULT
2266 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2268 TRACE("%ld\n", id);
2270 switch(id) {
2271 case MC_PREVNEXTMONTHTIMER:
2272 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2273 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2274 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2275 break;
2276 case MC_TODAYUPDATETIMER:
2278 SYSTEMTIME st;
2280 if(infoPtr->todaySet) return 0;
2282 GetLocalTime(&st);
2283 MONTHCAL_UpdateToday(infoPtr, &st);
2285 /* notification sent anyway */
2286 MONTHCAL_NotifySelectionChange(infoPtr);
2288 return 0;
2290 default:
2291 ERR("got unknown timer %ld\n", id);
2292 break;
2295 return 0;
2299 static LRESULT
2300 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2302 MCHITTESTINFO ht;
2303 SYSTEMTIME st_ht;
2304 INT hit;
2305 RECT r;
2307 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2309 ht.cbSize = sizeof(MCHITTESTINFO);
2310 ht.pt.x = (short)LOWORD(lParam);
2311 ht.pt.y = (short)HIWORD(lParam);
2312 ht.iOffset = -1;
2314 hit = MONTHCAL_HitTest(infoPtr, &ht);
2316 /* not on the calendar date numbers? bail out */
2317 TRACE("hit:%x\n",hit);
2318 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2320 MONTHCAL_SetDayFocus(infoPtr, NULL);
2321 return 0;
2324 st_ht = ht.st;
2326 /* if pointer is over focused day still there's nothing to do */
2327 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2329 MONTHCAL_GetDayRect(infoPtr, &ht.st, &r, ht.iOffset);
2331 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2332 SYSTEMTIME st[2];
2334 MONTHCAL_GetSelRange(infoPtr, st);
2336 /* If we're still at the first selected date and range is empty, return.
2337 If range isn't empty we should change range to a single firstSel */
2338 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2339 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2341 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2343 st[0] = infoPtr->firstSel;
2344 /* we should overwrite timestamp here */
2345 MONTHCAL_CopyDate(&st_ht, &st[1]);
2347 /* bounds will be swapped here if needed */
2348 MONTHCAL_SetSelRange(infoPtr, st);
2350 return 0;
2353 done:
2355 /* FIXME: this should specify a rectangle containing only the days that changed
2356 using InvalidateRect */
2357 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2359 return 0;
2363 static LRESULT
2364 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2366 HDC hdc;
2367 PAINTSTRUCT ps;
2369 if (hdc_paint)
2371 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2372 hdc = hdc_paint;
2374 else
2375 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2377 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2378 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2379 return 0;
2382 static LRESULT
2383 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2385 RECT rc;
2387 if (!GetClipBox(hdc, &rc)) return FALSE;
2389 FillRect(hdc, &rc, infoPtr->brushes[BrushBackground]);
2391 return TRUE;
2394 static LRESULT
2395 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2397 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2399 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2400 return 0;
2402 if (options & PRF_ERASEBKGND)
2403 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2405 if (options & PRF_CLIENT)
2406 MONTHCAL_Paint(infoPtr, hdc);
2408 return 0;
2411 static LRESULT
2412 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2414 TRACE("\n");
2416 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2418 return 0;
2421 /* sets the size information */
2422 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2424 static const WCHAR O0W[] = { '0','0',0 };
2425 RECT *title=&infoPtr->calendars[0].title;
2426 RECT *prev=&infoPtr->titlebtnprev;
2427 RECT *next=&infoPtr->titlebtnnext;
2428 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2429 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2430 RECT *wdays=&infoPtr->calendars[0].wdays;
2431 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2432 RECT *days=&infoPtr->calendars[0].days;
2433 RECT *todayrect=&infoPtr->todayrect;
2435 INT xdiv, dx, dy, i, j, x, y, c_dx, c_dy;
2436 WCHAR buff[80];
2437 TEXTMETRICW tm;
2438 SIZE size, sz;
2439 RECT client;
2440 HFONT font;
2441 HDC hdc;
2443 GetClientRect(infoPtr->hwndSelf, &client);
2445 hdc = GetDC(infoPtr->hwndSelf);
2446 font = SelectObject(hdc, infoPtr->hFont);
2448 /* get the height and width of each day's text */
2449 GetTextMetricsW(hdc, &tm);
2450 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2452 /* find largest abbreviated day name for current locale */
2453 size.cx = sz.cx = 0;
2454 for (i = 0; i < 7; i++)
2456 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + i,
2457 buff, countof(buff)))
2459 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2460 if (sz.cx > size.cx) size.cx = sz.cx;
2462 else /* locale independent fallback on failure */
2464 static const WCHAR SunW[] = { 'S','u','n',0 };
2466 GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2467 break;
2471 infoPtr->textWidth = size.cx + 2;
2473 /* recalculate the height and width increments and offsets */
2474 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2476 /* restore the originally selected font */
2477 SelectObject(hdc, font);
2478 ReleaseDC(infoPtr->hwndSelf, hdc);
2480 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2482 infoPtr->width_increment = size.cx * 2 + 4;
2483 infoPtr->height_increment = infoPtr->textHeight;
2485 /* calculate title area */
2486 title->top = 0;
2487 title->bottom = 3 * infoPtr->height_increment / 2;
2488 title->left = 0;
2489 title->right = infoPtr->width_increment * xdiv;
2491 /* set the dimensions of the next and previous buttons and center */
2492 /* the month text vertically */
2493 prev->top = next->top = title->top + 4;
2494 prev->bottom = next->bottom = title->bottom - 4;
2495 prev->left = title->left + 4;
2496 prev->right = prev->left + (title->bottom - title->top);
2497 next->right = title->right - 4;
2498 next->left = next->right - (title->bottom - title->top);
2500 /* titlemonth->left and right change based upon the current month
2501 and are recalculated in refresh as the current month may change
2502 without the control being resized */
2503 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2504 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2506 /* week numbers */
2507 weeknumrect->left = 0;
2508 weeknumrect->right = infoPtr->dwStyle & MCS_WEEKNUMBERS ? prev->right : 0;
2510 /* days abbreviated names */
2511 wdays->left = days->left = weeknumrect->right;
2512 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2513 wdays->top = title->bottom;
2514 wdays->bottom = wdays->top + infoPtr->height_increment;
2516 days->top = weeknumrect->top = wdays->bottom;
2517 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2519 todayrect->left = 0;
2520 todayrect->right = title->right;
2521 todayrect->top = days->bottom;
2522 todayrect->bottom = days->bottom + infoPtr->height_increment;
2524 /* compute calendar count, update all calendars */
2525 x = (client.right + MC_CALENDAR_PADDING) / (title->right - title->left + MC_CALENDAR_PADDING);
2526 /* today label affects whole height */
2527 if (infoPtr->dwStyle & MCS_NOTODAY)
2528 y = (client.bottom + MC_CALENDAR_PADDING) / (days->bottom - title->top + MC_CALENDAR_PADDING);
2529 else
2530 y = (client.bottom - todayrect->bottom + todayrect->top + MC_CALENDAR_PADDING) /
2531 (days->bottom - title->top + MC_CALENDAR_PADDING);
2533 /* TODO: ensure that count is properly adjusted to fit 12 months constraint */
2534 if (x == 0) x = 1;
2535 if (y == 0) y = 1;
2537 if (x*y != MONTHCAL_GetCalCount(infoPtr))
2539 infoPtr->dim.cx = x;
2540 infoPtr->dim.cy = y;
2541 infoPtr->calendars = ReAlloc(infoPtr->calendars, MONTHCAL_GetCalCount(infoPtr)*sizeof(CALENDAR_INFO));
2543 infoPtr->monthdayState = ReAlloc(infoPtr->monthdayState,
2544 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
2545 MONTHCAL_NotifyDayState(infoPtr);
2548 for (i = 1; i < MONTHCAL_GetCalCount(infoPtr); i++)
2550 /* set months */
2551 infoPtr->calendars[i] = infoPtr->calendars[0];
2552 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, i);
2555 /* offset all rectangles to center in client area */
2556 c_dx = (client.right - x * title->right - MC_CALENDAR_PADDING * (x-1)) / 2;
2557 c_dy = (client.bottom - y * todayrect->bottom - MC_CALENDAR_PADDING * (y-1)) / 2;
2559 /* if calendar doesn't fit client area show it at left/top bounds */
2560 if (title->left + c_dx < 0) c_dx = 0;
2561 if (title->top + c_dy < 0) c_dy = 0;
2563 for (i = 0; i < y; i++)
2565 for (j = 0; j < x; j++)
2567 dx = j*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx;
2568 dy = i*(days->bottom - title->top + MC_CALENDAR_PADDING) + c_dy;
2570 OffsetRect(&infoPtr->calendars[i*x+j].title, dx, dy);
2571 OffsetRect(&infoPtr->calendars[i*x+j].titlemonth, dx, dy);
2572 OffsetRect(&infoPtr->calendars[i*x+j].titleyear, dx, dy);
2573 OffsetRect(&infoPtr->calendars[i*x+j].wdays, dx, dy);
2574 OffsetRect(&infoPtr->calendars[i*x+j].weeknums, dx, dy);
2575 OffsetRect(&infoPtr->calendars[i*x+j].days, dx, dy);
2579 OffsetRect(prev, c_dx, c_dy);
2580 OffsetRect(next, (x-1)*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx, c_dy);
2582 i = infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx;
2583 todayrect->left = infoPtr->calendars[i].title.left;
2584 todayrect->right = infoPtr->calendars[i].title.right;
2585 todayrect->top = infoPtr->calendars[i].days.bottom;
2586 todayrect->bottom = infoPtr->calendars[i].days.bottom + infoPtr->height_increment;
2588 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2589 infoPtr->width_increment,infoPtr->height_increment,
2590 wine_dbgstr_rect(&client),
2591 wine_dbgstr_rect(title),
2592 wine_dbgstr_rect(wdays),
2593 wine_dbgstr_rect(days),
2594 wine_dbgstr_rect(todayrect));
2597 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2599 TRACE("(width=%d, height=%d)\n", Width, Height);
2601 MONTHCAL_UpdateSize(infoPtr);
2602 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2604 return 0;
2607 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2609 return (LRESULT)infoPtr->hFont;
2612 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2614 HFONT hOldFont;
2615 LOGFONTW lf;
2617 if (!hFont) return 0;
2619 hOldFont = infoPtr->hFont;
2620 infoPtr->hFont = hFont;
2622 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2623 lf.lfWeight = FW_BOLD;
2624 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2626 MONTHCAL_UpdateSize(infoPtr);
2628 if (redraw)
2629 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2631 return (LRESULT)hOldFont;
2634 /* update theme after a WM_THEMECHANGED message */
2635 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2637 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2638 CloseThemeData (theme);
2639 OpenThemeData (infoPtr->hwndSelf, themeClass);
2640 return 0;
2643 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2644 const STYLESTRUCT *lpss)
2646 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2647 wStyleType, lpss->styleOld, lpss->styleNew);
2649 if (wStyleType != GWL_STYLE) return 0;
2651 infoPtr->dwStyle = lpss->styleNew;
2653 /* make room for week numbers */
2654 if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2655 MONTHCAL_UpdateSize(infoPtr);
2657 return 0;
2660 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2661 STYLESTRUCT *lpss)
2663 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2664 wStyleType, lpss->styleOld, lpss->styleNew);
2666 /* block MCS_MULTISELECT change */
2667 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2669 if (lpss->styleOld & MCS_MULTISELECT)
2670 lpss->styleNew |= MCS_MULTISELECT;
2671 else
2672 lpss->styleNew &= ~MCS_MULTISELECT;
2675 /* block MCS_DAYSTATE change */
2676 if ((lpss->styleNew ^ lpss->styleOld) & MCS_DAYSTATE)
2678 if (lpss->styleOld & MCS_DAYSTATE)
2679 lpss->styleNew |= MCS_DAYSTATE;
2680 else
2681 lpss->styleNew &= ~MCS_DAYSTATE;
2684 return 0;
2687 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2688 static LRESULT
2689 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2691 MONTHCAL_INFO *infoPtr;
2693 /* allocate memory for info structure */
2694 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2695 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2697 if (infoPtr == NULL) {
2698 ERR("could not allocate info memory!\n");
2699 return 0;
2702 infoPtr->hwndSelf = hwnd;
2703 infoPtr->hwndNotify = lpcs->hwndParent;
2704 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2705 infoPtr->dim.cx = infoPtr->dim.cy = 1;
2706 infoPtr->calendars = Alloc(sizeof(CALENDAR_INFO));
2707 if (!infoPtr->calendars) goto fail;
2708 infoPtr->monthdayState = Alloc(3*sizeof(MONTHDAYSTATE));
2709 if (!infoPtr->monthdayState) goto fail;
2711 /* initialize info structure */
2712 /* FIXME: calculate systemtime ->> localtime(subtract timezoneinfo) */
2714 GetLocalTime(&infoPtr->todaysDate);
2715 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2717 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2719 infoPtr->colors[MCSC_BACKGROUND] = comctl32_color.clrWindow;
2720 infoPtr->colors[MCSC_TEXT] = comctl32_color.clrWindowText;
2721 infoPtr->colors[MCSC_TITLEBK] = comctl32_color.clrActiveCaption;
2722 infoPtr->colors[MCSC_TITLETEXT] = comctl32_color.clrWindow;
2723 infoPtr->colors[MCSC_MONTHBK] = comctl32_color.clrWindow;
2724 infoPtr->colors[MCSC_TRAILINGTEXT] = comctl32_color.clrGrayText;
2726 infoPtr->brushes[BrushBackground] = CreateSolidBrush(infoPtr->colors[MCSC_BACKGROUND]);
2727 infoPtr->brushes[BrushTitle] = CreateSolidBrush(infoPtr->colors[MCSC_TITLEBK]);
2728 infoPtr->brushes[BrushMonth] = CreateSolidBrush(infoPtr->colors[MCSC_MONTHBK]);
2730 infoPtr->pens[PenRed] = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
2731 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[MCSC_TEXT]);
2733 infoPtr->minSel = infoPtr->todaysDate;
2734 infoPtr->maxSel = infoPtr->todaysDate;
2735 infoPtr->calendars[0].month = infoPtr->todaysDate;
2736 infoPtr->isUnicode = TRUE;
2738 /* setup control layout and day state data */
2739 MONTHCAL_UpdateSize(infoPtr);
2741 /* today auto update timer, to be freed only on control destruction */
2742 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2744 OpenThemeData (infoPtr->hwndSelf, themeClass);
2746 return 0;
2748 fail:
2749 Free(infoPtr->monthdayState);
2750 Free(infoPtr->calendars);
2751 Free(infoPtr);
2752 return 0;
2755 static LRESULT
2756 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2758 INT i;
2760 /* free month calendar info data */
2761 Free(infoPtr->monthdayState);
2762 Free(infoPtr->calendars);
2763 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2765 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2767 for (i = 0; i < BrushLast; i++) DeleteObject(infoPtr->brushes[i]);
2768 for (i = 0; i < PenLast; i++) DeleteObject(infoPtr->pens[i]);
2770 Free(infoPtr);
2771 return 0;
2775 * Handler for WM_NOTIFY messages
2777 static LRESULT
2778 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2780 /* notification from year edit updown */
2781 if (hdr->code == UDN_DELTAPOS)
2783 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2785 if (hdr->hwndFrom == infoPtr->hWndYearUpDown && nmud->iDelta)
2787 /* year value limits are set up explicitly after updown creation */
2788 MONTHCAL_Scroll(infoPtr, 12 * nmud->iDelta);
2789 MONTHCAL_NotifyDayState(infoPtr);
2790 MONTHCAL_NotifySelectionChange(infoPtr);
2793 return 0;
2796 static inline BOOL
2797 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2799 BOOL prev = infoPtr->isUnicode;
2800 infoPtr->isUnicode = isUnicode;
2801 return prev;
2804 static inline BOOL
2805 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2807 return infoPtr->isUnicode;
2810 static LRESULT WINAPI
2811 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2813 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0);
2815 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2817 if (!infoPtr && (uMsg != WM_CREATE))
2818 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2819 switch(uMsg)
2821 case MCM_GETCURSEL:
2822 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2824 case MCM_SETCURSEL:
2825 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2827 case MCM_GETMAXSELCOUNT:
2828 return MONTHCAL_GetMaxSelCount(infoPtr);
2830 case MCM_SETMAXSELCOUNT:
2831 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2833 case MCM_GETSELRANGE:
2834 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2836 case MCM_SETSELRANGE:
2837 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2839 case MCM_GETMONTHRANGE:
2840 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2842 case MCM_SETDAYSTATE:
2843 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2845 case MCM_GETMINREQRECT:
2846 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2848 case MCM_GETCOLOR:
2849 return MONTHCAL_GetColor(infoPtr, wParam);
2851 case MCM_SETCOLOR:
2852 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2854 case MCM_GETTODAY:
2855 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2857 case MCM_SETTODAY:
2858 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2860 case MCM_HITTEST:
2861 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2863 case MCM_GETFIRSTDAYOFWEEK:
2864 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2866 case MCM_SETFIRSTDAYOFWEEK:
2867 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2869 case MCM_GETRANGE:
2870 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2872 case MCM_SETRANGE:
2873 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2875 case MCM_GETMONTHDELTA:
2876 return MONTHCAL_GetMonthDelta(infoPtr);
2878 case MCM_SETMONTHDELTA:
2879 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2881 case MCM_GETMAXTODAYWIDTH:
2882 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2884 case MCM_SETUNICODEFORMAT:
2885 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2887 case MCM_GETUNICODEFORMAT:
2888 return MONTHCAL_GetUnicodeFormat(infoPtr);
2890 case MCM_GETCALENDARCOUNT:
2891 return MONTHCAL_GetCalCount(infoPtr);
2893 case WM_GETDLGCODE:
2894 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2896 case WM_RBUTTONUP:
2897 return MONTHCAL_RButtonUp(infoPtr, lParam);
2899 case WM_LBUTTONDOWN:
2900 return MONTHCAL_LButtonDown(infoPtr, lParam);
2902 case WM_MOUSEMOVE:
2903 return MONTHCAL_MouseMove(infoPtr, lParam);
2905 case WM_LBUTTONUP:
2906 return MONTHCAL_LButtonUp(infoPtr, lParam);
2908 case WM_PAINT:
2909 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2911 case WM_PRINTCLIENT:
2912 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2914 case WM_ERASEBKGND:
2915 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2917 case WM_SETFOCUS:
2918 return MONTHCAL_SetFocus(infoPtr);
2920 case WM_SIZE:
2921 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2923 case WM_NOTIFY:
2924 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2926 case WM_CREATE:
2927 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2929 case WM_SETFONT:
2930 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2932 case WM_GETFONT:
2933 return MONTHCAL_GetFont(infoPtr);
2935 case WM_TIMER:
2936 return MONTHCAL_Timer(infoPtr, wParam);
2938 case WM_THEMECHANGED:
2939 return theme_changed (infoPtr);
2941 case WM_DESTROY:
2942 return MONTHCAL_Destroy(infoPtr);
2944 case WM_SYSCOLORCHANGE:
2945 COMCTL32_RefreshSysColors();
2946 return 0;
2948 case WM_STYLECHANGED:
2949 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2951 case WM_STYLECHANGING:
2952 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2954 default:
2955 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2956 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2957 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2962 void
2963 MONTHCAL_Register(void)
2965 WNDCLASSW wndClass;
2967 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2968 wndClass.style = CS_GLOBALCLASS;
2969 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2970 wndClass.cbClsExtra = 0;
2971 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2972 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2973 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2974 wndClass.lpszClassName = MONTHCAL_CLASSW;
2976 RegisterClassW(&wndClass);
2980 void
2981 MONTHCAL_Unregister(void)
2983 UnregisterClassW(MONTHCAL_CLASSW, NULL);