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