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>
13 * FIXME: handle resources better (doesn't work now); also take care
14 of internationalization.
15 * FIXME: keyboard handling.
29 #include "debugtools.h"
31 DEFAULT_DEBUG_CHANNEL(monthcal);
33 #define MC_SEL_LBUTUP 1 /* Left button released */
34 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
35 #define MC_PREVPRESSED 4 /* Prev month button pressed */
36 #define MC_NEXTPRESSED 8 /* Next month button pressed */
37 #define MC_NEXTMONTHDELAY 350 /* when continuously pressing `next */
38 /* month', wait 500 ms before going */
39 /* to the next month */
40 #define MC_NEXTMONTHTIMER 1 /* Timer ID's */
41 #define MC_PREVMONTHTIMER 2
59 int firstDayplace; /* place of the first day of the current month */
60 int delta; /* scroll rate; # of months that the */
61 /* control moves when user clicks a scroll button */
62 int visible; /* # of months visible */
63 int firstDay; /* Start month calendar with firstDay's day */
65 MONTHDAYSTATE *monthdayState;
66 SYSTEMTIME todaysDate;
69 int status; /* See MC_SEL flags */
70 int curSelDay; /* current selected day */
71 int firstSelDay; /* first selected day */
79 RECT rcClient; /* rect for whole client area */
80 RECT rcDraw; /* rect for drawable portion of client area */
81 RECT title; /* rect for the header above the calendar */
82 RECT titlebtnnext; /* the `next month' button in the header */
83 RECT titlebtnprev; /* the `prev month' button in the header */
84 RECT titlemonth; /* the `month name' txt in the header */
85 RECT titleyear; /* the `year number' txt in the header */
86 RECT prevmonth; /* day numbers of the previous month */
87 RECT nextmonth; /* day numbers of the next month */
88 RECT days; /* week numbers at left side */
89 RECT weeknums; /* week numbers at left side */
90 RECT today; /* `today: xx/xx/xx' text rect */
91 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
94 /* take #days/month from ole/parsedt.c;
95 * we want full month-names, and abbreviated weekdays, so these are
98 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0};
100 const char * const monthtxt[] = {"January", "February", "March", "April", "May",
101 "June", "July", "August", "September", "October",
102 "November", "December"};
103 static const char * const daytxt[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
104 static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
107 #define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongA(hwnd, 0))
109 /* helper functions */
111 /* returns the number of days in any given month */
112 /* january is 1, december is 12 */
113 static int MONTHCAL_MonthLength(int month, int year)
115 /* if we have a leap year add 1 day to February */
116 /* a leap year is a year either divisible by 400 */
117 /* or divisible by 4 and not by 100 */
118 if(month == 2) { /* February */
119 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
120 (year%4 == 0)) ? 1 : 0);
123 return mdays[month - 1];
128 /* make sure that time is valid */
129 static int MONTHCAL_ValidateTime(SYSTEMTIME time)
131 if(time.wMonth > 12) return FALSE;
132 if(time.wDayOfWeek > 6) return FALSE;
133 if(time.wDay > MONTHCAL_MonthLength(time.wMonth, time.wYear))
135 if(time.wHour > 23) return FALSE;
136 if(time.wMinute > 59) return FALSE;
137 if(time.wSecond > 59) return FALSE;
138 if(time.wMilliseconds > 999) return FALSE;
144 void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
146 to->wYear = from->wYear;
147 to->wMonth = from->wMonth;
148 to->wDayOfWeek = from->wDayOfWeek;
149 to->wDay = from->wDay;
150 to->wHour = from->wHour;
151 to->wMinute = from->wMinute;
152 to->wSecond = from->wSecond;
153 to->wMilliseconds = from->wMilliseconds;
157 /* Note:Depending on DST, this may be offset by a day.
158 Need to find out if we're on a DST place & adjust the clock accordingly.
159 Above function assumes we have a valid data.
160 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
164 /* returns the day in the week(0 == sunday, 6 == saturday) */
165 /* day(1 == 1st, 2 == 2nd... etc), year is the year value */
166 static int MONTHCAL_CalculateDayOfWeek(DWORD day, DWORD month, DWORD year)
170 return((year + year/4 - year/100 + year/400 +
171 DayOfWeekTable[month-1] + day - 1 ) % 7);
175 static int MONTHCAL_CalcDayFromPos(MONTHCAL_INFO *infoPtr, int x, int y)
177 int daypos, weekpos, retval, firstDay;
179 /* if the point is outside the x bounds of the window put
181 if(x > (infoPtr->width_increment * 7.0)) {
182 x = infoPtr->rcClient.right - infoPtr->rcClient.left - infoPtr->left_offset;
185 daypos = (x -(infoPtr->prevmonth.left + infoPtr->left_offset)) / infoPtr->width_increment;
186 weekpos = (y - infoPtr->days.bottom - infoPtr->rcClient.top) / infoPtr->height_increment;
188 firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear);
189 retval = daypos + (7 * weekpos) - firstDay;
190 TRACE("%d %d %d\n", daypos, weekpos, retval);
194 /* day is the day of the month, 1 == 1st day of the month */
195 /* sets x and y to be the position of the day */
196 /* x == day, y == week where(0,0) == sunday, 1st week */
197 static void MONTHCAL_CalcDayXY(MONTHCAL_INFO *infoPtr, int day, int month,
200 int firstDay, prevMonth;
202 firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear);
204 if(month==infoPtr->currentMonth) {
205 *x = (day + firstDay) % 7;
206 *y = (day + firstDay - *x) / 7;
209 if(month < infoPtr->currentMonth) {
210 prevMonth = month - 1;
214 *x = (MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear) - firstDay) % 7;
219 *y = MONTHCAL_MonthLength(month, infoPtr->currentYear - 1) / 7;
220 *x = (day + firstDay + MONTHCAL_MonthLength(month,
221 infoPtr->currentYear)) % 7;
225 /* x: column(day), y: row(week) */
226 static void MONTHCAL_CalcDayRect(MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
228 r->left = infoPtr->prevmonth.left + x * infoPtr->width_increment + infoPtr->left_offset;
229 r->right = r->left + infoPtr->width_increment;
230 r->top = infoPtr->height_increment * y + infoPtr->days.bottom + infoPtr->top_offset;
231 r->bottom = r->top + infoPtr->textHeight;
235 /* sets the RECT struct r to the rectangle around the day and month */
236 /* day is the day value of the month(1 == 1st), month is the month */
237 /* value(january == 1, december == 12) */
238 static inline void MONTHCAL_CalcPosFromDay(MONTHCAL_INFO *infoPtr,
239 int day, int month, RECT *r)
243 MONTHCAL_CalcDayXY(infoPtr, day, month, &x, &y);
244 MONTHCAL_CalcDayRect(infoPtr, r, x, y);
248 /* day is the day in the month(1 == 1st of the month) */
249 /* month is the month value(1 == january, 12 == december) */
250 static void MONTHCAL_CircleDay(HDC hdc, MONTHCAL_INFO *infoPtr, int day,
253 HPEN hRedPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
254 HPEN hOldPen2 = SelectObject(hdc, hRedPen);
259 /* use prevmonth to calculate position because it contains the extra width
260 * from MCS_WEEKNUMBERS
263 MONTHCAL_CalcPosFromDay(infoPtr, day, month, &day_rect);
270 points[1].x = x + 0.8 * infoPtr->width_increment;
272 points[2].x = x + 0.9 * infoPtr->width_increment;
274 points[3].x = x + infoPtr->width_increment;
275 points[3].y = y + 0.5 * infoPtr->textHeight;
277 points[4].x = x + infoPtr->width_increment;
278 points[4].y = y + 0.9 * infoPtr->textHeight;
279 points[5].x = x + 0.6 * infoPtr->width_increment;
280 points[5].y = y + 0.9 * infoPtr->textHeight;
281 points[6].x = x + 0.5 * infoPtr->width_increment;
282 points[6].y = y + 0.9 * infoPtr->textHeight; /* bring the bottom up just
283 a hair to fit inside the day rectangle */
285 points[7].x = x + 0.2 * infoPtr->width_increment;
286 points[7].y = y + 0.8 * infoPtr->textHeight;
287 points[8].x = x + 0.1 * infoPtr->width_increment;
288 points[8].y = y + 0.8 * infoPtr->textHeight;
290 points[9].y = y + 0.5 * infoPtr->textHeight;
292 points[10].x = x + 0.1 * infoPtr->width_increment;
293 points[10].y = y + 0.2 * infoPtr->textHeight;
294 points[11].x = x + 0.2 * infoPtr->width_increment;
295 points[11].y = y + 0.3 * infoPtr->textHeight;
296 points[12].x = x + 0.5 * infoPtr->width_increment;
297 points[12].y = y + 0.3 * infoPtr->textHeight;
299 PolyBezier(hdc, points, 13);
300 DeleteObject(hRedPen);
301 SelectObject(hdc, hOldPen2);
305 static void MONTHCAL_DrawDay(HDC hdc, MONTHCAL_INFO *infoPtr, int day, int month,
306 int x, int y, int bold)
310 static int haveBoldFont, haveSelectedDay = FALSE;
312 HPEN hNewPen, hOldPen = 0;
316 sprintf(buf, "%d", day);
318 /* No need to check styles: when selection is not valid, it is set to zero.
319 * 1<day<31, so evertyhing's OK.
322 MONTHCAL_CalcDayRect(infoPtr, &r, x, y);
324 if((day>=infoPtr->minSel.wDay) && (day<=infoPtr->maxSel.wDay)
325 && (month==infoPtr->currentMonth)) {
329 TRACE("%d %d %d\n",day, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
330 TRACE("%d %d %d %d\n", r.left, r.top, r.right, r.bottom);
331 oldCol = SetTextColor(hdc, infoPtr->monthbk);
332 oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
333 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
334 hrgn = CreateEllipticRgn(r.left, r.top, r.right, r.bottom);
335 FillRgn(hdc, hrgn, hbr);
337 /* FIXME: this may need to be changed now b/c of the other
338 drawing changes 11/3/99 CMM */
339 r2.left = r.left - 0.25 * infoPtr->textWidth;
341 r2.right = r.left + 0.5 * infoPtr->textWidth;
342 r2.bottom = r.bottom;
343 if(haveSelectedDay) FillRect(hdc, &r2, hbr);
344 haveSelectedDay = TRUE;
346 haveSelectedDay = FALSE;
349 /* need to add some code for multiple selections */
351 if((bold) &&(!haveBoldFont)) {
352 SelectObject(hdc, infoPtr->hBoldFont);
355 if((!bold) &&(haveBoldFont)) {
356 SelectObject(hdc, infoPtr->hFont);
357 haveBoldFont = FALSE;
360 if(haveSelectedDay) {
361 SetTextColor(hdc, oldCol);
362 SetBkColor(hdc, oldBk);
365 DrawTextA(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
367 /* draw a rectangle around the currently selected days text */
368 if((day==infoPtr->curSelDay) && (month==infoPtr->currentMonth)) {
369 hNewPen = CreatePen(PS_DOT, 0, GetSysColor(COLOR_WINDOWTEXT) );
370 hbr = GetSysColorBrush(COLOR_WINDOWTEXT);
371 FrameRect(hdc, &r, hbr);
372 SelectObject(hdc, hOldPen);
377 /* CHECKME: For `todays date', do we need to check the locale?*/
378 static void MONTHCAL_Refresh(HWND hwnd, HDC hdc, PAINTSTRUCT* ps)
380 MONTHCAL_INFO *infoPtr=MONTHCAL_GetInfoPtr(hwnd);
381 RECT *rcClient=&infoPtr->rcClient;
382 RECT *rcDraw=&infoPtr->rcDraw;
383 RECT *title=&infoPtr->title;
384 RECT *prev=&infoPtr->titlebtnprev;
385 RECT *next=&infoPtr->titlebtnnext;
386 RECT *titlemonth=&infoPtr->titlemonth;
387 RECT *titleyear=&infoPtr->titleyear;
388 RECT *prevmonth=&infoPtr->prevmonth;
389 RECT *nextmonth=&infoPtr->nextmonth;
392 RECT *weeknums=&infoPtr->weeknums;
393 RECT *rtoday=&infoPtr->today;
394 int i, j, m, mask, day, firstDay, weeknum, prevMonth;
395 int textHeight = infoPtr->textHeight, textWidth = infoPtr->textWidth;
399 /* LOGFONTA logFont; */
401 const char *thisMonthtxt;
402 COLORREF oldTextColor, oldBkColor;
403 DWORD dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
405 RECT rcDay; /* used in MONTHCAL_CalcDayRect() */
407 oldTextColor = SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
410 /* fill background */
411 hbr = CreateSolidBrush (infoPtr->bk);
412 FillRect(hdc, rcClient, hbr);
416 if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
418 hbr = CreateSolidBrush(infoPtr->titlebk);
419 FillRect(hdc, title, hbr);
423 /* if the previous button is pressed draw it depressed */
424 if(IntersectRect(&rcTemp, &(ps->rcPaint), prev))
426 if((infoPtr->status & MC_PREVPRESSED))
427 DrawFrameControl(hdc, prev, DFC_SCROLL,
428 DFCS_SCROLLLEFT | DFCS_PUSHED |
429 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
430 else /* if the previous button is pressed draw it depressed */
431 DrawFrameControl(hdc, prev, DFC_SCROLL,
432 DFCS_SCROLLLEFT |(dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
435 /* if next button is depressed draw it depressed */
436 if(IntersectRect(&rcTemp, &(ps->rcPaint), next))
438 if((infoPtr->status & MC_NEXTPRESSED))
439 DrawFrameControl(hdc, next, DFC_SCROLL,
440 DFCS_SCROLLRIGHT | DFCS_PUSHED |
441 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
442 else /* if the next button is pressed draw it depressed */
443 DrawFrameControl(hdc, next, DFC_SCROLL,
444 DFCS_SCROLLRIGHT |(dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
447 oldBkColor = SetBkColor(hdc, infoPtr->titlebk);
448 SetTextColor(hdc, infoPtr->titletxt);
449 currentFont = SelectObject(hdc, infoPtr->hBoldFont);
451 /* titlemonth->left and right are set in MONTHCAL_UpdateSize */
452 titlemonth->left = title->left;
453 titlemonth->right = title->right;
455 thisMonthtxt = monthtxt[infoPtr->currentMonth - 1];
456 sprintf(buf, "%s %ld", thisMonthtxt, infoPtr->currentYear);
458 if(IntersectRect(&rcTemp, &(ps->rcPaint), titlemonth))
460 DrawTextA(hdc, buf, strlen(buf), titlemonth,
461 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
464 SelectObject(hdc, infoPtr->hFont);
466 /* titlemonth left/right contained rect for whole titletxt('June 1999')
467 * MCM_HitTestInfo wants month & year rects, so prepare these now.
468 *(no, we can't draw them separately; the whole text is centered)
470 GetTextExtentPoint32A(hdc, buf, strlen(buf), &size);
471 titlemonth->left = title->right / 2 - size.cx / 2;
472 titleyear->right = title->right / 2 + size.cx / 2;
473 GetTextExtentPoint32A(hdc, thisMonthtxt, strlen(thisMonthtxt), &size);
474 titlemonth->right = titlemonth->left + size.cx;
475 titleyear->right = titlemonth->right;
477 /* draw line under day abbreviatons */
479 if(dwStyle & MCS_WEEKNUMBERS)
480 MoveToEx(hdc, rcDraw->left + textWidth + 3, title->bottom + textHeight + 2, NULL);
482 MoveToEx(hdc, rcDraw->left + 3, title->bottom + textHeight + 2, NULL);
484 LineTo(hdc, rcDraw->right - 3, title->bottom + textHeight + 2);
486 /* draw day abbreviations */
488 SetBkColor(hdc, infoPtr->monthbk);
489 SetTextColor(hdc, infoPtr->trailingtxt);
491 /* copy this rect so we can change the values without changing */
492 /* the original version */
493 days->left = infoPtr->days.left;
494 days->right = infoPtr->days.right;
495 days->top = infoPtr->days.top;
496 days->bottom = infoPtr->days.bottom;
498 i = infoPtr->firstDay;
501 DrawTextA(hdc, daytxt[i], strlen(daytxt[i]), days,
502 DT_CENTER | DT_VCENTER | DT_SINGLELINE );
504 days->left+=infoPtr->width_increment;
505 days->right+=infoPtr->width_increment;
508 days->left = rcDraw->left + j;
509 if(dwStyle & MCS_WEEKNUMBERS) days->left+=textWidth;
510 /* FIXME: this may need to be changed now 11/10/99 CMM */
511 days->right = rcDraw->left + (j+1) * textWidth - 2;
513 /* draw day numbers; first, the previous month */
515 firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear);
517 prevMonth = infoPtr->currentMonth - 1;
518 if(prevMonth == 0) /* if currentMonth is january(1) prevMonth is */
519 prevMonth = 12; /* december(12) of the previous year */
521 day = MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear) - firstDay;
526 while(day <= MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear)) {
527 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
528 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
530 MONTHCAL_DrawDay(hdc, infoPtr, day, prevMonth, i, 0,
531 infoPtr->monthdayState[m] & mask);
540 if(dwStyle & MCS_WEEKNUMBERS) prevmonth->left = textWidth;
541 prevmonth->right = prevmonth->left + (i * infoPtr->width_increment) +
542 infoPtr->left_offset;
543 prevmonth->top = days->bottom;
544 prevmonth->bottom = prevmonth->top + textHeight;
546 /* draw `current' month */
548 day = 1; /* start at the beginning of the current month */
550 infoPtr->firstDayplace = i;
551 SetTextColor(hdc, infoPtr->txt);
555 /* draw the first week of the current month */
557 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
558 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
561 MONTHCAL_DrawDay(hdc, infoPtr, day, infoPtr->currentMonth, i, 0,
562 infoPtr->monthdayState[m] & mask);
564 if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
565 (day==infoPtr->todaysDate.wDay) &&
566 (infoPtr->currentYear == infoPtr->todaysDate.wYear)) {
567 MONTHCAL_CircleDay(hdc, infoPtr, day, infoPtr->currentMonth);
576 j = 1; /* move to the 2nd week of the current month */
577 i = 0; /* move back to sunday */
578 while(day <= MONTHCAL_MonthLength(infoPtr->currentMonth, infoPtr->currentYear)) {
579 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
580 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
582 MONTHCAL_DrawDay(hdc, infoPtr, day, infoPtr->currentMonth, i, j,
583 infoPtr->monthdayState[m] & mask);
585 if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
586 (day==infoPtr->todaysDate.wDay) &&
587 (infoPtr->currentYear == infoPtr->todaysDate.wYear))
588 MONTHCAL_CircleDay(hdc, infoPtr, day, infoPtr->currentMonth);
593 if(i>6) { /* past saturday, goto the next weeks sunday */
599 /* draw `next' month */
601 /* note: the nextmonth rect only hints for the `half-week' that needs to be
602 * drawn to complete the current week. An eventual next week that needs to
603 * be drawn to complete the month calendar is not taken into account in
604 * this rect -- HitTest knows about this.*/
605 nextmonth->left = rcDraw->left + (i * infoPtr->width_increment) +
606 infoPtr->left_offset;
607 nextmonth->right = rcDraw->right;
608 nextmonth->top = days->bottom + (j+1) * textHeight;
609 nextmonth->bottom = nextmonth->top + textHeight;
611 day = 1; /* start at the first day of the next month */
615 SetTextColor(hdc, infoPtr->trailingtxt);
616 while((i<7) &&(j<6)) {
617 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
618 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
620 MONTHCAL_DrawDay(hdc, infoPtr, day, infoPtr->currentMonth + 1, i, j,
621 infoPtr->monthdayState[m] & mask);
627 if(i==7) { /* past saturday, go to next week's sunday */
632 SetTextColor(hdc, infoPtr->txt);
635 /* draw `today' date if style allows it, and draw a circle before today's
636 * date if necessary */
638 if(!(dwStyle & MCS_NOTODAY)) {
640 if(!(dwStyle & MCS_NOTODAYCIRCLE)) {
641 day = MONTHCAL_CalcDayFromPos(infoPtr, 0, nextmonth->bottom + textHeight);
642 MONTHCAL_CircleDay(hdc, infoPtr, day, infoPtr->currentMonth);
645 MONTHCAL_CalcDayRect(infoPtr, rtoday, 1, 6);
646 sprintf(buf, "Today: %d/%d/%d", infoPtr->todaysDate.wMonth,
647 infoPtr->todaysDate.wDay, infoPtr->todaysDate.wYear);
648 rtoday->left = rtoday->left + 3; /* move text slightly away from circle */
649 rtoday->right = rcDraw->right;
650 SelectObject(hdc, infoPtr->hBoldFont);
652 if(IntersectRect(&rcTemp, &(ps->rcPaint), rtoday))
654 DrawTextA(hdc, buf, -1, rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
656 SelectObject(hdc, infoPtr->hFont);
659 if(dwStyle & MCS_WEEKNUMBERS) {
660 /* display weeknumbers*/
662 weeknums->right = textWidth;
663 weeknums->top = days->bottom + 2;
664 weeknums->bottom = days->bottom + 2 + textHeight;
667 for(i=0; i<infoPtr->currentMonth-1; i++)
668 weeknum+=MONTHCAL_MonthLength(i, infoPtr->currentYear);
672 sprintf(buf, "%d", weeknum + i);
673 DrawTextA(hdc, buf, -1, weeknums, DT_CENTER | DT_BOTTOM | DT_SINGLELINE );
674 weeknums->top+=textHeight * 1.25;
675 weeknums->bottom+=textHeight * 1.25;
678 MoveToEx(hdc, weeknums->right, days->bottom + 5 , NULL);
679 LineTo(hdc, weeknums->right, weeknums->bottom - 1.25 * textHeight - 5);
683 /* currentFont was font at entering Refresh */
685 SetBkColor(hdc, oldBkColor);
686 SelectObject(hdc, currentFont);
687 SetTextColor(hdc, oldTextColor);
692 MONTHCAL_GetMinReqRect(HWND hwnd, WPARAM wParam, LPARAM lParam)
694 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
695 LPRECT lpRect = (LPRECT) lParam;
696 TRACE("%x %lx\n", wParam, lParam);
698 /* validate parameters */
700 if((infoPtr==NULL) ||(lpRect == NULL) ) return FALSE;
702 lpRect->left = infoPtr->rcClient.left;
703 lpRect->right = infoPtr->rcClient.right;
704 lpRect->top = infoPtr->rcClient.top;
705 lpRect->bottom = infoPtr->rcClient.bottom;
711 MONTHCAL_GetColor(HWND hwnd, WPARAM wParam, LPARAM lParam)
713 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
715 TRACE("%x %lx\n", wParam, lParam);
717 switch((int)wParam) {
718 case MCSC_BACKGROUND:
723 return infoPtr->titlebk;
725 return infoPtr->titletxt;
727 return infoPtr->monthbk;
728 case MCSC_TRAILINGTEXT:
729 return infoPtr->trailingtxt;
737 MONTHCAL_SetColor(HWND hwnd, WPARAM wParam, LPARAM lParam)
739 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
742 TRACE("%x %lx\n", wParam, lParam);
744 switch((int)wParam) {
745 case MCSC_BACKGROUND:
747 infoPtr->bk = (COLORREF)lParam;
751 infoPtr->txt = (COLORREF)lParam;
754 prev = infoPtr->titlebk;
755 infoPtr->titlebk = (COLORREF)lParam;
758 prev=infoPtr->titletxt;
759 infoPtr->titletxt = (COLORREF)lParam;
762 prev = infoPtr->monthbk;
763 infoPtr->monthbk = (COLORREF)lParam;
765 case MCSC_TRAILINGTEXT:
766 prev = infoPtr->trailingtxt;
767 infoPtr->trailingtxt = (COLORREF)lParam;
776 MONTHCAL_GetMonthDelta(HWND hwnd, WPARAM wParam, LPARAM lParam)
778 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
780 TRACE("%x %lx\n", wParam, lParam);
783 return infoPtr->delta;
785 return infoPtr->visible;
790 MONTHCAL_SetMonthDelta(HWND hwnd, WPARAM wParam, LPARAM lParam)
792 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
793 int prev = infoPtr->delta;
795 TRACE("%x %lx\n", wParam, lParam);
797 infoPtr->delta = (int)wParam;
803 MONTHCAL_GetFirstDayOfWeek(HWND hwnd, WPARAM wParam, LPARAM lParam)
805 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
807 return infoPtr->firstDay;
811 /* sets the first day of the week that will appear in the control */
812 /* 0 == Monday, 6 == Sunday */
813 /* FIXME: this needs to be implemented properly in MONTHCAL_Refresh() */
814 /* FIXME: we need more error checking here */
816 MONTHCAL_SetFirstDayOfWeek(HWND hwnd, WPARAM wParam, LPARAM lParam)
818 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
819 int prev = infoPtr->firstDay;
823 TRACE("%x %lx\n", wParam, lParam);
825 if((lParam >= 0) && (lParam < 7)) {
826 infoPtr->firstDay = (int)lParam;
827 GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK,
829 TRACE("%s %d\n", buf, strlen(buf));
830 if((sscanf(buf, "%d", &day) == 1) &&(infoPtr->firstDay != day))
831 infoPtr->firstDay = day;
837 /* FIXME: fill this in */
839 MONTHCAL_GetMonthRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
841 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
843 TRACE("%x %lx\n", wParam, lParam);
846 return infoPtr->monthRange;
851 MONTHCAL_GetMaxTodayWidth(HWND hwnd)
853 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
855 return(infoPtr->today.right - infoPtr->today.left);
859 /* FIXME: are validated times taken from current date/time or simply
861 * FIXME: check whether MCM_GETMONTHRANGE shows correct result after
862 * adjusting range with MCM_SETRANGE
866 MONTHCAL_SetRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
868 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
869 SYSTEMTIME lprgSysTimeArray[1];
872 TRACE("%x %lx\n", wParam, lParam);
874 if(wParam & GDTR_MAX) {
875 if(MONTHCAL_ValidateTime(lprgSysTimeArray[1])){
876 MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxDate);
877 infoPtr->rangeValid|=GDTR_MAX;
879 GetSystemTime(&infoPtr->todaysDate);
880 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
883 if(wParam & GDTR_MIN) {
884 if(MONTHCAL_ValidateTime(lprgSysTimeArray[0])) {
885 MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->maxDate);
886 infoPtr->rangeValid|=GDTR_MIN;
888 GetSystemTime(&infoPtr->todaysDate);
889 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
893 prev = infoPtr->monthRange;
894 infoPtr->monthRange = infoPtr->maxDate.wMonth - infoPtr->minDate.wMonth;
896 if(infoPtr->monthRange!=prev) {
897 COMCTL32_ReAlloc(infoPtr->monthdayState,
898 infoPtr->monthRange * sizeof(MONTHDAYSTATE));
905 /* CHECKME: At the moment, we copy ranges anyway,regardless of
906 * infoPtr->rangeValid; a invalid range is simply filled with zeros in
907 * SetRange. Is this the right behavior?
911 MONTHCAL_GetRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
913 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
914 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *)lParam;
916 /* validate parameters */
918 if((infoPtr==NULL) || (lprgSysTimeArray==NULL)) return FALSE;
920 MONTHCAL_CopyTime(&infoPtr->maxDate, &lprgSysTimeArray[1]);
921 MONTHCAL_CopyTime(&infoPtr->minDate, &lprgSysTimeArray[0]);
923 return infoPtr->rangeValid;
928 MONTHCAL_SetDayState(HWND hwnd, WPARAM wParam, LPARAM lParam)
931 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
932 int i, iMonths = (int)wParam;
933 MONTHDAYSTATE *dayStates = (LPMONTHDAYSTATE)lParam;
935 TRACE("%x %lx\n", wParam, lParam);
936 if(iMonths!=infoPtr->monthRange) return 0;
938 for(i=0; i<iMonths; i++)
939 infoPtr->monthdayState[i] = dayStates[i];
945 MONTHCAL_GetCurSel(HWND hwnd, WPARAM wParam, LPARAM lParam)
947 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
948 SYSTEMTIME *lpSel = (SYSTEMTIME *) lParam;
950 TRACE("%x %lx\n", wParam, lParam);
951 if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
952 if(GetWindowLongA(hwnd, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
954 MONTHCAL_CopyTime(&infoPtr->minSel, lpSel);
959 /* FIXME: if the specified date is not visible, make it visible */
962 MONTHCAL_SetCurSel(HWND hwnd, WPARAM wParam, LPARAM lParam)
964 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
965 SYSTEMTIME *lpSel = (SYSTEMTIME *)lParam;
967 TRACE("%x %lx\n", wParam, lParam);
968 if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
969 if(GetWindowLongA(hwnd, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
971 TRACE("%d %d\n", lpSel->wMonth, lpSel->wDay);
973 MONTHCAL_CopyTime(lpSel, &infoPtr->minSel);
974 MONTHCAL_CopyTime(lpSel, &infoPtr->maxSel);
976 InvalidateRect(hwnd, NULL, FALSE);
983 MONTHCAL_GetMaxSelCount(HWND hwnd, WPARAM wParam, LPARAM lParam)
985 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
987 TRACE("%x %lx\n", wParam, lParam);
988 return infoPtr->maxSelCount;
993 MONTHCAL_SetMaxSelCount(HWND hwnd, WPARAM wParam, LPARAM lParam)
995 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
997 TRACE("%x %lx\n", wParam, lParam);
998 if(GetWindowLongA(hwnd, GWL_STYLE) & MCS_MULTISELECT) {
999 infoPtr->maxSelCount = wParam;
1007 MONTHCAL_GetSelRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
1009 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1010 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1012 TRACE("%x %lx\n", wParam, lParam);
1014 /* validate parameters */
1016 if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1018 if(GetWindowLongA(hwnd, GWL_STYLE) & MCS_MULTISELECT)
1020 MONTHCAL_CopyTime(&infoPtr->maxSel, &lprgSysTimeArray[1]);
1021 MONTHCAL_CopyTime(&infoPtr->minSel, &lprgSysTimeArray[0]);
1022 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1031 MONTHCAL_SetSelRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
1033 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1034 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1036 TRACE("%x %lx\n", wParam, lParam);
1038 /* validate parameters */
1040 if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1042 if(GetWindowLongA( hwnd, GWL_STYLE) & MCS_MULTISELECT)
1044 MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxSel);
1045 MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minSel);
1046 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1055 MONTHCAL_GetToday(HWND hwnd, WPARAM wParam, LPARAM lParam)
1057 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1058 SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1060 TRACE("%x %lx\n", wParam, lParam);
1062 /* validate parameters */
1064 if((infoPtr==NULL) || (lpToday==NULL)) return FALSE;
1065 MONTHCAL_CopyTime(&infoPtr->todaysDate, lpToday);
1071 MONTHCAL_SetToday(HWND hwnd, WPARAM wParam, LPARAM lParam)
1073 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1074 SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1076 TRACE("%x %lx\n", wParam, lParam);
1078 /* validate parameters */
1080 if((infoPtr==NULL) ||(lpToday==NULL)) return FALSE;
1081 MONTHCAL_CopyTime(lpToday, &infoPtr->todaysDate);
1087 MONTHCAL_HitTest(HWND hwnd, LPARAM lParam)
1089 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1090 PMCHITTESTINFO lpht = (PMCHITTESTINFO)lParam;
1096 retval = MCHT_NOWHERE;
1099 /* are we in the header? */
1101 if(PtInRect(&infoPtr->title, lpht->pt)) {
1102 if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1103 retval = MCHT_TITLEBTNPREV;
1106 if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1107 retval = MCHT_TITLEBTNNEXT;
1110 if(PtInRect(&infoPtr->titlemonth, lpht->pt)) {
1111 retval = MCHT_TITLEMONTH;
1114 if(PtInRect(&infoPtr->titleyear, lpht->pt)) {
1115 retval = MCHT_TITLEYEAR;
1119 retval = MCHT_TITLE;
1123 if(PtInRect(&infoPtr->days, lpht->pt)) {
1124 retval = MCHT_CALENDARDAY; /* FIXME: find out which day we're on */
1127 if(PtInRect(&infoPtr->weeknums, lpht->pt)) {
1128 retval = MCHT_CALENDARWEEKNUM; /* FIXME: find out which day we're on */
1131 if(PtInRect(&infoPtr->prevmonth, lpht->pt)) {
1132 retval = MCHT_CALENDARDATEPREV;
1136 if(PtInRect(&infoPtr->nextmonth, lpht->pt) ||
1137 ((y > infoPtr->nextmonth.bottom) && (y < infoPtr->nextmonth.bottom +
1138 infoPtr->height_increment) && (x < infoPtr->rcClient.right) &&
1139 (x > infoPtr->rcDraw.left))) {
1140 retval = MCHT_CALENDARDATENEXT;
1144 if(PtInRect(&infoPtr->today, lpht->pt)) {
1145 retval = MCHT_TODAYLINK;
1149 /* MCHT_CALENDARDATE determination: since the next & previous month have
1150 * been handled already(MCHT_CALENDARDATEPREV/NEXT), we only have to check
1151 * whether we're in the calendar area. infoPtr->prevMonth.left handles the
1152 * MCS_WEEKNUMBERS style nicely.
1156 TRACE("%d %d [%d %d %d %d] [%d %d %d %d]\n", x, y,
1157 infoPtr->prevmonth.left, infoPtr->prevmonth.right,
1158 infoPtr->prevmonth.top, infoPtr->prevmonth.bottom,
1159 infoPtr->nextmonth.left, infoPtr->nextmonth.right,
1160 infoPtr->nextmonth.top, infoPtr->nextmonth.bottom);
1161 if((x > infoPtr->rcClient.left) && (x < infoPtr->rcClient.right) &&
1162 (y > infoPtr->rcClient.top) && (y < infoPtr->nextmonth.bottom)) {
1163 lpht->st.wYear = infoPtr->currentYear;
1164 lpht->st.wMonth = infoPtr->currentMonth;
1166 lpht->st.wDay = MONTHCAL_CalcDayFromPos(infoPtr, x, y);
1168 TRACE("day hit: %d\n", lpht->st.wDay);
1169 retval = MCHT_CALENDARDATE;
1174 /* Hit nothing special? What's left must be background :-) */
1176 retval = MCHT_CALENDARBK;
1178 lpht->uHit = retval;
1183 static void MONTHCAL_GoToNextMonth(HWND hwnd, MONTHCAL_INFO *infoPtr)
1185 DWORD dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
1187 TRACE("MONTHCAL_GoToNextMonth\n");
1189 infoPtr->currentMonth++;
1190 if(infoPtr->currentMonth > 12) {
1191 infoPtr->currentYear++;
1192 infoPtr->currentMonth = 1;
1195 if(dwStyle & MCS_DAYSTATE) {
1199 nmds.nmhdr.hwndFrom = hwnd;
1200 nmds.nmhdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
1201 nmds.nmhdr.code = MCN_GETDAYSTATE;
1202 nmds.cDayState = infoPtr->monthRange;
1203 nmds.prgDayState = COMCTL32_Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1205 SendMessageA(GetParent(hwnd), WM_NOTIFY,
1206 (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
1207 for(i=0; i<infoPtr->monthRange; i++)
1208 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1213 static void MONTHCAL_GoToPrevMonth(HWND hwnd, MONTHCAL_INFO *infoPtr)
1215 DWORD dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
1217 TRACE("MONTHCAL_GoToPrevMonth\n");
1219 infoPtr->currentMonth--;
1220 if(infoPtr->currentMonth < 1) {
1221 infoPtr->currentYear--;
1222 infoPtr->currentMonth = 12;
1225 if(dwStyle & MCS_DAYSTATE) {
1229 nmds.nmhdr.hwndFrom = hwnd;
1230 nmds.nmhdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
1231 nmds.nmhdr.code = MCN_GETDAYSTATE;
1232 nmds.cDayState = infoPtr->monthRange;
1233 nmds.prgDayState = COMCTL32_Alloc
1234 (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1236 SendMessageA(GetParent(hwnd), WM_NOTIFY,
1237 (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
1238 for(i=0; i<infoPtr->monthRange; i++)
1239 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1245 MONTHCAL_LButtonDown(HWND hwnd, WPARAM wParam, LPARAM lParam)
1247 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1252 BOOL redraw = FALSE;
1253 RECT rcDay; /* used in determining area to invalidate */
1255 TRACE("%x %lx\n", wParam, lParam);
1257 ht.pt.x = (INT)LOWORD(lParam);
1258 ht.pt.y = (INT)HIWORD(lParam);
1259 hit = MONTHCAL_HitTest(hwnd, (LPARAM)&ht);
1261 /* FIXME: these flags should be checked by */
1262 /*((hit & MCHT_XXX) == MCHT_XXX) b/c some of the flags are */
1264 if(hit & MCHT_NEXT) {
1266 MONTHCAL_GoToNextMonth(hwnd, infoPtr);
1267 infoPtr->status = MC_NEXTPRESSED;
1268 SetTimer(hwnd, MC_NEXTMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1269 InvalidateRect(hwnd, NULL, FALSE);
1271 if(hit & MCHT_PREV) {
1273 MONTHCAL_GoToPrevMonth(hwnd, infoPtr);
1274 infoPtr->status = MC_PREVPRESSED;
1275 SetTimer(hwnd, MC_PREVMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1276 InvalidateRect(hwnd, NULL, FALSE);
1279 if(hit == MCHT_TITLEMONTH) {
1281 HRSRC hrsrc = FindResourceA( COMCTL32_hModule, MAKEINTRESOURCEA(IDD_MCMONTHMENU), RT_MENUA );
1283 TRACE("returning zero\n");
1286 TRACE("resource is:%x\n",hrsrc);
1287 hMenu = LoadMenuIndirectA((LPCVOID)LoadResource( COMCTL32_hModule, hrsrc ));
1289 TRACE("menu is:%x\n",hMenu);
1292 hMenu = CreateMenu();
1293 AppendMenuA(hMenu, MF_STRING,IDM_JAN, "January");
1294 AppendMenuA(hMenu, MF_STRING,IDM_FEB, "February");
1295 AppendMenuA(hMenu, MF_STRING,IDM_MAR, "March");
1297 retval = CreateWindowA(POPUPMENU_CLASS_ATOM, NULL,
1298 WS_CHILD | WS_VISIBLE, 0, 0 ,100 , 220,
1299 hwnd, hMenu, GetWindowLongA(hwnd, GWL_HINSTANCE), NULL);
1300 TRACE("hwnd returned:%x\n", retval);
1303 if(hit == MCHT_TITLEYEAR) {
1304 FIXME("create updown for yearselection\n");
1306 if(hit == MCHT_TODAYLINK) {
1307 FIXME("set currentday\n");
1309 if(hit == MCHT_CALENDARDATE) {
1310 SYSTEMTIME selArray[2];
1314 nmsc.nmhdr.hwndFrom = hwnd;
1315 nmsc.nmhdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
1316 nmsc.nmhdr.code = MCN_SELCHANGE;
1317 MONTHCAL_CopyTime(&nmsc.stSelStart, &infoPtr->minSel);
1318 MONTHCAL_CopyTime(&nmsc.stSelEnd, &infoPtr->maxSel);
1320 SendMessageA(GetParent(hwnd), WM_NOTIFY,
1321 (WPARAM)nmsc.nmhdr.idFrom,(LPARAM)&nmsc);
1323 MONTHCAL_CopyTime(&ht.st, &selArray[0]);
1324 MONTHCAL_CopyTime(&ht.st, &selArray[1]);
1325 MONTHCAL_SetSelRange(hwnd,0,(LPARAM) &selArray);
1327 /* FIXME: for some reason if RedrawWindow has a NULL instead of zero it gives */
1328 /* a compiler warning */
1329 /* redraw both old and new days if the selected day changed */
1330 if(infoPtr->curSelDay != ht.st.wDay) {
1331 MONTHCAL_CalcPosFromDay(infoPtr, ht.st.wDay, ht.st.wMonth, &rcDay);
1332 RedrawWindow(hwnd, &rcDay, 0, RDW_ERASE|RDW_INVALIDATE);
1334 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->curSelDay, infoPtr->currentMonth, &rcDay);
1335 RedrawWindow(hwnd, &rcDay, 0, RDW_ERASE|RDW_INVALIDATE);
1338 infoPtr->firstSelDay = ht.st.wDay;
1339 infoPtr->curSelDay = ht.st.wDay;
1340 infoPtr->status = MC_SEL_LBUTDOWN;
1349 MONTHCAL_LButtonUp(HWND hwnd, WPARAM wParam, LPARAM lParam)
1351 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1354 BOOL redraw = FALSE;
1358 if(infoPtr->status & MC_NEXTPRESSED) {
1359 KillTimer(hwnd, MC_NEXTMONTHTIMER);
1362 if(infoPtr->status & MC_PREVPRESSED) {
1363 KillTimer(hwnd, MC_PREVMONTHTIMER);
1367 infoPtr->status = MC_SEL_LBUTUP;
1369 nmhdr.hwndFrom = hwnd;
1370 nmhdr.idFrom = GetWindowLongA( hwnd, GWL_ID);
1371 nmhdr.code = NM_RELEASEDCAPTURE;
1372 TRACE("Sent notification from %x to %x\n", hwnd, GetParent(hwnd));
1374 SendMessageA(GetParent(hwnd), WM_NOTIFY,
1375 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
1377 nmsc.nmhdr.hwndFrom = hwnd;
1378 nmsc.nmhdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
1379 nmsc.nmhdr.code = MCN_SELECT;
1380 MONTHCAL_CopyTime(&nmsc.stSelStart, &infoPtr->minSel);
1381 MONTHCAL_CopyTime(&nmsc.stSelEnd, &infoPtr->maxSel);
1383 SendMessageA(GetParent(hwnd), WM_NOTIFY,
1384 (WPARAM)nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1386 /* redraw if necessary */
1388 InvalidateRect(hwnd, NULL, FALSE);
1395 MONTHCAL_Timer(HWND hwnd, WPARAM wParam, LPARAM lParam)
1397 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1398 BOOL redraw = FALSE;
1400 TRACE(" %d\n", wParam);
1401 if(!infoPtr) return 0;
1404 case MC_NEXTMONTHTIMER:
1406 MONTHCAL_GoToNextMonth(hwnd, infoPtr);
1408 case MC_PREVMONTHTIMER:
1410 MONTHCAL_GoToPrevMonth(hwnd, infoPtr);
1413 ERR("got unknown timer\n");
1416 /* redraw only if necessary */
1418 InvalidateRect(hwnd, NULL, FALSE);
1425 MONTHCAL_MouseMove(HWND hwnd, WPARAM wParam, LPARAM lParam)
1427 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1429 int oldselday, selday, hit;
1432 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
1434 ht.pt.x = LOWORD(lParam);
1435 ht.pt.y = HIWORD(lParam);
1437 hit = MONTHCAL_HitTest(hwnd, (LPARAM)&ht);
1439 /* not on the calendar date numbers? bail out */
1440 TRACE("hit:%x\n",hit);
1441 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE) return 0;
1443 selday = ht.st.wDay;
1444 oldselday = infoPtr->curSelDay;
1445 infoPtr->curSelDay = selday;
1446 MONTHCAL_CalcPosFromDay(infoPtr, selday, ht.st. wMonth, &r);
1448 if(GetWindowLongA(hwnd, GWL_STYLE) & MCS_MULTISELECT) {
1449 SYSTEMTIME selArray[2];
1452 MONTHCAL_GetSelRange(hwnd, 0, (LPARAM)&selArray);
1454 if(infoPtr->firstSelDay==selArray[0].wDay) i=1;
1455 TRACE("oldRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1456 if(infoPtr->firstSelDay==selArray[1].wDay) {
1457 /* 1st time we get here: selArray[0]=selArray[1]) */
1458 /* if we're still at the first selected date, return */
1459 if(infoPtr->firstSelDay==selday) goto done;
1460 if(selday<infoPtr->firstSelDay) i = 0;
1463 if(abs(infoPtr->firstSelDay - selday) >= infoPtr->maxSelCount) {
1464 if(selday>infoPtr->firstSelDay)
1465 selday = infoPtr->firstSelDay + infoPtr->maxSelCount;
1467 selday = infoPtr->firstSelDay - infoPtr->maxSelCount;
1470 if(selArray[i].wDay!=selday) {
1471 TRACE("newRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1473 selArray[i].wDay = selday;
1475 if(selArray[0].wDay>selArray[1].wDay) {
1477 tempday = selArray[1].wDay;
1478 selArray[1].wDay = selArray[0].wDay;
1479 selArray[0].wDay = tempday;
1482 MONTHCAL_SetSelRange(hwnd, 0, (LPARAM)&selArray);
1488 /* only redraw if the currently selected day changed */
1489 /* FIXME: this should specify a rectangle containing only the days that changed */
1490 /* using RedrawWindow */
1491 if(oldselday != infoPtr->curSelDay)
1492 InvalidateRect(hwnd, NULL, FALSE);
1499 MONTHCAL_Paint(HWND hwnd, WPARAM wParam)
1501 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1505 /* fill ps.rcPaint with a default rect */
1506 memcpy(&(ps.rcPaint), &(infoPtr->rcClient), sizeof(infoPtr->rcClient));
1508 hdc = (wParam==0 ? BeginPaint(hwnd, &ps) : (HDC)wParam);
1509 MONTHCAL_Refresh(hwnd, hdc, &ps);
1510 if(!wParam) EndPaint(hwnd, &ps);
1516 MONTHCAL_KillFocus(HWND hwnd, WPARAM wParam, LPARAM lParam)
1520 InvalidateRect(hwnd, NULL, TRUE);
1527 MONTHCAL_SetFocus(HWND hwnd, WPARAM wParam, LPARAM lParam)
1531 InvalidateRect(hwnd, NULL, FALSE);
1536 /* sets the size information */
1537 static void MONTHCAL_UpdateSize(HWND hwnd)
1539 HDC hdc = GetDC(hwnd);
1540 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1541 RECT *rcClient=&infoPtr->rcClient;
1542 RECT *rcDraw=&infoPtr->rcDraw;
1543 RECT *title=&infoPtr->title;
1544 RECT *prev=&infoPtr->titlebtnprev;
1545 RECT *next=&infoPtr->titlebtnnext;
1546 RECT *titlemonth=&infoPtr->titlemonth;
1547 RECT *titleyear=&infoPtr->titleyear;
1548 RECT *days=&infoPtr->days;
1551 DWORD dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
1554 currentFont = SelectObject(hdc, infoPtr->hFont);
1556 /* FIXME: need a way to determine current font, without setting it */
1558 if(infoPtr->hFont!=currentFont) {
1559 SelectObject(hdc, currentFont);
1560 infoPtr->hFont=currentFont;
1561 GetObjectA(currentFont, sizeof(LOGFONTA), &logFont);
1562 logFont.lfWeight=FW_BOLD;
1563 infoPtr->hBoldFont = CreateFontIndirectA(&logFont);
1567 /* get the height and width of each day's text */
1568 GetTextMetricsA(hdc, &tm);
1569 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading;
1570 GetTextExtentPoint32A(hdc, "Sun", 3, &size);
1571 infoPtr->textWidth = size.cx + 2;
1573 /* retrieve the controls client rectangle info infoPtr->rcClient */
1574 GetClientRect(hwnd, rcClient);
1576 if(dwStyle & MCS_WEEKNUMBERS)
1577 infoPtr->rcClient.right+=infoPtr->textWidth;
1579 /* rcDraw is the rectangle the control is drawn in */
1580 rcDraw->left = rcClient->left;
1581 rcDraw->right = rcClient->right;
1582 rcDraw->top = rcClient->top;
1583 rcDraw->bottom = rcClient->bottom;
1585 /* this is correct, the control does NOT expand vertically */
1586 /* like it does horizontally */
1587 /* make sure we don't move the controls bottom out of the client */
1589 if((rcDraw->top + 8 * infoPtr->textHeight + 5) < rcDraw->bottom) {
1590 rcDraw->bottom = rcDraw->top + 8 * infoPtr->textHeight + 5;
1593 /* calculate title area */
1594 title->top = rcClient->top;
1595 title->bottom = title->top + 2 * infoPtr->textHeight + 4;
1596 title->left = rcClient->left;
1597 title->right = rcClient->right;
1599 /* recalculate the height and width increments and offsets */
1600 infoPtr->width_increment = (infoPtr->rcDraw.right - infoPtr->rcDraw.left) / 7.0;
1601 infoPtr->height_increment = (infoPtr->rcDraw.bottom - infoPtr->rcDraw.top) / 7.0;
1602 infoPtr->left_offset = (infoPtr->rcDraw.right - infoPtr->rcDraw.left) - (infoPtr->width_increment * 7.0);
1603 infoPtr->top_offset = (infoPtr->rcDraw.bottom - infoPtr->rcDraw.top) - (infoPtr->height_increment * 7.0);
1605 /* set the dimensions of the next and previous buttons and center */
1606 /* the month text vertically */
1607 prev->top = next->top = title->top + 6;
1608 prev->bottom = next->bottom = title->top + 2 * infoPtr->textHeight - 3;
1609 prev->right = title->left + 28;
1610 prev->left = title->left + 4;
1611 next->left = title->right - 28;
1612 next->right = title->right - 4;
1614 /* titlemonth->left and right change based upon the current month */
1615 /* and are recalculated in refresh as the current month may change */
1616 /* without the control being resized */
1617 titlemonth->bottom = titleyear->bottom = prev->top + 2 * infoPtr->textHeight - 3;
1618 titlemonth->top = titleyear->top = title->top;
1620 /* setup the dimensions of the rectangle we draw the names of the */
1621 /* days of the week in */
1622 days->left = infoPtr->left_offset;
1623 if(dwStyle & MCS_WEEKNUMBERS) days->left+=infoPtr->textWidth;
1624 days->right = days->left + infoPtr->width_increment;
1625 days->top = title->bottom + 2;
1626 days->bottom = title->bottom + infoPtr->textHeight + 2;
1628 /* restore the originally selected font */
1629 SelectObject(hdc, currentFont);
1631 ReleaseDC(hwnd, hdc);
1634 static LRESULT MONTHCAL_Size(HWND hwnd, int Width, int Height)
1636 TRACE("(hwnd=%x, width=%d, height=%d)\n", hwnd, Width, Height);
1638 MONTHCAL_UpdateSize(hwnd);
1640 /* invalidate client area and erase background */
1641 InvalidateRect(hwnd, NULL, TRUE);
1646 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
1648 MONTHCAL_Create(HWND hwnd, WPARAM wParam, LPARAM lParam)
1650 MONTHCAL_INFO *infoPtr;
1653 /* allocate memory for info structure */
1654 infoPtr =(MONTHCAL_INFO*)COMCTL32_Alloc(sizeof(MONTHCAL_INFO));
1655 SetWindowLongA(hwnd, 0, (DWORD)infoPtr);
1657 if(infoPtr == NULL) {
1658 ERR( "could not allocate info memory!\n");
1661 if((MONTHCAL_INFO*)GetWindowLongA(hwnd, 0) != infoPtr) {
1662 ERR( "pointer assignment error!\n");
1666 infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1667 GetObjectA(infoPtr->hFont, sizeof(LOGFONTA), &logFont);
1668 logFont.lfWeight = FW_BOLD;
1669 infoPtr->hBoldFont = CreateFontIndirectA(&logFont);
1671 /* initialize info structure */
1672 /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
1674 GetSystemTime(&infoPtr->todaysDate);
1675 infoPtr->firstDay = 0;
1676 infoPtr->currentMonth = infoPtr->todaysDate.wMonth;
1677 infoPtr->currentYear = infoPtr->todaysDate.wYear;
1678 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->minDate);
1679 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
1680 infoPtr->maxSelCount = 6;
1681 infoPtr->monthRange = 3;
1682 infoPtr->monthdayState = COMCTL32_Alloc
1683 (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1684 infoPtr->titlebk = GetSysColor(COLOR_ACTIVECAPTION);
1685 infoPtr->titletxt = GetSysColor(COLOR_WINDOW);
1686 infoPtr->monthbk = GetSysColor(COLOR_WINDOW);
1687 infoPtr->trailingtxt = GetSysColor(COLOR_GRAYTEXT);
1688 infoPtr->bk = GetSysColor(COLOR_WINDOW);
1689 infoPtr->txt = GetSysColor(COLOR_WINDOWTEXT);
1691 /* call MONTHCAL_UpdateSize to set all of the dimensions */
1692 /* of the control */
1693 MONTHCAL_UpdateSize(hwnd);
1700 MONTHCAL_Destroy(HWND hwnd, WPARAM wParam, LPARAM lParam)
1702 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1704 /* free month calendar info data */
1705 COMCTL32_Free(infoPtr);
1706 SetWindowLongA(hwnd, 0, 0);
1711 static LRESULT WINAPI
1712 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1714 TRACE("hwnd=%x msg=%x wparam=%x lparam=%lx\n", hwnd, uMsg, wParam, lParam);
1715 if (!MONTHCAL_GetInfoPtr(hwnd) && (uMsg != WM_CREATE))
1716 return DefWindowProcA(hwnd, uMsg, wParam, lParam);
1720 return MONTHCAL_GetCurSel(hwnd, wParam, lParam);
1723 return MONTHCAL_SetCurSel(hwnd, wParam, lParam);
1725 case MCM_GETMAXSELCOUNT:
1726 return MONTHCAL_GetMaxSelCount(hwnd, wParam, lParam);
1728 case MCM_SETMAXSELCOUNT:
1729 return MONTHCAL_SetMaxSelCount(hwnd, wParam, lParam);
1731 case MCM_GETSELRANGE:
1732 return MONTHCAL_GetSelRange(hwnd, wParam, lParam);
1734 case MCM_SETSELRANGE:
1735 return MONTHCAL_SetSelRange(hwnd, wParam, lParam);
1737 case MCM_GETMONTHRANGE:
1738 return MONTHCAL_GetMonthRange(hwnd, wParam, lParam);
1740 case MCM_SETDAYSTATE:
1741 return MONTHCAL_SetDayState(hwnd, wParam, lParam);
1743 case MCM_GETMINREQRECT:
1744 return MONTHCAL_GetMinReqRect(hwnd, wParam, lParam);
1747 return MONTHCAL_GetColor(hwnd, wParam, lParam);
1750 return MONTHCAL_SetColor(hwnd, wParam, lParam);
1753 return MONTHCAL_GetToday(hwnd, wParam, lParam);
1756 return MONTHCAL_SetToday(hwnd, wParam, lParam);
1759 return MONTHCAL_HitTest(hwnd,lParam);
1761 case MCM_GETFIRSTDAYOFWEEK:
1762 return MONTHCAL_GetFirstDayOfWeek(hwnd, wParam, lParam);
1764 case MCM_SETFIRSTDAYOFWEEK:
1765 return MONTHCAL_SetFirstDayOfWeek(hwnd, wParam, lParam);
1768 return MONTHCAL_GetRange(hwnd, wParam, lParam);
1771 return MONTHCAL_SetRange(hwnd, wParam, lParam);
1773 case MCM_GETMONTHDELTA:
1774 return MONTHCAL_GetMonthDelta(hwnd, wParam, lParam);
1776 case MCM_SETMONTHDELTA:
1777 return MONTHCAL_SetMonthDelta(hwnd, wParam, lParam);
1779 case MCM_GETMAXTODAYWIDTH:
1780 return MONTHCAL_GetMaxTodayWidth(hwnd);
1783 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1786 return MONTHCAL_KillFocus(hwnd, wParam, lParam);
1788 case WM_LBUTTONDOWN:
1789 return MONTHCAL_LButtonDown(hwnd, wParam, lParam);
1792 return MONTHCAL_MouseMove(hwnd, wParam, lParam);
1795 return MONTHCAL_LButtonUp(hwnd, wParam, lParam);
1798 return MONTHCAL_Paint(hwnd, wParam);
1801 return MONTHCAL_SetFocus(hwnd, wParam, lParam);
1804 return MONTHCAL_Size(hwnd, (int)SLOWORD(lParam), (int)SHIWORD(lParam));
1807 return MONTHCAL_Create(hwnd, wParam, lParam);
1810 return MONTHCAL_Timer(hwnd, wParam, lParam);
1813 return MONTHCAL_Destroy(hwnd, wParam, lParam);
1817 ERR( "unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
1818 return DefWindowProcA(hwnd, uMsg, wParam, lParam);
1825 MONTHCAL_Register(void)
1829 ZeroMemory(&wndClass, sizeof(WNDCLASSA));
1830 wndClass.style = CS_GLOBALCLASS;
1831 wndClass.lpfnWndProc = (WNDPROC)MONTHCAL_WindowProc;
1832 wndClass.cbClsExtra = 0;
1833 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
1834 wndClass.hCursor = LoadCursorA(0, IDC_ARROWA);
1835 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1836 wndClass.lpszClassName = MONTHCAL_CLASSA;
1838 RegisterClassA(&wndClass);
1843 MONTHCAL_Unregister(void)
1845 UnregisterClassA(MONTHCAL_CLASSA, (HINSTANCE)NULL);