kernel32: GetLongPathName should fail when called with a wildcard.
[wine.git] / dlls / comctl32 / monthcal.c
blob3fb76000d59196b664c859e0a1af0d1b92bfffeb
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-2011 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 * TODO:
26 * -- MCM_[GS]ETUNICODEFORMAT
27 * -- handle resources better (doesn't work now);
28 * -- take care of internationalization.
29 * -- keyboard handling.
30 * -- search for FIXME
33 #include <math.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "winnls.h"
44 #include "commctrl.h"
45 #include "comctl32.h"
46 #include "uxtheme.h"
47 #include "vssym32.h"
48 #include "wine/unicode.h"
49 #include "wine/debug.h"
50 #include "wine/heap.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
54 #define MC_SEL_LBUTUP 1 /* Left button released */
55 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
56 #define MC_PREVPRESSED 4 /* Prev month button pressed */
57 #define MC_NEXTPRESSED 8 /* Next month button pressed */
58 #define MC_PREVNEXTMONTHDELAY 350 /* when continuously pressing `next/prev
59 month', wait 350 ms before going
60 to the next/prev month */
61 #define MC_TODAYUPDATEDELAY 120000 /* time between today check for update (2 min) */
63 #define MC_PREVNEXTMONTHTIMER 1 /* Timer IDs */
64 #define MC_TODAYUPDATETIMER 2
66 #define MC_CALENDAR_PADDING 6
68 /* convert from days to 100 nanoseconds unit - used as FILETIME unit */
69 #define DAYSTO100NSECS(days) (((ULONGLONG)(days))*24*60*60*10000000)
71 enum CachedPen
73 PenRed = 0,
74 PenText,
75 PenLast
78 enum CachedBrush
80 BrushTitle = 0,
81 BrushMonth,
82 BrushBackground,
83 BrushLast
86 /* single calendar data */
87 typedef struct _CALENDAR_INFO
89 RECT title; /* rect for the header above the calendar */
90 RECT titlemonth; /* the 'month name' text in the header */
91 RECT titleyear; /* the 'year number' text in the header */
92 RECT wdays; /* week days at top */
93 RECT days; /* calendar area */
94 RECT weeknums; /* week numbers at left side */
96 SYSTEMTIME month;/* contains calendar main month/year */
97 } CALENDAR_INFO;
99 typedef struct
101 HWND hwndSelf;
102 DWORD dwStyle; /* cached GWL_STYLE */
104 COLORREF colors[MCSC_TRAILINGTEXT+1];
105 HBRUSH brushes[BrushLast];
106 HPEN pens[PenLast];
108 HFONT hFont;
109 HFONT hBoldFont;
110 int textHeight;
111 int height_increment;
112 int width_increment;
113 INT delta; /* scroll rate; # of months that the */
114 /* control moves when user clicks a scroll button */
115 int firstDay; /* Start month calendar with firstDay's day,
116 stored in SYSTEMTIME format */
117 BOOL firstDaySet; /* first week day differs from locale defined */
119 BOOL isUnicode; /* value set with MCM_SETUNICODE format */
121 MONTHDAYSTATE *monthdayState;
122 SYSTEMTIME todaysDate;
123 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
124 int status; /* See MC_SEL flags */
125 SYSTEMTIME firstSel; /* first selected day */
126 INT maxSelCount;
127 SYSTEMTIME minSel; /* contains single selection when used without MCS_MULTISELECT */
128 SYSTEMTIME maxSel;
129 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
130 DWORD rangeValid;
131 SYSTEMTIME minDate;
132 SYSTEMTIME maxDate;
134 RECT titlebtnnext; /* the `next month' button in the header */
135 RECT titlebtnprev; /* the `prev month' button in the header */
136 RECT todayrect; /* `today: xx/xx/xx' text rect */
137 HWND hwndNotify; /* Window to receive the notifications */
138 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
139 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
140 WNDPROC EditWndProc; /* original Edit window procedure */
142 CALENDAR_INFO *calendars;
143 SIZE dim; /* [cx,cy] - dimensions of calendars matrix, row/column count */
144 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
146 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
148 /* empty SYSTEMTIME const */
149 static const SYSTEMTIME st_null;
150 /* valid date limits */
151 static const SYSTEMTIME max_allowed_date = { /* wYear */ 9999, /* wMonth */ 12, /* wDayOfWeek */ 0, /* wDay */ 31 };
152 static const SYSTEMTIME min_allowed_date = { /* wYear */ 1752, /* wMonth */ 9, /* wDayOfWeek */ 0, /* wDay */ 14 };
154 /* Prev/Next buttons */
155 enum nav_direction
157 DIRECTION_BACKWARD,
158 DIRECTION_FORWARD
161 /* helper functions */
162 static inline INT MONTHCAL_GetCalCount(const MONTHCAL_INFO *infoPtr)
164 return infoPtr->dim.cx * infoPtr->dim.cy;
167 /* send a single MCN_SELCHANGE notification */
168 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
170 NMSELCHANGE nmsc;
172 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
173 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
174 nmsc.nmhdr.code = MCN_SELCHANGE;
175 nmsc.stSelStart = infoPtr->minSel;
176 nmsc.stSelStart.wDayOfWeek = 0;
177 if(infoPtr->dwStyle & MCS_MULTISELECT){
178 nmsc.stSelEnd = infoPtr->maxSel;
179 nmsc.stSelEnd.wDayOfWeek = 0;
181 else
182 nmsc.stSelEnd = st_null;
184 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
187 /* send a single MCN_SELECT notification */
188 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
190 NMSELCHANGE nmsc;
192 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
193 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
194 nmsc.nmhdr.code = MCN_SELECT;
195 nmsc.stSelStart = infoPtr->minSel;
196 nmsc.stSelStart.wDayOfWeek = 0;
197 if(infoPtr->dwStyle & MCS_MULTISELECT){
198 nmsc.stSelEnd = infoPtr->maxSel;
199 nmsc.stSelEnd.wDayOfWeek = 0;
201 else
202 nmsc.stSelEnd = st_null;
204 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
207 static inline int MONTHCAL_MonthDiff(const SYSTEMTIME *left, const SYSTEMTIME *right)
209 return (right->wYear - left->wYear)*12 + right->wMonth - left->wMonth;
212 /* returns the number of days in any given month, checking for leap days */
213 /* January is 1, December is 12 */
214 int MONTHCAL_MonthLength(int month, int year)
216 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
217 /* Wrap around, this eases handling. Getting length only we shouldn't care
218 about year change here cause January and December have
219 the same day quantity */
220 if(month == 0)
221 month = 12;
222 else if(month == 13)
223 month = 1;
225 /* special case for calendar transition year */
226 if(month == min_allowed_date.wMonth && year == min_allowed_date.wYear) return 19;
228 /* if we have a leap year add 1 day to February */
229 /* a leap year is a year either divisible by 400 */
230 /* or divisible by 4 and not by 100 */
231 if(month == 2) { /* February */
232 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
233 (year%4 == 0)) ? 1 : 0);
235 else {
236 return mdays[month - 1];
240 /* compares timestamps using date part only */
241 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
243 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
244 (first->wDay == second->wDay);
247 /* make sure that date fields are valid */
248 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
250 if (time->wMonth < 1 || time->wMonth > 12 )
251 return FALSE;
252 if (time->wDay == 0 || time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear))
253 return FALSE;
255 return TRUE;
258 /* Copies timestamp part only.
260 * PARAMETERS
262 * [I] from : source date
263 * [O] to : dest date
265 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
267 to->wHour = from->wHour;
268 to->wMinute = from->wMinute;
269 to->wSecond = from->wSecond;
272 /* Copies date part only.
274 * PARAMETERS
276 * [I] from : source date
277 * [O] to : dest date
279 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
281 to->wYear = from->wYear;
282 to->wMonth = from->wMonth;
283 to->wDay = from->wDay;
284 to->wDayOfWeek = from->wDayOfWeek;
287 /* Compares two dates in SYSTEMTIME format
289 * PARAMETERS
291 * [I] first : pointer to valid first date data to compare
292 * [I] second : pointer to valid second date data to compare
294 * RETURN VALUE
296 * -1 : first < second
297 * 0 : first == second
298 * 1 : first > second
300 * Note that no date validation performed, already validated values expected.
302 LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
304 FILETIME ft_first, ft_second;
306 SystemTimeToFileTime(first, &ft_first);
307 SystemTimeToFileTime(second, &ft_second);
309 return CompareFileTime(&ft_first, &ft_second);
312 static LONG MONTHCAL_CompareMonths(const SYSTEMTIME *first, const SYSTEMTIME *second)
314 SYSTEMTIME st_first, st_second;
316 st_first = st_second = st_null;
317 MONTHCAL_CopyDate(first, &st_first);
318 MONTHCAL_CopyDate(second, &st_second);
319 st_first.wDay = st_second.wDay = 1;
321 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
324 static LONG MONTHCAL_CompareDate(const SYSTEMTIME *first, const SYSTEMTIME *second)
326 SYSTEMTIME st_first, st_second;
328 st_first = st_second = st_null;
329 MONTHCAL_CopyDate(first, &st_first);
330 MONTHCAL_CopyDate(second, &st_second);
332 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
335 /* Checks largest possible date range and configured one
337 * PARAMETERS
339 * [I] infoPtr : valid pointer to control data
340 * [I] date : pointer to valid date data to check
341 * [I] fix : make date fit valid range
343 * RETURN VALUE
345 * TRUE - date within largest and configured range
346 * FALSE - date is outside largest or configured range
348 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr,
349 SYSTEMTIME *date, BOOL fix)
351 const SYSTEMTIME *fix_st = NULL;
353 if(MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) {
354 fix_st = &max_allowed_date;
356 else if(MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1) {
357 fix_st = &min_allowed_date;
359 else {
360 if(infoPtr->rangeValid & GDTR_MAX) {
361 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
362 fix_st = &infoPtr->maxDate;
366 if(infoPtr->rangeValid & GDTR_MIN) {
367 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
368 fix_st = &infoPtr->minDate;
373 if (fix && fix_st) {
374 date->wYear = fix_st->wYear;
375 date->wMonth = fix_st->wMonth;
378 return !fix_st;
381 /* Checks passed range width with configured maximum selection count
383 * PARAMETERS
385 * [I] infoPtr : valid pointer to control data
386 * [I] range0 : pointer to valid date data (requested bound)
387 * [I] range1 : pointer to valid date data (primary bound)
388 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
390 * Adjust value computed basing on primary bound and current maximum selection
391 * count. For simple range check (without adjusted value required) (range0, range1)
392 * relation means nothing.
394 * RETURN VALUE
396 * TRUE - range is shorter or equal to maximum
397 * FALSE - range is larger than maximum
399 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
400 const SYSTEMTIME *range0,
401 const SYSTEMTIME *range1,
402 SYSTEMTIME *adjust)
404 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
405 FILETIME ft_range0, ft_range1;
406 LONG cmp;
408 SystemTimeToFileTime(range0, &ft_range0);
409 SystemTimeToFileTime(range1, &ft_range1);
411 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
412 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
413 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
414 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
416 cmp = CompareFileTime(&ft_range0, &ft_range1);
418 if(cmp == 1)
419 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
420 else
421 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
423 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
425 if(adjust) {
426 if(cmp == 1)
427 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
428 else
429 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
431 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
432 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
433 FileTimeToSystemTime(&ft_range0, adjust);
436 return FALSE;
438 else return TRUE;
441 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
442 Milliseconds are intentionally not validated. */
443 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
445 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
446 return FALSE;
447 else
448 return TRUE;
451 /* Note:Depending on DST, this may be offset by a day.
452 Need to find out if we're on a DST place & adjust the clock accordingly.
453 Above function assumes we have a valid data.
454 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
455 0 = Sunday.
458 /* Returns the day in the week
460 * PARAMETERS
461 * [i] date : input date
462 * [I] inplace : set calculated value back to date structure
464 * RETURN VALUE
465 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
467 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
469 SYSTEMTIME st = st_null;
470 FILETIME ft;
472 MONTHCAL_CopyDate(date, &st);
474 SystemTimeToFileTime(&st, &ft);
475 FileTimeToSystemTime(&ft, &st);
477 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
479 return st.wDayOfWeek;
482 /* add/subtract 'months' from date */
483 static inline void MONTHCAL_GetMonth(SYSTEMTIME *date, INT months)
485 INT length, m = date->wMonth + months;
487 date->wYear += m > 0 ? (m - 1) / 12 : m / 12 - 1;
488 date->wMonth = m > 0 ? (m - 1) % 12 + 1 : 12 + m % 12;
489 /* fix moving from last day in a month */
490 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
491 if(date->wDay > length) date->wDay = length;
492 MONTHCAL_CalculateDayOfWeek(date, TRUE);
495 /* properly updates date to point on next month */
496 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
498 MONTHCAL_GetMonth(date, 1);
501 /* properly updates date to point on prev month */
502 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
504 MONTHCAL_GetMonth(date, -1);
507 /* Returns full date for a first currently visible day */
508 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
510 /* zero indexed calendar has the earliest date */
511 SYSTEMTIME st_first = infoPtr->calendars[0].month;
512 INT firstDay;
514 st_first.wDay = 1;
515 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
517 *date = infoPtr->calendars[0].month;
518 MONTHCAL_GetPrevMonth(date);
520 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
521 (infoPtr->firstDay - firstDay) % 7 + 1;
523 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
524 date->wDay -= 7;
526 /* fix day of week */
527 MONTHCAL_CalculateDayOfWeek(date, TRUE);
530 /* Returns full date for a last currently visible day */
531 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
533 /* the latest date is in latest calendar */
534 SYSTEMTIME st, *lt_month = &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
535 INT first_day;
537 *date = *lt_month;
538 st = *lt_month;
540 /* day of week of first day of current month */
541 st.wDay = 1;
542 first_day = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
544 MONTHCAL_GetNextMonth(date);
545 MONTHCAL_GetPrevMonth(&st);
547 /* last calendar starts with some date from previous month that not displayed */
548 st.wDay = MONTHCAL_MonthLength(st.wMonth, st.wYear) +
549 (infoPtr->firstDay - first_day) % 7 + 1;
550 if (st.wDay > MONTHCAL_MonthLength(st.wMonth, st.wYear)) st.wDay -= 7;
552 /* Use month length to get max day. 42 means max day count in calendar area */
553 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
554 MONTHCAL_MonthLength(lt_month->wMonth, lt_month->wYear);
556 /* fix day of week */
557 MONTHCAL_CalculateDayOfWeek(date, TRUE);
560 /* From a given point calculate the row, column and day in the calendar,
561 'day == 0' means the last day of the last month. */
562 static int MONTHCAL_GetDayFromPos(const MONTHCAL_INFO *infoPtr, POINT pt, INT calIdx)
564 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
565 int firstDay, col, row;
566 RECT client;
568 GetClientRect(infoPtr->hwndSelf, &client);
570 /* if the point is outside the x bounds of the window put it at the boundary */
571 if (pt.x > client.right) pt.x = client.right;
573 col = (pt.x - infoPtr->calendars[calIdx].days.left ) / infoPtr->width_increment;
574 row = (pt.y - infoPtr->calendars[calIdx].days.top ) / infoPtr->height_increment;
576 st.wDay = 1;
577 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
578 return col + 7 * row - firstDay;
581 /* Get day position for given date and calendar
583 * PARAMETERS
585 * [I] infoPtr : pointer to control data
586 * [I] date : date value
587 * [O] col : day column (zero based)
588 * [O] row : week column (zero based)
589 * [I] calIdx : calendar index
591 static void MONTHCAL_GetDayPos(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
592 INT *col, INT *row, INT calIdx)
594 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
595 INT first;
597 st.wDay = 1;
598 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
600 if (calIdx == 0 || calIdx == MONTHCAL_GetCalCount(infoPtr)-1) {
601 const SYSTEMTIME *cal = &infoPtr->calendars[calIdx].month;
602 LONG cmp = MONTHCAL_CompareMonths(date, &st);
604 /* previous month */
605 if (cmp == -1) {
606 *col = (first - MONTHCAL_MonthLength(date->wMonth, cal->wYear) + date->wDay) % 7;
607 *row = 0;
608 return;
611 /* next month calculation is same as for current, just add current month length */
612 if (cmp == 1)
613 first += MONTHCAL_MonthLength(cal->wMonth, cal->wYear);
616 *col = (date->wDay + first) % 7;
617 *row = (date->wDay + first - *col) / 7;
620 /* returns bounding box for day in given position in given calendar */
621 static inline void MONTHCAL_GetDayRectI(const MONTHCAL_INFO *infoPtr, RECT *r,
622 INT col, INT row, INT calIdx)
624 r->left = infoPtr->calendars[calIdx].days.left + col * infoPtr->width_increment;
625 r->right = r->left + infoPtr->width_increment;
626 r->top = infoPtr->calendars[calIdx].days.top + row * infoPtr->height_increment;
627 r->bottom = r->top + infoPtr->textHeight;
630 /* Returns bounding box for given date
632 * NOTE: when calendar index is unknown pass -1
634 static BOOL MONTHCAL_GetDayRect(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date, RECT *r, INT calIdx)
636 INT col, row;
638 if (!MONTHCAL_ValidateDate(date))
640 SetRectEmpty(r);
641 return FALSE;
644 if (calIdx == -1)
646 INT cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[0].month);
648 if (cmp <= 0)
649 calIdx = 0;
650 else
652 cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month);
653 if (cmp >= 0)
654 calIdx = MONTHCAL_GetCalCount(infoPtr)-1;
655 else
657 for (calIdx = 1; calIdx < MONTHCAL_GetCalCount(infoPtr)-1; calIdx++)
658 if (MONTHCAL_CompareMonths(date, &infoPtr->calendars[calIdx].month) == 0)
659 break;
664 MONTHCAL_GetDayPos(infoPtr, date, &col, &row, calIdx);
665 MONTHCAL_GetDayRectI(infoPtr, r, col, row, calIdx);
667 return TRUE;
670 static LRESULT
671 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
673 INT range;
675 TRACE("flag=%d, st=%p\n", flag, st);
677 switch (flag) {
678 case GMR_VISIBLE:
680 if (st)
682 st[0] = infoPtr->calendars[0].month;
683 st[1] = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
685 if (st[0].wMonth == min_allowed_date.wMonth &&
686 st[0].wYear == min_allowed_date.wYear)
688 st[0].wDay = min_allowed_date.wDay;
690 else
691 st[0].wDay = 1;
692 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
694 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
695 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
698 range = MONTHCAL_GetCalCount(infoPtr);
699 break;
701 case GMR_DAYSTATE:
703 if (st)
705 MONTHCAL_GetMinDate(infoPtr, &st[0]);
706 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
708 /* include two partially visible months */
709 range = MONTHCAL_GetCalCount(infoPtr) + 2;
710 break;
712 default:
713 WARN("Unknown flag value, got %d\n", flag);
714 range = 0;
717 return range;
720 /* Focused day helper:
722 - set focused date to given value;
723 - reset to zero value if NULL passed;
724 - invalidate previous and new day rectangle only if needed.
726 Returns TRUE if focused day changed, FALSE otherwise.
728 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
730 RECT r;
732 if(st)
734 /* there's nothing to do if it's the same date,
735 mouse move within same date rectangle case */
736 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
738 /* invalidate old focused day */
739 if (MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1))
740 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
742 infoPtr->focusedSel = *st;
745 /* On set invalidates new day, on reset clears previous focused day. */
746 if (MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1))
747 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
749 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
750 infoPtr->focusedSel = st_null;
752 return TRUE;
755 /* draw today boundary box for specified rectangle */
756 static void MONTHCAL_Circle(const MONTHCAL_INFO *infoPtr, HDC hdc, const RECT *r)
758 HPEN old_pen = SelectObject(hdc, infoPtr->pens[PenRed]);
759 HBRUSH old_brush;
761 old_brush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
762 Rectangle(hdc, r->left, r->top, r->right, r->bottom);
764 SelectObject(hdc, old_brush);
765 SelectObject(hdc, old_pen);
768 /* Draw today day mark rectangle
770 * [I] hdc : context to draw in
771 * [I] date : day to mark with rectangle
774 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
775 const SYSTEMTIME *date)
777 RECT r;
779 MONTHCAL_GetDayRect(infoPtr, date, &r, -1);
780 MONTHCAL_Circle(infoPtr, hdc, &r);
783 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
784 int bold, const PAINTSTRUCT *ps)
786 static const WCHAR fmtW[] = { '%','d',0 };
787 WCHAR buf[10];
788 RECT r, r_temp;
789 COLORREF oldCol = 0;
790 COLORREF oldBk = 0;
791 INT old_bkmode, selection;
793 /* no need to check styles: when selection is not valid, it is set to zero.
794 1 < day < 31, so everything is OK */
795 MONTHCAL_GetDayRect(infoPtr, st, &r, -1);
796 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
798 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
799 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0))
801 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
802 TRACE("%s\n", wine_dbgstr_rect(&r));
803 oldCol = SetTextColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
804 oldBk = SetBkColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
805 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
807 selection = 1;
809 else
810 selection = 0;
812 SelectObject(hdc, bold ? infoPtr->hBoldFont : infoPtr->hFont);
814 old_bkmode = SetBkMode(hdc, TRANSPARENT);
815 wsprintfW(buf, fmtW, st->wDay);
816 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
817 SetBkMode(hdc, old_bkmode);
819 if (selection)
821 SetTextColor(hdc, oldCol);
822 SetBkColor(hdc, oldBk);
826 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, enum nav_direction button)
828 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
829 RECT *r = button == DIRECTION_FORWARD ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
830 BOOL pressed = button == DIRECTION_FORWARD ? infoPtr->status & MC_NEXTPRESSED :
831 infoPtr->status & MC_PREVPRESSED;
832 if (theme)
834 static const int states[] = {
835 /* Prev button */
836 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
837 /* Next button */
838 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
840 int stateNum = button == DIRECTION_FORWARD ? 3 : 0;
841 if (pressed)
842 stateNum += 1;
843 else
845 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
847 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
849 else
851 int style = button == DIRECTION_FORWARD ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
852 if (pressed)
853 style |= DFCS_PUSHED;
854 else
856 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
859 DrawFrameControl(hdc, r, DFC_SCROLL, style);
863 /* paint a title with buttons and month/year string */
864 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
866 static const WCHAR mmmmW[] = {'M','M','M','M',0};
867 static const WCHAR mmmW[] = {'M','M','M',0};
868 static const WCHAR mmW[] = {'M','M',0};
869 static const WCHAR fmtyearW[] = {'%','l','d',0};
870 static const WCHAR fmtmmW[] = {'%','0','2','d',0};
871 static const WCHAR fmtmW[] = {'%','d',0};
872 RECT *title = &infoPtr->calendars[calIdx].title;
873 const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
874 WCHAR monthW[80], strW[80], fmtW[80], yearW[6] /* valid year range is 1601-30827 */;
875 int yearoffset, monthoffset, shiftX;
876 SIZE sz;
878 /* fill header box */
879 FillRect(hdc, title, infoPtr->brushes[BrushTitle]);
881 /* month/year string */
882 SetBkColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
883 SetTextColor(hdc, infoPtr->colors[MCSC_TITLETEXT]);
884 SelectObject(hdc, infoPtr->hBoldFont);
886 /* draw formatted date string */
887 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_YEARMONTH, st, NULL, strW, ARRAY_SIZE(strW));
888 DrawTextW(hdc, strW, strlenW(strW), title, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
890 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SYEARMONTH, fmtW, ARRAY_SIZE(fmtW));
891 wsprintfW(yearW, fmtyearW, st->wYear);
893 /* month is trickier as it's possible to have different format pictures, we'll
894 test for M, MM, MMM, and MMMM */
895 if (strstrW(fmtW, mmmmW))
896 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+st->wMonth-1, monthW, ARRAY_SIZE(monthW));
897 else if (strstrW(fmtW, mmmW))
898 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME1+st->wMonth-1, monthW, ARRAY_SIZE(monthW));
899 else if (strstrW(fmtW, mmW))
900 wsprintfW(monthW, fmtmmW, st->wMonth);
901 else
902 wsprintfW(monthW, fmtmW, st->wMonth);
904 /* update hit boxes */
905 yearoffset = 0;
906 while (strW[yearoffset])
908 if (!strncmpW(&strW[yearoffset], yearW, strlenW(yearW)))
909 break;
910 yearoffset++;
913 monthoffset = 0;
914 while (strW[monthoffset])
916 if (!strncmpW(&strW[monthoffset], monthW, strlenW(monthW)))
917 break;
918 monthoffset++;
921 /* for left limits use offsets */
922 sz.cx = 0;
923 if (yearoffset)
924 GetTextExtentPoint32W(hdc, strW, yearoffset, &sz);
925 infoPtr->calendars[calIdx].titleyear.left = sz.cx;
927 sz.cx = 0;
928 if (monthoffset)
929 GetTextExtentPoint32W(hdc, strW, monthoffset, &sz);
930 infoPtr->calendars[calIdx].titlemonth.left = sz.cx;
932 /* for right limits use actual string parts lengths */
933 GetTextExtentPoint32W(hdc, &strW[yearoffset], strlenW(yearW), &sz);
934 infoPtr->calendars[calIdx].titleyear.right = infoPtr->calendars[calIdx].titleyear.left + sz.cx;
936 GetTextExtentPoint32W(hdc, monthW, strlenW(monthW), &sz);
937 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
939 /* Finally translate rectangles to match center aligned string,
940 hit rectangles are relative to title rectangle before translation. */
941 GetTextExtentPoint32W(hdc, strW, strlenW(strW), &sz);
942 shiftX = (title->right - title->left - sz.cx) / 2 + title->left;
943 OffsetRect(&infoPtr->calendars[calIdx].titleyear, shiftX, 0);
944 OffsetRect(&infoPtr->calendars[calIdx].titlemonth, shiftX, 0);
947 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
949 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
950 static const WCHAR fmt_weekW[] = { '%','d',0 };
951 INT mindays, weeknum, weeknum1, startofprescal;
952 INT i, prev_month;
953 SYSTEMTIME st;
954 WCHAR buf[80];
955 HPEN old_pen;
956 RECT r;
958 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
960 MONTHCAL_GetMinDate(infoPtr, &st);
961 startofprescal = st.wDay;
962 st = *date;
964 prev_month = date->wMonth - 1;
965 if(prev_month == 0) prev_month = 12;
968 Rules what week to call the first week of a new year:
969 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
970 The week containing Jan 1 is the first week of year
971 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
972 First week of year must contain 4 days of the new year
973 LOCALE_IFIRSTWEEKOFYEAR == 1 (what countries?)
974 The first week of the year must contain only days of the new year
976 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, ARRAY_SIZE(buf));
977 weeknum = atoiW(buf);
978 switch (weeknum)
980 case 1: mindays = 6;
981 break;
982 case 2: mindays = 3;
983 break;
984 case 0: mindays = 0;
985 break;
986 default:
987 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
988 mindays = 0;
991 if (date->wMonth == 1)
993 /* calculate all those exceptions for January */
994 st.wDay = st.wMonth = 1;
995 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
996 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
997 weeknum = 1;
998 else
1000 weeknum = 0;
1001 for(i = 0; i < 11; i++)
1002 weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
1004 weeknum += startofprescal + 7;
1005 weeknum /= 7;
1006 st.wYear -= 1;
1007 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
1008 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
1011 else
1013 weeknum = 0;
1014 for(i = 0; i < prev_month - 1; i++)
1015 weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
1017 weeknum += startofprescal + 7;
1018 weeknum /= 7;
1019 st.wDay = st.wMonth = 1;
1020 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
1021 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
1024 r = infoPtr->calendars[calIdx].weeknums;
1026 /* erase whole week numbers area */
1027 FillRect(hdc, &r, infoPtr->brushes[BrushMonth]);
1028 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1030 /* reduce rectangle to one week number */
1031 r.bottom = r.top + infoPtr->height_increment;
1033 for(i = 0; i < 6; i++) {
1034 if((i == 0) && (weeknum > 50))
1036 wsprintfW(buf, fmt_weekW, weeknum);
1037 weeknum = 0;
1039 else if((i == 5) && (weeknum > 47))
1041 wsprintfW(buf, fmt_weekW, 1);
1043 else
1044 wsprintfW(buf, fmt_weekW, weeknum + i);
1046 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1047 OffsetRect(&r, 0, infoPtr->height_increment);
1050 /* line separator for week numbers column */
1051 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1052 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
1053 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
1054 SelectObject(hdc, old_pen);
1057 /* bottom today date */
1058 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1060 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
1061 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
1062 RECT text_rect, box_rect;
1063 HFONT old_font;
1064 INT col;
1066 if(infoPtr->dwStyle & MCS_NOTODAY) return;
1068 LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, ARRAY_SIZE(buf_todayW));
1069 col = infoPtr->dwStyle & MCS_NOTODAYCIRCLE ? 0 : 1;
1070 if (infoPtr->dwStyle & MCS_WEEKNUMBERS) col--;
1071 /* label is located below first calendar last row */
1072 MONTHCAL_GetDayRectI(infoPtr, &text_rect, col, 6, infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx);
1073 box_rect = text_rect;
1075 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL, buf_dateW, ARRAY_SIZE(buf_dateW));
1076 old_font = SelectObject(hdc, infoPtr->hBoldFont);
1077 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1079 wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
1080 DrawTextW(hdc, buf, -1, &text_rect, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1081 DrawTextW(hdc, buf, -1, &text_rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1083 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
1084 OffsetRect(&box_rect, -infoPtr->width_increment, 0);
1085 MONTHCAL_Circle(infoPtr, hdc, &box_rect);
1088 SelectObject(hdc, old_font);
1091 /* today mark + focus */
1092 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1094 /* circle today date if only it's in fully visible month */
1095 if (!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
1097 INT i;
1099 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1100 if (!MONTHCAL_CompareMonths(&infoPtr->todaysDate, &infoPtr->calendars[i].month))
1102 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
1103 break;
1107 if (!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
1109 RECT r;
1110 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
1111 DrawFocusRect(hdc, &r);
1115 /* months before first calendar month and after last calendar month */
1116 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1118 INT mask, index;
1119 UINT length;
1120 SYSTEMTIME st_max, st;
1122 if (infoPtr->dwStyle & MCS_NOTRAILINGDATES) return;
1124 SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
1126 /* draw prev month */
1127 MONTHCAL_GetMinDate(infoPtr, &st);
1128 mask = 1 << (st.wDay-1);
1129 /* December and January both 31 days long, so no worries if wrapped */
1130 length = MONTHCAL_MonthLength(infoPtr->calendars[0].month.wMonth - 1,
1131 infoPtr->calendars[0].month.wYear);
1132 index = 0;
1133 while(st.wDay <= length)
1135 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1136 mask <<= 1;
1137 st.wDay++;
1140 /* draw next month */
1141 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1142 st.wDay = 1;
1143 MONTHCAL_GetNextMonth(&st);
1144 MONTHCAL_GetMaxDate(infoPtr, &st_max);
1145 mask = 1;
1146 index = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)-1;
1147 while(st.wDay <= st_max.wDay)
1149 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1150 mask <<= 1;
1151 st.wDay++;
1155 static int get_localized_dayname(const MONTHCAL_INFO *infoPtr, unsigned int day, WCHAR *buff, unsigned int count)
1157 LCTYPE lctype;
1159 if (infoPtr->dwStyle & MCS_SHORTDAYSOFWEEK)
1160 lctype = LOCALE_SSHORTESTDAYNAME1 + day;
1161 else
1162 lctype = LOCALE_SABBREVDAYNAME1 + day;
1164 return GetLocaleInfoW(LOCALE_USER_DEFAULT, lctype, buff, count);
1167 /* paint a calendar area */
1168 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
1170 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
1171 INT i, j;
1172 UINT length;
1173 RECT r, fill_bk_rect;
1174 SYSTEMTIME st;
1175 WCHAR buf[80];
1176 HPEN old_pen;
1177 int mask;
1179 /* fill whole days area - from week days area to today note rectangle */
1180 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
1181 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
1182 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
1184 FillRect(hdc, &fill_bk_rect, infoPtr->brushes[BrushMonth]);
1186 /* draw line under day abbreviations */
1187 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1188 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
1189 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
1190 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
1191 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
1192 SelectObject(hdc, old_pen);
1194 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
1195 infoPtr->calendars[calIdx].weeknums.right;
1197 /* draw day abbreviations */
1198 SelectObject(hdc, infoPtr->hFont);
1199 SetBkColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
1200 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1201 /* rectangle to draw a single day abbreviation within */
1202 r = infoPtr->calendars[calIdx].wdays;
1203 r.right = r.left + infoPtr->width_increment;
1205 i = infoPtr->firstDay;
1206 for(j = 0; j < 7; j++) {
1207 get_localized_dayname(infoPtr, (i + j + 6) % 7, buf, ARRAY_SIZE(buf));
1208 DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1209 OffsetRect(&r, infoPtr->width_increment, 0);
1212 /* draw current month */
1213 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1214 st = *date;
1215 st.wDay = 1;
1216 mask = 1;
1217 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1218 while(st.wDay <= length)
1220 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[calIdx+1] & mask, ps);
1221 mask <<= 1;
1222 st.wDay++;
1226 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1228 COLORREF old_text_clr, old_bk_clr;
1229 HFONT old_font;
1230 INT i;
1232 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1233 old_bk_clr = GetBkColor(hdc);
1234 old_font = GetCurrentObject(hdc, OBJ_FONT);
1236 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1238 RECT *title = &infoPtr->calendars[i].title;
1239 RECT r;
1241 /* draw title, redraw all its elements */
1242 if (IntersectRect(&r, &(ps->rcPaint), title))
1243 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1245 /* draw calendar area */
1246 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1247 if (IntersectRect(&r, &(ps->rcPaint), &r))
1248 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1250 /* week numbers */
1251 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1254 /* partially visible months */
1255 MONTHCAL_PaintLeadTrailMonths(infoPtr, hdc, ps);
1257 /* focus and today rectangle */
1258 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1260 /* today at the bottom left */
1261 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1263 /* navigation buttons */
1264 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_BACKWARD);
1265 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_FORWARD);
1267 /* restore context */
1268 SetBkColor(hdc, old_bk_clr);
1269 SelectObject(hdc, old_font);
1270 SetTextColor(hdc, old_text_clr);
1273 static LRESULT
1274 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, RECT *rect)
1276 TRACE("rect %p\n", rect);
1278 if(!rect) return FALSE;
1280 *rect = infoPtr->calendars[0].title;
1281 rect->bottom = infoPtr->calendars[0].days.bottom + infoPtr->todayrect.bottom -
1282 infoPtr->todayrect.top;
1284 AdjustWindowRect(rect, infoPtr->dwStyle, FALSE);
1286 /* minimal rectangle is zero based */
1287 OffsetRect(rect, -rect->left, -rect->top);
1289 TRACE("%s\n", wine_dbgstr_rect(rect));
1291 return TRUE;
1294 static COLORREF
1295 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, UINT index)
1297 TRACE("%p, %d\n", infoPtr, index);
1299 if (index > MCSC_TRAILINGTEXT) return -1;
1300 return infoPtr->colors[index];
1303 static LRESULT
1304 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, UINT index, COLORREF color)
1306 enum CachedBrush type;
1307 COLORREF prev;
1309 TRACE("%p, %d: color %08x\n", infoPtr, index, color);
1311 if (index > MCSC_TRAILINGTEXT) return -1;
1313 prev = infoPtr->colors[index];
1314 infoPtr->colors[index] = color;
1316 /* update cached brush */
1317 switch (index)
1319 case MCSC_BACKGROUND:
1320 type = BrushBackground;
1321 break;
1322 case MCSC_TITLEBK:
1323 type = BrushTitle;
1324 break;
1325 case MCSC_MONTHBK:
1326 type = BrushMonth;
1327 break;
1328 default:
1329 type = BrushLast;
1332 if (type != BrushLast)
1334 DeleteObject(infoPtr->brushes[type]);
1335 infoPtr->brushes[type] = CreateSolidBrush(color);
1338 /* update cached pen */
1339 if (index == MCSC_TEXT)
1341 DeleteObject(infoPtr->pens[PenText]);
1342 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[index]);
1345 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND);
1346 return prev;
1349 static LRESULT
1350 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1352 TRACE("\n");
1354 if(infoPtr->delta)
1355 return infoPtr->delta;
1357 return MONTHCAL_GetMonthRange(infoPtr, GMR_VISIBLE, NULL);
1361 static LRESULT
1362 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1364 INT prev = infoPtr->delta;
1366 TRACE("delta %d\n", delta);
1368 infoPtr->delta = delta;
1369 return prev;
1373 static inline LRESULT
1374 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1376 int day;
1378 /* convert from SYSTEMTIME to locale format */
1379 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1381 return MAKELONG(day, infoPtr->firstDaySet);
1385 /* Sets the first day of the week that will appear in the control
1388 * PARAMETERS:
1389 * [I] infoPtr : valid pointer to control data
1390 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1393 * RETURN VALUE:
1394 * Low word contains previous first day,
1395 * high word indicates was first day forced with this message before or is
1396 * locale defined (TRUE - was forced, FALSE - wasn't).
1398 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1399 * FIXME: we need more error checking here
1401 static LRESULT
1402 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1404 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1405 int new_day;
1407 TRACE("%d\n", day);
1409 if(day == -1)
1411 WCHAR buf[80];
1413 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, ARRAY_SIZE(buf));
1414 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1416 new_day = atoiW(buf);
1418 infoPtr->firstDaySet = FALSE;
1420 else if(day >= 7)
1422 new_day = 6; /* max first day allowed */
1423 infoPtr->firstDaySet = TRUE;
1425 else
1427 /* Native behaviour for that case is broken: invalid date number >31
1428 got displayed at (0,0) position, current month starts always from
1429 (1,0) position. Should be implemented here as well only if there's
1430 nothing else to do. */
1431 if (day < -1)
1432 FIXME("No bug compatibility for day=%d\n", day);
1434 new_day = day;
1435 infoPtr->firstDaySet = TRUE;
1438 /* convert from locale to SYSTEMTIME format */
1439 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1441 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1443 return prev;
1446 static LRESULT
1447 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1449 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1452 static LRESULT
1453 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1455 FILETIME ft_min, ft_max;
1457 TRACE("%x %p\n", limits, range);
1459 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1460 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1461 return FALSE;
1463 infoPtr->rangeValid = 0;
1464 infoPtr->minDate = infoPtr->maxDate = st_null;
1466 if (limits & GDTR_MIN)
1468 if (!MONTHCAL_ValidateTime(&range[0]))
1469 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1471 infoPtr->minDate = range[0];
1472 infoPtr->rangeValid |= GDTR_MIN;
1474 if (limits & GDTR_MAX)
1476 if (!MONTHCAL_ValidateTime(&range[1]))
1477 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1479 infoPtr->maxDate = range[1];
1480 infoPtr->rangeValid |= GDTR_MAX;
1483 /* Only one limit set - we are done */
1484 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1485 return TRUE;
1487 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1488 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1490 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1492 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1494 /* Native swaps limits only when both limits are being set. */
1495 SYSTEMTIME st_tmp = infoPtr->minDate;
1496 infoPtr->minDate = infoPtr->maxDate;
1497 infoPtr->maxDate = st_tmp;
1499 else
1501 /* reset the other limit */
1502 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1503 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1504 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1508 return TRUE;
1512 static LRESULT
1513 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1515 TRACE("%p\n", range);
1517 if (!range) return 0;
1519 range[1] = infoPtr->maxDate;
1520 range[0] = infoPtr->minDate;
1522 return infoPtr->rangeValid;
1526 static LRESULT
1527 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1529 TRACE("%p %d %p\n", infoPtr, months, states);
1531 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return 0;
1532 if (months != MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)) return 0;
1534 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1536 return 1;
1539 static LRESULT
1540 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1542 TRACE("%p\n", curSel);
1543 if(!curSel) return FALSE;
1544 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1546 *curSel = infoPtr->minSel;
1547 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1548 return TRUE;
1551 static LRESULT
1552 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1554 SYSTEMTIME prev = infoPtr->minSel, selection;
1555 INT diff;
1556 WORD day;
1558 TRACE("%p\n", curSel);
1559 if(!curSel) return FALSE;
1560 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1562 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1563 /* exit earlier if selection equals current */
1564 if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1566 selection = *curSel;
1567 selection.wHour = selection.wMinute = selection.wSecond = selection.wMilliseconds = 0;
1568 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1570 if(!MONTHCAL_IsDateInValidRange(infoPtr, &selection, FALSE)) return FALSE;
1572 /* scroll calendars only if we have to */
1573 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, curSel);
1574 if (diff <= 0)
1576 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, curSel);
1577 if (diff > 0) diff = 0;
1580 if (diff != 0)
1582 INT i;
1584 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1585 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1588 /* we need to store time part as it is */
1589 selection = *curSel;
1590 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1591 infoPtr->minSel = infoPtr->maxSel = selection;
1593 /* if selection is still in current month, reduce rectangle */
1594 day = prev.wDay;
1595 prev.wDay = curSel->wDay;
1596 if (MONTHCAL_IsDateEqual(&prev, curSel))
1598 RECT r_prev, r_new;
1600 prev.wDay = day;
1601 MONTHCAL_GetDayRect(infoPtr, &prev, &r_prev, -1);
1602 MONTHCAL_GetDayRect(infoPtr, curSel, &r_new, -1);
1604 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1605 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1607 else
1608 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1610 return TRUE;
1614 static LRESULT
1615 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1617 return infoPtr->maxSelCount;
1621 static LRESULT
1622 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1624 TRACE("%d\n", max);
1626 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1627 if(max <= 0) return FALSE;
1629 infoPtr->maxSelCount = max;
1631 return TRUE;
1635 static LRESULT
1636 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1638 TRACE("%p\n", range);
1640 if(!range) return FALSE;
1642 if(infoPtr->dwStyle & MCS_MULTISELECT)
1644 range[1] = infoPtr->maxSel;
1645 range[0] = infoPtr->minSel;
1646 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1647 return TRUE;
1650 return FALSE;
1654 static LRESULT
1655 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1657 SYSTEMTIME old_range[2];
1658 INT diff;
1660 TRACE("%p\n", range);
1662 if(!range || !(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1664 /* adjust timestamps */
1665 if(!MONTHCAL_ValidateTime(&range[0])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1666 if(!MONTHCAL_ValidateTime(&range[1])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1668 /* maximum range exceeded */
1669 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1671 old_range[0] = infoPtr->minSel;
1672 old_range[1] = infoPtr->maxSel;
1674 /* swap if min > max */
1675 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1677 infoPtr->minSel = range[0];
1678 infoPtr->maxSel = range[1];
1680 else
1682 infoPtr->minSel = range[1];
1683 infoPtr->maxSel = range[0];
1686 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, &infoPtr->maxSel);
1687 if (diff < 0)
1689 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, &infoPtr->maxSel);
1690 if (diff > 0) diff = 0;
1693 if (diff != 0)
1695 INT i;
1697 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1698 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1701 /* update day of week */
1702 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1703 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1705 /* redraw if bounds changed */
1706 /* FIXME: no actual need to redraw everything */
1707 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1708 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1710 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1713 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1714 return TRUE;
1718 static LRESULT
1719 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1721 TRACE("%p\n", today);
1723 if(!today) return FALSE;
1724 *today = infoPtr->todaysDate;
1725 return TRUE;
1728 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1730 * RETURN VALUE
1732 * TRUE - today date changed
1733 * FALSE - today date isn't changed
1735 static BOOL
1736 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1738 RECT rect;
1740 if (MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate))
1741 return FALSE;
1743 /* Invalidate old and new today day rectangle, and today label. */
1744 if (MONTHCAL_GetDayRect(infoPtr, &infoPtr->todaysDate, &rect, -1))
1745 InvalidateRect(infoPtr->hwndSelf, &rect, FALSE);
1747 if (MONTHCAL_GetDayRect(infoPtr, today, &rect, -1))
1748 InvalidateRect(infoPtr->hwndSelf, &rect, FALSE);
1750 infoPtr->todaysDate = *today;
1752 InvalidateRect(infoPtr->hwndSelf, &infoPtr->todayrect, FALSE);
1753 return TRUE;
1756 /* MCM_SETTODAT handler */
1757 static LRESULT
1758 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1760 TRACE("%p\n", today);
1762 if (today)
1764 /* remember if date was set successfully */
1765 if (MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1768 return 0;
1771 /* returns calendar index containing specified point, or -1 if it's background */
1772 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1774 RECT r;
1775 INT i;
1777 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1779 /* whole bounding rectangle allows some optimization to compute */
1780 r.left = infoPtr->calendars[i].title.left;
1781 r.top = infoPtr->calendars[i].title.top;
1782 r.bottom = infoPtr->calendars[i].days.bottom;
1783 r.right = infoPtr->calendars[i].days.right;
1785 if (PtInRect(&r, *pt)) return i;
1788 return -1;
1791 static inline UINT fill_hittest_info(const MCHITTESTINFO *src, MCHITTESTINFO *dest)
1793 dest->uHit = src->uHit;
1794 dest->st = src->st;
1796 if (dest->cbSize == sizeof(MCHITTESTINFO))
1797 memcpy(&dest->rc, &src->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1799 return src->uHit;
1802 static LRESULT
1803 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1805 MCHITTESTINFO htinfo;
1806 SYSTEMTIME *ht_month;
1807 INT day, calIdx;
1809 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1811 htinfo.st = st_null;
1813 /* we should preserve passed fields if hit area doesn't need them */
1814 if (lpht->cbSize == sizeof(MCHITTESTINFO))
1815 memcpy(&htinfo.rc, &lpht->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1817 /* guess in what calendar we are */
1818 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1819 if (calIdx == -1)
1821 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1823 htinfo.uHit = MCHT_TODAYLINK;
1824 htinfo.rc = infoPtr->todayrect;
1826 else
1827 /* outside of calendar area? What's left must be background :-) */
1828 htinfo.uHit = MCHT_CALENDARBK;
1830 return fill_hittest_info(&htinfo, lpht);
1833 /* are we in the header? */
1834 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1835 /* FIXME: buttons hittesting could be optimized cause maximum
1836 two calendars have buttons */
1837 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1839 htinfo.uHit = MCHT_TITLEBTNPREV;
1840 htinfo.rc = infoPtr->titlebtnprev;
1842 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1844 htinfo.uHit = MCHT_TITLEBTNNEXT;
1845 htinfo.rc = infoPtr->titlebtnnext;
1847 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1849 htinfo.uHit = MCHT_TITLEMONTH;
1850 htinfo.rc = infoPtr->calendars[calIdx].titlemonth;
1851 htinfo.iOffset = calIdx;
1853 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1855 htinfo.uHit = MCHT_TITLEYEAR;
1856 htinfo.rc = infoPtr->calendars[calIdx].titleyear;
1857 htinfo.iOffset = calIdx;
1859 else
1861 htinfo.uHit = MCHT_TITLE;
1862 htinfo.rc = infoPtr->calendars[calIdx].title;
1863 htinfo.iOffset = calIdx;
1866 return fill_hittest_info(&htinfo, lpht);
1869 ht_month = &infoPtr->calendars[calIdx].month;
1870 /* days area (including week days and week numbers) */
1871 day = MONTHCAL_GetDayFromPos(infoPtr, lpht->pt, calIdx);
1872 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1874 htinfo.uHit = MCHT_CALENDARDAY;
1875 htinfo.iOffset = calIdx;
1876 htinfo.st.wYear = ht_month->wYear;
1877 htinfo.st.wMonth = (day < 1) ? ht_month->wMonth -1 : ht_month->wMonth;
1878 htinfo.st.wDay = (day < 1) ?
1879 MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day : day;
1881 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1883 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1885 htinfo.uHit = MCHT_CALENDARWEEKNUM;
1886 htinfo.st.wYear = ht_month->wYear;
1887 htinfo.iOffset = calIdx;
1889 if (day < 1)
1891 htinfo.st.wMonth = ht_month->wMonth - 1;
1892 htinfo.st.wDay = MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day;
1894 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1896 htinfo.st.wMonth = ht_month->wMonth + 1;
1897 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1899 else
1901 htinfo.st.wMonth = ht_month->wMonth;
1902 htinfo.st.wDay = day;
1905 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1907 htinfo.iOffset = calIdx;
1908 htinfo.st.wDay = ht_month->wDay;
1909 htinfo.st.wYear = ht_month->wYear;
1910 htinfo.st.wMonth = ht_month->wMonth;
1911 /* previous month only valid for first calendar */
1912 if (day < 1 && calIdx == 0)
1914 htinfo.uHit = MCHT_CALENDARDATEPREV;
1915 MONTHCAL_GetPrevMonth(&htinfo.st);
1916 htinfo.st.wDay = MONTHCAL_MonthLength(htinfo.st.wMonth, htinfo.st.wYear) + day;
1918 /* next month only valid for last calendar */
1919 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear) &&
1920 calIdx == MONTHCAL_GetCalCount(infoPtr)-1)
1922 htinfo.uHit = MCHT_CALENDARDATENEXT;
1923 MONTHCAL_GetNextMonth(&htinfo.st);
1924 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1926 /* multiple calendars case - blank areas for previous/next month */
1927 else if (day < 1 || day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1929 htinfo.uHit = MCHT_CALENDARBK;
1931 else
1933 htinfo.uHit = MCHT_CALENDARDATE;
1934 htinfo.st.wDay = day;
1937 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1938 MONTHCAL_GetDayRectI(infoPtr, &htinfo.rc, htinfo.iCol, htinfo.iRow, calIdx);
1939 /* always update day of week */
1940 MONTHCAL_CalculateDayOfWeek(&htinfo.st, TRUE);
1943 return fill_hittest_info(&htinfo, lpht);
1946 /* MCN_GETDAYSTATE notification helper */
1947 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1949 MONTHDAYSTATE *state;
1950 NMDAYSTATE nmds;
1952 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return;
1954 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1955 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1956 nmds.nmhdr.code = MCN_GETDAYSTATE;
1957 nmds.cDayState = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0);
1958 nmds.prgDayState = state = heap_alloc_zero(nmds.cDayState * sizeof(MONTHDAYSTATE));
1960 MONTHCAL_GetMinDate(infoPtr, &nmds.stStart);
1961 nmds.stStart.wDay = 1;
1963 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1964 memcpy(infoPtr->monthdayState, nmds.prgDayState,
1965 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
1967 heap_free(state);
1970 /* no valid range check performed */
1971 static void MONTHCAL_Scroll(MONTHCAL_INFO *infoPtr, INT delta, BOOL keep_selection)
1973 INT i, selIdx = -1;
1975 for(i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1977 /* save selection position to shift it later */
1978 if (selIdx == -1 && MONTHCAL_CompareMonths(&infoPtr->minSel, &infoPtr->calendars[i].month) == 0)
1979 selIdx = i;
1981 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, delta);
1984 if (keep_selection)
1985 return;
1987 /* selection is always shifted to first calendar */
1988 if (infoPtr->dwStyle & MCS_MULTISELECT)
1990 SYSTEMTIME range[2];
1992 MONTHCAL_GetSelRange(infoPtr, range);
1993 MONTHCAL_GetMonth(&range[0], delta - selIdx);
1994 MONTHCAL_GetMonth(&range[1], delta - selIdx);
1995 MONTHCAL_SetSelRange(infoPtr, range);
1997 else
1999 SYSTEMTIME st = infoPtr->minSel;
2001 MONTHCAL_GetMonth(&st, delta - selIdx);
2002 MONTHCAL_SetCurSel(infoPtr, &st);
2006 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
2008 INT delta = infoPtr->delta ? infoPtr->delta : MONTHCAL_GetCalCount(infoPtr);
2009 BOOL keep_selection;
2010 SYSTEMTIME st;
2012 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
2014 /* check if change allowed by range set */
2015 if(direction == DIRECTION_BACKWARD)
2017 st = infoPtr->calendars[0].month;
2018 MONTHCAL_GetMonth(&st, -delta);
2020 else
2022 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2023 MONTHCAL_GetMonth(&st, delta);
2026 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
2028 keep_selection = infoPtr->dwStyle & MCS_NOSELCHANGEONNAV;
2029 MONTHCAL_Scroll(infoPtr, direction == DIRECTION_BACKWARD ? -delta : delta, keep_selection);
2030 MONTHCAL_NotifyDayState(infoPtr);
2031 if (!keep_selection)
2032 MONTHCAL_NotifySelectionChange(infoPtr);
2035 static LRESULT
2036 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2038 HMENU hMenu;
2039 POINT menupoint;
2040 WCHAR buf[32];
2042 hMenu = CreatePopupMenu();
2043 LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, ARRAY_SIZE(buf));
2044 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
2045 menupoint.x = (short)LOWORD(lParam);
2046 menupoint.y = (short)HIWORD(lParam);
2047 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2048 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
2049 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
2051 if (infoPtr->dwStyle & MCS_MULTISELECT)
2053 SYSTEMTIME range[2];
2055 range[0] = range[1] = infoPtr->todaysDate;
2056 MONTHCAL_SetSelRange(infoPtr, range);
2058 else
2059 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2061 MONTHCAL_NotifySelectionChange(infoPtr);
2062 MONTHCAL_NotifySelect(infoPtr);
2065 return 0;
2068 /***
2069 * DESCRIPTION:
2070 * Subclassed edit control windproc function
2072 * PARAMETER(S):
2073 * [I] hwnd : the edit window handle
2074 * [I] uMsg : the message that is to be processed
2075 * [I] wParam : first message parameter
2076 * [I] lParam : second message parameter
2079 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2081 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
2083 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
2084 hwnd, uMsg, wParam, lParam);
2086 switch (uMsg)
2088 case WM_GETDLGCODE:
2089 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
2091 case WM_DESTROY:
2093 WNDPROC editProc = infoPtr->EditWndProc;
2094 infoPtr->EditWndProc = NULL;
2095 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
2096 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
2099 case WM_KILLFOCUS:
2100 break;
2102 case WM_KEYDOWN:
2103 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
2104 break;
2106 default:
2107 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
2110 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
2111 SendMessageW(hwnd, WM_CLOSE, 0, 0);
2112 return 0;
2115 /* creates updown control and edit box */
2116 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr, INT calIdx)
2118 RECT *rc = &infoPtr->calendars[calIdx].titleyear;
2119 RECT *title = &infoPtr->calendars[calIdx].title;
2121 infoPtr->hWndYearEdit =
2122 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
2123 rc->left + 3, (title->bottom + title->top - infoPtr->textHeight) / 2,
2124 rc->right - rc->left + 4,
2125 infoPtr->textHeight, infoPtr->hwndSelf,
2126 NULL, NULL, NULL);
2128 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
2130 infoPtr->hWndYearUpDown =
2131 CreateWindowExW(0, UPDOWN_CLASSW, 0,
2132 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
2133 rc->right + 7, (title->bottom + title->top - infoPtr->textHeight) / 2,
2134 18, infoPtr->textHeight, infoPtr->hwndSelf,
2135 NULL, NULL, NULL);
2137 /* attach edit box */
2138 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
2139 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
2140 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
2141 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->calendars[calIdx].month.wYear);
2143 /* subclass edit box */
2144 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
2145 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
2147 SetFocus(infoPtr->hWndYearEdit);
2150 static LRESULT
2151 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2153 MCHITTESTINFO ht;
2154 DWORD hit;
2156 /* Actually we don't need input focus for calendar, this is used to kill
2157 year updown and its buddy edit box */
2158 if (IsWindow(infoPtr->hWndYearUpDown))
2160 SetFocus(infoPtr->hwndSelf);
2161 return 0;
2164 SetCapture(infoPtr->hwndSelf);
2166 ht.cbSize = sizeof(MCHITTESTINFO);
2167 ht.pt.x = (short)LOWORD(lParam);
2168 ht.pt.y = (short)HIWORD(lParam);
2170 hit = MONTHCAL_HitTest(infoPtr, &ht);
2172 TRACE("%x at %s\n", hit, wine_dbgstr_point(&ht.pt));
2174 switch(hit)
2176 case MCHT_TITLEBTNNEXT:
2177 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2178 infoPtr->status = MC_NEXTPRESSED;
2179 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2180 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2181 return 0;
2183 case MCHT_TITLEBTNPREV:
2184 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2185 infoPtr->status = MC_PREVPRESSED;
2186 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2187 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2188 return 0;
2190 case MCHT_TITLEMONTH:
2192 HMENU hMenu = CreatePopupMenu();
2193 WCHAR buf[32];
2194 POINT menupoint;
2195 INT i;
2197 for (i = 0; i < 12; i++)
2199 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, ARRAY_SIZE(buf));
2200 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
2202 menupoint.x = ht.pt.x;
2203 menupoint.y = ht.pt.y;
2204 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2205 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
2206 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
2208 if ((i > 0) && (i < 13) && infoPtr->calendars[ht.iOffset].month.wMonth != i)
2210 INT delta = i - infoPtr->calendars[ht.iOffset].month.wMonth;
2211 SYSTEMTIME st;
2213 /* check if change allowed by range set */
2214 st = delta < 0 ? infoPtr->calendars[0].month :
2215 infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2216 MONTHCAL_GetMonth(&st, delta);
2218 if (MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE))
2220 MONTHCAL_Scroll(infoPtr, delta, FALSE);
2221 MONTHCAL_NotifyDayState(infoPtr);
2222 MONTHCAL_NotifySelectionChange(infoPtr);
2223 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2226 return 0;
2228 case MCHT_TITLEYEAR:
2230 MONTHCAL_EditYear(infoPtr, ht.iOffset);
2231 return 0;
2233 case MCHT_TODAYLINK:
2235 if (infoPtr->dwStyle & MCS_MULTISELECT)
2237 SYSTEMTIME range[2];
2239 range[0] = range[1] = infoPtr->todaysDate;
2240 MONTHCAL_SetSelRange(infoPtr, range);
2242 else
2243 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2245 MONTHCAL_NotifySelectionChange(infoPtr);
2246 MONTHCAL_NotifySelect(infoPtr);
2247 return 0;
2249 case MCHT_CALENDARDATENEXT:
2250 case MCHT_CALENDARDATEPREV:
2251 case MCHT_CALENDARDATE:
2253 SYSTEMTIME st[2];
2255 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2257 st[0] = st[1] = ht.st;
2258 /* clear selection range */
2259 MONTHCAL_SetSelRange(infoPtr, st);
2261 infoPtr->status = MC_SEL_LBUTDOWN;
2262 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
2263 return 0;
2267 return 1;
2271 static LRESULT
2272 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2274 NMHDR nmhdr;
2275 MCHITTESTINFO ht;
2276 DWORD hit;
2278 TRACE("\n");
2280 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2281 RECT *r;
2283 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2284 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2285 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2287 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2290 ReleaseCapture();
2292 /* always send NM_RELEASEDCAPTURE notification */
2293 nmhdr.hwndFrom = infoPtr->hwndSelf;
2294 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2295 nmhdr.code = NM_RELEASEDCAPTURE;
2296 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2298 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2300 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2302 ht.cbSize = sizeof(MCHITTESTINFO);
2303 ht.pt.x = (short)LOWORD(lParam);
2304 ht.pt.y = (short)HIWORD(lParam);
2305 hit = MONTHCAL_HitTest(infoPtr, &ht);
2307 infoPtr->status = MC_SEL_LBUTUP;
2308 MONTHCAL_SetDayFocus(infoPtr, NULL);
2310 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2312 SYSTEMTIME sel = infoPtr->minSel;
2314 /* will be invalidated here */
2315 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2317 /* send MCN_SELCHANGE only if new date selected */
2318 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2319 MONTHCAL_NotifySelectionChange(infoPtr);
2321 MONTHCAL_NotifySelect(infoPtr);
2324 return 0;
2328 static LRESULT
2329 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2331 TRACE("%ld\n", id);
2333 switch(id) {
2334 case MC_PREVNEXTMONTHTIMER:
2335 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2336 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2337 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2338 break;
2339 case MC_TODAYUPDATETIMER:
2341 SYSTEMTIME st;
2343 if(infoPtr->todaySet) return 0;
2345 GetLocalTime(&st);
2346 MONTHCAL_UpdateToday(infoPtr, &st);
2348 /* notification sent anyway */
2349 MONTHCAL_NotifySelectionChange(infoPtr);
2351 return 0;
2353 default:
2354 ERR("got unknown timer %ld\n", id);
2355 break;
2358 return 0;
2362 static LRESULT
2363 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2365 MCHITTESTINFO ht;
2366 SYSTEMTIME st_ht;
2367 INT hit;
2368 RECT r;
2370 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2372 ht.cbSize = sizeof(MCHITTESTINFO);
2373 ht.pt.x = (short)LOWORD(lParam);
2374 ht.pt.y = (short)HIWORD(lParam);
2375 ht.iOffset = -1;
2377 hit = MONTHCAL_HitTest(infoPtr, &ht);
2379 /* not on the calendar date numbers? bail out */
2380 TRACE("hit:%x\n",hit);
2381 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2383 MONTHCAL_SetDayFocus(infoPtr, NULL);
2384 return 0;
2387 st_ht = ht.st;
2389 /* if pointer is over focused day still there's nothing to do */
2390 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2392 MONTHCAL_GetDayRect(infoPtr, &ht.st, &r, ht.iOffset);
2394 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2395 SYSTEMTIME st[2];
2397 MONTHCAL_GetSelRange(infoPtr, st);
2399 /* If we're still at the first selected date and range is empty, return.
2400 If range isn't empty we should change range to a single firstSel */
2401 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2402 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2404 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2406 st[0] = infoPtr->firstSel;
2407 /* we should overwrite timestamp here */
2408 MONTHCAL_CopyDate(&st_ht, &st[1]);
2410 /* bounds will be swapped here if needed */
2411 MONTHCAL_SetSelRange(infoPtr, st);
2413 return 0;
2416 done:
2418 /* FIXME: this should specify a rectangle containing only the days that changed
2419 using InvalidateRect */
2420 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2422 return 0;
2426 static LRESULT
2427 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2429 HDC hdc;
2430 PAINTSTRUCT ps;
2432 if (hdc_paint)
2434 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2435 hdc = hdc_paint;
2437 else
2438 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2440 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2441 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2442 return 0;
2445 static LRESULT
2446 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2448 RECT rc;
2450 if (!GetClipBox(hdc, &rc)) return FALSE;
2452 FillRect(hdc, &rc, infoPtr->brushes[BrushBackground]);
2454 return TRUE;
2457 static LRESULT
2458 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2460 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2462 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2463 return 0;
2465 if (options & PRF_ERASEBKGND)
2466 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2468 if (options & PRF_CLIENT)
2469 MONTHCAL_Paint(infoPtr, hdc);
2471 return 0;
2474 static LRESULT
2475 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2477 TRACE("\n");
2479 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2481 return 0;
2484 /* sets the size information */
2485 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2487 static const WCHAR O0W[] = { '0','0',0 };
2488 RECT *title=&infoPtr->calendars[0].title;
2489 RECT *prev=&infoPtr->titlebtnprev;
2490 RECT *next=&infoPtr->titlebtnnext;
2491 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2492 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2493 RECT *wdays=&infoPtr->calendars[0].wdays;
2494 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2495 RECT *days=&infoPtr->calendars[0].days;
2496 RECT *todayrect=&infoPtr->todayrect;
2498 INT xdiv, dx, dy, i, j, x, y, c_dx, c_dy;
2499 WCHAR buff[80];
2500 TEXTMETRICW tm;
2501 INT day_width;
2502 RECT client;
2503 HFONT font;
2504 SIZE size;
2505 HDC hdc;
2507 GetClientRect(infoPtr->hwndSelf, &client);
2509 hdc = GetDC(infoPtr->hwndSelf);
2510 font = SelectObject(hdc, infoPtr->hFont);
2512 /* get the height and width of each day's text */
2513 GetTextMetricsW(hdc, &tm);
2514 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2516 /* find widest day name for current locale and font */
2517 day_width = 0;
2518 for (i = 0; i < 7; i++)
2520 SIZE sz;
2522 if (get_localized_dayname(infoPtr, i, buff, ARRAY_SIZE(buff)))
2524 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2525 if (sz.cx > day_width) day_width = sz.cx;
2527 else /* locale independent fallback on failure */
2529 static const WCHAR sunW[] = { 'S','u','n' };
2530 GetTextExtentPoint32W(hdc, sunW, ARRAY_SIZE(sunW), &sz);
2531 day_width = sz.cx;
2532 break;
2536 day_width += 2;
2538 /* recalculate the height and width increments and offsets */
2539 size.cx = 0;
2540 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2542 /* restore the originally selected font */
2543 SelectObject(hdc, font);
2544 ReleaseDC(infoPtr->hwndSelf, hdc);
2546 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2548 infoPtr->width_increment = max(day_width, size.cx * 2 + 4);
2549 infoPtr->height_increment = infoPtr->textHeight;
2551 /* calculate title area */
2552 title->top = 0;
2553 title->bottom = 3 * infoPtr->height_increment / 2;
2554 title->left = 0;
2555 title->right = infoPtr->width_increment * xdiv;
2557 /* set the dimensions of the next and previous buttons and center */
2558 /* the month text vertically */
2559 prev->top = next->top = title->top + 4;
2560 prev->bottom = next->bottom = title->bottom - 4;
2561 prev->left = title->left + 4;
2562 prev->right = prev->left + (title->bottom - title->top);
2563 next->right = title->right - 4;
2564 next->left = next->right - (title->bottom - title->top);
2566 /* titlemonth->left and right change based upon the current month
2567 and are recalculated in refresh as the current month may change
2568 without the control being resized */
2569 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2570 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2572 /* week numbers */
2573 weeknumrect->left = 0;
2574 weeknumrect->right = infoPtr->dwStyle & MCS_WEEKNUMBERS ? prev->right : 0;
2576 /* days abbreviated names */
2577 wdays->left = days->left = weeknumrect->right;
2578 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2579 wdays->top = title->bottom;
2580 wdays->bottom = wdays->top + infoPtr->height_increment;
2582 days->top = weeknumrect->top = wdays->bottom;
2583 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2585 todayrect->left = 0;
2586 todayrect->right = title->right;
2587 todayrect->top = days->bottom;
2588 todayrect->bottom = days->bottom + infoPtr->height_increment;
2590 /* compute calendar count, update all calendars */
2591 x = (client.right + MC_CALENDAR_PADDING) / (title->right - title->left + MC_CALENDAR_PADDING);
2592 /* today label affects whole height */
2593 if (infoPtr->dwStyle & MCS_NOTODAY)
2594 y = (client.bottom + MC_CALENDAR_PADDING) / (days->bottom - title->top + MC_CALENDAR_PADDING);
2595 else
2596 y = (client.bottom - todayrect->bottom + todayrect->top + MC_CALENDAR_PADDING) /
2597 (days->bottom - title->top + MC_CALENDAR_PADDING);
2599 /* TODO: ensure that count is properly adjusted to fit 12 months constraint */
2600 if (x == 0) x = 1;
2601 if (y == 0) y = 1;
2603 if (x*y != MONTHCAL_GetCalCount(infoPtr))
2605 infoPtr->dim.cx = x;
2606 infoPtr->dim.cy = y;
2607 infoPtr->calendars = heap_realloc(infoPtr->calendars, MONTHCAL_GetCalCount(infoPtr)*sizeof(CALENDAR_INFO));
2609 infoPtr->monthdayState = heap_realloc(infoPtr->monthdayState,
2610 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
2611 MONTHCAL_NotifyDayState(infoPtr);
2613 /* update pointers that we'll need */
2614 title = &infoPtr->calendars[0].title;
2615 wdays = &infoPtr->calendars[0].wdays;
2616 days = &infoPtr->calendars[0].days;
2619 for (i = 1; i < MONTHCAL_GetCalCount(infoPtr); i++)
2621 /* set months */
2622 infoPtr->calendars[i] = infoPtr->calendars[0];
2623 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, i);
2626 /* offset all rectangles to center in client area */
2627 c_dx = (client.right - x * title->right - MC_CALENDAR_PADDING * (x-1)) / 2;
2628 c_dy = (client.bottom - y * todayrect->bottom - MC_CALENDAR_PADDING * (y-1)) / 2;
2630 /* if calendar doesn't fit client area show it at left/top bounds */
2631 if (title->left + c_dx < 0) c_dx = 0;
2632 if (title->top + c_dy < 0) c_dy = 0;
2634 for (i = 0; i < y; i++)
2636 for (j = 0; j < x; j++)
2638 dx = j*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx;
2639 dy = i*(days->bottom - title->top + MC_CALENDAR_PADDING) + c_dy;
2641 OffsetRect(&infoPtr->calendars[i*x+j].title, dx, dy);
2642 OffsetRect(&infoPtr->calendars[i*x+j].titlemonth, dx, dy);
2643 OffsetRect(&infoPtr->calendars[i*x+j].titleyear, dx, dy);
2644 OffsetRect(&infoPtr->calendars[i*x+j].wdays, dx, dy);
2645 OffsetRect(&infoPtr->calendars[i*x+j].weeknums, dx, dy);
2646 OffsetRect(&infoPtr->calendars[i*x+j].days, dx, dy);
2650 OffsetRect(prev, c_dx, c_dy);
2651 OffsetRect(next, (x-1)*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx, c_dy);
2653 i = infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx;
2654 todayrect->left = infoPtr->calendars[i].title.left;
2655 todayrect->right = infoPtr->calendars[i].title.right;
2656 todayrect->top = infoPtr->calendars[i].days.bottom;
2657 todayrect->bottom = infoPtr->calendars[i].days.bottom + infoPtr->height_increment;
2659 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2660 infoPtr->width_increment,infoPtr->height_increment,
2661 wine_dbgstr_rect(&client),
2662 wine_dbgstr_rect(title),
2663 wine_dbgstr_rect(wdays),
2664 wine_dbgstr_rect(days),
2665 wine_dbgstr_rect(todayrect));
2668 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2670 TRACE("(width=%d, height=%d)\n", Width, Height);
2672 MONTHCAL_UpdateSize(infoPtr);
2673 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2675 return 0;
2678 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2680 return (LRESULT)infoPtr->hFont;
2683 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2685 HFONT hOldFont;
2686 LOGFONTW lf;
2688 if (!hFont) return 0;
2690 hOldFont = infoPtr->hFont;
2691 infoPtr->hFont = hFont;
2693 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2694 lf.lfWeight = FW_BOLD;
2695 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2697 MONTHCAL_UpdateSize(infoPtr);
2699 if (redraw)
2700 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2702 return (LRESULT)hOldFont;
2705 /* update theme after a WM_THEMECHANGED message */
2706 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2708 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2709 CloseThemeData (theme);
2710 OpenThemeData (infoPtr->hwndSelf, themeClass);
2711 return 0;
2714 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2715 const STYLESTRUCT *lpss)
2717 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2718 wStyleType, lpss->styleOld, lpss->styleNew);
2720 if (wStyleType != GWL_STYLE) return 0;
2722 infoPtr->dwStyle = lpss->styleNew;
2724 /* make room for week numbers */
2725 if ((lpss->styleNew ^ lpss->styleOld) & (MCS_WEEKNUMBERS | MCS_SHORTDAYSOFWEEK))
2726 MONTHCAL_UpdateSize(infoPtr);
2728 return 0;
2731 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2732 STYLESTRUCT *lpss)
2734 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2735 wStyleType, lpss->styleOld, lpss->styleNew);
2737 /* block MCS_MULTISELECT change */
2738 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2740 if (lpss->styleOld & MCS_MULTISELECT)
2741 lpss->styleNew |= MCS_MULTISELECT;
2742 else
2743 lpss->styleNew &= ~MCS_MULTISELECT;
2746 /* block MCS_DAYSTATE change */
2747 if ((lpss->styleNew ^ lpss->styleOld) & MCS_DAYSTATE)
2749 if (lpss->styleOld & MCS_DAYSTATE)
2750 lpss->styleNew |= MCS_DAYSTATE;
2751 else
2752 lpss->styleNew &= ~MCS_DAYSTATE;
2755 return 0;
2758 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2759 static LRESULT
2760 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2762 MONTHCAL_INFO *infoPtr;
2764 /* allocate memory for info structure */
2765 infoPtr = heap_alloc_zero(sizeof(*infoPtr));
2766 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2768 if (infoPtr == NULL) {
2769 ERR("could not allocate info memory!\n");
2770 return 0;
2773 infoPtr->hwndSelf = hwnd;
2774 infoPtr->hwndNotify = lpcs->hwndParent;
2775 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2776 infoPtr->dim.cx = infoPtr->dim.cy = 1;
2777 infoPtr->calendars = heap_alloc_zero(sizeof(CALENDAR_INFO));
2778 if (!infoPtr->calendars) goto fail;
2779 infoPtr->monthdayState = heap_alloc_zero(3 * sizeof(MONTHDAYSTATE));
2780 if (!infoPtr->monthdayState) goto fail;
2782 /* initialize info structure */
2783 /* FIXME: calculate systemtime ->> localtime(subtract timezoneinfo) */
2785 GetLocalTime(&infoPtr->todaysDate);
2786 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2788 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2790 infoPtr->colors[MCSC_BACKGROUND] = comctl32_color.clrWindow;
2791 infoPtr->colors[MCSC_TEXT] = comctl32_color.clrWindowText;
2792 infoPtr->colors[MCSC_TITLEBK] = comctl32_color.clrActiveCaption;
2793 infoPtr->colors[MCSC_TITLETEXT] = comctl32_color.clrWindow;
2794 infoPtr->colors[MCSC_MONTHBK] = comctl32_color.clrWindow;
2795 infoPtr->colors[MCSC_TRAILINGTEXT] = comctl32_color.clrGrayText;
2797 infoPtr->brushes[BrushBackground] = CreateSolidBrush(infoPtr->colors[MCSC_BACKGROUND]);
2798 infoPtr->brushes[BrushTitle] = CreateSolidBrush(infoPtr->colors[MCSC_TITLEBK]);
2799 infoPtr->brushes[BrushMonth] = CreateSolidBrush(infoPtr->colors[MCSC_MONTHBK]);
2801 infoPtr->pens[PenRed] = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
2802 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[MCSC_TEXT]);
2804 infoPtr->minSel = infoPtr->todaysDate;
2805 infoPtr->maxSel = infoPtr->todaysDate;
2806 infoPtr->calendars[0].month = infoPtr->todaysDate;
2807 infoPtr->isUnicode = TRUE;
2809 /* setup control layout and day state data */
2810 MONTHCAL_UpdateSize(infoPtr);
2812 /* today auto update timer, to be freed only on control destruction */
2813 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2815 OpenThemeData (infoPtr->hwndSelf, themeClass);
2817 return 0;
2819 fail:
2820 heap_free(infoPtr->monthdayState);
2821 heap_free(infoPtr->calendars);
2822 heap_free(infoPtr);
2823 return 0;
2826 static LRESULT
2827 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2829 INT i;
2831 /* free month calendar info data */
2832 heap_free(infoPtr->monthdayState);
2833 heap_free(infoPtr->calendars);
2834 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2836 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2838 for (i = 0; i < BrushLast; i++) DeleteObject(infoPtr->brushes[i]);
2839 for (i = 0; i < PenLast; i++) DeleteObject(infoPtr->pens[i]);
2841 heap_free(infoPtr);
2842 return 0;
2846 * Handler for WM_NOTIFY messages
2848 static LRESULT
2849 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2851 /* notification from year edit updown */
2852 if (hdr->code == UDN_DELTAPOS)
2854 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2856 if (hdr->hwndFrom == infoPtr->hWndYearUpDown && nmud->iDelta)
2858 /* year value limits are set up explicitly after updown creation */
2859 MONTHCAL_Scroll(infoPtr, 12 * nmud->iDelta, FALSE);
2860 MONTHCAL_NotifyDayState(infoPtr);
2861 MONTHCAL_NotifySelectionChange(infoPtr);
2864 return 0;
2867 static inline BOOL
2868 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2870 BOOL prev = infoPtr->isUnicode;
2871 infoPtr->isUnicode = isUnicode;
2872 return prev;
2875 static inline BOOL
2876 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2878 return infoPtr->isUnicode;
2881 static LRESULT WINAPI
2882 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2884 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0);
2886 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2888 if (!infoPtr && (uMsg != WM_CREATE))
2889 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2890 switch(uMsg)
2892 case MCM_GETCURSEL:
2893 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2895 case MCM_SETCURSEL:
2896 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2898 case MCM_GETMAXSELCOUNT:
2899 return MONTHCAL_GetMaxSelCount(infoPtr);
2901 case MCM_SETMAXSELCOUNT:
2902 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2904 case MCM_GETSELRANGE:
2905 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2907 case MCM_SETSELRANGE:
2908 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2910 case MCM_GETMONTHRANGE:
2911 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2913 case MCM_SETDAYSTATE:
2914 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2916 case MCM_GETMINREQRECT:
2917 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2919 case MCM_GETCOLOR:
2920 return MONTHCAL_GetColor(infoPtr, wParam);
2922 case MCM_SETCOLOR:
2923 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2925 case MCM_GETTODAY:
2926 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2928 case MCM_SETTODAY:
2929 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2931 case MCM_HITTEST:
2932 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2934 case MCM_GETFIRSTDAYOFWEEK:
2935 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2937 case MCM_SETFIRSTDAYOFWEEK:
2938 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2940 case MCM_GETRANGE:
2941 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2943 case MCM_SETRANGE:
2944 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2946 case MCM_GETMONTHDELTA:
2947 return MONTHCAL_GetMonthDelta(infoPtr);
2949 case MCM_SETMONTHDELTA:
2950 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2952 case MCM_GETMAXTODAYWIDTH:
2953 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2955 case MCM_SETUNICODEFORMAT:
2956 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2958 case MCM_GETUNICODEFORMAT:
2959 return MONTHCAL_GetUnicodeFormat(infoPtr);
2961 case MCM_GETCALENDARCOUNT:
2962 return MONTHCAL_GetCalCount(infoPtr);
2964 case WM_GETDLGCODE:
2965 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2967 case WM_RBUTTONUP:
2968 return MONTHCAL_RButtonUp(infoPtr, lParam);
2970 case WM_LBUTTONDOWN:
2971 return MONTHCAL_LButtonDown(infoPtr, lParam);
2973 case WM_MOUSEMOVE:
2974 return MONTHCAL_MouseMove(infoPtr, lParam);
2976 case WM_LBUTTONUP:
2977 return MONTHCAL_LButtonUp(infoPtr, lParam);
2979 case WM_PAINT:
2980 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2982 case WM_PRINTCLIENT:
2983 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2985 case WM_ERASEBKGND:
2986 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2988 case WM_SETFOCUS:
2989 return MONTHCAL_SetFocus(infoPtr);
2991 case WM_SIZE:
2992 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2994 case WM_NOTIFY:
2995 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2997 case WM_CREATE:
2998 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
3000 case WM_SETFONT:
3001 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
3003 case WM_GETFONT:
3004 return MONTHCAL_GetFont(infoPtr);
3006 case WM_TIMER:
3007 return MONTHCAL_Timer(infoPtr, wParam);
3009 case WM_THEMECHANGED:
3010 return theme_changed (infoPtr);
3012 case WM_DESTROY:
3013 return MONTHCAL_Destroy(infoPtr);
3015 case WM_SYSCOLORCHANGE:
3016 COMCTL32_RefreshSysColors();
3017 return 0;
3019 case WM_STYLECHANGED:
3020 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
3022 case WM_STYLECHANGING:
3023 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
3025 default:
3026 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
3027 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
3028 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
3033 void
3034 MONTHCAL_Register(void)
3036 WNDCLASSW wndClass;
3038 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
3039 wndClass.style = CS_GLOBALCLASS;
3040 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
3041 wndClass.cbClsExtra = 0;
3042 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
3043 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3044 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
3045 wndClass.lpszClassName = MONTHCAL_CLASSW;
3047 RegisterClassW(&wndClass);
3051 void
3052 MONTHCAL_Unregister(void)
3054 UnregisterClassW(MONTHCAL_CLASSW, NULL);