comctl32/monthcal: Prepare drawing helpers for multiple calendars.
[wine/multimedia.git] / dlls / comctl32 / monthcal.c
blobcfd09de63249973f8378a91f681fb8a46ab1f5ef
1 /*
2 * Month calendar control
4 * Copyright 1998, 1999 Eric Kohl (ekohl@abo.rhein-zeitung.de)
5 * Copyright 1999 Alex Priem (alexp@sci.kun.nl)
6 * Copyright 1999 Chris Morgan <cmorgan@wpi.edu> and
7 * James Abbatiello <abbeyj@wpi.edu>
8 * Copyright 2000 Uwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de>
9 * Copyright 2009 Nikolay Sivov
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * NOTE
27 * This code was audited for completeness against the documented features
28 * of Comctl32.dll version 6.0 on Oct. 20, 2004, by Dimitrie O. Paun.
30 * Unless otherwise noted, we believe this code to be complete, as per
31 * the specification mentioned above.
32 * If you discover missing features, or bugs, please note them below.
34 * TODO:
35 * -- MCM_[GS]ETUNICODEFORMAT
36 * -- MONTHCAL_GetMonthRange
37 * -- handle resources better (doesn't work now);
38 * -- take care of internationalization.
39 * -- keyboard handling.
40 * -- search for FIXME
43 #include <math.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
49 #include "windef.h"
50 #include "winbase.h"
51 #include "wingdi.h"
52 #include "winuser.h"
53 #include "winnls.h"
54 #include "commctrl.h"
55 #include "comctl32.h"
56 #include "uxtheme.h"
57 #include "tmschema.h"
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 350 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)
80 /* single calendar data */
81 typedef struct _CALENDAR_INFO
83 RECT title; /* rect for the header above the calendar */
84 RECT titlemonth; /* the 'month name' text in the header */
85 RECT titleyear; /* the 'year number' text in the header */
86 RECT wdays; /* week days at top */
87 RECT days; /* calendar area */
88 RECT weeknums; /* week numbers at left side */
89 } CALENDAR_INFO;
91 typedef struct
93 HWND hwndSelf;
94 DWORD dwStyle; /* cached GWL_STYLE */
95 COLORREF bk;
96 COLORREF txt;
97 COLORREF titlebk;
98 COLORREF titletxt;
99 COLORREF monthbk;
100 COLORREF trailingtxt;
101 HFONT hFont;
102 HFONT hBoldFont;
103 int textHeight;
104 int textWidth;
105 int height_increment;
106 int width_increment;
107 INT delta; /* scroll rate; # of months that the */
108 /* control moves when user clicks a scroll button */
109 int visible; /* # of months visible */
110 int firstDay; /* Start month calendar with firstDay's day,
111 stored in SYSTEMTIME format */
112 BOOL firstDaySet; /* first week day differs from locale defined */
114 BOOL isUnicode; /* value set with MCM_SETUNICODE format */
116 int monthRange;
117 MONTHDAYSTATE *monthdayState;
118 SYSTEMTIME todaysDate;
119 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
120 int status; /* See MC_SEL flags */
121 SYSTEMTIME firstSel; /* first selected day */
122 INT maxSelCount;
123 SYSTEMTIME minSel;
124 SYSTEMTIME maxSel;
125 SYSTEMTIME curSel; /* contains currently selected year, month and day */
126 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
127 DWORD rangeValid;
128 SYSTEMTIME minDate;
129 SYSTEMTIME maxDate;
131 RECT titlebtnnext; /* the `next month' button in the header */
132 RECT titlebtnprev; /* the `prev month' button in the header */
133 RECT todayrect; /* `today: xx/xx/xx' text rect */
134 HWND hwndNotify; /* Window to receive the notifications */
135 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
136 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
137 WNDPROC EditWndProc; /* original Edit window procedure */
139 CALENDAR_INFO *calendars;
140 INT cal_num;
141 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
143 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
145 /* empty SYSTEMTIME const */
146 static const SYSTEMTIME st_null;
147 /* valid date limits */
148 static const SYSTEMTIME max_allowed_date = { .wYear = 9999, .wMonth = 12, .wDay = 31 };
149 static const SYSTEMTIME min_allowed_date = { .wYear = 1752, .wMonth = 9, .wDay = 14 };
152 #define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0))
154 /* helper functions */
156 /* send a single MCN_SELCHANGE notification */
157 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
159 NMSELCHANGE nmsc;
161 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
162 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
163 nmsc.nmhdr.code = MCN_SELCHANGE;
164 nmsc.stSelStart = infoPtr->minSel;
165 nmsc.stSelEnd = infoPtr->maxSel;
166 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
169 /* send a single MCN_SELECT notification */
170 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
172 NMSELCHANGE nmsc;
174 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
175 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
176 nmsc.nmhdr.code = MCN_SELECT;
177 nmsc.stSelStart = infoPtr->minSel;
178 nmsc.stSelEnd = infoPtr->maxSel;
180 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
183 /* returns the number of days in any given month, checking for leap days */
184 /* january is 1, december is 12 */
185 int MONTHCAL_MonthLength(int month, int year)
187 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
188 /* Wrap around, this eases handling. Getting length only we shouldn't care
189 about year change here cause January and December have
190 the same day quantity */
191 if(month == 0)
192 month = 12;
193 else if(month == 13)
194 month = 1;
196 /* special case for calendar transition year */
197 if(month == min_allowed_date.wMonth && year == min_allowed_date.wYear) return 19;
199 /* if we have a leap year add 1 day to February */
200 /* a leap year is a year either divisible by 400 */
201 /* or divisible by 4 and not by 100 */
202 if(month == 2) { /* February */
203 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
204 (year%4 == 0)) ? 1 : 0);
206 else {
207 return mdays[month - 1];
211 /* compares timestamps using date part only */
212 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
214 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
215 (first->wDay == second->wDay);
218 /* make sure that date fields are valid */
219 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
221 if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
222 if(time->wDayOfWeek > 6) return FALSE;
223 if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear))
224 return FALSE;
226 return TRUE;
229 /* Copies timestamp part only.
231 * PARAMETERS
233 * [I] from : source date
234 * [O] to : dest date
236 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
238 to->wHour = from->wHour;
239 to->wMinute = from->wMinute;
240 to->wSecond = from->wSecond;
243 /* Copies date part only.
245 * PARAMETERS
247 * [I] from : source date
248 * [O] to : dest date
250 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
252 to->wYear = from->wYear;
253 to->wMonth = from->wMonth;
254 to->wDay = from->wDay;
255 to->wDayOfWeek = from->wDayOfWeek;
258 /* Compares two dates in SYSTEMTIME format
260 * PARAMETERS
262 * [I] first : pointer to valid first date data to compare
263 * [I] second : pointer to valid second date data to compare
265 * RETURN VALUE
267 * -1 : first < second
268 * 0 : first == second
269 * 1 : first > second
271 * Note that no date validation performed, alreadt validated values expected.
273 static LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
275 FILETIME ft_first, ft_second;
277 SystemTimeToFileTime(first, &ft_first);
278 SystemTimeToFileTime(second, &ft_second);
280 return CompareFileTime(&ft_first, &ft_second);
283 static LONG MONTHCAL_CompareMonths(const SYSTEMTIME *first, const SYSTEMTIME *second)
285 SYSTEMTIME st_first, st_second;
287 st_first = st_second = st_null;
288 MONTHCAL_CopyDate(first, &st_first);
289 MONTHCAL_CopyDate(second, &st_second);
290 st_first.wDay = st_second.wDay = 1;
292 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
295 static LONG MONTHCAL_CompareDate(const SYSTEMTIME *first, const SYSTEMTIME *second)
297 SYSTEMTIME st_first, st_second;
299 st_first = st_second = st_null;
300 MONTHCAL_CopyDate(first, &st_first);
301 MONTHCAL_CopyDate(second, &st_second);
303 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
306 /* Checks largest possible date range and configured one
308 * PARAMETERS
310 * [I] infoPtr : valid pointer to control data
311 * [I] date : pointer to valid date data to check
312 * [I] fix : make date fit valid range
314 * RETURN VALUE
316 * TRUE - date whithin largest and configured range
317 * FALSE - date is outside largest or configured range
319 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr,
320 SYSTEMTIME *date, BOOL fix)
322 const SYSTEMTIME *fix_st = NULL;
324 if(MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) {
325 fix_st = &max_allowed_date;
327 else if(MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1) {
328 fix_st = &min_allowed_date;
330 else if(infoPtr->rangeValid & GDTR_MAX) {
331 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
332 fix_st = &infoPtr->maxDate;
335 else if(infoPtr->rangeValid & GDTR_MIN) {
336 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
337 fix_st = &infoPtr->minDate;
341 if (fix && fix_st) {
342 date->wYear = fix_st->wYear;
343 date->wMonth = fix_st->wMonth;
346 return fix_st ? FALSE : TRUE;
349 /* Checks passed range width with configured maximum selection count
351 * PARAMETERS
353 * [I] infoPtr : valid pointer to control data
354 * [I] range0 : pointer to valid date data (requested bound)
355 * [I] range1 : pointer to valid date data (primary bound)
356 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
358 * Adjust value computed basing on primary bound and current maximum selection
359 * count. For simple range check (without adjusted value required) (range0, range1)
360 * relation means nothing.
362 * RETURN VALUE
364 * TRUE - range is shorter or equal to maximum
365 * FALSE - range is larger than maximum
367 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
368 const SYSTEMTIME *range0,
369 const SYSTEMTIME *range1,
370 SYSTEMTIME *adjust)
372 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
373 FILETIME ft_range0, ft_range1;
374 LONG cmp;
376 SystemTimeToFileTime(range0, &ft_range0);
377 SystemTimeToFileTime(range1, &ft_range1);
379 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
380 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
381 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
382 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
384 cmp = CompareFileTime(&ft_range0, &ft_range1);
386 if(cmp == 1)
387 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
388 else
389 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
391 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
393 if(adjust) {
394 if(cmp == 1)
395 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
396 else
397 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
399 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
400 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
401 FileTimeToSystemTime(&ft_range0, adjust);
404 return FALSE;
406 else return TRUE;
409 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
410 Milliseconds are intentionally not validated. */
411 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
413 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
414 return FALSE;
415 else
416 return TRUE;
419 /* Note:Depending on DST, this may be offset by a day.
420 Need to find out if we're on a DST place & adjust the clock accordingly.
421 Above function assumes we have a valid data.
422 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
423 0 = Sunday.
426 /* Returns the day in the week
428 * PARAMETERS
429 * [i] date : input date
430 * [I] inplace : set calculated value back to date structure
432 * RETURN VALUE
433 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
435 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
437 SYSTEMTIME st = st_null;
438 FILETIME ft;
440 MONTHCAL_CopyDate(date, &st);
442 SystemTimeToFileTime(&st, &ft);
443 FileTimeToSystemTime(&ft, &st);
445 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
447 return st.wDayOfWeek;
450 /* properly updates date to point on next month */
451 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
453 if(++date->wMonth > 12)
455 date->wMonth = 1;
456 date->wYear++;
458 MONTHCAL_CalculateDayOfWeek(date, TRUE);
461 /* properly updates date to point on prev month */
462 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
464 if(--date->wMonth < 1)
466 date->wMonth = 12;
467 date->wYear--;
469 MONTHCAL_CalculateDayOfWeek(date, TRUE);
472 /* Returns full date for a first currently visible day */
473 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
475 SYSTEMTIME st_first = infoPtr->curSel;
476 int firstDay;
478 st_first.wDay = 1;
479 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
481 *date = infoPtr->curSel;
482 MONTHCAL_GetPrevMonth(date);
484 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
485 (infoPtr->firstDay - firstDay) % 7 + 1;
487 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
488 date->wDay -= 7;
490 /* fix day of week */
491 MONTHCAL_CalculateDayOfWeek(date, TRUE);
494 /* Returns full date for a last currently visible day */
495 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
497 SYSTEMTIME st;
499 *date = infoPtr->curSel;
500 MONTHCAL_GetNextMonth(date);
502 MONTHCAL_GetMinDate(infoPtr, &st);
503 /* Use month length to get max day. 42 means max day count in calendar area */
504 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
505 MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear);
507 /* fix day of week */
508 MONTHCAL_CalculateDayOfWeek(date, TRUE);
511 /* From a given point, calculate the row (weekpos), column(daypos)
512 and day in the calendar. day== 0 mean the last day of tha last month
514 static int MONTHCAL_CalcDayFromPos(const MONTHCAL_INFO *infoPtr, int x, int y,
515 int *daypos,int *weekpos)
517 int retval, firstDay;
518 RECT rcClient;
519 SYSTEMTIME st = infoPtr->curSel;
521 GetClientRect(infoPtr->hwndSelf, &rcClient);
523 /* if the point is outside the x bounds of the window put
524 it at the boundary */
525 if (x > rcClient.right)
526 x = rcClient.right;
529 *daypos = (x - infoPtr->calendars[0].days.left ) / infoPtr->width_increment;
530 *weekpos = (y - infoPtr->calendars[0].days.top ) / infoPtr->height_increment;
532 st.wDay = 1;
533 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
534 retval = *daypos + (7 * *weekpos) - firstDay;
535 return retval;
538 /* Sets the RECT struct r to the rectangle around the date
540 * PARAMETERS
542 * [I] infoPtr : pointer to control data
543 * [I] date : date value
544 * [O] x : day column (zero based)
545 * [O] y : week column (zero based)
547 static void MONTHCAL_CalcDayXY(const MONTHCAL_INFO *infoPtr,
548 const SYSTEMTIME *date, int *x, int *y)
550 SYSTEMTIME st = infoPtr->curSel;
551 LONG cmp;
552 int first;
554 st.wDay = 1;
555 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
557 cmp = MONTHCAL_CompareMonths(date, &infoPtr->curSel);
559 /* previous month */
560 if(cmp == -1) {
561 *x = (first - MONTHCAL_MonthLength(date->wMonth, infoPtr->curSel.wYear) + date->wDay) % 7;
562 *y = 0;
563 return;
566 /* next month calculation is same as for current,
567 just add current month length */
568 if(cmp == 1) {
569 first += MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear);
572 *x = (date->wDay + first) % 7;
573 *y = (date->wDay + first - *x) / 7;
577 /* x: column(day), y: row(week) */
578 static inline void MONTHCAL_CalcDayRect(const MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
580 r->left = infoPtr->calendars[0].days.left + x * infoPtr->width_increment;
581 r->right = r->left + infoPtr->width_increment;
582 r->top = infoPtr->calendars[0].days.top + y * infoPtr->height_increment;
583 r->bottom = r->top + infoPtr->textHeight;
587 /* Sets the RECT struct r to the rectangle around the date */
588 static inline void MONTHCAL_CalcPosFromDay(const MONTHCAL_INFO *infoPtr,
589 const SYSTEMTIME *date, RECT *r)
591 int x, y;
593 MONTHCAL_CalcDayXY(infoPtr, date, &x, &y);
594 MONTHCAL_CalcDayRect(infoPtr, r, x, y);
597 /* Focused day helper:
599 - set focused date to given value;
600 - reset to zero value if NULL passed;
601 - invalidate previous and new day rectangle only if needed.
603 Returns TRUE if focused day changed, FALSE otherwise.
605 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
607 RECT r;
609 if(st)
611 /* there's nothing to do if it's the same date,
612 mouse move within same date rectangle case */
613 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
615 /* invalidate old focused day */
616 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
617 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
619 infoPtr->focusedSel = *st;
622 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
624 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
625 infoPtr->focusedSel = st_null;
627 /* on set invalidates new day, on reset clears previous focused day */
628 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
630 return TRUE;
633 /* Draw today day mark rectangle
635 * [I] hdc : context to draw in
636 * [I] day : day to mark with rectangle
639 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
640 const SYSTEMTIME *date)
642 HPEN hRedPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
643 HPEN hOldPen2 = SelectObject(hdc, hRedPen);
644 HBRUSH hOldBrush;
645 RECT day_rect;
647 MONTHCAL_CalcPosFromDay(infoPtr, date, &day_rect);
649 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
650 Rectangle(hdc, day_rect.left, day_rect.top, day_rect.right, day_rect.bottom);
652 SelectObject(hdc, hOldBrush);
653 DeleteObject(hRedPen);
654 SelectObject(hdc, hOldPen2);
657 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
658 int bold, const PAINTSTRUCT *ps)
660 static const WCHAR fmtW[] = { '%','d',0 };
661 WCHAR buf[10];
662 RECT r, r_temp;
663 static BOOL bold_selected;
664 BOOL selected_day = FALSE;
665 HBRUSH hbr;
666 COLORREF oldCol = 0;
667 COLORREF oldBk = 0;
669 /* No need to check styles: when selection is not valid, it is set to zero.
670 * 1<day<31, so everything is OK.
673 MONTHCAL_CalcPosFromDay(infoPtr, st, &r);
674 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
676 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
677 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0)) {
679 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
680 TRACE("%s\n", wine_dbgstr_rect(&r));
681 oldCol = SetTextColor(hdc, infoPtr->monthbk);
682 oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
683 hbr = GetSysColorBrush(COLOR_HIGHLIGHT);
684 FillRect(hdc, &r, hbr);
686 selected_day = TRUE;
689 if(bold && !bold_selected) {
690 SelectObject(hdc, infoPtr->hBoldFont);
691 bold_selected = TRUE;
693 if(!bold && bold_selected) {
694 SelectObject(hdc, infoPtr->hFont);
695 bold_selected = FALSE;
698 SetBkMode(hdc,TRANSPARENT);
699 wsprintfW(buf, fmtW, st->wDay);
700 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
702 if(selected_day) {
703 SetTextColor(hdc, oldCol);
704 SetBkColor(hdc, oldBk);
709 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, BOOL btnNext)
711 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
712 RECT *r = btnNext ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
713 BOOL pressed = btnNext ? (infoPtr->status & MC_NEXTPRESSED) :
714 (infoPtr->status & MC_PREVPRESSED);
715 if (theme)
717 static const int states[] = {
718 /* Prev button */
719 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
720 /* Next button */
721 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
723 int stateNum = btnNext ? 3 : 0;
724 if (pressed)
725 stateNum += 1;
726 else
728 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
730 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
732 else
734 int style = btnNext ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
735 if (pressed)
736 style |= DFCS_PUSHED;
737 else
739 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
742 DrawFrameControl(hdc, r, DFC_SCROLL, style);
745 /* paint a title with buttons and month/year string */
746 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
748 static const WCHAR fmt_monthW[] = { '%','s',' ','%','l','d',0 };
749 RECT *title = &infoPtr->calendars[calIdx].title;
750 WCHAR buf_month[80], buf_fmt[80];
751 HBRUSH hbr;
752 SIZE sz;
754 /* fill header box */
755 hbr = CreateSolidBrush(infoPtr->titlebk);
756 FillRect(hdc, title, hbr);
757 DeleteObject(hbr);
759 /* month/year string */
760 SetBkColor(hdc, infoPtr->titlebk);
761 SetTextColor(hdc, infoPtr->titletxt);
762 SelectObject(hdc, infoPtr->hBoldFont);
764 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+infoPtr->curSel.wMonth-1,
765 buf_month, countof(buf_month));
767 wsprintfW(buf_fmt, fmt_monthW, buf_month, infoPtr->curSel.wYear);
768 DrawTextW(hdc, buf_fmt, strlenW(buf_fmt), title,
769 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
771 /* update title rectangles with current month - used while testing hits */
772 GetTextExtentPoint32W(hdc, buf_fmt, strlenW(buf_fmt), &sz);
773 infoPtr->calendars[calIdx].titlemonth.left = title->right / 2 + title->left / 2 - sz.cx / 2;
774 infoPtr->calendars[calIdx].titleyear.right = title->right / 2 + title->left / 2 + sz.cx / 2;
776 GetTextExtentPoint32W(hdc, buf_month, strlenW(buf_month), &sz);
777 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
778 infoPtr->calendars[calIdx].titleyear.left = infoPtr->calendars[calIdx].titlemonth.right;
781 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
783 static const WCHAR fmt_weekW[] = { '%','d',0 };
784 INT mindays, weeknum, weeknum1, startofprescal;
785 SYSTEMTIME st = infoPtr->curSel;
786 RECT r;
787 WCHAR buf[80];
788 INT i, prev_month;
790 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
792 MONTHCAL_GetMinDate(infoPtr, &st);
793 startofprescal = st.wDay;
794 st = infoPtr->curSel;
796 prev_month = infoPtr->curSel.wMonth - 1;
797 if(prev_month == 0) prev_month = 12;
800 Rules what week to call the first week of a new year:
801 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
802 The week containing Jan 1 is the first week of year
803 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
804 First week of year must contain 4 days of the new year
805 LOCALE_IFIRSTWEEKOFYEAR == 1 (what contries?)
806 The first week of the year must contain only days of the new year
808 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
809 weeknum = atoiW(buf);
810 switch (weeknum)
812 case 1: mindays = 6;
813 break;
814 case 2: mindays = 3;
815 break;
816 case 0: mindays = 0;
817 break;
818 default:
819 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
820 mindays = 0;
823 if (infoPtr->curSel.wMonth == 1)
825 /* calculate all those exceptions for january */
826 st.wDay = st.wMonth = 1;
827 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
828 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
829 weeknum = 1;
830 else
832 weeknum = 0;
833 for(i = 0; i < 11; i++)
834 weeknum += MONTHCAL_MonthLength(i+1, infoPtr->curSel.wYear - 1);
836 weeknum += startofprescal + 7;
837 weeknum /= 7;
838 st.wYear -= 1;
839 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
840 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
843 else
845 weeknum = 0;
846 for(i = 0; i < prev_month - 1; i++)
847 weeknum += MONTHCAL_MonthLength(i+1, infoPtr->curSel.wYear);
849 weeknum += startofprescal + 7;
850 weeknum /= 7;
851 st.wDay = st.wMonth = 1;
852 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
853 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
856 r = infoPtr->calendars[calIdx].weeknums;
857 r.bottom = r.top + infoPtr->height_increment;
859 for(i = 0; i < 6; i++) {
860 if((i == 0) && (weeknum > 50))
862 wsprintfW(buf, fmt_weekW, weeknum);
863 weeknum = 0;
865 else if((i == 5) && (weeknum > 47))
867 wsprintfW(buf, fmt_weekW, 1);
869 else
870 wsprintfW(buf, fmt_weekW, weeknum + i);
872 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
873 OffsetRect(&r, 0, infoPtr->height_increment);
876 /* line separator for week numbers column */
877 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
878 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
881 /* bottom today date */
882 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
884 if(!(infoPtr->dwStyle & MCS_NOTODAY)) {
885 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
886 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
887 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
888 RECT rtoday;
890 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
891 SYSTEMTIME fake_st;
893 MONTHCAL_GetMaxDate(infoPtr, &fake_st);
894 /* this is always safe cause next month will never fully fit calendar */
895 fake_st.wDay += 1;
896 MONTHCAL_CircleDay(infoPtr, hdc, &fake_st);
898 if (!LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, countof(buf_todayW)))
900 WARN("Can't load resource\n");
901 strcpyW(buf_todayW, todayW);
903 MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
904 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
905 buf_dateW, countof(buf_dateW));
906 SelectObject(hdc, infoPtr->hBoldFont);
908 wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
909 DrawTextW(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
910 DrawTextW(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
912 SelectObject(hdc, infoPtr->hFont);
916 /* today mark + focus */
917 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
919 if((infoPtr->curSel.wMonth == infoPtr->todaysDate.wMonth) &&
920 (infoPtr->curSel.wYear == infoPtr->todaysDate.wYear) &&
921 !(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
923 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
926 if(!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
928 RECT r;
929 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
930 DrawFocusRect(hdc, &r);
934 /* paint a calendar area */
935 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
937 INT prev_month, i, j;
938 WCHAR buf[80];
939 HBRUSH hbr;
940 RECT r, fill_bk_rect;
941 int mask;
942 SYSTEMTIME st;
944 /* fill whole days area - from week days area to today note rectangle */
945 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
946 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
947 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
949 hbr = CreateSolidBrush(infoPtr->monthbk);
950 FillRect(hdc, &fill_bk_rect, hbr);
951 DeleteObject(hbr);
953 /* draw line under day abbreviations */
954 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
955 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
956 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
957 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
959 prev_month = infoPtr->curSel.wMonth - 1;
960 if (prev_month == 0) prev_month = 12;
962 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
963 infoPtr->calendars[calIdx].weeknums.right;
965 /* 1. draw day abbreviations */
966 SelectObject(hdc, infoPtr->hFont);
967 SetBkColor(hdc, infoPtr->monthbk);
968 SetTextColor(hdc, infoPtr->trailingtxt);
969 /* rectangle to draw a single day abbreviation within */
970 r = infoPtr->calendars[calIdx].wdays;
971 r.right = r.left + infoPtr->width_increment;
973 i = infoPtr->firstDay;
974 for(j = 0; j < 7; j++) {
975 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
976 DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
977 OffsetRect(&r, infoPtr->width_increment, 0);
980 /* 2. previous and next months */
981 if (!(infoPtr->dwStyle & MCS_NOTRAILINGDATES) && (calIdx == 0 || calIdx == infoPtr->cal_num - 1))
983 SYSTEMTIME st_max;
985 SetTextColor(hdc, infoPtr->trailingtxt);
987 /* draw prev month */
988 if (calIdx == 0)
990 MONTHCAL_GetMinDate(infoPtr, &st);
991 mask = 1 << (st.wDay-1);
993 while(st.wDay <= MONTHCAL_MonthLength(prev_month, infoPtr->curSel.wYear))
995 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[0] & mask, ps);
996 mask <<= 1;
997 st.wDay++;
1001 /* draw next month */
1002 if (calIdx == infoPtr->cal_num - 1)
1004 st = infoPtr->curSel;
1005 st.wDay = 1;
1006 MONTHCAL_GetNextMonth(&st);
1007 MONTHCAL_GetMaxDate(infoPtr, &st_max);
1008 mask = 1;
1010 while(st.wDay <= st_max.wDay)
1012 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[2] & mask, ps);
1013 mask <<= 1;
1014 st.wDay++;
1019 /* 3. current month */
1020 SetTextColor(hdc, infoPtr->txt);
1021 st = infoPtr->curSel;
1022 st.wDay = 1;
1023 mask = 1;
1024 while(st.wDay <= MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear)) {
1025 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[1] & mask, ps);
1026 mask <<= 1;
1027 st.wDay++;
1031 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1033 COLORREF old_text_clr, old_bk_clr;
1034 HFONT old_font;
1035 INT i;
1037 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1038 old_bk_clr = GetBkColor(hdc);
1039 old_font = GetCurrentObject(hdc, OBJ_FONT);
1041 for (i = 0; i < infoPtr->cal_num; i++)
1043 RECT *title = &infoPtr->calendars[i].title;
1044 RECT r;
1046 /* draw title, redraw all its elements */
1047 if (IntersectRect(&r, &(ps->rcPaint), title))
1048 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1050 /* draw calendar area */
1051 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1052 if (IntersectRect(&r, &(ps->rcPaint), &r))
1053 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1055 /* week numbers */
1056 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1059 /* focus and today rectangle */
1060 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1062 /* today at the bottom left */
1063 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1065 /* navigation buttons */
1066 MONTHCAL_PaintButton(infoPtr, hdc, FALSE);
1067 MONTHCAL_PaintButton(infoPtr, hdc, TRUE);
1069 /* restore context */
1070 SetBkColor(hdc, old_bk_clr);
1071 SelectObject(hdc, old_font);
1072 SetTextColor(hdc, old_text_clr);
1075 static LRESULT
1076 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, LPRECT lpRect)
1078 TRACE("rect %p\n", lpRect);
1080 if(!lpRect) return FALSE;
1082 lpRect->left = infoPtr->calendars[0].title.left;
1083 lpRect->top = infoPtr->calendars[0].title.top;
1084 lpRect->right = infoPtr->calendars[0].title.right;
1085 lpRect->bottom = infoPtr->todayrect.bottom;
1087 AdjustWindowRect(lpRect, infoPtr->dwStyle, FALSE);
1089 /* minimal rectangle is zero based */
1090 OffsetRect(lpRect, -lpRect->left, -lpRect->top);
1092 TRACE("%s\n", wine_dbgstr_rect(lpRect));
1094 return TRUE;
1098 static LRESULT
1099 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, INT index)
1101 TRACE("\n");
1103 switch(index) {
1104 case MCSC_BACKGROUND:
1105 return infoPtr->bk;
1106 case MCSC_TEXT:
1107 return infoPtr->txt;
1108 case MCSC_TITLEBK:
1109 return infoPtr->titlebk;
1110 case MCSC_TITLETEXT:
1111 return infoPtr->titletxt;
1112 case MCSC_MONTHBK:
1113 return infoPtr->monthbk;
1114 case MCSC_TRAILINGTEXT:
1115 return infoPtr->trailingtxt;
1118 return -1;
1122 static LRESULT
1123 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, INT index, COLORREF color)
1125 COLORREF prev = -1;
1127 TRACE("%d: color %08x\n", index, color);
1129 switch(index) {
1130 case MCSC_BACKGROUND:
1131 prev = infoPtr->bk;
1132 infoPtr->bk = color;
1133 break;
1134 case MCSC_TEXT:
1135 prev = infoPtr->txt;
1136 infoPtr->txt = color;
1137 break;
1138 case MCSC_TITLEBK:
1139 prev = infoPtr->titlebk;
1140 infoPtr->titlebk = color;
1141 break;
1142 case MCSC_TITLETEXT:
1143 prev=infoPtr->titletxt;
1144 infoPtr->titletxt = color;
1145 break;
1146 case MCSC_MONTHBK:
1147 prev = infoPtr->monthbk;
1148 infoPtr->monthbk = color;
1149 break;
1150 case MCSC_TRAILINGTEXT:
1151 prev = infoPtr->trailingtxt;
1152 infoPtr->trailingtxt = color;
1153 break;
1156 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND ? TRUE : FALSE);
1157 return prev;
1161 static LRESULT
1162 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1164 TRACE("\n");
1166 if(infoPtr->delta)
1167 return infoPtr->delta;
1168 else
1169 return infoPtr->visible;
1173 static LRESULT
1174 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1176 INT prev = infoPtr->delta;
1178 TRACE("delta %d\n", delta);
1180 infoPtr->delta = delta;
1181 return prev;
1185 static inline LRESULT
1186 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1188 int day;
1190 /* convert from SYSTEMTIME to locale format */
1191 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1193 return MAKELONG(day, infoPtr->firstDaySet);
1197 /* Sets the first day of the week that will appear in the control
1200 * PARAMETERS:
1201 * [I] infoPtr : valid pointer to control data
1202 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1205 * RETURN VALUE:
1206 * Low word contains previous first day,
1207 * high word indicates was first day forced with this message before or is
1208 * locale difined (TRUE - was forced, FALSE - wasn't).
1210 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1211 * FIXME: we need more error checking here
1213 static LRESULT
1214 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1216 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1217 int new_day;
1219 TRACE("%d\n", day);
1221 if(day == -1)
1223 WCHAR buf[80];
1225 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1226 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1228 new_day = atoiW(buf);
1230 infoPtr->firstDaySet = FALSE;
1232 else if(day >= 7)
1234 new_day = 6; /* max first day allowed */
1235 infoPtr->firstDaySet = TRUE;
1237 else
1239 /* Native behaviour for that case is broken: invalid date number >31
1240 got displayed at (0,0) position, current month starts always from
1241 (1,0) position. Should be implemnted here as well. */
1242 if (day < -1)
1243 FIXME("No bug compatibility for day=%d\n", day);
1245 new_day = day;
1246 infoPtr->firstDaySet = TRUE;
1249 /* convert from locale to SYSTEMTIME format */
1250 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1252 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1254 return prev;
1258 static LRESULT
1259 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
1261 TRACE("\n");
1263 if(st)
1265 switch (flag) {
1266 case GMR_VISIBLE:
1268 /*FIXME: currently multicalendar feature isn't implemented, so entirely
1269 visible month is current */
1270 st[0] = st[1] = infoPtr->curSel;
1272 if (infoPtr->curSel.wMonth == min_allowed_date.wMonth &&
1273 infoPtr->curSel.wYear == min_allowed_date.wYear)
1275 st[0].wDay = min_allowed_date.wDay;
1277 else
1278 st[0].wDay = 1;
1279 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
1281 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
1282 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
1283 /* a single current month used */
1284 return 1;
1286 case GMR_DAYSTATE:
1288 /*FIXME: currently multicalendar feature isn't implemented,
1289 min date from previous month and max date from next one returned */
1290 MONTHCAL_GetMinDate(infoPtr, &st[0]);
1291 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
1292 break;
1294 default:
1295 WARN("Unknown flag value, got %d\n", flag);
1299 return infoPtr->monthRange;
1303 static LRESULT
1304 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1306 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1310 static LRESULT
1311 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1313 FILETIME ft_min, ft_max;
1315 TRACE("%x %p\n", limits, range);
1317 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1318 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1319 return FALSE;
1321 if (limits & GDTR_MIN)
1323 if (!MONTHCAL_ValidateTime(&range[0]))
1324 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1326 infoPtr->minDate = range[0];
1327 infoPtr->rangeValid |= GDTR_MIN;
1329 if (limits & GDTR_MAX)
1331 if (!MONTHCAL_ValidateTime(&range[1]))
1332 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1334 infoPtr->maxDate = range[1];
1335 infoPtr->rangeValid |= GDTR_MAX;
1338 /* Only one limit set - we are done */
1339 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1340 return TRUE;
1342 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1343 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1345 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1347 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1349 /* Native swaps limits only when both limits are being set. */
1350 SYSTEMTIME st_tmp = infoPtr->minDate;
1351 infoPtr->minDate = infoPtr->maxDate;
1352 infoPtr->maxDate = st_tmp;
1354 else
1356 /* reset the other limit */
1357 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1358 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1359 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1363 return TRUE;
1367 static LRESULT
1368 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1370 TRACE("%p\n", range);
1372 if(!range) return FALSE;
1374 range[1] = infoPtr->maxDate;
1375 range[0] = infoPtr->minDate;
1377 return infoPtr->rangeValid;
1381 static LRESULT
1382 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1384 int i;
1386 TRACE("%d %p\n", months, states);
1387 if(months != infoPtr->monthRange) return 0;
1389 for(i = 0; i < months; i++)
1390 infoPtr->monthdayState[i] = states[i];
1392 return 1;
1395 static LRESULT
1396 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1398 TRACE("%p\n", curSel);
1399 if(!curSel) return FALSE;
1400 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1402 *curSel = infoPtr->curSel;
1403 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1404 return TRUE;
1407 static LRESULT
1408 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1410 SYSTEMTIME prev = infoPtr->curSel;
1412 TRACE("%p\n", curSel);
1413 if(!curSel) return FALSE;
1414 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1416 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1417 /* exit earlier if selection equals current */
1418 if (MONTHCAL_IsDateEqual(&infoPtr->curSel, curSel)) return TRUE;
1420 if(!MONTHCAL_IsDateInValidRange(infoPtr, curSel, FALSE)) return FALSE;
1422 infoPtr->minSel = *curSel;
1423 infoPtr->maxSel = *curSel;
1425 /* if selection is still in current month, reduce rectangle */
1426 prev.wDay = curSel->wDay;
1427 if (MONTHCAL_IsDateEqual(&prev, curSel))
1429 RECT r_prev, r_new;
1431 /* note that infoPtr->curSel isn't updated yet */
1432 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->curSel, &r_prev);
1433 MONTHCAL_CalcPosFromDay(infoPtr, curSel, &r_new);
1435 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1436 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1438 else
1439 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1441 infoPtr->curSel = *curSel;
1443 return TRUE;
1447 static LRESULT
1448 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1450 return infoPtr->maxSelCount;
1454 static LRESULT
1455 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1457 TRACE("%d\n", max);
1459 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1460 if(max <= 0) return FALSE;
1462 infoPtr->maxSelCount = max;
1464 return TRUE;
1468 static LRESULT
1469 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1471 TRACE("%p\n", range);
1473 if(!range) return FALSE;
1475 if(infoPtr->dwStyle & MCS_MULTISELECT)
1477 range[1] = infoPtr->maxSel;
1478 range[0] = infoPtr->minSel;
1479 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1480 return TRUE;
1483 return FALSE;
1487 static LRESULT
1488 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1490 TRACE("%p\n", range);
1492 if(!range) return FALSE;
1494 if(infoPtr->dwStyle & MCS_MULTISELECT)
1496 SYSTEMTIME old_range[2];
1498 /* adjust timestamps */
1499 if(!MONTHCAL_ValidateTime(&range[0]))
1500 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1501 if(!MONTHCAL_ValidateTime(&range[1]))
1502 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1504 /* maximum range exceeded */
1505 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1507 old_range[0] = infoPtr->minSel;
1508 old_range[1] = infoPtr->maxSel;
1510 /* swap if min > max */
1511 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1513 infoPtr->minSel = range[0];
1514 infoPtr->maxSel = range[1];
1516 else
1518 infoPtr->minSel = range[1];
1519 infoPtr->maxSel = range[0];
1521 infoPtr->curSel = infoPtr->minSel;
1523 /* update day of week */
1524 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1525 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1526 MONTHCAL_CalculateDayOfWeek(&infoPtr->curSel, TRUE);
1528 /* redraw if bounds changed */
1529 /* FIXME: no actual need to redraw everything */
1530 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1531 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1533 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1536 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1537 return TRUE;
1540 return FALSE;
1544 static LRESULT
1545 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1547 TRACE("%p\n", today);
1549 if(!today) return FALSE;
1550 *today = infoPtr->todaysDate;
1551 return TRUE;
1554 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1556 * RETURN VALUE
1558 * TRUE - today date changed
1559 * FALSE - today date isn't changed
1561 static BOOL
1562 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1564 RECT new_r, old_r;
1566 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1568 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->todaysDate, &old_r);
1569 MONTHCAL_CalcPosFromDay(infoPtr, today, &new_r);
1571 infoPtr->todaysDate = *today;
1573 /* only two days need redrawing */
1574 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1575 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1576 return TRUE;
1579 /* MCM_SETTODAT handler */
1580 static LRESULT
1581 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1583 TRACE("%p\n", today);
1585 if(!today) return FALSE;
1587 /* remember if date was set successfully */
1588 if(MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1590 return TRUE;
1593 static LRESULT
1594 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1596 UINT x,y;
1597 DWORD retval;
1598 int day,wday,wnum;
1600 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1602 x = lpht->pt.x;
1603 y = lpht->pt.y;
1605 ZeroMemory(&lpht->st, sizeof(lpht->st));
1607 /* Comment in for debugging...
1608 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,
1609 infoPtr->wdays.left, infoPtr->wdays.right,
1610 infoPtr->wdays.top, infoPtr->wdays.bottom,
1611 infoPtr->days.left, infoPtr->days.right,
1612 infoPtr->days.top, infoPtr->days.bottom,
1613 infoPtr->todayrect.left, infoPtr->todayrect.right,
1614 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1615 infoPtr->weeknums.left, infoPtr->weeknums.right,
1616 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1619 /* are we in the header? */
1621 if(PtInRect(&infoPtr->calendars[0].title, lpht->pt)) {
1622 if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1623 retval = MCHT_TITLEBTNPREV;
1624 goto done;
1626 if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1627 retval = MCHT_TITLEBTNNEXT;
1628 goto done;
1630 if(PtInRect(&infoPtr->calendars[0].titlemonth, lpht->pt)) {
1631 retval = MCHT_TITLEMONTH;
1632 goto done;
1634 if(PtInRect(&infoPtr->calendars[0].titleyear, lpht->pt)) {
1635 retval = MCHT_TITLEYEAR;
1636 goto done;
1639 retval = MCHT_TITLE;
1640 goto done;
1643 day = MONTHCAL_CalcDayFromPos(infoPtr,x,y,&wday,&wnum);
1644 if(PtInRect(&infoPtr->calendars[0].wdays, lpht->pt)) {
1645 retval = MCHT_CALENDARDAY;
1646 lpht->st.wYear = infoPtr->curSel.wYear;
1647 lpht->st.wMonth = (day < 1)? infoPtr->curSel.wMonth -1 : infoPtr->curSel.wMonth;
1648 lpht->st.wDay = (day < 1)?
1649 MONTHCAL_MonthLength(infoPtr->curSel.wMonth-1, infoPtr->curSel.wYear) -day : day;
1650 goto done;
1652 if(PtInRect(&infoPtr->calendars[0].weeknums, lpht->pt)) {
1653 retval = MCHT_CALENDARWEEKNUM;
1654 lpht->st.wYear = infoPtr->curSel.wYear;
1655 lpht->st.wMonth = (day < 1) ? infoPtr->curSel.wMonth -1 :
1656 (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear)) ?
1657 infoPtr->curSel.wMonth +1 :infoPtr->curSel.wMonth;
1658 lpht->st.wDay = (day < 1 ) ?
1659 MONTHCAL_MonthLength(infoPtr->curSel.wMonth-1,infoPtr->curSel.wYear) -day :
1660 (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear)) ?
1661 day - MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear) : day;
1662 goto done;
1664 if(PtInRect(&infoPtr->calendars[0].days, lpht->pt))
1666 lpht->st.wYear = infoPtr->curSel.wYear;
1667 lpht->st.wMonth = infoPtr->curSel.wMonth;
1668 if (day < 1)
1670 retval = MCHT_CALENDARDATEPREV;
1671 MONTHCAL_GetPrevMonth(&lpht->st);
1672 lpht->st.wDay = MONTHCAL_MonthLength(lpht->st.wMonth, lpht->st.wYear) + day;
1674 else if (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear))
1676 retval = MCHT_CALENDARDATENEXT;
1677 MONTHCAL_GetNextMonth(&lpht->st);
1678 lpht->st.wDay = day - MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear);
1680 else {
1681 retval = MCHT_CALENDARDATE;
1682 lpht->st.wDay = day;
1684 /* always update day of week */
1685 MONTHCAL_CalculateDayOfWeek(&lpht->st, TRUE);
1686 goto done;
1688 if(PtInRect(&infoPtr->todayrect, lpht->pt)) {
1689 retval = MCHT_TODAYLINK;
1690 goto done;
1694 /* Hit nothing special? What's left must be background :-) */
1696 retval = MCHT_CALENDARBK;
1697 done:
1698 lpht->uHit = retval;
1699 return retval;
1702 /* MCN_GETDAYSTATE notification helper */
1703 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1705 if(infoPtr->dwStyle & MCS_DAYSTATE) {
1706 NMDAYSTATE nmds;
1707 INT i;
1709 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1710 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1711 nmds.nmhdr.code = MCN_GETDAYSTATE;
1712 nmds.cDayState = infoPtr->monthRange;
1713 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1715 nmds.stStart = infoPtr->todaysDate;
1716 nmds.stStart.wYear = infoPtr->curSel.wYear;
1717 nmds.stStart.wMonth = infoPtr->curSel.wMonth;
1718 nmds.stStart.wDay = 1;
1720 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1721 for(i = 0; i < infoPtr->monthRange; i++)
1722 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1724 Free(nmds.prgDayState);
1728 static void MONTHCAL_GoToPrevNextMonth(MONTHCAL_INFO *infoPtr, BOOL prev)
1730 SYSTEMTIME st = infoPtr->curSel;
1732 TRACE("%s\n", prev ? "prev" : "next");
1734 if(prev) MONTHCAL_GetPrevMonth(&st); else MONTHCAL_GetNextMonth(&st);
1736 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
1738 if(infoPtr->dwStyle & MCS_MULTISELECT)
1740 SYSTEMTIME range[2];
1742 range[0] = infoPtr->minSel;
1743 range[1] = infoPtr->maxSel;
1745 if(prev)
1747 MONTHCAL_GetPrevMonth(&range[0]);
1748 MONTHCAL_GetPrevMonth(&range[1]);
1750 else
1752 MONTHCAL_GetNextMonth(&range[0]);
1753 MONTHCAL_GetNextMonth(&range[1]);
1756 MONTHCAL_SetSelRange(infoPtr, range);
1758 else
1759 MONTHCAL_SetCurSel(infoPtr, &st);
1761 MONTHCAL_NotifyDayState(infoPtr);
1763 MONTHCAL_NotifySelectionChange(infoPtr);
1766 static LRESULT
1767 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1769 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1770 HMENU hMenu;
1771 POINT menupoint;
1772 WCHAR buf[32];
1774 hMenu = CreatePopupMenu();
1775 if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1777 WARN("Can't load resource\n");
1778 strcpyW(buf, todayW);
1780 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1781 menupoint.x = (short)LOWORD(lParam);
1782 menupoint.y = (short)HIWORD(lParam);
1783 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1784 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
1785 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1787 infoPtr->curSel = infoPtr->todaysDate;
1788 infoPtr->minSel = infoPtr->todaysDate;
1789 infoPtr->maxSel = infoPtr->todaysDate;
1790 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1793 return 0;
1796 /***
1797 * DESCRIPTION:
1798 * Subclassed edit control windproc function
1800 * PARAMETER(S):
1801 * [I] hwnd : the edit window handle
1802 * [I] uMsg : the message that is to be processed
1803 * [I] wParam : first message parameter
1804 * [I] lParam : second message parameter
1807 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1809 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
1811 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
1812 hwnd, uMsg, wParam, lParam);
1814 switch (uMsg)
1816 case WM_GETDLGCODE:
1817 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
1819 case WM_DESTROY:
1821 WNDPROC editProc = infoPtr->EditWndProc;
1822 infoPtr->EditWndProc = NULL;
1823 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
1824 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
1827 case WM_KILLFOCUS:
1828 break;
1830 case WM_KEYDOWN:
1831 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
1832 break;
1834 default:
1835 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
1838 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
1839 SendMessageW(hwnd, WM_CLOSE, 0, 0);
1840 return 0;
1843 /* creates updown control and edit box */
1844 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr)
1846 infoPtr->hWndYearEdit =
1847 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
1848 infoPtr->calendars[0].titleyear.left + 3, infoPtr->titlebtnnext.top,
1849 infoPtr->calendars[0].titleyear.right - infoPtr->calendars[0].titleyear.left + 4,
1850 infoPtr->textHeight, infoPtr->hwndSelf,
1851 NULL, NULL, NULL);
1853 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
1855 infoPtr->hWndYearUpDown =
1856 CreateWindowExW(0, UPDOWN_CLASSW, 0,
1857 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
1858 infoPtr->calendars[0].titleyear.right + 7, infoPtr->titlebtnnext.top,
1859 18, infoPtr->textHeight, infoPtr->hwndSelf,
1860 NULL, NULL, NULL);
1862 /* attach edit box */
1863 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
1864 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
1865 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
1866 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->curSel.wYear);
1868 /* subclass edit box */
1869 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
1870 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
1872 SetFocus(infoPtr->hWndYearEdit);
1875 static LRESULT
1876 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1878 MCHITTESTINFO ht;
1879 DWORD hit;
1881 /* Actually we don't need input focus for calendar, this is used to kill
1882 year updown and its buddy edit box */
1883 if (IsWindow(infoPtr->hWndYearUpDown))
1885 SetFocus(infoPtr->hwndSelf);
1886 return 0;
1889 SetCapture(infoPtr->hwndSelf);
1891 ht.cbSize = sizeof(MCHITTESTINFO);
1892 ht.pt.x = (short)LOWORD(lParam);
1893 ht.pt.y = (short)HIWORD(lParam);
1895 hit = MONTHCAL_HitTest(infoPtr, &ht);
1897 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
1899 switch(hit)
1901 case MCHT_TITLEBTNNEXT:
1902 MONTHCAL_GoToPrevNextMonth(infoPtr, FALSE);
1903 infoPtr->status = MC_NEXTPRESSED;
1904 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1905 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1906 return 0;
1908 case MCHT_TITLEBTNPREV:
1909 MONTHCAL_GoToPrevNextMonth(infoPtr, TRUE);
1910 infoPtr->status = MC_PREVPRESSED;
1911 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1912 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1913 return 0;
1915 case MCHT_TITLEMONTH:
1917 HMENU hMenu = CreatePopupMenu();
1918 WCHAR buf[32];
1919 POINT menupoint;
1920 INT i;
1922 for (i = 0; i < 12; i++)
1924 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
1925 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
1927 menupoint.x = ht.pt.x;
1928 menupoint.y = ht.pt.y;
1929 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1930 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
1931 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
1933 if ((i > 0) && (i < 13) && infoPtr->curSel.wMonth != i)
1935 infoPtr->curSel.wMonth = i;
1936 MONTHCAL_IsDateInValidRange(infoPtr, &infoPtr->curSel, TRUE);
1937 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1939 return 0;
1941 case MCHT_TITLEYEAR:
1943 MONTHCAL_EditYear(infoPtr);
1944 return 0;
1946 case MCHT_TODAYLINK:
1948 infoPtr->curSel = infoPtr->todaysDate;
1949 infoPtr->minSel = infoPtr->todaysDate;
1950 infoPtr->maxSel = infoPtr->todaysDate;
1951 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1953 MONTHCAL_NotifySelectionChange(infoPtr);
1954 MONTHCAL_NotifySelect(infoPtr);
1955 return 0;
1957 case MCHT_CALENDARDATENEXT:
1958 case MCHT_CALENDARDATEPREV:
1959 case MCHT_CALENDARDATE:
1961 SYSTEMTIME st[2];
1963 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
1965 st[0] = st[1] = ht.st;
1966 /* clear selection range */
1967 MONTHCAL_SetSelRange(infoPtr, st);
1969 infoPtr->status = MC_SEL_LBUTDOWN;
1970 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
1971 return 0;
1975 return 1;
1979 static LRESULT
1980 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1982 NMHDR nmhdr;
1983 MCHITTESTINFO ht;
1984 DWORD hit;
1986 TRACE("\n");
1988 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
1989 RECT *r;
1991 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
1992 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
1993 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
1995 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
1998 ReleaseCapture();
2000 /* always send NM_RELEASEDCAPTURE notification */
2001 nmhdr.hwndFrom = infoPtr->hwndSelf;
2002 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2003 nmhdr.code = NM_RELEASEDCAPTURE;
2004 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2006 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2008 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2010 ht.cbSize = sizeof(MCHITTESTINFO);
2011 ht.pt.x = (short)LOWORD(lParam);
2012 ht.pt.y = (short)HIWORD(lParam);
2013 hit = MONTHCAL_HitTest(infoPtr, &ht);
2015 infoPtr->status = MC_SEL_LBUTUP;
2016 MONTHCAL_SetDayFocus(infoPtr, NULL);
2018 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2020 SYSTEMTIME sel = infoPtr->curSel;
2022 /* will be invalidated here */
2023 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2025 /* send MCN_SELCHANGE only if new date selected */
2026 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2027 MONTHCAL_NotifySelectionChange(infoPtr);
2029 MONTHCAL_NotifySelect(infoPtr);
2032 return 0;
2036 static LRESULT
2037 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2039 TRACE("%ld\n", id);
2041 switch(id) {
2042 case MC_PREVNEXTMONTHTIMER:
2043 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToPrevNextMonth(infoPtr, FALSE);
2044 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToPrevNextMonth(infoPtr, TRUE);
2045 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2046 break;
2047 case MC_TODAYUPDATETIMER:
2049 SYSTEMTIME st;
2051 if(infoPtr->todaySet) return 0;
2053 GetLocalTime(&st);
2054 MONTHCAL_UpdateToday(infoPtr, &st);
2056 /* notification sent anyway */
2057 MONTHCAL_NotifySelectionChange(infoPtr);
2059 return 0;
2061 default:
2062 ERR("got unknown timer %ld\n", id);
2063 break;
2066 return 0;
2070 static LRESULT
2071 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2073 MCHITTESTINFO ht;
2074 SYSTEMTIME st_ht;
2075 INT hit;
2076 RECT r;
2078 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2080 ht.cbSize = sizeof(MCHITTESTINFO);
2081 ht.pt.x = (short)LOWORD(lParam);
2082 ht.pt.y = (short)HIWORD(lParam);
2084 hit = MONTHCAL_HitTest(infoPtr, &ht);
2086 /* not on the calendar date numbers? bail out */
2087 TRACE("hit:%x\n",hit);
2088 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2090 MONTHCAL_SetDayFocus(infoPtr, NULL);
2091 return 0;
2094 st_ht = ht.st;
2096 /* if pointer is over focused day still there's nothing to do */
2097 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2099 MONTHCAL_CalcPosFromDay(infoPtr, &ht.st, &r);
2101 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2102 SYSTEMTIME st[2];
2104 MONTHCAL_GetSelRange(infoPtr, st);
2106 /* If we're still at the first selected date and range is empty, return.
2107 If range isn't empty we should change range to a single firstSel */
2108 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2109 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2111 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2113 st[0] = infoPtr->firstSel;
2114 /* we should overwrite timestamp here */
2115 MONTHCAL_CopyDate(&st_ht, &st[1]);
2117 /* bounds will be swapped here if needed */
2118 MONTHCAL_SetSelRange(infoPtr, st);
2120 return 0;
2123 done:
2125 /* FIXME: this should specify a rectangle containing only the days that changed
2126 using InvalidateRect */
2127 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2129 return 0;
2133 static LRESULT
2134 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2136 HDC hdc;
2137 PAINTSTRUCT ps;
2139 if (hdc_paint)
2141 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2142 hdc = hdc_paint;
2144 else
2145 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2147 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2148 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2149 return 0;
2152 static LRESULT
2153 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2155 HBRUSH hbr;
2156 RECT rc;
2158 if (!GetClipBox(hdc, &rc)) return FALSE;
2160 /* fill background */
2161 hbr = CreateSolidBrush (infoPtr->bk);
2162 FillRect(hdc, &rc, hbr);
2163 DeleteObject(hbr);
2165 return TRUE;
2168 static LRESULT
2169 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2171 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2173 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2174 return 0;
2176 if (options & PRF_ERASEBKGND)
2177 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2179 if (options & PRF_CLIENT)
2180 MONTHCAL_Paint(infoPtr, hdc);
2182 return 0;
2185 static LRESULT
2186 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2188 TRACE("\n");
2190 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2192 return 0;
2195 /* sets the size information */
2196 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2198 static const WCHAR O0W[] = { '0','0',0 };
2199 HDC hdc = GetDC(infoPtr->hwndSelf);
2200 RECT *title=&infoPtr->calendars[0].title;
2201 RECT *prev=&infoPtr->titlebtnprev;
2202 RECT *next=&infoPtr->titlebtnnext;
2203 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2204 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2205 RECT *wdays=&infoPtr->calendars[0].wdays;
2206 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2207 RECT *days=&infoPtr->calendars[0].days;
2208 RECT *todayrect=&infoPtr->todayrect;
2209 SIZE size, sz;
2210 TEXTMETRICW tm;
2211 HFONT currentFont;
2212 INT xdiv, dx, dy, i;
2213 RECT rcClient;
2214 WCHAR buff[80];
2216 GetClientRect(infoPtr->hwndSelf, &rcClient);
2218 currentFont = SelectObject(hdc, infoPtr->hFont);
2220 /* get the height and width of each day's text */
2221 GetTextMetricsW(hdc, &tm);
2222 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2224 /* find largest abbreviated day name for current locale */
2225 size.cx = sz.cx = 0;
2226 for (i = 0; i < 7; i++)
2228 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + i,
2229 buff, countof(buff)))
2231 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2232 if (sz.cx > size.cx) size.cx = sz.cx;
2234 else /* locale independent fallback on failure */
2236 static const WCHAR SunW[] = { 'S','u','n',0 };
2238 GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2239 break;
2243 infoPtr->textWidth = size.cx + 2;
2245 /* recalculate the height and width increments and offsets */
2246 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2248 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2250 infoPtr->width_increment = size.cx * 2 + 4;
2251 infoPtr->height_increment = infoPtr->textHeight;
2253 /* calculate title area */
2254 title->top = 0;
2255 title->bottom = 3 * infoPtr->height_increment / 2;
2256 title->left = 0;
2257 title->right = infoPtr->width_increment * xdiv;
2259 /* set the dimensions of the next and previous buttons and center */
2260 /* the month text vertically */
2261 prev->top = next->top = title->top + 4;
2262 prev->bottom = next->bottom = title->bottom - 4;
2263 prev->left = title->left + 4;
2264 prev->right = prev->left + (title->bottom - title->top);
2265 next->right = title->right - 4;
2266 next->left = next->right - (title->bottom - title->top);
2268 /* titlemonth->left and right change based upon the current month */
2269 /* and are recalculated in refresh as the current month may change */
2270 /* without the control being resized */
2271 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2272 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2274 /* setup the dimensions of the rectangle we draw the names of the */
2275 /* days of the week in */
2276 weeknumrect->left = 0;
2278 if(infoPtr->dwStyle & MCS_WEEKNUMBERS)
2279 weeknumrect->right = prev->right;
2280 else
2281 weeknumrect->right = weeknumrect->left;
2283 wdays->left = days->left = weeknumrect->right;
2284 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2285 wdays->top = title->bottom;
2286 wdays->bottom = wdays->top + infoPtr->height_increment;
2288 days->top = weeknumrect->top = wdays->bottom;
2289 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2291 todayrect->left = 0;
2292 todayrect->right = title->right;
2293 todayrect->top = days->bottom;
2294 todayrect->bottom = days->bottom + infoPtr->height_increment;
2296 /* offset all rectangles to center in client area */
2297 dx = (rcClient.right - title->right) / 2;
2298 dy = (rcClient.bottom - todayrect->bottom) / 2;
2300 /* if calendar doesn't fit client area show it at left/top bounds */
2301 if (title->left + dx < 0) dx = 0;
2302 if (title->top + dy < 0) dy = 0;
2304 if (dx != 0 || dy != 0)
2306 OffsetRect(title, dx, dy);
2307 OffsetRect(prev, dx, dy);
2308 OffsetRect(next, dx, dy);
2309 OffsetRect(titlemonth, dx, dy);
2310 OffsetRect(titleyear, dx, dy);
2311 OffsetRect(wdays, dx, dy);
2312 OffsetRect(weeknumrect, dx, dy);
2313 OffsetRect(days, dx, dy);
2314 OffsetRect(todayrect, dx, dy);
2317 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2318 infoPtr->width_increment,infoPtr->height_increment,
2319 wine_dbgstr_rect(&rcClient),
2320 wine_dbgstr_rect(title),
2321 wine_dbgstr_rect(wdays),
2322 wine_dbgstr_rect(days),
2323 wine_dbgstr_rect(todayrect));
2325 /* restore the originally selected font */
2326 SelectObject(hdc, currentFont);
2328 ReleaseDC(infoPtr->hwndSelf, hdc);
2331 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2333 TRACE("(width=%d, height=%d)\n", Width, Height);
2335 MONTHCAL_UpdateSize(infoPtr);
2337 /* invalidate client area and erase background */
2338 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2340 return 0;
2343 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2345 return (LRESULT)infoPtr->hFont;
2348 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2350 HFONT hOldFont;
2351 LOGFONTW lf;
2353 if (!hFont) return 0;
2355 hOldFont = infoPtr->hFont;
2356 infoPtr->hFont = hFont;
2358 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2359 lf.lfWeight = FW_BOLD;
2360 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2362 MONTHCAL_UpdateSize(infoPtr);
2364 if (redraw)
2365 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2367 return (LRESULT)hOldFont;
2370 /* update theme after a WM_THEMECHANGED message */
2371 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2373 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2374 CloseThemeData (theme);
2375 OpenThemeData (infoPtr->hwndSelf, themeClass);
2376 return 0;
2379 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2380 const STYLESTRUCT *lpss)
2382 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2383 wStyleType, lpss->styleOld, lpss->styleNew);
2385 if (wStyleType != GWL_STYLE) return 0;
2387 infoPtr->dwStyle = lpss->styleNew;
2389 /* make room for week numbers */
2390 if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2391 MONTHCAL_UpdateSize(infoPtr);
2393 return 0;
2396 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2397 STYLESTRUCT *lpss)
2399 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2400 wStyleType, lpss->styleOld, lpss->styleNew);
2402 /* block MCS_MULTISELECT change */
2403 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2405 if (lpss->styleOld & MCS_MULTISELECT)
2406 lpss->styleNew |= MCS_MULTISELECT;
2407 else
2408 lpss->styleNew &= ~MCS_MULTISELECT;
2411 return 0;
2414 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2415 static LRESULT
2416 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2418 MONTHCAL_INFO *infoPtr;
2420 /* allocate memory for info structure */
2421 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2422 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2424 if (infoPtr == NULL) {
2425 ERR("could not allocate info memory!\n");
2426 return 0;
2429 infoPtr->hwndSelf = hwnd;
2430 infoPtr->hwndNotify = lpcs->hwndParent;
2431 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2432 infoPtr->calendars = Alloc(sizeof(CALENDAR_INFO));
2433 if (!infoPtr->calendars) goto fail;
2435 infoPtr->cal_num = 1;
2437 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
2439 /* initialize info structure */
2440 /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
2442 GetLocalTime(&infoPtr->todaysDate);
2443 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2445 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2446 infoPtr->monthRange = 3;
2448 infoPtr->monthdayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
2449 if (!infoPtr->monthdayState) goto fail;
2451 infoPtr->titlebk = comctl32_color.clrActiveCaption;
2452 infoPtr->titletxt = comctl32_color.clrWindow;
2453 infoPtr->monthbk = comctl32_color.clrWindow;
2454 infoPtr->trailingtxt = comctl32_color.clrGrayText;
2455 infoPtr->bk = comctl32_color.clrWindow;
2456 infoPtr->txt = comctl32_color.clrWindowText;
2458 infoPtr->minSel = infoPtr->todaysDate;
2459 infoPtr->maxSel = infoPtr->todaysDate;
2460 infoPtr->curSel = infoPtr->todaysDate;
2461 infoPtr->isUnicode = TRUE;
2463 /* call MONTHCAL_UpdateSize to set all of the dimensions */
2464 /* of the control */
2465 MONTHCAL_UpdateSize(infoPtr);
2467 /* today auto update timer, to be freed only on control destruction */
2468 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2470 OpenThemeData (infoPtr->hwndSelf, themeClass);
2472 return 0;
2474 fail:
2475 Free(infoPtr->monthdayState);
2476 Free(infoPtr->calendars);
2477 Free(infoPtr);
2478 return 0;
2481 static LRESULT
2482 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2484 /* free month calendar info data */
2485 Free(infoPtr->monthdayState);
2486 Free(infoPtr->calendars);
2487 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2489 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2491 Free(infoPtr);
2492 return 0;
2496 * Handler for WM_NOTIFY messages
2498 static LRESULT
2499 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2501 /* notification from year edit updown */
2502 if (hdr->code == UDN_DELTAPOS)
2504 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2506 if (hdr->hwndFrom == infoPtr->hWndYearUpDown)
2508 /* year value limits are set up explicitly after updown creation */
2509 if ((nmud->iDelta + nmud->iPos) != infoPtr->curSel.wYear)
2511 SYSTEMTIME new_date = infoPtr->curSel;
2513 new_date.wYear = nmud->iDelta + nmud->iPos;
2514 MONTHCAL_SetCurSel(infoPtr, &new_date);
2518 return 0;
2521 static inline BOOL
2522 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2524 BOOL prev = infoPtr->isUnicode;
2525 infoPtr->isUnicode = isUnicode;
2526 return prev;
2529 static inline BOOL
2530 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2532 return infoPtr->isUnicode;
2535 static LRESULT WINAPI
2536 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2538 MONTHCAL_INFO *infoPtr;
2540 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2542 infoPtr = MONTHCAL_GetInfoPtr(hwnd);
2543 if (!infoPtr && (uMsg != WM_CREATE))
2544 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2545 switch(uMsg)
2547 case MCM_GETCURSEL:
2548 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2550 case MCM_SETCURSEL:
2551 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2553 case MCM_GETMAXSELCOUNT:
2554 return MONTHCAL_GetMaxSelCount(infoPtr);
2556 case MCM_SETMAXSELCOUNT:
2557 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2559 case MCM_GETSELRANGE:
2560 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2562 case MCM_SETSELRANGE:
2563 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2565 case MCM_GETMONTHRANGE:
2566 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2568 case MCM_SETDAYSTATE:
2569 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2571 case MCM_GETMINREQRECT:
2572 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2574 case MCM_GETCOLOR:
2575 return MONTHCAL_GetColor(infoPtr, wParam);
2577 case MCM_SETCOLOR:
2578 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2580 case MCM_GETTODAY:
2581 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2583 case MCM_SETTODAY:
2584 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2586 case MCM_HITTEST:
2587 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2589 case MCM_GETFIRSTDAYOFWEEK:
2590 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2592 case MCM_SETFIRSTDAYOFWEEK:
2593 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2595 case MCM_GETRANGE:
2596 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2598 case MCM_SETRANGE:
2599 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2601 case MCM_GETMONTHDELTA:
2602 return MONTHCAL_GetMonthDelta(infoPtr);
2604 case MCM_SETMONTHDELTA:
2605 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2607 case MCM_GETMAXTODAYWIDTH:
2608 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2610 case MCM_SETUNICODEFORMAT:
2611 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2613 case MCM_GETUNICODEFORMAT:
2614 return MONTHCAL_GetUnicodeFormat(infoPtr);
2616 case WM_GETDLGCODE:
2617 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2619 case WM_RBUTTONUP:
2620 return MONTHCAL_RButtonUp(infoPtr, lParam);
2622 case WM_LBUTTONDOWN:
2623 return MONTHCAL_LButtonDown(infoPtr, lParam);
2625 case WM_MOUSEMOVE:
2626 return MONTHCAL_MouseMove(infoPtr, lParam);
2628 case WM_LBUTTONUP:
2629 return MONTHCAL_LButtonUp(infoPtr, lParam);
2631 case WM_PAINT:
2632 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2634 case WM_PRINTCLIENT:
2635 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2637 case WM_ERASEBKGND:
2638 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2640 case WM_SETFOCUS:
2641 return MONTHCAL_SetFocus(infoPtr);
2643 case WM_SIZE:
2644 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2646 case WM_NOTIFY:
2647 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2649 case WM_CREATE:
2650 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2652 case WM_SETFONT:
2653 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2655 case WM_GETFONT:
2656 return MONTHCAL_GetFont(infoPtr);
2658 case WM_TIMER:
2659 return MONTHCAL_Timer(infoPtr, wParam);
2661 case WM_THEMECHANGED:
2662 return theme_changed (infoPtr);
2664 case WM_DESTROY:
2665 return MONTHCAL_Destroy(infoPtr);
2667 case WM_SYSCOLORCHANGE:
2668 COMCTL32_RefreshSysColors();
2669 return 0;
2671 case WM_STYLECHANGED:
2672 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2674 case WM_STYLECHANGING:
2675 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2677 default:
2678 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2679 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2680 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2685 void
2686 MONTHCAL_Register(void)
2688 WNDCLASSW wndClass;
2690 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2691 wndClass.style = CS_GLOBALCLASS;
2692 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2693 wndClass.cbClsExtra = 0;
2694 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2695 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2696 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2697 wndClass.lpszClassName = MONTHCAL_CLASSW;
2699 RegisterClassW(&wndClass);
2703 void
2704 MONTHCAL_Unregister(void)
2706 UnregisterClassW(MONTHCAL_CLASSW, NULL);