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