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 all native styles but we should add wx styles
113 // corresponding to MCS_NOTODAY and MCS_NOTODAYCIRCLE probably (TODO)
115 // for compatibility with the other versions, just turn off today display
116 // unconditionally for now
117 styleMSW
|= MCS_NOTODAY
;
119 // we also need this style for Mark() to work
120 styleMSW
|= MCS_DAYSTATE
;
122 if ( style
& wxCAL_SHOW_WEEK_NUMBERS
)
123 styleMSW
|= MCS_WEEKNUMBERS
;
128 void wxCalendarCtrl::SetWindowStyleFlag(long style
)
130 const bool hadMondayFirst
= HasFlag(wxCAL_MONDAY_FIRST
);
132 wxCalendarCtrlBase::SetWindowStyleFlag(style
);
134 if ( HasFlag(wxCAL_MONDAY_FIRST
) != hadMondayFirst
)
135 UpdateFirstDayOfWeek();
138 // ----------------------------------------------------------------------------
139 // wxCalendarCtrl geometry
140 // ----------------------------------------------------------------------------
142 // TODO: handle WM_WININICHANGE
143 wxSize
wxCalendarCtrl::DoGetBestSize() const
146 if ( !GetHwnd() || !MonthCal_GetMinReqRect(GetHwnd(), &rc
) )
148 return wxCalendarCtrlBase::DoGetBestSize();
151 const wxSize best
= wxRectFromRECT(rc
).GetSize() + GetWindowBorderSize();
156 wxCalendarHitTestResult
157 wxCalendarCtrl::HitTest(const wxPoint
& pos
,
159 wxDateTime::WeekDay
*wd
)
161 WinStruct
<MCHITTESTINFO
> hti
;
164 switch ( MonthCal_HitTest(GetHwnd(), &hti
) )
167 case MCHT_CALENDARWEEKNUM
:
168 wxFAIL_MSG( "unexpected" );
172 case MCHT_CALENDARBK
:
174 case MCHT_TITLEMONTH
:
176 return wxCAL_HITTEST_NOWHERE
;
178 case MCHT_CALENDARDATE
:
180 date
->SetFromMSWSysTime(hti
.st
);
181 return wxCAL_HITTEST_DAY
;
183 case MCHT_CALENDARDAY
:
186 *wd
= wx_static_cast(wxDateTime::WeekDay
, hti
.st
.wDayOfWeek
);
188 return wxCAL_HITTEST_HEADER
;
190 case MCHT_TITLEBTNNEXT
:
191 return wxCAL_HITTEST_INCMONTH
;
193 case MCHT_TITLEBTNPREV
:
194 return wxCAL_HITTEST_DECMONTH
;
196 case MCHT_CALENDARDATENEXT
:
197 case MCHT_CALENDARDATEPREV
:
198 return wxCAL_HITTEST_SURROUNDING_WEEK
;
202 // ----------------------------------------------------------------------------
203 // wxCalendarCtrl operations
204 // ----------------------------------------------------------------------------
206 bool wxCalendarCtrl::SetDate(const wxDateTime
& dt
)
208 wxCHECK_MSG( dt
.IsValid(), false, "invalid date" );
211 dt
.GetAsMSWSysTime(&st
);
212 if ( !MonthCal_SetCurSel(GetHwnd(), &st
) )
214 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
224 wxDateTime
wxCalendarCtrl::GetDate() const
228 if ( !MonthCal_GetCurSel(GetHwnd(), &st
) )
230 wxASSERT_MSG( !m_date
.IsValid(), "mismatch between data and control" );
232 return wxDefaultDateTime
;
237 wxASSERT_MSG( dt
== m_date
, "mismatch between data and control" );
238 #endif // __WXDEBUG__
243 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& dt1
, const wxDateTime
& dt2
)
250 dt1
.GetAsMSWSysTime(st
+ 0);
256 dt2
.GetAsMSWSysTime(st
+ 1);
260 if ( !MonthCal_SetRange(GetHwnd(), flags
, st
) )
262 wxLogDebug(_T("MonthCal_SetRange() failed"));
268 bool wxCalendarCtrl::GetDateRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
272 DWORD flags
= MonthCal_GetRange(GetHwnd(), st
);
275 if ( flags
& GDTR_MIN
)
276 dt1
->SetFromMSWSysTime(st
[0]);
278 *dt1
= wxDefaultDateTime
;
283 if ( flags
& GDTR_MAX
)
284 dt2
->SetFromMSWSysTime(st
[1]);
286 *dt2
= wxDefaultDateTime
;
292 // ----------------------------------------------------------------------------
293 // other wxCalendarCtrl operations
294 // ----------------------------------------------------------------------------
296 bool wxCalendarCtrl::EnableMonthChange(bool enable
)
298 if ( !wxCalendarCtrlBase::EnableMonthChange(enable
) )
301 wxDateTime dtStart
, dtEnd
;
307 dtEnd
= dtStart
.GetLastMonthDay();
309 //else: leave them invalid to remove the restriction
311 SetDateRange(dtStart
, dtEnd
);
316 void wxCalendarCtrl::Mark(size_t day
, bool mark
)
318 wxCHECK_RET( day
> 0 && day
< 32, "invalid day" );
320 int mask
= 1 << (day
- 1);
326 // calling Refresh() here is not enough to change the day appearance
330 void wxCalendarCtrl::UpdateMarks()
332 MONTHDAYSTATE states
[3];
333 const int nMonths
= MonthCal_GetMonthRange(GetHwnd(), GMR_DAYSTATE
, NULL
);
334 wxCHECK_RET( nMonths
<= (int)WXSIZEOF(states
), "unexpected months range" );
336 for ( int i
= 0; i
< nMonths
; i
++ )
339 if ( !MonthCal_SetDayState(GetHwnd(), nMonths
, states
) )
341 wxLogLastError(_T("MonthCal_SetDayState"));
345 void wxCalendarCtrl::UpdateFirstDayOfWeek()
347 MonthCal_SetFirstDayOfWeek(GetHwnd(), HasFlag(wxCAL_MONDAY_FIRST
) ? 0 : 6);
350 // ----------------------------------------------------------------------------
351 // wxCalendarCtrl events
352 // ----------------------------------------------------------------------------
354 bool wxCalendarCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
356 NMHDR
* hdr
= (NMHDR
*)lParam
;
361 // we need to update m_date first, before calling the user code
362 // which expects GetDate() to return the new date
363 const wxDateTime dateOld
= m_date
;
364 const NMSELCHANGE
* const sch
= (NMSELCHANGE
*)lParam
;
365 m_date
.SetFromMSWSysTime(sch
->stSelStart
);
367 // changing the year or the month results in a second dummy
368 // MCN_SELCHANGE event on this system which doesn't really
369 // change anything -- filter it out
370 if ( m_date
!= dateOld
)
372 GenerateAllChangeEvents(dateOld
);
377 case MCN_GETDAYSTATE
:
379 const NMDAYSTATE
* const ds
= (NMDAYSTATE
*)lParam
;
380 for ( int i
= 0; i
< ds
->cDayState
; i
++ )
382 ds
->prgDayState
[i
] = m_marks
;
388 return wxCalendarCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
395 void wxCalendarCtrl::MSWOnDoubleClick(wxMouseEvent
& event
)
397 if ( HitTest(event
.GetPosition()) == wxCAL_HITTEST_DAY
)
399 if ( GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
) )
400 return; // skip event.Skip() below
406 void wxCalendarCtrl::MSWOnClick(wxMouseEvent
& event
)
408 // for some reason, the control doesn't get focus on its own when the user
415 #endif // wxUSE_CALENDARCTRL