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 int firstDayHighWord; /* High word only used externally */
103 MONTHDAYSTATE *monthdayState;
104 SYSTEMTIME todaysDate;
105 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
106 int status; /* See MC_SEL flags */
107 SYSTEMTIME firstSel; /* first selected day */
111 SYSTEMTIME curSel; /* contains currently selected year, month and day */
112 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
117 RECT title; /* rect for the header above the calendar */
118 RECT titlebtnnext; /* the `next month' button in the header */
119 RECT titlebtnprev; /* the `prev month' button in the header */
120 RECT titlemonth; /* the `month name' txt in the header */
121 RECT titleyear; /* the `year number' txt in the header */
122 RECT wdays; /* week days at top */
123 RECT days; /* calendar area */
124 RECT weeknums; /* week numbers at left side */
125 RECT todayrect; /* `today: xx/xx/xx' text rect */
126 HWND hwndNotify; /* Window to receive the notifications */
127 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
128 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
129 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
132 /* Offsets of days in the week to the weekday of january 1 in a leap year */
133 static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
135 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
137 /* empty SYSTEMTIME const */
138 static const SYSTEMTIME st_null;
139 /* valid date limits */
140 static const SYSTEMTIME max_allowed_date = { .wYear = 9999, .wMonth = 12, .wDay = 31 };
141 static const SYSTEMTIME min_allowed_date = { .wYear = 1752, .wMonth = 9, .wDay = 14 };
144 #define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0))
146 /* helper functions */
148 /* send a single MCN_SELCHANGE notification */
149 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
153 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
154 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
155 nmsc.nmhdr.code = MCN_SELCHANGE;
156 nmsc.stSelStart = infoPtr->minSel;
157 nmsc.stSelEnd = infoPtr->maxSel;
158 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
161 /* send a single MCN_SELECT notification */
162 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
166 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
167 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
168 nmsc.nmhdr.code = MCN_SELECT;
169 nmsc.stSelStart = infoPtr->minSel;
170 nmsc.stSelEnd = infoPtr->maxSel;
172 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
175 /* returns the number of days in any given month, checking for leap days */
176 /* january is 1, december is 12 */
177 int MONTHCAL_MonthLength(int month, int year)
179 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0};
180 /*Wrap around, this eases handling*/
186 /* if we have a leap year add 1 day to February */
187 /* a leap year is a year either divisible by 400 */
188 /* or divisible by 4 and not by 100 */
189 if(month == 2) { /* February */
190 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
191 (year%4 == 0)) ? 1 : 0);
194 return mdays[month - 1];
198 /* compares timestamps using date part only */
199 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
201 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
202 (first->wDay == second->wDay);
205 /* make sure that date fields are valid */
206 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
208 if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
209 if(time->wDayOfWeek > 6) return FALSE;
210 if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear))
216 /* Compares two dates in SYSTEMTIME format
220 * [I] first : pointer to valid first date data to compare
221 * [I] second : pointer to valid second date data to compare
225 * -1 : first < second
226 * 0 : first == second
229 * Note that no date validation performed, alreadt validated values expected.
231 static LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
233 FILETIME ft_first, ft_second;
235 SystemTimeToFileTime(first, &ft_first);
236 SystemTimeToFileTime(second, &ft_second);
238 return CompareFileTime(&ft_first, &ft_second);
241 /* Checks largest possible date range and configured one
245 * [I] infoPtr : valid pointer to control data
246 * [I] date : pointer to valid date data to check
250 * TRUE - date whithin largest and configured range
251 * FALSE - date is outside largest or configured range
253 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date)
255 if((MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) ||
256 (MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1)) return FALSE;
258 if(infoPtr->rangeValid & GDTR_MAX) {
259 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) return FALSE;
262 if(infoPtr->rangeValid & GDTR_MIN) {
263 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) return FALSE;
269 /* Checks passed range width with configured maximum selection count
273 * [I] infoPtr : valid pointer to control data
274 * [I] range0 : pointer to valid date data (requested bound)
275 * [I] range1 : pointer to valid date data (primary bound)
276 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
278 * Adjust value computed basing on primary bound and current maximum selection
279 * count. For simple range check (without adjusted value required) (range0, range1)
280 * relation means nothing.
284 * TRUE - range is shorter or equal to maximum
285 * FALSE - range is larger than maximum
287 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
288 const SYSTEMTIME *range0,
289 const SYSTEMTIME *range1,
292 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
293 FILETIME ft_range0, ft_range1;
296 SystemTimeToFileTime(range0, &ft_range0);
297 SystemTimeToFileTime(range1, &ft_range1);
299 ul_range0.LowPart = ft_range0.dwLowDateTime;
300 ul_range0.HighPart = ft_range0.dwHighDateTime;
301 ul_range1.LowPart = ft_range1.dwLowDateTime;
302 ul_range1.HighPart = ft_range1.dwHighDateTime;
304 cmp = CompareFileTime(&ft_range0, &ft_range1);
307 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
309 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
311 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
315 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
317 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
319 ft_range0.dwLowDateTime = ul_range0.LowPart;
320 ft_range0.dwHighDateTime = ul_range0.HighPart;
321 FileTimeToSystemTime(&ft_range0, adjust);
329 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
330 Milliseconds are intentionaly not validated. */
331 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
333 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
339 /* Copies timestamp part only.
343 * [I] from : source date
346 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
348 to->wHour = from->wHour;
349 to->wMinute = from->wMinute;
350 to->wSecond = from->wSecond;
353 /* Copies date part only.
357 * [I] from : source date
360 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
362 to->wYear = from->wYear;
363 to->wMonth = from->wMonth;
364 to->wDay = from->wDay;
365 to->wDayOfWeek = from->wDayOfWeek;
368 /* Note:Depending on DST, this may be offset by a day.
369 Need to find out if we're on a DST place & adjust the clock accordingly.
370 Above function assumes we have a valid data.
371 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
375 /* returns the day in the week(0 == sunday, 6 == saturday) */
376 /* day(1 == 1st, 2 == 2nd... etc), year is the year value */
377 int MONTHCAL_CalculateDayOfWeek(DWORD day, DWORD month, DWORD year)
381 return((year + year/4 - year/100 + year/400 +
382 DayOfWeekTable[month-1] + day ) % 7);
385 /* From a given point, calculate the row (weekpos), column(daypos)
386 and day in the calendar. day== 0 mean the last day of tha last month
388 static int MONTHCAL_CalcDayFromPos(const MONTHCAL_INFO *infoPtr, int x, int y,
389 int *daypos,int *weekpos)
391 int retval, firstDay;
394 GetClientRect(infoPtr->hwndSelf, &rcClient);
396 /* if the point is outside the x bounds of the window put
397 it at the boundary */
398 if (x > rcClient.right)
402 *daypos = (x - infoPtr->days.left ) / infoPtr->width_increment;
403 *weekpos = (y - infoPtr->days.top ) / infoPtr->height_increment;
405 firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear)+6 - infoPtr->firstDay)%7;
406 retval = *daypos + (7 * *weekpos) - firstDay;
410 /* day is the day of the month, 1 == 1st day of the month */
411 /* sets x and y to be the position of the day */
412 /* x == day, y == week where(0,0) == firstDay, 1st week */
413 static void MONTHCAL_CalcDayXY(const MONTHCAL_INFO *infoPtr, int day, int month,
416 int firstDay, prevMonth;
418 firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear) +6 - infoPtr->firstDay)%7;
420 if(month==infoPtr->curSel.wMonth) {
421 *x = (day + firstDay) % 7;
422 *y = (day + firstDay - *x) / 7;
425 if(month < infoPtr->curSel.wMonth) {
426 prevMonth = month - 1;
430 *x = (MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear) - firstDay) % 7;
435 *y = MONTHCAL_MonthLength(month, infoPtr->curSel.wYear - 1) / 7;
436 *x = (day + firstDay + MONTHCAL_MonthLength(month,
437 infoPtr->curSel.wYear)) % 7;
441 /* x: column(day), y: row(week) */
442 static void MONTHCAL_CalcDayRect(const MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
444 r->left = infoPtr->days.left + x * infoPtr->width_increment;
445 r->right = r->left + infoPtr->width_increment;
446 r->top = infoPtr->days.top + y * infoPtr->height_increment;
447 r->bottom = r->top + infoPtr->textHeight;
451 /* sets the RECT struct r to the rectangle around the day and month */
452 /* day is the day value of the month(1 == 1st), month is the month */
453 /* value(january == 1, december == 12) */
454 static inline void MONTHCAL_CalcPosFromDay(const MONTHCAL_INFO *infoPtr,
455 int day, int month, RECT *r)
459 MONTHCAL_CalcDayXY(infoPtr, day, month, &x, &y);
460 MONTHCAL_CalcDayRect(infoPtr, r, x, y);
463 /* Focused day helper:
465 - set focused date to given value;
466 - reset to zero value if NULL passed;
467 - invalidate previous and new day rectangle only if needed.
469 Returns TRUE if focused day changed, FALSE otherwise.
471 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
477 /* there's nothing to do if it's the same date,
478 mouse move within same date rectangle case */
479 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
481 /* invalidate old focused day */
482 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->focusedSel.wDay,
483 infoPtr->focusedSel.wMonth, &r);
484 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
486 infoPtr->focusedSel = *st;
489 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->focusedSel.wDay,
490 infoPtr->focusedSel.wMonth, &r);
492 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
493 infoPtr->focusedSel = st_null;
495 /* on set invalidates new day, on reset clears previous focused day */
496 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
501 /* day is the day in the month(1 == 1st of the month) */
502 /* month is the month value(1 == january, 12 == december) */
503 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month)
505 HPEN hRedPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
506 HPEN hOldPen2 = SelectObject(hdc, hRedPen);
510 MONTHCAL_CalcPosFromDay(infoPtr, day, month, &day_rect);
512 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
513 Rectangle(hdc, day_rect.left, day_rect.top, day_rect.right, day_rect.bottom);
515 SelectObject(hdc, hOldBrush);
516 DeleteObject(hRedPen);
517 SelectObject(hdc, hOldPen2);
520 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month,
521 int x, int y, int bold)
523 static const WCHAR fmtW[] = { '%','d',0 };
526 static BOOL haveBoldFont, haveSelectedDay = FALSE;
531 wsprintfW(buf, fmtW, day);
533 /* No need to check styles: when selection is not valid, it is set to zero.
534 * 1<day<31, so everything is OK.
537 MONTHCAL_CalcDayRect(infoPtr, &r, x, y);
539 if((day>=infoPtr->minSel.wDay) && (day<=infoPtr->maxSel.wDay)
540 && (month == infoPtr->curSel.wMonth)) {
543 TRACE("%d %d %d\n",day, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
544 TRACE("%s\n", wine_dbgstr_rect(&r));
545 oldCol = SetTextColor(hdc, infoPtr->monthbk);
546 oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
547 hbr = GetSysColorBrush(COLOR_HIGHLIGHT);
548 FillRect(hdc, &r, hbr);
550 /* FIXME: this may need to be changed now b/c of the other
551 drawing changes 11/3/99 CMM */
552 r2.left = r.left - 0.25 * infoPtr->textWidth;
554 r2.right = r.left + 0.5 * infoPtr->textWidth;
555 r2.bottom = r.bottom;
556 if(haveSelectedDay) FillRect(hdc, &r2, hbr);
557 haveSelectedDay = TRUE;
559 haveSelectedDay = FALSE;
562 /* need to add some code for multiple selections */
564 if((bold) &&(!haveBoldFont)) {
565 SelectObject(hdc, infoPtr->hBoldFont);
568 if((!bold) &&(haveBoldFont)) {
569 SelectObject(hdc, infoPtr->hFont);
570 haveBoldFont = FALSE;
573 SetBkMode(hdc,TRANSPARENT);
574 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
576 if(haveSelectedDay) {
577 SetTextColor(hdc, oldCol);
578 SetBkColor(hdc, oldBk);
583 static void paint_button (const MONTHCAL_INFO *infoPtr, HDC hdc, BOOL btnNext,
584 BOOL pressed, RECT* r)
586 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
590 static const int states[] = {
592 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
594 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
596 int stateNum = btnNext ? 3 : 0;
601 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
603 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
607 int style = btnNext ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
609 style |= DFCS_PUSHED;
612 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
615 DrawFrameControl(hdc, r, DFC_SCROLL, style);
620 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
622 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
623 static const WCHAR fmt1W[] = { '%','s',' ','%','l','d',0 };
624 static const WCHAR fmt2W[] = { '%','s',' ','%','s',0 };
625 static const WCHAR fmt3W[] = { '%','d',0 };
626 RECT *title=&infoPtr->title;
627 RECT *prev=&infoPtr->titlebtnprev;
628 RECT *next=&infoPtr->titlebtnnext;
629 RECT *titlemonth=&infoPtr->titlemonth;
630 RECT *titleyear=&infoPtr->titleyear;
634 int i, j, m, mask, day, firstDay, weeknum, weeknum1,prevMonth;
635 int textHeight = infoPtr->textHeight;
642 COLORREF oldTextColor, oldBkColor;
644 RECT rcDay; /* used in MONTHCAL_CalcDayRect() */
645 SYSTEMTIME localtime;
648 oldTextColor = SetTextColor(hdc, comctl32_color.clrWindowText);
650 /* fill background */
651 hbr = CreateSolidBrush (infoPtr->bk);
652 FillRect(hdc, &ps->rcPaint, hbr);
656 if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
658 hbr = CreateSolidBrush(infoPtr->titlebk);
659 FillRect(hdc, title, hbr);
663 /* if the previous button is pressed draw it depressed */
664 if(IntersectRect(&rcTemp, &(ps->rcPaint), prev))
665 paint_button (infoPtr, hdc, FALSE, infoPtr->status & MC_PREVPRESSED, prev);
667 /* if next button is depressed draw it depressed */
668 if(IntersectRect(&rcTemp, &(ps->rcPaint), next))
669 paint_button (infoPtr, hdc, TRUE, infoPtr->status & MC_NEXTPRESSED, next);
671 oldBkColor = SetBkColor(hdc, infoPtr->titlebk);
672 SetTextColor(hdc, infoPtr->titletxt);
673 currentFont = SelectObject(hdc, infoPtr->hBoldFont);
675 GetLocaleInfoW( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+infoPtr->curSel.wMonth -1,
677 wsprintfW(buf, fmt1W, buf1, infoPtr->curSel.wYear);
679 if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
681 DrawTextW(hdc, buf, strlenW(buf), title,
682 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
685 /* titlemonth left/right contained rect for whole titletxt('June 1999')
686 * MCM_HitTestInfo wants month & year rects, so prepare these now.
687 *(no, we can't draw them separately; the whole text is centered)
689 GetTextExtentPoint32W(hdc, buf, strlenW(buf), &size);
690 titlemonth->left = title->right / 2 + title->left / 2 - size.cx / 2;
691 titleyear->right = title->right / 2 + title->left / 2 + size.cx / 2;
692 GetTextExtentPoint32W(hdc, buf1, strlenW(buf1), &size);
693 titlemonth->right = titlemonth->left + size.cx;
694 titleyear->left = titlemonth->right;
696 /* draw month area */
697 rcTemp.top=infoPtr->wdays.top;
698 rcTemp.left=infoPtr->wdays.left;
699 rcTemp.bottom=infoPtr->todayrect.bottom;
700 rcTemp.right =infoPtr->todayrect.right;
701 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcTemp))
703 hbr = CreateSolidBrush(infoPtr->monthbk);
704 FillRect(hdc, &rcTemp, hbr);
708 /* draw line under day abbreviations */
710 MoveToEx(hdc, infoPtr->days.left + 3, title->bottom + textHeight + 1, NULL);
711 LineTo(hdc, infoPtr->days.right - 3, title->bottom + textHeight + 1);
713 prevMonth = infoPtr->curSel.wMonth - 1;
714 if(prevMonth == 0) /* if curSel.wMonth is january(1) prevMonth is */
715 prevMonth = 12; /* december(12) of the previous year */
717 infoPtr->wdays.left = infoPtr->days.left = infoPtr->weeknums.right;
718 /* draw day abbreviations */
720 SelectObject(hdc, infoPtr->hFont);
721 SetBkColor(hdc, infoPtr->monthbk);
722 SetTextColor(hdc, infoPtr->trailingtxt);
724 /* copy this rect so we can change the values without changing */
725 /* the original version */
726 days->left = infoPtr->wdays.left;
727 days->right = days->left + infoPtr->width_increment;
728 days->top = infoPtr->wdays.top;
729 days->bottom = infoPtr->wdays.bottom;
731 i = infoPtr->firstDay;
734 GetLocaleInfoW( LOCALE_USER_DEFAULT,LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
735 DrawTextW(hdc, buf, strlenW(buf), days, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
736 days->left+=infoPtr->width_increment;
737 days->right+=infoPtr->width_increment;
740 /* draw day numbers; first, the previous month */
742 firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear);
744 day = MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear) +
745 (infoPtr->firstDay + 7 - firstDay)%7 + 1;
746 if (day > MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear))
748 startofprescal = day;
753 while(day <= MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear)) {
754 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
755 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
757 MONTHCAL_DrawDay(infoPtr, hdc, day, prevMonth, i, 0,
758 infoPtr->monthdayState[m] & mask);
766 /* draw `current' month */
768 day = 1; /* start at the beginning of the current month */
770 infoPtr->firstDayplace = i;
771 SetTextColor(hdc, infoPtr->txt);
775 /* draw the first week of the current month */
777 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
778 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
780 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth, i, 0,
781 infoPtr->monthdayState[m] & mask);
789 j = 1; /* move to the 2nd week of the current month */
790 i = 0; /* move back to sunday */
791 while(day <= MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear)) {
792 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
793 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
795 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth, i, j,
796 infoPtr->monthdayState[m] & mask);
801 if(i>6) { /* past saturday, goto the next weeks sunday */
807 /* draw `next' month */
809 day = 1; /* start at the first day of the next month */
813 SetTextColor(hdc, infoPtr->trailingtxt);
814 while((i<7) &&(j<6)) {
815 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
816 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
818 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth + 1, i, j,
819 infoPtr->monthdayState[m] & mask);
825 if(i==7) { /* past saturday, go to next week's sunday */
830 SetTextColor(hdc, infoPtr->txt);
832 /* draw today mark rectangle */
833 if((infoPtr->curSel.wMonth == infoPtr->todaysDate.wMonth) &&
834 (infoPtr->curSel.wYear == infoPtr->todaysDate.wYear) &&
835 !(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
837 MONTHCAL_CircleDay(infoPtr, hdc, infoPtr->todaysDate.wDay, infoPtr->todaysDate.wMonth);
840 /* draw focused day */
841 if(!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
843 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->focusedSel.wDay,
844 infoPtr->focusedSel.wMonth, &rcDay);
846 DrawFocusRect(hdc, &rcDay);
849 /* draw `today' date if style allows it, and draw a circle before today's
850 * date if necessary */
852 if(!(infoPtr->dwStyle & MCS_NOTODAY)) {
853 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
854 /*day is the number of days from nextmonth we put on the calendar */
855 MONTHCAL_CircleDay(infoPtr, hdc,
856 day+MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear),
857 infoPtr->curSel.wMonth);
859 if (!LoadStringW(COMCTL32_hModule,IDM_TODAY,buf1,countof(buf1)))
861 WARN("Can't load resource\n");
862 strcpyW(buf1, todayW);
864 MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
865 localtime = infoPtr->todaysDate;
866 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&localtime,NULL,buf2,countof(buf2));
867 wsprintfW(buf, fmt2W, buf1, buf2);
868 SelectObject(hdc, infoPtr->hBoldFont);
870 DrawTextW(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
871 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rtoday))
873 DrawTextW(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
875 SelectObject(hdc, infoPtr->hFont);
878 /*eventually draw week numbers*/
879 if(infoPtr->dwStyle & MCS_WEEKNUMBERS) {
880 /* display weeknumbers*/
883 /* Rules what week to call the first week of a new year:
884 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
885 The week containing Jan 1 is the first week of year
886 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
887 First week of year must contain 4 days of the new year
888 LOCALE_IFIRSTWEEKOFYEAR == 1 (what contries?)
889 The first week of the year must contain only days of the new year
891 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
892 weeknum = atoiW(buf);
903 if (infoPtr->curSel.wMonth < 2)
905 /* calculate all those exceptions for january */
906 weeknum1=MONTHCAL_CalculateDayOfWeek(1, 1, infoPtr->curSel.wYear);
907 if ((infoPtr->firstDay +7 - weeknum1)%7 > mindays)
913 weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->curSel.wYear - 1);
914 weeknum +=startofprescal+ 7;
916 weeknum1=MONTHCAL_CalculateDayOfWeek(1, 1, infoPtr->curSel.wYear - 1);
917 if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
924 for(i=0; i<prevMonth-1; i++)
925 weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->curSel.wYear);
926 weeknum +=startofprescal+ 7;
928 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->curSel.wYear);
929 if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
932 days->left = infoPtr->weeknums.left;
933 days->right = infoPtr->weeknums.right;
934 days->top = infoPtr->weeknums.top;
935 days->bottom = days->top +infoPtr->height_increment;
937 if((i==0)&&(weeknum>50))
939 wsprintfW(buf, fmt3W, weeknum);
942 else if((i==5)&&(weeknum>47))
944 wsprintfW(buf, fmt3W, 1);
947 wsprintfW(buf, fmt3W, weeknum + i);
948 DrawTextW(hdc, buf, -1, days, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
949 days->top+=infoPtr->height_increment;
950 days->bottom+=infoPtr->height_increment;
953 MoveToEx(hdc, infoPtr->weeknums.right, infoPtr->weeknums.top + 3 , NULL);
954 LineTo(hdc, infoPtr->weeknums.right, infoPtr->weeknums.bottom );
957 /* currentFont was font at entering Refresh */
959 SetBkColor(hdc, oldBkColor);
960 SelectObject(hdc, currentFont);
961 SetTextColor(hdc, oldTextColor);
966 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, LPRECT lpRect)
968 TRACE("rect %p\n", lpRect);
970 if(!lpRect) return FALSE;
972 lpRect->left = infoPtr->title.left;
973 lpRect->top = infoPtr->title.top;
974 lpRect->right = infoPtr->title.right;
975 lpRect->bottom = infoPtr->todayrect.bottom;
977 AdjustWindowRect(lpRect, infoPtr->dwStyle, FALSE);
979 /* minimal rectangle is zero based */
980 OffsetRect(lpRect, -lpRect->left, -lpRect->top);
982 TRACE("%s\n", wine_dbgstr_rect(lpRect));
989 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, INT index)
994 case MCSC_BACKGROUND:
999 return infoPtr->titlebk;
1000 case MCSC_TITLETEXT:
1001 return infoPtr->titletxt;
1003 return infoPtr->monthbk;
1004 case MCSC_TRAILINGTEXT:
1005 return infoPtr->trailingtxt;
1013 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, INT index, COLORREF color)
1017 TRACE("%d: color %08x\n", index, color);
1020 case MCSC_BACKGROUND:
1022 infoPtr->bk = color;
1025 prev = infoPtr->txt;
1026 infoPtr->txt = color;
1029 prev = infoPtr->titlebk;
1030 infoPtr->titlebk = color;
1032 case MCSC_TITLETEXT:
1033 prev=infoPtr->titletxt;
1034 infoPtr->titletxt = color;
1037 prev = infoPtr->monthbk;
1038 infoPtr->monthbk = color;
1040 case MCSC_TRAILINGTEXT:
1041 prev = infoPtr->trailingtxt;
1042 infoPtr->trailingtxt = color;
1046 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND ? TRUE : FALSE);
1052 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1057 return infoPtr->delta;
1059 return infoPtr->visible;
1064 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1066 INT prev = infoPtr->delta;
1068 TRACE("delta %d\n", delta);
1070 infoPtr->delta = delta;
1076 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1078 return MAKELONG(infoPtr->firstDay, infoPtr->firstDayHighWord);
1082 /* sets the first day of the week that will appear in the control */
1083 /* 0 == Sunday, 6 == Saturday */
1084 /* FIXME: this needs to be implemented properly in MONTHCAL_Refresh() */
1085 /* FIXME: we need more error checking here */
1087 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1089 int prev = MAKELONG(infoPtr->firstDay, infoPtr->firstDayHighWord);
1091 TRACE("day %d\n", day);
1098 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1099 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1101 localFirstDay = atoiW(buf);
1103 infoPtr->firstDay = localFirstDay;
1104 infoPtr->firstDayHighWord = FALSE;
1108 infoPtr->firstDay = 6; /* max first day allowed */
1109 infoPtr->firstDayHighWord = TRUE;
1113 infoPtr->firstDay = day;
1114 infoPtr->firstDayHighWord = TRUE;
1122 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
1128 if(flag == GMR_VISIBLE)
1130 /*FIXME: currently multicalendar feature isn't implelented, so entirely
1131 visible month is current */
1132 st[0] = st[1] = infoPtr->curSel;
1135 st[0].wDayOfWeek = MONTHCAL_CalculateDayOfWeek(1, st[0].wMonth, st[0].wYear);
1137 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
1138 st[1].wDayOfWeek = MONTHCAL_CalculateDayOfWeek(st[1].wDay, st[1].wMonth,
1142 FIXME("only GMR_VISIBLE flag supported, got %d\n", flag);
1145 return infoPtr->monthRange;
1150 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1152 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1157 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1159 FILETIME ft_min, ft_max;
1161 TRACE("%x %p\n", limits, range);
1163 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1164 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1167 if (limits & GDTR_MIN)
1169 if (!MONTHCAL_ValidateTime(&range[0]))
1170 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1172 infoPtr->minDate = range[0];
1173 infoPtr->rangeValid |= GDTR_MIN;
1175 if (limits & GDTR_MAX)
1177 if (!MONTHCAL_ValidateTime(&range[1]))
1178 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1180 infoPtr->maxDate = range[1];
1181 infoPtr->rangeValid |= GDTR_MAX;
1184 /* Only one limit set - we are done */
1185 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1188 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1189 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1191 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1193 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1195 /* Native swaps limits only when both limits are being set. */
1196 SYSTEMTIME st_tmp = infoPtr->minDate;
1197 infoPtr->minDate = infoPtr->maxDate;
1198 infoPtr->maxDate = st_tmp;
1202 /* reset the other limit */
1203 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1204 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1205 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1214 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1216 TRACE("%p\n", range);
1218 if(!range) return FALSE;
1220 range[1] = infoPtr->maxDate;
1221 range[0] = infoPtr->minDate;
1223 return infoPtr->rangeValid;
1228 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1232 TRACE("%d %p\n", months, states);
1233 if(months != infoPtr->monthRange) return 0;
1235 for(i = 0; i < months; i++)
1236 infoPtr->monthdayState[i] = states[i];
1242 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1244 TRACE("%p\n", curSel);
1245 if(!curSel) return FALSE;
1246 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1248 *curSel = infoPtr->minSel;
1249 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1253 /* FIXME: if the specified date is not visible, make it visible */
1255 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1257 TRACE("%p\n", curSel);
1258 if(!curSel) return FALSE;
1259 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1261 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1262 /* exit earlier if selection equals current */
1263 if (MONTHCAL_IsDateEqual(&infoPtr->curSel, curSel)) return TRUE;
1265 if(!MONTHCAL_IsDateInValidRange(infoPtr, curSel)) return FALSE;
1267 infoPtr->minSel = *curSel;
1268 infoPtr->maxSel = *curSel;
1270 infoPtr->curSel = *curSel;
1272 /* FIXME: it's possible to reduce rectangle here */
1273 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1280 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1282 return infoPtr->maxSelCount;
1287 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1291 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1292 if(max <= 0) return FALSE;
1294 infoPtr->maxSelCount = max;
1301 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1303 TRACE("%p\n", range);
1305 if(!range) return FALSE;
1307 if(infoPtr->dwStyle & MCS_MULTISELECT)
1309 range[1] = infoPtr->maxSel;
1310 range[0] = infoPtr->minSel;
1311 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1320 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1322 TRACE("%p\n", range);
1324 if(!range) return FALSE;
1326 if(infoPtr->dwStyle & MCS_MULTISELECT)
1328 SYSTEMTIME old_range[2];
1330 /* adjust timestamps */
1331 if(!MONTHCAL_ValidateTime(&range[0]))
1332 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1333 if(!MONTHCAL_ValidateTime(&range[1]))
1334 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1336 /* maximum range exceeded */
1337 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1339 old_range[0] = infoPtr->minSel;
1340 old_range[1] = infoPtr->maxSel;
1342 /* swap if min > max */
1343 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1345 infoPtr->minSel = range[0];
1346 infoPtr->maxSel = range[1];
1350 infoPtr->minSel = range[1];
1351 infoPtr->maxSel = range[0];
1354 /* update day of week */
1355 infoPtr->minSel.wDayOfWeek =
1356 MONTHCAL_CalculateDayOfWeek(infoPtr->minSel.wDay, infoPtr->minSel.wMonth,
1357 infoPtr->minSel.wYear);
1358 infoPtr->maxSel.wDayOfWeek =
1359 MONTHCAL_CalculateDayOfWeek(infoPtr->maxSel.wDay, infoPtr->maxSel.wMonth,
1360 infoPtr->maxSel.wYear);
1362 /* redraw if bounds changed */
1363 /* FIXME: no actual need to redraw everything */
1364 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1365 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1367 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1370 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1379 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1381 TRACE("%p\n", today);
1383 if(!today) return FALSE;
1384 *today = infoPtr->todaysDate;
1388 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1392 * TRUE - today date changed
1393 * FALSE - today date isn't changed
1396 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1400 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1402 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->todaysDate.wDay,
1403 infoPtr->todaysDate.wMonth, &old_r);
1404 MONTHCAL_CalcPosFromDay(infoPtr, today->wDay, today->wMonth, &new_r);
1406 infoPtr->todaysDate = *today;
1408 /* only two days need redrawing */
1409 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1410 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1414 /* MCM_SETTODAT handler */
1416 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1418 TRACE("%p\n", today);
1420 if(!today) return FALSE;
1422 /* remember if date was set successfully */
1423 if(MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1429 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1435 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1440 ZeroMemory(&lpht->st, sizeof(lpht->st));
1442 /* Comment in for debugging...
1443 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,
1444 infoPtr->wdays.left, infoPtr->wdays.right,
1445 infoPtr->wdays.top, infoPtr->wdays.bottom,
1446 infoPtr->days.left, infoPtr->days.right,
1447 infoPtr->days.top, infoPtr->days.bottom,
1448 infoPtr->todayrect.left, infoPtr->todayrect.right,
1449 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1450 infoPtr->weeknums.left, infoPtr->weeknums.right,
1451 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1454 /* are we in the header? */
1456 if(PtInRect(&infoPtr->title, lpht->pt)) {
1457 if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1458 retval = MCHT_TITLEBTNPREV;
1461 if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1462 retval = MCHT_TITLEBTNNEXT;
1465 if(PtInRect(&infoPtr->titlemonth, lpht->pt)) {
1466 retval = MCHT_TITLEMONTH;
1469 if(PtInRect(&infoPtr->titleyear, lpht->pt)) {
1470 retval = MCHT_TITLEYEAR;
1474 retval = MCHT_TITLE;
1478 day = MONTHCAL_CalcDayFromPos(infoPtr,x,y,&wday,&wnum);
1479 if(PtInRect(&infoPtr->wdays, lpht->pt)) {
1480 retval = MCHT_CALENDARDAY;
1481 lpht->st.wYear = infoPtr->curSel.wYear;
1482 lpht->st.wMonth = (day < 1)? infoPtr->curSel.wMonth -1 : infoPtr->curSel.wMonth;
1483 lpht->st.wDay = (day < 1)?
1484 MONTHCAL_MonthLength(infoPtr->curSel.wMonth-1, infoPtr->curSel.wYear) -day : day;
1487 if(PtInRect(&infoPtr->weeknums, lpht->pt)) {
1488 retval = MCHT_CALENDARWEEKNUM;
1489 lpht->st.wYear = infoPtr->curSel.wYear;
1490 lpht->st.wMonth = (day < 1) ? infoPtr->curSel.wMonth -1 :
1491 (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear)) ?
1492 infoPtr->curSel.wMonth +1 :infoPtr->curSel.wMonth;
1493 lpht->st.wDay = (day < 1 ) ?
1494 MONTHCAL_MonthLength(infoPtr->curSel.wMonth-1,infoPtr->curSel.wYear) -day :
1495 (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear)) ?
1496 day - MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear) : day;
1499 if(PtInRect(&infoPtr->days, lpht->pt))
1501 lpht->st.wYear = infoPtr->curSel.wYear;
1504 retval = MCHT_CALENDARDATEPREV;
1505 lpht->st.wMonth = infoPtr->curSel.wMonth - 1;
1506 if (lpht->st.wMonth < 1)
1508 lpht->st.wMonth = 12;
1511 lpht->st.wDay = MONTHCAL_MonthLength(lpht->st.wMonth,lpht->st.wYear) + day;
1513 else if (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear))
1515 retval = MCHT_CALENDARDATENEXT;
1516 lpht->st.wMonth = infoPtr->curSel.wMonth + 1;
1517 if (lpht->st.wMonth > 12)
1519 lpht->st.wMonth = 1;
1522 lpht->st.wDay = day - MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear);
1525 retval = MCHT_CALENDARDATE;
1526 lpht->st.wMonth = infoPtr->curSel.wMonth;
1527 lpht->st.wDay = day;
1528 lpht->st.wDayOfWeek = MONTHCAL_CalculateDayOfWeek(day,lpht->st.wMonth,lpht->st.wYear);
1532 if(PtInRect(&infoPtr->todayrect, lpht->pt)) {
1533 retval = MCHT_TODAYLINK;
1538 /* Hit nothing special? What's left must be background :-) */
1540 retval = MCHT_CALENDARBK;
1542 lpht->uHit = retval;
1546 /* MCN_GETDAYSTATE notification helper */
1547 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1549 if(infoPtr->dwStyle & MCS_DAYSTATE) {
1553 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1554 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1555 nmds.nmhdr.code = MCN_GETDAYSTATE;
1556 nmds.cDayState = infoPtr->monthRange;
1557 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1559 nmds.stStart = infoPtr->todaysDate;
1560 nmds.stStart.wYear = infoPtr->curSel.wYear;
1561 nmds.stStart.wMonth = infoPtr->curSel.wMonth;
1562 nmds.stStart.wDay = 1;
1564 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1565 for(i = 0; i < infoPtr->monthRange; i++)
1566 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1568 Free(nmds.prgDayState);
1572 static void MONTHCAL_GoToNextMonth(MONTHCAL_INFO *infoPtr)
1574 SYSTEMTIME next = infoPtr->curSel;
1579 if(next.wMonth > 12) {
1584 if(!MONTHCAL_IsDateInValidRange(infoPtr, &next)) return;
1586 infoPtr->curSel = next;
1588 MONTHCAL_NotifyDayState(infoPtr);
1592 static void MONTHCAL_GoToPrevMonth(MONTHCAL_INFO *infoPtr)
1594 SYSTEMTIME prev = infoPtr->curSel;
1599 if(prev.wMonth < 1) {
1604 if(!MONTHCAL_IsDateInValidRange(infoPtr, &prev)) return;
1606 infoPtr->curSel = prev;
1608 MONTHCAL_NotifyDayState(infoPtr);
1612 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1614 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1619 hMenu = CreatePopupMenu();
1620 if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1622 WARN("Can't load resource\n");
1623 strcpyW(buf, todayW);
1625 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1626 menupoint.x = (short)LOWORD(lParam);
1627 menupoint.y = (short)HIWORD(lParam);
1628 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1629 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
1630 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1632 infoPtr->curSel = infoPtr->todaysDate;
1633 infoPtr->minSel = infoPtr->todaysDate;
1634 infoPtr->maxSel = infoPtr->todaysDate;
1635 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1641 /* creates updown control and edit box */
1642 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr)
1644 static const WCHAR EditW[] = { 'E','D','I','T',0 };
1646 infoPtr->hWndYearEdit =
1647 CreateWindowExW(0, EditW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
1648 infoPtr->titleyear.left + 3, infoPtr->titlebtnnext.top,
1649 infoPtr->titleyear.right - infoPtr->titleyear.left + 4,
1650 infoPtr->textHeight, infoPtr->hwndSelf,
1653 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
1655 infoPtr->hWndYearUpDown =
1656 CreateWindowExW(0, UPDOWN_CLASSW, 0,
1657 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
1658 infoPtr->titleyear.right + 7, infoPtr->titlebtnnext.top,
1659 18, infoPtr->textHeight, infoPtr->hwndSelf,
1662 /* attach edit box */
1663 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
1664 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
1665 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
1666 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->curSel.wYear);
1670 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1675 if (infoPtr->hWndYearUpDown)
1677 infoPtr->curSel.wYear = SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, 0);
1678 if(!DestroyWindow(infoPtr->hWndYearUpDown))
1680 FIXME("Can't destroy Updown Control\n");
1683 infoPtr->hWndYearUpDown = 0;
1685 if(!DestroyWindow(infoPtr->hWndYearEdit))
1687 FIXME("Can't destroy Updown Control\n");
1690 infoPtr->hWndYearEdit = 0;
1692 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1695 SetCapture(infoPtr->hwndSelf);
1697 ht.cbSize = sizeof(MCHITTESTINFO);
1698 ht.pt.x = (short)LOWORD(lParam);
1699 ht.pt.y = (short)HIWORD(lParam);
1701 hit = MONTHCAL_HitTest(infoPtr, &ht);
1703 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
1707 case MCHT_TITLEBTNNEXT:
1708 MONTHCAL_GoToNextMonth(infoPtr);
1709 infoPtr->status = MC_NEXTPRESSED;
1710 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1711 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1714 case MCHT_TITLEBTNPREV:
1715 MONTHCAL_GoToPrevMonth(infoPtr);
1716 infoPtr->status = MC_PREVPRESSED;
1717 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1718 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1721 case MCHT_TITLEMONTH:
1723 HMENU hMenu = CreatePopupMenu();
1728 for (i = 0; i < 12; i++)
1730 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
1731 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
1733 menupoint.x = ht.pt.x;
1734 menupoint.y = ht.pt.y;
1735 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1736 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
1737 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
1739 if ((i > 0) && (i < 13) && infoPtr->curSel.wMonth != i)
1741 infoPtr->curSel.wMonth = i;
1742 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1746 case MCHT_TITLEYEAR:
1748 MONTHCAL_EditYear(infoPtr);
1751 case MCHT_TODAYLINK:
1753 infoPtr->curSel = infoPtr->todaysDate;
1754 infoPtr->minSel = infoPtr->todaysDate;
1755 infoPtr->maxSel = infoPtr->todaysDate;
1756 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1758 MONTHCAL_NotifySelectionChange(infoPtr);
1759 MONTHCAL_NotifySelect(infoPtr);
1762 case MCHT_CALENDARDATENEXT:
1763 case MCHT_CALENDARDATEPREV:
1764 case MCHT_CALENDARDATE:
1766 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
1768 if(infoPtr->dwStyle & MCS_MULTISELECT)
1772 st[0] = st[1] = ht.st;
1774 /* clear selection range */
1775 MONTHCAL_SetSelRange(infoPtr, st);
1778 infoPtr->status = MC_SEL_LBUTDOWN;
1779 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
1789 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1797 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
1800 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
1801 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
1802 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
1804 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
1809 /* always send NM_RELEASEDCAPTURE notification */
1810 nmhdr.hwndFrom = infoPtr->hwndSelf;
1811 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1812 nmhdr.code = NM_RELEASEDCAPTURE;
1813 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
1815 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
1817 ht.cbSize = sizeof(MCHITTESTINFO);
1818 ht.pt.x = (short)LOWORD(lParam);
1819 ht.pt.y = (short)HIWORD(lParam);
1820 hit = MONTHCAL_HitTest(infoPtr, &ht);
1822 infoPtr->status = MC_SEL_LBUTUP;
1823 MONTHCAL_SetDayFocus(infoPtr, NULL);
1825 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
1827 SYSTEMTIME sel = infoPtr->curSel;
1829 if(!(infoPtr->dwStyle & MCS_MULTISELECT))
1833 st[0] = st[1] = ht.st;
1834 MONTHCAL_SetSelRange(infoPtr, st);
1835 /* will be invalidated here */
1836 MONTHCAL_SetCurSel(infoPtr, &st[0]);
1839 /* send MCN_SELCHANGE only if new date selected */
1840 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
1841 MONTHCAL_NotifySelectionChange(infoPtr);
1843 MONTHCAL_NotifySelect(infoPtr);
1851 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
1856 case MC_PREVNEXTMONTHTIMER:
1857 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToNextMonth(infoPtr);
1858 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToPrevMonth(infoPtr);
1859 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1861 case MC_TODAYUPDATETIMER:
1865 if(infoPtr->todaySet) return 0;
1868 MONTHCAL_UpdateToday(infoPtr, &st);
1870 /* notification sent anyway */
1871 MONTHCAL_NotifySelectionChange(infoPtr);
1876 ERR("got unknown timer %ld\n", id);
1885 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1888 SYSTEMTIME old_focused, st_ht;
1892 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
1894 ht.cbSize = sizeof(MCHITTESTINFO);
1895 ht.pt.x = (short)LOWORD(lParam);
1896 ht.pt.y = (short)HIWORD(lParam);
1898 hit = MONTHCAL_HitTest(infoPtr, &ht);
1900 /* not on the calendar date numbers? bail out */
1901 TRACE("hit:%x\n",hit);
1902 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
1904 MONTHCAL_SetDayFocus(infoPtr, NULL);
1909 old_focused = infoPtr->focusedSel;
1911 /* if pointer is over focused day still there's nothing to do */
1912 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
1914 MONTHCAL_CalcPosFromDay(infoPtr, ht.st.wDay, ht.st.wMonth, &r);
1916 if(infoPtr->dwStyle & MCS_MULTISELECT) {
1919 MONTHCAL_GetSelRange(infoPtr, st);
1921 /* If we're still at the first selected date and range is empty, return.
1922 If range isn't empty we should change range to a single firstSel */
1923 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
1924 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
1926 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
1928 st[0] = infoPtr->firstSel;
1929 /* we should overwrite timestamp here */
1930 MONTHCAL_CopyDate(&st_ht, &st[1]);
1932 /* bounds will be swapped here if needed */
1933 MONTHCAL_SetSelRange(infoPtr, st);
1940 /* FIXME: this should specify a rectangle containing only the days that changed
1941 using InvalidateRect */
1942 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1949 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
1956 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
1960 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
1962 MONTHCAL_Refresh(infoPtr, hdc, &ps);
1963 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
1969 MONTHCAL_KillFocus(const MONTHCAL_INFO *infoPtr, HWND hFocusWnd)
1973 if (infoPtr->hwndNotify != hFocusWnd)
1974 ShowWindow(infoPtr->hwndSelf, SW_HIDE);
1976 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1983 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
1987 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1992 /* sets the size information */
1993 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
1995 static const WCHAR O0W[] = { '0','0',0 };
1996 HDC hdc = GetDC(infoPtr->hwndSelf);
1997 RECT *title=&infoPtr->title;
1998 RECT *prev=&infoPtr->titlebtnprev;
1999 RECT *next=&infoPtr->titlebtnnext;
2000 RECT *titlemonth=&infoPtr->titlemonth;
2001 RECT *titleyear=&infoPtr->titleyear;
2002 RECT *wdays=&infoPtr->wdays;
2003 RECT *weeknumrect=&infoPtr->weeknums;
2004 RECT *days=&infoPtr->days;
2005 RECT *todayrect=&infoPtr->todayrect;
2009 INT xdiv, dx, dy, i;
2013 GetClientRect(infoPtr->hwndSelf, &rcClient);
2015 currentFont = SelectObject(hdc, infoPtr->hFont);
2017 /* get the height and width of each day's text */
2018 GetTextMetricsW(hdc, &tm);
2019 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2021 /* find largest abbreviated day name for current locale */
2022 size.cx = sz.cx = 0;
2023 for (i = 0; i < 7; i++)
2025 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1,
2026 buff, countof(buff)))
2028 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2029 if (sz.cx > size.cx) size.cx = sz.cx;
2031 else /* locale independent fallback on failure */
2033 static const WCHAR SunW[] = { 'S','u','n',0 };
2035 GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2040 infoPtr->textWidth = size.cx + 2;
2042 /* recalculate the height and width increments and offsets */
2043 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2045 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2047 infoPtr->width_increment = size.cx * 2 + 4;
2048 infoPtr->height_increment = infoPtr->textHeight;
2050 /* calculate title area */
2052 title->bottom = 3 * infoPtr->height_increment / 2;
2054 title->right = infoPtr->width_increment * xdiv;
2056 /* set the dimensions of the next and previous buttons and center */
2057 /* the month text vertically */
2058 prev->top = next->top = title->top + 4;
2059 prev->bottom = next->bottom = title->bottom - 4;
2060 prev->left = title->left + 4;
2061 prev->right = prev->left + (title->bottom - title->top);
2062 next->right = title->right - 4;
2063 next->left = next->right - (title->bottom - title->top);
2065 /* titlemonth->left and right change based upon the current month */
2066 /* and are recalculated in refresh as the current month may change */
2067 /* without the control being resized */
2068 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2069 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2071 /* setup the dimensions of the rectangle we draw the names of the */
2072 /* days of the week in */
2073 weeknumrect->left = 0;
2075 if(infoPtr->dwStyle & MCS_WEEKNUMBERS)
2076 weeknumrect->right = prev->right;
2078 weeknumrect->right = weeknumrect->left;
2080 wdays->left = days->left = weeknumrect->right;
2081 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2082 wdays->top = title->bottom;
2083 wdays->bottom = wdays->top + infoPtr->height_increment;
2085 days->top = weeknumrect->top = wdays->bottom;
2086 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2088 todayrect->left = 0;
2089 todayrect->right = title->right;
2090 todayrect->top = days->bottom;
2091 todayrect->bottom = days->bottom + infoPtr->height_increment;
2093 /* offset all rectangles to center in client area */
2094 dx = (rcClient.right - title->right) / 2;
2095 dy = (rcClient.bottom - todayrect->bottom) / 2;
2097 /* if calendar doesn't fit client area show it at left/top bounds */
2098 if (title->left + dx < 0) dx = 0;
2099 if (title->top + dy < 0) dy = 0;
2101 if (dx != 0 || dy != 0)
2103 OffsetRect(title, dx, dy);
2104 OffsetRect(prev, dx, dy);
2105 OffsetRect(next, dx, dy);
2106 OffsetRect(titlemonth, dx, dy);
2107 OffsetRect(titleyear, dx, dy);
2108 OffsetRect(wdays, dx, dy);
2109 OffsetRect(weeknumrect, dx, dy);
2110 OffsetRect(days, dx, dy);
2111 OffsetRect(todayrect, dx, dy);
2114 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2115 infoPtr->width_increment,infoPtr->height_increment,
2116 wine_dbgstr_rect(&rcClient),
2117 wine_dbgstr_rect(title),
2118 wine_dbgstr_rect(wdays),
2119 wine_dbgstr_rect(days),
2120 wine_dbgstr_rect(todayrect));
2122 /* restore the originally selected font */
2123 SelectObject(hdc, currentFont);
2125 ReleaseDC(infoPtr->hwndSelf, hdc);
2128 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2130 TRACE("(width=%d, height=%d)\n", Width, Height);
2132 MONTHCAL_UpdateSize(infoPtr);
2134 /* invalidate client area and erase background */
2135 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2140 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2142 return (LRESULT)infoPtr->hFont;
2145 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2150 if (!hFont) return 0;
2152 hOldFont = infoPtr->hFont;
2153 infoPtr->hFont = hFont;
2155 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2156 lf.lfWeight = FW_BOLD;
2157 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2159 MONTHCAL_UpdateSize(infoPtr);
2162 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2164 return (LRESULT)hOldFont;
2167 /* update theme after a WM_THEMECHANGED message */
2168 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2170 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2171 CloseThemeData (theme);
2172 OpenThemeData (infoPtr->hwndSelf, themeClass);
2176 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2177 const STYLESTRUCT *lpss)
2179 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2180 wStyleType, lpss->styleOld, lpss->styleNew);
2182 if (wStyleType != GWL_STYLE) return 0;
2184 infoPtr->dwStyle = lpss->styleNew;
2189 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2192 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2193 wStyleType, lpss->styleOld, lpss->styleNew);
2195 /* block MCS_MULTISELECT change */
2196 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2198 if (lpss->styleOld & MCS_MULTISELECT)
2199 lpss->styleNew |= MCS_MULTISELECT;
2201 lpss->styleNew &= ~MCS_MULTISELECT;
2207 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2209 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2211 MONTHCAL_INFO *infoPtr;
2213 /* allocate memory for info structure */
2214 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2215 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2217 if(infoPtr == NULL) {
2218 ERR( "could not allocate info memory!\n");
2222 infoPtr->hwndSelf = hwnd;
2223 infoPtr->hwndNotify = lpcs->hwndParent;
2224 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2226 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
2228 /* initialize info structure */
2229 /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
2231 GetLocalTime(&infoPtr->todaysDate);
2232 infoPtr->firstDayHighWord = FALSE;
2233 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2235 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2236 infoPtr->monthRange = 3;
2237 infoPtr->monthdayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
2238 infoPtr->titlebk = comctl32_color.clrActiveCaption;
2239 infoPtr->titletxt = comctl32_color.clrWindow;
2240 infoPtr->monthbk = comctl32_color.clrWindow;
2241 infoPtr->trailingtxt = comctl32_color.clrGrayText;
2242 infoPtr->bk = comctl32_color.clrWindow;
2243 infoPtr->txt = comctl32_color.clrWindowText;
2245 infoPtr->minSel = infoPtr->todaysDate;
2246 infoPtr->maxSel = infoPtr->todaysDate;
2247 infoPtr->curSel = infoPtr->todaysDate;
2249 /* call MONTHCAL_UpdateSize to set all of the dimensions */
2250 /* of the control */
2251 MONTHCAL_UpdateSize(infoPtr);
2253 /* today auto update timer, to be freed only on control destruction */
2254 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2256 OpenThemeData (infoPtr->hwndSelf, themeClass);
2263 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2265 /* free month calendar info data */
2266 Free(infoPtr->monthdayState);
2267 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2269 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2276 static LRESULT WINAPI
2277 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2279 MONTHCAL_INFO *infoPtr;
2281 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2283 infoPtr = MONTHCAL_GetInfoPtr(hwnd);
2284 if (!infoPtr && (uMsg != WM_CREATE))
2285 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2289 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2292 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2294 case MCM_GETMAXSELCOUNT:
2295 return MONTHCAL_GetMaxSelCount(infoPtr);
2297 case MCM_SETMAXSELCOUNT:
2298 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2300 case MCM_GETSELRANGE:
2301 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2303 case MCM_SETSELRANGE:
2304 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2306 case MCM_GETMONTHRANGE:
2307 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2309 case MCM_SETDAYSTATE:
2310 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2312 case MCM_GETMINREQRECT:
2313 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2316 return MONTHCAL_GetColor(infoPtr, wParam);
2319 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2322 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2325 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2328 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2330 case MCM_GETFIRSTDAYOFWEEK:
2331 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2333 case MCM_SETFIRSTDAYOFWEEK:
2334 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2337 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2340 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2342 case MCM_GETMONTHDELTA:
2343 return MONTHCAL_GetMonthDelta(infoPtr);
2345 case MCM_SETMONTHDELTA:
2346 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2348 case MCM_GETMAXTODAYWIDTH:
2349 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2352 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2355 return MONTHCAL_KillFocus(infoPtr, (HWND)wParam);
2358 return MONTHCAL_RButtonUp(infoPtr, lParam);
2360 case WM_LBUTTONDOWN:
2361 return MONTHCAL_LButtonDown(infoPtr, lParam);
2364 return MONTHCAL_MouseMove(infoPtr, lParam);
2367 return MONTHCAL_LButtonUp(infoPtr, lParam);
2369 case WM_PRINTCLIENT:
2371 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2374 return MONTHCAL_SetFocus(infoPtr);
2377 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2380 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2383 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2386 return MONTHCAL_GetFont(infoPtr);
2389 return MONTHCAL_Timer(infoPtr, wParam);
2391 case WM_THEMECHANGED:
2392 return theme_changed (infoPtr);
2395 return MONTHCAL_Destroy(infoPtr);
2397 case WM_SYSCOLORCHANGE:
2398 COMCTL32_RefreshSysColors();
2401 case WM_STYLECHANGED:
2402 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2404 case WM_STYLECHANGING:
2405 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2408 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2409 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2410 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2416 MONTHCAL_Register(void)
2420 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2421 wndClass.style = CS_GLOBALCLASS;
2422 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2423 wndClass.cbClsExtra = 0;
2424 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2425 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2426 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2427 wndClass.lpszClassName = MONTHCAL_CLASSW;
2429 RegisterClassW(&wndClass);
2434 MONTHCAL_Unregister(void)
2436 UnregisterClassW(MONTHCAL_CLASSW, NULL);