comctl32/monthcal: Cache brush handles.
[wine.git] / dlls / comctl32 / monthcal.c
blobb750a18f3d5d0cdf5a5f31595026b61df30320a2
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, 2010 Nikolay Sivov
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * NOTE
27 * This code was audited for completeness against the documented features
28 * of Comctl32.dll version 6.0 on Oct. 20, 2004, by Dimitrie O. Paun.
30 * Unless otherwise noted, we believe this code to be complete, as per
31 * the specification mentioned above.
32 * If you discover missing features, or bugs, please note them below.
34 * TODO:
35 * -- MCM_[GS]ETUNICODEFORMAT
36 * -- handle resources better (doesn't work now);
37 * -- take care of internationalization.
38 * -- keyboard handling.
39 * -- search for FIXME
42 #include <math.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "commctrl.h"
54 #include "comctl32.h"
55 #include "uxtheme.h"
56 #include "tmschema.h"
57 #include "wine/unicode.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
62 #define MC_SEL_LBUTUP 1 /* Left button released */
63 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
64 #define MC_PREVPRESSED 4 /* Prev month button pressed */
65 #define MC_NEXTPRESSED 8 /* Next month button pressed */
66 #define MC_PREVNEXTMONTHDELAY 350 /* when continuously pressing `next/prev
67 month', wait 350 ms before going
68 to the next/prev month */
69 #define MC_TODAYUPDATEDELAY 120000 /* time between today check for update (2 min) */
71 #define MC_PREVNEXTMONTHTIMER 1 /* Timer ID's */
72 #define MC_TODAYUPDATETIMER 2
74 #define countof(arr) (sizeof(arr)/sizeof(arr[0]))
76 /* convert from days to 100 nanoseconds unit - used as FILETIME unit */
77 #define DAYSTO100NSECS(days) (((ULONGLONG)(days))*24*60*60*10000000)
79 /* single calendar data */
80 typedef struct _CALENDAR_INFO
82 RECT title; /* rect for the header above the calendar */
83 RECT titlemonth; /* the 'month name' text in the header */
84 RECT titleyear; /* the 'year number' text in the header */
85 RECT wdays; /* week days at top */
86 RECT days; /* calendar area */
87 RECT weeknums; /* week numbers at left side */
89 SYSTEMTIME month;/* contains calendar main month/year */
90 } CALENDAR_INFO;
92 typedef struct
94 HWND hwndSelf;
95 DWORD dwStyle; /* cached GWL_STYLE */
97 COLORREF colors[MCSC_TRAILINGTEXT+1];
98 HBRUSH brushes[MCSC_MONTHBK+1];
100 HFONT hFont;
101 HFONT hBoldFont;
102 int textHeight;
103 int textWidth;
104 int height_increment;
105 int width_increment;
106 INT delta; /* scroll rate; # of months that the */
107 /* control moves when user clicks a scroll button */
108 int visible; /* # of months visible */
109 int firstDay; /* Start month calendar with firstDay's day,
110 stored in SYSTEMTIME format */
111 BOOL firstDaySet; /* first week day differs from locale defined */
113 BOOL isUnicode; /* value set with MCM_SETUNICODE format */
115 int monthRange;
116 MONTHDAYSTATE *monthdayState;
117 SYSTEMTIME todaysDate;
118 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
119 int status; /* See MC_SEL flags */
120 SYSTEMTIME firstSel; /* first selected day */
121 INT maxSelCount;
122 SYSTEMTIME minSel; /* contains single selection when used without MCS_MULTISELECT */
123 SYSTEMTIME maxSel;
124 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
125 DWORD rangeValid;
126 SYSTEMTIME minDate;
127 SYSTEMTIME maxDate;
129 RECT titlebtnnext; /* the `next month' button in the header */
130 RECT titlebtnprev; /* the `prev month' button in the header */
131 RECT todayrect; /* `today: xx/xx/xx' text rect */
132 HWND hwndNotify; /* Window to receive the notifications */
133 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
134 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
135 WNDPROC EditWndProc; /* original Edit window procedure */
137 CALENDAR_INFO *calendars;
138 INT cal_num;
139 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
141 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
143 /* empty SYSTEMTIME const */
144 static const SYSTEMTIME st_null;
145 /* valid date limits */
146 static const SYSTEMTIME max_allowed_date = { .wYear = 9999, .wMonth = 12, .wDay = 31 };
147 static const SYSTEMTIME min_allowed_date = { .wYear = 1752, .wMonth = 9, .wDay = 14 };
149 /* Prev/Next buttons */
150 enum nav_direction
152 DIRECTION_BACKWARD,
153 DIRECTION_FORWARD
156 /* helper functions */
158 /* send a single MCN_SELCHANGE notification */
159 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
161 NMSELCHANGE nmsc;
163 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
164 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
165 nmsc.nmhdr.code = MCN_SELCHANGE;
166 nmsc.stSelStart = infoPtr->minSel;
167 nmsc.stSelEnd = infoPtr->maxSel;
168 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
171 /* send a single MCN_SELECT notification */
172 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
174 NMSELCHANGE nmsc;
176 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
177 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
178 nmsc.nmhdr.code = MCN_SELECT;
179 nmsc.stSelStart = infoPtr->minSel;
180 nmsc.stSelEnd = infoPtr->maxSel;
182 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
185 /* returns the number of days in any given month, checking for leap days */
186 /* january is 1, december is 12 */
187 int MONTHCAL_MonthLength(int month, int year)
189 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
190 /* Wrap around, this eases handling. Getting length only we shouldn't care
191 about year change here cause January and December have
192 the same day quantity */
193 if(month == 0)
194 month = 12;
195 else if(month == 13)
196 month = 1;
198 /* special case for calendar transition year */
199 if(month == min_allowed_date.wMonth && year == min_allowed_date.wYear) return 19;
201 /* if we have a leap year add 1 day to February */
202 /* a leap year is a year either divisible by 400 */
203 /* or divisible by 4 and not by 100 */
204 if(month == 2) { /* February */
205 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
206 (year%4 == 0)) ? 1 : 0);
208 else {
209 return mdays[month - 1];
213 /* compares timestamps using date part only */
214 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
216 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
217 (first->wDay == second->wDay);
220 /* make sure that date fields are valid */
221 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
223 if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
224 if(time->wDayOfWeek > 6) return FALSE;
225 if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear))
226 return FALSE;
228 return TRUE;
231 /* Copies timestamp part only.
233 * PARAMETERS
235 * [I] from : source date
236 * [O] to : dest date
238 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
240 to->wHour = from->wHour;
241 to->wMinute = from->wMinute;
242 to->wSecond = from->wSecond;
245 /* Copies date part only.
247 * PARAMETERS
249 * [I] from : source date
250 * [O] to : dest date
252 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
254 to->wYear = from->wYear;
255 to->wMonth = from->wMonth;
256 to->wDay = from->wDay;
257 to->wDayOfWeek = from->wDayOfWeek;
260 /* Compares two dates in SYSTEMTIME format
262 * PARAMETERS
264 * [I] first : pointer to valid first date data to compare
265 * [I] second : pointer to valid second date data to compare
267 * RETURN VALUE
269 * -1 : first < second
270 * 0 : first == second
271 * 1 : first > second
273 * Note that no date validation performed, alreadt validated values expected.
275 static LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
277 FILETIME ft_first, ft_second;
279 SystemTimeToFileTime(first, &ft_first);
280 SystemTimeToFileTime(second, &ft_second);
282 return CompareFileTime(&ft_first, &ft_second);
285 static LONG MONTHCAL_CompareMonths(const SYSTEMTIME *first, const SYSTEMTIME *second)
287 SYSTEMTIME st_first, st_second;
289 st_first = st_second = st_null;
290 MONTHCAL_CopyDate(first, &st_first);
291 MONTHCAL_CopyDate(second, &st_second);
292 st_first.wDay = st_second.wDay = 1;
294 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
297 static LONG MONTHCAL_CompareDate(const SYSTEMTIME *first, const SYSTEMTIME *second)
299 SYSTEMTIME st_first, st_second;
301 st_first = st_second = st_null;
302 MONTHCAL_CopyDate(first, &st_first);
303 MONTHCAL_CopyDate(second, &st_second);
305 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
308 /* Checks largest possible date range and configured one
310 * PARAMETERS
312 * [I] infoPtr : valid pointer to control data
313 * [I] date : pointer to valid date data to check
314 * [I] fix : make date fit valid range
316 * RETURN VALUE
318 * TRUE - date whithin largest and configured range
319 * FALSE - date is outside largest or configured range
321 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr,
322 SYSTEMTIME *date, BOOL fix)
324 const SYSTEMTIME *fix_st = NULL;
326 if(MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) {
327 fix_st = &max_allowed_date;
329 else if(MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1) {
330 fix_st = &min_allowed_date;
332 else if(infoPtr->rangeValid & GDTR_MAX) {
333 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
334 fix_st = &infoPtr->maxDate;
337 else if(infoPtr->rangeValid & GDTR_MIN) {
338 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
339 fix_st = &infoPtr->minDate;
343 if (fix && fix_st) {
344 date->wYear = fix_st->wYear;
345 date->wMonth = fix_st->wMonth;
348 return fix_st ? FALSE : TRUE;
351 /* Checks passed range width with configured maximum selection count
353 * PARAMETERS
355 * [I] infoPtr : valid pointer to control data
356 * [I] range0 : pointer to valid date data (requested bound)
357 * [I] range1 : pointer to valid date data (primary bound)
358 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
360 * Adjust value computed basing on primary bound and current maximum selection
361 * count. For simple range check (without adjusted value required) (range0, range1)
362 * relation means nothing.
364 * RETURN VALUE
366 * TRUE - range is shorter or equal to maximum
367 * FALSE - range is larger than maximum
369 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
370 const SYSTEMTIME *range0,
371 const SYSTEMTIME *range1,
372 SYSTEMTIME *adjust)
374 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
375 FILETIME ft_range0, ft_range1;
376 LONG cmp;
378 SystemTimeToFileTime(range0, &ft_range0);
379 SystemTimeToFileTime(range1, &ft_range1);
381 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
382 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
383 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
384 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
386 cmp = CompareFileTime(&ft_range0, &ft_range1);
388 if(cmp == 1)
389 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
390 else
391 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
393 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
395 if(adjust) {
396 if(cmp == 1)
397 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
398 else
399 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
401 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
402 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
403 FileTimeToSystemTime(&ft_range0, adjust);
406 return FALSE;
408 else return TRUE;
411 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
412 Milliseconds are intentionally not validated. */
413 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
415 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
416 return FALSE;
417 else
418 return TRUE;
421 /* Note:Depending on DST, this may be offset by a day.
422 Need to find out if we're on a DST place & adjust the clock accordingly.
423 Above function assumes we have a valid data.
424 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
425 0 = Sunday.
428 /* Returns the day in the week
430 * PARAMETERS
431 * [i] date : input date
432 * [I] inplace : set calculated value back to date structure
434 * RETURN VALUE
435 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
437 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
439 SYSTEMTIME st = st_null;
440 FILETIME ft;
442 MONTHCAL_CopyDate(date, &st);
444 SystemTimeToFileTime(&st, &ft);
445 FileTimeToSystemTime(&ft, &st);
447 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
449 return st.wDayOfWeek;
452 /* add/substract 'months' from date */
453 static inline void MONTHCAL_GetMonth(SYSTEMTIME *date, INT months)
455 INT length, m = date->wMonth + months;
457 date->wYear += m > 0 ? (m - 1) / 12 : m / 12 - 1;
458 date->wMonth = m > 0 ? (m - 1) % 12 + 1 : 12 + m % 12;
459 /* fix moving from last day in a month */
460 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
461 if(date->wDay > length) date->wDay = length;
462 MONTHCAL_CalculateDayOfWeek(date, TRUE);
465 /* properly updates date to point on next month */
466 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
468 return MONTHCAL_GetMonth(date, 1);
471 /* properly updates date to point on prev month */
472 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
474 return MONTHCAL_GetMonth(date, -1);
477 /* Returns full date for a first currently visible day */
478 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
480 /* zero indexed calendar has the earliest date */
481 SYSTEMTIME st_first = infoPtr->calendars[0].month;
482 INT firstDay;
484 st_first.wDay = 1;
485 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
487 *date = infoPtr->calendars[0].month;
488 MONTHCAL_GetPrevMonth(date);
490 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
491 (infoPtr->firstDay - firstDay) % 7 + 1;
493 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
494 date->wDay -= 7;
496 /* fix day of week */
497 MONTHCAL_CalculateDayOfWeek(date, TRUE);
500 /* Returns full date for a last currently visible day */
501 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
503 /* the latest date is in latest calendar */
504 SYSTEMTIME st, lt_month = infoPtr->calendars[infoPtr->cal_num-1].month;
506 *date = lt_month;
507 MONTHCAL_GetNextMonth(date);
509 MONTHCAL_GetMinDate(infoPtr, &st);
510 /* Use month length to get max day. 42 means max day count in calendar area */
511 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
512 MONTHCAL_MonthLength(lt_month.wMonth, lt_month.wYear);
514 /* fix day of week */
515 MONTHCAL_CalculateDayOfWeek(date, TRUE);
518 /* From a given point, calculate the row (weekpos), column(daypos)
519 and day in the calendar. day== 0 mean the last day of tha last month
521 static int MONTHCAL_CalcDayFromPos(const MONTHCAL_INFO *infoPtr, int x, int y,
522 int *daypos, int *weekpos)
524 int retval, firstDay;
525 RECT rcClient;
526 SYSTEMTIME st = infoPtr->minSel;
528 GetClientRect(infoPtr->hwndSelf, &rcClient);
530 /* if the point is outside the x bounds of the window put
531 it at the boundary */
532 if (x > rcClient.right)
533 x = rcClient.right;
535 *daypos = (x - infoPtr->calendars[0].days.left ) / infoPtr->width_increment;
536 *weekpos = (y - infoPtr->calendars[0].days.top ) / infoPtr->height_increment;
538 st.wDay = 1;
539 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
540 retval = *daypos + (7 * *weekpos) - firstDay;
541 return retval;
544 /* Sets the RECT struct r to the rectangle around the date
546 * PARAMETERS
548 * [I] infoPtr : pointer to control data
549 * [I] date : date value
550 * [O] x : day column (zero based)
551 * [O] y : week column (zero based)
553 static void MONTHCAL_CalcDayXY(const MONTHCAL_INFO *infoPtr,
554 const SYSTEMTIME *date, INT *x, INT *y)
556 SYSTEMTIME st = infoPtr->minSel;
557 LONG cmp;
558 int first;
560 st.wDay = 1;
561 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
563 cmp = MONTHCAL_CompareMonths(date, &infoPtr->minSel);
565 /* previous month */
566 if(cmp == -1) {
567 *x = (first - MONTHCAL_MonthLength(date->wMonth, infoPtr->minSel.wYear) + date->wDay) % 7;
568 *y = 0;
569 return;
572 /* next month calculation is same as for current,
573 just add current month length */
574 if(cmp == 1) {
575 first += MONTHCAL_MonthLength(infoPtr->minSel.wMonth, infoPtr->minSel.wYear);
578 *x = (date->wDay + first) % 7;
579 *y = (date->wDay + first - *x) / 7;
583 /* x: column(day), y: row(week) */
584 static inline void MONTHCAL_CalcDayRect(const MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
586 r->left = infoPtr->calendars[0].days.left + x * infoPtr->width_increment;
587 r->right = r->left + infoPtr->width_increment;
588 r->top = infoPtr->calendars[0].days.top + y * infoPtr->height_increment;
589 r->bottom = r->top + infoPtr->textHeight;
593 /* Sets the RECT struct r to the rectangle around the date */
594 static inline void MONTHCAL_CalcPosFromDay(const MONTHCAL_INFO *infoPtr,
595 const SYSTEMTIME *date, RECT *r)
597 int x, y;
599 MONTHCAL_CalcDayXY(infoPtr, date, &x, &y);
600 MONTHCAL_CalcDayRect(infoPtr, r, x, y);
603 /* Focused day helper:
605 - set focused date to given value;
606 - reset to zero value if NULL passed;
607 - invalidate previous and new day rectangle only if needed.
609 Returns TRUE if focused day changed, FALSE otherwise.
611 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
613 RECT r;
615 if(st)
617 /* there's nothing to do if it's the same date,
618 mouse move within same date rectangle case */
619 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
621 /* invalidate old focused day */
622 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
623 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
625 infoPtr->focusedSel = *st;
628 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
630 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
631 infoPtr->focusedSel = st_null;
633 /* on set invalidates new day, on reset clears previous focused day */
634 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
636 return TRUE;
639 /* Draw today day mark rectangle
641 * [I] hdc : context to draw in
642 * [I] day : day to mark with rectangle
645 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
646 const SYSTEMTIME *date)
648 HPEN hRedPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
649 HPEN hOldPen2 = SelectObject(hdc, hRedPen);
650 HBRUSH hOldBrush;
651 RECT day_rect;
653 MONTHCAL_CalcPosFromDay(infoPtr, date, &day_rect);
655 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
656 Rectangle(hdc, day_rect.left, day_rect.top, day_rect.right, day_rect.bottom);
658 SelectObject(hdc, hOldBrush);
659 DeleteObject(hRedPen);
660 SelectObject(hdc, hOldPen2);
663 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
664 int bold, const PAINTSTRUCT *ps)
666 static const WCHAR fmtW[] = { '%','d',0 };
667 WCHAR buf[10];
668 RECT r, r_temp;
669 static BOOL bold_selected;
670 BOOL selected_day = FALSE;
671 HBRUSH hbr;
672 COLORREF oldCol = 0;
673 COLORREF oldBk = 0;
675 /* No need to check styles: when selection is not valid, it is set to zero.
676 * 1<day<31, so everything is OK.
679 MONTHCAL_CalcPosFromDay(infoPtr, st, &r);
680 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
682 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
683 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0)) {
685 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
686 TRACE("%s\n", wine_dbgstr_rect(&r));
687 oldCol = SetTextColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
688 oldBk = SetBkColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
689 hbr = GetSysColorBrush(COLOR_HIGHLIGHT);
690 FillRect(hdc, &r, hbr);
692 selected_day = TRUE;
695 if(bold && !bold_selected) {
696 SelectObject(hdc, infoPtr->hBoldFont);
697 bold_selected = TRUE;
699 if(!bold && bold_selected) {
700 SelectObject(hdc, infoPtr->hFont);
701 bold_selected = FALSE;
704 SetBkMode(hdc,TRANSPARENT);
705 wsprintfW(buf, fmtW, st->wDay);
706 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
708 if(selected_day) {
709 SetTextColor(hdc, oldCol);
710 SetBkColor(hdc, oldBk);
715 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, enum nav_direction button)
717 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
718 RECT *r = button == DIRECTION_FORWARD ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
719 BOOL pressed = button == DIRECTION_FORWARD ? infoPtr->status & MC_NEXTPRESSED :
720 infoPtr->status & MC_PREVPRESSED;
721 if (theme)
723 static const int states[] = {
724 /* Prev button */
725 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
726 /* Next button */
727 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
729 int stateNum = button == DIRECTION_FORWARD ? 3 : 0;
730 if (pressed)
731 stateNum += 1;
732 else
734 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
736 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
738 else
740 int style = button == DIRECTION_FORWARD ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
741 if (pressed)
742 style |= DFCS_PUSHED;
743 else
745 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
748 DrawFrameControl(hdc, r, DFC_SCROLL, style);
751 /* paint a title with buttons and month/year string */
752 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
754 static const WCHAR fmt_monthW[] = { '%','s',' ','%','l','d',0 };
755 RECT *title = &infoPtr->calendars[calIdx].title;
756 const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
757 WCHAR buf_month[80], buf_fmt[80];
758 SIZE sz;
760 /* fill header box */
761 FillRect(hdc, title, infoPtr->brushes[MCSC_TITLEBK]);
763 /* month/year string */
764 SetBkColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
765 SetTextColor(hdc, infoPtr->colors[MCSC_TITLETEXT]);
766 SelectObject(hdc, infoPtr->hBoldFont);
768 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1 + st->wMonth - 1,
769 buf_month, countof(buf_month));
771 wsprintfW(buf_fmt, fmt_monthW, buf_month, st->wYear);
772 DrawTextW(hdc, buf_fmt, strlenW(buf_fmt), title,
773 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
775 /* update title rectangles with current month - used while testing hits */
776 GetTextExtentPoint32W(hdc, buf_fmt, strlenW(buf_fmt), &sz);
777 infoPtr->calendars[calIdx].titlemonth.left = title->right / 2 + title->left / 2 - sz.cx / 2;
778 infoPtr->calendars[calIdx].titleyear.right = title->right / 2 + title->left / 2 + sz.cx / 2;
780 GetTextExtentPoint32W(hdc, buf_month, strlenW(buf_month), &sz);
781 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
782 infoPtr->calendars[calIdx].titleyear.left = infoPtr->calendars[calIdx].titlemonth.right;
785 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
787 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
788 static const WCHAR fmt_weekW[] = { '%','d',0 };
789 INT mindays, weeknum, weeknum1, startofprescal;
790 INT i, prev_month;
791 SYSTEMTIME st;
792 WCHAR buf[80];
793 RECT r;
795 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
797 MONTHCAL_GetMinDate(infoPtr, &st);
798 startofprescal = st.wDay;
799 st = *date;
801 prev_month = date->wMonth - 1;
802 if(prev_month == 0) prev_month = 12;
805 Rules what week to call the first week of a new year:
806 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
807 The week containing Jan 1 is the first week of year
808 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
809 First week of year must contain 4 days of the new year
810 LOCALE_IFIRSTWEEKOFYEAR == 1 (what contries?)
811 The first week of the year must contain only days of the new year
813 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
814 weeknum = atoiW(buf);
815 switch (weeknum)
817 case 1: mindays = 6;
818 break;
819 case 2: mindays = 3;
820 break;
821 case 0: mindays = 0;
822 break;
823 default:
824 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
825 mindays = 0;
828 if (date->wMonth == 1)
830 /* calculate all those exceptions for january */
831 st.wDay = st.wMonth = 1;
832 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
833 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
834 weeknum = 1;
835 else
837 weeknum = 0;
838 for(i = 0; i < 11; i++)
839 weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
841 weeknum += startofprescal + 7;
842 weeknum /= 7;
843 st.wYear -= 1;
844 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
845 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
848 else
850 weeknum = 0;
851 for(i = 0; i < prev_month - 1; i++)
852 weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
854 weeknum += startofprescal + 7;
855 weeknum /= 7;
856 st.wDay = st.wMonth = 1;
857 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
858 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
861 r = infoPtr->calendars[calIdx].weeknums;
863 /* erase whole week numbers area */
864 FillRect(hdc, &r, infoPtr->brushes[MCSC_MONTHBK]);
866 /* reduce rectangle to one week number */
867 r.bottom = r.top + infoPtr->height_increment;
869 for(i = 0; i < 6; i++) {
870 if((i == 0) && (weeknum > 50))
872 wsprintfW(buf, fmt_weekW, weeknum);
873 weeknum = 0;
875 else if((i == 5) && (weeknum > 47))
877 wsprintfW(buf, fmt_weekW, 1);
879 else
880 wsprintfW(buf, fmt_weekW, weeknum + i);
882 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
883 OffsetRect(&r, 0, infoPtr->height_increment);
886 /* line separator for week numbers column */
887 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
888 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
891 /* bottom today date */
892 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
894 if(!(infoPtr->dwStyle & MCS_NOTODAY)) {
895 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
896 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
897 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
898 RECT rtoday;
900 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
901 SYSTEMTIME fake_st;
903 MONTHCAL_GetMaxDate(infoPtr, &fake_st);
904 /* this is always safe cause next month will never fully fit calendar */
905 fake_st.wDay += 1;
906 MONTHCAL_CircleDay(infoPtr, hdc, &fake_st);
908 if (!LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, countof(buf_todayW)))
910 WARN("Can't load resource\n");
911 strcpyW(buf_todayW, todayW);
913 MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
914 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
915 buf_dateW, countof(buf_dateW));
916 SelectObject(hdc, infoPtr->hBoldFont);
918 wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
919 DrawTextW(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
920 DrawTextW(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
922 SelectObject(hdc, infoPtr->hFont);
926 /* today mark + focus */
927 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
929 if((infoPtr->minSel.wMonth == infoPtr->todaysDate.wMonth) &&
930 (infoPtr->minSel.wYear == infoPtr->todaysDate.wYear) &&
931 !(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
933 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
936 if(!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
938 RECT r;
939 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
940 DrawFocusRect(hdc, &r);
944 /* months before first calendar month and after last calendar month */
945 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
947 INT prev_month, mask, length;
948 SYSTEMTIME st_max, st;
950 if (infoPtr->dwStyle & MCS_NOTRAILINGDATES) return;
952 SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
954 prev_month = infoPtr->calendars[0].month.wMonth - 1;
956 /* draw prev month */
957 MONTHCAL_GetMinDate(infoPtr, &st);
958 mask = 1 << (st.wDay-1);
959 /* December and January both 31 days long, so no worries if wrapped */
960 length = MONTHCAL_MonthLength(infoPtr->calendars[0].month.wMonth - 1,
961 infoPtr->calendars[0].month.wYear);
962 while(st.wDay <= length)
964 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[0] & mask, ps);
965 mask <<= 1;
966 st.wDay++;
969 /* draw next month */
970 st = infoPtr->calendars[infoPtr->cal_num-1].month;
971 st.wDay = 1;
972 MONTHCAL_GetNextMonth(&st);
973 MONTHCAL_GetMaxDate(infoPtr, &st_max);
974 mask = 1;
976 while(st.wDay <= st_max.wDay)
978 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[2] & mask, ps);
979 mask <<= 1;
980 st.wDay++;
984 /* paint a calendar area */
985 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
987 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
988 INT prev_month, i, j, length;
989 RECT r, fill_bk_rect;
990 SYSTEMTIME st;
991 WCHAR buf[80];
992 int mask;
994 /* fill whole days area - from week days area to today note rectangle */
995 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
996 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
997 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
999 FillRect(hdc, &fill_bk_rect, infoPtr->brushes[MCSC_MONTHBK]);
1001 /* draw line under day abbreviations */
1002 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
1003 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
1004 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
1005 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
1007 prev_month = date->wMonth - 1;
1008 if (prev_month == 0) prev_month = 12;
1010 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
1011 infoPtr->calendars[calIdx].weeknums.right;
1013 /* draw day abbreviations */
1014 SelectObject(hdc, infoPtr->hFont);
1015 SetBkColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
1016 SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
1017 /* rectangle to draw a single day abbreviation within */
1018 r = infoPtr->calendars[calIdx].wdays;
1019 r.right = r.left + infoPtr->width_increment;
1021 i = infoPtr->firstDay;
1022 for(j = 0; j < 7; j++) {
1023 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
1024 DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1025 OffsetRect(&r, infoPtr->width_increment, 0);
1028 /* draw current month */
1029 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1030 st = *date;
1031 st.wDay = 1;
1032 mask = 1;
1033 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1034 while(st.wDay <= length)
1036 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[1] & mask, ps);
1037 mask <<= 1;
1038 st.wDay++;
1042 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1044 COLORREF old_text_clr, old_bk_clr;
1045 HFONT old_font;
1046 INT i;
1048 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1049 old_bk_clr = GetBkColor(hdc);
1050 old_font = GetCurrentObject(hdc, OBJ_FONT);
1052 for (i = 0; i < infoPtr->cal_num; i++)
1054 RECT *title = &infoPtr->calendars[i].title;
1055 RECT r;
1057 /* draw title, redraw all its elements */
1058 if (IntersectRect(&r, &(ps->rcPaint), title))
1059 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1061 /* draw calendar area */
1062 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1063 if (IntersectRect(&r, &(ps->rcPaint), &r))
1064 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1066 /* week numbers */
1067 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1070 /* partially visible months */
1071 MONTHCAL_PaintLeadTrailMonths(infoPtr, hdc, ps);
1073 /* focus and today rectangle */
1074 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1076 /* today at the bottom left */
1077 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1079 /* navigation buttons */
1080 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_BACKWARD);
1081 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_FORWARD);
1083 /* restore context */
1084 SetBkColor(hdc, old_bk_clr);
1085 SelectObject(hdc, old_font);
1086 SetTextColor(hdc, old_text_clr);
1089 static LRESULT
1090 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, RECT *rect)
1092 TRACE("rect %p\n", rect);
1094 if(!rect) return FALSE;
1096 *rect = infoPtr->calendars[0].title;
1097 rect->bottom = infoPtr->calendars[0].days.bottom + infoPtr->todayrect.bottom -
1098 infoPtr->todayrect.top;
1100 AdjustWindowRect(rect, infoPtr->dwStyle, FALSE);
1102 /* minimal rectangle is zero based */
1103 OffsetRect(rect, -rect->left, -rect->top);
1105 TRACE("%s\n", wine_dbgstr_rect(rect));
1107 return TRUE;
1110 static COLORREF
1111 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, UINT index)
1113 TRACE("%p, %d\n", infoPtr, index);
1115 if (index > MCSC_TRAILINGTEXT) return -1;
1116 return infoPtr->colors[index];
1119 static LRESULT
1120 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, UINT index, COLORREF color)
1122 COLORREF prev;
1124 TRACE("%p, %d: color %08x\n", infoPtr, index, color);
1126 if (index > MCSC_TRAILINGTEXT) return -1;
1128 prev = infoPtr->colors[index];
1129 infoPtr->colors[index] = color;
1131 /* update cached brush */
1132 if (index == MCSC_BACKGROUND || index == MCSC_TITLEBK || index == MCSC_MONTHBK)
1134 DeleteObject(infoPtr->brushes[index]);
1135 infoPtr->brushes[index] = CreateSolidBrush(color);
1138 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND ? TRUE : FALSE);
1139 return prev;
1142 static LRESULT
1143 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1145 TRACE("\n");
1147 if(infoPtr->delta)
1148 return infoPtr->delta;
1149 else
1150 return infoPtr->visible;
1154 static LRESULT
1155 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1157 INT prev = infoPtr->delta;
1159 TRACE("delta %d\n", delta);
1161 infoPtr->delta = delta;
1162 return prev;
1166 static inline LRESULT
1167 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1169 int day;
1171 /* convert from SYSTEMTIME to locale format */
1172 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1174 return MAKELONG(day, infoPtr->firstDaySet);
1178 /* Sets the first day of the week that will appear in the control
1181 * PARAMETERS:
1182 * [I] infoPtr : valid pointer to control data
1183 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1186 * RETURN VALUE:
1187 * Low word contains previous first day,
1188 * high word indicates was first day forced with this message before or is
1189 * locale difined (TRUE - was forced, FALSE - wasn't).
1191 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1192 * FIXME: we need more error checking here
1194 static LRESULT
1195 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1197 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1198 int new_day;
1200 TRACE("%d\n", day);
1202 if(day == -1)
1204 WCHAR buf[80];
1206 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1207 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1209 new_day = atoiW(buf);
1211 infoPtr->firstDaySet = FALSE;
1213 else if(day >= 7)
1215 new_day = 6; /* max first day allowed */
1216 infoPtr->firstDaySet = TRUE;
1218 else
1220 /* Native behaviour for that case is broken: invalid date number >31
1221 got displayed at (0,0) position, current month starts always from
1222 (1,0) position. Should be implemented here as well only if there's
1223 nothing else to do. */
1224 if (day < -1)
1225 FIXME("No bug compatibility for day=%d\n", day);
1227 new_day = day;
1228 infoPtr->firstDaySet = TRUE;
1231 /* convert from locale to SYSTEMTIME format */
1232 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1234 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1236 return prev;
1240 static LRESULT
1241 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
1243 TRACE("flag=%d, st=%p\n", flag, st);
1245 if(st)
1247 switch (flag) {
1248 case GMR_VISIBLE:
1250 st[0] = infoPtr->calendars[0].month;
1251 st[1] = infoPtr->calendars[infoPtr->cal_num-1].month;
1253 if (st[0].wMonth == min_allowed_date.wMonth &&
1254 st[0].wYear == min_allowed_date.wYear)
1256 st[0].wDay = min_allowed_date.wDay;
1258 else
1259 st[0].wDay = 1;
1260 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
1262 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
1263 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
1265 return infoPtr->cal_num;
1267 case GMR_DAYSTATE:
1269 MONTHCAL_GetMinDate(infoPtr, &st[0]);
1270 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
1271 break;
1273 default:
1274 WARN("Unknown flag value, got %d\n", flag);
1278 return infoPtr->monthRange;
1282 static LRESULT
1283 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1285 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1289 static LRESULT
1290 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1292 FILETIME ft_min, ft_max;
1294 TRACE("%x %p\n", limits, range);
1296 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1297 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1298 return FALSE;
1300 if (limits & GDTR_MIN)
1302 if (!MONTHCAL_ValidateTime(&range[0]))
1303 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1305 infoPtr->minDate = range[0];
1306 infoPtr->rangeValid |= GDTR_MIN;
1308 if (limits & GDTR_MAX)
1310 if (!MONTHCAL_ValidateTime(&range[1]))
1311 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1313 infoPtr->maxDate = range[1];
1314 infoPtr->rangeValid |= GDTR_MAX;
1317 /* Only one limit set - we are done */
1318 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1319 return TRUE;
1321 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1322 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1324 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1326 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1328 /* Native swaps limits only when both limits are being set. */
1329 SYSTEMTIME st_tmp = infoPtr->minDate;
1330 infoPtr->minDate = infoPtr->maxDate;
1331 infoPtr->maxDate = st_tmp;
1333 else
1335 /* reset the other limit */
1336 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1337 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1338 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1342 return TRUE;
1346 static LRESULT
1347 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1349 TRACE("%p\n", range);
1351 if(!range) return FALSE;
1353 range[1] = infoPtr->maxDate;
1354 range[0] = infoPtr->minDate;
1356 return infoPtr->rangeValid;
1360 static LRESULT
1361 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1363 TRACE("%p %d %p\n", infoPtr, months, states);
1364 if(months != infoPtr->monthRange) return 0;
1366 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1368 return 1;
1371 static LRESULT
1372 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1374 TRACE("%p\n", curSel);
1375 if(!curSel) return FALSE;
1376 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1378 *curSel = infoPtr->minSel;
1379 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1380 return TRUE;
1383 static LRESULT
1384 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1386 SYSTEMTIME prev = infoPtr->minSel;
1387 WORD day;
1389 TRACE("%p\n", curSel);
1390 if(!curSel) return FALSE;
1391 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1393 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1394 /* exit earlier if selection equals current */
1395 if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1397 if(!MONTHCAL_IsDateInValidRange(infoPtr, curSel, FALSE)) return FALSE;
1399 infoPtr->calendars[0].month = *curSel;
1400 infoPtr->minSel = *curSel;
1401 infoPtr->maxSel = *curSel;
1403 /* if selection is still in current month, reduce rectangle */
1404 day = prev.wDay;
1405 prev.wDay = curSel->wDay;
1406 if (MONTHCAL_IsDateEqual(&prev, curSel))
1408 RECT r_prev, r_new;
1410 prev.wDay = day;
1411 MONTHCAL_CalcPosFromDay(infoPtr, &prev, &r_prev);
1412 MONTHCAL_CalcPosFromDay(infoPtr, curSel, &r_new);
1414 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1415 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1417 else
1418 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1420 return TRUE;
1424 static LRESULT
1425 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1427 return infoPtr->maxSelCount;
1431 static LRESULT
1432 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1434 TRACE("%d\n", max);
1436 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1437 if(max <= 0) return FALSE;
1439 infoPtr->maxSelCount = max;
1441 return TRUE;
1445 static LRESULT
1446 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1448 TRACE("%p\n", range);
1450 if(!range) return FALSE;
1452 if(infoPtr->dwStyle & MCS_MULTISELECT)
1454 range[1] = infoPtr->maxSel;
1455 range[0] = infoPtr->minSel;
1456 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1457 return TRUE;
1460 return FALSE;
1464 static LRESULT
1465 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1467 TRACE("%p\n", range);
1469 if(!range) return FALSE;
1471 if(infoPtr->dwStyle & MCS_MULTISELECT)
1473 SYSTEMTIME old_range[2];
1475 /* adjust timestamps */
1476 if(!MONTHCAL_ValidateTime(&range[0]))
1477 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1478 if(!MONTHCAL_ValidateTime(&range[1]))
1479 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1481 /* maximum range exceeded */
1482 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1484 old_range[0] = infoPtr->minSel;
1485 old_range[1] = infoPtr->maxSel;
1487 /* swap if min > max */
1488 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1490 infoPtr->minSel = range[0];
1491 infoPtr->maxSel = range[1];
1493 else
1495 infoPtr->minSel = range[1];
1496 infoPtr->maxSel = range[0];
1498 infoPtr->calendars[0].month = infoPtr->minSel;
1500 /* update day of week */
1501 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1502 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1504 /* redraw if bounds changed */
1505 /* FIXME: no actual need to redraw everything */
1506 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1507 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1509 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1512 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1513 return TRUE;
1516 return FALSE;
1520 static LRESULT
1521 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1523 TRACE("%p\n", today);
1525 if(!today) return FALSE;
1526 *today = infoPtr->todaysDate;
1527 return TRUE;
1530 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1532 * RETURN VALUE
1534 * TRUE - today date changed
1535 * FALSE - today date isn't changed
1537 static BOOL
1538 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1540 RECT new_r, old_r;
1542 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1544 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->todaysDate, &old_r);
1545 MONTHCAL_CalcPosFromDay(infoPtr, today, &new_r);
1547 infoPtr->todaysDate = *today;
1549 /* only two days need redrawing */
1550 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1551 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1552 return TRUE;
1555 /* MCM_SETTODAT handler */
1556 static LRESULT
1557 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1559 TRACE("%p\n", today);
1561 if(!today) return FALSE;
1563 /* remember if date was set successfully */
1564 if(MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1566 return TRUE;
1569 /* returns calendar index containing specified point, or -1 if it's background */
1570 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1572 RECT r;
1573 INT i;
1575 for (i = 0; i < infoPtr->cal_num; i++)
1577 /* whole bounding rectangle allows some optimization to compute */
1578 r.left = infoPtr->calendars[i].title.left;
1579 r.top = infoPtr->calendars[i].title.top;
1580 r.bottom = infoPtr->calendars[i].days.bottom;
1581 r.right = infoPtr->calendars[i].days.right;
1583 if (PtInRect(&r, *pt)) return i;
1586 return -1;
1589 static inline UINT fill_hittest_info(const MCHITTESTINFO *src, MCHITTESTINFO *dest)
1591 dest->uHit = src->uHit;
1592 dest->st = src->st;
1594 if (dest->cbSize == sizeof(MCHITTESTINFO))
1595 memcpy(&dest->rc, &src->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1597 return src->uHit;
1600 static LRESULT
1601 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1603 INT day, wday, wnum, calIdx;
1604 MCHITTESTINFO htinfo;
1605 SYSTEMTIME ht_month;
1606 UINT x, y;
1608 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1610 x = lpht->pt.x;
1611 y = lpht->pt.y;
1613 htinfo.st = st_null;
1615 /* we should preserve passed fields if hit area doesn't need them */
1616 if (lpht->cbSize == sizeof(MCHITTESTINFO))
1617 memcpy(&htinfo.rc, &lpht->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1619 /* Comment in for debugging...
1620 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,
1621 infoPtr->wdays.left, infoPtr->wdays.right,
1622 infoPtr->wdays.top, infoPtr->wdays.bottom,
1623 infoPtr->days.left, infoPtr->days.right,
1624 infoPtr->days.top, infoPtr->days.bottom,
1625 infoPtr->todayrect.left, infoPtr->todayrect.right,
1626 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1627 infoPtr->weeknums.left, infoPtr->weeknums.right,
1628 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1631 /* guess in what calendar we are */
1632 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1633 if (calIdx == -1)
1635 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1637 htinfo.uHit = MCHT_TODAYLINK;
1638 htinfo.rc = infoPtr->todayrect;
1640 else
1641 /* outside of calendar area? What's left must be background :-) */
1642 htinfo.uHit = MCHT_CALENDARBK;
1644 return fill_hittest_info(&htinfo, lpht);
1647 ht_month = infoPtr->calendars[calIdx].month;
1649 /* are we in the header? */
1650 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1651 /* FIXME: buttons hittesting could be optimized cause maximum
1652 two calendars have buttons */
1653 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1655 htinfo.uHit = MCHT_TITLEBTNPREV;
1656 htinfo.rc = infoPtr->titlebtnprev;
1658 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1660 htinfo.uHit = MCHT_TITLEBTNNEXT;
1661 htinfo.rc = infoPtr->titlebtnnext;
1663 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1665 htinfo.uHit = MCHT_TITLEMONTH;
1666 htinfo.rc = infoPtr->calendars[calIdx].titlemonth;
1667 htinfo.iOffset = calIdx;
1669 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1671 htinfo.uHit = MCHT_TITLEYEAR;
1672 htinfo.rc = infoPtr->calendars[calIdx].titleyear;
1673 htinfo.iOffset = calIdx;
1675 else
1677 htinfo.uHit = MCHT_TITLE;
1678 htinfo.rc = infoPtr->calendars[calIdx].title;
1679 htinfo.iOffset = calIdx;
1682 return fill_hittest_info(&htinfo, lpht);
1685 /* days area (including week days and week numbers */
1686 day = MONTHCAL_CalcDayFromPos(infoPtr, x, y, &wday, &wnum);
1687 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1689 htinfo.uHit = MCHT_CALENDARDAY;
1690 htinfo.iOffset = calIdx;
1691 htinfo.st.wYear = ht_month.wYear;
1692 htinfo.st.wMonth = (day < 1) ? ht_month.wMonth -1 : ht_month.wMonth;
1693 htinfo.st.wDay = (day < 1) ?
1694 MONTHCAL_MonthLength(ht_month.wMonth-1, ht_month.wYear) - day : day;
1696 MONTHCAL_CalcDayXY(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow);
1698 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1700 htinfo.uHit = MCHT_CALENDARWEEKNUM;
1701 htinfo.st.wYear = ht_month.wYear;
1702 htinfo.iOffset = calIdx;
1704 if (day < 1)
1706 htinfo.st.wMonth = ht_month.wMonth - 1;
1707 htinfo.st.wDay = MONTHCAL_MonthLength(ht_month.wMonth-1, ht_month.wYear) - day;
1709 else if (day > MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear))
1711 htinfo.st.wMonth = ht_month.wMonth + 1;
1712 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear);
1714 else
1716 htinfo.st.wMonth = ht_month.wMonth;
1717 htinfo.st.wDay = day;
1720 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1722 htinfo.iOffset = calIdx;
1723 htinfo.st.wYear = ht_month.wYear;
1724 htinfo.st.wMonth = ht_month.wMonth;
1725 if (day < 1)
1727 htinfo.uHit = MCHT_CALENDARDATEPREV;
1728 MONTHCAL_GetPrevMonth(&htinfo.st);
1729 htinfo.st.wDay = MONTHCAL_MonthLength(htinfo.st.wMonth, htinfo.st.wYear) + day;
1731 else if (day > MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear))
1733 htinfo.uHit = MCHT_CALENDARDATENEXT;
1734 MONTHCAL_GetNextMonth(&htinfo.st);
1735 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear);
1737 else
1739 htinfo.uHit = MCHT_CALENDARDATE;
1740 htinfo.st.wDay = day;
1743 MONTHCAL_CalcDayXY(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow);
1744 MONTHCAL_CalcDayRect(infoPtr, &htinfo.rc, htinfo.iCol, htinfo.iRow);
1745 /* always update day of week */
1746 MONTHCAL_CalculateDayOfWeek(&htinfo.st, TRUE);
1749 return fill_hittest_info(&htinfo, lpht);
1752 /* MCN_GETDAYSTATE notification helper */
1753 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1755 if(infoPtr->dwStyle & MCS_DAYSTATE) {
1756 NMDAYSTATE nmds;
1758 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1759 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1760 nmds.nmhdr.code = MCN_GETDAYSTATE;
1761 nmds.cDayState = infoPtr->monthRange;
1762 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1764 nmds.stStart = infoPtr->todaysDate;
1765 nmds.stStart.wYear = infoPtr->minSel.wYear;
1766 nmds.stStart.wMonth = infoPtr->minSel.wMonth;
1767 nmds.stStart.wDay = 1;
1769 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1770 memcpy(infoPtr->monthdayState, nmds.prgDayState, infoPtr->monthRange*sizeof(MONTHDAYSTATE));
1772 Free(nmds.prgDayState);
1776 /* no valid range check performed */
1777 static void MONTHCAL_Scroll(MONTHCAL_INFO *infoPtr, INT delta)
1779 INT i, selIdx = -1;
1781 for(i = 0; i < infoPtr->cal_num; i++)
1783 /* save selection position to shift it later */
1784 if (selIdx == -1 && MONTHCAL_CompareMonths(&infoPtr->minSel, &infoPtr->calendars[i].month) == 0)
1785 selIdx = i;
1787 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, delta);
1790 /* selection is always shifted to first calendar */
1791 if(infoPtr->dwStyle & MCS_MULTISELECT)
1793 SYSTEMTIME range[2];
1795 MONTHCAL_GetSelRange(infoPtr, range);
1796 MONTHCAL_GetMonth(&range[0], delta - selIdx);
1797 MONTHCAL_GetMonth(&range[1], delta - selIdx);
1798 MONTHCAL_SetSelRange(infoPtr, range);
1800 else
1802 SYSTEMTIME st = infoPtr->minSel;
1804 MONTHCAL_GetMonth(&st, delta - selIdx);
1805 MONTHCAL_SetCurSel(infoPtr, &st);
1809 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
1811 INT delta = infoPtr->delta ? infoPtr->delta : infoPtr->cal_num;
1812 SYSTEMTIME st;
1814 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
1816 /* check if change allowed by range set */
1817 if(direction == DIRECTION_BACKWARD)
1819 st = infoPtr->calendars[0].month;
1820 MONTHCAL_GetMonth(&st, -delta);
1822 else
1824 st = infoPtr->calendars[infoPtr->cal_num-1].month;
1825 MONTHCAL_GetMonth(&st, delta);
1828 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
1830 MONTHCAL_Scroll(infoPtr, direction == DIRECTION_BACKWARD ? -delta : delta);
1831 MONTHCAL_NotifyDayState(infoPtr);
1832 MONTHCAL_NotifySelectionChange(infoPtr);
1835 static LRESULT
1836 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1838 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1839 HMENU hMenu;
1840 POINT menupoint;
1841 WCHAR buf[32];
1843 hMenu = CreatePopupMenu();
1844 if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1846 WARN("Can't load resource\n");
1847 strcpyW(buf, todayW);
1849 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1850 menupoint.x = (short)LOWORD(lParam);
1851 menupoint.y = (short)HIWORD(lParam);
1852 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1853 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
1854 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1856 infoPtr->calendars[0].month = infoPtr->todaysDate;
1857 infoPtr->minSel = infoPtr->todaysDate;
1858 infoPtr->maxSel = infoPtr->todaysDate;
1859 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1862 return 0;
1865 /***
1866 * DESCRIPTION:
1867 * Subclassed edit control windproc function
1869 * PARAMETER(S):
1870 * [I] hwnd : the edit window handle
1871 * [I] uMsg : the message that is to be processed
1872 * [I] wParam : first message parameter
1873 * [I] lParam : second message parameter
1876 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1878 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
1880 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
1881 hwnd, uMsg, wParam, lParam);
1883 switch (uMsg)
1885 case WM_GETDLGCODE:
1886 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
1888 case WM_DESTROY:
1890 WNDPROC editProc = infoPtr->EditWndProc;
1891 infoPtr->EditWndProc = NULL;
1892 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
1893 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
1896 case WM_KILLFOCUS:
1897 break;
1899 case WM_KEYDOWN:
1900 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
1901 break;
1903 default:
1904 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
1907 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
1908 SendMessageW(hwnd, WM_CLOSE, 0, 0);
1909 return 0;
1912 /* creates updown control and edit box */
1913 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr, INT calIdx)
1915 RECT *rc = &infoPtr->calendars[calIdx].titleyear;
1916 RECT *title = &infoPtr->calendars[calIdx].title;
1918 infoPtr->hWndYearEdit =
1919 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
1920 rc->left + 3, (title->bottom + title->top - infoPtr->textHeight) / 2,
1921 rc->right - rc->left + 4,
1922 infoPtr->textHeight, infoPtr->hwndSelf,
1923 NULL, NULL, NULL);
1925 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
1927 infoPtr->hWndYearUpDown =
1928 CreateWindowExW(0, UPDOWN_CLASSW, 0,
1929 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
1930 rc->right + 7, (title->bottom + title->top - infoPtr->textHeight) / 2,
1931 18, infoPtr->textHeight, infoPtr->hwndSelf,
1932 NULL, NULL, NULL);
1934 /* attach edit box */
1935 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
1936 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
1937 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
1938 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->calendars[calIdx].month.wYear);
1940 /* subclass edit box */
1941 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
1942 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
1944 SetFocus(infoPtr->hWndYearEdit);
1947 static LRESULT
1948 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1950 MCHITTESTINFO ht;
1951 DWORD hit;
1953 /* Actually we don't need input focus for calendar, this is used to kill
1954 year updown and its buddy edit box */
1955 if (IsWindow(infoPtr->hWndYearUpDown))
1957 SetFocus(infoPtr->hwndSelf);
1958 return 0;
1961 SetCapture(infoPtr->hwndSelf);
1963 ht.cbSize = sizeof(MCHITTESTINFO);
1964 ht.pt.x = (short)LOWORD(lParam);
1965 ht.pt.y = (short)HIWORD(lParam);
1967 hit = MONTHCAL_HitTest(infoPtr, &ht);
1969 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
1971 switch(hit)
1973 case MCHT_TITLEBTNNEXT:
1974 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
1975 infoPtr->status = MC_NEXTPRESSED;
1976 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1977 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1978 return 0;
1980 case MCHT_TITLEBTNPREV:
1981 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
1982 infoPtr->status = MC_PREVPRESSED;
1983 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1984 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1985 return 0;
1987 case MCHT_TITLEMONTH:
1989 HMENU hMenu = CreatePopupMenu();
1990 WCHAR buf[32];
1991 POINT menupoint;
1992 INT i;
1994 for (i = 0; i < 12; i++)
1996 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
1997 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
1999 menupoint.x = ht.pt.x;
2000 menupoint.y = ht.pt.y;
2001 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2002 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
2003 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
2005 if ((i > 0) && (i < 13) && infoPtr->calendars[ht.iOffset].month.wMonth != i)
2007 INT delta = i - infoPtr->calendars[ht.iOffset].month.wMonth;
2008 SYSTEMTIME st;
2010 /* check if change allowed by range set */
2011 st = delta < 0 ? infoPtr->calendars[0].month :
2012 infoPtr->calendars[infoPtr->cal_num-1].month;
2013 MONTHCAL_GetMonth(&st, delta);
2015 if (MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE))
2017 MONTHCAL_Scroll(infoPtr, delta);
2018 MONTHCAL_NotifyDayState(infoPtr);
2019 MONTHCAL_NotifySelectionChange(infoPtr);
2020 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2023 return 0;
2025 case MCHT_TITLEYEAR:
2027 MONTHCAL_EditYear(infoPtr, ht.iOffset);
2028 return 0;
2030 case MCHT_TODAYLINK:
2032 infoPtr->calendars[0].month = infoPtr->todaysDate;
2033 infoPtr->minSel = infoPtr->todaysDate;
2034 infoPtr->maxSel = infoPtr->todaysDate;
2035 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2037 MONTHCAL_NotifySelectionChange(infoPtr);
2038 MONTHCAL_NotifySelect(infoPtr);
2039 return 0;
2041 case MCHT_CALENDARDATENEXT:
2042 case MCHT_CALENDARDATEPREV:
2043 case MCHT_CALENDARDATE:
2045 SYSTEMTIME st[2];
2047 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2049 st[0] = st[1] = ht.st;
2050 /* clear selection range */
2051 MONTHCAL_SetSelRange(infoPtr, st);
2053 infoPtr->status = MC_SEL_LBUTDOWN;
2054 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
2055 return 0;
2059 return 1;
2063 static LRESULT
2064 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2066 NMHDR nmhdr;
2067 MCHITTESTINFO ht;
2068 DWORD hit;
2070 TRACE("\n");
2072 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2073 RECT *r;
2075 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2076 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2077 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2079 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2082 ReleaseCapture();
2084 /* always send NM_RELEASEDCAPTURE notification */
2085 nmhdr.hwndFrom = infoPtr->hwndSelf;
2086 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2087 nmhdr.code = NM_RELEASEDCAPTURE;
2088 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2090 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2092 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2094 ht.cbSize = sizeof(MCHITTESTINFO);
2095 ht.pt.x = (short)LOWORD(lParam);
2096 ht.pt.y = (short)HIWORD(lParam);
2097 hit = MONTHCAL_HitTest(infoPtr, &ht);
2099 infoPtr->status = MC_SEL_LBUTUP;
2100 MONTHCAL_SetDayFocus(infoPtr, NULL);
2102 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2104 SYSTEMTIME sel = infoPtr->minSel;
2106 /* will be invalidated here */
2107 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2109 /* send MCN_SELCHANGE only if new date selected */
2110 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2111 MONTHCAL_NotifySelectionChange(infoPtr);
2113 MONTHCAL_NotifySelect(infoPtr);
2116 return 0;
2120 static LRESULT
2121 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2123 TRACE("%ld\n", id);
2125 switch(id) {
2126 case MC_PREVNEXTMONTHTIMER:
2127 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2128 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2129 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2130 break;
2131 case MC_TODAYUPDATETIMER:
2133 SYSTEMTIME st;
2135 if(infoPtr->todaySet) return 0;
2137 GetLocalTime(&st);
2138 MONTHCAL_UpdateToday(infoPtr, &st);
2140 /* notification sent anyway */
2141 MONTHCAL_NotifySelectionChange(infoPtr);
2143 return 0;
2145 default:
2146 ERR("got unknown timer %ld\n", id);
2147 break;
2150 return 0;
2154 static LRESULT
2155 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2157 MCHITTESTINFO ht;
2158 SYSTEMTIME st_ht;
2159 INT hit;
2160 RECT r;
2162 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2164 ht.cbSize = sizeof(MCHITTESTINFO);
2165 ht.pt.x = (short)LOWORD(lParam);
2166 ht.pt.y = (short)HIWORD(lParam);
2168 hit = MONTHCAL_HitTest(infoPtr, &ht);
2170 /* not on the calendar date numbers? bail out */
2171 TRACE("hit:%x\n",hit);
2172 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2174 MONTHCAL_SetDayFocus(infoPtr, NULL);
2175 return 0;
2178 st_ht = ht.st;
2180 /* if pointer is over focused day still there's nothing to do */
2181 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2183 MONTHCAL_CalcPosFromDay(infoPtr, &ht.st, &r);
2185 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2186 SYSTEMTIME st[2];
2188 MONTHCAL_GetSelRange(infoPtr, st);
2190 /* If we're still at the first selected date and range is empty, return.
2191 If range isn't empty we should change range to a single firstSel */
2192 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2193 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2195 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2197 st[0] = infoPtr->firstSel;
2198 /* we should overwrite timestamp here */
2199 MONTHCAL_CopyDate(&st_ht, &st[1]);
2201 /* bounds will be swapped here if needed */
2202 MONTHCAL_SetSelRange(infoPtr, st);
2204 return 0;
2207 done:
2209 /* FIXME: this should specify a rectangle containing only the days that changed
2210 using InvalidateRect */
2211 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2213 return 0;
2217 static LRESULT
2218 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2220 HDC hdc;
2221 PAINTSTRUCT ps;
2223 if (hdc_paint)
2225 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2226 hdc = hdc_paint;
2228 else
2229 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2231 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2232 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2233 return 0;
2236 static LRESULT
2237 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2239 RECT rc;
2241 if (!GetClipBox(hdc, &rc)) return FALSE;
2243 /* fill background */
2244 FillRect(hdc, &rc, infoPtr->brushes[MCSC_BACKGROUND]);
2246 return TRUE;
2249 static LRESULT
2250 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2252 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2254 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2255 return 0;
2257 if (options & PRF_ERASEBKGND)
2258 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2260 if (options & PRF_CLIENT)
2261 MONTHCAL_Paint(infoPtr, hdc);
2263 return 0;
2266 static LRESULT
2267 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2269 TRACE("\n");
2271 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2273 return 0;
2276 /* sets the size information */
2277 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2279 static const WCHAR O0W[] = { '0','0',0 };
2280 HDC hdc = GetDC(infoPtr->hwndSelf);
2281 RECT *title=&infoPtr->calendars[0].title;
2282 RECT *prev=&infoPtr->titlebtnprev;
2283 RECT *next=&infoPtr->titlebtnnext;
2284 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2285 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2286 RECT *wdays=&infoPtr->calendars[0].wdays;
2287 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2288 RECT *days=&infoPtr->calendars[0].days;
2289 RECT *todayrect=&infoPtr->todayrect;
2290 SIZE size, sz;
2291 TEXTMETRICW tm;
2292 HFONT currentFont;
2293 INT xdiv, dx, dy, i;
2294 RECT rcClient;
2295 WCHAR buff[80];
2297 GetClientRect(infoPtr->hwndSelf, &rcClient);
2299 currentFont = SelectObject(hdc, infoPtr->hFont);
2301 /* get the height and width of each day's text */
2302 GetTextMetricsW(hdc, &tm);
2303 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2305 /* find largest abbreviated day name for current locale */
2306 size.cx = sz.cx = 0;
2307 for (i = 0; i < 7; i++)
2309 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + i,
2310 buff, countof(buff)))
2312 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2313 if (sz.cx > size.cx) size.cx = sz.cx;
2315 else /* locale independent fallback on failure */
2317 static const WCHAR SunW[] = { 'S','u','n',0 };
2319 GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2320 break;
2324 infoPtr->textWidth = size.cx + 2;
2326 /* recalculate the height and width increments and offsets */
2327 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2329 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2331 infoPtr->width_increment = size.cx * 2 + 4;
2332 infoPtr->height_increment = infoPtr->textHeight;
2334 /* calculate title area */
2335 title->top = 0;
2336 title->bottom = 3 * infoPtr->height_increment / 2;
2337 title->left = 0;
2338 title->right = infoPtr->width_increment * xdiv;
2340 /* set the dimensions of the next and previous buttons and center */
2341 /* the month text vertically */
2342 prev->top = next->top = title->top + 4;
2343 prev->bottom = next->bottom = title->bottom - 4;
2344 prev->left = title->left + 4;
2345 prev->right = prev->left + (title->bottom - title->top);
2346 next->right = title->right - 4;
2347 next->left = next->right - (title->bottom - title->top);
2349 /* titlemonth->left and right change based upon the current month */
2350 /* and are recalculated in refresh as the current month may change */
2351 /* without the control being resized */
2352 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2353 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2355 /* setup the dimensions of the rectangle we draw the names of the */
2356 /* days of the week in */
2357 weeknumrect->left = 0;
2359 if(infoPtr->dwStyle & MCS_WEEKNUMBERS)
2360 weeknumrect->right = prev->right;
2361 else
2362 weeknumrect->right = weeknumrect->left;
2364 wdays->left = days->left = weeknumrect->right;
2365 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2366 wdays->top = title->bottom;
2367 wdays->bottom = wdays->top + infoPtr->height_increment;
2369 days->top = weeknumrect->top = wdays->bottom;
2370 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2372 todayrect->left = 0;
2373 todayrect->right = title->right;
2374 todayrect->top = days->bottom;
2375 todayrect->bottom = days->bottom + infoPtr->height_increment;
2377 /* offset all rectangles to center in client area */
2378 dx = (rcClient.right - title->right) / 2;
2379 dy = (rcClient.bottom - todayrect->bottom) / 2;
2381 /* if calendar doesn't fit client area show it at left/top bounds */
2382 if (title->left + dx < 0) dx = 0;
2383 if (title->top + dy < 0) dy = 0;
2385 if (dx != 0 || dy != 0)
2387 OffsetRect(title, dx, dy);
2388 OffsetRect(prev, dx, dy);
2389 OffsetRect(next, dx, dy);
2390 OffsetRect(titlemonth, dx, dy);
2391 OffsetRect(titleyear, dx, dy);
2392 OffsetRect(wdays, dx, dy);
2393 OffsetRect(weeknumrect, dx, dy);
2394 OffsetRect(days, dx, dy);
2395 OffsetRect(todayrect, dx, dy);
2398 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2399 infoPtr->width_increment,infoPtr->height_increment,
2400 wine_dbgstr_rect(&rcClient),
2401 wine_dbgstr_rect(title),
2402 wine_dbgstr_rect(wdays),
2403 wine_dbgstr_rect(days),
2404 wine_dbgstr_rect(todayrect));
2406 /* restore the originally selected font */
2407 SelectObject(hdc, currentFont);
2409 ReleaseDC(infoPtr->hwndSelf, hdc);
2412 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2414 TRACE("(width=%d, height=%d)\n", Width, Height);
2416 MONTHCAL_UpdateSize(infoPtr);
2417 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2419 return 0;
2422 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2424 return (LRESULT)infoPtr->hFont;
2427 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2429 HFONT hOldFont;
2430 LOGFONTW lf;
2432 if (!hFont) return 0;
2434 hOldFont = infoPtr->hFont;
2435 infoPtr->hFont = hFont;
2437 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2438 lf.lfWeight = FW_BOLD;
2439 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2441 MONTHCAL_UpdateSize(infoPtr);
2443 if (redraw)
2444 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2446 return (LRESULT)hOldFont;
2449 /* update theme after a WM_THEMECHANGED message */
2450 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2452 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2453 CloseThemeData (theme);
2454 OpenThemeData (infoPtr->hwndSelf, themeClass);
2455 return 0;
2458 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2459 const STYLESTRUCT *lpss)
2461 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2462 wStyleType, lpss->styleOld, lpss->styleNew);
2464 if (wStyleType != GWL_STYLE) return 0;
2466 infoPtr->dwStyle = lpss->styleNew;
2468 /* make room for week numbers */
2469 if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2470 MONTHCAL_UpdateSize(infoPtr);
2472 return 0;
2475 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2476 STYLESTRUCT *lpss)
2478 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2479 wStyleType, lpss->styleOld, lpss->styleNew);
2481 /* block MCS_MULTISELECT change */
2482 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2484 if (lpss->styleOld & MCS_MULTISELECT)
2485 lpss->styleNew |= MCS_MULTISELECT;
2486 else
2487 lpss->styleNew &= ~MCS_MULTISELECT;
2490 return 0;
2493 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2494 static LRESULT
2495 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2497 MONTHCAL_INFO *infoPtr;
2499 /* allocate memory for info structure */
2500 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2501 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2503 if (infoPtr == NULL) {
2504 ERR("could not allocate info memory!\n");
2505 return 0;
2508 infoPtr->hwndSelf = hwnd;
2509 infoPtr->hwndNotify = lpcs->hwndParent;
2510 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2511 infoPtr->calendars = Alloc(sizeof(CALENDAR_INFO));
2512 if (!infoPtr->calendars) goto fail;
2514 infoPtr->cal_num = 1;
2516 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
2518 /* initialize info structure */
2519 /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
2521 GetLocalTime(&infoPtr->todaysDate);
2522 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2524 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2525 infoPtr->monthRange = 3;
2527 infoPtr->monthdayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
2528 if (!infoPtr->monthdayState) goto fail;
2530 infoPtr->colors[MCSC_BACKGROUND] = comctl32_color.clrWindow;
2531 infoPtr->colors[MCSC_TEXT] = comctl32_color.clrWindowText;
2532 infoPtr->colors[MCSC_TITLEBK] = comctl32_color.clrActiveCaption;
2533 infoPtr->colors[MCSC_TITLETEXT] = comctl32_color.clrWindow;
2534 infoPtr->colors[MCSC_MONTHBK] = comctl32_color.clrWindow;
2535 infoPtr->colors[MCSC_TRAILINGTEXT] = comctl32_color.clrGrayText;
2537 infoPtr->brushes[MCSC_BACKGROUND] = CreateSolidBrush(infoPtr->colors[MCSC_BACKGROUND]);
2538 infoPtr->brushes[MCSC_TITLEBK] = CreateSolidBrush(infoPtr->colors[MCSC_TITLEBK]);
2539 infoPtr->brushes[MCSC_MONTHBK] = CreateSolidBrush(infoPtr->colors[MCSC_MONTHBK]);
2541 infoPtr->minSel = infoPtr->todaysDate;
2542 infoPtr->maxSel = infoPtr->todaysDate;
2543 infoPtr->calendars[0].month = infoPtr->todaysDate;
2544 infoPtr->isUnicode = TRUE;
2546 /* call MONTHCAL_UpdateSize to set all of the dimensions */
2547 /* of the control */
2548 MONTHCAL_UpdateSize(infoPtr);
2550 /* today auto update timer, to be freed only on control destruction */
2551 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2553 OpenThemeData (infoPtr->hwndSelf, themeClass);
2555 return 0;
2557 fail:
2558 Free(infoPtr->monthdayState);
2559 Free(infoPtr->calendars);
2560 Free(infoPtr);
2561 return 0;
2564 static LRESULT
2565 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2567 /* free month calendar info data */
2568 Free(infoPtr->monthdayState);
2569 Free(infoPtr->calendars);
2570 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2572 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2574 DeleteObject(infoPtr->brushes[MCSC_BACKGROUND]);
2575 DeleteObject(infoPtr->brushes[MCSC_TITLEBK]);
2576 DeleteObject(infoPtr->brushes[MCSC_MONTHBK]);
2578 Free(infoPtr);
2579 return 0;
2583 * Handler for WM_NOTIFY messages
2585 static LRESULT
2586 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2588 /* notification from year edit updown */
2589 if (hdr->code == UDN_DELTAPOS)
2591 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2593 if (hdr->hwndFrom == infoPtr->hWndYearUpDown && nmud->iDelta)
2595 /* year value limits are set up explicitly after updown creation */
2596 MONTHCAL_Scroll(infoPtr, 12 * nmud->iDelta);
2597 MONTHCAL_NotifyDayState(infoPtr);
2598 MONTHCAL_NotifySelectionChange(infoPtr);
2601 return 0;
2604 static inline BOOL
2605 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2607 BOOL prev = infoPtr->isUnicode;
2608 infoPtr->isUnicode = isUnicode;
2609 return prev;
2612 static inline BOOL
2613 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2615 return infoPtr->isUnicode;
2618 static LRESULT WINAPI
2619 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2621 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0);
2623 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2625 if (!infoPtr && (uMsg != WM_CREATE))
2626 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2627 switch(uMsg)
2629 case MCM_GETCURSEL:
2630 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2632 case MCM_SETCURSEL:
2633 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2635 case MCM_GETMAXSELCOUNT:
2636 return MONTHCAL_GetMaxSelCount(infoPtr);
2638 case MCM_SETMAXSELCOUNT:
2639 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2641 case MCM_GETSELRANGE:
2642 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2644 case MCM_SETSELRANGE:
2645 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2647 case MCM_GETMONTHRANGE:
2648 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2650 case MCM_SETDAYSTATE:
2651 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2653 case MCM_GETMINREQRECT:
2654 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2656 case MCM_GETCOLOR:
2657 return MONTHCAL_GetColor(infoPtr, wParam);
2659 case MCM_SETCOLOR:
2660 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2662 case MCM_GETTODAY:
2663 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2665 case MCM_SETTODAY:
2666 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2668 case MCM_HITTEST:
2669 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2671 case MCM_GETFIRSTDAYOFWEEK:
2672 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2674 case MCM_SETFIRSTDAYOFWEEK:
2675 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2677 case MCM_GETRANGE:
2678 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2680 case MCM_SETRANGE:
2681 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2683 case MCM_GETMONTHDELTA:
2684 return MONTHCAL_GetMonthDelta(infoPtr);
2686 case MCM_SETMONTHDELTA:
2687 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2689 case MCM_GETMAXTODAYWIDTH:
2690 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2692 case MCM_SETUNICODEFORMAT:
2693 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2695 case MCM_GETUNICODEFORMAT:
2696 return MONTHCAL_GetUnicodeFormat(infoPtr);
2698 case WM_GETDLGCODE:
2699 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2701 case WM_RBUTTONUP:
2702 return MONTHCAL_RButtonUp(infoPtr, lParam);
2704 case WM_LBUTTONDOWN:
2705 return MONTHCAL_LButtonDown(infoPtr, lParam);
2707 case WM_MOUSEMOVE:
2708 return MONTHCAL_MouseMove(infoPtr, lParam);
2710 case WM_LBUTTONUP:
2711 return MONTHCAL_LButtonUp(infoPtr, lParam);
2713 case WM_PAINT:
2714 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2716 case WM_PRINTCLIENT:
2717 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2719 case WM_ERASEBKGND:
2720 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2722 case WM_SETFOCUS:
2723 return MONTHCAL_SetFocus(infoPtr);
2725 case WM_SIZE:
2726 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2728 case WM_NOTIFY:
2729 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2731 case WM_CREATE:
2732 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2734 case WM_SETFONT:
2735 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2737 case WM_GETFONT:
2738 return MONTHCAL_GetFont(infoPtr);
2740 case WM_TIMER:
2741 return MONTHCAL_Timer(infoPtr, wParam);
2743 case WM_THEMECHANGED:
2744 return theme_changed (infoPtr);
2746 case WM_DESTROY:
2747 return MONTHCAL_Destroy(infoPtr);
2749 case WM_SYSCOLORCHANGE:
2750 COMCTL32_RefreshSysColors();
2751 return 0;
2753 case WM_STYLECHANGED:
2754 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2756 case WM_STYLECHANGING:
2757 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2759 default:
2760 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2761 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2762 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2767 void
2768 MONTHCAL_Register(void)
2770 WNDCLASSW wndClass;
2772 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2773 wndClass.style = CS_GLOBALCLASS;
2774 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2775 wndClass.cbClsExtra = 0;
2776 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2777 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2778 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2779 wndClass.lpszClassName = MONTHCAL_CLASSW;
2781 RegisterClassW(&wndClass);
2785 void
2786 MONTHCAL_Unregister(void)
2788 UnregisterClassW(MONTHCAL_CLASSW, NULL);