1 /* 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>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * This code was audited for completeness against the documented features
27 * of Comctl32.dll version 6.0 on Oct. 20, 2004, by Dimitrie O. Paun.
29 * Unless otherwise noted, we believe this code to be complete, as per
30 * the specification mentioned above.
31 * If you discover missing features, or bugs, please note them below.
34 * -- MCM_[GS]ETUNICODEFORMAT
35 * -- MONTHCAL_GetMonthRange
36 * -- handle resources better (doesn't work now);
37 * -- take care of internationalization.
38 * -- keyboard handling.
39 * -- GetRange: At the moment, we copy ranges anyway, regardless of
40 * infoPtr->rangeValid; an invalid range is simply filled
41 * with zeros in SetRange. Is this the right behavior?
58 #include "wine/unicode.h"
59 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
63 #define MC_SEL_LBUTUP 1 /* Left button released */
64 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
65 #define MC_PREVPRESSED 4 /* Prev month button pressed */
66 #define MC_NEXTPRESSED 8 /* Next month button pressed */
67 #define MC_NEXTMONTHDELAY 350 /* when continuously pressing `next */
68 /* month', wait 500 ms before going */
69 /* to the next month */
70 #define MC_NEXTMONTHTIMER 1 /* Timer ID's */
71 #define MC_PREVMONTHTIMER 2
73 #define countof(arr) (sizeof(arr)/sizeof(arr[0]))
90 int firstDayplace; /* place of the first day of the current month */
91 int delta; /* scroll rate; # of months that the */
92 /* control moves when user clicks a scroll button */
93 int visible; /* # of months visible */
94 int firstDay; /* Start month calendar with firstDay's day */
96 MONTHDAYSTATE *monthdayState;
97 SYSTEMTIME todaysDate;
100 int status; /* See MC_SEL flags */
101 int curSelDay; /* current selected day */
102 int firstSelDay; /* first selected day */
110 RECT title; /* rect for the header above the calendar */
111 RECT titlebtnnext; /* the `next month' button in the header */
112 RECT titlebtnprev; /* the `prev month' button in the header */
113 RECT titlemonth; /* the `month name' txt in the header */
114 RECT titleyear; /* the `year number' txt in the header */
115 RECT wdays; /* week days at top */
116 RECT days; /* calendar area */
117 RECT weeknums; /* week numbers at left side */
118 RECT todayrect; /* `today: xx/xx/xx' text rect */
119 HWND hwndNotify; /* Window to receive the notifications */
120 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
121 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
122 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
125 /* Offsets of days in the week to the weekday of january 1 in a leap year */
126 static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
129 #define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0))
131 /* helper functions */
133 /* returns the number of days in any given month, checking for leap days */
134 /* january is 1, december is 12 */
135 int MONTHCAL_MonthLength(int month, int year)
137 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0};
138 /*Wrap around, this eases handling*/
144 /* if we have a leap year add 1 day to February */
145 /* a leap year is a year either divisible by 400 */
146 /* or divisible by 4 and not by 100 */
147 if(month == 2) { /* February */
148 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
149 (year%4 == 0)) ? 1 : 0);
152 return mdays[month - 1];
157 /* make sure that time is valid */
158 static int MONTHCAL_ValidateTime(SYSTEMTIME time)
160 if(time.wMonth > 12) return FALSE;
161 if(time.wDayOfWeek > 6) return FALSE;
162 if(time.wDay > MONTHCAL_MonthLength(time.wMonth, time.wYear))
164 if(time.wHour > 23) return FALSE;
165 if(time.wMinute > 59) return FALSE;
166 if(time.wSecond > 59) return FALSE;
167 if(time.wMilliseconds > 999) return FALSE;
173 /* Note:Depending on DST, this may be offset by a day.
174 Need to find out if we're on a DST place & adjust the clock accordingly.
175 Above function assumes we have a valid data.
176 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
180 /* returns the day in the week(0 == sunday, 6 == saturday) */
181 /* day(1 == 1st, 2 == 2nd... etc), year is the year value */
182 static int MONTHCAL_CalculateDayOfWeek(DWORD day, DWORD month, DWORD year)
186 return((year + year/4 - year/100 + year/400 +
187 DayOfWeekTable[month-1] + day ) % 7);
190 /* From a given point, calculate the row (weekpos), column(daypos)
191 and day in the calendar. day== 0 mean the last day of tha last month
193 static int MONTHCAL_CalcDayFromPos(MONTHCAL_INFO *infoPtr, int x, int y,
194 int *daypos,int *weekpos)
196 int retval, firstDay;
199 GetClientRect(infoPtr->hwndSelf, &rcClient);
201 /* if the point is outside the x bounds of the window put
202 it at the boundary */
203 if (x > rcClient.right)
207 *daypos = (x - infoPtr->days.left ) / infoPtr->width_increment;
208 *weekpos = (y - infoPtr->days.top ) / infoPtr->height_increment;
210 firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear)+6 - infoPtr->firstDay)%7;
211 retval = *daypos + (7 * *weekpos) - firstDay;
215 /* day is the day of the month, 1 == 1st day of the month */
216 /* sets x and y to be the position of the day */
217 /* x == day, y == week where(0,0) == firstDay, 1st week */
218 static void MONTHCAL_CalcDayXY(MONTHCAL_INFO *infoPtr, int day, int month,
221 int firstDay, prevMonth;
223 firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear) +6 - infoPtr->firstDay)%7;
225 if(month==infoPtr->currentMonth) {
226 *x = (day + firstDay) % 7;
227 *y = (day + firstDay - *x) / 7;
230 if(month < infoPtr->currentMonth) {
231 prevMonth = month - 1;
235 *x = (MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear) - firstDay) % 7;
240 *y = MONTHCAL_MonthLength(month, infoPtr->currentYear - 1) / 7;
241 *x = (day + firstDay + MONTHCAL_MonthLength(month,
242 infoPtr->currentYear)) % 7;
246 /* x: column(day), y: row(week) */
247 static void MONTHCAL_CalcDayRect(MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
249 r->left = infoPtr->days.left + x * infoPtr->width_increment;
250 r->right = r->left + infoPtr->width_increment;
251 r->top = infoPtr->days.top + y * infoPtr->height_increment;
252 r->bottom = r->top + infoPtr->textHeight;
256 /* sets the RECT struct r to the rectangle around the day and month */
257 /* day is the day value of the month(1 == 1st), month is the month */
258 /* value(january == 1, december == 12) */
259 static inline void MONTHCAL_CalcPosFromDay(MONTHCAL_INFO *infoPtr,
260 int day, int month, RECT *r)
264 MONTHCAL_CalcDayXY(infoPtr, day, month, &x, &y);
265 MONTHCAL_CalcDayRect(infoPtr, r, x, y);
269 /* day is the day in the month(1 == 1st of the month) */
270 /* month is the month value(1 == january, 12 == december) */
271 static void MONTHCAL_CircleDay(MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month)
273 HPEN hRedPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
274 HPEN hOldPen2 = SelectObject(hdc, hRedPen);
280 MONTHCAL_CalcPosFromDay(infoPtr, day, month, &day_rect);
287 points[1].x = x + 0.8 * infoPtr->width_increment;
289 points[2].x = x + 0.9 * infoPtr->width_increment;
291 points[3].x = x + infoPtr->width_increment;
292 points[3].y = y + 0.5 * infoPtr->height_increment;
294 points[4].x = x + infoPtr->width_increment;
295 points[4].y = y + 0.9 * infoPtr->height_increment;
296 points[5].x = x + 0.6 * infoPtr->width_increment;
297 points[5].y = y + 0.9 * infoPtr->height_increment;
298 points[6].x = x + 0.5 * infoPtr->width_increment;
299 points[6].y = y + 0.9 * infoPtr->height_increment; /* bring the bottom up just
300 a hair to fit inside the day rectangle */
302 points[7].x = x + 0.2 * infoPtr->width_increment;
303 points[7].y = y + 0.8 * infoPtr->height_increment;
304 points[8].x = x + 0.1 * infoPtr->width_increment;
305 points[8].y = y + 0.8 * infoPtr->height_increment;
307 points[9].y = y + 0.5 * infoPtr->height_increment;
309 points[10].x = x + 0.1 * infoPtr->width_increment;
310 points[10].y = y + 0.2 * infoPtr->height_increment;
311 points[11].x = x + 0.2 * infoPtr->width_increment;
312 points[11].y = y + 0.3 * infoPtr->height_increment;
313 points[12].x = x + 0.4 * infoPtr->width_increment;
314 points[12].y = y + 0.2 * infoPtr->height_increment;
316 PolyBezier(hdc, points, 13);
317 DeleteObject(hRedPen);
318 SelectObject(hdc, hOldPen2);
322 static void MONTHCAL_DrawDay(MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month,
323 int x, int y, int bold)
325 static const WCHAR fmtW[] = { '%','d',0 };
328 static int haveBoldFont, haveSelectedDay = FALSE;
333 wsprintfW(buf, fmtW, day);
335 /* No need to check styles: when selection is not valid, it is set to zero.
336 * 1<day<31, so evertyhing's OK.
339 MONTHCAL_CalcDayRect(infoPtr, &r, x, y);
341 if((day>=infoPtr->minSel.wDay) && (day<=infoPtr->maxSel.wDay)
342 && (month==infoPtr->currentMonth)) {
346 TRACE("%d %d %d\n",day, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
347 TRACE("%ld %ld %ld %ld\n", r.left, r.top, r.right, r.bottom);
348 oldCol = SetTextColor(hdc, infoPtr->monthbk);
349 oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
350 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
351 hrgn = CreateEllipticRgn(r.left, r.top, r.right, r.bottom);
352 FillRgn(hdc, hrgn, hbr);
354 /* FIXME: this may need to be changed now b/c of the other
355 drawing changes 11/3/99 CMM */
356 r2.left = r.left - 0.25 * infoPtr->textWidth;
358 r2.right = r.left + 0.5 * infoPtr->textWidth;
359 r2.bottom = r.bottom;
360 if(haveSelectedDay) FillRect(hdc, &r2, hbr);
361 haveSelectedDay = TRUE;
363 haveSelectedDay = FALSE;
366 /* need to add some code for multiple selections */
368 if((bold) &&(!haveBoldFont)) {
369 SelectObject(hdc, infoPtr->hBoldFont);
372 if((!bold) &&(haveBoldFont)) {
373 SelectObject(hdc, infoPtr->hFont);
374 haveBoldFont = FALSE;
377 if(haveSelectedDay) {
378 SetTextColor(hdc, oldCol);
379 SetBkColor(hdc, oldBk);
382 SetBkMode(hdc,TRANSPARENT);
383 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
385 /* draw a rectangle around the currently selected days text */
386 if((day==infoPtr->curSelDay) && (month==infoPtr->currentMonth))
387 DrawFocusRect(hdc, &r);
391 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, PAINTSTRUCT* ps)
393 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
394 static const WCHAR fmt1W[] = { '%','s',' ','%','l','d',0 };
395 static const WCHAR fmt2W[] = { '%','s',' ','%','s',0 };
396 static const WCHAR fmt3W[] = { '%','d',0 };
397 RECT *title=&infoPtr->title;
398 RECT *prev=&infoPtr->titlebtnprev;
399 RECT *next=&infoPtr->titlebtnnext;
400 RECT *titlemonth=&infoPtr->titlemonth;
401 RECT *titleyear=&infoPtr->titleyear;
405 int i, j, m, mask, day, firstDay, weeknum, weeknum1,prevMonth;
406 int textHeight = infoPtr->textHeight, textWidth = infoPtr->textWidth;
413 COLORREF oldTextColor, oldBkColor;
414 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
416 RECT rcDay; /* used in MONTHCAL_CalcDayRect() */
417 SYSTEMTIME localtime;
420 oldTextColor = SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
422 /* fill background */
423 hbr = CreateSolidBrush (infoPtr->bk);
424 FillRect(hdc, &ps->rcPaint, hbr);
428 if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
430 hbr = CreateSolidBrush(infoPtr->titlebk);
431 FillRect(hdc, title, hbr);
435 /* if the previous button is pressed draw it depressed */
436 if(IntersectRect(&rcTemp, &(ps->rcPaint), prev))
438 if((infoPtr->status & MC_PREVPRESSED))
439 DrawFrameControl(hdc, prev, DFC_SCROLL,
440 DFCS_SCROLLLEFT | DFCS_PUSHED |
441 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
442 else /* if the previous button is pressed draw it depressed */
443 DrawFrameControl(hdc, prev, DFC_SCROLL,
444 DFCS_SCROLLLEFT |(dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
447 /* if next button is depressed draw it depressed */
448 if(IntersectRect(&rcTemp, &(ps->rcPaint), next))
450 if((infoPtr->status & MC_NEXTPRESSED))
451 DrawFrameControl(hdc, next, DFC_SCROLL,
452 DFCS_SCROLLRIGHT | DFCS_PUSHED |
453 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
454 else /* if the next button is pressed draw it depressed */
455 DrawFrameControl(hdc, next, DFC_SCROLL,
456 DFCS_SCROLLRIGHT |(dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
459 oldBkColor = SetBkColor(hdc, infoPtr->titlebk);
460 SetTextColor(hdc, infoPtr->titletxt);
461 currentFont = SelectObject(hdc, infoPtr->hBoldFont);
463 GetLocaleInfoW( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+infoPtr->currentMonth -1,
465 wsprintfW(buf, fmt1W, buf1, infoPtr->currentYear);
467 if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
469 DrawTextW(hdc, buf, strlenW(buf), title,
470 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
473 /* titlemonth left/right contained rect for whole titletxt('June 1999')
474 * MCM_HitTestInfo wants month & year rects, so prepare these now.
475 *(no, we can't draw them separately; the whole text is centered)
477 GetTextExtentPoint32W(hdc, buf, strlenW(buf), &size);
478 titlemonth->left = title->right / 2 + title->left / 2 - size.cx / 2;
479 titleyear->right = title->right / 2 + title->left / 2 + size.cx / 2;
480 GetTextExtentPoint32W(hdc, buf1, strlenW(buf1), &size);
481 titlemonth->right = titlemonth->left + size.cx;
482 titleyear->left = titlemonth->right;
484 /* draw month area */
485 rcTemp.top=infoPtr->wdays.top;
486 rcTemp.left=infoPtr->wdays.left;
487 rcTemp.bottom=infoPtr->todayrect.bottom;
488 rcTemp.right =infoPtr->todayrect.right;
489 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcTemp))
491 hbr = CreateSolidBrush(infoPtr->monthbk);
492 FillRect(hdc, &rcTemp, hbr);
496 /* draw line under day abbreviatons */
498 MoveToEx(hdc, infoPtr->days.left + 3, title->bottom + textHeight + 1, NULL);
499 LineTo(hdc, infoPtr->days.right - 3, title->bottom + textHeight + 1);
501 prevMonth = infoPtr->currentMonth - 1;
502 if(prevMonth == 0) /* if currentMonth is january(1) prevMonth is */
503 prevMonth = 12; /* december(12) of the previous year */
505 infoPtr->wdays.left = infoPtr->days.left = infoPtr->weeknums.right;
506 /* draw day abbreviations */
508 SelectObject(hdc, infoPtr->hFont);
509 SetBkColor(hdc, infoPtr->monthbk);
510 SetTextColor(hdc, infoPtr->trailingtxt);
512 /* copy this rect so we can change the values without changing */
513 /* the original version */
514 days->left = infoPtr->wdays.left;
515 days->right = days->left + infoPtr->width_increment;
516 days->top = infoPtr->wdays.top;
517 days->bottom = infoPtr->wdays.bottom;
519 i = infoPtr->firstDay;
522 GetLocaleInfoW( LOCALE_USER_DEFAULT,LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
523 DrawTextW(hdc, buf, strlenW(buf), days, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
524 days->left+=infoPtr->width_increment;
525 days->right+=infoPtr->width_increment;
528 /* draw day numbers; first, the previous month */
530 firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear);
532 day = MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear) +
533 (infoPtr->firstDay + 7 - firstDay)%7 + 1;
534 if (day > MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear))
536 startofprescal = day;
541 while(day <= MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear)) {
542 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
543 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
545 MONTHCAL_DrawDay(infoPtr, hdc, day, prevMonth, i, 0,
546 infoPtr->monthdayState[m] & mask);
554 /* draw `current' month */
556 day = 1; /* start at the beginning of the current month */
558 infoPtr->firstDayplace = i;
559 SetTextColor(hdc, infoPtr->txt);
563 /* draw the first week of the current month */
565 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
566 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
569 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth, i, 0,
570 infoPtr->monthdayState[m] & mask);
572 if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
573 (day==infoPtr->todaysDate.wDay) &&
574 (infoPtr->currentYear == infoPtr->todaysDate.wYear)) {
575 if(!(dwStyle & MCS_NOTODAYCIRCLE))
576 MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->currentMonth);
585 j = 1; /* move to the 2nd week of the current month */
586 i = 0; /* move back to sunday */
587 while(day <= MONTHCAL_MonthLength(infoPtr->currentMonth, infoPtr->currentYear)) {
588 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
589 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
591 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth, i, j,
592 infoPtr->monthdayState[m] & mask);
594 if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
595 (day==infoPtr->todaysDate.wDay) &&
596 (infoPtr->currentYear == infoPtr->todaysDate.wYear))
597 if(!(dwStyle & MCS_NOTODAYCIRCLE))
598 MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->currentMonth);
603 if(i>6) { /* past saturday, goto the next weeks sunday */
609 /* draw `next' month */
611 day = 1; /* start at the first day of the next month */
615 SetTextColor(hdc, infoPtr->trailingtxt);
616 while((i<7) &&(j<6)) {
617 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
618 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
620 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth + 1, i, j,
621 infoPtr->monthdayState[m] & mask);
627 if(i==7) { /* past saturday, go to next week's sunday */
632 SetTextColor(hdc, infoPtr->txt);
635 /* draw `today' date if style allows it, and draw a circle before today's
636 * date if necessary */
638 if(!(dwStyle & MCS_NOTODAY)) {
640 if(!(dwStyle & MCS_NOTODAYCIRCLE)) {
641 /*day is the number of days from nextmonth we put on the calendar */
642 MONTHCAL_CircleDay(infoPtr, hdc,
643 day+MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear),
644 infoPtr->currentMonth);
647 if (!LoadStringW(COMCTL32_hModule,IDM_TODAY,buf1,countof(buf1)))
649 WARN("Can't load resource\n");
650 strcpyW(buf1, todayW);
652 MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
653 MONTHCAL_CopyTime(&infoPtr->todaysDate,&localtime);
654 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&localtime,NULL,buf2,countof(buf2));
655 wsprintfW(buf, fmt2W, buf1, buf2);
656 SelectObject(hdc, infoPtr->hBoldFont);
658 DrawTextW(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
659 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rtoday))
661 DrawTextW(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
663 SelectObject(hdc, infoPtr->hFont);
666 /*eventually draw week numbers*/
667 if(dwStyle & MCS_WEEKNUMBERS) {
668 /* display weeknumbers*/
671 /* Rules what week to call the first week of a new year:
672 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
673 The week containing Jan 1 is the first week of year
674 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
675 First week of year must contain 4 days of the new year
676 LOCALE_IFIRSTWEEKOFYEAR == 1 (what contries?)
677 The first week of the year must contain only days of the new year
679 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
680 weeknum = atoiW(buf);
691 if (infoPtr->currentMonth < 2)
693 /* calculate all those exceptions for january */
694 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear);
695 if ((infoPtr->firstDay +7 - weeknum1)%7 > mindays)
701 weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->currentYear-1);
702 weeknum +=startofprescal+ 7;
704 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear-1);
705 if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
712 for(i=0; i<prevMonth-1; i++)
713 weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->currentYear);
714 weeknum +=startofprescal+ 7;
716 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear);
717 if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
720 days->left = infoPtr->weeknums.left;
721 days->right = infoPtr->weeknums.right;
722 days->top = infoPtr->weeknums.top;
723 days->bottom = days->top +infoPtr->height_increment;
725 if((i==0)&&(weeknum>50))
727 wsprintfW(buf, fmt3W, weeknum);
730 else if((i==5)&&(weeknum>47))
732 wsprintfW(buf, fmt3W, 1);
735 wsprintfW(buf, fmt3W, weeknum + i);
736 DrawTextW(hdc, buf, -1, days, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
737 days->top+=infoPtr->height_increment;
738 days->bottom+=infoPtr->height_increment;
741 MoveToEx(hdc, infoPtr->weeknums.right, infoPtr->weeknums.top + 3 , NULL);
742 LineTo(hdc, infoPtr->weeknums.right, infoPtr->weeknums.bottom );
745 /* currentFont was font at entering Refresh */
747 SetBkColor(hdc, oldBkColor);
748 SelectObject(hdc, currentFont);
749 SetTextColor(hdc, oldTextColor);
754 MONTHCAL_GetMinReqRect(MONTHCAL_INFO *infoPtr, LPARAM lParam)
756 LPRECT lpRect = (LPRECT) lParam;
758 TRACE("rect %p\n", lpRect);
760 /* validate parameters */
762 if((infoPtr==NULL) ||(lpRect == NULL) ) return FALSE;
764 lpRect->left = infoPtr->title.left;
765 lpRect->top = infoPtr->title.top;
766 lpRect->right = infoPtr->title.right;
767 lpRect->bottom = infoPtr->todayrect.bottom;
768 AdjustWindowRect(lpRect, GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE), FALSE);
770 TRACE("%s\n", wine_dbgstr_rect(lpRect));
777 MONTHCAL_GetColor(MONTHCAL_INFO *infoPtr, WPARAM wParam)
781 switch((int)wParam) {
782 case MCSC_BACKGROUND:
787 return infoPtr->titlebk;
789 return infoPtr->titletxt;
791 return infoPtr->monthbk;
792 case MCSC_TRAILINGTEXT:
793 return infoPtr->trailingtxt;
801 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
805 TRACE("%d: color %08lx\n", wParam, lParam);
807 switch((int)wParam) {
808 case MCSC_BACKGROUND:
810 infoPtr->bk = (COLORREF)lParam;
814 infoPtr->txt = (COLORREF)lParam;
817 prev = infoPtr->titlebk;
818 infoPtr->titlebk = (COLORREF)lParam;
821 prev=infoPtr->titletxt;
822 infoPtr->titletxt = (COLORREF)lParam;
825 prev = infoPtr->monthbk;
826 infoPtr->monthbk = (COLORREF)lParam;
828 case MCSC_TRAILINGTEXT:
829 prev = infoPtr->trailingtxt;
830 infoPtr->trailingtxt = (COLORREF)lParam;
834 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
840 MONTHCAL_GetMonthDelta(MONTHCAL_INFO *infoPtr)
845 return infoPtr->delta;
847 return infoPtr->visible;
852 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, WPARAM wParam)
854 int prev = infoPtr->delta;
856 TRACE("delta %d\n", wParam);
858 infoPtr->delta = (int)wParam;
864 MONTHCAL_GetFirstDayOfWeek(MONTHCAL_INFO *infoPtr)
866 return infoPtr->firstDay;
870 /* sets the first day of the week that will appear in the control */
871 /* 0 == Sunday, 6 == Saturday */
872 /* FIXME: this needs to be implemented properly in MONTHCAL_Refresh() */
873 /* FIXME: we need more error checking here */
875 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, LPARAM lParam)
877 int prev = infoPtr->firstDay;
880 TRACE("day %ld\n", lParam);
882 if((lParam >= 0) && (lParam < 7)) {
883 infoPtr->firstDay = (int)lParam;
887 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
888 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
889 infoPtr->firstDay = (atoiW(buf)+1)%7;
896 MONTHCAL_GetMonthRange(MONTHCAL_INFO *infoPtr)
900 return infoPtr->monthRange;
905 MONTHCAL_GetMaxTodayWidth(MONTHCAL_INFO *infoPtr)
907 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
911 /* FIXME: are validated times taken from current date/time or simply
913 * FIXME: check whether MCM_GETMONTHRANGE shows correct result after
914 * adjusting range with MCM_SETRANGE
918 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
920 SYSTEMTIME *lprgSysTimeArray=(SYSTEMTIME *)lParam;
923 TRACE("%x %lx\n", wParam, lParam);
925 if(wParam & GDTR_MAX) {
926 if(MONTHCAL_ValidateTime(lprgSysTimeArray[1])){
927 MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxDate);
928 infoPtr->rangeValid|=GDTR_MAX;
930 GetSystemTime(&infoPtr->todaysDate);
931 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
934 if(wParam & GDTR_MIN) {
935 if(MONTHCAL_ValidateTime(lprgSysTimeArray[0])) {
936 MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minDate);
937 infoPtr->rangeValid|=GDTR_MIN;
939 GetSystemTime(&infoPtr->todaysDate);
940 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->minDate);
944 prev = infoPtr->monthRange;
945 infoPtr->monthRange = infoPtr->maxDate.wMonth - infoPtr->minDate.wMonth;
947 if(infoPtr->monthRange!=prev) {
948 infoPtr->monthdayState = ReAlloc(infoPtr->monthdayState,
949 infoPtr->monthRange * sizeof(MONTHDAYSTATE));
957 MONTHCAL_GetRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
959 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
960 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *)lParam;
962 /* validate parameters */
964 if((infoPtr==NULL) || (lprgSysTimeArray==NULL)) return FALSE;
966 MONTHCAL_CopyTime(&infoPtr->maxDate, &lprgSysTimeArray[1]);
967 MONTHCAL_CopyTime(&infoPtr->minDate, &lprgSysTimeArray[0]);
969 return infoPtr->rangeValid;
974 MONTHCAL_SetDayState(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
977 int i, iMonths = (int)wParam;
978 MONTHDAYSTATE *dayStates = (LPMONTHDAYSTATE)lParam;
980 TRACE("%x %lx\n", wParam, lParam);
981 if(iMonths!=infoPtr->monthRange) return 0;
983 for(i=0; i<iMonths; i++)
984 infoPtr->monthdayState[i] = dayStates[i];
989 MONTHCAL_GetCurSel(MONTHCAL_INFO *infoPtr, LPARAM lParam)
991 SYSTEMTIME *lpSel = (SYSTEMTIME *) lParam;
993 TRACE("%lx\n", lParam);
994 if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
995 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
997 MONTHCAL_CopyTime(&infoPtr->minSel, lpSel);
998 TRACE("%d/%d/%d\n", lpSel->wYear, lpSel->wMonth, lpSel->wDay);
1002 /* FIXME: if the specified date is not visible, make it visible */
1003 /* FIXME: redraw? */
1005 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1007 SYSTEMTIME *lpSel = (SYSTEMTIME *)lParam;
1009 TRACE("%lx\n", lParam);
1010 if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
1011 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
1013 infoPtr->currentMonth=lpSel->wMonth;
1014 infoPtr->currentYear=lpSel->wYear;
1016 MONTHCAL_CopyTime(lpSel, &infoPtr->minSel);
1017 MONTHCAL_CopyTime(lpSel, &infoPtr->maxSel);
1019 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1026 MONTHCAL_GetMaxSelCount(MONTHCAL_INFO *infoPtr)
1028 return infoPtr->maxSelCount;
1033 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1035 TRACE("%x\n", wParam);
1037 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) {
1038 infoPtr->maxSelCount = wParam;
1046 MONTHCAL_GetSelRange(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1048 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1050 TRACE("%lx\n", lParam);
1052 /* validate parameters */
1054 if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1056 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)
1058 MONTHCAL_CopyTime(&infoPtr->maxSel, &lprgSysTimeArray[1]);
1059 MONTHCAL_CopyTime(&infoPtr->minSel, &lprgSysTimeArray[0]);
1060 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1069 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1071 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1073 TRACE("%lx\n", lParam);
1075 /* validate parameters */
1077 if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1079 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)
1081 MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxSel);
1082 MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minSel);
1083 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1092 MONTHCAL_GetToday(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1094 SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1096 TRACE("%lx\n", lParam);
1098 /* validate parameters */
1100 if((infoPtr==NULL) || (lpToday==NULL)) return FALSE;
1101 MONTHCAL_CopyTime(&infoPtr->todaysDate, lpToday);
1107 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1109 SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1111 TRACE("%lx\n", lParam);
1113 /* validate parameters */
1115 if((infoPtr==NULL) ||(lpToday==NULL)) return FALSE;
1116 MONTHCAL_CopyTime(lpToday, &infoPtr->todaysDate);
1117 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1123 MONTHCAL_HitTest(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1125 PMCHITTESTINFO lpht = (PMCHITTESTINFO)lParam;
1133 retval = MCHT_NOWHERE;
1135 ZeroMemory(&lpht->st, sizeof(lpht->st));
1137 /* Comment in for debugging...
1138 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,
1139 infoPtr->wdays.left, infoPtr->wdays.right,
1140 infoPtr->wdays.top, infoPtr->wdays.bottom,
1141 infoPtr->days.left, infoPtr->days.right,
1142 infoPtr->days.top, infoPtr->days.bottom,
1143 infoPtr->todayrect.left, infoPtr->todayrect.right,
1144 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1145 infoPtr->weeknums.left, infoPtr->weeknums.right,
1146 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1149 /* are we in the header? */
1151 if(PtInRect(&infoPtr->title, lpht->pt)) {
1152 if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1153 retval = MCHT_TITLEBTNPREV;
1156 if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1157 retval = MCHT_TITLEBTNNEXT;
1160 if(PtInRect(&infoPtr->titlemonth, lpht->pt)) {
1161 retval = MCHT_TITLEMONTH;
1164 if(PtInRect(&infoPtr->titleyear, lpht->pt)) {
1165 retval = MCHT_TITLEYEAR;
1169 retval = MCHT_TITLE;
1173 day = MONTHCAL_CalcDayFromPos(infoPtr,x,y,&wday,&wnum);
1174 if(PtInRect(&infoPtr->wdays, lpht->pt)) {
1175 retval = MCHT_CALENDARDAY;
1176 lpht->st.wYear = infoPtr->currentYear;
1177 lpht->st.wMonth = (day < 1)? infoPtr->currentMonth -1 : infoPtr->currentMonth;
1178 lpht->st.wDay = (day < 1)?
1179 MONTHCAL_MonthLength(infoPtr->currentMonth-1,infoPtr->currentYear) -day : day;
1182 if(PtInRect(&infoPtr->weeknums, lpht->pt)) {
1183 retval = MCHT_CALENDARWEEKNUM;
1184 lpht->st.wYear = infoPtr->currentYear;
1185 lpht->st.wMonth = (day < 1) ? infoPtr->currentMonth -1 :
1186 (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear)) ?
1187 infoPtr->currentMonth +1 :infoPtr->currentMonth;
1188 lpht->st.wDay = (day < 1 ) ?
1189 MONTHCAL_MonthLength(infoPtr->currentMonth-1,infoPtr->currentYear) -day :
1190 (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear)) ?
1191 day - MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear) : day;
1194 if(PtInRect(&infoPtr->days, lpht->pt))
1196 lpht->st.wYear = infoPtr->currentYear;
1199 retval = MCHT_CALENDARDATEPREV;
1200 lpht->st.wMonth = infoPtr->currentMonth - 1;
1201 if (lpht->st.wMonth <1)
1203 lpht->st.wMonth = 12;
1206 lpht->st.wDay = MONTHCAL_MonthLength(lpht->st.wMonth,lpht->st.wYear) -day;
1208 else if (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear))
1210 retval = MCHT_CALENDARDATENEXT;
1211 lpht->st.wMonth = infoPtr->currentMonth + 1;
1212 if (lpht->st.wMonth <12)
1214 lpht->st.wMonth = 1;
1217 lpht->st.wDay = day - MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear) ;
1220 retval = MCHT_CALENDARDATE;
1221 lpht->st.wMonth = infoPtr->currentMonth;
1222 lpht->st.wDay = day;
1223 lpht->st.wDayOfWeek = MONTHCAL_CalculateDayOfWeek(day,lpht->st.wMonth,lpht->st.wYear);
1227 if(PtInRect(&infoPtr->todayrect, lpht->pt)) {
1228 retval = MCHT_TODAYLINK;
1233 /* Hit nothing special? What's left must be background :-) */
1235 retval = MCHT_CALENDARBK;
1237 lpht->uHit = retval;
1242 static void MONTHCAL_GoToNextMonth(MONTHCAL_INFO *infoPtr)
1244 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1246 TRACE("MONTHCAL_GoToNextMonth\n");
1248 infoPtr->currentMonth++;
1249 if(infoPtr->currentMonth > 12) {
1250 infoPtr->currentYear++;
1251 infoPtr->currentMonth = 1;
1254 if(dwStyle & MCS_DAYSTATE) {
1258 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1259 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1260 nmds.nmhdr.code = MCN_GETDAYSTATE;
1261 nmds.cDayState = infoPtr->monthRange;
1262 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1264 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
1265 (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
1266 for(i=0; i<infoPtr->monthRange; i++)
1267 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1272 static void MONTHCAL_GoToPrevMonth(MONTHCAL_INFO *infoPtr)
1274 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1278 infoPtr->currentMonth--;
1279 if(infoPtr->currentMonth < 1) {
1280 infoPtr->currentYear--;
1281 infoPtr->currentMonth = 12;
1284 if(dwStyle & MCS_DAYSTATE) {
1288 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1289 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1290 nmds.nmhdr.code = MCN_GETDAYSTATE;
1291 nmds.cDayState = infoPtr->monthRange;
1292 nmds.prgDayState = Alloc
1293 (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1295 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
1296 (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
1297 for(i=0; i<infoPtr->monthRange; i++)
1298 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1303 MONTHCAL_RButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1305 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1310 hMenu = CreatePopupMenu();
1311 if (!LoadStringW(COMCTL32_hModule,IDM_GOTODAY,buf,countof(buf)))
1313 WARN("Can't load resource\n");
1314 strcpyW(buf, todayW);
1316 AppendMenuW(hMenu, MF_STRING|MF_ENABLED,1, buf);
1317 menupoint.x=(INT)LOWORD(lParam);
1318 menupoint.y=(INT)HIWORD(lParam);
1319 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1320 if( TrackPopupMenu(hMenu,TPM_RIGHTBUTTON| TPM_NONOTIFY|TPM_RETURNCMD,
1321 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1323 infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
1324 infoPtr->currentYear=infoPtr->todaysDate.wYear;
1325 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1331 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1333 static const WCHAR EditW[] = { 'E','D','I','T',0 };
1337 RECT rcDay; /* used in determining area to invalidate */
1342 TRACE("%lx\n", lParam);
1344 if (infoPtr->hWndYearUpDown)
1346 infoPtr->currentYear=SendMessageW( infoPtr->hWndYearUpDown, UDM_SETPOS, (WPARAM) 0,(LPARAM)0);
1347 if(!DestroyWindow(infoPtr->hWndYearUpDown))
1349 FIXME("Can't destroy Updown Control\n");
1352 infoPtr->hWndYearUpDown=0;
1353 if(!DestroyWindow(infoPtr->hWndYearEdit))
1355 FIXME("Can't destroy Updown Control\n");
1358 infoPtr->hWndYearEdit=0;
1359 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1362 ht.pt.x = (INT)LOWORD(lParam);
1363 ht.pt.y = (INT)HIWORD(lParam);
1364 hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1366 /* FIXME: these flags should be checked by */
1367 /*((hit & MCHT_XXX) == MCHT_XXX) b/c some of the flags are */
1369 if(hit ==MCHT_TITLEBTNNEXT) {
1370 MONTHCAL_GoToNextMonth(infoPtr);
1371 infoPtr->status = MC_NEXTPRESSED;
1372 SetTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1373 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1376 if(hit == MCHT_TITLEBTNPREV){
1377 MONTHCAL_GoToPrevMonth(infoPtr);
1378 infoPtr->status = MC_PREVPRESSED;
1379 SetTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1380 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1384 if(hit == MCHT_TITLEMONTH) {
1385 hMenu = CreatePopupMenu();
1389 GetLocaleInfoW(LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+i, buf,countof(buf));
1390 AppendMenuW(hMenu, MF_STRING|MF_ENABLED,i+1, buf);
1392 menupoint.x=infoPtr->titlemonth.right;
1393 menupoint.y=infoPtr->titlemonth.bottom;
1394 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1395 i= TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
1396 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
1397 if ((i>0) && (i<13))
1399 infoPtr->currentMonth=i;
1400 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1403 if(hit == MCHT_TITLEYEAR) {
1404 infoPtr->hWndYearEdit=CreateWindowExW(0,
1407 WS_VISIBLE | WS_CHILD |UDS_SETBUDDYINT,
1408 infoPtr->titleyear.left+3,infoPtr->titlebtnnext.top,
1409 infoPtr->titleyear.right-infoPtr->titleyear.left+4,
1410 infoPtr->textHeight,
1415 SendMessageW( infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM) infoPtr->hBoldFont, (LPARAM)TRUE);
1416 infoPtr->hWndYearUpDown=CreateWindowExW(0,
1419 WS_VISIBLE | WS_CHILD |UDS_SETBUDDYINT|UDS_NOTHOUSANDS|UDS_ARROWKEYS,
1420 infoPtr->titleyear.right+7,infoPtr->titlebtnnext.top,
1422 infoPtr->textHeight,
1427 SendMessageW( infoPtr->hWndYearUpDown, UDM_SETRANGE, (WPARAM) 0, MAKELONG (9999, 1753));
1428 SendMessageW( infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM) infoPtr->hWndYearEdit, (LPARAM)0 );
1429 SendMessageW( infoPtr->hWndYearUpDown, UDM_SETPOS, (WPARAM) 0,(LPARAM)infoPtr->currentYear );
1433 if(hit == MCHT_TODAYLINK) {
1434 infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
1435 infoPtr->currentYear=infoPtr->todaysDate.wYear;
1436 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1439 if(hit == MCHT_CALENDARDATE) {
1440 SYSTEMTIME selArray[2];
1443 MONTHCAL_CopyTime(&ht.st, &selArray[0]);
1444 MONTHCAL_CopyTime(&ht.st, &selArray[1]);
1445 MONTHCAL_SetSelRange(infoPtr, (LPARAM)&selArray);
1446 MONTHCAL_SetCurSel(infoPtr, (LPARAM)&selArray);
1447 TRACE("MCHT_CALENDARDATE\n");
1448 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1449 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1450 nmsc.nmhdr.code = MCN_SELCHANGE;
1451 MONTHCAL_CopyTime(&infoPtr->minSel,&nmsc.stSelStart);
1452 MONTHCAL_CopyTime(&infoPtr->maxSel,&nmsc.stSelEnd);
1454 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
1455 (WPARAM)nmsc.nmhdr.idFrom,(LPARAM)&nmsc);
1458 /* redraw both old and new days if the selected day changed */
1459 if(infoPtr->curSelDay != ht.st.wDay) {
1460 MONTHCAL_CalcPosFromDay(infoPtr, ht.st.wDay, ht.st.wMonth, &rcDay);
1461 InvalidateRect(infoPtr->hwndSelf, &rcDay, TRUE);
1463 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->curSelDay, infoPtr->currentMonth, &rcDay);
1464 InvalidateRect(infoPtr->hwndSelf, &rcDay, TRUE);
1467 infoPtr->firstSelDay = ht.st.wDay;
1468 infoPtr->curSelDay = ht.st.wDay;
1469 infoPtr->status = MC_SEL_LBUTDOWN;
1478 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1482 BOOL redraw = FALSE;
1488 if(infoPtr->status & MC_NEXTPRESSED) {
1489 KillTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER);
1490 infoPtr->status &= ~MC_NEXTPRESSED;
1493 if(infoPtr->status & MC_PREVPRESSED) {
1494 KillTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER);
1495 infoPtr->status &= ~MC_PREVPRESSED;
1499 ht.pt.x = (INT)LOWORD(lParam);
1500 ht.pt.y = (INT)HIWORD(lParam);
1501 hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1503 infoPtr->status = MC_SEL_LBUTUP;
1505 if(hit ==MCHT_CALENDARDATENEXT) {
1506 MONTHCAL_GoToNextMonth(infoPtr);
1507 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1510 if(hit == MCHT_CALENDARDATEPREV){
1511 MONTHCAL_GoToPrevMonth(infoPtr);
1512 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1515 nmhdr.hwndFrom = infoPtr->hwndSelf;
1516 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1517 nmhdr.code = NM_RELEASEDCAPTURE;
1518 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
1520 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
1521 /* redraw if necessary */
1523 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1524 /* only send MCN_SELECT if currently displayed month's day was selected */
1525 if(hit == MCHT_CALENDARDATE) {
1526 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1527 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1528 nmsc.nmhdr.code = MCN_SELECT;
1529 MONTHCAL_CopyTime(&infoPtr->minSel, &nmsc.stSelStart);
1530 MONTHCAL_CopyTime(&infoPtr->maxSel, &nmsc.stSelEnd);
1532 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, (WPARAM)nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1540 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1542 BOOL redraw = FALSE;
1544 TRACE("%d\n", wParam);
1547 case MC_NEXTMONTHTIMER:
1549 MONTHCAL_GoToNextMonth(infoPtr);
1551 case MC_PREVMONTHTIMER:
1553 MONTHCAL_GoToPrevMonth(infoPtr);
1556 ERR("got unknown timer\n");
1560 /* redraw only if necessary */
1562 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1569 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1572 int oldselday, selday, hit;
1575 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
1577 ht.pt.x = LOWORD(lParam);
1578 ht.pt.y = HIWORD(lParam);
1580 hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1582 /* not on the calendar date numbers? bail out */
1583 TRACE("hit:%x\n",hit);
1584 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE) return 0;
1586 selday = ht.st.wDay;
1587 oldselday = infoPtr->curSelDay;
1588 infoPtr->curSelDay = selday;
1589 MONTHCAL_CalcPosFromDay(infoPtr, selday, ht.st. wMonth, &r);
1591 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) {
1592 SYSTEMTIME selArray[2];
1595 MONTHCAL_GetSelRange(infoPtr, (LPARAM)&selArray);
1597 if(infoPtr->firstSelDay==selArray[0].wDay) i=1;
1598 TRACE("oldRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1599 if(infoPtr->firstSelDay==selArray[1].wDay) {
1600 /* 1st time we get here: selArray[0]=selArray[1]) */
1601 /* if we're still at the first selected date, return */
1602 if(infoPtr->firstSelDay==selday) goto done;
1603 if(selday<infoPtr->firstSelDay) i = 0;
1606 if(abs(infoPtr->firstSelDay - selday) >= infoPtr->maxSelCount) {
1607 if(selday>infoPtr->firstSelDay)
1608 selday = infoPtr->firstSelDay + infoPtr->maxSelCount;
1610 selday = infoPtr->firstSelDay - infoPtr->maxSelCount;
1613 if(selArray[i].wDay!=selday) {
1614 TRACE("newRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1616 selArray[i].wDay = selday;
1618 if(selArray[0].wDay>selArray[1].wDay) {
1620 tempday = selArray[1].wDay;
1621 selArray[1].wDay = selArray[0].wDay;
1622 selArray[0].wDay = tempday;
1625 MONTHCAL_SetSelRange(infoPtr, (LPARAM)&selArray);
1631 /* only redraw if the currently selected day changed */
1632 /* FIXME: this should specify a rectangle containing only the days that changed */
1633 /* using InvalidateRect */
1634 if(oldselday != infoPtr->curSelDay)
1635 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1642 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1649 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
1653 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
1655 MONTHCAL_Refresh(infoPtr, hdc, &ps);
1656 if (!wParam) EndPaint(infoPtr->hwndSelf, &ps);
1662 MONTHCAL_KillFocus(MONTHCAL_INFO *infoPtr)
1666 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1673 MONTHCAL_SetFocus(MONTHCAL_INFO *infoPtr)
1677 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1682 /* sets the size information */
1683 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
1685 static const WCHAR SunW[] = { 'S','u','n',0 };
1686 static const WCHAR O0W[] = { '0','0',0 };
1687 HDC hdc = GetDC(infoPtr->hwndSelf);
1688 RECT *title=&infoPtr->title;
1689 RECT *prev=&infoPtr->titlebtnprev;
1690 RECT *next=&infoPtr->titlebtnnext;
1691 RECT *titlemonth=&infoPtr->titlemonth;
1692 RECT *titleyear=&infoPtr->titleyear;
1693 RECT *wdays=&infoPtr->wdays;
1694 RECT *weeknumrect=&infoPtr->weeknums;
1695 RECT *days=&infoPtr->days;
1696 RECT *todayrect=&infoPtr->todayrect;
1699 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1701 int xdiv, left_offset;
1704 GetClientRect(infoPtr->hwndSelf, &rcClient);
1706 currentFont = SelectObject(hdc, infoPtr->hFont);
1708 /* get the height and width of each day's text */
1709 GetTextMetricsW(hdc, &tm);
1710 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
1711 GetTextExtentPoint32W(hdc, SunW, 3, &size);
1712 infoPtr->textWidth = size.cx + 2;
1714 /* recalculate the height and width increments and offsets */
1715 GetTextExtentPoint32W(hdc, O0W, 2, &size);
1717 xdiv = (dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
1719 infoPtr->width_increment = size.cx * 2 + 4;
1720 infoPtr->height_increment = infoPtr->textHeight;
1721 left_offset = (rcClient.right - rcClient.left) - (infoPtr->width_increment * xdiv);
1723 /* calculate title area */
1724 title->top = rcClient.top;
1725 title->bottom = title->top + 3 * infoPtr->height_increment / 2;
1726 title->left = left_offset;
1727 title->right = rcClient.right;
1729 /* set the dimensions of the next and previous buttons and center */
1730 /* the month text vertically */
1731 prev->top = next->top = title->top + 4;
1732 prev->bottom = next->bottom = title->bottom - 4;
1733 prev->left = title->left + 4;
1734 prev->right = prev->left + (title->bottom - title->top) ;
1735 next->right = title->right - 4;
1736 next->left = next->right - (title->bottom - title->top);
1738 /* titlemonth->left and right change based upon the current month */
1739 /* and are recalculated in refresh as the current month may change */
1740 /* without the control being resized */
1741 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
1742 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
1744 /* setup the dimensions of the rectangle we draw the names of the */
1745 /* days of the week in */
1746 weeknumrect->left = left_offset;
1747 if(dwStyle & MCS_WEEKNUMBERS)
1748 weeknumrect->right=prev->right;
1750 weeknumrect->right=weeknumrect->left;
1751 wdays->left = days->left = weeknumrect->right;
1752 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
1753 wdays->top = title->bottom ;
1754 wdays->bottom = wdays->top + infoPtr->height_increment;
1756 days->top = weeknumrect->top = wdays->bottom ;
1757 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
1759 todayrect->left = rcClient.left;
1760 todayrect->right = rcClient.right;
1761 todayrect->top = days->bottom;
1762 todayrect->bottom = days->bottom + infoPtr->height_increment;
1764 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
1765 infoPtr->width_increment,infoPtr->height_increment,
1766 wine_dbgstr_rect(&rcClient),
1767 wine_dbgstr_rect(title),
1768 wine_dbgstr_rect(wdays),
1769 wine_dbgstr_rect(days),
1770 wine_dbgstr_rect(todayrect));
1772 /* restore the originally selected font */
1773 SelectObject(hdc, currentFont);
1775 ReleaseDC(infoPtr->hwndSelf, hdc);
1778 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
1780 TRACE("(width=%d, height=%d)\n", Width, Height);
1782 MONTHCAL_UpdateSize(infoPtr);
1784 /* invalidate client area and erase background */
1785 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1790 static LRESULT MONTHCAL_GetFont(MONTHCAL_INFO *infoPtr)
1792 return (LRESULT)infoPtr->hFont;
1795 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
1800 if (!hFont) return 0;
1802 hOldFont = infoPtr->hFont;
1803 infoPtr->hFont = hFont;
1805 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
1806 lf.lfWeight = FW_BOLD;
1807 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
1810 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1812 return (LRESULT)hOldFont;
1815 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
1817 MONTHCAL_Create(HWND hwnd, WPARAM wParam, LPARAM lParam)
1819 MONTHCAL_INFO *infoPtr;
1821 /* allocate memory for info structure */
1822 infoPtr =(MONTHCAL_INFO*)Alloc(sizeof(MONTHCAL_INFO));
1823 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
1825 if(infoPtr == NULL) {
1826 ERR( "could not allocate info memory!\n");
1830 infoPtr->hwndSelf = hwnd;
1831 infoPtr->hwndNotify = ((LPCREATESTRUCTW)lParam)->hwndParent;
1833 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
1835 /* initialize info structure */
1836 /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
1838 GetLocalTime(&infoPtr->todaysDate);
1839 MONTHCAL_SetFirstDayOfWeek(infoPtr, (LPARAM)-1);
1840 infoPtr->currentMonth = infoPtr->todaysDate.wMonth;
1841 infoPtr->currentYear = infoPtr->todaysDate.wYear;
1842 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->minDate);
1843 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
1844 infoPtr->maxDate.wYear=2050;
1845 infoPtr->minDate.wYear=1950;
1846 infoPtr->maxSelCount = 7;
1847 infoPtr->monthRange = 3;
1848 infoPtr->monthdayState = Alloc
1849 (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1850 infoPtr->titlebk = GetSysColor(COLOR_ACTIVECAPTION);
1851 infoPtr->titletxt = GetSysColor(COLOR_WINDOW);
1852 infoPtr->monthbk = GetSysColor(COLOR_WINDOW);
1853 infoPtr->trailingtxt = GetSysColor(COLOR_GRAYTEXT);
1854 infoPtr->bk = GetSysColor(COLOR_WINDOW);
1855 infoPtr->txt = GetSysColor(COLOR_WINDOWTEXT);
1857 /* call MONTHCAL_UpdateSize to set all of the dimensions */
1858 /* of the control */
1859 MONTHCAL_UpdateSize(infoPtr);
1866 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
1868 /* free month calendar info data */
1869 if(infoPtr->monthdayState)
1870 Free(infoPtr->monthdayState);
1871 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
1877 static LRESULT WINAPI
1878 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1880 MONTHCAL_INFO *infoPtr;
1882 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hwnd, uMsg, wParam, lParam);
1884 infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1885 if (!infoPtr && (uMsg != WM_CREATE))
1886 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
1890 return MONTHCAL_GetCurSel(infoPtr, lParam);
1893 return MONTHCAL_SetCurSel(infoPtr, lParam);
1895 case MCM_GETMAXSELCOUNT:
1896 return MONTHCAL_GetMaxSelCount(infoPtr);
1898 case MCM_SETMAXSELCOUNT:
1899 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
1901 case MCM_GETSELRANGE:
1902 return MONTHCAL_GetSelRange(infoPtr, lParam);
1904 case MCM_SETSELRANGE:
1905 return MONTHCAL_SetSelRange(infoPtr, lParam);
1907 case MCM_GETMONTHRANGE:
1908 return MONTHCAL_GetMonthRange(infoPtr);
1910 case MCM_SETDAYSTATE:
1911 return MONTHCAL_SetDayState(infoPtr, wParam, lParam);
1913 case MCM_GETMINREQRECT:
1914 return MONTHCAL_GetMinReqRect(infoPtr, lParam);
1917 return MONTHCAL_GetColor(infoPtr, wParam);
1920 return MONTHCAL_SetColor(infoPtr, wParam, lParam);
1923 return MONTHCAL_GetToday(infoPtr, lParam);
1926 return MONTHCAL_SetToday(infoPtr, lParam);
1929 return MONTHCAL_HitTest(infoPtr, lParam);
1931 case MCM_GETFIRSTDAYOFWEEK:
1932 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
1934 case MCM_SETFIRSTDAYOFWEEK:
1935 return MONTHCAL_SetFirstDayOfWeek(infoPtr, lParam);
1938 return MONTHCAL_GetRange(hwnd, wParam, lParam);
1941 return MONTHCAL_SetRange(infoPtr, wParam, lParam);
1943 case MCM_GETMONTHDELTA:
1944 return MONTHCAL_GetMonthDelta(infoPtr);
1946 case MCM_SETMONTHDELTA:
1947 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
1949 case MCM_GETMAXTODAYWIDTH:
1950 return MONTHCAL_GetMaxTodayWidth(infoPtr);
1953 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1956 return MONTHCAL_KillFocus(infoPtr);
1958 case WM_RBUTTONDOWN:
1959 return MONTHCAL_RButtonDown(infoPtr, lParam);
1961 case WM_LBUTTONDOWN:
1962 return MONTHCAL_LButtonDown(infoPtr, lParam);
1965 return MONTHCAL_MouseMove(infoPtr, wParam, lParam);
1968 return MONTHCAL_LButtonUp(infoPtr, lParam);
1971 return MONTHCAL_Paint(infoPtr, wParam);
1974 return MONTHCAL_SetFocus(infoPtr);
1977 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1980 return MONTHCAL_Create(hwnd, wParam, lParam);
1983 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1986 return MONTHCAL_GetFont(infoPtr);
1989 return MONTHCAL_Timer(infoPtr, wParam);
1992 return MONTHCAL_Destroy(infoPtr);
1995 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
1996 ERR( "unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
1997 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2003 MONTHCAL_Register(void)
2007 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2008 wndClass.style = CS_GLOBALCLASS;
2009 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2010 wndClass.cbClsExtra = 0;
2011 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2012 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2013 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2014 wndClass.lpszClassName = MONTHCAL_CLASSW;
2016 RegisterClassW(&wndClass);
2021 MONTHCAL_Unregister(void)
2023 UnregisterClassW(MONTHCAL_CLASSW, NULL);