shdocvw: Update Italian translation.
[wine.git] / dlls / comctl32 / monthcal.c
blobc1c105caba2ca20d0bb1562a32d4a0f47dc74373
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; /* contains single selection when used without MCS_MULTISELECT */
126 SYSTEMTIME maxSel;
127 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
128 DWORD rangeValid;
129 SYSTEMTIME minDate;
130 SYSTEMTIME maxDate;
132 RECT titlebtnnext; /* the `next month' button in the header */
133 RECT titlebtnprev; /* the `prev month' button in the header */
134 RECT todayrect; /* `today: xx/xx/xx' text rect */
135 HWND hwndNotify; /* Window to receive the notifications */
136 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
137 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
138 WNDPROC EditWndProc; /* original Edit window procedure */
140 CALENDAR_INFO *calendars;
141 INT cal_num;
142 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
144 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
146 /* empty SYSTEMTIME const */
147 static const SYSTEMTIME st_null;
148 /* valid date limits */
149 static const SYSTEMTIME max_allowed_date = { .wYear = 9999, .wMonth = 12, .wDay = 31 };
150 static const SYSTEMTIME min_allowed_date = { .wYear = 1752, .wMonth = 9, .wDay = 14 };
152 /* Prev/Next buttons */
153 enum nav_direction
155 DIRECTION_BACKWARD,
156 DIRECTION_FORWARD
159 #define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0))
161 /* helper functions */
163 /* send a single MCN_SELCHANGE notification */
164 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
166 NMSELCHANGE nmsc;
168 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
169 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
170 nmsc.nmhdr.code = MCN_SELCHANGE;
171 nmsc.stSelStart = infoPtr->minSel;
172 nmsc.stSelEnd = infoPtr->maxSel;
173 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
176 /* send a single MCN_SELECT notification */
177 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
179 NMSELCHANGE nmsc;
181 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
182 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
183 nmsc.nmhdr.code = MCN_SELECT;
184 nmsc.stSelStart = infoPtr->minSel;
185 nmsc.stSelEnd = infoPtr->maxSel;
187 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
190 /* returns the number of days in any given month, checking for leap days */
191 /* january is 1, december is 12 */
192 int MONTHCAL_MonthLength(int month, int year)
194 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
195 /* Wrap around, this eases handling. Getting length only we shouldn't care
196 about year change here cause January and December have
197 the same day quantity */
198 if(month == 0)
199 month = 12;
200 else if(month == 13)
201 month = 1;
203 /* special case for calendar transition year */
204 if(month == min_allowed_date.wMonth && year == min_allowed_date.wYear) return 19;
206 /* if we have a leap year add 1 day to February */
207 /* a leap year is a year either divisible by 400 */
208 /* or divisible by 4 and not by 100 */
209 if(month == 2) { /* February */
210 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
211 (year%4 == 0)) ? 1 : 0);
213 else {
214 return mdays[month - 1];
218 /* compares timestamps using date part only */
219 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
221 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
222 (first->wDay == second->wDay);
225 /* make sure that date fields are valid */
226 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
228 if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
229 if(time->wDayOfWeek > 6) return FALSE;
230 if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear))
231 return FALSE;
233 return TRUE;
236 /* Copies timestamp part only.
238 * PARAMETERS
240 * [I] from : source date
241 * [O] to : dest date
243 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
245 to->wHour = from->wHour;
246 to->wMinute = from->wMinute;
247 to->wSecond = from->wSecond;
250 /* Copies date part only.
252 * PARAMETERS
254 * [I] from : source date
255 * [O] to : dest date
257 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
259 to->wYear = from->wYear;
260 to->wMonth = from->wMonth;
261 to->wDay = from->wDay;
262 to->wDayOfWeek = from->wDayOfWeek;
265 /* Compares two dates in SYSTEMTIME format
267 * PARAMETERS
269 * [I] first : pointer to valid first date data to compare
270 * [I] second : pointer to valid second date data to compare
272 * RETURN VALUE
274 * -1 : first < second
275 * 0 : first == second
276 * 1 : first > second
278 * Note that no date validation performed, alreadt validated values expected.
280 static LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
282 FILETIME ft_first, ft_second;
284 SystemTimeToFileTime(first, &ft_first);
285 SystemTimeToFileTime(second, &ft_second);
287 return CompareFileTime(&ft_first, &ft_second);
290 static LONG MONTHCAL_CompareMonths(const SYSTEMTIME *first, const SYSTEMTIME *second)
292 SYSTEMTIME st_first, st_second;
294 st_first = st_second = st_null;
295 MONTHCAL_CopyDate(first, &st_first);
296 MONTHCAL_CopyDate(second, &st_second);
297 st_first.wDay = st_second.wDay = 1;
299 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
302 static LONG MONTHCAL_CompareDate(const SYSTEMTIME *first, const SYSTEMTIME *second)
304 SYSTEMTIME st_first, st_second;
306 st_first = st_second = st_null;
307 MONTHCAL_CopyDate(first, &st_first);
308 MONTHCAL_CopyDate(second, &st_second);
310 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
313 /* Checks largest possible date range and configured one
315 * PARAMETERS
317 * [I] infoPtr : valid pointer to control data
318 * [I] date : pointer to valid date data to check
319 * [I] fix : make date fit valid range
321 * RETURN VALUE
323 * TRUE - date whithin largest and configured range
324 * FALSE - date is outside largest or configured range
326 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr,
327 SYSTEMTIME *date, BOOL fix)
329 const SYSTEMTIME *fix_st = NULL;
331 if(MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) {
332 fix_st = &max_allowed_date;
334 else if(MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1) {
335 fix_st = &min_allowed_date;
337 else if(infoPtr->rangeValid & GDTR_MAX) {
338 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
339 fix_st = &infoPtr->maxDate;
342 else if(infoPtr->rangeValid & GDTR_MIN) {
343 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
344 fix_st = &infoPtr->minDate;
348 if (fix && fix_st) {
349 date->wYear = fix_st->wYear;
350 date->wMonth = fix_st->wMonth;
353 return fix_st ? FALSE : TRUE;
356 /* Checks passed range width with configured maximum selection count
358 * PARAMETERS
360 * [I] infoPtr : valid pointer to control data
361 * [I] range0 : pointer to valid date data (requested bound)
362 * [I] range1 : pointer to valid date data (primary bound)
363 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
365 * Adjust value computed basing on primary bound and current maximum selection
366 * count. For simple range check (without adjusted value required) (range0, range1)
367 * relation means nothing.
369 * RETURN VALUE
371 * TRUE - range is shorter or equal to maximum
372 * FALSE - range is larger than maximum
374 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
375 const SYSTEMTIME *range0,
376 const SYSTEMTIME *range1,
377 SYSTEMTIME *adjust)
379 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
380 FILETIME ft_range0, ft_range1;
381 LONG cmp;
383 SystemTimeToFileTime(range0, &ft_range0);
384 SystemTimeToFileTime(range1, &ft_range1);
386 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
387 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
388 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
389 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
391 cmp = CompareFileTime(&ft_range0, &ft_range1);
393 if(cmp == 1)
394 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
395 else
396 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
398 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
400 if(adjust) {
401 if(cmp == 1)
402 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
403 else
404 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
406 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
407 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
408 FileTimeToSystemTime(&ft_range0, adjust);
411 return FALSE;
413 else return TRUE;
416 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
417 Milliseconds are intentionally not validated. */
418 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
420 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
421 return FALSE;
422 else
423 return TRUE;
426 /* Note:Depending on DST, this may be offset by a day.
427 Need to find out if we're on a DST place & adjust the clock accordingly.
428 Above function assumes we have a valid data.
429 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
430 0 = Sunday.
433 /* Returns the day in the week
435 * PARAMETERS
436 * [i] date : input date
437 * [I] inplace : set calculated value back to date structure
439 * RETURN VALUE
440 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
442 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
444 SYSTEMTIME st = st_null;
445 FILETIME ft;
447 MONTHCAL_CopyDate(date, &st);
449 SystemTimeToFileTime(&st, &ft);
450 FileTimeToSystemTime(&ft, &st);
452 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
454 return st.wDayOfWeek;
457 /* add/substract 'months' from date */
458 static inline void MONTHCAL_GetMonth(SYSTEMTIME *date, INT months)
460 INT length;
462 date->wMonth += months;
463 date->wYear += date->wMonth > 0 ? (date->wMonth - 1) / 12 : date->wMonth / 12 - 1;
464 date->wMonth = date->wMonth > 0 ? (date->wMonth - 1) % 12 + 1 : 12 + date->wMonth % 12;
465 /* fix moving from last day in a month */
466 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
467 if(date->wDay > length) date->wDay = length;
468 MONTHCAL_CalculateDayOfWeek(date, TRUE);
471 /* properly updates date to point on next month */
472 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
474 return MONTHCAL_GetMonth(date, 1);
477 /* properly updates date to point on prev month */
478 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
480 return MONTHCAL_GetMonth(date, -1);
483 /* Returns full date for a first currently visible day */
484 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
486 /* zero indexed calendar has the earliest date */
487 SYSTEMTIME st_first = infoPtr->calendars[0].month;
488 INT firstDay;
490 st_first.wDay = 1;
491 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
493 *date = infoPtr->calendars[0].month;
494 MONTHCAL_GetPrevMonth(date);
496 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
497 (infoPtr->firstDay - firstDay) % 7 + 1;
499 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
500 date->wDay -= 7;
502 /* fix day of week */
503 MONTHCAL_CalculateDayOfWeek(date, TRUE);
506 /* Returns full date for a last currently visible day */
507 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
509 /* the latest date is in latest calendar */
510 SYSTEMTIME st, lt_month = infoPtr->calendars[infoPtr->cal_num-1].month;
512 *date = lt_month;
513 MONTHCAL_GetNextMonth(date);
515 MONTHCAL_GetMinDate(infoPtr, &st);
516 /* Use month length to get max day. 42 means max day count in calendar area */
517 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
518 MONTHCAL_MonthLength(lt_month.wMonth, lt_month.wYear);
520 /* fix day of week */
521 MONTHCAL_CalculateDayOfWeek(date, TRUE);
524 /* From a given point, calculate the row (weekpos), column(daypos)
525 and day in the calendar. day== 0 mean the last day of tha last month
527 static int MONTHCAL_CalcDayFromPos(const MONTHCAL_INFO *infoPtr, int x, int y,
528 int *daypos, int *weekpos)
530 int retval, firstDay;
531 RECT rcClient;
532 SYSTEMTIME st = infoPtr->minSel;
534 GetClientRect(infoPtr->hwndSelf, &rcClient);
536 /* if the point is outside the x bounds of the window put
537 it at the boundary */
538 if (x > rcClient.right)
539 x = rcClient.right;
541 *daypos = (x - infoPtr->calendars[0].days.left ) / infoPtr->width_increment;
542 *weekpos = (y - infoPtr->calendars[0].days.top ) / infoPtr->height_increment;
544 st.wDay = 1;
545 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
546 retval = *daypos + (7 * *weekpos) - firstDay;
547 return retval;
550 /* Sets the RECT struct r to the rectangle around the date
552 * PARAMETERS
554 * [I] infoPtr : pointer to control data
555 * [I] date : date value
556 * [O] x : day column (zero based)
557 * [O] y : week column (zero based)
559 static void MONTHCAL_CalcDayXY(const MONTHCAL_INFO *infoPtr,
560 const SYSTEMTIME *date, int *x, int *y)
562 SYSTEMTIME st = infoPtr->minSel;
563 LONG cmp;
564 int first;
566 st.wDay = 1;
567 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
569 cmp = MONTHCAL_CompareMonths(date, &infoPtr->minSel);
571 /* previous month */
572 if(cmp == -1) {
573 *x = (first - MONTHCAL_MonthLength(date->wMonth, infoPtr->minSel.wYear) + date->wDay) % 7;
574 *y = 0;
575 return;
578 /* next month calculation is same as for current,
579 just add current month length */
580 if(cmp == 1) {
581 first += MONTHCAL_MonthLength(infoPtr->minSel.wMonth, infoPtr->minSel.wYear);
584 *x = (date->wDay + first) % 7;
585 *y = (date->wDay + first - *x) / 7;
589 /* x: column(day), y: row(week) */
590 static inline void MONTHCAL_CalcDayRect(const MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
592 r->left = infoPtr->calendars[0].days.left + x * infoPtr->width_increment;
593 r->right = r->left + infoPtr->width_increment;
594 r->top = infoPtr->calendars[0].days.top + y * infoPtr->height_increment;
595 r->bottom = r->top + infoPtr->textHeight;
599 /* Sets the RECT struct r to the rectangle around the date */
600 static inline void MONTHCAL_CalcPosFromDay(const MONTHCAL_INFO *infoPtr,
601 const SYSTEMTIME *date, RECT *r)
603 int x, y;
605 MONTHCAL_CalcDayXY(infoPtr, date, &x, &y);
606 MONTHCAL_CalcDayRect(infoPtr, r, x, y);
609 /* Focused day helper:
611 - set focused date to given value;
612 - reset to zero value if NULL passed;
613 - invalidate previous and new day rectangle only if needed.
615 Returns TRUE if focused day changed, FALSE otherwise.
617 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
619 RECT r;
621 if(st)
623 /* there's nothing to do if it's the same date,
624 mouse move within same date rectangle case */
625 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
627 /* invalidate old focused day */
628 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
629 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
631 infoPtr->focusedSel = *st;
634 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
636 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
637 infoPtr->focusedSel = st_null;
639 /* on set invalidates new day, on reset clears previous focused day */
640 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
642 return TRUE;
645 /* Draw today day mark rectangle
647 * [I] hdc : context to draw in
648 * [I] day : day to mark with rectangle
651 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
652 const SYSTEMTIME *date)
654 HPEN hRedPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
655 HPEN hOldPen2 = SelectObject(hdc, hRedPen);
656 HBRUSH hOldBrush;
657 RECT day_rect;
659 MONTHCAL_CalcPosFromDay(infoPtr, date, &day_rect);
661 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
662 Rectangle(hdc, day_rect.left, day_rect.top, day_rect.right, day_rect.bottom);
664 SelectObject(hdc, hOldBrush);
665 DeleteObject(hRedPen);
666 SelectObject(hdc, hOldPen2);
669 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
670 int bold, const PAINTSTRUCT *ps)
672 static const WCHAR fmtW[] = { '%','d',0 };
673 WCHAR buf[10];
674 RECT r, r_temp;
675 static BOOL bold_selected;
676 BOOL selected_day = FALSE;
677 HBRUSH hbr;
678 COLORREF oldCol = 0;
679 COLORREF oldBk = 0;
681 /* No need to check styles: when selection is not valid, it is set to zero.
682 * 1<day<31, so everything is OK.
685 MONTHCAL_CalcPosFromDay(infoPtr, st, &r);
686 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
688 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
689 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0)) {
691 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
692 TRACE("%s\n", wine_dbgstr_rect(&r));
693 oldCol = SetTextColor(hdc, infoPtr->monthbk);
694 oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
695 hbr = GetSysColorBrush(COLOR_HIGHLIGHT);
696 FillRect(hdc, &r, hbr);
698 selected_day = TRUE;
701 if(bold && !bold_selected) {
702 SelectObject(hdc, infoPtr->hBoldFont);
703 bold_selected = TRUE;
705 if(!bold && bold_selected) {
706 SelectObject(hdc, infoPtr->hFont);
707 bold_selected = FALSE;
710 SetBkMode(hdc,TRANSPARENT);
711 wsprintfW(buf, fmtW, st->wDay);
712 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
714 if(selected_day) {
715 SetTextColor(hdc, oldCol);
716 SetBkColor(hdc, oldBk);
721 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, BOOL btnNext)
723 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
724 RECT *r = btnNext ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
725 BOOL pressed = btnNext ? (infoPtr->status & MC_NEXTPRESSED) :
726 (infoPtr->status & MC_PREVPRESSED);
727 if (theme)
729 static const int states[] = {
730 /* Prev button */
731 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
732 /* Next button */
733 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
735 int stateNum = btnNext ? 3 : 0;
736 if (pressed)
737 stateNum += 1;
738 else
740 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
742 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
744 else
746 int style = btnNext ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
747 if (pressed)
748 style |= DFCS_PUSHED;
749 else
751 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
754 DrawFrameControl(hdc, r, DFC_SCROLL, style);
757 /* paint a title with buttons and month/year string */
758 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
760 static const WCHAR fmt_monthW[] = { '%','s',' ','%','l','d',0 };
761 RECT *title = &infoPtr->calendars[calIdx].title;
762 const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
763 WCHAR buf_month[80], buf_fmt[80];
764 HBRUSH hbr;
765 SIZE sz;
767 /* fill header box */
768 hbr = CreateSolidBrush(infoPtr->titlebk);
769 FillRect(hdc, title, hbr);
770 DeleteObject(hbr);
772 /* month/year string */
773 SetBkColor(hdc, infoPtr->titlebk);
774 SetTextColor(hdc, infoPtr->titletxt);
775 SelectObject(hdc, infoPtr->hBoldFont);
777 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1 + st->wMonth - 1,
778 buf_month, countof(buf_month));
780 wsprintfW(buf_fmt, fmt_monthW, buf_month, st->wYear);
781 DrawTextW(hdc, buf_fmt, strlenW(buf_fmt), title,
782 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
784 /* update title rectangles with current month - used while testing hits */
785 GetTextExtentPoint32W(hdc, buf_fmt, strlenW(buf_fmt), &sz);
786 infoPtr->calendars[calIdx].titlemonth.left = title->right / 2 + title->left / 2 - sz.cx / 2;
787 infoPtr->calendars[calIdx].titleyear.right = title->right / 2 + title->left / 2 + sz.cx / 2;
789 GetTextExtentPoint32W(hdc, buf_month, strlenW(buf_month), &sz);
790 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
791 infoPtr->calendars[calIdx].titleyear.left = infoPtr->calendars[calIdx].titlemonth.right;
794 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
796 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
797 static const WCHAR fmt_weekW[] = { '%','d',0 };
798 INT mindays, weeknum, weeknum1, startofprescal;
799 INT i, prev_month;
800 SYSTEMTIME st;
801 WCHAR buf[80];
802 HBRUSH hbr;
803 RECT r;
805 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
807 MONTHCAL_GetMinDate(infoPtr, &st);
808 startofprescal = st.wDay;
809 st = *date;
811 prev_month = date->wMonth - 1;
812 if(prev_month == 0) prev_month = 12;
815 Rules what week to call the first week of a new year:
816 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
817 The week containing Jan 1 is the first week of year
818 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
819 First week of year must contain 4 days of the new year
820 LOCALE_IFIRSTWEEKOFYEAR == 1 (what contries?)
821 The first week of the year must contain only days of the new year
823 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
824 weeknum = atoiW(buf);
825 switch (weeknum)
827 case 1: mindays = 6;
828 break;
829 case 2: mindays = 3;
830 break;
831 case 0: mindays = 0;
832 break;
833 default:
834 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
835 mindays = 0;
838 if (date->wMonth == 1)
840 /* calculate all those exceptions for january */
841 st.wDay = st.wMonth = 1;
842 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
843 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
844 weeknum = 1;
845 else
847 weeknum = 0;
848 for(i = 0; i < 11; i++)
849 weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
851 weeknum += startofprescal + 7;
852 weeknum /= 7;
853 st.wYear -= 1;
854 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
855 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
858 else
860 weeknum = 0;
861 for(i = 0; i < prev_month - 1; i++)
862 weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
864 weeknum += startofprescal + 7;
865 weeknum /= 7;
866 st.wDay = st.wMonth = 1;
867 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
868 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
871 r = infoPtr->calendars[calIdx].weeknums;
873 /* erase whole week numbers area */
874 hbr = CreateSolidBrush(infoPtr->monthbk);
875 FillRect(hdc, &r, hbr);
876 DeleteObject(hbr);
878 /* reduce rectangle to one week number */
879 r.bottom = r.top + infoPtr->height_increment;
881 for(i = 0; i < 6; i++) {
882 if((i == 0) && (weeknum > 50))
884 wsprintfW(buf, fmt_weekW, weeknum);
885 weeknum = 0;
887 else if((i == 5) && (weeknum > 47))
889 wsprintfW(buf, fmt_weekW, 1);
891 else
892 wsprintfW(buf, fmt_weekW, weeknum + i);
894 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
895 OffsetRect(&r, 0, infoPtr->height_increment);
898 /* line separator for week numbers column */
899 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
900 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
903 /* bottom today date */
904 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
906 if(!(infoPtr->dwStyle & MCS_NOTODAY)) {
907 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
908 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
909 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
910 RECT rtoday;
912 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
913 SYSTEMTIME fake_st;
915 MONTHCAL_GetMaxDate(infoPtr, &fake_st);
916 /* this is always safe cause next month will never fully fit calendar */
917 fake_st.wDay += 1;
918 MONTHCAL_CircleDay(infoPtr, hdc, &fake_st);
920 if (!LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, countof(buf_todayW)))
922 WARN("Can't load resource\n");
923 strcpyW(buf_todayW, todayW);
925 MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
926 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
927 buf_dateW, countof(buf_dateW));
928 SelectObject(hdc, infoPtr->hBoldFont);
930 wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
931 DrawTextW(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
932 DrawTextW(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
934 SelectObject(hdc, infoPtr->hFont);
938 /* today mark + focus */
939 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
941 if((infoPtr->minSel.wMonth == infoPtr->todaysDate.wMonth) &&
942 (infoPtr->minSel.wYear == infoPtr->todaysDate.wYear) &&
943 !(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
945 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
948 if(!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
950 RECT r;
951 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
952 DrawFocusRect(hdc, &r);
956 /* paint a calendar area */
957 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
959 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
960 INT prev_month, i, j, length;
961 RECT r, fill_bk_rect;
962 SYSTEMTIME st;
963 WCHAR buf[80];
964 HBRUSH hbr;
965 int mask;
967 /* fill whole days area - from week days area to today note rectangle */
968 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
969 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
970 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
972 hbr = CreateSolidBrush(infoPtr->monthbk);
973 FillRect(hdc, &fill_bk_rect, hbr);
974 DeleteObject(hbr);
976 /* draw line under day abbreviations */
977 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
978 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
979 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
980 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
982 prev_month = date->wMonth - 1;
983 if (prev_month == 0) prev_month = 12;
985 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
986 infoPtr->calendars[calIdx].weeknums.right;
988 /* 1. draw day abbreviations */
989 SelectObject(hdc, infoPtr->hFont);
990 SetBkColor(hdc, infoPtr->monthbk);
991 SetTextColor(hdc, infoPtr->trailingtxt);
992 /* rectangle to draw a single day abbreviation within */
993 r = infoPtr->calendars[calIdx].wdays;
994 r.right = r.left + infoPtr->width_increment;
996 i = infoPtr->firstDay;
997 for(j = 0; j < 7; j++) {
998 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
999 DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1000 OffsetRect(&r, infoPtr->width_increment, 0);
1003 /* 2. previous and next months */
1004 if (!(infoPtr->dwStyle & MCS_NOTRAILINGDATES) && (calIdx == 0 || calIdx == infoPtr->cal_num - 1))
1006 SYSTEMTIME st_max;
1008 SetTextColor(hdc, infoPtr->trailingtxt);
1010 /* draw prev month */
1011 if (calIdx == 0)
1013 MONTHCAL_GetMinDate(infoPtr, &st);
1014 mask = 1 << (st.wDay-1);
1015 length = MONTHCAL_MonthLength(prev_month, date->wYear);
1017 while(st.wDay <= length)
1019 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[0] & mask, ps);
1020 mask <<= 1;
1021 st.wDay++;
1025 /* draw next month */
1026 if (calIdx == infoPtr->cal_num - 1)
1028 st = *date;
1029 st.wDay = 1;
1030 MONTHCAL_GetNextMonth(&st);
1031 MONTHCAL_GetMaxDate(infoPtr, &st_max);
1032 mask = 1;
1034 while(st.wDay <= st_max.wDay)
1036 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[2] & mask, ps);
1037 mask <<= 1;
1038 st.wDay++;
1043 /* 3. current month */
1044 SetTextColor(hdc, infoPtr->txt);
1045 st = *date;
1046 st.wDay = 1;
1047 mask = 1;
1048 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1049 while(st.wDay <= length)
1051 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[1] & mask, ps);
1052 mask <<= 1;
1053 st.wDay++;
1057 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1059 COLORREF old_text_clr, old_bk_clr;
1060 HFONT old_font;
1061 INT i;
1063 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1064 old_bk_clr = GetBkColor(hdc);
1065 old_font = GetCurrentObject(hdc, OBJ_FONT);
1067 for (i = 0; i < infoPtr->cal_num; i++)
1069 RECT *title = &infoPtr->calendars[i].title;
1070 RECT r;
1072 /* draw title, redraw all its elements */
1073 if (IntersectRect(&r, &(ps->rcPaint), title))
1074 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1076 /* draw calendar area */
1077 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1078 if (IntersectRect(&r, &(ps->rcPaint), &r))
1079 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1081 /* week numbers */
1082 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1085 /* focus and today rectangle */
1086 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1088 /* today at the bottom left */
1089 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1091 /* navigation buttons */
1092 MONTHCAL_PaintButton(infoPtr, hdc, FALSE);
1093 MONTHCAL_PaintButton(infoPtr, hdc, TRUE);
1095 /* restore context */
1096 SetBkColor(hdc, old_bk_clr);
1097 SelectObject(hdc, old_font);
1098 SetTextColor(hdc, old_text_clr);
1101 static LRESULT
1102 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, LPRECT lpRect)
1104 TRACE("rect %p\n", lpRect);
1106 if(!lpRect) return FALSE;
1108 lpRect->left = infoPtr->calendars[0].title.left;
1109 lpRect->top = infoPtr->calendars[0].title.top;
1110 lpRect->right = infoPtr->calendars[0].title.right;
1111 lpRect->bottom = infoPtr->todayrect.bottom;
1113 AdjustWindowRect(lpRect, infoPtr->dwStyle, FALSE);
1115 /* minimal rectangle is zero based */
1116 OffsetRect(lpRect, -lpRect->left, -lpRect->top);
1118 TRACE("%s\n", wine_dbgstr_rect(lpRect));
1120 return TRUE;
1124 static LRESULT
1125 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, INT index)
1127 TRACE("\n");
1129 switch(index) {
1130 case MCSC_BACKGROUND:
1131 return infoPtr->bk;
1132 case MCSC_TEXT:
1133 return infoPtr->txt;
1134 case MCSC_TITLEBK:
1135 return infoPtr->titlebk;
1136 case MCSC_TITLETEXT:
1137 return infoPtr->titletxt;
1138 case MCSC_MONTHBK:
1139 return infoPtr->monthbk;
1140 case MCSC_TRAILINGTEXT:
1141 return infoPtr->trailingtxt;
1144 return -1;
1148 static LRESULT
1149 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, INT index, COLORREF color)
1151 COLORREF prev = -1;
1153 TRACE("%d: color %08x\n", index, color);
1155 switch(index) {
1156 case MCSC_BACKGROUND:
1157 prev = infoPtr->bk;
1158 infoPtr->bk = color;
1159 break;
1160 case MCSC_TEXT:
1161 prev = infoPtr->txt;
1162 infoPtr->txt = color;
1163 break;
1164 case MCSC_TITLEBK:
1165 prev = infoPtr->titlebk;
1166 infoPtr->titlebk = color;
1167 break;
1168 case MCSC_TITLETEXT:
1169 prev=infoPtr->titletxt;
1170 infoPtr->titletxt = color;
1171 break;
1172 case MCSC_MONTHBK:
1173 prev = infoPtr->monthbk;
1174 infoPtr->monthbk = color;
1175 break;
1176 case MCSC_TRAILINGTEXT:
1177 prev = infoPtr->trailingtxt;
1178 infoPtr->trailingtxt = color;
1179 break;
1182 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND ? TRUE : FALSE);
1183 return prev;
1187 static LRESULT
1188 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1190 TRACE("\n");
1192 if(infoPtr->delta)
1193 return infoPtr->delta;
1194 else
1195 return infoPtr->visible;
1199 static LRESULT
1200 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1202 INT prev = infoPtr->delta;
1204 TRACE("delta %d\n", delta);
1206 infoPtr->delta = delta;
1207 return prev;
1211 static inline LRESULT
1212 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1214 int day;
1216 /* convert from SYSTEMTIME to locale format */
1217 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1219 return MAKELONG(day, infoPtr->firstDaySet);
1223 /* Sets the first day of the week that will appear in the control
1226 * PARAMETERS:
1227 * [I] infoPtr : valid pointer to control data
1228 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1231 * RETURN VALUE:
1232 * Low word contains previous first day,
1233 * high word indicates was first day forced with this message before or is
1234 * locale difined (TRUE - was forced, FALSE - wasn't).
1236 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1237 * FIXME: we need more error checking here
1239 static LRESULT
1240 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1242 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1243 int new_day;
1245 TRACE("%d\n", day);
1247 if(day == -1)
1249 WCHAR buf[80];
1251 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1252 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1254 new_day = atoiW(buf);
1256 infoPtr->firstDaySet = FALSE;
1258 else if(day >= 7)
1260 new_day = 6; /* max first day allowed */
1261 infoPtr->firstDaySet = TRUE;
1263 else
1265 /* Native behaviour for that case is broken: invalid date number >31
1266 got displayed at (0,0) position, current month starts always from
1267 (1,0) position. Should be implemented here as well only if there's
1268 nothing else to do. */
1269 if (day < -1)
1270 FIXME("No bug compatibility for day=%d\n", day);
1272 new_day = day;
1273 infoPtr->firstDaySet = TRUE;
1276 /* convert from locale to SYSTEMTIME format */
1277 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1279 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1281 return prev;
1285 static LRESULT
1286 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
1288 TRACE("flag=%d, st=%p\n", flag, st);
1290 if(st)
1292 switch (flag) {
1293 case GMR_VISIBLE:
1295 st[0] = infoPtr->calendars[0].month;
1296 st[1] = infoPtr->calendars[infoPtr->cal_num-1].month;
1298 if (st[0].wMonth == min_allowed_date.wMonth &&
1299 st[0].wYear == min_allowed_date.wYear)
1301 st[0].wDay = min_allowed_date.wDay;
1303 else
1304 st[0].wDay = 1;
1305 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
1307 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
1308 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
1310 return infoPtr->cal_num;
1312 case GMR_DAYSTATE:
1314 /*FIXME: currently multicalendar feature isn't implemented,
1315 min date from previous month and max date from next one returned */
1316 MONTHCAL_GetMinDate(infoPtr, &st[0]);
1317 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
1318 break;
1320 default:
1321 WARN("Unknown flag value, got %d\n", flag);
1325 return infoPtr->monthRange;
1329 static LRESULT
1330 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1332 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1336 static LRESULT
1337 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1339 FILETIME ft_min, ft_max;
1341 TRACE("%x %p\n", limits, range);
1343 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1344 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1345 return FALSE;
1347 if (limits & GDTR_MIN)
1349 if (!MONTHCAL_ValidateTime(&range[0]))
1350 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1352 infoPtr->minDate = range[0];
1353 infoPtr->rangeValid |= GDTR_MIN;
1355 if (limits & GDTR_MAX)
1357 if (!MONTHCAL_ValidateTime(&range[1]))
1358 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1360 infoPtr->maxDate = range[1];
1361 infoPtr->rangeValid |= GDTR_MAX;
1364 /* Only one limit set - we are done */
1365 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1366 return TRUE;
1368 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1369 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1371 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1373 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1375 /* Native swaps limits only when both limits are being set. */
1376 SYSTEMTIME st_tmp = infoPtr->minDate;
1377 infoPtr->minDate = infoPtr->maxDate;
1378 infoPtr->maxDate = st_tmp;
1380 else
1382 /* reset the other limit */
1383 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1384 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1385 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1389 return TRUE;
1393 static LRESULT
1394 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1396 TRACE("%p\n", range);
1398 if(!range) return FALSE;
1400 range[1] = infoPtr->maxDate;
1401 range[0] = infoPtr->minDate;
1403 return infoPtr->rangeValid;
1407 static LRESULT
1408 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1410 TRACE("%p %d %p\n", infoPtr, months, states);
1411 if(months != infoPtr->monthRange) return 0;
1413 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1415 return 1;
1418 static LRESULT
1419 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1421 TRACE("%p\n", curSel);
1422 if(!curSel) return FALSE;
1423 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1425 *curSel = infoPtr->minSel;
1426 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1427 return TRUE;
1430 static LRESULT
1431 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1433 SYSTEMTIME prev = infoPtr->minSel;
1434 WORD day;
1436 TRACE("%p\n", curSel);
1437 if(!curSel) return FALSE;
1438 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1440 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1441 /* exit earlier if selection equals current */
1442 if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1444 if(!MONTHCAL_IsDateInValidRange(infoPtr, curSel, FALSE)) return FALSE;
1446 infoPtr->calendars[0].month = *curSel;
1447 infoPtr->minSel = *curSel;
1448 infoPtr->maxSel = *curSel;
1450 /* if selection is still in current month, reduce rectangle */
1451 day = prev.wDay;
1452 prev.wDay = curSel->wDay;
1453 if (MONTHCAL_IsDateEqual(&prev, curSel))
1455 RECT r_prev, r_new;
1457 prev.wDay = day;
1458 MONTHCAL_CalcPosFromDay(infoPtr, &prev, &r_prev);
1459 MONTHCAL_CalcPosFromDay(infoPtr, curSel, &r_new);
1461 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1462 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1464 else
1465 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1467 return TRUE;
1471 static LRESULT
1472 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1474 return infoPtr->maxSelCount;
1478 static LRESULT
1479 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1481 TRACE("%d\n", max);
1483 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1484 if(max <= 0) return FALSE;
1486 infoPtr->maxSelCount = max;
1488 return TRUE;
1492 static LRESULT
1493 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1495 TRACE("%p\n", range);
1497 if(!range) return FALSE;
1499 if(infoPtr->dwStyle & MCS_MULTISELECT)
1501 range[1] = infoPtr->maxSel;
1502 range[0] = infoPtr->minSel;
1503 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1504 return TRUE;
1507 return FALSE;
1511 static LRESULT
1512 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1514 TRACE("%p\n", range);
1516 if(!range) return FALSE;
1518 if(infoPtr->dwStyle & MCS_MULTISELECT)
1520 SYSTEMTIME old_range[2];
1522 /* adjust timestamps */
1523 if(!MONTHCAL_ValidateTime(&range[0]))
1524 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1525 if(!MONTHCAL_ValidateTime(&range[1]))
1526 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1528 /* maximum range exceeded */
1529 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1531 old_range[0] = infoPtr->minSel;
1532 old_range[1] = infoPtr->maxSel;
1534 /* swap if min > max */
1535 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1537 infoPtr->minSel = range[0];
1538 infoPtr->maxSel = range[1];
1540 else
1542 infoPtr->minSel = range[1];
1543 infoPtr->maxSel = range[0];
1545 infoPtr->calendars[0].month = infoPtr->minSel;
1547 /* update day of week */
1548 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1549 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1551 /* redraw if bounds changed */
1552 /* FIXME: no actual need to redraw everything */
1553 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1554 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1556 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1559 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1560 return TRUE;
1563 return FALSE;
1567 static LRESULT
1568 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1570 TRACE("%p\n", today);
1572 if(!today) return FALSE;
1573 *today = infoPtr->todaysDate;
1574 return TRUE;
1577 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1579 * RETURN VALUE
1581 * TRUE - today date changed
1582 * FALSE - today date isn't changed
1584 static BOOL
1585 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1587 RECT new_r, old_r;
1589 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1591 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->todaysDate, &old_r);
1592 MONTHCAL_CalcPosFromDay(infoPtr, today, &new_r);
1594 infoPtr->todaysDate = *today;
1596 /* only two days need redrawing */
1597 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1598 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1599 return TRUE;
1602 /* MCM_SETTODAT handler */
1603 static LRESULT
1604 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1606 TRACE("%p\n", today);
1608 if(!today) return FALSE;
1610 /* remember if date was set successfully */
1611 if(MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1613 return TRUE;
1616 /* returns calendar index containing specified point, or -1 if it's background */
1617 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1619 RECT r;
1620 INT i;
1622 for (i = 0; i < infoPtr->cal_num; i++)
1624 /* whole bounding rectangle allows some optimization to compute */
1625 r.left = infoPtr->calendars[i].title.left;
1626 r.top = infoPtr->calendars[i].title.top;
1627 r.bottom = infoPtr->calendars[i].days.bottom;
1628 r.right = infoPtr->calendars[i].days.right;
1630 if (PtInRect(&r, *pt)) return i;
1633 return -1;
1636 static LRESULT
1637 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1639 INT day, wday, wnum, calIdx;
1640 SYSTEMTIME ht_month;
1641 UINT x, y;
1643 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1645 x = lpht->pt.x;
1646 y = lpht->pt.y;
1648 memset(&lpht->st, 0, sizeof(lpht->st));
1650 /* Comment in for debugging...
1651 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,
1652 infoPtr->wdays.left, infoPtr->wdays.right,
1653 infoPtr->wdays.top, infoPtr->wdays.bottom,
1654 infoPtr->days.left, infoPtr->days.right,
1655 infoPtr->days.top, infoPtr->days.bottom,
1656 infoPtr->todayrect.left, infoPtr->todayrect.right,
1657 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1658 infoPtr->weeknums.left, infoPtr->weeknums.right,
1659 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1662 /* guess in what calendar we are */
1663 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1664 if (calIdx == -1)
1666 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1667 lpht->uHit = MCHT_TODAYLINK;
1668 else
1669 /* outside of calendar area? What's left must be background :-) */
1670 lpht->uHit = MCHT_CALENDARBK;
1672 return lpht->uHit;
1675 ht_month = infoPtr->calendars[calIdx].month;
1677 /* are we in the header? */
1678 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1679 /* FIXME: buttons hittesting could be optimized cause maximum
1680 two calendars have buttons */
1681 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1683 lpht->uHit = MCHT_TITLEBTNPREV;
1685 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1687 lpht->uHit = MCHT_TITLEBTNNEXT;
1689 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1691 lpht->uHit = MCHT_TITLEMONTH;
1693 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1695 lpht->uHit = MCHT_TITLEYEAR;
1697 else
1698 lpht->uHit = MCHT_TITLE;
1700 return lpht->uHit;
1703 /* days area (including week days and week numbers */
1704 day = MONTHCAL_CalcDayFromPos(infoPtr, x, y, &wday, &wnum);
1705 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1707 lpht->uHit = MCHT_CALENDARDAY;
1708 lpht->st.wYear = ht_month.wYear;
1709 lpht->st.wMonth = (day < 1) ? ht_month.wMonth -1 : ht_month.wMonth;
1710 lpht->st.wDay = (day < 1) ?
1711 MONTHCAL_MonthLength(ht_month.wMonth-1, ht_month.wYear) - day : day;
1713 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1715 lpht->uHit = MCHT_CALENDARWEEKNUM;
1716 lpht->st.wYear = ht_month.wYear;
1718 if (day < 1) {
1719 lpht->st.wMonth = ht_month.wMonth - 1;
1721 else if (day > MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear)) {
1722 lpht->st.wMonth = ht_month.wMonth + 1;
1724 else
1725 lpht->st.wMonth = ht_month.wMonth;
1727 if (day < 1) {
1728 lpht->st.wDay = MONTHCAL_MonthLength(ht_month.wMonth-1, ht_month.wYear) - day;
1730 else if (day > MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear)) {
1731 lpht->st.wDay = day - MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear);
1733 else
1734 lpht->st.wDay = day;
1736 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1738 lpht->st.wYear = ht_month.wYear;
1739 lpht->st.wMonth = ht_month.wMonth;
1740 if (day < 1)
1742 lpht->uHit = MCHT_CALENDARDATEPREV;
1743 MONTHCAL_GetPrevMonth(&lpht->st);
1744 lpht->st.wDay = MONTHCAL_MonthLength(lpht->st.wMonth, lpht->st.wYear) + day;
1746 else if (day > MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear))
1748 lpht->uHit = MCHT_CALENDARDATENEXT;
1749 MONTHCAL_GetNextMonth(&lpht->st);
1750 lpht->st.wDay = day - MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear);
1752 else {
1753 lpht->uHit = MCHT_CALENDARDATE;
1754 lpht->st.wDay = day;
1757 /* always update day of week */
1758 MONTHCAL_CalculateDayOfWeek(&lpht->st, TRUE);
1761 return lpht->uHit;
1764 /* MCN_GETDAYSTATE notification helper */
1765 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1767 if(infoPtr->dwStyle & MCS_DAYSTATE) {
1768 NMDAYSTATE nmds;
1770 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1771 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1772 nmds.nmhdr.code = MCN_GETDAYSTATE;
1773 nmds.cDayState = infoPtr->monthRange;
1774 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1776 nmds.stStart = infoPtr->todaysDate;
1777 nmds.stStart.wYear = infoPtr->minSel.wYear;
1778 nmds.stStart.wMonth = infoPtr->minSel.wMonth;
1779 nmds.stStart.wDay = 1;
1781 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1782 memcpy(infoPtr->monthdayState, nmds.prgDayState, infoPtr->monthRange*sizeof(MONTHDAYSTATE));
1784 Free(nmds.prgDayState);
1788 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
1790 INT delta = infoPtr->delta ? infoPtr->delta : infoPtr->cal_num;
1791 SYSTEMTIME st;
1793 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
1795 /* check if change allowed by range set */
1796 if(direction == DIRECTION_BACKWARD)
1798 st = infoPtr->calendars[0].month;
1799 MONTHCAL_GetMonth(&st, -delta);
1801 else
1803 st = infoPtr->calendars[infoPtr->cal_num-1].month;
1804 MONTHCAL_GetMonth(&st, delta);
1807 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
1809 if(infoPtr->dwStyle & MCS_MULTISELECT)
1811 SYSTEMTIME range[2];
1813 range[0] = infoPtr->minSel;
1814 range[1] = infoPtr->maxSel;
1816 if(direction == DIRECTION_BACKWARD)
1818 MONTHCAL_GetMonth(&range[0], -delta);
1819 MONTHCAL_GetMonth(&range[1], -delta);
1821 else
1823 MONTHCAL_GetMonth(&range[0], delta);
1824 MONTHCAL_GetMonth(&range[1], delta);
1827 MONTHCAL_SetSelRange(infoPtr, range);
1829 else
1830 MONTHCAL_SetCurSel(infoPtr, &st);
1832 MONTHCAL_NotifyDayState(infoPtr);
1833 MONTHCAL_NotifySelectionChange(infoPtr);
1836 static LRESULT
1837 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1839 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1840 HMENU hMenu;
1841 POINT menupoint;
1842 WCHAR buf[32];
1844 hMenu = CreatePopupMenu();
1845 if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1847 WARN("Can't load resource\n");
1848 strcpyW(buf, todayW);
1850 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1851 menupoint.x = (short)LOWORD(lParam);
1852 menupoint.y = (short)HIWORD(lParam);
1853 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1854 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
1855 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1857 infoPtr->calendars[0].month = infoPtr->todaysDate;
1858 infoPtr->minSel = infoPtr->todaysDate;
1859 infoPtr->maxSel = infoPtr->todaysDate;
1860 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1863 return 0;
1866 /***
1867 * DESCRIPTION:
1868 * Subclassed edit control windproc function
1870 * PARAMETER(S):
1871 * [I] hwnd : the edit window handle
1872 * [I] uMsg : the message that is to be processed
1873 * [I] wParam : first message parameter
1874 * [I] lParam : second message parameter
1877 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1879 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
1881 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
1882 hwnd, uMsg, wParam, lParam);
1884 switch (uMsg)
1886 case WM_GETDLGCODE:
1887 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
1889 case WM_DESTROY:
1891 WNDPROC editProc = infoPtr->EditWndProc;
1892 infoPtr->EditWndProc = NULL;
1893 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
1894 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
1897 case WM_KILLFOCUS:
1898 break;
1900 case WM_KEYDOWN:
1901 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
1902 break;
1904 default:
1905 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
1908 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
1909 SendMessageW(hwnd, WM_CLOSE, 0, 0);
1910 return 0;
1913 /* creates updown control and edit box */
1914 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr)
1916 infoPtr->hWndYearEdit =
1917 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
1918 infoPtr->calendars[0].titleyear.left + 3, infoPtr->titlebtnnext.top,
1919 infoPtr->calendars[0].titleyear.right - infoPtr->calendars[0].titleyear.left + 4,
1920 infoPtr->textHeight, infoPtr->hwndSelf,
1921 NULL, NULL, NULL);
1923 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
1925 infoPtr->hWndYearUpDown =
1926 CreateWindowExW(0, UPDOWN_CLASSW, 0,
1927 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
1928 infoPtr->calendars[0].titleyear.right + 7, infoPtr->titlebtnnext.top,
1929 18, infoPtr->textHeight, infoPtr->hwndSelf,
1930 NULL, NULL, NULL);
1932 /* attach edit box */
1933 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
1934 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
1935 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
1936 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->minSel.wYear);
1938 /* subclass edit box */
1939 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
1940 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
1942 SetFocus(infoPtr->hWndYearEdit);
1945 static LRESULT
1946 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1948 MCHITTESTINFO ht;
1949 DWORD hit;
1951 /* Actually we don't need input focus for calendar, this is used to kill
1952 year updown and its buddy edit box */
1953 if (IsWindow(infoPtr->hWndYearUpDown))
1955 SetFocus(infoPtr->hwndSelf);
1956 return 0;
1959 SetCapture(infoPtr->hwndSelf);
1961 ht.cbSize = sizeof(MCHITTESTINFO);
1962 ht.pt.x = (short)LOWORD(lParam);
1963 ht.pt.y = (short)HIWORD(lParam);
1965 hit = MONTHCAL_HitTest(infoPtr, &ht);
1967 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
1969 switch(hit)
1971 case MCHT_TITLEBTNNEXT:
1972 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
1973 infoPtr->status = MC_NEXTPRESSED;
1974 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1975 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1976 return 0;
1978 case MCHT_TITLEBTNPREV:
1979 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
1980 infoPtr->status = MC_PREVPRESSED;
1981 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1982 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1983 return 0;
1985 case MCHT_TITLEMONTH:
1987 HMENU hMenu = CreatePopupMenu();
1988 WCHAR buf[32];
1989 POINT menupoint;
1990 INT i;
1992 for (i = 0; i < 12; i++)
1994 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
1995 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
1997 menupoint.x = ht.pt.x;
1998 menupoint.y = ht.pt.y;
1999 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2000 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
2001 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
2003 if ((i > 0) && (i < 13) && infoPtr->minSel.wMonth != i)
2005 infoPtr->minSel.wMonth = i;
2006 MONTHCAL_IsDateInValidRange(infoPtr, &infoPtr->minSel, TRUE);
2007 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2009 return 0;
2011 case MCHT_TITLEYEAR:
2013 MONTHCAL_EditYear(infoPtr);
2014 return 0;
2016 case MCHT_TODAYLINK:
2018 infoPtr->calendars[0].month = infoPtr->todaysDate;
2019 infoPtr->minSel = infoPtr->todaysDate;
2020 infoPtr->maxSel = infoPtr->todaysDate;
2021 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2023 MONTHCAL_NotifySelectionChange(infoPtr);
2024 MONTHCAL_NotifySelect(infoPtr);
2025 return 0;
2027 case MCHT_CALENDARDATENEXT:
2028 case MCHT_CALENDARDATEPREV:
2029 case MCHT_CALENDARDATE:
2031 SYSTEMTIME st[2];
2033 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2035 st[0] = st[1] = ht.st;
2036 /* clear selection range */
2037 MONTHCAL_SetSelRange(infoPtr, st);
2039 infoPtr->status = MC_SEL_LBUTDOWN;
2040 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
2041 return 0;
2045 return 1;
2049 static LRESULT
2050 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2052 NMHDR nmhdr;
2053 MCHITTESTINFO ht;
2054 DWORD hit;
2056 TRACE("\n");
2058 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2059 RECT *r;
2061 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2062 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2063 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2065 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2068 ReleaseCapture();
2070 /* always send NM_RELEASEDCAPTURE notification */
2071 nmhdr.hwndFrom = infoPtr->hwndSelf;
2072 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2073 nmhdr.code = NM_RELEASEDCAPTURE;
2074 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2076 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2078 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2080 ht.cbSize = sizeof(MCHITTESTINFO);
2081 ht.pt.x = (short)LOWORD(lParam);
2082 ht.pt.y = (short)HIWORD(lParam);
2083 hit = MONTHCAL_HitTest(infoPtr, &ht);
2085 infoPtr->status = MC_SEL_LBUTUP;
2086 MONTHCAL_SetDayFocus(infoPtr, NULL);
2088 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2090 SYSTEMTIME sel = infoPtr->minSel;
2092 /* will be invalidated here */
2093 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2095 /* send MCN_SELCHANGE only if new date selected */
2096 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2097 MONTHCAL_NotifySelectionChange(infoPtr);
2099 MONTHCAL_NotifySelect(infoPtr);
2102 return 0;
2106 static LRESULT
2107 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2109 TRACE("%ld\n", id);
2111 switch(id) {
2112 case MC_PREVNEXTMONTHTIMER:
2113 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2114 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2115 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2116 break;
2117 case MC_TODAYUPDATETIMER:
2119 SYSTEMTIME st;
2121 if(infoPtr->todaySet) return 0;
2123 GetLocalTime(&st);
2124 MONTHCAL_UpdateToday(infoPtr, &st);
2126 /* notification sent anyway */
2127 MONTHCAL_NotifySelectionChange(infoPtr);
2129 return 0;
2131 default:
2132 ERR("got unknown timer %ld\n", id);
2133 break;
2136 return 0;
2140 static LRESULT
2141 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2143 MCHITTESTINFO ht;
2144 SYSTEMTIME st_ht;
2145 INT hit;
2146 RECT r;
2148 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2150 ht.cbSize = sizeof(MCHITTESTINFO);
2151 ht.pt.x = (short)LOWORD(lParam);
2152 ht.pt.y = (short)HIWORD(lParam);
2154 hit = MONTHCAL_HitTest(infoPtr, &ht);
2156 /* not on the calendar date numbers? bail out */
2157 TRACE("hit:%x\n",hit);
2158 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2160 MONTHCAL_SetDayFocus(infoPtr, NULL);
2161 return 0;
2164 st_ht = ht.st;
2166 /* if pointer is over focused day still there's nothing to do */
2167 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2169 MONTHCAL_CalcPosFromDay(infoPtr, &ht.st, &r);
2171 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2172 SYSTEMTIME st[2];
2174 MONTHCAL_GetSelRange(infoPtr, st);
2176 /* If we're still at the first selected date and range is empty, return.
2177 If range isn't empty we should change range to a single firstSel */
2178 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2179 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2181 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2183 st[0] = infoPtr->firstSel;
2184 /* we should overwrite timestamp here */
2185 MONTHCAL_CopyDate(&st_ht, &st[1]);
2187 /* bounds will be swapped here if needed */
2188 MONTHCAL_SetSelRange(infoPtr, st);
2190 return 0;
2193 done:
2195 /* FIXME: this should specify a rectangle containing only the days that changed
2196 using InvalidateRect */
2197 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2199 return 0;
2203 static LRESULT
2204 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2206 HDC hdc;
2207 PAINTSTRUCT ps;
2209 if (hdc_paint)
2211 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2212 hdc = hdc_paint;
2214 else
2215 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2217 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2218 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2219 return 0;
2222 static LRESULT
2223 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2225 HBRUSH hbr;
2226 RECT rc;
2228 if (!GetClipBox(hdc, &rc)) return FALSE;
2230 /* fill background */
2231 hbr = CreateSolidBrush (infoPtr->bk);
2232 FillRect(hdc, &rc, hbr);
2233 DeleteObject(hbr);
2235 return TRUE;
2238 static LRESULT
2239 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2241 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2243 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2244 return 0;
2246 if (options & PRF_ERASEBKGND)
2247 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2249 if (options & PRF_CLIENT)
2250 MONTHCAL_Paint(infoPtr, hdc);
2252 return 0;
2255 static LRESULT
2256 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2258 TRACE("\n");
2260 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2262 return 0;
2265 /* sets the size information */
2266 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2268 static const WCHAR O0W[] = { '0','0',0 };
2269 HDC hdc = GetDC(infoPtr->hwndSelf);
2270 RECT *title=&infoPtr->calendars[0].title;
2271 RECT *prev=&infoPtr->titlebtnprev;
2272 RECT *next=&infoPtr->titlebtnnext;
2273 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2274 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2275 RECT *wdays=&infoPtr->calendars[0].wdays;
2276 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2277 RECT *days=&infoPtr->calendars[0].days;
2278 RECT *todayrect=&infoPtr->todayrect;
2279 SIZE size, sz;
2280 TEXTMETRICW tm;
2281 HFONT currentFont;
2282 INT xdiv, dx, dy, i;
2283 RECT rcClient;
2284 WCHAR buff[80];
2286 GetClientRect(infoPtr->hwndSelf, &rcClient);
2288 currentFont = SelectObject(hdc, infoPtr->hFont);
2290 /* get the height and width of each day's text */
2291 GetTextMetricsW(hdc, &tm);
2292 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2294 /* find largest abbreviated day name for current locale */
2295 size.cx = sz.cx = 0;
2296 for (i = 0; i < 7; i++)
2298 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + i,
2299 buff, countof(buff)))
2301 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2302 if (sz.cx > size.cx) size.cx = sz.cx;
2304 else /* locale independent fallback on failure */
2306 static const WCHAR SunW[] = { 'S','u','n',0 };
2308 GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2309 break;
2313 infoPtr->textWidth = size.cx + 2;
2315 /* recalculate the height and width increments and offsets */
2316 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2318 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2320 infoPtr->width_increment = size.cx * 2 + 4;
2321 infoPtr->height_increment = infoPtr->textHeight;
2323 /* calculate title area */
2324 title->top = 0;
2325 title->bottom = 3 * infoPtr->height_increment / 2;
2326 title->left = 0;
2327 title->right = infoPtr->width_increment * xdiv;
2329 /* set the dimensions of the next and previous buttons and center */
2330 /* the month text vertically */
2331 prev->top = next->top = title->top + 4;
2332 prev->bottom = next->bottom = title->bottom - 4;
2333 prev->left = title->left + 4;
2334 prev->right = prev->left + (title->bottom - title->top);
2335 next->right = title->right - 4;
2336 next->left = next->right - (title->bottom - title->top);
2338 /* titlemonth->left and right change based upon the current month */
2339 /* and are recalculated in refresh as the current month may change */
2340 /* without the control being resized */
2341 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2342 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2344 /* setup the dimensions of the rectangle we draw the names of the */
2345 /* days of the week in */
2346 weeknumrect->left = 0;
2348 if(infoPtr->dwStyle & MCS_WEEKNUMBERS)
2349 weeknumrect->right = prev->right;
2350 else
2351 weeknumrect->right = weeknumrect->left;
2353 wdays->left = days->left = weeknumrect->right;
2354 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2355 wdays->top = title->bottom;
2356 wdays->bottom = wdays->top + infoPtr->height_increment;
2358 days->top = weeknumrect->top = wdays->bottom;
2359 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2361 todayrect->left = 0;
2362 todayrect->right = title->right;
2363 todayrect->top = days->bottom;
2364 todayrect->bottom = days->bottom + infoPtr->height_increment;
2366 /* offset all rectangles to center in client area */
2367 dx = (rcClient.right - title->right) / 2;
2368 dy = (rcClient.bottom - todayrect->bottom) / 2;
2370 /* if calendar doesn't fit client area show it at left/top bounds */
2371 if (title->left + dx < 0) dx = 0;
2372 if (title->top + dy < 0) dy = 0;
2374 if (dx != 0 || dy != 0)
2376 OffsetRect(title, dx, dy);
2377 OffsetRect(prev, dx, dy);
2378 OffsetRect(next, dx, dy);
2379 OffsetRect(titlemonth, dx, dy);
2380 OffsetRect(titleyear, dx, dy);
2381 OffsetRect(wdays, dx, dy);
2382 OffsetRect(weeknumrect, dx, dy);
2383 OffsetRect(days, dx, dy);
2384 OffsetRect(todayrect, dx, dy);
2387 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2388 infoPtr->width_increment,infoPtr->height_increment,
2389 wine_dbgstr_rect(&rcClient),
2390 wine_dbgstr_rect(title),
2391 wine_dbgstr_rect(wdays),
2392 wine_dbgstr_rect(days),
2393 wine_dbgstr_rect(todayrect));
2395 /* restore the originally selected font */
2396 SelectObject(hdc, currentFont);
2398 ReleaseDC(infoPtr->hwndSelf, hdc);
2401 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2403 TRACE("(width=%d, height=%d)\n", Width, Height);
2405 MONTHCAL_UpdateSize(infoPtr);
2407 /* invalidate client area and erase background */
2408 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2410 return 0;
2413 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2415 return (LRESULT)infoPtr->hFont;
2418 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2420 HFONT hOldFont;
2421 LOGFONTW lf;
2423 if (!hFont) return 0;
2425 hOldFont = infoPtr->hFont;
2426 infoPtr->hFont = hFont;
2428 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2429 lf.lfWeight = FW_BOLD;
2430 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2432 MONTHCAL_UpdateSize(infoPtr);
2434 if (redraw)
2435 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2437 return (LRESULT)hOldFont;
2440 /* update theme after a WM_THEMECHANGED message */
2441 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2443 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2444 CloseThemeData (theme);
2445 OpenThemeData (infoPtr->hwndSelf, themeClass);
2446 return 0;
2449 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2450 const STYLESTRUCT *lpss)
2452 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2453 wStyleType, lpss->styleOld, lpss->styleNew);
2455 if (wStyleType != GWL_STYLE) return 0;
2457 infoPtr->dwStyle = lpss->styleNew;
2459 /* make room for week numbers */
2460 if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2461 MONTHCAL_UpdateSize(infoPtr);
2463 return 0;
2466 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2467 STYLESTRUCT *lpss)
2469 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2470 wStyleType, lpss->styleOld, lpss->styleNew);
2472 /* block MCS_MULTISELECT change */
2473 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2475 if (lpss->styleOld & MCS_MULTISELECT)
2476 lpss->styleNew |= MCS_MULTISELECT;
2477 else
2478 lpss->styleNew &= ~MCS_MULTISELECT;
2481 return 0;
2484 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2485 static LRESULT
2486 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2488 MONTHCAL_INFO *infoPtr;
2490 /* allocate memory for info structure */
2491 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2492 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2494 if (infoPtr == NULL) {
2495 ERR("could not allocate info memory!\n");
2496 return 0;
2499 infoPtr->hwndSelf = hwnd;
2500 infoPtr->hwndNotify = lpcs->hwndParent;
2501 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2502 infoPtr->calendars = Alloc(sizeof(CALENDAR_INFO));
2503 if (!infoPtr->calendars) goto fail;
2505 infoPtr->cal_num = 1;
2507 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
2509 /* initialize info structure */
2510 /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
2512 GetLocalTime(&infoPtr->todaysDate);
2513 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2515 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2516 infoPtr->monthRange = 3;
2518 infoPtr->monthdayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
2519 if (!infoPtr->monthdayState) goto fail;
2521 infoPtr->titlebk = comctl32_color.clrActiveCaption;
2522 infoPtr->titletxt = comctl32_color.clrWindow;
2523 infoPtr->monthbk = comctl32_color.clrWindow;
2524 infoPtr->trailingtxt = comctl32_color.clrGrayText;
2525 infoPtr->bk = comctl32_color.clrWindow;
2526 infoPtr->txt = comctl32_color.clrWindowText;
2528 infoPtr->minSel = infoPtr->todaysDate;
2529 infoPtr->maxSel = infoPtr->todaysDate;
2530 infoPtr->calendars[0].month = infoPtr->todaysDate;
2531 infoPtr->isUnicode = TRUE;
2533 /* call MONTHCAL_UpdateSize to set all of the dimensions */
2534 /* of the control */
2535 MONTHCAL_UpdateSize(infoPtr);
2537 /* today auto update timer, to be freed only on control destruction */
2538 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2540 OpenThemeData (infoPtr->hwndSelf, themeClass);
2542 return 0;
2544 fail:
2545 Free(infoPtr->monthdayState);
2546 Free(infoPtr->calendars);
2547 Free(infoPtr);
2548 return 0;
2551 static LRESULT
2552 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2554 /* free month calendar info data */
2555 Free(infoPtr->monthdayState);
2556 Free(infoPtr->calendars);
2557 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2559 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2561 Free(infoPtr);
2562 return 0;
2566 * Handler for WM_NOTIFY messages
2568 static LRESULT
2569 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2571 /* notification from year edit updown */
2572 if (hdr->code == UDN_DELTAPOS)
2574 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2576 if (hdr->hwndFrom == infoPtr->hWndYearUpDown)
2578 /* year value limits are set up explicitly after updown creation */
2579 if ((nmud->iDelta + nmud->iPos) != infoPtr->minSel.wYear)
2581 SYSTEMTIME new_date = infoPtr->minSel;
2583 new_date.wYear = nmud->iDelta + nmud->iPos;
2584 MONTHCAL_SetCurSel(infoPtr, &new_date);
2588 return 0;
2591 static inline BOOL
2592 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2594 BOOL prev = infoPtr->isUnicode;
2595 infoPtr->isUnicode = isUnicode;
2596 return prev;
2599 static inline BOOL
2600 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2602 return infoPtr->isUnicode;
2605 static LRESULT WINAPI
2606 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2608 MONTHCAL_INFO *infoPtr;
2610 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2612 infoPtr = MONTHCAL_GetInfoPtr(hwnd);
2613 if (!infoPtr && (uMsg != WM_CREATE))
2614 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2615 switch(uMsg)
2617 case MCM_GETCURSEL:
2618 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2620 case MCM_SETCURSEL:
2621 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2623 case MCM_GETMAXSELCOUNT:
2624 return MONTHCAL_GetMaxSelCount(infoPtr);
2626 case MCM_SETMAXSELCOUNT:
2627 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2629 case MCM_GETSELRANGE:
2630 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2632 case MCM_SETSELRANGE:
2633 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2635 case MCM_GETMONTHRANGE:
2636 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2638 case MCM_SETDAYSTATE:
2639 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2641 case MCM_GETMINREQRECT:
2642 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2644 case MCM_GETCOLOR:
2645 return MONTHCAL_GetColor(infoPtr, wParam);
2647 case MCM_SETCOLOR:
2648 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2650 case MCM_GETTODAY:
2651 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2653 case MCM_SETTODAY:
2654 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2656 case MCM_HITTEST:
2657 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2659 case MCM_GETFIRSTDAYOFWEEK:
2660 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2662 case MCM_SETFIRSTDAYOFWEEK:
2663 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2665 case MCM_GETRANGE:
2666 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2668 case MCM_SETRANGE:
2669 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2671 case MCM_GETMONTHDELTA:
2672 return MONTHCAL_GetMonthDelta(infoPtr);
2674 case MCM_SETMONTHDELTA:
2675 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2677 case MCM_GETMAXTODAYWIDTH:
2678 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2680 case MCM_SETUNICODEFORMAT:
2681 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2683 case MCM_GETUNICODEFORMAT:
2684 return MONTHCAL_GetUnicodeFormat(infoPtr);
2686 case WM_GETDLGCODE:
2687 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2689 case WM_RBUTTONUP:
2690 return MONTHCAL_RButtonUp(infoPtr, lParam);
2692 case WM_LBUTTONDOWN:
2693 return MONTHCAL_LButtonDown(infoPtr, lParam);
2695 case WM_MOUSEMOVE:
2696 return MONTHCAL_MouseMove(infoPtr, lParam);
2698 case WM_LBUTTONUP:
2699 return MONTHCAL_LButtonUp(infoPtr, lParam);
2701 case WM_PAINT:
2702 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2704 case WM_PRINTCLIENT:
2705 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2707 case WM_ERASEBKGND:
2708 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2710 case WM_SETFOCUS:
2711 return MONTHCAL_SetFocus(infoPtr);
2713 case WM_SIZE:
2714 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2716 case WM_NOTIFY:
2717 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2719 case WM_CREATE:
2720 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2722 case WM_SETFONT:
2723 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2725 case WM_GETFONT:
2726 return MONTHCAL_GetFont(infoPtr);
2728 case WM_TIMER:
2729 return MONTHCAL_Timer(infoPtr, wParam);
2731 case WM_THEMECHANGED:
2732 return theme_changed (infoPtr);
2734 case WM_DESTROY:
2735 return MONTHCAL_Destroy(infoPtr);
2737 case WM_SYSCOLORCHANGE:
2738 COMCTL32_RefreshSysColors();
2739 return 0;
2741 case WM_STYLECHANGED:
2742 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2744 case WM_STYLECHANGING:
2745 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2747 default:
2748 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2749 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2750 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2755 void
2756 MONTHCAL_Register(void)
2758 WNDCLASSW wndClass;
2760 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2761 wndClass.style = CS_GLOBALCLASS;
2762 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2763 wndClass.cbClsExtra = 0;
2764 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2765 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2766 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2767 wndClass.lpszClassName = MONTHCAL_CLASSW;
2769 RegisterClassW(&wndClass);
2773 void
2774 MONTHCAL_Unregister(void)
2776 UnregisterClassW(MONTHCAL_CLASSW, NULL);