comctl32: Remove redundant NULL checks before Free() calls.
[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  *    -- GetRange: At the moment, we copy ranges anyway, regardless of
40  *                 infoPtr->rangeValid; an invalid range is simply filled 
41  *                 with zeros in SetRange.  Is this the right behavior?
42  *    -- search for FIXME
43  */
44
45 #include <math.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50
51 #include "windef.h"
52 #include "winbase.h"
53 #include "wingdi.h"
54 #include "winuser.h"
55 #include "winnls.h"
56 #include "commctrl.h"
57 #include "comctl32.h"
58 #include "uxtheme.h"
59 #include "tmschema.h"
60 #include "wine/unicode.h"
61 #include "wine/debug.h"
62
63 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
64
65 #define MC_SEL_LBUTUP       1   /* Left button released */
66 #define MC_SEL_LBUTDOWN     2   /* Left button pressed in calendar */
67 #define MC_PREVPRESSED      4   /* Prev month button pressed */
68 #define MC_NEXTPRESSED      8   /* Next month button pressed */
69 #define MC_NEXTMONTHDELAY   350 /* when continuously pressing `next */
70                                                                                 /* month', wait 500 ms before going */
71                                                                                 /* to the next month */
72 #define MC_NEXTMONTHTIMER   1                   /* Timer ID's */
73 #define MC_PREVMONTHTIMER   2
74
75 #define countof(arr) (sizeof(arr)/sizeof(arr[0]))
76
77 typedef struct
78 {
79     HWND hwndSelf;
80     COLORREF    bk;
81     COLORREF    txt;
82     COLORREF    titlebk;
83     COLORREF    titletxt;
84     COLORREF    monthbk;
85     COLORREF    trailingtxt;
86     HFONT       hFont;
87     HFONT       hBoldFont;
88     int         textHeight;
89     int         textWidth;
90     int         height_increment;
91     int         width_increment;
92     int         firstDayplace; /* place of the first day of the current month */
93     int         delta;  /* scroll rate; # of months that the */
94                         /* control moves when user clicks a scroll button */
95     int         visible;        /* # of months visible */
96     int         firstDay;       /* Start month calendar with firstDay's day */
97     int         monthRange;
98     MONTHDAYSTATE *monthdayState;
99     SYSTEMTIME  todaysDate;
100     DWORD       currentMonth;
101     DWORD       currentYear;
102     int         status;         /* See MC_SEL flags */
103     int         curSelDay;      /* current selected day */
104     int         firstSelDay;    /* first selected day */
105     int         maxSelCount;
106     SYSTEMTIME  minSel;
107     SYSTEMTIME  maxSel;
108     DWORD       rangeValid;
109     SYSTEMTIME  minDate;
110     SYSTEMTIME  maxDate;
111
112     RECT title;         /* rect for the header above the calendar */
113     RECT titlebtnnext;  /* the `next month' button in the header */
114     RECT titlebtnprev;  /* the `prev month' button in the header */
115     RECT titlemonth;    /* the `month name' txt in the header */
116     RECT titleyear;     /* the `year number' txt in the header */
117     RECT wdays;         /* week days at top */
118     RECT days;          /* calendar area */
119     RECT weeknums;      /* week numbers at left side */
120     RECT todayrect;     /* `today: xx/xx/xx' text rect */
121     HWND hwndNotify;    /* Window to receive the notifications */
122     HWND hWndYearEdit;  /* Window Handle of edit box to handle years */
123     HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
124 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
125
126
127 /* Offsets of days in the week to the weekday of january 1 in a leap year */
128 static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
129
130 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
131
132 #define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0))
133
134 /* helper functions  */
135
136 /* returns the number of days in any given month, checking for leap days */
137 /* january is 1, december is 12 */
138 int MONTHCAL_MonthLength(int month, int year)
139 {
140   const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0};
141   /*Wrap around, this eases handling*/
142   if(month == 0)
143     month = 12;
144   if(month == 13)
145     month = 1;
146
147   /* if we have a leap year add 1 day to February */
148   /* a leap year is a year either divisible by 400 */
149   /* or divisible by 4 and not by 100 */
150   if(month == 2) { /* February */
151     return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
152      (year%4 == 0)) ? 1 : 0);
153   }
154   else {
155     return mdays[month - 1];
156   }
157 }
158
159
160 /* make sure that time is valid */
161 static int MONTHCAL_ValidateTime(SYSTEMTIME time)
162 {
163   if(time.wMonth < 1 || time.wMonth > 12 ) return FALSE;
164   if(time.wDayOfWeek > 6) return FALSE;
165   if(time.wDay > MONTHCAL_MonthLength(time.wMonth, time.wYear))
166           return FALSE;
167
168   return TRUE;
169 }
170
171
172 /* Note:Depending on DST, this may be offset by a day.
173    Need to find out if we're on a DST place & adjust the clock accordingly.
174    Above function assumes we have a valid data.
175    Valid for year>1752;  1 <= d <= 31, 1 <= m <= 12.
176    0 = Sunday.
177 */
178
179 /* returns the day in the week(0 == sunday, 6 == saturday) */
180 /* day(1 == 1st, 2 == 2nd... etc), year is the  year value */
181 static int MONTHCAL_CalculateDayOfWeek(DWORD day, DWORD month, DWORD year)
182 {
183   year-=(month < 3);
184
185   return((year + year/4 - year/100 + year/400 +
186          DayOfWeekTable[month-1] + day ) % 7);
187 }
188
189 /* From a given point, calculate the row (weekpos), column(daypos)
190    and day in the calendar. day== 0 mean the last day of tha last month
191 */
192 static int MONTHCAL_CalcDayFromPos(MONTHCAL_INFO *infoPtr, int x, int y,
193                                    int *daypos,int *weekpos)
194 {
195   int retval, firstDay;
196   RECT rcClient;
197
198   GetClientRect(infoPtr->hwndSelf, &rcClient);
199
200   /* if the point is outside the x bounds of the window put
201   it at the boundary */
202   if (x > rcClient.right)
203     x = rcClient.right;
204
205
206   *daypos = (x - infoPtr->days.left ) / infoPtr->width_increment;
207   *weekpos = (y - infoPtr->days.top ) / infoPtr->height_increment;
208
209   firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear)+6 - infoPtr->firstDay)%7;
210   retval = *daypos + (7 * *weekpos) - firstDay;
211   return retval;
212 }
213
214 /* day is the day of the month, 1 == 1st day of the month */
215 /* sets x and y to be the position of the day */
216 /* x == day, y == week where(0,0) == firstDay, 1st week */
217 static void MONTHCAL_CalcDayXY(MONTHCAL_INFO *infoPtr, int day, int month,
218                                  int *x, int *y)
219 {
220   int firstDay, prevMonth;
221
222   firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear) +6 - infoPtr->firstDay)%7;
223
224   if(month==infoPtr->currentMonth) {
225     *x = (day + firstDay) % 7;
226     *y = (day + firstDay - *x) / 7;
227     return;
228   }
229   if(month < infoPtr->currentMonth) {
230     prevMonth = month - 1;
231     if(prevMonth==0)
232        prevMonth = 12;
233
234     *x = (MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear) - firstDay) % 7;
235     *y = 0;
236     return;
237   }
238
239   *y = MONTHCAL_MonthLength(month, infoPtr->currentYear - 1) / 7;
240   *x = (day + firstDay + MONTHCAL_MonthLength(month,
241        infoPtr->currentYear)) % 7;
242 }
243
244
245 /* x: column(day), y: row(week) */
246 static void MONTHCAL_CalcDayRect(MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
247 {
248   r->left = infoPtr->days.left + x * infoPtr->width_increment;
249   r->right = r->left + infoPtr->width_increment;
250   r->top  = infoPtr->days.top  + y * infoPtr->height_increment;
251   r->bottom = r->top + infoPtr->textHeight;
252 }
253
254
255 /* sets the RECT struct r to the rectangle around the day and month */
256 /* day is the day value of the month(1 == 1st), month is the month */
257 /* value(january == 1, december == 12) */
258 static inline void MONTHCAL_CalcPosFromDay(MONTHCAL_INFO *infoPtr,
259                                             int day, int month, RECT *r)
260 {
261   int x, y;
262
263   MONTHCAL_CalcDayXY(infoPtr, day, month, &x, &y);
264   MONTHCAL_CalcDayRect(infoPtr, r, x, y);
265 }
266
267
268 /* day is the day in the month(1 == 1st of the month) */
269 /* month is the month value(1 == january, 12 == december) */
270 static void MONTHCAL_CircleDay(MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month)
271 {
272   HPEN hRedPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
273   HPEN hOldPen2 = SelectObject(hdc, hRedPen);
274   POINT points[13];
275   int x, y;
276   RECT day_rect;
277
278
279   MONTHCAL_CalcPosFromDay(infoPtr, day, month, &day_rect);
280
281   x = day_rect.left;
282   y = day_rect.top;
283
284   points[0].x = x;
285   points[0].y = y - 1;
286   points[1].x = x + 0.8 * infoPtr->width_increment;
287   points[1].y = y - 1;
288   points[2].x = x + 0.9 * infoPtr->width_increment;
289   points[2].y = y;
290   points[3].x = x + infoPtr->width_increment;
291   points[3].y = y + 0.5 * infoPtr->height_increment;
292
293   points[4].x = x + infoPtr->width_increment;
294   points[4].y = y + 0.9 * infoPtr->height_increment;
295   points[5].x = x + 0.6 * infoPtr->width_increment;
296   points[5].y = y + 0.9 * infoPtr->height_increment;
297   points[6].x = x + 0.5 * infoPtr->width_increment;
298   points[6].y = y + 0.9 * infoPtr->height_increment; /* bring the bottom up just
299                                 a hair to fit inside the day rectangle */
300
301   points[7].x = x + 0.2 * infoPtr->width_increment;
302   points[7].y = y + 0.8 * infoPtr->height_increment;
303   points[8].x = x + 0.1 * infoPtr->width_increment;
304   points[8].y = y + 0.8 * infoPtr->height_increment;
305   points[9].x = x;
306   points[9].y = y + 0.5 * infoPtr->height_increment;
307
308   points[10].x = x + 0.1 * infoPtr->width_increment;
309   points[10].y = y + 0.2 * infoPtr->height_increment;
310   points[11].x = x + 0.2 * infoPtr->width_increment;
311   points[11].y = y + 0.3 * infoPtr->height_increment;
312   points[12].x = x + 0.4 * infoPtr->width_increment;
313   points[12].y = y + 0.2 * infoPtr->height_increment;
314
315   PolyBezier(hdc, points, 13);
316   DeleteObject(hRedPen);
317   SelectObject(hdc, hOldPen2);
318 }
319
320
321 static void MONTHCAL_DrawDay(MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month,
322                              int x, int y, int bold)
323 {
324   static const WCHAR fmtW[] = { '%','d',0 };
325   WCHAR buf[10];
326   RECT r;
327   static int haveBoldFont, haveSelectedDay = FALSE;
328   HBRUSH hbr;
329   COLORREF oldCol = 0;
330   COLORREF oldBk = 0;
331
332   wsprintfW(buf, fmtW, day);
333
334 /* No need to check styles: when selection is not valid, it is set to zero.
335  * 1<day<31, so evertyhing's OK.
336  */
337
338   MONTHCAL_CalcDayRect(infoPtr, &r, x, y);
339
340   if((day>=infoPtr->minSel.wDay) && (day<=infoPtr->maxSel.wDay)
341        && (month==infoPtr->currentMonth)) {
342     HRGN hrgn;
343     RECT r2;
344
345     TRACE("%d %d %d\n",day, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
346     TRACE("%d %d %d %d\n", r.left, r.top, r.right, r.bottom);
347     oldCol = SetTextColor(hdc, infoPtr->monthbk);
348     oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
349     hbr = GetSysColorBrush(COLOR_GRAYTEXT);
350     hrgn = CreateEllipticRgn(r.left, r.top, r.right, r.bottom);
351     FillRgn(hdc, hrgn, hbr);
352
353     /* FIXME: this may need to be changed now b/c of the other
354         drawing changes 11/3/99 CMM */
355     r2.left   = r.left - 0.25 * infoPtr->textWidth;
356     r2.top    = r.top;
357     r2.right  = r.left + 0.5 * infoPtr->textWidth;
358     r2.bottom = r.bottom;
359     if(haveSelectedDay) FillRect(hdc, &r2, hbr);
360       haveSelectedDay = TRUE;
361   } else {
362     haveSelectedDay = FALSE;
363   }
364
365   /* need to add some code for multiple selections */
366
367   if((bold) &&(!haveBoldFont)) {
368     SelectObject(hdc, infoPtr->hBoldFont);
369     haveBoldFont = TRUE;
370   }
371   if((!bold) &&(haveBoldFont)) {
372     SelectObject(hdc, infoPtr->hFont);
373     haveBoldFont = FALSE;
374   }
375
376   if(haveSelectedDay) {
377     SetTextColor(hdc, oldCol);
378     SetBkColor(hdc, oldBk);
379   }
380
381   SetBkMode(hdc,TRANSPARENT);
382   DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
383
384   /* draw a rectangle around the currently selected days text */
385   if((day==infoPtr->curSelDay) && (month==infoPtr->currentMonth))
386     DrawFocusRect(hdc, &r);
387 }
388
389
390 static void paint_button (MONTHCAL_INFO *infoPtr, HDC hdc, BOOL btnNext, 
391                           BOOL pressed, RECT* r)
392 {
393     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
394     
395     if (theme)
396     {
397         static const int states[] = {
398             /* Prev button */
399             ABS_LEFTNORMAL,  ABS_LEFTPRESSED,  ABS_LEFTDISABLED,
400             /* Next button */
401             ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
402         };
403         int stateNum = btnNext ? 3 : 0;
404         if (pressed)
405             stateNum += 1;
406         else
407         {
408             DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
409             if (dwStyle & WS_DISABLED) stateNum += 2;
410         }
411         DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
412     }
413     else
414     {
415         int style = btnNext ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
416         if (pressed)
417             style |= DFCS_PUSHED;
418         else
419         {
420             DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
421             if (dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
422         }
423         
424         DrawFrameControl(hdc, r, DFC_SCROLL, style);
425     }
426 }
427
428
429 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, PAINTSTRUCT* ps)
430 {
431   static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
432   static const WCHAR fmt1W[] = { '%','s',' ','%','l','d',0 };
433   static const WCHAR fmt2W[] = { '%','s',' ','%','s',0 };
434   static const WCHAR fmt3W[] = { '%','d',0 };
435   RECT *title=&infoPtr->title;
436   RECT *prev=&infoPtr->titlebtnprev;
437   RECT *next=&infoPtr->titlebtnnext;
438   RECT *titlemonth=&infoPtr->titlemonth;
439   RECT *titleyear=&infoPtr->titleyear;
440   RECT dayrect;
441   RECT *days=&dayrect;
442   RECT rtoday;
443   int i, j, m, mask, day, firstDay, weeknum, weeknum1,prevMonth;
444   int textHeight = infoPtr->textHeight, textWidth = infoPtr->textWidth;
445   SIZE size;
446   HBRUSH hbr;
447   HFONT currentFont;
448   WCHAR buf[20];
449   WCHAR buf1[20];
450   WCHAR buf2[32];
451   COLORREF oldTextColor, oldBkColor;
452   DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
453   RECT rcTemp;
454   RECT rcDay; /* used in MONTHCAL_CalcDayRect() */
455   SYSTEMTIME localtime;
456   int startofprescal;
457
458   oldTextColor = SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
459
460   /* fill background */
461   hbr = CreateSolidBrush (infoPtr->bk);
462   FillRect(hdc, &ps->rcPaint, hbr);
463   DeleteObject(hbr);
464
465   /* draw header */
466   if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
467   {
468     hbr =  CreateSolidBrush(infoPtr->titlebk);
469     FillRect(hdc, title, hbr);
470     DeleteObject(hbr);
471   }
472
473   /* if the previous button is pressed draw it depressed */
474   if(IntersectRect(&rcTemp, &(ps->rcPaint), prev))
475     paint_button (infoPtr, hdc, FALSE, infoPtr->status & MC_PREVPRESSED, prev);
476
477   /* if next button is depressed draw it depressed */
478   if(IntersectRect(&rcTemp, &(ps->rcPaint), next))
479     paint_button (infoPtr, hdc, TRUE, infoPtr->status & MC_NEXTPRESSED, next);
480
481   oldBkColor = SetBkColor(hdc, infoPtr->titlebk);
482   SetTextColor(hdc, infoPtr->titletxt);
483   currentFont = SelectObject(hdc, infoPtr->hBoldFont);
484
485   GetLocaleInfoW( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+infoPtr->currentMonth -1,
486                   buf1,countof(buf1));
487   wsprintfW(buf, fmt1W, buf1, infoPtr->currentYear);
488
489   if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
490   {
491     DrawTextW(hdc, buf, strlenW(buf), title,
492                         DT_CENTER | DT_VCENTER | DT_SINGLELINE);
493   }
494
495 /* titlemonth left/right contained rect for whole titletxt('June  1999')
496   * MCM_HitTestInfo wants month & year rects, so prepare these now.
497   *(no, we can't draw them separately; the whole text is centered)
498   */
499   GetTextExtentPoint32W(hdc, buf, strlenW(buf), &size);
500   titlemonth->left = title->right / 2 + title->left / 2 - size.cx / 2;
501   titleyear->right = title->right / 2 + title->left / 2 + size.cx / 2;
502   GetTextExtentPoint32W(hdc, buf1, strlenW(buf1), &size);
503   titlemonth->right = titlemonth->left + size.cx;
504   titleyear->left = titlemonth->right;
505
506   /* draw month area */
507   rcTemp.top=infoPtr->wdays.top;
508   rcTemp.left=infoPtr->wdays.left;
509   rcTemp.bottom=infoPtr->todayrect.bottom;
510   rcTemp.right =infoPtr->todayrect.right;
511   if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcTemp))
512   {
513     hbr =  CreateSolidBrush(infoPtr->monthbk);
514     FillRect(hdc, &rcTemp, hbr);
515     DeleteObject(hbr);
516   }
517
518 /* draw line under day abbreviatons */
519
520   MoveToEx(hdc, infoPtr->days.left + 3, title->bottom + textHeight + 1, NULL);
521   LineTo(hdc, infoPtr->days.right - 3, title->bottom + textHeight + 1);
522
523   prevMonth = infoPtr->currentMonth - 1;
524   if(prevMonth == 0) /* if currentMonth is january(1) prevMonth is */
525     prevMonth = 12;    /* december(12) of the previous year */
526
527   infoPtr->wdays.left   = infoPtr->days.left   = infoPtr->weeknums.right;
528 /* draw day abbreviations */
529
530   SelectObject(hdc, infoPtr->hFont);
531   SetBkColor(hdc, infoPtr->monthbk);
532   SetTextColor(hdc, infoPtr->trailingtxt);
533
534   /* copy this rect so we can change the values without changing */
535   /* the original version */
536   days->left = infoPtr->wdays.left;
537   days->right = days->left + infoPtr->width_increment;
538   days->top = infoPtr->wdays.top;
539   days->bottom = infoPtr->wdays.bottom;
540
541   i = infoPtr->firstDay;
542
543   for(j=0; j<7; j++) {
544     GetLocaleInfoW( LOCALE_USER_DEFAULT,LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
545     DrawTextW(hdc, buf, strlenW(buf), days, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
546     days->left+=infoPtr->width_increment;
547     days->right+=infoPtr->width_increment;
548   }
549
550 /* draw day numbers; first, the previous month */
551
552   firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear);
553
554   day = MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear)  +
555     (infoPtr->firstDay + 7  - firstDay)%7 + 1;
556   if (day > MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear))
557     day -=7;
558   startofprescal = day;
559   mask = 1<<(day-1);
560
561   i = 0;
562   m = 0;
563   while(day <= MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear)) {
564     MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
565     if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
566     {
567       MONTHCAL_DrawDay(infoPtr, hdc, day, prevMonth, i, 0,
568           infoPtr->monthdayState[m] & mask);
569     }
570
571     mask<<=1;
572     day++;
573     i++;
574   }
575
576 /* draw `current' month  */
577
578   day = 1; /* start at the beginning of the current month */
579
580   infoPtr->firstDayplace = i;
581   SetTextColor(hdc, infoPtr->txt);
582   m++;
583   mask = 1;
584
585   /* draw the first week of the current month */
586   while(i<7) {
587     MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
588     if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
589     {
590
591       MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth, i, 0,
592         infoPtr->monthdayState[m] & mask);
593
594       if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
595           (day==infoPtr->todaysDate.wDay) &&
596           (infoPtr->currentYear == infoPtr->todaysDate.wYear)) {
597         if(!(dwStyle & MCS_NOTODAYCIRCLE))
598           MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->currentMonth);
599       }
600     }
601
602     mask<<=1;
603     day++;
604     i++;
605   }
606
607   j = 1; /* move to the 2nd week of the current month */
608   i = 0; /* move back to sunday */
609   while(day <= MONTHCAL_MonthLength(infoPtr->currentMonth, infoPtr->currentYear)) {
610     MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
611     if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
612     {
613       MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth, i, j,
614           infoPtr->monthdayState[m] & mask);
615
616       if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
617           (day==infoPtr->todaysDate.wDay) &&
618           (infoPtr->currentYear == infoPtr->todaysDate.wYear))
619         if(!(dwStyle & MCS_NOTODAYCIRCLE))
620           MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->currentMonth);
621     }
622     mask<<=1;
623     day++;
624     i++;
625     if(i>6) { /* past saturday, goto the next weeks sunday */
626       i = 0;
627       j++;
628     }
629   }
630
631 /*  draw `next' month */
632
633   day = 1; /* start at the first day of the next month */
634   m++;
635   mask = 1;
636
637   SetTextColor(hdc, infoPtr->trailingtxt);
638   while((i<7) &&(j<6)) {
639     MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
640     if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
641     {
642       MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth + 1, i, j,
643                 infoPtr->monthdayState[m] & mask);
644     }
645
646     mask<<=1;
647     day++;
648     i++;
649     if(i==7) { /* past saturday, go to next week's sunday */
650       i = 0;
651       j++;
652     }
653   }
654   SetTextColor(hdc, infoPtr->txt);
655
656
657 /* draw `today' date if style allows it, and draw a circle before today's
658  * date if necessary */
659
660   if(!(dwStyle & MCS_NOTODAY))  {
661     int offset = 0;
662     if(!(dwStyle & MCS_NOTODAYCIRCLE))  {
663       /*day is the number of days from nextmonth we put on the calendar */
664       MONTHCAL_CircleDay(infoPtr, hdc,
665                          day+MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear),
666                          infoPtr->currentMonth);
667       offset+=textWidth;
668     }
669     if (!LoadStringW(COMCTL32_hModule,IDM_TODAY,buf1,countof(buf1)))
670       {
671         WARN("Can't load resource\n");
672         strcpyW(buf1, todayW);
673       }
674     MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
675     MONTHCAL_CopyTime(&infoPtr->todaysDate,&localtime);
676     GetDateFormatW(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&localtime,NULL,buf2,countof(buf2));
677     wsprintfW(buf, fmt2W, buf1, buf2);
678     SelectObject(hdc, infoPtr->hBoldFont);
679
680     DrawTextW(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
681     if(IntersectRect(&rcTemp, &(ps->rcPaint), &rtoday))
682     {
683       DrawTextW(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
684     }
685     SelectObject(hdc, infoPtr->hFont);
686   }
687
688 /*eventually draw week numbers*/
689   if(dwStyle & MCS_WEEKNUMBERS)  {
690     /* display weeknumbers*/
691     int mindays;
692
693     /* Rules what week to call the first week of a new year:
694        LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
695        The week containing Jan 1 is the first week of year
696        LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
697        First week of year must contain 4 days of the new year
698        LOCALE_IFIRSTWEEKOFYEAR == 1  (what contries?)
699        The first week of the year must contain only days of the new year
700     */
701     GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
702     weeknum = atoiW(buf);
703     switch (weeknum)
704       {
705       case 1: mindays = 6;
706         break;
707       case 2: mindays = 3;
708         break;
709       case 0:
710       default:
711         mindays = 0;
712       }
713     if (infoPtr->currentMonth < 2)
714       {
715         /* calculate all those exceptions for january */
716         weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear);
717         if ((infoPtr->firstDay +7 - weeknum1)%7 > mindays)
718             weeknum =1;
719         else
720           {
721             weeknum = 0;
722             for(i=0; i<11; i++)
723               weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->currentYear-1);
724             weeknum +=startofprescal+ 7;
725             weeknum /=7;
726             weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear-1);
727             if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
728               weeknum++;
729           }
730       }
731     else
732       {
733         weeknum = 0;
734         for(i=0; i<prevMonth-1; i++)
735           weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->currentYear);
736         weeknum +=startofprescal+ 7;
737         weeknum /=7;
738         weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear);
739         if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
740           weeknum++;
741       }
742     days->left = infoPtr->weeknums.left;
743     days->right = infoPtr->weeknums.right;
744     days->top = infoPtr->weeknums.top;
745     days->bottom = days->top +infoPtr->height_increment;
746     for(i=0; i<6; i++) {
747       if((i==0)&&(weeknum>50))
748         {
749           wsprintfW(buf, fmt3W, weeknum);
750           weeknum=0;
751         }
752       else if((i==5)&&(weeknum>47))
753         {
754           wsprintfW(buf, fmt3W, 1);
755         }
756       else
757         wsprintfW(buf, fmt3W, weeknum + i);
758       DrawTextW(hdc, buf, -1, days, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
759       days->top+=infoPtr->height_increment;
760       days->bottom+=infoPtr->height_increment;
761     }
762
763     MoveToEx(hdc, infoPtr->weeknums.right, infoPtr->weeknums.top + 3 , NULL);
764     LineTo(hdc,   infoPtr->weeknums.right, infoPtr->weeknums.bottom );
765
766   }
767   /* currentFont was font at entering Refresh */
768
769   SetBkColor(hdc, oldBkColor);
770   SelectObject(hdc, currentFont);
771   SetTextColor(hdc, oldTextColor);
772 }
773
774
775 static LRESULT
776 MONTHCAL_GetMinReqRect(MONTHCAL_INFO *infoPtr, LPARAM lParam)
777 {
778   LPRECT lpRect = (LPRECT) lParam;
779
780   TRACE("rect %p\n", lpRect);
781
782   /* validate parameters */
783
784   if((infoPtr==NULL) ||(lpRect == NULL) ) return FALSE;
785
786   lpRect->left = infoPtr->title.left;
787   lpRect->top = infoPtr->title.top;
788   lpRect->right = infoPtr->title.right;
789   lpRect->bottom = infoPtr->todayrect.bottom;
790   AdjustWindowRect(lpRect, GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE), FALSE);
791
792   TRACE("%s\n", wine_dbgstr_rect(lpRect));
793
794   return TRUE;
795 }
796
797
798 static LRESULT
799 MONTHCAL_GetColor(MONTHCAL_INFO *infoPtr, WPARAM wParam)
800 {
801   TRACE("\n");
802
803   switch((int)wParam) {
804     case MCSC_BACKGROUND:
805       return infoPtr->bk;
806     case MCSC_TEXT:
807       return infoPtr->txt;
808     case MCSC_TITLEBK:
809       return infoPtr->titlebk;
810     case MCSC_TITLETEXT:
811       return infoPtr->titletxt;
812     case MCSC_MONTHBK:
813       return infoPtr->monthbk;
814     case MCSC_TRAILINGTEXT:
815       return infoPtr->trailingtxt;
816   }
817
818   return -1;
819 }
820
821
822 static LRESULT
823 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
824 {
825   int prev = -1;
826
827   TRACE("%d: color %08lx\n", wParam, lParam);
828
829   switch((int)wParam) {
830     case MCSC_BACKGROUND:
831       prev = infoPtr->bk;
832       infoPtr->bk = (COLORREF)lParam;
833       break;
834     case MCSC_TEXT:
835       prev = infoPtr->txt;
836       infoPtr->txt = (COLORREF)lParam;
837       break;
838     case MCSC_TITLEBK:
839       prev = infoPtr->titlebk;
840       infoPtr->titlebk = (COLORREF)lParam;
841       break;
842     case MCSC_TITLETEXT:
843       prev=infoPtr->titletxt;
844       infoPtr->titletxt = (COLORREF)lParam;
845       break;
846     case MCSC_MONTHBK:
847       prev = infoPtr->monthbk;
848       infoPtr->monthbk = (COLORREF)lParam;
849       break;
850     case MCSC_TRAILINGTEXT:
851       prev = infoPtr->trailingtxt;
852       infoPtr->trailingtxt = (COLORREF)lParam;
853       break;
854   }
855
856   InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
857   return prev;
858 }
859
860
861 static LRESULT
862 MONTHCAL_GetMonthDelta(MONTHCAL_INFO *infoPtr)
863 {
864   TRACE("\n");
865
866   if(infoPtr->delta)
867     return infoPtr->delta;
868   else
869     return infoPtr->visible;
870 }
871
872
873 static LRESULT
874 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, WPARAM wParam)
875 {
876   int prev = infoPtr->delta;
877
878   TRACE("delta %d\n", wParam);
879
880   infoPtr->delta = (int)wParam;
881   return prev;
882 }
883
884
885 static LRESULT
886 MONTHCAL_GetFirstDayOfWeek(MONTHCAL_INFO *infoPtr)
887 {
888   return infoPtr->firstDay;
889 }
890
891
892 /* sets the first day of the week that will appear in the control */
893 /* 0 == Sunday, 6 == Saturday */
894 /* FIXME: this needs to be implemented properly in MONTHCAL_Refresh() */
895 /* FIXME: we need more error checking here */
896 static LRESULT
897 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, LPARAM lParam)
898 {
899   int prev = infoPtr->firstDay;
900   WCHAR buf[40];
901
902   TRACE("day %ld\n", lParam);
903
904   if((lParam >= 0) && (lParam < 7)) {
905     infoPtr->firstDay = (int)lParam;
906   }
907   else
908     {
909       GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
910       TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
911       infoPtr->firstDay = (atoiW(buf)+1)%7;
912     }
913   return prev;
914 }
915
916
917 static LRESULT
918 MONTHCAL_GetMonthRange(MONTHCAL_INFO *infoPtr)
919 {
920   TRACE("\n");
921
922   return infoPtr->monthRange;
923 }
924
925
926 static LRESULT
927 MONTHCAL_GetMaxTodayWidth(MONTHCAL_INFO *infoPtr)
928 {
929   return(infoPtr->todayrect.right - infoPtr->todayrect.left);
930 }
931
932
933 static LRESULT
934 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
935 {
936     SYSTEMTIME *lprgSysTimeArray=(SYSTEMTIME *)lParam;
937     FILETIME ft_min, ft_max;
938
939     TRACE("%x %lx\n", wParam, lParam);
940
941     if ((wParam & GDTR_MIN && !MONTHCAL_ValidateTime(lprgSysTimeArray[0])) ||
942         (wParam & GDTR_MAX && !MONTHCAL_ValidateTime(lprgSysTimeArray[1])))
943         return FALSE;
944
945     if (wParam & GDTR_MIN)
946     {
947         MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minDate);
948         infoPtr->rangeValid |= GDTR_MIN;
949     }
950     if (wParam & GDTR_MAX)
951     {
952         MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxDate);
953         infoPtr->rangeValid |= GDTR_MAX;
954     }
955
956     /* Only one limit set - we are done */
957     if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
958         return TRUE;
959     
960     SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
961     SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
962
963     if (CompareFileTime(&ft_min, &ft_max) > 0)
964     {
965         if ((wParam & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
966         {
967             /* Native swaps limits only when both limits are being set. */
968             SYSTEMTIME st_tmp = infoPtr->minDate;
969             infoPtr->minDate  = infoPtr->maxDate;
970             infoPtr->maxDate  = st_tmp;
971         }
972         else
973         {
974             /* Reset the other limit. */
975             /* FIXME: native sets date&time to 0. Should we do this too? */
976             infoPtr->rangeValid &= wParam & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN ;
977         }
978     }
979
980     return TRUE;
981 }
982
983
984 static LRESULT
985 MONTHCAL_GetRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
986 {
987   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
988   SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *)lParam;
989
990   /* validate parameters */
991
992   if((infoPtr==NULL) || (lprgSysTimeArray==NULL)) return FALSE;
993
994   MONTHCAL_CopyTime(&infoPtr->maxDate, &lprgSysTimeArray[1]);
995   MONTHCAL_CopyTime(&infoPtr->minDate, &lprgSysTimeArray[0]);
996
997   return infoPtr->rangeValid;
998 }
999
1000
1001 static LRESULT
1002 MONTHCAL_SetDayState(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1003
1004 {
1005   int i, iMonths = (int)wParam;
1006   MONTHDAYSTATE *dayStates = (LPMONTHDAYSTATE)lParam;
1007
1008   TRACE("%x %lx\n", wParam, lParam);
1009   if(iMonths!=infoPtr->monthRange) return 0;
1010
1011   for(i=0; i<iMonths; i++)
1012     infoPtr->monthdayState[i] = dayStates[i];
1013   return 1;
1014 }
1015
1016 static LRESULT
1017 MONTHCAL_GetCurSel(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1018 {
1019   SYSTEMTIME *lpSel = (SYSTEMTIME *) lParam;
1020
1021   TRACE("%lx\n", lParam);
1022   if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
1023   if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
1024
1025   MONTHCAL_CopyTime(&infoPtr->minSel, lpSel);
1026   TRACE("%d/%d/%d\n", lpSel->wYear, lpSel->wMonth, lpSel->wDay);
1027   return TRUE;
1028 }
1029
1030 /* FIXME: if the specified date is not visible, make it visible */
1031 /* FIXME: redraw? */
1032 static LRESULT
1033 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1034 {
1035   SYSTEMTIME *lpSel = (SYSTEMTIME *)lParam;
1036
1037   TRACE("%lx\n", lParam);
1038   if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
1039   if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
1040
1041   infoPtr->currentMonth=lpSel->wMonth;
1042   infoPtr->currentYear=lpSel->wYear;
1043
1044   MONTHCAL_CopyTime(lpSel, &infoPtr->minSel);
1045   MONTHCAL_CopyTime(lpSel, &infoPtr->maxSel);
1046
1047   InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1048
1049   return TRUE;
1050 }
1051
1052
1053 static LRESULT
1054 MONTHCAL_GetMaxSelCount(MONTHCAL_INFO *infoPtr)
1055 {
1056   return infoPtr->maxSelCount;
1057 }
1058
1059
1060 static LRESULT
1061 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1062 {
1063   TRACE("%x\n", wParam);
1064
1065   if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)  {
1066     infoPtr->maxSelCount = wParam;
1067   }
1068
1069   return TRUE;
1070 }
1071
1072
1073 static LRESULT
1074 MONTHCAL_GetSelRange(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1075 {
1076   SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1077
1078   TRACE("%lx\n", lParam);
1079
1080   /* validate parameters */
1081
1082   if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1083
1084   if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)
1085   {
1086     MONTHCAL_CopyTime(&infoPtr->maxSel, &lprgSysTimeArray[1]);
1087     MONTHCAL_CopyTime(&infoPtr->minSel, &lprgSysTimeArray[0]);
1088     TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1089     return TRUE;
1090   }
1091
1092   return FALSE;
1093 }
1094
1095
1096 static LRESULT
1097 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1098 {
1099   SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1100
1101   TRACE("%lx\n", lParam);
1102
1103   /* validate parameters */
1104
1105   if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1106
1107   if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)
1108   {
1109     MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxSel);
1110     MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minSel);
1111     TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1112     return TRUE;
1113   }
1114
1115   return FALSE;
1116 }
1117
1118
1119 static LRESULT
1120 MONTHCAL_GetToday(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1121 {
1122   SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1123
1124   TRACE("%lx\n", lParam);
1125
1126   /* validate parameters */
1127
1128   if((infoPtr==NULL) || (lpToday==NULL)) return FALSE;
1129   MONTHCAL_CopyTime(&infoPtr->todaysDate, lpToday);
1130   return TRUE;
1131 }
1132
1133
1134 static LRESULT
1135 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1136 {
1137   SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1138
1139   TRACE("%lx\n", lParam);
1140
1141   /* validate parameters */
1142
1143   if((infoPtr==NULL) ||(lpToday==NULL)) return FALSE;
1144   MONTHCAL_CopyTime(lpToday, &infoPtr->todaysDate);
1145   InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1146   return TRUE;
1147 }
1148
1149
1150 static LRESULT
1151 MONTHCAL_HitTest(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1152 {
1153   PMCHITTESTINFO lpht = (PMCHITTESTINFO)lParam;
1154   UINT x,y;
1155   DWORD retval;
1156   int day,wday,wnum;
1157
1158
1159   x = lpht->pt.x;
1160   y = lpht->pt.y;
1161   retval = MCHT_NOWHERE;
1162
1163   ZeroMemory(&lpht->st, sizeof(lpht->st));
1164
1165   /* Comment in for debugging...
1166   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,
1167         infoPtr->wdays.left, infoPtr->wdays.right,
1168         infoPtr->wdays.top, infoPtr->wdays.bottom,
1169         infoPtr->days.left, infoPtr->days.right,
1170         infoPtr->days.top, infoPtr->days.bottom,
1171         infoPtr->todayrect.left, infoPtr->todayrect.right,
1172         infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1173         infoPtr->weeknums.left, infoPtr->weeknums.right,
1174         infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1175   */
1176
1177   /* are we in the header? */
1178
1179   if(PtInRect(&infoPtr->title, lpht->pt)) {
1180     if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1181       retval = MCHT_TITLEBTNPREV;
1182       goto done;
1183     }
1184     if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1185       retval = MCHT_TITLEBTNNEXT;
1186       goto done;
1187     }
1188     if(PtInRect(&infoPtr->titlemonth, lpht->pt)) {
1189       retval = MCHT_TITLEMONTH;
1190       goto done;
1191     }
1192     if(PtInRect(&infoPtr->titleyear, lpht->pt)) {
1193       retval = MCHT_TITLEYEAR;
1194       goto done;
1195     }
1196
1197     retval = MCHT_TITLE;
1198     goto done;
1199   }
1200
1201   day = MONTHCAL_CalcDayFromPos(infoPtr,x,y,&wday,&wnum);
1202   if(PtInRect(&infoPtr->wdays, lpht->pt)) {
1203     retval = MCHT_CALENDARDAY;
1204     lpht->st.wYear  = infoPtr->currentYear;
1205     lpht->st.wMonth = (day < 1)? infoPtr->currentMonth -1 : infoPtr->currentMonth;
1206     lpht->st.wDay   = (day < 1)?
1207       MONTHCAL_MonthLength(infoPtr->currentMonth-1,infoPtr->currentYear) -day : day;
1208     goto done;
1209   }
1210   if(PtInRect(&infoPtr->weeknums, lpht->pt)) {
1211     retval = MCHT_CALENDARWEEKNUM;
1212     lpht->st.wYear  = infoPtr->currentYear;
1213     lpht->st.wMonth = (day < 1) ? infoPtr->currentMonth -1 :
1214       (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear)) ?
1215       infoPtr->currentMonth +1 :infoPtr->currentMonth;
1216     lpht->st.wDay   = (day < 1 ) ?
1217       MONTHCAL_MonthLength(infoPtr->currentMonth-1,infoPtr->currentYear) -day :
1218       (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear)) ?
1219       day - MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear) : day;
1220     goto done;
1221   }
1222   if(PtInRect(&infoPtr->days, lpht->pt))
1223     {
1224       lpht->st.wYear  = infoPtr->currentYear;
1225       if ( day < 1)
1226         {
1227           retval = MCHT_CALENDARDATEPREV;
1228           lpht->st.wMonth = infoPtr->currentMonth - 1;
1229           if (lpht->st.wMonth <1)
1230             {
1231               lpht->st.wMonth = 12;
1232               lpht->st.wYear--;
1233             }
1234           lpht->st.wDay   = MONTHCAL_MonthLength(lpht->st.wMonth,lpht->st.wYear) -day;
1235         }
1236       else if (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear))
1237         {
1238           retval = MCHT_CALENDARDATENEXT;
1239           lpht->st.wMonth = infoPtr->currentMonth + 1;
1240           if (lpht->st.wMonth <12)
1241             {
1242               lpht->st.wMonth = 1;
1243               lpht->st.wYear++;
1244             }
1245           lpht->st.wDay   = day - MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear) ;
1246         }
1247       else {
1248         retval = MCHT_CALENDARDATE;
1249         lpht->st.wMonth = infoPtr->currentMonth;
1250         lpht->st.wDay   = day;
1251         lpht->st.wDayOfWeek   = MONTHCAL_CalculateDayOfWeek(day,lpht->st.wMonth,lpht->st.wYear);
1252       }
1253       goto done;
1254     }
1255   if(PtInRect(&infoPtr->todayrect, lpht->pt)) {
1256     retval = MCHT_TODAYLINK;
1257     goto done;
1258   }
1259
1260
1261   /* Hit nothing special? What's left must be background :-) */
1262
1263   retval = MCHT_CALENDARBK;
1264  done:
1265   lpht->uHit = retval;
1266   return retval;
1267 }
1268
1269
1270 static void MONTHCAL_GoToNextMonth(MONTHCAL_INFO *infoPtr)
1271 {
1272   DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1273
1274   TRACE("MONTHCAL_GoToNextMonth\n");
1275
1276   infoPtr->currentMonth++;
1277   if(infoPtr->currentMonth > 12) {
1278     infoPtr->currentYear++;
1279     infoPtr->currentMonth = 1;
1280   }
1281
1282   if(dwStyle & MCS_DAYSTATE) {
1283     NMDAYSTATE nmds;
1284     int i;
1285
1286     nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1287     nmds.nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1288     nmds.nmhdr.code     = MCN_GETDAYSTATE;
1289     nmds.cDayState      = infoPtr->monthRange;
1290     nmds.prgDayState    = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1291
1292     SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
1293     (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
1294     for(i=0; i<infoPtr->monthRange; i++)
1295       infoPtr->monthdayState[i] = nmds.prgDayState[i];
1296   }
1297 }
1298
1299
1300 static void MONTHCAL_GoToPrevMonth(MONTHCAL_INFO *infoPtr)
1301 {
1302   DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1303
1304   TRACE("\n");
1305
1306   infoPtr->currentMonth--;
1307   if(infoPtr->currentMonth < 1) {
1308     infoPtr->currentYear--;
1309     infoPtr->currentMonth = 12;
1310   }
1311
1312   if(dwStyle & MCS_DAYSTATE) {
1313     NMDAYSTATE nmds;
1314     int i;
1315
1316     nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1317     nmds.nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1318     nmds.nmhdr.code     = MCN_GETDAYSTATE;
1319     nmds.cDayState      = infoPtr->monthRange;
1320     nmds.prgDayState    = Alloc
1321                         (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1322
1323     SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
1324         (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
1325     for(i=0; i<infoPtr->monthRange; i++)
1326        infoPtr->monthdayState[i] = nmds.prgDayState[i];
1327   }
1328 }
1329
1330 static LRESULT
1331 MONTHCAL_RButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1332 {
1333   static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1334   HMENU hMenu;
1335   POINT menupoint;
1336   WCHAR buf[32];
1337
1338   hMenu = CreatePopupMenu();
1339   if (!LoadStringW(COMCTL32_hModule,IDM_GOTODAY,buf,countof(buf)))
1340     {
1341       WARN("Can't load resource\n");
1342       strcpyW(buf, todayW);
1343     }
1344   AppendMenuW(hMenu, MF_STRING|MF_ENABLED,1, buf);
1345   menupoint.x=(short)LOWORD(lParam);
1346   menupoint.y=(short)HIWORD(lParam);
1347   ClientToScreen(infoPtr->hwndSelf, &menupoint);
1348   if( TrackPopupMenu(hMenu,TPM_RIGHTBUTTON| TPM_NONOTIFY|TPM_RETURNCMD,
1349                      menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1350     {
1351       infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
1352       infoPtr->currentYear=infoPtr->todaysDate.wYear;
1353       InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1354     }
1355   return 0;
1356 }
1357
1358 static LRESULT
1359 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1360 {
1361   static const WCHAR EditW[] = { 'E','D','I','T',0 };
1362   MCHITTESTINFO ht;
1363   DWORD hit;
1364   HMENU hMenu;
1365   RECT rcDay; /* used in determining area to invalidate */
1366   WCHAR buf[32];
1367   int i;
1368   POINT menupoint;
1369
1370   TRACE("%lx\n", lParam);
1371
1372   if (infoPtr->hWndYearUpDown)
1373     {
1374       infoPtr->currentYear=SendMessageW( infoPtr->hWndYearUpDown, UDM_SETPOS,   (WPARAM) 0,(LPARAM)0);
1375       if(!DestroyWindow(infoPtr->hWndYearUpDown))
1376         {
1377           FIXME("Can't destroy Updown Control\n");
1378         }
1379       else
1380         infoPtr->hWndYearUpDown=0;
1381       if(!DestroyWindow(infoPtr->hWndYearEdit))
1382         {
1383           FIXME("Can't destroy Updown Control\n");
1384         }
1385       else
1386         infoPtr->hWndYearEdit=0;
1387       InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1388     }
1389
1390   ht.pt.x = (short)LOWORD(lParam);
1391   ht.pt.y = (short)HIWORD(lParam);
1392   hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1393
1394   /* FIXME: these flags should be checked by */
1395   /*((hit & MCHT_XXX) == MCHT_XXX) b/c some of the flags are */
1396   /* multi-bit */
1397   if(hit ==MCHT_TITLEBTNNEXT) {
1398     MONTHCAL_GoToNextMonth(infoPtr);
1399     infoPtr->status = MC_NEXTPRESSED;
1400     SetTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1401     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1402     return TRUE;
1403   }
1404   if(hit == MCHT_TITLEBTNPREV){
1405     MONTHCAL_GoToPrevMonth(infoPtr);
1406     infoPtr->status = MC_PREVPRESSED;
1407     SetTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1408     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1409     return TRUE;
1410   }
1411
1412   if(hit == MCHT_TITLEMONTH) {
1413     hMenu = CreatePopupMenu();
1414
1415     for (i=0; i<12;i++)
1416       {
1417         GetLocaleInfoW(LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+i, buf,countof(buf));
1418         AppendMenuW(hMenu, MF_STRING|MF_ENABLED,i+1, buf);
1419       }
1420     menupoint.x=infoPtr->titlemonth.right;
1421     menupoint.y=infoPtr->titlemonth.bottom;
1422     ClientToScreen(infoPtr->hwndSelf, &menupoint);
1423     i= TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
1424                       menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
1425     if ((i>0) && (i<13))
1426       {
1427         infoPtr->currentMonth=i;
1428         InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1429       }
1430   }
1431   if(hit == MCHT_TITLEYEAR) {
1432     infoPtr->hWndYearEdit=CreateWindowExW(0,
1433                          EditW,
1434                            0,
1435                          WS_VISIBLE | WS_CHILD |UDS_SETBUDDYINT,
1436                          infoPtr->titleyear.left+3,infoPtr->titlebtnnext.top,
1437                          infoPtr->titleyear.right-infoPtr->titleyear.left+4,
1438                          infoPtr->textHeight,
1439                          infoPtr->hwndSelf,
1440                          NULL,
1441                          NULL,
1442                          NULL);
1443     SendMessageW( infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM) infoPtr->hBoldFont, (LPARAM)TRUE);
1444     infoPtr->hWndYearUpDown=CreateWindowExW(0,
1445                          UPDOWN_CLASSW,
1446                            0,
1447                          WS_VISIBLE | WS_CHILD |UDS_SETBUDDYINT|UDS_NOTHOUSANDS|UDS_ARROWKEYS,
1448                          infoPtr->titleyear.right+7,infoPtr->titlebtnnext.top,
1449                          18,
1450                          infoPtr->textHeight,
1451                          infoPtr->hwndSelf,
1452                          NULL,
1453                          NULL,
1454                          NULL);
1455     SendMessageW( infoPtr->hWndYearUpDown, UDM_SETRANGE, (WPARAM) 0, MAKELONG (9999, 1753));
1456     SendMessageW( infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM) infoPtr->hWndYearEdit, (LPARAM)0 );
1457     SendMessageW( infoPtr->hWndYearUpDown, UDM_SETPOS,   (WPARAM) 0,(LPARAM)infoPtr->currentYear );
1458     return TRUE;
1459
1460   }
1461   if(hit == MCHT_TODAYLINK) {
1462     infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
1463     infoPtr->currentYear=infoPtr->todaysDate.wYear;
1464     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1465     return TRUE;
1466   }
1467   if(hit == MCHT_CALENDARDATE) {
1468     SYSTEMTIME selArray[2];
1469     NMSELCHANGE nmsc;
1470
1471     MONTHCAL_CopyTime(&ht.st, &selArray[0]);
1472     MONTHCAL_CopyTime(&ht.st, &selArray[1]);
1473     MONTHCAL_SetSelRange(infoPtr, (LPARAM)&selArray);
1474     MONTHCAL_SetCurSel(infoPtr, (LPARAM)&selArray);
1475     TRACE("MCHT_CALENDARDATE\n");
1476     nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1477     nmsc.nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1478     nmsc.nmhdr.code     = MCN_SELCHANGE;
1479     MONTHCAL_CopyTime(&infoPtr->minSel,&nmsc.stSelStart);
1480     MONTHCAL_CopyTime(&infoPtr->maxSel,&nmsc.stSelEnd);
1481
1482     SendMessageW(infoPtr->hwndNotify, WM_NOTIFY,
1483            (WPARAM)nmsc.nmhdr.idFrom,(LPARAM)&nmsc);
1484
1485
1486     /* redraw both old and new days if the selected day changed */
1487     if(infoPtr->curSelDay != ht.st.wDay) {
1488       MONTHCAL_CalcPosFromDay(infoPtr, ht.st.wDay, ht.st.wMonth, &rcDay);
1489       InvalidateRect(infoPtr->hwndSelf, &rcDay, TRUE);
1490
1491       MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->curSelDay, infoPtr->currentMonth, &rcDay);
1492       InvalidateRect(infoPtr->hwndSelf, &rcDay, TRUE);
1493     }
1494
1495     infoPtr->firstSelDay = ht.st.wDay;
1496     infoPtr->curSelDay = ht.st.wDay;
1497     infoPtr->status = MC_SEL_LBUTDOWN;
1498     return TRUE;
1499   }
1500
1501   return 0;
1502 }
1503
1504
1505 static LRESULT
1506 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1507 {
1508   NMSELCHANGE nmsc;
1509   NMHDR nmhdr;
1510   BOOL redraw = FALSE;
1511   MCHITTESTINFO ht;
1512   DWORD hit;
1513
1514   TRACE("\n");
1515
1516   if(infoPtr->status & MC_NEXTPRESSED) {
1517     KillTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER);
1518     infoPtr->status &= ~MC_NEXTPRESSED;
1519     redraw = TRUE;
1520   }
1521   if(infoPtr->status & MC_PREVPRESSED) {
1522     KillTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER);
1523     infoPtr->status &= ~MC_PREVPRESSED;
1524     redraw = TRUE;
1525   }
1526
1527   ht.pt.x = (short)LOWORD(lParam);
1528   ht.pt.y = (short)HIWORD(lParam);
1529   hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1530
1531   infoPtr->status = MC_SEL_LBUTUP;
1532
1533   if(hit ==MCHT_CALENDARDATENEXT) {
1534     MONTHCAL_GoToNextMonth(infoPtr);
1535     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1536     return TRUE;
1537   }
1538   if(hit == MCHT_CALENDARDATEPREV){
1539     MONTHCAL_GoToPrevMonth(infoPtr);
1540     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1541     return TRUE;
1542   }
1543   nmhdr.hwndFrom = infoPtr->hwndSelf;
1544   nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1545   nmhdr.code     = NM_RELEASEDCAPTURE;
1546   TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
1547
1548   SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
1549   /* redraw if necessary */
1550   if(redraw)
1551     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1552   /* only send MCN_SELECT if currently displayed month's day was selected */
1553   if(hit == MCHT_CALENDARDATE) {
1554     nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1555     nmsc.nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1556     nmsc.nmhdr.code     = MCN_SELECT;
1557     MONTHCAL_CopyTime(&infoPtr->minSel, &nmsc.stSelStart);
1558     MONTHCAL_CopyTime(&infoPtr->maxSel, &nmsc.stSelEnd);
1559
1560     SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, (WPARAM)nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1561
1562   }
1563   return 0;
1564 }
1565
1566
1567 static LRESULT
1568 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1569 {
1570   BOOL redraw = FALSE;
1571
1572   TRACE("%d\n", wParam);
1573
1574   switch(wParam) {
1575   case MC_NEXTMONTHTIMER:
1576     redraw = TRUE;
1577     MONTHCAL_GoToNextMonth(infoPtr);
1578     break;
1579   case MC_PREVMONTHTIMER:
1580     redraw = TRUE;
1581     MONTHCAL_GoToPrevMonth(infoPtr);
1582     break;
1583   default:
1584     ERR("got unknown timer\n");
1585     break;
1586   }
1587
1588   /* redraw only if necessary */
1589   if(redraw)
1590     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1591
1592   return 0;
1593 }
1594
1595
1596 static LRESULT
1597 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1598 {
1599   MCHITTESTINFO ht;
1600   int oldselday, selday, hit;
1601   RECT r;
1602
1603   if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
1604
1605   ht.pt.x = (short)LOWORD(lParam);
1606   ht.pt.y = (short)HIWORD(lParam);
1607
1608   hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1609
1610   /* not on the calendar date numbers? bail out */
1611   TRACE("hit:%x\n",hit);
1612   if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE) return 0;
1613
1614   selday = ht.st.wDay;
1615   oldselday = infoPtr->curSelDay;
1616   infoPtr->curSelDay = selday;
1617   MONTHCAL_CalcPosFromDay(infoPtr, selday, ht.st. wMonth, &r);
1618
1619   if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)  {
1620     SYSTEMTIME selArray[2];
1621     int i;
1622
1623     MONTHCAL_GetSelRange(infoPtr, (LPARAM)&selArray);
1624     i = 0;
1625     if(infoPtr->firstSelDay==selArray[0].wDay) i=1;
1626     TRACE("oldRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1627     if(infoPtr->firstSelDay==selArray[1].wDay) {
1628       /* 1st time we get here: selArray[0]=selArray[1])  */
1629       /* if we're still at the first selected date, return */
1630       if(infoPtr->firstSelDay==selday) goto done;
1631       if(selday<infoPtr->firstSelDay) i = 0;
1632     }
1633
1634     if(abs(infoPtr->firstSelDay - selday) >= infoPtr->maxSelCount) {
1635       if(selday>infoPtr->firstSelDay)
1636         selday = infoPtr->firstSelDay + infoPtr->maxSelCount;
1637       else
1638         selday = infoPtr->firstSelDay - infoPtr->maxSelCount;
1639     }
1640
1641     if(selArray[i].wDay!=selday) {
1642       TRACE("newRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1643
1644       selArray[i].wDay = selday;
1645
1646       if(selArray[0].wDay>selArray[1].wDay) {
1647         DWORD tempday;
1648         tempday = selArray[1].wDay;
1649         selArray[1].wDay = selArray[0].wDay;
1650         selArray[0].wDay = tempday;
1651       }
1652
1653       MONTHCAL_SetSelRange(infoPtr, (LPARAM)&selArray);
1654     }
1655   }
1656
1657 done:
1658
1659   /* only redraw if the currently selected day changed */
1660   /* FIXME: this should specify a rectangle containing only the days that changed */
1661   /* using InvalidateRect */
1662   if(oldselday != infoPtr->curSelDay)
1663     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1664
1665   return 0;
1666 }
1667
1668
1669 static LRESULT
1670 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1671 {
1672   HDC hdc;
1673   PAINTSTRUCT ps;
1674
1675   if (wParam)
1676   {
1677     GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
1678     hdc = (HDC)wParam;
1679   }
1680   else
1681     hdc = BeginPaint(infoPtr->hwndSelf, &ps);
1682
1683   MONTHCAL_Refresh(infoPtr, hdc, &ps);
1684   if (!wParam) EndPaint(infoPtr->hwndSelf, &ps);
1685   return 0;
1686 }
1687
1688
1689 static LRESULT
1690 MONTHCAL_KillFocus(MONTHCAL_INFO *infoPtr)
1691 {
1692   TRACE("\n");
1693
1694   InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1695
1696   return 0;
1697 }
1698
1699
1700 static LRESULT
1701 MONTHCAL_SetFocus(MONTHCAL_INFO *infoPtr)
1702 {
1703   TRACE("\n");
1704
1705   InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1706
1707   return 0;
1708 }
1709
1710 /* sets the size information */
1711 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
1712 {
1713   static const WCHAR SunW[] = { 'S','u','n',0 };
1714   static const WCHAR O0W[] = { '0','0',0 };
1715   HDC hdc = GetDC(infoPtr->hwndSelf);
1716   RECT *title=&infoPtr->title;
1717   RECT *prev=&infoPtr->titlebtnprev;
1718   RECT *next=&infoPtr->titlebtnnext;
1719   RECT *titlemonth=&infoPtr->titlemonth;
1720   RECT *titleyear=&infoPtr->titleyear;
1721   RECT *wdays=&infoPtr->wdays;
1722   RECT *weeknumrect=&infoPtr->weeknums;
1723   RECT *days=&infoPtr->days;
1724   RECT *todayrect=&infoPtr->todayrect;
1725   SIZE size;
1726   TEXTMETRICW tm;
1727   DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1728   HFONT currentFont;
1729   int xdiv, left_offset;
1730   RECT rcClient;
1731
1732   GetClientRect(infoPtr->hwndSelf, &rcClient);
1733
1734   currentFont = SelectObject(hdc, infoPtr->hFont);
1735
1736   /* get the height and width of each day's text */
1737   GetTextMetricsW(hdc, &tm);
1738   infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
1739   GetTextExtentPoint32W(hdc, SunW, 3, &size);
1740   infoPtr->textWidth = size.cx + 2;
1741
1742   /* recalculate the height and width increments and offsets */
1743   GetTextExtentPoint32W(hdc, O0W, 2, &size);
1744
1745   xdiv = (dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
1746
1747   infoPtr->width_increment = size.cx * 2 + 4;
1748   infoPtr->height_increment = infoPtr->textHeight;
1749   left_offset = (rcClient.right - rcClient.left) - (infoPtr->width_increment * xdiv);
1750
1751   /* calculate title area */
1752   title->top    = rcClient.top;
1753   title->bottom = title->top + 3 * infoPtr->height_increment / 2;
1754   title->left   = left_offset;
1755   title->right  = rcClient.right;
1756
1757   /* set the dimensions of the next and previous buttons and center */
1758   /* the month text vertically */
1759   prev->top    = next->top    = title->top + 4;
1760   prev->bottom = next->bottom = title->bottom - 4;
1761   prev->left   = title->left + 4;
1762   prev->right  = prev->left + (title->bottom - title->top) ;
1763   next->right  = title->right - 4;
1764   next->left   = next->right - (title->bottom - title->top);
1765
1766   /* titlemonth->left and right change based upon the current month */
1767   /* and are recalculated in refresh as the current month may change */
1768   /* without the control being resized */
1769   titlemonth->top    = titleyear->top    = title->top    + (infoPtr->height_increment)/2;
1770   titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
1771
1772   /* setup the dimensions of the rectangle we draw the names of the */
1773   /* days of the week in */
1774   weeknumrect->left = left_offset;
1775   if(dwStyle & MCS_WEEKNUMBERS)
1776     weeknumrect->right=prev->right;
1777   else
1778     weeknumrect->right=weeknumrect->left;
1779   wdays->left   = days->left   = weeknumrect->right;
1780   wdays->right  = days->right  = wdays->left + 7 * infoPtr->width_increment;
1781   wdays->top    = title->bottom ;
1782   wdays->bottom = wdays->top + infoPtr->height_increment;
1783
1784   days->top    = weeknumrect->top = wdays->bottom ;
1785   days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
1786
1787   todayrect->left   = rcClient.left;
1788   todayrect->right  = rcClient.right;
1789   todayrect->top    = days->bottom;
1790   todayrect->bottom = days->bottom + infoPtr->height_increment;
1791
1792   TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
1793         infoPtr->width_increment,infoPtr->height_increment,
1794         wine_dbgstr_rect(&rcClient),
1795         wine_dbgstr_rect(title),
1796         wine_dbgstr_rect(wdays),
1797         wine_dbgstr_rect(days),
1798         wine_dbgstr_rect(todayrect));
1799
1800   /* restore the originally selected font */
1801   SelectObject(hdc, currentFont);
1802
1803   ReleaseDC(infoPtr->hwndSelf, hdc);
1804 }
1805
1806 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
1807 {
1808   TRACE("(width=%d, height=%d)\n", Width, Height);
1809
1810   MONTHCAL_UpdateSize(infoPtr);
1811
1812   /* invalidate client area and erase background */
1813   InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1814
1815   return 0;
1816 }
1817
1818 static LRESULT MONTHCAL_GetFont(MONTHCAL_INFO *infoPtr)
1819 {
1820     return (LRESULT)infoPtr->hFont;
1821 }
1822
1823 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
1824 {
1825     HFONT hOldFont;
1826     LOGFONTW lf;
1827
1828     if (!hFont) return 0;
1829
1830     hOldFont = infoPtr->hFont;
1831     infoPtr->hFont = hFont;
1832
1833     GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
1834     lf.lfWeight = FW_BOLD;
1835     infoPtr->hBoldFont = CreateFontIndirectW(&lf);
1836
1837     if (redraw)
1838         InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1839
1840     return (LRESULT)hOldFont;
1841 }
1842
1843 /* update theme after a WM_THEMECHANGED message */
1844 static LRESULT theme_changed (MONTHCAL_INFO* infoPtr)
1845 {
1846     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
1847     CloseThemeData (theme);
1848     theme = OpenThemeData (infoPtr->hwndSelf, themeClass);
1849     return 0;
1850 }
1851
1852 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
1853 static LRESULT
1854 MONTHCAL_Create(HWND hwnd, WPARAM wParam, LPARAM lParam)
1855 {
1856   MONTHCAL_INFO *infoPtr;
1857
1858   /* allocate memory for info structure */
1859   infoPtr =(MONTHCAL_INFO*)Alloc(sizeof(MONTHCAL_INFO));
1860   SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
1861
1862   if(infoPtr == NULL) {
1863     ERR( "could not allocate info memory!\n");
1864     return 0;
1865   }
1866
1867   infoPtr->hwndSelf = hwnd;
1868   infoPtr->hwndNotify = ((LPCREATESTRUCTW)lParam)->hwndParent;
1869
1870   MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
1871
1872   /* initialize info structure */
1873   /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
1874
1875   GetLocalTime(&infoPtr->todaysDate);
1876   MONTHCAL_SetFirstDayOfWeek(infoPtr, (LPARAM)-1);
1877   infoPtr->currentMonth = infoPtr->todaysDate.wMonth;
1878   infoPtr->currentYear = infoPtr->todaysDate.wYear;
1879   MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->minDate);
1880   MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
1881   infoPtr->maxDate.wYear=2050;
1882   infoPtr->minDate.wYear=1950;
1883   infoPtr->maxSelCount  = 7;
1884   infoPtr->monthRange = 3;
1885   infoPtr->monthdayState = Alloc
1886                          (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1887   infoPtr->titlebk     = GetSysColor(COLOR_ACTIVECAPTION);
1888   infoPtr->titletxt    = GetSysColor(COLOR_WINDOW);
1889   infoPtr->monthbk     = GetSysColor(COLOR_WINDOW);
1890   infoPtr->trailingtxt = GetSysColor(COLOR_GRAYTEXT);
1891   infoPtr->bk          = GetSysColor(COLOR_WINDOW);
1892   infoPtr->txt         = GetSysColor(COLOR_WINDOWTEXT);
1893
1894   /* set the current day for highlighing */
1895   infoPtr->minSel.wDay = infoPtr->todaysDate.wDay;
1896   infoPtr->maxSel.wDay = infoPtr->todaysDate.wDay;
1897
1898   /* call MONTHCAL_UpdateSize to set all of the dimensions */
1899   /* of the control */
1900   MONTHCAL_UpdateSize(infoPtr);
1901   
1902   OpenThemeData (infoPtr->hwndSelf, themeClass);
1903
1904   return 0;
1905 }
1906
1907
1908 static LRESULT
1909 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
1910 {
1911   /* free month calendar info data */
1912   Free(infoPtr->monthdayState);
1913   SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
1914
1915   CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
1916   
1917   Free(infoPtr);
1918   return 0;
1919 }
1920
1921
1922 static LRESULT WINAPI
1923 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1924 {
1925   MONTHCAL_INFO *infoPtr;
1926
1927   TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hwnd, uMsg, wParam, lParam);
1928
1929   infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1930   if (!infoPtr && (uMsg != WM_CREATE))
1931     return DefWindowProcW(hwnd, uMsg, wParam, lParam);
1932   switch(uMsg)
1933   {
1934   case MCM_GETCURSEL:
1935     return MONTHCAL_GetCurSel(infoPtr, lParam);
1936
1937   case MCM_SETCURSEL:
1938     return MONTHCAL_SetCurSel(infoPtr, lParam);
1939
1940   case MCM_GETMAXSELCOUNT:
1941     return MONTHCAL_GetMaxSelCount(infoPtr);
1942
1943   case MCM_SETMAXSELCOUNT:
1944     return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
1945
1946   case MCM_GETSELRANGE:
1947     return MONTHCAL_GetSelRange(infoPtr, lParam);
1948
1949   case MCM_SETSELRANGE:
1950     return MONTHCAL_SetSelRange(infoPtr, lParam);
1951
1952   case MCM_GETMONTHRANGE:
1953     return MONTHCAL_GetMonthRange(infoPtr);
1954
1955   case MCM_SETDAYSTATE:
1956     return MONTHCAL_SetDayState(infoPtr, wParam, lParam);
1957
1958   case MCM_GETMINREQRECT:
1959     return MONTHCAL_GetMinReqRect(infoPtr, lParam);
1960
1961   case MCM_GETCOLOR:
1962     return MONTHCAL_GetColor(infoPtr, wParam);
1963
1964   case MCM_SETCOLOR:
1965     return MONTHCAL_SetColor(infoPtr, wParam, lParam);
1966
1967   case MCM_GETTODAY:
1968     return MONTHCAL_GetToday(infoPtr, lParam);
1969
1970   case MCM_SETTODAY:
1971     return MONTHCAL_SetToday(infoPtr, lParam);
1972
1973   case MCM_HITTEST:
1974     return MONTHCAL_HitTest(infoPtr, lParam);
1975
1976   case MCM_GETFIRSTDAYOFWEEK:
1977     return MONTHCAL_GetFirstDayOfWeek(infoPtr);
1978
1979   case MCM_SETFIRSTDAYOFWEEK:
1980     return MONTHCAL_SetFirstDayOfWeek(infoPtr, lParam);
1981
1982   case MCM_GETRANGE:
1983     return MONTHCAL_GetRange(hwnd, wParam, lParam);
1984
1985   case MCM_SETRANGE:
1986     return MONTHCAL_SetRange(infoPtr, wParam, lParam);
1987
1988   case MCM_GETMONTHDELTA:
1989     return MONTHCAL_GetMonthDelta(infoPtr);
1990
1991   case MCM_SETMONTHDELTA:
1992     return MONTHCAL_SetMonthDelta(infoPtr, wParam);
1993
1994   case MCM_GETMAXTODAYWIDTH:
1995     return MONTHCAL_GetMaxTodayWidth(infoPtr);
1996
1997   case WM_GETDLGCODE:
1998     return DLGC_WANTARROWS | DLGC_WANTCHARS;
1999
2000   case WM_KILLFOCUS:
2001     return MONTHCAL_KillFocus(infoPtr);
2002
2003   case WM_RBUTTONDOWN:
2004     return MONTHCAL_RButtonDown(infoPtr, lParam);
2005
2006   case WM_LBUTTONDOWN:
2007     return MONTHCAL_LButtonDown(infoPtr, lParam);
2008
2009   case WM_MOUSEMOVE:
2010     return MONTHCAL_MouseMove(infoPtr, wParam, lParam);
2011
2012   case WM_LBUTTONUP:
2013     return MONTHCAL_LButtonUp(infoPtr, lParam);
2014
2015   case WM_PRINTCLIENT:
2016   case WM_PAINT:
2017     return MONTHCAL_Paint(infoPtr, wParam);
2018
2019   case WM_SETFOCUS:
2020     return MONTHCAL_SetFocus(infoPtr);
2021
2022   case WM_SIZE:
2023     return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2024
2025   case WM_CREATE:
2026     return MONTHCAL_Create(hwnd, wParam, lParam);
2027
2028   case WM_SETFONT:
2029     return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2030
2031   case WM_GETFONT:
2032     return MONTHCAL_GetFont(infoPtr);
2033
2034   case WM_TIMER:
2035     return MONTHCAL_Timer(infoPtr, wParam);
2036     
2037   case WM_THEMECHANGED:
2038     return theme_changed (infoPtr);
2039
2040   case WM_DESTROY:
2041     return MONTHCAL_Destroy(infoPtr);
2042
2043   default:
2044     if ((uMsg >= WM_USER) && (uMsg < WM_APP))
2045       ERR( "unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
2046     return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2047   }
2048 }
2049
2050
2051 void
2052 MONTHCAL_Register(void)
2053 {
2054   WNDCLASSW wndClass;
2055
2056   ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2057   wndClass.style         = CS_GLOBALCLASS;
2058   wndClass.lpfnWndProc   = MONTHCAL_WindowProc;
2059   wndClass.cbClsExtra    = 0;
2060   wndClass.cbWndExtra    = sizeof(MONTHCAL_INFO *);
2061   wndClass.hCursor       = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2062   wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2063   wndClass.lpszClassName = MONTHCAL_CLASSW;
2064
2065   RegisterClassW(&wndClass);
2066 }
2067
2068
2069 void
2070 MONTHCAL_Unregister(void)
2071 {
2072     UnregisterClassW(MONTHCAL_CLASSW, NULL);
2073 }