wined3d: Simplify shader interface matching for SM4+ vertex shaders.
[wine.git] / dlls / comctl32 / monthcal.c
blob36acb41cdf289b1217c99a5ea4b6884252efa3b0
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 * NOTE
27 * This code was audited for completeness against the documented features
28 * of Comctl32.dll version 6.0 on Oct. 20, 2004, by Dimitrie O. Paun.
30 * Unless otherwise noted, we believe this code to be complete, as per
31 * the specification mentioned above.
32 * If you discover missing features, or bugs, please note them below.
34 * TODO:
35 * -- MCM_[GS]ETUNICODEFORMAT
36 * -- handle resources better (doesn't work now);
37 * -- take care of internationalization.
38 * -- keyboard handling.
39 * -- search for FIXME
42 #include <math.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "commctrl.h"
54 #include "comctl32.h"
55 #include "uxtheme.h"
56 #include "vssym32.h"
57 #include "wine/unicode.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
62 #define MC_SEL_LBUTUP 1 /* Left button released */
63 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
64 #define MC_PREVPRESSED 4 /* Prev month button pressed */
65 #define MC_NEXTPRESSED 8 /* Next month button pressed */
66 #define MC_PREVNEXTMONTHDELAY 350 /* when continuously pressing `next/prev
67 month', wait 350 ms before going
68 to the next/prev month */
69 #define MC_TODAYUPDATEDELAY 120000 /* time between today check for update (2 min) */
71 #define MC_PREVNEXTMONTHTIMER 1 /* Timer IDs */
72 #define MC_TODAYUPDATETIMER 2
74 #define MC_CALENDAR_PADDING 6
76 #define countof(arr) (sizeof(arr)/sizeof(arr[0]))
78 /* convert from days to 100 nanoseconds unit - used as FILETIME unit */
79 #define DAYSTO100NSECS(days) (((ULONGLONG)(days))*24*60*60*10000000)
81 enum CachedPen
83 PenRed = 0,
84 PenText,
85 PenLast
88 enum CachedBrush
90 BrushTitle = 0,
91 BrushMonth,
92 BrushBackground,
93 BrushLast
96 /* single calendar data */
97 typedef struct _CALENDAR_INFO
99 RECT title; /* rect for the header above the calendar */
100 RECT titlemonth; /* the 'month name' text in the header */
101 RECT titleyear; /* the 'year number' text in the header */
102 RECT wdays; /* week days at top */
103 RECT days; /* calendar area */
104 RECT weeknums; /* week numbers at left side */
106 SYSTEMTIME month;/* contains calendar main month/year */
107 } CALENDAR_INFO;
109 typedef struct
111 HWND hwndSelf;
112 DWORD dwStyle; /* cached GWL_STYLE */
114 COLORREF colors[MCSC_TRAILINGTEXT+1];
115 HBRUSH brushes[BrushLast];
116 HPEN pens[PenLast];
118 HFONT hFont;
119 HFONT hBoldFont;
120 int textHeight;
121 int height_increment;
122 int width_increment;
123 INT delta; /* scroll rate; # of months that the */
124 /* control moves when user clicks a scroll button */
125 int firstDay; /* Start month calendar with firstDay's day,
126 stored in SYSTEMTIME format */
127 BOOL firstDaySet; /* first week day differs from locale defined */
129 BOOL isUnicode; /* value set with MCM_SETUNICODE format */
131 MONTHDAYSTATE *monthdayState;
132 SYSTEMTIME todaysDate;
133 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
134 int status; /* See MC_SEL flags */
135 SYSTEMTIME firstSel; /* first selected day */
136 INT maxSelCount;
137 SYSTEMTIME minSel; /* contains single selection when used without MCS_MULTISELECT */
138 SYSTEMTIME maxSel;
139 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
140 DWORD rangeValid;
141 SYSTEMTIME minDate;
142 SYSTEMTIME maxDate;
144 RECT titlebtnnext; /* the `next month' button in the header */
145 RECT titlebtnprev; /* the `prev month' button in the header */
146 RECT todayrect; /* `today: xx/xx/xx' text rect */
147 HWND hwndNotify; /* Window to receive the notifications */
148 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
149 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
150 WNDPROC EditWndProc; /* original Edit window procedure */
152 CALENDAR_INFO *calendars;
153 SIZE dim; /* [cx,cy] - dimensions of calendars matrix, row/column count */
154 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
156 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
158 /* empty SYSTEMTIME const */
159 static const SYSTEMTIME st_null;
160 /* valid date limits */
161 static const SYSTEMTIME max_allowed_date = { /* wYear */ 9999, /* wMonth */ 12, /* wDayOfWeek */ 0, /* wDay */ 31 };
162 static const SYSTEMTIME min_allowed_date = { /* wYear */ 1752, /* wMonth */ 9, /* wDayOfWeek */ 0, /* wDay */ 14 };
164 /* Prev/Next buttons */
165 enum nav_direction
167 DIRECTION_BACKWARD,
168 DIRECTION_FORWARD
171 /* helper functions */
172 static inline INT MONTHCAL_GetCalCount(const MONTHCAL_INFO *infoPtr)
174 return infoPtr->dim.cx * infoPtr->dim.cy;
177 /* send a single MCN_SELCHANGE notification */
178 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
180 NMSELCHANGE nmsc;
182 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
183 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
184 nmsc.nmhdr.code = MCN_SELCHANGE;
185 nmsc.stSelStart = infoPtr->minSel;
186 nmsc.stSelStart.wDayOfWeek = 0;
187 if(infoPtr->dwStyle & MCS_MULTISELECT){
188 nmsc.stSelEnd = infoPtr->maxSel;
189 nmsc.stSelEnd.wDayOfWeek = 0;
191 else
192 nmsc.stSelEnd = st_null;
194 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
197 /* send a single MCN_SELECT notification */
198 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
200 NMSELCHANGE nmsc;
202 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
203 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
204 nmsc.nmhdr.code = MCN_SELECT;
205 nmsc.stSelStart = infoPtr->minSel;
206 nmsc.stSelStart.wDayOfWeek = 0;
207 if(infoPtr->dwStyle & MCS_MULTISELECT){
208 nmsc.stSelEnd = infoPtr->maxSel;
209 nmsc.stSelEnd.wDayOfWeek = 0;
211 else
212 nmsc.stSelEnd = st_null;
214 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
217 static inline int MONTHCAL_MonthDiff(const SYSTEMTIME *left, const SYSTEMTIME *right)
219 return (right->wYear - left->wYear)*12 + right->wMonth - left->wMonth;
222 /* returns the number of days in any given month, checking for leap days */
223 /* January is 1, December is 12 */
224 int MONTHCAL_MonthLength(int month, int year)
226 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
227 /* Wrap around, this eases handling. Getting length only we shouldn't care
228 about year change here cause January and December have
229 the same day quantity */
230 if(month == 0)
231 month = 12;
232 else if(month == 13)
233 month = 1;
235 /* special case for calendar transition year */
236 if(month == min_allowed_date.wMonth && year == min_allowed_date.wYear) return 19;
238 /* if we have a leap year add 1 day to February */
239 /* a leap year is a year either divisible by 400 */
240 /* or divisible by 4 and not by 100 */
241 if(month == 2) { /* February */
242 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
243 (year%4 == 0)) ? 1 : 0);
245 else {
246 return mdays[month - 1];
250 /* compares timestamps using date part only */
251 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
253 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
254 (first->wDay == second->wDay);
257 /* make sure that date fields are valid */
258 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
260 if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
261 if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear)) return FALSE;
263 return TRUE;
266 /* Copies timestamp part only.
268 * PARAMETERS
270 * [I] from : source date
271 * [O] to : dest date
273 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
275 to->wHour = from->wHour;
276 to->wMinute = from->wMinute;
277 to->wSecond = from->wSecond;
280 /* Copies date part only.
282 * PARAMETERS
284 * [I] from : source date
285 * [O] to : dest date
287 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
289 to->wYear = from->wYear;
290 to->wMonth = from->wMonth;
291 to->wDay = from->wDay;
292 to->wDayOfWeek = from->wDayOfWeek;
295 /* Compares two dates in SYSTEMTIME format
297 * PARAMETERS
299 * [I] first : pointer to valid first date data to compare
300 * [I] second : pointer to valid second date data to compare
302 * RETURN VALUE
304 * -1 : first < second
305 * 0 : first == second
306 * 1 : first > second
308 * Note that no date validation performed, already validated values expected.
310 LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
312 FILETIME ft_first, ft_second;
314 SystemTimeToFileTime(first, &ft_first);
315 SystemTimeToFileTime(second, &ft_second);
317 return CompareFileTime(&ft_first, &ft_second);
320 static LONG MONTHCAL_CompareMonths(const SYSTEMTIME *first, const SYSTEMTIME *second)
322 SYSTEMTIME st_first, st_second;
324 st_first = st_second = st_null;
325 MONTHCAL_CopyDate(first, &st_first);
326 MONTHCAL_CopyDate(second, &st_second);
327 st_first.wDay = st_second.wDay = 1;
329 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
332 static LONG MONTHCAL_CompareDate(const SYSTEMTIME *first, const SYSTEMTIME *second)
334 SYSTEMTIME st_first, st_second;
336 st_first = st_second = st_null;
337 MONTHCAL_CopyDate(first, &st_first);
338 MONTHCAL_CopyDate(second, &st_second);
340 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
343 /* Checks largest possible date range and configured one
345 * PARAMETERS
347 * [I] infoPtr : valid pointer to control data
348 * [I] date : pointer to valid date data to check
349 * [I] fix : make date fit valid range
351 * RETURN VALUE
353 * TRUE - date within largest and configured range
354 * FALSE - date is outside largest or configured range
356 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr,
357 SYSTEMTIME *date, BOOL fix)
359 const SYSTEMTIME *fix_st = NULL;
361 if(MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) {
362 fix_st = &max_allowed_date;
364 else if(MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1) {
365 fix_st = &min_allowed_date;
367 else {
368 if(infoPtr->rangeValid & GDTR_MAX) {
369 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
370 fix_st = &infoPtr->maxDate;
374 if(infoPtr->rangeValid & GDTR_MIN) {
375 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
376 fix_st = &infoPtr->minDate;
381 if (fix && fix_st) {
382 date->wYear = fix_st->wYear;
383 date->wMonth = fix_st->wMonth;
386 return !fix_st;
389 /* Checks passed range width with configured maximum selection count
391 * PARAMETERS
393 * [I] infoPtr : valid pointer to control data
394 * [I] range0 : pointer to valid date data (requested bound)
395 * [I] range1 : pointer to valid date data (primary bound)
396 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
398 * Adjust value computed basing on primary bound and current maximum selection
399 * count. For simple range check (without adjusted value required) (range0, range1)
400 * relation means nothing.
402 * RETURN VALUE
404 * TRUE - range is shorter or equal to maximum
405 * FALSE - range is larger than maximum
407 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
408 const SYSTEMTIME *range0,
409 const SYSTEMTIME *range1,
410 SYSTEMTIME *adjust)
412 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
413 FILETIME ft_range0, ft_range1;
414 LONG cmp;
416 SystemTimeToFileTime(range0, &ft_range0);
417 SystemTimeToFileTime(range1, &ft_range1);
419 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
420 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
421 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
422 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
424 cmp = CompareFileTime(&ft_range0, &ft_range1);
426 if(cmp == 1)
427 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
428 else
429 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
431 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
433 if(adjust) {
434 if(cmp == 1)
435 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
436 else
437 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
439 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
440 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
441 FileTimeToSystemTime(&ft_range0, adjust);
444 return FALSE;
446 else return TRUE;
449 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
450 Milliseconds are intentionally not validated. */
451 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
453 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
454 return FALSE;
455 else
456 return TRUE;
459 /* Note:Depending on DST, this may be offset by a day.
460 Need to find out if we're on a DST place & adjust the clock accordingly.
461 Above function assumes we have a valid data.
462 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
463 0 = Sunday.
466 /* Returns the day in the week
468 * PARAMETERS
469 * [i] date : input date
470 * [I] inplace : set calculated value back to date structure
472 * RETURN VALUE
473 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
475 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
477 SYSTEMTIME st = st_null;
478 FILETIME ft;
480 MONTHCAL_CopyDate(date, &st);
482 SystemTimeToFileTime(&st, &ft);
483 FileTimeToSystemTime(&ft, &st);
485 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
487 return st.wDayOfWeek;
490 /* add/subtract 'months' from date */
491 static inline void MONTHCAL_GetMonth(SYSTEMTIME *date, INT months)
493 INT length, m = date->wMonth + months;
495 date->wYear += m > 0 ? (m - 1) / 12 : m / 12 - 1;
496 date->wMonth = m > 0 ? (m - 1) % 12 + 1 : 12 + m % 12;
497 /* fix moving from last day in a month */
498 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
499 if(date->wDay > length) date->wDay = length;
500 MONTHCAL_CalculateDayOfWeek(date, TRUE);
503 /* properly updates date to point on next month */
504 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
506 MONTHCAL_GetMonth(date, 1);
509 /* properly updates date to point on prev month */
510 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
512 MONTHCAL_GetMonth(date, -1);
515 /* Returns full date for a first currently visible day */
516 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
518 /* zero indexed calendar has the earliest date */
519 SYSTEMTIME st_first = infoPtr->calendars[0].month;
520 INT firstDay;
522 st_first.wDay = 1;
523 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
525 *date = infoPtr->calendars[0].month;
526 MONTHCAL_GetPrevMonth(date);
528 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
529 (infoPtr->firstDay - firstDay) % 7 + 1;
531 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
532 date->wDay -= 7;
534 /* fix day of week */
535 MONTHCAL_CalculateDayOfWeek(date, TRUE);
538 /* Returns full date for a last currently visible day */
539 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
541 /* the latest date is in latest calendar */
542 SYSTEMTIME st, *lt_month = &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
543 INT first_day;
545 *date = *lt_month;
546 st = *lt_month;
548 /* day of week of first day of current month */
549 st.wDay = 1;
550 first_day = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
552 MONTHCAL_GetNextMonth(date);
553 MONTHCAL_GetPrevMonth(&st);
555 /* last calendar starts with some date from previous month that not displayed */
556 st.wDay = MONTHCAL_MonthLength(st.wMonth, st.wYear) +
557 (infoPtr->firstDay - first_day) % 7 + 1;
558 if (st.wDay > MONTHCAL_MonthLength(st.wMonth, st.wYear)) st.wDay -= 7;
560 /* Use month length to get max day. 42 means max day count in calendar area */
561 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
562 MONTHCAL_MonthLength(lt_month->wMonth, lt_month->wYear);
564 /* fix day of week */
565 MONTHCAL_CalculateDayOfWeek(date, TRUE);
568 /* From a given point calculate the row, column and day in the calendar,
569 'day == 0' means the last day of the last month. */
570 static int MONTHCAL_GetDayFromPos(const MONTHCAL_INFO *infoPtr, POINT pt, INT calIdx)
572 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
573 int firstDay, col, row;
574 RECT client;
576 GetClientRect(infoPtr->hwndSelf, &client);
578 /* if the point is outside the x bounds of the window put it at the boundary */
579 if (pt.x > client.right) pt.x = client.right;
581 col = (pt.x - infoPtr->calendars[calIdx].days.left ) / infoPtr->width_increment;
582 row = (pt.y - infoPtr->calendars[calIdx].days.top ) / infoPtr->height_increment;
584 st.wDay = 1;
585 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
586 return col + 7 * row - firstDay;
589 /* Get day position for given date and calendar
591 * PARAMETERS
593 * [I] infoPtr : pointer to control data
594 * [I] date : date value
595 * [O] col : day column (zero based)
596 * [O] row : week column (zero based)
597 * [I] calIdx : calendar index
599 static void MONTHCAL_GetDayPos(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
600 INT *col, INT *row, INT calIdx)
602 SYSTEMTIME st = infoPtr->calendars[calIdx].month;
603 INT first;
605 st.wDay = 1;
606 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
608 if (calIdx == 0 || calIdx == MONTHCAL_GetCalCount(infoPtr)-1) {
609 const SYSTEMTIME *cal = &infoPtr->calendars[calIdx].month;
610 LONG cmp = MONTHCAL_CompareMonths(date, &st);
612 /* previous month */
613 if (cmp == -1) {
614 *col = (first - MONTHCAL_MonthLength(date->wMonth, cal->wYear) + date->wDay) % 7;
615 *row = 0;
616 return;
619 /* next month calculation is same as for current, just add current month length */
620 if (cmp == 1)
621 first += MONTHCAL_MonthLength(cal->wMonth, cal->wYear);
624 *col = (date->wDay + first) % 7;
625 *row = (date->wDay + first - *col) / 7;
628 /* returns bounding box for day in given position in given calendar */
629 static inline void MONTHCAL_GetDayRectI(const MONTHCAL_INFO *infoPtr, RECT *r,
630 INT col, INT row, INT calIdx)
632 r->left = infoPtr->calendars[calIdx].days.left + col * infoPtr->width_increment;
633 r->right = r->left + infoPtr->width_increment;
634 r->top = infoPtr->calendars[calIdx].days.top + row * infoPtr->height_increment;
635 r->bottom = r->top + infoPtr->textHeight;
638 /* Returns bounding box for given date
640 * NOTE: when calendar index is unknown pass -1
642 static inline void MONTHCAL_GetDayRect(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
643 RECT *r, INT calIdx)
645 INT col, row;
647 if (calIdx == -1)
649 INT cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[0].month);
651 if (cmp <= 0)
652 calIdx = 0;
653 else
655 cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month);
656 if (cmp >= 0)
657 calIdx = MONTHCAL_GetCalCount(infoPtr)-1;
658 else
660 for (calIdx = 1; calIdx < MONTHCAL_GetCalCount(infoPtr)-1; calIdx++)
661 if (MONTHCAL_CompareMonths(date, &infoPtr->calendars[calIdx].month) == 0)
662 break;
667 MONTHCAL_GetDayPos(infoPtr, date, &col, &row, calIdx);
668 MONTHCAL_GetDayRectI(infoPtr, r, col, row, calIdx);
671 static LRESULT
672 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
674 INT range;
676 TRACE("flag=%d, st=%p\n", flag, st);
678 switch (flag) {
679 case GMR_VISIBLE:
681 if (st)
683 st[0] = infoPtr->calendars[0].month;
684 st[1] = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
686 if (st[0].wMonth == min_allowed_date.wMonth &&
687 st[0].wYear == min_allowed_date.wYear)
689 st[0].wDay = min_allowed_date.wDay;
691 else
692 st[0].wDay = 1;
693 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
695 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
696 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
699 range = MONTHCAL_GetCalCount(infoPtr);
700 break;
702 case GMR_DAYSTATE:
704 if (st)
706 MONTHCAL_GetMinDate(infoPtr, &st[0]);
707 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
709 /* include two partially visible months */
710 range = MONTHCAL_GetCalCount(infoPtr) + 2;
711 break;
713 default:
714 WARN("Unknown flag value, got %d\n", flag);
715 range = 0;
718 return range;
721 /* Focused day helper:
723 - set focused date to given value;
724 - reset to zero value if NULL passed;
725 - invalidate previous and new day rectangle only if needed.
727 Returns TRUE if focused day changed, FALSE otherwise.
729 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
731 RECT r;
733 if(st)
735 /* there's nothing to do if it's the same date,
736 mouse move within same date rectangle case */
737 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
739 /* invalidate old focused day */
740 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
741 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
743 infoPtr->focusedSel = *st;
746 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
748 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
749 infoPtr->focusedSel = st_null;
751 /* on set invalidates new day, on reset clears previous focused day */
752 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
754 return TRUE;
757 /* draw today boundary box for specified rectangle */
758 static void MONTHCAL_Circle(const MONTHCAL_INFO *infoPtr, HDC hdc, const RECT *r)
760 HPEN old_pen = SelectObject(hdc, infoPtr->pens[PenRed]);
761 HBRUSH old_brush;
763 old_brush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
764 Rectangle(hdc, r->left, r->top, r->right, r->bottom);
766 SelectObject(hdc, old_brush);
767 SelectObject(hdc, old_pen);
770 /* Draw today day mark rectangle
772 * [I] hdc : context to draw in
773 * [I] date : day to mark with rectangle
776 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
777 const SYSTEMTIME *date)
779 RECT r;
781 MONTHCAL_GetDayRect(infoPtr, date, &r, -1);
782 MONTHCAL_Circle(infoPtr, hdc, &r);
785 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
786 int bold, const PAINTSTRUCT *ps)
788 static const WCHAR fmtW[] = { '%','d',0 };
789 WCHAR buf[10];
790 RECT r, r_temp;
791 COLORREF oldCol = 0;
792 COLORREF oldBk = 0;
793 INT old_bkmode, selection;
795 /* no need to check styles: when selection is not valid, it is set to zero.
796 1 < day < 31, so everything is OK */
797 MONTHCAL_GetDayRect(infoPtr, st, &r, -1);
798 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
800 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
801 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0))
803 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
804 TRACE("%s\n", wine_dbgstr_rect(&r));
805 oldCol = SetTextColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
806 oldBk = SetBkColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
807 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
809 selection = 1;
811 else
812 selection = 0;
814 SelectObject(hdc, bold ? infoPtr->hBoldFont : infoPtr->hFont);
816 old_bkmode = SetBkMode(hdc, TRANSPARENT);
817 wsprintfW(buf, fmtW, st->wDay);
818 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
819 SetBkMode(hdc, old_bkmode);
821 if (selection)
823 SetTextColor(hdc, oldCol);
824 SetBkColor(hdc, oldBk);
828 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, enum nav_direction button)
830 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
831 RECT *r = button == DIRECTION_FORWARD ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
832 BOOL pressed = button == DIRECTION_FORWARD ? infoPtr->status & MC_NEXTPRESSED :
833 infoPtr->status & MC_PREVPRESSED;
834 if (theme)
836 static const int states[] = {
837 /* Prev button */
838 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
839 /* Next button */
840 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
842 int stateNum = button == DIRECTION_FORWARD ? 3 : 0;
843 if (pressed)
844 stateNum += 1;
845 else
847 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
849 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
851 else
853 int style = button == DIRECTION_FORWARD ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
854 if (pressed)
855 style |= DFCS_PUSHED;
856 else
858 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
861 DrawFrameControl(hdc, r, DFC_SCROLL, style);
865 /* paint a title with buttons and month/year string */
866 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
868 static const WCHAR mmmmW[] = {'M','M','M','M',0};
869 static const WCHAR mmmW[] = {'M','M','M',0};
870 static const WCHAR mmW[] = {'M','M',0};
871 static const WCHAR fmtyearW[] = {'%','l','d',0};
872 static const WCHAR fmtmmW[] = {'%','0','2','d',0};
873 static const WCHAR fmtmW[] = {'%','d',0};
874 RECT *title = &infoPtr->calendars[calIdx].title;
875 const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
876 WCHAR monthW[80], strW[80], fmtW[80], yearW[6] /* valid year range is 1601-30827 */;
877 int yearoffset, monthoffset, shiftX;
878 SIZE sz;
880 /* fill header box */
881 FillRect(hdc, title, infoPtr->brushes[BrushTitle]);
883 /* month/year string */
884 SetBkColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
885 SetTextColor(hdc, infoPtr->colors[MCSC_TITLETEXT]);
886 SelectObject(hdc, infoPtr->hBoldFont);
888 /* draw formatted date string */
889 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_YEARMONTH, st, NULL, strW, countof(strW));
890 DrawTextW(hdc, strW, strlenW(strW), title, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
892 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SYEARMONTH, fmtW, countof(fmtW));
893 wsprintfW(yearW, fmtyearW, st->wYear);
895 /* month is trickier as it's possible to have different format pictures, we'll
896 test for M, MM, MMM, and MMMM */
897 if (strstrW(fmtW, mmmmW))
898 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+st->wMonth-1, monthW, countof(monthW));
899 else if (strstrW(fmtW, mmmW))
900 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME1+st->wMonth-1, monthW, countof(monthW));
901 else if (strstrW(fmtW, mmW))
902 wsprintfW(monthW, fmtmmW, st->wMonth);
903 else
904 wsprintfW(monthW, fmtmW, st->wMonth);
906 /* update hit boxes */
907 yearoffset = 0;
908 while (strW[yearoffset])
910 if (!strncmpW(&strW[yearoffset], yearW, strlenW(yearW)))
911 break;
912 yearoffset++;
915 monthoffset = 0;
916 while (strW[monthoffset])
918 if (!strncmpW(&strW[monthoffset], monthW, strlenW(monthW)))
919 break;
920 monthoffset++;
923 /* for left limits use offsets */
924 sz.cx = 0;
925 if (yearoffset)
926 GetTextExtentPoint32W(hdc, strW, yearoffset, &sz);
927 infoPtr->calendars[calIdx].titleyear.left = sz.cx;
929 sz.cx = 0;
930 if (monthoffset)
931 GetTextExtentPoint32W(hdc, strW, monthoffset, &sz);
932 infoPtr->calendars[calIdx].titlemonth.left = sz.cx;
934 /* for right limits use actual string parts lengths */
935 GetTextExtentPoint32W(hdc, &strW[yearoffset], strlenW(yearW), &sz);
936 infoPtr->calendars[calIdx].titleyear.right = infoPtr->calendars[calIdx].titleyear.left + sz.cx;
938 GetTextExtentPoint32W(hdc, monthW, strlenW(monthW), &sz);
939 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
941 /* Finally translate rectangles to match center aligned string,
942 hit rectangles are relative to title rectangle before translation. */
943 GetTextExtentPoint32W(hdc, strW, strlenW(strW), &sz);
944 shiftX = (title->right - title->left - sz.cx) / 2 + title->left;
945 OffsetRect(&infoPtr->calendars[calIdx].titleyear, shiftX, 0);
946 OffsetRect(&infoPtr->calendars[calIdx].titlemonth, shiftX, 0);
949 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
951 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
952 static const WCHAR fmt_weekW[] = { '%','d',0 };
953 INT mindays, weeknum, weeknum1, startofprescal;
954 INT i, prev_month;
955 SYSTEMTIME st;
956 WCHAR buf[80];
957 HPEN old_pen;
958 RECT r;
960 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
962 MONTHCAL_GetMinDate(infoPtr, &st);
963 startofprescal = st.wDay;
964 st = *date;
966 prev_month = date->wMonth - 1;
967 if(prev_month == 0) prev_month = 12;
970 Rules what week to call the first week of a new year:
971 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
972 The week containing Jan 1 is the first week of year
973 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
974 First week of year must contain 4 days of the new year
975 LOCALE_IFIRSTWEEKOFYEAR == 1 (what countries?)
976 The first week of the year must contain only days of the new year
978 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
979 weeknum = atoiW(buf);
980 switch (weeknum)
982 case 1: mindays = 6;
983 break;
984 case 2: mindays = 3;
985 break;
986 case 0: mindays = 0;
987 break;
988 default:
989 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
990 mindays = 0;
993 if (date->wMonth == 1)
995 /* calculate all those exceptions for January */
996 st.wDay = st.wMonth = 1;
997 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
998 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
999 weeknum = 1;
1000 else
1002 weeknum = 0;
1003 for(i = 0; i < 11; i++)
1004 weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
1006 weeknum += startofprescal + 7;
1007 weeknum /= 7;
1008 st.wYear -= 1;
1009 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
1010 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
1013 else
1015 weeknum = 0;
1016 for(i = 0; i < prev_month - 1; i++)
1017 weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
1019 weeknum += startofprescal + 7;
1020 weeknum /= 7;
1021 st.wDay = st.wMonth = 1;
1022 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
1023 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
1026 r = infoPtr->calendars[calIdx].weeknums;
1028 /* erase whole week numbers area */
1029 FillRect(hdc, &r, infoPtr->brushes[BrushMonth]);
1030 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1032 /* reduce rectangle to one week number */
1033 r.bottom = r.top + infoPtr->height_increment;
1035 for(i = 0; i < 6; i++) {
1036 if((i == 0) && (weeknum > 50))
1038 wsprintfW(buf, fmt_weekW, weeknum);
1039 weeknum = 0;
1041 else if((i == 5) && (weeknum > 47))
1043 wsprintfW(buf, fmt_weekW, 1);
1045 else
1046 wsprintfW(buf, fmt_weekW, weeknum + i);
1048 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1049 OffsetRect(&r, 0, infoPtr->height_increment);
1052 /* line separator for week numbers column */
1053 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1054 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
1055 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
1056 SelectObject(hdc, old_pen);
1059 /* bottom today date */
1060 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1062 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
1063 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
1064 RECT text_rect, box_rect;
1065 HFONT old_font;
1066 INT col;
1068 if(infoPtr->dwStyle & MCS_NOTODAY) return;
1070 LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, countof(buf_todayW));
1071 col = infoPtr->dwStyle & MCS_NOTODAYCIRCLE ? 0 : 1;
1072 if (infoPtr->dwStyle & MCS_WEEKNUMBERS) col--;
1073 /* label is located below first calendar last row */
1074 MONTHCAL_GetDayRectI(infoPtr, &text_rect, col, 6, infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx);
1075 box_rect = text_rect;
1077 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
1078 buf_dateW, countof(buf_dateW));
1079 old_font = SelectObject(hdc, infoPtr->hBoldFont);
1080 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1082 wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
1083 DrawTextW(hdc, buf, -1, &text_rect, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1084 DrawTextW(hdc, buf, -1, &text_rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1086 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
1087 OffsetRect(&box_rect, -infoPtr->width_increment, 0);
1088 MONTHCAL_Circle(infoPtr, hdc, &box_rect);
1091 SelectObject(hdc, old_font);
1094 /* today mark + focus */
1095 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1097 /* circle today date if only it's in fully visible month */
1098 if (!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
1100 INT i;
1102 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1103 if (!MONTHCAL_CompareMonths(&infoPtr->todaysDate, &infoPtr->calendars[i].month))
1105 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
1106 break;
1110 if (!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
1112 RECT r;
1113 MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
1114 DrawFocusRect(hdc, &r);
1118 /* months before first calendar month and after last calendar month */
1119 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1121 INT mask, index;
1122 UINT length;
1123 SYSTEMTIME st_max, st;
1125 if (infoPtr->dwStyle & MCS_NOTRAILINGDATES) return;
1127 SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
1129 /* draw prev month */
1130 MONTHCAL_GetMinDate(infoPtr, &st);
1131 mask = 1 << (st.wDay-1);
1132 /* December and January both 31 days long, so no worries if wrapped */
1133 length = MONTHCAL_MonthLength(infoPtr->calendars[0].month.wMonth - 1,
1134 infoPtr->calendars[0].month.wYear);
1135 index = 0;
1136 while(st.wDay <= length)
1138 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1139 mask <<= 1;
1140 st.wDay++;
1143 /* draw next month */
1144 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1145 st.wDay = 1;
1146 MONTHCAL_GetNextMonth(&st);
1147 MONTHCAL_GetMaxDate(infoPtr, &st_max);
1148 mask = 1;
1149 index = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)-1;
1150 while(st.wDay <= st_max.wDay)
1152 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1153 mask <<= 1;
1154 st.wDay++;
1158 static int get_localized_dayname(const MONTHCAL_INFO *infoPtr, unsigned int day, WCHAR *buff, unsigned int count)
1160 LCTYPE lctype;
1162 if (infoPtr->dwStyle & MCS_SHORTDAYSOFWEEK)
1163 lctype = LOCALE_SSHORTESTDAYNAME1 + day;
1164 else
1165 lctype = LOCALE_SABBREVDAYNAME1 + day;
1167 return GetLocaleInfoW(LOCALE_USER_DEFAULT, lctype, buff, count);
1170 /* paint a calendar area */
1171 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
1173 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
1174 INT i, j;
1175 UINT length;
1176 RECT r, fill_bk_rect;
1177 SYSTEMTIME st;
1178 WCHAR buf[80];
1179 HPEN old_pen;
1180 int mask;
1182 /* fill whole days area - from week days area to today note rectangle */
1183 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
1184 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
1185 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
1187 FillRect(hdc, &fill_bk_rect, infoPtr->brushes[BrushMonth]);
1189 /* draw line under day abbreviations */
1190 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1191 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
1192 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
1193 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
1194 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
1195 SelectObject(hdc, old_pen);
1197 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
1198 infoPtr->calendars[calIdx].weeknums.right;
1200 /* draw day abbreviations */
1201 SelectObject(hdc, infoPtr->hFont);
1202 SetBkColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
1203 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1204 /* rectangle to draw a single day abbreviation within */
1205 r = infoPtr->calendars[calIdx].wdays;
1206 r.right = r.left + infoPtr->width_increment;
1208 i = infoPtr->firstDay;
1209 for(j = 0; j < 7; j++) {
1210 get_localized_dayname(infoPtr, (i + j + 6) % 7, buf, countof(buf));
1211 DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1212 OffsetRect(&r, infoPtr->width_increment, 0);
1215 /* draw current month */
1216 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1217 st = *date;
1218 st.wDay = 1;
1219 mask = 1;
1220 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1221 while(st.wDay <= length)
1223 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[calIdx+1] & mask, ps);
1224 mask <<= 1;
1225 st.wDay++;
1229 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1231 COLORREF old_text_clr, old_bk_clr;
1232 HFONT old_font;
1233 INT i;
1235 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1236 old_bk_clr = GetBkColor(hdc);
1237 old_font = GetCurrentObject(hdc, OBJ_FONT);
1239 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1241 RECT *title = &infoPtr->calendars[i].title;
1242 RECT r;
1244 /* draw title, redraw all its elements */
1245 if (IntersectRect(&r, &(ps->rcPaint), title))
1246 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1248 /* draw calendar area */
1249 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1250 if (IntersectRect(&r, &(ps->rcPaint), &r))
1251 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1253 /* week numbers */
1254 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1257 /* partially visible months */
1258 MONTHCAL_PaintLeadTrailMonths(infoPtr, hdc, ps);
1260 /* focus and today rectangle */
1261 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1263 /* today at the bottom left */
1264 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1266 /* navigation buttons */
1267 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_BACKWARD);
1268 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_FORWARD);
1270 /* restore context */
1271 SetBkColor(hdc, old_bk_clr);
1272 SelectObject(hdc, old_font);
1273 SetTextColor(hdc, old_text_clr);
1276 static LRESULT
1277 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, RECT *rect)
1279 TRACE("rect %p\n", rect);
1281 if(!rect) return FALSE;
1283 *rect = infoPtr->calendars[0].title;
1284 rect->bottom = infoPtr->calendars[0].days.bottom + infoPtr->todayrect.bottom -
1285 infoPtr->todayrect.top;
1287 AdjustWindowRect(rect, infoPtr->dwStyle, FALSE);
1289 /* minimal rectangle is zero based */
1290 OffsetRect(rect, -rect->left, -rect->top);
1292 TRACE("%s\n", wine_dbgstr_rect(rect));
1294 return TRUE;
1297 static COLORREF
1298 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, UINT index)
1300 TRACE("%p, %d\n", infoPtr, index);
1302 if (index > MCSC_TRAILINGTEXT) return -1;
1303 return infoPtr->colors[index];
1306 static LRESULT
1307 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, UINT index, COLORREF color)
1309 enum CachedBrush type;
1310 COLORREF prev;
1312 TRACE("%p, %d: color %08x\n", infoPtr, index, color);
1314 if (index > MCSC_TRAILINGTEXT) return -1;
1316 prev = infoPtr->colors[index];
1317 infoPtr->colors[index] = color;
1319 /* update cached brush */
1320 switch (index)
1322 case MCSC_BACKGROUND:
1323 type = BrushBackground;
1324 break;
1325 case MCSC_TITLEBK:
1326 type = BrushTitle;
1327 break;
1328 case MCSC_MONTHBK:
1329 type = BrushMonth;
1330 break;
1331 default:
1332 type = BrushLast;
1335 if (type != BrushLast)
1337 DeleteObject(infoPtr->brushes[type]);
1338 infoPtr->brushes[type] = CreateSolidBrush(color);
1341 /* update cached pen */
1342 if (index == MCSC_TEXT)
1344 DeleteObject(infoPtr->pens[PenText]);
1345 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[index]);
1348 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND);
1349 return prev;
1352 static LRESULT
1353 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1355 TRACE("\n");
1357 if(infoPtr->delta)
1358 return infoPtr->delta;
1360 return MONTHCAL_GetMonthRange(infoPtr, GMR_VISIBLE, NULL);
1364 static LRESULT
1365 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1367 INT prev = infoPtr->delta;
1369 TRACE("delta %d\n", delta);
1371 infoPtr->delta = delta;
1372 return prev;
1376 static inline LRESULT
1377 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1379 int day;
1381 /* convert from SYSTEMTIME to locale format */
1382 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1384 return MAKELONG(day, infoPtr->firstDaySet);
1388 /* Sets the first day of the week that will appear in the control
1391 * PARAMETERS:
1392 * [I] infoPtr : valid pointer to control data
1393 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1396 * RETURN VALUE:
1397 * Low word contains previous first day,
1398 * high word indicates was first day forced with this message before or is
1399 * locale defined (TRUE - was forced, FALSE - wasn't).
1401 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1402 * FIXME: we need more error checking here
1404 static LRESULT
1405 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1407 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1408 int new_day;
1410 TRACE("%d\n", day);
1412 if(day == -1)
1414 WCHAR buf[80];
1416 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1417 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1419 new_day = atoiW(buf);
1421 infoPtr->firstDaySet = FALSE;
1423 else if(day >= 7)
1425 new_day = 6; /* max first day allowed */
1426 infoPtr->firstDaySet = TRUE;
1428 else
1430 /* Native behaviour for that case is broken: invalid date number >31
1431 got displayed at (0,0) position, current month starts always from
1432 (1,0) position. Should be implemented here as well only if there's
1433 nothing else to do. */
1434 if (day < -1)
1435 FIXME("No bug compatibility for day=%d\n", day);
1437 new_day = day;
1438 infoPtr->firstDaySet = TRUE;
1441 /* convert from locale to SYSTEMTIME format */
1442 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1444 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1446 return prev;
1449 static LRESULT
1450 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1452 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1455 static LRESULT
1456 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1458 FILETIME ft_min, ft_max;
1460 TRACE("%x %p\n", limits, range);
1462 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1463 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1464 return FALSE;
1466 infoPtr->rangeValid = 0;
1467 infoPtr->minDate = infoPtr->maxDate = st_null;
1469 if (limits & GDTR_MIN)
1471 if (!MONTHCAL_ValidateTime(&range[0]))
1472 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1474 infoPtr->minDate = range[0];
1475 infoPtr->rangeValid |= GDTR_MIN;
1477 if (limits & GDTR_MAX)
1479 if (!MONTHCAL_ValidateTime(&range[1]))
1480 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1482 infoPtr->maxDate = range[1];
1483 infoPtr->rangeValid |= GDTR_MAX;
1486 /* Only one limit set - we are done */
1487 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1488 return TRUE;
1490 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1491 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1493 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1495 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1497 /* Native swaps limits only when both limits are being set. */
1498 SYSTEMTIME st_tmp = infoPtr->minDate;
1499 infoPtr->minDate = infoPtr->maxDate;
1500 infoPtr->maxDate = st_tmp;
1502 else
1504 /* reset the other limit */
1505 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1506 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1507 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1511 return TRUE;
1515 static LRESULT
1516 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1518 TRACE("%p\n", range);
1520 if (!range) return 0;
1522 range[1] = infoPtr->maxDate;
1523 range[0] = infoPtr->minDate;
1525 return infoPtr->rangeValid;
1529 static LRESULT
1530 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1532 TRACE("%p %d %p\n", infoPtr, months, states);
1534 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return 0;
1535 if (months != MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)) return 0;
1537 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1539 return 1;
1542 static LRESULT
1543 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1545 TRACE("%p\n", curSel);
1546 if(!curSel) return FALSE;
1547 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1549 *curSel = infoPtr->minSel;
1550 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1551 return TRUE;
1554 static LRESULT
1555 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1557 SYSTEMTIME prev = infoPtr->minSel, selection;
1558 INT diff;
1559 WORD day;
1561 TRACE("%p\n", curSel);
1562 if(!curSel) return FALSE;
1563 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1565 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1566 /* exit earlier if selection equals current */
1567 if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1569 selection = *curSel;
1570 selection.wHour = selection.wMinute = selection.wSecond = selection.wMilliseconds = 0;
1571 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1573 if(!MONTHCAL_IsDateInValidRange(infoPtr, &selection, FALSE)) return FALSE;
1575 /* scroll calendars only if we have to */
1576 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, curSel);
1577 if (diff <= 0)
1579 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, curSel);
1580 if (diff > 0) diff = 0;
1583 if (diff != 0)
1585 INT i;
1587 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1588 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1591 /* we need to store time part as it is */
1592 selection = *curSel;
1593 MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1594 infoPtr->minSel = infoPtr->maxSel = selection;
1596 /* if selection is still in current month, reduce rectangle */
1597 day = prev.wDay;
1598 prev.wDay = curSel->wDay;
1599 if (MONTHCAL_IsDateEqual(&prev, curSel))
1601 RECT r_prev, r_new;
1603 prev.wDay = day;
1604 MONTHCAL_GetDayRect(infoPtr, &prev, &r_prev, -1);
1605 MONTHCAL_GetDayRect(infoPtr, curSel, &r_new, -1);
1607 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1608 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1610 else
1611 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1613 return TRUE;
1617 static LRESULT
1618 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1620 return infoPtr->maxSelCount;
1624 static LRESULT
1625 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1627 TRACE("%d\n", max);
1629 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1630 if(max <= 0) return FALSE;
1632 infoPtr->maxSelCount = max;
1634 return TRUE;
1638 static LRESULT
1639 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1641 TRACE("%p\n", range);
1643 if(!range) return FALSE;
1645 if(infoPtr->dwStyle & MCS_MULTISELECT)
1647 range[1] = infoPtr->maxSel;
1648 range[0] = infoPtr->minSel;
1649 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1650 return TRUE;
1653 return FALSE;
1657 static LRESULT
1658 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1660 SYSTEMTIME old_range[2];
1661 INT diff;
1663 TRACE("%p\n", range);
1665 if(!range || !(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1667 /* adjust timestamps */
1668 if(!MONTHCAL_ValidateTime(&range[0])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1669 if(!MONTHCAL_ValidateTime(&range[1])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1671 /* maximum range exceeded */
1672 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1674 old_range[0] = infoPtr->minSel;
1675 old_range[1] = infoPtr->maxSel;
1677 /* swap if min > max */
1678 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1680 infoPtr->minSel = range[0];
1681 infoPtr->maxSel = range[1];
1683 else
1685 infoPtr->minSel = range[1];
1686 infoPtr->maxSel = range[0];
1689 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, &infoPtr->maxSel);
1690 if (diff < 0)
1692 diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, &infoPtr->maxSel);
1693 if (diff > 0) diff = 0;
1696 if (diff != 0)
1698 INT i;
1700 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1701 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1704 /* update day of week */
1705 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1706 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1708 /* redraw if bounds changed */
1709 /* FIXME: no actual need to redraw everything */
1710 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1711 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1713 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1716 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1717 return TRUE;
1721 static LRESULT
1722 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1724 TRACE("%p\n", today);
1726 if(!today) return FALSE;
1727 *today = infoPtr->todaysDate;
1728 return TRUE;
1731 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1733 * RETURN VALUE
1735 * TRUE - today date changed
1736 * FALSE - today date isn't changed
1738 static BOOL
1739 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1741 RECT new_r, old_r;
1743 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1745 MONTHCAL_GetDayRect(infoPtr, &infoPtr->todaysDate, &old_r, -1);
1746 MONTHCAL_GetDayRect(infoPtr, today, &new_r, -1);
1748 infoPtr->todaysDate = *today;
1750 /* only two days need redrawing */
1751 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1752 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1753 /* and today label */
1754 InvalidateRect(infoPtr->hwndSelf, &infoPtr->todayrect, FALSE);
1755 return TRUE;
1758 /* MCM_SETTODAT handler */
1759 static LRESULT
1760 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1762 TRACE("%p\n", today);
1764 if (today)
1766 /* remember if date was set successfully */
1767 if (MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1770 return 0;
1773 /* returns calendar index containing specified point, or -1 if it's background */
1774 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1776 RECT r;
1777 INT i;
1779 for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1781 /* whole bounding rectangle allows some optimization to compute */
1782 r.left = infoPtr->calendars[i].title.left;
1783 r.top = infoPtr->calendars[i].title.top;
1784 r.bottom = infoPtr->calendars[i].days.bottom;
1785 r.right = infoPtr->calendars[i].days.right;
1787 if (PtInRect(&r, *pt)) return i;
1790 return -1;
1793 static inline UINT fill_hittest_info(const MCHITTESTINFO *src, MCHITTESTINFO *dest)
1795 dest->uHit = src->uHit;
1796 dest->st = src->st;
1798 if (dest->cbSize == sizeof(MCHITTESTINFO))
1799 memcpy(&dest->rc, &src->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1801 return src->uHit;
1804 static LRESULT
1805 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1807 MCHITTESTINFO htinfo;
1808 SYSTEMTIME *ht_month;
1809 INT day, calIdx;
1811 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1813 htinfo.st = st_null;
1815 /* we should preserve passed fields if hit area doesn't need them */
1816 if (lpht->cbSize == sizeof(MCHITTESTINFO))
1817 memcpy(&htinfo.rc, &lpht->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1819 /* guess in what calendar we are */
1820 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1821 if (calIdx == -1)
1823 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1825 htinfo.uHit = MCHT_TODAYLINK;
1826 htinfo.rc = infoPtr->todayrect;
1828 else
1829 /* outside of calendar area? What's left must be background :-) */
1830 htinfo.uHit = MCHT_CALENDARBK;
1832 return fill_hittest_info(&htinfo, lpht);
1835 /* are we in the header? */
1836 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1837 /* FIXME: buttons hittesting could be optimized cause maximum
1838 two calendars have buttons */
1839 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1841 htinfo.uHit = MCHT_TITLEBTNPREV;
1842 htinfo.rc = infoPtr->titlebtnprev;
1844 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1846 htinfo.uHit = MCHT_TITLEBTNNEXT;
1847 htinfo.rc = infoPtr->titlebtnnext;
1849 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1851 htinfo.uHit = MCHT_TITLEMONTH;
1852 htinfo.rc = infoPtr->calendars[calIdx].titlemonth;
1853 htinfo.iOffset = calIdx;
1855 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1857 htinfo.uHit = MCHT_TITLEYEAR;
1858 htinfo.rc = infoPtr->calendars[calIdx].titleyear;
1859 htinfo.iOffset = calIdx;
1861 else
1863 htinfo.uHit = MCHT_TITLE;
1864 htinfo.rc = infoPtr->calendars[calIdx].title;
1865 htinfo.iOffset = calIdx;
1868 return fill_hittest_info(&htinfo, lpht);
1871 ht_month = &infoPtr->calendars[calIdx].month;
1872 /* days area (including week days and week numbers) */
1873 day = MONTHCAL_GetDayFromPos(infoPtr, lpht->pt, calIdx);
1874 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1876 htinfo.uHit = MCHT_CALENDARDAY;
1877 htinfo.iOffset = calIdx;
1878 htinfo.st.wYear = ht_month->wYear;
1879 htinfo.st.wMonth = (day < 1) ? ht_month->wMonth -1 : ht_month->wMonth;
1880 htinfo.st.wDay = (day < 1) ?
1881 MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day : day;
1883 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1885 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1887 htinfo.uHit = MCHT_CALENDARWEEKNUM;
1888 htinfo.st.wYear = ht_month->wYear;
1889 htinfo.iOffset = calIdx;
1891 if (day < 1)
1893 htinfo.st.wMonth = ht_month->wMonth - 1;
1894 htinfo.st.wDay = MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day;
1896 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1898 htinfo.st.wMonth = ht_month->wMonth + 1;
1899 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1901 else
1903 htinfo.st.wMonth = ht_month->wMonth;
1904 htinfo.st.wDay = day;
1907 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1909 htinfo.iOffset = calIdx;
1910 htinfo.st.wYear = ht_month->wYear;
1911 htinfo.st.wMonth = ht_month->wMonth;
1912 /* previous month only valid for first calendar */
1913 if (day < 1 && calIdx == 0)
1915 htinfo.uHit = MCHT_CALENDARDATEPREV;
1916 MONTHCAL_GetPrevMonth(&htinfo.st);
1917 htinfo.st.wDay = MONTHCAL_MonthLength(htinfo.st.wMonth, htinfo.st.wYear) + day;
1919 /* next month only valid for last calendar */
1920 else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear) &&
1921 calIdx == MONTHCAL_GetCalCount(infoPtr)-1)
1923 htinfo.uHit = MCHT_CALENDARDATENEXT;
1924 MONTHCAL_GetNextMonth(&htinfo.st);
1925 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1927 /* multiple calendars case - blank areas for previous/next month */
1928 else if (day < 1 || day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1930 htinfo.uHit = MCHT_CALENDARBK;
1932 else
1934 htinfo.uHit = MCHT_CALENDARDATE;
1935 htinfo.st.wDay = day;
1938 MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1939 MONTHCAL_GetDayRectI(infoPtr, &htinfo.rc, htinfo.iCol, htinfo.iRow, calIdx);
1940 /* always update day of week */
1941 MONTHCAL_CalculateDayOfWeek(&htinfo.st, TRUE);
1944 return fill_hittest_info(&htinfo, lpht);
1947 /* MCN_GETDAYSTATE notification helper */
1948 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1950 MONTHDAYSTATE *state;
1951 NMDAYSTATE nmds;
1953 if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return;
1955 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1956 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1957 nmds.nmhdr.code = MCN_GETDAYSTATE;
1958 nmds.cDayState = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0);
1959 nmds.prgDayState = state = Alloc(nmds.cDayState * sizeof(MONTHDAYSTATE));
1961 MONTHCAL_GetMinDate(infoPtr, &nmds.stStart);
1962 nmds.stStart.wDay = 1;
1964 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1965 memcpy(infoPtr->monthdayState, nmds.prgDayState,
1966 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
1968 Free(state);
1971 /* no valid range check performed */
1972 static void MONTHCAL_Scroll(MONTHCAL_INFO *infoPtr, INT delta)
1974 INT i, selIdx = -1;
1976 for(i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1978 /* save selection position to shift it later */
1979 if (selIdx == -1 && MONTHCAL_CompareMonths(&infoPtr->minSel, &infoPtr->calendars[i].month) == 0)
1980 selIdx = i;
1982 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, delta);
1985 /* selection is always shifted to first calendar */
1986 if(infoPtr->dwStyle & MCS_MULTISELECT)
1988 SYSTEMTIME range[2];
1990 MONTHCAL_GetSelRange(infoPtr, range);
1991 MONTHCAL_GetMonth(&range[0], delta - selIdx);
1992 MONTHCAL_GetMonth(&range[1], delta - selIdx);
1993 MONTHCAL_SetSelRange(infoPtr, range);
1995 else
1997 SYSTEMTIME st = infoPtr->minSel;
1999 MONTHCAL_GetMonth(&st, delta - selIdx);
2000 MONTHCAL_SetCurSel(infoPtr, &st);
2004 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
2006 INT delta = infoPtr->delta ? infoPtr->delta : MONTHCAL_GetCalCount(infoPtr);
2007 SYSTEMTIME st;
2009 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
2011 /* check if change allowed by range set */
2012 if(direction == DIRECTION_BACKWARD)
2014 st = infoPtr->calendars[0].month;
2015 MONTHCAL_GetMonth(&st, -delta);
2017 else
2019 st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2020 MONTHCAL_GetMonth(&st, delta);
2023 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
2025 MONTHCAL_Scroll(infoPtr, direction == DIRECTION_BACKWARD ? -delta : delta);
2026 MONTHCAL_NotifyDayState(infoPtr);
2027 MONTHCAL_NotifySelectionChange(infoPtr);
2030 static LRESULT
2031 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2033 HMENU hMenu;
2034 POINT menupoint;
2035 WCHAR buf[32];
2037 hMenu = CreatePopupMenu();
2038 LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf));
2039 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
2040 menupoint.x = (short)LOWORD(lParam);
2041 menupoint.y = (short)HIWORD(lParam);
2042 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2043 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
2044 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
2046 if (infoPtr->dwStyle & MCS_MULTISELECT)
2048 SYSTEMTIME range[2];
2050 range[0] = range[1] = infoPtr->todaysDate;
2051 MONTHCAL_SetSelRange(infoPtr, range);
2053 else
2054 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2056 MONTHCAL_NotifySelectionChange(infoPtr);
2057 MONTHCAL_NotifySelect(infoPtr);
2060 return 0;
2063 /***
2064 * DESCRIPTION:
2065 * Subclassed edit control windproc function
2067 * PARAMETER(S):
2068 * [I] hwnd : the edit window handle
2069 * [I] uMsg : the message that is to be processed
2070 * [I] wParam : first message parameter
2071 * [I] lParam : second message parameter
2074 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2076 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
2078 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
2079 hwnd, uMsg, wParam, lParam);
2081 switch (uMsg)
2083 case WM_GETDLGCODE:
2084 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
2086 case WM_DESTROY:
2088 WNDPROC editProc = infoPtr->EditWndProc;
2089 infoPtr->EditWndProc = NULL;
2090 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
2091 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
2094 case WM_KILLFOCUS:
2095 break;
2097 case WM_KEYDOWN:
2098 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
2099 break;
2101 default:
2102 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
2105 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
2106 SendMessageW(hwnd, WM_CLOSE, 0, 0);
2107 return 0;
2110 /* creates updown control and edit box */
2111 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr, INT calIdx)
2113 RECT *rc = &infoPtr->calendars[calIdx].titleyear;
2114 RECT *title = &infoPtr->calendars[calIdx].title;
2116 infoPtr->hWndYearEdit =
2117 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
2118 rc->left + 3, (title->bottom + title->top - infoPtr->textHeight) / 2,
2119 rc->right - rc->left + 4,
2120 infoPtr->textHeight, infoPtr->hwndSelf,
2121 NULL, NULL, NULL);
2123 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
2125 infoPtr->hWndYearUpDown =
2126 CreateWindowExW(0, UPDOWN_CLASSW, 0,
2127 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
2128 rc->right + 7, (title->bottom + title->top - infoPtr->textHeight) / 2,
2129 18, infoPtr->textHeight, infoPtr->hwndSelf,
2130 NULL, NULL, NULL);
2132 /* attach edit box */
2133 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
2134 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
2135 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
2136 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->calendars[calIdx].month.wYear);
2138 /* subclass edit box */
2139 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
2140 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
2142 SetFocus(infoPtr->hWndYearEdit);
2145 static LRESULT
2146 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2148 MCHITTESTINFO ht;
2149 DWORD hit;
2151 /* Actually we don't need input focus for calendar, this is used to kill
2152 year updown and its buddy edit box */
2153 if (IsWindow(infoPtr->hWndYearUpDown))
2155 SetFocus(infoPtr->hwndSelf);
2156 return 0;
2159 SetCapture(infoPtr->hwndSelf);
2161 ht.cbSize = sizeof(MCHITTESTINFO);
2162 ht.pt.x = (short)LOWORD(lParam);
2163 ht.pt.y = (short)HIWORD(lParam);
2165 hit = MONTHCAL_HitTest(infoPtr, &ht);
2167 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
2169 switch(hit)
2171 case MCHT_TITLEBTNNEXT:
2172 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2173 infoPtr->status = MC_NEXTPRESSED;
2174 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2175 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2176 return 0;
2178 case MCHT_TITLEBTNPREV:
2179 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2180 infoPtr->status = MC_PREVPRESSED;
2181 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2182 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2183 return 0;
2185 case MCHT_TITLEMONTH:
2187 HMENU hMenu = CreatePopupMenu();
2188 WCHAR buf[32];
2189 POINT menupoint;
2190 INT i;
2192 for (i = 0; i < 12; i++)
2194 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
2195 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
2197 menupoint.x = ht.pt.x;
2198 menupoint.y = ht.pt.y;
2199 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2200 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
2201 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
2203 if ((i > 0) && (i < 13) && infoPtr->calendars[ht.iOffset].month.wMonth != i)
2205 INT delta = i - infoPtr->calendars[ht.iOffset].month.wMonth;
2206 SYSTEMTIME st;
2208 /* check if change allowed by range set */
2209 st = delta < 0 ? infoPtr->calendars[0].month :
2210 infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2211 MONTHCAL_GetMonth(&st, delta);
2213 if (MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE))
2215 MONTHCAL_Scroll(infoPtr, delta);
2216 MONTHCAL_NotifyDayState(infoPtr);
2217 MONTHCAL_NotifySelectionChange(infoPtr);
2218 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2221 return 0;
2223 case MCHT_TITLEYEAR:
2225 MONTHCAL_EditYear(infoPtr, ht.iOffset);
2226 return 0;
2228 case MCHT_TODAYLINK:
2230 if (infoPtr->dwStyle & MCS_MULTISELECT)
2232 SYSTEMTIME range[2];
2234 range[0] = range[1] = infoPtr->todaysDate;
2235 MONTHCAL_SetSelRange(infoPtr, range);
2237 else
2238 MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2240 MONTHCAL_NotifySelectionChange(infoPtr);
2241 MONTHCAL_NotifySelect(infoPtr);
2242 return 0;
2244 case MCHT_CALENDARDATENEXT:
2245 case MCHT_CALENDARDATEPREV:
2246 case MCHT_CALENDARDATE:
2248 SYSTEMTIME st[2];
2250 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2252 st[0] = st[1] = ht.st;
2253 /* clear selection range */
2254 MONTHCAL_SetSelRange(infoPtr, st);
2256 infoPtr->status = MC_SEL_LBUTDOWN;
2257 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
2258 return 0;
2262 return 1;
2266 static LRESULT
2267 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2269 NMHDR nmhdr;
2270 MCHITTESTINFO ht;
2271 DWORD hit;
2273 TRACE("\n");
2275 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2276 RECT *r;
2278 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2279 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2280 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2282 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2285 ReleaseCapture();
2287 /* always send NM_RELEASEDCAPTURE notification */
2288 nmhdr.hwndFrom = infoPtr->hwndSelf;
2289 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2290 nmhdr.code = NM_RELEASEDCAPTURE;
2291 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2293 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2295 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2297 ht.cbSize = sizeof(MCHITTESTINFO);
2298 ht.pt.x = (short)LOWORD(lParam);
2299 ht.pt.y = (short)HIWORD(lParam);
2300 hit = MONTHCAL_HitTest(infoPtr, &ht);
2302 infoPtr->status = MC_SEL_LBUTUP;
2303 MONTHCAL_SetDayFocus(infoPtr, NULL);
2305 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2307 SYSTEMTIME sel = infoPtr->minSel;
2309 /* will be invalidated here */
2310 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2312 /* send MCN_SELCHANGE only if new date selected */
2313 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2314 MONTHCAL_NotifySelectionChange(infoPtr);
2316 MONTHCAL_NotifySelect(infoPtr);
2319 return 0;
2323 static LRESULT
2324 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2326 TRACE("%ld\n", id);
2328 switch(id) {
2329 case MC_PREVNEXTMONTHTIMER:
2330 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2331 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2332 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2333 break;
2334 case MC_TODAYUPDATETIMER:
2336 SYSTEMTIME st;
2338 if(infoPtr->todaySet) return 0;
2340 GetLocalTime(&st);
2341 MONTHCAL_UpdateToday(infoPtr, &st);
2343 /* notification sent anyway */
2344 MONTHCAL_NotifySelectionChange(infoPtr);
2346 return 0;
2348 default:
2349 ERR("got unknown timer %ld\n", id);
2350 break;
2353 return 0;
2357 static LRESULT
2358 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2360 MCHITTESTINFO ht;
2361 SYSTEMTIME st_ht;
2362 INT hit;
2363 RECT r;
2365 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2367 ht.cbSize = sizeof(MCHITTESTINFO);
2368 ht.pt.x = (short)LOWORD(lParam);
2369 ht.pt.y = (short)HIWORD(lParam);
2370 ht.iOffset = -1;
2372 hit = MONTHCAL_HitTest(infoPtr, &ht);
2374 /* not on the calendar date numbers? bail out */
2375 TRACE("hit:%x\n",hit);
2376 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2378 MONTHCAL_SetDayFocus(infoPtr, NULL);
2379 return 0;
2382 st_ht = ht.st;
2384 /* if pointer is over focused day still there's nothing to do */
2385 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2387 MONTHCAL_GetDayRect(infoPtr, &ht.st, &r, ht.iOffset);
2389 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2390 SYSTEMTIME st[2];
2392 MONTHCAL_GetSelRange(infoPtr, st);
2394 /* If we're still at the first selected date and range is empty, return.
2395 If range isn't empty we should change range to a single firstSel */
2396 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2397 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2399 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2401 st[0] = infoPtr->firstSel;
2402 /* we should overwrite timestamp here */
2403 MONTHCAL_CopyDate(&st_ht, &st[1]);
2405 /* bounds will be swapped here if needed */
2406 MONTHCAL_SetSelRange(infoPtr, st);
2408 return 0;
2411 done:
2413 /* FIXME: this should specify a rectangle containing only the days that changed
2414 using InvalidateRect */
2415 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2417 return 0;
2421 static LRESULT
2422 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2424 HDC hdc;
2425 PAINTSTRUCT ps;
2427 if (hdc_paint)
2429 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2430 hdc = hdc_paint;
2432 else
2433 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2435 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2436 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2437 return 0;
2440 static LRESULT
2441 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2443 RECT rc;
2445 if (!GetClipBox(hdc, &rc)) return FALSE;
2447 FillRect(hdc, &rc, infoPtr->brushes[BrushBackground]);
2449 return TRUE;
2452 static LRESULT
2453 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2455 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2457 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2458 return 0;
2460 if (options & PRF_ERASEBKGND)
2461 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2463 if (options & PRF_CLIENT)
2464 MONTHCAL_Paint(infoPtr, hdc);
2466 return 0;
2469 static LRESULT
2470 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2472 TRACE("\n");
2474 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2476 return 0;
2479 /* sets the size information */
2480 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2482 static const WCHAR O0W[] = { '0','0',0 };
2483 RECT *title=&infoPtr->calendars[0].title;
2484 RECT *prev=&infoPtr->titlebtnprev;
2485 RECT *next=&infoPtr->titlebtnnext;
2486 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2487 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2488 RECT *wdays=&infoPtr->calendars[0].wdays;
2489 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2490 RECT *days=&infoPtr->calendars[0].days;
2491 RECT *todayrect=&infoPtr->todayrect;
2493 INT xdiv, dx, dy, i, j, x, y, c_dx, c_dy;
2494 WCHAR buff[80];
2495 TEXTMETRICW tm;
2496 INT day_width;
2497 RECT client;
2498 HFONT font;
2499 SIZE size;
2500 HDC hdc;
2502 GetClientRect(infoPtr->hwndSelf, &client);
2504 hdc = GetDC(infoPtr->hwndSelf);
2505 font = SelectObject(hdc, infoPtr->hFont);
2507 /* get the height and width of each day's text */
2508 GetTextMetricsW(hdc, &tm);
2509 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2511 /* find widest day name for current locale and font */
2512 day_width = 0;
2513 for (i = 0; i < 7; i++)
2515 SIZE sz;
2517 if (get_localized_dayname(infoPtr, i, buff, countof(buff)))
2519 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2520 if (sz.cx > day_width) day_width = sz.cx;
2522 else /* locale independent fallback on failure */
2524 static const WCHAR sunW[] = { 'S','u','n' };
2525 GetTextExtentPoint32W(hdc, sunW, countof(sunW), &sz);
2526 day_width = sz.cx;
2527 break;
2531 day_width += 2;
2533 /* recalculate the height and width increments and offsets */
2534 size.cx = 0;
2535 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2537 /* restore the originally selected font */
2538 SelectObject(hdc, font);
2539 ReleaseDC(infoPtr->hwndSelf, hdc);
2541 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2543 infoPtr->width_increment = max(day_width, size.cx * 2 + 4);
2544 infoPtr->height_increment = infoPtr->textHeight;
2546 /* calculate title area */
2547 title->top = 0;
2548 title->bottom = 3 * infoPtr->height_increment / 2;
2549 title->left = 0;
2550 title->right = infoPtr->width_increment * xdiv;
2552 /* set the dimensions of the next and previous buttons and center */
2553 /* the month text vertically */
2554 prev->top = next->top = title->top + 4;
2555 prev->bottom = next->bottom = title->bottom - 4;
2556 prev->left = title->left + 4;
2557 prev->right = prev->left + (title->bottom - title->top);
2558 next->right = title->right - 4;
2559 next->left = next->right - (title->bottom - title->top);
2561 /* titlemonth->left and right change based upon the current month
2562 and are recalculated in refresh as the current month may change
2563 without the control being resized */
2564 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2565 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2567 /* week numbers */
2568 weeknumrect->left = 0;
2569 weeknumrect->right = infoPtr->dwStyle & MCS_WEEKNUMBERS ? prev->right : 0;
2571 /* days abbreviated names */
2572 wdays->left = days->left = weeknumrect->right;
2573 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2574 wdays->top = title->bottom;
2575 wdays->bottom = wdays->top + infoPtr->height_increment;
2577 days->top = weeknumrect->top = wdays->bottom;
2578 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2580 todayrect->left = 0;
2581 todayrect->right = title->right;
2582 todayrect->top = days->bottom;
2583 todayrect->bottom = days->bottom + infoPtr->height_increment;
2585 /* compute calendar count, update all calendars */
2586 x = (client.right + MC_CALENDAR_PADDING) / (title->right - title->left + MC_CALENDAR_PADDING);
2587 /* today label affects whole height */
2588 if (infoPtr->dwStyle & MCS_NOTODAY)
2589 y = (client.bottom + MC_CALENDAR_PADDING) / (days->bottom - title->top + MC_CALENDAR_PADDING);
2590 else
2591 y = (client.bottom - todayrect->bottom + todayrect->top + MC_CALENDAR_PADDING) /
2592 (days->bottom - title->top + MC_CALENDAR_PADDING);
2594 /* TODO: ensure that count is properly adjusted to fit 12 months constraint */
2595 if (x == 0) x = 1;
2596 if (y == 0) y = 1;
2598 if (x*y != MONTHCAL_GetCalCount(infoPtr))
2600 infoPtr->dim.cx = x;
2601 infoPtr->dim.cy = y;
2602 infoPtr->calendars = ReAlloc(infoPtr->calendars, MONTHCAL_GetCalCount(infoPtr)*sizeof(CALENDAR_INFO));
2604 infoPtr->monthdayState = ReAlloc(infoPtr->monthdayState,
2605 MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
2606 MONTHCAL_NotifyDayState(infoPtr);
2608 /* update pointers that we'll need */
2609 title = &infoPtr->calendars[0].title;
2610 wdays = &infoPtr->calendars[0].wdays;
2611 days = &infoPtr->calendars[0].days;
2614 for (i = 1; i < MONTHCAL_GetCalCount(infoPtr); i++)
2616 /* set months */
2617 infoPtr->calendars[i] = infoPtr->calendars[0];
2618 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, i);
2621 /* offset all rectangles to center in client area */
2622 c_dx = (client.right - x * title->right - MC_CALENDAR_PADDING * (x-1)) / 2;
2623 c_dy = (client.bottom - y * todayrect->bottom - MC_CALENDAR_PADDING * (y-1)) / 2;
2625 /* if calendar doesn't fit client area show it at left/top bounds */
2626 if (title->left + c_dx < 0) c_dx = 0;
2627 if (title->top + c_dy < 0) c_dy = 0;
2629 for (i = 0; i < y; i++)
2631 for (j = 0; j < x; j++)
2633 dx = j*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx;
2634 dy = i*(days->bottom - title->top + MC_CALENDAR_PADDING) + c_dy;
2636 OffsetRect(&infoPtr->calendars[i*x+j].title, dx, dy);
2637 OffsetRect(&infoPtr->calendars[i*x+j].titlemonth, dx, dy);
2638 OffsetRect(&infoPtr->calendars[i*x+j].titleyear, dx, dy);
2639 OffsetRect(&infoPtr->calendars[i*x+j].wdays, dx, dy);
2640 OffsetRect(&infoPtr->calendars[i*x+j].weeknums, dx, dy);
2641 OffsetRect(&infoPtr->calendars[i*x+j].days, dx, dy);
2645 OffsetRect(prev, c_dx, c_dy);
2646 OffsetRect(next, (x-1)*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx, c_dy);
2648 i = infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx;
2649 todayrect->left = infoPtr->calendars[i].title.left;
2650 todayrect->right = infoPtr->calendars[i].title.right;
2651 todayrect->top = infoPtr->calendars[i].days.bottom;
2652 todayrect->bottom = infoPtr->calendars[i].days.bottom + infoPtr->height_increment;
2654 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2655 infoPtr->width_increment,infoPtr->height_increment,
2656 wine_dbgstr_rect(&client),
2657 wine_dbgstr_rect(title),
2658 wine_dbgstr_rect(wdays),
2659 wine_dbgstr_rect(days),
2660 wine_dbgstr_rect(todayrect));
2663 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2665 TRACE("(width=%d, height=%d)\n", Width, Height);
2667 MONTHCAL_UpdateSize(infoPtr);
2668 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2670 return 0;
2673 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2675 return (LRESULT)infoPtr->hFont;
2678 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2680 HFONT hOldFont;
2681 LOGFONTW lf;
2683 if (!hFont) return 0;
2685 hOldFont = infoPtr->hFont;
2686 infoPtr->hFont = hFont;
2688 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2689 lf.lfWeight = FW_BOLD;
2690 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2692 MONTHCAL_UpdateSize(infoPtr);
2694 if (redraw)
2695 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2697 return (LRESULT)hOldFont;
2700 /* update theme after a WM_THEMECHANGED message */
2701 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2703 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2704 CloseThemeData (theme);
2705 OpenThemeData (infoPtr->hwndSelf, themeClass);
2706 return 0;
2709 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2710 const STYLESTRUCT *lpss)
2712 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2713 wStyleType, lpss->styleOld, lpss->styleNew);
2715 if (wStyleType != GWL_STYLE) return 0;
2717 infoPtr->dwStyle = lpss->styleNew;
2719 /* make room for week numbers */
2720 if ((lpss->styleNew ^ lpss->styleOld) & (MCS_WEEKNUMBERS | MCS_SHORTDAYSOFWEEK))
2721 MONTHCAL_UpdateSize(infoPtr);
2723 return 0;
2726 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2727 STYLESTRUCT *lpss)
2729 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2730 wStyleType, lpss->styleOld, lpss->styleNew);
2732 /* block MCS_MULTISELECT change */
2733 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2735 if (lpss->styleOld & MCS_MULTISELECT)
2736 lpss->styleNew |= MCS_MULTISELECT;
2737 else
2738 lpss->styleNew &= ~MCS_MULTISELECT;
2741 /* block MCS_DAYSTATE change */
2742 if ((lpss->styleNew ^ lpss->styleOld) & MCS_DAYSTATE)
2744 if (lpss->styleOld & MCS_DAYSTATE)
2745 lpss->styleNew |= MCS_DAYSTATE;
2746 else
2747 lpss->styleNew &= ~MCS_DAYSTATE;
2750 return 0;
2753 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2754 static LRESULT
2755 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2757 MONTHCAL_INFO *infoPtr;
2759 /* allocate memory for info structure */
2760 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2761 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2763 if (infoPtr == NULL) {
2764 ERR("could not allocate info memory!\n");
2765 return 0;
2768 infoPtr->hwndSelf = hwnd;
2769 infoPtr->hwndNotify = lpcs->hwndParent;
2770 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2771 infoPtr->dim.cx = infoPtr->dim.cy = 1;
2772 infoPtr->calendars = Alloc(sizeof(CALENDAR_INFO));
2773 if (!infoPtr->calendars) goto fail;
2774 infoPtr->monthdayState = Alloc(3*sizeof(MONTHDAYSTATE));
2775 if (!infoPtr->monthdayState) goto fail;
2777 /* initialize info structure */
2778 /* FIXME: calculate systemtime ->> localtime(subtract timezoneinfo) */
2780 GetLocalTime(&infoPtr->todaysDate);
2781 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2783 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2785 infoPtr->colors[MCSC_BACKGROUND] = comctl32_color.clrWindow;
2786 infoPtr->colors[MCSC_TEXT] = comctl32_color.clrWindowText;
2787 infoPtr->colors[MCSC_TITLEBK] = comctl32_color.clrActiveCaption;
2788 infoPtr->colors[MCSC_TITLETEXT] = comctl32_color.clrWindow;
2789 infoPtr->colors[MCSC_MONTHBK] = comctl32_color.clrWindow;
2790 infoPtr->colors[MCSC_TRAILINGTEXT] = comctl32_color.clrGrayText;
2792 infoPtr->brushes[BrushBackground] = CreateSolidBrush(infoPtr->colors[MCSC_BACKGROUND]);
2793 infoPtr->brushes[BrushTitle] = CreateSolidBrush(infoPtr->colors[MCSC_TITLEBK]);
2794 infoPtr->brushes[BrushMonth] = CreateSolidBrush(infoPtr->colors[MCSC_MONTHBK]);
2796 infoPtr->pens[PenRed] = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
2797 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[MCSC_TEXT]);
2799 infoPtr->minSel = infoPtr->todaysDate;
2800 infoPtr->maxSel = infoPtr->todaysDate;
2801 infoPtr->calendars[0].month = infoPtr->todaysDate;
2802 infoPtr->isUnicode = TRUE;
2804 /* setup control layout and day state data */
2805 MONTHCAL_UpdateSize(infoPtr);
2807 /* today auto update timer, to be freed only on control destruction */
2808 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2810 OpenThemeData (infoPtr->hwndSelf, themeClass);
2812 return 0;
2814 fail:
2815 Free(infoPtr->monthdayState);
2816 Free(infoPtr->calendars);
2817 Free(infoPtr);
2818 return 0;
2821 static LRESULT
2822 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2824 INT i;
2826 /* free month calendar info data */
2827 Free(infoPtr->monthdayState);
2828 Free(infoPtr->calendars);
2829 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2831 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2833 for (i = 0; i < BrushLast; i++) DeleteObject(infoPtr->brushes[i]);
2834 for (i = 0; i < PenLast; i++) DeleteObject(infoPtr->pens[i]);
2836 Free(infoPtr);
2837 return 0;
2841 * Handler for WM_NOTIFY messages
2843 static LRESULT
2844 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2846 /* notification from year edit updown */
2847 if (hdr->code == UDN_DELTAPOS)
2849 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2851 if (hdr->hwndFrom == infoPtr->hWndYearUpDown && nmud->iDelta)
2853 /* year value limits are set up explicitly after updown creation */
2854 MONTHCAL_Scroll(infoPtr, 12 * nmud->iDelta);
2855 MONTHCAL_NotifyDayState(infoPtr);
2856 MONTHCAL_NotifySelectionChange(infoPtr);
2859 return 0;
2862 static inline BOOL
2863 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2865 BOOL prev = infoPtr->isUnicode;
2866 infoPtr->isUnicode = isUnicode;
2867 return prev;
2870 static inline BOOL
2871 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2873 return infoPtr->isUnicode;
2876 static LRESULT WINAPI
2877 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2879 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0);
2881 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2883 if (!infoPtr && (uMsg != WM_CREATE))
2884 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2885 switch(uMsg)
2887 case MCM_GETCURSEL:
2888 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2890 case MCM_SETCURSEL:
2891 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2893 case MCM_GETMAXSELCOUNT:
2894 return MONTHCAL_GetMaxSelCount(infoPtr);
2896 case MCM_SETMAXSELCOUNT:
2897 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2899 case MCM_GETSELRANGE:
2900 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2902 case MCM_SETSELRANGE:
2903 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2905 case MCM_GETMONTHRANGE:
2906 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2908 case MCM_SETDAYSTATE:
2909 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2911 case MCM_GETMINREQRECT:
2912 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2914 case MCM_GETCOLOR:
2915 return MONTHCAL_GetColor(infoPtr, wParam);
2917 case MCM_SETCOLOR:
2918 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2920 case MCM_GETTODAY:
2921 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2923 case MCM_SETTODAY:
2924 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2926 case MCM_HITTEST:
2927 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2929 case MCM_GETFIRSTDAYOFWEEK:
2930 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2932 case MCM_SETFIRSTDAYOFWEEK:
2933 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2935 case MCM_GETRANGE:
2936 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2938 case MCM_SETRANGE:
2939 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2941 case MCM_GETMONTHDELTA:
2942 return MONTHCAL_GetMonthDelta(infoPtr);
2944 case MCM_SETMONTHDELTA:
2945 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2947 case MCM_GETMAXTODAYWIDTH:
2948 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2950 case MCM_SETUNICODEFORMAT:
2951 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2953 case MCM_GETUNICODEFORMAT:
2954 return MONTHCAL_GetUnicodeFormat(infoPtr);
2956 case MCM_GETCALENDARCOUNT:
2957 return MONTHCAL_GetCalCount(infoPtr);
2959 case WM_GETDLGCODE:
2960 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2962 case WM_RBUTTONUP:
2963 return MONTHCAL_RButtonUp(infoPtr, lParam);
2965 case WM_LBUTTONDOWN:
2966 return MONTHCAL_LButtonDown(infoPtr, lParam);
2968 case WM_MOUSEMOVE:
2969 return MONTHCAL_MouseMove(infoPtr, lParam);
2971 case WM_LBUTTONUP:
2972 return MONTHCAL_LButtonUp(infoPtr, lParam);
2974 case WM_PAINT:
2975 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2977 case WM_PRINTCLIENT:
2978 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2980 case WM_ERASEBKGND:
2981 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2983 case WM_SETFOCUS:
2984 return MONTHCAL_SetFocus(infoPtr);
2986 case WM_SIZE:
2987 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2989 case WM_NOTIFY:
2990 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2992 case WM_CREATE:
2993 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2995 case WM_SETFONT:
2996 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2998 case WM_GETFONT:
2999 return MONTHCAL_GetFont(infoPtr);
3001 case WM_TIMER:
3002 return MONTHCAL_Timer(infoPtr, wParam);
3004 case WM_THEMECHANGED:
3005 return theme_changed (infoPtr);
3007 case WM_DESTROY:
3008 return MONTHCAL_Destroy(infoPtr);
3010 case WM_SYSCOLORCHANGE:
3011 COMCTL32_RefreshSysColors();
3012 return 0;
3014 case WM_STYLECHANGED:
3015 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
3017 case WM_STYLECHANGING:
3018 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
3020 default:
3021 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
3022 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
3023 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
3028 void
3029 MONTHCAL_Register(void)
3031 WNDCLASSW wndClass;
3033 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
3034 wndClass.style = CS_GLOBALCLASS;
3035 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
3036 wndClass.cbClsExtra = 0;
3037 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
3038 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
3039 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
3040 wndClass.lpszClassName = MONTHCAL_CLASSW;
3042 RegisterClassW(&wndClass);
3046 void
3047 MONTHCAL_Unregister(void)
3049 UnregisterClassW(MONTHCAL_CLASSW, NULL);