]>
Commit | Line | Data |
---|---|---|
51317496 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/msw/calctrl.cpp | |
3 | // Purpose: wxCalendarCtrl implementation | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2008-04-04 | |
aa7ee888 | 6 | // RCS-ID: $Id$ |
51317496 VZ |
7 | // Copyright: (C) 2008 Vadim Zeitlin <vadim@wxwidgets.org> |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | #include "wx/wxprec.h" | |
20 | ||
21 | #ifdef __BORLANDC__ | |
22 | #pragma hdrstop | |
23 | #endif | |
24 | ||
25 | #if wxUSE_CALENDARCTRL | |
26 | ||
27 | #ifndef WX_PRECOMP | |
28 | #include "wx/msw/wrapwin.h" | |
29 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" | |
aa7ee888 | 30 | #include "wx/msw/private.h" |
51317496 VZ |
31 | #endif |
32 | ||
33 | #include "wx/calctrl.h" | |
34 | ||
35 | #include "wx/msw/private/datecontrols.h" | |
36 | ||
18547275 VZ |
37 | // ---------------------------------------------------------------------------- |
38 | // constants | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | namespace | |
42 | { | |
43 | ||
44 | // values of week days used by the native control | |
45 | enum | |
46 | { | |
47 | MonthCal_Monday, | |
48 | MonthCal_Tuesday, | |
49 | MonthCal_Wednesday, | |
50 | MonthCal_Thursday, | |
51 | MonthCal_Friday, | |
52 | MonthCal_Saturday, | |
53 | MonthCal_Sunday | |
54 | }; | |
55 | ||
56 | } // anonymous namespace | |
57 | ||
51317496 VZ |
58 | // ============================================================================ |
59 | // implementation | |
60 | // ============================================================================ | |
61 | ||
62 | // ---------------------------------------------------------------------------- | |
63 | // wxCalendarCtrl creation | |
64 | // ---------------------------------------------------------------------------- | |
65 | ||
6d9b6716 VZ |
66 | void wxCalendarCtrl::Init() |
67 | { | |
68 | m_marks = | |
69 | m_holidays = 0; | |
70 | } | |
71 | ||
51317496 VZ |
72 | bool |
73 | wxCalendarCtrl::Create(wxWindow *parent, | |
74 | wxWindowID id, | |
75 | const wxDateTime& dt, | |
76 | const wxPoint& pos, | |
77 | const wxSize& size, | |
78 | long style, | |
79 | const wxString& name) | |
80 | { | |
81 | if ( !wxMSWDateControls::CheckInitialization() ) | |
82 | return false; | |
83 | ||
d6f04127 VZ |
84 | // we need the arrows for the navigation |
85 | style |= wxWANTS_CHARS; | |
86 | ||
51317496 VZ |
87 | // initialize the base class |
88 | if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) ) | |
89 | return false; | |
90 | ||
b3ed7020 VZ |
91 | // create the native control: this is a bit tricky as we want to receive |
92 | // double click events but the MONTHCAL_CLASS doesn't use CS_DBLCLKS style | |
93 | // and so we create our own copy of it which does | |
94 | static ClassRegistrar s_clsMonthCal; | |
95 | if ( !s_clsMonthCal.IsInitialized() ) | |
96 | { | |
97 | // get a copy of standard class and modify it | |
98 | WNDCLASS wc; | |
99 | if ( ::GetClassInfo(NULL, MONTHCAL_CLASS, &wc) ) | |
100 | { | |
101 | wc.lpszClassName = wxT("_wx_SysMonthCtl32"); | |
102 | wc.style |= CS_DBLCLKS; | |
103 | s_clsMonthCal.Register(wc); | |
104 | } | |
105 | else | |
106 | { | |
9a83f860 | 107 | wxLogLastError(wxT("GetClassInfoEx(SysMonthCal32)")); |
b3ed7020 VZ |
108 | } |
109 | } | |
110 | ||
111 | const wxChar * const clsname = s_clsMonthCal.IsRegistered() | |
112 | ? s_clsMonthCal.GetName().wx_str() | |
113 | : MONTHCAL_CLASS; | |
114 | ||
115 | if ( !MSWCreateControl(clsname, wxEmptyString, pos, size) ) | |
51317496 VZ |
116 | return false; |
117 | ||
03647350 | 118 | // initialize the control |
db0b0942 VZ |
119 | UpdateFirstDayOfWeek(); |
120 | ||
51317496 VZ |
121 | SetDate(dt.IsValid() ? dt : wxDateTime::Today()); |
122 | ||
169b83dd VZ |
123 | SetHolidayAttrs(); |
124 | UpdateMarks(); | |
82c6027b | 125 | |
3ccd1b49 VZ |
126 | Connect(wxEVT_LEFT_DOWN, |
127 | wxMouseEventHandler(wxCalendarCtrl::MSWOnClick)); | |
82c6027b | 128 | Connect(wxEVT_LEFT_DCLICK, |
b3ed7020 VZ |
129 | wxMouseEventHandler(wxCalendarCtrl::MSWOnDoubleClick)); |
130 | ||
51317496 VZ |
131 | return true; |
132 | } | |
133 | ||
134 | WXDWORD wxCalendarCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const | |
135 | { | |
136 | WXDWORD styleMSW = wxCalendarCtrlBase::MSWGetStyle(style, exstyle); | |
137 | ||
bf9b73bb VZ |
138 | // right now we don't support all native styles but we should add wx styles |
139 | // corresponding to MCS_NOTODAY and MCS_NOTODAYCIRCLE probably (TODO) | |
51317496 VZ |
140 | |
141 | // for compatibility with the other versions, just turn off today display | |
142 | // unconditionally for now | |
143 | styleMSW |= MCS_NOTODAY; | |
144 | ||
82c6027b VZ |
145 | // we also need this style for Mark() to work |
146 | styleMSW |= MCS_DAYSTATE; | |
147 | ||
bf9b73bb VZ |
148 | if ( style & wxCAL_SHOW_WEEK_NUMBERS ) |
149 | styleMSW |= MCS_WEEKNUMBERS; | |
150 | ||
51317496 VZ |
151 | return styleMSW; |
152 | } | |
153 | ||
db0b0942 VZ |
154 | void wxCalendarCtrl::SetWindowStyleFlag(long style) |
155 | { | |
156 | const bool hadMondayFirst = HasFlag(wxCAL_MONDAY_FIRST); | |
157 | ||
158 | wxCalendarCtrlBase::SetWindowStyleFlag(style); | |
159 | ||
160 | if ( HasFlag(wxCAL_MONDAY_FIRST) != hadMondayFirst ) | |
161 | UpdateFirstDayOfWeek(); | |
162 | } | |
51317496 VZ |
163 | |
164 | // ---------------------------------------------------------------------------- | |
165 | // wxCalendarCtrl geometry | |
166 | // ---------------------------------------------------------------------------- | |
167 | ||
db0b0942 | 168 | // TODO: handle WM_WININICHANGE |
51317496 VZ |
169 | wxSize wxCalendarCtrl::DoGetBestSize() const |
170 | { | |
171 | RECT rc; | |
172 | if ( !GetHwnd() || !MonthCal_GetMinReqRect(GetHwnd(), &rc) ) | |
173 | { | |
174 | return wxCalendarCtrlBase::DoGetBestSize(); | |
175 | } | |
176 | ||
177 | const wxSize best = wxRectFromRECT(rc).GetSize() + GetWindowBorderSize(); | |
178 | CacheBestSize(best); | |
179 | return best; | |
180 | } | |
181 | ||
ee22a3a2 VZ |
182 | wxCalendarHitTestResult |
183 | wxCalendarCtrl::HitTest(const wxPoint& pos, | |
184 | wxDateTime *date, | |
185 | wxDateTime::WeekDay *wd) | |
186 | { | |
187 | WinStruct<MCHITTESTINFO> hti; | |
f9d2e19d VZ |
188 | |
189 | // Vista and later SDKs add a few extra fields to MCHITTESTINFO which are | |
190 | // not supported by the previous versions, as we don't use them anyhow we | |
191 | // should pretend that we always use the old struct format to make the call | |
192 | // below work on pre-Vista systems (see #11057) | |
193 | #ifdef MCHITTESTINFO_V1_SIZE | |
194 | hti.cbSize = MCHITTESTINFO_V1_SIZE; | |
195 | #endif | |
196 | ||
ee22a3a2 VZ |
197 | hti.pt.x = pos.x; |
198 | hti.pt.y = pos.y; | |
199 | switch ( MonthCal_HitTest(GetHwnd(), &hti) ) | |
200 | { | |
201 | default: | |
202 | case MCHT_CALENDARWEEKNUM: | |
203 | wxFAIL_MSG( "unexpected" ); | |
204 | // fall through | |
205 | ||
206 | case MCHT_NOWHERE: | |
207 | case MCHT_CALENDARBK: | |
208 | case MCHT_TITLEBK: | |
209 | case MCHT_TITLEMONTH: | |
210 | case MCHT_TITLEYEAR: | |
211 | return wxCAL_HITTEST_NOWHERE; | |
212 | ||
213 | case MCHT_CALENDARDATE: | |
214 | if ( date ) | |
10acc3ef | 215 | date->SetFromMSWSysDate(hti.st); |
ee22a3a2 VZ |
216 | return wxCAL_HITTEST_DAY; |
217 | ||
218 | case MCHT_CALENDARDAY: | |
219 | if ( wd ) | |
220 | { | |
1b88c4e4 VZ |
221 | int day = hti.st.wDayOfWeek; |
222 | ||
223 | // the native control returns incorrect day of the week when | |
224 | // the first day isn't Monday, i.e. the first column is always | |
225 | // "Monday" even if its label is "Sunday", compensate for it | |
226 | const int first = LOWORD(MonthCal_GetFirstDayOfWeek(GetHwnd())); | |
227 | if ( first == MonthCal_Monday ) | |
228 | { | |
229 | // as MonthCal_Monday is 0 while wxDateTime::Mon is 1, | |
230 | // normally we need to do this to transform from MSW | |
231 | // convention to wx one | |
232 | day++; | |
233 | day %= 7; | |
234 | } | |
235 | //else: but when the first day is MonthCal_Sunday, the native | |
236 | // control still returns 0 (i.e. MonthCal_Monday) for the | |
237 | // first column which looks like a bug in it but to work | |
238 | // around it it's enough to not apply the correction above | |
239 | ||
240 | *wd = static_cast<wxDateTime::WeekDay>(day); | |
ee22a3a2 VZ |
241 | } |
242 | return wxCAL_HITTEST_HEADER; | |
243 | ||
244 | case MCHT_TITLEBTNNEXT: | |
245 | return wxCAL_HITTEST_INCMONTH; | |
246 | ||
247 | case MCHT_TITLEBTNPREV: | |
248 | return wxCAL_HITTEST_DECMONTH; | |
249 | ||
250 | case MCHT_CALENDARDATENEXT: | |
251 | case MCHT_CALENDARDATEPREV: | |
252 | return wxCAL_HITTEST_SURROUNDING_WEEK; | |
253 | } | |
254 | } | |
255 | ||
51317496 VZ |
256 | // ---------------------------------------------------------------------------- |
257 | // wxCalendarCtrl operations | |
258 | // ---------------------------------------------------------------------------- | |
259 | ||
260 | bool wxCalendarCtrl::SetDate(const wxDateTime& dt) | |
261 | { | |
262 | wxCHECK_MSG( dt.IsValid(), false, "invalid date" ); | |
6331afb0 | 263 | |
51317496 | 264 | SYSTEMTIME st; |
10acc3ef | 265 | dt.GetAsMSWSysDate(&st); |
51317496 VZ |
266 | if ( !MonthCal_SetCurSel(GetHwnd(), &st) ) |
267 | { | |
9a83f860 | 268 | wxLogDebug(wxT("DateTime_SetSystemtime() failed")); |
51317496 VZ |
269 | |
270 | return false; | |
271 | } | |
272 | ||
10acc3ef | 273 | m_date = dt.GetDateOnly(); |
a4fcd589 | 274 | |
51317496 VZ |
275 | return true; |
276 | } | |
277 | ||
278 | wxDateTime wxCalendarCtrl::GetDate() const | |
279 | { | |
4b6a582b | 280 | #if wxDEBUG_LEVEL |
51317496 | 281 | SYSTEMTIME st; |
10acc3ef | 282 | |
51317496 | 283 | if ( !MonthCal_GetCurSel(GetHwnd(), &st) ) |
a4fcd589 VZ |
284 | { |
285 | wxASSERT_MSG( !m_date.IsValid(), "mismatch between data and control" ); | |
286 | ||
51317496 | 287 | return wxDefaultDateTime; |
a4fcd589 | 288 | } |
51317496 | 289 | |
10acc3ef VZ |
290 | wxDateTime dt; |
291 | dt.SetFromMSWSysDate(st); | |
a4fcd589 | 292 | |
6331afb0 VZ |
293 | // Windows XP and earlier didn't include the time component into the |
294 | // returned date but Windows 7 does, so we can't compare the full objects | |
295 | // in the same way under all the Windows versions, just compare their date | |
296 | // parts | |
10acc3ef | 297 | wxASSERT_MSG( dt.IsSameDate(m_date), "mismatch between data and control" ); |
4b6a582b | 298 | #endif // wxDEBUG_LEVEL |
a4fcd589 VZ |
299 | |
300 | return m_date; | |
51317496 VZ |
301 | } |
302 | ||
303 | bool wxCalendarCtrl::SetDateRange(const wxDateTime& dt1, const wxDateTime& dt2) | |
304 | { | |
305 | SYSTEMTIME st[2]; | |
306 | ||
307 | DWORD flags = 0; | |
308 | if ( dt1.IsValid() ) | |
309 | { | |
154014d6 | 310 | dt1.GetAsMSWSysTime(st + 0); |
51317496 VZ |
311 | flags |= GDTR_MIN; |
312 | } | |
313 | ||
314 | if ( dt2.IsValid() ) | |
315 | { | |
154014d6 | 316 | dt2.GetAsMSWSysTime(st + 1); |
51317496 VZ |
317 | flags |= GDTR_MAX; |
318 | } | |
319 | ||
320 | if ( !MonthCal_SetRange(GetHwnd(), flags, st) ) | |
321 | { | |
9a83f860 | 322 | wxLogDebug(wxT("MonthCal_SetRange() failed")); |
51317496 VZ |
323 | } |
324 | ||
325 | return flags != 0; | |
326 | } | |
327 | ||
328 | bool wxCalendarCtrl::GetDateRange(wxDateTime *dt1, wxDateTime *dt2) const | |
329 | { | |
330 | SYSTEMTIME st[2]; | |
331 | ||
332 | DWORD flags = MonthCal_GetRange(GetHwnd(), st); | |
333 | if ( dt1 ) | |
334 | { | |
335 | if ( flags & GDTR_MIN ) | |
10acc3ef | 336 | dt1->SetFromMSWSysDate(st[0]); |
51317496 VZ |
337 | else |
338 | *dt1 = wxDefaultDateTime; | |
339 | } | |
340 | ||
341 | if ( dt2 ) | |
342 | { | |
343 | if ( flags & GDTR_MAX ) | |
10acc3ef | 344 | dt2->SetFromMSWSysDate(st[1]); |
51317496 VZ |
345 | else |
346 | *dt2 = wxDefaultDateTime; | |
347 | } | |
348 | ||
349 | return flags != 0; | |
350 | } | |
351 | ||
352 | // ---------------------------------------------------------------------------- | |
353 | // other wxCalendarCtrl operations | |
354 | // ---------------------------------------------------------------------------- | |
355 | ||
356 | bool wxCalendarCtrl::EnableMonthChange(bool enable) | |
357 | { | |
7ec5c42e VZ |
358 | if ( !wxCalendarCtrlBase::EnableMonthChange(enable) ) |
359 | return false; | |
360 | ||
361 | wxDateTime dtStart, dtEnd; | |
362 | if ( !enable ) | |
363 | { | |
364 | dtStart = GetDate(); | |
365 | dtStart.SetDay(1); | |
51317496 | 366 | |
7ec5c42e VZ |
367 | dtEnd = dtStart.GetLastMonthDay(); |
368 | } | |
369 | //else: leave them invalid to remove the restriction | |
370 | ||
371 | SetDateRange(dtStart, dtEnd); | |
372 | ||
373 | return true; | |
51317496 VZ |
374 | } |
375 | ||
376 | void wxCalendarCtrl::Mark(size_t day, bool mark) | |
377 | { | |
82c6027b VZ |
378 | wxCHECK_RET( day > 0 && day < 32, "invalid day" ); |
379 | ||
380 | int mask = 1 << (day - 1); | |
381 | if ( mark ) | |
382 | m_marks |= mask; | |
383 | else | |
384 | m_marks &= ~mask; | |
385 | ||
386 | // calling Refresh() here is not enough to change the day appearance | |
387 | UpdateMarks(); | |
388 | } | |
389 | ||
6d9b6716 VZ |
390 | void wxCalendarCtrl::SetHoliday(size_t day) |
391 | { | |
392 | wxCHECK_RET( day > 0 && day < 32, "invalid day" ); | |
393 | ||
394 | m_holidays |= 1 << (day - 1); | |
395 | } | |
396 | ||
82c6027b VZ |
397 | void wxCalendarCtrl::UpdateMarks() |
398 | { | |
c3d3028a VZ |
399 | // Currently the native control may show more than one month if its size is |
400 | // big enough. Ideal would be to prevent this from happening but there | |
401 | // doesn't seem to be any obvious way to do it, so for now just handle the | |
402 | // possibility that we can display several of them: one before the current | |
403 | // one and up to 12 after it. | |
404 | MONTHDAYSTATE states[14] = { 0 }; | |
9573840b | 405 | const DWORD nMonths = MonthCal_GetMonthRange(GetHwnd(), GMR_DAYSTATE, NULL); |
82c6027b | 406 | |
52980340 VZ |
407 | // although in principle the calendar might not show any days from the |
408 | // preceding months, it seems like it always does, consider e.g. Feb 2010 | |
409 | // which starts on Monday and ends on Sunday and so could fit on 4 lines | |
410 | // without showing any subsequent months -- the standard control still | |
411 | // shows it on 6 lines and the number of visible months is still 3 | |
9573840b VZ |
412 | // |
413 | // OTOH Windows 7 control can show all 12 months or even years or decades | |
414 | // in its window if you "zoom out" of it by double clicking on free areas | |
415 | // so the return value can be (much, in case of decades view) greater than | |
416 | // 3 but in this case marks are not visible anyhow so simply ignore it | |
c3d3028a | 417 | if ( nMonths >= 2 && nMonths <= WXSIZEOF(states) ) |
9573840b | 418 | { |
c3d3028a | 419 | // The current, fully visible month is always the second one. |
9573840b VZ |
420 | states[1] = m_marks | m_holidays; |
421 | ||
422 | if ( !MonthCal_SetDayState(GetHwnd(), nMonths, states) ) | |
423 | { | |
424 | wxLogLastError(wxT("MonthCal_SetDayState")); | |
425 | } | |
82c6027b | 426 | } |
9573840b | 427 | //else: not a month view at all |
51317496 VZ |
428 | } |
429 | ||
db0b0942 VZ |
430 | void wxCalendarCtrl::UpdateFirstDayOfWeek() |
431 | { | |
18547275 VZ |
432 | MonthCal_SetFirstDayOfWeek(GetHwnd(), |
433 | HasFlag(wxCAL_MONDAY_FIRST) ? MonthCal_Monday | |
434 | : MonthCal_Sunday); | |
db0b0942 VZ |
435 | } |
436 | ||
51317496 VZ |
437 | // ---------------------------------------------------------------------------- |
438 | // wxCalendarCtrl events | |
439 | // ---------------------------------------------------------------------------- | |
440 | ||
441 | bool wxCalendarCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) | |
442 | { | |
443 | NMHDR* hdr = (NMHDR *)lParam; | |
444 | switch ( hdr->code ) | |
445 | { | |
a4fcd589 | 446 | case MCN_SELCHANGE: |
a4fcd589 | 447 | { |
82c6027b VZ |
448 | // we need to update m_date first, before calling the user code |
449 | // which expects GetDate() to return the new date | |
450 | const wxDateTime dateOld = m_date; | |
451 | const NMSELCHANGE * const sch = (NMSELCHANGE *)lParam; | |
10acc3ef | 452 | m_date.SetFromMSWSysDate(sch->stSelStart); |
82c6027b VZ |
453 | |
454 | // changing the year or the month results in a second dummy | |
455 | // MCN_SELCHANGE event on this system which doesn't really | |
456 | // change anything -- filter it out | |
457 | if ( m_date != dateOld ) | |
458 | { | |
6d9b6716 VZ |
459 | if ( GenerateAllChangeEvents(dateOld) ) |
460 | { | |
461 | // month changed, need to update the holidays if we use | |
462 | // them | |
169b83dd VZ |
463 | SetHolidayAttrs(); |
464 | UpdateMarks(); | |
6d9b6716 | 465 | } |
82c6027b VZ |
466 | } |
467 | } | |
468 | break; | |
a4fcd589 | 469 | |
82c6027b VZ |
470 | case MCN_GETDAYSTATE: |
471 | { | |
472 | const NMDAYSTATE * const ds = (NMDAYSTATE *)lParam; | |
c3d3028a VZ |
473 | |
474 | wxDateTime startDate; | |
475 | startDate.SetFromMSWSysDate(ds->stStart); | |
476 | ||
477 | wxDateTime currentDate = m_date; | |
478 | // Set to the start of month for comparison with startDate to | |
479 | // work correctly. | |
480 | currentDate.SetDay(1); | |
481 | ||
82c6027b VZ |
482 | for ( int i = 0; i < ds->cDayState; i++ ) |
483 | { | |
c3d3028a VZ |
484 | // set holiday/marks only for the "current" month |
485 | if ( startDate == currentDate ) | |
486 | ds->prgDayState[i] = m_marks | m_holidays; | |
487 | else | |
488 | ds->prgDayState[i] = 0; | |
489 | ||
490 | startDate += wxDateSpan::Month(); | |
82c6027b | 491 | } |
a4fcd589 | 492 | } |
82c6027b VZ |
493 | break; |
494 | ||
495 | default: | |
496 | return wxCalendarCtrlBase::MSWOnNotify(idCtrl, lParam, result); | |
51317496 VZ |
497 | } |
498 | ||
82c6027b VZ |
499 | *result = 0; |
500 | return true; | |
51317496 VZ |
501 | } |
502 | ||
b3ed7020 VZ |
503 | void wxCalendarCtrl::MSWOnDoubleClick(wxMouseEvent& event) |
504 | { | |
505 | if ( HitTest(event.GetPosition()) == wxCAL_HITTEST_DAY ) | |
506 | { | |
507 | if ( GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED) ) | |
508 | return; // skip event.Skip() below | |
509 | } | |
510 | ||
511 | event.Skip(); | |
512 | } | |
513 | ||
3ccd1b49 VZ |
514 | void wxCalendarCtrl::MSWOnClick(wxMouseEvent& event) |
515 | { | |
516 | // for some reason, the control doesn't get focus on its own when the user | |
517 | // clicks in it | |
518 | SetFocus(); | |
519 | ||
520 | event.Skip(); | |
521 | } | |
522 | ||
51317496 | 523 | #endif // wxUSE_CALENDARCTRL |