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