Added symbolic names for native control week days.
[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 // constants
41 // ----------------------------------------------------------------------------
42
43 namespace
44 {
45
46 // values of week days used by the native control
47 enum
48 {
49 MonthCal_Monday,
50 MonthCal_Tuesday,
51 MonthCal_Wednesday,
52 MonthCal_Thursday,
53 MonthCal_Friday,
54 MonthCal_Saturday,
55 MonthCal_Sunday
56 };
57
58 } // anonymous namespace
59
60 // ============================================================================
61 // implementation
62 // ============================================================================
63
64 // ----------------------------------------------------------------------------
65 // wxCalendarCtrl creation
66 // ----------------------------------------------------------------------------
67
68 void wxCalendarCtrl::Init()
69 {
70 m_marks =
71 m_holidays = 0;
72 }
73
74 bool
75 wxCalendarCtrl::Create(wxWindow *parent,
76 wxWindowID id,
77 const wxDateTime& dt,
78 const wxPoint& pos,
79 const wxSize& size,
80 long style,
81 const wxString& name)
82 {
83 if ( !wxMSWDateControls::CheckInitialization() )
84 return false;
85
86 // we need the arrows for the navigation
87 style |= wxWANTS_CHARS;
88
89 // initialize the base class
90 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
91 return false;
92
93 // create the native control: this is a bit tricky as we want to receive
94 // double click events but the MONTHCAL_CLASS doesn't use CS_DBLCLKS style
95 // and so we create our own copy of it which does
96 static ClassRegistrar s_clsMonthCal;
97 if ( !s_clsMonthCal.IsInitialized() )
98 {
99 // get a copy of standard class and modify it
100 WNDCLASS wc;
101 if ( ::GetClassInfo(NULL, MONTHCAL_CLASS, &wc) )
102 {
103 wc.lpszClassName = wxT("_wx_SysMonthCtl32");
104 wc.style |= CS_DBLCLKS;
105 s_clsMonthCal.Register(wc);
106 }
107 else
108 {
109 wxLogLastError(wxT("GetClassInfoEx(SysMonthCal32)"));
110 }
111 }
112
113 const wxChar * const clsname = s_clsMonthCal.IsRegistered()
114 ? s_clsMonthCal.GetName().wx_str()
115 : MONTHCAL_CLASS;
116
117 if ( !MSWCreateControl(clsname, wxEmptyString, pos, size) )
118 return false;
119
120 // initialize the control
121 UpdateFirstDayOfWeek();
122
123 SetDate(dt.IsValid() ? dt : wxDateTime::Today());
124
125 if ( SetHolidayAttrs() )
126 UpdateMarks();
127
128 Connect(wxEVT_LEFT_DOWN,
129 wxMouseEventHandler(wxCalendarCtrl::MSWOnClick));
130 Connect(wxEVT_LEFT_DCLICK,
131 wxMouseEventHandler(wxCalendarCtrl::MSWOnDoubleClick));
132
133 return true;
134 }
135
136 WXDWORD wxCalendarCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
137 {
138 WXDWORD styleMSW = wxCalendarCtrlBase::MSWGetStyle(style, exstyle);
139
140 // right now we don't support all native styles but we should add wx styles
141 // corresponding to MCS_NOTODAY and MCS_NOTODAYCIRCLE probably (TODO)
142
143 // for compatibility with the other versions, just turn off today display
144 // unconditionally for now
145 styleMSW |= MCS_NOTODAY;
146
147 // we also need this style for Mark() to work
148 styleMSW |= MCS_DAYSTATE;
149
150 if ( style & wxCAL_SHOW_WEEK_NUMBERS )
151 styleMSW |= MCS_WEEKNUMBERS;
152
153 return styleMSW;
154 }
155
156 void wxCalendarCtrl::SetWindowStyleFlag(long style)
157 {
158 const bool hadMondayFirst = HasFlag(wxCAL_MONDAY_FIRST);
159
160 wxCalendarCtrlBase::SetWindowStyleFlag(style);
161
162 if ( HasFlag(wxCAL_MONDAY_FIRST) != hadMondayFirst )
163 UpdateFirstDayOfWeek();
164 }
165
166 // ----------------------------------------------------------------------------
167 // wxCalendarCtrl geometry
168 // ----------------------------------------------------------------------------
169
170 // TODO: handle WM_WININICHANGE
171 wxSize wxCalendarCtrl::DoGetBestSize() const
172 {
173 RECT rc;
174 if ( !GetHwnd() || !MonthCal_GetMinReqRect(GetHwnd(), &rc) )
175 {
176 return wxCalendarCtrlBase::DoGetBestSize();
177 }
178
179 const wxSize best = wxRectFromRECT(rc).GetSize() + GetWindowBorderSize();
180 CacheBestSize(best);
181 return best;
182 }
183
184 wxCalendarHitTestResult
185 wxCalendarCtrl::HitTest(const wxPoint& pos,
186 wxDateTime *date,
187 wxDateTime::WeekDay *wd)
188 {
189 WinStruct<MCHITTESTINFO> hti;
190
191 // Vista and later SDKs add a few extra fields to MCHITTESTINFO which are
192 // not supported by the previous versions, as we don't use them anyhow we
193 // should pretend that we always use the old struct format to make the call
194 // below work on pre-Vista systems (see #11057)
195 #ifdef MCHITTESTINFO_V1_SIZE
196 hti.cbSize = MCHITTESTINFO_V1_SIZE;
197 #endif
198
199 hti.pt.x = pos.x;
200 hti.pt.y = pos.y;
201 switch ( MonthCal_HitTest(GetHwnd(), &hti) )
202 {
203 default:
204 case MCHT_CALENDARWEEKNUM:
205 wxFAIL_MSG( "unexpected" );
206 // fall through
207
208 case MCHT_NOWHERE:
209 case MCHT_CALENDARBK:
210 case MCHT_TITLEBK:
211 case MCHT_TITLEMONTH:
212 case MCHT_TITLEYEAR:
213 return wxCAL_HITTEST_NOWHERE;
214
215 case MCHT_CALENDARDATE:
216 if ( date )
217 date->SetFromMSWSysTime(hti.st);
218 return wxCAL_HITTEST_DAY;
219
220 case MCHT_CALENDARDAY:
221 if ( wd )
222 {
223 *wd = static_cast<wxDateTime::WeekDay>(hti.st.wDayOfWeek);
224 }
225 return wxCAL_HITTEST_HEADER;
226
227 case MCHT_TITLEBTNNEXT:
228 return wxCAL_HITTEST_INCMONTH;
229
230 case MCHT_TITLEBTNPREV:
231 return wxCAL_HITTEST_DECMONTH;
232
233 case MCHT_CALENDARDATENEXT:
234 case MCHT_CALENDARDATEPREV:
235 return wxCAL_HITTEST_SURROUNDING_WEEK;
236 }
237 }
238
239 // ----------------------------------------------------------------------------
240 // wxCalendarCtrl operations
241 // ----------------------------------------------------------------------------
242
243 bool wxCalendarCtrl::SetDate(const wxDateTime& dt)
244 {
245 wxCHECK_MSG( dt.IsValid(), false, "invalid date" );
246
247 SYSTEMTIME st;
248 dt.GetAsMSWSysTime(&st);
249 if ( !MonthCal_SetCurSel(GetHwnd(), &st) )
250 {
251 wxLogDebug(wxT("DateTime_SetSystemtime() failed"));
252
253 return false;
254 }
255
256 m_date = dt;
257
258 return true;
259 }
260
261 wxDateTime wxCalendarCtrl::GetDate() const
262 {
263 #if wxDEBUG_LEVEL
264 SYSTEMTIME st;
265 if ( !MonthCal_GetCurSel(GetHwnd(), &st) )
266 {
267 wxASSERT_MSG( !m_date.IsValid(), "mismatch between data and control" );
268
269 return wxDefaultDateTime;
270 }
271
272 wxDateTime dt(st);
273
274 wxASSERT_MSG( dt == m_date, "mismatch between data and control" );
275 #endif // wxDEBUG_LEVEL
276
277 return m_date;
278 }
279
280 bool wxCalendarCtrl::SetDateRange(const wxDateTime& dt1, const wxDateTime& dt2)
281 {
282 SYSTEMTIME st[2];
283
284 DWORD flags = 0;
285 if ( dt1.IsValid() )
286 {
287 dt1.GetAsMSWSysTime(st + 0);
288 flags |= GDTR_MIN;
289 }
290
291 if ( dt2.IsValid() )
292 {
293 dt2.GetAsMSWSysTime(st + 1);
294 flags |= GDTR_MAX;
295 }
296
297 if ( !MonthCal_SetRange(GetHwnd(), flags, st) )
298 {
299 wxLogDebug(wxT("MonthCal_SetRange() failed"));
300 }
301
302 return flags != 0;
303 }
304
305 bool wxCalendarCtrl::GetDateRange(wxDateTime *dt1, wxDateTime *dt2) const
306 {
307 SYSTEMTIME st[2];
308
309 DWORD flags = MonthCal_GetRange(GetHwnd(), st);
310 if ( dt1 )
311 {
312 if ( flags & GDTR_MIN )
313 dt1->SetFromMSWSysTime(st[0]);
314 else
315 *dt1 = wxDefaultDateTime;
316 }
317
318 if ( dt2 )
319 {
320 if ( flags & GDTR_MAX )
321 dt2->SetFromMSWSysTime(st[1]);
322 else
323 *dt2 = wxDefaultDateTime;
324 }
325
326 return flags != 0;
327 }
328
329 // ----------------------------------------------------------------------------
330 // other wxCalendarCtrl operations
331 // ----------------------------------------------------------------------------
332
333 bool wxCalendarCtrl::EnableMonthChange(bool enable)
334 {
335 if ( !wxCalendarCtrlBase::EnableMonthChange(enable) )
336 return false;
337
338 wxDateTime dtStart, dtEnd;
339 if ( !enable )
340 {
341 dtStart = GetDate();
342 dtStart.SetDay(1);
343
344 dtEnd = dtStart.GetLastMonthDay();
345 }
346 //else: leave them invalid to remove the restriction
347
348 SetDateRange(dtStart, dtEnd);
349
350 return true;
351 }
352
353 void wxCalendarCtrl::Mark(size_t day, bool mark)
354 {
355 wxCHECK_RET( day > 0 && day < 32, "invalid day" );
356
357 int mask = 1 << (day - 1);
358 if ( mark )
359 m_marks |= mask;
360 else
361 m_marks &= ~mask;
362
363 // calling Refresh() here is not enough to change the day appearance
364 UpdateMarks();
365 }
366
367 void wxCalendarCtrl::SetHoliday(size_t day)
368 {
369 wxCHECK_RET( day > 0 && day < 32, "invalid day" );
370
371 m_holidays |= 1 << (day - 1);
372 }
373
374 void wxCalendarCtrl::UpdateMarks()
375 {
376 // we show only one full month but there can be some days from the month
377 // before it and from the one after it so days from 3 different months can
378 // be partially shown
379 MONTHDAYSTATE states[3] = { 0 };
380 const int nMonths = MonthCal_GetMonthRange(GetHwnd(), GMR_DAYSTATE, NULL);
381
382 // although in principle the calendar might not show any days from the
383 // preceding months, it seems like it always does, consider e.g. Feb 2010
384 // which starts on Monday and ends on Sunday and so could fit on 4 lines
385 // without showing any subsequent months -- the standard control still
386 // shows it on 6 lines and the number of visible months is still 3
387 wxCHECK_RET( nMonths == (int)WXSIZEOF(states), "unexpected months range" );
388
389 // the fully visible month is the one in the middle
390 states[1] = m_marks | m_holidays;
391
392 if ( !MonthCal_SetDayState(GetHwnd(), nMonths, states) )
393 {
394 wxLogLastError(wxT("MonthCal_SetDayState"));
395 }
396 }
397
398 void wxCalendarCtrl::UpdateFirstDayOfWeek()
399 {
400 MonthCal_SetFirstDayOfWeek(GetHwnd(),
401 HasFlag(wxCAL_MONDAY_FIRST) ? MonthCal_Monday
402 : MonthCal_Sunday);
403 }
404
405 // ----------------------------------------------------------------------------
406 // wxCalendarCtrl events
407 // ----------------------------------------------------------------------------
408
409 bool wxCalendarCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
410 {
411 NMHDR* hdr = (NMHDR *)lParam;
412 switch ( hdr->code )
413 {
414 case MCN_SELCHANGE:
415 {
416 // we need to update m_date first, before calling the user code
417 // which expects GetDate() to return the new date
418 const wxDateTime dateOld = m_date;
419 const NMSELCHANGE * const sch = (NMSELCHANGE *)lParam;
420 m_date.SetFromMSWSysTime(sch->stSelStart);
421
422 // changing the year or the month results in a second dummy
423 // MCN_SELCHANGE event on this system which doesn't really
424 // change anything -- filter it out
425 if ( m_date != dateOld )
426 {
427 if ( GenerateAllChangeEvents(dateOld) )
428 {
429 // month changed, need to update the holidays if we use
430 // them
431 if ( SetHolidayAttrs() )
432 UpdateMarks();
433 }
434 }
435 }
436 break;
437
438 case MCN_GETDAYSTATE:
439 {
440 const NMDAYSTATE * const ds = (NMDAYSTATE *)lParam;
441 for ( int i = 0; i < ds->cDayState; i++ )
442 {
443 ds->prgDayState[i] = m_marks | m_holidays;
444 }
445 }
446 break;
447
448 default:
449 return wxCalendarCtrlBase::MSWOnNotify(idCtrl, lParam, result);
450 }
451
452 *result = 0;
453 return true;
454 }
455
456 void wxCalendarCtrl::MSWOnDoubleClick(wxMouseEvent& event)
457 {
458 if ( HitTest(event.GetPosition()) == wxCAL_HITTEST_DAY )
459 {
460 if ( GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED) )
461 return; // skip event.Skip() below
462 }
463
464 event.Skip();
465 }
466
467 void wxCalendarCtrl::MSWOnClick(wxMouseEvent& event)
468 {
469 // for some reason, the control doesn't get focus on its own when the user
470 // clicks in it
471 SetFocus();
472
473 event.Skip();
474 }
475
476 #endif // wxUSE_CALENDARCTRL