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 // initialize the control
91 UpdateFirstDayOfWeek();
93 SetDate(dt
.IsValid() ? dt
: wxDateTime::Today());
97 Connect(wxEVT_LEFT_DCLICK
,
98 wxMouseEventHandler(wxCalendarCtrl::MSWOnDoubleClick
));
103 WXDWORD
wxCalendarCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
105 WXDWORD styleMSW
= wxCalendarCtrlBase::MSWGetStyle(style
, exstyle
);
107 // right now we don't support any native styles but we should add wx styles
108 // corresponding to MCS_NOTODAY, MCS_NOTODAYCIRCLE and MCS_WEEKNUMBERS
111 // for compatibility with the other versions, just turn off today display
112 // unconditionally for now
113 styleMSW
|= MCS_NOTODAY
;
115 // we also need this style for Mark() to work
116 styleMSW
|= MCS_DAYSTATE
;
121 void wxCalendarCtrl::SetWindowStyleFlag(long style
)
123 const bool hadMondayFirst
= HasFlag(wxCAL_MONDAY_FIRST
);
125 wxCalendarCtrlBase::SetWindowStyleFlag(style
);
127 if ( HasFlag(wxCAL_MONDAY_FIRST
) != hadMondayFirst
)
128 UpdateFirstDayOfWeek();
131 // ----------------------------------------------------------------------------
132 // wxCalendarCtrl geometry
133 // ----------------------------------------------------------------------------
135 // TODO: handle WM_WININICHANGE
136 wxSize
wxCalendarCtrl::DoGetBestSize() const
139 if ( !GetHwnd() || !MonthCal_GetMinReqRect(GetHwnd(), &rc
) )
141 return wxCalendarCtrlBase::DoGetBestSize();
144 const wxSize best
= wxRectFromRECT(rc
).GetSize() + GetWindowBorderSize();
149 wxCalendarHitTestResult
150 wxCalendarCtrl::HitTest(const wxPoint
& pos
,
152 wxDateTime::WeekDay
*wd
)
154 WinStruct
<MCHITTESTINFO
> hti
;
157 switch ( MonthCal_HitTest(GetHwnd(), &hti
) )
160 case MCHT_CALENDARWEEKNUM
:
161 wxFAIL_MSG( "unexpected" );
165 case MCHT_CALENDARBK
:
167 case MCHT_TITLEMONTH
:
169 return wxCAL_HITTEST_NOWHERE
;
171 case MCHT_CALENDARDATE
:
173 wxMSWDateControls::FromSystemTime(date
, hti
.st
);
174 return wxCAL_HITTEST_DAY
;
176 case MCHT_CALENDARDAY
:
179 *wd
= wx_static_cast(wxDateTime::WeekDay
, hti
.st
.wDayOfWeek
);
181 return wxCAL_HITTEST_HEADER
;
183 case MCHT_TITLEBTNNEXT
:
184 return wxCAL_HITTEST_INCMONTH
;
186 case MCHT_TITLEBTNPREV
:
187 return wxCAL_HITTEST_DECMONTH
;
189 case MCHT_CALENDARDATENEXT
:
190 case MCHT_CALENDARDATEPREV
:
191 return wxCAL_HITTEST_SURROUNDING_WEEK
;
195 // ----------------------------------------------------------------------------
196 // wxCalendarCtrl operations
197 // ----------------------------------------------------------------------------
199 bool wxCalendarCtrl::SetDate(const wxDateTime
& dt
)
201 wxCHECK_MSG( dt
.IsValid(), false, "invalid date" );
204 wxMSWDateControls::ToSystemTime(&st
, dt
);
205 if ( !MonthCal_SetCurSel(GetHwnd(), &st
) )
207 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
217 wxDateTime
wxCalendarCtrl::GetDate() const
221 if ( !MonthCal_GetCurSel(GetHwnd(), &st
) )
223 wxASSERT_MSG( !m_date
.IsValid(), "mismatch between data and control" );
225 return wxDefaultDateTime
;
229 wxMSWDateControls::FromSystemTime(&dt
, st
);
231 wxASSERT_MSG( dt
== m_date
, "mismatch between data and control" );
232 #endif // __WXDEBUG__
237 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& dt1
, const wxDateTime
& dt2
)
244 wxMSWDateControls::ToSystemTime(&st
[0], dt1
);
250 wxMSWDateControls::ToSystemTime(&st
[1], dt2
);
254 if ( !MonthCal_SetRange(GetHwnd(), flags
, st
) )
256 wxLogDebug(_T("MonthCal_SetRange() failed"));
262 bool wxCalendarCtrl::GetDateRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
266 DWORD flags
= MonthCal_GetRange(GetHwnd(), st
);
269 if ( flags
& GDTR_MIN
)
270 wxMSWDateControls::FromSystemTime(dt1
, st
[0]);
272 *dt1
= wxDefaultDateTime
;
277 if ( flags
& GDTR_MAX
)
278 wxMSWDateControls::FromSystemTime(dt2
, st
[1]);
280 *dt2
= wxDefaultDateTime
;
286 // ----------------------------------------------------------------------------
287 // other wxCalendarCtrl operations
288 // ----------------------------------------------------------------------------
290 bool wxCalendarCtrl::EnableMonthChange(bool enable
)
292 if ( !wxCalendarCtrlBase::EnableMonthChange(enable
) )
295 wxDateTime dtStart
, dtEnd
;
301 dtEnd
= dtStart
.GetLastMonthDay();
303 //else: leave them invalid to remove the restriction
305 SetDateRange(dtStart
, dtEnd
);
310 void wxCalendarCtrl::Mark(size_t day
, bool mark
)
312 wxCHECK_RET( day
> 0 && day
< 32, "invalid day" );
314 int mask
= 1 << (day
- 1);
320 // calling Refresh() here is not enough to change the day appearance
324 void wxCalendarCtrl::UpdateMarks()
326 MONTHDAYSTATE states
[3];
327 const int nMonths
= MonthCal_GetMonthRange(GetHwnd(), GMR_DAYSTATE
, NULL
);
328 wxCHECK_RET( nMonths
<= WXSIZEOF(states
), "unexpected months range" );
330 for ( int i
= 0; i
< nMonths
; i
++ )
333 if ( !MonthCal_SetDayState(GetHwnd(), nMonths
, states
) )
335 wxLogLastError(_T("MonthCal_SetDayState"));
339 void wxCalendarCtrl::UpdateFirstDayOfWeek()
341 MonthCal_SetFirstDayOfWeek(GetHwnd(), HasFlag(wxCAL_MONDAY_FIRST
) ? 0 : 6);
344 // ----------------------------------------------------------------------------
345 // wxCalendarCtrl events
346 // ----------------------------------------------------------------------------
348 bool wxCalendarCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
350 NMHDR
* hdr
= (NMHDR
*)lParam
;
355 // we need to update m_date first, before calling the user code
356 // which expects GetDate() to return the new date
357 const wxDateTime dateOld
= m_date
;
358 const NMSELCHANGE
* const sch
= (NMSELCHANGE
*)lParam
;
359 wxMSWDateControls::FromSystemTime(&m_date
, sch
->stSelStart
);
361 // changing the year or the month results in a second dummy
362 // MCN_SELCHANGE event on this system which doesn't really
363 // change anything -- filter it out
364 if ( m_date
!= dateOld
)
366 GenerateAllChangeEvents(dateOld
);
371 case MCN_GETDAYSTATE
:
373 const NMDAYSTATE
* const ds
= (NMDAYSTATE
*)lParam
;
374 for ( int i
= 0; i
< ds
->cDayState
; i
++ )
376 ds
->prgDayState
[i
] = m_marks
;
382 return wxCalendarCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
389 void wxCalendarCtrl::MSWOnDoubleClick(wxMouseEvent
& event
)
391 if ( HitTest(event
.GetPosition()) == wxCAL_HITTEST_DAY
)
393 if ( GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
) )
394 return; // skip event.Skip() below
400 #endif // wxUSE_CALENDARCTRL