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 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
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.
35 * -- MCM_[GS]ETUNICODEFORMAT
36 * -- MONTHCAL_GetMonthRange
37 * -- handle resources better (doesn't work now);
38 * -- take care of internationalization.
39 * -- keyboard handling.
58 #include "wine/unicode.h"
59 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
63 #define MC_SEL_LBUTUP 1 /* Left button released */
64 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
65 #define MC_PREVPRESSED 4 /* Prev month button pressed */
66 #define MC_NEXTPRESSED 8 /* Next month button pressed */
67 #define MC_PREVNEXTMONTHDELAY 350 /* when continuously pressing `next/prev
68 month', wait 500 ms before going
69 to the next/prev month */
70 #define MC_TODAYUPDATEDELAY 120000 /* time between today check for update (2 min) */
72 #define MC_PREVNEXTMONTHTIMER 1 /* Timer ID's */
73 #define MC_TODAYUPDATETIMER 2
75 #define countof(arr) (sizeof(arr)/sizeof(arr[0]))
77 /* convert from days to 100 nanoseconds unit - used as FILETIME unit */
78 #define DAYSTO100NSECS(days) (((ULONGLONG)(days))*24*60*60*10000000)
83 DWORD dwStyle; /* cached GWL_STYLE */
96 int firstDayplace; /* place of the first day of the current month */
97 INT delta; /* scroll rate; # of months that the */
98 /* control moves when user clicks a scroll button */
99 int visible; /* # of months visible */
100 int firstDay; /* Start month calendar with firstDay's day,
101 stored in SYSTEMTIME format */
102 BOOL firstDaySet; /* first week day differs from locale defined */
105 MONTHDAYSTATE *monthdayState;
106 SYSTEMTIME todaysDate;
107 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
108 int status; /* See MC_SEL flags */
109 SYSTEMTIME firstSel; /* first selected day */
113 SYSTEMTIME curSel; /* contains currently selected year, month and day */
114 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
119 RECT title; /* rect for the header above the calendar */
120 RECT titlebtnnext; /* the `next month' button in the header */
121 RECT titlebtnprev; /* the `prev month' button in the header */
122 RECT titlemonth; /* the `month name' txt in the header */
123 RECT titleyear; /* the `year number' txt in the header */
124 RECT wdays; /* week days at top */
125 RECT days; /* calendar area */
126 RECT weeknums; /* week numbers at left side */
127 RECT todayrect; /* `today: xx/xx/xx' text rect */
128 HWND hwndNotify; /* Window to receive the notifications */
129 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
130 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
131 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
134 /* Offsets of days in the week to the weekday of january 1 in a leap year */
135 static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
137 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
139 /* empty SYSTEMTIME const */
140 static const SYSTEMTIME st_null;
141 /* valid date limits */
142 static const SYSTEMTIME max_allowed_date = { .wYear = 9999, .wMonth = 12, .wDay = 31 };
143 static const SYSTEMTIME min_allowed_date = { .wYear = 1752, .wMonth = 9, .wDay = 14 };
146 #define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0))
148 /* helper functions */
150 /* send a single MCN_SELCHANGE notification */
151 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
155 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
156 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
157 nmsc.nmhdr.code = MCN_SELCHANGE;
158 nmsc.stSelStart = infoPtr->minSel;
159 nmsc.stSelEnd = infoPtr->maxSel;
160 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
163 /* send a single MCN_SELECT notification */
164 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
168 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
169 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
170 nmsc.nmhdr.code = MCN_SELECT;
171 nmsc.stSelStart = infoPtr->minSel;
172 nmsc.stSelEnd = infoPtr->maxSel;
174 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
177 /* returns the number of days in any given month, checking for leap days */
178 /* january is 1, december is 12 */
179 int MONTHCAL_MonthLength(int month, int year)
181 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0};
182 /* Wrap around, this eases handling. Getting length only we shouldn't care
183 about year change here cause January and December have
184 the same day quantity */
190 /* if we have a leap year add 1 day to February */
191 /* a leap year is a year either divisible by 400 */
192 /* or divisible by 4 and not by 100 */
193 if(month == 2) { /* February */
194 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
195 (year%4 == 0)) ? 1 : 0);
198 return mdays[month - 1];
202 /* compares timestamps using date part only */
203 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
205 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
206 (first->wDay == second->wDay);
209 /* make sure that date fields are valid */
210 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
212 if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
213 if(time->wDayOfWeek > 6) return FALSE;
214 if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear))
220 /* Compares two dates in SYSTEMTIME format
224 * [I] first : pointer to valid first date data to compare
225 * [I] second : pointer to valid second date data to compare
229 * -1 : first < second
230 * 0 : first == second
233 * Note that no date validation performed, alreadt validated values expected.
235 static LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
237 FILETIME ft_first, ft_second;
239 SystemTimeToFileTime(first, &ft_first);
240 SystemTimeToFileTime(second, &ft_second);
242 return CompareFileTime(&ft_first, &ft_second);
245 /* Checks largest possible date range and configured one
249 * [I] infoPtr : valid pointer to control data
250 * [I] date : pointer to valid date data to check
254 * TRUE - date whithin largest and configured range
255 * FALSE - date is outside largest or configured range
257 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date)
259 if((MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) ||
260 (MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1)) return FALSE;
262 if(infoPtr->rangeValid & GDTR_MAX) {
263 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) return FALSE;
266 if(infoPtr->rangeValid & GDTR_MIN) {
267 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) return FALSE;
273 /* Checks passed range width with configured maximum selection count
277 * [I] infoPtr : valid pointer to control data
278 * [I] range0 : pointer to valid date data (requested bound)
279 * [I] range1 : pointer to valid date data (primary bound)
280 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
282 * Adjust value computed basing on primary bound and current maximum selection
283 * count. For simple range check (without adjusted value required) (range0, range1)
284 * relation means nothing.
288 * TRUE - range is shorter or equal to maximum
289 * FALSE - range is larger than maximum
291 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
292 const SYSTEMTIME *range0,
293 const SYSTEMTIME *range1,
296 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
297 FILETIME ft_range0, ft_range1;
300 SystemTimeToFileTime(range0, &ft_range0);
301 SystemTimeToFileTime(range1, &ft_range1);
303 ul_range0.LowPart = ft_range0.dwLowDateTime;
304 ul_range0.HighPart = ft_range0.dwHighDateTime;
305 ul_range1.LowPart = ft_range1.dwLowDateTime;
306 ul_range1.HighPart = ft_range1.dwHighDateTime;
308 cmp = CompareFileTime(&ft_range0, &ft_range1);
311 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
313 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
315 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
319 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
321 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
323 ft_range0.dwLowDateTime = ul_range0.LowPart;
324 ft_range0.dwHighDateTime = ul_range0.HighPart;
325 FileTimeToSystemTime(&ft_range0, adjust);
333 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
334 Milliseconds are intentionaly not validated. */
335 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
337 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
343 /* Copies timestamp part only.
347 * [I] from : source date
350 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
352 to->wHour = from->wHour;
353 to->wMinute = from->wMinute;
354 to->wSecond = from->wSecond;
357 /* Copies date part only.
361 * [I] from : source date
364 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
366 to->wYear = from->wYear;
367 to->wMonth = from->wMonth;
368 to->wDay = from->wDay;
369 to->wDayOfWeek = from->wDayOfWeek;
372 /* Note:Depending on DST, this may be offset by a day.
373 Need to find out if we're on a DST place & adjust the clock accordingly.
374 Above function assumes we have a valid data.
375 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
379 /* Returns the day in the week
382 * [i] day : day of month [1, 31]
383 * [I] month : month number [1, 12]
384 * [I] year : year value
387 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
389 int MONTHCAL_CalculateDayOfWeek(WORD day, WORD month, WORD year)
393 return((year + year/4 - year/100 + year/400 +
394 DayOfWeekTable[month-1] + day ) % 7);
397 /* properly updates date to point on next month */
398 void inline MONTHCAL_GetNextMonth(SYSTEMTIME *date)
400 if(++date->wMonth > 12)
405 date->wDayOfWeek = MONTHCAL_CalculateDayOfWeek(date->wDay, date->wMonth,
409 /* properly updates date to point on prev month */
410 void inline MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
412 if(--date->wMonth < 1)
417 date->wDayOfWeek = MONTHCAL_CalculateDayOfWeek(date->wDay, date->wMonth,
421 /* From a given point, calculate the row (weekpos), column(daypos)
422 and day in the calendar. day== 0 mean the last day of tha last month
424 static int MONTHCAL_CalcDayFromPos(const MONTHCAL_INFO *infoPtr, int x, int y,
425 int *daypos,int *weekpos)
427 int retval, firstDay;
430 GetClientRect(infoPtr->hwndSelf, &rcClient);
432 /* if the point is outside the x bounds of the window put
433 it at the boundary */
434 if (x > rcClient.right)
438 *daypos = (x - infoPtr->days.left ) / infoPtr->width_increment;
439 *weekpos = (y - infoPtr->days.top ) / infoPtr->height_increment;
441 firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear)+6 - infoPtr->firstDay)%7;
442 retval = *daypos + (7 * *weekpos) - firstDay;
446 /* day is the day of the month, 1 == 1st day of the month */
447 /* sets x and y to be the position of the day */
448 /* x == day, y == week where(0,0) == firstDay, 1st week */
449 static void MONTHCAL_CalcDayXY(const MONTHCAL_INFO *infoPtr, int day, int month,
452 int firstDay, prevMonth;
454 firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear) +6 - infoPtr->firstDay)%7;
456 if(month==infoPtr->curSel.wMonth) {
457 *x = (day + firstDay) % 7;
458 *y = (day + firstDay - *x) / 7;
461 if(month < infoPtr->curSel.wMonth) {
462 prevMonth = month - 1;
466 *x = (MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear) - firstDay) % 7;
471 *y = MONTHCAL_MonthLength(month, infoPtr->curSel.wYear - 1) / 7;
472 *x = (day + firstDay + MONTHCAL_MonthLength(month,
473 infoPtr->curSel.wYear)) % 7;
477 /* x: column(day), y: row(week) */
478 static void MONTHCAL_CalcDayRect(const MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
480 r->left = infoPtr->days.left + x * infoPtr->width_increment;
481 r->right = r->left + infoPtr->width_increment;
482 r->top = infoPtr->days.top + y * infoPtr->height_increment;
483 r->bottom = r->top + infoPtr->textHeight;
487 /* sets the RECT struct r to the rectangle around the day and month */
488 /* day is the day value of the month(1 == 1st), month is the month */
489 /* value(january == 1, december == 12) */
490 static inline void MONTHCAL_CalcPosFromDay(const MONTHCAL_INFO *infoPtr,
491 int day, int month, RECT *r)
495 MONTHCAL_CalcDayXY(infoPtr, day, month, &x, &y);
496 MONTHCAL_CalcDayRect(infoPtr, r, x, y);
499 /* Focused day helper:
501 - set focused date to given value;
502 - reset to zero value if NULL passed;
503 - invalidate previous and new day rectangle only if needed.
505 Returns TRUE if focused day changed, FALSE otherwise.
507 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
513 /* there's nothing to do if it's the same date,
514 mouse move within same date rectangle case */
515 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
517 /* invalidate old focused day */
518 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->focusedSel.wDay,
519 infoPtr->focusedSel.wMonth, &r);
520 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
522 infoPtr->focusedSel = *st;
525 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->focusedSel.wDay,
526 infoPtr->focusedSel.wMonth, &r);
528 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
529 infoPtr->focusedSel = st_null;
531 /* on set invalidates new day, on reset clears previous focused day */
532 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
537 /* day is the day in the month(1 == 1st of the month) */
538 /* month is the month value(1 == january, 12 == december) */
539 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month)
541 HPEN hRedPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
542 HPEN hOldPen2 = SelectObject(hdc, hRedPen);
546 MONTHCAL_CalcPosFromDay(infoPtr, day, month, &day_rect);
548 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
549 Rectangle(hdc, day_rect.left, day_rect.top, day_rect.right, day_rect.bottom);
551 SelectObject(hdc, hOldBrush);
552 DeleteObject(hRedPen);
553 SelectObject(hdc, hOldPen2);
556 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month,
557 int x, int y, int bold)
559 static const WCHAR fmtW[] = { '%','d',0 };
562 static BOOL haveBoldFont, haveSelectedDay = FALSE;
567 wsprintfW(buf, fmtW, day);
569 /* No need to check styles: when selection is not valid, it is set to zero.
570 * 1<day<31, so everything is OK.
573 MONTHCAL_CalcDayRect(infoPtr, &r, x, y);
575 if((day>=infoPtr->minSel.wDay) && (day<=infoPtr->maxSel.wDay)
576 && (month == infoPtr->curSel.wMonth)) {
579 TRACE("%d %d %d\n",day, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
580 TRACE("%s\n", wine_dbgstr_rect(&r));
581 oldCol = SetTextColor(hdc, infoPtr->monthbk);
582 oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
583 hbr = GetSysColorBrush(COLOR_HIGHLIGHT);
584 FillRect(hdc, &r, hbr);
586 /* FIXME: this may need to be changed now b/c of the other
587 drawing changes 11/3/99 CMM */
588 r2.left = r.left - 0.25 * infoPtr->textWidth;
590 r2.right = r.left + 0.5 * infoPtr->textWidth;
591 r2.bottom = r.bottom;
592 if(haveSelectedDay) FillRect(hdc, &r2, hbr);
593 haveSelectedDay = TRUE;
595 haveSelectedDay = FALSE;
598 /* need to add some code for multiple selections */
600 if((bold) &&(!haveBoldFont)) {
601 SelectObject(hdc, infoPtr->hBoldFont);
604 if((!bold) &&(haveBoldFont)) {
605 SelectObject(hdc, infoPtr->hFont);
606 haveBoldFont = FALSE;
609 SetBkMode(hdc,TRANSPARENT);
610 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
612 if(haveSelectedDay) {
613 SetTextColor(hdc, oldCol);
614 SetBkColor(hdc, oldBk);
619 static void paint_button (MONTHCAL_INFO *infoPtr, HDC hdc, BOOL btnNext)
621 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
622 RECT *r = btnNext ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
623 BOOL pressed = btnNext ? (infoPtr->status & MC_NEXTPRESSED) :
624 (infoPtr->status & MC_PREVPRESSED);
627 static const int states[] = {
629 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
631 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
633 int stateNum = btnNext ? 3 : 0;
638 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
640 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
644 int style = btnNext ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
646 style |= DFCS_PUSHED;
649 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
652 DrawFrameControl(hdc, r, DFC_SCROLL, style);
657 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
659 static const WCHAR fmt_monthW[] = { '%','s',' ','%','l','d',0 };
660 RECT *title=&infoPtr->title;
661 RECT *titlemonth=&infoPtr->titlemonth;
662 RECT *titleyear=&infoPtr->titleyear;
664 int i, j, m, mask, day, firstDay, prevMonth;
665 int textHeight = infoPtr->textHeight;
672 COLORREF oldTextColor, oldBkColor;
674 RECT rcDay; /* used in MONTHCAL_CalcDayRect() */
677 oldTextColor = SetTextColor(hdc, comctl32_color.clrWindowText);
679 /* fill background */
680 hbr = CreateSolidBrush (infoPtr->bk);
681 FillRect(hdc, &ps->rcPaint, hbr);
685 if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
687 hbr = CreateSolidBrush(infoPtr->titlebk);
688 FillRect(hdc, title, hbr);
692 /* if the previous button is pressed draw it depressed */
693 if(IntersectRect(&rcTemp, &(ps->rcPaint), &infoPtr->titlebtnprev))
694 paint_button(infoPtr, hdc, FALSE);
696 /* if next button is depressed draw it depressed */
697 if(IntersectRect(&rcTemp, &(ps->rcPaint), &infoPtr->titlebtnnext))
698 paint_button(infoPtr, hdc, TRUE);
700 oldBkColor = SetBkColor(hdc, infoPtr->titlebk);
701 SetTextColor(hdc, infoPtr->titletxt);
702 currentFont = SelectObject(hdc, infoPtr->hBoldFont);
704 GetLocaleInfoW( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+infoPtr->curSel.wMonth -1,
706 wsprintfW(buf, fmt_monthW, buf1, infoPtr->curSel.wYear);
708 if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
710 DrawTextW(hdc, buf, strlenW(buf), title,
711 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
714 /* titlemonth left/right contained rect for whole titletxt('June 1999')
715 * MCM_HitTestInfo wants month & year rects, so prepare these now.
716 *(no, we can't draw them separately; the whole text is centered)
718 GetTextExtentPoint32W(hdc, buf, strlenW(buf), &size);
719 titlemonth->left = title->right / 2 + title->left / 2 - size.cx / 2;
720 titleyear->right = title->right / 2 + title->left / 2 + size.cx / 2;
721 GetTextExtentPoint32W(hdc, buf1, strlenW(buf1), &size);
722 titlemonth->right = titlemonth->left + size.cx;
723 titleyear->left = titlemonth->right;
725 /* draw month area */
726 rcTemp.top=infoPtr->wdays.top;
727 rcTemp.left=infoPtr->wdays.left;
728 rcTemp.bottom=infoPtr->todayrect.bottom;
729 rcTemp.right =infoPtr->todayrect.right;
730 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcTemp))
732 hbr = CreateSolidBrush(infoPtr->monthbk);
733 FillRect(hdc, &rcTemp, hbr);
737 /* draw line under day abbreviations */
739 MoveToEx(hdc, infoPtr->days.left + 3, title->bottom + textHeight + 1, NULL);
740 LineTo(hdc, infoPtr->days.right - 3, title->bottom + textHeight + 1);
742 prevMonth = infoPtr->curSel.wMonth - 1;
743 if(prevMonth == 0) /* if curSel.wMonth is january(1) prevMonth is */
744 prevMonth = 12; /* december(12) of the previous year */
746 infoPtr->wdays.left = infoPtr->days.left = infoPtr->weeknums.right;
748 /* draw day abbreviations */
749 SelectObject(hdc, infoPtr->hFont);
750 SetBkColor(hdc, infoPtr->monthbk);
751 SetTextColor(hdc, infoPtr->trailingtxt);
753 /* rectangle to draw a single day abbreviation within */
754 dayrect = infoPtr->wdays;
755 dayrect.right = dayrect.left + infoPtr->width_increment;
757 i = infoPtr->firstDay;
759 for(j = 0; j < 7; j++) {
760 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
761 DrawTextW(hdc, buf, strlenW(buf), &dayrect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
762 dayrect.left += infoPtr->width_increment;
763 dayrect.right += infoPtr->width_increment;
766 /* draw day numbers; first, the previous month */
767 firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear);
769 day = MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear) +
770 (infoPtr->firstDay - firstDay)%7 + 1;
772 if (day > MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear))
774 startofprescal = day;
779 while(day <= MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear)) {
780 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
781 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
783 MONTHCAL_DrawDay(infoPtr, hdc, day, prevMonth, i, 0,
784 infoPtr->monthdayState[m] & mask);
792 /* draw `current' month */
794 day = 1; /* start at the beginning of the current month */
796 infoPtr->firstDayplace = i;
797 SetTextColor(hdc, infoPtr->txt);
801 /* draw the first week of the current month */
803 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
804 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
806 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth, i, 0,
807 infoPtr->monthdayState[m] & mask);
815 j = 1; /* move to the 2nd week of the current month */
816 i = 0; /* move back to sunday */
817 while(day <= MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear)) {
818 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
819 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
821 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth, i, j,
822 infoPtr->monthdayState[m] & mask);
827 if(i>6) { /* past saturday, goto the next weeks sunday */
833 /* draw `next' month */
835 day = 1; /* start at the first day of the next month */
839 SetTextColor(hdc, infoPtr->trailingtxt);
840 while((i<7) &&(j<6)) {
841 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
842 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
844 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth + 1, i, j,
845 infoPtr->monthdayState[m] & mask);
851 if(i==7) { /* past saturday, go to next week's sunday */
856 SetTextColor(hdc, infoPtr->txt);
858 /* draw today mark rectangle */
859 if((infoPtr->curSel.wMonth == infoPtr->todaysDate.wMonth) &&
860 (infoPtr->curSel.wYear == infoPtr->todaysDate.wYear) &&
861 !(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
863 MONTHCAL_CircleDay(infoPtr, hdc, infoPtr->todaysDate.wDay, infoPtr->todaysDate.wMonth);
866 /* draw focused day */
867 if(!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
869 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->focusedSel.wDay,
870 infoPtr->focusedSel.wMonth, &rcDay);
872 DrawFocusRect(hdc, &rcDay);
875 /* draw `today' date if style allows it, and draw a circle before today's
876 * date if necessary */
877 if(!(infoPtr->dwStyle & MCS_NOTODAY)) {
878 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
879 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
882 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
883 /*day is the number of days from nextmonth we put on the calendar */
884 MONTHCAL_CircleDay(infoPtr, hdc,
885 day+MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear),
886 infoPtr->curSel.wMonth);
888 if (!LoadStringW(COMCTL32_hModule, IDM_TODAY, buf1, countof(buf1)))
890 WARN("Can't load resource\n");
891 strcpyW(buf1, todayW);
893 MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
894 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
895 buf2, countof(buf2));
896 wsprintfW(buf, fmt_todayW, buf1, buf2);
897 SelectObject(hdc, infoPtr->hBoldFont);
899 DrawTextW(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
900 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rtoday))
902 DrawTextW(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
904 SelectObject(hdc, infoPtr->hFont);
907 /* eventually draw week numbers */
908 if(infoPtr->dwStyle & MCS_WEEKNUMBERS) {
909 static const WCHAR fmt_weekW[] = { '%','d',0 }; /* week numbers format */
910 int mindays, weeknum, weeknum1;
912 /* Rules what week to call the first week of a new year:
913 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
914 The week containing Jan 1 is the first week of year
915 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
916 First week of year must contain 4 days of the new year
917 LOCALE_IFIRSTWEEKOFYEAR == 1 (what contries?)
918 The first week of the year must contain only days of the new year
920 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
921 weeknum = atoiW(buf);
932 if (infoPtr->curSel.wMonth < 2)
934 /* calculate all those exceptions for january */
935 weeknum1 = MONTHCAL_CalculateDayOfWeek(1, 1, infoPtr->curSel.wYear);
936 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
941 for(i = 0; i < 11; i++)
942 weeknum += MONTHCAL_MonthLength(i+1, infoPtr->curSel.wYear - 1);
944 weeknum += startofprescal + 7;
946 weeknum1 = MONTHCAL_CalculateDayOfWeek(1, 1, infoPtr->curSel.wYear - 1);
947 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
953 for(i = 0; i < prevMonth - 1; i++)
954 weeknum += MONTHCAL_MonthLength(i+1, infoPtr->curSel.wYear);
956 weeknum += startofprescal + 7;
958 weeknum1 = MONTHCAL_CalculateDayOfWeek(1, 1, infoPtr->curSel.wYear);
959 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
962 dayrect = infoPtr->weeknums;
963 dayrect.bottom = dayrect.top + infoPtr->height_increment;
965 for(i = 0; i < 6; i++) {
966 if((i == 0) && (weeknum > 50))
968 wsprintfW(buf, fmt_weekW, weeknum);
971 else if((i == 5) && (weeknum > 47))
973 wsprintfW(buf, fmt_weekW, 1);
976 wsprintfW(buf, fmt_weekW, weeknum + i);
978 DrawTextW(hdc, buf, -1, &dayrect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
979 dayrect.top += infoPtr->height_increment;
980 dayrect.bottom += infoPtr->height_increment;
983 MoveToEx(hdc, infoPtr->weeknums.right, infoPtr->weeknums.top + 3 , NULL);
984 LineTo(hdc, infoPtr->weeknums.right, infoPtr->weeknums.bottom);
987 /* currentFont was font at entering Refresh */
988 SetBkColor(hdc, oldBkColor);
989 SelectObject(hdc, currentFont);
990 SetTextColor(hdc, oldTextColor);
995 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, LPRECT lpRect)
997 TRACE("rect %p\n", lpRect);
999 if(!lpRect) return FALSE;
1001 lpRect->left = infoPtr->title.left;
1002 lpRect->top = infoPtr->title.top;
1003 lpRect->right = infoPtr->title.right;
1004 lpRect->bottom = infoPtr->todayrect.bottom;
1006 AdjustWindowRect(lpRect, infoPtr->dwStyle, FALSE);
1008 /* minimal rectangle is zero based */
1009 OffsetRect(lpRect, -lpRect->left, -lpRect->top);
1011 TRACE("%s\n", wine_dbgstr_rect(lpRect));
1018 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, INT index)
1023 case MCSC_BACKGROUND:
1026 return infoPtr->txt;
1028 return infoPtr->titlebk;
1029 case MCSC_TITLETEXT:
1030 return infoPtr->titletxt;
1032 return infoPtr->monthbk;
1033 case MCSC_TRAILINGTEXT:
1034 return infoPtr->trailingtxt;
1042 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, INT index, COLORREF color)
1046 TRACE("%d: color %08x\n", index, color);
1049 case MCSC_BACKGROUND:
1051 infoPtr->bk = color;
1054 prev = infoPtr->txt;
1055 infoPtr->txt = color;
1058 prev = infoPtr->titlebk;
1059 infoPtr->titlebk = color;
1061 case MCSC_TITLETEXT:
1062 prev=infoPtr->titletxt;
1063 infoPtr->titletxt = color;
1066 prev = infoPtr->monthbk;
1067 infoPtr->monthbk = color;
1069 case MCSC_TRAILINGTEXT:
1070 prev = infoPtr->trailingtxt;
1071 infoPtr->trailingtxt = color;
1075 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND ? TRUE : FALSE);
1081 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1086 return infoPtr->delta;
1088 return infoPtr->visible;
1093 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1095 INT prev = infoPtr->delta;
1097 TRACE("delta %d\n", delta);
1099 infoPtr->delta = delta;
1104 static inline LRESULT
1105 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1109 /* convert from SYSTEMTIME to locale format */
1110 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1112 return MAKELONG(day, infoPtr->firstDaySet);
1116 /* Sets the first day of the week that will appear in the control
1120 * [I] infoPtr : valid pointer to control data
1121 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1125 * Low word contains previous first day,
1126 * high word indicates was first day forced with this message before or is
1127 * locale difined (TRUE - was forced, FALSE - wasn't).
1129 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1130 * FIXME: we need more error checking here
1133 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1135 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1144 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1145 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1147 new_day = atoiW(buf);
1149 infoPtr->firstDaySet = FALSE;
1153 new_day = 6; /* max first day allowed */
1154 infoPtr->firstDaySet = TRUE;
1158 /* Native behaviour for that case is broken: invalid date number >31
1159 got displayed at (0,0) position, current month starts always from
1160 (1,0) position. Should be implemnted here as well. */
1162 FIXME("No bug compatibility for day=%d\n", day);
1165 infoPtr->firstDaySet = TRUE;
1168 /* convert from locale to SYSTEMTIME format */
1169 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1171 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1178 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
1184 if(flag == GMR_VISIBLE)
1186 /*FIXME: currently multicalendar feature isn't implelented, so entirely
1187 visible month is current */
1188 st[0] = st[1] = infoPtr->curSel;
1191 st[0].wDayOfWeek = MONTHCAL_CalculateDayOfWeek(1, st[0].wMonth, st[0].wYear);
1193 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
1194 st[1].wDayOfWeek = MONTHCAL_CalculateDayOfWeek(st[1].wDay, st[1].wMonth,
1198 FIXME("only GMR_VISIBLE flag supported, got %d\n", flag);
1201 return infoPtr->monthRange;
1206 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1208 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1213 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1215 FILETIME ft_min, ft_max;
1217 TRACE("%x %p\n", limits, range);
1219 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1220 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1223 if (limits & GDTR_MIN)
1225 if (!MONTHCAL_ValidateTime(&range[0]))
1226 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1228 infoPtr->minDate = range[0];
1229 infoPtr->rangeValid |= GDTR_MIN;
1231 if (limits & GDTR_MAX)
1233 if (!MONTHCAL_ValidateTime(&range[1]))
1234 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1236 infoPtr->maxDate = range[1];
1237 infoPtr->rangeValid |= GDTR_MAX;
1240 /* Only one limit set - we are done */
1241 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1244 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1245 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1247 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1249 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1251 /* Native swaps limits only when both limits are being set. */
1252 SYSTEMTIME st_tmp = infoPtr->minDate;
1253 infoPtr->minDate = infoPtr->maxDate;
1254 infoPtr->maxDate = st_tmp;
1258 /* reset the other limit */
1259 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1260 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1261 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1270 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1272 TRACE("%p\n", range);
1274 if(!range) return FALSE;
1276 range[1] = infoPtr->maxDate;
1277 range[0] = infoPtr->minDate;
1279 return infoPtr->rangeValid;
1284 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1288 TRACE("%d %p\n", months, states);
1289 if(months != infoPtr->monthRange) return 0;
1291 for(i = 0; i < months; i++)
1292 infoPtr->monthdayState[i] = states[i];
1298 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1300 TRACE("%p\n", curSel);
1301 if(!curSel) return FALSE;
1302 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1304 *curSel = infoPtr->curSel;
1305 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1309 /* FIXME: if the specified date is not visible, make it visible */
1311 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1313 TRACE("%p\n", curSel);
1314 if(!curSel) return FALSE;
1315 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1317 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1318 /* exit earlier if selection equals current */
1319 if (MONTHCAL_IsDateEqual(&infoPtr->curSel, curSel)) return TRUE;
1321 if(!MONTHCAL_IsDateInValidRange(infoPtr, curSel)) return FALSE;
1323 infoPtr->minSel = *curSel;
1324 infoPtr->maxSel = *curSel;
1326 infoPtr->curSel = *curSel;
1328 /* FIXME: it's possible to reduce rectangle here */
1329 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1336 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1338 return infoPtr->maxSelCount;
1343 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1347 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1348 if(max <= 0) return FALSE;
1350 infoPtr->maxSelCount = max;
1357 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1359 TRACE("%p\n", range);
1361 if(!range) return FALSE;
1363 if(infoPtr->dwStyle & MCS_MULTISELECT)
1365 range[1] = infoPtr->maxSel;
1366 range[0] = infoPtr->minSel;
1367 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1376 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1378 TRACE("%p\n", range);
1380 if(!range) return FALSE;
1382 if(infoPtr->dwStyle & MCS_MULTISELECT)
1384 SYSTEMTIME old_range[2];
1386 /* adjust timestamps */
1387 if(!MONTHCAL_ValidateTime(&range[0]))
1388 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1389 if(!MONTHCAL_ValidateTime(&range[1]))
1390 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1392 /* maximum range exceeded */
1393 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1395 old_range[0] = infoPtr->minSel;
1396 old_range[1] = infoPtr->maxSel;
1398 /* swap if min > max */
1399 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1401 infoPtr->minSel = range[0];
1402 infoPtr->maxSel = range[1];
1406 infoPtr->minSel = range[1];
1407 infoPtr->maxSel = range[0];
1410 /* update day of week */
1411 infoPtr->minSel.wDayOfWeek =
1412 MONTHCAL_CalculateDayOfWeek(infoPtr->minSel.wDay, infoPtr->minSel.wMonth,
1413 infoPtr->minSel.wYear);
1414 infoPtr->maxSel.wDayOfWeek =
1415 MONTHCAL_CalculateDayOfWeek(infoPtr->maxSel.wDay, infoPtr->maxSel.wMonth,
1416 infoPtr->maxSel.wYear);
1418 /* redraw if bounds changed */
1419 /* FIXME: no actual need to redraw everything */
1420 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1421 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1423 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1426 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1435 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1437 TRACE("%p\n", today);
1439 if(!today) return FALSE;
1440 *today = infoPtr->todaysDate;
1444 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1448 * TRUE - today date changed
1449 * FALSE - today date isn't changed
1452 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1456 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1458 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->todaysDate.wDay,
1459 infoPtr->todaysDate.wMonth, &old_r);
1460 MONTHCAL_CalcPosFromDay(infoPtr, today->wDay, today->wMonth, &new_r);
1462 infoPtr->todaysDate = *today;
1464 /* only two days need redrawing */
1465 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1466 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1470 /* MCM_SETTODAT handler */
1472 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1474 TRACE("%p\n", today);
1476 if(!today) return FALSE;
1478 /* remember if date was set successfully */
1479 if(MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1485 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1491 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1496 ZeroMemory(&lpht->st, sizeof(lpht->st));
1498 /* Comment in for debugging...
1499 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,
1500 infoPtr->wdays.left, infoPtr->wdays.right,
1501 infoPtr->wdays.top, infoPtr->wdays.bottom,
1502 infoPtr->days.left, infoPtr->days.right,
1503 infoPtr->days.top, infoPtr->days.bottom,
1504 infoPtr->todayrect.left, infoPtr->todayrect.right,
1505 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1506 infoPtr->weeknums.left, infoPtr->weeknums.right,
1507 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1510 /* are we in the header? */
1512 if(PtInRect(&infoPtr->title, lpht->pt)) {
1513 if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1514 retval = MCHT_TITLEBTNPREV;
1517 if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1518 retval = MCHT_TITLEBTNNEXT;
1521 if(PtInRect(&infoPtr->titlemonth, lpht->pt)) {
1522 retval = MCHT_TITLEMONTH;
1525 if(PtInRect(&infoPtr->titleyear, lpht->pt)) {
1526 retval = MCHT_TITLEYEAR;
1530 retval = MCHT_TITLE;
1534 day = MONTHCAL_CalcDayFromPos(infoPtr,x,y,&wday,&wnum);
1535 if(PtInRect(&infoPtr->wdays, lpht->pt)) {
1536 retval = MCHT_CALENDARDAY;
1537 lpht->st.wYear = infoPtr->curSel.wYear;
1538 lpht->st.wMonth = (day < 1)? infoPtr->curSel.wMonth -1 : infoPtr->curSel.wMonth;
1539 lpht->st.wDay = (day < 1)?
1540 MONTHCAL_MonthLength(infoPtr->curSel.wMonth-1, infoPtr->curSel.wYear) -day : day;
1543 if(PtInRect(&infoPtr->weeknums, lpht->pt)) {
1544 retval = MCHT_CALENDARWEEKNUM;
1545 lpht->st.wYear = infoPtr->curSel.wYear;
1546 lpht->st.wMonth = (day < 1) ? infoPtr->curSel.wMonth -1 :
1547 (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear)) ?
1548 infoPtr->curSel.wMonth +1 :infoPtr->curSel.wMonth;
1549 lpht->st.wDay = (day < 1 ) ?
1550 MONTHCAL_MonthLength(infoPtr->curSel.wMonth-1,infoPtr->curSel.wYear) -day :
1551 (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear)) ?
1552 day - MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear) : day;
1555 if(PtInRect(&infoPtr->days, lpht->pt))
1557 lpht->st.wYear = infoPtr->curSel.wYear;
1558 lpht->st.wMonth = infoPtr->curSel.wMonth;
1561 retval = MCHT_CALENDARDATEPREV;
1562 MONTHCAL_GetPrevMonth(&lpht->st);
1563 lpht->st.wDay = MONTHCAL_MonthLength(lpht->st.wMonth, lpht->st.wYear) + day;
1565 else if (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear))
1567 retval = MCHT_CALENDARDATENEXT;
1568 MONTHCAL_GetNextMonth(&lpht->st);
1569 lpht->st.wDay = day - MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear);
1572 retval = MCHT_CALENDARDATE;
1573 lpht->st.wDay = day;
1575 /* always update day of week */
1576 lpht->st.wDayOfWeek = MONTHCAL_CalculateDayOfWeek(day, lpht->st.wMonth,
1580 if(PtInRect(&infoPtr->todayrect, lpht->pt)) {
1581 retval = MCHT_TODAYLINK;
1586 /* Hit nothing special? What's left must be background :-) */
1588 retval = MCHT_CALENDARBK;
1590 lpht->uHit = retval;
1594 /* MCN_GETDAYSTATE notification helper */
1595 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1597 if(infoPtr->dwStyle & MCS_DAYSTATE) {
1601 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1602 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1603 nmds.nmhdr.code = MCN_GETDAYSTATE;
1604 nmds.cDayState = infoPtr->monthRange;
1605 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1607 nmds.stStart = infoPtr->todaysDate;
1608 nmds.stStart.wYear = infoPtr->curSel.wYear;
1609 nmds.stStart.wMonth = infoPtr->curSel.wMonth;
1610 nmds.stStart.wDay = 1;
1612 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1613 for(i = 0; i < infoPtr->monthRange; i++)
1614 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1616 Free(nmds.prgDayState);
1620 static void MONTHCAL_GoToNextMonth(MONTHCAL_INFO *infoPtr)
1622 SYSTEMTIME next = infoPtr->curSel;
1626 MONTHCAL_GetNextMonth(&next);
1628 if(!MONTHCAL_IsDateInValidRange(infoPtr, &next)) return;
1630 infoPtr->curSel = next;
1632 MONTHCAL_NotifyDayState(infoPtr);
1636 static void MONTHCAL_GoToPrevMonth(MONTHCAL_INFO *infoPtr)
1638 SYSTEMTIME prev = infoPtr->curSel;
1642 MONTHCAL_GetPrevMonth(&prev);
1644 if(!MONTHCAL_IsDateInValidRange(infoPtr, &prev)) return;
1646 infoPtr->curSel = prev;
1648 MONTHCAL_NotifyDayState(infoPtr);
1652 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1654 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1659 hMenu = CreatePopupMenu();
1660 if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1662 WARN("Can't load resource\n");
1663 strcpyW(buf, todayW);
1665 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1666 menupoint.x = (short)LOWORD(lParam);
1667 menupoint.y = (short)HIWORD(lParam);
1668 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1669 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
1670 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1672 infoPtr->curSel = infoPtr->todaysDate;
1673 infoPtr->minSel = infoPtr->todaysDate;
1674 infoPtr->maxSel = infoPtr->todaysDate;
1675 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1681 /* creates updown control and edit box */
1682 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr)
1684 infoPtr->hWndYearEdit =
1685 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
1686 infoPtr->titleyear.left + 3, infoPtr->titlebtnnext.top,
1687 infoPtr->titleyear.right - infoPtr->titleyear.left + 4,
1688 infoPtr->textHeight, infoPtr->hwndSelf,
1691 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
1693 infoPtr->hWndYearUpDown =
1694 CreateWindowExW(0, UPDOWN_CLASSW, 0,
1695 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
1696 infoPtr->titleyear.right + 7, infoPtr->titlebtnnext.top,
1697 18, infoPtr->textHeight, infoPtr->hwndSelf,
1700 /* attach edit box */
1701 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
1702 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
1703 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
1704 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->curSel.wYear);
1708 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1713 if (infoPtr->hWndYearUpDown)
1715 infoPtr->curSel.wYear = SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, 0);
1716 if(!DestroyWindow(infoPtr->hWndYearUpDown))
1718 FIXME("Can't destroy Updown Control\n");
1721 infoPtr->hWndYearUpDown = 0;
1723 if(!DestroyWindow(infoPtr->hWndYearEdit))
1725 FIXME("Can't destroy Updown Control\n");
1728 infoPtr->hWndYearEdit = 0;
1730 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1733 SetCapture(infoPtr->hwndSelf);
1735 ht.cbSize = sizeof(MCHITTESTINFO);
1736 ht.pt.x = (short)LOWORD(lParam);
1737 ht.pt.y = (short)HIWORD(lParam);
1739 hit = MONTHCAL_HitTest(infoPtr, &ht);
1741 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
1745 case MCHT_TITLEBTNNEXT:
1746 MONTHCAL_GoToNextMonth(infoPtr);
1747 infoPtr->status = MC_NEXTPRESSED;
1748 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1749 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1752 case MCHT_TITLEBTNPREV:
1753 MONTHCAL_GoToPrevMonth(infoPtr);
1754 infoPtr->status = MC_PREVPRESSED;
1755 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1756 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1759 case MCHT_TITLEMONTH:
1761 HMENU hMenu = CreatePopupMenu();
1766 for (i = 0; i < 12; i++)
1768 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
1769 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
1771 menupoint.x = ht.pt.x;
1772 menupoint.y = ht.pt.y;
1773 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1774 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
1775 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
1777 if ((i > 0) && (i < 13) && infoPtr->curSel.wMonth != i)
1779 infoPtr->curSel.wMonth = i;
1780 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1784 case MCHT_TITLEYEAR:
1786 MONTHCAL_EditYear(infoPtr);
1789 case MCHT_TODAYLINK:
1791 infoPtr->curSel = infoPtr->todaysDate;
1792 infoPtr->minSel = infoPtr->todaysDate;
1793 infoPtr->maxSel = infoPtr->todaysDate;
1794 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1796 MONTHCAL_NotifySelectionChange(infoPtr);
1797 MONTHCAL_NotifySelect(infoPtr);
1800 case MCHT_CALENDARDATENEXT:
1801 case MCHT_CALENDARDATEPREV:
1802 case MCHT_CALENDARDATE:
1804 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
1806 if(infoPtr->dwStyle & MCS_MULTISELECT)
1810 st[0] = st[1] = ht.st;
1812 /* clear selection range */
1813 MONTHCAL_SetSelRange(infoPtr, st);
1816 infoPtr->status = MC_SEL_LBUTDOWN;
1817 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
1827 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1835 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
1838 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
1839 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
1840 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
1842 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
1847 /* always send NM_RELEASEDCAPTURE notification */
1848 nmhdr.hwndFrom = infoPtr->hwndSelf;
1849 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1850 nmhdr.code = NM_RELEASEDCAPTURE;
1851 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
1853 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
1855 ht.cbSize = sizeof(MCHITTESTINFO);
1856 ht.pt.x = (short)LOWORD(lParam);
1857 ht.pt.y = (short)HIWORD(lParam);
1858 hit = MONTHCAL_HitTest(infoPtr, &ht);
1860 infoPtr->status = MC_SEL_LBUTUP;
1861 MONTHCAL_SetDayFocus(infoPtr, NULL);
1863 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
1865 SYSTEMTIME sel = infoPtr->curSel;
1867 if(!(infoPtr->dwStyle & MCS_MULTISELECT))
1871 st[0] = st[1] = ht.st;
1872 MONTHCAL_SetSelRange(infoPtr, st);
1873 /* will be invalidated here */
1874 MONTHCAL_SetCurSel(infoPtr, &st[0]);
1877 /* send MCN_SELCHANGE only if new date selected */
1878 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
1879 MONTHCAL_NotifySelectionChange(infoPtr);
1881 MONTHCAL_NotifySelect(infoPtr);
1889 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
1894 case MC_PREVNEXTMONTHTIMER:
1895 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToNextMonth(infoPtr);
1896 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToPrevMonth(infoPtr);
1897 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1899 case MC_TODAYUPDATETIMER:
1903 if(infoPtr->todaySet) return 0;
1906 MONTHCAL_UpdateToday(infoPtr, &st);
1908 /* notification sent anyway */
1909 MONTHCAL_NotifySelectionChange(infoPtr);
1914 ERR("got unknown timer %ld\n", id);
1923 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1926 SYSTEMTIME old_focused, st_ht;
1930 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
1932 ht.cbSize = sizeof(MCHITTESTINFO);
1933 ht.pt.x = (short)LOWORD(lParam);
1934 ht.pt.y = (short)HIWORD(lParam);
1936 hit = MONTHCAL_HitTest(infoPtr, &ht);
1938 /* not on the calendar date numbers? bail out */
1939 TRACE("hit:%x\n",hit);
1940 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
1942 MONTHCAL_SetDayFocus(infoPtr, NULL);
1947 old_focused = infoPtr->focusedSel;
1949 /* if pointer is over focused day still there's nothing to do */
1950 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
1952 MONTHCAL_CalcPosFromDay(infoPtr, ht.st.wDay, ht.st.wMonth, &r);
1954 if(infoPtr->dwStyle & MCS_MULTISELECT) {
1957 MONTHCAL_GetSelRange(infoPtr, st);
1959 /* If we're still at the first selected date and range is empty, return.
1960 If range isn't empty we should change range to a single firstSel */
1961 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
1962 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
1964 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
1966 st[0] = infoPtr->firstSel;
1967 /* we should overwrite timestamp here */
1968 MONTHCAL_CopyDate(&st_ht, &st[1]);
1970 /* bounds will be swapped here if needed */
1971 MONTHCAL_SetSelRange(infoPtr, st);
1978 /* FIXME: this should specify a rectangle containing only the days that changed
1979 using InvalidateRect */
1980 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1987 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
1994 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
1998 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2000 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2001 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2007 MONTHCAL_KillFocus(const MONTHCAL_INFO *infoPtr, HWND hFocusWnd)
2011 if (infoPtr->hwndNotify != hFocusWnd)
2012 ShowWindow(infoPtr->hwndSelf, SW_HIDE);
2014 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2021 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2025 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2030 /* sets the size information */
2031 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2033 static const WCHAR O0W[] = { '0','0',0 };
2034 HDC hdc = GetDC(infoPtr->hwndSelf);
2035 RECT *title=&infoPtr->title;
2036 RECT *prev=&infoPtr->titlebtnprev;
2037 RECT *next=&infoPtr->titlebtnnext;
2038 RECT *titlemonth=&infoPtr->titlemonth;
2039 RECT *titleyear=&infoPtr->titleyear;
2040 RECT *wdays=&infoPtr->wdays;
2041 RECT *weeknumrect=&infoPtr->weeknums;
2042 RECT *days=&infoPtr->days;
2043 RECT *todayrect=&infoPtr->todayrect;
2047 INT xdiv, dx, dy, i;
2051 GetClientRect(infoPtr->hwndSelf, &rcClient);
2053 currentFont = SelectObject(hdc, infoPtr->hFont);
2055 /* get the height and width of each day's text */
2056 GetTextMetricsW(hdc, &tm);
2057 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2059 /* find largest abbreviated day name for current locale */
2060 size.cx = sz.cx = 0;
2061 for (i = 0; i < 7; i++)
2063 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1,
2064 buff, countof(buff)))
2066 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2067 if (sz.cx > size.cx) size.cx = sz.cx;
2069 else /* locale independent fallback on failure */
2071 static const WCHAR SunW[] = { 'S','u','n',0 };
2073 GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2078 infoPtr->textWidth = size.cx + 2;
2080 /* recalculate the height and width increments and offsets */
2081 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2083 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2085 infoPtr->width_increment = size.cx * 2 + 4;
2086 infoPtr->height_increment = infoPtr->textHeight;
2088 /* calculate title area */
2090 title->bottom = 3 * infoPtr->height_increment / 2;
2092 title->right = infoPtr->width_increment * xdiv;
2094 /* set the dimensions of the next and previous buttons and center */
2095 /* the month text vertically */
2096 prev->top = next->top = title->top + 4;
2097 prev->bottom = next->bottom = title->bottom - 4;
2098 prev->left = title->left + 4;
2099 prev->right = prev->left + (title->bottom - title->top);
2100 next->right = title->right - 4;
2101 next->left = next->right - (title->bottom - title->top);
2103 /* titlemonth->left and right change based upon the current month */
2104 /* and are recalculated in refresh as the current month may change */
2105 /* without the control being resized */
2106 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2107 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2109 /* setup the dimensions of the rectangle we draw the names of the */
2110 /* days of the week in */
2111 weeknumrect->left = 0;
2113 if(infoPtr->dwStyle & MCS_WEEKNUMBERS)
2114 weeknumrect->right = prev->right;
2116 weeknumrect->right = weeknumrect->left;
2118 wdays->left = days->left = weeknumrect->right;
2119 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2120 wdays->top = title->bottom;
2121 wdays->bottom = wdays->top + infoPtr->height_increment;
2123 days->top = weeknumrect->top = wdays->bottom;
2124 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2126 todayrect->left = 0;
2127 todayrect->right = title->right;
2128 todayrect->top = days->bottom;
2129 todayrect->bottom = days->bottom + infoPtr->height_increment;
2131 /* offset all rectangles to center in client area */
2132 dx = (rcClient.right - title->right) / 2;
2133 dy = (rcClient.bottom - todayrect->bottom) / 2;
2135 /* if calendar doesn't fit client area show it at left/top bounds */
2136 if (title->left + dx < 0) dx = 0;
2137 if (title->top + dy < 0) dy = 0;
2139 if (dx != 0 || dy != 0)
2141 OffsetRect(title, dx, dy);
2142 OffsetRect(prev, dx, dy);
2143 OffsetRect(next, dx, dy);
2144 OffsetRect(titlemonth, dx, dy);
2145 OffsetRect(titleyear, dx, dy);
2146 OffsetRect(wdays, dx, dy);
2147 OffsetRect(weeknumrect, dx, dy);
2148 OffsetRect(days, dx, dy);
2149 OffsetRect(todayrect, dx, dy);
2152 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2153 infoPtr->width_increment,infoPtr->height_increment,
2154 wine_dbgstr_rect(&rcClient),
2155 wine_dbgstr_rect(title),
2156 wine_dbgstr_rect(wdays),
2157 wine_dbgstr_rect(days),
2158 wine_dbgstr_rect(todayrect));
2160 /* restore the originally selected font */
2161 SelectObject(hdc, currentFont);
2163 ReleaseDC(infoPtr->hwndSelf, hdc);
2166 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2168 TRACE("(width=%d, height=%d)\n", Width, Height);
2170 MONTHCAL_UpdateSize(infoPtr);
2172 /* invalidate client area and erase background */
2173 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2178 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2180 return (LRESULT)infoPtr->hFont;
2183 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2188 if (!hFont) return 0;
2190 hOldFont = infoPtr->hFont;
2191 infoPtr->hFont = hFont;
2193 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2194 lf.lfWeight = FW_BOLD;
2195 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2197 MONTHCAL_UpdateSize(infoPtr);
2200 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2202 return (LRESULT)hOldFont;
2205 /* update theme after a WM_THEMECHANGED message */
2206 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2208 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2209 CloseThemeData (theme);
2210 OpenThemeData (infoPtr->hwndSelf, themeClass);
2214 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2215 const STYLESTRUCT *lpss)
2217 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2218 wStyleType, lpss->styleOld, lpss->styleNew);
2220 if (wStyleType != GWL_STYLE) return 0;
2222 infoPtr->dwStyle = lpss->styleNew;
2224 /* make room for week numbers */
2225 if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2226 MONTHCAL_UpdateSize(infoPtr);
2231 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2234 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2235 wStyleType, lpss->styleOld, lpss->styleNew);
2237 /* block MCS_MULTISELECT change */
2238 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2240 if (lpss->styleOld & MCS_MULTISELECT)
2241 lpss->styleNew |= MCS_MULTISELECT;
2243 lpss->styleNew &= ~MCS_MULTISELECT;
2249 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2251 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2253 MONTHCAL_INFO *infoPtr;
2255 /* allocate memory for info structure */
2256 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2257 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2259 if(infoPtr == NULL) {
2260 ERR( "could not allocate info memory!\n");
2264 infoPtr->hwndSelf = hwnd;
2265 infoPtr->hwndNotify = lpcs->hwndParent;
2266 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2268 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
2270 /* initialize info structure */
2271 /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
2273 GetLocalTime(&infoPtr->todaysDate);
2274 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2276 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2277 infoPtr->monthRange = 3;
2278 infoPtr->monthdayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
2279 infoPtr->titlebk = comctl32_color.clrActiveCaption;
2280 infoPtr->titletxt = comctl32_color.clrWindow;
2281 infoPtr->monthbk = comctl32_color.clrWindow;
2282 infoPtr->trailingtxt = comctl32_color.clrGrayText;
2283 infoPtr->bk = comctl32_color.clrWindow;
2284 infoPtr->txt = comctl32_color.clrWindowText;
2286 infoPtr->minSel = infoPtr->todaysDate;
2287 infoPtr->maxSel = infoPtr->todaysDate;
2288 infoPtr->curSel = infoPtr->todaysDate;
2290 /* call MONTHCAL_UpdateSize to set all of the dimensions */
2291 /* of the control */
2292 MONTHCAL_UpdateSize(infoPtr);
2294 /* today auto update timer, to be freed only on control destruction */
2295 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2297 OpenThemeData (infoPtr->hwndSelf, themeClass);
2304 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2306 /* free month calendar info data */
2307 Free(infoPtr->monthdayState);
2308 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2310 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2317 static LRESULT WINAPI
2318 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2320 MONTHCAL_INFO *infoPtr;
2322 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2324 infoPtr = MONTHCAL_GetInfoPtr(hwnd);
2325 if (!infoPtr && (uMsg != WM_CREATE))
2326 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2330 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2333 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2335 case MCM_GETMAXSELCOUNT:
2336 return MONTHCAL_GetMaxSelCount(infoPtr);
2338 case MCM_SETMAXSELCOUNT:
2339 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2341 case MCM_GETSELRANGE:
2342 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2344 case MCM_SETSELRANGE:
2345 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2347 case MCM_GETMONTHRANGE:
2348 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2350 case MCM_SETDAYSTATE:
2351 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2353 case MCM_GETMINREQRECT:
2354 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2357 return MONTHCAL_GetColor(infoPtr, wParam);
2360 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2363 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2366 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2369 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2371 case MCM_GETFIRSTDAYOFWEEK:
2372 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2374 case MCM_SETFIRSTDAYOFWEEK:
2375 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2378 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2381 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2383 case MCM_GETMONTHDELTA:
2384 return MONTHCAL_GetMonthDelta(infoPtr);
2386 case MCM_SETMONTHDELTA:
2387 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2389 case MCM_GETMAXTODAYWIDTH:
2390 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2393 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2396 return MONTHCAL_KillFocus(infoPtr, (HWND)wParam);
2399 return MONTHCAL_RButtonUp(infoPtr, lParam);
2401 case WM_LBUTTONDOWN:
2402 return MONTHCAL_LButtonDown(infoPtr, lParam);
2405 return MONTHCAL_MouseMove(infoPtr, lParam);
2408 return MONTHCAL_LButtonUp(infoPtr, lParam);
2410 case WM_PRINTCLIENT:
2412 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2415 return MONTHCAL_SetFocus(infoPtr);
2418 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2421 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2424 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2427 return MONTHCAL_GetFont(infoPtr);
2430 return MONTHCAL_Timer(infoPtr, wParam);
2432 case WM_THEMECHANGED:
2433 return theme_changed (infoPtr);
2436 return MONTHCAL_Destroy(infoPtr);
2438 case WM_SYSCOLORCHANGE:
2439 COMCTL32_RefreshSysColors();
2442 case WM_STYLECHANGED:
2443 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2445 case WM_STYLECHANGING:
2446 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2449 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2450 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2451 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2457 MONTHCAL_Register(void)
2461 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2462 wndClass.style = CS_GLOBALCLASS;
2463 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2464 wndClass.cbClsExtra = 0;
2465 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2466 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2467 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2468 wndClass.lpszClassName = MONTHCAL_CLASSW;
2470 RegisterClassW(&wndClass);
2475 MONTHCAL_Unregister(void)
2477 UnregisterClassW(MONTHCAL_CLASSW, NULL);