Copy wininet to branch
[reactos.git] / reactos / lib / comctl32 / monthcal.c
1 /* Month calendar control
2
3 *
4 * Copyright 1998, 1999 Eric Kohl (ekohl@abo.rhein-zeitung.de)
5 * Copyright 1999 Alex Priem (alexp@sci.kun.nl)
6 * Copyright 1999 Chris Morgan <cmorgan@wpi.edu> and
7 * James Abbatiello <abbeyj@wpi.edu>
8 * Copyright 2000 Uwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * NOTE
25 *
26 * This code was audited for completeness against the documented features
27 * of Comctl32.dll version 6.0 on Oct. 20, 2004, by Dimitrie O. Paun.
28 *
29 * Unless otherwise noted, we believe this code to be complete, as per
30 * the specification mentioned above.
31 * If you discover missing features, or bugs, please note them below.
32 *
33 * TODO:
34 * -- MCM_[GS]ETUNICODEFORMAT
35 * -- MONTHCAL_GetMonthRange
36 * -- Unicodification
37 * -- handle resources better (doesn't work now);
38 * -- take care of internationalization.
39 * -- keyboard handling.
40 * -- GetRange: At the moment, we copy ranges anyway, regardless of
41 * infoPtr->rangeValid; a invalid range is simply filled
42 * with zeros in SetRange. Is this the right behavior?
43 * -- 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 #include "winuser.h"
56 #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 #define MC_SEL_LBUTUP 1 /* Left button released */
64 #define MC_SEL_LBUTDOWN 2 /* Left button pressed in calendar */
65 #define MC_PREVPRESSED 4 /* Prev month button pressed */
66 #define MC_NEXTPRESSED 8 /* Next month button pressed */
67 #define MC_NEXTMONTHDELAY 350 /* when continuously pressing `next */
68 /* month', wait 500 ms before going */
69 /* to the next month */
70 #define MC_NEXTMONTHTIMER 1 /* Timer ID's */
71 #define MC_PREVMONTHTIMER 2
72
73 typedef struct
74 {
75 HWND hwndSelf;
76 COLORREF bk;
77 COLORREF txt;
78 COLORREF titlebk;
79 COLORREF titletxt;
80 COLORREF monthbk;
81 COLORREF trailingtxt;
82 HFONT hFont;
83 HFONT hBoldFont;
84 int textHeight;
85 int textWidth;
86 int height_increment;
87 int width_increment;
88 int firstDayplace; /* place of the first day of the current month */
89 int delta; /* scroll rate; # of months that the */
90 /* control moves when user clicks a scroll button */
91 int visible; /* # of months visible */
92 int firstDay; /* Start month calendar with firstDay's day */
93 int monthRange;
94 MONTHDAYSTATE *monthdayState;
95 SYSTEMTIME todaysDate;
96 DWORD currentMonth;
97 DWORD currentYear;
98 int status; /* See MC_SEL flags */
99 int curSelDay; /* current selected day */
100 int firstSelDay; /* first selected day */
101 int maxSelCount;
102 SYSTEMTIME minSel;
103 SYSTEMTIME maxSel;
104 DWORD rangeValid;
105 SYSTEMTIME minDate;
106 SYSTEMTIME maxDate;
107
108 RECT title; /* rect for the header above the calendar */
109 RECT titlebtnnext; /* the `next month' button in the header */
110 RECT titlebtnprev; /* the `prev month' button in the header */
111 RECT titlemonth; /* the `month name' txt in the header */
112 RECT titleyear; /* the `year number' txt in the header */
113 RECT wdays; /* week days at top */
114 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 HWND hWndYearEdit; /* Window Handle of edit box to handle years */
119 HWND hWndYearUpDown;/* Window Handle of updown box to handle years */
120 } MONTHCAL_INFO, *LPMONTHCAL_INFO;
121
122
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 /* helper functions */
130
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};
136 /*Wrap around, this eases handling*/
137 if(month == 0)
138 month = 12;
139 if(month == 13)
140 month = 1;
141
142 /* if we have a leap year add 1 day to February */
143 /* a leap year is a year either divisible by 400 */
144 /* or divisible by 4 and not by 100 */
145 if(month == 2) { /* February */
146 return mdays[month - 1] + ((year%400 == 0) ? 1 : ((year%100 != 0) &&
147 (year%4 == 0)) ? 1 : 0);
148 }
149 else {
150 return mdays[month - 1];
151 }
152 }
153
154
155 /* make sure that time is valid */
156 static int MONTHCAL_ValidateTime(SYSTEMTIME time)
157 {
158 if(time.wMonth > 12) return FALSE;
159 if(time.wDayOfWeek > 6) return FALSE;
160 if(time.wDay > MONTHCAL_MonthLength(time.wMonth, time.wYear))
161 return FALSE;
162 if(time.wHour > 23) return FALSE;
163 if(time.wMinute > 59) return FALSE;
164 if(time.wSecond > 59) return FALSE;
165 if(time.wMilliseconds > 999) return FALSE;
166
167 return TRUE;
168 }
169
170
171 void MONTHCAL_CopyTime(const SYSTEMTIME *from, SYSTEMTIME *to)
172 {
173 to->wYear = from->wYear;
174 to->wMonth = from->wMonth;
175 to->wDayOfWeek = from->wDayOfWeek;
176 to->wDay = from->wDay;
177 to->wHour = from->wHour;
178 to->wMinute = from->wMinute;
179 to->wSecond = from->wSecond;
180 to->wMilliseconds = from->wMilliseconds;
181 }
182
183
184 /* Note:Depending on DST, this may be offset by a day.
185 Need to find out if we're on a DST place & adjust the clock accordingly.
186 Above function assumes we have a valid data.
187 Valid for year>1752; 1 <= d <= 31, 1 <= m <= 12.
188 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 /* From a given point, calculate the row (weekpos), column(daypos)
202 and day in the calendar. day== 0 mean the last day of tha last month
203 */
204 static int MONTHCAL_CalcDayFromPos(MONTHCAL_INFO *infoPtr, int x, int y,
205 int *daypos,int *weekpos)
206 {
207 int retval, firstDay;
208 RECT rcClient;
209
210 GetClientRect(infoPtr->hwndSelf, &rcClient);
211
212 /* if the point is outside the x bounds of the window put
213 it at the boundry */
214 if (x > rcClient.right)
215 x = rcClient.right;
216
217
218 *daypos = (x - infoPtr->days.left ) / infoPtr->width_increment;
219 *weekpos = (y - infoPtr->days.top ) / infoPtr->height_increment;
220
221 firstDay = (MONTHCAL_CalculateDayOfWeek(1, infoPtr->currentMonth, infoPtr->currentYear)+6 - infoPtr->firstDay)%7;
222 retval = *daypos + (7 * *weekpos) - firstDay;
223 return retval;
224 }
225
226 /* day is the day of the month, 1 == 1st day of the month */
227 /* 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 int *x, int *y)
231 {
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 *x = (day + firstDay) % 7;
238 *y = (day + firstDay - *x) / 7;
239 return;
240 }
241 if(month < infoPtr->currentMonth) {
242 prevMonth = month - 1;
243 if(prevMonth==0)
244 prevMonth = 12;
245
246 *x = (MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear) - firstDay) % 7;
247 *y = 0;
248 return;
249 }
250
251 *y = MONTHCAL_MonthLength(month, infoPtr->currentYear - 1) / 7;
252 *x = (day + firstDay + MONTHCAL_MonthLength(month,
253 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 r->bottom = r->top + infoPtr->textHeight;
264 }
265
266
267 /* sets the RECT struct r to the rectangle around the day and month */
268 /* day is the day value of the month(1 == 1st), month is the month */
269 /* value(january == 1, december == 12) */
270 static inline void MONTHCAL_CalcPosFromDay(MONTHCAL_INFO *infoPtr,
271 int day, int month, RECT *r)
272 {
273 int x, y;
274
275 MONTHCAL_CalcDayXY(infoPtr, day, month, &x, &y);
276 MONTHCAL_CalcDayRect(infoPtr, r, x, y);
277 }
278
279
280 /* day is the day in the month(1 == 1st of the month) */
281 /* 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 HPEN hRedPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
285 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 x = day_rect.left;
294 y = day_rect.top;
295
296 points[0].x = x;
297 points[0].y = y - 1;
298 points[1].x = x + 0.8 * infoPtr->width_increment;
299 points[1].y = y - 1;
300 points[2].x = x + 0.9 * infoPtr->width_increment;
301 points[2].y = y;
302 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 points[11].y = y + 0.3 * infoPtr->height_increment;
324 points[12].x = x + 0.4 * infoPtr->width_increment;
325 points[12].y = y + 0.2 * infoPtr->height_increment;
326
327 PolyBezier(hdc, points, 13);
328 DeleteObject(hRedPen);
329 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 char buf[10];
337 RECT r;
338 static int haveBoldFont, haveSelectedDay = FALSE;
339 HBRUSH hbr;
340 COLORREF oldCol = 0;
341 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 * 1<day<31, so evertyhing's OK.
347 */
348
349 MONTHCAL_CalcDayRect(infoPtr, &r, x, y);
350
351 if((day>=infoPtr->minSel.wDay) && (day<=infoPtr->maxSel.wDay)
352 && (month==infoPtr->currentMonth)) {
353 HRGN hrgn;
354 RECT r2;
355
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 oldCol = SetTextColor(hdc, infoPtr->monthbk);
359 oldBk = SetBkColor(hdc, infoPtr->trailingtxt);
360 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
361 hrgn = CreateEllipticRgn(r.left, r.top, r.right, r.bottom);
362 FillRgn(hdc, hrgn, hbr);
363
364 /* FIXME: this may need to be changed now b/c of the other
365 drawing changes 11/3/99 CMM */
366 r2.left = r.left - 0.25 * infoPtr->textWidth;
367 r2.top = r.top;
368 r2.right = r.left + 0.5 * infoPtr->textWidth;
369 r2.bottom = r.bottom;
370 if(haveSelectedDay) FillRect(hdc, &r2, hbr);
371 haveSelectedDay = TRUE;
372 } else {
373 haveSelectedDay = FALSE;
374 }
375
376 /* need to add some code for multiple selections */
377
378 if((bold) &&(!haveBoldFont)) {
379 SelectObject(hdc, infoPtr->hBoldFont);
380 haveBoldFont = TRUE;
381 }
382 if((!bold) &&(haveBoldFont)) {
383 SelectObject(hdc, infoPtr->hFont);
384 haveBoldFont = FALSE;
385 }
386
387 if(haveSelectedDay) {
388 SetTextColor(hdc, oldCol);
389 SetBkColor(hdc, oldBk);
390 }
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 if((day==infoPtr->curSelDay) && (month==infoPtr->currentMonth))
397 DrawFocusRect(hdc, &r);
398 }
399
400
401 static void MONTHCAL_Refresh(MONTHCAL_INFO *infoPtr, HDC hdc, PAINTSTRUCT* ps)
402 {
403 RECT *title=&infoPtr->title;
404 RECT *prev=&infoPtr->titlebtnprev;
405 RECT *next=&infoPtr->titlebtnnext;
406 RECT *titlemonth=&infoPtr->titlemonth;
407 RECT *titleyear=&infoPtr->titleyear;
408 RECT dayrect;
409 RECT *days=&dayrect;
410 RECT rtoday;
411 int i, j, m, mask, day, firstDay, weeknum, weeknum1,prevMonth;
412 int textHeight = infoPtr->textHeight, textWidth = infoPtr->textWidth;
413 SIZE size;
414 HBRUSH hbr;
415 HFONT currentFont;
416 char buf[20];
417 char buf1[20];
418 char buf2[32];
419 COLORREF oldTextColor, oldBkColor;
420 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
421 RECT rcTemp;
422 RECT rcDay; /* used in MONTHCAL_CalcDayRect() */
423 SYSTEMTIME localtime;
424 int startofprescal;
425
426 oldTextColor = SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
427
428 /* fill background */
429 hbr = CreateSolidBrush (infoPtr->bk);
430 FillRect(hdc, &ps->rcPaint, hbr);
431 DeleteObject(hbr);
432
433 /* draw header */
434 if(IntersectRect(&rcTemp, &(ps->rcPaint), title))
435 {
436 hbr = CreateSolidBrush(infoPtr->titlebk);
437 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 if((infoPtr->status & MC_PREVPRESSED))
445 DrawFrameControl(hdc, prev, DFC_SCROLL,
446 DFCS_SCROLLLEFT | DFCS_PUSHED |
447 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
448 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 if(IntersectRect(&rcTemp, &(ps->rcPaint), next))
455 {
456 if((infoPtr->status & MC_NEXTPRESSED))
457 DrawFrameControl(hdc, next, DFC_SCROLL,
458 DFCS_SCROLLRIGHT | DFCS_PUSHED |
459 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
460 else /* if the next button is pressed draw it depressed */
461 DrawFrameControl(hdc, next, DFC_SCROLL,
462 DFCS_SCROLLRIGHT |(dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0));
463 }
464
465 oldBkColor = SetBkColor(hdc, infoPtr->titlebk);
466 SetTextColor(hdc, infoPtr->titletxt);
467 currentFont = SelectObject(hdc, infoPtr->hBoldFont);
468
469 /* titlemonth->left and right are set in MONTHCAL_UpdateSize */
470 titlemonth->left = title->left;
471 titlemonth->right = title->right;
472
473 GetLocaleInfoA( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+infoPtr->currentMonth -1,
474 buf1,sizeof(buf1));
475 sprintf(buf, "%s %ld", buf1, infoPtr->currentYear);
476
477 if(IntersectRect(&rcTemp, &(ps->rcPaint), titlemonth))
478 {
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 titlemonth->left = title->right / 2 - size.cx / 2;
491 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 /* draw month area */
497 rcTemp.top=infoPtr->wdays.top;
498 rcTemp.left=infoPtr->wdays.left;
499 rcTemp.bottom=infoPtr->todayrect.bottom;
500 rcTemp.right =infoPtr->todayrect.right;
501 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcTemp))
502 {
503 hbr = CreateSolidBrush(infoPtr->monthbk);
504 FillRect(hdc, &rcTemp, hbr);
505 DeleteObject(hbr);
506 }
507
508 /* draw line under day abbreviatons */
509
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 prevMonth = infoPtr->currentMonth - 1;
514 if(prevMonth == 0) /* if currentMonth is january(1) prevMonth is */
515 prevMonth = 12; /* december(12) of the previous year */
516
517 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 /* copy this rect so we can change the values without changing */
524 /* the original version */
525 days->left = infoPtr->wdays.left;
526 days->right = days->left + infoPtr->width_increment;
527 days->top = infoPtr->wdays.top;
528 days->bottom = infoPtr->wdays.bottom;
529
530 i = infoPtr->firstDay;
531
532 for(j=0; j<7; j++) {
533 GetLocaleInfoA( LOCALE_USER_DEFAULT,LOCALE_SABBREVDAYNAME1 + (i +j)%7,
534 buf,sizeof(buf));
535 DrawTextA(hdc, buf, strlen(buf), days,
536 DT_CENTER | DT_VCENTER | DT_SINGLELINE );
537 days->left+=infoPtr->width_increment;
538 days->right+=infoPtr->width_increment;
539 }
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 (infoPtr->firstDay + 7 - firstDay)%7 + 1;
547 if (day > MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear))
548 day -=7;
549 startofprescal = day;
550 mask = 1<<(day-1);
551
552 i = 0;
553 m = 0;
554 while(day <= MONTHCAL_MonthLength(prevMonth, infoPtr->currentYear)) {
555 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
556 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
557 {
558 MONTHCAL_DrawDay(infoPtr, hdc, day, prevMonth, i, 0,
559 infoPtr->monthdayState[m] & mask);
560 }
561
562 mask<<=1;
563 day++;
564 i++;
565 }
566
567 /* draw `current' month */
568
569 day = 1; /* start at the beginning of the current month */
570
571 infoPtr->firstDayplace = i;
572 SetTextColor(hdc, infoPtr->txt);
573 m++;
574 mask = 1;
575
576 /* draw the first week of the current month */
577 while(i<7) {
578 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, 0);
579 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
580 {
581
582 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth, i, 0,
583 infoPtr->monthdayState[m] & mask);
584
585 if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
586 (day==infoPtr->todaysDate.wDay) &&
587 (infoPtr->currentYear == infoPtr->todaysDate.wYear)) {
588 if(!(dwStyle & MCS_NOTODAYCIRCLE))
589 MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->currentMonth);
590 }
591 }
592
593 mask<<=1;
594 day++;
595 i++;
596 }
597
598 j = 1; /* move to the 2nd week of the current month */
599 i = 0; /* move back to sunday */
600 while(day <= MONTHCAL_MonthLength(infoPtr->currentMonth, infoPtr->currentYear)) {
601 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
602 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rcDay))
603 {
604 MONTHCAL_DrawDay(infoPtr, hdc, day, infoPtr->currentMonth, i, j,
605 infoPtr->monthdayState[m] & mask);
606
607 if((infoPtr->currentMonth==infoPtr->todaysDate.wMonth) &&
608 (day==infoPtr->todaysDate.wDay) &&
609 (infoPtr->currentYear == infoPtr->todaysDate.wYear))
610 if(!(dwStyle & MCS_NOTODAYCIRCLE))
611 MONTHCAL_CircleDay(infoPtr, hdc, day, infoPtr->currentMonth);
612 }
613 mask<<=1;
614 day++;
615 i++;
616 if(i>6) { /* past saturday, goto the next weeks sunday */
617 i = 0;
618 j++;
619 }
620 }
621
622 /* draw `next' month */
623
624 day = 1; /* start at the first day of the next month */
625 m++;
626 mask = 1;
627
628 SetTextColor(hdc, infoPtr->trailingtxt);
629 while((i<7) &&(j<6)) {
630 MONTHCAL_CalcDayRect(infoPtr, &rcDay, i, j);
631 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 mask<<=1;
638 day++;
639 i++;
640 if(i==7) { /* past saturday, go to next week's sunday */
641 i = 0;
642 j++;
643 }
644 }
645 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 offset+=textWidth;
659 }
660 if (!LoadStringA(COMCTL32_hModule,IDM_TODAY,buf1,sizeof(buf1)))
661 {
662 WARN("Can't load resource\n");
663 strcpy(buf1,"Today:");
664 }
665 MONTHCAL_CalcDayRect(infoPtr, &rtoday, 1, 6);
666 MONTHCAL_CopyTime(&infoPtr->todaysDate,&localtime);
667 GetDateFormatA(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&localtime,NULL,buf2,sizeof(buf2));
668 sprintf(buf, "%s %s", buf1,buf2);
669 SelectObject(hdc, infoPtr->hBoldFont);
670
671 if(IntersectRect(&rcTemp, &(ps->rcPaint), &rtoday))
672 {
673 DrawTextA(hdc, buf, -1, &rtoday, DT_CALCRECT | DT_LEFT | DT_VCENTER | DT_SINGLELINE);
674 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 int mindays;
683
684 /* Rules what week to call the first week of a new year:
685 LOCALE_IFIRSTWEEKOFYEAR == 0 (e.g US?):
686 The week containing Jan 1 is the first week of year
687 LOCALE_IFIRSTWEEKOFYEAR == 2 (e.g. Germany):
688 First week of year must contain 4 days of the new year
689 LOCALE_IFIRSTWEEKOFYEAR == 1 (what contries?)
690 The first week of the year must contain only days of the new year
691 */
692 GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IFIRSTWEEKOFYEAR,
693 buf, sizeof(buf));
694 sscanf(buf, "%d", &weeknum);
695 switch (weeknum)
696 {
697 case 1: mindays = 6;
698 break;
699 case 2: mindays = 3;
700 break;
701 case 0:
702 default:
703 mindays = 0;
704 }
705 if (infoPtr->currentMonth < 2)
706 {
707 /* calculate all those exceptions for january */
708 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear);
709 if ((infoPtr->firstDay +7 - weeknum1)%7 > mindays)
710 weeknum =1;
711 else
712 {
713 weeknum = 0;
714 for(i=0; i<11; i++)
715 weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->currentYear-1);
716 weeknum +=startofprescal+ 7;
717 weeknum /=7;
718 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear-1);
719 if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
720 weeknum++;
721 }
722 }
723 else
724 {
725 weeknum = 0;
726 for(i=0; i<prevMonth-1; i++)
727 weeknum+=MONTHCAL_MonthLength(i+1, infoPtr->currentYear);
728 weeknum +=startofprescal+ 7;
729 weeknum /=7;
730 weeknum1=MONTHCAL_CalculateDayOfWeek(1,1,infoPtr->currentYear);
731 if ((infoPtr->firstDay + 7 - weeknum1)%7 > mindays)
732 weeknum++;
733 }
734 days->left = infoPtr->weeknums.left;
735 days->right = infoPtr->weeknums.right;
736 days->top = infoPtr->weeknums.top;
737 days->bottom = days->top +infoPtr->height_increment;
738 for(i=0; i<6; i++) {
739 if((i==0)&&(weeknum>50))
740 {
741 sprintf(buf, "%d", weeknum);
742 weeknum=0;
743 }
744 else if((i==5)&&(weeknum>47))
745 {
746 sprintf(buf, "%d", 1);
747 }
748 else
749 sprintf(buf, "%d", weeknum + i);
750 DrawTextA(hdc, buf, -1, days, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
751 days->top+=infoPtr->height_increment;
752 days->bottom+=infoPtr->height_increment;
753 }
754
755 MoveToEx(hdc, infoPtr->weeknums.right, infoPtr->weeknums.top + 3 , NULL);
756 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 /* validate parameters */
775
776 if((infoPtr==NULL) ||(lpRect == NULL) ) return FALSE;
777
778 lpRect->left = infoPtr->title.left;
779 lpRect->top = infoPtr->title.top;
780 lpRect->right = infoPtr->title.right;
781 lpRect->bottom = infoPtr->todayrect.bottom;
782 AdjustWindowRect(lpRect, GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE), FALSE);
783
784 TRACE("%s\n", wine_dbgstr_rect(lpRect));
785
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 case MCSC_BACKGROUND:
797 return infoPtr->bk;
798 case MCSC_TEXT:
799 return infoPtr->txt;
800 case MCSC_TITLEBK:
801 return infoPtr->titlebk;
802 case MCSC_TITLETEXT:
803 return infoPtr->titletxt;
804 case MCSC_MONTHBK:
805 return infoPtr->monthbk;
806 case MCSC_TRAILINGTEXT:
807 return infoPtr->trailingtxt;
808 }
809
810 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 case MCSC_BACKGROUND:
823 prev = infoPtr->bk;
824 infoPtr->bk = (COLORREF)lParam;
825 break;
826 case MCSC_TEXT:
827 prev = infoPtr->txt;
828 infoPtr->txt = (COLORREF)lParam;
829 break;
830 case MCSC_TITLEBK:
831 prev = infoPtr->titlebk;
832 infoPtr->titlebk = (COLORREF)lParam;
833 break;
834 case MCSC_TITLETEXT:
835 prev=infoPtr->titletxt;
836 infoPtr->titletxt = (COLORREF)lParam;
837 break;
838 case MCSC_MONTHBK:
839 prev = infoPtr->monthbk;
840 infoPtr->monthbk = (COLORREF)lParam;
841 break;
842 case MCSC_TRAILINGTEXT:
843 prev = infoPtr->trailingtxt;
844 infoPtr->trailingtxt = (COLORREF)lParam;
845 break;
846 }
847
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 return infoPtr->delta;
860 else
861 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 infoPtr->delta = (int)wParam;
873 return prev;
874 }
875
876
877 static LRESULT
878 MONTHCAL_GetFirstDayOfWeek(MONTHCAL_INFO *infoPtr)
879 {
880 return infoPtr->firstDay;
881 }
882
883
884 /* sets the first day of the week that will appear in the control */
885 /* 0 == Monday, 6 == Sunday */
886 /* FIXME: this needs to be implemented properly in MONTHCAL_Refresh() */
887 /* FIXME: we need more error checking here */
888 static LRESULT
889 MONTHCAL_SetFirstDayOfWeek(MONTHCAL_INFO *infoPtr, LPARAM lParam)
890 {
891 int prev = infoPtr->firstDay;
892 char buf[40];
893 int day;
894
895 TRACE("day %ld\n", lParam);
896
897 if((lParam >= 0) && (lParam < 7)) {
898 infoPtr->firstDay = (int)lParam;
899 }
900 else
901 {
902 GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK,
903 buf, sizeof(buf));
904 TRACE("%s %d\n", buf, strlen(buf));
905 if(sscanf(buf, "%d", &day) == 1)
906 infoPtr->firstDay = day;
907 else
908 infoPtr->firstDay = 0;
909 }
910 return prev;
911 }
912
913
914 static LRESULT
915 MONTHCAL_GetMonthRange(MONTHCAL_INFO *infoPtr)
916 {
917 TRACE("\n");
918
919 return infoPtr->monthRange;
920 }
921
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 * FIXME: check whether MCM_GETMONTHRANGE shows correct result after
933 * adjusting range with MCM_SETRANGE
934 */
935
936 static LRESULT
937 MONTHCAL_SetRange(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
938 {
939 SYSTEMTIME *lprgSysTimeArray=(SYSTEMTIME *)lParam;
940 int prev;
941
942 TRACE("%x %lx\n", wParam, lParam);
943
944 if(wParam & GDTR_MAX) {
945 if(MONTHCAL_ValidateTime(lprgSysTimeArray[1])){
946 MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxDate);
947 infoPtr->rangeValid|=GDTR_MAX;
948 } else {
949 GetSystemTime(&infoPtr->todaysDate);
950 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
951 }
952 }
953 if(wParam & GDTR_MIN) {
954 if(MONTHCAL_ValidateTime(lprgSysTimeArray[0])) {
955 MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minDate);
956 infoPtr->rangeValid|=GDTR_MIN;
957 } 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 return 1;
972 }
973
974
975 static LRESULT
976 MONTHCAL_GetRange(HWND hwnd, WPARAM wParam, LPARAM lParam)
977 {
978 MONTHCAL_INFO *infoPtr = MONTHCAL_GetInfoPtr(hwnd);
979 SYSTEMTIME *lprgSysTimeArray = (SYSTEMTIME *)lParam;
980
981 /* validate parameters */
982
983 if((infoPtr==NULL) || (lprgSysTimeArray==NULL)) return FALSE;
984
985 MONTHCAL_CopyTime(&infoPtr->maxDate, &lprgSysTimeArray[1]);
986 MONTHCAL_CopyTime(&infoPtr->minDate, &lprgSysTimeArray[0]);
987
988 return infoPtr->rangeValid;
989 }
990
991
992 static LRESULT
993 MONTHCAL_SetDayState(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
994
995 {
996 int i, iMonths = (int)wParam;
997 MONTHDAYSTATE *dayStates = (LPMONTHDAYSTATE)lParam;
998
999 TRACE("%x %lx\n", wParam, lParam);
1000 if(iMonths!=infoPtr->monthRange) return 0;
1001
1002 for(i=0; i<iMonths; i++)
1003 infoPtr->monthdayState[i] = dayStates[i];
1004 return 1;
1005 }
1006
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 return TRUE;
1019 }
1020
1021 /* FIXME: if the specified date is not visible, make it visible */
1022 /* 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 infoPtr->currentMonth=lpSel->wMonth;
1033 infoPtr->currentYear=lpSel->wYear;
1034
1035 MONTHCAL_CopyTime(lpSel, &infoPtr->minSel);
1036 MONTHCAL_CopyTime(lpSel, &infoPtr->maxSel);
1037
1038 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1039
1040 return TRUE;
1041 }
1042
1043
1044 static LRESULT
1045 MONTHCAL_GetMaxSelCount(MONTHCAL_INFO *infoPtr)
1046 {
1047 return infoPtr->maxSelCount;
1048 }
1049
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 return TRUE;
1061 }
1062
1063
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 /* validate parameters */
1072
1073 if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1074
1075 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)
1076 {
1077 MONTHCAL_CopyTime(&infoPtr->maxSel, &lprgSysTimeArray[1]);
1078 MONTHCAL_CopyTime(&infoPtr->minSel, &lprgSysTimeArray[0]);
1079 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1080 return TRUE;
1081 }
1082
1083 return FALSE;
1084 }
1085
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 /* validate parameters */
1095
1096 if((infoPtr==NULL) ||(lprgSysTimeArray==NULL)) return FALSE;
1097
1098 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT)
1099 {
1100 MONTHCAL_CopyTime(&lprgSysTimeArray[1], &infoPtr->maxSel);
1101 MONTHCAL_CopyTime(&lprgSysTimeArray[0], &infoPtr->minSel);
1102 TRACE("[min,max]=[%d %d]\n", infoPtr->minSel.wDay, infoPtr->maxSel.wDay);
1103 return TRUE;
1104 }
1105
1106 return FALSE;
1107 }
1108
1109
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 /* validate parameters */
1118
1119 if((infoPtr==NULL) || (lpToday==NULL)) return FALSE;
1120 MONTHCAL_CopyTime(&infoPtr->todaysDate, lpToday);
1121 return TRUE;
1122 }
1123
1124
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 if((infoPtr==NULL) ||(lpToday==NULL)) return FALSE;
1135 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 PMCHITTESTINFO lpht = (PMCHITTESTINFO)lParam;
1145 UINT x,y;
1146 DWORD retval;
1147 int day,wday,wnum;
1148
1149
1150 x = lpht->pt.x;
1151 y = lpht->pt.y;
1152 retval = MCHT_NOWHERE;
1153
1154
1155 /* Comment in for debugging...
1156 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 infoPtr->wdays.left, infoPtr->wdays.right,
1158 infoPtr->wdays.top, infoPtr->wdays.bottom,
1159 infoPtr->days.left, infoPtr->days.right,
1160 infoPtr->days.top, infoPtr->days.bottom,
1161 infoPtr->todayrect.left, infoPtr->todayrect.right,
1162 infoPtr->todayrect.top, infoPtr->todayrect.bottom,
1163 infoPtr->weeknums.left, infoPtr->weeknums.right,
1164 infoPtr->weeknums.top, infoPtr->weeknums.bottom);
1165 */
1166
1167 /* are we in the header? */
1168
1169 if(PtInRect(&infoPtr->title, lpht->pt)) {
1170 if(PtInRect(&infoPtr->titlebtnprev, lpht->pt)) {
1171 retval = MCHT_TITLEBTNPREV;
1172 goto done;
1173 }
1174 if(PtInRect(&infoPtr->titlebtnnext, lpht->pt)) {
1175 retval = MCHT_TITLEBTNNEXT;
1176 goto done;
1177 }
1178 if(PtInRect(&infoPtr->titlemonth, lpht->pt)) {
1179 retval = MCHT_TITLEMONTH;
1180 goto done;
1181 }
1182 if(PtInRect(&infoPtr->titleyear, lpht->pt)) {
1183 retval = MCHT_TITLEYEAR;
1184 goto done;
1185 }
1186
1187 retval = MCHT_TITLE;
1188 goto done;
1189 }
1190
1191 day = MONTHCAL_CalcDayFromPos(infoPtr,x,y,&wday,&wnum);
1192 if(PtInRect(&infoPtr->wdays, lpht->pt)) {
1193 retval = MCHT_CALENDARDAY;
1194 lpht->st.wYear = infoPtr->currentYear;
1195 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 goto done;
1199 }
1200 if(PtInRect(&infoPtr->weeknums, lpht->pt)) {
1201 retval = MCHT_CALENDARWEEKNUM;
1202 lpht->st.wYear = infoPtr->currentYear;
1203 lpht->st.wMonth = (day < 1) ? infoPtr->currentMonth -1 :
1204 (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear)) ?
1205 infoPtr->currentMonth +1 :infoPtr->currentMonth;
1206 lpht->st.wDay = (day < 1 ) ?
1207 MONTHCAL_MonthLength(infoPtr->currentMonth-1,infoPtr->currentYear) -day :
1208 (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 retval = MCHT_CALENDARDATEPREV;
1218 lpht->st.wMonth = infoPtr->currentMonth - 1;
1219 if (lpht->st.wMonth <1)
1220 {
1221 lpht->st.wMonth = 12;
1222 lpht->st.wYear--;
1223 }
1224 lpht->st.wDay = MONTHCAL_MonthLength(lpht->st.wMonth,lpht->st.wYear) -day;
1225 }
1226 else if (day > MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear))
1227 {
1228 retval = MCHT_CALENDARDATENEXT;
1229 lpht->st.wMonth = infoPtr->currentMonth + 1;
1230 if (lpht->st.wMonth <12)
1231 {
1232 lpht->st.wMonth = 1;
1233 lpht->st.wYear++;
1234 }
1235 lpht->st.wDay = day - MONTHCAL_MonthLength(infoPtr->currentMonth,infoPtr->currentYear) ;
1236 }
1237 else {
1238 retval = MCHT_CALENDARDATE;
1239 lpht->st.wMonth = infoPtr->currentMonth;
1240 lpht->st.wDay = day;
1241 }
1242 goto done;
1243 }
1244 if(PtInRect(&infoPtr->todayrect, lpht->pt)) {
1245 retval = MCHT_TODAYLINK;
1246 goto done;
1247 }
1248
1249
1250 /* Hit nothing special? What's left must be background :-) */
1251
1252 retval = MCHT_CALENDARBK;
1253 done:
1254 lpht->uHit = retval;
1255 return retval;
1256 }
1257
1258
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 infoPtr->currentYear++;
1268 infoPtr->currentMonth = 1;
1269 }
1270
1271 if(dwStyle & MCS_DAYSTATE) {
1272 NMDAYSTATE nmds;
1273 int i;
1274
1275 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1276 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1277 nmds.nmhdr.code = MCN_GETDAYSTATE;
1278 nmds.cDayState = infoPtr->monthRange;
1279 nmds.prgDayState = Alloc(infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1280
1281 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1282 (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
1283 for(i=0; i<infoPtr->monthRange; i++)
1284 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1285 }
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 infoPtr->currentYear--;
1298 infoPtr->currentMonth = 12;
1299 }
1300
1301 if(dwStyle & MCS_DAYSTATE) {
1302 NMDAYSTATE nmds;
1303 int i;
1304
1305 nmds.nmhdr.hwndFrom = infoPtr->hwndSelf;
1306 nmds.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1307 nmds.nmhdr.code = MCN_GETDAYSTATE;
1308 nmds.cDayState = infoPtr->monthRange;
1309 nmds.prgDayState = Alloc
1310 (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1311
1312 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1313 (WPARAM)nmds.nmhdr.idFrom, (LPARAM)&nmds);
1314 for(i=0; i<infoPtr->monthRange; i++)
1315 infoPtr->monthdayState[i] = nmds.prgDayState[i];
1316 }
1317 }
1318
1319 static LRESULT
1320 MONTHCAL_RButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1321 {
1322 HMENU hMenu;
1323 POINT menupoint;
1324 char buf[32];
1325
1326 hMenu = CreatePopupMenu();
1327 if (!LoadStringA(COMCTL32_hModule,IDM_GOTODAY,buf,sizeof(buf)))
1328 {
1329 WARN("Can't load resource\n");
1330 strcpy(buf,"Go to Today:");
1331 }
1332 AppendMenuA(hMenu, MF_STRING|MF_ENABLED,1, buf);
1333 menupoint.x=(INT)LOWORD(lParam);
1334 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 infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
1340 infoPtr->currentYear=infoPtr->todaysDate.wYear;
1341 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1342 }
1343 return 0;
1344 }
1345
1346 static LRESULT
1347 MONTHCAL_LButtonDown(MONTHCAL_INFO *infoPtr, LPARAM lParam)
1348 {
1349 MCHITTESTINFO ht;
1350 DWORD hit;
1351 HMENU hMenu;
1352 RECT rcDay; /* used in determining area to invalidate */
1353 char buf[32];
1354 int i;
1355 POINT menupoint;
1356
1357 TRACE("%lx\n", lParam);
1358
1359 if (infoPtr->hWndYearUpDown)
1360 {
1361 infoPtr->currentYear=SendMessageA( infoPtr->hWndYearUpDown, UDM_SETPOS, (WPARAM) 0,(LPARAM)0);
1362 if(!DestroyWindow(infoPtr->hWndYearUpDown))
1363 {
1364 FIXME("Can't destroy Updown Control\n");
1365 }
1366 else
1367 infoPtr->hWndYearUpDown=0;
1368 if(!DestroyWindow(infoPtr->hWndYearEdit))
1369 {
1370 FIXME("Can't destroy Updown Control\n");
1371 }
1372 else
1373 infoPtr->hWndYearEdit=0;
1374 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1375 }
1376
1377 ht.pt.x = (INT)LOWORD(lParam);
1378 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 SetTimer(infoPtr->hwndSelf, MC_NEXTMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1388 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1389 return TRUE;
1390 }
1391 if(hit == MCHT_TITLEBTNPREV){
1392 MONTHCAL_GoToPrevMonth(infoPtr);
1393 infoPtr->status = MC_PREVPRESSED;
1394 SetTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER, MC_NEXTMONTHDELAY, 0);
1395 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1396 return TRUE;
1397 }
1398
1399 if(hit == MCHT_TITLEMONTH) {
1400 hMenu = CreatePopupMenu();
1401
1402 for (i=0; i<12;i++)
1403 {
1404 GetLocaleInfoA( LOCALE_USER_DEFAULT,LOCALE_SMONTHNAME1+i,
1405 buf,sizeof(buf));
1406 AppendMenuA(hMenu, MF_STRING|MF_ENABLED,i+1, buf);
1407 }
1408 menupoint.x=infoPtr->titlemonth.right;
1409 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 if ((i>0) && (i<13))
1414 {
1415 infoPtr->currentMonth=i;
1416 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1417 }
1418 }
1419 if(hit == MCHT_TITLEYEAR) {
1420 infoPtr->hWndYearEdit=CreateWindowExA(0,
1421 "EDIT",
1422 0,
1423 WS_VISIBLE | WS_CHILD |UDS_SETBUDDYINT,
1424 infoPtr->titleyear.left+3,infoPtr->titlebtnnext.top,
1425 infoPtr->titleyear.right-infoPtr->titleyear.left,
1426 infoPtr->textHeight,
1427 infoPtr->hwndSelf,
1428 NULL,
1429 NULL,
1430 NULL);
1431 infoPtr->hWndYearUpDown=CreateWindowExA(0,
1432 UPDOWN_CLASSA,
1433 0,
1434 WS_VISIBLE | WS_CHILD |UDS_SETBUDDYINT|UDS_NOTHOUSANDS|UDS_ARROWKEYS,
1435 infoPtr->titleyear.right+6,infoPtr->titlebtnnext.top,
1436 20,
1437 infoPtr->textHeight,
1438 infoPtr->hwndSelf,
1439 NULL,
1440 NULL,
1441 NULL);
1442 SendMessageA( infoPtr->hWndYearUpDown, UDM_SETRANGE, (WPARAM) 0, MAKELONG (9999, 1753));
1443 SendMessageA( infoPtr->hWndYearUpDown, UDM_SETBUDDY, (WPARAM) infoPtr->hWndYearEdit, (LPARAM)0 );
1444 SendMessageA( infoPtr->hWndYearUpDown, UDM_SETPOS, (WPARAM) 0,(LPARAM)infoPtr->currentYear );
1445 return TRUE;
1446
1447 }
1448 if(hit == MCHT_TODAYLINK) {
1449 infoPtr->currentMonth=infoPtr->todaysDate.wMonth;
1450 infoPtr->currentYear=infoPtr->todaysDate.wYear;
1451 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1452 return TRUE;
1453 }
1454 if(hit == MCHT_CALENDARDATE) {
1455 SYSTEMTIME selArray[2];
1456 NMSELCHANGE nmsc;
1457
1458 MONTHCAL_CopyTime(&ht.st, &selArray[0]);
1459 MONTHCAL_CopyTime(&ht.st, &selArray[1]);
1460 MONTHCAL_SetSelRange(infoPtr, (LPARAM)&selArray);
1461 MONTHCAL_SetCurSel(infoPtr, (LPARAM)&selArray);
1462 TRACE("MCHT_CALENDARDATE\n");
1463 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1464 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1465 nmsc.nmhdr.code = MCN_SELCHANGE;
1466 MONTHCAL_CopyTime(&infoPtr->minSel,&nmsc.stSelStart);
1467 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 infoPtr->firstSelDay = ht.st.wDay;
1483 infoPtr->curSelDay = ht.st.wDay;
1484 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 NMSELCHANGE nmsc;
1496 NMHDR nmhdr;
1497 BOOL redraw = FALSE;
1498 MCHITTESTINFO ht;
1499 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 redraw = TRUE;
1507 }
1508 if(infoPtr->status & MC_PREVPRESSED) {
1509 KillTimer(infoPtr->hwndSelf, MC_PREVMONTHTIMER);
1510 infoPtr->status &= ~MC_PREVPRESSED;
1511 redraw = TRUE;
1512 }
1513
1514 ht.pt.x = (INT)LOWORD(lParam);
1515 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 MONTHCAL_GoToNextMonth(infoPtr);
1522 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1523 return TRUE;
1524 }
1525 if(hit == MCHT_CALENDARDATEPREV){
1526 MONTHCAL_GoToPrevMonth(infoPtr);
1527 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1528 return TRUE;
1529 }
1530 nmhdr.hwndFrom = infoPtr->hwndSelf;
1531 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 (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
1537 /* redraw if necessary */
1538 if(redraw)
1539 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1540 /* only send MCN_SELECT if currently displayed month's day was selected */
1541 if(hit == MCHT_CALENDARDATE) {
1542 nmsc.nmhdr.hwndFrom = infoPtr->hwndSelf;
1543 nmsc.nmhdr.idFrom = GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_ID);
1544 nmsc.nmhdr.code = MCN_SELECT;
1545 MONTHCAL_CopyTime(&infoPtr->minSel, &nmsc.stSelStart);
1546 MONTHCAL_CopyTime(&infoPtr->maxSel, &nmsc.stSelEnd);
1547
1548 SendMessageA(infoPtr->hwndNotify, WM_NOTIFY,
1549 (WPARAM)nmsc.nmhdr.idFrom, (LPARAM)&nmsc);
1550
1551 }
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 break;
1568 case MC_PREVMONTHTIMER:
1569 redraw = TRUE;
1570 MONTHCAL_GoToPrevMonth(infoPtr);
1571 break;
1572 default:
1573 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 return 0;
1582 }
1583
1584
1585 static LRESULT
1586 MONTHCAL_MouseMove(MONTHCAL_INFO *infoPtr, WPARAM wParam, LPARAM lParam)
1587 {
1588 MCHITTESTINFO ht;
1589 int oldselday, selday, hit;
1590 RECT r;
1591
1592 if(!(infoPtr->status & MC_SEL_LBUTDOWN)) return 0;
1593
1594 ht.pt.x = LOWORD(lParam);
1595 ht.pt.y = HIWORD(lParam);
1596
1597 hit = MONTHCAL_HitTest(infoPtr, (LPARAM)&ht);
1598
1599 /* not on the calendar date numbers? bail out */
1600 TRACE("hit:%x\n",hit);
1601 if((hit & MCHT_CALENDARDATE) != MCHT_CALENDARDATE) return 0;
1602
1603 selday = ht.st.wDay;
1604 oldselday = infoPtr->curSelDay;
1605 infoPtr->curSelDay = selday;
1606 MONTHCAL_CalcPosFromDay(infoPtr, selday, ht.st. wMonth, &r);
1607
1608 if(GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE) & MCS_MULTISELECT) {
1609 SYSTEMTIME selArray[2];
1610 int i;
1611
1612 MONTHCAL_GetSelRange(infoPtr, (LPARAM)&selArray);
1613 i = 0;
1614 if(infoPtr->firstSelDay==selArray[0].wDay) i=1;
1615 TRACE("oldRange:%d %d %d %d\n", infoPtr->firstSelDay, selArray[0].wDay, selArray[1].wDay, i);
1616 if(infoPtr->firstSelDay==selArray[1].wDay) {
1617 /* 1st time we get here: selArray[0]=selArray[1]) */
1618 /* if we're still at the first selected date, return */
1619 if(infoPtr->firstSelDay==selday) goto done;
1620 if(selday<infoPtr->firstSelDay) i = 0;
1621 }
1622
1623 if(abs(infoPtr->firstSelDay - selday) >= infoPtr->maxSelCount) {
1624 if(selday>infoPtr->firstSelDay)
1625 selday = infoPtr->firstSelDay + infoPtr->maxSelCount;
1626 else
1627 selday = infoPtr->firstSelDay - infoPtr->maxSelCount;
1628 }
1629
1630 if(selArray[i].wDay!=selday) {
1631 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 DWORD tempday;
1637 tempday = selArray[1].wDay;
1638 selArray[1].wDay = selArray[0].wDay;
1639 selArray[0].wDay = tempday;
1640 }
1641
1642 MONTHCAL_SetSelRange(infoPtr, (LPARAM)&selArray);
1643 }
1644 }
1645
1646 done:
1647
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 HDC hdc;
1662 PAINTSTRUCT ps;
1663
1664 if (wParam)
1665 {
1666 GetClientRect(infoPtr->hwndSelf, &ps.rcPaint);
1667 hdc = (HDC)wParam;
1668 }
1669 else
1670 hdc = BeginPaint(infoPtr->hwndSelf, &ps);
1671
1672 MONTHCAL_Refresh(infoPtr, hdc, &ps);
1673 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 RECT *title=&infoPtr->title;
1704 RECT *prev=&infoPtr->titlebtnprev;
1705 RECT *next=&infoPtr->titlebtnnext;
1706 RECT *titlemonth=&infoPtr->titlemonth;
1707 RECT *titleyear=&infoPtr->titleyear;
1708 RECT *wdays=&infoPtr->wdays;
1709 RECT *weeknumrect=&infoPtr->weeknums;
1710 RECT *days=&infoPtr->days;
1711 RECT *todayrect=&infoPtr->todayrect;
1712 SIZE size;
1713 TEXTMETRICA tm;
1714 DWORD dwStyle = GetWindowLongW(infoPtr->hwndSelf, GWL_STYLE);
1715 HFONT currentFont;
1716 int xdiv, left_offset;
1717 RECT rcClient;
1718
1719 GetClientRect(infoPtr->hwndSelf, &rcClient);
1720
1721 currentFont = SelectObject(hdc, infoPtr->hFont);
1722
1723 /* get the height and width of each day's text */
1724 GetTextMetricsA(hdc, &tm);
1725 infoPtr->textHeight = tm.tmHeight + tm.tmExternalLeading + tm.tmInternalLeading;
1726 GetTextExtentPoint32A(hdc, "Sun", 3, &size);
1727 infoPtr->textWidth = size.cx + 2;
1728
1729 /* recalculate the height and width increments and offsets */
1730 GetTextExtentPoint32A(hdc, "00", 2, &size);
1731
1732 xdiv = (dwStyle & MCS_WEEKNUMBERS) ? 8 : 7;
1733
1734 infoPtr->width_increment = size.cx * 2;
1735 infoPtr->height_increment = infoPtr->textHeight;
1736 left_offset = (rcClient.right - rcClient.left) - (infoPtr->width_increment * xdiv);
1737
1738 /* calculate title area */
1739 title->top = rcClient.top;
1740 title->bottom = title->top + 3 * infoPtr->height_increment / 2;
1741 title->left = left_offset;
1742 title->right = rcClient.right;
1743
1744 /* set the dimensions of the next and previous buttons and center */
1745 /* the month text vertically */
1746 prev->top = next->top = title->top + 4;
1747 prev->bottom = next->bottom = title->bottom - 4;
1748 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 /* titlemonth->left and right change based upon the current month */
1754 /* and are recalculated in refresh as the current month may change */
1755 /* without the control being resized */
1756 titlemonth->top = titleyear->top = title->top + (infoPtr->height_increment)/2;
1757 titlemonth->bottom = titleyear->bottom = title->bottom - (infoPtr->height_increment)/2;
1758
1759 /* setup the dimensions of the rectangle we draw the names of the */
1760 /* days of the week in */
1761 weeknumrect->left = left_offset;
1762 if(dwStyle & MCS_WEEKNUMBERS)
1763 weeknumrect->right=prev->right;
1764 else
1765 weeknumrect->right=weeknumrect->left;
1766 wdays->left = days->left = weeknumrect->right;
1767 wdays->right = days->right = wdays->left + 7 * infoPtr->width_increment;
1768 wdays->top = title->bottom ;
1769 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 todayrect->left = rcClient.left;
1775 todayrect->right = rcClient.right;
1776 todayrect->top = days->bottom;
1777 todayrect->bottom = days->bottom + infoPtr->height_increment;
1778
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 wine_dbgstr_rect(&rcClient),
1782 wine_dbgstr_rect(title),
1783 wine_dbgstr_rect(wdays),
1784 wine_dbgstr_rect(days),
1785 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 return 0;
1803 }
1804
1805 static LRESULT MONTHCAL_GetFont(MONTHCAL_INFO *infoPtr)
1806 {
1807 return (LRESULT)infoPtr->hFont;
1808 }
1809
1810 static LRESULT MONTHCAL_SetFont(MONTHCAL_INFO *infoPtr, HFONT hFont, BOOL redraw)
1811 {
1812 HFONT hOldFont;
1813 LOGFONTW lf;
1814
1815 if (!hFont) return 0;
1816
1817 hOldFont = infoPtr->hFont;
1818 infoPtr->hFont = hFont;
1819
1820 GetObjectW(infoPtr->hFont, sizeof(lf), &lf);
1821 lf.lfWeight = FW_BOLD;
1822 infoPtr->hBoldFont = CreateFontIndirectW(&lf);
1823
1824 if (redraw)
1825 InvalidateRect(infoPtr->hwndSelf, NULL, FALSE);
1826
1827 return (LRESULT)hOldFont;
1828 }
1829
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 if(infoPtr == NULL) {
1841 ERR( "could not allocate info memory!\n");
1842 return 0;
1843 }
1844
1845 infoPtr->hwndSelf = hwnd;
1846 infoPtr->hwndNotify = ((LPCREATESTRUCTW)lParam)->hwndParent;
1847
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 infoPtr->currentMonth = infoPtr->todaysDate.wMonth;
1856 infoPtr->currentYear = infoPtr->todaysDate.wYear;
1857 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->minDate);
1858 MONTHCAL_CopyTime(&infoPtr->todaysDate, &infoPtr->maxDate);
1859 infoPtr->maxDate.wYear=2050;
1860 infoPtr->minDate.wYear=1950;
1861 infoPtr->maxSelCount = 7;
1862 infoPtr->monthRange = 3;
1863 infoPtr->monthdayState = Alloc
1864 (infoPtr->monthRange * sizeof(MONTHDAYSTATE));
1865 infoPtr->titlebk = GetSysColor(COLOR_ACTIVECAPTION);
1866 infoPtr->titletxt = GetSysColor(COLOR_WINDOW);
1867 infoPtr->monthbk = GetSysColor(COLOR_WINDOW);
1868 infoPtr->trailingtxt = GetSysColor(COLOR_GRAYTEXT);
1869 infoPtr->bk = GetSysColor(COLOR_WINDOW);
1870 infoPtr->txt = GetSysColor(COLOR_WINDOWTEXT);
1871
1872 /* call MONTHCAL_UpdateSize to set all of the dimensions */
1873 /* 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 MONTHCAL_INFO *infoPtr;
1896
1897 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hwnd, uMsg, wParam, lParam);
1898
1899 infoPtr = MONTHCAL_GetInfoPtr(hwnd);
1900 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 case WM_GETDLGCODE:
1968 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 case WM_SETFONT:
1998 return MONTHCAL_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1999
2000 case WM_GETFONT:
2001 return MONTHCAL_GetFont(infoPtr);
2002
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 ERR( "unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
2012 return DefWindowProcA(hwnd, uMsg, wParam, lParam);
2013 }
2014 }
2015
2016
2017 void
2018 MONTHCAL_Register(void)
2019 {
2020 WNDCLASSA wndClass;
2021
2022 ZeroMemory(&wndClass, sizeof(WNDCLASSA));
2023 wndClass.style = CS_GLOBALCLASS;
2024 wndClass.lpfnWndProc = MONTHCAL_WindowProc;
2025 wndClass.cbClsExtra = 0;
2026 wndClass.cbWndExtra = sizeof(MONTHCAL_INFO *);
2027 wndClass.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
2028 wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
2029 wndClass.lpszClassName = MONTHCAL_CLASSA;
2030
2031 RegisterClassA(&wndClass);
2032 }
2033
2034
2035 void
2036 MONTHCAL_Unregister(void)
2037 {
2038 UnregisterClassA(MONTHCAL_CLASSA, NULL);
2039 }