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