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