]> git.saurik.com Git - wxWidgets.git/blame - src/msw/calctrl.cpp
fix crashes due to missing npos handling in several wxString methods in STL build...
[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
47bool
48wxCalendarCtrl::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
d6f04127
VZ
59 // we need the arrows for the navigation
60 style |= wxWANTS_CHARS;
61
51317496
VZ
62 // initialize the base class
63 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
64 return false;
65
b3ed7020
VZ
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) )
51317496
VZ
91 return false;
92
db0b0942
VZ
93 // initialize the control
94 UpdateFirstDayOfWeek();
95
51317496
VZ
96 SetDate(dt.IsValid() ? dt : wxDateTime::Today());
97
82c6027b
VZ
98 UpdateMarks();
99
3ccd1b49
VZ
100 Connect(wxEVT_LEFT_DOWN,
101 wxMouseEventHandler(wxCalendarCtrl::MSWOnClick));
82c6027b 102 Connect(wxEVT_LEFT_DCLICK,
b3ed7020
VZ
103 wxMouseEventHandler(wxCalendarCtrl::MSWOnDoubleClick));
104
51317496
VZ
105 return true;
106}
107
108WXDWORD 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
82c6027b
VZ
120 // we also need this style for Mark() to work
121 styleMSW |= MCS_DAYSTATE;
122
51317496
VZ
123 return styleMSW;
124}
125
db0b0942
VZ
126void 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}
51317496
VZ
135
136// ----------------------------------------------------------------------------
137// wxCalendarCtrl geometry
138// ----------------------------------------------------------------------------
139
db0b0942 140// TODO: handle WM_WININICHANGE
51317496
VZ
141wxSize 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
ee22a3a2
VZ
154wxCalendarHitTestResult
155wxCalendarCtrl::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
51317496
VZ
200// ----------------------------------------------------------------------------
201// wxCalendarCtrl operations
202// ----------------------------------------------------------------------------
203
204bool 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
a4fcd589
VZ
217 m_date = dt;
218
51317496
VZ
219 return true;
220}
221
222wxDateTime wxCalendarCtrl::GetDate() const
223{
a4fcd589 224#ifdef __WXDEBUG__
51317496
VZ
225 SYSTEMTIME st;
226 if ( !MonthCal_GetCurSel(GetHwnd(), &st) )
a4fcd589
VZ
227 {
228 wxASSERT_MSG( !m_date.IsValid(), "mismatch between data and control" );
229
51317496 230 return wxDefaultDateTime;
a4fcd589 231 }
51317496
VZ
232
233 wxDateTime dt;
234 wxMSWDateControls::FromSystemTime(&dt, st);
a4fcd589
VZ
235
236 wxASSERT_MSG( dt == m_date, "mismatch between data and control" );
237#endif // __WXDEBUG__
238
239 return m_date;
51317496
VZ
240}
241
242bool 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
267bool 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
295bool wxCalendarCtrl::EnableMonthChange(bool enable)
296{
7ec5c42e
VZ
297 if ( !wxCalendarCtrlBase::EnableMonthChange(enable) )
298 return false;
299
300 wxDateTime dtStart, dtEnd;
301 if ( !enable )
302 {
303 dtStart = GetDate();
304 dtStart.SetDay(1);
51317496 305
7ec5c42e
VZ
306 dtEnd = dtStart.GetLastMonthDay();
307 }
308 //else: leave them invalid to remove the restriction
309
310 SetDateRange(dtStart, dtEnd);
311
312 return true;
51317496
VZ
313}
314
315void wxCalendarCtrl::Mark(size_t day, bool mark)
316{
82c6027b
VZ
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
329void wxCalendarCtrl::UpdateMarks()
330{
331 MONTHDAYSTATE states[3];
332 const int nMonths = MonthCal_GetMonthRange(GetHwnd(), GMR_DAYSTATE, NULL);
c912c940 333 wxCHECK_RET( nMonths <= (int)WXSIZEOF(states), "unexpected months range" );
82c6027b
VZ
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 }
51317496
VZ
342}
343
db0b0942
VZ
344void wxCalendarCtrl::UpdateFirstDayOfWeek()
345{
346 MonthCal_SetFirstDayOfWeek(GetHwnd(), HasFlag(wxCAL_MONDAY_FIRST) ? 0 : 6);
347}
348
51317496
VZ
349// ----------------------------------------------------------------------------
350// wxCalendarCtrl events
351// ----------------------------------------------------------------------------
352
353bool wxCalendarCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
354{
355 NMHDR* hdr = (NMHDR *)lParam;
356 switch ( hdr->code )
357 {
a4fcd589 358 case MCN_SELCHANGE:
a4fcd589 359 {
82c6027b
VZ
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;
a4fcd589 375
82c6027b
VZ
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 }
a4fcd589 383 }
82c6027b
VZ
384 break;
385
386 default:
387 return wxCalendarCtrlBase::MSWOnNotify(idCtrl, lParam, result);
51317496
VZ
388 }
389
82c6027b
VZ
390 *result = 0;
391 return true;
51317496
VZ
392}
393
b3ed7020
VZ
394void 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
3ccd1b49
VZ
405void 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
51317496 414#endif // wxUSE_CALENDARCTRL