1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/calctrl.cpp
3 // Purpose: wxCalendarCtrl implementation
4 // Author: Vadim Zeitlin
7 // Copyright: (C) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
25 #if wxUSE_CALENDARCTRL
28 #include "wx/msw/wrapwin.h"
29 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
30 #include "wx/msw/private.h"
33 #include "wx/calctrl.h"
35 #include "wx/msw/private/datecontrols.h"
37 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl
, wxControl
)
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
46 // values of week days used by the native control
58 } // anonymous namespace
60 // ============================================================================
62 // ============================================================================
64 // ----------------------------------------------------------------------------
65 // wxCalendarCtrl creation
66 // ----------------------------------------------------------------------------
68 void wxCalendarCtrl::Init()
75 wxCalendarCtrl::Create(wxWindow
*parent
,
83 if ( !wxMSWDateControls::CheckInitialization() )
86 // we need the arrows for the navigation
87 style
|= wxWANTS_CHARS
;
89 // initialize the base class
90 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
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() )
99 // get a copy of standard class and modify it
101 if ( ::GetClassInfo(NULL
, MONTHCAL_CLASS
, &wc
) )
103 wc
.lpszClassName
= wxT("_wx_SysMonthCtl32");
104 wc
.style
|= CS_DBLCLKS
;
105 s_clsMonthCal
.Register(wc
);
109 wxLogLastError(wxT("GetClassInfoEx(SysMonthCal32)"));
113 const wxChar
* const clsname
= s_clsMonthCal
.IsRegistered()
114 ? s_clsMonthCal
.GetName().wx_str()
117 if ( !MSWCreateControl(clsname
, wxEmptyString
, pos
, size
) )
120 // initialize the control
121 UpdateFirstDayOfWeek();
123 SetDate(dt
.IsValid() ? dt
: wxDateTime::Today());
125 if ( SetHolidayAttrs() )
128 Connect(wxEVT_LEFT_DOWN
,
129 wxMouseEventHandler(wxCalendarCtrl::MSWOnClick
));
130 Connect(wxEVT_LEFT_DCLICK
,
131 wxMouseEventHandler(wxCalendarCtrl::MSWOnDoubleClick
));
136 WXDWORD
wxCalendarCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
138 WXDWORD styleMSW
= wxCalendarCtrlBase::MSWGetStyle(style
, exstyle
);
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)
143 // for compatibility with the other versions, just turn off today display
144 // unconditionally for now
145 styleMSW
|= MCS_NOTODAY
;
147 // we also need this style for Mark() to work
148 styleMSW
|= MCS_DAYSTATE
;
150 if ( style
& wxCAL_SHOW_WEEK_NUMBERS
)
151 styleMSW
|= MCS_WEEKNUMBERS
;
156 void wxCalendarCtrl::SetWindowStyleFlag(long style
)
158 const bool hadMondayFirst
= HasFlag(wxCAL_MONDAY_FIRST
);
160 wxCalendarCtrlBase::SetWindowStyleFlag(style
);
162 if ( HasFlag(wxCAL_MONDAY_FIRST
) != hadMondayFirst
)
163 UpdateFirstDayOfWeek();
166 // ----------------------------------------------------------------------------
167 // wxCalendarCtrl geometry
168 // ----------------------------------------------------------------------------
170 // TODO: handle WM_WININICHANGE
171 wxSize
wxCalendarCtrl::DoGetBestSize() const
174 if ( !GetHwnd() || !MonthCal_GetMinReqRect(GetHwnd(), &rc
) )
176 return wxCalendarCtrlBase::DoGetBestSize();
179 const wxSize best
= wxRectFromRECT(rc
).GetSize() + GetWindowBorderSize();
184 wxCalendarHitTestResult
185 wxCalendarCtrl::HitTest(const wxPoint
& pos
,
187 wxDateTime::WeekDay
*wd
)
189 WinStruct
<MCHITTESTINFO
> hti
;
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
;
201 switch ( MonthCal_HitTest(GetHwnd(), &hti
) )
204 case MCHT_CALENDARWEEKNUM
:
205 wxFAIL_MSG( "unexpected" );
209 case MCHT_CALENDARBK
:
211 case MCHT_TITLEMONTH
:
213 return wxCAL_HITTEST_NOWHERE
;
215 case MCHT_CALENDARDATE
:
217 date
->SetFromMSWSysTime(hti
.st
);
218 return wxCAL_HITTEST_DAY
;
220 case MCHT_CALENDARDAY
:
223 int day
= hti
.st
.wDayOfWeek
;
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
)
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
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
242 *wd
= static_cast<wxDateTime::WeekDay
>(day
);
244 return wxCAL_HITTEST_HEADER
;
246 case MCHT_TITLEBTNNEXT
:
247 return wxCAL_HITTEST_INCMONTH
;
249 case MCHT_TITLEBTNPREV
:
250 return wxCAL_HITTEST_DECMONTH
;
252 case MCHT_CALENDARDATENEXT
:
253 case MCHT_CALENDARDATEPREV
:
254 return wxCAL_HITTEST_SURROUNDING_WEEK
;
258 // ----------------------------------------------------------------------------
259 // wxCalendarCtrl operations
260 // ----------------------------------------------------------------------------
262 bool wxCalendarCtrl::SetDate(const wxDateTime
& dt
)
264 wxCHECK_MSG( dt
.IsValid(), false, "invalid date" );
267 dt
.GetAsMSWSysTime(&st
);
268 if ( !MonthCal_SetCurSel(GetHwnd(), &st
) )
270 wxLogDebug(wxT("DateTime_SetSystemtime() failed"));
280 wxDateTime
wxCalendarCtrl::GetDate() const
284 if ( !MonthCal_GetCurSel(GetHwnd(), &st
) )
286 wxASSERT_MSG( !m_date
.IsValid(), "mismatch between data and control" );
288 return wxDefaultDateTime
;
293 wxASSERT_MSG( dt
== m_date
, "mismatch between data and control" );
294 #endif // wxDEBUG_LEVEL
299 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& dt1
, const wxDateTime
& dt2
)
306 dt1
.GetAsMSWSysTime(st
+ 0);
312 dt2
.GetAsMSWSysTime(st
+ 1);
316 if ( !MonthCal_SetRange(GetHwnd(), flags
, st
) )
318 wxLogDebug(wxT("MonthCal_SetRange() failed"));
324 bool wxCalendarCtrl::GetDateRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
328 DWORD flags
= MonthCal_GetRange(GetHwnd(), st
);
331 if ( flags
& GDTR_MIN
)
332 dt1
->SetFromMSWSysTime(st
[0]);
334 *dt1
= wxDefaultDateTime
;
339 if ( flags
& GDTR_MAX
)
340 dt2
->SetFromMSWSysTime(st
[1]);
342 *dt2
= wxDefaultDateTime
;
348 // ----------------------------------------------------------------------------
349 // other wxCalendarCtrl operations
350 // ----------------------------------------------------------------------------
352 bool wxCalendarCtrl::EnableMonthChange(bool enable
)
354 if ( !wxCalendarCtrlBase::EnableMonthChange(enable
) )
357 wxDateTime dtStart
, dtEnd
;
363 dtEnd
= dtStart
.GetLastMonthDay();
365 //else: leave them invalid to remove the restriction
367 SetDateRange(dtStart
, dtEnd
);
372 void wxCalendarCtrl::Mark(size_t day
, bool mark
)
374 wxCHECK_RET( day
> 0 && day
< 32, "invalid day" );
376 int mask
= 1 << (day
- 1);
382 // calling Refresh() here is not enough to change the day appearance
386 void wxCalendarCtrl::SetHoliday(size_t day
)
388 wxCHECK_RET( day
> 0 && day
< 32, "invalid day" );
390 m_holidays
|= 1 << (day
- 1);
393 void wxCalendarCtrl::UpdateMarks()
395 // we show only one full month but there can be some days from the month
396 // before it and from the one after it so days from 3 different months can
397 // be partially shown
398 MONTHDAYSTATE states
[3] = { 0 };
399 const int nMonths
= MonthCal_GetMonthRange(GetHwnd(), GMR_DAYSTATE
, NULL
);
401 // although in principle the calendar might not show any days from the
402 // preceding months, it seems like it always does, consider e.g. Feb 2010
403 // which starts on Monday and ends on Sunday and so could fit on 4 lines
404 // without showing any subsequent months -- the standard control still
405 // shows it on 6 lines and the number of visible months is still 3
406 wxCHECK_RET( nMonths
== (int)WXSIZEOF(states
), "unexpected months range" );
408 // the fully visible month is the one in the middle
409 states
[1] = m_marks
| m_holidays
;
411 if ( !MonthCal_SetDayState(GetHwnd(), nMonths
, states
) )
413 wxLogLastError(wxT("MonthCal_SetDayState"));
417 void wxCalendarCtrl::UpdateFirstDayOfWeek()
419 MonthCal_SetFirstDayOfWeek(GetHwnd(),
420 HasFlag(wxCAL_MONDAY_FIRST
) ? MonthCal_Monday
424 // ----------------------------------------------------------------------------
425 // wxCalendarCtrl events
426 // ----------------------------------------------------------------------------
428 bool wxCalendarCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
430 NMHDR
* hdr
= (NMHDR
*)lParam
;
435 // we need to update m_date first, before calling the user code
436 // which expects GetDate() to return the new date
437 const wxDateTime dateOld
= m_date
;
438 const NMSELCHANGE
* const sch
= (NMSELCHANGE
*)lParam
;
439 m_date
.SetFromMSWSysTime(sch
->stSelStart
);
441 // changing the year or the month results in a second dummy
442 // MCN_SELCHANGE event on this system which doesn't really
443 // change anything -- filter it out
444 if ( m_date
!= dateOld
)
446 if ( GenerateAllChangeEvents(dateOld
) )
448 // month changed, need to update the holidays if we use
450 if ( SetHolidayAttrs() )
457 case MCN_GETDAYSTATE
:
459 const NMDAYSTATE
* const ds
= (NMDAYSTATE
*)lParam
;
460 for ( int i
= 0; i
< ds
->cDayState
; i
++ )
462 ds
->prgDayState
[i
] = m_marks
| m_holidays
;
468 return wxCalendarCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
475 void wxCalendarCtrl::MSWOnDoubleClick(wxMouseEvent
& event
)
477 if ( HitTest(event
.GetPosition()) == wxCAL_HITTEST_DAY
)
479 if ( GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
) )
480 return; // skip event.Skip() below
486 void wxCalendarCtrl::MSWOnClick(wxMouseEvent
& event
)
488 // for some reason, the control doesn't get focus on its own when the user
495 #endif // wxUSE_CALENDARCTRL