d3dx9: Implement ID3DXBaseEffect::GetMatrixTranspose().
[wine/multimedia.git] / dlls / comctl32 / monthcal.c
blobccede7fb84d9ab92d03c69d3e73c85c4b6205d01
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 ID's */
72 #define MC_TODAYUPDATETIMER 2
74 #define countof(arr) (sizeof(arr)/sizeof(arr[0]))
76 /* convert from days to 100 nanoseconds unit - used as FILETIME unit */
77 #define DAYSTO100NSECS(days) (((ULONGLONG)(days))*24*60*60*10000000)
79 enum CachedPen
81 PenRed = 0,
82 PenText,
83 PenLast
86 enum CachedBrush
88 BrushTitle = 0,
89 BrushMonth,
90 BrushBackground,
91 BrushLast
94 /* single calendar data */
95 typedef struct _CALENDAR_INFO
97 RECT title; /* rect for the header above the calendar */
98 RECT titlemonth; /* the 'month name' text in the header */
99 RECT titleyear; /* the 'year number' text in the header */
100 RECT wdays; /* week days at top */
101 RECT days; /* calendar area */
102 RECT weeknums; /* week numbers at left side */
104 SYSTEMTIME month;/* contains calendar main month/year */
105 } CALENDAR_INFO;
107 typedef struct
109 HWND hwndSelf;
110 DWORD dwStyle; /* cached GWL_STYLE */
112 COLORREF colors[MCSC_TRAILINGTEXT+1];
113 HBRUSH brushes[BrushLast];
114 HPEN pens[PenLast];
116 HFONT hFont;
117 HFONT hBoldFont;
118 int textHeight;
119 int textWidth;
120 int height_increment;
121 int width_increment;
122 INT delta; /* scroll rate; # of months that the */
123 /* control moves when user clicks a scroll button */
124 int visible; /* # of months visible */
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 int monthRange;
132 MONTHDAYSTATE *monthdayState;
133 SYSTEMTIME todaysDate;
134 BOOL todaySet; /* Today was forced with MCM_SETTODAY */
135 int status; /* See MC_SEL flags */
136 SYSTEMTIME firstSel; /* first selected day */
137 INT maxSelCount;
138 SYSTEMTIME minSel; /* contains single selection when used without MCS_MULTISELECT */
139 SYSTEMTIME maxSel;
140 SYSTEMTIME focusedSel; /* date currently focused with mouse movement */
141 DWORD rangeValid;
142 SYSTEMTIME minDate;
143 SYSTEMTIME maxDate;
145 RECT titlebtnnext; /* the `next month' button in the header */
146 RECT titlebtnprev; /* the `prev month' button in the header */
147 RECT todayrect; /* `today: xx/xx/xx' text rect */
148 HWND hwndNotify; /* Window to receive the notifications */
149 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
150 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
151 WNDPROC EditWndProc; /* original Edit window procedure */
153 CALENDAR_INFO *calendars;
154 INT cal_num;
155 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
157 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
159 /* empty SYSTEMTIME const */
160 static const SYSTEMTIME st_null;
161 /* valid date limits */
162 static const SYSTEMTIME max_allowed_date = { .wYear = 9999, .wMonth = 12, .wDay = 31 };
163 static const SYSTEMTIME min_allowed_date = { .wYear = 1752, .wMonth = 9, .wDay = 14 };
165 /* Prev/Next buttons */
166 enum nav_direction
168 DIRECTION_BACKWARD,
169 DIRECTION_FORWARD
172 /* helper functions */
174 /* send a single MCN_SELCHANGE notification */
175 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
177 NMSELCHANGE nmsc;
179 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
180 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
181 nmsc.nmhdr.code = MCN_SELCHANGE;
182 nmsc.stSelStart = infoPtr->minSel;
183 nmsc.stSelEnd = infoPtr->maxSel;
184 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
187 /* send a single MCN_SELECT notification */
188 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
190 NMSELCHANGE nmsc;
192 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
193 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
194 nmsc.nmhdr.code = MCN_SELECT;
195 nmsc.stSelStart = infoPtr->minSel;
196 nmsc.stSelEnd = infoPtr->maxSel;
198 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
201 /* returns the number of days in any given month, checking for leap days */
202 /* january is 1, december is 12 */
203 int MONTHCAL_MonthLength(int month, int year)
205 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
206 /* Wrap around, this eases handling. Getting length only we shouldn't care
207 about year change here cause January and December have
208 the same day quantity */
209 if(month == 0)
210 month = 12;
211 else if(month == 13)
212 month = 1;
214 /* special case for calendar transition year */
215 if(month == min_allowed_date.wMonth && year == min_allowed_date.wYear) return 19;
217 /* if we have a leap year add 1 day to February */
218 /* a leap year is a year either divisible by 400 */
219 /* or divisible by 4 and not by 100 */
220 if(month == 2) { /* February */
221 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
222 (year%4 == 0)) ? 1 : 0);
224 else {
225 return mdays[month - 1];
229 /* compares timestamps using date part only */
230 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
232 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
233 (first->wDay == second->wDay);
236 /* make sure that date fields are valid */
237 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
239 if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
240 if(time->wDayOfWeek > 6) return FALSE;
241 if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear))
242 return FALSE;
244 return TRUE;
247 /* Copies timestamp part only.
249 * PARAMETERS
251 * [I] from : source date
252 * [O] to : dest date
254 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
256 to->wHour = from->wHour;
257 to->wMinute = from->wMinute;
258 to->wSecond = from->wSecond;
261 /* Copies date part only.
263 * PARAMETERS
265 * [I] from : source date
266 * [O] to : dest date
268 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
270 to->wYear = from->wYear;
271 to->wMonth = from->wMonth;
272 to->wDay = from->wDay;
273 to->wDayOfWeek = from->wDayOfWeek;
276 /* Compares two dates in SYSTEMTIME format
278 * PARAMETERS
280 * [I] first : pointer to valid first date data to compare
281 * [I] second : pointer to valid second date data to compare
283 * RETURN VALUE
285 * -1 : first < second
286 * 0 : first == second
287 * 1 : first > second
289 * Note that no date validation performed, alreadt validated values expected.
291 static LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
293 FILETIME ft_first, ft_second;
295 SystemTimeToFileTime(first, &ft_first);
296 SystemTimeToFileTime(second, &ft_second);
298 return CompareFileTime(&ft_first, &ft_second);
301 static LONG MONTHCAL_CompareMonths(const SYSTEMTIME *first, const SYSTEMTIME *second)
303 SYSTEMTIME st_first, st_second;
305 st_first = st_second = st_null;
306 MONTHCAL_CopyDate(first, &st_first);
307 MONTHCAL_CopyDate(second, &st_second);
308 st_first.wDay = st_second.wDay = 1;
310 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
313 static LONG MONTHCAL_CompareDate(const SYSTEMTIME *first, const SYSTEMTIME *second)
315 SYSTEMTIME st_first, st_second;
317 st_first = st_second = st_null;
318 MONTHCAL_CopyDate(first, &st_first);
319 MONTHCAL_CopyDate(second, &st_second);
321 return MONTHCAL_CompareSystemTime(&st_first, &st_second);
324 /* Checks largest possible date range and configured one
326 * PARAMETERS
328 * [I] infoPtr : valid pointer to control data
329 * [I] date : pointer to valid date data to check
330 * [I] fix : make date fit valid range
332 * RETURN VALUE
334 * TRUE - date whithin largest and configured range
335 * FALSE - date is outside largest or configured range
337 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr,
338 SYSTEMTIME *date, BOOL fix)
340 const SYSTEMTIME *fix_st = NULL;
342 if(MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) {
343 fix_st = &max_allowed_date;
345 else if(MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1) {
346 fix_st = &min_allowed_date;
348 else if(infoPtr->rangeValid & GDTR_MAX) {
349 if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
350 fix_st = &infoPtr->maxDate;
353 else if(infoPtr->rangeValid & GDTR_MIN) {
354 if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
355 fix_st = &infoPtr->minDate;
359 if (fix && fix_st) {
360 date->wYear = fix_st->wYear;
361 date->wMonth = fix_st->wMonth;
364 return fix_st ? FALSE : TRUE;
367 /* Checks passed range width with configured maximum selection count
369 * PARAMETERS
371 * [I] infoPtr : valid pointer to control data
372 * [I] range0 : pointer to valid date data (requested bound)
373 * [I] range1 : pointer to valid date data (primary bound)
374 * [O] adjust : returns adjusted range bound to fit maximum range (optional)
376 * Adjust value computed basing on primary bound and current maximum selection
377 * count. For simple range check (without adjusted value required) (range0, range1)
378 * relation means nothing.
380 * RETURN VALUE
382 * TRUE - range is shorter or equal to maximum
383 * FALSE - range is larger than maximum
385 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
386 const SYSTEMTIME *range0,
387 const SYSTEMTIME *range1,
388 SYSTEMTIME *adjust)
390 ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
391 FILETIME ft_range0, ft_range1;
392 LONG cmp;
394 SystemTimeToFileTime(range0, &ft_range0);
395 SystemTimeToFileTime(range1, &ft_range1);
397 ul_range0.u.LowPart = ft_range0.dwLowDateTime;
398 ul_range0.u.HighPart = ft_range0.dwHighDateTime;
399 ul_range1.u.LowPart = ft_range1.dwLowDateTime;
400 ul_range1.u.HighPart = ft_range1.dwHighDateTime;
402 cmp = CompareFileTime(&ft_range0, &ft_range1);
404 if(cmp == 1)
405 ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
406 else
407 ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
409 if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
411 if(adjust) {
412 if(cmp == 1)
413 ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
414 else
415 ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
417 ft_range0.dwLowDateTime = ul_range0.u.LowPart;
418 ft_range0.dwHighDateTime = ul_range0.u.HighPart;
419 FileTimeToSystemTime(&ft_range0, adjust);
422 return FALSE;
424 else return TRUE;
427 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
428 Milliseconds are intentionally not validated. */
429 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
431 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
432 return FALSE;
433 else
434 return TRUE;
437 /* Note:Depending on DST, this may be offset by a day.
438 Need to find out if we're on a DST place & adjust the clock accordingly.
439 Above function assumes we have a valid data.
440 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
441 0 = Sunday.
444 /* Returns the day in the week
446 * PARAMETERS
447 * [i] date : input date
448 * [I] inplace : set calculated value back to date structure
450 * RETURN VALUE
451 * day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
453 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
455 SYSTEMTIME st = st_null;
456 FILETIME ft;
458 MONTHCAL_CopyDate(date, &st);
460 SystemTimeToFileTime(&st, &ft);
461 FileTimeToSystemTime(&ft, &st);
463 if (inplace) date->wDayOfWeek = st.wDayOfWeek;
465 return st.wDayOfWeek;
468 /* add/subtract 'months' from date */
469 static inline void MONTHCAL_GetMonth(SYSTEMTIME *date, INT months)
471 INT length, m = date->wMonth + months;
473 date->wYear += m > 0 ? (m - 1) / 12 : m / 12 - 1;
474 date->wMonth = m > 0 ? (m - 1) % 12 + 1 : 12 + m % 12;
475 /* fix moving from last day in a month */
476 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
477 if(date->wDay > length) date->wDay = length;
478 MONTHCAL_CalculateDayOfWeek(date, TRUE);
481 /* properly updates date to point on next month */
482 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
484 return MONTHCAL_GetMonth(date, 1);
487 /* properly updates date to point on prev month */
488 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
490 return MONTHCAL_GetMonth(date, -1);
493 /* Returns full date for a first currently visible day */
494 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
496 /* zero indexed calendar has the earliest date */
497 SYSTEMTIME st_first = infoPtr->calendars[0].month;
498 INT firstDay;
500 st_first.wDay = 1;
501 firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
503 *date = infoPtr->calendars[0].month;
504 MONTHCAL_GetPrevMonth(date);
506 date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
507 (infoPtr->firstDay - firstDay) % 7 + 1;
509 if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
510 date->wDay -= 7;
512 /* fix day of week */
513 MONTHCAL_CalculateDayOfWeek(date, TRUE);
516 /* Returns full date for a last currently visible day */
517 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
519 /* the latest date is in latest calendar */
520 SYSTEMTIME st, lt_month = infoPtr->calendars[infoPtr->cal_num-1].month;
522 *date = lt_month;
523 MONTHCAL_GetNextMonth(date);
525 MONTHCAL_GetMinDate(infoPtr, &st);
526 /* Use month length to get max day. 42 means max day count in calendar area */
527 date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
528 MONTHCAL_MonthLength(lt_month.wMonth, lt_month.wYear);
530 /* fix day of week */
531 MONTHCAL_CalculateDayOfWeek(date, TRUE);
534 /* From a given point, calculate the row (weekpos), column(daypos)
535 and day in the calendar. day== 0 mean the last day of tha last month
537 static int MONTHCAL_CalcDayFromPos(const MONTHCAL_INFO *infoPtr, int x, int y,
538 int *daypos, int *weekpos)
540 int retval, firstDay;
541 RECT rcClient;
542 SYSTEMTIME st = infoPtr->minSel;
544 GetClientRect(infoPtr->hwndSelf, &rcClient);
546 /* if the point is outside the x bounds of the window put
547 it at the boundary */
548 if (x > rcClient.right)
549 x = rcClient.right;
551 *daypos = (x - infoPtr->calendars[0].days.left ) / infoPtr->width_increment;
552 *weekpos = (y - infoPtr->calendars[0].days.top ) / infoPtr->height_increment;
554 st.wDay = 1;
555 firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
556 retval = *daypos + (7 * *weekpos) - firstDay;
557 return retval;
560 /* Sets the RECT struct r to the rectangle around the date
562 * PARAMETERS
564 * [I] infoPtr : pointer to control data
565 * [I] date : date value
566 * [O] x : day column (zero based)
567 * [O] y : week column (zero based)
569 static void MONTHCAL_CalcDayXY(const MONTHCAL_INFO *infoPtr,
570 const SYSTEMTIME *date, INT *x, INT *y)
572 SYSTEMTIME st = infoPtr->minSel;
573 LONG cmp;
574 int first;
576 st.wDay = 1;
577 first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
579 cmp = MONTHCAL_CompareMonths(date, &infoPtr->minSel);
581 /* previous month */
582 if(cmp == -1) {
583 *x = (first - MONTHCAL_MonthLength(date->wMonth, infoPtr->minSel.wYear) + date->wDay) % 7;
584 *y = 0;
585 return;
588 /* next month calculation is same as for current,
589 just add current month length */
590 if(cmp == 1) {
591 first += MONTHCAL_MonthLength(infoPtr->minSel.wMonth, infoPtr->minSel.wYear);
594 *x = (date->wDay + first) % 7;
595 *y = (date->wDay + first - *x) / 7;
599 /* x: column(day), y: row(week) */
600 static inline void MONTHCAL_CalcDayRect(const MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
602 r->left = infoPtr->calendars[0].days.left + x * infoPtr->width_increment;
603 r->right = r->left + infoPtr->width_increment;
604 r->top = infoPtr->calendars[0].days.top + y * infoPtr->height_increment;
605 r->bottom = r->top + infoPtr->textHeight;
609 /* Sets the RECT struct r to the rectangle around the date */
610 static inline void MONTHCAL_CalcPosFromDay(const MONTHCAL_INFO *infoPtr,
611 const SYSTEMTIME *date, RECT *r)
613 int x, y;
615 MONTHCAL_CalcDayXY(infoPtr, date, &x, &y);
616 MONTHCAL_CalcDayRect(infoPtr, r, x, y);
619 /* Focused day helper:
621 - set focused date to given value;
622 - reset to zero value if NULL passed;
623 - invalidate previous and new day rectangle only if needed.
625 Returns TRUE if focused day changed, FALSE otherwise.
627 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
629 RECT r;
631 if(st)
633 /* there's nothing to do if it's the same date,
634 mouse move within same date rectangle case */
635 if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
637 /* invalidate old focused day */
638 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
639 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
641 infoPtr->focusedSel = *st;
644 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
646 if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
647 infoPtr->focusedSel = st_null;
649 /* on set invalidates new day, on reset clears previous focused day */
650 InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
652 return TRUE;
655 /* draw today boundary box for specified rectangle */
656 static void MONTHCAL_Circle(const MONTHCAL_INFO *infoPtr, HDC hdc, const RECT *r)
658 HPEN old_pen = SelectObject(hdc, infoPtr->pens[PenRed]);
659 HBRUSH old_brush;
661 old_brush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
662 Rectangle(hdc, r->left, r->top, r->right, r->bottom);
664 SelectObject(hdc, old_brush);
665 SelectObject(hdc, old_pen);
668 /* Draw today day mark rectangle
670 * [I] hdc : context to draw in
671 * [I] date : day to mark with rectangle
674 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
675 const SYSTEMTIME *date)
677 RECT day_rect;
678 MONTHCAL_CalcPosFromDay(infoPtr, date, &day_rect);
679 MONTHCAL_Circle(infoPtr, hdc, &day_rect);
682 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
683 int bold, const PAINTSTRUCT *ps)
685 static const WCHAR fmtW[] = { '%','d',0 };
686 WCHAR buf[10];
687 RECT r, r_temp;
688 COLORREF oldCol = 0;
689 COLORREF oldBk = 0;
690 INT old_bkmode, selection;
692 /* no need to check styles: when selection is not valid, it is set to zero.
693 1 < day < 31, so everything is OK */
694 MONTHCAL_CalcPosFromDay(infoPtr, st, &r);
695 if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
697 if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
698 (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0))
701 TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
702 TRACE("%s\n", wine_dbgstr_rect(&r));
703 oldCol = SetTextColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
704 oldBk = SetBkColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
705 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
707 selection = 1;
709 else
710 selection = 0;
712 SelectObject(hdc, bold ? infoPtr->hBoldFont : infoPtr->hFont);
714 old_bkmode = SetBkMode(hdc, TRANSPARENT);
715 wsprintfW(buf, fmtW, st->wDay);
716 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
717 SetBkMode(hdc, old_bkmode);
719 if (selection)
721 SetTextColor(hdc, oldCol);
722 SetBkColor(hdc, oldBk);
727 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, enum nav_direction button)
729 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
730 RECT *r = button == DIRECTION_FORWARD ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
731 BOOL pressed = button == DIRECTION_FORWARD ? infoPtr->status & MC_NEXTPRESSED :
732 infoPtr->status & MC_PREVPRESSED;
733 if (theme)
735 static const int states[] = {
736 /* Prev button */
737 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
738 /* Next button */
739 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
741 int stateNum = button == DIRECTION_FORWARD ? 3 : 0;
742 if (pressed)
743 stateNum += 1;
744 else
746 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
748 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
750 else
752 int style = button == DIRECTION_FORWARD ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
753 if (pressed)
754 style |= DFCS_PUSHED;
755 else
757 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
760 DrawFrameControl(hdc, r, DFC_SCROLL, style);
763 /* paint a title with buttons and month/year string */
764 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
766 static const WCHAR fmt_monthW[] = { '%','s',' ','%','l','d',0 };
767 RECT *title = &infoPtr->calendars[calIdx].title;
768 const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
769 WCHAR buf_month[80], buf_fmt[80];
770 SIZE sz;
772 /* fill header box */
773 FillRect(hdc, title, infoPtr->brushes[BrushTitle]);
775 /* month/year string */
776 SetBkColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
777 SetTextColor(hdc, infoPtr->colors[MCSC_TITLETEXT]);
778 SelectObject(hdc, infoPtr->hBoldFont);
780 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1 + st->wMonth - 1,
781 buf_month, countof(buf_month));
783 wsprintfW(buf_fmt, fmt_monthW, buf_month, st->wYear);
784 DrawTextW(hdc, buf_fmt, strlenW(buf_fmt), title,
785 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
787 /* update title rectangles with current month - used while testing hits */
788 GetTextExtentPoint32W(hdc, buf_fmt, strlenW(buf_fmt), &sz);
789 infoPtr->calendars[calIdx].titlemonth.left = title->right / 2 + title->left / 2 - sz.cx / 2;
790 infoPtr->calendars[calIdx].titleyear.right = title->right / 2 + title->left / 2 + sz.cx / 2;
792 GetTextExtentPoint32W(hdc, buf_month, strlenW(buf_month), &sz);
793 infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
794 infoPtr->calendars[calIdx].titleyear.left = infoPtr->calendars[calIdx].titlemonth.right;
797 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
799 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
800 static const WCHAR fmt_weekW[] = { '%','d',0 };
801 INT mindays, weeknum, weeknum1, startofprescal;
802 INT i, prev_month;
803 SYSTEMTIME st;
804 WCHAR buf[80];
805 HPEN old_pen;
806 RECT r;
808 if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
810 MONTHCAL_GetMinDate(infoPtr, &st);
811 startofprescal = st.wDay;
812 st = *date;
814 prev_month = date->wMonth - 1;
815 if(prev_month == 0) prev_month = 12;
818 Rules what week to call the first week of a new year:
819 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
820 The week containing Jan 1 is the first week of year
821 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
822 First week of year must contain 4 days of the new year
823 LOCALE_IFIRSTWEEKOFYEAR == 1 (what contries?)
824 The first week of the year must contain only days of the new year
826 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
827 weeknum = atoiW(buf);
828 switch (weeknum)
830 case 1: mindays = 6;
831 break;
832 case 2: mindays = 3;
833 break;
834 case 0: mindays = 0;
835 break;
836 default:
837 WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
838 mindays = 0;
841 if (date->wMonth == 1)
843 /* calculate all those exceptions for january */
844 st.wDay = st.wMonth = 1;
845 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
846 if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
847 weeknum = 1;
848 else
850 weeknum = 0;
851 for(i = 0; i < 11; i++)
852 weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
854 weeknum += startofprescal + 7;
855 weeknum /= 7;
856 st.wYear -= 1;
857 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
858 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
861 else
863 weeknum = 0;
864 for(i = 0; i < prev_month - 1; i++)
865 weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
867 weeknum += startofprescal + 7;
868 weeknum /= 7;
869 st.wDay = st.wMonth = 1;
870 weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
871 if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
874 r = infoPtr->calendars[calIdx].weeknums;
876 /* erase whole week numbers area */
877 FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
878 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
880 /* reduce rectangle to one week number */
881 r.bottom = r.top + infoPtr->height_increment;
883 for(i = 0; i < 6; i++) {
884 if((i == 0) && (weeknum > 50))
886 wsprintfW(buf, fmt_weekW, weeknum);
887 weeknum = 0;
889 else if((i == 5) && (weeknum > 47))
891 wsprintfW(buf, fmt_weekW, 1);
893 else
894 wsprintfW(buf, fmt_weekW, weeknum + i);
896 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
897 OffsetRect(&r, 0, infoPtr->height_increment);
900 /* line separator for week numbers column */
901 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
902 MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
903 LineTo(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
904 SelectObject(hdc, old_pen);
907 /* bottom today date */
908 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
910 static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
911 WCHAR buf_todayW[30], buf_dateW[20], buf[80];
912 RECT text_rect, box_rect;
913 HFONT old_font;
914 INT col;
916 if(infoPtr->dwStyle & MCS_NOTODAY) return;
918 if (!LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, countof(buf_todayW)))
920 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
921 WARN("Can't load resource\n");
922 strcpyW(buf_todayW, todayW);
925 col = infoPtr->dwStyle & MCS_NOTODAYCIRCLE ? 0 : 1;
926 if (infoPtr->dwStyle & MCS_WEEKNUMBERS) col--;
927 MONTHCAL_CalcDayRect(infoPtr, &text_rect, col, 6);
928 box_rect = text_rect;
930 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
931 buf_dateW, countof(buf_dateW));
932 old_font = SelectObject(hdc, infoPtr->hBoldFont);
933 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
935 wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
936 DrawTextW(hdc, buf, -1, &text_rect, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
937 DrawTextW(hdc, buf, -1, &text_rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
939 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
940 OffsetRect(&box_rect, -infoPtr->width_increment, 0);
941 MONTHCAL_Circle(infoPtr, hdc, &box_rect);
944 SelectObject(hdc, old_font);
947 /* today mark + focus */
948 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
950 if((infoPtr->minSel.wMonth == infoPtr->todaysDate.wMonth) &&
951 (infoPtr->minSel.wYear == infoPtr->todaysDate.wYear) &&
952 !(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
954 MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
957 if(!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
959 RECT r;
960 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->focusedSel, &r);
961 DrawFocusRect(hdc, &r);
965 /* months before first calendar month and after last calendar month */
966 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
968 INT mask, length;
969 SYSTEMTIME st_max, st;
971 if (infoPtr->dwStyle & MCS_NOTRAILINGDATES) return;
973 SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
975 /* draw prev month */
976 MONTHCAL_GetMinDate(infoPtr, &st);
977 mask = 1 << (st.wDay-1);
978 /* December and January both 31 days long, so no worries if wrapped */
979 length = MONTHCAL_MonthLength(infoPtr->calendars[0].month.wMonth - 1,
980 infoPtr->calendars[0].month.wYear);
981 while(st.wDay <= length)
983 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[0] & mask, ps);
984 mask <<= 1;
985 st.wDay++;
988 /* draw next month */
989 st = infoPtr->calendars[infoPtr->cal_num-1].month;
990 st.wDay = 1;
991 MONTHCAL_GetNextMonth(&st);
992 MONTHCAL_GetMaxDate(infoPtr, &st_max);
993 mask = 1;
995 while(st.wDay <= st_max.wDay)
997 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[2] & mask, ps);
998 mask <<= 1;
999 st.wDay++;
1003 /* paint a calendar area */
1004 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
1006 const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
1007 INT i, j, length;
1008 RECT r, fill_bk_rect;
1009 SYSTEMTIME st;
1010 WCHAR buf[80];
1011 HPEN old_pen;
1012 int mask;
1014 /* fill whole days area - from week days area to today note rectangle */
1015 fill_bk_rect = infoPtr->calendars[calIdx].wdays;
1016 fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
1017 (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
1019 FillRect(hdc, &fill_bk_rect, infoPtr->brushes[BrushMonth]);
1021 /* draw line under day abbreviations */
1022 old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1023 MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
1024 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
1025 LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
1026 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
1027 SelectObject(hdc, old_pen);
1029 infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
1030 infoPtr->calendars[calIdx].weeknums.right;
1032 /* draw day abbreviations */
1033 SelectObject(hdc, infoPtr->hFont);
1034 SetBkColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
1035 SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1036 /* rectangle to draw a single day abbreviation within */
1037 r = infoPtr->calendars[calIdx].wdays;
1038 r.right = r.left + infoPtr->width_increment;
1040 i = infoPtr->firstDay;
1041 for(j = 0; j < 7; j++) {
1042 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
1043 DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1044 OffsetRect(&r, infoPtr->width_increment, 0);
1047 /* draw current month */
1048 SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1049 st = *date;
1050 st.wDay = 1;
1051 mask = 1;
1052 length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1053 while(st.wDay <= length)
1055 MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[1] & mask, ps);
1056 mask <<= 1;
1057 st.wDay++;
1061 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1063 COLORREF old_text_clr, old_bk_clr;
1064 HFONT old_font;
1065 INT i;
1067 old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1068 old_bk_clr = GetBkColor(hdc);
1069 old_font = GetCurrentObject(hdc, OBJ_FONT);
1071 for (i = 0; i < infoPtr->cal_num; i++)
1073 RECT *title = &infoPtr->calendars[i].title;
1074 RECT r;
1076 /* draw title, redraw all its elements */
1077 if (IntersectRect(&r, &(ps->rcPaint), title))
1078 MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1080 /* draw calendar area */
1081 UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1082 if (IntersectRect(&r, &(ps->rcPaint), &r))
1083 MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1085 /* week numbers */
1086 MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1089 /* partially visible months */
1090 MONTHCAL_PaintLeadTrailMonths(infoPtr, hdc, ps);
1092 /* focus and today rectangle */
1093 MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1095 /* today at the bottom left */
1096 MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1098 /* navigation buttons */
1099 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_BACKWARD);
1100 MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_FORWARD);
1102 /* restore context */
1103 SetBkColor(hdc, old_bk_clr);
1104 SelectObject(hdc, old_font);
1105 SetTextColor(hdc, old_text_clr);
1108 static LRESULT
1109 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, RECT *rect)
1111 TRACE("rect %p\n", rect);
1113 if(!rect) return FALSE;
1115 *rect = infoPtr->calendars[0].title;
1116 rect->bottom = infoPtr->calendars[0].days.bottom + infoPtr->todayrect.bottom -
1117 infoPtr->todayrect.top;
1119 AdjustWindowRect(rect, infoPtr->dwStyle, FALSE);
1121 /* minimal rectangle is zero based */
1122 OffsetRect(rect, -rect->left, -rect->top);
1124 TRACE("%s\n", wine_dbgstr_rect(rect));
1126 return TRUE;
1129 static COLORREF
1130 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, UINT index)
1132 TRACE("%p, %d\n", infoPtr, index);
1134 if (index > MCSC_TRAILINGTEXT) return -1;
1135 return infoPtr->colors[index];
1138 static LRESULT
1139 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, UINT index, COLORREF color)
1141 enum CachedBrush type;
1142 COLORREF prev;
1144 TRACE("%p, %d: color %08x\n", infoPtr, index, color);
1146 if (index > MCSC_TRAILINGTEXT) return -1;
1148 prev = infoPtr->colors[index];
1149 infoPtr->colors[index] = color;
1151 /* update cached brush */
1152 switch (index)
1154 case MCSC_BACKGROUND:
1155 type = BrushBackground;
1156 break;
1157 case MCSC_TITLEBK:
1158 type = BrushTitle;
1159 break;
1160 case MCSC_MONTHBK:
1161 type = BrushMonth;
1162 break;
1163 default:
1164 type = BrushLast;
1167 if (type != BrushLast)
1169 DeleteObject(infoPtr->brushes[type]);
1170 infoPtr->brushes[type] = CreateSolidBrush(color);
1173 /* update cached pen */
1174 if (index == MCSC_TEXT)
1176 DeleteObject(infoPtr->pens[PenText]);
1177 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[index]);
1180 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND ? TRUE : FALSE);
1181 return prev;
1184 static LRESULT
1185 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1187 TRACE("\n");
1189 if(infoPtr->delta)
1190 return infoPtr->delta;
1191 else
1192 return infoPtr->visible;
1196 static LRESULT
1197 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1199 INT prev = infoPtr->delta;
1201 TRACE("delta %d\n", delta);
1203 infoPtr->delta = delta;
1204 return prev;
1208 static inline LRESULT
1209 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1211 int day;
1213 /* convert from SYSTEMTIME to locale format */
1214 day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1216 return MAKELONG(day, infoPtr->firstDaySet);
1220 /* Sets the first day of the week that will appear in the control
1223 * PARAMETERS:
1224 * [I] infoPtr : valid pointer to control data
1225 * [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1228 * RETURN VALUE:
1229 * Low word contains previous first day,
1230 * high word indicates was first day forced with this message before or is
1231 * locale difined (TRUE - was forced, FALSE - wasn't).
1233 * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1234 * FIXME: we need more error checking here
1236 static LRESULT
1237 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1239 LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1240 int new_day;
1242 TRACE("%d\n", day);
1244 if(day == -1)
1246 WCHAR buf[80];
1248 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1249 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1251 new_day = atoiW(buf);
1253 infoPtr->firstDaySet = FALSE;
1255 else if(day >= 7)
1257 new_day = 6; /* max first day allowed */
1258 infoPtr->firstDaySet = TRUE;
1260 else
1262 /* Native behaviour for that case is broken: invalid date number >31
1263 got displayed at (0,0) position, current month starts always from
1264 (1,0) position. Should be implemented here as well only if there's
1265 nothing else to do. */
1266 if (day < -1)
1267 FIXME("No bug compatibility for day=%d\n", day);
1269 new_day = day;
1270 infoPtr->firstDaySet = TRUE;
1273 /* convert from locale to SYSTEMTIME format */
1274 infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1276 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1278 return prev;
1282 static LRESULT
1283 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
1285 TRACE("flag=%d, st=%p\n", flag, st);
1287 if(st)
1289 switch (flag) {
1290 case GMR_VISIBLE:
1292 st[0] = infoPtr->calendars[0].month;
1293 st[1] = infoPtr->calendars[infoPtr->cal_num-1].month;
1295 if (st[0].wMonth == min_allowed_date.wMonth &&
1296 st[0].wYear == min_allowed_date.wYear)
1298 st[0].wDay = min_allowed_date.wDay;
1300 else
1301 st[0].wDay = 1;
1302 MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
1304 st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
1305 MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
1307 return infoPtr->cal_num;
1309 case GMR_DAYSTATE:
1311 MONTHCAL_GetMinDate(infoPtr, &st[0]);
1312 MONTHCAL_GetMaxDate(infoPtr, &st[1]);
1313 break;
1315 default:
1316 WARN("Unknown flag value, got %d\n", flag);
1320 return infoPtr->monthRange;
1324 static LRESULT
1325 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1327 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1331 static LRESULT
1332 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1334 FILETIME ft_min, ft_max;
1336 TRACE("%x %p\n", limits, range);
1338 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1339 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1340 return FALSE;
1342 if (limits & GDTR_MIN)
1344 if (!MONTHCAL_ValidateTime(&range[0]))
1345 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1347 infoPtr->minDate = range[0];
1348 infoPtr->rangeValid |= GDTR_MIN;
1350 if (limits & GDTR_MAX)
1352 if (!MONTHCAL_ValidateTime(&range[1]))
1353 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1355 infoPtr->maxDate = range[1];
1356 infoPtr->rangeValid |= GDTR_MAX;
1359 /* Only one limit set - we are done */
1360 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1361 return TRUE;
1363 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1364 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1366 if (CompareFileTime(&ft_min, &ft_max) >= 0)
1368 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1370 /* Native swaps limits only when both limits are being set. */
1371 SYSTEMTIME st_tmp = infoPtr->minDate;
1372 infoPtr->minDate = infoPtr->maxDate;
1373 infoPtr->maxDate = st_tmp;
1375 else
1377 /* reset the other limit */
1378 if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1379 if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1380 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1384 return TRUE;
1388 static LRESULT
1389 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1391 TRACE("%p\n", range);
1393 if(!range) return FALSE;
1395 range[1] = infoPtr->maxDate;
1396 range[0] = infoPtr->minDate;
1398 return infoPtr->rangeValid;
1402 static LRESULT
1403 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1405 TRACE("%p %d %p\n", infoPtr, months, states);
1406 if(months != infoPtr->monthRange) return 0;
1408 memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1410 return 1;
1413 static LRESULT
1414 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1416 TRACE("%p\n", curSel);
1417 if(!curSel) return FALSE;
1418 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1420 *curSel = infoPtr->minSel;
1421 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1422 return TRUE;
1425 static LRESULT
1426 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1428 SYSTEMTIME prev = infoPtr->minSel;
1429 WORD day;
1431 TRACE("%p\n", curSel);
1432 if(!curSel) return FALSE;
1433 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1435 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1436 /* exit earlier if selection equals current */
1437 if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1439 if(!MONTHCAL_IsDateInValidRange(infoPtr, curSel, FALSE)) return FALSE;
1441 infoPtr->calendars[0].month = *curSel;
1442 infoPtr->minSel = *curSel;
1443 infoPtr->maxSel = *curSel;
1445 /* if selection is still in current month, reduce rectangle */
1446 day = prev.wDay;
1447 prev.wDay = curSel->wDay;
1448 if (MONTHCAL_IsDateEqual(&prev, curSel))
1450 RECT r_prev, r_new;
1452 prev.wDay = day;
1453 MONTHCAL_CalcPosFromDay(infoPtr, &prev, &r_prev);
1454 MONTHCAL_CalcPosFromDay(infoPtr, curSel, &r_new);
1456 InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1457 InvalidateRect(infoPtr->hwndSelf, &r_new, FALSE);
1459 else
1460 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1462 return TRUE;
1466 static LRESULT
1467 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1469 return infoPtr->maxSelCount;
1473 static LRESULT
1474 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1476 TRACE("%d\n", max);
1478 if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1479 if(max <= 0) return FALSE;
1481 infoPtr->maxSelCount = max;
1483 return TRUE;
1487 static LRESULT
1488 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1490 TRACE("%p\n", range);
1492 if(!range) return FALSE;
1494 if(infoPtr->dwStyle & MCS_MULTISELECT)
1496 range[1] = infoPtr->maxSel;
1497 range[0] = infoPtr->minSel;
1498 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1499 return TRUE;
1502 return FALSE;
1506 static LRESULT
1507 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1509 TRACE("%p\n", range);
1511 if(!range) return FALSE;
1513 if(infoPtr->dwStyle & MCS_MULTISELECT)
1515 SYSTEMTIME old_range[2];
1517 /* adjust timestamps */
1518 if(!MONTHCAL_ValidateTime(&range[0]))
1519 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1520 if(!MONTHCAL_ValidateTime(&range[1]))
1521 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1523 /* maximum range exceeded */
1524 if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1526 old_range[0] = infoPtr->minSel;
1527 old_range[1] = infoPtr->maxSel;
1529 /* swap if min > max */
1530 if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1532 infoPtr->minSel = range[0];
1533 infoPtr->maxSel = range[1];
1535 else
1537 infoPtr->minSel = range[1];
1538 infoPtr->maxSel = range[0];
1540 infoPtr->calendars[0].month = infoPtr->minSel;
1542 /* update day of week */
1543 MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1544 MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1546 /* redraw if bounds changed */
1547 /* FIXME: no actual need to redraw everything */
1548 if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1549 !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1551 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1554 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1555 return TRUE;
1558 return FALSE;
1562 static LRESULT
1563 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1565 TRACE("%p\n", today);
1567 if(!today) return FALSE;
1568 *today = infoPtr->todaysDate;
1569 return TRUE;
1572 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1574 * RETURN VALUE
1576 * TRUE - today date changed
1577 * FALSE - today date isn't changed
1579 static BOOL
1580 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1582 RECT new_r, old_r;
1584 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1586 MONTHCAL_CalcPosFromDay(infoPtr, &infoPtr->todaysDate, &old_r);
1587 MONTHCAL_CalcPosFromDay(infoPtr, today, &new_r);
1589 infoPtr->todaysDate = *today;
1591 /* only two days need redrawing */
1592 InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1593 InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1594 return TRUE;
1597 /* MCM_SETTODAT handler */
1598 static LRESULT
1599 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1601 TRACE("%p\n", today);
1603 if(!today) return FALSE;
1605 /* remember if date was set successfully */
1606 if(MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1608 return TRUE;
1611 /* returns calendar index containing specified point, or -1 if it's background */
1612 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1614 RECT r;
1615 INT i;
1617 for (i = 0; i < infoPtr->cal_num; i++)
1619 /* whole bounding rectangle allows some optimization to compute */
1620 r.left = infoPtr->calendars[i].title.left;
1621 r.top = infoPtr->calendars[i].title.top;
1622 r.bottom = infoPtr->calendars[i].days.bottom;
1623 r.right = infoPtr->calendars[i].days.right;
1625 if (PtInRect(&r, *pt)) return i;
1628 return -1;
1631 static inline UINT fill_hittest_info(const MCHITTESTINFO *src, MCHITTESTINFO *dest)
1633 dest->uHit = src->uHit;
1634 dest->st = src->st;
1636 if (dest->cbSize == sizeof(MCHITTESTINFO))
1637 memcpy(&dest->rc, &src->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1639 return src->uHit;
1642 static LRESULT
1643 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1645 INT day, wday, wnum, calIdx;
1646 MCHITTESTINFO htinfo;
1647 SYSTEMTIME ht_month;
1648 UINT x, y;
1650 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1652 x = lpht->pt.x;
1653 y = lpht->pt.y;
1655 htinfo.st = st_null;
1657 /* we should preserve passed fields if hit area doesn't need them */
1658 if (lpht->cbSize == sizeof(MCHITTESTINFO))
1659 memcpy(&htinfo.rc, &lpht->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1661 /* Comment in for debugging...
1662 TRACE("%d %d wd[%d %d %d %d] d[%d %d %d %d] t[%d %d %d %d] wn[%d %d %d %d]\n", x, y,
1663 infoPtr->wdays.left, infoPtr->wdays.right,
1664 infoPtr->wdays.top, infoPtr->wdays.bottom,
1665 infoPtr->days.left, infoPtr->days.right,
1666 infoPtr->days.top, infoPtr->days.bottom,
1667 infoPtr->todayrect.left, infoPtr->todayrect.right,
1668 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1669 infoPtr->weeknums.left, infoPtr->weeknums.right,
1670 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1673 /* guess in what calendar we are */
1674 calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1675 if (calIdx == -1)
1677 if (PtInRect(&infoPtr->todayrect, lpht->pt))
1679 htinfo.uHit = MCHT_TODAYLINK;
1680 htinfo.rc = infoPtr->todayrect;
1682 else
1683 /* outside of calendar area? What's left must be background :-) */
1684 htinfo.uHit = MCHT_CALENDARBK;
1686 return fill_hittest_info(&htinfo, lpht);
1689 ht_month = infoPtr->calendars[calIdx].month;
1691 /* are we in the header? */
1692 if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1693 /* FIXME: buttons hittesting could be optimized cause maximum
1694 two calendars have buttons */
1695 if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1697 htinfo.uHit = MCHT_TITLEBTNPREV;
1698 htinfo.rc = infoPtr->titlebtnprev;
1700 else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1702 htinfo.uHit = MCHT_TITLEBTNNEXT;
1703 htinfo.rc = infoPtr->titlebtnnext;
1705 else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1707 htinfo.uHit = MCHT_TITLEMONTH;
1708 htinfo.rc = infoPtr->calendars[calIdx].titlemonth;
1709 htinfo.iOffset = calIdx;
1711 else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1713 htinfo.uHit = MCHT_TITLEYEAR;
1714 htinfo.rc = infoPtr->calendars[calIdx].titleyear;
1715 htinfo.iOffset = calIdx;
1717 else
1719 htinfo.uHit = MCHT_TITLE;
1720 htinfo.rc = infoPtr->calendars[calIdx].title;
1721 htinfo.iOffset = calIdx;
1724 return fill_hittest_info(&htinfo, lpht);
1727 /* days area (including week days and week numbers */
1728 day = MONTHCAL_CalcDayFromPos(infoPtr, x, y, &wday, &wnum);
1729 if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1731 htinfo.uHit = MCHT_CALENDARDAY;
1732 htinfo.iOffset = calIdx;
1733 htinfo.st.wYear = ht_month.wYear;
1734 htinfo.st.wMonth = (day < 1) ? ht_month.wMonth -1 : ht_month.wMonth;
1735 htinfo.st.wDay = (day < 1) ?
1736 MONTHCAL_MonthLength(ht_month.wMonth-1, ht_month.wYear) - day : day;
1738 MONTHCAL_CalcDayXY(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow);
1740 else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1742 htinfo.uHit = MCHT_CALENDARWEEKNUM;
1743 htinfo.st.wYear = ht_month.wYear;
1744 htinfo.iOffset = calIdx;
1746 if (day < 1)
1748 htinfo.st.wMonth = ht_month.wMonth - 1;
1749 htinfo.st.wDay = MONTHCAL_MonthLength(ht_month.wMonth-1, ht_month.wYear) - day;
1751 else if (day > MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear))
1753 htinfo.st.wMonth = ht_month.wMonth + 1;
1754 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear);
1756 else
1758 htinfo.st.wMonth = ht_month.wMonth;
1759 htinfo.st.wDay = day;
1762 else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1764 htinfo.iOffset = calIdx;
1765 htinfo.st.wYear = ht_month.wYear;
1766 htinfo.st.wMonth = ht_month.wMonth;
1767 if (day < 1)
1769 htinfo.uHit = MCHT_CALENDARDATEPREV;
1770 MONTHCAL_GetPrevMonth(&htinfo.st);
1771 htinfo.st.wDay = MONTHCAL_MonthLength(htinfo.st.wMonth, htinfo.st.wYear) + day;
1773 else if (day > MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear))
1775 htinfo.uHit = MCHT_CALENDARDATENEXT;
1776 MONTHCAL_GetNextMonth(&htinfo.st);
1777 htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month.wMonth, ht_month.wYear);
1779 else
1781 htinfo.uHit = MCHT_CALENDARDATE;
1782 htinfo.st.wDay = day;
1785 MONTHCAL_CalcDayXY(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow);
1786 MONTHCAL_CalcDayRect(infoPtr, &htinfo.rc, htinfo.iCol, htinfo.iRow);
1787 /* always update day of week */
1788 MONTHCAL_CalculateDayOfWeek(&htinfo.st, TRUE);
1791 return fill_hittest_info(&htinfo, lpht);
1794 /* MCN_GETDAYSTATE notification helper */
1795 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1797 if(infoPtr->dwStyle & MCS_DAYSTATE) {
1798 NMDAYSTATE nmds;
1800 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1801 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1802 nmds.nmhdr.code = MCN_GETDAYSTATE;
1803 nmds.cDayState = infoPtr->monthRange;
1804 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1806 nmds.stStart = infoPtr->todaysDate;
1807 nmds.stStart.wYear = infoPtr->minSel.wYear;
1808 nmds.stStart.wMonth = infoPtr->minSel.wMonth;
1809 nmds.stStart.wDay = 1;
1811 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1812 memcpy(infoPtr->monthdayState, nmds.prgDayState, infoPtr->monthRange*sizeof(MONTHDAYSTATE));
1814 Free(nmds.prgDayState);
1818 /* no valid range check performed */
1819 static void MONTHCAL_Scroll(MONTHCAL_INFO *infoPtr, INT delta)
1821 INT i, selIdx = -1;
1823 for(i = 0; i < infoPtr->cal_num; i++)
1825 /* save selection position to shift it later */
1826 if (selIdx == -1 && MONTHCAL_CompareMonths(&infoPtr->minSel, &infoPtr->calendars[i].month) == 0)
1827 selIdx = i;
1829 MONTHCAL_GetMonth(&infoPtr->calendars[i].month, delta);
1832 /* selection is always shifted to first calendar */
1833 if(infoPtr->dwStyle & MCS_MULTISELECT)
1835 SYSTEMTIME range[2];
1837 MONTHCAL_GetSelRange(infoPtr, range);
1838 MONTHCAL_GetMonth(&range[0], delta - selIdx);
1839 MONTHCAL_GetMonth(&range[1], delta - selIdx);
1840 MONTHCAL_SetSelRange(infoPtr, range);
1842 else
1844 SYSTEMTIME st = infoPtr->minSel;
1846 MONTHCAL_GetMonth(&st, delta - selIdx);
1847 MONTHCAL_SetCurSel(infoPtr, &st);
1851 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
1853 INT delta = infoPtr->delta ? infoPtr->delta : infoPtr->cal_num;
1854 SYSTEMTIME st;
1856 TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
1858 /* check if change allowed by range set */
1859 if(direction == DIRECTION_BACKWARD)
1861 st = infoPtr->calendars[0].month;
1862 MONTHCAL_GetMonth(&st, -delta);
1864 else
1866 st = infoPtr->calendars[infoPtr->cal_num-1].month;
1867 MONTHCAL_GetMonth(&st, delta);
1870 if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
1872 MONTHCAL_Scroll(infoPtr, direction == DIRECTION_BACKWARD ? -delta : delta);
1873 MONTHCAL_NotifyDayState(infoPtr);
1874 MONTHCAL_NotifySelectionChange(infoPtr);
1877 static LRESULT
1878 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1880 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1881 HMENU hMenu;
1882 POINT menupoint;
1883 WCHAR buf[32];
1885 hMenu = CreatePopupMenu();
1886 if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1888 WARN("Can't load resource\n");
1889 strcpyW(buf, todayW);
1891 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1892 menupoint.x = (short)LOWORD(lParam);
1893 menupoint.y = (short)HIWORD(lParam);
1894 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1895 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
1896 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1898 infoPtr->calendars[0].month = infoPtr->todaysDate;
1899 infoPtr->minSel = infoPtr->todaysDate;
1900 infoPtr->maxSel = infoPtr->todaysDate;
1901 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1904 return 0;
1907 /***
1908 * DESCRIPTION:
1909 * Subclassed edit control windproc function
1911 * PARAMETER(S):
1912 * [I] hwnd : the edit window handle
1913 * [I] uMsg : the message that is to be processed
1914 * [I] wParam : first message parameter
1915 * [I] lParam : second message parameter
1918 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1920 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
1922 TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
1923 hwnd, uMsg, wParam, lParam);
1925 switch (uMsg)
1927 case WM_GETDLGCODE:
1928 return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
1930 case WM_DESTROY:
1932 WNDPROC editProc = infoPtr->EditWndProc;
1933 infoPtr->EditWndProc = NULL;
1934 SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
1935 return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
1938 case WM_KILLFOCUS:
1939 break;
1941 case WM_KEYDOWN:
1942 if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
1943 break;
1945 default:
1946 return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
1949 SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
1950 SendMessageW(hwnd, WM_CLOSE, 0, 0);
1951 return 0;
1954 /* creates updown control and edit box */
1955 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr, INT calIdx)
1957 RECT *rc = &infoPtr->calendars[calIdx].titleyear;
1958 RECT *title = &infoPtr->calendars[calIdx].title;
1960 infoPtr->hWndYearEdit =
1961 CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
1962 rc->left + 3, (title->bottom + title->top - infoPtr->textHeight) / 2,
1963 rc->right - rc->left + 4,
1964 infoPtr->textHeight, infoPtr->hwndSelf,
1965 NULL, NULL, NULL);
1967 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
1969 infoPtr->hWndYearUpDown =
1970 CreateWindowExW(0, UPDOWN_CLASSW, 0,
1971 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
1972 rc->right + 7, (title->bottom + title->top - infoPtr->textHeight) / 2,
1973 18, infoPtr->textHeight, infoPtr->hwndSelf,
1974 NULL, NULL, NULL);
1976 /* attach edit box */
1977 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
1978 MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
1979 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
1980 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->calendars[calIdx].month.wYear);
1982 /* subclass edit box */
1983 infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
1984 GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
1986 SetFocus(infoPtr->hWndYearEdit);
1989 static LRESULT
1990 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1992 MCHITTESTINFO ht;
1993 DWORD hit;
1995 /* Actually we don't need input focus for calendar, this is used to kill
1996 year updown and its buddy edit box */
1997 if (IsWindow(infoPtr->hWndYearUpDown))
1999 SetFocus(infoPtr->hwndSelf);
2000 return 0;
2003 SetCapture(infoPtr->hwndSelf);
2005 ht.cbSize = sizeof(MCHITTESTINFO);
2006 ht.pt.x = (short)LOWORD(lParam);
2007 ht.pt.y = (short)HIWORD(lParam);
2009 hit = MONTHCAL_HitTest(infoPtr, &ht);
2011 TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
2013 switch(hit)
2015 case MCHT_TITLEBTNNEXT:
2016 MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2017 infoPtr->status = MC_NEXTPRESSED;
2018 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2019 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2020 return 0;
2022 case MCHT_TITLEBTNPREV:
2023 MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2024 infoPtr->status = MC_PREVPRESSED;
2025 SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2026 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2027 return 0;
2029 case MCHT_TITLEMONTH:
2031 HMENU hMenu = CreatePopupMenu();
2032 WCHAR buf[32];
2033 POINT menupoint;
2034 INT i;
2036 for (i = 0; i < 12; i++)
2038 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
2039 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
2041 menupoint.x = ht.pt.x;
2042 menupoint.y = ht.pt.y;
2043 ClientToScreen(infoPtr->hwndSelf, &menupoint);
2044 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
2045 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
2047 if ((i > 0) && (i < 13) && infoPtr->calendars[ht.iOffset].month.wMonth != i)
2049 INT delta = i - infoPtr->calendars[ht.iOffset].month.wMonth;
2050 SYSTEMTIME st;
2052 /* check if change allowed by range set */
2053 st = delta < 0 ? infoPtr->calendars[0].month :
2054 infoPtr->calendars[infoPtr->cal_num-1].month;
2055 MONTHCAL_GetMonth(&st, delta);
2057 if (MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE))
2059 MONTHCAL_Scroll(infoPtr, delta);
2060 MONTHCAL_NotifyDayState(infoPtr);
2061 MONTHCAL_NotifySelectionChange(infoPtr);
2062 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2065 return 0;
2067 case MCHT_TITLEYEAR:
2069 MONTHCAL_EditYear(infoPtr, ht.iOffset);
2070 return 0;
2072 case MCHT_TODAYLINK:
2074 infoPtr->calendars[0].month = infoPtr->todaysDate;
2075 infoPtr->minSel = infoPtr->todaysDate;
2076 infoPtr->maxSel = infoPtr->todaysDate;
2077 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2079 MONTHCAL_NotifySelectionChange(infoPtr);
2080 MONTHCAL_NotifySelect(infoPtr);
2081 return 0;
2083 case MCHT_CALENDARDATENEXT:
2084 case MCHT_CALENDARDATEPREV:
2085 case MCHT_CALENDARDATE:
2087 SYSTEMTIME st[2];
2089 MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2091 st[0] = st[1] = ht.st;
2092 /* clear selection range */
2093 MONTHCAL_SetSelRange(infoPtr, st);
2095 infoPtr->status = MC_SEL_LBUTDOWN;
2096 MONTHCAL_SetDayFocus(infoPtr, &ht.st);
2097 return 0;
2101 return 1;
2105 static LRESULT
2106 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2108 NMHDR nmhdr;
2109 MCHITTESTINFO ht;
2110 DWORD hit;
2112 TRACE("\n");
2114 if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2115 RECT *r;
2117 KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2118 r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2119 infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2121 InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2124 ReleaseCapture();
2126 /* always send NM_RELEASEDCAPTURE notification */
2127 nmhdr.hwndFrom = infoPtr->hwndSelf;
2128 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2129 nmhdr.code = NM_RELEASEDCAPTURE;
2130 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2132 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2134 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2136 ht.cbSize = sizeof(MCHITTESTINFO);
2137 ht.pt.x = (short)LOWORD(lParam);
2138 ht.pt.y = (short)HIWORD(lParam);
2139 hit = MONTHCAL_HitTest(infoPtr, &ht);
2141 infoPtr->status = MC_SEL_LBUTUP;
2142 MONTHCAL_SetDayFocus(infoPtr, NULL);
2144 if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2146 SYSTEMTIME sel = infoPtr->minSel;
2148 /* will be invalidated here */
2149 MONTHCAL_SetCurSel(infoPtr, &ht.st);
2151 /* send MCN_SELCHANGE only if new date selected */
2152 if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2153 MONTHCAL_NotifySelectionChange(infoPtr);
2155 MONTHCAL_NotifySelect(infoPtr);
2158 return 0;
2162 static LRESULT
2163 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2165 TRACE("%ld\n", id);
2167 switch(id) {
2168 case MC_PREVNEXTMONTHTIMER:
2169 if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2170 if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2171 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2172 break;
2173 case MC_TODAYUPDATETIMER:
2175 SYSTEMTIME st;
2177 if(infoPtr->todaySet) return 0;
2179 GetLocalTime(&st);
2180 MONTHCAL_UpdateToday(infoPtr, &st);
2182 /* notification sent anyway */
2183 MONTHCAL_NotifySelectionChange(infoPtr);
2185 return 0;
2187 default:
2188 ERR("got unknown timer %ld\n", id);
2189 break;
2192 return 0;
2196 static LRESULT
2197 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2199 MCHITTESTINFO ht;
2200 SYSTEMTIME st_ht;
2201 INT hit;
2202 RECT r;
2204 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2206 ht.cbSize = sizeof(MCHITTESTINFO);
2207 ht.pt.x = (short)LOWORD(lParam);
2208 ht.pt.y = (short)HIWORD(lParam);
2210 hit = MONTHCAL_HitTest(infoPtr, &ht);
2212 /* not on the calendar date numbers? bail out */
2213 TRACE("hit:%x\n",hit);
2214 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2216 MONTHCAL_SetDayFocus(infoPtr, NULL);
2217 return 0;
2220 st_ht = ht.st;
2222 /* if pointer is over focused day still there's nothing to do */
2223 if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2225 MONTHCAL_CalcPosFromDay(infoPtr, &ht.st, &r);
2227 if(infoPtr->dwStyle & MCS_MULTISELECT) {
2228 SYSTEMTIME st[2];
2230 MONTHCAL_GetSelRange(infoPtr, st);
2232 /* If we're still at the first selected date and range is empty, return.
2233 If range isn't empty we should change range to a single firstSel */
2234 if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2235 MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2237 MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2239 st[0] = infoPtr->firstSel;
2240 /* we should overwrite timestamp here */
2241 MONTHCAL_CopyDate(&st_ht, &st[1]);
2243 /* bounds will be swapped here if needed */
2244 MONTHCAL_SetSelRange(infoPtr, st);
2246 return 0;
2249 done:
2251 /* FIXME: this should specify a rectangle containing only the days that changed
2252 using InvalidateRect */
2253 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2255 return 0;
2259 static LRESULT
2260 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2262 HDC hdc;
2263 PAINTSTRUCT ps;
2265 if (hdc_paint)
2267 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2268 hdc = hdc_paint;
2270 else
2271 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2273 MONTHCAL_Refresh(infoPtr, hdc, &ps);
2274 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2275 return 0;
2278 static LRESULT
2279 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2281 RECT rc;
2283 if (!GetClipBox(hdc, &rc)) return FALSE;
2285 FillRect(hdc, &rc, infoPtr->brushes[BrushBackground]);
2287 return TRUE;
2290 static LRESULT
2291 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2293 FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2295 if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2296 return 0;
2298 if (options & PRF_ERASEBKGND)
2299 MONTHCAL_EraseBkgnd(infoPtr, hdc);
2301 if (options & PRF_CLIENT)
2302 MONTHCAL_Paint(infoPtr, hdc);
2304 return 0;
2307 static LRESULT
2308 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2310 TRACE("\n");
2312 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2314 return 0;
2317 /* sets the size information */
2318 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2320 static const WCHAR O0W[] = { '0','0',0 };
2321 HDC hdc = GetDC(infoPtr->hwndSelf);
2322 RECT *title=&infoPtr->calendars[0].title;
2323 RECT *prev=&infoPtr->titlebtnprev;
2324 RECT *next=&infoPtr->titlebtnnext;
2325 RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2326 RECT *titleyear=&infoPtr->calendars[0].titleyear;
2327 RECT *wdays=&infoPtr->calendars[0].wdays;
2328 RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2329 RECT *days=&infoPtr->calendars[0].days;
2330 RECT *todayrect=&infoPtr->todayrect;
2331 SIZE size, sz;
2332 TEXTMETRICW tm;
2333 HFONT currentFont;
2334 INT xdiv, dx, dy, i;
2335 RECT rcClient;
2336 WCHAR buff[80];
2338 GetClientRect(infoPtr->hwndSelf, &rcClient);
2340 currentFont = SelectObject(hdc, infoPtr->hFont);
2342 /* get the height and width of each day's text */
2343 GetTextMetricsW(hdc, &tm);
2344 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2346 /* find largest abbreviated day name for current locale */
2347 size.cx = sz.cx = 0;
2348 for (i = 0; i < 7; i++)
2350 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + i,
2351 buff, countof(buff)))
2353 GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2354 if (sz.cx > size.cx) size.cx = sz.cx;
2356 else /* locale independent fallback on failure */
2358 static const WCHAR SunW[] = { 'S','u','n',0 };
2360 GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2361 break;
2365 infoPtr->textWidth = size.cx + 2;
2367 /* recalculate the height and width increments and offsets */
2368 GetTextExtentPoint32W(hdc, O0W, 2, &size);
2370 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2372 infoPtr->width_increment = size.cx * 2 + 4;
2373 infoPtr->height_increment = infoPtr->textHeight;
2375 /* calculate title area */
2376 title->top = 0;
2377 title->bottom = 3 * infoPtr->height_increment / 2;
2378 title->left = 0;
2379 title->right = infoPtr->width_increment * xdiv;
2381 /* set the dimensions of the next and previous buttons and center */
2382 /* the month text vertically */
2383 prev->top = next->top = title->top + 4;
2384 prev->bottom = next->bottom = title->bottom - 4;
2385 prev->left = title->left + 4;
2386 prev->right = prev->left + (title->bottom - title->top);
2387 next->right = title->right - 4;
2388 next->left = next->right - (title->bottom - title->top);
2390 /* titlemonth->left and right change based upon the current month */
2391 /* and are recalculated in refresh as the current month may change */
2392 /* without the control being resized */
2393 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
2394 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2396 /* setup the dimensions of the rectangle we draw the names of the */
2397 /* days of the week in */
2398 weeknumrect->left = 0;
2400 if(infoPtr->dwStyle & MCS_WEEKNUMBERS)
2401 weeknumrect->right = prev->right;
2402 else
2403 weeknumrect->right = weeknumrect->left;
2405 wdays->left = days->left = weeknumrect->right;
2406 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
2407 wdays->top = title->bottom;
2408 wdays->bottom = wdays->top + infoPtr->height_increment;
2410 days->top = weeknumrect->top = wdays->bottom;
2411 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2413 todayrect->left = 0;
2414 todayrect->right = title->right;
2415 todayrect->top = days->bottom;
2416 todayrect->bottom = days->bottom + infoPtr->height_increment;
2418 /* offset all rectangles to center in client area */
2419 dx = (rcClient.right - title->right) / 2;
2420 dy = (rcClient.bottom - todayrect->bottom) / 2;
2422 /* if calendar doesn't fit client area show it at left/top bounds */
2423 if (title->left + dx < 0) dx = 0;
2424 if (title->top + dy < 0) dy = 0;
2426 if (dx != 0 || dy != 0)
2428 OffsetRect(title, dx, dy);
2429 OffsetRect(prev, dx, dy);
2430 OffsetRect(next, dx, dy);
2431 OffsetRect(titlemonth, dx, dy);
2432 OffsetRect(titleyear, dx, dy);
2433 OffsetRect(wdays, dx, dy);
2434 OffsetRect(weeknumrect, dx, dy);
2435 OffsetRect(days, dx, dy);
2436 OffsetRect(todayrect, dx, dy);
2439 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2440 infoPtr->width_increment,infoPtr->height_increment,
2441 wine_dbgstr_rect(&rcClient),
2442 wine_dbgstr_rect(title),
2443 wine_dbgstr_rect(wdays),
2444 wine_dbgstr_rect(days),
2445 wine_dbgstr_rect(todayrect));
2447 /* restore the originally selected font */
2448 SelectObject(hdc, currentFont);
2450 ReleaseDC(infoPtr->hwndSelf, hdc);
2453 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2455 TRACE("(width=%d, height=%d)\n", Width, Height);
2457 MONTHCAL_UpdateSize(infoPtr);
2458 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2460 return 0;
2463 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2465 return (LRESULT)infoPtr->hFont;
2468 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2470 HFONT hOldFont;
2471 LOGFONTW lf;
2473 if (!hFont) return 0;
2475 hOldFont = infoPtr->hFont;
2476 infoPtr->hFont = hFont;
2478 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2479 lf.lfWeight = FW_BOLD;
2480 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2482 MONTHCAL_UpdateSize(infoPtr);
2484 if (redraw)
2485 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2487 return (LRESULT)hOldFont;
2490 /* update theme after a WM_THEMECHANGED message */
2491 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2493 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2494 CloseThemeData (theme);
2495 OpenThemeData (infoPtr->hwndSelf, themeClass);
2496 return 0;
2499 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2500 const STYLESTRUCT *lpss)
2502 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2503 wStyleType, lpss->styleOld, lpss->styleNew);
2505 if (wStyleType != GWL_STYLE) return 0;
2507 infoPtr->dwStyle = lpss->styleNew;
2509 /* make room for week numbers */
2510 if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2511 MONTHCAL_UpdateSize(infoPtr);
2513 return 0;
2516 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2517 STYLESTRUCT *lpss)
2519 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2520 wStyleType, lpss->styleOld, lpss->styleNew);
2522 /* block MCS_MULTISELECT change */
2523 if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2525 if (lpss->styleOld & MCS_MULTISELECT)
2526 lpss->styleNew |= MCS_MULTISELECT;
2527 else
2528 lpss->styleNew &= ~MCS_MULTISELECT;
2531 return 0;
2534 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2535 static LRESULT
2536 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2538 MONTHCAL_INFO *infoPtr;
2540 /* allocate memory for info structure */
2541 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2542 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2544 if (infoPtr == NULL) {
2545 ERR("could not allocate info memory!\n");
2546 return 0;
2549 infoPtr->hwndSelf = hwnd;
2550 infoPtr->hwndNotify = lpcs->hwndParent;
2551 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2552 infoPtr->calendars = Alloc(sizeof(CALENDAR_INFO));
2553 if (!infoPtr->calendars) goto fail;
2555 infoPtr->cal_num = 1;
2557 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
2559 /* initialize info structure */
2560 /* FIXME: calculate systemtime ->> localtime(subtract timezoneinfo) */
2562 GetLocalTime(&infoPtr->todaysDate);
2563 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2565 infoPtr->maxSelCount = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2566 infoPtr->monthRange = 3;
2568 infoPtr->monthdayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
2569 if (!infoPtr->monthdayState) goto fail;
2571 infoPtr->colors[MCSC_BACKGROUND] = comctl32_color.clrWindow;
2572 infoPtr->colors[MCSC_TEXT] = comctl32_color.clrWindowText;
2573 infoPtr->colors[MCSC_TITLEBK] = comctl32_color.clrActiveCaption;
2574 infoPtr->colors[MCSC_TITLETEXT] = comctl32_color.clrWindow;
2575 infoPtr->colors[MCSC_MONTHBK] = comctl32_color.clrWindow;
2576 infoPtr->colors[MCSC_TRAILINGTEXT] = comctl32_color.clrGrayText;
2578 infoPtr->brushes[BrushBackground] = CreateSolidBrush(infoPtr->colors[MCSC_BACKGROUND]);
2579 infoPtr->brushes[BrushTitle] = CreateSolidBrush(infoPtr->colors[MCSC_TITLEBK]);
2580 infoPtr->brushes[BrushMonth] = CreateSolidBrush(infoPtr->colors[MCSC_MONTHBK]);
2582 infoPtr->pens[PenRed] = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
2583 infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[MCSC_TEXT]);
2585 infoPtr->minSel = infoPtr->todaysDate;
2586 infoPtr->maxSel = infoPtr->todaysDate;
2587 infoPtr->calendars[0].month = infoPtr->todaysDate;
2588 infoPtr->isUnicode = TRUE;
2590 /* call MONTHCAL_UpdateSize to set all of the dimensions */
2591 /* of the control */
2592 MONTHCAL_UpdateSize(infoPtr);
2594 /* today auto update timer, to be freed only on control destruction */
2595 SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2597 OpenThemeData (infoPtr->hwndSelf, themeClass);
2599 return 0;
2601 fail:
2602 Free(infoPtr->monthdayState);
2603 Free(infoPtr->calendars);
2604 Free(infoPtr);
2605 return 0;
2608 static LRESULT
2609 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2611 INT i;
2613 /* free month calendar info data */
2614 Free(infoPtr->monthdayState);
2615 Free(infoPtr->calendars);
2616 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2618 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2620 for (i = 0; i < BrushLast; i++) DeleteObject(infoPtr->brushes[i]);
2621 for (i = 0; i < PenLast; i++) DeleteObject(infoPtr->pens[i]);
2623 Free(infoPtr);
2624 return 0;
2628 * Handler for WM_NOTIFY messages
2630 static LRESULT
2631 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2633 /* notification from year edit updown */
2634 if (hdr->code == UDN_DELTAPOS)
2636 NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2638 if (hdr->hwndFrom == infoPtr->hWndYearUpDown && nmud->iDelta)
2640 /* year value limits are set up explicitly after updown creation */
2641 MONTHCAL_Scroll(infoPtr, 12 * nmud->iDelta);
2642 MONTHCAL_NotifyDayState(infoPtr);
2643 MONTHCAL_NotifySelectionChange(infoPtr);
2646 return 0;
2649 static inline BOOL
2650 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2652 BOOL prev = infoPtr->isUnicode;
2653 infoPtr->isUnicode = isUnicode;
2654 return prev;
2657 static inline BOOL
2658 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2660 return infoPtr->isUnicode;
2663 static LRESULT WINAPI
2664 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2666 MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0);
2668 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2670 if (!infoPtr && (uMsg != WM_CREATE))
2671 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2672 switch(uMsg)
2674 case MCM_GETCURSEL:
2675 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2677 case MCM_SETCURSEL:
2678 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2680 case MCM_GETMAXSELCOUNT:
2681 return MONTHCAL_GetMaxSelCount(infoPtr);
2683 case MCM_SETMAXSELCOUNT:
2684 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2686 case MCM_GETSELRANGE:
2687 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2689 case MCM_SETSELRANGE:
2690 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2692 case MCM_GETMONTHRANGE:
2693 return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2695 case MCM_SETDAYSTATE:
2696 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2698 case MCM_GETMINREQRECT:
2699 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2701 case MCM_GETCOLOR:
2702 return MONTHCAL_GetColor(infoPtr, wParam);
2704 case MCM_SETCOLOR:
2705 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2707 case MCM_GETTODAY:
2708 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2710 case MCM_SETTODAY:
2711 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2713 case MCM_HITTEST:
2714 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2716 case MCM_GETFIRSTDAYOFWEEK:
2717 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2719 case MCM_SETFIRSTDAYOFWEEK:
2720 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2722 case MCM_GETRANGE:
2723 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2725 case MCM_SETRANGE:
2726 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2728 case MCM_GETMONTHDELTA:
2729 return MONTHCAL_GetMonthDelta(infoPtr);
2731 case MCM_SETMONTHDELTA:
2732 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2734 case MCM_GETMAXTODAYWIDTH:
2735 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2737 case MCM_SETUNICODEFORMAT:
2738 return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2740 case MCM_GETUNICODEFORMAT:
2741 return MONTHCAL_GetUnicodeFormat(infoPtr);
2743 case WM_GETDLGCODE:
2744 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2746 case WM_RBUTTONUP:
2747 return MONTHCAL_RButtonUp(infoPtr, lParam);
2749 case WM_LBUTTONDOWN:
2750 return MONTHCAL_LButtonDown(infoPtr, lParam);
2752 case WM_MOUSEMOVE:
2753 return MONTHCAL_MouseMove(infoPtr, lParam);
2755 case WM_LBUTTONUP:
2756 return MONTHCAL_LButtonUp(infoPtr, lParam);
2758 case WM_PAINT:
2759 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2761 case WM_PRINTCLIENT:
2762 return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2764 case WM_ERASEBKGND:
2765 return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2767 case WM_SETFOCUS:
2768 return MONTHCAL_SetFocus(infoPtr);
2770 case WM_SIZE:
2771 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2773 case WM_NOTIFY:
2774 return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2776 case WM_CREATE:
2777 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2779 case WM_SETFONT:
2780 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2782 case WM_GETFONT:
2783 return MONTHCAL_GetFont(infoPtr);
2785 case WM_TIMER:
2786 return MONTHCAL_Timer(infoPtr, wParam);
2788 case WM_THEMECHANGED:
2789 return theme_changed (infoPtr);
2791 case WM_DESTROY:
2792 return MONTHCAL_Destroy(infoPtr);
2794 case WM_SYSCOLORCHANGE:
2795 COMCTL32_RefreshSysColors();
2796 return 0;
2798 case WM_STYLECHANGED:
2799 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2801 case WM_STYLECHANGING:
2802 return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2804 default:
2805 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2806 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2807 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2812 void
2813 MONTHCAL_Register(void)
2815 WNDCLASSW wndClass;
2817 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2818 wndClass.style = CS_GLOBALCLASS;
2819 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2820 wndClass.cbClsExtra = 0;
2821 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2822 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2823 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2824 wndClass.lpszClassName = MONTHCAL_CLASSW;
2826 RegisterClassW(&wndClass);
2830 void
2831 MONTHCAL_Unregister(void)
2833 UnregisterClassW(MONTHCAL_CLASSW, NULL);