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 // initialize the base class
60 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
63 // create the native control: this is a bit tricky as we want to receive
64 // double click events but the MONTHCAL_CLASS doesn't use CS_DBLCLKS style
65 // and so we create our own copy of it which does
66 static ClassRegistrar s_clsMonthCal
;
67 if ( !s_clsMonthCal
.IsInitialized() )
69 // get a copy of standard class and modify it
71 if ( ::GetClassInfo(NULL
, MONTHCAL_CLASS
, &wc
) )
73 wc
.lpszClassName
= wxT("_wx_SysMonthCtl32");
74 wc
.style
|= CS_DBLCLKS
;
75 s_clsMonthCal
.Register(wc
);
79 wxLogLastError(_T("GetClassInfoEx(SysMonthCal32)"));
83 const wxChar
* const clsname
= s_clsMonthCal
.IsRegistered()
84 ? s_clsMonthCal
.GetName().wx_str()
87 if ( !MSWCreateControl(clsname
, wxEmptyString
, pos
, size
) )
90 SetDate(dt
.IsValid() ? dt
: wxDateTime::Today());
94 Connect(wxEVT_LEFT_DCLICK
,
95 wxMouseEventHandler(wxCalendarCtrl::MSWOnDoubleClick
));
100 WXDWORD
wxCalendarCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
102 WXDWORD styleMSW
= wxCalendarCtrlBase::MSWGetStyle(style
, exstyle
);
104 // right now we don't support any native styles but we should add wx styles
105 // corresponding to MCS_NOTODAY, MCS_NOTODAYCIRCLE and MCS_WEEKNUMBERS
108 // for compatibility with the other versions, just turn off today display
109 // unconditionally for now
110 styleMSW
|= MCS_NOTODAY
;
112 // we also need this style for Mark() to work
113 styleMSW
|= MCS_DAYSTATE
;
118 // TODO: handle WM_WININICHANGE
120 // ----------------------------------------------------------------------------
121 // wxCalendarCtrl geometry
122 // ----------------------------------------------------------------------------
124 wxSize
wxCalendarCtrl::DoGetBestSize() const
127 if ( !GetHwnd() || !MonthCal_GetMinReqRect(GetHwnd(), &rc
) )
129 return wxCalendarCtrlBase::DoGetBestSize();
132 const wxSize best
= wxRectFromRECT(rc
).GetSize() + GetWindowBorderSize();
137 wxCalendarHitTestResult
138 wxCalendarCtrl::HitTest(const wxPoint
& pos
,
140 wxDateTime::WeekDay
*wd
)
142 WinStruct
<MCHITTESTINFO
> hti
;
145 switch ( MonthCal_HitTest(GetHwnd(), &hti
) )
148 case MCHT_CALENDARWEEKNUM
:
149 wxFAIL_MSG( "unexpected" );
153 case MCHT_CALENDARBK
:
155 case MCHT_TITLEMONTH
:
157 return wxCAL_HITTEST_NOWHERE
;
159 case MCHT_CALENDARDATE
:
161 wxMSWDateControls::FromSystemTime(date
, hti
.st
);
162 return wxCAL_HITTEST_DAY
;
164 case MCHT_CALENDARDAY
:
167 *wd
= wx_static_cast(wxDateTime::WeekDay
, hti
.st
.wDayOfWeek
);
169 return wxCAL_HITTEST_HEADER
;
171 case MCHT_TITLEBTNNEXT
:
172 return wxCAL_HITTEST_INCMONTH
;
174 case MCHT_TITLEBTNPREV
:
175 return wxCAL_HITTEST_DECMONTH
;
177 case MCHT_CALENDARDATENEXT
:
178 case MCHT_CALENDARDATEPREV
:
179 return wxCAL_HITTEST_SURROUNDING_WEEK
;
183 // ----------------------------------------------------------------------------
184 // wxCalendarCtrl operations
185 // ----------------------------------------------------------------------------
187 bool wxCalendarCtrl::SetDate(const wxDateTime
& dt
)
189 wxCHECK_MSG( dt
.IsValid(), false, "invalid date" );
192 wxMSWDateControls::ToSystemTime(&st
, dt
);
193 if ( !MonthCal_SetCurSel(GetHwnd(), &st
) )
195 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
205 wxDateTime
wxCalendarCtrl::GetDate() const
209 if ( !MonthCal_GetCurSel(GetHwnd(), &st
) )
211 wxASSERT_MSG( !m_date
.IsValid(), "mismatch between data and control" );
213 return wxDefaultDateTime
;
217 wxMSWDateControls::FromSystemTime(&dt
, st
);
219 wxASSERT_MSG( dt
== m_date
, "mismatch between data and control" );
220 #endif // __WXDEBUG__
225 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& dt1
, const wxDateTime
& dt2
)
232 wxMSWDateControls::ToSystemTime(&st
[0], dt1
);
238 wxMSWDateControls::ToSystemTime(&st
[1], dt2
);
242 if ( !MonthCal_SetRange(GetHwnd(), flags
, st
) )
244 wxLogDebug(_T("MonthCal_SetRange() failed"));
250 bool wxCalendarCtrl::GetDateRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
254 DWORD flags
= MonthCal_GetRange(GetHwnd(), st
);
257 if ( flags
& GDTR_MIN
)
258 wxMSWDateControls::FromSystemTime(dt1
, st
[0]);
260 *dt1
= wxDefaultDateTime
;
265 if ( flags
& GDTR_MAX
)
266 wxMSWDateControls::FromSystemTime(dt2
, st
[1]);
268 *dt2
= wxDefaultDateTime
;
274 // ----------------------------------------------------------------------------
275 // other wxCalendarCtrl operations
276 // ----------------------------------------------------------------------------
278 bool wxCalendarCtrl::EnableMonthChange(bool enable
)
280 if ( !wxCalendarCtrlBase::EnableMonthChange(enable
) )
283 wxDateTime dtStart
, dtEnd
;
289 dtEnd
= dtStart
.GetLastMonthDay();
291 //else: leave them invalid to remove the restriction
293 SetDateRange(dtStart
, dtEnd
);
298 void wxCalendarCtrl::Mark(size_t day
, bool mark
)
300 wxCHECK_RET( day
> 0 && day
< 32, "invalid day" );
302 int mask
= 1 << (day
- 1);
308 // calling Refresh() here is not enough to change the day appearance
312 void wxCalendarCtrl::UpdateMarks()
314 MONTHDAYSTATE states
[3];
315 const int nMonths
= MonthCal_GetMonthRange(GetHwnd(), GMR_DAYSTATE
, NULL
);
316 wxCHECK_RET( nMonths
<= WXSIZEOF(states
), "unexpected months range" );
318 for ( int i
= 0; i
< nMonths
; i
++ )
321 if ( !MonthCal_SetDayState(GetHwnd(), nMonths
, states
) )
323 wxLogLastError(_T("MonthCal_SetDayState"));
327 // ----------------------------------------------------------------------------
328 // wxCalendarCtrl events
329 // ----------------------------------------------------------------------------
331 bool wxCalendarCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
333 NMHDR
* hdr
= (NMHDR
*)lParam
;
338 // we need to update m_date first, before calling the user code
339 // which expects GetDate() to return the new date
340 const wxDateTime dateOld
= m_date
;
341 const NMSELCHANGE
* const sch
= (NMSELCHANGE
*)lParam
;
342 wxMSWDateControls::FromSystemTime(&m_date
, sch
->stSelStart
);
344 // changing the year or the month results in a second dummy
345 // MCN_SELCHANGE event on this system which doesn't really
346 // change anything -- filter it out
347 if ( m_date
!= dateOld
)
349 GenerateAllChangeEvents(dateOld
);
354 case MCN_GETDAYSTATE
:
356 const NMDAYSTATE
* const ds
= (NMDAYSTATE
*)lParam
;
357 for ( int i
= 0; i
< ds
->cDayState
; i
++ )
359 ds
->prgDayState
[i
] = m_marks
;
365 return wxCalendarCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
372 void wxCalendarCtrl::MSWOnDoubleClick(wxMouseEvent
& event
)
374 if ( HitTest(event
.GetPosition()) == wxCAL_HITTEST_DAY
)
376 if ( GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
) )
377 return; // skip event.Skip() below
383 #endif // wxUSE_CALENDARCTRL