Removed a few dependencies on kernel32 functions.
[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  *
9  * TODO:
10  *   - Notifications.
11  *
12  *
13  *  FIXME: handle resources better (doesn't work now); also take care
14            of internationalization. 
15  *  FIXME: keyboard handling.
16  */
17
18 #include <math.h>
19 #include <stdio.h>
20
21 #include "winbase.h"
22 #include "windef.h"
23 #include "wingdi.h"
24 #include "winuser.h"
25 #include "win.h"
26 #include "winnls.h"
27 #include "commctrl.h"
28 #include "comctl32.h"
29 #include "debugtools.h"
30
31 DEFAULT_DEBUG_CHANNEL(monthcal);
32
33 #define MC_SEL_LBUTUP       1   /* Left button released */
34 #define MC_SEL_LBUTDOWN     2   /* Left button pressed in calendar */
35 #define MC_PREVPRESSED      4   /* Prev month button pressed */
36 #define MC_NEXTPRESSED      8   /* Next month button pressed */
37 #define MC_NEXTMONTHDELAY   350 /* when continuously pressing `next */
38                                                                                 /* month', wait 500 ms before going */
39                                                                                 /* to the next month */
40 #define MC_NEXTMONTHTIMER   1                   /* Timer ID's */
41 #define MC_PREVMONTHTIMER   2                   
42
43 typedef struct
44 {
45     COLORREF    bk;
46     COLORREF    txt;
47     COLORREF    titlebk;
48     COLORREF    titletxt;
49     COLORREF    monthbk;
50     COLORREF    trailingtxt;
51     HFONT       hFont;
52     HFONT       hBoldFont;
53     int         textHeight;
54     int         textWidth;
55     int         height_increment;
56     int         width_increment;
57     int         left_offset;
58     int         top_offset;
59     int         firstDayplace; /* place of the first day of the current month */
60     int         delta;  /* scroll rate; # of months that the */
61                         /* control moves when user clicks a scroll button */
62     int         visible;        /* # of months visible */
63     int         firstDay;       /* Start month calendar with firstDay's day */
64     int         monthRange;
65     MONTHDAYSTATE *monthdayState;
66     SYSTEMTIME  todaysDate;
67     DWORD       currentMonth;
68     DWORD       currentYear;
69     int         status;         /* See MC_SEL flags */
70     int         curSelDay;      /* current selected day */
71     int         firstSelDay;    /* first selected day */
72     int         maxSelCount;
73     SYSTEMTIME  minSel;
74     SYSTEMTIME  maxSel;
75     DWORD       rangeValid;
76     SYSTEMTIME  minDate;
77     SYSTEMTIME  maxDate;
78                 
79     RECT rcClient;      /* rect for whole client area */
80     RECT rcDraw;        /* rect for drawable portion of client area */
81     RECT title;         /* rect for the header above the calendar */
82     RECT titlebtnnext;  /* the `next month' button in the header */
83     RECT titlebtnprev;  /* the `prev month' button in the header */     
84     RECT titlemonth;    /* the `month name' txt in the header */
85     RECT titleyear;     /* the `year number' txt in the header */
86     RECT prevmonth;     /* day numbers of the previous month */
87     RECT nextmonth;     /* day numbers of the next month */
88     RECT days;          /* week numbers at left side */
89     RECT weeknums;      /* week numbers at left side */
90     RECT today;         /* `today: xx/xx/xx' text rect */
91 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
92
93
94 /* take #days/month from ole/parsedt.c;
95  * we want full month-names, and abbreviated weekdays, so these are
96  * defined here */
97
98 const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0};
99
100 const char * const monthtxt[] = {"January", "February", "March", "April", "May", 
101                       "June", "July", "August", "September", "October", 
102                       "November", "December"};
103 static const char * const daytxt[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
104 static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
105
106
107 #define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongA(hwnd, 0))
108
109 /* helper functions  */
110
111 /* returns the number of days in any given month */
112 /* january is 1, december is 12 */
113 static int MONTHCAL_MonthLength(int month, int year)
114 {
115   /* if we have a leap year add 1 day to February */
116   /* a leap year is a year either divisible by 400 */
117   /* or divisible by 4 and not by 100 */
118   if(month == 2) { /* February */
119     return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
120      (year%4 == 0)) ? 1 : 0);
121   }
122   else {
123     return mdays[month - 1];
124   }
125 }
126
127
128 /* make sure that time is valid */
129 static int MONTHCAL_ValidateTime(SYSTEMTIME time) 
130 {
131   if(time.wMonth > 12) return FALSE;
132   if(time.wDayOfWeek > 6) return FALSE;
133   if(time.wDay > MONTHCAL_MonthLength(time.wMonth, time.wYear))
134           return FALSE;
135   if(time.wHour > 23) return FALSE;
136   if(time.wMinute > 59) return FALSE;
137   if(time.wSecond > 59) return FALSE;
138   if(time.wMilliseconds > 999) return FALSE;
139
140   return TRUE;
141 }
142
143
144 void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to) 
145 {
146   to->wYear = from->wYear;
147   to->wMonth = from->wMonth;
148   to->wDayOfWeek = from->wDayOfWeek;
149   to->wDay = from->wDay;
150   to->wHour = from->wHour;
151   to->wMinute = from->wMinute;
152   to->wSecond = from->wSecond;
153   to->wMilliseconds = from->wMilliseconds;
154 }
155
156
157 /* Note:Depending on DST, this may be offset by a day. 
158    Need to find out if we're on a DST place & adjust the clock accordingly.
159    Above function assumes we have a valid data.
160    Valid for year>1752;  1 <= d <= 31, 1 <= m <= 12.
161    0 = Monday.
162 */
163
164 /* returns the day in the week(0 == sunday, 6 == saturday) */
165 /* day(1 == 1st, 2 == 2nd... etc), year is the  year value */
166 static int MONTHCAL_CalculateDayOfWeek(DWORD day, DWORD month, DWORD year)
167 {
168   year-=(month < 3);
169
170   return((year + year/4 - year/100 + year/400 + 
171          DayOfWeekTable[month-1] + day - 1 ) % 7);
172 }
173
174
175 static int MONTHCAL_CalcDayFromPos(MONTHCAL_INFO *infoPtr, int x, int y) 
176 {
177   int daypos, weekpos, retval, firstDay;
178
179   /* if the point is outside the x bounds of the window put
180   it at the boundry */
181   if(x > (infoPtr->width_increment * 7.0)) {
182     x = infoPtr->rcClient.right - infoPtr->rcClient.left - infoPtr->left_offset;
183   }
184
185   daypos = (x -(infoPtr->prevmonth.left + infoPtr->left_offset)) / infoPtr->width_increment;
186   weekpos = (y - infoPtr->days.bottom - infoPtr->rcClient.top) / infoPtr->height_increment;
187     
188   firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear);
189   retval = daypos + (7 * weekpos) - firstDay;
190   TRACE("%d %d %d\n", daypos, weekpos, retval);
191   return retval;
192 }
193
194 /* day is the day of the month, 1 == 1st day of the month */
195 /* sets x and y to be the position of the day */
196 /* x == day, y == week where(0,0) == sunday, 1st week */
197 static void MONTHCAL_CalcDayXY(MONTHCAL_INFO *infoPtr, int day, int month, 
198                                  int *x, int *y)
199 {
200   int firstDay, prevMonth;
201
202   firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear);
203
204   if(month==infoPtr->currentMonth) {
205     *x = (day + firstDay) % 7;
206     *y = (day + firstDay - *x) / 7;
207     return;
208   }
209   if(month < infoPtr->currentMonth) {
210     prevMonth = month - 1;
211     if(prevMonth==0)
212        prevMonth = 12;
213    
214     *x = (MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear) - firstDay) % 7;
215     *y = 0;
216     return;
217   }
218
219   *y = MONTHCAL_MonthLength(month, infoPtr->currentYear - 1) / 7;
220   *x = (day + firstDay + MONTHCAL_MonthLength(month,
221        infoPtr->currentYear)) % 7;
222 }
223
224
225 /* x: column(day), y: row(week) */
226 static void MONTHCAL_CalcDayRect(MONTHCAL_INFO *infoPtr, RECT *r, int x, int y) 
227 {
228   r->left = infoPtr->prevmonth.left + x * infoPtr->width_increment + infoPtr->left_offset;
229   r->right = r->left + infoPtr->width_increment;
230   r->top = infoPtr->height_increment * y  + infoPtr->days.bottom + infoPtr->top_offset;
231   r->bottom = r->top + infoPtr->textHeight;
232 }
233
234
235 /* sets the RECT struct r to the rectangle around the day and month */
236 /* day is the day value of the month(1 == 1st), month is the month */
237 /* value(january == 1, december == 12) */
238 static inline void MONTHCAL_CalcPosFromDay(MONTHCAL_INFO *infoPtr, 
239                                             int day, int month, RECT *r)
240 {
241   int x, y;
242
243   MONTHCAL_CalcDayXY(infoPtr, day, month, &x, &y);
244   MONTHCAL_CalcDayRect(infoPtr, r, x, y);
245 }
246
247
248 /* day is the day in the month(1 == 1st of the month) */
249 /* month is the month value(1 == january, 12 == december) */
250 static void MONTHCAL_CircleDay(HDC hdc, MONTHCAL_INFO *infoPtr, int day,
251 int month)
252 {
253   HPEN hRedPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
254   HPEN hOldPen2 = SelectObject(hdc, hRedPen);
255   POINT points[13];
256   int x, y;
257   RECT day_rect;
258
259  /* use prevmonth to calculate position because it contains the extra width 
260   * from MCS_WEEKNUMBERS
261   */
262
263   MONTHCAL_CalcPosFromDay(infoPtr, day, month, &day_rect);
264
265   x = day_rect.left;
266   y = day_rect.top;
267         
268   points[0].x = x;
269   points[0].y = y - 1;
270   points[1].x = x + 0.8 * infoPtr->width_increment;
271   points[1].y = y - 1;
272   points[2].x = x + 0.9 * infoPtr->width_increment;
273   points[2].y = y;
274   points[3].x = x + infoPtr->width_increment;
275   points[3].y = y + 0.5 * infoPtr->textHeight;
276         
277   points[4].x = x + infoPtr->width_increment;
278   points[4].y = y + 0.9 * infoPtr->textHeight;
279   points[5].x = x + 0.6 * infoPtr->width_increment;
280   points[5].y = y + 0.9 * infoPtr->textHeight;
281   points[6].x = x + 0.5 * infoPtr->width_increment;
282   points[6].y = y + 0.9 * infoPtr->textHeight; /* bring the bottom up just
283                                 a hair to fit inside the day rectangle */
284         
285   points[7].x = x + 0.2 * infoPtr->width_increment;
286   points[7].y = y + 0.8 * infoPtr->textHeight;
287   points[8].x = x + 0.1 * infoPtr->width_increment;
288   points[8].y = y + 0.8 * infoPtr->textHeight;
289   points[9].x = x;
290   points[9].y = y + 0.5 * infoPtr->textHeight;
291
292   points[10].x = x + 0.1 * infoPtr->width_increment;
293   points[10].y = y + 0.2 * infoPtr->textHeight;
294   points[11].x = x + 0.2 * infoPtr->width_increment;
295   points[11].y = y + 0.3 * infoPtr->textHeight;
296   points[12].x = x + 0.5 * infoPtr->width_increment;
297   points[12].y = y + 0.3 * infoPtr->textHeight;
298   
299   PolyBezier(hdc, points, 13);
300   DeleteObject(hRedPen);
301   SelectObject(hdc, hOldPen2);
302 }
303
304
305 static void MONTHCAL_DrawDay(HDC hdc, MONTHCAL_INFO *infoPtr, int day, int month,
306                              int x, int y, int bold)
307 {
308   char buf[10];
309   RECT r;
310   static int haveBoldFont, haveSelectedDay = FALSE;
311   HBRUSH hbr;
312   HPEN hNewPen, hOldPen = 0;
313   COLORREF oldCol = 0;
314   COLORREF oldBk = 0;
315
316   sprintf(buf, "%d", day);
317
318 /* No need to check styles: when selection is not valid, it is set to zero. 
319  * 1<day<31, so evertyhing's OK.
320  */
321
322   MONTHCAL_CalcDayRect(infoPtr, &r, x, y);
323
324   if((day>=infoPtr->minSel.wDay) && (day<=infoPtr->maxSel.wDay)
325        && (month==infoPtr->currentMonth)) {
326     HRGN hrgn;
327     RECT r2;
328
329     TRACE("%d %d %d\n",day, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
330     TRACE("%d %d %d %d\n", r.left, r.top, r.right, r.bottom);
331     oldCol = SetTextColor(hdc, infoPtr->monthbk);
332     oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
333     hbr = GetSysColorBrush(COLOR_GRAYTEXT);
334     hrgn = CreateEllipticRgn(r.left, r.top, r.right, r.bottom);
335     FillRgn(hdc, hrgn, hbr);
336
337     /* FIXME: this may need to be changed now b/c of the other
338         drawing changes 11/3/99 CMM */
339     r2.left   = r.left - 0.25 * infoPtr->textWidth;
340     r2.top    = r.top;
341     r2.right  = r.left + 0.5 * infoPtr->textWidth;
342     r2.bottom = r.bottom;
343     if(haveSelectedDay) FillRect(hdc, &r2, hbr);
344       haveSelectedDay = TRUE;
345   } else {
346     haveSelectedDay = FALSE;
347   }
348
349   /* need to add some code for multiple selections */
350
351   if((bold) &&(!haveBoldFont)) {
352     SelectObject(hdc, infoPtr->hBoldFont);
353     haveBoldFont = TRUE;
354   }
355   if((!bold) &&(haveBoldFont)) {
356     SelectObject(hdc, infoPtr->hFont);
357     haveBoldFont = FALSE;
358   }
359
360   if(haveSelectedDay) {
361     SetTextColor(hdc, oldCol);
362     SetBkColor(hdc, oldBk);
363   }
364
365   DrawTextA(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
366
367   /* draw a rectangle around the currently selected days text */
368   if((day==infoPtr->curSelDay) && (month==infoPtr->currentMonth)) {
369     hNewPen = CreatePen(PS_DOT, 0, GetSysColor(COLOR_WINDOWTEXT) );
370     hbr = GetSysColorBrush(COLOR_WINDOWTEXT);
371     FrameRect(hdc, &r, hbr);
372     SelectObject(hdc, hOldPen);
373   }
374 }
375
376
377 /* CHECKME: For `todays date', do we need to check the locale?*/
378 static void MONTHCAL_Refresh(HWND hwnd, HDC hdc, PAINTSTRUCT* ps) 
379 {
380   MONTHCAL_INFO *infoPtr=MONTHCAL_GetInfoPtr(hwnd);
381   RECT *rcClient=&infoPtr->rcClient;
382   RECT *rcDraw=&infoPtr->rcDraw;
383   RECT *title=&infoPtr->title;
384   RECT *prev=&infoPtr->titlebtnprev;
385   RECT *next=&infoPtr->titlebtnnext;
386   RECT *titlemonth=&infoPtr->titlemonth;
387   RECT *titleyear=&infoPtr->titleyear;
388   RECT *prevmonth=&infoPtr->prevmonth;
389   RECT *nextmonth=&infoPtr->nextmonth;
390   RECT dayrect;
391   RECT *days=&dayrect;
392   RECT *weeknums=&infoPtr->weeknums;
393   RECT *rtoday=&infoPtr->today;
394   int i, j, m, mask, day, firstDay, weeknum, prevMonth;
395   int textHeight = infoPtr->textHeight, textWidth = infoPtr->textWidth;
396   SIZE size;
397   HBRUSH hbr;
398   HFONT currentFont;
399   /* LOGFONTA logFont; */
400   char buf[20];
401   const char *thisMonthtxt;
402   COLORREF oldTextColor, oldBkColor;
403   DWORD dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
404   RECT rcTemp;
405   RECT rcDay; /* used in MONTHCAL_CalcDayRect() */
406
407   oldTextColor = SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
408
409
410   /* fill background */
411   hbr = CreateSolidBrush (infoPtr->bk);
412   FillRect(hdc, rcClient, hbr);
413   DeleteObject(hbr);       
414
415   /* draw header */
416   if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
417   {
418     hbr =  CreateSolidBrush(infoPtr->titlebk);
419     FillRect(hdc, title, hbr);
420     DeleteObject(hbr);
421   }
422         
423   /* if the previous button is pressed draw it depressed */
424   if(IntersectRect(&rcTemp, &(ps->rcPaint), prev))
425   {  
426     if((infoPtr->status & MC_PREVPRESSED))
427         DrawFrameControl(hdc, prev, DFC_SCROLL,
428            DFCS_SCROLLLEFT | DFCS_PUSHED |
429           (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
430     else /* if the previous button is pressed draw it depressed */
431       DrawFrameControl(hdc, prev, DFC_SCROLL,
432            DFCS_SCROLLLEFT |(dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
433   }
434
435   /* if next button is depressed draw it depressed */   
436   if(IntersectRect(&rcTemp, &(ps->rcPaint), next))
437   {
438     if((infoPtr->status & MC_NEXTPRESSED))
439       DrawFrameControl(hdc, next, DFC_SCROLL,
440            DFCS_SCROLLRIGHT | DFCS_PUSHED |
441            (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
442     else /* if the next button is pressed draw it depressed */
443       DrawFrameControl(hdc, next, DFC_SCROLL,
444            DFCS_SCROLLRIGHT |(dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
445   }
446
447   oldBkColor = SetBkColor(hdc, infoPtr->titlebk);
448   SetTextColor(hdc, infoPtr->titletxt);
449   currentFont = SelectObject(hdc, infoPtr->hBoldFont);
450
451   /* titlemonth->left and right are set in MONTHCAL_UpdateSize */
452   titlemonth->left   = title->left;
453   titlemonth->right  = title->right;
454  
455   thisMonthtxt = monthtxt[infoPtr->currentMonth - 1];
456   sprintf(buf, "%s %ld", thisMonthtxt, infoPtr->currentYear);
457  
458   if(IntersectRect(&rcTemp, &(ps->rcPaint), titlemonth))
459   {
460     DrawTextA(hdc, buf, strlen(buf), titlemonth, 
461                         DT_CENTER | DT_VCENTER | DT_SINGLELINE);
462   }
463
464   SelectObject(hdc, infoPtr->hFont);
465
466 /* titlemonth left/right contained rect for whole titletxt('June  1999')
467   * MCM_HitTestInfo wants month & year rects, so prepare these now.
468   *(no, we can't draw them separately; the whole text is centered) 
469   */
470   GetTextExtentPoint32A(hdc, buf, strlen(buf), &size);
471   titlemonth->left = title->right / 2 - size.cx / 2;
472   titleyear->right = title->right / 2 + size.cx / 2;
473   GetTextExtentPoint32A(hdc, thisMonthtxt, strlen(thisMonthtxt), &size);
474   titlemonth->right = titlemonth->left + size.cx;
475   titleyear->right = titlemonth->right;
476  
477 /* draw line under day abbreviatons */
478
479    if(dwStyle & MCS_WEEKNUMBERS) 
480      MoveToEx(hdc, rcDraw->left + textWidth + 3, title->bottom + textHeight + 2, NULL);
481    else 
482      MoveToEx(hdc, rcDraw->left + 3, title->bottom + textHeight + 2, NULL);
483      
484   LineTo(hdc, rcDraw->right - 3, title->bottom + textHeight + 2);
485    
486 /* draw day abbreviations */
487
488   SetBkColor(hdc, infoPtr->monthbk);
489   SetTextColor(hdc, infoPtr->trailingtxt);
490
491   /* copy this rect so we can change the values without changing */
492   /* the original version */
493   days->left = infoPtr->days.left;
494   days->right = infoPtr->days.right;
495   days->top = infoPtr->days.top;
496   days->bottom = infoPtr->days.bottom;
497
498   i = infoPtr->firstDay;
499
500   for(j=0; j<7; j++) {
501     DrawTextA(hdc, daytxt[i], strlen(daytxt[i]), days,
502                          DT_CENTER | DT_VCENTER | DT_SINGLELINE );
503     i = (i + 1) % 7;
504     days->left+=infoPtr->width_increment;
505     days->right+=infoPtr->width_increment;
506   }
507
508   days->left = rcDraw->left + j;
509   if(dwStyle & MCS_WEEKNUMBERS) days->left+=textWidth;
510   /* FIXME: this may need to be changed now 11/10/99 CMM */     
511   days->right = rcDraw->left + (j+1) * textWidth - 2;
512
513 /* draw day numbers; first, the previous month */
514   
515   firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear);
516   
517   prevMonth = infoPtr->currentMonth - 1;
518   if(prevMonth == 0) /* if currentMonth is january(1) prevMonth is */
519     prevMonth = 12;    /* december(12) of the previous year */
520   
521   day = MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear) - firstDay;
522   mask = 1<<(day-1);
523
524   i = 0;
525   m = 0;
526   while(day <= MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear)) {
527     MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
528     if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
529     {
530       MONTHCAL_DrawDay(hdc, infoPtr, day, prevMonth, i, 0, 
531           infoPtr->monthdayState[m] & mask);
532     }
533
534     mask<<=1;
535     day++;
536     i++;
537   }
538
539   prevmonth->left = 0;
540   if(dwStyle & MCS_WEEKNUMBERS) prevmonth->left = textWidth;
541   prevmonth->right  = prevmonth->left + (i * infoPtr->width_increment) +
542                       infoPtr->left_offset;
543   prevmonth->top    = days->bottom;
544   prevmonth->bottom = prevmonth->top + textHeight;
545
546 /* draw `current' month  */
547
548   day = 1; /* start at the beginning of the current month */
549
550   infoPtr->firstDayplace = i;
551   SetTextColor(hdc, infoPtr->txt);
552   m++;
553   mask = 1;
554
555   /* draw the first week of the current month */
556   while(i<7) {
557     MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
558     if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
559     {
560
561       MONTHCAL_DrawDay(hdc, infoPtr, day, infoPtr->currentMonth, i, 0, 
562         infoPtr->monthdayState[m] & mask);
563
564       if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
565           (day==infoPtr->todaysDate.wDay) &&
566           (infoPtr->currentYear == infoPtr->todaysDate.wYear)) {
567         MONTHCAL_CircleDay(hdc, infoPtr, day, infoPtr->currentMonth);
568       }
569     }
570
571     mask<<=1;
572     day++;
573     i++;
574   }
575
576   j = 1; /* move to the 2nd week of the current month */
577   i = 0; /* move back to sunday */
578   while(day <= MONTHCAL_MonthLength(infoPtr->currentMonth, infoPtr->currentYear)) {     
579     MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
580     if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
581     {
582       MONTHCAL_DrawDay(hdc, infoPtr, day, infoPtr->currentMonth, i, j,
583           infoPtr->monthdayState[m] & mask);
584
585       if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
586           (day==infoPtr->todaysDate.wDay) &&
587           (infoPtr->currentYear == infoPtr->todaysDate.wYear)) 
588         MONTHCAL_CircleDay(hdc, infoPtr, day, infoPtr->currentMonth);
589     }
590     mask<<=1;
591     day++;
592     i++;
593     if(i>6) { /* past saturday, goto the next weeks sunday */
594       i = 0;
595       j++;
596     }
597   }
598
599 /*  draw `next' month */
600
601 /* note: the nextmonth rect only hints for the `half-week' that needs to be
602  * drawn to complete the current week. An eventual next week that needs to
603  * be drawn to complete the month calendar is not taken into account in
604  * this rect -- HitTest knows about this.*/
605   nextmonth->left = rcDraw->left + (i * infoPtr->width_increment) +
606                     infoPtr->left_offset;
607   nextmonth->right  = rcDraw->right;
608   nextmonth->top    = days->bottom + (j+1) * textHeight;
609   nextmonth->bottom = nextmonth->top + textHeight;
610
611   day = 1; /* start at the first day of the next month */
612   m++;
613   mask = 1;
614
615   SetTextColor(hdc, infoPtr->trailingtxt);
616   while((i<7) &&(j<6)) {
617     MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
618     if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
619     {   
620       MONTHCAL_DrawDay(hdc, infoPtr, day, infoPtr->currentMonth + 1, i, j,
621                 infoPtr->monthdayState[m] & mask);
622     }
623
624     mask<<=1;
625     day++;
626     i++;        
627     if(i==7) { /* past saturday, go to next week's sunday */
628       i = 0;
629       j++;
630     }
631   }
632   SetTextColor(hdc, infoPtr->txt);
633
634
635 /* draw `today' date if style allows it, and draw a circle before today's
636  * date if necessary */
637
638   if(!(dwStyle & MCS_NOTODAY))  {
639     int offset = 0;
640     if(!(dwStyle & MCS_NOTODAYCIRCLE))  {
641       day = MONTHCAL_CalcDayFromPos(infoPtr, 0, nextmonth->bottom + textHeight);
642       MONTHCAL_CircleDay(hdc, infoPtr, day, infoPtr->currentMonth);
643       offset+=textWidth;
644     }
645     MONTHCAL_CalcDayRect(infoPtr, rtoday, 1, 6);
646     sprintf(buf, "Today: %d/%d/%d", infoPtr->todaysDate.wMonth,
647              infoPtr->todaysDate.wDay, infoPtr->todaysDate.wYear);
648     rtoday->left = rtoday->left + 3; /* move text slightly away from circle */
649     rtoday->right = rcDraw->right;
650     SelectObject(hdc, infoPtr->hBoldFont);
651
652     if(IntersectRect(&rcTemp, &(ps->rcPaint), rtoday))
653     {
654       DrawTextA(hdc, buf, -1, rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
655     }
656     SelectObject(hdc, infoPtr->hFont);
657   }
658
659   if(dwStyle & MCS_WEEKNUMBERS)  {
660     /* display weeknumbers*/
661     weeknums->left   = 0;
662     weeknums->right  = textWidth;
663     weeknums->top    = days->bottom + 2;
664     weeknums->bottom = days->bottom + 2 + textHeight;
665                 
666     weeknum = 0;
667     for(i=0; i<infoPtr->currentMonth-1; i++) 
668       weeknum+=MONTHCAL_MonthLength(i, infoPtr->currentYear);
669
670     weeknum/=7;
671     for(i=0; i<6; i++) {
672       sprintf(buf, "%d", weeknum + i);
673       DrawTextA(hdc, buf, -1, weeknums, DT_CENTER | DT_BOTTOM | DT_SINGLELINE );
674       weeknums->top+=textHeight * 1.25;
675       weeknums->bottom+=textHeight * 1.25;
676     }
677                         
678     MoveToEx(hdc, weeknums->right, days->bottom + 5 , NULL);
679     LineTo(hdc, weeknums->right, weeknums->bottom - 1.25 * textHeight - 5);
680                 
681   }
682
683   /* currentFont was font at entering Refresh */
684
685   SetBkColor(hdc, oldBkColor);
686   SelectObject(hdc, currentFont);     
687   SetTextColor(hdc, oldTextColor);
688 }
689
690
691 static LRESULT 
692 MONTHCAL_GetMinReqRect(HWND hwnd, WPARAM wParam, LPARAM lParam)
693 {
694   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
695   LPRECT lpRect = (LPRECT) lParam;
696   TRACE("%x %lx\n", wParam, lParam);
697         
698   /* validate parameters */
699
700   if((infoPtr==NULL) ||(lpRect == NULL) ) return FALSE;
701
702   lpRect->left = infoPtr->rcClient.left;
703   lpRect->right = infoPtr->rcClient.right;
704   lpRect->top = infoPtr->rcClient.top;
705   lpRect->bottom = infoPtr->rcClient.bottom;
706   return TRUE;
707 }
708
709
710 static LRESULT 
711 MONTHCAL_GetColor(HWND hwnd, WPARAM wParam, LPARAM lParam)
712 {
713   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
714
715   TRACE("%x %lx\n", wParam, lParam);
716
717   switch((int)wParam) {
718     case MCSC_BACKGROUND:
719       return infoPtr->bk;
720     case MCSC_TEXT:
721       return infoPtr->txt;
722     case MCSC_TITLEBK:
723       return infoPtr->titlebk;
724     case MCSC_TITLETEXT:
725       return infoPtr->titletxt;
726     case MCSC_MONTHBK:
727       return infoPtr->monthbk;
728     case MCSC_TRAILINGTEXT:
729       return infoPtr->trailingtxt;
730   }
731
732   return -1;
733 }
734
735
736 static LRESULT 
737 MONTHCAL_SetColor(HWND hwnd, WPARAM wParam, LPARAM lParam)
738 {
739   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
740   int prev = -1;
741
742   TRACE("%x %lx\n", wParam, lParam);
743
744   switch((int)wParam) {
745     case MCSC_BACKGROUND:
746       prev = infoPtr->bk;
747       infoPtr->bk = (COLORREF)lParam;
748       break;
749     case MCSC_TEXT:
750       prev = infoPtr->txt;
751       infoPtr->txt = (COLORREF)lParam;
752       break;
753     case MCSC_TITLEBK:
754       prev = infoPtr->titlebk;
755       infoPtr->titlebk = (COLORREF)lParam;
756       break;
757     case MCSC_TITLETEXT:
758       prev=infoPtr->titletxt;
759       infoPtr->titletxt = (COLORREF)lParam;
760       break;
761     case MCSC_MONTHBK:
762       prev = infoPtr->monthbk;
763       infoPtr->monthbk = (COLORREF)lParam;
764       break;
765     case MCSC_TRAILINGTEXT:
766       prev = infoPtr->trailingtxt;
767       infoPtr->trailingtxt = (COLORREF)lParam;
768       break;
769   }
770
771   return prev;
772 }
773
774
775 static LRESULT 
776 MONTHCAL_GetMonthDelta(HWND hwnd, WPARAM wParam, LPARAM lParam)
777 {
778   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
779
780   TRACE("%x %lx\n", wParam, lParam);
781   
782   if(infoPtr->delta)
783     return infoPtr->delta;
784   else
785     return infoPtr->visible;
786 }
787
788
789 static LRESULT 
790 MONTHCAL_SetMonthDelta(HWND hwnd, WPARAM wParam, LPARAM lParam)
791 {
792   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
793   int prev = infoPtr->delta;
794
795   TRACE("%x %lx\n", wParam, lParam);
796         
797   infoPtr->delta = (int)wParam;
798   return prev;
799 }
800
801
802 static LRESULT 
803 MONTHCAL_GetFirstDayOfWeek(HWND hwnd, WPARAM wParam, LPARAM lParam)
804 {
805   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
806         
807   return infoPtr->firstDay;
808 }
809
810
811 /* sets the first day of the week that will appear in the control */
812 /* 0 == Monday, 6 == Sunday */
813 /* FIXME: this needs to be implemented properly in MONTHCAL_Refresh() */
814 /* FIXME: we need more error checking here */
815 static LRESULT 
816 MONTHCAL_SetFirstDayOfWeek(HWND hwnd, WPARAM wParam, LPARAM lParam)
817 {
818   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
819   int prev = infoPtr->firstDay;
820   char buf[40];
821   int day;
822
823   TRACE("%x %lx\n", wParam, lParam);
824
825   if((lParam >= 0) && (lParam < 7)) {
826     infoPtr->firstDay = (int)lParam;
827     GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK,
828                 buf, sizeof(buf));
829     TRACE("%s %d\n", buf, strlen(buf));
830     if((sscanf(buf, "%d", &day) == 1) &&(infoPtr->firstDay != day)) 
831       infoPtr->firstDay = day;  
832   }
833   return prev;
834 }
835
836
837 /* FIXME: fill this in */
838 static LRESULT
839 MONTHCAL_GetMonthRange(HWND hwnd, WPARAM wParam, LPARAM lParam) 
840 {
841   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
842
843   TRACE("%x %lx\n", wParam, lParam);
844   FIXME("stub\n");
845
846   return infoPtr->monthRange;
847 }
848
849
850 static LRESULT
851 MONTHCAL_GetMaxTodayWidth(HWND hwnd)
852 {
853   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
854
855   return(infoPtr->today.right - infoPtr->today.left);
856 }
857
858
859 /* FIXME: are validated times taken from current date/time or simply
860  * copied? 
861  * FIXME:    check whether MCM_GETMONTHRANGE shows correct result after
862  *            adjusting range with MCM_SETRANGE
863  */
864
865 static LRESULT
866 MONTHCAL_SetRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
867 {
868   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
869   SYSTEMTIME lprgSysTimeArray[1];
870   int prev;
871
872   TRACE("%x %lx\n", wParam, lParam);
873   
874   if(wParam & GDTR_MAX) {
875     if(MONTHCAL_ValidateTime(lprgSysTimeArray[1])){
876       MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxDate);
877       infoPtr->rangeValid|=GDTR_MAX;
878     } else  {
879       GetSystemTime(&infoPtr->todaysDate);
880       MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
881     }
882   }
883   if(wParam & GDTR_MIN) {
884     if(MONTHCAL_ValidateTime(lprgSysTimeArray[0])) {
885       MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->maxDate);
886       infoPtr->rangeValid|=GDTR_MIN;
887     } else {
888       GetSystemTime(&infoPtr->todaysDate);
889       MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
890     }
891   }
892
893   prev = infoPtr->monthRange;
894   infoPtr->monthRange = infoPtr->maxDate.wMonth - infoPtr->minDate.wMonth;
895
896   if(infoPtr->monthRange!=prev) { 
897         COMCTL32_ReAlloc(infoPtr->monthdayState, 
898                 infoPtr->monthRange * sizeof(MONTHDAYSTATE));
899   }
900
901   return 1;
902 }
903
904
905 /* CHECKME: At the moment, we copy ranges anyway,regardless of
906  * infoPtr->rangeValid; a invalid range is simply filled with zeros in 
907  * SetRange.  Is this the right behavior?
908 */
909
910 static LRESULT
911 MONTHCAL_GetRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
912 {
913   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
914   SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *)lParam;
915
916   /* validate parameters */
917
918   if((infoPtr==NULL) || (lprgSysTimeArray==NULL)) return FALSE;
919
920   MONTHCAL_CopyTime(&infoPtr->maxDate, &lprgSysTimeArray[1]);
921   MONTHCAL_CopyTime(&infoPtr->minDate, &lprgSysTimeArray[0]);
922
923   return infoPtr->rangeValid;
924 }
925
926
927 static LRESULT
928 MONTHCAL_SetDayState(HWND hwnd, WPARAM wParam, LPARAM lParam)
929
930 {
931   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
932   int i, iMonths = (int)wParam;
933   MONTHDAYSTATE *dayStates = (LPMONTHDAYSTATE)lParam;
934
935   TRACE("%x %lx\n", wParam, lParam);
936   if(iMonths!=infoPtr->monthRange) return 0;
937
938   for(i=0; i<iMonths; i++) 
939     infoPtr->monthdayState[i] = dayStates[i];
940   return 1;
941 }
942
943
944 static LRESULT 
945 MONTHCAL_GetCurSel(HWND hwnd, WPARAM wParam, LPARAM lParam)
946 {
947   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
948   SYSTEMTIME *lpSel = (SYSTEMTIME *) lParam;
949
950   TRACE("%x %lx\n", wParam, lParam);
951   if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
952   if(GetWindowLongA(hwnd, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
953
954   MONTHCAL_CopyTime(&infoPtr->minSel, lpSel);
955   return TRUE;
956 }
957
958
959 /* FIXME: if the specified date is not visible, make it visible */
960 /* FIXME: redraw? */
961 static LRESULT 
962 MONTHCAL_SetCurSel(HWND hwnd, WPARAM wParam, LPARAM lParam)
963 {
964   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
965   SYSTEMTIME *lpSel = (SYSTEMTIME *)lParam;
966
967   TRACE("%x %lx\n", wParam, lParam);
968   if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
969   if(GetWindowLongA(hwnd, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
970
971   TRACE("%d %d\n", lpSel->wMonth, lpSel->wDay);
972
973   MONTHCAL_CopyTime(lpSel, &infoPtr->minSel);
974   MONTHCAL_CopyTime(lpSel, &infoPtr->maxSel);
975
976   InvalidateRect(hwnd, NULL, FALSE);
977
978   return TRUE;
979 }
980
981
982 static LRESULT 
983 MONTHCAL_GetMaxSelCount(HWND hwnd, WPARAM wParam, LPARAM lParam)
984 {
985   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
986
987   TRACE("%x %lx\n", wParam, lParam);
988   return infoPtr->maxSelCount;
989 }
990
991
992 static LRESULT 
993 MONTHCAL_SetMaxSelCount(HWND hwnd, WPARAM wParam, LPARAM lParam)
994 {
995   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
996
997   TRACE("%x %lx\n", wParam, lParam);
998   if(GetWindowLongA(hwnd, GWL_STYLE) & MCS_MULTISELECT)  {
999     infoPtr->maxSelCount = wParam;
1000   }
1001
1002   return TRUE;
1003 }
1004
1005
1006 static LRESULT 
1007 MONTHCAL_GetSelRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
1008 {
1009   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1010   SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1011
1012   TRACE("%x %lx\n", wParam, lParam);
1013
1014   /* validate parameters */
1015
1016   if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1017
1018   if(GetWindowLongA(hwnd, GWL_STYLE) & MCS_MULTISELECT)
1019   {
1020     MONTHCAL_CopyTime(&infoPtr->maxSel, &lprgSysTimeArray[1]);
1021     MONTHCAL_CopyTime(&infoPtr->minSel, &lprgSysTimeArray[0]);
1022     TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1023     return TRUE;
1024   }
1025  
1026   return FALSE;
1027 }
1028
1029
1030 static LRESULT 
1031 MONTHCAL_SetSelRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
1032 {
1033   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1034   SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1035
1036   TRACE("%x %lx\n", wParam, lParam);
1037
1038   /* validate parameters */
1039
1040   if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1041
1042   if(GetWindowLongA( hwnd, GWL_STYLE) & MCS_MULTISELECT)
1043   {
1044     MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxSel);
1045     MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minSel);
1046     TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1047     return TRUE;
1048   }
1049  
1050   return FALSE;
1051 }
1052
1053
1054 static LRESULT 
1055 MONTHCAL_GetToday(HWND hwnd, WPARAM wParam, LPARAM lParam)
1056 {
1057   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1058   SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1059
1060   TRACE("%x %lx\n", wParam, lParam);
1061
1062   /* validate parameters */
1063
1064   if((infoPtr==NULL) || (lpToday==NULL)) return FALSE;
1065   MONTHCAL_CopyTime(&infoPtr->todaysDate, lpToday);
1066   return TRUE;
1067 }
1068
1069
1070 static LRESULT 
1071 MONTHCAL_SetToday(HWND hwnd, WPARAM wParam, LPARAM lParam)
1072 {
1073   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1074   SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1075
1076   TRACE("%x %lx\n", wParam, lParam);
1077
1078   /* validate parameters */
1079
1080   if((infoPtr==NULL) ||(lpToday==NULL)) return FALSE;
1081   MONTHCAL_CopyTime(lpToday, &infoPtr->todaysDate);
1082   return TRUE;
1083 }
1084
1085
1086 static LRESULT
1087 MONTHCAL_HitTest(HWND hwnd, LPARAM lParam)
1088 {
1089  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1090  PMCHITTESTINFO lpht = (PMCHITTESTINFO)lParam;
1091  UINT x,y;
1092  DWORD retval;
1093
1094  x = lpht->pt.x;
1095  y = lpht->pt.y;
1096  retval = MCHT_NOWHERE;
1097  
1098
1099   /* are we in the header? */
1100
1101   if(PtInRect(&infoPtr->title, lpht->pt)) {
1102     if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1103       retval = MCHT_TITLEBTNPREV;
1104       goto done;
1105     }
1106     if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1107       retval = MCHT_TITLEBTNNEXT;
1108       goto done;
1109     }
1110     if(PtInRect(&infoPtr->titlemonth, lpht->pt)) {
1111       retval = MCHT_TITLEMONTH;
1112       goto done;
1113     }
1114     if(PtInRect(&infoPtr->titleyear, lpht->pt)) {
1115       retval = MCHT_TITLEYEAR;
1116       goto done;
1117     }
1118     
1119     retval = MCHT_TITLE;
1120     goto done;
1121   }
1122
1123   if(PtInRect(&infoPtr->days, lpht->pt)) {
1124     retval = MCHT_CALENDARDAY;  /* FIXME: find out which day we're on */
1125     goto done;
1126   }
1127   if(PtInRect(&infoPtr->weeknums, lpht->pt)) {  
1128     retval = MCHT_CALENDARWEEKNUM; /* FIXME: find out which day we're on */
1129     goto done;                              
1130   }
1131   if(PtInRect(&infoPtr->prevmonth, lpht->pt)) {  
1132     retval = MCHT_CALENDARDATEPREV;
1133     goto done;                              
1134   }
1135
1136   if(PtInRect(&infoPtr->nextmonth, lpht->pt) ||
1137   ((y > infoPtr->nextmonth.bottom) && (y < infoPtr->nextmonth.bottom +
1138       infoPtr->height_increment) && (x < infoPtr->rcClient.right) &&
1139       (x > infoPtr->rcDraw.left))) {
1140     retval = MCHT_CALENDARDATENEXT;
1141     goto done;                             
1142   }
1143
1144   if(PtInRect(&infoPtr->today, lpht->pt)) {
1145     retval = MCHT_TODAYLINK; 
1146     goto done;
1147   }
1148
1149 /* MCHT_CALENDARDATE determination: since the next & previous month have
1150  * been handled already(MCHT_CALENDARDATEPREV/NEXT), we only have to check
1151  * whether we're in the calendar area. infoPtr->prevMonth.left handles the 
1152  * MCS_WEEKNUMBERS style nicely.
1153  */
1154         
1155
1156  TRACE("%d %d [%d %d %d %d] [%d %d %d %d]\n", x, y, 
1157         infoPtr->prevmonth.left, infoPtr->prevmonth.right,
1158         infoPtr->prevmonth.top, infoPtr->prevmonth.bottom,
1159         infoPtr->nextmonth.left, infoPtr->nextmonth.right,
1160         infoPtr->nextmonth.top, infoPtr->nextmonth.bottom);
1161   if((x > infoPtr->rcClient.left) && (x < infoPtr->rcClient.right) &&
1162        (y > infoPtr->rcClient.top) && (y < infoPtr->nextmonth.bottom)) {
1163     lpht->st.wYear = infoPtr->currentYear;
1164     lpht->st.wMonth = infoPtr->currentMonth;
1165                 
1166     lpht->st.wDay = MONTHCAL_CalcDayFromPos(infoPtr, x, y);
1167
1168     TRACE("day hit: %d\n", lpht->st.wDay);
1169     retval = MCHT_CALENDARDATE;
1170     goto done;
1171
1172   }
1173
1174   /* Hit nothing special? What's left must be background :-) */
1175                 
1176   retval = MCHT_CALENDARBK;       
1177  done: 
1178   lpht->uHit = retval;
1179   return retval;
1180 }
1181
1182
1183 static void MONTHCAL_GoToNextMonth(HWND hwnd, MONTHCAL_INFO *infoPtr)
1184 {
1185   DWORD dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
1186
1187   TRACE("MONTHCAL_GoToNextMonth\n");
1188
1189   infoPtr->currentMonth++;
1190   if(infoPtr->currentMonth > 12) {
1191     infoPtr->currentYear++;
1192     infoPtr->currentMonth = 1;
1193   }
1194
1195   if(dwStyle & MCS_DAYSTATE) {
1196     NMDAYSTATE nmds;
1197     int i;
1198
1199     nmds.nmhdr.hwndFrom = hwnd;
1200     nmds.nmhdr.idFrom   = GetWindowLongA(hwnd, GWL_ID);
1201     nmds.nmhdr.code     = MCN_GETDAYSTATE;
1202     nmds.cDayState      = infoPtr->monthRange;
1203     nmds.prgDayState    = COMCTL32_Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1204
1205     SendMessageA(GetParent(hwnd), WM_NOTIFY,
1206     (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
1207     for(i=0; i<infoPtr->monthRange; i++)
1208       infoPtr->monthdayState[i] = nmds.prgDayState[i];
1209   }
1210 }
1211
1212
1213 static void MONTHCAL_GoToPrevMonth(HWND hwnd,  MONTHCAL_INFO *infoPtr)
1214 {
1215   DWORD dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
1216
1217   TRACE("MONTHCAL_GoToPrevMonth\n");
1218
1219   infoPtr->currentMonth--;
1220   if(infoPtr->currentMonth < 1) {
1221     infoPtr->currentYear--;
1222     infoPtr->currentMonth = 12;
1223   }
1224
1225   if(dwStyle & MCS_DAYSTATE) {
1226     NMDAYSTATE nmds;
1227     int i;
1228
1229     nmds.nmhdr.hwndFrom = hwnd;
1230     nmds.nmhdr.idFrom   = GetWindowLongA(hwnd, GWL_ID);
1231     nmds.nmhdr.code     = MCN_GETDAYSTATE;
1232     nmds.cDayState      = infoPtr->monthRange;
1233     nmds.prgDayState    = COMCTL32_Alloc 
1234                         (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1235
1236     SendMessageA(GetParent(hwnd), WM_NOTIFY,
1237         (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
1238     for(i=0; i<infoPtr->monthRange; i++)
1239        infoPtr->monthdayState[i] = nmds.prgDayState[i];
1240   }
1241 }
1242
1243
1244 static LRESULT
1245 MONTHCAL_LButtonDown(HWND hwnd, WPARAM wParam, LPARAM lParam)
1246 {
1247   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1248   MCHITTESTINFO ht;
1249   DWORD hit;
1250   HMENU hMenu;
1251   HWND retval;
1252   BOOL redraw = FALSE;
1253   RECT rcDay; /* used in determining area to invalidate */
1254
1255   TRACE("%x %lx\n", wParam, lParam);
1256         
1257   ht.pt.x = (INT)LOWORD(lParam);
1258   ht.pt.y = (INT)HIWORD(lParam);
1259   hit = MONTHCAL_HitTest(hwnd, (LPARAM)&ht);
1260
1261   /* FIXME: these flags should be checked by */
1262   /*((hit & MCHT_XXX) == MCHT_XXX) b/c some of the flags are */
1263   /* multi-bit */
1264   if(hit & MCHT_NEXT) {
1265     redraw = TRUE;
1266     MONTHCAL_GoToNextMonth(hwnd, infoPtr);
1267     infoPtr->status = MC_NEXTPRESSED;
1268     SetTimer(hwnd, MC_NEXTMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1269     InvalidateRect(hwnd, NULL, FALSE);
1270   }
1271   if(hit & MCHT_PREV) { 
1272     redraw = TRUE;
1273     MONTHCAL_GoToPrevMonth(hwnd, infoPtr);
1274     infoPtr->status = MC_PREVPRESSED;
1275     SetTimer(hwnd, MC_PREVMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1276     InvalidateRect(hwnd, NULL, FALSE);
1277   }
1278
1279   if(hit == MCHT_TITLEMONTH) {
1280 /*
1281     HRSRC hrsrc = FindResourceA( COMCTL32_hModule, MAKEINTRESOURCEA(IDD_MCMONTHMENU), RT_MENUA );
1282     if(!hrsrc) { 
1283       TRACE("returning zero\n");
1284       return 0;
1285     }
1286     TRACE("resource is:%x\n",hrsrc);
1287     hMenu = LoadMenuIndirectA((LPCVOID)LoadResource( COMCTL32_hModule, hrsrc ));
1288                         
1289     TRACE("menu is:%x\n",hMenu);
1290 */
1291
1292     hMenu = CreateMenu();
1293     AppendMenuA(hMenu, MF_STRING,IDM_JAN, "January");
1294     AppendMenuA(hMenu, MF_STRING,IDM_FEB, "February");
1295     AppendMenuA(hMenu, MF_STRING,IDM_MAR, "March");
1296         
1297     retval = CreateWindowA(POPUPMENU_CLASS_ATOM, NULL, 
1298               WS_CHILD | WS_VISIBLE, 0, 0 ,100 , 220, 
1299               hwnd, hMenu, GetWindowLongA(hwnd, GWL_HINSTANCE), NULL);
1300     TRACE("hwnd returned:%x\n", retval);
1301
1302   }
1303   if(hit == MCHT_TITLEYEAR) {
1304     FIXME("create updown for yearselection\n");
1305   }
1306   if(hit == MCHT_TODAYLINK) {
1307     FIXME("set currentday\n");
1308   }
1309   if(hit == MCHT_CALENDARDATE) {
1310     SYSTEMTIME selArray[2];
1311     NMSELCHANGE nmsc;
1312
1313     TRACE("\n");
1314     nmsc.nmhdr.hwndFrom = hwnd;
1315     nmsc.nmhdr.idFrom   = GetWindowLongA(hwnd, GWL_ID);
1316     nmsc.nmhdr.code     = MCN_SELCHANGE;
1317     MONTHCAL_CopyTime(&nmsc.stSelStart, &infoPtr->minSel);
1318     MONTHCAL_CopyTime(&nmsc.stSelEnd, &infoPtr->maxSel);
1319         
1320     SendMessageA(GetParent(hwnd), WM_NOTIFY,
1321            (WPARAM)nmsc.nmhdr.idFrom,(LPARAM)&nmsc);
1322
1323     MONTHCAL_CopyTime(&ht.st, &selArray[0]);
1324     MONTHCAL_CopyTime(&ht.st, &selArray[1]);
1325     MONTHCAL_SetSelRange(hwnd,0,(LPARAM) &selArray); 
1326
1327     /* redraw both old and new days if the selected day changed */
1328     if(infoPtr->curSelDay != ht.st.wDay) {
1329       MONTHCAL_CalcPosFromDay(infoPtr, ht.st.wDay, ht.st.wMonth, &rcDay);
1330       InvalidateRect(hwnd, &rcDay, TRUE);
1331
1332       MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->curSelDay, infoPtr->currentMonth, &rcDay);
1333       InvalidateRect(hwnd, &rcDay, TRUE);
1334     }
1335
1336     infoPtr->firstSelDay = ht.st.wDay;
1337     infoPtr->curSelDay = ht.st.wDay;
1338     infoPtr->status = MC_SEL_LBUTDOWN;
1339
1340   }
1341
1342   return 0;
1343 }
1344
1345
1346 static LRESULT
1347 MONTHCAL_LButtonUp(HWND hwnd, WPARAM wParam, LPARAM lParam)
1348 {
1349   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1350   NMSELCHANGE nmsc;
1351   NMHDR nmhdr;
1352   BOOL redraw = FALSE;
1353
1354   TRACE("\n");
1355
1356   if(infoPtr->status & MC_NEXTPRESSED) {
1357     KillTimer(hwnd, MC_NEXTMONTHTIMER);
1358     redraw = TRUE;
1359   }
1360   if(infoPtr->status & MC_PREVPRESSED) {
1361     KillTimer(hwnd, MC_PREVMONTHTIMER);
1362     redraw = TRUE;
1363   }
1364
1365   infoPtr->status = MC_SEL_LBUTUP;
1366
1367   nmhdr.hwndFrom = hwnd;
1368   nmhdr.idFrom   = GetWindowLongA( hwnd, GWL_ID);
1369   nmhdr.code     = NM_RELEASEDCAPTURE;
1370   TRACE("Sent notification from %x to %x\n", hwnd, GetParent(hwnd));
1371
1372   SendMessageA(GetParent(hwnd), WM_NOTIFY,
1373                                 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
1374
1375   nmsc.nmhdr.hwndFrom = hwnd;
1376   nmsc.nmhdr.idFrom   = GetWindowLongA(hwnd, GWL_ID);
1377   nmsc.nmhdr.code     = MCN_SELECT;
1378   MONTHCAL_CopyTime(&nmsc.stSelStart, &infoPtr->minSel);
1379   MONTHCAL_CopyTime(&nmsc.stSelEnd, &infoPtr->maxSel);
1380         
1381   SendMessageA(GetParent(hwnd), WM_NOTIFY,
1382            (WPARAM)nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1383   
1384   /* redraw if necessary */
1385   if(redraw)
1386     InvalidateRect(hwnd, NULL, FALSE);
1387         
1388   return 0;
1389 }
1390
1391
1392 static LRESULT
1393 MONTHCAL_Timer(HWND hwnd, WPARAM wParam, LPARAM lParam)
1394 {
1395   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1396   BOOL redraw = FALSE;
1397
1398   TRACE(" %d\n", wParam);
1399   if(!infoPtr) return 0;
1400
1401   switch(wParam) {
1402   case MC_NEXTMONTHTIMER: 
1403     redraw = TRUE;
1404     MONTHCAL_GoToNextMonth(hwnd, infoPtr);
1405     break;
1406   case MC_PREVMONTHTIMER:
1407     redraw = TRUE;
1408     MONTHCAL_GoToPrevMonth(hwnd, infoPtr);
1409     break;
1410   default:
1411     ERR("got unknown timer\n");
1412   }
1413
1414   /* redraw only if necessary */
1415   if(redraw)
1416     InvalidateRect(hwnd, NULL, FALSE);
1417
1418   return 0;
1419 }
1420
1421
1422 static LRESULT
1423 MONTHCAL_MouseMove(HWND hwnd, WPARAM wParam, LPARAM lParam)
1424 {
1425   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1426   MCHITTESTINFO ht;
1427   int oldselday, selday, hit;
1428   RECT r;
1429
1430   if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
1431
1432   ht.pt.x = LOWORD(lParam);
1433   ht.pt.y = HIWORD(lParam);
1434         
1435   hit = MONTHCAL_HitTest(hwnd, (LPARAM)&ht);
1436   
1437   /* not on the calendar date numbers? bail out */
1438   TRACE("hit:%x\n",hit);
1439   if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE) return 0;
1440
1441   selday = ht.st.wDay;
1442   oldselday = infoPtr->curSelDay;
1443   infoPtr->curSelDay = selday;
1444   MONTHCAL_CalcPosFromDay(infoPtr, selday, ht.st. wMonth, &r);
1445
1446   if(GetWindowLongA(hwnd, GWL_STYLE) & MCS_MULTISELECT)  {
1447     SYSTEMTIME selArray[2];
1448     int i;
1449
1450     MONTHCAL_GetSelRange(hwnd, 0, (LPARAM)&selArray);
1451     i = 0;
1452     if(infoPtr->firstSelDay==selArray[0].wDay) i=1;
1453     TRACE("oldRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1454     if(infoPtr->firstSelDay==selArray[1].wDay) {  
1455       /* 1st time we get here: selArray[0]=selArray[1])  */
1456       /* if we're still at the first selected date, return */
1457       if(infoPtr->firstSelDay==selday) goto done;
1458       if(selday<infoPtr->firstSelDay) i = 0;
1459     }
1460                         
1461     if(abs(infoPtr->firstSelDay - selday) >= infoPtr->maxSelCount) {
1462       if(selday>infoPtr->firstSelDay)
1463         selday = infoPtr->firstSelDay + infoPtr->maxSelCount;
1464       else
1465         selday = infoPtr->firstSelDay - infoPtr->maxSelCount;
1466     }
1467                 
1468     if(selArray[i].wDay!=selday) {
1469       TRACE("newRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1470                         
1471       selArray[i].wDay = selday;
1472
1473       if(selArray[0].wDay>selArray[1].wDay) {
1474         DWORD tempday;
1475         tempday = selArray[1].wDay;
1476         selArray[1].wDay = selArray[0].wDay;
1477         selArray[0].wDay = tempday;
1478       }
1479
1480       MONTHCAL_SetSelRange(hwnd, 0, (LPARAM)&selArray);
1481     }
1482   }
1483
1484 done:
1485
1486   /* only redraw if the currently selected day changed */
1487   /* FIXME: this should specify a rectangle containing only the days that changed */
1488   /* using InvalidateRect */
1489   if(oldselday != infoPtr->curSelDay)
1490     InvalidateRect(hwnd, NULL, FALSE);
1491
1492   return 0;
1493 }
1494
1495
1496 static LRESULT
1497 MONTHCAL_Paint(HWND hwnd, WPARAM wParam)
1498 {
1499   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1500   HDC hdc;
1501   PAINTSTRUCT ps;
1502
1503   /* fill ps.rcPaint with a default rect */
1504   memcpy(&(ps.rcPaint), &(infoPtr->rcClient), sizeof(infoPtr->rcClient));
1505
1506   hdc = (wParam==0 ? BeginPaint(hwnd, &ps) : (HDC)wParam);
1507   MONTHCAL_Refresh(hwnd, hdc, &ps);
1508   if(!wParam) EndPaint(hwnd, &ps);
1509   return 0;
1510 }
1511
1512
1513 static LRESULT
1514 MONTHCAL_KillFocus(HWND hwnd, WPARAM wParam, LPARAM lParam)
1515 {
1516   TRACE("\n");
1517
1518   InvalidateRect(hwnd, NULL, TRUE);
1519
1520   return 0;
1521 }
1522
1523
1524 static LRESULT
1525 MONTHCAL_SetFocus(HWND hwnd, WPARAM wParam, LPARAM lParam)
1526 {
1527   TRACE("\n");
1528   
1529   InvalidateRect(hwnd, NULL, FALSE);
1530
1531   return 0;
1532 }
1533
1534 /* sets the size information */
1535 static void MONTHCAL_UpdateSize(HWND hwnd)
1536 {
1537   HDC hdc = GetDC(hwnd);
1538   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1539   RECT *rcClient=&infoPtr->rcClient;
1540   RECT *rcDraw=&infoPtr->rcDraw;
1541   RECT *title=&infoPtr->title;
1542   RECT *prev=&infoPtr->titlebtnprev;
1543   RECT *next=&infoPtr->titlebtnnext;
1544   RECT *titlemonth=&infoPtr->titlemonth;
1545   RECT *titleyear=&infoPtr->titleyear;
1546   RECT *days=&infoPtr->days;
1547   SIZE size;
1548   TEXTMETRICA tm;
1549   DWORD dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
1550   HFONT currentFont;
1551
1552   currentFont = SelectObject(hdc, infoPtr->hFont);
1553
1554   /* FIXME: need a way to determine current font, without setting it */
1555   /*
1556   if(infoPtr->hFont!=currentFont) {
1557     SelectObject(hdc, currentFont);
1558     infoPtr->hFont=currentFont;
1559     GetObjectA(currentFont, sizeof(LOGFONTA), &logFont);
1560     logFont.lfWeight=FW_BOLD;
1561     infoPtr->hBoldFont = CreateFontIndirectA(&logFont);
1562   }
1563   */
1564
1565   /* get the height and width of each day's text */
1566   GetTextMetricsA(hdc, &tm);
1567   infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading;
1568   GetTextExtentPoint32A(hdc, "Sun", 3, &size);
1569   infoPtr->textWidth = size.cx + 2;
1570
1571   /* retrieve the controls client rectangle info infoPtr->rcClient */
1572   GetClientRect(hwnd, rcClient);
1573
1574   if(dwStyle & MCS_WEEKNUMBERS)
1575     infoPtr->rcClient.right+=infoPtr->textWidth;
1576
1577   /* rcDraw is the rectangle the control is drawn in */
1578   rcDraw->left = rcClient->left;
1579   rcDraw->right = rcClient->right;
1580   rcDraw->top = rcClient->top;
1581   rcDraw->bottom = rcClient->bottom;
1582
1583   /* this is correct, the control does NOT expand vertically */
1584   /* like it does horizontally */
1585   /* make sure we don't move the controls bottom out of the client */
1586   /* area */
1587   if((rcDraw->top + 8 * infoPtr->textHeight + 5) < rcDraw->bottom) {
1588     rcDraw->bottom = rcDraw->top + 8 * infoPtr->textHeight + 5;
1589   }
1590    
1591   /* calculate title area */
1592   title->top    = rcClient->top;
1593   title->bottom = title->top + 2 * infoPtr->textHeight + 4;
1594   title->left   = rcClient->left;
1595   title->right  = rcClient->right;
1596
1597   /* recalculate the height and width increments and offsets */
1598   infoPtr->width_increment = (infoPtr->rcDraw.right - infoPtr->rcDraw.left) / 7.0; 
1599   infoPtr->height_increment = (infoPtr->rcDraw.bottom - infoPtr->rcDraw.top) / 7.0; 
1600   infoPtr->left_offset = (infoPtr->rcDraw.right - infoPtr->rcDraw.left) - (infoPtr->width_increment * 7.0);
1601   infoPtr->top_offset = (infoPtr->rcDraw.bottom - infoPtr->rcDraw.top) - (infoPtr->height_increment * 7.0);
1602
1603   /* set the dimensions of the next and previous buttons and center */
1604   /* the month text vertically */
1605   prev->top        = next->top    = title->top + 6;
1606   prev->bottom = next->bottom = title->top + 2 * infoPtr->textHeight - 3;
1607   prev->right  = title->left  + 28;
1608   prev->left   = title->left  + 4;
1609   next->left   = title->right - 28;
1610   next->right  = title->right - 4;
1611   
1612   /* titlemonth->left and right change based upon the current month */
1613   /* and are recalculated in refresh as the current month may change */
1614   /* without the control being resized */
1615   titlemonth->bottom = titleyear->bottom = prev->top + 2 * infoPtr->textHeight - 3;
1616   titlemonth->top    = titleyear->top    = title->top;
1617   
1618   /* setup the dimensions of the rectangle we draw the names of the */
1619   /* days of the week in */
1620   days->left = infoPtr->left_offset;
1621   if(dwStyle & MCS_WEEKNUMBERS) days->left+=infoPtr->textWidth;
1622   days->right  = days->left + infoPtr->width_increment;
1623   days->top    = title->bottom + 2;
1624   days->bottom = title->bottom + infoPtr->textHeight + 2;
1625   
1626   /* restore the originally selected font */
1627   SelectObject(hdc, currentFont);     
1628
1629   ReleaseDC(hwnd, hdc);
1630 }
1631
1632 static LRESULT MONTHCAL_Size(HWND hwnd, int Width, int Height)
1633 {
1634   TRACE("(hwnd=%x, width=%d, height=%d)\n", hwnd, Width, Height);
1635
1636   MONTHCAL_UpdateSize(hwnd);
1637
1638   /* invalidate client area and erase background */
1639   InvalidateRect(hwnd, NULL, TRUE);
1640
1641   return 0;
1642 }
1643
1644 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
1645 static LRESULT
1646 MONTHCAL_Create(HWND hwnd, WPARAM wParam, LPARAM lParam)
1647 {
1648   MONTHCAL_INFO *infoPtr;
1649   LOGFONTA      logFont;
1650
1651   /* allocate memory for info structure */
1652   infoPtr =(MONTHCAL_INFO*)COMCTL32_Alloc(sizeof(MONTHCAL_INFO));
1653   SetWindowLongA(hwnd, 0, (DWORD)infoPtr);
1654
1655   if(infoPtr == NULL) {
1656     ERR( "could not allocate info memory!\n");
1657     return 0;
1658   }
1659   if((MONTHCAL_INFO*)GetWindowLongA(hwnd, 0) != infoPtr) {
1660     ERR( "pointer assignment error!\n");
1661     return 0;
1662   }
1663
1664   infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
1665   GetObjectA(infoPtr->hFont, sizeof(LOGFONTA), &logFont);
1666   logFont.lfWeight = FW_BOLD;
1667   infoPtr->hBoldFont = CreateFontIndirectA(&logFont);
1668
1669   /* initialize info structure */
1670   /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
1671
1672   GetSystemTime(&infoPtr->todaysDate);
1673   infoPtr->firstDay = 0;
1674   infoPtr->currentMonth = infoPtr->todaysDate.wMonth;
1675   infoPtr->currentYear = infoPtr->todaysDate.wYear;
1676   MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->minDate);
1677   MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
1678   infoPtr->maxSelCount  = 6;
1679   infoPtr->monthRange = 3;
1680   infoPtr->monthdayState = COMCTL32_Alloc 
1681                          (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1682   infoPtr->titlebk     = GetSysColor(COLOR_ACTIVECAPTION);
1683   infoPtr->titletxt    = GetSysColor(COLOR_WINDOW);
1684   infoPtr->monthbk     = GetSysColor(COLOR_WINDOW);
1685   infoPtr->trailingtxt = GetSysColor(COLOR_GRAYTEXT);
1686   infoPtr->bk          = GetSysColor(COLOR_WINDOW);
1687   infoPtr->txt         = GetSysColor(COLOR_WINDOWTEXT);
1688
1689   /* call MONTHCAL_UpdateSize to set all of the dimensions */
1690   /* of the control */
1691   MONTHCAL_UpdateSize(hwnd);
1692
1693   return 0;
1694 }
1695
1696
1697 static LRESULT
1698 MONTHCAL_Destroy(HWND hwnd, WPARAM wParam, LPARAM lParam)
1699 {
1700   MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1701
1702   /* free month calendar info data */
1703   COMCTL32_Free(infoPtr);
1704   SetWindowLongA(hwnd, 0, 0);
1705   return 0;
1706 }
1707
1708
1709 static LRESULT WINAPI
1710 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1711 {
1712   TRACE("hwnd=%x msg=%x wparam=%x lparam=%lx\n", hwnd, uMsg, wParam, lParam);
1713   if (!MONTHCAL_GetInfoPtr(hwnd) && (uMsg != WM_CREATE))
1714     return DefWindowProcA(hwnd, uMsg, wParam, lParam);
1715   switch(uMsg)
1716   {
1717   case MCM_GETCURSEL:
1718     return MONTHCAL_GetCurSel(hwnd, wParam, lParam);
1719
1720   case MCM_SETCURSEL:
1721     return MONTHCAL_SetCurSel(hwnd, wParam, lParam);
1722
1723   case MCM_GETMAXSELCOUNT:
1724     return MONTHCAL_GetMaxSelCount(hwnd, wParam, lParam);
1725
1726   case MCM_SETMAXSELCOUNT:
1727     return MONTHCAL_SetMaxSelCount(hwnd, wParam, lParam);
1728
1729   case MCM_GETSELRANGE:
1730     return MONTHCAL_GetSelRange(hwnd, wParam, lParam);
1731
1732   case MCM_SETSELRANGE:
1733     return MONTHCAL_SetSelRange(hwnd, wParam, lParam);
1734
1735   case MCM_GETMONTHRANGE:
1736     return MONTHCAL_GetMonthRange(hwnd, wParam, lParam);
1737
1738   case MCM_SETDAYSTATE:
1739     return MONTHCAL_SetDayState(hwnd, wParam, lParam);
1740
1741   case MCM_GETMINREQRECT:
1742     return MONTHCAL_GetMinReqRect(hwnd, wParam, lParam);
1743
1744   case MCM_GETCOLOR:
1745     return MONTHCAL_GetColor(hwnd, wParam, lParam);
1746
1747   case MCM_SETCOLOR:
1748     return MONTHCAL_SetColor(hwnd, wParam, lParam);
1749
1750   case MCM_GETTODAY:
1751     return MONTHCAL_GetToday(hwnd, wParam, lParam);
1752
1753   case MCM_SETTODAY:
1754     return MONTHCAL_SetToday(hwnd, wParam, lParam);
1755
1756   case MCM_HITTEST:
1757     return MONTHCAL_HitTest(hwnd,lParam);
1758
1759   case MCM_GETFIRSTDAYOFWEEK:
1760     return MONTHCAL_GetFirstDayOfWeek(hwnd, wParam, lParam);
1761
1762   case MCM_SETFIRSTDAYOFWEEK:
1763     return MONTHCAL_SetFirstDayOfWeek(hwnd, wParam, lParam);
1764
1765   case MCM_GETRANGE:
1766     return MONTHCAL_GetRange(hwnd, wParam, lParam);
1767
1768   case MCM_SETRANGE:
1769     return MONTHCAL_SetRange(hwnd, wParam, lParam);
1770
1771   case MCM_GETMONTHDELTA:
1772     return MONTHCAL_GetMonthDelta(hwnd, wParam, lParam);
1773
1774   case MCM_SETMONTHDELTA:
1775     return MONTHCAL_SetMonthDelta(hwnd, wParam, lParam);
1776
1777   case MCM_GETMAXTODAYWIDTH:
1778     return MONTHCAL_GetMaxTodayWidth(hwnd);
1779
1780   case WM_GETDLGCODE:
1781     return DLGC_WANTARROWS | DLGC_WANTCHARS;
1782
1783   case WM_KILLFOCUS:
1784     return MONTHCAL_KillFocus(hwnd, wParam, lParam);
1785
1786   case WM_LBUTTONDOWN:
1787     return MONTHCAL_LButtonDown(hwnd, wParam, lParam);
1788
1789   case WM_MOUSEMOVE:
1790     return MONTHCAL_MouseMove(hwnd, wParam, lParam);
1791
1792   case WM_LBUTTONUP:
1793     return MONTHCAL_LButtonUp(hwnd, wParam, lParam);
1794
1795   case WM_PAINT:
1796     return MONTHCAL_Paint(hwnd, wParam);
1797
1798   case WM_SETFOCUS:
1799     return MONTHCAL_SetFocus(hwnd, wParam, lParam);
1800
1801   case WM_SIZE:
1802     return MONTHCAL_Size(hwnd, (int)SLOWORD(lParam), (int)SHIWORD(lParam));
1803
1804   case WM_CREATE:
1805     return MONTHCAL_Create(hwnd, wParam, lParam);
1806
1807   case WM_TIMER:
1808     return MONTHCAL_Timer(hwnd, wParam, lParam);
1809
1810   case WM_DESTROY:
1811     return MONTHCAL_Destroy(hwnd, wParam, lParam);
1812
1813   default:
1814     if(uMsg >= WM_USER)
1815       ERR( "unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
1816     return DefWindowProcA(hwnd, uMsg, wParam, lParam);
1817   }
1818   return 0;
1819 }
1820
1821
1822 void
1823 MONTHCAL_Register(void)
1824 {
1825   WNDCLASSA wndClass;
1826
1827   ZeroMemory(&wndClass, sizeof(WNDCLASSA));
1828   wndClass.style         = CS_GLOBALCLASS;
1829   wndClass.lpfnWndProc   = (WNDPROC)MONTHCAL_WindowProc;
1830   wndClass.cbClsExtra    = 0;
1831   wndClass.cbWndExtra    = sizeof(MONTHCAL_INFO *);
1832   wndClass.hCursor       = LoadCursorA(0, IDC_ARROWA);
1833   wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
1834   wndClass.lpszClassName = MONTHCAL_CLASSA;
1835  
1836   RegisterClassA(&wndClass);
1837 }
1838
1839
1840 void
1841 MONTHCAL_Unregister(void)
1842 {
1843     UnregisterClassA(MONTHCAL_CLASSA, (HINSTANCE)NULL);
1844 }