comctl32: Use user32 control names from public header constants instead of defining...
[wine] / dlls / comctl32 / monthcal.c
1 /*
2  * Month calendar control
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  * Copyright 2009 Nikolay Sivov
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24  *
25  * NOTE
26  * 
27  * This code was audited for completeness against the documented features
28  * of Comctl32.dll version 6.0 on Oct. 20, 2004, by Dimitrie O. Paun.
29  * 
30  * Unless otherwise noted, we believe this code to be complete, as per
31  * the specification mentioned above.
32  * If you discover missing features, or bugs, please note them below.
33  * 
34  * TODO:
35  *    -- MCM_[GS]ETUNICODEFORMAT
36  *    -- MONTHCAL_GetMonthRange
37  *    -- handle resources better (doesn't work now); 
38  *    -- take care of internationalization.
39  *    -- keyboard handling.
40  *    -- search for FIXME
41  */
42
43 #include <math.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #include "windef.h"
50 #include "winbase.h"
51 #include "wingdi.h"
52 #include "winuser.h"
53 #include "winnls.h"
54 #include "commctrl.h"
55 #include "comctl32.h"
56 #include "uxtheme.h"
57 #include "tmschema.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_PREVNEXTMONTHDELAY   350     /* when continuously pressing `next/prev
68                                            month', wait 500 ms before going
69                                            to the next/prev month */
70 #define MC_TODAYUPDATEDELAY 120000 /* time between today check for update (2 min) */
71
72 #define MC_PREVNEXTMONTHTIMER   1       /* Timer ID's */
73 #define MC_TODAYUPDATETIMER     2
74
75 #define countof(arr) (sizeof(arr)/sizeof(arr[0]))
76
77 /* convert from days to 100 nanoseconds unit - used as FILETIME unit */
78 #define DAYSTO100NSECS(days) (((ULONGLONG)(days))*24*60*60*10000000)
79
80 typedef struct
81 {
82     HWND        hwndSelf;
83     DWORD       dwStyle; /* cached GWL_STYLE */
84     COLORREF    bk;
85     COLORREF    txt;
86     COLORREF    titlebk;
87     COLORREF    titletxt;
88     COLORREF    monthbk;
89     COLORREF    trailingtxt;
90     HFONT       hFont;
91     HFONT       hBoldFont;
92     int         textHeight;
93     int         textWidth;
94     int         height_increment;
95     int         width_increment;
96     int         firstDayplace; /* place of the first day of the current month */
97     INT         delta;  /* scroll rate; # of months that the */
98                         /* control moves when user clicks a scroll button */
99     int         visible;        /* # of months visible */
100     int         firstDay;       /* Start month calendar with firstDay's day,
101                                    stored in SYSTEMTIME format */
102     BOOL        firstDaySet;    /* first week day differs from locale defined */
103
104     int         monthRange;
105     MONTHDAYSTATE *monthdayState;
106     SYSTEMTIME  todaysDate;
107     BOOL        todaySet;       /* Today was forced with MCM_SETTODAY */
108     int         status;         /* See MC_SEL flags */
109     SYSTEMTIME  firstSel;       /* first selected day */
110     INT         maxSelCount;
111     SYSTEMTIME  minSel;
112     SYSTEMTIME  maxSel;
113     SYSTEMTIME  curSel;         /* contains currently selected year, month and day */
114     SYSTEMTIME  focusedSel;     /* date currently focused with mouse movement */
115     DWORD       rangeValid;
116     SYSTEMTIME  minDate;
117     SYSTEMTIME  maxDate;
118
119     RECT title;         /* rect for the header above the calendar */
120     RECT titlebtnnext;  /* the `next month' button in the header */
121     RECT titlebtnprev;  /* the `prev month' button in the header */
122     RECT titlemonth;    /* the `month name' txt in the header */
123     RECT titleyear;     /* the `year number' txt in the header */
124     RECT wdays;         /* week days at top */
125     RECT days;          /* calendar area */
126     RECT weeknums;      /* week numbers at left side */
127     RECT todayrect;     /* `today: xx/xx/xx' text rect */
128     HWND hwndNotify;    /* Window to receive the notifications */
129     HWND hWndYearEdit;  /* Window Handle of edit box to handle years */
130     HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
131 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
132
133
134 /* Offsets of days in the week to the weekday of january 1 in a leap year */
135 static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
136
137 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
138
139 /* empty SYSTEMTIME const */
140 static const SYSTEMTIME st_null;
141 /* valid date limits */
142 static const SYSTEMTIME max_allowed_date = { .wYear = 9999, .wMonth = 12, .wDay = 31 };
143 static const SYSTEMTIME min_allowed_date = { .wYear = 1752, .wMonth = 9,  .wDay = 14 };
144
145
146 #define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0))
147
148 /* helper functions  */
149
150 /* send a single MCN_SELCHANGE notification */
151 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
152 {
153     NMSELCHANGE nmsc;
154
155     nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
156     nmsc.nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
157     nmsc.nmhdr.code     = MCN_SELCHANGE;
158     nmsc.stSelStart     = infoPtr->minSel;
159     nmsc.stSelEnd       = infoPtr->maxSel;
160     SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
161 }
162
163 /* send a single MCN_SELECT notification */
164 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
165 {
166     NMSELCHANGE nmsc;
167
168     nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
169     nmsc.nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
170     nmsc.nmhdr.code     = MCN_SELECT;
171     nmsc.stSelStart     = infoPtr->minSel;
172     nmsc.stSelEnd       = infoPtr->maxSel;
173
174     SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
175 }
176
177 /* returns the number of days in any given month, checking for leap days */
178 /* january is 1, december is 12 */
179 int MONTHCAL_MonthLength(int month, int year)
180 {
181   const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0};
182   /* Wrap around, this eases handling. Getting length only we shouldn't care
183      about year change here cause January and December have
184      the same day quantity */
185   if(month == 0)
186     month = 12;
187   else if(month == 13)
188     month = 1;
189
190   /* if we have a leap year add 1 day to February */
191   /* a leap year is a year either divisible by 400 */
192   /* or divisible by 4 and not by 100 */
193   if(month == 2) { /* February */
194     return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
195      (year%4 == 0)) ? 1 : 0);
196   }
197   else {
198     return mdays[month - 1];
199   }
200 }
201
202 /* compares timestamps using date part only */
203 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
204 {
205   return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
206          (first->wDay  == second->wDay);
207 }
208
209 /* make sure that date fields are valid */
210 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
211 {
212   if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
213   if(time->wDayOfWeek > 6) return FALSE;
214   if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear))
215           return FALSE;
216
217   return TRUE;
218 }
219
220 /* Compares two dates in SYSTEMTIME format
221  *
222  * PARAMETERS
223  *
224  *  [I] first  : pointer to valid first date data to compare
225  *  [I] second : pointer to valid second date data to compare
226  *
227  * RETURN VALUE
228  *
229  *  -1 : first <  second
230  *   0 : first == second
231  *   1 : first >  second
232  *
233  *  Note that no date validation performed, alreadt validated values expected.
234  */
235 static LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
236 {
237   FILETIME ft_first, ft_second;
238
239   SystemTimeToFileTime(first, &ft_first);
240   SystemTimeToFileTime(second, &ft_second);
241
242   return CompareFileTime(&ft_first, &ft_second);
243 }
244
245 /* Checks largest possible date range and configured one
246  *
247  * PARAMETERS
248  *
249  *  [I] infoPtr : valid pointer to control data
250  *  [I] date    : pointer to valid date data to check
251  *
252  * RETURN VALUE
253  *
254  *  TRUE  - date whithin largest and configured range
255  *  FALSE - date is outside largest or configured range
256  */
257 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date)
258 {
259   if((MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) ||
260      (MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1)) return FALSE;
261
262   if(infoPtr->rangeValid & GDTR_MAX) {
263      if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) return FALSE;
264   }
265
266   if(infoPtr->rangeValid & GDTR_MIN) {
267      if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) return FALSE;
268   }
269
270   return TRUE;
271 }
272
273 /* Checks passed range width with configured maximum selection count
274  *
275  * PARAMETERS
276  *
277  *  [I] infoPtr : valid pointer to control data
278  *  [I] range0  : pointer to valid date data (requested bound)
279  *  [I] range1  : pointer to valid date data (primary bound)
280  *  [O] adjust  : returns adjusted range bound to fit maximum range (optional)
281  *
282  *  Adjust value computed basing on primary bound and current maximum selection
283  *  count. For simple range check (without adjusted value required) (range0, range1)
284  *  relation means nothing.
285  *
286  * RETURN VALUE
287  *
288  *  TRUE  - range is shorter or equal to maximum
289  *  FALSE - range is larger than maximum
290  */
291 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
292                                      const SYSTEMTIME *range0,
293                                      const SYSTEMTIME *range1,
294                                      SYSTEMTIME *adjust)
295 {
296   ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
297   FILETIME ft_range0, ft_range1;
298   LONG cmp;
299
300   SystemTimeToFileTime(range0, &ft_range0);
301   SystemTimeToFileTime(range1, &ft_range1);
302
303   ul_range0.LowPart  = ft_range0.dwLowDateTime;
304   ul_range0.HighPart = ft_range0.dwHighDateTime;
305   ul_range1.LowPart  = ft_range1.dwLowDateTime;
306   ul_range1.HighPart = ft_range1.dwHighDateTime;
307
308   cmp = CompareFileTime(&ft_range0, &ft_range1);
309
310   if(cmp == 1)
311      ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
312   else
313      ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
314
315   if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
316
317      if(adjust) {
318        if(cmp == 1)
319           ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
320        else
321           ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
322
323        ft_range0.dwLowDateTime  = ul_range0.LowPart;
324        ft_range0.dwHighDateTime = ul_range0.HighPart;
325        FileTimeToSystemTime(&ft_range0, adjust);
326      }
327
328      return FALSE;
329   }
330   else return TRUE;
331 }
332
333 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
334    Milliseconds are intentionaly not validated. */
335 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
336 {
337   if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
338     return FALSE;
339   else
340     return TRUE;
341 }
342
343 /* Copies timestamp part only.
344  *
345  * PARAMETERS
346  *
347  *  [I] from : source date
348  *  [O] to   : dest date
349  */
350 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
351 {
352   to->wHour   = from->wHour;
353   to->wMinute = from->wMinute;
354   to->wSecond = from->wSecond;
355 }
356
357 /* Copies date part only.
358  *
359  * PARAMETERS
360  *
361  *  [I] from : source date
362  *  [O] to   : dest date
363  */
364 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
365 {
366   to->wYear  = from->wYear;
367   to->wMonth = from->wMonth;
368   to->wDay   = from->wDay;
369   to->wDayOfWeek = from->wDayOfWeek;
370 }
371
372 /* Note:Depending on DST, this may be offset by a day.
373    Need to find out if we're on a DST place & adjust the clock accordingly.
374    Above function assumes we have a valid data.
375    Valid for year>1752;  1 <= d <= 31, 1 <= m <= 12.
376    0 = Sunday.
377 */
378
379 /* Returns the day in the week
380  *
381  * PARAMETERS
382  *  [i] day : day of month [1, 31]
383  *  [I] month : month number [1, 12]
384  *  [I] year : year value
385  *
386  * RETURN VALUE
387  *   day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
388  */
389 int MONTHCAL_CalculateDayOfWeek(WORD day, WORD month, WORD year)
390 {
391   year-=(month < 3);
392
393   return((year + year/4 - year/100 + year/400 +
394          DayOfWeekTable[month-1] + day ) % 7);
395 }
396
397 /* properly updates date to point on next month */
398 void inline MONTHCAL_GetNextMonth(SYSTEMTIME *date)
399 {
400   if(++date->wMonth > 12)
401   {
402     date->wMonth = 1;
403     date->wYear++;
404   }
405   date->wDayOfWeek = MONTHCAL_CalculateDayOfWeek(date->wDay, date->wMonth,
406                                                  date->wYear);
407 }
408
409 /* properly updates date to point on prev month */
410 void inline MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
411 {
412   if(--date->wMonth < 1)
413   {
414     date->wMonth = 12;
415     date->wYear--;
416   }
417   date->wDayOfWeek = MONTHCAL_CalculateDayOfWeek(date->wDay, date->wMonth,
418                                                  date->wYear);
419 }
420
421 /* From a given point, calculate the row (weekpos), column(daypos)
422    and day in the calendar. day== 0 mean the last day of tha last month
423 */
424 static int MONTHCAL_CalcDayFromPos(const MONTHCAL_INFO *infoPtr, int x, int y,
425                                    int *daypos,int *weekpos)
426 {
427   int retval, firstDay;
428   RECT rcClient;
429
430   GetClientRect(infoPtr->hwndSelf, &rcClient);
431
432   /* if the point is outside the x bounds of the window put
433   it at the boundary */
434   if (x > rcClient.right)
435     x = rcClient.right;
436
437
438   *daypos = (x - infoPtr->days.left ) / infoPtr->width_increment;
439   *weekpos = (y - infoPtr->days.top ) / infoPtr->height_increment;
440
441   firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear)+6 - infoPtr->firstDay)%7;
442   retval = *daypos + (7 * *weekpos) - firstDay;
443   return retval;
444 }
445
446 /* day is the day of the month, 1 == 1st day of the month */
447 /* sets x and y to be the position of the day */
448 /* x == day, y == week where(0,0) == firstDay, 1st week */
449 static void MONTHCAL_CalcDayXY(const MONTHCAL_INFO *infoPtr, int day, int month,
450                                  int *x, int *y)
451 {
452   int firstDay, prevMonth;
453
454   firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear) +6 - infoPtr->firstDay)%7;
455
456   if(month==infoPtr->curSel.wMonth) {
457     *x = (day + firstDay) % 7;
458     *y = (day + firstDay - *x) / 7;
459     return;
460   }
461   if(month < infoPtr->curSel.wMonth) {
462     prevMonth = month - 1;
463     if(prevMonth==0)
464        prevMonth = 12;
465
466     *x = (MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear) - firstDay) % 7;
467     *y = 0;
468     return;
469   }
470
471   *y = MONTHCAL_MonthLength(month, infoPtr->curSel.wYear - 1) / 7;
472   *x = (day + firstDay + MONTHCAL_MonthLength(month,
473        infoPtr->curSel.wYear)) % 7;
474 }
475
476
477 /* x: column(day), y: row(week) */
478 static void MONTHCAL_CalcDayRect(const MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
479 {
480   r->left = infoPtr->days.left + x * infoPtr->width_increment;
481   r->right = r->left + infoPtr->width_increment;
482   r->top  = infoPtr->days.top  + y * infoPtr->height_increment;
483   r->bottom = r->top + infoPtr->textHeight;
484 }
485
486
487 /* sets the RECT struct r to the rectangle around the day and month */
488 /* day is the day value of the month(1 == 1st), month is the month */
489 /* value(january == 1, december == 12) */
490 static inline void MONTHCAL_CalcPosFromDay(const MONTHCAL_INFO *infoPtr,
491                                             int day, int month, RECT *r)
492 {
493   int x, y;
494
495   MONTHCAL_CalcDayXY(infoPtr, day, month, &x, &y);
496   MONTHCAL_CalcDayRect(infoPtr, r, x, y);
497 }
498
499 /* Focused day helper:
500
501    - set focused date to given value;
502    - reset to zero value if NULL passed;
503    - invalidate previous and new day rectangle only if needed.
504
505    Returns TRUE if focused day changed, FALSE otherwise.
506 */
507 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
508 {
509   RECT r;
510
511   if(st)
512   {
513     /* there's nothing to do if it's the same date,
514        mouse move within same date rectangle case */
515     if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
516
517     /* invalidate old focused day */
518     MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->focusedSel.wDay,
519                                      infoPtr->focusedSel.wMonth, &r);
520     InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
521
522     infoPtr->focusedSel = *st;
523   }
524
525   MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->focusedSel.wDay,
526                                    infoPtr->focusedSel.wMonth, &r);
527
528   if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
529     infoPtr->focusedSel = st_null;
530
531   /* on set invalidates new day, on reset clears previous focused day */
532   InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
533
534   return TRUE;
535 }
536
537 /* day is the day in the month(1 == 1st of the month) */
538 /* month is the month value(1 == january, 12 == december) */
539 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month)
540 {
541   HPEN hRedPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
542   HPEN hOldPen2 = SelectObject(hdc, hRedPen);
543   HBRUSH hOldBrush;
544   RECT day_rect;
545
546   MONTHCAL_CalcPosFromDay(infoPtr, day, month, &day_rect);
547
548   hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
549   Rectangle(hdc, day_rect.left, day_rect.top, day_rect.right, day_rect.bottom);
550
551   SelectObject(hdc, hOldBrush);
552   DeleteObject(hRedPen);
553   SelectObject(hdc, hOldPen2);
554 }
555
556 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month,
557                              int x, int y, int bold)
558 {
559   static const WCHAR fmtW[] = { '%','d',0 };
560   WCHAR buf[10];
561   RECT r;
562   static BOOL haveBoldFont, haveSelectedDay = FALSE;
563   HBRUSH hbr;
564   COLORREF oldCol = 0;
565   COLORREF oldBk = 0;
566
567   wsprintfW(buf, fmtW, day);
568
569 /* No need to check styles: when selection is not valid, it is set to zero.
570  * 1<day<31, so everything is OK.
571  */
572
573   MONTHCAL_CalcDayRect(infoPtr, &r, x, y);
574
575   if((day>=infoPtr->minSel.wDay) && (day<=infoPtr->maxSel.wDay)
576        && (month == infoPtr->curSel.wMonth)) {
577     RECT r2;
578
579     TRACE("%d %d %d\n",day, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
580     TRACE("%s\n", wine_dbgstr_rect(&r));
581     oldCol = SetTextColor(hdc, infoPtr->monthbk);
582     oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
583     hbr = GetSysColorBrush(COLOR_HIGHLIGHT);
584     FillRect(hdc, &r, hbr);
585
586     /* FIXME: this may need to be changed now b/c of the other
587         drawing changes 11/3/99 CMM */
588     r2.left   = r.left - 0.25 * infoPtr->textWidth;
589     r2.top    = r.top;
590     r2.right  = r.left + 0.5 * infoPtr->textWidth;
591     r2.bottom = r.bottom;
592     if(haveSelectedDay) FillRect(hdc, &r2, hbr);
593       haveSelectedDay = TRUE;
594   } else {
595     haveSelectedDay = FALSE;
596   }
597
598   /* need to add some code for multiple selections */
599
600   if((bold) &&(!haveBoldFont)) {
601     SelectObject(hdc, infoPtr->hBoldFont);
602     haveBoldFont = TRUE;
603   }
604   if((!bold) &&(haveBoldFont)) {
605     SelectObject(hdc, infoPtr->hFont);
606     haveBoldFont = FALSE;
607   }
608
609   SetBkMode(hdc,TRANSPARENT);
610   DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
611
612   if(haveSelectedDay) {
613     SetTextColor(hdc, oldCol);
614     SetBkColor(hdc, oldBk);
615   }
616 }
617
618
619 static void paint_button (MONTHCAL_INFO *infoPtr, HDC hdc, BOOL btnNext)
620 {
621     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
622     RECT *r = btnNext ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
623     BOOL pressed = btnNext ? (infoPtr->status & MC_NEXTPRESSED) :
624                              (infoPtr->status & MC_PREVPRESSED);
625     if (theme)
626     {
627         static const int states[] = {
628             /* Prev button */
629             ABS_LEFTNORMAL,  ABS_LEFTPRESSED,  ABS_LEFTDISABLED,
630             /* Next button */
631             ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
632         };
633         int stateNum = btnNext ? 3 : 0;
634         if (pressed)
635             stateNum += 1;
636         else
637         {
638             if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
639         }
640         DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
641     }
642     else
643     {
644         int style = btnNext ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
645         if (pressed)
646             style |= DFCS_PUSHED;
647         else
648         {
649             if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
650         }
651         
652         DrawFrameControl(hdc, r, DFC_SCROLL, style);
653     }
654 }
655
656
657 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
658 {
659   static const WCHAR fmt_monthW[] = { '%','s',' ','%','l','d',0 };
660   RECT *title=&infoPtr->title;
661   RECT *titlemonth=&infoPtr->titlemonth;
662   RECT *titleyear=&infoPtr->titleyear;
663   RECT dayrect;
664   int i, j, m, mask, day, firstDay, prevMonth;
665   int textHeight = infoPtr->textHeight;
666   SIZE size;
667   HBRUSH hbr;
668   HFONT currentFont;
669   WCHAR buf[20];
670   WCHAR buf1[20];
671   WCHAR buf2[32];
672   COLORREF oldTextColor, oldBkColor;
673   RECT rcTemp;
674   RECT rcDay; /* used in MONTHCAL_CalcDayRect() */
675   int startofprescal;
676
677   oldTextColor = SetTextColor(hdc, comctl32_color.clrWindowText);
678
679   /* fill background */
680   hbr = CreateSolidBrush (infoPtr->bk);
681   FillRect(hdc, &ps->rcPaint, hbr);
682   DeleteObject(hbr);
683
684   /* draw header */
685   if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
686   {
687     hbr =  CreateSolidBrush(infoPtr->titlebk);
688     FillRect(hdc, title, hbr);
689     DeleteObject(hbr);
690   }
691
692   /* if the previous button is pressed draw it depressed */
693   if(IntersectRect(&rcTemp, &(ps->rcPaint), &infoPtr->titlebtnprev))
694     paint_button(infoPtr, hdc, FALSE);
695
696   /* if next button is depressed draw it depressed */
697   if(IntersectRect(&rcTemp, &(ps->rcPaint), &infoPtr->titlebtnnext))
698     paint_button(infoPtr, hdc, TRUE);
699
700   oldBkColor = SetBkColor(hdc, infoPtr->titlebk);
701   SetTextColor(hdc, infoPtr->titletxt);
702   currentFont = SelectObject(hdc, infoPtr->hBoldFont);
703
704   GetLocaleInfoW( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+infoPtr->curSel.wMonth -1,
705                   buf1,countof(buf1));
706   wsprintfW(buf, fmt_monthW, buf1, infoPtr->curSel.wYear);
707
708   if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
709   {
710     DrawTextW(hdc, buf, strlenW(buf), title,
711                         DT_CENTER | DT_VCENTER | DT_SINGLELINE);
712   }
713
714 /* titlemonth left/right contained rect for whole titletxt('June  1999')
715   * MCM_HitTestInfo wants month & year rects, so prepare these now.
716   *(no, we can't draw them separately; the whole text is centered)
717   */
718   GetTextExtentPoint32W(hdc, buf, strlenW(buf), &size);
719   titlemonth->left = title->right / 2 + title->left / 2 - size.cx / 2;
720   titleyear->right = title->right / 2 + title->left / 2 + size.cx / 2;
721   GetTextExtentPoint32W(hdc, buf1, strlenW(buf1), &size);
722   titlemonth->right = titlemonth->left + size.cx;
723   titleyear->left = titlemonth->right;
724
725   /* draw month area */
726   rcTemp.top=infoPtr->wdays.top;
727   rcTemp.left=infoPtr->wdays.left;
728   rcTemp.bottom=infoPtr->todayrect.bottom;
729   rcTemp.right =infoPtr->todayrect.right;
730   if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcTemp))
731   {
732     hbr =  CreateSolidBrush(infoPtr->monthbk);
733     FillRect(hdc, &rcTemp, hbr);
734     DeleteObject(hbr);
735   }
736
737 /* draw line under day abbreviations */
738
739   MoveToEx(hdc, infoPtr->days.left + 3, title->bottom + textHeight + 1, NULL);
740   LineTo(hdc, infoPtr->days.right - 3, title->bottom + textHeight + 1);
741
742   prevMonth = infoPtr->curSel.wMonth - 1;
743   if(prevMonth == 0) /* if curSel.wMonth is january(1) prevMonth is */
744     prevMonth = 12;    /* december(12) of the previous year */
745
746   infoPtr->wdays.left   = infoPtr->days.left   = infoPtr->weeknums.right;
747
748   /* draw day abbreviations */
749   SelectObject(hdc, infoPtr->hFont);
750   SetBkColor(hdc, infoPtr->monthbk);
751   SetTextColor(hdc, infoPtr->trailingtxt);
752
753   /* rectangle to draw a single day abbreviation within */
754   dayrect = infoPtr->wdays;
755   dayrect.right = dayrect.left + infoPtr->width_increment;
756
757   i = infoPtr->firstDay;
758
759   for(j = 0; j < 7; j++) {
760     GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
761     DrawTextW(hdc, buf, strlenW(buf), &dayrect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
762     dayrect.left  += infoPtr->width_increment;
763     dayrect.right += infoPtr->width_increment;
764   }
765
766   /* draw day numbers; first, the previous month */
767   firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->curSel.wMonth, infoPtr->curSel.wYear);
768
769   day = MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear) +
770     (infoPtr->firstDay - firstDay)%7 + 1;
771
772   if (day > MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear))
773     day -= 7;
774   startofprescal = day;
775   mask = 1<<(day-1);
776
777   i = 0;
778   m = 0;
779   while(day <= MONTHCAL_MonthLength(prevMonth, infoPtr->curSel.wYear)) {
780     MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
781     if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
782     {
783       MONTHCAL_DrawDay(infoPtr, hdc, day, prevMonth, i, 0,
784           infoPtr->monthdayState[m] & mask);
785     }
786
787     mask<<=1;
788     day++;
789     i++;
790   }
791
792 /* draw `current' month  */
793
794   day = 1; /* start at the beginning of the current month */
795
796   infoPtr->firstDayplace = i;
797   SetTextColor(hdc, infoPtr->txt);
798   m++;
799   mask = 1;
800
801   /* draw the first week of the current month */
802   while(i<7) {
803     MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
804     if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
805     {
806       MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth, i, 0,
807         infoPtr->monthdayState[m] & mask);
808     }
809
810     mask<<=1;
811     day++;
812     i++;
813   }
814
815   j = 1; /* move to the 2nd week of the current month */
816   i = 0; /* move back to sunday */
817   while(day <= MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear)) {
818     MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
819     if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
820     {
821       MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth, i, j,
822           infoPtr->monthdayState[m] & mask);
823     }
824     mask<<=1;
825     day++;
826     i++;
827     if(i>6) { /* past saturday, goto the next weeks sunday */
828       i = 0;
829       j++;
830     }
831   }
832
833 /*  draw `next' month */
834
835   day = 1; /* start at the first day of the next month */
836   m++;
837   mask = 1;
838
839   SetTextColor(hdc, infoPtr->trailingtxt);
840   while((i<7) &&(j<6)) {
841     MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
842     if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
843     {
844       MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->curSel.wMonth + 1, i, j,
845                 infoPtr->monthdayState[m] & mask);
846     }
847
848     mask<<=1;
849     day++;
850     i++;
851     if(i==7) { /* past saturday, go to next week's sunday */
852       i = 0;
853       j++;
854     }
855   }
856   SetTextColor(hdc, infoPtr->txt);
857
858   /* draw today mark rectangle */
859   if((infoPtr->curSel.wMonth == infoPtr->todaysDate.wMonth) &&
860      (infoPtr->curSel.wYear == infoPtr->todaysDate.wYear) &&
861     !(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
862   {
863     MONTHCAL_CircleDay(infoPtr, hdc, infoPtr->todaysDate.wDay, infoPtr->todaysDate.wMonth);
864   }
865
866   /* draw focused day */
867   if(!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
868   {
869     MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->focusedSel.wDay,
870                                      infoPtr->focusedSel.wMonth, &rcDay);
871
872     DrawFocusRect(hdc, &rcDay);
873   }
874
875   /* draw `today' date if style allows it, and draw a circle before today's
876    * date if necessary */
877   if(!(infoPtr->dwStyle & MCS_NOTODAY))  {
878     static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
879     static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
880     RECT rtoday;
881
882     if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))  {
883       /*day is the number of days from nextmonth we put on the calendar */
884       MONTHCAL_CircleDay(infoPtr, hdc,
885                          day+MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear),
886                          infoPtr->curSel.wMonth);
887     }
888     if (!LoadStringW(COMCTL32_hModule, IDM_TODAY, buf1, countof(buf1)))
889     {
890         WARN("Can't load resource\n");
891         strcpyW(buf1, todayW);
892     }
893     MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
894     GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
895                                                         buf2, countof(buf2));
896     wsprintfW(buf, fmt_todayW, buf1, buf2);
897     SelectObject(hdc, infoPtr->hBoldFont);
898
899     DrawTextW(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
900     if(IntersectRect(&rcTemp, &(ps->rcPaint), &rtoday))
901     {
902       DrawTextW(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
903     }
904     SelectObject(hdc, infoPtr->hFont);
905   }
906
907   /* eventually draw week numbers */
908   if(infoPtr->dwStyle & MCS_WEEKNUMBERS) {
909     static const WCHAR fmt_weekW[] = { '%','d',0 }; /* week numbers format */
910     int mindays, weeknum, weeknum1;
911
912     /* Rules what week to call the first week of a new year:
913        LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
914        The week containing Jan 1 is the first week of year
915        LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
916        First week of year must contain 4 days of the new year
917        LOCALE_IFIRSTWEEKOFYEAR == 1  (what contries?)
918        The first week of the year must contain only days of the new year
919     */
920     GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
921     weeknum = atoiW(buf);
922     switch (weeknum)
923     {
924       case 1: mindays = 6;
925         break;
926       case 2: mindays = 3;
927         break;
928       case 0:
929       default:
930         mindays = 0;
931     }
932     if (infoPtr->curSel.wMonth < 2)
933     {
934         /* calculate all those exceptions for january */
935         weeknum1 = MONTHCAL_CalculateDayOfWeek(1, 1, infoPtr->curSel.wYear);
936         if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
937             weeknum = 1;
938         else
939         {
940             weeknum = 0;
941             for(i = 0; i < 11; i++)
942               weeknum += MONTHCAL_MonthLength(i+1, infoPtr->curSel.wYear - 1);
943
944             weeknum += startofprescal + 7;
945             weeknum /= 7;
946             weeknum1 = MONTHCAL_CalculateDayOfWeek(1, 1, infoPtr->curSel.wYear - 1);
947             if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
948         }
949     }
950     else
951     {
952         weeknum = 0;
953         for(i = 0; i < prevMonth - 1; i++)
954           weeknum += MONTHCAL_MonthLength(i+1, infoPtr->curSel.wYear);
955
956         weeknum += startofprescal + 7;
957         weeknum /= 7;
958         weeknum1 = MONTHCAL_CalculateDayOfWeek(1, 1, infoPtr->curSel.wYear);
959         if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
960     }
961
962     dayrect = infoPtr->weeknums;
963     dayrect.bottom = dayrect.top + infoPtr->height_increment;
964
965     for(i = 0; i < 6; i++) {
966       if((i == 0) && (weeknum > 50))
967       {
968           wsprintfW(buf, fmt_weekW, weeknum);
969           weeknum = 0;
970       }
971       else if((i == 5) && (weeknum > 47))
972       {
973           wsprintfW(buf, fmt_weekW, 1);
974       }
975       else
976           wsprintfW(buf, fmt_weekW, weeknum + i);
977
978       DrawTextW(hdc, buf, -1, &dayrect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
979       dayrect.top    += infoPtr->height_increment;
980       dayrect.bottom += infoPtr->height_increment;
981     }
982
983     MoveToEx(hdc, infoPtr->weeknums.right, infoPtr->weeknums.top + 3 , NULL);
984     LineTo(hdc,   infoPtr->weeknums.right, infoPtr->weeknums.bottom);
985   }
986
987   /* currentFont was font at entering Refresh */
988   SetBkColor(hdc, oldBkColor);
989   SelectObject(hdc, currentFont);
990   SetTextColor(hdc, oldTextColor);
991 }
992
993
994 static LRESULT
995 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, LPRECT lpRect)
996 {
997   TRACE("rect %p\n", lpRect);
998
999   if(!lpRect) return FALSE;
1000
1001   lpRect->left   = infoPtr->title.left;
1002   lpRect->top    = infoPtr->title.top;
1003   lpRect->right  = infoPtr->title.right;
1004   lpRect->bottom = infoPtr->todayrect.bottom;
1005
1006   AdjustWindowRect(lpRect, infoPtr->dwStyle, FALSE);
1007
1008   /* minimal rectangle is zero based */
1009   OffsetRect(lpRect, -lpRect->left, -lpRect->top);
1010
1011   TRACE("%s\n", wine_dbgstr_rect(lpRect));
1012
1013   return TRUE;
1014 }
1015
1016
1017 static LRESULT
1018 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, INT index)
1019 {
1020   TRACE("\n");
1021
1022   switch(index) {
1023     case MCSC_BACKGROUND:
1024       return infoPtr->bk;
1025     case MCSC_TEXT:
1026       return infoPtr->txt;
1027     case MCSC_TITLEBK:
1028       return infoPtr->titlebk;
1029     case MCSC_TITLETEXT:
1030       return infoPtr->titletxt;
1031     case MCSC_MONTHBK:
1032       return infoPtr->monthbk;
1033     case MCSC_TRAILINGTEXT:
1034       return infoPtr->trailingtxt;
1035   }
1036
1037   return -1;
1038 }
1039
1040
1041 static LRESULT
1042 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, INT index, COLORREF color)
1043 {
1044   COLORREF prev = -1;
1045
1046   TRACE("%d: color %08x\n", index, color);
1047
1048   switch(index) {
1049     case MCSC_BACKGROUND:
1050       prev = infoPtr->bk;
1051       infoPtr->bk = color;
1052       break;
1053     case MCSC_TEXT:
1054       prev = infoPtr->txt;
1055       infoPtr->txt = color;
1056       break;
1057     case MCSC_TITLEBK:
1058       prev = infoPtr->titlebk;
1059       infoPtr->titlebk = color;
1060       break;
1061     case MCSC_TITLETEXT:
1062       prev=infoPtr->titletxt;
1063       infoPtr->titletxt = color;
1064       break;
1065     case MCSC_MONTHBK:
1066       prev = infoPtr->monthbk;
1067       infoPtr->monthbk = color;
1068       break;
1069     case MCSC_TRAILINGTEXT:
1070       prev = infoPtr->trailingtxt;
1071       infoPtr->trailingtxt = color;
1072       break;
1073   }
1074
1075   InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND ? TRUE : FALSE);
1076   return prev;
1077 }
1078
1079
1080 static LRESULT
1081 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1082 {
1083   TRACE("\n");
1084
1085   if(infoPtr->delta)
1086     return infoPtr->delta;
1087   else
1088     return infoPtr->visible;
1089 }
1090
1091
1092 static LRESULT
1093 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1094 {
1095   INT prev = infoPtr->delta;
1096
1097   TRACE("delta %d\n", delta);
1098
1099   infoPtr->delta = delta;
1100   return prev;
1101 }
1102
1103
1104 static inline LRESULT
1105 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1106 {
1107   int day;
1108
1109   /* convert from SYSTEMTIME to locale format */
1110   day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1111
1112   return MAKELONG(day, infoPtr->firstDaySet);
1113 }
1114
1115
1116 /* Sets the first day of the week that will appear in the control
1117  *
1118  *
1119  * PARAMETERS:
1120  *  [I] infoPtr : valid pointer to control data
1121  *  [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1122  *
1123  *
1124  * RETURN VALUE:
1125  *  Low word contains previous first day,
1126  *  high word indicates was first day forced with this message before or is
1127  *  locale difined (TRUE - was forced, FALSE - wasn't).
1128  *
1129  * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1130  * FIXME: we need more error checking here
1131  */
1132 static LRESULT
1133 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1134 {
1135   LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1136   int new_day;
1137
1138   TRACE("%d\n", day);
1139
1140   if(day == -1)
1141   {
1142     WCHAR buf[80];
1143
1144     GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1145     TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1146
1147     new_day = atoiW(buf);
1148
1149     infoPtr->firstDaySet = FALSE;
1150   }
1151   else if(day >= 7)
1152   {
1153     new_day = 6; /* max first day allowed */
1154     infoPtr->firstDaySet = TRUE;
1155   }
1156   else
1157   {
1158     /* Native behaviour for that case is broken: invalid date number >31
1159        got displayed at (0,0) position, current month starts always from
1160        (1,0) position. Should be implemnted here as well. */
1161     if (day < -1)
1162       FIXME("No bug compatibility for day=%d\n", day);
1163
1164     new_day = day;
1165     infoPtr->firstDaySet = TRUE;
1166   }
1167
1168   /* convert from locale to SYSTEMTIME format */
1169   infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1170
1171   InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1172
1173   return prev;
1174 }
1175
1176
1177 static LRESULT
1178 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
1179 {
1180   TRACE("\n");
1181
1182   if(st)
1183   {
1184     if(flag == GMR_VISIBLE)
1185     {
1186         /*FIXME: currently multicalendar feature isn't implelented, so entirely
1187                  visible month is current */
1188         st[0] = st[1] = infoPtr->curSel;
1189
1190         st[0].wDay = 1;
1191         st[0].wDayOfWeek = MONTHCAL_CalculateDayOfWeek(1, st[0].wMonth, st[0].wYear);
1192
1193         st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
1194         st[1].wDayOfWeek = MONTHCAL_CalculateDayOfWeek(st[1].wDay, st[1].wMonth,
1195                                                        st[1].wYear);
1196     }
1197     else
1198         FIXME("only GMR_VISIBLE flag supported, got %d\n", flag);
1199   }
1200
1201   return infoPtr->monthRange;
1202 }
1203
1204
1205 static LRESULT
1206 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1207 {
1208   return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1209 }
1210
1211
1212 static LRESULT
1213 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1214 {
1215     FILETIME ft_min, ft_max;
1216
1217     TRACE("%x %p\n", limits, range);
1218
1219     if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1220         (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1221         return FALSE;
1222
1223     if (limits & GDTR_MIN)
1224     {
1225         if (!MONTHCAL_ValidateTime(&range[0]))
1226             MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1227
1228         infoPtr->minDate = range[0];
1229         infoPtr->rangeValid |= GDTR_MIN;
1230     }
1231     if (limits & GDTR_MAX)
1232     {
1233         if (!MONTHCAL_ValidateTime(&range[1]))
1234             MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1235
1236         infoPtr->maxDate = range[1];
1237         infoPtr->rangeValid |= GDTR_MAX;
1238     }
1239
1240     /* Only one limit set - we are done */
1241     if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1242         return TRUE;
1243
1244     SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1245     SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1246
1247     if (CompareFileTime(&ft_min, &ft_max) >= 0)
1248     {
1249         if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1250         {
1251             /* Native swaps limits only when both limits are being set. */
1252             SYSTEMTIME st_tmp = infoPtr->minDate;
1253             infoPtr->minDate  = infoPtr->maxDate;
1254             infoPtr->maxDate  = st_tmp;
1255         }
1256         else
1257         {
1258             /* reset the other limit */
1259             if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1260             if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1261             infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1262         }
1263     }
1264
1265     return TRUE;
1266 }
1267
1268
1269 static LRESULT
1270 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1271 {
1272   TRACE("%p\n", range);
1273
1274   if(!range) return FALSE;
1275
1276   range[1] = infoPtr->maxDate;
1277   range[0] = infoPtr->minDate;
1278
1279   return infoPtr->rangeValid;
1280 }
1281
1282
1283 static LRESULT
1284 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1285 {
1286   int i;
1287
1288   TRACE("%d %p\n", months, states);
1289   if(months != infoPtr->monthRange) return 0;
1290
1291   for(i = 0; i < months; i++)
1292     infoPtr->monthdayState[i] = states[i];
1293
1294   return 1;
1295 }
1296
1297 static LRESULT
1298 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1299 {
1300   TRACE("%p\n", curSel);
1301   if(!curSel) return FALSE;
1302   if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1303
1304   *curSel = infoPtr->curSel;
1305   TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1306   return TRUE;
1307 }
1308
1309 /* FIXME: if the specified date is not visible, make it visible */
1310 static LRESULT
1311 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1312 {
1313   TRACE("%p\n", curSel);
1314   if(!curSel) return FALSE;
1315   if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1316
1317   if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1318   /* exit earlier if selection equals current */
1319   if (MONTHCAL_IsDateEqual(&infoPtr->curSel, curSel)) return TRUE;
1320
1321   if(!MONTHCAL_IsDateInValidRange(infoPtr, curSel)) return FALSE;
1322
1323   infoPtr->minSel = *curSel;
1324   infoPtr->maxSel = *curSel;
1325
1326   infoPtr->curSel = *curSel;
1327
1328   /* FIXME: it's possible to reduce rectangle here */
1329   InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1330
1331   return TRUE;
1332 }
1333
1334
1335 static LRESULT
1336 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1337 {
1338   return infoPtr->maxSelCount;
1339 }
1340
1341
1342 static LRESULT
1343 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1344 {
1345   TRACE("%d\n", max);
1346
1347   if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1348   if(max <= 0) return FALSE;
1349
1350   infoPtr->maxSelCount = max;
1351
1352   return TRUE;
1353 }
1354
1355
1356 static LRESULT
1357 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1358 {
1359   TRACE("%p\n", range);
1360
1361   if(!range) return FALSE;
1362
1363   if(infoPtr->dwStyle & MCS_MULTISELECT)
1364   {
1365     range[1] = infoPtr->maxSel;
1366     range[0] = infoPtr->minSel;
1367     TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1368     return TRUE;
1369   }
1370
1371   return FALSE;
1372 }
1373
1374
1375 static LRESULT
1376 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1377 {
1378   TRACE("%p\n", range);
1379
1380   if(!range) return FALSE;
1381
1382   if(infoPtr->dwStyle & MCS_MULTISELECT)
1383   {
1384     SYSTEMTIME old_range[2];
1385
1386     /* adjust timestamps */
1387     if(!MONTHCAL_ValidateTime(&range[0]))
1388       MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1389     if(!MONTHCAL_ValidateTime(&range[1]))
1390       MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1391
1392     /* maximum range exceeded */
1393     if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1394
1395     old_range[0] = infoPtr->minSel;
1396     old_range[1] = infoPtr->maxSel;
1397
1398     /* swap if min > max */
1399     if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1400     {
1401       infoPtr->minSel = range[0];
1402       infoPtr->maxSel = range[1];
1403     }
1404     else
1405     {
1406       infoPtr->minSel = range[1];
1407       infoPtr->maxSel = range[0];
1408     }
1409
1410     /* update day of week */
1411     infoPtr->minSel.wDayOfWeek =
1412             MONTHCAL_CalculateDayOfWeek(infoPtr->minSel.wDay, infoPtr->minSel.wMonth,
1413                                                               infoPtr->minSel.wYear);
1414     infoPtr->maxSel.wDayOfWeek =
1415             MONTHCAL_CalculateDayOfWeek(infoPtr->maxSel.wDay, infoPtr->maxSel.wMonth,
1416                                                               infoPtr->maxSel.wYear);
1417
1418     /* redraw if bounds changed */
1419     /* FIXME: no actual need to redraw everything */
1420     if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1421        !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1422     {
1423        InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1424     }
1425
1426     TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1427     return TRUE;
1428   }
1429
1430   return FALSE;
1431 }
1432
1433
1434 static LRESULT
1435 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1436 {
1437   TRACE("%p\n", today);
1438
1439   if(!today) return FALSE;
1440   *today = infoPtr->todaysDate;
1441   return TRUE;
1442 }
1443
1444 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1445  *
1446  * RETURN VALUE
1447  *
1448  *  TRUE  - today date changed
1449  *  FALSE - today date isn't changed
1450  */
1451 static BOOL
1452 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1453 {
1454   RECT new_r, old_r;
1455
1456   if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1457
1458   MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->todaysDate.wDay,
1459                                    infoPtr->todaysDate.wMonth, &old_r);
1460   MONTHCAL_CalcPosFromDay(infoPtr, today->wDay, today->wMonth, &new_r);
1461
1462   infoPtr->todaysDate = *today;
1463
1464   /* only two days need redrawing */
1465   InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1466   InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1467   return TRUE;
1468 }
1469
1470 /* MCM_SETTODAT handler */
1471 static LRESULT
1472 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1473 {
1474   TRACE("%p\n", today);
1475
1476   if(!today) return FALSE;
1477
1478   /* remember if date was set successfully */
1479   if(MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1480
1481   return TRUE;
1482 }
1483
1484 static LRESULT
1485 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1486 {
1487   UINT x,y;
1488   DWORD retval;
1489   int day,wday,wnum;
1490
1491   if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1492
1493   x = lpht->pt.x;
1494   y = lpht->pt.y;
1495
1496   ZeroMemory(&lpht->st, sizeof(lpht->st));
1497
1498   /* Comment in for debugging...
1499   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,
1500         infoPtr->wdays.left, infoPtr->wdays.right,
1501         infoPtr->wdays.top, infoPtr->wdays.bottom,
1502         infoPtr->days.left, infoPtr->days.right,
1503         infoPtr->days.top, infoPtr->days.bottom,
1504         infoPtr->todayrect.left, infoPtr->todayrect.right,
1505         infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1506         infoPtr->weeknums.left, infoPtr->weeknums.right,
1507         infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1508   */
1509
1510   /* are we in the header? */
1511
1512   if(PtInRect(&infoPtr->title, lpht->pt)) {
1513     if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1514       retval = MCHT_TITLEBTNPREV;
1515       goto done;
1516     }
1517     if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1518       retval = MCHT_TITLEBTNNEXT;
1519       goto done;
1520     }
1521     if(PtInRect(&infoPtr->titlemonth, lpht->pt)) {
1522       retval = MCHT_TITLEMONTH;
1523       goto done;
1524     }
1525     if(PtInRect(&infoPtr->titleyear, lpht->pt)) {
1526       retval = MCHT_TITLEYEAR;
1527       goto done;
1528     }
1529
1530     retval = MCHT_TITLE;
1531     goto done;
1532   }
1533
1534   day = MONTHCAL_CalcDayFromPos(infoPtr,x,y,&wday,&wnum);
1535   if(PtInRect(&infoPtr->wdays, lpht->pt)) {
1536     retval = MCHT_CALENDARDAY;
1537     lpht->st.wYear  = infoPtr->curSel.wYear;
1538     lpht->st.wMonth = (day < 1)? infoPtr->curSel.wMonth -1 : infoPtr->curSel.wMonth;
1539     lpht->st.wDay   = (day < 1)?
1540       MONTHCAL_MonthLength(infoPtr->curSel.wMonth-1, infoPtr->curSel.wYear) -day : day;
1541     goto done;
1542   }
1543   if(PtInRect(&infoPtr->weeknums, lpht->pt)) {
1544     retval = MCHT_CALENDARWEEKNUM;
1545     lpht->st.wYear  = infoPtr->curSel.wYear;
1546     lpht->st.wMonth = (day < 1) ? infoPtr->curSel.wMonth -1 :
1547       (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear)) ?
1548       infoPtr->curSel.wMonth +1 :infoPtr->curSel.wMonth;
1549     lpht->st.wDay   = (day < 1 ) ?
1550       MONTHCAL_MonthLength(infoPtr->curSel.wMonth-1,infoPtr->curSel.wYear) -day :
1551       (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear)) ?
1552       day - MONTHCAL_MonthLength(infoPtr->curSel.wMonth,infoPtr->curSel.wYear) : day;
1553     goto done;
1554   }
1555   if(PtInRect(&infoPtr->days, lpht->pt))
1556   {
1557       lpht->st.wYear  = infoPtr->curSel.wYear;
1558       lpht->st.wMonth = infoPtr->curSel.wMonth;
1559       if (day < 1)
1560       {
1561           retval = MCHT_CALENDARDATEPREV;
1562           MONTHCAL_GetPrevMonth(&lpht->st);
1563           lpht->st.wDay = MONTHCAL_MonthLength(lpht->st.wMonth, lpht->st.wYear) + day;
1564       }
1565       else if (day > MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear))
1566       {
1567           retval = MCHT_CALENDARDATENEXT;
1568           MONTHCAL_GetNextMonth(&lpht->st);
1569           lpht->st.wDay = day - MONTHCAL_MonthLength(infoPtr->curSel.wMonth, infoPtr->curSel.wYear);
1570       }
1571       else {
1572         retval = MCHT_CALENDARDATE;
1573         lpht->st.wDay = day;
1574       }
1575       /* always update day of week */
1576       lpht->st.wDayOfWeek = MONTHCAL_CalculateDayOfWeek(day, lpht->st.wMonth,
1577                                                              lpht->st.wYear);
1578       goto done;
1579   }
1580   if(PtInRect(&infoPtr->todayrect, lpht->pt)) {
1581     retval = MCHT_TODAYLINK;
1582     goto done;
1583   }
1584
1585
1586   /* Hit nothing special? What's left must be background :-) */
1587
1588   retval = MCHT_CALENDARBK;
1589  done:
1590   lpht->uHit = retval;
1591   return retval;
1592 }
1593
1594 /* MCN_GETDAYSTATE notification helper */
1595 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1596 {
1597   if(infoPtr->dwStyle & MCS_DAYSTATE) {
1598     NMDAYSTATE nmds;
1599     INT i;
1600
1601     nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1602     nmds.nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1603     nmds.nmhdr.code     = MCN_GETDAYSTATE;
1604     nmds.cDayState      = infoPtr->monthRange;
1605     nmds.prgDayState    = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1606
1607     nmds.stStart = infoPtr->todaysDate;
1608     nmds.stStart.wYear  = infoPtr->curSel.wYear;
1609     nmds.stStart.wMonth = infoPtr->curSel.wMonth;
1610     nmds.stStart.wDay = 1;
1611
1612     SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1613     for(i = 0; i < infoPtr->monthRange; i++)
1614       infoPtr->monthdayState[i] = nmds.prgDayState[i];
1615
1616     Free(nmds.prgDayState);
1617   }
1618 }
1619
1620 static void MONTHCAL_GoToNextMonth(MONTHCAL_INFO *infoPtr)
1621 {
1622   SYSTEMTIME next = infoPtr->curSel;
1623
1624   TRACE("\n");
1625
1626   MONTHCAL_GetNextMonth(&next);
1627
1628   if(!MONTHCAL_IsDateInValidRange(infoPtr, &next)) return;
1629
1630   infoPtr->curSel = next;
1631
1632   MONTHCAL_NotifyDayState(infoPtr);
1633 }
1634
1635
1636 static void MONTHCAL_GoToPrevMonth(MONTHCAL_INFO *infoPtr)
1637 {
1638   SYSTEMTIME prev = infoPtr->curSel;
1639
1640   TRACE("\n");
1641
1642   MONTHCAL_GetPrevMonth(&prev);
1643
1644   if(!MONTHCAL_IsDateInValidRange(infoPtr, &prev)) return;
1645
1646   infoPtr->curSel = prev;
1647
1648   MONTHCAL_NotifyDayState(infoPtr);
1649 }
1650
1651 static LRESULT
1652 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1653 {
1654   static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1655   HMENU hMenu;
1656   POINT menupoint;
1657   WCHAR buf[32];
1658
1659   hMenu = CreatePopupMenu();
1660   if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1661   {
1662       WARN("Can't load resource\n");
1663       strcpyW(buf, todayW);
1664   }
1665   AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1666   menupoint.x = (short)LOWORD(lParam);
1667   menupoint.y = (short)HIWORD(lParam);
1668   ClientToScreen(infoPtr->hwndSelf, &menupoint);
1669   if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
1670                      menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1671   {
1672       infoPtr->curSel = infoPtr->todaysDate;
1673       infoPtr->minSel = infoPtr->todaysDate;
1674       infoPtr->maxSel = infoPtr->todaysDate;
1675       InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1676   }
1677
1678   return 0;
1679 }
1680
1681 /* creates updown control and edit box */
1682 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr)
1683 {
1684     infoPtr->hWndYearEdit =
1685         CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
1686                         infoPtr->titleyear.left + 3, infoPtr->titlebtnnext.top,
1687                         infoPtr->titleyear.right - infoPtr->titleyear.left + 4,
1688                         infoPtr->textHeight, infoPtr->hwndSelf,
1689                         NULL, NULL, NULL);
1690
1691     SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
1692
1693     infoPtr->hWndYearUpDown =
1694         CreateWindowExW(0, UPDOWN_CLASSW, 0,
1695                         WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
1696                         infoPtr->titleyear.right + 7, infoPtr->titlebtnnext.top,
1697                         18, infoPtr->textHeight, infoPtr->hwndSelf,
1698                         NULL, NULL, NULL);
1699
1700     /* attach edit box */
1701     SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
1702                  MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
1703     SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
1704     SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->curSel.wYear);
1705 }
1706
1707 static LRESULT
1708 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1709 {
1710   MCHITTESTINFO ht;
1711   DWORD hit;
1712
1713   if (infoPtr->hWndYearUpDown)
1714   {
1715       infoPtr->curSel.wYear = SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, 0);
1716       if(!DestroyWindow(infoPtr->hWndYearUpDown))
1717       {
1718           FIXME("Can't destroy Updown Control\n");
1719       }
1720       else
1721           infoPtr->hWndYearUpDown = 0;
1722
1723       if(!DestroyWindow(infoPtr->hWndYearEdit))
1724       {
1725           FIXME("Can't destroy Updown Control\n");
1726       }
1727       else
1728           infoPtr->hWndYearEdit = 0;
1729
1730       InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1731   }
1732
1733   SetCapture(infoPtr->hwndSelf);
1734
1735   ht.cbSize = sizeof(MCHITTESTINFO);
1736   ht.pt.x = (short)LOWORD(lParam);
1737   ht.pt.y = (short)HIWORD(lParam);
1738
1739   hit = MONTHCAL_HitTest(infoPtr, &ht);
1740
1741   TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
1742
1743   switch(hit)
1744   {
1745   case MCHT_TITLEBTNNEXT:
1746     MONTHCAL_GoToNextMonth(infoPtr);
1747     infoPtr->status = MC_NEXTPRESSED;
1748     SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1749     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1750     return 0;
1751
1752   case MCHT_TITLEBTNPREV:
1753     MONTHCAL_GoToPrevMonth(infoPtr);
1754     infoPtr->status = MC_PREVPRESSED;
1755     SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
1756     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1757     return 0;
1758
1759   case MCHT_TITLEMONTH:
1760   {
1761     HMENU hMenu = CreatePopupMenu();
1762     WCHAR buf[32];
1763     POINT menupoint;
1764     INT i;
1765
1766     for (i = 0; i < 12; i++)
1767     {
1768         GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
1769         AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
1770     }
1771     menupoint.x = ht.pt.x;
1772     menupoint.y = ht.pt.y;
1773     ClientToScreen(infoPtr->hwndSelf, &menupoint);
1774     i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
1775                        menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
1776
1777     if ((i > 0) && (i < 13) && infoPtr->curSel.wMonth != i)
1778     {
1779         infoPtr->curSel.wMonth = i;
1780         InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1781     }
1782     return 0;
1783   }
1784   case MCHT_TITLEYEAR:
1785   {
1786     MONTHCAL_EditYear(infoPtr);
1787     return 0;
1788   }
1789   case MCHT_TODAYLINK:
1790   {
1791     infoPtr->curSel = infoPtr->todaysDate;
1792     infoPtr->minSel = infoPtr->todaysDate;
1793     infoPtr->maxSel = infoPtr->todaysDate;
1794     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1795
1796     MONTHCAL_NotifySelectionChange(infoPtr);
1797     MONTHCAL_NotifySelect(infoPtr);
1798     return 0;
1799   }
1800   case MCHT_CALENDARDATENEXT:
1801   case MCHT_CALENDARDATEPREV:
1802   case MCHT_CALENDARDATE:
1803   {
1804     MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
1805
1806     if(infoPtr->dwStyle & MCS_MULTISELECT)
1807     {
1808       SYSTEMTIME st[2];
1809
1810       st[0] = st[1] = ht.st;
1811
1812       /* clear selection range */
1813       MONTHCAL_SetSelRange(infoPtr, st);
1814     }
1815
1816     infoPtr->status = MC_SEL_LBUTDOWN;
1817     MONTHCAL_SetDayFocus(infoPtr, &ht.st);
1818     return 0;
1819   }
1820   }
1821
1822   return 1;
1823 }
1824
1825
1826 static LRESULT
1827 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1828 {
1829   NMHDR nmhdr;
1830   MCHITTESTINFO ht;
1831   DWORD hit;
1832
1833   TRACE("\n");
1834
1835   if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
1836     RECT *r;
1837
1838     KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
1839     r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
1840     infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
1841
1842     InvalidateRect(infoPtr->hwndSelf, r, FALSE);
1843   }
1844
1845   ReleaseCapture();
1846
1847   /* always send NM_RELEASEDCAPTURE notification */
1848   nmhdr.hwndFrom = infoPtr->hwndSelf;
1849   nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1850   nmhdr.code     = NM_RELEASEDCAPTURE;
1851   TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
1852
1853   SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
1854
1855   ht.cbSize = sizeof(MCHITTESTINFO);
1856   ht.pt.x = (short)LOWORD(lParam);
1857   ht.pt.y = (short)HIWORD(lParam);
1858   hit = MONTHCAL_HitTest(infoPtr, &ht);
1859
1860   infoPtr->status = MC_SEL_LBUTUP;
1861   MONTHCAL_SetDayFocus(infoPtr, NULL);
1862
1863   if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
1864   {
1865     SYSTEMTIME sel = infoPtr->curSel;
1866
1867     if(!(infoPtr->dwStyle & MCS_MULTISELECT))
1868     {
1869         SYSTEMTIME st[2];
1870
1871         st[0] = st[1] = ht.st;
1872         MONTHCAL_SetSelRange(infoPtr, st);
1873         /* will be invalidated here */
1874         MONTHCAL_SetCurSel(infoPtr, &st[0]);
1875     }
1876
1877     /* send MCN_SELCHANGE only if new date selected */
1878     if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
1879         MONTHCAL_NotifySelectionChange(infoPtr);
1880
1881     MONTHCAL_NotifySelect(infoPtr);
1882   }
1883
1884   return 0;
1885 }
1886
1887
1888 static LRESULT
1889 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
1890 {
1891   TRACE("%ld\n", id);
1892
1893   switch(id) {
1894   case MC_PREVNEXTMONTHTIMER:
1895     if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToNextMonth(infoPtr);
1896     if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToPrevMonth(infoPtr);
1897     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1898     break;
1899   case MC_TODAYUPDATETIMER:
1900   {
1901     SYSTEMTIME st;
1902
1903     if(infoPtr->todaySet) return 0;
1904
1905     GetLocalTime(&st);
1906     MONTHCAL_UpdateToday(infoPtr, &st);
1907
1908     /* notification sent anyway */
1909     MONTHCAL_NotifySelectionChange(infoPtr);
1910
1911     return 0;
1912   }
1913   default:
1914     ERR("got unknown timer %ld\n", id);
1915     break;
1916   }
1917
1918   return 0;
1919 }
1920
1921
1922 static LRESULT
1923 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1924 {
1925   MCHITTESTINFO ht;
1926   SYSTEMTIME old_focused, st_ht;
1927   INT hit;
1928   RECT r;
1929
1930   if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
1931
1932   ht.cbSize = sizeof(MCHITTESTINFO);
1933   ht.pt.x = (short)LOWORD(lParam);
1934   ht.pt.y = (short)HIWORD(lParam);
1935
1936   hit = MONTHCAL_HitTest(infoPtr, &ht);
1937
1938   /* not on the calendar date numbers? bail out */
1939   TRACE("hit:%x\n",hit);
1940   if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
1941   {
1942     MONTHCAL_SetDayFocus(infoPtr, NULL);
1943     return 0;
1944   }
1945
1946   st_ht = ht.st;
1947   old_focused = infoPtr->focusedSel;
1948
1949   /* if pointer is over focused day still there's nothing to do */
1950   if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
1951
1952   MONTHCAL_CalcPosFromDay(infoPtr, ht.st.wDay, ht.st.wMonth, &r);
1953
1954   if(infoPtr->dwStyle & MCS_MULTISELECT) {
1955     SYSTEMTIME st[2];
1956
1957     MONTHCAL_GetSelRange(infoPtr, st);
1958
1959     /* If we're still at the first selected date and range is empty, return.
1960        If range isn't empty we should change range to a single firstSel */
1961     if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
1962        MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
1963
1964     MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
1965
1966     st[0] = infoPtr->firstSel;
1967     /* we should overwrite timestamp here */
1968     MONTHCAL_CopyDate(&st_ht, &st[1]);
1969
1970     /* bounds will be swapped here if needed */
1971     MONTHCAL_SetSelRange(infoPtr, st);
1972
1973     return 0;
1974   }
1975
1976 done:
1977
1978   /* FIXME: this should specify a rectangle containing only the days that changed
1979      using InvalidateRect */
1980   InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1981
1982   return 0;
1983 }
1984
1985
1986 static LRESULT
1987 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
1988 {
1989   HDC hdc;
1990   PAINTSTRUCT ps;
1991
1992   if (hdc_paint)
1993   {
1994     GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
1995     hdc = hdc_paint;
1996   }
1997   else
1998     hdc = BeginPaint(infoPtr->hwndSelf, &ps);
1999
2000   MONTHCAL_Refresh(infoPtr, hdc, &ps);
2001   if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2002   return 0;
2003 }
2004
2005
2006 static LRESULT
2007 MONTHCAL_KillFocus(const MONTHCAL_INFO *infoPtr, HWND hFocusWnd)
2008 {
2009   TRACE("\n");
2010
2011   if (infoPtr->hwndNotify != hFocusWnd)
2012     ShowWindow(infoPtr->hwndSelf, SW_HIDE);
2013   else
2014     InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2015
2016   return 0;
2017 }
2018
2019
2020 static LRESULT
2021 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2022 {
2023   TRACE("\n");
2024
2025   InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2026
2027   return 0;
2028 }
2029
2030 /* sets the size information */
2031 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2032 {
2033   static const WCHAR O0W[] = { '0','0',0 };
2034   HDC hdc = GetDC(infoPtr->hwndSelf);
2035   RECT *title=&infoPtr->title;
2036   RECT *prev=&infoPtr->titlebtnprev;
2037   RECT *next=&infoPtr->titlebtnnext;
2038   RECT *titlemonth=&infoPtr->titlemonth;
2039   RECT *titleyear=&infoPtr->titleyear;
2040   RECT *wdays=&infoPtr->wdays;
2041   RECT *weeknumrect=&infoPtr->weeknums;
2042   RECT *days=&infoPtr->days;
2043   RECT *todayrect=&infoPtr->todayrect;
2044   SIZE size, sz;
2045   TEXTMETRICW tm;
2046   HFONT currentFont;
2047   INT xdiv, dx, dy, i;
2048   RECT rcClient;
2049   WCHAR buff[80];
2050
2051   GetClientRect(infoPtr->hwndSelf, &rcClient);
2052
2053   currentFont = SelectObject(hdc, infoPtr->hFont);
2054
2055   /* get the height and width of each day's text */
2056   GetTextMetricsW(hdc, &tm);
2057   infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2058
2059   /* find largest abbreviated day name for current locale */
2060   size.cx = sz.cx = 0;
2061   for (i = 0; i < 7; i++)
2062   {
2063       if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1,
2064                         buff, countof(buff)))
2065       {
2066           GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2067           if (sz.cx > size.cx) size.cx = sz.cx;
2068       }
2069       else /* locale independent fallback on failure */
2070       {
2071           static const WCHAR SunW[] = { 'S','u','n',0 };
2072
2073           GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2074           break;
2075       }
2076   }
2077
2078   infoPtr->textWidth = size.cx + 2;
2079
2080   /* recalculate the height and width increments and offsets */
2081   GetTextExtentPoint32W(hdc, O0W, 2, &size);
2082
2083   xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2084
2085   infoPtr->width_increment  = size.cx * 2 + 4;
2086   infoPtr->height_increment = infoPtr->textHeight;
2087
2088   /* calculate title area */
2089   title->top    = 0;
2090   title->bottom = 3 * infoPtr->height_increment / 2;
2091   title->left   = 0;
2092   title->right  = infoPtr->width_increment * xdiv;
2093
2094   /* set the dimensions of the next and previous buttons and center */
2095   /* the month text vertically */
2096   prev->top    = next->top    = title->top + 4;
2097   prev->bottom = next->bottom = title->bottom - 4;
2098   prev->left   = title->left + 4;
2099   prev->right  = prev->left + (title->bottom - title->top);
2100   next->right  = title->right - 4;
2101   next->left   = next->right - (title->bottom - title->top);
2102
2103   /* titlemonth->left and right change based upon the current month */
2104   /* and are recalculated in refresh as the current month may change */
2105   /* without the control being resized */
2106   titlemonth->top    = titleyear->top    = title->top    + (infoPtr->height_increment)/2;
2107   titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2108
2109   /* setup the dimensions of the rectangle we draw the names of the */
2110   /* days of the week in */
2111   weeknumrect->left = 0;
2112
2113   if(infoPtr->dwStyle & MCS_WEEKNUMBERS)
2114     weeknumrect->right = prev->right;
2115   else
2116     weeknumrect->right = weeknumrect->left;
2117
2118   wdays->left   = days->left   = weeknumrect->right;
2119   wdays->right  = days->right  = wdays->left + 7 * infoPtr->width_increment;
2120   wdays->top    = title->bottom;
2121   wdays->bottom = wdays->top + infoPtr->height_increment;
2122
2123   days->top    = weeknumrect->top = wdays->bottom;
2124   days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2125
2126   todayrect->left   = 0;
2127   todayrect->right  = title->right;
2128   todayrect->top    = days->bottom;
2129   todayrect->bottom = days->bottom + infoPtr->height_increment;
2130
2131   /* offset all rectangles to center in client area */
2132   dx = (rcClient.right  - title->right) / 2;
2133   dy = (rcClient.bottom - todayrect->bottom) / 2;
2134
2135   /* if calendar doesn't fit client area show it at left/top bounds */
2136   if (title->left + dx < 0) dx = 0;
2137   if (title->top  + dy < 0) dy = 0;
2138
2139   if (dx != 0 || dy != 0)
2140   {
2141     OffsetRect(title, dx, dy);
2142     OffsetRect(prev,  dx, dy);
2143     OffsetRect(next,  dx, dy);
2144     OffsetRect(titlemonth, dx, dy);
2145     OffsetRect(titleyear, dx, dy);
2146     OffsetRect(wdays, dx, dy);
2147     OffsetRect(weeknumrect, dx, dy);
2148     OffsetRect(days, dx, dy);
2149     OffsetRect(todayrect, dx, dy);
2150   }
2151
2152   TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2153         infoPtr->width_increment,infoPtr->height_increment,
2154         wine_dbgstr_rect(&rcClient),
2155         wine_dbgstr_rect(title),
2156         wine_dbgstr_rect(wdays),
2157         wine_dbgstr_rect(days),
2158         wine_dbgstr_rect(todayrect));
2159
2160   /* restore the originally selected font */
2161   SelectObject(hdc, currentFont);
2162
2163   ReleaseDC(infoPtr->hwndSelf, hdc);
2164 }
2165
2166 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2167 {
2168   TRACE("(width=%d, height=%d)\n", Width, Height);
2169
2170   MONTHCAL_UpdateSize(infoPtr);
2171
2172   /* invalidate client area and erase background */
2173   InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2174
2175   return 0;
2176 }
2177
2178 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2179 {
2180     return (LRESULT)infoPtr->hFont;
2181 }
2182
2183 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2184 {
2185     HFONT hOldFont;
2186     LOGFONTW lf;
2187
2188     if (!hFont) return 0;
2189
2190     hOldFont = infoPtr->hFont;
2191     infoPtr->hFont = hFont;
2192
2193     GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2194     lf.lfWeight = FW_BOLD;
2195     infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2196
2197     MONTHCAL_UpdateSize(infoPtr);
2198
2199     if (redraw)
2200         InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2201
2202     return (LRESULT)hOldFont;
2203 }
2204
2205 /* update theme after a WM_THEMECHANGED message */
2206 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2207 {
2208     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2209     CloseThemeData (theme);
2210     OpenThemeData (infoPtr->hwndSelf, themeClass);
2211     return 0;
2212 }
2213
2214 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2215                                  const STYLESTRUCT *lpss)
2216 {
2217     TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2218           wStyleType, lpss->styleOld, lpss->styleNew);
2219
2220     if (wStyleType != GWL_STYLE) return 0;
2221
2222     infoPtr->dwStyle = lpss->styleNew;
2223
2224     /* make room for week numbers */
2225     if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2226         MONTHCAL_UpdateSize(infoPtr);
2227
2228     return 0;
2229 }
2230
2231 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2232                                   STYLESTRUCT *lpss)
2233 {
2234     TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2235           wStyleType, lpss->styleOld, lpss->styleNew);
2236
2237     /* block MCS_MULTISELECT change */
2238     if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2239     {
2240         if (lpss->styleOld & MCS_MULTISELECT)
2241             lpss->styleNew |= MCS_MULTISELECT;
2242         else
2243             lpss->styleNew &= ~MCS_MULTISELECT;
2244     }
2245
2246     return 0;
2247 }
2248
2249 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2250 static LRESULT
2251 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2252 {
2253   MONTHCAL_INFO *infoPtr;
2254
2255   /* allocate memory for info structure */
2256   infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2257   SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2258
2259   if(infoPtr == NULL) {
2260     ERR( "could not allocate info memory!\n");
2261     return 0;
2262   }
2263
2264   infoPtr->hwndSelf = hwnd;
2265   infoPtr->hwndNotify = lpcs->hwndParent;
2266   infoPtr->dwStyle  = GetWindowLongW(hwnd, GWL_STYLE);
2267
2268   MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
2269
2270   /* initialize info structure */
2271   /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
2272
2273   GetLocalTime(&infoPtr->todaysDate);
2274   MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2275
2276   infoPtr->maxSelCount   = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2277   infoPtr->monthRange    = 3;
2278   infoPtr->monthdayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
2279   infoPtr->titlebk       = comctl32_color.clrActiveCaption;
2280   infoPtr->titletxt      = comctl32_color.clrWindow;
2281   infoPtr->monthbk       = comctl32_color.clrWindow;
2282   infoPtr->trailingtxt   = comctl32_color.clrGrayText;
2283   infoPtr->bk            = comctl32_color.clrWindow;
2284   infoPtr->txt           = comctl32_color.clrWindowText;
2285
2286   infoPtr->minSel = infoPtr->todaysDate;
2287   infoPtr->maxSel = infoPtr->todaysDate;
2288   infoPtr->curSel = infoPtr->todaysDate;
2289
2290   /* call MONTHCAL_UpdateSize to set all of the dimensions */
2291   /* of the control */
2292   MONTHCAL_UpdateSize(infoPtr);
2293
2294   /* today auto update timer, to be freed only on control destruction */
2295   SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2296
2297   OpenThemeData (infoPtr->hwndSelf, themeClass);
2298
2299   return 0;
2300 }
2301
2302
2303 static LRESULT
2304 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2305 {
2306   /* free month calendar info data */
2307   Free(infoPtr->monthdayState);
2308   SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2309
2310   CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2311   
2312   Free(infoPtr);
2313   return 0;
2314 }
2315
2316
2317 static LRESULT WINAPI
2318 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2319 {
2320   MONTHCAL_INFO *infoPtr;
2321
2322   TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2323
2324   infoPtr = MONTHCAL_GetInfoPtr(hwnd);
2325   if (!infoPtr && (uMsg != WM_CREATE))
2326     return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2327   switch(uMsg)
2328   {
2329   case MCM_GETCURSEL:
2330     return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2331
2332   case MCM_SETCURSEL:
2333     return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2334
2335   case MCM_GETMAXSELCOUNT:
2336     return MONTHCAL_GetMaxSelCount(infoPtr);
2337
2338   case MCM_SETMAXSELCOUNT:
2339     return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2340
2341   case MCM_GETSELRANGE:
2342     return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2343
2344   case MCM_SETSELRANGE:
2345     return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2346
2347   case MCM_GETMONTHRANGE:
2348     return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2349
2350   case MCM_SETDAYSTATE:
2351     return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2352
2353   case MCM_GETMINREQRECT:
2354     return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2355
2356   case MCM_GETCOLOR:
2357     return MONTHCAL_GetColor(infoPtr, wParam);
2358
2359   case MCM_SETCOLOR:
2360     return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2361
2362   case MCM_GETTODAY:
2363     return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2364
2365   case MCM_SETTODAY:
2366     return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2367
2368   case MCM_HITTEST:
2369     return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2370
2371   case MCM_GETFIRSTDAYOFWEEK:
2372     return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2373
2374   case MCM_SETFIRSTDAYOFWEEK:
2375     return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2376
2377   case MCM_GETRANGE:
2378     return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2379
2380   case MCM_SETRANGE:
2381     return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2382
2383   case MCM_GETMONTHDELTA:
2384     return MONTHCAL_GetMonthDelta(infoPtr);
2385
2386   case MCM_SETMONTHDELTA:
2387     return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2388
2389   case MCM_GETMAXTODAYWIDTH:
2390     return MONTHCAL_GetMaxTodayWidth(infoPtr);
2391
2392   case WM_GETDLGCODE:
2393     return DLGC_WANTARROWS | DLGC_WANTCHARS;
2394
2395   case WM_KILLFOCUS:
2396     return MONTHCAL_KillFocus(infoPtr, (HWND)wParam);
2397
2398   case WM_RBUTTONUP:
2399     return MONTHCAL_RButtonUp(infoPtr, lParam);
2400
2401   case WM_LBUTTONDOWN:
2402     return MONTHCAL_LButtonDown(infoPtr, lParam);
2403
2404   case WM_MOUSEMOVE:
2405     return MONTHCAL_MouseMove(infoPtr, lParam);
2406
2407   case WM_LBUTTONUP:
2408     return MONTHCAL_LButtonUp(infoPtr, lParam);
2409
2410   case WM_PRINTCLIENT:
2411   case WM_PAINT:
2412     return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2413
2414   case WM_SETFOCUS:
2415     return MONTHCAL_SetFocus(infoPtr);
2416
2417   case WM_SIZE:
2418     return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2419
2420   case WM_CREATE:
2421     return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2422
2423   case WM_SETFONT:
2424     return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2425
2426   case WM_GETFONT:
2427     return MONTHCAL_GetFont(infoPtr);
2428
2429   case WM_TIMER:
2430     return MONTHCAL_Timer(infoPtr, wParam);
2431     
2432   case WM_THEMECHANGED:
2433     return theme_changed (infoPtr);
2434
2435   case WM_DESTROY:
2436     return MONTHCAL_Destroy(infoPtr);
2437
2438   case WM_SYSCOLORCHANGE:
2439     COMCTL32_RefreshSysColors();
2440     return 0;
2441
2442   case WM_STYLECHANGED:
2443     return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2444
2445   case WM_STYLECHANGING:
2446     return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2447
2448   default:
2449     if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2450       ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2451     return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2452   }
2453 }
2454
2455
2456 void
2457 MONTHCAL_Register(void)
2458 {
2459   WNDCLASSW wndClass;
2460
2461   ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2462   wndClass.style         = CS_GLOBALCLASS;
2463   wndClass.lpfnWndProc   = MONTHCAL_WindowProc;
2464   wndClass.cbClsExtra    = 0;
2465   wndClass.cbWndExtra    = sizeof(MONTHCAL_INFO *);
2466   wndClass.hCursor       = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2467   wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2468   wndClass.lpszClassName = MONTHCAL_CLASSW;
2469
2470   RegisterClassW(&wndClass);
2471 }
2472
2473
2474 void
2475 MONTHCAL_Unregister(void)
2476 {
2477     UnregisterClassW(MONTHCAL_CLASSW, NULL);
2478 }