comctl32/monthcal: Prepare MCM_GETMONTHRANGE with GMR_VISIBLE for multiple calendars.
[wine/multimedia.git] / dlls / comctl32 / monthcal.c
blobc5a71226dca69cdc5d122f48fb727b9e90e08f7d
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 };
154 #define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0))
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 /* properly updates date to point on next month */
453 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
455 if(++date->wMonth > 12)
457 date->wMonth = 1;
458 date->wYear++;
460 MONTHCAL_CalculateDayOfWeek(date, TRUE);
463 /* properly updates date to point on prev month */
464 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
466 if(--date->wMonth < 1)
468 date->wMonth = 12;
469 date->wYear--;
471 MONTHCAL_CalculateDayOfWeek(date, TRUE);
474 /* Returns full date for a first currently visible day */
475 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
477 /* zero indexed calendar has the earliest date */
478 SYSTEMTIME st_first = infoPtr->calendars[0].month;
479 INT firstDay;
481 st_first.wDay = 1;
482 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
484 *date = infoPtr->calendars[0].month;
485 MONTHCAL_GetPrevMonth(date);
487 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
488 (infoPtr->firstDay - firstDay) % 7 + 1;
490 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
491 date->wDay -= 7;
493 /* fix day of week */
494 MONTHCAL_CalculateDayOfWeek(date, TRUE);
497 /* Returns full date for a last currently visible day */
498 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
500 /* the latest date is in latest calendar */
501 SYSTEMTIME st, lt_month = infoPtr->calendars[infoPtr->cal_num-1].month;
503 *date = lt_month;
504 MONTHCAL_GetNextMonth(date);
506 MONTHCAL_GetMinDate(infoPtr, &st);
507 /* Use month length to get max day. 42 means max day count in calendar area */
508 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
509 MONTHCAL_MonthLength(lt_month.wMonth, lt_month.wYear);
511 /* fix day of week */
512 MONTHCAL_CalculateDayOfWeek(date, TRUE);
515 /* From a given point, calculate the row (weekpos), column(daypos)
516 and day in the calendar. day== 0 mean the last day of tha last month
518 static int MONTHCAL_CalcDayFromPos(const MONTHCAL_INFO *infoPtr, int x, int y,
519 int *daypos, int *weekpos)
521 int retval, firstDay;
522 RECT rcClient;
523 SYSTEMTIME st = infoPtr->curSel;
525 GetClientRect(infoPtr->hwndSelf, &rcClient);
527 /* if the point is outside the x bounds of the window put
528 it at the boundary */
529 if (x > rcClient.right)
530 x = rcClient.right;
532 *daypos = (x - infoPtr->calendars[0].days.left ) / infoPtr->width_increment;
533 *weekpos = (y - infoPtr->calendars[0].days.top ) / infoPtr->height_increment;
535 st.wDay = 1;
536 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
537 retval = *daypos + (7 * *weekpos) - firstDay;
538 return retval;
541 /* Sets the RECT struct r to the rectangle around the date
543 * PARAMETERS
545 * [I] infoPtr : pointer to control data
546 * [I] date : date value
547 * [O] x : day column (zero based)
548 * [O] y : week column (zero based)
550 static void MONTHCAL_CalcDayXY(const MONTHCAL_INFO *infoPtr,
551 const SYSTEMTIME *date, int *x, int *y)
553 SYSTEMTIME st = infoPtr->curSel;
554 LONG cmp;
555 int first;
557 st.wDay = 1;
558 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
560 cmp = MONTHCAL_CompareMonths(date, &infoPtr->curSel);
562 /* previous month */
563 if(cmp == -1) {
564 *x = (first - MONTHCAL_MonthLength(date->wMonth, infoPtr->curSel.wYear) + date->wDay) % 7;
565 *y = 0;
566 return;
569 /* next month calculation is same as for current,
570 just add current month length */
571 if(cmp == 1) {
572 first += MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear);
575 *x = (date->wDay + first) % 7;
576 *y = (date->wDay + first - *x) / 7;
580 /* x: column(day), y: row(week) */
581 static inline void MONTHCAL_CalcDayRect(const MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
583 r->left = infoPtr->calendars[0].days.left + x * infoPtr->width_increment;
584 r->right = r->left + infoPtr->width_increment;
585 r->top = infoPtr->calendars[0].days.top + y * infoPtr->height_increment;
586 r->bottom = r->top + infoPtr->textHeight;
590 /* Sets the RECT struct r to the rectangle around the date */
591 static inline void MONTHCAL_CalcPosFromDay(const MONTHCAL_INFO *infoPtr,
592 const SYSTEMTIME *date, RECT *r)
594 int x, y;
596 MONTHCAL_CalcDayXY(infoPtr, date, &x, &y);
597 MONTHCAL_CalcDayRect(infoPtr, r, x, y);
600 /* Focused day helper:
602 - set focused date to given value;
603 - reset to zero value if NULL passed;
604 - invalidate previous and new day rectangle only if needed.
606 Returns TRUE if focused day changed, FALSE otherwise.
608 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
610 RECT r;
612 if(st)
614 /* there's nothing to do if it's the same date,
615 mouse move within same date rectangle case */
616 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
618 /* invalidate old focused day */
619 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
620 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
622 infoPtr->focusedSel = *st;
625 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
627 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
628 infoPtr->focusedSel = st_null;
630 /* on set invalidates new day, on reset clears previous focused day */
631 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
633 return TRUE;
636 /* Draw today day mark rectangle
638 * [I] hdc : context to draw in
639 * [I] day : day to mark with rectangle
642 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
643 const SYSTEMTIME *date)
645 HPEN hRedPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
646 HPEN hOldPen2 = SelectObject(hdc, hRedPen);
647 HBRUSH hOldBrush;
648 RECT day_rect;
650 MONTHCAL_CalcPosFromDay(infoPtr, date, &day_rect);
652 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
653 Rectangle(hdc, day_rect.left, day_rect.top, day_rect.right, day_rect.bottom);
655 SelectObject(hdc, hOldBrush);
656 DeleteObject(hRedPen);
657 SelectObject(hdc, hOldPen2);
660 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
661 int bold, const PAINTSTRUCT *ps)
663 static const WCHAR fmtW[] = { '%','d',0 };
664 WCHAR buf[10];
665 RECT r, r_temp;
666 static BOOL bold_selected;
667 BOOL selected_day = FALSE;
668 HBRUSH hbr;
669 COLORREF oldCol = 0;
670 COLORREF oldBk = 0;
672 /* No need to check styles: when selection is not valid, it is set to zero.
673 * 1<day<31, so everything is OK.
676 MONTHCAL_CalcPosFromDay(infoPtr, st, &r);
677 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
679 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
680 (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->monthbk);
685 oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
686 hbr = GetSysColorBrush(COLOR_HIGHLIGHT);
687 FillRect(hdc, &r, hbr);
689 selected_day = TRUE;
692 if(bold && !bold_selected) {
693 SelectObject(hdc, infoPtr->hBoldFont);
694 bold_selected = TRUE;
696 if(!bold && bold_selected) {
697 SelectObject(hdc, infoPtr->hFont);
698 bold_selected = FALSE;
701 SetBkMode(hdc,TRANSPARENT);
702 wsprintfW(buf, fmtW, st->wDay);
703 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
705 if(selected_day) {
706 SetTextColor(hdc, oldCol);
707 SetBkColor(hdc, oldBk);
712 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, BOOL btnNext)
714 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
715 RECT *r = btnNext ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
716 BOOL pressed = btnNext ? (infoPtr->status & MC_NEXTPRESSED) :
717 (infoPtr->status & MC_PREVPRESSED);
718 if (theme)
720 static const int states[] = {
721 /* Prev button */
722 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
723 /* Next button */
724 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
726 int stateNum = btnNext ? 3 : 0;
727 if (pressed)
728 stateNum += 1;
729 else
731 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
733 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
735 else
737 int style = btnNext ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
738 if (pressed)
739 style |= DFCS_PUSHED;
740 else
742 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
745 DrawFrameControl(hdc, r, DFC_SCROLL, style);
748 /* paint a title with buttons and month/year string */
749 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
751 static const WCHAR fmt_monthW[] = { '%','s',' ','%','l','d',0 };
752 RECT *title = &infoPtr->calendars[calIdx].title;
753 WCHAR buf_month[80], buf_fmt[80];
754 HBRUSH hbr;
755 SIZE sz;
757 /* fill header box */
758 hbr = CreateSolidBrush(infoPtr->titlebk);
759 FillRect(hdc, title, hbr);
760 DeleteObject(hbr);
762 /* month/year string */
763 SetBkColor(hdc, infoPtr->titlebk);
764 SetTextColor(hdc, infoPtr->titletxt);
765 SelectObject(hdc, infoPtr->hBoldFont);
767 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+infoPtr->curSel.wMonth-1,
768 buf_month, countof(buf_month));
770 wsprintfW(buf_fmt, fmt_monthW, buf_month, infoPtr->curSel.wYear);
771 DrawTextW(hdc, buf_fmt, strlenW(buf_fmt), title,
772 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
774 /* update title rectangles with current month - used while testing hits */
775 GetTextExtentPoint32W(hdc, buf_fmt, strlenW(buf_fmt), &sz);
776 infoPtr->calendars[calIdx].titlemonth.left = title->right / 2 + title->left / 2 - sz.cx / 2;
777 infoPtr->calendars[calIdx].titleyear.right = title->right / 2 + title->left / 2 + sz.cx / 2;
779 GetTextExtentPoint32W(hdc, buf_month, strlenW(buf_month), &sz);
780 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
781 infoPtr->calendars[calIdx].titleyear.left = infoPtr->calendars[calIdx].titlemonth.right;
784 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
786 static const WCHAR fmt_weekW[] = { '%','d',0 };
787 INT mindays, weeknum, weeknum1, startofprescal;
788 SYSTEMTIME st = infoPtr->curSel;
789 RECT r;
790 WCHAR buf[80];
791 INT i, prev_month;
793 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
795 MONTHCAL_GetMinDate(infoPtr, &st);
796 startofprescal = st.wDay;
797 st = infoPtr->curSel;
799 prev_month = infoPtr->curSel.wMonth - 1;
800 if(prev_month == 0) prev_month = 12;
803 Rules what week to call the first week of a new year:
804 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
805 The week containing Jan 1 is the first week of year
806 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
807 First week of year must contain 4 days of the new year
808 LOCALE_IFIRSTWEEKOFYEAR == 1 (what contries?)
809 The first week of the year must contain only days of the new year
811 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
812 weeknum = atoiW(buf);
813 switch (weeknum)
815 case 1: mindays = 6;
816 break;
817 case 2: mindays = 3;
818 break;
819 case 0: mindays = 0;
820 break;
821 default:
822 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
823 mindays = 0;
826 if (infoPtr->curSel.wMonth == 1)
828 /* calculate all those exceptions for january */
829 st.wDay = st.wMonth = 1;
830 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
831 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
832 weeknum = 1;
833 else
835 weeknum = 0;
836 for(i = 0; i < 11; i++)
837 weeknum += MONTHCAL_MonthLength(i+1, infoPtr->curSel.wYear - 1);
839 weeknum += startofprescal + 7;
840 weeknum /= 7;
841 st.wYear -= 1;
842 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
843 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
846 else
848 weeknum = 0;
849 for(i = 0; i < prev_month - 1; i++)
850 weeknum += MONTHCAL_MonthLength(i+1, infoPtr->curSel.wYear);
852 weeknum += startofprescal + 7;
853 weeknum /= 7;
854 st.wDay = st.wMonth = 1;
855 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
856 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
859 r = infoPtr->calendars[calIdx].weeknums;
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->curSel.wMonth == infoPtr->todaysDate.wMonth) &&
923 (infoPtr->curSel.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 /* paint a calendar area */
938 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
940 INT prev_month, i, j;
941 WCHAR buf[80];
942 HBRUSH hbr;
943 RECT r, fill_bk_rect;
944 int mask;
945 SYSTEMTIME st;
947 /* fill whole days area - from week days area to today note rectangle */
948 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
949 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
950 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
952 hbr = CreateSolidBrush(infoPtr->monthbk);
953 FillRect(hdc, &fill_bk_rect, hbr);
954 DeleteObject(hbr);
956 /* draw line under day abbreviations */
957 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
958 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
959 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
960 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
962 prev_month = infoPtr->curSel.wMonth - 1;
963 if (prev_month == 0) prev_month = 12;
965 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
966 infoPtr->calendars[calIdx].weeknums.right;
968 /* 1. draw day abbreviations */
969 SelectObject(hdc, infoPtr->hFont);
970 SetBkColor(hdc, infoPtr->monthbk);
971 SetTextColor(hdc, infoPtr->trailingtxt);
972 /* rectangle to draw a single day abbreviation within */
973 r = infoPtr->calendars[calIdx].wdays;
974 r.right = r.left + infoPtr->width_increment;
976 i = infoPtr->firstDay;
977 for(j = 0; j < 7; j++) {
978 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
979 DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
980 OffsetRect(&r, infoPtr->width_increment, 0);
983 /* 2. previous and next months */
984 if (!(infoPtr->dwStyle & MCS_NOTRAILINGDATES) && (calIdx == 0 || calIdx == infoPtr->cal_num - 1))
986 SYSTEMTIME st_max;
988 SetTextColor(hdc, infoPtr->trailingtxt);
990 /* draw prev month */
991 if (calIdx == 0)
993 MONTHCAL_GetMinDate(infoPtr, &st);
994 mask = 1 << (st.wDay-1);
996 while(st.wDay <= MONTHCAL_MonthLength(prev_month, infoPtr->curSel.wYear))
998 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[0] & mask, ps);
999 mask <<= 1;
1000 st.wDay++;
1004 /* draw next month */
1005 if (calIdx == infoPtr->cal_num - 1)
1007 st = infoPtr->curSel;
1008 st.wDay = 1;
1009 MONTHCAL_GetNextMonth(&st);
1010 MONTHCAL_GetMaxDate(infoPtr, &st_max);
1011 mask = 1;
1013 while(st.wDay <= st_max.wDay)
1015 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[2] & mask, ps);
1016 mask <<= 1;
1017 st.wDay++;
1022 /* 3. current month */
1023 SetTextColor(hdc, infoPtr->txt);
1024 st = infoPtr->curSel;
1025 st.wDay = 1;
1026 mask = 1;
1027 while(st.wDay <= MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear)) {
1028 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[1] & mask, ps);
1029 mask <<= 1;
1030 st.wDay++;
1034 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1036 COLORREF old_text_clr, old_bk_clr;
1037 HFONT old_font;
1038 INT i;
1040 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1041 old_bk_clr = GetBkColor(hdc);
1042 old_font = GetCurrentObject(hdc, OBJ_FONT);
1044 for (i = 0; i < infoPtr->cal_num; i++)
1046 RECT *title = &infoPtr->calendars[i].title;
1047 RECT r;
1049 /* draw title, redraw all its elements */
1050 if (IntersectRect(&r, &(ps->rcPaint), title))
1051 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1053 /* draw calendar area */
1054 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1055 if (IntersectRect(&r, &(ps->rcPaint), &r))
1056 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1058 /* week numbers */
1059 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1062 /* focus and today rectangle */
1063 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1065 /* today at the bottom left */
1066 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1068 /* navigation buttons */
1069 MONTHCAL_PaintButton(infoPtr, hdc, FALSE);
1070 MONTHCAL_PaintButton(infoPtr, hdc, TRUE);
1072 /* restore context */
1073 SetBkColor(hdc, old_bk_clr);
1074 SelectObject(hdc, old_font);
1075 SetTextColor(hdc, old_text_clr);
1078 static LRESULT
1079 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, LPRECT lpRect)
1081 TRACE("rect %p\n", lpRect);
1083 if(!lpRect) return FALSE;
1085 lpRect->left = infoPtr->calendars[0].title.left;
1086 lpRect->top = infoPtr->calendars[0].title.top;
1087 lpRect->right = infoPtr->calendars[0].title.right;
1088 lpRect->bottom = infoPtr->todayrect.bottom;
1090 AdjustWindowRect(lpRect, infoPtr->dwStyle, FALSE);
1092 /* minimal rectangle is zero based */
1093 OffsetRect(lpRect, -lpRect->left, -lpRect->top);
1095 TRACE("%s\n", wine_dbgstr_rect(lpRect));
1097 return TRUE;
1101 static LRESULT
1102 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, INT index)
1104 TRACE("\n");
1106 switch(index) {
1107 case MCSC_BACKGROUND:
1108 return infoPtr->bk;
1109 case MCSC_TEXT:
1110 return infoPtr->txt;
1111 case MCSC_TITLEBK:
1112 return infoPtr->titlebk;
1113 case MCSC_TITLETEXT:
1114 return infoPtr->titletxt;
1115 case MCSC_MONTHBK:
1116 return infoPtr->monthbk;
1117 case MCSC_TRAILINGTEXT:
1118 return infoPtr->trailingtxt;
1121 return -1;
1125 static LRESULT
1126 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, INT index, COLORREF color)
1128 COLORREF prev = -1;
1130 TRACE("%d: color %08x\n", index, color);
1132 switch(index) {
1133 case MCSC_BACKGROUND:
1134 prev = infoPtr->bk;
1135 infoPtr->bk = color;
1136 break;
1137 case MCSC_TEXT:
1138 prev = infoPtr->txt;
1139 infoPtr->txt = color;
1140 break;
1141 case MCSC_TITLEBK:
1142 prev = infoPtr->titlebk;
1143 infoPtr->titlebk = color;
1144 break;
1145 case MCSC_TITLETEXT:
1146 prev=infoPtr->titletxt;
1147 infoPtr->titletxt = color;
1148 break;
1149 case MCSC_MONTHBK:
1150 prev = infoPtr->monthbk;
1151 infoPtr->monthbk = color;
1152 break;
1153 case MCSC_TRAILINGTEXT:
1154 prev = infoPtr->trailingtxt;
1155 infoPtr->trailingtxt = color;
1156 break;
1159 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND ? TRUE : FALSE);
1160 return prev;
1164 static LRESULT
1165 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1167 TRACE("\n");
1169 if(infoPtr->delta)
1170 return infoPtr->delta;
1171 else
1172 return infoPtr->visible;
1176 static LRESULT
1177 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1179 INT prev = infoPtr->delta;
1181 TRACE("delta %d\n", delta);
1183 infoPtr->delta = delta;
1184 return prev;
1188 static inline LRESULT
1189 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1191 int day;
1193 /* convert from SYSTEMTIME to locale format */
1194 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1196 return MAKELONG(day, infoPtr->firstDaySet);
1200 /* Sets the first day of the week that will appear in the control
1203 * PARAMETERS:
1204 * [I] infoPtr : valid pointer to control data
1205 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1208 * RETURN VALUE:
1209 * Low word contains previous first day,
1210 * high word indicates was first day forced with this message before or is
1211 * locale difined (TRUE - was forced, FALSE - wasn't).
1213 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1214 * FIXME: we need more error checking here
1216 static LRESULT
1217 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1219 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1220 int new_day;
1222 TRACE("%d\n", day);
1224 if(day == -1)
1226 WCHAR buf[80];
1228 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1229 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1231 new_day = atoiW(buf);
1233 infoPtr->firstDaySet = FALSE;
1235 else if(day >= 7)
1237 new_day = 6; /* max first day allowed */
1238 infoPtr->firstDaySet = TRUE;
1240 else
1242 /* Native behaviour for that case is broken: invalid date number >31
1243 got displayed at (0,0) position, current month starts always from
1244 (1,0) position. Should be implemented here as well only if there's
1245 nothing else to do. */
1246 if (day < -1)
1247 FIXME("No bug compatibility for day=%d\n", day);
1249 new_day = day;
1250 infoPtr->firstDaySet = TRUE;
1253 /* convert from locale to SYSTEMTIME format */
1254 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1256 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1258 return prev;
1262 static LRESULT
1263 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
1265 TRACE("flag=%d, st=%p\n", flag, st);
1267 if(st)
1269 switch (flag) {
1270 case GMR_VISIBLE:
1272 st[0] = infoPtr->calendars[0].month;
1273 st[1] = infoPtr->calendars[infoPtr->cal_num-1].month;
1275 if (st[0].wMonth == min_allowed_date.wMonth &&
1276 st[0].wYear == min_allowed_date.wYear)
1278 st[0].wDay = min_allowed_date.wDay;
1280 else
1281 st[0].wDay = 1;
1282 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
1284 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
1285 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
1287 return infoPtr->cal_num;
1289 case GMR_DAYSTATE:
1291 /*FIXME: currently multicalendar feature isn't implemented,
1292 min date from previous month and max date from next one returned */
1293 MONTHCAL_GetMinDate(infoPtr, &st[0]);
1294 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
1295 break;
1297 default:
1298 WARN("Unknown flag value, got %d\n", flag);
1302 return infoPtr->monthRange;
1306 static LRESULT
1307 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1309 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1313 static LRESULT
1314 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1316 FILETIME ft_min, ft_max;
1318 TRACE("%x %p\n", limits, range);
1320 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1321 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1322 return FALSE;
1324 if (limits & GDTR_MIN)
1326 if (!MONTHCAL_ValidateTime(&range[0]))
1327 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1329 infoPtr->minDate = range[0];
1330 infoPtr->rangeValid |= GDTR_MIN;
1332 if (limits & GDTR_MAX)
1334 if (!MONTHCAL_ValidateTime(&range[1]))
1335 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1337 infoPtr->maxDate = range[1];
1338 infoPtr->rangeValid |= GDTR_MAX;
1341 /* Only one limit set - we are done */
1342 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1343 return TRUE;
1345 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1346 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1348 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1350 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1352 /* Native swaps limits only when both limits are being set. */
1353 SYSTEMTIME st_tmp = infoPtr->minDate;
1354 infoPtr->minDate = infoPtr->maxDate;
1355 infoPtr->maxDate = st_tmp;
1357 else
1359 /* reset the other limit */
1360 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1361 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1362 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1366 return TRUE;
1370 static LRESULT
1371 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1373 TRACE("%p\n", range);
1375 if(!range) return FALSE;
1377 range[1] = infoPtr->maxDate;
1378 range[0] = infoPtr->minDate;
1380 return infoPtr->rangeValid;
1384 static LRESULT
1385 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1387 int i;
1389 TRACE("%d %p\n", months, states);
1390 if(months != infoPtr->monthRange) return 0;
1392 for(i = 0; i < months; i++)
1393 infoPtr->monthdayState[i] = states[i];
1395 return 1;
1398 static LRESULT
1399 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1401 TRACE("%p\n", curSel);
1402 if(!curSel) return FALSE;
1403 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1405 *curSel = infoPtr->curSel;
1406 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1407 return TRUE;
1410 static LRESULT
1411 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1413 SYSTEMTIME prev = infoPtr->curSel;
1415 TRACE("%p\n", curSel);
1416 if(!curSel) return FALSE;
1417 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1419 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1420 /* exit earlier if selection equals current */
1421 if (MONTHCAL_IsDateEqual(&infoPtr->curSel, curSel)) return TRUE;
1423 if(!MONTHCAL_IsDateInValidRange(infoPtr, curSel, FALSE)) return FALSE;
1425 infoPtr->minSel = *curSel;
1426 infoPtr->maxSel = *curSel;
1428 /* if selection is still in current month, reduce rectangle */
1429 prev.wDay = curSel->wDay;
1430 if (MONTHCAL_IsDateEqual(&prev, curSel))
1432 RECT r_prev, r_new;
1434 /* note that infoPtr->curSel isn't updated yet */
1435 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->curSel, &r_prev);
1436 MONTHCAL_CalcPosFromDay(infoPtr, curSel, &r_new);
1438 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1439 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1441 else
1442 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1444 infoPtr->curSel = *curSel;
1445 infoPtr->calendars[0].month = *curSel;
1447 return TRUE;
1451 static LRESULT
1452 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1454 return infoPtr->maxSelCount;
1458 static LRESULT
1459 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1461 TRACE("%d\n", max);
1463 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1464 if(max <= 0) return FALSE;
1466 infoPtr->maxSelCount = max;
1468 return TRUE;
1472 static LRESULT
1473 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1475 TRACE("%p\n", range);
1477 if(!range) return FALSE;
1479 if(infoPtr->dwStyle & MCS_MULTISELECT)
1481 range[1] = infoPtr->maxSel;
1482 range[0] = infoPtr->minSel;
1483 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1484 return TRUE;
1487 return FALSE;
1491 static LRESULT
1492 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1494 TRACE("%p\n", range);
1496 if(!range) return FALSE;
1498 if(infoPtr->dwStyle & MCS_MULTISELECT)
1500 SYSTEMTIME old_range[2];
1502 /* adjust timestamps */
1503 if(!MONTHCAL_ValidateTime(&range[0]))
1504 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1505 if(!MONTHCAL_ValidateTime(&range[1]))
1506 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1508 /* maximum range exceeded */
1509 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1511 old_range[0] = infoPtr->minSel;
1512 old_range[1] = infoPtr->maxSel;
1514 /* swap if min > max */
1515 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1517 infoPtr->minSel = range[0];
1518 infoPtr->maxSel = range[1];
1520 else
1522 infoPtr->minSel = range[1];
1523 infoPtr->maxSel = range[0];
1525 infoPtr->curSel = infoPtr->minSel;
1526 infoPtr->calendars[0].month = infoPtr->minSel;
1528 /* update day of week */
1529 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1530 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1531 MONTHCAL_CalculateDayOfWeek(&infoPtr->curSel, TRUE);
1533 /* redraw if bounds changed */
1534 /* FIXME: no actual need to redraw everything */
1535 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1536 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1538 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1541 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1542 return TRUE;
1545 return FALSE;
1549 static LRESULT
1550 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1552 TRACE("%p\n", today);
1554 if(!today) return FALSE;
1555 *today = infoPtr->todaysDate;
1556 return TRUE;
1559 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1561 * RETURN VALUE
1563 * TRUE - today date changed
1564 * FALSE - today date isn't changed
1566 static BOOL
1567 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1569 RECT new_r, old_r;
1571 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1573 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->todaysDate, &old_r);
1574 MONTHCAL_CalcPosFromDay(infoPtr, today, &new_r);
1576 infoPtr->todaysDate = *today;
1578 /* only two days need redrawing */
1579 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1580 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1581 return TRUE;
1584 /* MCM_SETTODAT handler */
1585 static LRESULT
1586 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1588 TRACE("%p\n", today);
1590 if(!today) return FALSE;
1592 /* remember if date was set successfully */
1593 if(MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1595 return TRUE;
1598 /* returns calendar index containing specified point, or -1 if it's background */
1599 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1601 RECT r;
1602 INT i;
1604 for (i = 0; i < infoPtr->cal_num; i++)
1606 /* whole bounding rectangle allows some optimization to compute */
1607 r.left = infoPtr->calendars[i].title.left;
1608 r.top = infoPtr->calendars[i].title.top;
1609 r.bottom = infoPtr->calendars[i].days.bottom;
1610 r.right = infoPtr->calendars[i].days.right;
1612 if (PtInRect(&r, *pt)) return i;
1615 return -1;
1618 static LRESULT
1619 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1621 INT day, wday, wnum, calIdx;
1622 SYSTEMTIME ht_month;
1623 UINT x, y;
1625 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1627 x = lpht->pt.x;
1628 y = lpht->pt.y;
1630 memset(&lpht->st, 0, sizeof(lpht->st));
1632 /* Comment in for debugging...
1633 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,
1634 infoPtr->wdays.left, infoPtr->wdays.right,
1635 infoPtr->wdays.top, infoPtr->wdays.bottom,
1636 infoPtr->days.left, infoPtr->days.right,
1637 infoPtr->days.top, infoPtr->days.bottom,
1638 infoPtr->todayrect.left, infoPtr->todayrect.right,
1639 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1640 infoPtr->weeknums.left, infoPtr->weeknums.right,
1641 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1644 /* guess in what calendar we are */
1645 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1646 if (calIdx == -1)
1648 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1649 lpht->uHit = MCHT_TODAYLINK;
1650 else
1651 /* outside of calendar area? What's left must be background :-) */
1652 lpht->uHit = MCHT_CALENDARBK;
1654 return lpht->uHit;
1657 ht_month = infoPtr->calendars[calIdx].month;
1659 /* are we in the header? */
1660 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1661 /* FIXME: buttons hittesting could be optimized cause maximum
1662 two calendars have buttons */
1663 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1665 lpht->uHit = MCHT_TITLEBTNPREV;
1667 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1669 lpht->uHit = MCHT_TITLEBTNNEXT;
1671 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1673 lpht->uHit = MCHT_TITLEMONTH;
1675 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1677 lpht->uHit = MCHT_TITLEYEAR;
1679 else
1680 lpht->uHit = MCHT_TITLE;
1682 return lpht->uHit;
1685 /* days area (including week days and week numbers */
1686 day = MONTHCAL_CalcDayFromPos(infoPtr, x, y, &wday, &wnum);
1687 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1689 lpht->uHit = MCHT_CALENDARDAY;
1690 lpht->st.wYear = ht_month.wYear;
1691 lpht->st.wMonth = (day < 1) ? ht_month.wMonth -1 : ht_month.wMonth;
1692 lpht->st.wDay = (day < 1) ?
1693 MONTHCAL_MonthLength(ht_month.wMonth-1, ht_month.wYear) - day : day;
1695 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1697 lpht->uHit = MCHT_CALENDARWEEKNUM;
1698 lpht->st.wYear = ht_month.wYear;
1700 if (day < 1) {
1701 lpht->st.wMonth = ht_month.wMonth - 1;
1703 else if (day > MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear)) {
1704 lpht->st.wMonth = ht_month.wMonth + 1;
1706 else
1707 lpht->st.wMonth = ht_month.wMonth;
1709 if (day < 1) {
1710 lpht->st.wDay = MONTHCAL_MonthLength(ht_month.wMonth-1, ht_month.wYear) - day;
1712 else if (day > MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear)) {
1713 lpht->st.wDay = day - MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear);
1715 else
1716 lpht->st.wDay = day;
1718 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1720 lpht->st.wYear = ht_month.wYear;
1721 lpht->st.wMonth = ht_month.wMonth;
1722 if (day < 1)
1724 lpht->uHit = MCHT_CALENDARDATEPREV;
1725 MONTHCAL_GetPrevMonth(&lpht->st);
1726 lpht->st.wDay = MONTHCAL_MonthLength(lpht->st.wMonth, lpht->st.wYear) + day;
1728 else if (day > MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear))
1730 lpht->uHit = MCHT_CALENDARDATENEXT;
1731 MONTHCAL_GetNextMonth(&lpht->st);
1732 lpht->st.wDay = day - MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear);
1734 else {
1735 lpht->uHit = MCHT_CALENDARDATE;
1736 lpht->st.wDay = day;
1739 /* always update day of week */
1740 MONTHCAL_CalculateDayOfWeek(&lpht->st, TRUE);
1743 return lpht->uHit;
1746 /* MCN_GETDAYSTATE notification helper */
1747 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1749 if(infoPtr->dwStyle & MCS_DAYSTATE) {
1750 NMDAYSTATE nmds;
1751 INT i;
1753 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1754 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1755 nmds.nmhdr.code = MCN_GETDAYSTATE;
1756 nmds.cDayState = infoPtr->monthRange;
1757 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1759 nmds.stStart = infoPtr->todaysDate;
1760 nmds.stStart.wYear = infoPtr->curSel.wYear;
1761 nmds.stStart.wMonth = infoPtr->curSel.wMonth;
1762 nmds.stStart.wDay = 1;
1764 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1765 for(i = 0; i < infoPtr->monthRange; i++)
1766 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1768 Free(nmds.prgDayState);
1772 static void MONTHCAL_GoToPrevNextMonth(MONTHCAL_INFO *infoPtr, BOOL prev)
1774 SYSTEMTIME st = infoPtr->curSel;
1776 TRACE("%s\n", prev ? "prev" : "next");
1778 if(prev) MONTHCAL_GetPrevMonth(&st); else MONTHCAL_GetNextMonth(&st);
1780 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
1782 if(infoPtr->dwStyle & MCS_MULTISELECT)
1784 SYSTEMTIME range[2];
1786 range[0] = infoPtr->minSel;
1787 range[1] = infoPtr->maxSel;
1789 if(prev)
1791 MONTHCAL_GetPrevMonth(&range[0]);
1792 MONTHCAL_GetPrevMonth(&range[1]);
1794 else
1796 MONTHCAL_GetNextMonth(&range[0]);
1797 MONTHCAL_GetNextMonth(&range[1]);
1800 MONTHCAL_SetSelRange(infoPtr, range);
1802 else
1803 MONTHCAL_SetCurSel(infoPtr, &st);
1805 MONTHCAL_NotifyDayState(infoPtr);
1807 MONTHCAL_NotifySelectionChange(infoPtr);
1810 static LRESULT
1811 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1813 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1814 HMENU hMenu;
1815 POINT menupoint;
1816 WCHAR buf[32];
1818 hMenu = CreatePopupMenu();
1819 if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1821 WARN("Can't load resource\n");
1822 strcpyW(buf, todayW);
1824 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1825 menupoint.x = (short)LOWORD(lParam);
1826 menupoint.y = (short)HIWORD(lParam);
1827 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1828 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
1829 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1831 infoPtr->curSel = infoPtr->todaysDate;
1832 infoPtr->calendars[0].month = infoPtr->todaysDate;
1833 infoPtr->minSel = infoPtr->todaysDate;
1834 infoPtr->maxSel = infoPtr->todaysDate;
1835 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1838 return 0;
1841 /***
1842 * DESCRIPTION:
1843 * Subclassed edit control windproc function
1845 * PARAMETER(S):
1846 * [I] hwnd : the edit window handle
1847 * [I] uMsg : the message that is to be processed
1848 * [I] wParam : first message parameter
1849 * [I] lParam : second message parameter
1852 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1854 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
1856 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
1857 hwnd, uMsg, wParam, lParam);
1859 switch (uMsg)
1861 case WM_GETDLGCODE:
1862 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
1864 case WM_DESTROY:
1866 WNDPROC editProc = infoPtr->EditWndProc;
1867 infoPtr->EditWndProc = NULL;
1868 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
1869 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
1872 case WM_KILLFOCUS:
1873 break;
1875 case WM_KEYDOWN:
1876 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
1877 break;
1879 default:
1880 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
1883 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
1884 SendMessageW(hwnd, WM_CLOSE, 0, 0);
1885 return 0;
1888 /* creates updown control and edit box */
1889 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr)
1891 infoPtr->hWndYearEdit =
1892 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
1893 infoPtr->calendars[0].titleyear.left + 3, infoPtr->titlebtnnext.top,
1894 infoPtr->calendars[0].titleyear.right - infoPtr->calendars[0].titleyear.left + 4,
1895 infoPtr->textHeight, infoPtr->hwndSelf,
1896 NULL, NULL, NULL);
1898 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
1900 infoPtr->hWndYearUpDown =
1901 CreateWindowExW(0, UPDOWN_CLASSW, 0,
1902 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
1903 infoPtr->calendars[0].titleyear.right + 7, infoPtr->titlebtnnext.top,
1904 18, infoPtr->textHeight, infoPtr->hwndSelf,
1905 NULL, NULL, NULL);
1907 /* attach edit box */
1908 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
1909 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
1910 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
1911 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->curSel.wYear);
1913 /* subclass edit box */
1914 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
1915 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
1917 SetFocus(infoPtr->hWndYearEdit);
1920 static LRESULT
1921 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1923 MCHITTESTINFO ht;
1924 DWORD hit;
1926 /* Actually we don't need input focus for calendar, this is used to kill
1927 year updown and its buddy edit box */
1928 if (IsWindow(infoPtr->hWndYearUpDown))
1930 SetFocus(infoPtr->hwndSelf);
1931 return 0;
1934 SetCapture(infoPtr->hwndSelf);
1936 ht.cbSize = sizeof(MCHITTESTINFO);
1937 ht.pt.x = (short)LOWORD(lParam);
1938 ht.pt.y = (short)HIWORD(lParam);
1940 hit = MONTHCAL_HitTest(infoPtr, &ht);
1942 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
1944 switch(hit)
1946 case MCHT_TITLEBTNNEXT:
1947 MONTHCAL_GoToPrevNextMonth(infoPtr, FALSE);
1948 infoPtr->status = MC_NEXTPRESSED;
1949 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1950 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1951 return 0;
1953 case MCHT_TITLEBTNPREV:
1954 MONTHCAL_GoToPrevNextMonth(infoPtr, TRUE);
1955 infoPtr->status = MC_PREVPRESSED;
1956 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1957 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1958 return 0;
1960 case MCHT_TITLEMONTH:
1962 HMENU hMenu = CreatePopupMenu();
1963 WCHAR buf[32];
1964 POINT menupoint;
1965 INT i;
1967 for (i = 0; i < 12; i++)
1969 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
1970 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
1972 menupoint.x = ht.pt.x;
1973 menupoint.y = ht.pt.y;
1974 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1975 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
1976 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
1978 if ((i > 0) && (i < 13) && infoPtr->curSel.wMonth != i)
1980 infoPtr->curSel.wMonth = i;
1981 MONTHCAL_IsDateInValidRange(infoPtr, &infoPtr->curSel, TRUE);
1982 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1984 return 0;
1986 case MCHT_TITLEYEAR:
1988 MONTHCAL_EditYear(infoPtr);
1989 return 0;
1991 case MCHT_TODAYLINK:
1993 infoPtr->curSel = infoPtr->todaysDate;
1994 infoPtr->calendars[0].month = infoPtr->todaysDate;
1995 infoPtr->minSel = infoPtr->todaysDate;
1996 infoPtr->maxSel = infoPtr->todaysDate;
1997 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1999 MONTHCAL_NotifySelectionChange(infoPtr);
2000 MONTHCAL_NotifySelect(infoPtr);
2001 return 0;
2003 case MCHT_CALENDARDATENEXT:
2004 case MCHT_CALENDARDATEPREV:
2005 case MCHT_CALENDARDATE:
2007 SYSTEMTIME st[2];
2009 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2011 st[0] = st[1] = ht.st;
2012 /* clear selection range */
2013 MONTHCAL_SetSelRange(infoPtr, st);
2015 infoPtr->status = MC_SEL_LBUTDOWN;
2016 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
2017 return 0;
2021 return 1;
2025 static LRESULT
2026 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2028 NMHDR nmhdr;
2029 MCHITTESTINFO ht;
2030 DWORD hit;
2032 TRACE("\n");
2034 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2035 RECT *r;
2037 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2038 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2039 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2041 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2044 ReleaseCapture();
2046 /* always send NM_RELEASEDCAPTURE notification */
2047 nmhdr.hwndFrom = infoPtr->hwndSelf;
2048 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2049 nmhdr.code = NM_RELEASEDCAPTURE;
2050 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2052 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2054 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2056 ht.cbSize = sizeof(MCHITTESTINFO);
2057 ht.pt.x = (short)LOWORD(lParam);
2058 ht.pt.y = (short)HIWORD(lParam);
2059 hit = MONTHCAL_HitTest(infoPtr, &ht);
2061 infoPtr->status = MC_SEL_LBUTUP;
2062 MONTHCAL_SetDayFocus(infoPtr, NULL);
2064 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2066 SYSTEMTIME sel = infoPtr->curSel;
2068 /* will be invalidated here */
2069 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2071 /* send MCN_SELCHANGE only if new date selected */
2072 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2073 MONTHCAL_NotifySelectionChange(infoPtr);
2075 MONTHCAL_NotifySelect(infoPtr);
2078 return 0;
2082 static LRESULT
2083 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2085 TRACE("%ld\n", id);
2087 switch(id) {
2088 case MC_PREVNEXTMONTHTIMER:
2089 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToPrevNextMonth(infoPtr, FALSE);
2090 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToPrevNextMonth(infoPtr, TRUE);
2091 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2092 break;
2093 case MC_TODAYUPDATETIMER:
2095 SYSTEMTIME st;
2097 if(infoPtr->todaySet) return 0;
2099 GetLocalTime(&st);
2100 MONTHCAL_UpdateToday(infoPtr, &st);
2102 /* notification sent anyway */
2103 MONTHCAL_NotifySelectionChange(infoPtr);
2105 return 0;
2107 default:
2108 ERR("got unknown timer %ld\n", id);
2109 break;
2112 return 0;
2116 static LRESULT
2117 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2119 MCHITTESTINFO ht;
2120 SYSTEMTIME st_ht;
2121 INT hit;
2122 RECT r;
2124 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2126 ht.cbSize = sizeof(MCHITTESTINFO);
2127 ht.pt.x = (short)LOWORD(lParam);
2128 ht.pt.y = (short)HIWORD(lParam);
2130 hit = MONTHCAL_HitTest(infoPtr, &ht);
2132 /* not on the calendar date numbers? bail out */
2133 TRACE("hit:%x\n",hit);
2134 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2136 MONTHCAL_SetDayFocus(infoPtr, NULL);
2137 return 0;
2140 st_ht = ht.st;
2142 /* if pointer is over focused day still there's nothing to do */
2143 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2145 MONTHCAL_CalcPosFromDay(infoPtr, &ht.st, &r);
2147 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2148 SYSTEMTIME st[2];
2150 MONTHCAL_GetSelRange(infoPtr, st);
2152 /* If we're still at the first selected date and range is empty, return.
2153 If range isn't empty we should change range to a single firstSel */
2154 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2155 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2157 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2159 st[0] = infoPtr->firstSel;
2160 /* we should overwrite timestamp here */
2161 MONTHCAL_CopyDate(&st_ht, &st[1]);
2163 /* bounds will be swapped here if needed */
2164 MONTHCAL_SetSelRange(infoPtr, st);
2166 return 0;
2169 done:
2171 /* FIXME: this should specify a rectangle containing only the days that changed
2172 using InvalidateRect */
2173 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2175 return 0;
2179 static LRESULT
2180 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2182 HDC hdc;
2183 PAINTSTRUCT ps;
2185 if (hdc_paint)
2187 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2188 hdc = hdc_paint;
2190 else
2191 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2193 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2194 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2195 return 0;
2198 static LRESULT
2199 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2201 HBRUSH hbr;
2202 RECT rc;
2204 if (!GetClipBox(hdc, &rc)) return FALSE;
2206 /* fill background */
2207 hbr = CreateSolidBrush (infoPtr->bk);
2208 FillRect(hdc, &rc, hbr);
2209 DeleteObject(hbr);
2211 return TRUE;
2214 static LRESULT
2215 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2217 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2219 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2220 return 0;
2222 if (options & PRF_ERASEBKGND)
2223 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2225 if (options & PRF_CLIENT)
2226 MONTHCAL_Paint(infoPtr, hdc);
2228 return 0;
2231 static LRESULT
2232 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2234 TRACE("\n");
2236 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2238 return 0;
2241 /* sets the size information */
2242 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2244 static const WCHAR O0W[] = { '0','0',0 };
2245 HDC hdc = GetDC(infoPtr->hwndSelf);
2246 RECT *title=&infoPtr->calendars[0].title;
2247 RECT *prev=&infoPtr->titlebtnprev;
2248 RECT *next=&infoPtr->titlebtnnext;
2249 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2250 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2251 RECT *wdays=&infoPtr->calendars[0].wdays;
2252 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2253 RECT *days=&infoPtr->calendars[0].days;
2254 RECT *todayrect=&infoPtr->todayrect;
2255 SIZE size, sz;
2256 TEXTMETRICW tm;
2257 HFONT currentFont;
2258 INT xdiv, dx, dy, i;
2259 RECT rcClient;
2260 WCHAR buff[80];
2262 GetClientRect(infoPtr->hwndSelf, &rcClient);
2264 currentFont = SelectObject(hdc, infoPtr->hFont);
2266 /* get the height and width of each day's text */
2267 GetTextMetricsW(hdc, &tm);
2268 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2270 /* find largest abbreviated day name for current locale */
2271 size.cx = sz.cx = 0;
2272 for (i = 0; i < 7; i++)
2274 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + i,
2275 buff, countof(buff)))
2277 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2278 if (sz.cx > size.cx) size.cx = sz.cx;
2280 else /* locale independent fallback on failure */
2282 static const WCHAR SunW[] = { 'S','u','n',0 };
2284 GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2285 break;
2289 infoPtr->textWidth = size.cx + 2;
2291 /* recalculate the height and width increments and offsets */
2292 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2294 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2296 infoPtr->width_increment = size.cx * 2 + 4;
2297 infoPtr->height_increment = infoPtr->textHeight;
2299 /* calculate title area */
2300 title->top = 0;
2301 title->bottom = 3 * infoPtr->height_increment / 2;
2302 title->left = 0;
2303 title->right = infoPtr->width_increment * xdiv;
2305 /* set the dimensions of the next and previous buttons and center */
2306 /* the month text vertically */
2307 prev->top = next->top = title->top + 4;
2308 prev->bottom = next->bottom = title->bottom - 4;
2309 prev->left = title->left + 4;
2310 prev->right = prev->left + (title->bottom - title->top);
2311 next->right = title->right - 4;
2312 next->left = next->right - (title->bottom - title->top);
2314 /* titlemonth->left and right change based upon the current month */
2315 /* and are recalculated in refresh as the current month may change */
2316 /* without the control being resized */
2317 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2318 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2320 /* setup the dimensions of the rectangle we draw the names of the */
2321 /* days of the week in */
2322 weeknumrect->left = 0;
2324 if(infoPtr->dwStyle & MCS_WEEKNUMBERS)
2325 weeknumrect->right = prev->right;
2326 else
2327 weeknumrect->right = weeknumrect->left;
2329 wdays->left = days->left = weeknumrect->right;
2330 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2331 wdays->top = title->bottom;
2332 wdays->bottom = wdays->top + infoPtr->height_increment;
2334 days->top = weeknumrect->top = wdays->bottom;
2335 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2337 todayrect->left = 0;
2338 todayrect->right = title->right;
2339 todayrect->top = days->bottom;
2340 todayrect->bottom = days->bottom + infoPtr->height_increment;
2342 /* offset all rectangles to center in client area */
2343 dx = (rcClient.right - title->right) / 2;
2344 dy = (rcClient.bottom - todayrect->bottom) / 2;
2346 /* if calendar doesn't fit client area show it at left/top bounds */
2347 if (title->left + dx < 0) dx = 0;
2348 if (title->top + dy < 0) dy = 0;
2350 if (dx != 0 || dy != 0)
2352 OffsetRect(title, dx, dy);
2353 OffsetRect(prev, dx, dy);
2354 OffsetRect(next, dx, dy);
2355 OffsetRect(titlemonth, dx, dy);
2356 OffsetRect(titleyear, dx, dy);
2357 OffsetRect(wdays, dx, dy);
2358 OffsetRect(weeknumrect, dx, dy);
2359 OffsetRect(days, dx, dy);
2360 OffsetRect(todayrect, dx, dy);
2363 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2364 infoPtr->width_increment,infoPtr->height_increment,
2365 wine_dbgstr_rect(&rcClient),
2366 wine_dbgstr_rect(title),
2367 wine_dbgstr_rect(wdays),
2368 wine_dbgstr_rect(days),
2369 wine_dbgstr_rect(todayrect));
2371 /* restore the originally selected font */
2372 SelectObject(hdc, currentFont);
2374 ReleaseDC(infoPtr->hwndSelf, hdc);
2377 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2379 TRACE("(width=%d, height=%d)\n", Width, Height);
2381 MONTHCAL_UpdateSize(infoPtr);
2383 /* invalidate client area and erase background */
2384 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2386 return 0;
2389 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2391 return (LRESULT)infoPtr->hFont;
2394 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2396 HFONT hOldFont;
2397 LOGFONTW lf;
2399 if (!hFont) return 0;
2401 hOldFont = infoPtr->hFont;
2402 infoPtr->hFont = hFont;
2404 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2405 lf.lfWeight = FW_BOLD;
2406 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2408 MONTHCAL_UpdateSize(infoPtr);
2410 if (redraw)
2411 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2413 return (LRESULT)hOldFont;
2416 /* update theme after a WM_THEMECHANGED message */
2417 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2419 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2420 CloseThemeData (theme);
2421 OpenThemeData (infoPtr->hwndSelf, themeClass);
2422 return 0;
2425 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2426 const STYLESTRUCT *lpss)
2428 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2429 wStyleType, lpss->styleOld, lpss->styleNew);
2431 if (wStyleType != GWL_STYLE) return 0;
2433 infoPtr->dwStyle = lpss->styleNew;
2435 /* make room for week numbers */
2436 if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2437 MONTHCAL_UpdateSize(infoPtr);
2439 return 0;
2442 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2443 STYLESTRUCT *lpss)
2445 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2446 wStyleType, lpss->styleOld, lpss->styleNew);
2448 /* block MCS_MULTISELECT change */
2449 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2451 if (lpss->styleOld & MCS_MULTISELECT)
2452 lpss->styleNew |= MCS_MULTISELECT;
2453 else
2454 lpss->styleNew &= ~MCS_MULTISELECT;
2457 return 0;
2460 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2461 static LRESULT
2462 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2464 MONTHCAL_INFO *infoPtr;
2466 /* allocate memory for info structure */
2467 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2468 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2470 if (infoPtr == NULL) {
2471 ERR("could not allocate info memory!\n");
2472 return 0;
2475 infoPtr->hwndSelf = hwnd;
2476 infoPtr->hwndNotify = lpcs->hwndParent;
2477 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2478 infoPtr->calendars = Alloc(sizeof(CALENDAR_INFO));
2479 if (!infoPtr->calendars) goto fail;
2481 infoPtr->cal_num = 1;
2483 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
2485 /* initialize info structure */
2486 /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
2488 GetLocalTime(&infoPtr->todaysDate);
2489 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2491 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2492 infoPtr->monthRange = 3;
2494 infoPtr->monthdayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
2495 if (!infoPtr->monthdayState) goto fail;
2497 infoPtr->titlebk = comctl32_color.clrActiveCaption;
2498 infoPtr->titletxt = comctl32_color.clrWindow;
2499 infoPtr->monthbk = comctl32_color.clrWindow;
2500 infoPtr->trailingtxt = comctl32_color.clrGrayText;
2501 infoPtr->bk = comctl32_color.clrWindow;
2502 infoPtr->txt = comctl32_color.clrWindowText;
2504 infoPtr->minSel = infoPtr->todaysDate;
2505 infoPtr->maxSel = infoPtr->todaysDate;
2506 infoPtr->curSel = infoPtr->todaysDate;
2507 infoPtr->calendars[0].month = infoPtr->todaysDate;
2508 infoPtr->isUnicode = TRUE;
2510 /* call MONTHCAL_UpdateSize to set all of the dimensions */
2511 /* of the control */
2512 MONTHCAL_UpdateSize(infoPtr);
2514 /* today auto update timer, to be freed only on control destruction */
2515 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2517 OpenThemeData (infoPtr->hwndSelf, themeClass);
2519 return 0;
2521 fail:
2522 Free(infoPtr->monthdayState);
2523 Free(infoPtr->calendars);
2524 Free(infoPtr);
2525 return 0;
2528 static LRESULT
2529 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2531 /* free month calendar info data */
2532 Free(infoPtr->monthdayState);
2533 Free(infoPtr->calendars);
2534 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2536 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2538 Free(infoPtr);
2539 return 0;
2543 * Handler for WM_NOTIFY messages
2545 static LRESULT
2546 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2548 /* notification from year edit updown */
2549 if (hdr->code == UDN_DELTAPOS)
2551 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2553 if (hdr->hwndFrom == infoPtr->hWndYearUpDown)
2555 /* year value limits are set up explicitly after updown creation */
2556 if ((nmud->iDelta + nmud->iPos) != infoPtr->curSel.wYear)
2558 SYSTEMTIME new_date = infoPtr->curSel;
2560 new_date.wYear = nmud->iDelta + nmud->iPos;
2561 MONTHCAL_SetCurSel(infoPtr, &new_date);
2565 return 0;
2568 static inline BOOL
2569 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2571 BOOL prev = infoPtr->isUnicode;
2572 infoPtr->isUnicode = isUnicode;
2573 return prev;
2576 static inline BOOL
2577 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2579 return infoPtr->isUnicode;
2582 static LRESULT WINAPI
2583 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2585 MONTHCAL_INFO *infoPtr;
2587 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2589 infoPtr = MONTHCAL_GetInfoPtr(hwnd);
2590 if (!infoPtr && (uMsg != WM_CREATE))
2591 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2592 switch(uMsg)
2594 case MCM_GETCURSEL:
2595 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2597 case MCM_SETCURSEL:
2598 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2600 case MCM_GETMAXSELCOUNT:
2601 return MONTHCAL_GetMaxSelCount(infoPtr);
2603 case MCM_SETMAXSELCOUNT:
2604 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2606 case MCM_GETSELRANGE:
2607 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2609 case MCM_SETSELRANGE:
2610 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2612 case MCM_GETMONTHRANGE:
2613 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2615 case MCM_SETDAYSTATE:
2616 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2618 case MCM_GETMINREQRECT:
2619 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2621 case MCM_GETCOLOR:
2622 return MONTHCAL_GetColor(infoPtr, wParam);
2624 case MCM_SETCOLOR:
2625 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2627 case MCM_GETTODAY:
2628 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2630 case MCM_SETTODAY:
2631 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2633 case MCM_HITTEST:
2634 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2636 case MCM_GETFIRSTDAYOFWEEK:
2637 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2639 case MCM_SETFIRSTDAYOFWEEK:
2640 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2642 case MCM_GETRANGE:
2643 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2645 case MCM_SETRANGE:
2646 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2648 case MCM_GETMONTHDELTA:
2649 return MONTHCAL_GetMonthDelta(infoPtr);
2651 case MCM_SETMONTHDELTA:
2652 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2654 case MCM_GETMAXTODAYWIDTH:
2655 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2657 case MCM_SETUNICODEFORMAT:
2658 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2660 case MCM_GETUNICODEFORMAT:
2661 return MONTHCAL_GetUnicodeFormat(infoPtr);
2663 case WM_GETDLGCODE:
2664 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2666 case WM_RBUTTONUP:
2667 return MONTHCAL_RButtonUp(infoPtr, lParam);
2669 case WM_LBUTTONDOWN:
2670 return MONTHCAL_LButtonDown(infoPtr, lParam);
2672 case WM_MOUSEMOVE:
2673 return MONTHCAL_MouseMove(infoPtr, lParam);
2675 case WM_LBUTTONUP:
2676 return MONTHCAL_LButtonUp(infoPtr, lParam);
2678 case WM_PAINT:
2679 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2681 case WM_PRINTCLIENT:
2682 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2684 case WM_ERASEBKGND:
2685 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2687 case WM_SETFOCUS:
2688 return MONTHCAL_SetFocus(infoPtr);
2690 case WM_SIZE:
2691 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2693 case WM_NOTIFY:
2694 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2696 case WM_CREATE:
2697 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2699 case WM_SETFONT:
2700 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2702 case WM_GETFONT:
2703 return MONTHCAL_GetFont(infoPtr);
2705 case WM_TIMER:
2706 return MONTHCAL_Timer(infoPtr, wParam);
2708 case WM_THEMECHANGED:
2709 return theme_changed (infoPtr);
2711 case WM_DESTROY:
2712 return MONTHCAL_Destroy(infoPtr);
2714 case WM_SYSCOLORCHANGE:
2715 COMCTL32_RefreshSysColors();
2716 return 0;
2718 case WM_STYLECHANGED:
2719 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2721 case WM_STYLECHANGING:
2722 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2724 default:
2725 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2726 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2727 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2732 void
2733 MONTHCAL_Register(void)
2735 WNDCLASSW wndClass;
2737 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2738 wndClass.style = CS_GLOBALCLASS;
2739 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2740 wndClass.cbClsExtra = 0;
2741 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2742 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2743 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2744 wndClass.lpszClassName = MONTHCAL_CLASSW;
2746 RegisterClassW(&wndClass);
2750 void
2751 MONTHCAL_Unregister(void)
2753 UnregisterClassW(MONTHCAL_CLASSW, NULL);