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; a 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;
340 HPEN hNewPen, hOldPen = 0;
344 sprintf(buf, "%d", day);
346 /* No need to check styles: when selection is not valid, it is set to zero.
347 * 1<day<31, so evertyhing's OK.
350 MONTHCAL_CalcDayRect(infoPtr, &r, x, y);
352 if((day>=infoPtr->minSel.wDay) && (day<=infoPtr->maxSel.wDay)
353 && (month==infoPtr->currentMonth)) {
357 TRACE("%d %d %d\n",day, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
358 TRACE("%ld %ld %ld %ld\n", r.left, r.top, r.right, r.bottom);
359 oldCol = SetTextColor(hdc, infoPtr->monthbk);
360 oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
361 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
362 hrgn = CreateEllipticRgn(r.left, r.top, r.right, r.bottom);
363 FillRgn(hdc, hrgn, hbr);
365 /* FIXME: this may need to be changed now b/c of the other
366 drawing changes 11/3/99 CMM */
367 r2.left = r.left - 0.25 * infoPtr->textWidth;
369 r2.right = r.left + 0.5 * infoPtr->textWidth;
370 r2.bottom = r.bottom;
371 if(haveSelectedDay) FillRect(hdc, &r2, hbr);
372 haveSelectedDay = TRUE;
374 haveSelectedDay = FALSE;
377 /* need to add some code for multiple selections */
379 if((bold) &&(!haveBoldFont)) {
380 SelectObject(hdc, infoPtr->hBoldFont);
383 if((!bold) &&(haveBoldFont)) {
384 SelectObject(hdc, infoPtr->hFont);
385 haveBoldFont = FALSE;
388 if(haveSelectedDay) {
389 SetTextColor(hdc, oldCol);
390 SetBkColor(hdc, oldBk);
393 SetBkMode(hdc,TRANSPARENT);
394 DrawTextA(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
396 /* draw a rectangle around the currently selected days text */
397 if((day==infoPtr->curSelDay) && (month==infoPtr->currentMonth)) {
398 hNewPen = CreatePen(PS_ALTERNATE, 0, GetSysColor(COLOR_WINDOWTEXT) );
399 hbr = GetSysColorBrush(COLOR_WINDOWTEXT);
400 FrameRect(hdc, &r, hbr);
401 SelectObject(hdc, hOldPen);
406 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, PAINTSTRUCT* ps)
408 RECT *title=&infoPtr->title;
409 RECT *prev=&infoPtr->titlebtnprev;
410 RECT *next=&infoPtr->titlebtnnext;
411 RECT *titlemonth=&infoPtr->titlemonth;
412 RECT *titleyear=&infoPtr->titleyear;
416 int i, j, m, mask, day, firstDay, weeknum, weeknum1,prevMonth;
417 int textHeight = infoPtr->textHeight, textWidth = infoPtr->textWidth;
424 COLORREF oldTextColor, oldBkColor;
425 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
427 RECT rcDay; /* used in MONTHCAL_CalcDayRect() */
428 SYSTEMTIME localtime;
431 oldTextColor = SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
433 /* fill background */
434 hbr = CreateSolidBrush (infoPtr->bk);
435 FillRect(hdc, &ps->rcPaint, hbr);
439 if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
441 hbr = CreateSolidBrush(infoPtr->titlebk);
442 FillRect(hdc, title, hbr);
446 /* if the previous button is pressed draw it depressed */
447 if(IntersectRect(&rcTemp, &(ps->rcPaint), prev))
449 if((infoPtr->status & MC_PREVPRESSED))
450 DrawFrameControl(hdc, prev, DFC_SCROLL,
451 DFCS_SCROLLLEFT | DFCS_PUSHED |
452 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
453 else /* if the previous button is pressed draw it depressed */
454 DrawFrameControl(hdc, prev, DFC_SCROLL,
455 DFCS_SCROLLLEFT |(dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
458 /* if next button is depressed draw it depressed */
459 if(IntersectRect(&rcTemp, &(ps->rcPaint), next))
461 if((infoPtr->status & MC_NEXTPRESSED))
462 DrawFrameControl(hdc, next, DFC_SCROLL,
463 DFCS_SCROLLRIGHT | DFCS_PUSHED |
464 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
465 else /* if the next button is pressed draw it depressed */
466 DrawFrameControl(hdc, next, DFC_SCROLL,
467 DFCS_SCROLLRIGHT |(dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
470 oldBkColor = SetBkColor(hdc, infoPtr->titlebk);
471 SetTextColor(hdc, infoPtr->titletxt);
472 currentFont = SelectObject(hdc, infoPtr->hBoldFont);
474 /* titlemonth->left and right are set in MONTHCAL_UpdateSize */
475 titlemonth->left = title->left;
476 titlemonth->right = title->right;
478 GetLocaleInfoA( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+infoPtr->currentMonth -1,
480 sprintf(buf, "%s %ld", buf1, infoPtr->currentYear);
482 if(IntersectRect(&rcTemp, &(ps->rcPaint), titlemonth))
484 DrawTextA(hdc, buf, strlen(buf), titlemonth,
485 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
488 SelectObject(hdc, infoPtr->hFont);
490 /* titlemonth left/right contained rect for whole titletxt('June 1999')
491 * MCM_HitTestInfo wants month & year rects, so prepare these now.
492 *(no, we can't draw them separately; the whole text is centered)
494 GetTextExtentPoint32A(hdc, buf, strlen(buf), &size);
495 titlemonth->left = title->right / 2 - size.cx / 2;
496 titleyear->right = title->right / 2 + size.cx / 2;
497 GetTextExtentPoint32A(hdc, buf1, strlen(buf1), &size);
498 titlemonth->right = titlemonth->left + size.cx;
499 titleyear->left = titlemonth->right;
501 /* draw month area */
502 rcTemp.top=infoPtr->wdays.top;
503 rcTemp.left=infoPtr->wdays.left;
504 rcTemp.bottom=infoPtr->todayrect.bottom;
505 rcTemp.right =infoPtr->todayrect.right;
506 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcTemp))
508 hbr = CreateSolidBrush(infoPtr->monthbk);
509 FillRect(hdc, &rcTemp, hbr);
513 /* draw line under day abbreviatons */
515 MoveToEx(hdc, infoPtr->days.left + 3, title->bottom + textHeight + 1, NULL);
516 LineTo(hdc, infoPtr->days.right - 3, title->bottom + textHeight + 1);
518 prevMonth = infoPtr->currentMonth - 1;
519 if(prevMonth == 0) /* if currentMonth is january(1) prevMonth is */
520 prevMonth = 12; /* december(12) of the previous year */
522 infoPtr->wdays.left = infoPtr->days.left = infoPtr->weeknums.right;
523 /* draw day abbreviations */
525 SetBkColor(hdc, infoPtr->monthbk);
526 SetTextColor(hdc, infoPtr->trailingtxt);
528 /* copy this rect so we can change the values without changing */
529 /* the original version */
530 days->left = infoPtr->wdays.left;
531 days->right = days->left + infoPtr->width_increment;
532 days->top = infoPtr->wdays.top;
533 days->bottom = infoPtr->wdays.bottom;
535 i = infoPtr->firstDay;
538 GetLocaleInfoA( LOCALE_USER_DEFAULT,LOCALE_SABBREVDAYNAME1 + (i +j)%7,
540 DrawTextA(hdc, buf, strlen(buf), days,
541 DT_CENTER | DT_VCENTER | DT_SINGLELINE );
542 days->left+=infoPtr->width_increment;
543 days->right+=infoPtr->width_increment;
546 /* draw day numbers; first, the previous month */
548 firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear);
550 day = MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear) +
551 (infoPtr->firstDay + 7 - firstDay)%7 + 1;
552 if (day > MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear))
554 startofprescal = day;
559 while(day <= MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear)) {
560 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
561 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
563 MONTHCAL_DrawDay(infoPtr, hdc, day, prevMonth, i, 0,
564 infoPtr->monthdayState[m] & mask);
572 /* draw `current' month */
574 day = 1; /* start at the beginning of the current month */
576 infoPtr->firstDayplace = i;
577 SetTextColor(hdc, infoPtr->txt);
581 /* draw the first week of the current month */
583 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
584 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
587 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth, i, 0,
588 infoPtr->monthdayState[m] & mask);
590 if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
591 (day==infoPtr->todaysDate.wDay) &&
592 (infoPtr->currentYear == infoPtr->todaysDate.wYear)) {
593 if(!(dwStyle & MCS_NOTODAYCIRCLE))
594 MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->currentMonth);
603 j = 1; /* move to the 2nd week of the current month */
604 i = 0; /* move back to sunday */
605 while(day <= MONTHCAL_MonthLength(infoPtr->currentMonth, infoPtr->currentYear)) {
606 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
607 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
609 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth, i, j,
610 infoPtr->monthdayState[m] & mask);
612 if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
613 (day==infoPtr->todaysDate.wDay) &&
614 (infoPtr->currentYear == infoPtr->todaysDate.wYear))
615 if(!(dwStyle & MCS_NOTODAYCIRCLE))
616 MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->currentMonth);
621 if(i>6) { /* past saturday, goto the next weeks sunday */
627 /* draw `next' month */
629 day = 1; /* start at the first day of the next month */
633 SetTextColor(hdc, infoPtr->trailingtxt);
634 while((i<7) &&(j<6)) {
635 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
636 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
638 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth + 1, i, j,
639 infoPtr->monthdayState[m] & mask);
645 if(i==7) { /* past saturday, go to next week's sunday */
650 SetTextColor(hdc, infoPtr->txt);
653 /* draw `today' date if style allows it, and draw a circle before today's
654 * date if necessary */
656 if(!(dwStyle & MCS_NOTODAY)) {
658 if(!(dwStyle & MCS_NOTODAYCIRCLE)) {
659 /*day is the number of days from nextmonth we put on the calendar */
660 MONTHCAL_CircleDay(infoPtr, hdc,
661 day+MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear),
662 infoPtr->currentMonth);
665 if (!LoadStringA(COMCTL32_hModule,IDM_TODAY,buf1,sizeof(buf1)))
667 WARN("Can't load resource\n");
668 strcpy(buf1,"Today:");
670 MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
671 MONTHCAL_CopyTime(&infoPtr->todaysDate,&localtime);
672 GetDateFormatA(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&localtime,NULL,buf2,sizeof(buf2));
673 sprintf(buf, "%s %s", buf1,buf2);
674 SelectObject(hdc, infoPtr->hBoldFont);
676 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rtoday))
678 DrawTextA(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
679 DrawTextA(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
681 SelectObject(hdc, infoPtr->hFont);
684 /*eventually draw week numbers*/
685 if(dwStyle & MCS_WEEKNUMBERS) {
686 /* display weeknumbers*/
689 /* Rules what week to call the first week of a new year:
690 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
691 The week containing Jan 1 is the first week of year
692 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
693 First week of year must contain 4 days of the new year
694 LOCALE_IFIRSTWEEKOFYEAR == 1 (what contries?)
695 The first week of the year must contain only days of the new year
697 GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR,
699 sscanf(buf, "%d", &weeknum);
710 if (infoPtr->currentMonth < 2)
712 /* calculate all those exceptions for january */
713 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear);
714 if ((infoPtr->firstDay +7 - weeknum1)%7 > mindays)
720 weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->currentYear-1);
721 weeknum +=startofprescal+ 7;
723 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear-1);
724 if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
731 for(i=0; i<prevMonth-1; i++)
732 weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->currentYear);
733 weeknum +=startofprescal+ 7;
735 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear);
736 if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
739 days->left = infoPtr->weeknums.left;
740 days->right = infoPtr->weeknums.right;
741 days->top = infoPtr->weeknums.top;
742 days->bottom = days->top +infoPtr->height_increment;
744 if((i==0)&&(weeknum>50))
746 sprintf(buf, "%d", weeknum);
749 else if((i==5)&&(weeknum>47))
751 sprintf(buf, "%d", 1);
754 sprintf(buf, "%d", weeknum + i);
755 DrawTextA(hdc, buf, -1, days, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
756 days->top+=infoPtr->height_increment;
757 days->bottom+=infoPtr->height_increment;
760 MoveToEx(hdc, infoPtr->weeknums.right, infoPtr->weeknums.top + 3 , NULL);
761 LineTo(hdc, infoPtr->weeknums.right, infoPtr->weeknums.bottom );
764 /* currentFont was font at entering Refresh */
766 SetBkColor(hdc, oldBkColor);
767 SelectObject(hdc, currentFont);
768 SetTextColor(hdc, oldTextColor);
773 MONTHCAL_GetMinReqRect(MONTHCAL_INFO *infoPtr, LPARAM lParam)
775 LPRECT lpRect = (LPRECT) lParam;
777 TRACE("rect %p\n", lpRect);
779 /* validate parameters */
781 if((infoPtr==NULL) ||(lpRect == NULL) ) return FALSE;
783 lpRect->left = infoPtr->title.left;
784 lpRect->top = infoPtr->title.top;
785 lpRect->right = infoPtr->title.right;
786 lpRect->bottom = infoPtr->todayrect.bottom;
787 AdjustWindowRect(lpRect, GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE), FALSE);
789 TRACE("%s\n", wine_dbgstr_rect(lpRect));
796 MONTHCAL_GetColor(MONTHCAL_INFO *infoPtr, WPARAM wParam)
800 switch((int)wParam) {
801 case MCSC_BACKGROUND:
806 return infoPtr->titlebk;
808 return infoPtr->titletxt;
810 return infoPtr->monthbk;
811 case MCSC_TRAILINGTEXT:
812 return infoPtr->trailingtxt;
820 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
824 TRACE("%d: color %08lx\n", wParam, lParam);
826 switch((int)wParam) {
827 case MCSC_BACKGROUND:
829 infoPtr->bk = (COLORREF)lParam;
833 infoPtr->txt = (COLORREF)lParam;
836 prev = infoPtr->titlebk;
837 infoPtr->titlebk = (COLORREF)lParam;
840 prev=infoPtr->titletxt;
841 infoPtr->titletxt = (COLORREF)lParam;
844 prev = infoPtr->monthbk;
845 infoPtr->monthbk = (COLORREF)lParam;
847 case MCSC_TRAILINGTEXT:
848 prev = infoPtr->trailingtxt;
849 infoPtr->trailingtxt = (COLORREF)lParam;
853 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
859 MONTHCAL_GetMonthDelta(MONTHCAL_INFO *infoPtr)
864 return infoPtr->delta;
866 return infoPtr->visible;
871 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, WPARAM wParam)
873 int prev = infoPtr->delta;
875 TRACE("delta %d\n", wParam);
877 infoPtr->delta = (int)wParam;
883 MONTHCAL_GetFirstDayOfWeek(MONTHCAL_INFO *infoPtr)
885 return infoPtr->firstDay;
889 /* sets the first day of the week that will appear in the control */
890 /* 0 == Monday, 6 == Sunday */
891 /* FIXME: this needs to be implemented properly in MONTHCAL_Refresh() */
892 /* FIXME: we need more error checking here */
894 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, LPARAM lParam)
896 int prev = infoPtr->firstDay;
900 TRACE("day %ld\n", lParam);
902 if((lParam >= 0) && (lParam < 7)) {
903 infoPtr->firstDay = (int)lParam;
907 GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK,
909 TRACE("%s %d\n", buf, strlen(buf));
910 if(sscanf(buf, "%d", &day) == 1)
911 infoPtr->firstDay = day;
913 infoPtr->firstDay = 0;
920 MONTHCAL_GetMonthRange(MONTHCAL_INFO *infoPtr)
924 return infoPtr->monthRange;
929 MONTHCAL_GetMaxTodayWidth(MONTHCAL_INFO *infoPtr)
931 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
935 /* FIXME: are validated times taken from current date/time or simply
937 * FIXME: check whether MCM_GETMONTHRANGE shows correct result after
938 * adjusting range with MCM_SETRANGE
942 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
944 SYSTEMTIME *lprgSysTimeArray=(SYSTEMTIME *)lParam;
947 TRACE("%x %lx\n", wParam, lParam);
949 if(wParam & GDTR_MAX) {
950 if(MONTHCAL_ValidateTime(lprgSysTimeArray[1])){
951 MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxDate);
952 infoPtr->rangeValid|=GDTR_MAX;
954 GetSystemTime(&infoPtr->todaysDate);
955 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
958 if(wParam & GDTR_MIN) {
959 if(MONTHCAL_ValidateTime(lprgSysTimeArray[0])) {
960 MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minDate);
961 infoPtr->rangeValid|=GDTR_MIN;
963 GetSystemTime(&infoPtr->todaysDate);
964 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->minDate);
968 prev = infoPtr->monthRange;
969 infoPtr->monthRange = infoPtr->maxDate.wMonth - infoPtr->minDate.wMonth;
971 if(infoPtr->monthRange!=prev) {
972 infoPtr->monthdayState = ReAlloc(infoPtr->monthdayState,
973 infoPtr->monthRange * sizeof(MONTHDAYSTATE));
981 MONTHCAL_GetRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
983 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
984 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *)lParam;
986 /* validate parameters */
988 if((infoPtr==NULL) || (lprgSysTimeArray==NULL)) return FALSE;
990 MONTHCAL_CopyTime(&infoPtr->maxDate, &lprgSysTimeArray[1]);
991 MONTHCAL_CopyTime(&infoPtr->minDate, &lprgSysTimeArray[0]);
993 return infoPtr->rangeValid;
998 MONTHCAL_SetDayState(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1001 int i, iMonths = (int)wParam;
1002 MONTHDAYSTATE *dayStates = (LPMONTHDAYSTATE)lParam;
1004 TRACE("%x %lx\n", wParam, lParam);
1005 if(iMonths!=infoPtr->monthRange) return 0;
1007 for(i=0; i<iMonths; i++)
1008 infoPtr->monthdayState[i] = dayStates[i];
1013 MONTHCAL_GetCurSel(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1015 SYSTEMTIME *lpSel = (SYSTEMTIME *) lParam;
1017 TRACE("%lx\n", lParam);
1018 if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
1019 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
1021 MONTHCAL_CopyTime(&infoPtr->minSel, lpSel);
1022 TRACE("%d/%d/%d\n", lpSel->wYear, lpSel->wMonth, lpSel->wDay);
1026 /* FIXME: if the specified date is not visible, make it visible */
1027 /* FIXME: redraw? */
1029 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1031 SYSTEMTIME *lpSel = (SYSTEMTIME *)lParam;
1033 TRACE("%lx\n", lParam);
1034 if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
1035 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
1037 infoPtr->currentMonth=lpSel->wMonth;
1038 infoPtr->currentYear=lpSel->wYear;
1040 MONTHCAL_CopyTime(lpSel, &infoPtr->minSel);
1041 MONTHCAL_CopyTime(lpSel, &infoPtr->maxSel);
1043 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1050 MONTHCAL_GetMaxSelCount(MONTHCAL_INFO *infoPtr)
1052 return infoPtr->maxSelCount;
1057 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1059 TRACE("%x\n", wParam);
1061 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) {
1062 infoPtr->maxSelCount = wParam;
1070 MONTHCAL_GetSelRange(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1072 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1074 TRACE("%lx\n", lParam);
1076 /* validate parameters */
1078 if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1080 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)
1082 MONTHCAL_CopyTime(&infoPtr->maxSel, &lprgSysTimeArray[1]);
1083 MONTHCAL_CopyTime(&infoPtr->minSel, &lprgSysTimeArray[0]);
1084 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1093 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1095 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1097 TRACE("%lx\n", lParam);
1099 /* validate parameters */
1101 if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1103 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)
1105 MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxSel);
1106 MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minSel);
1107 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1116 MONTHCAL_GetToday(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1118 SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1120 TRACE("%lx\n", lParam);
1122 /* validate parameters */
1124 if((infoPtr==NULL) || (lpToday==NULL)) return FALSE;
1125 MONTHCAL_CopyTime(&infoPtr->todaysDate, lpToday);
1131 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1133 SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1135 TRACE("%lx\n", lParam);
1137 /* validate parameters */
1139 if((infoPtr==NULL) ||(lpToday==NULL)) return FALSE;
1140 MONTHCAL_CopyTime(lpToday, &infoPtr->todaysDate);
1141 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1147 MONTHCAL_HitTest(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1149 PMCHITTESTINFO lpht = (PMCHITTESTINFO)lParam;
1157 retval = MCHT_NOWHERE;
1160 /* Comment in for debugging...
1161 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,
1162 infoPtr->wdays.left, infoPtr->wdays.right,
1163 infoPtr->wdays.top, infoPtr->wdays.bottom,
1164 infoPtr->days.left, infoPtr->days.right,
1165 infoPtr->days.top, infoPtr->days.bottom,
1166 infoPtr->todayrect.left, infoPtr->todayrect.right,
1167 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1168 infoPtr->weeknums.left, infoPtr->weeknums.right,
1169 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1172 /* are we in the header? */
1174 if(PtInRect(&infoPtr->title, lpht->pt)) {
1175 if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1176 retval = MCHT_TITLEBTNPREV;
1179 if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1180 retval = MCHT_TITLEBTNNEXT;
1183 if(PtInRect(&infoPtr->titlemonth, lpht->pt)) {
1184 retval = MCHT_TITLEMONTH;
1187 if(PtInRect(&infoPtr->titleyear, lpht->pt)) {
1188 retval = MCHT_TITLEYEAR;
1192 retval = MCHT_TITLE;
1196 day = MONTHCAL_CalcDayFromPos(infoPtr,x,y,&wday,&wnum);
1197 if(PtInRect(&infoPtr->wdays, lpht->pt)) {
1198 retval = MCHT_CALENDARDAY;
1199 lpht->st.wYear = infoPtr->currentYear;
1200 lpht->st.wMonth = (day < 1)? infoPtr->currentMonth -1 : infoPtr->currentMonth;
1201 lpht->st.wDay = (day < 1)?
1202 MONTHCAL_MonthLength(infoPtr->currentMonth-1,infoPtr->currentYear) -day : day;
1205 if(PtInRect(&infoPtr->weeknums, lpht->pt)) {
1206 retval = MCHT_CALENDARWEEKNUM;
1207 lpht->st.wYear = infoPtr->currentYear;
1208 lpht->st.wMonth = (day < 1) ? infoPtr->currentMonth -1 :
1209 (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear)) ?
1210 infoPtr->currentMonth +1 :infoPtr->currentMonth;
1211 lpht->st.wDay = (day < 1 ) ?
1212 MONTHCAL_MonthLength(infoPtr->currentMonth-1,infoPtr->currentYear) -day :
1213 (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear)) ?
1214 day - MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear) : day;
1217 if(PtInRect(&infoPtr->days, lpht->pt))
1219 lpht->st.wYear = infoPtr->currentYear;
1222 retval = MCHT_CALENDARDATEPREV;
1223 lpht->st.wMonth = infoPtr->currentMonth - 1;
1224 if (lpht->st.wMonth <1)
1226 lpht->st.wMonth = 12;
1229 lpht->st.wDay = MONTHCAL_MonthLength(lpht->st.wMonth,lpht->st.wYear) -day;
1231 else if (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear))
1233 retval = MCHT_CALENDARDATENEXT;
1234 lpht->st.wMonth = infoPtr->currentMonth + 1;
1235 if (lpht->st.wMonth <12)
1237 lpht->st.wMonth = 1;
1240 lpht->st.wDay = day - MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear) ;
1243 retval = MCHT_CALENDARDATE;
1244 lpht->st.wMonth = infoPtr->currentMonth;
1245 lpht->st.wDay = day;
1249 if(PtInRect(&infoPtr->todayrect, lpht->pt)) {
1250 retval = MCHT_TODAYLINK;
1255 /* Hit nothing special? What's left must be background :-) */
1257 retval = MCHT_CALENDARBK;
1259 lpht->uHit = retval;
1264 static void MONTHCAL_GoToNextMonth(MONTHCAL_INFO *infoPtr)
1266 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1268 TRACE("MONTHCAL_GoToNextMonth\n");
1270 infoPtr->currentMonth++;
1271 if(infoPtr->currentMonth > 12) {
1272 infoPtr->currentYear++;
1273 infoPtr->currentMonth = 1;
1276 if(dwStyle & MCS_DAYSTATE) {
1280 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1281 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1282 nmds.nmhdr.code = MCN_GETDAYSTATE;
1283 nmds.cDayState = infoPtr->monthRange;
1284 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1286 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1287 (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
1288 for(i=0; i<infoPtr->monthRange; i++)
1289 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1294 static void MONTHCAL_GoToPrevMonth(MONTHCAL_INFO *infoPtr)
1296 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1300 infoPtr->currentMonth--;
1301 if(infoPtr->currentMonth < 1) {
1302 infoPtr->currentYear--;
1303 infoPtr->currentMonth = 12;
1306 if(dwStyle & MCS_DAYSTATE) {
1310 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1311 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1312 nmds.nmhdr.code = MCN_GETDAYSTATE;
1313 nmds.cDayState = infoPtr->monthRange;
1314 nmds.prgDayState = Alloc
1315 (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1317 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1318 (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
1319 for(i=0; i<infoPtr->monthRange; i++)
1320 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1325 MONTHCAL_RButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1331 hMenu = CreatePopupMenu();
1332 if (!LoadStringA(COMCTL32_hModule,IDM_GOTODAY,buf,sizeof(buf)))
1334 WARN("Can't load resource\n");
1335 strcpy(buf,"Go to Today:");
1337 AppendMenuA(hMenu, MF_STRING|MF_ENABLED,1, buf);
1338 menupoint.x=(INT)LOWORD(lParam);
1339 menupoint.y=(INT)HIWORD(lParam);
1340 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1341 if( TrackPopupMenu(hMenu,TPM_RIGHTBUTTON| TPM_NONOTIFY|TPM_RETURNCMD,
1342 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1344 infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
1345 infoPtr->currentYear=infoPtr->todaysDate.wYear;
1346 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1352 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1357 RECT rcDay; /* used in determining area to invalidate */
1362 TRACE("%lx\n", lParam);
1364 if (infoPtr->hWndYearUpDown)
1366 infoPtr->currentYear=SendMessageA( infoPtr->hWndYearUpDown, UDM_SETPOS, (WPARAM) 0,(LPARAM)0);
1367 if(!DestroyWindow(infoPtr->hWndYearUpDown))
1369 FIXME("Can't destroy Updown Control\n");
1372 infoPtr->hWndYearUpDown=0;
1373 if(!DestroyWindow(infoPtr->hWndYearEdit))
1375 FIXME("Can't destroy Updown Control\n");
1378 infoPtr->hWndYearEdit=0;
1379 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1382 ht.pt.x = (INT)LOWORD(lParam);
1383 ht.pt.y = (INT)HIWORD(lParam);
1384 hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1386 /* FIXME: these flags should be checked by */
1387 /*((hit & MCHT_XXX) == MCHT_XXX) b/c some of the flags are */
1389 if(hit ==MCHT_TITLEBTNNEXT) {
1390 MONTHCAL_GoToNextMonth(infoPtr);
1391 infoPtr->status = MC_NEXTPRESSED;
1392 SetTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1393 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1396 if(hit == MCHT_TITLEBTNPREV){
1397 MONTHCAL_GoToPrevMonth(infoPtr);
1398 infoPtr->status = MC_PREVPRESSED;
1399 SetTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1400 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1404 if(hit == MCHT_TITLEMONTH) {
1405 hMenu = CreatePopupMenu();
1409 GetLocaleInfoA( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+i,
1411 AppendMenuA(hMenu, MF_STRING|MF_ENABLED,i+1, buf);
1413 menupoint.x=infoPtr->titlemonth.right;
1414 menupoint.y=infoPtr->titlemonth.bottom;
1415 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1416 i= TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
1417 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
1418 if ((i>0) && (i<13))
1420 infoPtr->currentMonth=i;
1421 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1424 if(hit == MCHT_TITLEYEAR) {
1425 infoPtr->hWndYearEdit=CreateWindowExA(0,
1428 WS_VISIBLE | WS_CHILD |UDS_SETBUDDYINT,
1429 infoPtr->titleyear.left+3,infoPtr->titlebtnnext.top,
1430 infoPtr->titleyear.right-infoPtr->titleyear.left,
1431 infoPtr->textHeight,
1436 infoPtr->hWndYearUpDown=CreateWindowExA(0,
1439 WS_VISIBLE | WS_CHILD |UDS_SETBUDDYINT|UDS_NOTHOUSANDS|UDS_ARROWKEYS,
1440 infoPtr->titleyear.right+6,infoPtr->titlebtnnext.top,
1442 infoPtr->textHeight,
1447 SendMessageA( infoPtr->hWndYearUpDown, UDM_SETRANGE, (WPARAM) 0, MAKELONG (9999, 1753));
1448 SendMessageA( infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM) infoPtr->hWndYearEdit, (LPARAM)0 );
1449 SendMessageA( infoPtr->hWndYearUpDown, UDM_SETPOS, (WPARAM) 0,(LPARAM)infoPtr->currentYear );
1453 if(hit == MCHT_TODAYLINK) {
1454 infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
1455 infoPtr->currentYear=infoPtr->todaysDate.wYear;
1456 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1459 if(hit == MCHT_CALENDARDATE) {
1460 SYSTEMTIME selArray[2];
1463 MONTHCAL_CopyTime(&ht.st, &selArray[0]);
1464 MONTHCAL_CopyTime(&ht.st, &selArray[1]);
1465 MONTHCAL_SetSelRange(infoPtr, (LPARAM)&selArray);
1466 MONTHCAL_SetCurSel(infoPtr, (LPARAM)&selArray);
1467 TRACE("MCHT_CALENDARDATE\n");
1468 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1469 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1470 nmsc.nmhdr.code = MCN_SELCHANGE;
1471 MONTHCAL_CopyTime(&infoPtr->minSel,&nmsc.stSelStart);
1472 MONTHCAL_CopyTime(&infoPtr->maxSel,&nmsc.stSelEnd);
1474 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1475 (WPARAM)nmsc.nmhdr.idFrom,(LPARAM)&nmsc);
1478 /* redraw both old and new days if the selected day changed */
1479 if(infoPtr->curSelDay != ht.st.wDay) {
1480 MONTHCAL_CalcPosFromDay(infoPtr, ht.st.wDay, ht.st.wMonth, &rcDay);
1481 InvalidateRect(infoPtr->hwndSelf, &rcDay, TRUE);
1483 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->curSelDay, infoPtr->currentMonth, &rcDay);
1484 InvalidateRect(infoPtr->hwndSelf, &rcDay, TRUE);
1487 infoPtr->firstSelDay = ht.st.wDay;
1488 infoPtr->curSelDay = ht.st.wDay;
1489 infoPtr->status = MC_SEL_LBUTDOWN;
1498 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1502 BOOL redraw = FALSE;
1508 if(infoPtr->status & MC_NEXTPRESSED) {
1509 KillTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER);
1510 infoPtr->status &= ~MC_NEXTPRESSED;
1513 if(infoPtr->status & MC_PREVPRESSED) {
1514 KillTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER);
1515 infoPtr->status &= ~MC_PREVPRESSED;
1519 ht.pt.x = (INT)LOWORD(lParam);
1520 ht.pt.y = (INT)HIWORD(lParam);
1521 hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1523 infoPtr->status = MC_SEL_LBUTUP;
1525 if(hit ==MCHT_CALENDARDATENEXT) {
1526 MONTHCAL_GoToNextMonth(infoPtr);
1527 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1530 if(hit == MCHT_CALENDARDATEPREV){
1531 MONTHCAL_GoToPrevMonth(infoPtr);
1532 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1535 nmhdr.hwndFrom = infoPtr->hwndSelf;
1536 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1537 nmhdr.code = NM_RELEASEDCAPTURE;
1538 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
1540 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1541 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
1542 /* redraw if necessary */
1544 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1545 /* only send MCN_SELECT if currently displayed month's day was selected */
1546 if(hit == MCHT_CALENDARDATE) {
1547 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1548 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1549 nmsc.nmhdr.code = MCN_SELECT;
1550 MONTHCAL_CopyTime(&infoPtr->minSel, &nmsc.stSelStart);
1551 MONTHCAL_CopyTime(&infoPtr->maxSel, &nmsc.stSelEnd);
1553 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1554 (WPARAM)nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1562 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1564 BOOL redraw = FALSE;
1566 TRACE("%d\n", wParam);
1569 case MC_NEXTMONTHTIMER:
1571 MONTHCAL_GoToNextMonth(infoPtr);
1573 case MC_PREVMONTHTIMER:
1575 MONTHCAL_GoToPrevMonth(infoPtr);
1578 ERR("got unknown timer\n");
1582 /* redraw only if necessary */
1584 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1591 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1594 int oldselday, selday, hit;
1597 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
1599 ht.pt.x = LOWORD(lParam);
1600 ht.pt.y = HIWORD(lParam);
1602 hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1604 /* not on the calendar date numbers? bail out */
1605 TRACE("hit:%x\n",hit);
1606 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE) return 0;
1608 selday = ht.st.wDay;
1609 oldselday = infoPtr->curSelDay;
1610 infoPtr->curSelDay = selday;
1611 MONTHCAL_CalcPosFromDay(infoPtr, selday, ht.st. wMonth, &r);
1613 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) {
1614 SYSTEMTIME selArray[2];
1617 MONTHCAL_GetSelRange(infoPtr, (LPARAM)&selArray);
1619 if(infoPtr->firstSelDay==selArray[0].wDay) i=1;
1620 TRACE("oldRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1621 if(infoPtr->firstSelDay==selArray[1].wDay) {
1622 /* 1st time we get here: selArray[0]=selArray[1]) */
1623 /* if we're still at the first selected date, return */
1624 if(infoPtr->firstSelDay==selday) goto done;
1625 if(selday<infoPtr->firstSelDay) i = 0;
1628 if(abs(infoPtr->firstSelDay - selday) >= infoPtr->maxSelCount) {
1629 if(selday>infoPtr->firstSelDay)
1630 selday = infoPtr->firstSelDay + infoPtr->maxSelCount;
1632 selday = infoPtr->firstSelDay - infoPtr->maxSelCount;
1635 if(selArray[i].wDay!=selday) {
1636 TRACE("newRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1638 selArray[i].wDay = selday;
1640 if(selArray[0].wDay>selArray[1].wDay) {
1642 tempday = selArray[1].wDay;
1643 selArray[1].wDay = selArray[0].wDay;
1644 selArray[0].wDay = tempday;
1647 MONTHCAL_SetSelRange(infoPtr, (LPARAM)&selArray);
1653 /* only redraw if the currently selected day changed */
1654 /* FIXME: this should specify a rectangle containing only the days that changed */
1655 /* using InvalidateRect */
1656 if(oldselday != infoPtr->curSelDay)
1657 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1664 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1671 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
1675 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
1677 MONTHCAL_Refresh(infoPtr, hdc, &ps);
1678 if (!wParam) EndPaint(infoPtr->hwndSelf, &ps);
1684 MONTHCAL_KillFocus(MONTHCAL_INFO *infoPtr)
1688 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1695 MONTHCAL_SetFocus(MONTHCAL_INFO *infoPtr)
1699 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1704 /* sets the size information */
1705 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
1707 HDC hdc = GetDC(infoPtr->hwndSelf);
1708 RECT *title=&infoPtr->title;
1709 RECT *prev=&infoPtr->titlebtnprev;
1710 RECT *next=&infoPtr->titlebtnnext;
1711 RECT *titlemonth=&infoPtr->titlemonth;
1712 RECT *titleyear=&infoPtr->titleyear;
1713 RECT *wdays=&infoPtr->wdays;
1714 RECT *weeknumrect=&infoPtr->weeknums;
1715 RECT *days=&infoPtr->days;
1716 RECT *todayrect=&infoPtr->todayrect;
1719 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1721 int xdiv, left_offset;
1724 GetClientRect(infoPtr->hwndSelf, &rcClient);
1726 currentFont = SelectObject(hdc, infoPtr->hFont);
1728 /* get the height and width of each day's text */
1729 GetTextMetricsA(hdc, &tm);
1730 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
1731 GetTextExtentPoint32A(hdc, "Sun", 3, &size);
1732 infoPtr->textWidth = size.cx + 2;
1734 /* recalculate the height and width increments and offsets */
1735 GetTextExtentPoint32A(hdc, "00", 2, &size);
1737 xdiv = (dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
1739 infoPtr->width_increment = size.cx * 2;
1740 infoPtr->height_increment = infoPtr->textHeight;
1741 left_offset = (rcClient.right - rcClient.left) - (infoPtr->width_increment * xdiv);
1743 /* calculate title area */
1744 title->top = rcClient.top;
1745 title->bottom = title->top + 3 * infoPtr->height_increment / 2;
1746 title->left = left_offset;
1747 title->right = rcClient.right;
1749 /* set the dimensions of the next and previous buttons and center */
1750 /* the month text vertically */
1751 prev->top = next->top = title->top + 4;
1752 prev->bottom = next->bottom = title->bottom - 4;
1753 prev->left = title->left + 4;
1754 prev->right = prev->left + (title->bottom - title->top) ;
1755 next->right = title->right - 4;
1756 next->left = next->right - (title->bottom - title->top);
1758 /* titlemonth->left and right change based upon the current month */
1759 /* and are recalculated in refresh as the current month may change */
1760 /* without the control being resized */
1761 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
1762 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
1764 /* setup the dimensions of the rectangle we draw the names of the */
1765 /* days of the week in */
1766 weeknumrect->left = left_offset;
1767 if(dwStyle & MCS_WEEKNUMBERS)
1768 weeknumrect->right=prev->right;
1770 weeknumrect->right=weeknumrect->left;
1771 wdays->left = days->left = weeknumrect->right;
1772 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
1773 wdays->top = title->bottom ;
1774 wdays->bottom = wdays->top + infoPtr->height_increment;
1776 days->top = weeknumrect->top = wdays->bottom ;
1777 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
1779 todayrect->left = rcClient.left;
1780 todayrect->right = rcClient.right;
1781 todayrect->top = days->bottom;
1782 todayrect->bottom = days->bottom + infoPtr->height_increment;
1784 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
1785 infoPtr->width_increment,infoPtr->height_increment,
1786 wine_dbgstr_rect(&rcClient),
1787 wine_dbgstr_rect(title),
1788 wine_dbgstr_rect(wdays),
1789 wine_dbgstr_rect(days),
1790 wine_dbgstr_rect(todayrect));
1792 /* restore the originally selected font */
1793 SelectObject(hdc, currentFont);
1795 ReleaseDC(infoPtr->hwndSelf, hdc);
1798 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
1800 TRACE("(width=%d, height=%d)\n", Width, Height);
1802 MONTHCAL_UpdateSize(infoPtr);
1804 /* invalidate client area and erase background */
1805 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1810 static LRESULT MONTHCAL_GetFont(MONTHCAL_INFO *infoPtr)
1812 return (LRESULT)infoPtr->hFont;
1815 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
1820 if (!hFont) return 0;
1822 hOldFont = infoPtr->hFont;
1823 infoPtr->hFont = hFont;
1825 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
1826 lf.lfWeight = FW_BOLD;
1827 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
1830 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1832 return (LRESULT)hOldFont;
1835 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
1837 MONTHCAL_Create(HWND hwnd, WPARAM wParam, LPARAM lParam)
1839 MONTHCAL_INFO *infoPtr;
1841 /* allocate memory for info structure */
1842 infoPtr =(MONTHCAL_INFO*)Alloc(sizeof(MONTHCAL_INFO));
1843 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
1845 if(infoPtr == NULL) {
1846 ERR( "could not allocate info memory!\n");
1850 infoPtr->hwndSelf = hwnd;
1851 infoPtr->hwndNotify = ((LPCREATESTRUCTW)lParam)->hwndParent;
1853 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
1855 /* initialize info structure */
1856 /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
1858 GetSystemTime(&infoPtr->todaysDate);
1859 MONTHCAL_SetFirstDayOfWeek(infoPtr, (LPARAM)-1);
1860 infoPtr->currentMonth = infoPtr->todaysDate.wMonth;
1861 infoPtr->currentYear = infoPtr->todaysDate.wYear;
1862 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->minDate);
1863 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
1864 infoPtr->maxDate.wYear=2050;
1865 infoPtr->minDate.wYear=1950;
1866 infoPtr->maxSelCount = 7;
1867 infoPtr->monthRange = 3;
1868 infoPtr->monthdayState = Alloc
1869 (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1870 infoPtr->titlebk = GetSysColor(COLOR_ACTIVECAPTION);
1871 infoPtr->titletxt = GetSysColor(COLOR_WINDOW);
1872 infoPtr->monthbk = GetSysColor(COLOR_WINDOW);
1873 infoPtr->trailingtxt = GetSysColor(COLOR_GRAYTEXT);
1874 infoPtr->bk = GetSysColor(COLOR_WINDOW);
1875 infoPtr->txt = GetSysColor(COLOR_WINDOWTEXT);
1877 /* call MONTHCAL_UpdateSize to set all of the dimensions */
1878 /* of the control */
1879 MONTHCAL_UpdateSize(infoPtr);
1886 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
1888 /* free month calendar info data */
1889 if(infoPtr->monthdayState)
1890 Free(infoPtr->monthdayState);
1891 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
1897 static LRESULT WINAPI
1898 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1900 MONTHCAL_INFO *infoPtr;
1902 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hwnd, uMsg, wParam, lParam);
1904 infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1905 if (!infoPtr && (uMsg != WM_CREATE))
1906 return DefWindowProcA(hwnd, uMsg, wParam, lParam);
1910 return MONTHCAL_GetCurSel(infoPtr, lParam);
1913 return MONTHCAL_SetCurSel(infoPtr, lParam);
1915 case MCM_GETMAXSELCOUNT:
1916 return MONTHCAL_GetMaxSelCount(infoPtr);
1918 case MCM_SETMAXSELCOUNT:
1919 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
1921 case MCM_GETSELRANGE:
1922 return MONTHCAL_GetSelRange(infoPtr, lParam);
1924 case MCM_SETSELRANGE:
1925 return MONTHCAL_SetSelRange(infoPtr, lParam);
1927 case MCM_GETMONTHRANGE:
1928 return MONTHCAL_GetMonthRange(infoPtr);
1930 case MCM_SETDAYSTATE:
1931 return MONTHCAL_SetDayState(infoPtr, wParam, lParam);
1933 case MCM_GETMINREQRECT:
1934 return MONTHCAL_GetMinReqRect(infoPtr, lParam);
1937 return MONTHCAL_GetColor(infoPtr, wParam);
1940 return MONTHCAL_SetColor(infoPtr, wParam, lParam);
1943 return MONTHCAL_GetToday(infoPtr, lParam);
1946 return MONTHCAL_SetToday(infoPtr, lParam);
1949 return MONTHCAL_HitTest(infoPtr, lParam);
1951 case MCM_GETFIRSTDAYOFWEEK:
1952 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
1954 case MCM_SETFIRSTDAYOFWEEK:
1955 return MONTHCAL_SetFirstDayOfWeek(infoPtr, lParam);
1958 return MONTHCAL_GetRange(hwnd, wParam, lParam);
1961 return MONTHCAL_SetRange(infoPtr, wParam, lParam);
1963 case MCM_GETMONTHDELTA:
1964 return MONTHCAL_GetMonthDelta(infoPtr);
1966 case MCM_SETMONTHDELTA:
1967 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
1969 case MCM_GETMAXTODAYWIDTH:
1970 return MONTHCAL_GetMaxTodayWidth(infoPtr);
1973 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1976 return MONTHCAL_KillFocus(infoPtr);
1978 case WM_RBUTTONDOWN:
1979 return MONTHCAL_RButtonDown(infoPtr, lParam);
1981 case WM_LBUTTONDOWN:
1982 return MONTHCAL_LButtonDown(infoPtr, lParam);
1985 return MONTHCAL_MouseMove(infoPtr, wParam, lParam);
1988 return MONTHCAL_LButtonUp(infoPtr, lParam);
1991 return MONTHCAL_Paint(infoPtr, wParam);
1994 return MONTHCAL_SetFocus(infoPtr);
1997 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2000 return MONTHCAL_Create(hwnd, wParam, lParam);
2003 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2006 return MONTHCAL_GetFont(infoPtr);
2009 return MONTHCAL_Timer(infoPtr, wParam);
2012 return MONTHCAL_Destroy(infoPtr);
2015 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
2016 ERR( "unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
2017 return DefWindowProcA(hwnd, uMsg, wParam, lParam);
2024 MONTHCAL_Register(void)
2028 ZeroMemory(&wndClass, sizeof(WNDCLASSA));
2029 wndClass.style = CS_GLOBALCLASS;
2030 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2031 wndClass.cbClsExtra = 0;
2032 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2033 wndClass.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
2034 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2035 wndClass.lpszClassName = MONTHCAL_CLASSA;
2037 RegisterClassA(&wndClass);
2042 MONTHCAL_Unregister(void)
2044 UnregisterClassA(MONTHCAL_CLASSA, NULL);