1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/calctrl.cpp
3 // Purpose: wxCalendarCtrl implementation
4 // Author: Vadim Zeitlin
6 // Copyright: (C) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 #include "wx/wxprec.h"
24 #if wxUSE_CALENDARCTRL
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
29 #include "wx/msw/private.h"
32 #include "wx/calctrl.h"
34 #include "wx/msw/private/datecontrols.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
43 // values of week days used by the native control
55 } // anonymous namespace
57 // ============================================================================
59 // ============================================================================
61 // ----------------------------------------------------------------------------
62 // wxCalendarCtrl creation
63 // ----------------------------------------------------------------------------
65 void wxCalendarCtrl::Init()
72 wxCalendarCtrl::Create(wxWindow
*parent
,
80 if ( !wxMSWDateControls::CheckInitialization() )
83 // we need the arrows for the navigation
84 style
|= wxWANTS_CHARS
;
86 // initialize the base class
87 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
90 // create the native control: this is a bit tricky as we want to receive
91 // double click events but the MONTHCAL_CLASS doesn't use CS_DBLCLKS style
92 // and so we create our own copy of it which does
93 static ClassRegistrar s_clsMonthCal
;
94 if ( !s_clsMonthCal
.IsInitialized() )
96 // get a copy of standard class and modify it
98 if ( ::GetClassInfo(NULL
, MONTHCAL_CLASS
, &wc
) )
100 wc
.lpszClassName
= wxT("_wx_SysMonthCtl32");
101 wc
.style
|= CS_DBLCLKS
;
102 s_clsMonthCal
.Register(wc
);
106 wxLogLastError(wxT("GetClassInfoEx(SysMonthCal32)"));
110 const wxChar
* const clsname
= s_clsMonthCal
.IsRegistered()
111 ? s_clsMonthCal
.GetName().t_str()
114 if ( !MSWCreateControl(clsname
, wxEmptyString
, pos
, size
) )
117 // initialize the control
118 UpdateFirstDayOfWeek();
120 SetDate(dt
.IsValid() ? dt
: wxDateTime::Today());
125 Connect(wxEVT_LEFT_DOWN
,
126 wxMouseEventHandler(wxCalendarCtrl::MSWOnClick
));
127 Connect(wxEVT_LEFT_DCLICK
,
128 wxMouseEventHandler(wxCalendarCtrl::MSWOnDoubleClick
));
133 WXDWORD
wxCalendarCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
135 WXDWORD styleMSW
= wxCalendarCtrlBase::MSWGetStyle(style
, exstyle
);
137 // right now we don't support all native styles but we should add wx styles
138 // corresponding to MCS_NOTODAY and MCS_NOTODAYCIRCLE probably (TODO)
140 // for compatibility with the other versions, just turn off today display
141 // unconditionally for now
142 styleMSW
|= MCS_NOTODAY
;
144 // we also need this style for Mark() to work
145 styleMSW
|= MCS_DAYSTATE
;
147 if ( style
& wxCAL_SHOW_WEEK_NUMBERS
)
148 styleMSW
|= MCS_WEEKNUMBERS
;
153 void wxCalendarCtrl::SetWindowStyleFlag(long style
)
155 const bool hadMondayFirst
= HasFlag(wxCAL_MONDAY_FIRST
);
157 wxCalendarCtrlBase::SetWindowStyleFlag(style
);
159 if ( HasFlag(wxCAL_MONDAY_FIRST
) != hadMondayFirst
)
160 UpdateFirstDayOfWeek();
163 // ----------------------------------------------------------------------------
164 // wxCalendarCtrl geometry
165 // ----------------------------------------------------------------------------
167 // TODO: handle WM_WININICHANGE
168 wxSize
wxCalendarCtrl::DoGetBestSize() const
171 if ( !GetHwnd() || !MonthCal_GetMinReqRect(GetHwnd(), &rc
) )
173 return wxCalendarCtrlBase::DoGetBestSize();
176 const wxSize best
= wxRectFromRECT(rc
).GetSize() + GetWindowBorderSize();
181 wxCalendarHitTestResult
182 wxCalendarCtrl::HitTest(const wxPoint
& pos
,
184 wxDateTime::WeekDay
*wd
)
186 WinStruct
<MCHITTESTINFO
> hti
;
188 // Vista and later SDKs add a few extra fields to MCHITTESTINFO which are
189 // not supported by the previous versions, as we don't use them anyhow we
190 // should pretend that we always use the old struct format to make the call
191 // below work on pre-Vista systems (see #11057)
192 #ifdef MCHITTESTINFO_V1_SIZE
193 hti
.cbSize
= MCHITTESTINFO_V1_SIZE
;
198 switch ( MonthCal_HitTest(GetHwnd(), &hti
) )
201 case MCHT_CALENDARWEEKNUM
:
202 wxFAIL_MSG( "unexpected" );
206 case MCHT_CALENDARBK
:
208 case MCHT_TITLEMONTH
:
210 return wxCAL_HITTEST_NOWHERE
;
212 case MCHT_CALENDARDATE
:
214 date
->SetFromMSWSysDate(hti
.st
);
215 return wxCAL_HITTEST_DAY
;
217 case MCHT_CALENDARDAY
:
220 int day
= hti
.st
.wDayOfWeek
;
222 // the native control returns incorrect day of the week when
223 // the first day isn't Monday, i.e. the first column is always
224 // "Monday" even if its label is "Sunday", compensate for it
225 const int first
= LOWORD(MonthCal_GetFirstDayOfWeek(GetHwnd()));
226 if ( first
== MonthCal_Monday
)
228 // as MonthCal_Monday is 0 while wxDateTime::Mon is 1,
229 // normally we need to do this to transform from MSW
230 // convention to wx one
234 //else: but when the first day is MonthCal_Sunday, the native
235 // control still returns 0 (i.e. MonthCal_Monday) for the
236 // first column which looks like a bug in it but to work
237 // around it it's enough to not apply the correction above
239 *wd
= static_cast<wxDateTime::WeekDay
>(day
);
241 return wxCAL_HITTEST_HEADER
;
243 case MCHT_TITLEBTNNEXT
:
244 return wxCAL_HITTEST_INCMONTH
;
246 case MCHT_TITLEBTNPREV
:
247 return wxCAL_HITTEST_DECMONTH
;
249 case MCHT_CALENDARDATENEXT
:
250 case MCHT_CALENDARDATEPREV
:
251 return wxCAL_HITTEST_SURROUNDING_WEEK
;
255 // ----------------------------------------------------------------------------
256 // wxCalendarCtrl operations
257 // ----------------------------------------------------------------------------
259 bool wxCalendarCtrl::SetDate(const wxDateTime
& dt
)
261 wxCHECK_MSG( dt
.IsValid(), false, "invalid date" );
264 dt
.GetAsMSWSysDate(&st
);
265 if ( !MonthCal_SetCurSel(GetHwnd(), &st
) )
267 wxLogDebug(wxT("DateTime_SetSystemtime() failed"));
272 m_date
= dt
.GetDateOnly();
277 wxDateTime
wxCalendarCtrl::GetDate() const
282 if ( !MonthCal_GetCurSel(GetHwnd(), &st
) )
284 wxASSERT_MSG( !m_date
.IsValid(), "mismatch between data and control" );
286 return wxDefaultDateTime
;
290 dt
.SetFromMSWSysDate(st
);
292 // Windows XP and earlier didn't include the time component into the
293 // returned date but Windows 7 does, so we can't compare the full objects
294 // in the same way under all the Windows versions, just compare their date
296 wxASSERT_MSG( dt
.IsSameDate(m_date
), "mismatch between data and control" );
297 #endif // wxDEBUG_LEVEL
302 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& dt1
, const wxDateTime
& dt2
)
309 dt1
.GetAsMSWSysTime(st
+ 0);
315 dt2
.GetAsMSWSysTime(st
+ 1);
319 if ( !MonthCal_SetRange(GetHwnd(), flags
, st
) )
321 wxLogDebug(wxT("MonthCal_SetRange() failed"));
327 bool wxCalendarCtrl::GetDateRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
331 DWORD flags
= MonthCal_GetRange(GetHwnd(), st
);
334 if ( flags
& GDTR_MIN
)
335 dt1
->SetFromMSWSysDate(st
[0]);
337 *dt1
= wxDefaultDateTime
;
342 if ( flags
& GDTR_MAX
)
343 dt2
->SetFromMSWSysDate(st
[1]);
345 *dt2
= wxDefaultDateTime
;
351 // ----------------------------------------------------------------------------
352 // other wxCalendarCtrl operations
353 // ----------------------------------------------------------------------------
355 bool wxCalendarCtrl::EnableMonthChange(bool enable
)
357 if ( !wxCalendarCtrlBase::EnableMonthChange(enable
) )
360 wxDateTime dtStart
, dtEnd
;
366 dtEnd
= dtStart
.GetLastMonthDay();
368 //else: leave them invalid to remove the restriction
370 SetDateRange(dtStart
, dtEnd
);
375 void wxCalendarCtrl::Mark(size_t day
, bool mark
)
377 wxCHECK_RET( day
> 0 && day
< 32, "invalid day" );
379 int mask
= 1 << (day
- 1);
385 // calling Refresh() here is not enough to change the day appearance
389 void wxCalendarCtrl::SetHoliday(size_t day
)
391 wxCHECK_RET( day
> 0 && day
< 32, "invalid day" );
393 m_holidays
|= 1 << (day
- 1);
396 void wxCalendarCtrl::UpdateMarks()
398 // Currently the native control may show more than one month if its size is
399 // big enough. Ideal would be to prevent this from happening but there
400 // doesn't seem to be any obvious way to do it, so for now just handle the
401 // possibility that we can display several of them: one before the current
402 // one and up to 12 after it.
403 MONTHDAYSTATE states
[14] = { 0 };
404 const DWORD nMonths
= MonthCal_GetMonthRange(GetHwnd(), GMR_DAYSTATE
, NULL
);
406 // although in principle the calendar might not show any days from the
407 // preceding months, it seems like it always does, consider e.g. Feb 2010
408 // which starts on Monday and ends on Sunday and so could fit on 4 lines
409 // without showing any subsequent months -- the standard control still
410 // shows it on 6 lines and the number of visible months is still 3
412 // OTOH Windows 7 control can show all 12 months or even years or decades
413 // in its window if you "zoom out" of it by double clicking on free areas
414 // so the return value can be (much, in case of decades view) greater than
415 // 3 but in this case marks are not visible anyhow so simply ignore it
416 if ( nMonths
>= 2 && nMonths
<= WXSIZEOF(states
) )
418 // The current, fully visible month is always the second one.
419 states
[1] = m_marks
| m_holidays
;
421 if ( !MonthCal_SetDayState(GetHwnd(), nMonths
, states
) )
423 wxLogLastError(wxT("MonthCal_SetDayState"));
426 //else: not a month view at all
429 void wxCalendarCtrl::UpdateFirstDayOfWeek()
431 MonthCal_SetFirstDayOfWeek(GetHwnd(),
432 HasFlag(wxCAL_MONDAY_FIRST
) ? MonthCal_Monday
436 // ----------------------------------------------------------------------------
437 // wxCalendarCtrl events
438 // ----------------------------------------------------------------------------
440 bool wxCalendarCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
442 NMHDR
* hdr
= (NMHDR
*)lParam
;
447 // we need to update m_date first, before calling the user code
448 // which expects GetDate() to return the new date
449 const wxDateTime dateOld
= m_date
;
450 const NMSELCHANGE
* const sch
= (NMSELCHANGE
*)lParam
;
451 m_date
.SetFromMSWSysDate(sch
->stSelStart
);
453 // changing the year or the month results in a second dummy
454 // MCN_SELCHANGE event on this system which doesn't really
455 // change anything -- filter it out
456 if ( m_date
!= dateOld
)
458 if ( GenerateAllChangeEvents(dateOld
) )
460 // month changed, need to update the holidays if we use
469 case MCN_GETDAYSTATE
:
471 const NMDAYSTATE
* const ds
= (NMDAYSTATE
*)lParam
;
473 wxDateTime startDate
;
474 startDate
.SetFromMSWSysDate(ds
->stStart
);
476 // Ensure we have a valid date to work with.
477 wxDateTime currentDate
= m_date
.IsValid() ? m_date
: startDate
;
479 // Set to the start of month for comparison with startDate to
481 currentDate
.SetDay(1);
483 for ( int i
= 0; i
< ds
->cDayState
; i
++ )
485 // set holiday/marks only for the "current" month
486 if ( startDate
== currentDate
)
487 ds
->prgDayState
[i
] = m_marks
| m_holidays
;
489 ds
->prgDayState
[i
] = 0;
491 startDate
+= wxDateSpan::Month();
497 return wxCalendarCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
504 void wxCalendarCtrl::MSWOnDoubleClick(wxMouseEvent
& event
)
506 if ( HitTest(event
.GetPosition()) == wxCAL_HITTEST_DAY
)
508 if ( GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
) )
509 return; // skip event.Skip() below
515 void wxCalendarCtrl::MSWOnClick(wxMouseEvent
& event
)
517 // for some reason, the control doesn't get focus on its own when the user
524 #endif // wxUSE_CALENDARCTRL