]> git.saurik.com Git - wxWidgets.git/blob - src/msw/calctrl.cpp
use wxWANTS_CHARS to allow arrows to work inside the control
[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 bool
48 wxCalendarCtrl::Create(wxWindow *parent,
49 wxWindowID id,
50 const wxDateTime& dt,
51 const wxPoint& pos,
52 const wxSize& size,
53 long style,
54 const wxString& name)
55 {
56 if ( !wxMSWDateControls::CheckInitialization() )
57 return false;
58
59 // we need the arrows for the navigation
60 style |= wxWANTS_CHARS;
61
62 // initialize the base class
63 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
64 return false;
65
66 // create the native control: this is a bit tricky as we want to receive
67 // double click events but the MONTHCAL_CLASS doesn't use CS_DBLCLKS style
68 // and so we create our own copy of it which does
69 static ClassRegistrar s_clsMonthCal;
70 if ( !s_clsMonthCal.IsInitialized() )
71 {
72 // get a copy of standard class and modify it
73 WNDCLASS wc;
74 if ( ::GetClassInfo(NULL, MONTHCAL_CLASS, &wc) )
75 {
76 wc.lpszClassName = wxT("_wx_SysMonthCtl32");
77 wc.style |= CS_DBLCLKS;
78 s_clsMonthCal.Register(wc);
79 }
80 else
81 {
82 wxLogLastError(_T("GetClassInfoEx(SysMonthCal32)"));
83 }
84 }
85
86 const wxChar * const clsname = s_clsMonthCal.IsRegistered()
87 ? s_clsMonthCal.GetName().wx_str()
88 : MONTHCAL_CLASS;
89
90 if ( !MSWCreateControl(clsname, wxEmptyString, pos, size) )
91 return false;
92
93 // initialize the control
94 UpdateFirstDayOfWeek();
95
96 SetDate(dt.IsValid() ? dt : wxDateTime::Today());
97
98 UpdateMarks();
99
100 Connect(wxEVT_LEFT_DCLICK,
101 wxMouseEventHandler(wxCalendarCtrl::MSWOnDoubleClick));
102
103 return true;
104 }
105
106 WXDWORD wxCalendarCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
107 {
108 WXDWORD styleMSW = wxCalendarCtrlBase::MSWGetStyle(style, exstyle);
109
110 // right now we don't support any native styles but we should add wx styles
111 // corresponding to MCS_NOTODAY, MCS_NOTODAYCIRCLE and MCS_WEEKNUMBERS
112 // probably (TODO)
113
114 // for compatibility with the other versions, just turn off today display
115 // unconditionally for now
116 styleMSW |= MCS_NOTODAY;
117
118 // we also need this style for Mark() to work
119 styleMSW |= MCS_DAYSTATE;
120
121 return styleMSW;
122 }
123
124 void wxCalendarCtrl::SetWindowStyleFlag(long style)
125 {
126 const bool hadMondayFirst = HasFlag(wxCAL_MONDAY_FIRST);
127
128 wxCalendarCtrlBase::SetWindowStyleFlag(style);
129
130 if ( HasFlag(wxCAL_MONDAY_FIRST) != hadMondayFirst )
131 UpdateFirstDayOfWeek();
132 }
133
134 // ----------------------------------------------------------------------------
135 // wxCalendarCtrl geometry
136 // ----------------------------------------------------------------------------
137
138 // TODO: handle WM_WININICHANGE
139 wxSize wxCalendarCtrl::DoGetBestSize() const
140 {
141 RECT rc;
142 if ( !GetHwnd() || !MonthCal_GetMinReqRect(GetHwnd(), &rc) )
143 {
144 return wxCalendarCtrlBase::DoGetBestSize();
145 }
146
147 const wxSize best = wxRectFromRECT(rc).GetSize() + GetWindowBorderSize();
148 CacheBestSize(best);
149 return best;
150 }
151
152 wxCalendarHitTestResult
153 wxCalendarCtrl::HitTest(const wxPoint& pos,
154 wxDateTime *date,
155 wxDateTime::WeekDay *wd)
156 {
157 WinStruct<MCHITTESTINFO> hti;
158 hti.pt.x = pos.x;
159 hti.pt.y = pos.y;
160 switch ( MonthCal_HitTest(GetHwnd(), &hti) )
161 {
162 default:
163 case MCHT_CALENDARWEEKNUM:
164 wxFAIL_MSG( "unexpected" );
165 // fall through
166
167 case MCHT_NOWHERE:
168 case MCHT_CALENDARBK:
169 case MCHT_TITLEBK:
170 case MCHT_TITLEMONTH:
171 case MCHT_TITLEYEAR:
172 return wxCAL_HITTEST_NOWHERE;
173
174 case MCHT_CALENDARDATE:
175 if ( date )
176 wxMSWDateControls::FromSystemTime(date, hti.st);
177 return wxCAL_HITTEST_DAY;
178
179 case MCHT_CALENDARDAY:
180 if ( wd )
181 {
182 *wd = wx_static_cast(wxDateTime::WeekDay, hti.st.wDayOfWeek);
183 }
184 return wxCAL_HITTEST_HEADER;
185
186 case MCHT_TITLEBTNNEXT:
187 return wxCAL_HITTEST_INCMONTH;
188
189 case MCHT_TITLEBTNPREV:
190 return wxCAL_HITTEST_DECMONTH;
191
192 case MCHT_CALENDARDATENEXT:
193 case MCHT_CALENDARDATEPREV:
194 return wxCAL_HITTEST_SURROUNDING_WEEK;
195 }
196 }
197
198 // ----------------------------------------------------------------------------
199 // wxCalendarCtrl operations
200 // ----------------------------------------------------------------------------
201
202 bool wxCalendarCtrl::SetDate(const wxDateTime& dt)
203 {
204 wxCHECK_MSG( dt.IsValid(), false, "invalid date" );
205
206 SYSTEMTIME st;
207 wxMSWDateControls::ToSystemTime(&st, dt);
208 if ( !MonthCal_SetCurSel(GetHwnd(), &st) )
209 {
210 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
211
212 return false;
213 }
214
215 m_date = dt;
216
217 return true;
218 }
219
220 wxDateTime wxCalendarCtrl::GetDate() const
221 {
222 #ifdef __WXDEBUG__
223 SYSTEMTIME st;
224 if ( !MonthCal_GetCurSel(GetHwnd(), &st) )
225 {
226 wxASSERT_MSG( !m_date.IsValid(), "mismatch between data and control" );
227
228 return wxDefaultDateTime;
229 }
230
231 wxDateTime dt;
232 wxMSWDateControls::FromSystemTime(&dt, st);
233
234 wxASSERT_MSG( dt == m_date, "mismatch between data and control" );
235 #endif // __WXDEBUG__
236
237 return m_date;
238 }
239
240 bool wxCalendarCtrl::SetDateRange(const wxDateTime& dt1, const wxDateTime& dt2)
241 {
242 SYSTEMTIME st[2];
243
244 DWORD flags = 0;
245 if ( dt1.IsValid() )
246 {
247 wxMSWDateControls::ToSystemTime(&st[0], dt1);
248 flags |= GDTR_MIN;
249 }
250
251 if ( dt2.IsValid() )
252 {
253 wxMSWDateControls::ToSystemTime(&st[1], dt2);
254 flags |= GDTR_MAX;
255 }
256
257 if ( !MonthCal_SetRange(GetHwnd(), flags, st) )
258 {
259 wxLogDebug(_T("MonthCal_SetRange() failed"));
260 }
261
262 return flags != 0;
263 }
264
265 bool wxCalendarCtrl::GetDateRange(wxDateTime *dt1, wxDateTime *dt2) const
266 {
267 SYSTEMTIME st[2];
268
269 DWORD flags = MonthCal_GetRange(GetHwnd(), st);
270 if ( dt1 )
271 {
272 if ( flags & GDTR_MIN )
273 wxMSWDateControls::FromSystemTime(dt1, st[0]);
274 else
275 *dt1 = wxDefaultDateTime;
276 }
277
278 if ( dt2 )
279 {
280 if ( flags & GDTR_MAX )
281 wxMSWDateControls::FromSystemTime(dt2, st[1]);
282 else
283 *dt2 = wxDefaultDateTime;
284 }
285
286 return flags != 0;
287 }
288
289 // ----------------------------------------------------------------------------
290 // other wxCalendarCtrl operations
291 // ----------------------------------------------------------------------------
292
293 bool wxCalendarCtrl::EnableMonthChange(bool enable)
294 {
295 if ( !wxCalendarCtrlBase::EnableMonthChange(enable) )
296 return false;
297
298 wxDateTime dtStart, dtEnd;
299 if ( !enable )
300 {
301 dtStart = GetDate();
302 dtStart.SetDay(1);
303
304 dtEnd = dtStart.GetLastMonthDay();
305 }
306 //else: leave them invalid to remove the restriction
307
308 SetDateRange(dtStart, dtEnd);
309
310 return true;
311 }
312
313 void wxCalendarCtrl::Mark(size_t day, bool mark)
314 {
315 wxCHECK_RET( day > 0 && day < 32, "invalid day" );
316
317 int mask = 1 << (day - 1);
318 if ( mark )
319 m_marks |= mask;
320 else
321 m_marks &= ~mask;
322
323 // calling Refresh() here is not enough to change the day appearance
324 UpdateMarks();
325 }
326
327 void wxCalendarCtrl::UpdateMarks()
328 {
329 MONTHDAYSTATE states[3];
330 const int nMonths = MonthCal_GetMonthRange(GetHwnd(), GMR_DAYSTATE, NULL);
331 wxCHECK_RET( nMonths <= WXSIZEOF(states), "unexpected months range" );
332
333 for ( int i = 0; i < nMonths; i++ )
334 states[i] = m_marks;
335
336 if ( !MonthCal_SetDayState(GetHwnd(), nMonths, states) )
337 {
338 wxLogLastError(_T("MonthCal_SetDayState"));
339 }
340 }
341
342 void wxCalendarCtrl::UpdateFirstDayOfWeek()
343 {
344 MonthCal_SetFirstDayOfWeek(GetHwnd(), HasFlag(wxCAL_MONDAY_FIRST) ? 0 : 6);
345 }
346
347 // ----------------------------------------------------------------------------
348 // wxCalendarCtrl events
349 // ----------------------------------------------------------------------------
350
351 bool wxCalendarCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
352 {
353 NMHDR* hdr = (NMHDR *)lParam;
354 switch ( hdr->code )
355 {
356 case MCN_SELCHANGE:
357 {
358 // we need to update m_date first, before calling the user code
359 // which expects GetDate() to return the new date
360 const wxDateTime dateOld = m_date;
361 const NMSELCHANGE * const sch = (NMSELCHANGE *)lParam;
362 wxMSWDateControls::FromSystemTime(&m_date, sch->stSelStart);
363
364 // changing the year or the month results in a second dummy
365 // MCN_SELCHANGE event on this system which doesn't really
366 // change anything -- filter it out
367 if ( m_date != dateOld )
368 {
369 GenerateAllChangeEvents(dateOld);
370 }
371 }
372 break;
373
374 case MCN_GETDAYSTATE:
375 {
376 const NMDAYSTATE * const ds = (NMDAYSTATE *)lParam;
377 for ( int i = 0; i < ds->cDayState; i++ )
378 {
379 ds->prgDayState[i] = m_marks;
380 }
381 }
382 break;
383
384 default:
385 return wxCalendarCtrlBase::MSWOnNotify(idCtrl, lParam, result);
386 }
387
388 *result = 0;
389 return true;
390 }
391
392 void wxCalendarCtrl::MSWOnDoubleClick(wxMouseEvent& event)
393 {
394 if ( HitTest(event.GetPosition()) == wxCAL_HITTEST_DAY )
395 {
396 if ( GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED) )
397 return; // skip event.Skip() below
398 }
399
400 event.Skip();
401 }
402
403 #endif // wxUSE_CALENDARCTRL