monthcal.c 59.4 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
 * TODO:
25 26
 *   - Notifications.
 *
27
 *
28
 *  FIXME: handle resources better (doesn't work now); also take care
29
           of internationalization.
30
 *  FIXME: keyboard handling.
31 32
 */

33
#include <math.h>
34
#include <stdarg.h>
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <string.h>
38

39
#include "windef.h"
40
#include "winbase.h"
41
#include "wingdi.h"
42 43
#include "winuser.h"
#include "winnls.h"
44
#include "commctrl.h"
45
#include "comctl32.h"
46
#include "wine/debug.h"
47

48
WINE_DEFAULT_DEBUG_CHANNEL(monthcal);
49

50 51 52 53 54 55 56 57
#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 */
58
#define MC_PREVMONTHTIMER   2
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

typedef struct
{
    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		left_offset;
    int		top_offset;
    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;
95

96 97 98 99
    RECT rcClient;	/* rect for whole client area */
    RECT rcDraw;	/* rect for drawable portion of client area */
    RECT title;		/* rect for the header above the calendar */
    RECT titlebtnnext;	/* the `next month' button in the header */
100
    RECT titlebtnprev;  /* the `prev month' button in the header */
101 102
    RECT titlemonth;	/* the `month name' txt in the header */
    RECT titleyear;	/* the `year number' txt in the header */
103 104
    RECT wdays;		/* week days at top */
    RECT days;		/* calendar area */
105
    RECT weeknums;	/* week numbers at left side */
106
    RECT todayrect;	/* `today: xx/xx/xx' text rect */
107
    HWND hwndNotify;    /* Window to receive the notifications */
108 109
    HWND hWndYearEdit;  /* Window Handle of edit box to handle years */
    HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
110 111 112
} MONTHCAL_INFO, *LPMONTHCAL_INFO;


113
/* Offsets of days in the week to the weekday of  january 1. */
114
static const int DayOfWeekTable[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
115 116


117
#define MONTHCAL_GetInfoPtr(hwnd) ((MONTHCAL_INFO *)GetWindowLongA(hwnd, 0))
118

119 120
/* helper functions  */

121
/* returns the number of days in any given month, checking for leap days */
122
/* january is 1, december is 12 */
123
int MONTHCAL_MonthLength(int month, int year)
124
{
125
const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0};
Francois Gouget's avatar
Francois Gouget committed
126
  /*Wrap around, this eases handling*/
127 128 129 130 131
  if(month == 0)
    month = 12;
  if(month == 13)
    month = 1;

132 133 134 135 136
  /* 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) &&
137
     (year%4 == 0)) ? 1 : 0);
138 139 140 141 142
  }
  else {
    return mdays[month - 1];
  }
}
143 144


145
/* make sure that time is valid */
146
static int MONTHCAL_ValidateTime(SYSTEMTIME time)
147
{
148 149 150 151 152 153 154 155 156
  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;

157
  return TRUE;
158 159 160
}


161
void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
162
{
163 164 165 166 167 168 169 170
  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;
171 172 173
}


174
/* Note:Depending on DST, this may be offset by a day.
175 176
   Need to find out if we're on a DST place & adjust the clock accordingly.
   Above function assumes we have a valid data.
177 178
   Valid for year>1752;  1 <= d <= 31, 1 <= m <= 12.
   0 = Monday.
179 180
*/

181
/* returns the day in the week(0 == monday, 6 == sunday) */
182
/* day(1 == 1st, 2 == 2nd... etc), year is the  year value */
183
static int MONTHCAL_CalculateDayOfWeek(DWORD day, DWORD month, DWORD year)
184
{
185
  year-=(month < 3);
186

187
  return((year + year/4 - year/100 + year/400 +
188
         DayOfWeekTable[month-1] + day - 1 ) % 7);
189 190
}

191 192 193
/* 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
*/
194 195
static int MONTHCAL_CalcDayFromPos(MONTHCAL_INFO *infoPtr, int x, int y,
				   int *daypos,int *weekpos)
196
{
197
  int retval, firstDay;
198

199 200
  /* if the point is outside the x bounds of the window put
  it at the boundry */
201 202
  if(x > infoPtr->rcClient.right) {
    x = infoPtr->rcClient.right ;
203
  }
204

205 206
  *daypos = (x - infoPtr->days.left ) / infoPtr->width_increment;
  *weekpos = (y - infoPtr->days.top ) / infoPtr->height_increment;
207

208 209
  firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear)+6 - infoPtr->firstDay)%7;
  retval = *daypos + (7 * *weekpos) - firstDay;
210 211 212 213 214
  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 */
215
/* x == day, y == week where(0,0) == firstDay, 1st week */
216
static void MONTHCAL_CalcDayXY(MONTHCAL_INFO *infoPtr, int day, int month,
217 218
                                 int *x, int *y)
{
219
  int firstDay, prevMonth;
220

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

223
  if(month==infoPtr->currentMonth) {
224 225 226 227
    *x = (day + firstDay) % 7;
    *y = (day + firstDay - *x) / 7;
    return;
  }
228
  if(month < infoPtr->currentMonth) {
229
    prevMonth = month - 1;
230
    if(prevMonth==0)
231
       prevMonth = 12;
232

233 234 235 236 237 238 239 240
    *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;
241 242 243
}


244
/* x: column(day), y: row(week) */
245
static void MONTHCAL_CalcDayRect(MONTHCAL_INFO *infoPtr, RECT *r, int x, int y)
246
{
247
  r->left = infoPtr->days.left + x * infoPtr->width_increment;
248
  r->right = r->left + infoPtr->width_increment;
249
  r->top  = infoPtr->days.top  + y * infoPtr->height_increment;
250 251 252
  r->bottom = r->top + infoPtr->textHeight;
}

253 254 255 256

/* 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) */
257
static inline void MONTHCAL_CalcPosFromDay(MONTHCAL_INFO *infoPtr,
258 259
                                            int day, int month, RECT *r)
{
260
  int x, y;
261

262 263
  MONTHCAL_CalcDayXY(infoPtr, day, month, &x, &y);
  MONTHCAL_CalcDayRect(infoPtr, r, x, y);
264 265 266
}


267 268
/* day is the day in the month(1 == 1st of the month) */
/* month is the month value(1 == january, 12 == december) */
269
static void MONTHCAL_CircleDay(HDC hdc, MONTHCAL_INFO *infoPtr, int day,
270
int month)
271
{
272 273
  HPEN hRedPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
  HPEN hOldPen2 = SelectObject(hdc, hRedPen);
274
  POINT points[13];
275
  int x, y;
276
  RECT day_rect;
277 278


279
  MONTHCAL_CalcPosFromDay(infoPtr, day, month, &day_rect);
280

281 282
  x = day_rect.left;
  y = day_rect.top;
283

284 285 286 287 288 289 290
  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;
291
  points[3].y = y + 0.5 * infoPtr->height_increment;
292

293
  points[4].x = x + infoPtr->width_increment;
294
  points[4].y = y + 0.9 * infoPtr->height_increment;
295
  points[5].x = x + 0.6 * infoPtr->width_increment;
296
  points[5].y = y + 0.9 * infoPtr->height_increment;
297
  points[6].x = x + 0.5 * infoPtr->width_increment;
298
  points[6].y = y + 0.9 * infoPtr->height_increment; /* bring the bottom up just
299
				a hair to fit inside the day rectangle */
300

301
  points[7].x = x + 0.2 * infoPtr->width_increment;
302
  points[7].y = y + 0.8 * infoPtr->height_increment;
303
  points[8].x = x + 0.1 * infoPtr->width_increment;
304
  points[8].y = y + 0.8 * infoPtr->height_increment;
305
  points[9].x = x;
306
  points[9].y = y + 0.5 * infoPtr->height_increment;
307 308

  points[10].x = x + 0.1 * infoPtr->width_increment;
309
  points[10].y = y + 0.2 * infoPtr->height_increment;
310
  points[11].x = x + 0.2 * infoPtr->width_increment;
311 312 313
  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;
314

315 316 317
  PolyBezier(hdc, points, 13);
  DeleteObject(hRedPen);
  SelectObject(hdc, hOldPen2);
318
}
319 320


321 322
static void MONTHCAL_DrawDay(HDC hdc, MONTHCAL_INFO *infoPtr, int day, int month,
                             int x, int y, int bold)
323 324 325
{
  char buf[10];
  RECT r;
326
  static int haveBoldFont, haveSelectedDay = FALSE;
327 328 329 330
  HBRUSH hbr;
  HPEN hNewPen, hOldPen = 0;
  COLORREF oldCol = 0;
  COLORREF oldBk = 0;
331

332
  sprintf(buf, "%d", day);
333

334
/* No need to check styles: when selection is not valid, it is set to zero.
335 336 337
 * 1<day<31, so evertyhing's OK.
 */

338
  MONTHCAL_CalcDayRect(infoPtr, &r, x, y);
339

340
  if((day>=infoPtr->minSel.wDay) && (day<=infoPtr->maxSel.wDay)
341 342 343 344
       && (month==infoPtr->currentMonth)) {
    HRGN hrgn;
    RECT r2;

345
    TRACE("%d %d %d\n",day, infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
346
    TRACE("%ld %ld %ld %ld\n", r.left, r.top, r.right, r.bottom);
347 348 349 350 351
    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);
352 353 354 355 356 357 358

    /* 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;
359
    if(haveSelectedDay) FillRect(hdc, &r2, hbr);
360 361 362 363
      haveSelectedDay = TRUE;
  } else {
    haveSelectedDay = FALSE;
  }
364

365
  /* need to add some code for multiple selections */
366

367 368
  if((bold) &&(!haveBoldFont)) {
    SelectObject(hdc, infoPtr->hBoldFont);
369 370
    haveBoldFont = TRUE;
  }
371 372
  if((!bold) &&(haveBoldFont)) {
    SelectObject(hdc, infoPtr->hFont);
373 374 375
    haveBoldFont = FALSE;
  }

376
  if(haveSelectedDay) {
377 378 379
    SetTextColor(hdc, oldCol);
    SetBkColor(hdc, oldBk);
  }
380

381
  SetBkMode(hdc,TRANSPARENT);
382
  DrawTextA(hdc, buf, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
383

384
  /* draw a rectangle around the currently selected days text */
385
  if((day==infoPtr->curSelDay) && (month==infoPtr->currentMonth)) {
386
    hNewPen = CreatePen(PS_ALTERNATE, 0, GetSysColor(COLOR_WINDOWTEXT) );
387 388 389
    hbr = GetSysColorBrush(COLOR_WINDOWTEXT);
    FrameRect(hdc, &r, hbr);
    SelectObject(hdc, hOldPen);
390 391 392 393 394
  }
}


/* CHECKME: For `todays date', do we need to check the locale?*/
395
static void MONTHCAL_Refresh(HWND hwnd, HDC hdc, PAINTSTRUCT* ps)
396
{
397 398
  MONTHCAL_INFO *infoPtr=MONTHCAL_GetInfoPtr(hwnd);
  RECT *rcClient=&infoPtr->rcClient;
399
  RECT *rcDraw=&infoPtr->rcDraw;
400 401 402 403 404
  RECT *title=&infoPtr->title;
  RECT *prev=&infoPtr->titlebtnprev;
  RECT *next=&infoPtr->titlebtnnext;
  RECT *titlemonth=&infoPtr->titlemonth;
  RECT *titleyear=&infoPtr->titleyear;
405 406
  RECT dayrect;
  RECT *days=&dayrect;
407 408
  RECT rtoday;
  int i, j, m, mask, day, firstDay, weeknum, weeknum1,prevMonth;
409
  int textHeight = infoPtr->textHeight, textWidth = infoPtr->textWidth;
410 411 412 413
  SIZE size;
  HBRUSH hbr;
  HFONT currentFont;
  /* LOGFONTA logFont; */
414
  char buf[20];
415 416
  char buf1[20];
  char buf2[32];
417 418
  COLORREF oldTextColor, oldBkColor;
  DWORD dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
419 420
  RECT rcTemp;
  RECT rcDay; /* used in MONTHCAL_CalcDayRect() */
421 422
  SYSTEMTIME localtime;
  int startofprescal;
423

424
  oldTextColor = SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
425

426 427 428 429

  /* fill background */
  hbr = CreateSolidBrush (infoPtr->bk);
  FillRect(hdc, rcClient, hbr);
430
  DeleteObject(hbr);
431

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

440
  /* if the previous button is pressed draw it depressed */
441
  if(IntersectRect(&rcTemp, &(ps->rcPaint), prev))
442
  {
443 444 445 446 447
    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 */
448
      DrawFrameControl(hdc, prev, DFC_SCROLL,
449
	   DFCS_SCROLLLEFT |(dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
450
  }
451

452
  /* if next button is depressed draw it depressed */
453 454 455 456
  if(IntersectRect(&rcTemp, &(ps->rcPaint), next))
  {
    if((infoPtr->status & MC_NEXTPRESSED))
      DrawFrameControl(hdc, next, DFC_SCROLL,
457
    	   DFCS_SCROLLRIGHT | DFCS_PUSHED |
458 459 460 461 462
           (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));
  }
463

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

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

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

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

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

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

495 496 497 498 499 500 501 502 503 504 505
  /* 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);
  }
506

507 508
/* draw line under day abbreviatons */

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

511
  LineTo(hdc, rcDraw->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(hdc, infoPtr, 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(hdc, infoPtr, 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 589
        if(!(dwStyle & MCS_NOTODAYCIRCLE))
	  MONTHCAL_CircleDay(hdc, infoPtr, 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 604
    MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
    if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
    {
      MONTHCAL_DrawDay(hdc, infoPtr, 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(hdc, infoPtr, 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(hdc, infoPtr, 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 656
      MONTHCAL_CircleDay(hdc, infoPtr,
			 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(HWND hwnd, WPARAM wParam, LPARAM lParam)
769
{
770 771 772
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
  LPRECT lpRect = (LPRECT) lParam;
  TRACE("%x %lx\n", wParam, lParam);
773

774 775
  /* validate parameters */

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

778 779 780 781 782
  lpRect->left = infoPtr->rcClient.left;
  lpRect->right = infoPtr->rcClient.right;
  lpRect->top = infoPtr->rcClient.top;
  lpRect->bottom = infoPtr->rcClient.bottom;
  return TRUE;
783 784
}

785

786
static LRESULT
787
MONTHCAL_GetColor(HWND hwnd, WPARAM wParam, LPARAM lParam)
788
{
789 790
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);

791
  TRACE("%x %lx\n", wParam, lParam);
792

793
  switch((int)wParam) {
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
    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;
809 810
}

811

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

818
  TRACE("%x %lx\n", wParam, lParam);
819

820
  switch((int)wParam) {
821 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
    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;
  }

847
  InvalidateRect(hwnd, NULL, FALSE);
848
  return prev;
849 850
}

851

852
static LRESULT
853
MONTHCAL_GetMonthDelta(HWND hwnd, WPARAM wParam, LPARAM lParam)
854
{
855
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
856

857
  TRACE("%x %lx\n", wParam, lParam);
858

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

865

866
static LRESULT
867
MONTHCAL_SetMonthDelta(HWND hwnd, WPARAM wParam, LPARAM lParam)
868
{
869 870
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
  int prev = infoPtr->delta;
871

872
  TRACE("%x %lx\n", wParam, lParam);
873

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


879
static LRESULT
880
MONTHCAL_GetFirstDayOfWeek(HWND hwnd, WPARAM wParam, LPARAM lParam)
881
{
882
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
883

884
  return infoPtr->firstDay;
885 886 887
}


888 889 890 891
/* 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 */
892
static LRESULT
893
MONTHCAL_SetFirstDayOfWeek(HWND hwnd, WPARAM wParam, LPARAM lParam)
894
{
895 896 897 898 899
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
  int prev = infoPtr->firstDay;
  char buf[40];
  int day;

900
  TRACE("%x %lx\n", wParam, lParam);
901

902
  if((lParam >= 0) && (lParam < 7)) {
903 904
    infoPtr->firstDay = (int)lParam;
  }
905 906 907 908 909 910 911 912 913 914
  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;
    }
915
  return prev;
916 917 918 919 920
}


/* FIXME: fill this in */
static LRESULT
921
MONTHCAL_GetMonthRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
922
{
923
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
924

925
  TRACE("%x %lx\n", wParam, lParam);
926 927
  FIXME("stub\n");

928 929 930
  return infoPtr->monthRange;
}

931

932
static LRESULT
933
MONTHCAL_GetMaxTodayWidth(HWND hwnd)
934
{
935
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
936

937
  return(infoPtr->todayrect.right - infoPtr->todayrect.left);
938 939
}

940

941
/* FIXME: are validated times taken from current date/time or simply
942
 * copied?
943 944 945 946 947
 * FIXME:    check whether MCM_GETMONTHRANGE shows correct result after
 *            adjusting range with MCM_SETRANGE
 */

static LRESULT
948
MONTHCAL_SetRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
949
{
950
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
951
  SYSTEMTIME *lprgSysTimeArray=(SYSTEMTIME *)lParam;
952 953
  int prev;

954
  TRACE("%x %lx\n", wParam, lParam);
955

956 957 958
  if(wParam & GDTR_MAX) {
    if(MONTHCAL_ValidateTime(lprgSysTimeArray[1])){
      MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxDate);
959 960
      infoPtr->rangeValid|=GDTR_MAX;
    } else  {
961 962
      GetSystemTime(&infoPtr->todaysDate);
      MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
963 964
    }
  }
965 966 967
  if(wParam & GDTR_MIN) {
    if(MONTHCAL_ValidateTime(lprgSysTimeArray[0])) {
      MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->maxDate);
968 969
      infoPtr->rangeValid|=GDTR_MIN;
    } else {
970 971
      GetSystemTime(&infoPtr->todaysDate);
      MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
972 973 974 975
    }
  }

  prev = infoPtr->monthRange;
976
  infoPtr->monthRange = infoPtr->maxDate.wMonth - infoPtr->minDate.wMonth;
977

978
  if(infoPtr->monthRange!=prev) {
979
	infoPtr->monthdayState = ReAlloc(infoPtr->monthdayState,
980
                                                  infoPtr->monthRange * sizeof(MONTHDAYSTATE));
981
  }
982 983 984 985

  return 1;
}

986

987
/* CHECKME: At the moment, we copy ranges anyway,regardless of
988
 * infoPtr->rangeValid; a invalid range is simply filled with zeros in
989 990 991 992
 * SetRange.  Is this the right behavior?
*/

static LRESULT
993
MONTHCAL_GetRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
994
{
995 996
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
  SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *)lParam;
997 998 999

  /* validate parameters */

1000
  if((infoPtr==NULL) || (lprgSysTimeArray==NULL)) return FALSE;
1001

1002 1003
  MONTHCAL_CopyTime(&infoPtr->maxDate, &lprgSysTimeArray[1]);
  MONTHCAL_CopyTime(&infoPtr->minDate, &lprgSysTimeArray[0]);
1004 1005 1006 1007

  return infoPtr->rangeValid;
}

1008

1009
static LRESULT
1010
MONTHCAL_SetDayState(HWND hwnd, WPARAM wParam, LPARAM lParam)
1011 1012

{
1013 1014 1015
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
  int i, iMonths = (int)wParam;
  MONTHDAYSTATE *dayStates = (LPMONTHDAYSTATE)lParam;
1016

1017 1018
  TRACE("%x %lx\n", wParam, lParam);
  if(iMonths!=infoPtr->monthRange) return 0;
1019

1020
  for(i=0; i<iMonths; i++)
1021
    infoPtr->monthdayState[i] = dayStates[i];
1022 1023 1024
  return 1;
}

1025
static LRESULT
1026
MONTHCAL_GetCurSel(HWND hwnd, WPARAM wParam, LPARAM lParam)
1027
{
1028 1029
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
  SYSTEMTIME *lpSel = (SYSTEMTIME *) lParam;
1030

1031 1032 1033
  TRACE("%x %lx\n", wParam, lParam);
  if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
  if(GetWindowLongA(hwnd, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
1034

1035
  MONTHCAL_CopyTime(&infoPtr->minSel, lpSel);
1036
  TRACE("%d/%d/%d\n", lpSel->wYear, lpSel->wMonth, lpSel->wDay);
1037 1038 1039 1040 1041
  return TRUE;
}

/* FIXME: if the specified date is not visible, make it visible */
/* FIXME: redraw? */
1042
static LRESULT
1043
MONTHCAL_SetCurSel(HWND hwnd, WPARAM wParam, LPARAM lParam)
1044
{
1045
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1046
  SYSTEMTIME *lpSel = (SYSTEMTIME *)lParam;
1047

1048 1049 1050
  TRACE("%x %lx\n", wParam, lParam);
  if((infoPtr==NULL) ||(lpSel==NULL)) return FALSE;
  if(GetWindowLongA(hwnd, GWL_STYLE) & MCS_MULTISELECT) return FALSE;
1051

1052 1053
  infoPtr->currentMonth=lpSel->wMonth;
  infoPtr->currentYear=lpSel->wYear;
1054

1055 1056
  MONTHCAL_CopyTime(lpSel, &infoPtr->minSel);
  MONTHCAL_CopyTime(lpSel, &infoPtr->maxSel);
1057

1058 1059
  InvalidateRect(hwnd, NULL, FALSE);

1060 1061 1062
  return TRUE;
}

1063

1064
static LRESULT
1065
MONTHCAL_GetMaxSelCount(HWND hwnd, WPARAM wParam, LPARAM lParam)
1066
{
1067
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1068

1069
  TRACE("%x %lx\n", wParam, lParam);
1070 1071 1072
  return infoPtr->maxSelCount;
}

1073

1074
static LRESULT
1075
MONTHCAL_SetMaxSelCount(HWND hwnd, WPARAM wParam, LPARAM lParam)
1076
{
1077
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1078

1079 1080
  TRACE("%x %lx\n", wParam, lParam);
  if(GetWindowLongA(hwnd, GWL_STYLE) & MCS_MULTISELECT)  {
1081
    infoPtr->maxSelCount = wParam;
1082 1083 1084 1085 1086 1087
  }

  return TRUE;
}


1088
static LRESULT
1089
MONTHCAL_GetSelRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
1090
{
1091 1092
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
  SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1093

1094
  TRACE("%x %lx\n", wParam, lParam);
1095 1096 1097

  /* validate parameters */

1098
  if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1099

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

1108 1109 1110
  return FALSE;
}

1111

1112
static LRESULT
1113
MONTHCAL_SetSelRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
1114
{
1115 1116
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
  SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *) lParam;
1117

1118
  TRACE("%x %lx\n", wParam, lParam);
1119 1120 1121

  /* validate parameters */

1122
  if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1123

1124 1125 1126 1127 1128
  if(GetWindowLongA( hwnd, GWL_STYLE) & MCS_MULTISELECT)
  {
    MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxSel);
    MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minSel);
    TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1129
    return TRUE;
1130
  }
1131

1132 1133 1134 1135
  return FALSE;
}


1136
static LRESULT
1137
MONTHCAL_GetToday(HWND hwnd, WPARAM wParam, LPARAM lParam)
1138
{
1139 1140
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
  SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1141

1142
  TRACE("%x %lx\n", wParam, lParam);
1143 1144 1145

  /* validate parameters */

1146 1147
  if((infoPtr==NULL) || (lpToday==NULL)) return FALSE;
  MONTHCAL_CopyTime(&infoPtr->todaysDate, lpToday);
1148 1149 1150 1151
  return TRUE;
}


1152
static LRESULT
1153
MONTHCAL_SetToday(HWND hwnd, WPARAM wParam, LPARAM lParam)
1154
{
1155 1156
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
  SYSTEMTIME *lpToday = (SYSTEMTIME *) lParam;
1157

1158
  TRACE("%x %lx\n", wParam, lParam);
1159

1160
  /* validate parameters */
1161

1162 1163
  if((infoPtr==NULL) ||(lpToday==NULL)) return FALSE;
  MONTHCAL_CopyTime(lpToday, &infoPtr->todaysDate);
1164
  InvalidateRect(hwnd, NULL, FALSE);
1165
  return TRUE;
1166 1167 1168 1169
}


static LRESULT
1170
MONTHCAL_HitTest(HWND hwnd, LPARAM lParam)
1171
{
1172 1173 1174 1175 1176
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
  PMCHITTESTINFO lpht = (PMCHITTESTINFO)lParam;
  UINT x,y;
  DWORD retval;
  int day,wday,wnum;
1177 1178


1179 1180 1181
  x = lpht->pt.x;
  y = lpht->pt.y;
  retval = MCHT_NOWHERE;
1182

1183 1184 1185

  /* 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,
1186 1187 1188 1189 1190 1191 1192 1193 1194
	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);
  */
1195

1196
  /* are we in the header? */
1197

1198 1199
  if(PtInRect(&infoPtr->title, lpht->pt)) {
    if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1200 1201 1202
      retval = MCHT_TITLEBTNPREV;
      goto done;
    }
1203
    if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1204 1205 1206
      retval = MCHT_TITLEBTNNEXT;
      goto done;
    }
1207
    if(PtInRect(&infoPtr->titlemonth, lpht->pt)) {
1208 1209 1210
      retval = MCHT_TITLEMONTH;
      goto done;
    }
1211
    if(PtInRect(&infoPtr->titleyear, lpht->pt)) {
1212 1213 1214
      retval = MCHT_TITLEYEAR;
      goto done;
    }
1215

1216 1217 1218
    retval = MCHT_TITLE;
    goto done;
  }
1219

1220 1221 1222 1223 1224
  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;
1225
    lpht->st.wDay   = (day < 1)?
1226
      MONTHCAL_MonthLength(infoPtr->currentMonth-1,infoPtr->currentYear) -day : day;
1227 1228
    goto done;
  }
1229 1230
  if(PtInRect(&infoPtr->weeknums, lpht->pt)) {
    retval = MCHT_CALENDARWEEKNUM;
1231
    lpht->st.wYear  = infoPtr->currentYear;
1232 1233
    lpht->st.wMonth = (day < 1) ? infoPtr->currentMonth -1 :
      (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear)) ?
1234
      infoPtr->currentMonth +1 :infoPtr->currentMonth;
1235 1236 1237
    lpht->st.wDay   = (day < 1 ) ?
      MONTHCAL_MonthLength(infoPtr->currentMonth-1,infoPtr->currentYear) -day :
      (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear)) ?
1238
      day - MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear) : day;
1239
    goto done;
1240
  }
1241
  if(PtInRect(&infoPtr->days, lpht->pt))
1242 1243
    {
      lpht->st.wYear  = infoPtr->currentYear;
1244
      if ( day < 1)
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
	{
	  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)) {
1274
    retval = MCHT_TODAYLINK;
1275 1276
    goto done;
  }
1277 1278


1279
  /* Hit nothing special? What's left must be background :-) */
1280 1281 1282

  retval = MCHT_CALENDARBK;
 done:
1283
  lpht->uHit = retval;
1284 1285 1286 1287
  return retval;
}


1288
static void MONTHCAL_GoToNextMonth(HWND hwnd, MONTHCAL_INFO *infoPtr)
1289
{
1290
  DWORD dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
1291

1292
  TRACE("MONTHCAL_GoToNextMonth\n");
1293

1294
  infoPtr->currentMonth++;
1295
  if(infoPtr->currentMonth > 12) {
1296 1297 1298
    infoPtr->currentYear++;
    infoPtr->currentMonth = 1;
  }
1299

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

1304
    nmds.nmhdr.hwndFrom = hwnd;
1305
    nmds.nmhdr.idFrom   = GetWindowLongA(hwnd, GWL_ID);
1306 1307
    nmds.nmhdr.code     = MCN_GETDAYSTATE;
    nmds.cDayState	= infoPtr->monthRange;
1308
    nmds.prgDayState	= Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1309

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


1318
static void MONTHCAL_GoToPrevMonth(HWND hwnd,  MONTHCAL_INFO *infoPtr)
1319
{
1320
  DWORD dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
1321

1322
  TRACE("MONTHCAL_GoToPrevMonth\n");
1323 1324

  infoPtr->currentMonth--;
1325
  if(infoPtr->currentMonth < 1) {
1326 1327 1328 1329
    infoPtr->currentYear--;
    infoPtr->currentMonth = 12;
  }

1330
  if(dwStyle & MCS_DAYSTATE) {
1331 1332 1333 1334
    NMDAYSTATE nmds;
    int i;

    nmds.nmhdr.hwndFrom = hwnd;
1335
    nmds.nmhdr.idFrom   = GetWindowLongA(hwnd, GWL_ID);
1336 1337
    nmds.nmhdr.code     = MCN_GETDAYSTATE;
    nmds.cDayState	= infoPtr->monthRange;
1338
    nmds.prgDayState	= Alloc
1339
                        (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1340

1341
    SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1342 1343 1344
        (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
    for(i=0; i<infoPtr->monthRange; i++)
       infoPtr->monthdayState[i] = nmds.prgDayState[i];
1345
  }
1346 1347
}

1348 1349 1350 1351 1352 1353 1354
static LRESULT
MONTHCAL_RButtonDown(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
  HMENU hMenu;
  POINT menupoint;
  char buf[32];
1355

1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
  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);
  ClientToScreen(hwnd, &menupoint);
  if( TrackPopupMenu(hMenu,TPM_RIGHTBUTTON| TPM_NONOTIFY|TPM_RETURNCMD,
		     menupoint.x,menupoint.y,0,hwnd,NULL))
    {
      infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
      infoPtr->currentYear=infoPtr->todaysDate.wYear;
      InvalidateRect(hwnd, NULL, FALSE);
1372
    }
1373 1374
  return 0;
}
1375

1376
static LRESULT
1377
MONTHCAL_LButtonDown(HWND hwnd, WPARAM wParam, LPARAM lParam)
1378
{
1379
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1380 1381 1382
  MCHITTESTINFO ht;
  DWORD hit;
  HMENU hMenu;
1383
  RECT rcDay; /* used in determining area to invalidate */
1384 1385 1386
  char buf[32];
  int i;
  POINT menupoint;
1387
  TRACE("%x %lx\n", wParam, lParam);
1388

1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
  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;
      InvalidateRect(hwnd, NULL, FALSE);
    }
1406

1407 1408
  ht.pt.x = (INT)LOWORD(lParam);
  ht.pt.y = (INT)HIWORD(lParam);
1409
  hit = MONTHCAL_HitTest(hwnd, (LPARAM)&ht);
1410 1411

  /* FIXME: these flags should be checked by */
1412
  /*((hit & MCHT_XXX) == MCHT_XXX) b/c some of the flags are */
1413
  /* multi-bit */
1414
  if(hit ==MCHT_TITLEBTNNEXT) {
1415
    MONTHCAL_GoToNextMonth(hwnd, infoPtr);
1416
    infoPtr->status = MC_NEXTPRESSED;
1417
    SetTimer(hwnd, MC_NEXTMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1418
    InvalidateRect(hwnd, NULL, FALSE);
1419
    return TRUE;
1420
  }
1421
  if(hit == MCHT_TITLEBTNPREV){
1422 1423 1424
    MONTHCAL_GoToPrevMonth(hwnd, infoPtr);
    infoPtr->status = MC_PREVPRESSED;
    SetTimer(hwnd, MC_PREVMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1425
    InvalidateRect(hwnd, NULL, FALSE);
1426
    return TRUE;
1427 1428
  }

1429
  if(hit == MCHT_TITLEMONTH) {
1430
    hMenu = CreatePopupMenu();
1431

1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
    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;
    ClientToScreen(hwnd, &menupoint);
    i= TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RIGHTBUTTON | TPM_RETURNCMD,
		      menupoint.x,menupoint.y,0,hwnd,NULL);
    if ((i>0) && (i<13))
      {
	infoPtr->currentMonth=i;
	InvalidateRect(hwnd, NULL, FALSE);
      }
1448
  }
1449
  if(hit == MCHT_TITLEYEAR) {
1450 1451 1452 1453 1454 1455 1456 1457
    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,
			 hwnd,
1458 1459
			 NULL,
			 NULL,
1460 1461 1462 1463 1464 1465 1466 1467 1468
			 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,
			 hwnd,
1469 1470
			 NULL,
			 NULL,
1471 1472 1473 1474 1475
			 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;
1476

1477
  }
1478
  if(hit == MCHT_TODAYLINK) {
1479 1480 1481 1482
    infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
    infoPtr->currentYear=infoPtr->todaysDate.wYear;
    InvalidateRect(hwnd, NULL, FALSE);
    return TRUE;
1483
  }
1484
  if(hit == MCHT_CALENDARDATE) {
1485 1486 1487
    SYSTEMTIME selArray[2];
    NMSELCHANGE nmsc;

1488 1489 1490 1491
    MONTHCAL_CopyTime(&ht.st, &selArray[0]);
    MONTHCAL_CopyTime(&ht.st, &selArray[1]);
    MONTHCAL_SetSelRange(hwnd,0,(LPARAM) &selArray);
    MONTHCAL_SetCurSel(hwnd,0,(LPARAM) &selArray);
1492
    TRACE("MCHT_CALENDARDATE\n");
1493
    nmsc.nmhdr.hwndFrom = hwnd;
1494
    nmsc.nmhdr.idFrom   = GetWindowLongA(hwnd, GWL_ID);
1495
    nmsc.nmhdr.code     = MCN_SELCHANGE;
1496 1497
    MONTHCAL_CopyTime(&infoPtr->minSel,&nmsc.stSelStart);
    MONTHCAL_CopyTime(&infoPtr->maxSel,&nmsc.stSelEnd);
1498

1499
    SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1500
           (WPARAM)nmsc.nmhdr.idFrom,(LPARAM)&nmsc);
1501

1502

1503
    /* redraw both old and new days if the selected day changed */
1504
    if(infoPtr->curSelDay != ht.st.wDay) {
1505
      MONTHCAL_CalcPosFromDay(infoPtr, ht.st.wDay, ht.st.wMonth, &rcDay);
1506
      InvalidateRect(hwnd, &rcDay, TRUE);
1507 1508

      MONTHCAL_CalcPosFromDay(infoPtr, infoPtr->curSelDay, infoPtr->currentMonth, &rcDay);
1509
      InvalidateRect(hwnd, &rcDay, TRUE);
1510
    }
1511

1512 1513 1514
    infoPtr->firstSelDay = ht.st.wDay;
    infoPtr->curSelDay = ht.st.wDay;
    infoPtr->status = MC_SEL_LBUTDOWN;
1515
    return TRUE;
1516 1517
  }

1518
  return 0;
1519 1520
}

1521

1522
static LRESULT
1523
MONTHCAL_LButtonUp(HWND hwnd, WPARAM wParam, LPARAM lParam)
1524
{
1525
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1526 1527
  NMSELCHANGE nmsc;
  NMHDR nmhdr;
1528
  BOOL redraw = FALSE;
1529 1530
  MCHITTESTINFO ht;
  DWORD hit;
1531 1532

  TRACE("\n");
1533

1534 1535
  if(infoPtr->status & MC_NEXTPRESSED) {
    KillTimer(hwnd, MC_NEXTMONTHTIMER);
1536
    infoPtr->status &= ~MC_NEXTPRESSED;
1537 1538 1539 1540
    redraw = TRUE;
  }
  if(infoPtr->status & MC_PREVPRESSED) {
    KillTimer(hwnd, MC_PREVMONTHTIMER);
1541
    infoPtr->status &= ~MC_PREVPRESSED;
1542 1543
    redraw = TRUE;
  }
1544

1545 1546 1547 1548
  ht.pt.x = (INT)LOWORD(lParam);
  ht.pt.y = (INT)HIWORD(lParam);
  hit = MONTHCAL_HitTest(hwnd, (LPARAM)&ht);

1549
  infoPtr->status = MC_SEL_LBUTUP;
1550

1551 1552 1553 1554 1555
  if(hit ==MCHT_CALENDARDATENEXT) {
    MONTHCAL_GoToNextMonth(hwnd, infoPtr);
    InvalidateRect(hwnd, NULL, FALSE);
    return TRUE;
  }
1556
  if(hit == MCHT_CALENDARDATEPREV){
1557 1558 1559 1560
    MONTHCAL_GoToPrevMonth(hwnd, infoPtr);
    InvalidateRect(hwnd, NULL, FALSE);
    return TRUE;
  }
1561 1562 1563
  nmhdr.hwndFrom = hwnd;
  nmhdr.idFrom   = GetWindowLongA( hwnd, GWL_ID);
  nmhdr.code     = NM_RELEASEDCAPTURE;
1564
  TRACE("Sent notification from %p to %p\n", hwnd, infoPtr->hwndNotify);
1565

1566
  SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1567 1568
                                (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
  /* redraw if necessary */
1569 1570
  if(redraw)
    InvalidateRect(hwnd, NULL, FALSE);
1571 1572 1573 1574 1575 1576 1577
  /* only send MCN_SELECT if currently displayed month's day was selected */
  if(hit == MCHT_CALENDARDATE) {
    nmsc.nmhdr.hwndFrom = hwnd;
    nmsc.nmhdr.idFrom   = GetWindowLongA(hwnd, GWL_ID);
    nmsc.nmhdr.code     = MCN_SELECT;
    MONTHCAL_CopyTime(&infoPtr->minSel, &nmsc.stSelStart);
    MONTHCAL_CopyTime(&infoPtr->maxSel, &nmsc.stSelEnd);
1578

1579 1580 1581 1582
    SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
             (WPARAM)nmsc.nmhdr.idFrom, (LPARAM)&nmsc);

  }
1583
  return 0;
1584 1585
}

1586

1587
static LRESULT
1588
MONTHCAL_Timer(HWND hwnd, WPARAM wParam, LPARAM lParam)
1589
{
1590 1591
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
  BOOL redraw = FALSE;
1592

1593 1594
  TRACE(" %d\n", wParam);
  if(!infoPtr) return 0;
1595

1596
  switch(wParam) {
1597
  case MC_NEXTMONTHTIMER:
1598 1599
    redraw = TRUE;
    MONTHCAL_GoToNextMonth(hwnd, infoPtr);
1600 1601
    break;
  case MC_PREVMONTHTIMER:
1602 1603
    redraw = TRUE;
    MONTHCAL_GoToPrevMonth(hwnd, infoPtr);
1604 1605 1606 1607
    break;
  default:
    ERR("got unknown timer\n");
  }
1608

1609
  /* redraw only if necessary */
1610 1611
  if(redraw)
    InvalidateRect(hwnd, NULL, FALSE);
1612

1613 1614
  return 0;
}
1615

1616 1617

static LRESULT
1618
MONTHCAL_MouseMove(HWND hwnd, WPARAM wParam, LPARAM lParam)
1619
{
1620
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1621 1622 1623
  MCHITTESTINFO ht;
  int oldselday, selday, hit;
  RECT r;
1624

1625
  if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
1626

1627 1628
  ht.pt.x = LOWORD(lParam);
  ht.pt.y = HIWORD(lParam);
1629

1630
  hit = MONTHCAL_HitTest(hwnd, (LPARAM)&ht);
1631

1632
  /* not on the calendar date numbers? bail out */
1633 1634
  TRACE("hit:%x\n",hit);
  if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE) return 0;
1635 1636 1637 1638

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

1641
  if(GetWindowLongA(hwnd, GWL_STYLE) & MCS_MULTISELECT)  {
1642 1643 1644
    SYSTEMTIME selArray[2];
    int i;

1645
    MONTHCAL_GetSelRange(hwnd, 0, (LPARAM)&selArray);
1646
    i = 0;
1647 1648
    if(infoPtr->firstSelDay==selArray[0].wDay) i=1;
    TRACE("oldRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1649
    if(infoPtr->firstSelDay==selArray[1].wDay) {
1650 1651
      /* 1st time we get here: selArray[0]=selArray[1])  */
      /* if we're still at the first selected date, return */
1652 1653
      if(infoPtr->firstSelDay==selday) goto done;
      if(selday<infoPtr->firstSelDay) i = 0;
1654
    }
1655

1656 1657
    if(abs(infoPtr->firstSelDay - selday) >= infoPtr->maxSelCount) {
      if(selday>infoPtr->firstSelDay)
1658 1659 1660 1661
        selday = infoPtr->firstSelDay + infoPtr->maxSelCount;
      else
        selday = infoPtr->firstSelDay - infoPtr->maxSelCount;
    }
1662

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

1666
      selArray[i].wDay = selday;
1667

1668
      if(selArray[0].wDay>selArray[1].wDay) {
1669 1670 1671 1672 1673
        DWORD tempday;
        tempday = selArray[1].wDay;
        selArray[1].wDay = selArray[0].wDay;
        selArray[0].wDay = tempday;
      }
1674

1675
      MONTHCAL_SetSelRange(hwnd, 0, (LPARAM)&selArray);
1676 1677
    }
  }
1678 1679 1680

done:

1681
  /* only redraw if the currently selected day changed */
1682
  /* FIXME: this should specify a rectangle containing only the days that changed */
1683
  /* using InvalidateRect */
1684 1685
  if(oldselday != infoPtr->curSelDay)
    InvalidateRect(hwnd, NULL, FALSE);
1686

1687
  return 0;
1688 1689
}

1690

1691
static LRESULT
1692
MONTHCAL_Paint(HWND hwnd, WPARAM wParam)
1693
{
1694
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1695 1696
  HDC hdc;
  PAINTSTRUCT ps;
1697

1698 1699 1700
  /* fill ps.rcPaint with a default rect */
  memcpy(&(ps.rcPaint), &(infoPtr->rcClient), sizeof(infoPtr->rcClient));

1701
  hdc = (wParam==0 ? BeginPaint(hwnd, &ps) : (HDC)wParam);
1702
  MONTHCAL_Refresh(hwnd, hdc, &ps);
1703
  if(!wParam) EndPaint(hwnd, &ps);
1704
  return 0;
1705 1706
}

1707

1708
static LRESULT
1709
MONTHCAL_KillFocus(HWND hwnd, WPARAM wParam, LPARAM lParam)
1710
{
1711
  TRACE("\n");
1712

1713
  InvalidateRect(hwnd, NULL, TRUE);
1714

1715
  return 0;
1716 1717 1718 1719
}


static LRESULT
1720
MONTHCAL_SetFocus(HWND hwnd, WPARAM wParam, LPARAM lParam)
1721
{
1722
  TRACE("\n");
1723

1724
  InvalidateRect(hwnd, NULL, FALSE);
1725

1726
  return 0;
1727 1728
}

1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
/* sets the size information */
static void MONTHCAL_UpdateSize(HWND hwnd)
{
  HDC hdc = GetDC(hwnd);
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
  RECT *rcClient=&infoPtr->rcClient;
  RECT *rcDraw=&infoPtr->rcDraw;
  RECT *title=&infoPtr->title;
  RECT *prev=&infoPtr->titlebtnprev;
  RECT *next=&infoPtr->titlebtnnext;
  RECT *titlemonth=&infoPtr->titlemonth;
  RECT *titleyear=&infoPtr->titleyear;
1741 1742
  RECT *wdays=&infoPtr->wdays;
  RECT *weeknumrect=&infoPtr->weeknums;
1743
  RECT *days=&infoPtr->days;
1744
  RECT *todayrect=&infoPtr->todayrect;
1745 1746 1747 1748
  SIZE size;
  TEXTMETRICA tm;
  DWORD dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
  HFONT currentFont;
1749
  double xdiv;
1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778

  currentFont = SelectObject(hdc, infoPtr->hFont);

  /* FIXME: need a way to determine current font, without setting it */
  /*
  if(infoPtr->hFont!=currentFont) {
    SelectObject(hdc, currentFont);
    infoPtr->hFont=currentFont;
    GetObjectA(currentFont, sizeof(LOGFONTA), &logFont);
    logFont.lfWeight=FW_BOLD;
    infoPtr->hBoldFont = CreateFontIndirectA(&logFont);
  }
  */

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

  /* retrieve the controls client rectangle info infoPtr->rcClient */
  GetClientRect(hwnd, rcClient);

  /* rcDraw is the rectangle the control is drawn in */
  rcDraw->left = rcClient->left;
  rcDraw->right = rcClient->right;
  rcDraw->top = rcClient->top;
  rcDraw->bottom = rcClient->bottom;

1779
  /* recalculate the height and width increments and offsets */
1780 1781
  /* FIXME: We use up all available width. This will inhibit having multiple
     calendars in a row, like win doesn
1782 1783 1784 1785 1786
  */
  if(dwStyle & MCS_WEEKNUMBERS)
    xdiv=8.0;
  else
    xdiv=7.0;
1787 1788
  infoPtr->width_increment = (infoPtr->rcDraw.right - infoPtr->rcDraw.left) / xdiv;
  infoPtr->height_increment = (infoPtr->rcDraw.bottom - infoPtr->rcDraw.top) / 10.0;
1789 1790 1791 1792
  infoPtr->left_offset = (infoPtr->rcDraw.right - infoPtr->rcDraw.left) - (infoPtr->width_increment * xdiv);
  infoPtr->top_offset = (infoPtr->rcDraw.bottom - infoPtr->rcDraw.top) - (infoPtr->height_increment * 10.0);

  rcDraw->bottom = rcDraw->top + 10 * infoPtr->height_increment;
1793 1794 1795 1796
  /* this is correct, the control does NOT expand vertically */
  /* like it does horizontally */
  /* make sure we don't move the controls bottom out of the client */
  /* area */
1797 1798 1799 1800
  /* title line has about 3 text heights, abrev days line, 6 weeksline and today circle line*/
  /*if((rcDraw->top + 9 * infoPtr->textHeight + 5) < rcDraw->bottom) {
    rcDraw->bottom = rcDraw->top + 9 * infoPtr->textHeight + 5;
    }*/
1801

1802
  /* calculate title area */
1803
  title->top    = rcClient->top;
1804
  title->bottom = title->top + 2 * infoPtr->height_increment;
1805 1806
  title->left   = rcClient->left;
  title->right  = rcClient->right;
1807 1808 1809

  /* set the dimensions of the next and previous buttons and center */
  /* the month text vertically */
1810 1811 1812 1813 1814 1815
  prev->top    = next->top    = title->top + 6;
  prev->bottom = next->bottom = title->bottom - 6;
  prev->left   = title->left  + 6;
  prev->right  = prev->left + (title->bottom - title->top) ;
  next->right  = title->right - 6;
  next->left   = next->right - (title->bottom - title->top);
1816

1817 1818 1819
  /* 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 */
1820 1821
  titlemonth->top    = titleyear->top    = title->top    + (infoPtr->height_increment)/2;
  titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
1822

1823 1824
  /* setup the dimensions of the rectangle we draw the names of the */
  /* days of the week in */
1825
  weeknumrect->left =infoPtr->left_offset;
1826
  if(dwStyle & MCS_WEEKNUMBERS)
1827 1828 1829 1830 1831 1832 1833
    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;
1834

1835 1836
  days->top    = weeknumrect->top = wdays->bottom ;
  days->bottom = weeknumrect->bottom = days->top     + 6 * infoPtr->height_increment;
1837

1838 1839 1840 1841 1842
  todayrect->left   = rcClient->left;
  todayrect->right  = rcClient->right;
  todayrect->top    = days->bottom;
  todayrect->bottom = days->bottom + infoPtr->height_increment;

1843
  /* uncomment for excessive debugging
1844 1845 1846 1847 1848 1849 1850 1851
  TRACE("dx=%d dy=%d rcC[%d %d %d %d] t[%d %d %d %d] wd[%d %d %d %d] w[%d %d %d %d] t[%d %d %d %d]\n",
	infoPtr->width_increment,infoPtr->height_increment,
	 rcClient->left, rcClient->right, rcClient->top, rcClient->bottom,
	    title->left,    title->right,    title->top,    title->bottom,
	    wdays->left,    wdays->right,    wdays->top,    wdays->bottom,
	     days->left,     days->right,     days->top,     days->bottom,
	todayrect->left,todayrect->right,todayrect->top,todayrect->bottom);
  */
1852

1853
  /* restore the originally selected font */
1854
  SelectObject(hdc, currentFont);
1855 1856 1857 1858 1859 1860

  ReleaseDC(hwnd, hdc);
}

static LRESULT MONTHCAL_Size(HWND hwnd, int Width, int Height)
{
1861
  TRACE("(hwnd=%p, width=%d, height=%d)\n", hwnd, Width, Height);
1862 1863 1864 1865 1866 1867 1868 1869

  MONTHCAL_UpdateSize(hwnd);

  /* invalidate client area and erase background */
  InvalidateRect(hwnd, NULL, TRUE);

  return 0;
}
1870 1871

/* FIXME: check whether dateMin/dateMax need to be adjusted. */
1872
static LRESULT
1873
MONTHCAL_Create(HWND hwnd, WPARAM wParam, LPARAM lParam)
1874
{
1875 1876
  MONTHCAL_INFO *infoPtr;
  LOGFONTA	logFont;
1877

1878
  /* allocate memory for info structure */
1879
  infoPtr =(MONTHCAL_INFO*)Alloc(sizeof(MONTHCAL_INFO));
1880
  SetWindowLongA(hwnd, 0, (DWORD)infoPtr);
1881

1882 1883
  if(infoPtr == NULL) {
    ERR( "could not allocate info memory!\n");
1884 1885
    return 0;
  }
1886 1887
  if((MONTHCAL_INFO*)GetWindowLongA(hwnd, 0) != infoPtr) {
    ERR( "pointer assignment error!\n");
1888 1889
    return 0;
  }
1890

1891 1892
  infoPtr->hwndNotify = ((LPCREATESTRUCTW)lParam)->hwndParent;

1893 1894
  infoPtr->hFont = GetStockObject(DEFAULT_GUI_FONT);
  GetObjectA(infoPtr->hFont, sizeof(LOGFONTA), &logFont);
1895
  logFont.lfWeight = FW_BOLD;
1896
  infoPtr->hBoldFont = CreateFontIndirectA(&logFont);
1897 1898

  /* initialize info structure */
1899
  /* FIXME: calculate systemtime ->> localtime(substract timezoneinfo) */
1900 1901

  GetSystemTime(&infoPtr->todaysDate);
1902
  MONTHCAL_SetFirstDayOfWeek(hwnd,0,(LPARAM)-1);
1903 1904
  infoPtr->currentMonth = infoPtr->todaysDate.wMonth;
  infoPtr->currentYear = infoPtr->todaysDate.wYear;
1905 1906
  MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->minDate);
  MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
1907 1908
  infoPtr->maxDate.wYear=2050;
  infoPtr->minDate.wYear=1950;
1909
  infoPtr->maxSelCount  = 7;
1910
  infoPtr->monthRange = 3;
1911
  infoPtr->monthdayState = Alloc
1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
                         (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 */
  MONTHCAL_UpdateSize(hwnd);
1923 1924

  return 0;
1925 1926 1927 1928
}


static LRESULT
1929
MONTHCAL_Destroy(HWND hwnd, WPARAM wParam, LPARAM lParam)
1930
{
1931
  MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1932

1933
  /* free month calendar info data */
1934
  if(infoPtr->monthdayState)
1935 1936
      Free(infoPtr->monthdayState);
  Free(infoPtr);
1937
  SetWindowLongA(hwnd, 0, 0);
1938
  return 0;
1939 1940 1941
}


1942
static LRESULT WINAPI
1943
MONTHCAL_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1944
{
1945
  TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hwnd, uMsg, wParam, lParam);
1946 1947
  if (!MONTHCAL_GetInfoPtr(hwnd) && (uMsg != WM_CREATE))
    return DefWindowProcA(hwnd, uMsg, wParam, lParam);
1948
  switch(uMsg)
1949 1950
  {
  case MCM_GETCURSEL:
1951
    return MONTHCAL_GetCurSel(hwnd, wParam, lParam);
1952

1953
  case MCM_SETCURSEL:
1954
    return MONTHCAL_SetCurSel(hwnd, wParam, lParam);
1955

1956
  case MCM_GETMAXSELCOUNT:
1957
    return MONTHCAL_GetMaxSelCount(hwnd, wParam, lParam);
1958

1959
  case MCM_SETMAXSELCOUNT:
1960
    return MONTHCAL_SetMaxSelCount(hwnd, wParam, lParam);
1961

1962
  case MCM_GETSELRANGE:
1963
    return MONTHCAL_GetSelRange(hwnd, wParam, lParam);
1964

1965
  case MCM_SETSELRANGE:
1966
    return MONTHCAL_SetSelRange(hwnd, wParam, lParam);
1967

1968
  case MCM_GETMONTHRANGE:
1969
    return MONTHCAL_GetMonthRange(hwnd, wParam, lParam);
1970

1971
  case MCM_SETDAYSTATE:
1972
    return MONTHCAL_SetDayState(hwnd, wParam, lParam);
1973

1974
  case MCM_GETMINREQRECT:
1975
    return MONTHCAL_GetMinReqRect(hwnd, wParam, lParam);
1976

1977
  case MCM_GETCOLOR:
1978
    return MONTHCAL_GetColor(hwnd, wParam, lParam);
1979

1980
  case MCM_SETCOLOR:
1981
    return MONTHCAL_SetColor(hwnd, wParam, lParam);
1982

1983
  case MCM_GETTODAY:
1984
    return MONTHCAL_GetToday(hwnd, wParam, lParam);
1985

1986
  case MCM_SETTODAY:
1987
    return MONTHCAL_SetToday(hwnd, wParam, lParam);
1988

1989
  case MCM_HITTEST:
1990
    return MONTHCAL_HitTest(hwnd,lParam);
1991

1992
  case MCM_GETFIRSTDAYOFWEEK:
1993
    return MONTHCAL_GetFirstDayOfWeek(hwnd, wParam, lParam);
1994

1995
  case MCM_SETFIRSTDAYOFWEEK:
1996
    return MONTHCAL_SetFirstDayOfWeek(hwnd, wParam, lParam);
1997

1998
  case MCM_GETRANGE:
1999
    return MONTHCAL_GetRange(hwnd, wParam, lParam);
2000

2001
  case MCM_SETRANGE:
2002
    return MONTHCAL_SetRange(hwnd, wParam, lParam);
2003

2004
  case MCM_GETMONTHDELTA:
2005
    return MONTHCAL_GetMonthDelta(hwnd, wParam, lParam);
2006

2007
  case MCM_SETMONTHDELTA:
2008
    return MONTHCAL_SetMonthDelta(hwnd, wParam, lParam);
2009

2010
  case MCM_GETMAXTODAYWIDTH:
2011
    return MONTHCAL_GetMaxTodayWidth(hwnd);
2012

2013 2014
  case WM_GETDLGCODE:
    return DLGC_WANTARROWS | DLGC_WANTCHARS;
2015

2016
  case WM_KILLFOCUS:
2017
    return MONTHCAL_KillFocus(hwnd, wParam, lParam);
2018

2019 2020 2021
  case WM_RBUTTONDOWN:
    return MONTHCAL_RButtonDown(hwnd, wParam, lParam);

2022
  case WM_LBUTTONDOWN:
2023
    return MONTHCAL_LButtonDown(hwnd, wParam, lParam);
2024

2025
  case WM_MOUSEMOVE:
2026
    return MONTHCAL_MouseMove(hwnd, wParam, lParam);
2027

2028
  case WM_LBUTTONUP:
2029
    return MONTHCAL_LButtonUp(hwnd, wParam, lParam);
2030

2031
  case WM_PAINT:
2032
    return MONTHCAL_Paint(hwnd, wParam);
2033

2034
  case WM_SETFOCUS:
2035 2036 2037
    return MONTHCAL_SetFocus(hwnd, wParam, lParam);

  case WM_SIZE:
2038
    return MONTHCAL_Size(hwnd, (short)LOWORD(lParam), (short)HIWORD(lParam));
2039

2040
  case WM_CREATE:
2041
    return MONTHCAL_Create(hwnd, wParam, lParam);
2042

2043
  case WM_TIMER:
2044
    return MONTHCAL_Timer(hwnd, wParam, lParam);
2045

2046
  case WM_DESTROY:
2047
    return MONTHCAL_Destroy(hwnd, wParam, lParam);
2048

2049
  default:
2050
    if ((uMsg >= WM_USER) && (uMsg < WM_APP))
2051 2052
      ERR( "unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
    return DefWindowProcA(hwnd, uMsg, wParam, lParam);
2053 2054
  }
  return 0;
2055 2056 2057
}


2058
void
2059
MONTHCAL_Register(void)
2060
{
2061 2062
  WNDCLASSA wndClass;

2063
  ZeroMemory(&wndClass, sizeof(WNDCLASSA));
2064 2065 2066 2067
  wndClass.style         = CS_GLOBALCLASS;
  wndClass.lpfnWndProc   = (WNDPROC)MONTHCAL_WindowProc;
  wndClass.cbClsExtra    = 0;
  wndClass.cbWndExtra    = sizeof(MONTHCAL_INFO *);
2068
  wndClass.hCursor       = LoadCursorA(0, (LPSTR)IDC_ARROW);
2069 2070
  wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  wndClass.lpszClassName = MONTHCAL_CLASSA;
2071

2072
  RegisterClassA(&wndClass);
2073 2074 2075
}


2076
void
2077
MONTHCAL_Unregister(void)
2078
{
2079
    UnregisterClassA(MONTHCAL_CLASSA, NULL);
2080
}