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