Always use MCHITTESTINFO of minimal size.
[wxWidgets.git] / src / msw / calctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/calctrl.cpp
3 // Purpose: wxCalendarCtrl implementation
4 // Author: Vadim Zeitlin
5 // Created: 2008-04-04
6 // RCS-ID: $Id$
7 // Copyright: (C) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #if wxUSE_CALENDARCTRL
26
27 #ifndef WX_PRECOMP
28 #include "wx/msw/wrapwin.h"
29 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
30 #include "wx/msw/private.h"
31 #endif
32
33 #include "wx/calctrl.h"
34
35 #include "wx/msw/private/datecontrols.h"
36
37 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl, wxControl)
38
39 // ============================================================================
40 // implementation
41 // ============================================================================
42
43 // ----------------------------------------------------------------------------
44 // wxCalendarCtrl creation
45 // ----------------------------------------------------------------------------
46
47 void wxCalendarCtrl::Init()
48 {
49 m_marks =
50 m_holidays = 0;
51 }
52
53 bool
54 wxCalendarCtrl::Create(wxWindow *parent,
55 wxWindowID id,
56 const wxDateTime& dt,
57 const wxPoint& pos,
58 const wxSize& size,
59 long style,
60 const wxString& name)
61 {
62 if ( !wxMSWDateControls::CheckInitialization() )
63 return false;
64
65 // we need the arrows for the navigation
66 style |= wxWANTS_CHARS;
67
68 // initialize the base class
69 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
70 return false;
71
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() )
77 {
78 // get a copy of standard class and modify it
79 WNDCLASS wc;
80 if ( ::GetClassInfo(NULL, MONTHCAL_CLASS, &wc) )
81 {
82 wc.lpszClassName = wxT("_wx_SysMonthCtl32");
83 wc.style |= CS_DBLCLKS;
84 s_clsMonthCal.Register(wc);
85 }
86 else
87 {
88 wxLogLastError(wxT("GetClassInfoEx(SysMonthCal32)"));
89 }
90 }
91
92 const wxChar * const clsname = s_clsMonthCal.IsRegistered()
93 ? s_clsMonthCal.GetName().wx_str()
94 : MONTHCAL_CLASS;
95
96 if ( !MSWCreateControl(clsname, wxEmptyString, pos, size) )
97 return false;
98
99 // initialize the control
100 UpdateFirstDayOfWeek();
101
102 SetDate(dt.IsValid() ? dt : wxDateTime::Today());
103
104 if ( SetHolidayAttrs() )
105 UpdateMarks();
106
107 Connect(wxEVT_LEFT_DOWN,
108 wxMouseEventHandler(wxCalendarCtrl::MSWOnClick));
109 Connect(wxEVT_LEFT_DCLICK,
110 wxMouseEventHandler(wxCalendarCtrl::MSWOnDoubleClick));
111
112 return true;
113 }
114
115 WXDWORD wxCalendarCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
116 {
117 WXDWORD styleMSW = wxCalendarCtrlBase::MSWGetStyle(style, exstyle);
118
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)
121
122 // for compatibility with the other versions, just turn off today display
123 // unconditionally for now
124 styleMSW |= MCS_NOTODAY;
125
126 // we also need this style for Mark() to work
127 styleMSW |= MCS_DAYSTATE;
128
129 if ( style & wxCAL_SHOW_WEEK_NUMBERS )
130 styleMSW |= MCS_WEEKNUMBERS;
131
132 return styleMSW;
133 }
134
135 void wxCalendarCtrl::SetWindowStyleFlag(long style)
136 {
137 const bool hadMondayFirst = HasFlag(wxCAL_MONDAY_FIRST);
138
139 wxCalendarCtrlBase::SetWindowStyleFlag(style);
140
141 if ( HasFlag(wxCAL_MONDAY_FIRST) != hadMondayFirst )
142 UpdateFirstDayOfWeek();
143 }
144
145 // ----------------------------------------------------------------------------
146 // wxCalendarCtrl geometry
147 // ----------------------------------------------------------------------------
148
149 // TODO: handle WM_WININICHANGE
150 wxSize wxCalendarCtrl::DoGetBestSize() const
151 {
152 RECT rc;
153 if ( !GetHwnd() || !MonthCal_GetMinReqRect(GetHwnd(), &rc) )
154 {
155 return wxCalendarCtrlBase::DoGetBestSize();
156 }
157
158 const wxSize best = wxRectFromRECT(rc).GetSize() + GetWindowBorderSize();
159 CacheBestSize(best);
160 return best;
161 }
162
163 wxCalendarHitTestResult
164 wxCalendarCtrl::HitTest(const wxPoint& pos,
165 wxDateTime *date,
166 wxDateTime::WeekDay *wd)
167 {
168 WinStruct<MCHITTESTINFO> hti;
169
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;
176 #endif
177
178 hti.pt.x = pos.x;
179 hti.pt.y = pos.y;
180 switch ( MonthCal_HitTest(GetHwnd(), &hti) )
181 {
182 default:
183 case MCHT_CALENDARWEEKNUM:
184 wxFAIL_MSG( "unexpected" );
185 // fall through
186
187 case MCHT_NOWHERE:
188 case MCHT_CALENDARBK:
189 case MCHT_TITLEBK:
190 case MCHT_TITLEMONTH:
191 case MCHT_TITLEYEAR:
192 return wxCAL_HITTEST_NOWHERE;
193
194 case MCHT_CALENDARDATE:
195 if ( date )
196 date->SetFromMSWSysTime(hti.st);
197 return wxCAL_HITTEST_DAY;
198
199 case MCHT_CALENDARDAY:
200 if ( wd )
201 {
202 *wd = static_cast<wxDateTime::WeekDay>(hti.st.wDayOfWeek);
203 }
204 return wxCAL_HITTEST_HEADER;
205
206 case MCHT_TITLEBTNNEXT:
207 return wxCAL_HITTEST_INCMONTH;
208
209 case MCHT_TITLEBTNPREV:
210 return wxCAL_HITTEST_DECMONTH;
211
212 case MCHT_CALENDARDATENEXT:
213 case MCHT_CALENDARDATEPREV:
214 return wxCAL_HITTEST_SURROUNDING_WEEK;
215 }
216 }
217
218 // ----------------------------------------------------------------------------
219 // wxCalendarCtrl operations
220 // ----------------------------------------------------------------------------
221
222 bool wxCalendarCtrl::SetDate(const wxDateTime& dt)
223 {
224 wxCHECK_MSG( dt.IsValid(), false, "invalid date" );
225
226 SYSTEMTIME st;
227 dt.GetAsMSWSysTime(&st);
228 if ( !MonthCal_SetCurSel(GetHwnd(), &st) )
229 {
230 wxLogDebug(wxT("DateTime_SetSystemtime() failed"));
231
232 return false;
233 }
234
235 m_date = dt;
236
237 return true;
238 }
239
240 wxDateTime wxCalendarCtrl::GetDate() const
241 {
242 #if wxDEBUG_LEVEL
243 SYSTEMTIME st;
244 if ( !MonthCal_GetCurSel(GetHwnd(), &st) )
245 {
246 wxASSERT_MSG( !m_date.IsValid(), "mismatch between data and control" );
247
248 return wxDefaultDateTime;
249 }
250
251 wxDateTime dt(st);
252
253 wxASSERT_MSG( dt == m_date, "mismatch between data and control" );
254 #endif // wxDEBUG_LEVEL
255
256 return m_date;
257 }
258
259 bool wxCalendarCtrl::SetDateRange(const wxDateTime& dt1, const wxDateTime& dt2)
260 {
261 SYSTEMTIME st[2];
262
263 DWORD flags = 0;
264 if ( dt1.IsValid() )
265 {
266 dt1.GetAsMSWSysTime(st + 0);
267 flags |= GDTR_MIN;
268 }
269
270 if ( dt2.IsValid() )
271 {
272 dt2.GetAsMSWSysTime(st + 1);
273 flags |= GDTR_MAX;
274 }
275
276 if ( !MonthCal_SetRange(GetHwnd(), flags, st) )
277 {
278 wxLogDebug(wxT("MonthCal_SetRange() failed"));
279 }
280
281 return flags != 0;
282 }
283
284 bool wxCalendarCtrl::GetDateRange(wxDateTime *dt1, wxDateTime *dt2) const
285 {
286 SYSTEMTIME st[2];
287
288 DWORD flags = MonthCal_GetRange(GetHwnd(), st);
289 if ( dt1 )
290 {
291 if ( flags & GDTR_MIN )
292 dt1->SetFromMSWSysTime(st[0]);
293 else
294 *dt1 = wxDefaultDateTime;
295 }
296
297 if ( dt2 )
298 {
299 if ( flags & GDTR_MAX )
300 dt2->SetFromMSWSysTime(st[1]);
301 else
302 *dt2 = wxDefaultDateTime;
303 }
304
305 return flags != 0;
306 }
307
308 // ----------------------------------------------------------------------------
309 // other wxCalendarCtrl operations
310 // ----------------------------------------------------------------------------
311
312 bool wxCalendarCtrl::EnableMonthChange(bool enable)
313 {
314 if ( !wxCalendarCtrlBase::EnableMonthChange(enable) )
315 return false;
316
317 wxDateTime dtStart, dtEnd;
318 if ( !enable )
319 {
320 dtStart = GetDate();
321 dtStart.SetDay(1);
322
323 dtEnd = dtStart.GetLastMonthDay();
324 }
325 //else: leave them invalid to remove the restriction
326
327 SetDateRange(dtStart, dtEnd);
328
329 return true;
330 }
331
332 void wxCalendarCtrl::Mark(size_t day, bool mark)
333 {
334 wxCHECK_RET( day > 0 && day < 32, "invalid day" );
335
336 int mask = 1 << (day - 1);
337 if ( mark )
338 m_marks |= mask;
339 else
340 m_marks &= ~mask;
341
342 // calling Refresh() here is not enough to change the day appearance
343 UpdateMarks();
344 }
345
346 void wxCalendarCtrl::SetHoliday(size_t day)
347 {
348 wxCHECK_RET( day > 0 && day < 32, "invalid day" );
349
350 m_holidays |= 1 << (day - 1);
351 }
352
353 void wxCalendarCtrl::UpdateMarks()
354 {
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);
360
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" );
367
368 // the fully visible month is the one in the middle
369 states[1] = m_marks | m_holidays;
370
371 if ( !MonthCal_SetDayState(GetHwnd(), nMonths, states) )
372 {
373 wxLogLastError(wxT("MonthCal_SetDayState"));
374 }
375 }
376
377 void wxCalendarCtrl::UpdateFirstDayOfWeek()
378 {
379 MonthCal_SetFirstDayOfWeek(GetHwnd(), HasFlag(wxCAL_MONDAY_FIRST) ? 0 : 6);
380 }
381
382 // ----------------------------------------------------------------------------
383 // wxCalendarCtrl events
384 // ----------------------------------------------------------------------------
385
386 bool wxCalendarCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
387 {
388 NMHDR* hdr = (NMHDR *)lParam;
389 switch ( hdr->code )
390 {
391 case MCN_SELCHANGE:
392 {
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);
398
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 )
403 {
404 if ( GenerateAllChangeEvents(dateOld) )
405 {
406 // month changed, need to update the holidays if we use
407 // them
408 if ( SetHolidayAttrs() )
409 UpdateMarks();
410 }
411 }
412 }
413 break;
414
415 case MCN_GETDAYSTATE:
416 {
417 const NMDAYSTATE * const ds = (NMDAYSTATE *)lParam;
418 for ( int i = 0; i < ds->cDayState; i++ )
419 {
420 ds->prgDayState[i] = m_marks | m_holidays;
421 }
422 }
423 break;
424
425 default:
426 return wxCalendarCtrlBase::MSWOnNotify(idCtrl, lParam, result);
427 }
428
429 *result = 0;
430 return true;
431 }
432
433 void wxCalendarCtrl::MSWOnDoubleClick(wxMouseEvent& event)
434 {
435 if ( HitTest(event.GetPosition()) == wxCAL_HITTEST_DAY )
436 {
437 if ( GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED) )
438 return; // skip event.Skip() below
439 }
440
441 event.Skip();
442 }
443
444 void wxCalendarCtrl::MSWOnClick(wxMouseEvent& event)
445 {
446 // for some reason, the control doesn't get focus on its own when the user
447 // clicks in it
448 SetFocus();
449
450 event.Skip();
451 }
452
453 #endif // wxUSE_CALENDARCTRL