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 // ----------------------------------------------------------------------------
47 void wxCalendarCtrl
::Init()
54 wxCalendarCtrl
::Create(wxWindow
*parent
,
62 if ( !wxMSWDateControls
::CheckInitialization() )
65 // we need the arrows for the navigation
66 style
|= wxWANTS_CHARS
;
68 // initialize the base class
69 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
72 // create the native control: this is a bit tricky as we want to receive
73 // double click events but the MONTHCAL_CLASS doesn't use CS_DBLCLKS style
74 // and so we create our own copy of it which does
75 static ClassRegistrar s_clsMonthCal
;
76 if ( !s_clsMonthCal
.IsInitialized() )
78 // get a copy of standard class and modify it
80 if ( ::GetClassInfo(NULL
, MONTHCAL_CLASS
, &wc
) )
82 wc
.lpszClassName
= wxT("_wx_SysMonthCtl32");
83 wc
.style
|= CS_DBLCLKS
;
84 s_clsMonthCal
.Register(wc
);
88 wxLogLastError(wxT("GetClassInfoEx(SysMonthCal32)"));
92 const wxChar
* const clsname
= s_clsMonthCal
.IsRegistered()
93 ? s_clsMonthCal
.GetName().wx_str()
96 if ( !MSWCreateControl(clsname
, wxEmptyString
, pos
, size
) )
99 // initialize the control
100 UpdateFirstDayOfWeek();
102 SetDate(dt
.IsValid() ? dt
: wxDateTime
::Today());
104 if ( SetHolidayAttrs() )
107 Connect(wxEVT_LEFT_DOWN
,
108 wxMouseEventHandler(wxCalendarCtrl
::MSWOnClick
));
109 Connect(wxEVT_LEFT_DCLICK
,
110 wxMouseEventHandler(wxCalendarCtrl
::MSWOnDoubleClick
));
115 WXDWORD wxCalendarCtrl
::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
117 WXDWORD styleMSW
= wxCalendarCtrlBase
::MSWGetStyle(style
, exstyle
);
119 // right now we don't support all native styles but we should add wx styles
120 // corresponding to MCS_NOTODAY and MCS_NOTODAYCIRCLE probably (TODO)
122 // for compatibility with the other versions, just turn off today display
123 // unconditionally for now
124 styleMSW
|= MCS_NOTODAY
;
126 // we also need this style for Mark() to work
127 styleMSW
|= MCS_DAYSTATE
;
129 if ( style
& wxCAL_SHOW_WEEK_NUMBERS
)
130 styleMSW
|= MCS_WEEKNUMBERS
;
135 void wxCalendarCtrl
::SetWindowStyleFlag(long style
)
137 const bool hadMondayFirst
= HasFlag(wxCAL_MONDAY_FIRST
);
139 wxCalendarCtrlBase
::SetWindowStyleFlag(style
);
141 if ( HasFlag(wxCAL_MONDAY_FIRST
) != hadMondayFirst
)
142 UpdateFirstDayOfWeek();
145 // ----------------------------------------------------------------------------
146 // wxCalendarCtrl geometry
147 // ----------------------------------------------------------------------------
149 // TODO: handle WM_WININICHANGE
150 wxSize wxCalendarCtrl
::DoGetBestSize() const
153 if ( !GetHwnd() || !MonthCal_GetMinReqRect(GetHwnd(), &rc
) )
155 return wxCalendarCtrlBase
::DoGetBestSize();
158 const wxSize best
= wxRectFromRECT(rc
).GetSize() + GetWindowBorderSize();
163 wxCalendarHitTestResult
164 wxCalendarCtrl
::HitTest(const wxPoint
& pos
,
166 wxDateTime
::WeekDay
*wd
)
168 WinStruct
<MCHITTESTINFO
> hti
;
170 // Vista and later SDKs add a few extra fields to MCHITTESTINFO which are
171 // not supported by the previous versions, as we don't use them anyhow we
172 // should pretend that we always use the old struct format to make the call
173 // below work on pre-Vista systems (see #11057)
174 #ifdef MCHITTESTINFO_V1_SIZE
175 hti
.cbSize
= MCHITTESTINFO_V1_SIZE
;
180 switch ( MonthCal_HitTest(GetHwnd(), &hti
) )
183 case MCHT_CALENDARWEEKNUM
:
184 wxFAIL_MSG( "unexpected" );
188 case MCHT_CALENDARBK
:
190 case MCHT_TITLEMONTH
:
192 return wxCAL_HITTEST_NOWHERE
;
194 case MCHT_CALENDARDATE
:
196 date
->SetFromMSWSysTime(hti
.st
);
197 return wxCAL_HITTEST_DAY
;
199 case MCHT_CALENDARDAY
:
202 *wd
= static_cast<wxDateTime
::WeekDay
>(hti
.st
.wDayOfWeek
);
204 return wxCAL_HITTEST_HEADER
;
206 case MCHT_TITLEBTNNEXT
:
207 return wxCAL_HITTEST_INCMONTH
;
209 case MCHT_TITLEBTNPREV
:
210 return wxCAL_HITTEST_DECMONTH
;
212 case MCHT_CALENDARDATENEXT
:
213 case MCHT_CALENDARDATEPREV
:
214 return wxCAL_HITTEST_SURROUNDING_WEEK
;
218 // ----------------------------------------------------------------------------
219 // wxCalendarCtrl operations
220 // ----------------------------------------------------------------------------
222 bool wxCalendarCtrl
::SetDate(const wxDateTime
& dt
)
224 wxCHECK_MSG( dt
.IsValid(), false, "invalid date" );
227 dt
.GetAsMSWSysTime(&st
);
228 if ( !MonthCal_SetCurSel(GetHwnd(), &st
) )
230 wxLogDebug(wxT("DateTime_SetSystemtime() failed"));
240 wxDateTime wxCalendarCtrl
::GetDate() const
244 if ( !MonthCal_GetCurSel(GetHwnd(), &st
) )
246 wxASSERT_MSG( !m_date
.IsValid(), "mismatch between data and control" );
248 return wxDefaultDateTime
;
253 wxASSERT_MSG( dt
== m_date
, "mismatch between data and control" );
254 #endif // wxDEBUG_LEVEL
259 bool wxCalendarCtrl
::SetDateRange(const wxDateTime
& dt1
, const wxDateTime
& dt2
)
266 dt1
.GetAsMSWSysTime(st
+ 0);
272 dt2
.GetAsMSWSysTime(st
+ 1);
276 if ( !MonthCal_SetRange(GetHwnd(), flags
, st
) )
278 wxLogDebug(wxT("MonthCal_SetRange() failed"));
284 bool wxCalendarCtrl
::GetDateRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
288 DWORD flags
= MonthCal_GetRange(GetHwnd(), st
);
291 if ( flags
& GDTR_MIN
)
292 dt1
->SetFromMSWSysTime(st
[0]);
294 *dt1
= wxDefaultDateTime
;
299 if ( flags
& GDTR_MAX
)
300 dt2
->SetFromMSWSysTime(st
[1]);
302 *dt2
= wxDefaultDateTime
;
308 // ----------------------------------------------------------------------------
309 // other wxCalendarCtrl operations
310 // ----------------------------------------------------------------------------
312 bool wxCalendarCtrl
::EnableMonthChange(bool enable
)
314 if ( !wxCalendarCtrlBase
::EnableMonthChange(enable
) )
317 wxDateTime dtStart
, dtEnd
;
323 dtEnd
= dtStart
.GetLastMonthDay();
325 //else: leave them invalid to remove the restriction
327 SetDateRange(dtStart
, dtEnd
);
332 void wxCalendarCtrl
::Mark(size_t day
, bool mark
)
334 wxCHECK_RET( day
> 0 && day
< 32, "invalid day" );
336 int mask
= 1 << (day
- 1);
342 // calling Refresh() here is not enough to change the day appearance
346 void wxCalendarCtrl
::SetHoliday(size_t day
)
348 wxCHECK_RET( day
> 0 && day
< 32, "invalid day" );
350 m_holidays
|= 1 << (day
- 1);
353 void wxCalendarCtrl
::UpdateMarks()
355 // we show only one full month but there can be some days from the month
356 // before it and from the one after it so days from 3 different months can
357 // be partially shown
358 MONTHDAYSTATE states
[3] = { 0 };
359 const int nMonths
= MonthCal_GetMonthRange(GetHwnd(), GMR_DAYSTATE
, NULL
);
361 // although in principle the calendar might not show any days from the
362 // preceding months, it seems like it always does, consider e.g. Feb 2010
363 // which starts on Monday and ends on Sunday and so could fit on 4 lines
364 // without showing any subsequent months -- the standard control still
365 // shows it on 6 lines and the number of visible months is still 3
366 wxCHECK_RET( nMonths
== (int)WXSIZEOF(states
), "unexpected months range" );
368 // the fully visible month is the one in the middle
369 states
[1] = m_marks
| m_holidays
;
371 if ( !MonthCal_SetDayState(GetHwnd(), nMonths
, states
) )
373 wxLogLastError(wxT("MonthCal_SetDayState"));
377 void wxCalendarCtrl
::UpdateFirstDayOfWeek()
379 MonthCal_SetFirstDayOfWeek(GetHwnd(), HasFlag(wxCAL_MONDAY_FIRST
) ?
0 : 6);
382 // ----------------------------------------------------------------------------
383 // wxCalendarCtrl events
384 // ----------------------------------------------------------------------------
386 bool wxCalendarCtrl
::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
388 NMHDR
* hdr
= (NMHDR
*)lParam
;
393 // we need to update m_date first, before calling the user code
394 // which expects GetDate() to return the new date
395 const wxDateTime dateOld
= m_date
;
396 const NMSELCHANGE
* const sch
= (NMSELCHANGE
*)lParam
;
397 m_date
.SetFromMSWSysTime(sch
->stSelStart
);
399 // changing the year or the month results in a second dummy
400 // MCN_SELCHANGE event on this system which doesn't really
401 // change anything -- filter it out
402 if ( m_date
!= dateOld
)
404 if ( GenerateAllChangeEvents(dateOld
) )
406 // month changed, need to update the holidays if we use
408 if ( SetHolidayAttrs() )
415 case MCN_GETDAYSTATE
:
417 const NMDAYSTATE
* const ds
= (NMDAYSTATE
*)lParam
;
418 for ( int i
= 0; i
< ds
->cDayState
; i
++ )
420 ds
->prgDayState
[i
] = m_marks
| m_holidays
;
426 return wxCalendarCtrlBase
::MSWOnNotify(idCtrl
, lParam
, result
);
433 void wxCalendarCtrl
::MSWOnDoubleClick(wxMouseEvent
& event
)
435 if ( HitTest(event
.GetPosition()) == wxCAL_HITTEST_DAY
)
437 if ( GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
) )
438 return; // skip event.Skip() below
444 void wxCalendarCtrl
::MSWOnClick(wxMouseEvent
& event
)
446 // for some reason, the control doesn't get focus on its own when the user
453 #endif // wxUSE_CALENDARCTRL