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