]> git.saurik.com Git - wxWidgets.git/blame - src/msw/calctrl.cpp
Forward-ported wxPython-bindings related cleanup from 2.9.0 branch
[wxWidgets.git] / src / msw / calctrl.cpp
CommitLineData
51317496
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/msw/calctrl.cpp
3// Purpose: wxCalendarCtrl implementation
4// Author: Vadim Zeitlin
5// Created: 2008-04-04
aa7ee888 6// RCS-ID: $Id$
51317496
VZ
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"
aa7ee888 30 #include "wx/msw/private.h"
51317496
VZ
31#endif
32
33#include "wx/calctrl.h"
34
35#include "wx/msw/private/datecontrols.h"
36
37IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl, wxControl)
38
39// ============================================================================
40// implementation
41// ============================================================================
42
43// ----------------------------------------------------------------------------
44// wxCalendarCtrl creation
45// ----------------------------------------------------------------------------
46
6d9b6716
VZ
47void wxCalendarCtrl::Init()
48{
49 m_marks =
50 m_holidays = 0;
51}
52
51317496
VZ
53bool
54wxCalendarCtrl::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
d6f04127
VZ
65 // we need the arrows for the navigation
66 style |= wxWANTS_CHARS;
67
51317496
VZ
68 // initialize the base class
69 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
70 return false;
71
b3ed7020
VZ
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) )
51317496
VZ
97 return false;
98
db0b0942
VZ
99 // initialize the control
100 UpdateFirstDayOfWeek();
101
51317496
VZ
102 SetDate(dt.IsValid() ? dt : wxDateTime::Today());
103
6d9b6716
VZ
104 if ( SetHolidayAttrs() )
105 UpdateMarks();
82c6027b 106
3ccd1b49
VZ
107 Connect(wxEVT_LEFT_DOWN,
108 wxMouseEventHandler(wxCalendarCtrl::MSWOnClick));
82c6027b 109 Connect(wxEVT_LEFT_DCLICK,
b3ed7020
VZ
110 wxMouseEventHandler(wxCalendarCtrl::MSWOnDoubleClick));
111
51317496
VZ
112 return true;
113}
114
115WXDWORD wxCalendarCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
116{
117 WXDWORD styleMSW = wxCalendarCtrlBase::MSWGetStyle(style, exstyle);
118
bf9b73bb
VZ
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)
51317496
VZ
121
122 // for compatibility with the other versions, just turn off today display
123 // unconditionally for now
124 styleMSW |= MCS_NOTODAY;
125
82c6027b
VZ
126 // we also need this style for Mark() to work
127 styleMSW |= MCS_DAYSTATE;
128
bf9b73bb
VZ
129 if ( style & wxCAL_SHOW_WEEK_NUMBERS )
130 styleMSW |= MCS_WEEKNUMBERS;
131
51317496
VZ
132 return styleMSW;
133}
134
db0b0942
VZ
135void 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}
51317496
VZ
144
145// ----------------------------------------------------------------------------
146// wxCalendarCtrl geometry
147// ----------------------------------------------------------------------------
148
db0b0942 149// TODO: handle WM_WININICHANGE
51317496
VZ
150wxSize 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
ee22a3a2
VZ
163wxCalendarHitTestResult
164wxCalendarCtrl::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 )
154014d6 187 date->SetFromMSWSysTime(hti.st);
ee22a3a2
VZ
188 return wxCAL_HITTEST_DAY;
189
190 case MCHT_CALENDARDAY:
191 if ( wd )
192 {
5c33522f 193 *wd = static_cast<wxDateTime::WeekDay>(hti.st.wDayOfWeek);
ee22a3a2
VZ
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
51317496
VZ
209// ----------------------------------------------------------------------------
210// wxCalendarCtrl operations
211// ----------------------------------------------------------------------------
212
213bool wxCalendarCtrl::SetDate(const wxDateTime& dt)
214{
215 wxCHECK_MSG( dt.IsValid(), false, "invalid date" );
216
217 SYSTEMTIME st;
154014d6 218 dt.GetAsMSWSysTime(&st);
51317496
VZ
219 if ( !MonthCal_SetCurSel(GetHwnd(), &st) )
220 {
221 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
222
223 return false;
224 }
225
a4fcd589
VZ
226 m_date = dt;
227
51317496
VZ
228 return true;
229}
230
231wxDateTime wxCalendarCtrl::GetDate() const
232{
4b6a582b 233#if wxDEBUG_LEVEL
51317496
VZ
234 SYSTEMTIME st;
235 if ( !MonthCal_GetCurSel(GetHwnd(), &st) )
a4fcd589
VZ
236 {
237 wxASSERT_MSG( !m_date.IsValid(), "mismatch between data and control" );
238
51317496 239 return wxDefaultDateTime;
a4fcd589 240 }
51317496 241
154014d6 242 wxDateTime dt(st);
a4fcd589
VZ
243
244 wxASSERT_MSG( dt == m_date, "mismatch between data and control" );
4b6a582b 245#endif // wxDEBUG_LEVEL
a4fcd589
VZ
246
247 return m_date;
51317496
VZ
248}
249
250bool wxCalendarCtrl::SetDateRange(const wxDateTime& dt1, const wxDateTime& dt2)
251{
252 SYSTEMTIME st[2];
253
254 DWORD flags = 0;
255 if ( dt1.IsValid() )
256 {
154014d6 257 dt1.GetAsMSWSysTime(st + 0);
51317496
VZ
258 flags |= GDTR_MIN;
259 }
260
261 if ( dt2.IsValid() )
262 {
154014d6 263 dt2.GetAsMSWSysTime(st + 1);
51317496
VZ
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
275bool 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 )
154014d6 283 dt1->SetFromMSWSysTime(st[0]);
51317496
VZ
284 else
285 *dt1 = wxDefaultDateTime;
286 }
287
288 if ( dt2 )
289 {
290 if ( flags & GDTR_MAX )
154014d6 291 dt2->SetFromMSWSysTime(st[1]);
51317496
VZ
292 else
293 *dt2 = wxDefaultDateTime;
294 }
295
296 return flags != 0;
297}
298
299// ----------------------------------------------------------------------------
300// other wxCalendarCtrl operations
301// ----------------------------------------------------------------------------
302
303bool wxCalendarCtrl::EnableMonthChange(bool enable)
304{
7ec5c42e
VZ
305 if ( !wxCalendarCtrlBase::EnableMonthChange(enable) )
306 return false;
307
308 wxDateTime dtStart, dtEnd;
309 if ( !enable )
310 {
311 dtStart = GetDate();
312 dtStart.SetDay(1);
51317496 313
7ec5c42e
VZ
314 dtEnd = dtStart.GetLastMonthDay();
315 }
316 //else: leave them invalid to remove the restriction
317
318 SetDateRange(dtStart, dtEnd);
319
320 return true;
51317496
VZ
321}
322
323void wxCalendarCtrl::Mark(size_t day, bool mark)
324{
82c6027b
VZ
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
6d9b6716
VZ
337void wxCalendarCtrl::SetHoliday(size_t day)
338{
339 wxCHECK_RET( day > 0 && day < 32, "invalid day" );
340
341 m_holidays |= 1 << (day - 1);
342}
343
82c6027b
VZ
344void wxCalendarCtrl::UpdateMarks()
345{
52980340
VZ
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 };
82c6027b 350 const int nMonths = MonthCal_GetMonthRange(GetHwnd(), GMR_DAYSTATE, NULL);
82c6027b 351
52980340
VZ
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
6d9b6716
VZ
359 // the fully visible month is the one in the middle
360 states[1] = m_marks | m_holidays;
82c6027b
VZ
361
362 if ( !MonthCal_SetDayState(GetHwnd(), nMonths, states) )
363 {
364 wxLogLastError(_T("MonthCal_SetDayState"));
365 }
51317496
VZ
366}
367
db0b0942
VZ
368void wxCalendarCtrl::UpdateFirstDayOfWeek()
369{
370 MonthCal_SetFirstDayOfWeek(GetHwnd(), HasFlag(wxCAL_MONDAY_FIRST) ? 0 : 6);
371}
372
51317496
VZ
373// ----------------------------------------------------------------------------
374// wxCalendarCtrl events
375// ----------------------------------------------------------------------------
376
377bool wxCalendarCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
378{
379 NMHDR* hdr = (NMHDR *)lParam;
380 switch ( hdr->code )
381 {
a4fcd589 382 case MCN_SELCHANGE:
a4fcd589 383 {
82c6027b
VZ
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;
154014d6 388 m_date.SetFromMSWSysTime(sch->stSelStart);
82c6027b
VZ
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 {
6d9b6716
VZ
395 if ( GenerateAllChangeEvents(dateOld) )
396 {
397 // month changed, need to update the holidays if we use
398 // them
399 if ( SetHolidayAttrs() )
400 UpdateMarks();
401 }
82c6027b
VZ
402 }
403 }
404 break;
a4fcd589 405
82c6027b
VZ
406 case MCN_GETDAYSTATE:
407 {
408 const NMDAYSTATE * const ds = (NMDAYSTATE *)lParam;
409 for ( int i = 0; i < ds->cDayState; i++ )
410 {
6d9b6716 411 ds->prgDayState[i] = m_marks | m_holidays;
82c6027b 412 }
a4fcd589 413 }
82c6027b
VZ
414 break;
415
416 default:
417 return wxCalendarCtrlBase::MSWOnNotify(idCtrl, lParam, result);
51317496
VZ
418 }
419
82c6027b
VZ
420 *result = 0;
421 return true;
51317496
VZ
422}
423
b3ed7020
VZ
424void 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
3ccd1b49
VZ
435void 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
51317496 444#endif // wxUSE_CALENDARCTRL