monthcal.c 57.1 KB
Newer Older
1 2
/* Month calendar control

3
 *
4
 * Copyright 1998, 1999 Eric Kohl (ekohl@abo.rhein-zeitung.de)
5 6 7
 * Copyright 1999 Alex Priem (alexp@sci.kun.nl)
 * Copyright 1999 Chris Morgan <cmorgan@wpi.edu> and
 *		  James Abbatiello <abbeyj@wpi.edu>
8
 * Copyright 2000 Uwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de>
9
 *
10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
24 25 26 27 28 29 30 31 32
 * NOTE
 * 
 * This code was audited for completeness against the documented features
 * of Comctl32.dll version 6.0 on Oct. 20, 2004, by Dimitrie O. Paun.
 * 
 * Unless otherwise noted, we believe this code to be complete, as per
 * the specification mentioned above.
 * If you discover missing features, or bugs, please note them below.
 * 
33
 * TODO:
34 35 36 37 38 39 40 41 42 43
 *    -- MCM_[GS]ETUNICODEFORMAT
 *    -- MONTHCAL_GetMonthRange
 *    -- Unicodification
 *    -- handle resources better (doesn't work now); 
 *    -- take care of internationalization.
 *    -- keyboard handling.
 *    -- GetRange: At the moment, we copy ranges anyway, regardless of
 *                 infoPtr->rangeValid; a invalid range is simply filled 
 *                 with zeros in SetRange.  Is this the right behavior?
 *    -- search for FIXME
44 45
 */

46
#include <math.h>
47
#include <stdarg.h>
48
#include <stdio.h>
49
#include <stdlib.h>
50
#include <string.h>
51

52
#include "windef.h"
53
#include "winbase.h"
54
#include "wingdi.h"
55 56
#include "winuser.h"
#include "winnls.h"
57
#include "commctrl.h"
58
#include "comctl32.h"
59
#include "wine/debug.h"
60

61
WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
62

63 64 65 66 67 68 69 70
#define MC_SEL_LBUTUP	    1	/* Left button released */
#define MC_SEL_LBUTDOWN	    2	/* Left button pressed in calendar */
#define MC_PREVPRESSED      4   /* Prev month button pressed */
#define MC_NEXTPRESSED      8   /* Next month button pressed */
#define MC_NEXTMONTHDELAY   350	/* when continuously pressing `next */
										/* month', wait 500 ms before going */
										/* to the next month */
#define MC_NEXTMONTHTIMER   1			/* Timer ID's */
71
#define MC_PREVMONTHTIMER   2
72 73 74

typedef struct
{
75
    HWND hwndSelf;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    COLORREF	bk;
    COLORREF	txt;
    COLORREF	titlebk;
    COLORREF	titletxt;
    COLORREF	monthbk;
    COLORREF	trailingtxt;
    HFONT	hFont;
    HFONT	hBoldFont;
    int		textHeight;
    int		textWidth;
    int		height_increment;
    int		width_increment;
    int		firstDayplace; /* place of the first day of the current month */
    int		delta;	/* scroll rate; # of months that the */
                        /* control moves when user clicks a scroll button */
    int		visible;	/* # of months visible */
    int		firstDay;	/* Start month calendar with firstDay's day */
    int		monthRange;
    MONTHDAYSTATE *monthdayState;
    SYSTEMTIME	todaysDate;
    DWORD	currentMonth;
    DWORD	currentYear;
    int		status;		/* See MC_SEL flags */
    int		curSelDay;	/* current selected day */
    int		firstSelDay;	/* first selected day */
    int		maxSelCount;
    SYSTEMTIME	minSel;
    SYSTEMTIME	maxSel;
    DWORD	rangeValid;
    SYSTEMTIME	minDate;
    SYSTEMTIME	maxDate;
107

108 109
    RECT title;		/* rect for the header above the calendar */
    RECT titlebtnnext;	/* the `next month' button in the header */
110
    RECT titlebtnprev;  /* the `prev month' button in the header */
111 112
    RECT titlemonth;	/* the `month name' txt in the header */
    RECT titleyear;	/* the `year number' txt in the header */
113 114
    RECT wdays;		/* week days at top */
    RECT days;		/* calendar area */
115
    RECT weeknums;	/* week numbers at left side */
116
    RECT todayrect;	/* `today: xx/xx/xx' text rect */
117
    HWND hwndNotify;    /* Window to receive the notifications */
118 119
    HWND hWndYearEdit;  /* Window Handle of edit box to handle years */
    HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
120 121 122
} MONTHCAL_INFO, *LPMONTHCAL_INFO;


123
/* Offsets of days in the week to the weekday of  january 1. */
124
static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
125 126


127
#define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongPtrW(hwnd, 0))
128

129 130
/* helper functions  */

131
/* returns the number of days in any given month, checking for leap days */
132
/* january is 1, december is 12 */
133
int MONTHCAL_MonthLength(int month, int year)
134
{
135
const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0};
Francois Gouget's avatar
Francois Gouget committed
136
  /*Wrap around, this eases handling*/
137 138 139 140 141
  if(month == 0)
    month = 12;
  if(month == 13)
    month = 1;

142 143 144 145 146
  /* if we have a leap year add 1 day to February */
  /* a leap year is a year either divisible by 400 */
  /* or divisible by 4 and not by 100 */
  if(month == 2) { /* February */
    return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
147
     (year%4 == 0)) ? 1 : 0);
148 149 150 151 152
  }
  else {
    return mdays[month - 1];
  }
}
153 154


155
/* make sure that time is valid */
156
static int MONTHCAL_ValidateTime(SYSTEMTIME time)
157
{
158 159 160 161 162 163 164 165 166
  if(time.wMonth > 12) return FALSE;
  if(time.wDayOfWeek > 6) return FALSE;
  if(time.wDay > MONTHCAL_MonthLength(time.wMonth, time.wYear))
	  return FALSE;
  if(time.wHour > 23) return FALSE;
  if(time.wMinute > 59) return FALSE;
  if(time.wSecond > 59) return FALSE;
  if(time.wMilliseconds > 999) return FALSE;

167
  return TRUE;
168 169 170
}


171
void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
172
{
173 174 175 176 177 178 179 180
  to->wYear = from->wYear;
  to->wMonth = from->wMonth;
  to->wDayOfWeek = from->wDayOfWeek;
  to->wDay = from->wDay;
  to->wHour = from->wHour;
  to->wMinute = from->wMinute;
  to->wSecond = from->wSecond;
  to->wMilliseconds = from->wMilliseconds;
181 182 183
}


184
/* Note:Depending on DST, this may be offset by a day.
185 186
   Need to find out if we're on a DST place & adjust the clock accordingly.
   Above function assumes we have a valid data.
187 188
   Valid for year>1752;  1 <= d <= 31, 1 <= m <= 12.
   0 = Monday.
189 190
*/

191
/* returns the day in the week(0 == monday, 6 == sunday) */
192
/* day(1 == 1st, 2 == 2nd... etc), year is the  year value */
193
static int MONTHCAL_CalculateDayOfWeek(DWORD day, DWORD month, DWORD year)
194
{
195
  year-=(month < 3);
196

197
  return((year + year/4 - year/100 + year/400 +
198
         DayOfWeekTable[month-1] + day - 1 ) % 7);
199 200
}

201 202 203
/* From a given point, calculate the row (weekpos), column(daypos)
   and day in the calendar. day== 0 mean the last day of tha last month
*/
204 205
static int MONTHCAL_CalcDayFromPos(MONTHCAL_INFO *infoPtr, int x, int y,
				   int *daypos,int *weekpos)
206
{
207
  int retval, firstDay;
208 209 210
  RECT rcClient;

  GetClientRect(infoPtr->hwndSelf, &rcClient);
211

212 213
  /* if the point is outside the x bounds of the window put
  it at the boundry */
214 215 216
  if (x > rcClient.right)
    x = rcClient.right;

217

218 219
  *daypos = (x - infoPtr->days.left ) / infoPtr->width_increment;
  *weekpos = (y - infoPtr->days.top ) / infoPtr->height_increment;
220

221 222
  firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear)+6 - infoPtr->firstDay)%7;
  retval = *daypos + (7 * *weekpos) - firstDay;
223 224 225 226 227
  return retval;
}

/* day is the day of the month, 1 == 1st day of the month */
/* sets x and y to be the position of the day */
228
/* x == day, y == week where(0,0) == firstDay, 1st week */
229
static void MONTHCAL_CalcDayXY(MONTHCAL_INFO *infoPtr, int day, int month,
230 231
                                 int *x, int *y)
{
232
  int firstDay, prevMonth;
233

234
  firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear) +6 - infoPtr->firstDay)%7;
235

236
  if(month==infoPtr->currentMonth) {
237 238 239 240
    *x = (day + firstDay) % 7;
    *y = (day + firstDay - *x) / 7;
    return;
  }
241
  if(month < infoPtr->currentMonth) {
242
    prevMonth = month - 1;
243
    if(prevMonth==0)
244
       prevMonth = 12;
245

246 247 248 249 250 251 252 253
    *x = (MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear) - firstDay) % 7;
    *y = 0;
    return;
  }

  *y = MONTHCAL_MonthLength(month, infoPtr->currentYear - 1) / 7;
  *x = (day + firstDay + MONTHCAL_MonthLength(month,
       infoPtr->currentYear)) % 7;
254 255 256
}


257
/* x: column(day), y: row(week) */
258
static void MONTHCAL_CalcDayRect(MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
259
{
260
  r->left = infoPtr->days.left + x * infoPtr->width_increment;
261
  r->right = r->left + infoPtr->width_increment;
262
  r->top  = infoPtr->days.top  + y * infoPtr->height_increment;
263 264 265
  r->bottom = r->top + infoPtr->textHeight;
}

266 267 268 269

/* sets the RECT struct r to the rectangle around the day and month */
/* day is the day value of the month(1 == 1st), month is the month */
/* value(january == 1, december == 12) */
270
static inline void MONTHCAL_CalcPosFromDay(MONTHCAL_INFO *infoPtr,
271 272
                                            int day, int month, RECT *r)
{
273
  int x, y;
274

275 276
  MONTHCAL_CalcDayXY(infoPtr, day, month, &x, &y);
  MONTHCAL_CalcDayRect(infoPtr, r, x, y);
277 278 279
}


280 281
/* day is the day in the month(1 == 1st of the month) */
/* month is the month value(1 == january, 12 == december) */
282
static void MONTHCAL_CircleDay(MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month)
283
{
284 285
  HPEN hRedPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
  HPEN hOldPen2 = SelectObject(hdc, hRedPen);
286
  POINT points[13];
287
  int x, y;
288
  RECT day_rect;
289 290


291
  MONTHCAL_CalcPosFromDay(infoPtr, day, month, &day_rect);
292

293 294
  x = day_rect.left;
  y = day_rect.top;
295

296 297 298 299 300 301 302
  points[0].x = x;
  points[0].y = y - 1;
  points[1].x = x + 0.8 * infoPtr->width_increment;
  points[1].y = y - 1;
  points[2].x = x + 0.9 * infoPtr->width_increment;
  points[2].y = y;
  points[3].x = x + infoPtr->width_increment;
303
  points[3].y = y + 0.5 * infoPtr->height_increment;
304

305
  points[4].x = x + infoPtr->width_increment;
306
  points[4].y = y + 0.9 * infoPtr->height_increment;
307
  points[5].x = x + 0.6 * infoPtr->width_increment;
308
  points[5].y = y + 0.9 * infoPtr->height_increment;
309
  points[6].x = x + 0.5 * infoPtr->width_increment;
310
  points[6].y = y + 0.9 * infoPtr->height_increment; /* bring the bottom up just
311
				a hair to fit inside the day rectangle */
312

313
  points[7].x = x + 0.2 * infoPtr->width_increment;
314
  points[7].y = y + 0.8 * infoPtr->height_increment;
315
  points[8].x = x + 0.1 * infoPtr->width_increment;
316
  points[8].y = y + 0.8 * infoPtr->height_increment;
317
  points[9].x = x;
318
  points[9].y = y + 0.5 * infoPtr->height_increment;
319 320

  points[10].x = x + 0.1 * infoPtr->width_increment;
321
  points[10].y = y + 0.2 * infoPtr->height_increment;
322
  points[11].x = x + 0.2 * infoPtr->width_increment;
323 324 325
  points[11].y = y + 0.3 * infoPtr->height_increment;
  points[12].x = x + 0.4 * infoPtr->width_increment;
  points[12].y = y + 0.2 * infoPtr->height_increment;
326

327 328 329
  PolyBezier(hdc, points, 13);
  DeleteObject(hRedPen);
  SelectObject(hdc, hOldPen2);
330
}
331 332


333
static void MONTHCAL_DrawDay(MONTHCAL_INFO *infoPtr, HDC hdc, int day, int month,
334
                             int x, int y, int bold)
335 336 337
{
  char buf[10];
  RECT r;
338
  static int haveBoldFont, haveSelectedDay = FALSE;
339 340 341
  HBRUSH hbr;
  COLORREF oldCol = 0;
  COLORREF oldBk = 0;
342

343
  sprintf(buf, "%d", day);
344

345
/* No need to check styles: when selection is not valid, it is set to zero.
346 347 348
 * 1<day<31, so evertyhing's OK.
 */

349
  MONTHCAL_CalcDayRect(infoPtr, &r, x, y);
350

351
  if((day>=infoPtr->minSel.wDay) && (day<=infoPtr->maxSel.wDay)
352 353 354 355
       && (month==infoPtr->currentMonth)) {
    HRGN hrgn;
    RECT r2;

356
    TRACE("%d %d %d\n",day, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
357
    TRACE("%ld %ld %ld %ld\n", r.left, r.top, r.right, r.bottom);
358 359 360 361 362
    oldCol = SetTextColor(hdc, infoPtr->monthbk);
    oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
    hbr = GetSysColorBrush(COLOR_GRAYTEXT);
    hrgn = CreateEllipticRgn(r.left, r.top, r.right, r.bottom);
    FillRgn(hdc, hrgn, hbr);
363 364 365 366 367 368 369

    /* FIXME: this may need to be changed now b/c of the other
	drawing changes 11/3/99 CMM */
    r2.left   = r.left - 0.25 * infoPtr->textWidth;
    r2.top    = r.top;
    r2.right  = r.left + 0.5 * infoPtr->textWidth;
    r2.bottom = r.bottom;
370
    if(haveSelectedDay) FillRect(hdc, &r2, hbr);
371 372 373 374
      haveSelectedDay = TRUE;
  } else {
    haveSelectedDay = FALSE;
  }
375

376
  /* need to add some code for multiple selections */
377

378 379
  if((bold) &&(!haveBoldFont)) {
    SelectObject(hdc, infoPtr->hBoldFont);
380 381
    haveBoldFont = TRUE;
  }
382 383
  if((!bold) &&(haveBoldFont)) {
    SelectObject(hdc, infoPtr->hFont);
384 385 386
    haveBoldFont = FALSE;
  }

387
  if(haveSelectedDay) {
388 389 390
    SetTextColor(hdc, oldCol);
    SetBkColor(hdc, oldBk);
  }
391

392
  SetBkMode(hdc,TRANSPARENT);
393
  DrawTextA(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
394

395
  /* draw a rectangle around the currently selected days text */
396 397
  if((day==infoPtr->curSelDay) && (month==infoPtr->currentMonth))
    DrawFocusRect(hdc, &r);
398 399 400
}


401
static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, PAINTSTRUCT* ps)
402
{
403 404 405 406 407
  RECT *title=&infoPtr->title;
  RECT *prev=&infoPtr->titlebtnprev;
  RECT *next=&infoPtr->titlebtnnext;
  RECT *titlemonth=&infoPtr->titlemonth;
  RECT *titleyear=&infoPtr->titleyear;
408 409
  RECT dayrect;
  RECT *days=&dayrect;
410 411
  RECT rtoday;
  int i, j, m, mask, day, firstDay, weeknum, weeknum1,prevMonth;
412
  int textHeight = infoPtr->textHeight, textWidth = infoPtr->textWidth;
413 414 415
  SIZE size;
  HBRUSH hbr;
  HFONT currentFont;
416
  char buf[20];
417 418
  char buf1[20];
  char buf2[32];
419
  COLORREF oldTextColor, oldBkColor;
420
  DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
421 422
  RECT rcTemp;
  RECT rcDay; /* used in MONTHCAL_CalcDayRect() */
423 424
  SYSTEMTIME localtime;
  int startofprescal;
425

426
  oldTextColor = SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
427

428 429
  /* fill background */
  hbr = CreateSolidBrush (infoPtr->bk);
430
  FillRect(hdc, &ps->rcPaint, hbr);
431
  DeleteObject(hbr);
432

433
  /* draw header */
434 435 436 437
  if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
  {
    hbr =  CreateSolidBrush(infoPtr->titlebk);
    FillRect(hdc, title, hbr);
438
    DeleteObject(hbr);
439
  }
440

441
  /* if the previous button is pressed draw it depressed */
442
  if(IntersectRect(&rcTemp, &(ps->rcPaint), prev))
443
  {
444 445 446 447 448
    if((infoPtr->status & MC_PREVPRESSED))
        DrawFrameControl(hdc, prev, DFC_SCROLL,
  	   DFCS_SCROLLLEFT | DFCS_PUSHED |
          (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
    else /* if the previous button is pressed draw it depressed */
449
      DrawFrameControl(hdc, prev, DFC_SCROLL,
450
	   DFCS_SCROLLLEFT |(dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
451
  }
452

453
  /* if next button is depressed draw it depressed */
454 455 456 457
  if(IntersectRect(&rcTemp, &(ps->rcPaint), next))
  {
    if((infoPtr->status & MC_NEXTPRESSED))
      DrawFrameControl(hdc, next, DFC_SCROLL,
458
    	   DFCS_SCROLLRIGHT | DFCS_PUSHED |
459 460 461 462 463
           (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
    else /* if the next button is pressed draw it depressed */
      DrawFrameControl(hdc, next, DFC_SCROLL,
           DFCS_SCROLLRIGHT |(dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
  }
464

465
  oldBkColor = SetBkColor(hdc, infoPtr->titlebk);
466
  SetTextColor(hdc, infoPtr->titletxt);
467
  currentFont = SelectObject(hdc, infoPtr->hBoldFont);
468

469 470 471
  /* titlemonth->left and right are set in MONTHCAL_UpdateSize */
  titlemonth->left   = title->left;
  titlemonth->right  = title->right;
472

473 474 475
  GetLocaleInfoA( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+infoPtr->currentMonth -1,
		  buf1,sizeof(buf1));
  sprintf(buf, "%s %ld", buf1, infoPtr->currentYear);
476

477 478
  if(IntersectRect(&rcTemp, &(ps->rcPaint), titlemonth))
  {
479
    DrawTextA(hdc, buf, strlen(buf), titlemonth,
480
                        DT_CENTER | DT_VCENTER | DT_SINGLELINE);
481 482
  }

483
  SelectObject(hdc, infoPtr->hFont);
484

485
/* titlemonth left/right contained rect for whole titletxt('June  1999')
486
  * MCM_HitTestInfo wants month & year rects, so prepare these now.
487
  *(no, we can't draw them separately; the whole text is centered)
488
  */
489
  GetTextExtentPoint32A(hdc, buf, strlen(buf), &size);
490 491
  titlemonth->left = title->right / 2 - size.cx / 2;
  titleyear->right = title->right / 2 + size.cx / 2;
492
  GetTextExtentPoint32A(hdc, buf1, strlen(buf1), &size);
493
  titlemonth->right = titlemonth->left + size.cx;
494
  titleyear->left = titlemonth->right;
495

496 497 498 499 500 501 502 503 504 505 506
  /* draw month area */
  rcTemp.top=infoPtr->wdays.top;
  rcTemp.left=infoPtr->wdays.left;
  rcTemp.bottom=infoPtr->todayrect.bottom;
  rcTemp.right =infoPtr->todayrect.right;
  if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcTemp))
  {
    hbr =  CreateSolidBrush(infoPtr->monthbk);
    FillRect(hdc, &rcTemp, hbr);
    DeleteObject(hbr);
  }
507

508 509
/* draw line under day abbreviatons */

510
  MoveToEx(hdc, infoPtr->days.left + 3, title->bottom + textHeight + 1, NULL);
511
  LineTo(hdc, infoPtr->days.right - 3, title->bottom + textHeight + 1);
512

513 514 515 516 517
  prevMonth = infoPtr->currentMonth - 1;
  if(prevMonth == 0) /* if currentMonth is january(1) prevMonth is */
    prevMonth = 12;    /* december(12) of the previous year */

  infoPtr->wdays.left   = infoPtr->days.left   = infoPtr->weeknums.right;
518
/* draw day abbreviations */
519

520
  SetBkColor(hdc, infoPtr->monthbk);
521
  SetTextColor(hdc, infoPtr->trailingtxt);
522

523 524
  /* copy this rect so we can change the values without changing */
  /* the original version */
525 526 527 528
  days->left = infoPtr->wdays.left;
  days->right = days->left + infoPtr->width_increment;
  days->top = infoPtr->wdays.top;
  days->bottom = infoPtr->wdays.bottom;
529

530
  i = infoPtr->firstDay;
531

532
  for(j=0; j<7; j++) {
533 534 535
    GetLocaleInfoA( LOCALE_USER_DEFAULT,LOCALE_SABBREVDAYNAME1 + (i +j)%7,
		    buf,sizeof(buf));
    DrawTextA(hdc, buf, strlen(buf), days,
536
                         DT_CENTER | DT_VCENTER | DT_SINGLELINE );
537 538 539
    days->left+=infoPtr->width_increment;
    days->right+=infoPtr->width_increment;
  }
540 541

/* draw day numbers; first, the previous month */
542

543
  firstDay = MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear);
544 545

  day = MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear)  +
546 547 548 549
    (infoPtr->firstDay + 7  - firstDay)%7 + 1;
  if (day > MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear))
    day -=7;
  startofprescal = day;
550 551 552 553
  mask = 1<<(day-1);

  i = 0;
  m = 0;
554
  while(day <= MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear)) {
555 556 557
    MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
    if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
    {
558
      MONTHCAL_DrawDay(infoPtr, hdc, day, prevMonth, i, 0,
559
          infoPtr->monthdayState[m] & mask);
560 561
    }

562 563 564 565 566
    mask<<=1;
    day++;
    i++;
  }

567 568
/* draw `current' month  */

569 570 571 572 573 574 575 576
  day = 1; /* start at the beginning of the current month */

  infoPtr->firstDayplace = i;
  SetTextColor(hdc, infoPtr->txt);
  m++;
  mask = 1;

  /* draw the first week of the current month */
577
  while(i<7) {
578 579 580 581
    MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
    if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
    {

582
      MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth, i, 0,
583 584
	infoPtr->monthdayState[m] & mask);

585 586 587
      if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
          (day==infoPtr->todaysDate.wDay) &&
	  (infoPtr->currentYear == infoPtr->todaysDate.wYear)) {
588
        if(!(dwStyle & MCS_NOTODAYCIRCLE))
589
	  MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->currentMonth);
590
      }
591 592 593 594 595 596 597 598 599
    }

    mask<<=1;
    day++;
    i++;
  }

  j = 1; /* move to the 2nd week of the current month */
  i = 0; /* move back to sunday */
600
  while(day <= MONTHCAL_MonthLength(infoPtr->currentMonth, infoPtr->currentYear)) {
601 602 603
    MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
    if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
    {
604
      MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth, i, j,
605 606
          infoPtr->monthdayState[m] & mask);

607 608
      if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
          (day==infoPtr->todaysDate.wDay) &&
609 610
          (infoPtr->currentYear == infoPtr->todaysDate.wYear))
        if(!(dwStyle & MCS_NOTODAYCIRCLE))
611
	  MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->currentMonth);
612
    }
613 614 615
    mask<<=1;
    day++;
    i++;
616
    if(i>6) { /* past saturday, goto the next weeks sunday */
617 618 619 620
      i = 0;
      j++;
    }
  }
621 622 623

/*  draw `next' month */

624 625 626 627 628
  day = 1; /* start at the first day of the next month */
  m++;
  mask = 1;

  SetTextColor(hdc, infoPtr->trailingtxt);
629
  while((i<7) &&(j<6)) {
630 631
    MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
    if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
632
    {
633
      MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth + 1, i, j,
634
		infoPtr->monthdayState[m] & mask);
635
    }
636 637 638

    mask<<=1;
    day++;
639
    i++;
640
    if(i==7) { /* past saturday, go to next week's sunday */
641 642 643 644 645
      i = 0;
      j++;
    }
  }
  SetTextColor(hdc, infoPtr->txt);
646 647 648


/* draw `today' date if style allows it, and draw a circle before today's
649
 * date if necessary */
650

651
  if(!(dwStyle & MCS_NOTODAY))  {
652
    int offset = 0;
653
    if(!(dwStyle & MCS_NOTODAYCIRCLE))  {
654
      /*day is the number of days from nextmonth we put on the calendar */
655
      MONTHCAL_CircleDay(infoPtr, hdc,
656
			 day+MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear),
657
			 infoPtr->currentMonth);
658 659
      offset+=textWidth;
    }
660 661 662 663 664 665 666 667 668
    if (!LoadStringA(COMCTL32_hModule,IDM_TODAY,buf1,sizeof(buf1)))
      {
	WARN("Can't load resource\n");
	strcpy(buf1,"Today:");
      }
    MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
    MONTHCAL_CopyTime(&infoPtr->todaysDate,&localtime);
    GetDateFormatA(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&localtime,NULL,buf2,sizeof(buf2));
    sprintf(buf, "%s %s", buf1,buf2);
669
    SelectObject(hdc, infoPtr->hBoldFont);
670

671
    if(IntersectRect(&rcTemp, &(ps->rcPaint), &rtoday))
672
    {
673 674
      DrawTextA(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
      DrawTextA(hdc, buf, -1, &rtoday, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
675
    }
676
    SelectObject(hdc, infoPtr->hFont);
677
  }
678

679
/*eventually draw week numbers*/
680
  if(dwStyle & MCS_WEEKNUMBERS)  {
681
    /* display weeknumbers*/
682 683 684 685 686
    int mindays;

    /* Rules what week to call the first week of a new year:
       LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
       The week containing Jan 1 is the first week of year
687
       LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
       First week of year must contain 4 days of the new year
       LOCALE_IFIRSTWEEKOFYEAR == 1  (what contries?)
       The first week of the year must contain only days of the new year
    */
    GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR,
		     buf, sizeof(buf));
    sscanf(buf, "%d", &weeknum);
    switch (weeknum)
      {
      case 1: mindays = 6;
	break;
      case 2: mindays = 3;
	break;
      case 0:
      default:
	mindays = 0;
      }
    if (infoPtr->currentMonth < 2)
      {
	/* calculate all those exceptions for january */
	weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear);
709
	if ((infoPtr->firstDay +7 - weeknum1)%7 > mindays)
710 711 712 713
	    weeknum =1;
	else
	  {
	    weeknum = 0;
714
	    for(i=0; i<11; i++)
715 716 717 718 719 720 721 722 723 724 725
	      weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->currentYear-1);
	    weeknum +=startofprescal+ 7;
	    weeknum /=7;
	    weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear-1);
	    if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
	      weeknum++;
	  }
      }
    else
      {
	weeknum = 0;
726
	for(i=0; i<prevMonth-1; i++)
727 728 729 730 731 732 733 734 735 736 737
	  weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->currentYear);
	weeknum +=startofprescal+ 7;
	weeknum /=7;
	weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear);
	if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
	  weeknum++;
      }
    days->left = infoPtr->weeknums.left;
    days->right = infoPtr->weeknums.right;
    days->top = infoPtr->weeknums.top;
    days->bottom = days->top +infoPtr->height_increment;
738
    for(i=0; i<6; i++) {
739 740 741 742 743 744 745 746 747 748 749 750 751 752
      if((i==0)&&(weeknum>50))
	{
	  sprintf(buf, "%d", weeknum);
	  weeknum=0;
	}
      else if((i==5)&&(weeknum>47))
	{
	  sprintf(buf, "%d", 1);
	}
      else
	sprintf(buf, "%d", weeknum + i);
      DrawTextA(hdc, buf, -1, days, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
      days->top+=infoPtr->height_increment;
      days->bottom+=infoPtr->height_increment;
753
    }
754

755 756
    MoveToEx(hdc, infoPtr->weeknums.right, infoPtr->weeknums.top + 3 , NULL);
    LineTo(hdc,   infoPtr->weeknums.right, infoPtr->weeknums.bottom );
757

758 759
  }
  /* currentFont was font at entering Refresh */
760

761
  SetBkColor(hdc, oldBkColor);
762
  SelectObject(hdc, currentFont);
763
  SetTextColor(hdc, oldTextColor);
764 765 766
}


767
static LRESULT
768
MONTHCAL_GetMinReqRect(MONTHCAL_INFO *infoPtr, LPARAM lParam)
769
{
770
  LPRECT lpRect = (LPRECT) lParam;
771 772

  TRACE("rect %p\n", lpRect);
773

774 775
  /* validate parameters */

776
  if((infoPtr==NULL) ||(lpRect == NULL) ) return FALSE;
777

778 779 780 781
  lpRect->left = infoPtr->title.left;
  lpRect->top = infoPtr->title.top;
  lpRect->right = infoPtr->title.right;
  lpRect->bottom = infoPtr->todayrect.bottom;
782
  AdjustWindowRect(lpRect, GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE), FALSE);
783 784 785

  TRACE("%s\n", wine_dbgstr_rect(lpRect));

786
  return TRUE;
787 788
}

789

790
static LRESULT
791
MONTHCAL_GetColor(MONTHCAL_INFO *infoPtr, WPARAM wParam)
792
{
793
  TRACE("\n");
794

795
  switch((int)wParam) {
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
    case MCSC_BACKGROUND:
      return infoPtr->bk;
    case MCSC_TEXT:
      return infoPtr->txt;
    case MCSC_TITLEBK:
      return infoPtr->titlebk;
    case MCSC_TITLETEXT:
      return infoPtr->titletxt;
    case MCSC_MONTHBK:
      return infoPtr->monthbk;
    case MCSC_TRAILINGTEXT:
      return infoPtr->trailingtxt;
  }

  return -1;
811 812
}

813

814
static LRESULT
815
MONTHCAL_SetColor(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
816
{
817
  int prev = -1;
818

819
  TRACE("%d: color %08lx\n", wParam, lParam);
820

821
  switch((int)wParam) {
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
    case MCSC_BACKGROUND:
      prev = infoPtr->bk;
      infoPtr->bk = (COLORREF)lParam;
      break;
    case MCSC_TEXT:
      prev = infoPtr->txt;
      infoPtr->txt = (COLORREF)lParam;
      break;
    case MCSC_TITLEBK:
      prev = infoPtr->titlebk;
      infoPtr->titlebk = (COLORREF)lParam;
      break;
    case MCSC_TITLETEXT:
      prev=infoPtr->titletxt;
      infoPtr->titletxt = (COLORREF)lParam;
      break;
    case MCSC_MONTHBK:
      prev = infoPtr->monthbk;
      infoPtr->monthbk = (COLORREF)lParam;
      break;
    case MCSC_TRAILINGTEXT:
      prev = infoPtr->trailingtxt;
      infoPtr->trailingtxt = (COLORREF)lParam;
      break;
  }

848
  InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
849
  return prev;
850 851
}

852

853
static LRESULT
854
MONTHCAL_GetMonthDelta(MONTHCAL_INFO *infoPtr)
855
{
856
  TRACE("\n");
857

858
  if(infoPtr->delta)
859 860 861
    return infoPtr->delta;
  else
    return infoPtr->visible;
862 863
}

864

865
static LRESULT
866
MONTHCAL_SetMonthDelta(MONTHCAL_INFO *infoPtr, WPARAM wParam)
867
{
868
  int prev = infoPtr->delta;
869

870
  TRACE("delta %d\n", wParam);
871

872 873
  infoPtr->delta = (int)wParam;
  return prev;
874 875 876
}


877
static LRESULT
878
MONTHCAL_GetFirstDayOfWeek(MONTHCAL_INFO *infoPtr)
879
{
880
  return infoPtr->firstDay;
881 882 883
}


884 885 886 887
/* sets the first day of the week that will appear in the control */
/* 0 == Monday, 6 == Sunday */
/* FIXME: this needs to be implemented properly in MONTHCAL_Refresh() */
/* FIXME: we need more error checking here */
888
static LRESULT
889
MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, LPARAM lParam)
890
{
891 892 893 894
  int prev = infoPtr->firstDay;
  char buf[40];
  int day;

895
  TRACE("day %ld\n", lParam);
896

897
  if((lParam >= 0) && (lParam < 7)) {
898 899
    infoPtr->firstDay = (int)lParam;
  }
900 901 902 903 904 905 906 907 908 909
  else
    {
      GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK,
		     buf, sizeof(buf));
      TRACE("%s %d\n", buf, strlen(buf));
      if(sscanf(buf, "%d", &day) == 1)
	infoPtr->firstDay = day;
      else
	infoPtr->firstDay = 0;
    }
910
  return prev;
911 912 913 914
}


static LRESULT
915
MONTHCAL_GetMonthRange(MONTHCAL_INFO *infoPtr)
916
{
917
  TRACE("\n");
918

919 920 921
  return infoPtr->monthRange;
}

922

923
static LRESULT
924
MONTHCAL_GetMaxTodayWidth(MONTHCAL_INFO *infoPtr)
925
{
926
  return(infoPtr->todayrect.right - infoPtr->todayrect.left);
927 928
}

929

930
/* FIXME: are validated times taken from current date/time or simply
931
 * copied?
932 933 934 935 936
 * FIXME:    check whether MCM_GETMONTHRANGE shows correct result after
 *            adjusting range with MCM_SETRANGE
 */

static LRESULT
937
MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
938
{
939
  SYSTEMTIME *lprgSysTimeArray=(SYSTEMTIME *)lParam;
940 941
  int prev;

942
  TRACE("%x %lx\n", wParam, lParam);
943

944 945 946
  if(wParam & GDTR_MAX) {
    if(MONTHCAL_ValidateTime(lprgSysTimeArray[1])){
      MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxDate);
947 948
      infoPtr->rangeValid|=GDTR_MAX;
    } else  {
949 950
      GetSystemTime(&infoPtr->todaysDate);
      MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
951 952
    }
  }
953 954
  if(wParam & GDTR_MIN) {
    if(MONTHCAL_ValidateTime(lprgSysTimeArray[0])) {
955
      MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minDate);
956 957
      infoPtr->rangeValid|=GDTR_MIN;
    } else {
958
      GetSystemTime(&infoPtr->todaysDate);
959
      MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->minDate);
960 961 962 963
    }
  }

  prev = infoPtr->monthRange;
964
  infoPtr->monthRange = infoPtr->maxDate.wMonth - infoPtr->minDate.wMonth;
965

966
  if(infoPtr->monthRange!=prev) {
967
	infoPtr->monthdayState = ReAlloc(infoPtr->monthdayState,
968
                                                  infoPtr->monthRange * sizeof(MONTHDAYSTATE));
969
  }
970 971 972 973

  return 1;
}

974

975
static LRESULT
976
MONTHCAL_GetRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
977
{
978 979
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
  SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *)lParam;
980 981 982

  /* validate parameters */

983
  if((infoPtr==NULL) || (lprgSysTimeArray==NULL)) return FALSE;
984

985 986
  MONTHCAL_CopyTime(&infoPtr->maxDate, &lprgSysTimeArray[1]);
  MONTHCAL_CopyTime(&infoPtr->minDate, &lprgSysTimeArray[0]);
987 988 989 990

  return infoPtr->rangeValid;
}

991

992
static LRESULT
993
MONTHCAL_SetDayState(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
994 995

{
996 997
  int i, iMonths = (int)wParam;
  MONTHDAYSTATE *dayStates = (LPMONTHDAYSTATE)lParam;
998

999 1000
  TRACE("%x %lx\n", wParam, lParam);
  if(iMonths!=infoPtr->monthRange) return 0;
1001

1002
  for(i=0; i<iMonths; i++)
1003
    infoPtr->monthdayState[i] = dayStates[i];
1004 1005 1006
  return 1;
}

1007
static LRESULT
1008
MONTHCAL_GetCurSel(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1009
{
1010
  SYSTEMTIME *lpSel = (SYSTEMTIME *) lParam;
1011

1012
  TRACE("%lx\n", lParam);
1013
  if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
1014
  if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
1015

1016
  MONTHCAL_CopyTime(&infoPtr->minSel, lpSel);
1017
  TRACE("%d/%d/%d\n", lpSel->wYear, lpSel->wMonth, lpSel->wDay);
1018 1019 1020 1021 1022
  return TRUE;
}

/* FIXME: if the specified date is not visible, make it visible */
/* FIXME: redraw? */
1023
static LRESULT
1024
MONTHCAL_SetCurSel(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1025
{
1026
  SYSTEMTIME *lpSel = (SYSTEMTIME *)lParam;
1027

1028
  TRACE("%lx\n", lParam);
1029
  if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
1030
  if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
1031

1032 1033
  infoPtr->currentMonth=lpSel->wMonth;
  infoPtr->currentYear=lpSel->wYear;
1034

1035 1036
  MONTHCAL_CopyTime(lpSel, &infoPtr->minSel);
  MONTHCAL_CopyTime(lpSel, &infoPtr->maxSel);
1037

1038
  InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1039

1040 1041 1042
  return TRUE;
}

1043

1044
static LRESULT
1045
MONTHCAL_GetMaxSelCount(MONTHCAL_INFO *infoPtr)
1046 1047 1048 1049
{
  return infoPtr->maxSelCount;
}

1050

1051
static LRESULT
1052
MONTHCAL_SetMaxSelCount(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1053
{
1054
  TRACE("%x\n", wParam);
1055

1056
  if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)  {
1057
    infoPtr->maxSelCount = wParam;
1058 1059 1060 1061 1062 1063
  }

  return TRUE;
}


1064
static LRESULT
1065
MONTHCAL_GetSelRange(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1066
{
1067
  SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1068

1069
  TRACE("%lx\n", lParam);
1070 1071 1072

  /* validate parameters */

1073
  if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1074

1075
  if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)
1076 1077 1078 1079
  {
    MONTHCAL_CopyTime(&infoPtr->maxSel, &lprgSysTimeArray[1]);
    MONTHCAL_CopyTime(&infoPtr->minSel, &lprgSysTimeArray[0]);
    TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1080
    return TRUE;
1081
  }
1082

1083 1084 1085
  return FALSE;
}

1086

1087
static LRESULT
1088
MONTHCAL_SetSelRange(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1089
{
1090
  SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1091

1092
  TRACE("%lx\n", lParam);
1093 1094 1095

  /* validate parameters */

1096
  if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1097

1098
  if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)
1099 1100 1101 1102
  {
    MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxSel);
    MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minSel);
    TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1103
    return TRUE;
1104
  }
1105

1106 1107 1108 1109
  return FALSE;
}


1110
static LRESULT
1111
MONTHCAL_GetToday(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1112
{
1113
  SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1114

1115
  TRACE("%lx\n", lParam);
1116 1117 1118

  /* validate parameters */

1119 1120
  if((infoPtr==NULL) || (lpToday==NULL)) return FALSE;
  MONTHCAL_CopyTime(&infoPtr->todaysDate, lpToday);
1121 1122 1123 1124
  return TRUE;
}


1125
static LRESULT
1126
MONTHCAL_SetToday(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1127
{
1128
  SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1129

1130
  TRACE("%lx\n", lParam);
1131

1132
  /* validate parameters */
1133

1134 1135
  if((infoPtr==NULL) ||(lpToday==NULL)) return FALSE;
  MONTHCAL_CopyTime(lpToday, &infoPtr->todaysDate);
1136
  InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1137
  return TRUE;
1138 1139 1140 1141
}


static LRESULT
1142
MONTHCAL_HitTest(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1143
{
1144 1145 1146 1147
  PMCHITTESTINFO lpht = (PMCHITTESTINFO)lParam;
  UINT x,y;
  DWORD retval;
  int day,wday,wnum;
1148 1149


1150 1151 1152
  x = lpht->pt.x;
  y = lpht->pt.y;
  retval = MCHT_NOWHERE;
1153

1154 1155 1156

  /* Comment in for debugging...
  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,
1157 1158 1159 1160 1161 1162 1163 1164 1165
	infoPtr->wdays.left, infoPtr->wdays.right,
	infoPtr->wdays.top, infoPtr->wdays.bottom,
	infoPtr->days.left, infoPtr->days.right,
	infoPtr->days.top, infoPtr->days.bottom,
	infoPtr->todayrect.left, infoPtr->todayrect.right,
	infoPtr->todayrect.top, infoPtr->todayrect.bottom,
	infoPtr->weeknums.left, infoPtr->weeknums.right,
	infoPtr->weeknums.top, infoPtr->weeknums.bottom);
  */
1166

1167
  /* are we in the header? */
1168

1169 1170
  if(PtInRect(&infoPtr->title, lpht->pt)) {
    if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1171 1172 1173
      retval = MCHT_TITLEBTNPREV;
      goto done;
    }
1174
    if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1175 1176 1177
      retval = MCHT_TITLEBTNNEXT;
      goto done;
    }
1178
    if(PtInRect(&infoPtr->titlemonth, lpht->pt)) {
1179 1180 1181
      retval = MCHT_TITLEMONTH;
      goto done;
    }
1182
    if(PtInRect(&infoPtr->titleyear, lpht->pt)) {
1183 1184 1185
      retval = MCHT_TITLEYEAR;
      goto done;
    }
1186

1187 1188 1189
    retval = MCHT_TITLE;
    goto done;
  }
1190

1191 1192 1193 1194 1195
  day = MONTHCAL_CalcDayFromPos(infoPtr,x,y,&wday,&wnum);
  if(PtInRect(&infoPtr->wdays, lpht->pt)) {
    retval = MCHT_CALENDARDAY;
    lpht->st.wYear  = infoPtr->currentYear;
    lpht->st.wMonth = (day < 1)? infoPtr->currentMonth -1 : infoPtr->currentMonth;
1196
    lpht->st.wDay   = (day < 1)?
1197
      MONTHCAL_MonthLength(infoPtr->currentMonth-1,infoPtr->currentYear) -day : day;
1198 1199
    goto done;
  }
1200 1201
  if(PtInRect(&infoPtr->weeknums, lpht->pt)) {
    retval = MCHT_CALENDARWEEKNUM;
1202
    lpht->st.wYear  = infoPtr->currentYear;
1203 1204
    lpht->st.wMonth = (day < 1) ? infoPtr->currentMonth -1 :
      (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear)) ?
1205
      infoPtr->currentMonth +1 :infoPtr->currentMonth;
1206 1207 1208
    lpht->st.wDay   = (day < 1 ) ?
      MONTHCAL_MonthLength(infoPtr->currentMonth-1,infoPtr->currentYear) -day :
      (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear)) ?
1209
      day - MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear) : day;
1210
    goto done;
1211
  }
1212
  if(PtInRect(&infoPtr->days, lpht->pt))
1213 1214
    {
      lpht->st.wYear  = infoPtr->currentYear;
1215
      if ( day < 1)
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
	{
	  retval = MCHT_CALENDARDATEPREV;
	  lpht->st.wMonth = infoPtr->currentMonth - 1;
	  if (lpht->st.wMonth <1)
	    {
	      lpht->st.wMonth = 12;
	      lpht->st.wYear--;
	    }
	  lpht->st.wDay   = MONTHCAL_MonthLength(lpht->st.wMonth,lpht->st.wYear) -day;
	}
      else if (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear))
	{
	  retval = MCHT_CALENDARDATENEXT;
	  lpht->st.wMonth = infoPtr->currentMonth + 1;
	  if (lpht->st.wMonth <12)
	    {
	      lpht->st.wMonth = 1;
	      lpht->st.wYear++;
	    }
	  lpht->st.wDay   = day - MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear) ;
	}
      else {
	retval = MCHT_CALENDARDATE;
	lpht->st.wMonth = infoPtr->currentMonth;
	lpht->st.wDay   = day;
      }
      goto done;
    }
  if(PtInRect(&infoPtr->todayrect, lpht->pt)) {
1245
    retval = MCHT_TODAYLINK;
1246 1247
    goto done;
  }
1248 1249


1250
  /* Hit nothing special? What's left must be background :-) */
1251 1252 1253

  retval = MCHT_CALENDARBK;
 done:
1254
  lpht->uHit = retval;
1255 1256 1257 1258
  return retval;
}


1259
static void MONTHCAL_GoToNextMonth(MONTHCAL_INFO *infoPtr)
1260
{
1261
  DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1262

1263
  TRACE("MONTHCAL_GoToNextMonth\n");
1264

1265
  infoPtr->currentMonth++;
1266
  if(infoPtr->currentMonth > 12) {
1267 1268 1269
    infoPtr->currentYear++;
    infoPtr->currentMonth = 1;
  }
1270

1271
  if(dwStyle & MCS_DAYSTATE) {
1272 1273
    NMDAYSTATE nmds;
    int i;
1274

1275 1276
    nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
    nmds.nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1277 1278
    nmds.nmhdr.code     = MCN_GETDAYSTATE;
    nmds.cDayState	= infoPtr->monthRange;
1279
    nmds.prgDayState	= Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1280

1281
    SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1282 1283
    (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
    for(i=0; i<infoPtr->monthRange; i++)
1284 1285
      infoPtr->monthdayState[i] = nmds.prgDayState[i];
  }
1286 1287 1288
}


1289
static void MONTHCAL_GoToPrevMonth(MONTHCAL_INFO *infoPtr)
1290
{
1291
  DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1292

1293
  TRACE("\n");
1294 1295

  infoPtr->currentMonth--;
1296
  if(infoPtr->currentMonth < 1) {
1297 1298 1299 1300
    infoPtr->currentYear--;
    infoPtr->currentMonth = 12;
  }

1301
  if(dwStyle & MCS_DAYSTATE) {
1302 1303 1304
    NMDAYSTATE nmds;
    int i;

1305 1306
    nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
    nmds.nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1307 1308
    nmds.nmhdr.code     = MCN_GETDAYSTATE;
    nmds.cDayState	= infoPtr->monthRange;
1309
    nmds.prgDayState	= Alloc
1310
                        (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1311

1312
    SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1313 1314 1315
        (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
    for(i=0; i<infoPtr->monthRange; i++)
       infoPtr->monthdayState[i] = nmds.prgDayState[i];
1316
  }
1317 1318
}

1319
static LRESULT
1320
MONTHCAL_RButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1321 1322 1323 1324
{
  HMENU hMenu;
  POINT menupoint;
  char buf[32];
1325

1326 1327 1328 1329 1330 1331 1332 1333 1334
  hMenu = CreatePopupMenu();
  if (!LoadStringA(COMCTL32_hModule,IDM_GOTODAY,buf,sizeof(buf)))
    {
      WARN("Can't load resource\n");
      strcpy(buf,"Go to Today:");
    }
  AppendMenuA(hMenu, MF_STRING|MF_ENABLED,1, buf);
  menupoint.x=(INT)LOWORD(lParam);
  menupoint.y=(INT)HIWORD(lParam);
1335
  ClientToScreen(infoPtr->hwndSelf, &menupoint);
1336
  if( TrackPopupMenu(hMenu,TPM_RIGHTBUTTON| TPM_NONOTIFY|TPM_RETURNCMD,
1337
		     menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL))
1338 1339 1340
    {
      infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
      infoPtr->currentYear=infoPtr->todaysDate.wYear;
1341
      InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1342
    }
1343 1344
  return 0;
}
1345

1346
static LRESULT
1347
MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1348
{
1349 1350 1351
  MCHITTESTINFO ht;
  DWORD hit;
  HMENU hMenu;
1352
  RECT rcDay; /* used in determining area to invalidate */
1353 1354 1355
  char buf[32];
  int i;
  POINT menupoint;
1356 1357

  TRACE("%lx\n", lParam);
1358

1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
  if (infoPtr->hWndYearUpDown)
    {
      infoPtr->currentYear=SendMessageA( infoPtr->hWndYearUpDown, UDM_SETPOS,   (WPARAM) 0,(LPARAM)0);
      if(!DestroyWindow(infoPtr->hWndYearUpDown))
	{
	  FIXME("Can't destroy Updown Control\n");
	}
      else
	infoPtr->hWndYearUpDown=0;
      if(!DestroyWindow(infoPtr->hWndYearEdit))
	{
	  FIXME("Can't destroy Updown Control\n");
	}
      else
	infoPtr->hWndYearEdit=0;
1374
      InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1375
    }
1376

1377 1378
  ht.pt.x = (INT)LOWORD(lParam);
  ht.pt.y = (INT)HIWORD(lParam);
1379
  hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1380 1381

  /* FIXME: these flags should be checked by */
1382
  /*((hit & MCHT_XXX) == MCHT_XXX) b/c some of the flags are */
1383
  /* multi-bit */
1384
  if(hit ==MCHT_TITLEBTNNEXT) {
1385
    MONTHCAL_GoToNextMonth(infoPtr);
1386
    infoPtr->status = MC_NEXTPRESSED;
1387 1388
    SetTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER, MC_NEXTMONTHDELAY, 0);
    InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1389
    return TRUE;
1390
  }
1391
  if(hit == MCHT_TITLEBTNPREV){
1392
    MONTHCAL_GoToPrevMonth(infoPtr);
1393
    infoPtr->status = MC_PREVPRESSED;
1394 1395
    SetTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER, MC_NEXTMONTHDELAY, 0);
    InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1396
    return TRUE;
1397 1398
  }

1399
  if(hit == MCHT_TITLEMONTH) {
1400
    hMenu = CreatePopupMenu();
1401

1402 1403 1404 1405 1406 1407 1408 1409
    for (i=0; i<12;i++)
      {
	GetLocaleInfoA( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+i,
		  buf,sizeof(buf));
	AppendMenuA(hMenu, MF_STRING|MF_ENABLED,i+1, buf);
      }
    menupoint.x=infoPtr->titlemonth.right;
    menupoint.y=infoPtr->titlemonth.bottom;
1410
    ClientToScreen(infoPtr->hwndSelf, &menupoint);
1411
    i= TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
1412
		      menupoint.x, menupoint.y, 0, infoPtr->hwndSelf, NULL);
1413 1414 1415
    if ((i>0) && (i<13))
      {
	infoPtr->currentMonth=i;
1416
	InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1417
      }
1418
  }
1419
  if(hit == MCHT_TITLEYEAR) {
1420 1421 1422 1423 1424 1425 1426
    infoPtr->hWndYearEdit=CreateWindowExA(0,
			 "EDIT",
			   0,
			 WS_VISIBLE | WS_CHILD |UDS_SETBUDDYINT,
			 infoPtr->titleyear.left+3,infoPtr->titlebtnnext.top,
			 infoPtr->titleyear.right-infoPtr->titleyear.left,
			 infoPtr->textHeight,
1427
			 infoPtr->hwndSelf,
1428 1429
			 NULL,
			 NULL,
1430 1431 1432 1433 1434 1435 1436 1437
			 NULL);
    infoPtr->hWndYearUpDown=CreateWindowExA(0,
			 UPDOWN_CLASSA,
			   0,
			 WS_VISIBLE | WS_CHILD |UDS_SETBUDDYINT|UDS_NOTHOUSANDS|UDS_ARROWKEYS,
			 infoPtr->titleyear.right+6,infoPtr->titlebtnnext.top,
			 20,
			 infoPtr->textHeight,
1438
			 infoPtr->hwndSelf,
1439 1440
			 NULL,
			 NULL,
1441 1442 1443 1444 1445
			 NULL);
    SendMessageA( infoPtr->hWndYearUpDown, UDM_SETRANGE, (WPARAM) 0, MAKELONG (9999, 1753));
    SendMessageA( infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM) infoPtr->hWndYearEdit, (LPARAM)0 );
    SendMessageA( infoPtr->hWndYearUpDown, UDM_SETPOS,   (WPARAM) 0,(LPARAM)infoPtr->currentYear );
    return TRUE;
1446

1447
  }
1448
  if(hit == MCHT_TODAYLINK) {
1449 1450
    infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
    infoPtr->currentYear=infoPtr->todaysDate.wYear;
1451
    InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1452
    return TRUE;
1453
  }
1454
  if(hit == MCHT_CALENDARDATE) {
1455 1456 1457
    SYSTEMTIME selArray[2];
    NMSELCHANGE nmsc;

1458 1459
    MONTHCAL_CopyTime(&ht.st, &selArray[0]);
    MONTHCAL_CopyTime(&ht.st, &selArray[1]);
1460 1461
    MONTHCAL_SetSelRange(infoPtr, (LPARAM)&selArray);
    MONTHCAL_SetCurSel(infoPtr, (LPARAM)&selArray);
1462
    TRACE("MCHT_CALENDARDATE\n");
1463 1464
    nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
    nmsc.nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1465
    nmsc.nmhdr.code     = MCN_SELCHANGE;
1466 1467
    MONTHCAL_CopyTime(&infoPtr->minSel,&nmsc.stSelStart);
    MONTHCAL_CopyTime(&infoPtr->maxSel,&nmsc.stSelEnd);
1468

1469
    SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1470
           (WPARAM)nmsc.nmhdr.idFrom,(LPARAM)&nmsc);
1471

1472

1473
    /* redraw both old and new days if the selected day changed */
1474
    if(infoPtr->curSelDay != ht.st.wDay) {
1475
      MONTHCAL_CalcPosFromDay(infoPtr, ht.st.wDay, ht.st.wMonth, &rcDay);
1476
      InvalidateRect(infoPtr->hwndSelf, &rcDay, TRUE);
1477 1478

      MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->curSelDay, infoPtr->currentMonth, &rcDay);
1479
      InvalidateRect(infoPtr->hwndSelf, &rcDay, TRUE);
1480
    }
1481

1482 1483 1484
    infoPtr->firstSelDay = ht.st.wDay;
    infoPtr->curSelDay = ht.st.wDay;
    infoPtr->status = MC_SEL_LBUTDOWN;
1485
    return TRUE;
1486 1487
  }

1488
  return 0;
1489 1490
}

1491

1492
static LRESULT
1493
MONTHCAL_LButtonUp(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1494
{
1495 1496
  NMSELCHANGE nmsc;
  NMHDR nmhdr;
1497
  BOOL redraw = FALSE;
1498 1499
  MCHITTESTINFO ht;
  DWORD hit;
1500 1501

  TRACE("\n");
1502

1503
  if(infoPtr->status & MC_NEXTPRESSED) {
1504
    KillTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER);
1505
    infoPtr->status &= ~MC_NEXTPRESSED;
1506 1507 1508
    redraw = TRUE;
  }
  if(infoPtr->status & MC_PREVPRESSED) {
1509
    KillTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER);
1510
    infoPtr->status &= ~MC_PREVPRESSED;
1511 1512
    redraw = TRUE;
  }
1513

1514 1515
  ht.pt.x = (INT)LOWORD(lParam);
  ht.pt.y = (INT)HIWORD(lParam);
1516
  hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1517

1518
  infoPtr->status = MC_SEL_LBUTUP;
1519

1520
  if(hit ==MCHT_CALENDARDATENEXT) {
1521 1522
    MONTHCAL_GoToNextMonth(infoPtr);
    InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1523 1524
    return TRUE;
  }
1525
  if(hit == MCHT_CALENDARDATEPREV){
1526 1527
    MONTHCAL_GoToPrevMonth(infoPtr);
    InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1528 1529
    return TRUE;
  }
1530 1531
  nmhdr.hwndFrom = infoPtr->hwndSelf;
  nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1532
  nmhdr.code     = NM_RELEASEDCAPTURE;
1533
  TRACE("Sent notification from %p to %p\n", infoPtr->hwndSelf, infoPtr->hwndNotify);
1534

1535
  SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1536 1537
                                (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
  /* redraw if necessary */
1538
  if(redraw)
1539
    InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1540 1541
  /* only send MCN_SELECT if currently displayed month's day was selected */
  if(hit == MCHT_CALENDARDATE) {
1542 1543
    nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
    nmsc.nmhdr.idFrom   = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1544 1545 1546
    nmsc.nmhdr.code     = MCN_SELECT;
    MONTHCAL_CopyTime(&infoPtr->minSel, &nmsc.stSelStart);
    MONTHCAL_CopyTime(&infoPtr->maxSel, &nmsc.stSelEnd);
1547

1548 1549 1550 1551
    SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
             (WPARAM)nmsc.nmhdr.idFrom, (LPARAM)&nmsc);

  }
1552
  return 0;
1553 1554
}

1555

1556
static LRESULT
1557
MONTHCAL_Timer(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1558
{
1559
  BOOL redraw = FALSE;
1560

1561
  TRACE("%d\n", wParam);
1562

1563
  switch(wParam) {
1564
  case MC_NEXTMONTHTIMER:
1565
    redraw = TRUE;
1566
    MONTHCAL_GoToNextMonth(infoPtr);
1567 1568
    break;
  case MC_PREVMONTHTIMER:
1569
    redraw = TRUE;
1570
    MONTHCAL_GoToPrevMonth(infoPtr);
1571 1572 1573
    break;
  default:
    ERR("got unknown timer\n");
1574
    break;
1575
  }
1576

1577
  /* redraw only if necessary */
1578
  if(redraw)
1579
    InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1580

1581 1582
  return 0;
}
1583

1584 1585

static LRESULT
1586
MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1587
{
1588 1589 1590
  MCHITTESTINFO ht;
  int oldselday, selday, hit;
  RECT r;
1591

1592
  if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
1593

1594 1595
  ht.pt.x = LOWORD(lParam);
  ht.pt.y = HIWORD(lParam);
1596

1597
  hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1598

1599
  /* not on the calendar date numbers? bail out */
1600 1601
  TRACE("hit:%x\n",hit);
  if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE) return 0;
1602 1603 1604 1605

  selday = ht.st.wDay;
  oldselday = infoPtr->curSelDay;
  infoPtr->curSelDay = selday;
1606
  MONTHCAL_CalcPosFromDay(infoPtr, selday, ht.st. wMonth, &r);
1607

1608
  if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)  {
1609 1610 1611
    SYSTEMTIME selArray[2];
    int i;

1612
    MONTHCAL_GetSelRange(infoPtr, (LPARAM)&selArray);
1613
    i = 0;
1614 1615
    if(infoPtr->firstSelDay==selArray[0].wDay) i=1;
    TRACE("oldRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1616
    if(infoPtr->firstSelDay==selArray[1].wDay) {
1617 1618
      /* 1st time we get here: selArray[0]=selArray[1])  */
      /* if we're still at the first selected date, return */
1619 1620
      if(infoPtr->firstSelDay==selday) goto done;
      if(selday<infoPtr->firstSelDay) i = 0;
1621
    }
1622

1623 1624
    if(abs(infoPtr->firstSelDay - selday) >= infoPtr->maxSelCount) {
      if(selday>infoPtr->firstSelDay)
1625 1626 1627 1628
        selday = infoPtr->firstSelDay + infoPtr->maxSelCount;
      else
        selday = infoPtr->firstSelDay - infoPtr->maxSelCount;
    }
1629

1630 1631
    if(selArray[i].wDay!=selday) {
      TRACE("newRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1632

1633
      selArray[i].wDay = selday;
1634

1635
      if(selArray[0].wDay>selArray[1].wDay) {
1636 1637 1638 1639 1640
        DWORD tempday;
        tempday = selArray[1].wDay;
        selArray[1].wDay = selArray[0].wDay;
        selArray[0].wDay = tempday;
      }
1641

1642
      MONTHCAL_SetSelRange(infoPtr, (LPARAM)&selArray);
1643 1644
    }
  }
1645 1646 1647

done:

1648
  /* only redraw if the currently selected day changed */
1649
  /* FIXME: this should specify a rectangle containing only the days that changed */
1650
  /* using InvalidateRect */
1651
  if(oldselday != infoPtr->curSelDay)
1652
    InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1653

1654
  return 0;
1655 1656
}

1657

1658
static LRESULT
1659
MONTHCAL_Paint(MONTHCAL_INFO *infoPtr, WPARAM wParam)
1660
{
1661 1662
  HDC hdc;
  PAINTSTRUCT ps;
1663

1664 1665
  if (wParam)
  {
1666
    GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
1667 1668 1669
    hdc = (HDC)wParam;
  }
  else
1670
    hdc = BeginPaint(infoPtr->hwndSelf, &ps);
1671

1672 1673
  MONTHCAL_Refresh(infoPtr, hdc, &ps);
  if (!wParam) EndPaint(infoPtr->hwndSelf, &ps);
1674
  return 0;
1675 1676
}

1677

1678
static LRESULT
1679
MONTHCAL_KillFocus(MONTHCAL_INFO *infoPtr)
1680
{
1681
  TRACE("\n");
1682

1683
  InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1684

1685
  return 0;
1686 1687 1688 1689
}


static LRESULT
1690
MONTHCAL_SetFocus(MONTHCAL_INFO *infoPtr)
1691
{
1692
  TRACE("\n");
1693

1694
  InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1695

1696
  return 0;
1697 1698
}

1699
/* sets the size information */
1700
static void MONTHCAL_UpdateSize(MONTHCAL_INFO *infoPtr)
1701
{
1702
  HDC hdc = GetDC(infoPtr->hwndSelf);
1703 1704 1705 1706 1707
  RECT *title=&infoPtr->title;
  RECT *prev=&infoPtr->titlebtnprev;
  RECT *next=&infoPtr->titlebtnnext;
  RECT *titlemonth=&infoPtr->titlemonth;
  RECT *titleyear=&infoPtr->titleyear;
1708 1709
  RECT *wdays=&infoPtr->wdays;
  RECT *weeknumrect=&infoPtr->weeknums;
1710
  RECT *days=&infoPtr->days;
1711
  RECT *todayrect=&infoPtr->todayrect;
1712 1713
  SIZE size;
  TEXTMETRICA tm;
1714
  DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1715
  HFONT currentFont;
1716 1717
  int xdiv, left_offset;
  RECT rcClient;
1718

1719
  GetClientRect(infoPtr->hwndSelf, &rcClient);
1720

1721
  currentFont = SelectObject(hdc, infoPtr->hFont);
1722 1723 1724

  /* get the height and width of each day's text */
  GetTextMetricsA(hdc, &tm);
1725
  infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
1726 1727 1728
  GetTextExtentPoint32A(hdc, "Sun", 3, &size);
  infoPtr->textWidth = size.cx + 2;

1729 1730
  /* recalculate the height and width increments and offsets */
  GetTextExtentPoint32A(hdc, "00", 2, &size);
1731

1732
  xdiv = (dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
1733

1734 1735 1736
  infoPtr->width_increment = size.cx * 2;
  infoPtr->height_increment = infoPtr->textHeight;
  left_offset = (rcClient.right - rcClient.left) - (infoPtr->width_increment * xdiv);
1737

1738
  /* calculate title area */
1739 1740 1741 1742
  title->top    = rcClient.top;
  title->bottom = title->top + 3 * infoPtr->height_increment / 2;
  title->left   = left_offset;
  title->right  = rcClient.right;
1743 1744 1745

  /* set the dimensions of the next and previous buttons and center */
  /* the month text vertically */
1746 1747 1748
  prev->top    = next->top    = title->top + 4;
  prev->bottom = next->bottom = title->bottom - 4;
  prev->left   = title->left + 4;
1749
  prev->right  = prev->left + (title->bottom - title->top) ;
1750
  next->right  = title->right - 4;
1751
  next->left   = next->right - (title->bottom - title->top);
1752

1753 1754 1755
  /* titlemonth->left and right change based upon the current month */
  /* and are recalculated in refresh as the current month may change */
  /* without the control being resized */
1756 1757
  titlemonth->top    = titleyear->top    = title->top    + (infoPtr->height_increment)/2;
  titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
1758

1759 1760
  /* setup the dimensions of the rectangle we draw the names of the */
  /* days of the week in */
1761
  weeknumrect->left = left_offset;
1762
  if(dwStyle & MCS_WEEKNUMBERS)
1763 1764 1765 1766 1767 1768 1769
    weeknumrect->right=prev->right;
  else
    weeknumrect->right=weeknumrect->left;
  wdays->left   = days->left   = weeknumrect->right;
  wdays->right  = days->right  = wdays->left + 7 * infoPtr->width_increment;
  wdays->top    = title->bottom ;
  wdays->bottom = wdays->top + infoPtr->height_increment;
1770

1771
  days->top    = weeknumrect->top = wdays->bottom ;
1772
  days->bottom = weeknumrect->bottom = days->top + 6 * infoPtr->height_increment;
1773

1774 1775
  todayrect->left   = rcClient.left;
  todayrect->right  = rcClient.right;
1776 1777 1778
  todayrect->top    = days->bottom;
  todayrect->bottom = days->bottom + infoPtr->height_increment;

1779
  TRACE("dx=%d dy=%d client[%s] title[%s] wdays[%s] days[%s] today[%s]\n",
1780
	infoPtr->width_increment,infoPtr->height_increment,
1781 1782 1783 1784 1785
        wine_dbgstr_rect(&rcClient),
        wine_dbgstr_rect(title),
        wine_dbgstr_rect(wdays),
        wine_dbgstr_rect(days),
        wine_dbgstr_rect(todayrect));
1786

1787
  /* restore the originally selected font */
1788
  SelectObject(hdc, currentFont);
1789

1790
  ReleaseDC(infoPtr->hwndSelf, hdc);
1791 1792
}

1793
static LRESULT MONTHCAL_Size(MONTHCAL_INFO *infoPtr, int Width, int Height)
1794
{
1795
  TRACE("(width=%d, height=%d)\n", Width, Height);
1796

1797
  MONTHCAL_UpdateSize(infoPtr);
1798 1799

  /* invalidate client area and erase background */
1800
  InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
1801 1802 1803

  return 0;
}
1804

1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
static LRESULT MONTHCAL_GetFont(MONTHCAL_INFO *infoPtr)
{
    return (LRESULT)infoPtr->hFont;
}

static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
{
    HFONT hOldFont;
    LOGFONTW lf;

    if (!hFont) return 0;

    hOldFont = infoPtr->hFont;
    infoPtr->hFont = hFont;

    GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
    lf.lfWeight = FW_BOLD;
    infoPtr->hBoldFont = CreateFontIndirectW(&lf);

    if (redraw)
        InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);

    return (LRESULT)hOldFont;
}

1830
/* FIXME: check whether dateMin/dateMax need to be adjusted. */
1831
static LRESULT
1832
MONTHCAL_Create(HWND hwnd, WPARAM wParam, LPARAM lParam)
1833
{
1834
  MONTHCAL_INFO *infoPtr;
1835

1836
  /* allocate memory for info structure */
1837
  infoPtr =(MONTHCAL_INFO*)Alloc(sizeof(MONTHCAL_INFO));
1838
  SetWindowLongPtrW(hwnd, 0, (DWORD_PTR)infoPtr);
1839

1840 1841
  if(infoPtr == NULL) {
    ERR( "could not allocate info memory!\n");
1842 1843
    return 0;
  }
1844

1845
  infoPtr->hwndSelf = hwnd;
1846 1847
  infoPtr->hwndNotify = ((LPCREATESTRUCTW)lParam)->hwndParent;

1848
  MONTHCAL_SetFont(infoPtr, GetStockObject(DEFAULT_GUI_FONT), FALSE);
1849 1850

  /* initialize info structure */
1851
  /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
1852 1853

  GetSystemTime(&infoPtr->todaysDate);
1854
  MONTHCAL_SetFirstDayOfWeek(infoPtr, (LPARAM)-1);
1855 1856
  infoPtr->currentMonth = infoPtr->todaysDate.wMonth;
  infoPtr->currentYear = infoPtr->todaysDate.wYear;
1857 1858
  MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->minDate);
  MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
1859 1860
  infoPtr->maxDate.wYear=2050;
  infoPtr->minDate.wYear=1950;
1861
  infoPtr->maxSelCount  = 7;
1862
  infoPtr->monthRange = 3;
1863
  infoPtr->monthdayState = Alloc
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
                         (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
  infoPtr->titlebk     = GetSysColor(COLOR_ACTIVECAPTION);
  infoPtr->titletxt    = GetSysColor(COLOR_WINDOW);
  infoPtr->monthbk     = GetSysColor(COLOR_WINDOW);
  infoPtr->trailingtxt = GetSysColor(COLOR_GRAYTEXT);
  infoPtr->bk          = GetSysColor(COLOR_WINDOW);
  infoPtr->txt	       = GetSysColor(COLOR_WINDOWTEXT);

  /* call MONTHCAL_UpdateSize to set all of the dimensions */
  /* of the control */
1874
  MONTHCAL_UpdateSize(infoPtr);
1875 1876

  return 0;
1877 1878 1879 1880
}


static LRESULT
1881
MONTHCAL_Destroy(MONTHCAL_INFO *infoPtr)
1882
{
1883
  /* free month calendar info data */
1884
  if(infoPtr->monthdayState)
1885
      Free(infoPtr->monthdayState);
1886
  SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
1887
  Free(infoPtr);
1888
  return 0;
1889 1890 1891
}


1892
static LRESULT WINAPI
1893
MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1894
{
1895 1896
  MONTHCAL_INFO *infoPtr;

1897
  TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hwnd, uMsg, wParam, lParam);
1898 1899 1900

  infoPtr = MONTHCAL_GetInfoPtr(hwnd);
  if (!infoPtr && (uMsg != WM_CREATE))
1901
    return DefWindowProcA(hwnd, uMsg, wParam, lParam);
1902
  switch(uMsg)
1903 1904
  {
  case MCM_GETCURSEL:
1905
    return MONTHCAL_GetCurSel(infoPtr, lParam);
1906

1907
  case MCM_SETCURSEL:
1908
    return MONTHCAL_SetCurSel(infoPtr, lParam);
1909

1910
  case MCM_GETMAXSELCOUNT:
1911
    return MONTHCAL_GetMaxSelCount(infoPtr);
1912

1913
  case MCM_SETMAXSELCOUNT:
1914
    return MONTHCAL_SetMaxSelCount(infoPtr, wParam);
1915

1916
  case MCM_GETSELRANGE:
1917
    return MONTHCAL_GetSelRange(infoPtr, lParam);
1918

1919
  case MCM_SETSELRANGE:
1920
    return MONTHCAL_SetSelRange(infoPtr, lParam);
1921

1922
  case MCM_GETMONTHRANGE:
1923
    return MONTHCAL_GetMonthRange(infoPtr);
1924

1925
  case MCM_SETDAYSTATE:
1926
    return MONTHCAL_SetDayState(infoPtr, wParam, lParam);
1927

1928
  case MCM_GETMINREQRECT:
1929
    return MONTHCAL_GetMinReqRect(infoPtr, lParam);
1930

1931
  case MCM_GETCOLOR:
1932
    return MONTHCAL_GetColor(infoPtr, wParam);
1933

1934
  case MCM_SETCOLOR:
1935
    return MONTHCAL_SetColor(infoPtr, wParam, lParam);
1936

1937
  case MCM_GETTODAY:
1938
    return MONTHCAL_GetToday(infoPtr, lParam);
1939

1940
  case MCM_SETTODAY:
1941
    return MONTHCAL_SetToday(infoPtr, lParam);
1942

1943
  case MCM_HITTEST:
1944
    return MONTHCAL_HitTest(infoPtr, lParam);
1945

1946
  case MCM_GETFIRSTDAYOFWEEK:
1947
    return MONTHCAL_GetFirstDayOfWeek(infoPtr);
1948

1949
  case MCM_SETFIRSTDAYOFWEEK:
1950
    return MONTHCAL_SetFirstDayOfWeek(infoPtr, lParam);
1951

1952
  case MCM_GETRANGE:
1953
    return MONTHCAL_GetRange(hwnd, wParam, lParam);
1954

1955
  case MCM_SETRANGE:
1956
    return MONTHCAL_SetRange(infoPtr, wParam, lParam);
1957

1958
  case MCM_GETMONTHDELTA:
1959
    return MONTHCAL_GetMonthDelta(infoPtr);
1960

1961
  case MCM_SETMONTHDELTA:
1962
    return MONTHCAL_SetMonthDelta(infoPtr, wParam);
1963

1964
  case MCM_GETMAXTODAYWIDTH:
1965
    return MONTHCAL_GetMaxTodayWidth(infoPtr);
1966

1967 1968
  case WM_GETDLGCODE:
    return DLGC_WANTARROWS | DLGC_WANTCHARS;
1969

1970
  case WM_KILLFOCUS:
1971
    return MONTHCAL_KillFocus(infoPtr);
1972

1973
  case WM_RBUTTONDOWN:
1974
    return MONTHCAL_RButtonDown(infoPtr, lParam);
1975

1976
  case WM_LBUTTONDOWN:
1977
    return MONTHCAL_LButtonDown(infoPtr, lParam);
1978

1979
  case WM_MOUSEMOVE:
1980
    return MONTHCAL_MouseMove(infoPtr, wParam, lParam);
1981

1982
  case WM_LBUTTONUP:
1983
    return MONTHCAL_LButtonUp(infoPtr, lParam);
1984

1985
  case WM_PAINT:
1986
    return MONTHCAL_Paint(infoPtr, wParam);
1987

1988
  case WM_SETFOCUS:
1989
    return MONTHCAL_SetFocus(infoPtr);
1990 1991

  case WM_SIZE:
1992
    return MONTHCAL_Size(infoPtr, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1993

1994
  case WM_CREATE:
1995
    return MONTHCAL_Create(hwnd, wParam, lParam);
1996

1997 1998 1999 2000 2001 2002
  case WM_SETFONT:
    return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);

  case WM_GETFONT:
    return MONTHCAL_GetFont(infoPtr);

2003
  case WM_TIMER:
2004
    return MONTHCAL_Timer(infoPtr, wParam);
2005

2006
  case WM_DESTROY:
2007
    return MONTHCAL_Destroy(infoPtr);
2008

2009
  default:
2010
    if ((uMsg >= WM_USER) && (uMsg < WM_APP))
2011 2012
      ERR( "unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
    return DefWindowProcA(hwnd, uMsg, wParam, lParam);
2013
  }
2014 2015 2016
}


2017
void
2018
MONTHCAL_Register(void)
2019
{
2020 2021
  WNDCLASSA wndClass;

2022
  ZeroMemory(&wndClass, sizeof(WNDCLASSA));
2023
  wndClass.style         = CS_GLOBALCLASS;
2024
  wndClass.lpfnWndProc   = MONTHCAL_WindowProc;
2025 2026
  wndClass.cbClsExtra    = 0;
  wndClass.cbWndExtra    = sizeof(MONTHCAL_INFO *);
2027
  wndClass.hCursor       = LoadCursorA(0, (LPSTR)IDC_ARROW);
2028 2029
  wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  wndClass.lpszClassName = MONTHCAL_CLASSA;
2030

2031
  RegisterClassA(&wndClass);
2032 2033 2034
}


2035
void
2036
MONTHCAL_Unregister(void)
2037
{
2038
    UnregisterClassA(MONTHCAL_CLASSA, NULL);
2039
}