comctl32/monthcal: Rename button navigation helper to better reflect its purpose.
[wine.git] / dlls / comctl32 / monthcal.c
blob2ae3f46344b69a1585028958be18853ee7930983
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 * -- MONTHCAL_GetMonthRange
37 * -- handle resources better (doesn't work now);
38 * -- take care of internationalization.
39 * -- keyboard handling.
40 * -- search for FIXME
43 #include <math.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
49 #include "windef.h"
50 #include "winbase.h"
51 #include "wingdi.h"
52 #include "winuser.h"
53 #include "winnls.h"
54 #include "commctrl.h"
55 #include "comctl32.h"
56 #include "uxtheme.h"
57 #include "tmschema.h"
58 #include "wine/unicode.h"
59 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
63 #define MC_SEL_LBUTUP 1 /* Left button released */
64 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
65 #define MC_PREVPRESSED 4 /* Prev month button pressed */
66 #define MC_NEXTPRESSED 8 /* Next month button pressed */
67 #define MC_PREVNEXTMONTHDELAY 350 /* when continuously pressing `next/prev
68 month', wait 350 ms before going
69 to the next/prev month */
70 #define MC_TODAYUPDATEDELAY 120000 /* time between today check for update (2 min) */
72 #define MC_PREVNEXTMONTHTIMER 1 /* Timer ID's */
73 #define MC_TODAYUPDATETIMER 2
75 #define countof(arr) (sizeof(arr)/sizeof(arr[0]))
77 /* convert from days to 100 nanoseconds unit - used as FILETIME unit */
78 #define DAYSTO100NSECS(days) (((ULONGLONG)(days))*24*60*60*10000000)
80 /* single calendar data */
81 typedef struct _CALENDAR_INFO
83 RECT title; /* rect for the header above the calendar */
84 RECT titlemonth; /* the 'month name' text in the header */
85 RECT titleyear; /* the 'year number' text in the header */
86 RECT wdays; /* week days at top */
87 RECT days; /* calendar area */
88 RECT weeknums; /* week numbers at left side */
90 SYSTEMTIME month;/* contains calendar main month/year */
91 } CALENDAR_INFO;
93 typedef struct
95 HWND hwndSelf;
96 DWORD dwStyle; /* cached GWL_STYLE */
97 COLORREF bk;
98 COLORREF txt;
99 COLORREF titlebk;
100 COLORREF titletxt;
101 COLORREF monthbk;
102 COLORREF trailingtxt;
103 HFONT hFont;
104 HFONT hBoldFont;
105 int textHeight;
106 int textWidth;
107 int height_increment;
108 int width_increment;
109 INT delta; /* scroll rate; # of months that the */
110 /* control moves when user clicks a scroll button */
111 int visible; /* # of months visible */
112 int firstDay; /* Start month calendar with firstDay's day,
113 stored in SYSTEMTIME format */
114 BOOL firstDaySet; /* first week day differs from locale defined */
116 BOOL isUnicode; /* value set with MCM_SETUNICODE format */
118 int monthRange;
119 MONTHDAYSTATE *monthdayState;
120 SYSTEMTIME todaysDate;
121 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
122 int status; /* See MC_SEL flags */
123 SYSTEMTIME firstSel; /* first selected day */
124 INT maxSelCount;
125 SYSTEMTIME minSel;
126 SYSTEMTIME maxSel;
127 SYSTEMTIME curSel; /* contains currently selected year, month and day */
128 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
129 DWORD rangeValid;
130 SYSTEMTIME minDate;
131 SYSTEMTIME maxDate;
133 RECT titlebtnnext; /* the `next month' button in the header */
134 RECT titlebtnprev; /* the `prev month' button in the header */
135 RECT todayrect; /* `today: xx/xx/xx' text rect */
136 HWND hwndNotify; /* Window to receive the notifications */
137 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
138 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
139 WNDPROC EditWndProc; /* original Edit window procedure */
141 CALENDAR_INFO *calendars;
142 INT cal_num;
143 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
145 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
147 /* empty SYSTEMTIME const */
148 static const SYSTEMTIME st_null;
149 /* valid date limits */
150 static const SYSTEMTIME max_allowed_date = { .wYear = 9999, .wMonth = 12, .wDay = 31 };
151 static const SYSTEMTIME min_allowed_date = { .wYear = 1752, .wMonth = 9, .wDay = 14 };
153 /* Prev/Next buttons */
154 enum nav_direction
156 DIRECTION_BACKWARD,
157 DIRECTION_FORWARD
160 #define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0))
162 /* helper functions */
164 /* send a single MCN_SELCHANGE notification */
165 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
167 NMSELCHANGE nmsc;
169 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
170 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
171 nmsc.nmhdr.code = MCN_SELCHANGE;
172 nmsc.stSelStart = infoPtr->minSel;
173 nmsc.stSelEnd = infoPtr->maxSel;
174 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
177 /* send a single MCN_SELECT notification */
178 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
180 NMSELCHANGE nmsc;
182 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
183 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
184 nmsc.nmhdr.code = MCN_SELECT;
185 nmsc.stSelStart = infoPtr->minSel;
186 nmsc.stSelEnd = infoPtr->maxSel;
188 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
191 /* returns the number of days in any given month, checking for leap days */
192 /* january is 1, december is 12 */
193 int MONTHCAL_MonthLength(int month, int year)
195 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
196 /* Wrap around, this eases handling. Getting length only we shouldn't care
197 about year change here cause January and December have
198 the same day quantity */
199 if(month == 0)
200 month = 12;
201 else if(month == 13)
202 month = 1;
204 /* special case for calendar transition year */
205 if(month == min_allowed_date.wMonth && year == min_allowed_date.wYear) return 19;
207 /* if we have a leap year add 1 day to February */
208 /* a leap year is a year either divisible by 400 */
209 /* or divisible by 4 and not by 100 */
210 if(month == 2) { /* February */
211 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
212 (year%4 == 0)) ? 1 : 0);
214 else {
215 return mdays[month - 1];
219 /* compares timestamps using date part only */
220 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
222 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
223 (first->wDay == second->wDay);
226 /* make sure that date fields are valid */
227 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
229 if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
230 if(time->wDayOfWeek > 6) return FALSE;
231 if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear))
232 return FALSE;
234 return TRUE;
237 /* Copies timestamp part only.
239 * PARAMETERS
241 * [I] from : source date
242 * [O] to : dest date
244 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
246 to->wHour = from->wHour;
247 to->wMinute = from->wMinute;
248 to->wSecond = from->wSecond;
251 /* Copies date part only.
253 * PARAMETERS
255 * [I] from : source date
256 * [O] to : dest date
258 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
260 to->wYear = from->wYear;
261 to->wMonth = from->wMonth;
262 to->wDay = from->wDay;
263 to->wDayOfWeek = from->wDayOfWeek;
266 /* Compares two dates in SYSTEMTIME format
268 * PARAMETERS
270 * [I] first : pointer to valid first date data to compare
271 * [I] second : pointer to valid second date data to compare
273 * RETURN VALUE
275 * -1 : first < second
276 * 0 : first == second
277 * 1 : first > second
279 * Note that no date validation performed, alreadt validated values expected.
281 static LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
283 FILETIME ft_first, ft_second;
285 SystemTimeToFileTime(first, &ft_first);
286 SystemTimeToFileTime(second, &ft_second);
288 return CompareFileTime(&ft_first, &ft_second);
291 static LONG MONTHCAL_CompareMonths(const SYSTEMTIME *first, const SYSTEMTIME *second)
293 SYSTEMTIME st_first, st_second;
295 st_first = st_second = st_null;
296 MONTHCAL_CopyDate(first, &st_first);
297 MONTHCAL_CopyDate(second, &st_second);
298 st_first.wDay = st_second.wDay = 1;
300 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
303 static LONG MONTHCAL_CompareDate(const SYSTEMTIME *first, const SYSTEMTIME *second)
305 SYSTEMTIME st_first, st_second;
307 st_first = st_second = st_null;
308 MONTHCAL_CopyDate(first, &st_first);
309 MONTHCAL_CopyDate(second, &st_second);
311 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
314 /* Checks largest possible date range and configured one
316 * PARAMETERS
318 * [I] infoPtr : valid pointer to control data
319 * [I] date : pointer to valid date data to check
320 * [I] fix : make date fit valid range
322 * RETURN VALUE
324 * TRUE - date whithin largest and configured range
325 * FALSE - date is outside largest or configured range
327 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr,
328 SYSTEMTIME *date, BOOL fix)
330 const SYSTEMTIME *fix_st = NULL;
332 if(MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) {
333 fix_st = &max_allowed_date;
335 else if(MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1) {
336 fix_st = &min_allowed_date;
338 else if(infoPtr->rangeValid & GDTR_MAX) {
339 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
340 fix_st = &infoPtr->maxDate;
343 else if(infoPtr->rangeValid & GDTR_MIN) {
344 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
345 fix_st = &infoPtr->minDate;
349 if (fix && fix_st) {
350 date->wYear = fix_st->wYear;
351 date->wMonth = fix_st->wMonth;
354 return fix_st ? FALSE : TRUE;
357 /* Checks passed range width with configured maximum selection count
359 * PARAMETERS
361 * [I] infoPtr : valid pointer to control data
362 * [I] range0 : pointer to valid date data (requested bound)
363 * [I] range1 : pointer to valid date data (primary bound)
364 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
366 * Adjust value computed basing on primary bound and current maximum selection
367 * count. For simple range check (without adjusted value required) (range0, range1)
368 * relation means nothing.
370 * RETURN VALUE
372 * TRUE - range is shorter or equal to maximum
373 * FALSE - range is larger than maximum
375 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
376 const SYSTEMTIME *range0,
377 const SYSTEMTIME *range1,
378 SYSTEMTIME *adjust)
380 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
381 FILETIME ft_range0, ft_range1;
382 LONG cmp;
384 SystemTimeToFileTime(range0, &ft_range0);
385 SystemTimeToFileTime(range1, &ft_range1);
387 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
388 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
389 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
390 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
392 cmp = CompareFileTime(&ft_range0, &ft_range1);
394 if(cmp == 1)
395 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
396 else
397 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
399 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
401 if(adjust) {
402 if(cmp == 1)
403 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
404 else
405 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
407 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
408 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
409 FileTimeToSystemTime(&ft_range0, adjust);
412 return FALSE;
414 else return TRUE;
417 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
418 Milliseconds are intentionally not validated. */
419 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
421 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
422 return FALSE;
423 else
424 return TRUE;
427 /* Note:Depending on DST, this may be offset by a day.
428 Need to find out if we're on a DST place & adjust the clock accordingly.
429 Above function assumes we have a valid data.
430 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
431 0 = Sunday.
434 /* Returns the day in the week
436 * PARAMETERS
437 * [i] date : input date
438 * [I] inplace : set calculated value back to date structure
440 * RETURN VALUE
441 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
443 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
445 SYSTEMTIME st = st_null;
446 FILETIME ft;
448 MONTHCAL_CopyDate(date, &st);
450 SystemTimeToFileTime(&st, &ft);
451 FileTimeToSystemTime(&ft, &st);
453 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
455 return st.wDayOfWeek;
458 /* properly updates date to point on next month */
459 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
461 if(++date->wMonth > 12)
463 date->wMonth = 1;
464 date->wYear++;
466 MONTHCAL_CalculateDayOfWeek(date, TRUE);
469 /* properly updates date to point on prev month */
470 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
472 if(--date->wMonth < 1)
474 date->wMonth = 12;
475 date->wYear--;
477 MONTHCAL_CalculateDayOfWeek(date, TRUE);
480 /* Returns full date for a first currently visible day */
481 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
483 /* zero indexed calendar has the earliest date */
484 SYSTEMTIME st_first = infoPtr->calendars[0].month;
485 INT firstDay;
487 st_first.wDay = 1;
488 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
490 *date = infoPtr->calendars[0].month;
491 MONTHCAL_GetPrevMonth(date);
493 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
494 (infoPtr->firstDay - firstDay) % 7 + 1;
496 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
497 date->wDay -= 7;
499 /* fix day of week */
500 MONTHCAL_CalculateDayOfWeek(date, TRUE);
503 /* Returns full date for a last currently visible day */
504 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
506 /* the latest date is in latest calendar */
507 SYSTEMTIME st, lt_month = infoPtr->calendars[infoPtr->cal_num-1].month;
509 *date = lt_month;
510 MONTHCAL_GetNextMonth(date);
512 MONTHCAL_GetMinDate(infoPtr, &st);
513 /* Use month length to get max day. 42 means max day count in calendar area */
514 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
515 MONTHCAL_MonthLength(lt_month.wMonth, lt_month.wYear);
517 /* fix day of week */
518 MONTHCAL_CalculateDayOfWeek(date, TRUE);
521 /* From a given point, calculate the row (weekpos), column(daypos)
522 and day in the calendar. day== 0 mean the last day of tha last month
524 static int MONTHCAL_CalcDayFromPos(const MONTHCAL_INFO *infoPtr, int x, int y,
525 int *daypos, int *weekpos)
527 int retval, firstDay;
528 RECT rcClient;
529 SYSTEMTIME st = infoPtr->curSel;
531 GetClientRect(infoPtr->hwndSelf, &rcClient);
533 /* if the point is outside the x bounds of the window put
534 it at the boundary */
535 if (x > rcClient.right)
536 x = rcClient.right;
538 *daypos = (x - infoPtr->calendars[0].days.left ) / infoPtr->width_increment;
539 *weekpos = (y - infoPtr->calendars[0].days.top ) / infoPtr->height_increment;
541 st.wDay = 1;
542 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
543 retval = *daypos + (7 * *weekpos) - firstDay;
544 return retval;
547 /* Sets the RECT struct r to the rectangle around the date
549 * PARAMETERS
551 * [I] infoPtr : pointer to control data
552 * [I] date : date value
553 * [O] x : day column (zero based)
554 * [O] y : week column (zero based)
556 static void MONTHCAL_CalcDayXY(const MONTHCAL_INFO *infoPtr,
557 const SYSTEMTIME *date, int *x, int *y)
559 SYSTEMTIME st = infoPtr->curSel;
560 LONG cmp;
561 int first;
563 st.wDay = 1;
564 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
566 cmp = MONTHCAL_CompareMonths(date, &infoPtr->curSel);
568 /* previous month */
569 if(cmp == -1) {
570 *x = (first - MONTHCAL_MonthLength(date->wMonth, infoPtr->curSel.wYear) + date->wDay) % 7;
571 *y = 0;
572 return;
575 /* next month calculation is same as for current,
576 just add current month length */
577 if(cmp == 1) {
578 first += MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear);
581 *x = (date->wDay + first) % 7;
582 *y = (date->wDay + first - *x) / 7;
586 /* x: column(day), y: row(week) */
587 static inline void MONTHCAL_CalcDayRect(const MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
589 r->left = infoPtr->calendars[0].days.left + x * infoPtr->width_increment;
590 r->right = r->left + infoPtr->width_increment;
591 r->top = infoPtr->calendars[0].days.top + y * infoPtr->height_increment;
592 r->bottom = r->top + infoPtr->textHeight;
596 /* Sets the RECT struct r to the rectangle around the date */
597 static inline void MONTHCAL_CalcPosFromDay(const MONTHCAL_INFO *infoPtr,
598 const SYSTEMTIME *date, RECT *r)
600 int x, y;
602 MONTHCAL_CalcDayXY(infoPtr, date, &x, &y);
603 MONTHCAL_CalcDayRect(infoPtr, r, x, y);
606 /* Focused day helper:
608 - set focused date to given value;
609 - reset to zero value if NULL passed;
610 - invalidate previous and new day rectangle only if needed.
612 Returns TRUE if focused day changed, FALSE otherwise.
614 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
616 RECT r;
618 if(st)
620 /* there's nothing to do if it's the same date,
621 mouse move within same date rectangle case */
622 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
624 /* invalidate old focused day */
625 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
626 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
628 infoPtr->focusedSel = *st;
631 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
633 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
634 infoPtr->focusedSel = st_null;
636 /* on set invalidates new day, on reset clears previous focused day */
637 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
639 return TRUE;
642 /* Draw today day mark rectangle
644 * [I] hdc : context to draw in
645 * [I] day : day to mark with rectangle
648 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
649 const SYSTEMTIME *date)
651 HPEN hRedPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
652 HPEN hOldPen2 = SelectObject(hdc, hRedPen);
653 HBRUSH hOldBrush;
654 RECT day_rect;
656 MONTHCAL_CalcPosFromDay(infoPtr, date, &day_rect);
658 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
659 Rectangle(hdc, day_rect.left, day_rect.top, day_rect.right, day_rect.bottom);
661 SelectObject(hdc, hOldBrush);
662 DeleteObject(hRedPen);
663 SelectObject(hdc, hOldPen2);
666 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
667 int bold, const PAINTSTRUCT *ps)
669 static const WCHAR fmtW[] = { '%','d',0 };
670 WCHAR buf[10];
671 RECT r, r_temp;
672 static BOOL bold_selected;
673 BOOL selected_day = FALSE;
674 HBRUSH hbr;
675 COLORREF oldCol = 0;
676 COLORREF oldBk = 0;
678 /* No need to check styles: when selection is not valid, it is set to zero.
679 * 1<day<31, so everything is OK.
682 MONTHCAL_CalcPosFromDay(infoPtr, st, &r);
683 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
685 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
686 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0)) {
688 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
689 TRACE("%s\n", wine_dbgstr_rect(&r));
690 oldCol = SetTextColor(hdc, infoPtr->monthbk);
691 oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
692 hbr = GetSysColorBrush(COLOR_HIGHLIGHT);
693 FillRect(hdc, &r, hbr);
695 selected_day = TRUE;
698 if(bold && !bold_selected) {
699 SelectObject(hdc, infoPtr->hBoldFont);
700 bold_selected = TRUE;
702 if(!bold && bold_selected) {
703 SelectObject(hdc, infoPtr->hFont);
704 bold_selected = FALSE;
707 SetBkMode(hdc,TRANSPARENT);
708 wsprintfW(buf, fmtW, st->wDay);
709 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
711 if(selected_day) {
712 SetTextColor(hdc, oldCol);
713 SetBkColor(hdc, oldBk);
718 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, BOOL btnNext)
720 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
721 RECT *r = btnNext ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
722 BOOL pressed = btnNext ? (infoPtr->status & MC_NEXTPRESSED) :
723 (infoPtr->status & MC_PREVPRESSED);
724 if (theme)
726 static const int states[] = {
727 /* Prev button */
728 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
729 /* Next button */
730 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
732 int stateNum = btnNext ? 3 : 0;
733 if (pressed)
734 stateNum += 1;
735 else
737 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
739 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
741 else
743 int style = btnNext ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
744 if (pressed)
745 style |= DFCS_PUSHED;
746 else
748 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
751 DrawFrameControl(hdc, r, DFC_SCROLL, style);
754 /* paint a title with buttons and month/year string */
755 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
757 static const WCHAR fmt_monthW[] = { '%','s',' ','%','l','d',0 };
758 RECT *title = &infoPtr->calendars[calIdx].title;
759 WCHAR buf_month[80], buf_fmt[80];
760 HBRUSH hbr;
761 SIZE sz;
763 /* fill header box */
764 hbr = CreateSolidBrush(infoPtr->titlebk);
765 FillRect(hdc, title, hbr);
766 DeleteObject(hbr);
768 /* month/year string */
769 SetBkColor(hdc, infoPtr->titlebk);
770 SetTextColor(hdc, infoPtr->titletxt);
771 SelectObject(hdc, infoPtr->hBoldFont);
773 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+infoPtr->curSel.wMonth-1,
774 buf_month, countof(buf_month));
776 wsprintfW(buf_fmt, fmt_monthW, buf_month, infoPtr->curSel.wYear);
777 DrawTextW(hdc, buf_fmt, strlenW(buf_fmt), title,
778 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
780 /* update title rectangles with current month - used while testing hits */
781 GetTextExtentPoint32W(hdc, buf_fmt, strlenW(buf_fmt), &sz);
782 infoPtr->calendars[calIdx].titlemonth.left = title->right / 2 + title->left / 2 - sz.cx / 2;
783 infoPtr->calendars[calIdx].titleyear.right = title->right / 2 + title->left / 2 + sz.cx / 2;
785 GetTextExtentPoint32W(hdc, buf_month, strlenW(buf_month), &sz);
786 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
787 infoPtr->calendars[calIdx].titleyear.left = infoPtr->calendars[calIdx].titlemonth.right;
790 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
792 static const WCHAR fmt_weekW[] = { '%','d',0 };
793 INT mindays, weeknum, weeknum1, startofprescal;
794 SYSTEMTIME st = infoPtr->curSel;
795 RECT r;
796 WCHAR buf[80];
797 INT i, prev_month;
799 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
801 MONTHCAL_GetMinDate(infoPtr, &st);
802 startofprescal = st.wDay;
803 st = infoPtr->curSel;
805 prev_month = infoPtr->curSel.wMonth - 1;
806 if(prev_month == 0) prev_month = 12;
809 Rules what week to call the first week of a new year:
810 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
811 The week containing Jan 1 is the first week of year
812 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
813 First week of year must contain 4 days of the new year
814 LOCALE_IFIRSTWEEKOFYEAR == 1 (what contries?)
815 The first week of the year must contain only days of the new year
817 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
818 weeknum = atoiW(buf);
819 switch (weeknum)
821 case 1: mindays = 6;
822 break;
823 case 2: mindays = 3;
824 break;
825 case 0: mindays = 0;
826 break;
827 default:
828 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
829 mindays = 0;
832 if (infoPtr->curSel.wMonth == 1)
834 /* calculate all those exceptions for january */
835 st.wDay = st.wMonth = 1;
836 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
837 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
838 weeknum = 1;
839 else
841 weeknum = 0;
842 for(i = 0; i < 11; i++)
843 weeknum += MONTHCAL_MonthLength(i+1, infoPtr->curSel.wYear - 1);
845 weeknum += startofprescal + 7;
846 weeknum /= 7;
847 st.wYear -= 1;
848 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
849 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
852 else
854 weeknum = 0;
855 for(i = 0; i < prev_month - 1; i++)
856 weeknum += MONTHCAL_MonthLength(i+1, infoPtr->curSel.wYear);
858 weeknum += startofprescal + 7;
859 weeknum /= 7;
860 st.wDay = st.wMonth = 1;
861 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
862 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
865 r = infoPtr->calendars[calIdx].weeknums;
866 r.bottom = r.top + infoPtr->height_increment;
868 for(i = 0; i < 6; i++) {
869 if((i == 0) && (weeknum > 50))
871 wsprintfW(buf, fmt_weekW, weeknum);
872 weeknum = 0;
874 else if((i == 5) && (weeknum > 47))
876 wsprintfW(buf, fmt_weekW, 1);
878 else
879 wsprintfW(buf, fmt_weekW, weeknum + i);
881 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
882 OffsetRect(&r, 0, infoPtr->height_increment);
885 /* line separator for week numbers column */
886 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
887 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
890 /* bottom today date */
891 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
893 if(!(infoPtr->dwStyle & MCS_NOTODAY)) {
894 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
895 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
896 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
897 RECT rtoday;
899 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
900 SYSTEMTIME fake_st;
902 MONTHCAL_GetMaxDate(infoPtr, &fake_st);
903 /* this is always safe cause next month will never fully fit calendar */
904 fake_st.wDay += 1;
905 MONTHCAL_CircleDay(infoPtr, hdc, &fake_st);
907 if (!LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, countof(buf_todayW)))
909 WARN("Can't load resource\n");
910 strcpyW(buf_todayW, todayW);
912 MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
913 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
914 buf_dateW, countof(buf_dateW));
915 SelectObject(hdc, infoPtr->hBoldFont);
917 wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
918 DrawTextW(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
919 DrawTextW(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
921 SelectObject(hdc, infoPtr->hFont);
925 /* today mark + focus */
926 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
928 if((infoPtr->curSel.wMonth == infoPtr->todaysDate.wMonth) &&
929 (infoPtr->curSel.wYear == infoPtr->todaysDate.wYear) &&
930 !(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
932 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
935 if(!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
937 RECT r;
938 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
939 DrawFocusRect(hdc, &r);
943 /* paint a calendar area */
944 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
946 INT prev_month, i, j;
947 WCHAR buf[80];
948 HBRUSH hbr;
949 RECT r, fill_bk_rect;
950 int mask;
951 SYSTEMTIME st;
953 /* fill whole days area - from week days area to today note rectangle */
954 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
955 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
956 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
958 hbr = CreateSolidBrush(infoPtr->monthbk);
959 FillRect(hdc, &fill_bk_rect, hbr);
960 DeleteObject(hbr);
962 /* draw line under day abbreviations */
963 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
964 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
965 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
966 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
968 prev_month = infoPtr->curSel.wMonth - 1;
969 if (prev_month == 0) prev_month = 12;
971 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
972 infoPtr->calendars[calIdx].weeknums.right;
974 /* 1. draw day abbreviations */
975 SelectObject(hdc, infoPtr->hFont);
976 SetBkColor(hdc, infoPtr->monthbk);
977 SetTextColor(hdc, infoPtr->trailingtxt);
978 /* rectangle to draw a single day abbreviation within */
979 r = infoPtr->calendars[calIdx].wdays;
980 r.right = r.left + infoPtr->width_increment;
982 i = infoPtr->firstDay;
983 for(j = 0; j < 7; j++) {
984 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
985 DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
986 OffsetRect(&r, infoPtr->width_increment, 0);
989 /* 2. previous and next months */
990 if (!(infoPtr->dwStyle & MCS_NOTRAILINGDATES) && (calIdx == 0 || calIdx == infoPtr->cal_num - 1))
992 SYSTEMTIME st_max;
994 SetTextColor(hdc, infoPtr->trailingtxt);
996 /* draw prev month */
997 if (calIdx == 0)
999 MONTHCAL_GetMinDate(infoPtr, &st);
1000 mask = 1 << (st.wDay-1);
1002 while(st.wDay <= MONTHCAL_MonthLength(prev_month, infoPtr->curSel.wYear))
1004 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[0] & mask, ps);
1005 mask <<= 1;
1006 st.wDay++;
1010 /* draw next month */
1011 if (calIdx == infoPtr->cal_num - 1)
1013 st = infoPtr->curSel;
1014 st.wDay = 1;
1015 MONTHCAL_GetNextMonth(&st);
1016 MONTHCAL_GetMaxDate(infoPtr, &st_max);
1017 mask = 1;
1019 while(st.wDay <= st_max.wDay)
1021 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[2] & mask, ps);
1022 mask <<= 1;
1023 st.wDay++;
1028 /* 3. current month */
1029 SetTextColor(hdc, infoPtr->txt);
1030 st = infoPtr->curSel;
1031 st.wDay = 1;
1032 mask = 1;
1033 while(st.wDay <= MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear)) {
1034 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[1] & mask, ps);
1035 mask <<= 1;
1036 st.wDay++;
1040 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1042 COLORREF old_text_clr, old_bk_clr;
1043 HFONT old_font;
1044 INT i;
1046 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1047 old_bk_clr = GetBkColor(hdc);
1048 old_font = GetCurrentObject(hdc, OBJ_FONT);
1050 for (i = 0; i < infoPtr->cal_num; i++)
1052 RECT *title = &infoPtr->calendars[i].title;
1053 RECT r;
1055 /* draw title, redraw all its elements */
1056 if (IntersectRect(&r, &(ps->rcPaint), title))
1057 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1059 /* draw calendar area */
1060 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1061 if (IntersectRect(&r, &(ps->rcPaint), &r))
1062 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1064 /* week numbers */
1065 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1068 /* focus and today rectangle */
1069 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1071 /* today at the bottom left */
1072 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1074 /* navigation buttons */
1075 MONTHCAL_PaintButton(infoPtr, hdc, FALSE);
1076 MONTHCAL_PaintButton(infoPtr, hdc, TRUE);
1078 /* restore context */
1079 SetBkColor(hdc, old_bk_clr);
1080 SelectObject(hdc, old_font);
1081 SetTextColor(hdc, old_text_clr);
1084 static LRESULT
1085 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, LPRECT lpRect)
1087 TRACE("rect %p\n", lpRect);
1089 if(!lpRect) return FALSE;
1091 lpRect->left = infoPtr->calendars[0].title.left;
1092 lpRect->top = infoPtr->calendars[0].title.top;
1093 lpRect->right = infoPtr->calendars[0].title.right;
1094 lpRect->bottom = infoPtr->todayrect.bottom;
1096 AdjustWindowRect(lpRect, infoPtr->dwStyle, FALSE);
1098 /* minimal rectangle is zero based */
1099 OffsetRect(lpRect, -lpRect->left, -lpRect->top);
1101 TRACE("%s\n", wine_dbgstr_rect(lpRect));
1103 return TRUE;
1107 static LRESULT
1108 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, INT index)
1110 TRACE("\n");
1112 switch(index) {
1113 case MCSC_BACKGROUND:
1114 return infoPtr->bk;
1115 case MCSC_TEXT:
1116 return infoPtr->txt;
1117 case MCSC_TITLEBK:
1118 return infoPtr->titlebk;
1119 case MCSC_TITLETEXT:
1120 return infoPtr->titletxt;
1121 case MCSC_MONTHBK:
1122 return infoPtr->monthbk;
1123 case MCSC_TRAILINGTEXT:
1124 return infoPtr->trailingtxt;
1127 return -1;
1131 static LRESULT
1132 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, INT index, COLORREF color)
1134 COLORREF prev = -1;
1136 TRACE("%d: color %08x\n", index, color);
1138 switch(index) {
1139 case MCSC_BACKGROUND:
1140 prev = infoPtr->bk;
1141 infoPtr->bk = color;
1142 break;
1143 case MCSC_TEXT:
1144 prev = infoPtr->txt;
1145 infoPtr->txt = color;
1146 break;
1147 case MCSC_TITLEBK:
1148 prev = infoPtr->titlebk;
1149 infoPtr->titlebk = color;
1150 break;
1151 case MCSC_TITLETEXT:
1152 prev=infoPtr->titletxt;
1153 infoPtr->titletxt = color;
1154 break;
1155 case MCSC_MONTHBK:
1156 prev = infoPtr->monthbk;
1157 infoPtr->monthbk = color;
1158 break;
1159 case MCSC_TRAILINGTEXT:
1160 prev = infoPtr->trailingtxt;
1161 infoPtr->trailingtxt = color;
1162 break;
1165 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND ? TRUE : FALSE);
1166 return prev;
1170 static LRESULT
1171 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1173 TRACE("\n");
1175 if(infoPtr->delta)
1176 return infoPtr->delta;
1177 else
1178 return infoPtr->visible;
1182 static LRESULT
1183 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1185 INT prev = infoPtr->delta;
1187 TRACE("delta %d\n", delta);
1189 infoPtr->delta = delta;
1190 return prev;
1194 static inline LRESULT
1195 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1197 int day;
1199 /* convert from SYSTEMTIME to locale format */
1200 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1202 return MAKELONG(day, infoPtr->firstDaySet);
1206 /* Sets the first day of the week that will appear in the control
1209 * PARAMETERS:
1210 * [I] infoPtr : valid pointer to control data
1211 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1214 * RETURN VALUE:
1215 * Low word contains previous first day,
1216 * high word indicates was first day forced with this message before or is
1217 * locale difined (TRUE - was forced, FALSE - wasn't).
1219 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1220 * FIXME: we need more error checking here
1222 static LRESULT
1223 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1225 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1226 int new_day;
1228 TRACE("%d\n", day);
1230 if(day == -1)
1232 WCHAR buf[80];
1234 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1235 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1237 new_day = atoiW(buf);
1239 infoPtr->firstDaySet = FALSE;
1241 else if(day >= 7)
1243 new_day = 6; /* max first day allowed */
1244 infoPtr->firstDaySet = TRUE;
1246 else
1248 /* Native behaviour for that case is broken: invalid date number >31
1249 got displayed at (0,0) position, current month starts always from
1250 (1,0) position. Should be implemented here as well only if there's
1251 nothing else to do. */
1252 if (day < -1)
1253 FIXME("No bug compatibility for day=%d\n", day);
1255 new_day = day;
1256 infoPtr->firstDaySet = TRUE;
1259 /* convert from locale to SYSTEMTIME format */
1260 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1262 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1264 return prev;
1268 static LRESULT
1269 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
1271 TRACE("flag=%d, st=%p\n", flag, st);
1273 if(st)
1275 switch (flag) {
1276 case GMR_VISIBLE:
1278 st[0] = infoPtr->calendars[0].month;
1279 st[1] = infoPtr->calendars[infoPtr->cal_num-1].month;
1281 if (st[0].wMonth == min_allowed_date.wMonth &&
1282 st[0].wYear == min_allowed_date.wYear)
1284 st[0].wDay = min_allowed_date.wDay;
1286 else
1287 st[0].wDay = 1;
1288 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
1290 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
1291 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
1293 return infoPtr->cal_num;
1295 case GMR_DAYSTATE:
1297 /*FIXME: currently multicalendar feature isn't implemented,
1298 min date from previous month and max date from next one returned */
1299 MONTHCAL_GetMinDate(infoPtr, &st[0]);
1300 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
1301 break;
1303 default:
1304 WARN("Unknown flag value, got %d\n", flag);
1308 return infoPtr->monthRange;
1312 static LRESULT
1313 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1315 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1319 static LRESULT
1320 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1322 FILETIME ft_min, ft_max;
1324 TRACE("%x %p\n", limits, range);
1326 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1327 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1328 return FALSE;
1330 if (limits & GDTR_MIN)
1332 if (!MONTHCAL_ValidateTime(&range[0]))
1333 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1335 infoPtr->minDate = range[0];
1336 infoPtr->rangeValid |= GDTR_MIN;
1338 if (limits & GDTR_MAX)
1340 if (!MONTHCAL_ValidateTime(&range[1]))
1341 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1343 infoPtr->maxDate = range[1];
1344 infoPtr->rangeValid |= GDTR_MAX;
1347 /* Only one limit set - we are done */
1348 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1349 return TRUE;
1351 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1352 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1354 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1356 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1358 /* Native swaps limits only when both limits are being set. */
1359 SYSTEMTIME st_tmp = infoPtr->minDate;
1360 infoPtr->minDate = infoPtr->maxDate;
1361 infoPtr->maxDate = st_tmp;
1363 else
1365 /* reset the other limit */
1366 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1367 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1368 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1372 return TRUE;
1376 static LRESULT
1377 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1379 TRACE("%p\n", range);
1381 if(!range) return FALSE;
1383 range[1] = infoPtr->maxDate;
1384 range[0] = infoPtr->minDate;
1386 return infoPtr->rangeValid;
1390 static LRESULT
1391 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1393 TRACE("%p %d %p\n", infoPtr, months, states);
1394 if(months != infoPtr->monthRange) return 0;
1396 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1398 return 1;
1401 static LRESULT
1402 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1404 TRACE("%p\n", curSel);
1405 if(!curSel) return FALSE;
1406 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1408 *curSel = infoPtr->curSel;
1409 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1410 return TRUE;
1413 static LRESULT
1414 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1416 SYSTEMTIME prev = infoPtr->curSel;
1418 TRACE("%p\n", curSel);
1419 if(!curSel) return FALSE;
1420 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1422 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1423 /* exit earlier if selection equals current */
1424 if (MONTHCAL_IsDateEqual(&infoPtr->curSel, curSel)) return TRUE;
1426 if(!MONTHCAL_IsDateInValidRange(infoPtr, curSel, FALSE)) return FALSE;
1428 infoPtr->minSel = *curSel;
1429 infoPtr->maxSel = *curSel;
1431 /* if selection is still in current month, reduce rectangle */
1432 prev.wDay = curSel->wDay;
1433 if (MONTHCAL_IsDateEqual(&prev, curSel))
1435 RECT r_prev, r_new;
1437 /* note that infoPtr->curSel isn't updated yet */
1438 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->curSel, &r_prev);
1439 MONTHCAL_CalcPosFromDay(infoPtr, curSel, &r_new);
1441 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1442 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1444 else
1445 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1447 infoPtr->curSel = *curSel;
1448 infoPtr->calendars[0].month = *curSel;
1450 return TRUE;
1454 static LRESULT
1455 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1457 return infoPtr->maxSelCount;
1461 static LRESULT
1462 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1464 TRACE("%d\n", max);
1466 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1467 if(max <= 0) return FALSE;
1469 infoPtr->maxSelCount = max;
1471 return TRUE;
1475 static LRESULT
1476 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1478 TRACE("%p\n", range);
1480 if(!range) return FALSE;
1482 if(infoPtr->dwStyle & MCS_MULTISELECT)
1484 range[1] = infoPtr->maxSel;
1485 range[0] = infoPtr->minSel;
1486 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1487 return TRUE;
1490 return FALSE;
1494 static LRESULT
1495 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1497 TRACE("%p\n", range);
1499 if(!range) return FALSE;
1501 if(infoPtr->dwStyle & MCS_MULTISELECT)
1503 SYSTEMTIME old_range[2];
1505 /* adjust timestamps */
1506 if(!MONTHCAL_ValidateTime(&range[0]))
1507 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1508 if(!MONTHCAL_ValidateTime(&range[1]))
1509 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1511 /* maximum range exceeded */
1512 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1514 old_range[0] = infoPtr->minSel;
1515 old_range[1] = infoPtr->maxSel;
1517 /* swap if min > max */
1518 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1520 infoPtr->minSel = range[0];
1521 infoPtr->maxSel = range[1];
1523 else
1525 infoPtr->minSel = range[1];
1526 infoPtr->maxSel = range[0];
1528 infoPtr->curSel = infoPtr->minSel;
1529 infoPtr->calendars[0].month = infoPtr->minSel;
1531 /* update day of week */
1532 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1533 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1534 MONTHCAL_CalculateDayOfWeek(&infoPtr->curSel, TRUE);
1536 /* redraw if bounds changed */
1537 /* FIXME: no actual need to redraw everything */
1538 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1539 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1541 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1544 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1545 return TRUE;
1548 return FALSE;
1552 static LRESULT
1553 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1555 TRACE("%p\n", today);
1557 if(!today) return FALSE;
1558 *today = infoPtr->todaysDate;
1559 return TRUE;
1562 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1564 * RETURN VALUE
1566 * TRUE - today date changed
1567 * FALSE - today date isn't changed
1569 static BOOL
1570 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1572 RECT new_r, old_r;
1574 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1576 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->todaysDate, &old_r);
1577 MONTHCAL_CalcPosFromDay(infoPtr, today, &new_r);
1579 infoPtr->todaysDate = *today;
1581 /* only two days need redrawing */
1582 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1583 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1584 return TRUE;
1587 /* MCM_SETTODAT handler */
1588 static LRESULT
1589 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1591 TRACE("%p\n", today);
1593 if(!today) return FALSE;
1595 /* remember if date was set successfully */
1596 if(MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1598 return TRUE;
1601 /* returns calendar index containing specified point, or -1 if it's background */
1602 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1604 RECT r;
1605 INT i;
1607 for (i = 0; i < infoPtr->cal_num; i++)
1609 /* whole bounding rectangle allows some optimization to compute */
1610 r.left = infoPtr->calendars[i].title.left;
1611 r.top = infoPtr->calendars[i].title.top;
1612 r.bottom = infoPtr->calendars[i].days.bottom;
1613 r.right = infoPtr->calendars[i].days.right;
1615 if (PtInRect(&r, *pt)) return i;
1618 return -1;
1621 static LRESULT
1622 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1624 INT day, wday, wnum, calIdx;
1625 SYSTEMTIME ht_month;
1626 UINT x, y;
1628 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1630 x = lpht->pt.x;
1631 y = lpht->pt.y;
1633 memset(&lpht->st, 0, sizeof(lpht->st));
1635 /* Comment in for debugging...
1636 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,
1637 infoPtr->wdays.left, infoPtr->wdays.right,
1638 infoPtr->wdays.top, infoPtr->wdays.bottom,
1639 infoPtr->days.left, infoPtr->days.right,
1640 infoPtr->days.top, infoPtr->days.bottom,
1641 infoPtr->todayrect.left, infoPtr->todayrect.right,
1642 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1643 infoPtr->weeknums.left, infoPtr->weeknums.right,
1644 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1647 /* guess in what calendar we are */
1648 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1649 if (calIdx == -1)
1651 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1652 lpht->uHit = MCHT_TODAYLINK;
1653 else
1654 /* outside of calendar area? What's left must be background :-) */
1655 lpht->uHit = MCHT_CALENDARBK;
1657 return lpht->uHit;
1660 ht_month = infoPtr->calendars[calIdx].month;
1662 /* are we in the header? */
1663 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1664 /* FIXME: buttons hittesting could be optimized cause maximum
1665 two calendars have buttons */
1666 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1668 lpht->uHit = MCHT_TITLEBTNPREV;
1670 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1672 lpht->uHit = MCHT_TITLEBTNNEXT;
1674 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1676 lpht->uHit = MCHT_TITLEMONTH;
1678 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1680 lpht->uHit = MCHT_TITLEYEAR;
1682 else
1683 lpht->uHit = MCHT_TITLE;
1685 return lpht->uHit;
1688 /* days area (including week days and week numbers */
1689 day = MONTHCAL_CalcDayFromPos(infoPtr, x, y, &wday, &wnum);
1690 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1692 lpht->uHit = MCHT_CALENDARDAY;
1693 lpht->st.wYear = ht_month.wYear;
1694 lpht->st.wMonth = (day < 1) ? ht_month.wMonth -1 : ht_month.wMonth;
1695 lpht->st.wDay = (day < 1) ?
1696 MONTHCAL_MonthLength(ht_month.wMonth-1, ht_month.wYear) - day : day;
1698 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1700 lpht->uHit = MCHT_CALENDARWEEKNUM;
1701 lpht->st.wYear = ht_month.wYear;
1703 if (day < 1) {
1704 lpht->st.wMonth = ht_month.wMonth - 1;
1706 else if (day > MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear)) {
1707 lpht->st.wMonth = ht_month.wMonth + 1;
1709 else
1710 lpht->st.wMonth = ht_month.wMonth;
1712 if (day < 1) {
1713 lpht->st.wDay = MONTHCAL_MonthLength(ht_month.wMonth-1, ht_month.wYear) - day;
1715 else if (day > MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear)) {
1716 lpht->st.wDay = day - MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear);
1718 else
1719 lpht->st.wDay = day;
1721 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1723 lpht->st.wYear = ht_month.wYear;
1724 lpht->st.wMonth = ht_month.wMonth;
1725 if (day < 1)
1727 lpht->uHit = MCHT_CALENDARDATEPREV;
1728 MONTHCAL_GetPrevMonth(&lpht->st);
1729 lpht->st.wDay = MONTHCAL_MonthLength(lpht->st.wMonth, lpht->st.wYear) + day;
1731 else if (day > MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear))
1733 lpht->uHit = MCHT_CALENDARDATENEXT;
1734 MONTHCAL_GetNextMonth(&lpht->st);
1735 lpht->st.wDay = day - MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear);
1737 else {
1738 lpht->uHit = MCHT_CALENDARDATE;
1739 lpht->st.wDay = day;
1742 /* always update day of week */
1743 MONTHCAL_CalculateDayOfWeek(&lpht->st, TRUE);
1746 return lpht->uHit;
1749 /* MCN_GETDAYSTATE notification helper */
1750 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1752 if(infoPtr->dwStyle & MCS_DAYSTATE) {
1753 NMDAYSTATE nmds;
1755 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1756 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1757 nmds.nmhdr.code = MCN_GETDAYSTATE;
1758 nmds.cDayState = infoPtr->monthRange;
1759 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1761 nmds.stStart = infoPtr->todaysDate;
1762 nmds.stStart.wYear = infoPtr->curSel.wYear;
1763 nmds.stStart.wMonth = infoPtr->curSel.wMonth;
1764 nmds.stStart.wDay = 1;
1766 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1767 memcpy(infoPtr->monthdayState, nmds.prgDayState, infoPtr->monthRange*sizeof(MONTHDAYSTATE));
1769 Free(nmds.prgDayState);
1773 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
1775 SYSTEMTIME st = infoPtr->curSel;
1777 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
1779 if(direction == DIRECTION_BACKWARD) MONTHCAL_GetPrevMonth(&st); else MONTHCAL_GetNextMonth(&st);
1781 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
1783 if(infoPtr->dwStyle & MCS_MULTISELECT)
1785 SYSTEMTIME range[2];
1787 range[0] = infoPtr->minSel;
1788 range[1] = infoPtr->maxSel;
1790 if(direction == DIRECTION_BACKWARD)
1792 MONTHCAL_GetPrevMonth(&range[0]);
1793 MONTHCAL_GetPrevMonth(&range[1]);
1795 else
1797 MONTHCAL_GetNextMonth(&range[0]);
1798 MONTHCAL_GetNextMonth(&range[1]);
1801 MONTHCAL_SetSelRange(infoPtr, range);
1803 else
1804 MONTHCAL_SetCurSel(infoPtr, &st);
1806 MONTHCAL_NotifyDayState(infoPtr);
1808 MONTHCAL_NotifySelectionChange(infoPtr);
1811 static LRESULT
1812 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1814 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1815 HMENU hMenu;
1816 POINT menupoint;
1817 WCHAR buf[32];
1819 hMenu = CreatePopupMenu();
1820 if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1822 WARN("Can't load resource\n");
1823 strcpyW(buf, todayW);
1825 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1826 menupoint.x = (short)LOWORD(lParam);
1827 menupoint.y = (short)HIWORD(lParam);
1828 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1829 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
1830 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1832 infoPtr->curSel = infoPtr->todaysDate;
1833 infoPtr->calendars[0].month = infoPtr->todaysDate;
1834 infoPtr->minSel = infoPtr->todaysDate;
1835 infoPtr->maxSel = infoPtr->todaysDate;
1836 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1839 return 0;
1842 /***
1843 * DESCRIPTION:
1844 * Subclassed edit control windproc function
1846 * PARAMETER(S):
1847 * [I] hwnd : the edit window handle
1848 * [I] uMsg : the message that is to be processed
1849 * [I] wParam : first message parameter
1850 * [I] lParam : second message parameter
1853 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1855 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
1857 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
1858 hwnd, uMsg, wParam, lParam);
1860 switch (uMsg)
1862 case WM_GETDLGCODE:
1863 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
1865 case WM_DESTROY:
1867 WNDPROC editProc = infoPtr->EditWndProc;
1868 infoPtr->EditWndProc = NULL;
1869 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
1870 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
1873 case WM_KILLFOCUS:
1874 break;
1876 case WM_KEYDOWN:
1877 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
1878 break;
1880 default:
1881 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
1884 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
1885 SendMessageW(hwnd, WM_CLOSE, 0, 0);
1886 return 0;
1889 /* creates updown control and edit box */
1890 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr)
1892 infoPtr->hWndYearEdit =
1893 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
1894 infoPtr->calendars[0].titleyear.left + 3, infoPtr->titlebtnnext.top,
1895 infoPtr->calendars[0].titleyear.right - infoPtr->calendars[0].titleyear.left + 4,
1896 infoPtr->textHeight, infoPtr->hwndSelf,
1897 NULL, NULL, NULL);
1899 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
1901 infoPtr->hWndYearUpDown =
1902 CreateWindowExW(0, UPDOWN_CLASSW, 0,
1903 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
1904 infoPtr->calendars[0].titleyear.right + 7, infoPtr->titlebtnnext.top,
1905 18, infoPtr->textHeight, infoPtr->hwndSelf,
1906 NULL, NULL, NULL);
1908 /* attach edit box */
1909 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
1910 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
1911 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
1912 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->curSel.wYear);
1914 /* subclass edit box */
1915 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
1916 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
1918 SetFocus(infoPtr->hWndYearEdit);
1921 static LRESULT
1922 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1924 MCHITTESTINFO ht;
1925 DWORD hit;
1927 /* Actually we don't need input focus for calendar, this is used to kill
1928 year updown and its buddy edit box */
1929 if (IsWindow(infoPtr->hWndYearUpDown))
1931 SetFocus(infoPtr->hwndSelf);
1932 return 0;
1935 SetCapture(infoPtr->hwndSelf);
1937 ht.cbSize = sizeof(MCHITTESTINFO);
1938 ht.pt.x = (short)LOWORD(lParam);
1939 ht.pt.y = (short)HIWORD(lParam);
1941 hit = MONTHCAL_HitTest(infoPtr, &ht);
1943 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
1945 switch(hit)
1947 case MCHT_TITLEBTNNEXT:
1948 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
1949 infoPtr->status = MC_NEXTPRESSED;
1950 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1951 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1952 return 0;
1954 case MCHT_TITLEBTNPREV:
1955 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
1956 infoPtr->status = MC_PREVPRESSED;
1957 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1958 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1959 return 0;
1961 case MCHT_TITLEMONTH:
1963 HMENU hMenu = CreatePopupMenu();
1964 WCHAR buf[32];
1965 POINT menupoint;
1966 INT i;
1968 for (i = 0; i < 12; i++)
1970 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
1971 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
1973 menupoint.x = ht.pt.x;
1974 menupoint.y = ht.pt.y;
1975 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1976 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
1977 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
1979 if ((i > 0) && (i < 13) && infoPtr->curSel.wMonth != i)
1981 infoPtr->curSel.wMonth = i;
1982 MONTHCAL_IsDateInValidRange(infoPtr, &infoPtr->curSel, TRUE);
1983 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1985 return 0;
1987 case MCHT_TITLEYEAR:
1989 MONTHCAL_EditYear(infoPtr);
1990 return 0;
1992 case MCHT_TODAYLINK:
1994 infoPtr->curSel = infoPtr->todaysDate;
1995 infoPtr->calendars[0].month = infoPtr->todaysDate;
1996 infoPtr->minSel = infoPtr->todaysDate;
1997 infoPtr->maxSel = infoPtr->todaysDate;
1998 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2000 MONTHCAL_NotifySelectionChange(infoPtr);
2001 MONTHCAL_NotifySelect(infoPtr);
2002 return 0;
2004 case MCHT_CALENDARDATENEXT:
2005 case MCHT_CALENDARDATEPREV:
2006 case MCHT_CALENDARDATE:
2008 SYSTEMTIME st[2];
2010 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2012 st[0] = st[1] = ht.st;
2013 /* clear selection range */
2014 MONTHCAL_SetSelRange(infoPtr, st);
2016 infoPtr->status = MC_SEL_LBUTDOWN;
2017 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
2018 return 0;
2022 return 1;
2026 static LRESULT
2027 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2029 NMHDR nmhdr;
2030 MCHITTESTINFO ht;
2031 DWORD hit;
2033 TRACE("\n");
2035 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2036 RECT *r;
2038 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2039 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2040 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2042 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2045 ReleaseCapture();
2047 /* always send NM_RELEASEDCAPTURE notification */
2048 nmhdr.hwndFrom = infoPtr->hwndSelf;
2049 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2050 nmhdr.code = NM_RELEASEDCAPTURE;
2051 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2053 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2055 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2057 ht.cbSize = sizeof(MCHITTESTINFO);
2058 ht.pt.x = (short)LOWORD(lParam);
2059 ht.pt.y = (short)HIWORD(lParam);
2060 hit = MONTHCAL_HitTest(infoPtr, &ht);
2062 infoPtr->status = MC_SEL_LBUTUP;
2063 MONTHCAL_SetDayFocus(infoPtr, NULL);
2065 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2067 SYSTEMTIME sel = infoPtr->curSel;
2069 /* will be invalidated here */
2070 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2072 /* send MCN_SELCHANGE only if new date selected */
2073 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2074 MONTHCAL_NotifySelectionChange(infoPtr);
2076 MONTHCAL_NotifySelect(infoPtr);
2079 return 0;
2083 static LRESULT
2084 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2086 TRACE("%ld\n", id);
2088 switch(id) {
2089 case MC_PREVNEXTMONTHTIMER:
2090 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2091 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2092 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2093 break;
2094 case MC_TODAYUPDATETIMER:
2096 SYSTEMTIME st;
2098 if(infoPtr->todaySet) return 0;
2100 GetLocalTime(&st);
2101 MONTHCAL_UpdateToday(infoPtr, &st);
2103 /* notification sent anyway */
2104 MONTHCAL_NotifySelectionChange(infoPtr);
2106 return 0;
2108 default:
2109 ERR("got unknown timer %ld\n", id);
2110 break;
2113 return 0;
2117 static LRESULT
2118 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2120 MCHITTESTINFO ht;
2121 SYSTEMTIME st_ht;
2122 INT hit;
2123 RECT r;
2125 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2127 ht.cbSize = sizeof(MCHITTESTINFO);
2128 ht.pt.x = (short)LOWORD(lParam);
2129 ht.pt.y = (short)HIWORD(lParam);
2131 hit = MONTHCAL_HitTest(infoPtr, &ht);
2133 /* not on the calendar date numbers? bail out */
2134 TRACE("hit:%x\n",hit);
2135 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2137 MONTHCAL_SetDayFocus(infoPtr, NULL);
2138 return 0;
2141 st_ht = ht.st;
2143 /* if pointer is over focused day still there's nothing to do */
2144 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2146 MONTHCAL_CalcPosFromDay(infoPtr, &ht.st, &r);
2148 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2149 SYSTEMTIME st[2];
2151 MONTHCAL_GetSelRange(infoPtr, st);
2153 /* If we're still at the first selected date and range is empty, return.
2154 If range isn't empty we should change range to a single firstSel */
2155 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2156 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2158 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2160 st[0] = infoPtr->firstSel;
2161 /* we should overwrite timestamp here */
2162 MONTHCAL_CopyDate(&st_ht, &st[1]);
2164 /* bounds will be swapped here if needed */
2165 MONTHCAL_SetSelRange(infoPtr, st);
2167 return 0;
2170 done:
2172 /* FIXME: this should specify a rectangle containing only the days that changed
2173 using InvalidateRect */
2174 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2176 return 0;
2180 static LRESULT
2181 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2183 HDC hdc;
2184 PAINTSTRUCT ps;
2186 if (hdc_paint)
2188 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2189 hdc = hdc_paint;
2191 else
2192 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2194 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2195 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2196 return 0;
2199 static LRESULT
2200 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2202 HBRUSH hbr;
2203 RECT rc;
2205 if (!GetClipBox(hdc, &rc)) return FALSE;
2207 /* fill background */
2208 hbr = CreateSolidBrush (infoPtr->bk);
2209 FillRect(hdc, &rc, hbr);
2210 DeleteObject(hbr);
2212 return TRUE;
2215 static LRESULT
2216 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2218 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2220 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2221 return 0;
2223 if (options & PRF_ERASEBKGND)
2224 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2226 if (options & PRF_CLIENT)
2227 MONTHCAL_Paint(infoPtr, hdc);
2229 return 0;
2232 static LRESULT
2233 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2235 TRACE("\n");
2237 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2239 return 0;
2242 /* sets the size information */
2243 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2245 static const WCHAR O0W[] = { '0','0',0 };
2246 HDC hdc = GetDC(infoPtr->hwndSelf);
2247 RECT *title=&infoPtr->calendars[0].title;
2248 RECT *prev=&infoPtr->titlebtnprev;
2249 RECT *next=&infoPtr->titlebtnnext;
2250 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2251 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2252 RECT *wdays=&infoPtr->calendars[0].wdays;
2253 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2254 RECT *days=&infoPtr->calendars[0].days;
2255 RECT *todayrect=&infoPtr->todayrect;
2256 SIZE size, sz;
2257 TEXTMETRICW tm;
2258 HFONT currentFont;
2259 INT xdiv, dx, dy, i;
2260 RECT rcClient;
2261 WCHAR buff[80];
2263 GetClientRect(infoPtr->hwndSelf, &rcClient);
2265 currentFont = SelectObject(hdc, infoPtr->hFont);
2267 /* get the height and width of each day's text */
2268 GetTextMetricsW(hdc, &tm);
2269 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2271 /* find largest abbreviated day name for current locale */
2272 size.cx = sz.cx = 0;
2273 for (i = 0; i < 7; i++)
2275 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + i,
2276 buff, countof(buff)))
2278 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2279 if (sz.cx > size.cx) size.cx = sz.cx;
2281 else /* locale independent fallback on failure */
2283 static const WCHAR SunW[] = { 'S','u','n',0 };
2285 GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2286 break;
2290 infoPtr->textWidth = size.cx + 2;
2292 /* recalculate the height and width increments and offsets */
2293 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2295 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2297 infoPtr->width_increment = size.cx * 2 + 4;
2298 infoPtr->height_increment = infoPtr->textHeight;
2300 /* calculate title area */
2301 title->top = 0;
2302 title->bottom = 3 * infoPtr->height_increment / 2;
2303 title->left = 0;
2304 title->right = infoPtr->width_increment * xdiv;
2306 /* set the dimensions of the next and previous buttons and center */
2307 /* the month text vertically */
2308 prev->top = next->top = title->top + 4;
2309 prev->bottom = next->bottom = title->bottom - 4;
2310 prev->left = title->left + 4;
2311 prev->right = prev->left + (title->bottom - title->top);
2312 next->right = title->right - 4;
2313 next->left = next->right - (title->bottom - title->top);
2315 /* titlemonth->left and right change based upon the current month */
2316 /* and are recalculated in refresh as the current month may change */
2317 /* without the control being resized */
2318 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2319 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2321 /* setup the dimensions of the rectangle we draw the names of the */
2322 /* days of the week in */
2323 weeknumrect->left = 0;
2325 if(infoPtr->dwStyle & MCS_WEEKNUMBERS)
2326 weeknumrect->right = prev->right;
2327 else
2328 weeknumrect->right = weeknumrect->left;
2330 wdays->left = days->left = weeknumrect->right;
2331 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2332 wdays->top = title->bottom;
2333 wdays->bottom = wdays->top + infoPtr->height_increment;
2335 days->top = weeknumrect->top = wdays->bottom;
2336 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2338 todayrect->left = 0;
2339 todayrect->right = title->right;
2340 todayrect->top = days->bottom;
2341 todayrect->bottom = days->bottom + infoPtr->height_increment;
2343 /* offset all rectangles to center in client area */
2344 dx = (rcClient.right - title->right) / 2;
2345 dy = (rcClient.bottom - todayrect->bottom) / 2;
2347 /* if calendar doesn't fit client area show it at left/top bounds */
2348 if (title->left + dx < 0) dx = 0;
2349 if (title->top + dy < 0) dy = 0;
2351 if (dx != 0 || dy != 0)
2353 OffsetRect(title, dx, dy);
2354 OffsetRect(prev, dx, dy);
2355 OffsetRect(next, dx, dy);
2356 OffsetRect(titlemonth, dx, dy);
2357 OffsetRect(titleyear, dx, dy);
2358 OffsetRect(wdays, dx, dy);
2359 OffsetRect(weeknumrect, dx, dy);
2360 OffsetRect(days, dx, dy);
2361 OffsetRect(todayrect, dx, dy);
2364 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2365 infoPtr->width_increment,infoPtr->height_increment,
2366 wine_dbgstr_rect(&rcClient),
2367 wine_dbgstr_rect(title),
2368 wine_dbgstr_rect(wdays),
2369 wine_dbgstr_rect(days),
2370 wine_dbgstr_rect(todayrect));
2372 /* restore the originally selected font */
2373 SelectObject(hdc, currentFont);
2375 ReleaseDC(infoPtr->hwndSelf, hdc);
2378 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2380 TRACE("(width=%d, height=%d)\n", Width, Height);
2382 MONTHCAL_UpdateSize(infoPtr);
2384 /* invalidate client area and erase background */
2385 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2387 return 0;
2390 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2392 return (LRESULT)infoPtr->hFont;
2395 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2397 HFONT hOldFont;
2398 LOGFONTW lf;
2400 if (!hFont) return 0;
2402 hOldFont = infoPtr->hFont;
2403 infoPtr->hFont = hFont;
2405 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2406 lf.lfWeight = FW_BOLD;
2407 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2409 MONTHCAL_UpdateSize(infoPtr);
2411 if (redraw)
2412 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2414 return (LRESULT)hOldFont;
2417 /* update theme after a WM_THEMECHANGED message */
2418 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2420 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2421 CloseThemeData (theme);
2422 OpenThemeData (infoPtr->hwndSelf, themeClass);
2423 return 0;
2426 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2427 const STYLESTRUCT *lpss)
2429 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2430 wStyleType, lpss->styleOld, lpss->styleNew);
2432 if (wStyleType != GWL_STYLE) return 0;
2434 infoPtr->dwStyle = lpss->styleNew;
2436 /* make room for week numbers */
2437 if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2438 MONTHCAL_UpdateSize(infoPtr);
2440 return 0;
2443 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2444 STYLESTRUCT *lpss)
2446 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2447 wStyleType, lpss->styleOld, lpss->styleNew);
2449 /* block MCS_MULTISELECT change */
2450 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2452 if (lpss->styleOld & MCS_MULTISELECT)
2453 lpss->styleNew |= MCS_MULTISELECT;
2454 else
2455 lpss->styleNew &= ~MCS_MULTISELECT;
2458 return 0;
2461 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2462 static LRESULT
2463 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2465 MONTHCAL_INFO *infoPtr;
2467 /* allocate memory for info structure */
2468 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2469 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2471 if (infoPtr == NULL) {
2472 ERR("could not allocate info memory!\n");
2473 return 0;
2476 infoPtr->hwndSelf = hwnd;
2477 infoPtr->hwndNotify = lpcs->hwndParent;
2478 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2479 infoPtr->calendars = Alloc(sizeof(CALENDAR_INFO));
2480 if (!infoPtr->calendars) goto fail;
2482 infoPtr->cal_num = 1;
2484 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
2486 /* initialize info structure */
2487 /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
2489 GetLocalTime(&infoPtr->todaysDate);
2490 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2492 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2493 infoPtr->monthRange = 3;
2495 infoPtr->monthdayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
2496 if (!infoPtr->monthdayState) goto fail;
2498 infoPtr->titlebk = comctl32_color.clrActiveCaption;
2499 infoPtr->titletxt = comctl32_color.clrWindow;
2500 infoPtr->monthbk = comctl32_color.clrWindow;
2501 infoPtr->trailingtxt = comctl32_color.clrGrayText;
2502 infoPtr->bk = comctl32_color.clrWindow;
2503 infoPtr->txt = comctl32_color.clrWindowText;
2505 infoPtr->minSel = infoPtr->todaysDate;
2506 infoPtr->maxSel = infoPtr->todaysDate;
2507 infoPtr->curSel = infoPtr->todaysDate;
2508 infoPtr->calendars[0].month = infoPtr->todaysDate;
2509 infoPtr->isUnicode = TRUE;
2511 /* call MONTHCAL_UpdateSize to set all of the dimensions */
2512 /* of the control */
2513 MONTHCAL_UpdateSize(infoPtr);
2515 /* today auto update timer, to be freed only on control destruction */
2516 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2518 OpenThemeData (infoPtr->hwndSelf, themeClass);
2520 return 0;
2522 fail:
2523 Free(infoPtr->monthdayState);
2524 Free(infoPtr->calendars);
2525 Free(infoPtr);
2526 return 0;
2529 static LRESULT
2530 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2532 /* free month calendar info data */
2533 Free(infoPtr->monthdayState);
2534 Free(infoPtr->calendars);
2535 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2537 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2539 Free(infoPtr);
2540 return 0;
2544 * Handler for WM_NOTIFY messages
2546 static LRESULT
2547 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2549 /* notification from year edit updown */
2550 if (hdr->code == UDN_DELTAPOS)
2552 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2554 if (hdr->hwndFrom == infoPtr->hWndYearUpDown)
2556 /* year value limits are set up explicitly after updown creation */
2557 if ((nmud->iDelta + nmud->iPos) != infoPtr->curSel.wYear)
2559 SYSTEMTIME new_date = infoPtr->curSel;
2561 new_date.wYear = nmud->iDelta + nmud->iPos;
2562 MONTHCAL_SetCurSel(infoPtr, &new_date);
2566 return 0;
2569 static inline BOOL
2570 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2572 BOOL prev = infoPtr->isUnicode;
2573 infoPtr->isUnicode = isUnicode;
2574 return prev;
2577 static inline BOOL
2578 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2580 return infoPtr->isUnicode;
2583 static LRESULT WINAPI
2584 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2586 MONTHCAL_INFO *infoPtr;
2588 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2590 infoPtr = MONTHCAL_GetInfoPtr(hwnd);
2591 if (!infoPtr && (uMsg != WM_CREATE))
2592 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2593 switch(uMsg)
2595 case MCM_GETCURSEL:
2596 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2598 case MCM_SETCURSEL:
2599 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2601 case MCM_GETMAXSELCOUNT:
2602 return MONTHCAL_GetMaxSelCount(infoPtr);
2604 case MCM_SETMAXSELCOUNT:
2605 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2607 case MCM_GETSELRANGE:
2608 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2610 case MCM_SETSELRANGE:
2611 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2613 case MCM_GETMONTHRANGE:
2614 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2616 case MCM_SETDAYSTATE:
2617 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2619 case MCM_GETMINREQRECT:
2620 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2622 case MCM_GETCOLOR:
2623 return MONTHCAL_GetColor(infoPtr, wParam);
2625 case MCM_SETCOLOR:
2626 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2628 case MCM_GETTODAY:
2629 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2631 case MCM_SETTODAY:
2632 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2634 case MCM_HITTEST:
2635 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2637 case MCM_GETFIRSTDAYOFWEEK:
2638 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2640 case MCM_SETFIRSTDAYOFWEEK:
2641 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2643 case MCM_GETRANGE:
2644 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2646 case MCM_SETRANGE:
2647 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2649 case MCM_GETMONTHDELTA:
2650 return MONTHCAL_GetMonthDelta(infoPtr);
2652 case MCM_SETMONTHDELTA:
2653 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2655 case MCM_GETMAXTODAYWIDTH:
2656 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2658 case MCM_SETUNICODEFORMAT:
2659 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2661 case MCM_GETUNICODEFORMAT:
2662 return MONTHCAL_GetUnicodeFormat(infoPtr);
2664 case WM_GETDLGCODE:
2665 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2667 case WM_RBUTTONUP:
2668 return MONTHCAL_RButtonUp(infoPtr, lParam);
2670 case WM_LBUTTONDOWN:
2671 return MONTHCAL_LButtonDown(infoPtr, lParam);
2673 case WM_MOUSEMOVE:
2674 return MONTHCAL_MouseMove(infoPtr, lParam);
2676 case WM_LBUTTONUP:
2677 return MONTHCAL_LButtonUp(infoPtr, lParam);
2679 case WM_PAINT:
2680 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2682 case WM_PRINTCLIENT:
2683 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2685 case WM_ERASEBKGND:
2686 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2688 case WM_SETFOCUS:
2689 return MONTHCAL_SetFocus(infoPtr);
2691 case WM_SIZE:
2692 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2694 case WM_NOTIFY:
2695 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2697 case WM_CREATE:
2698 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2700 case WM_SETFONT:
2701 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2703 case WM_GETFONT:
2704 return MONTHCAL_GetFont(infoPtr);
2706 case WM_TIMER:
2707 return MONTHCAL_Timer(infoPtr, wParam);
2709 case WM_THEMECHANGED:
2710 return theme_changed (infoPtr);
2712 case WM_DESTROY:
2713 return MONTHCAL_Destroy(infoPtr);
2715 case WM_SYSCOLORCHANGE:
2716 COMCTL32_RefreshSysColors();
2717 return 0;
2719 case WM_STYLECHANGED:
2720 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2722 case WM_STYLECHANGING:
2723 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2725 default:
2726 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2727 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2728 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2733 void
2734 MONTHCAL_Register(void)
2736 WNDCLASSW wndClass;
2738 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2739 wndClass.style = CS_GLOBALCLASS;
2740 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2741 wndClass.cbClsExtra = 0;
2742 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2743 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2744 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2745 wndClass.lpszClassName = MONTHCAL_CLASSW;
2747 RegisterClassW(&wndClass);
2751 void
2752 MONTHCAL_Unregister(void)
2754 UnregisterClassW(MONTHCAL_CLASSW, NULL);