msvcrt: NULL terminate program arguments list in __getmainargs.
[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-2011 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  *    -- handle resources better (doesn't work now); 
37  *    -- take care of internationalization.
38  *    -- keyboard handling.
39  *    -- search for FIXME
40  */
41
42 #include <math.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "commctrl.h"
54 #include "comctl32.h"
55 #include "uxtheme.h"
56 #include "vssym32.h"
57 #include "wine/unicode.h"
58 #include "wine/debug.h"
59
60 WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
61
62 #define MC_SEL_LBUTUP       1   /* Left button released */
63 #define MC_SEL_LBUTDOWN     2   /* Left button pressed in calendar */
64 #define MC_PREVPRESSED      4   /* Prev month button pressed */
65 #define MC_NEXTPRESSED      8   /* Next month button pressed */
66 #define MC_PREVNEXTMONTHDELAY   350     /* when continuously pressing `next/prev
67                                            month', wait 350 ms before going
68                                            to the next/prev month */
69 #define MC_TODAYUPDATEDELAY 120000 /* time between today check for update (2 min) */
70
71 #define MC_PREVNEXTMONTHTIMER   1       /* Timer IDs */
72 #define MC_TODAYUPDATETIMER     2
73
74 #define MC_CALENDAR_PADDING     6
75
76 #define countof(arr) (sizeof(arr)/sizeof(arr[0]))
77
78 /* convert from days to 100 nanoseconds unit - used as FILETIME unit */
79 #define DAYSTO100NSECS(days) (((ULONGLONG)(days))*24*60*60*10000000)
80
81 enum CachedPen
82 {
83     PenRed = 0,
84     PenText,
85     PenLast
86 };
87
88 enum CachedBrush
89 {
90     BrushTitle = 0,
91     BrushMonth,
92     BrushBackground,
93     BrushLast
94 };
95
96 /* single calendar data */
97 typedef struct _CALENDAR_INFO
98 {
99     RECT title;      /* rect for the header above the calendar */
100     RECT titlemonth; /* the 'month name' text in the header */
101     RECT titleyear;  /* the 'year number' text in the header */
102     RECT wdays;      /* week days at top */
103     RECT days;       /* calendar area */
104     RECT weeknums;   /* week numbers at left side */
105
106     SYSTEMTIME month;/* contains calendar main month/year */
107 } CALENDAR_INFO;
108
109 typedef struct
110 {
111     HWND        hwndSelf;
112     DWORD       dwStyle; /* cached GWL_STYLE */
113
114     COLORREF    colors[MCSC_TRAILINGTEXT+1];
115     HBRUSH      brushes[BrushLast];
116     HPEN        pens[PenLast];
117
118     HFONT       hFont;
119     HFONT       hBoldFont;
120     int         textHeight;
121     int         textWidth;
122     int         height_increment;
123     int         width_increment;
124     INT         delta;  /* scroll rate; # of months that the */
125                         /* control moves when user clicks a scroll button */
126     int         visible;        /* # of months visible */
127     int         firstDay;       /* Start month calendar with firstDay's day,
128                                    stored in SYSTEMTIME format */
129     BOOL        firstDaySet;    /* first week day differs from locale defined */
130
131     BOOL        isUnicode;      /* value set with MCM_SETUNICODE format */
132
133     MONTHDAYSTATE *monthdayState;
134     SYSTEMTIME  todaysDate;
135     BOOL        todaySet;       /* Today was forced with MCM_SETTODAY */
136     int         status;         /* See MC_SEL flags */
137     SYSTEMTIME  firstSel;       /* first selected day */
138     INT         maxSelCount;
139     SYSTEMTIME  minSel;         /* contains single selection when used without MCS_MULTISELECT */
140     SYSTEMTIME  maxSel;
141     SYSTEMTIME  focusedSel;     /* date currently focused with mouse movement */
142     DWORD       rangeValid;
143     SYSTEMTIME  minDate;
144     SYSTEMTIME  maxDate;
145
146     RECT titlebtnnext;  /* the `next month' button in the header */
147     RECT titlebtnprev;  /* the `prev month' button in the header */
148     RECT todayrect;     /* `today: xx/xx/xx' text rect */
149     HWND hwndNotify;    /* Window to receive the notifications */
150     HWND hWndYearEdit;  /* Window Handle of edit box to handle years */
151     HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
152     WNDPROC EditWndProc;  /* original Edit window procedure */
153
154     CALENDAR_INFO *calendars;
155     SIZE dim;           /* [cx,cy] - dimensions of calendars matrix, row/column count */
156 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
157
158 static const WCHAR themeClass[] = { 'S','c','r','o','l','l','b','a','r',0 };
159
160 /* empty SYSTEMTIME const */
161 static const SYSTEMTIME st_null;
162 /* valid date limits */
163 static const SYSTEMTIME max_allowed_date = { /* wYear */ 9999, /* wMonth */ 12, /* wDayOfWeek */ 0, /* wDay */ 31 };
164 static const SYSTEMTIME min_allowed_date = { /* wYear */ 1752, /* wMonth */ 9,  /* wDayOfWeek */ 0, /* wDay */ 14 };
165
166 /* Prev/Next buttons */
167 enum nav_direction
168 {
169     DIRECTION_BACKWARD,
170     DIRECTION_FORWARD
171 };
172
173 /* helper functions  */
174 static inline INT MONTHCAL_GetCalCount(const MONTHCAL_INFO *infoPtr)
175 {
176    return infoPtr->dim.cx * infoPtr->dim.cy;
177 }
178
179 /* send a single MCN_SELCHANGE notification */
180 static inline void MONTHCAL_NotifySelectionChange(const MONTHCAL_INFO *infoPtr)
181 {
182     NMSELCHANGE nmsc;
183
184     nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
185     nmsc.nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
186     nmsc.nmhdr.code     = MCN_SELCHANGE;
187     nmsc.stSelStart     = infoPtr->minSel;
188     nmsc.stSelStart.wDayOfWeek = 0;
189     if(infoPtr->dwStyle & MCS_MULTISELECT){
190         nmsc.stSelEnd = infoPtr->maxSel;
191         nmsc.stSelEnd.wDayOfWeek = 0;
192     }
193     else
194         nmsc.stSelEnd = st_null;
195
196     SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
197 }
198
199 /* send a single MCN_SELECT notification */
200 static inline void MONTHCAL_NotifySelect(const MONTHCAL_INFO *infoPtr)
201 {
202     NMSELCHANGE nmsc;
203
204     nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
205     nmsc.nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
206     nmsc.nmhdr.code     = MCN_SELECT;
207     nmsc.stSelStart     = infoPtr->minSel;
208     nmsc.stSelStart.wDayOfWeek = 0;
209     if(infoPtr->dwStyle & MCS_MULTISELECT){
210         nmsc.stSelEnd = infoPtr->maxSel;
211         nmsc.stSelEnd.wDayOfWeek = 0;
212     }
213     else
214         nmsc.stSelEnd = st_null;
215
216     SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
217 }
218
219 static inline int MONTHCAL_MonthDiff(const SYSTEMTIME *left, const SYSTEMTIME *right)
220 {
221     return (right->wYear - left->wYear)*12 + right->wMonth - left->wMonth;
222 }
223
224 /* returns the number of days in any given month, checking for leap days */
225 /* January is 1, December is 12 */
226 int MONTHCAL_MonthLength(int month, int year)
227 {
228   const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
229   /* Wrap around, this eases handling. Getting length only we shouldn't care
230      about year change here cause January and December have
231      the same day quantity */
232   if(month == 0)
233     month = 12;
234   else if(month == 13)
235     month = 1;
236
237   /* special case for calendar transition year */
238   if(month == min_allowed_date.wMonth && year == min_allowed_date.wYear) return 19;
239
240   /* if we have a leap year add 1 day to February */
241   /* a leap year is a year either divisible by 400 */
242   /* or divisible by 4 and not by 100 */
243   if(month == 2) { /* February */
244     return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
245      (year%4 == 0)) ? 1 : 0);
246   }
247   else {
248     return mdays[month - 1];
249   }
250 }
251
252 /* compares timestamps using date part only */
253 static inline BOOL MONTHCAL_IsDateEqual(const SYSTEMTIME *first, const SYSTEMTIME *second)
254 {
255   return (first->wYear == second->wYear) && (first->wMonth == second->wMonth) &&
256          (first->wDay  == second->wDay);
257 }
258
259 /* make sure that date fields are valid */
260 static BOOL MONTHCAL_ValidateDate(const SYSTEMTIME *time)
261 {
262   if(time->wMonth < 1 || time->wMonth > 12 ) return FALSE;
263   if(time->wDay > MONTHCAL_MonthLength(time->wMonth, time->wYear)) return FALSE;
264
265   return TRUE;
266 }
267
268 /* Copies timestamp part only.
269  *
270  * PARAMETERS
271  *
272  *  [I] from : source date
273  *  [O] to   : dest date
274  */
275 static void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
276 {
277   to->wHour   = from->wHour;
278   to->wMinute = from->wMinute;
279   to->wSecond = from->wSecond;
280 }
281
282 /* Copies date part only.
283  *
284  * PARAMETERS
285  *
286  *  [I] from : source date
287  *  [O] to   : dest date
288  */
289 static void MONTHCAL_CopyDate(const SYSTEMTIME *from, SYSTEMTIME *to)
290 {
291   to->wYear  = from->wYear;
292   to->wMonth = from->wMonth;
293   to->wDay   = from->wDay;
294   to->wDayOfWeek = from->wDayOfWeek;
295 }
296
297 /* Compares two dates in SYSTEMTIME format
298  *
299  * PARAMETERS
300  *
301  *  [I] first  : pointer to valid first date data to compare
302  *  [I] second : pointer to valid second date data to compare
303  *
304  * RETURN VALUE
305  *
306  *  -1 : first <  second
307  *   0 : first == second
308  *   1 : first >  second
309  *
310  *  Note that no date validation performed, already validated values expected.
311  */
312 LONG MONTHCAL_CompareSystemTime(const SYSTEMTIME *first, const SYSTEMTIME *second)
313 {
314   FILETIME ft_first, ft_second;
315
316   SystemTimeToFileTime(first, &ft_first);
317   SystemTimeToFileTime(second, &ft_second);
318
319   return CompareFileTime(&ft_first, &ft_second);
320 }
321
322 static LONG MONTHCAL_CompareMonths(const SYSTEMTIME *first, const SYSTEMTIME *second)
323 {
324   SYSTEMTIME st_first, st_second;
325
326   st_first = st_second = st_null;
327   MONTHCAL_CopyDate(first, &st_first);
328   MONTHCAL_CopyDate(second, &st_second);
329   st_first.wDay = st_second.wDay = 1;
330
331   return MONTHCAL_CompareSystemTime(&st_first, &st_second);
332 }
333
334 static LONG MONTHCAL_CompareDate(const SYSTEMTIME *first, const SYSTEMTIME *second)
335 {
336   SYSTEMTIME st_first, st_second;
337
338   st_first = st_second = st_null;
339   MONTHCAL_CopyDate(first, &st_first);
340   MONTHCAL_CopyDate(second, &st_second);
341
342   return MONTHCAL_CompareSystemTime(&st_first, &st_second);
343 }
344
345 /* Checks largest possible date range and configured one
346  *
347  * PARAMETERS
348  *
349  *  [I] infoPtr : valid pointer to control data
350  *  [I] date    : pointer to valid date data to check
351  *  [I] fix     : make date fit valid range
352  *
353  * RETURN VALUE
354  *
355  *  TRUE  - date within largest and configured range
356  *  FALSE - date is outside largest or configured range
357  */
358 static BOOL MONTHCAL_IsDateInValidRange(const MONTHCAL_INFO *infoPtr,
359                                         SYSTEMTIME *date, BOOL fix)
360 {
361   const SYSTEMTIME *fix_st = NULL;
362
363   if(MONTHCAL_CompareSystemTime(date, &max_allowed_date) == 1) {
364      fix_st = &max_allowed_date;
365   }
366   else if(MONTHCAL_CompareSystemTime(date, &min_allowed_date) == -1) {
367      fix_st = &min_allowed_date;
368   }
369   else {
370      if(infoPtr->rangeValid & GDTR_MAX) {
371         if((MONTHCAL_CompareSystemTime(date, &infoPtr->maxDate) == 1)) {
372            fix_st = &infoPtr->maxDate;
373         }
374      }
375
376      if(infoPtr->rangeValid & GDTR_MIN) {
377         if((MONTHCAL_CompareSystemTime(date, &infoPtr->minDate) == -1)) {
378            fix_st = &infoPtr->minDate;
379         }
380      }
381   }
382
383   if (fix && fix_st) {
384     date->wYear  = fix_st->wYear;
385     date->wMonth = fix_st->wMonth;
386   }
387
388   return !fix_st;
389 }
390
391 /* Checks passed range width with configured maximum selection count
392  *
393  * PARAMETERS
394  *
395  *  [I] infoPtr : valid pointer to control data
396  *  [I] range0  : pointer to valid date data (requested bound)
397  *  [I] range1  : pointer to valid date data (primary bound)
398  *  [O] adjust  : returns adjusted range bound to fit maximum range (optional)
399  *
400  *  Adjust value computed basing on primary bound and current maximum selection
401  *  count. For simple range check (without adjusted value required) (range0, range1)
402  *  relation means nothing.
403  *
404  * RETURN VALUE
405  *
406  *  TRUE  - range is shorter or equal to maximum
407  *  FALSE - range is larger than maximum
408  */
409 static BOOL MONTHCAL_IsSelRangeValid(const MONTHCAL_INFO *infoPtr,
410                                      const SYSTEMTIME *range0,
411                                      const SYSTEMTIME *range1,
412                                      SYSTEMTIME *adjust)
413 {
414   ULARGE_INTEGER ul_range0, ul_range1, ul_diff;
415   FILETIME ft_range0, ft_range1;
416   LONG cmp;
417
418   SystemTimeToFileTime(range0, &ft_range0);
419   SystemTimeToFileTime(range1, &ft_range1);
420
421   ul_range0.u.LowPart  = ft_range0.dwLowDateTime;
422   ul_range0.u.HighPart = ft_range0.dwHighDateTime;
423   ul_range1.u.LowPart  = ft_range1.dwLowDateTime;
424   ul_range1.u.HighPart = ft_range1.dwHighDateTime;
425
426   cmp = CompareFileTime(&ft_range0, &ft_range1);
427
428   if(cmp == 1)
429      ul_diff.QuadPart = ul_range0.QuadPart - ul_range1.QuadPart;
430   else
431      ul_diff.QuadPart = -ul_range0.QuadPart + ul_range1.QuadPart;
432
433   if(ul_diff.QuadPart >= DAYSTO100NSECS(infoPtr->maxSelCount)) {
434
435      if(adjust) {
436        if(cmp == 1)
437           ul_range0.QuadPart = ul_range1.QuadPart + DAYSTO100NSECS(infoPtr->maxSelCount - 1);
438        else
439           ul_range0.QuadPart = ul_range1.QuadPart - DAYSTO100NSECS(infoPtr->maxSelCount - 1);
440
441        ft_range0.dwLowDateTime  = ul_range0.u.LowPart;
442        ft_range0.dwHighDateTime = ul_range0.u.HighPart;
443        FileTimeToSystemTime(&ft_range0, adjust);
444      }
445
446      return FALSE;
447   }
448   else return TRUE;
449 }
450
451 /* Used in MCM_SETRANGE/MCM_SETSELRANGE to determine resulting time part.
452    Milliseconds are intentionally not validated. */
453 static BOOL MONTHCAL_ValidateTime(const SYSTEMTIME *time)
454 {
455   if((time->wHour > 24) || (time->wMinute > 59) || (time->wSecond > 59))
456     return FALSE;
457   else
458     return TRUE;
459 }
460
461 /* Note:Depending on DST, this may be offset by a day.
462    Need to find out if we're on a DST place & adjust the clock accordingly.
463    Above function assumes we have a valid data.
464    Valid for year>1752;  1 <= d <= 31, 1 <= m <= 12.
465    0 = Sunday.
466 */
467
468 /* Returns the day in the week
469  *
470  * PARAMETERS
471  *  [i] date    : input date
472  *  [I] inplace : set calculated value back to date structure
473  *
474  * RETURN VALUE
475  *   day of week in SYSTEMTIME format: (0 == sunday,..., 6 == saturday)
476  */
477 int MONTHCAL_CalculateDayOfWeek(SYSTEMTIME *date, BOOL inplace)
478 {
479   SYSTEMTIME st = st_null;
480   FILETIME ft;
481
482   MONTHCAL_CopyDate(date, &st);
483
484   SystemTimeToFileTime(&st, &ft);
485   FileTimeToSystemTime(&ft, &st);
486
487   if (inplace) date->wDayOfWeek = st.wDayOfWeek;
488
489   return st.wDayOfWeek;
490 }
491
492 /* add/subtract 'months' from date */
493 static inline void MONTHCAL_GetMonth(SYSTEMTIME *date, INT months)
494 {
495   INT length, m = date->wMonth + months;
496
497   date->wYear += m > 0 ? (m - 1) / 12 : m / 12 - 1;
498   date->wMonth = m > 0 ? (m - 1) % 12 + 1 : 12 + m % 12;
499   /* fix moving from last day in a month */
500   length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
501   if(date->wDay > length) date->wDay = length;
502   MONTHCAL_CalculateDayOfWeek(date, TRUE);
503 }
504
505 /* properly updates date to point on next month */
506 static inline void MONTHCAL_GetNextMonth(SYSTEMTIME *date)
507 {
508   MONTHCAL_GetMonth(date, 1);
509 }
510
511 /* properly updates date to point on prev month */
512 static inline void MONTHCAL_GetPrevMonth(SYSTEMTIME *date)
513 {
514   MONTHCAL_GetMonth(date, -1);
515 }
516
517 /* Returns full date for a first currently visible day */
518 static void MONTHCAL_GetMinDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
519 {
520   /* zero indexed calendar has the earliest date */
521   SYSTEMTIME st_first = infoPtr->calendars[0].month;
522   INT firstDay;
523
524   st_first.wDay = 1;
525   firstDay = MONTHCAL_CalculateDayOfWeek(&st_first, FALSE);
526
527   *date = infoPtr->calendars[0].month;
528   MONTHCAL_GetPrevMonth(date);
529
530   date->wDay = MONTHCAL_MonthLength(date->wMonth, date->wYear) +
531                (infoPtr->firstDay - firstDay) % 7 + 1;
532
533   if(date->wDay > MONTHCAL_MonthLength(date->wMonth, date->wYear))
534     date->wDay -= 7;
535
536   /* fix day of week */
537   MONTHCAL_CalculateDayOfWeek(date, TRUE);
538 }
539
540 /* Returns full date for a last currently visible day */
541 static void MONTHCAL_GetMaxDate(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *date)
542 {
543   /* the latest date is in latest calendar */
544   SYSTEMTIME st, *lt_month = &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
545   INT first_day;
546
547   *date = *lt_month;
548   st = *lt_month;
549
550   /* day of week of first day of current month */
551   st.wDay = 1;
552   first_day = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
553
554   MONTHCAL_GetNextMonth(date);
555   MONTHCAL_GetPrevMonth(&st);
556
557   /* last calendar starts with some date from previous month that not displayed */
558   st.wDay = MONTHCAL_MonthLength(st.wMonth, st.wYear) +
559              (infoPtr->firstDay - first_day) % 7 + 1;
560   if (st.wDay > MONTHCAL_MonthLength(st.wMonth, st.wYear)) st.wDay -= 7;
561
562   /* Use month length to get max day. 42 means max day count in calendar area */
563   date->wDay = 42 - (MONTHCAL_MonthLength(st.wMonth, st.wYear) - st.wDay + 1) -
564                      MONTHCAL_MonthLength(lt_month->wMonth, lt_month->wYear);
565
566   /* fix day of week */
567   MONTHCAL_CalculateDayOfWeek(date, TRUE);
568 }
569
570 /* From a given point calculate the row, column and day in the calendar,
571    'day == 0' means the last day of the last month. */
572 static int MONTHCAL_GetDayFromPos(const MONTHCAL_INFO *infoPtr, POINT pt, INT calIdx)
573 {
574   SYSTEMTIME st = infoPtr->calendars[calIdx].month;
575   int firstDay, col, row;
576   RECT client;
577
578   GetClientRect(infoPtr->hwndSelf, &client);
579
580   /* if the point is outside the x bounds of the window put it at the boundary */
581   if (pt.x > client.right) pt.x = client.right;
582
583   col = (pt.x - infoPtr->calendars[calIdx].days.left ) / infoPtr->width_increment;
584   row = (pt.y - infoPtr->calendars[calIdx].days.top  ) / infoPtr->height_increment;
585
586   st.wDay = 1;
587   firstDay = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
588   return col + 7 * row - firstDay;
589 }
590
591 /* Get day position for given date and calendar
592  *
593  * PARAMETERS
594  *
595  *  [I] infoPtr : pointer to control data
596  *  [I] date : date value
597  *  [O] col : day column (zero based)
598  *  [O] row : week column (zero based)
599  *  [I] calIdx : calendar index
600  */
601 static void MONTHCAL_GetDayPos(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
602     INT *col, INT *row, INT calIdx)
603 {
604   SYSTEMTIME st = infoPtr->calendars[calIdx].month;
605   INT first;
606
607   st.wDay = 1;
608   first = (MONTHCAL_CalculateDayOfWeek(&st, FALSE) + 6 - infoPtr->firstDay) % 7;
609
610   if (calIdx == 0 || calIdx == MONTHCAL_GetCalCount(infoPtr)-1) {
611       const SYSTEMTIME *cal = &infoPtr->calendars[calIdx].month;
612       LONG cmp = MONTHCAL_CompareMonths(date, &st);
613
614       /* previous month */
615       if (cmp == -1) {
616         *col = (first - MONTHCAL_MonthLength(date->wMonth, cal->wYear) + date->wDay) % 7;
617         *row = 0;
618         return;
619       }
620
621       /* next month calculation is same as for current, just add current month length */
622       if (cmp == 1)
623           first += MONTHCAL_MonthLength(cal->wMonth, cal->wYear);
624   }
625
626   *col = (date->wDay + first) % 7;
627   *row = (date->wDay + first - *col) / 7;
628 }
629
630 /* returns bounding box for day in given position in given calendar */
631 static inline void MONTHCAL_GetDayRectI(const MONTHCAL_INFO *infoPtr, RECT *r,
632   INT col, INT row, INT calIdx)
633 {
634   r->left   = infoPtr->calendars[calIdx].days.left + col * infoPtr->width_increment;
635   r->right  = r->left + infoPtr->width_increment;
636   r->top    = infoPtr->calendars[calIdx].days.top  + row * infoPtr->height_increment;
637   r->bottom = r->top + infoPtr->textHeight;
638 }
639
640 /* Returns bounding box for given date
641  *
642  * NOTE: when calendar index is unknown pass -1
643  */
644 static inline void MONTHCAL_GetDayRect(const MONTHCAL_INFO *infoPtr, const SYSTEMTIME *date,
645     RECT *r, INT calIdx)
646 {
647   INT col, row;
648
649   if (calIdx == -1)
650   {
651       INT cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[0].month);
652
653       if (cmp <= 0)
654           calIdx = 0;
655       else
656       {
657           cmp = MONTHCAL_CompareMonths(date, &infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month);
658           if (cmp >= 0)
659               calIdx = MONTHCAL_GetCalCount(infoPtr)-1;
660           else
661           {
662               for (calIdx = 1; calIdx < MONTHCAL_GetCalCount(infoPtr)-1; calIdx++)
663                   if (MONTHCAL_CompareMonths(date, &infoPtr->calendars[calIdx].month) == 0)
664                       break;
665           }
666       }
667   }
668
669   MONTHCAL_GetDayPos(infoPtr, date, &col, &row, calIdx);
670   MONTHCAL_GetDayRectI(infoPtr, r, col, row, calIdx);
671 }
672
673 static LRESULT
674 MONTHCAL_GetMonthRange(const MONTHCAL_INFO *infoPtr, DWORD flag, SYSTEMTIME *st)
675 {
676   INT range;
677
678   TRACE("flag=%d, st=%p\n", flag, st);
679
680   switch (flag) {
681   case GMR_VISIBLE:
682   {
683       if (st)
684       {
685           st[0] = infoPtr->calendars[0].month;
686           st[1] = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
687
688           if (st[0].wMonth == min_allowed_date.wMonth &&
689               st[0].wYear  == min_allowed_date.wYear)
690           {
691               st[0].wDay = min_allowed_date.wDay;
692           }
693           else
694               st[0].wDay = 1;
695           MONTHCAL_CalculateDayOfWeek(&st[0], TRUE);
696
697           st[1].wDay = MONTHCAL_MonthLength(st[1].wMonth, st[1].wYear);
698           MONTHCAL_CalculateDayOfWeek(&st[1], TRUE);
699       }
700
701       range = MONTHCAL_GetCalCount(infoPtr);
702       break;
703   }
704   case GMR_DAYSTATE:
705   {
706       if (st)
707       {
708           MONTHCAL_GetMinDate(infoPtr, &st[0]);
709           MONTHCAL_GetMaxDate(infoPtr, &st[1]);
710       }
711       /* include two partially visible months */
712       range = MONTHCAL_GetCalCount(infoPtr) + 2;
713       break;
714   }
715   default:
716       WARN("Unknown flag value, got %d\n", flag);
717       range = 0;
718   }
719
720   return range;
721 }
722
723 /* Focused day helper:
724
725    - set focused date to given value;
726    - reset to zero value if NULL passed;
727    - invalidate previous and new day rectangle only if needed.
728
729    Returns TRUE if focused day changed, FALSE otherwise.
730 */
731 static BOOL MONTHCAL_SetDayFocus(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *st)
732 {
733   RECT r;
734
735   if(st)
736   {
737     /* there's nothing to do if it's the same date,
738        mouse move within same date rectangle case */
739     if(MONTHCAL_IsDateEqual(&infoPtr->focusedSel, st)) return FALSE;
740
741     /* invalidate old focused day */
742     MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
743     InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
744
745     infoPtr->focusedSel = *st;
746   }
747
748   MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
749
750   if(!st && MONTHCAL_ValidateDate(&infoPtr->focusedSel))
751     infoPtr->focusedSel = st_null;
752
753   /* on set invalidates new day, on reset clears previous focused day */
754   InvalidateRect(infoPtr->hwndSelf, &r, FALSE);
755
756   return TRUE;
757 }
758
759 /* draw today boundary box for specified rectangle */
760 static void MONTHCAL_Circle(const MONTHCAL_INFO *infoPtr, HDC hdc, const RECT *r)
761 {
762   HPEN old_pen = SelectObject(hdc, infoPtr->pens[PenRed]);
763   HBRUSH old_brush;
764
765   old_brush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
766   Rectangle(hdc, r->left, r->top, r->right, r->bottom);
767
768   SelectObject(hdc, old_brush);
769   SelectObject(hdc, old_pen);
770 }
771
772 /* Draw today day mark rectangle
773  *
774  * [I] hdc  : context to draw in
775  * [I] date : day to mark with rectangle
776  *
777  */
778 static void MONTHCAL_CircleDay(const MONTHCAL_INFO *infoPtr, HDC hdc,
779                                const SYSTEMTIME *date)
780 {
781   RECT r;
782
783   MONTHCAL_GetDayRect(infoPtr, date, &r, -1);
784   MONTHCAL_Circle(infoPtr, hdc, &r);
785 }
786
787 static void MONTHCAL_DrawDay(const MONTHCAL_INFO *infoPtr, HDC hdc, const SYSTEMTIME *st,
788                              int bold, const PAINTSTRUCT *ps)
789 {
790   static const WCHAR fmtW[] = { '%','d',0 };
791   WCHAR buf[10];
792   RECT r, r_temp;
793   COLORREF oldCol = 0;
794   COLORREF oldBk  = 0;
795   INT old_bkmode, selection;
796
797   /* no need to check styles: when selection is not valid, it is set to zero.
798      1 < day < 31, so everything is OK */
799   MONTHCAL_GetDayRect(infoPtr, st, &r, -1);
800   if(!IntersectRect(&r_temp, &(ps->rcPaint), &r)) return;
801
802   if ((MONTHCAL_CompareDate(st, &infoPtr->minSel) >= 0) &&
803       (MONTHCAL_CompareDate(st, &infoPtr->maxSel) <= 0))
804   {
805     TRACE("%d %d %d\n", st->wDay, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
806     TRACE("%s\n", wine_dbgstr_rect(&r));
807     oldCol = SetTextColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
808     oldBk = SetBkColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
809     FillRect(hdc, &r, infoPtr->brushes[BrushTitle]);
810
811     selection = 1;
812   }
813   else
814     selection = 0;
815
816   SelectObject(hdc, bold ? infoPtr->hBoldFont : infoPtr->hFont);
817
818   old_bkmode = SetBkMode(hdc, TRANSPARENT);
819   wsprintfW(buf, fmtW, st->wDay);
820   DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
821   SetBkMode(hdc, old_bkmode);
822
823   if (selection)
824   {
825     SetTextColor(hdc, oldCol);
826     SetBkColor(hdc, oldBk);
827   }
828 }
829
830 static void MONTHCAL_PaintButton(MONTHCAL_INFO *infoPtr, HDC hdc, enum nav_direction button)
831 {
832     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
833     RECT *r = button == DIRECTION_FORWARD ? &infoPtr->titlebtnnext : &infoPtr->titlebtnprev;
834     BOOL pressed = button == DIRECTION_FORWARD ? infoPtr->status & MC_NEXTPRESSED :
835                                                  infoPtr->status & MC_PREVPRESSED;
836     if (theme)
837     {
838         static const int states[] = {
839             /* Prev button */
840             ABS_LEFTNORMAL,  ABS_LEFTPRESSED,  ABS_LEFTDISABLED,
841             /* Next button */
842             ABS_RIGHTNORMAL, ABS_RIGHTPRESSED, ABS_RIGHTDISABLED
843         };
844         int stateNum = button == DIRECTION_FORWARD ? 3 : 0;
845         if (pressed)
846             stateNum += 1;
847         else
848         {
849             if (infoPtr->dwStyle & WS_DISABLED) stateNum += 2;
850         }
851         DrawThemeBackground (theme, hdc, SBP_ARROWBTN, states[stateNum], r, NULL);
852     }
853     else
854     {
855         int style = button == DIRECTION_FORWARD ? DFCS_SCROLLRIGHT : DFCS_SCROLLLEFT;
856         if (pressed)
857             style |= DFCS_PUSHED;
858         else
859         {
860             if (infoPtr->dwStyle & WS_DISABLED) style |= DFCS_INACTIVE;
861         }
862         
863         DrawFrameControl(hdc, r, DFC_SCROLL, style);
864     }
865 }
866
867 /* paint a title with buttons and month/year string */
868 static void MONTHCAL_PaintTitle(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
869 {
870   static const WCHAR fmt_monthW[] = { '%','s',' ','%','l','d',0 };
871   RECT *title = &infoPtr->calendars[calIdx].title;
872   const SYSTEMTIME *st = &infoPtr->calendars[calIdx].month;
873   WCHAR buf_month[80], buf_fmt[80];
874   SIZE sz;
875
876   /* fill header box */
877   FillRect(hdc, title, infoPtr->brushes[BrushTitle]);
878
879   /* month/year string */
880   SetBkColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
881   SetTextColor(hdc, infoPtr->colors[MCSC_TITLETEXT]);
882   SelectObject(hdc, infoPtr->hBoldFont);
883
884   GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1 + st->wMonth - 1,
885                  buf_month, countof(buf_month));
886
887   wsprintfW(buf_fmt, fmt_monthW, buf_month, st->wYear);
888   DrawTextW(hdc, buf_fmt, strlenW(buf_fmt), title,
889                       DT_CENTER | DT_VCENTER | DT_SINGLELINE);
890
891   /* update title rectangles with current month - used while testing hits */
892   GetTextExtentPoint32W(hdc, buf_fmt, strlenW(buf_fmt), &sz);
893   infoPtr->calendars[calIdx].titlemonth.left = title->right / 2 + title->left / 2 - sz.cx / 2;
894   infoPtr->calendars[calIdx].titleyear.right = title->right / 2 + title->left / 2 + sz.cx / 2;
895
896   GetTextExtentPoint32W(hdc, buf_month, strlenW(buf_month), &sz);
897   infoPtr->calendars[calIdx].titlemonth.right = infoPtr->calendars[calIdx].titlemonth.left + sz.cx;
898   infoPtr->calendars[calIdx].titleyear.left   = infoPtr->calendars[calIdx].titlemonth.right;
899 }
900
901 static void MONTHCAL_PaintWeeknumbers(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
902 {
903   const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
904   static const WCHAR fmt_weekW[] = { '%','d',0 };
905   INT mindays, weeknum, weeknum1, startofprescal;
906   INT i, prev_month;
907   SYSTEMTIME st;
908   WCHAR buf[80];
909   HPEN old_pen;
910   RECT r;
911
912   if (!(infoPtr->dwStyle & MCS_WEEKNUMBERS)) return;
913
914   MONTHCAL_GetMinDate(infoPtr, &st);
915   startofprescal = st.wDay;
916   st = *date;
917
918   prev_month = date->wMonth - 1;
919   if(prev_month == 0) prev_month = 12;
920
921   /*
922      Rules what week to call the first week of a new year:
923      LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
924      The week containing Jan 1 is the first week of year
925      LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
926      First week of year must contain 4 days of the new year
927      LOCALE_IFIRSTWEEKOFYEAR == 1  (what countries?)
928      The first week of the year must contain only days of the new year
929   */
930   GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR, buf, countof(buf));
931   weeknum = atoiW(buf);
932   switch (weeknum)
933   {
934     case 1: mindays = 6;
935         break;
936     case 2: mindays = 3;
937         break;
938     case 0: mindays = 0;
939         break;
940     default:
941         WARN("Unknown LOCALE_IFIRSTWEEKOFYEAR value %d, defaulting to 0\n", weeknum);
942         mindays = 0;
943   }
944
945   if (date->wMonth == 1)
946   {
947     /* calculate all those exceptions for January */
948     st.wDay = st.wMonth = 1;
949     weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
950     if ((infoPtr->firstDay - weeknum1) % 7 > mindays)
951         weeknum = 1;
952     else
953     {
954         weeknum = 0;
955         for(i = 0; i < 11; i++)
956            weeknum += MONTHCAL_MonthLength(i+1, date->wYear - 1);
957
958         weeknum  += startofprescal + 7;
959         weeknum  /= 7;
960         st.wYear -= 1;
961         weeknum1  = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
962         if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
963     }
964   }
965   else
966   {
967     weeknum = 0;
968     for(i = 0; i < prev_month - 1; i++)
969         weeknum += MONTHCAL_MonthLength(i+1, date->wYear);
970
971     weeknum += startofprescal + 7;
972     weeknum /= 7;
973     st.wDay = st.wMonth = 1;
974     weeknum1 = MONTHCAL_CalculateDayOfWeek(&st, FALSE);
975     if ((infoPtr->firstDay - weeknum1) % 7 > mindays) weeknum++;
976   }
977
978   r = infoPtr->calendars[calIdx].weeknums;
979
980   /* erase whole week numbers area */
981   FillRect(hdc, &r, infoPtr->brushes[BrushMonth]);
982   SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
983
984   /* reduce rectangle to one week number */
985   r.bottom = r.top + infoPtr->height_increment;
986
987   for(i = 0; i < 6; i++) {
988     if((i == 0) && (weeknum > 50))
989     {
990         wsprintfW(buf, fmt_weekW, weeknum);
991         weeknum = 0;
992     }
993     else if((i == 5) && (weeknum > 47))
994     {
995         wsprintfW(buf, fmt_weekW, 1);
996     }
997     else
998         wsprintfW(buf, fmt_weekW, weeknum + i);
999
1000     DrawTextW(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1001     OffsetRect(&r, 0, infoPtr->height_increment);
1002   }
1003
1004   /* line separator for week numbers column */
1005   old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1006   MoveToEx(hdc, infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.top + 3 , NULL);
1007   LineTo(hdc,   infoPtr->calendars[calIdx].weeknums.right, infoPtr->calendars[calIdx].weeknums.bottom);
1008   SelectObject(hdc, old_pen);
1009 }
1010
1011 /* bottom today date */
1012 static void MONTHCAL_PaintTodayTitle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1013 {
1014   static const WCHAR fmt_todayW[] = { '%','s',' ','%','s',0 };
1015   WCHAR buf_todayW[30], buf_dateW[20], buf[80];
1016   RECT text_rect, box_rect;
1017   HFONT old_font;
1018   INT col;
1019
1020   if(infoPtr->dwStyle & MCS_NOTODAY) return;
1021
1022   if (!LoadStringW(COMCTL32_hModule, IDM_TODAY, buf_todayW, countof(buf_todayW)))
1023   {
1024     static const WCHAR todayW[] = { 'T','o','d','a','y',':',0 };
1025     WARN("Can't load resource\n");
1026     strcpyW(buf_todayW, todayW);
1027   }
1028
1029   col = infoPtr->dwStyle & MCS_NOTODAYCIRCLE ? 0 : 1;
1030   if (infoPtr->dwStyle & MCS_WEEKNUMBERS) col--;
1031   /* label is located below first calendar last row */
1032   MONTHCAL_GetDayRectI(infoPtr, &text_rect, col, 6, infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx);
1033   box_rect = text_rect;
1034
1035   GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &infoPtr->todaysDate, NULL,
1036                                                       buf_dateW, countof(buf_dateW));
1037   old_font = SelectObject(hdc, infoPtr->hBoldFont);
1038   SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1039
1040   wsprintfW(buf, fmt_todayW, buf_todayW, buf_dateW);
1041   DrawTextW(hdc, buf, -1, &text_rect, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1042   DrawTextW(hdc, buf, -1, &text_rect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
1043
1044   if(!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE)) {
1045     OffsetRect(&box_rect, -infoPtr->width_increment, 0);
1046     MONTHCAL_Circle(infoPtr, hdc, &box_rect);
1047   }
1048
1049   SelectObject(hdc, old_font);
1050 }
1051
1052 /* today mark + focus */
1053 static void MONTHCAL_PaintFocusAndCircle(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1054 {
1055   /* circle today date if only it's in fully visible month */
1056   if (!(infoPtr->dwStyle & MCS_NOTODAYCIRCLE))
1057   {
1058     INT i;
1059
1060     for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1061       if (!MONTHCAL_CompareMonths(&infoPtr->todaysDate, &infoPtr->calendars[i].month))
1062       {
1063         MONTHCAL_CircleDay(infoPtr, hdc, &infoPtr->todaysDate);
1064         break;
1065       }
1066   }
1067
1068   if (!MONTHCAL_IsDateEqual(&infoPtr->focusedSel, &st_null))
1069   {
1070     RECT r;
1071     MONTHCAL_GetDayRect(infoPtr, &infoPtr->focusedSel, &r, -1);
1072     DrawFocusRect(hdc, &r);
1073   }
1074 }
1075
1076 /* months before first calendar month and after last calendar month */
1077 static void MONTHCAL_PaintLeadTrailMonths(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1078 {
1079   INT mask, length, index;
1080   SYSTEMTIME st_max, st;
1081
1082   if (infoPtr->dwStyle & MCS_NOTRAILINGDATES) return;
1083
1084   SetTextColor(hdc, infoPtr->colors[MCSC_TRAILINGTEXT]);
1085
1086   /* draw prev month */
1087   MONTHCAL_GetMinDate(infoPtr, &st);
1088   mask = 1 << (st.wDay-1);
1089   /* December and January both 31 days long, so no worries if wrapped */
1090   length = MONTHCAL_MonthLength(infoPtr->calendars[0].month.wMonth - 1,
1091                                 infoPtr->calendars[0].month.wYear);
1092   index = 0;
1093   while(st.wDay <= length)
1094   {
1095       MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1096       mask <<= 1;
1097       st.wDay++;
1098   }
1099
1100   /* draw next month */
1101   st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1102   st.wDay = 1;
1103   MONTHCAL_GetNextMonth(&st);
1104   MONTHCAL_GetMaxDate(infoPtr, &st_max);
1105   mask = 1;
1106   index = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)-1;
1107   while(st.wDay <= st_max.wDay)
1108   {
1109       MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[index] & mask, ps);
1110       mask <<= 1;
1111       st.wDay++;
1112   }
1113 }
1114
1115 /* paint a calendar area */
1116 static void MONTHCAL_PaintCalendar(const MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps, INT calIdx)
1117 {
1118   const SYSTEMTIME *date = &infoPtr->calendars[calIdx].month;
1119   INT i, j, length;
1120   RECT r, fill_bk_rect;
1121   SYSTEMTIME st;
1122   WCHAR buf[80];
1123   HPEN old_pen;
1124   int mask;
1125
1126   /* fill whole days area - from week days area to today note rectangle */
1127   fill_bk_rect = infoPtr->calendars[calIdx].wdays;
1128   fill_bk_rect.bottom = infoPtr->calendars[calIdx].days.bottom +
1129                           (infoPtr->todayrect.bottom - infoPtr->todayrect.top);
1130
1131   FillRect(hdc, &fill_bk_rect, infoPtr->brushes[BrushMonth]);
1132
1133   /* draw line under day abbreviations */
1134   old_pen = SelectObject(hdc, infoPtr->pens[PenText]);
1135   MoveToEx(hdc, infoPtr->calendars[calIdx].days.left + 3,
1136                 infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1, NULL);
1137   LineTo(hdc, infoPtr->calendars[calIdx].days.right - 3,
1138               infoPtr->calendars[calIdx].title.bottom + infoPtr->textHeight + 1);
1139   SelectObject(hdc, old_pen);
1140
1141   infoPtr->calendars[calIdx].wdays.left = infoPtr->calendars[calIdx].days.left =
1142       infoPtr->calendars[calIdx].weeknums.right;
1143
1144   /* draw day abbreviations */
1145   SelectObject(hdc, infoPtr->hFont);
1146   SetBkColor(hdc, infoPtr->colors[MCSC_MONTHBK]);
1147   SetTextColor(hdc, infoPtr->colors[MCSC_TITLEBK]);
1148   /* rectangle to draw a single day abbreviation within */
1149   r = infoPtr->calendars[calIdx].wdays;
1150   r.right = r.left + infoPtr->width_increment;
1151
1152   i = infoPtr->firstDay;
1153   for(j = 0; j < 7; j++) {
1154     GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + (i+j+6)%7, buf, countof(buf));
1155     DrawTextW(hdc, buf, strlenW(buf), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
1156     OffsetRect(&r, infoPtr->width_increment, 0);
1157   }
1158
1159   /* draw current month */
1160   SetTextColor(hdc, infoPtr->colors[MCSC_TEXT]);
1161   st = *date;
1162   st.wDay = 1;
1163   mask = 1;
1164   length = MONTHCAL_MonthLength(date->wMonth, date->wYear);
1165   while(st.wDay <= length)
1166   {
1167     MONTHCAL_DrawDay(infoPtr, hdc, &st, infoPtr->monthdayState[calIdx+1] & mask, ps);
1168     mask <<= 1;
1169     st.wDay++;
1170   }
1171 }
1172
1173 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, const PAINTSTRUCT *ps)
1174 {
1175   COLORREF old_text_clr, old_bk_clr;
1176   HFONT old_font;
1177   INT i;
1178
1179   old_text_clr = SetTextColor(hdc, comctl32_color.clrWindowText);
1180   old_bk_clr   = GetBkColor(hdc);
1181   old_font     = GetCurrentObject(hdc, OBJ_FONT);
1182
1183   for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1184   {
1185     RECT *title = &infoPtr->calendars[i].title;
1186     RECT r;
1187
1188     /* draw title, redraw all its elements */
1189     if (IntersectRect(&r, &(ps->rcPaint), title))
1190         MONTHCAL_PaintTitle(infoPtr, hdc, ps, i);
1191
1192     /* draw calendar area */
1193     UnionRect(&r, &infoPtr->calendars[i].wdays, &infoPtr->todayrect);
1194     if (IntersectRect(&r, &(ps->rcPaint), &r))
1195         MONTHCAL_PaintCalendar(infoPtr, hdc, ps, i);
1196
1197     /* week numbers */
1198     MONTHCAL_PaintWeeknumbers(infoPtr, hdc, ps, i);
1199   }
1200
1201   /* partially visible months */
1202   MONTHCAL_PaintLeadTrailMonths(infoPtr, hdc, ps);
1203
1204   /* focus and today rectangle */
1205   MONTHCAL_PaintFocusAndCircle(infoPtr, hdc, ps);
1206
1207   /* today at the bottom left */
1208   MONTHCAL_PaintTodayTitle(infoPtr, hdc, ps);
1209
1210   /* navigation buttons */
1211   MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_BACKWARD);
1212   MONTHCAL_PaintButton(infoPtr, hdc, DIRECTION_FORWARD);
1213
1214   /* restore context */
1215   SetBkColor(hdc, old_bk_clr);
1216   SelectObject(hdc, old_font);
1217   SetTextColor(hdc, old_text_clr);
1218 }
1219
1220 static LRESULT
1221 MONTHCAL_GetMinReqRect(const MONTHCAL_INFO *infoPtr, RECT *rect)
1222 {
1223   TRACE("rect %p\n", rect);
1224
1225   if(!rect) return FALSE;
1226
1227   *rect = infoPtr->calendars[0].title;
1228   rect->bottom = infoPtr->calendars[0].days.bottom + infoPtr->todayrect.bottom -
1229                  infoPtr->todayrect.top;
1230
1231   AdjustWindowRect(rect, infoPtr->dwStyle, FALSE);
1232
1233   /* minimal rectangle is zero based */
1234   OffsetRect(rect, -rect->left, -rect->top);
1235
1236   TRACE("%s\n", wine_dbgstr_rect(rect));
1237
1238   return TRUE;
1239 }
1240
1241 static COLORREF
1242 MONTHCAL_GetColor(const MONTHCAL_INFO *infoPtr, UINT index)
1243 {
1244   TRACE("%p, %d\n", infoPtr, index);
1245
1246   if (index > MCSC_TRAILINGTEXT) return -1;
1247   return infoPtr->colors[index];
1248 }
1249
1250 static LRESULT
1251 MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, UINT index, COLORREF color)
1252 {
1253   enum CachedBrush type;
1254   COLORREF prev;
1255
1256   TRACE("%p, %d: color %08x\n", infoPtr, index, color);
1257
1258   if (index > MCSC_TRAILINGTEXT) return -1;
1259
1260   prev = infoPtr->colors[index];
1261   infoPtr->colors[index] = color;
1262
1263   /* update cached brush */
1264   switch (index)
1265   {
1266   case MCSC_BACKGROUND:
1267     type = BrushBackground;
1268     break;
1269   case MCSC_TITLEBK:
1270     type = BrushTitle;
1271     break;
1272   case MCSC_MONTHBK:
1273     type = BrushMonth;
1274     break;
1275   default:
1276     type = BrushLast;
1277   }
1278
1279   if (type != BrushLast)
1280   {
1281     DeleteObject(infoPtr->brushes[type]);
1282     infoPtr->brushes[type] = CreateSolidBrush(color);
1283   }
1284
1285   /* update cached pen */
1286   if (index == MCSC_TEXT)
1287   {
1288     DeleteObject(infoPtr->pens[PenText]);
1289     infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[index]);
1290   }
1291
1292   InvalidateRect(infoPtr->hwndSelf, NULL, index == MCSC_BACKGROUND);
1293   return prev;
1294 }
1295
1296 static LRESULT
1297 MONTHCAL_GetMonthDelta(const MONTHCAL_INFO *infoPtr)
1298 {
1299   TRACE("\n");
1300
1301   if(infoPtr->delta)
1302     return infoPtr->delta;
1303   else
1304     return infoPtr->visible;
1305 }
1306
1307
1308 static LRESULT
1309 MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, INT delta)
1310 {
1311   INT prev = infoPtr->delta;
1312
1313   TRACE("delta %d\n", delta);
1314
1315   infoPtr->delta = delta;
1316   return prev;
1317 }
1318
1319
1320 static inline LRESULT
1321 MONTHCAL_GetFirstDayOfWeek(const MONTHCAL_INFO *infoPtr)
1322 {
1323   int day;
1324
1325   /* convert from SYSTEMTIME to locale format */
1326   day = (infoPtr->firstDay >= 0) ? (infoPtr->firstDay+6)%7 : infoPtr->firstDay;
1327
1328   return MAKELONG(day, infoPtr->firstDaySet);
1329 }
1330
1331
1332 /* Sets the first day of the week that will appear in the control
1333  *
1334  *
1335  * PARAMETERS:
1336  *  [I] infoPtr : valid pointer to control data
1337  *  [I] day : day number to set as new first day (0 == Monday,...,6 == Sunday)
1338  *
1339  *
1340  * RETURN VALUE:
1341  *  Low word contains previous first day,
1342  *  high word indicates was first day forced with this message before or is
1343  *  locale defined (TRUE - was forced, FALSE - wasn't).
1344  *
1345  * FIXME: this needs to be implemented properly in MONTHCAL_Refresh()
1346  * FIXME: we need more error checking here
1347  */
1348 static LRESULT
1349 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, INT day)
1350 {
1351   LRESULT prev = MONTHCAL_GetFirstDayOfWeek(infoPtr);
1352   int new_day;
1353
1354   TRACE("%d\n", day);
1355
1356   if(day == -1)
1357   {
1358     WCHAR buf[80];
1359
1360     GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, buf, countof(buf));
1361     TRACE("%s %d\n", debugstr_w(buf), strlenW(buf));
1362
1363     new_day = atoiW(buf);
1364
1365     infoPtr->firstDaySet = FALSE;
1366   }
1367   else if(day >= 7)
1368   {
1369     new_day = 6; /* max first day allowed */
1370     infoPtr->firstDaySet = TRUE;
1371   }
1372   else
1373   {
1374     /* Native behaviour for that case is broken: invalid date number >31
1375        got displayed at (0,0) position, current month starts always from
1376        (1,0) position. Should be implemented here as well only if there's
1377        nothing else to do. */
1378     if (day < -1)
1379       FIXME("No bug compatibility for day=%d\n", day);
1380
1381     new_day = day;
1382     infoPtr->firstDaySet = TRUE;
1383   }
1384
1385   /* convert from locale to SYSTEMTIME format */
1386   infoPtr->firstDay = (new_day >= 0) ? (++new_day) % 7 : new_day;
1387
1388   InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1389
1390   return prev;
1391 }
1392
1393 static LRESULT
1394 MONTHCAL_GetMaxTodayWidth(const MONTHCAL_INFO *infoPtr)
1395 {
1396   return(infoPtr->todayrect.right - infoPtr->todayrect.left);
1397 }
1398
1399 static LRESULT
1400 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, SHORT limits, SYSTEMTIME *range)
1401 {
1402     FILETIME ft_min, ft_max;
1403
1404     TRACE("%x %p\n", limits, range);
1405
1406     if ((limits & GDTR_MIN && !MONTHCAL_ValidateDate(&range[0])) ||
1407         (limits & GDTR_MAX && !MONTHCAL_ValidateDate(&range[1])))
1408         return FALSE;
1409
1410     if (limits & GDTR_MIN)
1411     {
1412         if (!MONTHCAL_ValidateTime(&range[0]))
1413             MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1414
1415         infoPtr->minDate = range[0];
1416         infoPtr->rangeValid |= GDTR_MIN;
1417     }
1418     if (limits & GDTR_MAX)
1419     {
1420         if (!MONTHCAL_ValidateTime(&range[1]))
1421             MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1422
1423         infoPtr->maxDate = range[1];
1424         infoPtr->rangeValid |= GDTR_MAX;
1425     }
1426
1427     /* Only one limit set - we are done */
1428     if ((infoPtr->rangeValid & (GDTR_MIN | GDTR_MAX)) != (GDTR_MIN | GDTR_MAX))
1429         return TRUE;
1430
1431     SystemTimeToFileTime(&infoPtr->maxDate, &ft_max);
1432     SystemTimeToFileTime(&infoPtr->minDate, &ft_min);
1433
1434     if (CompareFileTime(&ft_min, &ft_max) >= 0)
1435     {
1436         if ((limits & (GDTR_MIN | GDTR_MAX)) == (GDTR_MIN | GDTR_MAX))
1437         {
1438             /* Native swaps limits only when both limits are being set. */
1439             SYSTEMTIME st_tmp = infoPtr->minDate;
1440             infoPtr->minDate  = infoPtr->maxDate;
1441             infoPtr->maxDate  = st_tmp;
1442         }
1443         else
1444         {
1445             /* reset the other limit */
1446             if (limits & GDTR_MIN) infoPtr->maxDate = st_null;
1447             if (limits & GDTR_MAX) infoPtr->minDate = st_null;
1448             infoPtr->rangeValid &= limits & GDTR_MIN ? ~GDTR_MAX : ~GDTR_MIN;
1449         }
1450     }
1451
1452     return TRUE;
1453 }
1454
1455
1456 static LRESULT
1457 MONTHCAL_GetRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1458 {
1459   TRACE("%p\n", range);
1460
1461   if(!range) return FALSE;
1462
1463   range[1] = infoPtr->maxDate;
1464   range[0] = infoPtr->minDate;
1465
1466   return infoPtr->rangeValid;
1467 }
1468
1469
1470 static LRESULT
1471 MONTHCAL_SetDayState(const MONTHCAL_INFO *infoPtr, INT months, MONTHDAYSTATE *states)
1472 {
1473   TRACE("%p %d %p\n", infoPtr, months, states);
1474
1475   if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return 0;
1476   if (months != MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)) return 0;
1477
1478   memcpy(infoPtr->monthdayState, states, months*sizeof(MONTHDAYSTATE));
1479
1480   return 1;
1481 }
1482
1483 static LRESULT
1484 MONTHCAL_GetCurSel(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1485 {
1486   TRACE("%p\n", curSel);
1487   if(!curSel) return FALSE;
1488   if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1489
1490   *curSel = infoPtr->minSel;
1491   TRACE("%d/%d/%d\n", curSel->wYear, curSel->wMonth, curSel->wDay);
1492   return TRUE;
1493 }
1494
1495 static LRESULT
1496 MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, SYSTEMTIME *curSel)
1497 {
1498   SYSTEMTIME prev = infoPtr->minSel, selection;
1499   INT diff;
1500   WORD day;
1501
1502   TRACE("%p\n", curSel);
1503   if(!curSel) return FALSE;
1504   if(infoPtr->dwStyle & MCS_MULTISELECT) return FALSE;
1505
1506   if(!MONTHCAL_ValidateDate(curSel)) return FALSE;
1507   /* exit earlier if selection equals current */
1508   if (MONTHCAL_IsDateEqual(&infoPtr->minSel, curSel)) return TRUE;
1509
1510   selection = *curSel;
1511   selection.wHour = selection.wMinute = selection.wSecond = selection.wMilliseconds = 0;
1512   MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1513
1514   if(!MONTHCAL_IsDateInValidRange(infoPtr, &selection, FALSE)) return FALSE;
1515
1516   /* scroll calendars only if we have to */
1517   diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, curSel);
1518   if (diff <= 0)
1519   {
1520     diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, curSel);
1521     if (diff > 0) diff = 0;
1522   }
1523
1524   if (diff != 0)
1525   {
1526     INT i;
1527
1528     for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1529       MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1530   }
1531
1532   /* we need to store time part as it is */
1533   selection = *curSel;
1534   MONTHCAL_CalculateDayOfWeek(&selection, TRUE);
1535   infoPtr->minSel = infoPtr->maxSel = selection;
1536
1537   /* if selection is still in current month, reduce rectangle */
1538   day = prev.wDay;
1539   prev.wDay = curSel->wDay;
1540   if (MONTHCAL_IsDateEqual(&prev, curSel))
1541   {
1542     RECT r_prev, r_new;
1543
1544     prev.wDay = day;
1545     MONTHCAL_GetDayRect(infoPtr, &prev, &r_prev, -1);
1546     MONTHCAL_GetDayRect(infoPtr, curSel, &r_new, -1);
1547
1548     InvalidateRect(infoPtr->hwndSelf, &r_prev, FALSE);
1549     InvalidateRect(infoPtr->hwndSelf, &r_new,  FALSE);
1550   }
1551   else
1552     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1553
1554   return TRUE;
1555 }
1556
1557
1558 static LRESULT
1559 MONTHCAL_GetMaxSelCount(const MONTHCAL_INFO *infoPtr)
1560 {
1561   return infoPtr->maxSelCount;
1562 }
1563
1564
1565 static LRESULT
1566 MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, INT max)
1567 {
1568   TRACE("%d\n", max);
1569
1570   if(!(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1571   if(max <= 0) return FALSE;
1572
1573   infoPtr->maxSelCount = max;
1574
1575   return TRUE;
1576 }
1577
1578
1579 static LRESULT
1580 MONTHCAL_GetSelRange(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1581 {
1582   TRACE("%p\n", range);
1583
1584   if(!range) return FALSE;
1585
1586   if(infoPtr->dwStyle & MCS_MULTISELECT)
1587   {
1588     range[1] = infoPtr->maxSel;
1589     range[0] = infoPtr->minSel;
1590     TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1591     return TRUE;
1592   }
1593
1594   return FALSE;
1595 }
1596
1597
1598 static LRESULT
1599 MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, SYSTEMTIME *range)
1600 {
1601   SYSTEMTIME old_range[2];
1602   INT diff;
1603
1604   TRACE("%p\n", range);
1605
1606   if(!range || !(infoPtr->dwStyle & MCS_MULTISELECT)) return FALSE;
1607
1608   /* adjust timestamps */
1609   if(!MONTHCAL_ValidateTime(&range[0])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[0]);
1610   if(!MONTHCAL_ValidateTime(&range[1])) MONTHCAL_CopyTime(&infoPtr->todaysDate, &range[1]);
1611
1612   /* maximum range exceeded */
1613   if(!MONTHCAL_IsSelRangeValid(infoPtr, &range[0], &range[1], NULL)) return FALSE;
1614
1615   old_range[0] = infoPtr->minSel;
1616   old_range[1] = infoPtr->maxSel;
1617
1618   /* swap if min > max */
1619   if(MONTHCAL_CompareSystemTime(&range[0], &range[1]) <= 0)
1620   {
1621     infoPtr->minSel = range[0];
1622     infoPtr->maxSel = range[1];
1623   }
1624   else
1625   {
1626     infoPtr->minSel = range[1];
1627     infoPtr->maxSel = range[0];
1628   }
1629
1630   diff = MONTHCAL_MonthDiff(&infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month, &infoPtr->maxSel);
1631   if (diff < 0)
1632   {
1633     diff = MONTHCAL_MonthDiff(&infoPtr->calendars[0].month, &infoPtr->maxSel);
1634     if (diff > 0) diff = 0;
1635   }
1636
1637   if (diff != 0)
1638   {
1639     INT i;
1640
1641     for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1642       MONTHCAL_GetMonth(&infoPtr->calendars[i].month, diff);
1643   }
1644
1645   /* update day of week */
1646   MONTHCAL_CalculateDayOfWeek(&infoPtr->minSel, TRUE);
1647   MONTHCAL_CalculateDayOfWeek(&infoPtr->maxSel, TRUE);
1648
1649   /* redraw if bounds changed */
1650   /* FIXME: no actual need to redraw everything */
1651   if(!MONTHCAL_IsDateEqual(&old_range[0], &range[0]) ||
1652      !MONTHCAL_IsDateEqual(&old_range[1], &range[1]))
1653   {
1654      InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1655   }
1656
1657   TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1658   return TRUE;
1659 }
1660
1661
1662 static LRESULT
1663 MONTHCAL_GetToday(const MONTHCAL_INFO *infoPtr, SYSTEMTIME *today)
1664 {
1665   TRACE("%p\n", today);
1666
1667   if(!today) return FALSE;
1668   *today = infoPtr->todaysDate;
1669   return TRUE;
1670 }
1671
1672 /* Internal helper for MCM_SETTODAY handler and auto update timer handler
1673  *
1674  * RETURN VALUE
1675  *
1676  *  TRUE  - today date changed
1677  *  FALSE - today date isn't changed
1678  */
1679 static BOOL
1680 MONTHCAL_UpdateToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1681 {
1682   RECT new_r, old_r;
1683
1684   if(MONTHCAL_IsDateEqual(today, &infoPtr->todaysDate)) return FALSE;
1685
1686   MONTHCAL_GetDayRect(infoPtr, &infoPtr->todaysDate, &old_r, -1);
1687   MONTHCAL_GetDayRect(infoPtr, today, &new_r, -1);
1688
1689   infoPtr->todaysDate = *today;
1690
1691   /* only two days need redrawing */
1692   InvalidateRect(infoPtr->hwndSelf, &old_r, FALSE);
1693   InvalidateRect(infoPtr->hwndSelf, &new_r, FALSE);
1694   /* and today label */
1695   InvalidateRect(infoPtr->hwndSelf, &infoPtr->todayrect, FALSE);
1696   return TRUE;
1697 }
1698
1699 /* MCM_SETTODAT handler */
1700 static LRESULT
1701 MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, const SYSTEMTIME *today)
1702 {
1703   TRACE("%p\n", today);
1704
1705   if (today)
1706   {
1707     /* remember if date was set successfully */
1708     if (MONTHCAL_UpdateToday(infoPtr, today)) infoPtr->todaySet = TRUE;
1709   }
1710
1711   return 0;
1712 }
1713
1714 /* returns calendar index containing specified point, or -1 if it's background */
1715 static INT MONTHCAL_GetCalendarFromPoint(const MONTHCAL_INFO *infoPtr, const POINT *pt)
1716 {
1717   RECT r;
1718   INT i;
1719
1720   for (i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1721   {
1722      /* whole bounding rectangle allows some optimization to compute */
1723      r.left   = infoPtr->calendars[i].title.left;
1724      r.top    = infoPtr->calendars[i].title.top;
1725      r.bottom = infoPtr->calendars[i].days.bottom;
1726      r.right  = infoPtr->calendars[i].days.right;
1727
1728      if (PtInRect(&r, *pt)) return i;
1729   }
1730
1731   return -1;
1732 }
1733
1734 static inline UINT fill_hittest_info(const MCHITTESTINFO *src, MCHITTESTINFO *dest)
1735 {
1736   dest->uHit = src->uHit;
1737   dest->st = src->st;
1738
1739   if (dest->cbSize == sizeof(MCHITTESTINFO))
1740     memcpy(&dest->rc, &src->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1741
1742   return src->uHit;
1743 }
1744
1745 static LRESULT
1746 MONTHCAL_HitTest(const MONTHCAL_INFO *infoPtr, MCHITTESTINFO *lpht)
1747 {
1748   MCHITTESTINFO htinfo;
1749   SYSTEMTIME *ht_month;
1750   INT day, calIdx;
1751
1752   if(!lpht || lpht->cbSize < MCHITTESTINFO_V1_SIZE) return -1;
1753
1754   htinfo.st = st_null;
1755
1756   /* we should preserve passed fields if hit area doesn't need them */
1757   if (lpht->cbSize == sizeof(MCHITTESTINFO))
1758     memcpy(&htinfo.rc, &lpht->rc, sizeof(MCHITTESTINFO) - MCHITTESTINFO_V1_SIZE);
1759
1760   /* Comment in for debugging...
1761   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,
1762         infoPtr->wdays.left, infoPtr->wdays.right,
1763         infoPtr->wdays.top, infoPtr->wdays.bottom,
1764         infoPtr->days.left, infoPtr->days.right,
1765         infoPtr->days.top, infoPtr->days.bottom,
1766         infoPtr->todayrect.left, infoPtr->todayrect.right,
1767         infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1768         infoPtr->weeknums.left, infoPtr->weeknums.right,
1769         infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1770   */
1771
1772   /* guess in what calendar we are */
1773   calIdx = MONTHCAL_GetCalendarFromPoint(infoPtr, &lpht->pt);
1774   if (calIdx == -1)
1775   {
1776     if (PtInRect(&infoPtr->todayrect, lpht->pt))
1777     {
1778       htinfo.uHit = MCHT_TODAYLINK;
1779       htinfo.rc = infoPtr->todayrect;
1780     }
1781     else
1782       /* outside of calendar area? What's left must be background :-) */
1783       htinfo.uHit = MCHT_CALENDARBK;
1784
1785     return fill_hittest_info(&htinfo, lpht);
1786   }
1787
1788   /* are we in the header? */
1789   if (PtInRect(&infoPtr->calendars[calIdx].title, lpht->pt)) {
1790     /* FIXME: buttons hittesting could be optimized cause maximum
1791               two calendars have buttons */
1792     if (calIdx == 0 && PtInRect(&infoPtr->titlebtnprev, lpht->pt))
1793     {
1794       htinfo.uHit = MCHT_TITLEBTNPREV;
1795       htinfo.rc = infoPtr->titlebtnprev;
1796     }
1797     else if (PtInRect(&infoPtr->titlebtnnext, lpht->pt))
1798     {
1799       htinfo.uHit = MCHT_TITLEBTNNEXT;
1800       htinfo.rc = infoPtr->titlebtnnext;
1801     }
1802     else if (PtInRect(&infoPtr->calendars[calIdx].titlemonth, lpht->pt))
1803     {
1804       htinfo.uHit = MCHT_TITLEMONTH;
1805       htinfo.rc = infoPtr->calendars[calIdx].titlemonth;
1806       htinfo.iOffset = calIdx;
1807     }
1808     else if (PtInRect(&infoPtr->calendars[calIdx].titleyear, lpht->pt))
1809     {
1810       htinfo.uHit = MCHT_TITLEYEAR;
1811       htinfo.rc = infoPtr->calendars[calIdx].titleyear;
1812       htinfo.iOffset = calIdx;
1813     }
1814     else
1815     {
1816       htinfo.uHit = MCHT_TITLE;
1817       htinfo.rc = infoPtr->calendars[calIdx].title;
1818       htinfo.iOffset = calIdx;
1819     }
1820
1821     return fill_hittest_info(&htinfo, lpht);
1822   }
1823
1824   ht_month = &infoPtr->calendars[calIdx].month;
1825   /* days area (including week days and week numbers) */
1826   day = MONTHCAL_GetDayFromPos(infoPtr, lpht->pt, calIdx);
1827   if (PtInRect(&infoPtr->calendars[calIdx].wdays, lpht->pt))
1828   {
1829     htinfo.uHit = MCHT_CALENDARDAY;
1830     htinfo.iOffset = calIdx;
1831     htinfo.st.wYear  = ht_month->wYear;
1832     htinfo.st.wMonth = (day < 1) ? ht_month->wMonth -1 : ht_month->wMonth;
1833     htinfo.st.wDay   = (day < 1) ?
1834       MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day : day;
1835
1836     MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1837   }
1838   else if(PtInRect(&infoPtr->calendars[calIdx].weeknums, lpht->pt))
1839   {
1840     htinfo.uHit = MCHT_CALENDARWEEKNUM;
1841     htinfo.st.wYear = ht_month->wYear;
1842     htinfo.iOffset = calIdx;
1843
1844     if (day < 1)
1845     {
1846       htinfo.st.wMonth = ht_month->wMonth - 1;
1847       htinfo.st.wDay = MONTHCAL_MonthLength(ht_month->wMonth-1, ht_month->wYear) - day;
1848     }
1849     else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1850     {
1851       htinfo.st.wMonth = ht_month->wMonth + 1;
1852       htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1853     }
1854     else
1855     {
1856       htinfo.st.wMonth = ht_month->wMonth;
1857       htinfo.st.wDay = day;
1858     }
1859   }
1860   else if(PtInRect(&infoPtr->calendars[calIdx].days, lpht->pt))
1861   {
1862       htinfo.iOffset = calIdx;
1863       htinfo.st.wYear  = ht_month->wYear;
1864       htinfo.st.wMonth = ht_month->wMonth;
1865       /* previous month only valid for first calendar */
1866       if (day < 1 && calIdx == 0)
1867       {
1868           htinfo.uHit = MCHT_CALENDARDATEPREV;
1869           MONTHCAL_GetPrevMonth(&htinfo.st);
1870           htinfo.st.wDay = MONTHCAL_MonthLength(htinfo.st.wMonth, htinfo.st.wYear) + day;
1871       }
1872       /* next month only valid for last calendar */
1873       else if (day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear) &&
1874                calIdx == MONTHCAL_GetCalCount(infoPtr)-1)
1875       {
1876           htinfo.uHit = MCHT_CALENDARDATENEXT;
1877           MONTHCAL_GetNextMonth(&htinfo.st);
1878           htinfo.st.wDay = day - MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear);
1879       }
1880       /* multiple calendars case - blank areas for previous/next month */
1881       else if (day < 1 || day > MONTHCAL_MonthLength(ht_month->wMonth, ht_month->wYear))
1882       {
1883           htinfo.uHit = MCHT_CALENDARBK;
1884       }
1885       else
1886       {
1887         htinfo.uHit = MCHT_CALENDARDATE;
1888         htinfo.st.wDay = day;
1889       }
1890
1891       MONTHCAL_GetDayPos(infoPtr, &htinfo.st, &htinfo.iCol, &htinfo.iRow, calIdx);
1892       MONTHCAL_GetDayRectI(infoPtr, &htinfo.rc, htinfo.iCol, htinfo.iRow, calIdx);
1893       /* always update day of week */
1894       MONTHCAL_CalculateDayOfWeek(&htinfo.st, TRUE);
1895   }
1896
1897   return fill_hittest_info(&htinfo, lpht);
1898 }
1899
1900 /* MCN_GETDAYSTATE notification helper */
1901 static void MONTHCAL_NotifyDayState(MONTHCAL_INFO *infoPtr)
1902 {
1903   MONTHDAYSTATE *state;
1904   NMDAYSTATE nmds;
1905
1906   if (!(infoPtr->dwStyle & MCS_DAYSTATE)) return;
1907
1908   nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1909   nmds.nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1910   nmds.nmhdr.code     = MCN_GETDAYSTATE;
1911   nmds.cDayState      = MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0);
1912   nmds.prgDayState    = state = Alloc(nmds.cDayState * sizeof(MONTHDAYSTATE));
1913
1914   MONTHCAL_GetMinDate(infoPtr, &nmds.stStart);
1915   nmds.stStart.wDay = 1;
1916
1917   SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmds.nmhdr.idFrom, (LPARAM)&nmds);
1918   memcpy(infoPtr->monthdayState, nmds.prgDayState,
1919       MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
1920
1921   Free(state);
1922 }
1923
1924 /* no valid range check performed */
1925 static void MONTHCAL_Scroll(MONTHCAL_INFO *infoPtr, INT delta)
1926 {
1927   INT i, selIdx = -1;
1928
1929   for(i = 0; i < MONTHCAL_GetCalCount(infoPtr); i++)
1930   {
1931     /* save selection position to shift it later */
1932     if (selIdx == -1 && MONTHCAL_CompareMonths(&infoPtr->minSel, &infoPtr->calendars[i].month) == 0)
1933       selIdx = i;
1934
1935     MONTHCAL_GetMonth(&infoPtr->calendars[i].month, delta);
1936   }
1937
1938   /* selection is always shifted to first calendar */
1939   if(infoPtr->dwStyle & MCS_MULTISELECT)
1940   {
1941     SYSTEMTIME range[2];
1942
1943     MONTHCAL_GetSelRange(infoPtr, range);
1944     MONTHCAL_GetMonth(&range[0], delta - selIdx);
1945     MONTHCAL_GetMonth(&range[1], delta - selIdx);
1946     MONTHCAL_SetSelRange(infoPtr, range);
1947   }
1948   else
1949   {
1950     SYSTEMTIME st = infoPtr->minSel;
1951
1952     MONTHCAL_GetMonth(&st, delta - selIdx);
1953     MONTHCAL_SetCurSel(infoPtr, &st);
1954   }
1955 }
1956
1957 static void MONTHCAL_GoToMonth(MONTHCAL_INFO *infoPtr, enum nav_direction direction)
1958 {
1959   INT delta = infoPtr->delta ? infoPtr->delta : MONTHCAL_GetCalCount(infoPtr);
1960   SYSTEMTIME st;
1961
1962   TRACE("%s\n", direction == DIRECTION_BACKWARD ? "back" : "fwd");
1963
1964   /* check if change allowed by range set */
1965   if(direction == DIRECTION_BACKWARD)
1966   {
1967     st = infoPtr->calendars[0].month;
1968     MONTHCAL_GetMonth(&st, -delta);
1969   }
1970   else
1971   {
1972     st = infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
1973     MONTHCAL_GetMonth(&st, delta);
1974   }
1975
1976   if(!MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE)) return;
1977
1978   MONTHCAL_Scroll(infoPtr, direction == DIRECTION_BACKWARD ? -delta : delta);
1979   MONTHCAL_NotifyDayState(infoPtr);
1980   MONTHCAL_NotifySelectionChange(infoPtr);
1981 }
1982
1983 static LRESULT
1984 MONTHCAL_RButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1985 {
1986   static const WCHAR todayW[] = { 'G','o',' ','t','o',' ','T','o','d','a','y',':',0 };
1987   HMENU hMenu;
1988   POINT menupoint;
1989   WCHAR buf[32];
1990
1991   hMenu = CreatePopupMenu();
1992   if (!LoadStringW(COMCTL32_hModule, IDM_GOTODAY, buf, countof(buf)))
1993   {
1994       WARN("Can't load resource\n");
1995       strcpyW(buf, todayW);
1996   }
1997   AppendMenuW(hMenu, MF_STRING|MF_ENABLED, 1, buf);
1998   menupoint.x = (short)LOWORD(lParam);
1999   menupoint.y = (short)HIWORD(lParam);
2000   ClientToScreen(infoPtr->hwndSelf, &menupoint);
2001   if( TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD,
2002                      menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
2003   {
2004       if (infoPtr->dwStyle & MCS_MULTISELECT)
2005       {
2006           SYSTEMTIME range[2];
2007
2008           range[0] = range[1] = infoPtr->todaysDate;
2009           MONTHCAL_SetSelRange(infoPtr, range);
2010       }
2011       else
2012           MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2013
2014       MONTHCAL_NotifySelectionChange(infoPtr);
2015       MONTHCAL_NotifySelect(infoPtr);
2016   }
2017
2018   return 0;
2019 }
2020
2021 /***
2022  * DESCRIPTION:
2023  * Subclassed edit control windproc function
2024  *
2025  * PARAMETER(S):
2026  * [I] hwnd : the edit window handle
2027  * [I] uMsg : the message that is to be processed
2028  * [I] wParam : first message parameter
2029  * [I] lParam : second message parameter
2030  *
2031  */
2032 static LRESULT CALLBACK EditWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2033 {
2034     MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(GetParent(hwnd), 0);
2035
2036     TRACE("(hwnd=%p, uMsg=%x, wParam=%lx, lParam=%lx)\n",
2037           hwnd, uMsg, wParam, lParam);
2038
2039     switch (uMsg)
2040     {
2041         case WM_GETDLGCODE:
2042           return DLGC_WANTARROWS | DLGC_WANTALLKEYS;
2043
2044         case WM_DESTROY:
2045         {
2046             WNDPROC editProc = infoPtr->EditWndProc;
2047             infoPtr->EditWndProc = NULL;
2048             SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (DWORD_PTR)editProc);
2049             return CallWindowProcW(editProc, hwnd, uMsg, wParam, lParam);
2050         }
2051
2052         case WM_KILLFOCUS:
2053             break;
2054
2055         case WM_KEYDOWN:
2056             if ((VK_ESCAPE == (INT)wParam) || (VK_RETURN == (INT)wParam))
2057                 break;
2058
2059         default:
2060             return CallWindowProcW(infoPtr->EditWndProc, hwnd, uMsg, wParam, lParam);
2061     }
2062
2063     SendMessageW(infoPtr->hWndYearUpDown, WM_CLOSE, 0, 0);
2064     SendMessageW(hwnd, WM_CLOSE, 0, 0);
2065     return 0;
2066 }
2067
2068 /* creates updown control and edit box */
2069 static void MONTHCAL_EditYear(MONTHCAL_INFO *infoPtr, INT calIdx)
2070 {
2071     RECT *rc = &infoPtr->calendars[calIdx].titleyear;
2072     RECT *title = &infoPtr->calendars[calIdx].title;
2073
2074     infoPtr->hWndYearEdit =
2075         CreateWindowExW(0, WC_EDITW, 0, WS_VISIBLE | WS_CHILD | ES_READONLY,
2076                         rc->left + 3, (title->bottom + title->top - infoPtr->textHeight) / 2,
2077                         rc->right - rc->left + 4,
2078                         infoPtr->textHeight, infoPtr->hwndSelf,
2079                         NULL, NULL, NULL);
2080
2081     SendMessageW(infoPtr->hWndYearEdit, WM_SETFONT, (WPARAM)infoPtr->hBoldFont, TRUE);
2082
2083     infoPtr->hWndYearUpDown =
2084         CreateWindowExW(0, UPDOWN_CLASSW, 0,
2085                         WS_VISIBLE | WS_CHILD | UDS_SETBUDDYINT | UDS_NOTHOUSANDS | UDS_ARROWKEYS,
2086                         rc->right + 7, (title->bottom + title->top - infoPtr->textHeight) / 2,
2087                         18, infoPtr->textHeight, infoPtr->hwndSelf,
2088                         NULL, NULL, NULL);
2089
2090     /* attach edit box */
2091     SendMessageW(infoPtr->hWndYearUpDown, UDM_SETRANGE, 0,
2092                  MAKELONG(max_allowed_date.wYear, min_allowed_date.wYear));
2093     SendMessageW(infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM)infoPtr->hWndYearEdit, 0);
2094     SendMessageW(infoPtr->hWndYearUpDown, UDM_SETPOS, 0, infoPtr->calendars[calIdx].month.wYear);
2095
2096     /* subclass edit box */
2097     infoPtr->EditWndProc = (WNDPROC)SetWindowLongPtrW(infoPtr->hWndYearEdit,
2098                                   GWLP_WNDPROC, (DWORD_PTR)EditWndProc);
2099
2100     SetFocus(infoPtr->hWndYearEdit);
2101 }
2102
2103 static LRESULT
2104 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2105 {
2106   MCHITTESTINFO ht;
2107   DWORD hit;
2108
2109   /* Actually we don't need input focus for calendar, this is used to kill
2110      year updown and its buddy edit box */
2111   if (IsWindow(infoPtr->hWndYearUpDown))
2112   {
2113       SetFocus(infoPtr->hwndSelf);
2114       return 0;
2115   }
2116
2117   SetCapture(infoPtr->hwndSelf);
2118
2119   ht.cbSize = sizeof(MCHITTESTINFO);
2120   ht.pt.x = (short)LOWORD(lParam);
2121   ht.pt.y = (short)HIWORD(lParam);
2122
2123   hit = MONTHCAL_HitTest(infoPtr, &ht);
2124
2125   TRACE("%x at (%d, %d)\n", hit, ht.pt.x, ht.pt.y);
2126
2127   switch(hit)
2128   {
2129   case MCHT_TITLEBTNNEXT:
2130     MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2131     infoPtr->status = MC_NEXTPRESSED;
2132     SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2133     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2134     return 0;
2135
2136   case MCHT_TITLEBTNPREV:
2137     MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2138     infoPtr->status = MC_PREVPRESSED;
2139     SetTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER, MC_PREVNEXTMONTHDELAY, 0);
2140     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2141     return 0;
2142
2143   case MCHT_TITLEMONTH:
2144   {
2145     HMENU hMenu = CreatePopupMenu();
2146     WCHAR buf[32];
2147     POINT menupoint;
2148     INT i;
2149
2150     for (i = 0; i < 12; i++)
2151     {
2152         GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1+i, buf, countof(buf));
2153         AppendMenuW(hMenu, MF_STRING|MF_ENABLED, i + 1, buf);
2154     }
2155     menupoint.x = ht.pt.x;
2156     menupoint.y = ht.pt.y;
2157     ClientToScreen(infoPtr->hwndSelf, &menupoint);
2158     i = TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
2159                        menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
2160
2161     if ((i > 0) && (i < 13) && infoPtr->calendars[ht.iOffset].month.wMonth != i)
2162     {
2163         INT delta = i - infoPtr->calendars[ht.iOffset].month.wMonth;
2164         SYSTEMTIME st;
2165
2166         /* check if change allowed by range set */
2167         st = delta < 0 ? infoPtr->calendars[0].month :
2168                          infoPtr->calendars[MONTHCAL_GetCalCount(infoPtr)-1].month;
2169         MONTHCAL_GetMonth(&st, delta);
2170
2171         if (MONTHCAL_IsDateInValidRange(infoPtr, &st, FALSE))
2172         {
2173             MONTHCAL_Scroll(infoPtr, delta);
2174             MONTHCAL_NotifyDayState(infoPtr);
2175             MONTHCAL_NotifySelectionChange(infoPtr);
2176             InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2177         }
2178     }
2179     return 0;
2180   }
2181   case MCHT_TITLEYEAR:
2182   {
2183     MONTHCAL_EditYear(infoPtr, ht.iOffset);
2184     return 0;
2185   }
2186   case MCHT_TODAYLINK:
2187   {
2188     if (infoPtr->dwStyle & MCS_MULTISELECT)
2189     {
2190         SYSTEMTIME range[2];
2191
2192         range[0] = range[1] = infoPtr->todaysDate;
2193         MONTHCAL_SetSelRange(infoPtr, range);
2194     }
2195     else
2196         MONTHCAL_SetCurSel(infoPtr, &infoPtr->todaysDate);
2197
2198     MONTHCAL_NotifySelectionChange(infoPtr);
2199     MONTHCAL_NotifySelect(infoPtr);
2200     return 0;
2201   }
2202   case MCHT_CALENDARDATENEXT:
2203   case MCHT_CALENDARDATEPREV:
2204   case MCHT_CALENDARDATE:
2205   {
2206     SYSTEMTIME st[2];
2207
2208     MONTHCAL_CopyDate(&ht.st, &infoPtr->firstSel);
2209
2210     st[0] = st[1] = ht.st;
2211     /* clear selection range */
2212     MONTHCAL_SetSelRange(infoPtr, st);
2213
2214     infoPtr->status = MC_SEL_LBUTDOWN;
2215     MONTHCAL_SetDayFocus(infoPtr, &ht.st);
2216     return 0;
2217   }
2218   }
2219
2220   return 1;
2221 }
2222
2223
2224 static LRESULT
2225 MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2226 {
2227   NMHDR nmhdr;
2228   MCHITTESTINFO ht;
2229   DWORD hit;
2230
2231   TRACE("\n");
2232
2233   if(infoPtr->status & (MC_PREVPRESSED | MC_NEXTPRESSED)) {
2234     RECT *r;
2235
2236     KillTimer(infoPtr->hwndSelf, MC_PREVNEXTMONTHTIMER);
2237     r = infoPtr->status & MC_PREVPRESSED ? &infoPtr->titlebtnprev : &infoPtr->titlebtnnext;
2238     infoPtr->status &= ~(MC_PREVPRESSED | MC_NEXTPRESSED);
2239
2240     InvalidateRect(infoPtr->hwndSelf, r, FALSE);
2241   }
2242
2243   ReleaseCapture();
2244
2245   /* always send NM_RELEASEDCAPTURE notification */
2246   nmhdr.hwndFrom = infoPtr->hwndSelf;
2247   nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
2248   nmhdr.code     = NM_RELEASEDCAPTURE;
2249   TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
2250
2251   SendMessageW(infoPtr->hwndNotify, WM_NOTIFY, nmhdr.idFrom, (LPARAM)&nmhdr);
2252
2253   if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2254
2255   ht.cbSize = sizeof(MCHITTESTINFO);
2256   ht.pt.x = (short)LOWORD(lParam);
2257   ht.pt.y = (short)HIWORD(lParam);
2258   hit = MONTHCAL_HitTest(infoPtr, &ht);
2259
2260   infoPtr->status = MC_SEL_LBUTUP;
2261   MONTHCAL_SetDayFocus(infoPtr, NULL);
2262
2263   if((hit & MCHT_CALENDARDATE) == MCHT_CALENDARDATE)
2264   {
2265     SYSTEMTIME sel = infoPtr->minSel;
2266
2267     /* will be invalidated here */
2268     MONTHCAL_SetCurSel(infoPtr, &ht.st);
2269
2270     /* send MCN_SELCHANGE only if new date selected */
2271     if (!MONTHCAL_IsDateEqual(&sel, &ht.st))
2272         MONTHCAL_NotifySelectionChange(infoPtr);
2273
2274     MONTHCAL_NotifySelect(infoPtr);
2275   }
2276
2277   return 0;
2278 }
2279
2280
2281 static LRESULT
2282 MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM id)
2283 {
2284   TRACE("%ld\n", id);
2285
2286   switch(id) {
2287   case MC_PREVNEXTMONTHTIMER:
2288     if(infoPtr->status & MC_NEXTPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_FORWARD);
2289     if(infoPtr->status & MC_PREVPRESSED) MONTHCAL_GoToMonth(infoPtr, DIRECTION_BACKWARD);
2290     InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2291     break;
2292   case MC_TODAYUPDATETIMER:
2293   {
2294     SYSTEMTIME st;
2295
2296     if(infoPtr->todaySet) return 0;
2297
2298     GetLocalTime(&st);
2299     MONTHCAL_UpdateToday(infoPtr, &st);
2300
2301     /* notification sent anyway */
2302     MONTHCAL_NotifySelectionChange(infoPtr);
2303
2304     return 0;
2305   }
2306   default:
2307     ERR("got unknown timer %ld\n", id);
2308     break;
2309   }
2310
2311   return 0;
2312 }
2313
2314
2315 static LRESULT
2316 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, LPARAM lParam)
2317 {
2318   MCHITTESTINFO ht;
2319   SYSTEMTIME st_ht;
2320   INT hit;
2321   RECT r;
2322
2323   if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
2324
2325   ht.cbSize = sizeof(MCHITTESTINFO);
2326   ht.pt.x = (short)LOWORD(lParam);
2327   ht.pt.y = (short)HIWORD(lParam);
2328   ht.iOffset = -1;
2329
2330   hit = MONTHCAL_HitTest(infoPtr, &ht);
2331
2332   /* not on the calendar date numbers? bail out */
2333   TRACE("hit:%x\n",hit);
2334   if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE)
2335   {
2336     MONTHCAL_SetDayFocus(infoPtr, NULL);
2337     return 0;
2338   }
2339
2340   st_ht = ht.st;
2341
2342   /* if pointer is over focused day still there's nothing to do */
2343   if(!MONTHCAL_SetDayFocus(infoPtr, &ht.st)) return 0;
2344
2345   MONTHCAL_GetDayRect(infoPtr, &ht.st, &r, ht.iOffset);
2346
2347   if(infoPtr->dwStyle & MCS_MULTISELECT) {
2348     SYSTEMTIME st[2];
2349
2350     MONTHCAL_GetSelRange(infoPtr, st);
2351
2352     /* If we're still at the first selected date and range is empty, return.
2353        If range isn't empty we should change range to a single firstSel */
2354     if(MONTHCAL_IsDateEqual(&infoPtr->firstSel, &st_ht) &&
2355        MONTHCAL_IsDateEqual(&st[0], &st[1])) goto done;
2356
2357     MONTHCAL_IsSelRangeValid(infoPtr, &st_ht, &infoPtr->firstSel, &st_ht);
2358
2359     st[0] = infoPtr->firstSel;
2360     /* we should overwrite timestamp here */
2361     MONTHCAL_CopyDate(&st_ht, &st[1]);
2362
2363     /* bounds will be swapped here if needed */
2364     MONTHCAL_SetSelRange(infoPtr, st);
2365
2366     return 0;
2367   }
2368
2369 done:
2370
2371   /* FIXME: this should specify a rectangle containing only the days that changed
2372      using InvalidateRect */
2373   InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2374
2375   return 0;
2376 }
2377
2378
2379 static LRESULT
2380 MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, HDC hdc_paint)
2381 {
2382   HDC hdc;
2383   PAINTSTRUCT ps;
2384
2385   if (hdc_paint)
2386   {
2387     GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
2388     hdc = hdc_paint;
2389   }
2390   else
2391     hdc = BeginPaint(infoPtr->hwndSelf, &ps);
2392
2393   MONTHCAL_Refresh(infoPtr, hdc, &ps);
2394   if (!hdc_paint) EndPaint(infoPtr->hwndSelf, &ps);
2395   return 0;
2396 }
2397
2398 static LRESULT
2399 MONTHCAL_EraseBkgnd(const MONTHCAL_INFO *infoPtr, HDC hdc)
2400 {
2401   RECT rc;
2402
2403   if (!GetClipBox(hdc, &rc)) return FALSE;
2404
2405   FillRect(hdc, &rc, infoPtr->brushes[BrushBackground]);
2406
2407   return TRUE;
2408 }
2409
2410 static LRESULT
2411 MONTHCAL_PrintClient(MONTHCAL_INFO *infoPtr, HDC hdc, DWORD options)
2412 {
2413   FIXME("Partial Stub: (hdc=%p options=0x%08x)\n", hdc, options);
2414
2415   if ((options & PRF_CHECKVISIBLE) && !IsWindowVisible(infoPtr->hwndSelf))
2416       return 0;
2417
2418   if (options & PRF_ERASEBKGND)
2419       MONTHCAL_EraseBkgnd(infoPtr, hdc);
2420
2421   if (options & PRF_CLIENT)
2422       MONTHCAL_Paint(infoPtr, hdc);
2423
2424   return 0;
2425 }
2426
2427 static LRESULT
2428 MONTHCAL_SetFocus(const MONTHCAL_INFO *infoPtr)
2429 {
2430   TRACE("\n");
2431
2432   InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2433
2434   return 0;
2435 }
2436
2437 /* sets the size information */
2438 static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
2439 {
2440   static const WCHAR O0W[] = { '0','0',0 };
2441   RECT *title=&infoPtr->calendars[0].title;
2442   RECT *prev=&infoPtr->titlebtnprev;
2443   RECT *next=&infoPtr->titlebtnnext;
2444   RECT *titlemonth=&infoPtr->calendars[0].titlemonth;
2445   RECT *titleyear=&infoPtr->calendars[0].titleyear;
2446   RECT *wdays=&infoPtr->calendars[0].wdays;
2447   RECT *weeknumrect=&infoPtr->calendars[0].weeknums;
2448   RECT *days=&infoPtr->calendars[0].days;
2449   RECT *todayrect=&infoPtr->todayrect;
2450
2451   INT xdiv, dx, dy, i, j, x, y, c_dx, c_dy;
2452   WCHAR buff[80];
2453   TEXTMETRICW tm;
2454   SIZE size, sz;
2455   RECT client;
2456   HFONT font;
2457   HDC hdc;
2458
2459   GetClientRect(infoPtr->hwndSelf, &client);
2460
2461   hdc = GetDC(infoPtr->hwndSelf);
2462   font = SelectObject(hdc, infoPtr->hFont);
2463
2464   /* get the height and width of each day's text */
2465   GetTextMetricsW(hdc, &tm);
2466   infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
2467
2468   /* find largest abbreviated day name for current locale */
2469   size.cx = sz.cx = 0;
2470   for (i = 0; i < 7; i++)
2471   {
2472       if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + i,
2473                         buff, countof(buff)))
2474       {
2475           GetTextExtentPoint32W(hdc, buff, lstrlenW(buff), &sz);
2476           if (sz.cx > size.cx) size.cx = sz.cx;
2477       }
2478       else /* locale independent fallback on failure */
2479       {
2480           static const WCHAR SunW[] = { 'S','u','n',0 };
2481
2482           GetTextExtentPoint32W(hdc, SunW, lstrlenW(SunW), &size);
2483           break;
2484       }
2485   }
2486
2487   infoPtr->textWidth = size.cx + 2;
2488
2489   /* recalculate the height and width increments and offsets */
2490   GetTextExtentPoint32W(hdc, O0W, 2, &size);
2491
2492   /* restore the originally selected font */
2493   SelectObject(hdc, font);
2494   ReleaseDC(infoPtr->hwndSelf, hdc);
2495
2496   xdiv = (infoPtr->dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
2497
2498   infoPtr->width_increment  = size.cx * 2 + 4;
2499   infoPtr->height_increment = infoPtr->textHeight;
2500
2501   /* calculate title area */
2502   title->top    = 0;
2503   title->bottom = 3 * infoPtr->height_increment / 2;
2504   title->left   = 0;
2505   title->right  = infoPtr->width_increment * xdiv;
2506
2507   /* set the dimensions of the next and previous buttons and center */
2508   /* the month text vertically */
2509   prev->top    = next->top    = title->top + 4;
2510   prev->bottom = next->bottom = title->bottom - 4;
2511   prev->left   = title->left + 4;
2512   prev->right  = prev->left + (title->bottom - title->top);
2513   next->right  = title->right - 4;
2514   next->left   = next->right - (title->bottom - title->top);
2515
2516   /* titlemonth->left and right change based upon the current month
2517      and are recalculated in refresh as the current month may change
2518      without the control being resized */
2519   titlemonth->top    = titleyear->top    = title->top    + (infoPtr->height_increment)/2;
2520   titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
2521
2522   /* week numbers */
2523   weeknumrect->left  = 0;
2524   weeknumrect->right = infoPtr->dwStyle & MCS_WEEKNUMBERS ? prev->right : 0;
2525
2526   /* days abbreviated names */
2527   wdays->left   = days->left   = weeknumrect->right;
2528   wdays->right  = days->right  = wdays->left + 7 * infoPtr->width_increment;
2529   wdays->top    = title->bottom;
2530   wdays->bottom = wdays->top + infoPtr->height_increment;
2531
2532   days->top    = weeknumrect->top = wdays->bottom;
2533   days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
2534
2535   todayrect->left   = 0;
2536   todayrect->right  = title->right;
2537   todayrect->top    = days->bottom;
2538   todayrect->bottom = days->bottom + infoPtr->height_increment;
2539
2540   /* compute calendar count, update all calendars */
2541   x = (client.right  + MC_CALENDAR_PADDING) / (title->right - title->left + MC_CALENDAR_PADDING);
2542   /* today label affects whole height */
2543   if (infoPtr->dwStyle & MCS_NOTODAY)
2544     y = (client.bottom + MC_CALENDAR_PADDING) / (days->bottom - title->top + MC_CALENDAR_PADDING);
2545   else
2546     y = (client.bottom - todayrect->bottom + todayrect->top + MC_CALENDAR_PADDING) /
2547          (days->bottom - title->top + MC_CALENDAR_PADDING);
2548
2549   /* TODO: ensure that count is properly adjusted to fit 12 months constraint */
2550   if (x == 0) x = 1;
2551   if (y == 0) y = 1;
2552
2553   if (x*y != MONTHCAL_GetCalCount(infoPtr))
2554   {
2555       infoPtr->dim.cx = x;
2556       infoPtr->dim.cy = y;
2557       infoPtr->calendars = ReAlloc(infoPtr->calendars, MONTHCAL_GetCalCount(infoPtr)*sizeof(CALENDAR_INFO));
2558
2559       infoPtr->monthdayState = ReAlloc(infoPtr->monthdayState,
2560           MONTHCAL_GetMonthRange(infoPtr, GMR_DAYSTATE, 0)*sizeof(MONTHDAYSTATE));
2561       MONTHCAL_NotifyDayState(infoPtr);
2562
2563       /* update pointers that we'll need */
2564       title = &infoPtr->calendars[0].title;
2565       wdays = &infoPtr->calendars[0].wdays;
2566       days  = &infoPtr->calendars[0].days;
2567   }
2568
2569   for (i = 1; i < MONTHCAL_GetCalCount(infoPtr); i++)
2570   {
2571       /* set months */
2572       infoPtr->calendars[i] = infoPtr->calendars[0];
2573       MONTHCAL_GetMonth(&infoPtr->calendars[i].month, i);
2574   }
2575
2576   /* offset all rectangles to center in client area */
2577   c_dx = (client.right  - x * title->right - MC_CALENDAR_PADDING * (x-1)) / 2;
2578   c_dy = (client.bottom - y * todayrect->bottom - MC_CALENDAR_PADDING * (y-1)) / 2;
2579
2580   /* if calendar doesn't fit client area show it at left/top bounds */
2581   if (title->left + c_dx < 0) c_dx = 0;
2582   if (title->top  + c_dy < 0) c_dy = 0;
2583
2584   for (i = 0; i < y; i++)
2585   {
2586       for (j = 0; j < x; j++)
2587       {
2588           dx = j*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx;
2589           dy = i*(days->bottom - title->top  + MC_CALENDAR_PADDING) + c_dy;
2590
2591           OffsetRect(&infoPtr->calendars[i*x+j].title, dx, dy);
2592           OffsetRect(&infoPtr->calendars[i*x+j].titlemonth, dx, dy);
2593           OffsetRect(&infoPtr->calendars[i*x+j].titleyear, dx, dy);
2594           OffsetRect(&infoPtr->calendars[i*x+j].wdays, dx, dy);
2595           OffsetRect(&infoPtr->calendars[i*x+j].weeknums, dx, dy);
2596           OffsetRect(&infoPtr->calendars[i*x+j].days, dx, dy);
2597       }
2598   }
2599
2600   OffsetRect(prev, c_dx, c_dy);
2601   OffsetRect(next, (x-1)*(title->right - title->left + MC_CALENDAR_PADDING) + c_dx, c_dy);
2602
2603   i = infoPtr->dim.cx * infoPtr->dim.cy - infoPtr->dim.cx;
2604   todayrect->left   = infoPtr->calendars[i].title.left;
2605   todayrect->right  = infoPtr->calendars[i].title.right;
2606   todayrect->top    = infoPtr->calendars[i].days.bottom;
2607   todayrect->bottom = infoPtr->calendars[i].days.bottom + infoPtr->height_increment;
2608
2609   TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
2610         infoPtr->width_increment,infoPtr->height_increment,
2611         wine_dbgstr_rect(&client),
2612         wine_dbgstr_rect(title),
2613         wine_dbgstr_rect(wdays),
2614         wine_dbgstr_rect(days),
2615         wine_dbgstr_rect(todayrect));
2616 }
2617
2618 static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
2619 {
2620   TRACE("(width=%d, height=%d)\n", Width, Height);
2621
2622   MONTHCAL_UpdateSize(infoPtr);
2623   InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
2624
2625   return 0;
2626 }
2627
2628 static LRESULT MONTHCAL_GetFont(const MONTHCAL_INFO *infoPtr)
2629 {
2630     return (LRESULT)infoPtr->hFont;
2631 }
2632
2633 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
2634 {
2635     HFONT hOldFont;
2636     LOGFONTW lf;
2637
2638     if (!hFont) return 0;
2639
2640     hOldFont = infoPtr->hFont;
2641     infoPtr->hFont = hFont;
2642
2643     GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
2644     lf.lfWeight = FW_BOLD;
2645     infoPtr->hBoldFont = CreateFontIndirectW(&lf);
2646
2647     MONTHCAL_UpdateSize(infoPtr);
2648
2649     if (redraw)
2650         InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
2651
2652     return (LRESULT)hOldFont;
2653 }
2654
2655 /* update theme after a WM_THEMECHANGED message */
2656 static LRESULT theme_changed (const MONTHCAL_INFO* infoPtr)
2657 {
2658     HTHEME theme = GetWindowTheme (infoPtr->hwndSelf);
2659     CloseThemeData (theme);
2660     OpenThemeData (infoPtr->hwndSelf, themeClass);
2661     return 0;
2662 }
2663
2664 static INT MONTHCAL_StyleChanged(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2665                                  const STYLESTRUCT *lpss)
2666 {
2667     TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2668           wStyleType, lpss->styleOld, lpss->styleNew);
2669
2670     if (wStyleType != GWL_STYLE) return 0;
2671
2672     infoPtr->dwStyle = lpss->styleNew;
2673
2674     /* make room for week numbers */
2675     if ((lpss->styleNew ^ lpss->styleOld) & MCS_WEEKNUMBERS)
2676         MONTHCAL_UpdateSize(infoPtr);
2677
2678     return 0;
2679 }
2680
2681 static INT MONTHCAL_StyleChanging(MONTHCAL_INFO *infoPtr, WPARAM wStyleType,
2682                                   STYLESTRUCT *lpss)
2683 {
2684     TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
2685           wStyleType, lpss->styleOld, lpss->styleNew);
2686
2687     /* block MCS_MULTISELECT change */
2688     if ((lpss->styleNew ^ lpss->styleOld) & MCS_MULTISELECT)
2689     {
2690         if (lpss->styleOld & MCS_MULTISELECT)
2691             lpss->styleNew |= MCS_MULTISELECT;
2692         else
2693             lpss->styleNew &= ~MCS_MULTISELECT;
2694     }
2695
2696     /* block MCS_DAYSTATE change */
2697     if ((lpss->styleNew ^ lpss->styleOld) & MCS_DAYSTATE)
2698     {
2699         if (lpss->styleOld & MCS_DAYSTATE)
2700             lpss->styleNew |= MCS_DAYSTATE;
2701         else
2702             lpss->styleNew &= ~MCS_DAYSTATE;
2703     }
2704
2705     return 0;
2706 }
2707
2708 /* FIXME: check whether dateMin/dateMax need to be adjusted. */
2709 static LRESULT
2710 MONTHCAL_Create(HWND hwnd, LPCREATESTRUCTW lpcs)
2711 {
2712   MONTHCAL_INFO *infoPtr;
2713
2714   /* allocate memory for info structure */
2715   infoPtr = Alloc(sizeof(MONTHCAL_INFO));
2716   SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
2717
2718   if (infoPtr == NULL) {
2719     ERR("could not allocate info memory!\n");
2720     return 0;
2721   }
2722
2723   infoPtr->hwndSelf = hwnd;
2724   infoPtr->hwndNotify = lpcs->hwndParent;
2725   infoPtr->dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
2726   infoPtr->dim.cx = infoPtr->dim.cy = 1;
2727   infoPtr->calendars = Alloc(sizeof(CALENDAR_INFO));
2728   if (!infoPtr->calendars) goto fail;
2729   infoPtr->monthdayState = Alloc(3*sizeof(MONTHDAYSTATE));
2730   if (!infoPtr->monthdayState) goto fail;
2731
2732   /* initialize info structure */
2733   /* FIXME: calculate systemtime ->> localtime(subtract timezoneinfo) */
2734
2735   GetLocalTime(&infoPtr->todaysDate);
2736   MONTHCAL_SetFirstDayOfWeek(infoPtr, -1);
2737
2738   infoPtr->maxSelCount   = (infoPtr->dwStyle & MCS_MULTISELECT) ? 7 : 1;
2739
2740   infoPtr->colors[MCSC_BACKGROUND]   = comctl32_color.clrWindow;
2741   infoPtr->colors[MCSC_TEXT]         = comctl32_color.clrWindowText;
2742   infoPtr->colors[MCSC_TITLEBK]      = comctl32_color.clrActiveCaption;
2743   infoPtr->colors[MCSC_TITLETEXT]    = comctl32_color.clrWindow;
2744   infoPtr->colors[MCSC_MONTHBK]      = comctl32_color.clrWindow;
2745   infoPtr->colors[MCSC_TRAILINGTEXT] = comctl32_color.clrGrayText;
2746
2747   infoPtr->brushes[BrushBackground]  = CreateSolidBrush(infoPtr->colors[MCSC_BACKGROUND]);
2748   infoPtr->brushes[BrushTitle]       = CreateSolidBrush(infoPtr->colors[MCSC_TITLEBK]);
2749   infoPtr->brushes[BrushMonth]       = CreateSolidBrush(infoPtr->colors[MCSC_MONTHBK]);
2750
2751   infoPtr->pens[PenRed]  = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
2752   infoPtr->pens[PenText] = CreatePen(PS_SOLID, 1, infoPtr->colors[MCSC_TEXT]);
2753
2754   infoPtr->minSel = infoPtr->todaysDate;
2755   infoPtr->maxSel = infoPtr->todaysDate;
2756   infoPtr->calendars[0].month = infoPtr->todaysDate;
2757   infoPtr->isUnicode = TRUE;
2758
2759   /* setup control layout and day state data */
2760   MONTHCAL_UpdateSize(infoPtr);
2761
2762   /* today auto update timer, to be freed only on control destruction */
2763   SetTimer(infoPtr->hwndSelf, MC_TODAYUPDATETIMER, MC_TODAYUPDATEDELAY, 0);
2764
2765   OpenThemeData (infoPtr->hwndSelf, themeClass);
2766
2767   return 0;
2768
2769 fail:
2770   Free(infoPtr->monthdayState);
2771   Free(infoPtr->calendars);
2772   Free(infoPtr);
2773   return 0;
2774 }
2775
2776 static LRESULT
2777 MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
2778 {
2779   INT i;
2780
2781   /* free month calendar info data */
2782   Free(infoPtr->monthdayState);
2783   Free(infoPtr->calendars);
2784   SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
2785
2786   CloseThemeData (GetWindowTheme (infoPtr->hwndSelf));
2787
2788   for (i = 0; i < BrushLast; i++) DeleteObject(infoPtr->brushes[i]);
2789   for (i = 0; i < PenLast; i++) DeleteObject(infoPtr->pens[i]);
2790
2791   Free(infoPtr);
2792   return 0;
2793 }
2794
2795 /*
2796  * Handler for WM_NOTIFY messages
2797  */
2798 static LRESULT
2799 MONTHCAL_Notify(MONTHCAL_INFO *infoPtr, NMHDR *hdr)
2800 {
2801   /* notification from year edit updown */
2802   if (hdr->code == UDN_DELTAPOS)
2803   {
2804     NMUPDOWN *nmud = (NMUPDOWN*)hdr;
2805
2806     if (hdr->hwndFrom == infoPtr->hWndYearUpDown && nmud->iDelta)
2807     {
2808       /* year value limits are set up explicitly after updown creation */
2809       MONTHCAL_Scroll(infoPtr, 12 * nmud->iDelta);
2810       MONTHCAL_NotifyDayState(infoPtr);
2811       MONTHCAL_NotifySelectionChange(infoPtr);
2812     }
2813   }
2814   return 0;
2815 }
2816
2817 static inline BOOL
2818 MONTHCAL_SetUnicodeFormat(MONTHCAL_INFO *infoPtr, BOOL isUnicode)
2819 {
2820   BOOL prev = infoPtr->isUnicode;
2821   infoPtr->isUnicode = isUnicode;
2822   return prev;
2823 }
2824
2825 static inline BOOL
2826 MONTHCAL_GetUnicodeFormat(const MONTHCAL_INFO *infoPtr)
2827 {
2828   return infoPtr->isUnicode;
2829 }
2830
2831 static LRESULT WINAPI
2832 MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2833 {
2834   MONTHCAL_INFO *infoPtr = (MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0);
2835
2836   TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hwnd, uMsg, wParam, lParam);
2837
2838   if (!infoPtr && (uMsg != WM_CREATE))
2839     return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2840   switch(uMsg)
2841   {
2842   case MCM_GETCURSEL:
2843     return MONTHCAL_GetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2844
2845   case MCM_SETCURSEL:
2846     return MONTHCAL_SetCurSel(infoPtr, (LPSYSTEMTIME)lParam);
2847
2848   case MCM_GETMAXSELCOUNT:
2849     return MONTHCAL_GetMaxSelCount(infoPtr);
2850
2851   case MCM_SETMAXSELCOUNT:
2852     return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
2853
2854   case MCM_GETSELRANGE:
2855     return MONTHCAL_GetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2856
2857   case MCM_SETSELRANGE:
2858     return MONTHCAL_SetSelRange(infoPtr, (LPSYSTEMTIME)lParam);
2859
2860   case MCM_GETMONTHRANGE:
2861     return MONTHCAL_GetMonthRange(infoPtr, wParam, (SYSTEMTIME*)lParam);
2862
2863   case MCM_SETDAYSTATE:
2864     return MONTHCAL_SetDayState(infoPtr, (INT)wParam, (LPMONTHDAYSTATE)lParam);
2865
2866   case MCM_GETMINREQRECT:
2867     return MONTHCAL_GetMinReqRect(infoPtr, (LPRECT)lParam);
2868
2869   case MCM_GETCOLOR:
2870     return MONTHCAL_GetColor(infoPtr, wParam);
2871
2872   case MCM_SETCOLOR:
2873     return MONTHCAL_SetColor(infoPtr, wParam, (COLORREF)lParam);
2874
2875   case MCM_GETTODAY:
2876     return MONTHCAL_GetToday(infoPtr, (LPSYSTEMTIME)lParam);
2877
2878   case MCM_SETTODAY:
2879     return MONTHCAL_SetToday(infoPtr, (LPSYSTEMTIME)lParam);
2880
2881   case MCM_HITTEST:
2882     return MONTHCAL_HitTest(infoPtr, (PMCHITTESTINFO)lParam);
2883
2884   case MCM_GETFIRSTDAYOFWEEK:
2885     return MONTHCAL_GetFirstDayOfWeek(infoPtr);
2886
2887   case MCM_SETFIRSTDAYOFWEEK:
2888     return MONTHCAL_SetFirstDayOfWeek(infoPtr, (INT)lParam);
2889
2890   case MCM_GETRANGE:
2891     return MONTHCAL_GetRange(infoPtr, (LPSYSTEMTIME)lParam);
2892
2893   case MCM_SETRANGE:
2894     return MONTHCAL_SetRange(infoPtr, (SHORT)wParam, (LPSYSTEMTIME)lParam);
2895
2896   case MCM_GETMONTHDELTA:
2897     return MONTHCAL_GetMonthDelta(infoPtr);
2898
2899   case MCM_SETMONTHDELTA:
2900     return MONTHCAL_SetMonthDelta(infoPtr, wParam);
2901
2902   case MCM_GETMAXTODAYWIDTH:
2903     return MONTHCAL_GetMaxTodayWidth(infoPtr);
2904
2905   case MCM_SETUNICODEFORMAT:
2906     return MONTHCAL_SetUnicodeFormat(infoPtr, (BOOL)wParam);
2907
2908   case MCM_GETUNICODEFORMAT:
2909     return MONTHCAL_GetUnicodeFormat(infoPtr);
2910
2911   case MCM_GETCALENDARCOUNT:
2912     return MONTHCAL_GetCalCount(infoPtr);
2913
2914   case WM_GETDLGCODE:
2915     return DLGC_WANTARROWS | DLGC_WANTCHARS;
2916
2917   case WM_RBUTTONUP:
2918     return MONTHCAL_RButtonUp(infoPtr, lParam);
2919
2920   case WM_LBUTTONDOWN:
2921     return MONTHCAL_LButtonDown(infoPtr, lParam);
2922
2923   case WM_MOUSEMOVE:
2924     return MONTHCAL_MouseMove(infoPtr, lParam);
2925
2926   case WM_LBUTTONUP:
2927     return MONTHCAL_LButtonUp(infoPtr, lParam);
2928
2929   case WM_PAINT:
2930     return MONTHCAL_Paint(infoPtr, (HDC)wParam);
2931
2932   case WM_PRINTCLIENT:
2933     return MONTHCAL_PrintClient(infoPtr, (HDC)wParam, (DWORD)lParam);
2934
2935   case WM_ERASEBKGND:
2936     return MONTHCAL_EraseBkgnd(infoPtr, (HDC)wParam);
2937
2938   case WM_SETFOCUS:
2939     return MONTHCAL_SetFocus(infoPtr);
2940
2941   case WM_SIZE:
2942     return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
2943
2944   case WM_NOTIFY:
2945     return MONTHCAL_Notify(infoPtr, (NMHDR*)lParam);
2946
2947   case WM_CREATE:
2948     return MONTHCAL_Create(hwnd, (LPCREATESTRUCTW)lParam);
2949
2950   case WM_SETFONT:
2951     return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
2952
2953   case WM_GETFONT:
2954     return MONTHCAL_GetFont(infoPtr);
2955
2956   case WM_TIMER:
2957     return MONTHCAL_Timer(infoPtr, wParam);
2958     
2959   case WM_THEMECHANGED:
2960     return theme_changed (infoPtr);
2961
2962   case WM_DESTROY:
2963     return MONTHCAL_Destroy(infoPtr);
2964
2965   case WM_SYSCOLORCHANGE:
2966     COMCTL32_RefreshSysColors();
2967     return 0;
2968
2969   case WM_STYLECHANGED:
2970     return MONTHCAL_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2971
2972   case WM_STYLECHANGING:
2973     return MONTHCAL_StyleChanging(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
2974
2975   default:
2976     if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
2977       ERR( "unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
2978     return DefWindowProcW(hwnd, uMsg, wParam, lParam);
2979   }
2980 }
2981
2982
2983 void
2984 MONTHCAL_Register(void)
2985 {
2986   WNDCLASSW wndClass;
2987
2988   ZeroMemory(&wndClass, sizeof(WNDCLASSW));
2989   wndClass.style         = CS_GLOBALCLASS;
2990   wndClass.lpfnWndProc   = MONTHCAL_WindowProc;
2991   wndClass.cbClsExtra    = 0;
2992   wndClass.cbWndExtra    = sizeof(MONTHCAL_INFO *);
2993   wndClass.hCursor       = LoadCursorW(0, (LPWSTR)IDC_ARROW);
2994   wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2995   wndClass.lpszClassName = MONTHCAL_CLASSW;
2996
2997   RegisterClassW(&wndClass);
2998 }
2999
3000
3001 void
3002 MONTHCAL_Unregister(void)
3003 {
3004     UnregisterClassW(MONTHCAL_CLASSW, NULL);
3005 }