comctl32/monthcal: Minimal rectangle should be zero based.
[wine/wine-gecko.git] / dlls / comctl32 / monthcal.c
blob6f60587f932f1aab49ee205665ad55efb292b54f
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 * -- search for FIXME
42 #include <math.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "commctrl.h"
54 #include "comctl32.h"
55 #include "uxtheme.h"
56 #include "tmschema.h"
57 #include "wine/unicode.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
62 #define MC_SEL_LBUTUP 1 /* Left button released */
63 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
64 #define MC_PREVPRESSED 4 /* Prev month button pressed */
65 #define MC_NEXTPRESSED 8 /* Next month button pressed */
66 #define MC_NEXTMONTHDELAY 350 /* when continuously pressing `next */
67 /* month', wait 500 ms before going */
68 /* to the next month */
69 #define MC_NEXTMONTHTIMER 1 /* Timer ID's */
70 #define MC_PREVMONTHTIMER 2
72 #define countof(arr) (sizeof(arr)/sizeof(arr[0]))
74 typedef struct
76 HWND hwndSelf;
77 DWORD dwStyle; /* cached GWL_STYLE */
78 COLORREF bk;
79 COLORREF txt;
80 COLORREF titlebk;
81 COLORREF titletxt;
82 COLORREF monthbk;
83 COLORREF trailingtxt;
84 HFONT hFont;
85 HFONT hBoldFont;
86 int textHeight;
87 int textWidth;
88 int height_increment;
89 int width_increment;
90 int firstDayplace; /* place of the first day of the current month */
91 INT delta; /* scroll rate; # of months that the */
92 /* control moves when user clicks a scroll button */
93 int visible; /* # of months visible */
94 int firstDay; /* Start month calendar with firstDay's day */
95 int firstDayHighWord; /* High word only used externally */
96 int monthRange;
97 MONTHDAYSTATE *monthdayState;
98 SYSTEMTIME todaysDate;
99 int status; /* See MC_SEL flags */
100 int firstSelDay; /* first selected day */
101 INT maxSelCount;
102 SYSTEMTIME minSel;
103 SYSTEMTIME maxSel;
104 SYSTEMTIME curSel; /* contains currently selected year, month and day */
105 DWORD rangeValid;
106 SYSTEMTIME minDate;
107 SYSTEMTIME maxDate;
109 RECT title; /* rect for the header above the calendar */
110 RECT titlebtnnext; /* the `next month' button in the header */
111 RECT titlebtnprev; /* the `prev month' button in the header */
112 RECT titlemonth; /* the `month name' txt in the header */
113 RECT titleyear; /* the `year number' txt in the header */
114 RECT wdays; /* week days at top */
115 RECT days; /* calendar area */
116 RECT weeknums; /* week numbers at left side */
117 RECT todayrect; /* `today: xx/xx/xx' text rect */
118 HWND hwndNotify; /* Window to receive the notifications */
119 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
120 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
121 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
124 /* Offsets of days in the week to the weekday of january 1 in a leap year */
125 static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
127 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
129 #define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0))
131 /* helper functions */
133 /* returns the number of days in any given month, checking for leap days */
134 /* january is 1, december is 12 */
135 int MONTHCAL_MonthLength(int month, int year)
137 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0};
138 /*Wrap around, this eases handling*/
139 if(month == 0)
140 month = 12;
141 if(month == 13)
142 month = 1;
144 /* if we have a leap year add 1 day to February */
145 /* a leap year is a year either divisible by 400 */
146 /* or divisible by 4 and not by 100 */
147 if(month == 2) { /* February */
148 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
149 (year%4 == 0)) ? 1 : 0);
151 else {
152 return mdays[month - 1];
156 /* compares timestamps using date part only */
157 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
159 return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
160 (first->wDay == second->wDay);
163 /* make sure that date fields are valid */
164 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
166 if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
167 if(time->wDayOfWeek > 6) return FALSE;
168 if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear))
169 return FALSE;
171 return TRUE;
174 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
175 Milliseconds are intentionaly not validated. */
176 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
178 if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
179 return FALSE;
180 else
181 return TRUE;
184 /* Copies timestamp part only. Milliseconds are intentionaly not copied
185 cause it matches required behaviour for current use of this helper */
186 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
188 to->wHour = from->wHour;
189 to->wMinute = from->wMinute;
190 to->wSecond = from->wSecond;
193 /* Note:Depending on DST, this may be offset by a day.
194 Need to find out if we're on a DST place & adjust the clock accordingly.
195 Above function assumes we have a valid data.
196 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
197 0 = Sunday.
200 /* returns the day in the week(0 == sunday, 6 == saturday) */
201 /* day(1 == 1st, 2 == 2nd... etc), year is the year value */
202 static int MONTHCAL_CalculateDayOfWeek(DWORD day, DWORD month, DWORD year)
204 year-=(month < 3);
206 return((year + year/4 - year/100 + year/400 +
207 DayOfWeekTable[month-1] + day ) % 7);
210 /* From a given point, calculate the row (weekpos), column(daypos)
211 and day in the calendar. day== 0 mean the last day of tha last month
213 static int MONTHCAL_CalcDayFromPos(const MONTHCAL_INFO *infoPtr, int x, int y,
214 int *daypos,int *weekpos)
216 int retval, firstDay;
217 RECT rcClient;
219 GetClientRect(infoPtr->hwndSelf, &rcClient);
221 /* if the point is outside the x bounds of the window put
222 it at the boundary */
223 if (x > rcClient.right)
224 x = rcClient.right;
227 *daypos = (x - infoPtr->days.left ) / infoPtr->width_increment;
228 *weekpos = (y - infoPtr->days.top ) / infoPtr->height_increment;
230 firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear)+6 - infoPtr->firstDay)%7;
231 retval = *daypos + (7 * *weekpos) - firstDay;
232 return retval;
235 /* day is the day of the month, 1 == 1st day of the month */
236 /* sets x and y to be the position of the day */
237 /* x == day, y == week where(0,0) == firstDay, 1st week */
238 static void MONTHCAL_CalcDayXY(const MONTHCAL_INFO *infoPtr, int day, int month,
239 int *x, int *y)
241 int firstDay, prevMonth;
243 firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear) +6 - infoPtr->firstDay)%7;
245 if(month==infoPtr->curSel.wMonth) {
246 *x = (day + firstDay) % 7;
247 *y = (day + firstDay - *x) / 7;
248 return;
250 if(month < infoPtr->curSel.wMonth) {
251 prevMonth = month - 1;
252 if(prevMonth==0)
253 prevMonth = 12;
255 *x = (MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear) - firstDay) % 7;
256 *y = 0;
257 return;
260 *y = MONTHCAL_MonthLength(month, infoPtr->curSel.wYear - 1) / 7;
261 *x = (day + firstDay + MONTHCAL_MonthLength(month,
262 infoPtr->curSel.wYear)) % 7;
266 /* x: column(day), y: row(week) */
267 static void MONTHCAL_CalcDayRect(const MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
269 r->left = infoPtr->days.left + x * infoPtr->width_increment;
270 r->right = r->left + infoPtr->width_increment;
271 r->top = infoPtr->days.top + y * infoPtr->height_increment;
272 r->bottom = r->top + infoPtr->textHeight;
276 /* sets the RECT struct r to the rectangle around the day and month */
277 /* day is the day value of the month(1 == 1st), month is the month */
278 /* value(january == 1, december == 12) */
279 static inline void MONTHCAL_CalcPosFromDay(const MONTHCAL_INFO *infoPtr,
280 int day, int month, RECT *r)
282 int x, y;
284 MONTHCAL_CalcDayXY(infoPtr, day, month, &x, &y);
285 MONTHCAL_CalcDayRect(infoPtr, r, x, y);
289 /* day is the day in the month(1 == 1st of the month) */
290 /* month is the month value(1 == january, 12 == december) */
291 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month)
293 HPEN hRedPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
294 HPEN hOldPen2 = SelectObject(hdc, hRedPen);
295 HBRUSH hOldBrush;
296 RECT day_rect;
298 MONTHCAL_CalcPosFromDay(infoPtr, day, month, &day_rect);
300 hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
301 Rectangle(hdc, day_rect.left, day_rect.top, day_rect.right, day_rect.bottom);
303 SelectObject(hdc, hOldBrush);
304 DeleteObject(hRedPen);
305 SelectObject(hdc, hOldPen2);
308 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month,
309 int x, int y, int bold)
311 static const WCHAR fmtW[] = { '%','d',0 };
312 WCHAR buf[10];
313 RECT r;
314 static BOOL haveBoldFont, haveSelectedDay = FALSE;
315 HBRUSH hbr;
316 COLORREF oldCol = 0;
317 COLORREF oldBk = 0;
319 wsprintfW(buf, fmtW, day);
321 /* No need to check styles: when selection is not valid, it is set to zero.
322 * 1<day<31, so everything is OK.
325 MONTHCAL_CalcDayRect(infoPtr, &r, x, y);
327 if((day>=infoPtr->minSel.wDay) && (day<=infoPtr->maxSel.wDay)
328 && (month == infoPtr->curSel.wMonth)) {
329 RECT r2;
331 TRACE("%d %d %d\n",day, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
332 TRACE("%s\n", wine_dbgstr_rect(&r));
333 oldCol = SetTextColor(hdc, infoPtr->monthbk);
334 oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
335 hbr = GetSysColorBrush(COLOR_HIGHLIGHT);
336 FillRect(hdc, &r, hbr);
338 /* FIXME: this may need to be changed now b/c of the other
339 drawing changes 11/3/99 CMM */
340 r2.left = r.left - 0.25 * infoPtr->textWidth;
341 r2.top = r.top;
342 r2.right = r.left + 0.5 * infoPtr->textWidth;
343 r2.bottom = r.bottom;
344 if(haveSelectedDay) FillRect(hdc, &r2, hbr);
345 haveSelectedDay = TRUE;
346 } else {
347 haveSelectedDay = FALSE;
350 /* need to add some code for multiple selections */
352 if((bold) &&(!haveBoldFont)) {
353 SelectObject(hdc, infoPtr->hBoldFont);
354 haveBoldFont = TRUE;
356 if((!bold) &&(haveBoldFont)) {
357 SelectObject(hdc, infoPtr->hFont);
358 haveBoldFont = FALSE;
361 SetBkMode(hdc,TRANSPARENT);
362 DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
364 if(haveSelectedDay) {
365 SetTextColor(hdc, oldCol);
366 SetBkColor(hdc, oldBk);
369 /* draw a rectangle around the currently selected days text */
370 if((day == infoPtr->curSel.wDay) && (month == infoPtr->curSel.wMonth))
371 DrawFocusRect(hdc, &r);
375 static void paint_button (const MONTHCAL_INFO *infoPtr, HDC hdc, BOOL btnNext,
376 BOOL pressed, RECT* r)
378 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
380 if (theme)
382 static const int states[] = {
383 /* Prev button */
384 ABS_LEFTNORMAL, ABS_LEFTPRESSED, ABS_LEFTDISABLED,
385 /* Next button */
386 ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
388 int stateNum = btnNext ? 3 : 0;
389 if (pressed)
390 stateNum += 1;
391 else
393 if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
395 DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
397 else
399 int style = btnNext ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
400 if (pressed)
401 style |= DFCS_PUSHED;
402 else
404 if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
407 DrawFrameControl(hdc, r, DFC_SCROLL, style);
412 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
414 static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
415 static const WCHAR fmt1W[] = { '%','s',' ','%','l','d',0 };
416 static const WCHAR fmt2W[] = { '%','s',' ','%','s',0 };
417 static const WCHAR fmt3W[] = { '%','d',0 };
418 RECT *title=&infoPtr->title;
419 RECT *prev=&infoPtr->titlebtnprev;
420 RECT *next=&infoPtr->titlebtnnext;
421 RECT *titlemonth=&infoPtr->titlemonth;
422 RECT *titleyear=&infoPtr->titleyear;
423 RECT dayrect;
424 RECT *days=&dayrect;
425 RECT rtoday;
426 int i, j, m, mask, day, firstDay, weeknum, weeknum1,prevMonth;
427 int textHeight = infoPtr->textHeight;
428 SIZE size;
429 HBRUSH hbr;
430 HFONT currentFont;
431 WCHAR buf[20];
432 WCHAR buf1[20];
433 WCHAR buf2[32];
434 COLORREF oldTextColor, oldBkColor;
435 RECT rcTemp;
436 RECT rcDay; /* used in MONTHCAL_CalcDayRect() */
437 SYSTEMTIME localtime;
438 int startofprescal;
440 oldTextColor = SetTextColor(hdc, comctl32_color.clrWindowText);
442 /* fill background */
443 hbr = CreateSolidBrush (infoPtr->bk);
444 FillRect(hdc, &ps->rcPaint, hbr);
445 DeleteObject(hbr);
447 /* draw header */
448 if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
450 hbr = CreateSolidBrush(infoPtr->titlebk);
451 FillRect(hdc, title, hbr);
452 DeleteObject(hbr);
455 /* if the previous button is pressed draw it depressed */
456 if(IntersectRect(&rcTemp, &(ps->rcPaint), prev))
457 paint_button (infoPtr, hdc, FALSE, infoPtr->status & MC_PREVPRESSED, prev);
459 /* if next button is depressed draw it depressed */
460 if(IntersectRect(&rcTemp, &(ps->rcPaint), next))
461 paint_button (infoPtr, hdc, TRUE, infoPtr->status & MC_NEXTPRESSED, next);
463 oldBkColor = SetBkColor(hdc, infoPtr->titlebk);
464 SetTextColor(hdc, infoPtr->titletxt);
465 currentFont = SelectObject(hdc, infoPtr->hBoldFont);
467 GetLocaleInfoW( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+infoPtr->curSel.wMonth -1,
468 buf1,countof(buf1));
469 wsprintfW(buf, fmt1W, buf1, infoPtr->curSel.wYear);
471 if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
473 DrawTextW(hdc, buf, strlenW(buf), title,
474 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
477 /* titlemonth left/right contained rect for whole titletxt('June 1999')
478 * MCM_HitTestInfo wants month & year rects, so prepare these now.
479 *(no, we can't draw them separately; the whole text is centered)
481 GetTextExtentPoint32W(hdc, buf, strlenW(buf), &size);
482 titlemonth->left = title->right / 2 + title->left / 2 - size.cx / 2;
483 titleyear->right = title->right / 2 + title->left / 2 + size.cx / 2;
484 GetTextExtentPoint32W(hdc, buf1, strlenW(buf1), &size);
485 titlemonth->right = titlemonth->left + size.cx;
486 titleyear->left = titlemonth->right;
488 /* draw month area */
489 rcTemp.top=infoPtr->wdays.top;
490 rcTemp.left=infoPtr->wdays.left;
491 rcTemp.bottom=infoPtr->todayrect.bottom;
492 rcTemp.right =infoPtr->todayrect.right;
493 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcTemp))
495 hbr = CreateSolidBrush(infoPtr->monthbk);
496 FillRect(hdc, &rcTemp, hbr);
497 DeleteObject(hbr);
500 /* draw line under day abbreviations */
502 MoveToEx(hdc, infoPtr->days.left + 3, title->bottom + textHeight + 1, NULL);
503 LineTo(hdc, infoPtr->days.right - 3, title->bottom + textHeight + 1);
505 prevMonth = infoPtr->curSel.wMonth - 1;
506 if(prevMonth == 0) /* if curSel.wMonth is january(1) prevMonth is */
507 prevMonth = 12; /* december(12) of the previous year */
509 infoPtr->wdays.left = infoPtr->days.left = infoPtr->weeknums.right;
510 /* draw day abbreviations */
512 SelectObject(hdc, infoPtr->hFont);
513 SetBkColor(hdc, infoPtr->monthbk);
514 SetTextColor(hdc, infoPtr->trailingtxt);
516 /* copy this rect so we can change the values without changing */
517 /* the original version */
518 days->left = infoPtr->wdays.left;
519 days->right = days->left + infoPtr->width_increment;
520 days->top = infoPtr->wdays.top;
521 days->bottom = infoPtr->wdays.bottom;
523 i = infoPtr->firstDay;
525 for(j=0; j<7; j++) {
526 GetLocaleInfoW( LOCALE_USER_DEFAULT,LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
527 DrawTextW(hdc, buf, strlenW(buf), days, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
528 days->left+=infoPtr->width_increment;
529 days->right+=infoPtr->width_increment;
532 /* draw day numbers; first, the previous month */
534 firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear);
536 day = MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear) +
537 (infoPtr->firstDay + 7 - firstDay)%7 + 1;
538 if (day > MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear))
539 day -=7;
540 startofprescal = day;
541 mask = 1<<(day-1);
543 i = 0;
544 m = 0;
545 while(day <= MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear)) {
546 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
547 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
549 MONTHCAL_DrawDay(infoPtr, hdc, day, prevMonth, i, 0,
550 infoPtr->monthdayState[m] & mask);
553 mask<<=1;
554 day++;
555 i++;
558 /* draw `current' month */
560 day = 1; /* start at the beginning of the current month */
562 infoPtr->firstDayplace = i;
563 SetTextColor(hdc, infoPtr->txt);
564 m++;
565 mask = 1;
567 /* draw the first week of the current month */
568 while(i<7) {
569 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
570 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
573 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth, i, 0,
574 infoPtr->monthdayState[m] & mask);
576 if((infoPtr->curSel.wMonth == infoPtr->todaysDate.wMonth) &&
577 (day==infoPtr->todaysDate.wDay) &&
578 (infoPtr->curSel.wYear == infoPtr->todaysDate.wYear)) {
579 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
580 MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->curSel.wMonth);
584 mask<<=1;
585 day++;
586 i++;
589 j = 1; /* move to the 2nd week of the current month */
590 i = 0; /* move back to sunday */
591 while(day <= MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear)) {
592 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
593 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
595 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth, i, j,
596 infoPtr->monthdayState[m] & mask);
598 if((infoPtr->curSel.wMonth == infoPtr->todaysDate.wMonth) &&
599 (day==infoPtr->todaysDate.wDay) &&
600 (infoPtr->curSel.wYear == infoPtr->todaysDate.wYear))
601 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
602 MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->curSel.wMonth);
604 mask<<=1;
605 day++;
606 i++;
607 if(i>6) { /* past saturday, goto the next weeks sunday */
608 i = 0;
609 j++;
613 /* draw `next' month */
615 day = 1; /* start at the first day of the next month */
616 m++;
617 mask = 1;
619 SetTextColor(hdc, infoPtr->trailingtxt);
620 while((i<7) &&(j<6)) {
621 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
622 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
624 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth + 1, i, j,
625 infoPtr->monthdayState[m] & mask);
628 mask<<=1;
629 day++;
630 i++;
631 if(i==7) { /* past saturday, go to next week's sunday */
632 i = 0;
633 j++;
636 SetTextColor(hdc, infoPtr->txt);
639 /* draw `today' date if style allows it, and draw a circle before today's
640 * date if necessary */
642 if(!(infoPtr->dwStyle & MCS_NOTODAY)) {
643 if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
644 /*day is the number of days from nextmonth we put on the calendar */
645 MONTHCAL_CircleDay(infoPtr, hdc,
646 day+MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear),
647 infoPtr->curSel.wMonth);
649 if (!LoadStringW(COMCTL32_hModule,IDM_TODAY,buf1,countof(buf1)))
651 WARN("Can't load resource\n");
652 strcpyW(buf1, todayW);
654 MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
655 localtime = infoPtr->todaysDate;
656 GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&localtime,NULL,buf2,countof(buf2));
657 wsprintfW(buf, fmt2W, buf1, buf2);
658 SelectObject(hdc, infoPtr->hBoldFont);
660 DrawTextW(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
661 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rtoday))
663 DrawTextW(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
665 SelectObject(hdc, infoPtr->hFont);
668 /*eventually draw week numbers*/
669 if(infoPtr->dwStyle & MCS_WEEKNUMBERS) {
670 /* display weeknumbers*/
671 int mindays;
673 /* Rules what week to call the first week of a new year:
674 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
675 The week containing Jan 1 is the first week of year
676 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
677 First week of year must contain 4 days of the new year
678 LOCALE_IFIRSTWEEKOFYEAR == 1 (what contries?)
679 The first week of the year must contain only days of the new year
681 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
682 weeknum = atoiW(buf);
683 switch (weeknum)
685 case 1: mindays = 6;
686 break;
687 case 2: mindays = 3;
688 break;
689 case 0:
690 default:
691 mindays = 0;
693 if (infoPtr->curSel.wMonth < 2)
695 /* calculate all those exceptions for january */
696 weeknum1=MONTHCAL_CalculateDayOfWeek(1, 1, infoPtr->curSel.wYear);
697 if ((infoPtr->firstDay +7 - weeknum1)%7 > mindays)
698 weeknum =1;
699 else
701 weeknum = 0;
702 for(i=0; i<11; i++)
703 weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->curSel.wYear - 1);
704 weeknum +=startofprescal+ 7;
705 weeknum /=7;
706 weeknum1=MONTHCAL_CalculateDayOfWeek(1, 1, infoPtr->curSel.wYear - 1);
707 if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
708 weeknum++;
711 else
713 weeknum = 0;
714 for(i=0; i<prevMonth-1; i++)
715 weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->curSel.wYear);
716 weeknum +=startofprescal+ 7;
717 weeknum /=7;
718 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->curSel.wYear);
719 if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
720 weeknum++;
722 days->left = infoPtr->weeknums.left;
723 days->right = infoPtr->weeknums.right;
724 days->top = infoPtr->weeknums.top;
725 days->bottom = days->top +infoPtr->height_increment;
726 for(i=0; i<6; i++) {
727 if((i==0)&&(weeknum>50))
729 wsprintfW(buf, fmt3W, weeknum);
730 weeknum=0;
732 else if((i==5)&&(weeknum>47))
734 wsprintfW(buf, fmt3W, 1);
736 else
737 wsprintfW(buf, fmt3W, weeknum + i);
738 DrawTextW(hdc, buf, -1, days, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
739 days->top+=infoPtr->height_increment;
740 days->bottom+=infoPtr->height_increment;
743 MoveToEx(hdc, infoPtr->weeknums.right, infoPtr->weeknums.top + 3 , NULL);
744 LineTo(hdc, infoPtr->weeknums.right, infoPtr->weeknums.bottom );
747 /* currentFont was font at entering Refresh */
749 SetBkColor(hdc, oldBkColor);
750 SelectObject(hdc, currentFont);
751 SetTextColor(hdc, oldTextColor);
755 static LRESULT
756 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, LPRECT lpRect)
758 TRACE("rect %p\n", lpRect);
760 if(!lpRect) return FALSE;
762 lpRect->left = infoPtr->title.left;
763 lpRect->top = infoPtr->title.top;
764 lpRect->right = infoPtr->title.right;
765 lpRect->bottom = infoPtr->todayrect.bottom;
767 AdjustWindowRect(lpRect, infoPtr->dwStyle, FALSE);
769 /* minimal rectangle is zero based */
770 OffsetRect(lpRect, -lpRect->left, -lpRect->top);
772 TRACE("%s\n", wine_dbgstr_rect(lpRect));
774 return TRUE;
778 static LRESULT
779 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, INT index)
781 TRACE("\n");
783 switch(index) {
784 case MCSC_BACKGROUND:
785 return infoPtr->bk;
786 case MCSC_TEXT:
787 return infoPtr->txt;
788 case MCSC_TITLEBK:
789 return infoPtr->titlebk;
790 case MCSC_TITLETEXT:
791 return infoPtr->titletxt;
792 case MCSC_MONTHBK:
793 return infoPtr->monthbk;
794 case MCSC_TRAILINGTEXT:
795 return infoPtr->trailingtxt;
798 return -1;
802 static LRESULT
803 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, INT index, COLORREF color)
805 COLORREF prev = -1;
807 TRACE("%d: color %08x\n", index, color);
809 switch(index) {
810 case MCSC_BACKGROUND:
811 prev = infoPtr->bk;
812 infoPtr->bk = color;
813 break;
814 case MCSC_TEXT:
815 prev = infoPtr->txt;
816 infoPtr->txt = color;
817 break;
818 case MCSC_TITLEBK:
819 prev = infoPtr->titlebk;
820 infoPtr->titlebk = color;
821 break;
822 case MCSC_TITLETEXT:
823 prev=infoPtr->titletxt;
824 infoPtr->titletxt = color;
825 break;
826 case MCSC_MONTHBK:
827 prev = infoPtr->monthbk;
828 infoPtr->monthbk = color;
829 break;
830 case MCSC_TRAILINGTEXT:
831 prev = infoPtr->trailingtxt;
832 infoPtr->trailingtxt = color;
833 break;
836 InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND ? TRUE : FALSE);
837 return prev;
841 static LRESULT
842 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
844 TRACE("\n");
846 if(infoPtr->delta)
847 return infoPtr->delta;
848 else
849 return infoPtr->visible;
853 static LRESULT
854 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
856 INT prev = infoPtr->delta;
858 TRACE("delta %d\n", delta);
860 infoPtr->delta = delta;
861 return prev;
865 static LRESULT
866 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
868 return MAKELONG(infoPtr->firstDay, infoPtr->firstDayHighWord);
872 /* sets the first day of the week that will appear in the control */
873 /* 0 == Sunday, 6 == Saturday */
874 /* FIXME: this needs to be implemented properly in MONTHCAL_Refresh() */
875 /* FIXME: we need more error checking here */
876 static LRESULT
877 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
879 int prev = MAKELONG(infoPtr->firstDay, infoPtr->firstDayHighWord);
880 int localFirstDay;
881 WCHAR buf[40];
883 TRACE("day %d\n", day);
885 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
886 TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
888 localFirstDay = atoiW(buf);
890 if(day == -1)
892 infoPtr->firstDay = localFirstDay;
893 infoPtr->firstDayHighWord = FALSE;
895 else if(day >= 7)
897 infoPtr->firstDay = 6; /* max first day allowed */
898 infoPtr->firstDayHighWord = TRUE;
900 else
902 infoPtr->firstDay = day;
903 infoPtr->firstDayHighWord = TRUE;
906 return prev;
910 static LRESULT
911 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr)
913 TRACE("\n");
915 return infoPtr->monthRange;
919 static LRESULT
920 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
922 return(infoPtr->todayrect.right - infoPtr->todayrect.left);
926 static LRESULT
927 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
929 FILETIME ft_min, ft_max;
931 TRACE("%x %p\n", limits, range);
933 if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
934 (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
935 return FALSE;
937 if (limits & GDTR_MIN)
939 if (!MONTHCAL_ValidateTime(&range[0]))
940 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
942 infoPtr->minDate = range[0];
943 infoPtr->rangeValid |= GDTR_MIN;
945 if (limits & GDTR_MAX)
947 if (!MONTHCAL_ValidateTime(&range[1]))
948 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
950 infoPtr->maxDate = range[1];
951 infoPtr->rangeValid |= GDTR_MAX;
954 /* Only one limit set - we are done */
955 if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
956 return TRUE;
958 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
959 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
961 if (CompareFileTime(&ft_min, &ft_max) >= 0)
963 if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
965 /* Native swaps limits only when both limits are being set. */
966 SYSTEMTIME st_tmp = infoPtr->minDate;
967 infoPtr->minDate = infoPtr->maxDate;
968 infoPtr->maxDate = st_tmp;
970 else
972 static const SYSTEMTIME zero;
974 /* reset the other limit */
975 if (limits & GDTR_MIN) infoPtr->maxDate = zero;
976 if (limits & GDTR_MAX) infoPtr->minDate = zero;
977 infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN ;
981 return TRUE;
985 static LRESULT
986 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
988 TRACE("%p\n", range);
990 if(!range) return FALSE;
992 range[1] = infoPtr->maxDate;
993 range[0] = infoPtr->minDate;
995 return infoPtr->rangeValid;
999 static LRESULT
1000 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1002 int i;
1004 TRACE("%d %p\n", months, states);
1005 if(months != infoPtr->monthRange) return 0;
1007 for(i = 0; i < months; i++)
1008 infoPtr->monthdayState[i] = states[i];
1010 return 1;
1013 static LRESULT
1014 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1016 TRACE("%p\n", curSel);
1017 if(!curSel) return FALSE;
1018 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1020 *curSel = infoPtr->minSel;
1021 TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1022 return TRUE;
1025 /* FIXME: if the specified date is not visible, make it visible */
1026 /* FIXME: redraw? */
1027 static LRESULT
1028 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1030 TRACE("%p\n", curSel);
1031 if(!curSel) return FALSE;
1032 if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1034 if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1036 infoPtr->minSel = *curSel;
1037 infoPtr->maxSel = *curSel;
1039 /* exit earlier if selection equals current */
1040 if (MONTHCAL_IsDateEqual(&infoPtr->curSel, curSel)) return TRUE;
1042 infoPtr->curSel = *curSel;
1044 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1046 return TRUE;
1050 static LRESULT
1051 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1053 return infoPtr->maxSelCount;
1057 static LRESULT
1058 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1060 TRACE("%d\n", max);
1062 if(infoPtr->dwStyle & MCS_MULTISELECT) {
1063 infoPtr->maxSelCount = max;
1066 return TRUE;
1070 static LRESULT
1071 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1073 TRACE("%p\n", range);
1075 if(!range) return FALSE;
1077 if(infoPtr->dwStyle & MCS_MULTISELECT)
1079 range[1] = infoPtr->maxSel;
1080 range[0] = infoPtr->minSel;
1081 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1082 return TRUE;
1085 return FALSE;
1089 static LRESULT
1090 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1092 TRACE("%p\n", range);
1094 if(!range) return FALSE;
1096 if(infoPtr->dwStyle & MCS_MULTISELECT)
1098 /* adjust timestamps */
1099 if(!MONTHCAL_ValidateTime(&range[0]))
1100 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1101 if(!MONTHCAL_ValidateTime(&range[1]))
1102 MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1104 infoPtr->minSel = range[0];
1105 infoPtr->maxSel = range[1];
1107 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1108 return TRUE;
1111 return FALSE;
1115 static LRESULT
1116 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1118 TRACE("%p\n", today);
1120 if(!today) return FALSE;
1121 *today = infoPtr->todaysDate;
1122 return TRUE;
1126 static LRESULT
1127 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1129 TRACE("%p\n", today);
1131 if(!today) return FALSE;
1133 if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return TRUE;
1135 infoPtr->todaysDate = *today;
1136 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1137 return TRUE;
1141 static LRESULT
1142 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1144 UINT x,y;
1145 DWORD retval;
1146 int day,wday,wnum;
1148 if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1150 x = lpht->pt.x;
1151 y = lpht->pt.y;
1153 ZeroMemory(&lpht->st, sizeof(lpht->st));
1155 /* Comment in for debugging...
1156 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,
1157 infoPtr->wdays.left, infoPtr->wdays.right,
1158 infoPtr->wdays.top, infoPtr->wdays.bottom,
1159 infoPtr->days.left, infoPtr->days.right,
1160 infoPtr->days.top, infoPtr->days.bottom,
1161 infoPtr->todayrect.left, infoPtr->todayrect.right,
1162 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1163 infoPtr->weeknums.left, infoPtr->weeknums.right,
1164 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1167 /* are we in the header? */
1169 if(PtInRect(&infoPtr->title, lpht->pt)) {
1170 if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1171 retval = MCHT_TITLEBTNPREV;
1172 goto done;
1174 if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1175 retval = MCHT_TITLEBTNNEXT;
1176 goto done;
1178 if(PtInRect(&infoPtr->titlemonth, lpht->pt)) {
1179 retval = MCHT_TITLEMONTH;
1180 goto done;
1182 if(PtInRect(&infoPtr->titleyear, lpht->pt)) {
1183 retval = MCHT_TITLEYEAR;
1184 goto done;
1187 retval = MCHT_TITLE;
1188 goto done;
1191 day = MONTHCAL_CalcDayFromPos(infoPtr,x,y,&wday,&wnum);
1192 if(PtInRect(&infoPtr->wdays, lpht->pt)) {
1193 retval = MCHT_CALENDARDAY;
1194 lpht->st.wYear = infoPtr->curSel.wYear;
1195 lpht->st.wMonth = (day < 1)? infoPtr->curSel.wMonth -1 : infoPtr->curSel.wMonth;
1196 lpht->st.wDay = (day < 1)?
1197 MONTHCAL_MonthLength(infoPtr->curSel.wMonth-1, infoPtr->curSel.wYear) -day : day;
1198 goto done;
1200 if(PtInRect(&infoPtr->weeknums, lpht->pt)) {
1201 retval = MCHT_CALENDARWEEKNUM;
1202 lpht->st.wYear = infoPtr->curSel.wYear;
1203 lpht->st.wMonth = (day < 1) ? infoPtr->curSel.wMonth -1 :
1204 (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear)) ?
1205 infoPtr->curSel.wMonth +1 :infoPtr->curSel.wMonth;
1206 lpht->st.wDay = (day < 1 ) ?
1207 MONTHCAL_MonthLength(infoPtr->curSel.wMonth-1,infoPtr->curSel.wYear) -day :
1208 (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear)) ?
1209 day - MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear) : day;
1210 goto done;
1212 if(PtInRect(&infoPtr->days, lpht->pt))
1214 lpht->st.wYear = infoPtr->curSel.wYear;
1215 if ( day < 1)
1217 retval = MCHT_CALENDARDATEPREV;
1218 lpht->st.wMonth = infoPtr->curSel.wMonth - 1;
1219 if (lpht->st.wMonth <1)
1221 lpht->st.wMonth = 12;
1222 lpht->st.wYear--;
1224 lpht->st.wDay = MONTHCAL_MonthLength(lpht->st.wMonth,lpht->st.wYear) -day;
1226 else if (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear))
1228 retval = MCHT_CALENDARDATENEXT;
1229 lpht->st.wMonth = infoPtr->curSel.wMonth + 1;
1230 if (lpht->st.wMonth <12)
1232 lpht->st.wMonth = 1;
1233 lpht->st.wYear++;
1235 lpht->st.wDay = day - MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear) ;
1237 else {
1238 retval = MCHT_CALENDARDATE;
1239 lpht->st.wMonth = infoPtr->curSel.wMonth;
1240 lpht->st.wDay = day;
1241 lpht->st.wDayOfWeek = MONTHCAL_CalculateDayOfWeek(day,lpht->st.wMonth,lpht->st.wYear);
1243 goto done;
1245 if(PtInRect(&infoPtr->todayrect, lpht->pt)) {
1246 retval = MCHT_TODAYLINK;
1247 goto done;
1251 /* Hit nothing special? What's left must be background :-) */
1253 retval = MCHT_CALENDARBK;
1254 done:
1255 lpht->uHit = retval;
1256 return retval;
1259 /* MCN_GETDAYSTATE notification helper */
1260 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1262 if(infoPtr->dwStyle & MCS_DAYSTATE) {
1263 NMDAYSTATE nmds;
1264 INT i;
1266 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1267 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1268 nmds.nmhdr.code = MCN_GETDAYSTATE;
1269 nmds.cDayState = infoPtr->monthRange;
1270 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1272 nmds.stStart = infoPtr->todaysDate;
1273 nmds.stStart.wYear = infoPtr->curSel.wYear;
1274 nmds.stStart.wMonth = infoPtr->curSel.wMonth;
1275 nmds.stStart.wDay = 1;
1277 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1278 for(i = 0; i < infoPtr->monthRange; i++)
1279 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1281 Free(nmds.prgDayState);
1285 static void MONTHCAL_GoToNextMonth(MONTHCAL_INFO *infoPtr)
1287 SYSTEMTIME next = infoPtr->curSel;
1289 TRACE("\n");
1291 next.wMonth++;
1292 if(next.wMonth > 12) {
1293 next.wYear++;
1294 next.wMonth = 1;
1297 /* prevent max range exceeding */
1298 if(infoPtr->rangeValid & GDTR_MAX)
1300 FILETIME ft_next, ft_max;
1302 SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1303 SystemTimeToFileTime(&next, &ft_next);
1305 if (CompareFileTime(&ft_next, &ft_max) > 0) return;
1308 infoPtr->curSel = next;
1310 MONTHCAL_NotifyDayState(infoPtr);
1314 static void MONTHCAL_GoToPrevMonth(MONTHCAL_INFO *infoPtr)
1316 SYSTEMTIME prev = infoPtr->curSel;
1318 TRACE("\n");
1320 prev.wMonth--;
1321 if(prev.wMonth < 1) {
1322 prev.wYear--;
1323 prev.wMonth = 12;
1326 /* prevent min range exceeding */
1327 if(infoPtr->rangeValid & GDTR_MIN)
1329 FILETIME ft_prev, ft_min;
1331 SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1332 SystemTimeToFileTime(&prev, &ft_prev);
1334 if (CompareFileTime(&ft_prev, &ft_min) < 0) return;
1337 infoPtr->curSel = prev;
1339 MONTHCAL_NotifyDayState(infoPtr);
1342 static LRESULT
1343 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1345 static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1346 HMENU hMenu;
1347 POINT menupoint;
1348 WCHAR buf[32];
1350 hMenu = CreatePopupMenu();
1351 if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1353 WARN("Can't load resource\n");
1354 strcpyW(buf, todayW);
1356 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1357 menupoint.x = (short)LOWORD(lParam);
1358 menupoint.y = (short)HIWORD(lParam);
1359 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1360 if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
1361 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1363 infoPtr->curSel = infoPtr->todaysDate;
1364 infoPtr->minSel = infoPtr->todaysDate;
1365 infoPtr->maxSel = infoPtr->todaysDate;
1366 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1369 return 0;
1372 /* creates updown control and edit box */
1373 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr)
1375 static const WCHAR EditW[] = { 'E','D','I','T',0 };
1377 infoPtr->hWndYearEdit =
1378 CreateWindowExW(0, EditW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
1379 infoPtr->titleyear.left + 3, infoPtr->titlebtnnext.top,
1380 infoPtr->titleyear.right - infoPtr->titleyear.left + 4,
1381 infoPtr->textHeight, infoPtr->hwndSelf,
1382 NULL, NULL, NULL);
1384 SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
1386 infoPtr->hWndYearUpDown =
1387 CreateWindowExW(0, UPDOWN_CLASSW, 0,
1388 WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
1389 infoPtr->titleyear.right + 7, infoPtr->titlebtnnext.top,
1390 18, infoPtr->textHeight, infoPtr->hwndSelf,
1391 NULL, NULL, NULL);
1393 /* attach edit box */
1394 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0, MAKELONG(9999, 1753));
1395 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
1396 SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->curSel.wYear);
1399 static LRESULT
1400 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1402 MCHITTESTINFO ht;
1403 DWORD hit;
1405 if (infoPtr->hWndYearUpDown)
1407 infoPtr->curSel.wYear = SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, 0);
1408 if(!DestroyWindow(infoPtr->hWndYearUpDown))
1410 FIXME("Can't destroy Updown Control\n");
1412 else
1413 infoPtr->hWndYearUpDown = 0;
1415 if(!DestroyWindow(infoPtr->hWndYearEdit))
1417 FIXME("Can't destroy Updown Control\n");
1419 else
1420 infoPtr->hWndYearEdit = 0;
1422 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1425 ht.cbSize = sizeof(MCHITTESTINFO);
1426 ht.pt.x = (short)LOWORD(lParam);
1427 ht.pt.y = (short)HIWORD(lParam);
1428 TRACE("(%d, %d)\n", ht.pt.x, ht.pt.y);
1430 hit = MONTHCAL_HitTest(infoPtr, &ht);
1432 switch(hit)
1434 case MCHT_TITLEBTNNEXT:
1435 MONTHCAL_GoToNextMonth(infoPtr);
1436 infoPtr->status = MC_NEXTPRESSED;
1437 SetTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1438 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1439 return 0;
1441 case MCHT_TITLEBTNPREV:
1442 MONTHCAL_GoToPrevMonth(infoPtr);
1443 infoPtr->status = MC_PREVPRESSED;
1444 SetTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1445 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1446 return 0;
1448 case MCHT_TITLEMONTH:
1450 HMENU hMenu = CreatePopupMenu();
1451 WCHAR buf[32];
1452 POINT menupoint;
1453 INT i;
1455 for (i = 0; i < 12; i++)
1457 GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
1458 AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
1460 menupoint.x = ht.pt.x;
1461 menupoint.y = ht.pt.y;
1462 ClientToScreen(infoPtr->hwndSelf, &menupoint);
1463 i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
1464 menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
1466 if ((i > 0) && (i < 13))
1468 infoPtr->curSel.wMonth = i;
1469 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1471 return 0;
1473 case MCHT_TITLEYEAR:
1475 MONTHCAL_EditYear(infoPtr);
1476 return 0;
1478 case MCHT_TODAYLINK:
1480 NMSELCHANGE nmsc;
1482 infoPtr->firstSelDay = infoPtr->todaysDate.wDay;
1483 infoPtr->curSel = infoPtr->todaysDate;
1484 infoPtr->minSel = infoPtr->todaysDate;
1485 infoPtr->maxSel = infoPtr->todaysDate;
1486 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1488 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1489 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1490 nmsc.nmhdr.code = MCN_SELCHANGE;
1491 nmsc.stSelStart = infoPtr->minSel;
1492 nmsc.stSelEnd = infoPtr->maxSel;
1493 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1495 nmsc.nmhdr.code = MCN_SELECT;
1496 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1497 return 0;
1499 case MCHT_CALENDARDATE:
1501 RECT rcDay; /* used in determining area to invalidate */
1502 SYSTEMTIME selArray[2];
1503 NMSELCHANGE nmsc;
1505 selArray[0] = ht.st;
1506 selArray[1] = ht.st;
1507 MONTHCAL_SetSelRange(infoPtr, selArray);
1508 MONTHCAL_SetCurSel(infoPtr, &selArray[0]);
1509 TRACE("MCHT_CALENDARDATE\n");
1510 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1511 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1512 nmsc.nmhdr.code = MCN_SELCHANGE;
1513 nmsc.stSelStart = infoPtr->minSel;
1514 nmsc.stSelEnd = infoPtr->maxSel;
1516 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1518 /* redraw both old and new days if the selected day changed */
1519 if(infoPtr->curSel.wDay != ht.st.wDay) {
1520 MONTHCAL_CalcPosFromDay(infoPtr, ht.st.wDay, ht.st.wMonth, &rcDay);
1521 InvalidateRect(infoPtr->hwndSelf, &rcDay, TRUE);
1523 MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->curSel.wDay, infoPtr->curSel.wMonth, &rcDay);
1524 InvalidateRect(infoPtr->hwndSelf, &rcDay, TRUE);
1527 infoPtr->firstSelDay = ht.st.wDay;
1528 infoPtr->curSel.wDay = ht.st.wDay;
1529 infoPtr->status = MC_SEL_LBUTDOWN;
1530 return 0;
1534 return 1;
1538 static LRESULT
1539 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1541 NMSELCHANGE nmsc;
1542 NMHDR nmhdr;
1543 BOOL redraw = FALSE;
1544 MCHITTESTINFO ht;
1545 DWORD hit;
1547 TRACE("\n");
1549 if(infoPtr->status & MC_NEXTPRESSED) {
1550 KillTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER);
1551 infoPtr->status &= ~MC_NEXTPRESSED;
1552 redraw = TRUE;
1554 if(infoPtr->status & MC_PREVPRESSED) {
1555 KillTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER);
1556 infoPtr->status &= ~MC_PREVPRESSED;
1557 redraw = TRUE;
1560 ht.cbSize = sizeof(MCHITTESTINFO);
1561 ht.pt.x = (short)LOWORD(lParam);
1562 ht.pt.y = (short)HIWORD(lParam);
1563 hit = MONTHCAL_HitTest(infoPtr, &ht);
1565 infoPtr->status = MC_SEL_LBUTUP;
1567 if(hit == MCHT_CALENDARDATENEXT) {
1568 MONTHCAL_GoToNextMonth(infoPtr);
1569 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1570 return TRUE;
1572 if(hit == MCHT_CALENDARDATEPREV){
1573 MONTHCAL_GoToPrevMonth(infoPtr);
1574 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1575 return TRUE;
1577 nmhdr.hwndFrom = infoPtr->hwndSelf;
1578 nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1579 nmhdr.code = NM_RELEASEDCAPTURE;
1580 TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
1582 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
1583 /* redraw if necessary */
1584 if(redraw)
1585 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1586 /* only send MCN_SELECT if currently displayed month's day was selected */
1587 if(hit == MCHT_CALENDARDATE) {
1588 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1589 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1590 nmsc.nmhdr.code = MCN_SELECT;
1591 nmsc.stSelStart = infoPtr->minSel;
1592 nmsc.stSelEnd = infoPtr->maxSel;
1594 SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1597 return 0;
1601 static LRESULT
1602 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1604 BOOL redraw = FALSE;
1606 TRACE("%ld\n", wParam);
1608 switch(wParam) {
1609 case MC_NEXTMONTHTIMER:
1610 redraw = TRUE;
1611 MONTHCAL_GoToNextMonth(infoPtr);
1612 break;
1613 case MC_PREVMONTHTIMER:
1614 redraw = TRUE;
1615 MONTHCAL_GoToPrevMonth(infoPtr);
1616 break;
1617 default:
1618 ERR("got unknown timer\n");
1619 break;
1622 /* redraw only if necessary */
1623 if(redraw)
1624 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1626 return 0;
1630 static LRESULT
1631 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1633 MCHITTESTINFO ht;
1634 int oldselday, selday, hit;
1635 RECT r;
1637 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
1639 ht.cbSize = sizeof(MCHITTESTINFO);
1640 ht.pt.x = (short)LOWORD(lParam);
1641 ht.pt.y = (short)HIWORD(lParam);
1643 hit = MONTHCAL_HitTest(infoPtr, &ht);
1645 /* not on the calendar date numbers? bail out */
1646 TRACE("hit:%x\n",hit);
1647 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE) return 0;
1649 selday = ht.st.wDay;
1650 oldselday = infoPtr->curSel.wDay;
1651 infoPtr->curSel.wDay = selday;
1652 MONTHCAL_CalcPosFromDay(infoPtr, selday, ht.st. wMonth, &r);
1654 if(infoPtr->dwStyle & MCS_MULTISELECT) {
1655 SYSTEMTIME selArray[2];
1656 int i;
1658 MONTHCAL_GetSelRange(infoPtr, selArray);
1659 i = 0;
1660 if(infoPtr->firstSelDay==selArray[0].wDay) i=1;
1661 TRACE("oldRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1662 if(infoPtr->firstSelDay==selArray[1].wDay) {
1663 /* 1st time we get here: selArray[0]=selArray[1]) */
1664 /* if we're still at the first selected date, return */
1665 if(infoPtr->firstSelDay==selday) goto done;
1666 if(selday<infoPtr->firstSelDay) i = 0;
1669 if(abs(infoPtr->firstSelDay - selday) >= infoPtr->maxSelCount) {
1670 if(selday>infoPtr->firstSelDay)
1671 selday = infoPtr->firstSelDay + infoPtr->maxSelCount;
1672 else
1673 selday = infoPtr->firstSelDay - infoPtr->maxSelCount;
1676 if(selArray[i].wDay!=selday) {
1677 TRACE("newRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1679 selArray[i].wDay = selday;
1681 if(selArray[0].wDay>selArray[1].wDay) {
1682 DWORD tempday;
1683 tempday = selArray[1].wDay;
1684 selArray[1].wDay = selArray[0].wDay;
1685 selArray[0].wDay = tempday;
1688 MONTHCAL_SetSelRange(infoPtr, selArray);
1692 done:
1694 /* only redraw if the currently selected day changed */
1695 /* FIXME: this should specify a rectangle containing only the days that changed */
1696 /* using InvalidateRect */
1697 if(oldselday != infoPtr->curSel.wDay)
1698 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1700 return 0;
1704 static LRESULT
1705 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
1707 HDC hdc;
1708 PAINTSTRUCT ps;
1710 if (hdc_paint)
1712 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
1713 hdc = hdc_paint;
1715 else
1716 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
1718 MONTHCAL_Refresh(infoPtr, hdc, &ps);
1719 if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
1720 return 0;
1724 static LRESULT
1725 MONTHCAL_KillFocus(const MONTHCAL_INFO *infoPtr, HWND hFocusWnd)
1727 TRACE("\n");
1729 if (infoPtr->hwndNotify != hFocusWnd)
1730 ShowWindow(infoPtr->hwndSelf, SW_HIDE);
1731 else
1732 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1734 return 0;
1738 static LRESULT
1739 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
1741 TRACE("\n");
1743 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1745 return 0;
1748 /* sets the size information */
1749 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
1751 static const WCHAR SunW[] = { 'S','u','n',0 };
1752 static const WCHAR O0W[] = { '0','0',0 };
1753 HDC hdc = GetDC(infoPtr->hwndSelf);
1754 RECT *title=&infoPtr->title;
1755 RECT *prev=&infoPtr->titlebtnprev;
1756 RECT *next=&infoPtr->titlebtnnext;
1757 RECT *titlemonth=&infoPtr->titlemonth;
1758 RECT *titleyear=&infoPtr->titleyear;
1759 RECT *wdays=&infoPtr->wdays;
1760 RECT *weeknumrect=&infoPtr->weeknums;
1761 RECT *days=&infoPtr->days;
1762 RECT *todayrect=&infoPtr->todayrect;
1763 SIZE size;
1764 TEXTMETRICW tm;
1765 HFONT currentFont;
1766 int xdiv, left_offset;
1767 RECT rcClient;
1769 GetClientRect(infoPtr->hwndSelf, &rcClient);
1771 currentFont = SelectObject(hdc, infoPtr->hFont);
1773 /* get the height and width of each day's text */
1774 GetTextMetricsW(hdc, &tm);
1775 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
1776 GetTextExtentPoint32W(hdc, SunW, 3, &size);
1777 infoPtr->textWidth = size.cx + 2;
1779 /* recalculate the height and width increments and offsets */
1780 GetTextExtentPoint32W(hdc, O0W, 2, &size);
1782 xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
1784 infoPtr->width_increment = size.cx * 2 + 4;
1785 infoPtr->height_increment = infoPtr->textHeight;
1786 left_offset = (rcClient.right - rcClient.left) - (infoPtr->width_increment * xdiv);
1788 /* calculate title area */
1789 title->top = rcClient.top;
1790 title->bottom = title->top + 3 * infoPtr->height_increment / 2;
1791 title->left = left_offset;
1792 title->right = rcClient.right;
1794 /* set the dimensions of the next and previous buttons and center */
1795 /* the month text vertically */
1796 prev->top = next->top = title->top + 4;
1797 prev->bottom = next->bottom = title->bottom - 4;
1798 prev->left = title->left + 4;
1799 prev->right = prev->left + (title->bottom - title->top) ;
1800 next->right = title->right - 4;
1801 next->left = next->right - (title->bottom - title->top);
1803 /* titlemonth->left and right change based upon the current month */
1804 /* and are recalculated in refresh as the current month may change */
1805 /* without the control being resized */
1806 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
1807 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
1809 /* setup the dimensions of the rectangle we draw the names of the */
1810 /* days of the week in */
1811 weeknumrect->left = left_offset;
1812 if(infoPtr->dwStyle & MCS_WEEKNUMBERS)
1813 weeknumrect->right=prev->right;
1814 else
1815 weeknumrect->right=weeknumrect->left;
1816 wdays->left = days->left = weeknumrect->right;
1817 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
1818 wdays->top = title->bottom ;
1819 wdays->bottom = wdays->top + infoPtr->height_increment;
1821 days->top = weeknumrect->top = wdays->bottom ;
1822 days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
1824 todayrect->left = rcClient.left;
1825 todayrect->right = rcClient.right;
1826 todayrect->top = days->bottom;
1827 todayrect->bottom = days->bottom + infoPtr->height_increment;
1829 TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
1830 infoPtr->width_increment,infoPtr->height_increment,
1831 wine_dbgstr_rect(&rcClient),
1832 wine_dbgstr_rect(title),
1833 wine_dbgstr_rect(wdays),
1834 wine_dbgstr_rect(days),
1835 wine_dbgstr_rect(todayrect));
1837 /* restore the originally selected font */
1838 SelectObject(hdc, currentFont);
1840 ReleaseDC(infoPtr->hwndSelf, hdc);
1843 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
1845 TRACE("(width=%d, height=%d)\n", Width, Height);
1847 MONTHCAL_UpdateSize(infoPtr);
1849 /* invalidate client area and erase background */
1850 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1852 return 0;
1855 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
1857 return (LRESULT)infoPtr->hFont;
1860 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
1862 HFONT hOldFont;
1863 LOGFONTW lf;
1865 if (!hFont) return 0;
1867 hOldFont = infoPtr->hFont;
1868 infoPtr->hFont = hFont;
1870 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
1871 lf.lfWeight = FW_BOLD;
1872 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
1874 MONTHCAL_UpdateSize(infoPtr);
1876 if (redraw)
1877 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1879 return (LRESULT)hOldFont;
1882 /* update theme after a WM_THEMECHANGED message */
1883 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
1885 HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
1886 CloseThemeData (theme);
1887 OpenThemeData (infoPtr->hwndSelf, themeClass);
1888 return 0;
1891 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
1892 const STYLESTRUCT *lpss)
1894 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1895 wStyleType, lpss->styleOld, lpss->styleNew);
1897 if (wStyleType != GWL_STYLE) return 0;
1899 infoPtr->dwStyle = lpss->styleNew;
1901 return 0;
1904 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
1905 static LRESULT
1906 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
1908 MONTHCAL_INFO *infoPtr;
1910 /* allocate memory for info structure */
1911 infoPtr = Alloc(sizeof(MONTHCAL_INFO));
1912 SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
1914 if(infoPtr == NULL) {
1915 ERR( "could not allocate info memory!\n");
1916 return 0;
1919 infoPtr->hwndSelf = hwnd;
1920 infoPtr->hwndNotify = lpcs->hwndParent;
1921 infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
1923 MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
1925 /* initialize info structure */
1926 /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
1928 GetLocalTime(&infoPtr->todaysDate);
1929 infoPtr->firstDayHighWord = FALSE;
1930 MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
1932 infoPtr->maxSelCount = 7;
1933 infoPtr->monthRange = 3;
1934 infoPtr->monthdayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1935 infoPtr->titlebk = comctl32_color.clrActiveCaption;
1936 infoPtr->titletxt = comctl32_color.clrWindow;
1937 infoPtr->monthbk = comctl32_color.clrWindow;
1938 infoPtr->trailingtxt = comctl32_color.clrGrayText;
1939 infoPtr->bk = comctl32_color.clrWindow;
1940 infoPtr->txt = comctl32_color.clrWindowText;
1942 infoPtr->minSel = infoPtr->todaysDate;
1943 infoPtr->maxSel = infoPtr->todaysDate;
1944 infoPtr->curSel = infoPtr->todaysDate;
1946 /* call MONTHCAL_UpdateSize to set all of the dimensions */
1947 /* of the control */
1948 MONTHCAL_UpdateSize(infoPtr);
1950 OpenThemeData (infoPtr->hwndSelf, themeClass);
1952 return 0;
1956 static LRESULT
1957 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
1959 /* free month calendar info data */
1960 Free(infoPtr->monthdayState);
1961 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
1963 CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
1965 Free(infoPtr);
1966 return 0;
1970 static LRESULT WINAPI
1971 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1973 MONTHCAL_INFO *infoPtr;
1975 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
1977 infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1978 if (!infoPtr && (uMsg != WM_CREATE))
1979 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
1980 switch(uMsg)
1982 case MCM_GETCURSEL:
1983 return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
1985 case MCM_SETCURSEL:
1986 return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
1988 case MCM_GETMAXSELCOUNT:
1989 return MONTHCAL_GetMaxSelCount(infoPtr);
1991 case MCM_SETMAXSELCOUNT:
1992 return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
1994 case MCM_GETSELRANGE:
1995 return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
1997 case MCM_SETSELRANGE:
1998 return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2000 case MCM_GETMONTHRANGE:
2001 return MONTHCAL_GetMonthRange(infoPtr);
2003 case MCM_SETDAYSTATE:
2004 return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2006 case MCM_GETMINREQRECT:
2007 return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2009 case MCM_GETCOLOR:
2010 return MONTHCAL_GetColor(infoPtr, wParam);
2012 case MCM_SETCOLOR:
2013 return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2015 case MCM_GETTODAY:
2016 return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2018 case MCM_SETTODAY:
2019 return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2021 case MCM_HITTEST:
2022 return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2024 case MCM_GETFIRSTDAYOFWEEK:
2025 return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2027 case MCM_SETFIRSTDAYOFWEEK:
2028 return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2030 case MCM_GETRANGE:
2031 return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2033 case MCM_SETRANGE:
2034 return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2036 case MCM_GETMONTHDELTA:
2037 return MONTHCAL_GetMonthDelta(infoPtr);
2039 case MCM_SETMONTHDELTA:
2040 return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2042 case MCM_GETMAXTODAYWIDTH:
2043 return MONTHCAL_GetMaxTodayWidth(infoPtr);
2045 case WM_GETDLGCODE:
2046 return DLGC_WANTARROWS | DLGC_WANTCHARS;
2048 case WM_KILLFOCUS:
2049 return MONTHCAL_KillFocus(infoPtr, (HWND)wParam);
2051 case WM_RBUTTONUP:
2052 return MONTHCAL_RButtonUp(infoPtr, lParam);
2054 case WM_LBUTTONDOWN:
2055 return MONTHCAL_LButtonDown(infoPtr, lParam);
2057 case WM_MOUSEMOVE:
2058 return MONTHCAL_MouseMove(infoPtr, lParam);
2060 case WM_LBUTTONUP:
2061 return MONTHCAL_LButtonUp(infoPtr, lParam);
2063 case WM_PRINTCLIENT:
2064 case WM_PAINT:
2065 return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2067 case WM_SETFOCUS:
2068 return MONTHCAL_SetFocus(infoPtr);
2070 case WM_SIZE:
2071 return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2073 case WM_CREATE:
2074 return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2076 case WM_SETFONT:
2077 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2079 case WM_GETFONT:
2080 return MONTHCAL_GetFont(infoPtr);
2082 case WM_TIMER:
2083 return MONTHCAL_Timer(infoPtr, wParam);
2085 case WM_THEMECHANGED:
2086 return theme_changed (infoPtr);
2088 case WM_DESTROY:
2089 return MONTHCAL_Destroy(infoPtr);
2091 case WM_SYSCOLORCHANGE:
2092 COMCTL32_RefreshSysColors();
2093 return 0;
2095 case WM_STYLECHANGED:
2096 return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2098 default:
2099 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2100 ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2101 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2106 void
2107 MONTHCAL_Register(void)
2109 WNDCLASSW wndClass;
2111 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2112 wndClass.style = CS_GLOBALCLASS;
2113 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2114 wndClass.cbClsExtra = 0;
2115 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2116 wndClass.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2117 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2118 wndClass.lpszClassName = MONTHCAL_CLASSW;
2120 RegisterClassW(&wndClass);
2124 void
2125 MONTHCAL_Unregister(void)
2127 UnregisterClassW(MONTHCAL_CLASSW, NULL);