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 // ============================================================================
43 // ----------------------------------------------------------------------------
44 // wxCalendarCtrl creation
45 // ----------------------------------------------------------------------------
48 wxCalendarCtrl::Create(wxWindow
*parent
,
56 if ( !wxMSWDateControls::CheckInitialization() )
59 // we need the arrows for the navigation
60 style
|= wxWANTS_CHARS
;
62 // initialize the base class
63 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
66 // create the native control: this is a bit tricky as we want to receive
67 // double click events but the MONTHCAL_CLASS doesn't use CS_DBLCLKS style
68 // and so we create our own copy of it which does
69 static ClassRegistrar s_clsMonthCal
;
70 if ( !s_clsMonthCal
.IsInitialized() )
72 // get a copy of standard class and modify it
74 if ( ::GetClassInfo(NULL
, MONTHCAL_CLASS
, &wc
) )
76 wc
.lpszClassName
= wxT("_wx_SysMonthCtl32");
77 wc
.style
|= CS_DBLCLKS
;
78 s_clsMonthCal
.Register(wc
);
82 wxLogLastError(_T("GetClassInfoEx(SysMonthCal32)"));
86 const wxChar
* const clsname
= s_clsMonthCal
.IsRegistered()
87 ? s_clsMonthCal
.GetName().wx_str()
90 if ( !MSWCreateControl(clsname
, wxEmptyString
, pos
, size
) )
93 // initialize the control
94 UpdateFirstDayOfWeek();
96 SetDate(dt
.IsValid() ? dt
: wxDateTime::Today());
100 Connect(wxEVT_LEFT_DCLICK
,
101 wxMouseEventHandler(wxCalendarCtrl::MSWOnDoubleClick
));
106 WXDWORD
wxCalendarCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
108 WXDWORD styleMSW
= wxCalendarCtrlBase::MSWGetStyle(style
, exstyle
);
110 // right now we don't support any native styles but we should add wx styles
111 // corresponding to MCS_NOTODAY, MCS_NOTODAYCIRCLE and MCS_WEEKNUMBERS
114 // for compatibility with the other versions, just turn off today display
115 // unconditionally for now
116 styleMSW
|= MCS_NOTODAY
;
118 // we also need this style for Mark() to work
119 styleMSW
|= MCS_DAYSTATE
;
124 void wxCalendarCtrl::SetWindowStyleFlag(long style
)
126 const bool hadMondayFirst
= HasFlag(wxCAL_MONDAY_FIRST
);
128 wxCalendarCtrlBase::SetWindowStyleFlag(style
);
130 if ( HasFlag(wxCAL_MONDAY_FIRST
) != hadMondayFirst
)
131 UpdateFirstDayOfWeek();
134 // ----------------------------------------------------------------------------
135 // wxCalendarCtrl geometry
136 // ----------------------------------------------------------------------------
138 // TODO: handle WM_WININICHANGE
139 wxSize
wxCalendarCtrl::DoGetBestSize() const
142 if ( !GetHwnd() || !MonthCal_GetMinReqRect(GetHwnd(), &rc
) )
144 return wxCalendarCtrlBase::DoGetBestSize();
147 const wxSize best
= wxRectFromRECT(rc
).GetSize() + GetWindowBorderSize();
152 wxCalendarHitTestResult
153 wxCalendarCtrl::HitTest(const wxPoint
& pos
,
155 wxDateTime::WeekDay
*wd
)
157 WinStruct
<MCHITTESTINFO
> hti
;
160 switch ( MonthCal_HitTest(GetHwnd(), &hti
) )
163 case MCHT_CALENDARWEEKNUM
:
164 wxFAIL_MSG( "unexpected" );
168 case MCHT_CALENDARBK
:
170 case MCHT_TITLEMONTH
:
172 return wxCAL_HITTEST_NOWHERE
;
174 case MCHT_CALENDARDATE
:
176 wxMSWDateControls::FromSystemTime(date
, hti
.st
);
177 return wxCAL_HITTEST_DAY
;
179 case MCHT_CALENDARDAY
:
182 *wd
= wx_static_cast(wxDateTime::WeekDay
, hti
.st
.wDayOfWeek
);
184 return wxCAL_HITTEST_HEADER
;
186 case MCHT_TITLEBTNNEXT
:
187 return wxCAL_HITTEST_INCMONTH
;
189 case MCHT_TITLEBTNPREV
:
190 return wxCAL_HITTEST_DECMONTH
;
192 case MCHT_CALENDARDATENEXT
:
193 case MCHT_CALENDARDATEPREV
:
194 return wxCAL_HITTEST_SURROUNDING_WEEK
;
198 // ----------------------------------------------------------------------------
199 // wxCalendarCtrl operations
200 // ----------------------------------------------------------------------------
202 bool wxCalendarCtrl::SetDate(const wxDateTime
& dt
)
204 wxCHECK_MSG( dt
.IsValid(), false, "invalid date" );
207 wxMSWDateControls::ToSystemTime(&st
, dt
);
208 if ( !MonthCal_SetCurSel(GetHwnd(), &st
) )
210 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
220 wxDateTime
wxCalendarCtrl::GetDate() const
224 if ( !MonthCal_GetCurSel(GetHwnd(), &st
) )
226 wxASSERT_MSG( !m_date
.IsValid(), "mismatch between data and control" );
228 return wxDefaultDateTime
;
232 wxMSWDateControls::FromSystemTime(&dt
, st
);
234 wxASSERT_MSG( dt
== m_date
, "mismatch between data and control" );
235 #endif // __WXDEBUG__
240 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& dt1
, const wxDateTime
& dt2
)
247 wxMSWDateControls::ToSystemTime(&st
[0], dt1
);
253 wxMSWDateControls::ToSystemTime(&st
[1], dt2
);
257 if ( !MonthCal_SetRange(GetHwnd(), flags
, st
) )
259 wxLogDebug(_T("MonthCal_SetRange() failed"));
265 bool wxCalendarCtrl::GetDateRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
269 DWORD flags
= MonthCal_GetRange(GetHwnd(), st
);
272 if ( flags
& GDTR_MIN
)
273 wxMSWDateControls::FromSystemTime(dt1
, st
[0]);
275 *dt1
= wxDefaultDateTime
;
280 if ( flags
& GDTR_MAX
)
281 wxMSWDateControls::FromSystemTime(dt2
, st
[1]);
283 *dt2
= wxDefaultDateTime
;
289 // ----------------------------------------------------------------------------
290 // other wxCalendarCtrl operations
291 // ----------------------------------------------------------------------------
293 bool wxCalendarCtrl::EnableMonthChange(bool enable
)
295 if ( !wxCalendarCtrlBase::EnableMonthChange(enable
) )
298 wxDateTime dtStart
, dtEnd
;
304 dtEnd
= dtStart
.GetLastMonthDay();
306 //else: leave them invalid to remove the restriction
308 SetDateRange(dtStart
, dtEnd
);
313 void wxCalendarCtrl::Mark(size_t day
, bool mark
)
315 wxCHECK_RET( day
> 0 && day
< 32, "invalid day" );
317 int mask
= 1 << (day
- 1);
323 // calling Refresh() here is not enough to change the day appearance
327 void wxCalendarCtrl::UpdateMarks()
329 MONTHDAYSTATE states
[3];
330 const int nMonths
= MonthCal_GetMonthRange(GetHwnd(), GMR_DAYSTATE
, NULL
);
331 wxCHECK_RET( nMonths
<= WXSIZEOF(states
), "unexpected months range" );
333 for ( int i
= 0; i
< nMonths
; i
++ )
336 if ( !MonthCal_SetDayState(GetHwnd(), nMonths
, states
) )
338 wxLogLastError(_T("MonthCal_SetDayState"));
342 void wxCalendarCtrl::UpdateFirstDayOfWeek()
344 MonthCal_SetFirstDayOfWeek(GetHwnd(), HasFlag(wxCAL_MONDAY_FIRST
) ? 0 : 6);
347 // ----------------------------------------------------------------------------
348 // wxCalendarCtrl events
349 // ----------------------------------------------------------------------------
351 bool wxCalendarCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
353 NMHDR
* hdr
= (NMHDR
*)lParam
;
358 // we need to update m_date first, before calling the user code
359 // which expects GetDate() to return the new date
360 const wxDateTime dateOld
= m_date
;
361 const NMSELCHANGE
* const sch
= (NMSELCHANGE
*)lParam
;
362 wxMSWDateControls::FromSystemTime(&m_date
, sch
->stSelStart
);
364 // changing the year or the month results in a second dummy
365 // MCN_SELCHANGE event on this system which doesn't really
366 // change anything -- filter it out
367 if ( m_date
!= dateOld
)
369 GenerateAllChangeEvents(dateOld
);
374 case MCN_GETDAYSTATE
:
376 const NMDAYSTATE
* const ds
= (NMDAYSTATE
*)lParam
;
377 for ( int i
= 0; i
< ds
->cDayState
; i
++ )
379 ds
->prgDayState
[i
] = m_marks
;
385 return wxCalendarCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
392 void wxCalendarCtrl::MSWOnDoubleClick(wxMouseEvent
& event
)
394 if ( HitTest(event
.GetPosition()) == wxCAL_HITTEST_DAY
)
396 if ( GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
) )
397 return; // skip event.Skip() below
403 #endif // wxUSE_CALENDARCTRL