comctl32/monthcal: Clean day painting function.
[wine.git] / dlls / comctl32 / monthcal.c
blob58afc9790c5ec1fb14c161f2c7a85cebed77394b
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 COLORREF oldCol = 0;
670 COLORREF oldBk = 0;
671 INT old_bkmode, selection;
673 /* no need to check styles: when selection is not valid, it is set to zero.
674 1 < day < 31, so everything is OK */
675 MONTHCAL_CalcPosFromDay(infoPtr, st, &r);
676 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
678 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
679 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0))
682 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
683 TRACE("%s\n", wine_dbgstr_rect(&r));
684 oldCol = SetTextColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
685 oldBk = SetBkColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
686 FillRect(hdc, &r, infoPtr->brushes[MCSC_TITLEBK]);
688 selection = 1;
690 else
691 selection = 0;
693 SelectObject(hdc, bold ? infoPtr->hBoldFont : infoPtr->hFont);
695 old_bkmode = SetBkMode(hdc, TRANSPARENT);
696 wsprintfW(buf, fmtW, st->wDay);
697 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
698 SetBkMode(hdc, old_bkmode);
700 if (selection)
702 SetTextColor(hdc, oldCol);
703 SetBkColor(hdc, oldBk);
708 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, enum nav_direction button)
710 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
711 RECT *r = button == DIRECTION_FORWARD ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
712 BOOL pressed = button == DIRECTION_FORWARD ? infoPtr->status & MC_NEXTPRESSED :
713 infoPtr->status & MC_PREVPRESSED;
714 if (theme)
716 static const int states[] = {
717 /* Prev button */
718 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
719 /* Next button */
720 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
722 int stateNum = button == DIRECTION_FORWARD ? 3 : 0;
723 if (pressed)
724 stateNum += 1;
725 else
727 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
729 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
731 else
733 int style = button == DIRECTION_FORWARD ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
734 if (pressed)
735 style |= DFCS_PUSHED;
736 else
738 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
741 DrawFrameControl(hdc, r, DFC_SCROLL, style);
744 /* paint a title with buttons and month/year string */
745 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
747 static const WCHAR fmt_monthW[] = { '%','s',' ','%','l','d',0 };
748 RECT *title = &infoPtr->calendars[calIdx].title;
749 const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
750 WCHAR buf_month[80], buf_fmt[80];
751 SIZE sz;
753 /* fill header box */
754 FillRect(hdc, title, infoPtr->brushes[MCSC_TITLEBK]);
756 /* month/year string */
757 SetBkColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
758 SetTextColor(hdc, infoPtr->colors[MCSC_TITLETEXT]);
759 SelectObject(hdc, infoPtr->hBoldFont);
761 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1 + st->wMonth - 1,
762 buf_month, countof(buf_month));
764 wsprintfW(buf_fmt, fmt_monthW, buf_month, st->wYear);
765 DrawTextW(hdc, buf_fmt, strlenW(buf_fmt), title,
766 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
768 /* update title rectangles with current month - used while testing hits */
769 GetTextExtentPoint32W(hdc, buf_fmt, strlenW(buf_fmt), &sz);
770 infoPtr->calendars[calIdx].titlemonth.left = title->right / 2 + title->left / 2 - sz.cx / 2;
771 infoPtr->calendars[calIdx].titleyear.right = title->right / 2 + title->left / 2 + sz.cx / 2;
773 GetTextExtentPoint32W(hdc, buf_month, strlenW(buf_month), &sz);
774 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
775 infoPtr->calendars[calIdx].titleyear.left = infoPtr->calendars[calIdx].titlemonth.right;
778 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
780 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
781 static const WCHAR fmt_weekW[] = { '%','d',0 };
782 INT mindays, weeknum, weeknum1, startofprescal;
783 INT i, prev_month;
784 SYSTEMTIME st;
785 WCHAR buf[80];
786 RECT r;
788 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
790 MONTHCAL_GetMinDate(infoPtr, &st);
791 startofprescal = st.wDay;
792 st = *date;
794 prev_month = date->wMonth - 1;
795 if(prev_month == 0) prev_month = 12;
798 Rules what week to call the first week of a new year:
799 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
800 The week containing Jan 1 is the first week of year
801 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
802 First week of year must contain 4 days of the new year
803 LOCALE_IFIRSTWEEKOFYEAR == 1 (what contries?)
804 The first week of the year must contain only days of the new year
806 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
807 weeknum = atoiW(buf);
808 switch (weeknum)
810 case 1: mindays = 6;
811 break;
812 case 2: mindays = 3;
813 break;
814 case 0: mindays = 0;
815 break;
816 default:
817 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
818 mindays = 0;
821 if (date->wMonth == 1)
823 /* calculate all those exceptions for january */
824 st.wDay = st.wMonth = 1;
825 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
826 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
827 weeknum = 1;
828 else
830 weeknum = 0;
831 for(i = 0; i < 11; i++)
832 weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
834 weeknum += startofprescal + 7;
835 weeknum /= 7;
836 st.wYear -= 1;
837 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
838 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
841 else
843 weeknum = 0;
844 for(i = 0; i < prev_month - 1; i++)
845 weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
847 weeknum += startofprescal + 7;
848 weeknum /= 7;
849 st.wDay = st.wMonth = 1;
850 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
851 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
854 r = infoPtr->calendars[calIdx].weeknums;
856 /* erase whole week numbers area */
857 FillRect(hdc, &r, infoPtr->brushes[MCSC_MONTHBK]);
859 /* reduce rectangle to one week number */
860 r.bottom = r.top + infoPtr->height_increment;
862 for(i = 0; i < 6; i++) {
863 if((i == 0) && (weeknum > 50))
865 wsprintfW(buf, fmt_weekW, weeknum);
866 weeknum = 0;
868 else if((i == 5) && (weeknum > 47))
870 wsprintfW(buf, fmt_weekW, 1);
872 else
873 wsprintfW(buf, fmt_weekW, weeknum + i);
875 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
876 OffsetRect(&r, 0, infoPtr->height_increment);
879 /* line separator for week numbers column */
880 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
881 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
884 /* bottom today date */
885 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
887 if(!(infoPtr->dwStyle & MCS_NOTODAY)) {
888 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
889 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
890 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
891 RECT rtoday;
893 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
894 SYSTEMTIME fake_st;
896 MONTHCAL_GetMaxDate(infoPtr, &fake_st);
897 /* this is always safe cause next month will never fully fit calendar */
898 fake_st.wDay += 1;
899 MONTHCAL_CircleDay(infoPtr, hdc, &fake_st);
901 if (!LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, countof(buf_todayW)))
903 WARN("Can't load resource\n");
904 strcpyW(buf_todayW, todayW);
906 MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
907 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
908 buf_dateW, countof(buf_dateW));
909 SelectObject(hdc, infoPtr->hBoldFont);
911 wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
912 DrawTextW(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
913 DrawTextW(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
915 SelectObject(hdc, infoPtr->hFont);
919 /* today mark + focus */
920 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
922 if((infoPtr->minSel.wMonth == infoPtr->todaysDate.wMonth) &&
923 (infoPtr->minSel.wYear == infoPtr->todaysDate.wYear) &&
924 !(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
926 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
929 if(!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
931 RECT r;
932 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
933 DrawFocusRect(hdc, &r);
937 /* months before first calendar month and after last calendar month */
938 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
940 INT prev_month, mask, length;
941 SYSTEMTIME st_max, st;
943 if (infoPtr->dwStyle & MCS_NOTRAILINGDATES) return;
945 SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
947 prev_month = infoPtr->calendars[0].month.wMonth - 1;
949 /* draw prev month */
950 MONTHCAL_GetMinDate(infoPtr, &st);
951 mask = 1 << (st.wDay-1);
952 /* December and January both 31 days long, so no worries if wrapped */
953 length = MONTHCAL_MonthLength(infoPtr->calendars[0].month.wMonth - 1,
954 infoPtr->calendars[0].month.wYear);
955 while(st.wDay <= length)
957 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[0] & mask, ps);
958 mask <<= 1;
959 st.wDay++;
962 /* draw next month */
963 st = infoPtr->calendars[infoPtr->cal_num-1].month;
964 st.wDay = 1;
965 MONTHCAL_GetNextMonth(&st);
966 MONTHCAL_GetMaxDate(infoPtr, &st_max);
967 mask = 1;
969 while(st.wDay <= st_max.wDay)
971 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[2] & mask, ps);
972 mask <<= 1;
973 st.wDay++;
977 /* paint a calendar area */
978 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
980 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
981 INT prev_month, i, j, length;
982 RECT r, fill_bk_rect;
983 SYSTEMTIME st;
984 WCHAR buf[80];
985 int mask;
987 /* fill whole days area - from week days area to today note rectangle */
988 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
989 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
990 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
992 FillRect(hdc, &fill_bk_rect, infoPtr->brushes[MCSC_MONTHBK]);
994 /* draw line under day abbreviations */
995 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
996 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
997 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
998 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
1000 prev_month = date->wMonth - 1;
1001 if (prev_month == 0) prev_month = 12;
1003 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
1004 infoPtr->calendars[calIdx].weeknums.right;
1006 /* draw day abbreviations */
1007 SelectObject(hdc, infoPtr->hFont);
1008 SetBkColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
1009 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1010 /* rectangle to draw a single day abbreviation within */
1011 r = infoPtr->calendars[calIdx].wdays;
1012 r.right = r.left + infoPtr->width_increment;
1014 i = infoPtr->firstDay;
1015 for(j = 0; j < 7; j++) {
1016 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
1017 DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1018 OffsetRect(&r, infoPtr->width_increment, 0);
1021 /* draw current month */
1022 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1023 st = *date;
1024 st.wDay = 1;
1025 mask = 1;
1026 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1027 while(st.wDay <= length)
1029 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[1] & mask, ps);
1030 mask <<= 1;
1031 st.wDay++;
1035 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1037 COLORREF old_text_clr, old_bk_clr;
1038 HFONT old_font;
1039 INT i;
1041 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1042 old_bk_clr = GetBkColor(hdc);
1043 old_font = GetCurrentObject(hdc, OBJ_FONT);
1045 for (i = 0; i < infoPtr->cal_num; i++)
1047 RECT *title = &infoPtr->calendars[i].title;
1048 RECT r;
1050 /* draw title, redraw all its elements */
1051 if (IntersectRect(&r, &(ps->rcPaint), title))
1052 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1054 /* draw calendar area */
1055 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1056 if (IntersectRect(&r, &(ps->rcPaint), &r))
1057 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1059 /* week numbers */
1060 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1063 /* partially visible months */
1064 MONTHCAL_PaintLeadTrailMonths(infoPtr, hdc, ps);
1066 /* focus and today rectangle */
1067 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1069 /* today at the bottom left */
1070 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1072 /* navigation buttons */
1073 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_BACKWARD);
1074 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_FORWARD);
1076 /* restore context */
1077 SetBkColor(hdc, old_bk_clr);
1078 SelectObject(hdc, old_font);
1079 SetTextColor(hdc, old_text_clr);
1082 static LRESULT
1083 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, RECT *rect)
1085 TRACE("rect %p\n", rect);
1087 if(!rect) return FALSE;
1089 *rect = infoPtr->calendars[0].title;
1090 rect->bottom = infoPtr->calendars[0].days.bottom + infoPtr->todayrect.bottom -
1091 infoPtr->todayrect.top;
1093 AdjustWindowRect(rect, infoPtr->dwStyle, FALSE);
1095 /* minimal rectangle is zero based */
1096 OffsetRect(rect, -rect->left, -rect->top);
1098 TRACE("%s\n", wine_dbgstr_rect(rect));
1100 return TRUE;
1103 static COLORREF
1104 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, UINT index)
1106 TRACE("%p, %d\n", infoPtr, index);
1108 if (index > MCSC_TRAILINGTEXT) return -1;
1109 return infoPtr->colors[index];
1112 static LRESULT
1113 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, UINT index, COLORREF color)
1115 COLORREF prev;
1117 TRACE("%p, %d: color %08x\n", infoPtr, index, color);
1119 if (index > MCSC_TRAILINGTEXT) return -1;
1121 prev = infoPtr->colors[index];
1122 infoPtr->colors[index] = color;
1124 /* update cached brush */
1125 if (index == MCSC_BACKGROUND || index == MCSC_TITLEBK || index == MCSC_MONTHBK)
1127 DeleteObject(infoPtr->brushes[index]);
1128 infoPtr->brushes[index] = CreateSolidBrush(color);
1131 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND ? TRUE : FALSE);
1132 return prev;
1135 static LRESULT
1136 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1138 TRACE("\n");
1140 if(infoPtr->delta)
1141 return infoPtr->delta;
1142 else
1143 return infoPtr->visible;
1147 static LRESULT
1148 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1150 INT prev = infoPtr->delta;
1152 TRACE("delta %d\n", delta);
1154 infoPtr->delta = delta;
1155 return prev;
1159 static inline LRESULT
1160 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1162 int day;
1164 /* convert from SYSTEMTIME to locale format */
1165 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1167 return MAKELONG(day, infoPtr->firstDaySet);
1171 /* Sets the first day of the week that will appear in the control
1174 * PARAMETERS:
1175 * [I] infoPtr : valid pointer to control data
1176 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1179 * RETURN VALUE:
1180 * Low word contains previous first day,
1181 * high word indicates was first day forced with this message before or is
1182 * locale difined (TRUE - was forced, FALSE - wasn't).
1184 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1185 * FIXME: we need more error checking here
1187 static LRESULT
1188 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1190 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1191 int new_day;
1193 TRACE("%d\n", day);
1195 if(day == -1)
1197 WCHAR buf[80];
1199 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1200 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1202 new_day = atoiW(buf);
1204 infoPtr->firstDaySet = FALSE;
1206 else if(day >= 7)
1208 new_day = 6; /* max first day allowed */
1209 infoPtr->firstDaySet = TRUE;
1211 else
1213 /* Native behaviour for that case is broken: invalid date number >31
1214 got displayed at (0,0) position, current month starts always from
1215 (1,0) position. Should be implemented here as well only if there's
1216 nothing else to do. */
1217 if (day < -1)
1218 FIXME("No bug compatibility for day=%d\n", day);
1220 new_day = day;
1221 infoPtr->firstDaySet = TRUE;
1224 /* convert from locale to SYSTEMTIME format */
1225 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1227 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1229 return prev;
1233 static LRESULT
1234 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
1236 TRACE("flag=%d, st=%p\n", flag, st);
1238 if(st)
1240 switch (flag) {
1241 case GMR_VISIBLE:
1243 st[0] = infoPtr->calendars[0].month;
1244 st[1] = infoPtr->calendars[infoPtr->cal_num-1].month;
1246 if (st[0].wMonth == min_allowed_date.wMonth &&
1247 st[0].wYear == min_allowed_date.wYear)
1249 st[0].wDay = min_allowed_date.wDay;
1251 else
1252 st[0].wDay = 1;
1253 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
1255 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
1256 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
1258 return infoPtr->cal_num;
1260 case GMR_DAYSTATE:
1262 MONTHCAL_GetMinDate(infoPtr, &st[0]);
1263 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
1264 break;
1266 default:
1267 WARN("Unknown flag value, got %d\n", flag);
1271 return infoPtr->monthRange;
1275 static LRESULT
1276 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1278 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1282 static LRESULT
1283 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1285 FILETIME ft_min, ft_max;
1287 TRACE("%x %p\n", limits, range);
1289 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1290 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1291 return FALSE;
1293 if (limits & GDTR_MIN)
1295 if (!MONTHCAL_ValidateTime(&range[0]))
1296 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1298 infoPtr->minDate = range[0];
1299 infoPtr->rangeValid |= GDTR_MIN;
1301 if (limits & GDTR_MAX)
1303 if (!MONTHCAL_ValidateTime(&range[1]))
1304 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1306 infoPtr->maxDate = range[1];
1307 infoPtr->rangeValid |= GDTR_MAX;
1310 /* Only one limit set - we are done */
1311 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1312 return TRUE;
1314 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1315 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1317 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1319 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1321 /* Native swaps limits only when both limits are being set. */
1322 SYSTEMTIME st_tmp = infoPtr->minDate;
1323 infoPtr->minDate = infoPtr->maxDate;
1324 infoPtr->maxDate = st_tmp;
1326 else
1328 /* reset the other limit */
1329 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1330 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1331 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1335 return TRUE;
1339 static LRESULT
1340 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1342 TRACE("%p\n", range);
1344 if(!range) return FALSE;
1346 range[1] = infoPtr->maxDate;
1347 range[0] = infoPtr->minDate;
1349 return infoPtr->rangeValid;
1353 static LRESULT
1354 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1356 TRACE("%p %d %p\n", infoPtr, months, states);
1357 if(months != infoPtr->monthRange) return 0;
1359 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1361 return 1;
1364 static LRESULT
1365 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1367 TRACE("%p\n", curSel);
1368 if(!curSel) return FALSE;
1369 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1371 *curSel = infoPtr->minSel;
1372 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1373 return TRUE;
1376 static LRESULT
1377 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1379 SYSTEMTIME prev = infoPtr->minSel;
1380 WORD day;
1382 TRACE("%p\n", curSel);
1383 if(!curSel) return FALSE;
1384 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1386 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1387 /* exit earlier if selection equals current */
1388 if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1390 if(!MONTHCAL_IsDateInValidRange(infoPtr, curSel, FALSE)) return FALSE;
1392 infoPtr->calendars[0].month = *curSel;
1393 infoPtr->minSel = *curSel;
1394 infoPtr->maxSel = *curSel;
1396 /* if selection is still in current month, reduce rectangle */
1397 day = prev.wDay;
1398 prev.wDay = curSel->wDay;
1399 if (MONTHCAL_IsDateEqual(&prev, curSel))
1401 RECT r_prev, r_new;
1403 prev.wDay = day;
1404 MONTHCAL_CalcPosFromDay(infoPtr, &prev, &r_prev);
1405 MONTHCAL_CalcPosFromDay(infoPtr, curSel, &r_new);
1407 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1408 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1410 else
1411 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1413 return TRUE;
1417 static LRESULT
1418 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1420 return infoPtr->maxSelCount;
1424 static LRESULT
1425 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1427 TRACE("%d\n", max);
1429 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1430 if(max <= 0) return FALSE;
1432 infoPtr->maxSelCount = max;
1434 return TRUE;
1438 static LRESULT
1439 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1441 TRACE("%p\n", range);
1443 if(!range) return FALSE;
1445 if(infoPtr->dwStyle & MCS_MULTISELECT)
1447 range[1] = infoPtr->maxSel;
1448 range[0] = infoPtr->minSel;
1449 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1450 return TRUE;
1453 return FALSE;
1457 static LRESULT
1458 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1460 TRACE("%p\n", range);
1462 if(!range) return FALSE;
1464 if(infoPtr->dwStyle & MCS_MULTISELECT)
1466 SYSTEMTIME old_range[2];
1468 /* adjust timestamps */
1469 if(!MONTHCAL_ValidateTime(&range[0]))
1470 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1471 if(!MONTHCAL_ValidateTime(&range[1]))
1472 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1474 /* maximum range exceeded */
1475 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1477 old_range[0] = infoPtr->minSel;
1478 old_range[1] = infoPtr->maxSel;
1480 /* swap if min > max */
1481 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1483 infoPtr->minSel = range[0];
1484 infoPtr->maxSel = range[1];
1486 else
1488 infoPtr->minSel = range[1];
1489 infoPtr->maxSel = range[0];
1491 infoPtr->calendars[0].month = infoPtr->minSel;
1493 /* update day of week */
1494 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1495 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1497 /* redraw if bounds changed */
1498 /* FIXME: no actual need to redraw everything */
1499 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1500 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1502 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1505 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1506 return TRUE;
1509 return FALSE;
1513 static LRESULT
1514 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1516 TRACE("%p\n", today);
1518 if(!today) return FALSE;
1519 *today = infoPtr->todaysDate;
1520 return TRUE;
1523 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1525 * RETURN VALUE
1527 * TRUE - today date changed
1528 * FALSE - today date isn't changed
1530 static BOOL
1531 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1533 RECT new_r, old_r;
1535 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1537 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->todaysDate, &old_r);
1538 MONTHCAL_CalcPosFromDay(infoPtr, today, &new_r);
1540 infoPtr->todaysDate = *today;
1542 /* only two days need redrawing */
1543 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1544 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1545 return TRUE;
1548 /* MCM_SETTODAT handler */
1549 static LRESULT
1550 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1552 TRACE("%p\n", today);
1554 if(!today) return FALSE;
1556 /* remember if date was set successfully */
1557 if(MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1559 return TRUE;
1562 /* returns calendar index containing specified point, or -1 if it's background */
1563 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1565 RECT r;
1566 INT i;
1568 for (i = 0; i < infoPtr->cal_num; i++)
1570 /* whole bounding rectangle allows some optimization to compute */
1571 r.left = infoPtr->calendars[i].title.left;
1572 r.top = infoPtr->calendars[i].title.top;
1573 r.bottom = infoPtr->calendars[i].days.bottom;
1574 r.right = infoPtr->calendars[i].days.right;
1576 if (PtInRect(&r, *pt)) return i;
1579 return -1;
1582 static inline UINT fill_hittest_info(const MCHITTESTINFO *src, MCHITTESTINFO *dest)
1584 dest->uHit = src->uHit;
1585 dest->st = src->st;
1587 if (dest->cbSize == sizeof(MCHITTESTINFO))
1588 memcpy(&dest->rc, &src->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1590 return src->uHit;
1593 static LRESULT
1594 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1596 INT day, wday, wnum, calIdx;
1597 MCHITTESTINFO htinfo;
1598 SYSTEMTIME ht_month;
1599 UINT x, y;
1601 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1603 x = lpht->pt.x;
1604 y = lpht->pt.y;
1606 htinfo.st = st_null;
1608 /* we should preserve passed fields if hit area doesn't need them */
1609 if (lpht->cbSize == sizeof(MCHITTESTINFO))
1610 memcpy(&htinfo.rc, &lpht->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1612 /* Comment in for debugging...
1613 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,
1614 infoPtr->wdays.left, infoPtr->wdays.right,
1615 infoPtr->wdays.top, infoPtr->wdays.bottom,
1616 infoPtr->days.left, infoPtr->days.right,
1617 infoPtr->days.top, infoPtr->days.bottom,
1618 infoPtr->todayrect.left, infoPtr->todayrect.right,
1619 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1620 infoPtr->weeknums.left, infoPtr->weeknums.right,
1621 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1624 /* guess in what calendar we are */
1625 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1626 if (calIdx == -1)
1628 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1630 htinfo.uHit = MCHT_TODAYLINK;
1631 htinfo.rc = infoPtr->todayrect;
1633 else
1634 /* outside of calendar area? What's left must be background :-) */
1635 htinfo.uHit = MCHT_CALENDARBK;
1637 return fill_hittest_info(&htinfo, lpht);
1640 ht_month = infoPtr->calendars[calIdx].month;
1642 /* are we in the header? */
1643 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1644 /* FIXME: buttons hittesting could be optimized cause maximum
1645 two calendars have buttons */
1646 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1648 htinfo.uHit = MCHT_TITLEBTNPREV;
1649 htinfo.rc = infoPtr->titlebtnprev;
1651 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1653 htinfo.uHit = MCHT_TITLEBTNNEXT;
1654 htinfo.rc = infoPtr->titlebtnnext;
1656 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1658 htinfo.uHit = MCHT_TITLEMONTH;
1659 htinfo.rc = infoPtr->calendars[calIdx].titlemonth;
1660 htinfo.iOffset = calIdx;
1662 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1664 htinfo.uHit = MCHT_TITLEYEAR;
1665 htinfo.rc = infoPtr->calendars[calIdx].titleyear;
1666 htinfo.iOffset = calIdx;
1668 else
1670 htinfo.uHit = MCHT_TITLE;
1671 htinfo.rc = infoPtr->calendars[calIdx].title;
1672 htinfo.iOffset = calIdx;
1675 return fill_hittest_info(&htinfo, lpht);
1678 /* days area (including week days and week numbers */
1679 day = MONTHCAL_CalcDayFromPos(infoPtr, x, y, &wday, &wnum);
1680 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1682 htinfo.uHit = MCHT_CALENDARDAY;
1683 htinfo.iOffset = calIdx;
1684 htinfo.st.wYear = ht_month.wYear;
1685 htinfo.st.wMonth = (day < 1) ? ht_month.wMonth -1 : ht_month.wMonth;
1686 htinfo.st.wDay = (day < 1) ?
1687 MONTHCAL_MonthLength(ht_month.wMonth-1, ht_month.wYear) - day : day;
1689 MONTHCAL_CalcDayXY(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow);
1691 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1693 htinfo.uHit = MCHT_CALENDARWEEKNUM;
1694 htinfo.st.wYear = ht_month.wYear;
1695 htinfo.iOffset = calIdx;
1697 if (day < 1)
1699 htinfo.st.wMonth = ht_month.wMonth - 1;
1700 htinfo.st.wDay = MONTHCAL_MonthLength(ht_month.wMonth-1, ht_month.wYear) - day;
1702 else if (day > MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear))
1704 htinfo.st.wMonth = ht_month.wMonth + 1;
1705 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear);
1707 else
1709 htinfo.st.wMonth = ht_month.wMonth;
1710 htinfo.st.wDay = day;
1713 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1715 htinfo.iOffset = calIdx;
1716 htinfo.st.wYear = ht_month.wYear;
1717 htinfo.st.wMonth = ht_month.wMonth;
1718 if (day < 1)
1720 htinfo.uHit = MCHT_CALENDARDATEPREV;
1721 MONTHCAL_GetPrevMonth(&htinfo.st);
1722 htinfo.st.wDay = MONTHCAL_MonthLength(htinfo.st.wMonth, htinfo.st.wYear) + day;
1724 else if (day > MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear))
1726 htinfo.uHit = MCHT_CALENDARDATENEXT;
1727 MONTHCAL_GetNextMonth(&htinfo.st);
1728 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear);
1730 else
1732 htinfo.uHit = MCHT_CALENDARDATE;
1733 htinfo.st.wDay = day;
1736 MONTHCAL_CalcDayXY(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow);
1737 MONTHCAL_CalcDayRect(infoPtr, &htinfo.rc, htinfo.iCol, htinfo.iRow);
1738 /* always update day of week */
1739 MONTHCAL_CalculateDayOfWeek(&htinfo.st, TRUE);
1742 return fill_hittest_info(&htinfo, lpht);
1745 /* MCN_GETDAYSTATE notification helper */
1746 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1748 if(infoPtr->dwStyle & MCS_DAYSTATE) {
1749 NMDAYSTATE nmds;
1751 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1752 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1753 nmds.nmhdr.code = MCN_GETDAYSTATE;
1754 nmds.cDayState = infoPtr->monthRange;
1755 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1757 nmds.stStart = infoPtr->todaysDate;
1758 nmds.stStart.wYear = infoPtr->minSel.wYear;
1759 nmds.stStart.wMonth = infoPtr->minSel.wMonth;
1760 nmds.stStart.wDay = 1;
1762 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1763 memcpy(infoPtr->monthdayState, nmds.prgDayState, infoPtr->monthRange*sizeof(MONTHDAYSTATE));
1765 Free(nmds.prgDayState);
1769 /* no valid range check performed */
1770 static void MONTHCAL_Scroll(MONTHCAL_INFO *infoPtr, INT delta)
1772 INT i, selIdx = -1;
1774 for(i = 0; i < infoPtr->cal_num; i++)
1776 /* save selection position to shift it later */
1777 if (selIdx == -1 && MONTHCAL_CompareMonths(&infoPtr->minSel, &infoPtr->calendars[i].month) == 0)
1778 selIdx = i;
1780 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, delta);
1783 /* selection is always shifted to first calendar */
1784 if(infoPtr->dwStyle & MCS_MULTISELECT)
1786 SYSTEMTIME range[2];
1788 MONTHCAL_GetSelRange(infoPtr, range);
1789 MONTHCAL_GetMonth(&range[0], delta - selIdx);
1790 MONTHCAL_GetMonth(&range[1], delta - selIdx);
1791 MONTHCAL_SetSelRange(infoPtr, range);
1793 else
1795 SYSTEMTIME st = infoPtr->minSel;
1797 MONTHCAL_GetMonth(&st, delta - selIdx);
1798 MONTHCAL_SetCurSel(infoPtr, &st);
1802 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
1804 INT delta = infoPtr->delta ? infoPtr->delta : infoPtr->cal_num;
1805 SYSTEMTIME st;
1807 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
1809 /* check if change allowed by range set */
1810 if(direction == DIRECTION_BACKWARD)
1812 st = infoPtr->calendars[0].month;
1813 MONTHCAL_GetMonth(&st, -delta);
1815 else
1817 st = infoPtr->calendars[infoPtr->cal_num-1].month;
1818 MONTHCAL_GetMonth(&st, delta);
1821 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
1823 MONTHCAL_Scroll(infoPtr, direction == DIRECTION_BACKWARD ? -delta : delta);
1824 MONTHCAL_NotifyDayState(infoPtr);
1825 MONTHCAL_NotifySelectionChange(infoPtr);
1828 static LRESULT
1829 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1831 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1832 HMENU hMenu;
1833 POINT menupoint;
1834 WCHAR buf[32];
1836 hMenu = CreatePopupMenu();
1837 if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1839 WARN("Can't load resource\n");
1840 strcpyW(buf, todayW);
1842 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1843 menupoint.x = (short)LOWORD(lParam);
1844 menupoint.y = (short)HIWORD(lParam);
1845 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1846 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
1847 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1849 infoPtr->calendars[0].month = infoPtr->todaysDate;
1850 infoPtr->minSel = infoPtr->todaysDate;
1851 infoPtr->maxSel = infoPtr->todaysDate;
1852 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1855 return 0;
1858 /***
1859 * DESCRIPTION:
1860 * Subclassed edit control windproc function
1862 * PARAMETER(S):
1863 * [I] hwnd : the edit window handle
1864 * [I] uMsg : the message that is to be processed
1865 * [I] wParam : first message parameter
1866 * [I] lParam : second message parameter
1869 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1871 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
1873 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
1874 hwnd, uMsg, wParam, lParam);
1876 switch (uMsg)
1878 case WM_GETDLGCODE:
1879 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
1881 case WM_DESTROY:
1883 WNDPROC editProc = infoPtr->EditWndProc;
1884 infoPtr->EditWndProc = NULL;
1885 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
1886 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
1889 case WM_KILLFOCUS:
1890 break;
1892 case WM_KEYDOWN:
1893 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
1894 break;
1896 default:
1897 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
1900 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
1901 SendMessageW(hwnd, WM_CLOSE, 0, 0);
1902 return 0;
1905 /* creates updown control and edit box */
1906 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr, INT calIdx)
1908 RECT *rc = &infoPtr->calendars[calIdx].titleyear;
1909 RECT *title = &infoPtr->calendars[calIdx].title;
1911 infoPtr->hWndYearEdit =
1912 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
1913 rc->left + 3, (title->bottom + title->top - infoPtr->textHeight) / 2,
1914 rc->right - rc->left + 4,
1915 infoPtr->textHeight, infoPtr->hwndSelf,
1916 NULL, NULL, NULL);
1918 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
1920 infoPtr->hWndYearUpDown =
1921 CreateWindowExW(0, UPDOWN_CLASSW, 0,
1922 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
1923 rc->right + 7, (title->bottom + title->top - infoPtr->textHeight) / 2,
1924 18, infoPtr->textHeight, infoPtr->hwndSelf,
1925 NULL, NULL, NULL);
1927 /* attach edit box */
1928 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
1929 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
1930 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
1931 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->calendars[calIdx].month.wYear);
1933 /* subclass edit box */
1934 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
1935 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
1937 SetFocus(infoPtr->hWndYearEdit);
1940 static LRESULT
1941 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1943 MCHITTESTINFO ht;
1944 DWORD hit;
1946 /* Actually we don't need input focus for calendar, this is used to kill
1947 year updown and its buddy edit box */
1948 if (IsWindow(infoPtr->hWndYearUpDown))
1950 SetFocus(infoPtr->hwndSelf);
1951 return 0;
1954 SetCapture(infoPtr->hwndSelf);
1956 ht.cbSize = sizeof(MCHITTESTINFO);
1957 ht.pt.x = (short)LOWORD(lParam);
1958 ht.pt.y = (short)HIWORD(lParam);
1960 hit = MONTHCAL_HitTest(infoPtr, &ht);
1962 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
1964 switch(hit)
1966 case MCHT_TITLEBTNNEXT:
1967 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
1968 infoPtr->status = MC_NEXTPRESSED;
1969 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1970 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1971 return 0;
1973 case MCHT_TITLEBTNPREV:
1974 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
1975 infoPtr->status = MC_PREVPRESSED;
1976 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1977 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1978 return 0;
1980 case MCHT_TITLEMONTH:
1982 HMENU hMenu = CreatePopupMenu();
1983 WCHAR buf[32];
1984 POINT menupoint;
1985 INT i;
1987 for (i = 0; i < 12; i++)
1989 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
1990 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
1992 menupoint.x = ht.pt.x;
1993 menupoint.y = ht.pt.y;
1994 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1995 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
1996 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
1998 if ((i > 0) && (i < 13) && infoPtr->calendars[ht.iOffset].month.wMonth != i)
2000 INT delta = i - infoPtr->calendars[ht.iOffset].month.wMonth;
2001 SYSTEMTIME st;
2003 /* check if change allowed by range set */
2004 st = delta < 0 ? infoPtr->calendars[0].month :
2005 infoPtr->calendars[infoPtr->cal_num-1].month;
2006 MONTHCAL_GetMonth(&st, delta);
2008 if (MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE))
2010 MONTHCAL_Scroll(infoPtr, delta);
2011 MONTHCAL_NotifyDayState(infoPtr);
2012 MONTHCAL_NotifySelectionChange(infoPtr);
2013 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2016 return 0;
2018 case MCHT_TITLEYEAR:
2020 MONTHCAL_EditYear(infoPtr, ht.iOffset);
2021 return 0;
2023 case MCHT_TODAYLINK:
2025 infoPtr->calendars[0].month = infoPtr->todaysDate;
2026 infoPtr->minSel = infoPtr->todaysDate;
2027 infoPtr->maxSel = infoPtr->todaysDate;
2028 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2030 MONTHCAL_NotifySelectionChange(infoPtr);
2031 MONTHCAL_NotifySelect(infoPtr);
2032 return 0;
2034 case MCHT_CALENDARDATENEXT:
2035 case MCHT_CALENDARDATEPREV:
2036 case MCHT_CALENDARDATE:
2038 SYSTEMTIME st[2];
2040 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2042 st[0] = st[1] = ht.st;
2043 /* clear selection range */
2044 MONTHCAL_SetSelRange(infoPtr, st);
2046 infoPtr->status = MC_SEL_LBUTDOWN;
2047 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
2048 return 0;
2052 return 1;
2056 static LRESULT
2057 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2059 NMHDR nmhdr;
2060 MCHITTESTINFO ht;
2061 DWORD hit;
2063 TRACE("\n");
2065 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2066 RECT *r;
2068 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2069 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2070 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2072 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2075 ReleaseCapture();
2077 /* always send NM_RELEASEDCAPTURE notification */
2078 nmhdr.hwndFrom = infoPtr->hwndSelf;
2079 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2080 nmhdr.code = NM_RELEASEDCAPTURE;
2081 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2083 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2085 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2087 ht.cbSize = sizeof(MCHITTESTINFO);
2088 ht.pt.x = (short)LOWORD(lParam);
2089 ht.pt.y = (short)HIWORD(lParam);
2090 hit = MONTHCAL_HitTest(infoPtr, &ht);
2092 infoPtr->status = MC_SEL_LBUTUP;
2093 MONTHCAL_SetDayFocus(infoPtr, NULL);
2095 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2097 SYSTEMTIME sel = infoPtr->minSel;
2099 /* will be invalidated here */
2100 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2102 /* send MCN_SELCHANGE only if new date selected */
2103 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2104 MONTHCAL_NotifySelectionChange(infoPtr);
2106 MONTHCAL_NotifySelect(infoPtr);
2109 return 0;
2113 static LRESULT
2114 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2116 TRACE("%ld\n", id);
2118 switch(id) {
2119 case MC_PREVNEXTMONTHTIMER:
2120 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2121 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2122 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2123 break;
2124 case MC_TODAYUPDATETIMER:
2126 SYSTEMTIME st;
2128 if(infoPtr->todaySet) return 0;
2130 GetLocalTime(&st);
2131 MONTHCAL_UpdateToday(infoPtr, &st);
2133 /* notification sent anyway */
2134 MONTHCAL_NotifySelectionChange(infoPtr);
2136 return 0;
2138 default:
2139 ERR("got unknown timer %ld\n", id);
2140 break;
2143 return 0;
2147 static LRESULT
2148 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2150 MCHITTESTINFO ht;
2151 SYSTEMTIME st_ht;
2152 INT hit;
2153 RECT r;
2155 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2157 ht.cbSize = sizeof(MCHITTESTINFO);
2158 ht.pt.x = (short)LOWORD(lParam);
2159 ht.pt.y = (short)HIWORD(lParam);
2161 hit = MONTHCAL_HitTest(infoPtr, &ht);
2163 /* not on the calendar date numbers? bail out */
2164 TRACE("hit:%x\n",hit);
2165 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2167 MONTHCAL_SetDayFocus(infoPtr, NULL);
2168 return 0;
2171 st_ht = ht.st;
2173 /* if pointer is over focused day still there's nothing to do */
2174 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2176 MONTHCAL_CalcPosFromDay(infoPtr, &ht.st, &r);
2178 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2179 SYSTEMTIME st[2];
2181 MONTHCAL_GetSelRange(infoPtr, st);
2183 /* If we're still at the first selected date and range is empty, return.
2184 If range isn't empty we should change range to a single firstSel */
2185 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2186 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2188 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2190 st[0] = infoPtr->firstSel;
2191 /* we should overwrite timestamp here */
2192 MONTHCAL_CopyDate(&st_ht, &st[1]);
2194 /* bounds will be swapped here if needed */
2195 MONTHCAL_SetSelRange(infoPtr, st);
2197 return 0;
2200 done:
2202 /* FIXME: this should specify a rectangle containing only the days that changed
2203 using InvalidateRect */
2204 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2206 return 0;
2210 static LRESULT
2211 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2213 HDC hdc;
2214 PAINTSTRUCT ps;
2216 if (hdc_paint)
2218 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2219 hdc = hdc_paint;
2221 else
2222 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2224 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2225 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2226 return 0;
2229 static LRESULT
2230 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2232 RECT rc;
2234 if (!GetClipBox(hdc, &rc)) return FALSE;
2236 /* fill background */
2237 FillRect(hdc, &rc, infoPtr->brushes[MCSC_BACKGROUND]);
2239 return TRUE;
2242 static LRESULT
2243 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2245 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2247 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2248 return 0;
2250 if (options & PRF_ERASEBKGND)
2251 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2253 if (options & PRF_CLIENT)
2254 MONTHCAL_Paint(infoPtr, hdc);
2256 return 0;
2259 static LRESULT
2260 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2262 TRACE("\n");
2264 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2266 return 0;
2269 /* sets the size information */
2270 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2272 static const WCHAR O0W[] = { '0','0',0 };
2273 HDC hdc = GetDC(infoPtr->hwndSelf);
2274 RECT *title=&infoPtr->calendars[0].title;
2275 RECT *prev=&infoPtr->titlebtnprev;
2276 RECT *next=&infoPtr->titlebtnnext;
2277 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2278 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2279 RECT *wdays=&infoPtr->calendars[0].wdays;
2280 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2281 RECT *days=&infoPtr->calendars[0].days;
2282 RECT *todayrect=&infoPtr->todayrect;
2283 SIZE size, sz;
2284 TEXTMETRICW tm;
2285 HFONT currentFont;
2286 INT xdiv, dx, dy, i;
2287 RECT rcClient;
2288 WCHAR buff[80];
2290 GetClientRect(infoPtr->hwndSelf, &rcClient);
2292 currentFont = SelectObject(hdc, infoPtr->hFont);
2294 /* get the height and width of each day's text */
2295 GetTextMetricsW(hdc, &tm);
2296 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2298 /* find largest abbreviated day name for current locale */
2299 size.cx = sz.cx = 0;
2300 for (i = 0; i < 7; i++)
2302 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + i,
2303 buff, countof(buff)))
2305 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2306 if (sz.cx > size.cx) size.cx = sz.cx;
2308 else /* locale independent fallback on failure */
2310 static const WCHAR SunW[] = { 'S','u','n',0 };
2312 GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2313 break;
2317 infoPtr->textWidth = size.cx + 2;
2319 /* recalculate the height and width increments and offsets */
2320 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2322 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2324 infoPtr->width_increment = size.cx * 2 + 4;
2325 infoPtr->height_increment = infoPtr->textHeight;
2327 /* calculate title area */
2328 title->top = 0;
2329 title->bottom = 3 * infoPtr->height_increment / 2;
2330 title->left = 0;
2331 title->right = infoPtr->width_increment * xdiv;
2333 /* set the dimensions of the next and previous buttons and center */
2334 /* the month text vertically */
2335 prev->top = next->top = title->top + 4;
2336 prev->bottom = next->bottom = title->bottom - 4;
2337 prev->left = title->left + 4;
2338 prev->right = prev->left + (title->bottom - title->top);
2339 next->right = title->right - 4;
2340 next->left = next->right - (title->bottom - title->top);
2342 /* titlemonth->left and right change based upon the current month */
2343 /* and are recalculated in refresh as the current month may change */
2344 /* without the control being resized */
2345 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2346 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2348 /* setup the dimensions of the rectangle we draw the names of the */
2349 /* days of the week in */
2350 weeknumrect->left = 0;
2352 if(infoPtr->dwStyle & MCS_WEEKNUMBERS)
2353 weeknumrect->right = prev->right;
2354 else
2355 weeknumrect->right = weeknumrect->left;
2357 wdays->left = days->left = weeknumrect->right;
2358 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2359 wdays->top = title->bottom;
2360 wdays->bottom = wdays->top + infoPtr->height_increment;
2362 days->top = weeknumrect->top = wdays->bottom;
2363 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2365 todayrect->left = 0;
2366 todayrect->right = title->right;
2367 todayrect->top = days->bottom;
2368 todayrect->bottom = days->bottom + infoPtr->height_increment;
2370 /* offset all rectangles to center in client area */
2371 dx = (rcClient.right - title->right) / 2;
2372 dy = (rcClient.bottom - todayrect->bottom) / 2;
2374 /* if calendar doesn't fit client area show it at left/top bounds */
2375 if (title->left + dx < 0) dx = 0;
2376 if (title->top + dy < 0) dy = 0;
2378 if (dx != 0 || dy != 0)
2380 OffsetRect(title, dx, dy);
2381 OffsetRect(prev, dx, dy);
2382 OffsetRect(next, dx, dy);
2383 OffsetRect(titlemonth, dx, dy);
2384 OffsetRect(titleyear, dx, dy);
2385 OffsetRect(wdays, dx, dy);
2386 OffsetRect(weeknumrect, dx, dy);
2387 OffsetRect(days, dx, dy);
2388 OffsetRect(todayrect, dx, dy);
2391 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2392 infoPtr->width_increment,infoPtr->height_increment,
2393 wine_dbgstr_rect(&rcClient),
2394 wine_dbgstr_rect(title),
2395 wine_dbgstr_rect(wdays),
2396 wine_dbgstr_rect(days),
2397 wine_dbgstr_rect(todayrect));
2399 /* restore the originally selected font */
2400 SelectObject(hdc, currentFont);
2402 ReleaseDC(infoPtr->hwndSelf, hdc);
2405 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2407 TRACE("(width=%d, height=%d)\n", Width, Height);
2409 MONTHCAL_UpdateSize(infoPtr);
2410 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2412 return 0;
2415 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2417 return (LRESULT)infoPtr->hFont;
2420 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2422 HFONT hOldFont;
2423 LOGFONTW lf;
2425 if (!hFont) return 0;
2427 hOldFont = infoPtr->hFont;
2428 infoPtr->hFont = hFont;
2430 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2431 lf.lfWeight = FW_BOLD;
2432 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2434 MONTHCAL_UpdateSize(infoPtr);
2436 if (redraw)
2437 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2439 return (LRESULT)hOldFont;
2442 /* update theme after a WM_THEMECHANGED message */
2443 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2445 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2446 CloseThemeData (theme);
2447 OpenThemeData (infoPtr->hwndSelf, themeClass);
2448 return 0;
2451 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2452 const STYLESTRUCT *lpss)
2454 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2455 wStyleType, lpss->styleOld, lpss->styleNew);
2457 if (wStyleType != GWL_STYLE) return 0;
2459 infoPtr->dwStyle = lpss->styleNew;
2461 /* make room for week numbers */
2462 if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2463 MONTHCAL_UpdateSize(infoPtr);
2465 return 0;
2468 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2469 STYLESTRUCT *lpss)
2471 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2472 wStyleType, lpss->styleOld, lpss->styleNew);
2474 /* block MCS_MULTISELECT change */
2475 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2477 if (lpss->styleOld & MCS_MULTISELECT)
2478 lpss->styleNew |= MCS_MULTISELECT;
2479 else
2480 lpss->styleNew &= ~MCS_MULTISELECT;
2483 return 0;
2486 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2487 static LRESULT
2488 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2490 MONTHCAL_INFO *infoPtr;
2492 /* allocate memory for info structure */
2493 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2494 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2496 if (infoPtr == NULL) {
2497 ERR("could not allocate info memory!\n");
2498 return 0;
2501 infoPtr->hwndSelf = hwnd;
2502 infoPtr->hwndNotify = lpcs->hwndParent;
2503 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2504 infoPtr->calendars = Alloc(sizeof(CALENDAR_INFO));
2505 if (!infoPtr->calendars) goto fail;
2507 infoPtr->cal_num = 1;
2509 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
2511 /* initialize info structure */
2512 /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
2514 GetLocalTime(&infoPtr->todaysDate);
2515 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2517 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2518 infoPtr->monthRange = 3;
2520 infoPtr->monthdayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
2521 if (!infoPtr->monthdayState) goto fail;
2523 infoPtr->colors[MCSC_BACKGROUND] = comctl32_color.clrWindow;
2524 infoPtr->colors[MCSC_TEXT] = comctl32_color.clrWindowText;
2525 infoPtr->colors[MCSC_TITLEBK] = comctl32_color.clrActiveCaption;
2526 infoPtr->colors[MCSC_TITLETEXT] = comctl32_color.clrWindow;
2527 infoPtr->colors[MCSC_MONTHBK] = comctl32_color.clrWindow;
2528 infoPtr->colors[MCSC_TRAILINGTEXT] = comctl32_color.clrGrayText;
2530 infoPtr->brushes[MCSC_BACKGROUND] = CreateSolidBrush(infoPtr->colors[MCSC_BACKGROUND]);
2531 infoPtr->brushes[MCSC_TITLEBK] = CreateSolidBrush(infoPtr->colors[MCSC_TITLEBK]);
2532 infoPtr->brushes[MCSC_MONTHBK] = CreateSolidBrush(infoPtr->colors[MCSC_MONTHBK]);
2534 infoPtr->minSel = infoPtr->todaysDate;
2535 infoPtr->maxSel = infoPtr->todaysDate;
2536 infoPtr->calendars[0].month = infoPtr->todaysDate;
2537 infoPtr->isUnicode = TRUE;
2539 /* call MONTHCAL_UpdateSize to set all of the dimensions */
2540 /* of the control */
2541 MONTHCAL_UpdateSize(infoPtr);
2543 /* today auto update timer, to be freed only on control destruction */
2544 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2546 OpenThemeData (infoPtr->hwndSelf, themeClass);
2548 return 0;
2550 fail:
2551 Free(infoPtr->monthdayState);
2552 Free(infoPtr->calendars);
2553 Free(infoPtr);
2554 return 0;
2557 static LRESULT
2558 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2560 /* free month calendar info data */
2561 Free(infoPtr->monthdayState);
2562 Free(infoPtr->calendars);
2563 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2565 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2567 DeleteObject(infoPtr->brushes[MCSC_BACKGROUND]);
2568 DeleteObject(infoPtr->brushes[MCSC_TITLEBK]);
2569 DeleteObject(infoPtr->brushes[MCSC_MONTHBK]);
2571 Free(infoPtr);
2572 return 0;
2576 * Handler for WM_NOTIFY messages
2578 static LRESULT
2579 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2581 /* notification from year edit updown */
2582 if (hdr->code == UDN_DELTAPOS)
2584 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2586 if (hdr->hwndFrom == infoPtr->hWndYearUpDown && nmud->iDelta)
2588 /* year value limits are set up explicitly after updown creation */
2589 MONTHCAL_Scroll(infoPtr, 12 * nmud->iDelta);
2590 MONTHCAL_NotifyDayState(infoPtr);
2591 MONTHCAL_NotifySelectionChange(infoPtr);
2594 return 0;
2597 static inline BOOL
2598 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2600 BOOL prev = infoPtr->isUnicode;
2601 infoPtr->isUnicode = isUnicode;
2602 return prev;
2605 static inline BOOL
2606 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2608 return infoPtr->isUnicode;
2611 static LRESULT WINAPI
2612 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2614 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0);
2616 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2618 if (!infoPtr && (uMsg != WM_CREATE))
2619 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2620 switch(uMsg)
2622 case MCM_GETCURSEL:
2623 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2625 case MCM_SETCURSEL:
2626 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2628 case MCM_GETMAXSELCOUNT:
2629 return MONTHCAL_GetMaxSelCount(infoPtr);
2631 case MCM_SETMAXSELCOUNT:
2632 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2634 case MCM_GETSELRANGE:
2635 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2637 case MCM_SETSELRANGE:
2638 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2640 case MCM_GETMONTHRANGE:
2641 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2643 case MCM_SETDAYSTATE:
2644 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2646 case MCM_GETMINREQRECT:
2647 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2649 case MCM_GETCOLOR:
2650 return MONTHCAL_GetColor(infoPtr, wParam);
2652 case MCM_SETCOLOR:
2653 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2655 case MCM_GETTODAY:
2656 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2658 case MCM_SETTODAY:
2659 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2661 case MCM_HITTEST:
2662 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2664 case MCM_GETFIRSTDAYOFWEEK:
2665 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2667 case MCM_SETFIRSTDAYOFWEEK:
2668 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2670 case MCM_GETRANGE:
2671 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2673 case MCM_SETRANGE:
2674 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2676 case MCM_GETMONTHDELTA:
2677 return MONTHCAL_GetMonthDelta(infoPtr);
2679 case MCM_SETMONTHDELTA:
2680 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2682 case MCM_GETMAXTODAYWIDTH:
2683 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2685 case MCM_SETUNICODEFORMAT:
2686 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2688 case MCM_GETUNICODEFORMAT:
2689 return MONTHCAL_GetUnicodeFormat(infoPtr);
2691 case WM_GETDLGCODE:
2692 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2694 case WM_RBUTTONUP:
2695 return MONTHCAL_RButtonUp(infoPtr, lParam);
2697 case WM_LBUTTONDOWN:
2698 return MONTHCAL_LButtonDown(infoPtr, lParam);
2700 case WM_MOUSEMOVE:
2701 return MONTHCAL_MouseMove(infoPtr, lParam);
2703 case WM_LBUTTONUP:
2704 return MONTHCAL_LButtonUp(infoPtr, lParam);
2706 case WM_PAINT:
2707 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2709 case WM_PRINTCLIENT:
2710 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2712 case WM_ERASEBKGND:
2713 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2715 case WM_SETFOCUS:
2716 return MONTHCAL_SetFocus(infoPtr);
2718 case WM_SIZE:
2719 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2721 case WM_NOTIFY:
2722 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2724 case WM_CREATE:
2725 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2727 case WM_SETFONT:
2728 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2730 case WM_GETFONT:
2731 return MONTHCAL_GetFont(infoPtr);
2733 case WM_TIMER:
2734 return MONTHCAL_Timer(infoPtr, wParam);
2736 case WM_THEMECHANGED:
2737 return theme_changed (infoPtr);
2739 case WM_DESTROY:
2740 return MONTHCAL_Destroy(infoPtr);
2742 case WM_SYSCOLORCHANGE:
2743 COMCTL32_RefreshSysColors();
2744 return 0;
2746 case WM_STYLECHANGED:
2747 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2749 case WM_STYLECHANGING:
2750 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2752 default:
2753 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2754 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2755 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2760 void
2761 MONTHCAL_Register(void)
2763 WNDCLASSW wndClass;
2765 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2766 wndClass.style = CS_GLOBALCLASS;
2767 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2768 wndClass.cbClsExtra = 0;
2769 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2770 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2771 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2772 wndClass.lpszClassName = MONTHCAL_CLASSW;
2774 RegisterClassW(&wndClass);
2778 void
2779 MONTHCAL_Unregister(void)
2781 UnregisterClassW(MONTHCAL_CLASSW, NULL);