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 intentionally 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 static inline void 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 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
412 if(--date->wMonth < 1)
417 date->wDayOfWeek = MONTHCAL_CalculateDayOfWeek(date->wDay, date->wMonth,
421 /* Returns full date for a first currently visible day */
422 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
426 firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear);
428 *date = infoPtr->curSel;
429 MONTHCAL_GetPrevMonth(date);
431 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
432 (infoPtr->firstDay - firstDay) % 7 + 1;
434 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
437 /* fix day of week */
438 date->wDayOfWeek = MONTHCAL_CalculateDayOfWeek(date->wDay, date->wMonth,
442 /* Returns full date for a last currently visible day */
443 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
447 *date = infoPtr->curSel;
448 MONTHCAL_GetNextMonth(date);
450 MONTHCAL_GetMinDate(infoPtr, &st);
451 /* Use month length to get max day. 42 means max day count in calendar area */
452 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
453 MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear);
455 /* fix day of week */
456 date->wDayOfWeek = MONTHCAL_CalculateDayOfWeek(date->wDay, date->wMonth,
460 /* From a given point, calculate the row (weekpos), column(daypos)
461 and day in the calendar. day== 0 mean the last day of tha last month
463 static int MONTHCAL_CalcDayFromPos(const MONTHCAL_INFO *infoPtr, int x, int y,
464 int *daypos,int *weekpos)
466 int retval, firstDay;
469 GetClientRect(infoPtr->hwndSelf, &rcClient);
471 /* if the point is outside the x bounds of the window put
472 it at the boundary */
473 if (x > rcClient.right)
477 *daypos = (x - infoPtr->days.left ) / infoPtr->width_increment;
478 *weekpos = (y - infoPtr->days.top ) / infoPtr->height_increment;
480 firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear)+6 - infoPtr->firstDay)%7;
481 retval = *daypos + (7 * *weekpos) - firstDay;
485 /* day is the day of the month, 1 == 1st day of the month */
486 /* sets x and y to be the position of the day */
487 /* x == day, y == week where(0,0) == firstDay, 1st week */
488 static void MONTHCAL_CalcDayXY(const MONTHCAL_INFO *infoPtr, int day, int month,
491 int firstDay, prevMonth;
493 firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear) +6 - infoPtr->firstDay)%7;
495 if(month==infoPtr->curSel.wMonth) {
496 *x = (day + firstDay) % 7;
497 *y = (day + firstDay - *x) / 7;
500 if(month < infoPtr->curSel.wMonth) {
501 prevMonth = month - 1;
505 *x = (MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear) - firstDay) % 7;
510 *y = MONTHCAL_MonthLength(month, infoPtr->curSel.wYear - 1) / 7;
511 *x = (day + firstDay + MONTHCAL_MonthLength(month,
512 infoPtr->curSel.wYear)) % 7;
516 /* x: column(day), y: row(week) */
517 static void MONTHCAL_CalcDayRect(const MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
519 r->left = infoPtr->days.left + x * infoPtr->width_increment;
520 r->right = r->left + infoPtr->width_increment;
521 r->top = infoPtr->days.top + y * infoPtr->height_increment;
522 r->bottom = r->top + infoPtr->textHeight;
526 /* sets the RECT struct r to the rectangle around the day and month */
527 /* day is the day value of the month(1 == 1st), month is the month */
528 /* value(january == 1, december == 12) */
529 static inline void MONTHCAL_CalcPosFromDay(const MONTHCAL_INFO *infoPtr,
530 int day, int month, RECT *r)
534 MONTHCAL_CalcDayXY(infoPtr, day, month, &x, &y);
535 MONTHCAL_CalcDayRect(infoPtr, r, x, y);
538 /* Focused day helper:
540 - set focused date to given value;
541 - reset to zero value if NULL passed;
542 - invalidate previous and new day rectangle only if needed.
544 Returns TRUE if focused day changed, FALSE otherwise.
546 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
552 /* there's nothing to do if it's the same date,
553 mouse move within same date rectangle case */
554 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
556 /* invalidate old focused day */
557 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->focusedSel.wDay,
558 infoPtr->focusedSel.wMonth, &r);
559 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
561 infoPtr->focusedSel = *st;
564 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->focusedSel.wDay,
565 infoPtr->focusedSel.wMonth, &r);
567 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
568 infoPtr->focusedSel = st_null;
570 /* on set invalidates new day, on reset clears previous focused day */
571 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
576 /* day is the day in the month(1 == 1st of the month) */
577 /* month is the month value(1 == january, 12 == december) */
578 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month)
580 HPEN hRedPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
581 HPEN hOldPen2 = SelectObject(hdc, hRedPen);
585 MONTHCAL_CalcPosFromDay(infoPtr, day, month, &day_rect);
587 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
588 Rectangle(hdc, day_rect.left, day_rect.top, day_rect.right, day_rect.bottom);
590 SelectObject(hdc, hOldBrush);
591 DeleteObject(hRedPen);
592 SelectObject(hdc, hOldPen2);
595 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month,
596 int x, int y, int bold)
598 static const WCHAR fmtW[] = { '%','d',0 };
601 static BOOL haveBoldFont, haveSelectedDay = FALSE;
606 wsprintfW(buf, fmtW, day);
608 /* No need to check styles: when selection is not valid, it is set to zero.
609 * 1<day<31, so everything is OK.
612 MONTHCAL_CalcDayRect(infoPtr, &r, x, y);
614 if((day>=infoPtr->minSel.wDay) && (day<=infoPtr->maxSel.wDay)
615 && (month == infoPtr->curSel.wMonth)) {
618 TRACE("%d %d %d\n",day, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
619 TRACE("%s\n", wine_dbgstr_rect(&r));
620 oldCol = SetTextColor(hdc, infoPtr->monthbk);
621 oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
622 hbr = GetSysColorBrush(COLOR_HIGHLIGHT);
623 FillRect(hdc, &r, hbr);
625 /* FIXME: this may need to be changed now b/c of the other
626 drawing changes 11/3/99 CMM */
627 r2.left = r.left - 0.25 * infoPtr->textWidth;
629 r2.right = r.left + 0.5 * infoPtr->textWidth;
630 r2.bottom = r.bottom;
631 if(haveSelectedDay) FillRect(hdc, &r2, hbr);
632 haveSelectedDay = TRUE;
634 haveSelectedDay = FALSE;
637 /* need to add some code for multiple selections */
639 if((bold) &&(!haveBoldFont)) {
640 SelectObject(hdc, infoPtr->hBoldFont);
643 if((!bold) &&(haveBoldFont)) {
644 SelectObject(hdc, infoPtr->hFont);
645 haveBoldFont = FALSE;
648 SetBkMode(hdc,TRANSPARENT);
649 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
651 if(haveSelectedDay) {
652 SetTextColor(hdc, oldCol);
653 SetBkColor(hdc, oldBk);
658 static void paint_button (MONTHCAL_INFO *infoPtr, HDC hdc, BOOL btnNext)
660 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
661 RECT *r = btnNext ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
662 BOOL pressed = btnNext ? (infoPtr->status & MC_NEXTPRESSED) :
663 (infoPtr->status & MC_PREVPRESSED);
666 static const int states[] = {
668 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
670 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
672 int stateNum = btnNext ? 3 : 0;
677 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
679 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
683 int style = btnNext ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
685 style |= DFCS_PUSHED;
688 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
691 DrawFrameControl(hdc, r, DFC_SCROLL, style);
696 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
698 static const WCHAR fmt_monthW[] = { '%','s',' ','%','l','d',0 };
699 RECT *title=&infoPtr->title;
700 RECT *titlemonth=&infoPtr->titlemonth;
701 RECT *titleyear=&infoPtr->titleyear;
703 int i, j, m, mask, day, prevMonth;
704 int textHeight = infoPtr->textHeight;
711 COLORREF oldTextColor, oldBkColor;
713 RECT rcDay; /* used in MONTHCAL_CalcDayRect() */
717 oldTextColor = SetTextColor(hdc, comctl32_color.clrWindowText);
719 /* fill background */
720 hbr = CreateSolidBrush (infoPtr->bk);
721 FillRect(hdc, &ps->rcPaint, hbr);
725 if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
727 hbr = CreateSolidBrush(infoPtr->titlebk);
728 FillRect(hdc, title, hbr);
732 /* if the previous button is pressed draw it depressed */
733 if(IntersectRect(&rcTemp, &(ps->rcPaint), &infoPtr->titlebtnprev))
734 paint_button(infoPtr, hdc, FALSE);
736 /* if next button is depressed draw it depressed */
737 if(IntersectRect(&rcTemp, &(ps->rcPaint), &infoPtr->titlebtnnext))
738 paint_button(infoPtr, hdc, TRUE);
740 oldBkColor = SetBkColor(hdc, infoPtr->titlebk);
741 SetTextColor(hdc, infoPtr->titletxt);
742 currentFont = SelectObject(hdc, infoPtr->hBoldFont);
744 GetLocaleInfoW( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+infoPtr->curSel.wMonth -1,
746 wsprintfW(buf, fmt_monthW, buf1, infoPtr->curSel.wYear);
748 if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
750 DrawTextW(hdc, buf, strlenW(buf), title,
751 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
754 /* titlemonth left/right contained rect for whole titletxt('June 1999')
755 * MCM_HitTestInfo wants month & year rects, so prepare these now.
756 *(no, we can't draw them separately; the whole text is centered)
758 GetTextExtentPoint32W(hdc, buf, strlenW(buf), &size);
759 titlemonth->left = title->right / 2 + title->left / 2 - size.cx / 2;
760 titleyear->right = title->right / 2 + title->left / 2 + size.cx / 2;
761 GetTextExtentPoint32W(hdc, buf1, strlenW(buf1), &size);
762 titlemonth->right = titlemonth->left + size.cx;
763 titleyear->left = titlemonth->right;
765 /* draw month area */
766 rcTemp.top=infoPtr->wdays.top;
767 rcTemp.left=infoPtr->wdays.left;
768 rcTemp.bottom=infoPtr->todayrect.bottom;
769 rcTemp.right =infoPtr->todayrect.right;
770 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcTemp))
772 hbr = CreateSolidBrush(infoPtr->monthbk);
773 FillRect(hdc, &rcTemp, hbr);
777 /* draw line under day abbreviations */
779 MoveToEx(hdc, infoPtr->days.left + 3, title->bottom + textHeight + 1, NULL);
780 LineTo(hdc, infoPtr->days.right - 3, title->bottom + textHeight + 1);
782 prevMonth = infoPtr->curSel.wMonth - 1;
783 if(prevMonth == 0) /* if curSel.wMonth is january(1) prevMonth is */
784 prevMonth = 12; /* december(12) of the previous year */
786 infoPtr->wdays.left = infoPtr->days.left = infoPtr->weeknums.right;
788 /* draw day abbreviations */
789 SelectObject(hdc, infoPtr->hFont);
790 SetBkColor(hdc, infoPtr->monthbk);
791 SetTextColor(hdc, infoPtr->trailingtxt);
793 /* rectangle to draw a single day abbreviation within */
794 dayrect = infoPtr->wdays;
795 dayrect.right = dayrect.left + infoPtr->width_increment;
797 i = infoPtr->firstDay;
799 for(j = 0; j < 7; j++) {
800 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
801 DrawTextW(hdc, buf, strlenW(buf), &dayrect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
802 dayrect.left += infoPtr->width_increment;
803 dayrect.right += infoPtr->width_increment;
806 /* draw day numbers; first, the previous month */
807 MONTHCAL_GetMinDate(infoPtr, &st);
809 startofprescal = day;
814 while(day <= MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear)) {
815 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
816 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
818 MONTHCAL_DrawDay(infoPtr, hdc, day, prevMonth, i, 0,
819 infoPtr->monthdayState[m] & mask);
827 /* draw `current' month */
829 day = 1; /* start at the beginning of the current month */
831 infoPtr->firstDayplace = i;
832 SetTextColor(hdc, infoPtr->txt);
836 /* draw the first week of the current month */
838 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
839 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
841 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth, i, 0,
842 infoPtr->monthdayState[m] & mask);
850 j = 1; /* move to the 2nd week of the current month */
851 i = 0; /* move back to sunday */
852 while(day <= MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear)) {
853 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
854 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
856 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth, i, j,
857 infoPtr->monthdayState[m] & mask);
862 if(i>6) { /* past saturday, goto the next weeks sunday */
868 /* draw `next' month */
870 day = 1; /* start at the first day of the next month */
874 SetTextColor(hdc, infoPtr->trailingtxt);
875 while((i<7) &&(j<6)) {
876 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
877 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
879 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth + 1, i, j,
880 infoPtr->monthdayState[m] & mask);
886 if(i==7) { /* past saturday, go to next week's sunday */
891 SetTextColor(hdc, infoPtr->txt);
893 /* draw today mark rectangle */
894 if((infoPtr->curSel.wMonth == infoPtr->todaysDate.wMonth) &&
895 (infoPtr->curSel.wYear == infoPtr->todaysDate.wYear) &&
896 !(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
898 MONTHCAL_CircleDay(infoPtr, hdc, infoPtr->todaysDate.wDay, infoPtr->todaysDate.wMonth);
901 /* draw focused day */
902 if(!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
904 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->focusedSel.wDay,
905 infoPtr->focusedSel.wMonth, &rcDay);
907 DrawFocusRect(hdc, &rcDay);
910 /* draw `today' date if style allows it, and draw a circle before today's
911 * date if necessary */
912 if(!(infoPtr->dwStyle & MCS_NOTODAY)) {
913 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
914 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
917 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
918 /*day is the number of days from nextmonth we put on the calendar */
919 MONTHCAL_CircleDay(infoPtr, hdc,
920 day+MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear),
921 infoPtr->curSel.wMonth);
923 if (!LoadStringW(COMCTL32_hModule, IDM_TODAY, buf1, countof(buf1)))
925 WARN("Can't load resource\n");
926 strcpyW(buf1, todayW);
928 MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
929 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
930 buf2, countof(buf2));
931 wsprintfW(buf, fmt_todayW, buf1, buf2);
932 SelectObject(hdc, infoPtr->hBoldFont);
934 DrawTextW(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
935 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rtoday))
937 DrawTextW(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
939 SelectObject(hdc, infoPtr->hFont);
942 /* eventually draw week numbers */
943 if(infoPtr->dwStyle & MCS_WEEKNUMBERS) {
944 static const WCHAR fmt_weekW[] = { '%','d',0 }; /* week numbers format */
945 int mindays, weeknum, weeknum1;
947 /* Rules what week to call the first week of a new year:
948 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
949 The week containing Jan 1 is the first week of year
950 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
951 First week of year must contain 4 days of the new year
952 LOCALE_IFIRSTWEEKOFYEAR == 1 (what contries?)
953 The first week of the year must contain only days of the new year
955 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
956 weeknum = atoiW(buf);
967 if (infoPtr->curSel.wMonth < 2)
969 /* calculate all those exceptions for january */
970 weeknum1 = MONTHCAL_CalculateDayOfWeek(1, 1, infoPtr->curSel.wYear);
971 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
976 for(i = 0; i < 11; i++)
977 weeknum += MONTHCAL_MonthLength(i+1, infoPtr->curSel.wYear - 1);
979 weeknum += startofprescal + 7;
981 weeknum1 = MONTHCAL_CalculateDayOfWeek(1, 1, infoPtr->curSel.wYear - 1);
982 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
988 for(i = 0; i < prevMonth - 1; i++)
989 weeknum += MONTHCAL_MonthLength(i+1, infoPtr->curSel.wYear);
991 weeknum += startofprescal + 7;
993 weeknum1 = MONTHCAL_CalculateDayOfWeek(1, 1, infoPtr->curSel.wYear);
994 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
997 dayrect = infoPtr->weeknums;
998 dayrect.bottom = dayrect.top + infoPtr->height_increment;
1000 for(i = 0; i < 6; i++) {
1001 if((i == 0) && (weeknum > 50))
1003 wsprintfW(buf, fmt_weekW, weeknum);
1006 else if((i == 5) && (weeknum > 47))
1008 wsprintfW(buf, fmt_weekW, 1);
1011 wsprintfW(buf, fmt_weekW, weeknum + i);
1013 DrawTextW(hdc, buf, -1, &dayrect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1014 dayrect.top += infoPtr->height_increment;
1015 dayrect.bottom += infoPtr->height_increment;
1018 MoveToEx(hdc, infoPtr->weeknums.right, infoPtr->weeknums.top + 3 , NULL);
1019 LineTo(hdc, infoPtr->weeknums.right, infoPtr->weeknums.bottom);
1022 /* currentFont was font at entering Refresh */
1023 SetBkColor(hdc, oldBkColor);
1024 SelectObject(hdc, currentFont);
1025 SetTextColor(hdc, oldTextColor);
1030 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, LPRECT lpRect)
1032 TRACE("rect %p\n", lpRect);
1034 if(!lpRect) return FALSE;
1036 lpRect->left = infoPtr->title.left;
1037 lpRect->top = infoPtr->title.top;
1038 lpRect->right = infoPtr->title.right;
1039 lpRect->bottom = infoPtr->todayrect.bottom;
1041 AdjustWindowRect(lpRect, infoPtr->dwStyle, FALSE);
1043 /* minimal rectangle is zero based */
1044 OffsetRect(lpRect, -lpRect->left, -lpRect->top);
1046 TRACE("%s\n", wine_dbgstr_rect(lpRect));
1053 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, INT index)
1058 case MCSC_BACKGROUND:
1061 return infoPtr->txt;
1063 return infoPtr->titlebk;
1064 case MCSC_TITLETEXT:
1065 return infoPtr->titletxt;
1067 return infoPtr->monthbk;
1068 case MCSC_TRAILINGTEXT:
1069 return infoPtr->trailingtxt;
1077 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, INT index, COLORREF color)
1081 TRACE("%d: color %08x\n", index, color);
1084 case MCSC_BACKGROUND:
1086 infoPtr->bk = color;
1089 prev = infoPtr->txt;
1090 infoPtr->txt = color;
1093 prev = infoPtr->titlebk;
1094 infoPtr->titlebk = color;
1096 case MCSC_TITLETEXT:
1097 prev=infoPtr->titletxt;
1098 infoPtr->titletxt = color;
1101 prev = infoPtr->monthbk;
1102 infoPtr->monthbk = color;
1104 case MCSC_TRAILINGTEXT:
1105 prev = infoPtr->trailingtxt;
1106 infoPtr->trailingtxt = color;
1110 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND ? TRUE : FALSE);
1116 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1121 return infoPtr->delta;
1123 return infoPtr->visible;
1128 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1130 INT prev = infoPtr->delta;
1132 TRACE("delta %d\n", delta);
1134 infoPtr->delta = delta;
1139 static inline LRESULT
1140 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1144 /* convert from SYSTEMTIME to locale format */
1145 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1147 return MAKELONG(day, infoPtr->firstDaySet);
1151 /* Sets the first day of the week that will appear in the control
1155 * [I] infoPtr : valid pointer to control data
1156 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1160 * Low word contains previous first day,
1161 * high word indicates was first day forced with this message before or is
1162 * locale difined (TRUE - was forced, FALSE - wasn't).
1164 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1165 * FIXME: we need more error checking here
1168 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1170 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1179 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1180 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1182 new_day = atoiW(buf);
1184 infoPtr->firstDaySet = FALSE;
1188 new_day = 6; /* max first day allowed */
1189 infoPtr->firstDaySet = TRUE;
1193 /* Native behaviour for that case is broken: invalid date number >31
1194 got displayed at (0,0) position, current month starts always from
1195 (1,0) position. Should be implemnted here as well. */
1197 FIXME("No bug compatibility for day=%d\n", day);
1200 infoPtr->firstDaySet = TRUE;
1203 /* convert from locale to SYSTEMTIME format */
1204 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1206 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1213 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
1222 /*FIXME: currently multicalendar feature isn't implelented, so entirely
1223 visible month is current */
1224 st[0] = st[1] = infoPtr->curSel;
1227 st[0].wDayOfWeek = MONTHCAL_CalculateDayOfWeek(1, st[0].wMonth, st[0].wYear);
1229 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
1230 st[1].wDayOfWeek = MONTHCAL_CalculateDayOfWeek(st[1].wDay, st[1].wMonth,
1232 /* a single current month used */
1237 /*FIXME: currently multicalendar feature isn't implelented,
1238 min date from previous month and max date from next one returned */
1239 MONTHCAL_GetMinDate(infoPtr, &st[0]);
1240 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
1244 WARN("Unknown flag value, got %d\n", flag);
1248 return infoPtr->monthRange;
1253 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1255 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1260 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1262 FILETIME ft_min, ft_max;
1264 TRACE("%x %p\n", limits, range);
1266 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1267 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1270 if (limits & GDTR_MIN)
1272 if (!MONTHCAL_ValidateTime(&range[0]))
1273 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1275 infoPtr->minDate = range[0];
1276 infoPtr->rangeValid |= GDTR_MIN;
1278 if (limits & GDTR_MAX)
1280 if (!MONTHCAL_ValidateTime(&range[1]))
1281 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1283 infoPtr->maxDate = range[1];
1284 infoPtr->rangeValid |= GDTR_MAX;
1287 /* Only one limit set - we are done */
1288 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1291 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1292 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1294 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1296 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1298 /* Native swaps limits only when both limits are being set. */
1299 SYSTEMTIME st_tmp = infoPtr->minDate;
1300 infoPtr->minDate = infoPtr->maxDate;
1301 infoPtr->maxDate = st_tmp;
1305 /* reset the other limit */
1306 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1307 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1308 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1317 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1319 TRACE("%p\n", range);
1321 if(!range) return FALSE;
1323 range[1] = infoPtr->maxDate;
1324 range[0] = infoPtr->minDate;
1326 return infoPtr->rangeValid;
1331 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1335 TRACE("%d %p\n", months, states);
1336 if(months != infoPtr->monthRange) return 0;
1338 for(i = 0; i < months; i++)
1339 infoPtr->monthdayState[i] = states[i];
1345 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1347 TRACE("%p\n", curSel);
1348 if(!curSel) return FALSE;
1349 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1351 *curSel = infoPtr->curSel;
1352 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1356 /* FIXME: if the specified date is not visible, make it visible */
1358 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1360 TRACE("%p\n", curSel);
1361 if(!curSel) return FALSE;
1362 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1364 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1365 /* exit earlier if selection equals current */
1366 if (MONTHCAL_IsDateEqual(&infoPtr->curSel, curSel)) return TRUE;
1368 if(!MONTHCAL_IsDateInValidRange(infoPtr, curSel)) return FALSE;
1370 infoPtr->minSel = *curSel;
1371 infoPtr->maxSel = *curSel;
1373 infoPtr->curSel = *curSel;
1375 /* FIXME: it's possible to reduce rectangle here */
1376 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1383 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1385 return infoPtr->maxSelCount;
1390 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1394 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1395 if(max <= 0) return FALSE;
1397 infoPtr->maxSelCount = max;
1404 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1406 TRACE("%p\n", range);
1408 if(!range) return FALSE;
1410 if(infoPtr->dwStyle & MCS_MULTISELECT)
1412 range[1] = infoPtr->maxSel;
1413 range[0] = infoPtr->minSel;
1414 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1423 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1425 TRACE("%p\n", range);
1427 if(!range) return FALSE;
1429 if(infoPtr->dwStyle & MCS_MULTISELECT)
1431 SYSTEMTIME old_range[2];
1433 /* adjust timestamps */
1434 if(!MONTHCAL_ValidateTime(&range[0]))
1435 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1436 if(!MONTHCAL_ValidateTime(&range[1]))
1437 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1439 /* maximum range exceeded */
1440 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1442 old_range[0] = infoPtr->minSel;
1443 old_range[1] = infoPtr->maxSel;
1445 /* swap if min > max */
1446 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1448 infoPtr->minSel = range[0];
1449 infoPtr->maxSel = range[1];
1453 infoPtr->minSel = range[1];
1454 infoPtr->maxSel = range[0];
1457 /* update day of week */
1458 infoPtr->minSel.wDayOfWeek =
1459 MONTHCAL_CalculateDayOfWeek(infoPtr->minSel.wDay, infoPtr->minSel.wMonth,
1460 infoPtr->minSel.wYear);
1461 infoPtr->maxSel.wDayOfWeek =
1462 MONTHCAL_CalculateDayOfWeek(infoPtr->maxSel.wDay, infoPtr->maxSel.wMonth,
1463 infoPtr->maxSel.wYear);
1465 /* redraw if bounds changed */
1466 /* FIXME: no actual need to redraw everything */
1467 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1468 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1470 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1473 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1482 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1484 TRACE("%p\n", today);
1486 if(!today) return FALSE;
1487 *today = infoPtr->todaysDate;
1491 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1495 * TRUE - today date changed
1496 * FALSE - today date isn't changed
1499 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1503 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1505 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->todaysDate.wDay,
1506 infoPtr->todaysDate.wMonth, &old_r);
1507 MONTHCAL_CalcPosFromDay(infoPtr, today->wDay, today->wMonth, &new_r);
1509 infoPtr->todaysDate = *today;
1511 /* only two days need redrawing */
1512 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1513 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1517 /* MCM_SETTODAT handler */
1519 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1521 TRACE("%p\n", today);
1523 if(!today) return FALSE;
1525 /* remember if date was set successfully */
1526 if(MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1532 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1538 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1543 ZeroMemory(&lpht->st, sizeof(lpht->st));
1545 /* Comment in for debugging...
1546 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,
1547 infoPtr->wdays.left, infoPtr->wdays.right,
1548 infoPtr->wdays.top, infoPtr->wdays.bottom,
1549 infoPtr->days.left, infoPtr->days.right,
1550 infoPtr->days.top, infoPtr->days.bottom,
1551 infoPtr->todayrect.left, infoPtr->todayrect.right,
1552 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1553 infoPtr->weeknums.left, infoPtr->weeknums.right,
1554 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1557 /* are we in the header? */
1559 if(PtInRect(&infoPtr->title, lpht->pt)) {
1560 if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1561 retval = MCHT_TITLEBTNPREV;
1564 if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1565 retval = MCHT_TITLEBTNNEXT;
1568 if(PtInRect(&infoPtr->titlemonth, lpht->pt)) {
1569 retval = MCHT_TITLEMONTH;
1572 if(PtInRect(&infoPtr->titleyear, lpht->pt)) {
1573 retval = MCHT_TITLEYEAR;
1577 retval = MCHT_TITLE;
1581 day = MONTHCAL_CalcDayFromPos(infoPtr,x,y,&wday,&wnum);
1582 if(PtInRect(&infoPtr->wdays, lpht->pt)) {
1583 retval = MCHT_CALENDARDAY;
1584 lpht->st.wYear = infoPtr->curSel.wYear;
1585 lpht->st.wMonth = (day < 1)? infoPtr->curSel.wMonth -1 : infoPtr->curSel.wMonth;
1586 lpht->st.wDay = (day < 1)?
1587 MONTHCAL_MonthLength(infoPtr->curSel.wMonth-1, infoPtr->curSel.wYear) -day : day;
1590 if(PtInRect(&infoPtr->weeknums, lpht->pt)) {
1591 retval = MCHT_CALENDARWEEKNUM;
1592 lpht->st.wYear = infoPtr->curSel.wYear;
1593 lpht->st.wMonth = (day < 1) ? infoPtr->curSel.wMonth -1 :
1594 (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear)) ?
1595 infoPtr->curSel.wMonth +1 :infoPtr->curSel.wMonth;
1596 lpht->st.wDay = (day < 1 ) ?
1597 MONTHCAL_MonthLength(infoPtr->curSel.wMonth-1,infoPtr->curSel.wYear) -day :
1598 (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear)) ?
1599 day - MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear) : day;
1602 if(PtInRect(&infoPtr->days, lpht->pt))
1604 lpht->st.wYear = infoPtr->curSel.wYear;
1605 lpht->st.wMonth = infoPtr->curSel.wMonth;
1608 retval = MCHT_CALENDARDATEPREV;
1609 MONTHCAL_GetPrevMonth(&lpht->st);
1610 lpht->st.wDay = MONTHCAL_MonthLength(lpht->st.wMonth, lpht->st.wYear) + day;
1612 else if (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear))
1614 retval = MCHT_CALENDARDATENEXT;
1615 MONTHCAL_GetNextMonth(&lpht->st);
1616 lpht->st.wDay = day - MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear);
1619 retval = MCHT_CALENDARDATE;
1620 lpht->st.wDay = day;
1622 /* always update day of week */
1623 lpht->st.wDayOfWeek = MONTHCAL_CalculateDayOfWeek(day, lpht->st.wMonth,
1627 if(PtInRect(&infoPtr->todayrect, lpht->pt)) {
1628 retval = MCHT_TODAYLINK;
1633 /* Hit nothing special? What's left must be background :-) */
1635 retval = MCHT_CALENDARBK;
1637 lpht->uHit = retval;
1641 /* MCN_GETDAYSTATE notification helper */
1642 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1644 if(infoPtr->dwStyle & MCS_DAYSTATE) {
1648 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1649 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1650 nmds.nmhdr.code = MCN_GETDAYSTATE;
1651 nmds.cDayState = infoPtr->monthRange;
1652 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1654 nmds.stStart = infoPtr->todaysDate;
1655 nmds.stStart.wYear = infoPtr->curSel.wYear;
1656 nmds.stStart.wMonth = infoPtr->curSel.wMonth;
1657 nmds.stStart.wDay = 1;
1659 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1660 for(i = 0; i < infoPtr->monthRange; i++)
1661 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1663 Free(nmds.prgDayState);
1667 static void MONTHCAL_GoToNextMonth(MONTHCAL_INFO *infoPtr)
1669 SYSTEMTIME next = infoPtr->curSel;
1673 MONTHCAL_GetNextMonth(&next);
1675 if(!MONTHCAL_IsDateInValidRange(infoPtr, &next)) return;
1677 infoPtr->curSel = next;
1679 MONTHCAL_NotifyDayState(infoPtr);
1683 static void MONTHCAL_GoToPrevMonth(MONTHCAL_INFO *infoPtr)
1685 SYSTEMTIME prev = infoPtr->curSel;
1689 MONTHCAL_GetPrevMonth(&prev);
1691 if(!MONTHCAL_IsDateInValidRange(infoPtr, &prev)) return;
1693 infoPtr->curSel = prev;
1695 MONTHCAL_NotifyDayState(infoPtr);
1699 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1701 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1706 hMenu = CreatePopupMenu();
1707 if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1709 WARN("Can't load resource\n");
1710 strcpyW(buf, todayW);
1712 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1713 menupoint.x = (short)LOWORD(lParam);
1714 menupoint.y = (short)HIWORD(lParam);
1715 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1716 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
1717 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1719 infoPtr->curSel = infoPtr->todaysDate;
1720 infoPtr->minSel = infoPtr->todaysDate;
1721 infoPtr->maxSel = infoPtr->todaysDate;
1722 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1728 /* creates updown control and edit box */
1729 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr)
1731 infoPtr->hWndYearEdit =
1732 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
1733 infoPtr->titleyear.left + 3, infoPtr->titlebtnnext.top,
1734 infoPtr->titleyear.right - infoPtr->titleyear.left + 4,
1735 infoPtr->textHeight, infoPtr->hwndSelf,
1738 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
1740 infoPtr->hWndYearUpDown =
1741 CreateWindowExW(0, UPDOWN_CLASSW, 0,
1742 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
1743 infoPtr->titleyear.right + 7, infoPtr->titlebtnnext.top,
1744 18, infoPtr->textHeight, infoPtr->hwndSelf,
1747 /* attach edit box */
1748 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
1749 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
1750 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
1751 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->curSel.wYear);
1755 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1760 if (infoPtr->hWndYearUpDown)
1762 infoPtr->curSel.wYear = SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, 0);
1763 if(!DestroyWindow(infoPtr->hWndYearUpDown))
1765 FIXME("Can't destroy Updown Control\n");
1768 infoPtr->hWndYearUpDown = 0;
1770 if(!DestroyWindow(infoPtr->hWndYearEdit))
1772 FIXME("Can't destroy Updown Control\n");
1775 infoPtr->hWndYearEdit = 0;
1777 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1780 SetCapture(infoPtr->hwndSelf);
1782 ht.cbSize = sizeof(MCHITTESTINFO);
1783 ht.pt.x = (short)LOWORD(lParam);
1784 ht.pt.y = (short)HIWORD(lParam);
1786 hit = MONTHCAL_HitTest(infoPtr, &ht);
1788 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
1792 case MCHT_TITLEBTNNEXT:
1793 MONTHCAL_GoToNextMonth(infoPtr);
1794 infoPtr->status = MC_NEXTPRESSED;
1795 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1796 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1799 case MCHT_TITLEBTNPREV:
1800 MONTHCAL_GoToPrevMonth(infoPtr);
1801 infoPtr->status = MC_PREVPRESSED;
1802 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1803 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1806 case MCHT_TITLEMONTH:
1808 HMENU hMenu = CreatePopupMenu();
1813 for (i = 0; i < 12; i++)
1815 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
1816 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
1818 menupoint.x = ht.pt.x;
1819 menupoint.y = ht.pt.y;
1820 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1821 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
1822 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
1824 if ((i > 0) && (i < 13) && infoPtr->curSel.wMonth != i)
1826 infoPtr->curSel.wMonth = i;
1827 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1831 case MCHT_TITLEYEAR:
1833 MONTHCAL_EditYear(infoPtr);
1836 case MCHT_TODAYLINK:
1838 infoPtr->curSel = infoPtr->todaysDate;
1839 infoPtr->minSel = infoPtr->todaysDate;
1840 infoPtr->maxSel = infoPtr->todaysDate;
1841 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1843 MONTHCAL_NotifySelectionChange(infoPtr);
1844 MONTHCAL_NotifySelect(infoPtr);
1847 case MCHT_CALENDARDATENEXT:
1848 case MCHT_CALENDARDATEPREV:
1849 case MCHT_CALENDARDATE:
1851 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
1853 if(infoPtr->dwStyle & MCS_MULTISELECT)
1857 st[0] = st[1] = ht.st;
1859 /* clear selection range */
1860 MONTHCAL_SetSelRange(infoPtr, st);
1863 infoPtr->status = MC_SEL_LBUTDOWN;
1864 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
1874 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1882 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
1885 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
1886 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
1887 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
1889 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
1894 /* always send NM_RELEASEDCAPTURE notification */
1895 nmhdr.hwndFrom = infoPtr->hwndSelf;
1896 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1897 nmhdr.code = NM_RELEASEDCAPTURE;
1898 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
1900 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
1902 ht.cbSize = sizeof(MCHITTESTINFO);
1903 ht.pt.x = (short)LOWORD(lParam);
1904 ht.pt.y = (short)HIWORD(lParam);
1905 hit = MONTHCAL_HitTest(infoPtr, &ht);
1907 infoPtr->status = MC_SEL_LBUTUP;
1908 MONTHCAL_SetDayFocus(infoPtr, NULL);
1910 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
1912 SYSTEMTIME sel = infoPtr->curSel;
1914 if(!(infoPtr->dwStyle & MCS_MULTISELECT))
1918 st[0] = st[1] = ht.st;
1919 MONTHCAL_SetSelRange(infoPtr, st);
1920 /* will be invalidated here */
1921 MONTHCAL_SetCurSel(infoPtr, &st[0]);
1924 /* send MCN_SELCHANGE only if new date selected */
1925 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
1926 MONTHCAL_NotifySelectionChange(infoPtr);
1928 MONTHCAL_NotifySelect(infoPtr);
1936 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
1941 case MC_PREVNEXTMONTHTIMER:
1942 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToNextMonth(infoPtr);
1943 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToPrevMonth(infoPtr);
1944 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1946 case MC_TODAYUPDATETIMER:
1950 if(infoPtr->todaySet) return 0;
1953 MONTHCAL_UpdateToday(infoPtr, &st);
1955 /* notification sent anyway */
1956 MONTHCAL_NotifySelectionChange(infoPtr);
1961 ERR("got unknown timer %ld\n", id);
1970 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1973 SYSTEMTIME old_focused, st_ht;
1977 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
1979 ht.cbSize = sizeof(MCHITTESTINFO);
1980 ht.pt.x = (short)LOWORD(lParam);
1981 ht.pt.y = (short)HIWORD(lParam);
1983 hit = MONTHCAL_HitTest(infoPtr, &ht);
1985 /* not on the calendar date numbers? bail out */
1986 TRACE("hit:%x\n",hit);
1987 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
1989 MONTHCAL_SetDayFocus(infoPtr, NULL);
1994 old_focused = infoPtr->focusedSel;
1996 /* if pointer is over focused day still there's nothing to do */
1997 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
1999 MONTHCAL_CalcPosFromDay(infoPtr, ht.st.wDay, ht.st.wMonth, &r);
2001 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2004 MONTHCAL_GetSelRange(infoPtr, st);
2006 /* If we're still at the first selected date and range is empty, return.
2007 If range isn't empty we should change range to a single firstSel */
2008 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2009 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2011 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2013 st[0] = infoPtr->firstSel;
2014 /* we should overwrite timestamp here */
2015 MONTHCAL_CopyDate(&st_ht, &st[1]);
2017 /* bounds will be swapped here if needed */
2018 MONTHCAL_SetSelRange(infoPtr, st);
2025 /* FIXME: this should specify a rectangle containing only the days that changed
2026 using InvalidateRect */
2027 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2034 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2041 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2045 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2047 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2048 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2054 MONTHCAL_KillFocus(const MONTHCAL_INFO *infoPtr, HWND hFocusWnd)
2058 if (infoPtr->hwndNotify != hFocusWnd)
2059 ShowWindow(infoPtr->hwndSelf, SW_HIDE);
2061 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2068 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2072 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2077 /* sets the size information */
2078 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2080 static const WCHAR O0W[] = { '0','0',0 };
2081 HDC hdc = GetDC(infoPtr->hwndSelf);
2082 RECT *title=&infoPtr->title;
2083 RECT *prev=&infoPtr->titlebtnprev;
2084 RECT *next=&infoPtr->titlebtnnext;
2085 RECT *titlemonth=&infoPtr->titlemonth;
2086 RECT *titleyear=&infoPtr->titleyear;
2087 RECT *wdays=&infoPtr->wdays;
2088 RECT *weeknumrect=&infoPtr->weeknums;
2089 RECT *days=&infoPtr->days;
2090 RECT *todayrect=&infoPtr->todayrect;
2094 INT xdiv, dx, dy, i;
2098 GetClientRect(infoPtr->hwndSelf, &rcClient);
2100 currentFont = SelectObject(hdc, infoPtr->hFont);
2102 /* get the height and width of each day's text */
2103 GetTextMetricsW(hdc, &tm);
2104 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2106 /* find largest abbreviated day name for current locale */
2107 size.cx = sz.cx = 0;
2108 for (i = 0; i < 7; i++)
2110 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1,
2111 buff, countof(buff)))
2113 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2114 if (sz.cx > size.cx) size.cx = sz.cx;
2116 else /* locale independent fallback on failure */
2118 static const WCHAR SunW[] = { 'S','u','n',0 };
2120 GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2125 infoPtr->textWidth = size.cx + 2;
2127 /* recalculate the height and width increments and offsets */
2128 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2130 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2132 infoPtr->width_increment = size.cx * 2 + 4;
2133 infoPtr->height_increment = infoPtr->textHeight;
2135 /* calculate title area */
2137 title->bottom = 3 * infoPtr->height_increment / 2;
2139 title->right = infoPtr->width_increment * xdiv;
2141 /* set the dimensions of the next and previous buttons and center */
2142 /* the month text vertically */
2143 prev->top = next->top = title->top + 4;
2144 prev->bottom = next->bottom = title->bottom - 4;
2145 prev->left = title->left + 4;
2146 prev->right = prev->left + (title->bottom - title->top);
2147 next->right = title->right - 4;
2148 next->left = next->right - (title->bottom - title->top);
2150 /* titlemonth->left and right change based upon the current month */
2151 /* and are recalculated in refresh as the current month may change */
2152 /* without the control being resized */
2153 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2154 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2156 /* setup the dimensions of the rectangle we draw the names of the */
2157 /* days of the week in */
2158 weeknumrect->left = 0;
2160 if(infoPtr->dwStyle & MCS_WEEKNUMBERS)
2161 weeknumrect->right = prev->right;
2163 weeknumrect->right = weeknumrect->left;
2165 wdays->left = days->left = weeknumrect->right;
2166 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2167 wdays->top = title->bottom;
2168 wdays->bottom = wdays->top + infoPtr->height_increment;
2170 days->top = weeknumrect->top = wdays->bottom;
2171 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2173 todayrect->left = 0;
2174 todayrect->right = title->right;
2175 todayrect->top = days->bottom;
2176 todayrect->bottom = days->bottom + infoPtr->height_increment;
2178 /* offset all rectangles to center in client area */
2179 dx = (rcClient.right - title->right) / 2;
2180 dy = (rcClient.bottom - todayrect->bottom) / 2;
2182 /* if calendar doesn't fit client area show it at left/top bounds */
2183 if (title->left + dx < 0) dx = 0;
2184 if (title->top + dy < 0) dy = 0;
2186 if (dx != 0 || dy != 0)
2188 OffsetRect(title, dx, dy);
2189 OffsetRect(prev, dx, dy);
2190 OffsetRect(next, dx, dy);
2191 OffsetRect(titlemonth, dx, dy);
2192 OffsetRect(titleyear, dx, dy);
2193 OffsetRect(wdays, dx, dy);
2194 OffsetRect(weeknumrect, dx, dy);
2195 OffsetRect(days, dx, dy);
2196 OffsetRect(todayrect, dx, dy);
2199 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2200 infoPtr->width_increment,infoPtr->height_increment,
2201 wine_dbgstr_rect(&rcClient),
2202 wine_dbgstr_rect(title),
2203 wine_dbgstr_rect(wdays),
2204 wine_dbgstr_rect(days),
2205 wine_dbgstr_rect(todayrect));
2207 /* restore the originally selected font */
2208 SelectObject(hdc, currentFont);
2210 ReleaseDC(infoPtr->hwndSelf, hdc);
2213 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2215 TRACE("(width=%d, height=%d)\n", Width, Height);
2217 MONTHCAL_UpdateSize(infoPtr);
2219 /* invalidate client area and erase background */
2220 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2225 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2227 return (LRESULT)infoPtr->hFont;
2230 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2235 if (!hFont) return 0;
2237 hOldFont = infoPtr->hFont;
2238 infoPtr->hFont = hFont;
2240 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2241 lf.lfWeight = FW_BOLD;
2242 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2244 MONTHCAL_UpdateSize(infoPtr);
2247 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2249 return (LRESULT)hOldFont;
2252 /* update theme after a WM_THEMECHANGED message */
2253 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2255 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2256 CloseThemeData (theme);
2257 OpenThemeData (infoPtr->hwndSelf, themeClass);
2261 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2262 const STYLESTRUCT *lpss)
2264 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2265 wStyleType, lpss->styleOld, lpss->styleNew);
2267 if (wStyleType != GWL_STYLE) return 0;
2269 infoPtr->dwStyle = lpss->styleNew;
2271 /* make room for week numbers */
2272 if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2273 MONTHCAL_UpdateSize(infoPtr);
2278 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2281 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2282 wStyleType, lpss->styleOld, lpss->styleNew);
2284 /* block MCS_MULTISELECT change */
2285 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2287 if (lpss->styleOld & MCS_MULTISELECT)
2288 lpss->styleNew |= MCS_MULTISELECT;
2290 lpss->styleNew &= ~MCS_MULTISELECT;
2296 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2298 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2300 MONTHCAL_INFO *infoPtr;
2302 /* allocate memory for info structure */
2303 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2304 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2306 if(infoPtr == NULL) {
2307 ERR( "could not allocate info memory!\n");
2311 infoPtr->hwndSelf = hwnd;
2312 infoPtr->hwndNotify = lpcs->hwndParent;
2313 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2315 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
2317 /* initialize info structure */
2318 /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
2320 GetLocalTime(&infoPtr->todaysDate);
2321 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2323 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2324 infoPtr->monthRange = 3;
2325 infoPtr->monthdayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
2326 infoPtr->titlebk = comctl32_color.clrActiveCaption;
2327 infoPtr->titletxt = comctl32_color.clrWindow;
2328 infoPtr->monthbk = comctl32_color.clrWindow;
2329 infoPtr->trailingtxt = comctl32_color.clrGrayText;
2330 infoPtr->bk = comctl32_color.clrWindow;
2331 infoPtr->txt = comctl32_color.clrWindowText;
2333 infoPtr->minSel = infoPtr->todaysDate;
2334 infoPtr->maxSel = infoPtr->todaysDate;
2335 infoPtr->curSel = infoPtr->todaysDate;
2337 /* call MONTHCAL_UpdateSize to set all of the dimensions */
2338 /* of the control */
2339 MONTHCAL_UpdateSize(infoPtr);
2341 /* today auto update timer, to be freed only on control destruction */
2342 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2344 OpenThemeData (infoPtr->hwndSelf, themeClass);
2351 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2353 /* free month calendar info data */
2354 Free(infoPtr->monthdayState);
2355 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2357 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2364 static LRESULT WINAPI
2365 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2367 MONTHCAL_INFO *infoPtr;
2369 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2371 infoPtr = MONTHCAL_GetInfoPtr(hwnd);
2372 if (!infoPtr && (uMsg != WM_CREATE))
2373 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2377 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2380 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2382 case MCM_GETMAXSELCOUNT:
2383 return MONTHCAL_GetMaxSelCount(infoPtr);
2385 case MCM_SETMAXSELCOUNT:
2386 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2388 case MCM_GETSELRANGE:
2389 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2391 case MCM_SETSELRANGE:
2392 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2394 case MCM_GETMONTHRANGE:
2395 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2397 case MCM_SETDAYSTATE:
2398 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2400 case MCM_GETMINREQRECT:
2401 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2404 return MONTHCAL_GetColor(infoPtr, wParam);
2407 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2410 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2413 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2416 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2418 case MCM_GETFIRSTDAYOFWEEK:
2419 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2421 case MCM_SETFIRSTDAYOFWEEK:
2422 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2425 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2428 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2430 case MCM_GETMONTHDELTA:
2431 return MONTHCAL_GetMonthDelta(infoPtr);
2433 case MCM_SETMONTHDELTA:
2434 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2436 case MCM_GETMAXTODAYWIDTH:
2437 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2440 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2443 return MONTHCAL_KillFocus(infoPtr, (HWND)wParam);
2446 return MONTHCAL_RButtonUp(infoPtr, lParam);
2448 case WM_LBUTTONDOWN:
2449 return MONTHCAL_LButtonDown(infoPtr, lParam);
2452 return MONTHCAL_MouseMove(infoPtr, lParam);
2455 return MONTHCAL_LButtonUp(infoPtr, lParam);
2457 case WM_PRINTCLIENT:
2459 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2462 return MONTHCAL_SetFocus(infoPtr);
2465 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2468 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2471 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2474 return MONTHCAL_GetFont(infoPtr);
2477 return MONTHCAL_Timer(infoPtr, wParam);
2479 case WM_THEMECHANGED:
2480 return theme_changed (infoPtr);
2483 return MONTHCAL_Destroy(infoPtr);
2485 case WM_SYSCOLORCHANGE:
2486 COMCTL32_RefreshSysColors();
2489 case WM_STYLECHANGED:
2490 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2492 case WM_STYLECHANGING:
2493 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2496 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2497 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2498 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2504 MONTHCAL_Register(void)
2508 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2509 wndClass.style = CS_GLOBALCLASS;
2510 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2511 wndClass.cbClsExtra = 0;
2512 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2513 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2514 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2515 wndClass.lpszClassName = MONTHCAL_CLASSW;
2517 RegisterClassW(&wndClass);
2522 MONTHCAL_Unregister(void)
2524 UnregisterClassW(MONTHCAL_CLASSW, NULL);