]> git.saurik.com Git - wxWidgets.git/blame - src/msw/calctrl.cpp
Correct making the newly inserted menu item owner drawn in some cases.
[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
51317496
VZ
6// Copyright: (C) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10// ============================================================================
11// declarations
12// ============================================================================
13
14// ----------------------------------------------------------------------------
15// headers
16// ----------------------------------------------------------------------------
17
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
21 #pragma hdrstop
22#endif
23
24#if wxUSE_CALENDARCTRL
25
26#ifndef WX_PRECOMP
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
aa7ee888 29 #include "wx/msw/private.h"
51317496
VZ
30#endif
31
32#include "wx/calctrl.h"
33
34#include "wx/msw/private/datecontrols.h"
35
18547275
VZ
36// ----------------------------------------------------------------------------
37// constants
38// ----------------------------------------------------------------------------
39
40namespace
41{
42
43// values of week days used by the native control
44enum
45{
46 MonthCal_Monday,
47 MonthCal_Tuesday,
48 MonthCal_Wednesday,
49 MonthCal_Thursday,
50 MonthCal_Friday,
51 MonthCal_Saturday,
52 MonthCal_Sunday
53};
54
55} // anonymous namespace
56
51317496
VZ
57// ============================================================================
58// implementation
59// ============================================================================
60
61// ----------------------------------------------------------------------------
62// wxCalendarCtrl creation
63// ----------------------------------------------------------------------------
64
6d9b6716
VZ
65void wxCalendarCtrl::Init()
66{
67 m_marks =
68 m_holidays = 0;
69}
70
51317496
VZ
71bool
72wxCalendarCtrl::Create(wxWindow *parent,
73 wxWindowID id,
74 const wxDateTime& dt,
75 const wxPoint& pos,
76 const wxSize& size,
77 long style,
78 const wxString& name)
79{
80 if ( !wxMSWDateControls::CheckInitialization() )
81 return false;
82
d6f04127
VZ
83 // we need the arrows for the navigation
84 style |= wxWANTS_CHARS;
85
51317496
VZ
86 // initialize the base class
87 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
88 return false;
89
b3ed7020
VZ
90 // create the native control: this is a bit tricky as we want to receive
91 // double click events but the MONTHCAL_CLASS doesn't use CS_DBLCLKS style
92 // and so we create our own copy of it which does
93 static ClassRegistrar s_clsMonthCal;
94 if ( !s_clsMonthCal.IsInitialized() )
95 {
96 // get a copy of standard class and modify it
97 WNDCLASS wc;
98 if ( ::GetClassInfo(NULL, MONTHCAL_CLASS, &wc) )
99 {
100 wc.lpszClassName = wxT("_wx_SysMonthCtl32");
101 wc.style |= CS_DBLCLKS;
102 s_clsMonthCal.Register(wc);
103 }
104 else
105 {
9a83f860 106 wxLogLastError(wxT("GetClassInfoEx(SysMonthCal32)"));
b3ed7020
VZ
107 }
108 }
109
110 const wxChar * const clsname = s_clsMonthCal.IsRegistered()
017dc06b 111 ? s_clsMonthCal.GetName().t_str()
b3ed7020
VZ
112 : MONTHCAL_CLASS;
113
114 if ( !MSWCreateControl(clsname, wxEmptyString, pos, size) )
51317496
VZ
115 return false;
116
03647350 117 // initialize the control
db0b0942
VZ
118 UpdateFirstDayOfWeek();
119
51317496
VZ
120 SetDate(dt.IsValid() ? dt : wxDateTime::Today());
121
169b83dd
VZ
122 SetHolidayAttrs();
123 UpdateMarks();
82c6027b 124
3ccd1b49
VZ
125 Connect(wxEVT_LEFT_DOWN,
126 wxMouseEventHandler(wxCalendarCtrl::MSWOnClick));
82c6027b 127 Connect(wxEVT_LEFT_DCLICK,
b3ed7020
VZ
128 wxMouseEventHandler(wxCalendarCtrl::MSWOnDoubleClick));
129
51317496
VZ
130 return true;
131}
132
133WXDWORD wxCalendarCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
134{
135 WXDWORD styleMSW = wxCalendarCtrlBase::MSWGetStyle(style, exstyle);
136
bf9b73bb
VZ
137 // right now we don't support all native styles but we should add wx styles
138 // corresponding to MCS_NOTODAY and MCS_NOTODAYCIRCLE probably (TODO)
51317496
VZ
139
140 // for compatibility with the other versions, just turn off today display
141 // unconditionally for now
142 styleMSW |= MCS_NOTODAY;
143
82c6027b
VZ
144 // we also need this style for Mark() to work
145 styleMSW |= MCS_DAYSTATE;
146
bf9b73bb
VZ
147 if ( style & wxCAL_SHOW_WEEK_NUMBERS )
148 styleMSW |= MCS_WEEKNUMBERS;
149
51317496
VZ
150 return styleMSW;
151}
152
db0b0942
VZ
153void wxCalendarCtrl::SetWindowStyleFlag(long style)
154{
155 const bool hadMondayFirst = HasFlag(wxCAL_MONDAY_FIRST);
156
157 wxCalendarCtrlBase::SetWindowStyleFlag(style);
158
159 if ( HasFlag(wxCAL_MONDAY_FIRST) != hadMondayFirst )
160 UpdateFirstDayOfWeek();
161}
51317496
VZ
162
163// ----------------------------------------------------------------------------
164// wxCalendarCtrl geometry
165// ----------------------------------------------------------------------------
166
db0b0942 167// TODO: handle WM_WININICHANGE
51317496
VZ
168wxSize wxCalendarCtrl::DoGetBestSize() const
169{
170 RECT rc;
171 if ( !GetHwnd() || !MonthCal_GetMinReqRect(GetHwnd(), &rc) )
172 {
173 return wxCalendarCtrlBase::DoGetBestSize();
174 }
175
176 const wxSize best = wxRectFromRECT(rc).GetSize() + GetWindowBorderSize();
177 CacheBestSize(best);
178 return best;
179}
180
ee22a3a2
VZ
181wxCalendarHitTestResult
182wxCalendarCtrl::HitTest(const wxPoint& pos,
183 wxDateTime *date,
184 wxDateTime::WeekDay *wd)
185{
186 WinStruct<MCHITTESTINFO> hti;
f9d2e19d
VZ
187
188 // Vista and later SDKs add a few extra fields to MCHITTESTINFO which are
189 // not supported by the previous versions, as we don't use them anyhow we
190 // should pretend that we always use the old struct format to make the call
191 // below work on pre-Vista systems (see #11057)
192#ifdef MCHITTESTINFO_V1_SIZE
193 hti.cbSize = MCHITTESTINFO_V1_SIZE;
194#endif
195
ee22a3a2
VZ
196 hti.pt.x = pos.x;
197 hti.pt.y = pos.y;
198 switch ( MonthCal_HitTest(GetHwnd(), &hti) )
199 {
200 default:
201 case MCHT_CALENDARWEEKNUM:
202 wxFAIL_MSG( "unexpected" );
203 // fall through
204
205 case MCHT_NOWHERE:
206 case MCHT_CALENDARBK:
207 case MCHT_TITLEBK:
208 case MCHT_TITLEMONTH:
209 case MCHT_TITLEYEAR:
210 return wxCAL_HITTEST_NOWHERE;
211
212 case MCHT_CALENDARDATE:
213 if ( date )
10acc3ef 214 date->SetFromMSWSysDate(hti.st);
ee22a3a2
VZ
215 return wxCAL_HITTEST_DAY;
216
217 case MCHT_CALENDARDAY:
218 if ( wd )
219 {
1b88c4e4
VZ
220 int day = hti.st.wDayOfWeek;
221
222 // the native control returns incorrect day of the week when
223 // the first day isn't Monday, i.e. the first column is always
224 // "Monday" even if its label is "Sunday", compensate for it
225 const int first = LOWORD(MonthCal_GetFirstDayOfWeek(GetHwnd()));
226 if ( first == MonthCal_Monday )
227 {
228 // as MonthCal_Monday is 0 while wxDateTime::Mon is 1,
229 // normally we need to do this to transform from MSW
230 // convention to wx one
231 day++;
232 day %= 7;
233 }
234 //else: but when the first day is MonthCal_Sunday, the native
235 // control still returns 0 (i.e. MonthCal_Monday) for the
236 // first column which looks like a bug in it but to work
237 // around it it's enough to not apply the correction above
238
239 *wd = static_cast<wxDateTime::WeekDay>(day);
ee22a3a2
VZ
240 }
241 return wxCAL_HITTEST_HEADER;
242
243 case MCHT_TITLEBTNNEXT:
244 return wxCAL_HITTEST_INCMONTH;
245
246 case MCHT_TITLEBTNPREV:
247 return wxCAL_HITTEST_DECMONTH;
248
249 case MCHT_CALENDARDATENEXT:
250 case MCHT_CALENDARDATEPREV:
251 return wxCAL_HITTEST_SURROUNDING_WEEK;
252 }
253}
254
51317496
VZ
255// ----------------------------------------------------------------------------
256// wxCalendarCtrl operations
257// ----------------------------------------------------------------------------
258
259bool wxCalendarCtrl::SetDate(const wxDateTime& dt)
260{
261 wxCHECK_MSG( dt.IsValid(), false, "invalid date" );
6331afb0 262
51317496 263 SYSTEMTIME st;
10acc3ef 264 dt.GetAsMSWSysDate(&st);
51317496
VZ
265 if ( !MonthCal_SetCurSel(GetHwnd(), &st) )
266 {
9a83f860 267 wxLogDebug(wxT("DateTime_SetSystemtime() failed"));
51317496
VZ
268
269 return false;
270 }
271
10acc3ef 272 m_date = dt.GetDateOnly();
a4fcd589 273
51317496
VZ
274 return true;
275}
276
277wxDateTime wxCalendarCtrl::GetDate() const
278{
4b6a582b 279#if wxDEBUG_LEVEL
51317496 280 SYSTEMTIME st;
10acc3ef 281
51317496 282 if ( !MonthCal_GetCurSel(GetHwnd(), &st) )
a4fcd589
VZ
283 {
284 wxASSERT_MSG( !m_date.IsValid(), "mismatch between data and control" );
285
51317496 286 return wxDefaultDateTime;
a4fcd589 287 }
51317496 288
10acc3ef
VZ
289 wxDateTime dt;
290 dt.SetFromMSWSysDate(st);
a4fcd589 291
6331afb0
VZ
292 // Windows XP and earlier didn't include the time component into the
293 // returned date but Windows 7 does, so we can't compare the full objects
294 // in the same way under all the Windows versions, just compare their date
295 // parts
10acc3ef 296 wxASSERT_MSG( dt.IsSameDate(m_date), "mismatch between data and control" );
4b6a582b 297#endif // wxDEBUG_LEVEL
a4fcd589
VZ
298
299 return m_date;
51317496
VZ
300}
301
302bool wxCalendarCtrl::SetDateRange(const wxDateTime& dt1, const wxDateTime& dt2)
303{
304 SYSTEMTIME st[2];
305
306 DWORD flags = 0;
307 if ( dt1.IsValid() )
308 {
154014d6 309 dt1.GetAsMSWSysTime(st + 0);
51317496
VZ
310 flags |= GDTR_MIN;
311 }
312
313 if ( dt2.IsValid() )
314 {
154014d6 315 dt2.GetAsMSWSysTime(st + 1);
51317496
VZ
316 flags |= GDTR_MAX;
317 }
318
319 if ( !MonthCal_SetRange(GetHwnd(), flags, st) )
320 {
9a83f860 321 wxLogDebug(wxT("MonthCal_SetRange() failed"));
51317496
VZ
322 }
323
324 return flags != 0;
325}
326
327bool wxCalendarCtrl::GetDateRange(wxDateTime *dt1, wxDateTime *dt2) const
328{
329 SYSTEMTIME st[2];
330
331 DWORD flags = MonthCal_GetRange(GetHwnd(), st);
332 if ( dt1 )
333 {
334 if ( flags & GDTR_MIN )
10acc3ef 335 dt1->SetFromMSWSysDate(st[0]);
51317496
VZ
336 else
337 *dt1 = wxDefaultDateTime;
338 }
339
340 if ( dt2 )
341 {
342 if ( flags & GDTR_MAX )
10acc3ef 343 dt2->SetFromMSWSysDate(st[1]);
51317496
VZ
344 else
345 *dt2 = wxDefaultDateTime;
346 }
347
348 return flags != 0;
349}
350
351// ----------------------------------------------------------------------------
352// other wxCalendarCtrl operations
353// ----------------------------------------------------------------------------
354
355bool wxCalendarCtrl::EnableMonthChange(bool enable)
356{
7ec5c42e
VZ
357 if ( !wxCalendarCtrlBase::EnableMonthChange(enable) )
358 return false;
359
360 wxDateTime dtStart, dtEnd;
361 if ( !enable )
362 {
363 dtStart = GetDate();
364 dtStart.SetDay(1);
51317496 365
7ec5c42e
VZ
366 dtEnd = dtStart.GetLastMonthDay();
367 }
368 //else: leave them invalid to remove the restriction
369
370 SetDateRange(dtStart, dtEnd);
371
372 return true;
51317496
VZ
373}
374
375void wxCalendarCtrl::Mark(size_t day, bool mark)
376{
82c6027b
VZ
377 wxCHECK_RET( day > 0 && day < 32, "invalid day" );
378
379 int mask = 1 << (day - 1);
380 if ( mark )
381 m_marks |= mask;
382 else
383 m_marks &= ~mask;
384
385 // calling Refresh() here is not enough to change the day appearance
386 UpdateMarks();
387}
388
6d9b6716
VZ
389void wxCalendarCtrl::SetHoliday(size_t day)
390{
391 wxCHECK_RET( day > 0 && day < 32, "invalid day" );
392
393 m_holidays |= 1 << (day - 1);
394}
395
82c6027b
VZ
396void wxCalendarCtrl::UpdateMarks()
397{
c3d3028a
VZ
398 // Currently the native control may show more than one month if its size is
399 // big enough. Ideal would be to prevent this from happening but there
400 // doesn't seem to be any obvious way to do it, so for now just handle the
401 // possibility that we can display several of them: one before the current
402 // one and up to 12 after it.
403 MONTHDAYSTATE states[14] = { 0 };
9573840b 404 const DWORD nMonths = MonthCal_GetMonthRange(GetHwnd(), GMR_DAYSTATE, NULL);
82c6027b 405
52980340
VZ
406 // although in principle the calendar might not show any days from the
407 // preceding months, it seems like it always does, consider e.g. Feb 2010
408 // which starts on Monday and ends on Sunday and so could fit on 4 lines
409 // without showing any subsequent months -- the standard control still
410 // shows it on 6 lines and the number of visible months is still 3
9573840b
VZ
411 //
412 // OTOH Windows 7 control can show all 12 months or even years or decades
413 // in its window if you "zoom out" of it by double clicking on free areas
414 // so the return value can be (much, in case of decades view) greater than
415 // 3 but in this case marks are not visible anyhow so simply ignore it
c3d3028a 416 if ( nMonths >= 2 && nMonths <= WXSIZEOF(states) )
9573840b 417 {
c3d3028a 418 // The current, fully visible month is always the second one.
9573840b
VZ
419 states[1] = m_marks | m_holidays;
420
421 if ( !MonthCal_SetDayState(GetHwnd(), nMonths, states) )
422 {
423 wxLogLastError(wxT("MonthCal_SetDayState"));
424 }
82c6027b 425 }
9573840b 426 //else: not a month view at all
51317496
VZ
427}
428
db0b0942
VZ
429void wxCalendarCtrl::UpdateFirstDayOfWeek()
430{
18547275
VZ
431 MonthCal_SetFirstDayOfWeek(GetHwnd(),
432 HasFlag(wxCAL_MONDAY_FIRST) ? MonthCal_Monday
433 : MonthCal_Sunday);
db0b0942
VZ
434}
435
51317496
VZ
436// ----------------------------------------------------------------------------
437// wxCalendarCtrl events
438// ----------------------------------------------------------------------------
439
440bool wxCalendarCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
441{
442 NMHDR* hdr = (NMHDR *)lParam;
443 switch ( hdr->code )
444 {
a4fcd589 445 case MCN_SELCHANGE:
a4fcd589 446 {
82c6027b
VZ
447 // we need to update m_date first, before calling the user code
448 // which expects GetDate() to return the new date
449 const wxDateTime dateOld = m_date;
450 const NMSELCHANGE * const sch = (NMSELCHANGE *)lParam;
10acc3ef 451 m_date.SetFromMSWSysDate(sch->stSelStart);
82c6027b
VZ
452
453 // changing the year or the month results in a second dummy
454 // MCN_SELCHANGE event on this system which doesn't really
455 // change anything -- filter it out
456 if ( m_date != dateOld )
457 {
6d9b6716
VZ
458 if ( GenerateAllChangeEvents(dateOld) )
459 {
460 // month changed, need to update the holidays if we use
461 // them
169b83dd
VZ
462 SetHolidayAttrs();
463 UpdateMarks();
6d9b6716 464 }
82c6027b
VZ
465 }
466 }
467 break;
a4fcd589 468
82c6027b
VZ
469 case MCN_GETDAYSTATE:
470 {
471 const NMDAYSTATE * const ds = (NMDAYSTATE *)lParam;
c3d3028a
VZ
472
473 wxDateTime startDate;
474 startDate.SetFromMSWSysDate(ds->stStart);
475
4f7222ca
VZ
476 // Ensure we have a valid date to work with.
477 wxDateTime currentDate = m_date.IsValid() ? m_date : startDate;
478
c3d3028a
VZ
479 // Set to the start of month for comparison with startDate to
480 // work correctly.
481 currentDate.SetDay(1);
482
82c6027b
VZ
483 for ( int i = 0; i < ds->cDayState; i++ )
484 {
c3d3028a
VZ
485 // set holiday/marks only for the "current" month
486 if ( startDate == currentDate )
487 ds->prgDayState[i] = m_marks | m_holidays;
488 else
489 ds->prgDayState[i] = 0;
490
491 startDate += wxDateSpan::Month();
82c6027b 492 }
a4fcd589 493 }
82c6027b
VZ
494 break;
495
496 default:
497 return wxCalendarCtrlBase::MSWOnNotify(idCtrl, lParam, result);
51317496
VZ
498 }
499
82c6027b
VZ
500 *result = 0;
501 return true;
51317496
VZ
502}
503
b3ed7020
VZ
504void wxCalendarCtrl::MSWOnDoubleClick(wxMouseEvent& event)
505{
506 if ( HitTest(event.GetPosition()) == wxCAL_HITTEST_DAY )
507 {
508 if ( GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED) )
509 return; // skip event.Skip() below
510 }
511
512 event.Skip();
513}
514
3ccd1b49
VZ
515void wxCalendarCtrl::MSWOnClick(wxMouseEvent& event)
516{
517 // for some reason, the control doesn't get focus on its own when the user
518 // clicks in it
519 SetFocus();
520
521 event.Skip();
522}
523
51317496 524#endif // wxUSE_CALENDARCTRL