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