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(_T("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
;
171 switch ( MonthCal_HitTest(GetHwnd(), &hti
) )
174 case MCHT_CALENDARWEEKNUM
:
175 wxFAIL_MSG( "unexpected" );
179 case MCHT_CALENDARBK
:
181 case MCHT_TITLEMONTH
:
183 return wxCAL_HITTEST_NOWHERE
;
185 case MCHT_CALENDARDATE
:
187 date
->SetFromMSWSysTime(hti
.st
);
188 return wxCAL_HITTEST_DAY
;
190 case MCHT_CALENDARDAY
:
193 *wd
= wx_static_cast(wxDateTime::WeekDay
, hti
.st
.wDayOfWeek
);
195 return wxCAL_HITTEST_HEADER
;
197 case MCHT_TITLEBTNNEXT
:
198 return wxCAL_HITTEST_INCMONTH
;
200 case MCHT_TITLEBTNPREV
:
201 return wxCAL_HITTEST_DECMONTH
;
203 case MCHT_CALENDARDATENEXT
:
204 case MCHT_CALENDARDATEPREV
:
205 return wxCAL_HITTEST_SURROUNDING_WEEK
;
209 // ----------------------------------------------------------------------------
210 // wxCalendarCtrl operations
211 // ----------------------------------------------------------------------------
213 bool wxCalendarCtrl::SetDate(const wxDateTime
& dt
)
215 wxCHECK_MSG( dt
.IsValid(), false, "invalid date" );
218 dt
.GetAsMSWSysTime(&st
);
219 if ( !MonthCal_SetCurSel(GetHwnd(), &st
) )
221 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
231 wxDateTime
wxCalendarCtrl::GetDate() const
235 if ( !MonthCal_GetCurSel(GetHwnd(), &st
) )
237 wxASSERT_MSG( !m_date
.IsValid(), "mismatch between data and control" );
239 return wxDefaultDateTime
;
244 wxASSERT_MSG( dt
== m_date
, "mismatch between data and control" );
245 #endif // __WXDEBUG__
250 bool wxCalendarCtrl::SetDateRange(const wxDateTime
& dt1
, const wxDateTime
& dt2
)
257 dt1
.GetAsMSWSysTime(st
+ 0);
263 dt2
.GetAsMSWSysTime(st
+ 1);
267 if ( !MonthCal_SetRange(GetHwnd(), flags
, st
) )
269 wxLogDebug(_T("MonthCal_SetRange() failed"));
275 bool wxCalendarCtrl::GetDateRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
279 DWORD flags
= MonthCal_GetRange(GetHwnd(), st
);
282 if ( flags
& GDTR_MIN
)
283 dt1
->SetFromMSWSysTime(st
[0]);
285 *dt1
= wxDefaultDateTime
;
290 if ( flags
& GDTR_MAX
)
291 dt2
->SetFromMSWSysTime(st
[1]);
293 *dt2
= wxDefaultDateTime
;
299 // ----------------------------------------------------------------------------
300 // other wxCalendarCtrl operations
301 // ----------------------------------------------------------------------------
303 bool wxCalendarCtrl::EnableMonthChange(bool enable
)
305 if ( !wxCalendarCtrlBase::EnableMonthChange(enable
) )
308 wxDateTime dtStart
, dtEnd
;
314 dtEnd
= dtStart
.GetLastMonthDay();
316 //else: leave them invalid to remove the restriction
318 SetDateRange(dtStart
, dtEnd
);
323 void wxCalendarCtrl::Mark(size_t day
, bool mark
)
325 wxCHECK_RET( day
> 0 && day
< 32, "invalid day" );
327 int mask
= 1 << (day
- 1);
333 // calling Refresh() here is not enough to change the day appearance
337 void wxCalendarCtrl::SetHoliday(size_t day
)
339 wxCHECK_RET( day
> 0 && day
< 32, "invalid day" );
341 m_holidays
|= 1 << (day
- 1);
344 void wxCalendarCtrl::UpdateMarks()
346 // we show only one full month but there can be some days from the month
347 // before it and from the one after it so days from 3 different months can
348 // be partially shown
349 MONTHDAYSTATE states
[3] = { 0 };
350 const int nMonths
= MonthCal_GetMonthRange(GetHwnd(), GMR_DAYSTATE
, NULL
);
352 // although in principle the calendar might not show any days from the
353 // preceding months, it seems like it always does, consider e.g. Feb 2010
354 // which starts on Monday and ends on Sunday and so could fit on 4 lines
355 // without showing any subsequent months -- the standard control still
356 // shows it on 6 lines and the number of visible months is still 3
357 wxCHECK_RET( nMonths
== (int)WXSIZEOF(states
), "unexpected months range" );
359 // the fully visible month is the one in the middle
360 states
[1] = m_marks
| m_holidays
;
362 if ( !MonthCal_SetDayState(GetHwnd(), nMonths
, states
) )
364 wxLogLastError(_T("MonthCal_SetDayState"));
368 void wxCalendarCtrl::UpdateFirstDayOfWeek()
370 MonthCal_SetFirstDayOfWeek(GetHwnd(), HasFlag(wxCAL_MONDAY_FIRST
) ? 0 : 6);
373 // ----------------------------------------------------------------------------
374 // wxCalendarCtrl events
375 // ----------------------------------------------------------------------------
377 bool wxCalendarCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
379 NMHDR
* hdr
= (NMHDR
*)lParam
;
384 // we need to update m_date first, before calling the user code
385 // which expects GetDate() to return the new date
386 const wxDateTime dateOld
= m_date
;
387 const NMSELCHANGE
* const sch
= (NMSELCHANGE
*)lParam
;
388 m_date
.SetFromMSWSysTime(sch
->stSelStart
);
390 // changing the year or the month results in a second dummy
391 // MCN_SELCHANGE event on this system which doesn't really
392 // change anything -- filter it out
393 if ( m_date
!= dateOld
)
395 if ( GenerateAllChangeEvents(dateOld
) )
397 // month changed, need to update the holidays if we use
399 if ( SetHolidayAttrs() )
406 case MCN_GETDAYSTATE
:
408 const NMDAYSTATE
* const ds
= (NMDAYSTATE
*)lParam
;
409 for ( int i
= 0; i
< ds
->cDayState
; i
++ )
411 ds
->prgDayState
[i
] = m_marks
| m_holidays
;
417 return wxCalendarCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
424 void wxCalendarCtrl::MSWOnDoubleClick(wxMouseEvent
& event
)
426 if ( HitTest(event
.GetPosition()) == wxCAL_HITTEST_DAY
)
428 if ( GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED
) )
429 return; // skip event.Skip() below
435 void wxCalendarCtrl::MSWOnClick(wxMouseEvent
& event
)
437 // for some reason, the control doesn't get focus on its own when the user
444 #endif // wxUSE_CALENDARCTRL