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_DOWN
,
101 wxMouseEventHandler(wxCalendarCtrl::MSWOnClick
));
102 Connect(wxEVT_LEFT_DCLICK
,
103 wxMouseEventHandler(wxCalendarCtrl::MSWOnDoubleClick
));
108 WXDWORD
wxCalendarCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
110 WXDWORD styleMSW
= wxCalendarCtrlBase::MSWGetStyle(style
, exstyle
);
112 // right now we don't support any native styles but we should add wx styles
113 // corresponding to MCS_NOTODAY, MCS_NOTODAYCIRCLE and MCS_WEEKNUMBERS
116 // for compatibility with the other versions, just turn off today display
117 // unconditionally for now
118 styleMSW
|= MCS_NOTODAY
;
120 // we also need this style for Mark() to work
121 styleMSW
|= MCS_DAYSTATE
;
126 void wxCalendarCtrl::SetWindowStyleFlag(long style
)
128 const bool hadMondayFirst
= HasFlag(wxCAL_MONDAY_FIRST
);
130 wxCalendarCtrlBase::SetWindowStyleFlag(style
);
132 if ( HasFlag(wxCAL_MONDAY_FIRST
) != hadMondayFirst
)
133 UpdateFirstDayOfWeek();
136 // ----------------------------------------------------------------------------
137 // wxCalendarCtrl geometry
138 // ----------------------------------------------------------------------------
140 // TODO: handle WM_WININICHANGE
141 wxSize
wxCalendarCtrl::DoGetBestSize() const
144 if ( !GetHwnd() || !MonthCal_GetMinReqRect(GetHwnd(), &rc
) )
146 return wxCalendarCtrlBase::DoGetBestSize();
149 const wxSize best
= wxRectFromRECT(rc
).GetSize() + GetWindowBorderSize();
154 wxCalendarHitTestResult
155 wxCalendarCtrl::HitTest(const wxPoint
& pos
,
157 wxDateTime::WeekDay
*wd
)
159 WinStruct
<MCHITTESTINFO
> hti
;
162 switch ( MonthCal_HitTest(GetHwnd(), &hti
) )
165 case MCHT_CALENDARWEEKNUM
:
166 wxFAIL_MSG( "unexpected" );
170 case MCHT_CALENDARBK
:
172 case MCHT_TITLEMONTH
:
174 return wxCAL_HITTEST_NOWHERE
;
176 case MCHT_CALENDARDATE
:
178 wxMSWDateControls::FromSystemTime(date
, hti
.st
);
179 return wxCAL_HITTEST_DAY
;
181 case MCHT_CALENDARDAY
:
184 *wd
= wx_static_cast(wxDateTime::WeekDay
, hti
.st
.wDayOfWeek
);
186 return wxCAL_HITTEST_HEADER
;
188 case MCHT_TITLEBTNNEXT
:
189 return wxCAL_HITTEST_INCMONTH
;
191 case MCHT_TITLEBTNPREV
:
192 return wxCAL_HITTEST_DECMONTH
;
194 case MCHT_CALENDARDATENEXT
:
195 case MCHT_CALENDARDATEPREV
:
196 return wxCAL_HITTEST_SURROUNDING_WEEK
;
200 // ----------------------------------------------------------------------------
201 // wxCalendarCtrl operations
202 // ----------------------------------------------------------------------------
204 bool wxCalendarCtrl::SetDate(const wxDateTime
& dt
)
206 wxCHECK_MSG( dt
.IsValid(), false, "invalid date" );
209 wxMSWDateControls::ToSystemTime(&st
, dt
);
210 if ( !MonthCal_SetCurSel(GetHwnd(), &st
) )
212 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
222 wxDateTime
wxCalendarCtrl::GetDate() const
226 if ( !MonthCal_GetCurSel(GetHwnd(), &st
) )
228 wxASSERT_MSG( !m_date
.IsValid(), "mismatch between data and control" );
230 return wxDefaultDateTime
;
234 wxMSWDateControls::FromSystemTime(&dt
, st
);
236 wxASSERT_MSG( dt
== m_date
, "mismatch between data and control" );
237 #endif // __WXDEBUG__
242 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& dt1
, const wxDateTime
& dt2
)
249 wxMSWDateControls::ToSystemTime(&st
[0], dt1
);
255 wxMSWDateControls::ToSystemTime(&st
[1], dt2
);
259 if ( !MonthCal_SetRange(GetHwnd(), flags
, st
) )
261 wxLogDebug(_T("MonthCal_SetRange() failed"));
267 bool wxCalendarCtrl::GetDateRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
271 DWORD flags
= MonthCal_GetRange(GetHwnd(), st
);
274 if ( flags
& GDTR_MIN
)
275 wxMSWDateControls::FromSystemTime(dt1
, st
[0]);
277 *dt1
= wxDefaultDateTime
;
282 if ( flags
& GDTR_MAX
)
283 wxMSWDateControls::FromSystemTime(dt2
, st
[1]);
285 *dt2
= wxDefaultDateTime
;
291 // ----------------------------------------------------------------------------
292 // other wxCalendarCtrl operations
293 // ----------------------------------------------------------------------------
295 bool wxCalendarCtrl::EnableMonthChange(bool enable
)
297 if ( !wxCalendarCtrlBase::EnableMonthChange(enable
) )
300 wxDateTime dtStart
, dtEnd
;
306 dtEnd
= dtStart
.GetLastMonthDay();
308 //else: leave them invalid to remove the restriction
310 SetDateRange(dtStart
, dtEnd
);
315 void wxCalendarCtrl::Mark(size_t day
, bool mark
)
317 wxCHECK_RET( day
> 0 && day
< 32, "invalid day" );
319 int mask
= 1 << (day
- 1);
325 // calling Refresh() here is not enough to change the day appearance
329 void wxCalendarCtrl::UpdateMarks()
331 MONTHDAYSTATE states
[3];
332 const int nMonths
= MonthCal_GetMonthRange(GetHwnd(), GMR_DAYSTATE
, NULL
);
333 wxCHECK_RET( nMonths
<= (int)WXSIZEOF(states
), "unexpected months range" );
335 for ( int i
= 0; i
< nMonths
; i
++ )
338 if ( !MonthCal_SetDayState(GetHwnd(), nMonths
, states
) )
340 wxLogLastError(_T("MonthCal_SetDayState"));
344 void wxCalendarCtrl::UpdateFirstDayOfWeek()
346 MonthCal_SetFirstDayOfWeek(GetHwnd(), HasFlag(wxCAL_MONDAY_FIRST
) ? 0 : 6);
349 // ----------------------------------------------------------------------------
350 // wxCalendarCtrl events
351 // ----------------------------------------------------------------------------
353 bool wxCalendarCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
355 NMHDR
* hdr
= (NMHDR
*)lParam
;
360 // we need to update m_date first, before calling the user code
361 // which expects GetDate() to return the new date
362 const wxDateTime dateOld
= m_date
;
363 const NMSELCHANGE
* const sch
= (NMSELCHANGE
*)lParam
;
364 wxMSWDateControls::FromSystemTime(&m_date
, sch
->stSelStart
);
366 // changing the year or the month results in a second dummy
367 // MCN_SELCHANGE event on this system which doesn't really
368 // change anything -- filter it out
369 if ( m_date
!= dateOld
)
371 GenerateAllChangeEvents(dateOld
);
376 case MCN_GETDAYSTATE
:
378 const NMDAYSTATE
* const ds
= (NMDAYSTATE
*)lParam
;
379 for ( int i
= 0; i
< ds
->cDayState
; i
++ )
381 ds
->prgDayState
[i
] = m_marks
;
387 return wxCalendarCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
394 void wxCalendarCtrl::MSWOnDoubleClick(wxMouseEvent
& event
)
396 if ( HitTest(event
.GetPosition()) == wxCAL_HITTEST_DAY
)
398 if ( GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
) )
399 return; // skip event.Skip() below
405 void wxCalendarCtrl::MSWOnClick(wxMouseEvent
& event
)
407 // for some reason, the control doesn't get focus on its own when the user
414 #endif // wxUSE_CALENDARCTRL