]> git.saurik.com Git - wxWidgets.git/blob - src/msw/calctrl.cpp
fix unused parameter warning after r60715
[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(_T("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 hti.pt.x = pos.x;
170 hti.pt.y = pos.y;
171 switch ( MonthCal_HitTest(GetHwnd(), &hti) )
172 {
173 default:
174 case MCHT_CALENDARWEEKNUM:
175 wxFAIL_MSG( "unexpected" );
176 // fall through
177
178 case MCHT_NOWHERE:
179 case MCHT_CALENDARBK:
180 case MCHT_TITLEBK:
181 case MCHT_TITLEMONTH:
182 case MCHT_TITLEYEAR:
183 return wxCAL_HITTEST_NOWHERE;
184
185 case MCHT_CALENDARDATE:
186 if ( date )
187 date->SetFromMSWSysTime(hti.st);
188 return wxCAL_HITTEST_DAY;
189
190 case MCHT_CALENDARDAY:
191 if ( wd )
192 {
193 *wd = static_cast<wxDateTime::WeekDay>(hti.st.wDayOfWeek);
194 }
195 return wxCAL_HITTEST_HEADER;
196
197 case MCHT_TITLEBTNNEXT:
198 return wxCAL_HITTEST_INCMONTH;
199
200 case MCHT_TITLEBTNPREV:
201 return wxCAL_HITTEST_DECMONTH;
202
203 case MCHT_CALENDARDATENEXT:
204 case MCHT_CALENDARDATEPREV:
205 return wxCAL_HITTEST_SURROUNDING_WEEK;
206 }
207 }
208
209 // ----------------------------------------------------------------------------
210 // wxCalendarCtrl operations
211 // ----------------------------------------------------------------------------
212
213 bool wxCalendarCtrl::SetDate(const wxDateTime& dt)
214 {
215 wxCHECK_MSG( dt.IsValid(), false, "invalid date" );
216
217 SYSTEMTIME st;
218 dt.GetAsMSWSysTime(&st);
219 if ( !MonthCal_SetCurSel(GetHwnd(), &st) )
220 {
221 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
222
223 return false;
224 }
225
226 m_date = dt;
227
228 return true;
229 }
230
231 wxDateTime wxCalendarCtrl::GetDate() const
232 {
233 #if wxDEBUG_LEVEL
234 SYSTEMTIME st;
235 if ( !MonthCal_GetCurSel(GetHwnd(), &st) )
236 {
237 wxASSERT_MSG( !m_date.IsValid(), "mismatch between data and control" );
238
239 return wxDefaultDateTime;
240 }
241
242 wxDateTime dt(st);
243
244 wxASSERT_MSG( dt == m_date, "mismatch between data and control" );
245 #endif // wxDEBUG_LEVEL
246
247 return m_date;
248 }
249
250 bool wxCalendarCtrl::SetDateRange(const wxDateTime& dt1, const wxDateTime& dt2)
251 {
252 SYSTEMTIME st[2];
253
254 DWORD flags = 0;
255 if ( dt1.IsValid() )
256 {
257 dt1.GetAsMSWSysTime(st + 0);
258 flags |= GDTR_MIN;
259 }
260
261 if ( dt2.IsValid() )
262 {
263 dt2.GetAsMSWSysTime(st + 1);
264 flags |= GDTR_MAX;
265 }
266
267 if ( !MonthCal_SetRange(GetHwnd(), flags, st) )
268 {
269 wxLogDebug(_T("MonthCal_SetRange() failed"));
270 }
271
272 return flags != 0;
273 }
274
275 bool wxCalendarCtrl::GetDateRange(wxDateTime *dt1, wxDateTime *dt2) const
276 {
277 SYSTEMTIME st[2];
278
279 DWORD flags = MonthCal_GetRange(GetHwnd(), st);
280 if ( dt1 )
281 {
282 if ( flags & GDTR_MIN )
283 dt1->SetFromMSWSysTime(st[0]);
284 else
285 *dt1 = wxDefaultDateTime;
286 }
287
288 if ( dt2 )
289 {
290 if ( flags & GDTR_MAX )
291 dt2->SetFromMSWSysTime(st[1]);
292 else
293 *dt2 = wxDefaultDateTime;
294 }
295
296 return flags != 0;
297 }
298
299 // ----------------------------------------------------------------------------
300 // other wxCalendarCtrl operations
301 // ----------------------------------------------------------------------------
302
303 bool wxCalendarCtrl::EnableMonthChange(bool enable)
304 {
305 if ( !wxCalendarCtrlBase::EnableMonthChange(enable) )
306 return false;
307
308 wxDateTime dtStart, dtEnd;
309 if ( !enable )
310 {
311 dtStart = GetDate();
312 dtStart.SetDay(1);
313
314 dtEnd = dtStart.GetLastMonthDay();
315 }
316 //else: leave them invalid to remove the restriction
317
318 SetDateRange(dtStart, dtEnd);
319
320 return true;
321 }
322
323 void wxCalendarCtrl::Mark(size_t day, bool mark)
324 {
325 wxCHECK_RET( day > 0 && day < 32, "invalid day" );
326
327 int mask = 1 << (day - 1);
328 if ( mark )
329 m_marks |= mask;
330 else
331 m_marks &= ~mask;
332
333 // calling Refresh() here is not enough to change the day appearance
334 UpdateMarks();
335 }
336
337 void wxCalendarCtrl::SetHoliday(size_t day)
338 {
339 wxCHECK_RET( day > 0 && day < 32, "invalid day" );
340
341 m_holidays |= 1 << (day - 1);
342 }
343
344 void wxCalendarCtrl::UpdateMarks()
345 {
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);
351
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" );
358
359 // the fully visible month is the one in the middle
360 states[1] = m_marks | m_holidays;
361
362 if ( !MonthCal_SetDayState(GetHwnd(), nMonths, states) )
363 {
364 wxLogLastError(_T("MonthCal_SetDayState"));
365 }
366 }
367
368 void wxCalendarCtrl::UpdateFirstDayOfWeek()
369 {
370 MonthCal_SetFirstDayOfWeek(GetHwnd(), HasFlag(wxCAL_MONDAY_FIRST) ? 0 : 6);
371 }
372
373 // ----------------------------------------------------------------------------
374 // wxCalendarCtrl events
375 // ----------------------------------------------------------------------------
376
377 bool wxCalendarCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
378 {
379 NMHDR* hdr = (NMHDR *)lParam;
380 switch ( hdr->code )
381 {
382 case MCN_SELCHANGE:
383 {
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);
389
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 )
394 {
395 if ( GenerateAllChangeEvents(dateOld) )
396 {
397 // month changed, need to update the holidays if we use
398 // them
399 if ( SetHolidayAttrs() )
400 UpdateMarks();
401 }
402 }
403 }
404 break;
405
406 case MCN_GETDAYSTATE:
407 {
408 const NMDAYSTATE * const ds = (NMDAYSTATE *)lParam;
409 for ( int i = 0; i < ds->cDayState; i++ )
410 {
411 ds->prgDayState[i] = m_marks | m_holidays;
412 }
413 }
414 break;
415
416 default:
417 return wxCalendarCtrlBase::MSWOnNotify(idCtrl, lParam, result);
418 }
419
420 *result = 0;
421 return true;
422 }
423
424 void wxCalendarCtrl::MSWOnDoubleClick(wxMouseEvent& event)
425 {
426 if ( HitTest(event.GetPosition()) == wxCAL_HITTEST_DAY )
427 {
428 if ( GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED) )
429 return; // skip event.Skip() below
430 }
431
432 event.Skip();
433 }
434
435 void wxCalendarCtrl::MSWOnClick(wxMouseEvent& event)
436 {
437 // for some reason, the control doesn't get focus on its own when the user
438 // clicks in it
439 SetFocus();
440
441 event.Skip();
442 }
443
444 #endif // wxUSE_CALENDARCTRL