gdiplus: Added TRACE(..) for Pen calls.
[wine/multimedia.git] / dlls / comctl32 / monthcal.c
blob8cae0db45b4f0e968988eb7f2ba5da06445dd3b0
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * NOTE
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.
33 * TODO:
34 * -- MCM_[GS]ETUNICODEFORMAT
35 * -- MONTHCAL_GetMonthRange
36 * -- handle resources better (doesn't work now);
37 * -- take care of internationalization.
38 * -- keyboard handling.
39 * -- GetRange: At the moment, we copy ranges anyway, regardless of
40 * infoPtr->rangeValid; an invalid range is simply filled
41 * with zeros in SetRange. Is this the right behavior?
42 * -- search for FIXME
45 #include <math.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
51 #include "windef.h"
52 #include "winbase.h"
53 #include "wingdi.h"
54 #include "winuser.h"
55 #include "winnls.h"
56 #include "commctrl.h"
57 #include "comctl32.h"
58 #include "uxtheme.h"
59 #include "tmschema.h"
60 #include "wine/unicode.h"
61 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
65 #define MC_SEL_LBUTUP 1 /* Left button released */
66 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
67 #define MC_PREVPRESSED 4 /* Prev month button pressed */
68 #define MC_NEXTPRESSED 8 /* Next month button pressed */
69 #define MC_NEXTMONTHDELAY 350 /* when continuously pressing `next */
70 /* month', wait 500 ms before going */
71 /* to the next month */
72 #define MC_NEXTMONTHTIMER 1 /* Timer ID's */
73 #define MC_PREVMONTHTIMER 2
75 #define countof(arr) (sizeof(arr)/sizeof(arr[0]))
77 typedef struct
79 HWND hwndSelf;
80 COLORREF bk;
81 COLORREF txt;
82 COLORREF titlebk;
83 COLORREF titletxt;
84 COLORREF monthbk;
85 COLORREF trailingtxt;
86 HFONT hFont;
87 HFONT hBoldFont;
88 int textHeight;
89 int textWidth;
90 int height_increment;
91 int width_increment;
92 int firstDayplace; /* place of the first day of the current month */
93 int delta; /* scroll rate; # of months that the */
94 /* control moves when user clicks a scroll button */
95 int visible; /* # of months visible */
96 int firstDay; /* Start month calendar with firstDay's day */
97 int firstDayHighWord; /* High word only used externally */
98 int monthRange;
99 MONTHDAYSTATE *monthdayState;
100 SYSTEMTIME todaysDate;
101 DWORD currentMonth;
102 DWORD currentYear;
103 int status; /* See MC_SEL flags */
104 int curSelDay; /* current selected day */
105 int firstSelDay; /* first selected day */
106 int maxSelCount;
107 SYSTEMTIME minSel;
108 SYSTEMTIME maxSel;
109 DWORD rangeValid;
110 SYSTEMTIME minDate;
111 SYSTEMTIME maxDate;
113 RECT title; /* rect for the header above the calendar */
114 RECT titlebtnnext; /* the `next month' button in the header */
115 RECT titlebtnprev; /* the `prev month' button in the header */
116 RECT titlemonth; /* the `month name' txt in the header */
117 RECT titleyear; /* the `year number' txt in the header */
118 RECT wdays; /* week days at top */
119 RECT days; /* calendar area */
120 RECT weeknums; /* week numbers at left side */
121 RECT todayrect; /* `today: xx/xx/xx' text rect */
122 HWND hwndNotify; /* Window to receive the notifications */
123 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
124 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
125 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
128 /* Offsets of days in the week to the weekday of january 1 in a leap year */
129 static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
131 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
133 #define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0))
135 /* helper functions */
137 /* returns the number of days in any given month, checking for leap days */
138 /* january is 1, december is 12 */
139 int MONTHCAL_MonthLength(int month, int year)
141 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0};
142 /*Wrap around, this eases handling*/
143 if(month == 0)
144 month = 12;
145 if(month == 13)
146 month = 1;
148 /* if we have a leap year add 1 day to February */
149 /* a leap year is a year either divisible by 400 */
150 /* or divisible by 4 and not by 100 */
151 if(month == 2) { /* February */
152 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
153 (year%4 == 0)) ? 1 : 0);
155 else {
156 return mdays[month - 1];
161 /* make sure that time is valid */
162 static int MONTHCAL_ValidateTime(SYSTEMTIME time)
164 if(time.wMonth < 1 || time.wMonth > 12 ) return FALSE;
165 if(time.wDayOfWeek > 6) return FALSE;
166 if(time.wDay > MONTHCAL_MonthLength(time.wMonth, time.wYear))
167 return FALSE;
169 return TRUE;
173 /* Note:Depending on DST, this may be offset by a day.
174 Need to find out if we're on a DST place & adjust the clock accordingly.
175 Above function assumes we have a valid data.
176 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
177 0 = Sunday.
180 /* returns the day in the week(0 == sunday, 6 == saturday) */
181 /* day(1 == 1st, 2 == 2nd... etc), year is the year value */
182 static int MONTHCAL_CalculateDayOfWeek(DWORD day, DWORD month, DWORD year)
184 year-=(month < 3);
186 return((year + year/4 - year/100 + year/400 +
187 DayOfWeekTable[month-1] + day ) % 7);
190 /* From a given point, calculate the row (weekpos), column(daypos)
191 and day in the calendar. day== 0 mean the last day of tha last month
193 static int MONTHCAL_CalcDayFromPos(const MONTHCAL_INFO *infoPtr, int x, int y,
194 int *daypos,int *weekpos)
196 int retval, firstDay;
197 RECT rcClient;
199 GetClientRect(infoPtr->hwndSelf, &rcClient);
201 /* if the point is outside the x bounds of the window put
202 it at the boundary */
203 if (x > rcClient.right)
204 x = rcClient.right;
207 *daypos = (x - infoPtr->days.left ) / infoPtr->width_increment;
208 *weekpos = (y - infoPtr->days.top ) / infoPtr->height_increment;
210 firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear)+6 - infoPtr->firstDay)%7;
211 retval = *daypos + (7 * *weekpos) - firstDay;
212 return retval;
215 /* day is the day of the month, 1 == 1st day of the month */
216 /* sets x and y to be the position of the day */
217 /* x == day, y == week where(0,0) == firstDay, 1st week */
218 static void MONTHCAL_CalcDayXY(const MONTHCAL_INFO *infoPtr, int day, int month,
219 int *x, int *y)
221 int firstDay, prevMonth;
223 firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear) +6 - infoPtr->firstDay)%7;
225 if(month==infoPtr->currentMonth) {
226 *x = (day + firstDay) % 7;
227 *y = (day + firstDay - *x) / 7;
228 return;
230 if(month < infoPtr->currentMonth) {
231 prevMonth = month - 1;
232 if(prevMonth==0)
233 prevMonth = 12;
235 *x = (MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear) - firstDay) % 7;
236 *y = 0;
237 return;
240 *y = MONTHCAL_MonthLength(month, infoPtr->currentYear - 1) / 7;
241 *x = (day + firstDay + MONTHCAL_MonthLength(month,
242 infoPtr->currentYear)) % 7;
246 /* x: column(day), y: row(week) */
247 static void MONTHCAL_CalcDayRect(const MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
249 r->left = infoPtr->days.left + x * infoPtr->width_increment;
250 r->right = r->left + infoPtr->width_increment;
251 r->top = infoPtr->days.top + y * infoPtr->height_increment;
252 r->bottom = r->top + infoPtr->textHeight;
256 /* sets the RECT struct r to the rectangle around the day and month */
257 /* day is the day value of the month(1 == 1st), month is the month */
258 /* value(january == 1, december == 12) */
259 static inline void MONTHCAL_CalcPosFromDay(const MONTHCAL_INFO *infoPtr,
260 int day, int month, RECT *r)
262 int x, y;
264 MONTHCAL_CalcDayXY(infoPtr, day, month, &x, &y);
265 MONTHCAL_CalcDayRect(infoPtr, r, x, y);
269 /* day is the day in the month(1 == 1st of the month) */
270 /* month is the month value(1 == january, 12 == december) */
271 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month)
273 HPEN hRedPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
274 HPEN hOldPen2 = SelectObject(hdc, hRedPen);
275 POINT points[13];
276 int x, y;
277 RECT day_rect;
280 MONTHCAL_CalcPosFromDay(infoPtr, day, month, &day_rect);
282 x = day_rect.left;
283 y = day_rect.top;
285 points[0].x = x;
286 points[0].y = y - 1;
287 points[1].x = x + 0.8 * infoPtr->width_increment;
288 points[1].y = y - 1;
289 points[2].x = x + 0.9 * infoPtr->width_increment;
290 points[2].y = y;
291 points[3].x = x + infoPtr->width_increment;
292 points[3].y = y + 0.5 * infoPtr->height_increment;
294 points[4].x = x + infoPtr->width_increment;
295 points[4].y = y + 0.9 * infoPtr->height_increment;
296 points[5].x = x + 0.6 * infoPtr->width_increment;
297 points[5].y = y + 0.9 * infoPtr->height_increment;
298 points[6].x = x + 0.5 * infoPtr->width_increment;
299 points[6].y = y + 0.9 * infoPtr->height_increment; /* bring the bottom up just
300 a hair to fit inside the day rectangle */
302 points[7].x = x + 0.2 * infoPtr->width_increment;
303 points[7].y = y + 0.8 * infoPtr->height_increment;
304 points[8].x = x + 0.1 * infoPtr->width_increment;
305 points[8].y = y + 0.8 * infoPtr->height_increment;
306 points[9].x = x;
307 points[9].y = y + 0.5 * infoPtr->height_increment;
309 points[10].x = x + 0.1 * infoPtr->width_increment;
310 points[10].y = y + 0.2 * infoPtr->height_increment;
311 points[11].x = x + 0.2 * infoPtr->width_increment;
312 points[11].y = y + 0.3 * infoPtr->height_increment;
313 points[12].x = x + 0.4 * infoPtr->width_increment;
314 points[12].y = y + 0.2 * infoPtr->height_increment;
316 PolyBezier(hdc, points, 13);
317 DeleteObject(hRedPen);
318 SelectObject(hdc, hOldPen2);
322 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month,
323 int x, int y, int bold)
325 static const WCHAR fmtW[] = { '%','d',0 };
326 WCHAR buf[10];
327 RECT r;
328 static int haveBoldFont, haveSelectedDay = FALSE;
329 HBRUSH hbr;
330 COLORREF oldCol = 0;
331 COLORREF oldBk = 0;
333 wsprintfW(buf, fmtW, day);
335 /* No need to check styles: when selection is not valid, it is set to zero.
336 * 1<day<31, so everything is OK.
339 MONTHCAL_CalcDayRect(infoPtr, &r, x, y);
341 if((day>=infoPtr->minSel.wDay) && (day<=infoPtr->maxSel.wDay)
342 && (month==infoPtr->currentMonth)) {
343 HRGN hrgn;
344 RECT r2;
346 TRACE("%d %d %d\n",day, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
347 TRACE("%s\n", wine_dbgstr_rect(&r));
348 oldCol = SetTextColor(hdc, infoPtr->monthbk);
349 oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
350 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
351 hrgn = CreateEllipticRgn(r.left, r.top, r.right, r.bottom);
352 FillRgn(hdc, hrgn, hbr);
354 /* FIXME: this may need to be changed now b/c of the other
355 drawing changes 11/3/99 CMM */
356 r2.left = r.left - 0.25 * infoPtr->textWidth;
357 r2.top = r.top;
358 r2.right = r.left + 0.5 * infoPtr->textWidth;
359 r2.bottom = r.bottom;
360 if(haveSelectedDay) FillRect(hdc, &r2, hbr);
361 haveSelectedDay = TRUE;
362 } else {
363 haveSelectedDay = FALSE;
366 /* need to add some code for multiple selections */
368 if((bold) &&(!haveBoldFont)) {
369 SelectObject(hdc, infoPtr->hBoldFont);
370 haveBoldFont = TRUE;
372 if((!bold) &&(haveBoldFont)) {
373 SelectObject(hdc, infoPtr->hFont);
374 haveBoldFont = FALSE;
377 if(haveSelectedDay) {
378 SetTextColor(hdc, oldCol);
379 SetBkColor(hdc, oldBk);
382 SetBkMode(hdc,TRANSPARENT);
383 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
385 /* draw a rectangle around the currently selected days text */
386 if((day==infoPtr->curSelDay) && (month==infoPtr->currentMonth))
387 DrawFocusRect(hdc, &r);
391 static void paint_button (const MONTHCAL_INFO *infoPtr, HDC hdc, BOOL btnNext,
392 BOOL pressed, RECT* r)
394 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
396 if (theme)
398 static const int states[] = {
399 /* Prev button */
400 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
401 /* Next button */
402 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
404 int stateNum = btnNext ? 3 : 0;
405 if (pressed)
406 stateNum += 1;
407 else
409 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
410 if (dwStyle & WS_DISABLED) stateNum += 2;
412 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
414 else
416 int style = btnNext ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
417 if (pressed)
418 style |= DFCS_PUSHED;
419 else
421 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
422 if (dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
425 DrawFrameControl(hdc, r, DFC_SCROLL, style);
430 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
432 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
433 static const WCHAR fmt1W[] = { '%','s',' ','%','l','d',0 };
434 static const WCHAR fmt2W[] = { '%','s',' ','%','s',0 };
435 static const WCHAR fmt3W[] = { '%','d',0 };
436 RECT *title=&infoPtr->title;
437 RECT *prev=&infoPtr->titlebtnprev;
438 RECT *next=&infoPtr->titlebtnnext;
439 RECT *titlemonth=&infoPtr->titlemonth;
440 RECT *titleyear=&infoPtr->titleyear;
441 RECT dayrect;
442 RECT *days=&dayrect;
443 RECT rtoday;
444 int i, j, m, mask, day, firstDay, weeknum, weeknum1,prevMonth;
445 int textHeight = infoPtr->textHeight;
446 SIZE size;
447 HBRUSH hbr;
448 HFONT currentFont;
449 WCHAR buf[20];
450 WCHAR buf1[20];
451 WCHAR buf2[32];
452 COLORREF oldTextColor, oldBkColor;
453 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
454 RECT rcTemp;
455 RECT rcDay; /* used in MONTHCAL_CalcDayRect() */
456 SYSTEMTIME localtime;
457 int startofprescal;
459 oldTextColor = SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
461 /* fill background */
462 hbr = CreateSolidBrush (infoPtr->bk);
463 FillRect(hdc, &ps->rcPaint, hbr);
464 DeleteObject(hbr);
466 /* draw header */
467 if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
469 hbr = CreateSolidBrush(infoPtr->titlebk);
470 FillRect(hdc, title, hbr);
471 DeleteObject(hbr);
474 /* if the previous button is pressed draw it depressed */
475 if(IntersectRect(&rcTemp, &(ps->rcPaint), prev))
476 paint_button (infoPtr, hdc, FALSE, infoPtr->status & MC_PREVPRESSED, prev);
478 /* if next button is depressed draw it depressed */
479 if(IntersectRect(&rcTemp, &(ps->rcPaint), next))
480 paint_button (infoPtr, hdc, TRUE, infoPtr->status & MC_NEXTPRESSED, next);
482 oldBkColor = SetBkColor(hdc, infoPtr->titlebk);
483 SetTextColor(hdc, infoPtr->titletxt);
484 currentFont = SelectObject(hdc, infoPtr->hBoldFont);
486 GetLocaleInfoW( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+infoPtr->currentMonth -1,
487 buf1,countof(buf1));
488 wsprintfW(buf, fmt1W, buf1, infoPtr->currentYear);
490 if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
492 DrawTextW(hdc, buf, strlenW(buf), title,
493 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
496 /* titlemonth left/right contained rect for whole titletxt('June 1999')
497 * MCM_HitTestInfo wants month & year rects, so prepare these now.
498 *(no, we can't draw them separately; the whole text is centered)
500 GetTextExtentPoint32W(hdc, buf, strlenW(buf), &size);
501 titlemonth->left = title->right / 2 + title->left / 2 - size.cx / 2;
502 titleyear->right = title->right / 2 + title->left / 2 + size.cx / 2;
503 GetTextExtentPoint32W(hdc, buf1, strlenW(buf1), &size);
504 titlemonth->right = titlemonth->left + size.cx;
505 titleyear->left = titlemonth->right;
507 /* draw month area */
508 rcTemp.top=infoPtr->wdays.top;
509 rcTemp.left=infoPtr->wdays.left;
510 rcTemp.bottom=infoPtr->todayrect.bottom;
511 rcTemp.right =infoPtr->todayrect.right;
512 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcTemp))
514 hbr = CreateSolidBrush(infoPtr->monthbk);
515 FillRect(hdc, &rcTemp, hbr);
516 DeleteObject(hbr);
519 /* draw line under day abbreviations */
521 MoveToEx(hdc, infoPtr->days.left + 3, title->bottom + textHeight + 1, NULL);
522 LineTo(hdc, infoPtr->days.right - 3, title->bottom + textHeight + 1);
524 prevMonth = infoPtr->currentMonth - 1;
525 if(prevMonth == 0) /* if currentMonth is january(1) prevMonth is */
526 prevMonth = 12; /* december(12) of the previous year */
528 infoPtr->wdays.left = infoPtr->days.left = infoPtr->weeknums.right;
529 /* draw day abbreviations */
531 SelectObject(hdc, infoPtr->hFont);
532 SetBkColor(hdc, infoPtr->monthbk);
533 SetTextColor(hdc, infoPtr->trailingtxt);
535 /* copy this rect so we can change the values without changing */
536 /* the original version */
537 days->left = infoPtr->wdays.left;
538 days->right = days->left + infoPtr->width_increment;
539 days->top = infoPtr->wdays.top;
540 days->bottom = infoPtr->wdays.bottom;
542 i = infoPtr->firstDay;
544 for(j=0; j<7; j++) {
545 GetLocaleInfoW( LOCALE_USER_DEFAULT,LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
546 DrawTextW(hdc, buf, strlenW(buf), days, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
547 days->left+=infoPtr->width_increment;
548 days->right+=infoPtr->width_increment;
551 /* draw day numbers; first, the previous month */
553 firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear);
555 day = MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear) +
556 (infoPtr->firstDay + 7 - firstDay)%7 + 1;
557 if (day > MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear))
558 day -=7;
559 startofprescal = day;
560 mask = 1<<(day-1);
562 i = 0;
563 m = 0;
564 while(day <= MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear)) {
565 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
566 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
568 MONTHCAL_DrawDay(infoPtr, hdc, day, prevMonth, i, 0,
569 infoPtr->monthdayState[m] & mask);
572 mask<<=1;
573 day++;
574 i++;
577 /* draw `current' month */
579 day = 1; /* start at the beginning of the current month */
581 infoPtr->firstDayplace = i;
582 SetTextColor(hdc, infoPtr->txt);
583 m++;
584 mask = 1;
586 /* draw the first week of the current month */
587 while(i<7) {
588 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
589 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
592 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth, i, 0,
593 infoPtr->monthdayState[m] & mask);
595 if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
596 (day==infoPtr->todaysDate.wDay) &&
597 (infoPtr->currentYear == infoPtr->todaysDate.wYear)) {
598 if(!(dwStyle & MCS_NOTODAYCIRCLE))
599 MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->currentMonth);
603 mask<<=1;
604 day++;
605 i++;
608 j = 1; /* move to the 2nd week of the current month */
609 i = 0; /* move back to sunday */
610 while(day <= MONTHCAL_MonthLength(infoPtr->currentMonth, infoPtr->currentYear)) {
611 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
612 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
614 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth, i, j,
615 infoPtr->monthdayState[m] & mask);
617 if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
618 (day==infoPtr->todaysDate.wDay) &&
619 (infoPtr->currentYear == infoPtr->todaysDate.wYear))
620 if(!(dwStyle & MCS_NOTODAYCIRCLE))
621 MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->currentMonth);
623 mask<<=1;
624 day++;
625 i++;
626 if(i>6) { /* past saturday, goto the next weeks sunday */
627 i = 0;
628 j++;
632 /* draw `next' month */
634 day = 1; /* start at the first day of the next month */
635 m++;
636 mask = 1;
638 SetTextColor(hdc, infoPtr->trailingtxt);
639 while((i<7) &&(j<6)) {
640 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
641 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
643 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth + 1, i, j,
644 infoPtr->monthdayState[m] & mask);
647 mask<<=1;
648 day++;
649 i++;
650 if(i==7) { /* past saturday, go to next week's sunday */
651 i = 0;
652 j++;
655 SetTextColor(hdc, infoPtr->txt);
658 /* draw `today' date if style allows it, and draw a circle before today's
659 * date if necessary */
661 if(!(dwStyle & MCS_NOTODAY)) {
662 if(!(dwStyle & MCS_NOTODAYCIRCLE)) {
663 /*day is the number of days from nextmonth we put on the calendar */
664 MONTHCAL_CircleDay(infoPtr, hdc,
665 day+MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear),
666 infoPtr->currentMonth);
668 if (!LoadStringW(COMCTL32_hModule,IDM_TODAY,buf1,countof(buf1)))
670 WARN("Can't load resource\n");
671 strcpyW(buf1, todayW);
673 MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
674 MONTHCAL_CopyTime(&infoPtr->todaysDate,&localtime);
675 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&localtime,NULL,buf2,countof(buf2));
676 wsprintfW(buf, fmt2W, buf1, buf2);
677 SelectObject(hdc, infoPtr->hBoldFont);
679 DrawTextW(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
680 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rtoday))
682 DrawTextW(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
684 SelectObject(hdc, infoPtr->hFont);
687 /*eventually draw week numbers*/
688 if(dwStyle & MCS_WEEKNUMBERS) {
689 /* display weeknumbers*/
690 int mindays;
692 /* Rules what week to call the first week of a new year:
693 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
694 The week containing Jan 1 is the first week of year
695 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
696 First week of year must contain 4 days of the new year
697 LOCALE_IFIRSTWEEKOFYEAR == 1 (what contries?)
698 The first week of the year must contain only days of the new year
700 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
701 weeknum = atoiW(buf);
702 switch (weeknum)
704 case 1: mindays = 6;
705 break;
706 case 2: mindays = 3;
707 break;
708 case 0:
709 default:
710 mindays = 0;
712 if (infoPtr->currentMonth < 2)
714 /* calculate all those exceptions for january */
715 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear);
716 if ((infoPtr->firstDay +7 - weeknum1)%7 > mindays)
717 weeknum =1;
718 else
720 weeknum = 0;
721 for(i=0; i<11; i++)
722 weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->currentYear-1);
723 weeknum +=startofprescal+ 7;
724 weeknum /=7;
725 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear-1);
726 if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
727 weeknum++;
730 else
732 weeknum = 0;
733 for(i=0; i<prevMonth-1; i++)
734 weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->currentYear);
735 weeknum +=startofprescal+ 7;
736 weeknum /=7;
737 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear);
738 if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
739 weeknum++;
741 days->left = infoPtr->weeknums.left;
742 days->right = infoPtr->weeknums.right;
743 days->top = infoPtr->weeknums.top;
744 days->bottom = days->top +infoPtr->height_increment;
745 for(i=0; i<6; i++) {
746 if((i==0)&&(weeknum>50))
748 wsprintfW(buf, fmt3W, weeknum);
749 weeknum=0;
751 else if((i==5)&&(weeknum>47))
753 wsprintfW(buf, fmt3W, 1);
755 else
756 wsprintfW(buf, fmt3W, weeknum + i);
757 DrawTextW(hdc, buf, -1, days, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
758 days->top+=infoPtr->height_increment;
759 days->bottom+=infoPtr->height_increment;
762 MoveToEx(hdc, infoPtr->weeknums.right, infoPtr->weeknums.top + 3 , NULL);
763 LineTo(hdc, infoPtr->weeknums.right, infoPtr->weeknums.bottom );
766 /* currentFont was font at entering Refresh */
768 SetBkColor(hdc, oldBkColor);
769 SelectObject(hdc, currentFont);
770 SetTextColor(hdc, oldTextColor);
774 static LRESULT
775 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, LPARAM lParam)
777 LPRECT lpRect = (LPRECT) lParam;
779 TRACE("rect %p\n", lpRect);
781 /* validate parameters */
783 if((infoPtr==NULL) ||(lpRect == NULL) ) return FALSE;
785 lpRect->left = infoPtr->title.left;
786 lpRect->top = infoPtr->title.top;
787 lpRect->right = infoPtr->title.right;
788 lpRect->bottom = infoPtr->todayrect.bottom;
789 AdjustWindowRect(lpRect, GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE), FALSE);
791 TRACE("%s\n", wine_dbgstr_rect(lpRect));
793 return TRUE;
797 static LRESULT
798 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, WPARAM wParam)
800 TRACE("\n");
802 switch((int)wParam) {
803 case MCSC_BACKGROUND:
804 return infoPtr->bk;
805 case MCSC_TEXT:
806 return infoPtr->txt;
807 case MCSC_TITLEBK:
808 return infoPtr->titlebk;
809 case MCSC_TITLETEXT:
810 return infoPtr->titletxt;
811 case MCSC_MONTHBK:
812 return infoPtr->monthbk;
813 case MCSC_TRAILINGTEXT:
814 return infoPtr->trailingtxt;
817 return -1;
821 static LRESULT
822 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
824 int prev = -1;
826 TRACE("%ld: color %08lx\n", wParam, lParam);
828 switch((int)wParam) {
829 case MCSC_BACKGROUND:
830 prev = infoPtr->bk;
831 infoPtr->bk = (COLORREF)lParam;
832 break;
833 case MCSC_TEXT:
834 prev = infoPtr->txt;
835 infoPtr->txt = (COLORREF)lParam;
836 break;
837 case MCSC_TITLEBK:
838 prev = infoPtr->titlebk;
839 infoPtr->titlebk = (COLORREF)lParam;
840 break;
841 case MCSC_TITLETEXT:
842 prev=infoPtr->titletxt;
843 infoPtr->titletxt = (COLORREF)lParam;
844 break;
845 case MCSC_MONTHBK:
846 prev = infoPtr->monthbk;
847 infoPtr->monthbk = (COLORREF)lParam;
848 break;
849 case MCSC_TRAILINGTEXT:
850 prev = infoPtr->trailingtxt;
851 infoPtr->trailingtxt = (COLORREF)lParam;
852 break;
855 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
856 return prev;
860 static LRESULT
861 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
863 TRACE("\n");
865 if(infoPtr->delta)
866 return infoPtr->delta;
867 else
868 return infoPtr->visible;
872 static LRESULT
873 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, WPARAM wParam)
875 int prev = infoPtr->delta;
877 TRACE("delta %ld\n", wParam);
879 infoPtr->delta = (int)wParam;
880 return prev;
884 static LRESULT
885 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
887 return MAKELONG(infoPtr->firstDay, infoPtr->firstDayHighWord);
891 /* sets the first day of the week that will appear in the control */
892 /* 0 == Sunday, 6 == Saturday */
893 /* FIXME: this needs to be implemented properly in MONTHCAL_Refresh() */
894 /* FIXME: we need more error checking here */
895 static LRESULT
896 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, LPARAM lParam)
898 int prev = MAKELONG(infoPtr->firstDay, infoPtr->firstDayHighWord);
899 int localFirstDay;
900 WCHAR buf[40];
902 TRACE("day %ld\n", lParam);
904 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
905 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
907 localFirstDay = atoiW(buf);
909 if(lParam == -1)
911 infoPtr->firstDay = localFirstDay;
912 infoPtr->firstDayHighWord = FALSE;
914 else if(lParam >= 7)
916 infoPtr->firstDay = 6; /* max first day allowed */
917 infoPtr->firstDayHighWord = TRUE;
919 else
921 infoPtr->firstDay = lParam;
922 infoPtr->firstDayHighWord = TRUE;
925 return prev;
929 static LRESULT
930 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr)
932 TRACE("\n");
934 return infoPtr->monthRange;
938 static LRESULT
939 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
941 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
945 static LRESULT
946 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
948 SYSTEMTIME *lprgSysTimeArray=(SYSTEMTIME *)lParam;
949 FILETIME ft_min, ft_max;
951 TRACE("%lx %lx\n", wParam, lParam);
953 if ((wParam & GDTR_MIN && !MONTHCAL_ValidateTime(lprgSysTimeArray[0])) ||
954 (wParam & GDTR_MAX && !MONTHCAL_ValidateTime(lprgSysTimeArray[1])))
955 return FALSE;
957 if (wParam & GDTR_MIN)
959 MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minDate);
960 infoPtr->rangeValid |= GDTR_MIN;
962 if (wParam & GDTR_MAX)
964 MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxDate);
965 infoPtr->rangeValid |= GDTR_MAX;
968 /* Only one limit set - we are done */
969 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
970 return TRUE;
972 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
973 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
975 if (CompareFileTime(&ft_min, &ft_max) > 0)
977 if ((wParam & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
979 /* Native swaps limits only when both limits are being set. */
980 SYSTEMTIME st_tmp = infoPtr->minDate;
981 infoPtr->minDate = infoPtr->maxDate;
982 infoPtr->maxDate = st_tmp;
984 else
986 /* Reset the other limit. */
987 /* FIXME: native sets date&time to 0. Should we do this too? */
988 infoPtr->rangeValid &= wParam & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN ;
992 return TRUE;
996 static LRESULT
997 MONTHCAL_GetRange(HWND hwnd, LPARAM lParam)
999 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1000 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *)lParam;
1002 /* validate parameters */
1004 if((infoPtr==NULL) || (lprgSysTimeArray==NULL)) return FALSE;
1006 MONTHCAL_CopyTime(&infoPtr->maxDate, &lprgSysTimeArray[1]);
1007 MONTHCAL_CopyTime(&infoPtr->minDate, &lprgSysTimeArray[0]);
1009 return infoPtr->rangeValid;
1013 static LRESULT
1014 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1017 int i, iMonths = (int)wParam;
1018 MONTHDAYSTATE *dayStates = (LPMONTHDAYSTATE)lParam;
1020 TRACE("%lx %lx\n", wParam, lParam);
1021 if(iMonths!=infoPtr->monthRange) return 0;
1023 for(i=0; i<iMonths; i++)
1024 infoPtr->monthdayState[i] = dayStates[i];
1025 return 1;
1028 static LRESULT
1029 MONTHCAL_GetCurSel(const 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 MONTHCAL_CopyTime(&infoPtr->minSel, lpSel);
1038 TRACE("%d/%d/%d\n", lpSel->wYear, lpSel->wMonth, lpSel->wDay);
1039 return TRUE;
1042 /* FIXME: if the specified date is not visible, make it visible */
1043 /* FIXME: redraw? */
1044 static LRESULT
1045 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1047 SYSTEMTIME *lpSel = (SYSTEMTIME *)lParam;
1049 TRACE("%lx\n", lParam);
1050 if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
1051 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
1053 if(!MONTHCAL_ValidateTime(*lpSel)) return FALSE;
1055 infoPtr->currentMonth=lpSel->wMonth;
1056 infoPtr->currentYear=lpSel->wYear;
1058 MONTHCAL_CopyTime(lpSel, &infoPtr->minSel);
1059 MONTHCAL_CopyTime(lpSel, &infoPtr->maxSel);
1061 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1063 return TRUE;
1067 static LRESULT
1068 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1070 return infoPtr->maxSelCount;
1074 static LRESULT
1075 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1077 TRACE("%lx\n", wParam);
1079 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) {
1080 infoPtr->maxSelCount = wParam;
1083 return TRUE;
1087 static LRESULT
1088 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, LPARAM lParam)
1090 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1092 TRACE("%lx\n", lParam);
1094 /* validate parameters */
1096 if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1098 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)
1100 MONTHCAL_CopyTime(&infoPtr->maxSel, &lprgSysTimeArray[1]);
1101 MONTHCAL_CopyTime(&infoPtr->minSel, &lprgSysTimeArray[0]);
1102 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1103 return TRUE;
1106 return FALSE;
1110 static LRESULT
1111 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1113 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1115 TRACE("%lx\n", lParam);
1117 /* validate parameters */
1119 if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1121 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)
1123 MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxSel);
1124 MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minSel);
1125 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1126 return TRUE;
1129 return FALSE;
1133 static LRESULT
1134 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, LPARAM lParam)
1136 SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1138 TRACE("%lx\n", lParam);
1140 /* validate parameters */
1142 if((infoPtr==NULL) || (lpToday==NULL)) return FALSE;
1143 MONTHCAL_CopyTime(&infoPtr->todaysDate, lpToday);
1144 return TRUE;
1148 static LRESULT
1149 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1151 SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1153 TRACE("%lx\n", lParam);
1155 /* validate parameters */
1157 if((infoPtr==NULL) ||(lpToday==NULL)) return FALSE;
1158 MONTHCAL_CopyTime(lpToday, &infoPtr->todaysDate);
1159 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1160 return TRUE;
1164 static LRESULT
1165 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, LPARAM lParam)
1167 PMCHITTESTINFO lpht = (PMCHITTESTINFO)lParam;
1168 UINT x,y;
1169 DWORD retval;
1170 int day,wday,wnum;
1173 x = lpht->pt.x;
1174 y = lpht->pt.y;
1175 retval = MCHT_NOWHERE;
1177 ZeroMemory(&lpht->st, sizeof(lpht->st));
1179 /* Comment in for debugging...
1180 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,
1181 infoPtr->wdays.left, infoPtr->wdays.right,
1182 infoPtr->wdays.top, infoPtr->wdays.bottom,
1183 infoPtr->days.left, infoPtr->days.right,
1184 infoPtr->days.top, infoPtr->days.bottom,
1185 infoPtr->todayrect.left, infoPtr->todayrect.right,
1186 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1187 infoPtr->weeknums.left, infoPtr->weeknums.right,
1188 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1191 /* are we in the header? */
1193 if(PtInRect(&infoPtr->title, lpht->pt)) {
1194 if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1195 retval = MCHT_TITLEBTNPREV;
1196 goto done;
1198 if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1199 retval = MCHT_TITLEBTNNEXT;
1200 goto done;
1202 if(PtInRect(&infoPtr->titlemonth, lpht->pt)) {
1203 retval = MCHT_TITLEMONTH;
1204 goto done;
1206 if(PtInRect(&infoPtr->titleyear, lpht->pt)) {
1207 retval = MCHT_TITLEYEAR;
1208 goto done;
1211 retval = MCHT_TITLE;
1212 goto done;
1215 day = MONTHCAL_CalcDayFromPos(infoPtr,x,y,&wday,&wnum);
1216 if(PtInRect(&infoPtr->wdays, lpht->pt)) {
1217 retval = MCHT_CALENDARDAY;
1218 lpht->st.wYear = infoPtr->currentYear;
1219 lpht->st.wMonth = (day < 1)? infoPtr->currentMonth -1 : infoPtr->currentMonth;
1220 lpht->st.wDay = (day < 1)?
1221 MONTHCAL_MonthLength(infoPtr->currentMonth-1,infoPtr->currentYear) -day : day;
1222 goto done;
1224 if(PtInRect(&infoPtr->weeknums, lpht->pt)) {
1225 retval = MCHT_CALENDARWEEKNUM;
1226 lpht->st.wYear = infoPtr->currentYear;
1227 lpht->st.wMonth = (day < 1) ? infoPtr->currentMonth -1 :
1228 (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear)) ?
1229 infoPtr->currentMonth +1 :infoPtr->currentMonth;
1230 lpht->st.wDay = (day < 1 ) ?
1231 MONTHCAL_MonthLength(infoPtr->currentMonth-1,infoPtr->currentYear) -day :
1232 (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear)) ?
1233 day - MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear) : day;
1234 goto done;
1236 if(PtInRect(&infoPtr->days, lpht->pt))
1238 lpht->st.wYear = infoPtr->currentYear;
1239 if ( day < 1)
1241 retval = MCHT_CALENDARDATEPREV;
1242 lpht->st.wMonth = infoPtr->currentMonth - 1;
1243 if (lpht->st.wMonth <1)
1245 lpht->st.wMonth = 12;
1246 lpht->st.wYear--;
1248 lpht->st.wDay = MONTHCAL_MonthLength(lpht->st.wMonth,lpht->st.wYear) -day;
1250 else if (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear))
1252 retval = MCHT_CALENDARDATENEXT;
1253 lpht->st.wMonth = infoPtr->currentMonth + 1;
1254 if (lpht->st.wMonth <12)
1256 lpht->st.wMonth = 1;
1257 lpht->st.wYear++;
1259 lpht->st.wDay = day - MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear) ;
1261 else {
1262 retval = MCHT_CALENDARDATE;
1263 lpht->st.wMonth = infoPtr->currentMonth;
1264 lpht->st.wDay = day;
1265 lpht->st.wDayOfWeek = MONTHCAL_CalculateDayOfWeek(day,lpht->st.wMonth,lpht->st.wYear);
1267 goto done;
1269 if(PtInRect(&infoPtr->todayrect, lpht->pt)) {
1270 retval = MCHT_TODAYLINK;
1271 goto done;
1275 /* Hit nothing special? What's left must be background :-) */
1277 retval = MCHT_CALENDARBK;
1278 done:
1279 lpht->uHit = retval;
1280 return retval;
1284 static void MONTHCAL_GoToNextMonth(MONTHCAL_INFO *infoPtr)
1286 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1288 TRACE("MONTHCAL_GoToNextMonth\n");
1290 infoPtr->currentMonth++;
1291 if(infoPtr->currentMonth > 12) {
1292 infoPtr->currentYear++;
1293 infoPtr->currentMonth = 1;
1296 if(dwStyle & MCS_DAYSTATE) {
1297 NMDAYSTATE nmds;
1298 int i;
1300 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1301 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1302 nmds.nmhdr.code = MCN_GETDAYSTATE;
1303 nmds.cDayState = infoPtr->monthRange;
1304 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1306 nmds.stStart = infoPtr->todaysDate;
1307 nmds.stStart.wYear = infoPtr->currentYear;
1308 nmds.stStart.wMonth = infoPtr->currentMonth;
1309 nmds.stStart.wDay = 1;
1311 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1312 for(i=0; i<infoPtr->monthRange; i++)
1313 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1318 static void MONTHCAL_GoToPrevMonth(MONTHCAL_INFO *infoPtr)
1320 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1322 TRACE("\n");
1324 infoPtr->currentMonth--;
1325 if(infoPtr->currentMonth < 1) {
1326 infoPtr->currentYear--;
1327 infoPtr->currentMonth = 12;
1330 if(dwStyle & MCS_DAYSTATE) {
1331 NMDAYSTATE nmds;
1332 int i;
1334 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1335 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1336 nmds.nmhdr.code = MCN_GETDAYSTATE;
1337 nmds.cDayState = infoPtr->monthRange;
1338 nmds.prgDayState = Alloc
1339 (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1341 nmds.stStart = infoPtr->todaysDate;
1342 nmds.stStart.wYear = infoPtr->currentYear;
1343 nmds.stStart.wMonth = infoPtr->currentMonth;
1344 nmds.stStart.wDay = 1;
1346 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1347 for(i=0; i<infoPtr->monthRange; i++)
1348 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1352 static LRESULT
1353 MONTHCAL_RButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1355 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1356 HMENU hMenu;
1357 POINT menupoint;
1358 WCHAR buf[32];
1360 hMenu = CreatePopupMenu();
1361 if (!LoadStringW(COMCTL32_hModule,IDM_GOTODAY,buf,countof(buf)))
1363 WARN("Can't load resource\n");
1364 strcpyW(buf, todayW);
1366 AppendMenuW(hMenu, MF_STRING|MF_ENABLED,1, buf);
1367 menupoint.x=(short)LOWORD(lParam);
1368 menupoint.y=(short)HIWORD(lParam);
1369 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1370 if( TrackPopupMenu(hMenu,TPM_RIGHTBUTTON| TPM_NONOTIFY|TPM_RETURNCMD,
1371 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1373 infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
1374 infoPtr->currentYear=infoPtr->todaysDate.wYear;
1375 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1377 return 0;
1380 static LRESULT
1381 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1383 static const WCHAR EditW[] = { 'E','D','I','T',0 };
1384 MCHITTESTINFO ht;
1385 DWORD hit;
1386 HMENU hMenu;
1387 RECT rcDay; /* used in determining area to invalidate */
1388 WCHAR buf[32];
1389 int i;
1390 POINT menupoint;
1392 TRACE("%lx\n", lParam);
1394 if (infoPtr->hWndYearUpDown)
1396 infoPtr->currentYear=SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, 0);
1397 if(!DestroyWindow(infoPtr->hWndYearUpDown))
1399 FIXME("Can't destroy Updown Control\n");
1401 else
1402 infoPtr->hWndYearUpDown=0;
1403 if(!DestroyWindow(infoPtr->hWndYearEdit))
1405 FIXME("Can't destroy Updown Control\n");
1407 else
1408 infoPtr->hWndYearEdit=0;
1409 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1412 ht.pt.x = (short)LOWORD(lParam);
1413 ht.pt.y = (short)HIWORD(lParam);
1414 hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1416 /* FIXME: these flags should be checked by */
1417 /*((hit & MCHT_XXX) == MCHT_XXX) b/c some of the flags are */
1418 /* multi-bit */
1419 if(hit ==MCHT_TITLEBTNNEXT) {
1420 MONTHCAL_GoToNextMonth(infoPtr);
1421 infoPtr->status = MC_NEXTPRESSED;
1422 SetTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1423 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1424 return 0;
1426 if(hit == MCHT_TITLEBTNPREV){
1427 MONTHCAL_GoToPrevMonth(infoPtr);
1428 infoPtr->status = MC_PREVPRESSED;
1429 SetTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1430 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1431 return 0;
1434 if(hit == MCHT_TITLEMONTH) {
1435 hMenu = CreatePopupMenu();
1437 for (i=0; i<12;i++)
1439 GetLocaleInfoW(LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+i, buf,countof(buf));
1440 AppendMenuW(hMenu, MF_STRING|MF_ENABLED,i+1, buf);
1442 menupoint.x=infoPtr->titlemonth.right;
1443 menupoint.y=infoPtr->titlemonth.bottom;
1444 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1445 i= TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
1446 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
1447 if ((i>0) && (i<13))
1449 infoPtr->currentMonth=i;
1450 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1453 if(hit == MCHT_TITLEYEAR) {
1454 infoPtr->hWndYearEdit=CreateWindowExW(0,
1455 EditW,
1457 WS_VISIBLE | WS_CHILD |UDS_SETBUDDYINT,
1458 infoPtr->titleyear.left+3,infoPtr->titlebtnnext.top,
1459 infoPtr->titleyear.right-infoPtr->titleyear.left+4,
1460 infoPtr->textHeight,
1461 infoPtr->hwndSelf,
1462 NULL,
1463 NULL,
1464 NULL);
1465 SendMessageW( infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM) infoPtr->hBoldFont, (LPARAM)TRUE);
1466 infoPtr->hWndYearUpDown=CreateWindowExW(0,
1467 UPDOWN_CLASSW,
1469 WS_VISIBLE | WS_CHILD |UDS_SETBUDDYINT|UDS_NOTHOUSANDS|UDS_ARROWKEYS,
1470 infoPtr->titleyear.right+7,infoPtr->titlebtnnext.top,
1472 infoPtr->textHeight,
1473 infoPtr->hwndSelf,
1474 NULL,
1475 NULL,
1476 NULL);
1477 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0, MAKELONG (9999, 1753));
1478 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM) infoPtr->hWndYearEdit, 0);
1479 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->currentYear);
1480 return 0;
1483 if(hit == MCHT_TODAYLINK) {
1484 NMSELCHANGE nmsc;
1486 infoPtr->curSelDay = infoPtr->todaysDate.wDay;
1487 infoPtr->firstSelDay = infoPtr->todaysDate.wDay;
1488 infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
1489 infoPtr->currentYear=infoPtr->todaysDate.wYear;
1490 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->minSel);
1491 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxSel);
1492 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1494 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1495 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1496 nmsc.nmhdr.code = MCN_SELCHANGE;
1497 MONTHCAL_CopyTime(&infoPtr->minSel, &nmsc.stSelStart);
1498 MONTHCAL_CopyTime(&infoPtr->maxSel, &nmsc.stSelEnd);
1499 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1501 nmsc.nmhdr.code = MCN_SELECT;
1502 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1503 return 0;
1505 if(hit == MCHT_CALENDARDATE) {
1506 SYSTEMTIME selArray[2];
1507 NMSELCHANGE nmsc;
1509 MONTHCAL_CopyTime(&ht.st, &selArray[0]);
1510 MONTHCAL_CopyTime(&ht.st, &selArray[1]);
1511 MONTHCAL_SetSelRange(infoPtr, (LPARAM)selArray);
1512 MONTHCAL_SetCurSel(infoPtr, (LPARAM)selArray);
1513 TRACE("MCHT_CALENDARDATE\n");
1514 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1515 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1516 nmsc.nmhdr.code = MCN_SELCHANGE;
1517 MONTHCAL_CopyTime(&infoPtr->minSel,&nmsc.stSelStart);
1518 MONTHCAL_CopyTime(&infoPtr->maxSel,&nmsc.stSelEnd);
1520 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1523 /* redraw both old and new days if the selected day changed */
1524 if(infoPtr->curSelDay != ht.st.wDay) {
1525 MONTHCAL_CalcPosFromDay(infoPtr, ht.st.wDay, ht.st.wMonth, &rcDay);
1526 InvalidateRect(infoPtr->hwndSelf, &rcDay, TRUE);
1528 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->curSelDay, infoPtr->currentMonth, &rcDay);
1529 InvalidateRect(infoPtr->hwndSelf, &rcDay, TRUE);
1532 infoPtr->firstSelDay = ht.st.wDay;
1533 infoPtr->curSelDay = ht.st.wDay;
1534 infoPtr->status = MC_SEL_LBUTDOWN;
1535 return 0;
1538 return 1;
1542 static LRESULT
1543 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1545 NMSELCHANGE nmsc;
1546 NMHDR nmhdr;
1547 BOOL redraw = FALSE;
1548 MCHITTESTINFO ht;
1549 DWORD hit;
1551 TRACE("\n");
1553 if(infoPtr->status & MC_NEXTPRESSED) {
1554 KillTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER);
1555 infoPtr->status &= ~MC_NEXTPRESSED;
1556 redraw = TRUE;
1558 if(infoPtr->status & MC_PREVPRESSED) {
1559 KillTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER);
1560 infoPtr->status &= ~MC_PREVPRESSED;
1561 redraw = TRUE;
1564 ht.pt.x = (short)LOWORD(lParam);
1565 ht.pt.y = (short)HIWORD(lParam);
1566 hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1568 infoPtr->status = MC_SEL_LBUTUP;
1570 if(hit ==MCHT_CALENDARDATENEXT) {
1571 MONTHCAL_GoToNextMonth(infoPtr);
1572 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1573 return TRUE;
1575 if(hit == MCHT_CALENDARDATEPREV){
1576 MONTHCAL_GoToPrevMonth(infoPtr);
1577 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1578 return TRUE;
1580 nmhdr.hwndFrom = infoPtr->hwndSelf;
1581 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1582 nmhdr.code = NM_RELEASEDCAPTURE;
1583 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
1585 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
1586 /* redraw if necessary */
1587 if(redraw)
1588 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1589 /* only send MCN_SELECT if currently displayed month's day was selected */
1590 if(hit == MCHT_CALENDARDATE) {
1591 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1592 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1593 nmsc.nmhdr.code = MCN_SELECT;
1594 MONTHCAL_CopyTime(&infoPtr->minSel, &nmsc.stSelStart);
1595 MONTHCAL_CopyTime(&infoPtr->maxSel, &nmsc.stSelEnd);
1597 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1600 return 0;
1604 static LRESULT
1605 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1607 BOOL redraw = FALSE;
1609 TRACE("%ld\n", wParam);
1611 switch(wParam) {
1612 case MC_NEXTMONTHTIMER:
1613 redraw = TRUE;
1614 MONTHCAL_GoToNextMonth(infoPtr);
1615 break;
1616 case MC_PREVMONTHTIMER:
1617 redraw = TRUE;
1618 MONTHCAL_GoToPrevMonth(infoPtr);
1619 break;
1620 default:
1621 ERR("got unknown timer\n");
1622 break;
1625 /* redraw only if necessary */
1626 if(redraw)
1627 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1629 return 0;
1633 static LRESULT
1634 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1636 MCHITTESTINFO ht;
1637 int oldselday, selday, hit;
1638 RECT r;
1640 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
1642 ht.pt.x = (short)LOWORD(lParam);
1643 ht.pt.y = (short)HIWORD(lParam);
1645 hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1647 /* not on the calendar date numbers? bail out */
1648 TRACE("hit:%x\n",hit);
1649 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE) return 0;
1651 selday = ht.st.wDay;
1652 oldselday = infoPtr->curSelDay;
1653 infoPtr->curSelDay = selday;
1654 MONTHCAL_CalcPosFromDay(infoPtr, selday, ht.st. wMonth, &r);
1656 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) {
1657 SYSTEMTIME selArray[2];
1658 int i;
1660 MONTHCAL_GetSelRange(infoPtr, (LPARAM)selArray);
1661 i = 0;
1662 if(infoPtr->firstSelDay==selArray[0].wDay) i=1;
1663 TRACE("oldRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1664 if(infoPtr->firstSelDay==selArray[1].wDay) {
1665 /* 1st time we get here: selArray[0]=selArray[1]) */
1666 /* if we're still at the first selected date, return */
1667 if(infoPtr->firstSelDay==selday) goto done;
1668 if(selday<infoPtr->firstSelDay) i = 0;
1671 if(abs(infoPtr->firstSelDay - selday) >= infoPtr->maxSelCount) {
1672 if(selday>infoPtr->firstSelDay)
1673 selday = infoPtr->firstSelDay + infoPtr->maxSelCount;
1674 else
1675 selday = infoPtr->firstSelDay - infoPtr->maxSelCount;
1678 if(selArray[i].wDay!=selday) {
1679 TRACE("newRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1681 selArray[i].wDay = selday;
1683 if(selArray[0].wDay>selArray[1].wDay) {
1684 DWORD tempday;
1685 tempday = selArray[1].wDay;
1686 selArray[1].wDay = selArray[0].wDay;
1687 selArray[0].wDay = tempday;
1690 MONTHCAL_SetSelRange(infoPtr, (LPARAM)selArray);
1694 done:
1696 /* only redraw if the currently selected day changed */
1697 /* FIXME: this should specify a rectangle containing only the days that changed */
1698 /* using InvalidateRect */
1699 if(oldselday != infoPtr->curSelDay)
1700 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1702 return 0;
1706 static LRESULT
1707 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1709 HDC hdc;
1710 PAINTSTRUCT ps;
1712 if (wParam)
1714 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
1715 hdc = (HDC)wParam;
1717 else
1718 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
1720 MONTHCAL_Refresh(infoPtr, hdc, &ps);
1721 if (!wParam) EndPaint(infoPtr->hwndSelf, &ps);
1722 return 0;
1726 static LRESULT
1727 MONTHCAL_KillFocus(const MONTHCAL_INFO *infoPtr, HWND hFocusWnd)
1729 TRACE("\n");
1731 if (infoPtr->hwndNotify != hFocusWnd)
1732 ShowWindow(infoPtr->hwndSelf, SW_HIDE);
1733 else
1734 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1736 return 0;
1740 static LRESULT
1741 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
1743 TRACE("\n");
1745 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1747 return 0;
1750 /* sets the size information */
1751 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
1753 static const WCHAR SunW[] = { 'S','u','n',0 };
1754 static const WCHAR O0W[] = { '0','0',0 };
1755 HDC hdc = GetDC(infoPtr->hwndSelf);
1756 RECT *title=&infoPtr->title;
1757 RECT *prev=&infoPtr->titlebtnprev;
1758 RECT *next=&infoPtr->titlebtnnext;
1759 RECT *titlemonth=&infoPtr->titlemonth;
1760 RECT *titleyear=&infoPtr->titleyear;
1761 RECT *wdays=&infoPtr->wdays;
1762 RECT *weeknumrect=&infoPtr->weeknums;
1763 RECT *days=&infoPtr->days;
1764 RECT *todayrect=&infoPtr->todayrect;
1765 SIZE size;
1766 TEXTMETRICW tm;
1767 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1768 HFONT currentFont;
1769 int xdiv, left_offset;
1770 RECT rcClient;
1772 GetClientRect(infoPtr->hwndSelf, &rcClient);
1774 currentFont = SelectObject(hdc, infoPtr->hFont);
1776 /* get the height and width of each day's text */
1777 GetTextMetricsW(hdc, &tm);
1778 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
1779 GetTextExtentPoint32W(hdc, SunW, 3, &size);
1780 infoPtr->textWidth = size.cx + 2;
1782 /* recalculate the height and width increments and offsets */
1783 GetTextExtentPoint32W(hdc, O0W, 2, &size);
1785 xdiv = (dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
1787 infoPtr->width_increment = size.cx * 2 + 4;
1788 infoPtr->height_increment = infoPtr->textHeight;
1789 left_offset = (rcClient.right - rcClient.left) - (infoPtr->width_increment * xdiv);
1791 /* calculate title area */
1792 title->top = rcClient.top;
1793 title->bottom = title->top + 3 * infoPtr->height_increment / 2;
1794 title->left = left_offset;
1795 title->right = rcClient.right;
1797 /* set the dimensions of the next and previous buttons and center */
1798 /* the month text vertically */
1799 prev->top = next->top = title->top + 4;
1800 prev->bottom = next->bottom = title->bottom - 4;
1801 prev->left = title->left + 4;
1802 prev->right = prev->left + (title->bottom - title->top) ;
1803 next->right = title->right - 4;
1804 next->left = next->right - (title->bottom - title->top);
1806 /* titlemonth->left and right change based upon the current month */
1807 /* and are recalculated in refresh as the current month may change */
1808 /* without the control being resized */
1809 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
1810 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
1812 /* setup the dimensions of the rectangle we draw the names of the */
1813 /* days of the week in */
1814 weeknumrect->left = left_offset;
1815 if(dwStyle & MCS_WEEKNUMBERS)
1816 weeknumrect->right=prev->right;
1817 else
1818 weeknumrect->right=weeknumrect->left;
1819 wdays->left = days->left = weeknumrect->right;
1820 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
1821 wdays->top = title->bottom ;
1822 wdays->bottom = wdays->top + infoPtr->height_increment;
1824 days->top = weeknumrect->top = wdays->bottom ;
1825 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
1827 todayrect->left = rcClient.left;
1828 todayrect->right = rcClient.right;
1829 todayrect->top = days->bottom;
1830 todayrect->bottom = days->bottom + infoPtr->height_increment;
1832 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
1833 infoPtr->width_increment,infoPtr->height_increment,
1834 wine_dbgstr_rect(&rcClient),
1835 wine_dbgstr_rect(title),
1836 wine_dbgstr_rect(wdays),
1837 wine_dbgstr_rect(days),
1838 wine_dbgstr_rect(todayrect));
1840 /* restore the originally selected font */
1841 SelectObject(hdc, currentFont);
1843 ReleaseDC(infoPtr->hwndSelf, hdc);
1846 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
1848 TRACE("(width=%d, height=%d)\n", Width, Height);
1850 MONTHCAL_UpdateSize(infoPtr);
1852 /* invalidate client area and erase background */
1853 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1855 return 0;
1858 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
1860 return (LRESULT)infoPtr->hFont;
1863 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
1865 HFONT hOldFont;
1866 LOGFONTW lf;
1868 if (!hFont) return 0;
1870 hOldFont = infoPtr->hFont;
1871 infoPtr->hFont = hFont;
1873 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
1874 lf.lfWeight = FW_BOLD;
1875 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
1877 MONTHCAL_UpdateSize(infoPtr);
1879 if (redraw)
1880 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1882 return (LRESULT)hOldFont;
1885 /* update theme after a WM_THEMECHANGED message */
1886 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
1888 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
1889 CloseThemeData (theme);
1890 theme = OpenThemeData (infoPtr->hwndSelf, themeClass);
1891 return 0;
1894 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
1895 static LRESULT
1896 MONTHCAL_Create(HWND hwnd, LPARAM lParam)
1898 MONTHCAL_INFO *infoPtr;
1900 /* allocate memory for info structure */
1901 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
1902 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
1904 if(infoPtr == NULL) {
1905 ERR( "could not allocate info memory!\n");
1906 return 0;
1909 infoPtr->hwndSelf = hwnd;
1910 infoPtr->hwndNotify = ((LPCREATESTRUCTW)lParam)->hwndParent;
1912 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
1914 /* initialize info structure */
1915 /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
1917 GetLocalTime(&infoPtr->todaysDate);
1918 infoPtr->firstDayHighWord = FALSE;
1919 MONTHCAL_SetFirstDayOfWeek(infoPtr, (LPARAM)-1);
1920 infoPtr->currentMonth = infoPtr->todaysDate.wMonth;
1921 infoPtr->currentYear = infoPtr->todaysDate.wYear;
1922 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->minDate);
1923 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
1924 infoPtr->maxDate.wYear=2050;
1925 infoPtr->minDate.wYear=1950;
1926 infoPtr->maxSelCount = 7;
1927 infoPtr->monthRange = 3;
1928 infoPtr->monthdayState = Alloc
1929 (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1930 infoPtr->titlebk = GetSysColor(COLOR_ACTIVECAPTION);
1931 infoPtr->titletxt = GetSysColor(COLOR_WINDOW);
1932 infoPtr->monthbk = GetSysColor(COLOR_WINDOW);
1933 infoPtr->trailingtxt = GetSysColor(COLOR_GRAYTEXT);
1934 infoPtr->bk = GetSysColor(COLOR_WINDOW);
1935 infoPtr->txt = GetSysColor(COLOR_WINDOWTEXT);
1937 /* set the current day for highlighing */
1938 infoPtr->minSel.wDay = infoPtr->todaysDate.wDay;
1939 infoPtr->maxSel.wDay = infoPtr->todaysDate.wDay;
1941 /* call MONTHCAL_UpdateSize to set all of the dimensions */
1942 /* of the control */
1943 MONTHCAL_UpdateSize(infoPtr);
1945 OpenThemeData (infoPtr->hwndSelf, themeClass);
1947 return 0;
1951 static LRESULT
1952 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
1954 /* free month calendar info data */
1955 Free(infoPtr->monthdayState);
1956 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
1958 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
1960 Free(infoPtr);
1961 return 0;
1965 static LRESULT WINAPI
1966 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1968 MONTHCAL_INFO *infoPtr;
1970 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
1972 infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1973 if (!infoPtr && (uMsg != WM_CREATE))
1974 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
1975 switch(uMsg)
1977 case MCM_GETCURSEL:
1978 return MONTHCAL_GetCurSel(infoPtr, lParam);
1980 case MCM_SETCURSEL:
1981 return MONTHCAL_SetCurSel(infoPtr, lParam);
1983 case MCM_GETMAXSELCOUNT:
1984 return MONTHCAL_GetMaxSelCount(infoPtr);
1986 case MCM_SETMAXSELCOUNT:
1987 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
1989 case MCM_GETSELRANGE:
1990 return MONTHCAL_GetSelRange(infoPtr, lParam);
1992 case MCM_SETSELRANGE:
1993 return MONTHCAL_SetSelRange(infoPtr, lParam);
1995 case MCM_GETMONTHRANGE:
1996 return MONTHCAL_GetMonthRange(infoPtr);
1998 case MCM_SETDAYSTATE:
1999 return MONTHCAL_SetDayState(infoPtr, wParam, lParam);
2001 case MCM_GETMINREQRECT:
2002 return MONTHCAL_GetMinReqRect(infoPtr, lParam);
2004 case MCM_GETCOLOR:
2005 return MONTHCAL_GetColor(infoPtr, wParam);
2007 case MCM_SETCOLOR:
2008 return MONTHCAL_SetColor(infoPtr, wParam, lParam);
2010 case MCM_GETTODAY:
2011 return MONTHCAL_GetToday(infoPtr, lParam);
2013 case MCM_SETTODAY:
2014 return MONTHCAL_SetToday(infoPtr, lParam);
2016 case MCM_HITTEST:
2017 return MONTHCAL_HitTest(infoPtr, lParam);
2019 case MCM_GETFIRSTDAYOFWEEK:
2020 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2022 case MCM_SETFIRSTDAYOFWEEK:
2023 return MONTHCAL_SetFirstDayOfWeek(infoPtr, lParam);
2025 case MCM_GETRANGE:
2026 return MONTHCAL_GetRange(hwnd, lParam);
2028 case MCM_SETRANGE:
2029 return MONTHCAL_SetRange(infoPtr, wParam, lParam);
2031 case MCM_GETMONTHDELTA:
2032 return MONTHCAL_GetMonthDelta(infoPtr);
2034 case MCM_SETMONTHDELTA:
2035 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2037 case MCM_GETMAXTODAYWIDTH:
2038 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2040 case WM_GETDLGCODE:
2041 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2043 case WM_KILLFOCUS:
2044 return MONTHCAL_KillFocus(infoPtr, (HWND)wParam);
2046 case WM_RBUTTONDOWN:
2047 return MONTHCAL_RButtonDown(infoPtr, lParam);
2049 case WM_LBUTTONDOWN:
2050 return MONTHCAL_LButtonDown(infoPtr, lParam);
2052 case WM_MOUSEMOVE:
2053 return MONTHCAL_MouseMove(infoPtr, lParam);
2055 case WM_LBUTTONUP:
2056 return MONTHCAL_LButtonUp(infoPtr, lParam);
2058 case WM_PRINTCLIENT:
2059 case WM_PAINT:
2060 return MONTHCAL_Paint(infoPtr, wParam);
2062 case WM_SETFOCUS:
2063 return MONTHCAL_SetFocus(infoPtr);
2065 case WM_SIZE:
2066 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2068 case WM_CREATE:
2069 return MONTHCAL_Create(hwnd, lParam);
2071 case WM_SETFONT:
2072 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2074 case WM_GETFONT:
2075 return MONTHCAL_GetFont(infoPtr);
2077 case WM_TIMER:
2078 return MONTHCAL_Timer(infoPtr, wParam);
2080 case WM_THEMECHANGED:
2081 return theme_changed (infoPtr);
2083 case WM_DESTROY:
2084 return MONTHCAL_Destroy(infoPtr);
2086 default:
2087 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2088 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2089 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2094 void
2095 MONTHCAL_Register(void)
2097 WNDCLASSW wndClass;
2099 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2100 wndClass.style = CS_GLOBALCLASS;
2101 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2102 wndClass.cbClsExtra = 0;
2103 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2104 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2105 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2106 wndClass.lpszClassName = MONTHCAL_CLASSW;
2108 RegisterClassW(&wndClass);
2112 void
2113 MONTHCAL_Unregister(void)
2115 UnregisterClassW(MONTHCAL_CLASSW, NULL);