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());
92 Connect(wxEVT_LEFT_DOWN
,
93 wxMouseEventHandler(wxCalendarCtrl
::MSWOnDoubleClick
));
98 WXDWORD wxCalendarCtrl
::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
100 WXDWORD styleMSW
= wxCalendarCtrlBase
::MSWGetStyle(style
, exstyle
);
102 // right now we don't support any native styles but we should add wx styles
103 // corresponding to MCS_NOTODAY, MCS_NOTODAYCIRCLE and MCS_WEEKNUMBERS
106 // for compatibility with the other versions, just turn off today display
107 // unconditionally for now
108 styleMSW
|= MCS_NOTODAY
;
113 // TODO: handle WM_WININICHANGE
115 // ----------------------------------------------------------------------------
116 // wxCalendarCtrl geometry
117 // ----------------------------------------------------------------------------
119 wxSize wxCalendarCtrl
::DoGetBestSize() const
122 if ( !GetHwnd() || !MonthCal_GetMinReqRect(GetHwnd(), &rc
) )
124 return wxCalendarCtrlBase
::DoGetBestSize();
127 const wxSize best
= wxRectFromRECT(rc
).GetSize() + GetWindowBorderSize();
132 wxCalendarHitTestResult
133 wxCalendarCtrl
::HitTest(const wxPoint
& pos
,
135 wxDateTime
::WeekDay
*wd
)
137 WinStruct
<MCHITTESTINFO
> hti
;
140 switch ( MonthCal_HitTest(GetHwnd(), &hti
) )
143 case MCHT_CALENDARWEEKNUM
:
144 wxFAIL_MSG( "unexpected" );
148 case MCHT_CALENDARBK
:
150 case MCHT_TITLEMONTH
:
152 return wxCAL_HITTEST_NOWHERE
;
154 case MCHT_CALENDARDATE
:
156 wxMSWDateControls
::FromSystemTime(date
, hti
.st
);
157 return wxCAL_HITTEST_DAY
;
159 case MCHT_CALENDARDAY
:
162 *wd
= wx_static_cast(wxDateTime
::WeekDay
, hti
.st
.wDayOfWeek
);
164 return wxCAL_HITTEST_HEADER
;
166 case MCHT_TITLEBTNNEXT
:
167 return wxCAL_HITTEST_INCMONTH
;
169 case MCHT_TITLEBTNPREV
:
170 return wxCAL_HITTEST_DECMONTH
;
172 case MCHT_CALENDARDATENEXT
:
173 case MCHT_CALENDARDATEPREV
:
174 return wxCAL_HITTEST_SURROUNDING_WEEK
;
178 // ----------------------------------------------------------------------------
179 // wxCalendarCtrl operations
180 // ----------------------------------------------------------------------------
182 bool wxCalendarCtrl
::SetDate(const wxDateTime
& dt
)
184 wxCHECK_MSG( dt
.IsValid(), false, "invalid date" );
187 wxMSWDateControls
::ToSystemTime(&st
, dt
);
188 if ( !MonthCal_SetCurSel(GetHwnd(), &st
) )
190 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
200 wxDateTime wxCalendarCtrl
::GetDate() const
204 if ( !MonthCal_GetCurSel(GetHwnd(), &st
) )
206 wxASSERT_MSG( !m_date
.IsValid(), "mismatch between data and control" );
208 return wxDefaultDateTime
;
212 wxMSWDateControls
::FromSystemTime(&dt
, st
);
214 wxASSERT_MSG( dt
== m_date
, "mismatch between data and control" );
215 #endif // __WXDEBUG__
220 bool wxCalendarCtrl
::SetDateRange(const wxDateTime
& dt1
, const wxDateTime
& dt2
)
227 wxMSWDateControls
::ToSystemTime(&st
[0], dt1
);
233 wxMSWDateControls
::ToSystemTime(&st
[1], dt2
);
237 if ( !MonthCal_SetRange(GetHwnd(), flags
, st
) )
239 wxLogDebug(_T("MonthCal_SetRange() failed"));
245 bool wxCalendarCtrl
::GetDateRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
249 DWORD flags
= MonthCal_GetRange(GetHwnd(), st
);
252 if ( flags
& GDTR_MIN
)
253 wxMSWDateControls
::FromSystemTime(dt1
, st
[0]);
255 *dt1
= wxDefaultDateTime
;
260 if ( flags
& GDTR_MAX
)
261 wxMSWDateControls
::FromSystemTime(dt2
, st
[1]);
263 *dt2
= wxDefaultDateTime
;
269 // ----------------------------------------------------------------------------
270 // other wxCalendarCtrl operations
271 // ----------------------------------------------------------------------------
273 bool wxCalendarCtrl
::EnableMonthChange(bool enable
)
275 if ( !wxCalendarCtrlBase
::EnableMonthChange(enable
) )
278 wxDateTime dtStart
, dtEnd
;
284 dtEnd
= dtStart
.GetLastMonthDay();
286 //else: leave them invalid to remove the restriction
288 SetDateRange(dtStart
, dtEnd
);
293 void wxCalendarCtrl
::Mark(size_t day
, bool mark
)
295 wxFAIL_MSG( "not implemented" );
298 // ----------------------------------------------------------------------------
299 // wxCalendarCtrl events
300 // ----------------------------------------------------------------------------
302 bool wxCalendarCtrl
::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
304 NMHDR
* hdr
= (NMHDR
*)lParam
;
308 // we need to update m_date first, before calling the user code
309 // which expects GetDate() to return the new date
310 const wxDateTime dateOld
= m_date
;
311 const NMSELCHANGE
* const sch
= (NMSELCHANGE
*)lParam
;
312 wxMSWDateControls
::FromSystemTime(&m_date
, sch
->stSelStart
);
314 // changing the year or the month results in a second dummy
315 // MCN_SELCHANGE event on this system which doesn't really change
316 // anything -- filter it out
317 if ( m_date
!= dateOld
)
319 GenerateAllChangeEvents(dateOld
);
326 return wxCalendarCtrlBase
::MSWOnNotify(idCtrl
, lParam
, result
);
329 void wxCalendarCtrl
::MSWOnDoubleClick(wxMouseEvent
& event
)
331 if ( HitTest(event
.GetPosition()) == wxCAL_HITTEST_DAY
)
333 if ( GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
) )
334 return; // skip event.Skip() below
340 #endif // wxUSE_CALENDARCTRL