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
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.
35 * -- MCM_[GS]ETUNICODEFORMAT
36 * -- handle resources better (doesn't work now);
37 * -- take care of internationalization.
38 * -- keyboard handling.
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)
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 */
112 DWORD dwStyle
; /* cached GWL_STYLE */
114 COLORREF colors
[MCSC_TRAILINGTEXT
+1];
115 HBRUSH brushes
[BrushLast
];
121 int height_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 */
137 SYSTEMTIME minSel
; /* contains single selection when used without MCS_MULTISELECT */
139 SYSTEMTIME focusedSel
; /* date currently focused with mouse movement */
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 */
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
)
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;
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
)
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;
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 */
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);
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 )
262 if (time
->wDay
== 0 || time
->wDay
> MONTHCAL_MonthLength(time
->wMonth
, time
->wYear
))
268 /* Copies timestamp part only.
272 * [I] from : source date
275 static void MONTHCAL_CopyTime(const SYSTEMTIME
*from
, SYSTEMTIME
*to
)
277 to
->wHour
= from
->wHour
;
278 to
->wMinute
= from
->wMinute
;
279 to
->wSecond
= from
->wSecond
;
282 /* Copies date part only.
286 * [I] from : source date
289 static void MONTHCAL_CopyDate(const SYSTEMTIME
*from
, SYSTEMTIME
*to
)
291 to
->wYear
= from
->wYear
;
292 to
->wMonth
= from
->wMonth
;
293 to
->wDay
= from
->wDay
;
294 to
->wDayOfWeek
= from
->wDayOfWeek
;
297 /* Compares two dates in SYSTEMTIME format
301 * [I] first : pointer to valid first date data to compare
302 * [I] second : pointer to valid second date data to compare
306 * -1 : first < second
307 * 0 : first == second
310 * Note that no date validation performed, already validated values expected.
312 LONG
MONTHCAL_CompareSystemTime(const SYSTEMTIME
*first
, const SYSTEMTIME
*second
)
314 FILETIME ft_first
, ft_second
;
316 SystemTimeToFileTime(first
, &ft_first
);
317 SystemTimeToFileTime(second
, &ft_second
);
319 return CompareFileTime(&ft_first
, &ft_second
);
322 static LONG
MONTHCAL_CompareMonths(const SYSTEMTIME
*first
, const SYSTEMTIME
*second
)
324 SYSTEMTIME st_first
, st_second
;
326 st_first
= st_second
= st_null
;
327 MONTHCAL_CopyDate(first
, &st_first
);
328 MONTHCAL_CopyDate(second
, &st_second
);
329 st_first
.wDay
= st_second
.wDay
= 1;
331 return MONTHCAL_CompareSystemTime(&st_first
, &st_second
);
334 static LONG
MONTHCAL_CompareDate(const SYSTEMTIME
*first
, const SYSTEMTIME
*second
)
336 SYSTEMTIME st_first
, st_second
;
338 st_first
= st_second
= st_null
;
339 MONTHCAL_CopyDate(first
, &st_first
);
340 MONTHCAL_CopyDate(second
, &st_second
);
342 return MONTHCAL_CompareSystemTime(&st_first
, &st_second
);
345 /* Checks largest possible date range and configured one
349 * [I] infoPtr : valid pointer to control data
350 * [I] date : pointer to valid date data to check
351 * [I] fix : make date fit valid range
355 * TRUE - date within largest and configured range
356 * FALSE - date is outside largest or configured range
358 static BOOL
MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO
*infoPtr
,
359 SYSTEMTIME
*date
, BOOL fix
)
361 const SYSTEMTIME
*fix_st
= NULL
;
363 if(MONTHCAL_CompareSystemTime(date
, &max_allowed_date
) == 1) {
364 fix_st
= &max_allowed_date
;
366 else if(MONTHCAL_CompareSystemTime(date
, &min_allowed_date
) == -1) {
367 fix_st
= &min_allowed_date
;
370 if(infoPtr
->rangeValid
& GDTR_MAX
) {
371 if((MONTHCAL_CompareSystemTime(date
, &infoPtr
->maxDate
) == 1)) {
372 fix_st
= &infoPtr
->maxDate
;
376 if(infoPtr
->rangeValid
& GDTR_MIN
) {
377 if((MONTHCAL_CompareSystemTime(date
, &infoPtr
->minDate
) == -1)) {
378 fix_st
= &infoPtr
->minDate
;
384 date
->wYear
= fix_st
->wYear
;
385 date
->wMonth
= fix_st
->wMonth
;
391 /* Checks passed range width with configured maximum selection count
395 * [I] infoPtr : valid pointer to control data
396 * [I] range0 : pointer to valid date data (requested bound)
397 * [I] range1 : pointer to valid date data (primary bound)
398 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
400 * Adjust value computed basing on primary bound and current maximum selection
401 * count. For simple range check (without adjusted value required) (range0, range1)
402 * relation means nothing.
406 * TRUE - range is shorter or equal to maximum
407 * FALSE - range is larger than maximum
409 static BOOL
MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO
*infoPtr
,
410 const SYSTEMTIME
*range0
,
411 const SYSTEMTIME
*range1
,
414 ULARGE_INTEGER ul_range0
, ul_range1
, ul_diff
;
415 FILETIME ft_range0
, ft_range1
;
418 SystemTimeToFileTime(range0
, &ft_range0
);
419 SystemTimeToFileTime(range1
, &ft_range1
);
421 ul_range0
.u
.LowPart
= ft_range0
.dwLowDateTime
;
422 ul_range0
.u
.HighPart
= ft_range0
.dwHighDateTime
;
423 ul_range1
.u
.LowPart
= ft_range1
.dwLowDateTime
;
424 ul_range1
.u
.HighPart
= ft_range1
.dwHighDateTime
;
426 cmp
= CompareFileTime(&ft_range0
, &ft_range1
);
429 ul_diff
.QuadPart
= ul_range0
.QuadPart
- ul_range1
.QuadPart
;
431 ul_diff
.QuadPart
= -ul_range0
.QuadPart
+ ul_range1
.QuadPart
;
433 if(ul_diff
.QuadPart
>= DAYSTO100NSECS(infoPtr
->maxSelCount
)) {
437 ul_range0
.QuadPart
= ul_range1
.QuadPart
+ DAYSTO100NSECS(infoPtr
->maxSelCount
- 1);
439 ul_range0
.QuadPart
= ul_range1
.QuadPart
- DAYSTO100NSECS(infoPtr
->maxSelCount
- 1);
441 ft_range0
.dwLowDateTime
= ul_range0
.u
.LowPart
;
442 ft_range0
.dwHighDateTime
= ul_range0
.u
.HighPart
;
443 FileTimeToSystemTime(&ft_range0
, adjust
);
451 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
452 Milliseconds are intentionally not validated. */
453 static BOOL
MONTHCAL_ValidateTime(const SYSTEMTIME
*time
)
455 if((time
->wHour
> 24) || (time
->wMinute
> 59) || (time
->wSecond
> 59))
461 /* Note:Depending on DST, this may be offset by a day.
462 Need to find out if we're on a DST place & adjust the clock accordingly.
463 Above function assumes we have a valid data.
464 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
468 /* Returns the day in the week
471 * [i] date : input date
472 * [I] inplace : set calculated value back to date structure
475 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
477 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME
*date
, BOOL inplace
)
479 SYSTEMTIME st
= st_null
;
482 MONTHCAL_CopyDate(date
, &st
);
484 SystemTimeToFileTime(&st
, &ft
);
485 FileTimeToSystemTime(&ft
, &st
);
487 if (inplace
) date
->wDayOfWeek
= st
.wDayOfWeek
;
489 return st
.wDayOfWeek
;
492 /* add/subtract 'months' from date */
493 static inline void MONTHCAL_GetMonth(SYSTEMTIME
*date
, INT months
)
495 INT length
, m
= date
->wMonth
+ months
;
497 date
->wYear
+= m
> 0 ? (m
- 1) / 12 : m
/ 12 - 1;
498 date
->wMonth
= m
> 0 ? (m
- 1) % 12 + 1 : 12 + m
% 12;
499 /* fix moving from last day in a month */
500 length
= MONTHCAL_MonthLength(date
->wMonth
, date
->wYear
);
501 if(date
->wDay
> length
) date
->wDay
= length
;
502 MONTHCAL_CalculateDayOfWeek(date
, TRUE
);
505 /* properly updates date to point on next month */
506 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME
*date
)
508 MONTHCAL_GetMonth(date
, 1);
511 /* properly updates date to point on prev month */
512 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME
*date
)
514 MONTHCAL_GetMonth(date
, -1);
517 /* Returns full date for a first currently visible day */
518 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO
*infoPtr
, SYSTEMTIME
*date
)
520 /* zero indexed calendar has the earliest date */
521 SYSTEMTIME st_first
= infoPtr
->calendars
[0].month
;
525 firstDay
= MONTHCAL_CalculateDayOfWeek(&st_first
, FALSE
);
527 *date
= infoPtr
->calendars
[0].month
;
528 MONTHCAL_GetPrevMonth(date
);
530 date
->wDay
= MONTHCAL_MonthLength(date
->wMonth
, date
->wYear
) +
531 (infoPtr
->firstDay
- firstDay
) % 7 + 1;
533 if(date
->wDay
> MONTHCAL_MonthLength(date
->wMonth
, date
->wYear
))
536 /* fix day of week */
537 MONTHCAL_CalculateDayOfWeek(date
, TRUE
);
540 /* Returns full date for a last currently visible day */
541 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO
*infoPtr
, SYSTEMTIME
*date
)
543 /* the latest date is in latest calendar */
544 SYSTEMTIME st
, *lt_month
= &infoPtr
->calendars
[MONTHCAL_GetCalCount(infoPtr
)-1].month
;
550 /* day of week of first day of current month */
552 first_day
= MONTHCAL_CalculateDayOfWeek(&st
, FALSE
);
554 MONTHCAL_GetNextMonth(date
);
555 MONTHCAL_GetPrevMonth(&st
);
557 /* last calendar starts with some date from previous month that not displayed */
558 st
.wDay
= MONTHCAL_MonthLength(st
.wMonth
, st
.wYear
) +
559 (infoPtr
->firstDay
- first_day
) % 7 + 1;
560 if (st
.wDay
> MONTHCAL_MonthLength(st
.wMonth
, st
.wYear
)) st
.wDay
-= 7;
562 /* Use month length to get max day. 42 means max day count in calendar area */
563 date
->wDay
= 42 - (MONTHCAL_MonthLength(st
.wMonth
, st
.wYear
) - st
.wDay
+ 1) -
564 MONTHCAL_MonthLength(lt_month
->wMonth
, lt_month
->wYear
);
566 /* fix day of week */
567 MONTHCAL_CalculateDayOfWeek(date
, TRUE
);
570 /* From a given point calculate the row, column and day in the calendar,
571 'day == 0' means the last day of the last month. */
572 static int MONTHCAL_GetDayFromPos(const MONTHCAL_INFO
*infoPtr
, POINT pt
, INT calIdx
)
574 SYSTEMTIME st
= infoPtr
->calendars
[calIdx
].month
;
575 int firstDay
, col
, row
;
578 GetClientRect(infoPtr
->hwndSelf
, &client
);
580 /* if the point is outside the x bounds of the window put it at the boundary */
581 if (pt
.x
> client
.right
) pt
.x
= client
.right
;
583 col
= (pt
.x
- infoPtr
->calendars
[calIdx
].days
.left
) / infoPtr
->width_increment
;
584 row
= (pt
.y
- infoPtr
->calendars
[calIdx
].days
.top
) / infoPtr
->height_increment
;
587 firstDay
= (MONTHCAL_CalculateDayOfWeek(&st
, FALSE
) + 6 - infoPtr
->firstDay
) % 7;
588 return col
+ 7 * row
- firstDay
;
591 /* Get day position for given date and calendar
595 * [I] infoPtr : pointer to control data
596 * [I] date : date value
597 * [O] col : day column (zero based)
598 * [O] row : week column (zero based)
599 * [I] calIdx : calendar index
601 static void MONTHCAL_GetDayPos(const MONTHCAL_INFO
*infoPtr
, const SYSTEMTIME
*date
,
602 INT
*col
, INT
*row
, INT calIdx
)
604 SYSTEMTIME st
= infoPtr
->calendars
[calIdx
].month
;
608 first
= (MONTHCAL_CalculateDayOfWeek(&st
, FALSE
) + 6 - infoPtr
->firstDay
) % 7;
610 if (calIdx
== 0 || calIdx
== MONTHCAL_GetCalCount(infoPtr
)-1) {
611 const SYSTEMTIME
*cal
= &infoPtr
->calendars
[calIdx
].month
;
612 LONG cmp
= MONTHCAL_CompareMonths(date
, &st
);
616 *col
= (first
- MONTHCAL_MonthLength(date
->wMonth
, cal
->wYear
) + date
->wDay
) % 7;
621 /* next month calculation is same as for current, just add current month length */
623 first
+= MONTHCAL_MonthLength(cal
->wMonth
, cal
->wYear
);
626 *col
= (date
->wDay
+ first
) % 7;
627 *row
= (date
->wDay
+ first
- *col
) / 7;
630 /* returns bounding box for day in given position in given calendar */
631 static inline void MONTHCAL_GetDayRectI(const MONTHCAL_INFO
*infoPtr
, RECT
*r
,
632 INT col
, INT row
, INT calIdx
)
634 r
->left
= infoPtr
->calendars
[calIdx
].days
.left
+ col
* infoPtr
->width_increment
;
635 r
->right
= r
->left
+ infoPtr
->width_increment
;
636 r
->top
= infoPtr
->calendars
[calIdx
].days
.top
+ row
* infoPtr
->height_increment
;
637 r
->bottom
= r
->top
+ infoPtr
->textHeight
;
640 /* Returns bounding box for given date
642 * NOTE: when calendar index is unknown pass -1
644 static BOOL
MONTHCAL_GetDayRect(const MONTHCAL_INFO
*infoPtr
, const SYSTEMTIME
*date
, RECT
*r
, INT calIdx
)
648 if (!MONTHCAL_ValidateDate(date
))
656 INT cmp
= MONTHCAL_CompareMonths(date
, &infoPtr
->calendars
[0].month
);
662 cmp
= MONTHCAL_CompareMonths(date
, &infoPtr
->calendars
[MONTHCAL_GetCalCount(infoPtr
)-1].month
);
664 calIdx
= MONTHCAL_GetCalCount(infoPtr
)-1;
667 for (calIdx
= 1; calIdx
< MONTHCAL_GetCalCount(infoPtr
)-1; calIdx
++)
668 if (MONTHCAL_CompareMonths(date
, &infoPtr
->calendars
[calIdx
].month
) == 0)
674 MONTHCAL_GetDayPos(infoPtr
, date
, &col
, &row
, calIdx
);
675 MONTHCAL_GetDayRectI(infoPtr
, r
, col
, row
, calIdx
);
681 MONTHCAL_GetMonthRange(const MONTHCAL_INFO
*infoPtr
, DWORD flag
, SYSTEMTIME
*st
)
685 TRACE("flag=%d, st=%p\n", flag
, st
);
692 st
[0] = infoPtr
->calendars
[0].month
;
693 st
[1] = infoPtr
->calendars
[MONTHCAL_GetCalCount(infoPtr
)-1].month
;
695 if (st
[0].wMonth
== min_allowed_date
.wMonth
&&
696 st
[0].wYear
== min_allowed_date
.wYear
)
698 st
[0].wDay
= min_allowed_date
.wDay
;
702 MONTHCAL_CalculateDayOfWeek(&st
[0], TRUE
);
704 st
[1].wDay
= MONTHCAL_MonthLength(st
[1].wMonth
, st
[1].wYear
);
705 MONTHCAL_CalculateDayOfWeek(&st
[1], TRUE
);
708 range
= MONTHCAL_GetCalCount(infoPtr
);
715 MONTHCAL_GetMinDate(infoPtr
, &st
[0]);
716 MONTHCAL_GetMaxDate(infoPtr
, &st
[1]);
718 /* include two partially visible months */
719 range
= MONTHCAL_GetCalCount(infoPtr
) + 2;
723 WARN("Unknown flag value, got %d\n", flag
);
730 /* Focused day helper:
732 - set focused date to given value;
733 - reset to zero value if NULL passed;
734 - invalidate previous and new day rectangle only if needed.
736 Returns TRUE if focused day changed, FALSE otherwise.
738 static BOOL
MONTHCAL_SetDayFocus(MONTHCAL_INFO
*infoPtr
, const SYSTEMTIME
*st
)
744 /* there's nothing to do if it's the same date,
745 mouse move within same date rectangle case */
746 if(MONTHCAL_IsDateEqual(&infoPtr
->focusedSel
, st
)) return FALSE
;
748 /* invalidate old focused day */
749 if (MONTHCAL_GetDayRect(infoPtr
, &infoPtr
->focusedSel
, &r
, -1))
750 InvalidateRect(infoPtr
->hwndSelf
, &r
, FALSE
);
752 infoPtr
->focusedSel
= *st
;
755 /* On set invalidates new day, on reset clears previous focused day. */
756 if (MONTHCAL_GetDayRect(infoPtr
, &infoPtr
->focusedSel
, &r
, -1))
757 InvalidateRect(infoPtr
->hwndSelf
, &r
, FALSE
);
759 if(!st
&& MONTHCAL_ValidateDate(&infoPtr
->focusedSel
))
760 infoPtr
->focusedSel
= st_null
;
765 /* draw today boundary box for specified rectangle */
766 static void MONTHCAL_Circle(const MONTHCAL_INFO
*infoPtr
, HDC hdc
, const RECT
*r
)
768 HPEN old_pen
= SelectObject(hdc
, infoPtr
->pens
[PenRed
]);
771 old_brush
= SelectObject(hdc
, GetStockObject(NULL_BRUSH
));
772 Rectangle(hdc
, r
->left
, r
->top
, r
->right
, r
->bottom
);
774 SelectObject(hdc
, old_brush
);
775 SelectObject(hdc
, old_pen
);
778 /* Draw today day mark rectangle
780 * [I] hdc : context to draw in
781 * [I] date : day to mark with rectangle
784 static void MONTHCAL_CircleDay(const MONTHCAL_INFO
*infoPtr
, HDC hdc
,
785 const SYSTEMTIME
*date
)
789 MONTHCAL_GetDayRect(infoPtr
, date
, &r
, -1);
790 MONTHCAL_Circle(infoPtr
, hdc
, &r
);
793 static void MONTHCAL_DrawDay(const MONTHCAL_INFO
*infoPtr
, HDC hdc
, const SYSTEMTIME
*st
,
794 int bold
, const PAINTSTRUCT
*ps
)
796 static const WCHAR fmtW
[] = { '%','d',0 };
801 INT old_bkmode
, selection
;
803 /* no need to check styles: when selection is not valid, it is set to zero.
804 1 < day < 31, so everything is OK */
805 MONTHCAL_GetDayRect(infoPtr
, st
, &r
, -1);
806 if(!IntersectRect(&r_temp
, &(ps
->rcPaint
), &r
)) return;
808 if ((MONTHCAL_CompareDate(st
, &infoPtr
->minSel
) >= 0) &&
809 (MONTHCAL_CompareDate(st
, &infoPtr
->maxSel
) <= 0))
811 TRACE("%d %d %d\n", st
->wDay
, infoPtr
->minSel
.wDay
, infoPtr
->maxSel
.wDay
);
812 TRACE("%s\n", wine_dbgstr_rect(&r
));
813 oldCol
= SetTextColor(hdc
, infoPtr
->colors
[MCSC_MONTHBK
]);
814 oldBk
= SetBkColor(hdc
, infoPtr
->colors
[MCSC_TRAILINGTEXT
]);
815 FillRect(hdc
, &r
, infoPtr
->brushes
[BrushTitle
]);
822 SelectObject(hdc
, bold
? infoPtr
->hBoldFont
: infoPtr
->hFont
);
824 old_bkmode
= SetBkMode(hdc
, TRANSPARENT
);
825 wsprintfW(buf
, fmtW
, st
->wDay
);
826 DrawTextW(hdc
, buf
, -1, &r
, DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
);
827 SetBkMode(hdc
, old_bkmode
);
831 SetTextColor(hdc
, oldCol
);
832 SetBkColor(hdc
, oldBk
);
836 static void MONTHCAL_PaintButton(MONTHCAL_INFO
*infoPtr
, HDC hdc
, enum nav_direction button
)
838 HTHEME theme
= GetWindowTheme (infoPtr
->hwndSelf
);
839 RECT
*r
= button
== DIRECTION_FORWARD
? &infoPtr
->titlebtnnext
: &infoPtr
->titlebtnprev
;
840 BOOL pressed
= button
== DIRECTION_FORWARD
? infoPtr
->status
& MC_NEXTPRESSED
:
841 infoPtr
->status
& MC_PREVPRESSED
;
844 static const int states
[] = {
846 ABS_LEFTNORMAL
, ABS_LEFTPRESSED
, ABS_LEFTDISABLED
,
848 ABS_RIGHTNORMAL
, ABS_RIGHTPRESSED
, ABS_RIGHTDISABLED
850 int stateNum
= button
== DIRECTION_FORWARD
? 3 : 0;
855 if (infoPtr
->dwStyle
& WS_DISABLED
) stateNum
+= 2;
857 DrawThemeBackground (theme
, hdc
, SBP_ARROWBTN
, states
[stateNum
], r
, NULL
);
861 int style
= button
== DIRECTION_FORWARD
? DFCS_SCROLLRIGHT
: DFCS_SCROLLLEFT
;
863 style
|= DFCS_PUSHED
;
866 if (infoPtr
->dwStyle
& WS_DISABLED
) style
|= DFCS_INACTIVE
;
869 DrawFrameControl(hdc
, r
, DFC_SCROLL
, style
);
873 /* paint a title with buttons and month/year string */
874 static void MONTHCAL_PaintTitle(MONTHCAL_INFO
*infoPtr
, HDC hdc
, const PAINTSTRUCT
*ps
, INT calIdx
)
876 static const WCHAR mmmmW
[] = {'M','M','M','M',0};
877 static const WCHAR mmmW
[] = {'M','M','M',0};
878 static const WCHAR mmW
[] = {'M','M',0};
879 static const WCHAR fmtyearW
[] = {'%','l','d',0};
880 static const WCHAR fmtmmW
[] = {'%','0','2','d',0};
881 static const WCHAR fmtmW
[] = {'%','d',0};
882 RECT
*title
= &infoPtr
->calendars
[calIdx
].title
;
883 const SYSTEMTIME
*st
= &infoPtr
->calendars
[calIdx
].month
;
884 WCHAR monthW
[80], strW
[80], fmtW
[80], yearW
[6] /* valid year range is 1601-30827 */;
885 int yearoffset
, monthoffset
, shiftX
;
888 /* fill header box */
889 FillRect(hdc
, title
, infoPtr
->brushes
[BrushTitle
]);
891 /* month/year string */
892 SetBkColor(hdc
, infoPtr
->colors
[MCSC_TITLEBK
]);
893 SetTextColor(hdc
, infoPtr
->colors
[MCSC_TITLETEXT
]);
894 SelectObject(hdc
, infoPtr
->hBoldFont
);
896 /* draw formatted date string */
897 GetDateFormatW(LOCALE_USER_DEFAULT
, DATE_YEARMONTH
, st
, NULL
, strW
, countof(strW
));
898 DrawTextW(hdc
, strW
, strlenW(strW
), title
, DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
);
900 GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_SYEARMONTH
, fmtW
, countof(fmtW
));
901 wsprintfW(yearW
, fmtyearW
, st
->wYear
);
903 /* month is trickier as it's possible to have different format pictures, we'll
904 test for M, MM, MMM, and MMMM */
905 if (strstrW(fmtW
, mmmmW
))
906 GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_SMONTHNAME1
+st
->wMonth
-1, monthW
, countof(monthW
));
907 else if (strstrW(fmtW
, mmmW
))
908 GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_SABBREVMONTHNAME1
+st
->wMonth
-1, monthW
, countof(monthW
));
909 else if (strstrW(fmtW
, mmW
))
910 wsprintfW(monthW
, fmtmmW
, st
->wMonth
);
912 wsprintfW(monthW
, fmtmW
, st
->wMonth
);
914 /* update hit boxes */
916 while (strW
[yearoffset
])
918 if (!strncmpW(&strW
[yearoffset
], yearW
, strlenW(yearW
)))
924 while (strW
[monthoffset
])
926 if (!strncmpW(&strW
[monthoffset
], monthW
, strlenW(monthW
)))
931 /* for left limits use offsets */
934 GetTextExtentPoint32W(hdc
, strW
, yearoffset
, &sz
);
935 infoPtr
->calendars
[calIdx
].titleyear
.left
= sz
.cx
;
939 GetTextExtentPoint32W(hdc
, strW
, monthoffset
, &sz
);
940 infoPtr
->calendars
[calIdx
].titlemonth
.left
= sz
.cx
;
942 /* for right limits use actual string parts lengths */
943 GetTextExtentPoint32W(hdc
, &strW
[yearoffset
], strlenW(yearW
), &sz
);
944 infoPtr
->calendars
[calIdx
].titleyear
.right
= infoPtr
->calendars
[calIdx
].titleyear
.left
+ sz
.cx
;
946 GetTextExtentPoint32W(hdc
, monthW
, strlenW(monthW
), &sz
);
947 infoPtr
->calendars
[calIdx
].titlemonth
.right
= infoPtr
->calendars
[calIdx
].titlemonth
.left
+ sz
.cx
;
949 /* Finally translate rectangles to match center aligned string,
950 hit rectangles are relative to title rectangle before translation. */
951 GetTextExtentPoint32W(hdc
, strW
, strlenW(strW
), &sz
);
952 shiftX
= (title
->right
- title
->left
- sz
.cx
) / 2 + title
->left
;
953 OffsetRect(&infoPtr
->calendars
[calIdx
].titleyear
, shiftX
, 0);
954 OffsetRect(&infoPtr
->calendars
[calIdx
].titlemonth
, shiftX
, 0);
957 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO
*infoPtr
, HDC hdc
, const PAINTSTRUCT
*ps
, INT calIdx
)
959 const SYSTEMTIME
*date
= &infoPtr
->calendars
[calIdx
].month
;
960 static const WCHAR fmt_weekW
[] = { '%','d',0 };
961 INT mindays
, weeknum
, weeknum1
, startofprescal
;
968 if (!(infoPtr
->dwStyle
& MCS_WEEKNUMBERS
)) return;
970 MONTHCAL_GetMinDate(infoPtr
, &st
);
971 startofprescal
= st
.wDay
;
974 prev_month
= date
->wMonth
- 1;
975 if(prev_month
== 0) prev_month
= 12;
978 Rules what week to call the first week of a new year:
979 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
980 The week containing Jan 1 is the first week of year
981 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
982 First week of year must contain 4 days of the new year
983 LOCALE_IFIRSTWEEKOFYEAR == 1 (what countries?)
984 The first week of the year must contain only days of the new year
986 GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_IFIRSTWEEKOFYEAR
, buf
, countof(buf
));
987 weeknum
= atoiW(buf
);
997 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum
);
1001 if (date
->wMonth
== 1)
1003 /* calculate all those exceptions for January */
1004 st
.wDay
= st
.wMonth
= 1;
1005 weeknum1
= MONTHCAL_CalculateDayOfWeek(&st
, FALSE
);
1006 if ((infoPtr
->firstDay
- weeknum1
) % 7 > mindays
)
1011 for(i
= 0; i
< 11; i
++)
1012 weeknum
+= MONTHCAL_MonthLength(i
+1, date
->wYear
- 1);
1014 weeknum
+= startofprescal
+ 7;
1017 weeknum1
= MONTHCAL_CalculateDayOfWeek(&st
, FALSE
);
1018 if ((infoPtr
->firstDay
- weeknum1
) % 7 > mindays
) weeknum
++;
1024 for(i
= 0; i
< prev_month
- 1; i
++)
1025 weeknum
+= MONTHCAL_MonthLength(i
+1, date
->wYear
);
1027 weeknum
+= startofprescal
+ 7;
1029 st
.wDay
= st
.wMonth
= 1;
1030 weeknum1
= MONTHCAL_CalculateDayOfWeek(&st
, FALSE
);
1031 if ((infoPtr
->firstDay
- weeknum1
) % 7 > mindays
) weeknum
++;
1034 r
= infoPtr
->calendars
[calIdx
].weeknums
;
1036 /* erase whole week numbers area */
1037 FillRect(hdc
, &r
, infoPtr
->brushes
[BrushMonth
]);
1038 SetTextColor(hdc
, infoPtr
->colors
[MCSC_TITLEBK
]);
1040 /* reduce rectangle to one week number */
1041 r
.bottom
= r
.top
+ infoPtr
->height_increment
;
1043 for(i
= 0; i
< 6; i
++) {
1044 if((i
== 0) && (weeknum
> 50))
1046 wsprintfW(buf
, fmt_weekW
, weeknum
);
1049 else if((i
== 5) && (weeknum
> 47))
1051 wsprintfW(buf
, fmt_weekW
, 1);
1054 wsprintfW(buf
, fmt_weekW
, weeknum
+ i
);
1056 DrawTextW(hdc
, buf
, -1, &r
, DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
);
1057 OffsetRect(&r
, 0, infoPtr
->height_increment
);
1060 /* line separator for week numbers column */
1061 old_pen
= SelectObject(hdc
, infoPtr
->pens
[PenText
]);
1062 MoveToEx(hdc
, infoPtr
->calendars
[calIdx
].weeknums
.right
, infoPtr
->calendars
[calIdx
].weeknums
.top
+ 3 , NULL
);
1063 LineTo(hdc
, infoPtr
->calendars
[calIdx
].weeknums
.right
, infoPtr
->calendars
[calIdx
].weeknums
.bottom
);
1064 SelectObject(hdc
, old_pen
);
1067 /* bottom today date */
1068 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO
*infoPtr
, HDC hdc
, const PAINTSTRUCT
*ps
)
1070 static const WCHAR fmt_todayW
[] = { '%','s',' ','%','s',0 };
1071 WCHAR buf_todayW
[30], buf_dateW
[20], buf
[80];
1072 RECT text_rect
, box_rect
;
1076 if(infoPtr
->dwStyle
& MCS_NOTODAY
) return;
1078 LoadStringW(COMCTL32_hModule
, IDM_TODAY
, buf_todayW
, countof(buf_todayW
));
1079 col
= infoPtr
->dwStyle
& MCS_NOTODAYCIRCLE
? 0 : 1;
1080 if (infoPtr
->dwStyle
& MCS_WEEKNUMBERS
) col
--;
1081 /* label is located below first calendar last row */
1082 MONTHCAL_GetDayRectI(infoPtr
, &text_rect
, col
, 6, infoPtr
->dim
.cx
* infoPtr
->dim
.cy
- infoPtr
->dim
.cx
);
1083 box_rect
= text_rect
;
1085 GetDateFormatW(LOCALE_USER_DEFAULT
, DATE_SHORTDATE
, &infoPtr
->todaysDate
, NULL
,
1086 buf_dateW
, countof(buf_dateW
));
1087 old_font
= SelectObject(hdc
, infoPtr
->hBoldFont
);
1088 SetTextColor(hdc
, infoPtr
->colors
[MCSC_TEXT
]);
1090 wsprintfW(buf
, fmt_todayW
, buf_todayW
, buf_dateW
);
1091 DrawTextW(hdc
, buf
, -1, &text_rect
, DT_CALCRECT
| DT_LEFT
| DT_VCENTER
| DT_SINGLELINE
);
1092 DrawTextW(hdc
, buf
, -1, &text_rect
, DT_LEFT
| DT_VCENTER
| DT_SINGLELINE
);
1094 if(!(infoPtr
->dwStyle
& MCS_NOTODAYCIRCLE
)) {
1095 OffsetRect(&box_rect
, -infoPtr
->width_increment
, 0);
1096 MONTHCAL_Circle(infoPtr
, hdc
, &box_rect
);
1099 SelectObject(hdc
, old_font
);
1102 /* today mark + focus */
1103 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO
*infoPtr
, HDC hdc
, const PAINTSTRUCT
*ps
)
1105 /* circle today date if only it's in fully visible month */
1106 if (!(infoPtr
->dwStyle
& MCS_NOTODAYCIRCLE
))
1110 for (i
= 0; i
< MONTHCAL_GetCalCount(infoPtr
); i
++)
1111 if (!MONTHCAL_CompareMonths(&infoPtr
->todaysDate
, &infoPtr
->calendars
[i
].month
))
1113 MONTHCAL_CircleDay(infoPtr
, hdc
, &infoPtr
->todaysDate
);
1118 if (!MONTHCAL_IsDateEqual(&infoPtr
->focusedSel
, &st_null
))
1121 MONTHCAL_GetDayRect(infoPtr
, &infoPtr
->focusedSel
, &r
, -1);
1122 DrawFocusRect(hdc
, &r
);
1126 /* months before first calendar month and after last calendar month */
1127 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO
*infoPtr
, HDC hdc
, const PAINTSTRUCT
*ps
)
1131 SYSTEMTIME st_max
, st
;
1133 if (infoPtr
->dwStyle
& MCS_NOTRAILINGDATES
) return;
1135 SetTextColor(hdc
, infoPtr
->colors
[MCSC_TRAILINGTEXT
]);
1137 /* draw prev month */
1138 MONTHCAL_GetMinDate(infoPtr
, &st
);
1139 mask
= 1 << (st
.wDay
-1);
1140 /* December and January both 31 days long, so no worries if wrapped */
1141 length
= MONTHCAL_MonthLength(infoPtr
->calendars
[0].month
.wMonth
- 1,
1142 infoPtr
->calendars
[0].month
.wYear
);
1144 while(st
.wDay
<= length
)
1146 MONTHCAL_DrawDay(infoPtr
, hdc
, &st
, infoPtr
->monthdayState
[index
] & mask
, ps
);
1151 /* draw next month */
1152 st
= infoPtr
->calendars
[MONTHCAL_GetCalCount(infoPtr
)-1].month
;
1154 MONTHCAL_GetNextMonth(&st
);
1155 MONTHCAL_GetMaxDate(infoPtr
, &st_max
);
1157 index
= MONTHCAL_GetMonthRange(infoPtr
, GMR_DAYSTATE
, 0)-1;
1158 while(st
.wDay
<= st_max
.wDay
)
1160 MONTHCAL_DrawDay(infoPtr
, hdc
, &st
, infoPtr
->monthdayState
[index
] & mask
, ps
);
1166 static int get_localized_dayname(const MONTHCAL_INFO
*infoPtr
, unsigned int day
, WCHAR
*buff
, unsigned int count
)
1170 if (infoPtr
->dwStyle
& MCS_SHORTDAYSOFWEEK
)
1171 lctype
= LOCALE_SSHORTESTDAYNAME1
+ day
;
1173 lctype
= LOCALE_SABBREVDAYNAME1
+ day
;
1175 return GetLocaleInfoW(LOCALE_USER_DEFAULT
, lctype
, buff
, count
);
1178 /* paint a calendar area */
1179 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO
*infoPtr
, HDC hdc
, const PAINTSTRUCT
*ps
, INT calIdx
)
1181 const SYSTEMTIME
*date
= &infoPtr
->calendars
[calIdx
].month
;
1184 RECT r
, fill_bk_rect
;
1190 /* fill whole days area - from week days area to today note rectangle */
1191 fill_bk_rect
= infoPtr
->calendars
[calIdx
].wdays
;
1192 fill_bk_rect
.bottom
= infoPtr
->calendars
[calIdx
].days
.bottom
+
1193 (infoPtr
->todayrect
.bottom
- infoPtr
->todayrect
.top
);
1195 FillRect(hdc
, &fill_bk_rect
, infoPtr
->brushes
[BrushMonth
]);
1197 /* draw line under day abbreviations */
1198 old_pen
= SelectObject(hdc
, infoPtr
->pens
[PenText
]);
1199 MoveToEx(hdc
, infoPtr
->calendars
[calIdx
].days
.left
+ 3,
1200 infoPtr
->calendars
[calIdx
].title
.bottom
+ infoPtr
->textHeight
+ 1, NULL
);
1201 LineTo(hdc
, infoPtr
->calendars
[calIdx
].days
.right
- 3,
1202 infoPtr
->calendars
[calIdx
].title
.bottom
+ infoPtr
->textHeight
+ 1);
1203 SelectObject(hdc
, old_pen
);
1205 infoPtr
->calendars
[calIdx
].wdays
.left
= infoPtr
->calendars
[calIdx
].days
.left
=
1206 infoPtr
->calendars
[calIdx
].weeknums
.right
;
1208 /* draw day abbreviations */
1209 SelectObject(hdc
, infoPtr
->hFont
);
1210 SetBkColor(hdc
, infoPtr
->colors
[MCSC_MONTHBK
]);
1211 SetTextColor(hdc
, infoPtr
->colors
[MCSC_TITLEBK
]);
1212 /* rectangle to draw a single day abbreviation within */
1213 r
= infoPtr
->calendars
[calIdx
].wdays
;
1214 r
.right
= r
.left
+ infoPtr
->width_increment
;
1216 i
= infoPtr
->firstDay
;
1217 for(j
= 0; j
< 7; j
++) {
1218 get_localized_dayname(infoPtr
, (i
+ j
+ 6) % 7, buf
, countof(buf
));
1219 DrawTextW(hdc
, buf
, strlenW(buf
), &r
, DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
);
1220 OffsetRect(&r
, infoPtr
->width_increment
, 0);
1223 /* draw current month */
1224 SetTextColor(hdc
, infoPtr
->colors
[MCSC_TEXT
]);
1228 length
= MONTHCAL_MonthLength(date
->wMonth
, date
->wYear
);
1229 while(st
.wDay
<= length
)
1231 MONTHCAL_DrawDay(infoPtr
, hdc
, &st
, infoPtr
->monthdayState
[calIdx
+1] & mask
, ps
);
1237 static void MONTHCAL_Refresh(MONTHCAL_INFO
*infoPtr
, HDC hdc
, const PAINTSTRUCT
*ps
)
1239 COLORREF old_text_clr
, old_bk_clr
;
1243 old_text_clr
= SetTextColor(hdc
, comctl32_color
.clrWindowText
);
1244 old_bk_clr
= GetBkColor(hdc
);
1245 old_font
= GetCurrentObject(hdc
, OBJ_FONT
);
1247 for (i
= 0; i
< MONTHCAL_GetCalCount(infoPtr
); i
++)
1249 RECT
*title
= &infoPtr
->calendars
[i
].title
;
1252 /* draw title, redraw all its elements */
1253 if (IntersectRect(&r
, &(ps
->rcPaint
), title
))
1254 MONTHCAL_PaintTitle(infoPtr
, hdc
, ps
, i
);
1256 /* draw calendar area */
1257 UnionRect(&r
, &infoPtr
->calendars
[i
].wdays
, &infoPtr
->todayrect
);
1258 if (IntersectRect(&r
, &(ps
->rcPaint
), &r
))
1259 MONTHCAL_PaintCalendar(infoPtr
, hdc
, ps
, i
);
1262 MONTHCAL_PaintWeeknumbers(infoPtr
, hdc
, ps
, i
);
1265 /* partially visible months */
1266 MONTHCAL_PaintLeadTrailMonths(infoPtr
, hdc
, ps
);
1268 /* focus and today rectangle */
1269 MONTHCAL_PaintFocusAndCircle(infoPtr
, hdc
, ps
);
1271 /* today at the bottom left */
1272 MONTHCAL_PaintTodayTitle(infoPtr
, hdc
, ps
);
1274 /* navigation buttons */
1275 MONTHCAL_PaintButton(infoPtr
, hdc
, DIRECTION_BACKWARD
);
1276 MONTHCAL_PaintButton(infoPtr
, hdc
, DIRECTION_FORWARD
);
1278 /* restore context */
1279 SetBkColor(hdc
, old_bk_clr
);
1280 SelectObject(hdc
, old_font
);
1281 SetTextColor(hdc
, old_text_clr
);
1285 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO
*infoPtr
, RECT
*rect
)
1287 TRACE("rect %p\n", rect
);
1289 if(!rect
) return FALSE
;
1291 *rect
= infoPtr
->calendars
[0].title
;
1292 rect
->bottom
= infoPtr
->calendars
[0].days
.bottom
+ infoPtr
->todayrect
.bottom
-
1293 infoPtr
->todayrect
.top
;
1295 AdjustWindowRect(rect
, infoPtr
->dwStyle
, FALSE
);
1297 /* minimal rectangle is zero based */
1298 OffsetRect(rect
, -rect
->left
, -rect
->top
);
1300 TRACE("%s\n", wine_dbgstr_rect(rect
));
1306 MONTHCAL_GetColor(const MONTHCAL_INFO
*infoPtr
, UINT index
)
1308 TRACE("%p, %d\n", infoPtr
, index
);
1310 if (index
> MCSC_TRAILINGTEXT
) return -1;
1311 return infoPtr
->colors
[index
];
1315 MONTHCAL_SetColor(MONTHCAL_INFO
*infoPtr
, UINT index
, COLORREF color
)
1317 enum CachedBrush type
;
1320 TRACE("%p, %d: color %08x\n", infoPtr
, index
, color
);
1322 if (index
> MCSC_TRAILINGTEXT
) return -1;
1324 prev
= infoPtr
->colors
[index
];
1325 infoPtr
->colors
[index
] = color
;
1327 /* update cached brush */
1330 case MCSC_BACKGROUND
:
1331 type
= BrushBackground
;
1343 if (type
!= BrushLast
)
1345 DeleteObject(infoPtr
->brushes
[type
]);
1346 infoPtr
->brushes
[type
] = CreateSolidBrush(color
);
1349 /* update cached pen */
1350 if (index
== MCSC_TEXT
)
1352 DeleteObject(infoPtr
->pens
[PenText
]);
1353 infoPtr
->pens
[PenText
] = CreatePen(PS_SOLID
, 1, infoPtr
->colors
[index
]);
1356 InvalidateRect(infoPtr
->hwndSelf
, NULL
, index
== MCSC_BACKGROUND
);
1361 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO
*infoPtr
)
1366 return infoPtr
->delta
;
1368 return MONTHCAL_GetMonthRange(infoPtr
, GMR_VISIBLE
, NULL
);
1373 MONTHCAL_SetMonthDelta(MONTHCAL_INFO
*infoPtr
, INT delta
)
1375 INT prev
= infoPtr
->delta
;
1377 TRACE("delta %d\n", delta
);
1379 infoPtr
->delta
= delta
;
1384 static inline LRESULT
1385 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO
*infoPtr
)
1389 /* convert from SYSTEMTIME to locale format */
1390 day
= (infoPtr
->firstDay
>= 0) ? (infoPtr
->firstDay
+6)%7 : infoPtr
->firstDay
;
1392 return MAKELONG(day
, infoPtr
->firstDaySet
);
1396 /* Sets the first day of the week that will appear in the control
1400 * [I] infoPtr : valid pointer to control data
1401 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1405 * Low word contains previous first day,
1406 * high word indicates was first day forced with this message before or is
1407 * locale defined (TRUE - was forced, FALSE - wasn't).
1409 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1410 * FIXME: we need more error checking here
1413 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO
*infoPtr
, INT day
)
1415 LRESULT prev
= MONTHCAL_GetFirstDayOfWeek(infoPtr
);
1424 GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_IFIRSTDAYOFWEEK
, buf
, countof(buf
));
1425 TRACE("%s %d\n", debugstr_w(buf
), strlenW(buf
));
1427 new_day
= atoiW(buf
);
1429 infoPtr
->firstDaySet
= FALSE
;
1433 new_day
= 6; /* max first day allowed */
1434 infoPtr
->firstDaySet
= TRUE
;
1438 /* Native behaviour for that case is broken: invalid date number >31
1439 got displayed at (0,0) position, current month starts always from
1440 (1,0) position. Should be implemented here as well only if there's
1441 nothing else to do. */
1443 FIXME("No bug compatibility for day=%d\n", day
);
1446 infoPtr
->firstDaySet
= TRUE
;
1449 /* convert from locale to SYSTEMTIME format */
1450 infoPtr
->firstDay
= (new_day
>= 0) ? (++new_day
) % 7 : new_day
;
1452 InvalidateRect(infoPtr
->hwndSelf
, NULL
, FALSE
);
1458 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO
*infoPtr
)
1460 return(infoPtr
->todayrect
.right
- infoPtr
->todayrect
.left
);
1464 MONTHCAL_SetRange(MONTHCAL_INFO
*infoPtr
, SHORT limits
, SYSTEMTIME
*range
)
1466 FILETIME ft_min
, ft_max
;
1468 TRACE("%x %p\n", limits
, range
);
1470 if ((limits
& GDTR_MIN
&& !MONTHCAL_ValidateDate(&range
[0])) ||
1471 (limits
& GDTR_MAX
&& !MONTHCAL_ValidateDate(&range
[1])))
1474 infoPtr
->rangeValid
= 0;
1475 infoPtr
->minDate
= infoPtr
->maxDate
= st_null
;
1477 if (limits
& GDTR_MIN
)
1479 if (!MONTHCAL_ValidateTime(&range
[0]))
1480 MONTHCAL_CopyTime(&infoPtr
->todaysDate
, &range
[0]);
1482 infoPtr
->minDate
= range
[0];
1483 infoPtr
->rangeValid
|= GDTR_MIN
;
1485 if (limits
& GDTR_MAX
)
1487 if (!MONTHCAL_ValidateTime(&range
[1]))
1488 MONTHCAL_CopyTime(&infoPtr
->todaysDate
, &range
[1]);
1490 infoPtr
->maxDate
= range
[1];
1491 infoPtr
->rangeValid
|= GDTR_MAX
;
1494 /* Only one limit set - we are done */
1495 if ((infoPtr
->rangeValid
& (GDTR_MIN
| GDTR_MAX
)) != (GDTR_MIN
| GDTR_MAX
))
1498 SystemTimeToFileTime(&infoPtr
->maxDate
, &ft_max
);
1499 SystemTimeToFileTime(&infoPtr
->minDate
, &ft_min
);
1501 if (CompareFileTime(&ft_min
, &ft_max
) >= 0)
1503 if ((limits
& (GDTR_MIN
| GDTR_MAX
)) == (GDTR_MIN
| GDTR_MAX
))
1505 /* Native swaps limits only when both limits are being set. */
1506 SYSTEMTIME st_tmp
= infoPtr
->minDate
;
1507 infoPtr
->minDate
= infoPtr
->maxDate
;
1508 infoPtr
->maxDate
= st_tmp
;
1512 /* reset the other limit */
1513 if (limits
& GDTR_MIN
) infoPtr
->maxDate
= st_null
;
1514 if (limits
& GDTR_MAX
) infoPtr
->minDate
= st_null
;
1515 infoPtr
->rangeValid
&= limits
& GDTR_MIN
? ~GDTR_MAX
: ~GDTR_MIN
;
1524 MONTHCAL_GetRange(const MONTHCAL_INFO
*infoPtr
, SYSTEMTIME
*range
)
1526 TRACE("%p\n", range
);
1528 if (!range
) return 0;
1530 range
[1] = infoPtr
->maxDate
;
1531 range
[0] = infoPtr
->minDate
;
1533 return infoPtr
->rangeValid
;
1538 MONTHCAL_SetDayState(const MONTHCAL_INFO
*infoPtr
, INT months
, MONTHDAYSTATE
*states
)
1540 TRACE("%p %d %p\n", infoPtr
, months
, states
);
1542 if (!(infoPtr
->dwStyle
& MCS_DAYSTATE
)) return 0;
1543 if (months
!= MONTHCAL_GetMonthRange(infoPtr
, GMR_DAYSTATE
, 0)) return 0;
1545 memcpy(infoPtr
->monthdayState
, states
, months
*sizeof(MONTHDAYSTATE
));
1551 MONTHCAL_GetCurSel(const MONTHCAL_INFO
*infoPtr
, SYSTEMTIME
*curSel
)
1553 TRACE("%p\n", curSel
);
1554 if(!curSel
) return FALSE
;
1555 if(infoPtr
->dwStyle
& MCS_MULTISELECT
) return FALSE
;
1557 *curSel
= infoPtr
->minSel
;
1558 TRACE("%d/%d/%d\n", curSel
->wYear
, curSel
->wMonth
, curSel
->wDay
);
1563 MONTHCAL_SetCurSel(MONTHCAL_INFO
*infoPtr
, SYSTEMTIME
*curSel
)
1565 SYSTEMTIME prev
= infoPtr
->minSel
, selection
;
1569 TRACE("%p\n", curSel
);
1570 if(!curSel
) return FALSE
;
1571 if(infoPtr
->dwStyle
& MCS_MULTISELECT
) return FALSE
;
1573 if(!MONTHCAL_ValidateDate(curSel
)) return FALSE
;
1574 /* exit earlier if selection equals current */
1575 if (MONTHCAL_IsDateEqual(&infoPtr
->minSel
, curSel
)) return TRUE
;
1577 selection
= *curSel
;
1578 selection
.wHour
= selection
.wMinute
= selection
.wSecond
= selection
.wMilliseconds
= 0;
1579 MONTHCAL_CalculateDayOfWeek(&selection
, TRUE
);
1581 if(!MONTHCAL_IsDateInValidRange(infoPtr
, &selection
, FALSE
)) return FALSE
;
1583 /* scroll calendars only if we have to */
1584 diff
= MONTHCAL_MonthDiff(&infoPtr
->calendars
[MONTHCAL_GetCalCount(infoPtr
)-1].month
, curSel
);
1587 diff
= MONTHCAL_MonthDiff(&infoPtr
->calendars
[0].month
, curSel
);
1588 if (diff
> 0) diff
= 0;
1595 for (i
= 0; i
< MONTHCAL_GetCalCount(infoPtr
); i
++)
1596 MONTHCAL_GetMonth(&infoPtr
->calendars
[i
].month
, diff
);
1599 /* we need to store time part as it is */
1600 selection
= *curSel
;
1601 MONTHCAL_CalculateDayOfWeek(&selection
, TRUE
);
1602 infoPtr
->minSel
= infoPtr
->maxSel
= selection
;
1604 /* if selection is still in current month, reduce rectangle */
1606 prev
.wDay
= curSel
->wDay
;
1607 if (MONTHCAL_IsDateEqual(&prev
, curSel
))
1612 MONTHCAL_GetDayRect(infoPtr
, &prev
, &r_prev
, -1);
1613 MONTHCAL_GetDayRect(infoPtr
, curSel
, &r_new
, -1);
1615 InvalidateRect(infoPtr
->hwndSelf
, &r_prev
, FALSE
);
1616 InvalidateRect(infoPtr
->hwndSelf
, &r_new
, FALSE
);
1619 InvalidateRect(infoPtr
->hwndSelf
, NULL
, FALSE
);
1626 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO
*infoPtr
)
1628 return infoPtr
->maxSelCount
;
1633 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO
*infoPtr
, INT max
)
1637 if(!(infoPtr
->dwStyle
& MCS_MULTISELECT
)) return FALSE
;
1638 if(max
<= 0) return FALSE
;
1640 infoPtr
->maxSelCount
= max
;
1647 MONTHCAL_GetSelRange(const MONTHCAL_INFO
*infoPtr
, SYSTEMTIME
*range
)
1649 TRACE("%p\n", range
);
1651 if(!range
) return FALSE
;
1653 if(infoPtr
->dwStyle
& MCS_MULTISELECT
)
1655 range
[1] = infoPtr
->maxSel
;
1656 range
[0] = infoPtr
->minSel
;
1657 TRACE("[min,max]=[%d %d]\n", infoPtr
->minSel
.wDay
, infoPtr
->maxSel
.wDay
);
1666 MONTHCAL_SetSelRange(MONTHCAL_INFO
*infoPtr
, SYSTEMTIME
*range
)
1668 SYSTEMTIME old_range
[2];
1671 TRACE("%p\n", range
);
1673 if(!range
|| !(infoPtr
->dwStyle
& MCS_MULTISELECT
)) return FALSE
;
1675 /* adjust timestamps */
1676 if(!MONTHCAL_ValidateTime(&range
[0])) MONTHCAL_CopyTime(&infoPtr
->todaysDate
, &range
[0]);
1677 if(!MONTHCAL_ValidateTime(&range
[1])) MONTHCAL_CopyTime(&infoPtr
->todaysDate
, &range
[1]);
1679 /* maximum range exceeded */
1680 if(!MONTHCAL_IsSelRangeValid(infoPtr
, &range
[0], &range
[1], NULL
)) return FALSE
;
1682 old_range
[0] = infoPtr
->minSel
;
1683 old_range
[1] = infoPtr
->maxSel
;
1685 /* swap if min > max */
1686 if(MONTHCAL_CompareSystemTime(&range
[0], &range
[1]) <= 0)
1688 infoPtr
->minSel
= range
[0];
1689 infoPtr
->maxSel
= range
[1];
1693 infoPtr
->minSel
= range
[1];
1694 infoPtr
->maxSel
= range
[0];
1697 diff
= MONTHCAL_MonthDiff(&infoPtr
->calendars
[MONTHCAL_GetCalCount(infoPtr
)-1].month
, &infoPtr
->maxSel
);
1700 diff
= MONTHCAL_MonthDiff(&infoPtr
->calendars
[0].month
, &infoPtr
->maxSel
);
1701 if (diff
> 0) diff
= 0;
1708 for (i
= 0; i
< MONTHCAL_GetCalCount(infoPtr
); i
++)
1709 MONTHCAL_GetMonth(&infoPtr
->calendars
[i
].month
, diff
);
1712 /* update day of week */
1713 MONTHCAL_CalculateDayOfWeek(&infoPtr
->minSel
, TRUE
);
1714 MONTHCAL_CalculateDayOfWeek(&infoPtr
->maxSel
, TRUE
);
1716 /* redraw if bounds changed */
1717 /* FIXME: no actual need to redraw everything */
1718 if(!MONTHCAL_IsDateEqual(&old_range
[0], &range
[0]) ||
1719 !MONTHCAL_IsDateEqual(&old_range
[1], &range
[1]))
1721 InvalidateRect(infoPtr
->hwndSelf
, NULL
, FALSE
);
1724 TRACE("[min,max]=[%d %d]\n", infoPtr
->minSel
.wDay
, infoPtr
->maxSel
.wDay
);
1730 MONTHCAL_GetToday(const MONTHCAL_INFO
*infoPtr
, SYSTEMTIME
*today
)
1732 TRACE("%p\n", today
);
1734 if(!today
) return FALSE
;
1735 *today
= infoPtr
->todaysDate
;
1739 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1743 * TRUE - today date changed
1744 * FALSE - today date isn't changed
1747 MONTHCAL_UpdateToday(MONTHCAL_INFO
*infoPtr
, const SYSTEMTIME
*today
)
1751 if (MONTHCAL_IsDateEqual(today
, &infoPtr
->todaysDate
))
1754 /* Invalidate old and new today day rectangle, and today label. */
1755 if (MONTHCAL_GetDayRect(infoPtr
, &infoPtr
->todaysDate
, &rect
, -1))
1756 InvalidateRect(infoPtr
->hwndSelf
, &rect
, FALSE
);
1758 if (MONTHCAL_GetDayRect(infoPtr
, today
, &rect
, -1))
1759 InvalidateRect(infoPtr
->hwndSelf
, &rect
, FALSE
);
1761 infoPtr
->todaysDate
= *today
;
1763 InvalidateRect(infoPtr
->hwndSelf
, &infoPtr
->todayrect
, FALSE
);
1767 /* MCM_SETTODAT handler */
1769 MONTHCAL_SetToday(MONTHCAL_INFO
*infoPtr
, const SYSTEMTIME
*today
)
1771 TRACE("%p\n", today
);
1775 /* remember if date was set successfully */
1776 if (MONTHCAL_UpdateToday(infoPtr
, today
)) infoPtr
->todaySet
= TRUE
;
1782 /* returns calendar index containing specified point, or -1 if it's background */
1783 static INT
MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO
*infoPtr
, const POINT
*pt
)
1788 for (i
= 0; i
< MONTHCAL_GetCalCount(infoPtr
); i
++)
1790 /* whole bounding rectangle allows some optimization to compute */
1791 r
.left
= infoPtr
->calendars
[i
].title
.left
;
1792 r
.top
= infoPtr
->calendars
[i
].title
.top
;
1793 r
.bottom
= infoPtr
->calendars
[i
].days
.bottom
;
1794 r
.right
= infoPtr
->calendars
[i
].days
.right
;
1796 if (PtInRect(&r
, *pt
)) return i
;
1802 static inline UINT
fill_hittest_info(const MCHITTESTINFO
*src
, MCHITTESTINFO
*dest
)
1804 dest
->uHit
= src
->uHit
;
1807 if (dest
->cbSize
== sizeof(MCHITTESTINFO
))
1808 memcpy(&dest
->rc
, &src
->rc
, sizeof(MCHITTESTINFO
) - MCHITTESTINFO_V1_SIZE
);
1814 MONTHCAL_HitTest(const MONTHCAL_INFO
*infoPtr
, MCHITTESTINFO
*lpht
)
1816 MCHITTESTINFO htinfo
;
1817 SYSTEMTIME
*ht_month
;
1820 if(!lpht
|| lpht
->cbSize
< MCHITTESTINFO_V1_SIZE
) return -1;
1822 htinfo
.st
= st_null
;
1824 /* we should preserve passed fields if hit area doesn't need them */
1825 if (lpht
->cbSize
== sizeof(MCHITTESTINFO
))
1826 memcpy(&htinfo
.rc
, &lpht
->rc
, sizeof(MCHITTESTINFO
) - MCHITTESTINFO_V1_SIZE
);
1828 /* guess in what calendar we are */
1829 calIdx
= MONTHCAL_GetCalendarFromPoint(infoPtr
, &lpht
->pt
);
1832 if (PtInRect(&infoPtr
->todayrect
, lpht
->pt
))
1834 htinfo
.uHit
= MCHT_TODAYLINK
;
1835 htinfo
.rc
= infoPtr
->todayrect
;
1838 /* outside of calendar area? What's left must be background :-) */
1839 htinfo
.uHit
= MCHT_CALENDARBK
;
1841 return fill_hittest_info(&htinfo
, lpht
);
1844 /* are we in the header? */
1845 if (PtInRect(&infoPtr
->calendars
[calIdx
].title
, lpht
->pt
)) {
1846 /* FIXME: buttons hittesting could be optimized cause maximum
1847 two calendars have buttons */
1848 if (calIdx
== 0 && PtInRect(&infoPtr
->titlebtnprev
, lpht
->pt
))
1850 htinfo
.uHit
= MCHT_TITLEBTNPREV
;
1851 htinfo
.rc
= infoPtr
->titlebtnprev
;
1853 else if (PtInRect(&infoPtr
->titlebtnnext
, lpht
->pt
))
1855 htinfo
.uHit
= MCHT_TITLEBTNNEXT
;
1856 htinfo
.rc
= infoPtr
->titlebtnnext
;
1858 else if (PtInRect(&infoPtr
->calendars
[calIdx
].titlemonth
, lpht
->pt
))
1860 htinfo
.uHit
= MCHT_TITLEMONTH
;
1861 htinfo
.rc
= infoPtr
->calendars
[calIdx
].titlemonth
;
1862 htinfo
.iOffset
= calIdx
;
1864 else if (PtInRect(&infoPtr
->calendars
[calIdx
].titleyear
, lpht
->pt
))
1866 htinfo
.uHit
= MCHT_TITLEYEAR
;
1867 htinfo
.rc
= infoPtr
->calendars
[calIdx
].titleyear
;
1868 htinfo
.iOffset
= calIdx
;
1872 htinfo
.uHit
= MCHT_TITLE
;
1873 htinfo
.rc
= infoPtr
->calendars
[calIdx
].title
;
1874 htinfo
.iOffset
= calIdx
;
1877 return fill_hittest_info(&htinfo
, lpht
);
1880 ht_month
= &infoPtr
->calendars
[calIdx
].month
;
1881 /* days area (including week days and week numbers) */
1882 day
= MONTHCAL_GetDayFromPos(infoPtr
, lpht
->pt
, calIdx
);
1883 if (PtInRect(&infoPtr
->calendars
[calIdx
].wdays
, lpht
->pt
))
1885 htinfo
.uHit
= MCHT_CALENDARDAY
;
1886 htinfo
.iOffset
= calIdx
;
1887 htinfo
.st
.wYear
= ht_month
->wYear
;
1888 htinfo
.st
.wMonth
= (day
< 1) ? ht_month
->wMonth
-1 : ht_month
->wMonth
;
1889 htinfo
.st
.wDay
= (day
< 1) ?
1890 MONTHCAL_MonthLength(ht_month
->wMonth
-1, ht_month
->wYear
) - day
: day
;
1892 MONTHCAL_GetDayPos(infoPtr
, &htinfo
.st
, &htinfo
.iCol
, &htinfo
.iRow
, calIdx
);
1894 else if(PtInRect(&infoPtr
->calendars
[calIdx
].weeknums
, lpht
->pt
))
1896 htinfo
.uHit
= MCHT_CALENDARWEEKNUM
;
1897 htinfo
.st
.wYear
= ht_month
->wYear
;
1898 htinfo
.iOffset
= calIdx
;
1902 htinfo
.st
.wMonth
= ht_month
->wMonth
- 1;
1903 htinfo
.st
.wDay
= MONTHCAL_MonthLength(ht_month
->wMonth
-1, ht_month
->wYear
) - day
;
1905 else if (day
> MONTHCAL_MonthLength(ht_month
->wMonth
, ht_month
->wYear
))
1907 htinfo
.st
.wMonth
= ht_month
->wMonth
+ 1;
1908 htinfo
.st
.wDay
= day
- MONTHCAL_MonthLength(ht_month
->wMonth
, ht_month
->wYear
);
1912 htinfo
.st
.wMonth
= ht_month
->wMonth
;
1913 htinfo
.st
.wDay
= day
;
1916 else if(PtInRect(&infoPtr
->calendars
[calIdx
].days
, lpht
->pt
))
1918 htinfo
.iOffset
= calIdx
;
1919 htinfo
.st
.wDay
= ht_month
->wDay
;
1920 htinfo
.st
.wYear
= ht_month
->wYear
;
1921 htinfo
.st
.wMonth
= ht_month
->wMonth
;
1922 /* previous month only valid for first calendar */
1923 if (day
< 1 && calIdx
== 0)
1925 htinfo
.uHit
= MCHT_CALENDARDATEPREV
;
1926 MONTHCAL_GetPrevMonth(&htinfo
.st
);
1927 htinfo
.st
.wDay
= MONTHCAL_MonthLength(htinfo
.st
.wMonth
, htinfo
.st
.wYear
) + day
;
1929 /* next month only valid for last calendar */
1930 else if (day
> MONTHCAL_MonthLength(ht_month
->wMonth
, ht_month
->wYear
) &&
1931 calIdx
== MONTHCAL_GetCalCount(infoPtr
)-1)
1933 htinfo
.uHit
= MCHT_CALENDARDATENEXT
;
1934 MONTHCAL_GetNextMonth(&htinfo
.st
);
1935 htinfo
.st
.wDay
= day
- MONTHCAL_MonthLength(ht_month
->wMonth
, ht_month
->wYear
);
1937 /* multiple calendars case - blank areas for previous/next month */
1938 else if (day
< 1 || day
> MONTHCAL_MonthLength(ht_month
->wMonth
, ht_month
->wYear
))
1940 htinfo
.uHit
= MCHT_CALENDARBK
;
1944 htinfo
.uHit
= MCHT_CALENDARDATE
;
1945 htinfo
.st
.wDay
= day
;
1948 MONTHCAL_GetDayPos(infoPtr
, &htinfo
.st
, &htinfo
.iCol
, &htinfo
.iRow
, calIdx
);
1949 MONTHCAL_GetDayRectI(infoPtr
, &htinfo
.rc
, htinfo
.iCol
, htinfo
.iRow
, calIdx
);
1950 /* always update day of week */
1951 MONTHCAL_CalculateDayOfWeek(&htinfo
.st
, TRUE
);
1954 return fill_hittest_info(&htinfo
, lpht
);
1957 /* MCN_GETDAYSTATE notification helper */
1958 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO
*infoPtr
)
1960 MONTHDAYSTATE
*state
;
1963 if (!(infoPtr
->dwStyle
& MCS_DAYSTATE
)) return;
1965 nmds
.nmhdr
.hwndFrom
= infoPtr
->hwndSelf
;
1966 nmds
.nmhdr
.idFrom
= GetWindowLongPtrW(infoPtr
->hwndSelf
, GWLP_ID
);
1967 nmds
.nmhdr
.code
= MCN_GETDAYSTATE
;
1968 nmds
.cDayState
= MONTHCAL_GetMonthRange(infoPtr
, GMR_DAYSTATE
, 0);
1969 nmds
.prgDayState
= state
= Alloc(nmds
.cDayState
* sizeof(MONTHDAYSTATE
));
1971 MONTHCAL_GetMinDate(infoPtr
, &nmds
.stStart
);
1972 nmds
.stStart
.wDay
= 1;
1974 SendMessageW(infoPtr
->hwndNotify
, WM_NOTIFY
, nmds
.nmhdr
.idFrom
, (LPARAM
)&nmds
);
1975 memcpy(infoPtr
->monthdayState
, nmds
.prgDayState
,
1976 MONTHCAL_GetMonthRange(infoPtr
, GMR_DAYSTATE
, 0)*sizeof(MONTHDAYSTATE
));
1981 /* no valid range check performed */
1982 static void MONTHCAL_Scroll(MONTHCAL_INFO
*infoPtr
, INT delta
, BOOL keep_selection
)
1986 for(i
= 0; i
< MONTHCAL_GetCalCount(infoPtr
); i
++)
1988 /* save selection position to shift it later */
1989 if (selIdx
== -1 && MONTHCAL_CompareMonths(&infoPtr
->minSel
, &infoPtr
->calendars
[i
].month
) == 0)
1992 MONTHCAL_GetMonth(&infoPtr
->calendars
[i
].month
, delta
);
1998 /* selection is always shifted to first calendar */
1999 if (infoPtr
->dwStyle
& MCS_MULTISELECT
)
2001 SYSTEMTIME range
[2];
2003 MONTHCAL_GetSelRange(infoPtr
, range
);
2004 MONTHCAL_GetMonth(&range
[0], delta
- selIdx
);
2005 MONTHCAL_GetMonth(&range
[1], delta
- selIdx
);
2006 MONTHCAL_SetSelRange(infoPtr
, range
);
2010 SYSTEMTIME st
= infoPtr
->minSel
;
2012 MONTHCAL_GetMonth(&st
, delta
- selIdx
);
2013 MONTHCAL_SetCurSel(infoPtr
, &st
);
2017 static void MONTHCAL_GoToMonth(MONTHCAL_INFO
*infoPtr
, enum nav_direction direction
)
2019 INT delta
= infoPtr
->delta
? infoPtr
->delta
: MONTHCAL_GetCalCount(infoPtr
);
2020 BOOL keep_selection
;
2023 TRACE("%s\n", direction
== DIRECTION_BACKWARD
? "back" : "fwd");
2025 /* check if change allowed by range set */
2026 if(direction
== DIRECTION_BACKWARD
)
2028 st
= infoPtr
->calendars
[0].month
;
2029 MONTHCAL_GetMonth(&st
, -delta
);
2033 st
= infoPtr
->calendars
[MONTHCAL_GetCalCount(infoPtr
)-1].month
;
2034 MONTHCAL_GetMonth(&st
, delta
);
2037 if(!MONTHCAL_IsDateInValidRange(infoPtr
, &st
, FALSE
)) return;
2039 keep_selection
= infoPtr
->dwStyle
& MCS_NOSELCHANGEONNAV
;
2040 MONTHCAL_Scroll(infoPtr
, direction
== DIRECTION_BACKWARD
? -delta
: delta
, keep_selection
);
2041 MONTHCAL_NotifyDayState(infoPtr
);
2042 if (!keep_selection
)
2043 MONTHCAL_NotifySelectionChange(infoPtr
);
2047 MONTHCAL_RButtonUp(MONTHCAL_INFO
*infoPtr
, LPARAM lParam
)
2053 hMenu
= CreatePopupMenu();
2054 LoadStringW(COMCTL32_hModule
, IDM_GOTODAY
, buf
, countof(buf
));
2055 AppendMenuW(hMenu
, MF_STRING
|MF_ENABLED
, 1, buf
);
2056 menupoint
.x
= (short)LOWORD(lParam
);
2057 menupoint
.y
= (short)HIWORD(lParam
);
2058 ClientToScreen(infoPtr
->hwndSelf
, &menupoint
);
2059 if( TrackPopupMenu(hMenu
, TPM_RIGHTBUTTON
| TPM_NONOTIFY
| TPM_RETURNCMD
,
2060 menupoint
.x
, menupoint
.y
, 0, infoPtr
->hwndSelf
, NULL
))
2062 if (infoPtr
->dwStyle
& MCS_MULTISELECT
)
2064 SYSTEMTIME range
[2];
2066 range
[0] = range
[1] = infoPtr
->todaysDate
;
2067 MONTHCAL_SetSelRange(infoPtr
, range
);
2070 MONTHCAL_SetCurSel(infoPtr
, &infoPtr
->todaysDate
);
2072 MONTHCAL_NotifySelectionChange(infoPtr
);
2073 MONTHCAL_NotifySelect(infoPtr
);
2081 * Subclassed edit control windproc function
2084 * [I] hwnd : the edit window handle
2085 * [I] uMsg : the message that is to be processed
2086 * [I] wParam : first message parameter
2087 * [I] lParam : second message parameter
2090 static LRESULT CALLBACK
EditWndProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
2092 MONTHCAL_INFO
*infoPtr
= (MONTHCAL_INFO
*)GetWindowLongPtrW(GetParent(hwnd
), 0);
2094 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
2095 hwnd
, uMsg
, wParam
, lParam
);
2100 return DLGC_WANTARROWS
| DLGC_WANTALLKEYS
;
2104 WNDPROC editProc
= infoPtr
->EditWndProc
;
2105 infoPtr
->EditWndProc
= NULL
;
2106 SetWindowLongPtrW(hwnd
, GWLP_WNDPROC
, (DWORD_PTR
)editProc
);
2107 return CallWindowProcW(editProc
, hwnd
, uMsg
, wParam
, lParam
);
2114 if ((VK_ESCAPE
== (INT
)wParam
) || (VK_RETURN
== (INT
)wParam
))
2118 return CallWindowProcW(infoPtr
->EditWndProc
, hwnd
, uMsg
, wParam
, lParam
);
2121 SendMessageW(infoPtr
->hWndYearUpDown
, WM_CLOSE
, 0, 0);
2122 SendMessageW(hwnd
, WM_CLOSE
, 0, 0);
2126 /* creates updown control and edit box */
2127 static void MONTHCAL_EditYear(MONTHCAL_INFO
*infoPtr
, INT calIdx
)
2129 RECT
*rc
= &infoPtr
->calendars
[calIdx
].titleyear
;
2130 RECT
*title
= &infoPtr
->calendars
[calIdx
].title
;
2132 infoPtr
->hWndYearEdit
=
2133 CreateWindowExW(0, WC_EDITW
, 0, WS_VISIBLE
| WS_CHILD
| ES_READONLY
,
2134 rc
->left
+ 3, (title
->bottom
+ title
->top
- infoPtr
->textHeight
) / 2,
2135 rc
->right
- rc
->left
+ 4,
2136 infoPtr
->textHeight
, infoPtr
->hwndSelf
,
2139 SendMessageW(infoPtr
->hWndYearEdit
, WM_SETFONT
, (WPARAM
)infoPtr
->hBoldFont
, TRUE
);
2141 infoPtr
->hWndYearUpDown
=
2142 CreateWindowExW(0, UPDOWN_CLASSW
, 0,
2143 WS_VISIBLE
| WS_CHILD
| UDS_SETBUDDYINT
| UDS_NOTHOUSANDS
| UDS_ARROWKEYS
,
2144 rc
->right
+ 7, (title
->bottom
+ title
->top
- infoPtr
->textHeight
) / 2,
2145 18, infoPtr
->textHeight
, infoPtr
->hwndSelf
,
2148 /* attach edit box */
2149 SendMessageW(infoPtr
->hWndYearUpDown
, UDM_SETRANGE
, 0,
2150 MAKELONG(max_allowed_date
.wYear
, min_allowed_date
.wYear
));
2151 SendMessageW(infoPtr
->hWndYearUpDown
, UDM_SETBUDDY
, (WPARAM
)infoPtr
->hWndYearEdit
, 0);
2152 SendMessageW(infoPtr
->hWndYearUpDown
, UDM_SETPOS
, 0, infoPtr
->calendars
[calIdx
].month
.wYear
);
2154 /* subclass edit box */
2155 infoPtr
->EditWndProc
= (WNDPROC
)SetWindowLongPtrW(infoPtr
->hWndYearEdit
,
2156 GWLP_WNDPROC
, (DWORD_PTR
)EditWndProc
);
2158 SetFocus(infoPtr
->hWndYearEdit
);
2162 MONTHCAL_LButtonDown(MONTHCAL_INFO
*infoPtr
, LPARAM lParam
)
2167 /* Actually we don't need input focus for calendar, this is used to kill
2168 year updown and its buddy edit box */
2169 if (IsWindow(infoPtr
->hWndYearUpDown
))
2171 SetFocus(infoPtr
->hwndSelf
);
2175 SetCapture(infoPtr
->hwndSelf
);
2177 ht
.cbSize
= sizeof(MCHITTESTINFO
);
2178 ht
.pt
.x
= (short)LOWORD(lParam
);
2179 ht
.pt
.y
= (short)HIWORD(lParam
);
2181 hit
= MONTHCAL_HitTest(infoPtr
, &ht
);
2183 TRACE("%x at %s\n", hit
, wine_dbgstr_point(&ht
.pt
));
2187 case MCHT_TITLEBTNNEXT
:
2188 MONTHCAL_GoToMonth(infoPtr
, DIRECTION_FORWARD
);
2189 infoPtr
->status
= MC_NEXTPRESSED
;
2190 SetTimer(infoPtr
->hwndSelf
, MC_PREVNEXTMONTHTIMER
, MC_PREVNEXTMONTHDELAY
, 0);
2191 InvalidateRect(infoPtr
->hwndSelf
, NULL
, FALSE
);
2194 case MCHT_TITLEBTNPREV
:
2195 MONTHCAL_GoToMonth(infoPtr
, DIRECTION_BACKWARD
);
2196 infoPtr
->status
= MC_PREVPRESSED
;
2197 SetTimer(infoPtr
->hwndSelf
, MC_PREVNEXTMONTHTIMER
, MC_PREVNEXTMONTHDELAY
, 0);
2198 InvalidateRect(infoPtr
->hwndSelf
, NULL
, FALSE
);
2201 case MCHT_TITLEMONTH
:
2203 HMENU hMenu
= CreatePopupMenu();
2208 for (i
= 0; i
< 12; i
++)
2210 GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_SMONTHNAME1
+i
, buf
, countof(buf
));
2211 AppendMenuW(hMenu
, MF_STRING
|MF_ENABLED
, i
+ 1, buf
);
2213 menupoint
.x
= ht
.pt
.x
;
2214 menupoint
.y
= ht
.pt
.y
;
2215 ClientToScreen(infoPtr
->hwndSelf
, &menupoint
);
2216 i
= TrackPopupMenu(hMenu
,TPM_LEFTALIGN
| TPM_NONOTIFY
| TPM_RIGHTBUTTON
| TPM_RETURNCMD
,
2217 menupoint
.x
, menupoint
.y
, 0, infoPtr
->hwndSelf
, NULL
);
2219 if ((i
> 0) && (i
< 13) && infoPtr
->calendars
[ht
.iOffset
].month
.wMonth
!= i
)
2221 INT delta
= i
- infoPtr
->calendars
[ht
.iOffset
].month
.wMonth
;
2224 /* check if change allowed by range set */
2225 st
= delta
< 0 ? infoPtr
->calendars
[0].month
:
2226 infoPtr
->calendars
[MONTHCAL_GetCalCount(infoPtr
)-1].month
;
2227 MONTHCAL_GetMonth(&st
, delta
);
2229 if (MONTHCAL_IsDateInValidRange(infoPtr
, &st
, FALSE
))
2231 MONTHCAL_Scroll(infoPtr
, delta
, FALSE
);
2232 MONTHCAL_NotifyDayState(infoPtr
);
2233 MONTHCAL_NotifySelectionChange(infoPtr
);
2234 InvalidateRect(infoPtr
->hwndSelf
, NULL
, FALSE
);
2239 case MCHT_TITLEYEAR
:
2241 MONTHCAL_EditYear(infoPtr
, ht
.iOffset
);
2244 case MCHT_TODAYLINK
:
2246 if (infoPtr
->dwStyle
& MCS_MULTISELECT
)
2248 SYSTEMTIME range
[2];
2250 range
[0] = range
[1] = infoPtr
->todaysDate
;
2251 MONTHCAL_SetSelRange(infoPtr
, range
);
2254 MONTHCAL_SetCurSel(infoPtr
, &infoPtr
->todaysDate
);
2256 MONTHCAL_NotifySelectionChange(infoPtr
);
2257 MONTHCAL_NotifySelect(infoPtr
);
2260 case MCHT_CALENDARDATENEXT
:
2261 case MCHT_CALENDARDATEPREV
:
2262 case MCHT_CALENDARDATE
:
2266 MONTHCAL_CopyDate(&ht
.st
, &infoPtr
->firstSel
);
2268 st
[0] = st
[1] = ht
.st
;
2269 /* clear selection range */
2270 MONTHCAL_SetSelRange(infoPtr
, st
);
2272 infoPtr
->status
= MC_SEL_LBUTDOWN
;
2273 MONTHCAL_SetDayFocus(infoPtr
, &ht
.st
);
2283 MONTHCAL_LButtonUp(MONTHCAL_INFO
*infoPtr
, LPARAM lParam
)
2291 if(infoPtr
->status
& (MC_PREVPRESSED
| MC_NEXTPRESSED
)) {
2294 KillTimer(infoPtr
->hwndSelf
, MC_PREVNEXTMONTHTIMER
);
2295 r
= infoPtr
->status
& MC_PREVPRESSED
? &infoPtr
->titlebtnprev
: &infoPtr
->titlebtnnext
;
2296 infoPtr
->status
&= ~(MC_PREVPRESSED
| MC_NEXTPRESSED
);
2298 InvalidateRect(infoPtr
->hwndSelf
, r
, FALSE
);
2303 /* always send NM_RELEASEDCAPTURE notification */
2304 nmhdr
.hwndFrom
= infoPtr
->hwndSelf
;
2305 nmhdr
.idFrom
= GetWindowLongPtrW(infoPtr
->hwndSelf
, GWLP_ID
);
2306 nmhdr
.code
= NM_RELEASEDCAPTURE
;
2307 TRACE("Sent notification from %p to %p\n", infoPtr
->hwndSelf
, infoPtr
->hwndNotify
);
2309 SendMessageW(infoPtr
->hwndNotify
, WM_NOTIFY
, nmhdr
.idFrom
, (LPARAM
)&nmhdr
);
2311 if(!(infoPtr
->status
& MC_SEL_LBUTDOWN
)) return 0;
2313 ht
.cbSize
= sizeof(MCHITTESTINFO
);
2314 ht
.pt
.x
= (short)LOWORD(lParam
);
2315 ht
.pt
.y
= (short)HIWORD(lParam
);
2316 hit
= MONTHCAL_HitTest(infoPtr
, &ht
);
2318 infoPtr
->status
= MC_SEL_LBUTUP
;
2319 MONTHCAL_SetDayFocus(infoPtr
, NULL
);
2321 if((hit
& MCHT_CALENDARDATE
) == MCHT_CALENDARDATE
)
2323 SYSTEMTIME sel
= infoPtr
->minSel
;
2325 /* will be invalidated here */
2326 MONTHCAL_SetCurSel(infoPtr
, &ht
.st
);
2328 /* send MCN_SELCHANGE only if new date selected */
2329 if (!MONTHCAL_IsDateEqual(&sel
, &ht
.st
))
2330 MONTHCAL_NotifySelectionChange(infoPtr
);
2332 MONTHCAL_NotifySelect(infoPtr
);
2340 MONTHCAL_Timer(MONTHCAL_INFO
*infoPtr
, WPARAM id
)
2345 case MC_PREVNEXTMONTHTIMER
:
2346 if(infoPtr
->status
& MC_NEXTPRESSED
) MONTHCAL_GoToMonth(infoPtr
, DIRECTION_FORWARD
);
2347 if(infoPtr
->status
& MC_PREVPRESSED
) MONTHCAL_GoToMonth(infoPtr
, DIRECTION_BACKWARD
);
2348 InvalidateRect(infoPtr
->hwndSelf
, NULL
, FALSE
);
2350 case MC_TODAYUPDATETIMER
:
2354 if(infoPtr
->todaySet
) return 0;
2357 MONTHCAL_UpdateToday(infoPtr
, &st
);
2359 /* notification sent anyway */
2360 MONTHCAL_NotifySelectionChange(infoPtr
);
2365 ERR("got unknown timer %ld\n", id
);
2374 MONTHCAL_MouseMove(MONTHCAL_INFO
*infoPtr
, LPARAM lParam
)
2381 if(!(infoPtr
->status
& MC_SEL_LBUTDOWN
)) return 0;
2383 ht
.cbSize
= sizeof(MCHITTESTINFO
);
2384 ht
.pt
.x
= (short)LOWORD(lParam
);
2385 ht
.pt
.y
= (short)HIWORD(lParam
);
2388 hit
= MONTHCAL_HitTest(infoPtr
, &ht
);
2390 /* not on the calendar date numbers? bail out */
2391 TRACE("hit:%x\n",hit
);
2392 if((hit
& MCHT_CALENDARDATE
) != MCHT_CALENDARDATE
)
2394 MONTHCAL_SetDayFocus(infoPtr
, NULL
);
2400 /* if pointer is over focused day still there's nothing to do */
2401 if(!MONTHCAL_SetDayFocus(infoPtr
, &ht
.st
)) return 0;
2403 MONTHCAL_GetDayRect(infoPtr
, &ht
.st
, &r
, ht
.iOffset
);
2405 if(infoPtr
->dwStyle
& MCS_MULTISELECT
) {
2408 MONTHCAL_GetSelRange(infoPtr
, st
);
2410 /* If we're still at the first selected date and range is empty, return.
2411 If range isn't empty we should change range to a single firstSel */
2412 if(MONTHCAL_IsDateEqual(&infoPtr
->firstSel
, &st_ht
) &&
2413 MONTHCAL_IsDateEqual(&st
[0], &st
[1])) goto done
;
2415 MONTHCAL_IsSelRangeValid(infoPtr
, &st_ht
, &infoPtr
->firstSel
, &st_ht
);
2417 st
[0] = infoPtr
->firstSel
;
2418 /* we should overwrite timestamp here */
2419 MONTHCAL_CopyDate(&st_ht
, &st
[1]);
2421 /* bounds will be swapped here if needed */
2422 MONTHCAL_SetSelRange(infoPtr
, st
);
2429 /* FIXME: this should specify a rectangle containing only the days that changed
2430 using InvalidateRect */
2431 InvalidateRect(infoPtr
->hwndSelf
, NULL
, FALSE
);
2438 MONTHCAL_Paint(MONTHCAL_INFO
*infoPtr
, HDC hdc_paint
)
2445 GetClientRect(infoPtr
->hwndSelf
, &ps
.rcPaint
);
2449 hdc
= BeginPaint(infoPtr
->hwndSelf
, &ps
);
2451 MONTHCAL_Refresh(infoPtr
, hdc
, &ps
);
2452 if (!hdc_paint
) EndPaint(infoPtr
->hwndSelf
, &ps
);
2457 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO
*infoPtr
, HDC hdc
)
2461 if (!GetClipBox(hdc
, &rc
)) return FALSE
;
2463 FillRect(hdc
, &rc
, infoPtr
->brushes
[BrushBackground
]);
2469 MONTHCAL_PrintClient(MONTHCAL_INFO
*infoPtr
, HDC hdc
, DWORD options
)
2471 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc
, options
);
2473 if ((options
& PRF_CHECKVISIBLE
) && !IsWindowVisible(infoPtr
->hwndSelf
))
2476 if (options
& PRF_ERASEBKGND
)
2477 MONTHCAL_EraseBkgnd(infoPtr
, hdc
);
2479 if (options
& PRF_CLIENT
)
2480 MONTHCAL_Paint(infoPtr
, hdc
);
2486 MONTHCAL_SetFocus(const MONTHCAL_INFO
*infoPtr
)
2490 InvalidateRect(infoPtr
->hwndSelf
, NULL
, FALSE
);
2495 /* sets the size information */
2496 static void MONTHCAL_UpdateSize(MONTHCAL_INFO
*infoPtr
)
2498 static const WCHAR O0W
[] = { '0','0',0 };
2499 RECT
*title
=&infoPtr
->calendars
[0].title
;
2500 RECT
*prev
=&infoPtr
->titlebtnprev
;
2501 RECT
*next
=&infoPtr
->titlebtnnext
;
2502 RECT
*titlemonth
=&infoPtr
->calendars
[0].titlemonth
;
2503 RECT
*titleyear
=&infoPtr
->calendars
[0].titleyear
;
2504 RECT
*wdays
=&infoPtr
->calendars
[0].wdays
;
2505 RECT
*weeknumrect
=&infoPtr
->calendars
[0].weeknums
;
2506 RECT
*days
=&infoPtr
->calendars
[0].days
;
2507 RECT
*todayrect
=&infoPtr
->todayrect
;
2509 INT xdiv
, dx
, dy
, i
, j
, x
, y
, c_dx
, c_dy
;
2518 GetClientRect(infoPtr
->hwndSelf
, &client
);
2520 hdc
= GetDC(infoPtr
->hwndSelf
);
2521 font
= SelectObject(hdc
, infoPtr
->hFont
);
2523 /* get the height and width of each day's text */
2524 GetTextMetricsW(hdc
, &tm
);
2525 infoPtr
->textHeight
= tm
.tmHeight
+ tm
.tmExternalLeading
+ tm
.tmInternalLeading
;
2527 /* find widest day name for current locale and font */
2529 for (i
= 0; i
< 7; i
++)
2533 if (get_localized_dayname(infoPtr
, i
, buff
, countof(buff
)))
2535 GetTextExtentPoint32W(hdc
, buff
, lstrlenW(buff
), &sz
);
2536 if (sz
.cx
> day_width
) day_width
= sz
.cx
;
2538 else /* locale independent fallback on failure */
2540 static const WCHAR sunW
[] = { 'S','u','n' };
2541 GetTextExtentPoint32W(hdc
, sunW
, countof(sunW
), &sz
);
2549 /* recalculate the height and width increments and offsets */
2551 GetTextExtentPoint32W(hdc
, O0W
, 2, &size
);
2553 /* restore the originally selected font */
2554 SelectObject(hdc
, font
);
2555 ReleaseDC(infoPtr
->hwndSelf
, hdc
);
2557 xdiv
= (infoPtr
->dwStyle
& MCS_WEEKNUMBERS
) ? 8 : 7;
2559 infoPtr
->width_increment
= max(day_width
, size
.cx
* 2 + 4);
2560 infoPtr
->height_increment
= infoPtr
->textHeight
;
2562 /* calculate title area */
2564 title
->bottom
= 3 * infoPtr
->height_increment
/ 2;
2566 title
->right
= infoPtr
->width_increment
* xdiv
;
2568 /* set the dimensions of the next and previous buttons and center */
2569 /* the month text vertically */
2570 prev
->top
= next
->top
= title
->top
+ 4;
2571 prev
->bottom
= next
->bottom
= title
->bottom
- 4;
2572 prev
->left
= title
->left
+ 4;
2573 prev
->right
= prev
->left
+ (title
->bottom
- title
->top
);
2574 next
->right
= title
->right
- 4;
2575 next
->left
= next
->right
- (title
->bottom
- title
->top
);
2577 /* titlemonth->left and right change based upon the current month
2578 and are recalculated in refresh as the current month may change
2579 without the control being resized */
2580 titlemonth
->top
= titleyear
->top
= title
->top
+ (infoPtr
->height_increment
)/2;
2581 titlemonth
->bottom
= titleyear
->bottom
= title
->bottom
- (infoPtr
->height_increment
)/2;
2584 weeknumrect
->left
= 0;
2585 weeknumrect
->right
= infoPtr
->dwStyle
& MCS_WEEKNUMBERS
? prev
->right
: 0;
2587 /* days abbreviated names */
2588 wdays
->left
= days
->left
= weeknumrect
->right
;
2589 wdays
->right
= days
->right
= wdays
->left
+ 7 * infoPtr
->width_increment
;
2590 wdays
->top
= title
->bottom
;
2591 wdays
->bottom
= wdays
->top
+ infoPtr
->height_increment
;
2593 days
->top
= weeknumrect
->top
= wdays
->bottom
;
2594 days
->bottom
= weeknumrect
->bottom
= days
->top
+ 6 * infoPtr
->height_increment
;
2596 todayrect
->left
= 0;
2597 todayrect
->right
= title
->right
;
2598 todayrect
->top
= days
->bottom
;
2599 todayrect
->bottom
= days
->bottom
+ infoPtr
->height_increment
;
2601 /* compute calendar count, update all calendars */
2602 x
= (client
.right
+ MC_CALENDAR_PADDING
) / (title
->right
- title
->left
+ MC_CALENDAR_PADDING
);
2603 /* today label affects whole height */
2604 if (infoPtr
->dwStyle
& MCS_NOTODAY
)
2605 y
= (client
.bottom
+ MC_CALENDAR_PADDING
) / (days
->bottom
- title
->top
+ MC_CALENDAR_PADDING
);
2607 y
= (client
.bottom
- todayrect
->bottom
+ todayrect
->top
+ MC_CALENDAR_PADDING
) /
2608 (days
->bottom
- title
->top
+ MC_CALENDAR_PADDING
);
2610 /* TODO: ensure that count is properly adjusted to fit 12 months constraint */
2614 if (x
*y
!= MONTHCAL_GetCalCount(infoPtr
))
2616 infoPtr
->dim
.cx
= x
;
2617 infoPtr
->dim
.cy
= y
;
2618 infoPtr
->calendars
= ReAlloc(infoPtr
->calendars
, MONTHCAL_GetCalCount(infoPtr
)*sizeof(CALENDAR_INFO
));
2620 infoPtr
->monthdayState
= ReAlloc(infoPtr
->monthdayState
,
2621 MONTHCAL_GetMonthRange(infoPtr
, GMR_DAYSTATE
, 0)*sizeof(MONTHDAYSTATE
));
2622 MONTHCAL_NotifyDayState(infoPtr
);
2624 /* update pointers that we'll need */
2625 title
= &infoPtr
->calendars
[0].title
;
2626 wdays
= &infoPtr
->calendars
[0].wdays
;
2627 days
= &infoPtr
->calendars
[0].days
;
2630 for (i
= 1; i
< MONTHCAL_GetCalCount(infoPtr
); i
++)
2633 infoPtr
->calendars
[i
] = infoPtr
->calendars
[0];
2634 MONTHCAL_GetMonth(&infoPtr
->calendars
[i
].month
, i
);
2637 /* offset all rectangles to center in client area */
2638 c_dx
= (client
.right
- x
* title
->right
- MC_CALENDAR_PADDING
* (x
-1)) / 2;
2639 c_dy
= (client
.bottom
- y
* todayrect
->bottom
- MC_CALENDAR_PADDING
* (y
-1)) / 2;
2641 /* if calendar doesn't fit client area show it at left/top bounds */
2642 if (title
->left
+ c_dx
< 0) c_dx
= 0;
2643 if (title
->top
+ c_dy
< 0) c_dy
= 0;
2645 for (i
= 0; i
< y
; i
++)
2647 for (j
= 0; j
< x
; j
++)
2649 dx
= j
*(title
->right
- title
->left
+ MC_CALENDAR_PADDING
) + c_dx
;
2650 dy
= i
*(days
->bottom
- title
->top
+ MC_CALENDAR_PADDING
) + c_dy
;
2652 OffsetRect(&infoPtr
->calendars
[i
*x
+j
].title
, dx
, dy
);
2653 OffsetRect(&infoPtr
->calendars
[i
*x
+j
].titlemonth
, dx
, dy
);
2654 OffsetRect(&infoPtr
->calendars
[i
*x
+j
].titleyear
, dx
, dy
);
2655 OffsetRect(&infoPtr
->calendars
[i
*x
+j
].wdays
, dx
, dy
);
2656 OffsetRect(&infoPtr
->calendars
[i
*x
+j
].weeknums
, dx
, dy
);
2657 OffsetRect(&infoPtr
->calendars
[i
*x
+j
].days
, dx
, dy
);
2661 OffsetRect(prev
, c_dx
, c_dy
);
2662 OffsetRect(next
, (x
-1)*(title
->right
- title
->left
+ MC_CALENDAR_PADDING
) + c_dx
, c_dy
);
2664 i
= infoPtr
->dim
.cx
* infoPtr
->dim
.cy
- infoPtr
->dim
.cx
;
2665 todayrect
->left
= infoPtr
->calendars
[i
].title
.left
;
2666 todayrect
->right
= infoPtr
->calendars
[i
].title
.right
;
2667 todayrect
->top
= infoPtr
->calendars
[i
].days
.bottom
;
2668 todayrect
->bottom
= infoPtr
->calendars
[i
].days
.bottom
+ infoPtr
->height_increment
;
2670 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2671 infoPtr
->width_increment
,infoPtr
->height_increment
,
2672 wine_dbgstr_rect(&client
),
2673 wine_dbgstr_rect(title
),
2674 wine_dbgstr_rect(wdays
),
2675 wine_dbgstr_rect(days
),
2676 wine_dbgstr_rect(todayrect
));
2679 static LRESULT
MONTHCAL_Size(MONTHCAL_INFO
*infoPtr
, int Width
, int Height
)
2681 TRACE("(width=%d, height=%d)\n", Width
, Height
);
2683 MONTHCAL_UpdateSize(infoPtr
);
2684 InvalidateRect(infoPtr
->hwndSelf
, NULL
, TRUE
);
2689 static LRESULT
MONTHCAL_GetFont(const MONTHCAL_INFO
*infoPtr
)
2691 return (LRESULT
)infoPtr
->hFont
;
2694 static LRESULT
MONTHCAL_SetFont(MONTHCAL_INFO
*infoPtr
, HFONT hFont
, BOOL redraw
)
2699 if (!hFont
) return 0;
2701 hOldFont
= infoPtr
->hFont
;
2702 infoPtr
->hFont
= hFont
;
2704 GetObjectW(infoPtr
->hFont
, sizeof(lf
), &lf
);
2705 lf
.lfWeight
= FW_BOLD
;
2706 infoPtr
->hBoldFont
= CreateFontIndirectW(&lf
);
2708 MONTHCAL_UpdateSize(infoPtr
);
2711 InvalidateRect(infoPtr
->hwndSelf
, NULL
, FALSE
);
2713 return (LRESULT
)hOldFont
;
2716 /* update theme after a WM_THEMECHANGED message */
2717 static LRESULT
theme_changed (const MONTHCAL_INFO
* infoPtr
)
2719 HTHEME theme
= GetWindowTheme (infoPtr
->hwndSelf
);
2720 CloseThemeData (theme
);
2721 OpenThemeData (infoPtr
->hwndSelf
, themeClass
);
2725 static INT
MONTHCAL_StyleChanged(MONTHCAL_INFO
*infoPtr
, WPARAM wStyleType
,
2726 const STYLESTRUCT
*lpss
)
2728 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2729 wStyleType
, lpss
->styleOld
, lpss
->styleNew
);
2731 if (wStyleType
!= GWL_STYLE
) return 0;
2733 infoPtr
->dwStyle
= lpss
->styleNew
;
2735 /* make room for week numbers */
2736 if ((lpss
->styleNew
^ lpss
->styleOld
) & (MCS_WEEKNUMBERS
| MCS_SHORTDAYSOFWEEK
))
2737 MONTHCAL_UpdateSize(infoPtr
);
2742 static INT
MONTHCAL_StyleChanging(MONTHCAL_INFO
*infoPtr
, WPARAM wStyleType
,
2745 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2746 wStyleType
, lpss
->styleOld
, lpss
->styleNew
);
2748 /* block MCS_MULTISELECT change */
2749 if ((lpss
->styleNew
^ lpss
->styleOld
) & MCS_MULTISELECT
)
2751 if (lpss
->styleOld
& MCS_MULTISELECT
)
2752 lpss
->styleNew
|= MCS_MULTISELECT
;
2754 lpss
->styleNew
&= ~MCS_MULTISELECT
;
2757 /* block MCS_DAYSTATE change */
2758 if ((lpss
->styleNew
^ lpss
->styleOld
) & MCS_DAYSTATE
)
2760 if (lpss
->styleOld
& MCS_DAYSTATE
)
2761 lpss
->styleNew
|= MCS_DAYSTATE
;
2763 lpss
->styleNew
&= ~MCS_DAYSTATE
;
2769 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2771 MONTHCAL_Create(HWND hwnd
, LPCREATESTRUCTW lpcs
)
2773 MONTHCAL_INFO
*infoPtr
;
2775 /* allocate memory for info structure */
2776 infoPtr
= Alloc(sizeof(MONTHCAL_INFO
));
2777 SetWindowLongPtrW(hwnd
, 0, (DWORD_PTR
)infoPtr
);
2779 if (infoPtr
== NULL
) {
2780 ERR("could not allocate info memory!\n");
2784 infoPtr
->hwndSelf
= hwnd
;
2785 infoPtr
->hwndNotify
= lpcs
->hwndParent
;
2786 infoPtr
->dwStyle
= GetWindowLongW(hwnd
, GWL_STYLE
);
2787 infoPtr
->dim
.cx
= infoPtr
->dim
.cy
= 1;
2788 infoPtr
->calendars
= Alloc(sizeof(CALENDAR_INFO
));
2789 if (!infoPtr
->calendars
) goto fail
;
2790 infoPtr
->monthdayState
= Alloc(3*sizeof(MONTHDAYSTATE
));
2791 if (!infoPtr
->monthdayState
) goto fail
;
2793 /* initialize info structure */
2794 /* FIXME: calculate systemtime ->> localtime(subtract timezoneinfo) */
2796 GetLocalTime(&infoPtr
->todaysDate
);
2797 MONTHCAL_SetFirstDayOfWeek(infoPtr
, -1);
2799 infoPtr
->maxSelCount
= (infoPtr
->dwStyle
& MCS_MULTISELECT
) ? 7 : 1;
2801 infoPtr
->colors
[MCSC_BACKGROUND
] = comctl32_color
.clrWindow
;
2802 infoPtr
->colors
[MCSC_TEXT
] = comctl32_color
.clrWindowText
;
2803 infoPtr
->colors
[MCSC_TITLEBK
] = comctl32_color
.clrActiveCaption
;
2804 infoPtr
->colors
[MCSC_TITLETEXT
] = comctl32_color
.clrWindow
;
2805 infoPtr
->colors
[MCSC_MONTHBK
] = comctl32_color
.clrWindow
;
2806 infoPtr
->colors
[MCSC_TRAILINGTEXT
] = comctl32_color
.clrGrayText
;
2808 infoPtr
->brushes
[BrushBackground
] = CreateSolidBrush(infoPtr
->colors
[MCSC_BACKGROUND
]);
2809 infoPtr
->brushes
[BrushTitle
] = CreateSolidBrush(infoPtr
->colors
[MCSC_TITLEBK
]);
2810 infoPtr
->brushes
[BrushMonth
] = CreateSolidBrush(infoPtr
->colors
[MCSC_MONTHBK
]);
2812 infoPtr
->pens
[PenRed
] = CreatePen(PS_SOLID
, 1, RGB(255, 0, 0));
2813 infoPtr
->pens
[PenText
] = CreatePen(PS_SOLID
, 1, infoPtr
->colors
[MCSC_TEXT
]);
2815 infoPtr
->minSel
= infoPtr
->todaysDate
;
2816 infoPtr
->maxSel
= infoPtr
->todaysDate
;
2817 infoPtr
->calendars
[0].month
= infoPtr
->todaysDate
;
2818 infoPtr
->isUnicode
= TRUE
;
2820 /* setup control layout and day state data */
2821 MONTHCAL_UpdateSize(infoPtr
);
2823 /* today auto update timer, to be freed only on control destruction */
2824 SetTimer(infoPtr
->hwndSelf
, MC_TODAYUPDATETIMER
, MC_TODAYUPDATEDELAY
, 0);
2826 OpenThemeData (infoPtr
->hwndSelf
, themeClass
);
2831 Free(infoPtr
->monthdayState
);
2832 Free(infoPtr
->calendars
);
2838 MONTHCAL_Destroy(MONTHCAL_INFO
*infoPtr
)
2842 /* free month calendar info data */
2843 Free(infoPtr
->monthdayState
);
2844 Free(infoPtr
->calendars
);
2845 SetWindowLongPtrW(infoPtr
->hwndSelf
, 0, 0);
2847 CloseThemeData (GetWindowTheme (infoPtr
->hwndSelf
));
2849 for (i
= 0; i
< BrushLast
; i
++) DeleteObject(infoPtr
->brushes
[i
]);
2850 for (i
= 0; i
< PenLast
; i
++) DeleteObject(infoPtr
->pens
[i
]);
2857 * Handler for WM_NOTIFY messages
2860 MONTHCAL_Notify(MONTHCAL_INFO
*infoPtr
, NMHDR
*hdr
)
2862 /* notification from year edit updown */
2863 if (hdr
->code
== UDN_DELTAPOS
)
2865 NMUPDOWN
*nmud
= (NMUPDOWN
*)hdr
;
2867 if (hdr
->hwndFrom
== infoPtr
->hWndYearUpDown
&& nmud
->iDelta
)
2869 /* year value limits are set up explicitly after updown creation */
2870 MONTHCAL_Scroll(infoPtr
, 12 * nmud
->iDelta
, FALSE
);
2871 MONTHCAL_NotifyDayState(infoPtr
);
2872 MONTHCAL_NotifySelectionChange(infoPtr
);
2879 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO
*infoPtr
, BOOL isUnicode
)
2881 BOOL prev
= infoPtr
->isUnicode
;
2882 infoPtr
->isUnicode
= isUnicode
;
2887 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO
*infoPtr
)
2889 return infoPtr
->isUnicode
;
2892 static LRESULT WINAPI
2893 MONTHCAL_WindowProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
2895 MONTHCAL_INFO
*infoPtr
= (MONTHCAL_INFO
*)GetWindowLongPtrW(hwnd
, 0);
2897 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd
, uMsg
, wParam
, lParam
);
2899 if (!infoPtr
&& (uMsg
!= WM_CREATE
))
2900 return DefWindowProcW(hwnd
, uMsg
, wParam
, lParam
);
2904 return MONTHCAL_GetCurSel(infoPtr
, (LPSYSTEMTIME
)lParam
);
2907 return MONTHCAL_SetCurSel(infoPtr
, (LPSYSTEMTIME
)lParam
);
2909 case MCM_GETMAXSELCOUNT
:
2910 return MONTHCAL_GetMaxSelCount(infoPtr
);
2912 case MCM_SETMAXSELCOUNT
:
2913 return MONTHCAL_SetMaxSelCount(infoPtr
, wParam
);
2915 case MCM_GETSELRANGE
:
2916 return MONTHCAL_GetSelRange(infoPtr
, (LPSYSTEMTIME
)lParam
);
2918 case MCM_SETSELRANGE
:
2919 return MONTHCAL_SetSelRange(infoPtr
, (LPSYSTEMTIME
)lParam
);
2921 case MCM_GETMONTHRANGE
:
2922 return MONTHCAL_GetMonthRange(infoPtr
, wParam
, (SYSTEMTIME
*)lParam
);
2924 case MCM_SETDAYSTATE
:
2925 return MONTHCAL_SetDayState(infoPtr
, (INT
)wParam
, (LPMONTHDAYSTATE
)lParam
);
2927 case MCM_GETMINREQRECT
:
2928 return MONTHCAL_GetMinReqRect(infoPtr
, (LPRECT
)lParam
);
2931 return MONTHCAL_GetColor(infoPtr
, wParam
);
2934 return MONTHCAL_SetColor(infoPtr
, wParam
, (COLORREF
)lParam
);
2937 return MONTHCAL_GetToday(infoPtr
, (LPSYSTEMTIME
)lParam
);
2940 return MONTHCAL_SetToday(infoPtr
, (LPSYSTEMTIME
)lParam
);
2943 return MONTHCAL_HitTest(infoPtr
, (PMCHITTESTINFO
)lParam
);
2945 case MCM_GETFIRSTDAYOFWEEK
:
2946 return MONTHCAL_GetFirstDayOfWeek(infoPtr
);
2948 case MCM_SETFIRSTDAYOFWEEK
:
2949 return MONTHCAL_SetFirstDayOfWeek(infoPtr
, (INT
)lParam
);
2952 return MONTHCAL_GetRange(infoPtr
, (LPSYSTEMTIME
)lParam
);
2955 return MONTHCAL_SetRange(infoPtr
, (SHORT
)wParam
, (LPSYSTEMTIME
)lParam
);
2957 case MCM_GETMONTHDELTA
:
2958 return MONTHCAL_GetMonthDelta(infoPtr
);
2960 case MCM_SETMONTHDELTA
:
2961 return MONTHCAL_SetMonthDelta(infoPtr
, wParam
);
2963 case MCM_GETMAXTODAYWIDTH
:
2964 return MONTHCAL_GetMaxTodayWidth(infoPtr
);
2966 case MCM_SETUNICODEFORMAT
:
2967 return MONTHCAL_SetUnicodeFormat(infoPtr
, (BOOL
)wParam
);
2969 case MCM_GETUNICODEFORMAT
:
2970 return MONTHCAL_GetUnicodeFormat(infoPtr
);
2972 case MCM_GETCALENDARCOUNT
:
2973 return MONTHCAL_GetCalCount(infoPtr
);
2976 return DLGC_WANTARROWS
| DLGC_WANTCHARS
;
2979 return MONTHCAL_RButtonUp(infoPtr
, lParam
);
2981 case WM_LBUTTONDOWN
:
2982 return MONTHCAL_LButtonDown(infoPtr
, lParam
);
2985 return MONTHCAL_MouseMove(infoPtr
, lParam
);
2988 return MONTHCAL_LButtonUp(infoPtr
, lParam
);
2991 return MONTHCAL_Paint(infoPtr
, (HDC
)wParam
);
2993 case WM_PRINTCLIENT
:
2994 return MONTHCAL_PrintClient(infoPtr
, (HDC
)wParam
, (DWORD
)lParam
);
2997 return MONTHCAL_EraseBkgnd(infoPtr
, (HDC
)wParam
);
3000 return MONTHCAL_SetFocus(infoPtr
);
3003 return MONTHCAL_Size(infoPtr
, (SHORT
)LOWORD(lParam
), (SHORT
)HIWORD(lParam
));
3006 return MONTHCAL_Notify(infoPtr
, (NMHDR
*)lParam
);
3009 return MONTHCAL_Create(hwnd
, (LPCREATESTRUCTW
)lParam
);
3012 return MONTHCAL_SetFont(infoPtr
, (HFONT
)wParam
, (BOOL
)lParam
);
3015 return MONTHCAL_GetFont(infoPtr
);
3018 return MONTHCAL_Timer(infoPtr
, wParam
);
3020 case WM_THEMECHANGED
:
3021 return theme_changed (infoPtr
);
3024 return MONTHCAL_Destroy(infoPtr
);
3026 case WM_SYSCOLORCHANGE
:
3027 COMCTL32_RefreshSysColors();
3030 case WM_STYLECHANGED
:
3031 return MONTHCAL_StyleChanged(infoPtr
, wParam
, (LPSTYLESTRUCT
)lParam
);
3033 case WM_STYLECHANGING
:
3034 return MONTHCAL_StyleChanging(infoPtr
, wParam
, (LPSTYLESTRUCT
)lParam
);
3037 if ((uMsg
>= WM_USER
) && (uMsg
< WM_APP
) && !COMCTL32_IsReflectedMessage(uMsg
))
3038 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg
, wParam
, lParam
);
3039 return DefWindowProcW(hwnd
, uMsg
, wParam
, lParam
);
3045 MONTHCAL_Register(void)
3049 ZeroMemory(&wndClass
, sizeof(WNDCLASSW
));
3050 wndClass
.style
= CS_GLOBALCLASS
;
3051 wndClass
.lpfnWndProc
= MONTHCAL_WindowProc
;
3052 wndClass
.cbClsExtra
= 0;
3053 wndClass
.cbWndExtra
= sizeof(MONTHCAL_INFO
*);
3054 wndClass
.hCursor
= LoadCursorW(0, (LPWSTR
)IDC_ARROW
);
3055 wndClass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
3056 wndClass
.lpszClassName
= MONTHCAL_CLASSW
;
3058 RegisterClassW(&wndClass
);
3063 MONTHCAL_Unregister(void)
3065 UnregisterClassW(MONTHCAL_CLASSW
, NULL
);