reg: Do not allow combinations of /v, /ve or /va in the 'delete' function.
[wine.git] / dlls / comctl32 / monthcal.c
blob5accbe60e2058ecefa100fd55886ecb592792ff2
1 /*
2 * Month calendar control
4 * Copyright 1998, 1999 Eric Kohl (ekohl@abo.rhein-zeitung.de)
5 * Copyright 1999 Alex Priem (alexp@sci.kun.nl)
6 * Copyright 1999 Chris Morgan <cmorgan@wpi.edu> and
7 * James Abbatiello <abbeyj@wpi.edu>
8 * Copyright 2000 Uwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de>
9 * Copyright 2009-2011 Nikolay Sivov
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * NOTE
27 * This code was audited for completeness against the documented features
28 * of Comctl32.dll version 6.0 on Oct. 20, 2004, by Dimitrie O. Paun.
30 * Unless otherwise noted, we believe this code to be complete, as per
31 * the specification mentioned above.
32 * If you discover missing features, or bugs, please note them below.
34 * TODO:
35 * -- MCM_[GS]ETUNICODEFORMAT
36 * -- handle resources better (doesn't work now);
37 * -- take care of internationalization.
38 * -- keyboard handling.
39 * -- search for FIXME
42 #include <math.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "commctrl.h"
54 #include "comctl32.h"
55 #include "uxtheme.h"
56 #include "vssym32.h"
57 #include "wine/unicode.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
62 #define MC_SEL_LBUTUP 1 /* Left button released */
63 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
64 #define MC_PREVPRESSED 4 /* Prev month button pressed */
65 #define MC_NEXTPRESSED 8 /* Next month button pressed */
66 #define MC_PREVNEXTMONTHDELAY 350 /* when continuously pressing `next/prev
67 month', wait 350 ms before going
68 to the next/prev month */
69 #define MC_TODAYUPDATEDELAY 120000 /* time between today check for update (2 min) */
71 #define MC_PREVNEXTMONTHTIMER 1 /* Timer IDs */
72 #define MC_TODAYUPDATETIMER 2
74 #define MC_CALENDAR_PADDING 6
76 #define countof(arr) (sizeof(arr)/sizeof(arr[0]))
78 /* convert from days to 100 nanoseconds unit - used as FILETIME unit */
79 #define DAYSTO100NSECS(days) (((ULONGLONG)(days))*24*60*60*10000000)
81 enum CachedPen
83 PenRed = 0,
84 PenText,
85 PenLast
88 enum CachedBrush
90 BrushTitle = 0,
91 BrushMonth,
92 BrushBackground,
93 BrushLast
96 /* single calendar data */
97 typedef struct _CALENDAR_INFO
99 RECT title; /* rect for the header above the calendar */
100 RECT titlemonth; /* the 'month name' text in the header */
101 RECT titleyear; /* the 'year number' text in the header */
102 RECT wdays; /* week days at top */
103 RECT days; /* calendar area */
104 RECT weeknums; /* week numbers at left side */
106 SYSTEMTIME month;/* contains calendar main month/year */
107 } CALENDAR_INFO;
109 typedef struct
111 HWND hwndSelf;
112 DWORD dwStyle; /* cached GWL_STYLE */
114 COLORREF colors[MCSC_TRAILINGTEXT+1];
115 HBRUSH brushes[BrushLast];
116 HPEN pens[PenLast];
118 HFONT hFont;
119 HFONT hBoldFont;
120 int textHeight;
121 int textWidth;
122 int height_increment;
123 int width_increment;
124 INT delta; /* scroll rate; # of months that the */
125 /* control moves when user clicks a scroll button */
126 int firstDay; /* Start month calendar with firstDay's day,
127 stored in SYSTEMTIME format */
128 BOOL firstDaySet; /* first week day differs from locale defined */
130 BOOL isUnicode; /* value set with MCM_SETUNICODE format */
132 MONTHDAYSTATE *monthdayState;
133 SYSTEMTIME todaysDate;
134 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
135 int status; /* See MC_SEL flags */
136 SYSTEMTIME firstSel; /* first selected day */
137 INT maxSelCount;
138 SYSTEMTIME minSel; /* contains single selection when used without MCS_MULTISELECT */
139 SYSTEMTIME maxSel;
140 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
141 DWORD rangeValid;
142 SYSTEMTIME minDate;
143 SYSTEMTIME maxDate;
145 RECT titlebtnnext; /* the `next month' button in the header */
146 RECT titlebtnprev; /* the `prev month' button in the header */
147 RECT todayrect; /* `today: xx/xx/xx' text rect */
148 HWND hwndNotify; /* Window to receive the notifications */
149 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
150 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
151 WNDPROC EditWndProc; /* original Edit window procedure */
153 CALENDAR_INFO *calendars;
154 SIZE dim; /* [cx,cy] - dimensions of calendars matrix, row/column count */
155 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
157 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
159 /* empty SYSTEMTIME const */
160 static const SYSTEMTIME st_null;
161 /* valid date limits */
162 static const SYSTEMTIME max_allowed_date = { /* wYear */ 9999, /* wMonth */ 12, /* wDayOfWeek */ 0, /* wDay */ 31 };
163 static const SYSTEMTIME min_allowed_date = { /* wYear */ 1752, /* wMonth */ 9, /* wDayOfWeek */ 0, /* wDay */ 14 };
165 /* Prev/Next buttons */
166 enum nav_direction
168 DIRECTION_BACKWARD,
169 DIRECTION_FORWARD
172 /* helper functions */
173 static inline INT MONTHCAL_GetCalCount(const MONTHCAL_INFO *infoPtr)
175 return infoPtr->dim.cx * infoPtr->dim.cy;
178 /* send a single MCN_SELCHANGE notification */
179 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
181 NMSELCHANGE nmsc;
183 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
184 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
185 nmsc.nmhdr.code = MCN_SELCHANGE;
186 nmsc.stSelStart = infoPtr->minSel;
187 nmsc.stSelStart.wDayOfWeek = 0;
188 if(infoPtr->dwStyle & MCS_MULTISELECT){
189 nmsc.stSelEnd = infoPtr->maxSel;
190 nmsc.stSelEnd.wDayOfWeek = 0;
192 else
193 nmsc.stSelEnd = st_null;
195 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
198 /* send a single MCN_SELECT notification */
199 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
201 NMSELCHANGE nmsc;
203 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
204 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
205 nmsc.nmhdr.code = MCN_SELECT;
206 nmsc.stSelStart = infoPtr->minSel;
207 nmsc.stSelStart.wDayOfWeek = 0;
208 if(infoPtr->dwStyle & MCS_MULTISELECT){
209 nmsc.stSelEnd = infoPtr->maxSel;
210 nmsc.stSelEnd.wDayOfWeek = 0;
212 else
213 nmsc.stSelEnd = st_null;
215 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
218 static inline int MONTHCAL_MonthDiff(const SYSTEMTIME *left, const SYSTEMTIME *right)
220 return (right->wYear - left->wYear)*12 + right->wMonth - left->wMonth;
223 /* returns the number of days in any given month, checking for leap days */
224 /* January is 1, December is 12 */
225 int MONTHCAL_MonthLength(int month, int year)
227 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
228 /* Wrap around, this eases handling. Getting length only we shouldn't care
229 about year change here cause January and December have
230 the same day quantity */
231 if(month == 0)
232 month = 12;
233 else if(month == 13)
234 month = 1;
236 /* special case for calendar transition year */
237 if(month == min_allowed_date.wMonth && year == min_allowed_date.wYear) return 19;
239 /* if we have a leap year add 1 day to February */
240 /* a leap year is a year either divisible by 400 */
241 /* or divisible by 4 and not by 100 */
242 if(month == 2) { /* February */
243 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
244 (year%4 == 0)) ? 1 : 0);
246 else {
247 return mdays[month - 1];
251 /* compares timestamps using date part only */
252 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
254 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
255 (first->wDay == second->wDay);
258 /* make sure that date fields are valid */
259 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
261 if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
262 if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear)) return FALSE;
264 return TRUE;
267 /* Copies timestamp part only.
269 * PARAMETERS
271 * [I] from : source date
272 * [O] to : dest date
274 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
276 to->wHour = from->wHour;
277 to->wMinute = from->wMinute;
278 to->wSecond = from->wSecond;
281 /* Copies date part only.
283 * PARAMETERS
285 * [I] from : source date
286 * [O] to : dest date
288 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
290 to->wYear = from->wYear;
291 to->wMonth = from->wMonth;
292 to->wDay = from->wDay;
293 to->wDayOfWeek = from->wDayOfWeek;
296 /* Compares two dates in SYSTEMTIME format
298 * PARAMETERS
300 * [I] first : pointer to valid first date data to compare
301 * [I] second : pointer to valid second date data to compare
303 * RETURN VALUE
305 * -1 : first < second
306 * 0 : first == second
307 * 1 : first > second
309 * Note that no date validation performed, already validated values expected.
311 LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
313 FILETIME ft_first, ft_second;
315 SystemTimeToFileTime(first, &ft_first);
316 SystemTimeToFileTime(second, &ft_second);
318 return CompareFileTime(&ft_first, &ft_second);
321 static LONG MONTHCAL_CompareMonths(const SYSTEMTIME *first, const SYSTEMTIME *second)
323 SYSTEMTIME st_first, st_second;
325 st_first = st_second = st_null;
326 MONTHCAL_CopyDate(first, &st_first);
327 MONTHCAL_CopyDate(second, &st_second);
328 st_first.wDay = st_second.wDay = 1;
330 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
333 static LONG MONTHCAL_CompareDate(const SYSTEMTIME *first, const SYSTEMTIME *second)
335 SYSTEMTIME st_first, st_second;
337 st_first = st_second = st_null;
338 MONTHCAL_CopyDate(first, &st_first);
339 MONTHCAL_CopyDate(second, &st_second);
341 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
344 /* Checks largest possible date range and configured one
346 * PARAMETERS
348 * [I] infoPtr : valid pointer to control data
349 * [I] date : pointer to valid date data to check
350 * [I] fix : make date fit valid range
352 * RETURN VALUE
354 * TRUE - date within largest and configured range
355 * FALSE - date is outside largest or configured range
357 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr,
358 SYSTEMTIME *date, BOOL fix)
360 const SYSTEMTIME *fix_st = NULL;
362 if(MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) {
363 fix_st = &max_allowed_date;
365 else if(MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1) {
366 fix_st = &min_allowed_date;
368 else {
369 if(infoPtr->rangeValid & GDTR_MAX) {
370 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
371 fix_st = &infoPtr->maxDate;
375 if(infoPtr->rangeValid & GDTR_MIN) {
376 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
377 fix_st = &infoPtr->minDate;
382 if (fix && fix_st) {
383 date->wYear = fix_st->wYear;
384 date->wMonth = fix_st->wMonth;
387 return !fix_st;
390 /* Checks passed range width with configured maximum selection count
392 * PARAMETERS
394 * [I] infoPtr : valid pointer to control data
395 * [I] range0 : pointer to valid date data (requested bound)
396 * [I] range1 : pointer to valid date data (primary bound)
397 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
399 * Adjust value computed basing on primary bound and current maximum selection
400 * count. For simple range check (without adjusted value required) (range0, range1)
401 * relation means nothing.
403 * RETURN VALUE
405 * TRUE - range is shorter or equal to maximum
406 * FALSE - range is larger than maximum
408 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
409 const SYSTEMTIME *range0,
410 const SYSTEMTIME *range1,
411 SYSTEMTIME *adjust)
413 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
414 FILETIME ft_range0, ft_range1;
415 LONG cmp;
417 SystemTimeToFileTime(range0, &ft_range0);
418 SystemTimeToFileTime(range1, &ft_range1);
420 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
421 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
422 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
423 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
425 cmp = CompareFileTime(&ft_range0, &ft_range1);
427 if(cmp == 1)
428 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
429 else
430 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
432 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
434 if(adjust) {
435 if(cmp == 1)
436 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
437 else
438 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
440 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
441 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
442 FileTimeToSystemTime(&ft_range0, adjust);
445 return FALSE;
447 else return TRUE;
450 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
451 Milliseconds are intentionally not validated. */
452 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
454 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
455 return FALSE;
456 else
457 return TRUE;
460 /* Note:Depending on DST, this may be offset by a day.
461 Need to find out if we're on a DST place & adjust the clock accordingly.
462 Above function assumes we have a valid data.
463 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
464 0 = Sunday.
467 /* Returns the day in the week
469 * PARAMETERS
470 * [i] date : input date
471 * [I] inplace : set calculated value back to date structure
473 * RETURN VALUE
474 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
476 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
478 SYSTEMTIME st = st_null;
479 FILETIME ft;
481 MONTHCAL_CopyDate(date, &st);
483 SystemTimeToFileTime(&st, &ft);
484 FileTimeToSystemTime(&ft, &st);
486 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
488 return st.wDayOfWeek;
491 /* add/subtract 'months' from date */
492 static inline void MONTHCAL_GetMonth(SYSTEMTIME *date, INT months)
494 INT length, m = date->wMonth + months;
496 date->wYear += m > 0 ? (m - 1) / 12 : m / 12 - 1;
497 date->wMonth = m > 0 ? (m - 1) % 12 + 1 : 12 + m % 12;
498 /* fix moving from last day in a month */
499 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
500 if(date->wDay > length) date->wDay = length;
501 MONTHCAL_CalculateDayOfWeek(date, TRUE);
504 /* properly updates date to point on next month */
505 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
507 MONTHCAL_GetMonth(date, 1);
510 /* properly updates date to point on prev month */
511 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
513 MONTHCAL_GetMonth(date, -1);
516 /* Returns full date for a first currently visible day */
517 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
519 /* zero indexed calendar has the earliest date */
520 SYSTEMTIME st_first = infoPtr->calendars[0].month;
521 INT firstDay;
523 st_first.wDay = 1;
524 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
526 *date = infoPtr->calendars[0].month;
527 MONTHCAL_GetPrevMonth(date);
529 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
530 (infoPtr->firstDay - firstDay) % 7 + 1;
532 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
533 date->wDay -= 7;
535 /* fix day of week */
536 MONTHCAL_CalculateDayOfWeek(date, TRUE);
539 /* Returns full date for a last currently visible day */
540 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
542 /* the latest date is in latest calendar */
543 SYSTEMTIME st, *lt_month = &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
544 INT first_day;
546 *date = *lt_month;
547 st = *lt_month;
549 /* day of week of first day of current month */
550 st.wDay = 1;
551 first_day = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
553 MONTHCAL_GetNextMonth(date);
554 MONTHCAL_GetPrevMonth(&st);
556 /* last calendar starts with some date from previous month that not displayed */
557 st.wDay = MONTHCAL_MonthLength(st.wMonth, st.wYear) +
558 (infoPtr->firstDay - first_day) % 7 + 1;
559 if (st.wDay > MONTHCAL_MonthLength(st.wMonth, st.wYear)) st.wDay -= 7;
561 /* Use month length to get max day. 42 means max day count in calendar area */
562 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
563 MONTHCAL_MonthLength(lt_month->wMonth, lt_month->wYear);
565 /* fix day of week */
566 MONTHCAL_CalculateDayOfWeek(date, TRUE);
569 /* From a given point calculate the row, column and day in the calendar,
570 'day == 0' means the last day of the last month. */
571 static int MONTHCAL_GetDayFromPos(const MONTHCAL_INFO *infoPtr, POINT pt, INT calIdx)
573 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
574 int firstDay, col, row;
575 RECT client;
577 GetClientRect(infoPtr->hwndSelf, &client);
579 /* if the point is outside the x bounds of the window put it at the boundary */
580 if (pt.x > client.right) pt.x = client.right;
582 col = (pt.x - infoPtr->calendars[calIdx].days.left ) / infoPtr->width_increment;
583 row = (pt.y - infoPtr->calendars[calIdx].days.top ) / infoPtr->height_increment;
585 st.wDay = 1;
586 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
587 return col + 7 * row - firstDay;
590 /* Get day position for given date and calendar
592 * PARAMETERS
594 * [I] infoPtr : pointer to control data
595 * [I] date : date value
596 * [O] col : day column (zero based)
597 * [O] row : week column (zero based)
598 * [I] calIdx : calendar index
600 static void MONTHCAL_GetDayPos(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
601 INT *col, INT *row, INT calIdx)
603 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
604 INT first;
606 st.wDay = 1;
607 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
609 if (calIdx == 0 || calIdx == MONTHCAL_GetCalCount(infoPtr)-1) {
610 const SYSTEMTIME *cal = &infoPtr->calendars[calIdx].month;
611 LONG cmp = MONTHCAL_CompareMonths(date, &st);
613 /* previous month */
614 if (cmp == -1) {
615 *col = (first - MONTHCAL_MonthLength(date->wMonth, cal->wYear) + date->wDay) % 7;
616 *row = 0;
617 return;
620 /* next month calculation is same as for current, just add current month length */
621 if (cmp == 1)
622 first += MONTHCAL_MonthLength(cal->wMonth, cal->wYear);
625 *col = (date->wDay + first) % 7;
626 *row = (date->wDay + first - *col) / 7;
629 /* returns bounding box for day in given position in given calendar */
630 static inline void MONTHCAL_GetDayRectI(const MONTHCAL_INFO *infoPtr, RECT *r,
631 INT col, INT row, INT calIdx)
633 r->left = infoPtr->calendars[calIdx].days.left + col * infoPtr->width_increment;
634 r->right = r->left + infoPtr->width_increment;
635 r->top = infoPtr->calendars[calIdx].days.top + row * infoPtr->height_increment;
636 r->bottom = r->top + infoPtr->textHeight;
639 /* Returns bounding box for given date
641 * NOTE: when calendar index is unknown pass -1
643 static inline void MONTHCAL_GetDayRect(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
644 RECT *r, INT calIdx)
646 INT col, row;
648 if (calIdx == -1)
650 INT cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[0].month);
652 if (cmp <= 0)
653 calIdx = 0;
654 else
656 cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month);
657 if (cmp >= 0)
658 calIdx = MONTHCAL_GetCalCount(infoPtr)-1;
659 else
661 for (calIdx = 1; calIdx < MONTHCAL_GetCalCount(infoPtr)-1; calIdx++)
662 if (MONTHCAL_CompareMonths(date, &infoPtr->calendars[calIdx].month) == 0)
663 break;
668 MONTHCAL_GetDayPos(infoPtr, date, &col, &row, calIdx);
669 MONTHCAL_GetDayRectI(infoPtr, r, col, row, calIdx);
672 static LRESULT
673 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
675 INT range;
677 TRACE("flag=%d, st=%p\n", flag, st);
679 switch (flag) {
680 case GMR_VISIBLE:
682 if (st)
684 st[0] = infoPtr->calendars[0].month;
685 st[1] = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
687 if (st[0].wMonth == min_allowed_date.wMonth &&
688 st[0].wYear == min_allowed_date.wYear)
690 st[0].wDay = min_allowed_date.wDay;
692 else
693 st[0].wDay = 1;
694 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
696 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
697 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
700 range = MONTHCAL_GetCalCount(infoPtr);
701 break;
703 case GMR_DAYSTATE:
705 if (st)
707 MONTHCAL_GetMinDate(infoPtr, &st[0]);
708 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
710 /* include two partially visible months */
711 range = MONTHCAL_GetCalCount(infoPtr) + 2;
712 break;
714 default:
715 WARN("Unknown flag value, got %d\n", flag);
716 range = 0;
719 return range;
722 /* Focused day helper:
724 - set focused date to given value;
725 - reset to zero value if NULL passed;
726 - invalidate previous and new day rectangle only if needed.
728 Returns TRUE if focused day changed, FALSE otherwise.
730 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
732 RECT r;
734 if(st)
736 /* there's nothing to do if it's the same date,
737 mouse move within same date rectangle case */
738 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
740 /* invalidate old focused day */
741 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
742 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
744 infoPtr->focusedSel = *st;
747 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
749 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
750 infoPtr->focusedSel = st_null;
752 /* on set invalidates new day, on reset clears previous focused day */
753 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
755 return TRUE;
758 /* draw today boundary box for specified rectangle */
759 static void MONTHCAL_Circle(const MONTHCAL_INFO *infoPtr, HDC hdc, const RECT *r)
761 HPEN old_pen = SelectObject(hdc, infoPtr->pens[PenRed]);
762 HBRUSH old_brush;
764 old_brush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
765 Rectangle(hdc, r->left, r->top, r->right, r->bottom);
767 SelectObject(hdc, old_brush);
768 SelectObject(hdc, old_pen);
771 /* Draw today day mark rectangle
773 * [I] hdc : context to draw in
774 * [I] date : day to mark with rectangle
777 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
778 const SYSTEMTIME *date)
780 RECT r;
782 MONTHCAL_GetDayRect(infoPtr, date, &r, -1);
783 MONTHCAL_Circle(infoPtr, hdc, &r);
786 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
787 int bold, const PAINTSTRUCT *ps)
789 static const WCHAR fmtW[] = { '%','d',0 };
790 WCHAR buf[10];
791 RECT r, r_temp;
792 COLORREF oldCol = 0;
793 COLORREF oldBk = 0;
794 INT old_bkmode, selection;
796 /* no need to check styles: when selection is not valid, it is set to zero.
797 1 < day < 31, so everything is OK */
798 MONTHCAL_GetDayRect(infoPtr, st, &r, -1);
799 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
801 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
802 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0))
804 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
805 TRACE("%s\n", wine_dbgstr_rect(&r));
806 oldCol = SetTextColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
807 oldBk = SetBkColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
808 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
810 selection = 1;
812 else
813 selection = 0;
815 SelectObject(hdc, bold ? infoPtr->hBoldFont : infoPtr->hFont);
817 old_bkmode = SetBkMode(hdc, TRANSPARENT);
818 wsprintfW(buf, fmtW, st->wDay);
819 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
820 SetBkMode(hdc, old_bkmode);
822 if (selection)
824 SetTextColor(hdc, oldCol);
825 SetBkColor(hdc, oldBk);
829 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, enum nav_direction button)
831 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
832 RECT *r = button == DIRECTION_FORWARD ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
833 BOOL pressed = button == DIRECTION_FORWARD ? infoPtr->status & MC_NEXTPRESSED :
834 infoPtr->status & MC_PREVPRESSED;
835 if (theme)
837 static const int states[] = {
838 /* Prev button */
839 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
840 /* Next button */
841 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
843 int stateNum = button == DIRECTION_FORWARD ? 3 : 0;
844 if (pressed)
845 stateNum += 1;
846 else
848 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
850 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
852 else
854 int style = button == DIRECTION_FORWARD ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
855 if (pressed)
856 style |= DFCS_PUSHED;
857 else
859 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
862 DrawFrameControl(hdc, r, DFC_SCROLL, style);
866 /* paint a title with buttons and month/year string */
867 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
869 static const WCHAR mmmmW[] = {'M','M','M','M',0};
870 static const WCHAR mmmW[] = {'M','M','M',0};
871 static const WCHAR mmW[] = {'M','M',0};
872 static const WCHAR fmtyearW[] = {'%','l','d',0};
873 static const WCHAR fmtmmW[] = {'%','0','2','d',0};
874 static const WCHAR fmtmW[] = {'%','d',0};
875 RECT *title = &infoPtr->calendars[calIdx].title;
876 const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
877 WCHAR monthW[80], strW[80], fmtW[80], yearW[6] /* valid year range is 1601-30827 */;
878 int yearoffset, monthoffset, shiftX;
879 SIZE sz;
881 /* fill header box */
882 FillRect(hdc, title, infoPtr->brushes[BrushTitle]);
884 /* month/year string */
885 SetBkColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
886 SetTextColor(hdc, infoPtr->colors[MCSC_TITLETEXT]);
887 SelectObject(hdc, infoPtr->hBoldFont);
889 /* draw formatted date string */
890 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_YEARMONTH, st, NULL, strW, countof(strW));
891 DrawTextW(hdc, strW, strlenW(strW), title, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
893 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SYEARMONTH, fmtW, countof(fmtW));
894 wsprintfW(yearW, fmtyearW, st->wYear);
896 /* month is trickier as it's possible to have different format pictures, we'll
897 test for M, MM, MMM, and MMMM */
898 if (strstrW(fmtW, mmmmW))
899 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+st->wMonth-1, monthW, countof(monthW));
900 else if (strstrW(fmtW, mmmW))
901 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME1+st->wMonth-1, monthW, countof(monthW));
902 else if (strstrW(fmtW, mmW))
903 wsprintfW(monthW, fmtmmW, st->wMonth);
904 else
905 wsprintfW(monthW, fmtmW, st->wMonth);
907 /* update hit boxes */
908 yearoffset = 0;
909 while (strW[yearoffset])
911 if (!strncmpW(&strW[yearoffset], yearW, strlenW(yearW)))
912 break;
913 yearoffset++;
916 monthoffset = 0;
917 while (strW[monthoffset])
919 if (!strncmpW(&strW[monthoffset], monthW, strlenW(monthW)))
920 break;
921 monthoffset++;
924 /* for left limits use offsets */
925 sz.cx = 0;
926 if (yearoffset)
927 GetTextExtentPoint32W(hdc, strW, yearoffset, &sz);
928 infoPtr->calendars[calIdx].titleyear.left = sz.cx;
930 sz.cx = 0;
931 if (monthoffset)
932 GetTextExtentPoint32W(hdc, strW, monthoffset, &sz);
933 infoPtr->calendars[calIdx].titlemonth.left = sz.cx;
935 /* for right limits use actual string parts lengths */
936 GetTextExtentPoint32W(hdc, &strW[yearoffset], strlenW(yearW), &sz);
937 infoPtr->calendars[calIdx].titleyear.right = infoPtr->calendars[calIdx].titleyear.left + sz.cx;
939 GetTextExtentPoint32W(hdc, monthW, strlenW(monthW), &sz);
940 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
942 /* Finally translate rectangles to match center aligned string,
943 hit rectangles are relative to title rectangle before translation. */
944 GetTextExtentPoint32W(hdc, strW, strlenW(strW), &sz);
945 shiftX = (title->right - title->left - sz.cx) / 2 + title->left;
946 OffsetRect(&infoPtr->calendars[calIdx].titleyear, shiftX, 0);
947 OffsetRect(&infoPtr->calendars[calIdx].titlemonth, shiftX, 0);
950 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
952 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
953 static const WCHAR fmt_weekW[] = { '%','d',0 };
954 INT mindays, weeknum, weeknum1, startofprescal;
955 INT i, prev_month;
956 SYSTEMTIME st;
957 WCHAR buf[80];
958 HPEN old_pen;
959 RECT r;
961 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
963 MONTHCAL_GetMinDate(infoPtr, &st);
964 startofprescal = st.wDay;
965 st = *date;
967 prev_month = date->wMonth - 1;
968 if(prev_month == 0) prev_month = 12;
971 Rules what week to call the first week of a new year:
972 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
973 The week containing Jan 1 is the first week of year
974 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
975 First week of year must contain 4 days of the new year
976 LOCALE_IFIRSTWEEKOFYEAR == 1 (what countries?)
977 The first week of the year must contain only days of the new year
979 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
980 weeknum = atoiW(buf);
981 switch (weeknum)
983 case 1: mindays = 6;
984 break;
985 case 2: mindays = 3;
986 break;
987 case 0: mindays = 0;
988 break;
989 default:
990 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
991 mindays = 0;
994 if (date->wMonth == 1)
996 /* calculate all those exceptions for January */
997 st.wDay = st.wMonth = 1;
998 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
999 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
1000 weeknum = 1;
1001 else
1003 weeknum = 0;
1004 for(i = 0; i < 11; i++)
1005 weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
1007 weeknum += startofprescal + 7;
1008 weeknum /= 7;
1009 st.wYear -= 1;
1010 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
1011 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
1014 else
1016 weeknum = 0;
1017 for(i = 0; i < prev_month - 1; i++)
1018 weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
1020 weeknum += startofprescal + 7;
1021 weeknum /= 7;
1022 st.wDay = st.wMonth = 1;
1023 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
1024 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
1027 r = infoPtr->calendars[calIdx].weeknums;
1029 /* erase whole week numbers area */
1030 FillRect(hdc, &r, infoPtr->brushes[BrushMonth]);
1031 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1033 /* reduce rectangle to one week number */
1034 r.bottom = r.top + infoPtr->height_increment;
1036 for(i = 0; i < 6; i++) {
1037 if((i == 0) && (weeknum > 50))
1039 wsprintfW(buf, fmt_weekW, weeknum);
1040 weeknum = 0;
1042 else if((i == 5) && (weeknum > 47))
1044 wsprintfW(buf, fmt_weekW, 1);
1046 else
1047 wsprintfW(buf, fmt_weekW, weeknum + i);
1049 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1050 OffsetRect(&r, 0, infoPtr->height_increment);
1053 /* line separator for week numbers column */
1054 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1055 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
1056 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
1057 SelectObject(hdc, old_pen);
1060 /* bottom today date */
1061 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1063 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
1064 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
1065 RECT text_rect, box_rect;
1066 HFONT old_font;
1067 INT col;
1069 if(infoPtr->dwStyle & MCS_NOTODAY) return;
1071 LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, countof(buf_todayW));
1072 col = infoPtr->dwStyle & MCS_NOTODAYCIRCLE ? 0 : 1;
1073 if (infoPtr->dwStyle & MCS_WEEKNUMBERS) col--;
1074 /* label is located below first calendar last row */
1075 MONTHCAL_GetDayRectI(infoPtr, &text_rect, col, 6, infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx);
1076 box_rect = text_rect;
1078 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
1079 buf_dateW, countof(buf_dateW));
1080 old_font = SelectObject(hdc, infoPtr->hBoldFont);
1081 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1083 wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
1084 DrawTextW(hdc, buf, -1, &text_rect, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1085 DrawTextW(hdc, buf, -1, &text_rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1087 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
1088 OffsetRect(&box_rect, -infoPtr->width_increment, 0);
1089 MONTHCAL_Circle(infoPtr, hdc, &box_rect);
1092 SelectObject(hdc, old_font);
1095 /* today mark + focus */
1096 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1098 /* circle today date if only it's in fully visible month */
1099 if (!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
1101 INT i;
1103 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1104 if (!MONTHCAL_CompareMonths(&infoPtr->todaysDate, &infoPtr->calendars[i].month))
1106 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
1107 break;
1111 if (!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
1113 RECT r;
1114 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
1115 DrawFocusRect(hdc, &r);
1119 /* months before first calendar month and after last calendar month */
1120 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1122 INT mask, index;
1123 UINT length;
1124 SYSTEMTIME st_max, st;
1126 if (infoPtr->dwStyle & MCS_NOTRAILINGDATES) return;
1128 SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
1130 /* draw prev month */
1131 MONTHCAL_GetMinDate(infoPtr, &st);
1132 mask = 1 << (st.wDay-1);
1133 /* December and January both 31 days long, so no worries if wrapped */
1134 length = MONTHCAL_MonthLength(infoPtr->calendars[0].month.wMonth - 1,
1135 infoPtr->calendars[0].month.wYear);
1136 index = 0;
1137 while(st.wDay <= length)
1139 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1140 mask <<= 1;
1141 st.wDay++;
1144 /* draw next month */
1145 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1146 st.wDay = 1;
1147 MONTHCAL_GetNextMonth(&st);
1148 MONTHCAL_GetMaxDate(infoPtr, &st_max);
1149 mask = 1;
1150 index = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)-1;
1151 while(st.wDay <= st_max.wDay)
1153 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1154 mask <<= 1;
1155 st.wDay++;
1159 /* paint a calendar area */
1160 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
1162 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
1163 INT i, j;
1164 UINT length;
1165 RECT r, fill_bk_rect;
1166 SYSTEMTIME st;
1167 WCHAR buf[80];
1168 HPEN old_pen;
1169 int mask;
1171 /* fill whole days area - from week days area to today note rectangle */
1172 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
1173 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
1174 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
1176 FillRect(hdc, &fill_bk_rect, infoPtr->brushes[BrushMonth]);
1178 /* draw line under day abbreviations */
1179 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1180 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
1181 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
1182 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
1183 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
1184 SelectObject(hdc, old_pen);
1186 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
1187 infoPtr->calendars[calIdx].weeknums.right;
1189 /* draw day abbreviations */
1190 SelectObject(hdc, infoPtr->hFont);
1191 SetBkColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
1192 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1193 /* rectangle to draw a single day abbreviation within */
1194 r = infoPtr->calendars[calIdx].wdays;
1195 r.right = r.left + infoPtr->width_increment;
1197 i = infoPtr->firstDay;
1198 for(j = 0; j < 7; j++) {
1199 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
1200 DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1201 OffsetRect(&r, infoPtr->width_increment, 0);
1204 /* draw current month */
1205 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1206 st = *date;
1207 st.wDay = 1;
1208 mask = 1;
1209 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1210 while(st.wDay <= length)
1212 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[calIdx+1] & mask, ps);
1213 mask <<= 1;
1214 st.wDay++;
1218 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1220 COLORREF old_text_clr, old_bk_clr;
1221 HFONT old_font;
1222 INT i;
1224 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1225 old_bk_clr = GetBkColor(hdc);
1226 old_font = GetCurrentObject(hdc, OBJ_FONT);
1228 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1230 RECT *title = &infoPtr->calendars[i].title;
1231 RECT r;
1233 /* draw title, redraw all its elements */
1234 if (IntersectRect(&r, &(ps->rcPaint), title))
1235 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1237 /* draw calendar area */
1238 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1239 if (IntersectRect(&r, &(ps->rcPaint), &r))
1240 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1242 /* week numbers */
1243 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1246 /* partially visible months */
1247 MONTHCAL_PaintLeadTrailMonths(infoPtr, hdc, ps);
1249 /* focus and today rectangle */
1250 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1252 /* today at the bottom left */
1253 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1255 /* navigation buttons */
1256 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_BACKWARD);
1257 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_FORWARD);
1259 /* restore context */
1260 SetBkColor(hdc, old_bk_clr);
1261 SelectObject(hdc, old_font);
1262 SetTextColor(hdc, old_text_clr);
1265 static LRESULT
1266 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, RECT *rect)
1268 TRACE("rect %p\n", rect);
1270 if(!rect) return FALSE;
1272 *rect = infoPtr->calendars[0].title;
1273 rect->bottom = infoPtr->calendars[0].days.bottom + infoPtr->todayrect.bottom -
1274 infoPtr->todayrect.top;
1276 AdjustWindowRect(rect, infoPtr->dwStyle, FALSE);
1278 /* minimal rectangle is zero based */
1279 OffsetRect(rect, -rect->left, -rect->top);
1281 TRACE("%s\n", wine_dbgstr_rect(rect));
1283 return TRUE;
1286 static COLORREF
1287 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, UINT index)
1289 TRACE("%p, %d\n", infoPtr, index);
1291 if (index > MCSC_TRAILINGTEXT) return -1;
1292 return infoPtr->colors[index];
1295 static LRESULT
1296 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, UINT index, COLORREF color)
1298 enum CachedBrush type;
1299 COLORREF prev;
1301 TRACE("%p, %d: color %08x\n", infoPtr, index, color);
1303 if (index > MCSC_TRAILINGTEXT) return -1;
1305 prev = infoPtr->colors[index];
1306 infoPtr->colors[index] = color;
1308 /* update cached brush */
1309 switch (index)
1311 case MCSC_BACKGROUND:
1312 type = BrushBackground;
1313 break;
1314 case MCSC_TITLEBK:
1315 type = BrushTitle;
1316 break;
1317 case MCSC_MONTHBK:
1318 type = BrushMonth;
1319 break;
1320 default:
1321 type = BrushLast;
1324 if (type != BrushLast)
1326 DeleteObject(infoPtr->brushes[type]);
1327 infoPtr->brushes[type] = CreateSolidBrush(color);
1330 /* update cached pen */
1331 if (index == MCSC_TEXT)
1333 DeleteObject(infoPtr->pens[PenText]);
1334 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[index]);
1337 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND);
1338 return prev;
1341 static LRESULT
1342 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1344 TRACE("\n");
1346 if(infoPtr->delta)
1347 return infoPtr->delta;
1349 return MONTHCAL_GetMonthRange(infoPtr, GMR_VISIBLE, NULL);
1353 static LRESULT
1354 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1356 INT prev = infoPtr->delta;
1358 TRACE("delta %d\n", delta);
1360 infoPtr->delta = delta;
1361 return prev;
1365 static inline LRESULT
1366 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1368 int day;
1370 /* convert from SYSTEMTIME to locale format */
1371 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1373 return MAKELONG(day, infoPtr->firstDaySet);
1377 /* Sets the first day of the week that will appear in the control
1380 * PARAMETERS:
1381 * [I] infoPtr : valid pointer to control data
1382 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1385 * RETURN VALUE:
1386 * Low word contains previous first day,
1387 * high word indicates was first day forced with this message before or is
1388 * locale defined (TRUE - was forced, FALSE - wasn't).
1390 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1391 * FIXME: we need more error checking here
1393 static LRESULT
1394 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1396 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1397 int new_day;
1399 TRACE("%d\n", day);
1401 if(day == -1)
1403 WCHAR buf[80];
1405 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1406 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1408 new_day = atoiW(buf);
1410 infoPtr->firstDaySet = FALSE;
1412 else if(day >= 7)
1414 new_day = 6; /* max first day allowed */
1415 infoPtr->firstDaySet = TRUE;
1417 else
1419 /* Native behaviour for that case is broken: invalid date number >31
1420 got displayed at (0,0) position, current month starts always from
1421 (1,0) position. Should be implemented here as well only if there's
1422 nothing else to do. */
1423 if (day < -1)
1424 FIXME("No bug compatibility for day=%d\n", day);
1426 new_day = day;
1427 infoPtr->firstDaySet = TRUE;
1430 /* convert from locale to SYSTEMTIME format */
1431 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1433 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1435 return prev;
1438 static LRESULT
1439 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1441 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1444 static LRESULT
1445 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1447 FILETIME ft_min, ft_max;
1449 TRACE("%x %p\n", limits, range);
1451 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1452 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1453 return FALSE;
1455 if (limits & GDTR_MIN)
1457 if (!MONTHCAL_ValidateTime(&range[0]))
1458 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1460 infoPtr->minDate = range[0];
1461 infoPtr->rangeValid |= GDTR_MIN;
1463 if (limits & GDTR_MAX)
1465 if (!MONTHCAL_ValidateTime(&range[1]))
1466 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1468 infoPtr->maxDate = range[1];
1469 infoPtr->rangeValid |= GDTR_MAX;
1472 /* Only one limit set - we are done */
1473 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1474 return TRUE;
1476 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1477 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1479 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1481 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1483 /* Native swaps limits only when both limits are being set. */
1484 SYSTEMTIME st_tmp = infoPtr->minDate;
1485 infoPtr->minDate = infoPtr->maxDate;
1486 infoPtr->maxDate = st_tmp;
1488 else
1490 /* reset the other limit */
1491 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1492 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1493 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1497 return TRUE;
1501 static LRESULT
1502 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1504 TRACE("%p\n", range);
1506 if(!range) return FALSE;
1508 range[1] = infoPtr->maxDate;
1509 range[0] = infoPtr->minDate;
1511 return infoPtr->rangeValid;
1515 static LRESULT
1516 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1518 TRACE("%p %d %p\n", infoPtr, months, states);
1520 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return 0;
1521 if (months != MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)) return 0;
1523 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1525 return 1;
1528 static LRESULT
1529 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1531 TRACE("%p\n", curSel);
1532 if(!curSel) return FALSE;
1533 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1535 *curSel = infoPtr->minSel;
1536 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1537 return TRUE;
1540 static LRESULT
1541 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1543 SYSTEMTIME prev = infoPtr->minSel, selection;
1544 INT diff;
1545 WORD day;
1547 TRACE("%p\n", curSel);
1548 if(!curSel) return FALSE;
1549 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1551 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1552 /* exit earlier if selection equals current */
1553 if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1555 selection = *curSel;
1556 selection.wHour = selection.wMinute = selection.wSecond = selection.wMilliseconds = 0;
1557 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1559 if(!MONTHCAL_IsDateInValidRange(infoPtr, &selection, FALSE)) return FALSE;
1561 /* scroll calendars only if we have to */
1562 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, curSel);
1563 if (diff <= 0)
1565 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, curSel);
1566 if (diff > 0) diff = 0;
1569 if (diff != 0)
1571 INT i;
1573 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1574 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1577 /* we need to store time part as it is */
1578 selection = *curSel;
1579 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1580 infoPtr->minSel = infoPtr->maxSel = selection;
1582 /* if selection is still in current month, reduce rectangle */
1583 day = prev.wDay;
1584 prev.wDay = curSel->wDay;
1585 if (MONTHCAL_IsDateEqual(&prev, curSel))
1587 RECT r_prev, r_new;
1589 prev.wDay = day;
1590 MONTHCAL_GetDayRect(infoPtr, &prev, &r_prev, -1);
1591 MONTHCAL_GetDayRect(infoPtr, curSel, &r_new, -1);
1593 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1594 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1596 else
1597 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1599 return TRUE;
1603 static LRESULT
1604 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1606 return infoPtr->maxSelCount;
1610 static LRESULT
1611 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1613 TRACE("%d\n", max);
1615 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1616 if(max <= 0) return FALSE;
1618 infoPtr->maxSelCount = max;
1620 return TRUE;
1624 static LRESULT
1625 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1627 TRACE("%p\n", range);
1629 if(!range) return FALSE;
1631 if(infoPtr->dwStyle & MCS_MULTISELECT)
1633 range[1] = infoPtr->maxSel;
1634 range[0] = infoPtr->minSel;
1635 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1636 return TRUE;
1639 return FALSE;
1643 static LRESULT
1644 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1646 SYSTEMTIME old_range[2];
1647 INT diff;
1649 TRACE("%p\n", range);
1651 if(!range || !(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1653 /* adjust timestamps */
1654 if(!MONTHCAL_ValidateTime(&range[0])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1655 if(!MONTHCAL_ValidateTime(&range[1])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1657 /* maximum range exceeded */
1658 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1660 old_range[0] = infoPtr->minSel;
1661 old_range[1] = infoPtr->maxSel;
1663 /* swap if min > max */
1664 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1666 infoPtr->minSel = range[0];
1667 infoPtr->maxSel = range[1];
1669 else
1671 infoPtr->minSel = range[1];
1672 infoPtr->maxSel = range[0];
1675 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, &infoPtr->maxSel);
1676 if (diff < 0)
1678 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, &infoPtr->maxSel);
1679 if (diff > 0) diff = 0;
1682 if (diff != 0)
1684 INT i;
1686 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1687 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1690 /* update day of week */
1691 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1692 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1694 /* redraw if bounds changed */
1695 /* FIXME: no actual need to redraw everything */
1696 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1697 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1699 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1702 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1703 return TRUE;
1707 static LRESULT
1708 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1710 TRACE("%p\n", today);
1712 if(!today) return FALSE;
1713 *today = infoPtr->todaysDate;
1714 return TRUE;
1717 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1719 * RETURN VALUE
1721 * TRUE - today date changed
1722 * FALSE - today date isn't changed
1724 static BOOL
1725 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1727 RECT new_r, old_r;
1729 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1731 MONTHCAL_GetDayRect(infoPtr, &infoPtr->todaysDate, &old_r, -1);
1732 MONTHCAL_GetDayRect(infoPtr, today, &new_r, -1);
1734 infoPtr->todaysDate = *today;
1736 /* only two days need redrawing */
1737 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1738 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1739 /* and today label */
1740 InvalidateRect(infoPtr->hwndSelf, &infoPtr->todayrect, FALSE);
1741 return TRUE;
1744 /* MCM_SETTODAT handler */
1745 static LRESULT
1746 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1748 TRACE("%p\n", today);
1750 if (today)
1752 /* remember if date was set successfully */
1753 if (MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1756 return 0;
1759 /* returns calendar index containing specified point, or -1 if it's background */
1760 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1762 RECT r;
1763 INT i;
1765 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1767 /* whole bounding rectangle allows some optimization to compute */
1768 r.left = infoPtr->calendars[i].title.left;
1769 r.top = infoPtr->calendars[i].title.top;
1770 r.bottom = infoPtr->calendars[i].days.bottom;
1771 r.right = infoPtr->calendars[i].days.right;
1773 if (PtInRect(&r, *pt)) return i;
1776 return -1;
1779 static inline UINT fill_hittest_info(const MCHITTESTINFO *src, MCHITTESTINFO *dest)
1781 dest->uHit = src->uHit;
1782 dest->st = src->st;
1784 if (dest->cbSize == sizeof(MCHITTESTINFO))
1785 memcpy(&dest->rc, &src->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1787 return src->uHit;
1790 static LRESULT
1791 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1793 MCHITTESTINFO htinfo;
1794 SYSTEMTIME *ht_month;
1795 INT day, calIdx;
1797 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1799 htinfo.st = st_null;
1801 /* we should preserve passed fields if hit area doesn't need them */
1802 if (lpht->cbSize == sizeof(MCHITTESTINFO))
1803 memcpy(&htinfo.rc, &lpht->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1805 /* Comment in for debugging...
1806 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,
1807 infoPtr->wdays.left, infoPtr->wdays.right,
1808 infoPtr->wdays.top, infoPtr->wdays.bottom,
1809 infoPtr->days.left, infoPtr->days.right,
1810 infoPtr->days.top, infoPtr->days.bottom,
1811 infoPtr->todayrect.left, infoPtr->todayrect.right,
1812 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1813 infoPtr->weeknums.left, infoPtr->weeknums.right,
1814 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1817 /* guess in what calendar we are */
1818 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1819 if (calIdx == -1)
1821 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1823 htinfo.uHit = MCHT_TODAYLINK;
1824 htinfo.rc = infoPtr->todayrect;
1826 else
1827 /* outside of calendar area? What's left must be background :-) */
1828 htinfo.uHit = MCHT_CALENDARBK;
1830 return fill_hittest_info(&htinfo, lpht);
1833 /* are we in the header? */
1834 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1835 /* FIXME: buttons hittesting could be optimized cause maximum
1836 two calendars have buttons */
1837 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1839 htinfo.uHit = MCHT_TITLEBTNPREV;
1840 htinfo.rc = infoPtr->titlebtnprev;
1842 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1844 htinfo.uHit = MCHT_TITLEBTNNEXT;
1845 htinfo.rc = infoPtr->titlebtnnext;
1847 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1849 htinfo.uHit = MCHT_TITLEMONTH;
1850 htinfo.rc = infoPtr->calendars[calIdx].titlemonth;
1851 htinfo.iOffset = calIdx;
1853 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1855 htinfo.uHit = MCHT_TITLEYEAR;
1856 htinfo.rc = infoPtr->calendars[calIdx].titleyear;
1857 htinfo.iOffset = calIdx;
1859 else
1861 htinfo.uHit = MCHT_TITLE;
1862 htinfo.rc = infoPtr->calendars[calIdx].title;
1863 htinfo.iOffset = calIdx;
1866 return fill_hittest_info(&htinfo, lpht);
1869 ht_month = &infoPtr->calendars[calIdx].month;
1870 /* days area (including week days and week numbers) */
1871 day = MONTHCAL_GetDayFromPos(infoPtr, lpht->pt, calIdx);
1872 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1874 htinfo.uHit = MCHT_CALENDARDAY;
1875 htinfo.iOffset = calIdx;
1876 htinfo.st.wYear = ht_month->wYear;
1877 htinfo.st.wMonth = (day < 1) ? ht_month->wMonth -1 : ht_month->wMonth;
1878 htinfo.st.wDay = (day < 1) ?
1879 MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day : day;
1881 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1883 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1885 htinfo.uHit = MCHT_CALENDARWEEKNUM;
1886 htinfo.st.wYear = ht_month->wYear;
1887 htinfo.iOffset = calIdx;
1889 if (day < 1)
1891 htinfo.st.wMonth = ht_month->wMonth - 1;
1892 htinfo.st.wDay = MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day;
1894 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1896 htinfo.st.wMonth = ht_month->wMonth + 1;
1897 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1899 else
1901 htinfo.st.wMonth = ht_month->wMonth;
1902 htinfo.st.wDay = day;
1905 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1907 htinfo.iOffset = calIdx;
1908 htinfo.st.wYear = ht_month->wYear;
1909 htinfo.st.wMonth = ht_month->wMonth;
1910 /* previous month only valid for first calendar */
1911 if (day < 1 && calIdx == 0)
1913 htinfo.uHit = MCHT_CALENDARDATEPREV;
1914 MONTHCAL_GetPrevMonth(&htinfo.st);
1915 htinfo.st.wDay = MONTHCAL_MonthLength(htinfo.st.wMonth, htinfo.st.wYear) + day;
1917 /* next month only valid for last calendar */
1918 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear) &&
1919 calIdx == MONTHCAL_GetCalCount(infoPtr)-1)
1921 htinfo.uHit = MCHT_CALENDARDATENEXT;
1922 MONTHCAL_GetNextMonth(&htinfo.st);
1923 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1925 /* multiple calendars case - blank areas for previous/next month */
1926 else if (day < 1 || day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1928 htinfo.uHit = MCHT_CALENDARBK;
1930 else
1932 htinfo.uHit = MCHT_CALENDARDATE;
1933 htinfo.st.wDay = day;
1936 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1937 MONTHCAL_GetDayRectI(infoPtr, &htinfo.rc, htinfo.iCol, htinfo.iRow, calIdx);
1938 /* always update day of week */
1939 MONTHCAL_CalculateDayOfWeek(&htinfo.st, TRUE);
1942 return fill_hittest_info(&htinfo, lpht);
1945 /* MCN_GETDAYSTATE notification helper */
1946 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1948 MONTHDAYSTATE *state;
1949 NMDAYSTATE nmds;
1951 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return;
1953 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1954 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1955 nmds.nmhdr.code = MCN_GETDAYSTATE;
1956 nmds.cDayState = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0);
1957 nmds.prgDayState = state = Alloc(nmds.cDayState * sizeof(MONTHDAYSTATE));
1959 MONTHCAL_GetMinDate(infoPtr, &nmds.stStart);
1960 nmds.stStart.wDay = 1;
1962 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1963 memcpy(infoPtr->monthdayState, nmds.prgDayState,
1964 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
1966 Free(state);
1969 /* no valid range check performed */
1970 static void MONTHCAL_Scroll(MONTHCAL_INFO *infoPtr, INT delta)
1972 INT i, selIdx = -1;
1974 for(i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1976 /* save selection position to shift it later */
1977 if (selIdx == -1 && MONTHCAL_CompareMonths(&infoPtr->minSel, &infoPtr->calendars[i].month) == 0)
1978 selIdx = i;
1980 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, delta);
1983 /* selection is always shifted to first calendar */
1984 if(infoPtr->dwStyle & MCS_MULTISELECT)
1986 SYSTEMTIME range[2];
1988 MONTHCAL_GetSelRange(infoPtr, range);
1989 MONTHCAL_GetMonth(&range[0], delta - selIdx);
1990 MONTHCAL_GetMonth(&range[1], delta - selIdx);
1991 MONTHCAL_SetSelRange(infoPtr, range);
1993 else
1995 SYSTEMTIME st = infoPtr->minSel;
1997 MONTHCAL_GetMonth(&st, delta - selIdx);
1998 MONTHCAL_SetCurSel(infoPtr, &st);
2002 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
2004 INT delta = infoPtr->delta ? infoPtr->delta : MONTHCAL_GetCalCount(infoPtr);
2005 SYSTEMTIME st;
2007 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
2009 /* check if change allowed by range set */
2010 if(direction == DIRECTION_BACKWARD)
2012 st = infoPtr->calendars[0].month;
2013 MONTHCAL_GetMonth(&st, -delta);
2015 else
2017 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2018 MONTHCAL_GetMonth(&st, delta);
2021 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
2023 MONTHCAL_Scroll(infoPtr, direction == DIRECTION_BACKWARD ? -delta : delta);
2024 MONTHCAL_NotifyDayState(infoPtr);
2025 MONTHCAL_NotifySelectionChange(infoPtr);
2028 static LRESULT
2029 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2031 HMENU hMenu;
2032 POINT menupoint;
2033 WCHAR buf[32];
2035 hMenu = CreatePopupMenu();
2036 LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf));
2037 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
2038 menupoint.x = (short)LOWORD(lParam);
2039 menupoint.y = (short)HIWORD(lParam);
2040 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2041 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
2042 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
2044 if (infoPtr->dwStyle & MCS_MULTISELECT)
2046 SYSTEMTIME range[2];
2048 range[0] = range[1] = infoPtr->todaysDate;
2049 MONTHCAL_SetSelRange(infoPtr, range);
2051 else
2052 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2054 MONTHCAL_NotifySelectionChange(infoPtr);
2055 MONTHCAL_NotifySelect(infoPtr);
2058 return 0;
2061 /***
2062 * DESCRIPTION:
2063 * Subclassed edit control windproc function
2065 * PARAMETER(S):
2066 * [I] hwnd : the edit window handle
2067 * [I] uMsg : the message that is to be processed
2068 * [I] wParam : first message parameter
2069 * [I] lParam : second message parameter
2072 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2074 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
2076 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
2077 hwnd, uMsg, wParam, lParam);
2079 switch (uMsg)
2081 case WM_GETDLGCODE:
2082 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
2084 case WM_DESTROY:
2086 WNDPROC editProc = infoPtr->EditWndProc;
2087 infoPtr->EditWndProc = NULL;
2088 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
2089 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
2092 case WM_KILLFOCUS:
2093 break;
2095 case WM_KEYDOWN:
2096 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
2097 break;
2099 default:
2100 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
2103 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
2104 SendMessageW(hwnd, WM_CLOSE, 0, 0);
2105 return 0;
2108 /* creates updown control and edit box */
2109 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr, INT calIdx)
2111 RECT *rc = &infoPtr->calendars[calIdx].titleyear;
2112 RECT *title = &infoPtr->calendars[calIdx].title;
2114 infoPtr->hWndYearEdit =
2115 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
2116 rc->left + 3, (title->bottom + title->top - infoPtr->textHeight) / 2,
2117 rc->right - rc->left + 4,
2118 infoPtr->textHeight, infoPtr->hwndSelf,
2119 NULL, NULL, NULL);
2121 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
2123 infoPtr->hWndYearUpDown =
2124 CreateWindowExW(0, UPDOWN_CLASSW, 0,
2125 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
2126 rc->right + 7, (title->bottom + title->top - infoPtr->textHeight) / 2,
2127 18, infoPtr->textHeight, infoPtr->hwndSelf,
2128 NULL, NULL, NULL);
2130 /* attach edit box */
2131 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
2132 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
2133 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
2134 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->calendars[calIdx].month.wYear);
2136 /* subclass edit box */
2137 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
2138 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
2140 SetFocus(infoPtr->hWndYearEdit);
2143 static LRESULT
2144 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2146 MCHITTESTINFO ht;
2147 DWORD hit;
2149 /* Actually we don't need input focus for calendar, this is used to kill
2150 year updown and its buddy edit box */
2151 if (IsWindow(infoPtr->hWndYearUpDown))
2153 SetFocus(infoPtr->hwndSelf);
2154 return 0;
2157 SetCapture(infoPtr->hwndSelf);
2159 ht.cbSize = sizeof(MCHITTESTINFO);
2160 ht.pt.x = (short)LOWORD(lParam);
2161 ht.pt.y = (short)HIWORD(lParam);
2163 hit = MONTHCAL_HitTest(infoPtr, &ht);
2165 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
2167 switch(hit)
2169 case MCHT_TITLEBTNNEXT:
2170 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2171 infoPtr->status = MC_NEXTPRESSED;
2172 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2173 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2174 return 0;
2176 case MCHT_TITLEBTNPREV:
2177 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2178 infoPtr->status = MC_PREVPRESSED;
2179 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2180 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2181 return 0;
2183 case MCHT_TITLEMONTH:
2185 HMENU hMenu = CreatePopupMenu();
2186 WCHAR buf[32];
2187 POINT menupoint;
2188 INT i;
2190 for (i = 0; i < 12; i++)
2192 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
2193 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
2195 menupoint.x = ht.pt.x;
2196 menupoint.y = ht.pt.y;
2197 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2198 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
2199 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
2201 if ((i > 0) && (i < 13) && infoPtr->calendars[ht.iOffset].month.wMonth != i)
2203 INT delta = i - infoPtr->calendars[ht.iOffset].month.wMonth;
2204 SYSTEMTIME st;
2206 /* check if change allowed by range set */
2207 st = delta < 0 ? infoPtr->calendars[0].month :
2208 infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2209 MONTHCAL_GetMonth(&st, delta);
2211 if (MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE))
2213 MONTHCAL_Scroll(infoPtr, delta);
2214 MONTHCAL_NotifyDayState(infoPtr);
2215 MONTHCAL_NotifySelectionChange(infoPtr);
2216 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2219 return 0;
2221 case MCHT_TITLEYEAR:
2223 MONTHCAL_EditYear(infoPtr, ht.iOffset);
2224 return 0;
2226 case MCHT_TODAYLINK:
2228 if (infoPtr->dwStyle & MCS_MULTISELECT)
2230 SYSTEMTIME range[2];
2232 range[0] = range[1] = infoPtr->todaysDate;
2233 MONTHCAL_SetSelRange(infoPtr, range);
2235 else
2236 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2238 MONTHCAL_NotifySelectionChange(infoPtr);
2239 MONTHCAL_NotifySelect(infoPtr);
2240 return 0;
2242 case MCHT_CALENDARDATENEXT:
2243 case MCHT_CALENDARDATEPREV:
2244 case MCHT_CALENDARDATE:
2246 SYSTEMTIME st[2];
2248 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2250 st[0] = st[1] = ht.st;
2251 /* clear selection range */
2252 MONTHCAL_SetSelRange(infoPtr, st);
2254 infoPtr->status = MC_SEL_LBUTDOWN;
2255 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
2256 return 0;
2260 return 1;
2264 static LRESULT
2265 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2267 NMHDR nmhdr;
2268 MCHITTESTINFO ht;
2269 DWORD hit;
2271 TRACE("\n");
2273 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2274 RECT *r;
2276 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2277 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2278 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2280 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2283 ReleaseCapture();
2285 /* always send NM_RELEASEDCAPTURE notification */
2286 nmhdr.hwndFrom = infoPtr->hwndSelf;
2287 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2288 nmhdr.code = NM_RELEASEDCAPTURE;
2289 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2291 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2293 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2295 ht.cbSize = sizeof(MCHITTESTINFO);
2296 ht.pt.x = (short)LOWORD(lParam);
2297 ht.pt.y = (short)HIWORD(lParam);
2298 hit = MONTHCAL_HitTest(infoPtr, &ht);
2300 infoPtr->status = MC_SEL_LBUTUP;
2301 MONTHCAL_SetDayFocus(infoPtr, NULL);
2303 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2305 SYSTEMTIME sel = infoPtr->minSel;
2307 /* will be invalidated here */
2308 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2310 /* send MCN_SELCHANGE only if new date selected */
2311 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2312 MONTHCAL_NotifySelectionChange(infoPtr);
2314 MONTHCAL_NotifySelect(infoPtr);
2317 return 0;
2321 static LRESULT
2322 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2324 TRACE("%ld\n", id);
2326 switch(id) {
2327 case MC_PREVNEXTMONTHTIMER:
2328 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2329 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2330 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2331 break;
2332 case MC_TODAYUPDATETIMER:
2334 SYSTEMTIME st;
2336 if(infoPtr->todaySet) return 0;
2338 GetLocalTime(&st);
2339 MONTHCAL_UpdateToday(infoPtr, &st);
2341 /* notification sent anyway */
2342 MONTHCAL_NotifySelectionChange(infoPtr);
2344 return 0;
2346 default:
2347 ERR("got unknown timer %ld\n", id);
2348 break;
2351 return 0;
2355 static LRESULT
2356 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2358 MCHITTESTINFO ht;
2359 SYSTEMTIME st_ht;
2360 INT hit;
2361 RECT r;
2363 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2365 ht.cbSize = sizeof(MCHITTESTINFO);
2366 ht.pt.x = (short)LOWORD(lParam);
2367 ht.pt.y = (short)HIWORD(lParam);
2368 ht.iOffset = -1;
2370 hit = MONTHCAL_HitTest(infoPtr, &ht);
2372 /* not on the calendar date numbers? bail out */
2373 TRACE("hit:%x\n",hit);
2374 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2376 MONTHCAL_SetDayFocus(infoPtr, NULL);
2377 return 0;
2380 st_ht = ht.st;
2382 /* if pointer is over focused day still there's nothing to do */
2383 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2385 MONTHCAL_GetDayRect(infoPtr, &ht.st, &r, ht.iOffset);
2387 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2388 SYSTEMTIME st[2];
2390 MONTHCAL_GetSelRange(infoPtr, st);
2392 /* If we're still at the first selected date and range is empty, return.
2393 If range isn't empty we should change range to a single firstSel */
2394 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2395 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2397 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2399 st[0] = infoPtr->firstSel;
2400 /* we should overwrite timestamp here */
2401 MONTHCAL_CopyDate(&st_ht, &st[1]);
2403 /* bounds will be swapped here if needed */
2404 MONTHCAL_SetSelRange(infoPtr, st);
2406 return 0;
2409 done:
2411 /* FIXME: this should specify a rectangle containing only the days that changed
2412 using InvalidateRect */
2413 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2415 return 0;
2419 static LRESULT
2420 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2422 HDC hdc;
2423 PAINTSTRUCT ps;
2425 if (hdc_paint)
2427 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2428 hdc = hdc_paint;
2430 else
2431 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2433 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2434 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2435 return 0;
2438 static LRESULT
2439 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2441 RECT rc;
2443 if (!GetClipBox(hdc, &rc)) return FALSE;
2445 FillRect(hdc, &rc, infoPtr->brushes[BrushBackground]);
2447 return TRUE;
2450 static LRESULT
2451 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2453 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2455 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2456 return 0;
2458 if (options & PRF_ERASEBKGND)
2459 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2461 if (options & PRF_CLIENT)
2462 MONTHCAL_Paint(infoPtr, hdc);
2464 return 0;
2467 static LRESULT
2468 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2470 TRACE("\n");
2472 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2474 return 0;
2477 /* sets the size information */
2478 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2480 static const WCHAR O0W[] = { '0','0',0 };
2481 RECT *title=&infoPtr->calendars[0].title;
2482 RECT *prev=&infoPtr->titlebtnprev;
2483 RECT *next=&infoPtr->titlebtnnext;
2484 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2485 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2486 RECT *wdays=&infoPtr->calendars[0].wdays;
2487 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2488 RECT *days=&infoPtr->calendars[0].days;
2489 RECT *todayrect=&infoPtr->todayrect;
2491 INT xdiv, dx, dy, i, j, x, y, c_dx, c_dy;
2492 WCHAR buff[80];
2493 TEXTMETRICW tm;
2494 SIZE size, sz;
2495 RECT client;
2496 HFONT font;
2497 HDC hdc;
2499 GetClientRect(infoPtr->hwndSelf, &client);
2501 hdc = GetDC(infoPtr->hwndSelf);
2502 font = SelectObject(hdc, infoPtr->hFont);
2504 /* get the height and width of each day's text */
2505 GetTextMetricsW(hdc, &tm);
2506 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2508 /* find largest abbreviated day name for current locale */
2509 size.cx = sz.cx = 0;
2510 for (i = 0; i < 7; i++)
2512 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + i,
2513 buff, countof(buff)))
2515 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2516 if (sz.cx > size.cx) size.cx = sz.cx;
2518 else /* locale independent fallback on failure */
2520 static const WCHAR SunW[] = { 'S','u','n',0 };
2522 GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2523 break;
2527 infoPtr->textWidth = size.cx + 2;
2529 /* recalculate the height and width increments and offsets */
2530 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2532 /* restore the originally selected font */
2533 SelectObject(hdc, font);
2534 ReleaseDC(infoPtr->hwndSelf, hdc);
2536 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2538 infoPtr->width_increment = size.cx * 2 + 4;
2539 infoPtr->height_increment = infoPtr->textHeight;
2541 /* calculate title area */
2542 title->top = 0;
2543 title->bottom = 3 * infoPtr->height_increment / 2;
2544 title->left = 0;
2545 title->right = infoPtr->width_increment * xdiv;
2547 /* set the dimensions of the next and previous buttons and center */
2548 /* the month text vertically */
2549 prev->top = next->top = title->top + 4;
2550 prev->bottom = next->bottom = title->bottom - 4;
2551 prev->left = title->left + 4;
2552 prev->right = prev->left + (title->bottom - title->top);
2553 next->right = title->right - 4;
2554 next->left = next->right - (title->bottom - title->top);
2556 /* titlemonth->left and right change based upon the current month
2557 and are recalculated in refresh as the current month may change
2558 without the control being resized */
2559 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2560 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2562 /* week numbers */
2563 weeknumrect->left = 0;
2564 weeknumrect->right = infoPtr->dwStyle & MCS_WEEKNUMBERS ? prev->right : 0;
2566 /* days abbreviated names */
2567 wdays->left = days->left = weeknumrect->right;
2568 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2569 wdays->top = title->bottom;
2570 wdays->bottom = wdays->top + infoPtr->height_increment;
2572 days->top = weeknumrect->top = wdays->bottom;
2573 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2575 todayrect->left = 0;
2576 todayrect->right = title->right;
2577 todayrect->top = days->bottom;
2578 todayrect->bottom = days->bottom + infoPtr->height_increment;
2580 /* compute calendar count, update all calendars */
2581 x = (client.right + MC_CALENDAR_PADDING) / (title->right - title->left + MC_CALENDAR_PADDING);
2582 /* today label affects whole height */
2583 if (infoPtr->dwStyle & MCS_NOTODAY)
2584 y = (client.bottom + MC_CALENDAR_PADDING) / (days->bottom - title->top + MC_CALENDAR_PADDING);
2585 else
2586 y = (client.bottom - todayrect->bottom + todayrect->top + MC_CALENDAR_PADDING) /
2587 (days->bottom - title->top + MC_CALENDAR_PADDING);
2589 /* TODO: ensure that count is properly adjusted to fit 12 months constraint */
2590 if (x == 0) x = 1;
2591 if (y == 0) y = 1;
2593 if (x*y != MONTHCAL_GetCalCount(infoPtr))
2595 infoPtr->dim.cx = x;
2596 infoPtr->dim.cy = y;
2597 infoPtr->calendars = ReAlloc(infoPtr->calendars, MONTHCAL_GetCalCount(infoPtr)*sizeof(CALENDAR_INFO));
2599 infoPtr->monthdayState = ReAlloc(infoPtr->monthdayState,
2600 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
2601 MONTHCAL_NotifyDayState(infoPtr);
2603 /* update pointers that we'll need */
2604 title = &infoPtr->calendars[0].title;
2605 wdays = &infoPtr->calendars[0].wdays;
2606 days = &infoPtr->calendars[0].days;
2609 for (i = 1; i < MONTHCAL_GetCalCount(infoPtr); i++)
2611 /* set months */
2612 infoPtr->calendars[i] = infoPtr->calendars[0];
2613 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, i);
2616 /* offset all rectangles to center in client area */
2617 c_dx = (client.right - x * title->right - MC_CALENDAR_PADDING * (x-1)) / 2;
2618 c_dy = (client.bottom - y * todayrect->bottom - MC_CALENDAR_PADDING * (y-1)) / 2;
2620 /* if calendar doesn't fit client area show it at left/top bounds */
2621 if (title->left + c_dx < 0) c_dx = 0;
2622 if (title->top + c_dy < 0) c_dy = 0;
2624 for (i = 0; i < y; i++)
2626 for (j = 0; j < x; j++)
2628 dx = j*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx;
2629 dy = i*(days->bottom - title->top + MC_CALENDAR_PADDING) + c_dy;
2631 OffsetRect(&infoPtr->calendars[i*x+j].title, dx, dy);
2632 OffsetRect(&infoPtr->calendars[i*x+j].titlemonth, dx, dy);
2633 OffsetRect(&infoPtr->calendars[i*x+j].titleyear, dx, dy);
2634 OffsetRect(&infoPtr->calendars[i*x+j].wdays, dx, dy);
2635 OffsetRect(&infoPtr->calendars[i*x+j].weeknums, dx, dy);
2636 OffsetRect(&infoPtr->calendars[i*x+j].days, dx, dy);
2640 OffsetRect(prev, c_dx, c_dy);
2641 OffsetRect(next, (x-1)*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx, c_dy);
2643 i = infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx;
2644 todayrect->left = infoPtr->calendars[i].title.left;
2645 todayrect->right = infoPtr->calendars[i].title.right;
2646 todayrect->top = infoPtr->calendars[i].days.bottom;
2647 todayrect->bottom = infoPtr->calendars[i].days.bottom + infoPtr->height_increment;
2649 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2650 infoPtr->width_increment,infoPtr->height_increment,
2651 wine_dbgstr_rect(&client),
2652 wine_dbgstr_rect(title),
2653 wine_dbgstr_rect(wdays),
2654 wine_dbgstr_rect(days),
2655 wine_dbgstr_rect(todayrect));
2658 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2660 TRACE("(width=%d, height=%d)\n", Width, Height);
2662 MONTHCAL_UpdateSize(infoPtr);
2663 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2665 return 0;
2668 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2670 return (LRESULT)infoPtr->hFont;
2673 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2675 HFONT hOldFont;
2676 LOGFONTW lf;
2678 if (!hFont) return 0;
2680 hOldFont = infoPtr->hFont;
2681 infoPtr->hFont = hFont;
2683 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2684 lf.lfWeight = FW_BOLD;
2685 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2687 MONTHCAL_UpdateSize(infoPtr);
2689 if (redraw)
2690 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2692 return (LRESULT)hOldFont;
2695 /* update theme after a WM_THEMECHANGED message */
2696 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2698 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2699 CloseThemeData (theme);
2700 OpenThemeData (infoPtr->hwndSelf, themeClass);
2701 return 0;
2704 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2705 const STYLESTRUCT *lpss)
2707 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2708 wStyleType, lpss->styleOld, lpss->styleNew);
2710 if (wStyleType != GWL_STYLE) return 0;
2712 infoPtr->dwStyle = lpss->styleNew;
2714 /* make room for week numbers */
2715 if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2716 MONTHCAL_UpdateSize(infoPtr);
2718 return 0;
2721 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2722 STYLESTRUCT *lpss)
2724 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2725 wStyleType, lpss->styleOld, lpss->styleNew);
2727 /* block MCS_MULTISELECT change */
2728 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2730 if (lpss->styleOld & MCS_MULTISELECT)
2731 lpss->styleNew |= MCS_MULTISELECT;
2732 else
2733 lpss->styleNew &= ~MCS_MULTISELECT;
2736 /* block MCS_DAYSTATE change */
2737 if ((lpss->styleNew ^ lpss->styleOld) & MCS_DAYSTATE)
2739 if (lpss->styleOld & MCS_DAYSTATE)
2740 lpss->styleNew |= MCS_DAYSTATE;
2741 else
2742 lpss->styleNew &= ~MCS_DAYSTATE;
2745 return 0;
2748 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2749 static LRESULT
2750 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2752 MONTHCAL_INFO *infoPtr;
2754 /* allocate memory for info structure */
2755 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2756 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2758 if (infoPtr == NULL) {
2759 ERR("could not allocate info memory!\n");
2760 return 0;
2763 infoPtr->hwndSelf = hwnd;
2764 infoPtr->hwndNotify = lpcs->hwndParent;
2765 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2766 infoPtr->dim.cx = infoPtr->dim.cy = 1;
2767 infoPtr->calendars = Alloc(sizeof(CALENDAR_INFO));
2768 if (!infoPtr->calendars) goto fail;
2769 infoPtr->monthdayState = Alloc(3*sizeof(MONTHDAYSTATE));
2770 if (!infoPtr->monthdayState) goto fail;
2772 /* initialize info structure */
2773 /* FIXME: calculate systemtime ->> localtime(subtract timezoneinfo) */
2775 GetLocalTime(&infoPtr->todaysDate);
2776 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2778 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2780 infoPtr->colors[MCSC_BACKGROUND] = comctl32_color.clrWindow;
2781 infoPtr->colors[MCSC_TEXT] = comctl32_color.clrWindowText;
2782 infoPtr->colors[MCSC_TITLEBK] = comctl32_color.clrActiveCaption;
2783 infoPtr->colors[MCSC_TITLETEXT] = comctl32_color.clrWindow;
2784 infoPtr->colors[MCSC_MONTHBK] = comctl32_color.clrWindow;
2785 infoPtr->colors[MCSC_TRAILINGTEXT] = comctl32_color.clrGrayText;
2787 infoPtr->brushes[BrushBackground] = CreateSolidBrush(infoPtr->colors[MCSC_BACKGROUND]);
2788 infoPtr->brushes[BrushTitle] = CreateSolidBrush(infoPtr->colors[MCSC_TITLEBK]);
2789 infoPtr->brushes[BrushMonth] = CreateSolidBrush(infoPtr->colors[MCSC_MONTHBK]);
2791 infoPtr->pens[PenRed] = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
2792 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[MCSC_TEXT]);
2794 infoPtr->minSel = infoPtr->todaysDate;
2795 infoPtr->maxSel = infoPtr->todaysDate;
2796 infoPtr->calendars[0].month = infoPtr->todaysDate;
2797 infoPtr->isUnicode = TRUE;
2799 /* setup control layout and day state data */
2800 MONTHCAL_UpdateSize(infoPtr);
2802 /* today auto update timer, to be freed only on control destruction */
2803 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2805 OpenThemeData (infoPtr->hwndSelf, themeClass);
2807 return 0;
2809 fail:
2810 Free(infoPtr->monthdayState);
2811 Free(infoPtr->calendars);
2812 Free(infoPtr);
2813 return 0;
2816 static LRESULT
2817 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2819 INT i;
2821 /* free month calendar info data */
2822 Free(infoPtr->monthdayState);
2823 Free(infoPtr->calendars);
2824 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2826 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2828 for (i = 0; i < BrushLast; i++) DeleteObject(infoPtr->brushes[i]);
2829 for (i = 0; i < PenLast; i++) DeleteObject(infoPtr->pens[i]);
2831 Free(infoPtr);
2832 return 0;
2836 * Handler for WM_NOTIFY messages
2838 static LRESULT
2839 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2841 /* notification from year edit updown */
2842 if (hdr->code == UDN_DELTAPOS)
2844 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2846 if (hdr->hwndFrom == infoPtr->hWndYearUpDown && nmud->iDelta)
2848 /* year value limits are set up explicitly after updown creation */
2849 MONTHCAL_Scroll(infoPtr, 12 * nmud->iDelta);
2850 MONTHCAL_NotifyDayState(infoPtr);
2851 MONTHCAL_NotifySelectionChange(infoPtr);
2854 return 0;
2857 static inline BOOL
2858 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2860 BOOL prev = infoPtr->isUnicode;
2861 infoPtr->isUnicode = isUnicode;
2862 return prev;
2865 static inline BOOL
2866 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2868 return infoPtr->isUnicode;
2871 static LRESULT WINAPI
2872 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2874 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0);
2876 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2878 if (!infoPtr && (uMsg != WM_CREATE))
2879 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2880 switch(uMsg)
2882 case MCM_GETCURSEL:
2883 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2885 case MCM_SETCURSEL:
2886 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2888 case MCM_GETMAXSELCOUNT:
2889 return MONTHCAL_GetMaxSelCount(infoPtr);
2891 case MCM_SETMAXSELCOUNT:
2892 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2894 case MCM_GETSELRANGE:
2895 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2897 case MCM_SETSELRANGE:
2898 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2900 case MCM_GETMONTHRANGE:
2901 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2903 case MCM_SETDAYSTATE:
2904 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2906 case MCM_GETMINREQRECT:
2907 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2909 case MCM_GETCOLOR:
2910 return MONTHCAL_GetColor(infoPtr, wParam);
2912 case MCM_SETCOLOR:
2913 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2915 case MCM_GETTODAY:
2916 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2918 case MCM_SETTODAY:
2919 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2921 case MCM_HITTEST:
2922 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2924 case MCM_GETFIRSTDAYOFWEEK:
2925 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2927 case MCM_SETFIRSTDAYOFWEEK:
2928 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2930 case MCM_GETRANGE:
2931 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2933 case MCM_SETRANGE:
2934 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2936 case MCM_GETMONTHDELTA:
2937 return MONTHCAL_GetMonthDelta(infoPtr);
2939 case MCM_SETMONTHDELTA:
2940 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2942 case MCM_GETMAXTODAYWIDTH:
2943 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2945 case MCM_SETUNICODEFORMAT:
2946 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2948 case MCM_GETUNICODEFORMAT:
2949 return MONTHCAL_GetUnicodeFormat(infoPtr);
2951 case MCM_GETCALENDARCOUNT:
2952 return MONTHCAL_GetCalCount(infoPtr);
2954 case WM_GETDLGCODE:
2955 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2957 case WM_RBUTTONUP:
2958 return MONTHCAL_RButtonUp(infoPtr, lParam);
2960 case WM_LBUTTONDOWN:
2961 return MONTHCAL_LButtonDown(infoPtr, lParam);
2963 case WM_MOUSEMOVE:
2964 return MONTHCAL_MouseMove(infoPtr, lParam);
2966 case WM_LBUTTONUP:
2967 return MONTHCAL_LButtonUp(infoPtr, lParam);
2969 case WM_PAINT:
2970 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2972 case WM_PRINTCLIENT:
2973 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2975 case WM_ERASEBKGND:
2976 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2978 case WM_SETFOCUS:
2979 return MONTHCAL_SetFocus(infoPtr);
2981 case WM_SIZE:
2982 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2984 case WM_NOTIFY:
2985 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2987 case WM_CREATE:
2988 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2990 case WM_SETFONT:
2991 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2993 case WM_GETFONT:
2994 return MONTHCAL_GetFont(infoPtr);
2996 case WM_TIMER:
2997 return MONTHCAL_Timer(infoPtr, wParam);
2999 case WM_THEMECHANGED:
3000 return theme_changed (infoPtr);
3002 case WM_DESTROY:
3003 return MONTHCAL_Destroy(infoPtr);
3005 case WM_SYSCOLORCHANGE:
3006 COMCTL32_RefreshSysColors();
3007 return 0;
3009 case WM_STYLECHANGED:
3010 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
3012 case WM_STYLECHANGING:
3013 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
3015 default:
3016 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
3017 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
3018 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
3023 void
3024 MONTHCAL_Register(void)
3026 WNDCLASSW wndClass;
3028 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
3029 wndClass.style = CS_GLOBALCLASS;
3030 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
3031 wndClass.cbClsExtra = 0;
3032 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
3033 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3034 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
3035 wndClass.lpszClassName = MONTHCAL_CLASSW;
3037 RegisterClassW(&wndClass);
3041 void
3042 MONTHCAL_Unregister(void)
3044 UnregisterClassW(MONTHCAL_CLASSW, NULL);