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