comctl32/monthcal: Fix hittesting for MCHT_CALENDARDATEPREV/MCHT_CALENDARDATENEXT...
[wine] / dlls / comctl32 / monthcal.c
1 /* Month calendar control
2
3  *
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>
9  *
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.
14  *
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.
19  *
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
23  *
24  * NOTE
25  * 
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.
28  * 
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.
32  * 
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
40  */
41
42 #include <math.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47
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"
59
60 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
61
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
71
72 #define countof(arr) (sizeof(arr)/sizeof(arr[0]))
73
74 typedef struct
75 {
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;
108
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;
122
123
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};
126
127 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
128
129 #define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0))
130
131 /* helper functions  */
132
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)
136 {
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;
143
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);
150   }
151   else {
152     return mdays[month - 1];
153   }
154 }
155
156 /* compares timestamps using date part only */
157 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
158 {
159   return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
160          (first->wDay  == second->wDay);
161 }
162
163 /* make sure that date fields are valid */
164 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
165 {
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;
170
171   return TRUE;
172 }
173
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)
177 {
178   if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
179     return FALSE;
180   else
181     return TRUE;
182 }
183
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)
187 {
188   to->wHour   = from->wHour;
189   to->wMinute = from->wMinute;
190   to->wSecond = from->wSecond;
191 }
192
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.
198 */
199
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)
203 {
204   year-=(month < 3);
205
206   return((year + year/4 - year/100 + year/400 +
207          DayOfWeekTable[month-1] + day ) % 7);
208 }
209
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
212 */
213 static int MONTHCAL_CalcDayFromPos(const MONTHCAL_INFO *infoPtr, int x, int y,
214                                    int *daypos,int *weekpos)
215 {
216   int retval, firstDay;
217   RECT rcClient;
218
219   GetClientRect(infoPtr->hwndSelf, &rcClient);
220
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;
225
226
227   *daypos = (x - infoPtr->days.left ) / infoPtr->width_increment;
228   *weekpos = (y - infoPtr->days.top ) / infoPtr->height_increment;
229
230   firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear)+6 - infoPtr->firstDay)%7;
231   retval = *daypos + (7 * *weekpos) - firstDay;
232   return retval;
233 }
234
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)
240 {
241   int firstDay, prevMonth;
242
243   firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear) +6 - infoPtr->firstDay)%7;
244
245   if(month==infoPtr->curSel.wMonth) {
246     *x = (day + firstDay) % 7;
247     *y = (day + firstDay - *x) / 7;
248     return;
249   }
250   if(month < infoPtr->curSel.wMonth) {
251     prevMonth = month - 1;
252     if(prevMonth==0)
253        prevMonth = 12;
254
255     *x = (MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear) - firstDay) % 7;
256     *y = 0;
257     return;
258   }
259
260   *y = MONTHCAL_MonthLength(month, infoPtr->curSel.wYear - 1) / 7;
261   *x = (day + firstDay + MONTHCAL_MonthLength(month,
262        infoPtr->curSel.wYear)) % 7;
263 }
264
265
266 /* x: column(day), y: row(week) */
267 static void MONTHCAL_CalcDayRect(const MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
268 {
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;
273 }
274
275
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)
281 {
282   int x, y;
283
284   MONTHCAL_CalcDayXY(infoPtr, day, month, &x, &y);
285   MONTHCAL_CalcDayRect(infoPtr, r, x, y);
286 }
287
288
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)
292 {
293   HPEN hRedPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
294   HPEN hOldPen2 = SelectObject(hdc, hRedPen);
295   HBRUSH hOldBrush;
296   RECT day_rect;
297
298   MONTHCAL_CalcPosFromDay(infoPtr, day, month, &day_rect);
299
300   hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
301   Rectangle(hdc, day_rect.left, day_rect.top, day_rect.right, day_rect.bottom);
302
303   SelectObject(hdc, hOldBrush);
304   DeleteObject(hRedPen);
305   SelectObject(hdc, hOldPen2);
306 }
307
308 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month,
309                              int x, int y, int bold)
310 {
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;
318
319   wsprintfW(buf, fmtW, day);
320
321 /* No need to check styles: when selection is not valid, it is set to zero.
322  * 1<day<31, so everything is OK.
323  */
324
325   MONTHCAL_CalcDayRect(infoPtr, &r, x, y);
326
327   if((day>=infoPtr->minSel.wDay) && (day<=infoPtr->maxSel.wDay)
328        && (month == infoPtr->curSel.wMonth)) {
329     RECT r2;
330
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);
337
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;
348   }
349
350   /* need to add some code for multiple selections */
351
352   if((bold) &&(!haveBoldFont)) {
353     SelectObject(hdc, infoPtr->hBoldFont);
354     haveBoldFont = TRUE;
355   }
356   if((!bold) &&(haveBoldFont)) {
357     SelectObject(hdc, infoPtr->hFont);
358     haveBoldFont = FALSE;
359   }
360
361   SetBkMode(hdc,TRANSPARENT);
362   DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
363
364   if(haveSelectedDay) {
365     SetTextColor(hdc, oldCol);
366     SetBkColor(hdc, oldBk);
367   }
368
369   /* draw a rectangle around the currently selected days text */
370   if((day == infoPtr->curSel.wDay) && (month == infoPtr->curSel.wMonth))
371     DrawFocusRect(hdc, &r);
372 }
373
374
375 static void paint_button (const MONTHCAL_INFO *infoPtr, HDC hdc, BOOL btnNext,
376                           BOOL pressed, RECT* r)
377 {
378     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
379     
380     if (theme)
381     {
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
387         };
388         int stateNum = btnNext ? 3 : 0;
389         if (pressed)
390             stateNum += 1;
391         else
392         {
393             if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
394         }
395         DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
396     }
397     else
398     {
399         int style = btnNext ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
400         if (pressed)
401             style |= DFCS_PUSHED;
402         else
403         {
404             if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
405         }
406         
407         DrawFrameControl(hdc, r, DFC_SCROLL, style);
408     }
409 }
410
411
412 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
413 {
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;
439
440   oldTextColor = SetTextColor(hdc, comctl32_color.clrWindowText);
441
442   /* fill background */
443   hbr = CreateSolidBrush (infoPtr->bk);
444   FillRect(hdc, &ps->rcPaint, hbr);
445   DeleteObject(hbr);
446
447   /* draw header */
448   if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
449   {
450     hbr =  CreateSolidBrush(infoPtr->titlebk);
451     FillRect(hdc, title, hbr);
452     DeleteObject(hbr);
453   }
454
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);
458
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);
462
463   oldBkColor = SetBkColor(hdc, infoPtr->titlebk);
464   SetTextColor(hdc, infoPtr->titletxt);
465   currentFont = SelectObject(hdc, infoPtr->hBoldFont);
466
467   GetLocaleInfoW( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+infoPtr->curSel.wMonth -1,
468                   buf1,countof(buf1));
469   wsprintfW(buf, fmt1W, buf1, infoPtr->curSel.wYear);
470
471   if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
472   {
473     DrawTextW(hdc, buf, strlenW(buf), title,
474                         DT_CENTER | DT_VCENTER | DT_SINGLELINE);
475   }
476
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)
480   */
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;
487
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))
494   {
495     hbr =  CreateSolidBrush(infoPtr->monthbk);
496     FillRect(hdc, &rcTemp, hbr);
497     DeleteObject(hbr);
498   }
499
500 /* draw line under day abbreviations */
501
502   MoveToEx(hdc, infoPtr->days.left + 3, title->bottom + textHeight + 1, NULL);
503   LineTo(hdc, infoPtr->days.right - 3, title->bottom + textHeight + 1);
504
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 */
508
509   infoPtr->wdays.left   = infoPtr->days.left   = infoPtr->weeknums.right;
510 /* draw day abbreviations */
511
512   SelectObject(hdc, infoPtr->hFont);
513   SetBkColor(hdc, infoPtr->monthbk);
514   SetTextColor(hdc, infoPtr->trailingtxt);
515
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;
522
523   i = infoPtr->firstDay;
524
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;
530   }
531
532 /* draw day numbers; first, the previous month */
533
534   firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear);
535
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);
542
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))
548     {
549       MONTHCAL_DrawDay(infoPtr, hdc, day, prevMonth, i, 0,
550           infoPtr->monthdayState[m] & mask);
551     }
552
553     mask<<=1;
554     day++;
555     i++;
556   }
557
558 /* draw `current' month  */
559
560   day = 1; /* start at the beginning of the current month */
561
562   infoPtr->firstDayplace = i;
563   SetTextColor(hdc, infoPtr->txt);
564   m++;
565   mask = 1;
566
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))
571     {
572
573       MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth, i, 0,
574         infoPtr->monthdayState[m] & mask);
575
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);
581       }
582     }
583
584     mask<<=1;
585     day++;
586     i++;
587   }
588
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))
594     {
595       MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth, i, j,
596           infoPtr->monthdayState[m] & mask);
597
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);
603     }
604     mask<<=1;
605     day++;
606     i++;
607     if(i>6) { /* past saturday, goto the next weeks sunday */
608       i = 0;
609       j++;
610     }
611   }
612
613 /*  draw `next' month */
614
615   day = 1; /* start at the first day of the next month */
616   m++;
617   mask = 1;
618
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))
623     {
624       MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth + 1, i, j,
625                 infoPtr->monthdayState[m] & mask);
626     }
627
628     mask<<=1;
629     day++;
630     i++;
631     if(i==7) { /* past saturday, go to next week's sunday */
632       i = 0;
633       j++;
634     }
635   }
636   SetTextColor(hdc, infoPtr->txt);
637
638
639 /* draw `today' date if style allows it, and draw a circle before today's
640  * date if necessary */
641
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);
648     }
649     if (!LoadStringW(COMCTL32_hModule,IDM_TODAY,buf1,countof(buf1)))
650       {
651         WARN("Can't load resource\n");
652         strcpyW(buf1, todayW);
653       }
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);
659
660     DrawTextW(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
661     if(IntersectRect(&rcTemp, &(ps->rcPaint), &rtoday))
662     {
663       DrawTextW(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
664     }
665     SelectObject(hdc, infoPtr->hFont);
666   }
667
668 /*eventually draw week numbers*/
669   if(infoPtr->dwStyle & MCS_WEEKNUMBERS)  {
670     /* display weeknumbers*/
671     int mindays;
672
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
680     */
681     GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
682     weeknum = atoiW(buf);
683     switch (weeknum)
684       {
685       case 1: mindays = 6;
686         break;
687       case 2: mindays = 3;
688         break;
689       case 0:
690       default:
691         mindays = 0;
692       }
693     if (infoPtr->curSel.wMonth < 2)
694       {
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
700           {
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++;
709           }
710       }
711     else
712       {
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++;
721       }
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))
728         {
729           wsprintfW(buf, fmt3W, weeknum);
730           weeknum=0;
731         }
732       else if((i==5)&&(weeknum>47))
733         {
734           wsprintfW(buf, fmt3W, 1);
735         }
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;
741     }
742
743     MoveToEx(hdc, infoPtr->weeknums.right, infoPtr->weeknums.top + 3 , NULL);
744     LineTo(hdc,   infoPtr->weeknums.right, infoPtr->weeknums.bottom );
745
746   }
747   /* currentFont was font at entering Refresh */
748
749   SetBkColor(hdc, oldBkColor);
750   SelectObject(hdc, currentFont);
751   SetTextColor(hdc, oldTextColor);
752 }
753
754
755 static LRESULT
756 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, LPRECT lpRect)
757 {
758   TRACE("rect %p\n", lpRect);
759
760   if(!lpRect) return FALSE;
761
762   lpRect->left   = infoPtr->title.left;
763   lpRect->top    = infoPtr->title.top;
764   lpRect->right  = infoPtr->title.right;
765   lpRect->bottom = infoPtr->todayrect.bottom;
766
767   AdjustWindowRect(lpRect, infoPtr->dwStyle, FALSE);
768
769   /* minimal rectangle is zero based */
770   OffsetRect(lpRect, -lpRect->left, -lpRect->top);
771
772   TRACE("%s\n", wine_dbgstr_rect(lpRect));
773
774   return TRUE;
775 }
776
777
778 static LRESULT
779 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, INT index)
780 {
781   TRACE("\n");
782
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;
796   }
797
798   return -1;
799 }
800
801
802 static LRESULT
803 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, INT index, COLORREF color)
804 {
805   COLORREF prev = -1;
806
807   TRACE("%d: color %08x\n", index, color);
808
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;
834   }
835
836   InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND ? TRUE : FALSE);
837   return prev;
838 }
839
840
841 static LRESULT
842 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
843 {
844   TRACE("\n");
845
846   if(infoPtr->delta)
847     return infoPtr->delta;
848   else
849     return infoPtr->visible;
850 }
851
852
853 static LRESULT
854 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
855 {
856   INT prev = infoPtr->delta;
857
858   TRACE("delta %d\n", delta);
859
860   infoPtr->delta = delta;
861   return prev;
862 }
863
864
865 static LRESULT
866 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
867 {
868   return MAKELONG(infoPtr->firstDay, infoPtr->firstDayHighWord);
869 }
870
871
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)
878 {
879   int prev = MAKELONG(infoPtr->firstDay, infoPtr->firstDayHighWord);
880   int localFirstDay;
881   WCHAR buf[40];
882
883   TRACE("day %d\n", day);
884
885   GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
886   TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
887
888   localFirstDay = atoiW(buf);
889
890   if(day == -1)
891   {
892     infoPtr->firstDay = localFirstDay;
893     infoPtr->firstDayHighWord = FALSE;
894   }
895   else if(day >= 7)
896   {
897     infoPtr->firstDay = 6; /* max first day allowed */
898     infoPtr->firstDayHighWord = TRUE;
899   }
900   else
901   {
902     infoPtr->firstDay = day;
903     infoPtr->firstDayHighWord = TRUE;
904   }
905
906   return prev;
907 }
908
909
910 static LRESULT
911 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr)
912 {
913   TRACE("\n");
914
915   return infoPtr->monthRange;
916 }
917
918
919 static LRESULT
920 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
921 {
922   return(infoPtr->todayrect.right - infoPtr->todayrect.left);
923 }
924
925
926 static LRESULT
927 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
928 {
929     FILETIME ft_min, ft_max;
930
931     TRACE("%x %p\n", limits, range);
932
933     if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
934         (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
935         return FALSE;
936
937     if (limits & GDTR_MIN)
938     {
939         if (!MONTHCAL_ValidateTime(&range[0]))
940             MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
941
942         infoPtr->minDate = range[0];
943         infoPtr->rangeValid |= GDTR_MIN;
944     }
945     if (limits & GDTR_MAX)
946     {
947         if (!MONTHCAL_ValidateTime(&range[1]))
948             MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
949
950         infoPtr->maxDate = range[1];
951         infoPtr->rangeValid |= GDTR_MAX;
952     }
953
954     /* Only one limit set - we are done */
955     if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
956         return TRUE;
957
958     SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
959     SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
960
961     if (CompareFileTime(&ft_min, &ft_max) >= 0)
962     {
963         if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
964         {
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;
969         }
970         else
971         {
972             static const SYSTEMTIME zero;
973
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 ;
978         }
979     }
980
981     return TRUE;
982 }
983
984
985 static LRESULT
986 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
987 {
988   TRACE("%p\n", range);
989
990   if(!range) return FALSE;
991
992   range[1] = infoPtr->maxDate;
993   range[0] = infoPtr->minDate;
994
995   return infoPtr->rangeValid;
996 }
997
998
999 static LRESULT
1000 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1001 {
1002   int i;
1003
1004   TRACE("%d %p\n", months, states);
1005   if(months != infoPtr->monthRange) return 0;
1006
1007   for(i = 0; i < months; i++)
1008     infoPtr->monthdayState[i] = states[i];
1009
1010   return 1;
1011 }
1012
1013 static LRESULT
1014 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1015 {
1016   TRACE("%p\n", curSel);
1017   if(!curSel) return FALSE;
1018   if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1019
1020   *curSel = infoPtr->minSel;
1021   TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1022   return TRUE;
1023 }
1024
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)
1029 {
1030   TRACE("%p\n", curSel);
1031   if(!curSel) return FALSE;
1032   if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1033
1034   if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1035
1036   infoPtr->minSel = *curSel;
1037   infoPtr->maxSel = *curSel;
1038
1039   /* exit earlier if selection equals current */
1040   if (MONTHCAL_IsDateEqual(&infoPtr->curSel, curSel)) return TRUE;
1041
1042   infoPtr->curSel = *curSel;
1043
1044   InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1045
1046   return TRUE;
1047 }
1048
1049
1050 static LRESULT
1051 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1052 {
1053   return infoPtr->maxSelCount;
1054 }
1055
1056
1057 static LRESULT
1058 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1059 {
1060   TRACE("%d\n", max);
1061
1062   if(infoPtr->dwStyle & MCS_MULTISELECT)  {
1063     infoPtr->maxSelCount = max;
1064   }
1065
1066   return TRUE;
1067 }
1068
1069
1070 static LRESULT
1071 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1072 {
1073   TRACE("%p\n", range);
1074
1075   if(!range) return FALSE;
1076
1077   if(infoPtr->dwStyle & MCS_MULTISELECT)
1078   {
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;
1083   }
1084
1085   return FALSE;
1086 }
1087
1088
1089 static LRESULT
1090 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1091 {
1092   TRACE("%p\n", range);
1093
1094   if(!range) return FALSE;
1095
1096   if(infoPtr->dwStyle & MCS_MULTISELECT)
1097   {
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]);
1103
1104     infoPtr->minSel = range[0];
1105     infoPtr->maxSel = range[1];
1106
1107     TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1108     return TRUE;
1109   }
1110
1111   return FALSE;
1112 }
1113
1114
1115 static LRESULT
1116 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1117 {
1118   TRACE("%p\n", today);
1119
1120   if(!today) return FALSE;
1121   *today = infoPtr->todaysDate;
1122   return TRUE;
1123 }
1124
1125
1126 static LRESULT
1127 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1128 {
1129   TRACE("%p\n", today);
1130
1131   if(!today) return FALSE;
1132
1133   if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return TRUE;
1134
1135   infoPtr->todaysDate = *today;
1136   InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1137   return TRUE;
1138 }
1139
1140
1141 static LRESULT
1142 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1143 {
1144   UINT x,y;
1145   DWORD retval;
1146   int day,wday,wnum;
1147
1148   if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1149
1150   x = lpht->pt.x;
1151   y = lpht->pt.y;
1152
1153   ZeroMemory(&lpht->st, sizeof(lpht->st));
1154
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);
1165   */
1166
1167   /* are we in the header? */
1168
1169   if(PtInRect(&infoPtr->title, lpht->pt)) {
1170     if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1171       retval = MCHT_TITLEBTNPREV;
1172       goto done;
1173     }
1174     if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1175       retval = MCHT_TITLEBTNNEXT;
1176       goto done;
1177     }
1178     if(PtInRect(&infoPtr->titlemonth, lpht->pt)) {
1179       retval = MCHT_TITLEMONTH;
1180       goto done;
1181     }
1182     if(PtInRect(&infoPtr->titleyear, lpht->pt)) {
1183       retval = MCHT_TITLEYEAR;
1184       goto done;
1185     }
1186
1187     retval = MCHT_TITLE;
1188     goto done;
1189   }
1190
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;
1199   }
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;
1211   }
1212   if(PtInRect(&infoPtr->days, lpht->pt))
1213   {
1214       lpht->st.wYear  = infoPtr->curSel.wYear;
1215       if ( day < 1)
1216       {
1217           retval = MCHT_CALENDARDATEPREV;
1218           lpht->st.wMonth = infoPtr->curSel.wMonth - 1;
1219           if (lpht->st.wMonth < 1)
1220           {
1221               lpht->st.wMonth = 12;
1222               lpht->st.wYear--;
1223           }
1224           lpht->st.wDay = MONTHCAL_MonthLength(lpht->st.wMonth,lpht->st.wYear) + day;
1225       }
1226       else if (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear))
1227       {
1228           retval = MCHT_CALENDARDATENEXT;
1229           lpht->st.wMonth = infoPtr->curSel.wMonth + 1;
1230           if (lpht->st.wMonth > 12)
1231           {
1232               lpht->st.wMonth = 1;
1233               lpht->st.wYear++;
1234           }
1235           lpht->st.wDay = day - MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear);
1236       }
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);
1242       }
1243       goto done;
1244     }
1245   if(PtInRect(&infoPtr->todayrect, lpht->pt)) {
1246     retval = MCHT_TODAYLINK;
1247     goto done;
1248   }
1249
1250
1251   /* Hit nothing special? What's left must be background :-) */
1252
1253   retval = MCHT_CALENDARBK;
1254  done:
1255   lpht->uHit = retval;
1256   return retval;
1257 }
1258
1259 /* MCN_GETDAYSTATE notification helper */
1260 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1261 {
1262   if(infoPtr->dwStyle & MCS_DAYSTATE) {
1263     NMDAYSTATE nmds;
1264     INT i;
1265
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));
1271
1272     nmds.stStart = infoPtr->todaysDate;
1273     nmds.stStart.wYear  = infoPtr->curSel.wYear;
1274     nmds.stStart.wMonth = infoPtr->curSel.wMonth;
1275     nmds.stStart.wDay = 1;
1276
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];
1280
1281     Free(nmds.prgDayState);
1282   }
1283 }
1284
1285 static void MONTHCAL_GoToNextMonth(MONTHCAL_INFO *infoPtr)
1286 {
1287   SYSTEMTIME next = infoPtr->curSel;
1288
1289   TRACE("\n");
1290
1291   next.wMonth++;
1292   if(next.wMonth > 12) {
1293     next.wYear++;
1294     next.wMonth = 1;
1295   }
1296
1297   /* prevent max range exceeding */
1298   if(infoPtr->rangeValid & GDTR_MAX)
1299   {
1300      FILETIME ft_next, ft_max;
1301
1302      SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1303      SystemTimeToFileTime(&next, &ft_next);
1304
1305      if (CompareFileTime(&ft_next, &ft_max) > 0) return;
1306   }
1307
1308   infoPtr->curSel = next;
1309
1310   MONTHCAL_NotifyDayState(infoPtr);
1311 }
1312
1313
1314 static void MONTHCAL_GoToPrevMonth(MONTHCAL_INFO *infoPtr)
1315 {
1316   SYSTEMTIME prev = infoPtr->curSel;
1317
1318   TRACE("\n");
1319
1320   prev.wMonth--;
1321   if(prev.wMonth < 1) {
1322     prev.wYear--;
1323     prev.wMonth = 12;
1324   }
1325
1326   /* prevent min range exceeding */
1327   if(infoPtr->rangeValid & GDTR_MIN)
1328   {
1329      FILETIME ft_prev, ft_min;
1330
1331      SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1332      SystemTimeToFileTime(&prev, &ft_prev);
1333
1334      if (CompareFileTime(&ft_prev, &ft_min) < 0) return;
1335   }
1336
1337   infoPtr->curSel = prev;
1338
1339   MONTHCAL_NotifyDayState(infoPtr);
1340 }
1341
1342 static LRESULT
1343 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1344 {
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];
1349
1350   hMenu = CreatePopupMenu();
1351   if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1352   {
1353       WARN("Can't load resource\n");
1354       strcpyW(buf, todayW);
1355   }
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))
1362   {
1363       infoPtr->curSel = infoPtr->todaysDate;
1364       infoPtr->minSel = infoPtr->todaysDate;
1365       infoPtr->maxSel = infoPtr->todaysDate;
1366       InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1367   }
1368
1369   return 0;
1370 }
1371
1372 /* creates updown control and edit box */
1373 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr)
1374 {
1375     static const WCHAR EditW[] = { 'E','D','I','T',0 };
1376
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);
1383
1384     SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
1385
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);
1392
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);
1397 }
1398
1399 static LRESULT
1400 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1401 {
1402   MCHITTESTINFO ht;
1403   DWORD hit;
1404
1405   if (infoPtr->hWndYearUpDown)
1406   {
1407       infoPtr->curSel.wYear = SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, 0);
1408       if(!DestroyWindow(infoPtr->hWndYearUpDown))
1409       {
1410           FIXME("Can't destroy Updown Control\n");
1411       }
1412       else
1413           infoPtr->hWndYearUpDown = 0;
1414
1415       if(!DestroyWindow(infoPtr->hWndYearEdit))
1416       {
1417           FIXME("Can't destroy Updown Control\n");
1418       }
1419       else
1420           infoPtr->hWndYearEdit = 0;
1421
1422       InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1423   }
1424
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);
1429
1430   hit = MONTHCAL_HitTest(infoPtr, &ht);
1431
1432   switch(hit)
1433   {
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;
1440
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;
1447
1448   case MCHT_TITLEMONTH:
1449   {
1450     HMENU hMenu = CreatePopupMenu();
1451     WCHAR buf[32];
1452     POINT menupoint;
1453     INT i;
1454
1455     for (i = 0; i < 12; i++)
1456     {
1457         GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
1458         AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
1459     }
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);
1465
1466     if ((i > 0) && (i < 13))
1467     {
1468         infoPtr->curSel.wMonth = i;
1469         InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1470     }
1471     return 0;
1472   }
1473   case MCHT_TITLEYEAR:
1474   {
1475     MONTHCAL_EditYear(infoPtr);
1476     return 0;
1477   }
1478   case MCHT_TODAYLINK:
1479   {
1480     NMSELCHANGE nmsc;
1481
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);
1487
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);
1494
1495     nmsc.nmhdr.code     = MCN_SELECT;
1496     SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1497     return 0;
1498   }
1499   case MCHT_CALENDARDATE:
1500   {
1501     RECT rcDay; /* used in determining area to invalidate */
1502     SYSTEMTIME selArray[2];
1503     NMSELCHANGE nmsc;
1504
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;
1515
1516     SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1517
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);
1522
1523       MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->curSel.wDay, infoPtr->curSel.wMonth, &rcDay);
1524       InvalidateRect(infoPtr->hwndSelf, &rcDay, TRUE);
1525     }
1526
1527     infoPtr->firstSelDay = ht.st.wDay;
1528     infoPtr->curSel.wDay = ht.st.wDay;
1529     infoPtr->status = MC_SEL_LBUTDOWN;
1530     return 0;
1531   }
1532   }
1533
1534   return 1;
1535 }
1536
1537
1538 static LRESULT
1539 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1540 {
1541   NMSELCHANGE nmsc;
1542   NMHDR nmhdr;
1543   BOOL redraw = FALSE;
1544   MCHITTESTINFO ht;
1545   DWORD hit;
1546
1547   TRACE("\n");
1548
1549   if(infoPtr->status & MC_NEXTPRESSED) {
1550     KillTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER);
1551     infoPtr->status &= ~MC_NEXTPRESSED;
1552     redraw = TRUE;
1553   }
1554   if(infoPtr->status & MC_PREVPRESSED) {
1555     KillTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER);
1556     infoPtr->status &= ~MC_PREVPRESSED;
1557     redraw = TRUE;
1558   }
1559
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);
1564
1565   infoPtr->status = MC_SEL_LBUTUP;
1566
1567   if(hit == MCHT_CALENDARDATENEXT) {
1568     MONTHCAL_GoToNextMonth(infoPtr);
1569     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1570     return TRUE;
1571   }
1572   if(hit == MCHT_CALENDARDATEPREV){
1573     MONTHCAL_GoToPrevMonth(infoPtr);
1574     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1575     return TRUE;
1576   }
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);
1581
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;
1593
1594     SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1595
1596   }
1597   return 0;
1598 }
1599
1600
1601 static LRESULT
1602 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1603 {
1604   BOOL redraw = FALSE;
1605
1606   TRACE("%ld\n", wParam);
1607
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;
1620   }
1621
1622   /* redraw only if necessary */
1623   if(redraw)
1624     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1625
1626   return 0;
1627 }
1628
1629
1630 static LRESULT
1631 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1632 {
1633   MCHITTESTINFO ht;
1634   int oldselday, selday, hit;
1635   RECT r;
1636
1637   if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
1638
1639   ht.cbSize = sizeof(MCHITTESTINFO);
1640   ht.pt.x = (short)LOWORD(lParam);
1641   ht.pt.y = (short)HIWORD(lParam);
1642
1643   hit = MONTHCAL_HitTest(infoPtr, &ht);
1644
1645   /* not on the calendar date numbers? bail out */
1646   TRACE("hit:%x\n",hit);
1647   if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE) return 0;
1648
1649   selday = ht.st.wDay;
1650   oldselday = infoPtr->curSel.wDay;
1651   infoPtr->curSel.wDay = selday;
1652   MONTHCAL_CalcPosFromDay(infoPtr, selday, ht.st. wMonth, &r);
1653
1654   if(infoPtr->dwStyle & MCS_MULTISELECT)  {
1655     SYSTEMTIME selArray[2];
1656     int i;
1657
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;
1667     }
1668
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;
1674     }
1675
1676     if(selArray[i].wDay!=selday) {
1677       TRACE("newRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1678
1679       selArray[i].wDay = selday;
1680
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;
1686       }
1687
1688       MONTHCAL_SetSelRange(infoPtr, selArray);
1689     }
1690   }
1691
1692 done:
1693
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);
1699
1700   return 0;
1701 }
1702
1703
1704 static LRESULT
1705 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
1706 {
1707   HDC hdc;
1708   PAINTSTRUCT ps;
1709
1710   if (hdc_paint)
1711   {
1712     GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
1713     hdc = hdc_paint;
1714   }
1715   else
1716     hdc = BeginPaint(infoPtr->hwndSelf, &ps);
1717
1718   MONTHCAL_Refresh(infoPtr, hdc, &ps);
1719   if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
1720   return 0;
1721 }
1722
1723
1724 static LRESULT
1725 MONTHCAL_KillFocus(const MONTHCAL_INFO *infoPtr, HWND hFocusWnd)
1726 {
1727   TRACE("\n");
1728
1729   if (infoPtr->hwndNotify != hFocusWnd)
1730     ShowWindow(infoPtr->hwndSelf, SW_HIDE);
1731   else
1732     InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1733
1734   return 0;
1735 }
1736
1737
1738 static LRESULT
1739 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
1740 {
1741   TRACE("\n");
1742
1743   InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1744
1745   return 0;
1746 }
1747
1748 /* sets the size information */
1749 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
1750 {
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, dx, dy;
1767   RECT rcClient;
1768
1769   GetClientRect(infoPtr->hwndSelf, &rcClient);
1770
1771   currentFont = SelectObject(hdc, infoPtr->hFont);
1772
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;
1778
1779   /* recalculate the height and width increments and offsets */
1780   GetTextExtentPoint32W(hdc, O0W, 2, &size);
1781
1782   xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
1783
1784   infoPtr->width_increment  = size.cx * 2 + 4;
1785   infoPtr->height_increment = infoPtr->textHeight;
1786
1787   /* calculate title area */
1788   title->top    = 0;
1789   title->bottom = 3 * infoPtr->height_increment / 2;
1790   title->left   = 0;
1791   title->right  = infoPtr->width_increment * xdiv;
1792
1793   /* set the dimensions of the next and previous buttons and center */
1794   /* the month text vertically */
1795   prev->top    = next->top    = title->top + 4;
1796   prev->bottom = next->bottom = title->bottom - 4;
1797   prev->left   = title->left + 4;
1798   prev->right  = prev->left + (title->bottom - title->top);
1799   next->right  = title->right - 4;
1800   next->left   = next->right - (title->bottom - title->top);
1801
1802   /* titlemonth->left and right change based upon the current month */
1803   /* and are recalculated in refresh as the current month may change */
1804   /* without the control being resized */
1805   titlemonth->top    = titleyear->top    = title->top    + (infoPtr->height_increment)/2;
1806   titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
1807
1808   /* setup the dimensions of the rectangle we draw the names of the */
1809   /* days of the week in */
1810   weeknumrect->left = 0;
1811
1812   if(infoPtr->dwStyle & MCS_WEEKNUMBERS)
1813     weeknumrect->right = prev->right;
1814   else
1815     weeknumrect->right = weeknumrect->left;
1816
1817   wdays->left   = days->left   = weeknumrect->right;
1818   wdays->right  = days->right  = wdays->left + 7 * infoPtr->width_increment;
1819   wdays->top    = title->bottom;
1820   wdays->bottom = wdays->top + infoPtr->height_increment;
1821
1822   days->top    = weeknumrect->top = wdays->bottom;
1823   days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
1824
1825   todayrect->left   = 0;
1826   todayrect->right  = title->right;
1827   todayrect->top    = days->bottom;
1828   todayrect->bottom = days->bottom + infoPtr->height_increment;
1829
1830   /* offset all rectangles to center in client area */
1831   dx = (rcClient.right  - title->right) / 2;
1832   dy = (rcClient.bottom - todayrect->bottom) / 2;
1833
1834   /* if calendar doesn't fit client area show it at left/top bounds */
1835   if (title->left + dx < 0) dx = 0;
1836   if (title->top  + dy < 0) dy = 0;
1837
1838   if (dx != 0 || dy != 0)
1839   {
1840     OffsetRect(title, dx, dy);
1841     OffsetRect(prev,  dx, dy);
1842     OffsetRect(next,  dx, dy);
1843     OffsetRect(titlemonth, dx, dy);
1844     OffsetRect(titleyear, dx, dy);
1845     OffsetRect(wdays, dx, dy);
1846     OffsetRect(weeknumrect, dx, dy);
1847     OffsetRect(days, dx, dy);
1848     OffsetRect(todayrect, dx, dy);
1849   }
1850
1851   TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
1852         infoPtr->width_increment,infoPtr->height_increment,
1853         wine_dbgstr_rect(&rcClient),
1854         wine_dbgstr_rect(title),
1855         wine_dbgstr_rect(wdays),
1856         wine_dbgstr_rect(days),
1857         wine_dbgstr_rect(todayrect));
1858
1859   /* restore the originally selected font */
1860   SelectObject(hdc, currentFont);
1861
1862   ReleaseDC(infoPtr->hwndSelf, hdc);
1863 }
1864
1865 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
1866 {
1867   TRACE("(width=%d, height=%d)\n", Width, Height);
1868
1869   MONTHCAL_UpdateSize(infoPtr);
1870
1871   /* invalidate client area and erase background */
1872   InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1873
1874   return 0;
1875 }
1876
1877 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
1878 {
1879     return (LRESULT)infoPtr->hFont;
1880 }
1881
1882 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
1883 {
1884     HFONT hOldFont;
1885     LOGFONTW lf;
1886
1887     if (!hFont) return 0;
1888
1889     hOldFont = infoPtr->hFont;
1890     infoPtr->hFont = hFont;
1891
1892     GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
1893     lf.lfWeight = FW_BOLD;
1894     infoPtr->hBoldFont = CreateFontIndirectW(&lf);
1895
1896     MONTHCAL_UpdateSize(infoPtr);
1897
1898     if (redraw)
1899         InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1900
1901     return (LRESULT)hOldFont;
1902 }
1903
1904 /* update theme after a WM_THEMECHANGED message */
1905 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
1906 {
1907     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
1908     CloseThemeData (theme);
1909     OpenThemeData (infoPtr->hwndSelf, themeClass);
1910     return 0;
1911 }
1912
1913 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
1914                                  const STYLESTRUCT *lpss)
1915 {
1916     TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
1917           wStyleType, lpss->styleOld, lpss->styleNew);
1918
1919     if (wStyleType != GWL_STYLE) return 0;
1920
1921     infoPtr->dwStyle = lpss->styleNew;
1922
1923     return 0;
1924 }
1925
1926 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
1927 static LRESULT
1928 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
1929 {
1930   MONTHCAL_INFO *infoPtr;
1931
1932   /* allocate memory for info structure */
1933   infoPtr = Alloc(sizeof(MONTHCAL_INFO));
1934   SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
1935
1936   if(infoPtr == NULL) {
1937     ERR( "could not allocate info memory!\n");
1938     return 0;
1939   }
1940
1941   infoPtr->hwndSelf = hwnd;
1942   infoPtr->hwndNotify = lpcs->hwndParent;
1943   infoPtr->dwStyle  = GetWindowLongW(hwnd, GWL_STYLE);
1944
1945   MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
1946
1947   /* initialize info structure */
1948   /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
1949
1950   GetLocalTime(&infoPtr->todaysDate);
1951   infoPtr->firstDayHighWord = FALSE;
1952   MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
1953
1954   infoPtr->maxSelCount   = 7;
1955   infoPtr->monthRange    = 3;
1956   infoPtr->monthdayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1957   infoPtr->titlebk       = comctl32_color.clrActiveCaption;
1958   infoPtr->titletxt      = comctl32_color.clrWindow;
1959   infoPtr->monthbk       = comctl32_color.clrWindow;
1960   infoPtr->trailingtxt   = comctl32_color.clrGrayText;
1961   infoPtr->bk            = comctl32_color.clrWindow;
1962   infoPtr->txt           = comctl32_color.clrWindowText;
1963
1964   infoPtr->minSel = infoPtr->todaysDate;
1965   infoPtr->maxSel = infoPtr->todaysDate;
1966   infoPtr->curSel = infoPtr->todaysDate;
1967
1968   /* call MONTHCAL_UpdateSize to set all of the dimensions */
1969   /* of the control */
1970   MONTHCAL_UpdateSize(infoPtr);
1971   
1972   OpenThemeData (infoPtr->hwndSelf, themeClass);
1973
1974   return 0;
1975 }
1976
1977
1978 static LRESULT
1979 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
1980 {
1981   /* free month calendar info data */
1982   Free(infoPtr->monthdayState);
1983   SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
1984
1985   CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
1986   
1987   Free(infoPtr);
1988   return 0;
1989 }
1990
1991
1992 static LRESULT WINAPI
1993 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1994 {
1995   MONTHCAL_INFO *infoPtr;
1996
1997   TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
1998
1999   infoPtr = MONTHCAL_GetInfoPtr(hwnd);
2000   if (!infoPtr && (uMsg != WM_CREATE))
2001     return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2002   switch(uMsg)
2003   {
2004   case MCM_GETCURSEL:
2005     return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2006
2007   case MCM_SETCURSEL:
2008     return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2009
2010   case MCM_GETMAXSELCOUNT:
2011     return MONTHCAL_GetMaxSelCount(infoPtr);
2012
2013   case MCM_SETMAXSELCOUNT:
2014     return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2015
2016   case MCM_GETSELRANGE:
2017     return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2018
2019   case MCM_SETSELRANGE:
2020     return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2021
2022   case MCM_GETMONTHRANGE:
2023     return MONTHCAL_GetMonthRange(infoPtr);
2024
2025   case MCM_SETDAYSTATE:
2026     return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2027
2028   case MCM_GETMINREQRECT:
2029     return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2030
2031   case MCM_GETCOLOR:
2032     return MONTHCAL_GetColor(infoPtr, wParam);
2033
2034   case MCM_SETCOLOR:
2035     return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2036
2037   case MCM_GETTODAY:
2038     return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2039
2040   case MCM_SETTODAY:
2041     return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2042
2043   case MCM_HITTEST:
2044     return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2045
2046   case MCM_GETFIRSTDAYOFWEEK:
2047     return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2048
2049   case MCM_SETFIRSTDAYOFWEEK:
2050     return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2051
2052   case MCM_GETRANGE:
2053     return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2054
2055   case MCM_SETRANGE:
2056     return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2057
2058   case MCM_GETMONTHDELTA:
2059     return MONTHCAL_GetMonthDelta(infoPtr);
2060
2061   case MCM_SETMONTHDELTA:
2062     return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2063
2064   case MCM_GETMAXTODAYWIDTH:
2065     return MONTHCAL_GetMaxTodayWidth(infoPtr);
2066
2067   case WM_GETDLGCODE:
2068     return DLGC_WANTARROWS | DLGC_WANTCHARS;
2069
2070   case WM_KILLFOCUS:
2071     return MONTHCAL_KillFocus(infoPtr, (HWND)wParam);
2072
2073   case WM_RBUTTONUP:
2074     return MONTHCAL_RButtonUp(infoPtr, lParam);
2075
2076   case WM_LBUTTONDOWN:
2077     return MONTHCAL_LButtonDown(infoPtr, lParam);
2078
2079   case WM_MOUSEMOVE:
2080     return MONTHCAL_MouseMove(infoPtr, lParam);
2081
2082   case WM_LBUTTONUP:
2083     return MONTHCAL_LButtonUp(infoPtr, lParam);
2084
2085   case WM_PRINTCLIENT:
2086   case WM_PAINT:
2087     return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2088
2089   case WM_SETFOCUS:
2090     return MONTHCAL_SetFocus(infoPtr);
2091
2092   case WM_SIZE:
2093     return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2094
2095   case WM_CREATE:
2096     return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2097
2098   case WM_SETFONT:
2099     return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2100
2101   case WM_GETFONT:
2102     return MONTHCAL_GetFont(infoPtr);
2103
2104   case WM_TIMER:
2105     return MONTHCAL_Timer(infoPtr, wParam);
2106     
2107   case WM_THEMECHANGED:
2108     return theme_changed (infoPtr);
2109
2110   case WM_DESTROY:
2111     return MONTHCAL_Destroy(infoPtr);
2112
2113   case WM_SYSCOLORCHANGE:
2114     COMCTL32_RefreshSysColors();
2115     return 0;
2116
2117   case WM_STYLECHANGED:
2118     return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2119
2120   default:
2121     if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2122       ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2123     return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2124   }
2125 }
2126
2127
2128 void
2129 MONTHCAL_Register(void)
2130 {
2131   WNDCLASSW wndClass;
2132
2133   ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2134   wndClass.style         = CS_GLOBALCLASS;
2135   wndClass.lpfnWndProc   = MONTHCAL_WindowProc;
2136   wndClass.cbClsExtra    = 0;
2137   wndClass.cbWndExtra    = sizeof(MONTHCAL_INFO *);
2138   wndClass.hCursor       = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2139   wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2140   wndClass.lpszClassName = MONTHCAL_CLASSW;
2141
2142   RegisterClassW(&wndClass);
2143 }
2144
2145
2146 void
2147 MONTHCAL_Unregister(void)
2148 {
2149     UnregisterClassW(MONTHCAL_CLASSW, NULL);
2150 }