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
37 * -- handle resources better (doesn't work now);
38 * -- take care of internationalization.
39 * -- keyboard handling.
40 * -- GetRange: At the moment, we copy ranges anyway, regardless of
41 * infoPtr->rangeValid; an invalid range is simply filled
42 * with zeros in SetRange. Is this the right behavior?
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
88 int firstDayplace; /* place of the first day of the current month */
89 int delta; /* scroll rate; # of months that the */
90 /* control moves when user clicks a scroll button */
91 int visible; /* # of months visible */
92 int firstDay; /* Start month calendar with firstDay's day */
94 MONTHDAYSTATE *monthdayState;
95 SYSTEMTIME todaysDate;
98 int status; /* See MC_SEL flags */
99 int curSelDay; /* current selected day */
100 int firstSelDay; /* first selected day */
108 RECT title; /* rect for the header above the calendar */
109 RECT titlebtnnext; /* the `next month' button in the header */
110 RECT titlebtnprev; /* the `prev month' button in the header */
111 RECT titlemonth; /* the `month name' txt in the header */
112 RECT titleyear; /* the `year number' txt in the header */
113 RECT wdays; /* week days at top */
114 RECT days; /* calendar area */
115 RECT weeknums; /* week numbers at left side */
116 RECT todayrect; /* `today: xx/xx/xx' text rect */
117 HWND hwndNotify; /* Window to receive the notifications */
118 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
119 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
120 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
123 /* Offsets of days in the week to the weekday of january 1. */
124 static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
127 #define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0))
129 /* helper functions */
131 /* returns the number of days in any given month, checking for leap days */
132 /* january is 1, december is 12 */
133 int MONTHCAL_MonthLength(int month, int year)
135 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0};
136 /*Wrap around, this eases handling*/
142 /* if we have a leap year add 1 day to February */
143 /* a leap year is a year either divisible by 400 */
144 /* or divisible by 4 and not by 100 */
145 if(month == 2) { /* February */
146 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
147 (year%4 == 0)) ? 1 : 0);
150 return mdays[month - 1];
155 /* make sure that time is valid */
156 static int MONTHCAL_ValidateTime(SYSTEMTIME time)
158 if(time.wMonth > 12) return FALSE;
159 if(time.wDayOfWeek > 6) return FALSE;
160 if(time.wDay > MONTHCAL_MonthLength(time.wMonth, time.wYear))
162 if(time.wHour > 23) return FALSE;
163 if(time.wMinute > 59) return FALSE;
164 if(time.wSecond > 59) return FALSE;
165 if(time.wMilliseconds > 999) return FALSE;
171 void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
173 to->wYear = from->wYear;
174 to->wMonth = from->wMonth;
175 to->wDayOfWeek = from->wDayOfWeek;
176 to->wDay = from->wDay;
177 to->wHour = from->wHour;
178 to->wMinute = from->wMinute;
179 to->wSecond = from->wSecond;
180 to->wMilliseconds = from->wMilliseconds;
184 /* Note:Depending on DST, this may be offset by a day.
185 Need to find out if we're on a DST place & adjust the clock accordingly.
186 Above function assumes we have a valid data.
187 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
191 /* returns the day in the week(0 == monday, 6 == sunday) */
192 /* day(1 == 1st, 2 == 2nd... etc), year is the year value */
193 static int MONTHCAL_CalculateDayOfWeek(DWORD day, DWORD month, DWORD year)
197 return((year + year/4 - year/100 + year/400 +
198 DayOfWeekTable[month-1] + day - 1 ) % 7);
201 /* From a given point, calculate the row (weekpos), column(daypos)
202 and day in the calendar. day== 0 mean the last day of tha last month
204 static int MONTHCAL_CalcDayFromPos(MONTHCAL_INFO *infoPtr, int x, int y,
205 int *daypos,int *weekpos)
207 int retval, firstDay;
210 GetClientRect(infoPtr->hwndSelf, &rcClient);
212 /* if the point is outside the x bounds of the window put
214 if (x > rcClient.right)
218 *daypos = (x - infoPtr->days.left ) / infoPtr->width_increment;
219 *weekpos = (y - infoPtr->days.top ) / infoPtr->height_increment;
221 firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear)+6 - infoPtr->firstDay)%7;
222 retval = *daypos + (7 * *weekpos) - firstDay;
226 /* day is the day of the month, 1 == 1st day of the month */
227 /* sets x and y to be the position of the day */
228 /* x == day, y == week where(0,0) == firstDay, 1st week */
229 static void MONTHCAL_CalcDayXY(MONTHCAL_INFO *infoPtr, int day, int month,
232 int firstDay, prevMonth;
234 firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear) +6 - infoPtr->firstDay)%7;
236 if(month==infoPtr->currentMonth) {
237 *x = (day + firstDay) % 7;
238 *y = (day + firstDay - *x) / 7;
241 if(month < infoPtr->currentMonth) {
242 prevMonth = month - 1;
246 *x = (MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear) - firstDay) % 7;
251 *y = MONTHCAL_MonthLength(month, infoPtr->currentYear - 1) / 7;
252 *x = (day + firstDay + MONTHCAL_MonthLength(month,
253 infoPtr->currentYear)) % 7;
257 /* x: column(day), y: row(week) */
258 static void MONTHCAL_CalcDayRect(MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
260 r->left = infoPtr->days.left + x * infoPtr->width_increment;
261 r->right = r->left + infoPtr->width_increment;
262 r->top = infoPtr->days.top + y * infoPtr->height_increment;
263 r->bottom = r->top + infoPtr->textHeight;
267 /* sets the RECT struct r to the rectangle around the day and month */
268 /* day is the day value of the month(1 == 1st), month is the month */
269 /* value(january == 1, december == 12) */
270 static inline void MONTHCAL_CalcPosFromDay(MONTHCAL_INFO *infoPtr,
271 int day, int month, RECT *r)
275 MONTHCAL_CalcDayXY(infoPtr, day, month, &x, &y);
276 MONTHCAL_CalcDayRect(infoPtr, r, x, y);
280 /* day is the day in the month(1 == 1st of the month) */
281 /* month is the month value(1 == january, 12 == december) */
282 static void MONTHCAL_CircleDay(MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month)
284 HPEN hRedPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
285 HPEN hOldPen2 = SelectObject(hdc, hRedPen);
291 MONTHCAL_CalcPosFromDay(infoPtr, day, month, &day_rect);
298 points[1].x = x + 0.8 * infoPtr->width_increment;
300 points[2].x = x + 0.9 * infoPtr->width_increment;
302 points[3].x = x + infoPtr->width_increment;
303 points[3].y = y + 0.5 * infoPtr->height_increment;
305 points[4].x = x + infoPtr->width_increment;
306 points[4].y = y + 0.9 * infoPtr->height_increment;
307 points[5].x = x + 0.6 * infoPtr->width_increment;
308 points[5].y = y + 0.9 * infoPtr->height_increment;
309 points[6].x = x + 0.5 * infoPtr->width_increment;
310 points[6].y = y + 0.9 * infoPtr->height_increment; /* bring the bottom up just
311 a hair to fit inside the day rectangle */
313 points[7].x = x + 0.2 * infoPtr->width_increment;
314 points[7].y = y + 0.8 * infoPtr->height_increment;
315 points[8].x = x + 0.1 * infoPtr->width_increment;
316 points[8].y = y + 0.8 * infoPtr->height_increment;
318 points[9].y = y + 0.5 * infoPtr->height_increment;
320 points[10].x = x + 0.1 * infoPtr->width_increment;
321 points[10].y = y + 0.2 * infoPtr->height_increment;
322 points[11].x = x + 0.2 * infoPtr->width_increment;
323 points[11].y = y + 0.3 * infoPtr->height_increment;
324 points[12].x = x + 0.4 * infoPtr->width_increment;
325 points[12].y = y + 0.2 * infoPtr->height_increment;
327 PolyBezier(hdc, points, 13);
328 DeleteObject(hRedPen);
329 SelectObject(hdc, hOldPen2);
333 static void MONTHCAL_DrawDay(MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month,
334 int x, int y, int bold)
338 static int haveBoldFont, haveSelectedDay = FALSE;
343 sprintf(buf, "%d", day);
345 /* No need to check styles: when selection is not valid, it is set to zero.
346 * 1<day<31, so evertyhing's OK.
349 MONTHCAL_CalcDayRect(infoPtr, &r, x, y);
351 if((day>=infoPtr->minSel.wDay) && (day<=infoPtr->maxSel.wDay)
352 && (month==infoPtr->currentMonth)) {
356 TRACE("%d %d %d\n",day, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
357 TRACE("%ld %ld %ld %ld\n", r.left, r.top, r.right, r.bottom);
358 oldCol = SetTextColor(hdc, infoPtr->monthbk);
359 oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
360 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
361 hrgn = CreateEllipticRgn(r.left, r.top, r.right, r.bottom);
362 FillRgn(hdc, hrgn, hbr);
364 /* FIXME: this may need to be changed now b/c of the other
365 drawing changes 11/3/99 CMM */
366 r2.left = r.left - 0.25 * infoPtr->textWidth;
368 r2.right = r.left + 0.5 * infoPtr->textWidth;
369 r2.bottom = r.bottom;
370 if(haveSelectedDay) FillRect(hdc, &r2, hbr);
371 haveSelectedDay = TRUE;
373 haveSelectedDay = FALSE;
376 /* need to add some code for multiple selections */
378 if((bold) &&(!haveBoldFont)) {
379 SelectObject(hdc, infoPtr->hBoldFont);
382 if((!bold) &&(haveBoldFont)) {
383 SelectObject(hdc, infoPtr->hFont);
384 haveBoldFont = FALSE;
387 if(haveSelectedDay) {
388 SetTextColor(hdc, oldCol);
389 SetBkColor(hdc, oldBk);
392 SetBkMode(hdc,TRANSPARENT);
393 DrawTextA(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
395 /* draw a rectangle around the currently selected days text */
396 if((day==infoPtr->curSelDay) && (month==infoPtr->currentMonth))
397 DrawFocusRect(hdc, &r);
401 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, PAINTSTRUCT* ps)
403 RECT *title=&infoPtr->title;
404 RECT *prev=&infoPtr->titlebtnprev;
405 RECT *next=&infoPtr->titlebtnnext;
406 RECT *titlemonth=&infoPtr->titlemonth;
407 RECT *titleyear=&infoPtr->titleyear;
411 int i, j, m, mask, day, firstDay, weeknum, weeknum1,prevMonth;
412 int textHeight = infoPtr->textHeight, textWidth = infoPtr->textWidth;
419 COLORREF oldTextColor, oldBkColor;
420 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
422 RECT rcDay; /* used in MONTHCAL_CalcDayRect() */
423 SYSTEMTIME localtime;
426 oldTextColor = SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
428 /* fill background */
429 hbr = CreateSolidBrush (infoPtr->bk);
430 FillRect(hdc, &ps->rcPaint, hbr);
434 if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
436 hbr = CreateSolidBrush(infoPtr->titlebk);
437 FillRect(hdc, title, hbr);
441 /* if the previous button is pressed draw it depressed */
442 if(IntersectRect(&rcTemp, &(ps->rcPaint), prev))
444 if((infoPtr->status & MC_PREVPRESSED))
445 DrawFrameControl(hdc, prev, DFC_SCROLL,
446 DFCS_SCROLLLEFT | DFCS_PUSHED |
447 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
448 else /* if the previous button is pressed draw it depressed */
449 DrawFrameControl(hdc, prev, DFC_SCROLL,
450 DFCS_SCROLLLEFT |(dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
453 /* if next button is depressed draw it depressed */
454 if(IntersectRect(&rcTemp, &(ps->rcPaint), next))
456 if((infoPtr->status & MC_NEXTPRESSED))
457 DrawFrameControl(hdc, next, DFC_SCROLL,
458 DFCS_SCROLLRIGHT | DFCS_PUSHED |
459 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
460 else /* if the next button is pressed draw it depressed */
461 DrawFrameControl(hdc, next, DFC_SCROLL,
462 DFCS_SCROLLRIGHT |(dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
465 oldBkColor = SetBkColor(hdc, infoPtr->titlebk);
466 SetTextColor(hdc, infoPtr->titletxt);
467 currentFont = SelectObject(hdc, infoPtr->hBoldFont);
469 /* titlemonth->left and right are set in MONTHCAL_UpdateSize */
470 titlemonth->left = title->left;
471 titlemonth->right = title->right;
473 GetLocaleInfoA( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+infoPtr->currentMonth -1,
475 sprintf(buf, "%s %ld", buf1, infoPtr->currentYear);
477 if(IntersectRect(&rcTemp, &(ps->rcPaint), titlemonth))
479 DrawTextA(hdc, buf, strlen(buf), titlemonth,
480 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
483 SelectObject(hdc, infoPtr->hFont);
485 /* titlemonth left/right contained rect for whole titletxt('June 1999')
486 * MCM_HitTestInfo wants month & year rects, so prepare these now.
487 *(no, we can't draw them separately; the whole text is centered)
489 GetTextExtentPoint32A(hdc, buf, strlen(buf), &size);
490 titlemonth->left = title->right / 2 - size.cx / 2;
491 titleyear->right = title->right / 2 + size.cx / 2;
492 GetTextExtentPoint32A(hdc, buf1, strlen(buf1), &size);
493 titlemonth->right = titlemonth->left + size.cx;
494 titleyear->left = titlemonth->right;
496 /* draw month area */
497 rcTemp.top=infoPtr->wdays.top;
498 rcTemp.left=infoPtr->wdays.left;
499 rcTemp.bottom=infoPtr->todayrect.bottom;
500 rcTemp.right =infoPtr->todayrect.right;
501 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcTemp))
503 hbr = CreateSolidBrush(infoPtr->monthbk);
504 FillRect(hdc, &rcTemp, hbr);
508 /* draw line under day abbreviatons */
510 MoveToEx(hdc, infoPtr->days.left + 3, title->bottom + textHeight + 1, NULL);
511 LineTo(hdc, infoPtr->days.right - 3, title->bottom + textHeight + 1);
513 prevMonth = infoPtr->currentMonth - 1;
514 if(prevMonth == 0) /* if currentMonth is january(1) prevMonth is */
515 prevMonth = 12; /* december(12) of the previous year */
517 infoPtr->wdays.left = infoPtr->days.left = infoPtr->weeknums.right;
518 /* draw day abbreviations */
520 SetBkColor(hdc, infoPtr->monthbk);
521 SetTextColor(hdc, infoPtr->trailingtxt);
523 /* copy this rect so we can change the values without changing */
524 /* the original version */
525 days->left = infoPtr->wdays.left;
526 days->right = days->left + infoPtr->width_increment;
527 days->top = infoPtr->wdays.top;
528 days->bottom = infoPtr->wdays.bottom;
530 i = infoPtr->firstDay;
533 GetLocaleInfoA( LOCALE_USER_DEFAULT,LOCALE_SABBREVDAYNAME1 + (i +j)%7,
535 DrawTextA(hdc, buf, strlen(buf), days,
536 DT_CENTER | DT_VCENTER | DT_SINGLELINE );
537 days->left+=infoPtr->width_increment;
538 days->right+=infoPtr->width_increment;
541 /* draw day numbers; first, the previous month */
543 firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear);
545 day = MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear) +
546 (infoPtr->firstDay + 7 - firstDay)%7 + 1;
547 if (day > MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear))
549 startofprescal = day;
554 while(day <= MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear)) {
555 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
556 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
558 MONTHCAL_DrawDay(infoPtr, hdc, day, prevMonth, i, 0,
559 infoPtr->monthdayState[m] & mask);
567 /* draw `current' month */
569 day = 1; /* start at the beginning of the current month */
571 infoPtr->firstDayplace = i;
572 SetTextColor(hdc, infoPtr->txt);
576 /* draw the first week of the current month */
578 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
579 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
582 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth, i, 0,
583 infoPtr->monthdayState[m] & mask);
585 if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
586 (day==infoPtr->todaysDate.wDay) &&
587 (infoPtr->currentYear == infoPtr->todaysDate.wYear)) {
588 if(!(dwStyle & MCS_NOTODAYCIRCLE))
589 MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->currentMonth);
598 j = 1; /* move to the 2nd week of the current month */
599 i = 0; /* move back to sunday */
600 while(day <= MONTHCAL_MonthLength(infoPtr->currentMonth, infoPtr->currentYear)) {
601 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
602 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
604 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth, i, j,
605 infoPtr->monthdayState[m] & mask);
607 if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
608 (day==infoPtr->todaysDate.wDay) &&
609 (infoPtr->currentYear == infoPtr->todaysDate.wYear))
610 if(!(dwStyle & MCS_NOTODAYCIRCLE))
611 MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->currentMonth);
616 if(i>6) { /* past saturday, goto the next weeks sunday */
622 /* draw `next' month */
624 day = 1; /* start at the first day of the next month */
628 SetTextColor(hdc, infoPtr->trailingtxt);
629 while((i<7) &&(j<6)) {
630 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
631 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
633 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth + 1, i, j,
634 infoPtr->monthdayState[m] & mask);
640 if(i==7) { /* past saturday, go to next week's sunday */
645 SetTextColor(hdc, infoPtr->txt);
648 /* draw `today' date if style allows it, and draw a circle before today's
649 * date if necessary */
651 if(!(dwStyle & MCS_NOTODAY)) {
653 if(!(dwStyle & MCS_NOTODAYCIRCLE)) {
654 /*day is the number of days from nextmonth we put on the calendar */
655 MONTHCAL_CircleDay(infoPtr, hdc,
656 day+MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear),
657 infoPtr->currentMonth);
660 if (!LoadStringA(COMCTL32_hModule,IDM_TODAY,buf1,sizeof(buf1)))
662 WARN("Can't load resource\n");
663 strcpy(buf1,"Today:");
665 MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
666 MONTHCAL_CopyTime(&infoPtr->todaysDate,&localtime);
667 GetDateFormatA(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&localtime,NULL,buf2,sizeof(buf2));
668 sprintf(buf, "%s %s", buf1,buf2);
669 SelectObject(hdc, infoPtr->hBoldFont);
671 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rtoday))
673 DrawTextA(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
674 DrawTextA(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
676 SelectObject(hdc, infoPtr->hFont);
679 /*eventually draw week numbers*/
680 if(dwStyle & MCS_WEEKNUMBERS) {
681 /* display weeknumbers*/
684 /* Rules what week to call the first week of a new year:
685 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
686 The week containing Jan 1 is the first week of year
687 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
688 First week of year must contain 4 days of the new year
689 LOCALE_IFIRSTWEEKOFYEAR == 1 (what contries?)
690 The first week of the year must contain only days of the new year
692 GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR,
694 sscanf(buf, "%d", &weeknum);
705 if (infoPtr->currentMonth < 2)
707 /* calculate all those exceptions for january */
708 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear);
709 if ((infoPtr->firstDay +7 - weeknum1)%7 > mindays)
715 weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->currentYear-1);
716 weeknum +=startofprescal+ 7;
718 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear-1);
719 if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
726 for(i=0; i<prevMonth-1; i++)
727 weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->currentYear);
728 weeknum +=startofprescal+ 7;
730 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear);
731 if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
734 days->left = infoPtr->weeknums.left;
735 days->right = infoPtr->weeknums.right;
736 days->top = infoPtr->weeknums.top;
737 days->bottom = days->top +infoPtr->height_increment;
739 if((i==0)&&(weeknum>50))
741 sprintf(buf, "%d", weeknum);
744 else if((i==5)&&(weeknum>47))
746 sprintf(buf, "%d", 1);
749 sprintf(buf, "%d", weeknum + i);
750 DrawTextA(hdc, buf, -1, days, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
751 days->top+=infoPtr->height_increment;
752 days->bottom+=infoPtr->height_increment;
755 MoveToEx(hdc, infoPtr->weeknums.right, infoPtr->weeknums.top + 3 , NULL);
756 LineTo(hdc, infoPtr->weeknums.right, infoPtr->weeknums.bottom );
759 /* currentFont was font at entering Refresh */
761 SetBkColor(hdc, oldBkColor);
762 SelectObject(hdc, currentFont);
763 SetTextColor(hdc, oldTextColor);
768 MONTHCAL_GetMinReqRect(MONTHCAL_INFO *infoPtr, LPARAM lParam)
770 LPRECT lpRect = (LPRECT) lParam;
772 TRACE("rect %p\n", lpRect);
774 /* validate parameters */
776 if((infoPtr==NULL) ||(lpRect == NULL) ) return FALSE;
778 lpRect->left = infoPtr->title.left;
779 lpRect->top = infoPtr->title.top;
780 lpRect->right = infoPtr->title.right;
781 lpRect->bottom = infoPtr->todayrect.bottom;
782 AdjustWindowRect(lpRect, GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE), FALSE);
784 TRACE("%s\n", wine_dbgstr_rect(lpRect));
791 MONTHCAL_GetColor(MONTHCAL_INFO *infoPtr, WPARAM wParam)
795 switch((int)wParam) {
796 case MCSC_BACKGROUND:
801 return infoPtr->titlebk;
803 return infoPtr->titletxt;
805 return infoPtr->monthbk;
806 case MCSC_TRAILINGTEXT:
807 return infoPtr->trailingtxt;
815 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
819 TRACE("%d: color %08lx\n", wParam, lParam);
821 switch((int)wParam) {
822 case MCSC_BACKGROUND:
824 infoPtr->bk = (COLORREF)lParam;
828 infoPtr->txt = (COLORREF)lParam;
831 prev = infoPtr->titlebk;
832 infoPtr->titlebk = (COLORREF)lParam;
835 prev=infoPtr->titletxt;
836 infoPtr->titletxt = (COLORREF)lParam;
839 prev = infoPtr->monthbk;
840 infoPtr->monthbk = (COLORREF)lParam;
842 case MCSC_TRAILINGTEXT:
843 prev = infoPtr->trailingtxt;
844 infoPtr->trailingtxt = (COLORREF)lParam;
848 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
854 MONTHCAL_GetMonthDelta(MONTHCAL_INFO *infoPtr)
859 return infoPtr->delta;
861 return infoPtr->visible;
866 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, WPARAM wParam)
868 int prev = infoPtr->delta;
870 TRACE("delta %d\n", wParam);
872 infoPtr->delta = (int)wParam;
878 MONTHCAL_GetFirstDayOfWeek(MONTHCAL_INFO *infoPtr)
880 return infoPtr->firstDay;
884 /* sets the first day of the week that will appear in the control */
885 /* 0 == Monday, 6 == Sunday */
886 /* FIXME: this needs to be implemented properly in MONTHCAL_Refresh() */
887 /* FIXME: we need more error checking here */
889 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, LPARAM lParam)
891 int prev = infoPtr->firstDay;
895 TRACE("day %ld\n", lParam);
897 if((lParam >= 0) && (lParam < 7)) {
898 infoPtr->firstDay = (int)lParam;
902 GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK,
904 TRACE("%s %d\n", buf, strlen(buf));
905 if(sscanf(buf, "%d", &day) == 1)
906 infoPtr->firstDay = day;
908 infoPtr->firstDay = 0;
915 MONTHCAL_GetMonthRange(MONTHCAL_INFO *infoPtr)
919 return infoPtr->monthRange;
924 MONTHCAL_GetMaxTodayWidth(MONTHCAL_INFO *infoPtr)
926 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
930 /* FIXME: are validated times taken from current date/time or simply
932 * FIXME: check whether MCM_GETMONTHRANGE shows correct result after
933 * adjusting range with MCM_SETRANGE
937 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
939 SYSTEMTIME *lprgSysTimeArray=(SYSTEMTIME *)lParam;
942 TRACE("%x %lx\n", wParam, lParam);
944 if(wParam & GDTR_MAX) {
945 if(MONTHCAL_ValidateTime(lprgSysTimeArray[1])){
946 MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxDate);
947 infoPtr->rangeValid|=GDTR_MAX;
949 GetSystemTime(&infoPtr->todaysDate);
950 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
953 if(wParam & GDTR_MIN) {
954 if(MONTHCAL_ValidateTime(lprgSysTimeArray[0])) {
955 MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minDate);
956 infoPtr->rangeValid|=GDTR_MIN;
958 GetSystemTime(&infoPtr->todaysDate);
959 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->minDate);
963 prev = infoPtr->monthRange;
964 infoPtr->monthRange = infoPtr->maxDate.wMonth - infoPtr->minDate.wMonth;
966 if(infoPtr->monthRange!=prev) {
967 infoPtr->monthdayState = ReAlloc(infoPtr->monthdayState,
968 infoPtr->monthRange * sizeof(MONTHDAYSTATE));
976 MONTHCAL_GetRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
978 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
979 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *)lParam;
981 /* validate parameters */
983 if((infoPtr==NULL) || (lprgSysTimeArray==NULL)) return FALSE;
985 MONTHCAL_CopyTime(&infoPtr->maxDate, &lprgSysTimeArray[1]);
986 MONTHCAL_CopyTime(&infoPtr->minDate, &lprgSysTimeArray[0]);
988 return infoPtr->rangeValid;
993 MONTHCAL_SetDayState(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
996 int i, iMonths = (int)wParam;
997 MONTHDAYSTATE *dayStates = (LPMONTHDAYSTATE)lParam;
999 TRACE("%x %lx\n", wParam, lParam);
1000 if(iMonths!=infoPtr->monthRange) return 0;
1002 for(i=0; i<iMonths; i++)
1003 infoPtr->monthdayState[i] = dayStates[i];
1008 MONTHCAL_GetCurSel(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1010 SYSTEMTIME *lpSel = (SYSTEMTIME *) lParam;
1012 TRACE("%lx\n", lParam);
1013 if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
1014 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
1016 MONTHCAL_CopyTime(&infoPtr->minSel, lpSel);
1017 TRACE("%d/%d/%d\n", lpSel->wYear, lpSel->wMonth, lpSel->wDay);
1021 /* FIXME: if the specified date is not visible, make it visible */
1022 /* FIXME: redraw? */
1024 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1026 SYSTEMTIME *lpSel = (SYSTEMTIME *)lParam;
1028 TRACE("%lx\n", lParam);
1029 if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
1030 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
1032 infoPtr->currentMonth=lpSel->wMonth;
1033 infoPtr->currentYear=lpSel->wYear;
1035 MONTHCAL_CopyTime(lpSel, &infoPtr->minSel);
1036 MONTHCAL_CopyTime(lpSel, &infoPtr->maxSel);
1038 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1045 MONTHCAL_GetMaxSelCount(MONTHCAL_INFO *infoPtr)
1047 return infoPtr->maxSelCount;
1052 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1054 TRACE("%x\n", wParam);
1056 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) {
1057 infoPtr->maxSelCount = wParam;
1065 MONTHCAL_GetSelRange(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1067 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1069 TRACE("%lx\n", lParam);
1071 /* validate parameters */
1073 if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1075 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)
1077 MONTHCAL_CopyTime(&infoPtr->maxSel, &lprgSysTimeArray[1]);
1078 MONTHCAL_CopyTime(&infoPtr->minSel, &lprgSysTimeArray[0]);
1079 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1088 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1090 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1092 TRACE("%lx\n", lParam);
1094 /* validate parameters */
1096 if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1098 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)
1100 MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxSel);
1101 MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minSel);
1102 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1111 MONTHCAL_GetToday(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1113 SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1115 TRACE("%lx\n", lParam);
1117 /* validate parameters */
1119 if((infoPtr==NULL) || (lpToday==NULL)) return FALSE;
1120 MONTHCAL_CopyTime(&infoPtr->todaysDate, lpToday);
1126 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1128 SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1130 TRACE("%lx\n", lParam);
1132 /* validate parameters */
1134 if((infoPtr==NULL) ||(lpToday==NULL)) return FALSE;
1135 MONTHCAL_CopyTime(lpToday, &infoPtr->todaysDate);
1136 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1142 MONTHCAL_HitTest(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1144 PMCHITTESTINFO lpht = (PMCHITTESTINFO)lParam;
1152 retval = MCHT_NOWHERE;
1154 ZeroMemory(&lpht->st, sizeof(lpht->st));
1156 /* Comment in for debugging...
1157 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,
1158 infoPtr->wdays.left, infoPtr->wdays.right,
1159 infoPtr->wdays.top, infoPtr->wdays.bottom,
1160 infoPtr->days.left, infoPtr->days.right,
1161 infoPtr->days.top, infoPtr->days.bottom,
1162 infoPtr->todayrect.left, infoPtr->todayrect.right,
1163 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1164 infoPtr->weeknums.left, infoPtr->weeknums.right,
1165 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1168 /* are we in the header? */
1170 if(PtInRect(&infoPtr->title, lpht->pt)) {
1171 if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1172 retval = MCHT_TITLEBTNPREV;
1175 if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1176 retval = MCHT_TITLEBTNNEXT;
1179 if(PtInRect(&infoPtr->titlemonth, lpht->pt)) {
1180 retval = MCHT_TITLEMONTH;
1183 if(PtInRect(&infoPtr->titleyear, lpht->pt)) {
1184 retval = MCHT_TITLEYEAR;
1188 retval = MCHT_TITLE;
1192 day = MONTHCAL_CalcDayFromPos(infoPtr,x,y,&wday,&wnum);
1193 if(PtInRect(&infoPtr->wdays, lpht->pt)) {
1194 retval = MCHT_CALENDARDAY;
1195 lpht->st.wYear = infoPtr->currentYear;
1196 lpht->st.wMonth = (day < 1)? infoPtr->currentMonth -1 : infoPtr->currentMonth;
1197 lpht->st.wDay = (day < 1)?
1198 MONTHCAL_MonthLength(infoPtr->currentMonth-1,infoPtr->currentYear) -day : day;
1201 if(PtInRect(&infoPtr->weeknums, lpht->pt)) {
1202 retval = MCHT_CALENDARWEEKNUM;
1203 lpht->st.wYear = infoPtr->currentYear;
1204 lpht->st.wMonth = (day < 1) ? infoPtr->currentMonth -1 :
1205 (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear)) ?
1206 infoPtr->currentMonth +1 :infoPtr->currentMonth;
1207 lpht->st.wDay = (day < 1 ) ?
1208 MONTHCAL_MonthLength(infoPtr->currentMonth-1,infoPtr->currentYear) -day :
1209 (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear)) ?
1210 day - MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear) : day;
1213 if(PtInRect(&infoPtr->days, lpht->pt))
1215 lpht->st.wYear = infoPtr->currentYear;
1218 retval = MCHT_CALENDARDATEPREV;
1219 lpht->st.wMonth = infoPtr->currentMonth - 1;
1220 if (lpht->st.wMonth <1)
1222 lpht->st.wMonth = 12;
1225 lpht->st.wDay = MONTHCAL_MonthLength(lpht->st.wMonth,lpht->st.wYear) -day;
1227 else if (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear))
1229 retval = MCHT_CALENDARDATENEXT;
1230 lpht->st.wMonth = infoPtr->currentMonth + 1;
1231 if (lpht->st.wMonth <12)
1233 lpht->st.wMonth = 1;
1236 lpht->st.wDay = day - MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear) ;
1239 retval = MCHT_CALENDARDATE;
1240 lpht->st.wMonth = infoPtr->currentMonth;
1241 lpht->st.wDay = day;
1245 if(PtInRect(&infoPtr->todayrect, lpht->pt)) {
1246 retval = MCHT_TODAYLINK;
1251 /* Hit nothing special? What's left must be background :-) */
1253 retval = MCHT_CALENDARBK;
1255 lpht->uHit = retval;
1260 static void MONTHCAL_GoToNextMonth(MONTHCAL_INFO *infoPtr)
1262 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1264 TRACE("MONTHCAL_GoToNextMonth\n");
1266 infoPtr->currentMonth++;
1267 if(infoPtr->currentMonth > 12) {
1268 infoPtr->currentYear++;
1269 infoPtr->currentMonth = 1;
1272 if(dwStyle & MCS_DAYSTATE) {
1276 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1277 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1278 nmds.nmhdr.code = MCN_GETDAYSTATE;
1279 nmds.cDayState = infoPtr->monthRange;
1280 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1282 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1283 (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
1284 for(i=0; i<infoPtr->monthRange; i++)
1285 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1290 static void MONTHCAL_GoToPrevMonth(MONTHCAL_INFO *infoPtr)
1292 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1296 infoPtr->currentMonth--;
1297 if(infoPtr->currentMonth < 1) {
1298 infoPtr->currentYear--;
1299 infoPtr->currentMonth = 12;
1302 if(dwStyle & MCS_DAYSTATE) {
1306 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1307 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1308 nmds.nmhdr.code = MCN_GETDAYSTATE;
1309 nmds.cDayState = infoPtr->monthRange;
1310 nmds.prgDayState = Alloc
1311 (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1313 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1314 (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
1315 for(i=0; i<infoPtr->monthRange; i++)
1316 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1321 MONTHCAL_RButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1327 hMenu = CreatePopupMenu();
1328 if (!LoadStringA(COMCTL32_hModule,IDM_GOTODAY,buf,sizeof(buf)))
1330 WARN("Can't load resource\n");
1331 strcpy(buf,"Go to Today:");
1333 AppendMenuA(hMenu, MF_STRING|MF_ENABLED,1, buf);
1334 menupoint.x=(INT)LOWORD(lParam);
1335 menupoint.y=(INT)HIWORD(lParam);
1336 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1337 if( TrackPopupMenu(hMenu,TPM_RIGHTBUTTON| TPM_NONOTIFY|TPM_RETURNCMD,
1338 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1340 infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
1341 infoPtr->currentYear=infoPtr->todaysDate.wYear;
1342 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1348 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1353 RECT rcDay; /* used in determining area to invalidate */
1358 TRACE("%lx\n", lParam);
1360 if (infoPtr->hWndYearUpDown)
1362 infoPtr->currentYear=SendMessageA( infoPtr->hWndYearUpDown, UDM_SETPOS, (WPARAM) 0,(LPARAM)0);
1363 if(!DestroyWindow(infoPtr->hWndYearUpDown))
1365 FIXME("Can't destroy Updown Control\n");
1368 infoPtr->hWndYearUpDown=0;
1369 if(!DestroyWindow(infoPtr->hWndYearEdit))
1371 FIXME("Can't destroy Updown Control\n");
1374 infoPtr->hWndYearEdit=0;
1375 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1378 ht.pt.x = (INT)LOWORD(lParam);
1379 ht.pt.y = (INT)HIWORD(lParam);
1380 hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1382 /* FIXME: these flags should be checked by */
1383 /*((hit & MCHT_XXX) == MCHT_XXX) b/c some of the flags are */
1385 if(hit ==MCHT_TITLEBTNNEXT) {
1386 MONTHCAL_GoToNextMonth(infoPtr);
1387 infoPtr->status = MC_NEXTPRESSED;
1388 SetTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1389 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1392 if(hit == MCHT_TITLEBTNPREV){
1393 MONTHCAL_GoToPrevMonth(infoPtr);
1394 infoPtr->status = MC_PREVPRESSED;
1395 SetTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1396 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1400 if(hit == MCHT_TITLEMONTH) {
1401 hMenu = CreatePopupMenu();
1405 GetLocaleInfoA( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+i,
1407 AppendMenuA(hMenu, MF_STRING|MF_ENABLED,i+1, buf);
1409 menupoint.x=infoPtr->titlemonth.right;
1410 menupoint.y=infoPtr->titlemonth.bottom;
1411 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1412 i= TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
1413 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
1414 if ((i>0) && (i<13))
1416 infoPtr->currentMonth=i;
1417 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1420 if(hit == MCHT_TITLEYEAR) {
1421 infoPtr->hWndYearEdit=CreateWindowExA(0,
1424 WS_VISIBLE | WS_CHILD |UDS_SETBUDDYINT,
1425 infoPtr->titleyear.left+3,infoPtr->titlebtnnext.top,
1426 infoPtr->titleyear.right-infoPtr->titleyear.left,
1427 infoPtr->textHeight,
1432 infoPtr->hWndYearUpDown=CreateWindowExA(0,
1435 WS_VISIBLE | WS_CHILD |UDS_SETBUDDYINT|UDS_NOTHOUSANDS|UDS_ARROWKEYS,
1436 infoPtr->titleyear.right+6,infoPtr->titlebtnnext.top,
1438 infoPtr->textHeight,
1443 SendMessageA( infoPtr->hWndYearUpDown, UDM_SETRANGE, (WPARAM) 0, MAKELONG (9999, 1753));
1444 SendMessageA( infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM) infoPtr->hWndYearEdit, (LPARAM)0 );
1445 SendMessageA( infoPtr->hWndYearUpDown, UDM_SETPOS, (WPARAM) 0,(LPARAM)infoPtr->currentYear );
1449 if(hit == MCHT_TODAYLINK) {
1450 infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
1451 infoPtr->currentYear=infoPtr->todaysDate.wYear;
1452 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1455 if(hit == MCHT_CALENDARDATE) {
1456 SYSTEMTIME selArray[2];
1459 MONTHCAL_CopyTime(&ht.st, &selArray[0]);
1460 MONTHCAL_CopyTime(&ht.st, &selArray[1]);
1461 MONTHCAL_SetSelRange(infoPtr, (LPARAM)&selArray);
1462 MONTHCAL_SetCurSel(infoPtr, (LPARAM)&selArray);
1463 TRACE("MCHT_CALENDARDATE\n");
1464 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1465 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1466 nmsc.nmhdr.code = MCN_SELCHANGE;
1467 MONTHCAL_CopyTime(&infoPtr->minSel,&nmsc.stSelStart);
1468 MONTHCAL_CopyTime(&infoPtr->maxSel,&nmsc.stSelEnd);
1470 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1471 (WPARAM)nmsc.nmhdr.idFrom,(LPARAM)&nmsc);
1474 /* redraw both old and new days if the selected day changed */
1475 if(infoPtr->curSelDay != ht.st.wDay) {
1476 MONTHCAL_CalcPosFromDay(infoPtr, ht.st.wDay, ht.st.wMonth, &rcDay);
1477 InvalidateRect(infoPtr->hwndSelf, &rcDay, TRUE);
1479 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->curSelDay, infoPtr->currentMonth, &rcDay);
1480 InvalidateRect(infoPtr->hwndSelf, &rcDay, TRUE);
1483 infoPtr->firstSelDay = ht.st.wDay;
1484 infoPtr->curSelDay = ht.st.wDay;
1485 infoPtr->status = MC_SEL_LBUTDOWN;
1494 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1498 BOOL redraw = FALSE;
1504 if(infoPtr->status & MC_NEXTPRESSED) {
1505 KillTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER);
1506 infoPtr->status &= ~MC_NEXTPRESSED;
1509 if(infoPtr->status & MC_PREVPRESSED) {
1510 KillTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER);
1511 infoPtr->status &= ~MC_PREVPRESSED;
1515 ht.pt.x = (INT)LOWORD(lParam);
1516 ht.pt.y = (INT)HIWORD(lParam);
1517 hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1519 infoPtr->status = MC_SEL_LBUTUP;
1521 if(hit ==MCHT_CALENDARDATENEXT) {
1522 MONTHCAL_GoToNextMonth(infoPtr);
1523 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1526 if(hit == MCHT_CALENDARDATEPREV){
1527 MONTHCAL_GoToPrevMonth(infoPtr);
1528 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1531 nmhdr.hwndFrom = infoPtr->hwndSelf;
1532 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1533 nmhdr.code = NM_RELEASEDCAPTURE;
1534 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
1536 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1537 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
1538 /* redraw if necessary */
1540 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1541 /* only send MCN_SELECT if currently displayed month's day was selected */
1542 if(hit == MCHT_CALENDARDATE) {
1543 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1544 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1545 nmsc.nmhdr.code = MCN_SELECT;
1546 MONTHCAL_CopyTime(&infoPtr->minSel, &nmsc.stSelStart);
1547 MONTHCAL_CopyTime(&infoPtr->maxSel, &nmsc.stSelEnd);
1549 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1550 (WPARAM)nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1558 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1560 BOOL redraw = FALSE;
1562 TRACE("%d\n", wParam);
1565 case MC_NEXTMONTHTIMER:
1567 MONTHCAL_GoToNextMonth(infoPtr);
1569 case MC_PREVMONTHTIMER:
1571 MONTHCAL_GoToPrevMonth(infoPtr);
1574 ERR("got unknown timer\n");
1578 /* redraw only if necessary */
1580 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1587 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1590 int oldselday, selday, hit;
1593 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
1595 ht.pt.x = LOWORD(lParam);
1596 ht.pt.y = HIWORD(lParam);
1598 hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1600 /* not on the calendar date numbers? bail out */
1601 TRACE("hit:%x\n",hit);
1602 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE) return 0;
1604 selday = ht.st.wDay;
1605 oldselday = infoPtr->curSelDay;
1606 infoPtr->curSelDay = selday;
1607 MONTHCAL_CalcPosFromDay(infoPtr, selday, ht.st. wMonth, &r);
1609 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) {
1610 SYSTEMTIME selArray[2];
1613 MONTHCAL_GetSelRange(infoPtr, (LPARAM)&selArray);
1615 if(infoPtr->firstSelDay==selArray[0].wDay) i=1;
1616 TRACE("oldRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1617 if(infoPtr->firstSelDay==selArray[1].wDay) {
1618 /* 1st time we get here: selArray[0]=selArray[1]) */
1619 /* if we're still at the first selected date, return */
1620 if(infoPtr->firstSelDay==selday) goto done;
1621 if(selday<infoPtr->firstSelDay) i = 0;
1624 if(abs(infoPtr->firstSelDay - selday) >= infoPtr->maxSelCount) {
1625 if(selday>infoPtr->firstSelDay)
1626 selday = infoPtr->firstSelDay + infoPtr->maxSelCount;
1628 selday = infoPtr->firstSelDay - infoPtr->maxSelCount;
1631 if(selArray[i].wDay!=selday) {
1632 TRACE("newRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1634 selArray[i].wDay = selday;
1636 if(selArray[0].wDay>selArray[1].wDay) {
1638 tempday = selArray[1].wDay;
1639 selArray[1].wDay = selArray[0].wDay;
1640 selArray[0].wDay = tempday;
1643 MONTHCAL_SetSelRange(infoPtr, (LPARAM)&selArray);
1649 /* only redraw if the currently selected day changed */
1650 /* FIXME: this should specify a rectangle containing only the days that changed */
1651 /* using InvalidateRect */
1652 if(oldselday != infoPtr->curSelDay)
1653 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1660 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1667 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
1671 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
1673 MONTHCAL_Refresh(infoPtr, hdc, &ps);
1674 if (!wParam) EndPaint(infoPtr->hwndSelf, &ps);
1680 MONTHCAL_KillFocus(MONTHCAL_INFO *infoPtr)
1684 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1691 MONTHCAL_SetFocus(MONTHCAL_INFO *infoPtr)
1695 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1700 /* sets the size information */
1701 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
1703 HDC hdc = GetDC(infoPtr->hwndSelf);
1704 RECT *title=&infoPtr->title;
1705 RECT *prev=&infoPtr->titlebtnprev;
1706 RECT *next=&infoPtr->titlebtnnext;
1707 RECT *titlemonth=&infoPtr->titlemonth;
1708 RECT *titleyear=&infoPtr->titleyear;
1709 RECT *wdays=&infoPtr->wdays;
1710 RECT *weeknumrect=&infoPtr->weeknums;
1711 RECT *days=&infoPtr->days;
1712 RECT *todayrect=&infoPtr->todayrect;
1715 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1717 int xdiv, left_offset;
1720 GetClientRect(infoPtr->hwndSelf, &rcClient);
1722 currentFont = SelectObject(hdc, infoPtr->hFont);
1724 /* get the height and width of each day's text */
1725 GetTextMetricsA(hdc, &tm);
1726 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
1727 GetTextExtentPoint32A(hdc, "Sun", 3, &size);
1728 infoPtr->textWidth = size.cx + 2;
1730 /* recalculate the height and width increments and offsets */
1731 GetTextExtentPoint32A(hdc, "00", 2, &size);
1733 xdiv = (dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
1735 infoPtr->width_increment = size.cx * 2;
1736 infoPtr->height_increment = infoPtr->textHeight;
1737 left_offset = (rcClient.right - rcClient.left) - (infoPtr->width_increment * xdiv);
1739 /* calculate title area */
1740 title->top = rcClient.top;
1741 title->bottom = title->top + 3 * infoPtr->height_increment / 2;
1742 title->left = left_offset;
1743 title->right = rcClient.right;
1745 /* set the dimensions of the next and previous buttons and center */
1746 /* the month text vertically */
1747 prev->top = next->top = title->top + 4;
1748 prev->bottom = next->bottom = title->bottom - 4;
1749 prev->left = title->left + 4;
1750 prev->right = prev->left + (title->bottom - title->top) ;
1751 next->right = title->right - 4;
1752 next->left = next->right - (title->bottom - title->top);
1754 /* titlemonth->left and right change based upon the current month */
1755 /* and are recalculated in refresh as the current month may change */
1756 /* without the control being resized */
1757 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
1758 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
1760 /* setup the dimensions of the rectangle we draw the names of the */
1761 /* days of the week in */
1762 weeknumrect->left = left_offset;
1763 if(dwStyle & MCS_WEEKNUMBERS)
1764 weeknumrect->right=prev->right;
1766 weeknumrect->right=weeknumrect->left;
1767 wdays->left = days->left = weeknumrect->right;
1768 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
1769 wdays->top = title->bottom ;
1770 wdays->bottom = wdays->top + infoPtr->height_increment;
1772 days->top = weeknumrect->top = wdays->bottom ;
1773 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
1775 todayrect->left = rcClient.left;
1776 todayrect->right = rcClient.right;
1777 todayrect->top = days->bottom;
1778 todayrect->bottom = days->bottom + infoPtr->height_increment;
1780 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
1781 infoPtr->width_increment,infoPtr->height_increment,
1782 wine_dbgstr_rect(&rcClient),
1783 wine_dbgstr_rect(title),
1784 wine_dbgstr_rect(wdays),
1785 wine_dbgstr_rect(days),
1786 wine_dbgstr_rect(todayrect));
1788 /* restore the originally selected font */
1789 SelectObject(hdc, currentFont);
1791 ReleaseDC(infoPtr->hwndSelf, hdc);
1794 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
1796 TRACE("(width=%d, height=%d)\n", Width, Height);
1798 MONTHCAL_UpdateSize(infoPtr);
1800 /* invalidate client area and erase background */
1801 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1806 static LRESULT MONTHCAL_GetFont(MONTHCAL_INFO *infoPtr)
1808 return (LRESULT)infoPtr->hFont;
1811 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
1816 if (!hFont) return 0;
1818 hOldFont = infoPtr->hFont;
1819 infoPtr->hFont = hFont;
1821 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
1822 lf.lfWeight = FW_BOLD;
1823 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
1826 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1828 return (LRESULT)hOldFont;
1831 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
1833 MONTHCAL_Create(HWND hwnd, WPARAM wParam, LPARAM lParam)
1835 MONTHCAL_INFO *infoPtr;
1837 /* allocate memory for info structure */
1838 infoPtr =(MONTHCAL_INFO*)Alloc(sizeof(MONTHCAL_INFO));
1839 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
1841 if(infoPtr == NULL) {
1842 ERR( "could not allocate info memory!\n");
1846 infoPtr->hwndSelf = hwnd;
1847 infoPtr->hwndNotify = ((LPCREATESTRUCTW)lParam)->hwndParent;
1849 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
1851 /* initialize info structure */
1852 /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
1854 GetSystemTime(&infoPtr->todaysDate);
1855 MONTHCAL_SetFirstDayOfWeek(infoPtr, (LPARAM)-1);
1856 infoPtr->currentMonth = infoPtr->todaysDate.wMonth;
1857 infoPtr->currentYear = infoPtr->todaysDate.wYear;
1858 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->minDate);
1859 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
1860 infoPtr->maxDate.wYear=2050;
1861 infoPtr->minDate.wYear=1950;
1862 infoPtr->maxSelCount = 7;
1863 infoPtr->monthRange = 3;
1864 infoPtr->monthdayState = Alloc
1865 (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1866 infoPtr->titlebk = GetSysColor(COLOR_ACTIVECAPTION);
1867 infoPtr->titletxt = GetSysColor(COLOR_WINDOW);
1868 infoPtr->monthbk = GetSysColor(COLOR_WINDOW);
1869 infoPtr->trailingtxt = GetSysColor(COLOR_GRAYTEXT);
1870 infoPtr->bk = GetSysColor(COLOR_WINDOW);
1871 infoPtr->txt = GetSysColor(COLOR_WINDOWTEXT);
1873 /* call MONTHCAL_UpdateSize to set all of the dimensions */
1874 /* of the control */
1875 MONTHCAL_UpdateSize(infoPtr);
1882 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
1884 /* free month calendar info data */
1885 if(infoPtr->monthdayState)
1886 Free(infoPtr->monthdayState);
1887 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
1893 static LRESULT WINAPI
1894 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1896 MONTHCAL_INFO *infoPtr;
1898 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hwnd, uMsg, wParam, lParam);
1900 infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1901 if (!infoPtr && (uMsg != WM_CREATE))
1902 return DefWindowProcA(hwnd, uMsg, wParam, lParam);
1906 return MONTHCAL_GetCurSel(infoPtr, lParam);
1909 return MONTHCAL_SetCurSel(infoPtr, lParam);
1911 case MCM_GETMAXSELCOUNT:
1912 return MONTHCAL_GetMaxSelCount(infoPtr);
1914 case MCM_SETMAXSELCOUNT:
1915 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
1917 case MCM_GETSELRANGE:
1918 return MONTHCAL_GetSelRange(infoPtr, lParam);
1920 case MCM_SETSELRANGE:
1921 return MONTHCAL_SetSelRange(infoPtr, lParam);
1923 case MCM_GETMONTHRANGE:
1924 return MONTHCAL_GetMonthRange(infoPtr);
1926 case MCM_SETDAYSTATE:
1927 return MONTHCAL_SetDayState(infoPtr, wParam, lParam);
1929 case MCM_GETMINREQRECT:
1930 return MONTHCAL_GetMinReqRect(infoPtr, lParam);
1933 return MONTHCAL_GetColor(infoPtr, wParam);
1936 return MONTHCAL_SetColor(infoPtr, wParam, lParam);
1939 return MONTHCAL_GetToday(infoPtr, lParam);
1942 return MONTHCAL_SetToday(infoPtr, lParam);
1945 return MONTHCAL_HitTest(infoPtr, lParam);
1947 case MCM_GETFIRSTDAYOFWEEK:
1948 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
1950 case MCM_SETFIRSTDAYOFWEEK:
1951 return MONTHCAL_SetFirstDayOfWeek(infoPtr, lParam);
1954 return MONTHCAL_GetRange(hwnd, wParam, lParam);
1957 return MONTHCAL_SetRange(infoPtr, wParam, lParam);
1959 case MCM_GETMONTHDELTA:
1960 return MONTHCAL_GetMonthDelta(infoPtr);
1962 case MCM_SETMONTHDELTA:
1963 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
1965 case MCM_GETMAXTODAYWIDTH:
1966 return MONTHCAL_GetMaxTodayWidth(infoPtr);
1969 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1972 return MONTHCAL_KillFocus(infoPtr);
1974 case WM_RBUTTONDOWN:
1975 return MONTHCAL_RButtonDown(infoPtr, lParam);
1977 case WM_LBUTTONDOWN:
1978 return MONTHCAL_LButtonDown(infoPtr, lParam);
1981 return MONTHCAL_MouseMove(infoPtr, wParam, lParam);
1984 return MONTHCAL_LButtonUp(infoPtr, lParam);
1987 return MONTHCAL_Paint(infoPtr, wParam);
1990 return MONTHCAL_SetFocus(infoPtr);
1993 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1996 return MONTHCAL_Create(hwnd, wParam, lParam);
1999 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2002 return MONTHCAL_GetFont(infoPtr);
2005 return MONTHCAL_Timer(infoPtr, wParam);
2008 return MONTHCAL_Destroy(infoPtr);
2011 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
2012 ERR( "unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
2013 return DefWindowProcA(hwnd, uMsg, wParam, lParam);
2019 MONTHCAL_Register(void)
2023 ZeroMemory(&wndClass, sizeof(WNDCLASSA));
2024 wndClass.style = CS_GLOBALCLASS;
2025 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2026 wndClass.cbClsExtra = 0;
2027 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2028 wndClass.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
2029 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2030 wndClass.lpszClassName = MONTHCAL_CLASSA;
2032 RegisterClassA(&wndClass);
2037 MONTHCAL_Unregister(void)
2039 UnregisterClassA(MONTHCAL_CLASSA, NULL);