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